rs6000-protos.h (rs6000_initial_elimination_offset): Add.
[platform/upstream/gcc.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 /* Structure used to define the rs6000 stack */
74 typedef struct rs6000_stack {
75   int first_gp_reg_save;        /* first callee saved GP register used */
76   int first_fp_reg_save;        /* first callee saved FP register used */
77   int first_altivec_reg_save;   /* first callee saved AltiVec register used */
78   int lr_save_p;                /* true if the link reg needs to be saved */
79   int cr_save_p;                /* true if the CR reg needs to be saved */
80   unsigned int vrsave_mask;     /* mask of vec registers to save */
81   int toc_save_p;               /* true if the TOC needs to be saved */
82   int push_p;                   /* true if we need to allocate stack space */
83   int calls_p;                  /* true if the function makes any calls */
84   enum rs6000_abi abi;          /* which ABI to use */
85   int gp_save_offset;           /* offset to save GP regs from initial SP */
86   int fp_save_offset;           /* offset to save FP regs from initial SP */
87   int altivec_save_offset;      /* offset to save AltiVec regs from initial SP */
88   int lr_save_offset;           /* offset to save LR from initial SP */
89   int cr_save_offset;           /* offset to save CR from initial SP */
90   int vrsave_save_offset;       /* offset to save VRSAVE from initial SP */
91   int spe_gp_save_offset;       /* offset to save spe 64-bit gprs  */
92   int toc_save_offset;          /* offset to save the TOC pointer */
93   int varargs_save_offset;      /* offset to save the varargs registers */
94   int ehrd_offset;              /* offset to EH return data */
95   int reg_size;                 /* register size (4 or 8) */
96   int varargs_size;             /* size to hold V.4 args passed in regs */
97   HOST_WIDE_INT vars_size;      /* variable save area size */
98   int parm_size;                /* outgoing parameter size */
99   int save_size;                /* save area size */
100   int fixed_size;               /* fixed size of stack frame */
101   int gp_size;                  /* size of saved GP registers */
102   int fp_size;                  /* size of saved FP registers */
103   int altivec_size;             /* size of saved AltiVec registers */
104   int cr_size;                  /* size to hold CR if not in save_size */
105   int lr_size;                  /* size to hold LR if not in save_size */
106   int vrsave_size;              /* size to hold VRSAVE if not in save_size */
107   int altivec_padding_size;     /* size of altivec alignment padding if
108                                    not in save_size */
109   int spe_gp_size;              /* size of 64-bit GPR save size for SPE */
110   int spe_padding_size;
111   int toc_size;                 /* size to hold TOC if not in save_size */
112   HOST_WIDE_INT total_size;     /* total bytes allocated for stack */
113   int spe_64bit_regs_used;
114 } rs6000_stack_t;
115
116 /* Target cpu type */
117
118 enum processor_type rs6000_cpu;
119 struct rs6000_cpu_select rs6000_select[3] =
120 {
121   /* switch             name,                   tune    arch */
122   { (const char *)0,    "--with-cpu=",          1,      1 },
123   { (const char *)0,    "-mcpu=",               1,      1 },
124   { (const char *)0,    "-mtune=",              1,      0 },
125 };
126
127 /* Support adjust_priority scheduler hook 
128    and -mprioritize-restricted-insns= option.  */
129 const char *rs6000_sched_restricted_insns_priority_str;
130 int rs6000_sched_restricted_insns_priority;
131
132 /* Support for -msched-costly-dep option.  */
133 const char *rs6000_sched_costly_dep_str;
134 enum rs6000_dependence_cost rs6000_sched_costly_dep;
135
136 /* Support for -minsert-sched-nops option.  */
137 const char *rs6000_sched_insert_nops_str;
138 enum rs6000_nop_insertion rs6000_sched_insert_nops;
139
140 /* Size of long double */
141 const char *rs6000_long_double_size_string;
142 int rs6000_long_double_type_size;
143
144 /* Whether -mabi=altivec has appeared */
145 int rs6000_altivec_abi;
146
147 /* Whether VRSAVE instructions should be generated.  */
148 int rs6000_altivec_vrsave;
149
150 /* String from -mvrsave= option.  */
151 const char *rs6000_altivec_vrsave_string;
152
153 /* Nonzero if we want SPE ABI extensions.  */
154 int rs6000_spe_abi;
155
156 /* Whether isel instructions should be generated.  */
157 int rs6000_isel;
158
159 /* Whether SPE simd instructions should be generated.  */
160 int rs6000_spe;
161
162 /* Nonzero if floating point operations are done in the GPRs.  */
163 int rs6000_float_gprs = 0;
164
165 /* String from -mfloat-gprs=.  */
166 const char *rs6000_float_gprs_string;
167
168 /* String from -misel=.  */
169 const char *rs6000_isel_string;
170
171 /* String from -mspe=.  */
172 const char *rs6000_spe_string;
173
174 /* Set to nonzero once AIX common-mode calls have been defined.  */
175 static GTY(()) int common_mode_defined;
176
177 /* Save information from a "cmpxx" operation until the branch or scc is
178    emitted.  */
179 rtx rs6000_compare_op0, rs6000_compare_op1;
180 int rs6000_compare_fp_p;
181
182 /* Label number of label created for -mrelocatable, to call to so we can
183    get the address of the GOT section */
184 int rs6000_pic_labelno;
185
186 #ifdef USING_ELFOS_H
187 /* Which abi to adhere to */
188 const char *rs6000_abi_name;
189
190 /* Semantics of the small data area */
191 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
192
193 /* Which small data model to use */
194 const char *rs6000_sdata_name = (char *)0;
195
196 /* Counter for labels which are to be placed in .fixup.  */
197 int fixuplabelno = 0;
198 #endif
199
200 /* Bit size of immediate TLS offsets and string from which it is decoded.  */
201 int rs6000_tls_size = 32;
202 const char *rs6000_tls_size_string;
203
204 /* ABI enumeration available for subtarget to use.  */
205 enum rs6000_abi rs6000_current_abi;
206
207 /* ABI string from -mabi= option.  */
208 const char *rs6000_abi_string;
209
210 /* Debug flags */
211 const char *rs6000_debug_name;
212 int rs6000_debug_stack;         /* debug stack applications */
213 int rs6000_debug_arg;           /* debug argument handling */
214
215 /* Opaque types.  */
216 static GTY(()) tree opaque_V2SI_type_node;
217 static GTY(()) tree opaque_V2SF_type_node;
218 static GTY(()) tree opaque_p_V2SI_type_node;
219
220 const char *rs6000_traceback_name;
221 static enum {
222   traceback_default = 0,
223   traceback_none,
224   traceback_part,
225   traceback_full
226 } rs6000_traceback;
227
228 /* Flag to say the TOC is initialized */
229 int toc_initialized;
230 char toc_label_name[10];
231
232 /* Alias set for saves and restores from the rs6000 stack.  */
233 static int rs6000_sr_alias_set;
234
235 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
236    The only place that looks at this is rs6000_set_default_type_attributes;
237    everywhere else should rely on the presence or absence of a longcall
238    attribute on the function declaration.  */
239 int rs6000_default_long_calls;
240 const char *rs6000_longcall_switch;
241
242 /* Control alignment for fields within structures. */
243 /* String from -malign-XXXXX. */
244 const char *rs6000_alignment_string;
245 int rs6000_alignment_flags;
246
247 struct builtin_description
248 {
249   /* mask is not const because we're going to alter it below.  This
250      nonsense will go away when we rewrite the -march infrastructure
251      to give us more target flag bits.  */
252   unsigned int mask;
253   const enum insn_code icode;
254   const char *const name;
255   const enum rs6000_builtins code;
256 };
257
258 static bool rs6000_function_ok_for_sibcall (tree, tree);
259 static int num_insns_constant_wide (HOST_WIDE_INT);
260 static void validate_condition_mode (enum rtx_code, enum machine_mode);
261 static rtx rs6000_generate_compare (enum rtx_code);
262 static void rs6000_maybe_dead (rtx);
263 static void rs6000_emit_stack_tie (void);
264 static void rs6000_frame_related (rtx, rtx, HOST_WIDE_INT, rtx, rtx);
265 static rtx spe_synthesize_frame_save (rtx);
266 static bool spe_func_has_64bit_regs_p (void);
267 static void emit_frame_save (rtx, rtx, enum machine_mode, unsigned int,
268                              int, HOST_WIDE_INT);
269 static rtx gen_frame_mem_offset (enum machine_mode, rtx, int);
270 static void rs6000_emit_allocate_stack (HOST_WIDE_INT, int);
271 static unsigned rs6000_hash_constant (rtx);
272 static unsigned toc_hash_function (const void *);
273 static int toc_hash_eq (const void *, const void *);
274 static int constant_pool_expr_1 (rtx, int *, int *);
275 static bool constant_pool_expr_p (rtx);
276 static bool toc_relative_expr_p (rtx);
277 static bool legitimate_small_data_p (enum machine_mode, rtx);
278 static bool legitimate_offset_address_p (enum machine_mode, rtx, int);
279 static bool legitimate_indexed_address_p (rtx, int);
280 static bool legitimate_indirect_address_p (rtx, int);
281 static bool macho_lo_sum_memory_operand (rtx x, enum machine_mode mode);
282 static bool legitimate_lo_sum_address_p (enum machine_mode, rtx, int);
283 static struct machine_function * rs6000_init_machine_status (void);
284 static bool rs6000_assemble_integer (rtx, unsigned int, int);
285 #ifdef HAVE_GAS_HIDDEN
286 static void rs6000_assemble_visibility (tree, int);
287 #endif
288 static int rs6000_ra_ever_killed (void);
289 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
290 extern const struct attribute_spec rs6000_attribute_table[];
291 static void rs6000_set_default_type_attributes (tree);
292 static void rs6000_output_function_prologue (FILE *, HOST_WIDE_INT);
293 static void rs6000_output_function_epilogue (FILE *, HOST_WIDE_INT);
294 static void rs6000_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
295                                     tree);
296 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
297 static bool rs6000_return_in_memory (tree, tree);
298 static void rs6000_file_start (void);
299 #if TARGET_ELF
300 static unsigned int rs6000_elf_section_type_flags (tree, const char *, int);
301 static void rs6000_elf_asm_out_constructor (rtx, int);
302 static void rs6000_elf_asm_out_destructor (rtx, int);
303 static void rs6000_elf_select_section (tree, int, unsigned HOST_WIDE_INT);
304 static void rs6000_elf_unique_section (tree, int);
305 static void rs6000_elf_select_rtx_section (enum machine_mode, rtx,
306                                            unsigned HOST_WIDE_INT);
307 static void rs6000_elf_encode_section_info (tree, rtx, int)
308      ATTRIBUTE_UNUSED;
309 static bool rs6000_elf_in_small_data_p (tree);
310 #endif
311 #if TARGET_XCOFF
312 static void rs6000_xcoff_asm_globalize_label (FILE *, const char *);
313 static void rs6000_xcoff_asm_named_section (const char *, unsigned int);
314 static void rs6000_xcoff_select_section (tree, int, unsigned HOST_WIDE_INT);
315 static void rs6000_xcoff_unique_section (tree, int);
316 static void rs6000_xcoff_select_rtx_section (enum machine_mode, rtx,
317                                              unsigned HOST_WIDE_INT);
318 static const char * rs6000_xcoff_strip_name_encoding (const char *);
319 static unsigned int rs6000_xcoff_section_type_flags (tree, const char *, int);
320 static void rs6000_xcoff_file_start (void);
321 static void rs6000_xcoff_file_end (void);
322 #endif
323 #if TARGET_MACHO
324 static bool rs6000_binds_local_p (tree);
325 #endif
326 static int rs6000_use_dfa_pipeline_interface (void);
327 static int rs6000_variable_issue (FILE *, int, rtx, int);
328 static bool rs6000_rtx_costs (rtx, int, int, int *);
329 static int rs6000_adjust_cost (rtx, rtx, rtx, int);
330 static bool is_microcoded_insn (rtx);
331 static int is_dispatch_slot_restricted (rtx);
332 static bool is_cracked_insn (rtx);
333 static bool is_branch_slot_insn (rtx);
334 static int rs6000_adjust_priority (rtx, int);
335 static int rs6000_issue_rate (void);
336 static bool rs6000_is_costly_dependence (rtx, rtx, rtx, int, int);
337 static rtx get_next_active_insn (rtx, rtx);
338 static bool insn_terminates_group_p (rtx , enum group_termination);
339 static bool is_costly_group (rtx *, rtx);
340 static int force_new_group (int, FILE *, rtx *, rtx, bool *, int, int *);
341 static int redefine_groups (FILE *, int, rtx, rtx);
342 static int pad_groups (FILE *, int, rtx, rtx);
343 static void rs6000_sched_finish (FILE *, int);
344 static int rs6000_use_sched_lookahead (void);
345
346 static void rs6000_init_builtins (void);
347 static rtx rs6000_expand_unop_builtin (enum insn_code, tree, rtx);
348 static rtx rs6000_expand_binop_builtin (enum insn_code, tree, rtx);
349 static rtx rs6000_expand_ternop_builtin (enum insn_code, tree, rtx);
350 static rtx rs6000_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
351 static void altivec_init_builtins (void);
352 static void rs6000_common_init_builtins (void);
353 static void rs6000_init_libfuncs (void);
354
355 static void enable_mask_for_builtins (struct builtin_description *, int,
356                                       enum rs6000_builtins,
357                                       enum rs6000_builtins);
358 static void spe_init_builtins (void);
359 static rtx spe_expand_builtin (tree, rtx, bool *);
360 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
361 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
362 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
363 static rs6000_stack_t *rs6000_stack_info (void);
364 static void debug_stack_info (rs6000_stack_t *);
365
366 static rtx altivec_expand_builtin (tree, rtx, bool *);
367 static rtx altivec_expand_ld_builtin (tree, rtx, bool *);
368 static rtx altivec_expand_st_builtin (tree, rtx, bool *);
369 static rtx altivec_expand_dst_builtin (tree, rtx, bool *);
370 static rtx altivec_expand_abs_builtin (enum insn_code, tree, rtx);
371 static rtx altivec_expand_predicate_builtin (enum insn_code, 
372                                             const char *, tree, rtx);
373 static rtx altivec_expand_lv_builtin (enum insn_code, tree, rtx);
374 static rtx altivec_expand_stv_builtin (enum insn_code, tree);
375 static void rs6000_parse_abi_options (void);
376 static void rs6000_parse_alignment_option (void);
377 static void rs6000_parse_tls_size_option (void);
378 static void rs6000_parse_yes_no_option (const char *, const char *, int *);
379 static int first_altivec_reg_to_save (void);
380 static unsigned int compute_vrsave_mask (void);
381 static void is_altivec_return_reg (rtx, void *);
382 static rtx generate_set_vrsave (rtx, rs6000_stack_t *, int);
383 int easy_vector_constant (rtx, enum machine_mode);
384 static int easy_vector_same (rtx, enum machine_mode);
385 static bool is_ev64_opaque_type (tree);
386 static rtx rs6000_dwarf_register_span (rtx);
387 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
388 static rtx rs6000_tls_get_addr (void);
389 static rtx rs6000_got_sym (void);
390 static inline int rs6000_tls_symbol_ref_1 (rtx *, void *);
391 static const char *rs6000_get_some_local_dynamic_name (void);
392 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
393 static rtx rs6000_complex_function_value (enum machine_mode);
394 static rtx rs6000_spe_function_arg (CUMULATIVE_ARGS *,
395                                     enum machine_mode, tree);
396 static rtx rs6000_mixed_function_arg (CUMULATIVE_ARGS *,
397                                       enum machine_mode, tree, int);
398 static void setup_incoming_varargs (CUMULATIVE_ARGS *,
399                                     enum machine_mode, tree,
400                                     int *, int);
401 static tree rs6000_build_builtin_va_list (void);
402
403 /* Hash table stuff for keeping track of TOC entries.  */
404
405 struct toc_hash_struct GTY(())
406 {
407   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
408      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
409   rtx key;
410   enum machine_mode key_mode;
411   int labelno;
412 };
413
414 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
415 \f
416 /* Default register names.  */
417 char rs6000_reg_names[][8] =
418 {
419       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
420       "8",  "9", "10", "11", "12", "13", "14", "15",
421      "16", "17", "18", "19", "20", "21", "22", "23",
422      "24", "25", "26", "27", "28", "29", "30", "31",
423       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
424       "8",  "9", "10", "11", "12", "13", "14", "15",
425      "16", "17", "18", "19", "20", "21", "22", "23",
426      "24", "25", "26", "27", "28", "29", "30", "31",
427      "mq", "lr", "ctr","ap",
428       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
429       "xer",
430       /* AltiVec registers.  */
431       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
432       "8",  "9",  "10", "11", "12", "13", "14", "15",
433       "16", "17", "18", "19", "20", "21", "22", "23",
434       "24", "25", "26", "27", "28", "29", "30", "31",
435       "vrsave", "vscr",
436       /* SPE registers.  */
437       "spe_acc", "spefscr"
438 };
439
440 #ifdef TARGET_REGNAMES
441 static const char alt_reg_names[][8] =
442 {
443    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
444    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
445   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
446   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
447    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
448    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
449   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
450   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
451     "mq",    "lr",  "ctr",   "ap",
452   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
453    "xer",
454   /* AltiVec registers.  */
455    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
456    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
457   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
458   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
459   "vrsave", "vscr",
460   /* SPE registers.  */
461   "spe_acc", "spefscr"
462 };
463 #endif
464 \f
465 #ifndef MASK_STRICT_ALIGN
466 #define MASK_STRICT_ALIGN 0
467 #endif
468 #ifndef TARGET_PROFILE_KERNEL
469 #define TARGET_PROFILE_KERNEL 0
470 #endif
471
472 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
473 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
474
475 /* Return 1 for a symbol ref for a thread-local storage symbol.  */
476 #define RS6000_SYMBOL_REF_TLS_P(RTX) \
477   (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
478 \f
479 /* Initialize the GCC target structure.  */
480 #undef TARGET_ATTRIBUTE_TABLE
481 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
482 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
483 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
484
485 #undef TARGET_ASM_ALIGNED_DI_OP
486 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
487
488 /* Default unaligned ops are only provided for ELF.  Find the ops needed
489    for non-ELF systems.  */
490 #ifndef OBJECT_FORMAT_ELF
491 #if TARGET_XCOFF
492 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
493    64-bit targets.  */
494 #undef TARGET_ASM_UNALIGNED_HI_OP
495 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
496 #undef TARGET_ASM_UNALIGNED_SI_OP
497 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
498 #undef TARGET_ASM_UNALIGNED_DI_OP
499 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
500 #else
501 /* For Darwin.  */
502 #undef TARGET_ASM_UNALIGNED_HI_OP
503 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
504 #undef TARGET_ASM_UNALIGNED_SI_OP
505 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
506 #endif
507 #endif
508
509 /* This hook deals with fixups for relocatable code and DI-mode objects
510    in 64-bit code.  */
511 #undef TARGET_ASM_INTEGER
512 #define TARGET_ASM_INTEGER rs6000_assemble_integer
513
514 #ifdef HAVE_GAS_HIDDEN
515 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
516 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
517 #endif
518
519 #undef TARGET_HAVE_TLS
520 #define TARGET_HAVE_TLS HAVE_AS_TLS
521
522 #undef TARGET_CANNOT_FORCE_CONST_MEM
523 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_tls_referenced_p
524
525 #undef TARGET_ASM_FUNCTION_PROLOGUE
526 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
527 #undef TARGET_ASM_FUNCTION_EPILOGUE
528 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
529
530 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
531 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE rs6000_use_dfa_pipeline_interface
532 #undef  TARGET_SCHED_VARIABLE_ISSUE
533 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
534
535 #undef TARGET_SCHED_ISSUE_RATE
536 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
537 #undef TARGET_SCHED_ADJUST_COST
538 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
539 #undef TARGET_SCHED_ADJUST_PRIORITY
540 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
541 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE      
542 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
543 #undef TARGET_SCHED_FINISH
544 #define TARGET_SCHED_FINISH rs6000_sched_finish
545
546 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
547 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
548
549 #undef TARGET_INIT_BUILTINS
550 #define TARGET_INIT_BUILTINS rs6000_init_builtins
551
552 #undef TARGET_EXPAND_BUILTIN
553 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
554
555 #undef TARGET_INIT_LIBFUNCS
556 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
557
558 #if TARGET_MACHO
559 #undef TARGET_BINDS_LOCAL_P
560 #define TARGET_BINDS_LOCAL_P rs6000_binds_local_p
561 #endif
562
563 #undef TARGET_ASM_OUTPUT_MI_THUNK
564 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
565
566 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
567 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
568
569 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
570 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
571
572 #undef TARGET_RTX_COSTS
573 #define TARGET_RTX_COSTS rs6000_rtx_costs
574 #undef TARGET_ADDRESS_COST
575 #define TARGET_ADDRESS_COST hook_int_rtx_0
576
577 #undef TARGET_VECTOR_OPAQUE_P
578 #define TARGET_VECTOR_OPAQUE_P is_ev64_opaque_type
579
580 #undef TARGET_DWARF_REGISTER_SPAN
581 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
582
583 /* On rs6000, function arguments are promoted, as are function return
584    values.  */
585 #undef TARGET_PROMOTE_FUNCTION_ARGS
586 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
587 #undef TARGET_PROMOTE_FUNCTION_RETURN
588 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
589
590 /* Structure return values are passed as an extra parameter.  */
591 #undef TARGET_STRUCT_VALUE_RTX
592 #define TARGET_STRUCT_VALUE_RTX hook_rtx_tree_int_null
593
594 #undef TARGET_RETURN_IN_MEMORY
595 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
596
597 #undef TARGET_SETUP_INCOMING_VARARGS
598 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
599
600 /* Always strict argument naming on rs6000.  */
601 #undef TARGET_STRICT_ARGUMENT_NAMING
602 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
603 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
604 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
605
606 #undef TARGET_BUILD_BUILTIN_VA_LIST
607 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
608
609 struct gcc_target targetm = TARGET_INITIALIZER;
610 \f
611 /* Override command line options.  Mostly we process the processor
612    type and sometimes adjust other TARGET_ options.  */
613
614 void
615 rs6000_override_options (const char *default_cpu)
616 {
617   size_t i, j;
618   struct rs6000_cpu_select *ptr;
619
620   /* Simplify the entries below by making a mask for any POWER
621      variant and any PowerPC variant.  */
622
623 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
624 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
625                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
626 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
627
628   static struct ptt
629     {
630       const char *const name;           /* Canonical processor name.  */
631       const enum processor_type processor; /* Processor type enum value.  */
632       const int target_enable;  /* Target flags to enable.  */
633       const int target_disable; /* Target flags to disable.  */
634     } const processor_target_table[]
635       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
636             POWER_MASKS | POWERPC_MASKS},
637          {"power", PROCESSOR_POWER,
638             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
639             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
640          {"power2", PROCESSOR_POWER,
641             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
642             POWERPC_MASKS | MASK_NEW_MNEMONICS},
643          {"power3", PROCESSOR_PPC630,
644             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
645             POWER_MASKS},
646          {"power4", PROCESSOR_POWER4,
647             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
648             POWER_MASKS},
649          {"powerpc", PROCESSOR_POWERPC,
650             MASK_POWERPC | MASK_NEW_MNEMONICS,
651             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
652          {"powerpc64", PROCESSOR_POWERPC64,
653             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
654             POWER_MASKS | POWERPC_OPT_MASKS},
655          {"rios", PROCESSOR_RIOS1,
656             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
657             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
658          {"rios1", PROCESSOR_RIOS1,
659             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
660             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
661          {"rsc", PROCESSOR_PPC601,
662             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
663             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
664          {"rsc1", PROCESSOR_PPC601,
665             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
666             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
667          {"rios2", PROCESSOR_RIOS2,
668             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
669             POWERPC_MASKS | MASK_NEW_MNEMONICS},
670          {"rs64a", PROCESSOR_RS64A,
671             MASK_POWERPC | MASK_NEW_MNEMONICS,
672             POWER_MASKS | POWERPC_OPT_MASKS},
673          {"401", PROCESSOR_PPC403,
674             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
675             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
676          {"403", PROCESSOR_PPC403,
677             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
678             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
679          {"405", PROCESSOR_PPC405,
680             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
681             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
682          {"405fp", PROCESSOR_PPC405,
683             MASK_POWERPC | MASK_NEW_MNEMONICS,
684             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
685          {"440", PROCESSOR_PPC440,
686             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
687             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
688          {"440fp", PROCESSOR_PPC440,
689             MASK_POWERPC | MASK_NEW_MNEMONICS,
690             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
691          {"505", PROCESSOR_MPCCORE,
692             MASK_POWERPC | MASK_NEW_MNEMONICS,
693             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
694          {"601", PROCESSOR_PPC601,
695             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
696             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
697          {"602", PROCESSOR_PPC603,
698             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
699             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
700          {"603", PROCESSOR_PPC603,
701             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
702             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
703          {"603e", PROCESSOR_PPC603,
704             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
705             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
706          {"ec603e", PROCESSOR_PPC603,
707             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
708             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
709          {"604", PROCESSOR_PPC604,
710             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
711             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
712          {"604e", PROCESSOR_PPC604e,
713             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
714             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
715          {"620", PROCESSOR_PPC620,
716             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
717             POWER_MASKS},
718          {"630", PROCESSOR_PPC630,
719             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
720             POWER_MASKS},
721          {"740", PROCESSOR_PPC750,
722             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
723             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
724          {"750", PROCESSOR_PPC750,
725             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
726             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
727          {"G3", PROCESSOR_PPC750,
728             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
729             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
730          {"7400", PROCESSOR_PPC7400,
731             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
732             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
733          {"7450", PROCESSOR_PPC7450,
734             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
735             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
736          {"G4", PROCESSOR_PPC7450,
737             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
738             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
739          {"8540", PROCESSOR_PPC8540,
740             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
741             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
742          {"801", PROCESSOR_MPCCORE,
743             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
744             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
745          {"821", PROCESSOR_MPCCORE,
746             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
747             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
748          {"823", PROCESSOR_MPCCORE,
749             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
750             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
751          {"860", PROCESSOR_MPCCORE,
752             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
753             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
754          {"970", PROCESSOR_POWER4,
755             MASK_POWERPC | POWERPC_OPT_MASKS | MASK_NEW_MNEMONICS,
756             POWER_MASKS},
757          {"G5", PROCESSOR_POWER4,
758             MASK_POWERPC | POWERPC_OPT_MASKS | MASK_NEW_MNEMONICS,
759             POWER_MASKS}};
760
761   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
762
763   /* Save current -mmultiple/-mno-multiple status.  */
764   int multiple = TARGET_MULTIPLE;
765   /* Save current -mstring/-mno-string status.  */
766   int string = TARGET_STRING;
767
768   /* Identify the processor type.  */
769   rs6000_select[0].string = default_cpu;
770   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
771
772   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
773     {
774       ptr = &rs6000_select[i];
775       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
776         {
777           for (j = 0; j < ptt_size; j++)
778             if (! strcmp (ptr->string, processor_target_table[j].name))
779               {
780                 if (ptr->set_tune_p)
781                   rs6000_cpu = processor_target_table[j].processor;
782
783                 if (ptr->set_arch_p)
784                   {
785                     target_flags |= processor_target_table[j].target_enable;
786                     target_flags &= ~processor_target_table[j].target_disable;
787                   }
788                 break;
789               }
790
791           if (j == ptt_size)
792             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
793         }
794     }
795
796   if (TARGET_E500)
797     rs6000_isel = 1;
798
799   /* If we are optimizing big endian systems for space, use the load/store
800      multiple and string instructions.  */
801   if (BYTES_BIG_ENDIAN && optimize_size)
802     target_flags |= MASK_MULTIPLE | MASK_STRING;
803
804   /* If -mmultiple or -mno-multiple was explicitly used, don't
805      override with the processor default */
806   if ((target_flags_explicit & MASK_MULTIPLE) != 0)
807     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
808
809   /* If -mstring or -mno-string was explicitly used, don't override
810      with the processor default.  */
811   if ((target_flags_explicit & MASK_STRING) != 0)
812     target_flags = (target_flags & ~MASK_STRING) | string;
813
814   /* Don't allow -mmultiple or -mstring on little endian systems
815      unless the cpu is a 750, because the hardware doesn't support the
816      instructions used in little endian mode, and causes an alignment
817      trap.  The 750 does not cause an alignment trap (except when the
818      target is unaligned).  */
819
820   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
821     {
822       if (TARGET_MULTIPLE)
823         {
824           target_flags &= ~MASK_MULTIPLE;
825           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
826             warning ("-mmultiple is not supported on little endian systems");
827         }
828
829       if (TARGET_STRING)
830         {
831           target_flags &= ~MASK_STRING;
832           if ((target_flags_explicit & MASK_STRING) != 0)
833             warning ("-mstring is not supported on little endian systems");
834         }
835     }
836
837   /* Set debug flags */
838   if (rs6000_debug_name)
839     {
840       if (! strcmp (rs6000_debug_name, "all"))
841         rs6000_debug_stack = rs6000_debug_arg = 1;
842       else if (! strcmp (rs6000_debug_name, "stack"))
843         rs6000_debug_stack = 1;
844       else if (! strcmp (rs6000_debug_name, "arg"))
845         rs6000_debug_arg = 1;
846       else
847         error ("unknown -mdebug-%s switch", rs6000_debug_name);
848     }
849
850   if (rs6000_traceback_name)
851     {
852       if (! strncmp (rs6000_traceback_name, "full", 4))
853         rs6000_traceback = traceback_full;
854       else if (! strncmp (rs6000_traceback_name, "part", 4))
855         rs6000_traceback = traceback_part;
856       else if (! strncmp (rs6000_traceback_name, "no", 2))
857         rs6000_traceback = traceback_none;
858       else
859         error ("unknown -mtraceback arg `%s'; expecting `full', `partial' or `none'",
860                rs6000_traceback_name);
861     }
862
863   /* Set size of long double */
864   rs6000_long_double_type_size = 64;
865   if (rs6000_long_double_size_string)
866     {
867       char *tail;
868       int size = strtol (rs6000_long_double_size_string, &tail, 10);
869       if (*tail != '\0' || (size != 64 && size != 128))
870         error ("Unknown switch -mlong-double-%s",
871                rs6000_long_double_size_string);
872       else
873         rs6000_long_double_type_size = size;
874     }
875
876   /* Handle -mabi= options.  */
877   rs6000_parse_abi_options ();
878
879   /* Handle -malign-XXXXX option.  */
880   rs6000_parse_alignment_option ();
881
882   /* Handle generic -mFOO=YES/NO options.  */
883   rs6000_parse_yes_no_option ("vrsave", rs6000_altivec_vrsave_string,
884                               &rs6000_altivec_vrsave);
885   rs6000_parse_yes_no_option ("isel", rs6000_isel_string,
886                               &rs6000_isel);
887   rs6000_parse_yes_no_option ("spe", rs6000_spe_string, &rs6000_spe);
888   rs6000_parse_yes_no_option ("float-gprs", rs6000_float_gprs_string,
889                               &rs6000_float_gprs);
890
891   /* Handle -mtls-size option.  */
892   rs6000_parse_tls_size_option ();
893
894 #ifdef SUBTARGET_OVERRIDE_OPTIONS
895   SUBTARGET_OVERRIDE_OPTIONS;
896 #endif
897 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
898   SUBSUBTARGET_OVERRIDE_OPTIONS;
899 #endif
900
901   if (TARGET_E500)
902     {
903       /* The e500 does not have string instructions, and we set
904          MASK_STRING above when optimizing for size.  */
905       if ((target_flags & MASK_STRING) != 0)
906         target_flags = target_flags & ~MASK_STRING;
907
908       /* No SPE means 64-bit long doubles, even if an E500.  */
909       if (rs6000_spe_string != 0
910           && !strcmp (rs6000_spe_string, "no"))
911         rs6000_long_double_type_size = 64;
912     }
913   else if (rs6000_select[1].string != NULL)
914     {
915       /* For the powerpc-eabispe configuration, we set all these by
916          default, so let's unset them if we manually set another
917          CPU that is not the E500.  */
918       if (rs6000_abi_string == 0)
919         rs6000_spe_abi = 0;
920       if (rs6000_spe_string == 0)
921         rs6000_spe = 0;
922       if (rs6000_float_gprs_string == 0)
923         rs6000_float_gprs = 0;
924       if (rs6000_isel_string == 0)
925         rs6000_isel = 0;
926       if (rs6000_long_double_size_string == 0)
927         rs6000_long_double_type_size = 64;
928     }
929
930   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
931      using TARGET_OPTIONS to handle a toggle switch, but we're out of
932      bits in target_flags so TARGET_SWITCHES cannot be used.
933      Assumption here is that rs6000_longcall_switch points into the
934      text of the complete option, rather than being a copy, so we can
935      scan back for the presence or absence of the no- modifier.  */
936   if (rs6000_longcall_switch)
937     {
938       const char *base = rs6000_longcall_switch;
939       while (base[-1] != 'm') base--;
940
941       if (*rs6000_longcall_switch != '\0')
942         error ("invalid option `%s'", base);
943       rs6000_default_long_calls = (base[0] != 'n');
944     }
945
946   /* Handle -mprioritize-restricted-insns option.  */
947   rs6000_sched_restricted_insns_priority = DEFAULT_RESTRICTED_INSNS_PRIORITY;
948   if (rs6000_sched_restricted_insns_priority_str)
949     rs6000_sched_restricted_insns_priority =
950       atoi (rs6000_sched_restricted_insns_priority_str);
951
952   /* Handle -msched-costly-dep option.  */
953   rs6000_sched_costly_dep = DEFAULT_SCHED_COSTLY_DEP;
954   if (rs6000_sched_costly_dep_str)
955     {
956       if (! strcmp (rs6000_sched_costly_dep_str, "no"))  
957         rs6000_sched_costly_dep = no_dep_costly;
958       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
959         rs6000_sched_costly_dep = all_deps_costly;
960       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
961         rs6000_sched_costly_dep = true_store_to_load_dep_costly;
962       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
963         rs6000_sched_costly_dep = store_to_load_dep_costly;
964       else 
965         rs6000_sched_costly_dep = atoi (rs6000_sched_costly_dep_str);
966     }
967
968   /* Handle -minsert-sched-nops option.  */
969   rs6000_sched_insert_nops = DEFAULT_SCHED_FINISH_NOP_INSERTION_SCHEME;
970   if (rs6000_sched_insert_nops_str)
971     {
972       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
973         rs6000_sched_insert_nops = sched_finish_none;
974       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
975         rs6000_sched_insert_nops = sched_finish_pad_groups;
976       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
977         rs6000_sched_insert_nops = sched_finish_regroup_exact;
978       else
979         rs6000_sched_insert_nops = atoi (rs6000_sched_insert_nops_str);
980     }
981
982 #ifdef TARGET_REGNAMES
983   /* If the user desires alternate register names, copy in the
984      alternate names now.  */
985   if (TARGET_REGNAMES)
986     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
987 #endif
988
989   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
990      If -maix-struct-return or -msvr4-struct-return was explicitly
991      used, don't override with the ABI default.  */
992   if ((target_flags_explicit & MASK_AIX_STRUCT_RET) == 0)
993     {
994       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
995         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
996       else
997         target_flags |= MASK_AIX_STRUCT_RET;
998     }
999
1000   if (TARGET_LONG_DOUBLE_128
1001       && (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN))
1002     REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
1003
1004   /* Allocate an alias set for register saves & restores from stack.  */
1005   rs6000_sr_alias_set = new_alias_set ();
1006
1007   if (TARGET_TOC) 
1008     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
1009
1010   /* We can only guarantee the availability of DI pseudo-ops when
1011      assembling for 64-bit targets.  */
1012   if (!TARGET_64BIT)
1013     {
1014       targetm.asm_out.aligned_op.di = NULL;
1015       targetm.asm_out.unaligned_op.di = NULL;
1016     }
1017
1018   /* Set maximum branch target alignment at two instructions, eight bytes.  */
1019   align_jumps_max_skip = 8;
1020   align_loops_max_skip = 8;
1021
1022   /* Arrange to save and restore machine status around nested functions.  */
1023   init_machine_status = rs6000_init_machine_status;
1024 }
1025
1026 /* Handle generic options of the form -mfoo=yes/no.
1027    NAME is the option name.
1028    VALUE is the option value.
1029    FLAG is the pointer to the flag where to store a 1 or 0, depending on
1030    whether the option value is 'yes' or 'no' respectively.  */
1031 static void
1032 rs6000_parse_yes_no_option (const char *name, const char *value, int *flag)
1033 {
1034   if (value == 0)
1035     return;
1036   else if (!strcmp (value, "yes"))
1037     *flag = 1;
1038   else if (!strcmp (value, "no"))
1039     *flag = 0;
1040   else
1041     error ("unknown -m%s= option specified: '%s'", name, value);
1042 }
1043
1044 /* Handle -mabi= options.  */
1045 static void
1046 rs6000_parse_abi_options (void)
1047 {
1048   if (rs6000_abi_string == 0)
1049     return;
1050   else if (! strcmp (rs6000_abi_string, "altivec"))
1051     rs6000_altivec_abi = 1;
1052   else if (! strcmp (rs6000_abi_string, "no-altivec"))
1053     rs6000_altivec_abi = 0;
1054   else if (! strcmp (rs6000_abi_string, "spe"))
1055     {
1056       rs6000_spe_abi = 1;
1057       if (!TARGET_SPE_ABI)
1058         error ("not configured for ABI: '%s'", rs6000_abi_string);
1059     }
1060   
1061   else if (! strcmp (rs6000_abi_string, "no-spe"))
1062     rs6000_spe_abi = 0;
1063   else
1064     error ("unknown ABI specified: '%s'", rs6000_abi_string);
1065 }
1066
1067 /* Handle -malign-XXXXXX options.  */
1068 static void
1069 rs6000_parse_alignment_option (void)
1070 {
1071   if (rs6000_alignment_string == 0)
1072     return;
1073   else if (! strcmp (rs6000_alignment_string, "power"))
1074     rs6000_alignment_flags = MASK_ALIGN_POWER;
1075   else if (! strcmp (rs6000_alignment_string, "natural"))
1076     rs6000_alignment_flags = MASK_ALIGN_NATURAL;
1077   else
1078     error ("unknown -malign-XXXXX option specified: '%s'",
1079            rs6000_alignment_string);
1080 }
1081
1082 /* Validate and record the size specified with the -mtls-size option.  */
1083
1084 static void
1085 rs6000_parse_tls_size_option (void)
1086 {
1087   if (rs6000_tls_size_string == 0)
1088     return;
1089   else if (strcmp (rs6000_tls_size_string, "16") == 0)
1090     rs6000_tls_size = 16;
1091   else if (strcmp (rs6000_tls_size_string, "32") == 0)
1092     rs6000_tls_size = 32;
1093   else if (strcmp (rs6000_tls_size_string, "64") == 0)
1094     rs6000_tls_size = 64;
1095   else
1096     error ("bad value `%s' for -mtls-size switch", rs6000_tls_size_string);
1097 }
1098
1099 void
1100 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1101 {
1102 }
1103 \f
1104 /* Do anything needed at the start of the asm file.  */
1105
1106 static void
1107 rs6000_file_start (void)
1108 {
1109   size_t i;
1110   char buffer[80];
1111   const char *start = buffer;
1112   struct rs6000_cpu_select *ptr;
1113   const char *default_cpu = TARGET_CPU_DEFAULT;
1114   FILE *file = asm_out_file;
1115
1116   default_file_start ();
1117
1118 #ifdef TARGET_BI_ARCH
1119   if ((TARGET_DEFAULT ^ target_flags) & MASK_64BIT)
1120     default_cpu = 0;
1121 #endif
1122
1123   if (flag_verbose_asm)
1124     {
1125       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
1126       rs6000_select[0].string = default_cpu;
1127
1128       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
1129         {
1130           ptr = &rs6000_select[i];
1131           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
1132             {
1133               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
1134               start = "";
1135             }
1136         }
1137
1138 #ifdef USING_ELFOS_H
1139       switch (rs6000_sdata)
1140         {
1141         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
1142         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
1143         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
1144         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
1145         }
1146
1147       if (rs6000_sdata && g_switch_value)
1148         {
1149           fprintf (file, "%s -G " HOST_WIDE_INT_PRINT_UNSIGNED, start,
1150                    g_switch_value);
1151           start = "";
1152         }
1153 #endif
1154
1155       if (*start == '\0')
1156         putc ('\n', file);
1157     }
1158 }
1159 \f
1160 /* Return nonzero if this function is known to have a null epilogue.  */
1161
1162 int
1163 direct_return (void)
1164 {
1165   if (reload_completed)
1166     {
1167       rs6000_stack_t *info = rs6000_stack_info ();
1168
1169       if (info->first_gp_reg_save == 32
1170           && info->first_fp_reg_save == 64
1171           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
1172           && ! info->lr_save_p
1173           && ! info->cr_save_p
1174           && info->vrsave_mask == 0
1175           && ! info->push_p)
1176         return 1;
1177     }
1178
1179   return 0;
1180 }
1181
1182 /* Returns 1 always.  */
1183
1184 int
1185 any_operand (rtx op ATTRIBUTE_UNUSED, 
1186              enum machine_mode mode ATTRIBUTE_UNUSED)
1187 {
1188   return 1;
1189 }
1190
1191 /* Returns 1 if op is the count register.  */
1192 int
1193 count_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1194 {
1195   if (GET_CODE (op) != REG)
1196     return 0;
1197
1198   if (REGNO (op) == COUNT_REGISTER_REGNUM)
1199     return 1;
1200
1201   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
1202     return 1;
1203
1204   return 0;
1205 }
1206
1207 /* Returns 1 if op is an altivec register.  */
1208 int
1209 altivec_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1210 {
1211   
1212   return (register_operand (op, mode)
1213           && (GET_CODE (op) != REG
1214               || REGNO (op) > FIRST_PSEUDO_REGISTER
1215               || ALTIVEC_REGNO_P (REGNO (op))));
1216 }
1217
1218 int
1219 xer_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1220 {
1221   if (GET_CODE (op) != REG)
1222     return 0;
1223
1224   if (XER_REGNO_P (REGNO (op)))
1225     return 1;
1226
1227   return 0;
1228 }
1229
1230 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
1231    by such constants completes more quickly.  */
1232
1233 int
1234 s8bit_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1235 {
1236   return ( GET_CODE (op) == CONST_INT
1237           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
1238 }
1239
1240 /* Return 1 if OP is a constant that can fit in a D field.  */
1241
1242 int
1243 short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1244 {
1245   return (GET_CODE (op) == CONST_INT
1246           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
1247 }
1248
1249 /* Similar for an unsigned D field.  */
1250
1251 int
1252 u_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1253 {
1254   return (GET_CODE (op) == CONST_INT
1255           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
1256 }
1257
1258 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
1259
1260 int
1261 non_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1262 {
1263   return (GET_CODE (op) == CONST_INT
1264           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
1265 }
1266
1267 /* Returns 1 if OP is a CONST_INT that is a positive value
1268    and an exact power of 2.  */
1269
1270 int
1271 exact_log2_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1272 {
1273   return (GET_CODE (op) == CONST_INT
1274           && INTVAL (op) > 0
1275           && exact_log2 (INTVAL (op)) >= 0);
1276 }
1277
1278 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
1279    ctr, or lr).  */
1280
1281 int
1282 gpc_reg_operand (rtx op, enum machine_mode mode)
1283 {
1284   return (register_operand (op, mode)
1285           && (GET_CODE (op) != REG
1286               || (REGNO (op) >= ARG_POINTER_REGNUM 
1287                   && !XER_REGNO_P (REGNO (op)))
1288               || REGNO (op) < MQ_REGNO));
1289 }
1290
1291 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1292    CR field.  */
1293
1294 int
1295 cc_reg_operand (rtx op, enum machine_mode mode)
1296 {
1297   return (register_operand (op, mode)
1298           && (GET_CODE (op) != REG
1299               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1300               || CR_REGNO_P (REGNO (op))));
1301 }
1302
1303 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1304    CR field that isn't CR0.  */
1305
1306 int
1307 cc_reg_not_cr0_operand (rtx op, enum machine_mode mode)
1308 {
1309   return (register_operand (op, mode)
1310           && (GET_CODE (op) != REG
1311               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1312               || CR_REGNO_NOT_CR0_P (REGNO (op))));
1313 }
1314
1315 /* Returns 1 if OP is either a constant integer valid for a D-field or
1316    a non-special register.  If a register, it must be in the proper
1317    mode unless MODE is VOIDmode.  */
1318
1319 int
1320 reg_or_short_operand (rtx op, enum machine_mode mode)
1321 {
1322   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1323 }
1324
1325 /* Similar, except check if the negation of the constant would be
1326    valid for a D-field.  */
1327
1328 int
1329 reg_or_neg_short_operand (rtx op, enum machine_mode mode)
1330 {
1331   if (GET_CODE (op) == CONST_INT)
1332     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
1333
1334   return gpc_reg_operand (op, mode);
1335 }
1336
1337 /* Returns 1 if OP is either a constant integer valid for a DS-field or
1338    a non-special register.  If a register, it must be in the proper
1339    mode unless MODE is VOIDmode.  */
1340
1341 int
1342 reg_or_aligned_short_operand (rtx op, enum machine_mode mode)
1343 {
1344   if (gpc_reg_operand (op, mode))
1345     return 1;
1346   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1347     return 1;
1348
1349   return 0;
1350 }
1351
1352
1353 /* Return 1 if the operand is either a register or an integer whose
1354    high-order 16 bits are zero.  */
1355
1356 int
1357 reg_or_u_short_operand (rtx op, enum machine_mode mode)
1358 {
1359   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1360 }
1361
1362 /* Return 1 is the operand is either a non-special register or ANY
1363    constant integer.  */
1364
1365 int
1366 reg_or_cint_operand (rtx op, enum machine_mode mode)
1367 {
1368   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
1369 }
1370
1371 /* Return 1 is the operand is either a non-special register or ANY
1372    32-bit signed constant integer.  */
1373
1374 int
1375 reg_or_arith_cint_operand (rtx op, enum machine_mode mode)
1376 {
1377   return (gpc_reg_operand (op, mode)
1378           || (GET_CODE (op) == CONST_INT
1379 #if HOST_BITS_PER_WIDE_INT != 32
1380               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1381                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
1382 #endif
1383               ));
1384 }
1385
1386 /* Return 1 is the operand is either a non-special register or a 32-bit
1387    signed constant integer valid for 64-bit addition.  */
1388
1389 int
1390 reg_or_add_cint64_operand (rtx op, enum machine_mode mode)
1391 {
1392   return (gpc_reg_operand (op, mode)
1393           || (GET_CODE (op) == CONST_INT
1394 #if HOST_BITS_PER_WIDE_INT == 32
1395               && INTVAL (op) < 0x7fff8000
1396 #else
1397               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1398                   < 0x100000000ll)
1399 #endif
1400               ));
1401 }
1402
1403 /* Return 1 is the operand is either a non-special register or a 32-bit
1404    signed constant integer valid for 64-bit subtraction.  */
1405
1406 int
1407 reg_or_sub_cint64_operand (rtx op, enum machine_mode mode)
1408 {
1409   return (gpc_reg_operand (op, mode)
1410           || (GET_CODE (op) == CONST_INT
1411 #if HOST_BITS_PER_WIDE_INT == 32
1412               && (- INTVAL (op)) < 0x7fff8000
1413 #else
1414               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1415                   < 0x100000000ll)
1416 #endif
1417               ));
1418 }
1419
1420 /* Return 1 is the operand is either a non-special register or ANY
1421    32-bit unsigned constant integer.  */
1422
1423 int
1424 reg_or_logical_cint_operand (rtx op, enum machine_mode mode)
1425 {
1426   if (GET_CODE (op) == CONST_INT)
1427     {
1428       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1429         {
1430           if (GET_MODE_BITSIZE (mode) <= 32)
1431             abort ();
1432
1433           if (INTVAL (op) < 0)
1434             return 0;
1435         }
1436
1437       return ((INTVAL (op) & GET_MODE_MASK (mode)
1438                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1439     }
1440   else if (GET_CODE (op) == CONST_DOUBLE)
1441     {
1442       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1443           || mode != DImode)
1444         abort ();
1445
1446       return CONST_DOUBLE_HIGH (op) == 0;
1447     }
1448   else 
1449     return gpc_reg_operand (op, mode);
1450 }
1451
1452 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1453
1454 int
1455 got_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1456 {
1457   return (GET_CODE (op) == SYMBOL_REF
1458           || GET_CODE (op) == CONST
1459           || GET_CODE (op) == LABEL_REF);
1460 }
1461
1462 /* Return 1 if the operand is a simple references that can be loaded via
1463    the GOT (labels involving addition aren't allowed).  */
1464
1465 int
1466 got_no_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1467 {
1468   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1469 }
1470
1471 /* Return the number of instructions it takes to form a constant in an
1472    integer register.  */
1473
1474 static int
1475 num_insns_constant_wide (HOST_WIDE_INT value)
1476 {
1477   /* signed constant loadable with {cal|addi} */
1478   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1479     return 1;
1480
1481   /* constant loadable with {cau|addis} */
1482   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1483     return 1;
1484
1485 #if HOST_BITS_PER_WIDE_INT == 64
1486   else if (TARGET_POWERPC64)
1487     {
1488       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1489       HOST_WIDE_INT high = value >> 31;
1490
1491       if (high == 0 || high == -1)
1492         return 2;
1493
1494       high >>= 1;
1495
1496       if (low == 0)
1497         return num_insns_constant_wide (high) + 1;
1498       else
1499         return (num_insns_constant_wide (high)
1500                 + num_insns_constant_wide (low) + 1);
1501     }
1502 #endif
1503
1504   else
1505     return 2;
1506 }
1507
1508 int
1509 num_insns_constant (rtx op, enum machine_mode mode)
1510 {
1511   if (GET_CODE (op) == CONST_INT)
1512     {
1513 #if HOST_BITS_PER_WIDE_INT == 64
1514       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1515           && mask64_operand (op, mode))
1516             return 2;
1517       else
1518 #endif
1519         return num_insns_constant_wide (INTVAL (op));
1520     }
1521
1522   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1523     {
1524       long l;
1525       REAL_VALUE_TYPE rv;
1526
1527       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1528       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1529       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1530     }
1531
1532   else if (GET_CODE (op) == CONST_DOUBLE)
1533     {
1534       HOST_WIDE_INT low;
1535       HOST_WIDE_INT high;
1536       long l[2];
1537       REAL_VALUE_TYPE rv;
1538       int endian = (WORDS_BIG_ENDIAN == 0);
1539
1540       if (mode == VOIDmode || mode == DImode)
1541         {
1542           high = CONST_DOUBLE_HIGH (op);
1543           low  = CONST_DOUBLE_LOW (op);
1544         }
1545       else
1546         {
1547           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1548           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1549           high = l[endian];
1550           low  = l[1 - endian];
1551         }
1552
1553       if (TARGET_32BIT)
1554         return (num_insns_constant_wide (low)
1555                 + num_insns_constant_wide (high));
1556
1557       else
1558         {
1559           if (high == 0 && low >= 0)
1560             return num_insns_constant_wide (low);
1561
1562           else if (high == -1 && low < 0)
1563             return num_insns_constant_wide (low);
1564
1565           else if (mask64_operand (op, mode))
1566             return 2;
1567
1568           else if (low == 0)
1569             return num_insns_constant_wide (high) + 1;
1570
1571           else
1572             return (num_insns_constant_wide (high)
1573                     + num_insns_constant_wide (low) + 1);
1574         }
1575     }
1576
1577   else
1578     abort ();
1579 }
1580
1581 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1582    register with one instruction per word.  We only do this if we can
1583    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1584
1585 int
1586 easy_fp_constant (rtx op, enum machine_mode mode)
1587 {
1588   if (GET_CODE (op) != CONST_DOUBLE
1589       || GET_MODE (op) != mode
1590       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1591     return 0;
1592
1593   /* Consider all constants with -msoft-float to be easy.  */
1594   if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1595       && mode != DImode)
1596     return 1;
1597
1598   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1599   if (flag_pic && DEFAULT_ABI == ABI_V4)
1600     return 0;
1601
1602 #ifdef TARGET_RELOCATABLE
1603   /* Similarly if we are using -mrelocatable, consider all constants
1604      to be hard.  */
1605   if (TARGET_RELOCATABLE)
1606     return 0;
1607 #endif
1608
1609   if (mode == TFmode)
1610     {
1611       long k[4];
1612       REAL_VALUE_TYPE rv;
1613
1614       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1615       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1616
1617       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1618               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1
1619               && num_insns_constant_wide ((HOST_WIDE_INT) k[2]) == 1
1620               && num_insns_constant_wide ((HOST_WIDE_INT) k[3]) == 1);
1621     }
1622
1623   else if (mode == DFmode)
1624     {
1625       long k[2];
1626       REAL_VALUE_TYPE rv;
1627
1628       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1629       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1630
1631       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1632               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1633     }
1634
1635   else if (mode == SFmode)
1636     {
1637       long l;
1638       REAL_VALUE_TYPE rv;
1639
1640       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1641       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1642
1643       return num_insns_constant_wide (l) == 1;
1644     }
1645
1646   else if (mode == DImode)
1647     return ((TARGET_POWERPC64
1648              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1649             || (num_insns_constant (op, DImode) <= 2));
1650
1651   else if (mode == SImode)
1652     return 1;
1653   else
1654     abort ();
1655 }
1656
1657 /* Return nonzero if all elements of a vector have the same value.  */
1658
1659 static int
1660 easy_vector_same (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1661 {
1662   int units, i, cst;
1663
1664   units = CONST_VECTOR_NUNITS (op);
1665
1666   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1667   for (i = 1; i < units; ++i)
1668     if (INTVAL (CONST_VECTOR_ELT (op, i)) != cst)
1669       break;
1670   if (i == units)
1671     return 1;
1672   return 0;
1673 }
1674
1675 /* Return 1 if the operand is a CONST_INT and can be put into a
1676    register without using memory.  */
1677
1678 int
1679 easy_vector_constant (rtx op, enum machine_mode mode)
1680 {
1681   int cst, cst2;
1682
1683   if (GET_CODE (op) != CONST_VECTOR
1684       || (!TARGET_ALTIVEC
1685           && !TARGET_SPE))
1686     return 0;
1687
1688   if (zero_constant (op, mode)
1689       && ((TARGET_ALTIVEC && ALTIVEC_VECTOR_MODE (mode))
1690           || (TARGET_SPE && SPE_VECTOR_MODE (mode))))
1691     return 1;
1692
1693   if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
1694     return 0;
1695
1696   if (TARGET_SPE && mode == V1DImode)
1697     return 0;
1698
1699   cst  = INTVAL (CONST_VECTOR_ELT (op, 0));
1700   cst2 = INTVAL (CONST_VECTOR_ELT (op, 1));
1701
1702   /* Limit SPE vectors to 15 bits signed.  These we can generate with:
1703        li r0, CONSTANT1
1704        evmergelo r0, r0, r0
1705        li r0, CONSTANT2
1706
1707      I don't know how efficient it would be to allow bigger constants,
1708      considering we'll have an extra 'ori' for every 'li'.  I doubt 5
1709      instructions is better than a 64-bit memory load, but I don't
1710      have the e500 timing specs.  */
1711   if (TARGET_SPE && mode == V2SImode
1712       && cst  >= -0x7fff && cst <= 0x7fff
1713       && cst2 >= -0x7fff && cst2 <= 0x7fff)
1714     return 1;
1715
1716   if (TARGET_ALTIVEC && EASY_VECTOR_15 (cst, op, mode))
1717     return 1;
1718
1719   if (TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode))
1720     return 1;
1721
1722   return 0;
1723 }
1724
1725 /* Same as easy_vector_constant but only for EASY_VECTOR_15_ADD_SELF.  */
1726
1727 int
1728 easy_vector_constant_add_self (rtx op, enum machine_mode mode)
1729 {
1730   int cst;
1731
1732   if (!easy_vector_constant (op, mode))
1733     return 0;
1734
1735   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1736
1737   return TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode);
1738 }
1739
1740 const char *
1741 output_vec_const_move (rtx *operands)
1742 {
1743   int cst, cst2;
1744   enum machine_mode mode;
1745   rtx dest, vec;
1746
1747   dest = operands[0];
1748   vec = operands[1];
1749
1750   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
1751   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
1752   mode = GET_MODE (dest);
1753
1754   if (TARGET_ALTIVEC)
1755     {
1756       if (zero_constant (vec, mode))
1757         return "vxor %0,%0,%0";
1758       else if (EASY_VECTOR_15 (cst, vec, mode))
1759         {
1760           operands[1] = GEN_INT (cst);
1761           switch (mode)
1762             {
1763             case V4SImode:
1764               return "vspltisw %0,%1";
1765             case V8HImode:
1766               return "vspltish %0,%1";
1767             case V16QImode:
1768               return "vspltisb %0,%1";
1769             default:
1770               abort ();
1771             }
1772         }
1773       else if (EASY_VECTOR_15_ADD_SELF (cst, vec, mode))
1774         return "#";
1775       else
1776         abort ();
1777     }
1778
1779   if (TARGET_SPE)
1780     {
1781       /* Vector constant 0 is handled as a splitter of V2SI, and in the
1782          pattern of V1DI, V4HI, and V2SF.
1783
1784          FIXME: We should probably return # and add post reload
1785          splitters for these, but this way is so easy ;-).
1786       */
1787       operands[1] = GEN_INT (cst);
1788       operands[2] = GEN_INT (cst2);
1789       if (cst == cst2)
1790         return "li %0,%1\n\tevmergelo %0,%0,%0";
1791       else
1792         return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
1793     }
1794
1795   abort ();
1796 }
1797
1798 /* Return 1 if the operand is the constant 0.  This works for scalars
1799    as well as vectors.  */
1800 int
1801 zero_constant (rtx op, enum machine_mode mode)
1802 {
1803   return op == CONST0_RTX (mode);
1804 }
1805
1806 /* Return 1 if the operand is 0.0.  */
1807 int
1808 zero_fp_constant (rtx op, enum machine_mode mode)
1809 {
1810   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1811 }
1812
1813 /* Return 1 if the operand is in volatile memory.  Note that during
1814    the RTL generation phase, memory_operand does not return TRUE for
1815    volatile memory references.  So this function allows us to
1816    recognize volatile references where its safe.  */
1817
1818 int
1819 volatile_mem_operand (rtx op, enum machine_mode mode)
1820 {
1821   if (GET_CODE (op) != MEM)
1822     return 0;
1823
1824   if (!MEM_VOLATILE_P (op))
1825     return 0;
1826
1827   if (mode != GET_MODE (op))
1828     return 0;
1829
1830   if (reload_completed)
1831     return memory_operand (op, mode);
1832
1833   if (reload_in_progress)
1834     return strict_memory_address_p (mode, XEXP (op, 0));
1835
1836   return memory_address_p (mode, XEXP (op, 0));
1837 }
1838
1839 /* Return 1 if the operand is an offsettable memory operand.  */
1840
1841 int
1842 offsettable_mem_operand (rtx op, enum machine_mode mode)
1843 {
1844   return ((GET_CODE (op) == MEM)
1845           && offsettable_address_p (reload_completed || reload_in_progress,
1846                                     mode, XEXP (op, 0)));
1847 }
1848
1849 /* Return 1 if the operand is either an easy FP constant (see above) or
1850    memory.  */
1851
1852 int
1853 mem_or_easy_const_operand (rtx op, enum machine_mode mode)
1854 {
1855   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1856 }
1857
1858 /* Return 1 if the operand is either a non-special register or an item
1859    that can be used as the operand of a `mode' add insn.  */
1860
1861 int
1862 add_operand (rtx op, enum machine_mode mode)
1863 {
1864   if (GET_CODE (op) == CONST_INT)
1865     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1866             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1867
1868   return gpc_reg_operand (op, mode);
1869 }
1870
1871 /* Return 1 if OP is a constant but not a valid add_operand.  */
1872
1873 int
1874 non_add_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1875 {
1876   return (GET_CODE (op) == CONST_INT
1877           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1878           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1879 }
1880
1881 /* Return 1 if the operand is a non-special register or a constant that
1882    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1883
1884 int
1885 logical_operand (rtx op, enum machine_mode mode)
1886 {
1887   HOST_WIDE_INT opl, oph;
1888
1889   if (gpc_reg_operand (op, mode))
1890     return 1;
1891
1892   if (GET_CODE (op) == CONST_INT)
1893     {
1894       opl = INTVAL (op) & GET_MODE_MASK (mode);
1895
1896 #if HOST_BITS_PER_WIDE_INT <= 32
1897       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1898         return 0;
1899 #endif
1900     }
1901   else if (GET_CODE (op) == CONST_DOUBLE)
1902     {
1903       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1904         abort ();
1905
1906       opl = CONST_DOUBLE_LOW (op);
1907       oph = CONST_DOUBLE_HIGH (op);
1908       if (oph != 0)
1909         return 0;
1910     }
1911   else
1912     return 0;
1913
1914   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1915           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1916 }
1917
1918 /* Return 1 if C is a constant that is not a logical operand (as
1919    above), but could be split into one.  */
1920
1921 int
1922 non_logical_cint_operand (rtx op, enum machine_mode mode)
1923 {
1924   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1925           && ! logical_operand (op, mode)
1926           && reg_or_logical_cint_operand (op, mode));
1927 }
1928
1929 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1930    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1931    Reject all ones and all zeros, since these should have been optimized
1932    away and confuse the making of MB and ME.  */
1933
1934 int
1935 mask_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1936 {
1937   HOST_WIDE_INT c, lsb;
1938
1939   if (GET_CODE (op) != CONST_INT)
1940     return 0;
1941
1942   c = INTVAL (op);
1943
1944   /* Fail in 64-bit mode if the mask wraps around because the upper
1945      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
1946   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
1947     return 0;
1948
1949   /* We don't change the number of transitions by inverting,
1950      so make sure we start with the LS bit zero.  */
1951   if (c & 1)
1952     c = ~c;
1953
1954   /* Reject all zeros or all ones.  */
1955   if (c == 0)
1956     return 0;
1957
1958   /* Find the first transition.  */
1959   lsb = c & -c;
1960
1961   /* Invert to look for a second transition.  */
1962   c = ~c;
1963
1964   /* Erase first transition.  */
1965   c &= -lsb;
1966
1967   /* Find the second transition (if any).  */
1968   lsb = c & -c;
1969
1970   /* Match if all the bits above are 1's (or c is zero).  */
1971   return c == -lsb;
1972 }
1973
1974 /* Return 1 for the PowerPC64 rlwinm corner case.  */
1975
1976 int
1977 mask_operand_wrap (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1978 {
1979   HOST_WIDE_INT c, lsb;
1980
1981   if (GET_CODE (op) != CONST_INT)
1982     return 0;
1983
1984   c = INTVAL (op);
1985
1986   if ((c & 0x80000001) != 0x80000001)
1987     return 0;
1988
1989   c = ~c;
1990   if (c == 0)
1991     return 0;
1992
1993   lsb = c & -c;
1994   c = ~c;
1995   c &= -lsb;
1996   lsb = c & -c;
1997   return c == -lsb;
1998 }
1999
2000 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
2001    It is if there are no more than one 1->0 or 0->1 transitions.
2002    Reject all zeros, since zero should have been optimized away and
2003    confuses the making of MB and ME.  */
2004
2005 int
2006 mask64_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2007 {
2008   if (GET_CODE (op) == CONST_INT)
2009     {
2010       HOST_WIDE_INT c, lsb;
2011
2012       c = INTVAL (op);
2013
2014       /* Reject all zeros.  */
2015       if (c == 0)
2016         return 0;
2017
2018       /* We don't change the number of transitions by inverting,
2019          so make sure we start with the LS bit zero.  */
2020       if (c & 1)
2021         c = ~c;
2022
2023       /* Find the transition, and check that all bits above are 1's.  */
2024       lsb = c & -c;
2025
2026       /* Match if all the bits above are 1's (or c is zero).  */
2027       return c == -lsb;
2028     }
2029   return 0;
2030 }
2031
2032 /* Like mask64_operand, but allow up to three transitions.  This
2033    predicate is used by insn patterns that generate two rldicl or
2034    rldicr machine insns.  */
2035
2036 int
2037 mask64_2_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2038 {
2039   if (GET_CODE (op) == CONST_INT)
2040     {
2041       HOST_WIDE_INT c, lsb;
2042
2043       c = INTVAL (op);
2044
2045       /* Disallow all zeros.  */
2046       if (c == 0)
2047         return 0;
2048
2049       /* We don't change the number of transitions by inverting,
2050          so make sure we start with the LS bit zero.  */
2051       if (c & 1)
2052         c = ~c;
2053
2054       /* Find the first transition.  */
2055       lsb = c & -c;
2056
2057       /* Invert to look for a second transition.  */
2058       c = ~c;
2059
2060       /* Erase first transition.  */
2061       c &= -lsb;
2062
2063       /* Find the second transition.  */
2064       lsb = c & -c;
2065
2066       /* Invert to look for a third transition.  */
2067       c = ~c;
2068
2069       /* Erase second transition.  */
2070       c &= -lsb;
2071
2072       /* Find the third transition (if any).  */
2073       lsb = c & -c;
2074
2075       /* Match if all the bits above are 1's (or c is zero).  */
2076       return c == -lsb;
2077     }
2078   return 0;
2079 }
2080
2081 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
2082    implement ANDing by the mask IN.  */
2083 void
2084 build_mask64_2_operands (rtx in, rtx *out)
2085 {
2086 #if HOST_BITS_PER_WIDE_INT >= 64
2087   unsigned HOST_WIDE_INT c, lsb, m1, m2;
2088   int shift;
2089
2090   if (GET_CODE (in) != CONST_INT)
2091     abort ();
2092
2093   c = INTVAL (in);
2094   if (c & 1)
2095     {
2096       /* Assume c initially something like 0x00fff000000fffff.  The idea
2097          is to rotate the word so that the middle ^^^^^^ group of zeros
2098          is at the MS end and can be cleared with an rldicl mask.  We then
2099          rotate back and clear off the MS    ^^ group of zeros with a
2100          second rldicl.  */
2101       c = ~c;                   /*   c == 0xff000ffffff00000 */
2102       lsb = c & -c;             /* lsb == 0x0000000000100000 */
2103       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
2104       c = ~c;                   /*   c == 0x00fff000000fffff */
2105       c &= -lsb;                /*   c == 0x00fff00000000000 */
2106       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2107       c = ~c;                   /*   c == 0xff000fffffffffff */
2108       c &= -lsb;                /*   c == 0xff00000000000000 */
2109       shift = 0;
2110       while ((lsb >>= 1) != 0)
2111         shift++;                /* shift == 44 on exit from loop */
2112       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
2113       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
2114       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
2115     }
2116   else
2117     {
2118       /* Assume c initially something like 0xff000f0000000000.  The idea
2119          is to rotate the word so that the     ^^^  middle group of zeros
2120          is at the LS end and can be cleared with an rldicr mask.  We then
2121          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
2122          a second rldicr.  */
2123       lsb = c & -c;             /* lsb == 0x0000010000000000 */
2124       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
2125       c = ~c;                   /*   c == 0x00fff0ffffffffff */
2126       c &= -lsb;                /*   c == 0x00fff00000000000 */
2127       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2128       c = ~c;                   /*   c == 0xff000fffffffffff */
2129       c &= -lsb;                /*   c == 0xff00000000000000 */
2130       shift = 0;
2131       while ((lsb >>= 1) != 0)
2132         shift++;                /* shift == 44 on exit from loop */
2133       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
2134       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
2135       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
2136     }
2137
2138   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
2139      masks will be all 1's.  We are guaranteed more than one transition.  */
2140   out[0] = GEN_INT (64 - shift);
2141   out[1] = GEN_INT (m1);
2142   out[2] = GEN_INT (shift);
2143   out[3] = GEN_INT (m2);
2144 #else
2145   (void)in;
2146   (void)out;
2147   abort ();
2148 #endif
2149 }
2150
2151 /* Return 1 if the operand is either a non-special register or a constant
2152    that can be used as the operand of a PowerPC64 logical AND insn.  */
2153
2154 int
2155 and64_operand (rtx op, enum machine_mode mode)
2156 {
2157   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2158     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
2159
2160   return (logical_operand (op, mode) || mask64_operand (op, mode));
2161 }
2162
2163 /* Like the above, but also match constants that can be implemented
2164    with two rldicl or rldicr insns.  */
2165
2166 int
2167 and64_2_operand (rtx op, enum machine_mode mode)
2168 {
2169   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis. */
2170     return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
2171
2172   return logical_operand (op, mode) || mask64_2_operand (op, mode);
2173 }
2174
2175 /* Return 1 if the operand is either a non-special register or a
2176    constant that can be used as the operand of an RS/6000 logical AND insn.  */
2177
2178 int
2179 and_operand (rtx op, enum machine_mode mode)
2180 {
2181   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2182     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
2183
2184   return (logical_operand (op, mode) || mask_operand (op, mode));
2185 }
2186
2187 /* Return 1 if the operand is a general register or memory operand.  */
2188
2189 int
2190 reg_or_mem_operand (rtx op, enum machine_mode mode)
2191 {
2192   return (gpc_reg_operand (op, mode)
2193           || memory_operand (op, mode)
2194           || macho_lo_sum_memory_operand (op, mode)
2195           || volatile_mem_operand (op, mode));
2196 }
2197
2198 /* Return 1 if the operand is a general register or memory operand without
2199    pre_inc or pre_dec which produces invalid form of PowerPC lwa
2200    instruction.  */
2201
2202 int
2203 lwa_operand (rtx op, enum machine_mode mode)
2204 {
2205   rtx inner = op;
2206
2207   if (reload_completed && GET_CODE (inner) == SUBREG)
2208     inner = SUBREG_REG (inner);
2209     
2210   return gpc_reg_operand (inner, mode)
2211     || (memory_operand (inner, mode)
2212         && GET_CODE (XEXP (inner, 0)) != PRE_INC
2213         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
2214         && (GET_CODE (XEXP (inner, 0)) != PLUS
2215             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
2216             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
2217 }
2218
2219 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
2220
2221 int
2222 symbol_ref_operand (rtx op, enum machine_mode mode)
2223 {
2224   if (mode != VOIDmode && GET_MODE (op) != mode)
2225     return 0;
2226
2227   return (GET_CODE (op) == SYMBOL_REF
2228           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op)));
2229 }
2230
2231 /* Return 1 if the operand, used inside a MEM, is a valid first argument
2232    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
2233
2234 int
2235 call_operand (rtx op, enum machine_mode mode)
2236 {
2237   if (mode != VOIDmode && GET_MODE (op) != mode)
2238     return 0;
2239
2240   return (GET_CODE (op) == SYMBOL_REF
2241           || (GET_CODE (op) == REG
2242               && (REGNO (op) == LINK_REGISTER_REGNUM
2243                   || REGNO (op) == COUNT_REGISTER_REGNUM
2244                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2245 }
2246
2247 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
2248    this file.  */
2249
2250 int
2251 current_file_function_operand (rtx op, 
2252                               enum machine_mode mode ATTRIBUTE_UNUSED)
2253 {
2254   return (GET_CODE (op) == SYMBOL_REF
2255           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op))
2256           && (SYMBOL_REF_LOCAL_P (op)
2257               || (op == XEXP (DECL_RTL (current_function_decl), 0))));
2258 }
2259
2260 /* Return 1 if this operand is a valid input for a move insn.  */
2261
2262 int
2263 input_operand (rtx op, enum machine_mode mode)
2264 {
2265   /* Memory is always valid.  */
2266   if (memory_operand (op, mode))
2267     return 1;
2268
2269   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
2270   if (GET_CODE (op) == CONSTANT_P_RTX)
2271     return 1;
2272
2273   /* For floating-point, easy constants are valid.  */
2274   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2275       && CONSTANT_P (op)
2276       && easy_fp_constant (op, mode))
2277     return 1;
2278
2279   /* Allow any integer constant.  */
2280   if (GET_MODE_CLASS (mode) == MODE_INT
2281       && (GET_CODE (op) == CONST_INT
2282           || GET_CODE (op) == CONST_DOUBLE))
2283     return 1;
2284
2285   /* Allow easy vector constants.  */
2286   if (GET_CODE (op) == CONST_VECTOR
2287       && easy_vector_constant (op, mode))
2288     return 1;
2289
2290   /* For floating-point or multi-word mode, the only remaining valid type
2291      is a register.  */
2292   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2293       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2294     return register_operand (op, mode);
2295
2296   /* The only cases left are integral modes one word or smaller (we
2297      do not get called for MODE_CC values).  These can be in any
2298      register.  */
2299   if (register_operand (op, mode))
2300     return 1;
2301
2302   /* A SYMBOL_REF referring to the TOC is valid.  */
2303   if (legitimate_constant_pool_address_p (op))
2304     return 1;
2305
2306   /* A constant pool expression (relative to the TOC) is valid */
2307   if (toc_relative_expr_p (op))
2308     return 1;
2309
2310   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
2311      to be valid.  */
2312   if (DEFAULT_ABI == ABI_V4
2313       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
2314       && small_data_operand (op, Pmode))
2315     return 1;
2316
2317   return 0;
2318 }
2319
2320 /* Return 1 for an operand in small memory on V.4/eabi.  */
2321
2322 int
2323 small_data_operand (rtx op ATTRIBUTE_UNUSED, 
2324                     enum machine_mode mode ATTRIBUTE_UNUSED)
2325 {
2326 #if TARGET_ELF
2327   rtx sym_ref;
2328
2329   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
2330     return 0;
2331
2332   if (DEFAULT_ABI != ABI_V4)
2333     return 0;
2334
2335   if (GET_CODE (op) == SYMBOL_REF)
2336     sym_ref = op;
2337
2338   else if (GET_CODE (op) != CONST
2339            || GET_CODE (XEXP (op, 0)) != PLUS
2340            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
2341            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
2342     return 0;
2343
2344   else
2345     {
2346       rtx sum = XEXP (op, 0);
2347       HOST_WIDE_INT summand;
2348
2349       /* We have to be careful here, because it is the referenced address
2350         that must be 32k from _SDA_BASE_, not just the symbol.  */
2351       summand = INTVAL (XEXP (sum, 1));
2352       if (summand < 0 || (unsigned HOST_WIDE_INT) summand > g_switch_value)
2353        return 0;
2354
2355       sym_ref = XEXP (sum, 0);
2356     }
2357
2358   return SYMBOL_REF_SMALL_P (sym_ref);
2359 #else
2360   return 0;
2361 #endif
2362 }
2363
2364 /* Return true if either operand is a general purpose register.  */
2365
2366 bool
2367 gpr_or_gpr_p (rtx op0, rtx op1)
2368 {
2369   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
2370           || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
2371 }
2372
2373 \f
2374 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address.  */
2375
2376 static int 
2377 constant_pool_expr_1 (rtx op, int *have_sym, int *have_toc) 
2378 {
2379   switch (GET_CODE(op)) 
2380     {
2381     case SYMBOL_REF:
2382       if (RS6000_SYMBOL_REF_TLS_P (op))
2383         return 0;
2384       else if (CONSTANT_POOL_ADDRESS_P (op))
2385         {
2386           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
2387             {
2388               *have_sym = 1;
2389               return 1;
2390             }
2391           else
2392             return 0;
2393         }
2394       else if (! strcmp (XSTR (op, 0), toc_label_name))
2395         {
2396           *have_toc = 1;
2397           return 1;
2398         }
2399       else
2400         return 0;
2401     case PLUS:
2402     case MINUS:
2403       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2404               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
2405     case CONST:
2406       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
2407     case CONST_INT:
2408       return 1;
2409     default:
2410       return 0;
2411     }
2412 }
2413
2414 static bool
2415 constant_pool_expr_p (rtx op)
2416 {
2417   int have_sym = 0;
2418   int have_toc = 0;
2419   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2420 }
2421
2422 static bool
2423 toc_relative_expr_p (rtx op)
2424 {
2425   int have_sym = 0;
2426   int have_toc = 0;
2427   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2428 }
2429
2430 /* SPE offset addressing is limited to 5-bits worth of double words.  */
2431 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
2432
2433 bool
2434 legitimate_constant_pool_address_p (rtx x)
2435 {
2436   return (TARGET_TOC
2437           && GET_CODE (x) == PLUS
2438           && GET_CODE (XEXP (x, 0)) == REG
2439           && (TARGET_MINIMAL_TOC || REGNO (XEXP (x, 0)) == TOC_REGISTER)
2440           && constant_pool_expr_p (XEXP (x, 1)));
2441 }
2442
2443 static bool
2444 legitimate_small_data_p (enum machine_mode mode, rtx x)
2445 {
2446   return (DEFAULT_ABI == ABI_V4
2447           && !flag_pic && !TARGET_TOC
2448           && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
2449           && small_data_operand (x, mode));
2450 }
2451
2452 static bool
2453 legitimate_offset_address_p (enum machine_mode mode, rtx x, int strict)
2454 {
2455   unsigned HOST_WIDE_INT offset, extra;
2456
2457   if (GET_CODE (x) != PLUS)
2458     return false;
2459   if (GET_CODE (XEXP (x, 0)) != REG)
2460     return false;
2461   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2462     return false;
2463   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2464     return false;
2465
2466   offset = INTVAL (XEXP (x, 1));
2467   extra = 0;
2468   switch (mode)
2469     {
2470     case V16QImode:
2471     case V8HImode:
2472     case V4SFmode:
2473     case V4SImode:
2474       /* AltiVec vector modes.  Only reg+reg addressing is valid here,
2475          which leaves the only valid constant offset of zero, which by
2476          canonicalization rules is also invalid.  */
2477       return false;
2478
2479     case V4HImode:
2480     case V2SImode:
2481     case V1DImode:
2482     case V2SFmode:
2483       /* SPE vector modes.  */
2484       return SPE_CONST_OFFSET_OK (offset);
2485
2486     case DFmode:
2487     case DImode:
2488       if (TARGET_32BIT)
2489         extra = 4;
2490       else if (offset & 3)
2491         return false;
2492       break;
2493
2494     case TFmode:
2495     case TImode:
2496       if (TARGET_32BIT)
2497         extra = 12;
2498       else if (offset & 3)
2499         return false;
2500       else
2501         extra = 8;
2502       break;
2503
2504     default:
2505       break;
2506     }
2507
2508   return (offset + extra >= offset) && (offset + extra + 0x8000 < 0x10000);
2509 }
2510
2511 static bool
2512 legitimate_indexed_address_p (rtx x, int strict)
2513 {
2514   rtx op0, op1;
2515
2516   if (GET_CODE (x) != PLUS)
2517     return false;
2518   op0 = XEXP (x, 0);
2519   op1 = XEXP (x, 1);
2520
2521   if (!REG_P (op0) || !REG_P (op1))
2522     return false;
2523
2524   return ((INT_REG_OK_FOR_BASE_P (op0, strict)
2525            && INT_REG_OK_FOR_INDEX_P (op1, strict))
2526           || (INT_REG_OK_FOR_BASE_P (op1, strict)
2527               && INT_REG_OK_FOR_INDEX_P (op0, strict)));
2528 }
2529
2530 static inline bool
2531 legitimate_indirect_address_p (rtx x, int strict)
2532 {
2533   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
2534 }
2535
2536 static bool
2537 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
2538 {
2539     if (!TARGET_MACHO || !flag_pic
2540         || mode != SImode || GET_CODE(x) != MEM)
2541       return false;
2542     x = XEXP (x, 0);
2543
2544   if (GET_CODE (x) != LO_SUM)
2545     return false;
2546   if (GET_CODE (XEXP (x, 0)) != REG)
2547     return false;
2548   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
2549     return false;
2550   x = XEXP (x, 1);
2551
2552   return CONSTANT_P (x);
2553 }
2554
2555 static bool
2556 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
2557 {
2558   if (GET_CODE (x) != LO_SUM)
2559     return false;
2560   if (GET_CODE (XEXP (x, 0)) != REG)
2561     return false;
2562   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2563     return false;
2564   x = XEXP (x, 1);
2565
2566   if (TARGET_ELF)
2567     {
2568       if (DEFAULT_ABI != ABI_AIX && flag_pic)
2569         return false;
2570       if (TARGET_TOC)
2571         return false;
2572       if (GET_MODE_NUNITS (mode) != 1)
2573         return false;
2574       if (GET_MODE_BITSIZE (mode) > 32
2575           && !(TARGET_HARD_FLOAT && TARGET_FPRS && mode == DFmode))
2576         return false;
2577
2578       return CONSTANT_P (x);
2579     }
2580
2581   return false;
2582 }
2583
2584
2585 /* Try machine-dependent ways of modifying an illegitimate address
2586    to be legitimate.  If we find one, return the new, valid address.
2587    This is used from only one place: `memory_address' in explow.c.
2588
2589    OLDX is the address as it was before break_out_memory_refs was
2590    called.  In some cases it is useful to look at this to decide what
2591    needs to be done.
2592
2593    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
2594
2595    It is always safe for this function to do nothing.  It exists to
2596    recognize opportunities to optimize the output.
2597
2598    On RS/6000, first check for the sum of a register with a constant
2599    integer that is out of range.  If so, generate code to add the
2600    constant with the low-order 16 bits masked to the register and force
2601    this result into another register (this can be done with `cau').
2602    Then generate an address of REG+(CONST&0xffff), allowing for the
2603    possibility of bit 16 being a one.
2604
2605    Then check for the sum of a register and something not constant, try to
2606    load the other things into a register and return the sum.  */
2607
2608 rtx
2609 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2610                            enum machine_mode mode)
2611 {
2612   if (GET_CODE (x) == SYMBOL_REF)
2613     {
2614       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
2615       if (model != 0)
2616         return rs6000_legitimize_tls_address (x, model);
2617     }
2618
2619   if (GET_CODE (x) == PLUS 
2620       && GET_CODE (XEXP (x, 0)) == REG
2621       && GET_CODE (XEXP (x, 1)) == CONST_INT
2622       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2623     { 
2624       HOST_WIDE_INT high_int, low_int;
2625       rtx sum;
2626       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2627       high_int = INTVAL (XEXP (x, 1)) - low_int;
2628       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2629                                          GEN_INT (high_int)), 0);
2630       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2631     }
2632   else if (GET_CODE (x) == PLUS 
2633            && GET_CODE (XEXP (x, 0)) == REG
2634            && GET_CODE (XEXP (x, 1)) != CONST_INT
2635            && GET_MODE_NUNITS (mode) == 1
2636            && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2637                || TARGET_POWERPC64
2638                || (mode != DFmode && mode != TFmode))
2639            && (TARGET_POWERPC64 || mode != DImode)
2640            && mode != TImode)
2641     {
2642       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2643                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2644     }
2645   else if (ALTIVEC_VECTOR_MODE (mode))
2646     {
2647       rtx reg;
2648
2649       /* Make sure both operands are registers.  */
2650       if (GET_CODE (x) == PLUS)
2651         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
2652                              force_reg (Pmode, XEXP (x, 1)));
2653
2654       reg = force_reg (Pmode, x);
2655       return reg;
2656     }
2657   else if (SPE_VECTOR_MODE (mode))
2658     {
2659       /* We accept [reg + reg] and [reg + OFFSET].  */
2660
2661       if (GET_CODE (x) == PLUS)
2662       {
2663         rtx op1 = XEXP (x, 0);
2664         rtx op2 = XEXP (x, 1);
2665
2666         op1 = force_reg (Pmode, op1);
2667
2668         if (GET_CODE (op2) != REG
2669             && (GET_CODE (op2) != CONST_INT
2670                 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2671           op2 = force_reg (Pmode, op2);
2672
2673         return gen_rtx_PLUS (Pmode, op1, op2);
2674       }
2675
2676       return force_reg (Pmode, x);
2677     }
2678   else if (TARGET_ELF
2679            && TARGET_32BIT
2680            && TARGET_NO_TOC
2681            && ! flag_pic
2682            && GET_CODE (x) != CONST_INT
2683            && GET_CODE (x) != CONST_DOUBLE 
2684            && CONSTANT_P (x)
2685            && GET_MODE_NUNITS (mode) == 1
2686            && (GET_MODE_BITSIZE (mode) <= 32
2687                || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
2688     {
2689       rtx reg = gen_reg_rtx (Pmode);
2690       emit_insn (gen_elf_high (reg, x));
2691       return gen_rtx_LO_SUM (Pmode, reg, x);
2692     }
2693   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2694            && ! flag_pic
2695 #if TARGET_MACHO
2696            && ! MACHO_DYNAMIC_NO_PIC_P
2697 #endif
2698            && GET_CODE (x) != CONST_INT
2699            && GET_CODE (x) != CONST_DOUBLE 
2700            && CONSTANT_P (x)
2701            && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
2702            && mode != DImode 
2703            && mode != TImode)
2704     {
2705       rtx reg = gen_reg_rtx (Pmode);
2706       emit_insn (gen_macho_high (reg, x));
2707       return gen_rtx_LO_SUM (Pmode, reg, x);
2708     }
2709   else if (TARGET_TOC 
2710            && constant_pool_expr_p (x)
2711            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
2712     {
2713       return create_TOC_reference (x);
2714     }
2715   else
2716     return NULL_RTX;
2717 }
2718
2719 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
2720
2721 static GTY(()) rtx rs6000_tls_symbol;
2722 static rtx
2723 rs6000_tls_get_addr (void)
2724 {
2725   if (!rs6000_tls_symbol)
2726     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
2727
2728   return rs6000_tls_symbol;
2729 }
2730
2731 /* Construct the SYMBOL_REF for TLS GOT references.  */
2732
2733 static GTY(()) rtx rs6000_got_symbol;
2734 static rtx
2735 rs6000_got_sym (void)
2736 {
2737   if (!rs6000_got_symbol)
2738     {
2739       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2740       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
2741       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
2742     }     
2743
2744   return rs6000_got_symbol;
2745 }
2746
2747 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
2748    this (thread-local) address.  */
2749
2750 static rtx
2751 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
2752 {
2753   rtx dest, insn;
2754
2755   dest = gen_reg_rtx (Pmode);
2756   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
2757     {
2758       rtx tlsreg;
2759
2760       if (TARGET_64BIT)
2761         {
2762           tlsreg = gen_rtx_REG (Pmode, 13);
2763           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
2764         }
2765       else
2766         {
2767           tlsreg = gen_rtx_REG (Pmode, 2);
2768           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
2769         }
2770       emit_insn (insn);
2771     }
2772   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
2773     {
2774       rtx tlsreg, tmp;
2775
2776       tmp = gen_reg_rtx (Pmode);
2777       if (TARGET_64BIT)
2778         {
2779           tlsreg = gen_rtx_REG (Pmode, 13);
2780           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
2781         }
2782       else
2783         {
2784           tlsreg = gen_rtx_REG (Pmode, 2);
2785           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
2786         }
2787       emit_insn (insn);
2788       if (TARGET_64BIT)
2789         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
2790       else
2791         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
2792       emit_insn (insn);
2793     }
2794   else
2795     {
2796       rtx r3, got, tga, tmp1, tmp2, eqv;
2797
2798       if (TARGET_64BIT)
2799         got = gen_rtx_REG (Pmode, TOC_REGISTER);
2800       else
2801         {
2802           if (flag_pic == 1)
2803             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
2804           else
2805             {
2806               rtx gsym = rs6000_got_sym ();
2807               got = gen_reg_rtx (Pmode);
2808               if (flag_pic == 0)
2809                 rs6000_emit_move (got, gsym, Pmode);
2810               else
2811                 {
2812                   char buf[30];
2813                   static int tls_got_labelno = 0;
2814                   rtx tempLR, lab, tmp3, mem;
2815                   rtx first, last;
2816
2817                   ASM_GENERATE_INTERNAL_LABEL (buf, "LTLS", tls_got_labelno++);
2818                   lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2819                   tempLR = gen_reg_rtx (Pmode);
2820                   tmp1 = gen_reg_rtx (Pmode);
2821                   tmp2 = gen_reg_rtx (Pmode);
2822                   tmp3 = gen_reg_rtx (Pmode);
2823                   mem = gen_rtx_MEM (Pmode, tmp1);
2824                   RTX_UNCHANGING_P (mem) = 1;
2825
2826                   first = emit_insn (gen_load_toc_v4_PIC_1b (tempLR, lab,
2827                                                              gsym));
2828                   emit_move_insn (tmp1, tempLR);
2829                   emit_move_insn (tmp2, mem);
2830                   emit_insn (gen_addsi3 (tmp3, tmp1, tmp2));
2831                   last = emit_move_insn (got, tmp3);
2832                   REG_NOTES (last) = gen_rtx_EXPR_LIST (REG_EQUAL, gsym,
2833                                                         REG_NOTES (last));
2834                   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2835                                                          REG_NOTES (first));
2836                   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
2837                                                         REG_NOTES (last));
2838                 }
2839             }
2840         }
2841
2842       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
2843         {
2844           r3 = gen_rtx_REG (Pmode, 3);
2845           if (TARGET_64BIT)
2846             insn = gen_tls_gd_64 (r3, got, addr);
2847           else
2848             insn = gen_tls_gd_32 (r3, got, addr);
2849           start_sequence ();
2850           emit_insn (insn);
2851           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2852           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2853           insn = emit_call_insn (insn);
2854           CONST_OR_PURE_CALL_P (insn) = 1;
2855           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2856           insn = get_insns ();
2857           end_sequence ();
2858           emit_libcall_block (insn, dest, r3, addr);
2859         }
2860       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
2861         {
2862           r3 = gen_rtx_REG (Pmode, 3);
2863           if (TARGET_64BIT)
2864             insn = gen_tls_ld_64 (r3, got);
2865           else
2866             insn = gen_tls_ld_32 (r3, got);
2867           start_sequence ();
2868           emit_insn (insn);
2869           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2870           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2871           insn = emit_call_insn (insn);
2872           CONST_OR_PURE_CALL_P (insn) = 1;
2873           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2874           insn = get_insns ();
2875           end_sequence ();
2876           tmp1 = gen_reg_rtx (Pmode);
2877           eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2878                                 UNSPEC_TLSLD);
2879           emit_libcall_block (insn, tmp1, r3, eqv);
2880           if (rs6000_tls_size == 16)
2881             {
2882               if (TARGET_64BIT)
2883                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
2884               else
2885                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
2886             }
2887           else if (rs6000_tls_size == 32)
2888             {
2889               tmp2 = gen_reg_rtx (Pmode);
2890               if (TARGET_64BIT)
2891                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
2892               else
2893                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
2894               emit_insn (insn);
2895               if (TARGET_64BIT)
2896                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
2897               else
2898                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
2899             }
2900           else
2901             {
2902               tmp2 = gen_reg_rtx (Pmode);
2903               if (TARGET_64BIT)
2904                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
2905               else
2906                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
2907               emit_insn (insn);
2908               insn = gen_rtx_SET (Pmode, dest,
2909                                   gen_rtx_PLUS (Pmode, tmp2, tmp1));
2910             }
2911           emit_insn (insn);
2912         }
2913       else
2914         {
2915           /* IE, or 64 bit offset LE.  */
2916           tmp2 = gen_reg_rtx (Pmode);
2917           if (TARGET_64BIT)
2918             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
2919           else
2920             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
2921           emit_insn (insn);
2922           if (TARGET_64BIT)
2923             insn = gen_tls_tls_64 (dest, tmp2, addr);
2924           else
2925             insn = gen_tls_tls_32 (dest, tmp2, addr);
2926           emit_insn (insn);
2927         }
2928     }
2929
2930   return dest;
2931 }
2932
2933 /* Return 1 if X is a SYMBOL_REF for a TLS symbol.  This is used in
2934    instruction definitions.  */
2935
2936 int
2937 rs6000_tls_symbol_ref (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
2938 {
2939   return RS6000_SYMBOL_REF_TLS_P (x);
2940 }
2941
2942 /* Return 1 if X contains a thread-local symbol.  */
2943
2944 bool
2945 rs6000_tls_referenced_p (rtx x)
2946 {
2947   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
2948 }
2949
2950 /* Return 1 if *X is a thread-local symbol.  This is the same as
2951    rs6000_tls_symbol_ref except for the type of the unused argument.  */
2952
2953 static inline int
2954 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
2955 {
2956   return RS6000_SYMBOL_REF_TLS_P (*x);
2957 }
2958
2959 /* The convention appears to be to define this wherever it is used.
2960    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
2961    is now used here.  */
2962 #ifndef REG_MODE_OK_FOR_BASE_P
2963 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
2964 #endif
2965
2966 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
2967    replace the input X, or the original X if no replacement is called for.
2968    The output parameter *WIN is 1 if the calling macro should goto WIN,
2969    0 if it should not.
2970
2971    For RS/6000, we wish to handle large displacements off a base
2972    register by splitting the addend across an addiu/addis and the mem insn.
2973    This cuts number of extra insns needed from 3 to 1.
2974
2975    On Darwin, we use this to generate code for floating point constants.
2976    A movsf_low is generated so we wind up with 2 instructions rather than 3.
2977    The Darwin code is inside #if TARGET_MACHO because only then is
2978    machopic_function_base_name() defined.  */
2979 rtx
2980 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode, 
2981         int opnum, int type, int ind_levels ATTRIBUTE_UNUSED, int *win)
2982 {
2983   /* We must recognize output that we have already generated ourselves.  */ 
2984   if (GET_CODE (x) == PLUS
2985       && GET_CODE (XEXP (x, 0)) == PLUS
2986       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2987       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2988       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2989     {
2990       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2991                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2992                    opnum, (enum reload_type)type);
2993       *win = 1;
2994       return x;
2995     }
2996
2997 #if TARGET_MACHO
2998   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
2999       && GET_CODE (x) == LO_SUM
3000       && GET_CODE (XEXP (x, 0)) == PLUS
3001       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
3002       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
3003       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
3004       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
3005       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
3006       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
3007       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
3008     {
3009       /* Result of previous invocation of this function on Darwin
3010          floating point constant.  */
3011       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3012                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3013                 opnum, (enum reload_type)type);
3014       *win = 1;
3015       return x;
3016     }
3017 #endif
3018   if (GET_CODE (x) == PLUS
3019       && GET_CODE (XEXP (x, 0)) == REG
3020       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
3021       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
3022       && GET_CODE (XEXP (x, 1)) == CONST_INT
3023       && !SPE_VECTOR_MODE (mode)
3024       && !ALTIVEC_VECTOR_MODE (mode))
3025     {
3026       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3027       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
3028       HOST_WIDE_INT high
3029         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
3030
3031       /* Check for 32-bit overflow.  */
3032       if (high + low != val)
3033         {
3034           *win = 0;
3035           return x;
3036         }
3037
3038       /* Reload the high part into a base reg; leave the low part
3039          in the mem directly.  */
3040
3041       x = gen_rtx_PLUS (GET_MODE (x),
3042                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
3043                                       GEN_INT (high)),
3044                         GEN_INT (low));
3045
3046       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3047                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3048                    opnum, (enum reload_type)type);
3049       *win = 1;
3050       return x;
3051     }
3052 #if TARGET_MACHO
3053   if (GET_CODE (x) == SYMBOL_REF
3054       && DEFAULT_ABI == ABI_DARWIN
3055       && !ALTIVEC_VECTOR_MODE (mode)
3056       && flag_pic)
3057     {
3058       /* Darwin load of floating point constant.  */
3059       rtx offset = gen_rtx (CONST, Pmode,
3060                     gen_rtx (MINUS, Pmode, x,
3061                     gen_rtx (SYMBOL_REF, Pmode,
3062                         machopic_function_base_name ())));
3063       x = gen_rtx (LO_SUM, GET_MODE (x),
3064             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
3065                 gen_rtx (HIGH, Pmode, offset)), offset);
3066       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3067                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3068                 opnum, (enum reload_type)type);
3069       *win = 1;
3070       return x;
3071     }
3072    if (GET_CODE (x) == SYMBOL_REF
3073        && DEFAULT_ABI == ABI_DARWIN
3074        && !ALTIVEC_VECTOR_MODE (mode)
3075        && MACHO_DYNAMIC_NO_PIC_P)
3076      {
3077        /* Darwin load of floating point constant.  */
3078        x = gen_rtx (LO_SUM, GET_MODE (x),
3079                gen_rtx (HIGH, Pmode, x), x);
3080        push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3081                BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3082                opnum, (enum reload_type)type);
3083        *win = 1;
3084        return x;
3085      }
3086 #endif
3087   if (TARGET_TOC
3088       && constant_pool_expr_p (x)
3089       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
3090     {
3091       (x) = create_TOC_reference (x);
3092       *win = 1;
3093       return x;
3094     }
3095   *win = 0;
3096   return x;
3097 }    
3098
3099 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
3100    that is a valid memory address for an instruction.
3101    The MODE argument is the machine mode for the MEM expression
3102    that wants to use this address.
3103
3104    On the RS/6000, there are four valid address: a SYMBOL_REF that
3105    refers to a constant pool entry of an address (or the sum of it
3106    plus a constant), a short (16-bit signed) constant plus a register,
3107    the sum of two registers, or a register indirect, possibly with an
3108    auto-increment.  For DFmode and DImode with a constant plus register,
3109    we must ensure that both words are addressable or PowerPC64 with offset
3110    word aligned.
3111
3112    For modes spanning multiple registers (DFmode in 32-bit GPRs,
3113    32-bit DImode, TImode), indexed addressing cannot be used because
3114    adjacent memory cells are accessed by adding word-sized offsets
3115    during assembly output.  */
3116 int
3117 rs6000_legitimate_address (enum machine_mode mode, rtx x, int reg_ok_strict)
3118 {
3119   if (RS6000_SYMBOL_REF_TLS_P (x))
3120     return 0;
3121   if (legitimate_indirect_address_p (x, reg_ok_strict))
3122     return 1;
3123   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
3124       && !ALTIVEC_VECTOR_MODE (mode)
3125       && !SPE_VECTOR_MODE (mode)
3126       && TARGET_UPDATE
3127       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
3128     return 1;
3129   if (legitimate_small_data_p (mode, x))
3130     return 1;
3131   if (legitimate_constant_pool_address_p (x))
3132     return 1;
3133   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
3134   if (! reg_ok_strict
3135       && GET_CODE (x) == PLUS
3136       && GET_CODE (XEXP (x, 0)) == REG
3137       && XEXP (x, 0) == virtual_stack_vars_rtx
3138       && GET_CODE (XEXP (x, 1)) == CONST_INT)
3139     return 1;
3140   if (legitimate_offset_address_p (mode, x, reg_ok_strict))
3141     return 1;
3142   if (mode != TImode
3143       && ((TARGET_HARD_FLOAT && TARGET_FPRS)
3144           || TARGET_POWERPC64
3145           || (mode != DFmode && mode != TFmode))
3146       && (TARGET_POWERPC64 || mode != DImode)
3147       && legitimate_indexed_address_p (x, reg_ok_strict))
3148     return 1;
3149   if (legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
3150     return 1;
3151   return 0;
3152 }
3153
3154 /* Go to LABEL if ADDR (a legitimate address expression)
3155    has an effect that depends on the machine mode it is used for.
3156
3157    On the RS/6000 this is true of all integral offsets (since AltiVec
3158    modes don't allow them) or is a pre-increment or decrement.
3159
3160    ??? Except that due to conceptual problems in offsettable_address_p
3161    we can't really report the problems of integral offsets.  So leave
3162    this assuming that the adjustable offset must be valid for the 
3163    sub-words of a TFmode operand, which is what we had before.  */
3164
3165 bool
3166 rs6000_mode_dependent_address (rtx addr)
3167 {
3168   switch (GET_CODE (addr))
3169     {
3170     case PLUS:
3171       if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3172         {
3173           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
3174           return val + 12 + 0x8000 >= 0x10000;
3175         }
3176       break;
3177
3178     case LO_SUM:
3179       return true;
3180
3181     case PRE_INC:
3182     case PRE_DEC:
3183       return TARGET_UPDATE;
3184
3185     default:
3186       break;
3187     }
3188
3189   return false;
3190 }
3191 \f
3192 /* Try to output insns to set TARGET equal to the constant C if it can
3193    be done in less than N insns.  Do all computations in MODE.
3194    Returns the place where the output has been placed if it can be
3195    done and the insns have been emitted.  If it would take more than N
3196    insns, zero is returned and no insns and emitted.  */
3197
3198 rtx
3199 rs6000_emit_set_const (rtx dest, enum machine_mode mode, 
3200                        rtx source, int n ATTRIBUTE_UNUSED)
3201 {
3202   rtx result, insn, set;
3203   HOST_WIDE_INT c0, c1;
3204
3205   if (mode == QImode || mode == HImode)
3206     {
3207       if (dest == NULL)
3208         dest = gen_reg_rtx (mode);
3209       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
3210       return dest;
3211     }
3212   else if (mode == SImode)
3213     {
3214       result = no_new_pseudos ? dest : gen_reg_rtx (SImode);
3215
3216       emit_insn (gen_rtx_SET (VOIDmode, result,
3217                               GEN_INT (INTVAL (source)
3218                                        & (~ (HOST_WIDE_INT) 0xffff))));
3219       emit_insn (gen_rtx_SET (VOIDmode, dest,
3220                               gen_rtx_IOR (SImode, result,
3221                                            GEN_INT (INTVAL (source) & 0xffff))));
3222       result = dest;
3223     }
3224   else if (mode == DImode)
3225     {
3226       if (GET_CODE (source) == CONST_INT)
3227         {
3228           c0 = INTVAL (source);
3229           c1 = -(c0 < 0);
3230         }
3231       else if (GET_CODE (source) == CONST_DOUBLE)
3232         {
3233 #if HOST_BITS_PER_WIDE_INT >= 64
3234           c0 = CONST_DOUBLE_LOW (source);
3235           c1 = -(c0 < 0);
3236 #else
3237           c0 = CONST_DOUBLE_LOW (source);
3238           c1 = CONST_DOUBLE_HIGH (source);
3239 #endif
3240         }
3241       else
3242         abort ();
3243
3244       result = rs6000_emit_set_long_const (dest, c0, c1);
3245     }
3246   else
3247     abort ();
3248
3249   insn = get_last_insn ();
3250   set = single_set (insn);
3251   if (! CONSTANT_P (SET_SRC (set)))
3252     set_unique_reg_note (insn, REG_EQUAL, source);
3253
3254   return result;
3255 }
3256
3257 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
3258    fall back to a straight forward decomposition.  We do this to avoid
3259    exponential run times encountered when looking for longer sequences
3260    with rs6000_emit_set_const.  */
3261 static rtx
3262 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
3263 {
3264   if (!TARGET_POWERPC64)
3265     {
3266       rtx operand1, operand2;
3267
3268       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
3269                                         DImode);
3270       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
3271                                         DImode);
3272       emit_move_insn (operand1, GEN_INT (c1));
3273       emit_move_insn (operand2, GEN_INT (c2));
3274     }
3275   else
3276     {
3277       HOST_WIDE_INT ud1, ud2, ud3, ud4;
3278
3279       ud1 = c1 & 0xffff;
3280       ud2 = (c1 & 0xffff0000) >> 16;
3281 #if HOST_BITS_PER_WIDE_INT >= 64
3282       c2 = c1 >> 32;
3283 #endif
3284       ud3 = c2 & 0xffff;
3285       ud4 = (c2 & 0xffff0000) >> 16;
3286
3287       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
3288           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
3289         {
3290           if (ud1 & 0x8000)
3291             emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) -  0x8000)));
3292           else
3293             emit_move_insn (dest, GEN_INT (ud1));
3294         }
3295
3296       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
3297                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
3298         {
3299           if (ud2 & 0x8000)
3300             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
3301                                            - 0x80000000));
3302           else
3303             emit_move_insn (dest, GEN_INT (ud2 << 16));
3304           if (ud1 != 0)
3305             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3306         }
3307       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
3308                || (ud4 == 0 && ! (ud3 & 0x8000)))
3309         {
3310           if (ud3 & 0x8000)
3311             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
3312                                            - 0x80000000));
3313           else
3314             emit_move_insn (dest, GEN_INT (ud3 << 16));
3315
3316           if (ud2 != 0)
3317             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
3318           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
3319           if (ud1 != 0)
3320             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3321         }
3322       else 
3323         {
3324           if (ud4 & 0x8000)
3325             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
3326                                            - 0x80000000));
3327           else
3328             emit_move_insn (dest, GEN_INT (ud4 << 16));
3329
3330           if (ud3 != 0)
3331             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
3332
3333           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
3334           if (ud2 != 0)
3335             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
3336                                                GEN_INT (ud2 << 16)));   
3337           if (ud1 != 0)
3338             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3339         }
3340     }
3341   return dest;
3342 }
3343
3344 /* Emit a move from SOURCE to DEST in mode MODE.  */
3345 void
3346 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
3347 {
3348   rtx operands[2];
3349   operands[0] = dest;
3350   operands[1] = source;
3351   
3352   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
3353   if (GET_CODE (operands[1]) == CONST_DOUBLE
3354       && ! FLOAT_MODE_P (mode)
3355       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3356     {
3357       /* FIXME.  This should never happen.  */
3358       /* Since it seems that it does, do the safe thing and convert
3359          to a CONST_INT.  */
3360       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
3361     }
3362   if (GET_CODE (operands[1]) == CONST_DOUBLE
3363       && ! FLOAT_MODE_P (mode)
3364       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
3365            && CONST_DOUBLE_LOW (operands[1]) >= 0)
3366           || (CONST_DOUBLE_HIGH (operands[1]) == -1
3367               && CONST_DOUBLE_LOW (operands[1]) < 0)))
3368     abort ();
3369
3370   /* Check if GCC is setting up a block move that will end up using FP
3371      registers as temporaries.  We must make sure this is acceptable.  */
3372   if (GET_CODE (operands[0]) == MEM
3373       && GET_CODE (operands[1]) == MEM
3374       && mode == DImode
3375       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
3376           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
3377       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
3378                                             ? 32 : MEM_ALIGN (operands[0])))
3379             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
3380                                                ? 32 
3381                                                : MEM_ALIGN (operands[1]))))
3382       && ! MEM_VOLATILE_P (operands [0])
3383       && ! MEM_VOLATILE_P (operands [1]))
3384     {
3385       emit_move_insn (adjust_address (operands[0], SImode, 0),
3386                       adjust_address (operands[1], SImode, 0));
3387       emit_move_insn (adjust_address (operands[0], SImode, 4),
3388                       adjust_address (operands[1], SImode, 4));
3389       return;
3390     }
3391   
3392   if (!no_new_pseudos)
3393     {
3394       if (GET_CODE (operands[1]) == MEM && optimize > 0
3395           && (mode == QImode || mode == HImode || mode == SImode)
3396           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3397         {
3398           rtx reg = gen_reg_rtx (word_mode);
3399
3400           emit_insn (gen_rtx_SET (word_mode, reg,
3401                                   gen_rtx_ZERO_EXTEND (word_mode,
3402                                                        operands[1])));
3403           operands[1] = gen_lowpart (mode, reg);
3404         }
3405       if (GET_CODE (operands[0]) != REG)
3406         operands[1] = force_reg (mode, operands[1]);
3407     }
3408
3409   if (mode == SFmode && ! TARGET_POWERPC
3410       && TARGET_HARD_FLOAT && TARGET_FPRS
3411       && GET_CODE (operands[0]) == MEM)
3412     {
3413       int regnum;
3414
3415       if (reload_in_progress || reload_completed)
3416         regnum = true_regnum (operands[1]);
3417       else if (GET_CODE (operands[1]) == REG)
3418         regnum = REGNO (operands[1]);
3419       else
3420         regnum = -1;
3421       
3422       /* If operands[1] is a register, on POWER it may have
3423          double-precision data in it, so truncate it to single
3424          precision.  */
3425       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
3426         {
3427           rtx newreg;
3428           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
3429           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
3430           operands[1] = newreg;
3431         }
3432     }
3433
3434   /* Recognize the case where operand[1] is a reference to thread-local
3435      data and load its address to a register.  */
3436   if (GET_CODE (operands[1]) == SYMBOL_REF)
3437     {
3438       enum tls_model model = SYMBOL_REF_TLS_MODEL (operands[1]);
3439       if (model != 0)
3440         operands[1] = rs6000_legitimize_tls_address (operands[1], model);
3441     }
3442
3443   /* Handle the case where reload calls us with an invalid address.  */
3444   if (reload_in_progress && mode == Pmode
3445       && (! general_operand (operands[1], mode)
3446           || ! nonimmediate_operand (operands[0], mode)))
3447     goto emit_set;
3448
3449   /* Handle the case of CONSTANT_P_RTX.  */
3450   if (GET_CODE (operands[1]) == CONSTANT_P_RTX)
3451     goto emit_set;
3452   
3453   /* FIXME:  In the long term, this switch statement should go away
3454      and be replaced by a sequence of tests based on things like
3455      mode == Pmode.  */
3456   switch (mode)
3457     {
3458     case HImode:
3459     case QImode:
3460       if (CONSTANT_P (operands[1])
3461           && GET_CODE (operands[1]) != CONST_INT)
3462         operands[1] = force_const_mem (mode, operands[1]);
3463       break;
3464
3465     case TFmode:
3466     case DFmode:
3467     case SFmode:
3468       if (CONSTANT_P (operands[1]) 
3469           && ! easy_fp_constant (operands[1], mode))
3470         operands[1] = force_const_mem (mode, operands[1]);
3471       break;
3472       
3473     case V16QImode:
3474     case V8HImode:
3475     case V4SFmode:
3476     case V4SImode:
3477     case V4HImode:
3478     case V2SFmode:
3479     case V2SImode:
3480     case V1DImode:
3481       if (CONSTANT_P (operands[1])
3482           && !easy_vector_constant (operands[1], mode))
3483         operands[1] = force_const_mem (mode, operands[1]);
3484       break;
3485       
3486     case SImode:
3487     case DImode:
3488       /* Use default pattern for address of ELF small data */
3489       if (TARGET_ELF
3490           && mode == Pmode
3491           && DEFAULT_ABI == ABI_V4
3492           && (GET_CODE (operands[1]) == SYMBOL_REF 
3493               || GET_CODE (operands[1]) == CONST)
3494           && small_data_operand (operands[1], mode))
3495         {
3496           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3497           return;
3498         }
3499
3500       if (DEFAULT_ABI == ABI_V4
3501           && mode == Pmode && mode == SImode
3502           && flag_pic == 1 && got_operand (operands[1], mode))
3503         {
3504           emit_insn (gen_movsi_got (operands[0], operands[1]));
3505           return;
3506         }
3507
3508       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
3509           && TARGET_NO_TOC
3510           && ! flag_pic
3511           && mode == Pmode
3512           && CONSTANT_P (operands[1])
3513           && GET_CODE (operands[1]) != HIGH
3514           && GET_CODE (operands[1]) != CONST_INT)
3515         {
3516           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
3517
3518           /* If this is a function address on -mcall-aixdesc,
3519              convert it to the address of the descriptor.  */
3520           if (DEFAULT_ABI == ABI_AIX
3521               && GET_CODE (operands[1]) == SYMBOL_REF
3522               && XSTR (operands[1], 0)[0] == '.')
3523             {
3524               const char *name = XSTR (operands[1], 0);
3525               rtx new_ref;
3526               while (*name == '.')
3527                 name++;
3528               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
3529               CONSTANT_POOL_ADDRESS_P (new_ref)
3530                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
3531               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
3532               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
3533               SYMBOL_REF_DECL (new_ref) = SYMBOL_REF_DECL (operands[1]);
3534               operands[1] = new_ref;
3535             }
3536
3537           if (DEFAULT_ABI == ABI_DARWIN)
3538             {
3539 #if TARGET_MACHO
3540               if (MACHO_DYNAMIC_NO_PIC_P)
3541                 {
3542                   /* Take care of any required data indirection.  */
3543                   operands[1] = rs6000_machopic_legitimize_pic_address (
3544                                   operands[1], mode, operands[0]);
3545                   if (operands[0] != operands[1])
3546                     emit_insn (gen_rtx_SET (VOIDmode,
3547                                             operands[0], operands[1]));
3548                   return;
3549                 }
3550 #endif
3551               emit_insn (gen_macho_high (target, operands[1]));
3552               emit_insn (gen_macho_low (operands[0], target, operands[1]));
3553               return;
3554             }
3555
3556           emit_insn (gen_elf_high (target, operands[1]));
3557           emit_insn (gen_elf_low (operands[0], target, operands[1]));
3558           return;
3559         }
3560
3561       /* If this is a SYMBOL_REF that refers to a constant pool entry,
3562          and we have put it in the TOC, we just need to make a TOC-relative
3563          reference to it.  */
3564       if (TARGET_TOC
3565           && GET_CODE (operands[1]) == SYMBOL_REF
3566           && constant_pool_expr_p (operands[1])
3567           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
3568                                               get_pool_mode (operands[1])))
3569         {
3570           operands[1] = create_TOC_reference (operands[1]);
3571         }
3572       else if (mode == Pmode
3573                && CONSTANT_P (operands[1])
3574                && ((GET_CODE (operands[1]) != CONST_INT
3575                     && ! easy_fp_constant (operands[1], mode))
3576                    || (GET_CODE (operands[1]) == CONST_INT
3577                        && num_insns_constant (operands[1], mode) > 2)
3578                    || (GET_CODE (operands[0]) == REG
3579                        && FP_REGNO_P (REGNO (operands[0]))))
3580                && GET_CODE (operands[1]) != HIGH
3581                && ! legitimate_constant_pool_address_p (operands[1])
3582                && ! toc_relative_expr_p (operands[1]))
3583         {
3584           /* Emit a USE operation so that the constant isn't deleted if
3585              expensive optimizations are turned on because nobody
3586              references it.  This should only be done for operands that
3587              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
3588              This should not be done for operands that contain LABEL_REFs.
3589              For now, we just handle the obvious case.  */
3590           if (GET_CODE (operands[1]) != LABEL_REF)
3591             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
3592
3593 #if TARGET_MACHO
3594           /* Darwin uses a special PIC legitimizer.  */
3595           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
3596             {
3597               operands[1] =
3598                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
3599                                                         operands[0]);
3600               if (operands[0] != operands[1])
3601                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3602               return;
3603             }
3604 #endif
3605
3606           /* If we are to limit the number of things we put in the TOC and
3607              this is a symbol plus a constant we can add in one insn,
3608              just put the symbol in the TOC and add the constant.  Don't do
3609              this if reload is in progress.  */
3610           if (GET_CODE (operands[1]) == CONST
3611               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
3612               && GET_CODE (XEXP (operands[1], 0)) == PLUS
3613               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
3614               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
3615                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
3616               && ! side_effects_p (operands[0]))
3617             {
3618               rtx sym =
3619                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
3620               rtx other = XEXP (XEXP (operands[1], 0), 1);
3621
3622               sym = force_reg (mode, sym);
3623               if (mode == SImode)
3624                 emit_insn (gen_addsi3 (operands[0], sym, other));
3625               else
3626                 emit_insn (gen_adddi3 (operands[0], sym, other));
3627               return;
3628             }
3629
3630           operands[1] = force_const_mem (mode, operands[1]);
3631
3632           if (TARGET_TOC 
3633               && constant_pool_expr_p (XEXP (operands[1], 0))
3634               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
3635                         get_pool_constant (XEXP (operands[1], 0)),
3636                         get_pool_mode (XEXP (operands[1], 0))))
3637             {
3638               operands[1]
3639                 = gen_rtx_MEM (mode,
3640                                create_TOC_reference (XEXP (operands[1], 0)));
3641               set_mem_alias_set (operands[1], get_TOC_alias_set ());
3642               RTX_UNCHANGING_P (operands[1]) = 1;
3643             }
3644         }
3645       break;
3646
3647     case TImode:
3648       if (GET_CODE (operands[0]) == MEM
3649           && GET_CODE (XEXP (operands[0], 0)) != REG
3650           && ! reload_in_progress)
3651         operands[0]
3652           = replace_equiv_address (operands[0],
3653                                    copy_addr_to_reg (XEXP (operands[0], 0)));
3654
3655       if (GET_CODE (operands[1]) == MEM
3656           && GET_CODE (XEXP (operands[1], 0)) != REG
3657           && ! reload_in_progress)
3658         operands[1]
3659           = replace_equiv_address (operands[1],
3660                                    copy_addr_to_reg (XEXP (operands[1], 0)));
3661       if (TARGET_POWER)
3662         {
3663           emit_insn (gen_rtx_PARALLEL (VOIDmode,
3664                        gen_rtvec (2,
3665                                   gen_rtx_SET (VOIDmode,
3666                                                operands[0], operands[1]),
3667                                   gen_rtx_CLOBBER (VOIDmode,
3668                                                    gen_rtx_SCRATCH (SImode)))));
3669           return;
3670         }
3671       break;
3672
3673     default:
3674       abort ();
3675     }
3676
3677   /* Above, we may have called force_const_mem which may have returned
3678      an invalid address.  If we can, fix this up; otherwise, reload will
3679      have to deal with it.  */
3680   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
3681     operands[1] = validize_mem (operands[1]);
3682
3683  emit_set:
3684   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3685 }
3686 \f
3687 /* Nonzero if we can use a floating-point register to pass this arg.  */
3688 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE)         \
3689   (GET_MODE_CLASS (MODE) == MODE_FLOAT          \
3690    && (CUM)->fregno <= FP_ARG_MAX_REG           \
3691    && TARGET_HARD_FLOAT && TARGET_FPRS)
3692
3693 /* Nonzero if we can use an AltiVec register to pass this arg.  */
3694 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED)      \
3695   (ALTIVEC_VECTOR_MODE (MODE)                           \
3696    && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG              \
3697    && TARGET_ALTIVEC_ABI                                \
3698    && (DEFAULT_ABI == ABI_V4 || (NAMED)))
3699
3700 /* Return a nonzero value to say to return the function value in
3701    memory, just as large structures are always returned.  TYPE will be
3702    the data type of the value, and FNTYPE will be the type of the
3703    function doing the returning, or @code{NULL} for libcalls.
3704
3705    The AIX ABI for the RS/6000 specifies that all structures are
3706    returned in memory.  The Darwin ABI does the same.  The SVR4 ABI
3707    specifies that structures <= 8 bytes are returned in r3/r4, but a
3708    draft put them in memory, and GCC used to implement the draft
3709    instead of the final standard.  Therefore, TARGET_AIX_STRUCT_RET
3710    controls this instead of DEFAULT_ABI; V.4 targets needing backward
3711    compatibility can change DRAFT_V4_STRUCT_RET to override the
3712    default, and -m switches get the final word.  See
3713    rs6000_override_options for more details.
3714
3715    The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
3716    long double support is enabled.  These values are returned in memory.
3717
3718    int_size_in_bytes returns -1 for variable size objects, which go in
3719    memory always.  The cast to unsigned makes -1 > 8.  */
3720
3721 static bool
3722 rs6000_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
3723 {
3724   if (AGGREGATE_TYPE_P (type)
3725       && (TARGET_AIX_STRUCT_RET
3726           || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
3727     return true;
3728   if (DEFAULT_ABI == ABI_V4 && TYPE_MODE (type) == TFmode)
3729     return true;
3730   return false;
3731 }
3732
3733 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3734    for a call to a function whose data type is FNTYPE.
3735    For a library call, FNTYPE is 0.
3736
3737    For incoming args we set the number of arguments in the prototype large
3738    so we never return a PARALLEL.  */
3739
3740 void
3741 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, 
3742         rtx libname ATTRIBUTE_UNUSED, int incoming, int libcall)
3743 {
3744   static CUMULATIVE_ARGS zero_cumulative;
3745
3746   *cum = zero_cumulative;
3747   cum->words = 0;
3748   cum->fregno = FP_ARG_MIN_REG;
3749   cum->vregno = ALTIVEC_ARG_MIN_REG;
3750   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3751   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
3752                       ? CALL_LIBCALL : CALL_NORMAL);
3753   cum->sysv_gregno = GP_ARG_MIN_REG;
3754   cum->stdarg = fntype
3755     && (TYPE_ARG_TYPES (fntype) != 0
3756         && (TREE_VALUE (tree_last  (TYPE_ARG_TYPES (fntype)))
3757             != void_type_node));
3758
3759   if (incoming)
3760     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
3761
3762   else if (cum->prototype)
3763     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
3764                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
3765                                || rs6000_return_in_memory (TREE_TYPE (fntype),
3766                                                            fntype)));
3767
3768   else
3769     cum->nargs_prototype = 0;
3770
3771   /* Check for a longcall attribute.  */
3772   if (fntype
3773       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
3774       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
3775     cum->call_cookie = CALL_LONG;
3776
3777   if (TARGET_DEBUG_ARG)
3778     {
3779       fprintf (stderr, "\ninit_cumulative_args:");
3780       if (fntype)
3781         {
3782           tree ret_type = TREE_TYPE (fntype);
3783           fprintf (stderr, " ret code = %s,",
3784                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
3785         }
3786
3787       if (cum->call_cookie & CALL_LONG)
3788         fprintf (stderr, " longcall,");
3789
3790       fprintf (stderr, " proto = %d, nargs = %d\n",
3791                cum->prototype, cum->nargs_prototype);
3792     }
3793 }
3794 \f
3795 /* If defined, a C expression which determines whether, and in which
3796    direction, to pad out an argument with extra space.  The value
3797    should be of type `enum direction': either `upward' to pad above
3798    the argument, `downward' to pad below, or `none' to inhibit
3799    padding.
3800
3801    For the AIX ABI structs are always stored left shifted in their
3802    argument slot.  */
3803
3804 enum direction
3805 function_arg_padding (enum machine_mode mode, tree type)
3806 {
3807 #ifndef AGGREGATE_PADDING_FIXED
3808 #define AGGREGATE_PADDING_FIXED 0
3809 #endif
3810 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
3811 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
3812 #endif
3813
3814   if (!AGGREGATE_PADDING_FIXED)
3815     {
3816       /* GCC used to pass structures of the same size as integer types as
3817          if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
3818          ie. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
3819          passed padded downward, except that -mstrict-align further
3820          muddied the water in that multi-component structures of 2 and 4
3821          bytes in size were passed padded upward.
3822
3823          The following arranges for best compatibility with previous
3824          versions of gcc, but removes the -mstrict-align dependency.  */
3825       if (BYTES_BIG_ENDIAN)
3826         {
3827           HOST_WIDE_INT size = 0;
3828
3829           if (mode == BLKmode)
3830             {
3831               if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
3832                 size = int_size_in_bytes (type);
3833             }
3834           else
3835             size = GET_MODE_SIZE (mode);
3836
3837           if (size == 1 || size == 2 || size == 4)
3838             return downward;
3839         }
3840       return upward;
3841     }
3842
3843   if (AGGREGATES_PAD_UPWARD_ALWAYS)
3844     {
3845       if (type != 0 && AGGREGATE_TYPE_P (type))
3846         return upward;
3847     }
3848
3849   /* Fall back to the default.  */
3850   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
3851 }
3852
3853 /* If defined, a C expression that gives the alignment boundary, in bits,
3854    of an argument with the specified mode and type.  If it is not defined, 
3855    PARM_BOUNDARY is used for all arguments.
3856    
3857    V.4 wants long longs to be double word aligned.  */
3858
3859 int
3860 function_arg_boundary (enum machine_mode mode, tree type ATTRIBUTE_UNUSED)
3861 {
3862   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
3863     return 64;
3864    else if (SPE_VECTOR_MODE (mode))
3865      return 64;
3866   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3867     return 128;
3868   else
3869     return PARM_BOUNDARY;
3870 }
3871 \f
3872 /* Update the data in CUM to advance over an argument
3873    of mode MODE and data type TYPE.
3874    (TYPE is null for libcalls where that information may not be available.)  */
3875
3876 void
3877 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
3878                       tree type, int named)
3879 {
3880   cum->nargs_prototype--;
3881
3882   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3883     {
3884       if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
3885         cum->vregno++;
3886       
3887       /* In variable-argument functions, vector arguments get GPRs allocated
3888          even if they are going to be passed in a vector register.  */
3889       if (cum->stdarg && DEFAULT_ABI != ABI_V4)
3890         {
3891           int align;
3892           
3893           /* Vector parameters must be 16-byte aligned.  This places
3894              them at 2 mod 4 in terms of words in 32-bit mode, since
3895              the parameter save area starts at offset 24 from the
3896              stack.  In 64-bit mode, they just have to start on an
3897              even word, since the parameter save area is 16-byte
3898              aligned.  Space for GPRs is reserved even if the argument
3899              will be passed in memory.  */
3900           if (TARGET_32BIT)
3901             align = ((6 - (cum->words & 3)) & 3);
3902           else
3903             align = cum->words & 1;
3904           cum->words += align + RS6000_ARG_SIZE (mode, type);
3905           
3906           if (TARGET_DEBUG_ARG)
3907             {
3908               fprintf (stderr, "function_adv: words = %2d, align=%d, ", 
3909                        cum->words, align);
3910               fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
3911                        cum->nargs_prototype, cum->prototype, 
3912                        GET_MODE_NAME (mode));
3913             }
3914         }
3915     }
3916   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
3917            && !cum->stdarg
3918            && cum->sysv_gregno <= GP_ARG_MAX_REG)
3919     cum->sysv_gregno++;
3920   else if (DEFAULT_ABI == ABI_V4)
3921     {
3922       if (TARGET_HARD_FLOAT && TARGET_FPRS
3923           && (mode == SFmode || mode == DFmode))
3924         {
3925           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3926             cum->fregno++;
3927           else
3928             {
3929               if (mode == DFmode)
3930                 cum->words += cum->words & 1;
3931               cum->words += RS6000_ARG_SIZE (mode, type);
3932             }
3933         }
3934       else
3935         {
3936           int n_words;
3937           int gregno = cum->sysv_gregno;
3938
3939           /* Aggregates and IEEE quad get passed by reference.  */
3940           if ((type && AGGREGATE_TYPE_P (type))
3941               || mode == TFmode)
3942             n_words = 1;
3943           else 
3944             n_words = RS6000_ARG_SIZE (mode, type);
3945
3946           /* Long long and SPE vectors are put in odd registers.  */
3947           if (n_words == 2 && (gregno & 1) == 0)
3948             gregno += 1;
3949
3950           /* Long long and SPE vectors are not split between registers
3951              and stack.  */
3952           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
3953             {
3954               /* Long long is aligned on the stack.  */
3955               if (n_words == 2)
3956                 cum->words += cum->words & 1;
3957               cum->words += n_words;
3958             }
3959
3960           /* Note: continuing to accumulate gregno past when we've started
3961              spilling to the stack indicates the fact that we've started
3962              spilling to the stack to expand_builtin_saveregs.  */
3963           cum->sysv_gregno = gregno + n_words;
3964         }
3965
3966       if (TARGET_DEBUG_ARG)
3967         {
3968           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3969                    cum->words, cum->fregno);
3970           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
3971                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
3972           fprintf (stderr, "mode = %4s, named = %d\n",
3973                    GET_MODE_NAME (mode), named);
3974         }
3975     }
3976   else
3977     {
3978       int align = (TARGET_32BIT && (cum->words & 1) != 0
3979                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
3980
3981       cum->words += align + RS6000_ARG_SIZE (mode, type);
3982
3983       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3984           && TARGET_HARD_FLOAT && TARGET_FPRS)
3985         cum->fregno += (mode == TFmode ? 2 : 1);
3986
3987       if (TARGET_DEBUG_ARG)
3988         {
3989           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3990                    cum->words, cum->fregno);
3991           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
3992                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
3993           fprintf (stderr, "named = %d, align = %d\n", named, align);
3994         }
3995     }
3996 }
3997
3998 /* Determine where to put a SIMD argument on the SPE.  */
3999
4000 static rtx
4001 rs6000_spe_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4002                          tree type)
4003 {
4004   if (cum->stdarg)
4005     {
4006       int gregno = cum->sysv_gregno;
4007       int n_words = RS6000_ARG_SIZE (mode, type);
4008
4009       /* SPE vectors are put in odd registers.  */
4010       if (n_words == 2 && (gregno & 1) == 0)
4011         gregno += 1;
4012
4013       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4014         {
4015           rtx r1, r2;
4016           enum machine_mode m = SImode;
4017
4018           r1 = gen_rtx_REG (m, gregno);
4019           r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
4020           r2 = gen_rtx_REG (m, gregno + 1);
4021           r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
4022           return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
4023         }
4024       else
4025         return NULL_RTX;
4026     }
4027   else
4028     {
4029       if (cum->sysv_gregno <= GP_ARG_MAX_REG)
4030         return gen_rtx_REG (mode, cum->sysv_gregno);
4031       else
4032         return NULL_RTX;
4033     }
4034 }
4035
4036 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
4037
4038 static rtx
4039 rs6000_mixed_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4040                            tree type, int align_words)
4041 {
4042   if (mode == DFmode)
4043     {
4044       /* -mpowerpc64 with 32bit ABI splits up a DFmode argument
4045          in vararg list into zero, one or two GPRs */
4046       if (align_words >= GP_ARG_NUM_REG)
4047         return gen_rtx_PARALLEL (DFmode,
4048                  gen_rtvec (2,
4049                             gen_rtx_EXPR_LIST (VOIDmode,
4050                                                NULL_RTX, const0_rtx), 
4051                             gen_rtx_EXPR_LIST (VOIDmode,
4052                                                gen_rtx_REG (mode,
4053                                                             cum->fregno),
4054                                                const0_rtx)));
4055       else if (align_words + RS6000_ARG_SIZE (mode, type)
4056                > GP_ARG_NUM_REG)
4057         /* If this is partially on the stack, then we only
4058            include the portion actually in registers here. */
4059         return gen_rtx_PARALLEL (DFmode,
4060                  gen_rtvec (2,   
4061                             gen_rtx_EXPR_LIST (VOIDmode,
4062                                                gen_rtx_REG (SImode,
4063                                                             GP_ARG_MIN_REG
4064                                                             + align_words),
4065                                                const0_rtx),
4066                             gen_rtx_EXPR_LIST (VOIDmode,
4067                                                gen_rtx_REG (mode,
4068                                                             cum->fregno),
4069                                                const0_rtx)));
4070
4071       /* split a DFmode arg into two GPRs */
4072       return gen_rtx_PARALLEL (DFmode,
4073                gen_rtvec (3,
4074                           gen_rtx_EXPR_LIST (VOIDmode,       
4075                                              gen_rtx_REG (SImode,
4076                                                           GP_ARG_MIN_REG
4077                                                           + align_words),
4078                                              const0_rtx),
4079                           gen_rtx_EXPR_LIST (VOIDmode,
4080                                              gen_rtx_REG (SImode,
4081                                                           GP_ARG_MIN_REG
4082                                                           + align_words + 1),
4083                                              GEN_INT (4)),
4084                           gen_rtx_EXPR_LIST (VOIDmode,
4085                                              gen_rtx_REG (mode, cum->fregno),
4086                                              const0_rtx)));
4087     }
4088   /* -mpowerpc64 with 32bit ABI splits up a DImode argument into one
4089      or two GPRs */
4090   else if (mode == DImode)
4091     {
4092       if (align_words < GP_ARG_NUM_REG - 1)
4093         return gen_rtx_PARALLEL (DImode,
4094                  gen_rtvec (2,
4095                             gen_rtx_EXPR_LIST (VOIDmode,
4096                                                gen_rtx_REG (SImode,
4097                                                             GP_ARG_MIN_REG
4098                                                             + align_words),
4099                                                const0_rtx),
4100                             gen_rtx_EXPR_LIST (VOIDmode,
4101                                                gen_rtx_REG (SImode,
4102                                                             GP_ARG_MIN_REG
4103                                                             + align_words + 1),
4104                                                GEN_INT (4))));
4105       else if (align_words == GP_ARG_NUM_REG - 1)
4106           return gen_rtx_PARALLEL (DImode,
4107                    gen_rtvec (2,
4108                               gen_rtx_EXPR_LIST (VOIDmode,
4109                                                  NULL_RTX, const0_rtx),
4110                               gen_rtx_EXPR_LIST (VOIDmode,
4111                                                  gen_rtx_REG (SImode,
4112                                                               GP_ARG_MIN_REG
4113                                                               + align_words),
4114                                                  const0_rtx)));
4115     }
4116   else if (mode == BLKmode && align_words <= (GP_ARG_NUM_REG - 1))
4117     {
4118       int k;
4119       int size = int_size_in_bytes (type);
4120       int no_units = ((size - 1) / 4) + 1;
4121       int max_no_words = GP_ARG_NUM_REG - align_words;
4122       int rtlvec_len = no_units < max_no_words ? no_units : max_no_words;
4123       rtx *rtlvec = (rtx *) alloca (rtlvec_len * sizeof (rtx));
4124
4125       memset ((char *) rtlvec, 0, rtlvec_len * sizeof (rtx));
4126
4127       for (k=0; k < rtlvec_len; k++)
4128         rtlvec[k] = gen_rtx_EXPR_LIST (VOIDmode,
4129                                        gen_rtx_REG (SImode,
4130                                                     GP_ARG_MIN_REG
4131                                                     + align_words + k),
4132                                        k == 0 ? const0_rtx : GEN_INT (k*4));
4133
4134       return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k, rtlvec));
4135   }
4136
4137   return NULL_RTX;
4138 }
4139
4140 /* Determine where to put an argument to a function.
4141    Value is zero to push the argument on the stack,
4142    or a hard register in which to store the argument.
4143
4144    MODE is the argument's machine mode.
4145    TYPE is the data type of the argument (as a tree).
4146     This is null for libcalls where that information may
4147     not be available.
4148    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4149     the preceding args and about the function being called.
4150    NAMED is nonzero if this argument is a named parameter
4151     (otherwise it is an extra parameter matching an ellipsis).
4152
4153    On RS/6000 the first eight words of non-FP are normally in registers
4154    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
4155    Under V.4, the first 8 FP args are in registers.
4156
4157    If this is floating-point and no prototype is specified, we use
4158    both an FP and integer register (or possibly FP reg and stack).  Library
4159    functions (when CALL_LIBCALL is set) always have the proper types for args,
4160    so we can pass the FP value just in one register.  emit_library_function
4161    doesn't support PARALLEL anyway.  */
4162
4163 struct rtx_def *
4164 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4165               tree type, int named)
4166 {
4167   enum rs6000_abi abi = DEFAULT_ABI;
4168
4169   /* Return a marker to indicate whether CR1 needs to set or clear the
4170      bit that V.4 uses to say fp args were passed in registers.
4171      Assume that we don't need the marker for software floating point,
4172      or compiler generated library calls.  */
4173   if (mode == VOIDmode)
4174     {
4175       if (abi == ABI_V4
4176           && cum->nargs_prototype < 0
4177           && (cum->call_cookie & CALL_LIBCALL) == 0
4178           && (cum->prototype || TARGET_NO_PROTOTYPE))
4179         {
4180           /* For the SPE, we need to crxor CR6 always.  */
4181           if (TARGET_SPE_ABI)
4182             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
4183           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
4184             return GEN_INT (cum->call_cookie
4185                             | ((cum->fregno == FP_ARG_MIN_REG)
4186                                ? CALL_V4_SET_FP_ARGS
4187                                : CALL_V4_CLEAR_FP_ARGS));
4188         }
4189
4190       return GEN_INT (cum->call_cookie);
4191     }
4192
4193   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
4194     return gen_rtx_REG (mode, cum->vregno);
4195   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4196     {
4197       if (named || abi == ABI_V4)
4198         return NULL_RTX;
4199       else
4200         {
4201           /* Vector parameters to varargs functions under AIX or Darwin
4202              get passed in memory and possibly also in GPRs.  */
4203           int align, align_words;
4204           enum machine_mode part_mode = mode;
4205
4206           /* Vector parameters must be 16-byte aligned.  This places them at
4207              2 mod 4 in terms of words in 32-bit mode, since the parameter
4208              save area starts at offset 24 from the stack.  In 64-bit mode,
4209              they just have to start on an even word, since the parameter
4210              save area is 16-byte aligned.  */
4211           if (TARGET_32BIT)
4212             align = ((6 - (cum->words & 3)) & 3);
4213           else
4214             align = cum->words & 1;
4215           align_words = cum->words + align;
4216
4217           /* Out of registers?  Memory, then.  */
4218           if (align_words >= GP_ARG_NUM_REG)
4219             return NULL_RTX;
4220           
4221           /* The vector value goes in GPRs.  Only the part of the
4222              value in GPRs is reported here.  */
4223           if (align_words + CLASS_MAX_NREGS (mode, GENERAL_REGS)
4224               > GP_ARG_NUM_REG)
4225             /* Fortunately, there are only two possibilites, the value
4226                is either wholly in GPRs or half in GPRs and half not.  */
4227             part_mode = DImode;
4228           
4229           return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
4230         }
4231     }
4232   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
4233     return rs6000_spe_function_arg (cum, mode, type);
4234   else if (abi == ABI_V4)
4235     {
4236       if (TARGET_HARD_FLOAT && TARGET_FPRS
4237           && (mode == SFmode || mode == DFmode))
4238         {
4239           if (cum->fregno <= FP_ARG_V4_MAX_REG)
4240             return gen_rtx_REG (mode, cum->fregno);
4241           else
4242             return NULL_RTX;
4243         }
4244       else
4245         {
4246           int n_words;
4247           int gregno = cum->sysv_gregno;
4248
4249           /* Aggregates and IEEE quad get passed by reference.  */
4250           if ((type && AGGREGATE_TYPE_P (type))
4251               || mode == TFmode)
4252             n_words = 1;
4253           else 
4254             n_words = RS6000_ARG_SIZE (mode, type);
4255
4256           /* Long long and SPE vectors are put in odd registers.  */
4257           if (n_words == 2 && (gregno & 1) == 0)
4258             gregno += 1;
4259
4260           /* Long long do not split between registers and stack.  */
4261           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4262             return gen_rtx_REG (mode, gregno);
4263           else
4264             return NULL_RTX;
4265         }
4266     }
4267   else
4268     {
4269       int align = (TARGET_32BIT && (cum->words & 1) != 0
4270                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
4271       int align_words = cum->words + align;
4272
4273       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4274         return NULL_RTX;
4275
4276       if (TARGET_32BIT && TARGET_POWERPC64
4277           && (mode == DFmode || mode == DImode || mode == BLKmode))
4278         return rs6000_mixed_function_arg (cum, mode, type, align_words);
4279
4280       if (USE_FP_FOR_ARG_P (cum, mode, type))
4281         {
4282           if (! type
4283               || ((cum->nargs_prototype > 0)
4284                   /* IBM AIX extended its linkage convention definition always
4285                      to require FP args after register save area hole on the
4286                      stack.  */
4287                   && (DEFAULT_ABI != ABI_AIX
4288                       || ! TARGET_XL_CALL
4289                       || (align_words < GP_ARG_NUM_REG))))
4290             return gen_rtx_REG (mode, cum->fregno);
4291
4292           return gen_rtx_PARALLEL (mode,
4293             gen_rtvec (2,
4294                        gen_rtx_EXPR_LIST (VOIDmode,
4295                                 ((align_words >= GP_ARG_NUM_REG)
4296                                  ? NULL_RTX
4297                                  : (align_words
4298                                     + RS6000_ARG_SIZE (mode, type)
4299                                     > GP_ARG_NUM_REG
4300                                     /* If this is partially on the stack, then
4301                                        we only include the portion actually
4302                                        in registers here.  */
4303                                     ? gen_rtx_REG (SImode,
4304                                                GP_ARG_MIN_REG + align_words)
4305                                     : gen_rtx_REG (mode,
4306                                                GP_ARG_MIN_REG + align_words))),
4307                                 const0_rtx),
4308                        gen_rtx_EXPR_LIST (VOIDmode,
4309                                 gen_rtx_REG (mode, cum->fregno),
4310                                 const0_rtx)));
4311         }
4312       else if (align_words < GP_ARG_NUM_REG)
4313         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4314       else
4315         return NULL_RTX;
4316     }
4317 }
4318 \f
4319 /* For an arg passed partly in registers and partly in memory,
4320    this is the number of registers used.
4321    For args passed entirely in registers or entirely in memory, zero.  */
4322
4323 int
4324 function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4325                             tree type, int named)
4326 {
4327   if (DEFAULT_ABI == ABI_V4)
4328     return 0;
4329
4330   if (USE_FP_FOR_ARG_P (cum, mode, type)
4331       || USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
4332     {
4333       if (cum->nargs_prototype >= 0)
4334         return 0;
4335     }
4336
4337   if (cum->words < GP_ARG_NUM_REG
4338       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
4339     {
4340       int ret = GP_ARG_NUM_REG - cum->words;
4341       if (ret && TARGET_DEBUG_ARG)
4342         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
4343
4344       return ret;
4345     }
4346
4347   return 0;
4348 }
4349 \f
4350 /* A C expression that indicates when an argument must be passed by
4351    reference.  If nonzero for an argument, a copy of that argument is
4352    made in memory and a pointer to the argument is passed instead of
4353    the argument itself.  The pointer is passed in whatever way is
4354    appropriate for passing a pointer to that type.
4355
4356    Under V.4, structures and unions are passed by reference.
4357
4358    As an extension to all ABIs, variable sized types are passed by
4359    reference.  */
4360
4361 int
4362 function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED, 
4363                                 enum machine_mode mode ATTRIBUTE_UNUSED, 
4364                                 tree type, int named ATTRIBUTE_UNUSED)
4365 {
4366   if (DEFAULT_ABI == ABI_V4
4367       && ((type && AGGREGATE_TYPE_P (type))
4368           || mode == TFmode))
4369     {
4370       if (TARGET_DEBUG_ARG)
4371         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
4372
4373       return 1;
4374     }
4375   return type && int_size_in_bytes (type) < 0;
4376 }
4377 \f
4378 /* Perform any needed actions needed for a function that is receiving a
4379    variable number of arguments. 
4380
4381    CUM is as above.
4382
4383    MODE and TYPE are the mode and type of the current parameter.
4384
4385    PRETEND_SIZE is a variable that should be set to the amount of stack
4386    that must be pushed by the prolog to pretend that our caller pushed
4387    it.
4388
4389    Normally, this macro will push all remaining incoming registers on the
4390    stack and set PRETEND_SIZE to the length of the registers pushed.  */
4391
4392 static void
4393 setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4394                 tree type, int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
4395 {
4396   CUMULATIVE_ARGS next_cum;
4397   int reg_size = TARGET_32BIT ? 4 : 8;
4398   rtx save_area = NULL_RTX, mem;
4399   int first_reg_offset, set;
4400
4401   /* Skip the last named argument.  */
4402   next_cum = *cum;
4403   function_arg_advance (&next_cum, mode, type, 1);
4404
4405   if (DEFAULT_ABI == ABI_V4)
4406     {
4407       /* Indicate to allocate space on the stack for varargs save area.  */
4408       cfun->machine->sysv_varargs_p = 1;
4409       if (! no_rtl)
4410         save_area = plus_constant (virtual_stack_vars_rtx,
4411                                    - RS6000_VARARGS_SIZE);
4412
4413       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
4414     }
4415   else
4416     {
4417       first_reg_offset = next_cum.words;
4418       save_area = virtual_incoming_args_rtx;
4419       cfun->machine->sysv_varargs_p = 0;
4420
4421       if (MUST_PASS_IN_STACK (mode, type))
4422         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
4423     }
4424
4425   set = get_varargs_alias_set ();
4426   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
4427     {
4428       mem = gen_rtx_MEM (BLKmode,
4429                          plus_constant (save_area,
4430                                         first_reg_offset * reg_size)),
4431       set_mem_alias_set (mem, set);
4432       set_mem_align (mem, BITS_PER_WORD);
4433
4434       move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
4435                            GP_ARG_NUM_REG - first_reg_offset);
4436     }
4437
4438   /* Save FP registers if needed.  */
4439   if (DEFAULT_ABI == ABI_V4
4440       && TARGET_HARD_FLOAT && TARGET_FPRS
4441       && ! no_rtl
4442       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
4443     {
4444       int fregno = next_cum.fregno;
4445       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
4446       rtx lab = gen_label_rtx ();
4447       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
4448
4449       emit_jump_insn (gen_rtx_SET (VOIDmode,
4450                                    pc_rtx,
4451                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
4452                                             gen_rtx_NE (VOIDmode, cr1,
4453                                                         const0_rtx),
4454                                             gen_rtx_LABEL_REF (VOIDmode, lab),
4455                                             pc_rtx)));
4456
4457       while (fregno <= FP_ARG_V4_MAX_REG)
4458         {
4459           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
4460           set_mem_alias_set (mem, set);
4461           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
4462           fregno++;
4463           off += 8;
4464         }
4465
4466       emit_label (lab);
4467     }
4468 }
4469
4470 /* Create the va_list data type.  */
4471
4472 static tree
4473 rs6000_build_builtin_va_list (void)
4474 {
4475   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
4476
4477   /* For AIX, prefer 'char *' because that's what the system
4478      header files like.  */
4479   if (DEFAULT_ABI != ABI_V4)
4480     return build_pointer_type (char_type_node);
4481
4482   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4483   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4484
4485   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
4486                       unsigned_char_type_node);
4487   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
4488                       unsigned_char_type_node);
4489   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
4490      every user file.  */
4491   f_res = build_decl (FIELD_DECL, get_identifier ("reserved"),
4492                       short_unsigned_type_node);
4493   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4494                       ptr_type_node);
4495   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4496                       ptr_type_node);
4497
4498   DECL_FIELD_CONTEXT (f_gpr) = record;
4499   DECL_FIELD_CONTEXT (f_fpr) = record;
4500   DECL_FIELD_CONTEXT (f_res) = record;
4501   DECL_FIELD_CONTEXT (f_ovf) = record;
4502   DECL_FIELD_CONTEXT (f_sav) = record;
4503
4504   TREE_CHAIN (record) = type_decl;
4505   TYPE_NAME (record) = type_decl;
4506   TYPE_FIELDS (record) = f_gpr;
4507   TREE_CHAIN (f_gpr) = f_fpr;
4508   TREE_CHAIN (f_fpr) = f_res;
4509   TREE_CHAIN (f_res) = f_ovf;
4510   TREE_CHAIN (f_ovf) = f_sav;
4511
4512   layout_type (record);
4513
4514   /* The correct type is an array type of one element.  */
4515   return build_array_type (record, build_index_type (size_zero_node));
4516 }
4517
4518 /* Implement va_start.  */
4519
4520 void
4521 rs6000_va_start (tree valist, rtx nextarg)
4522 {
4523   HOST_WIDE_INT words, n_gpr, n_fpr;
4524   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
4525   tree gpr, fpr, ovf, sav, t;
4526
4527   /* Only SVR4 needs something special.  */
4528   if (DEFAULT_ABI != ABI_V4)
4529     {
4530       std_expand_builtin_va_start (valist, nextarg);
4531       return;
4532     }
4533
4534   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4535   f_fpr = TREE_CHAIN (f_gpr);
4536   f_res = TREE_CHAIN (f_fpr);
4537   f_ovf = TREE_CHAIN (f_res);
4538   f_sav = TREE_CHAIN (f_ovf);
4539
4540   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4541   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4542   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4543   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4544   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4545
4546   /* Count number of gp and fp argument registers used.  */
4547   words = current_function_args_info.words;
4548   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
4549   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
4550
4551   if (TARGET_DEBUG_ARG)
4552     fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
4553              HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
4554              words, n_gpr, n_fpr);
4555
4556   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
4557   TREE_SIDE_EFFECTS (t) = 1;
4558   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4559
4560   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
4561   TREE_SIDE_EFFECTS (t) = 1;
4562   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4563
4564   /* Find the overflow area.  */
4565   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
4566   if (words != 0)
4567     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
4568                build_int_2 (words * UNITS_PER_WORD, 0));
4569   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4570   TREE_SIDE_EFFECTS (t) = 1;
4571   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4572
4573   /* Find the register save area.  */
4574   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
4575   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
4576              build_int_2 (-RS6000_VARARGS_SIZE, -1));
4577   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
4578   TREE_SIDE_EFFECTS (t) = 1;
4579   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4580 }
4581
4582 /* Implement va_arg.  */
4583
4584 rtx
4585 rs6000_va_arg (tree valist, tree type)
4586 {
4587   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
4588   tree gpr, fpr, ovf, sav, reg, t, u;
4589   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
4590   rtx lab_false, lab_over, addr_rtx, r;
4591
4592   if (DEFAULT_ABI != ABI_V4)
4593     {
4594       /* Variable sized types are passed by reference.  */
4595       if (int_size_in_bytes (type) < 0)
4596         {
4597           u = build_pointer_type (type);
4598
4599           /* Args grow upward.  */
4600           t = build (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist,
4601                      build_int_2 (POINTER_SIZE / BITS_PER_UNIT, 0));
4602           TREE_SIDE_EFFECTS (t) = 1;
4603
4604           t = build1 (NOP_EXPR, build_pointer_type (u), t);
4605           TREE_SIDE_EFFECTS (t) = 1;
4606
4607           t = build1 (INDIRECT_REF, u, t);
4608           TREE_SIDE_EFFECTS (t) = 1;
4609
4610           return expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4611         }
4612       else
4613         {
4614           /* Altivec arguments must be aligned to a 128-bit boundary.  */
4615           if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
4616             {
4617               tree vtype = TREE_TYPE (valist);
4618               tree new_valist, modify;
4619               
4620               /* Round address up to multiple of 16.  Computes
4621                  (addr+15)&~0xf.  */
4622               new_valist = fold (build (BIT_AND_EXPR, vtype,
4623                                         fold (build (PLUS_EXPR, vtype, valist,
4624                                                      build_int_2 (15, 0))),
4625                                         build_int_2 (~15, -1)));
4626
4627               /* Update valist.  */
4628               modify = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4629                               new_valist);
4630               TREE_SIDE_EFFECTS (modify) = 1;
4631               expand_expr (modify, const0_rtx, VOIDmode, EXPAND_NORMAL);
4632             }
4633           
4634           return std_expand_builtin_va_arg (valist, type);
4635         }
4636     }
4637
4638   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4639   f_fpr = TREE_CHAIN (f_gpr);
4640   f_res = TREE_CHAIN (f_fpr);
4641   f_ovf = TREE_CHAIN (f_res);
4642   f_sav = TREE_CHAIN (f_ovf);
4643
4644   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4645   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4646   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4647   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4648   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4649
4650   size = int_size_in_bytes (type);
4651   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4652
4653   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
4654     {
4655       /* Aggregates and long doubles are passed by reference.  */
4656       indirect_p = 1;
4657       reg = gpr;
4658       n_reg = 1;
4659       sav_ofs = 0;
4660       sav_scale = 4;
4661       size = UNITS_PER_WORD;
4662       rsize = 1;
4663     }
4664   else if (FLOAT_TYPE_P (type) && TARGET_HARD_FLOAT && TARGET_FPRS)
4665     {
4666       /* FP args go in FP registers, if present.  */
4667       indirect_p = 0;
4668       reg = fpr;
4669       n_reg = 1;
4670       sav_ofs = 8*4;
4671       sav_scale = 8;
4672     }
4673   else
4674     {
4675       /* Otherwise into GP registers.  */
4676       indirect_p = 0;
4677       reg = gpr;
4678       n_reg = rsize;
4679       sav_ofs = 0;
4680       sav_scale = 4;
4681     }
4682
4683   /* Pull the value out of the saved registers ...  */
4684
4685   lab_false = gen_label_rtx ();
4686   lab_over = gen_label_rtx ();
4687   addr_rtx = gen_reg_rtx (Pmode);
4688
4689   /*  AltiVec vectors never go in registers.  */
4690   if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
4691     {
4692       TREE_THIS_VOLATILE (reg) = 1;
4693       emit_cmp_and_jump_insns
4694         (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
4695          GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
4696          lab_false);
4697
4698       /* Long long is aligned in the registers.  */
4699       if (n_reg > 1)
4700         {
4701           u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
4702                      build_int_2 (n_reg - 1, 0));
4703           u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
4704           u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
4705           TREE_SIDE_EFFECTS (u) = 1;
4706           expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
4707         }
4708
4709       if (sav_ofs)
4710         t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
4711       else
4712         t = sav;
4713
4714       u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
4715                  build_int_2 (n_reg, 0));
4716       TREE_SIDE_EFFECTS (u) = 1;
4717
4718       u = build1 (CONVERT_EXPR, integer_type_node, u);
4719       TREE_SIDE_EFFECTS (u) = 1;
4720
4721       u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
4722       TREE_SIDE_EFFECTS (u) = 1;
4723
4724       t = build (PLUS_EXPR, ptr_type_node, t, u);
4725       TREE_SIDE_EFFECTS (t) = 1;
4726
4727       r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4728       if (r != addr_rtx)
4729         emit_move_insn (addr_rtx, r);
4730
4731       emit_jump_insn (gen_jump (lab_over));
4732       emit_barrier ();
4733     }
4734
4735   emit_label (lab_false);
4736
4737   /* ... otherwise out of the overflow area.  */
4738
4739   /* Make sure we don't find reg 7 for the next int arg.
4740
4741      All AltiVec vectors go in the overflow area.  So in the AltiVec
4742      case we need to get the vectors from the overflow area, but
4743      remember where the GPRs and FPRs are.  */
4744   if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
4745                     || !TARGET_ALTIVEC))
4746     {
4747       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
4748       TREE_SIDE_EFFECTS (t) = 1;
4749       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4750     }
4751
4752   /* Care for on-stack alignment if needed.  */
4753   if (rsize <= 1)
4754     t = ovf;
4755   else
4756     {
4757       int align;
4758
4759       /* AltiVec vectors are 16 byte aligned.  */
4760       if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
4761         align = 15;
4762       else
4763         align = 7;
4764
4765       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
4766       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
4767     }
4768   t = save_expr (t);
4769
4770   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4771   if (r != addr_rtx)
4772     emit_move_insn (addr_rtx, r);
4773
4774   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
4775   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4776   TREE_SIDE_EFFECTS (t) = 1;
4777   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4778
4779   emit_label (lab_over);
4780
4781   if (indirect_p)
4782     {
4783       r = gen_rtx_MEM (Pmode, addr_rtx);
4784       set_mem_alias_set (r, get_varargs_alias_set ());
4785       emit_move_insn (addr_rtx, r);
4786     }
4787
4788   return addr_rtx;
4789 }
4790
4791 /* Builtins.  */
4792
4793 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
4794 do {                                                            \
4795   if ((MASK) & target_flags)                                    \
4796     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
4797                       NULL, NULL_TREE);                         \
4798 } while (0)
4799
4800 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
4801
4802 static const struct builtin_description bdesc_3arg[] =
4803 {
4804   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
4805   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
4806   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
4807   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
4808   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
4809   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
4810   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
4811   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
4812   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
4813   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
4814   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
4815   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
4816   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
4817   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
4818   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
4819   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
4820   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
4821   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
4822   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
4823   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
4824   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
4825   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
4826   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
4827 };
4828
4829 /* DST operations: void foo (void *, const int, const char).  */
4830
4831 static const struct builtin_description bdesc_dst[] =
4832 {
4833   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
4834   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
4835   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
4836   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
4837 };
4838
4839 /* Simple binary operations: VECc = foo (VECa, VECb).  */
4840
4841 static struct builtin_description bdesc_2arg[] =
4842 {
4843   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
4844   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
4845   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
4846   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
4847   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
4848   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
4849   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
4850   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
4851   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
4852   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
4853   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
4854   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
4855   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
4856   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
4857   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
4858   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
4859   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
4860   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
4861   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
4862   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
4863   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
4864   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
4865   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
4866   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
4867   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
4868   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
4869   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
4870   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
4871   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
4872   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
4873   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
4874   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
4875   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
4876   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
4877   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
4878   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
4879   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
4880   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
4881   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
4882   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
4883   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
4884   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
4885   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
4886   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
4887   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
4888   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
4889   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
4890   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
4891   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
4892   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
4893   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
4894   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
4895   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
4896   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
4897   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
4898   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
4899   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
4900   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
4901   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
4902   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
4903   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
4904   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
4905   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
4906   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
4907   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
4908   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
4909   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
4910   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
4911   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
4912   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
4913   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
4914   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
4915   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
4916   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
4917   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
4918   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
4919   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
4920   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
4921   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
4922   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
4923   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
4924   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
4925   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
4926   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
4927   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
4928   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
4929   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
4930   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
4931   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
4932   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
4933   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
4934   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
4935   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
4936   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
4937   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
4938   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
4939   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
4940   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
4941   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
4942   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
4943   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
4944   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
4945   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
4946   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
4947   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
4948   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
4949   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
4950   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
4951   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
4952   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
4953   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
4954   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
4955   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
4956
4957   /* Place holder, leave as first spe builtin.  */
4958   { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
4959   { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
4960   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
4961   { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
4962   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
4963   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
4964   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
4965   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
4966   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
4967   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
4968   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
4969   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
4970   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
4971   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
4972   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
4973   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
4974   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
4975   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
4976   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
4977   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
4978   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
4979   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
4980   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
4981   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
4982   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
4983   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
4984   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
4985   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
4986   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
4987   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
4988   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
4989   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
4990   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
4991   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
4992   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
4993   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
4994   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
4995   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
4996   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
4997   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
4998   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
4999   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
5000   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
5001   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
5002   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
5003   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
5004   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
5005   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
5006   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
5007   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
5008   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
5009   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
5010   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
5011   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
5012   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
5013   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
5014   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
5015   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
5016   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
5017   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
5018   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
5019   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
5020   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
5021   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
5022   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
5023   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
5024   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
5025   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
5026   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
5027   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
5028   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
5029   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
5030   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
5031   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
5032   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
5033   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
5034   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
5035   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
5036   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
5037   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
5038   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
5039   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
5040   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
5041   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
5042   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
5043   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
5044   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
5045   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
5046   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
5047   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
5048   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
5049   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
5050   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
5051   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
5052   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
5053   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
5054   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
5055   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
5056   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
5057   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
5058   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
5059   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
5060   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
5061   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
5062   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
5063   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
5064   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
5065   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
5066   { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
5067
5068   /* SPE binary operations expecting a 5-bit unsigned literal.  */
5069   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
5070
5071   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
5072   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
5073   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
5074   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
5075   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
5076   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
5077   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
5078   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
5079   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
5080   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
5081   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
5082   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
5083   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
5084   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
5085   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
5086   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
5087   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
5088   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
5089   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
5090   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
5091   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
5092   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
5093   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
5094   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
5095   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
5096   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
5097
5098   /* Place-holder.  Leave as last binary SPE builtin.  */
5099   { 0, CODE_FOR_xorv2si3, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
5100 };
5101
5102 /* AltiVec predicates.  */
5103
5104 struct builtin_description_predicates
5105 {
5106   const unsigned int mask;
5107   const enum insn_code icode;
5108   const char *opcode;
5109   const char *const name;
5110   const enum rs6000_builtins code;
5111 };
5112
5113 static const struct builtin_description_predicates bdesc_altivec_preds[] =
5114 {
5115   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
5116   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
5117   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
5118   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
5119   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
5120   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
5121   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
5122   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
5123   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
5124   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
5125   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
5126   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
5127   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
5128 };
5129
5130 /* SPE predicates.  */
5131 static struct builtin_description bdesc_spe_predicates[] =
5132 {
5133   /* Place-holder.  Leave as first.  */
5134   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
5135   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
5136   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
5137   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
5138   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
5139   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
5140   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
5141   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
5142   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
5143   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
5144   /* Place-holder.  Leave as last.  */
5145   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
5146 };
5147
5148 /* SPE evsel predicates.  */
5149 static struct builtin_description bdesc_spe_evsel[] =
5150 {
5151   /* Place-holder.  Leave as first.  */
5152   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
5153   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
5154   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
5155   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
5156   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
5157   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
5158   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
5159   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
5160   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
5161   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
5162   /* Place-holder.  Leave as last.  */
5163   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
5164 };
5165
5166 /* ABS* operations.  */
5167
5168 static const struct builtin_description bdesc_abs[] =
5169 {
5170   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
5171   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
5172   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
5173   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
5174   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
5175   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
5176   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
5177 };
5178
5179 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
5180    foo (VECa).  */
5181
5182 static struct builtin_description bdesc_1arg[] =
5183 {
5184   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
5185   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
5186   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
5187   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
5188   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
5189   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
5190   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
5191   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
5192   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
5193   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
5194   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
5195   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
5196   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
5197   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
5198   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
5199   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
5200   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
5201
5202   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
5203      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
5204   { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
5205   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
5206   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
5207   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
5208   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
5209   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
5210   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
5211   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
5212   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
5213   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
5214   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
5215   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
5216   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
5217   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
5218   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
5219   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
5220   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
5221   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
5222   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
5223   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
5224   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
5225   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
5226   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
5227   { 0, CODE_FOR_spe_evneg, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
5228   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
5229   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
5230   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
5231   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
5232
5233   /* Place-holder.  Leave as last unary SPE builtin.  */
5234   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
5235 };
5236
5237 static rtx
5238 rs6000_expand_unop_builtin (enum insn_code icode, tree arglist, rtx target)
5239 {
5240   rtx pat;
5241   tree arg0 = TREE_VALUE (arglist);
5242   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5243   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5244   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5245
5246   if (icode == CODE_FOR_nothing)
5247     /* Builtin not supported on this processor.  */
5248     return 0;
5249
5250   /* If we got invalid arguments bail out before generating bad rtl.  */
5251   if (arg0 == error_mark_node)
5252     return const0_rtx;
5253
5254   if (icode == CODE_FOR_altivec_vspltisb
5255       || icode == CODE_FOR_altivec_vspltish
5256       || icode == CODE_FOR_altivec_vspltisw
5257       || icode == CODE_FOR_spe_evsplatfi
5258       || icode == CODE_FOR_spe_evsplati)
5259     {
5260       /* Only allow 5-bit *signed* literals.  */
5261       if (GET_CODE (op0) != CONST_INT
5262           || INTVAL (op0) > 0x1f
5263           || INTVAL (op0) < -0x1f)
5264         {
5265           error ("argument 1 must be a 5-bit signed literal");
5266           return const0_rtx;
5267         }
5268     }
5269
5270   if (target == 0
5271       || GET_MODE (target) != tmode
5272       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5273     target = gen_reg_rtx (tmode);
5274
5275   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5276     op0 = copy_to_mode_reg (mode0, op0);
5277
5278   pat = GEN_FCN (icode) (target, op0);
5279   if (! pat)
5280     return 0;
5281   emit_insn (pat);
5282
5283   return target;
5284 }
5285
5286 static rtx
5287 altivec_expand_abs_builtin (enum insn_code icode, tree arglist, rtx target)
5288 {
5289   rtx pat, scratch1, scratch2;
5290   tree arg0 = TREE_VALUE (arglist);
5291   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5292   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5293   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5294
5295   /* If we have invalid arguments, bail out before generating bad rtl.  */
5296   if (arg0 == error_mark_node)
5297     return const0_rtx;
5298
5299   if (target == 0
5300       || GET_MODE (target) != tmode
5301       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5302     target = gen_reg_rtx (tmode);
5303
5304   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5305     op0 = copy_to_mode_reg (mode0, op0);
5306
5307   scratch1 = gen_reg_rtx (mode0);
5308   scratch2 = gen_reg_rtx (mode0);
5309
5310   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
5311   if (! pat)
5312     return 0;
5313   emit_insn (pat);
5314
5315   return target;
5316 }
5317
5318 static rtx
5319 rs6000_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
5320 {
5321   rtx pat;
5322   tree arg0 = TREE_VALUE (arglist);
5323   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5324   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5325   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5326   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5327   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5328   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5329
5330   if (icode == CODE_FOR_nothing)
5331     /* Builtin not supported on this processor.  */
5332     return 0;
5333
5334   /* If we got invalid arguments bail out before generating bad rtl.  */
5335   if (arg0 == error_mark_node || arg1 == error_mark_node)
5336     return const0_rtx;
5337
5338   if (icode == CODE_FOR_altivec_vcfux
5339       || icode == CODE_FOR_altivec_vcfsx
5340       || icode == CODE_FOR_altivec_vctsxs
5341       || icode == CODE_FOR_altivec_vctuxs
5342       || icode == CODE_FOR_altivec_vspltb
5343       || icode == CODE_FOR_altivec_vsplth
5344       || icode == CODE_FOR_altivec_vspltw
5345       || icode == CODE_FOR_spe_evaddiw
5346       || icode == CODE_FOR_spe_evldd
5347       || icode == CODE_FOR_spe_evldh
5348       || icode == CODE_FOR_spe_evldw
5349       || icode == CODE_FOR_spe_evlhhesplat
5350       || icode == CODE_FOR_spe_evlhhossplat
5351       || icode == CODE_FOR_spe_evlhhousplat
5352       || icode == CODE_FOR_spe_evlwhe
5353       || icode == CODE_FOR_spe_evlwhos
5354       || icode == CODE_FOR_spe_evlwhou
5355       || icode == CODE_FOR_spe_evlwhsplat
5356       || icode == CODE_FOR_spe_evlwwsplat
5357       || icode == CODE_FOR_spe_evrlwi
5358       || icode == CODE_FOR_spe_evslwi
5359       || icode == CODE_FOR_spe_evsrwis
5360       || icode == CODE_FOR_spe_evsubifw
5361       || icode == CODE_FOR_spe_evsrwiu)
5362     {
5363       /* Only allow 5-bit unsigned literals.  */
5364       if (TREE_CODE (arg1) != INTEGER_CST
5365           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5366         {
5367           error ("argument 2 must be a 5-bit unsigned literal");
5368           return const0_rtx;
5369         }
5370     }
5371
5372   if (target == 0
5373       || GET_MODE (target) != tmode
5374       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5375     target = gen_reg_rtx (tmode);
5376
5377   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5378     op0 = copy_to_mode_reg (mode0, op0);
5379   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5380     op1 = copy_to_mode_reg (mode1, op1);
5381
5382   pat = GEN_FCN (icode) (target, op0, op1);
5383   if (! pat)
5384     return 0;
5385   emit_insn (pat);
5386
5387   return target;
5388 }
5389
5390 static rtx
5391 altivec_expand_predicate_builtin (enum insn_code icode, const char *opcode, 
5392                                   tree arglist, rtx target)
5393 {
5394   rtx pat, scratch;
5395   tree cr6_form = TREE_VALUE (arglist);
5396   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5397   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5398   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5399   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5400   enum machine_mode tmode = SImode;
5401   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5402   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5403   int cr6_form_int;
5404
5405   if (TREE_CODE (cr6_form) != INTEGER_CST)
5406     {
5407       error ("argument 1 of __builtin_altivec_predicate must be a constant");
5408       return const0_rtx;
5409     }
5410   else
5411     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
5412
5413   if (mode0 != mode1)
5414     abort ();
5415
5416   /* If we have invalid arguments, bail out before generating bad rtl.  */
5417   if (arg0 == error_mark_node || arg1 == error_mark_node)
5418     return const0_rtx;
5419
5420   if (target == 0
5421       || GET_MODE (target) != tmode
5422       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5423     target = gen_reg_rtx (tmode);
5424
5425   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5426     op0 = copy_to_mode_reg (mode0, op0);
5427   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5428     op1 = copy_to_mode_reg (mode1, op1);
5429
5430   scratch = gen_reg_rtx (mode0);
5431
5432   pat = GEN_FCN (icode) (scratch, op0, op1,
5433                          gen_rtx (SYMBOL_REF, Pmode, opcode));
5434   if (! pat)
5435     return 0;
5436   emit_insn (pat);
5437
5438   /* The vec_any* and vec_all* predicates use the same opcodes for two
5439      different operations, but the bits in CR6 will be different
5440      depending on what information we want.  So we have to play tricks
5441      with CR6 to get the right bits out.
5442
5443      If you think this is disgusting, look at the specs for the
5444      AltiVec predicates.  */
5445
5446      switch (cr6_form_int)
5447        {
5448        case 0:
5449          emit_insn (gen_cr6_test_for_zero (target));
5450          break;
5451        case 1:
5452          emit_insn (gen_cr6_test_for_zero_reverse (target));
5453          break;
5454        case 2:
5455          emit_insn (gen_cr6_test_for_lt (target));
5456          break;
5457        case 3:
5458          emit_insn (gen_cr6_test_for_lt_reverse (target));
5459          break;
5460        default:
5461          error ("argument 1 of __builtin_altivec_predicate is out of range");
5462          break;
5463        }
5464
5465   return target;
5466 }
5467
5468 static rtx
5469 altivec_expand_lv_builtin (enum insn_code icode, tree arglist, rtx target)
5470 {
5471   rtx pat, addr;
5472   tree arg0 = TREE_VALUE (arglist);
5473   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5474   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5475   enum machine_mode mode0 = Pmode;
5476   enum machine_mode mode1 = Pmode;
5477   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5478   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5479
5480   if (icode == CODE_FOR_nothing)
5481     /* Builtin not supported on this processor.  */
5482     return 0;
5483
5484   /* If we got invalid arguments bail out before generating bad rtl.  */
5485   if (arg0 == error_mark_node || arg1 == error_mark_node)
5486     return const0_rtx;
5487
5488   if (target == 0
5489       || GET_MODE (target) != tmode
5490       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5491     target = gen_reg_rtx (tmode);
5492
5493   op1 = copy_to_mode_reg (mode1, op1); 
5494
5495   if (op0 == const0_rtx)
5496     {
5497       addr = gen_rtx_MEM (tmode, op1);
5498     }
5499   else
5500     {
5501       op0 = copy_to_mode_reg (mode0, op0);
5502       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
5503     }
5504
5505   pat = GEN_FCN (icode) (target, addr);
5506
5507   if (! pat)
5508     return 0;
5509   emit_insn (pat);
5510
5511   return target;
5512 }
5513
5514 static rtx
5515 altivec_expand_stv_builtin (enum insn_code icode, tree arglist)
5516 {
5517   tree arg0 = TREE_VALUE (arglist);
5518   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5519   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5520   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5521   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5522   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5523   rtx pat, addr;
5524   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5525   enum machine_mode mode1 = Pmode;
5526   enum machine_mode mode2 = Pmode;
5527
5528   /* Invalid arguments.  Bail before doing anything stoopid!  */
5529   if (arg0 == error_mark_node
5530       || arg1 == error_mark_node
5531       || arg2 == error_mark_node)
5532     return const0_rtx;
5533
5534   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
5535     op0 = copy_to_mode_reg (tmode, op0);
5536
5537   op2 = copy_to_mode_reg (mode2, op2); 
5538
5539   if (op1 == const0_rtx)
5540     {
5541       addr = gen_rtx_MEM (tmode, op2);
5542     }
5543   else
5544     {
5545       op1 = copy_to_mode_reg (mode1, op1);
5546       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
5547     }
5548
5549   pat = GEN_FCN (icode) (addr, op0);
5550   if (pat)
5551     emit_insn (pat);
5552   return NULL_RTX;
5553 }
5554
5555 static rtx
5556 rs6000_expand_ternop_builtin (enum insn_code icode, tree arglist, rtx target)
5557 {
5558   rtx pat;
5559   tree arg0 = TREE_VALUE (arglist);
5560   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5561   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5562   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5563   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5564   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5565   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5566   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5567   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5568   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
5569
5570   if (icode == CODE_FOR_nothing)
5571     /* Builtin not supported on this processor.  */
5572     return 0;
5573
5574   /* If we got invalid arguments bail out before generating bad rtl.  */
5575   if (arg0 == error_mark_node
5576       || arg1 == error_mark_node
5577       || arg2 == error_mark_node)
5578     return const0_rtx;
5579
5580   if (icode == CODE_FOR_altivec_vsldoi_4sf
5581       || icode == CODE_FOR_altivec_vsldoi_4si
5582       || icode == CODE_FOR_altivec_vsldoi_8hi
5583       || icode == CODE_FOR_altivec_vsldoi_16qi)
5584     {
5585       /* Only allow 4-bit unsigned literals.  */
5586       if (TREE_CODE (arg2) != INTEGER_CST
5587           || TREE_INT_CST_LOW (arg2) & ~0xf)
5588         {
5589           error ("argument 3 must be a 4-bit unsigned literal");
5590           return const0_rtx;
5591         }
5592     }
5593
5594   if (target == 0
5595       || GET_MODE (target) != tmode
5596       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5597     target = gen_reg_rtx (tmode);
5598
5599   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5600     op0 = copy_to_mode_reg (mode0, op0);
5601   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5602     op1 = copy_to_mode_reg (mode1, op1);
5603   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
5604     op2 = copy_to_mode_reg (mode2, op2);
5605
5606   pat = GEN_FCN (icode) (target, op0, op1, op2);
5607   if (! pat)
5608     return 0;
5609   emit_insn (pat);
5610
5611   return target;
5612 }
5613
5614 /* Expand the lvx builtins.  */
5615 static rtx
5616 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
5617 {
5618   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5619   tree arglist = TREE_OPERAND (exp, 1);
5620   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5621   tree arg0;
5622   enum machine_mode tmode, mode0;
5623   rtx pat, op0;
5624   enum insn_code icode;
5625
5626   switch (fcode)
5627     {
5628     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
5629       icode = CODE_FOR_altivec_lvx_16qi;
5630       break;
5631     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
5632       icode = CODE_FOR_altivec_lvx_8hi;
5633       break;
5634     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
5635       icode = CODE_FOR_altivec_lvx_4si;
5636       break;
5637     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
5638       icode = CODE_FOR_altivec_lvx_4sf;
5639       break;
5640     default:
5641       *expandedp = false;
5642       return NULL_RTX;
5643     }
5644
5645   *expandedp = true;
5646
5647   arg0 = TREE_VALUE (arglist);
5648   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5649   tmode = insn_data[icode].operand[0].mode;
5650   mode0 = insn_data[icode].operand[1].mode;
5651
5652   if (target == 0
5653       || GET_MODE (target) != tmode
5654       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5655     target = gen_reg_rtx (tmode);
5656
5657   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5658     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5659
5660   pat = GEN_FCN (icode) (target, op0);
5661   if (! pat)
5662     return 0;
5663   emit_insn (pat);
5664   return target;
5665 }
5666
5667 /* Expand the stvx builtins.  */
5668 static rtx
5669 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, 
5670                            bool *expandedp)
5671 {
5672   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5673   tree arglist = TREE_OPERAND (exp, 1);
5674   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5675   tree arg0, arg1;
5676   enum machine_mode mode0, mode1;
5677   rtx pat, op0, op1;
5678   enum insn_code icode;
5679
5680   switch (fcode)
5681     {
5682     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
5683       icode = CODE_FOR_altivec_stvx_16qi;
5684       break;
5685     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
5686       icode = CODE_FOR_altivec_stvx_8hi;
5687       break;
5688     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
5689       icode = CODE_FOR_altivec_stvx_4si;
5690       break;
5691     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
5692       icode = CODE_FOR_altivec_stvx_4sf;
5693       break;
5694     default:
5695       *expandedp = false;
5696       return NULL_RTX;
5697     }
5698
5699   arg0 = TREE_VALUE (arglist);
5700   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5701   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5702   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5703   mode0 = insn_data[icode].operand[0].mode;
5704   mode1 = insn_data[icode].operand[1].mode;
5705
5706   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5707     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5708   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5709     op1 = copy_to_mode_reg (mode1, op1);
5710
5711   pat = GEN_FCN (icode) (op0, op1);
5712   if (pat)
5713     emit_insn (pat);
5714
5715   *expandedp = true;
5716   return NULL_RTX;
5717 }
5718
5719 /* Expand the dst builtins.  */
5720 static rtx
5721 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, 
5722                             bool *expandedp)
5723 {
5724   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5725   tree arglist = TREE_OPERAND (exp, 1);
5726   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5727   tree arg0, arg1, arg2;
5728   enum machine_mode mode0, mode1, mode2;
5729   rtx pat, op0, op1, op2;
5730   struct builtin_description *d;
5731   size_t i;
5732
5733   *expandedp = false;
5734
5735   /* Handle DST variants.  */
5736   d = (struct builtin_description *) bdesc_dst;
5737   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
5738     if (d->code == fcode)
5739       {
5740         arg0 = TREE_VALUE (arglist);
5741         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5742         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5743         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5744         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5745         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5746         mode0 = insn_data[d->icode].operand[0].mode;
5747         mode1 = insn_data[d->icode].operand[1].mode;
5748         mode2 = insn_data[d->icode].operand[2].mode;
5749
5750         /* Invalid arguments, bail out before generating bad rtl.  */
5751         if (arg0 == error_mark_node
5752             || arg1 == error_mark_node
5753             || arg2 == error_mark_node)
5754           return const0_rtx;
5755
5756         if (TREE_CODE (arg2) != INTEGER_CST
5757             || TREE_INT_CST_LOW (arg2) & ~0x3)
5758           {
5759             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
5760             return const0_rtx;
5761           }
5762
5763         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
5764           op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5765         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
5766           op1 = copy_to_mode_reg (mode1, op1);
5767
5768         pat = GEN_FCN (d->icode) (op0, op1, op2);
5769         if (pat != 0)
5770           emit_insn (pat);
5771
5772         *expandedp = true;
5773         return NULL_RTX;
5774       }
5775
5776   return NULL_RTX;
5777 }
5778
5779 /* Expand the builtin in EXP and store the result in TARGET.  Store
5780    true in *EXPANDEDP if we found a builtin to expand.  */
5781 static rtx
5782 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
5783 {
5784   struct builtin_description *d;
5785   struct builtin_description_predicates *dp;
5786   size_t i;
5787   enum insn_code icode;
5788   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5789   tree arglist = TREE_OPERAND (exp, 1);
5790   tree arg0;
5791   rtx op0, pat;
5792   enum machine_mode tmode, mode0;
5793   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5794
5795   target = altivec_expand_ld_builtin (exp, target, expandedp);
5796   if (*expandedp)
5797     return target;
5798
5799   target = altivec_expand_st_builtin (exp, target, expandedp);
5800   if (*expandedp)
5801     return target;
5802
5803   target = altivec_expand_dst_builtin (exp, target, expandedp);
5804   if (*expandedp)
5805     return target;
5806
5807   *expandedp = true;
5808
5809   switch (fcode)
5810     {
5811     case ALTIVEC_BUILTIN_STVX:
5812       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
5813     case ALTIVEC_BUILTIN_STVEBX:
5814       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
5815     case ALTIVEC_BUILTIN_STVEHX:
5816       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
5817     case ALTIVEC_BUILTIN_STVEWX:
5818       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
5819     case ALTIVEC_BUILTIN_STVXL:
5820       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
5821
5822     case ALTIVEC_BUILTIN_MFVSCR:
5823       icode = CODE_FOR_altivec_mfvscr;
5824       tmode = insn_data[icode].operand[0].mode;
5825
5826       if (target == 0
5827           || GET_MODE (target) != tmode
5828           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5829         target = gen_reg_rtx (tmode);
5830       
5831       pat = GEN_FCN (icode) (target);
5832       if (! pat)
5833         return 0;
5834       emit_insn (pat);
5835       return target;
5836
5837     case ALTIVEC_BUILTIN_MTVSCR:
5838       icode = CODE_FOR_altivec_mtvscr;
5839       arg0 = TREE_VALUE (arglist);
5840       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5841       mode0 = insn_data[icode].operand[0].mode;
5842
5843       /* If we got invalid arguments bail out before generating bad rtl.  */
5844       if (arg0 == error_mark_node)
5845         return const0_rtx;
5846
5847       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5848         op0 = copy_to_mode_reg (mode0, op0);
5849
5850       pat = GEN_FCN (icode) (op0);
5851       if (pat)
5852         emit_insn (pat);
5853       return NULL_RTX;
5854
5855     case ALTIVEC_BUILTIN_DSSALL:
5856       emit_insn (gen_altivec_dssall ());
5857       return NULL_RTX;
5858
5859     case ALTIVEC_BUILTIN_DSS:
5860       icode = CODE_FOR_altivec_dss;
5861       arg0 = TREE_VALUE (arglist);
5862       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5863       mode0 = insn_data[icode].operand[0].mode;
5864
5865       /* If we got invalid arguments bail out before generating bad rtl.  */
5866       if (arg0 == error_mark_node)
5867         return const0_rtx;
5868
5869       if (TREE_CODE (arg0) != INTEGER_CST
5870           || TREE_INT_CST_LOW (arg0) & ~0x3)
5871         {
5872           error ("argument to dss must be a 2-bit unsigned literal");
5873           return const0_rtx;
5874         }
5875
5876       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5877         op0 = copy_to_mode_reg (mode0, op0);
5878
5879       emit_insn (gen_altivec_dss (op0));
5880       return NULL_RTX;
5881     }
5882
5883   /* Expand abs* operations.  */
5884   d = (struct builtin_description *) bdesc_abs;
5885   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
5886     if (d->code == fcode)
5887       return altivec_expand_abs_builtin (d->icode, arglist, target);
5888
5889   /* Expand the AltiVec predicates.  */
5890   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
5891   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
5892     if (dp->code == fcode)
5893       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
5894
5895   /* LV* are funky.  We initialized them differently.  */
5896   switch (fcode)
5897     {
5898     case ALTIVEC_BUILTIN_LVSL:
5899       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
5900                                            arglist, target);
5901     case ALTIVEC_BUILTIN_LVSR:
5902       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
5903                                           arglist, target);
5904     case ALTIVEC_BUILTIN_LVEBX:
5905       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
5906                                           arglist, target);
5907     case ALTIVEC_BUILTIN_LVEHX:
5908       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
5909                                           arglist, target);
5910     case ALTIVEC_BUILTIN_LVEWX:
5911       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
5912                                           arglist, target);
5913     case ALTIVEC_BUILTIN_LVXL:
5914       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
5915                                           arglist, target);
5916     case ALTIVEC_BUILTIN_LVX:
5917       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx,
5918                                           arglist, target);
5919     default:
5920       break;
5921       /* Fall through.  */
5922     }
5923
5924   *expandedp = false;
5925   return NULL_RTX;
5926 }
5927
5928 /* Binops that need to be initialized manually, but can be expanded
5929    automagically by rs6000_expand_binop_builtin.  */
5930 static struct builtin_description bdesc_2arg_spe[] =
5931 {
5932   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
5933   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
5934   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
5935   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
5936   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
5937   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
5938   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
5939   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
5940   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
5941   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
5942   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
5943   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
5944   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
5945   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
5946   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
5947   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
5948   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
5949   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
5950   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
5951   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
5952   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
5953   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
5954 };
5955
5956 /* Expand the builtin in EXP and store the result in TARGET.  Store
5957    true in *EXPANDEDP if we found a builtin to expand.
5958
5959    This expands the SPE builtins that are not simple unary and binary
5960    operations.  */
5961 static rtx
5962 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
5963 {
5964   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5965   tree arglist = TREE_OPERAND (exp, 1);
5966   tree arg1, arg0;
5967   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5968   enum insn_code icode;
5969   enum machine_mode tmode, mode0;
5970   rtx pat, op0;
5971   struct builtin_description *d;
5972   size_t i;
5973
5974   *expandedp = true;
5975
5976   /* Syntax check for a 5-bit unsigned immediate.  */
5977   switch (fcode)
5978     {
5979     case SPE_BUILTIN_EVSTDD:
5980     case SPE_BUILTIN_EVSTDH:
5981     case SPE_BUILTIN_EVSTDW:
5982     case SPE_BUILTIN_EVSTWHE:
5983     case SPE_BUILTIN_EVSTWHO:
5984     case SPE_BUILTIN_EVSTWWE:
5985     case SPE_BUILTIN_EVSTWWO:
5986       arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5987       if (TREE_CODE (arg1) != INTEGER_CST
5988           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5989         {
5990           error ("argument 2 must be a 5-bit unsigned literal");
5991           return const0_rtx;
5992         }
5993       break;
5994     default:
5995       break;
5996     }
5997
5998   /* The evsplat*i instructions are not quite generic.  */
5999   switch (fcode)
6000     {
6001     case SPE_BUILTIN_EVSPLATFI:
6002       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
6003                                          arglist, target);
6004     case SPE_BUILTIN_EVSPLATI:
6005       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
6006                                          arglist, target);
6007     default:
6008       break;
6009     }
6010
6011   d = (struct builtin_description *) bdesc_2arg_spe;
6012   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
6013     if (d->code == fcode)
6014       return rs6000_expand_binop_builtin (d->icode, arglist, target);
6015
6016   d = (struct builtin_description *) bdesc_spe_predicates;
6017   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
6018     if (d->code == fcode)
6019       return spe_expand_predicate_builtin (d->icode, arglist, target);
6020
6021   d = (struct builtin_description *) bdesc_spe_evsel;
6022   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
6023     if (d->code == fcode)
6024       return spe_expand_evsel_builtin (d->icode, arglist, target);
6025
6026   switch (fcode)
6027     {
6028     case SPE_BUILTIN_EVSTDDX:
6029       return altivec_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
6030     case SPE_BUILTIN_EVSTDHX:
6031       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
6032     case SPE_BUILTIN_EVSTDWX:
6033       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
6034     case SPE_BUILTIN_EVSTWHEX:
6035       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
6036     case SPE_BUILTIN_EVSTWHOX:
6037       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
6038     case SPE_BUILTIN_EVSTWWEX:
6039       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
6040     case SPE_BUILTIN_EVSTWWOX:
6041       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
6042     case SPE_BUILTIN_EVSTDD:
6043       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
6044     case SPE_BUILTIN_EVSTDH:
6045       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
6046     case SPE_BUILTIN_EVSTDW:
6047       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
6048     case SPE_BUILTIN_EVSTWHE:
6049       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
6050     case SPE_BUILTIN_EVSTWHO:
6051       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
6052     case SPE_BUILTIN_EVSTWWE:
6053       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
6054     case SPE_BUILTIN_EVSTWWO:
6055       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
6056     case SPE_BUILTIN_MFSPEFSCR:
6057       icode = CODE_FOR_spe_mfspefscr;
6058       tmode = insn_data[icode].operand[0].mode;
6059
6060       if (target == 0
6061           || GET_MODE (target) != tmode
6062           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6063         target = gen_reg_rtx (tmode);
6064       
6065       pat = GEN_FCN (icode) (target);
6066       if (! pat)
6067         return 0;
6068       emit_insn (pat);
6069       return target;
6070     case SPE_BUILTIN_MTSPEFSCR:
6071       icode = CODE_FOR_spe_mtspefscr;
6072       arg0 = TREE_VALUE (arglist);
6073       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6074       mode0 = insn_data[icode].operand[0].mode;
6075
6076       if (arg0 == error_mark_node)
6077         return const0_rtx;
6078
6079       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6080         op0 = copy_to_mode_reg (mode0, op0);
6081
6082       pat = GEN_FCN (icode) (op0);
6083       if (pat)
6084         emit_insn (pat);
6085       return NULL_RTX;
6086     default:
6087       break;
6088     }
6089
6090   *expandedp = false;
6091   return NULL_RTX;
6092 }
6093
6094 static rtx
6095 spe_expand_predicate_builtin (enum insn_code icode, tree arglist, rtx target)
6096 {
6097   rtx pat, scratch, tmp;
6098   tree form = TREE_VALUE (arglist);
6099   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
6100   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6101   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6102   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6103   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6104   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6105   int form_int;
6106   enum rtx_code code;
6107
6108   if (TREE_CODE (form) != INTEGER_CST)
6109     {
6110       error ("argument 1 of __builtin_spe_predicate must be a constant");
6111       return const0_rtx;
6112     }
6113   else
6114     form_int = TREE_INT_CST_LOW (form);
6115
6116   if (mode0 != mode1)
6117     abort ();
6118
6119   if (arg0 == error_mark_node || arg1 == error_mark_node)
6120     return const0_rtx;
6121
6122   if (target == 0
6123       || GET_MODE (target) != SImode
6124       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
6125     target = gen_reg_rtx (SImode);
6126
6127   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6128     op0 = copy_to_mode_reg (mode0, op0);
6129   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
6130     op1 = copy_to_mode_reg (mode1, op1);
6131
6132   scratch = gen_reg_rtx (CCmode);
6133
6134   pat = GEN_FCN (icode) (scratch, op0, op1);
6135   if (! pat)
6136     return const0_rtx;
6137   emit_insn (pat);
6138
6139   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
6140      _lower_.  We use one compare, but look in different bits of the
6141      CR for each variant.
6142
6143      There are 2 elements in each SPE simd type (upper/lower).  The CR
6144      bits are set as follows:
6145
6146      BIT0  | BIT 1  | BIT 2   | BIT 3
6147      U     |   L    | (U | L) | (U & L)
6148
6149      So, for an "all" relationship, BIT 3 would be set.
6150      For an "any" relationship, BIT 2 would be set.  Etc.
6151
6152      Following traditional nomenclature, these bits map to:
6153
6154      BIT0  | BIT 1  | BIT 2   | BIT 3
6155      LT    | GT     | EQ      | OV
6156
6157      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
6158   */
6159
6160   switch (form_int)
6161     {
6162       /* All variant.  OV bit.  */
6163     case 0:
6164       /* We need to get to the OV bit, which is the ORDERED bit.  We
6165          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
6166          that's ugly and will trigger a validate_condition_mode abort.
6167          So let's just use another pattern.  */
6168       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
6169       return target;
6170       /* Any variant.  EQ bit.  */
6171     case 1:
6172       code = EQ;
6173       break;
6174       /* Upper variant.  LT bit.  */
6175     case 2:
6176       code = LT;
6177       break;
6178       /* Lower variant.  GT bit.  */
6179     case 3:
6180       code = GT;
6181       break;
6182     default:
6183       error ("argument 1 of __builtin_spe_predicate is out of range");
6184       return const0_rtx;
6185     }
6186
6187   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
6188   emit_move_insn (target, tmp);
6189
6190   return target;
6191 }
6192
6193 /* The evsel builtins look like this:
6194
6195      e = __builtin_spe_evsel_OP (a, b, c, d);
6196
6197    and work like this:
6198
6199      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
6200      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
6201 */
6202
6203 static rtx
6204 spe_expand_evsel_builtin (enum insn_code icode, tree arglist, rtx target)
6205 {
6206   rtx pat, scratch;
6207   tree arg0 = TREE_VALUE (arglist);
6208   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6209   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6210   tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
6211   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6212   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6213   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6214   rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
6215   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6216   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6217
6218   if (mode0 != mode1)
6219     abort ();
6220
6221   if (arg0 == error_mark_node || arg1 == error_mark_node
6222       || arg2 == error_mark_node || arg3 == error_mark_node)
6223     return const0_rtx;
6224
6225   if (target == 0
6226       || GET_MODE (target) != mode0
6227       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
6228     target = gen_reg_rtx (mode0);
6229
6230   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6231     op0 = copy_to_mode_reg (mode0, op0);
6232   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
6233     op1 = copy_to_mode_reg (mode0, op1);
6234   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
6235     op2 = copy_to_mode_reg (mode0, op2);
6236   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
6237     op3 = copy_to_mode_reg (mode0, op3);
6238
6239   /* Generate the compare.  */
6240   scratch = gen_reg_rtx (CCmode);
6241   pat = GEN_FCN (icode) (scratch, op0, op1);
6242   if (! pat)
6243     return const0_rtx;
6244   emit_insn (pat);
6245
6246   if (mode0 == V2SImode)
6247     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
6248   else
6249     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
6250
6251   return target;
6252 }
6253
6254 /* Expand an expression EXP that calls a built-in function,
6255    with result going to TARGET if that's convenient
6256    (and in mode MODE if that's convenient).
6257    SUBTARGET may be used as the target for computing one of EXP's operands.
6258    IGNORE is nonzero if the value is to be ignored.  */
6259
6260 static rtx
6261 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
6262                       enum machine_mode mode ATTRIBUTE_UNUSED, 
6263                       int ignore ATTRIBUTE_UNUSED)
6264 {
6265   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6266   tree arglist = TREE_OPERAND (exp, 1);
6267   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6268   struct builtin_description *d;
6269   size_t i;
6270   rtx ret;
6271   bool success;
6272   
6273   if (TARGET_ALTIVEC)
6274     {
6275       ret = altivec_expand_builtin (exp, target, &success);
6276
6277       if (success)
6278         return ret;
6279     }
6280   if (TARGET_SPE)
6281     {
6282       ret = spe_expand_builtin (exp, target, &success);
6283
6284       if (success)
6285         return ret;
6286     }
6287
6288   if (TARGET_ALTIVEC || TARGET_SPE)
6289     {
6290       /* Handle simple unary operations.  */
6291       d = (struct builtin_description *) bdesc_1arg;
6292       for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6293         if (d->code == fcode)
6294           return rs6000_expand_unop_builtin (d->icode, arglist, target);
6295
6296       /* Handle simple binary operations.  */
6297       d = (struct builtin_description *) bdesc_2arg;
6298       for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6299         if (d->code == fcode)
6300           return rs6000_expand_binop_builtin (d->icode, arglist, target);
6301
6302       /* Handle simple ternary operations.  */
6303       d = (struct builtin_description *) bdesc_3arg;
6304       for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
6305         if (d->code == fcode)
6306           return rs6000_expand_ternop_builtin (d->icode, arglist, target);
6307     }
6308
6309   abort ();
6310   return NULL_RTX;
6311 }
6312
6313 static void
6314 rs6000_init_builtins (void)
6315 {
6316   opaque_V2SI_type_node = copy_node (V2SI_type_node);
6317   opaque_V2SF_type_node = copy_node (V2SF_type_node);
6318   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
6319
6320   if (TARGET_SPE)
6321     spe_init_builtins ();
6322   if (TARGET_ALTIVEC)
6323     altivec_init_builtins ();
6324   if (TARGET_ALTIVEC || TARGET_SPE)
6325     rs6000_common_init_builtins ();
6326 }
6327
6328 /* Search through a set of builtins and enable the mask bits.
6329    DESC is an array of builtins.
6330    SIZE is the total number of builtins.
6331    START is the builtin enum at which to start.
6332    END is the builtin enum at which to end.  */
6333 static void
6334 enable_mask_for_builtins (struct builtin_description *desc, int size,
6335                           enum rs6000_builtins start, 
6336                           enum rs6000_builtins end)
6337 {
6338   int i;
6339
6340   for (i = 0; i < size; ++i)
6341     if (desc[i].code == start)
6342       break;
6343
6344   if (i == size)
6345     return;
6346
6347   for (; i < size; ++i)
6348     {
6349       /* Flip all the bits on.  */
6350       desc[i].mask = target_flags;
6351       if (desc[i].code == end)
6352         break;
6353     }
6354 }
6355
6356 static void
6357 spe_init_builtins (void)
6358 {
6359   tree endlink = void_list_node;
6360   tree puint_type_node = build_pointer_type (unsigned_type_node);
6361   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
6362   struct builtin_description *d;
6363   size_t i;
6364
6365   tree v2si_ftype_4_v2si
6366     = build_function_type
6367     (opaque_V2SI_type_node,
6368      tree_cons (NULL_TREE, opaque_V2SI_type_node,
6369                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6370                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6371                                       tree_cons (NULL_TREE, opaque_V2SI_type_node,
6372                                                  endlink)))));
6373
6374   tree v2sf_ftype_4_v2sf
6375     = build_function_type
6376     (opaque_V2SF_type_node,
6377      tree_cons (NULL_TREE, opaque_V2SF_type_node,
6378                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6379                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6380                                       tree_cons (NULL_TREE, opaque_V2SF_type_node,
6381                                                  endlink)))));
6382
6383   tree int_ftype_int_v2si_v2si
6384     = build_function_type
6385     (integer_type_node,
6386      tree_cons (NULL_TREE, integer_type_node,
6387                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6388                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6389                                       endlink))));
6390
6391   tree int_ftype_int_v2sf_v2sf
6392     = build_function_type
6393     (integer_type_node,
6394      tree_cons (NULL_TREE, integer_type_node,
6395                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6396                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6397                                       endlink))));
6398
6399   tree void_ftype_v2si_puint_int
6400     = build_function_type (void_type_node,
6401                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6402                                       tree_cons (NULL_TREE, puint_type_node,
6403                                                  tree_cons (NULL_TREE,
6404                                                             integer_type_node,
6405                                                             endlink))));
6406
6407   tree void_ftype_v2si_puint_char
6408     = build_function_type (void_type_node,
6409                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6410                                       tree_cons (NULL_TREE, puint_type_node,
6411                                                  tree_cons (NULL_TREE,
6412                                                             char_type_node,
6413                                                             endlink))));
6414
6415   tree void_ftype_v2si_pv2si_int
6416     = build_function_type (void_type_node,
6417                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6418                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6419                                                  tree_cons (NULL_TREE,
6420                                                             integer_type_node,
6421                                                             endlink))));
6422
6423   tree void_ftype_v2si_pv2si_char
6424     = build_function_type (void_type_node,
6425                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6426                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6427                                                  tree_cons (NULL_TREE,
6428                                                             char_type_node,
6429                                                             endlink))));
6430
6431   tree void_ftype_int
6432     = build_function_type (void_type_node,
6433                            tree_cons (NULL_TREE, integer_type_node, endlink));
6434
6435   tree int_ftype_void
6436     = build_function_type (integer_type_node, endlink);
6437
6438   tree v2si_ftype_pv2si_int
6439     = build_function_type (opaque_V2SI_type_node,
6440                            tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6441                                       tree_cons (NULL_TREE, integer_type_node,
6442                                                  endlink)));
6443
6444   tree v2si_ftype_puint_int
6445     = build_function_type (opaque_V2SI_type_node,
6446                            tree_cons (NULL_TREE, puint_type_node,
6447                                       tree_cons (NULL_TREE, integer_type_node,
6448                                                  endlink)));
6449
6450   tree v2si_ftype_pushort_int
6451     = build_function_type (opaque_V2SI_type_node,
6452                            tree_cons (NULL_TREE, pushort_type_node,
6453                                       tree_cons (NULL_TREE, integer_type_node,
6454                                                  endlink)));
6455
6456   tree v2si_ftype_signed_char
6457     = build_function_type (opaque_V2SI_type_node,
6458                            tree_cons (NULL_TREE, signed_char_type_node,
6459                                       endlink));
6460
6461   /* The initialization of the simple binary and unary builtins is
6462      done in rs6000_common_init_builtins, but we have to enable the
6463      mask bits here manually because we have run out of `target_flags'
6464      bits.  We really need to redesign this mask business.  */
6465
6466   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
6467                             ARRAY_SIZE (bdesc_2arg),
6468                             SPE_BUILTIN_EVADDW,
6469                             SPE_BUILTIN_EVXOR);
6470   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
6471                             ARRAY_SIZE (bdesc_1arg),
6472                             SPE_BUILTIN_EVABS,
6473                             SPE_BUILTIN_EVSUBFUSIAAW);
6474   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
6475                             ARRAY_SIZE (bdesc_spe_predicates),
6476                             SPE_BUILTIN_EVCMPEQ,
6477                             SPE_BUILTIN_EVFSTSTLT);
6478   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
6479                             ARRAY_SIZE (bdesc_spe_evsel),
6480                             SPE_BUILTIN_EVSEL_CMPGTS,
6481                             SPE_BUILTIN_EVSEL_FSTSTEQ);
6482
6483   (*lang_hooks.decls.pushdecl)
6484     (build_decl (TYPE_DECL, get_identifier ("__ev64_opaque__"),
6485                  opaque_V2SI_type_node));
6486
6487   /* Initialize irregular SPE builtins.  */
6488   
6489   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
6490   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
6491   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
6492   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
6493   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
6494   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
6495   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
6496   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
6497   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
6498   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
6499   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
6500   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
6501   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
6502   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
6503   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
6504   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
6505   def_builtin (target_flags, "__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
6506   def_builtin (target_flags, "__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
6507
6508   /* Loads.  */
6509   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
6510   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
6511   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
6512   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
6513   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
6514   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
6515   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
6516   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
6517   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
6518   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
6519   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
6520   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
6521   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
6522   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
6523   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
6524   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
6525   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
6526   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
6527   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
6528   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
6529   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
6530   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
6531
6532   /* Predicates.  */
6533   d = (struct builtin_description *) bdesc_spe_predicates;
6534   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
6535     {
6536       tree type;
6537
6538       switch (insn_data[d->icode].operand[1].mode)
6539         {
6540         case V2SImode:
6541           type = int_ftype_int_v2si_v2si;
6542           break;
6543         case V2SFmode:
6544           type = int_ftype_int_v2sf_v2sf;
6545           break;
6546         default:
6547           abort ();
6548         }
6549
6550       def_builtin (d->mask, d->name, type, d->code);
6551     }
6552
6553   /* Evsel predicates.  */
6554   d = (struct builtin_description *) bdesc_spe_evsel;
6555   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
6556     {
6557       tree type;
6558
6559       switch (insn_data[d->icode].operand[1].mode)
6560         {
6561         case V2SImode:
6562           type = v2si_ftype_4_v2si;
6563           break;
6564         case V2SFmode:
6565           type = v2sf_ftype_4_v2sf;
6566           break;
6567         default:
6568           abort ();
6569         }
6570
6571       def_builtin (d->mask, d->name, type, d->code);
6572     }
6573 }
6574
6575 static void
6576 altivec_init_builtins (void)
6577 {
6578   struct builtin_description *d;
6579   struct builtin_description_predicates *dp;
6580   size_t i;
6581   tree pfloat_type_node = build_pointer_type (float_type_node);
6582   tree pint_type_node = build_pointer_type (integer_type_node);
6583   tree pshort_type_node = build_pointer_type (short_integer_type_node);
6584   tree pchar_type_node = build_pointer_type (char_type_node);
6585
6586   tree pvoid_type_node = build_pointer_type (void_type_node);
6587
6588   tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
6589   tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
6590   tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
6591   tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
6592
6593   tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
6594
6595   tree int_ftype_int_v4si_v4si
6596     = build_function_type_list (integer_type_node,
6597                                 integer_type_node, V4SI_type_node,
6598                                 V4SI_type_node, NULL_TREE);
6599   tree v4sf_ftype_pcfloat
6600     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
6601   tree void_ftype_pfloat_v4sf
6602     = build_function_type_list (void_type_node,
6603                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
6604   tree v4si_ftype_pcint
6605     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
6606   tree void_ftype_pint_v4si
6607     = build_function_type_list (void_type_node,
6608                                 pint_type_node, V4SI_type_node, NULL_TREE);
6609   tree v8hi_ftype_pcshort
6610     = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
6611   tree void_ftype_pshort_v8hi
6612     = build_function_type_list (void_type_node,
6613                                 pshort_type_node, V8HI_type_node, NULL_TREE);
6614   tree v16qi_ftype_pcchar
6615     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
6616   tree void_ftype_pchar_v16qi
6617     = build_function_type_list (void_type_node,
6618                                 pchar_type_node, V16QI_type_node, NULL_TREE);
6619   tree void_ftype_v4si
6620     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
6621   tree v8hi_ftype_void
6622     = build_function_type (V8HI_type_node, void_list_node);
6623   tree void_ftype_void
6624     = build_function_type (void_type_node, void_list_node);
6625   tree void_ftype_qi
6626     = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
6627
6628   tree v16qi_ftype_long_pcvoid
6629     = build_function_type_list (V16QI_type_node,
6630                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
6631   tree v8hi_ftype_long_pcvoid
6632     = build_function_type_list (V8HI_type_node,
6633                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
6634   tree v4si_ftype_long_pcvoid
6635     = build_function_type_list (V4SI_type_node,
6636                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
6637
6638   tree void_ftype_v4si_long_pvoid
6639     = build_function_type_list (void_type_node,
6640                                 V4SI_type_node, long_integer_type_node,
6641                                 pvoid_type_node, NULL_TREE);
6642   tree void_ftype_v16qi_long_pvoid
6643     = build_function_type_list (void_type_node,
6644                                 V16QI_type_node, long_integer_type_node,
6645                                 pvoid_type_node, NULL_TREE);
6646   tree void_ftype_v8hi_long_pvoid
6647     = build_function_type_list (void_type_node,
6648                                 V8HI_type_node, long_integer_type_node,
6649                                 pvoid_type_node, NULL_TREE);
6650   tree int_ftype_int_v8hi_v8hi
6651     = build_function_type_list (integer_type_node,
6652                                 integer_type_node, V8HI_type_node,
6653                                 V8HI_type_node, NULL_TREE);
6654   tree int_ftype_int_v16qi_v16qi
6655     = build_function_type_list (integer_type_node,
6656                                 integer_type_node, V16QI_type_node,
6657                                 V16QI_type_node, NULL_TREE);
6658   tree int_ftype_int_v4sf_v4sf
6659     = build_function_type_list (integer_type_node,
6660                                 integer_type_node, V4SF_type_node,
6661                                 V4SF_type_node, NULL_TREE);
6662   tree v4si_ftype_v4si
6663     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
6664   tree v8hi_ftype_v8hi
6665     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
6666   tree v16qi_ftype_v16qi
6667     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
6668   tree v4sf_ftype_v4sf
6669     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
6670   tree void_ftype_pcvoid_int_char
6671     = build_function_type_list (void_type_node,
6672                                 pcvoid_type_node, integer_type_node,
6673                                 char_type_node, NULL_TREE);
6674   
6675   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
6676                ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
6677   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
6678                ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
6679   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
6680                ALTIVEC_BUILTIN_LD_INTERNAL_4si);
6681   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
6682                ALTIVEC_BUILTIN_ST_INTERNAL_4si);
6683   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
6684                ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
6685   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
6686                ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
6687   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
6688                ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
6689   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
6690                ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
6691   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
6692   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
6693   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
6694   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
6695   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
6696   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
6697   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
6698   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
6699   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
6700   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
6701   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
6702   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
6703   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
6704   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
6705   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
6706   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
6707
6708   /* Add the DST variants.  */
6709   d = (struct builtin_description *) bdesc_dst;
6710   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
6711     def_builtin (d->mask, d->name, void_ftype_pcvoid_int_char, d->code);
6712
6713   /* Initialize the predicates.  */
6714   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
6715   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
6716     {
6717       enum machine_mode mode1;
6718       tree type;
6719
6720       mode1 = insn_data[dp->icode].operand[1].mode;
6721
6722       switch (mode1)
6723         {
6724         case V4SImode:
6725           type = int_ftype_int_v4si_v4si;
6726           break;
6727         case V8HImode:
6728           type = int_ftype_int_v8hi_v8hi;
6729           break;
6730         case V16QImode:
6731           type = int_ftype_int_v16qi_v16qi;
6732           break;
6733         case V4SFmode:
6734           type = int_ftype_int_v4sf_v4sf;
6735           break;
6736         default:
6737           abort ();
6738         }
6739       
6740       def_builtin (dp->mask, dp->name, type, dp->code);
6741     }
6742
6743   /* Initialize the abs* operators.  */
6744   d = (struct builtin_description *) bdesc_abs;
6745   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
6746     {
6747       enum machine_mode mode0;
6748       tree type;
6749
6750       mode0 = insn_data[d->icode].operand[0].mode;
6751
6752       switch (mode0)
6753         {
6754         case V4SImode:
6755           type = v4si_ftype_v4si;
6756           break;
6757         case V8HImode:
6758           type = v8hi_ftype_v8hi;
6759           break;
6760         case V16QImode:
6761           type = v16qi_ftype_v16qi;
6762           break;
6763         case V4SFmode:
6764           type = v4sf_ftype_v4sf;
6765           break;
6766         default:
6767           abort ();
6768         }
6769       
6770       def_builtin (d->mask, d->name, type, d->code);
6771     }
6772 }
6773
6774 static void
6775 rs6000_common_init_builtins (void)
6776 {
6777   struct builtin_description *d;
6778   size_t i;
6779
6780   tree v4sf_ftype_v4sf_v4sf_v16qi
6781     = build_function_type_list (V4SF_type_node,
6782                                 V4SF_type_node, V4SF_type_node,
6783                                 V16QI_type_node, NULL_TREE);
6784   tree v4si_ftype_v4si_v4si_v16qi
6785     = build_function_type_list (V4SI_type_node,
6786                                 V4SI_type_node, V4SI_type_node,
6787                                 V16QI_type_node, NULL_TREE);
6788   tree v8hi_ftype_v8hi_v8hi_v16qi
6789     = build_function_type_list (V8HI_type_node,
6790                                 V8HI_type_node, V8HI_type_node,
6791                                 V16QI_type_node, NULL_TREE);
6792   tree v16qi_ftype_v16qi_v16qi_v16qi
6793     = build_function_type_list (V16QI_type_node,
6794                                 V16QI_type_node, V16QI_type_node,
6795                                 V16QI_type_node, NULL_TREE);
6796   tree v4si_ftype_char
6797     = build_function_type_list (V4SI_type_node, char_type_node, NULL_TREE);
6798   tree v8hi_ftype_char
6799     = build_function_type_list (V8HI_type_node, char_type_node, NULL_TREE);
6800   tree v16qi_ftype_char
6801     = build_function_type_list (V16QI_type_node, char_type_node, NULL_TREE);
6802   tree v8hi_ftype_v16qi
6803     = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
6804   tree v4sf_ftype_v4sf
6805     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
6806
6807   tree v2si_ftype_v2si_v2si
6808     = build_function_type_list (opaque_V2SI_type_node,
6809                                 opaque_V2SI_type_node,
6810                                 opaque_V2SI_type_node, NULL_TREE);
6811
6812   tree v2sf_ftype_v2sf_v2sf
6813     = build_function_type_list (opaque_V2SF_type_node,
6814                                 opaque_V2SF_type_node,
6815                                 opaque_V2SF_type_node, NULL_TREE);
6816
6817   tree v2si_ftype_int_int
6818     = build_function_type_list (opaque_V2SI_type_node,
6819                                 integer_type_node, integer_type_node,
6820                                 NULL_TREE);
6821
6822   tree v2si_ftype_v2si
6823     = build_function_type_list (opaque_V2SI_type_node,
6824                                 opaque_V2SI_type_node, NULL_TREE);
6825
6826   tree v2sf_ftype_v2sf
6827     = build_function_type_list (opaque_V2SF_type_node,
6828                                 opaque_V2SF_type_node, NULL_TREE);
6829   
6830   tree v2sf_ftype_v2si
6831     = build_function_type_list (opaque_V2SF_type_node,
6832                                 opaque_V2SI_type_node, NULL_TREE);
6833
6834   tree v2si_ftype_v2sf
6835     = build_function_type_list (opaque_V2SI_type_node,
6836                                 opaque_V2SF_type_node, NULL_TREE);
6837
6838   tree v2si_ftype_v2si_char
6839     = build_function_type_list (opaque_V2SI_type_node,
6840                                 opaque_V2SI_type_node,
6841                                 char_type_node, NULL_TREE);
6842
6843   tree v2si_ftype_int_char
6844     = build_function_type_list (opaque_V2SI_type_node,
6845                                 integer_type_node, char_type_node, NULL_TREE);
6846
6847   tree v2si_ftype_char
6848     = build_function_type_list (opaque_V2SI_type_node,
6849                                 char_type_node, NULL_TREE);
6850
6851   tree int_ftype_int_int
6852     = build_function_type_list (integer_type_node,
6853                                 integer_type_node, integer_type_node,
6854                                 NULL_TREE);
6855
6856   tree v4si_ftype_v4si_v4si
6857     = build_function_type_list (V4SI_type_node,
6858                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6859   tree v4sf_ftype_v4si_char
6860     = build_function_type_list (V4SF_type_node,
6861                                 V4SI_type_node, char_type_node, NULL_TREE);
6862   tree v4si_ftype_v4sf_char
6863     = build_function_type_list (V4SI_type_node,
6864                                 V4SF_type_node, char_type_node, NULL_TREE);
6865   tree v4si_ftype_v4si_char
6866     = build_function_type_list (V4SI_type_node,
6867                                 V4SI_type_node, char_type_node, NULL_TREE);
6868   tree v8hi_ftype_v8hi_char
6869     = build_function_type_list (V8HI_type_node,
6870                                 V8HI_type_node, char_type_node, NULL_TREE);
6871   tree v16qi_ftype_v16qi_char
6872     = build_function_type_list (V16QI_type_node,
6873                                 V16QI_type_node, char_type_node, NULL_TREE);
6874   tree v16qi_ftype_v16qi_v16qi_char
6875     = build_function_type_list (V16QI_type_node,
6876                                 V16QI_type_node, V16QI_type_node,
6877                                 char_type_node, NULL_TREE);
6878   tree v8hi_ftype_v8hi_v8hi_char
6879     = build_function_type_list (V8HI_type_node,
6880                                 V8HI_type_node, V8HI_type_node,
6881                                 char_type_node, NULL_TREE);
6882   tree v4si_ftype_v4si_v4si_char
6883     = build_function_type_list (V4SI_type_node,
6884                                 V4SI_type_node, V4SI_type_node,
6885                                 char_type_node, NULL_TREE);
6886   tree v4sf_ftype_v4sf_v4sf_char
6887     = build_function_type_list (V4SF_type_node,
6888                                 V4SF_type_node, V4SF_type_node,
6889                                 char_type_node, NULL_TREE);
6890   tree v4sf_ftype_v4sf_v4sf
6891     = build_function_type_list (V4SF_type_node,
6892                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6893   tree v4sf_ftype_v4sf_v4sf_v4si
6894     = build_function_type_list (V4SF_type_node,
6895                                 V4SF_type_node, V4SF_type_node,
6896                                 V4SI_type_node, NULL_TREE);
6897   tree v4sf_ftype_v4sf_v4sf_v4sf
6898     = build_function_type_list (V4SF_type_node,
6899                                 V4SF_type_node, V4SF_type_node,
6900                                 V4SF_type_node, NULL_TREE);
6901   tree v4si_ftype_v4si_v4si_v4si 
6902     = build_function_type_list (V4SI_type_node,
6903                                 V4SI_type_node, V4SI_type_node,
6904                                 V4SI_type_node, NULL_TREE);
6905   tree v8hi_ftype_v8hi_v8hi
6906     = build_function_type_list (V8HI_type_node,
6907                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6908   tree v8hi_ftype_v8hi_v8hi_v8hi
6909     = build_function_type_list (V8HI_type_node,
6910                                 V8HI_type_node, V8HI_type_node,
6911                                 V8HI_type_node, NULL_TREE);
6912  tree v4si_ftype_v8hi_v8hi_v4si
6913     = build_function_type_list (V4SI_type_node,
6914                                 V8HI_type_node, V8HI_type_node,
6915                                 V4SI_type_node, NULL_TREE);
6916  tree v4si_ftype_v16qi_v16qi_v4si
6917     = build_function_type_list (V4SI_type_node,
6918                                 V16QI_type_node, V16QI_type_node,
6919                                 V4SI_type_node, NULL_TREE);
6920   tree v16qi_ftype_v16qi_v16qi
6921     = build_function_type_list (V16QI_type_node,
6922                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6923   tree v4si_ftype_v4sf_v4sf
6924     = build_function_type_list (V4SI_type_node,
6925                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6926   tree v8hi_ftype_v16qi_v16qi
6927     = build_function_type_list (V8HI_type_node,
6928                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6929   tree v4si_ftype_v8hi_v8hi
6930     = build_function_type_list (V4SI_type_node,
6931                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6932   tree v8hi_ftype_v4si_v4si
6933     = build_function_type_list (V8HI_type_node,
6934                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6935   tree v16qi_ftype_v8hi_v8hi
6936     = build_function_type_list (V16QI_type_node,
6937                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6938   tree v4si_ftype_v16qi_v4si
6939     = build_function_type_list (V4SI_type_node,
6940                                 V16QI_type_node, V4SI_type_node, NULL_TREE);
6941   tree v4si_ftype_v16qi_v16qi
6942     = build_function_type_list (V4SI_type_node,
6943                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6944   tree v4si_ftype_v8hi_v4si
6945     = build_function_type_list (V4SI_type_node,
6946                                 V8HI_type_node, V4SI_type_node, NULL_TREE);
6947   tree v4si_ftype_v8hi
6948     = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
6949   tree int_ftype_v4si_v4si
6950     = build_function_type_list (integer_type_node,
6951                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6952   tree int_ftype_v4sf_v4sf
6953     = build_function_type_list (integer_type_node,
6954                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6955   tree int_ftype_v16qi_v16qi
6956     = build_function_type_list (integer_type_node,
6957                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6958   tree int_ftype_v8hi_v8hi
6959     = build_function_type_list (integer_type_node,
6960                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6961
6962   /* Add the simple ternary operators.  */
6963   d = (struct builtin_description *) bdesc_3arg;
6964   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
6965     {
6966       
6967       enum machine_mode mode0, mode1, mode2, mode3;
6968       tree type;
6969
6970       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6971         continue;
6972       
6973       mode0 = insn_data[d->icode].operand[0].mode;
6974       mode1 = insn_data[d->icode].operand[1].mode;
6975       mode2 = insn_data[d->icode].operand[2].mode;
6976       mode3 = insn_data[d->icode].operand[3].mode;
6977       
6978       /* When all four are of the same mode.  */
6979       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
6980         {
6981           switch (mode0)
6982             {
6983             case V4SImode:
6984               type = v4si_ftype_v4si_v4si_v4si;
6985               break;
6986             case V4SFmode:
6987               type = v4sf_ftype_v4sf_v4sf_v4sf;
6988               break;
6989             case V8HImode:
6990               type = v8hi_ftype_v8hi_v8hi_v8hi;
6991               break;          
6992             case V16QImode:
6993               type = v16qi_ftype_v16qi_v16qi_v16qi;
6994               break;          
6995             default:
6996               abort();        
6997             }
6998         }
6999       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
7000         {
7001           switch (mode0)
7002             {
7003             case V4SImode:
7004               type = v4si_ftype_v4si_v4si_v16qi;
7005               break;
7006             case V4SFmode:
7007               type = v4sf_ftype_v4sf_v4sf_v16qi;
7008               break;
7009             case V8HImode:
7010               type = v8hi_ftype_v8hi_v8hi_v16qi;
7011               break;          
7012             case V16QImode:
7013               type = v16qi_ftype_v16qi_v16qi_v16qi;
7014               break;          
7015             default:
7016               abort();        
7017             }
7018         }
7019       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
7020                && mode3 == V4SImode)
7021         type = v4si_ftype_v16qi_v16qi_v4si;
7022       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
7023                && mode3 == V4SImode)
7024         type = v4si_ftype_v8hi_v8hi_v4si;
7025       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
7026                && mode3 == V4SImode)
7027         type = v4sf_ftype_v4sf_v4sf_v4si;
7028
7029       /* vchar, vchar, vchar, 4 bit literal.  */
7030       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
7031                && mode3 == QImode)
7032         type = v16qi_ftype_v16qi_v16qi_char;
7033
7034       /* vshort, vshort, vshort, 4 bit literal.  */
7035       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
7036                && mode3 == QImode)
7037         type = v8hi_ftype_v8hi_v8hi_char;
7038
7039       /* vint, vint, vint, 4 bit literal.  */
7040       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
7041                && mode3 == QImode)
7042         type = v4si_ftype_v4si_v4si_char;
7043
7044       /* vfloat, vfloat, vfloat, 4 bit literal.  */
7045       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
7046                && mode3 == QImode)
7047         type = v4sf_ftype_v4sf_v4sf_char;
7048
7049       else
7050         abort ();
7051
7052       def_builtin (d->mask, d->name, type, d->code);
7053     }
7054
7055   /* Add the simple binary operators.  */
7056   d = (struct builtin_description *) bdesc_2arg;
7057   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
7058     {
7059       enum machine_mode mode0, mode1, mode2;
7060       tree type;
7061
7062       if (d->name == 0 || d->icode == CODE_FOR_nothing)
7063         continue;
7064       
7065       mode0 = insn_data[d->icode].operand[0].mode;
7066       mode1 = insn_data[d->icode].operand[1].mode;
7067       mode2 = insn_data[d->icode].operand[2].mode;
7068
7069       /* When all three operands are of the same mode.  */
7070       if (mode0 == mode1 && mode1 == mode2)
7071         {
7072           switch (mode0)
7073             {
7074             case V4SFmode:
7075               type = v4sf_ftype_v4sf_v4sf;
7076               break;
7077             case V4SImode:
7078               type = v4si_ftype_v4si_v4si;
7079               break;
7080             case V16QImode:
7081               type = v16qi_ftype_v16qi_v16qi;
7082               break;
7083             case V8HImode:
7084               type = v8hi_ftype_v8hi_v8hi;
7085               break;
7086             case V2SImode:
7087               type = v2si_ftype_v2si_v2si;
7088               break;
7089             case V2SFmode:
7090               type = v2sf_ftype_v2sf_v2sf;
7091               break;
7092             case SImode:
7093               type = int_ftype_int_int;
7094               break;
7095             default:
7096               abort ();
7097             }
7098         }
7099
7100       /* A few other combos we really don't want to do manually.  */
7101
7102       /* vint, vfloat, vfloat.  */
7103       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
7104         type = v4si_ftype_v4sf_v4sf;
7105
7106       /* vshort, vchar, vchar.  */
7107       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
7108         type = v8hi_ftype_v16qi_v16qi;
7109
7110       /* vint, vshort, vshort.  */
7111       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
7112         type = v4si_ftype_v8hi_v8hi;
7113
7114       /* vshort, vint, vint.  */
7115       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
7116         type = v8hi_ftype_v4si_v4si;
7117
7118       /* vchar, vshort, vshort.  */
7119       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
7120         type = v16qi_ftype_v8hi_v8hi;
7121
7122       /* vint, vchar, vint.  */
7123       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
7124         type = v4si_ftype_v16qi_v4si;
7125
7126       /* vint, vchar, vchar.  */
7127       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
7128         type = v4si_ftype_v16qi_v16qi;
7129
7130       /* vint, vshort, vint.  */
7131       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
7132         type = v4si_ftype_v8hi_v4si;
7133       
7134       /* vint, vint, 5 bit literal.  */
7135       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
7136         type = v4si_ftype_v4si_char;
7137       
7138       /* vshort, vshort, 5 bit literal.  */
7139       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
7140         type = v8hi_ftype_v8hi_char;
7141       
7142       /* vchar, vchar, 5 bit literal.  */
7143       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
7144         type = v16qi_ftype_v16qi_char;
7145
7146       /* vfloat, vint, 5 bit literal.  */
7147       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
7148         type = v4sf_ftype_v4si_char;
7149       
7150       /* vint, vfloat, 5 bit literal.  */
7151       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
7152         type = v4si_ftype_v4sf_char;
7153
7154       else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
7155         type = v2si_ftype_int_int;
7156
7157       else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
7158         type = v2si_ftype_v2si_char;
7159
7160       else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
7161         type = v2si_ftype_int_char;
7162
7163       /* int, x, x.  */
7164       else if (mode0 == SImode)
7165         {
7166           switch (mode1)
7167             {
7168             case V4SImode:
7169               type = int_ftype_v4si_v4si;
7170               break;
7171             case V4SFmode:
7172               type = int_ftype_v4sf_v4sf;
7173               break;
7174             case V16QImode:
7175               type = int_ftype_v16qi_v16qi;
7176               break;
7177             case V8HImode:
7178               type = int_ftype_v8hi_v8hi;
7179               break;
7180             default:
7181               abort ();
7182             }
7183         }
7184
7185       else
7186         abort ();
7187
7188       def_builtin (d->mask, d->name, type, d->code);
7189     }
7190
7191   /* Add the simple unary operators.  */
7192   d = (struct builtin_description *) bdesc_1arg;
7193   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
7194     {
7195       enum machine_mode mode0, mode1;
7196       tree type;
7197
7198       if (d->name == 0 || d->icode == CODE_FOR_nothing)
7199         continue;
7200       
7201       mode0 = insn_data[d->icode].operand[0].mode;
7202       mode1 = insn_data[d->icode].operand[1].mode;
7203
7204       if (mode0 == V4SImode && mode1 == QImode)
7205         type = v4si_ftype_char;
7206       else if (mode0 == V8HImode && mode1 == QImode)
7207         type = v8hi_ftype_char;
7208       else if (mode0 == V16QImode && mode1 == QImode)
7209         type = v16qi_ftype_char;
7210       else if (mode0 == V4SFmode && mode1 == V4SFmode)
7211         type = v4sf_ftype_v4sf;
7212       else if (mode0 == V8HImode && mode1 == V16QImode)
7213         type = v8hi_ftype_v16qi;
7214       else if (mode0 == V4SImode && mode1 == V8HImode)
7215         type = v4si_ftype_v8hi;
7216       else if (mode0 == V2SImode && mode1 == V2SImode)
7217         type = v2si_ftype_v2si;
7218       else if (mode0 == V2SFmode && mode1 == V2SFmode)
7219         type = v2sf_ftype_v2sf;
7220       else if (mode0 == V2SFmode && mode1 == V2SImode)
7221         type = v2sf_ftype_v2si;
7222       else if (mode0 == V2SImode && mode1 == V2SFmode)
7223         type = v2si_ftype_v2sf;
7224       else if (mode0 == V2SImode && mode1 == QImode)
7225         type = v2si_ftype_char;
7226       else
7227         abort ();
7228
7229       def_builtin (d->mask, d->name, type, d->code);
7230     }
7231 }
7232
7233 static void
7234 rs6000_init_libfuncs (void)
7235 {
7236   if (!TARGET_HARD_FLOAT)
7237     return;
7238
7239   if (DEFAULT_ABI != ABI_V4)
7240     {
7241       if (TARGET_XCOFF && ! TARGET_POWER2 && ! TARGET_POWERPC)
7242         {
7243           /* AIX library routines for float->int conversion.  */
7244           set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
7245           set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
7246         }
7247
7248       /* Standard AIX/Darwin/64-bit SVR4 quad floating point routines.  */
7249       set_optab_libfunc (add_optab, TFmode, "_xlqadd");
7250       set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
7251       set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
7252       set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
7253     }
7254   else
7255     {
7256       /* 32-bit SVR4 quad floating point routines.  */
7257
7258       set_optab_libfunc (add_optab, TFmode, "_q_add");
7259       set_optab_libfunc (sub_optab, TFmode, "_q_sub");
7260       set_optab_libfunc (neg_optab, TFmode, "_q_neg");
7261       set_optab_libfunc (smul_optab, TFmode, "_q_mul");
7262       set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
7263       if (TARGET_PPC_GPOPT || TARGET_POWER2)
7264         set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
7265
7266       set_optab_libfunc (eq_optab, TFmode, "_q_feq");
7267       set_optab_libfunc (ne_optab, TFmode, "_q_fne");
7268       set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
7269       set_optab_libfunc (ge_optab, TFmode, "_q_fge");
7270       set_optab_libfunc (lt_optab, TFmode, "_q_flt");
7271       set_optab_libfunc (le_optab, TFmode, "_q_fle");
7272
7273       set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
7274       set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
7275       set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
7276       set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
7277       set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
7278       set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
7279       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
7280     }
7281 }
7282 \f
7283 /* Expand a block move operation, and return 1 if successful.  Return 0
7284    if we should let the compiler generate normal code.
7285
7286    operands[0] is the destination
7287    operands[1] is the source
7288    operands[2] is the length
7289    operands[3] is the alignment */
7290
7291 #define MAX_MOVE_REG 4
7292
7293 int
7294 expand_block_move (rtx operands[])
7295 {
7296   rtx orig_dest = operands[0];
7297   rtx orig_src  = operands[1];
7298   rtx bytes_rtx = operands[2];
7299   rtx align_rtx = operands[3];
7300   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
7301   int align;
7302   int bytes;
7303   int offset;
7304   int move_bytes;
7305   rtx stores[MAX_MOVE_REG];
7306   int num_reg = 0;
7307
7308   /* If this is not a fixed size move, just call memcpy */
7309   if (! constp)
7310     return 0;
7311
7312   /* If this is not a fixed size alignment, abort */
7313   if (GET_CODE (align_rtx) != CONST_INT)
7314     abort ();
7315   align = INTVAL (align_rtx);
7316
7317   /* Anything to move? */
7318   bytes = INTVAL (bytes_rtx);
7319   if (bytes <= 0)
7320     return 1;
7321
7322   /* store_one_arg depends on expand_block_move to handle at least the size of
7323      reg_parm_stack_space.  */  
7324   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
7325     return 0;
7326
7327   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
7328     {
7329       union {
7330         rtx (*movstrsi) (rtx, rtx, rtx, rtx);
7331         rtx (*mov) (rtx, rtx);
7332       } gen_func;
7333       enum machine_mode mode = BLKmode;
7334       rtx src, dest;
7335       
7336       if (TARGET_STRING
7337           && bytes > 24         /* move up to 32 bytes at a time */
7338           && ! fixed_regs[5]
7339           && ! fixed_regs[6]
7340           && ! fixed_regs[7]
7341           && ! fixed_regs[8]
7342           && ! fixed_regs[9]
7343           && ! fixed_regs[10]
7344           && ! fixed_regs[11]
7345           && ! fixed_regs[12])
7346         {
7347           move_bytes = (bytes > 32) ? 32 : bytes;
7348           gen_func.movstrsi = gen_movstrsi_8reg;
7349         }
7350       else if (TARGET_STRING
7351                && bytes > 16    /* move up to 24 bytes at a time */
7352                && ! fixed_regs[5]
7353                && ! fixed_regs[6]
7354                && ! fixed_regs[7]
7355                && ! fixed_regs[8]
7356                && ! fixed_regs[9]
7357                && ! fixed_regs[10])
7358         {
7359           move_bytes = (bytes > 24) ? 24 : bytes;
7360           gen_func.movstrsi = gen_movstrsi_6reg;
7361         }
7362       else if (TARGET_STRING
7363                && bytes > 8     /* move up to 16 bytes at a time */
7364                && ! fixed_regs[5]
7365                && ! fixed_regs[6]
7366                && ! fixed_regs[7]
7367                && ! fixed_regs[8])
7368         {
7369           move_bytes = (bytes > 16) ? 16 : bytes;
7370           gen_func.movstrsi = gen_movstrsi_4reg;
7371         }
7372       else if (bytes >= 8 && TARGET_POWERPC64
7373                /* 64-bit loads and stores require word-aligned
7374                   displacements.  */
7375                && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
7376         {
7377           move_bytes = 8;
7378           mode = DImode;
7379           gen_func.mov = gen_movdi;
7380         }
7381       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
7382         {                       /* move up to 8 bytes at a time */
7383           move_bytes = (bytes > 8) ? 8 : bytes;
7384           gen_func.movstrsi = gen_movstrsi_2reg;
7385         }
7386       else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
7387         {                       /* move 4 bytes */
7388           move_bytes = 4;
7389           mode = SImode;
7390           gen_func.mov = gen_movsi;
7391         }
7392       else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
7393         {                       /* move 2 bytes */
7394           move_bytes = 2;
7395           mode = HImode;
7396           gen_func.mov = gen_movhi;
7397         }
7398       else if (TARGET_STRING && bytes > 1)
7399         {                       /* move up to 4 bytes at a time */
7400           move_bytes = (bytes > 4) ? 4 : bytes;
7401           gen_func.movstrsi = gen_movstrsi_1reg;
7402         }
7403       else /* move 1 byte at a time */
7404         {
7405           move_bytes = 1;
7406           mode = QImode;
7407           gen_func.mov = gen_movqi;
7408         }
7409       
7410       src = adjust_address (orig_src, mode, offset);
7411       dest = adjust_address (orig_dest, mode, offset);
7412       
7413       if (mode != BLKmode) 
7414         {
7415           rtx tmp_reg = gen_reg_rtx (mode);
7416           
7417           emit_insn ((*gen_func.mov) (tmp_reg, src));
7418           stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
7419         }
7420
7421       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
7422         {
7423           int i;
7424           for (i = 0; i < num_reg; i++)
7425             emit_insn (stores[i]);
7426           num_reg = 0;
7427         }
7428
7429       if (mode == BLKmode)
7430         {
7431           /* Move the address into scratch registers.  The movstrsi
7432              patterns require zero offset.  */
7433           if (!REG_P (XEXP (src, 0)))
7434             {
7435               rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
7436               src = replace_equiv_address (src, src_reg);
7437             }
7438           set_mem_size (src, GEN_INT (move_bytes));
7439           
7440           if (!REG_P (XEXP (dest, 0)))
7441             {
7442               rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
7443               dest = replace_equiv_address (dest, dest_reg);
7444             }
7445           set_mem_size (dest, GEN_INT (move_bytes));
7446           
7447           emit_insn ((*gen_func.movstrsi) (dest, src,
7448                                            GEN_INT (move_bytes & 31),
7449                                            align_rtx));
7450         }
7451     }
7452
7453   return 1;
7454 }
7455
7456 \f
7457 /* Return 1 if OP is a load multiple operation.  It is known to be a
7458    PARALLEL and the first section will be tested.  */
7459
7460 int
7461 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7462 {
7463   int count = XVECLEN (op, 0);
7464   unsigned int dest_regno;
7465   rtx src_addr;
7466   int i;
7467
7468   /* Perform a quick check so we don't blow up below.  */
7469   if (count <= 1
7470       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7471       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7472       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7473     return 0;
7474
7475   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7476   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7477
7478   for (i = 1; i < count; i++)
7479     {
7480       rtx elt = XVECEXP (op, 0, i);
7481
7482       if (GET_CODE (elt) != SET
7483           || GET_CODE (SET_DEST (elt)) != REG
7484           || GET_MODE (SET_DEST (elt)) != SImode
7485           || REGNO (SET_DEST (elt)) != dest_regno + i
7486           || GET_CODE (SET_SRC (elt)) != MEM
7487           || GET_MODE (SET_SRC (elt)) != SImode
7488           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
7489           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
7490           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
7491           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
7492         return 0;
7493     }
7494
7495   return 1;
7496 }
7497
7498 /* Similar, but tests for store multiple.  Here, the second vector element
7499    is a CLOBBER.  It will be tested later.  */
7500
7501 int
7502 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7503 {
7504   int count = XVECLEN (op, 0) - 1;
7505   unsigned int src_regno;
7506   rtx dest_addr;
7507   int i;
7508
7509   /* Perform a quick check so we don't blow up below.  */
7510   if (count <= 1
7511       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7512       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7513       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7514     return 0;
7515
7516   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7517   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7518
7519   for (i = 1; i < count; i++)
7520     {
7521       rtx elt = XVECEXP (op, 0, i + 1);
7522
7523       if (GET_CODE (elt) != SET
7524           || GET_CODE (SET_SRC (elt)) != REG
7525           || GET_MODE (SET_SRC (elt)) != SImode
7526           || REGNO (SET_SRC (elt)) != src_regno + i
7527           || GET_CODE (SET_DEST (elt)) != MEM
7528           || GET_MODE (SET_DEST (elt)) != SImode
7529           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
7530           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
7531           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
7532           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
7533         return 0;
7534     }
7535
7536   return 1;
7537 }
7538
7539 /* Return a string to perform a load_multiple operation.
7540    operands[0] is the vector.
7541    operands[1] is the source address.
7542    operands[2] is the first destination register.  */
7543
7544 const char *
7545 rs6000_output_load_multiple (rtx operands[3])
7546 {
7547   /* We have to handle the case where the pseudo used to contain the address
7548      is assigned to one of the output registers.  */
7549   int i, j;
7550   int words = XVECLEN (operands[0], 0);
7551   rtx xop[10];
7552
7553   if (XVECLEN (operands[0], 0) == 1)
7554     return "{l|lwz} %2,0(%1)";
7555
7556   for (i = 0; i < words; i++)
7557     if (refers_to_regno_p (REGNO (operands[2]) + i,
7558                            REGNO (operands[2]) + i + 1, operands[1], 0))
7559       {
7560         if (i == words-1)
7561           {
7562             xop[0] = GEN_INT (4 * (words-1));
7563             xop[1] = operands[1];
7564             xop[2] = operands[2];
7565             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
7566             return "";
7567           }
7568         else if (i == 0)
7569           {
7570             xop[0] = GEN_INT (4 * (words-1));
7571             xop[1] = operands[1];
7572             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7573             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);
7574             return "";
7575           }
7576         else
7577           {
7578             for (j = 0; j < words; j++)
7579               if (j != i)
7580                 {
7581                   xop[0] = GEN_INT (j * 4);
7582                   xop[1] = operands[1];
7583                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
7584                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
7585                 }
7586             xop[0] = GEN_INT (i * 4);
7587             xop[1] = operands[1];
7588             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
7589             return "";
7590           }
7591       }
7592
7593   return "{lsi|lswi} %2,%1,%N0";
7594 }
7595
7596 /* Return 1 for a parallel vrsave operation.  */
7597
7598 int
7599 vrsave_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7600 {
7601   int count = XVECLEN (op, 0);
7602   unsigned int dest_regno, src_regno;
7603   int i;
7604
7605   if (count <= 1
7606       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7607       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7608       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
7609     return 0;
7610
7611   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7612   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7613
7614   if (dest_regno != VRSAVE_REGNO
7615       && src_regno != VRSAVE_REGNO)
7616     return 0;
7617
7618   for (i = 1; i < count; i++)
7619     {
7620       rtx elt = XVECEXP (op, 0, i);
7621
7622       if (GET_CODE (elt) != CLOBBER
7623           && GET_CODE (elt) != SET)
7624         return 0;
7625     }
7626
7627   return 1;
7628 }
7629
7630 /* Return 1 for an PARALLEL suitable for mfcr.  */
7631
7632 int
7633 mfcr_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7634 {
7635   int count = XVECLEN (op, 0);
7636   int i;
7637
7638   /* Perform a quick check so we don't blow up below.  */
7639   if (count < 1
7640       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7641       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
7642       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
7643     return 0;
7644
7645   for (i = 0; i < count; i++)
7646     {
7647       rtx exp = XVECEXP (op, 0, i);
7648       rtx unspec;
7649       int maskval;
7650       rtx src_reg;
7651
7652       src_reg = XVECEXP (SET_SRC (exp), 0, 0);
7653
7654       if (GET_CODE (src_reg) != REG
7655           || GET_MODE (src_reg) != CCmode
7656           || ! CR_REGNO_P (REGNO (src_reg)))
7657         return 0;
7658
7659       if (GET_CODE (exp) != SET
7660           || GET_CODE (SET_DEST (exp)) != REG
7661           || GET_MODE (SET_DEST (exp)) != SImode
7662           || ! INT_REGNO_P (REGNO (SET_DEST (exp))))
7663         return 0;
7664       unspec = SET_SRC (exp);
7665       maskval = 1 << (MAX_CR_REGNO - REGNO (src_reg));
7666
7667       if (GET_CODE (unspec) != UNSPEC
7668           || XINT (unspec, 1) != UNSPEC_MOVESI_FROM_CR
7669           || XVECLEN (unspec, 0) != 2
7670           || XVECEXP (unspec, 0, 0) != src_reg
7671           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
7672           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
7673         return 0;
7674     }
7675   return 1;
7676 }
7677
7678 /* Return 1 for an PARALLEL suitable for mtcrf.  */
7679
7680 int
7681 mtcrf_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7682 {
7683   int count = XVECLEN (op, 0);
7684   int i;
7685   rtx src_reg;
7686
7687   /* Perform a quick check so we don't blow up below.  */
7688   if (count < 1
7689       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7690       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
7691       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
7692     return 0;
7693   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
7694   
7695   if (GET_CODE (src_reg) != REG
7696       || GET_MODE (src_reg) != SImode
7697       || ! INT_REGNO_P (REGNO (src_reg)))
7698     return 0;
7699
7700   for (i = 0; i < count; i++)
7701     {
7702       rtx exp = XVECEXP (op, 0, i);
7703       rtx unspec;
7704       int maskval;
7705       
7706       if (GET_CODE (exp) != SET
7707           || GET_CODE (SET_DEST (exp)) != REG
7708           || GET_MODE (SET_DEST (exp)) != CCmode
7709           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
7710         return 0;
7711       unspec = SET_SRC (exp);
7712       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
7713       
7714       if (GET_CODE (unspec) != UNSPEC
7715           || XINT (unspec, 1) != UNSPEC_MOVESI_TO_CR
7716           || XVECLEN (unspec, 0) != 2
7717           || XVECEXP (unspec, 0, 0) != src_reg
7718           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
7719           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
7720         return 0;
7721     }
7722   return 1;
7723 }
7724
7725 /* Return 1 for an PARALLEL suitable for lmw.  */
7726
7727 int
7728 lmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7729 {
7730   int count = XVECLEN (op, 0);
7731   unsigned int dest_regno;
7732   rtx src_addr;
7733   unsigned int base_regno;
7734   HOST_WIDE_INT offset;
7735   int i;
7736
7737   /* Perform a quick check so we don't blow up below.  */
7738   if (count <= 1
7739       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7740       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7741       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7742     return 0;
7743
7744   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7745   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7746
7747   if (dest_regno > 31
7748       || count != 32 - (int) dest_regno)
7749     return 0;
7750
7751   if (legitimate_indirect_address_p (src_addr, 0))
7752     {
7753       offset = 0;
7754       base_regno = REGNO (src_addr);
7755       if (base_regno == 0)
7756         return 0;
7757     }
7758   else if (legitimate_offset_address_p (SImode, src_addr, 0))
7759     {
7760       offset = INTVAL (XEXP (src_addr, 1));
7761       base_regno = REGNO (XEXP (src_addr, 0));
7762     }
7763   else
7764     return 0;
7765
7766   for (i = 0; i < count; i++)
7767     {
7768       rtx elt = XVECEXP (op, 0, i);
7769       rtx newaddr;
7770       rtx addr_reg;
7771       HOST_WIDE_INT newoffset;
7772
7773       if (GET_CODE (elt) != SET
7774           || GET_CODE (SET_DEST (elt)) != REG
7775           || GET_MODE (SET_DEST (elt)) != SImode
7776           || REGNO (SET_DEST (elt)) != dest_regno + i
7777           || GET_CODE (SET_SRC (elt)) != MEM
7778           || GET_MODE (SET_SRC (elt)) != SImode)
7779         return 0;
7780       newaddr = XEXP (SET_SRC (elt), 0);
7781       if (legitimate_indirect_address_p (newaddr, 0))
7782         {
7783           newoffset = 0;
7784           addr_reg = newaddr;
7785         }
7786       else if (legitimate_offset_address_p (SImode, newaddr, 0))
7787         {
7788           addr_reg = XEXP (newaddr, 0);
7789           newoffset = INTVAL (XEXP (newaddr, 1));
7790         }
7791       else
7792         return 0;
7793       if (REGNO (addr_reg) != base_regno
7794           || newoffset != offset + 4 * i)
7795         return 0;
7796     }
7797
7798   return 1;
7799 }
7800
7801 /* Return 1 for an PARALLEL suitable for stmw.  */
7802
7803 int
7804 stmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7805 {
7806   int count = XVECLEN (op, 0);
7807   unsigned int src_regno;
7808   rtx dest_addr;
7809   unsigned int base_regno;
7810   HOST_WIDE_INT offset;
7811   int i;
7812
7813   /* Perform a quick check so we don't blow up below.  */
7814   if (count <= 1
7815       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7816       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7817       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7818     return 0;
7819
7820   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7821   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7822
7823   if (src_regno > 31
7824       || count != 32 - (int) src_regno)
7825     return 0;
7826
7827   if (legitimate_indirect_address_p (dest_addr, 0))
7828     {
7829       offset = 0;
7830       base_regno = REGNO (dest_addr);
7831       if (base_regno == 0)
7832         return 0;
7833     }
7834   else if (legitimate_offset_address_p (SImode, dest_addr, 0))
7835     {
7836       offset = INTVAL (XEXP (dest_addr, 1));
7837       base_regno = REGNO (XEXP (dest_addr, 0));
7838     }
7839   else
7840     return 0;
7841
7842   for (i = 0; i < count; i++)
7843     {
7844       rtx elt = XVECEXP (op, 0, i);
7845       rtx newaddr;
7846       rtx addr_reg;
7847       HOST_WIDE_INT newoffset;
7848
7849       if (GET_CODE (elt) != SET
7850           || GET_CODE (SET_SRC (elt)) != REG
7851           || GET_MODE (SET_SRC (elt)) != SImode
7852           || REGNO (SET_SRC (elt)) != src_regno + i
7853           || GET_CODE (SET_DEST (elt)) != MEM
7854           || GET_MODE (SET_DEST (elt)) != SImode)
7855         return 0;
7856       newaddr = XEXP (SET_DEST (elt), 0);
7857       if (legitimate_indirect_address_p (newaddr, 0))
7858         {
7859           newoffset = 0;
7860           addr_reg = newaddr;
7861         }
7862       else if (legitimate_offset_address_p (SImode, newaddr, 0))
7863         {
7864           addr_reg = XEXP (newaddr, 0);
7865           newoffset = INTVAL (XEXP (newaddr, 1));
7866         }
7867       else
7868         return 0;
7869       if (REGNO (addr_reg) != base_regno
7870           || newoffset != offset + 4 * i)
7871         return 0;
7872     }
7873
7874   return 1;
7875 }
7876 \f
7877 /* A validation routine: say whether CODE, a condition code, and MODE
7878    match.  The other alternatives either don't make sense or should
7879    never be generated.  */
7880
7881 static void
7882 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
7883 {
7884   if (GET_RTX_CLASS (code) != '<' 
7885       || GET_MODE_CLASS (mode) != MODE_CC)
7886     abort ();
7887
7888   /* These don't make sense.  */
7889   if ((code == GT || code == LT || code == GE || code == LE)
7890       && mode == CCUNSmode)
7891     abort ();
7892
7893   if ((code == GTU || code == LTU || code == GEU || code == LEU)
7894       && mode != CCUNSmode)
7895     abort ();
7896
7897   if (mode != CCFPmode
7898       && (code == ORDERED || code == UNORDERED
7899           || code == UNEQ || code == LTGT
7900           || code == UNGT || code == UNLT
7901           || code == UNGE || code == UNLE))
7902     abort ();
7903   
7904   /* These should never be generated except for 
7905      flag_finite_math_only.  */
7906   if (mode == CCFPmode
7907       && ! flag_finite_math_only
7908       && (code == LE || code == GE
7909           || code == UNEQ || code == LTGT
7910           || code == UNGT || code == UNLT))
7911     abort ();
7912
7913   /* These are invalid; the information is not there.  */
7914   if (mode == CCEQmode 
7915       && code != EQ && code != NE)
7916     abort ();
7917 }
7918
7919 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
7920    We only check the opcode against the mode of the CC value here.  */
7921
7922 int
7923 branch_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7924 {
7925   enum rtx_code code = GET_CODE (op);
7926   enum machine_mode cc_mode;
7927
7928   if (GET_RTX_CLASS (code) != '<')
7929     return 0;
7930
7931   cc_mode = GET_MODE (XEXP (op, 0));
7932   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
7933     return 0;
7934
7935   validate_condition_mode (code, cc_mode);
7936
7937   return 1;
7938 }
7939
7940 /* Return 1 if OP is a comparison operation that is valid for a branch
7941    insn and which is true if the corresponding bit in the CC register
7942    is set.  */
7943
7944 int
7945 branch_positive_comparison_operator (rtx op, enum machine_mode mode)
7946 {
7947   enum rtx_code code;
7948
7949   if (! branch_comparison_operator (op, mode))
7950     return 0;
7951
7952   code = GET_CODE (op);
7953   return (code == EQ || code == LT || code == GT
7954           || (TARGET_E500 && TARGET_HARD_FLOAT && !TARGET_FPRS && code == NE)
7955           || code == LTU || code == GTU
7956           || code == UNORDERED);
7957 }
7958
7959 /* Return 1 if OP is a comparison operation that is valid for an scc
7960    insn: it must be a positive comparison.  */
7961
7962 int
7963 scc_comparison_operator (rtx op, enum machine_mode mode)
7964 {
7965   return branch_positive_comparison_operator (op, mode);
7966 }
7967
7968 int
7969 trap_comparison_operator (rtx op, enum machine_mode mode)
7970 {
7971   if (mode != VOIDmode && mode != GET_MODE (op))
7972     return 0;
7973   return GET_RTX_CLASS (GET_CODE (op)) == '<';
7974 }
7975
7976 int
7977 boolean_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7978 {
7979   enum rtx_code code = GET_CODE (op);
7980   return (code == AND || code == IOR || code == XOR);
7981 }
7982
7983 int
7984 boolean_or_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7985 {
7986   enum rtx_code code = GET_CODE (op);
7987   return (code == IOR || code == XOR);
7988 }
7989
7990 int
7991 min_max_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7992 {
7993   enum rtx_code code = GET_CODE (op);
7994   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
7995 }
7996 \f
7997 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
7998    mask required to convert the result of a rotate insn into a shift
7999    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
8000
8001 int
8002 includes_lshift_p (rtx shiftop, rtx andop)
8003 {
8004   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
8005
8006   shift_mask <<= INTVAL (shiftop);
8007
8008   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
8009 }
8010
8011 /* Similar, but for right shift.  */
8012
8013 int
8014 includes_rshift_p (rtx shiftop, rtx andop)
8015 {
8016   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
8017
8018   shift_mask >>= INTVAL (shiftop);
8019
8020   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
8021 }
8022
8023 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
8024    to perform a left shift.  It must have exactly SHIFTOP least
8025    significant 0's, then one or more 1's, then zero or more 0's.  */
8026
8027 int
8028 includes_rldic_lshift_p (rtx shiftop, rtx andop)
8029 {
8030   if (GET_CODE (andop) == CONST_INT)
8031     {
8032       HOST_WIDE_INT c, lsb, shift_mask;
8033
8034       c = INTVAL (andop);
8035       if (c == 0 || c == ~0)
8036         return 0;
8037
8038       shift_mask = ~0;
8039       shift_mask <<= INTVAL (shiftop);
8040
8041       /* Find the least significant one bit.  */
8042       lsb = c & -c;
8043
8044       /* It must coincide with the LSB of the shift mask.  */
8045       if (-lsb != shift_mask)
8046         return 0;
8047
8048       /* Invert to look for the next transition (if any).  */
8049       c = ~c;
8050
8051       /* Remove the low group of ones (originally low group of zeros).  */
8052       c &= -lsb;
8053
8054       /* Again find the lsb, and check we have all 1's above.  */
8055       lsb = c & -c;
8056       return c == -lsb;
8057     }
8058   else if (GET_CODE (andop) == CONST_DOUBLE
8059            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8060     {
8061       HOST_WIDE_INT low, high, lsb;
8062       HOST_WIDE_INT shift_mask_low, shift_mask_high;
8063
8064       low = CONST_DOUBLE_LOW (andop);
8065       if (HOST_BITS_PER_WIDE_INT < 64)
8066         high = CONST_DOUBLE_HIGH (andop);
8067
8068       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
8069           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
8070         return 0;
8071
8072       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8073         {
8074           shift_mask_high = ~0;
8075           if (INTVAL (shiftop) > 32)
8076             shift_mask_high <<= INTVAL (shiftop) - 32;
8077
8078           lsb = high & -high;
8079
8080           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
8081             return 0;
8082
8083           high = ~high;
8084           high &= -lsb;
8085
8086           lsb = high & -high;
8087           return high == -lsb;
8088         }
8089
8090       shift_mask_low = ~0;
8091       shift_mask_low <<= INTVAL (shiftop);
8092
8093       lsb = low & -low;
8094
8095       if (-lsb != shift_mask_low)
8096         return 0;
8097
8098       if (HOST_BITS_PER_WIDE_INT < 64)
8099         high = ~high;
8100       low = ~low;
8101       low &= -lsb;
8102
8103       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8104         {
8105           lsb = high & -high;
8106           return high == -lsb;
8107         }
8108
8109       lsb = low & -low;
8110       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
8111     }
8112   else
8113     return 0;
8114 }
8115
8116 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
8117    to perform a left shift.  It must have SHIFTOP or more least
8118    significant 0's, with the remainder of the word 1's.  */
8119
8120 int
8121 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
8122 {
8123   if (GET_CODE (andop) == CONST_INT)
8124     {
8125       HOST_WIDE_INT c, lsb, shift_mask;
8126
8127       shift_mask = ~0;
8128       shift_mask <<= INTVAL (shiftop);
8129       c = INTVAL (andop);
8130
8131       /* Find the least significant one bit.  */
8132       lsb = c & -c;
8133
8134       /* It must be covered by the shift mask.
8135          This test also rejects c == 0.  */
8136       if ((lsb & shift_mask) == 0)
8137         return 0;
8138
8139       /* Check we have all 1's above the transition, and reject all 1's.  */
8140       return c == -lsb && lsb != 1;
8141     }
8142   else if (GET_CODE (andop) == CONST_DOUBLE
8143            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8144     {
8145       HOST_WIDE_INT low, lsb, shift_mask_low;
8146
8147       low = CONST_DOUBLE_LOW (andop);
8148
8149       if (HOST_BITS_PER_WIDE_INT < 64)
8150         {
8151           HOST_WIDE_INT high, shift_mask_high;
8152
8153           high = CONST_DOUBLE_HIGH (andop);
8154
8155           if (low == 0)
8156             {
8157               shift_mask_high = ~0;
8158               if (INTVAL (shiftop) > 32)
8159                 shift_mask_high <<= INTVAL (shiftop) - 32;
8160
8161               lsb = high & -high;
8162
8163               if ((lsb & shift_mask_high) == 0)
8164                 return 0;
8165
8166               return high == -lsb;
8167             }
8168           if (high != ~0)
8169             return 0;
8170         }
8171
8172       shift_mask_low = ~0;
8173       shift_mask_low <<= INTVAL (shiftop);
8174
8175       lsb = low & -low;
8176
8177       if ((lsb & shift_mask_low) == 0)
8178         return 0;
8179
8180       return low == -lsb && lsb != 1;
8181     }
8182   else
8183     return 0;
8184 }
8185
8186 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
8187    for lfq and stfq insns.
8188
8189    Note reg1 and reg2 *must* be hard registers.  To be sure we will
8190    abort if we are passed pseudo registers.  */
8191
8192 int
8193 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
8194 {
8195   /* We might have been passed a SUBREG.  */
8196   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
8197     return 0;
8198
8199   return (REGNO (reg1) == REGNO (reg2) - 1);
8200 }
8201
8202 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
8203    addr1 and addr2 must be in consecutive memory locations
8204    (addr2 == addr1 + 8).  */
8205
8206 int
8207 addrs_ok_for_quad_peep (rtx addr1, rtx addr2)
8208 {
8209   unsigned int reg1;
8210   int offset1;
8211
8212   /* Extract an offset (if used) from the first addr.  */
8213   if (GET_CODE (addr1) == PLUS)
8214     {
8215       /* If not a REG, return zero.  */
8216       if (GET_CODE (XEXP (addr1, 0)) != REG)
8217         return 0;
8218       else
8219         {
8220           reg1 = REGNO (XEXP (addr1, 0));
8221           /* The offset must be constant!  */
8222           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
8223             return 0;
8224           offset1 = INTVAL (XEXP (addr1, 1));
8225         }
8226     }
8227   else if (GET_CODE (addr1) != REG)
8228     return 0;
8229   else
8230     {
8231       reg1 = REGNO (addr1);
8232       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
8233       offset1 = 0;
8234     }
8235
8236   /* Make sure the second address is a (mem (plus (reg) (const_int)))
8237       or if it is (mem (reg)) then make sure that offset1 is -8 and the same 
8238       register as addr1.  */
8239   if (offset1 == -8 && GET_CODE (addr2) == REG && reg1 == REGNO (addr2))
8240    return 1;
8241   if (GET_CODE (addr2) != PLUS)
8242     return 0;
8243
8244   if (GET_CODE (XEXP (addr2, 0)) != REG
8245       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
8246     return 0;
8247
8248   if (reg1 != REGNO (XEXP (addr2, 0)))
8249     return 0;
8250
8251   /* The offset for the second addr must be 8 more than the first addr.  */
8252   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
8253     return 0;
8254
8255   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
8256      instructions.  */
8257   return 1;
8258 }
8259 \f
8260 /* Return the register class of a scratch register needed to copy IN into
8261    or out of a register in CLASS in MODE.  If it can be done directly,
8262    NO_REGS is returned.  */
8263
8264 enum reg_class
8265 secondary_reload_class (enum reg_class class, 
8266                 enum machine_mode mode ATTRIBUTE_UNUSED, rtx in)
8267 {
8268   int regno;
8269
8270   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
8271 #if TARGET_MACHO
8272                     && MACHOPIC_INDIRECT
8273 #endif
8274                     ))
8275     {
8276       /* We cannot copy a symbolic operand directly into anything
8277          other than BASE_REGS for TARGET_ELF.  So indicate that a
8278          register from BASE_REGS is needed as an intermediate
8279          register.
8280          
8281          On Darwin, pic addresses require a load from memory, which
8282          needs a base register.  */
8283       if (class != BASE_REGS
8284           && (GET_CODE (in) == SYMBOL_REF
8285               || GET_CODE (in) == HIGH
8286               || GET_CODE (in) == LABEL_REF
8287               || GET_CODE (in) == CONST))
8288         return BASE_REGS;
8289     }
8290
8291   if (GET_CODE (in) == REG)
8292     {
8293       regno = REGNO (in);
8294       if (regno >= FIRST_PSEUDO_REGISTER)
8295         {
8296           regno = true_regnum (in);
8297           if (regno >= FIRST_PSEUDO_REGISTER)
8298             regno = -1;
8299         }
8300     }
8301   else if (GET_CODE (in) == SUBREG)
8302     {
8303       regno = true_regnum (in);
8304       if (regno >= FIRST_PSEUDO_REGISTER)
8305         regno = -1;
8306     }
8307   else
8308     regno = -1;
8309
8310   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
8311      into anything.  */
8312   if (class == GENERAL_REGS || class == BASE_REGS
8313       || (regno >= 0 && INT_REGNO_P (regno)))
8314     return NO_REGS;
8315
8316   /* Constants, memory, and FP registers can go into FP registers.  */
8317   if ((regno == -1 || FP_REGNO_P (regno))
8318       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
8319     return NO_REGS;
8320
8321   /* Memory, and AltiVec registers can go into AltiVec registers.  */
8322   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
8323       && class == ALTIVEC_REGS)
8324     return NO_REGS;
8325
8326   /* We can copy among the CR registers.  */
8327   if ((class == CR_REGS || class == CR0_REGS)
8328       && regno >= 0 && CR_REGNO_P (regno))
8329     return NO_REGS;
8330
8331   /* Otherwise, we need GENERAL_REGS.  */
8332   return GENERAL_REGS;
8333 }
8334 \f
8335 /* Given a comparison operation, return the bit number in CCR to test.  We
8336    know this is a valid comparison.  
8337
8338    SCC_P is 1 if this is for an scc.  That means that %D will have been
8339    used instead of %C, so the bits will be in different places.
8340
8341    Return -1 if OP isn't a valid comparison for some reason.  */
8342
8343 int
8344 ccr_bit (rtx op, int scc_p)
8345 {
8346   enum rtx_code code = GET_CODE (op);
8347   enum machine_mode cc_mode;
8348   int cc_regnum;
8349   int base_bit;
8350   rtx reg;
8351
8352   if (GET_RTX_CLASS (code) != '<')
8353     return -1;
8354
8355   reg = XEXP (op, 0);
8356
8357   if (GET_CODE (reg) != REG
8358       || ! CR_REGNO_P (REGNO (reg)))
8359     abort ();
8360
8361   cc_mode = GET_MODE (reg);
8362   cc_regnum = REGNO (reg);
8363   base_bit = 4 * (cc_regnum - CR0_REGNO);
8364
8365   validate_condition_mode (code, cc_mode);
8366
8367   /* When generating a sCOND operation, only positive conditions are
8368      allowed.  */
8369   if (scc_p && code != EQ && code != GT && code != LT && code != UNORDERED
8370       && code != GTU && code != LTU)
8371     abort ();
8372   
8373   switch (code)
8374     {
8375     case NE:
8376       if (TARGET_E500 && !TARGET_FPRS
8377           && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
8378         return base_bit + 1;
8379       return scc_p ? base_bit + 3 : base_bit + 2;
8380     case EQ:
8381       if (TARGET_E500 && !TARGET_FPRS
8382           && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
8383         return base_bit + 1;
8384       return base_bit + 2;
8385     case GT:  case GTU:  case UNLE:
8386       return base_bit + 1;
8387     case LT:  case LTU:  case UNGE:
8388       return base_bit;
8389     case ORDERED:  case UNORDERED:
8390       return base_bit + 3;
8391
8392     case GE:  case GEU:
8393       /* If scc, we will have done a cror to put the bit in the
8394          unordered position.  So test that bit.  For integer, this is ! LT
8395          unless this is an scc insn.  */
8396       return scc_p ? base_bit + 3 : base_bit;
8397
8398     case LE:  case LEU:
8399       return scc_p ? base_bit + 3 : base_bit + 1;
8400
8401     default:
8402       abort ();
8403     }
8404 }
8405 \f
8406 /* Return the GOT register.  */
8407
8408 struct rtx_def *
8409 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
8410 {
8411   /* The second flow pass currently (June 1999) can't update
8412      regs_ever_live without disturbing other parts of the compiler, so
8413      update it here to make the prolog/epilogue code happy.  */
8414   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
8415     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8416
8417   current_function_uses_pic_offset_table = 1;
8418
8419   return pic_offset_table_rtx;
8420 }
8421 \f
8422 /* Function to init struct machine_function.
8423    This will be called, via a pointer variable,
8424    from push_function_context.  */
8425
8426 static struct machine_function *
8427 rs6000_init_machine_status (void)
8428 {
8429   return ggc_alloc_cleared (sizeof (machine_function));
8430 }
8431 \f
8432 /* These macros test for integers and extract the low-order bits.  */
8433 #define INT_P(X)  \
8434 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
8435  && GET_MODE (X) == VOIDmode)
8436
8437 #define INT_LOWPART(X) \
8438   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
8439
8440 int
8441 extract_MB (rtx op)
8442 {
8443   int i;
8444   unsigned long val = INT_LOWPART (op);
8445
8446   /* If the high bit is zero, the value is the first 1 bit we find
8447      from the left.  */
8448   if ((val & 0x80000000) == 0)
8449     {
8450       if ((val & 0xffffffff) == 0)
8451         abort ();
8452
8453       i = 1;
8454       while (((val <<= 1) & 0x80000000) == 0)
8455         ++i;
8456       return i;
8457     }
8458
8459   /* If the high bit is set and the low bit is not, or the mask is all
8460      1's, the value is zero.  */
8461   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
8462     return 0;
8463
8464   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
8465      from the right.  */
8466   i = 31;
8467   while (((val >>= 1) & 1) != 0)
8468     --i;
8469
8470   return i;
8471 }
8472
8473 int
8474 extract_ME (rtx op)
8475 {
8476   int i;
8477   unsigned long val = INT_LOWPART (op);
8478
8479   /* If the low bit is zero, the value is the first 1 bit we find from
8480      the right.  */
8481   if ((val & 1) == 0)
8482     {
8483       if ((val & 0xffffffff) == 0)
8484         abort ();
8485
8486       i = 30;
8487       while (((val >>= 1) & 1) == 0)
8488         --i;
8489
8490       return i;
8491     }
8492
8493   /* If the low bit is set and the high bit is not, or the mask is all
8494      1's, the value is 31.  */
8495   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
8496     return 31;
8497
8498   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
8499      from the left.  */
8500   i = 0;
8501   while (((val <<= 1) & 0x80000000) != 0)
8502     ++i;
8503
8504   return i;
8505 }
8506
8507 /* Locate some local-dynamic symbol still in use by this function
8508    so that we can print its name in some tls_ld pattern.  */
8509
8510 static const char *
8511 rs6000_get_some_local_dynamic_name (void)
8512 {
8513   rtx insn;
8514
8515   if (cfun->machine->some_ld_name)
8516     return cfun->machine->some_ld_name;
8517
8518   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8519     if (INSN_P (insn)
8520         && for_each_rtx (&PATTERN (insn),
8521                          rs6000_get_some_local_dynamic_name_1, 0))
8522       return cfun->machine->some_ld_name;
8523
8524   abort ();
8525 }
8526
8527 /* Helper function for rs6000_get_some_local_dynamic_name.  */
8528
8529 static int
8530 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
8531 {
8532   rtx x = *px;
8533
8534   if (GET_CODE (x) == SYMBOL_REF)
8535     {
8536       const char *str = XSTR (x, 0);
8537       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8538         {
8539           cfun->machine->some_ld_name = str;
8540           return 1;
8541         }
8542     }
8543
8544   return 0;
8545 }
8546
8547 /* Print an operand.  Recognize special options, documented below.  */
8548
8549 #if TARGET_ELF
8550 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
8551 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
8552 #else
8553 #define SMALL_DATA_RELOC "sda21"
8554 #define SMALL_DATA_REG 0
8555 #endif
8556
8557 void
8558 print_operand (FILE *file, rtx x, int code)
8559 {
8560   int i;
8561   HOST_WIDE_INT val;
8562   unsigned HOST_WIDE_INT uval;
8563
8564   switch (code)
8565     {
8566     case '.':
8567       /* Write out an instruction after the call which may be replaced
8568          with glue code by the loader.  This depends on the AIX version.  */
8569       asm_fprintf (file, RS6000_CALL_GLUE);
8570       return;
8571
8572       /* %a is output_address.  */
8573
8574     case 'A':
8575       /* If X is a constant integer whose low-order 5 bits are zero,
8576          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
8577          in the AIX assembler where "sri" with a zero shift count
8578          writes a trash instruction.  */
8579       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
8580         putc ('l', file);
8581       else
8582         putc ('r', file);
8583       return;
8584
8585     case 'b':
8586       /* If constant, low-order 16 bits of constant, unsigned.
8587          Otherwise, write normally.  */
8588       if (INT_P (x))
8589         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
8590       else
8591         print_operand (file, x, 0);
8592       return;
8593
8594     case 'B':
8595       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
8596          for 64-bit mask direction.  */
8597       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
8598       return;
8599
8600       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
8601          output_operand.  */
8602
8603     case 'E':
8604       /* X is a CR register.  Print the number of the EQ bit of the CR */
8605       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8606         output_operand_lossage ("invalid %%E value");
8607       else
8608         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
8609       return;
8610
8611     case 'f':
8612       /* X is a CR register.  Print the shift count needed to move it
8613          to the high-order four bits.  */
8614       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8615         output_operand_lossage ("invalid %%f value");
8616       else
8617         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
8618       return;
8619
8620     case 'F':
8621       /* Similar, but print the count for the rotate in the opposite
8622          direction.  */
8623       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8624         output_operand_lossage ("invalid %%F value");
8625       else
8626         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
8627       return;
8628
8629     case 'G':
8630       /* X is a constant integer.  If it is negative, print "m",
8631          otherwise print "z".  This is to make an aze or ame insn.  */
8632       if (GET_CODE (x) != CONST_INT)
8633         output_operand_lossage ("invalid %%G value");
8634       else if (INTVAL (x) >= 0)
8635         putc ('z', file);
8636       else
8637         putc ('m', file);
8638       return;
8639
8640     case 'h':
8641       /* If constant, output low-order five bits.  Otherwise, write
8642          normally.  */
8643       if (INT_P (x))
8644         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
8645       else
8646         print_operand (file, x, 0);
8647       return;
8648
8649     case 'H':
8650       /* If constant, output low-order six bits.  Otherwise, write
8651          normally.  */
8652       if (INT_P (x))
8653         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
8654       else
8655         print_operand (file, x, 0);
8656       return;
8657
8658     case 'I':
8659       /* Print `i' if this is a constant, else nothing.  */
8660       if (INT_P (x))
8661         putc ('i', file);
8662       return;
8663
8664     case 'j':
8665       /* Write the bit number in CCR for jump.  */
8666       i = ccr_bit (x, 0);
8667       if (i == -1)
8668         output_operand_lossage ("invalid %%j code");
8669       else
8670         fprintf (file, "%d", i);
8671       return;
8672
8673     case 'J':
8674       /* Similar, but add one for shift count in rlinm for scc and pass
8675          scc flag to `ccr_bit'.  */
8676       i = ccr_bit (x, 1);
8677       if (i == -1)
8678         output_operand_lossage ("invalid %%J code");
8679       else
8680         /* If we want bit 31, write a shift count of zero, not 32.  */
8681         fprintf (file, "%d", i == 31 ? 0 : i + 1);
8682       return;
8683
8684     case 'k':
8685       /* X must be a constant.  Write the 1's complement of the
8686          constant.  */
8687       if (! INT_P (x))
8688         output_operand_lossage ("invalid %%k value");
8689       else
8690         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
8691       return;
8692
8693     case 'K':
8694       /* X must be a symbolic constant on ELF.  Write an
8695          expression suitable for an 'addi' that adds in the low 16
8696          bits of the MEM.  */
8697       if (GET_CODE (x) != CONST)
8698         {
8699           print_operand_address (file, x);
8700           fputs ("@l", file);
8701         }
8702       else
8703         {
8704           if (GET_CODE (XEXP (x, 0)) != PLUS
8705               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
8706                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
8707               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
8708             output_operand_lossage ("invalid %%K value");
8709           print_operand_address (file, XEXP (XEXP (x, 0), 0));
8710           fputs ("@l", file);
8711           /* For GNU as, there must be a non-alphanumeric character
8712              between 'l' and the number.  The '-' is added by
8713              print_operand() already.  */
8714           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
8715             fputs ("+", file);
8716           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
8717         }
8718       return;
8719
8720       /* %l is output_asm_label.  */
8721
8722     case 'L':
8723       /* Write second word of DImode or DFmode reference.  Works on register
8724          or non-indexed memory only.  */
8725       if (GET_CODE (x) == REG)
8726         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
8727       else if (GET_CODE (x) == MEM)
8728         {
8729           /* Handle possible auto-increment.  Since it is pre-increment and
8730              we have already done it, we can just use an offset of word.  */
8731           if (GET_CODE (XEXP (x, 0)) == PRE_INC
8732               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
8733             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
8734                                            UNITS_PER_WORD));
8735           else
8736             output_address (XEXP (adjust_address_nv (x, SImode,
8737                                                      UNITS_PER_WORD),
8738                                   0));
8739
8740           if (small_data_operand (x, GET_MODE (x)))
8741             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8742                      reg_names[SMALL_DATA_REG]);
8743         }
8744       return;
8745                             
8746     case 'm':
8747       /* MB value for a mask operand.  */
8748       if (! mask_operand (x, SImode))
8749         output_operand_lossage ("invalid %%m value");
8750
8751       fprintf (file, "%d", extract_MB (x));
8752       return;
8753
8754     case 'M':
8755       /* ME value for a mask operand.  */
8756       if (! mask_operand (x, SImode))
8757         output_operand_lossage ("invalid %%M value");
8758
8759       fprintf (file, "%d", extract_ME (x));
8760       return;
8761
8762       /* %n outputs the negative of its operand.  */
8763
8764     case 'N':
8765       /* Write the number of elements in the vector times 4.  */
8766       if (GET_CODE (x) != PARALLEL)
8767         output_operand_lossage ("invalid %%N value");
8768       else
8769         fprintf (file, "%d", XVECLEN (x, 0) * 4);
8770       return;
8771
8772     case 'O':
8773       /* Similar, but subtract 1 first.  */
8774       if (GET_CODE (x) != PARALLEL)
8775         output_operand_lossage ("invalid %%O value");
8776       else
8777         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
8778       return;
8779
8780     case 'p':
8781       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
8782       if (! INT_P (x)
8783           || INT_LOWPART (x) < 0
8784           || (i = exact_log2 (INT_LOWPART (x))) < 0)
8785         output_operand_lossage ("invalid %%p value");
8786       else
8787         fprintf (file, "%d", i);
8788       return;
8789
8790     case 'P':
8791       /* The operand must be an indirect memory reference.  The result
8792          is the register number.  */
8793       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
8794           || REGNO (XEXP (x, 0)) >= 32)
8795         output_operand_lossage ("invalid %%P value");
8796       else
8797         fprintf (file, "%d", REGNO (XEXP (x, 0)));
8798       return;
8799
8800     case 'q':
8801       /* This outputs the logical code corresponding to a boolean
8802          expression.  The expression may have one or both operands
8803          negated (if one, only the first one).  For condition register
8804          logical operations, it will also treat the negated
8805          CR codes as NOTs, but not handle NOTs of them.  */
8806       {
8807         const char *const *t = 0;
8808         const char *s;
8809         enum rtx_code code = GET_CODE (x);
8810         static const char * const tbl[3][3] = {
8811           { "and", "andc", "nor" },
8812           { "or", "orc", "nand" },
8813           { "xor", "eqv", "xor" } };
8814
8815         if (code == AND)
8816           t = tbl[0];
8817         else if (code == IOR)
8818           t = tbl[1];
8819         else if (code == XOR)
8820           t = tbl[2];
8821         else
8822           output_operand_lossage ("invalid %%q value");
8823
8824         if (GET_CODE (XEXP (x, 0)) != NOT)
8825           s = t[0];
8826         else
8827           {
8828             if (GET_CODE (XEXP (x, 1)) == NOT)
8829               s = t[2];
8830             else
8831               s = t[1];
8832           }
8833         
8834         fputs (s, file);
8835       }
8836       return;
8837
8838     case 'Q':
8839       if (TARGET_MFCRF)
8840         fputc (',',file);
8841         /* FALLTHRU */
8842       else
8843         return;
8844
8845     case 'R':
8846       /* X is a CR register.  Print the mask for `mtcrf'.  */
8847       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8848         output_operand_lossage ("invalid %%R value");
8849       else
8850         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
8851       return;
8852
8853     case 's':
8854       /* Low 5 bits of 32 - value */
8855       if (! INT_P (x))
8856         output_operand_lossage ("invalid %%s value");
8857       else
8858         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
8859       return;
8860
8861     case 'S':
8862       /* PowerPC64 mask position.  All 0's is excluded.
8863          CONST_INT 32-bit mask is considered sign-extended so any
8864          transition must occur within the CONST_INT, not on the boundary.  */
8865       if (! mask64_operand (x, DImode))
8866         output_operand_lossage ("invalid %%S value");
8867
8868       uval = INT_LOWPART (x);
8869
8870       if (uval & 1)     /* Clear Left */
8871         {
8872 #if HOST_BITS_PER_WIDE_INT > 64
8873           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
8874 #endif
8875           i = 64;
8876         }
8877       else              /* Clear Right */
8878         {
8879           uval = ~uval;
8880 #if HOST_BITS_PER_WIDE_INT > 64
8881           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
8882 #endif
8883           i = 63;
8884         }
8885       while (uval != 0)
8886         --i, uval >>= 1;
8887       if (i < 0)
8888         abort ();
8889       fprintf (file, "%d", i);
8890       return;
8891
8892     case 't':
8893       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
8894       if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
8895         abort ();
8896
8897       /* Bit 3 is OV bit.  */
8898       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
8899
8900       /* If we want bit 31, write a shift count of zero, not 32.  */
8901       fprintf (file, "%d", i == 31 ? 0 : i + 1);
8902       return;
8903
8904     case 'T':
8905       /* Print the symbolic name of a branch target register.  */
8906       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
8907                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
8908         output_operand_lossage ("invalid %%T value");
8909       else if (REGNO (x) == LINK_REGISTER_REGNUM)
8910         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
8911       else
8912         fputs ("ctr", file);
8913       return;
8914
8915     case 'u':
8916       /* High-order 16 bits of constant for use in unsigned operand.  */
8917       if (! INT_P (x))
8918         output_operand_lossage ("invalid %%u value");
8919       else
8920         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
8921                  (INT_LOWPART (x) >> 16) & 0xffff);
8922       return;
8923
8924     case 'v':
8925       /* High-order 16 bits of constant for use in signed operand.  */
8926       if (! INT_P (x))
8927         output_operand_lossage ("invalid %%v value");
8928       else
8929         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
8930                  (INT_LOWPART (x) >> 16) & 0xffff);
8931       return;
8932
8933     case 'U':
8934       /* Print `u' if this has an auto-increment or auto-decrement.  */
8935       if (GET_CODE (x) == MEM
8936           && (GET_CODE (XEXP (x, 0)) == PRE_INC
8937               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
8938         putc ('u', file);
8939       return;
8940
8941     case 'V':
8942       /* Print the trap code for this operand.  */
8943       switch (GET_CODE (x))
8944         {
8945         case EQ:
8946           fputs ("eq", file);   /* 4 */
8947           break;
8948         case NE:
8949           fputs ("ne", file);   /* 24 */
8950           break;
8951         case LT:
8952           fputs ("lt", file);   /* 16 */
8953           break;
8954         case LE:
8955           fputs ("le", file);   /* 20 */
8956           break;
8957         case GT:
8958           fputs ("gt", file);   /* 8 */
8959           break;
8960         case GE:
8961           fputs ("ge", file);   /* 12 */
8962           break;
8963         case LTU:
8964           fputs ("llt", file);  /* 2 */
8965           break;
8966         case LEU:
8967           fputs ("lle", file);  /* 6 */
8968           break;
8969         case GTU:
8970           fputs ("lgt", file);  /* 1 */
8971           break;
8972         case GEU:
8973           fputs ("lge", file);  /* 5 */
8974           break;
8975         default:
8976           abort ();
8977         }
8978       break;
8979
8980     case 'w':
8981       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
8982          normally.  */
8983       if (INT_P (x))
8984         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
8985                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
8986       else
8987         print_operand (file, x, 0);
8988       return;
8989
8990     case 'W':
8991       /* MB value for a PowerPC64 rldic operand.  */
8992       val = (GET_CODE (x) == CONST_INT
8993              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
8994
8995       if (val < 0)
8996         i = -1;
8997       else
8998         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
8999           if ((val <<= 1) < 0)
9000             break;
9001
9002 #if HOST_BITS_PER_WIDE_INT == 32
9003       if (GET_CODE (x) == CONST_INT && i >= 0)
9004         i += 32;  /* zero-extend high-part was all 0's */
9005       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
9006         {
9007           val = CONST_DOUBLE_LOW (x);
9008
9009           if (val == 0)
9010             abort ();
9011           else if (val < 0)
9012             --i;
9013           else
9014             for ( ; i < 64; i++)
9015               if ((val <<= 1) < 0)
9016                 break;
9017         }
9018 #endif
9019
9020       fprintf (file, "%d", i + 1);
9021       return;
9022
9023     case 'X':
9024       if (GET_CODE (x) == MEM
9025           && legitimate_indexed_address_p (XEXP (x, 0), 0))
9026         putc ('x', file);
9027       return;
9028
9029     case 'Y':
9030       /* Like 'L', for third word of TImode  */
9031       if (GET_CODE (x) == REG)
9032         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
9033       else if (GET_CODE (x) == MEM)
9034         {
9035           if (GET_CODE (XEXP (x, 0)) == PRE_INC
9036               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
9037             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
9038           else
9039             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
9040           if (small_data_operand (x, GET_MODE (x)))
9041             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9042                      reg_names[SMALL_DATA_REG]);
9043         }
9044       return;
9045                             
9046     case 'z':
9047       /* X is a SYMBOL_REF.  Write out the name preceded by a
9048          period and without any trailing data in brackets.  Used for function
9049          names.  If we are configured for System V (or the embedded ABI) on
9050          the PowerPC, do not emit the period, since those systems do not use
9051          TOCs and the like.  */
9052       if (GET_CODE (x) != SYMBOL_REF)
9053         abort ();
9054
9055       if (XSTR (x, 0)[0] != '.')
9056         {
9057           switch (DEFAULT_ABI)
9058             {
9059             default:
9060               abort ();
9061
9062             case ABI_AIX:
9063               putc ('.', file);
9064               break;
9065
9066             case ABI_V4:
9067             case ABI_DARWIN:
9068               break;
9069             }
9070         }
9071       if (TARGET_AIX)
9072         RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
9073       else
9074         assemble_name (file, XSTR (x, 0));
9075       return;
9076
9077     case 'Z':
9078       /* Like 'L', for last word of TImode.  */
9079       if (GET_CODE (x) == REG)
9080         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
9081       else if (GET_CODE (x) == MEM)
9082         {
9083           if (GET_CODE (XEXP (x, 0)) == PRE_INC
9084               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
9085             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
9086           else
9087             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
9088           if (small_data_operand (x, GET_MODE (x)))
9089             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9090                      reg_names[SMALL_DATA_REG]);
9091         }
9092       return;
9093
9094       /* Print AltiVec or SPE memory operand.  */
9095     case 'y':
9096       {
9097         rtx tmp;
9098
9099         if (GET_CODE (x) != MEM)
9100           abort ();
9101
9102         tmp = XEXP (x, 0);
9103
9104         if (TARGET_E500)
9105           {
9106             /* Handle [reg].  */
9107             if (GET_CODE (tmp) == REG)
9108               {
9109                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
9110                 break;
9111               }
9112             /* Handle [reg+UIMM].  */
9113             else if (GET_CODE (tmp) == PLUS &&
9114                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
9115               {
9116                 int x;
9117
9118                 if (GET_CODE (XEXP (tmp, 0)) != REG)
9119                   abort ();
9120
9121                 x = INTVAL (XEXP (tmp, 1));
9122                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
9123                 break;
9124               }
9125
9126             /* Fall through.  Must be [reg+reg].  */
9127           }
9128         if (GET_CODE (tmp) == REG)
9129           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
9130         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
9131           {
9132             if (REGNO (XEXP (tmp, 0)) == 0)
9133               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
9134                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
9135             else
9136               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
9137                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
9138           }
9139         else
9140           abort ();
9141         break;
9142       }
9143                             
9144     case 0:
9145       if (GET_CODE (x) == REG)
9146         fprintf (file, "%s", reg_names[REGNO (x)]);
9147       else if (GET_CODE (x) == MEM)
9148         {
9149           /* We need to handle PRE_INC and PRE_DEC here, since we need to
9150              know the width from the mode.  */
9151           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
9152             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
9153                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9154           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
9155             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
9156                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9157           else
9158             output_address (XEXP (x, 0));
9159         }
9160       else
9161         output_addr_const (file, x);
9162       return;
9163
9164     case '&':
9165       assemble_name (file, rs6000_get_some_local_dynamic_name ());
9166       return;
9167
9168     default:
9169       output_operand_lossage ("invalid %%xn code");
9170     }
9171 }
9172 \f
9173 /* Print the address of an operand.  */
9174
9175 void
9176 print_operand_address (FILE *file, rtx x)
9177 {
9178   if (GET_CODE (x) == REG)
9179     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
9180   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
9181            || GET_CODE (x) == LABEL_REF)
9182     {
9183       output_addr_const (file, x);
9184       if (small_data_operand (x, GET_MODE (x)))
9185         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9186                  reg_names[SMALL_DATA_REG]);
9187       else if (TARGET_TOC)
9188         abort ();
9189     }
9190   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
9191     {
9192       if (REGNO (XEXP (x, 0)) == 0)
9193         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
9194                  reg_names[ REGNO (XEXP (x, 0)) ]);
9195       else
9196         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
9197                  reg_names[ REGNO (XEXP (x, 1)) ]);
9198     }
9199   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
9200     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
9201              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
9202 #if TARGET_ELF
9203   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9204            && CONSTANT_P (XEXP (x, 1)))
9205     {
9206       output_addr_const (file, XEXP (x, 1));
9207       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9208     }
9209 #endif
9210 #if TARGET_MACHO
9211   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9212            && CONSTANT_P (XEXP (x, 1)))
9213     {
9214       fprintf (file, "lo16(");
9215       output_addr_const (file, XEXP (x, 1));
9216       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9217     }
9218 #endif
9219   else if (legitimate_constant_pool_address_p (x))
9220     {
9221       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
9222         {
9223           rtx contains_minus = XEXP (x, 1);
9224           rtx minus, symref;
9225           const char *name;
9226           
9227           /* Find the (minus (sym) (toc)) buried in X, and temporarily
9228              turn it into (sym) for output_addr_const.  */
9229           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
9230             contains_minus = XEXP (contains_minus, 0);
9231
9232           minus = XEXP (contains_minus, 0);
9233           symref = XEXP (minus, 0);
9234           XEXP (contains_minus, 0) = symref;
9235           if (TARGET_ELF)
9236             {
9237               char *newname;
9238
9239               name = XSTR (symref, 0);
9240               newname = alloca (strlen (name) + sizeof ("@toc"));
9241               strcpy (newname, name);
9242               strcat (newname, "@toc");
9243               XSTR (symref, 0) = newname;
9244             }
9245           output_addr_const (file, XEXP (x, 1));
9246           if (TARGET_ELF)
9247             XSTR (symref, 0) = name;
9248           XEXP (contains_minus, 0) = minus;
9249         }
9250       else
9251         output_addr_const (file, XEXP (x, 1));
9252
9253       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
9254     }
9255   else
9256     abort ();
9257 }
9258 \f
9259 /* Target hook for assembling integer objects.  The PowerPC version has
9260    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
9261    is defined.  It also needs to handle DI-mode objects on 64-bit
9262    targets.  */
9263
9264 static bool
9265 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
9266 {
9267 #ifdef RELOCATABLE_NEEDS_FIXUP
9268   /* Special handling for SI values.  */
9269   if (size == 4 && aligned_p)
9270     {
9271       extern int in_toc_section (void);
9272       static int recurse = 0;
9273       
9274       /* For -mrelocatable, we mark all addresses that need to be fixed up
9275          in the .fixup section.  */
9276       if (TARGET_RELOCATABLE
9277           && !in_toc_section ()
9278           && !in_text_section ()
9279           && !recurse
9280           && GET_CODE (x) != CONST_INT
9281           && GET_CODE (x) != CONST_DOUBLE
9282           && CONSTANT_P (x))
9283         {
9284           char buf[256];
9285
9286           recurse = 1;
9287           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
9288           fixuplabelno++;
9289           ASM_OUTPUT_LABEL (asm_out_file, buf);
9290           fprintf (asm_out_file, "\t.long\t(");
9291           output_addr_const (asm_out_file, x);
9292           fprintf (asm_out_file, ")@fixup\n");
9293           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
9294           ASM_OUTPUT_ALIGN (asm_out_file, 2);
9295           fprintf (asm_out_file, "\t.long\t");
9296           assemble_name (asm_out_file, buf);
9297           fprintf (asm_out_file, "\n\t.previous\n");
9298           recurse = 0;
9299           return true;
9300         }
9301       /* Remove initial .'s to turn a -mcall-aixdesc function
9302          address into the address of the descriptor, not the function
9303          itself.  */
9304       else if (GET_CODE (x) == SYMBOL_REF
9305                && XSTR (x, 0)[0] == '.'
9306                && DEFAULT_ABI == ABI_AIX)
9307         {
9308           const char *name = XSTR (x, 0);
9309           while (*name == '.')
9310             name++;
9311
9312           fprintf (asm_out_file, "\t.long\t%s\n", name);
9313           return true;
9314         }
9315     }
9316 #endif /* RELOCATABLE_NEEDS_FIXUP */
9317   return default_assemble_integer (x, size, aligned_p);
9318 }
9319
9320 #ifdef HAVE_GAS_HIDDEN
9321 /* Emit an assembler directive to set symbol visibility for DECL to
9322    VISIBILITY_TYPE.  */
9323
9324 static void
9325 rs6000_assemble_visibility (tree decl, int vis)
9326 {
9327   /* Functions need to have their entry point symbol visibility set as
9328      well as their descriptor symbol visibility.  */
9329   if (DEFAULT_ABI == ABI_AIX && TREE_CODE (decl) == FUNCTION_DECL)
9330     {
9331       static const char * const visibility_types[] = {
9332         NULL, "internal", "hidden", "protected"
9333       };
9334
9335       const char *name, *type;
9336
9337       name = ((* targetm.strip_name_encoding)
9338               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
9339       type = visibility_types[vis];
9340
9341       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
9342       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
9343     }
9344   else
9345     default_assemble_visibility (decl, vis);
9346 }
9347 #endif
9348 \f
9349 enum rtx_code
9350 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
9351 {
9352   /* Reversal of FP compares takes care -- an ordered compare
9353      becomes an unordered compare and vice versa.  */
9354   if (mode == CCFPmode 
9355       && (!flag_finite_math_only
9356           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
9357           || code == UNEQ || code == LTGT))
9358     return reverse_condition_maybe_unordered (code);
9359   else
9360     return reverse_condition (code);
9361 }
9362
9363 /* Generate a compare for CODE.  Return a brand-new rtx that
9364    represents the result of the compare.  */
9365
9366 static rtx
9367 rs6000_generate_compare (enum rtx_code code)
9368 {
9369   enum machine_mode comp_mode;
9370   rtx compare_result;
9371
9372   if (rs6000_compare_fp_p)
9373     comp_mode = CCFPmode;
9374   else if (code == GTU || code == LTU
9375           || code == GEU || code == LEU)
9376     comp_mode = CCUNSmode;
9377   else
9378     comp_mode = CCmode;
9379
9380   /* First, the compare.  */
9381   compare_result = gen_reg_rtx (comp_mode);
9382
9383   /* SPE FP compare instructions on the GPRs.  Yuck!  */
9384   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT)
9385       && rs6000_compare_fp_p)
9386     {
9387       rtx cmp, or1, or2, or_result, compare_result2;
9388
9389       switch (code)
9390         {
9391         case EQ:
9392         case UNEQ:
9393         case NE:
9394         case LTGT:
9395           cmp = flag_finite_math_only
9396             ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
9397                                rs6000_compare_op1)
9398             : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
9399                                rs6000_compare_op1);
9400           break;
9401         case GT:
9402         case GTU:
9403         case UNGT:
9404         case UNGE:
9405         case GE:
9406         case GEU:
9407           cmp = flag_finite_math_only
9408             ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
9409                                rs6000_compare_op1)
9410             : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
9411                                rs6000_compare_op1);
9412           break;
9413         case LT:
9414         case LTU:
9415         case UNLT:
9416         case UNLE:
9417         case LE:
9418         case LEU:
9419           cmp = flag_finite_math_only
9420             ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
9421                                rs6000_compare_op1)
9422             : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
9423                                rs6000_compare_op1);
9424           break;
9425         default:
9426           abort ();
9427         }
9428
9429       /* Synthesize LE and GE from LT/GT || EQ.  */
9430       if (code == LE || code == GE || code == LEU || code == GEU)
9431         {
9432           /* Synthesize GE/LE frome GT/LT || EQ.  */
9433
9434           emit_insn (cmp);
9435
9436           switch (code)
9437             {
9438             case LE: code = LT; break;
9439             case GE: code = GT; break;
9440             case LEU: code = LT; break;
9441             case GEU: code = GT; break;
9442             default: abort ();
9443             }
9444
9445           or1 = gen_reg_rtx (SImode);
9446           or2 = gen_reg_rtx (SImode);
9447           or_result = gen_reg_rtx (CCEQmode);
9448           compare_result2 = gen_reg_rtx (CCFPmode);
9449
9450           /* Do the EQ.  */
9451           cmp = flag_finite_math_only
9452             ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
9453                                rs6000_compare_op1)
9454             : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
9455                                rs6000_compare_op1);
9456           emit_insn (cmp);
9457
9458           /* The MC8540 FP compare instructions set the CR bits
9459              differently than other PPC compare instructions.  For
9460              that matter, there is no generic test instruction, but a
9461              testgt, testlt, and testeq.  For a true condition, bit 2
9462              is set (x1xx) in the CR.  Following the traditional CR
9463              values:
9464
9465              LT    GT    EQ    OV
9466              bit3  bit2  bit1  bit0
9467
9468              ... bit 2 would be a GT CR alias, so later on we
9469              look in the GT bits for the branch instructions.
9470              However, we must be careful to emit correct RTL in
9471              the meantime, so optimizations don't get confused.  */
9472
9473           or1 = gen_rtx (NE, SImode, compare_result, const0_rtx);
9474           or2 = gen_rtx (NE, SImode, compare_result2, const0_rtx);
9475
9476           /* OR them together.  */
9477           cmp = gen_rtx_SET (VOIDmode, or_result,
9478                              gen_rtx_COMPARE (CCEQmode,
9479                                               gen_rtx_IOR (SImode, or1, or2),
9480                                               const_true_rtx));
9481           compare_result = or_result;
9482           code = EQ;
9483         }
9484       else
9485         {
9486           /* We only care about 1 bit (x1xx), so map everything to NE to
9487              maintain rtl sanity.  We'll get to the right bit (x1xx) at
9488              code output time.  */
9489           if (code == NE || code == LTGT)
9490             /* Do the inverse here because we have no cmpne
9491                instruction.  We use the cmpeq instruction and expect
9492                to get a 0 instead.  */
9493             code = EQ;
9494           else
9495             code = NE;
9496         }
9497
9498       emit_insn (cmp);
9499     }
9500   else
9501     emit_insn (gen_rtx_SET (VOIDmode, compare_result,
9502                             gen_rtx_COMPARE (comp_mode,
9503                                              rs6000_compare_op0, 
9504                                              rs6000_compare_op1)));
9505   
9506   /* Some kinds of FP comparisons need an OR operation;
9507      under flag_finite_math_only we don't bother.  */
9508   if (rs6000_compare_fp_p
9509       && ! flag_finite_math_only
9510       && ! (TARGET_HARD_FLOAT && TARGET_E500 && !TARGET_FPRS)
9511       && (code == LE || code == GE
9512           || code == UNEQ || code == LTGT
9513           || code == UNGT || code == UNLT))
9514     {
9515       enum rtx_code or1, or2;
9516       rtx or1_rtx, or2_rtx, compare2_rtx;
9517       rtx or_result = gen_reg_rtx (CCEQmode);
9518       
9519       switch (code)
9520         {
9521         case LE: or1 = LT;  or2 = EQ;  break;
9522         case GE: or1 = GT;  or2 = EQ;  break;
9523         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
9524         case LTGT: or1 = LT;  or2 = GT;  break;
9525         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
9526         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
9527         default:  abort ();
9528         }
9529       validate_condition_mode (or1, comp_mode);
9530       validate_condition_mode (or2, comp_mode);
9531       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
9532       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
9533       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
9534                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
9535                                       const_true_rtx);
9536       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
9537
9538       compare_result = or_result;
9539       code = EQ;
9540     }
9541
9542   validate_condition_mode (code, GET_MODE (compare_result));
9543   
9544   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
9545 }
9546
9547
9548 /* Emit the RTL for an sCOND pattern.  */
9549
9550 void
9551 rs6000_emit_sCOND (enum rtx_code code, rtx result)
9552 {
9553   rtx condition_rtx;
9554   enum machine_mode op_mode;
9555   enum rtx_code cond_code;
9556
9557   condition_rtx = rs6000_generate_compare (code);
9558   cond_code = GET_CODE (condition_rtx);
9559
9560   if (cond_code == NE
9561       || cond_code == GE || cond_code == LE
9562       || cond_code == GEU || cond_code == LEU
9563       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
9564     {
9565       rtx not_result = gen_reg_rtx (CCEQmode);
9566       rtx not_op, rev_cond_rtx;
9567       enum machine_mode cc_mode;
9568       
9569       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
9570
9571       rev_cond_rtx = gen_rtx (rs6000_reverse_condition (cc_mode, cond_code),
9572                               SImode, XEXP (condition_rtx, 0), const0_rtx);
9573       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
9574       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
9575       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
9576     }
9577
9578   op_mode = GET_MODE (rs6000_compare_op0);
9579   if (op_mode == VOIDmode)
9580     op_mode = GET_MODE (rs6000_compare_op1);
9581
9582   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
9583     {
9584       PUT_MODE (condition_rtx, DImode);
9585       convert_move (result, condition_rtx, 0);
9586     }
9587   else
9588     {
9589       PUT_MODE (condition_rtx, SImode);
9590       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
9591     }
9592 }
9593
9594 /* Emit a branch of kind CODE to location LOC.  */
9595
9596 void
9597 rs6000_emit_cbranch (enum rtx_code code, rtx loc)
9598 {
9599   rtx condition_rtx, loc_ref;
9600
9601   condition_rtx = rs6000_generate_compare (code);
9602   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
9603   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
9604                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
9605                                                      loc_ref, pc_rtx)));
9606 }
9607
9608 /* Return the string to output a conditional branch to LABEL, which is
9609    the operand number of the label, or -1 if the branch is really a
9610    conditional return.  
9611
9612    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
9613    condition code register and its mode specifies what kind of
9614    comparison we made.
9615
9616    REVERSED is nonzero if we should reverse the sense of the comparison.
9617
9618    INSN is the insn.  */
9619
9620 char *
9621 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
9622 {
9623   static char string[64];
9624   enum rtx_code code = GET_CODE (op);
9625   rtx cc_reg = XEXP (op, 0);
9626   enum machine_mode mode = GET_MODE (cc_reg);
9627   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
9628   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
9629   int really_reversed = reversed ^ need_longbranch;
9630   char *s = string;
9631   const char *ccode;
9632   const char *pred;
9633   rtx note;
9634
9635   validate_condition_mode (code, mode);
9636
9637   /* Work out which way this really branches.  We could use
9638      reverse_condition_maybe_unordered here always but this
9639      makes the resulting assembler clearer.  */
9640   if (really_reversed)
9641     {
9642       /* Reversal of FP compares takes care -- an ordered compare
9643          becomes an unordered compare and vice versa.  */
9644       if (mode == CCFPmode)
9645         code = reverse_condition_maybe_unordered (code);
9646       else
9647         code = reverse_condition (code);
9648     }
9649
9650   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
9651     {
9652       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
9653          to the GT bit.  */
9654       if (code == EQ)
9655         /* Opposite of GT.  */
9656         code = UNLE;
9657       else if (code == NE)
9658         code = GT;
9659       else
9660         abort ();
9661     }
9662
9663   switch (code)
9664     {
9665       /* Not all of these are actually distinct opcodes, but
9666          we distinguish them for clarity of the resulting assembler.  */
9667     case NE: case LTGT:
9668       ccode = "ne"; break;
9669     case EQ: case UNEQ:
9670       ccode = "eq"; break;
9671     case GE: case GEU: 
9672       ccode = "ge"; break;
9673     case GT: case GTU: case UNGT: 
9674       ccode = "gt"; break;
9675     case LE: case LEU: 
9676       ccode = "le"; break;
9677     case LT: case LTU: case UNLT: 
9678       ccode = "lt"; break;
9679     case UNORDERED: ccode = "un"; break;
9680     case ORDERED: ccode = "nu"; break;
9681     case UNGE: ccode = "nl"; break;
9682     case UNLE: ccode = "ng"; break;
9683     default:
9684       abort ();
9685     }
9686   
9687   /* Maybe we have a guess as to how likely the branch is.  
9688      The old mnemonics don't have a way to specify this information.  */
9689   pred = "";
9690   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
9691   if (note != NULL_RTX)
9692     {
9693       /* PROB is the difference from 50%.  */
9694       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
9695       bool always_hint = rs6000_cpu != PROCESSOR_POWER4;
9696
9697       /* Only hint for highly probable/improbable branches on newer
9698          cpus as static prediction overrides processor dynamic
9699          prediction.  For older cpus we may as well always hint, but
9700          assume not taken for branches that are very close to 50% as a
9701          mispredicted taken branch is more expensive than a
9702          mispredicted not-taken branch.  */ 
9703       if (always_hint
9704           || abs (prob) > REG_BR_PROB_BASE / 100 * 48)
9705         {
9706           if (abs (prob) > REG_BR_PROB_BASE / 20
9707               && ((prob > 0) ^ need_longbranch))
9708               pred = "+";
9709           else
9710             pred = "-";
9711         }
9712     }
9713
9714   if (label == NULL)
9715     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
9716   else
9717     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
9718
9719   /* We need to escape any '%' characters in the reg_names string.
9720      Assume they'd only be the first character...  */
9721   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
9722     *s++ = '%';
9723   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
9724
9725   if (label != NULL)
9726     {
9727       /* If the branch distance was too far, we may have to use an
9728          unconditional branch to go the distance.  */
9729       if (need_longbranch)
9730         s += sprintf (s, ",$+8\n\tb %s", label);
9731       else
9732         s += sprintf (s, ",%s", label);
9733     }
9734
9735   return string;
9736 }
9737
9738 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
9739    operands of the last comparison is nonzero/true, FALSE_COND if it
9740    is zero/false.  Return 0 if the hardware has no such operation.  */
9741
9742 int
9743 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
9744 {
9745   enum rtx_code code = GET_CODE (op);
9746   rtx op0 = rs6000_compare_op0;
9747   rtx op1 = rs6000_compare_op1;
9748   REAL_VALUE_TYPE c1;
9749   enum machine_mode compare_mode = GET_MODE (op0);
9750   enum machine_mode result_mode = GET_MODE (dest);
9751   rtx temp;
9752
9753   /* These modes should always match. */
9754   if (GET_MODE (op1) != compare_mode
9755       /* In the isel case however, we can use a compare immediate, so
9756          op1 may be a small constant.  */
9757       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
9758     return 0;
9759   if (GET_MODE (true_cond) != result_mode)
9760     return 0;
9761   if (GET_MODE (false_cond) != result_mode)
9762     return 0;
9763
9764   /* First, work out if the hardware can do this at all, or
9765      if it's too slow...  */
9766   if (! rs6000_compare_fp_p)
9767     {
9768       if (TARGET_ISEL)
9769         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
9770       return 0;
9771     }
9772
9773   /* Eliminate half of the comparisons by switching operands, this
9774      makes the remaining code simpler.  */
9775   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
9776       || code == LTGT || code == LT || code == UNLE)
9777     {
9778       code = reverse_condition_maybe_unordered (code);
9779       temp = true_cond;
9780       true_cond = false_cond;
9781       false_cond = temp;
9782     }
9783
9784   /* UNEQ and LTGT take four instructions for a comparison with zero,
9785      it'll probably be faster to use a branch here too.  */
9786   if (code == UNEQ && HONOR_NANS (compare_mode))
9787     return 0;
9788   
9789   if (GET_CODE (op1) == CONST_DOUBLE)
9790     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
9791     
9792   /* We're going to try to implement comparisons by performing
9793      a subtract, then comparing against zero.  Unfortunately,
9794      Inf - Inf is NaN which is not zero, and so if we don't
9795      know that the operand is finite and the comparison
9796      would treat EQ different to UNORDERED, we can't do it.  */
9797   if (HONOR_INFINITIES (compare_mode)
9798       && code != GT && code != UNGE
9799       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
9800       /* Constructs of the form (a OP b ? a : b) are safe.  */
9801       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
9802           || (! rtx_equal_p (op0, true_cond) 
9803               && ! rtx_equal_p (op1, true_cond))))
9804     return 0;
9805   /* At this point we know we can use fsel.  */
9806
9807   /* Reduce the comparison to a comparison against zero.  */
9808   temp = gen_reg_rtx (compare_mode);
9809   emit_insn (gen_rtx_SET (VOIDmode, temp,
9810                           gen_rtx_MINUS (compare_mode, op0, op1)));
9811   op0 = temp;
9812   op1 = CONST0_RTX (compare_mode);
9813
9814   /* If we don't care about NaNs we can reduce some of the comparisons
9815      down to faster ones.  */
9816   if (! HONOR_NANS (compare_mode))
9817     switch (code)
9818       {
9819       case GT:
9820         code = LE;
9821         temp = true_cond;
9822         true_cond = false_cond;
9823         false_cond = temp;
9824         break;
9825       case UNGE:
9826         code = GE;
9827         break;
9828       case UNEQ:
9829         code = EQ;
9830         break;
9831       default:
9832         break;
9833       }
9834
9835   /* Now, reduce everything down to a GE.  */
9836   switch (code)
9837     {
9838     case GE:
9839       break;
9840
9841     case LE:
9842       temp = gen_reg_rtx (compare_mode);
9843       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9844       op0 = temp;
9845       break;
9846
9847     case ORDERED:
9848       temp = gen_reg_rtx (compare_mode);
9849       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
9850       op0 = temp;
9851       break;
9852
9853     case EQ:
9854       temp = gen_reg_rtx (compare_mode);
9855       emit_insn (gen_rtx_SET (VOIDmode, temp, 
9856                               gen_rtx_NEG (compare_mode,
9857                                            gen_rtx_ABS (compare_mode, op0))));
9858       op0 = temp;
9859       break;
9860
9861     case UNGE:
9862       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
9863       temp = gen_reg_rtx (result_mode);
9864       emit_insn (gen_rtx_SET (VOIDmode, temp,
9865                               gen_rtx_IF_THEN_ELSE (result_mode,
9866                                                     gen_rtx_GE (VOIDmode,
9867                                                                 op0, op1),
9868                                                     true_cond, false_cond)));
9869       false_cond = true_cond;
9870       true_cond = temp;
9871
9872       temp = gen_reg_rtx (compare_mode);
9873       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9874       op0 = temp;
9875       break;
9876
9877     case GT:
9878       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
9879       temp = gen_reg_rtx (result_mode);
9880       emit_insn (gen_rtx_SET (VOIDmode, temp,
9881                               gen_rtx_IF_THEN_ELSE (result_mode, 
9882                                                     gen_rtx_GE (VOIDmode,
9883                                                                 op0, op1),
9884                                                     true_cond, false_cond)));
9885       true_cond = false_cond;
9886       false_cond = temp;
9887
9888       temp = gen_reg_rtx (compare_mode);
9889       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9890       op0 = temp;
9891       break;
9892
9893     default:
9894       abort ();
9895     }
9896
9897   emit_insn (gen_rtx_SET (VOIDmode, dest,
9898                           gen_rtx_IF_THEN_ELSE (result_mode,
9899                                                 gen_rtx_GE (VOIDmode,
9900                                                             op0, op1),
9901                                                 true_cond, false_cond)));
9902   return 1;
9903 }
9904
9905 /* Same as above, but for ints (isel).  */
9906
9907 static int
9908 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
9909 {
9910   rtx condition_rtx, cr;
9911
9912   /* All isel implementations thus far are 32-bits.  */
9913   if (GET_MODE (rs6000_compare_op0) != SImode)
9914     return 0;
9915
9916   /* We still have to do the compare, because isel doesn't do a
9917      compare, it just looks at the CRx bits set by a previous compare
9918      instruction.  */
9919   condition_rtx = rs6000_generate_compare (GET_CODE (op));
9920   cr = XEXP (condition_rtx, 0);
9921
9922   if (GET_MODE (cr) == CCmode)
9923     emit_insn (gen_isel_signed (dest, condition_rtx,
9924                                 true_cond, false_cond, cr));
9925   else
9926     emit_insn (gen_isel_unsigned (dest, condition_rtx,
9927                                   true_cond, false_cond, cr));
9928
9929   return 1;
9930 }
9931
9932 const char *
9933 output_isel (rtx *operands)
9934 {
9935   enum rtx_code code;
9936
9937   code = GET_CODE (operands[1]);
9938   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
9939     {
9940       PUT_CODE (operands[1], reverse_condition (code));
9941       return "isel %0,%3,%2,%j1";
9942     }
9943   else
9944     return "isel %0,%2,%3,%j1";
9945 }
9946
9947 void
9948 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
9949 {
9950   enum machine_mode mode = GET_MODE (op0);
9951   enum rtx_code c;
9952   rtx target;
9953
9954   if (code == SMAX || code == SMIN)
9955     c = GE;
9956   else
9957     c = GEU;
9958
9959   if (code == SMAX || code == UMAX)
9960     target = emit_conditional_move (dest, c, op0, op1, mode, 
9961                                     op0, op1, mode, 0);
9962   else
9963     target = emit_conditional_move (dest, c, op0, op1, mode, 
9964                                     op1, op0, mode, 0);
9965   if (target == NULL_RTX)
9966     abort ();
9967   if (target != dest)
9968     emit_move_insn (dest, target);
9969 }
9970
9971 /* Called by splitter for multireg moves.
9972    Input: 
9973           operands[0] : Destination of move
9974           operands[1] : Source of move
9975
9976    Output:
9977           operands[2-n] : Destination slots
9978           operands[n-m] : Source slots
9979    where n = 2 + HARD_REGNO_NREGS (reg, GET_MODE (operands[0]))
9980          m = 2 + 2 * HARD_REGNO_NREGS (reg, GET_MODE (operands[0])) - 1
9981
9982    Splits the move of operands[1] to operands[0].
9983    This is done, if GPRs are one of the operands.  In this case
9984    a sequence of simple move insns has to be issued.  The sequence of these
9985    move insns has to be done in correct order to avoid early clobber of the
9986    base register or destructive overlap of registers. 
9987 */
9988           
9989 void
9990 rs6000_split_multireg_move (rtx *operands)
9991 {
9992   int nregs, reg, i, j, used_update = 0;
9993   enum machine_mode mode; 
9994   rtx dst = operands[0];
9995   rtx src = operands[1];
9996   rtx insn = 0;
9997
9998   /* Calculate number to move (2/4 for 32/64 bit mode).  */ 
9999
10000   reg = REG_P (operands[0]) ? REGNO (operands[0]) : REGNO (operands[1]); 
10001   mode = GET_MODE (operands[0]);
10002   nregs = HARD_REGNO_NREGS (reg, mode);                                  
10003   
10004   if (REG_P (operands[1]) 
10005       && REG_P (operands[0]) 
10006       && (REGNO (operands[1]) < REGNO (operands[0])))
10007     {  
10008       /* Move register range backwards, if we have destructive overlap.  */
10009
10010       j = nregs;
10011       for (i = 0; i < nregs; i++)
10012         {
10013           j--;
10014           operands[i+2] = operand_subword (operands[0], j, 0, mode);
10015           operands[i+2+nregs] = 
10016             operand_subword (operands[1], j, 0, mode);   
10017         }
10018     }     
10019   else
10020     {
10021       j = -1;
10022
10023       if (GET_CODE (operands[1]) == MEM)
10024         {
10025           rtx breg;
10026
10027           if (GET_CODE (XEXP (operands[1], 0)) == PRE_INC
10028               || GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
10029             {
10030               rtx delta_rtx;
10031               breg = XEXP (XEXP (operands[1], 0), 0);
10032               delta_rtx =  GET_CODE (XEXP (operands[1], 0)) == PRE_INC 
10033                   ? GEN_INT (GET_MODE_SIZE (GET_MODE (operands[1]))) 
10034                   : GEN_INT (-GET_MODE_SIZE (GET_MODE (operands[1]))); 
10035               insn = emit_insn (TARGET_32BIT
10036                                 ? gen_addsi3 (breg, breg, delta_rtx)
10037                                 : gen_adddi3 (breg, breg, delta_rtx));
10038               src = gen_rtx_MEM (mode, breg);
10039             }
10040
10041           /* We have now address involving an base register only.
10042              If we use one of the registers to address memory, 
10043              we have change that register last.  */
10044
10045           breg = (GET_CODE (XEXP (src, 0)) == PLUS
10046                   ? XEXP (XEXP (src, 0), 0)
10047                   : XEXP (src, 0));
10048
10049           if (!REG_P (breg))
10050               abort();
10051
10052           if (REGNO (breg) >= REGNO (dst) 
10053               && REGNO (breg) < REGNO (dst) + nregs)
10054             j = REGNO (breg) - REGNO (dst);
10055         }
10056
10057       if (GET_CODE (operands[0]) == MEM)
10058         {
10059           rtx breg;
10060
10061           if (GET_CODE (XEXP (operands[0], 0)) == PRE_INC
10062               || GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
10063             {
10064               rtx delta_rtx;
10065               breg = XEXP (XEXP (operands[0], 0), 0);
10066               delta_rtx = GET_CODE (XEXP (operands[0], 0)) == PRE_INC 
10067                 ? GEN_INT (GET_MODE_SIZE (GET_MODE (operands[0]))) 
10068                 : GEN_INT (-GET_MODE_SIZE (GET_MODE (operands[0]))); 
10069
10070               /* We have to update the breg before doing the store.
10071                  Use store with update, if available.  */
10072
10073               if (TARGET_UPDATE)
10074                 {
10075                   insn = emit_insn (TARGET_32BIT
10076                                     ? gen_movsi_update (breg, breg, delta_rtx, 
10077                                         operand_subword (src, 0, 0, mode))
10078                                     : gen_movdi_update (breg, breg, delta_rtx,
10079                                         operand_subword (src, 0, 0, mode)));
10080                   used_update = 1;
10081                 }
10082               else
10083                   insn = emit_insn (TARGET_32BIT
10084                                     ? gen_addsi3 (breg, breg, delta_rtx)
10085                                     : gen_adddi3 (breg, breg, delta_rtx));
10086               dst = gen_rtx_MEM (mode, breg);
10087             }
10088         }
10089
10090       for (i = 0; i < nregs; i++)
10091         {  
10092           /* Calculate index to next subword.  */
10093           ++j;
10094           if (j == nregs) 
10095             j = 0;
10096
10097           operands[i+2] = operand_subword (dst, j, 0, mode);
10098           operands[i+2+nregs] = operand_subword (src, j, 0, mode);
10099
10100           if (j == 0 && used_update)
10101             {
10102               /* Already emited move of first word by 
10103                  store with update -> emit dead insn instead (r := r).  */
10104               operands[i+2] = operands[i+2+nregs];
10105             }
10106         }
10107     }
10108 }
10109
10110 \f
10111 /* This page contains routines that are used to determine what the
10112    function prologue and epilogue code will do and write them out.  */
10113
10114 /* Return the first fixed-point register that is required to be
10115    saved. 32 if none.  */
10116
10117 int
10118 first_reg_to_save (void)
10119 {
10120   int first_reg;
10121
10122   /* Find lowest numbered live register.  */
10123   for (first_reg = 13; first_reg <= 31; first_reg++)
10124     if (regs_ever_live[first_reg] 
10125         && (! call_used_regs[first_reg]
10126             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
10127                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
10128                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
10129       break;
10130
10131 #if TARGET_MACHO
10132   if (flag_pic
10133       && current_function_uses_pic_offset_table
10134       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
10135     return RS6000_PIC_OFFSET_TABLE_REGNUM;
10136 #endif
10137
10138   return first_reg;
10139 }
10140
10141 /* Similar, for FP regs.  */
10142
10143 int
10144 first_fp_reg_to_save (void)
10145 {
10146   int first_reg;
10147
10148   /* Find lowest numbered live register.  */
10149   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
10150     if (regs_ever_live[first_reg])
10151       break;
10152
10153   return first_reg;
10154 }
10155
10156 /* Similar, for AltiVec regs.  */
10157
10158 static int
10159 first_altivec_reg_to_save (void)
10160 {
10161   int i;
10162
10163   /* Stack frame remains as is unless we are in AltiVec ABI.  */
10164   if (! TARGET_ALTIVEC_ABI)
10165     return LAST_ALTIVEC_REGNO + 1;
10166
10167   /* Find lowest numbered live register.  */
10168   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
10169     if (regs_ever_live[i])
10170       break;
10171
10172   return i;
10173 }
10174
10175 /* Return a 32-bit mask of the AltiVec registers we need to set in
10176    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
10177    the 32-bit word is 0.  */
10178
10179 static unsigned int
10180 compute_vrsave_mask (void)
10181 {
10182   unsigned int i, mask = 0;
10183
10184   /* First, find out if we use _any_ altivec registers.  */
10185   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10186     if (regs_ever_live[i])
10187       mask |= ALTIVEC_REG_BIT (i);
10188
10189   if (mask == 0)
10190     return mask;
10191
10192   /* Next, remove the argument registers from the set.  These must
10193      be in the VRSAVE mask set by the caller, so we don't need to add
10194      them in again.  More importantly, the mask we compute here is
10195      used to generate CLOBBERs in the set_vrsave insn, and we do not
10196      wish the argument registers to die.  */
10197   for (i = cfun->args_info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
10198     mask &= ~ALTIVEC_REG_BIT (i);
10199
10200   /* Similarly, remove the return value from the set.  */
10201   {
10202     bool yes = false;
10203     diddle_return_value (is_altivec_return_reg, &yes);
10204     if (yes)
10205       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
10206   }
10207
10208   return mask;
10209 }
10210
10211 static void
10212 is_altivec_return_reg (rtx reg, void *xyes)
10213 {
10214   bool *yes = (bool *) xyes;
10215   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
10216     *yes = true;
10217 }
10218
10219 \f
10220 /* Calculate the stack information for the current function.  This is
10221    complicated by having two separate calling sequences, the AIX calling
10222    sequence and the V.4 calling sequence.
10223
10224    AIX (and Darwin/Mac OS X) stack frames look like:
10225                                                           32-bit  64-bit
10226         SP----> +---------------------------------------+
10227                 | back chain to caller                  | 0       0
10228                 +---------------------------------------+
10229                 | saved CR                              | 4       8 (8-11)
10230                 +---------------------------------------+
10231                 | saved LR                              | 8       16
10232                 +---------------------------------------+
10233                 | reserved for compilers                | 12      24
10234                 +---------------------------------------+
10235                 | reserved for binders                  | 16      32
10236                 +---------------------------------------+
10237                 | saved TOC pointer                     | 20      40
10238                 +---------------------------------------+
10239                 | Parameter save area (P)               | 24      48
10240                 +---------------------------------------+
10241                 | Alloca space (A)                      | 24+P    etc.
10242                 +---------------------------------------+
10243                 | Local variable space (L)              | 24+P+A
10244                 +---------------------------------------+
10245                 | Float/int conversion temporary (X)    | 24+P+A+L
10246                 +---------------------------------------+
10247                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
10248                 +---------------------------------------+
10249                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
10250                 +---------------------------------------+
10251                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
10252                 +---------------------------------------+
10253                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
10254                 +---------------------------------------+
10255                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
10256                 +---------------------------------------+
10257         old SP->| back chain to caller's caller         |
10258                 +---------------------------------------+
10259
10260    The required alignment for AIX configurations is two words (i.e., 8
10261    or 16 bytes).
10262
10263
10264    V.4 stack frames look like:
10265
10266         SP----> +---------------------------------------+
10267                 | back chain to caller                  | 0
10268                 +---------------------------------------+
10269                 | caller's saved LR                     | 4
10270                 +---------------------------------------+
10271                 | Parameter save area (P)               | 8
10272                 +---------------------------------------+
10273                 | Alloca space (A)                      | 8+P
10274                 +---------------------------------------+    
10275                 | Varargs save area (V)                 | 8+P+A
10276                 +---------------------------------------+    
10277                 | Local variable space (L)              | 8+P+A+V
10278                 +---------------------------------------+    
10279                 | Float/int conversion temporary (X)    | 8+P+A+V+L
10280                 +---------------------------------------+
10281                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
10282                 +---------------------------------------+
10283                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
10284                 +---------------------------------------+
10285                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
10286                 +---------------------------------------+
10287                 | SPE: area for 64-bit GP registers     |
10288                 +---------------------------------------+
10289                 | SPE alignment padding                 |
10290                 +---------------------------------------+
10291                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
10292                 +---------------------------------------+    
10293                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
10294                 +---------------------------------------+    
10295                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
10296                 +---------------------------------------+
10297         old SP->| back chain to caller's caller         |
10298                 +---------------------------------------+
10299
10300    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
10301    given.  (But note below and in sysv4.h that we require only 8 and
10302    may round up the size of our stack frame anyways.  The historical
10303    reason is early versions of powerpc-linux which didn't properly
10304    align the stack at program startup.  A happy side-effect is that
10305    -mno-eabi libraries can be used with -meabi programs.)
10306
10307    The EABI configuration defaults to the V.4 layout.  However,
10308    the stack alignment requirements may differ.  If -mno-eabi is not
10309    given, the required stack alignment is 8 bytes; if -mno-eabi is
10310    given, the required alignment is 16 bytes.  (But see V.4 comment
10311    above.)  */
10312
10313 #ifndef ABI_STACK_BOUNDARY
10314 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
10315 #endif
10316
10317 static rs6000_stack_t *
10318 rs6000_stack_info (void)
10319 {
10320   static rs6000_stack_t info, zero_info;
10321   rs6000_stack_t *info_ptr = &info;
10322   int reg_size = TARGET_POWERPC64 ? 8 : 4;
10323   int ehrd_size;
10324   HOST_WIDE_INT total_raw_size;
10325
10326   /* Zero all fields portably.  */
10327   info = zero_info;
10328
10329   if (TARGET_SPE)
10330     {
10331       /* Cache value so we don't rescan instruction chain over and over.  */
10332       if (cfun->machine->insn_chain_scanned_p == 0)
10333         {
10334           cfun->machine->insn_chain_scanned_p = 1;
10335           info_ptr->spe_64bit_regs_used = (int) spe_func_has_64bit_regs_p ();
10336         }
10337     }
10338
10339   /* Select which calling sequence.  */
10340   info_ptr->abi = DEFAULT_ABI;
10341
10342   /* Calculate which registers need to be saved & save area size.  */
10343   info_ptr->first_gp_reg_save = first_reg_to_save ();
10344   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
10345      even if it currently looks like we won't.  */
10346   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
10347        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
10348        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
10349       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
10350     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
10351   else
10352     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
10353
10354   /* For the SPE, we have an additional upper 32-bits on each GPR.
10355      Ideally we should save the entire 64-bits only when the upper
10356      half is used in SIMD instructions.  Since we only record
10357      registers live (not the size they are used in), this proves
10358      difficult because we'd have to traverse the instruction chain at
10359      the right time, taking reload into account.  This is a real pain,
10360      so we opt to save the GPRs in 64-bits always if but one register
10361      gets used in 64-bits.  Otherwise, all the registers in the frame
10362      get saved in 32-bits.
10363
10364      So... since when we save all GPRs (except the SP) in 64-bits, the
10365      traditional GP save area will be empty.  */
10366   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10367     info_ptr->gp_size = 0;
10368
10369   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
10370   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
10371
10372   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
10373   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
10374                                  - info_ptr->first_altivec_reg_save);
10375
10376   /* Does this function call anything?  */
10377   info_ptr->calls_p = (! current_function_is_leaf
10378                        || cfun->machine->ra_needs_full_frame);
10379
10380   /* Determine if we need to save the link register.  */
10381   if (rs6000_ra_ever_killed ()
10382       || (DEFAULT_ABI == ABI_AIX
10383           && current_function_profile
10384           && !TARGET_PROFILE_KERNEL)
10385 #ifdef TARGET_RELOCATABLE
10386       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
10387 #endif
10388       || (info_ptr->first_fp_reg_save != 64
10389           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
10390       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
10391       || (DEFAULT_ABI == ABI_V4 && current_function_calls_alloca)
10392       || (DEFAULT_ABI == ABI_DARWIN
10393           && flag_pic
10394           && current_function_uses_pic_offset_table)
10395       || info_ptr->calls_p)
10396     {
10397       info_ptr->lr_save_p = 1;
10398       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
10399     }
10400
10401   /* Determine if we need to save the condition code registers.  */
10402   if (regs_ever_live[CR2_REGNO] 
10403       || regs_ever_live[CR3_REGNO]
10404       || regs_ever_live[CR4_REGNO])
10405     {
10406       info_ptr->cr_save_p = 1;
10407       if (DEFAULT_ABI == ABI_V4)
10408         info_ptr->cr_size = reg_size;
10409     }
10410
10411   /* If the current function calls __builtin_eh_return, then we need
10412      to allocate stack space for registers that will hold data for
10413      the exception handler.  */
10414   if (current_function_calls_eh_return)
10415     {
10416       unsigned int i;
10417       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
10418         continue;
10419
10420       /* SPE saves EH registers in 64-bits.  */
10421       ehrd_size = i * (TARGET_SPE_ABI
10422                        && info_ptr->spe_64bit_regs_used != 0
10423                        ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
10424     }
10425   else
10426     ehrd_size = 0;
10427
10428   /* Determine various sizes.  */
10429   info_ptr->reg_size     = reg_size;
10430   info_ptr->fixed_size   = RS6000_SAVE_AREA;
10431   info_ptr->varargs_size = RS6000_VARARGS_AREA;
10432   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
10433   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
10434                                          8);
10435
10436   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10437     info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
10438   else
10439     info_ptr->spe_gp_size = 0;
10440
10441   if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE)
10442     {
10443       info_ptr->vrsave_mask = compute_vrsave_mask ();
10444       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
10445     }
10446   else
10447     {
10448       info_ptr->vrsave_mask = 0;
10449       info_ptr->vrsave_size = 0;
10450     }
10451
10452   /* Calculate the offsets.  */
10453   switch (DEFAULT_ABI)
10454     {
10455     case ABI_NONE:
10456     default:
10457       abort ();
10458
10459     case ABI_AIX:
10460     case ABI_DARWIN:
10461       info_ptr->fp_save_offset   = - info_ptr->fp_size;
10462       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
10463
10464       if (TARGET_ALTIVEC_ABI)
10465         {
10466           info_ptr->vrsave_save_offset
10467             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
10468
10469           /* Align stack so vector save area is on a quadword boundary.  */
10470           if (info_ptr->altivec_size != 0)
10471             info_ptr->altivec_padding_size
10472               = 16 - (-info_ptr->vrsave_save_offset % 16);
10473           else
10474             info_ptr->altivec_padding_size = 0;
10475
10476           info_ptr->altivec_save_offset
10477             = info_ptr->vrsave_save_offset
10478             - info_ptr->altivec_padding_size
10479             - info_ptr->altivec_size;
10480
10481           /* Adjust for AltiVec case.  */
10482           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
10483         }
10484       else
10485         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
10486       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
10487       info_ptr->lr_save_offset   = 2*reg_size;
10488       break;
10489
10490     case ABI_V4:
10491       info_ptr->fp_save_offset   = - info_ptr->fp_size;
10492       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
10493       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
10494
10495       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10496       {
10497         /* Align stack so SPE GPR save area is aligned on a
10498            double-word boundary.  */
10499         if (info_ptr->spe_gp_size != 0)
10500           info_ptr->spe_padding_size
10501             = 8 - (-info_ptr->cr_save_offset % 8);
10502         else
10503           info_ptr->spe_padding_size = 0;
10504
10505         info_ptr->spe_gp_save_offset
10506           = info_ptr->cr_save_offset
10507           - info_ptr->spe_padding_size
10508           - info_ptr->spe_gp_size;
10509
10510         /* Adjust for SPE case.  */
10511         info_ptr->toc_save_offset
10512           = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
10513       }
10514       else if (TARGET_ALTIVEC_ABI)
10515         {
10516           info_ptr->vrsave_save_offset
10517             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
10518
10519           /* Align stack so vector save area is on a quadword boundary.  */
10520           if (info_ptr->altivec_size != 0)
10521             info_ptr->altivec_padding_size
10522               = 16 - (-info_ptr->vrsave_save_offset % 16);
10523           else
10524             info_ptr->altivec_padding_size = 0;
10525
10526           info_ptr->altivec_save_offset
10527             = info_ptr->vrsave_save_offset
10528             - info_ptr->altivec_padding_size
10529             - info_ptr->altivec_size;
10530
10531           /* Adjust for AltiVec case.  */
10532           info_ptr->toc_save_offset
10533             = info_ptr->altivec_save_offset - info_ptr->toc_size;
10534         }
10535       else
10536         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
10537       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
10538       info_ptr->lr_save_offset   = reg_size;
10539       break;
10540     }
10541
10542   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
10543                                          + info_ptr->gp_size
10544                                          + info_ptr->altivec_size
10545                                          + info_ptr->altivec_padding_size
10546                                          + info_ptr->spe_gp_size
10547                                          + info_ptr->spe_padding_size
10548                                          + ehrd_size
10549                                          + info_ptr->cr_size
10550                                          + info_ptr->lr_size
10551                                          + info_ptr->vrsave_size
10552                                          + info_ptr->toc_size,
10553                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
10554                                          ? 16 : 8);
10555
10556   total_raw_size         = (info_ptr->vars_size
10557                             + info_ptr->parm_size
10558                             + info_ptr->save_size
10559                             + info_ptr->varargs_size
10560                             + info_ptr->fixed_size);
10561
10562   info_ptr->total_size =
10563     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
10564
10565   /* Determine if we need to allocate any stack frame:
10566
10567      For AIX we need to push the stack if a frame pointer is needed
10568      (because the stack might be dynamically adjusted), if we are
10569      debugging, if we make calls, or if the sum of fp_save, gp_save,
10570      and local variables are more than the space needed to save all
10571      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
10572      + 18*8 = 288 (GPR13 reserved).
10573
10574      For V.4 we don't have the stack cushion that AIX uses, but assume
10575      that the debugger can handle stackless frames.  */
10576
10577   if (info_ptr->calls_p)
10578     info_ptr->push_p = 1;
10579
10580   else if (DEFAULT_ABI == ABI_V4)
10581     info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
10582
10583   else if (frame_pointer_needed)
10584     info_ptr->push_p = 1;
10585
10586   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
10587     info_ptr->push_p = 1;
10588
10589   else
10590     info_ptr->push_p
10591       = total_raw_size - info_ptr->fixed_size > (TARGET_32BIT ? 220 : 288);
10592
10593   /* Zero offsets if we're not saving those registers.  */
10594   if (info_ptr->fp_size == 0)
10595     info_ptr->fp_save_offset = 0;
10596
10597   if (info_ptr->gp_size == 0)
10598     info_ptr->gp_save_offset = 0;
10599
10600   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
10601     info_ptr->altivec_save_offset = 0;
10602
10603   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
10604     info_ptr->vrsave_save_offset = 0;
10605
10606   if (! TARGET_SPE_ABI
10607       || info_ptr->spe_64bit_regs_used == 0
10608       || info_ptr->spe_gp_size == 0)
10609     info_ptr->spe_gp_save_offset = 0;
10610
10611   if (! info_ptr->lr_save_p)
10612     info_ptr->lr_save_offset = 0;
10613
10614   if (! info_ptr->cr_save_p)
10615     info_ptr->cr_save_offset = 0;
10616
10617   if (! info_ptr->toc_save_p)
10618     info_ptr->toc_save_offset = 0;
10619
10620   return info_ptr;
10621 }
10622
10623 /* Return true if the current function uses any GPRs in 64-bit SIMD
10624    mode.  */
10625
10626 static bool
10627 spe_func_has_64bit_regs_p (void)
10628 {
10629   rtx insns, insn;
10630
10631   /* Functions that save and restore all the call-saved registers will
10632      need to save/restore the registers in 64-bits.  */
10633   if (current_function_calls_eh_return
10634       || current_function_calls_setjmp
10635       || current_function_has_nonlocal_goto)
10636     return true;
10637
10638   insns = get_insns ();
10639
10640   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
10641     {
10642       if (INSN_P (insn))
10643         {
10644           rtx i;
10645
10646           i = PATTERN (insn);
10647           if (GET_CODE (i) == SET
10648               && SPE_VECTOR_MODE (GET_MODE (SET_SRC (i))))
10649             return true;
10650         }
10651     }
10652
10653   return false;
10654 }
10655
10656 static void
10657 debug_stack_info (rs6000_stack_t *info)
10658 {
10659   const char *abi_string;
10660
10661   if (! info)
10662     info = rs6000_stack_info ();
10663
10664   fprintf (stderr, "\nStack information for function %s:\n",
10665            ((current_function_decl && DECL_NAME (current_function_decl))
10666             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
10667             : "<unknown>"));
10668
10669   switch (info->abi)
10670     {
10671     default:             abi_string = "Unknown";        break;
10672     case ABI_NONE:       abi_string = "NONE";           break;
10673     case ABI_AIX:        abi_string = "AIX";            break;
10674     case ABI_DARWIN:     abi_string = "Darwin";         break;
10675     case ABI_V4:         abi_string = "V.4";            break;
10676     }
10677
10678   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
10679
10680   if (TARGET_ALTIVEC_ABI)
10681     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
10682
10683   if (TARGET_SPE_ABI)
10684     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
10685
10686   if (info->first_gp_reg_save != 32)
10687     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
10688
10689   if (info->first_fp_reg_save != 64)
10690     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
10691
10692   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
10693     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
10694              info->first_altivec_reg_save);
10695
10696   if (info->lr_save_p)
10697     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
10698
10699   if (info->cr_save_p)
10700     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
10701
10702   if (info->toc_save_p)
10703     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
10704
10705   if (info->vrsave_mask)
10706     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
10707
10708   if (info->push_p)
10709     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
10710
10711   if (info->calls_p)
10712     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
10713
10714   if (info->gp_save_offset)
10715     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
10716
10717   if (info->fp_save_offset)
10718     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
10719
10720   if (info->altivec_save_offset)
10721     fprintf (stderr, "\taltivec_save_offset = %5d\n",
10722              info->altivec_save_offset);
10723
10724   if (info->spe_gp_save_offset)
10725     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
10726              info->spe_gp_save_offset);
10727
10728   if (info->vrsave_save_offset)
10729     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
10730              info->vrsave_save_offset);
10731
10732   if (info->lr_save_offset)
10733     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
10734
10735   if (info->cr_save_offset)
10736     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
10737
10738   if (info->toc_save_offset)
10739     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
10740
10741   if (info->varargs_save_offset)
10742     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
10743
10744   if (info->total_size)
10745     fprintf (stderr, "\ttotal_size          = "HOST_WIDE_INT_PRINT_DEC"\n",
10746              info->total_size);
10747
10748   if (info->varargs_size)
10749     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
10750
10751   if (info->vars_size)
10752     fprintf (stderr, "\tvars_size           = "HOST_WIDE_INT_PRINT_DEC"\n",
10753              info->vars_size);
10754
10755   if (info->parm_size)
10756     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
10757
10758   if (info->fixed_size)
10759     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
10760
10761   if (info->gp_size)
10762     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
10763
10764   if (info->spe_gp_size)
10765     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
10766
10767   if (info->fp_size)
10768     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
10769
10770   if (info->altivec_size)
10771     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
10772
10773   if (info->vrsave_size)
10774     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
10775
10776   if (info->altivec_padding_size)
10777     fprintf (stderr, "\taltivec_padding_size= %5d\n",
10778              info->altivec_padding_size);
10779
10780   if (info->spe_padding_size)
10781     fprintf (stderr, "\tspe_padding_size    = %5d\n",
10782              info->spe_padding_size);
10783
10784   if (info->lr_size)
10785     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
10786
10787   if (info->cr_size)
10788     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
10789
10790   if (info->toc_size)
10791     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
10792
10793   if (info->save_size)
10794     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
10795
10796   if (info->reg_size != 4)
10797     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
10798
10799   fprintf (stderr, "\n");
10800 }
10801
10802 rtx
10803 rs6000_return_addr (int count, rtx frame)
10804 {
10805   /* Currently we don't optimize very well between prolog and body
10806      code and for PIC code the code can be actually quite bad, so
10807      don't try to be too clever here.  */
10808   if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
10809     {
10810       cfun->machine->ra_needs_full_frame = 1;
10811
10812       return
10813         gen_rtx_MEM
10814           (Pmode,
10815            memory_address
10816            (Pmode,
10817             plus_constant (copy_to_reg
10818                            (gen_rtx_MEM (Pmode,
10819                                          memory_address (Pmode, frame))),
10820                            RETURN_ADDRESS_OFFSET)));
10821     }
10822
10823   cfun->machine->ra_need_lr = 1;
10824   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
10825 }
10826
10827 /* Say whether a function is a candidate for sibcall handling or not.
10828    We do not allow indirect calls to be optimized into sibling calls.
10829    Also, we can't do it if there are any vector parameters; there's
10830    nowhere to put the VRsave code so it works; note that functions with
10831    vector parameters are required to have a prototype, so the argument
10832    type info must be available here.  (The tail recursion case can work
10833    with vector parameters, but there's no way to distinguish here.) */
10834 static bool
10835 rs6000_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
10836 {
10837   tree type;
10838   if (decl)
10839     {
10840       if (TARGET_ALTIVEC_VRSAVE)
10841         {
10842           for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
10843                type; type = TREE_CHAIN (type))
10844             {
10845               if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
10846                 return false;
10847             }
10848         }
10849       if (DEFAULT_ABI == ABI_DARWIN
10850           || (*targetm.binds_local_p) (decl))
10851         {
10852           tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
10853
10854           if (!lookup_attribute ("longcall", attr_list)
10855               || lookup_attribute ("shortcall", attr_list))
10856             return true;
10857         }
10858     }
10859   return false;
10860 }
10861
10862 static int
10863 rs6000_ra_ever_killed (void)
10864 {
10865   rtx top;
10866   rtx reg;
10867   rtx insn;
10868
10869   /* Irritatingly, there are two kinds of thunks -- those created with
10870      TARGET_ASM_OUTPUT_MI_THUNK and those with DECL_THUNK_P that go
10871      through the regular part of the compiler.  This is a very hacky
10872      way to tell them apart.  */
10873   if (current_function_is_thunk && !no_new_pseudos)
10874     return 0;
10875
10876   /* regs_ever_live has LR marked as used if any sibcalls are present,
10877      but this should not force saving and restoring in the
10878      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
10879      clobbers LR, so that is inappropriate. */
10880
10881   /* Also, the prologue can generate a store into LR that
10882      doesn't really count, like this:
10883
10884         move LR->R0
10885         bcl to set PIC register
10886         move LR->R31
10887         move R0->LR
10888
10889      When we're called from the epilogue, we need to avoid counting
10890      this as a store.  */
10891          
10892   push_topmost_sequence ();
10893   top = get_insns ();
10894   pop_topmost_sequence ();
10895   reg = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
10896
10897   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
10898     {
10899       if (INSN_P (insn))
10900         {
10901           if (FIND_REG_INC_NOTE (insn, reg))
10902             return 1;
10903           else if (GET_CODE (insn) == CALL_INSN 
10904                    && !SIBLING_CALL_P (insn))
10905             return 1;
10906           else if (set_of (reg, insn) != NULL_RTX
10907                    && !prologue_epilogue_contains (insn))
10908             return 1;
10909         }
10910     }
10911   return 0;
10912 }
10913 \f
10914 /* Add a REG_MAYBE_DEAD note to the insn.  */
10915 static void
10916 rs6000_maybe_dead (rtx insn)
10917 {
10918   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
10919                                         const0_rtx,
10920                                         REG_NOTES (insn));
10921 }
10922
10923 /* Emit instructions needed to load the TOC register.
10924    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
10925    a constant pool; or for SVR4 -fpic.  */
10926
10927 void
10928 rs6000_emit_load_toc_table (int fromprolog)
10929 {
10930   rtx dest, insn;
10931   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
10932
10933   if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10934     {
10935       rtx temp = (fromprolog
10936                   ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
10937                   : gen_reg_rtx (Pmode));
10938       insn = emit_insn (gen_load_toc_v4_pic_si (temp));
10939       if (fromprolog)
10940         rs6000_maybe_dead (insn);
10941       insn = emit_move_insn (dest, temp);
10942       if (fromprolog)
10943         rs6000_maybe_dead (insn);
10944     }
10945   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
10946     {
10947       char buf[30];
10948       rtx tempLR = (fromprolog
10949                     ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
10950                     : gen_reg_rtx (Pmode));
10951       rtx temp0 = (fromprolog
10952                    ? gen_rtx_REG (Pmode, 0)
10953                    : gen_reg_rtx (Pmode));
10954       rtx symF;
10955
10956       /* possibly create the toc section */
10957       if (! toc_initialized)
10958         {
10959           toc_section ();
10960           function_section (current_function_decl);
10961         }
10962
10963       if (fromprolog)
10964         {
10965           rtx symL;
10966
10967           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
10968           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10969
10970           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
10971           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10972
10973           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR,
10974                                                                symF)));
10975           rs6000_maybe_dead (emit_move_insn (dest, tempLR));
10976           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
10977                                                                symL,
10978                                                                symF)));
10979         }
10980       else
10981         {
10982           rtx tocsym;
10983           static int reload_toc_labelno = 0;
10984
10985           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
10986
10987           ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
10988           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10989
10990           emit_insn (gen_load_toc_v4_PIC_1b (tempLR, symF, tocsym));
10991           emit_move_insn (dest, tempLR);
10992           emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
10993         }
10994       insn = emit_insn (gen_addsi3 (dest, temp0, dest));
10995       if (fromprolog)
10996         rs6000_maybe_dead (insn);
10997     }
10998   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
10999     {
11000       /* This is for AIX code running in non-PIC ELF32.  */
11001       char buf[30];
11002       rtx realsym;
11003       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
11004       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11005
11006       insn = emit_insn (gen_elf_high (dest, realsym));
11007       if (fromprolog)
11008         rs6000_maybe_dead (insn);
11009       insn = emit_insn (gen_elf_low (dest, dest, realsym));
11010       if (fromprolog)
11011         rs6000_maybe_dead (insn);
11012     }
11013   else if (DEFAULT_ABI == ABI_AIX)
11014     {
11015       if (TARGET_32BIT)
11016         insn = emit_insn (gen_load_toc_aix_si (dest));
11017       else
11018         insn = emit_insn (gen_load_toc_aix_di (dest));
11019       if (fromprolog)
11020         rs6000_maybe_dead (insn);
11021     }
11022   else
11023     abort ();
11024 }
11025
11026 /* Emit instructions to restore the link register after determining where
11027    its value has been stored.  */
11028
11029 void
11030 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
11031 {
11032   rs6000_stack_t *info = rs6000_stack_info ();
11033   rtx operands[2];
11034
11035   operands[0] = source;
11036   operands[1] = scratch;
11037
11038   if (info->lr_save_p)
11039     {
11040       rtx frame_rtx = stack_pointer_rtx;
11041       HOST_WIDE_INT sp_offset = 0;
11042       rtx tmp;
11043
11044       if (frame_pointer_needed
11045           || current_function_calls_alloca
11046           || info->total_size > 32767)
11047         {
11048           emit_move_insn (operands[1], gen_rtx_MEM (Pmode, frame_rtx));
11049           frame_rtx = operands[1];
11050         }
11051       else if (info->push_p)
11052         sp_offset = info->total_size;
11053
11054       tmp = plus_constant (frame_rtx, info->lr_save_offset + sp_offset);
11055       tmp = gen_rtx_MEM (Pmode, tmp);
11056       emit_move_insn (tmp, operands[0]);
11057     }
11058   else
11059     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), operands[0]);
11060 }
11061
11062 int   
11063 get_TOC_alias_set (void)
11064 {
11065     static int set = -1;
11066     if (set == -1)
11067       set = new_alias_set ();
11068     return set;
11069 }   
11070
11071 /* This returns nonzero if the current function uses the TOC.  This is
11072    determined by the presence of (unspec ... UNSPEC_TOC) or
11073    use (unspec ... UNSPEC_TOC), which are generated by the various
11074    load_toc_* patterns.  */
11075
11076 int
11077 uses_TOC (void) 
11078 {
11079   rtx insn;
11080
11081   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11082     if (INSN_P (insn))
11083       {
11084         rtx pat = PATTERN (insn);
11085         int i;
11086
11087         if (GET_CODE (pat) == PARALLEL) 
11088           for (i = 0; i < XVECLEN (pat, 0); i++)
11089             {
11090               rtx sub = XVECEXP (pat, 0, i);
11091               if (GET_CODE (sub) == USE)
11092                 {
11093                   sub = XEXP (sub, 0);
11094                   if (GET_CODE (sub) == UNSPEC
11095                       && XINT (sub, 1) == UNSPEC_TOC)
11096                     return 1;
11097                 }
11098             }
11099       }
11100   return 0;
11101 }
11102
11103 rtx
11104 create_TOC_reference (rtx symbol) 
11105 {
11106   return gen_rtx_PLUS (Pmode, 
11107            gen_rtx_REG (Pmode, TOC_REGISTER),
11108              gen_rtx_CONST (Pmode, 
11109                gen_rtx_MINUS (Pmode, symbol, 
11110                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
11111 }
11112
11113 /* If _Unwind_* has been called from within the same module,
11114    toc register is not guaranteed to be saved to 40(1) on function
11115    entry.  Save it there in that case.  */
11116
11117 void
11118 rs6000_aix_emit_builtin_unwind_init (void)
11119 {
11120   rtx mem;
11121   rtx stack_top = gen_reg_rtx (Pmode);
11122   rtx opcode_addr = gen_reg_rtx (Pmode);
11123   rtx opcode = gen_reg_rtx (SImode);
11124   rtx tocompare = gen_reg_rtx (SImode);
11125   rtx no_toc_save_needed = gen_label_rtx ();
11126
11127   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
11128   emit_move_insn (stack_top, mem);
11129
11130   mem = gen_rtx_MEM (Pmode,
11131                      gen_rtx_PLUS (Pmode, stack_top,
11132                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
11133   emit_move_insn (opcode_addr, mem);
11134   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
11135   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014
11136                                            : 0xE8410028, SImode));
11137
11138   do_compare_rtx_and_jump (opcode, tocompare, EQ, 1,
11139                            SImode, NULL_RTX, NULL_RTX,
11140                            no_toc_save_needed);
11141
11142   mem = gen_rtx_MEM (Pmode,
11143                      gen_rtx_PLUS (Pmode, stack_top,
11144                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
11145   emit_move_insn (mem, gen_rtx_REG (Pmode, 2));
11146   emit_label (no_toc_save_needed);
11147 }
11148 \f
11149 /* This ties together stack memory (MEM with an alias set of
11150    rs6000_sr_alias_set) and the change to the stack pointer.  */
11151
11152 static void
11153 rs6000_emit_stack_tie (void)
11154 {
11155   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
11156
11157   set_mem_alias_set (mem, rs6000_sr_alias_set);
11158   emit_insn (gen_stack_tie (mem));
11159 }
11160
11161 /* Emit the correct code for allocating stack space, as insns.
11162    If COPY_R12, make sure a copy of the old frame is left in r12.
11163    The generated code may use hard register 0 as a temporary.  */
11164
11165 static void
11166 rs6000_emit_allocate_stack (HOST_WIDE_INT size, int copy_r12)
11167 {
11168   rtx insn;
11169   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11170   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
11171   rtx todec = GEN_INT (-size);
11172
11173   if (current_function_limit_stack)
11174     {
11175       if (REG_P (stack_limit_rtx)
11176           && REGNO (stack_limit_rtx) > 1 
11177           && REGNO (stack_limit_rtx) <= 31)
11178         {
11179           emit_insn (TARGET_32BIT
11180                      ? gen_addsi3 (tmp_reg,
11181                                    stack_limit_rtx,
11182                                    GEN_INT (size))
11183                      : gen_adddi3 (tmp_reg,
11184                                    stack_limit_rtx,
11185                                    GEN_INT (size)));
11186
11187           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11188                                     const0_rtx));
11189         }
11190       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
11191                && TARGET_32BIT
11192                && DEFAULT_ABI == ABI_V4)
11193         {
11194           rtx toload = gen_rtx_CONST (VOIDmode,
11195                                       gen_rtx_PLUS (Pmode, 
11196                                                     stack_limit_rtx, 
11197                                                     GEN_INT (size)));
11198
11199           emit_insn (gen_elf_high (tmp_reg, toload));
11200           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
11201           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11202                                     const0_rtx));
11203         }
11204       else
11205         warning ("stack limit expression is not supported");
11206     }
11207
11208   if (copy_r12 || ! TARGET_UPDATE)
11209     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
11210
11211   if (TARGET_UPDATE)
11212     {
11213       if (size > 32767)
11214         {
11215           /* Need a note here so that try_split doesn't get confused.  */
11216           if (get_last_insn() == NULL_RTX)
11217             emit_note (NOTE_INSN_DELETED);
11218           insn = emit_move_insn (tmp_reg, todec);
11219           try_split (PATTERN (insn), insn, 0);
11220           todec = tmp_reg;
11221         }
11222
11223       insn = emit_insn (TARGET_32BIT
11224                         ? gen_movsi_update (stack_reg, stack_reg,
11225                                             todec, stack_reg)
11226                         : gen_movdi_update (stack_reg, stack_reg, 
11227                                             todec, stack_reg));
11228     }
11229   else
11230     {
11231       insn = emit_insn (TARGET_32BIT
11232                         ? gen_addsi3 (stack_reg, stack_reg, todec)
11233                         : gen_adddi3 (stack_reg, stack_reg, todec));
11234       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
11235                       gen_rtx_REG (Pmode, 12));
11236     }
11237  
11238   RTX_FRAME_RELATED_P (insn) = 1;
11239   REG_NOTES (insn) = 
11240     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11241                        gen_rtx_SET (VOIDmode, stack_reg, 
11242                                     gen_rtx_PLUS (Pmode, stack_reg,
11243                                                   GEN_INT (-size))),
11244                        REG_NOTES (insn));
11245 }
11246
11247 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
11248    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
11249    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
11250    deduce these equivalences by itself so it wasn't necessary to hold
11251    its hand so much.  */
11252
11253 static void
11254 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val, 
11255                       rtx reg2, rtx rreg)
11256 {
11257   rtx real, temp;
11258
11259   /* copy_rtx will not make unique copies of registers, so we need to
11260      ensure we don't have unwanted sharing here.  */
11261   if (reg == reg2)
11262     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11263
11264   if (reg == rreg)
11265     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11266
11267   real = copy_rtx (PATTERN (insn));
11268
11269   if (reg2 != NULL_RTX)
11270     real = replace_rtx (real, reg2, rreg);
11271   
11272   real = replace_rtx (real, reg, 
11273                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
11274                                                         STACK_POINTER_REGNUM),
11275                                     GEN_INT (val)));
11276   
11277   /* We expect that 'real' is either a SET or a PARALLEL containing
11278      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
11279      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
11280
11281   if (GET_CODE (real) == SET)
11282     {
11283       rtx set = real;
11284       
11285       temp = simplify_rtx (SET_SRC (set));
11286       if (temp)
11287         SET_SRC (set) = temp;
11288       temp = simplify_rtx (SET_DEST (set));
11289       if (temp)
11290         SET_DEST (set) = temp;
11291       if (GET_CODE (SET_DEST (set)) == MEM)
11292         {
11293           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11294           if (temp)
11295             XEXP (SET_DEST (set), 0) = temp;
11296         }
11297     }
11298   else if (GET_CODE (real) == PARALLEL)
11299     {
11300       int i;
11301       for (i = 0; i < XVECLEN (real, 0); i++)
11302         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
11303           {
11304             rtx set = XVECEXP (real, 0, i);
11305             
11306             temp = simplify_rtx (SET_SRC (set));
11307             if (temp)
11308               SET_SRC (set) = temp;
11309             temp = simplify_rtx (SET_DEST (set));
11310             if (temp)
11311               SET_DEST (set) = temp;
11312             if (GET_CODE (SET_DEST (set)) == MEM)
11313               {
11314                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11315                 if (temp)
11316                   XEXP (SET_DEST (set), 0) = temp;
11317               }
11318             RTX_FRAME_RELATED_P (set) = 1;
11319           }
11320     }
11321   else
11322     abort ();
11323
11324   if (TARGET_SPE)
11325     real = spe_synthesize_frame_save (real);
11326
11327   RTX_FRAME_RELATED_P (insn) = 1;
11328   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11329                                         real,
11330                                         REG_NOTES (insn));
11331 }
11332
11333 /* Given an SPE frame note, return a PARALLEL of SETs with the
11334    original note, plus a synthetic register save.  */
11335
11336 static rtx
11337 spe_synthesize_frame_save (rtx real)
11338 {
11339   rtx synth, offset, reg, real2;
11340
11341   if (GET_CODE (real) != SET
11342       || GET_MODE (SET_SRC (real)) != V2SImode)
11343     return real;
11344
11345   /* For the SPE, registers saved in 64-bits, get a PARALLEL for their
11346      frame related note.  The parallel contains a set of the register
11347      being saved, and another set to a synthetic register (n+1200).
11348      This is so we can differentiate between 64-bit and 32-bit saves.
11349      Words cannot describe this nastiness.  */
11350
11351   if (GET_CODE (SET_DEST (real)) != MEM
11352       || GET_CODE (XEXP (SET_DEST (real), 0)) != PLUS
11353       || GET_CODE (SET_SRC (real)) != REG)
11354     abort ();
11355
11356   /* Transform:
11357        (set (mem (plus (reg x) (const y)))
11358             (reg z))
11359      into:
11360        (set (mem (plus (reg x) (const y+4)))
11361             (reg z+1200))
11362   */
11363
11364   real2 = copy_rtx (real);
11365   PUT_MODE (SET_DEST (real2), SImode);
11366   reg = SET_SRC (real2);
11367   real2 = replace_rtx (real2, reg, gen_rtx_REG (SImode, REGNO (reg)));
11368   synth = copy_rtx (real2);
11369
11370   if (BYTES_BIG_ENDIAN)
11371     {
11372       offset = XEXP (XEXP (SET_DEST (real2), 0), 1);
11373       real2 = replace_rtx (real2, offset, GEN_INT (INTVAL (offset) + 4));
11374     }
11375
11376   reg = SET_SRC (synth);
11377
11378   synth = replace_rtx (synth, reg,
11379                        gen_rtx_REG (SImode, REGNO (reg) + 1200));
11380
11381   offset = XEXP (XEXP (SET_DEST (synth), 0), 1);
11382   synth = replace_rtx (synth, offset,
11383                        GEN_INT (INTVAL (offset)
11384                                 + (BYTES_BIG_ENDIAN ? 0 : 4)));
11385
11386   RTX_FRAME_RELATED_P (synth) = 1;
11387   RTX_FRAME_RELATED_P (real2) = 1;
11388   if (BYTES_BIG_ENDIAN)
11389     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, synth, real2));
11390   else
11391     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, real2, synth));
11392
11393   return real;
11394 }
11395
11396 /* Returns an insn that has a vrsave set operation with the
11397    appropriate CLOBBERs.  */
11398
11399 static rtx
11400 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
11401 {
11402   int nclobs, i;
11403   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
11404   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
11405
11406   clobs[0]
11407     = gen_rtx_SET (VOIDmode,
11408                    vrsave,
11409                    gen_rtx_UNSPEC_VOLATILE (SImode,
11410                                             gen_rtvec (2, reg, vrsave),
11411                                             30));
11412
11413   nclobs = 1;
11414
11415   /* We need to clobber the registers in the mask so the scheduler
11416      does not move sets to VRSAVE before sets of AltiVec registers.
11417
11418      However, if the function receives nonlocal gotos, reload will set
11419      all call saved registers live.  We will end up with:
11420
11421         (set (reg 999) (mem))
11422         (parallel [ (set (reg vrsave) (unspec blah))
11423                     (clobber (reg 999))])
11424
11425      The clobber will cause the store into reg 999 to be dead, and
11426      flow will attempt to delete an epilogue insn.  In this case, we
11427      need an unspec use/set of the register.  */
11428
11429   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
11430     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
11431       {
11432         if (!epiloguep || call_used_regs [i])
11433           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
11434                                              gen_rtx_REG (V4SImode, i));
11435         else
11436           {
11437             rtx reg = gen_rtx_REG (V4SImode, i);
11438
11439             clobs[nclobs++]
11440               = gen_rtx_SET (VOIDmode,
11441                              reg,
11442                              gen_rtx_UNSPEC (V4SImode,
11443                                              gen_rtvec (1, reg), 27));
11444           }
11445       }
11446
11447   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
11448
11449   for (i = 0; i < nclobs; ++i)
11450     XVECEXP (insn, 0, i) = clobs[i];
11451
11452   return insn;
11453 }
11454
11455 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
11456    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
11457
11458 static void
11459 emit_frame_save (rtx frame_reg, rtx frame_ptr, enum machine_mode mode, 
11460                  unsigned int regno, int offset, HOST_WIDE_INT total_size)
11461 {
11462   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
11463   rtx replacea, replaceb;
11464
11465   int_rtx = GEN_INT (offset);
11466
11467   /* Some cases that need register indexed addressing.  */
11468   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
11469       || (TARGET_SPE_ABI
11470           && SPE_VECTOR_MODE (mode)
11471           && !SPE_CONST_OFFSET_OK (offset)))
11472     {
11473       /* Whomever calls us must make sure r11 is available in the
11474          flow path of instructions in the prologue.  */
11475       offset_rtx = gen_rtx_REG (Pmode, 11);
11476       emit_move_insn (offset_rtx, int_rtx);
11477
11478       replacea = offset_rtx;
11479       replaceb = int_rtx;
11480     }
11481   else
11482     {
11483       offset_rtx = int_rtx;
11484       replacea = NULL_RTX;
11485       replaceb = NULL_RTX;
11486     }
11487
11488   reg = gen_rtx_REG (mode, regno);
11489   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
11490   mem = gen_rtx_MEM (mode, addr);
11491   set_mem_alias_set (mem, rs6000_sr_alias_set);
11492
11493   insn = emit_move_insn (mem, reg);
11494
11495   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
11496 }
11497
11498 /* Emit an offset memory reference suitable for a frame store, while
11499    converting to a valid addressing mode.  */
11500
11501 static rtx
11502 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
11503 {
11504   rtx int_rtx, offset_rtx;
11505
11506   int_rtx = GEN_INT (offset);
11507
11508   if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
11509     {
11510       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11511       emit_move_insn (offset_rtx, int_rtx);
11512     }
11513   else
11514     offset_rtx = int_rtx;
11515
11516   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
11517 }
11518
11519 /* Emit function prologue as insns.  */
11520
11521 void
11522 rs6000_emit_prologue (void)
11523 {
11524   rs6000_stack_t *info = rs6000_stack_info ();
11525   enum machine_mode reg_mode = Pmode;
11526   int reg_size = UNITS_PER_WORD;
11527   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11528   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
11529   rtx frame_reg_rtx = sp_reg_rtx;
11530   rtx cr_save_rtx = NULL_RTX;
11531   rtx insn;
11532   int saving_FPRs_inline;
11533   int using_store_multiple;
11534   HOST_WIDE_INT sp_offset = 0;
11535   
11536    if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11537      {
11538        reg_mode = V2SImode;
11539        reg_size = 8;
11540      }
11541
11542   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
11543                           && (!TARGET_SPE_ABI
11544                               || info->spe_64bit_regs_used == 0)
11545                           && info->first_gp_reg_save < 31);
11546   saving_FPRs_inline = (info->first_fp_reg_save == 64
11547                         || FP_SAVE_INLINE (info->first_fp_reg_save)
11548                         || current_function_calls_eh_return
11549                         || cfun->machine->ra_need_lr);
11550
11551   /* For V.4, update stack before we do any saving and set back pointer.  */
11552   if (info->push_p
11553       && (DEFAULT_ABI == ABI_V4
11554           || current_function_calls_eh_return))
11555     {
11556       if (info->total_size < 32767)
11557         sp_offset = info->total_size;
11558       else
11559         frame_reg_rtx = frame_ptr_rtx;
11560       rs6000_emit_allocate_stack (info->total_size, 
11561                                   (frame_reg_rtx != sp_reg_rtx
11562                                    && (info->cr_save_p
11563                                        || info->lr_save_p
11564                                        || info->first_fp_reg_save < 64
11565                                        || info->first_gp_reg_save < 32
11566                                        )));
11567       if (frame_reg_rtx != sp_reg_rtx)
11568         rs6000_emit_stack_tie ();
11569     }
11570
11571   /* Save AltiVec registers if needed.  */
11572   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
11573     {
11574       int i;
11575
11576       /* There should be a non inline version of this, for when we
11577          are saving lots of vector registers.  */
11578       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
11579         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
11580           {
11581             rtx areg, savereg, mem;
11582             int offset;
11583
11584             offset = info->altivec_save_offset + sp_offset
11585               + 16 * (i - info->first_altivec_reg_save);
11586
11587             savereg = gen_rtx_REG (V4SImode, i);
11588
11589             areg = gen_rtx_REG (Pmode, 0);
11590             emit_move_insn (areg, GEN_INT (offset));
11591
11592             /* AltiVec addressing mode is [reg+reg].  */
11593             mem = gen_rtx_MEM (V4SImode,
11594                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
11595                                
11596             set_mem_alias_set (mem, rs6000_sr_alias_set);
11597
11598             insn = emit_move_insn (mem, savereg);
11599
11600             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11601                                   areg, GEN_INT (offset));
11602           }
11603     }
11604
11605   /* VRSAVE is a bit vector representing which AltiVec registers
11606      are used.  The OS uses this to determine which vector
11607      registers to save on a context switch.  We need to save
11608      VRSAVE on the stack frame, add whatever AltiVec registers we
11609      used in this function, and do the corresponding magic in the
11610      epilogue.  */
11611
11612   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
11613     {
11614       rtx reg, mem, vrsave;
11615       int offset;
11616
11617       /* Get VRSAVE onto a GPR.  */
11618       reg = gen_rtx_REG (SImode, 12);
11619       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
11620       if (TARGET_MACHO)
11621         emit_insn (gen_get_vrsave_internal (reg));
11622       else
11623         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
11624
11625       /* Save VRSAVE.  */
11626       offset = info->vrsave_save_offset + sp_offset;
11627       mem
11628         = gen_rtx_MEM (SImode,
11629                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
11630       set_mem_alias_set (mem, rs6000_sr_alias_set);
11631       insn = emit_move_insn (mem, reg);
11632
11633       /* Include the registers in the mask.  */
11634       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
11635
11636       insn = emit_insn (generate_set_vrsave (reg, info, 0));
11637     }
11638
11639   /* If we use the link register, get it into r0.  */
11640   if (info->lr_save_p)
11641     emit_move_insn (gen_rtx_REG (Pmode, 0),
11642                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
11643
11644   /* If we need to save CR, put it into r12.  */
11645   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
11646     {
11647       cr_save_rtx = gen_rtx_REG (SImode, 12);
11648       emit_insn (gen_movesi_from_cr (cr_save_rtx));
11649     }
11650
11651   /* Do any required saving of fpr's.  If only one or two to save, do
11652      it ourselves.  Otherwise, call function.  */
11653   if (saving_FPRs_inline)
11654     {
11655       int i;
11656       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11657         if ((regs_ever_live[info->first_fp_reg_save+i] 
11658              && ! call_used_regs[info->first_fp_reg_save+i]))
11659           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
11660                            info->first_fp_reg_save + i,
11661                            info->fp_save_offset + sp_offset + 8 * i,
11662                            info->total_size);
11663     }
11664   else if (info->first_fp_reg_save != 64)
11665     {
11666       int i;
11667       char rname[30];
11668       const char *alloc_rname;
11669       rtvec p;
11670       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
11671       
11672       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
11673                                           gen_rtx_REG (Pmode, 
11674                                                        LINK_REGISTER_REGNUM));
11675       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
11676                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
11677       alloc_rname = ggc_strdup (rname);
11678       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
11679                                       gen_rtx_SYMBOL_REF (Pmode,
11680                                                           alloc_rname));
11681       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11682         {
11683           rtx addr, reg, mem;
11684           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
11685           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11686                                GEN_INT (info->fp_save_offset 
11687                                         + sp_offset + 8*i));
11688           mem = gen_rtx_MEM (DFmode, addr);
11689           set_mem_alias_set (mem, rs6000_sr_alias_set);
11690
11691           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
11692         }
11693       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11694       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11695                             NULL_RTX, NULL_RTX);
11696     }
11697
11698   /* Save GPRs.  This is done as a PARALLEL if we are using
11699      the store-multiple instructions.  */
11700   if (using_store_multiple)
11701     {
11702       rtvec p;
11703       int i;
11704       p = rtvec_alloc (32 - info->first_gp_reg_save);
11705       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11706         {
11707           rtx addr, reg, mem;
11708           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
11709           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11710                                GEN_INT (info->gp_save_offset 
11711                                         + sp_offset 
11712                                         + reg_size * i));
11713           mem = gen_rtx_MEM (reg_mode, addr);
11714           set_mem_alias_set (mem, rs6000_sr_alias_set);
11715
11716           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
11717         }
11718       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11719       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11720                             NULL_RTX, NULL_RTX);
11721     }
11722   else
11723     {
11724       int i;
11725       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11726         if ((regs_ever_live[info->first_gp_reg_save+i] 
11727              && ! call_used_regs[info->first_gp_reg_save+i])
11728             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
11729                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
11730                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
11731           {
11732             rtx addr, reg, mem;
11733             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
11734
11735             if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11736               {
11737                 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
11738                 rtx b;
11739
11740                 if (!SPE_CONST_OFFSET_OK (offset))
11741                   {
11742                     b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11743                     emit_move_insn (b, GEN_INT (offset));
11744                   }
11745                 else
11746                   b = GEN_INT (offset);
11747
11748                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
11749                 mem = gen_rtx_MEM (V2SImode, addr);
11750                 set_mem_alias_set (mem, rs6000_sr_alias_set);
11751                 insn = emit_move_insn (mem, reg);
11752
11753                 if (GET_CODE (b) == CONST_INT)
11754                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11755                                         NULL_RTX, NULL_RTX);
11756                 else
11757                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11758                                         b, GEN_INT (offset));
11759               }
11760             else
11761               {
11762                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11763                                      GEN_INT (info->gp_save_offset 
11764                                               + sp_offset 
11765                                               + reg_size * i));
11766                 mem = gen_rtx_MEM (reg_mode, addr);
11767                 set_mem_alias_set (mem, rs6000_sr_alias_set);
11768
11769                 insn = emit_move_insn (mem, reg);
11770                 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11771                                       NULL_RTX, NULL_RTX);
11772               }
11773           }
11774     }
11775
11776   /* ??? There's no need to emit actual instructions here, but it's the
11777      easiest way to get the frame unwind information emitted.  */
11778   if (current_function_calls_eh_return)
11779     {
11780       unsigned int i, regno;
11781
11782       /* In AIX ABI we need to pretend we save r2 here.  */
11783       if (TARGET_AIX)
11784         {
11785           rtx addr, reg, mem;
11786
11787           reg = gen_rtx_REG (reg_mode, 2);
11788           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11789                                GEN_INT (sp_offset + 5 * reg_size));
11790           mem = gen_rtx_MEM (reg_mode, addr);
11791           set_mem_alias_set (mem, rs6000_sr_alias_set);
11792
11793           insn = emit_move_insn (mem, reg);
11794           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11795                                 NULL_RTX, NULL_RTX);
11796           PATTERN (insn) = gen_blockage ();
11797         }
11798
11799       for (i = 0; ; ++i)
11800         {
11801           regno = EH_RETURN_DATA_REGNO (i);
11802           if (regno == INVALID_REGNUM)
11803             break;
11804
11805           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
11806                            info->ehrd_offset + sp_offset
11807                            + reg_size * (int) i,
11808                            info->total_size);
11809         }
11810     }
11811
11812   /* Save lr if we used it.  */
11813   if (info->lr_save_p)
11814     {
11815       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11816                                GEN_INT (info->lr_save_offset + sp_offset));
11817       rtx reg = gen_rtx_REG (Pmode, 0);
11818       rtx mem = gen_rtx_MEM (Pmode, addr);
11819       /* This should not be of rs6000_sr_alias_set, because of
11820          __builtin_return_address.  */
11821       
11822       insn = emit_move_insn (mem, reg);
11823       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11824                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
11825     }
11826
11827   /* Save CR if we use any that must be preserved.  */
11828   if (info->cr_save_p)
11829     {
11830       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11831                                GEN_INT (info->cr_save_offset + sp_offset));
11832       rtx mem = gen_rtx_MEM (SImode, addr);
11833
11834       set_mem_alias_set (mem, rs6000_sr_alias_set);
11835
11836       /* If r12 was used to hold the original sp, copy cr into r0 now
11837          that it's free.  */
11838       if (REGNO (frame_reg_rtx) == 12)
11839         {
11840           cr_save_rtx = gen_rtx_REG (SImode, 0);
11841           emit_insn (gen_movesi_from_cr (cr_save_rtx));
11842         }
11843       insn = emit_move_insn (mem, cr_save_rtx);
11844
11845       /* Now, there's no way that dwarf2out_frame_debug_expr is going
11846          to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
11847          But that's OK.  All we have to do is specify that _one_ condition
11848          code register is saved in this stack slot.  The thrower's epilogue
11849          will then restore all the call-saved registers.
11850          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
11851       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11852                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
11853     }
11854
11855   /* Update stack and set back pointer unless this is V.4, 
11856      for which it was done previously.  */
11857   if (info->push_p
11858       && !(DEFAULT_ABI == ABI_V4 || current_function_calls_eh_return))
11859     rs6000_emit_allocate_stack (info->total_size, FALSE);
11860
11861   /* Set frame pointer, if needed.  */
11862   if (frame_pointer_needed)
11863     {
11864       insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), 
11865                              sp_reg_rtx);
11866       RTX_FRAME_RELATED_P (insn) = 1;
11867     }
11868
11869   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
11870   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
11871       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
11872           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
11873   {
11874     /* If emit_load_toc_table will use the link register, we need to save
11875        it.  We use R12 for this purpose because emit_load_toc_table
11876        can use register 0.  This allows us to use a plain 'blr' to return
11877        from the procedure more often.  */
11878     int save_LR_around_toc_setup = (TARGET_ELF
11879                                     && DEFAULT_ABI != ABI_AIX
11880                                     && flag_pic
11881                                     && ! info->lr_save_p
11882                                     && EXIT_BLOCK_PTR->pred != NULL);
11883     if (save_LR_around_toc_setup)
11884       {
11885         rtx lr = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11886         rs6000_maybe_dead (emit_move_insn (frame_ptr_rtx, lr));
11887         rs6000_emit_load_toc_table (TRUE);
11888         rs6000_maybe_dead (emit_move_insn (lr, frame_ptr_rtx));
11889       }
11890     else
11891       rs6000_emit_load_toc_table (TRUE);
11892   }
11893
11894 #if TARGET_MACHO
11895   if (DEFAULT_ABI == ABI_DARWIN
11896       && flag_pic && current_function_uses_pic_offset_table)
11897     {
11898       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11899       const char *picbase = machopic_function_base_name ();
11900       rtx src = gen_rtx_SYMBOL_REF (Pmode, picbase);
11901
11902       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest, src)));
11903
11904       rs6000_maybe_dead (
11905         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
11906                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
11907     }
11908 #endif
11909 }
11910
11911 /* Write function prologue.  */
11912
11913 static void
11914 rs6000_output_function_prologue (FILE *file, 
11915                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11916 {
11917   rs6000_stack_t *info = rs6000_stack_info ();
11918
11919   if (TARGET_DEBUG_STACK)
11920     debug_stack_info (info);
11921
11922   /* Write .extern for any function we will call to save and restore
11923      fp values.  */
11924   if (info->first_fp_reg_save < 64
11925       && !FP_SAVE_INLINE (info->first_fp_reg_save))
11926     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
11927              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
11928              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
11929              RESTORE_FP_SUFFIX);
11930
11931   /* Write .extern for AIX common mode routines, if needed.  */
11932   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
11933     {
11934       fputs ("\t.extern __mulh\n", file);
11935       fputs ("\t.extern __mull\n", file);
11936       fputs ("\t.extern __divss\n", file);
11937       fputs ("\t.extern __divus\n", file);
11938       fputs ("\t.extern __quoss\n", file);
11939       fputs ("\t.extern __quous\n", file);
11940       common_mode_defined = 1;
11941     }
11942
11943   if (! HAVE_prologue)
11944     {
11945       start_sequence ();
11946
11947       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
11948          the "toplevel" insn chain.  */
11949       emit_note (NOTE_INSN_DELETED);
11950       rs6000_emit_prologue ();
11951       emit_note (NOTE_INSN_DELETED);
11952
11953       /* Expand INSN_ADDRESSES so final() doesn't crash. */
11954       {
11955         rtx insn;
11956         unsigned addr = 0;
11957         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
11958           {
11959             INSN_ADDRESSES_NEW (insn, addr);
11960             addr += 4;
11961           }
11962       }
11963
11964       if (TARGET_DEBUG_STACK)
11965         debug_rtx_list (get_insns (), 100);
11966       final (get_insns (), file, FALSE, FALSE);
11967       end_sequence ();
11968     }
11969
11970   rs6000_pic_labelno++;
11971 }
11972   
11973 /* Emit function epilogue as insns.
11974
11975    At present, dwarf2out_frame_debug_expr doesn't understand
11976    register restores, so we don't bother setting RTX_FRAME_RELATED_P
11977    anywhere in the epilogue.  Most of the insns below would in any case
11978    need special notes to explain where r11 is in relation to the stack.  */
11979
11980 void
11981 rs6000_emit_epilogue (int sibcall)
11982 {
11983   rs6000_stack_t *info;
11984   int restoring_FPRs_inline;
11985   int using_load_multiple;
11986   int using_mfcr_multiple;
11987   int use_backchain_to_restore_sp;
11988   int sp_offset = 0;
11989   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
11990   rtx frame_reg_rtx = sp_reg_rtx;
11991   enum machine_mode reg_mode = Pmode;
11992   int reg_size = UNITS_PER_WORD;
11993   int i;
11994
11995   info = rs6000_stack_info ();
11996
11997   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11998     {
11999       reg_mode = V2SImode;
12000       reg_size = 8;
12001     }
12002
12003   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
12004                          && (!TARGET_SPE_ABI
12005                              || info->spe_64bit_regs_used == 0)
12006                          && info->first_gp_reg_save < 31);
12007   restoring_FPRs_inline = (sibcall
12008                            || current_function_calls_eh_return
12009                            || info->first_fp_reg_save == 64
12010                            || FP_SAVE_INLINE (info->first_fp_reg_save));
12011   use_backchain_to_restore_sp = (frame_pointer_needed 
12012                                  || current_function_calls_alloca
12013                                  || info->total_size > 32767);
12014   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
12015                          || rs6000_cpu == PROCESSOR_PPC603
12016                          || rs6000_cpu == PROCESSOR_PPC750
12017                          || optimize_size);
12018
12019   /* If we have a frame pointer, a call to alloca,  or a large stack
12020      frame, restore the old stack pointer using the backchain.  Otherwise,
12021      we know what size to update it with.  */
12022   if (use_backchain_to_restore_sp)
12023     {
12024       /* Under V.4, don't reset the stack pointer until after we're done
12025          loading the saved registers.  */
12026       if (DEFAULT_ABI == ABI_V4)
12027         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
12028
12029       emit_move_insn (frame_reg_rtx,
12030                       gen_rtx_MEM (Pmode, sp_reg_rtx));
12031       
12032     }
12033   else if (info->push_p)
12034     {
12035       if (DEFAULT_ABI == ABI_V4
12036           || current_function_calls_eh_return)
12037         sp_offset = info->total_size;
12038       else
12039         {
12040           emit_insn (TARGET_32BIT
12041                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12042                                    GEN_INT (info->total_size))
12043                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12044                                    GEN_INT (info->total_size)));
12045         }
12046     }
12047   
12048   /* Restore AltiVec registers if needed.  */
12049   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
12050     {
12051       int i;
12052
12053       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
12054         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
12055           {
12056             rtx addr, areg, mem;
12057
12058             areg = gen_rtx_REG (Pmode, 0);
12059             emit_move_insn
12060               (areg, GEN_INT (info->altivec_save_offset
12061                               + sp_offset
12062                               + 16 * (i - info->first_altivec_reg_save)));
12063
12064             /* AltiVec addressing mode is [reg+reg].  */
12065             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
12066             mem = gen_rtx_MEM (V4SImode, addr);
12067             set_mem_alias_set (mem, rs6000_sr_alias_set);
12068
12069             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
12070           }
12071     }
12072
12073   /* Restore VRSAVE if needed.  */
12074   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
12075     {
12076       rtx addr, mem, reg;
12077
12078       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12079                            GEN_INT (info->vrsave_save_offset + sp_offset));
12080       mem = gen_rtx_MEM (SImode, addr);
12081       set_mem_alias_set (mem, rs6000_sr_alias_set);
12082       reg = gen_rtx_REG (SImode, 12);
12083       emit_move_insn (reg, mem);
12084
12085       emit_insn (generate_set_vrsave (reg, info, 1));
12086     }
12087
12088   /* Get the old lr if we saved it.  */
12089   if (info->lr_save_p)
12090     {
12091       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
12092                                       info->lr_save_offset + sp_offset);
12093
12094       set_mem_alias_set (mem, rs6000_sr_alias_set);
12095
12096       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
12097     }
12098   
12099   /* Get the old cr if we saved it.  */
12100   if (info->cr_save_p)
12101     {
12102       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12103                                GEN_INT (info->cr_save_offset + sp_offset));
12104       rtx mem = gen_rtx_MEM (SImode, addr);
12105
12106       set_mem_alias_set (mem, rs6000_sr_alias_set);
12107
12108       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
12109     }
12110   
12111   /* Set LR here to try to overlap restores below.  */
12112   if (info->lr_save_p)
12113     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
12114                     gen_rtx_REG (Pmode, 0));
12115   
12116   /* Load exception handler data registers, if needed.  */
12117   if (current_function_calls_eh_return)
12118     {
12119       unsigned int i, regno;
12120
12121       if (TARGET_AIX)
12122         {
12123           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12124                                    GEN_INT (sp_offset + 5 * reg_size));
12125           rtx mem = gen_rtx_MEM (reg_mode, addr);
12126
12127           set_mem_alias_set (mem, rs6000_sr_alias_set);
12128
12129           emit_move_insn (gen_rtx_REG (reg_mode, 2), mem);
12130         }
12131
12132       for (i = 0; ; ++i)
12133         {
12134           rtx mem;
12135
12136           regno = EH_RETURN_DATA_REGNO (i);
12137           if (regno == INVALID_REGNUM)
12138             break;
12139
12140           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
12141                                       info->ehrd_offset + sp_offset
12142                                       + reg_size * (int) i);
12143           set_mem_alias_set (mem, rs6000_sr_alias_set);
12144
12145           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
12146         }
12147     }
12148   
12149   /* Restore GPRs.  This is done as a PARALLEL if we are using
12150      the load-multiple instructions.  */
12151   if (using_load_multiple)
12152     {
12153       rtvec p;
12154       p = rtvec_alloc (32 - info->first_gp_reg_save);
12155       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12156         {
12157           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12158                                    GEN_INT (info->gp_save_offset 
12159                                             + sp_offset 
12160                                             + reg_size * i));
12161           rtx mem = gen_rtx_MEM (reg_mode, addr);
12162
12163           set_mem_alias_set (mem, rs6000_sr_alias_set);
12164
12165           RTVEC_ELT (p, i) = 
12166             gen_rtx_SET (VOIDmode,
12167                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
12168                          mem);
12169         }
12170       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12171     }
12172   else
12173     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12174       if ((regs_ever_live[info->first_gp_reg_save+i] 
12175            && ! call_used_regs[info->first_gp_reg_save+i])
12176           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
12177               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
12178                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
12179         {
12180           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12181                                    GEN_INT (info->gp_save_offset 
12182                                             + sp_offset 
12183                                             + reg_size * i));
12184           rtx mem = gen_rtx_MEM (reg_mode, addr);
12185
12186           /* Restore 64-bit quantities for SPE.  */
12187           if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
12188             {
12189               int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
12190               rtx b;
12191
12192               if (!SPE_CONST_OFFSET_OK (offset))
12193                 {
12194                   b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12195                   emit_move_insn (b, GEN_INT (offset));
12196                 }
12197               else
12198                 b = GEN_INT (offset);
12199
12200               addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
12201               mem = gen_rtx_MEM (V2SImode, addr);
12202             }
12203
12204           set_mem_alias_set (mem, rs6000_sr_alias_set);
12205
12206           emit_move_insn (gen_rtx_REG (reg_mode, 
12207                                        info->first_gp_reg_save + i), mem);
12208         }
12209
12210   /* Restore fpr's if we need to do it without calling a function.  */
12211   if (restoring_FPRs_inline)
12212     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12213       if ((regs_ever_live[info->first_fp_reg_save+i] 
12214            && ! call_used_regs[info->first_fp_reg_save+i]))
12215         {
12216           rtx addr, mem;
12217           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12218                                GEN_INT (info->fp_save_offset 
12219                                         + sp_offset 
12220                                         + 8 * i));
12221           mem = gen_rtx_MEM (DFmode, addr);
12222           set_mem_alias_set (mem, rs6000_sr_alias_set);
12223
12224           emit_move_insn (gen_rtx_REG (DFmode, 
12225                                        info->first_fp_reg_save + i),
12226                           mem);
12227         }
12228
12229   /* If we saved cr, restore it here.  Just those that were used.  */
12230   if (info->cr_save_p)
12231     {
12232       rtx r12_rtx = gen_rtx_REG (SImode, 12);
12233       int count = 0;
12234       
12235       if (using_mfcr_multiple)
12236         {
12237           for (i = 0; i < 8; i++)
12238             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12239               count++;
12240           if (count == 0)
12241             abort ();
12242         }
12243
12244       if (using_mfcr_multiple && count > 1)
12245         {
12246           rtvec p;
12247           int ndx;
12248           
12249           p = rtvec_alloc (count);
12250
12251           ndx = 0;
12252           for (i = 0; i < 8; i++)
12253             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12254               {
12255                 rtvec r = rtvec_alloc (2);
12256                 RTVEC_ELT (r, 0) = r12_rtx;
12257                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
12258                 RTVEC_ELT (p, ndx) =
12259                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
12260                                gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
12261                 ndx++;
12262               }
12263           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12264           if (ndx != count)
12265             abort ();
12266         }
12267       else
12268         for (i = 0; i < 8; i++)
12269           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12270             {
12271               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
12272                                                            CR0_REGNO+i),
12273                                               r12_rtx));
12274             }
12275     }
12276
12277   /* If this is V.4, unwind the stack pointer after all of the loads
12278      have been done.  We need to emit a block here so that sched
12279      doesn't decide to move the sp change before the register restores
12280      (which may not have any obvious dependency on the stack).  This
12281      doesn't hurt performance, because there is no scheduling that can
12282      be done after this point.  */
12283   if (DEFAULT_ABI == ABI_V4
12284       || current_function_calls_eh_return)
12285     {
12286       if (frame_reg_rtx != sp_reg_rtx)
12287           rs6000_emit_stack_tie ();
12288
12289       if (use_backchain_to_restore_sp)
12290         {
12291           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
12292         }
12293       else if (sp_offset != 0)
12294         {
12295           emit_insn (TARGET_32BIT
12296                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12297                                    GEN_INT (sp_offset))
12298                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12299                                    GEN_INT (sp_offset)));
12300         }
12301     }
12302
12303   if (current_function_calls_eh_return)
12304     {
12305       rtx sa = EH_RETURN_STACKADJ_RTX;
12306       emit_insn (TARGET_32BIT
12307                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
12308                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
12309     }
12310
12311   if (!sibcall)
12312     {
12313       rtvec p;
12314       if (! restoring_FPRs_inline)
12315         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
12316       else
12317         p = rtvec_alloc (2);
12318
12319       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
12320       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
12321                                       gen_rtx_REG (Pmode, 
12322                                                    LINK_REGISTER_REGNUM));
12323
12324       /* If we have to restore more than two FP registers, branch to the
12325          restore function.  It will return to our caller.  */
12326       if (! restoring_FPRs_inline)
12327         {
12328           int i;
12329           char rname[30];
12330           const char *alloc_rname;
12331
12332           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
12333                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
12334           alloc_rname = ggc_strdup (rname);
12335           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
12336                                           gen_rtx_SYMBOL_REF (Pmode,
12337                                                               alloc_rname));
12338
12339           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12340             {
12341               rtx addr, mem;
12342               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
12343                                    GEN_INT (info->fp_save_offset + 8*i));
12344               mem = gen_rtx_MEM (DFmode, addr);
12345               set_mem_alias_set (mem, rs6000_sr_alias_set);
12346
12347               RTVEC_ELT (p, i+3) = 
12348                 gen_rtx_SET (VOIDmode,
12349                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
12350                              mem);
12351             }
12352         }
12353       
12354       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
12355     }
12356 }
12357
12358 /* Write function epilogue.  */
12359
12360 static void
12361 rs6000_output_function_epilogue (FILE *file, 
12362                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
12363 {
12364   rs6000_stack_t *info = rs6000_stack_info ();
12365
12366   if (! HAVE_epilogue)
12367     {
12368       rtx insn = get_last_insn ();
12369       /* If the last insn was a BARRIER, we don't have to write anything except
12370          the trace table.  */
12371       if (GET_CODE (insn) == NOTE)
12372         insn = prev_nonnote_insn (insn);
12373       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
12374         {
12375           /* This is slightly ugly, but at least we don't have two
12376              copies of the epilogue-emitting code.  */
12377           start_sequence ();
12378
12379           /* A NOTE_INSN_DELETED is supposed to be at the start
12380              and end of the "toplevel" insn chain.  */
12381           emit_note (NOTE_INSN_DELETED);
12382           rs6000_emit_epilogue (FALSE);
12383           emit_note (NOTE_INSN_DELETED);
12384
12385           /* Expand INSN_ADDRESSES so final() doesn't crash. */
12386           {
12387             rtx insn;
12388             unsigned addr = 0;
12389             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
12390               {
12391                 INSN_ADDRESSES_NEW (insn, addr);
12392                 addr += 4;
12393               }
12394           }
12395
12396           if (TARGET_DEBUG_STACK)
12397             debug_rtx_list (get_insns (), 100);
12398           final (get_insns (), file, FALSE, FALSE);
12399           end_sequence ();
12400         }
12401     }
12402
12403 #if TARGET_OBJECT_FORMAT == OBJECT_MACHO
12404   /* Mach-O doesn't support labels at the end of objects, so if
12405      it looks like we might want one, insert a NOP.  */
12406   {
12407     rtx insn = get_last_insn ();
12408     while (insn
12409            && NOTE_P (insn)
12410            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
12411       insn = PREV_INSN (insn);
12412     if (insn 
12413         && (LABEL_P (insn) 
12414             || (NOTE_P (insn)
12415                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
12416       fputs ("\tnop\n", file);
12417   }
12418 #endif
12419
12420   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
12421      on its format.
12422
12423      We don't output a traceback table if -finhibit-size-directive was
12424      used.  The documentation for -finhibit-size-directive reads
12425      ``don't output a @code{.size} assembler directive, or anything
12426      else that would cause trouble if the function is split in the
12427      middle, and the two halves are placed at locations far apart in
12428      memory.''  The traceback table has this property, since it
12429      includes the offset from the start of the function to the
12430      traceback table itself.
12431
12432      System V.4 Powerpc's (and the embedded ABI derived from it) use a
12433      different traceback table.  */
12434   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
12435       && rs6000_traceback != traceback_none)
12436     {
12437       const char *fname = NULL;
12438       const char *language_string = lang_hooks.name;
12439       int fixed_parms = 0, float_parms = 0, parm_info = 0;
12440       int i;
12441       int optional_tbtab;
12442
12443       if (rs6000_traceback == traceback_full)
12444         optional_tbtab = 1;
12445       else if (rs6000_traceback == traceback_part)
12446         optional_tbtab = 0;
12447       else
12448         optional_tbtab = !optimize_size && !TARGET_ELF;
12449
12450       if (optional_tbtab)
12451         {
12452           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
12453           while (*fname == '.') /* V.4 encodes . in the name */
12454             fname++;
12455
12456           /* Need label immediately before tbtab, so we can compute
12457              its offset from the function start.  */
12458           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12459           ASM_OUTPUT_LABEL (file, fname);
12460         }
12461
12462       /* The .tbtab pseudo-op can only be used for the first eight
12463          expressions, since it can't handle the possibly variable
12464          length fields that follow.  However, if you omit the optional
12465          fields, the assembler outputs zeros for all optional fields
12466          anyways, giving each variable length field is minimum length
12467          (as defined in sys/debug.h).  Thus we can not use the .tbtab
12468          pseudo-op at all.  */
12469
12470       /* An all-zero word flags the start of the tbtab, for debuggers
12471          that have to find it by searching forward from the entry
12472          point or from the current pc.  */
12473       fputs ("\t.long 0\n", file);
12474
12475       /* Tbtab format type.  Use format type 0.  */
12476       fputs ("\t.byte 0,", file);
12477
12478       /* Language type.  Unfortunately, there doesn't seem to be any
12479          official way to get this info, so we use language_string.  C
12480          is 0.  C++ is 9.  No number defined for Obj-C, so use the
12481          value for C for now.  There is no official value for Java,
12482          although IBM appears to be using 13.  There is no official value
12483          for Chill, so we've chosen 44 pseudo-randomly.  */
12484       if (! strcmp (language_string, "GNU C")
12485           || ! strcmp (language_string, "GNU Objective-C"))
12486         i = 0;
12487       else if (! strcmp (language_string, "GNU F77"))
12488         i = 1;
12489       else if (! strcmp (language_string, "GNU Ada"))
12490         i = 3;
12491       else if (! strcmp (language_string, "GNU Pascal"))
12492         i = 2;
12493       else if (! strcmp (language_string, "GNU C++"))
12494         i = 9;
12495       else if (! strcmp (language_string, "GNU Java"))
12496         i = 13;
12497       else if (! strcmp (language_string, "GNU CHILL"))
12498         i = 44;
12499       else
12500         abort ();
12501       fprintf (file, "%d,", i);
12502
12503       /* 8 single bit fields: global linkage (not set for C extern linkage,
12504          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
12505          from start of procedure stored in tbtab, internal function, function
12506          has controlled storage, function has no toc, function uses fp,
12507          function logs/aborts fp operations.  */
12508       /* Assume that fp operations are used if any fp reg must be saved.  */
12509       fprintf (file, "%d,",
12510                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
12511
12512       /* 6 bitfields: function is interrupt handler, name present in
12513          proc table, function calls alloca, on condition directives
12514          (controls stack walks, 3 bits), saves condition reg, saves
12515          link reg.  */
12516       /* The `function calls alloca' bit seems to be set whenever reg 31 is
12517          set up as a frame pointer, even when there is no alloca call.  */
12518       fprintf (file, "%d,",
12519                ((optional_tbtab << 6)
12520                 | ((optional_tbtab & frame_pointer_needed) << 5)
12521                 | (info->cr_save_p << 1)
12522                 | (info->lr_save_p)));
12523
12524       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
12525          (6 bits).  */
12526       fprintf (file, "%d,",
12527                (info->push_p << 7) | (64 - info->first_fp_reg_save));
12528
12529       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
12530       fprintf (file, "%d,", (32 - first_reg_to_save ()));
12531
12532       if (optional_tbtab)
12533         {
12534           /* Compute the parameter info from the function decl argument
12535              list.  */
12536           tree decl;
12537           int next_parm_info_bit = 31;
12538
12539           for (decl = DECL_ARGUMENTS (current_function_decl);
12540                decl; decl = TREE_CHAIN (decl))
12541             {
12542               rtx parameter = DECL_INCOMING_RTL (decl);
12543               enum machine_mode mode = GET_MODE (parameter);
12544
12545               if (GET_CODE (parameter) == REG)
12546                 {
12547                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
12548                     {
12549                       int bits;
12550
12551                       float_parms++;
12552
12553                       if (mode == SFmode)
12554                         bits = 0x2;
12555                       else if (mode == DFmode || mode == TFmode)
12556                         bits = 0x3;
12557                       else
12558                         abort ();
12559
12560                       /* If only one bit will fit, don't or in this entry.  */
12561                       if (next_parm_info_bit > 0)
12562                         parm_info |= (bits << (next_parm_info_bit - 1));
12563                       next_parm_info_bit -= 2;
12564                     }
12565                   else
12566                     {
12567                       fixed_parms += ((GET_MODE_SIZE (mode)
12568                                        + (UNITS_PER_WORD - 1))
12569                                       / UNITS_PER_WORD);
12570                       next_parm_info_bit -= 1;
12571                     }
12572                 }
12573             }
12574         }
12575
12576       /* Number of fixed point parameters.  */
12577       /* This is actually the number of words of fixed point parameters; thus
12578          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
12579       fprintf (file, "%d,", fixed_parms);
12580
12581       /* 2 bitfields: number of floating point parameters (7 bits), parameters
12582          all on stack.  */
12583       /* This is actually the number of fp registers that hold parameters;
12584          and thus the maximum value is 13.  */
12585       /* Set parameters on stack bit if parameters are not in their original
12586          registers, regardless of whether they are on the stack?  Xlc
12587          seems to set the bit when not optimizing.  */
12588       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
12589
12590       if (! optional_tbtab)
12591         return;
12592
12593       /* Optional fields follow.  Some are variable length.  */
12594
12595       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
12596          11 double float.  */
12597       /* There is an entry for each parameter in a register, in the order that
12598          they occur in the parameter list.  Any intervening arguments on the
12599          stack are ignored.  If the list overflows a long (max possible length
12600          34 bits) then completely leave off all elements that don't fit.  */
12601       /* Only emit this long if there was at least one parameter.  */
12602       if (fixed_parms || float_parms)
12603         fprintf (file, "\t.long %d\n", parm_info);
12604
12605       /* Offset from start of code to tb table.  */
12606       fputs ("\t.long ", file);
12607       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12608 #if TARGET_AIX
12609       RS6000_OUTPUT_BASENAME (file, fname);
12610 #else
12611       assemble_name (file, fname);
12612 #endif
12613       fputs ("-.", file);
12614 #if TARGET_AIX
12615       RS6000_OUTPUT_BASENAME (file, fname);
12616 #else
12617       assemble_name (file, fname);
12618 #endif
12619       putc ('\n', file);
12620
12621       /* Interrupt handler mask.  */
12622       /* Omit this long, since we never set the interrupt handler bit
12623          above.  */
12624
12625       /* Number of CTL (controlled storage) anchors.  */
12626       /* Omit this long, since the has_ctl bit is never set above.  */
12627
12628       /* Displacement into stack of each CTL anchor.  */
12629       /* Omit this list of longs, because there are no CTL anchors.  */
12630
12631       /* Length of function name.  */
12632       if (*fname == '*')
12633         ++fname;
12634       fprintf (file, "\t.short %d\n", (int) strlen (fname));
12635
12636       /* Function name.  */
12637       assemble_string (fname, strlen (fname));
12638
12639       /* Register for alloca automatic storage; this is always reg 31.
12640          Only emit this if the alloca bit was set above.  */
12641       if (frame_pointer_needed)
12642         fputs ("\t.byte 31\n", file);
12643
12644       fputs ("\t.align 2\n", file);
12645     }
12646 }
12647 \f
12648 /* A C compound statement that outputs the assembler code for a thunk
12649    function, used to implement C++ virtual function calls with
12650    multiple inheritance.  The thunk acts as a wrapper around a virtual
12651    function, adjusting the implicit object parameter before handing
12652    control off to the real function.
12653
12654    First, emit code to add the integer DELTA to the location that
12655    contains the incoming first argument.  Assume that this argument
12656    contains a pointer, and is the one used to pass the `this' pointer
12657    in C++.  This is the incoming argument *before* the function
12658    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
12659    values of all other incoming arguments.
12660
12661    After the addition, emit code to jump to FUNCTION, which is a
12662    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
12663    not touch the return address.  Hence returning from FUNCTION will
12664    return to whoever called the current `thunk'.
12665
12666    The effect must be as if FUNCTION had been called directly with the
12667    adjusted first argument.  This macro is responsible for emitting
12668    all of the code for a thunk function; output_function_prologue()
12669    and output_function_epilogue() are not invoked.
12670
12671    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
12672    been extracted from it.)  It might possibly be useful on some
12673    targets, but probably not.
12674
12675    If you do not define this macro, the target-independent code in the
12676    C++ frontend will generate a less efficient heavyweight thunk that
12677    calls FUNCTION instead of jumping to it.  The generic approach does
12678    not support varargs.  */
12679
12680 static void
12681 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED, 
12682                         HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset, 
12683                         tree function)
12684 {
12685   rtx this, insn, funexp;
12686
12687   reload_completed = 1;
12688   epilogue_completed = 1;
12689   no_new_pseudos = 1;
12690
12691   /* Mark the end of the (empty) prologue.  */
12692   emit_note (NOTE_INSN_PROLOGUE_END);
12693
12694   /* Find the "this" pointer.  If the function returns a structure,
12695      the structure return pointer is in r3.  */
12696   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
12697     this = gen_rtx_REG (Pmode, 4);
12698   else
12699     this = gen_rtx_REG (Pmode, 3);
12700
12701   /* Apply the constant offset, if required.  */
12702   if (delta)
12703     {
12704       rtx delta_rtx = GEN_INT (delta);
12705       emit_insn (TARGET_32BIT
12706                  ? gen_addsi3 (this, this, delta_rtx)
12707                  : gen_adddi3 (this, this, delta_rtx));
12708     }
12709
12710   /* Apply the offset from the vtable, if required.  */
12711   if (vcall_offset)
12712     {
12713       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
12714       rtx tmp = gen_rtx_REG (Pmode, 12);
12715
12716       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
12717       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
12718         {
12719           emit_insn (TARGET_32BIT
12720                      ? gen_addsi3 (tmp, tmp, vcall_offset_rtx)
12721                      : gen_adddi3 (tmp, tmp, vcall_offset_rtx));
12722           emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
12723         }
12724       else
12725         {
12726           rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
12727
12728           emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
12729         }
12730       emit_insn (TARGET_32BIT
12731                  ? gen_addsi3 (this, this, tmp)
12732                  : gen_adddi3 (this, this, tmp));
12733     }
12734
12735   /* Generate a tail call to the target function.  */
12736   if (!TREE_USED (function))
12737     {
12738       assemble_external (function);
12739       TREE_USED (function) = 1;
12740     }
12741   funexp = XEXP (DECL_RTL (function), 0);
12742   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
12743
12744 #if TARGET_MACHO
12745   if (MACHOPIC_INDIRECT)
12746     funexp = machopic_indirect_call_target (funexp);
12747 #endif
12748
12749   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
12750      generate sibcall RTL explicitly to avoid constraint abort.  */
12751   insn = emit_call_insn (
12752            gen_rtx_PARALLEL (VOIDmode,
12753              gen_rtvec (4,
12754                         gen_rtx_CALL (VOIDmode,
12755                                       funexp, const0_rtx),
12756                         gen_rtx_USE (VOIDmode, const0_rtx),
12757                         gen_rtx_USE (VOIDmode,
12758                                      gen_rtx_REG (SImode,
12759                                                   LINK_REGISTER_REGNUM)),
12760                         gen_rtx_RETURN (VOIDmode))));
12761   SIBLING_CALL_P (insn) = 1;
12762   emit_barrier ();
12763
12764   /* Run just enough of rest_of_compilation to get the insns emitted.
12765      There's not really enough bulk here to make other passes such as
12766      instruction scheduling worth while.  Note that use_thunk calls
12767      assemble_start_function and assemble_end_function.  */
12768   insn = get_insns ();
12769   insn_locators_initialize ();
12770   shorten_branches (insn);
12771   final_start_function (insn, file, 1);
12772   final (insn, file, 1, 0);
12773   final_end_function ();
12774
12775   reload_completed = 0;
12776   epilogue_completed = 0;
12777   no_new_pseudos = 0;
12778 }
12779 \f
12780 /* A quick summary of the various types of 'constant-pool tables'
12781    under PowerPC:
12782
12783    Target       Flags           Name            One table per   
12784    AIX          (none)          AIX TOC         object file
12785    AIX          -mfull-toc      AIX TOC         object file
12786    AIX          -mminimal-toc   AIX minimal TOC translation unit
12787    SVR4/EABI    (none)          SVR4 SDATA      object file
12788    SVR4/EABI    -fpic           SVR4 pic        object file
12789    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
12790    SVR4/EABI    -mrelocatable   EABI TOC        function
12791    SVR4/EABI    -maix           AIX TOC         object file
12792    SVR4/EABI    -maix -mminimal-toc 
12793                                 AIX minimal TOC translation unit
12794
12795    Name                 Reg.    Set by  entries       contains:
12796                                         made by  addrs? fp?     sum?
12797
12798    AIX TOC              2       crt0    as       Y      option  option
12799    AIX minimal TOC      30      prolog  gcc      Y      Y       option
12800    SVR4 SDATA           13      crt0    gcc      N      Y       N
12801    SVR4 pic             30      prolog  ld       Y      not yet N
12802    SVR4 PIC             30      prolog  gcc      Y      option  option
12803    EABI TOC             30      prolog  gcc      Y      option  option
12804
12805 */
12806
12807 /* Hash functions for the hash table.  */
12808
12809 static unsigned
12810 rs6000_hash_constant (rtx k)
12811 {
12812   enum rtx_code code = GET_CODE (k);
12813   enum machine_mode mode = GET_MODE (k);
12814   unsigned result = (code << 3) ^ mode;
12815   const char *format;
12816   int flen, fidx;
12817   
12818   format = GET_RTX_FORMAT (code);
12819   flen = strlen (format);
12820   fidx = 0;
12821
12822   switch (code)
12823     {
12824     case LABEL_REF:
12825       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
12826
12827     case CONST_DOUBLE:
12828       if (mode != VOIDmode)
12829         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
12830       flen = 2;
12831       break;
12832
12833     case CODE_LABEL:
12834       fidx = 3;
12835       break;
12836
12837     default:
12838       break;
12839     }
12840
12841   for (; fidx < flen; fidx++)
12842     switch (format[fidx])
12843       {
12844       case 's':
12845         {
12846           unsigned i, len;
12847           const char *str = XSTR (k, fidx);
12848           len = strlen (str);
12849           result = result * 613 + len;
12850           for (i = 0; i < len; i++)
12851             result = result * 613 + (unsigned) str[i];
12852           break;
12853         }
12854       case 'u':
12855       case 'e':
12856         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
12857         break;
12858       case 'i':
12859       case 'n':
12860         result = result * 613 + (unsigned) XINT (k, fidx);
12861         break;
12862       case 'w':
12863         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
12864           result = result * 613 + (unsigned) XWINT (k, fidx);
12865         else
12866           {
12867             size_t i;
12868             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
12869               result = result * 613 + (unsigned) (XWINT (k, fidx)
12870                                                   >> CHAR_BIT * i);
12871           }
12872         break;
12873       case '0':
12874         break;
12875       default:
12876         abort ();
12877       }
12878
12879   return result;
12880 }
12881
12882 static unsigned
12883 toc_hash_function (const void *hash_entry)
12884 {
12885   const struct toc_hash_struct *thc = 
12886     (const struct toc_hash_struct *) hash_entry;
12887   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
12888 }
12889
12890 /* Compare H1 and H2 for equivalence.  */
12891
12892 static int
12893 toc_hash_eq (const void *h1, const void *h2)
12894 {
12895   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
12896   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
12897
12898   if (((const struct toc_hash_struct *) h1)->key_mode
12899       != ((const struct toc_hash_struct *) h2)->key_mode)
12900     return 0;
12901
12902   return rtx_equal_p (r1, r2);
12903 }
12904
12905 /* These are the names given by the C++ front-end to vtables, and
12906    vtable-like objects.  Ideally, this logic should not be here;
12907    instead, there should be some programmatic way of inquiring as
12908    to whether or not an object is a vtable.  */
12909
12910 #define VTABLE_NAME_P(NAME)                             \
12911   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
12912   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
12913   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
12914   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
12915
12916 void
12917 rs6000_output_symbol_ref (FILE *file, rtx x)
12918 {
12919   /* Currently C++ toc references to vtables can be emitted before it
12920      is decided whether the vtable is public or private.  If this is
12921      the case, then the linker will eventually complain that there is
12922      a reference to an unknown section.  Thus, for vtables only, 
12923      we emit the TOC reference to reference the symbol and not the
12924      section.  */
12925   const char *name = XSTR (x, 0);
12926
12927   if (VTABLE_NAME_P (name)) 
12928     {
12929       RS6000_OUTPUT_BASENAME (file, name);
12930     }
12931   else
12932     assemble_name (file, name);
12933 }
12934
12935 /* Output a TOC entry.  We derive the entry name from what is being
12936    written.  */
12937
12938 void
12939 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
12940 {
12941   char buf[256];
12942   const char *name = buf;
12943   const char *real_name;
12944   rtx base = x;
12945   int offset = 0;
12946
12947   if (TARGET_NO_TOC)
12948     abort ();
12949
12950   /* When the linker won't eliminate them, don't output duplicate
12951      TOC entries (this happens on AIX if there is any kind of TOC,
12952      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
12953      CODE_LABELs.  */
12954   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
12955     {
12956       struct toc_hash_struct *h;
12957       void * * found;
12958       
12959       /* Create toc_hash_table.  This can't be done at OVERRIDE_OPTIONS
12960          time because GGC is not initialized at that point.  */
12961       if (toc_hash_table == NULL)
12962         toc_hash_table = htab_create_ggc (1021, toc_hash_function, 
12963                                           toc_hash_eq, NULL);
12964
12965       h = ggc_alloc (sizeof (*h));
12966       h->key = x;
12967       h->key_mode = mode;
12968       h->labelno = labelno;
12969       
12970       found = htab_find_slot (toc_hash_table, h, 1);
12971       if (*found == NULL)
12972         *found = h;
12973       else  /* This is indeed a duplicate.  
12974                Set this label equal to that label.  */
12975         {
12976           fputs ("\t.set ", file);
12977           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
12978           fprintf (file, "%d,", labelno);
12979           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
12980           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
12981                                               found)->labelno));
12982           return;
12983         }
12984     }
12985
12986   /* If we're going to put a double constant in the TOC, make sure it's
12987      aligned properly when strict alignment is on.  */
12988   if (GET_CODE (x) == CONST_DOUBLE
12989       && STRICT_ALIGNMENT
12990       && GET_MODE_BITSIZE (mode) >= 64
12991       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
12992     ASM_OUTPUT_ALIGN (file, 3);
12993   }
12994
12995   (*targetm.asm_out.internal_label) (file, "LC", labelno);
12996
12997   /* Handle FP constants specially.  Note that if we have a minimal
12998      TOC, things we put here aren't actually in the TOC, so we can allow
12999      FP constants.  */
13000   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == TFmode)
13001     {
13002       REAL_VALUE_TYPE rv;
13003       long k[4];
13004
13005       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13006       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
13007
13008       if (TARGET_64BIT)
13009         {
13010           if (TARGET_MINIMAL_TOC)
13011             fputs (DOUBLE_INT_ASM_OP, file);
13012           else
13013             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
13014                      k[0] & 0xffffffff, k[1] & 0xffffffff,
13015                      k[2] & 0xffffffff, k[3] & 0xffffffff);
13016           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
13017                    k[0] & 0xffffffff, k[1] & 0xffffffff,
13018                    k[2] & 0xffffffff, k[3] & 0xffffffff);
13019           return;
13020         }
13021       else
13022         {
13023           if (TARGET_MINIMAL_TOC)
13024             fputs ("\t.long ", file);
13025           else
13026             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
13027                      k[0] & 0xffffffff, k[1] & 0xffffffff,
13028                      k[2] & 0xffffffff, k[3] & 0xffffffff);
13029           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
13030                    k[0] & 0xffffffff, k[1] & 0xffffffff,
13031                    k[2] & 0xffffffff, k[3] & 0xffffffff);
13032           return;
13033         }
13034     }
13035   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
13036     {
13037       REAL_VALUE_TYPE rv;
13038       long k[2];
13039
13040       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13041       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
13042
13043       if (TARGET_64BIT)
13044         {
13045           if (TARGET_MINIMAL_TOC)
13046             fputs (DOUBLE_INT_ASM_OP, file);
13047           else
13048             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
13049                      k[0] & 0xffffffff, k[1] & 0xffffffff);
13050           fprintf (file, "0x%lx%08lx\n",
13051                    k[0] & 0xffffffff, k[1] & 0xffffffff);
13052           return;
13053         }
13054       else
13055         {
13056           if (TARGET_MINIMAL_TOC)
13057             fputs ("\t.long ", file);
13058           else
13059             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
13060                      k[0] & 0xffffffff, k[1] & 0xffffffff);
13061           fprintf (file, "0x%lx,0x%lx\n",
13062                    k[0] & 0xffffffff, k[1] & 0xffffffff);
13063           return;
13064         }
13065     }
13066   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
13067     {
13068       REAL_VALUE_TYPE rv;
13069       long l;
13070
13071       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13072       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
13073
13074       if (TARGET_64BIT)
13075         {
13076           if (TARGET_MINIMAL_TOC)
13077             fputs (DOUBLE_INT_ASM_OP, file);
13078           else
13079             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
13080           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
13081           return;
13082         }
13083       else
13084         {
13085           if (TARGET_MINIMAL_TOC)
13086             fputs ("\t.long ", file);
13087           else
13088             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
13089           fprintf (file, "0x%lx\n", l & 0xffffffff);
13090           return;
13091         }
13092     }
13093   else if (GET_MODE (x) == VOIDmode
13094            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
13095     {
13096       unsigned HOST_WIDE_INT low;
13097       HOST_WIDE_INT high;
13098
13099       if (GET_CODE (x) == CONST_DOUBLE)
13100         {
13101           low = CONST_DOUBLE_LOW (x);
13102           high = CONST_DOUBLE_HIGH (x);
13103         }
13104       else
13105 #if HOST_BITS_PER_WIDE_INT == 32
13106         {
13107           low = INTVAL (x);
13108           high = (low & 0x80000000) ? ~0 : 0;
13109         }
13110 #else
13111         {
13112           low = INTVAL (x) & 0xffffffff;
13113           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
13114         }
13115 #endif
13116
13117       /* TOC entries are always Pmode-sized, but since this
13118          is a bigendian machine then if we're putting smaller
13119          integer constants in the TOC we have to pad them.
13120          (This is still a win over putting the constants in
13121          a separate constant pool, because then we'd have
13122          to have both a TOC entry _and_ the actual constant.)
13123
13124          For a 32-bit target, CONST_INT values are loaded and shifted
13125          entirely within `low' and can be stored in one TOC entry.  */
13126
13127       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
13128         abort ();/* It would be easy to make this work, but it doesn't now.  */
13129
13130       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
13131         {
13132 #if HOST_BITS_PER_WIDE_INT == 32
13133           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
13134                          POINTER_SIZE, &low, &high, 0);
13135 #else
13136           low |= high << 32;
13137           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
13138           high = (HOST_WIDE_INT) low >> 32;
13139           low &= 0xffffffff;
13140 #endif
13141         }
13142
13143       if (TARGET_64BIT)
13144         {
13145           if (TARGET_MINIMAL_TOC)
13146             fputs (DOUBLE_INT_ASM_OP, file);
13147           else
13148             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13149                      (long) high & 0xffffffff, (long) low & 0xffffffff);
13150           fprintf (file, "0x%lx%08lx\n",
13151                    (long) high & 0xffffffff, (long) low & 0xffffffff);
13152           return;
13153         }
13154       else
13155         {
13156           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
13157             {
13158               if (TARGET_MINIMAL_TOC)
13159                 fputs ("\t.long ", file);
13160               else
13161                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13162                          (long) high & 0xffffffff, (long) low & 0xffffffff);
13163               fprintf (file, "0x%lx,0x%lx\n",
13164                        (long) high & 0xffffffff, (long) low & 0xffffffff);
13165             }
13166           else
13167             {
13168               if (TARGET_MINIMAL_TOC)
13169                 fputs ("\t.long ", file);
13170               else
13171                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
13172               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
13173             }
13174           return;
13175         }
13176     }
13177
13178   if (GET_CODE (x) == CONST)
13179     {
13180       if (GET_CODE (XEXP (x, 0)) != PLUS)
13181         abort ();
13182
13183       base = XEXP (XEXP (x, 0), 0);
13184       offset = INTVAL (XEXP (XEXP (x, 0), 1));
13185     }
13186   
13187   if (GET_CODE (base) == SYMBOL_REF)
13188     name = XSTR (base, 0);
13189   else if (GET_CODE (base) == LABEL_REF)
13190     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
13191   else if (GET_CODE (base) == CODE_LABEL)
13192     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
13193   else
13194     abort ();
13195
13196   real_name = (*targetm.strip_name_encoding) (name);
13197   if (TARGET_MINIMAL_TOC)
13198     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
13199   else
13200     {
13201       fprintf (file, "\t.tc %s", real_name);
13202
13203       if (offset < 0)
13204         fprintf (file, ".N%d", - offset);
13205       else if (offset)
13206         fprintf (file, ".P%d", offset);
13207
13208       fputs ("[TC],", file);
13209     }
13210
13211   /* Currently C++ toc references to vtables can be emitted before it
13212      is decided whether the vtable is public or private.  If this is
13213      the case, then the linker will eventually complain that there is
13214      a TOC reference to an unknown section.  Thus, for vtables only,
13215      we emit the TOC reference to reference the symbol and not the
13216      section.  */
13217   if (VTABLE_NAME_P (name))
13218     {
13219       RS6000_OUTPUT_BASENAME (file, name);
13220       if (offset < 0)
13221         fprintf (file, "%d", offset);
13222       else if (offset > 0)
13223         fprintf (file, "+%d", offset);
13224     }
13225   else
13226     output_addr_const (file, x);
13227   putc ('\n', file);
13228 }
13229 \f
13230 /* Output an assembler pseudo-op to write an ASCII string of N characters
13231    starting at P to FILE.
13232
13233    On the RS/6000, we have to do this using the .byte operation and
13234    write out special characters outside the quoted string.
13235    Also, the assembler is broken; very long strings are truncated,
13236    so we must artificially break them up early.  */
13237
13238 void
13239 output_ascii (FILE *file, const char *p, int n)
13240 {
13241   char c;
13242   int i, count_string;
13243   const char *for_string = "\t.byte \"";
13244   const char *for_decimal = "\t.byte ";
13245   const char *to_close = NULL;
13246
13247   count_string = 0;
13248   for (i = 0; i < n; i++)
13249     {
13250       c = *p++;
13251       if (c >= ' ' && c < 0177)
13252         {
13253           if (for_string)
13254             fputs (for_string, file);
13255           putc (c, file);
13256
13257           /* Write two quotes to get one.  */
13258           if (c == '"')
13259             {
13260               putc (c, file);
13261               ++count_string;
13262             }
13263
13264           for_string = NULL;
13265           for_decimal = "\"\n\t.byte ";
13266           to_close = "\"\n";
13267           ++count_string;
13268
13269           if (count_string >= 512)
13270             {
13271               fputs (to_close, file);
13272
13273               for_string = "\t.byte \"";
13274               for_decimal = "\t.byte ";
13275               to_close = NULL;
13276               count_string = 0;
13277             }
13278         }
13279       else
13280         {
13281           if (for_decimal)
13282             fputs (for_decimal, file);
13283           fprintf (file, "%d", c);
13284
13285           for_string = "\n\t.byte \"";
13286           for_decimal = ", ";
13287           to_close = "\n";
13288           count_string = 0;
13289         }
13290     }
13291
13292   /* Now close the string if we have written one.  Then end the line.  */
13293   if (to_close)
13294     fputs (to_close, file);
13295 }
13296 \f
13297 /* Generate a unique section name for FILENAME for a section type
13298    represented by SECTION_DESC.  Output goes into BUF.
13299
13300    SECTION_DESC can be any string, as long as it is different for each
13301    possible section type.
13302
13303    We name the section in the same manner as xlc.  The name begins with an
13304    underscore followed by the filename (after stripping any leading directory
13305    names) with the last period replaced by the string SECTION_DESC.  If
13306    FILENAME does not contain a period, SECTION_DESC is appended to the end of
13307    the name.  */
13308
13309 void
13310 rs6000_gen_section_name (char **buf, const char *filename, 
13311                          const char *section_desc)
13312 {
13313   const char *q, *after_last_slash, *last_period = 0;
13314   char *p;
13315   int len;
13316
13317   after_last_slash = filename;
13318   for (q = filename; *q; q++)
13319     {
13320       if (*q == '/')
13321         after_last_slash = q + 1;
13322       else if (*q == '.')
13323         last_period = q;
13324     }
13325
13326   len = strlen (after_last_slash) + strlen (section_desc) + 2;
13327   *buf = (char *) xmalloc (len);
13328
13329   p = *buf;
13330   *p++ = '_';
13331
13332   for (q = after_last_slash; *q; q++)
13333     {
13334       if (q == last_period)
13335         {
13336           strcpy (p, section_desc);
13337           p += strlen (section_desc);
13338           break;
13339         }
13340
13341       else if (ISALNUM (*q))
13342         *p++ = *q;
13343     }
13344
13345   if (last_period == 0)
13346     strcpy (p, section_desc);
13347   else
13348     *p = '\0';
13349 }
13350 \f
13351 /* Emit profile function.  */
13352
13353 void
13354 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
13355 {
13356   if (TARGET_PROFILE_KERNEL)
13357     return;
13358
13359   if (DEFAULT_ABI == ABI_AIX)
13360     {
13361 #ifndef NO_PROFILE_COUNTERS
13362 # define NO_PROFILE_COUNTERS 0
13363 #endif
13364       if (NO_PROFILE_COUNTERS)  
13365         emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 0);
13366       else
13367         {
13368           char buf[30];
13369           const char *label_name;
13370           rtx fun;
13371
13372           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13373           label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
13374           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
13375
13376           emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
13377                              fun, Pmode);
13378         }
13379     }
13380   else if (DEFAULT_ABI == ABI_DARWIN)
13381     {
13382       const char *mcount_name = RS6000_MCOUNT;
13383       int caller_addr_regno = LINK_REGISTER_REGNUM;
13384
13385       /* Be conservative and always set this, at least for now.  */
13386       current_function_uses_pic_offset_table = 1;
13387
13388 #if TARGET_MACHO
13389       /* For PIC code, set up a stub and collect the caller's address
13390          from r0, which is where the prologue puts it.  */
13391       if (MACHOPIC_INDIRECT)
13392         {
13393           mcount_name = machopic_stub_name (mcount_name);
13394           if (current_function_uses_pic_offset_table)
13395             caller_addr_regno = 0;
13396         }
13397 #endif
13398       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
13399                          0, VOIDmode, 1,
13400                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
13401     }
13402 }
13403
13404 /* Write function profiler code.  */
13405
13406 void
13407 output_function_profiler (FILE *file, int labelno)
13408 {
13409   char buf[100];
13410   int save_lr = 8;
13411
13412   switch (DEFAULT_ABI)
13413     {
13414     default:
13415       abort ();
13416
13417     case ABI_V4:
13418       save_lr = 4;
13419       if (!TARGET_32BIT)
13420         {
13421           warning ("no profiling of 64-bit code for this ABI");
13422           return;
13423         }
13424       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13425       fprintf (file, "\tmflr %s\n", reg_names[0]);
13426       if (flag_pic == 1)
13427         {
13428           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
13429           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13430                        reg_names[0], save_lr, reg_names[1]);
13431           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
13432           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
13433           assemble_name (file, buf);
13434           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
13435         }
13436       else if (flag_pic > 1)
13437         {
13438           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13439                        reg_names[0], save_lr, reg_names[1]);
13440           /* Now, we need to get the address of the label.  */
13441           fputs ("\tbl 1f\n\t.long ", file);
13442           assemble_name (file, buf);
13443           fputs ("-.\n1:", file);
13444           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
13445           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
13446                        reg_names[0], reg_names[11]);
13447           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
13448                        reg_names[0], reg_names[0], reg_names[11]);
13449         }
13450       else
13451         {
13452           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
13453           assemble_name (file, buf);
13454           fputs ("@ha\n", file);
13455           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13456                        reg_names[0], save_lr, reg_names[1]);
13457           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
13458           assemble_name (file, buf);
13459           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
13460         }
13461
13462       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
13463       fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13464       break;
13465
13466     case ABI_AIX:
13467     case ABI_DARWIN:
13468       if (!TARGET_PROFILE_KERNEL)
13469         {
13470           /* Don't do anything, done in output_profile_hook (). */
13471         }
13472       else
13473         {
13474           if (TARGET_32BIT)
13475             abort ();
13476
13477           asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
13478           asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
13479
13480           if (current_function_needs_context)
13481             {
13482               asm_fprintf (file, "\tstd %s,24(%s)\n",
13483                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13484               fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13485               asm_fprintf (file, "\tld %s,24(%s)\n",
13486                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13487             }
13488           else
13489             fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13490         }
13491       break;
13492     }
13493 }
13494
13495 \f
13496 static int
13497 rs6000_use_dfa_pipeline_interface (void)
13498 {
13499   return 1;
13500 }
13501
13502 /* Power4 load update and store update instructions are cracked into a
13503    load or store and an integer insn which are executed in the same cycle.
13504    Branches have their own dispatch slot which does not count against the
13505    GCC issue rate, but it changes the program flow so there are no other
13506    instructions to issue in this cycle.  */
13507
13508 static int
13509 rs6000_variable_issue (FILE *stream ATTRIBUTE_UNUSED, 
13510                        int verbose ATTRIBUTE_UNUSED, 
13511                        rtx insn, int more)
13512 {
13513   if (GET_CODE (PATTERN (insn)) == USE
13514       || GET_CODE (PATTERN (insn)) == CLOBBER)
13515     return more;
13516
13517   if (rs6000_cpu == PROCESSOR_POWER4)
13518     {
13519       if (is_microcoded_insn (insn))
13520         return 0;
13521       else if (is_cracked_insn (insn))
13522         return more > 2 ? more - 2 : 0;
13523     }
13524
13525   return more - 1;
13526 }
13527
13528 /* Adjust the cost of a scheduling dependency.  Return the new cost of
13529    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
13530
13531 static int
13532 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn ATTRIBUTE_UNUSED, 
13533                     int cost)
13534 {
13535   if (! recog_memoized (insn))
13536     return 0;
13537
13538   if (REG_NOTE_KIND (link) != 0)
13539     return 0;
13540
13541   if (REG_NOTE_KIND (link) == 0)
13542     {
13543       /* Data dependency; DEP_INSN writes a register that INSN reads
13544          some cycles later.  */
13545       switch (get_attr_type (insn))
13546         {
13547         case TYPE_JMPREG:
13548           /* Tell the first scheduling pass about the latency between
13549              a mtctr and bctr (and mtlr and br/blr).  The first
13550              scheduling pass will not know about this latency since
13551              the mtctr instruction, which has the latency associated
13552              to it, will be generated by reload.  */
13553           return TARGET_POWER ? 5 : 4;
13554         case TYPE_BRANCH:
13555           /* Leave some extra cycles between a compare and its
13556              dependent branch, to inhibit expensive mispredicts.  */
13557           if ((rs6000_cpu_attr == CPU_PPC603
13558                || rs6000_cpu_attr == CPU_PPC604
13559                || rs6000_cpu_attr == CPU_PPC604E
13560                || rs6000_cpu_attr == CPU_PPC620
13561                || rs6000_cpu_attr == CPU_PPC630
13562                || rs6000_cpu_attr == CPU_PPC750
13563                || rs6000_cpu_attr == CPU_PPC7400
13564                || rs6000_cpu_attr == CPU_PPC7450
13565                || rs6000_cpu_attr == CPU_POWER4)
13566               && recog_memoized (dep_insn)
13567               && (INSN_CODE (dep_insn) >= 0)
13568               && (get_attr_type (dep_insn) == TYPE_CMP
13569                   || get_attr_type (dep_insn) == TYPE_COMPARE
13570                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
13571                   || get_attr_type (dep_insn) == TYPE_IMUL_COMPARE
13572                   || get_attr_type (dep_insn) == TYPE_LMUL_COMPARE
13573                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
13574                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL
13575                   || get_attr_type (dep_insn) == TYPE_DELAYED_CR))
13576             return cost + 2;
13577         default:
13578           break;
13579         }
13580       /* Fall out to return default cost.  */
13581     }
13582
13583   return cost;
13584 }
13585
13586 /* The function returns a true if INSN is microcoded.
13587    Return false ptherwise.  */
13588
13589 static bool
13590 is_microcoded_insn (rtx insn)
13591 {
13592   if (!insn || !INSN_P (insn)
13593       || GET_CODE (PATTERN (insn)) == USE
13594       || GET_CODE (PATTERN (insn)) == CLOBBER)
13595     return false;
13596
13597   if (rs6000_cpu == PROCESSOR_POWER4)
13598     {
13599       enum attr_type type = get_attr_type (insn);
13600       if (type == TYPE_LOAD_EXT_U
13601           || type == TYPE_LOAD_EXT_UX
13602           || type == TYPE_LOAD_UX
13603           || type == TYPE_STORE_UX
13604           || type == TYPE_MFCR)
13605         return true;
13606     }
13607
13608   return false;
13609 }
13610
13611 /* The function returns a non-zero value if INSN can be scheduled only
13612    as the first insn in a dispatch group ("dispatch-slot restricted").
13613    In this case, the returned value indicates how many dispatch slots
13614    the insn occupies (at the beginning of the group).
13615    Return 0 otherwise.  */
13616
13617 static int
13618 is_dispatch_slot_restricted (rtx insn)
13619 {
13620   enum attr_type type;
13621
13622   if (rs6000_cpu != PROCESSOR_POWER4)
13623     return 0;
13624
13625   if (!insn
13626       || insn == NULL_RTX
13627       || GET_CODE (insn) == NOTE
13628       || GET_CODE (PATTERN (insn)) == USE
13629       || GET_CODE (PATTERN (insn)) == CLOBBER)
13630     return 0;
13631
13632   type = get_attr_type (insn);
13633
13634   switch (type){
13635   case TYPE_MFCR:
13636   case TYPE_MFCRF:
13637   case TYPE_MTCR:
13638   case TYPE_DELAYED_CR:
13639   case TYPE_CR_LOGICAL:
13640   case TYPE_MTJMPR:
13641   case TYPE_MFJMPR:
13642     return 1;
13643   case TYPE_IDIV:
13644   case TYPE_LDIV:
13645     return 2;
13646   default:
13647     return 0;
13648   }
13649 }
13650
13651 /* The function returns true if INSN is cracked into 2 instructions
13652    by the processor (and therefore occupies 2 issue slots).  */
13653
13654 static bool
13655 is_cracked_insn (rtx insn)
13656 {
13657   if (!insn || !INSN_P (insn)
13658       || GET_CODE (PATTERN (insn)) == USE
13659       || GET_CODE (PATTERN (insn)) == CLOBBER)
13660     return false;
13661
13662   if (rs6000_cpu == PROCESSOR_POWER4)
13663     {
13664       enum attr_type type = get_attr_type (insn);
13665       if (type == TYPE_LOAD_U || type == TYPE_STORE_U
13666                || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
13667                || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
13668                || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
13669                || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
13670                || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
13671                || type == TYPE_IDIV || type == TYPE_LDIV
13672                || type == TYPE_INSERT_WORD)
13673         return true;
13674     }
13675
13676   return false;
13677 }
13678
13679 /* The function returns true if INSN can be issued only from
13680    the branch slot. */
13681
13682 static bool
13683 is_branch_slot_insn (rtx insn)
13684 {
13685   if (!insn || !INSN_P (insn)
13686       || GET_CODE (PATTERN (insn)) == USE
13687       || GET_CODE (PATTERN (insn)) == CLOBBER)
13688     return false;
13689
13690   if (rs6000_cpu == PROCESSOR_POWER4)
13691     {
13692       enum attr_type type = get_attr_type (insn);
13693       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
13694         return true;     
13695       return false;
13696     }
13697
13698   return false;
13699 }
13700
13701 /* A C statement (sans semicolon) to update the integer scheduling
13702    priority INSN_PRIORITY (INSN). Increase the priority to execute the
13703    INSN earlier, reduce the priority to execute INSN later.  Do not
13704    define this macro if you do not need to adjust the scheduling
13705    priorities of insns.  */
13706
13707 static int
13708 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
13709 {
13710   /* On machines (like the 750) which have asymmetric integer units,
13711      where one integer unit can do multiply and divides and the other
13712      can't, reduce the priority of multiply/divide so it is scheduled
13713      before other integer operations.  */
13714
13715 #if 0
13716   if (! INSN_P (insn))
13717     return priority;
13718
13719   if (GET_CODE (PATTERN (insn)) == USE)
13720     return priority;
13721
13722   switch (rs6000_cpu_attr) {
13723   case CPU_PPC750:
13724     switch (get_attr_type (insn))
13725       {
13726       default:
13727         break;
13728
13729       case TYPE_IMUL:
13730       case TYPE_IDIV:
13731         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
13732                  priority, priority);
13733         if (priority >= 0 && priority < 0x01000000)
13734           priority >>= 3;
13735         break;
13736       }
13737   }
13738 #endif
13739
13740   if (is_dispatch_slot_restricted (insn)
13741       && reload_completed
13742       && current_sched_info->sched_max_insns_priority 
13743       && rs6000_sched_restricted_insns_priority)
13744     {
13745
13746       /* Prioritize insns that can be dispatched only in the first dispatch slot.  */
13747       if (rs6000_sched_restricted_insns_priority == 1)
13748         /* Attach highest priority to insn. This means that in 
13749            haifa-sched.c:ready_sort(), dispatch-slot restriction considerations 
13750            precede 'priority' (critical path) considerations.  */
13751         return current_sched_info->sched_max_insns_priority; 
13752       else if (rs6000_sched_restricted_insns_priority == 2)
13753         /* Increase priority of insn by a minimal amount. This means that in 
13754            haifa-sched.c:ready_sort(), only 'priority' (critical path) considerations
13755            precede dispatch-slot restriction considerations.  */
13756         return (priority + 1); 
13757     } 
13758
13759   return priority;
13760 }
13761
13762 /* Return how many instructions the machine can issue per cycle.  */
13763
13764 static int
13765 rs6000_issue_rate (void)
13766 {
13767   /* Use issue rate of 1 for first scheduling pass to decrease degradation.  */
13768   if (!reload_completed)
13769     return 1;
13770
13771   switch (rs6000_cpu_attr) {
13772   case CPU_RIOS1:  /* ? */
13773   case CPU_RS64A:
13774   case CPU_PPC601: /* ? */
13775   case CPU_PPC7450:
13776     return 3;
13777   case CPU_PPC440:
13778   case CPU_PPC603:
13779   case CPU_PPC750:
13780   case CPU_PPC7400:
13781   case CPU_PPC8540:
13782     return 2; 
13783   case CPU_RIOS2:
13784   case CPU_PPC604:
13785   case CPU_PPC604E:
13786   case CPU_PPC620:
13787   case CPU_PPC630:
13788     return 4;
13789   case CPU_POWER4:
13790     return 5;
13791   default:
13792     return 1;
13793   }
13794 }
13795
13796 /* Return how many instructions to look ahead for better insn
13797    scheduling.  */
13798
13799 static int
13800 rs6000_use_sched_lookahead (void)
13801 {
13802   if (rs6000_cpu_attr == CPU_PPC8540)
13803     return 4;
13804   return 0;
13805 }
13806
13807 /* Determine is PAT refers to memory.  */
13808
13809 static bool
13810 is_mem_ref (rtx pat)
13811 {
13812   const char * fmt;
13813   int i, j;
13814   bool ret = false;
13815
13816   if (GET_CODE (pat) == MEM)
13817     return true;
13818
13819   /* Recursively process the pattern.  */
13820   fmt = GET_RTX_FORMAT (GET_CODE (pat));
13821
13822   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0 && !ret; i--)
13823     {
13824       if (fmt[i] == 'e')
13825         ret |= is_mem_ref (XEXP (pat, i));
13826       else if (fmt[i] == 'E')
13827         for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
13828           ret |= is_mem_ref (XVECEXP (pat, i, j));
13829     }
13830
13831   return ret;
13832 }
13833
13834 /* Determine if PAT is a PATTERN of a load insn.  */
13835  
13836 static bool
13837 is_load_insn1 (rtx pat)
13838 {
13839   if (!pat || pat == NULL_RTX)
13840     return false;
13841
13842   if (GET_CODE (pat) == SET)
13843     return is_mem_ref (SET_SRC (pat));
13844
13845   if (GET_CODE (pat) == PARALLEL)
13846     {
13847       int i;
13848
13849       for (i = 0; i < XVECLEN (pat, 0); i++)
13850         if (is_load_insn1 (XVECEXP (pat, 0, i)))
13851           return true;
13852     }
13853
13854   return false;
13855 }
13856
13857 /* Determine if INSN loads from memory.  */
13858
13859 static bool
13860 is_load_insn (rtx insn)
13861 {
13862   if (!insn || !INSN_P (insn))
13863     return false;
13864
13865   if (GET_CODE (insn) == CALL_INSN)
13866     return false;
13867
13868   return is_load_insn1 (PATTERN (insn));
13869 }
13870
13871 /* Determine if PAT is a PATTERN of a store insn.  */
13872
13873 static bool
13874 is_store_insn1 (rtx pat)
13875 {
13876   if (!pat || pat == NULL_RTX)
13877     return false;
13878
13879   if (GET_CODE (pat) == SET)
13880     return is_mem_ref (SET_DEST (pat));
13881
13882   if (GET_CODE (pat) == PARALLEL)
13883     {
13884       int i;
13885
13886       for (i = 0; i < XVECLEN (pat, 0); i++)
13887         if (is_store_insn1 (XVECEXP (pat, 0, i)))
13888           return true;
13889     }
13890
13891   return false;
13892 }
13893
13894 /* Determine if INSN stores to memory.  */
13895
13896 static bool
13897 is_store_insn (rtx insn)
13898 {
13899   if (!insn || !INSN_P (insn))
13900     return false;
13901
13902   return is_store_insn1 (PATTERN (insn));
13903 }
13904
13905 /* Returns whether the dependence between INSN and NEXT is considered
13906    costly by the given target.  */
13907
13908 static bool
13909 rs6000_is_costly_dependence (rtx insn, rtx next, rtx link, int cost, int distance)
13910 {      
13911   /* If the flag is not enbled - no dependence is considered costly;
13912      allow all dependent insns in the same group. 
13913      This is the most aggressive option.  */
13914   if (rs6000_sched_costly_dep == no_dep_costly)
13915     return false;
13916
13917   /* If the flag is set to 1 - a dependence is always considered costly; 
13918      do not allow dependent instructions in the same group.
13919      This is the most conservative option.  */
13920   if (rs6000_sched_costly_dep == all_deps_costly)
13921     return true;       
13922
13923   if (rs6000_sched_costly_dep == store_to_load_dep_costly 
13924       && is_load_insn (next) 
13925       && is_store_insn (insn))
13926     /* Prevent load after store in the same group.  */
13927     return true;
13928
13929   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
13930       && is_load_insn (next) 
13931       && is_store_insn (insn)
13932       && (!link || (int) REG_NOTE_KIND (link) == 0))
13933      /* Prevent load after store in the same group if it is a true dependence.  */
13934      return true;
13935     
13936   /* The flag is set to X; dependences with latency >= X are considered costly, 
13937      and will not be scheduled in the same group.  */
13938   if (rs6000_sched_costly_dep <= max_dep_latency
13939       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
13940     return true;
13941
13942   return false;
13943 }
13944
13945 /* Return the next insn after INSN that is found before TAIL is reached, 
13946    skipping any "non-active" insns - insns that will not actually occupy
13947    an issue slot.  Return NULL_RTX if such an insn is not found.  */
13948
13949 static rtx
13950 get_next_active_insn (rtx insn, rtx tail)
13951 {
13952   rtx next_insn;
13953
13954   if (!insn || insn == tail)
13955     return NULL_RTX;
13956
13957   next_insn = NEXT_INSN (insn);
13958
13959   while (next_insn
13960          && next_insn != tail
13961          && (GET_CODE(next_insn) == NOTE
13962              || GET_CODE (PATTERN (next_insn)) == USE
13963              || GET_CODE (PATTERN (next_insn)) == CLOBBER))
13964     {
13965       next_insn = NEXT_INSN (next_insn);
13966     }
13967
13968   if (!next_insn || next_insn == tail)
13969     return NULL_RTX;
13970
13971   return next_insn;
13972 }
13973
13974 /* Return whether the presence of INSN causes a dispatch group terminatation
13975    of group WHICH_GROUP.
13976
13977    If WHICH_GROUP == current_group, this function will return true if INSN
13978    causes the termination of the current group (i.e, the dispatch group to
13979    which INSN belongs). This means that INSN will be the last insn in the
13980    group it belongs to.
13981
13982    If WHICH_GROUP == previous_group, this function will return true if INSN
13983    causes the termination of the previous group (i.e, the dispatch group that
13984    precedes the group to which INSN belongs).  This means that INSN will be
13985    the first insn in the group it belongs to).  */
13986
13987 static bool
13988 insn_terminates_group_p (rtx insn, enum group_termination which_group)
13989 {
13990   enum attr_type type;
13991
13992   if (! insn)
13993     return false;
13994
13995   type = get_attr_type (insn);
13996
13997   if (is_microcoded_insn (insn))
13998     return true;
13999
14000   if (which_group == current_group)
14001     {
14002       if (is_branch_slot_insn (insn))
14003         return true;
14004       return false;
14005     }
14006   else if (which_group == previous_group)
14007     {
14008       if (is_dispatch_slot_restricted (insn))
14009         return true;
14010       return false;
14011     }
14012
14013   return false;
14014 }
14015
14016 /* Return true if it is recommended to keep NEXT_INSN "far" (in a seperate
14017    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
14018
14019 static bool
14020 is_costly_group (rtx *group_insns, rtx next_insn)
14021 {
14022   int i;
14023   rtx link;
14024   int cost;
14025   int issue_rate = rs6000_issue_rate ();
14026
14027   for (i = 0; i < issue_rate; i++)
14028     {
14029       rtx insn = group_insns[i];
14030       if (!insn)
14031         continue;
14032       for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
14033         {
14034           rtx next = XEXP (link, 0);
14035           if (next == next_insn)
14036             {
14037               cost = insn_cost (insn, link, next_insn);
14038               if (rs6000_is_costly_dependence (insn, next_insn, link, cost, 0))
14039                 return true;
14040             }
14041         }
14042     }
14043
14044   return false;
14045 }
14046
14047 /* Utility of the function redefine_groups. 
14048    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
14049    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
14050    to keep it "far" (in a separate group) from GROUP_INSNS, following
14051    one of the following schemes, depending on the value of the flag
14052    -minsert_sched_nops = X:
14053    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
14054        in order to force NEXT_INSN into a seperate group.
14055    (2) X < sched_finish_regroup_exact: insert exactly X nops.  
14056    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop 
14057    insertion (has a group just ended, how many vacant issue slots remain in the
14058    last group, and how many dispatch groups were encountered so far).  */
14059
14060 static int 
14061 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns, rtx next_insn,
14062                  bool *group_end, int can_issue_more, int *group_count)
14063 {
14064   rtx nop;
14065   bool force;
14066   int issue_rate = rs6000_issue_rate ();
14067   bool end = *group_end;
14068   int i;
14069
14070   if (next_insn == NULL_RTX)
14071     return can_issue_more;
14072
14073   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
14074     return can_issue_more;
14075
14076   force = is_costly_group (group_insns, next_insn);
14077   if (!force)
14078     return can_issue_more;
14079
14080   if (sched_verbose > 6)
14081     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
14082                         *group_count ,can_issue_more);
14083
14084   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
14085     {
14086       if (*group_end)
14087         can_issue_more = 0;
14088
14089       /* Since only a branch can be issued in the last issue_slot, it is
14090          sufficient to insert 'can_issue_more - 1' nops if next_insn is not
14091          a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
14092          in this case the last nop will start a new group and the branch will be
14093          forced to the new group.  */
14094       if (can_issue_more && !is_branch_slot_insn (next_insn))
14095         can_issue_more--;
14096
14097       while (can_issue_more > 0)
14098         {
14099           nop = gen_nop();
14100           emit_insn_before (nop, next_insn);
14101           can_issue_more--;
14102         }
14103
14104       *group_end = true;
14105       return 0;
14106     } 
14107
14108   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
14109     {
14110       int n_nops = rs6000_sched_insert_nops;
14111
14112       /* Nops can't be issued from the branch slot, so the effective 
14113          issue_rate for nops is 'issue_rate - 1'.  */
14114       if (can_issue_more == 0)
14115         can_issue_more = issue_rate;
14116       can_issue_more--;
14117       if (can_issue_more == 0)
14118         {
14119           can_issue_more = issue_rate - 1;
14120           (*group_count)++;
14121           end = true;
14122           for (i = 0; i < issue_rate; i++)
14123             {
14124               group_insns[i] = 0;
14125             }
14126         }
14127
14128       while (n_nops > 0)
14129         {
14130           nop = gen_nop ();
14131           emit_insn_before (nop, next_insn);
14132           if (can_issue_more == issue_rate - 1) /* new group begins */
14133             end = false;
14134           can_issue_more--;
14135           if (can_issue_more == 0)
14136             {
14137               can_issue_more = issue_rate - 1;
14138               (*group_count)++;
14139               end = true;
14140               for (i = 0; i < issue_rate; i++)
14141                 {
14142                   group_insns[i] = 0;
14143                 } 
14144             }   
14145           n_nops--;
14146         }
14147
14148       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
14149       can_issue_more++; 
14150
14151       *group_end = /* Is next_insn going to start a new group?  */
14152           (end 
14153            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14154            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14155            || (can_issue_more < issue_rate &&
14156               insn_terminates_group_p (next_insn, previous_group)));
14157       if (*group_end && end)
14158         (*group_count)--;
14159
14160       if (sched_verbose > 6)
14161         fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
14162                         *group_count, can_issue_more);
14163       return can_issue_more;    
14164     } 
14165
14166   return can_issue_more;
14167 }
14168
14169 /* This function tries to synch the dispatch groups that the compiler "sees"
14170    with the dispatch groups that the processor dispatcher is expected to 
14171    form in practice.  It tries to achieve this synchronization by forcing the
14172    estimated processor grouping on the compiler (as opposed to the function
14173    'pad_goups' which tries to force the scheduler's grouping on the processor).
14174
14175    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
14176    examines the (estimated) dispatch groups that will be formed by the processor
14177    dispatcher.  It marks these group boundaries to reflect the estimated
14178    processor grouping, overriding the grouping that the scheduler had marked.
14179    Depending on the value of the flag '-minsert-sched-nops' this function can
14180    force certain insns into separate groups or force a certain distance between
14181    them by inserting nops, for example, if there exists a "costly dependence"
14182    between the insns.
14183
14184    The function estimates the group boundaries that the processor will form as
14185    folllows:  It keeps track of how many vacant issue slots are available after
14186    each insn.  A subsequent insn will start a new group if one of the following
14187    4 cases applies:
14188    - no more vacant issue slots remain in the current dispatch group.
14189    - only the last issue slot, which is the branch slot, is vacant, but the next
14190      insn is not a branch.
14191    - only the last 2 or less issue slots, including the branch slot, are vacant,
14192      which means that a cracked insn (which occupies two issue slots) can't be
14193      issued in this group.
14194    - less than 'issue_rate' slots are vacant, and the next insn always needs to 
14195      start a new group.  */
14196
14197 static int
14198 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14199 {
14200   rtx insn, next_insn;
14201   int issue_rate;
14202   int can_issue_more;
14203   int slot, i;
14204   bool group_end;
14205   int group_count = 0;
14206   rtx *group_insns;
14207
14208   /* Initialize.  */
14209   issue_rate = rs6000_issue_rate ();
14210   group_insns = alloca (issue_rate * sizeof (rtx));
14211   for (i = 0; i < issue_rate; i++) 
14212     {
14213       group_insns[i] = 0;
14214     }
14215   can_issue_more = issue_rate;
14216   slot = 0;
14217   insn = get_next_active_insn (prev_head_insn, tail);
14218   group_end = false;
14219
14220   while (insn != NULL_RTX)
14221     {
14222       slot = (issue_rate - can_issue_more);
14223       group_insns[slot] = insn;
14224       can_issue_more =
14225         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14226       if (insn_terminates_group_p (insn, current_group))
14227         can_issue_more = 0;
14228
14229       next_insn = get_next_active_insn (insn, tail);
14230       if (next_insn == NULL_RTX)
14231         return group_count + 1;
14232
14233       group_end = /* Is next_insn going to start a new group?  */
14234         (can_issue_more == 0
14235          || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14236          || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14237          || (can_issue_more < issue_rate &&
14238              insn_terminates_group_p (next_insn, previous_group)));
14239
14240       can_issue_more = force_new_group (sched_verbose, dump, group_insns, 
14241                         next_insn, &group_end, can_issue_more, &group_count);
14242
14243       if (group_end)
14244         {
14245           group_count++;
14246           can_issue_more = 0;
14247           for (i = 0; i < issue_rate; i++)
14248             {
14249               group_insns[i] = 0;
14250             }
14251         }
14252
14253       if (GET_MODE (next_insn) == TImode && can_issue_more)
14254         PUT_MODE(next_insn, VOIDmode);
14255       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
14256         PUT_MODE (next_insn, TImode);
14257
14258       insn = next_insn;
14259       if (can_issue_more == 0)
14260         can_issue_more = issue_rate;
14261    } /* while */
14262
14263   return group_count;
14264 }
14265
14266 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
14267    dispatch group boundaries that the scheduler had marked.  Pad with nops
14268    any dispatch groups which have vacant issue slots, in order to force the
14269    scheduler's grouping on the processor dispatcher.  The function
14270    returns the number of dispatch groups found.  */
14271
14272 static int
14273 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14274 {
14275   rtx insn, next_insn;
14276   rtx nop;
14277   int issue_rate;
14278   int can_issue_more;
14279   int group_end;
14280   int group_count = 0;
14281
14282   /* Initialize issue_rate.  */
14283   issue_rate = rs6000_issue_rate ();
14284   can_issue_more = issue_rate;
14285
14286   insn = get_next_active_insn (prev_head_insn, tail);
14287   next_insn = get_next_active_insn (insn, tail);
14288
14289   while (insn != NULL_RTX)
14290     {
14291       can_issue_more =
14292         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14293
14294       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
14295
14296       if (next_insn == NULL_RTX)
14297         break;
14298
14299       if (group_end)
14300         {
14301           /* If the scheduler had marked group termination at this location
14302              (between insn and next_indn), and neither insn nor next_insn will
14303              force group termination, pad the group with nops to force group
14304              termination.  */
14305           if (can_issue_more
14306               && (rs6000_sched_insert_nops == sched_finish_pad_groups)
14307               && !insn_terminates_group_p (insn, current_group)
14308               && !insn_terminates_group_p (next_insn, previous_group))
14309             {
14310               if (!is_branch_slot_insn(next_insn))
14311                 can_issue_more--;
14312
14313               while (can_issue_more)
14314                 {
14315                   nop = gen_nop ();
14316                   emit_insn_before (nop, next_insn);
14317                   can_issue_more--;
14318                 }
14319             }
14320
14321           can_issue_more = issue_rate;
14322           group_count++;
14323         }
14324
14325       insn = next_insn;
14326       next_insn = get_next_active_insn (insn, tail);
14327     }
14328
14329   return group_count;
14330 }
14331
14332 /* The following function is called at the end of scheduling BB.
14333    After reload, it inserts nops at insn group bundling.  */
14334
14335 static void
14336 rs6000_sched_finish (FILE *dump, int sched_verbose)
14337 {
14338   int n_groups;
14339
14340   if (sched_verbose)
14341     fprintf (dump, "=== Finishing schedule.\n");
14342
14343   if (reload_completed && rs6000_cpu == PROCESSOR_POWER4)
14344     {
14345       if (rs6000_sched_insert_nops == sched_finish_none)
14346         return;
14347
14348       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
14349         n_groups = pad_groups (dump, sched_verbose,
14350                                 current_sched_info->prev_head,
14351                                 current_sched_info->next_tail);
14352       else
14353         n_groups = redefine_groups (dump, sched_verbose,
14354                                 current_sched_info->prev_head,
14355                                 current_sched_info->next_tail);
14356
14357       if (sched_verbose >= 6)
14358         {
14359           fprintf (dump, "ngroups = %d\n", n_groups);
14360           print_rtl (dump, current_sched_info->prev_head);
14361           fprintf (dump, "Done finish_sched\n");
14362         }
14363     }
14364 }
14365 \f
14366 /* Length in units of the trampoline for entering a nested function.  */
14367
14368 int
14369 rs6000_trampoline_size (void)
14370 {
14371   int ret = 0;
14372
14373   switch (DEFAULT_ABI)
14374     {
14375     default:
14376       abort ();
14377
14378     case ABI_AIX:
14379       ret = (TARGET_32BIT) ? 12 : 24;
14380       break;
14381
14382     case ABI_DARWIN:
14383     case ABI_V4:
14384       ret = (TARGET_32BIT) ? 40 : 48;
14385       break;
14386     }
14387
14388   return ret;
14389 }
14390
14391 /* Emit RTL insns to initialize the variable parts of a trampoline.
14392    FNADDR is an RTX for the address of the function's pure code.
14393    CXT is an RTX for the static chain value for the function.  */
14394
14395 void
14396 rs6000_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
14397 {
14398   enum machine_mode pmode = Pmode;
14399   int regsize = (TARGET_32BIT) ? 4 : 8;
14400   rtx ctx_reg = force_reg (pmode, cxt);
14401
14402   switch (DEFAULT_ABI)
14403     {
14404     default:
14405       abort ();
14406
14407 /* Macros to shorten the code expansions below.  */
14408 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
14409 #define MEM_PLUS(addr,offset) \
14410   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
14411
14412     /* Under AIX, just build the 3 word function descriptor */
14413     case ABI_AIX:
14414       {
14415         rtx fn_reg = gen_reg_rtx (pmode);
14416         rtx toc_reg = gen_reg_rtx (pmode);
14417         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
14418         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
14419         emit_move_insn (MEM_DEREF (addr), fn_reg);
14420         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
14421         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
14422       }
14423       break;
14424
14425     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
14426     case ABI_DARWIN:
14427     case ABI_V4:
14428       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
14429                          FALSE, VOIDmode, 4,
14430                          addr, pmode,
14431                          GEN_INT (rs6000_trampoline_size ()), SImode,
14432                          fnaddr, pmode,
14433                          ctx_reg, pmode);
14434       break;
14435     }
14436
14437   return;
14438 }
14439
14440 \f
14441 /* Table of valid machine attributes.  */
14442
14443 const struct attribute_spec rs6000_attribute_table[] =
14444 {
14445   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
14446   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
14447   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
14448   { NULL,        0, 0, false, false, false, NULL }
14449 };
14450
14451 /* Handle a "longcall" or "shortcall" attribute; arguments as in
14452    struct attribute_spec.handler.  */
14453
14454 static tree
14455 rs6000_handle_longcall_attribute (tree *node, tree name, 
14456                                   tree args ATTRIBUTE_UNUSED, 
14457                                   int flags ATTRIBUTE_UNUSED, 
14458                                   bool *no_add_attrs)
14459 {
14460   if (TREE_CODE (*node) != FUNCTION_TYPE
14461       && TREE_CODE (*node) != FIELD_DECL
14462       && TREE_CODE (*node) != TYPE_DECL)
14463     {
14464       warning ("`%s' attribute only applies to functions",
14465                IDENTIFIER_POINTER (name));
14466       *no_add_attrs = true;
14467     }
14468
14469   return NULL_TREE;
14470 }
14471
14472 /* Set longcall attributes on all functions declared when
14473    rs6000_default_long_calls is true.  */
14474 static void
14475 rs6000_set_default_type_attributes (tree type)
14476 {
14477   if (rs6000_default_long_calls
14478       && (TREE_CODE (type) == FUNCTION_TYPE
14479           || TREE_CODE (type) == METHOD_TYPE))
14480     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
14481                                         NULL_TREE,
14482                                         TYPE_ATTRIBUTES (type));
14483 }
14484
14485 /* Return a reference suitable for calling a function with the
14486    longcall attribute.  */
14487
14488 struct rtx_def *
14489 rs6000_longcall_ref (rtx call_ref)
14490 {
14491   const char *call_name;
14492   tree node;
14493
14494   if (GET_CODE (call_ref) != SYMBOL_REF)
14495     return call_ref;
14496
14497   /* System V adds '.' to the internal name, so skip them.  */
14498   call_name = XSTR (call_ref, 0);
14499   if (*call_name == '.')
14500     {
14501       while (*call_name == '.')
14502         call_name++;
14503
14504       node = get_identifier (call_name);
14505       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
14506     }
14507
14508   return force_reg (Pmode, call_ref);
14509 }
14510 \f
14511 #ifdef USING_ELFOS_H
14512
14513 /* A C statement or statements to switch to the appropriate section
14514    for output of RTX in mode MODE.  You can assume that RTX is some
14515    kind of constant in RTL.  The argument MODE is redundant except in
14516    the case of a `const_int' rtx.  Select the section by calling
14517    `text_section' or one of the alternatives for other sections.
14518
14519    Do not define this macro if you put all constants in the read-only
14520    data section.  */
14521
14522 static void
14523 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x, 
14524                                unsigned HOST_WIDE_INT align)
14525 {
14526   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
14527     toc_section ();
14528   else
14529     default_elf_select_rtx_section (mode, x, align);
14530 }
14531
14532 /* A C statement or statements to switch to the appropriate
14533    section for output of DECL.  DECL is either a `VAR_DECL' node
14534    or a constant of some sort.  RELOC indicates whether forming
14535    the initial value of DECL requires link-time relocations.  */
14536
14537 static void
14538 rs6000_elf_select_section (tree decl, int reloc, 
14539                            unsigned HOST_WIDE_INT align)
14540 {
14541   /* Pretend that we're always building for a shared library when
14542      ABI_AIX, because otherwise we end up with dynamic relocations
14543      in read-only sections.  This happens for function pointers,
14544      references to vtables in typeinfo, and probably other cases.  */
14545   default_elf_select_section_1 (decl, reloc, align,
14546                                 flag_pic || DEFAULT_ABI == ABI_AIX);
14547 }
14548
14549 /* A C statement to build up a unique section name, expressed as a
14550    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
14551    RELOC indicates whether the initial value of EXP requires
14552    link-time relocations.  If you do not define this macro, GCC will use
14553    the symbol name prefixed by `.' as the section name.  Note - this
14554    macro can now be called for uninitialized data items as well as
14555    initialized data and functions.  */
14556
14557 static void
14558 rs6000_elf_unique_section (tree decl, int reloc)
14559 {
14560   /* As above, pretend that we're always building for a shared library
14561      when ABI_AIX, to avoid dynamic relocations in read-only sections.  */
14562   default_unique_section_1 (decl, reloc,
14563                             flag_pic || DEFAULT_ABI == ABI_AIX);
14564 }
14565 \f
14566 /* For a SYMBOL_REF, set generic flags and then perform some
14567    target-specific processing.
14568
14569    When the AIX ABI is requested on a non-AIX system, replace the
14570    function name with the real name (with a leading .) rather than the
14571    function descriptor name.  This saves a lot of overriding code to
14572    read the prefixes.  */
14573
14574 static void
14575 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
14576 {
14577   default_encode_section_info (decl, rtl, first);
14578
14579   if (first
14580       && TREE_CODE (decl) == FUNCTION_DECL
14581       && !TARGET_AIX
14582       && DEFAULT_ABI == ABI_AIX)
14583     {
14584       rtx sym_ref = XEXP (rtl, 0);
14585       size_t len = strlen (XSTR (sym_ref, 0));
14586       char *str = alloca (len + 2);
14587       str[0] = '.';
14588       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
14589       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
14590     }
14591 }
14592
14593 static bool
14594 rs6000_elf_in_small_data_p (tree decl)
14595 {
14596   if (rs6000_sdata == SDATA_NONE)
14597     return false;
14598
14599   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
14600     {
14601       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
14602       if (strcmp (section, ".sdata") == 0
14603           || strcmp (section, ".sdata2") == 0
14604           || strcmp (section, ".sbss") == 0
14605           || strcmp (section, ".sbss2") == 0
14606           || strcmp (section, ".PPC.EMB.sdata0") == 0
14607           || strcmp (section, ".PPC.EMB.sbss0") == 0)
14608         return true;
14609     }
14610   else
14611     {
14612       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
14613
14614       if (size > 0
14615           && (unsigned HOST_WIDE_INT) size <= g_switch_value
14616           /* If it's not public, and we're not going to reference it there,
14617              there's no need to put it in the small data section.  */
14618           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
14619         return true;
14620     }
14621
14622   return false;
14623 }
14624
14625 #endif /* USING_ELFOS_H */
14626
14627 \f
14628 /* Return a REG that occurs in ADDR with coefficient 1.
14629    ADDR can be effectively incremented by incrementing REG.
14630
14631    r0 is special and we must not select it as an address
14632    register by this routine since our caller will try to
14633    increment the returned register via an "la" instruction.  */
14634
14635 struct rtx_def *
14636 find_addr_reg (rtx addr)
14637 {
14638   while (GET_CODE (addr) == PLUS)
14639     {
14640       if (GET_CODE (XEXP (addr, 0)) == REG
14641           && REGNO (XEXP (addr, 0)) != 0)
14642         addr = XEXP (addr, 0);
14643       else if (GET_CODE (XEXP (addr, 1)) == REG
14644                && REGNO (XEXP (addr, 1)) != 0)
14645         addr = XEXP (addr, 1);
14646       else if (CONSTANT_P (XEXP (addr, 0)))
14647         addr = XEXP (addr, 1);
14648       else if (CONSTANT_P (XEXP (addr, 1)))
14649         addr = XEXP (addr, 0);
14650       else
14651         abort ();
14652     }
14653   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
14654     return addr;
14655   abort ();
14656 }
14657
14658 void
14659 rs6000_fatal_bad_address (rtx op)
14660 {
14661   fatal_insn ("bad address", op);
14662 }
14663
14664 #if TARGET_MACHO
14665
14666 #if 0
14667 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
14668    reference and a constant.  */
14669
14670 int
14671 symbolic_operand (rtx op)
14672 {
14673   switch (GET_CODE (op))
14674     {
14675     case SYMBOL_REF:
14676     case LABEL_REF:
14677       return 1;
14678     case CONST:
14679       op = XEXP (op, 0);
14680       return (GET_CODE (op) == SYMBOL_REF ||
14681               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
14682                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
14683               && GET_CODE (XEXP (op, 1)) == CONST_INT);
14684     default:
14685       return 0;
14686     }
14687 }
14688 #endif
14689
14690 #ifdef RS6000_LONG_BRANCH
14691
14692 static tree stub_list = 0;
14693
14694 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
14695    procedure calls to the linked list.  */
14696
14697 void 
14698 add_compiler_stub (tree label_name, tree function_name, int line_number)
14699 {
14700   tree stub = build_tree_list (function_name, label_name);
14701   TREE_TYPE (stub) = build_int_2 (line_number, 0);
14702   TREE_CHAIN (stub) = stub_list;
14703   stub_list = stub;
14704 }
14705
14706 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
14707 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
14708 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
14709
14710 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
14711    handling procedure calls from the linked list and initializes the
14712    linked list.  */
14713
14714 void
14715 output_compiler_stub (void)
14716 {
14717   char tmp_buf[256];
14718   char label_buf[256];
14719   tree stub;
14720
14721   if (!flag_pic)
14722     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
14723       {
14724         fprintf (asm_out_file,
14725                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
14726
14727 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
14728         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
14729           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
14730 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
14731
14732         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
14733           strcpy (label_buf,
14734                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
14735         else
14736           {
14737             label_buf[0] = '_';
14738             strcpy (label_buf+1,
14739                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
14740           }
14741
14742         strcpy (tmp_buf, "lis r12,hi16(");
14743         strcat (tmp_buf, label_buf);
14744         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
14745         strcat (tmp_buf, label_buf);
14746         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
14747         output_asm_insn (tmp_buf, 0);
14748
14749 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
14750         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
14751           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
14752 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
14753       }
14754
14755   stub_list = 0;
14756 }
14757
14758 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
14759    already there or not.  */
14760
14761 int
14762 no_previous_def (tree function_name)
14763 {
14764   tree stub;
14765   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
14766     if (function_name == STUB_FUNCTION_NAME (stub))
14767       return 0;
14768   return 1;
14769 }
14770
14771 /* GET_PREV_LABEL gets the label name from the previous definition of
14772    the function.  */
14773
14774 tree
14775 get_prev_label (tree function_name)
14776 {
14777   tree stub;
14778   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
14779     if (function_name == STUB_FUNCTION_NAME (stub))
14780       return STUB_LABEL_NAME (stub);
14781   return 0;
14782 }
14783
14784 /* INSN is either a function call or a millicode call.  It may have an
14785    unconditional jump in its delay slot.  
14786
14787    CALL_DEST is the routine we are calling.  */
14788
14789 char *
14790 output_call (rtx insn, rtx call_dest, int operand_number)
14791 {
14792   static char buf[256];
14793   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
14794     {
14795       tree labelname;
14796       tree funname = get_identifier (XSTR (call_dest, 0));
14797       
14798       if (no_previous_def (funname))
14799         {
14800           int line_number = 0;
14801           rtx label_rtx = gen_label_rtx ();
14802           char *label_buf, temp_buf[256];
14803           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
14804                                        CODE_LABEL_NUMBER (label_rtx));
14805           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
14806           labelname = get_identifier (label_buf);
14807           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
14808           if (insn)
14809             line_number = NOTE_LINE_NUMBER (insn);
14810           add_compiler_stub (labelname, funname, line_number);
14811         }
14812       else
14813         labelname = get_prev_label (funname);
14814
14815       sprintf (buf, "jbsr %%z%d,%.246s",
14816                operand_number, IDENTIFIER_POINTER (labelname));
14817       return buf;
14818     }
14819   else
14820     {
14821       sprintf (buf, "bl %%z%d", operand_number);
14822       return buf;
14823     }
14824 }
14825
14826 #endif /* RS6000_LONG_BRANCH */
14827
14828 /* Generate PIC and indirect symbol stubs.  */
14829
14830 void
14831 machopic_output_stub (FILE *file, const char *symb, const char *stub)
14832 {
14833   unsigned int length;
14834   char *symbol_name, *lazy_ptr_name;
14835   char *local_label_0;
14836   static int label = 0;
14837
14838   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
14839   symb = (*targetm.strip_name_encoding) (symb);
14840
14841
14842   length = strlen (symb);
14843   symbol_name = alloca (length + 32);
14844   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
14845
14846   lazy_ptr_name = alloca (length + 32);
14847   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
14848
14849   if (flag_pic == 2)
14850     machopic_picsymbol_stub1_section ();
14851   else
14852     machopic_symbol_stub1_section ();
14853   fprintf (file, "\t.align 2\n");
14854
14855   fprintf (file, "%s:\n", stub);
14856   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14857
14858   if (flag_pic == 2)
14859     {
14860       label++;
14861       local_label_0 = alloca (sizeof("\"L0000000000$spb\""));
14862       sprintf (local_label_0, "\"L%011d$spb\"", label);
14863     
14864       fprintf (file, "\tmflr r0\n");
14865       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
14866       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
14867       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
14868                lazy_ptr_name, local_label_0);
14869       fprintf (file, "\tmtlr r0\n");
14870       fprintf (file, "\tlwzu r12,lo16(%s-%s)(r11)\n",
14871                lazy_ptr_name, local_label_0);
14872       fprintf (file, "\tmtctr r12\n");
14873       fprintf (file, "\tbctr\n");
14874     }
14875   else
14876    {
14877      fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
14878      fprintf (file, "\tlwzu r12,lo16(%s)(r11)\n", lazy_ptr_name);
14879      fprintf (file, "\tmtctr r12\n");
14880      fprintf (file, "\tbctr\n");
14881    }
14882   
14883   machopic_lazy_symbol_ptr_section ();
14884   fprintf (file, "%s:\n", lazy_ptr_name);
14885   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14886   fprintf (file, "\t.long dyld_stub_binding_helper\n");
14887 }
14888
14889 /* Legitimize PIC addresses.  If the address is already
14890    position-independent, we return ORIG.  Newly generated
14891    position-independent addresses go into a reg.  This is REG if non
14892    zero, otherwise we allocate register(s) as necessary.  */
14893
14894 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
14895
14896 rtx
14897 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, 
14898                                         rtx reg)
14899 {
14900   rtx base, offset;
14901
14902   if (reg == NULL && ! reload_in_progress && ! reload_completed)
14903     reg = gen_reg_rtx (Pmode);
14904
14905   if (GET_CODE (orig) == CONST)
14906     {
14907       if (GET_CODE (XEXP (orig, 0)) == PLUS
14908           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
14909         return orig;
14910
14911       if (GET_CODE (XEXP (orig, 0)) == PLUS)
14912         {
14913           /* Use a different reg for the intermediate value, as
14914              it will be marked UNCHANGING. */
14915           rtx reg_temp = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
14916
14917           base =
14918             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
14919                                                     Pmode, reg_temp);
14920           offset =
14921             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
14922                                                     Pmode, reg);
14923         }
14924       else
14925         abort ();
14926
14927       if (GET_CODE (offset) == CONST_INT)
14928         {
14929           if (SMALL_INT (offset))
14930             return plus_constant (base, INTVAL (offset));
14931           else if (! reload_in_progress && ! reload_completed)
14932             offset = force_reg (Pmode, offset);
14933           else
14934             {
14935               rtx mem = force_const_mem (Pmode, orig);
14936               return machopic_legitimize_pic_address (mem, Pmode, reg);
14937             }
14938         }
14939       return gen_rtx (PLUS, Pmode, base, offset);
14940     }
14941
14942   /* Fall back on generic machopic code.  */
14943   return machopic_legitimize_pic_address (orig, mode, reg);
14944 }
14945
14946 /* This is just a placeholder to make linking work without having to
14947    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
14948    ever needed for Darwin (not too likely!) this would have to get a
14949    real definition.  */
14950
14951 void
14952 toc_section (void)
14953 {
14954 }
14955
14956 #endif /* TARGET_MACHO */
14957
14958 #if TARGET_ELF
14959 static unsigned int
14960 rs6000_elf_section_type_flags (tree decl, const char *name, int reloc)
14961 {
14962   unsigned int flags
14963     = default_section_type_flags_1 (decl, name, reloc,
14964                                     flag_pic || DEFAULT_ABI == ABI_AIX);
14965
14966   if (TARGET_RELOCATABLE)
14967     flags |= SECTION_WRITE;
14968
14969   return flags;
14970 }
14971
14972 /* Record an element in the table of global constructors.  SYMBOL is
14973    a SYMBOL_REF of the function to be called; PRIORITY is a number
14974    between 0 and MAX_INIT_PRIORITY.
14975
14976    This differs from default_named_section_asm_out_constructor in
14977    that we have special handling for -mrelocatable.  */
14978
14979 static void
14980 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
14981 {
14982   const char *section = ".ctors";
14983   char buf[16];
14984
14985   if (priority != DEFAULT_INIT_PRIORITY)
14986     {
14987       sprintf (buf, ".ctors.%.5u",
14988                /* Invert the numbering so the linker puts us in the proper
14989                   order; constructors are run from right to left, and the
14990                   linker sorts in increasing order.  */
14991                MAX_INIT_PRIORITY - priority);
14992       section = buf;
14993     }
14994
14995   named_section_flags (section, SECTION_WRITE);
14996   assemble_align (POINTER_SIZE);
14997
14998   if (TARGET_RELOCATABLE)
14999     {
15000       fputs ("\t.long (", asm_out_file);
15001       output_addr_const (asm_out_file, symbol);
15002       fputs (")@fixup\n", asm_out_file);
15003     }
15004   else
15005     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
15006 }
15007
15008 static void
15009 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
15010 {
15011   const char *section = ".dtors";
15012   char buf[16];
15013
15014   if (priority != DEFAULT_INIT_PRIORITY)
15015     {
15016       sprintf (buf, ".dtors.%.5u",
15017                /* Invert the numbering so the linker puts us in the proper
15018                   order; constructors are run from right to left, and the
15019                   linker sorts in increasing order.  */
15020                MAX_INIT_PRIORITY - priority);
15021       section = buf;
15022     }
15023
15024   named_section_flags (section, SECTION_WRITE);
15025   assemble_align (POINTER_SIZE);
15026
15027   if (TARGET_RELOCATABLE)
15028     {
15029       fputs ("\t.long (", asm_out_file);
15030       output_addr_const (asm_out_file, symbol);
15031       fputs (")@fixup\n", asm_out_file);
15032     }
15033   else
15034     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
15035 }
15036
15037 void
15038 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
15039 {
15040   if (TARGET_64BIT)
15041     {
15042       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
15043       ASM_OUTPUT_LABEL (file, name);
15044       fputs (DOUBLE_INT_ASM_OP, file);
15045       putc ('.', file);
15046       assemble_name (file, name);
15047       fputs (",.TOC.@tocbase,0\n\t.previous\n\t.size\t", file);
15048       assemble_name (file, name);
15049       fputs (",24\n\t.type\t.", file);
15050       assemble_name (file, name);
15051       fputs (",@function\n", file);
15052       if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
15053         {
15054           fputs ("\t.globl\t.", file);
15055           assemble_name (file, name);
15056           putc ('\n', file);
15057         }
15058       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
15059       putc ('.', file);
15060       ASM_OUTPUT_LABEL (file, name);
15061       return;
15062     }
15063
15064   if (TARGET_RELOCATABLE
15065       && (get_pool_size () != 0 || current_function_profile)
15066       && uses_TOC())
15067     {
15068       char buf[256];
15069
15070       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
15071
15072       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
15073       fprintf (file, "\t.long ");
15074       assemble_name (file, buf);
15075       putc ('-', file);
15076       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
15077       assemble_name (file, buf);
15078       putc ('\n', file);
15079     }
15080
15081   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
15082   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
15083
15084   if (DEFAULT_ABI == ABI_AIX)
15085     {
15086       const char *desc_name, *orig_name;
15087
15088       orig_name = (*targetm.strip_name_encoding) (name);
15089       desc_name = orig_name;
15090       while (*desc_name == '.')
15091         desc_name++;
15092
15093       if (TREE_PUBLIC (decl))
15094         fprintf (file, "\t.globl %s\n", desc_name);
15095
15096       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
15097       fprintf (file, "%s:\n", desc_name);
15098       fprintf (file, "\t.long %s\n", orig_name);
15099       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
15100       if (DEFAULT_ABI == ABI_AIX)
15101         fputs ("\t.long 0\n", file);
15102       fprintf (file, "\t.previous\n");
15103     }
15104   ASM_OUTPUT_LABEL (file, name);
15105 }
15106 #endif
15107
15108 #if TARGET_XCOFF
15109 static void
15110 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
15111 {
15112   fputs (GLOBAL_ASM_OP, stream);
15113   RS6000_OUTPUT_BASENAME (stream, name);
15114   putc ('\n', stream);
15115 }
15116
15117 static void
15118 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags)
15119 {
15120   int smclass;
15121   static const char * const suffix[3] = { "PR", "RO", "RW" };
15122
15123   if (flags & SECTION_CODE)
15124     smclass = 0;
15125   else if (flags & SECTION_WRITE)
15126     smclass = 2;
15127   else
15128     smclass = 1;
15129
15130   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
15131            (flags & SECTION_CODE) ? "." : "",
15132            name, suffix[smclass], flags & SECTION_ENTSIZE);
15133 }
15134
15135 static void
15136 rs6000_xcoff_select_section (tree decl, int reloc, 
15137                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
15138 {
15139   if (decl_readonly_section_1 (decl, reloc, 1))
15140     {
15141       if (TREE_PUBLIC (decl))
15142         read_only_data_section ();
15143       else
15144         read_only_private_data_section ();
15145     }
15146   else
15147     {
15148       if (TREE_PUBLIC (decl))
15149         data_section ();
15150       else
15151         private_data_section ();
15152     }
15153 }
15154
15155 static void
15156 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
15157 {
15158   const char *name;
15159
15160   /* Use select_section for private and uninitialized data.  */
15161   if (!TREE_PUBLIC (decl)
15162       || DECL_COMMON (decl)
15163       || DECL_INITIAL (decl) == NULL_TREE
15164       || DECL_INITIAL (decl) == error_mark_node
15165       || (flag_zero_initialized_in_bss
15166           && initializer_zerop (DECL_INITIAL (decl))))
15167     return;
15168
15169   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15170   name = (*targetm.strip_name_encoding) (name);
15171   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
15172 }
15173
15174 /* Select section for constant in constant pool.
15175
15176    On RS/6000, all constants are in the private read-only data area.
15177    However, if this is being placed in the TOC it must be output as a
15178    toc entry.  */
15179
15180 static void
15181 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x, 
15182                                 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
15183 {
15184   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
15185     toc_section ();
15186   else
15187     read_only_private_data_section ();
15188 }
15189
15190 /* Remove any trailing [DS] or the like from the symbol name.  */
15191
15192 static const char *
15193 rs6000_xcoff_strip_name_encoding (const char *name)
15194 {
15195   size_t len;
15196   if (*name == '*')
15197     name++;
15198   len = strlen (name);
15199   if (name[len - 1] == ']')
15200     return ggc_alloc_string (name, len - 4);
15201   else
15202     return name;
15203 }
15204
15205 /* Section attributes.  AIX is always PIC.  */
15206
15207 static unsigned int
15208 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
15209 {
15210   unsigned int align;
15211   unsigned int flags = default_section_type_flags_1 (decl, name, reloc, 1);
15212
15213   /* Align to at least UNIT size.  */
15214   if (flags & SECTION_CODE)
15215     align = MIN_UNITS_PER_WORD;
15216   else
15217     /* Increase alignment of large objects if not already stricter.  */
15218     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
15219                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
15220                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
15221
15222   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
15223 }
15224
15225 /* Output at beginning of assembler file.
15226
15227    Initialize the section names for the RS/6000 at this point.
15228
15229    Specify filename, including full path, to assembler.
15230
15231    We want to go into the TOC section so at least one .toc will be emitted.
15232    Also, in order to output proper .bs/.es pairs, we need at least one static
15233    [RW] section emitted.
15234
15235    Finally, declare mcount when profiling to make the assembler happy.  */
15236
15237 static void
15238 rs6000_xcoff_file_start (void)
15239 {
15240   rs6000_gen_section_name (&xcoff_bss_section_name,
15241                            main_input_filename, ".bss_");
15242   rs6000_gen_section_name (&xcoff_private_data_section_name,
15243                            main_input_filename, ".rw_");
15244   rs6000_gen_section_name (&xcoff_read_only_section_name,
15245                            main_input_filename, ".ro_");
15246
15247   fputs ("\t.file\t", asm_out_file);
15248   output_quoted_string (asm_out_file, main_input_filename);
15249   fputc ('\n', asm_out_file);
15250   toc_section ();
15251   if (write_symbols != NO_DEBUG)
15252     private_data_section ();
15253   text_section ();
15254   if (profile_flag)
15255     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
15256   rs6000_file_start ();
15257 }
15258
15259 /* Output at end of assembler file.
15260    On the RS/6000, referencing data should automatically pull in text.  */
15261
15262 static void
15263 rs6000_xcoff_file_end (void)
15264 {
15265   text_section ();
15266   fputs ("_section_.text:\n", asm_out_file);
15267   data_section ();
15268   fputs (TARGET_32BIT
15269          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
15270          asm_out_file);
15271 }
15272 #endif /* TARGET_XCOFF */
15273
15274 #if TARGET_MACHO
15275 /* Cross-module name binding.  Darwin does not support overriding
15276    functions at dynamic-link time.  */
15277
15278 static bool
15279 rs6000_binds_local_p (tree decl)
15280 {
15281   return default_binds_local_p_1 (decl, 0);
15282 }
15283 #endif
15284
15285 /* Compute a (partial) cost for rtx X.  Return true if the complete
15286    cost has been computed, and false if subexpressions should be
15287    scanned.  In either case, *TOTAL contains the cost result.  */
15288
15289 static bool
15290 rs6000_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, 
15291                   int *total)
15292 {
15293   switch (code)
15294     {
15295       /* On the RS/6000, if it is valid in the insn, it is free.
15296          So this always returns 0.  */
15297     case CONST_INT:
15298     case CONST:
15299     case LABEL_REF:
15300     case SYMBOL_REF:
15301     case CONST_DOUBLE:
15302     case HIGH:
15303       *total = 0;
15304       return true;
15305
15306     case PLUS:
15307       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
15308                  && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1))
15309                                                + 0x8000) >= 0x10000)
15310                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
15311                 ? COSTS_N_INSNS (2)
15312                 : COSTS_N_INSNS (1));
15313       return true;
15314
15315     case AND:
15316     case IOR:
15317     case XOR:
15318       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
15319                  && (INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff)) != 0
15320                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
15321                 ? COSTS_N_INSNS (2)
15322                 : COSTS_N_INSNS (1));
15323       return true;
15324
15325     case MULT:
15326       if (optimize_size)
15327         {
15328           *total = COSTS_N_INSNS (2);
15329           return true;
15330         }
15331       switch (rs6000_cpu)
15332         {
15333         case PROCESSOR_RIOS1:
15334         case PROCESSOR_PPC405:
15335           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15336                     ? COSTS_N_INSNS (5)
15337                     : (INTVAL (XEXP (x, 1)) >= -256
15338                        && INTVAL (XEXP (x, 1)) <= 255)
15339                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
15340           return true;
15341
15342         case PROCESSOR_PPC440:
15343           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15344                     ? COSTS_N_INSNS (3)
15345                     : COSTS_N_INSNS (2));
15346           return true;
15347
15348         case PROCESSOR_RS64A:
15349           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15350                     ? GET_MODE (XEXP (x, 1)) != DImode
15351                     ? COSTS_N_INSNS (20) : COSTS_N_INSNS (34)
15352                     : (INTVAL (XEXP (x, 1)) >= -256
15353                        && INTVAL (XEXP (x, 1)) <= 255)
15354                     ? COSTS_N_INSNS (8) : COSTS_N_INSNS (12));
15355           return true;
15356
15357         case PROCESSOR_RIOS2:
15358         case PROCESSOR_MPCCORE:
15359         case PROCESSOR_PPC604e:
15360           *total = COSTS_N_INSNS (2);
15361           return true;
15362
15363         case PROCESSOR_PPC601:
15364           *total = COSTS_N_INSNS (5);
15365           return true;
15366
15367         case PROCESSOR_PPC603:
15368         case PROCESSOR_PPC7400:
15369         case PROCESSOR_PPC750:
15370           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15371                     ? COSTS_N_INSNS (5)
15372                     : (INTVAL (XEXP (x, 1)) >= -256
15373                        && INTVAL (XEXP (x, 1)) <= 255)
15374                     ? COSTS_N_INSNS (2) : COSTS_N_INSNS (3));
15375           return true;
15376
15377         case PROCESSOR_PPC7450:
15378           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15379                     ? COSTS_N_INSNS (4)
15380                     : COSTS_N_INSNS (3));
15381           return true;
15382
15383         case PROCESSOR_PPC403:
15384         case PROCESSOR_PPC604:
15385         case PROCESSOR_PPC8540:
15386           *total = COSTS_N_INSNS (4);
15387           return true;
15388
15389         case PROCESSOR_PPC620:
15390         case PROCESSOR_PPC630:
15391           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15392                     ? GET_MODE (XEXP (x, 1)) != DImode
15393                     ? COSTS_N_INSNS (5) : COSTS_N_INSNS (7)
15394                     : (INTVAL (XEXP (x, 1)) >= -256
15395                        && INTVAL (XEXP (x, 1)) <= 255)
15396                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
15397           return true;
15398
15399         case PROCESSOR_POWER4:
15400           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15401                     ? GET_MODE (XEXP (x, 1)) != DImode
15402                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4)
15403                     : COSTS_N_INSNS (2));
15404           return true;
15405
15406         default:
15407           abort ();
15408         }
15409
15410     case DIV:
15411     case MOD:
15412       if (GET_CODE (XEXP (x, 1)) == CONST_INT
15413           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
15414         {
15415           *total = COSTS_N_INSNS (2);
15416           return true;
15417         }
15418       /* FALLTHRU */
15419
15420     case UDIV:
15421     case UMOD:
15422       switch (rs6000_cpu)
15423         {
15424         case PROCESSOR_RIOS1:
15425           *total = COSTS_N_INSNS (19);
15426           return true;
15427
15428         case PROCESSOR_RIOS2:
15429           *total = COSTS_N_INSNS (13);
15430           return true;
15431
15432         case PROCESSOR_RS64A:
15433           *total = (GET_MODE (XEXP (x, 1)) != DImode
15434                     ? COSTS_N_INSNS (65)
15435                     : COSTS_N_INSNS (67));
15436           return true;
15437
15438         case PROCESSOR_MPCCORE:
15439           *total = COSTS_N_INSNS (6);
15440           return true;
15441
15442         case PROCESSOR_PPC403:
15443           *total = COSTS_N_INSNS (33);
15444           return true;
15445
15446         case PROCESSOR_PPC405:
15447           *total = COSTS_N_INSNS (35);
15448           return true;
15449
15450         case PROCESSOR_PPC440:
15451           *total = COSTS_N_INSNS (34);
15452           return true;
15453
15454         case PROCESSOR_PPC601:
15455           *total = COSTS_N_INSNS (36);
15456           return true;
15457
15458         case PROCESSOR_PPC603:
15459           *total = COSTS_N_INSNS (37);
15460           return true;
15461
15462         case PROCESSOR_PPC604:
15463         case PROCESSOR_PPC604e:
15464           *total = COSTS_N_INSNS (20);
15465           return true;
15466
15467         case PROCESSOR_PPC620:
15468         case PROCESSOR_PPC630:
15469           *total = (GET_MODE (XEXP (x, 1)) != DImode
15470                     ? COSTS_N_INSNS (21)
15471                     : COSTS_N_INSNS (37));
15472           return true;
15473
15474         case PROCESSOR_PPC750:
15475         case PROCESSOR_PPC8540:
15476         case PROCESSOR_PPC7400:
15477           *total = COSTS_N_INSNS (19);
15478           return true;
15479
15480         case PROCESSOR_PPC7450:
15481           *total = COSTS_N_INSNS (23);
15482           return true;
15483
15484         case PROCESSOR_POWER4:
15485           *total = (GET_MODE (XEXP (x, 1)) != DImode
15486                     ? COSTS_N_INSNS (18)
15487                     : COSTS_N_INSNS (34));
15488           return true;
15489
15490         default:
15491           abort ();
15492         }
15493
15494     case FFS:
15495       *total = COSTS_N_INSNS (4);
15496       return true;
15497
15498     case MEM:
15499       /* MEM should be slightly more expensive than (plus (reg) (const)) */
15500       *total = 5;
15501       return true;
15502
15503     default:
15504       return false;
15505     }
15506 }
15507
15508 /* A C expression returning the cost of moving data from a register of class
15509    CLASS1 to one of CLASS2.  */
15510
15511 int
15512 rs6000_register_move_cost (enum machine_mode mode, 
15513                            enum reg_class from, enum reg_class to)
15514 {
15515   /*  Moves from/to GENERAL_REGS.  */
15516   if (reg_classes_intersect_p (to, GENERAL_REGS)
15517       || reg_classes_intersect_p (from, GENERAL_REGS))
15518     {
15519       if (! reg_classes_intersect_p (to, GENERAL_REGS))
15520         from = to;
15521
15522       if (from == FLOAT_REGS || from == ALTIVEC_REGS)
15523         return (rs6000_memory_move_cost (mode, from, 0)
15524                 + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
15525
15526 /* It's more expensive to move CR_REGS than CR0_REGS because of the shift...*/
15527       else if (from == CR_REGS)
15528         return 4;
15529
15530       else
15531 /* A move will cost one instruction per GPR moved.  */
15532         return 2 * HARD_REGNO_NREGS (0, mode);
15533     }
15534
15535 /* Moving between two similar registers is just one instruction.  */
15536   else if (reg_classes_intersect_p (to, from))
15537     return mode == TFmode ? 4 : 2;
15538
15539 /* Everything else has to go through GENERAL_REGS.  */
15540   else
15541     return (rs6000_register_move_cost (mode, GENERAL_REGS, to) 
15542             + rs6000_register_move_cost (mode, from, GENERAL_REGS));
15543 }
15544
15545 /* A C expressions returning the cost of moving data of MODE from a register to
15546    or from memory.  */
15547
15548 int
15549 rs6000_memory_move_cost (enum machine_mode mode, enum reg_class class, 
15550                          int in ATTRIBUTE_UNUSED)
15551 {
15552   if (reg_classes_intersect_p (class, GENERAL_REGS))
15553     return 4 * HARD_REGNO_NREGS (0, mode);
15554   else if (reg_classes_intersect_p (class, FLOAT_REGS))
15555     return 4 * HARD_REGNO_NREGS (32, mode);
15556   else if (reg_classes_intersect_p (class, ALTIVEC_REGS))
15557     return 4 * HARD_REGNO_NREGS (FIRST_ALTIVEC_REGNO, mode);
15558   else
15559     return 4 + rs6000_register_move_cost (mode, class, GENERAL_REGS);
15560 }
15561
15562 /* Return an RTX representing where to find the function value of a
15563    function returning MODE.  */
15564 static rtx
15565 rs6000_complex_function_value (enum machine_mode mode)
15566 {
15567   unsigned int regno;
15568   rtx r1, r2;
15569   enum machine_mode inner = GET_MODE_INNER (mode);
15570
15571   if (FLOAT_MODE_P (mode))
15572     regno = FP_ARG_RETURN;
15573   else
15574     {
15575       regno = GP_ARG_RETURN;
15576
15577       /* 32-bit is OK since it'll go in r3/r4.  */
15578       if (TARGET_32BIT
15579           && GET_MODE_BITSIZE (inner) >= 32)
15580         return gen_rtx_REG (mode, regno);
15581     }
15582
15583   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
15584                           const0_rtx);
15585   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
15586                           GEN_INT (GET_MODE_UNIT_SIZE (inner)));
15587   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
15588 }
15589
15590 /* Define how to find the value returned by a function.
15591    VALTYPE is the data type of the value (as a tree).
15592    If the precise function being called is known, FUNC is its FUNCTION_DECL;
15593    otherwise, FUNC is 0.
15594
15595    On the SPE, both FPs and vectors are returned in r3.
15596
15597    On RS/6000 an integer value is in r3 and a floating-point value is in
15598    fp1, unless -msoft-float.  */
15599
15600 rtx
15601 rs6000_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
15602 {
15603   enum machine_mode mode;
15604   unsigned int regno;
15605
15606   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
15607     {
15608       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
15609       return gen_rtx_PARALLEL (DImode,
15610         gen_rtvec (2,
15611                    gen_rtx_EXPR_LIST (VOIDmode,
15612                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
15613                                       const0_rtx),
15614                    gen_rtx_EXPR_LIST (VOIDmode,
15615                                       gen_rtx_REG (SImode,
15616                                                    GP_ARG_RETURN + 1),
15617                                       GEN_INT (4))));
15618     }
15619
15620   if ((INTEGRAL_TYPE_P (valtype)
15621        && TYPE_PRECISION (valtype) < BITS_PER_WORD)
15622       || POINTER_TYPE_P (valtype))
15623     mode = TARGET_32BIT ? SImode : DImode;
15624   else
15625     mode = TYPE_MODE (valtype);
15626
15627   if (TREE_CODE (valtype) == REAL_TYPE && TARGET_HARD_FLOAT && TARGET_FPRS)
15628     regno = FP_ARG_RETURN;
15629   else if (TREE_CODE (valtype) == COMPLEX_TYPE
15630            && TARGET_HARD_FLOAT
15631            && SPLIT_COMPLEX_ARGS)
15632     return rs6000_complex_function_value (mode);
15633   else if (TREE_CODE (valtype) == VECTOR_TYPE && TARGET_ALTIVEC)
15634     regno = ALTIVEC_ARG_RETURN;
15635   else
15636     regno = GP_ARG_RETURN;
15637
15638   return gen_rtx_REG (mode, regno);
15639 }
15640
15641 /* Define how to find the value returned by a library function
15642    assuming the value has mode MODE.  */
15643 rtx
15644 rs6000_libcall_value (enum machine_mode mode)
15645 {
15646   unsigned int regno;
15647
15648   if (GET_MODE_CLASS (mode) == MODE_FLOAT
15649            && TARGET_HARD_FLOAT && TARGET_FPRS)
15650     regno = FP_ARG_RETURN;
15651   else if (ALTIVEC_VECTOR_MODE (mode))
15652     regno = ALTIVEC_ARG_RETURN;
15653   else if (COMPLEX_MODE_P (mode) && SPLIT_COMPLEX_ARGS)
15654     return rs6000_complex_function_value (mode);
15655   else
15656     regno = GP_ARG_RETURN;
15657
15658   return gen_rtx_REG (mode, regno);
15659 }
15660
15661 /* Define the offset between two registers, FROM to be eliminated and its
15662    replacement TO, at the start of a routine.  */
15663 HOST_WIDE_INT
15664 rs6000_initial_elimination_offset (int from, int to)
15665 {
15666   rs6000_stack_t *info = rs6000_stack_info ();
15667   HOST_WIDE_INT offset;
15668
15669   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
15670     offset = info->push_p ? 0 : -info->total_size;
15671   else if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
15672     offset = info->total_size;
15673   else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
15674     offset = info->push_p ? info->total_size : 0;
15675   else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
15676     offset = 0;
15677   else
15678     abort ();
15679
15680   return offset;
15681 }
15682
15683 /* Return true if TYPE is of type __ev64_opaque__.  */
15684
15685 static bool
15686 is_ev64_opaque_type (tree type)
15687 {
15688   return (TARGET_SPE
15689           && (type == opaque_V2SI_type_node
15690               || type == opaque_V2SF_type_node
15691               || type == opaque_p_V2SI_type_node));
15692 }
15693
15694 static rtx
15695 rs6000_dwarf_register_span (rtx reg)
15696 {
15697   unsigned regno;
15698
15699   if (!TARGET_SPE || !SPE_VECTOR_MODE (GET_MODE (reg)))
15700     return NULL_RTX;
15701
15702   regno = REGNO (reg);
15703
15704   /* The duality of the SPE register size wreaks all kinds of havoc.
15705      This is a way of distinguishing r0 in 32-bits from r0 in
15706      64-bits.  */
15707   return
15708     gen_rtx_PARALLEL (VOIDmode,
15709                       BYTES_BIG_ENDIAN
15710                       ? gen_rtvec (2,
15711                                    gen_rtx_REG (SImode, regno + 1200),
15712                                    gen_rtx_REG (SImode, regno))
15713                       : gen_rtvec (2,
15714                                    gen_rtx_REG (SImode, regno),
15715                                    gen_rtx_REG (SImode, regno + 1200)));
15716 }
15717
15718 #include "gt-rs6000.h"