OSDN Git Service

* alias.c (adjust_offset_for_component_ref): Use
[pf3gnuchains/gcc-fork.git] / gcc / config / rs6000 / rs6000.c
1 /* Subroutines used for code generation on IBM RS/6000.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
3    2000, 2001, 2002, 2003, 2004 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 #include "tree-gimple.h"
56 #if TARGET_XCOFF
57 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
58 #endif
59
60 #ifndef TARGET_NO_PROTOTYPE
61 #define TARGET_NO_PROTOTYPE 0
62 #endif
63
64 #define EASY_VECTOR_15(n) ((n) >= -16 && (n) <= 15)
65 #define EASY_VECTOR_15_ADD_SELF(n) ((n) >= 0x10 && (n) <= 0x1e \
66                                           && !((n) & 1))
67
68 #define min(A,B)        ((A) < (B) ? (A) : (B))
69 #define max(A,B)        ((A) > (B) ? (A) : (B))
70
71 /* Structure used to define the rs6000 stack */
72 typedef struct rs6000_stack {
73   int first_gp_reg_save;        /* first callee saved GP register used */
74   int first_fp_reg_save;        /* first callee saved FP register used */
75   int first_altivec_reg_save;   /* first callee saved AltiVec register used */
76   int lr_save_p;                /* true if the link reg needs to be saved */
77   int cr_save_p;                /* true if the CR reg needs to be saved */
78   unsigned int vrsave_mask;     /* mask of vec registers to save */
79   int toc_save_p;               /* true if the TOC needs to be saved */
80   int push_p;                   /* true if we need to allocate stack space */
81   int calls_p;                  /* true if the function makes any calls */
82   enum rs6000_abi abi;          /* which ABI to use */
83   int gp_save_offset;           /* offset to save GP regs from initial SP */
84   int fp_save_offset;           /* offset to save FP regs from initial SP */
85   int altivec_save_offset;      /* offset to save AltiVec regs from initial SP */
86   int lr_save_offset;           /* offset to save LR from initial SP */
87   int cr_save_offset;           /* offset to save CR from initial SP */
88   int vrsave_save_offset;       /* offset to save VRSAVE from initial SP */
89   int spe_gp_save_offset;       /* offset to save spe 64-bit gprs  */
90   int toc_save_offset;          /* offset to save the TOC pointer */
91   int varargs_save_offset;      /* offset to save the varargs registers */
92   int ehrd_offset;              /* offset to EH return data */
93   int reg_size;                 /* register size (4 or 8) */
94   int varargs_size;             /* size to hold V.4 args passed in regs */
95   HOST_WIDE_INT vars_size;      /* variable save area size */
96   int parm_size;                /* outgoing parameter size */
97   int save_size;                /* save area size */
98   int fixed_size;               /* fixed size of stack frame */
99   int gp_size;                  /* size of saved GP registers */
100   int fp_size;                  /* size of saved FP registers */
101   int altivec_size;             /* size of saved AltiVec registers */
102   int cr_size;                  /* size to hold CR if not in save_size */
103   int lr_size;                  /* size to hold LR if not in save_size */
104   int vrsave_size;              /* size to hold VRSAVE if not in save_size */
105   int altivec_padding_size;     /* size of altivec alignment padding if
106                                    not in save_size */
107   int spe_gp_size;              /* size of 64-bit GPR save size for SPE */
108   int spe_padding_size;
109   int toc_size;                 /* size to hold TOC if not in save_size */
110   HOST_WIDE_INT total_size;     /* total bytes allocated for stack */
111   int spe_64bit_regs_used;
112 } rs6000_stack_t;
113
114 /* Target cpu type */
115
116 enum processor_type rs6000_cpu;
117 struct rs6000_cpu_select rs6000_select[3] =
118 {
119   /* switch             name,                   tune    arch */
120   { (const char *)0,    "--with-cpu=",          1,      1 },
121   { (const char *)0,    "-mcpu=",               1,      1 },
122   { (const char *)0,    "-mtune=",              1,      0 },
123 };
124
125 /* Always emit branch hint bits.  */
126 static GTY(()) bool rs6000_always_hint;
127
128 /* Schedule instructions for group formation.  */
129 static GTY(()) bool rs6000_sched_groups;
130
131 /* Support adjust_priority scheduler hook 
132    and -mprioritize-restricted-insns= option.  */
133 const char *rs6000_sched_restricted_insns_priority_str;
134 int rs6000_sched_restricted_insns_priority;
135
136 /* Support for -msched-costly-dep option.  */
137 const char *rs6000_sched_costly_dep_str;
138 enum rs6000_dependence_cost rs6000_sched_costly_dep;
139
140 /* Support for -minsert-sched-nops option.  */
141 const char *rs6000_sched_insert_nops_str;
142 enum rs6000_nop_insertion rs6000_sched_insert_nops;
143
144 /* Size of long double */
145 const char *rs6000_long_double_size_string;
146 int rs6000_long_double_type_size;
147
148 /* Whether -mabi=altivec has appeared */
149 int rs6000_altivec_abi;
150
151 /* Whether VRSAVE instructions should be generated.  */
152 int rs6000_altivec_vrsave;
153
154 /* String from -mvrsave= option.  */
155 const char *rs6000_altivec_vrsave_string;
156
157 /* Nonzero if we want SPE ABI extensions.  */
158 int rs6000_spe_abi;
159
160 /* Whether isel instructions should be generated.  */
161 int rs6000_isel;
162
163 /* Whether SPE simd instructions should be generated.  */
164 int rs6000_spe;
165
166 /* Nonzero if floating point operations are done in the GPRs.  */
167 int rs6000_float_gprs = 0;
168
169 /* String from -mfloat-gprs=.  */
170 const char *rs6000_float_gprs_string;
171
172 /* String from -misel=.  */
173 const char *rs6000_isel_string;
174
175 /* String from -mspe=.  */
176 const char *rs6000_spe_string;
177
178 /* Set to nonzero once AIX common-mode calls have been defined.  */
179 static GTY(()) int common_mode_defined;
180
181 /* Save information from a "cmpxx" operation until the branch or scc is
182    emitted.  */
183 rtx rs6000_compare_op0, rs6000_compare_op1;
184 int rs6000_compare_fp_p;
185
186 /* Label number of label created for -mrelocatable, to call to so we can
187    get the address of the GOT section */
188 int rs6000_pic_labelno;
189
190 #ifdef USING_ELFOS_H
191 /* Which abi to adhere to */
192 const char *rs6000_abi_name;
193
194 /* Semantics of the small data area */
195 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
196
197 /* Which small data model to use */
198 const char *rs6000_sdata_name = (char *)0;
199
200 /* Counter for labels which are to be placed in .fixup.  */
201 int fixuplabelno = 0;
202 #endif
203
204 /* Bit size of immediate TLS offsets and string from which it is decoded.  */
205 int rs6000_tls_size = 32;
206 const char *rs6000_tls_size_string;
207
208 /* ABI enumeration available for subtarget to use.  */
209 enum rs6000_abi rs6000_current_abi;
210
211 /* ABI string from -mabi= option.  */
212 const char *rs6000_abi_string;
213
214 /* Debug flags */
215 const char *rs6000_debug_name;
216 int rs6000_debug_stack;         /* debug stack applications */
217 int rs6000_debug_arg;           /* debug argument handling */
218
219 /* Value is TRUE if register/mode pair is accepatable.  */
220 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
221
222 /* Opaque types.  */
223 static GTY(()) tree opaque_V2SI_type_node;
224 static GTY(()) tree opaque_V2SF_type_node;
225 static GTY(()) tree opaque_p_V2SI_type_node;
226 static GTY(()) tree V16QI_type_node;
227 static GTY(()) tree V2SI_type_node;
228 static GTY(()) tree V2SF_type_node;
229 static GTY(()) tree V4HI_type_node;
230 static GTY(()) tree V4SI_type_node;
231 static GTY(()) tree V4SF_type_node;
232 static GTY(()) tree V8HI_type_node;
233 static GTY(()) tree unsigned_V16QI_type_node;
234 static GTY(()) tree unsigned_V8HI_type_node;
235 static GTY(()) tree unsigned_V4SI_type_node;
236 static GTY(()) tree bool_char_type_node;        /* __bool char */
237 static GTY(()) tree bool_short_type_node;       /* __bool short */
238 static GTY(()) tree bool_int_type_node;         /* __bool int */
239 static GTY(()) tree pixel_type_node;            /* __pixel */
240 static GTY(()) tree bool_V16QI_type_node;       /* __vector __bool char */
241 static GTY(()) tree bool_V8HI_type_node;        /* __vector __bool short */
242 static GTY(()) tree bool_V4SI_type_node;        /* __vector __bool int */
243 static GTY(()) tree pixel_V8HI_type_node;       /* __vector __pixel */
244
245 int rs6000_warn_altivec_long = 1;               /* On by default. */
246 const char *rs6000_warn_altivec_long_switch;
247
248 const char *rs6000_traceback_name;
249 static enum {
250   traceback_default = 0,
251   traceback_none,
252   traceback_part,
253   traceback_full
254 } rs6000_traceback;
255
256 /* Flag to say the TOC is initialized */
257 int toc_initialized;
258 char toc_label_name[10];
259
260 /* Alias set for saves and restores from the rs6000 stack.  */
261 static GTY(()) int rs6000_sr_alias_set;
262
263 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
264    The only place that looks at this is rs6000_set_default_type_attributes;
265    everywhere else should rely on the presence or absence of a longcall
266    attribute on the function declaration.  */
267 int rs6000_default_long_calls;
268 const char *rs6000_longcall_switch;
269
270 /* Control alignment for fields within structures.  */
271 /* String from -malign-XXXXX.  */
272 const char *rs6000_alignment_string;
273 int rs6000_alignment_flags;
274
275 struct builtin_description
276 {
277   /* mask is not const because we're going to alter it below.  This
278      nonsense will go away when we rewrite the -march infrastructure
279      to give us more target flag bits.  */
280   unsigned int mask;
281   const enum insn_code icode;
282   const char *const name;
283   const enum rs6000_builtins code;
284 };
285
286 static bool rs6000_function_ok_for_sibcall (tree, tree);
287 static int num_insns_constant_wide (HOST_WIDE_INT);
288 static void validate_condition_mode (enum rtx_code, enum machine_mode);
289 static rtx rs6000_generate_compare (enum rtx_code);
290 static void rs6000_maybe_dead (rtx);
291 static void rs6000_emit_stack_tie (void);
292 static void rs6000_frame_related (rtx, rtx, HOST_WIDE_INT, rtx, rtx);
293 static rtx spe_synthesize_frame_save (rtx);
294 static bool spe_func_has_64bit_regs_p (void);
295 static void emit_frame_save (rtx, rtx, enum machine_mode, unsigned int,
296                              int, HOST_WIDE_INT);
297 static rtx gen_frame_mem_offset (enum machine_mode, rtx, int);
298 static void rs6000_emit_allocate_stack (HOST_WIDE_INT, int);
299 static unsigned rs6000_hash_constant (rtx);
300 static unsigned toc_hash_function (const void *);
301 static int toc_hash_eq (const void *, const void *);
302 static int constant_pool_expr_1 (rtx, int *, int *);
303 static bool constant_pool_expr_p (rtx);
304 static bool toc_relative_expr_p (rtx);
305 static bool legitimate_small_data_p (enum machine_mode, rtx);
306 static bool legitimate_offset_address_p (enum machine_mode, rtx, int);
307 static bool legitimate_indexed_address_p (rtx, int);
308 static bool legitimate_indirect_address_p (rtx, int);
309 static bool macho_lo_sum_memory_operand (rtx x, enum machine_mode mode);
310 static bool legitimate_lo_sum_address_p (enum machine_mode, rtx, int);
311 static struct machine_function * rs6000_init_machine_status (void);
312 static bool rs6000_assemble_integer (rtx, unsigned int, int);
313 #ifdef HAVE_GAS_HIDDEN
314 static void rs6000_assemble_visibility (tree, int);
315 #endif
316 static int rs6000_ra_ever_killed (void);
317 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
318 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
319 static const char *rs6000_mangle_fundamental_type (tree);
320 extern const struct attribute_spec rs6000_attribute_table[];
321 static void rs6000_set_default_type_attributes (tree);
322 static void rs6000_output_function_prologue (FILE *, HOST_WIDE_INT);
323 static void rs6000_output_function_epilogue (FILE *, HOST_WIDE_INT);
324 static void rs6000_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
325                                     tree);
326 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
327 static bool rs6000_return_in_memory (tree, tree);
328 static void rs6000_file_start (void);
329 #if TARGET_ELF
330 static unsigned int rs6000_elf_section_type_flags (tree, const char *, int);
331 static void rs6000_elf_asm_out_constructor (rtx, int);
332 static void rs6000_elf_asm_out_destructor (rtx, int);
333 static void rs6000_elf_select_section (tree, int, unsigned HOST_WIDE_INT);
334 static void rs6000_elf_unique_section (tree, int);
335 static void rs6000_elf_select_rtx_section (enum machine_mode, rtx,
336                                            unsigned HOST_WIDE_INT);
337 static void rs6000_elf_encode_section_info (tree, rtx, int)
338      ATTRIBUTE_UNUSED;
339 static bool rs6000_elf_in_small_data_p (tree);
340 #endif
341 #if TARGET_XCOFF
342 static void rs6000_xcoff_asm_globalize_label (FILE *, const char *);
343 static void rs6000_xcoff_asm_named_section (const char *, unsigned int);
344 static void rs6000_xcoff_select_section (tree, int, unsigned HOST_WIDE_INT);
345 static void rs6000_xcoff_unique_section (tree, int);
346 static void rs6000_xcoff_select_rtx_section (enum machine_mode, rtx,
347                                              unsigned HOST_WIDE_INT);
348 static const char * rs6000_xcoff_strip_name_encoding (const char *);
349 static unsigned int rs6000_xcoff_section_type_flags (tree, const char *, int);
350 static void rs6000_xcoff_file_start (void);
351 static void rs6000_xcoff_file_end (void);
352 #endif
353 #if TARGET_MACHO
354 static bool rs6000_binds_local_p (tree);
355 #endif
356 static int rs6000_use_dfa_pipeline_interface (void);
357 static int rs6000_variable_issue (FILE *, int, rtx, int);
358 static bool rs6000_rtx_costs (rtx, int, int, int *);
359 static int rs6000_adjust_cost (rtx, rtx, rtx, int);
360 static bool is_microcoded_insn (rtx);
361 static int is_dispatch_slot_restricted (rtx);
362 static bool is_cracked_insn (rtx);
363 static bool is_branch_slot_insn (rtx);
364 static int rs6000_adjust_priority (rtx, int);
365 static int rs6000_issue_rate (void);
366 static bool rs6000_is_costly_dependence (rtx, rtx, rtx, int, int);
367 static rtx get_next_active_insn (rtx, rtx);
368 static bool insn_terminates_group_p (rtx , enum group_termination);
369 static bool is_costly_group (rtx *, rtx);
370 static int force_new_group (int, FILE *, rtx *, rtx, bool *, int, int *);
371 static int redefine_groups (FILE *, int, rtx, rtx);
372 static int pad_groups (FILE *, int, rtx, rtx);
373 static void rs6000_sched_finish (FILE *, int);
374 static int rs6000_use_sched_lookahead (void);
375
376 static void rs6000_init_builtins (void);
377 static rtx rs6000_expand_unop_builtin (enum insn_code, tree, rtx);
378 static rtx rs6000_expand_binop_builtin (enum insn_code, tree, rtx);
379 static rtx rs6000_expand_ternop_builtin (enum insn_code, tree, rtx);
380 static rtx rs6000_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
381 static void altivec_init_builtins (void);
382 static void rs6000_common_init_builtins (void);
383 static void rs6000_init_libfuncs (void);
384
385 static void enable_mask_for_builtins (struct builtin_description *, int,
386                                       enum rs6000_builtins,
387                                       enum rs6000_builtins);
388 static tree build_opaque_vector_type (tree, int);
389 static void spe_init_builtins (void);
390 static rtx spe_expand_builtin (tree, rtx, bool *);
391 static rtx spe_expand_stv_builtin (enum insn_code, tree);
392 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
393 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
394 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
395 static rs6000_stack_t *rs6000_stack_info (void);
396 static void debug_stack_info (rs6000_stack_t *);
397
398 static rtx altivec_expand_builtin (tree, rtx, bool *);
399 static rtx altivec_expand_ld_builtin (tree, rtx, bool *);
400 static rtx altivec_expand_st_builtin (tree, rtx, bool *);
401 static rtx altivec_expand_dst_builtin (tree, rtx, bool *);
402 static rtx altivec_expand_abs_builtin (enum insn_code, tree, rtx);
403 static rtx altivec_expand_predicate_builtin (enum insn_code, 
404                                             const char *, tree, rtx);
405 static rtx altivec_expand_lv_builtin (enum insn_code, tree, rtx);
406 static rtx altivec_expand_stv_builtin (enum insn_code, tree);
407 static void rs6000_parse_abi_options (void);
408 static void rs6000_parse_alignment_option (void);
409 static void rs6000_parse_tls_size_option (void);
410 static void rs6000_parse_yes_no_option (const char *, const char *, int *);
411 static int first_altivec_reg_to_save (void);
412 static unsigned int compute_vrsave_mask (void);
413 static void is_altivec_return_reg (rtx, void *);
414 static rtx generate_set_vrsave (rtx, rs6000_stack_t *, int);
415 int easy_vector_constant (rtx, enum machine_mode);
416 static int easy_vector_same (rtx, enum machine_mode);
417 static int easy_vector_splat_const (int, enum machine_mode);
418 static bool is_ev64_opaque_type (tree);
419 static rtx rs6000_dwarf_register_span (rtx);
420 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
421 static rtx rs6000_tls_get_addr (void);
422 static rtx rs6000_got_sym (void);
423 static inline int rs6000_tls_symbol_ref_1 (rtx *, void *);
424 static const char *rs6000_get_some_local_dynamic_name (void);
425 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
426 static rtx rs6000_complex_function_value (enum machine_mode);
427 static rtx rs6000_spe_function_arg (CUMULATIVE_ARGS *,
428                                     enum machine_mode, tree);
429 static rtx rs6000_mixed_function_arg (CUMULATIVE_ARGS *,
430                                       enum machine_mode, tree, int);
431 static void rs6000_move_block_from_reg (int regno, rtx x, int nregs);
432 static void setup_incoming_varargs (CUMULATIVE_ARGS *,
433                                     enum machine_mode, tree,
434                                     int *, int);
435 #if TARGET_MACHO
436 static void macho_branch_islands (void);
437 static void add_compiler_branch_island (tree, tree, int);
438 static int no_previous_def (tree function_name);
439 static tree get_prev_label (tree function_name);
440 #endif
441
442 static tree rs6000_build_builtin_va_list (void);
443 static tree rs6000_gimplify_va_arg (tree, tree, tree *, tree *);
444
445 /* Hash table stuff for keeping track of TOC entries.  */
446
447 struct toc_hash_struct GTY(())
448 {
449   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
450      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
451   rtx key;
452   enum machine_mode key_mode;
453   int labelno;
454 };
455
456 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
457 \f
458 /* Default register names.  */
459 char rs6000_reg_names[][8] =
460 {
461       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
462       "8",  "9", "10", "11", "12", "13", "14", "15",
463      "16", "17", "18", "19", "20", "21", "22", "23",
464      "24", "25", "26", "27", "28", "29", "30", "31",
465       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
466       "8",  "9", "10", "11", "12", "13", "14", "15",
467      "16", "17", "18", "19", "20", "21", "22", "23",
468      "24", "25", "26", "27", "28", "29", "30", "31",
469      "mq", "lr", "ctr","ap",
470       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
471       "xer",
472       /* AltiVec registers.  */
473       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
474       "8",  "9",  "10", "11", "12", "13", "14", "15",
475       "16", "17", "18", "19", "20", "21", "22", "23",
476       "24", "25", "26", "27", "28", "29", "30", "31",
477       "vrsave", "vscr",
478       /* SPE registers.  */
479       "spe_acc", "spefscr"
480 };
481
482 #ifdef TARGET_REGNAMES
483 static const char alt_reg_names[][8] =
484 {
485    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
486    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
487   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
488   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
489    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
490    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
491   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
492   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
493     "mq",    "lr",  "ctr",   "ap",
494   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
495    "xer",
496   /* AltiVec registers.  */
497    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
498    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
499   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
500   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
501   "vrsave", "vscr",
502   /* SPE registers.  */
503   "spe_acc", "spefscr"
504 };
505 #endif
506 \f
507 #ifndef MASK_STRICT_ALIGN
508 #define MASK_STRICT_ALIGN 0
509 #endif
510 #ifndef TARGET_PROFILE_KERNEL
511 #define TARGET_PROFILE_KERNEL 0
512 #endif
513
514 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
515 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
516
517 /* Return 1 for a symbol ref for a thread-local storage symbol.  */
518 #define RS6000_SYMBOL_REF_TLS_P(RTX) \
519   (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
520 \f
521 /* Initialize the GCC target structure.  */
522 #undef TARGET_ATTRIBUTE_TABLE
523 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
524 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
525 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
526
527 #undef TARGET_ASM_ALIGNED_DI_OP
528 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
529
530 /* Default unaligned ops are only provided for ELF.  Find the ops needed
531    for non-ELF systems.  */
532 #ifndef OBJECT_FORMAT_ELF
533 #if TARGET_XCOFF
534 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
535    64-bit targets.  */
536 #undef TARGET_ASM_UNALIGNED_HI_OP
537 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
538 #undef TARGET_ASM_UNALIGNED_SI_OP
539 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
540 #undef TARGET_ASM_UNALIGNED_DI_OP
541 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
542 #else
543 /* For Darwin.  */
544 #undef TARGET_ASM_UNALIGNED_HI_OP
545 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
546 #undef TARGET_ASM_UNALIGNED_SI_OP
547 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
548 #endif
549 #endif
550
551 /* This hook deals with fixups for relocatable code and DI-mode objects
552    in 64-bit code.  */
553 #undef TARGET_ASM_INTEGER
554 #define TARGET_ASM_INTEGER rs6000_assemble_integer
555
556 #ifdef HAVE_GAS_HIDDEN
557 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
558 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
559 #endif
560
561 #undef TARGET_HAVE_TLS
562 #define TARGET_HAVE_TLS HAVE_AS_TLS
563
564 #undef TARGET_CANNOT_FORCE_CONST_MEM
565 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_tls_referenced_p
566
567 #undef TARGET_ASM_FUNCTION_PROLOGUE
568 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
569 #undef TARGET_ASM_FUNCTION_EPILOGUE
570 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
571
572 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
573 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE rs6000_use_dfa_pipeline_interface
574 #undef  TARGET_SCHED_VARIABLE_ISSUE
575 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
576
577 #undef TARGET_SCHED_ISSUE_RATE
578 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
579 #undef TARGET_SCHED_ADJUST_COST
580 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
581 #undef TARGET_SCHED_ADJUST_PRIORITY
582 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
583 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE      
584 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
585 #undef TARGET_SCHED_FINISH
586 #define TARGET_SCHED_FINISH rs6000_sched_finish
587
588 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
589 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
590
591 #undef TARGET_INIT_BUILTINS
592 #define TARGET_INIT_BUILTINS rs6000_init_builtins
593
594 #undef TARGET_EXPAND_BUILTIN
595 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
596
597 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
598 #define TARGET_MANGLE_FUNDAMENTAL_TYPE rs6000_mangle_fundamental_type
599
600 #undef TARGET_INIT_LIBFUNCS
601 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
602
603 #if TARGET_MACHO
604 #undef TARGET_BINDS_LOCAL_P
605 #define TARGET_BINDS_LOCAL_P rs6000_binds_local_p
606 #endif
607
608 #undef TARGET_ASM_OUTPUT_MI_THUNK
609 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
610
611 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
612 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
613
614 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
615 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
616
617 #undef TARGET_RTX_COSTS
618 #define TARGET_RTX_COSTS rs6000_rtx_costs
619 #undef TARGET_ADDRESS_COST
620 #define TARGET_ADDRESS_COST hook_int_rtx_0
621
622 #undef TARGET_VECTOR_OPAQUE_P
623 #define TARGET_VECTOR_OPAQUE_P is_ev64_opaque_type
624
625 #undef TARGET_DWARF_REGISTER_SPAN
626 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
627
628 /* On rs6000, function arguments are promoted, as are function return
629    values.  */
630 #undef TARGET_PROMOTE_FUNCTION_ARGS
631 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
632 #undef TARGET_PROMOTE_FUNCTION_RETURN
633 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
634
635 #undef TARGET_RETURN_IN_MEMORY
636 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
637
638 #undef TARGET_SETUP_INCOMING_VARARGS
639 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
640
641 /* Always strict argument naming on rs6000.  */
642 #undef TARGET_STRICT_ARGUMENT_NAMING
643 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
644 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
645 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
646 #undef TARGET_SPLIT_COMPLEX_ARG
647 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_tree_true
648
649 #undef TARGET_BUILD_BUILTIN_VA_LIST
650 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
651
652 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
653 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
654
655 struct gcc_target targetm = TARGET_INITIALIZER;
656 \f
657
658 /* Value is 1 if hard register REGNO can hold a value of machine-mode
659    MODE.  */
660 static int
661 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
662 {
663   /* The GPRs can hold any mode, but values bigger than one register
664      cannot go past R31.  */
665   if (INT_REGNO_P (regno))
666     return INT_REGNO_P (regno + HARD_REGNO_NREGS (regno, mode) - 1);
667
668   /* The float registers can only hold floating modes and DImode.  */
669   if (FP_REGNO_P (regno))
670     return
671       (GET_MODE_CLASS (mode) == MODE_FLOAT
672        && FP_REGNO_P (regno + HARD_REGNO_NREGS (regno, mode) - 1))
673       || (GET_MODE_CLASS (mode) == MODE_INT
674           && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD);
675
676   /* The CR register can only hold CC modes.  */
677   if (CR_REGNO_P (regno))
678     return GET_MODE_CLASS (mode) == MODE_CC;
679
680   if (XER_REGNO_P (regno))
681     return mode == PSImode;
682
683   /* AltiVec only in AldyVec registers.  */
684   if (ALTIVEC_REGNO_P (regno))
685     return ALTIVEC_VECTOR_MODE (mode);
686
687   /* ...but GPRs can hold SIMD data on the SPE in one register.  */
688   if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
689     return 1;
690
691   /* We cannot put TImode anywhere except general register and it must be
692      able to fit within the register set.  */
693
694   return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
695 }
696
697 /* Initialize rs6000_hard_regno_mode_ok_p table.  */
698 static void
699 rs6000_init_hard_regno_mode_ok (void)
700 {
701   int r, m;
702
703   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
704     for (m = 0; m < NUM_MACHINE_MODES; ++m)
705       if (rs6000_hard_regno_mode_ok (r, m))
706         rs6000_hard_regno_mode_ok_p[m][r] = true;
707 }
708
709 /* Override command line options.  Mostly we process the processor
710    type and sometimes adjust other TARGET_ options.  */
711
712 void
713 rs6000_override_options (const char *default_cpu)
714 {
715   size_t i, j;
716   struct rs6000_cpu_select *ptr;
717   int set_masks;
718
719   /* Simplifications for entries below.  */
720
721   enum {
722     POWERPC_BASE_MASK = MASK_POWERPC | MASK_NEW_MNEMONICS,
723     POWERPC_7400_MASK = POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_ALTIVEC
724   };
725
726   /* This table occasionally claims that a processor does not support
727      a particular feature even though it does, but the feature is slower
728      than the alternative.  Thus, it shouldn't be relied on as a
729      complete description of the processor's support.  
730
731      Please keep this list in order, and don't forget to update the
732      documentation in invoke.texi when adding a new processor or
733      flag.  */
734   static struct ptt
735     {
736       const char *const name;           /* Canonical processor name.  */
737       const enum processor_type processor; /* Processor type enum value.  */
738       const int target_enable;  /* Target flags to enable.  */
739     } const processor_target_table[]
740       = {{"401", PROCESSOR_PPC403, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
741          {"403", PROCESSOR_PPC403,
742           POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_STRICT_ALIGN},
743          {"405", PROCESSOR_PPC405, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
744          {"405fp", PROCESSOR_PPC405, POWERPC_BASE_MASK},
745          {"440", PROCESSOR_PPC440, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
746          {"440fp", PROCESSOR_PPC440, POWERPC_BASE_MASK},
747          {"505", PROCESSOR_MPCCORE, POWERPC_BASE_MASK},
748          {"601", PROCESSOR_PPC601,
749           MASK_POWER | POWERPC_BASE_MASK | MASK_MULTIPLE | MASK_STRING},
750          {"602", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
751          {"603", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
752          {"603e", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
753          {"604", PROCESSOR_PPC604, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
754          {"604e", PROCESSOR_PPC604e, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
755          {"620", PROCESSOR_PPC620,
756           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
757          {"630", PROCESSOR_PPC630,
758           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
759          {"740", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
760          {"7400", PROCESSOR_PPC7400, POWERPC_7400_MASK},
761          {"7450", PROCESSOR_PPC7450, POWERPC_7400_MASK},
762          {"750", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
763          {"801", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
764          {"821", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
765          {"823", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
766          {"8540", PROCESSOR_PPC8540, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
767          {"860", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
768          {"970", PROCESSOR_POWER4,
769           POWERPC_7400_MASK | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
770          {"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS},
771          {"ec603e", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
772          {"G3", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
773          {"G4",  PROCESSOR_PPC7450, POWERPC_7400_MASK},
774          {"G5", PROCESSOR_POWER4,
775           POWERPC_7400_MASK | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
776          {"power", PROCESSOR_POWER, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
777          {"power2", PROCESSOR_POWER,
778           MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING},
779          {"power3", PROCESSOR_PPC630,
780           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
781          {"power4", PROCESSOR_POWER4,
782           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_MFCRF | MASK_POWERPC64},
783          {"power5", PROCESSOR_POWER5,
784           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_MFCRF | MASK_POWERPC64},
785          {"powerpc", PROCESSOR_POWERPC, POWERPC_BASE_MASK},
786          {"powerpc64", PROCESSOR_POWERPC64,
787           POWERPC_BASE_MASK | MASK_POWERPC64},
788          {"rios", PROCESSOR_RIOS1, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
789          {"rios1", PROCESSOR_RIOS1, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
790          {"rios2", PROCESSOR_RIOS2,
791           MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING},
792          {"rsc", PROCESSOR_PPC601, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
793          {"rsc1", PROCESSOR_PPC601, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
794          {"rs64a", PROCESSOR_RS64A, POWERPC_BASE_MASK | MASK_POWERPC64},
795       };
796
797   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
798
799   /* Some OSs don't support saving the high part of 64-bit registers on
800      context switch.  Other OSs don't support saving Altivec registers.
801      On those OSs, we don't touch the MASK_POWERPC64 or MASK_ALTIVEC
802      settings; if the user wants either, the user must explicitly specify
803      them and we won't interfere with the user's specification.  */
804
805   enum {
806     POWER_MASKS = MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
807     POWERPC_MASKS = (POWERPC_BASE_MASK | MASK_PPC_GPOPT 
808                      | MASK_PPC_GFXOPT | MASK_POWERPC64 | MASK_ALTIVEC
809                      | MASK_MFCRF)
810   };
811
812   rs6000_init_hard_regno_mode_ok ();
813
814  set_masks = POWER_MASKS | POWERPC_MASKS | MASK_SOFT_FLOAT;
815 #ifdef OS_MISSING_POWERPC64
816   if (OS_MISSING_POWERPC64)
817     set_masks &= ~MASK_POWERPC64;
818 #endif
819 #ifdef OS_MISSING_ALTIVEC
820   if (OS_MISSING_ALTIVEC)
821     set_masks &= ~MASK_ALTIVEC;
822 #endif
823
824   /* Don't override these by the processor default if given explicitly.  */
825   set_masks &= ~(target_flags_explicit
826                  & (MASK_MULTIPLE | MASK_STRING | MASK_SOFT_FLOAT));
827
828   /* Identify the processor type.  */
829   rs6000_select[0].string = default_cpu;
830   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
831
832   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
833     {
834       ptr = &rs6000_select[i];
835       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
836         {
837           for (j = 0; j < ptt_size; j++)
838             if (! strcmp (ptr->string, processor_target_table[j].name))
839               {
840                 if (ptr->set_tune_p)
841                   rs6000_cpu = processor_target_table[j].processor;
842
843                 if (ptr->set_arch_p)
844                   {
845                     target_flags &= ~set_masks;
846                     target_flags |= (processor_target_table[j].target_enable
847                                      & set_masks);
848                   }
849                 break;
850               }
851
852           if (j == ptt_size)
853             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
854         }
855     }
856
857   if (TARGET_E500)
858     rs6000_isel = 1;
859
860   /* If we are optimizing big endian systems for space, use the load/store
861      multiple and string instructions.  */
862   if (BYTES_BIG_ENDIAN && optimize_size)
863     target_flags |= ~target_flags_explicit & (MASK_MULTIPLE | MASK_STRING);
864
865   /* Don't allow -mmultiple or -mstring on little endian systems
866      unless the cpu is a 750, because the hardware doesn't support the
867      instructions used in little endian mode, and causes an alignment
868      trap.  The 750 does not cause an alignment trap (except when the
869      target is unaligned).  */
870
871   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
872     {
873       if (TARGET_MULTIPLE)
874         {
875           target_flags &= ~MASK_MULTIPLE;
876           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
877             warning ("-mmultiple is not supported on little endian systems");
878         }
879
880       if (TARGET_STRING)
881         {
882           target_flags &= ~MASK_STRING;
883           if ((target_flags_explicit & MASK_STRING) != 0)
884             warning ("-mstring is not supported on little endian systems");
885         }
886     }
887
888   /* Set debug flags */
889   if (rs6000_debug_name)
890     {
891       if (! strcmp (rs6000_debug_name, "all"))
892         rs6000_debug_stack = rs6000_debug_arg = 1;
893       else if (! strcmp (rs6000_debug_name, "stack"))
894         rs6000_debug_stack = 1;
895       else if (! strcmp (rs6000_debug_name, "arg"))
896         rs6000_debug_arg = 1;
897       else
898         error ("unknown -mdebug-%s switch", rs6000_debug_name);
899     }
900
901   if (rs6000_traceback_name)
902     {
903       if (! strncmp (rs6000_traceback_name, "full", 4))
904         rs6000_traceback = traceback_full;
905       else if (! strncmp (rs6000_traceback_name, "part", 4))
906         rs6000_traceback = traceback_part;
907       else if (! strncmp (rs6000_traceback_name, "no", 2))
908         rs6000_traceback = traceback_none;
909       else
910         error ("unknown -mtraceback arg `%s'; expecting `full', `partial' or `none'",
911                rs6000_traceback_name);
912     }
913
914   /* Set size of long double */
915   rs6000_long_double_type_size = 64;
916   if (rs6000_long_double_size_string)
917     {
918       char *tail;
919       int size = strtol (rs6000_long_double_size_string, &tail, 10);
920       if (*tail != '\0' || (size != 64 && size != 128))
921         error ("Unknown switch -mlong-double-%s",
922                rs6000_long_double_size_string);
923       else
924         rs6000_long_double_type_size = size;
925     }
926
927   /* Set Altivec ABI as default for powerpc64 linux.  */
928   if (TARGET_ELF && TARGET_64BIT)
929     {
930       rs6000_altivec_abi = 1;
931       rs6000_altivec_vrsave = 1;
932     }
933
934   /* Handle -mabi= options.  */
935   rs6000_parse_abi_options ();
936
937   /* Handle -malign-XXXXX option.  */
938   rs6000_parse_alignment_option ();
939
940   /* Handle generic -mFOO=YES/NO options.  */
941   rs6000_parse_yes_no_option ("vrsave", rs6000_altivec_vrsave_string,
942                               &rs6000_altivec_vrsave);
943   rs6000_parse_yes_no_option ("isel", rs6000_isel_string,
944                               &rs6000_isel);
945   rs6000_parse_yes_no_option ("spe", rs6000_spe_string, &rs6000_spe);
946   rs6000_parse_yes_no_option ("float-gprs", rs6000_float_gprs_string,
947                               &rs6000_float_gprs);
948
949   /* Handle -mtls-size option.  */
950   rs6000_parse_tls_size_option ();
951
952 #ifdef SUBTARGET_OVERRIDE_OPTIONS
953   SUBTARGET_OVERRIDE_OPTIONS;
954 #endif
955 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
956   SUBSUBTARGET_OVERRIDE_OPTIONS;
957 #endif
958
959   if (TARGET_E500)
960     {
961       if (TARGET_ALTIVEC)
962         error ("AltiVec and E500 instructions cannot coexist");
963
964       /* The e500 does not have string instructions, and we set
965          MASK_STRING above when optimizing for size.  */
966       if ((target_flags & MASK_STRING) != 0)
967         target_flags = target_flags & ~MASK_STRING;
968
969       /* No SPE means 64-bit long doubles, even if an E500.  */
970       if (rs6000_spe_string != 0
971           && !strcmp (rs6000_spe_string, "no"))
972         rs6000_long_double_type_size = 64;
973     }
974   else if (rs6000_select[1].string != NULL)
975     {
976       /* For the powerpc-eabispe configuration, we set all these by
977          default, so let's unset them if we manually set another
978          CPU that is not the E500.  */
979       if (rs6000_abi_string == 0)
980         rs6000_spe_abi = 0;
981       if (rs6000_spe_string == 0)
982         rs6000_spe = 0;
983       if (rs6000_float_gprs_string == 0)
984         rs6000_float_gprs = 0;
985       if (rs6000_isel_string == 0)
986         rs6000_isel = 0;
987       if (rs6000_long_double_size_string == 0)
988         rs6000_long_double_type_size = 64;
989     }
990
991   rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
992                         && rs6000_cpu != PROCESSOR_POWER5);
993   rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
994                          || rs6000_cpu == PROCESSOR_POWER5);
995
996   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
997      using TARGET_OPTIONS to handle a toggle switch, but we're out of
998      bits in target_flags so TARGET_SWITCHES cannot be used.
999      Assumption here is that rs6000_longcall_switch points into the
1000      text of the complete option, rather than being a copy, so we can
1001      scan back for the presence or absence of the no- modifier.  */
1002   if (rs6000_longcall_switch)
1003     {
1004       const char *base = rs6000_longcall_switch;
1005       while (base[-1] != 'm') base--;
1006
1007       if (*rs6000_longcall_switch != '\0')
1008         error ("invalid option `%s'", base);
1009       rs6000_default_long_calls = (base[0] != 'n');
1010     }
1011
1012   /* Handle -m(no-)warn-altivec-long similarly.  */
1013   if (rs6000_warn_altivec_long_switch)
1014     {
1015       const char *base = rs6000_warn_altivec_long_switch;
1016       while (base[-1] != 'm') base--;
1017
1018       if (*rs6000_warn_altivec_long_switch != '\0')
1019         error ("invalid option `%s'", base);
1020       rs6000_warn_altivec_long = (base[0] != 'n');
1021     }
1022
1023   /* Handle -mprioritize-restricted-insns option.  */
1024   rs6000_sched_restricted_insns_priority
1025     = (rs6000_sched_groups ? 1 : 0);
1026   if (rs6000_sched_restricted_insns_priority_str)
1027     rs6000_sched_restricted_insns_priority =
1028       atoi (rs6000_sched_restricted_insns_priority_str);
1029
1030   /* Handle -msched-costly-dep option.  */
1031   rs6000_sched_costly_dep
1032     = (rs6000_sched_groups ? store_to_load_dep_costly : no_dep_costly);
1033   if (rs6000_sched_costly_dep_str)
1034     {
1035       if (! strcmp (rs6000_sched_costly_dep_str, "no"))  
1036         rs6000_sched_costly_dep = no_dep_costly;
1037       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
1038         rs6000_sched_costly_dep = all_deps_costly;
1039       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
1040         rs6000_sched_costly_dep = true_store_to_load_dep_costly;
1041       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
1042         rs6000_sched_costly_dep = store_to_load_dep_costly;
1043       else 
1044         rs6000_sched_costly_dep = atoi (rs6000_sched_costly_dep_str);
1045     }
1046
1047   /* Handle -minsert-sched-nops option.  */
1048   rs6000_sched_insert_nops
1049     = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
1050   if (rs6000_sched_insert_nops_str)
1051     {
1052       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
1053         rs6000_sched_insert_nops = sched_finish_none;
1054       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
1055         rs6000_sched_insert_nops = sched_finish_pad_groups;
1056       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
1057         rs6000_sched_insert_nops = sched_finish_regroup_exact;
1058       else
1059         rs6000_sched_insert_nops = atoi (rs6000_sched_insert_nops_str);
1060     }
1061
1062 #ifdef TARGET_REGNAMES
1063   /* If the user desires alternate register names, copy in the
1064      alternate names now.  */
1065   if (TARGET_REGNAMES)
1066     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
1067 #endif
1068
1069   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
1070      If -maix-struct-return or -msvr4-struct-return was explicitly
1071      used, don't override with the ABI default.  */
1072   if ((target_flags_explicit & MASK_AIX_STRUCT_RET) == 0)
1073     {
1074       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
1075         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
1076       else
1077         target_flags |= MASK_AIX_STRUCT_RET;
1078     }
1079
1080   if (TARGET_LONG_DOUBLE_128
1081       && (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN))
1082     REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
1083
1084   /* Allocate an alias set for register saves & restores from stack.  */
1085   rs6000_sr_alias_set = new_alias_set ();
1086
1087   if (TARGET_TOC) 
1088     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
1089
1090   /* We can only guarantee the availability of DI pseudo-ops when
1091      assembling for 64-bit targets.  */
1092   if (!TARGET_64BIT)
1093     {
1094       targetm.asm_out.aligned_op.di = NULL;
1095       targetm.asm_out.unaligned_op.di = NULL;
1096     }
1097
1098   /* Set maximum branch target alignment at two instructions, eight bytes.  */
1099   align_jumps_max_skip = 8;
1100   align_loops_max_skip = 8;
1101
1102   /* Arrange to save and restore machine status around nested functions.  */
1103   init_machine_status = rs6000_init_machine_status;
1104
1105   /* We should always be splitting complex arguments, but we can't break
1106      Linux and Darwin ABIs at the moment.  For now, only AIX is fixed.  */
1107   if (DEFAULT_ABI != ABI_AIX)
1108     targetm.calls.split_complex_arg = NULL;
1109 }
1110
1111 /* Handle generic options of the form -mfoo=yes/no.
1112    NAME is the option name.
1113    VALUE is the option value.
1114    FLAG is the pointer to the flag where to store a 1 or 0, depending on
1115    whether the option value is 'yes' or 'no' respectively.  */
1116 static void
1117 rs6000_parse_yes_no_option (const char *name, const char *value, int *flag)
1118 {
1119   if (value == 0)
1120     return;
1121   else if (!strcmp (value, "yes"))
1122     *flag = 1;
1123   else if (!strcmp (value, "no"))
1124     *flag = 0;
1125   else
1126     error ("unknown -m%s= option specified: '%s'", name, value);
1127 }
1128
1129 /* Handle -mabi= options.  */
1130 static void
1131 rs6000_parse_abi_options (void)
1132 {
1133   if (rs6000_abi_string == 0)
1134     return;
1135   else if (! strcmp (rs6000_abi_string, "altivec"))
1136     {
1137       rs6000_altivec_abi = 1;
1138       rs6000_spe_abi = 0;
1139     }
1140   else if (! strcmp (rs6000_abi_string, "no-altivec"))
1141     rs6000_altivec_abi = 0;
1142   else if (! strcmp (rs6000_abi_string, "spe"))
1143     {
1144       rs6000_spe_abi = 1;
1145       rs6000_altivec_abi = 0;
1146       if (!TARGET_SPE_ABI)
1147         error ("not configured for ABI: '%s'", rs6000_abi_string);
1148     }
1149   
1150   else if (! strcmp (rs6000_abi_string, "no-spe"))
1151     rs6000_spe_abi = 0;
1152   else
1153     error ("unknown ABI specified: '%s'", rs6000_abi_string);
1154 }
1155
1156 /* Handle -malign-XXXXXX options.  */
1157 static void
1158 rs6000_parse_alignment_option (void)
1159 {
1160   if (rs6000_alignment_string == 0)
1161     return;
1162   else if (! strcmp (rs6000_alignment_string, "power"))
1163     rs6000_alignment_flags = MASK_ALIGN_POWER;
1164   else if (! strcmp (rs6000_alignment_string, "natural"))
1165     rs6000_alignment_flags = MASK_ALIGN_NATURAL;
1166   else
1167     error ("unknown -malign-XXXXX option specified: '%s'",
1168            rs6000_alignment_string);
1169 }
1170
1171 /* Validate and record the size specified with the -mtls-size option.  */
1172
1173 static void
1174 rs6000_parse_tls_size_option (void)
1175 {
1176   if (rs6000_tls_size_string == 0)
1177     return;
1178   else if (strcmp (rs6000_tls_size_string, "16") == 0)
1179     rs6000_tls_size = 16;
1180   else if (strcmp (rs6000_tls_size_string, "32") == 0)
1181     rs6000_tls_size = 32;
1182   else if (strcmp (rs6000_tls_size_string, "64") == 0)
1183     rs6000_tls_size = 64;
1184   else
1185     error ("bad value `%s' for -mtls-size switch", rs6000_tls_size_string);
1186 }
1187
1188 void
1189 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1190 {
1191 }
1192 \f
1193 /* Do anything needed at the start of the asm file.  */
1194
1195 static void
1196 rs6000_file_start (void)
1197 {
1198   size_t i;
1199   char buffer[80];
1200   const char *start = buffer;
1201   struct rs6000_cpu_select *ptr;
1202   const char *default_cpu = TARGET_CPU_DEFAULT;
1203   FILE *file = asm_out_file;
1204
1205   default_file_start ();
1206
1207 #ifdef TARGET_BI_ARCH
1208   if ((TARGET_DEFAULT ^ target_flags) & MASK_64BIT)
1209     default_cpu = 0;
1210 #endif
1211
1212   if (flag_verbose_asm)
1213     {
1214       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
1215       rs6000_select[0].string = default_cpu;
1216
1217       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
1218         {
1219           ptr = &rs6000_select[i];
1220           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
1221             {
1222               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
1223               start = "";
1224             }
1225         }
1226
1227 #ifdef USING_ELFOS_H
1228       switch (rs6000_sdata)
1229         {
1230         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
1231         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
1232         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
1233         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
1234         }
1235
1236       if (rs6000_sdata && g_switch_value)
1237         {
1238           fprintf (file, "%s -G " HOST_WIDE_INT_PRINT_UNSIGNED, start,
1239                    g_switch_value);
1240           start = "";
1241         }
1242 #endif
1243
1244       if (*start == '\0')
1245         putc ('\n', file);
1246     }
1247 }
1248 \f
1249 /* Return nonzero if this function is known to have a null epilogue.  */
1250
1251 int
1252 direct_return (void)
1253 {
1254   if (reload_completed)
1255     {
1256       rs6000_stack_t *info = rs6000_stack_info ();
1257
1258       if (info->first_gp_reg_save == 32
1259           && info->first_fp_reg_save == 64
1260           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
1261           && ! info->lr_save_p
1262           && ! info->cr_save_p
1263           && info->vrsave_mask == 0
1264           && ! info->push_p)
1265         return 1;
1266     }
1267
1268   return 0;
1269 }
1270
1271 /* Returns 1 always.  */
1272
1273 int
1274 any_operand (rtx op ATTRIBUTE_UNUSED, 
1275              enum machine_mode mode ATTRIBUTE_UNUSED)
1276 {
1277   return 1;
1278 }
1279
1280 /* Returns 1 if op is the count register.  */
1281 int
1282 count_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1283 {
1284   if (GET_CODE (op) != REG)
1285     return 0;
1286
1287   if (REGNO (op) == COUNT_REGISTER_REGNUM)
1288     return 1;
1289
1290   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
1291     return 1;
1292
1293   return 0;
1294 }
1295
1296 /* Returns 1 if op is an altivec register.  */
1297 int
1298 altivec_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1299 {
1300   
1301   return (register_operand (op, mode)
1302           && (GET_CODE (op) != REG
1303               || REGNO (op) > FIRST_PSEUDO_REGISTER
1304               || ALTIVEC_REGNO_P (REGNO (op))));
1305 }
1306
1307 int
1308 xer_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1309 {
1310   if (GET_CODE (op) != REG)
1311     return 0;
1312
1313   if (XER_REGNO_P (REGNO (op)))
1314     return 1;
1315
1316   return 0;
1317 }
1318
1319 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
1320    by such constants completes more quickly.  */
1321
1322 int
1323 s8bit_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1324 {
1325   return ( GET_CODE (op) == CONST_INT
1326           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
1327 }
1328
1329 /* Return 1 if OP is a constant that can fit in a D field.  */
1330
1331 int
1332 short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1333 {
1334   return (GET_CODE (op) == CONST_INT
1335           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
1336 }
1337
1338 /* Similar for an unsigned D field.  */
1339
1340 int
1341 u_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1342 {
1343   return (GET_CODE (op) == CONST_INT
1344           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
1345 }
1346
1347 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
1348
1349 int
1350 non_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1351 {
1352   return (GET_CODE (op) == CONST_INT
1353           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
1354 }
1355
1356 /* Returns 1 if OP is a CONST_INT that is a positive value
1357    and an exact power of 2.  */
1358
1359 int
1360 exact_log2_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1361 {
1362   return (GET_CODE (op) == CONST_INT
1363           && INTVAL (op) > 0
1364           && exact_log2 (INTVAL (op)) >= 0);
1365 }
1366
1367 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
1368    ctr, or lr).  */
1369
1370 int
1371 gpc_reg_operand (rtx op, enum machine_mode mode)
1372 {
1373   return (register_operand (op, mode)
1374           && (GET_CODE (op) != REG
1375               || (REGNO (op) >= ARG_POINTER_REGNUM 
1376                   && !XER_REGNO_P (REGNO (op)))
1377               || REGNO (op) < MQ_REGNO));
1378 }
1379
1380 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1381    CR field.  */
1382
1383 int
1384 cc_reg_operand (rtx op, enum machine_mode mode)
1385 {
1386   return (register_operand (op, mode)
1387           && (GET_CODE (op) != REG
1388               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1389               || CR_REGNO_P (REGNO (op))));
1390 }
1391
1392 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1393    CR field that isn't CR0.  */
1394
1395 int
1396 cc_reg_not_cr0_operand (rtx op, enum machine_mode mode)
1397 {
1398   return (register_operand (op, mode)
1399           && (GET_CODE (op) != REG
1400               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1401               || CR_REGNO_NOT_CR0_P (REGNO (op))));
1402 }
1403
1404 /* Returns 1 if OP is either a constant integer valid for a D-field or
1405    a non-special register.  If a register, it must be in the proper
1406    mode unless MODE is VOIDmode.  */
1407
1408 int
1409 reg_or_short_operand (rtx op, enum machine_mode mode)
1410 {
1411   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1412 }
1413
1414 /* Similar, except check if the negation of the constant would be
1415    valid for a D-field.  Don't allow a constant zero, since all the
1416    patterns that call this predicate use "addic r1,r2,-constant" on
1417    a constant value to set a carry when r2 is greater or equal to
1418    "constant".  That doesn't work for zero.  */
1419
1420 int
1421 reg_or_neg_short_operand (rtx op, enum machine_mode mode)
1422 {
1423   if (GET_CODE (op) == CONST_INT)
1424     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P') && INTVAL (op) != 0;
1425
1426   return gpc_reg_operand (op, mode);
1427 }
1428
1429 /* Returns 1 if OP is either a constant integer valid for a DS-field or
1430    a non-special register.  If a register, it must be in the proper
1431    mode unless MODE is VOIDmode.  */
1432
1433 int
1434 reg_or_aligned_short_operand (rtx op, enum machine_mode mode)
1435 {
1436   if (gpc_reg_operand (op, mode))
1437     return 1;
1438   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1439     return 1;
1440
1441   return 0;
1442 }
1443
1444
1445 /* Return 1 if the operand is either a register or an integer whose
1446    high-order 16 bits are zero.  */
1447
1448 int
1449 reg_or_u_short_operand (rtx op, enum machine_mode mode)
1450 {
1451   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1452 }
1453
1454 /* Return 1 is the operand is either a non-special register or ANY
1455    constant integer.  */
1456
1457 int
1458 reg_or_cint_operand (rtx op, enum machine_mode mode)
1459 {
1460   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
1461 }
1462
1463 /* Return 1 is the operand is either a non-special register or ANY
1464    32-bit signed constant integer.  */
1465
1466 int
1467 reg_or_arith_cint_operand (rtx op, enum machine_mode mode)
1468 {
1469   return (gpc_reg_operand (op, mode)
1470           || (GET_CODE (op) == CONST_INT
1471 #if HOST_BITS_PER_WIDE_INT != 32
1472               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1473                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
1474 #endif
1475               ));
1476 }
1477
1478 /* Return 1 is the operand is either a non-special register or a 32-bit
1479    signed constant integer valid for 64-bit addition.  */
1480
1481 int
1482 reg_or_add_cint64_operand (rtx op, enum machine_mode mode)
1483 {
1484   return (gpc_reg_operand (op, mode)
1485           || (GET_CODE (op) == CONST_INT
1486 #if HOST_BITS_PER_WIDE_INT == 32
1487               && INTVAL (op) < 0x7fff8000
1488 #else
1489               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1490                   < 0x100000000ll)
1491 #endif
1492               ));
1493 }
1494
1495 /* Return 1 is the operand is either a non-special register or a 32-bit
1496    signed constant integer valid for 64-bit subtraction.  */
1497
1498 int
1499 reg_or_sub_cint64_operand (rtx op, enum machine_mode mode)
1500 {
1501   return (gpc_reg_operand (op, mode)
1502           || (GET_CODE (op) == CONST_INT
1503 #if HOST_BITS_PER_WIDE_INT == 32
1504               && (- INTVAL (op)) < 0x7fff8000
1505 #else
1506               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1507                   < 0x100000000ll)
1508 #endif
1509               ));
1510 }
1511
1512 /* Return 1 is the operand is either a non-special register or ANY
1513    32-bit unsigned constant integer.  */
1514
1515 int
1516 reg_or_logical_cint_operand (rtx op, enum machine_mode mode)
1517 {
1518   if (GET_CODE (op) == CONST_INT)
1519     {
1520       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1521         {
1522           if (GET_MODE_BITSIZE (mode) <= 32)
1523             abort ();
1524
1525           if (INTVAL (op) < 0)
1526             return 0;
1527         }
1528
1529       return ((INTVAL (op) & GET_MODE_MASK (mode)
1530                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1531     }
1532   else if (GET_CODE (op) == CONST_DOUBLE)
1533     {
1534       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1535           || mode != DImode)
1536         abort ();
1537
1538       return CONST_DOUBLE_HIGH (op) == 0;
1539     }
1540   else 
1541     return gpc_reg_operand (op, mode);
1542 }
1543
1544 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1545
1546 int
1547 got_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1548 {
1549   return (GET_CODE (op) == SYMBOL_REF
1550           || GET_CODE (op) == CONST
1551           || GET_CODE (op) == LABEL_REF);
1552 }
1553
1554 /* Return 1 if the operand is a simple references that can be loaded via
1555    the GOT (labels involving addition aren't allowed).  */
1556
1557 int
1558 got_no_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1559 {
1560   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1561 }
1562
1563 /* Return the number of instructions it takes to form a constant in an
1564    integer register.  */
1565
1566 static int
1567 num_insns_constant_wide (HOST_WIDE_INT value)
1568 {
1569   /* signed constant loadable with {cal|addi} */
1570   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1571     return 1;
1572
1573   /* constant loadable with {cau|addis} */
1574   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1575     return 1;
1576
1577 #if HOST_BITS_PER_WIDE_INT == 64
1578   else if (TARGET_POWERPC64)
1579     {
1580       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1581       HOST_WIDE_INT high = value >> 31;
1582
1583       if (high == 0 || high == -1)
1584         return 2;
1585
1586       high >>= 1;
1587
1588       if (low == 0)
1589         return num_insns_constant_wide (high) + 1;
1590       else
1591         return (num_insns_constant_wide (high)
1592                 + num_insns_constant_wide (low) + 1);
1593     }
1594 #endif
1595
1596   else
1597     return 2;
1598 }
1599
1600 int
1601 num_insns_constant (rtx op, enum machine_mode mode)
1602 {
1603   if (GET_CODE (op) == CONST_INT)
1604     {
1605 #if HOST_BITS_PER_WIDE_INT == 64
1606       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1607           && mask64_operand (op, mode))
1608             return 2;
1609       else
1610 #endif
1611         return num_insns_constant_wide (INTVAL (op));
1612     }
1613
1614   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1615     {
1616       long l;
1617       REAL_VALUE_TYPE rv;
1618
1619       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1620       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1621       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1622     }
1623
1624   else if (GET_CODE (op) == CONST_DOUBLE)
1625     {
1626       HOST_WIDE_INT low;
1627       HOST_WIDE_INT high;
1628       long l[2];
1629       REAL_VALUE_TYPE rv;
1630       int endian = (WORDS_BIG_ENDIAN == 0);
1631
1632       if (mode == VOIDmode || mode == DImode)
1633         {
1634           high = CONST_DOUBLE_HIGH (op);
1635           low  = CONST_DOUBLE_LOW (op);
1636         }
1637       else
1638         {
1639           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1640           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1641           high = l[endian];
1642           low  = l[1 - endian];
1643         }
1644
1645       if (TARGET_32BIT)
1646         return (num_insns_constant_wide (low)
1647                 + num_insns_constant_wide (high));
1648
1649       else
1650         {
1651           if (high == 0 && low >= 0)
1652             return num_insns_constant_wide (low);
1653
1654           else if (high == -1 && low < 0)
1655             return num_insns_constant_wide (low);
1656
1657           else if (mask64_operand (op, mode))
1658             return 2;
1659
1660           else if (low == 0)
1661             return num_insns_constant_wide (high) + 1;
1662
1663           else
1664             return (num_insns_constant_wide (high)
1665                     + num_insns_constant_wide (low) + 1);
1666         }
1667     }
1668
1669   else
1670     abort ();
1671 }
1672
1673 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1674    register with one instruction per word.  We only do this if we can
1675    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1676
1677 int
1678 easy_fp_constant (rtx op, enum machine_mode mode)
1679 {
1680   if (GET_CODE (op) != CONST_DOUBLE
1681       || GET_MODE (op) != mode
1682       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1683     return 0;
1684
1685   /* Consider all constants with -msoft-float to be easy.  */
1686   if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1687       && mode != DImode)
1688     return 1;
1689
1690   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1691   if (flag_pic && DEFAULT_ABI == ABI_V4)
1692     return 0;
1693
1694 #ifdef TARGET_RELOCATABLE
1695   /* Similarly if we are using -mrelocatable, consider all constants
1696      to be hard.  */
1697   if (TARGET_RELOCATABLE)
1698     return 0;
1699 #endif
1700
1701   if (mode == TFmode)
1702     {
1703       long k[4];
1704       REAL_VALUE_TYPE rv;
1705
1706       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1707       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1708
1709       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1710               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1
1711               && num_insns_constant_wide ((HOST_WIDE_INT) k[2]) == 1
1712               && num_insns_constant_wide ((HOST_WIDE_INT) k[3]) == 1);
1713     }
1714
1715   else if (mode == DFmode)
1716     {
1717       long k[2];
1718       REAL_VALUE_TYPE rv;
1719
1720       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1721       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1722
1723       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1724               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1725     }
1726
1727   else if (mode == SFmode)
1728     {
1729       long l;
1730       REAL_VALUE_TYPE rv;
1731
1732       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1733       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1734
1735       return num_insns_constant_wide (l) == 1;
1736     }
1737
1738   else if (mode == DImode)
1739     return ((TARGET_POWERPC64
1740              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1741             || (num_insns_constant (op, DImode) <= 2));
1742
1743   else if (mode == SImode)
1744     return 1;
1745   else
1746     abort ();
1747 }
1748
1749 /* Returns the constant for the splat instruction, if exists.  */
1750
1751 static int
1752 easy_vector_splat_const (int cst, enum machine_mode mode)
1753 {
1754   switch (mode) 
1755     {
1756     case V4SImode:
1757       if (EASY_VECTOR_15 (cst) 
1758           || EASY_VECTOR_15_ADD_SELF (cst)) 
1759         return cst;
1760       if ((cst & 0xffff) != ((cst >> 16) & 0xffff))
1761         break;
1762       cst = cst >> 16;
1763     case V8HImode:
1764       if (EASY_VECTOR_15 (cst) 
1765           || EASY_VECTOR_15_ADD_SELF (cst)) 
1766         return cst;
1767       if ((cst & 0xff) != ((cst >> 8) & 0xff))
1768         break;
1769       cst = cst >> 8;
1770     case V16QImode:
1771           if (EASY_VECTOR_15 (cst) 
1772               || EASY_VECTOR_15_ADD_SELF (cst)) 
1773             return cst;
1774     default: 
1775       break;
1776     }
1777   return 0;
1778 }
1779
1780
1781 /* Return nonzero if all elements of a vector have the same value.  */
1782
1783 static int
1784 easy_vector_same (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1785 {
1786   int units, i, cst;
1787
1788   units = CONST_VECTOR_NUNITS (op);
1789
1790   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1791   for (i = 1; i < units; ++i)
1792     if (INTVAL (CONST_VECTOR_ELT (op, i)) != cst)
1793       break;
1794   if (i == units && easy_vector_splat_const (cst, mode))
1795     return 1;
1796   return 0;
1797 }
1798
1799 /* Return 1 if the operand is a CONST_INT and can be put into a
1800    register without using memory.  */
1801
1802 int
1803 easy_vector_constant (rtx op, enum machine_mode mode)
1804 {
1805   int cst, cst2;
1806
1807   if (GET_CODE (op) != CONST_VECTOR
1808       || (!TARGET_ALTIVEC
1809           && !TARGET_SPE))
1810     return 0;
1811
1812   if (zero_constant (op, mode)
1813       && ((TARGET_ALTIVEC && ALTIVEC_VECTOR_MODE (mode))
1814           || (TARGET_SPE && SPE_VECTOR_MODE (mode))))
1815     return 1;
1816
1817   if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
1818     return 0;
1819
1820   if (TARGET_SPE && mode == V1DImode)
1821     return 0;
1822
1823   cst  = INTVAL (CONST_VECTOR_ELT (op, 0));
1824   cst2 = INTVAL (CONST_VECTOR_ELT (op, 1));
1825
1826   /* Limit SPE vectors to 15 bits signed.  These we can generate with:
1827        li r0, CONSTANT1
1828        evmergelo r0, r0, r0
1829        li r0, CONSTANT2
1830
1831      I don't know how efficient it would be to allow bigger constants,
1832      considering we'll have an extra 'ori' for every 'li'.  I doubt 5
1833      instructions is better than a 64-bit memory load, but I don't
1834      have the e500 timing specs.  */
1835   if (TARGET_SPE && mode == V2SImode
1836       && cst  >= -0x7fff && cst <= 0x7fff
1837       && cst2 >= -0x7fff && cst2 <= 0x7fff)
1838     return 1;
1839
1840   if (TARGET_ALTIVEC 
1841       && easy_vector_same (op, mode))
1842     {
1843       cst = easy_vector_splat_const (cst, mode);
1844       if (EASY_VECTOR_15_ADD_SELF (cst) 
1845           || EASY_VECTOR_15 (cst))
1846         return 1;
1847     }  
1848   return 0;
1849 }
1850
1851 /* Same as easy_vector_constant but only for EASY_VECTOR_15_ADD_SELF.  */
1852
1853 int
1854 easy_vector_constant_add_self (rtx op, enum machine_mode mode)
1855 {
1856   int cst;
1857   if (TARGET_ALTIVEC
1858       && GET_CODE (op) == CONST_VECTOR
1859       && easy_vector_same (op, mode))
1860     {
1861       cst = easy_vector_splat_const (INTVAL (CONST_VECTOR_ELT (op, 0)), mode);
1862       if (EASY_VECTOR_15_ADD_SELF (cst))
1863         return 1;  
1864     }
1865   return 0;
1866 }
1867
1868 /* Generate easy_vector_constant out of a easy_vector_constant_add_self.  */
1869
1870 rtx 
1871 gen_easy_vector_constant_add_self (rtx op)
1872 {
1873   int i, units;
1874   rtvec v;
1875   units = GET_MODE_NUNITS (GET_MODE (op));
1876   v = rtvec_alloc (units);
1877
1878   for (i = 0; i < units; i++)
1879     RTVEC_ELT (v, i) = 
1880       GEN_INT (INTVAL (CONST_VECTOR_ELT (op, i)) >> 1);
1881   return gen_rtx_raw_CONST_VECTOR (GET_MODE (op), v);
1882 }
1883
1884 const char *
1885 output_vec_const_move (rtx *operands)
1886 {
1887   int cst, cst2;
1888   enum machine_mode mode;
1889   rtx dest, vec;
1890
1891   dest = operands[0];
1892   vec = operands[1];
1893
1894   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
1895   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
1896   mode = GET_MODE (dest);
1897
1898   if (TARGET_ALTIVEC)
1899     {
1900       if (zero_constant (vec, mode))
1901         return "vxor %0,%0,%0";
1902       else if (easy_vector_constant (vec, mode))
1903         {
1904           operands[1] = GEN_INT (cst);
1905           switch (mode)
1906             {
1907             case V4SImode:
1908               if (EASY_VECTOR_15 (cst))
1909                 {
1910                   operands[1] = GEN_INT (cst);
1911                   return "vspltisw %0,%1";
1912                 }
1913               else if (EASY_VECTOR_15_ADD_SELF (cst))
1914                 return "#";
1915               cst = cst >> 16;
1916             case V8HImode:
1917               if (EASY_VECTOR_15 (cst))
1918                 {
1919                   operands[1] = GEN_INT (cst);
1920                   return "vspltish %0,%1";
1921                 }
1922               else if (EASY_VECTOR_15_ADD_SELF (cst))
1923                 return "#";
1924               cst = cst >> 8;
1925             case V16QImode:
1926               if (EASY_VECTOR_15 (cst))
1927                 {
1928                   operands[1] = GEN_INT (cst);
1929                   return "vspltisb %0,%1";
1930                 }
1931               else if (EASY_VECTOR_15_ADD_SELF (cst))
1932                 return "#";
1933             default:
1934               abort ();
1935             }
1936         }
1937       else
1938         abort ();
1939     }
1940
1941   if (TARGET_SPE)
1942     {
1943       /* Vector constant 0 is handled as a splitter of V2SI, and in the
1944          pattern of V1DI, V4HI, and V2SF.
1945
1946          FIXME: We should probably return # and add post reload
1947          splitters for these, but this way is so easy ;-).
1948       */
1949       operands[1] = GEN_INT (cst);
1950       operands[2] = GEN_INT (cst2);
1951       if (cst == cst2)
1952         return "li %0,%1\n\tevmergelo %0,%0,%0";
1953       else
1954         return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
1955     }
1956
1957   abort ();
1958 }
1959
1960 /* Return 1 if the operand is the constant 0.  This works for scalars
1961    as well as vectors.  */
1962 int
1963 zero_constant (rtx op, enum machine_mode mode)
1964 {
1965   return op == CONST0_RTX (mode);
1966 }
1967
1968 /* Return 1 if the operand is 0.0.  */
1969 int
1970 zero_fp_constant (rtx op, enum machine_mode mode)
1971 {
1972   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1973 }
1974
1975 /* Return 1 if the operand is in volatile memory.  Note that during
1976    the RTL generation phase, memory_operand does not return TRUE for
1977    volatile memory references.  So this function allows us to
1978    recognize volatile references where its safe.  */
1979
1980 int
1981 volatile_mem_operand (rtx op, enum machine_mode mode)
1982 {
1983   if (GET_CODE (op) != MEM)
1984     return 0;
1985
1986   if (!MEM_VOLATILE_P (op))
1987     return 0;
1988
1989   if (mode != GET_MODE (op))
1990     return 0;
1991
1992   if (reload_completed)
1993     return memory_operand (op, mode);
1994
1995   if (reload_in_progress)
1996     return strict_memory_address_p (mode, XEXP (op, 0));
1997
1998   return memory_address_p (mode, XEXP (op, 0));
1999 }
2000
2001 /* Return 1 if the operand is an offsettable memory operand.  */
2002
2003 int
2004 offsettable_mem_operand (rtx op, enum machine_mode mode)
2005 {
2006   return ((GET_CODE (op) == MEM)
2007           && offsettable_address_p (reload_completed || reload_in_progress,
2008                                     mode, XEXP (op, 0)));
2009 }
2010
2011 /* Return 1 if the operand is either an easy FP constant (see above) or
2012    memory.  */
2013
2014 int
2015 mem_or_easy_const_operand (rtx op, enum machine_mode mode)
2016 {
2017   return memory_operand (op, mode) || easy_fp_constant (op, mode);
2018 }
2019
2020 /* Return 1 if the operand is either a non-special register or an item
2021    that can be used as the operand of a `mode' add insn.  */
2022
2023 int
2024 add_operand (rtx op, enum machine_mode mode)
2025 {
2026   if (GET_CODE (op) == CONST_INT)
2027     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
2028             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
2029
2030   return gpc_reg_operand (op, mode);
2031 }
2032
2033 /* Return 1 if OP is a constant but not a valid add_operand.  */
2034
2035 int
2036 non_add_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2037 {
2038   return (GET_CODE (op) == CONST_INT
2039           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
2040           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
2041 }
2042
2043 /* Return 1 if the operand is a non-special register or a constant that
2044    can be used as the operand of an OR or XOR insn on the RS/6000.  */
2045
2046 int
2047 logical_operand (rtx op, enum machine_mode mode)
2048 {
2049   HOST_WIDE_INT opl, oph;
2050
2051   if (gpc_reg_operand (op, mode))
2052     return 1;
2053
2054   if (GET_CODE (op) == CONST_INT)
2055     {
2056       opl = INTVAL (op) & GET_MODE_MASK (mode);
2057
2058 #if HOST_BITS_PER_WIDE_INT <= 32
2059       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
2060         return 0;
2061 #endif
2062     }
2063   else if (GET_CODE (op) == CONST_DOUBLE)
2064     {
2065       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
2066         abort ();
2067
2068       opl = CONST_DOUBLE_LOW (op);
2069       oph = CONST_DOUBLE_HIGH (op);
2070       if (oph != 0)
2071         return 0;
2072     }
2073   else
2074     return 0;
2075
2076   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
2077           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
2078 }
2079
2080 /* Return 1 if C is a constant that is not a logical operand (as
2081    above), but could be split into one.  */
2082
2083 int
2084 non_logical_cint_operand (rtx op, enum machine_mode mode)
2085 {
2086   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
2087           && ! logical_operand (op, mode)
2088           && reg_or_logical_cint_operand (op, mode));
2089 }
2090
2091 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
2092    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
2093    Reject all ones and all zeros, since these should have been optimized
2094    away and confuse the making of MB and ME.  */
2095
2096 int
2097 mask_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2098 {
2099   HOST_WIDE_INT c, lsb;
2100
2101   if (GET_CODE (op) != CONST_INT)
2102     return 0;
2103
2104   c = INTVAL (op);
2105
2106   /* Fail in 64-bit mode if the mask wraps around because the upper
2107      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
2108   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
2109     return 0;
2110
2111   /* We don't change the number of transitions by inverting,
2112      so make sure we start with the LS bit zero.  */
2113   if (c & 1)
2114     c = ~c;
2115
2116   /* Reject all zeros or all ones.  */
2117   if (c == 0)
2118     return 0;
2119
2120   /* Find the first transition.  */
2121   lsb = c & -c;
2122
2123   /* Invert to look for a second transition.  */
2124   c = ~c;
2125
2126   /* Erase first transition.  */
2127   c &= -lsb;
2128
2129   /* Find the second transition (if any).  */
2130   lsb = c & -c;
2131
2132   /* Match if all the bits above are 1's (or c is zero).  */
2133   return c == -lsb;
2134 }
2135
2136 /* Return 1 for the PowerPC64 rlwinm corner case.  */
2137
2138 int
2139 mask_operand_wrap (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2140 {
2141   HOST_WIDE_INT c, lsb;
2142
2143   if (GET_CODE (op) != CONST_INT)
2144     return 0;
2145
2146   c = INTVAL (op);
2147
2148   if ((c & 0x80000001) != 0x80000001)
2149     return 0;
2150
2151   c = ~c;
2152   if (c == 0)
2153     return 0;
2154
2155   lsb = c & -c;
2156   c = ~c;
2157   c &= -lsb;
2158   lsb = c & -c;
2159   return c == -lsb;
2160 }
2161
2162 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
2163    It is if there are no more than one 1->0 or 0->1 transitions.
2164    Reject all zeros, since zero should have been optimized away and
2165    confuses the making of MB and ME.  */
2166
2167 int
2168 mask64_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2169 {
2170   if (GET_CODE (op) == CONST_INT)
2171     {
2172       HOST_WIDE_INT c, lsb;
2173
2174       c = INTVAL (op);
2175
2176       /* Reject all zeros.  */
2177       if (c == 0)
2178         return 0;
2179
2180       /* We don't change the number of transitions by inverting,
2181          so make sure we start with the LS bit zero.  */
2182       if (c & 1)
2183         c = ~c;
2184
2185       /* Find the transition, and check that all bits above are 1's.  */
2186       lsb = c & -c;
2187
2188       /* Match if all the bits above are 1's (or c is zero).  */
2189       return c == -lsb;
2190     }
2191   return 0;
2192 }
2193
2194 /* Like mask64_operand, but allow up to three transitions.  This
2195    predicate is used by insn patterns that generate two rldicl or
2196    rldicr machine insns.  */
2197
2198 int
2199 mask64_2_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2200 {
2201   if (GET_CODE (op) == CONST_INT)
2202     {
2203       HOST_WIDE_INT c, lsb;
2204
2205       c = INTVAL (op);
2206
2207       /* Disallow all zeros.  */
2208       if (c == 0)
2209         return 0;
2210
2211       /* We don't change the number of transitions by inverting,
2212          so make sure we start with the LS bit zero.  */
2213       if (c & 1)
2214         c = ~c;
2215
2216       /* Find the first transition.  */
2217       lsb = c & -c;
2218
2219       /* Invert to look for a second transition.  */
2220       c = ~c;
2221
2222       /* Erase first transition.  */
2223       c &= -lsb;
2224
2225       /* Find the second transition.  */
2226       lsb = c & -c;
2227
2228       /* Invert to look for a third transition.  */
2229       c = ~c;
2230
2231       /* Erase second transition.  */
2232       c &= -lsb;
2233
2234       /* Find the third transition (if any).  */
2235       lsb = c & -c;
2236
2237       /* Match if all the bits above are 1's (or c is zero).  */
2238       return c == -lsb;
2239     }
2240   return 0;
2241 }
2242
2243 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
2244    implement ANDing by the mask IN.  */
2245 void
2246 build_mask64_2_operands (rtx in, rtx *out)
2247 {
2248 #if HOST_BITS_PER_WIDE_INT >= 64
2249   unsigned HOST_WIDE_INT c, lsb, m1, m2;
2250   int shift;
2251
2252   if (GET_CODE (in) != CONST_INT)
2253     abort ();
2254
2255   c = INTVAL (in);
2256   if (c & 1)
2257     {
2258       /* Assume c initially something like 0x00fff000000fffff.  The idea
2259          is to rotate the word so that the middle ^^^^^^ group of zeros
2260          is at the MS end and can be cleared with an rldicl mask.  We then
2261          rotate back and clear off the MS    ^^ group of zeros with a
2262          second rldicl.  */
2263       c = ~c;                   /*   c == 0xff000ffffff00000 */
2264       lsb = c & -c;             /* lsb == 0x0000000000100000 */
2265       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
2266       c = ~c;                   /*   c == 0x00fff000000fffff */
2267       c &= -lsb;                /*   c == 0x00fff00000000000 */
2268       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2269       c = ~c;                   /*   c == 0xff000fffffffffff */
2270       c &= -lsb;                /*   c == 0xff00000000000000 */
2271       shift = 0;
2272       while ((lsb >>= 1) != 0)
2273         shift++;                /* shift == 44 on exit from loop */
2274       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
2275       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
2276       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
2277     }
2278   else
2279     {
2280       /* Assume c initially something like 0xff000f0000000000.  The idea
2281          is to rotate the word so that the     ^^^  middle group of zeros
2282          is at the LS end and can be cleared with an rldicr mask.  We then
2283          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
2284          a second rldicr.  */
2285       lsb = c & -c;             /* lsb == 0x0000010000000000 */
2286       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
2287       c = ~c;                   /*   c == 0x00fff0ffffffffff */
2288       c &= -lsb;                /*   c == 0x00fff00000000000 */
2289       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2290       c = ~c;                   /*   c == 0xff000fffffffffff */
2291       c &= -lsb;                /*   c == 0xff00000000000000 */
2292       shift = 0;
2293       while ((lsb >>= 1) != 0)
2294         shift++;                /* shift == 44 on exit from loop */
2295       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
2296       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
2297       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
2298     }
2299
2300   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
2301      masks will be all 1's.  We are guaranteed more than one transition.  */
2302   out[0] = GEN_INT (64 - shift);
2303   out[1] = GEN_INT (m1);
2304   out[2] = GEN_INT (shift);
2305   out[3] = GEN_INT (m2);
2306 #else
2307   (void)in;
2308   (void)out;
2309   abort ();
2310 #endif
2311 }
2312
2313 /* Return 1 if the operand is either a non-special register or a constant
2314    that can be used as the operand of a PowerPC64 logical AND insn.  */
2315
2316 int
2317 and64_operand (rtx op, enum machine_mode mode)
2318 {
2319   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2320     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
2321
2322   return (logical_operand (op, mode) || mask64_operand (op, mode));
2323 }
2324
2325 /* Like the above, but also match constants that can be implemented
2326    with two rldicl or rldicr insns.  */
2327
2328 int
2329 and64_2_operand (rtx op, enum machine_mode mode)
2330 {
2331   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2332     return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
2333
2334   return logical_operand (op, mode) || mask64_2_operand (op, mode);
2335 }
2336
2337 /* Return 1 if the operand is either a non-special register or a
2338    constant that can be used as the operand of an RS/6000 logical AND insn.  */
2339
2340 int
2341 and_operand (rtx op, enum machine_mode mode)
2342 {
2343   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2344     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
2345
2346   return (logical_operand (op, mode) || mask_operand (op, mode));
2347 }
2348
2349 /* Return 1 if the operand is a general register or memory operand.  */
2350
2351 int
2352 reg_or_mem_operand (rtx op, enum machine_mode mode)
2353 {
2354   return (gpc_reg_operand (op, mode)
2355           || memory_operand (op, mode)
2356           || macho_lo_sum_memory_operand (op, mode)
2357           || volatile_mem_operand (op, mode));
2358 }
2359
2360 /* Return 1 if the operand is a general register or memory operand without
2361    pre_inc or pre_dec which produces invalid form of PowerPC lwa
2362    instruction.  */
2363
2364 int
2365 lwa_operand (rtx op, enum machine_mode mode)
2366 {
2367   rtx inner = op;
2368
2369   if (reload_completed && GET_CODE (inner) == SUBREG)
2370     inner = SUBREG_REG (inner);
2371     
2372   return gpc_reg_operand (inner, mode)
2373     || (memory_operand (inner, mode)
2374         && GET_CODE (XEXP (inner, 0)) != PRE_INC
2375         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
2376         && (GET_CODE (XEXP (inner, 0)) != PLUS
2377             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
2378             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
2379 }
2380
2381 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
2382
2383 int
2384 symbol_ref_operand (rtx op, enum machine_mode mode)
2385 {
2386   if (mode != VOIDmode && GET_MODE (op) != mode)
2387     return 0;
2388
2389   return (GET_CODE (op) == SYMBOL_REF
2390           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op)));
2391 }
2392
2393 /* Return 1 if the operand, used inside a MEM, is a valid first argument
2394    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
2395
2396 int
2397 call_operand (rtx op, enum machine_mode mode)
2398 {
2399   if (mode != VOIDmode && GET_MODE (op) != mode)
2400     return 0;
2401
2402   return (GET_CODE (op) == SYMBOL_REF
2403           || (GET_CODE (op) == REG
2404               && (REGNO (op) == LINK_REGISTER_REGNUM
2405                   || REGNO (op) == COUNT_REGISTER_REGNUM
2406                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2407 }
2408
2409 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
2410    this file.  */
2411
2412 int
2413 current_file_function_operand (rtx op, 
2414                               enum machine_mode mode ATTRIBUTE_UNUSED)
2415 {
2416   return (GET_CODE (op) == SYMBOL_REF
2417           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op))
2418           && (SYMBOL_REF_LOCAL_P (op)
2419               || (op == XEXP (DECL_RTL (current_function_decl), 0))));
2420 }
2421
2422 /* Return 1 if this operand is a valid input for a move insn.  */
2423
2424 int
2425 input_operand (rtx op, enum machine_mode mode)
2426 {
2427   /* Memory is always valid.  */
2428   if (memory_operand (op, mode))
2429     return 1;
2430
2431   /* For floating-point, easy constants are valid.  */
2432   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2433       && CONSTANT_P (op)
2434       && easy_fp_constant (op, mode))
2435     return 1;
2436
2437   /* Allow any integer constant.  */
2438   if (GET_MODE_CLASS (mode) == MODE_INT
2439       && (GET_CODE (op) == CONST_INT
2440           || GET_CODE (op) == CONST_DOUBLE))
2441     return 1;
2442
2443   /* Allow easy vector constants.  */
2444   if (GET_CODE (op) == CONST_VECTOR
2445       && easy_vector_constant (op, mode))
2446     return 1;
2447
2448   /* For floating-point or multi-word mode, the only remaining valid type
2449      is a register.  */
2450   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2451       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2452     return register_operand (op, mode);
2453
2454   /* The only cases left are integral modes one word or smaller (we
2455      do not get called for MODE_CC values).  These can be in any
2456      register.  */
2457   if (register_operand (op, mode))
2458     return 1;
2459
2460   /* A SYMBOL_REF referring to the TOC is valid.  */
2461   if (legitimate_constant_pool_address_p (op))
2462     return 1;
2463
2464   /* A constant pool expression (relative to the TOC) is valid */
2465   if (toc_relative_expr_p (op))
2466     return 1;
2467
2468   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
2469      to be valid.  */
2470   if (DEFAULT_ABI == ABI_V4
2471       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
2472       && small_data_operand (op, Pmode))
2473     return 1;
2474
2475   return 0;
2476 }
2477
2478
2479 /* Darwin, AIX increases natural record alignment to doubleword if the first
2480    field is an FP double while the FP fields remain word aligned.  */
2481
2482 unsigned int
2483 rs6000_special_round_type_align (tree type, int computed, int specified)
2484 {
2485   tree field = TYPE_FIELDS (type);
2486
2487   /* Skip all the static variables only if ABI is greater than
2488      1 or equal to 0.  */
2489   while (field != NULL && TREE_CODE (field) == VAR_DECL)
2490     field = TREE_CHAIN (field);
2491
2492   if (field == NULL || field == type || DECL_MODE (field) != DFmode)
2493     return MAX (computed, specified);
2494
2495   return MAX (MAX (computed, specified), 64);
2496 }
2497
2498 /* Return 1 for an operand in small memory on V.4/eabi.  */
2499
2500 int
2501 small_data_operand (rtx op ATTRIBUTE_UNUSED, 
2502                     enum machine_mode mode ATTRIBUTE_UNUSED)
2503 {
2504 #if TARGET_ELF
2505   rtx sym_ref;
2506
2507   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
2508     return 0;
2509
2510   if (DEFAULT_ABI != ABI_V4)
2511     return 0;
2512
2513   if (GET_CODE (op) == SYMBOL_REF)
2514     sym_ref = op;
2515
2516   else if (GET_CODE (op) != CONST
2517            || GET_CODE (XEXP (op, 0)) != PLUS
2518            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
2519            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
2520     return 0;
2521
2522   else
2523     {
2524       rtx sum = XEXP (op, 0);
2525       HOST_WIDE_INT summand;
2526
2527       /* We have to be careful here, because it is the referenced address
2528         that must be 32k from _SDA_BASE_, not just the symbol.  */
2529       summand = INTVAL (XEXP (sum, 1));
2530       if (summand < 0 || (unsigned HOST_WIDE_INT) summand > g_switch_value)
2531        return 0;
2532
2533       sym_ref = XEXP (sum, 0);
2534     }
2535
2536   return SYMBOL_REF_SMALL_P (sym_ref);
2537 #else
2538   return 0;
2539 #endif
2540 }
2541
2542 /* Return true, if operand is a memory operand and has a
2543    displacement divisible by 4.  */
2544
2545 int
2546 word_offset_memref_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2547 {
2548   rtx addr;
2549   int off = 0;
2550
2551   if (!memory_operand (op, mode))
2552     return 0;
2553
2554   addr = XEXP (op, 0);
2555   if (GET_CODE (addr) == PLUS
2556       && GET_CODE (XEXP (addr, 0)) == REG
2557       && GET_CODE (XEXP (addr, 1)) == CONST_INT)
2558     off = INTVAL (XEXP (addr, 1));
2559
2560   return (off % 4) == 0;
2561 }
2562
2563 /* Return true if either operand is a general purpose register.  */
2564
2565 bool
2566 gpr_or_gpr_p (rtx op0, rtx op1)
2567 {
2568   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
2569           || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
2570 }
2571
2572 \f
2573 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address.  */
2574
2575 static int 
2576 constant_pool_expr_1 (rtx op, int *have_sym, int *have_toc) 
2577 {
2578   switch (GET_CODE(op)) 
2579     {
2580     case SYMBOL_REF:
2581       if (RS6000_SYMBOL_REF_TLS_P (op))
2582         return 0;
2583       else if (CONSTANT_POOL_ADDRESS_P (op))
2584         {
2585           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
2586             {
2587               *have_sym = 1;
2588               return 1;
2589             }
2590           else
2591             return 0;
2592         }
2593       else if (! strcmp (XSTR (op, 0), toc_label_name))
2594         {
2595           *have_toc = 1;
2596           return 1;
2597         }
2598       else
2599         return 0;
2600     case PLUS:
2601     case MINUS:
2602       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2603               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
2604     case CONST:
2605       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
2606     case CONST_INT:
2607       return 1;
2608     default:
2609       return 0;
2610     }
2611 }
2612
2613 static bool
2614 constant_pool_expr_p (rtx op)
2615 {
2616   int have_sym = 0;
2617   int have_toc = 0;
2618   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2619 }
2620
2621 static bool
2622 toc_relative_expr_p (rtx op)
2623 {
2624   int have_sym = 0;
2625   int have_toc = 0;
2626   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2627 }
2628
2629 /* SPE offset addressing is limited to 5-bits worth of double words.  */
2630 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
2631
2632 bool
2633 legitimate_constant_pool_address_p (rtx x)
2634 {
2635   return (TARGET_TOC
2636           && GET_CODE (x) == PLUS
2637           && GET_CODE (XEXP (x, 0)) == REG
2638           && (TARGET_MINIMAL_TOC || REGNO (XEXP (x, 0)) == TOC_REGISTER)
2639           && constant_pool_expr_p (XEXP (x, 1)));
2640 }
2641
2642 static bool
2643 legitimate_small_data_p (enum machine_mode mode, rtx x)
2644 {
2645   return (DEFAULT_ABI == ABI_V4
2646           && !flag_pic && !TARGET_TOC
2647           && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
2648           && small_data_operand (x, mode));
2649 }
2650
2651 static bool
2652 legitimate_offset_address_p (enum machine_mode mode, rtx x, int strict)
2653 {
2654   unsigned HOST_WIDE_INT offset, extra;
2655
2656   if (GET_CODE (x) != PLUS)
2657     return false;
2658   if (GET_CODE (XEXP (x, 0)) != REG)
2659     return false;
2660   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2661     return false;
2662   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2663     return false;
2664
2665   offset = INTVAL (XEXP (x, 1));
2666   extra = 0;
2667   switch (mode)
2668     {
2669     case V16QImode:
2670     case V8HImode:
2671     case V4SFmode:
2672     case V4SImode:
2673       /* AltiVec vector modes.  Only reg+reg addressing is valid here,
2674          which leaves the only valid constant offset of zero, which by
2675          canonicalization rules is also invalid.  */
2676       return false;
2677
2678     case V4HImode:
2679     case V2SImode:
2680     case V1DImode:
2681     case V2SFmode:
2682       /* SPE vector modes.  */
2683       return SPE_CONST_OFFSET_OK (offset);
2684
2685     case DFmode:
2686     case DImode:
2687       if (mode == DFmode || !TARGET_POWERPC64)
2688         extra = 4;
2689       else if (offset & 3)
2690         return false;
2691       break;
2692
2693     case TFmode:
2694     case TImode:
2695       if (mode == TFmode || !TARGET_POWERPC64)
2696         extra = 12;
2697       else if (offset & 3)
2698         return false;
2699       else
2700         extra = 8;
2701       break;
2702
2703     default:
2704       break;
2705     }
2706
2707   offset += 0x8000;
2708   return (offset < 0x10000) && (offset + extra < 0x10000);
2709 }
2710
2711 static bool
2712 legitimate_indexed_address_p (rtx x, int strict)
2713 {
2714   rtx op0, op1;
2715
2716   if (GET_CODE (x) != PLUS)
2717     return false;
2718   op0 = XEXP (x, 0);
2719   op1 = XEXP (x, 1);
2720
2721   if (!REG_P (op0) || !REG_P (op1))
2722     return false;
2723
2724   return ((INT_REG_OK_FOR_BASE_P (op0, strict)
2725            && INT_REG_OK_FOR_INDEX_P (op1, strict))
2726           || (INT_REG_OK_FOR_BASE_P (op1, strict)
2727               && INT_REG_OK_FOR_INDEX_P (op0, strict)));
2728 }
2729
2730 static inline bool
2731 legitimate_indirect_address_p (rtx x, int strict)
2732 {
2733   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
2734 }
2735
2736 static bool
2737 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
2738 {
2739     if (!TARGET_MACHO || !flag_pic
2740         || mode != SImode || GET_CODE(x) != MEM)
2741       return false;
2742     x = XEXP (x, 0);
2743
2744   if (GET_CODE (x) != LO_SUM)
2745     return false;
2746   if (GET_CODE (XEXP (x, 0)) != REG)
2747     return false;
2748   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
2749     return false;
2750   x = XEXP (x, 1);
2751
2752   return CONSTANT_P (x);
2753 }
2754
2755 static bool
2756 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
2757 {
2758   if (GET_CODE (x) != LO_SUM)
2759     return false;
2760   if (GET_CODE (XEXP (x, 0)) != REG)
2761     return false;
2762   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2763     return false;
2764   x = XEXP (x, 1);
2765
2766   if (TARGET_ELF || TARGET_MACHO)
2767     {
2768       if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
2769         return false;
2770       if (TARGET_TOC)
2771         return false;
2772       if (GET_MODE_NUNITS (mode) != 1)
2773         return false;
2774       if (GET_MODE_BITSIZE (mode) > 32
2775           && !(TARGET_HARD_FLOAT && TARGET_FPRS && mode == DFmode))
2776         return false;
2777
2778       return CONSTANT_P (x);
2779     }
2780
2781   return false;
2782 }
2783
2784
2785 /* Try machine-dependent ways of modifying an illegitimate address
2786    to be legitimate.  If we find one, return the new, valid address.
2787    This is used from only one place: `memory_address' in explow.c.
2788
2789    OLDX is the address as it was before break_out_memory_refs was
2790    called.  In some cases it is useful to look at this to decide what
2791    needs to be done.
2792
2793    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
2794
2795    It is always safe for this function to do nothing.  It exists to
2796    recognize opportunities to optimize the output.
2797
2798    On RS/6000, first check for the sum of a register with a constant
2799    integer that is out of range.  If so, generate code to add the
2800    constant with the low-order 16 bits masked to the register and force
2801    this result into another register (this can be done with `cau').
2802    Then generate an address of REG+(CONST&0xffff), allowing for the
2803    possibility of bit 16 being a one.
2804
2805    Then check for the sum of a register and something not constant, try to
2806    load the other things into a register and return the sum.  */
2807
2808 rtx
2809 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2810                            enum machine_mode mode)
2811 {
2812   if (GET_CODE (x) == SYMBOL_REF)
2813     {
2814       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
2815       if (model != 0)
2816         return rs6000_legitimize_tls_address (x, model);
2817     }
2818
2819   if (GET_CODE (x) == PLUS 
2820       && GET_CODE (XEXP (x, 0)) == REG
2821       && GET_CODE (XEXP (x, 1)) == CONST_INT
2822       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2823     { 
2824       HOST_WIDE_INT high_int, low_int;
2825       rtx sum;
2826       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2827       high_int = INTVAL (XEXP (x, 1)) - low_int;
2828       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2829                                          GEN_INT (high_int)), 0);
2830       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2831     }
2832   else if (GET_CODE (x) == PLUS 
2833            && GET_CODE (XEXP (x, 0)) == REG
2834            && GET_CODE (XEXP (x, 1)) != CONST_INT
2835            && GET_MODE_NUNITS (mode) == 1
2836            && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2837                || TARGET_POWERPC64
2838                || (mode != DFmode && mode != TFmode))
2839            && (TARGET_POWERPC64 || mode != DImode)
2840            && mode != TImode)
2841     {
2842       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2843                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2844     }
2845   else if (ALTIVEC_VECTOR_MODE (mode))
2846     {
2847       rtx reg;
2848
2849       /* Make sure both operands are registers.  */
2850       if (GET_CODE (x) == PLUS)
2851         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
2852                              force_reg (Pmode, XEXP (x, 1)));
2853
2854       reg = force_reg (Pmode, x);
2855       return reg;
2856     }
2857   else if (SPE_VECTOR_MODE (mode))
2858     {
2859       /* We accept [reg + reg] and [reg + OFFSET].  */
2860
2861       if (GET_CODE (x) == PLUS)
2862       {
2863         rtx op1 = XEXP (x, 0);
2864         rtx op2 = XEXP (x, 1);
2865
2866         op1 = force_reg (Pmode, op1);
2867
2868         if (GET_CODE (op2) != REG
2869             && (GET_CODE (op2) != CONST_INT
2870                 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2871           op2 = force_reg (Pmode, op2);
2872
2873         return gen_rtx_PLUS (Pmode, op1, op2);
2874       }
2875
2876       return force_reg (Pmode, x);
2877     }
2878   else if (TARGET_ELF
2879            && TARGET_32BIT
2880            && TARGET_NO_TOC
2881            && ! flag_pic
2882            && GET_CODE (x) != CONST_INT
2883            && GET_CODE (x) != CONST_DOUBLE 
2884            && CONSTANT_P (x)
2885            && GET_MODE_NUNITS (mode) == 1
2886            && (GET_MODE_BITSIZE (mode) <= 32
2887                || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
2888     {
2889       rtx reg = gen_reg_rtx (Pmode);
2890       emit_insn (gen_elf_high (reg, x));
2891       return gen_rtx_LO_SUM (Pmode, reg, x);
2892     }
2893   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2894            && ! flag_pic
2895 #if TARGET_MACHO
2896            && ! MACHO_DYNAMIC_NO_PIC_P
2897 #endif
2898            && GET_CODE (x) != CONST_INT
2899            && GET_CODE (x) != CONST_DOUBLE 
2900            && CONSTANT_P (x)
2901            && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
2902            && mode != DImode 
2903            && mode != TImode)
2904     {
2905       rtx reg = gen_reg_rtx (Pmode);
2906       emit_insn (gen_macho_high (reg, x));
2907       return gen_rtx_LO_SUM (Pmode, reg, x);
2908     }
2909   else if (TARGET_TOC 
2910            && constant_pool_expr_p (x)
2911            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
2912     {
2913       return create_TOC_reference (x);
2914     }
2915   else
2916     return NULL_RTX;
2917 }
2918
2919 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
2920    We need to emit DTP-relative relocations.  */
2921
2922 void
2923 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
2924 {
2925   switch (size)
2926     {
2927     case 4:
2928       fputs ("\t.long\t", file);
2929       break;
2930     case 8:
2931       fputs (DOUBLE_INT_ASM_OP, file);
2932       break;
2933     default:
2934       abort ();
2935     }
2936   output_addr_const (file, x);
2937   fputs ("@dtprel+0x8000", file);
2938 }
2939
2940 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
2941
2942 static GTY(()) rtx rs6000_tls_symbol;
2943 static rtx
2944 rs6000_tls_get_addr (void)
2945 {
2946   if (!rs6000_tls_symbol)
2947     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
2948
2949   return rs6000_tls_symbol;
2950 }
2951
2952 /* Construct the SYMBOL_REF for TLS GOT references.  */
2953
2954 static GTY(()) rtx rs6000_got_symbol;
2955 static rtx
2956 rs6000_got_sym (void)
2957 {
2958   if (!rs6000_got_symbol)
2959     {
2960       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2961       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
2962       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
2963     }     
2964
2965   return rs6000_got_symbol;
2966 }
2967
2968 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
2969    this (thread-local) address.  */
2970
2971 static rtx
2972 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
2973 {
2974   rtx dest, insn;
2975
2976   dest = gen_reg_rtx (Pmode);
2977   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
2978     {
2979       rtx tlsreg;
2980
2981       if (TARGET_64BIT)
2982         {
2983           tlsreg = gen_rtx_REG (Pmode, 13);
2984           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
2985         }
2986       else
2987         {
2988           tlsreg = gen_rtx_REG (Pmode, 2);
2989           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
2990         }
2991       emit_insn (insn);
2992     }
2993   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
2994     {
2995       rtx tlsreg, tmp;
2996
2997       tmp = gen_reg_rtx (Pmode);
2998       if (TARGET_64BIT)
2999         {
3000           tlsreg = gen_rtx_REG (Pmode, 13);
3001           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
3002         }
3003       else
3004         {
3005           tlsreg = gen_rtx_REG (Pmode, 2);
3006           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
3007         }
3008       emit_insn (insn);
3009       if (TARGET_64BIT)
3010         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
3011       else
3012         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
3013       emit_insn (insn);
3014     }
3015   else
3016     {
3017       rtx r3, got, tga, tmp1, tmp2, eqv;
3018
3019       if (TARGET_64BIT)
3020         got = gen_rtx_REG (Pmode, TOC_REGISTER);
3021       else
3022         {
3023           if (flag_pic == 1)
3024             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
3025           else
3026             {
3027               rtx gsym = rs6000_got_sym ();
3028               got = gen_reg_rtx (Pmode);
3029               if (flag_pic == 0)
3030                 rs6000_emit_move (got, gsym, Pmode);
3031               else
3032                 {
3033                   char buf[30];
3034                   static int tls_got_labelno = 0;
3035                   rtx tempLR, lab, tmp3, mem;
3036                   rtx first, last;
3037
3038                   ASM_GENERATE_INTERNAL_LABEL (buf, "LTLS", tls_got_labelno++);
3039                   lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
3040                   tempLR = gen_reg_rtx (Pmode);
3041                   tmp1 = gen_reg_rtx (Pmode);
3042                   tmp2 = gen_reg_rtx (Pmode);
3043                   tmp3 = gen_reg_rtx (Pmode);
3044                   mem = gen_rtx_MEM (Pmode, tmp1);
3045                   RTX_UNCHANGING_P (mem) = 1;
3046
3047                   first = emit_insn (gen_load_toc_v4_PIC_1b (tempLR, lab,
3048                                                              gsym));
3049                   emit_move_insn (tmp1, tempLR);
3050                   emit_move_insn (tmp2, mem);
3051                   emit_insn (gen_addsi3 (tmp3, tmp1, tmp2));
3052                   last = emit_move_insn (got, tmp3);
3053                   REG_NOTES (last) = gen_rtx_EXPR_LIST (REG_EQUAL, gsym,
3054                                                         REG_NOTES (last));
3055                   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
3056                                                          REG_NOTES (first));
3057                   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
3058                                                         REG_NOTES (last));
3059                 }
3060             }
3061         }
3062
3063       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
3064         {
3065           r3 = gen_rtx_REG (Pmode, 3);
3066           if (TARGET_64BIT)
3067             insn = gen_tls_gd_64 (r3, got, addr);
3068           else
3069             insn = gen_tls_gd_32 (r3, got, addr);
3070           start_sequence ();
3071           emit_insn (insn);
3072           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
3073           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
3074           insn = emit_call_insn (insn);
3075           CONST_OR_PURE_CALL_P (insn) = 1;
3076           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
3077           insn = get_insns ();
3078           end_sequence ();
3079           emit_libcall_block (insn, dest, r3, addr);
3080         }
3081       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
3082         {
3083           r3 = gen_rtx_REG (Pmode, 3);
3084           if (TARGET_64BIT)
3085             insn = gen_tls_ld_64 (r3, got);
3086           else
3087             insn = gen_tls_ld_32 (r3, got);
3088           start_sequence ();
3089           emit_insn (insn);
3090           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
3091           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
3092           insn = emit_call_insn (insn);
3093           CONST_OR_PURE_CALL_P (insn) = 1;
3094           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
3095           insn = get_insns ();
3096           end_sequence ();
3097           tmp1 = gen_reg_rtx (Pmode);
3098           eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3099                                 UNSPEC_TLSLD);
3100           emit_libcall_block (insn, tmp1, r3, eqv);
3101           if (rs6000_tls_size == 16)
3102             {
3103               if (TARGET_64BIT)
3104                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
3105               else
3106                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
3107             }
3108           else if (rs6000_tls_size == 32)
3109             {
3110               tmp2 = gen_reg_rtx (Pmode);
3111               if (TARGET_64BIT)
3112                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
3113               else
3114                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
3115               emit_insn (insn);
3116               if (TARGET_64BIT)
3117                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
3118               else
3119                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
3120             }
3121           else
3122             {
3123               tmp2 = gen_reg_rtx (Pmode);
3124               if (TARGET_64BIT)
3125                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
3126               else
3127                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
3128               emit_insn (insn);
3129               insn = gen_rtx_SET (Pmode, dest,
3130                                   gen_rtx_PLUS (Pmode, tmp2, tmp1));
3131             }
3132           emit_insn (insn);
3133         }
3134       else
3135         {
3136           /* IE, or 64 bit offset LE.  */
3137           tmp2 = gen_reg_rtx (Pmode);
3138           if (TARGET_64BIT)
3139             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
3140           else
3141             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
3142           emit_insn (insn);
3143           if (TARGET_64BIT)
3144             insn = gen_tls_tls_64 (dest, tmp2, addr);
3145           else
3146             insn = gen_tls_tls_32 (dest, tmp2, addr);
3147           emit_insn (insn);
3148         }
3149     }
3150
3151   return dest;
3152 }
3153
3154 /* Return 1 if X is a SYMBOL_REF for a TLS symbol.  This is used in
3155    instruction definitions.  */
3156
3157 int
3158 rs6000_tls_symbol_ref (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
3159 {
3160   return RS6000_SYMBOL_REF_TLS_P (x);
3161 }
3162
3163 /* Return 1 if X contains a thread-local symbol.  */
3164
3165 bool
3166 rs6000_tls_referenced_p (rtx x)
3167 {
3168   if (! TARGET_HAVE_TLS)
3169     return false;
3170
3171   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
3172 }
3173
3174 /* Return 1 if *X is a thread-local symbol.  This is the same as
3175    rs6000_tls_symbol_ref except for the type of the unused argument.  */
3176
3177 static inline int
3178 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
3179 {
3180   return RS6000_SYMBOL_REF_TLS_P (*x);
3181 }
3182
3183 /* The convention appears to be to define this wherever it is used.
3184    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
3185    is now used here.  */
3186 #ifndef REG_MODE_OK_FOR_BASE_P
3187 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
3188 #endif
3189
3190 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
3191    replace the input X, or the original X if no replacement is called for.
3192    The output parameter *WIN is 1 if the calling macro should goto WIN,
3193    0 if it should not.
3194
3195    For RS/6000, we wish to handle large displacements off a base
3196    register by splitting the addend across an addiu/addis and the mem insn.
3197    This cuts number of extra insns needed from 3 to 1.
3198
3199    On Darwin, we use this to generate code for floating point constants.
3200    A movsf_low is generated so we wind up with 2 instructions rather than 3.
3201    The Darwin code is inside #if TARGET_MACHO because only then is
3202    machopic_function_base_name() defined.  */
3203 rtx
3204 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode, 
3205         int opnum, int type, int ind_levels ATTRIBUTE_UNUSED, int *win)
3206 {
3207   /* We must recognize output that we have already generated ourselves.  */ 
3208   if (GET_CODE (x) == PLUS
3209       && GET_CODE (XEXP (x, 0)) == PLUS
3210       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
3211       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3212       && GET_CODE (XEXP (x, 1)) == CONST_INT)
3213     {
3214       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3215                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3216                    opnum, (enum reload_type)type);
3217       *win = 1;
3218       return x;
3219     }
3220
3221 #if TARGET_MACHO
3222   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
3223       && GET_CODE (x) == LO_SUM
3224       && GET_CODE (XEXP (x, 0)) == PLUS
3225       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
3226       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
3227       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
3228       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
3229       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
3230       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
3231       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
3232     {
3233       /* Result of previous invocation of this function on Darwin
3234          floating point constant.  */
3235       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3236                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3237                 opnum, (enum reload_type)type);
3238       *win = 1;
3239       return x;
3240     }
3241 #endif
3242   if (GET_CODE (x) == PLUS
3243       && GET_CODE (XEXP (x, 0)) == REG
3244       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
3245       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
3246       && GET_CODE (XEXP (x, 1)) == CONST_INT
3247       && !SPE_VECTOR_MODE (mode)
3248       && !ALTIVEC_VECTOR_MODE (mode))
3249     {
3250       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3251       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
3252       HOST_WIDE_INT high
3253         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
3254
3255       /* Check for 32-bit overflow.  */
3256       if (high + low != val)
3257         {
3258           *win = 0;
3259           return x;
3260         }
3261
3262       /* Reload the high part into a base reg; leave the low part
3263          in the mem directly.  */
3264
3265       x = gen_rtx_PLUS (GET_MODE (x),
3266                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
3267                                       GEN_INT (high)),
3268                         GEN_INT (low));
3269
3270       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3271                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3272                    opnum, (enum reload_type)type);
3273       *win = 1;
3274       return x;
3275     }
3276 #if TARGET_MACHO
3277   if (GET_CODE (x) == SYMBOL_REF
3278       && DEFAULT_ABI == ABI_DARWIN
3279       && !ALTIVEC_VECTOR_MODE (mode)
3280       && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
3281       /* Don't do this for TFmode, since the result isn't offsettable.  */
3282       && mode != TFmode)
3283     {
3284       if (flag_pic)
3285         {
3286           rtx offset = gen_rtx_CONST (Pmode,
3287                          gen_rtx_MINUS (Pmode, x,
3288                            gen_rtx_SYMBOL_REF (Pmode,
3289                              machopic_function_base_name ())));
3290           x = gen_rtx_LO_SUM (GET_MODE (x),
3291                 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3292                   gen_rtx_HIGH (Pmode, offset)), offset);
3293         }
3294       else
3295         x = gen_rtx_LO_SUM (GET_MODE (x),
3296               gen_rtx_HIGH (Pmode, x), x);
3297
3298       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3299                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3300                    opnum, (enum reload_type)type);
3301       *win = 1;
3302       return x;
3303     }
3304 #endif
3305   if (TARGET_TOC
3306       && constant_pool_expr_p (x)
3307       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
3308     {
3309       (x) = create_TOC_reference (x);
3310       *win = 1;
3311       return x;
3312     }
3313   *win = 0;
3314   return x;
3315 }    
3316
3317 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
3318    that is a valid memory address for an instruction.
3319    The MODE argument is the machine mode for the MEM expression
3320    that wants to use this address.
3321
3322    On the RS/6000, there are four valid address: a SYMBOL_REF that
3323    refers to a constant pool entry of an address (or the sum of it
3324    plus a constant), a short (16-bit signed) constant plus a register,
3325    the sum of two registers, or a register indirect, possibly with an
3326    auto-increment.  For DFmode and DImode with a constant plus register,
3327    we must ensure that both words are addressable or PowerPC64 with offset
3328    word aligned.
3329
3330    For modes spanning multiple registers (DFmode in 32-bit GPRs,
3331    32-bit DImode, TImode), indexed addressing cannot be used because
3332    adjacent memory cells are accessed by adding word-sized offsets
3333    during assembly output.  */
3334 int
3335 rs6000_legitimate_address (enum machine_mode mode, rtx x, int reg_ok_strict)
3336 {
3337   if (RS6000_SYMBOL_REF_TLS_P (x))
3338     return 0;
3339   if (legitimate_indirect_address_p (x, reg_ok_strict))
3340     return 1;
3341   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
3342       && !ALTIVEC_VECTOR_MODE (mode)
3343       && !SPE_VECTOR_MODE (mode)
3344       && TARGET_UPDATE
3345       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
3346     return 1;
3347   if (legitimate_small_data_p (mode, x))
3348     return 1;
3349   if (legitimate_constant_pool_address_p (x))
3350     return 1;
3351   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
3352   if (! reg_ok_strict
3353       && GET_CODE (x) == PLUS
3354       && GET_CODE (XEXP (x, 0)) == REG
3355       && (XEXP (x, 0) == virtual_stack_vars_rtx
3356          || XEXP (x, 0) == arg_pointer_rtx)
3357       && GET_CODE (XEXP (x, 1)) == CONST_INT)
3358     return 1;
3359   if (legitimate_offset_address_p (mode, x, reg_ok_strict))
3360     return 1;
3361   if (mode != TImode
3362       && ((TARGET_HARD_FLOAT && TARGET_FPRS)
3363           || TARGET_POWERPC64
3364           || (mode != DFmode && mode != TFmode))
3365       && (TARGET_POWERPC64 || mode != DImode)
3366       && legitimate_indexed_address_p (x, reg_ok_strict))
3367     return 1;
3368   if (legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
3369     return 1;
3370   return 0;
3371 }
3372
3373 /* Go to LABEL if ADDR (a legitimate address expression)
3374    has an effect that depends on the machine mode it is used for.
3375
3376    On the RS/6000 this is true of all integral offsets (since AltiVec
3377    modes don't allow them) or is a pre-increment or decrement.
3378
3379    ??? Except that due to conceptual problems in offsettable_address_p
3380    we can't really report the problems of integral offsets.  So leave
3381    this assuming that the adjustable offset must be valid for the 
3382    sub-words of a TFmode operand, which is what we had before.  */
3383
3384 bool
3385 rs6000_mode_dependent_address (rtx addr)
3386 {
3387   switch (GET_CODE (addr))
3388     {
3389     case PLUS:
3390       if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3391         {
3392           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
3393           return val + 12 + 0x8000 >= 0x10000;
3394         }
3395       break;
3396
3397     case LO_SUM:
3398       return true;
3399
3400     case PRE_INC:
3401     case PRE_DEC:
3402       return TARGET_UPDATE;
3403
3404     default:
3405       break;
3406     }
3407
3408   return false;
3409 }
3410
3411 /* Return number of consecutive hard regs needed starting at reg REGNO
3412    to hold something of mode MODE.
3413    This is ordinarily the length in words of a value of mode MODE
3414    but can be less for certain modes in special long registers.
3415
3416    For the SPE, GPRs are 64 bits but only 32 bits are visible in
3417    scalar instructions.  The upper 32 bits are only available to the
3418    SIMD instructions.
3419
3420    POWER and PowerPC GPRs hold 32 bits worth;
3421    PowerPC64 GPRs and FPRs point register holds 64 bits worth.  */
3422
3423 int
3424 rs6000_hard_regno_nregs (int regno, enum machine_mode mode)
3425 {
3426   if (FP_REGNO_P (regno))
3427     return (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
3428
3429   if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
3430     return (GET_MODE_SIZE (mode) + UNITS_PER_SPE_WORD - 1) / UNITS_PER_SPE_WORD;
3431
3432   if (ALTIVEC_REGNO_P (regno))
3433     return
3434       (GET_MODE_SIZE (mode) + UNITS_PER_ALTIVEC_WORD - 1) / UNITS_PER_ALTIVEC_WORD;
3435
3436   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3437 }
3438
3439 /* Change register usage conditional on target flags.  */
3440 void
3441 rs6000_conditional_register_usage (void)
3442 {
3443   int i;
3444
3445   /* Set MQ register fixed (already call_used) if not POWER
3446      architecture (RIOS1, RIOS2, RSC, and PPC601) so that it will not
3447      be allocated.  */
3448   if (! TARGET_POWER)
3449     fixed_regs[64] = 1;
3450
3451   /* 64-bit AIX reserves GPR13 for thread-private data.  */
3452   if (TARGET_64BIT)
3453     fixed_regs[13] = call_used_regs[13]
3454       = call_really_used_regs[13] = 1;
3455
3456   /* Conditionally disable FPRs.  */
3457   if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
3458     for (i = 32; i < 64; i++)
3459       fixed_regs[i] = call_used_regs[i]
3460         = call_really_used_regs[i] = 1;
3461
3462   if (DEFAULT_ABI == ABI_V4
3463       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3464       && flag_pic == 2)
3465     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
3466
3467   if (DEFAULT_ABI == ABI_V4
3468       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3469       && flag_pic == 1)
3470     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
3471       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
3472       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
3473
3474   if (DEFAULT_ABI == ABI_DARWIN
3475       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
3476     global_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
3477       = fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
3478       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
3479       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
3480
3481   if (TARGET_ALTIVEC)
3482     global_regs[VSCR_REGNO] = 1;
3483
3484   if (TARGET_SPE)
3485     {
3486       global_regs[SPEFSCR_REGNO] = 1;
3487       fixed_regs[FIXED_SCRATCH]
3488         = call_used_regs[FIXED_SCRATCH]
3489         = call_really_used_regs[FIXED_SCRATCH] = 1;
3490     }
3491
3492   if (! TARGET_ALTIVEC)
3493     {
3494       for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
3495         fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
3496       call_really_used_regs[VRSAVE_REGNO] = 1;
3497     }
3498
3499   if (TARGET_ALTIVEC_ABI)
3500     for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
3501       call_used_regs[i] = call_really_used_regs[i] = 1;
3502 }
3503 \f
3504 /* Try to output insns to set TARGET equal to the constant C if it can
3505    be done in less than N insns.  Do all computations in MODE.
3506    Returns the place where the output has been placed if it can be
3507    done and the insns have been emitted.  If it would take more than N
3508    insns, zero is returned and no insns and emitted.  */
3509
3510 rtx
3511 rs6000_emit_set_const (rtx dest, enum machine_mode mode, 
3512                        rtx source, int n ATTRIBUTE_UNUSED)
3513 {
3514   rtx result, insn, set;
3515   HOST_WIDE_INT c0, c1;
3516
3517   if (mode == QImode || mode == HImode)
3518     {
3519       if (dest == NULL)
3520         dest = gen_reg_rtx (mode);
3521       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
3522       return dest;
3523     }
3524   else if (mode == SImode)
3525     {
3526       result = no_new_pseudos ? dest : gen_reg_rtx (SImode);
3527
3528       emit_insn (gen_rtx_SET (VOIDmode, result,
3529                               GEN_INT (INTVAL (source)
3530                                        & (~ (HOST_WIDE_INT) 0xffff))));
3531       emit_insn (gen_rtx_SET (VOIDmode, dest,
3532                               gen_rtx_IOR (SImode, result,
3533                                            GEN_INT (INTVAL (source) & 0xffff))));
3534       result = dest;
3535     }
3536   else if (mode == DImode)
3537     {
3538       if (GET_CODE (source) == CONST_INT)
3539         {
3540           c0 = INTVAL (source);
3541           c1 = -(c0 < 0);
3542         }
3543       else if (GET_CODE (source) == CONST_DOUBLE)
3544         {
3545 #if HOST_BITS_PER_WIDE_INT >= 64
3546           c0 = CONST_DOUBLE_LOW (source);
3547           c1 = -(c0 < 0);
3548 #else
3549           c0 = CONST_DOUBLE_LOW (source);
3550           c1 = CONST_DOUBLE_HIGH (source);
3551 #endif
3552         }
3553       else
3554         abort ();
3555
3556       result = rs6000_emit_set_long_const (dest, c0, c1);
3557     }
3558   else
3559     abort ();
3560
3561   insn = get_last_insn ();
3562   set = single_set (insn);
3563   if (! CONSTANT_P (SET_SRC (set)))
3564     set_unique_reg_note (insn, REG_EQUAL, source);
3565
3566   return result;
3567 }
3568
3569 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
3570    fall back to a straight forward decomposition.  We do this to avoid
3571    exponential run times encountered when looking for longer sequences
3572    with rs6000_emit_set_const.  */
3573 static rtx
3574 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
3575 {
3576   if (!TARGET_POWERPC64)
3577     {
3578       rtx operand1, operand2;
3579
3580       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
3581                                         DImode);
3582       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
3583                                         DImode);
3584       emit_move_insn (operand1, GEN_INT (c1));
3585       emit_move_insn (operand2, GEN_INT (c2));
3586     }
3587   else
3588     {
3589       HOST_WIDE_INT ud1, ud2, ud3, ud4;
3590
3591       ud1 = c1 & 0xffff;
3592       ud2 = (c1 & 0xffff0000) >> 16;
3593 #if HOST_BITS_PER_WIDE_INT >= 64
3594       c2 = c1 >> 32;
3595 #endif
3596       ud3 = c2 & 0xffff;
3597       ud4 = (c2 & 0xffff0000) >> 16;
3598
3599       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
3600           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
3601         {
3602           if (ud1 & 0x8000)
3603             emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) -  0x8000)));
3604           else
3605             emit_move_insn (dest, GEN_INT (ud1));
3606         }
3607
3608       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
3609                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
3610         {
3611           if (ud2 & 0x8000)
3612             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
3613                                            - 0x80000000));
3614           else
3615             emit_move_insn (dest, GEN_INT (ud2 << 16));
3616           if (ud1 != 0)
3617             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3618         }
3619       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
3620                || (ud4 == 0 && ! (ud3 & 0x8000)))
3621         {
3622           if (ud3 & 0x8000)
3623             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
3624                                            - 0x80000000));
3625           else
3626             emit_move_insn (dest, GEN_INT (ud3 << 16));
3627
3628           if (ud2 != 0)
3629             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
3630           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
3631           if (ud1 != 0)
3632             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3633         }
3634       else 
3635         {
3636           if (ud4 & 0x8000)
3637             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
3638                                            - 0x80000000));
3639           else
3640             emit_move_insn (dest, GEN_INT (ud4 << 16));
3641
3642           if (ud3 != 0)
3643             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
3644
3645           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
3646           if (ud2 != 0)
3647             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
3648                                                GEN_INT (ud2 << 16)));   
3649           if (ud1 != 0)
3650             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3651         }
3652     }
3653   return dest;
3654 }
3655
3656 /* Emit a move from SOURCE to DEST in mode MODE.  */
3657 void
3658 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
3659 {
3660   rtx operands[2];
3661   operands[0] = dest;
3662   operands[1] = source;
3663   
3664   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
3665   if (GET_CODE (operands[1]) == CONST_DOUBLE
3666       && ! FLOAT_MODE_P (mode)
3667       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3668     {
3669       /* FIXME.  This should never happen.  */
3670       /* Since it seems that it does, do the safe thing and convert
3671          to a CONST_INT.  */
3672       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
3673     }
3674   if (GET_CODE (operands[1]) == CONST_DOUBLE
3675       && ! FLOAT_MODE_P (mode)
3676       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
3677            && CONST_DOUBLE_LOW (operands[1]) >= 0)
3678           || (CONST_DOUBLE_HIGH (operands[1]) == -1
3679               && CONST_DOUBLE_LOW (operands[1]) < 0)))
3680     abort ();
3681
3682   /* Check if GCC is setting up a block move that will end up using FP
3683      registers as temporaries.  We must make sure this is acceptable.  */
3684   if (GET_CODE (operands[0]) == MEM
3685       && GET_CODE (operands[1]) == MEM
3686       && mode == DImode
3687       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
3688           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
3689       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
3690                                             ? 32 : MEM_ALIGN (operands[0])))
3691             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
3692                                                ? 32 
3693                                                : MEM_ALIGN (operands[1]))))
3694       && ! MEM_VOLATILE_P (operands [0])
3695       && ! MEM_VOLATILE_P (operands [1]))
3696     {
3697       emit_move_insn (adjust_address (operands[0], SImode, 0),
3698                       adjust_address (operands[1], SImode, 0));
3699       emit_move_insn (adjust_address (operands[0], SImode, 4),
3700                       adjust_address (operands[1], SImode, 4));
3701       return;
3702     }
3703
3704   if (!no_new_pseudos)
3705     {
3706       if (GET_CODE (operands[1]) == MEM && optimize > 0
3707           && (mode == QImode || mode == HImode || mode == SImode)
3708           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3709         {
3710           rtx reg = gen_reg_rtx (word_mode);
3711
3712           emit_insn (gen_rtx_SET (word_mode, reg,
3713                                   gen_rtx_ZERO_EXTEND (word_mode,
3714                                                        operands[1])));
3715           operands[1] = gen_lowpart (mode, reg);
3716         }
3717       if (GET_CODE (operands[0]) != REG)
3718         operands[1] = force_reg (mode, operands[1]);
3719     }
3720
3721   if (mode == SFmode && ! TARGET_POWERPC
3722       && TARGET_HARD_FLOAT && TARGET_FPRS
3723       && GET_CODE (operands[0]) == MEM)
3724     {
3725       int regnum;
3726
3727       if (reload_in_progress || reload_completed)
3728         regnum = true_regnum (operands[1]);
3729       else if (GET_CODE (operands[1]) == REG)
3730         regnum = REGNO (operands[1]);
3731       else
3732         regnum = -1;
3733       
3734       /* If operands[1] is a register, on POWER it may have
3735          double-precision data in it, so truncate it to single
3736          precision.  */
3737       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
3738         {
3739           rtx newreg;
3740           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
3741           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
3742           operands[1] = newreg;
3743         }
3744     }
3745
3746   /* Recognize the case where operand[1] is a reference to thread-local
3747      data and load its address to a register.  */
3748   if (GET_CODE (operands[1]) == SYMBOL_REF)
3749     {
3750       enum tls_model model = SYMBOL_REF_TLS_MODEL (operands[1]);
3751       if (model != 0)
3752         operands[1] = rs6000_legitimize_tls_address (operands[1], model);
3753     }
3754
3755   /* Handle the case where reload calls us with an invalid address.  */
3756   if (reload_in_progress && mode == Pmode
3757       && (! general_operand (operands[1], mode)
3758           || ! nonimmediate_operand (operands[0], mode)))
3759     goto emit_set;
3760
3761   /* 128-bit constant floating-point values on Darwin should really be
3762      loaded as two parts.  */
3763   if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN)
3764       && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128
3765       && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
3766     {
3767       /* DImode is used, not DFmode, because simplify_gen_subreg doesn't
3768          know how to get a DFmode SUBREG of a TFmode.  */
3769       rs6000_emit_move (simplify_gen_subreg (DImode, operands[0], mode, 0),
3770                         simplify_gen_subreg (DImode, operands[1], mode, 0),
3771                         DImode);
3772       rs6000_emit_move (simplify_gen_subreg (DImode, operands[0], mode,
3773                                              GET_MODE_SIZE (DImode)),
3774                         simplify_gen_subreg (DImode, operands[1], mode,
3775                                              GET_MODE_SIZE (DImode)),
3776                         DImode);
3777       return;
3778     }
3779
3780   /* FIXME:  In the long term, this switch statement should go away
3781      and be replaced by a sequence of tests based on things like
3782      mode == Pmode.  */
3783   switch (mode)
3784     {
3785     case HImode:
3786     case QImode:
3787       if (CONSTANT_P (operands[1])
3788           && GET_CODE (operands[1]) != CONST_INT)
3789         operands[1] = force_const_mem (mode, operands[1]);
3790       break;
3791
3792     case TFmode:
3793     case DFmode:
3794     case SFmode:
3795       if (CONSTANT_P (operands[1]) 
3796           && ! easy_fp_constant (operands[1], mode))
3797         operands[1] = force_const_mem (mode, operands[1]);
3798       break;
3799       
3800     case V16QImode:
3801     case V8HImode:
3802     case V4SFmode:
3803     case V4SImode:
3804     case V4HImode:
3805     case V2SFmode:
3806     case V2SImode:
3807     case V1DImode:
3808       if (CONSTANT_P (operands[1])
3809           && !easy_vector_constant (operands[1], mode))
3810         operands[1] = force_const_mem (mode, operands[1]);
3811       break;
3812       
3813     case SImode:
3814     case DImode:
3815       /* Use default pattern for address of ELF small data */
3816       if (TARGET_ELF
3817           && mode == Pmode
3818           && DEFAULT_ABI == ABI_V4
3819           && (GET_CODE (operands[1]) == SYMBOL_REF 
3820               || GET_CODE (operands[1]) == CONST)
3821           && small_data_operand (operands[1], mode))
3822         {
3823           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3824           return;
3825         }
3826
3827       if (DEFAULT_ABI == ABI_V4
3828           && mode == Pmode && mode == SImode
3829           && flag_pic == 1 && got_operand (operands[1], mode))
3830         {
3831           emit_insn (gen_movsi_got (operands[0], operands[1]));
3832           return;
3833         }
3834
3835       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
3836           && TARGET_NO_TOC
3837           && ! flag_pic
3838           && mode == Pmode
3839           && CONSTANT_P (operands[1])
3840           && GET_CODE (operands[1]) != HIGH
3841           && GET_CODE (operands[1]) != CONST_INT)
3842         {
3843           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
3844
3845           /* If this is a function address on -mcall-aixdesc,
3846              convert it to the address of the descriptor.  */
3847           if (DEFAULT_ABI == ABI_AIX
3848               && GET_CODE (operands[1]) == SYMBOL_REF
3849               && XSTR (operands[1], 0)[0] == '.')
3850             {
3851               const char *name = XSTR (operands[1], 0);
3852               rtx new_ref;
3853               while (*name == '.')
3854                 name++;
3855               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
3856               CONSTANT_POOL_ADDRESS_P (new_ref)
3857                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
3858               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
3859               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
3860               SYMBOL_REF_DECL (new_ref) = SYMBOL_REF_DECL (operands[1]);
3861               operands[1] = new_ref;
3862             }
3863
3864           if (DEFAULT_ABI == ABI_DARWIN)
3865             {
3866 #if TARGET_MACHO
3867               if (MACHO_DYNAMIC_NO_PIC_P)
3868                 {
3869                   /* Take care of any required data indirection.  */
3870                   operands[1] = rs6000_machopic_legitimize_pic_address (
3871                                   operands[1], mode, operands[0]);
3872                   if (operands[0] != operands[1])
3873                     emit_insn (gen_rtx_SET (VOIDmode,
3874                                             operands[0], operands[1]));
3875                   return;
3876                 }
3877 #endif
3878               emit_insn (gen_macho_high (target, operands[1]));
3879               emit_insn (gen_macho_low (operands[0], target, operands[1]));
3880               return;
3881             }
3882
3883           emit_insn (gen_elf_high (target, operands[1]));
3884           emit_insn (gen_elf_low (operands[0], target, operands[1]));
3885           return;
3886         }
3887
3888       /* If this is a SYMBOL_REF that refers to a constant pool entry,
3889          and we have put it in the TOC, we just need to make a TOC-relative
3890          reference to it.  */
3891       if (TARGET_TOC
3892           && GET_CODE (operands[1]) == SYMBOL_REF
3893           && constant_pool_expr_p (operands[1])
3894           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
3895                                               get_pool_mode (operands[1])))
3896         {
3897           operands[1] = create_TOC_reference (operands[1]);
3898         }
3899       else if (mode == Pmode
3900                && CONSTANT_P (operands[1])
3901                && ((GET_CODE (operands[1]) != CONST_INT
3902                     && ! easy_fp_constant (operands[1], mode))
3903                    || (GET_CODE (operands[1]) == CONST_INT
3904                        && num_insns_constant (operands[1], mode) > 2)
3905                    || (GET_CODE (operands[0]) == REG
3906                        && FP_REGNO_P (REGNO (operands[0]))))
3907                && GET_CODE (operands[1]) != HIGH
3908                && ! legitimate_constant_pool_address_p (operands[1])
3909                && ! toc_relative_expr_p (operands[1]))
3910         {
3911           /* Emit a USE operation so that the constant isn't deleted if
3912              expensive optimizations are turned on because nobody
3913              references it.  This should only be done for operands that
3914              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
3915              This should not be done for operands that contain LABEL_REFs.
3916              For now, we just handle the obvious case.  */
3917           if (GET_CODE (operands[1]) != LABEL_REF)
3918             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
3919
3920 #if TARGET_MACHO
3921           /* Darwin uses a special PIC legitimizer.  */
3922           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
3923             {
3924               operands[1] =
3925                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
3926                                                         operands[0]);
3927               if (operands[0] != operands[1])
3928                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3929               return;
3930             }
3931 #endif
3932
3933           /* If we are to limit the number of things we put in the TOC and
3934              this is a symbol plus a constant we can add in one insn,
3935              just put the symbol in the TOC and add the constant.  Don't do
3936              this if reload is in progress.  */
3937           if (GET_CODE (operands[1]) == CONST
3938               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
3939               && GET_CODE (XEXP (operands[1], 0)) == PLUS
3940               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
3941               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
3942                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
3943               && ! side_effects_p (operands[0]))
3944             {
3945               rtx sym =
3946                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
3947               rtx other = XEXP (XEXP (operands[1], 0), 1);
3948
3949               sym = force_reg (mode, sym);
3950               if (mode == SImode)
3951                 emit_insn (gen_addsi3 (operands[0], sym, other));
3952               else
3953                 emit_insn (gen_adddi3 (operands[0], sym, other));
3954               return;
3955             }
3956
3957           operands[1] = force_const_mem (mode, operands[1]);
3958
3959           if (TARGET_TOC 
3960               && constant_pool_expr_p (XEXP (operands[1], 0))
3961               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
3962                         get_pool_constant (XEXP (operands[1], 0)),
3963                         get_pool_mode (XEXP (operands[1], 0))))
3964             {
3965               operands[1]
3966                 = gen_rtx_MEM (mode,
3967                                create_TOC_reference (XEXP (operands[1], 0)));
3968               set_mem_alias_set (operands[1], get_TOC_alias_set ());
3969               RTX_UNCHANGING_P (operands[1]) = 1;
3970             }
3971         }
3972       break;
3973
3974     case TImode:
3975       if (GET_CODE (operands[0]) == MEM
3976           && GET_CODE (XEXP (operands[0], 0)) != REG
3977           && ! reload_in_progress)
3978         operands[0]
3979           = replace_equiv_address (operands[0],
3980                                    copy_addr_to_reg (XEXP (operands[0], 0)));
3981
3982       if (GET_CODE (operands[1]) == MEM
3983           && GET_CODE (XEXP (operands[1], 0)) != REG
3984           && ! reload_in_progress)
3985         operands[1]
3986           = replace_equiv_address (operands[1],
3987                                    copy_addr_to_reg (XEXP (operands[1], 0)));
3988       if (TARGET_POWER)
3989         {
3990           emit_insn (gen_rtx_PARALLEL (VOIDmode,
3991                        gen_rtvec (2,
3992                                   gen_rtx_SET (VOIDmode,
3993                                                operands[0], operands[1]),
3994                                   gen_rtx_CLOBBER (VOIDmode,
3995                                                    gen_rtx_SCRATCH (SImode)))));
3996           return;
3997         }
3998       break;
3999
4000     default:
4001       abort ();
4002     }
4003
4004   /* Above, we may have called force_const_mem which may have returned
4005      an invalid address.  If we can, fix this up; otherwise, reload will
4006      have to deal with it.  */
4007   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
4008     operands[1] = validize_mem (operands[1]);
4009
4010  emit_set:
4011   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
4012 }
4013 \f
4014 /* Nonzero if we can use a floating-point register to pass this arg.  */
4015 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE)         \
4016   (GET_MODE_CLASS (MODE) == MODE_FLOAT          \
4017    && (CUM)->fregno <= FP_ARG_MAX_REG           \
4018    && TARGET_HARD_FLOAT && TARGET_FPRS)
4019
4020 /* Nonzero if we can use an AltiVec register to pass this arg.  */
4021 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED)      \
4022   (ALTIVEC_VECTOR_MODE (MODE)                           \
4023    && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG              \
4024    && TARGET_ALTIVEC_ABI                                \
4025    && (NAMED))
4026
4027 /* Return a nonzero value to say to return the function value in
4028    memory, just as large structures are always returned.  TYPE will be
4029    the data type of the value, and FNTYPE will be the type of the
4030    function doing the returning, or @code{NULL} for libcalls.
4031
4032    The AIX ABI for the RS/6000 specifies that all structures are
4033    returned in memory.  The Darwin ABI does the same.  The SVR4 ABI
4034    specifies that structures <= 8 bytes are returned in r3/r4, but a
4035    draft put them in memory, and GCC used to implement the draft
4036    instead of the final standard.  Therefore, TARGET_AIX_STRUCT_RET
4037    controls this instead of DEFAULT_ABI; V.4 targets needing backward
4038    compatibility can change DRAFT_V4_STRUCT_RET to override the
4039    default, and -m switches get the final word.  See
4040    rs6000_override_options for more details.
4041
4042    The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
4043    long double support is enabled.  These values are returned in memory.
4044
4045    int_size_in_bytes returns -1 for variable size objects, which go in
4046    memory always.  The cast to unsigned makes -1 > 8.  */
4047
4048 static bool
4049 rs6000_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
4050 {
4051   if (AGGREGATE_TYPE_P (type)
4052       && (TARGET_AIX_STRUCT_RET
4053           || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
4054     return true;
4055   if (DEFAULT_ABI == ABI_V4 && TYPE_MODE (type) == TFmode)
4056     return true;
4057   return false;
4058 }
4059
4060 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4061    for a call to a function whose data type is FNTYPE.
4062    For a library call, FNTYPE is 0.
4063
4064    For incoming args we set the number of arguments in the prototype large
4065    so we never return a PARALLEL.  */
4066
4067 void
4068 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, 
4069                       rtx libname ATTRIBUTE_UNUSED, int incoming,
4070                       int libcall, int n_named_args)
4071 {
4072   static CUMULATIVE_ARGS zero_cumulative;
4073
4074   *cum = zero_cumulative;
4075   cum->words = 0;
4076   cum->fregno = FP_ARG_MIN_REG;
4077   cum->vregno = ALTIVEC_ARG_MIN_REG;
4078   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
4079   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
4080                       ? CALL_LIBCALL : CALL_NORMAL);
4081   cum->sysv_gregno = GP_ARG_MIN_REG;
4082   cum->stdarg = fntype
4083     && (TYPE_ARG_TYPES (fntype) != 0
4084         && (TREE_VALUE (tree_last  (TYPE_ARG_TYPES (fntype)))
4085             != void_type_node));
4086
4087   cum->nargs_prototype = 0;
4088   if (incoming || cum->prototype)
4089     cum->nargs_prototype = n_named_args;
4090
4091   /* Check for a longcall attribute.  */
4092   if (fntype
4093       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
4094       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
4095     cum->call_cookie = CALL_LONG;
4096
4097   if (TARGET_DEBUG_ARG)
4098     {
4099       fprintf (stderr, "\ninit_cumulative_args:");
4100       if (fntype)
4101         {
4102           tree ret_type = TREE_TYPE (fntype);
4103           fprintf (stderr, " ret code = %s,",
4104                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
4105         }
4106
4107       if (cum->call_cookie & CALL_LONG)
4108         fprintf (stderr, " longcall,");
4109
4110       fprintf (stderr, " proto = %d, nargs = %d\n",
4111                cum->prototype, cum->nargs_prototype);
4112     }
4113   
4114     if (fntype 
4115         && !TARGET_ALTIVEC 
4116         && TARGET_ALTIVEC_ABI
4117         && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
4118       {
4119         error ("Cannot return value in vector register because"
4120                " altivec instructions are disabled, use -maltivec"
4121                " to enable them.");
4122       }
4123 }
4124 \f
4125 /* If defined, a C expression which determines whether, and in which
4126    direction, to pad out an argument with extra space.  The value
4127    should be of type `enum direction': either `upward' to pad above
4128    the argument, `downward' to pad below, or `none' to inhibit
4129    padding.
4130
4131    For the AIX ABI structs are always stored left shifted in their
4132    argument slot.  */
4133
4134 enum direction
4135 function_arg_padding (enum machine_mode mode, tree type)
4136 {
4137 #ifndef AGGREGATE_PADDING_FIXED
4138 #define AGGREGATE_PADDING_FIXED 0
4139 #endif
4140 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
4141 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
4142 #endif
4143
4144   if (!AGGREGATE_PADDING_FIXED)
4145     {
4146       /* GCC used to pass structures of the same size as integer types as
4147          if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
4148          ie. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
4149          passed padded downward, except that -mstrict-align further
4150          muddied the water in that multi-component structures of 2 and 4
4151          bytes in size were passed padded upward.
4152
4153          The following arranges for best compatibility with previous
4154          versions of gcc, but removes the -mstrict-align dependency.  */
4155       if (BYTES_BIG_ENDIAN)
4156         {
4157           HOST_WIDE_INT size = 0;
4158
4159           if (mode == BLKmode)
4160             {
4161               if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4162                 size = int_size_in_bytes (type);
4163             }
4164           else
4165             size = GET_MODE_SIZE (mode);
4166
4167           if (size == 1 || size == 2 || size == 4)
4168             return downward;
4169         }
4170       return upward;
4171     }
4172
4173   if (AGGREGATES_PAD_UPWARD_ALWAYS)
4174     {
4175       if (type != 0 && AGGREGATE_TYPE_P (type))
4176         return upward;
4177     }
4178
4179   /* Fall back to the default.  */
4180   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
4181 }
4182
4183 /* If defined, a C expression that gives the alignment boundary, in bits,
4184    of an argument with the specified mode and type.  If it is not defined, 
4185    PARM_BOUNDARY is used for all arguments.
4186    
4187    V.4 wants long longs to be double word aligned.  */
4188
4189 int
4190 function_arg_boundary (enum machine_mode mode, tree type ATTRIBUTE_UNUSED)
4191 {
4192   if (DEFAULT_ABI == ABI_V4 && GET_MODE_SIZE (mode) == 8)
4193     return 64;
4194   else if (SPE_VECTOR_MODE (mode))
4195     return 64;
4196   else if (ALTIVEC_VECTOR_MODE (mode))
4197     return 128;
4198   else
4199     return PARM_BOUNDARY;
4200 }
4201
4202 /* Compute the size (in words) of a function argument.  */
4203
4204 static unsigned long
4205 rs6000_arg_size (enum machine_mode mode, tree type)
4206 {
4207   unsigned long size;
4208
4209   if (mode != BLKmode)
4210     size = GET_MODE_SIZE (mode);
4211   else
4212     size = int_size_in_bytes (type);
4213
4214   if (TARGET_32BIT)
4215     return (size + 3) >> 2;
4216   else
4217     return (size + 7) >> 3;
4218 }
4219 \f
4220 /* Update the data in CUM to advance over an argument
4221    of mode MODE and data type TYPE.
4222    (TYPE is null for libcalls where that information may not be available.)
4223
4224    Note that for args passed by reference, function_arg will be called
4225    with MODE and TYPE set to that of the pointer to the arg, not the arg
4226    itself.  */
4227
4228 void
4229 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4230                       tree type, int named)
4231 {
4232   cum->nargs_prototype--;
4233
4234   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4235     {
4236       bool stack = false;
4237
4238       if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
4239         {
4240           cum->vregno++;
4241           if (!TARGET_ALTIVEC)
4242             error ("Cannot pass argument in vector register because"
4243                    " altivec instructions are disabled, use -maltivec"
4244                    " to enable them.");
4245
4246           /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
4247              even if it is going to be passed in a vector register.  
4248              Darwin does the same for variable-argument functions.  */
4249           if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
4250               || (cum->stdarg && DEFAULT_ABI != ABI_V4))
4251             stack = true;
4252         }
4253       else
4254         stack = true;
4255
4256       if (stack)
4257         {
4258           int align;
4259           
4260           /* Vector parameters must be 16-byte aligned.  This places
4261              them at 2 mod 4 in terms of words in 32-bit mode, since
4262              the parameter save area starts at offset 24 from the
4263              stack.  In 64-bit mode, they just have to start on an
4264              even word, since the parameter save area is 16-byte
4265              aligned.  Space for GPRs is reserved even if the argument
4266              will be passed in memory.  */
4267           if (TARGET_32BIT)
4268             align = (2 - cum->words) & 3;
4269           else
4270             align = cum->words & 1;
4271           cum->words += align + rs6000_arg_size (mode, type);
4272           
4273           if (TARGET_DEBUG_ARG)
4274             {
4275               fprintf (stderr, "function_adv: words = %2d, align=%d, ", 
4276                        cum->words, align);
4277               fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
4278                        cum->nargs_prototype, cum->prototype, 
4279                        GET_MODE_NAME (mode));
4280             }
4281         }
4282     }
4283   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
4284            && !cum->stdarg
4285            && cum->sysv_gregno <= GP_ARG_MAX_REG)
4286     cum->sysv_gregno++;
4287   else if (DEFAULT_ABI == ABI_V4)
4288     {
4289       if (TARGET_HARD_FLOAT && TARGET_FPRS
4290           && (mode == SFmode || mode == DFmode))
4291         {
4292           if (cum->fregno <= FP_ARG_V4_MAX_REG)
4293             cum->fregno++;
4294           else
4295             {
4296               if (mode == DFmode)
4297                 cum->words += cum->words & 1;
4298               cum->words += rs6000_arg_size (mode, type);
4299             }
4300         }
4301       else
4302         {
4303           int n_words = rs6000_arg_size (mode, type);
4304           int gregno = cum->sysv_gregno;
4305
4306           /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
4307              (r7,r8) or (r9,r10).  As does any other 2 word item such
4308              as complex int due to a historical mistake.  */
4309           if (n_words == 2)
4310             gregno += (1 - gregno) & 1;
4311
4312           /* Multi-reg args are not split between registers and stack.  */
4313           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
4314             {
4315               /* Long long and SPE vectors are aligned on the stack.
4316                  So are other 2 word items such as complex int due to
4317                  a historical mistake.  */
4318               if (n_words == 2)
4319                 cum->words += cum->words & 1;
4320               cum->words += n_words;
4321             }
4322
4323           /* Note: continuing to accumulate gregno past when we've started
4324              spilling to the stack indicates the fact that we've started
4325              spilling to the stack to expand_builtin_saveregs.  */
4326           cum->sysv_gregno = gregno + n_words;
4327         }
4328
4329       if (TARGET_DEBUG_ARG)
4330         {
4331           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
4332                    cum->words, cum->fregno);
4333           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
4334                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
4335           fprintf (stderr, "mode = %4s, named = %d\n",
4336                    GET_MODE_NAME (mode), named);
4337         }
4338     }
4339   else
4340     {
4341       int n_words = rs6000_arg_size (mode, type);
4342       int align = function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
4343
4344       /* The simple alignment calculation here works because
4345          function_arg_boundary / PARM_BOUNDARY will only be 1 or 2.
4346          If we ever want to handle alignments larger than 8 bytes for
4347          32-bit or 16 bytes for 64-bit, then we'll need to take into
4348          account the offset to the start of the parm save area.  */
4349       align &= cum->words;
4350       cum->words += align + n_words;
4351
4352       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4353           && TARGET_HARD_FLOAT && TARGET_FPRS)
4354         cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
4355
4356       if (TARGET_DEBUG_ARG)
4357         {
4358           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
4359                    cum->words, cum->fregno);
4360           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
4361                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
4362           fprintf (stderr, "named = %d, align = %d\n", named, align);
4363         }
4364     }
4365 }
4366
4367 /* Determine where to put a SIMD argument on the SPE.  */
4368
4369 static rtx
4370 rs6000_spe_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4371                          tree type)
4372 {
4373   if (cum->stdarg)
4374     {
4375       int gregno = cum->sysv_gregno;
4376       int n_words = rs6000_arg_size (mode, type);
4377
4378       /* SPE vectors are put in odd registers.  */
4379       if (n_words == 2 && (gregno & 1) == 0)
4380         gregno += 1;
4381
4382       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4383         {
4384           rtx r1, r2;
4385           enum machine_mode m = SImode;
4386
4387           r1 = gen_rtx_REG (m, gregno);
4388           r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
4389           r2 = gen_rtx_REG (m, gregno + 1);
4390           r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
4391           return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
4392         }
4393       else
4394         return NULL_RTX;
4395     }
4396   else
4397     {
4398       if (cum->sysv_gregno <= GP_ARG_MAX_REG)
4399         return gen_rtx_REG (mode, cum->sysv_gregno);
4400       else
4401         return NULL_RTX;
4402     }
4403 }
4404
4405 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
4406
4407 static rtx
4408 rs6000_mixed_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4409                            tree type, int align_words)
4410 {
4411   if (mode == DFmode)
4412     {
4413       /* -mpowerpc64 with 32bit ABI splits up a DFmode argument
4414          in vararg list into zero, one or two GPRs */
4415       if (align_words >= GP_ARG_NUM_REG)
4416         return gen_rtx_PARALLEL (DFmode,
4417                  gen_rtvec (2,
4418                             gen_rtx_EXPR_LIST (VOIDmode,
4419                                                NULL_RTX, const0_rtx), 
4420                             gen_rtx_EXPR_LIST (VOIDmode,
4421                                                gen_rtx_REG (mode,
4422                                                             cum->fregno),
4423                                                const0_rtx)));
4424       else if (align_words + rs6000_arg_size (mode, type)
4425                > GP_ARG_NUM_REG)
4426         /* If this is partially on the stack, then we only
4427            include the portion actually in registers here.  */
4428         return gen_rtx_PARALLEL (DFmode,
4429                  gen_rtvec (2,   
4430                             gen_rtx_EXPR_LIST (VOIDmode,
4431                                                gen_rtx_REG (SImode,
4432                                                             GP_ARG_MIN_REG
4433                                                             + align_words),
4434                                                const0_rtx),
4435                             gen_rtx_EXPR_LIST (VOIDmode,
4436                                                gen_rtx_REG (mode,
4437                                                             cum->fregno),
4438                                                const0_rtx)));
4439
4440       /* split a DFmode arg into two GPRs */
4441       return gen_rtx_PARALLEL (DFmode,
4442                gen_rtvec (3,
4443                           gen_rtx_EXPR_LIST (VOIDmode,       
4444                                              gen_rtx_REG (SImode,
4445                                                           GP_ARG_MIN_REG
4446                                                           + align_words),
4447                                              const0_rtx),
4448                           gen_rtx_EXPR_LIST (VOIDmode,
4449                                              gen_rtx_REG (SImode,
4450                                                           GP_ARG_MIN_REG
4451                                                           + align_words + 1),
4452                                              GEN_INT (4)),
4453                           gen_rtx_EXPR_LIST (VOIDmode,
4454                                              gen_rtx_REG (mode, cum->fregno),
4455                                              const0_rtx)));
4456     }
4457   /* -mpowerpc64 with 32bit ABI splits up a DImode argument into one
4458      or two GPRs */
4459   else if (mode == DImode)
4460     {
4461       if (align_words < GP_ARG_NUM_REG - 1)
4462         return gen_rtx_PARALLEL (DImode,
4463                  gen_rtvec (2,
4464                             gen_rtx_EXPR_LIST (VOIDmode,
4465                                                gen_rtx_REG (SImode,
4466                                                             GP_ARG_MIN_REG
4467                                                             + align_words),
4468                                                const0_rtx),
4469                             gen_rtx_EXPR_LIST (VOIDmode,
4470                                                gen_rtx_REG (SImode,
4471                                                             GP_ARG_MIN_REG
4472                                                             + align_words + 1),
4473                                                GEN_INT (4))));
4474       else if (align_words == GP_ARG_NUM_REG - 1)
4475           return gen_rtx_PARALLEL (DImode,
4476                    gen_rtvec (2,
4477                               gen_rtx_EXPR_LIST (VOIDmode,
4478                                                  NULL_RTX, const0_rtx),
4479                               gen_rtx_EXPR_LIST (VOIDmode,
4480                                                  gen_rtx_REG (SImode,
4481                                                               GP_ARG_MIN_REG
4482                                                               + align_words),
4483                                                  const0_rtx)));
4484     }
4485   else if (ALTIVEC_VECTOR_MODE (mode) && align_words == GP_ARG_NUM_REG - 2)
4486     {
4487       /* Varargs vector regs must be saved in R9-R10.  */
4488       return gen_rtx_PARALLEL (mode,
4489                                gen_rtvec (3,
4490                                  gen_rtx_EXPR_LIST (VOIDmode,
4491                                                      NULL_RTX, const0_rtx),
4492                                  gen_rtx_EXPR_LIST (VOIDmode,
4493                                                     gen_rtx_REG (SImode,
4494                                                                  GP_ARG_MIN_REG
4495                                                                  + align_words),
4496                                                     const0_rtx),
4497                                  gen_rtx_EXPR_LIST (VOIDmode,
4498                                                     gen_rtx_REG (SImode,
4499                                                                  GP_ARG_MIN_REG
4500                                                                  + align_words + 1),
4501                                                     GEN_INT (4))));
4502     }
4503   else if ((mode == BLKmode || ALTIVEC_VECTOR_MODE (mode))
4504            && align_words <= (GP_ARG_NUM_REG - 1))
4505     {
4506       /* AltiVec vector regs are saved in R5-R8. */
4507       int k;
4508       int size = int_size_in_bytes (type);
4509       int no_units = ((size - 1) / 4) + 1;
4510       int max_no_words = GP_ARG_NUM_REG - align_words;
4511       int rtlvec_len = no_units < max_no_words ? no_units : max_no_words;
4512       rtx *rtlvec = (rtx *) alloca (rtlvec_len * sizeof (rtx));
4513
4514       memset ((char *) rtlvec, 0, rtlvec_len * sizeof (rtx));
4515
4516       for (k=0; k < rtlvec_len; k++)
4517         rtlvec[k] = gen_rtx_EXPR_LIST (VOIDmode,
4518                                        gen_rtx_REG (SImode,
4519                                                     GP_ARG_MIN_REG
4520                                                     + align_words + k),
4521                                        k == 0 ? const0_rtx : GEN_INT (k*4));
4522
4523       return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rtlvec));
4524     }
4525   return NULL_RTX;
4526 }
4527
4528 /* Determine where to put an argument to a function.
4529    Value is zero to push the argument on the stack,
4530    or a hard register in which to store the argument.
4531
4532    MODE is the argument's machine mode.
4533    TYPE is the data type of the argument (as a tree).
4534     This is null for libcalls where that information may
4535     not be available.
4536    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4537     the preceding args and about the function being called.
4538    NAMED is nonzero if this argument is a named parameter
4539     (otherwise it is an extra parameter matching an ellipsis).
4540
4541    On RS/6000 the first eight words of non-FP are normally in registers
4542    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
4543    Under V.4, the first 8 FP args are in registers.
4544
4545    If this is floating-point and no prototype is specified, we use
4546    both an FP and integer register (or possibly FP reg and stack).  Library
4547    functions (when CALL_LIBCALL is set) always have the proper types for args,
4548    so we can pass the FP value just in one register.  emit_library_function
4549    doesn't support PARALLEL anyway.
4550
4551    Note that for args passed by reference, function_arg will be called
4552    with MODE and TYPE set to that of the pointer to the arg, not the arg
4553    itself.  */
4554
4555 struct rtx_def *
4556 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4557               tree type, int named)
4558 {
4559   enum rs6000_abi abi = DEFAULT_ABI;
4560
4561   /* Return a marker to indicate whether CR1 needs to set or clear the
4562      bit that V.4 uses to say fp args were passed in registers.
4563      Assume that we don't need the marker for software floating point,
4564      or compiler generated library calls.  */
4565   if (mode == VOIDmode)
4566     {
4567       if (abi == ABI_V4
4568           && cum->nargs_prototype < 0
4569           && (cum->call_cookie & CALL_LIBCALL) == 0
4570           && (cum->prototype || TARGET_NO_PROTOTYPE))
4571         {
4572           /* For the SPE, we need to crxor CR6 always.  */
4573           if (TARGET_SPE_ABI)
4574             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
4575           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
4576             return GEN_INT (cum->call_cookie
4577                             | ((cum->fregno == FP_ARG_MIN_REG)
4578                                ? CALL_V4_SET_FP_ARGS
4579                                : CALL_V4_CLEAR_FP_ARGS));
4580         }
4581
4582       return GEN_INT (cum->call_cookie);
4583     }
4584
4585   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
4586     if (TARGET_64BIT && ! cum->prototype)
4587       {
4588        /* Vector parameters get passed in vector register
4589           and also in GPRs or memory, in absence of prototype.  */
4590        int align_words;
4591        rtx slot;
4592        align_words = (cum->words + 1) & ~1;
4593
4594        if (align_words >= GP_ARG_NUM_REG)
4595          {
4596            slot = NULL_RTX;
4597          }
4598        else
4599          {
4600            slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4601          }
4602        return gen_rtx_PARALLEL (mode,
4603                 gen_rtvec (2,
4604                            gen_rtx_EXPR_LIST (VOIDmode,
4605                                               slot, const0_rtx),
4606                            gen_rtx_EXPR_LIST (VOIDmode,
4607                                               gen_rtx_REG (mode, cum->vregno),
4608                                               const0_rtx)));
4609       }
4610     else
4611       return gen_rtx_REG (mode, cum->vregno);
4612   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4613     {
4614       if (named || abi == ABI_V4)
4615         return NULL_RTX;
4616       else
4617         {
4618           /* Vector parameters to varargs functions under AIX or Darwin
4619              get passed in memory and possibly also in GPRs.  */
4620           int align, align_words;
4621           enum machine_mode part_mode = mode;
4622
4623           /* Vector parameters must be 16-byte aligned.  This places them at
4624              2 mod 4 in terms of words in 32-bit mode, since the parameter
4625              save area starts at offset 24 from the stack.  In 64-bit mode,
4626              they just have to start on an even word, since the parameter
4627              save area is 16-byte aligned.  */
4628           if (TARGET_32BIT)
4629             align = (2 - cum->words) & 3;
4630           else
4631             align = cum->words & 1;
4632           align_words = cum->words + align;
4633
4634           /* Out of registers?  Memory, then.  */
4635           if (align_words >= GP_ARG_NUM_REG)
4636             return NULL_RTX;
4637           
4638           /* The vector value goes in GPRs.  Only the part of the
4639              value in GPRs is reported here.  */
4640           if (align_words + CLASS_MAX_NREGS (mode, GENERAL_REGS)
4641               > GP_ARG_NUM_REG)
4642             /* Fortunately, there are only two possibilities, the value
4643                is either wholly in GPRs or half in GPRs and half not.  */
4644             part_mode = DImode;
4645           
4646           if (TARGET_32BIT
4647               && (TARGET_POWERPC64 || (align_words == GP_ARG_NUM_REG - 2)))
4648             return rs6000_mixed_function_arg (cum, part_mode, type, align_words);
4649           else
4650             return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
4651         }
4652     }
4653   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
4654     return rs6000_spe_function_arg (cum, mode, type);
4655   else if (abi == ABI_V4)
4656     {
4657       if (TARGET_HARD_FLOAT && TARGET_FPRS
4658           && (mode == SFmode || mode == DFmode))
4659         {
4660           if (cum->fregno <= FP_ARG_V4_MAX_REG)
4661             return gen_rtx_REG (mode, cum->fregno);
4662           else
4663             return NULL_RTX;
4664         }
4665       else
4666         {
4667           int n_words = rs6000_arg_size (mode, type);
4668           int gregno = cum->sysv_gregno;
4669
4670           /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
4671              (r7,r8) or (r9,r10).  As does any other 2 word item such
4672              as complex int due to a historical mistake.  */
4673           if (n_words == 2)
4674             gregno += (1 - gregno) & 1;
4675
4676           /* Multi-reg args are not split between registers and stack.  */
4677           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4678             return gen_rtx_REG (mode, gregno);
4679           else
4680             return NULL_RTX;
4681         }
4682     }
4683   else
4684     {
4685       int align = function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
4686       int align_words = cum->words + (cum->words & align);
4687
4688       if (USE_FP_FOR_ARG_P (cum, mode, type))
4689         {
4690           rtx fpr[2];
4691           rtx *r;
4692           bool needs_psave;
4693           enum machine_mode fmode = mode;
4694           int n;
4695           unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
4696
4697           if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
4698             {
4699               /* Long double split over regs and memory.  */
4700               if (fmode == TFmode)
4701                 fmode = DFmode;
4702
4703               /* Currently, we only ever need one reg here because complex
4704                  doubles are split.  */
4705               if (cum->fregno != FP_ARG_MAX_REG - 1)
4706                 abort ();
4707             }
4708           fpr[1] = gen_rtx_REG (fmode, cum->fregno);
4709
4710           /* Do we also need to pass this arg in the parameter save
4711              area?  */
4712           needs_psave = (type
4713                          && (cum->nargs_prototype <= 0
4714                              || (DEFAULT_ABI == ABI_AIX
4715                                  && TARGET_XL_CALL
4716                                  && align_words >= GP_ARG_NUM_REG)));
4717
4718           if (!needs_psave && mode == fmode)
4719             return fpr[1];
4720
4721           if (TARGET_32BIT && TARGET_POWERPC64
4722               && mode == DFmode && cum->stdarg)
4723             return rs6000_mixed_function_arg (cum, mode, type, align_words);
4724
4725           /* Describe where this piece goes.  */
4726           r = fpr + 1;
4727           *r = gen_rtx_EXPR_LIST (VOIDmode, *r, const0_rtx);
4728           n = 1;
4729
4730           if (needs_psave)
4731             {
4732               /* Now describe the part that goes in gprs or the stack.
4733                  This piece must come first, before the fprs.  */
4734               rtx reg = NULL_RTX;
4735               if (align_words < GP_ARG_NUM_REG)
4736                 {
4737                   unsigned long n_words = rs6000_arg_size (mode, type);
4738                   enum machine_mode rmode = mode;
4739
4740                   if (align_words + n_words > GP_ARG_NUM_REG)
4741                     /* If this is partially on the stack, then we only
4742                        include the portion actually in registers here.
4743                        We know this can only be one register because
4744                        complex doubles are splt.  */
4745                     rmode = Pmode;
4746                   reg = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
4747                 }
4748               *--r = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
4749               ++n;
4750             }
4751
4752           return gen_rtx_PARALLEL (mode, gen_rtvec_v (n, r));
4753         }
4754       else if (align_words < GP_ARG_NUM_REG)
4755         {
4756           if (TARGET_32BIT && TARGET_POWERPC64
4757               && (mode == DImode || mode == BLKmode))
4758             return rs6000_mixed_function_arg (cum, mode, type, align_words);
4759
4760           return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4761         }
4762       else
4763         return NULL_RTX;
4764     }
4765 }
4766 \f
4767 /* For an arg passed partly in registers and partly in memory,
4768    this is the number of registers used.
4769    For args passed entirely in registers or entirely in memory, zero.  */
4770
4771 int
4772 function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4773                             tree type, int named)
4774 {
4775   int ret = 0;
4776
4777   if (DEFAULT_ABI == ABI_V4)
4778     return 0;
4779
4780   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
4781       && cum->nargs_prototype >= 0)
4782     return 0;
4783
4784   if (USE_FP_FOR_ARG_P (cum, mode, type))
4785     {
4786       if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3) > FP_ARG_MAX_REG + 1)
4787         ret = FP_ARG_MAX_REG - cum->fregno;
4788       else if (cum->nargs_prototype >= 0)
4789         return 0;
4790     }
4791
4792   if (cum->words < GP_ARG_NUM_REG
4793       && GP_ARG_NUM_REG < cum->words + rs6000_arg_size (mode, type))
4794     ret = GP_ARG_NUM_REG - cum->words;
4795
4796   if (ret != 0 && TARGET_DEBUG_ARG)
4797     fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
4798
4799   return ret;
4800 }
4801 \f
4802 /* A C expression that indicates when an argument must be passed by
4803    reference.  If nonzero for an argument, a copy of that argument is
4804    made in memory and a pointer to the argument is passed instead of
4805    the argument itself.  The pointer is passed in whatever way is
4806    appropriate for passing a pointer to that type.
4807
4808    Under V.4, aggregates and long double are passed by reference.
4809
4810    As an extension to all 32-bit ABIs, AltiVec vectors are passed by
4811    reference unless the AltiVec vector extension ABI is in force.
4812
4813    As an extension to all ABIs, variable sized types are passed by
4814    reference.  */
4815
4816 int
4817 function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED, 
4818                                 enum machine_mode mode ATTRIBUTE_UNUSED, 
4819                                 tree type, int named ATTRIBUTE_UNUSED)
4820 {
4821   if ((DEFAULT_ABI == ABI_V4
4822        && ((type && AGGREGATE_TYPE_P (type))
4823            || mode == TFmode))
4824       || (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4825       || (type && int_size_in_bytes (type) < 0))
4826     {
4827       if (TARGET_DEBUG_ARG)
4828         fprintf (stderr, "function_arg_pass_by_reference\n");
4829
4830       return 1;
4831     }
4832   return 0;
4833 }
4834
4835 static void
4836 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
4837 {
4838   int i;
4839   enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
4840
4841   if (nregs == 0)
4842     return;
4843
4844     for (i = 0; i < nregs; i++)
4845     {
4846       rtx tem = adjust_address_nv (x, reg_mode, i*GET_MODE_SIZE(reg_mode));
4847       if (reload_completed)
4848       {
4849         if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
4850           tem = NULL_RTX;
4851         else
4852           tem = simplify_gen_subreg (reg_mode, x, BLKmode, 
4853                                      i * GET_MODE_SIZE(reg_mode));
4854       }
4855       else
4856         tem = replace_equiv_address (tem, XEXP (tem, 0));
4857
4858       if (tem == NULL_RTX)
4859         abort ();
4860
4861       emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
4862     }
4863 }
4864
4865 \f
4866 /* Perform any needed actions needed for a function that is receiving a
4867    variable number of arguments. 
4868
4869    CUM is as above.
4870
4871    MODE and TYPE are the mode and type of the current parameter.
4872
4873    PRETEND_SIZE is a variable that should be set to the amount of stack
4874    that must be pushed by the prolog to pretend that our caller pushed
4875    it.
4876
4877    Normally, this macro will push all remaining incoming registers on the
4878    stack and set PRETEND_SIZE to the length of the registers pushed.  */
4879
4880 static void
4881 setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4882                 tree type, int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
4883 {
4884   CUMULATIVE_ARGS next_cum;
4885   int reg_size = TARGET_32BIT ? 4 : 8;
4886   rtx save_area = NULL_RTX, mem;
4887   int first_reg_offset, set;
4888
4889   /* Skip the last named argument.  */
4890   next_cum = *cum;
4891   function_arg_advance (&next_cum, mode, type, 1);
4892
4893   if (DEFAULT_ABI == ABI_V4)
4894     {
4895       /* Indicate to allocate space on the stack for varargs save area.  */
4896       cfun->machine->sysv_varargs_p = 1;
4897       if (! no_rtl)
4898         save_area = plus_constant (virtual_stack_vars_rtx,
4899                                    - RS6000_VARARGS_SIZE);
4900
4901       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
4902     }
4903   else
4904     {
4905       first_reg_offset = next_cum.words;
4906       save_area = virtual_incoming_args_rtx;
4907       cfun->machine->sysv_varargs_p = 0;
4908
4909       if (MUST_PASS_IN_STACK (mode, type))
4910         first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
4911     }
4912
4913   set = get_varargs_alias_set ();
4914   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
4915     {
4916       mem = gen_rtx_MEM (BLKmode,
4917                          plus_constant (save_area,
4918                                         first_reg_offset * reg_size)),
4919       set_mem_alias_set (mem, set);
4920       set_mem_align (mem, BITS_PER_WORD);
4921
4922       rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem, 
4923                                   GP_ARG_NUM_REG - first_reg_offset);
4924     }
4925
4926   /* Save FP registers if needed.  */
4927   if (DEFAULT_ABI == ABI_V4
4928       && TARGET_HARD_FLOAT && TARGET_FPRS
4929       && ! no_rtl
4930       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
4931     {
4932       int fregno = next_cum.fregno;
4933       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
4934       rtx lab = gen_label_rtx ();
4935       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
4936
4937       emit_jump_insn (gen_rtx_SET (VOIDmode,
4938                                    pc_rtx,
4939                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
4940                                             gen_rtx_NE (VOIDmode, cr1,
4941                                                         const0_rtx),
4942                                             gen_rtx_LABEL_REF (VOIDmode, lab),
4943                                             pc_rtx)));
4944
4945       while (fregno <= FP_ARG_V4_MAX_REG)
4946         {
4947           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
4948           set_mem_alias_set (mem, set);
4949           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
4950           fregno++;
4951           off += 8;
4952         }
4953
4954       emit_label (lab);
4955     }
4956 }
4957
4958 /* Create the va_list data type.  */
4959
4960 static tree
4961 rs6000_build_builtin_va_list (void)
4962 {
4963   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
4964
4965   /* For AIX, prefer 'char *' because that's what the system
4966      header files like.  */
4967   if (DEFAULT_ABI != ABI_V4)
4968     return build_pointer_type (char_type_node);
4969
4970   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4971   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4972
4973   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
4974                       unsigned_char_type_node);
4975   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
4976                       unsigned_char_type_node);
4977   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
4978      every user file.  */
4979   f_res = build_decl (FIELD_DECL, get_identifier ("reserved"),
4980                       short_unsigned_type_node);
4981   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4982                       ptr_type_node);
4983   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4984                       ptr_type_node);
4985
4986   DECL_FIELD_CONTEXT (f_gpr) = record;
4987   DECL_FIELD_CONTEXT (f_fpr) = record;
4988   DECL_FIELD_CONTEXT (f_res) = record;
4989   DECL_FIELD_CONTEXT (f_ovf) = record;
4990   DECL_FIELD_CONTEXT (f_sav) = record;
4991
4992   TREE_CHAIN (record) = type_decl;
4993   TYPE_NAME (record) = type_decl;
4994   TYPE_FIELDS (record) = f_gpr;
4995   TREE_CHAIN (f_gpr) = f_fpr;
4996   TREE_CHAIN (f_fpr) = f_res;
4997   TREE_CHAIN (f_res) = f_ovf;
4998   TREE_CHAIN (f_ovf) = f_sav;
4999
5000   layout_type (record);
5001
5002   /* The correct type is an array type of one element.  */
5003   return build_array_type (record, build_index_type (size_zero_node));
5004 }
5005
5006 /* Implement va_start.  */
5007
5008 void
5009 rs6000_va_start (tree valist, rtx nextarg)
5010 {
5011   HOST_WIDE_INT words, n_gpr, n_fpr;
5012   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
5013   tree gpr, fpr, ovf, sav, t;
5014
5015   /* Only SVR4 needs something special.  */
5016   if (DEFAULT_ABI != ABI_V4)
5017     {
5018       std_expand_builtin_va_start (valist, nextarg);
5019       return;
5020     }
5021
5022   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
5023   f_fpr = TREE_CHAIN (f_gpr);
5024   f_res = TREE_CHAIN (f_fpr);
5025   f_ovf = TREE_CHAIN (f_res);
5026   f_sav = TREE_CHAIN (f_ovf);
5027
5028   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
5029   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
5030   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
5031   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
5032   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
5033
5034   /* Count number of gp and fp argument registers used.  */
5035   words = current_function_args_info.words;
5036   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
5037   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
5038
5039   if (TARGET_DEBUG_ARG)
5040     fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
5041              HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
5042              words, n_gpr, n_fpr);
5043
5044   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
5045   TREE_SIDE_EFFECTS (t) = 1;
5046   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5047
5048   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
5049   TREE_SIDE_EFFECTS (t) = 1;
5050   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5051
5052   /* Find the overflow area.  */
5053   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
5054   if (words != 0)
5055     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
5056                build_int_2 (words * UNITS_PER_WORD, 0));
5057   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
5058   TREE_SIDE_EFFECTS (t) = 1;
5059   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5060
5061   /* Find the register save area.  */
5062   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
5063   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
5064              build_int_2 (-RS6000_VARARGS_SIZE, -1));
5065   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
5066   TREE_SIDE_EFFECTS (t) = 1;
5067   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5068 }
5069
5070 /* Implement va_arg.  */
5071
5072 tree
5073 rs6000_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
5074 {
5075   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
5076   tree gpr, fpr, ovf, sav, reg, t, u;
5077   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
5078   tree lab_false, lab_over, addr;
5079   int align;
5080   tree ptrtype = build_pointer_type (type);
5081
5082   if (DEFAULT_ABI != ABI_V4)
5083     {
5084       /* Variable sized types are passed by reference, as are AltiVec
5085          vectors when 32-bit and not using the AltiVec ABI extension.  */
5086       if (int_size_in_bytes (type) < 0
5087           || (TARGET_32BIT
5088               && !TARGET_ALTIVEC_ABI
5089               && ALTIVEC_VECTOR_MODE (TYPE_MODE (type))))
5090         {
5091           /* Args grow upward.  */
5092           t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist,
5093                       build_int_2 (POINTER_SIZE / BITS_PER_UNIT, 0));
5094           t = build1 (NOP_EXPR, build_pointer_type (ptrtype), t);
5095           t = build_fold_indirect_ref (t);
5096           return build_fold_indirect_ref (t);
5097         }
5098       if (targetm.calls.split_complex_arg
5099           && TREE_CODE (type) == COMPLEX_TYPE)
5100         {
5101           tree elem_type = TREE_TYPE (type);
5102           enum machine_mode elem_mode = TYPE_MODE (elem_type);
5103           int elem_size = GET_MODE_SIZE (elem_mode);
5104
5105           if (elem_size < UNITS_PER_WORD)
5106             {
5107               tree real_part, imag_part;
5108               tree post = NULL_TREE;
5109
5110               real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
5111                                                   &post);
5112               /* Copy the value into a temporary, lest the formal temporary
5113                  be reused out from under us.  */
5114               real_part = get_initialized_tmp_var (real_part, pre_p, &post);
5115               append_to_statement_list (post, pre_p);
5116
5117               imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
5118                                                   post_p);
5119
5120               return build (COMPLEX_EXPR, type, real_part, imag_part);
5121             }
5122         }
5123
5124       return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5125     }
5126
5127   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
5128   f_fpr = TREE_CHAIN (f_gpr);
5129   f_res = TREE_CHAIN (f_fpr);
5130   f_ovf = TREE_CHAIN (f_res);
5131   f_sav = TREE_CHAIN (f_ovf);
5132
5133   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
5134   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
5135   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
5136   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
5137   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
5138
5139   size = int_size_in_bytes (type);
5140   rsize = (size + 3) / 4;
5141   align = 1;
5142
5143   if (AGGREGATE_TYPE_P (type)
5144       || TYPE_MODE (type) == TFmode
5145       || (!TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type))))
5146     {
5147       /* Aggregates, long doubles, and AltiVec vectors are passed by
5148          reference.  */
5149       indirect_p = 1;
5150       reg = gpr;
5151       n_reg = 1;
5152       sav_ofs = 0;
5153       sav_scale = 4;
5154       size = 4;
5155       rsize = 1;
5156     }
5157   else if (TARGET_HARD_FLOAT && TARGET_FPRS
5158            && (TYPE_MODE (type) == SFmode || TYPE_MODE (type) == DFmode))
5159     {
5160       /* FP args go in FP registers, if present.  */
5161       indirect_p = 0;
5162       reg = fpr;
5163       n_reg = 1;
5164       sav_ofs = 8*4;
5165       sav_scale = 8;
5166       if (TYPE_MODE (type) == DFmode)
5167         align = 8;
5168     }
5169   else
5170     {
5171       /* Otherwise into GP registers.  */
5172       indirect_p = 0;
5173       reg = gpr;
5174       n_reg = rsize;
5175       sav_ofs = 0;
5176       sav_scale = 4;
5177       if (n_reg == 2)
5178         align = 8;
5179     }
5180
5181   /* Pull the value out of the saved registers....  */
5182
5183   lab_over = NULL;
5184   addr = create_tmp_var (ptr_type_node, "addr");
5185   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
5186
5187   /*  AltiVec vectors never go in registers when -mabi=altivec.  */
5188   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
5189     align = 16;
5190   else
5191     {
5192       lab_false = create_artificial_label ();
5193       lab_over = create_artificial_label ();
5194
5195       /* Long long and SPE vectors are aligned in the registers.
5196          As are any other 2 gpr item such as complex int due to a
5197          historical mistake.  */
5198       u = reg;
5199       if (n_reg == 2)
5200         {
5201           u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), reg,
5202                      build_int_2 (n_reg - 1, 0));
5203           u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg, u);
5204         }
5205
5206       t = build_int_2 (8 - n_reg + 1, 0);
5207       TREE_TYPE (t) = TREE_TYPE (reg);
5208       t = build2 (GE_EXPR, boolean_type_node, u, t);
5209       u = build1 (GOTO_EXPR, void_type_node, lab_false);
5210       t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
5211       gimplify_and_add (t, pre_p);
5212
5213       t = sav;
5214       if (sav_ofs)
5215         t = build2 (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
5216
5217       u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
5218                  build_int_2 (n_reg, 0));
5219       u = build1 (CONVERT_EXPR, integer_type_node, u);
5220       u = build2 (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
5221       t = build2 (PLUS_EXPR, ptr_type_node, t, u);
5222
5223       t = build2 (MODIFY_EXPR, void_type_node, addr, t);
5224       gimplify_and_add (t, pre_p);
5225
5226       t = build1 (GOTO_EXPR, void_type_node, lab_over);
5227       gimplify_and_add (t, pre_p);
5228
5229       t = build1 (LABEL_EXPR, void_type_node, lab_false);
5230       append_to_statement_list (t, pre_p);
5231
5232       if (n_reg > 2)
5233         {
5234           /* Ensure that we don't find any more args in regs.
5235              Alignment has taken care of the n_reg == 2 case.  */
5236           t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
5237           gimplify_and_add (t, pre_p);
5238         }
5239     }
5240
5241   /* ... otherwise out of the overflow area.  */
5242
5243   /* Care for on-stack alignment if needed.  */
5244   t = ovf;
5245   if (align != 1)
5246     {
5247       t = build2 (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (align - 1, 0));
5248       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
5249     }
5250   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
5251
5252   u = build2 (MODIFY_EXPR, void_type_node, addr, t);
5253   gimplify_and_add (u, pre_p);
5254
5255   t = build2 (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
5256   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
5257   gimplify_and_add (t, pre_p);
5258
5259   if (lab_over)
5260     {
5261       t = build1 (LABEL_EXPR, void_type_node, lab_over);
5262       append_to_statement_list (t, pre_p);
5263     }
5264
5265   if (indirect_p)
5266     {
5267       addr = fold_convert (build_pointer_type (ptrtype), addr);
5268       addr = build_fold_indirect_ref (addr);
5269     }
5270   else
5271     addr = fold_convert (ptrtype, addr);
5272
5273   return build_fold_indirect_ref (addr);
5274 }
5275
5276 /* Builtins.  */
5277
5278 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
5279 do {                                                            \
5280   if ((MASK) & target_flags)                                    \
5281     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
5282                       NULL, NULL_TREE);                         \
5283 } while (0)
5284
5285 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
5286
5287 static const struct builtin_description bdesc_3arg[] =
5288 {
5289   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
5290   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
5291   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
5292   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
5293   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
5294   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
5295   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
5296   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
5297   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
5298   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
5299   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
5300   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
5301   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
5302   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
5303   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
5304   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
5305   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
5306   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
5307   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
5308   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
5309   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
5310   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
5311   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
5312 };
5313
5314 /* DST operations: void foo (void *, const int, const char).  */
5315
5316 static const struct builtin_description bdesc_dst[] =
5317 {
5318   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
5319   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
5320   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
5321   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
5322 };
5323
5324 /* Simple binary operations: VECc = foo (VECa, VECb).  */
5325
5326 static struct builtin_description bdesc_2arg[] =
5327 {
5328   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
5329   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
5330   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
5331   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
5332   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
5333   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
5334   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
5335   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
5336   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
5337   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
5338   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
5339   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
5340   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
5341   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
5342   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
5343   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
5344   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
5345   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
5346   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
5347   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
5348   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
5349   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
5350   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
5351   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
5352   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
5353   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
5354   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
5355   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
5356   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
5357   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
5358   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
5359   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
5360   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
5361   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
5362   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
5363   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
5364   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
5365   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
5366   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
5367   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
5368   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
5369   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
5370   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
5371   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
5372   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
5373   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
5374   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
5375   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
5376   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
5377   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
5378   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
5379   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
5380   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
5381   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
5382   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
5383   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
5384   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
5385   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
5386   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
5387   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
5388   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
5389   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
5390   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
5391   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
5392   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
5393   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
5394   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
5395   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
5396   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
5397   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
5398   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
5399   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
5400   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
5401   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
5402   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
5403   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
5404   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
5405   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
5406   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
5407   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
5408   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
5409   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
5410   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
5411   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
5412   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
5413   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
5414   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
5415   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
5416   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
5417   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
5418   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
5419   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
5420   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
5421   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
5422   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
5423   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
5424   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
5425   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
5426   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
5427   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
5428   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
5429   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
5430   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
5431   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
5432   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
5433   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
5434   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
5435   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
5436   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
5437   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
5438   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
5439   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
5440   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
5441
5442   /* Place holder, leave as first spe builtin.  */
5443   { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
5444   { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
5445   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
5446   { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
5447   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
5448   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
5449   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
5450   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
5451   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
5452   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
5453   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
5454   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
5455   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
5456   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
5457   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
5458   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
5459   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
5460   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
5461   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
5462   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
5463   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
5464   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
5465   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
5466   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
5467   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
5468   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
5469   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
5470   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
5471   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
5472   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
5473   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
5474   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
5475   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
5476   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
5477   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
5478   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
5479   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
5480   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
5481   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
5482   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
5483   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
5484   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
5485   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
5486   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
5487   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
5488   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
5489   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
5490   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
5491   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
5492   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
5493   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
5494   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
5495   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
5496   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
5497   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
5498   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
5499   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
5500   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
5501   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
5502   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
5503   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
5504   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
5505   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
5506   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
5507   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
5508   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
5509   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
5510   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
5511   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
5512   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
5513   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
5514   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
5515   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
5516   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
5517   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
5518   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
5519   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
5520   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
5521   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
5522   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
5523   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
5524   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
5525   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
5526   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
5527   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
5528   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
5529   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
5530   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
5531   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
5532   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
5533   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
5534   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
5535   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
5536   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
5537   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
5538   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
5539   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
5540   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
5541   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
5542   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
5543   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
5544   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
5545   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
5546   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
5547   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
5548   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
5549   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
5550   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
5551   { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
5552
5553   /* SPE binary operations expecting a 5-bit unsigned literal.  */
5554   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
5555
5556   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
5557   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
5558   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
5559   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
5560   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
5561   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
5562   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
5563   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
5564   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
5565   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
5566   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
5567   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
5568   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
5569   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
5570   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
5571   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
5572   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
5573   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
5574   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
5575   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
5576   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
5577   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
5578   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
5579   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
5580   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
5581   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
5582
5583   /* Place-holder.  Leave as last binary SPE builtin.  */
5584   { 0, CODE_FOR_xorv2si3, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
5585 };
5586
5587 /* AltiVec predicates.  */
5588
5589 struct builtin_description_predicates
5590 {
5591   const unsigned int mask;
5592   const enum insn_code icode;
5593   const char *opcode;
5594   const char *const name;
5595   const enum rs6000_builtins code;
5596 };
5597
5598 static const struct builtin_description_predicates bdesc_altivec_preds[] =
5599 {
5600   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
5601   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
5602   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
5603   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
5604   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
5605   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
5606   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
5607   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
5608   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
5609   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
5610   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
5611   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
5612   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
5613 };
5614
5615 /* SPE predicates.  */
5616 static struct builtin_description bdesc_spe_predicates[] =
5617 {
5618   /* Place-holder.  Leave as first.  */
5619   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
5620   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
5621   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
5622   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
5623   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
5624   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
5625   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
5626   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
5627   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
5628   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
5629   /* Place-holder.  Leave as last.  */
5630   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
5631 };
5632
5633 /* SPE evsel predicates.  */
5634 static struct builtin_description bdesc_spe_evsel[] =
5635 {
5636   /* Place-holder.  Leave as first.  */
5637   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
5638   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
5639   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
5640   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
5641   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
5642   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
5643   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
5644   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
5645   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
5646   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
5647   /* Place-holder.  Leave as last.  */
5648   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
5649 };
5650
5651 /* ABS* operations.  */
5652
5653 static const struct builtin_description bdesc_abs[] =
5654 {
5655   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
5656   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
5657   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
5658   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
5659   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
5660   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
5661   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
5662 };
5663
5664 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
5665    foo (VECa).  */
5666
5667 static struct builtin_description bdesc_1arg[] =
5668 {
5669   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
5670   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
5671   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
5672   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
5673   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
5674   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
5675   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
5676   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
5677   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
5678   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
5679   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
5680   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
5681   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
5682   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
5683   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
5684   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
5685   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
5686
5687   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
5688      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
5689   { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
5690   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
5691   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
5692   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
5693   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
5694   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
5695   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
5696   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
5697   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
5698   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
5699   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
5700   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
5701   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
5702   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
5703   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
5704   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
5705   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
5706   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
5707   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
5708   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
5709   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
5710   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
5711   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
5712   { 0, CODE_FOR_negv2si2, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
5713   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
5714   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
5715   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
5716   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
5717
5718   /* Place-holder.  Leave as last unary SPE builtin.  */
5719   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
5720 };
5721
5722 static rtx
5723 rs6000_expand_unop_builtin (enum insn_code icode, tree arglist, rtx target)
5724 {
5725   rtx pat;
5726   tree arg0 = TREE_VALUE (arglist);
5727   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5728   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5729   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5730
5731   if (icode == CODE_FOR_nothing)
5732     /* Builtin not supported on this processor.  */
5733     return 0;
5734
5735   /* If we got invalid arguments bail out before generating bad rtl.  */
5736   if (arg0 == error_mark_node)
5737     return const0_rtx;
5738
5739   if (icode == CODE_FOR_altivec_vspltisb
5740       || icode == CODE_FOR_altivec_vspltish
5741       || icode == CODE_FOR_altivec_vspltisw
5742       || icode == CODE_FOR_spe_evsplatfi
5743       || icode == CODE_FOR_spe_evsplati)
5744     {
5745       /* Only allow 5-bit *signed* literals.  */
5746       if (GET_CODE (op0) != CONST_INT
5747           || INTVAL (op0) > 0x1f
5748           || INTVAL (op0) < -0x1f)
5749         {
5750           error ("argument 1 must be a 5-bit signed literal");
5751           return const0_rtx;
5752         }
5753     }
5754
5755   if (target == 0
5756       || GET_MODE (target) != tmode
5757       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5758     target = gen_reg_rtx (tmode);
5759
5760   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5761     op0 = copy_to_mode_reg (mode0, op0);
5762
5763   pat = GEN_FCN (icode) (target, op0);
5764   if (! pat)
5765     return 0;
5766   emit_insn (pat);
5767
5768   return target;
5769 }
5770
5771 static rtx
5772 altivec_expand_abs_builtin (enum insn_code icode, tree arglist, rtx target)
5773 {
5774   rtx pat, scratch1, scratch2;
5775   tree arg0 = TREE_VALUE (arglist);
5776   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5777   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5778   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5779
5780   /* If we have invalid arguments, bail out before generating bad rtl.  */
5781   if (arg0 == error_mark_node)
5782     return const0_rtx;
5783
5784   if (target == 0
5785       || GET_MODE (target) != tmode
5786       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5787     target = gen_reg_rtx (tmode);
5788
5789   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5790     op0 = copy_to_mode_reg (mode0, op0);
5791
5792   scratch1 = gen_reg_rtx (mode0);
5793   scratch2 = gen_reg_rtx (mode0);
5794
5795   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
5796   if (! pat)
5797     return 0;
5798   emit_insn (pat);
5799
5800   return target;
5801 }
5802
5803 static rtx
5804 rs6000_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
5805 {
5806   rtx pat;
5807   tree arg0 = TREE_VALUE (arglist);
5808   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5809   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5810   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5811   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5812   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5813   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5814
5815   if (icode == CODE_FOR_nothing)
5816     /* Builtin not supported on this processor.  */
5817     return 0;
5818
5819   /* If we got invalid arguments bail out before generating bad rtl.  */
5820   if (arg0 == error_mark_node || arg1 == error_mark_node)
5821     return const0_rtx;
5822
5823   if (icode == CODE_FOR_altivec_vcfux
5824       || icode == CODE_FOR_altivec_vcfsx
5825       || icode == CODE_FOR_altivec_vctsxs
5826       || icode == CODE_FOR_altivec_vctuxs
5827       || icode == CODE_FOR_altivec_vspltb
5828       || icode == CODE_FOR_altivec_vsplth
5829       || icode == CODE_FOR_altivec_vspltw
5830       || icode == CODE_FOR_spe_evaddiw
5831       || icode == CODE_FOR_spe_evldd
5832       || icode == CODE_FOR_spe_evldh
5833       || icode == CODE_FOR_spe_evldw
5834       || icode == CODE_FOR_spe_evlhhesplat
5835       || icode == CODE_FOR_spe_evlhhossplat
5836       || icode == CODE_FOR_spe_evlhhousplat
5837       || icode == CODE_FOR_spe_evlwhe
5838       || icode == CODE_FOR_spe_evlwhos
5839       || icode == CODE_FOR_spe_evlwhou
5840       || icode == CODE_FOR_spe_evlwhsplat
5841       || icode == CODE_FOR_spe_evlwwsplat
5842       || icode == CODE_FOR_spe_evrlwi
5843       || icode == CODE_FOR_spe_evslwi
5844       || icode == CODE_FOR_spe_evsrwis
5845       || icode == CODE_FOR_spe_evsubifw
5846       || icode == CODE_FOR_spe_evsrwiu)
5847     {
5848       /* Only allow 5-bit unsigned literals.  */
5849       STRIP_NOPS (arg1);
5850       if (TREE_CODE (arg1) != INTEGER_CST
5851           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5852         {
5853           error ("argument 2 must be a 5-bit unsigned literal");
5854           return const0_rtx;
5855         }
5856     }
5857
5858   if (target == 0
5859       || GET_MODE (target) != tmode
5860       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5861     target = gen_reg_rtx (tmode);
5862
5863   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5864     op0 = copy_to_mode_reg (mode0, op0);
5865   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5866     op1 = copy_to_mode_reg (mode1, op1);
5867
5868   pat = GEN_FCN (icode) (target, op0, op1);
5869   if (! pat)
5870     return 0;
5871   emit_insn (pat);
5872
5873   return target;
5874 }
5875
5876 static rtx
5877 altivec_expand_predicate_builtin (enum insn_code icode, const char *opcode, 
5878                                   tree arglist, rtx target)
5879 {
5880   rtx pat, scratch;
5881   tree cr6_form = TREE_VALUE (arglist);
5882   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5883   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5884   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5885   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5886   enum machine_mode tmode = SImode;
5887   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5888   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5889   int cr6_form_int;
5890
5891   if (TREE_CODE (cr6_form) != INTEGER_CST)
5892     {
5893       error ("argument 1 of __builtin_altivec_predicate must be a constant");
5894       return const0_rtx;
5895     }
5896   else
5897     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
5898
5899   if (mode0 != mode1)
5900     abort ();
5901
5902   /* If we have invalid arguments, bail out before generating bad rtl.  */
5903   if (arg0 == error_mark_node || arg1 == error_mark_node)
5904     return const0_rtx;
5905
5906   if (target == 0
5907       || GET_MODE (target) != tmode
5908       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5909     target = gen_reg_rtx (tmode);
5910
5911   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5912     op0 = copy_to_mode_reg (mode0, op0);
5913   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5914     op1 = copy_to_mode_reg (mode1, op1);
5915
5916   scratch = gen_reg_rtx (mode0);
5917
5918   pat = GEN_FCN (icode) (scratch, op0, op1,
5919                          gen_rtx_SYMBOL_REF (Pmode, opcode));
5920   if (! pat)
5921     return 0;
5922   emit_insn (pat);
5923
5924   /* The vec_any* and vec_all* predicates use the same opcodes for two
5925      different operations, but the bits in CR6 will be different
5926      depending on what information we want.  So we have to play tricks
5927      with CR6 to get the right bits out.
5928
5929      If you think this is disgusting, look at the specs for the
5930      AltiVec predicates.  */
5931
5932      switch (cr6_form_int)
5933        {
5934        case 0:
5935          emit_insn (gen_cr6_test_for_zero (target));
5936          break;
5937        case 1:
5938          emit_insn (gen_cr6_test_for_zero_reverse (target));
5939          break;
5940        case 2:
5941          emit_insn (gen_cr6_test_for_lt (target));
5942          break;
5943        case 3:
5944          emit_insn (gen_cr6_test_for_lt_reverse (target));
5945          break;
5946        default:
5947          error ("argument 1 of __builtin_altivec_predicate is out of range");
5948          break;
5949        }
5950
5951   return target;
5952 }
5953
5954 static rtx
5955 altivec_expand_lv_builtin (enum insn_code icode, tree arglist, rtx target)
5956 {
5957   rtx pat, addr;
5958   tree arg0 = TREE_VALUE (arglist);
5959   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5960   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5961   enum machine_mode mode0 = Pmode;
5962   enum machine_mode mode1 = Pmode;
5963   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5964   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5965
5966   if (icode == CODE_FOR_nothing)
5967     /* Builtin not supported on this processor.  */
5968     return 0;
5969
5970   /* If we got invalid arguments bail out before generating bad rtl.  */
5971   if (arg0 == error_mark_node || arg1 == error_mark_node)
5972     return const0_rtx;
5973
5974   if (target == 0
5975       || GET_MODE (target) != tmode
5976       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5977     target = gen_reg_rtx (tmode);
5978
5979   op1 = copy_to_mode_reg (mode1, op1); 
5980
5981   if (op0 == const0_rtx)
5982     {
5983       addr = gen_rtx_MEM (tmode, op1);
5984     }
5985   else
5986     {
5987       op0 = copy_to_mode_reg (mode0, op0);
5988       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
5989     }
5990
5991   pat = GEN_FCN (icode) (target, addr);
5992
5993   if (! pat)
5994     return 0;
5995   emit_insn (pat);
5996
5997   return target;
5998 }
5999
6000 static rtx
6001 spe_expand_stv_builtin (enum insn_code icode, tree arglist)
6002 {
6003   tree arg0 = TREE_VALUE (arglist);
6004   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6005   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6006   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6007   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6008   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6009   rtx pat;
6010   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
6011   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
6012   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
6013
6014   /* Invalid arguments.  Bail before doing anything stoopid!  */
6015   if (arg0 == error_mark_node
6016       || arg1 == error_mark_node
6017       || arg2 == error_mark_node)
6018     return const0_rtx;
6019
6020   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
6021     op0 = copy_to_mode_reg (mode2, op0);
6022   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
6023     op1 = copy_to_mode_reg (mode0, op1);
6024   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
6025     op2 = copy_to_mode_reg (mode1, op2);
6026
6027   pat = GEN_FCN (icode) (op1, op2, op0);
6028   if (pat)
6029     emit_insn (pat);
6030   return NULL_RTX;
6031 }
6032
6033 static rtx
6034 altivec_expand_stv_builtin (enum insn_code icode, tree arglist)
6035 {
6036   tree arg0 = TREE_VALUE (arglist);
6037   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6038   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6039   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6040   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6041   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6042   rtx pat, addr;
6043   enum machine_mode tmode = insn_data[icode].operand[0].mode;
6044   enum machine_mode mode1 = Pmode;
6045   enum machine_mode mode2 = Pmode;
6046
6047   /* Invalid arguments.  Bail before doing anything stoopid!  */
6048   if (arg0 == error_mark_node
6049       || arg1 == error_mark_node
6050       || arg2 == error_mark_node)
6051     return const0_rtx;
6052
6053   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
6054     op0 = copy_to_mode_reg (tmode, op0);
6055
6056   op2 = copy_to_mode_reg (mode2, op2); 
6057
6058   if (op1 == const0_rtx)
6059     {
6060       addr = gen_rtx_MEM (tmode, op2);
6061     }
6062   else
6063     {
6064       op1 = copy_to_mode_reg (mode1, op1);
6065       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
6066     }
6067
6068   pat = GEN_FCN (icode) (addr, op0);
6069   if (pat)
6070     emit_insn (pat);
6071   return NULL_RTX;
6072 }
6073
6074 static rtx
6075 rs6000_expand_ternop_builtin (enum insn_code icode, tree arglist, rtx target)
6076 {
6077   rtx pat;
6078   tree arg0 = TREE_VALUE (arglist);
6079   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6080   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6081   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6082   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6083   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6084   enum machine_mode tmode = insn_data[icode].operand[0].mode;
6085   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6086   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6087   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
6088
6089   if (icode == CODE_FOR_nothing)
6090     /* Builtin not supported on this processor.  */
6091     return 0;
6092
6093   /* If we got invalid arguments bail out before generating bad rtl.  */
6094   if (arg0 == error_mark_node
6095       || arg1 == error_mark_node
6096       || arg2 == error_mark_node)
6097     return const0_rtx;
6098
6099   if (icode == CODE_FOR_altivec_vsldoi_4sf
6100       || icode == CODE_FOR_altivec_vsldoi_4si
6101       || icode == CODE_FOR_altivec_vsldoi_8hi
6102       || icode == CODE_FOR_altivec_vsldoi_16qi)
6103     {
6104       /* Only allow 4-bit unsigned literals.  */
6105       STRIP_NOPS (arg2);
6106       if (TREE_CODE (arg2) != INTEGER_CST
6107           || TREE_INT_CST_LOW (arg2) & ~0xf)
6108         {
6109           error ("argument 3 must be a 4-bit unsigned literal");
6110           return const0_rtx;
6111         }
6112     }
6113
6114   if (target == 0
6115       || GET_MODE (target) != tmode
6116       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6117     target = gen_reg_rtx (tmode);
6118
6119   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6120     op0 = copy_to_mode_reg (mode0, op0);
6121   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
6122     op1 = copy_to_mode_reg (mode1, op1);
6123   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
6124     op2 = copy_to_mode_reg (mode2, op2);
6125
6126   pat = GEN_FCN (icode) (target, op0, op1, op2);
6127   if (! pat)
6128     return 0;
6129   emit_insn (pat);
6130
6131   return target;
6132 }
6133
6134 /* Expand the lvx builtins.  */
6135 static rtx
6136 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
6137 {
6138   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6139   tree arglist = TREE_OPERAND (exp, 1);
6140   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6141   tree arg0;
6142   enum machine_mode tmode, mode0;
6143   rtx pat, op0;
6144   enum insn_code icode;
6145
6146   switch (fcode)
6147     {
6148     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
6149       icode = CODE_FOR_altivec_lvx_16qi;
6150       break;
6151     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
6152       icode = CODE_FOR_altivec_lvx_8hi;
6153       break;
6154     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
6155       icode = CODE_FOR_altivec_lvx_4si;
6156       break;
6157     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
6158       icode = CODE_FOR_altivec_lvx_4sf;
6159       break;
6160     default:
6161       *expandedp = false;
6162       return NULL_RTX;
6163     }
6164
6165   *expandedp = true;
6166
6167   arg0 = TREE_VALUE (arglist);
6168   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6169   tmode = insn_data[icode].operand[0].mode;
6170   mode0 = insn_data[icode].operand[1].mode;
6171
6172   if (target == 0
6173       || GET_MODE (target) != tmode
6174       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6175     target = gen_reg_rtx (tmode);
6176
6177   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6178     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
6179
6180   pat = GEN_FCN (icode) (target, op0);
6181   if (! pat)
6182     return 0;
6183   emit_insn (pat);
6184   return target;
6185 }
6186
6187 /* Expand the stvx builtins.  */
6188 static rtx
6189 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, 
6190                            bool *expandedp)
6191 {
6192   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6193   tree arglist = TREE_OPERAND (exp, 1);
6194   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6195   tree arg0, arg1;
6196   enum machine_mode mode0, mode1;
6197   rtx pat, op0, op1;
6198   enum insn_code icode;
6199
6200   switch (fcode)
6201     {
6202     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
6203       icode = CODE_FOR_altivec_stvx_16qi;
6204       break;
6205     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
6206       icode = CODE_FOR_altivec_stvx_8hi;
6207       break;
6208     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
6209       icode = CODE_FOR_altivec_stvx_4si;
6210       break;
6211     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
6212       icode = CODE_FOR_altivec_stvx_4sf;
6213       break;
6214     default:
6215       *expandedp = false;
6216       return NULL_RTX;
6217     }
6218
6219   arg0 = TREE_VALUE (arglist);
6220   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6221   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6222   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6223   mode0 = insn_data[icode].operand[0].mode;
6224   mode1 = insn_data[icode].operand[1].mode;
6225
6226   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6227     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
6228   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
6229     op1 = copy_to_mode_reg (mode1, op1);
6230
6231   pat = GEN_FCN (icode) (op0, op1);
6232   if (pat)
6233     emit_insn (pat);
6234
6235   *expandedp = true;
6236   return NULL_RTX;
6237 }
6238
6239 /* Expand the dst builtins.  */
6240 static rtx
6241 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, 
6242                             bool *expandedp)
6243 {
6244   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6245   tree arglist = TREE_OPERAND (exp, 1);
6246   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6247   tree arg0, arg1, arg2;
6248   enum machine_mode mode0, mode1, mode2;
6249   rtx pat, op0, op1, op2;
6250   struct builtin_description *d;
6251   size_t i;
6252
6253   *expandedp = false;
6254
6255   /* Handle DST variants.  */
6256   d = (struct builtin_description *) bdesc_dst;
6257   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
6258     if (d->code == fcode)
6259       {
6260         arg0 = TREE_VALUE (arglist);
6261         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6262         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6263         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6264         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6265         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6266         mode0 = insn_data[d->icode].operand[0].mode;
6267         mode1 = insn_data[d->icode].operand[1].mode;
6268         mode2 = insn_data[d->icode].operand[2].mode;
6269
6270         /* Invalid arguments, bail out before generating bad rtl.  */
6271         if (arg0 == error_mark_node
6272             || arg1 == error_mark_node
6273             || arg2 == error_mark_node)
6274           return const0_rtx;
6275
6276         *expandedp = true;
6277         STRIP_NOPS (arg2);
6278         if (TREE_CODE (arg2) != INTEGER_CST
6279             || TREE_INT_CST_LOW (arg2) & ~0x3)
6280           {
6281             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
6282             return const0_rtx;
6283           }
6284
6285         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
6286           op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
6287         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
6288           op1 = copy_to_mode_reg (mode1, op1);
6289
6290         pat = GEN_FCN (d->icode) (op0, op1, op2);
6291         if (pat != 0)
6292           emit_insn (pat);
6293
6294         return NULL_RTX;
6295       }
6296
6297   return NULL_RTX;
6298 }
6299
6300 /* Expand the builtin in EXP and store the result in TARGET.  Store
6301    true in *EXPANDEDP if we found a builtin to expand.  */
6302 static rtx
6303 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
6304 {
6305   struct builtin_description *d;
6306   struct builtin_description_predicates *dp;
6307   size_t i;
6308   enum insn_code icode;
6309   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6310   tree arglist = TREE_OPERAND (exp, 1);
6311   tree arg0;
6312   rtx op0, pat;
6313   enum machine_mode tmode, mode0;
6314   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6315
6316   target = altivec_expand_ld_builtin (exp, target, expandedp);
6317   if (*expandedp)
6318     return target;
6319
6320   target = altivec_expand_st_builtin (exp, target, expandedp);
6321   if (*expandedp)
6322     return target;
6323
6324   target = altivec_expand_dst_builtin (exp, target, expandedp);
6325   if (*expandedp)
6326     return target;
6327
6328   *expandedp = true;
6329
6330   switch (fcode)
6331     {
6332     case ALTIVEC_BUILTIN_STVX:
6333       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
6334     case ALTIVEC_BUILTIN_STVEBX:
6335       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
6336     case ALTIVEC_BUILTIN_STVEHX:
6337       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
6338     case ALTIVEC_BUILTIN_STVEWX:
6339       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
6340     case ALTIVEC_BUILTIN_STVXL:
6341       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
6342
6343     case ALTIVEC_BUILTIN_MFVSCR:
6344       icode = CODE_FOR_altivec_mfvscr;
6345       tmode = insn_data[icode].operand[0].mode;
6346
6347       if (target == 0
6348           || GET_MODE (target) != tmode
6349           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6350         target = gen_reg_rtx (tmode);
6351       
6352       pat = GEN_FCN (icode) (target);
6353       if (! pat)
6354         return 0;
6355       emit_insn (pat);
6356       return target;
6357
6358     case ALTIVEC_BUILTIN_MTVSCR:
6359       icode = CODE_FOR_altivec_mtvscr;
6360       arg0 = TREE_VALUE (arglist);
6361       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6362       mode0 = insn_data[icode].operand[0].mode;
6363
6364       /* If we got invalid arguments bail out before generating bad rtl.  */
6365       if (arg0 == error_mark_node)
6366         return const0_rtx;
6367
6368       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6369         op0 = copy_to_mode_reg (mode0, op0);
6370
6371       pat = GEN_FCN (icode) (op0);
6372       if (pat)
6373         emit_insn (pat);
6374       return NULL_RTX;
6375
6376     case ALTIVEC_BUILTIN_DSSALL:
6377       emit_insn (gen_altivec_dssall ());
6378       return NULL_RTX;
6379
6380     case ALTIVEC_BUILTIN_DSS:
6381       icode = CODE_FOR_altivec_dss;
6382       arg0 = TREE_VALUE (arglist);
6383       STRIP_NOPS (arg0);
6384       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6385       mode0 = insn_data[icode].operand[0].mode;
6386
6387       /* If we got invalid arguments bail out before generating bad rtl.  */
6388       if (arg0 == error_mark_node)
6389         return const0_rtx;
6390
6391       if (TREE_CODE (arg0) != INTEGER_CST
6392           || TREE_INT_CST_LOW (arg0) & ~0x3)
6393         {
6394           error ("argument to dss must be a 2-bit unsigned literal");
6395           return const0_rtx;
6396         }
6397
6398       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6399         op0 = copy_to_mode_reg (mode0, op0);
6400
6401       emit_insn (gen_altivec_dss (op0));
6402       return NULL_RTX;
6403       
6404     case ALTIVEC_BUILTIN_COMPILETIME_ERROR:
6405       arg0 = TREE_VALUE (arglist);
6406       while (TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == ADDR_EXPR)
6407         arg0 = TREE_OPERAND (arg0, 0);
6408       error ("invalid parameter combination for `%s' AltiVec intrinsic",
6409              TREE_STRING_POINTER (arg0));
6410
6411       return const0_rtx;
6412     }
6413
6414   /* Expand abs* operations.  */
6415   d = (struct builtin_description *) bdesc_abs;
6416   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
6417     if (d->code == fcode)
6418       return altivec_expand_abs_builtin (d->icode, arglist, target);
6419
6420   /* Expand the AltiVec predicates.  */
6421   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
6422   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
6423     if (dp->code == fcode)
6424       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
6425
6426   /* LV* are funky.  We initialized them differently.  */
6427   switch (fcode)
6428     {
6429     case ALTIVEC_BUILTIN_LVSL:
6430       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
6431                                            arglist, target);
6432     case ALTIVEC_BUILTIN_LVSR:
6433       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
6434                                           arglist, target);
6435     case ALTIVEC_BUILTIN_LVEBX:
6436       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
6437                                           arglist, target);
6438     case ALTIVEC_BUILTIN_LVEHX:
6439       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
6440                                           arglist, target);
6441     case ALTIVEC_BUILTIN_LVEWX:
6442       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
6443                                           arglist, target);
6444     case ALTIVEC_BUILTIN_LVXL:
6445       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
6446                                           arglist, target);
6447     case ALTIVEC_BUILTIN_LVX:
6448       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx,
6449                                           arglist, target);
6450     default:
6451       break;
6452       /* Fall through.  */
6453     }
6454
6455   *expandedp = false;
6456   return NULL_RTX;
6457 }
6458
6459 /* Binops that need to be initialized manually, but can be expanded
6460    automagically by rs6000_expand_binop_builtin.  */
6461 static struct builtin_description bdesc_2arg_spe[] =
6462 {
6463   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
6464   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
6465   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
6466   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
6467   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
6468   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
6469   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
6470   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
6471   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
6472   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
6473   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
6474   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
6475   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
6476   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
6477   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
6478   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
6479   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
6480   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
6481   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
6482   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
6483   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
6484   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
6485 };
6486
6487 /* Expand the builtin in EXP and store the result in TARGET.  Store
6488    true in *EXPANDEDP if we found a builtin to expand.
6489
6490    This expands the SPE builtins that are not simple unary and binary
6491    operations.  */
6492 static rtx
6493 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
6494 {
6495   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6496   tree arglist = TREE_OPERAND (exp, 1);
6497   tree arg1, arg0;
6498   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6499   enum insn_code icode;
6500   enum machine_mode tmode, mode0;
6501   rtx pat, op0;
6502   struct builtin_description *d;
6503   size_t i;
6504
6505   *expandedp = true;
6506
6507   /* Syntax check for a 5-bit unsigned immediate.  */
6508   switch (fcode)
6509     {
6510     case SPE_BUILTIN_EVSTDD:
6511     case SPE_BUILTIN_EVSTDH:
6512     case SPE_BUILTIN_EVSTDW:
6513     case SPE_BUILTIN_EVSTWHE:
6514     case SPE_BUILTIN_EVSTWHO:
6515     case SPE_BUILTIN_EVSTWWE:
6516     case SPE_BUILTIN_EVSTWWO:
6517       arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6518       if (TREE_CODE (arg1) != INTEGER_CST
6519           || TREE_INT_CST_LOW (arg1) & ~0x1f)
6520         {
6521           error ("argument 2 must be a 5-bit unsigned literal");
6522           return const0_rtx;
6523         }
6524       break;
6525     default:
6526       break;
6527     }
6528
6529   /* The evsplat*i instructions are not quite generic.  */
6530   switch (fcode)
6531     {
6532     case SPE_BUILTIN_EVSPLATFI:
6533       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
6534                                          arglist, target);
6535     case SPE_BUILTIN_EVSPLATI:
6536       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
6537                                          arglist, target);
6538     default:
6539       break;
6540     }
6541
6542   d = (struct builtin_description *) bdesc_2arg_spe;
6543   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
6544     if (d->code == fcode)
6545       return rs6000_expand_binop_builtin (d->icode, arglist, target);
6546
6547   d = (struct builtin_description *) bdesc_spe_predicates;
6548   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
6549     if (d->code == fcode)
6550       return spe_expand_predicate_builtin (d->icode, arglist, target);
6551
6552   d = (struct builtin_description *) bdesc_spe_evsel;
6553   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
6554     if (d->code == fcode)
6555       return spe_expand_evsel_builtin (d->icode, arglist, target);
6556
6557   switch (fcode)
6558     {
6559     case SPE_BUILTIN_EVSTDDX:
6560       return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
6561     case SPE_BUILTIN_EVSTDHX:
6562       return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
6563     case SPE_BUILTIN_EVSTDWX:
6564       return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
6565     case SPE_BUILTIN_EVSTWHEX:
6566       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
6567     case SPE_BUILTIN_EVSTWHOX:
6568       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
6569     case SPE_BUILTIN_EVSTWWEX:
6570       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
6571     case SPE_BUILTIN_EVSTWWOX:
6572       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
6573     case SPE_BUILTIN_EVSTDD:
6574       return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
6575     case SPE_BUILTIN_EVSTDH:
6576       return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
6577     case SPE_BUILTIN_EVSTDW:
6578       return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
6579     case SPE_BUILTIN_EVSTWHE:
6580       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
6581     case SPE_BUILTIN_EVSTWHO:
6582       return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
6583     case SPE_BUILTIN_EVSTWWE:
6584       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
6585     case SPE_BUILTIN_EVSTWWO:
6586       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
6587     case SPE_BUILTIN_MFSPEFSCR:
6588       icode = CODE_FOR_spe_mfspefscr;
6589       tmode = insn_data[icode].operand[0].mode;
6590
6591       if (target == 0
6592           || GET_MODE (target) != tmode
6593           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6594         target = gen_reg_rtx (tmode);
6595       
6596       pat = GEN_FCN (icode) (target);
6597       if (! pat)
6598         return 0;
6599       emit_insn (pat);
6600       return target;
6601     case SPE_BUILTIN_MTSPEFSCR:
6602       icode = CODE_FOR_spe_mtspefscr;
6603       arg0 = TREE_VALUE (arglist);
6604       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6605       mode0 = insn_data[icode].operand[0].mode;
6606
6607       if (arg0 == error_mark_node)
6608         return const0_rtx;
6609
6610       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6611         op0 = copy_to_mode_reg (mode0, op0);
6612
6613       pat = GEN_FCN (icode) (op0);
6614       if (pat)
6615         emit_insn (pat);
6616       return NULL_RTX;
6617     default:
6618       break;
6619     }
6620
6621   *expandedp = false;
6622   return NULL_RTX;
6623 }
6624
6625 static rtx
6626 spe_expand_predicate_builtin (enum insn_code icode, tree arglist, rtx target)
6627 {
6628   rtx pat, scratch, tmp;
6629   tree form = TREE_VALUE (arglist);
6630   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
6631   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6632   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6633   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6634   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6635   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6636   int form_int;
6637   enum rtx_code code;
6638
6639   if (TREE_CODE (form) != INTEGER_CST)
6640     {
6641       error ("argument 1 of __builtin_spe_predicate must be a constant");
6642       return const0_rtx;
6643     }
6644   else
6645     form_int = TREE_INT_CST_LOW (form);
6646
6647   if (mode0 != mode1)
6648     abort ();
6649
6650   if (arg0 == error_mark_node || arg1 == error_mark_node)
6651     return const0_rtx;
6652
6653   if (target == 0
6654       || GET_MODE (target) != SImode
6655       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
6656     target = gen_reg_rtx (SImode);
6657
6658   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6659     op0 = copy_to_mode_reg (mode0, op0);
6660   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
6661     op1 = copy_to_mode_reg (mode1, op1);
6662
6663   scratch = gen_reg_rtx (CCmode);
6664
6665   pat = GEN_FCN (icode) (scratch, op0, op1);
6666   if (! pat)
6667     return const0_rtx;
6668   emit_insn (pat);
6669
6670   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
6671      _lower_.  We use one compare, but look in different bits of the
6672      CR for each variant.
6673
6674      There are 2 elements in each SPE simd type (upper/lower).  The CR
6675      bits are set as follows:
6676
6677      BIT0  | BIT 1  | BIT 2   | BIT 3
6678      U     |   L    | (U | L) | (U & L)
6679
6680      So, for an "all" relationship, BIT 3 would be set.
6681      For an "any" relationship, BIT 2 would be set.  Etc.
6682
6683      Following traditional nomenclature, these bits map to:
6684
6685      BIT0  | BIT 1  | BIT 2   | BIT 3
6686      LT    | GT     | EQ      | OV
6687
6688      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
6689   */
6690
6691   switch (form_int)
6692     {
6693       /* All variant.  OV bit.  */
6694     case 0:
6695       /* We need to get to the OV bit, which is the ORDERED bit.  We
6696          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
6697          that's ugly and will trigger a validate_condition_mode abort.
6698          So let's just use another pattern.  */
6699       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
6700       return target;
6701       /* Any variant.  EQ bit.  */
6702     case 1:
6703       code = EQ;
6704       break;
6705       /* Upper variant.  LT bit.  */
6706     case 2:
6707       code = LT;
6708       break;
6709       /* Lower variant.  GT bit.  */
6710     case 3:
6711       code = GT;
6712       break;
6713     default:
6714       error ("argument 1 of __builtin_spe_predicate is out of range");
6715       return const0_rtx;
6716     }
6717
6718   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
6719   emit_move_insn (target, tmp);
6720
6721   return target;
6722 }
6723
6724 /* The evsel builtins look like this:
6725
6726      e = __builtin_spe_evsel_OP (a, b, c, d);
6727
6728    and work like this:
6729
6730      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
6731      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
6732 */
6733
6734 static rtx
6735 spe_expand_evsel_builtin (enum insn_code icode, tree arglist, rtx target)
6736 {
6737   rtx pat, scratch;
6738   tree arg0 = TREE_VALUE (arglist);
6739   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6740   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6741   tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
6742   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6743   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6744   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6745   rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
6746   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6747   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6748
6749   if (mode0 != mode1)
6750     abort ();
6751
6752   if (arg0 == error_mark_node || arg1 == error_mark_node
6753       || arg2 == error_mark_node || arg3 == error_mark_node)
6754     return const0_rtx;
6755
6756   if (target == 0
6757       || GET_MODE (target) != mode0
6758       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
6759     target = gen_reg_rtx (mode0);
6760
6761   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6762     op0 = copy_to_mode_reg (mode0, op0);
6763   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
6764     op1 = copy_to_mode_reg (mode0, op1);
6765   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
6766     op2 = copy_to_mode_reg (mode0, op2);
6767   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
6768     op3 = copy_to_mode_reg (mode0, op3);
6769
6770   /* Generate the compare.  */
6771   scratch = gen_reg_rtx (CCmode);
6772   pat = GEN_FCN (icode) (scratch, op0, op1);
6773   if (! pat)
6774     return const0_rtx;
6775   emit_insn (pat);
6776
6777   if (mode0 == V2SImode)
6778     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
6779   else
6780     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
6781
6782   return target;
6783 }
6784
6785 /* Expand an expression EXP that calls a built-in function,
6786    with result going to TARGET if that's convenient
6787    (and in mode MODE if that's convenient).
6788    SUBTARGET may be used as the target for computing one of EXP's operands.
6789    IGNORE is nonzero if the value is to be ignored.  */
6790
6791 static rtx
6792 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
6793                       enum machine_mode mode ATTRIBUTE_UNUSED, 
6794                       int ignore ATTRIBUTE_UNUSED)
6795 {
6796   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6797   tree arglist = TREE_OPERAND (exp, 1);
6798   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6799   struct builtin_description *d;
6800   size_t i;
6801   rtx ret;
6802   bool success;
6803   
6804   if (TARGET_ALTIVEC)
6805     {
6806       ret = altivec_expand_builtin (exp, target, &success);
6807
6808       if (success)
6809         return ret;
6810     }
6811   if (TARGET_SPE)
6812     {
6813       ret = spe_expand_builtin (exp, target, &success);
6814
6815       if (success)
6816         return ret;
6817     }
6818
6819   if (TARGET_ALTIVEC || TARGET_SPE)
6820     {
6821       /* Handle simple unary operations.  */
6822       d = (struct builtin_description *) bdesc_1arg;
6823       for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6824         if (d->code == fcode)
6825           return rs6000_expand_unop_builtin (d->icode, arglist, target);
6826
6827       /* Handle simple binary operations.  */
6828       d = (struct builtin_description *) bdesc_2arg;
6829       for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6830         if (d->code == fcode)
6831           return rs6000_expand_binop_builtin (d->icode, arglist, target);
6832
6833       /* Handle simple ternary operations.  */
6834       d = (struct builtin_description *) bdesc_3arg;
6835       for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
6836         if (d->code == fcode)
6837           return rs6000_expand_ternop_builtin (d->icode, arglist, target);
6838     }
6839
6840   abort ();
6841   return NULL_RTX;
6842 }
6843
6844 static tree
6845 build_opaque_vector_type (tree node, int nunits)
6846 {
6847   node = copy_node (node);
6848   TYPE_MAIN_VARIANT (node) = node;
6849   return build_vector_type (node, nunits);
6850 }
6851
6852 static void
6853 rs6000_init_builtins (void)
6854 {
6855   V2SI_type_node = build_vector_type (intSI_type_node, 2);
6856   V2SF_type_node = build_vector_type (float_type_node, 2);
6857   V4HI_type_node = build_vector_type (intHI_type_node, 4);
6858   V4SI_type_node = build_vector_type (intSI_type_node, 4);
6859   V4SF_type_node = build_vector_type (float_type_node, 4);
6860   V8HI_type_node = build_vector_type (intHI_type_node, 8);
6861   V16QI_type_node = build_vector_type (intQI_type_node, 16);
6862
6863   unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
6864   unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
6865   unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
6866
6867   opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
6868   opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
6869   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
6870
6871   /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
6872      types, especially in C++ land.  Similarly, 'vector pixel' is distinct from
6873      'vector unsigned short'.  */
6874
6875   bool_char_type_node = copy_node (unsigned_intQI_type_node);
6876   TYPE_MAIN_VARIANT (bool_char_type_node) = bool_char_type_node;
6877   bool_short_type_node = copy_node (unsigned_intHI_type_node);
6878   TYPE_MAIN_VARIANT (bool_short_type_node) = bool_short_type_node;
6879   bool_int_type_node = copy_node (unsigned_intSI_type_node);
6880   TYPE_MAIN_VARIANT (bool_int_type_node) = bool_int_type_node;
6881   pixel_type_node = copy_node (unsigned_intHI_type_node);
6882   TYPE_MAIN_VARIANT (pixel_type_node) = pixel_type_node;
6883
6884   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6885                                             get_identifier ("__bool char"),
6886                                             bool_char_type_node));
6887   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6888                                             get_identifier ("__bool short"),
6889                                             bool_short_type_node));
6890   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6891                                             get_identifier ("__bool int"),
6892                                             bool_int_type_node));
6893   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6894                                             get_identifier ("__pixel"),
6895                                             pixel_type_node));
6896
6897   bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
6898   bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
6899   bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
6900   pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
6901
6902   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6903                                             get_identifier ("__vector unsigned char"),
6904                                             unsigned_V16QI_type_node));
6905   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6906                                             get_identifier ("__vector signed char"),
6907                                             V16QI_type_node));
6908   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6909                                             get_identifier ("__vector __bool char"),
6910                                             bool_V16QI_type_node));
6911
6912   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6913                                             get_identifier ("__vector unsigned short"),
6914                                             unsigned_V8HI_type_node));
6915   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6916                                             get_identifier ("__vector signed short"),
6917                                             V8HI_type_node));
6918   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6919                                             get_identifier ("__vector __bool short"),
6920                                             bool_V8HI_type_node));
6921
6922   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6923                                             get_identifier ("__vector unsigned int"),
6924                                             unsigned_V4SI_type_node));
6925   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6926                                             get_identifier ("__vector signed int"),
6927                                             V4SI_type_node));
6928   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6929                                             get_identifier ("__vector __bool int"),
6930                                             bool_V4SI_type_node));
6931
6932   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6933                                             get_identifier ("__vector float"),
6934                                             V4SF_type_node));
6935   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6936                                             get_identifier ("__vector __pixel"),
6937                                             pixel_V8HI_type_node));
6938
6939   if (TARGET_SPE)
6940     spe_init_builtins ();
6941   if (TARGET_ALTIVEC)
6942     altivec_init_builtins ();
6943   if (TARGET_ALTIVEC || TARGET_SPE)
6944     rs6000_common_init_builtins ();
6945 }
6946
6947 /* Search through a set of builtins and enable the mask bits.
6948    DESC is an array of builtins.
6949    SIZE is the total number of builtins.
6950    START is the builtin enum at which to start.
6951    END is the builtin enum at which to end.  */
6952 static void
6953 enable_mask_for_builtins (struct builtin_description *desc, int size,
6954                           enum rs6000_builtins start, 
6955                           enum rs6000_builtins end)
6956 {
6957   int i;
6958
6959   for (i = 0; i < size; ++i)
6960     if (desc[i].code == start)
6961       break;
6962
6963   if (i == size)
6964     return;
6965
6966   for (; i < size; ++i)
6967     {
6968       /* Flip all the bits on.  */
6969       desc[i].mask = target_flags;
6970       if (desc[i].code == end)
6971         break;
6972     }
6973 }
6974
6975 static void
6976 spe_init_builtins (void)
6977 {
6978   tree endlink = void_list_node;
6979   tree puint_type_node = build_pointer_type (unsigned_type_node);
6980   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
6981   struct builtin_description *d;
6982   size_t i;
6983
6984   tree v2si_ftype_4_v2si
6985     = build_function_type
6986     (opaque_V2SI_type_node,
6987      tree_cons (NULL_TREE, opaque_V2SI_type_node,
6988                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6989                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6990                                       tree_cons (NULL_TREE, opaque_V2SI_type_node,
6991                                                  endlink)))));
6992
6993   tree v2sf_ftype_4_v2sf
6994     = build_function_type
6995     (opaque_V2SF_type_node,
6996      tree_cons (NULL_TREE, opaque_V2SF_type_node,
6997                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6998                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6999                                       tree_cons (NULL_TREE, opaque_V2SF_type_node,
7000                                                  endlink)))));
7001
7002   tree int_ftype_int_v2si_v2si
7003     = build_function_type
7004     (integer_type_node,
7005      tree_cons (NULL_TREE, integer_type_node,
7006                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
7007                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
7008                                       endlink))));
7009
7010   tree int_ftype_int_v2sf_v2sf
7011     = build_function_type
7012     (integer_type_node,
7013      tree_cons (NULL_TREE, integer_type_node,
7014                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
7015                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
7016                                       endlink))));
7017
7018   tree void_ftype_v2si_puint_int
7019     = build_function_type (void_type_node,
7020                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
7021                                       tree_cons (NULL_TREE, puint_type_node,
7022                                                  tree_cons (NULL_TREE,
7023                                                             integer_type_node,
7024                                                             endlink))));
7025
7026   tree void_ftype_v2si_puint_char
7027     = build_function_type (void_type_node,
7028                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
7029                                       tree_cons (NULL_TREE, puint_type_node,
7030                                                  tree_cons (NULL_TREE,
7031                                                             char_type_node,
7032                                                             endlink))));
7033
7034   tree void_ftype_v2si_pv2si_int
7035     = build_function_type (void_type_node,
7036                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
7037                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
7038                                                  tree_cons (NULL_TREE,
7039                                                             integer_type_node,
7040                                                             endlink))));
7041
7042   tree void_ftype_v2si_pv2si_char
7043     = build_function_type (void_type_node,
7044                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
7045                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
7046                                                  tree_cons (NULL_TREE,
7047                                                             char_type_node,
7048                                                             endlink))));
7049
7050   tree void_ftype_int
7051     = build_function_type (void_type_node,
7052                            tree_cons (NULL_TREE, integer_type_node, endlink));
7053
7054   tree int_ftype_void
7055     = build_function_type (integer_type_node, endlink);
7056
7057   tree v2si_ftype_pv2si_int
7058     = build_function_type (opaque_V2SI_type_node,
7059                            tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
7060                                       tree_cons (NULL_TREE, integer_type_node,
7061                                                  endlink)));
7062
7063   tree v2si_ftype_puint_int
7064     = build_function_type (opaque_V2SI_type_node,
7065                            tree_cons (NULL_TREE, puint_type_node,
7066                                       tree_cons (NULL_TREE, integer_type_node,
7067                                                  endlink)));
7068
7069   tree v2si_ftype_pushort_int
7070     = build_function_type (opaque_V2SI_type_node,
7071                            tree_cons (NULL_TREE, pushort_type_node,
7072                                       tree_cons (NULL_TREE, integer_type_node,
7073                                                  endlink)));
7074
7075   tree v2si_ftype_signed_char
7076     = build_function_type (opaque_V2SI_type_node,
7077                            tree_cons (NULL_TREE, signed_char_type_node,
7078                                       endlink));
7079
7080   /* The initialization of the simple binary and unary builtins is
7081      done in rs6000_common_init_builtins, but we have to enable the
7082      mask bits here manually because we have run out of `target_flags'
7083      bits.  We really need to redesign this mask business.  */
7084
7085   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
7086                             ARRAY_SIZE (bdesc_2arg),
7087                             SPE_BUILTIN_EVADDW,
7088                             SPE_BUILTIN_EVXOR);
7089   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
7090                             ARRAY_SIZE (bdesc_1arg),
7091                             SPE_BUILTIN_EVABS,
7092                             SPE_BUILTIN_EVSUBFUSIAAW);
7093   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
7094                             ARRAY_SIZE (bdesc_spe_predicates),
7095                             SPE_BUILTIN_EVCMPEQ,
7096                             SPE_BUILTIN_EVFSTSTLT);
7097   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
7098                             ARRAY_SIZE (bdesc_spe_evsel),
7099                             SPE_BUILTIN_EVSEL_CMPGTS,
7100                             SPE_BUILTIN_EVSEL_FSTSTEQ);
7101
7102   (*lang_hooks.decls.pushdecl)
7103     (build_decl (TYPE_DECL, get_identifier ("__ev64_opaque__"),
7104                  opaque_V2SI_type_node));
7105
7106   /* Initialize irregular SPE builtins.  */
7107   
7108   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
7109   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
7110   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
7111   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
7112   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
7113   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
7114   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
7115   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
7116   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
7117   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
7118   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
7119   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
7120   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
7121   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
7122   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
7123   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
7124   def_builtin (target_flags, "__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
7125   def_builtin (target_flags, "__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
7126
7127   /* Loads.  */
7128   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
7129   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
7130   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
7131   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
7132   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
7133   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
7134   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
7135   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
7136   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
7137   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
7138   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
7139   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
7140   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
7141   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
7142   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
7143   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
7144   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
7145   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
7146   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
7147   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
7148   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
7149   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
7150
7151   /* Predicates.  */
7152   d = (struct builtin_description *) bdesc_spe_predicates;
7153   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
7154     {
7155       tree type;
7156
7157       switch (insn_data[d->icode].operand[1].mode)
7158         {
7159         case V2SImode:
7160           type = int_ftype_int_v2si_v2si;
7161           break;
7162         case V2SFmode:
7163           type = int_ftype_int_v2sf_v2sf;
7164           break;
7165         default:
7166           abort ();
7167         }
7168
7169       def_builtin (d->mask, d->name, type, d->code);
7170     }
7171
7172   /* Evsel predicates.  */
7173   d = (struct builtin_description *) bdesc_spe_evsel;
7174   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
7175     {
7176       tree type;
7177
7178       switch (insn_data[d->icode].operand[1].mode)
7179         {
7180         case V2SImode:
7181           type = v2si_ftype_4_v2si;
7182           break;
7183         case V2SFmode:
7184           type = v2sf_ftype_4_v2sf;
7185           break;
7186         default:
7187           abort ();
7188         }
7189
7190       def_builtin (d->mask, d->name, type, d->code);
7191     }
7192 }
7193
7194 static void
7195 altivec_init_builtins (void)
7196 {
7197   struct builtin_description *d;
7198   struct builtin_description_predicates *dp;
7199   size_t i;
7200   tree pfloat_type_node = build_pointer_type (float_type_node);
7201   tree pint_type_node = build_pointer_type (integer_type_node);
7202   tree pshort_type_node = build_pointer_type (short_integer_type_node);
7203   tree pchar_type_node = build_pointer_type (char_type_node);
7204
7205   tree pvoid_type_node = build_pointer_type (void_type_node);
7206
7207   tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
7208   tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
7209   tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
7210   tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
7211
7212   tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
7213
7214   tree int_ftype_int_v4si_v4si
7215     = build_function_type_list (integer_type_node,
7216                                 integer_type_node, V4SI_type_node,
7217                                 V4SI_type_node, NULL_TREE);
7218   tree v4sf_ftype_pcfloat
7219     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
7220   tree void_ftype_pfloat_v4sf
7221     = build_function_type_list (void_type_node,
7222                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
7223   tree v4si_ftype_pcint
7224     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
7225   tree void_ftype_pint_v4si
7226     = build_function_type_list (void_type_node,
7227                                 pint_type_node, V4SI_type_node, NULL_TREE);
7228   tree v8hi_ftype_pcshort
7229     = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
7230   tree void_ftype_pshort_v8hi
7231     = build_function_type_list (void_type_node,
7232                                 pshort_type_node, V8HI_type_node, NULL_TREE);
7233   tree v16qi_ftype_pcchar
7234     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
7235   tree void_ftype_pchar_v16qi
7236     = build_function_type_list (void_type_node,
7237                                 pchar_type_node, V16QI_type_node, NULL_TREE);
7238   tree void_ftype_v4si
7239     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
7240   tree v8hi_ftype_void
7241     = build_function_type (V8HI_type_node, void_list_node);
7242   tree void_ftype_void
7243     = build_function_type (void_type_node, void_list_node);
7244   tree void_ftype_qi
7245     = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
7246
7247   tree v16qi_ftype_long_pcvoid
7248     = build_function_type_list (V16QI_type_node,
7249                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
7250   tree v8hi_ftype_long_pcvoid
7251     = build_function_type_list (V8HI_type_node,
7252                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
7253   tree v4si_ftype_long_pcvoid
7254     = build_function_type_list (V4SI_type_node,
7255                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
7256
7257   tree void_ftype_v4si_long_pvoid
7258     = build_function_type_list (void_type_node,
7259                                 V4SI_type_node, long_integer_type_node,
7260                                 pvoid_type_node, NULL_TREE);
7261   tree void_ftype_v16qi_long_pvoid
7262     = build_function_type_list (void_type_node,
7263                                 V16QI_type_node, long_integer_type_node,
7264                                 pvoid_type_node, NULL_TREE);
7265   tree void_ftype_v8hi_long_pvoid
7266     = build_function_type_list (void_type_node,
7267                                 V8HI_type_node, long_integer_type_node,
7268                                 pvoid_type_node, NULL_TREE);
7269   tree int_ftype_int_v8hi_v8hi
7270     = build_function_type_list (integer_type_node,
7271                                 integer_type_node, V8HI_type_node,
7272                                 V8HI_type_node, NULL_TREE);
7273   tree int_ftype_int_v16qi_v16qi
7274     = build_function_type_list (integer_type_node,
7275                                 integer_type_node, V16QI_type_node,
7276                                 V16QI_type_node, NULL_TREE);
7277   tree int_ftype_int_v4sf_v4sf
7278     = build_function_type_list (integer_type_node,
7279                                 integer_type_node, V4SF_type_node,
7280                                 V4SF_type_node, NULL_TREE);
7281   tree v4si_ftype_v4si
7282     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
7283   tree v8hi_ftype_v8hi
7284     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
7285   tree v16qi_ftype_v16qi
7286     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
7287   tree v4sf_ftype_v4sf
7288     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
7289   tree void_ftype_pcvoid_int_int
7290     = build_function_type_list (void_type_node,
7291                                 pcvoid_type_node, integer_type_node,
7292                                 integer_type_node, NULL_TREE);
7293   tree int_ftype_pcchar
7294     = build_function_type_list (integer_type_node,
7295                                 pcchar_type_node, NULL_TREE);
7296
7297   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
7298                ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
7299   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
7300                ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
7301   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
7302                ALTIVEC_BUILTIN_LD_INTERNAL_4si);
7303   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
7304                ALTIVEC_BUILTIN_ST_INTERNAL_4si);
7305   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
7306                ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
7307   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
7308                ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
7309   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
7310                ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
7311   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
7312                ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
7313   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
7314   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
7315   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
7316   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
7317   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
7318   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
7319   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
7320   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
7321   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
7322   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
7323   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
7324   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
7325   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
7326   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
7327   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
7328   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
7329
7330   /* See altivec.h for usage of "__builtin_altivec_compiletime_error".  */
7331   def_builtin (MASK_ALTIVEC, "__builtin_altivec_compiletime_error", int_ftype_pcchar,
7332                ALTIVEC_BUILTIN_COMPILETIME_ERROR);
7333
7334   /* Add the DST variants.  */
7335   d = (struct builtin_description *) bdesc_dst;
7336   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
7337     def_builtin (d->mask, d->name, void_ftype_pcvoid_int_int, d->code);
7338
7339   /* Initialize the predicates.  */
7340   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
7341   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
7342     {
7343       enum machine_mode mode1;
7344       tree type;
7345
7346       mode1 = insn_data[dp->icode].operand[1].mode;
7347
7348       switch (mode1)
7349         {
7350         case V4SImode:
7351           type = int_ftype_int_v4si_v4si;
7352           break;
7353         case V8HImode:
7354           type = int_ftype_int_v8hi_v8hi;
7355           break;
7356         case V16QImode:
7357           type = int_ftype_int_v16qi_v16qi;
7358           break;
7359         case V4SFmode:
7360           type = int_ftype_int_v4sf_v4sf;
7361           break;
7362         default:
7363           abort ();
7364         }
7365       
7366       def_builtin (dp->mask, dp->name, type, dp->code);
7367     }
7368
7369   /* Initialize the abs* operators.  */
7370   d = (struct builtin_description *) bdesc_abs;
7371   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
7372     {
7373       enum machine_mode mode0;
7374       tree type;
7375
7376       mode0 = insn_data[d->icode].operand[0].mode;
7377
7378       switch (mode0)
7379         {
7380         case V4SImode:
7381           type = v4si_ftype_v4si;
7382           break;
7383         case V8HImode:
7384           type = v8hi_ftype_v8hi;
7385           break;
7386         case V16QImode:
7387           type = v16qi_ftype_v16qi;
7388           break;
7389         case V4SFmode:
7390           type = v4sf_ftype_v4sf;
7391           break;
7392         default:
7393           abort ();
7394         }
7395       
7396       def_builtin (d->mask, d->name, type, d->code);
7397     }
7398 }
7399
7400 static void
7401 rs6000_common_init_builtins (void)
7402 {
7403   struct builtin_description *d;
7404   size_t i;
7405
7406   tree v4sf_ftype_v4sf_v4sf_v16qi
7407     = build_function_type_list (V4SF_type_node,
7408                                 V4SF_type_node, V4SF_type_node,
7409                                 V16QI_type_node, NULL_TREE);
7410   tree v4si_ftype_v4si_v4si_v16qi
7411     = build_function_type_list (V4SI_type_node,
7412                                 V4SI_type_node, V4SI_type_node,
7413                                 V16QI_type_node, NULL_TREE);
7414   tree v8hi_ftype_v8hi_v8hi_v16qi
7415     = build_function_type_list (V8HI_type_node,
7416                                 V8HI_type_node, V8HI_type_node,
7417                                 V16QI_type_node, NULL_TREE);
7418   tree v16qi_ftype_v16qi_v16qi_v16qi
7419     = build_function_type_list (V16QI_type_node,
7420                                 V16QI_type_node, V16QI_type_node,
7421                                 V16QI_type_node, NULL_TREE);
7422   tree v4si_ftype_int
7423     = build_function_type_list (V4SI_type_node, integer_type_node, NULL_TREE);
7424   tree v8hi_ftype_int
7425     = build_function_type_list (V8HI_type_node, integer_type_node, NULL_TREE);
7426   tree v16qi_ftype_int
7427     = build_function_type_list (V16QI_type_node, integer_type_node, NULL_TREE);
7428   tree v8hi_ftype_v16qi
7429     = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
7430   tree v4sf_ftype_v4sf
7431     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
7432
7433   tree v2si_ftype_v2si_v2si
7434     = build_function_type_list (opaque_V2SI_type_node,
7435                                 opaque_V2SI_type_node,
7436                                 opaque_V2SI_type_node, NULL_TREE);
7437
7438   tree v2sf_ftype_v2sf_v2sf
7439     = build_function_type_list (opaque_V2SF_type_node,
7440                                 opaque_V2SF_type_node,
7441                                 opaque_V2SF_type_node, NULL_TREE);
7442
7443   tree v2si_ftype_int_int
7444     = build_function_type_list (opaque_V2SI_type_node,
7445                                 integer_type_node, integer_type_node,
7446                                 NULL_TREE);
7447
7448   tree v2si_ftype_v2si
7449     = build_function_type_list (opaque_V2SI_type_node,
7450                                 opaque_V2SI_type_node, NULL_TREE);
7451
7452   tree v2sf_ftype_v2sf
7453     = build_function_type_list (opaque_V2SF_type_node,
7454                                 opaque_V2SF_type_node, NULL_TREE);
7455   
7456   tree v2sf_ftype_v2si
7457     = build_function_type_list (opaque_V2SF_type_node,
7458                                 opaque_V2SI_type_node, NULL_TREE);
7459
7460   tree v2si_ftype_v2sf
7461     = build_function_type_list (opaque_V2SI_type_node,
7462                                 opaque_V2SF_type_node, NULL_TREE);
7463
7464   tree v2si_ftype_v2si_char
7465     = build_function_type_list (opaque_V2SI_type_node,
7466                                 opaque_V2SI_type_node,
7467                                 char_type_node, NULL_TREE);
7468
7469   tree v2si_ftype_int_char
7470     = build_function_type_list (opaque_V2SI_type_node,
7471                                 integer_type_node, char_type_node, NULL_TREE);
7472
7473   tree v2si_ftype_char
7474     = build_function_type_list (opaque_V2SI_type_node,
7475                                 char_type_node, NULL_TREE);
7476
7477   tree int_ftype_int_int
7478     = build_function_type_list (integer_type_node,
7479                                 integer_type_node, integer_type_node,
7480                                 NULL_TREE);
7481
7482   tree v4si_ftype_v4si_v4si
7483     = build_function_type_list (V4SI_type_node,
7484                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
7485   tree v4sf_ftype_v4si_int
7486     = build_function_type_list (V4SF_type_node,
7487                                 V4SI_type_node, integer_type_node, NULL_TREE);
7488   tree v4si_ftype_v4sf_int
7489     = build_function_type_list (V4SI_type_node,
7490                                 V4SF_type_node, integer_type_node, NULL_TREE);
7491   tree v4si_ftype_v4si_int
7492     = build_function_type_list (V4SI_type_node,
7493                                 V4SI_type_node, integer_type_node, NULL_TREE);
7494   tree v8hi_ftype_v8hi_int
7495     = build_function_type_list (V8HI_type_node,
7496                                 V8HI_type_node, integer_type_node, NULL_TREE);
7497   tree v16qi_ftype_v16qi_int
7498     = build_function_type_list (V16QI_type_node,
7499                                 V16QI_type_node, integer_type_node, NULL_TREE);
7500   tree v16qi_ftype_v16qi_v16qi_int
7501     = build_function_type_list (V16QI_type_node,
7502                                 V16QI_type_node, V16QI_type_node,
7503                                 integer_type_node, NULL_TREE);
7504   tree v8hi_ftype_v8hi_v8hi_int
7505     = build_function_type_list (V8HI_type_node,
7506                                 V8HI_type_node, V8HI_type_node,
7507                                 integer_type_node, NULL_TREE);
7508   tree v4si_ftype_v4si_v4si_int
7509     = build_function_type_list (V4SI_type_node,
7510                                 V4SI_type_node, V4SI_type_node,
7511                                 integer_type_node, NULL_TREE);
7512   tree v4sf_ftype_v4sf_v4sf_int
7513     = build_function_type_list (V4SF_type_node,
7514                                 V4SF_type_node, V4SF_type_node,
7515                                 integer_type_node, NULL_TREE);
7516   tree v4sf_ftype_v4sf_v4sf
7517     = build_function_type_list (V4SF_type_node,
7518                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
7519   tree v4sf_ftype_v4sf_v4sf_v4si
7520     = build_function_type_list (V4SF_type_node,
7521                                 V4SF_type_node, V4SF_type_node,
7522                                 V4SI_type_node, NULL_TREE);
7523   tree v4sf_ftype_v4sf_v4sf_v4sf
7524     = build_function_type_list (V4SF_type_node,
7525                                 V4SF_type_node, V4SF_type_node,
7526                                 V4SF_type_node, NULL_TREE);
7527   tree v4si_ftype_v4si_v4si_v4si 
7528     = build_function_type_list (V4SI_type_node,
7529                                 V4SI_type_node, V4SI_type_node,
7530                                 V4SI_type_node, NULL_TREE);
7531   tree v8hi_ftype_v8hi_v8hi
7532     = build_function_type_list (V8HI_type_node,
7533                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
7534   tree v8hi_ftype_v8hi_v8hi_v8hi
7535     = build_function_type_list (V8HI_type_node,
7536                                 V8HI_type_node, V8HI_type_node,
7537                                 V8HI_type_node, NULL_TREE);
7538  tree v4si_ftype_v8hi_v8hi_v4si
7539     = build_function_type_list (V4SI_type_node,
7540                                 V8HI_type_node, V8HI_type_node,
7541                                 V4SI_type_node, NULL_TREE);
7542  tree v4si_ftype_v16qi_v16qi_v4si
7543     = build_function_type_list (V4SI_type_node,
7544                                 V16QI_type_node, V16QI_type_node,
7545                                 V4SI_type_node, NULL_TREE);
7546   tree v16qi_ftype_v16qi_v16qi
7547     = build_function_type_list (V16QI_type_node,
7548                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
7549   tree v4si_ftype_v4sf_v4sf
7550     = build_function_type_list (V4SI_type_node,
7551                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
7552   tree v8hi_ftype_v16qi_v16qi
7553     = build_function_type_list (V8HI_type_node,
7554                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
7555   tree v4si_ftype_v8hi_v8hi
7556     = build_function_type_list (V4SI_type_node,
7557                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
7558   tree v8hi_ftype_v4si_v4si
7559     = build_function_type_list (V8HI_type_node,
7560                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
7561   tree v16qi_ftype_v8hi_v8hi
7562     = build_function_type_list (V16QI_type_node,
7563                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
7564   tree v4si_ftype_v16qi_v4si
7565     = build_function_type_list (V4SI_type_node,
7566                                 V16QI_type_node, V4SI_type_node, NULL_TREE);
7567   tree v4si_ftype_v16qi_v16qi
7568     = build_function_type_list (V4SI_type_node,
7569                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
7570   tree v4si_ftype_v8hi_v4si
7571     = build_function_type_list (V4SI_type_node,
7572                                 V8HI_type_node, V4SI_type_node, NULL_TREE);
7573   tree v4si_ftype_v8hi
7574     = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
7575   tree int_ftype_v4si_v4si
7576     = build_function_type_list (integer_type_node,
7577                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
7578   tree int_ftype_v4sf_v4sf
7579     = build_function_type_list (integer_type_node,
7580                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
7581   tree int_ftype_v16qi_v16qi
7582     = build_function_type_list (integer_type_node,
7583                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
7584   tree int_ftype_v8hi_v8hi
7585     = build_function_type_list (integer_type_node,
7586                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
7587
7588   /* Add the simple ternary operators.  */
7589   d = (struct builtin_description *) bdesc_3arg;
7590   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
7591     {
7592       
7593       enum machine_mode mode0, mode1, mode2, mode3;
7594       tree type;
7595
7596       if (d->name == 0 || d->icode == CODE_FOR_nothing)
7597         continue;
7598       
7599       mode0 = insn_data[d->icode].operand[0].mode;
7600       mode1 = insn_data[d->icode].operand[1].mode;
7601       mode2 = insn_data[d->icode].operand[2].mode;
7602       mode3 = insn_data[d->icode].operand[3].mode;
7603       
7604       /* When all four are of the same mode.  */
7605       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
7606         {
7607           switch (mode0)
7608             {
7609             case V4SImode:
7610               type = v4si_ftype_v4si_v4si_v4si;
7611               break;
7612             case V4SFmode:
7613               type = v4sf_ftype_v4sf_v4sf_v4sf;
7614               break;
7615             case V8HImode:
7616               type = v8hi_ftype_v8hi_v8hi_v8hi;
7617               break;          
7618             case V16QImode:
7619               type = v16qi_ftype_v16qi_v16qi_v16qi;
7620               break;          
7621             default:
7622               abort();        
7623             }
7624         }
7625       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
7626         {
7627           switch (mode0)
7628             {
7629             case V4SImode:
7630               type = v4si_ftype_v4si_v4si_v16qi;
7631               break;
7632             case V4SFmode:
7633               type = v4sf_ftype_v4sf_v4sf_v16qi;
7634               break;
7635             case V8HImode:
7636               type = v8hi_ftype_v8hi_v8hi_v16qi;
7637               break;          
7638             case V16QImode:
7639               type = v16qi_ftype_v16qi_v16qi_v16qi;
7640               break;          
7641             default:
7642               abort();        
7643             }
7644         }
7645       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
7646                && mode3 == V4SImode)
7647         type = v4si_ftype_v16qi_v16qi_v4si;
7648       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
7649                && mode3 == V4SImode)
7650         type = v4si_ftype_v8hi_v8hi_v4si;
7651       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
7652                && mode3 == V4SImode)
7653         type = v4sf_ftype_v4sf_v4sf_v4si;
7654
7655       /* vchar, vchar, vchar, 4 bit literal.  */
7656       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
7657                && mode3 == QImode)
7658         type = v16qi_ftype_v16qi_v16qi_int;
7659
7660       /* vshort, vshort, vshort, 4 bit literal.  */
7661       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
7662                && mode3 == QImode)
7663         type = v8hi_ftype_v8hi_v8hi_int;
7664
7665       /* vint, vint, vint, 4 bit literal.  */
7666       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
7667                && mode3 == QImode)
7668         type = v4si_ftype_v4si_v4si_int;
7669
7670       /* vfloat, vfloat, vfloat, 4 bit literal.  */
7671       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
7672                && mode3 == QImode)
7673         type = v4sf_ftype_v4sf_v4sf_int;
7674
7675       else
7676         abort ();
7677
7678       def_builtin (d->mask, d->name, type, d->code);
7679     }
7680
7681   /* Add the simple binary operators.  */
7682   d = (struct builtin_description *) bdesc_2arg;
7683   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
7684     {
7685       enum machine_mode mode0, mode1, mode2;
7686       tree type;
7687
7688       if (d->name == 0 || d->icode == CODE_FOR_nothing)
7689         continue;
7690       
7691       mode0 = insn_data[d->icode].operand[0].mode;
7692       mode1 = insn_data[d->icode].operand[1].mode;
7693       mode2 = insn_data[d->icode].operand[2].mode;
7694
7695       /* When all three operands are of the same mode.  */
7696       if (mode0 == mode1 && mode1 == mode2)
7697         {
7698           switch (mode0)
7699             {
7700             case V4SFmode:
7701               type = v4sf_ftype_v4sf_v4sf;
7702               break;
7703             case V4SImode:
7704               type = v4si_ftype_v4si_v4si;
7705               break;
7706             case V16QImode:
7707               type = v16qi_ftype_v16qi_v16qi;
7708               break;
7709             case V8HImode:
7710               type = v8hi_ftype_v8hi_v8hi;
7711               break;
7712             case V2SImode:
7713               type = v2si_ftype_v2si_v2si;
7714               break;
7715             case V2SFmode:
7716               type = v2sf_ftype_v2sf_v2sf;
7717               break;
7718             case SImode:
7719               type = int_ftype_int_int;
7720               break;
7721             default:
7722               abort ();
7723             }
7724         }
7725
7726       /* A few other combos we really don't want to do manually.  */
7727
7728       /* vint, vfloat, vfloat.  */
7729       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
7730         type = v4si_ftype_v4sf_v4sf;
7731
7732       /* vshort, vchar, vchar.  */
7733       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
7734         type = v8hi_ftype_v16qi_v16qi;
7735
7736       /* vint, vshort, vshort.  */
7737       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
7738         type = v4si_ftype_v8hi_v8hi;
7739
7740       /* vshort, vint, vint.  */
7741       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
7742         type = v8hi_ftype_v4si_v4si;
7743
7744       /* vchar, vshort, vshort.  */
7745       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
7746         type = v16qi_ftype_v8hi_v8hi;
7747
7748       /* vint, vchar, vint.  */
7749       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
7750         type = v4si_ftype_v16qi_v4si;
7751
7752       /* vint, vchar, vchar.  */
7753       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
7754         type = v4si_ftype_v16qi_v16qi;
7755
7756       /* vint, vshort, vint.  */
7757       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
7758         type = v4si_ftype_v8hi_v4si;
7759       
7760       /* vint, vint, 5 bit literal.  */
7761       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
7762         type = v4si_ftype_v4si_int;
7763       
7764       /* vshort, vshort, 5 bit literal.  */
7765       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
7766         type = v8hi_ftype_v8hi_int;
7767       
7768       /* vchar, vchar, 5 bit literal.  */
7769       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
7770         type = v16qi_ftype_v16qi_int;
7771
7772       /* vfloat, vint, 5 bit literal.  */
7773       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
7774         type = v4sf_ftype_v4si_int;
7775       
7776       /* vint, vfloat, 5 bit literal.  */
7777       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
7778         type = v4si_ftype_v4sf_int;
7779
7780       else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
7781         type = v2si_ftype_int_int;
7782
7783       else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
7784         type = v2si_ftype_v2si_char;
7785
7786       else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
7787         type = v2si_ftype_int_char;
7788
7789       /* int, x, x.  */
7790       else if (mode0 == SImode)
7791         {
7792           switch (mode1)
7793             {
7794             case V4SImode:
7795               type = int_ftype_v4si_v4si;
7796               break;
7797             case V4SFmode:
7798               type = int_ftype_v4sf_v4sf;
7799               break;
7800             case V16QImode:
7801               type = int_ftype_v16qi_v16qi;
7802               break;
7803             case V8HImode:
7804               type = int_ftype_v8hi_v8hi;
7805               break;
7806             default:
7807               abort ();
7808             }
7809         }
7810
7811       else
7812         abort ();
7813
7814       def_builtin (d->mask, d->name, type, d->code);
7815     }
7816
7817   /* Add the simple unary operators.  */
7818   d = (struct builtin_description *) bdesc_1arg;
7819   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
7820     {
7821       enum machine_mode mode0, mode1;
7822       tree type;
7823
7824       if (d->name == 0 || d->icode == CODE_FOR_nothing)
7825         continue;
7826       
7827       mode0 = insn_data[d->icode].operand[0].mode;
7828       mode1 = insn_data[d->icode].operand[1].mode;
7829
7830       if (mode0 == V4SImode && mode1 == QImode)
7831         type = v4si_ftype_int;
7832       else if (mode0 == V8HImode && mode1 == QImode)
7833         type = v8hi_ftype_int;
7834       else if (mode0 == V16QImode && mode1 == QImode)
7835         type = v16qi_ftype_int;
7836       else if (mode0 == V4SFmode && mode1 == V4SFmode)
7837         type = v4sf_ftype_v4sf;
7838       else if (mode0 == V8HImode && mode1 == V16QImode)
7839         type = v8hi_ftype_v16qi;
7840       else if (mode0 == V4SImode && mode1 == V8HImode)
7841         type = v4si_ftype_v8hi;
7842       else if (mode0 == V2SImode && mode1 == V2SImode)
7843         type = v2si_ftype_v2si;
7844       else if (mode0 == V2SFmode && mode1 == V2SFmode)
7845         type = v2sf_ftype_v2sf;
7846       else if (mode0 == V2SFmode && mode1 == V2SImode)
7847         type = v2sf_ftype_v2si;
7848       else if (mode0 == V2SImode && mode1 == V2SFmode)
7849         type = v2si_ftype_v2sf;
7850       else if (mode0 == V2SImode && mode1 == QImode)
7851         type = v2si_ftype_char;
7852       else
7853         abort ();
7854
7855       def_builtin (d->mask, d->name, type, d->code);
7856     }
7857 }
7858
7859 static void
7860 rs6000_init_libfuncs (void)
7861 {
7862   if (!TARGET_HARD_FLOAT)
7863     return;
7864
7865   if (DEFAULT_ABI != ABI_V4)
7866     {
7867       if (TARGET_XCOFF && ! TARGET_POWER2 && ! TARGET_POWERPC)
7868         {
7869           /* AIX library routines for float->int conversion.  */
7870           set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
7871           set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
7872           set_conv_libfunc (sfix_optab, SImode, TFmode, "_qitrunc");
7873           set_conv_libfunc (ufix_optab, SImode, TFmode, "_quitrunc");
7874         }
7875
7876       /* Standard AIX/Darwin/64-bit SVR4 quad floating point routines.  */
7877       set_optab_libfunc (add_optab, TFmode, "_xlqadd");
7878       set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
7879       set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
7880       set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
7881     }
7882   else
7883     {
7884       /* 32-bit SVR4 quad floating point routines.  */
7885
7886       set_optab_libfunc (add_optab, TFmode, "_q_add");
7887       set_optab_libfunc (sub_optab, TFmode, "_q_sub");
7888       set_optab_libfunc (neg_optab, TFmode, "_q_neg");
7889       set_optab_libfunc (smul_optab, TFmode, "_q_mul");
7890       set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
7891       if (TARGET_PPC_GPOPT || TARGET_POWER2)
7892         set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
7893
7894       set_optab_libfunc (eq_optab, TFmode, "_q_feq");
7895       set_optab_libfunc (ne_optab, TFmode, "_q_fne");
7896       set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
7897       set_optab_libfunc (ge_optab, TFmode, "_q_fge");
7898       set_optab_libfunc (lt_optab, TFmode, "_q_flt");
7899       set_optab_libfunc (le_optab, TFmode, "_q_fle");
7900
7901       set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
7902       set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
7903       set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
7904       set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
7905       set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
7906       set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
7907       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
7908     }
7909 }
7910 \f
7911 /* Expand a block move operation, and return 1 if successful.  Return 0
7912    if we should let the compiler generate normal code.
7913
7914    operands[0] is the destination
7915    operands[1] is the source
7916    operands[2] is the length
7917    operands[3] is the alignment */
7918
7919 #define MAX_MOVE_REG 4
7920
7921 int
7922 expand_block_move (rtx operands[])
7923 {
7924   rtx orig_dest = operands[0];
7925   rtx orig_src  = operands[1];
7926   rtx bytes_rtx = operands[2];
7927   rtx align_rtx = operands[3];
7928   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
7929   int align;
7930   int bytes;
7931   int offset;
7932   int move_bytes;
7933   rtx stores[MAX_MOVE_REG];
7934   int num_reg = 0;
7935
7936   /* If this is not a fixed size move, just call memcpy */
7937   if (! constp)
7938     return 0;
7939
7940   /* If this is not a fixed size alignment, abort */
7941   if (GET_CODE (align_rtx) != CONST_INT)
7942     abort ();
7943   align = INTVAL (align_rtx);
7944
7945   /* Anything to move? */
7946   bytes = INTVAL (bytes_rtx);
7947   if (bytes <= 0)
7948     return 1;
7949
7950   /* store_one_arg depends on expand_block_move to handle at least the size of
7951      reg_parm_stack_space.  */  
7952   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
7953     return 0;
7954
7955   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
7956     {
7957       union {
7958         rtx (*movstrsi) (rtx, rtx, rtx, rtx);
7959         rtx (*mov) (rtx, rtx);
7960       } gen_func;
7961       enum machine_mode mode = BLKmode;
7962       rtx src, dest;
7963       
7964       if (TARGET_STRING
7965           && bytes > 24         /* move up to 32 bytes at a time */
7966           && ! fixed_regs[5]
7967           && ! fixed_regs[6]
7968           && ! fixed_regs[7]
7969           && ! fixed_regs[8]
7970           && ! fixed_regs[9]
7971           && ! fixed_regs[10]
7972           && ! fixed_regs[11]
7973           && ! fixed_regs[12])
7974         {
7975           move_bytes = (bytes > 32) ? 32 : bytes;
7976           gen_func.movstrsi = gen_movstrsi_8reg;
7977         }
7978       else if (TARGET_STRING
7979                && bytes > 16    /* move up to 24 bytes at a time */
7980                && ! fixed_regs[5]
7981                && ! fixed_regs[6]
7982                && ! fixed_regs[7]
7983                && ! fixed_regs[8]
7984                && ! fixed_regs[9]
7985                && ! fixed_regs[10])
7986         {
7987           move_bytes = (bytes > 24) ? 24 : bytes;
7988           gen_func.movstrsi = gen_movstrsi_6reg;
7989         }
7990       else if (TARGET_STRING
7991                && bytes > 8     /* move up to 16 bytes at a time */
7992                && ! fixed_regs[5]
7993                && ! fixed_regs[6]
7994                && ! fixed_regs[7]
7995                && ! fixed_regs[8])
7996         {
7997           move_bytes = (bytes > 16) ? 16 : bytes;
7998           gen_func.movstrsi = gen_movstrsi_4reg;
7999         }
8000       else if (bytes >= 8 && TARGET_POWERPC64
8001                /* 64-bit loads and stores require word-aligned
8002                   displacements.  */
8003                && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
8004         {
8005           move_bytes = 8;
8006           mode = DImode;
8007           gen_func.mov = gen_movdi;
8008         }
8009       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
8010         {                       /* move up to 8 bytes at a time */
8011           move_bytes = (bytes > 8) ? 8 : bytes;
8012           gen_func.movstrsi = gen_movstrsi_2reg;
8013         }
8014       else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
8015         {                       /* move 4 bytes */
8016           move_bytes = 4;
8017           mode = SImode;
8018           gen_func.mov = gen_movsi;
8019         }
8020       else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
8021         {                       /* move 2 bytes */
8022           move_bytes = 2;
8023           mode = HImode;
8024           gen_func.mov = gen_movhi;
8025         }
8026       else if (TARGET_STRING && bytes > 1)
8027         {                       /* move up to 4 bytes at a time */
8028           move_bytes = (bytes > 4) ? 4 : bytes;
8029           gen_func.movstrsi = gen_movstrsi_1reg;
8030         }
8031       else /* move 1 byte at a time */
8032         {
8033           move_bytes = 1;
8034           mode = QImode;
8035           gen_func.mov = gen_movqi;
8036         }
8037       
8038       src = adjust_address (orig_src, mode, offset);
8039       dest = adjust_address (orig_dest, mode, offset);
8040       
8041       if (mode != BLKmode) 
8042         {
8043           rtx tmp_reg = gen_reg_rtx (mode);
8044           
8045           emit_insn ((*gen_func.mov) (tmp_reg, src));
8046           stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
8047         }
8048
8049       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
8050         {
8051           int i;
8052           for (i = 0; i < num_reg; i++)
8053             emit_insn (stores[i]);
8054           num_reg = 0;
8055         }
8056
8057       if (mode == BLKmode)
8058         {
8059           /* Move the address into scratch registers.  The movstrsi
8060              patterns require zero offset.  */
8061           if (!REG_P (XEXP (src, 0)))
8062             {
8063               rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
8064               src = replace_equiv_address (src, src_reg);
8065             }
8066           set_mem_size (src, GEN_INT (move_bytes));
8067           
8068           if (!REG_P (XEXP (dest, 0)))
8069             {
8070               rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
8071               dest = replace_equiv_address (dest, dest_reg);
8072             }
8073           set_mem_size (dest, GEN_INT (move_bytes));
8074           
8075           emit_insn ((*gen_func.movstrsi) (dest, src,
8076                                            GEN_INT (move_bytes & 31),
8077                                            align_rtx));
8078         }
8079     }
8080
8081   return 1;
8082 }
8083
8084 \f
8085 /* Return 1 if OP is a load multiple operation.  It is known to be a
8086    PARALLEL and the first section will be tested.  */
8087
8088 int
8089 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8090 {
8091   int count = XVECLEN (op, 0);
8092   unsigned int dest_regno;
8093   rtx src_addr;
8094   int i;
8095
8096   /* Perform a quick check so we don't blow up below.  */
8097   if (count <= 1
8098       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8099       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
8100       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
8101     return 0;
8102
8103   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
8104   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
8105
8106   for (i = 1; i < count; i++)
8107     {
8108       rtx elt = XVECEXP (op, 0, i);
8109
8110       if (GET_CODE (elt) != SET
8111           || GET_CODE (SET_DEST (elt)) != REG
8112           || GET_MODE (SET_DEST (elt)) != SImode
8113           || REGNO (SET_DEST (elt)) != dest_regno + i
8114           || GET_CODE (SET_SRC (elt)) != MEM
8115           || GET_MODE (SET_SRC (elt)) != SImode
8116           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
8117           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
8118           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
8119           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
8120         return 0;
8121     }
8122
8123   return 1;
8124 }
8125
8126 /* Similar, but tests for store multiple.  Here, the second vector element
8127    is a CLOBBER.  It will be tested later.  */
8128
8129 int
8130 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8131 {
8132   int count = XVECLEN (op, 0) - 1;
8133   unsigned int src_regno;
8134   rtx dest_addr;
8135   int i;
8136
8137   /* Perform a quick check so we don't blow up below.  */
8138   if (count <= 1
8139       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8140       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
8141       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
8142     return 0;
8143
8144   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
8145   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
8146
8147   for (i = 1; i < count; i++)
8148     {
8149       rtx elt = XVECEXP (op, 0, i + 1);
8150
8151       if (GET_CODE (elt) != SET
8152           || GET_CODE (SET_SRC (elt)) != REG
8153           || GET_MODE (SET_SRC (elt)) != SImode
8154           || REGNO (SET_SRC (elt)) != src_regno + i
8155           || GET_CODE (SET_DEST (elt)) != MEM
8156           || GET_MODE (SET_DEST (elt)) != SImode
8157           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
8158           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
8159           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
8160           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
8161         return 0;
8162     }
8163
8164   return 1;
8165 }
8166
8167 /* Return a string to perform a load_multiple operation.
8168    operands[0] is the vector.
8169    operands[1] is the source address.
8170    operands[2] is the first destination register.  */
8171
8172 const char *
8173 rs6000_output_load_multiple (rtx operands[3])
8174 {
8175   /* We have to handle the case where the pseudo used to contain the address
8176      is assigned to one of the output registers.  */
8177   int i, j;
8178   int words = XVECLEN (operands[0], 0);
8179   rtx xop[10];
8180
8181   if (XVECLEN (operands[0], 0) == 1)
8182     return "{l|lwz} %2,0(%1)";
8183
8184   for (i = 0; i < words; i++)
8185     if (refers_to_regno_p (REGNO (operands[2]) + i,
8186                            REGNO (operands[2]) + i + 1, operands[1], 0))
8187       {
8188         if (i == words-1)
8189           {
8190             xop[0] = GEN_INT (4 * (words-1));
8191             xop[1] = operands[1];
8192             xop[2] = operands[2];
8193             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
8194             return "";
8195           }
8196         else if (i == 0)
8197           {
8198             xop[0] = GEN_INT (4 * (words-1));
8199             xop[1] = operands[1];
8200             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8201             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);
8202             return "";
8203           }
8204         else
8205           {
8206             for (j = 0; j < words; j++)
8207               if (j != i)
8208                 {
8209                   xop[0] = GEN_INT (j * 4);
8210                   xop[1] = operands[1];
8211                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
8212                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
8213                 }
8214             xop[0] = GEN_INT (i * 4);
8215             xop[1] = operands[1];
8216             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
8217             return "";
8218           }
8219       }
8220
8221   return "{lsi|lswi} %2,%1,%N0";
8222 }
8223
8224 /* Return 1 for a parallel vrsave operation.  */
8225
8226 int
8227 vrsave_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8228 {
8229   int count = XVECLEN (op, 0);
8230   unsigned int dest_regno, src_regno;
8231   int i;
8232
8233   if (count <= 1
8234       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8235       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
8236       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
8237     return 0;
8238
8239   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
8240   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
8241
8242   if (dest_regno != VRSAVE_REGNO
8243       && src_regno != VRSAVE_REGNO)
8244     return 0;
8245
8246   for (i = 1; i < count; i++)
8247     {
8248       rtx elt = XVECEXP (op, 0, i);
8249
8250       if (GET_CODE (elt) != CLOBBER
8251           && GET_CODE (elt) != SET)
8252         return 0;
8253     }
8254
8255   return 1;
8256 }
8257
8258 /* Return 1 for an PARALLEL suitable for mfcr.  */
8259
8260 int
8261 mfcr_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8262 {
8263   int count = XVECLEN (op, 0);
8264   int i;
8265
8266   /* Perform a quick check so we don't blow up below.  */
8267   if (count < 1
8268       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8269       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
8270       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
8271     return 0;
8272
8273   for (i = 0; i < count; i++)
8274     {
8275       rtx exp = XVECEXP (op, 0, i);
8276       rtx unspec;
8277       int maskval;
8278       rtx src_reg;
8279
8280       src_reg = XVECEXP (SET_SRC (exp), 0, 0);
8281
8282       if (GET_CODE (src_reg) != REG
8283           || GET_MODE (src_reg) != CCmode
8284           || ! CR_REGNO_P (REGNO (src_reg)))
8285         return 0;
8286
8287       if (GET_CODE (exp) != SET
8288           || GET_CODE (SET_DEST (exp)) != REG
8289           || GET_MODE (SET_DEST (exp)) != SImode
8290           || ! INT_REGNO_P (REGNO (SET_DEST (exp))))
8291         return 0;
8292       unspec = SET_SRC (exp);
8293       maskval = 1 << (MAX_CR_REGNO - REGNO (src_reg));
8294
8295       if (GET_CODE (unspec) != UNSPEC
8296           || XINT (unspec, 1) != UNSPEC_MOVESI_FROM_CR
8297           || XVECLEN (unspec, 0) != 2
8298           || XVECEXP (unspec, 0, 0) != src_reg
8299           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
8300           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
8301         return 0;
8302     }
8303   return 1;
8304 }
8305
8306 /* Return 1 for an PARALLEL suitable for mtcrf.  */
8307
8308 int
8309 mtcrf_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8310 {
8311   int count = XVECLEN (op, 0);
8312   int i;
8313   rtx src_reg;
8314
8315   /* Perform a quick check so we don't blow up below.  */
8316   if (count < 1
8317       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8318       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
8319       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
8320     return 0;
8321   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
8322   
8323   if (GET_CODE (src_reg) != REG
8324       || GET_MODE (src_reg) != SImode
8325       || ! INT_REGNO_P (REGNO (src_reg)))
8326     return 0;
8327
8328   for (i = 0; i < count; i++)
8329     {
8330       rtx exp = XVECEXP (op, 0, i);
8331       rtx unspec;
8332       int maskval;
8333       
8334       if (GET_CODE (exp) != SET
8335           || GET_CODE (SET_DEST (exp)) != REG
8336           || GET_MODE (SET_DEST (exp)) != CCmode
8337           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
8338         return 0;
8339       unspec = SET_SRC (exp);
8340       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
8341       
8342       if (GET_CODE (unspec) != UNSPEC
8343           || XINT (unspec, 1) != UNSPEC_MOVESI_TO_CR
8344           || XVECLEN (unspec, 0) != 2
8345           || XVECEXP (unspec, 0, 0) != src_reg
8346           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
8347           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
8348         return 0;
8349     }
8350   return 1;
8351 }
8352
8353 /* Return 1 for an PARALLEL suitable for lmw.  */
8354
8355 int
8356 lmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8357 {
8358   int count = XVECLEN (op, 0);
8359   unsigned int dest_regno;
8360   rtx src_addr;
8361   unsigned int base_regno;
8362   HOST_WIDE_INT offset;
8363   int i;
8364
8365   /* Perform a quick check so we don't blow up below.  */
8366   if (count <= 1
8367       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8368       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
8369       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
8370     return 0;
8371
8372   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
8373   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
8374
8375   if (dest_regno > 31
8376       || count != 32 - (int) dest_regno)
8377     return 0;
8378
8379   if (legitimate_indirect_address_p (src_addr, 0))
8380     {
8381       offset = 0;
8382       base_regno = REGNO (src_addr);
8383       if (base_regno == 0)
8384         return 0;
8385     }
8386   else if (legitimate_offset_address_p (SImode, src_addr, 0))
8387     {
8388       offset = INTVAL (XEXP (src_addr, 1));
8389       base_regno = REGNO (XEXP (src_addr, 0));
8390     }
8391   else
8392     return 0;
8393
8394   for (i = 0; i < count; i++)
8395     {
8396       rtx elt = XVECEXP (op, 0, i);
8397       rtx newaddr;
8398       rtx addr_reg;
8399       HOST_WIDE_INT newoffset;
8400
8401       if (GET_CODE (elt) != SET
8402           || GET_CODE (SET_DEST (elt)) != REG
8403           || GET_MODE (SET_DEST (elt)) != SImode
8404           || REGNO (SET_DEST (elt)) != dest_regno + i
8405           || GET_CODE (SET_SRC (elt)) != MEM
8406           || GET_MODE (SET_SRC (elt)) != SImode)
8407         return 0;
8408       newaddr = XEXP (SET_SRC (elt), 0);
8409       if (legitimate_indirect_address_p (newaddr, 0))
8410         {
8411           newoffset = 0;
8412           addr_reg = newaddr;
8413         }
8414       else if (legitimate_offset_address_p (SImode, newaddr, 0))
8415         {
8416           addr_reg = XEXP (newaddr, 0);
8417           newoffset = INTVAL (XEXP (newaddr, 1));
8418         }
8419       else
8420         return 0;
8421       if (REGNO (addr_reg) != base_regno
8422           || newoffset != offset + 4 * i)
8423         return 0;
8424     }
8425
8426   return 1;
8427 }
8428
8429 /* Return 1 for an PARALLEL suitable for stmw.  */
8430
8431 int
8432 stmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8433 {
8434   int count = XVECLEN (op, 0);
8435   unsigned int src_regno;
8436   rtx dest_addr;
8437   unsigned int base_regno;
8438   HOST_WIDE_INT offset;
8439   int i;
8440
8441   /* Perform a quick check so we don't blow up below.  */
8442   if (count <= 1
8443       || GET_CODE (XVECEXP (op, 0, 0)) != SET
8444       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
8445       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
8446     return 0;
8447
8448   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
8449   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
8450
8451   if (src_regno > 31
8452       || count != 32 - (int) src_regno)
8453     return 0;
8454
8455   if (legitimate_indirect_address_p (dest_addr, 0))
8456     {
8457       offset = 0;
8458       base_regno = REGNO (dest_addr);
8459       if (base_regno == 0)
8460         return 0;
8461     }
8462   else if (legitimate_offset_address_p (SImode, dest_addr, 0))
8463     {
8464       offset = INTVAL (XEXP (dest_addr, 1));
8465       base_regno = REGNO (XEXP (dest_addr, 0));
8466     }
8467   else
8468     return 0;
8469
8470   for (i = 0; i < count; i++)
8471     {
8472       rtx elt = XVECEXP (op, 0, i);
8473       rtx newaddr;
8474       rtx addr_reg;
8475       HOST_WIDE_INT newoffset;
8476
8477       if (GET_CODE (elt) != SET
8478           || GET_CODE (SET_SRC (elt)) != REG
8479           || GET_MODE (SET_SRC (elt)) != SImode
8480           || REGNO (SET_SRC (elt)) != src_regno + i
8481           || GET_CODE (SET_DEST (elt)) != MEM
8482           || GET_MODE (SET_DEST (elt)) != SImode)
8483         return 0;
8484       newaddr = XEXP (SET_DEST (elt), 0);
8485       if (legitimate_indirect_address_p (newaddr, 0))
8486         {
8487           newoffset = 0;
8488           addr_reg = newaddr;
8489         }
8490       else if (legitimate_offset_address_p (SImode, newaddr, 0))
8491         {
8492           addr_reg = XEXP (newaddr, 0);
8493           newoffset = INTVAL (XEXP (newaddr, 1));
8494         }
8495       else
8496         return 0;
8497       if (REGNO (addr_reg) != base_regno
8498           || newoffset != offset + 4 * i)
8499         return 0;
8500     }
8501
8502   return 1;
8503 }
8504 \f
8505 /* A validation routine: say whether CODE, a condition code, and MODE
8506    match.  The other alternatives either don't make sense or should
8507    never be generated.  */
8508
8509 static void
8510 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
8511 {
8512   if ((GET_RTX_CLASS (code) != RTX_COMPARE
8513        && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
8514       || GET_MODE_CLASS (mode) != MODE_CC)
8515     abort ();
8516
8517   /* These don't make sense.  */
8518   if ((code == GT || code == LT || code == GE || code == LE)
8519       && mode == CCUNSmode)
8520     abort ();
8521
8522   if ((code == GTU || code == LTU || code == GEU || code == LEU)
8523       && mode != CCUNSmode)
8524     abort ();
8525
8526   if (mode != CCFPmode
8527       && (code == ORDERED || code == UNORDERED
8528           || code == UNEQ || code == LTGT
8529           || code == UNGT || code == UNLT
8530           || code == UNGE || code == UNLE))
8531     abort ();
8532   
8533   /* These should never be generated except for 
8534      flag_finite_math_only.  */
8535   if (mode == CCFPmode
8536       && ! flag_finite_math_only
8537       && (code == LE || code == GE
8538           || code == UNEQ || code == LTGT
8539           || code == UNGT || code == UNLT))
8540     abort ();
8541
8542   /* These are invalid; the information is not there.  */
8543   if (mode == CCEQmode 
8544       && code != EQ && code != NE)
8545     abort ();
8546 }
8547
8548 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
8549    We only check the opcode against the mode of the CC value here.  */
8550
8551 int
8552 branch_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8553 {
8554   enum rtx_code code = GET_CODE (op);
8555   enum machine_mode cc_mode;
8556
8557   if (!COMPARISON_P (op))
8558     return 0;
8559
8560   cc_mode = GET_MODE (XEXP (op, 0));
8561   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
8562     return 0;
8563
8564   validate_condition_mode (code, cc_mode);
8565
8566   return 1;
8567 }
8568
8569 /* Return 1 if OP is a comparison operation that is valid for a branch
8570    insn and which is true if the corresponding bit in the CC register
8571    is set.  */
8572
8573 int
8574 branch_positive_comparison_operator (rtx op, enum machine_mode mode)
8575 {
8576   enum rtx_code code;
8577
8578   if (! branch_comparison_operator (op, mode))
8579     return 0;
8580
8581   code = GET_CODE (op);
8582   return (code == EQ || code == LT || code == GT
8583           || code == LTU || code == GTU
8584           || code == UNORDERED);
8585 }
8586
8587 /* Return 1 if OP is a comparison operation that is valid for an scc
8588    insn: it must be a positive comparison.  */
8589
8590 int
8591 scc_comparison_operator (rtx op, enum machine_mode mode)
8592 {
8593   return branch_positive_comparison_operator (op, mode);
8594 }
8595
8596 int
8597 trap_comparison_operator (rtx op, enum machine_mode mode)
8598 {
8599   if (mode != VOIDmode && mode != GET_MODE (op))
8600     return 0;
8601   return COMPARISON_P (op);
8602 }
8603
8604 int
8605 boolean_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8606 {
8607   enum rtx_code code = GET_CODE (op);
8608   return (code == AND || code == IOR || code == XOR);
8609 }
8610
8611 int
8612 boolean_or_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8613 {
8614   enum rtx_code code = GET_CODE (op);
8615   return (code == IOR || code == XOR);
8616 }
8617
8618 int
8619 min_max_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
8620 {
8621   enum rtx_code code = GET_CODE (op);
8622   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
8623 }
8624 \f
8625 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
8626    mask required to convert the result of a rotate insn into a shift
8627    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
8628
8629 int
8630 includes_lshift_p (rtx shiftop, rtx andop)
8631 {
8632   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
8633
8634   shift_mask <<= INTVAL (shiftop);
8635
8636   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
8637 }
8638
8639 /* Similar, but for right shift.  */
8640
8641 int
8642 includes_rshift_p (rtx shiftop, rtx andop)
8643 {
8644   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
8645
8646   shift_mask >>= INTVAL (shiftop);
8647
8648   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
8649 }
8650
8651 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
8652    to perform a left shift.  It must have exactly SHIFTOP least
8653    significant 0's, then one or more 1's, then zero or more 0's.  */
8654
8655 int
8656 includes_rldic_lshift_p (rtx shiftop, rtx andop)
8657 {
8658   if (GET_CODE (andop) == CONST_INT)
8659     {
8660       HOST_WIDE_INT c, lsb, shift_mask;
8661
8662       c = INTVAL (andop);
8663       if (c == 0 || c == ~0)
8664         return 0;
8665
8666       shift_mask = ~0;
8667       shift_mask <<= INTVAL (shiftop);
8668
8669       /* Find the least significant one bit.  */
8670       lsb = c & -c;
8671
8672       /* It must coincide with the LSB of the shift mask.  */
8673       if (-lsb != shift_mask)
8674         return 0;
8675
8676       /* Invert to look for the next transition (if any).  */
8677       c = ~c;
8678
8679       /* Remove the low group of ones (originally low group of zeros).  */
8680       c &= -lsb;
8681
8682       /* Again find the lsb, and check we have all 1's above.  */
8683       lsb = c & -c;
8684       return c == -lsb;
8685     }
8686   else if (GET_CODE (andop) == CONST_DOUBLE
8687            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8688     {
8689       HOST_WIDE_INT low, high, lsb;
8690       HOST_WIDE_INT shift_mask_low, shift_mask_high;
8691
8692       low = CONST_DOUBLE_LOW (andop);
8693       if (HOST_BITS_PER_WIDE_INT < 64)
8694         high = CONST_DOUBLE_HIGH (andop);
8695
8696       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
8697           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
8698         return 0;
8699
8700       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8701         {
8702           shift_mask_high = ~0;
8703           if (INTVAL (shiftop) > 32)
8704             shift_mask_high <<= INTVAL (shiftop) - 32;
8705
8706           lsb = high & -high;
8707
8708           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
8709             return 0;
8710
8711           high = ~high;
8712           high &= -lsb;
8713
8714           lsb = high & -high;
8715           return high == -lsb;
8716         }
8717
8718       shift_mask_low = ~0;
8719       shift_mask_low <<= INTVAL (shiftop);
8720
8721       lsb = low & -low;
8722
8723       if (-lsb != shift_mask_low)
8724         return 0;
8725
8726       if (HOST_BITS_PER_WIDE_INT < 64)
8727         high = ~high;
8728       low = ~low;
8729       low &= -lsb;
8730
8731       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8732         {
8733           lsb = high & -high;
8734           return high == -lsb;
8735         }
8736
8737       lsb = low & -low;
8738       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
8739     }
8740   else
8741     return 0;
8742 }
8743
8744 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
8745    to perform a left shift.  It must have SHIFTOP or more least
8746    significant 0's, with the remainder of the word 1's.  */
8747
8748 int
8749 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
8750 {
8751   if (GET_CODE (andop) == CONST_INT)
8752     {
8753       HOST_WIDE_INT c, lsb, shift_mask;
8754
8755       shift_mask = ~0;
8756       shift_mask <<= INTVAL (shiftop);
8757       c = INTVAL (andop);
8758
8759       /* Find the least significant one bit.  */
8760       lsb = c & -c;
8761
8762       /* It must be covered by the shift mask.
8763          This test also rejects c == 0.  */
8764       if ((lsb & shift_mask) == 0)
8765         return 0;
8766
8767       /* Check we have all 1's above the transition, and reject all 1's.  */
8768       return c == -lsb && lsb != 1;
8769     }
8770   else if (GET_CODE (andop) == CONST_DOUBLE
8771            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8772     {
8773       HOST_WIDE_INT low, lsb, shift_mask_low;
8774
8775       low = CONST_DOUBLE_LOW (andop);
8776
8777       if (HOST_BITS_PER_WIDE_INT < 64)
8778         {
8779           HOST_WIDE_INT high, shift_mask_high;
8780
8781           high = CONST_DOUBLE_HIGH (andop);
8782
8783           if (low == 0)
8784             {
8785               shift_mask_high = ~0;
8786               if (INTVAL (shiftop) > 32)
8787                 shift_mask_high <<= INTVAL (shiftop) - 32;
8788
8789               lsb = high & -high;
8790
8791               if ((lsb & shift_mask_high) == 0)
8792                 return 0;
8793
8794               return high == -lsb;
8795             }
8796           if (high != ~0)
8797             return 0;
8798         }
8799
8800       shift_mask_low = ~0;
8801       shift_mask_low <<= INTVAL (shiftop);
8802
8803       lsb = low & -low;
8804
8805       if ((lsb & shift_mask_low) == 0)
8806         return 0;
8807
8808       return low == -lsb && lsb != 1;
8809     }
8810   else
8811     return 0;
8812 }
8813
8814 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
8815    for lfq and stfq insns iff the registers are hard registers.   */
8816
8817 int
8818 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
8819 {
8820   /* We might have been passed a SUBREG.  */
8821   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
8822     return 0;
8823     
8824   /* We might have been passed non floating point registers.  */
8825   if (!FP_REGNO_P (REGNO (reg1))
8826       || !FP_REGNO_P (REGNO (reg2)))
8827     return 0;
8828
8829   return (REGNO (reg1) == REGNO (reg2) - 1);
8830 }
8831
8832 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
8833    addr1 and addr2 must be in consecutive memory locations
8834    (addr2 == addr1 + 8).  */
8835
8836 int
8837 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
8838 {
8839   rtx addr1, addr2;
8840   unsigned int reg1;
8841   int offset1;
8842
8843   /* The mems cannot be volatile.  */
8844   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
8845     return 0;
8846   
8847   addr1 = XEXP (mem1, 0);
8848   addr2 = XEXP (mem2, 0);
8849
8850   /* Extract an offset (if used) from the first addr.  */
8851   if (GET_CODE (addr1) == PLUS)
8852     {
8853       /* If not a REG, return zero.  */
8854       if (GET_CODE (XEXP (addr1, 0)) != REG)
8855         return 0;
8856       else
8857         {
8858           reg1 = REGNO (XEXP (addr1, 0));
8859           /* The offset must be constant!  */
8860           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
8861             return 0;
8862           offset1 = INTVAL (XEXP (addr1, 1));
8863         }
8864     }
8865   else if (GET_CODE (addr1) != REG)
8866     return 0;
8867   else
8868     {
8869       reg1 = REGNO (addr1);
8870       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
8871       offset1 = 0;
8872     }
8873
8874   /* Make sure the second address is a (mem (plus (reg) (const_int)))
8875      or if it is (mem (reg)) then make sure that offset1 is -8 and the same 
8876      register as addr1.  */
8877   if (offset1 == -8 && GET_CODE (addr2) == REG && reg1 == REGNO (addr2))
8878     return 1;
8879   if (GET_CODE (addr2) != PLUS)
8880     return 0;
8881
8882   if (GET_CODE (XEXP (addr2, 0)) != REG
8883       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
8884     return 0;
8885
8886   if (reg1 != REGNO (XEXP (addr2, 0)))
8887     return 0;
8888
8889   /* The offset for the second addr must be 8 more than the first addr.  */
8890   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
8891     return 0;
8892
8893   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
8894      instructions.  */
8895   return 1;
8896 }
8897 \f
8898 /* Return the register class of a scratch register needed to copy IN into
8899    or out of a register in CLASS in MODE.  If it can be done directly,
8900    NO_REGS is returned.  */
8901
8902 enum reg_class
8903 secondary_reload_class (enum reg_class class, 
8904                         enum machine_mode mode ATTRIBUTE_UNUSED,
8905                         rtx in)
8906 {
8907   int regno;
8908
8909   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
8910 #if TARGET_MACHO
8911                     && MACHOPIC_INDIRECT
8912 #endif
8913                     ))
8914     {
8915       /* We cannot copy a symbolic operand directly into anything
8916          other than BASE_REGS for TARGET_ELF.  So indicate that a
8917          register from BASE_REGS is needed as an intermediate
8918          register.
8919          
8920          On Darwin, pic addresses require a load from memory, which
8921          needs a base register.  */
8922       if (class != BASE_REGS
8923           && (GET_CODE (in) == SYMBOL_REF
8924               || GET_CODE (in) == HIGH
8925               || GET_CODE (in) == LABEL_REF
8926               || GET_CODE (in) == CONST))
8927         return BASE_REGS;
8928     }
8929
8930   if (GET_CODE (in) == REG)
8931     {
8932       regno = REGNO (in);
8933       if (regno >= FIRST_PSEUDO_REGISTER)
8934         {
8935           regno = true_regnum (in);
8936           if (regno >= FIRST_PSEUDO_REGISTER)
8937             regno = -1;
8938         }
8939     }
8940   else if (GET_CODE (in) == SUBREG)
8941     {
8942       regno = true_regnum (in);
8943       if (regno >= FIRST_PSEUDO_REGISTER)
8944         regno = -1;
8945     }
8946   else
8947     regno = -1;
8948
8949   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
8950      into anything.  */
8951   if (class == GENERAL_REGS || class == BASE_REGS
8952       || (regno >= 0 && INT_REGNO_P (regno)))
8953     return NO_REGS;
8954
8955   /* Constants, memory, and FP registers can go into FP registers.  */
8956   if ((regno == -1 || FP_REGNO_P (regno))
8957       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
8958     return NO_REGS;
8959
8960   /* Memory, and AltiVec registers can go into AltiVec registers.  */
8961   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
8962       && class == ALTIVEC_REGS)
8963     return NO_REGS;
8964
8965   /* We can copy among the CR registers.  */
8966   if ((class == CR_REGS || class == CR0_REGS)
8967       && regno >= 0 && CR_REGNO_P (regno))
8968     return NO_REGS;
8969
8970   /* Otherwise, we need GENERAL_REGS.  */
8971   return GENERAL_REGS;
8972 }
8973 \f
8974 /* Given a comparison operation, return the bit number in CCR to test.  We
8975    know this is a valid comparison.  
8976
8977    SCC_P is 1 if this is for an scc.  That means that %D will have been
8978    used instead of %C, so the bits will be in different places.
8979
8980    Return -1 if OP isn't a valid comparison for some reason.  */
8981
8982 int
8983 ccr_bit (rtx op, int scc_p)
8984 {
8985   enum rtx_code code = GET_CODE (op);
8986   enum machine_mode cc_mode;
8987   int cc_regnum;
8988   int base_bit;
8989   rtx reg;
8990
8991   if (!COMPARISON_P (op))
8992     return -1;
8993
8994   reg = XEXP (op, 0);
8995
8996   if (GET_CODE (reg) != REG
8997       || ! CR_REGNO_P (REGNO (reg)))
8998     abort ();
8999
9000   cc_mode = GET_MODE (reg);
9001   cc_regnum = REGNO (reg);
9002   base_bit = 4 * (cc_regnum - CR0_REGNO);
9003
9004   validate_condition_mode (code, cc_mode);
9005
9006   /* When generating a sCOND operation, only positive conditions are
9007      allowed.  */
9008   if (scc_p && code != EQ && code != GT && code != LT && code != UNORDERED
9009       && code != GTU && code != LTU)
9010     abort ();
9011   
9012   switch (code)
9013     {
9014     case NE:
9015       return scc_p ? base_bit + 3 : base_bit + 2;
9016     case EQ:
9017       return base_bit + 2;
9018     case GT:  case GTU:  case UNLE:
9019       return base_bit + 1;
9020     case LT:  case LTU:  case UNGE:
9021       return base_bit;
9022     case ORDERED:  case UNORDERED:
9023       return base_bit + 3;
9024
9025     case GE:  case GEU:
9026       /* If scc, we will have done a cror to put the bit in the
9027          unordered position.  So test that bit.  For integer, this is ! LT
9028          unless this is an scc insn.  */
9029       return scc_p ? base_bit + 3 : base_bit;
9030
9031     case LE:  case LEU:
9032       return scc_p ? base_bit + 3 : base_bit + 1;
9033
9034     default:
9035       abort ();
9036     }
9037 }
9038 \f
9039 /* Return the GOT register.  */
9040
9041 struct rtx_def *
9042 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
9043 {
9044   /* The second flow pass currently (June 1999) can't update
9045      regs_ever_live without disturbing other parts of the compiler, so
9046      update it here to make the prolog/epilogue code happy.  */
9047   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
9048     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9049
9050   current_function_uses_pic_offset_table = 1;
9051
9052   return pic_offset_table_rtx;
9053 }
9054 \f
9055 /* Function to init struct machine_function.
9056    This will be called, via a pointer variable,
9057    from push_function_context.  */
9058
9059 static struct machine_function *
9060 rs6000_init_machine_status (void)
9061 {
9062   return ggc_alloc_cleared (sizeof (machine_function));
9063 }
9064 \f
9065 /* These macros test for integers and extract the low-order bits.  */
9066 #define INT_P(X)  \
9067 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
9068  && GET_MODE (X) == VOIDmode)
9069
9070 #define INT_LOWPART(X) \
9071   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
9072
9073 int
9074 extract_MB (rtx op)
9075 {
9076   int i;
9077   unsigned long val = INT_LOWPART (op);
9078
9079   /* If the high bit is zero, the value is the first 1 bit we find
9080      from the left.  */
9081   if ((val & 0x80000000) == 0)
9082     {
9083       if ((val & 0xffffffff) == 0)
9084         abort ();
9085
9086       i = 1;
9087       while (((val <<= 1) & 0x80000000) == 0)
9088         ++i;
9089       return i;
9090     }
9091
9092   /* If the high bit is set and the low bit is not, or the mask is all
9093      1's, the value is zero.  */
9094   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
9095     return 0;
9096
9097   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
9098      from the right.  */
9099   i = 31;
9100   while (((val >>= 1) & 1) != 0)
9101     --i;
9102
9103   return i;
9104 }
9105
9106 int
9107 extract_ME (rtx op)
9108 {
9109   int i;
9110   unsigned long val = INT_LOWPART (op);
9111
9112   /* If the low bit is zero, the value is the first 1 bit we find from
9113      the right.  */
9114   if ((val & 1) == 0)
9115     {
9116       if ((val & 0xffffffff) == 0)
9117         abort ();
9118
9119       i = 30;
9120       while (((val >>= 1) & 1) == 0)
9121         --i;
9122
9123       return i;
9124     }
9125
9126   /* If the low bit is set and the high bit is not, or the mask is all
9127      1's, the value is 31.  */
9128   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
9129     return 31;
9130
9131   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
9132      from the left.  */
9133   i = 0;
9134   while (((val <<= 1) & 0x80000000) != 0)
9135     ++i;
9136
9137   return i;
9138 }
9139
9140 /* Locate some local-dynamic symbol still in use by this function
9141    so that we can print its name in some tls_ld pattern.  */
9142
9143 static const char *
9144 rs6000_get_some_local_dynamic_name (void)
9145 {
9146   rtx insn;
9147
9148   if (cfun->machine->some_ld_name)
9149     return cfun->machine->some_ld_name;
9150
9151   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
9152     if (INSN_P (insn)
9153         && for_each_rtx (&PATTERN (insn),
9154                          rs6000_get_some_local_dynamic_name_1, 0))
9155       return cfun->machine->some_ld_name;
9156
9157   abort ();
9158 }
9159
9160 /* Helper function for rs6000_get_some_local_dynamic_name.  */
9161
9162 static int
9163 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
9164 {
9165   rtx x = *px;
9166
9167   if (GET_CODE (x) == SYMBOL_REF)
9168     {
9169       const char *str = XSTR (x, 0);
9170       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
9171         {
9172           cfun->machine->some_ld_name = str;
9173           return 1;
9174         }
9175     }
9176
9177   return 0;
9178 }
9179
9180 /* Print an operand.  Recognize special options, documented below.  */
9181
9182 #if TARGET_ELF
9183 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
9184 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
9185 #else
9186 #define SMALL_DATA_RELOC "sda21"
9187 #define SMALL_DATA_REG 0
9188 #endif
9189
9190 void
9191 print_operand (FILE *file, rtx x, int code)
9192 {
9193   int i;
9194   HOST_WIDE_INT val;
9195   unsigned HOST_WIDE_INT uval;
9196
9197   switch (code)
9198     {
9199     case '.':
9200       /* Write out an instruction after the call which may be replaced
9201          with glue code by the loader.  This depends on the AIX version.  */
9202       asm_fprintf (file, RS6000_CALL_GLUE);
9203       return;
9204
9205       /* %a is output_address.  */
9206
9207     case 'A':
9208       /* If X is a constant integer whose low-order 5 bits are zero,
9209          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
9210          in the AIX assembler where "sri" with a zero shift count
9211          writes a trash instruction.  */
9212       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
9213         putc ('l', file);
9214       else
9215         putc ('r', file);
9216       return;
9217
9218     case 'b':
9219       /* If constant, low-order 16 bits of constant, unsigned.
9220          Otherwise, write normally.  */
9221       if (INT_P (x))
9222         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
9223       else
9224         print_operand (file, x, 0);
9225       return;
9226
9227     case 'B':
9228       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
9229          for 64-bit mask direction.  */
9230       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
9231       return;
9232
9233       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
9234          output_operand.  */
9235
9236     case 'c':
9237       /* X is a CR register.  Print the number of the GT bit of the CR.  */
9238       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9239         output_operand_lossage ("invalid %%E value");
9240       else
9241         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 1);
9242       return;
9243
9244     case 'D':
9245       /* Like 'J' but get to the GT bit.  */
9246       if (GET_CODE (x) != REG)
9247         abort ();
9248
9249       /* Bit 1 is GT bit.  */
9250       i = 4 * (REGNO (x) - CR0_REGNO) + 1;
9251
9252       /* If we want bit 31, write a shift count of zero, not 32.  */
9253       fprintf (file, "%d", i == 31 ? 0 : i + 1);
9254       return;
9255
9256     case 'E':
9257       /* X is a CR register.  Print the number of the EQ bit of the CR */
9258       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9259         output_operand_lossage ("invalid %%E value");
9260       else
9261         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
9262       return;
9263
9264     case 'f':
9265       /* X is a CR register.  Print the shift count needed to move it
9266          to the high-order four bits.  */
9267       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9268         output_operand_lossage ("invalid %%f value");
9269       else
9270         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
9271       return;
9272
9273     case 'F':
9274       /* Similar, but print the count for the rotate in the opposite
9275          direction.  */
9276       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9277         output_operand_lossage ("invalid %%F value");
9278       else
9279         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
9280       return;
9281
9282     case 'G':
9283       /* X is a constant integer.  If it is negative, print "m",
9284          otherwise print "z".  This is to make an aze or ame insn.  */
9285       if (GET_CODE (x) != CONST_INT)
9286         output_operand_lossage ("invalid %%G value");
9287       else if (INTVAL (x) >= 0)
9288         putc ('z', file);
9289       else
9290         putc ('m', file);
9291       return;
9292
9293     case 'h':
9294       /* If constant, output low-order five bits.  Otherwise, write
9295          normally.  */
9296       if (INT_P (x))
9297         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
9298       else
9299         print_operand (file, x, 0);
9300       return;
9301
9302     case 'H':
9303       /* If constant, output low-order six bits.  Otherwise, write
9304          normally.  */
9305       if (INT_P (x))
9306         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
9307       else
9308         print_operand (file, x, 0);
9309       return;
9310
9311     case 'I':
9312       /* Print `i' if this is a constant, else nothing.  */
9313       if (INT_P (x))
9314         putc ('i', file);
9315       return;
9316
9317     case 'j':
9318       /* Write the bit number in CCR for jump.  */
9319       i = ccr_bit (x, 0);
9320       if (i == -1)
9321         output_operand_lossage ("invalid %%j code");
9322       else
9323         fprintf (file, "%d", i);
9324       return;
9325
9326     case 'J':
9327       /* Similar, but add one for shift count in rlinm for scc and pass
9328          scc flag to `ccr_bit'.  */
9329       i = ccr_bit (x, 1);
9330       if (i == -1)
9331         output_operand_lossage ("invalid %%J code");
9332       else
9333         /* If we want bit 31, write a shift count of zero, not 32.  */
9334         fprintf (file, "%d", i == 31 ? 0 : i + 1);
9335       return;
9336
9337     case 'k':
9338       /* X must be a constant.  Write the 1's complement of the
9339          constant.  */
9340       if (! INT_P (x))
9341         output_operand_lossage ("invalid %%k value");
9342       else
9343         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
9344       return;
9345
9346     case 'K':
9347       /* X must be a symbolic constant on ELF.  Write an
9348          expression suitable for an 'addi' that adds in the low 16
9349          bits of the MEM.  */
9350       if (GET_CODE (x) != CONST)
9351         {
9352           print_operand_address (file, x);
9353           fputs ("@l", file);
9354         }
9355       else
9356         {
9357           if (GET_CODE (XEXP (x, 0)) != PLUS
9358               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
9359                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
9360               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
9361             output_operand_lossage ("invalid %%K value");
9362           print_operand_address (file, XEXP (XEXP (x, 0), 0));
9363           fputs ("@l", file);
9364           /* For GNU as, there must be a non-alphanumeric character
9365              between 'l' and the number.  The '-' is added by
9366              print_operand() already.  */
9367           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
9368             fputs ("+", file);
9369           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
9370         }
9371       return;
9372
9373       /* %l is output_asm_label.  */
9374
9375     case 'L':
9376       /* Write second word of DImode or DFmode reference.  Works on register
9377          or non-indexed memory only.  */
9378       if (GET_CODE (x) == REG)
9379         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
9380       else if (GET_CODE (x) == MEM)
9381         {
9382           /* Handle possible auto-increment.  Since it is pre-increment and
9383              we have already done it, we can just use an offset of word.  */
9384           if (GET_CODE (XEXP (x, 0)) == PRE_INC
9385               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
9386             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
9387                                            UNITS_PER_WORD));
9388           else
9389             output_address (XEXP (adjust_address_nv (x, SImode,
9390                                                      UNITS_PER_WORD),
9391                                   0));
9392
9393           if (small_data_operand (x, GET_MODE (x)))
9394             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9395                      reg_names[SMALL_DATA_REG]);
9396         }
9397       return;
9398                             
9399     case 'm':
9400       /* MB value for a mask operand.  */
9401       if (! mask_operand (x, SImode))
9402         output_operand_lossage ("invalid %%m value");
9403
9404       fprintf (file, "%d", extract_MB (x));
9405       return;
9406
9407     case 'M':
9408       /* ME value for a mask operand.  */
9409       if (! mask_operand (x, SImode))
9410         output_operand_lossage ("invalid %%M value");
9411
9412       fprintf (file, "%d", extract_ME (x));
9413       return;
9414
9415       /* %n outputs the negative of its operand.  */
9416
9417     case 'N':
9418       /* Write the number of elements in the vector times 4.  */
9419       if (GET_CODE (x) != PARALLEL)
9420         output_operand_lossage ("invalid %%N value");
9421       else
9422         fprintf (file, "%d", XVECLEN (x, 0) * 4);
9423       return;
9424
9425     case 'O':
9426       /* Similar, but subtract 1 first.  */
9427       if (GET_CODE (x) != PARALLEL)
9428         output_operand_lossage ("invalid %%O value");
9429       else
9430         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
9431       return;
9432
9433     case 'p':
9434       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
9435       if (! INT_P (x)
9436           || INT_LOWPART (x) < 0
9437           || (i = exact_log2 (INT_LOWPART (x))) < 0)
9438         output_operand_lossage ("invalid %%p value");
9439       else
9440         fprintf (file, "%d", i);
9441       return;
9442
9443     case 'P':
9444       /* The operand must be an indirect memory reference.  The result
9445          is the register name.  */
9446       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
9447           || REGNO (XEXP (x, 0)) >= 32)
9448         output_operand_lossage ("invalid %%P value");
9449       else
9450         fprintf (file, "%s", reg_names[REGNO (XEXP (x, 0))]);
9451       return;
9452
9453     case 'q':
9454       /* This outputs the logical code corresponding to a boolean
9455          expression.  The expression may have one or both operands
9456          negated (if one, only the first one).  For condition register
9457          logical operations, it will also treat the negated
9458          CR codes as NOTs, but not handle NOTs of them.  */
9459       {
9460         const char *const *t = 0;
9461         const char *s;
9462         enum rtx_code code = GET_CODE (x);
9463         static const char * const tbl[3][3] = {
9464           { "and", "andc", "nor" },
9465           { "or", "orc", "nand" },
9466           { "xor", "eqv", "xor" } };
9467
9468         if (code == AND)
9469           t = tbl[0];
9470         else if (code == IOR)
9471           t = tbl[1];
9472         else if (code == XOR)
9473           t = tbl[2];
9474         else
9475           output_operand_lossage ("invalid %%q value");
9476
9477         if (GET_CODE (XEXP (x, 0)) != NOT)
9478           s = t[0];
9479         else
9480           {
9481             if (GET_CODE (XEXP (x, 1)) == NOT)
9482               s = t[2];
9483             else
9484               s = t[1];
9485           }
9486         
9487         fputs (s, file);
9488       }
9489       return;
9490
9491     case 'Q':
9492       if (TARGET_MFCRF)
9493         fputc (',', file);
9494         /* FALLTHRU */
9495       else
9496         return;
9497
9498     case 'R':
9499       /* X is a CR register.  Print the mask for `mtcrf'.  */
9500       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9501         output_operand_lossage ("invalid %%R value");
9502       else
9503         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
9504       return;
9505
9506     case 's':
9507       /* Low 5 bits of 32 - value */
9508       if (! INT_P (x))
9509         output_operand_lossage ("invalid %%s value");
9510       else
9511         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
9512       return;
9513
9514     case 'S':
9515       /* PowerPC64 mask position.  All 0's is excluded.
9516          CONST_INT 32-bit mask is considered sign-extended so any
9517          transition must occur within the CONST_INT, not on the boundary.  */
9518       if (! mask64_operand (x, DImode))
9519         output_operand_lossage ("invalid %%S value");
9520
9521       uval = INT_LOWPART (x);
9522
9523       if (uval & 1)     /* Clear Left */
9524         {
9525 #if HOST_BITS_PER_WIDE_INT > 64
9526           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
9527 #endif
9528           i = 64;
9529         }
9530       else              /* Clear Right */
9531         {
9532           uval = ~uval;
9533 #if HOST_BITS_PER_WIDE_INT > 64
9534           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
9535 #endif
9536           i = 63;
9537         }
9538       while (uval != 0)
9539         --i, uval >>= 1;
9540       if (i < 0)
9541         abort ();
9542       fprintf (file, "%d", i);
9543       return;
9544
9545     case 't':
9546       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
9547       if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
9548         abort ();
9549
9550       /* Bit 3 is OV bit.  */
9551       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
9552
9553       /* If we want bit 31, write a shift count of zero, not 32.  */
9554       fprintf (file, "%d", i == 31 ? 0 : i + 1);
9555       return;
9556
9557     case 'T':
9558       /* Print the symbolic name of a branch target register.  */
9559       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
9560                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
9561         output_operand_lossage ("invalid %%T value");
9562       else if (REGNO (x) == LINK_REGISTER_REGNUM)
9563         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
9564       else
9565         fputs ("ctr", file);
9566       return;
9567
9568     case 'u':
9569       /* High-order 16 bits of constant for use in unsigned operand.  */
9570       if (! INT_P (x))
9571         output_operand_lossage ("invalid %%u value");
9572       else
9573         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
9574                  (INT_LOWPART (x) >> 16) & 0xffff);
9575       return;
9576
9577     case 'v':
9578       /* High-order 16 bits of constant for use in signed operand.  */
9579       if (! INT_P (x))
9580         output_operand_lossage ("invalid %%v value");
9581       else
9582         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
9583                  (INT_LOWPART (x) >> 16) & 0xffff);
9584       return;
9585
9586     case 'U':
9587       /* Print `u' if this has an auto-increment or auto-decrement.  */
9588       if (GET_CODE (x) == MEM
9589           && (GET_CODE (XEXP (x, 0)) == PRE_INC
9590               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
9591         putc ('u', file);
9592       return;
9593
9594     case 'V':
9595       /* Print the trap code for this operand.  */
9596       switch (GET_CODE (x))
9597         {
9598         case EQ:
9599           fputs ("eq", file);   /* 4 */
9600           break;
9601         case NE:
9602           fputs ("ne", file);   /* 24 */
9603           break;
9604         case LT:
9605           fputs ("lt", file);   /* 16 */
9606           break;
9607         case LE:
9608           fputs ("le", file);   /* 20 */
9609           break;
9610         case GT:
9611           fputs ("gt", file);   /* 8 */
9612           break;
9613         case GE:
9614           fputs ("ge", file);   /* 12 */
9615           break;
9616         case LTU:
9617           fputs ("llt", file);  /* 2 */
9618           break;
9619         case LEU:
9620           fputs ("lle", file);  /* 6 */
9621           break;
9622         case GTU:
9623           fputs ("lgt", file);  /* 1 */
9624           break;
9625         case GEU:
9626           fputs ("lge", file);  /* 5 */
9627           break;
9628         default:
9629           abort ();
9630         }
9631       break;
9632
9633     case 'w':
9634       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
9635          normally.  */
9636       if (INT_P (x))
9637         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
9638                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
9639       else
9640         print_operand (file, x, 0);
9641       return;
9642
9643     case 'W':
9644       /* MB value for a PowerPC64 rldic operand.  */
9645       val = (GET_CODE (x) == CONST_INT
9646              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
9647
9648       if (val < 0)
9649         i = -1;
9650       else
9651         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
9652           if ((val <<= 1) < 0)
9653             break;
9654
9655 #if HOST_BITS_PER_WIDE_INT == 32
9656       if (GET_CODE (x) == CONST_INT && i >= 0)
9657         i += 32;  /* zero-extend high-part was all 0's */
9658       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
9659         {
9660           val = CONST_DOUBLE_LOW (x);
9661
9662           if (val == 0)
9663             abort ();
9664           else if (val < 0)
9665             --i;
9666           else
9667             for ( ; i < 64; i++)
9668               if ((val <<= 1) < 0)
9669                 break;
9670         }
9671 #endif
9672
9673       fprintf (file, "%d", i + 1);
9674       return;
9675
9676     case 'X':
9677       if (GET_CODE (x) == MEM
9678           && legitimate_indexed_address_p (XEXP (x, 0), 0))
9679         putc ('x', file);
9680       return;
9681
9682     case 'Y':
9683       /* Like 'L', for third word of TImode  */
9684       if (GET_CODE (x) == REG)
9685         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
9686       else if (GET_CODE (x) == MEM)
9687         {
9688           if (GET_CODE (XEXP (x, 0)) == PRE_INC
9689               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
9690             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
9691           else
9692             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
9693           if (small_data_operand (x, GET_MODE (x)))
9694             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9695                      reg_names[SMALL_DATA_REG]);
9696         }
9697       return;
9698                             
9699     case 'z':
9700       /* X is a SYMBOL_REF.  Write out the name preceded by a
9701          period and without any trailing data in brackets.  Used for function
9702          names.  If we are configured for System V (or the embedded ABI) on
9703          the PowerPC, do not emit the period, since those systems do not use
9704          TOCs and the like.  */
9705       if (GET_CODE (x) != SYMBOL_REF)
9706         abort ();
9707
9708       /* Mark the decl as referenced so that cgraph will output the function.  */
9709       if (SYMBOL_REF_DECL (x))
9710         mark_decl_referenced (SYMBOL_REF_DECL (x));
9711
9712       if (XSTR (x, 0)[0] != '.')
9713         {
9714           switch (DEFAULT_ABI)
9715             {
9716             default:
9717               abort ();
9718
9719             case ABI_AIX:
9720               putc ('.', file);
9721               break;
9722
9723             case ABI_V4:
9724             case ABI_DARWIN:
9725               break;
9726             }
9727         }
9728       /* For macho, we need to check it see if we need a stub.  */
9729       if (TARGET_MACHO)
9730         {
9731           const char *name = XSTR (x, 0);
9732 #if TARGET_MACHO
9733           if (MACHOPIC_INDIRECT
9734               && machopic_classify_name (name) == MACHOPIC_UNDEFINED_FUNCTION)
9735             name = machopic_stub_name (name);
9736 #endif
9737           assemble_name (file, name);
9738         }
9739      else if (TARGET_AIX)
9740         RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
9741       else
9742         assemble_name (file, XSTR (x, 0));
9743       return;
9744
9745     case 'Z':
9746       /* Like 'L', for last word of TImode.  */
9747       if (GET_CODE (x) == REG)
9748         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
9749       else if (GET_CODE (x) == MEM)
9750         {
9751           if (GET_CODE (XEXP (x, 0)) == PRE_INC
9752               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
9753             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
9754           else
9755             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
9756           if (small_data_operand (x, GET_MODE (x)))
9757             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9758                      reg_names[SMALL_DATA_REG]);
9759         }
9760       return;
9761
9762       /* Print AltiVec or SPE memory operand.  */
9763     case 'y':
9764       {
9765         rtx tmp;
9766
9767         if (GET_CODE (x) != MEM)
9768           abort ();
9769
9770         tmp = XEXP (x, 0);
9771
9772         if (TARGET_E500)
9773           {
9774             /* Handle [reg].  */
9775             if (GET_CODE (tmp) == REG)
9776               {
9777                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
9778                 break;
9779               }
9780             /* Handle [reg+UIMM].  */
9781             else if (GET_CODE (tmp) == PLUS &&
9782                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
9783               {
9784                 int x;
9785
9786                 if (GET_CODE (XEXP (tmp, 0)) != REG)
9787                   abort ();
9788
9789                 x = INTVAL (XEXP (tmp, 1));
9790                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
9791                 break;
9792               }
9793
9794             /* Fall through.  Must be [reg+reg].  */
9795           }
9796         if (GET_CODE (tmp) == REG)
9797           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
9798         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
9799           {
9800             if (REGNO (XEXP (tmp, 0)) == 0)
9801               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
9802                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
9803             else
9804               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
9805                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
9806           }
9807         else
9808           abort ();
9809         break;
9810       }
9811                             
9812     case 0:
9813       if (GET_CODE (x) == REG)
9814         fprintf (file, "%s", reg_names[REGNO (x)]);
9815       else if (GET_CODE (x) == MEM)
9816         {
9817           /* We need to handle PRE_INC and PRE_DEC here, since we need to
9818              know the width from the mode.  */
9819           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
9820             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
9821                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9822           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
9823             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
9824                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9825           else
9826             output_address (XEXP (x, 0));
9827         }
9828       else
9829         output_addr_const (file, x);
9830       return;
9831
9832     case '&':
9833       assemble_name (file, rs6000_get_some_local_dynamic_name ());
9834       return;
9835
9836     default:
9837       output_operand_lossage ("invalid %%xn code");
9838     }
9839 }
9840 \f
9841 /* Print the address of an operand.  */
9842
9843 void
9844 print_operand_address (FILE *file, rtx x)
9845 {
9846   if (GET_CODE (x) == REG)
9847     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
9848   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
9849            || GET_CODE (x) == LABEL_REF)
9850     {
9851       output_addr_const (file, x);
9852       if (small_data_operand (x, GET_MODE (x)))
9853         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9854                  reg_names[SMALL_DATA_REG]);
9855       else if (TARGET_TOC)
9856         abort ();
9857     }
9858   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
9859     {
9860       if (REGNO (XEXP (x, 0)) == 0)
9861         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
9862                  reg_names[ REGNO (XEXP (x, 0)) ]);
9863       else
9864         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
9865                  reg_names[ REGNO (XEXP (x, 1)) ]);
9866     }
9867   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
9868     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
9869              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
9870 #if TARGET_ELF
9871   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9872            && CONSTANT_P (XEXP (x, 1)))
9873     {
9874       output_addr_const (file, XEXP (x, 1));
9875       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9876     }
9877 #endif
9878 #if TARGET_MACHO
9879   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9880            && CONSTANT_P (XEXP (x, 1)))
9881     {
9882       fprintf (file, "lo16(");
9883       output_addr_const (file, XEXP (x, 1));
9884       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9885     }
9886 #endif
9887   else if (legitimate_constant_pool_address_p (x))
9888     {
9889       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
9890         {
9891           rtx contains_minus = XEXP (x, 1);
9892           rtx minus, symref;
9893           const char *name;
9894           
9895           /* Find the (minus (sym) (toc)) buried in X, and temporarily
9896              turn it into (sym) for output_addr_const.  */
9897           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
9898             contains_minus = XEXP (contains_minus, 0);
9899
9900           minus = XEXP (contains_minus, 0);
9901           symref = XEXP (minus, 0);
9902           XEXP (contains_minus, 0) = symref;
9903           if (TARGET_ELF)
9904             {
9905               char *newname;
9906
9907               name = XSTR (symref, 0);
9908               newname = alloca (strlen (name) + sizeof ("@toc"));
9909               strcpy (newname, name);
9910               strcat (newname, "@toc");
9911               XSTR (symref, 0) = newname;
9912             }
9913           output_addr_const (file, XEXP (x, 1));
9914           if (TARGET_ELF)
9915             XSTR (symref, 0) = name;
9916           XEXP (contains_minus, 0) = minus;
9917         }
9918       else
9919         output_addr_const (file, XEXP (x, 1));
9920
9921       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
9922     }
9923   else
9924     abort ();
9925 }
9926 \f
9927 /* Target hook for assembling integer objects.  The PowerPC version has
9928    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
9929    is defined.  It also needs to handle DI-mode objects on 64-bit
9930    targets.  */
9931
9932 static bool
9933 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
9934 {
9935 #ifdef RELOCATABLE_NEEDS_FIXUP
9936   /* Special handling for SI values.  */
9937   if (size == 4 && aligned_p)
9938     {
9939       extern int in_toc_section (void);
9940       static int recurse = 0;
9941       
9942       /* For -mrelocatable, we mark all addresses that need to be fixed up
9943          in the .fixup section.  */
9944       if (TARGET_RELOCATABLE
9945           && !in_toc_section ()
9946           && !in_text_section ()
9947           && !in_unlikely_text_section ()
9948           && !recurse
9949           && GET_CODE (x) != CONST_INT
9950           && GET_CODE (x) != CONST_DOUBLE
9951           && CONSTANT_P (x))
9952         {
9953           char buf[256];
9954
9955           recurse = 1;
9956           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
9957           fixuplabelno++;
9958           ASM_OUTPUT_LABEL (asm_out_file, buf);
9959           fprintf (asm_out_file, "\t.long\t(");
9960           output_addr_const (asm_out_file, x);
9961           fprintf (asm_out_file, ")@fixup\n");
9962           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
9963           ASM_OUTPUT_ALIGN (asm_out_file, 2);
9964           fprintf (asm_out_file, "\t.long\t");
9965           assemble_name (asm_out_file, buf);
9966           fprintf (asm_out_file, "\n\t.previous\n");
9967           recurse = 0;
9968           return true;
9969         }
9970       /* Remove initial .'s to turn a -mcall-aixdesc function
9971          address into the address of the descriptor, not the function
9972          itself.  */
9973       else if (GET_CODE (x) == SYMBOL_REF
9974                && XSTR (x, 0)[0] == '.'
9975                && DEFAULT_ABI == ABI_AIX)
9976         {
9977           const char *name = XSTR (x, 0);
9978           while (*name == '.')
9979             name++;
9980
9981           fprintf (asm_out_file, "\t.long\t%s\n", name);
9982           return true;
9983         }
9984     }
9985 #endif /* RELOCATABLE_NEEDS_FIXUP */
9986   return default_assemble_integer (x, size, aligned_p);
9987 }
9988
9989 #ifdef HAVE_GAS_HIDDEN
9990 /* Emit an assembler directive to set symbol visibility for DECL to
9991    VISIBILITY_TYPE.  */
9992
9993 static void
9994 rs6000_assemble_visibility (tree decl, int vis)
9995 {
9996   /* Functions need to have their entry point symbol visibility set as
9997      well as their descriptor symbol visibility.  */
9998   if (DEFAULT_ABI == ABI_AIX && TREE_CODE (decl) == FUNCTION_DECL)
9999     {
10000       static const char * const visibility_types[] = {
10001         NULL, "internal", "hidden", "protected"
10002       };
10003
10004       const char *name, *type;
10005
10006       name = ((* targetm.strip_name_encoding)
10007               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
10008       type = visibility_types[vis];
10009
10010       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
10011       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
10012     }
10013   else
10014     default_assemble_visibility (decl, vis);
10015 }
10016 #endif
10017 \f
10018 enum rtx_code
10019 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
10020 {
10021   /* Reversal of FP compares takes care -- an ordered compare
10022      becomes an unordered compare and vice versa.  */
10023   if (mode == CCFPmode 
10024       && (!flag_finite_math_only
10025           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
10026           || code == UNEQ || code == LTGT))
10027     return reverse_condition_maybe_unordered (code);
10028   else
10029     return reverse_condition (code);
10030 }
10031
10032 /* Generate a compare for CODE.  Return a brand-new rtx that
10033    represents the result of the compare.  */
10034
10035 static rtx
10036 rs6000_generate_compare (enum rtx_code code)
10037 {
10038   enum machine_mode comp_mode;
10039   rtx compare_result;
10040
10041   if (rs6000_compare_fp_p)
10042     comp_mode = CCFPmode;
10043   else if (code == GTU || code == LTU
10044           || code == GEU || code == LEU)
10045     comp_mode = CCUNSmode;
10046   else
10047     comp_mode = CCmode;
10048
10049   /* First, the compare.  */
10050   compare_result = gen_reg_rtx (comp_mode);
10051
10052   /* SPE FP compare instructions on the GPRs.  Yuck!  */
10053   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT)
10054       && rs6000_compare_fp_p)
10055     {
10056       rtx cmp, or1, or2, or_result, compare_result2;
10057
10058       /* Note: The E500 comparison instructions set the GT bit (x +
10059          1), on success.  This explains the mess.  */
10060
10061       switch (code)
10062         {
10063         case EQ: case UNEQ: case NE: case LTGT:
10064           cmp = flag_finite_math_only
10065             ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
10066                                rs6000_compare_op1)
10067             : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
10068                                rs6000_compare_op1);
10069           break;
10070         case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
10071           cmp = flag_finite_math_only
10072             ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
10073                                rs6000_compare_op1)
10074             : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
10075                                rs6000_compare_op1);
10076           break;
10077         case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
10078           cmp = flag_finite_math_only
10079             ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
10080                                rs6000_compare_op1)
10081             : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
10082                                rs6000_compare_op1);
10083           break;
10084         default:
10085           abort ();
10086         }
10087
10088       /* Synthesize LE and GE from LT/GT || EQ.  */
10089       if (code == LE || code == GE || code == LEU || code == GEU)
10090         {
10091           emit_insn (cmp);
10092
10093           switch (code)
10094             {
10095             case LE: code = LT; break;
10096             case GE: code = GT; break;
10097             case LEU: code = LT; break;
10098             case GEU: code = GT; break;
10099             default: abort ();
10100             }
10101
10102           or1 = gen_reg_rtx (SImode);
10103           or2 = gen_reg_rtx (SImode);
10104           or_result = gen_reg_rtx (CCEQmode);
10105           compare_result2 = gen_reg_rtx (CCFPmode);
10106
10107           /* Do the EQ.  */
10108           cmp = flag_finite_math_only
10109             ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
10110                                rs6000_compare_op1)
10111             : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
10112                                rs6000_compare_op1);
10113           emit_insn (cmp);
10114
10115           or1 = gen_rtx_GT (SImode, compare_result, const0_rtx);
10116           or2 = gen_rtx_GT (SImode, compare_result2, const0_rtx);
10117
10118           /* OR them together.  */
10119           cmp = gen_rtx_SET (VOIDmode, or_result,
10120                              gen_rtx_COMPARE (CCEQmode,
10121                                               gen_rtx_IOR (SImode, or1, or2),
10122                                               const_true_rtx));
10123           compare_result = or_result;
10124           code = EQ;
10125         }
10126       else
10127         {
10128           if (code == NE || code == LTGT)
10129             code = NE;
10130           else
10131             code = EQ;
10132         }
10133
10134       emit_insn (cmp);
10135     }
10136   else
10137     emit_insn (gen_rtx_SET (VOIDmode, compare_result,
10138                             gen_rtx_COMPARE (comp_mode,
10139                                              rs6000_compare_op0, 
10140                                              rs6000_compare_op1)));
10141   
10142   /* Some kinds of FP comparisons need an OR operation;
10143      under flag_finite_math_only we don't bother.  */
10144   if (rs6000_compare_fp_p
10145       && ! flag_finite_math_only
10146       && ! (TARGET_HARD_FLOAT && TARGET_E500 && !TARGET_FPRS)
10147       && (code == LE || code == GE
10148           || code == UNEQ || code == LTGT
10149           || code == UNGT || code == UNLT))
10150     {
10151       enum rtx_code or1, or2;
10152       rtx or1_rtx, or2_rtx, compare2_rtx;
10153       rtx or_result = gen_reg_rtx (CCEQmode);
10154       
10155       switch (code)
10156         {
10157         case LE: or1 = LT;  or2 = EQ;  break;
10158         case GE: or1 = GT;  or2 = EQ;  break;
10159         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
10160         case LTGT: or1 = LT;  or2 = GT;  break;
10161         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
10162         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
10163         default:  abort ();
10164         }
10165       validate_condition_mode (or1, comp_mode);
10166       validate_condition_mode (or2, comp_mode);
10167       or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
10168       or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
10169       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
10170                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
10171                                       const_true_rtx);
10172       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
10173
10174       compare_result = or_result;
10175       code = EQ;
10176     }
10177
10178   validate_condition_mode (code, GET_MODE (compare_result));
10179   
10180   return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
10181 }
10182
10183
10184 /* Emit the RTL for an sCOND pattern.  */
10185
10186 void
10187 rs6000_emit_sCOND (enum rtx_code code, rtx result)
10188 {
10189   rtx condition_rtx;
10190   enum machine_mode op_mode;
10191   enum rtx_code cond_code;
10192
10193   condition_rtx = rs6000_generate_compare (code);
10194   cond_code = GET_CODE (condition_rtx);
10195
10196   if (TARGET_E500 && rs6000_compare_fp_p
10197       && !TARGET_FPRS && TARGET_HARD_FLOAT)
10198     {
10199       rtx t;
10200
10201       PUT_MODE (condition_rtx, SImode);
10202       t = XEXP (condition_rtx, 0);
10203
10204       if (cond_code != NE && cond_code != EQ)
10205         abort ();
10206
10207       if (cond_code == NE)
10208         emit_insn (gen_e500_flip_gt_bit (t, t));
10209
10210       emit_insn (gen_move_from_CR_gt_bit (result, t));
10211       return;
10212     }
10213
10214   if (cond_code == NE
10215       || cond_code == GE || cond_code == LE
10216       || cond_code == GEU || cond_code == LEU
10217       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
10218     {
10219       rtx not_result = gen_reg_rtx (CCEQmode);
10220       rtx not_op, rev_cond_rtx;
10221       enum machine_mode cc_mode;
10222       
10223       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
10224
10225       rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
10226                                      SImode, XEXP (condition_rtx, 0), const0_rtx);
10227       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
10228       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
10229       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
10230     }
10231
10232   op_mode = GET_MODE (rs6000_compare_op0);
10233   if (op_mode == VOIDmode)
10234     op_mode = GET_MODE (rs6000_compare_op1);
10235
10236   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
10237     {
10238       PUT_MODE (condition_rtx, DImode);
10239       convert_move (result, condition_rtx, 0);
10240     }
10241   else
10242     {
10243       PUT_MODE (condition_rtx, SImode);
10244       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
10245     }
10246 }
10247
10248 /* Emit a branch of kind CODE to location LOC.  */
10249
10250 void
10251 rs6000_emit_cbranch (enum rtx_code code, rtx loc)
10252 {
10253   rtx condition_rtx, loc_ref;
10254
10255   condition_rtx = rs6000_generate_compare (code);
10256   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
10257   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
10258                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
10259                                                      loc_ref, pc_rtx)));
10260 }
10261
10262 /* Return the string to output a conditional branch to LABEL, which is
10263    the operand number of the label, or -1 if the branch is really a
10264    conditional return.  
10265
10266    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
10267    condition code register and its mode specifies what kind of
10268    comparison we made.
10269
10270    REVERSED is nonzero if we should reverse the sense of the comparison.
10271
10272    INSN is the insn.  */
10273
10274 char *
10275 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
10276 {
10277   static char string[64];
10278   enum rtx_code code = GET_CODE (op);
10279   rtx cc_reg = XEXP (op, 0);
10280   enum machine_mode mode = GET_MODE (cc_reg);
10281   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
10282   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
10283   int really_reversed = reversed ^ need_longbranch;
10284   char *s = string;
10285   const char *ccode;
10286   const char *pred;
10287   rtx note;
10288
10289   validate_condition_mode (code, mode);
10290
10291   /* Work out which way this really branches.  We could use
10292      reverse_condition_maybe_unordered here always but this
10293      makes the resulting assembler clearer.  */
10294   if (really_reversed)
10295     {
10296       /* Reversal of FP compares takes care -- an ordered compare
10297          becomes an unordered compare and vice versa.  */
10298       if (mode == CCFPmode)
10299         code = reverse_condition_maybe_unordered (code);
10300       else
10301         code = reverse_condition (code);
10302     }
10303
10304   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
10305     {
10306       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
10307          to the GT bit.  */
10308       if (code == EQ)
10309         /* Opposite of GT.  */
10310         code = GT;
10311       else if (code == NE)
10312         code = UNLE;
10313       else
10314         abort ();
10315     }
10316
10317   switch (code)
10318     {
10319       /* Not all of these are actually distinct opcodes, but
10320          we distinguish them for clarity of the resulting assembler.  */
10321     case NE: case LTGT:
10322       ccode = "ne"; break;
10323     case EQ: case UNEQ:
10324       ccode = "eq"; break;
10325     case GE: case GEU: 
10326       ccode = "ge"; break;
10327     case GT: case GTU: case UNGT: 
10328       ccode = "gt"; break;
10329     case LE: case LEU: 
10330       ccode = "le"; break;
10331     case LT: case LTU: case UNLT: 
10332       ccode = "lt"; break;
10333     case UNORDERED: ccode = "un"; break;
10334     case ORDERED: ccode = "nu"; break;
10335     case UNGE: ccode = "nl"; break;
10336     case UNLE: ccode = "ng"; break;
10337     default:
10338       abort ();
10339     }
10340   
10341   /* Maybe we have a guess as to how likely the branch is.  
10342      The old mnemonics don't have a way to specify this information.  */
10343   pred = "";
10344   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
10345   if (note != NULL_RTX)
10346     {
10347       /* PROB is the difference from 50%.  */
10348       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
10349
10350       /* Only hint for highly probable/improbable branches on newer
10351          cpus as static prediction overrides processor dynamic
10352          prediction.  For older cpus we may as well always hint, but
10353          assume not taken for branches that are very close to 50% as a
10354          mispredicted taken branch is more expensive than a
10355          mispredicted not-taken branch.  */ 
10356       if (rs6000_always_hint
10357           || abs (prob) > REG_BR_PROB_BASE / 100 * 48)
10358         {
10359           if (abs (prob) > REG_BR_PROB_BASE / 20
10360               && ((prob > 0) ^ need_longbranch))
10361               pred = "+";
10362           else
10363             pred = "-";
10364         }
10365     }
10366
10367   if (label == NULL)
10368     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
10369   else
10370     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
10371
10372   /* We need to escape any '%' characters in the reg_names string.
10373      Assume they'd only be the first character....  */
10374   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
10375     *s++ = '%';
10376   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
10377
10378   if (label != NULL)
10379     {
10380       /* If the branch distance was too far, we may have to use an
10381          unconditional branch to go the distance.  */
10382       if (need_longbranch)
10383         s += sprintf (s, ",$+8\n\tb %s", label);
10384       else
10385         s += sprintf (s, ",%s", label);
10386     }
10387
10388   return string;
10389 }
10390
10391 /* Return the string to flip the GT bit on a CR.  */
10392 char *
10393 output_e500_flip_gt_bit (rtx dst, rtx src)
10394 {
10395   static char string[64];
10396   int a, b;
10397
10398   if (GET_CODE (dst) != REG || ! CR_REGNO_P (REGNO (dst))
10399       || GET_CODE (src) != REG || ! CR_REGNO_P (REGNO (src)))
10400     abort ();
10401
10402   /* GT bit.  */
10403   a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
10404   b = 4 * (REGNO (src) - CR0_REGNO) + 1;
10405
10406   sprintf (string, "crnot %d,%d", a, b);
10407   return string;
10408 }
10409
10410 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
10411    operands of the last comparison is nonzero/true, FALSE_COND if it
10412    is zero/false.  Return 0 if the hardware has no such operation.  */
10413
10414 int
10415 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
10416 {
10417   enum rtx_code code = GET_CODE (op);
10418   rtx op0 = rs6000_compare_op0;
10419   rtx op1 = rs6000_compare_op1;
10420   REAL_VALUE_TYPE c1;
10421   enum machine_mode compare_mode = GET_MODE (op0);
10422   enum machine_mode result_mode = GET_MODE (dest);
10423   rtx temp;
10424
10425   /* These modes should always match.  */
10426   if (GET_MODE (op1) != compare_mode
10427       /* In the isel case however, we can use a compare immediate, so
10428          op1 may be a small constant.  */
10429       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
10430     return 0;
10431   if (GET_MODE (true_cond) != result_mode)
10432     return 0;
10433   if (GET_MODE (false_cond) != result_mode)
10434     return 0;
10435
10436   /* First, work out if the hardware can do this at all, or
10437      if it's too slow....  */
10438   if (! rs6000_compare_fp_p)
10439     {
10440       if (TARGET_ISEL)
10441         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
10442       return 0;
10443     }
10444   else if (TARGET_E500 && TARGET_HARD_FLOAT && !TARGET_FPRS
10445            && GET_MODE_CLASS (compare_mode) == MODE_FLOAT)
10446     return 0;
10447
10448   /* Eliminate half of the comparisons by switching operands, this
10449      makes the remaining code simpler.  */
10450   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
10451       || code == LTGT || code == LT || code == UNLE)
10452     {
10453       code = reverse_condition_maybe_unordered (code);
10454       temp = true_cond;
10455       true_cond = false_cond;
10456       false_cond = temp;
10457     }
10458
10459   /* UNEQ and LTGT take four instructions for a comparison with zero,
10460      it'll probably be faster to use a branch here too.  */
10461   if (code == UNEQ && HONOR_NANS (compare_mode))
10462     return 0;
10463   
10464   if (GET_CODE (op1) == CONST_DOUBLE)
10465     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
10466     
10467   /* We're going to try to implement comparisons by performing
10468      a subtract, then comparing against zero.  Unfortunately,
10469      Inf - Inf is NaN which is not zero, and so if we don't
10470      know that the operand is finite and the comparison
10471      would treat EQ different to UNORDERED, we can't do it.  */
10472   if (HONOR_INFINITIES (compare_mode)
10473       && code != GT && code != UNGE
10474       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
10475       /* Constructs of the form (a OP b ? a : b) are safe.  */
10476       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
10477           || (! rtx_equal_p (op0, true_cond) 
10478               && ! rtx_equal_p (op1, true_cond))))
10479     return 0;
10480   /* At this point we know we can use fsel.  */
10481
10482   /* Reduce the comparison to a comparison against zero.  */
10483   temp = gen_reg_rtx (compare_mode);
10484   emit_insn (gen_rtx_SET (VOIDmode, temp,
10485                           gen_rtx_MINUS (compare_mode, op0, op1)));
10486   op0 = temp;
10487   op1 = CONST0_RTX (compare_mode);
10488
10489   /* If we don't care about NaNs we can reduce some of the comparisons
10490      down to faster ones.  */
10491   if (! HONOR_NANS (compare_mode))
10492     switch (code)
10493       {
10494       case GT:
10495         code = LE;
10496         temp = true_cond;
10497         true_cond = false_cond;
10498         false_cond = temp;
10499         break;
10500       case UNGE:
10501         code = GE;
10502         break;
10503       case UNEQ:
10504         code = EQ;
10505         break;
10506       default:
10507         break;
10508       }
10509
10510   /* Now, reduce everything down to a GE.  */
10511   switch (code)
10512     {
10513     case GE:
10514       break;
10515
10516     case LE:
10517       temp = gen_reg_rtx (compare_mode);
10518       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
10519       op0 = temp;
10520       break;
10521
10522     case ORDERED:
10523       temp = gen_reg_rtx (compare_mode);
10524       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
10525       op0 = temp;
10526       break;
10527
10528     case EQ:
10529       temp = gen_reg_rtx (compare_mode);
10530       emit_insn (gen_rtx_SET (VOIDmode, temp, 
10531                               gen_rtx_NEG (compare_mode,
10532                                            gen_rtx_ABS (compare_mode, op0))));
10533       op0 = temp;
10534       break;
10535
10536     case UNGE:
10537       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
10538       temp = gen_reg_rtx (result_mode);
10539       emit_insn (gen_rtx_SET (VOIDmode, temp,
10540                               gen_rtx_IF_THEN_ELSE (result_mode,
10541                                                     gen_rtx_GE (VOIDmode,
10542                                                                 op0, op1),
10543                                                     true_cond, false_cond)));
10544       false_cond = true_cond;
10545       true_cond = temp;
10546
10547       temp = gen_reg_rtx (compare_mode);
10548       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
10549       op0 = temp;
10550       break;
10551
10552     case GT:
10553       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
10554       temp = gen_reg_rtx (result_mode);
10555       emit_insn (gen_rtx_SET (VOIDmode, temp,
10556                               gen_rtx_IF_THEN_ELSE (result_mode, 
10557                                                     gen_rtx_GE (VOIDmode,
10558                                                                 op0, op1),
10559                                                     true_cond, false_cond)));
10560       true_cond = false_cond;
10561       false_cond = temp;
10562
10563       temp = gen_reg_rtx (compare_mode);
10564       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
10565       op0 = temp;
10566       break;
10567
10568     default:
10569       abort ();
10570     }
10571
10572   emit_insn (gen_rtx_SET (VOIDmode, dest,
10573                           gen_rtx_IF_THEN_ELSE (result_mode,
10574                                                 gen_rtx_GE (VOIDmode,
10575                                                             op0, op1),
10576                                                 true_cond, false_cond)));
10577   return 1;
10578 }
10579
10580 /* Same as above, but for ints (isel).  */
10581
10582 static int
10583 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
10584 {
10585   rtx condition_rtx, cr;
10586
10587   /* All isel implementations thus far are 32-bits.  */
10588   if (GET_MODE (rs6000_compare_op0) != SImode)
10589     return 0;
10590
10591   /* We still have to do the compare, because isel doesn't do a
10592      compare, it just looks at the CRx bits set by a previous compare
10593      instruction.  */
10594   condition_rtx = rs6000_generate_compare (GET_CODE (op));
10595   cr = XEXP (condition_rtx, 0);
10596
10597   if (GET_MODE (cr) == CCmode)
10598     emit_insn (gen_isel_signed (dest, condition_rtx,
10599                                 true_cond, false_cond, cr));
10600   else
10601     emit_insn (gen_isel_unsigned (dest, condition_rtx,
10602                                   true_cond, false_cond, cr));
10603
10604   return 1;
10605 }
10606
10607 const char *
10608 output_isel (rtx *operands)
10609 {
10610   enum rtx_code code;
10611
10612   code = GET_CODE (operands[1]);
10613   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
10614     {
10615       PUT_CODE (operands[1], reverse_condition (code));
10616       return "isel %0,%3,%2,%j1";
10617     }
10618   else
10619     return "isel %0,%2,%3,%j1";
10620 }
10621
10622 void
10623 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
10624 {
10625   enum machine_mode mode = GET_MODE (op0);
10626   enum rtx_code c;
10627   rtx target;
10628
10629   if (code == SMAX || code == SMIN)
10630     c = GE;
10631   else
10632     c = GEU;
10633
10634   if (code == SMAX || code == UMAX)
10635     target = emit_conditional_move (dest, c, op0, op1, mode, 
10636                                     op0, op1, mode, 0);
10637   else
10638     target = emit_conditional_move (dest, c, op0, op1, mode, 
10639                                     op1, op0, mode, 0);
10640   if (target == NULL_RTX)
10641     abort ();
10642   if (target != dest)
10643     emit_move_insn (dest, target);
10644 }
10645
10646 /* Emit instructions to move SRC to DST.  Called by splitters for
10647    multi-register moves.  It will emit at most one instruction for
10648    each register that is accessed; that is, it won't emit li/lis pairs
10649    (or equivalent for 64-bit code).  One of SRC or DST must be a hard
10650    register.  */
10651
10652 void
10653 rs6000_split_multireg_move (rtx dst, rtx src)
10654 {
10655   /* The register number of the first register being moved.  */
10656   int reg;
10657   /* The mode that is to be moved.  */
10658   enum machine_mode mode;
10659   /* The mode that the move is being done in, and its size.  */
10660   enum machine_mode reg_mode;
10661   int reg_mode_size;
10662   /* The number of registers that will be moved.  */
10663   int nregs;
10664
10665   reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
10666   mode = GET_MODE (dst);
10667   nregs = HARD_REGNO_NREGS (reg, mode);
10668   if (FP_REGNO_P (reg))
10669     reg_mode = DFmode;
10670   else if (ALTIVEC_REGNO_P (reg))
10671     reg_mode = V16QImode;
10672   else
10673     reg_mode = word_mode;
10674   reg_mode_size = GET_MODE_SIZE (reg_mode);
10675   
10676   if (reg_mode_size * nregs != GET_MODE_SIZE (mode))
10677     abort ();
10678   
10679   if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
10680     {
10681       /* Move register range backwards, if we might have destructive
10682          overlap.  */
10683       int i;
10684       for (i = nregs - 1; i >= 0; i--)
10685         emit_insn (gen_rtx_SET (VOIDmode, 
10686                                 simplify_gen_subreg (reg_mode, dst, mode,
10687                                                      i * reg_mode_size),
10688                                 simplify_gen_subreg (reg_mode, src, mode,
10689                                                      i * reg_mode_size)));
10690     }
10691   else
10692     {
10693       int i;
10694       int j = -1;
10695       bool used_update = false;
10696
10697       if (GET_CODE (src) == MEM && INT_REGNO_P (reg))
10698         {
10699           rtx breg;
10700
10701           if (GET_CODE (XEXP (src, 0)) == PRE_INC
10702               || GET_CODE (XEXP (src, 0)) == PRE_DEC)
10703             {
10704               rtx delta_rtx;
10705               breg = XEXP (XEXP (src, 0), 0);
10706               delta_rtx =  GET_CODE (XEXP (src, 0)) == PRE_INC 
10707                   ? GEN_INT (GET_MODE_SIZE (GET_MODE (src))) 
10708                   : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))); 
10709               emit_insn (TARGET_32BIT
10710                          ? gen_addsi3 (breg, breg, delta_rtx)
10711                          : gen_adddi3 (breg, breg, delta_rtx));
10712               src = gen_rtx_MEM (mode, breg);
10713             }
10714
10715           /* We have now address involving an base register only.
10716              If we use one of the registers to address memory, 
10717              we have change that register last.  */
10718
10719           breg = (GET_CODE (XEXP (src, 0)) == PLUS
10720                   ? XEXP (XEXP (src, 0), 0)
10721                   : XEXP (src, 0));
10722
10723           if (!REG_P (breg))
10724               abort();
10725
10726           if (REGNO (breg) >= REGNO (dst) 
10727               && REGNO (breg) < REGNO (dst) + nregs)
10728             j = REGNO (breg) - REGNO (dst);
10729         }
10730
10731       if (GET_CODE (dst) == MEM && INT_REGNO_P (reg))
10732         {
10733           rtx breg;
10734
10735           if (GET_CODE (XEXP (dst, 0)) == PRE_INC
10736               || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
10737             {
10738               rtx delta_rtx;
10739               breg = XEXP (XEXP (dst, 0), 0);
10740               delta_rtx = GET_CODE (XEXP (dst, 0)) == PRE_INC 
10741                 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst))) 
10742                 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))); 
10743
10744               /* We have to update the breg before doing the store.
10745                  Use store with update, if available.  */
10746
10747               if (TARGET_UPDATE)
10748                 {
10749                   rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
10750                   emit_insn (TARGET_32BIT
10751                              ? gen_movsi_update (breg, breg, delta_rtx, nsrc)
10752                              : gen_movdi_update (breg, breg, delta_rtx, nsrc));
10753                   used_update = true;
10754                 }
10755               else
10756                 emit_insn (TARGET_32BIT
10757                            ? gen_addsi3 (breg, breg, delta_rtx)
10758                            : gen_adddi3 (breg, breg, delta_rtx));
10759               dst = gen_rtx_MEM (mode, breg);
10760             }
10761         }
10762
10763       for (i = 0; i < nregs; i++)
10764         {  
10765           /* Calculate index to next subword.  */
10766           ++j;
10767           if (j == nregs) 
10768             j = 0;
10769
10770           /* If compiler already emited move of first word by 
10771              store with update, no need to do anything.  */
10772           if (j == 0 && used_update)
10773             continue;
10774           
10775           emit_insn (gen_rtx_SET (VOIDmode,
10776                                   simplify_gen_subreg (reg_mode, dst, mode,
10777                                                        j * reg_mode_size),
10778                                   simplify_gen_subreg (reg_mode, src, mode,
10779                                                        j * reg_mode_size)));
10780         }
10781     }
10782 }
10783
10784 \f
10785 /* This page contains routines that are used to determine what the
10786    function prologue and epilogue code will do and write them out.  */
10787
10788 /* Return the first fixed-point register that is required to be
10789    saved. 32 if none.  */
10790
10791 int
10792 first_reg_to_save (void)
10793 {
10794   int first_reg;
10795
10796   /* Find lowest numbered live register.  */
10797   for (first_reg = 13; first_reg <= 31; first_reg++)
10798     if (regs_ever_live[first_reg] 
10799         && (! call_used_regs[first_reg]
10800             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
10801                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
10802                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
10803       break;
10804
10805 #if TARGET_MACHO
10806   if (flag_pic
10807       && current_function_uses_pic_offset_table
10808       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
10809     return RS6000_PIC_OFFSET_TABLE_REGNUM;
10810 #endif
10811
10812   return first_reg;
10813 }
10814
10815 /* Similar, for FP regs.  */
10816
10817 int
10818 first_fp_reg_to_save (void)
10819 {
10820   int first_reg;
10821
10822   /* Find lowest numbered live register.  */
10823   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
10824     if (regs_ever_live[first_reg])
10825       break;
10826
10827   return first_reg;
10828 }
10829
10830 /* Similar, for AltiVec regs.  */
10831
10832 static int
10833 first_altivec_reg_to_save (void)
10834 {
10835   int i;
10836
10837   /* Stack frame remains as is unless we are in AltiVec ABI.  */
10838   if (! TARGET_ALTIVEC_ABI)
10839     return LAST_ALTIVEC_REGNO + 1;
10840
10841   /* Find lowest numbered live register.  */
10842   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
10843     if (regs_ever_live[i])
10844       break;
10845
10846   return i;
10847 }
10848
10849 /* Return a 32-bit mask of the AltiVec registers we need to set in
10850    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
10851    the 32-bit word is 0.  */
10852
10853 static unsigned int
10854 compute_vrsave_mask (void)
10855 {
10856   unsigned int i, mask = 0;
10857
10858   /* First, find out if we use _any_ altivec registers.  */
10859   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10860     if (regs_ever_live[i])
10861       mask |= ALTIVEC_REG_BIT (i);
10862
10863   if (mask == 0)
10864     return mask;
10865
10866   /* Next, remove the argument registers from the set.  These must
10867      be in the VRSAVE mask set by the caller, so we don't need to add
10868      them in again.  More importantly, the mask we compute here is
10869      used to generate CLOBBERs in the set_vrsave insn, and we do not
10870      wish the argument registers to die.  */
10871   for (i = cfun->args_info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
10872     mask &= ~ALTIVEC_REG_BIT (i);
10873
10874   /* Similarly, remove the return value from the set.  */
10875   {
10876     bool yes = false;
10877     diddle_return_value (is_altivec_return_reg, &yes);
10878     if (yes)
10879       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
10880   }
10881
10882   return mask;
10883 }
10884
10885 static void
10886 is_altivec_return_reg (rtx reg, void *xyes)
10887 {
10888   bool *yes = (bool *) xyes;
10889   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
10890     *yes = true;
10891 }
10892
10893 \f
10894 /* Calculate the stack information for the current function.  This is
10895    complicated by having two separate calling sequences, the AIX calling
10896    sequence and the V.4 calling sequence.
10897
10898    AIX (and Darwin/Mac OS X) stack frames look like:
10899                                                           32-bit  64-bit
10900         SP----> +---------------------------------------+
10901                 | back chain to caller                  | 0       0
10902                 +---------------------------------------+
10903                 | saved CR                              | 4       8 (8-11)
10904                 +---------------------------------------+
10905                 | saved LR                              | 8       16
10906                 +---------------------------------------+
10907                 | reserved for compilers                | 12      24
10908                 +---------------------------------------+
10909                 | reserved for binders                  | 16      32
10910                 +---------------------------------------+
10911                 | saved TOC pointer                     | 20      40
10912                 +---------------------------------------+
10913                 | Parameter save area (P)               | 24      48
10914                 +---------------------------------------+
10915                 | Alloca space (A)                      | 24+P    etc.
10916                 +---------------------------------------+
10917                 | Local variable space (L)              | 24+P+A
10918                 +---------------------------------------+
10919                 | Float/int conversion temporary (X)    | 24+P+A+L
10920                 +---------------------------------------+
10921                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
10922                 +---------------------------------------+
10923                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
10924                 +---------------------------------------+
10925                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
10926                 +---------------------------------------+
10927                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
10928                 +---------------------------------------+
10929                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
10930                 +---------------------------------------+
10931         old SP->| back chain to caller's caller         |
10932                 +---------------------------------------+
10933
10934    The required alignment for AIX configurations is two words (i.e., 8
10935    or 16 bytes).
10936
10937
10938    V.4 stack frames look like:
10939
10940         SP----> +---------------------------------------+
10941                 | back chain to caller                  | 0
10942                 +---------------------------------------+
10943                 | caller's saved LR                     | 4
10944                 +---------------------------------------+
10945                 | Parameter save area (P)               | 8
10946                 +---------------------------------------+
10947                 | Alloca space (A)                      | 8+P
10948                 +---------------------------------------+    
10949                 | Varargs save area (V)                 | 8+P+A
10950                 +---------------------------------------+    
10951                 | Local variable space (L)              | 8+P+A+V
10952                 +---------------------------------------+    
10953                 | Float/int conversion temporary (X)    | 8+P+A+V+L
10954                 +---------------------------------------+
10955                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
10956                 +---------------------------------------+
10957                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
10958                 +---------------------------------------+
10959                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
10960                 +---------------------------------------+
10961                 | SPE: area for 64-bit GP registers     |
10962                 +---------------------------------------+
10963                 | SPE alignment padding                 |
10964                 +---------------------------------------+
10965                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
10966                 +---------------------------------------+    
10967                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
10968                 +---------------------------------------+    
10969                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
10970                 +---------------------------------------+
10971         old SP->| back chain to caller's caller         |
10972                 +---------------------------------------+
10973
10974    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
10975    given.  (But note below and in sysv4.h that we require only 8 and
10976    may round up the size of our stack frame anyways.  The historical
10977    reason is early versions of powerpc-linux which didn't properly
10978    align the stack at program startup.  A happy side-effect is that
10979    -mno-eabi libraries can be used with -meabi programs.)
10980
10981    The EABI configuration defaults to the V.4 layout.  However,
10982    the stack alignment requirements may differ.  If -mno-eabi is not
10983    given, the required stack alignment is 8 bytes; if -mno-eabi is
10984    given, the required alignment is 16 bytes.  (But see V.4 comment
10985    above.)  */
10986
10987 #ifndef ABI_STACK_BOUNDARY
10988 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
10989 #endif
10990
10991 static rs6000_stack_t *
10992 rs6000_stack_info (void)
10993 {
10994   static rs6000_stack_t info, zero_info;
10995   rs6000_stack_t *info_ptr = &info;
10996   int reg_size = TARGET_32BIT ? 4 : 8;
10997   int ehrd_size;
10998   HOST_WIDE_INT non_fixed_size;
10999
11000   /* Zero all fields portably.  */
11001   info = zero_info;
11002
11003   if (TARGET_SPE)
11004     {
11005       /* Cache value so we don't rescan instruction chain over and over.  */
11006       if (cfun->machine->insn_chain_scanned_p == 0)
11007         {
11008           cfun->machine->insn_chain_scanned_p = 1;
11009           info_ptr->spe_64bit_regs_used = (int) spe_func_has_64bit_regs_p ();
11010         }
11011     }
11012
11013   /* Select which calling sequence.  */
11014   info_ptr->abi = DEFAULT_ABI;
11015
11016   /* Calculate which registers need to be saved & save area size.  */
11017   info_ptr->first_gp_reg_save = first_reg_to_save ();
11018   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
11019      even if it currently looks like we won't.  */
11020   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
11021        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
11022        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
11023       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
11024     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
11025   else
11026     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
11027
11028   /* For the SPE, we have an additional upper 32-bits on each GPR.
11029      Ideally we should save the entire 64-bits only when the upper
11030      half is used in SIMD instructions.  Since we only record
11031      registers live (not the size they are used in), this proves
11032      difficult because we'd have to traverse the instruction chain at
11033      the right time, taking reload into account.  This is a real pain,
11034      so we opt to save the GPRs in 64-bits always if but one register
11035      gets used in 64-bits.  Otherwise, all the registers in the frame
11036      get saved in 32-bits.
11037
11038      So... since when we save all GPRs (except the SP) in 64-bits, the
11039      traditional GP save area will be empty.  */
11040   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
11041     info_ptr->gp_size = 0;
11042
11043   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
11044   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
11045
11046   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
11047   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
11048                                  - info_ptr->first_altivec_reg_save);
11049
11050   /* Does this function call anything?  */
11051   info_ptr->calls_p = (! current_function_is_leaf
11052                        || cfun->machine->ra_needs_full_frame);
11053
11054   /* Determine if we need to save the link register.  */
11055   if (rs6000_ra_ever_killed ()
11056       || (DEFAULT_ABI == ABI_AIX
11057           && current_function_profile
11058           && !TARGET_PROFILE_KERNEL)
11059 #ifdef TARGET_RELOCATABLE
11060       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
11061 #endif
11062       || (info_ptr->first_fp_reg_save != 64
11063           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
11064       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
11065       || (DEFAULT_ABI == ABI_V4 && current_function_calls_alloca)
11066       || (DEFAULT_ABI == ABI_DARWIN
11067           && flag_pic
11068           && current_function_uses_pic_offset_table)
11069       || info_ptr->calls_p)
11070     {
11071       info_ptr->lr_save_p = 1;
11072       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
11073     }
11074
11075   /* Determine if we need to save the condition code registers.  */
11076   if (regs_ever_live[CR2_REGNO] 
11077       || regs_ever_live[CR3_REGNO]
11078       || regs_ever_live[CR4_REGNO])
11079     {
11080       info_ptr->cr_save_p = 1;
11081       if (DEFAULT_ABI == ABI_V4)
11082         info_ptr->cr_size = reg_size;
11083     }
11084
11085   /* If the current function calls __builtin_eh_return, then we need
11086      to allocate stack space for registers that will hold data for
11087      the exception handler.  */
11088   if (current_function_calls_eh_return)
11089     {
11090       unsigned int i;
11091       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
11092         continue;
11093
11094       /* SPE saves EH registers in 64-bits.  */
11095       ehrd_size = i * (TARGET_SPE_ABI
11096                        && info_ptr->spe_64bit_regs_used != 0
11097                        ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
11098     }
11099   else
11100     ehrd_size = 0;
11101
11102   /* Determine various sizes.  */
11103   info_ptr->reg_size     = reg_size;
11104   info_ptr->fixed_size   = RS6000_SAVE_AREA;
11105   info_ptr->varargs_size = RS6000_VARARGS_AREA;
11106   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
11107   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
11108                                          TARGET_ALTIVEC ? 16 : 8);
11109
11110   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
11111     info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
11112   else
11113     info_ptr->spe_gp_size = 0;
11114
11115   if (TARGET_ALTIVEC_ABI)
11116     info_ptr->vrsave_mask = compute_vrsave_mask ();
11117   else
11118     info_ptr->vrsave_mask = 0;
11119
11120   if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
11121     info_ptr->vrsave_size  = 4;
11122   else
11123     info_ptr->vrsave_size  = 0;
11124
11125   /* Calculate the offsets.  */
11126   switch (DEFAULT_ABI)
11127     {
11128     case ABI_NONE:
11129     default:
11130       abort ();
11131
11132     case ABI_AIX:
11133     case ABI_DARWIN:
11134       info_ptr->fp_save_offset   = - info_ptr->fp_size;
11135       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
11136
11137       if (TARGET_ALTIVEC_ABI)
11138         {
11139           info_ptr->vrsave_save_offset
11140             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
11141
11142           /* Align stack so vector save area is on a quadword boundary.  */
11143           if (info_ptr->altivec_size != 0)
11144             info_ptr->altivec_padding_size
11145               = 16 - (-info_ptr->vrsave_save_offset % 16);
11146           else
11147             info_ptr->altivec_padding_size = 0;
11148
11149           info_ptr->altivec_save_offset
11150             = info_ptr->vrsave_save_offset
11151             - info_ptr->altivec_padding_size
11152             - info_ptr->altivec_size;
11153
11154           /* Adjust for AltiVec case.  */
11155           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
11156         }
11157       else
11158         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
11159       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
11160       info_ptr->lr_save_offset   = 2*reg_size;
11161       break;
11162
11163     case ABI_V4:
11164       info_ptr->fp_save_offset   = - info_ptr->fp_size;
11165       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
11166       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
11167
11168       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
11169       {
11170         /* Align stack so SPE GPR save area is aligned on a
11171            double-word boundary.  */
11172         if (info_ptr->spe_gp_size != 0)
11173           info_ptr->spe_padding_size
11174             = 8 - (-info_ptr->cr_save_offset % 8);
11175         else
11176           info_ptr->spe_padding_size = 0;
11177
11178         info_ptr->spe_gp_save_offset
11179           = info_ptr->cr_save_offset
11180           - info_ptr->spe_padding_size
11181           - info_ptr->spe_gp_size;
11182
11183         /* Adjust for SPE case.  */
11184         info_ptr->toc_save_offset
11185           = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
11186       }
11187       else if (TARGET_ALTIVEC_ABI)
11188         {
11189           info_ptr->vrsave_save_offset
11190             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
11191
11192           /* Align stack so vector save area is on a quadword boundary.  */
11193           if (info_ptr->altivec_size != 0)
11194             info_ptr->altivec_padding_size
11195               = 16 - (-info_ptr->vrsave_save_offset % 16);
11196           else
11197             info_ptr->altivec_padding_size = 0;
11198
11199           info_ptr->altivec_save_offset
11200             = info_ptr->vrsave_save_offset
11201             - info_ptr->altivec_padding_size
11202             - info_ptr->altivec_size;
11203
11204           /* Adjust for AltiVec case.  */
11205           info_ptr->toc_save_offset
11206             = info_ptr->altivec_save_offset - info_ptr->toc_size;
11207         }
11208       else
11209         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
11210       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
11211       info_ptr->lr_save_offset   = reg_size;
11212       break;
11213     }
11214
11215   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
11216                                          + info_ptr->gp_size
11217                                          + info_ptr->altivec_size
11218                                          + info_ptr->altivec_padding_size
11219                                          + info_ptr->spe_gp_size
11220                                          + info_ptr->spe_padding_size
11221                                          + ehrd_size
11222                                          + info_ptr->cr_size
11223                                          + info_ptr->lr_size
11224                                          + info_ptr->vrsave_size
11225                                          + info_ptr->toc_size,
11226                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
11227                                          ? 16 : 8);
11228
11229   non_fixed_size         = (info_ptr->vars_size
11230                             + info_ptr->parm_size
11231                             + info_ptr->save_size
11232                             + info_ptr->varargs_size);
11233
11234   info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
11235                                        ABI_STACK_BOUNDARY / BITS_PER_UNIT);
11236
11237   /* Determine if we need to allocate any stack frame:
11238
11239      For AIX we need to push the stack if a frame pointer is needed
11240      (because the stack might be dynamically adjusted), if we are
11241      debugging, if we make calls, or if the sum of fp_save, gp_save,
11242      and local variables are more than the space needed to save all
11243      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
11244      + 18*8 = 288 (GPR13 reserved).
11245
11246      For V.4 we don't have the stack cushion that AIX uses, but assume
11247      that the debugger can handle stackless frames.  */
11248
11249   if (info_ptr->calls_p)
11250     info_ptr->push_p = 1;
11251
11252   else if (DEFAULT_ABI == ABI_V4)
11253     info_ptr->push_p = non_fixed_size != 0;
11254
11255   else if (frame_pointer_needed)
11256     info_ptr->push_p = 1;
11257
11258   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
11259     info_ptr->push_p = 1;
11260
11261   else
11262     info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
11263
11264   /* Zero offsets if we're not saving those registers.  */
11265   if (info_ptr->fp_size == 0)
11266     info_ptr->fp_save_offset = 0;
11267
11268   if (info_ptr->gp_size == 0)
11269     info_ptr->gp_save_offset = 0;
11270
11271   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
11272     info_ptr->altivec_save_offset = 0;
11273
11274   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
11275     info_ptr->vrsave_save_offset = 0;
11276
11277   if (! TARGET_SPE_ABI
11278       || info_ptr->spe_64bit_regs_used == 0
11279       || info_ptr->spe_gp_size == 0)
11280     info_ptr->spe_gp_save_offset = 0;
11281
11282   if (! info_ptr->lr_save_p)
11283     info_ptr->lr_save_offset = 0;
11284
11285   if (! info_ptr->cr_save_p)
11286     info_ptr->cr_save_offset = 0;
11287
11288   if (! info_ptr->toc_save_p)
11289     info_ptr->toc_save_offset = 0;
11290
11291   return info_ptr;
11292 }
11293
11294 /* Return true if the current function uses any GPRs in 64-bit SIMD
11295    mode.  */
11296
11297 static bool
11298 spe_func_has_64bit_regs_p (void)
11299 {
11300   rtx insns, insn;
11301
11302   /* Functions that save and restore all the call-saved registers will
11303      need to save/restore the registers in 64-bits.  */
11304   if (current_function_calls_eh_return
11305       || current_function_calls_setjmp
11306       || current_function_has_nonlocal_goto)
11307     return true;
11308
11309   insns = get_insns ();
11310
11311   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
11312     {
11313       if (INSN_P (insn))
11314         {
11315           rtx i;
11316
11317           i = PATTERN (insn);
11318           if (GET_CODE (i) == SET
11319               && SPE_VECTOR_MODE (GET_MODE (SET_SRC (i))))
11320             return true;
11321         }
11322     }
11323
11324   return false;
11325 }
11326
11327 static void
11328 debug_stack_info (rs6000_stack_t *info)
11329 {
11330   const char *abi_string;
11331
11332   if (! info)
11333     info = rs6000_stack_info ();
11334
11335   fprintf (stderr, "\nStack information for function %s:\n",
11336            ((current_function_decl && DECL_NAME (current_function_decl))
11337             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
11338             : "<unknown>"));
11339
11340   switch (info->abi)
11341     {
11342     default:             abi_string = "Unknown";        break;
11343     case ABI_NONE:       abi_string = "NONE";           break;
11344     case ABI_AIX:        abi_string = "AIX";            break;
11345     case ABI_DARWIN:     abi_string = "Darwin";         break;
11346     case ABI_V4:         abi_string = "V.4";            break;
11347     }
11348
11349   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
11350
11351   if (TARGET_ALTIVEC_ABI)
11352     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
11353
11354   if (TARGET_SPE_ABI)
11355     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
11356
11357   if (info->first_gp_reg_save != 32)
11358     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
11359
11360   if (info->first_fp_reg_save != 64)
11361     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
11362
11363   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
11364     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
11365              info->first_altivec_reg_save);
11366
11367   if (info->lr_save_p)
11368     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
11369
11370   if (info->cr_save_p)
11371     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
11372
11373   if (info->toc_save_p)
11374     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
11375
11376   if (info->vrsave_mask)
11377     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
11378
11379   if (info->push_p)
11380     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
11381
11382   if (info->calls_p)
11383     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
11384
11385   if (info->gp_save_offset)
11386     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
11387
11388   if (info->fp_save_offset)
11389     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
11390
11391   if (info->altivec_save_offset)
11392     fprintf (stderr, "\taltivec_save_offset = %5d\n",
11393              info->altivec_save_offset);
11394
11395   if (info->spe_gp_save_offset)
11396     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
11397              info->spe_gp_save_offset);
11398
11399   if (info->vrsave_save_offset)
11400     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
11401              info->vrsave_save_offset);
11402
11403   if (info->lr_save_offset)
11404     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
11405
11406   if (info->cr_save_offset)
11407     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
11408
11409   if (info->toc_save_offset)
11410     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
11411
11412   if (info->varargs_save_offset)
11413     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
11414
11415   if (info->total_size)
11416     fprintf (stderr, "\ttotal_size          = "HOST_WIDE_INT_PRINT_DEC"\n",
11417              info->total_size);
11418
11419   if (info->varargs_size)
11420     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
11421
11422   if (info->vars_size)
11423     fprintf (stderr, "\tvars_size           = "HOST_WIDE_INT_PRINT_DEC"\n",
11424              info->vars_size);
11425
11426   if (info->parm_size)
11427     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
11428
11429   if (info->fixed_size)
11430     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
11431
11432   if (info->gp_size)
11433     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
11434
11435   if (info->spe_gp_size)
11436     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
11437
11438   if (info->fp_size)
11439     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
11440
11441   if (info->altivec_size)
11442     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
11443
11444   if (info->vrsave_size)
11445     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
11446
11447   if (info->altivec_padding_size)
11448     fprintf (stderr, "\taltivec_padding_size= %5d\n",
11449              info->altivec_padding_size);
11450
11451   if (info->spe_padding_size)
11452     fprintf (stderr, "\tspe_padding_size    = %5d\n",
11453              info->spe_padding_size);
11454
11455   if (info->lr_size)
11456     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
11457
11458   if (info->cr_size)
11459     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
11460
11461   if (info->toc_size)
11462     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
11463
11464   if (info->save_size)
11465     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
11466
11467   if (info->reg_size != 4)
11468     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
11469
11470   fprintf (stderr, "\n");
11471 }
11472
11473 rtx
11474 rs6000_return_addr (int count, rtx frame)
11475 {
11476   /* Currently we don't optimize very well between prolog and body
11477      code and for PIC code the code can be actually quite bad, so
11478      don't try to be too clever here.  */
11479   if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
11480     {
11481       cfun->machine->ra_needs_full_frame = 1;
11482
11483       return
11484         gen_rtx_MEM
11485           (Pmode,
11486            memory_address
11487            (Pmode,
11488             plus_constant (copy_to_reg
11489                            (gen_rtx_MEM (Pmode,
11490                                          memory_address (Pmode, frame))),
11491                            RETURN_ADDRESS_OFFSET)));
11492     }
11493
11494   cfun->machine->ra_need_lr = 1;
11495   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
11496 }
11497
11498 /* Say whether a function is a candidate for sibcall handling or not.
11499    We do not allow indirect calls to be optimized into sibling calls.
11500    Also, we can't do it if there are any vector parameters; there's
11501    nowhere to put the VRsave code so it works; note that functions with
11502    vector parameters are required to have a prototype, so the argument
11503    type info must be available here.  (The tail recursion case can work
11504    with vector parameters, but there's no way to distinguish here.) */
11505 static bool
11506 rs6000_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
11507 {
11508   tree type;
11509   if (decl)
11510     {
11511       if (TARGET_ALTIVEC_VRSAVE)
11512         {
11513           for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
11514                type; type = TREE_CHAIN (type))
11515             {
11516               if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
11517                 return false;
11518             }
11519         }
11520       if (DEFAULT_ABI == ABI_DARWIN
11521           || (*targetm.binds_local_p) (decl))
11522         {
11523           tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
11524
11525           if (!lookup_attribute ("longcall", attr_list)
11526               || lookup_attribute ("shortcall", attr_list))
11527             return true;
11528         }
11529     }
11530   return false;
11531 }
11532
11533 static int
11534 rs6000_ra_ever_killed (void)
11535 {
11536   rtx top;
11537   rtx reg;
11538   rtx insn;
11539
11540   if (current_function_is_thunk)
11541     return 0;
11542
11543   /* regs_ever_live has LR marked as used if any sibcalls are present,
11544      but this should not force saving and restoring in the
11545      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
11546      clobbers LR, so that is inappropriate.  */
11547
11548   /* Also, the prologue can generate a store into LR that
11549      doesn't really count, like this:
11550
11551         move LR->R0
11552         bcl to set PIC register
11553         move LR->R31
11554         move R0->LR
11555
11556      When we're called from the epilogue, we need to avoid counting
11557      this as a store.  */
11558          
11559   push_topmost_sequence ();
11560   top = get_insns ();
11561   pop_topmost_sequence ();
11562   reg = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11563
11564   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
11565     {
11566       if (INSN_P (insn))
11567         {
11568           if (FIND_REG_INC_NOTE (insn, reg))
11569             return 1;
11570           else if (GET_CODE (insn) == CALL_INSN 
11571                    && !SIBLING_CALL_P (insn))
11572             return 1;
11573           else if (set_of (reg, insn) != NULL_RTX
11574                    && !prologue_epilogue_contains (insn))
11575             return 1;
11576         }
11577     }
11578   return 0;
11579 }
11580 \f
11581 /* Add a REG_MAYBE_DEAD note to the insn.  */
11582 static void
11583 rs6000_maybe_dead (rtx insn)
11584 {
11585   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
11586                                         const0_rtx,
11587                                         REG_NOTES (insn));
11588 }
11589
11590 /* Emit instructions needed to load the TOC register.
11591    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
11592    a constant pool; or for SVR4 -fpic.  */
11593
11594 void
11595 rs6000_emit_load_toc_table (int fromprolog)
11596 {
11597   rtx dest, insn;
11598   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
11599
11600   if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
11601     {
11602       rtx temp = (fromprolog
11603                   ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
11604                   : gen_reg_rtx (Pmode));
11605       insn = emit_insn (gen_load_toc_v4_pic_si (temp));
11606       if (fromprolog)
11607         rs6000_maybe_dead (insn);
11608       insn = emit_move_insn (dest, temp);
11609       if (fromprolog)
11610         rs6000_maybe_dead (insn);
11611     }
11612   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
11613     {
11614       char buf[30];
11615       rtx tempLR = (fromprolog
11616                     ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
11617                     : gen_reg_rtx (Pmode));
11618       rtx temp0 = (fromprolog
11619                    ? gen_rtx_REG (Pmode, 0)
11620                    : gen_reg_rtx (Pmode));
11621       rtx symF;
11622
11623       /* possibly create the toc section */
11624       if (! toc_initialized)
11625         {
11626           toc_section ();
11627           function_section (current_function_decl);
11628         }
11629
11630       if (fromprolog)
11631         {
11632           rtx symL;
11633
11634           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
11635           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11636
11637           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
11638           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11639
11640           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR,
11641                                                                symF)));
11642           rs6000_maybe_dead (emit_move_insn (dest, tempLR));
11643           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
11644                                                                symL,
11645                                                                symF)));
11646         }
11647       else
11648         {
11649           rtx tocsym;
11650           static int reload_toc_labelno = 0;
11651
11652           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
11653
11654           ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
11655           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11656
11657           emit_insn (gen_load_toc_v4_PIC_1b (tempLR, symF, tocsym));
11658           emit_move_insn (dest, tempLR);
11659           emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
11660         }
11661       insn = emit_insn (gen_addsi3 (dest, temp0, dest));
11662       if (fromprolog)
11663         rs6000_maybe_dead (insn);
11664     }
11665   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
11666     {
11667       /* This is for AIX code running in non-PIC ELF32.  */
11668       char buf[30];
11669       rtx realsym;
11670       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
11671       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11672
11673       insn = emit_insn (gen_elf_high (dest, realsym));
11674       if (fromprolog)
11675         rs6000_maybe_dead (insn);
11676       insn = emit_insn (gen_elf_low (dest, dest, realsym));
11677       if (fromprolog)
11678         rs6000_maybe_dead (insn);
11679     }
11680   else if (DEFAULT_ABI == ABI_AIX)
11681     {
11682       if (TARGET_32BIT)
11683         insn = emit_insn (gen_load_toc_aix_si (dest));
11684       else
11685         insn = emit_insn (gen_load_toc_aix_di (dest));
11686       if (fromprolog)
11687         rs6000_maybe_dead (insn);
11688     }
11689   else
11690     abort ();
11691 }
11692
11693 /* Emit instructions to restore the link register after determining where
11694    its value has been stored.  */
11695
11696 void
11697 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
11698 {
11699   rs6000_stack_t *info = rs6000_stack_info ();
11700   rtx operands[2];
11701
11702   operands[0] = source;
11703   operands[1] = scratch;
11704
11705   if (info->lr_save_p)
11706     {
11707       rtx frame_rtx = stack_pointer_rtx;
11708       HOST_WIDE_INT sp_offset = 0;
11709       rtx tmp;
11710
11711       if (frame_pointer_needed
11712           || current_function_calls_alloca
11713           || info->total_size > 32767)
11714         {
11715           emit_move_insn (operands[1], gen_rtx_MEM (Pmode, frame_rtx));
11716           frame_rtx = operands[1];
11717         }
11718       else if (info->push_p)
11719         sp_offset = info->total_size;
11720
11721       tmp = plus_constant (frame_rtx, info->lr_save_offset + sp_offset);
11722       tmp = gen_rtx_MEM (Pmode, tmp);
11723       emit_move_insn (tmp, operands[0]);
11724     }
11725   else
11726     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), operands[0]);
11727 }
11728
11729 static GTY(()) int set = -1;
11730
11731 int   
11732 get_TOC_alias_set (void)
11733 {
11734   if (set == -1)
11735     set = new_alias_set ();
11736   return set;
11737 }   
11738
11739 /* This returns nonzero if the current function uses the TOC.  This is
11740    determined by the presence of (use (unspec ... UNSPEC_TOC)), which
11741    is generated by the ABI_V4 load_toc_* patterns.  */
11742 #if TARGET_ELF
11743 static int
11744 uses_TOC (void) 
11745 {
11746   rtx insn;
11747
11748   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11749     if (INSN_P (insn))
11750       {
11751         rtx pat = PATTERN (insn);
11752         int i;
11753
11754         if (GET_CODE (pat) == PARALLEL) 
11755           for (i = 0; i < XVECLEN (pat, 0); i++)
11756             {
11757               rtx sub = XVECEXP (pat, 0, i);
11758               if (GET_CODE (sub) == USE)
11759                 {
11760                   sub = XEXP (sub, 0);
11761                   if (GET_CODE (sub) == UNSPEC
11762                       && XINT (sub, 1) == UNSPEC_TOC)
11763                     return 1;
11764                 }
11765             }
11766       }
11767   return 0;
11768 }
11769 #endif
11770
11771 rtx
11772 create_TOC_reference (rtx symbol) 
11773 {
11774   return gen_rtx_PLUS (Pmode, 
11775            gen_rtx_REG (Pmode, TOC_REGISTER),
11776              gen_rtx_CONST (Pmode, 
11777                gen_rtx_MINUS (Pmode, symbol, 
11778                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
11779 }
11780
11781 /* If _Unwind_* has been called from within the same module,
11782    toc register is not guaranteed to be saved to 40(1) on function
11783    entry.  Save it there in that case.  */
11784
11785 void
11786 rs6000_aix_emit_builtin_unwind_init (void)
11787 {
11788   rtx mem;
11789   rtx stack_top = gen_reg_rtx (Pmode);
11790   rtx opcode_addr = gen_reg_rtx (Pmode);
11791   rtx opcode = gen_reg_rtx (SImode);
11792   rtx tocompare = gen_reg_rtx (SImode);
11793   rtx no_toc_save_needed = gen_label_rtx ();
11794
11795   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
11796   emit_move_insn (stack_top, mem);
11797
11798   mem = gen_rtx_MEM (Pmode,
11799                      gen_rtx_PLUS (Pmode, stack_top,
11800                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
11801   emit_move_insn (opcode_addr, mem);
11802   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
11803   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014
11804                                            : 0xE8410028, SImode));
11805
11806   do_compare_rtx_and_jump (opcode, tocompare, EQ, 1,
11807                            SImode, NULL_RTX, NULL_RTX,
11808                            no_toc_save_needed);
11809
11810   mem = gen_rtx_MEM (Pmode,
11811                      gen_rtx_PLUS (Pmode, stack_top,
11812                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
11813   emit_move_insn (mem, gen_rtx_REG (Pmode, 2));
11814   emit_label (no_toc_save_needed);
11815 }
11816 \f
11817 /* This ties together stack memory (MEM with an alias set of
11818    rs6000_sr_alias_set) and the change to the stack pointer.  */
11819
11820 static void
11821 rs6000_emit_stack_tie (void)
11822 {
11823   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
11824
11825   set_mem_alias_set (mem, rs6000_sr_alias_set);
11826   emit_insn (gen_stack_tie (mem));
11827 }
11828
11829 /* Emit the correct code for allocating stack space, as insns.
11830    If COPY_R12, make sure a copy of the old frame is left in r12.
11831    The generated code may use hard register 0 as a temporary.  */
11832
11833 static void
11834 rs6000_emit_allocate_stack (HOST_WIDE_INT size, int copy_r12)
11835 {
11836   rtx insn;
11837   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11838   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
11839   rtx todec = GEN_INT (-size);
11840
11841   if (current_function_limit_stack)
11842     {
11843       if (REG_P (stack_limit_rtx)
11844           && REGNO (stack_limit_rtx) > 1 
11845           && REGNO (stack_limit_rtx) <= 31)
11846         {
11847           emit_insn (TARGET_32BIT
11848                      ? gen_addsi3 (tmp_reg,
11849                                    stack_limit_rtx,
11850                                    GEN_INT (size))
11851                      : gen_adddi3 (tmp_reg,
11852                                    stack_limit_rtx,
11853                                    GEN_INT (size)));
11854
11855           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11856                                     const0_rtx));
11857         }
11858       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
11859                && TARGET_32BIT
11860                && DEFAULT_ABI == ABI_V4)
11861         {
11862           rtx toload = gen_rtx_CONST (VOIDmode,
11863                                       gen_rtx_PLUS (Pmode, 
11864                                                     stack_limit_rtx, 
11865                                                     GEN_INT (size)));
11866
11867           emit_insn (gen_elf_high (tmp_reg, toload));
11868           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
11869           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11870                                     const0_rtx));
11871         }
11872       else
11873         warning ("stack limit expression is not supported");
11874     }
11875
11876   if (copy_r12 || ! TARGET_UPDATE)
11877     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
11878
11879   if (TARGET_UPDATE)
11880     {
11881       if (size > 32767)
11882         {
11883           /* Need a note here so that try_split doesn't get confused.  */
11884           if (get_last_insn() == NULL_RTX)
11885             emit_note (NOTE_INSN_DELETED);
11886           insn = emit_move_insn (tmp_reg, todec);
11887           try_split (PATTERN (insn), insn, 0);
11888           todec = tmp_reg;
11889         }
11890
11891       insn = emit_insn (TARGET_32BIT
11892                         ? gen_movsi_update (stack_reg, stack_reg,
11893                                             todec, stack_reg)
11894                         : gen_movdi_update (stack_reg, stack_reg, 
11895                                             todec, stack_reg));
11896     }
11897   else
11898     {
11899       insn = emit_insn (TARGET_32BIT
11900                         ? gen_addsi3 (stack_reg, stack_reg, todec)
11901                         : gen_adddi3 (stack_reg, stack_reg, todec));
11902       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
11903                       gen_rtx_REG (Pmode, 12));
11904     }
11905  
11906   RTX_FRAME_RELATED_P (insn) = 1;
11907   REG_NOTES (insn) = 
11908     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11909                        gen_rtx_SET (VOIDmode, stack_reg, 
11910                                     gen_rtx_PLUS (Pmode, stack_reg,
11911                                                   GEN_INT (-size))),
11912                        REG_NOTES (insn));
11913 }
11914
11915 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
11916    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
11917    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
11918    deduce these equivalences by itself so it wasn't necessary to hold
11919    its hand so much.  */
11920
11921 static void
11922 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val, 
11923                       rtx reg2, rtx rreg)
11924 {
11925   rtx real, temp;
11926
11927   /* copy_rtx will not make unique copies of registers, so we need to
11928      ensure we don't have unwanted sharing here.  */
11929   if (reg == reg2)
11930     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11931
11932   if (reg == rreg)
11933     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11934
11935   real = copy_rtx (PATTERN (insn));
11936
11937   if (reg2 != NULL_RTX)
11938     real = replace_rtx (real, reg2, rreg);
11939   
11940   real = replace_rtx (real, reg, 
11941                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
11942                                                         STACK_POINTER_REGNUM),
11943                                     GEN_INT (val)));
11944   
11945   /* We expect that 'real' is either a SET or a PARALLEL containing
11946      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
11947      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
11948
11949   if (GET_CODE (real) == SET)
11950     {
11951       rtx set = real;
11952       
11953       temp = simplify_rtx (SET_SRC (set));
11954       if (temp)
11955         SET_SRC (set) = temp;
11956       temp = simplify_rtx (SET_DEST (set));
11957       if (temp)
11958         SET_DEST (set) = temp;
11959       if (GET_CODE (SET_DEST (set)) == MEM)
11960         {
11961           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11962           if (temp)
11963             XEXP (SET_DEST (set), 0) = temp;
11964         }
11965     }
11966   else if (GET_CODE (real) == PARALLEL)
11967     {
11968       int i;
11969       for (i = 0; i < XVECLEN (real, 0); i++)
11970         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
11971           {
11972             rtx set = XVECEXP (real, 0, i);
11973             
11974             temp = simplify_rtx (SET_SRC (set));
11975             if (temp)
11976               SET_SRC (set) = temp;
11977             temp = simplify_rtx (SET_DEST (set));
11978             if (temp)
11979               SET_DEST (set) = temp;
11980             if (GET_CODE (SET_DEST (set)) == MEM)
11981               {
11982                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11983                 if (temp)
11984                   XEXP (SET_DEST (set), 0) = temp;
11985               }
11986             RTX_FRAME_RELATED_P (set) = 1;
11987           }
11988     }
11989   else
11990     abort ();
11991
11992   if (TARGET_SPE)
11993     real = spe_synthesize_frame_save (real);
11994
11995   RTX_FRAME_RELATED_P (insn) = 1;
11996   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11997                                         real,
11998                                         REG_NOTES (insn));
11999 }
12000
12001 /* Given an SPE frame note, return a PARALLEL of SETs with the
12002    original note, plus a synthetic register save.  */
12003
12004 static rtx
12005 spe_synthesize_frame_save (rtx real)
12006 {
12007   rtx synth, offset, reg, real2;
12008
12009   if (GET_CODE (real) != SET
12010       || GET_MODE (SET_SRC (real)) != V2SImode)
12011     return real;
12012
12013   /* For the SPE, registers saved in 64-bits, get a PARALLEL for their
12014      frame related note.  The parallel contains a set of the register
12015      being saved, and another set to a synthetic register (n+1200).
12016      This is so we can differentiate between 64-bit and 32-bit saves.
12017      Words cannot describe this nastiness.  */
12018
12019   if (GET_CODE (SET_DEST (real)) != MEM
12020       || GET_CODE (XEXP (SET_DEST (real), 0)) != PLUS
12021       || GET_CODE (SET_SRC (real)) != REG)
12022     abort ();
12023
12024   /* Transform:
12025        (set (mem (plus (reg x) (const y)))
12026             (reg z))
12027      into:
12028        (set (mem (plus (reg x) (const y+4)))
12029             (reg z+1200))
12030   */
12031
12032   real2 = copy_rtx (real);
12033   PUT_MODE (SET_DEST (real2), SImode);
12034   reg = SET_SRC (real2);
12035   real2 = replace_rtx (real2, reg, gen_rtx_REG (SImode, REGNO (reg)));
12036   synth = copy_rtx (real2);
12037
12038   if (BYTES_BIG_ENDIAN)
12039     {
12040       offset = XEXP (XEXP (SET_DEST (real2), 0), 1);
12041       real2 = replace_rtx (real2, offset, GEN_INT (INTVAL (offset) + 4));
12042     }
12043
12044   reg = SET_SRC (synth);
12045
12046   synth = replace_rtx (synth, reg,
12047                        gen_rtx_REG (SImode, REGNO (reg) + 1200));
12048
12049   offset = XEXP (XEXP (SET_DEST (synth), 0), 1);
12050   synth = replace_rtx (synth, offset,
12051                        GEN_INT (INTVAL (offset)
12052                                 + (BYTES_BIG_ENDIAN ? 0 : 4)));
12053
12054   RTX_FRAME_RELATED_P (synth) = 1;
12055   RTX_FRAME_RELATED_P (real2) = 1;
12056   if (BYTES_BIG_ENDIAN)
12057     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, synth, real2));
12058   else
12059     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, real2, synth));
12060
12061   return real;
12062 }
12063
12064 /* Returns an insn that has a vrsave set operation with the
12065    appropriate CLOBBERs.  */
12066
12067 static rtx
12068 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
12069 {
12070   int nclobs, i;
12071   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
12072   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
12073
12074   clobs[0]
12075     = gen_rtx_SET (VOIDmode,
12076                    vrsave,
12077                    gen_rtx_UNSPEC_VOLATILE (SImode,
12078                                             gen_rtvec (2, reg, vrsave),
12079                                             30));
12080
12081   nclobs = 1;
12082
12083   /* We need to clobber the registers in the mask so the scheduler
12084      does not move sets to VRSAVE before sets of AltiVec registers.
12085
12086      However, if the function receives nonlocal gotos, reload will set
12087      all call saved registers live.  We will end up with:
12088
12089         (set (reg 999) (mem))
12090         (parallel [ (set (reg vrsave) (unspec blah))
12091                     (clobber (reg 999))])
12092
12093      The clobber will cause the store into reg 999 to be dead, and
12094      flow will attempt to delete an epilogue insn.  In this case, we
12095      need an unspec use/set of the register.  */
12096
12097   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
12098     if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
12099       {
12100         if (!epiloguep || call_used_regs [i])
12101           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
12102                                              gen_rtx_REG (V4SImode, i));
12103         else
12104           {
12105             rtx reg = gen_rtx_REG (V4SImode, i);
12106
12107             clobs[nclobs++]
12108               = gen_rtx_SET (VOIDmode,
12109                              reg,
12110                              gen_rtx_UNSPEC (V4SImode,
12111                                              gen_rtvec (1, reg), 27));
12112           }
12113       }
12114
12115   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
12116
12117   for (i = 0; i < nclobs; ++i)
12118     XVECEXP (insn, 0, i) = clobs[i];
12119
12120   return insn;
12121 }
12122
12123 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
12124    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
12125
12126 static void
12127 emit_frame_save (rtx frame_reg, rtx frame_ptr, enum machine_mode mode, 
12128                  unsigned int regno, int offset, HOST_WIDE_INT total_size)
12129 {
12130   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
12131   rtx replacea, replaceb;
12132
12133   int_rtx = GEN_INT (offset);
12134
12135   /* Some cases that need register indexed addressing.  */
12136   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
12137       || (TARGET_SPE_ABI
12138           && SPE_VECTOR_MODE (mode)
12139           && !SPE_CONST_OFFSET_OK (offset)))
12140     {
12141       /* Whomever calls us must make sure r11 is available in the
12142          flow path of instructions in the prologue.  */
12143       offset_rtx = gen_rtx_REG (Pmode, 11);
12144       emit_move_insn (offset_rtx, int_rtx);
12145
12146       replacea = offset_rtx;
12147       replaceb = int_rtx;
12148     }
12149   else
12150     {
12151       offset_rtx = int_rtx;
12152       replacea = NULL_RTX;
12153       replaceb = NULL_RTX;
12154     }
12155
12156   reg = gen_rtx_REG (mode, regno);
12157   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
12158   mem = gen_rtx_MEM (mode, addr);
12159   set_mem_alias_set (mem, rs6000_sr_alias_set);
12160
12161   insn = emit_move_insn (mem, reg);
12162
12163   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
12164 }
12165
12166 /* Emit an offset memory reference suitable for a frame store, while
12167    converting to a valid addressing mode.  */
12168
12169 static rtx
12170 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
12171 {
12172   rtx int_rtx, offset_rtx;
12173
12174   int_rtx = GEN_INT (offset);
12175
12176   if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
12177     {
12178       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12179       emit_move_insn (offset_rtx, int_rtx);
12180     }
12181   else
12182     offset_rtx = int_rtx;
12183
12184   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
12185 }
12186
12187 /* Emit function prologue as insns.  */
12188
12189 void
12190 rs6000_emit_prologue (void)
12191 {
12192   rs6000_stack_t *info = rs6000_stack_info ();
12193   enum machine_mode reg_mode = Pmode;
12194   int reg_size = TARGET_32BIT ? 4 : 8;
12195   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
12196   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
12197   rtx frame_reg_rtx = sp_reg_rtx;
12198   rtx cr_save_rtx = NULL_RTX;
12199   rtx insn;
12200   int saving_FPRs_inline;
12201   int using_store_multiple;
12202   HOST_WIDE_INT sp_offset = 0;
12203   
12204    if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
12205      {
12206        reg_mode = V2SImode;
12207        reg_size = 8;
12208      }
12209
12210   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
12211                           && (!TARGET_SPE_ABI
12212                               || info->spe_64bit_regs_used == 0)
12213                           && info->first_gp_reg_save < 31);
12214   saving_FPRs_inline = (info->first_fp_reg_save == 64
12215                         || FP_SAVE_INLINE (info->first_fp_reg_save)
12216                         || current_function_calls_eh_return
12217                         || cfun->machine->ra_need_lr);
12218
12219   /* For V.4, update stack before we do any saving and set back pointer.  */
12220   if (info->push_p
12221       && (DEFAULT_ABI == ABI_V4
12222           || current_function_calls_eh_return))
12223     {
12224       if (info->total_size < 32767)
12225         sp_offset = info->total_size;
12226       else
12227         frame_reg_rtx = frame_ptr_rtx;
12228       rs6000_emit_allocate_stack (info->total_size, 
12229                                   (frame_reg_rtx != sp_reg_rtx
12230                                    && (info->cr_save_p
12231                                        || info->lr_save_p
12232                                        || info->first_fp_reg_save < 64
12233                                        || info->first_gp_reg_save < 32
12234                                        )));
12235       if (frame_reg_rtx != sp_reg_rtx)
12236         rs6000_emit_stack_tie ();
12237     }
12238
12239   /* Save AltiVec registers if needed.  */
12240   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
12241     {
12242       int i;
12243
12244       /* There should be a non inline version of this, for when we
12245          are saving lots of vector registers.  */
12246       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
12247         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
12248           {
12249             rtx areg, savereg, mem;
12250             int offset;
12251
12252             offset = info->altivec_save_offset + sp_offset
12253               + 16 * (i - info->first_altivec_reg_save);
12254
12255             savereg = gen_rtx_REG (V4SImode, i);
12256
12257             areg = gen_rtx_REG (Pmode, 0);
12258             emit_move_insn (areg, GEN_INT (offset));
12259
12260             /* AltiVec addressing mode is [reg+reg].  */
12261             mem = gen_rtx_MEM (V4SImode,
12262                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
12263                                
12264             set_mem_alias_set (mem, rs6000_sr_alias_set);
12265
12266             insn = emit_move_insn (mem, savereg);
12267
12268             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12269                                   areg, GEN_INT (offset));
12270           }
12271     }
12272
12273   /* VRSAVE is a bit vector representing which AltiVec registers
12274      are used.  The OS uses this to determine which vector
12275      registers to save on a context switch.  We need to save
12276      VRSAVE on the stack frame, add whatever AltiVec registers we
12277      used in this function, and do the corresponding magic in the
12278      epilogue.  */
12279
12280   if (TARGET_ALTIVEC && TARGET_ALTIVEC_VRSAVE
12281       && info->vrsave_mask != 0)
12282     {
12283       rtx reg, mem, vrsave;
12284       int offset;
12285
12286       /* Get VRSAVE onto a GPR.  */
12287       reg = gen_rtx_REG (SImode, 12);
12288       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
12289       if (TARGET_MACHO)
12290         emit_insn (gen_get_vrsave_internal (reg));
12291       else
12292         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
12293
12294       /* Save VRSAVE.  */
12295       offset = info->vrsave_save_offset + sp_offset;
12296       mem
12297         = gen_rtx_MEM (SImode,
12298                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
12299       set_mem_alias_set (mem, rs6000_sr_alias_set);
12300       insn = emit_move_insn (mem, reg);
12301
12302       /* Include the registers in the mask.  */
12303       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
12304
12305       insn = emit_insn (generate_set_vrsave (reg, info, 0));
12306     }
12307
12308   /* If we use the link register, get it into r0.  */
12309   if (info->lr_save_p)
12310     emit_move_insn (gen_rtx_REG (Pmode, 0),
12311                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
12312
12313   /* If we need to save CR, put it into r12.  */
12314   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
12315     {
12316       cr_save_rtx = gen_rtx_REG (SImode, 12);
12317       emit_insn (gen_movesi_from_cr (cr_save_rtx));
12318     }
12319
12320   /* Do any required saving of fpr's.  If only one or two to save, do
12321      it ourselves.  Otherwise, call function.  */
12322   if (saving_FPRs_inline)
12323     {
12324       int i;
12325       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12326         if ((regs_ever_live[info->first_fp_reg_save+i] 
12327              && ! call_used_regs[info->first_fp_reg_save+i]))
12328           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
12329                            info->first_fp_reg_save + i,
12330                            info->fp_save_offset + sp_offset + 8 * i,
12331                            info->total_size);
12332     }
12333   else if (info->first_fp_reg_save != 64)
12334     {
12335       int i;
12336       char rname[30];
12337       const char *alloc_rname;
12338       rtvec p;
12339       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
12340       
12341       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
12342                                           gen_rtx_REG (Pmode, 
12343                                                        LINK_REGISTER_REGNUM));
12344       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
12345                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
12346       alloc_rname = ggc_strdup (rname);
12347       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
12348                                       gen_rtx_SYMBOL_REF (Pmode,
12349                                                           alloc_rname));
12350       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12351         {
12352           rtx addr, reg, mem;
12353           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
12354           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12355                                GEN_INT (info->fp_save_offset 
12356                                         + sp_offset + 8*i));
12357           mem = gen_rtx_MEM (DFmode, addr);
12358           set_mem_alias_set (mem, rs6000_sr_alias_set);
12359
12360           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
12361         }
12362       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12363       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
12364                             NULL_RTX, NULL_RTX);
12365     }
12366
12367   /* Save GPRs.  This is done as a PARALLEL if we are using
12368      the store-multiple instructions.  */
12369   if (using_store_multiple)
12370     {
12371       rtvec p;
12372       int i;
12373       p = rtvec_alloc (32 - info->first_gp_reg_save);
12374       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12375         {
12376           rtx addr, reg, mem;
12377           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
12378           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12379                                GEN_INT (info->gp_save_offset 
12380                                         + sp_offset 
12381                                         + reg_size * i));
12382           mem = gen_rtx_MEM (reg_mode, addr);
12383           set_mem_alias_set (mem, rs6000_sr_alias_set);
12384
12385           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
12386         }
12387       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12388       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
12389                             NULL_RTX, NULL_RTX);
12390     }
12391   else
12392     {
12393       int i;
12394       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12395         if ((regs_ever_live[info->first_gp_reg_save+i] 
12396              && ! call_used_regs[info->first_gp_reg_save+i])
12397             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
12398                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
12399                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
12400           {
12401             rtx addr, reg, mem;
12402             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
12403
12404             if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
12405               {
12406                 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
12407                 rtx b;
12408
12409                 if (!SPE_CONST_OFFSET_OK (offset))
12410                   {
12411                     b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12412                     emit_move_insn (b, GEN_INT (offset));
12413                   }
12414                 else
12415                   b = GEN_INT (offset);
12416
12417                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
12418                 mem = gen_rtx_MEM (V2SImode, addr);
12419                 set_mem_alias_set (mem, rs6000_sr_alias_set);
12420                 insn = emit_move_insn (mem, reg);
12421
12422                 if (GET_CODE (b) == CONST_INT)
12423                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12424                                         NULL_RTX, NULL_RTX);
12425                 else
12426                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12427                                         b, GEN_INT (offset));
12428               }
12429             else
12430               {
12431                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12432                                      GEN_INT (info->gp_save_offset 
12433                                               + sp_offset 
12434                                               + reg_size * i));
12435                 mem = gen_rtx_MEM (reg_mode, addr);
12436                 set_mem_alias_set (mem, rs6000_sr_alias_set);
12437
12438                 insn = emit_move_insn (mem, reg);
12439                 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
12440                                       NULL_RTX, NULL_RTX);
12441               }
12442           }
12443     }
12444
12445   /* ??? There's no need to emit actual instructions here, but it's the
12446      easiest way to get the frame unwind information emitted.  */
12447   if (current_function_calls_eh_return)
12448     {
12449       unsigned int i, regno;
12450
12451       /* In AIX ABI we need to pretend we save r2 here.  */
12452       if (TARGET_AIX)
12453         {
12454           rtx addr, reg, mem;
12455
12456           reg = gen_rtx_REG (reg_mode, 2);
12457           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12458                                GEN_INT (sp_offset + 5 * reg_size));
12459           mem = gen_rtx_MEM (reg_mode, addr);
12460           set_mem_alias_set (mem, rs6000_sr_alias_set);
12461
12462           insn = emit_move_insn (mem, reg);
12463           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
12464                                 NULL_RTX, NULL_RTX);
12465           PATTERN (insn) = gen_blockage ();
12466         }
12467
12468       for (i = 0; ; ++i)
12469         {
12470           regno = EH_RETURN_DATA_REGNO (i);
12471           if (regno == INVALID_REGNUM)
12472             break;
12473
12474           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
12475                            info->ehrd_offset + sp_offset
12476                            + reg_size * (int) i,
12477                            info->total_size);
12478         }
12479     }
12480
12481   /* Save lr if we used it.  */
12482   if (info->lr_save_p)
12483     {
12484       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12485                                GEN_INT (info->lr_save_offset + sp_offset));
12486       rtx reg = gen_rtx_REG (Pmode, 0);
12487       rtx mem = gen_rtx_MEM (Pmode, addr);
12488       /* This should not be of rs6000_sr_alias_set, because of
12489          __builtin_return_address.  */
12490       
12491       insn = emit_move_insn (mem, reg);
12492       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
12493                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
12494     }
12495
12496   /* Save CR if we use any that must be preserved.  */
12497   if (info->cr_save_p)
12498     {
12499       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12500                                GEN_INT (info->cr_save_offset + sp_offset));
12501       rtx mem = gen_rtx_MEM (SImode, addr);
12502
12503       set_mem_alias_set (mem, rs6000_sr_alias_set);
12504
12505       /* If r12 was used to hold the original sp, copy cr into r0 now
12506          that it's free.  */
12507       if (REGNO (frame_reg_rtx) == 12)
12508         {
12509           cr_save_rtx = gen_rtx_REG (SImode, 0);
12510           emit_insn (gen_movesi_from_cr (cr_save_rtx));
12511         }
12512       insn = emit_move_insn (mem, cr_save_rtx);
12513
12514       /* Now, there's no way that dwarf2out_frame_debug_expr is going
12515          to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
12516          But that's OK.  All we have to do is specify that _one_ condition
12517          code register is saved in this stack slot.  The thrower's epilogue
12518          will then restore all the call-saved registers.
12519          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
12520       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
12521                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
12522     }
12523
12524   /* Update stack and set back pointer unless this is V.4, 
12525      for which it was done previously.  */
12526   if (info->push_p
12527       && !(DEFAULT_ABI == ABI_V4 || current_function_calls_eh_return))
12528     rs6000_emit_allocate_stack (info->total_size, FALSE);
12529
12530   /* Set frame pointer, if needed.  */
12531   if (frame_pointer_needed)
12532     {
12533       insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), 
12534                              sp_reg_rtx);
12535       RTX_FRAME_RELATED_P (insn) = 1;
12536     }
12537
12538   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
12539   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
12540       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
12541           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
12542   {
12543     /* If emit_load_toc_table will use the link register, we need to save
12544        it.  We use R12 for this purpose because emit_load_toc_table
12545        can use register 0.  This allows us to use a plain 'blr' to return
12546        from the procedure more often.  */
12547     int save_LR_around_toc_setup = (TARGET_ELF
12548                                     && DEFAULT_ABI != ABI_AIX
12549                                     && flag_pic
12550                                     && ! info->lr_save_p
12551                                     && EXIT_BLOCK_PTR->pred != NULL);
12552     if (save_LR_around_toc_setup)
12553       {
12554         rtx lr = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
12555         rs6000_maybe_dead (emit_move_insn (frame_ptr_rtx, lr));
12556         rs6000_emit_load_toc_table (TRUE);
12557         rs6000_maybe_dead (emit_move_insn (lr, frame_ptr_rtx));
12558       }
12559     else
12560       rs6000_emit_load_toc_table (TRUE);
12561   }
12562
12563 #if TARGET_MACHO
12564   if (DEFAULT_ABI == ABI_DARWIN
12565       && flag_pic && current_function_uses_pic_offset_table)
12566     {
12567       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
12568       const char *picbase = machopic_function_base_name ();
12569       rtx src = gen_rtx_SYMBOL_REF (Pmode, picbase);
12570
12571       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest, src)));
12572
12573       rs6000_maybe_dead (
12574         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
12575                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
12576     }
12577 #endif
12578 }
12579
12580 /* Write function prologue.  */
12581
12582 static void
12583 rs6000_output_function_prologue (FILE *file, 
12584                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
12585 {
12586   rs6000_stack_t *info = rs6000_stack_info ();
12587
12588   if (TARGET_DEBUG_STACK)
12589     debug_stack_info (info);
12590
12591   /* Write .extern for any function we will call to save and restore
12592      fp values.  */
12593   if (info->first_fp_reg_save < 64
12594       && !FP_SAVE_INLINE (info->first_fp_reg_save))
12595     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
12596              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
12597              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
12598              RESTORE_FP_SUFFIX);
12599
12600   /* Write .extern for AIX common mode routines, if needed.  */
12601   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
12602     {
12603       fputs ("\t.extern __mulh\n", file);
12604       fputs ("\t.extern __mull\n", file);
12605       fputs ("\t.extern __divss\n", file);
12606       fputs ("\t.extern __divus\n", file);
12607       fputs ("\t.extern __quoss\n", file);
12608       fputs ("\t.extern __quous\n", file);
12609       common_mode_defined = 1;
12610     }
12611
12612   if (! HAVE_prologue)
12613     {
12614       start_sequence ();
12615
12616       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
12617          the "toplevel" insn chain.  */
12618       emit_note (NOTE_INSN_DELETED);
12619       rs6000_emit_prologue ();
12620       emit_note (NOTE_INSN_DELETED);
12621
12622       /* Expand INSN_ADDRESSES so final() doesn't crash.  */
12623       {
12624         rtx insn;
12625         unsigned addr = 0;
12626         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
12627           {
12628             INSN_ADDRESSES_NEW (insn, addr);
12629             addr += 4;
12630           }
12631       }
12632
12633       if (TARGET_DEBUG_STACK)
12634         debug_rtx_list (get_insns (), 100);
12635       final (get_insns (), file, FALSE, FALSE);
12636       end_sequence ();
12637     }
12638
12639   rs6000_pic_labelno++;
12640 }
12641   
12642 /* Emit function epilogue as insns.
12643
12644    At present, dwarf2out_frame_debug_expr doesn't understand
12645    register restores, so we don't bother setting RTX_FRAME_RELATED_P
12646    anywhere in the epilogue.  Most of the insns below would in any case
12647    need special notes to explain where r11 is in relation to the stack.  */
12648
12649 void
12650 rs6000_emit_epilogue (int sibcall)
12651 {
12652   rs6000_stack_t *info;
12653   int restoring_FPRs_inline;
12654   int using_load_multiple;
12655   int using_mfcr_multiple;
12656   int use_backchain_to_restore_sp;
12657   int sp_offset = 0;
12658   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
12659   rtx frame_reg_rtx = sp_reg_rtx;
12660   enum machine_mode reg_mode = Pmode;
12661   int reg_size = TARGET_32BIT ? 4 : 8;
12662   int i;
12663
12664   info = rs6000_stack_info ();
12665
12666   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
12667     {
12668       reg_mode = V2SImode;
12669       reg_size = 8;
12670     }
12671
12672   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
12673                          && (!TARGET_SPE_ABI
12674                              || info->spe_64bit_regs_used == 0)
12675                          && info->first_gp_reg_save < 31);
12676   restoring_FPRs_inline = (sibcall
12677                            || current_function_calls_eh_return
12678                            || info->first_fp_reg_save == 64
12679                            || FP_SAVE_INLINE (info->first_fp_reg_save));
12680   use_backchain_to_restore_sp = (frame_pointer_needed 
12681                                  || current_function_calls_alloca
12682                                  || info->total_size > 32767);
12683   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
12684                          || rs6000_cpu == PROCESSOR_PPC603
12685                          || rs6000_cpu == PROCESSOR_PPC750
12686                          || optimize_size);
12687
12688   /* If we have a frame pointer, a call to alloca,  or a large stack
12689      frame, restore the old stack pointer using the backchain.  Otherwise,
12690      we know what size to update it with.  */
12691   if (use_backchain_to_restore_sp)
12692     {
12693       /* Under V.4, don't reset the stack pointer until after we're done
12694          loading the saved registers.  */
12695       if (DEFAULT_ABI == ABI_V4)
12696         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
12697
12698       emit_move_insn (frame_reg_rtx,
12699                       gen_rtx_MEM (Pmode, sp_reg_rtx));
12700       
12701     }
12702   else if (info->push_p)
12703     {
12704       if (DEFAULT_ABI == ABI_V4
12705           || current_function_calls_eh_return)
12706         sp_offset = info->total_size;
12707       else
12708         {
12709           emit_insn (TARGET_32BIT
12710                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12711                                    GEN_INT (info->total_size))
12712                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12713                                    GEN_INT (info->total_size)));
12714         }
12715     }
12716   
12717   /* Restore AltiVec registers if needed.  */
12718   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
12719     {
12720       int i;
12721
12722       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
12723         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
12724           {
12725             rtx addr, areg, mem;
12726
12727             areg = gen_rtx_REG (Pmode, 0);
12728             emit_move_insn
12729               (areg, GEN_INT (info->altivec_save_offset
12730                               + sp_offset
12731                               + 16 * (i - info->first_altivec_reg_save)));
12732
12733             /* AltiVec addressing mode is [reg+reg].  */
12734             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
12735             mem = gen_rtx_MEM (V4SImode, addr);
12736             set_mem_alias_set (mem, rs6000_sr_alias_set);
12737
12738             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
12739           }
12740     }
12741
12742   /* Restore VRSAVE if needed.  */
12743   if (TARGET_ALTIVEC && TARGET_ALTIVEC_VRSAVE
12744       && info->vrsave_mask != 0)
12745     {
12746       rtx addr, mem, reg;
12747
12748       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12749                            GEN_INT (info->vrsave_save_offset + sp_offset));
12750       mem = gen_rtx_MEM (SImode, addr);
12751       set_mem_alias_set (mem, rs6000_sr_alias_set);
12752       reg = gen_rtx_REG (SImode, 12);
12753       emit_move_insn (reg, mem);
12754
12755       emit_insn (generate_set_vrsave (reg, info, 1));
12756     }
12757
12758   /* Get the old lr if we saved it.  */
12759   if (info->lr_save_p)
12760     {
12761       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
12762                                       info->lr_save_offset + sp_offset);
12763
12764       set_mem_alias_set (mem, rs6000_sr_alias_set);
12765
12766       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
12767     }
12768   
12769   /* Get the old cr if we saved it.  */
12770   if (info->cr_save_p)
12771     {
12772       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12773                                GEN_INT (info->cr_save_offset + sp_offset));
12774       rtx mem = gen_rtx_MEM (SImode, addr);
12775
12776       set_mem_alias_set (mem, rs6000_sr_alias_set);
12777
12778       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
12779     }
12780   
12781   /* Set LR here to try to overlap restores below.  */
12782   if (info->lr_save_p)
12783     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
12784                     gen_rtx_REG (Pmode, 0));
12785   
12786   /* Load exception handler data registers, if needed.  */
12787   if (current_function_calls_eh_return)
12788     {
12789       unsigned int i, regno;
12790
12791       if (TARGET_AIX)
12792         {
12793           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12794                                    GEN_INT (sp_offset + 5 * reg_size));
12795           rtx mem = gen_rtx_MEM (reg_mode, addr);
12796
12797           set_mem_alias_set (mem, rs6000_sr_alias_set);
12798
12799           emit_move_insn (gen_rtx_REG (reg_mode, 2), mem);
12800         }
12801
12802       for (i = 0; ; ++i)
12803         {
12804           rtx mem;
12805
12806           regno = EH_RETURN_DATA_REGNO (i);
12807           if (regno == INVALID_REGNUM)
12808             break;
12809
12810           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
12811                                       info->ehrd_offset + sp_offset
12812                                       + reg_size * (int) i);
12813           set_mem_alias_set (mem, rs6000_sr_alias_set);
12814
12815           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
12816         }
12817     }
12818   
12819   /* Restore GPRs.  This is done as a PARALLEL if we are using
12820      the load-multiple instructions.  */
12821   if (using_load_multiple)
12822     {
12823       rtvec p;
12824       p = rtvec_alloc (32 - info->first_gp_reg_save);
12825       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12826         {
12827           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12828                                    GEN_INT (info->gp_save_offset 
12829                                             + sp_offset 
12830                                             + reg_size * i));
12831           rtx mem = gen_rtx_MEM (reg_mode, addr);
12832
12833           set_mem_alias_set (mem, rs6000_sr_alias_set);
12834
12835           RTVEC_ELT (p, i) = 
12836             gen_rtx_SET (VOIDmode,
12837                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
12838                          mem);
12839         }
12840       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12841     }
12842   else
12843     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12844       if ((regs_ever_live[info->first_gp_reg_save+i] 
12845            && ! call_used_regs[info->first_gp_reg_save+i])
12846           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
12847               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
12848                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
12849         {
12850           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12851                                    GEN_INT (info->gp_save_offset 
12852                                             + sp_offset 
12853                                             + reg_size * i));
12854           rtx mem = gen_rtx_MEM (reg_mode, addr);
12855
12856           /* Restore 64-bit quantities for SPE.  */
12857           if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
12858             {
12859               int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
12860               rtx b;
12861
12862               if (!SPE_CONST_OFFSET_OK (offset))
12863                 {
12864                   b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12865                   emit_move_insn (b, GEN_INT (offset));
12866                 }
12867               else
12868                 b = GEN_INT (offset);
12869
12870               addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
12871               mem = gen_rtx_MEM (V2SImode, addr);
12872             }
12873
12874           set_mem_alias_set (mem, rs6000_sr_alias_set);
12875
12876           emit_move_insn (gen_rtx_REG (reg_mode, 
12877                                        info->first_gp_reg_save + i), mem);
12878         }
12879
12880   /* Restore fpr's if we need to do it without calling a function.  */
12881   if (restoring_FPRs_inline)
12882     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12883       if ((regs_ever_live[info->first_fp_reg_save+i] 
12884            && ! call_used_regs[info->first_fp_reg_save+i]))
12885         {
12886           rtx addr, mem;
12887           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12888                                GEN_INT (info->fp_save_offset 
12889                                         + sp_offset 
12890                                         + 8 * i));
12891           mem = gen_rtx_MEM (DFmode, addr);
12892           set_mem_alias_set (mem, rs6000_sr_alias_set);
12893
12894           emit_move_insn (gen_rtx_REG (DFmode, 
12895                                        info->first_fp_reg_save + i),
12896                           mem);
12897         }
12898
12899   /* If we saved cr, restore it here.  Just those that were used.  */
12900   if (info->cr_save_p)
12901     {
12902       rtx r12_rtx = gen_rtx_REG (SImode, 12);
12903       int count = 0;
12904       
12905       if (using_mfcr_multiple)
12906         {
12907           for (i = 0; i < 8; i++)
12908             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12909               count++;
12910           if (count == 0)
12911             abort ();
12912         }
12913
12914       if (using_mfcr_multiple && count > 1)
12915         {
12916           rtvec p;
12917           int ndx;
12918           
12919           p = rtvec_alloc (count);
12920
12921           ndx = 0;
12922           for (i = 0; i < 8; i++)
12923             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12924               {
12925                 rtvec r = rtvec_alloc (2);
12926                 RTVEC_ELT (r, 0) = r12_rtx;
12927                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
12928                 RTVEC_ELT (p, ndx) =
12929                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
12930                                gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
12931                 ndx++;
12932               }
12933           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12934           if (ndx != count)
12935             abort ();
12936         }
12937       else
12938         for (i = 0; i < 8; i++)
12939           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12940             {
12941               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
12942                                                            CR0_REGNO+i),
12943                                               r12_rtx));
12944             }
12945     }
12946
12947   /* If this is V.4, unwind the stack pointer after all of the loads
12948      have been done.  We need to emit a block here so that sched
12949      doesn't decide to move the sp change before the register restores
12950      (which may not have any obvious dependency on the stack).  This
12951      doesn't hurt performance, because there is no scheduling that can
12952      be done after this point.  */
12953   if (DEFAULT_ABI == ABI_V4
12954       || current_function_calls_eh_return)
12955     {
12956       if (frame_reg_rtx != sp_reg_rtx)
12957           rs6000_emit_stack_tie ();
12958
12959       if (use_backchain_to_restore_sp)
12960         {
12961           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
12962         }
12963       else if (sp_offset != 0)
12964         {
12965           emit_insn (TARGET_32BIT
12966                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12967                                    GEN_INT (sp_offset))
12968                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12969                                    GEN_INT (sp_offset)));
12970         }
12971     }
12972
12973   if (current_function_calls_eh_return)
12974     {
12975       rtx sa = EH_RETURN_STACKADJ_RTX;
12976       emit_insn (TARGET_32BIT
12977                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
12978                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
12979     }
12980
12981   if (!sibcall)
12982     {
12983       rtvec p;
12984       if (! restoring_FPRs_inline)
12985         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
12986       else
12987         p = rtvec_alloc (2);
12988
12989       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
12990       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
12991                                       gen_rtx_REG (Pmode, 
12992                                                    LINK_REGISTER_REGNUM));
12993
12994       /* If we have to restore more than two FP registers, branch to the
12995          restore function.  It will return to our caller.  */
12996       if (! restoring_FPRs_inline)
12997         {
12998           int i;
12999           char rname[30];
13000           const char *alloc_rname;
13001
13002           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
13003                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
13004           alloc_rname = ggc_strdup (rname);
13005           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
13006                                           gen_rtx_SYMBOL_REF (Pmode,
13007                                                               alloc_rname));
13008
13009           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
13010             {
13011               rtx addr, mem;
13012               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
13013                                    GEN_INT (info->fp_save_offset + 8*i));
13014               mem = gen_rtx_MEM (DFmode, addr);
13015               set_mem_alias_set (mem, rs6000_sr_alias_set);
13016
13017               RTVEC_ELT (p, i+3) = 
13018                 gen_rtx_SET (VOIDmode,
13019                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
13020                              mem);
13021             }
13022         }
13023       
13024       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
13025     }
13026 }
13027
13028 /* Write function epilogue.  */
13029
13030 static void
13031 rs6000_output_function_epilogue (FILE *file, 
13032                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13033 {
13034   rs6000_stack_t *info = rs6000_stack_info ();
13035
13036   if (! HAVE_epilogue)
13037     {
13038       rtx insn = get_last_insn ();
13039       /* If the last insn was a BARRIER, we don't have to write anything except
13040          the trace table.  */
13041       if (GET_CODE (insn) == NOTE)
13042         insn = prev_nonnote_insn (insn);
13043       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
13044         {
13045           /* This is slightly ugly, but at least we don't have two
13046              copies of the epilogue-emitting code.  */
13047           start_sequence ();
13048
13049           /* A NOTE_INSN_DELETED is supposed to be at the start
13050              and end of the "toplevel" insn chain.  */
13051           emit_note (NOTE_INSN_DELETED);
13052           rs6000_emit_epilogue (FALSE);
13053           emit_note (NOTE_INSN_DELETED);
13054
13055           /* Expand INSN_ADDRESSES so final() doesn't crash.  */
13056           {
13057             rtx insn;
13058             unsigned addr = 0;
13059             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
13060               {
13061                 INSN_ADDRESSES_NEW (insn, addr);
13062                 addr += 4;
13063               }
13064           }
13065
13066           if (TARGET_DEBUG_STACK)
13067             debug_rtx_list (get_insns (), 100);
13068           final (get_insns (), file, FALSE, FALSE);
13069           end_sequence ();
13070         }
13071     }
13072
13073 #if TARGET_MACHO
13074   macho_branch_islands ();
13075   /* Mach-O doesn't support labels at the end of objects, so if
13076      it looks like we might want one, insert a NOP.  */
13077   {
13078     rtx insn = get_last_insn ();
13079     while (insn
13080            && NOTE_P (insn)
13081            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
13082       insn = PREV_INSN (insn);
13083     if (insn 
13084         && (LABEL_P (insn) 
13085             || (NOTE_P (insn)
13086                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
13087       fputs ("\tnop\n", file);
13088   }
13089 #endif
13090
13091   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
13092      on its format.
13093
13094      We don't output a traceback table if -finhibit-size-directive was
13095      used.  The documentation for -finhibit-size-directive reads
13096      ``don't output a @code{.size} assembler directive, or anything
13097      else that would cause trouble if the function is split in the
13098      middle, and the two halves are placed at locations far apart in
13099      memory.''  The traceback table has this property, since it
13100      includes the offset from the start of the function to the
13101      traceback table itself.
13102
13103      System V.4 Powerpc's (and the embedded ABI derived from it) use a
13104      different traceback table.  */
13105   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
13106       && rs6000_traceback != traceback_none)
13107     {
13108       const char *fname = NULL;
13109       const char *language_string = lang_hooks.name;
13110       int fixed_parms = 0, float_parms = 0, parm_info = 0;
13111       int i;
13112       int optional_tbtab;
13113
13114       if (rs6000_traceback == traceback_full)
13115         optional_tbtab = 1;
13116       else if (rs6000_traceback == traceback_part)
13117         optional_tbtab = 0;
13118       else
13119         optional_tbtab = !optimize_size && !TARGET_ELF;
13120
13121       if (optional_tbtab)
13122         {
13123           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13124           while (*fname == '.') /* V.4 encodes . in the name */
13125             fname++;
13126
13127           /* Need label immediately before tbtab, so we can compute
13128              its offset from the function start.  */
13129           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
13130           ASM_OUTPUT_LABEL (file, fname);
13131         }
13132
13133       /* The .tbtab pseudo-op can only be used for the first eight
13134          expressions, since it can't handle the possibly variable
13135          length fields that follow.  However, if you omit the optional
13136          fields, the assembler outputs zeros for all optional fields
13137          anyways, giving each variable length field is minimum length
13138          (as defined in sys/debug.h).  Thus we can not use the .tbtab
13139          pseudo-op at all.  */
13140
13141       /* An all-zero word flags the start of the tbtab, for debuggers
13142          that have to find it by searching forward from the entry
13143          point or from the current pc.  */
13144       fputs ("\t.long 0\n", file);
13145
13146       /* Tbtab format type.  Use format type 0.  */
13147       fputs ("\t.byte 0,", file);
13148
13149       /* Language type.  Unfortunately, there does not seem to be any
13150          official way to discover the language being compiled, so we
13151          use language_string.
13152          C is 0.  Fortran is 1.  Pascal is 2.  Ada is 3.  C++ is 9.
13153          Java is 13.  Objective-C is 14.  */
13154       if (! strcmp (language_string, "GNU C"))
13155         i = 0;
13156       else if (! strcmp (language_string, "GNU F77")
13157                || ! strcmp (language_string, "GNU F95"))
13158         i = 1;
13159       else if (! strcmp (language_string, "GNU Pascal"))
13160         i = 2;
13161       else if (! strcmp (language_string, "GNU Ada"))
13162         i = 3;
13163       else if (! strcmp (language_string, "GNU C++"))
13164         i = 9;
13165       else if (! strcmp (language_string, "GNU Java"))
13166         i = 13;
13167       else if (! strcmp (language_string, "GNU Objective-C"))
13168         i = 14;
13169       else
13170         abort ();
13171       fprintf (file, "%d,", i);
13172
13173       /* 8 single bit fields: global linkage (not set for C extern linkage,
13174          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
13175          from start of procedure stored in tbtab, internal function, function
13176          has controlled storage, function has no toc, function uses fp,
13177          function logs/aborts fp operations.  */
13178       /* Assume that fp operations are used if any fp reg must be saved.  */
13179       fprintf (file, "%d,",
13180                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
13181
13182       /* 6 bitfields: function is interrupt handler, name present in
13183          proc table, function calls alloca, on condition directives
13184          (controls stack walks, 3 bits), saves condition reg, saves
13185          link reg.  */
13186       /* The `function calls alloca' bit seems to be set whenever reg 31 is
13187          set up as a frame pointer, even when there is no alloca call.  */
13188       fprintf (file, "%d,",
13189                ((optional_tbtab << 6)
13190                 | ((optional_tbtab & frame_pointer_needed) << 5)
13191                 | (info->cr_save_p << 1)
13192                 | (info->lr_save_p)));
13193
13194       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
13195          (6 bits).  */
13196       fprintf (file, "%d,",
13197                (info->push_p << 7) | (64 - info->first_fp_reg_save));
13198
13199       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
13200       fprintf (file, "%d,", (32 - first_reg_to_save ()));
13201
13202       if (optional_tbtab)
13203         {
13204           /* Compute the parameter info from the function decl argument
13205              list.  */
13206           tree decl;
13207           int next_parm_info_bit = 31;
13208
13209           for (decl = DECL_ARGUMENTS (current_function_decl);
13210                decl; decl = TREE_CHAIN (decl))
13211             {
13212               rtx parameter = DECL_INCOMING_RTL (decl);
13213               enum machine_mode mode = GET_MODE (parameter);
13214
13215               if (GET_CODE (parameter) == REG)
13216                 {
13217                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
13218                     {
13219                       int bits;
13220
13221                       float_parms++;
13222
13223                       if (mode == SFmode)
13224                         bits = 0x2;
13225                       else if (mode == DFmode || mode == TFmode)
13226                         bits = 0x3;
13227                       else
13228                         abort ();
13229
13230                       /* If only one bit will fit, don't or in this entry.  */
13231                       if (next_parm_info_bit > 0)
13232                         parm_info |= (bits << (next_parm_info_bit - 1));
13233                       next_parm_info_bit -= 2;
13234                     }
13235                   else
13236                     {
13237                       fixed_parms += ((GET_MODE_SIZE (mode)
13238                                        + (UNITS_PER_WORD - 1))
13239                                       / UNITS_PER_WORD);
13240                       next_parm_info_bit -= 1;
13241                     }
13242                 }
13243             }
13244         }
13245
13246       /* Number of fixed point parameters.  */
13247       /* This is actually the number of words of fixed point parameters; thus
13248          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
13249       fprintf (file, "%d,", fixed_parms);
13250
13251       /* 2 bitfields: number of floating point parameters (7 bits), parameters
13252          all on stack.  */
13253       /* This is actually the number of fp registers that hold parameters;
13254          and thus the maximum value is 13.  */
13255       /* Set parameters on stack bit if parameters are not in their original
13256          registers, regardless of whether they are on the stack?  Xlc
13257          seems to set the bit when not optimizing.  */
13258       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
13259
13260       if (! optional_tbtab)
13261         return;
13262
13263       /* Optional fields follow.  Some are variable length.  */
13264
13265       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
13266          11 double float.  */
13267       /* There is an entry for each parameter in a register, in the order that
13268          they occur in the parameter list.  Any intervening arguments on the
13269          stack are ignored.  If the list overflows a long (max possible length
13270          34 bits) then completely leave off all elements that don't fit.  */
13271       /* Only emit this long if there was at least one parameter.  */
13272       if (fixed_parms || float_parms)
13273         fprintf (file, "\t.long %d\n", parm_info);
13274
13275       /* Offset from start of code to tb table.  */
13276       fputs ("\t.long ", file);
13277       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
13278 #if TARGET_AIX
13279       RS6000_OUTPUT_BASENAME (file, fname);
13280 #else
13281       assemble_name (file, fname);
13282 #endif
13283       fputs ("-.", file);
13284 #if TARGET_AIX
13285       RS6000_OUTPUT_BASENAME (file, fname);
13286 #else
13287       assemble_name (file, fname);
13288 #endif
13289       putc ('\n', file);
13290
13291       /* Interrupt handler mask.  */
13292       /* Omit this long, since we never set the interrupt handler bit
13293          above.  */
13294
13295       /* Number of CTL (controlled storage) anchors.  */
13296       /* Omit this long, since the has_ctl bit is never set above.  */
13297
13298       /* Displacement into stack of each CTL anchor.  */
13299       /* Omit this list of longs, because there are no CTL anchors.  */
13300
13301       /* Length of function name.  */
13302       if (*fname == '*')
13303         ++fname;
13304       fprintf (file, "\t.short %d\n", (int) strlen (fname));
13305
13306       /* Function name.  */
13307       assemble_string (fname, strlen (fname));
13308
13309       /* Register for alloca automatic storage; this is always reg 31.
13310          Only emit this if the alloca bit was set above.  */
13311       if (frame_pointer_needed)
13312         fputs ("\t.byte 31\n", file);
13313
13314       fputs ("\t.align 2\n", file);
13315     }
13316 }
13317 \f
13318 /* A C compound statement that outputs the assembler code for a thunk
13319    function, used to implement C++ virtual function calls with
13320    multiple inheritance.  The thunk acts as a wrapper around a virtual
13321    function, adjusting the implicit object parameter before handing
13322    control off to the real function.
13323
13324    First, emit code to add the integer DELTA to the location that
13325    contains the incoming first argument.  Assume that this argument
13326    contains a pointer, and is the one used to pass the `this' pointer
13327    in C++.  This is the incoming argument *before* the function
13328    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
13329    values of all other incoming arguments.
13330
13331    After the addition, emit code to jump to FUNCTION, which is a
13332    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
13333    not touch the return address.  Hence returning from FUNCTION will
13334    return to whoever called the current `thunk'.
13335
13336    The effect must be as if FUNCTION had been called directly with the
13337    adjusted first argument.  This macro is responsible for emitting
13338    all of the code for a thunk function; output_function_prologue()
13339    and output_function_epilogue() are not invoked.
13340
13341    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
13342    been extracted from it.)  It might possibly be useful on some
13343    targets, but probably not.
13344
13345    If you do not define this macro, the target-independent code in the
13346    C++ frontend will generate a less efficient heavyweight thunk that
13347    calls FUNCTION instead of jumping to it.  The generic approach does
13348    not support varargs.  */
13349
13350 static void
13351 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED, 
13352                         HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset, 
13353                         tree function)
13354 {
13355   rtx this, insn, funexp;
13356
13357   reload_completed = 1;
13358   epilogue_completed = 1;
13359   no_new_pseudos = 1;
13360
13361   /* Mark the end of the (empty) prologue.  */
13362   emit_note (NOTE_INSN_PROLOGUE_END);
13363
13364   /* Find the "this" pointer.  If the function returns a structure,
13365      the structure return pointer is in r3.  */
13366   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
13367     this = gen_rtx_REG (Pmode, 4);
13368   else
13369     this = gen_rtx_REG (Pmode, 3);
13370
13371   /* Apply the constant offset, if required.  */
13372   if (delta)
13373     {
13374       rtx delta_rtx = GEN_INT (delta);
13375       emit_insn (TARGET_32BIT
13376                  ? gen_addsi3 (this, this, delta_rtx)
13377                  : gen_adddi3 (this, this, delta_rtx));
13378     }
13379
13380   /* Apply the offset from the vtable, if required.  */
13381   if (vcall_offset)
13382     {
13383       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
13384       rtx tmp = gen_rtx_REG (Pmode, 12);
13385
13386       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
13387       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
13388         {
13389           emit_insn (TARGET_32BIT
13390                      ? gen_addsi3 (tmp, tmp, vcall_offset_rtx)
13391                      : gen_adddi3 (tmp, tmp, vcall_offset_rtx));
13392           emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
13393         }
13394       else
13395         {
13396           rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
13397
13398           emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
13399         }
13400       emit_insn (TARGET_32BIT
13401                  ? gen_addsi3 (this, this, tmp)
13402                  : gen_adddi3 (this, this, tmp));
13403     }
13404
13405   /* Generate a tail call to the target function.  */
13406   if (!TREE_USED (function))
13407     {
13408       assemble_external (function);
13409       TREE_USED (function) = 1;
13410     }
13411   funexp = XEXP (DECL_RTL (function), 0);
13412   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
13413
13414 #if TARGET_MACHO
13415   if (MACHOPIC_INDIRECT)
13416     funexp = machopic_indirect_call_target (funexp);
13417 #endif
13418
13419   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
13420      generate sibcall RTL explicitly to avoid constraint abort.  */
13421   insn = emit_call_insn (
13422            gen_rtx_PARALLEL (VOIDmode,
13423              gen_rtvec (4,
13424                         gen_rtx_CALL (VOIDmode,
13425                                       funexp, const0_rtx),
13426                         gen_rtx_USE (VOIDmode, const0_rtx),
13427                         gen_rtx_USE (VOIDmode,
13428                                      gen_rtx_REG (SImode,
13429                                                   LINK_REGISTER_REGNUM)),
13430                         gen_rtx_RETURN (VOIDmode))));
13431   SIBLING_CALL_P (insn) = 1;
13432   emit_barrier ();
13433
13434   /* Run just enough of rest_of_compilation to get the insns emitted.
13435      There's not really enough bulk here to make other passes such as
13436      instruction scheduling worth while.  Note that use_thunk calls
13437      assemble_start_function and assemble_end_function.  */
13438   insn = get_insns ();
13439   insn_locators_initialize ();
13440   shorten_branches (insn);
13441   final_start_function (insn, file, 1);
13442   final (insn, file, 1, 0);
13443   final_end_function ();
13444
13445   reload_completed = 0;
13446   epilogue_completed = 0;
13447   no_new_pseudos = 0;
13448 }
13449 \f
13450 /* A quick summary of the various types of 'constant-pool tables'
13451    under PowerPC:
13452
13453    Target       Flags           Name            One table per   
13454    AIX          (none)          AIX TOC         object file
13455    AIX          -mfull-toc      AIX TOC         object file
13456    AIX          -mminimal-toc   AIX minimal TOC translation unit
13457    SVR4/EABI    (none)          SVR4 SDATA      object file
13458    SVR4/EABI    -fpic           SVR4 pic        object file
13459    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
13460    SVR4/EABI    -mrelocatable   EABI TOC        function
13461    SVR4/EABI    -maix           AIX TOC         object file
13462    SVR4/EABI    -maix -mminimal-toc 
13463                                 AIX minimal TOC translation unit
13464
13465    Name                 Reg.    Set by  entries       contains:
13466                                         made by  addrs? fp?     sum?
13467
13468    AIX TOC              2       crt0    as       Y      option  option
13469    AIX minimal TOC      30      prolog  gcc      Y      Y       option
13470    SVR4 SDATA           13      crt0    gcc      N      Y       N
13471    SVR4 pic             30      prolog  ld       Y      not yet N
13472    SVR4 PIC             30      prolog  gcc      Y      option  option
13473    EABI TOC             30      prolog  gcc      Y      option  option
13474
13475 */
13476
13477 /* Hash functions for the hash table.  */
13478
13479 static unsigned
13480 rs6000_hash_constant (rtx k)
13481 {
13482   enum rtx_code code = GET_CODE (k);
13483   enum machine_mode mode = GET_MODE (k);
13484   unsigned result = (code << 3) ^ mode;
13485   const char *format;
13486   int flen, fidx;
13487   
13488   format = GET_RTX_FORMAT (code);
13489   flen = strlen (format);
13490   fidx = 0;
13491
13492   switch (code)
13493     {
13494     case LABEL_REF:
13495       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
13496
13497     case CONST_DOUBLE:
13498       if (mode != VOIDmode)
13499         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
13500       flen = 2;
13501       break;
13502
13503     case CODE_LABEL:
13504       fidx = 3;
13505       break;
13506
13507     default:
13508       break;
13509     }
13510
13511   for (; fidx < flen; fidx++)
13512     switch (format[fidx])
13513       {
13514       case 's':
13515         {
13516           unsigned i, len;
13517           const char *str = XSTR (k, fidx);
13518           len = strlen (str);
13519           result = result * 613 + len;
13520           for (i = 0; i < len; i++)
13521             result = result * 613 + (unsigned) str[i];
13522           break;
13523         }
13524       case 'u':
13525       case 'e':
13526         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
13527         break;
13528       case 'i':
13529       case 'n':
13530         result = result * 613 + (unsigned) XINT (k, fidx);
13531         break;
13532       case 'w':
13533         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
13534           result = result * 613 + (unsigned) XWINT (k, fidx);
13535         else
13536           {
13537             size_t i;
13538             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
13539               result = result * 613 + (unsigned) (XWINT (k, fidx)
13540                                                   >> CHAR_BIT * i);
13541           }
13542         break;
13543       case '0':
13544         break;
13545       default:
13546         abort ();
13547       }
13548
13549   return result;
13550 }
13551
13552 static unsigned
13553 toc_hash_function (const void *hash_entry)
13554 {
13555   const struct toc_hash_struct *thc = 
13556     (const struct toc_hash_struct *) hash_entry;
13557   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
13558 }
13559
13560 /* Compare H1 and H2 for equivalence.  */
13561
13562 static int
13563 toc_hash_eq (const void *h1, const void *h2)
13564 {
13565   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
13566   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
13567
13568   if (((const struct toc_hash_struct *) h1)->key_mode
13569       != ((const struct toc_hash_struct *) h2)->key_mode)
13570     return 0;
13571
13572   return rtx_equal_p (r1, r2);
13573 }
13574
13575 /* These are the names given by the C++ front-end to vtables, and
13576    vtable-like objects.  Ideally, this logic should not be here;
13577    instead, there should be some programmatic way of inquiring as
13578    to whether or not an object is a vtable.  */
13579
13580 #define VTABLE_NAME_P(NAME)                             \
13581   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
13582   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
13583   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
13584   || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0       \
13585   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
13586
13587 void
13588 rs6000_output_symbol_ref (FILE *file, rtx x)
13589 {
13590   /* Currently C++ toc references to vtables can be emitted before it
13591      is decided whether the vtable is public or private.  If this is
13592      the case, then the linker will eventually complain that there is
13593      a reference to an unknown section.  Thus, for vtables only, 
13594      we emit the TOC reference to reference the symbol and not the
13595      section.  */
13596   const char *name = XSTR (x, 0);
13597
13598   if (VTABLE_NAME_P (name)) 
13599     {
13600       RS6000_OUTPUT_BASENAME (file, name);
13601     }
13602   else
13603     assemble_name (file, name);
13604 }
13605
13606 /* Output a TOC entry.  We derive the entry name from what is being
13607    written.  */
13608
13609 void
13610 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
13611 {
13612   char buf[256];
13613   const char *name = buf;
13614   const char *real_name;
13615   rtx base = x;
13616   int offset = 0;
13617
13618   if (TARGET_NO_TOC)
13619     abort ();
13620
13621   /* When the linker won't eliminate them, don't output duplicate
13622      TOC entries (this happens on AIX if there is any kind of TOC,
13623      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
13624      CODE_LABELs.  */
13625   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
13626     {
13627       struct toc_hash_struct *h;
13628       void * * found;
13629       
13630       /* Create toc_hash_table.  This can't be done at OVERRIDE_OPTIONS
13631          time because GGC is not initialized at that point.  */
13632       if (toc_hash_table == NULL)
13633         toc_hash_table = htab_create_ggc (1021, toc_hash_function, 
13634                                           toc_hash_eq, NULL);
13635
13636       h = ggc_alloc (sizeof (*h));
13637       h->key = x;
13638       h->key_mode = mode;
13639       h->labelno = labelno;
13640       
13641       found = htab_find_slot (toc_hash_table, h, 1);
13642       if (*found == NULL)
13643         *found = h;
13644       else  /* This is indeed a duplicate.  
13645                Set this label equal to that label.  */
13646         {
13647           fputs ("\t.set ", file);
13648           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
13649           fprintf (file, "%d,", labelno);
13650           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
13651           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
13652                                               found)->labelno));
13653           return;
13654         }
13655     }
13656
13657   /* If we're going to put a double constant in the TOC, make sure it's
13658      aligned properly when strict alignment is on.  */
13659   if (GET_CODE (x) == CONST_DOUBLE
13660       && STRICT_ALIGNMENT
13661       && GET_MODE_BITSIZE (mode) >= 64
13662       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
13663     ASM_OUTPUT_ALIGN (file, 3);
13664   }
13665
13666   (*targetm.asm_out.internal_label) (file, "LC", labelno);
13667
13668   /* Handle FP constants specially.  Note that if we have a minimal
13669      TOC, things we put here aren't actually in the TOC, so we can allow
13670      FP constants.  */
13671   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == TFmode)
13672     {
13673       REAL_VALUE_TYPE rv;
13674       long k[4];
13675
13676       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13677       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
13678
13679       if (TARGET_64BIT)
13680         {
13681           if (TARGET_MINIMAL_TOC)
13682             fputs (DOUBLE_INT_ASM_OP, file);
13683           else
13684             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
13685                      k[0] & 0xffffffff, k[1] & 0xffffffff,
13686                      k[2] & 0xffffffff, k[3] & 0xffffffff);
13687           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
13688                    k[0] & 0xffffffff, k[1] & 0xffffffff,
13689                    k[2] & 0xffffffff, k[3] & 0xffffffff);
13690           return;
13691         }
13692       else
13693         {
13694           if (TARGET_MINIMAL_TOC)
13695             fputs ("\t.long ", file);
13696           else
13697             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
13698                      k[0] & 0xffffffff, k[1] & 0xffffffff,
13699                      k[2] & 0xffffffff, k[3] & 0xffffffff);
13700           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
13701                    k[0] & 0xffffffff, k[1] & 0xffffffff,
13702                    k[2] & 0xffffffff, k[3] & 0xffffffff);
13703           return;
13704         }
13705     }
13706   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
13707     {
13708       REAL_VALUE_TYPE rv;
13709       long k[2];
13710
13711       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13712       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
13713
13714       if (TARGET_64BIT)
13715         {
13716           if (TARGET_MINIMAL_TOC)
13717             fputs (DOUBLE_INT_ASM_OP, file);
13718           else
13719             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
13720                      k[0] & 0xffffffff, k[1] & 0xffffffff);
13721           fprintf (file, "0x%lx%08lx\n",
13722                    k[0] & 0xffffffff, k[1] & 0xffffffff);
13723           return;
13724         }
13725       else
13726         {
13727           if (TARGET_MINIMAL_TOC)
13728             fputs ("\t.long ", file);
13729           else
13730             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
13731                      k[0] & 0xffffffff, k[1] & 0xffffffff);
13732           fprintf (file, "0x%lx,0x%lx\n",
13733                    k[0] & 0xffffffff, k[1] & 0xffffffff);
13734           return;
13735         }
13736     }
13737   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
13738     {
13739       REAL_VALUE_TYPE rv;
13740       long l;
13741
13742       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13743       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
13744
13745       if (TARGET_64BIT)
13746         {
13747           if (TARGET_MINIMAL_TOC)
13748             fputs (DOUBLE_INT_ASM_OP, file);
13749           else
13750             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
13751           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
13752           return;
13753         }
13754       else
13755         {
13756           if (TARGET_MINIMAL_TOC)
13757             fputs ("\t.long ", file);
13758           else
13759             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
13760           fprintf (file, "0x%lx\n", l & 0xffffffff);
13761           return;
13762         }
13763     }
13764   else if (GET_MODE (x) == VOIDmode
13765            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
13766     {
13767       unsigned HOST_WIDE_INT low;
13768       HOST_WIDE_INT high;
13769
13770       if (GET_CODE (x) == CONST_DOUBLE)
13771         {
13772           low = CONST_DOUBLE_LOW (x);
13773           high = CONST_DOUBLE_HIGH (x);
13774         }
13775       else
13776 #if HOST_BITS_PER_WIDE_INT == 32
13777         {
13778           low = INTVAL (x);
13779           high = (low & 0x80000000) ? ~0 : 0;
13780         }
13781 #else
13782         {
13783           low = INTVAL (x) & 0xffffffff;
13784           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
13785         }
13786 #endif
13787
13788       /* TOC entries are always Pmode-sized, but since this
13789          is a bigendian machine then if we're putting smaller
13790          integer constants in the TOC we have to pad them.
13791          (This is still a win over putting the constants in
13792          a separate constant pool, because then we'd have
13793          to have both a TOC entry _and_ the actual constant.)
13794
13795          For a 32-bit target, CONST_INT values are loaded and shifted
13796          entirely within `low' and can be stored in one TOC entry.  */
13797
13798       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
13799         abort ();/* It would be easy to make this work, but it doesn't now.  */
13800
13801       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
13802         {
13803 #if HOST_BITS_PER_WIDE_INT == 32
13804           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
13805                          POINTER_SIZE, &low, &high, 0);
13806 #else
13807           low |= high << 32;
13808           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
13809           high = (HOST_WIDE_INT) low >> 32;
13810           low &= 0xffffffff;
13811 #endif
13812         }
13813
13814       if (TARGET_64BIT)
13815         {
13816           if (TARGET_MINIMAL_TOC)
13817             fputs (DOUBLE_INT_ASM_OP, file);
13818           else
13819             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13820                      (long) high & 0xffffffff, (long) low & 0xffffffff);
13821           fprintf (file, "0x%lx%08lx\n",
13822                    (long) high & 0xffffffff, (long) low & 0xffffffff);
13823           return;
13824         }
13825       else
13826         {
13827           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
13828             {
13829               if (TARGET_MINIMAL_TOC)
13830                 fputs ("\t.long ", file);
13831               else
13832                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13833                          (long) high & 0xffffffff, (long) low & 0xffffffff);
13834               fprintf (file, "0x%lx,0x%lx\n",
13835                        (long) high & 0xffffffff, (long) low & 0xffffffff);
13836             }
13837           else
13838             {
13839               if (TARGET_MINIMAL_TOC)
13840                 fputs ("\t.long ", file);
13841               else
13842                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
13843               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
13844             }
13845           return;
13846         }
13847     }
13848
13849   if (GET_CODE (x) == CONST)
13850     {
13851       if (GET_CODE (XEXP (x, 0)) != PLUS)
13852         abort ();
13853
13854       base = XEXP (XEXP (x, 0), 0);
13855       offset = INTVAL (XEXP (XEXP (x, 0), 1));
13856     }
13857   
13858   if (GET_CODE (base) == SYMBOL_REF)
13859     name = XSTR (base, 0);
13860   else if (GET_CODE (base) == LABEL_REF)
13861     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
13862   else if (GET_CODE (base) == CODE_LABEL)
13863     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
13864   else
13865     abort ();
13866
13867   real_name = (*targetm.strip_name_encoding) (name);
13868   if (TARGET_MINIMAL_TOC)
13869     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
13870   else
13871     {
13872       fprintf (file, "\t.tc %s", real_name);
13873
13874       if (offset < 0)
13875         fprintf (file, ".N%d", - offset);
13876       else if (offset)
13877         fprintf (file, ".P%d", offset);
13878
13879       fputs ("[TC],", file);
13880     }
13881
13882   /* Currently C++ toc references to vtables can be emitted before it
13883      is decided whether the vtable is public or private.  If this is
13884      the case, then the linker will eventually complain that there is
13885      a TOC reference to an unknown section.  Thus, for vtables only,
13886      we emit the TOC reference to reference the symbol and not the
13887      section.  */
13888   if (VTABLE_NAME_P (name))
13889     {
13890       RS6000_OUTPUT_BASENAME (file, name);
13891       if (offset < 0)
13892         fprintf (file, "%d", offset);
13893       else if (offset > 0)
13894         fprintf (file, "+%d", offset);
13895     }
13896   else
13897     output_addr_const (file, x);
13898   putc ('\n', file);
13899 }
13900 \f
13901 /* Output an assembler pseudo-op to write an ASCII string of N characters
13902    starting at P to FILE.
13903
13904    On the RS/6000, we have to do this using the .byte operation and
13905    write out special characters outside the quoted string.
13906    Also, the assembler is broken; very long strings are truncated,
13907    so we must artificially break them up early.  */
13908
13909 void
13910 output_ascii (FILE *file, const char *p, int n)
13911 {
13912   char c;
13913   int i, count_string;
13914   const char *for_string = "\t.byte \"";
13915   const char *for_decimal = "\t.byte ";
13916   const char *to_close = NULL;
13917
13918   count_string = 0;
13919   for (i = 0; i < n; i++)
13920     {
13921       c = *p++;
13922       if (c >= ' ' && c < 0177)
13923         {
13924           if (for_string)
13925             fputs (for_string, file);
13926           putc (c, file);
13927
13928           /* Write two quotes to get one.  */
13929           if (c == '"')
13930             {
13931               putc (c, file);
13932               ++count_string;
13933             }
13934
13935           for_string = NULL;
13936           for_decimal = "\"\n\t.byte ";
13937           to_close = "\"\n";
13938           ++count_string;
13939
13940           if (count_string >= 512)
13941             {
13942               fputs (to_close, file);
13943
13944               for_string = "\t.byte \"";
13945               for_decimal = "\t.byte ";
13946               to_close = NULL;
13947               count_string = 0;
13948             }
13949         }
13950       else
13951         {
13952           if (for_decimal)
13953             fputs (for_decimal, file);
13954           fprintf (file, "%d", c);
13955
13956           for_string = "\n\t.byte \"";
13957           for_decimal = ", ";
13958           to_close = "\n";
13959           count_string = 0;
13960         }
13961     }
13962
13963   /* Now close the string if we have written one.  Then end the line.  */
13964   if (to_close)
13965     fputs (to_close, file);
13966 }
13967 \f
13968 /* Generate a unique section name for FILENAME for a section type
13969    represented by SECTION_DESC.  Output goes into BUF.
13970
13971    SECTION_DESC can be any string, as long as it is different for each
13972    possible section type.
13973
13974    We name the section in the same manner as xlc.  The name begins with an
13975    underscore followed by the filename (after stripping any leading directory
13976    names) with the last period replaced by the string SECTION_DESC.  If
13977    FILENAME does not contain a period, SECTION_DESC is appended to the end of
13978    the name.  */
13979
13980 void
13981 rs6000_gen_section_name (char **buf, const char *filename, 
13982                          const char *section_desc)
13983 {
13984   const char *q, *after_last_slash, *last_period = 0;
13985   char *p;
13986   int len;
13987
13988   after_last_slash = filename;
13989   for (q = filename; *q; q++)
13990     {
13991       if (*q == '/')
13992         after_last_slash = q + 1;
13993       else if (*q == '.')
13994         last_period = q;
13995     }
13996
13997   len = strlen (after_last_slash) + strlen (section_desc) + 2;
13998   *buf = (char *) xmalloc (len);
13999
14000   p = *buf;
14001   *p++ = '_';
14002
14003   for (q = after_last_slash; *q; q++)
14004     {
14005       if (q == last_period)
14006         {
14007           strcpy (p, section_desc);
14008           p += strlen (section_desc);
14009           break;
14010         }
14011
14012       else if (ISALNUM (*q))
14013         *p++ = *q;
14014     }
14015
14016   if (last_period == 0)
14017     strcpy (p, section_desc);
14018   else
14019     *p = '\0';
14020 }
14021 \f
14022 /* Emit profile function.  */
14023
14024 void
14025 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
14026 {
14027   if (TARGET_PROFILE_KERNEL)
14028     return;
14029
14030   if (DEFAULT_ABI == ABI_AIX)
14031     {
14032 #ifndef NO_PROFILE_COUNTERS
14033 # define NO_PROFILE_COUNTERS 0
14034 #endif
14035       if (NO_PROFILE_COUNTERS)  
14036         emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 0);
14037       else
14038         {
14039           char buf[30];
14040           const char *label_name;
14041           rtx fun;
14042
14043           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
14044           label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
14045           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
14046
14047           emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
14048                              fun, Pmode);
14049         }
14050     }
14051   else if (DEFAULT_ABI == ABI_DARWIN)
14052     {
14053       const char *mcount_name = RS6000_MCOUNT;
14054       int caller_addr_regno = LINK_REGISTER_REGNUM;
14055
14056       /* Be conservative and always set this, at least for now.  */
14057       current_function_uses_pic_offset_table = 1;
14058
14059 #if TARGET_MACHO
14060       /* For PIC code, set up a stub and collect the caller's address
14061          from r0, which is where the prologue puts it.  */
14062       if (MACHOPIC_INDIRECT)
14063         {
14064           mcount_name = machopic_stub_name (mcount_name);
14065           if (current_function_uses_pic_offset_table)
14066             caller_addr_regno = 0;
14067         }
14068 #endif
14069       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
14070                          0, VOIDmode, 1,
14071                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
14072     }
14073 }
14074
14075 /* Write function profiler code.  */
14076
14077 void
14078 output_function_profiler (FILE *file, int labelno)
14079 {
14080   char buf[100];
14081   int save_lr = 8;
14082
14083   switch (DEFAULT_ABI)
14084     {
14085     default:
14086       abort ();
14087
14088     case ABI_V4:
14089       save_lr = 4;
14090       if (!TARGET_32BIT)
14091         {
14092           warning ("no profiling of 64-bit code for this ABI");
14093           return;
14094         }
14095       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
14096       fprintf (file, "\tmflr %s\n", reg_names[0]);
14097       if (flag_pic == 1)
14098         {
14099           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
14100           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
14101                        reg_names[0], save_lr, reg_names[1]);
14102           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
14103           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
14104           assemble_name (file, buf);
14105           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
14106         }
14107       else if (flag_pic > 1)
14108         {
14109           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
14110                        reg_names[0], save_lr, reg_names[1]);
14111           /* Now, we need to get the address of the label.  */
14112           fputs ("\tbl 1f\n\t.long ", file);
14113           assemble_name (file, buf);
14114           fputs ("-.\n1:", file);
14115           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
14116           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
14117                        reg_names[0], reg_names[11]);
14118           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
14119                        reg_names[0], reg_names[0], reg_names[11]);
14120         }
14121       else
14122         {
14123           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
14124           assemble_name (file, buf);
14125           fputs ("@ha\n", file);
14126           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
14127                        reg_names[0], save_lr, reg_names[1]);
14128           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
14129           assemble_name (file, buf);
14130           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
14131         }
14132
14133       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
14134       fprintf (file, "\tbl %s%s\n",
14135                RS6000_MCOUNT, flag_pic ? "@plt" : "");
14136       break;
14137
14138     case ABI_AIX:
14139     case ABI_DARWIN:
14140       if (!TARGET_PROFILE_KERNEL)
14141         {
14142           /* Don't do anything, done in output_profile_hook ().  */
14143         }
14144       else
14145         {
14146           if (TARGET_32BIT)
14147             abort ();
14148
14149           asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
14150           asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
14151
14152           if (cfun->static_chain_decl != NULL)
14153             {
14154               asm_fprintf (file, "\tstd %s,24(%s)\n",
14155                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
14156               fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
14157               asm_fprintf (file, "\tld %s,24(%s)\n",
14158                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
14159             }
14160           else
14161             fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
14162         }
14163       break;
14164     }
14165 }
14166
14167 \f
14168 static int
14169 rs6000_use_dfa_pipeline_interface (void)
14170 {
14171   return 1;
14172 }
14173
14174 /* Power4 load update and store update instructions are cracked into a
14175    load or store and an integer insn which are executed in the same cycle.
14176    Branches have their own dispatch slot which does not count against the
14177    GCC issue rate, but it changes the program flow so there are no other
14178    instructions to issue in this cycle.  */
14179
14180 static int
14181 rs6000_variable_issue (FILE *stream ATTRIBUTE_UNUSED, 
14182                        int verbose ATTRIBUTE_UNUSED, 
14183                        rtx insn, int more)
14184 {
14185   if (GET_CODE (PATTERN (insn)) == USE
14186       || GET_CODE (PATTERN (insn)) == CLOBBER)
14187     return more;
14188
14189   if (rs6000_sched_groups)
14190     {
14191       if (is_microcoded_insn (insn))
14192         return 0;
14193       else if (is_cracked_insn (insn))
14194         return more > 2 ? more - 2 : 0;
14195     }
14196
14197   return more - 1;
14198 }
14199
14200 /* Adjust the cost of a scheduling dependency.  Return the new cost of
14201    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
14202
14203 static int
14204 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn ATTRIBUTE_UNUSED, 
14205                     int cost)
14206 {
14207   if (! recog_memoized (insn))
14208     return 0;
14209
14210   if (REG_NOTE_KIND (link) != 0)
14211     return 0;
14212
14213   if (REG_NOTE_KIND (link) == 0)
14214     {
14215       /* Data dependency; DEP_INSN writes a register that INSN reads
14216          some cycles later.  */
14217       switch (get_attr_type (insn))
14218         {
14219         case TYPE_JMPREG:
14220           /* Tell the first scheduling pass about the latency between
14221              a mtctr and bctr (and mtlr and br/blr).  The first
14222              scheduling pass will not know about this latency since
14223              the mtctr instruction, which has the latency associated
14224              to it, will be generated by reload.  */
14225           return TARGET_POWER ? 5 : 4;
14226         case TYPE_BRANCH:
14227           /* Leave some extra cycles between a compare and its
14228              dependent branch, to inhibit expensive mispredicts.  */
14229           if ((rs6000_cpu_attr == CPU_PPC603
14230                || rs6000_cpu_attr == CPU_PPC604
14231                || rs6000_cpu_attr == CPU_PPC604E
14232                || rs6000_cpu_attr == CPU_PPC620
14233                || rs6000_cpu_attr == CPU_PPC630
14234                || rs6000_cpu_attr == CPU_PPC750
14235                || rs6000_cpu_attr == CPU_PPC7400
14236                || rs6000_cpu_attr == CPU_PPC7450
14237                || rs6000_cpu_attr == CPU_POWER4
14238                || rs6000_cpu_attr == CPU_POWER5)
14239               && recog_memoized (dep_insn)
14240               && (INSN_CODE (dep_insn) >= 0)
14241               && (get_attr_type (dep_insn) == TYPE_CMP
14242                   || get_attr_type (dep_insn) == TYPE_COMPARE
14243                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
14244                   || get_attr_type (dep_insn) == TYPE_IMUL_COMPARE
14245                   || get_attr_type (dep_insn) == TYPE_LMUL_COMPARE
14246                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
14247                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL
14248                   || get_attr_type (dep_insn) == TYPE_DELAYED_CR))
14249             return cost + 2;
14250         default:
14251           break;
14252         }
14253       /* Fall out to return default cost.  */
14254     }
14255
14256   return cost;
14257 }
14258
14259 /* The function returns a true if INSN is microcoded.
14260    Return false otherwise.  */
14261
14262 static bool
14263 is_microcoded_insn (rtx insn)
14264 {
14265   if (!insn || !INSN_P (insn)
14266       || GET_CODE (PATTERN (insn)) == USE
14267       || GET_CODE (PATTERN (insn)) == CLOBBER)
14268     return false;
14269
14270   if (rs6000_sched_groups)
14271     {
14272       enum attr_type type = get_attr_type (insn);
14273       if (type == TYPE_LOAD_EXT_U
14274           || type == TYPE_LOAD_EXT_UX
14275           || type == TYPE_LOAD_UX
14276           || type == TYPE_STORE_UX
14277           || type == TYPE_MFCR)
14278         return true;
14279     }
14280
14281   return false;
14282 }
14283
14284 /* The function returns a nonzero value if INSN can be scheduled only
14285    as the first insn in a dispatch group ("dispatch-slot restricted").
14286    In this case, the returned value indicates how many dispatch slots
14287    the insn occupies (at the beginning of the group).
14288    Return 0 otherwise.  */
14289
14290 static int
14291 is_dispatch_slot_restricted (rtx insn)
14292 {
14293   enum attr_type type;
14294
14295   if (!rs6000_sched_groups)
14296     return 0;
14297
14298   if (!insn
14299       || insn == NULL_RTX
14300       || GET_CODE (insn) == NOTE
14301       || GET_CODE (PATTERN (insn)) == USE
14302       || GET_CODE (PATTERN (insn)) == CLOBBER)
14303     return 0;
14304
14305   type = get_attr_type (insn);
14306
14307   switch (type)
14308     {
14309     case TYPE_MFCR:
14310     case TYPE_MFCRF:
14311     case TYPE_MTCR:
14312     case TYPE_DELAYED_CR:
14313     case TYPE_CR_LOGICAL:
14314     case TYPE_MTJMPR:
14315     case TYPE_MFJMPR:
14316       return 1;
14317     case TYPE_IDIV:
14318     case TYPE_LDIV:
14319       return 2;
14320     default:
14321       if (rs6000_cpu == PROCESSOR_POWER5
14322           && is_cracked_insn (insn))
14323         return 2;
14324       return 0;
14325     }
14326 }
14327
14328 /* The function returns true if INSN is cracked into 2 instructions
14329    by the processor (and therefore occupies 2 issue slots).  */
14330
14331 static bool
14332 is_cracked_insn (rtx insn)
14333 {
14334   if (!insn || !INSN_P (insn)
14335       || GET_CODE (PATTERN (insn)) == USE
14336       || GET_CODE (PATTERN (insn)) == CLOBBER)
14337     return false;
14338
14339   if (rs6000_sched_groups)
14340     {
14341       enum attr_type type = get_attr_type (insn);
14342       if (type == TYPE_LOAD_U || type == TYPE_STORE_U
14343                || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
14344                || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
14345                || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
14346                || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
14347                || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
14348                || type == TYPE_IDIV || type == TYPE_LDIV
14349                || type == TYPE_INSERT_WORD)
14350         return true;
14351     }
14352
14353   return false;
14354 }
14355
14356 /* The function returns true if INSN can be issued only from
14357    the branch slot.  */
14358
14359 static bool
14360 is_branch_slot_insn (rtx insn)
14361 {
14362   if (!insn || !INSN_P (insn)
14363       || GET_CODE (PATTERN (insn)) == USE
14364       || GET_CODE (PATTERN (insn)) == CLOBBER)
14365     return false;
14366
14367   if (rs6000_sched_groups)
14368     {
14369       enum attr_type type = get_attr_type (insn);
14370       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
14371         return true;     
14372       return false;
14373     }
14374
14375   return false;
14376 }
14377
14378 /* A C statement (sans semicolon) to update the integer scheduling
14379    priority INSN_PRIORITY (INSN). Increase the priority to execute the
14380    INSN earlier, reduce the priority to execute INSN later.  Do not
14381    define this macro if you do not need to adjust the scheduling
14382    priorities of insns.  */
14383
14384 static int
14385 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
14386 {
14387   /* On machines (like the 750) which have asymmetric integer units,
14388      where one integer unit can do multiply and divides and the other
14389      can't, reduce the priority of multiply/divide so it is scheduled
14390      before other integer operations.  */
14391
14392 #if 0
14393   if (! INSN_P (insn))
14394     return priority;
14395
14396   if (GET_CODE (PATTERN (insn)) == USE)
14397     return priority;
14398
14399   switch (rs6000_cpu_attr) {
14400   case CPU_PPC750:
14401     switch (get_attr_type (insn))
14402       {
14403       default:
14404         break;
14405
14406       case TYPE_IMUL:
14407       case TYPE_IDIV:
14408         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
14409                  priority, priority);
14410         if (priority >= 0 && priority < 0x01000000)
14411           priority >>= 3;
14412         break;
14413       }
14414   }
14415 #endif
14416
14417   if (is_dispatch_slot_restricted (insn)
14418       && reload_completed
14419       && current_sched_info->sched_max_insns_priority 
14420       && rs6000_sched_restricted_insns_priority)
14421     {
14422
14423       /* Prioritize insns that can be dispatched only in the first dispatch slot.  */
14424       if (rs6000_sched_restricted_insns_priority == 1)
14425         /* Attach highest priority to insn. This means that in 
14426            haifa-sched.c:ready_sort(), dispatch-slot restriction considerations 
14427            precede 'priority' (critical path) considerations.  */
14428         return current_sched_info->sched_max_insns_priority; 
14429       else if (rs6000_sched_restricted_insns_priority == 2)
14430         /* Increase priority of insn by a minimal amount. This means that in 
14431            haifa-sched.c:ready_sort(), only 'priority' (critical path) considerations
14432            precede dispatch-slot restriction considerations.  */
14433         return (priority + 1); 
14434     } 
14435
14436   return priority;
14437 }
14438
14439 /* Return how many instructions the machine can issue per cycle.  */
14440
14441 static int
14442 rs6000_issue_rate (void)
14443 {
14444   /* Use issue rate of 1 for first scheduling pass to decrease degradation.  */
14445   if (!reload_completed)
14446     return 1;
14447
14448   switch (rs6000_cpu_attr) {
14449   case CPU_RIOS1:  /* ? */
14450   case CPU_RS64A:
14451   case CPU_PPC601: /* ? */
14452   case CPU_PPC7450:
14453     return 3;
14454   case CPU_PPC440:
14455   case CPU_PPC603:
14456   case CPU_PPC750:
14457   case CPU_PPC7400:
14458   case CPU_PPC8540:
14459     return 2; 
14460   case CPU_RIOS2:
14461   case CPU_PPC604:
14462   case CPU_PPC604E:
14463   case CPU_PPC620:
14464   case CPU_PPC630:
14465     return 4;
14466   case CPU_POWER4:
14467   case CPU_POWER5:
14468     return 5;
14469   default:
14470     return 1;
14471   }
14472 }
14473
14474 /* Return how many instructions to look ahead for better insn
14475    scheduling.  */
14476
14477 static int
14478 rs6000_use_sched_lookahead (void)
14479 {
14480   if (rs6000_cpu_attr == CPU_PPC8540)
14481     return 4;
14482   return 0;
14483 }
14484
14485 /* Determine is PAT refers to memory.  */
14486
14487 static bool
14488 is_mem_ref (rtx pat)
14489 {
14490   const char * fmt;
14491   int i, j;
14492   bool ret = false;
14493
14494   if (GET_CODE (pat) == MEM)
14495     return true;
14496
14497   /* Recursively process the pattern.  */
14498   fmt = GET_RTX_FORMAT (GET_CODE (pat));
14499
14500   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0 && !ret; i--)
14501     {
14502       if (fmt[i] == 'e')
14503         ret |= is_mem_ref (XEXP (pat, i));
14504       else if (fmt[i] == 'E')
14505         for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
14506           ret |= is_mem_ref (XVECEXP (pat, i, j));
14507     }
14508
14509   return ret;
14510 }
14511
14512 /* Determine if PAT is a PATTERN of a load insn.  */
14513  
14514 static bool
14515 is_load_insn1 (rtx pat)
14516 {
14517   if (!pat || pat == NULL_RTX)
14518     return false;
14519
14520   if (GET_CODE (pat) == SET)
14521     return is_mem_ref (SET_SRC (pat));
14522
14523   if (GET_CODE (pat) == PARALLEL)
14524     {
14525       int i;
14526
14527       for (i = 0; i < XVECLEN (pat, 0); i++)
14528         if (is_load_insn1 (XVECEXP (pat, 0, i)))
14529           return true;
14530     }
14531
14532   return false;
14533 }
14534
14535 /* Determine if INSN loads from memory.  */
14536
14537 static bool
14538 is_load_insn (rtx insn)
14539 {
14540   if (!insn || !INSN_P (insn))
14541     return false;
14542
14543   if (GET_CODE (insn) == CALL_INSN)
14544     return false;
14545
14546   return is_load_insn1 (PATTERN (insn));
14547 }
14548
14549 /* Determine if PAT is a PATTERN of a store insn.  */
14550
14551 static bool
14552 is_store_insn1 (rtx pat)
14553 {
14554   if (!pat || pat == NULL_RTX)
14555     return false;
14556
14557   if (GET_CODE (pat) == SET)
14558     return is_mem_ref (SET_DEST (pat));
14559
14560   if (GET_CODE (pat) == PARALLEL)
14561     {
14562       int i;
14563
14564       for (i = 0; i < XVECLEN (pat, 0); i++)
14565         if (is_store_insn1 (XVECEXP (pat, 0, i)))
14566           return true;
14567     }
14568
14569   return false;
14570 }
14571
14572 /* Determine if INSN stores to memory.  */
14573
14574 static bool
14575 is_store_insn (rtx insn)
14576 {
14577   if (!insn || !INSN_P (insn))
14578     return false;
14579
14580   return is_store_insn1 (PATTERN (insn));
14581 }
14582
14583 /* Returns whether the dependence between INSN and NEXT is considered
14584    costly by the given target.  */
14585
14586 static bool
14587 rs6000_is_costly_dependence (rtx insn, rtx next, rtx link, int cost, int distance)
14588 {      
14589   /* If the flag is not enbled - no dependence is considered costly;
14590      allow all dependent insns in the same group. 
14591      This is the most aggressive option.  */
14592   if (rs6000_sched_costly_dep == no_dep_costly)
14593     return false;
14594
14595   /* If the flag is set to 1 - a dependence is always considered costly; 
14596      do not allow dependent instructions in the same group.
14597      This is the most conservative option.  */
14598   if (rs6000_sched_costly_dep == all_deps_costly)
14599     return true;       
14600
14601   if (rs6000_sched_costly_dep == store_to_load_dep_costly 
14602       && is_load_insn (next) 
14603       && is_store_insn (insn))
14604     /* Prevent load after store in the same group.  */
14605     return true;
14606
14607   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
14608       && is_load_insn (next) 
14609       && is_store_insn (insn)
14610       && (!link || (int) REG_NOTE_KIND (link) == 0))
14611      /* Prevent load after store in the same group if it is a true dependence.  */
14612      return true;
14613     
14614   /* The flag is set to X; dependences with latency >= X are considered costly, 
14615      and will not be scheduled in the same group.  */
14616   if (rs6000_sched_costly_dep <= max_dep_latency
14617       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
14618     return true;
14619
14620   return false;
14621 }
14622
14623 /* Return the next insn after INSN that is found before TAIL is reached, 
14624    skipping any "non-active" insns - insns that will not actually occupy
14625    an issue slot.  Return NULL_RTX if such an insn is not found.  */
14626
14627 static rtx
14628 get_next_active_insn (rtx insn, rtx tail)
14629 {
14630   rtx next_insn;
14631
14632   if (!insn || insn == tail)
14633     return NULL_RTX;
14634
14635   next_insn = NEXT_INSN (insn);
14636
14637   while (next_insn
14638          && next_insn != tail
14639          && (GET_CODE(next_insn) == NOTE
14640              || GET_CODE (PATTERN (next_insn)) == USE
14641              || GET_CODE (PATTERN (next_insn)) == CLOBBER))
14642     {
14643       next_insn = NEXT_INSN (next_insn);
14644     }
14645
14646   if (!next_insn || next_insn == tail)
14647     return NULL_RTX;
14648
14649   return next_insn;
14650 }
14651
14652 /* Return whether the presence of INSN causes a dispatch group termination
14653    of group WHICH_GROUP.
14654
14655    If WHICH_GROUP == current_group, this function will return true if INSN
14656    causes the termination of the current group (i.e, the dispatch group to
14657    which INSN belongs). This means that INSN will be the last insn in the
14658    group it belongs to.
14659
14660    If WHICH_GROUP == previous_group, this function will return true if INSN
14661    causes the termination of the previous group (i.e, the dispatch group that
14662    precedes the group to which INSN belongs).  This means that INSN will be
14663    the first insn in the group it belongs to).  */
14664
14665 static bool
14666 insn_terminates_group_p (rtx insn, enum group_termination which_group)
14667 {
14668   enum attr_type type;
14669
14670   if (! insn)
14671     return false;
14672
14673   type = get_attr_type (insn);
14674
14675   if (is_microcoded_insn (insn))
14676     return true;
14677
14678   if (which_group == current_group)
14679     {
14680       if (is_branch_slot_insn (insn))
14681         return true;
14682       return false;
14683     }
14684   else if (which_group == previous_group)
14685     {
14686       if (is_dispatch_slot_restricted (insn))
14687         return true;
14688       return false;
14689     }
14690
14691   return false;
14692 }
14693
14694 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
14695    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
14696
14697 static bool
14698 is_costly_group (rtx *group_insns, rtx next_insn)
14699 {
14700   int i;
14701   rtx link;
14702   int cost;
14703   int issue_rate = rs6000_issue_rate ();
14704
14705   for (i = 0; i < issue_rate; i++)
14706     {
14707       rtx insn = group_insns[i];
14708       if (!insn)
14709         continue;
14710       for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
14711         {
14712           rtx next = XEXP (link, 0);
14713           if (next == next_insn)
14714             {
14715               cost = insn_cost (insn, link, next_insn);
14716               if (rs6000_is_costly_dependence (insn, next_insn, link, cost, 0))
14717                 return true;
14718             }
14719         }
14720     }
14721
14722   return false;
14723 }
14724
14725 /* Utility of the function redefine_groups. 
14726    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
14727    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
14728    to keep it "far" (in a separate group) from GROUP_INSNS, following
14729    one of the following schemes, depending on the value of the flag
14730    -minsert_sched_nops = X:
14731    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
14732        in order to force NEXT_INSN into a separate group.
14733    (2) X < sched_finish_regroup_exact: insert exactly X nops.  
14734    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop 
14735    insertion (has a group just ended, how many vacant issue slots remain in the
14736    last group, and how many dispatch groups were encountered so far).  */
14737
14738 static int 
14739 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns, rtx next_insn,
14740                  bool *group_end, int can_issue_more, int *group_count)
14741 {
14742   rtx nop;
14743   bool force;
14744   int issue_rate = rs6000_issue_rate ();
14745   bool end = *group_end;
14746   int i;
14747
14748   if (next_insn == NULL_RTX)
14749     return can_issue_more;
14750
14751   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
14752     return can_issue_more;
14753
14754   force = is_costly_group (group_insns, next_insn);
14755   if (!force)
14756     return can_issue_more;
14757
14758   if (sched_verbose > 6)
14759     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
14760                         *group_count ,can_issue_more);
14761
14762   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
14763     {
14764       if (*group_end)
14765         can_issue_more = 0;
14766
14767       /* Since only a branch can be issued in the last issue_slot, it is
14768          sufficient to insert 'can_issue_more - 1' nops if next_insn is not
14769          a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
14770          in this case the last nop will start a new group and the branch will be
14771          forced to the new group.  */
14772       if (can_issue_more && !is_branch_slot_insn (next_insn))
14773         can_issue_more--;
14774
14775       while (can_issue_more > 0)
14776         {
14777           nop = gen_nop();
14778           emit_insn_before (nop, next_insn);
14779           can_issue_more--;
14780         }
14781
14782       *group_end = true;
14783       return 0;
14784     } 
14785
14786   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
14787     {
14788       int n_nops = rs6000_sched_insert_nops;
14789
14790       /* Nops can't be issued from the branch slot, so the effective 
14791          issue_rate for nops is 'issue_rate - 1'.  */
14792       if (can_issue_more == 0)
14793         can_issue_more = issue_rate;
14794       can_issue_more--;
14795       if (can_issue_more == 0)
14796         {
14797           can_issue_more = issue_rate - 1;
14798           (*group_count)++;
14799           end = true;
14800           for (i = 0; i < issue_rate; i++)
14801             {
14802               group_insns[i] = 0;
14803             }
14804         }
14805
14806       while (n_nops > 0)
14807         {
14808           nop = gen_nop ();
14809           emit_insn_before (nop, next_insn);
14810           if (can_issue_more == issue_rate - 1) /* new group begins */
14811             end = false;
14812           can_issue_more--;
14813           if (can_issue_more == 0)
14814             {
14815               can_issue_more = issue_rate - 1;
14816               (*group_count)++;
14817               end = true;
14818               for (i = 0; i < issue_rate; i++)
14819                 {
14820                   group_insns[i] = 0;
14821                 } 
14822             }   
14823           n_nops--;
14824         }
14825
14826       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
14827       can_issue_more++; 
14828
14829       *group_end = /* Is next_insn going to start a new group?  */
14830           (end 
14831            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14832            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14833            || (can_issue_more < issue_rate &&
14834               insn_terminates_group_p (next_insn, previous_group)));
14835       if (*group_end && end)
14836         (*group_count)--;
14837
14838       if (sched_verbose > 6)
14839         fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
14840                         *group_count, can_issue_more);
14841       return can_issue_more;    
14842     } 
14843
14844   return can_issue_more;
14845 }
14846
14847 /* This function tries to synch the dispatch groups that the compiler "sees"
14848    with the dispatch groups that the processor dispatcher is expected to 
14849    form in practice.  It tries to achieve this synchronization by forcing the
14850    estimated processor grouping on the compiler (as opposed to the function
14851    'pad_goups' which tries to force the scheduler's grouping on the processor).
14852
14853    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
14854    examines the (estimated) dispatch groups that will be formed by the processor
14855    dispatcher.  It marks these group boundaries to reflect the estimated
14856    processor grouping, overriding the grouping that the scheduler had marked.
14857    Depending on the value of the flag '-minsert-sched-nops' this function can
14858    force certain insns into separate groups or force a certain distance between
14859    them by inserting nops, for example, if there exists a "costly dependence"
14860    between the insns.
14861
14862    The function estimates the group boundaries that the processor will form as
14863    folllows:  It keeps track of how many vacant issue slots are available after
14864    each insn.  A subsequent insn will start a new group if one of the following
14865    4 cases applies:
14866    - no more vacant issue slots remain in the current dispatch group.
14867    - only the last issue slot, which is the branch slot, is vacant, but the next
14868      insn is not a branch.
14869    - only the last 2 or less issue slots, including the branch slot, are vacant,
14870      which means that a cracked insn (which occupies two issue slots) can't be
14871      issued in this group.
14872    - less than 'issue_rate' slots are vacant, and the next insn always needs to 
14873      start a new group.  */
14874
14875 static int
14876 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14877 {
14878   rtx insn, next_insn;
14879   int issue_rate;
14880   int can_issue_more;
14881   int slot, i;
14882   bool group_end;
14883   int group_count = 0;
14884   rtx *group_insns;
14885
14886   /* Initialize.  */
14887   issue_rate = rs6000_issue_rate ();
14888   group_insns = alloca (issue_rate * sizeof (rtx));
14889   for (i = 0; i < issue_rate; i++) 
14890     {
14891       group_insns[i] = 0;
14892     }
14893   can_issue_more = issue_rate;
14894   slot = 0;
14895   insn = get_next_active_insn (prev_head_insn, tail);
14896   group_end = false;
14897
14898   while (insn != NULL_RTX)
14899     {
14900       slot = (issue_rate - can_issue_more);
14901       group_insns[slot] = insn;
14902       can_issue_more =
14903         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14904       if (insn_terminates_group_p (insn, current_group))
14905         can_issue_more = 0;
14906
14907       next_insn = get_next_active_insn (insn, tail);
14908       if (next_insn == NULL_RTX)
14909         return group_count + 1;
14910
14911       group_end = /* Is next_insn going to start a new group?  */
14912         (can_issue_more == 0
14913          || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14914          || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14915          || (can_issue_more < issue_rate &&
14916              insn_terminates_group_p (next_insn, previous_group)));
14917
14918       can_issue_more = force_new_group (sched_verbose, dump, group_insns, 
14919                         next_insn, &group_end, can_issue_more, &group_count);
14920
14921       if (group_end)
14922         {
14923           group_count++;
14924           can_issue_more = 0;
14925           for (i = 0; i < issue_rate; i++)
14926             {
14927               group_insns[i] = 0;
14928             }
14929         }
14930
14931       if (GET_MODE (next_insn) == TImode && can_issue_more)
14932         PUT_MODE(next_insn, VOIDmode);
14933       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
14934         PUT_MODE (next_insn, TImode);
14935
14936       insn = next_insn;
14937       if (can_issue_more == 0)
14938         can_issue_more = issue_rate;
14939    } /* while */
14940
14941   return group_count;
14942 }
14943
14944 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
14945    dispatch group boundaries that the scheduler had marked.  Pad with nops
14946    any dispatch groups which have vacant issue slots, in order to force the
14947    scheduler's grouping on the processor dispatcher.  The function
14948    returns the number of dispatch groups found.  */
14949
14950 static int
14951 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14952 {
14953   rtx insn, next_insn;
14954   rtx nop;
14955   int issue_rate;
14956   int can_issue_more;
14957   int group_end;
14958   int group_count = 0;
14959
14960   /* Initialize issue_rate.  */
14961   issue_rate = rs6000_issue_rate ();
14962   can_issue_more = issue_rate;
14963
14964   insn = get_next_active_insn (prev_head_insn, tail);
14965   next_insn = get_next_active_insn (insn, tail);
14966
14967   while (insn != NULL_RTX)
14968     {
14969       can_issue_more =
14970         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14971
14972       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
14973
14974       if (next_insn == NULL_RTX)
14975         break;
14976
14977       if (group_end)
14978         {
14979           /* If the scheduler had marked group termination at this location
14980              (between insn and next_indn), and neither insn nor next_insn will
14981              force group termination, pad the group with nops to force group
14982              termination.  */
14983           if (can_issue_more
14984               && (rs6000_sched_insert_nops == sched_finish_pad_groups)
14985               && !insn_terminates_group_p (insn, current_group)
14986               && !insn_terminates_group_p (next_insn, previous_group))
14987             {
14988               if (!is_branch_slot_insn(next_insn))
14989                 can_issue_more--;
14990
14991               while (can_issue_more)
14992                 {
14993                   nop = gen_nop ();
14994                   emit_insn_before (nop, next_insn);
14995                   can_issue_more--;
14996                 }
14997             }
14998
14999           can_issue_more = issue_rate;
15000           group_count++;
15001         }
15002
15003       insn = next_insn;
15004       next_insn = get_next_active_insn (insn, tail);
15005     }
15006
15007   return group_count;
15008 }
15009
15010 /* The following function is called at the end of scheduling BB.
15011    After reload, it inserts nops at insn group bundling.  */
15012
15013 static void
15014 rs6000_sched_finish (FILE *dump, int sched_verbose)
15015 {
15016   int n_groups;
15017
15018   if (sched_verbose)
15019     fprintf (dump, "=== Finishing schedule.\n");
15020
15021   if (reload_completed && rs6000_sched_groups)
15022     {
15023       if (rs6000_sched_insert_nops == sched_finish_none)
15024         return;
15025
15026       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
15027         n_groups = pad_groups (dump, sched_verbose,
15028                                 current_sched_info->prev_head,
15029                                 current_sched_info->next_tail);
15030       else
15031         n_groups = redefine_groups (dump, sched_verbose,
15032                                 current_sched_info->prev_head,
15033                                 current_sched_info->next_tail);
15034
15035       if (sched_verbose >= 6)
15036         {
15037           fprintf (dump, "ngroups = %d\n", n_groups);
15038           print_rtl (dump, current_sched_info->prev_head);
15039           fprintf (dump, "Done finish_sched\n");
15040         }
15041     }
15042 }
15043 \f
15044 /* Length in units of the trampoline for entering a nested function.  */
15045
15046 int
15047 rs6000_trampoline_size (void)
15048 {
15049   int ret = 0;
15050
15051   switch (DEFAULT_ABI)
15052     {
15053     default:
15054       abort ();
15055
15056     case ABI_AIX:
15057       ret = (TARGET_32BIT) ? 12 : 24;
15058       break;
15059
15060     case ABI_DARWIN:
15061     case ABI_V4:
15062       ret = (TARGET_32BIT) ? 40 : 48;
15063       break;
15064     }
15065
15066   return ret;
15067 }
15068
15069 /* Emit RTL insns to initialize the variable parts of a trampoline.
15070    FNADDR is an RTX for the address of the function's pure code.
15071    CXT is an RTX for the static chain value for the function.  */
15072
15073 void
15074 rs6000_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
15075 {
15076   enum machine_mode pmode = Pmode;
15077   int regsize = (TARGET_32BIT) ? 4 : 8;
15078   rtx ctx_reg = force_reg (pmode, cxt);
15079
15080   switch (DEFAULT_ABI)
15081     {
15082     default:
15083       abort ();
15084
15085 /* Macros to shorten the code expansions below.  */
15086 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
15087 #define MEM_PLUS(addr,offset) \
15088   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
15089
15090     /* Under AIX, just build the 3 word function descriptor */
15091     case ABI_AIX:
15092       {
15093         rtx fn_reg = gen_reg_rtx (pmode);
15094         rtx toc_reg = gen_reg_rtx (pmode);
15095         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
15096         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
15097         emit_move_insn (MEM_DEREF (addr), fn_reg);
15098         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
15099         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
15100       }
15101       break;
15102
15103     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
15104     case ABI_DARWIN:
15105     case ABI_V4:
15106       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
15107                          FALSE, VOIDmode, 4,
15108                          addr, pmode,
15109                          GEN_INT (rs6000_trampoline_size ()), SImode,
15110                          fnaddr, pmode,
15111                          ctx_reg, pmode);
15112       break;
15113     }
15114
15115   return;
15116 }
15117
15118 \f
15119 /* Table of valid machine attributes.  */
15120
15121 const struct attribute_spec rs6000_attribute_table[] =
15122 {
15123   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
15124   { "altivec",   1, 1, false, true,  false, rs6000_handle_altivec_attribute },
15125   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
15126   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
15127   { NULL,        0, 0, false, false, false, NULL }
15128 };
15129
15130 /* Handle the "altivec" attribute.  The attribute may have
15131    arguments as follows:
15132    
15133         __attribute__((altivec(vector__)))
15134         __attribute__((altivec(pixel__)))       (always followed by 'unsigned short')
15135         __attribute__((altivec(bool__)))        (always followed by 'unsigned')
15136
15137   and may appear more than once (e.g., 'vector bool char') in a
15138   given declaration.  */
15139
15140 static tree
15141 rs6000_handle_altivec_attribute (tree *node, tree name, tree args,
15142                                  int flags ATTRIBUTE_UNUSED,
15143                                  bool *no_add_attrs)
15144 {
15145   tree type = *node, result = NULL_TREE;
15146   enum machine_mode mode;
15147   int unsigned_p;
15148   char altivec_type
15149     = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
15150         && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
15151        ? *IDENTIFIER_POINTER (TREE_VALUE (args))
15152        : '?'); 
15153
15154   while (POINTER_TYPE_P (type)
15155          || TREE_CODE (type) == FUNCTION_TYPE
15156          || TREE_CODE (type) == METHOD_TYPE
15157          || TREE_CODE (type) == ARRAY_TYPE)
15158     type = TREE_TYPE (type);
15159
15160   mode = TYPE_MODE (type);
15161
15162   if (rs6000_warn_altivec_long
15163       && (type == long_unsigned_type_node || type == long_integer_type_node))
15164     warning ("use of 'long' in AltiVec types is deprecated; use 'int'");
15165
15166   switch (altivec_type)
15167     {
15168     case 'v':
15169       unsigned_p = TYPE_UNSIGNED (type);
15170       switch (mode)
15171         {
15172           case SImode:
15173             result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
15174             break;
15175           case HImode:
15176             result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
15177             break;
15178           case QImode:
15179             result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
15180             break;
15181           case SFmode: result = V4SF_type_node; break;
15182             /* If the user says 'vector int bool', we may be handed the 'bool'
15183                attribute _before_ the 'vector' attribute, and so select the proper
15184                type in the 'b' case below.  */
15185           case V4SImode: case V8HImode: case V16QImode: result = type;
15186           default: break;
15187         }
15188       break;
15189     case 'b':
15190       switch (mode)
15191         {
15192           case SImode: case V4SImode: result = bool_V4SI_type_node; break;
15193           case HImode: case V8HImode: result = bool_V8HI_type_node; break;
15194           case QImode: case V16QImode: result = bool_V16QI_type_node;
15195           default: break;
15196         }
15197       break;
15198     case 'p':
15199       switch (mode)
15200         {
15201           case V8HImode: result = pixel_V8HI_type_node;
15202           default: break;
15203         }
15204     default: break;
15205     }
15206
15207   if (result && result != type && TYPE_READONLY (type))
15208     result = build_qualified_type (result, TYPE_QUAL_CONST);
15209
15210   *no_add_attrs = true;  /* No need to hang on to the attribute.  */
15211
15212   if (!result)
15213     warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
15214   else
15215     *node = reconstruct_complex_type (*node, result);
15216
15217   return NULL_TREE;
15218 }
15219
15220 /* AltiVec defines four built-in scalar types that serve as vector
15221    elements; we must teach the compiler how to mangle them.  */
15222
15223 static const char *
15224 rs6000_mangle_fundamental_type (tree type)
15225 {
15226   if (type == bool_char_type_node) return "U6__boolc";
15227   if (type == bool_short_type_node) return "U6__bools";
15228   if (type == pixel_type_node) return "u7__pixel";
15229   if (type == bool_int_type_node) return "U6__booli";
15230
15231   /* For all other types, use normal C++ mangling.  */
15232   return NULL;
15233 }
15234
15235 /* Handle a "longcall" or "shortcall" attribute; arguments as in
15236    struct attribute_spec.handler.  */
15237
15238 static tree
15239 rs6000_handle_longcall_attribute (tree *node, tree name, 
15240                                   tree args ATTRIBUTE_UNUSED, 
15241                                   int flags ATTRIBUTE_UNUSED, 
15242                                   bool *no_add_attrs)
15243 {
15244   if (TREE_CODE (*node) != FUNCTION_TYPE
15245       && TREE_CODE (*node) != FIELD_DECL
15246       && TREE_CODE (*node) != TYPE_DECL)
15247     {
15248       warning ("`%s' attribute only applies to functions",
15249                IDENTIFIER_POINTER (name));
15250       *no_add_attrs = true;
15251     }
15252
15253   return NULL_TREE;
15254 }
15255
15256 /* Set longcall attributes on all functions declared when
15257    rs6000_default_long_calls is true.  */
15258 static void
15259 rs6000_set_default_type_attributes (tree type)
15260 {
15261   if (rs6000_default_long_calls
15262       && (TREE_CODE (type) == FUNCTION_TYPE
15263           || TREE_CODE (type) == METHOD_TYPE))
15264     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
15265                                         NULL_TREE,
15266                                         TYPE_ATTRIBUTES (type));
15267 }
15268
15269 /* Return a reference suitable for calling a function with the
15270    longcall attribute.  */
15271
15272 struct rtx_def *
15273 rs6000_longcall_ref (rtx call_ref)
15274 {
15275   const char *call_name;
15276   tree node;
15277
15278   if (GET_CODE (call_ref) != SYMBOL_REF)
15279     return call_ref;
15280
15281   /* System V adds '.' to the internal name, so skip them.  */
15282   call_name = XSTR (call_ref, 0);
15283   if (*call_name == '.')
15284     {
15285       while (*call_name == '.')
15286         call_name++;
15287
15288       node = get_identifier (call_name);
15289       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
15290     }
15291
15292   return force_reg (Pmode, call_ref);
15293 }
15294 \f
15295 #ifdef USING_ELFOS_H
15296
15297 /* A C statement or statements to switch to the appropriate section
15298    for output of RTX in mode MODE.  You can assume that RTX is some
15299    kind of constant in RTL.  The argument MODE is redundant except in
15300    the case of a `const_int' rtx.  Select the section by calling
15301    `text_section' or one of the alternatives for other sections.
15302
15303    Do not define this macro if you put all constants in the read-only
15304    data section.  */
15305
15306 static void
15307 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x, 
15308                                unsigned HOST_WIDE_INT align)
15309 {
15310   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
15311     toc_section ();
15312   else
15313     default_elf_select_rtx_section (mode, x, align);
15314 }
15315
15316 /* A C statement or statements to switch to the appropriate
15317    section for output of DECL.  DECL is either a `VAR_DECL' node
15318    or a constant of some sort.  RELOC indicates whether forming
15319    the initial value of DECL requires link-time relocations.  */
15320
15321 static void
15322 rs6000_elf_select_section (tree decl, int reloc, 
15323                            unsigned HOST_WIDE_INT align)
15324 {
15325   /* Pretend that we're always building for a shared library when
15326      ABI_AIX, because otherwise we end up with dynamic relocations
15327      in read-only sections.  This happens for function pointers,
15328      references to vtables in typeinfo, and probably other cases.  */
15329   default_elf_select_section_1 (decl, reloc, align,
15330                                 flag_pic || DEFAULT_ABI == ABI_AIX);
15331 }
15332
15333 /* A C statement to build up a unique section name, expressed as a
15334    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
15335    RELOC indicates whether the initial value of EXP requires
15336    link-time relocations.  If you do not define this macro, GCC will use
15337    the symbol name prefixed by `.' as the section name.  Note - this
15338    macro can now be called for uninitialized data items as well as
15339    initialized data and functions.  */
15340
15341 static void
15342 rs6000_elf_unique_section (tree decl, int reloc)
15343 {
15344   /* As above, pretend that we're always building for a shared library
15345      when ABI_AIX, to avoid dynamic relocations in read-only sections.  */
15346   default_unique_section_1 (decl, reloc,
15347                             flag_pic || DEFAULT_ABI == ABI_AIX);
15348 }
15349 \f
15350 /* For a SYMBOL_REF, set generic flags and then perform some
15351    target-specific processing.
15352
15353    When the AIX ABI is requested on a non-AIX system, replace the
15354    function name with the real name (with a leading .) rather than the
15355    function descriptor name.  This saves a lot of overriding code to
15356    read the prefixes.  */
15357
15358 static void
15359 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
15360 {
15361   default_encode_section_info (decl, rtl, first);
15362
15363   if (first
15364       && TREE_CODE (decl) == FUNCTION_DECL
15365       && !TARGET_AIX
15366       && DEFAULT_ABI == ABI_AIX)
15367     {
15368       rtx sym_ref = XEXP (rtl, 0);
15369       size_t len = strlen (XSTR (sym_ref, 0));
15370       char *str = alloca (len + 2);
15371       str[0] = '.';
15372       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
15373       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
15374     }
15375 }
15376
15377 static bool
15378 rs6000_elf_in_small_data_p (tree decl)
15379 {
15380   if (rs6000_sdata == SDATA_NONE)
15381     return false;
15382
15383   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
15384     {
15385       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
15386       if (strcmp (section, ".sdata") == 0
15387           || strcmp (section, ".sdata2") == 0
15388           || strcmp (section, ".sbss") == 0
15389           || strcmp (section, ".sbss2") == 0
15390           || strcmp (section, ".PPC.EMB.sdata0") == 0
15391           || strcmp (section, ".PPC.EMB.sbss0") == 0)
15392         return true;
15393     }
15394   else
15395     {
15396       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
15397
15398       if (size > 0
15399           && (unsigned HOST_WIDE_INT) size <= g_switch_value
15400           /* If it's not public, and we're not going to reference it there,
15401              there's no need to put it in the small data section.  */
15402           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
15403         return true;
15404     }
15405
15406   return false;
15407 }
15408
15409 #endif /* USING_ELFOS_H */
15410
15411 \f
15412 /* Return a REG that occurs in ADDR with coefficient 1.
15413    ADDR can be effectively incremented by incrementing REG.
15414
15415    r0 is special and we must not select it as an address
15416    register by this routine since our caller will try to
15417    increment the returned register via an "la" instruction.  */
15418
15419 struct rtx_def *
15420 find_addr_reg (rtx addr)
15421 {
15422   while (GET_CODE (addr) == PLUS)
15423     {
15424       if (GET_CODE (XEXP (addr, 0)) == REG
15425           && REGNO (XEXP (addr, 0)) != 0)
15426         addr = XEXP (addr, 0);
15427       else if (GET_CODE (XEXP (addr, 1)) == REG
15428                && REGNO (XEXP (addr, 1)) != 0)
15429         addr = XEXP (addr, 1);
15430       else if (CONSTANT_P (XEXP (addr, 0)))
15431         addr = XEXP (addr, 1);
15432       else if (CONSTANT_P (XEXP (addr, 1)))
15433         addr = XEXP (addr, 0);
15434       else
15435         abort ();
15436     }
15437   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
15438     return addr;
15439   abort ();
15440 }
15441
15442 void
15443 rs6000_fatal_bad_address (rtx op)
15444 {
15445   fatal_insn ("bad address", op);
15446 }
15447
15448 #if TARGET_MACHO
15449
15450 #if 0
15451 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
15452    reference and a constant.  */
15453
15454 int
15455 symbolic_operand (rtx op)
15456 {
15457   switch (GET_CODE (op))
15458     {
15459     case SYMBOL_REF:
15460     case LABEL_REF:
15461       return 1;
15462     case CONST:
15463       op = XEXP (op, 0);
15464       return (GET_CODE (op) == SYMBOL_REF ||
15465               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
15466                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
15467               && GET_CODE (XEXP (op, 1)) == CONST_INT);
15468     default:
15469       return 0;
15470     }
15471 }
15472 #endif
15473
15474 #if TARGET_MACHO
15475
15476 static tree branch_island_list = 0;
15477
15478 /* Remember to generate a branch island for far calls to the given
15479    function.  */
15480
15481 static void 
15482 add_compiler_branch_island (tree label_name, tree function_name, int line_number)
15483 {
15484   tree branch_island = build_tree_list (function_name, label_name);
15485   TREE_TYPE (branch_island) = build_int_2 (line_number, 0);
15486   TREE_CHAIN (branch_island) = branch_island_list;
15487   branch_island_list = branch_island;
15488 }
15489
15490 #define BRANCH_ISLAND_LABEL_NAME(BRANCH_ISLAND)     TREE_VALUE (BRANCH_ISLAND)
15491 #define BRANCH_ISLAND_FUNCTION_NAME(BRANCH_ISLAND)  TREE_PURPOSE (BRANCH_ISLAND)
15492 #define BRANCH_ISLAND_LINE_NUMBER(BRANCH_ISLAND)    \
15493                 TREE_INT_CST_LOW (TREE_TYPE (BRANCH_ISLAND))
15494
15495 /* Generate far-jump branch islands for everything on the
15496    branch_island_list.  Invoked immediately after the last instruction
15497    of the epilogue has been emitted; the branch-islands must be
15498    appended to, and contiguous with, the function body.  Mach-O stubs
15499    are generated in machopic_output_stub().  */
15500
15501 static void
15502 macho_branch_islands (void)
15503 {
15504   char tmp_buf[512];
15505   tree branch_island;
15506
15507   for (branch_island = branch_island_list;
15508        branch_island;
15509        branch_island = TREE_CHAIN (branch_island))
15510     {
15511       const char *label =
15512         IDENTIFIER_POINTER (BRANCH_ISLAND_LABEL_NAME (branch_island));
15513       const char *name  =
15514         darwin_strip_name_encoding (
15515           IDENTIFIER_POINTER (BRANCH_ISLAND_FUNCTION_NAME (branch_island)));
15516       char name_buf[512];
15517       /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF().  */
15518       if (name[0] == '*' || name[0] == '&')
15519         strcpy (name_buf, name+1);
15520       else
15521         {
15522           name_buf[0] = '_';
15523           strcpy (name_buf+1, name);
15524         }
15525       strcpy (tmp_buf, "\n");
15526       strcat (tmp_buf, label);
15527 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
15528       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
15529         fprintf (asm_out_file, "\t.stabd 68,0," HOST_WIDE_INT_PRINT_UNSIGNED "\n",
15530                  BRANCH_ISLAND_LINE_NUMBER(branch_island));
15531 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
15532       if (flag_pic)
15533         {
15534           strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
15535           strcat (tmp_buf, label);
15536           strcat (tmp_buf, "_pic\n");
15537           strcat (tmp_buf, label);
15538           strcat (tmp_buf, "_pic:\n\tmflr r11\n");
15539  
15540           strcat (tmp_buf, "\taddis r11,r11,ha16(");
15541           strcat (tmp_buf, name_buf);
15542           strcat (tmp_buf, " - ");
15543           strcat (tmp_buf, label);
15544           strcat (tmp_buf, "_pic)\n");
15545                    
15546           strcat (tmp_buf, "\tmtlr r0\n");
15547   
15548           strcat (tmp_buf, "\taddi r12,r11,lo16(");
15549           strcat (tmp_buf, name_buf);
15550           strcat (tmp_buf, " - ");
15551           strcat (tmp_buf, label);
15552           strcat (tmp_buf, "_pic)\n");
15553  
15554           strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
15555         }
15556       else
15557         {
15558           strcat (tmp_buf, ":\nlis r12,hi16(");
15559           strcat (tmp_buf, name_buf);
15560           strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
15561           strcat (tmp_buf, name_buf);
15562           strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
15563         }
15564       output_asm_insn (tmp_buf, 0);
15565 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
15566       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
15567         fprintf(asm_out_file, "\t.stabd 68,0," HOST_WIDE_INT_PRINT_UNSIGNED "\n",
15568                 BRANCH_ISLAND_LINE_NUMBER (branch_island));
15569 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
15570     }
15571
15572   branch_island_list = 0;
15573 }
15574
15575 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
15576    already there or not.  */
15577
15578 static int
15579 no_previous_def (tree function_name)
15580 {
15581   tree branch_island;
15582   for (branch_island = branch_island_list;
15583        branch_island;
15584        branch_island = TREE_CHAIN (branch_island))
15585     if (function_name == BRANCH_ISLAND_FUNCTION_NAME (branch_island))
15586       return 0;
15587   return 1;
15588 }
15589
15590 /* GET_PREV_LABEL gets the label name from the previous definition of
15591    the function.  */
15592
15593 static tree
15594 get_prev_label (tree function_name)
15595 {
15596   tree branch_island;
15597   for (branch_island = branch_island_list;
15598        branch_island;
15599        branch_island = TREE_CHAIN (branch_island))
15600     if (function_name == BRANCH_ISLAND_FUNCTION_NAME (branch_island))
15601       return BRANCH_ISLAND_LABEL_NAME (branch_island);
15602   return 0;
15603 }
15604
15605 /* INSN is either a function call or a millicode call.  It may have an
15606    unconditional jump in its delay slot.  
15607
15608    CALL_DEST is the routine we are calling.  */
15609
15610 char *
15611 output_call (rtx insn, rtx *operands, int dest_operand_number, int cookie_operand_number)
15612 {
15613   static char buf[256];
15614   if (GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
15615       && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
15616     {
15617       tree labelname;
15618       tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
15619       
15620       if (no_previous_def (funname))
15621         {
15622           int line_number = 0;
15623           rtx label_rtx = gen_label_rtx ();
15624           char *label_buf, temp_buf[256];
15625           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
15626                                        CODE_LABEL_NUMBER (label_rtx));
15627           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
15628           labelname = get_identifier (label_buf);
15629           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
15630           if (insn)
15631             line_number = NOTE_LINE_NUMBER (insn);
15632           add_compiler_branch_island (labelname, funname, line_number);
15633         }
15634       else
15635         labelname = get_prev_label (funname);
15636
15637       /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
15638          instruction will reach 'foo', otherwise link as 'bl L42'".
15639          "L42" should be a 'branch island', that will do a far jump to
15640          'foo'.  Branch islands are generated in
15641          macho_branch_islands().  */
15642       sprintf (buf, "jbsr %%z%d,%.246s",
15643                dest_operand_number, IDENTIFIER_POINTER (labelname));
15644     }
15645   else
15646     sprintf (buf, "bl %%z%d", dest_operand_number);
15647   return buf;
15648 }
15649
15650 #endif /* TARGET_MACHO */
15651
15652 /* Generate PIC and indirect symbol stubs.  */
15653
15654 void
15655 machopic_output_stub (FILE *file, const char *symb, const char *stub)
15656 {
15657   unsigned int length;
15658   char *symbol_name, *lazy_ptr_name;
15659   char *local_label_0;
15660   static int label = 0;
15661
15662   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
15663   symb = (*targetm.strip_name_encoding) (symb);
15664
15665
15666   length = strlen (symb);
15667   symbol_name = alloca (length + 32);
15668   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
15669
15670   lazy_ptr_name = alloca (length + 32);
15671   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
15672
15673   if (flag_pic == 2)
15674     machopic_picsymbol_stub1_section ();
15675   else
15676     machopic_symbol_stub1_section ();
15677   fprintf (file, "\t.align 2\n");
15678
15679   fprintf (file, "%s:\n", stub);
15680   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15681
15682   if (flag_pic == 2)
15683     {
15684       label++;
15685       local_label_0 = alloca (sizeof("\"L0000000000$spb\""));
15686       sprintf (local_label_0, "\"L%011d$spb\"", label);
15687     
15688       fprintf (file, "\tmflr r0\n");
15689       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
15690       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
15691       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
15692                lazy_ptr_name, local_label_0);
15693       fprintf (file, "\tmtlr r0\n");
15694       fprintf (file, "\tlwzu r12,lo16(%s-%s)(r11)\n",
15695                lazy_ptr_name, local_label_0);
15696       fprintf (file, "\tmtctr r12\n");
15697       fprintf (file, "\tbctr\n");
15698     }
15699   else
15700    {
15701      fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
15702      fprintf (file, "\tlwzu r12,lo16(%s)(r11)\n", lazy_ptr_name);
15703      fprintf (file, "\tmtctr r12\n");
15704      fprintf (file, "\tbctr\n");
15705    }
15706   
15707   machopic_lazy_symbol_ptr_section ();
15708   fprintf (file, "%s:\n", lazy_ptr_name);
15709   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15710   fprintf (file, "\t.long dyld_stub_binding_helper\n");
15711 }
15712
15713 /* Legitimize PIC addresses.  If the address is already
15714    position-independent, we return ORIG.  Newly generated
15715    position-independent addresses go into a reg.  This is REG if non
15716    zero, otherwise we allocate register(s) as necessary.  */
15717
15718 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
15719
15720 rtx
15721 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, 
15722                                         rtx reg)
15723 {
15724   rtx base, offset;
15725
15726   if (reg == NULL && ! reload_in_progress && ! reload_completed)
15727     reg = gen_reg_rtx (Pmode);
15728
15729   if (GET_CODE (orig) == CONST)
15730     {
15731       if (GET_CODE (XEXP (orig, 0)) == PLUS
15732           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
15733         return orig;
15734
15735       if (GET_CODE (XEXP (orig, 0)) == PLUS)
15736         {
15737           /* Use a different reg for the intermediate value, as
15738              it will be marked UNCHANGING.  */
15739           rtx reg_temp = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
15740
15741           base =
15742             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
15743                                                     Pmode, reg_temp);
15744           offset =
15745             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
15746                                                     Pmode, reg);
15747         }
15748       else
15749         abort ();
15750
15751       if (GET_CODE (offset) == CONST_INT)
15752         {
15753           if (SMALL_INT (offset))
15754             return plus_constant (base, INTVAL (offset));
15755           else if (! reload_in_progress && ! reload_completed)
15756             offset = force_reg (Pmode, offset);
15757           else
15758             {
15759               rtx mem = force_const_mem (Pmode, orig);
15760               return machopic_legitimize_pic_address (mem, Pmode, reg);
15761             }
15762         }
15763       return gen_rtx_PLUS (Pmode, base, offset);
15764     }
15765
15766   /* Fall back on generic machopic code.  */
15767   return machopic_legitimize_pic_address (orig, mode, reg);
15768 }
15769
15770 /* This is just a placeholder to make linking work without having to
15771    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
15772    ever needed for Darwin (not too likely!) this would have to get a
15773    real definition.  */
15774
15775 void
15776 toc_section (void)
15777 {
15778 }
15779
15780 #endif /* TARGET_MACHO */
15781
15782 #if TARGET_ELF
15783 static unsigned int
15784 rs6000_elf_section_type_flags (tree decl, const char *name, int reloc)
15785 {
15786   return default_section_type_flags_1 (decl, name, reloc,
15787                                        flag_pic || DEFAULT_ABI == ABI_AIX);
15788 }
15789
15790 /* Record an element in the table of global constructors.  SYMBOL is
15791    a SYMBOL_REF of the function to be called; PRIORITY is a number
15792    between 0 and MAX_INIT_PRIORITY.
15793
15794    This differs from default_named_section_asm_out_constructor in
15795    that we have special handling for -mrelocatable.  */
15796
15797 static void
15798 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
15799 {
15800   const char *section = ".ctors";
15801   char buf[16];
15802
15803   if (priority != DEFAULT_INIT_PRIORITY)
15804     {
15805       sprintf (buf, ".ctors.%.5u",
15806                /* Invert the numbering so the linker puts us in the proper
15807                   order; constructors are run from right to left, and the
15808                   linker sorts in increasing order.  */
15809                MAX_INIT_PRIORITY - priority);
15810       section = buf;
15811     }
15812
15813   named_section_flags (section, SECTION_WRITE);
15814   assemble_align (POINTER_SIZE);
15815
15816   if (TARGET_RELOCATABLE)
15817     {
15818       fputs ("\t.long (", asm_out_file);
15819       output_addr_const (asm_out_file, symbol);
15820       fputs (")@fixup\n", asm_out_file);
15821     }
15822   else
15823     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
15824 }
15825
15826 static void
15827 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
15828 {
15829   const char *section = ".dtors";
15830   char buf[16];
15831
15832   if (priority != DEFAULT_INIT_PRIORITY)
15833     {
15834       sprintf (buf, ".dtors.%.5u",
15835                /* Invert the numbering so the linker puts us in the proper
15836                   order; constructors are run from right to left, and the
15837                   linker sorts in increasing order.  */
15838                MAX_INIT_PRIORITY - priority);
15839       section = buf;
15840     }
15841
15842   named_section_flags (section, SECTION_WRITE);
15843   assemble_align (POINTER_SIZE);
15844
15845   if (TARGET_RELOCATABLE)
15846     {
15847       fputs ("\t.long (", asm_out_file);
15848       output_addr_const (asm_out_file, symbol);
15849       fputs (")@fixup\n", asm_out_file);
15850     }
15851   else
15852     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
15853 }
15854
15855 void
15856 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
15857 {
15858   if (TARGET_64BIT)
15859     {
15860       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
15861       ASM_OUTPUT_LABEL (file, name);
15862       fputs (DOUBLE_INT_ASM_OP, file);
15863       putc ('.', file);
15864       assemble_name (file, name);
15865       fputs (",.TOC.@tocbase,0\n\t.previous\n\t.size\t", file);
15866       assemble_name (file, name);
15867       fputs (",24\n\t.type\t.", file);
15868       assemble_name (file, name);
15869       fputs (",@function\n", file);
15870       if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
15871         {
15872           fputs ("\t.globl\t.", file);
15873           assemble_name (file, name);
15874           putc ('\n', file);
15875         }
15876       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
15877       putc ('.', file);
15878       ASM_OUTPUT_LABEL (file, name);
15879       return;
15880     }
15881
15882   if (TARGET_RELOCATABLE
15883       && (get_pool_size () != 0 || current_function_profile)
15884       && uses_TOC ())
15885     {
15886       char buf[256];
15887
15888       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
15889
15890       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
15891       fprintf (file, "\t.long ");
15892       assemble_name (file, buf);
15893       putc ('-', file);
15894       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
15895       assemble_name (file, buf);
15896       putc ('\n', file);
15897     }
15898
15899   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
15900   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
15901
15902   if (DEFAULT_ABI == ABI_AIX)
15903     {
15904       const char *desc_name, *orig_name;
15905
15906       orig_name = (*targetm.strip_name_encoding) (name);
15907       desc_name = orig_name;
15908       while (*desc_name == '.')
15909         desc_name++;
15910
15911       if (TREE_PUBLIC (decl))
15912         fprintf (file, "\t.globl %s\n", desc_name);
15913
15914       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
15915       fprintf (file, "%s:\n", desc_name);
15916       fprintf (file, "\t.long %s\n", orig_name);
15917       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
15918       if (DEFAULT_ABI == ABI_AIX)
15919         fputs ("\t.long 0\n", file);
15920       fprintf (file, "\t.previous\n");
15921     }
15922   ASM_OUTPUT_LABEL (file, name);
15923 }
15924 #endif
15925
15926 #if TARGET_XCOFF
15927 static void
15928 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
15929 {
15930   fputs (GLOBAL_ASM_OP, stream);
15931   RS6000_OUTPUT_BASENAME (stream, name);
15932   putc ('\n', stream);
15933 }
15934
15935 static void
15936 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags)
15937 {
15938   int smclass;
15939   static const char * const suffix[3] = { "PR", "RO", "RW" };
15940
15941   if (flags & SECTION_CODE)
15942     smclass = 0;
15943   else if (flags & SECTION_WRITE)
15944     smclass = 2;
15945   else
15946     smclass = 1;
15947
15948   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
15949            (flags & SECTION_CODE) ? "." : "",
15950            name, suffix[smclass], flags & SECTION_ENTSIZE);
15951 }
15952
15953 static void
15954 rs6000_xcoff_select_section (tree decl, int reloc, 
15955                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
15956 {
15957   if (decl_readonly_section_1 (decl, reloc, 1))
15958     {
15959       if (TREE_PUBLIC (decl))
15960         read_only_data_section ();
15961       else
15962         read_only_private_data_section ();
15963     }
15964   else
15965     {
15966       if (TREE_PUBLIC (decl))
15967         data_section ();
15968       else
15969         private_data_section ();
15970     }
15971 }
15972
15973 static void
15974 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
15975 {
15976   const char *name;
15977
15978   /* Use select_section for private and uninitialized data.  */
15979   if (!TREE_PUBLIC (decl)
15980       || DECL_COMMON (decl)
15981       || DECL_INITIAL (decl) == NULL_TREE
15982       || DECL_INITIAL (decl) == error_mark_node
15983       || (flag_zero_initialized_in_bss
15984           && initializer_zerop (DECL_INITIAL (decl))))
15985     return;
15986
15987   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15988   name = (*targetm.strip_name_encoding) (name);
15989   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
15990 }
15991
15992 /* Select section for constant in constant pool.
15993
15994    On RS/6000, all constants are in the private read-only data area.
15995    However, if this is being placed in the TOC it must be output as a
15996    toc entry.  */
15997
15998 static void
15999 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x, 
16000                                 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
16001 {
16002   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
16003     toc_section ();
16004   else
16005     read_only_private_data_section ();
16006 }
16007
16008 /* Remove any trailing [DS] or the like from the symbol name.  */
16009
16010 static const char *
16011 rs6000_xcoff_strip_name_encoding (const char *name)
16012 {
16013   size_t len;
16014   if (*name == '*')
16015     name++;
16016   len = strlen (name);
16017   if (name[len - 1] == ']')
16018     return ggc_alloc_string (name, len - 4);
16019   else
16020     return name;
16021 }
16022
16023 /* Section attributes.  AIX is always PIC.  */
16024
16025 static unsigned int
16026 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
16027 {
16028   unsigned int align;
16029   unsigned int flags = default_section_type_flags_1 (decl, name, reloc, 1);
16030
16031   /* Align to at least UNIT size.  */
16032   if (flags & SECTION_CODE)
16033     align = MIN_UNITS_PER_WORD;
16034   else
16035     /* Increase alignment of large objects if not already stricter.  */
16036     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
16037                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
16038                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
16039
16040   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
16041 }
16042
16043 /* Output at beginning of assembler file.
16044
16045    Initialize the section names for the RS/6000 at this point.
16046
16047    Specify filename, including full path, to assembler.
16048
16049    We want to go into the TOC section so at least one .toc will be emitted.
16050    Also, in order to output proper .bs/.es pairs, we need at least one static
16051    [RW] section emitted.
16052
16053    Finally, declare mcount when profiling to make the assembler happy.  */
16054
16055 static void
16056 rs6000_xcoff_file_start (void)
16057 {
16058   rs6000_gen_section_name (&xcoff_bss_section_name,
16059                            main_input_filename, ".bss_");
16060   rs6000_gen_section_name (&xcoff_private_data_section_name,
16061                            main_input_filename, ".rw_");
16062   rs6000_gen_section_name (&xcoff_read_only_section_name,
16063                            main_input_filename, ".ro_");
16064
16065   fputs ("\t.file\t", asm_out_file);
16066   output_quoted_string (asm_out_file, main_input_filename);
16067   fputc ('\n', asm_out_file);
16068   toc_section ();
16069   if (write_symbols != NO_DEBUG)
16070     private_data_section ();
16071   text_section ();
16072   if (profile_flag)
16073     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
16074   rs6000_file_start ();
16075 }
16076
16077 /* Output at end of assembler file.
16078    On the RS/6000, referencing data should automatically pull in text.  */
16079
16080 static void
16081 rs6000_xcoff_file_end (void)
16082 {
16083   text_section ();
16084   fputs ("_section_.text:\n", asm_out_file);
16085   data_section ();
16086   fputs (TARGET_32BIT
16087          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
16088          asm_out_file);
16089 }
16090 #endif /* TARGET_XCOFF */
16091
16092 #if TARGET_MACHO
16093 /* Cross-module name binding.  Darwin does not support overriding
16094    functions at dynamic-link time.  */
16095
16096 static bool
16097 rs6000_binds_local_p (tree decl)
16098 {
16099   return default_binds_local_p_1 (decl, 0);
16100 }
16101 #endif
16102
16103 /* Compute a (partial) cost for rtx X.  Return true if the complete
16104    cost has been computed, and false if subexpressions should be
16105    scanned.  In either case, *TOTAL contains the cost result.  */
16106
16107 static bool
16108 rs6000_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, 
16109                   int *total)
16110 {
16111   switch (code)
16112     {
16113       /* On the RS/6000, if it is valid in the insn, it is free.
16114          So this always returns 0.  */
16115     case CONST_INT:
16116     case CONST:
16117     case LABEL_REF:
16118     case SYMBOL_REF:
16119     case CONST_DOUBLE:
16120     case HIGH:
16121       *total = 0;
16122       return true;
16123
16124     case PLUS:
16125       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
16126                  && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1))
16127                                                + 0x8000) >= 0x10000)
16128                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
16129                 ? COSTS_N_INSNS (2)
16130                 : COSTS_N_INSNS (1));
16131       return true;
16132
16133     case AND:
16134     case IOR:
16135     case XOR:
16136       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
16137                  && (INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff)) != 0
16138                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
16139                 ? COSTS_N_INSNS (2)
16140                 : COSTS_N_INSNS (1));
16141       return true;
16142
16143     case MULT:
16144       if (optimize_size)
16145         {
16146           *total = COSTS_N_INSNS (2);
16147           return true;
16148         }
16149       switch (rs6000_cpu)
16150         {
16151         case PROCESSOR_RIOS1:
16152         case PROCESSOR_PPC405:
16153           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16154                     ? COSTS_N_INSNS (5)
16155                     : (INTVAL (XEXP (x, 1)) >= -256
16156                        && INTVAL (XEXP (x, 1)) <= 255)
16157                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
16158           return true;
16159
16160         case PROCESSOR_PPC440:
16161           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16162                     ? COSTS_N_INSNS (3)
16163                     : COSTS_N_INSNS (2));
16164           return true;
16165
16166         case PROCESSOR_RS64A:
16167           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16168                     ? GET_MODE (XEXP (x, 1)) != DImode
16169                     ? COSTS_N_INSNS (20) : COSTS_N_INSNS (34)
16170                     : (INTVAL (XEXP (x, 1)) >= -256
16171                        && INTVAL (XEXP (x, 1)) <= 255)
16172                     ? COSTS_N_INSNS (8) : COSTS_N_INSNS (12));
16173           return true;
16174
16175         case PROCESSOR_RIOS2:
16176         case PROCESSOR_MPCCORE:
16177         case PROCESSOR_PPC604e:
16178           *total = COSTS_N_INSNS (2);
16179           return true;
16180
16181         case PROCESSOR_PPC601:
16182           *total = COSTS_N_INSNS (5);
16183           return true;
16184
16185         case PROCESSOR_PPC603:
16186         case PROCESSOR_PPC7400:
16187         case PROCESSOR_PPC750:
16188           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16189                     ? COSTS_N_INSNS (5)
16190                     : (INTVAL (XEXP (x, 1)) >= -256
16191                        && INTVAL (XEXP (x, 1)) <= 255)
16192                     ? COSTS_N_INSNS (2) : COSTS_N_INSNS (3));
16193           return true;
16194
16195         case PROCESSOR_PPC7450:
16196           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16197                     ? COSTS_N_INSNS (4)
16198                     : COSTS_N_INSNS (3));
16199           return true;
16200
16201         case PROCESSOR_PPC403:
16202         case PROCESSOR_PPC604:
16203         case PROCESSOR_PPC8540:
16204           *total = COSTS_N_INSNS (4);
16205           return true;
16206
16207         case PROCESSOR_PPC620:
16208         case PROCESSOR_PPC630:
16209           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16210                     ? GET_MODE (XEXP (x, 1)) != DImode
16211                     ? COSTS_N_INSNS (5) : COSTS_N_INSNS (7)
16212                     : (INTVAL (XEXP (x, 1)) >= -256
16213                        && INTVAL (XEXP (x, 1)) <= 255)
16214                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
16215           return true;
16216
16217         case PROCESSOR_POWER4:
16218         case PROCESSOR_POWER5:
16219           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
16220                     ? GET_MODE (XEXP (x, 1)) != DImode
16221                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4)
16222                     : COSTS_N_INSNS (2));
16223           return true;
16224
16225         default:
16226           abort ();
16227         }
16228
16229     case DIV:
16230     case MOD:
16231       if (GET_CODE (XEXP (x, 1)) == CONST_INT
16232           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
16233         {
16234           *total = COSTS_N_INSNS (2);
16235           return true;
16236         }
16237       /* FALLTHRU */
16238
16239     case UDIV:
16240     case UMOD:
16241       switch (rs6000_cpu)
16242         {
16243         case PROCESSOR_RIOS1:
16244           *total = COSTS_N_INSNS (19);
16245           return true;
16246
16247         case PROCESSOR_RIOS2:
16248           *total = COSTS_N_INSNS (13);
16249           return true;
16250
16251         case PROCESSOR_RS64A:
16252           *total = (GET_MODE (XEXP (x, 1)) != DImode
16253                     ? COSTS_N_INSNS (65)
16254                     : COSTS_N_INSNS (67));
16255           return true;
16256
16257         case PROCESSOR_MPCCORE:
16258           *total = COSTS_N_INSNS (6);
16259           return true;
16260
16261         case PROCESSOR_PPC403:
16262           *total = COSTS_N_INSNS (33);
16263           return true;
16264
16265         case PROCESSOR_PPC405:
16266           *total = COSTS_N_INSNS (35);
16267           return true;
16268
16269         case PROCESSOR_PPC440:
16270           *total = COSTS_N_INSNS (34);
16271           return true;
16272
16273         case PROCESSOR_PPC601:
16274           *total = COSTS_N_INSNS (36);
16275           return true;
16276
16277         case PROCESSOR_PPC603:
16278           *total = COSTS_N_INSNS (37);
16279           return true;
16280
16281         case PROCESSOR_PPC604:
16282         case PROCESSOR_PPC604e:
16283           *total = COSTS_N_INSNS (20);
16284           return true;
16285
16286         case PROCESSOR_PPC620:
16287         case PROCESSOR_PPC630:
16288           *total = (GET_MODE (XEXP (x, 1)) != DImode
16289                     ? COSTS_N_INSNS (21)
16290                     : COSTS_N_INSNS (37));
16291           return true;
16292
16293         case PROCESSOR_PPC750:
16294         case PROCESSOR_PPC8540:
16295         case PROCESSOR_PPC7400:
16296           *total = COSTS_N_INSNS (19);
16297           return true;
16298
16299         case PROCESSOR_PPC7450:
16300           *total = COSTS_N_INSNS (23);
16301           return true;
16302
16303         case PROCESSOR_POWER4:
16304         case PROCESSOR_POWER5:
16305           *total = (GET_MODE (XEXP (x, 1)) != DImode
16306                     ? COSTS_N_INSNS (18)
16307                     : COSTS_N_INSNS (34));
16308           return true;
16309
16310         default:
16311           abort ();
16312         }
16313
16314     case FFS:
16315       *total = COSTS_N_INSNS (4);
16316       return true;
16317
16318     case MEM:
16319       /* MEM should be slightly more expensive than (plus (reg) (const)).  */
16320       *total = 5;
16321       return true;
16322
16323     default:
16324       return false;
16325     }
16326 }
16327
16328 /* A C expression returning the cost of moving data from a register of class
16329    CLASS1 to one of CLASS2.  */
16330
16331 int
16332 rs6000_register_move_cost (enum machine_mode mode, 
16333                            enum reg_class from, enum reg_class to)
16334 {
16335   /*  Moves from/to GENERAL_REGS.  */
16336   if (reg_classes_intersect_p (to, GENERAL_REGS)
16337       || reg_classes_intersect_p (from, GENERAL_REGS))
16338     {
16339       if (! reg_classes_intersect_p (to, GENERAL_REGS))
16340         from = to;
16341
16342       if (from == FLOAT_REGS || from == ALTIVEC_REGS)
16343         return (rs6000_memory_move_cost (mode, from, 0)
16344                 + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
16345
16346 /* It's more expensive to move CR_REGS than CR0_REGS because of the shift....  */
16347       else if (from == CR_REGS)
16348         return 4;
16349
16350       else
16351 /* A move will cost one instruction per GPR moved.  */
16352         return 2 * HARD_REGNO_NREGS (0, mode);
16353     }
16354
16355 /* Moving between two similar registers is just one instruction.  */
16356   else if (reg_classes_intersect_p (to, from))
16357     return mode == TFmode ? 4 : 2;
16358
16359 /* Everything else has to go through GENERAL_REGS.  */
16360   else
16361     return (rs6000_register_move_cost (mode, GENERAL_REGS, to) 
16362             + rs6000_register_move_cost (mode, from, GENERAL_REGS));
16363 }
16364
16365 /* A C expressions returning the cost of moving data of MODE from a register to
16366    or from memory.  */
16367
16368 int
16369 rs6000_memory_move_cost (enum machine_mode mode, enum reg_class class, 
16370                          int in ATTRIBUTE_UNUSED)
16371 {
16372   if (reg_classes_intersect_p (class, GENERAL_REGS))
16373     return 4 * HARD_REGNO_NREGS (0, mode);
16374   else if (reg_classes_intersect_p (class, FLOAT_REGS))
16375     return 4 * HARD_REGNO_NREGS (32, mode);
16376   else if (reg_classes_intersect_p (class, ALTIVEC_REGS))
16377     return 4 * HARD_REGNO_NREGS (FIRST_ALTIVEC_REGNO, mode);
16378   else
16379     return 4 + rs6000_register_move_cost (mode, class, GENERAL_REGS);
16380 }
16381
16382 /* Return an RTX representing where to find the function value of a
16383    function returning MODE.  */
16384 static rtx
16385 rs6000_complex_function_value (enum machine_mode mode)
16386 {
16387   unsigned int regno;
16388   rtx r1, r2;
16389   enum machine_mode inner = GET_MODE_INNER (mode);
16390   unsigned int inner_bytes = GET_MODE_SIZE (inner);
16391
16392   if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
16393     regno = FP_ARG_RETURN;
16394   else
16395     {
16396       regno = GP_ARG_RETURN;
16397
16398       /* 32-bit is OK since it'll go in r3/r4.  */
16399       if (TARGET_32BIT && inner_bytes >= 4)
16400         return gen_rtx_REG (mode, regno);
16401     }
16402
16403   if (inner_bytes >= 8)
16404     return gen_rtx_REG (mode, regno);
16405
16406   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
16407                           const0_rtx);
16408   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
16409                           GEN_INT (inner_bytes));
16410   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
16411 }
16412
16413 /* Define how to find the value returned by a function.
16414    VALTYPE is the data type of the value (as a tree).
16415    If the precise function being called is known, FUNC is its FUNCTION_DECL;
16416    otherwise, FUNC is 0.
16417
16418    On the SPE, both FPs and vectors are returned in r3.
16419
16420    On RS/6000 an integer value is in r3 and a floating-point value is in
16421    fp1, unless -msoft-float.  */
16422
16423 rtx
16424 rs6000_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
16425 {
16426   enum machine_mode mode;
16427   unsigned int regno;
16428
16429   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
16430     {
16431       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
16432       return gen_rtx_PARALLEL (DImode,
16433         gen_rtvec (2,
16434                    gen_rtx_EXPR_LIST (VOIDmode,
16435                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
16436                                       const0_rtx),
16437                    gen_rtx_EXPR_LIST (VOIDmode,
16438                                       gen_rtx_REG (SImode,
16439                                                    GP_ARG_RETURN + 1),
16440                                       GEN_INT (4))));
16441     }
16442
16443   if ((INTEGRAL_TYPE_P (valtype)
16444        && TYPE_PRECISION (valtype) < BITS_PER_WORD)
16445       || POINTER_TYPE_P (valtype))
16446     mode = TARGET_32BIT ? SImode : DImode;
16447   else
16448     mode = TYPE_MODE (valtype);
16449
16450   if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS)
16451     regno = FP_ARG_RETURN;
16452   else if (TREE_CODE (valtype) == COMPLEX_TYPE
16453            && targetm.calls.split_complex_arg)
16454     return rs6000_complex_function_value (mode);
16455   else if (TREE_CODE (valtype) == VECTOR_TYPE
16456            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
16457     regno = ALTIVEC_ARG_RETURN;
16458   else
16459     regno = GP_ARG_RETURN;
16460
16461   return gen_rtx_REG (mode, regno);
16462 }
16463
16464 /* Define how to find the value returned by a library function
16465    assuming the value has mode MODE.  */
16466 rtx
16467 rs6000_libcall_value (enum machine_mode mode)
16468 {
16469   unsigned int regno;
16470
16471   if (GET_MODE_CLASS (mode) == MODE_FLOAT
16472            && TARGET_HARD_FLOAT && TARGET_FPRS)
16473     regno = FP_ARG_RETURN;
16474   else if (ALTIVEC_VECTOR_MODE (mode)
16475            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
16476     regno = ALTIVEC_ARG_RETURN;
16477   else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
16478     return rs6000_complex_function_value (mode);
16479   else
16480     regno = GP_ARG_RETURN;
16481
16482   return gen_rtx_REG (mode, regno);
16483 }
16484
16485 /* Define the offset between two registers, FROM to be eliminated and its
16486    replacement TO, at the start of a routine.  */
16487 HOST_WIDE_INT
16488 rs6000_initial_elimination_offset (int from, int to)
16489 {
16490   rs6000_stack_t *info = rs6000_stack_info ();
16491   HOST_WIDE_INT offset;
16492
16493   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
16494     offset = info->push_p ? 0 : -info->total_size;
16495   else if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
16496     offset = info->total_size;
16497   else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
16498     offset = info->push_p ? info->total_size : 0;
16499   else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
16500     offset = 0;
16501   else
16502     abort ();
16503
16504   return offset;
16505 }
16506
16507 /* Return true if TYPE is of type __ev64_opaque__.  */
16508
16509 static bool
16510 is_ev64_opaque_type (tree type)
16511 {
16512   return (TARGET_SPE
16513           && (type == opaque_V2SI_type_node
16514               || type == opaque_V2SF_type_node
16515               || type == opaque_p_V2SI_type_node));
16516 }
16517
16518 static rtx
16519 rs6000_dwarf_register_span (rtx reg)
16520 {
16521   unsigned regno;
16522
16523   if (!TARGET_SPE || !SPE_VECTOR_MODE (GET_MODE (reg)))
16524     return NULL_RTX;
16525
16526   regno = REGNO (reg);
16527
16528   /* The duality of the SPE register size wreaks all kinds of havoc.
16529      This is a way of distinguishing r0 in 32-bits from r0 in
16530      64-bits.  */
16531   return
16532     gen_rtx_PARALLEL (VOIDmode,
16533                       BYTES_BIG_ENDIAN
16534                       ? gen_rtvec (2,
16535                                    gen_rtx_REG (SImode, regno + 1200),
16536                                    gen_rtx_REG (SImode, regno))
16537                       : gen_rtvec (2,
16538                                    gen_rtx_REG (SImode, regno),
16539                                    gen_rtx_REG (SImode, regno + 1200)));
16540 }
16541
16542 /* Map internal gcc register numbers to DWARF2 register numbers.  */
16543
16544 unsigned int
16545 rs6000_dbx_register_number (unsigned int regno)
16546 {
16547   if (regno <= 63 || write_symbols != DWARF2_DEBUG)
16548     return regno;
16549   if (regno == MQ_REGNO)
16550     return 100;
16551   if (regno == LINK_REGISTER_REGNUM)
16552     return 108;
16553   if (regno == COUNT_REGISTER_REGNUM)
16554     return 109;
16555   if (CR_REGNO_P (regno))
16556     return regno - CR0_REGNO + 86;
16557   if (regno == XER_REGNO)
16558     return 101;
16559   if (ALTIVEC_REGNO_P (regno))
16560     return regno - FIRST_ALTIVEC_REGNO + 1124;
16561   if (regno == VRSAVE_REGNO)
16562     return 356;
16563   if (regno == VSCR_REGNO)
16564     return 67;
16565   if (regno == SPE_ACC_REGNO)
16566     return 99;
16567   if (regno == SPEFSCR_REGNO)
16568     return 612;
16569   /* SPE high reg number.  We get these values of regno from
16570      rs6000_dwarf_register_span.  */
16571   if (regno >= 1200 && regno < 1232)
16572     return regno;
16573
16574   abort ();
16575 }
16576
16577 #include "gt-rs6000.h"