OSDN Git Service

2002-06-27 Aldy Hernandez <aldyh@redhat.com>
[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 Free Software Foundation, Inc.
4    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-attr.h"
32 #include "flags.h"
33 #include "recog.h"
34 #include "obstack.h"
35 #include "tree.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "except.h"
39 #include "function.h"
40 #include "output.h"
41 #include "basic-block.h"
42 #include "integrate.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "langhooks.h"
50 #include "reload.h"
51
52 #ifndef TARGET_NO_PROTOTYPE
53 #define TARGET_NO_PROTOTYPE 0
54 #endif
55
56 #define min(A,B)        ((A) < (B) ? (A) : (B))
57 #define max(A,B)        ((A) > (B) ? (A) : (B))
58
59 /* Target cpu type */
60
61 enum processor_type rs6000_cpu;
62 struct rs6000_cpu_select rs6000_select[3] =
63 {
64   /* switch             name,                   tune    arch */
65   { (const char *)0,    "--with-cpu=",          1,      1 },
66   { (const char *)0,    "-mcpu=",               1,      1 },
67   { (const char *)0,    "-mtune=",              1,      0 },
68 };
69
70 /* Size of long double */
71 const char *rs6000_long_double_size_string;
72 int rs6000_long_double_type_size;
73
74 /* Whether -mabi=altivec has appeared */
75 int rs6000_altivec_abi;
76
77 /* Whether VRSAVE instructions should be generated.  */
78 int rs6000_altivec_vrsave;
79
80 /* String from -mvrsave= option.  */
81 const char *rs6000_altivec_vrsave_string;
82
83 /* Set to non-zero once AIX common-mode calls have been defined.  */
84 static int common_mode_defined;
85
86 /* Save information from a "cmpxx" operation until the branch or scc is
87    emitted.  */
88 rtx rs6000_compare_op0, rs6000_compare_op1;
89 int rs6000_compare_fp_p;
90
91 /* Label number of label created for -mrelocatable, to call to so we can
92    get the address of the GOT section */
93 int rs6000_pic_labelno;
94
95 #ifdef USING_ELFOS_H
96 /* Which abi to adhere to */
97 const char *rs6000_abi_name = RS6000_ABI_NAME;
98
99 /* Semantics of the small data area */
100 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
101
102 /* Which small data model to use */
103 const char *rs6000_sdata_name = (char *)0;
104
105 /* Counter for labels which are to be placed in .fixup.  */
106 int fixuplabelno = 0;
107 #endif
108
109 /* ABI enumeration available for subtarget to use.  */
110 enum rs6000_abi rs6000_current_abi;
111
112 /* ABI string from -mabi= option.  */
113 const char *rs6000_abi_string;
114
115 /* Debug flags */
116 const char *rs6000_debug_name;
117 int rs6000_debug_stack;         /* debug stack applications */
118 int rs6000_debug_arg;           /* debug argument handling */
119
120 /* Flag to say the TOC is initialized */
121 int toc_initialized;
122 char toc_label_name[10];
123
124 /* Alias set for saves and restores from the rs6000 stack.  */
125 static int rs6000_sr_alias_set;
126
127 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
128    The only place that looks at this is rs6000_set_default_type_attributes;
129    everywhere else should rely on the presence or absence of a longcall
130    attribute on the function declaration.  */
131 int rs6000_default_long_calls;
132 const char *rs6000_longcall_switch;
133
134 static void rs6000_add_gc_roots PARAMS ((void));
135 static int num_insns_constant_wide PARAMS ((HOST_WIDE_INT));
136 static rtx expand_block_move_mem PARAMS ((enum machine_mode, rtx, rtx));
137 static void validate_condition_mode 
138   PARAMS ((enum rtx_code, enum machine_mode));
139 static rtx rs6000_generate_compare PARAMS ((enum rtx_code));
140 static void rs6000_maybe_dead PARAMS ((rtx));
141 static void rs6000_emit_stack_tie PARAMS ((void));
142 static void rs6000_frame_related PARAMS ((rtx, rtx, HOST_WIDE_INT, rtx, rtx));
143 static void rs6000_emit_allocate_stack PARAMS ((HOST_WIDE_INT, int));
144 static unsigned rs6000_hash_constant PARAMS ((rtx));
145 static unsigned toc_hash_function PARAMS ((const void *));
146 static int toc_hash_eq PARAMS ((const void *, const void *));
147 static int toc_hash_mark_entry PARAMS ((void **, void *));
148 static void toc_hash_mark_table PARAMS ((void *));
149 static int constant_pool_expr_1 PARAMS ((rtx, int *, int *));
150 static struct machine_function * rs6000_init_machine_status PARAMS ((void));
151 static bool rs6000_assemble_integer PARAMS ((rtx, unsigned int, int));
152 static int rs6000_ra_ever_killed PARAMS ((void));
153 static tree rs6000_handle_longcall_attribute PARAMS ((tree *, tree, tree, int, bool *));
154 const struct attribute_spec rs6000_attribute_table[];
155 static void rs6000_set_default_type_attributes PARAMS ((tree));
156 static void rs6000_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
157 static void rs6000_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
158 static rtx rs6000_emit_set_long_const PARAMS ((rtx,
159   HOST_WIDE_INT, HOST_WIDE_INT));
160 #if TARGET_ELF
161 static unsigned int rs6000_elf_section_type_flags PARAMS ((tree, const char *,
162                                                            int));
163 static void rs6000_elf_asm_out_constructor PARAMS ((rtx, int));
164 static void rs6000_elf_asm_out_destructor PARAMS ((rtx, int));
165 static void rs6000_elf_select_section PARAMS ((tree, int,
166                                                  unsigned HOST_WIDE_INT));
167 static void rs6000_elf_unique_section PARAMS ((tree, int));
168 static void rs6000_elf_select_rtx_section PARAMS ((enum machine_mode, rtx,
169                                                    unsigned HOST_WIDE_INT));
170 static void rs6000_elf_encode_section_info PARAMS ((tree, int));
171 static const char *rs6000_elf_strip_name_encoding PARAMS ((const char *));
172 #endif
173 #if TARGET_XCOFF
174 static void xcoff_asm_named_section PARAMS ((const char *, unsigned int));
175 static void rs6000_xcoff_select_section PARAMS ((tree, int,
176                                                  unsigned HOST_WIDE_INT));
177 static void rs6000_xcoff_unique_section PARAMS ((tree, int));
178 static void rs6000_xcoff_select_rtx_section PARAMS ((enum machine_mode, rtx,
179                                                      unsigned HOST_WIDE_INT));
180 static const char * rs6000_xcoff_strip_name_encoding PARAMS ((const char *));
181 #endif
182 static void rs6000_xcoff_encode_section_info PARAMS ((tree, int))
183      ATTRIBUTE_UNUSED;
184 static int rs6000_adjust_cost PARAMS ((rtx, rtx, rtx, int));
185 static int rs6000_adjust_priority PARAMS ((rtx, int));
186 static int rs6000_issue_rate PARAMS ((void));
187
188 static void rs6000_init_builtins PARAMS ((void));
189 static rtx rs6000_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx));
190 static rtx rs6000_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
191 static rtx rs6000_expand_ternop_builtin PARAMS ((enum insn_code, tree, rtx));
192 static rtx rs6000_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
193 static void altivec_init_builtins PARAMS ((void));
194 static rtx altivec_expand_builtin PARAMS ((tree, rtx, bool *));
195 static rtx altivec_expand_ld_builtin PARAMS ((tree, rtx, bool *));
196 static rtx altivec_expand_st_builtin PARAMS ((tree, rtx, bool *));
197 static rtx altivec_expand_dst_builtin PARAMS ((tree, rtx, bool *));
198 static rtx altivec_expand_abs_builtin PARAMS ((enum insn_code, tree, rtx));
199 static rtx altivec_expand_predicate_builtin PARAMS ((enum insn_code, const char *, tree, rtx));
200 static rtx altivec_expand_stv_builtin PARAMS ((enum insn_code, tree));
201 static void rs6000_parse_abi_options PARAMS ((void));
202 static void rs6000_parse_vrsave_option PARAMS ((void));
203 static int first_altivec_reg_to_save PARAMS ((void));
204 static unsigned int compute_vrsave_mask PARAMS ((void));
205 static void is_altivec_return_reg PARAMS ((rtx, void *));
206 int vrsave_operation PARAMS ((rtx, enum machine_mode));
207 static rtx generate_set_vrsave PARAMS ((rtx, rs6000_stack_t *, int));
208 static void altivec_frame_fixup PARAMS ((rtx, rtx, HOST_WIDE_INT));
209 static int easy_vector_constant PARAMS ((rtx));
210 \f
211 /* Default register names.  */
212 char rs6000_reg_names[][8] =
213 {
214       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
215       "8",  "9", "10", "11", "12", "13", "14", "15",
216      "16", "17", "18", "19", "20", "21", "22", "23",
217      "24", "25", "26", "27", "28", "29", "30", "31",
218       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
219       "8",  "9", "10", "11", "12", "13", "14", "15",
220      "16", "17", "18", "19", "20", "21", "22", "23",
221      "24", "25", "26", "27", "28", "29", "30", "31",
222      "mq", "lr", "ctr","ap",
223       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
224       "xer",
225       /* AltiVec registers.  */
226       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
227       "8",  "9",  "10", "11", "12", "13", "14", "15",
228       "16", "17", "18", "19", "20", "21", "22", "23",
229       "24", "25", "26", "27", "28", "29", "30", "31",
230       "vrsave"
231 };
232
233 #ifdef TARGET_REGNAMES
234 static const char alt_reg_names[][8] =
235 {
236    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
237    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
238   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
239   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
240    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
241    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
242   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
243   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
244     "mq",    "lr",  "ctr",   "ap",
245   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
246    "xer",
247    /* AltiVec registers.  */
248    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
249    "%v8",  "%v9",  "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
250    "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
251    "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
252    "vrsave"
253 };
254 #endif
255 \f
256 #ifndef MASK_STRICT_ALIGN
257 #define MASK_STRICT_ALIGN 0
258 #endif
259 \f
260 /* Initialize the GCC target structure.  */
261 #undef TARGET_ATTRIBUTE_TABLE
262 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
263 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
264 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
265
266 #undef TARGET_ASM_ALIGNED_DI_OP
267 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
268
269 /* Default unaligned ops are only provided for ELF.  Find the ops needed
270    for non-ELF systems.  */
271 #ifndef OBJECT_FORMAT_ELF
272 #if TARGET_XCOFF
273 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
274    64-bit targets.  */
275 #undef TARGET_ASM_UNALIGNED_HI_OP
276 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
277 #undef TARGET_ASM_UNALIGNED_SI_OP
278 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
279 #undef TARGET_ASM_UNALIGNED_DI_OP
280 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
281 #else
282 /* For Darwin.  */
283 #undef TARGET_ASM_UNALIGNED_HI_OP
284 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
285 #undef TARGET_ASM_UNALIGNED_SI_OP
286 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
287 #endif
288 #endif
289
290 /* This hook deals with fixups for relocatable code and DI-mode objects
291    in 64-bit code.  */
292 #undef TARGET_ASM_INTEGER
293 #define TARGET_ASM_INTEGER rs6000_assemble_integer
294
295 #undef TARGET_ASM_FUNCTION_PROLOGUE
296 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
297 #undef TARGET_ASM_FUNCTION_EPILOGUE
298 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
299
300 #if TARGET_ELF
301 #undef TARGET_SECTION_TYPE_FLAGS
302 #define TARGET_SECTION_TYPE_FLAGS  rs6000_elf_section_type_flags
303 #endif
304
305 #undef TARGET_SCHED_ISSUE_RATE
306 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
307 #undef TARGET_SCHED_ADJUST_COST
308 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
309 #undef TARGET_SCHED_ADJUST_PRIORITY
310 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
311
312 #undef TARGET_INIT_BUILTINS
313 #define TARGET_INIT_BUILTINS rs6000_init_builtins
314
315 #undef TARGET_EXPAND_BUILTIN
316 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
317
318 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
319 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
320
321 struct gcc_target targetm = TARGET_INITIALIZER;
322 \f
323 /* Override command line options.  Mostly we process the processor
324    type and sometimes adjust other TARGET_ options.  */
325
326 void
327 rs6000_override_options (default_cpu)
328      const char *default_cpu;
329 {
330   size_t i, j;
331   struct rs6000_cpu_select *ptr;
332
333   /* Simplify the entries below by making a mask for any POWER
334      variant and any PowerPC variant.  */
335
336 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
337 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
338                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
339 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
340
341   static struct ptt
342     {
343       const char *const name;           /* Canonical processor name.  */
344       const enum processor_type processor; /* Processor type enum value.  */
345       const int target_enable;  /* Target flags to enable.  */
346       const int target_disable; /* Target flags to disable.  */
347     } const processor_target_table[]
348       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
349             POWER_MASKS | POWERPC_MASKS},
350          {"power", PROCESSOR_POWER,
351             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
352             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
353          {"power2", PROCESSOR_POWER,
354             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
355             POWERPC_MASKS | MASK_NEW_MNEMONICS},
356          {"power3", PROCESSOR_PPC630,
357             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
358             POWER_MASKS | MASK_PPC_GPOPT},
359          {"power4", PROCESSOR_POWER4,
360             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
361             POWER_MASKS | MASK_PPC_GPOPT},
362          {"powerpc", PROCESSOR_POWERPC,
363             MASK_POWERPC | MASK_NEW_MNEMONICS,
364             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
365          {"powerpc64", PROCESSOR_POWERPC64,
366             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
367             POWER_MASKS | POWERPC_OPT_MASKS},
368          {"rios", PROCESSOR_RIOS1,
369             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
370             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
371          {"rios1", PROCESSOR_RIOS1,
372             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
373             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
374          {"rsc", PROCESSOR_PPC601,
375             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
376             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
377          {"rsc1", PROCESSOR_PPC601,
378             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
379             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
380          {"rios2", PROCESSOR_RIOS2,
381             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
382             POWERPC_MASKS | MASK_NEW_MNEMONICS},
383          {"rs64a", PROCESSOR_RS64A,
384             MASK_POWERPC | MASK_NEW_MNEMONICS,
385             POWER_MASKS | POWERPC_OPT_MASKS},
386          {"401", PROCESSOR_PPC403,
387             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
388             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
389          {"403", PROCESSOR_PPC403,
390             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
391             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
392          {"405", PROCESSOR_PPC405,
393             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
394             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
395          {"505", PROCESSOR_MPCCORE,
396             MASK_POWERPC | MASK_NEW_MNEMONICS,
397             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
398          {"601", PROCESSOR_PPC601,
399             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
400             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
401          {"602", PROCESSOR_PPC603,
402             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
403             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
404          {"603", PROCESSOR_PPC603,
405             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
406             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
407          {"603e", PROCESSOR_PPC603,
408             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
409             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
410          {"ec603e", PROCESSOR_PPC603,
411             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
412             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
413          {"604", PROCESSOR_PPC604,
414             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
415             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
416          {"604e", PROCESSOR_PPC604e,
417             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
418             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
419          {"620", PROCESSOR_PPC620,
420             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
421             POWER_MASKS | MASK_PPC_GPOPT},
422          {"630", PROCESSOR_PPC630,
423             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
424             POWER_MASKS | MASK_PPC_GPOPT},
425          {"740", PROCESSOR_PPC750,
426             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
427             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
428          {"750", PROCESSOR_PPC750,
429             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
430             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
431          {"7400", PROCESSOR_PPC7400,
432             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
433             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
434          {"7450", PROCESSOR_PPC7450,
435             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
436             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
437          {"801", PROCESSOR_MPCCORE,
438             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
439             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
440          {"821", PROCESSOR_MPCCORE,
441             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
442             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
443          {"823", PROCESSOR_MPCCORE,
444             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
445             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
446          {"860", PROCESSOR_MPCCORE,
447             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
448             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64}};
449
450   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
451
452   /* Save current -mmultiple/-mno-multiple status.  */
453   int multiple = TARGET_MULTIPLE;
454   /* Save current -mstring/-mno-string status.  */
455   int string = TARGET_STRING;
456
457   /* Identify the processor type.  */
458   rs6000_select[0].string = default_cpu;
459   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
460
461   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
462     {
463       ptr = &rs6000_select[i];
464       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
465         {
466           for (j = 0; j < ptt_size; j++)
467             if (! strcmp (ptr->string, processor_target_table[j].name))
468               {
469                 if (ptr->set_tune_p)
470                   rs6000_cpu = processor_target_table[j].processor;
471
472                 if (ptr->set_arch_p)
473                   {
474                     target_flags |= processor_target_table[j].target_enable;
475                     target_flags &= ~processor_target_table[j].target_disable;
476                   }
477                 break;
478               }
479
480           if (j == ptt_size)
481             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
482         }
483     }
484
485   /* If we are optimizing big endian systems for space, use the store
486      multiple instructions.  */
487   if (BYTES_BIG_ENDIAN && optimize_size)
488     target_flags |= MASK_MULTIPLE;
489
490   /* If -mmultiple or -mno-multiple was explicitly used, don't
491      override with the processor default */
492   if (TARGET_MULTIPLE_SET)
493     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
494
495   /* If -mstring or -mno-string was explicitly used, don't override
496      with the processor default.  */
497   if (TARGET_STRING_SET)
498     target_flags = (target_flags & ~MASK_STRING) | string;
499
500   /* Don't allow -mmultiple or -mstring on little endian systems
501      unless the cpu is a 750, because the hardware doesn't support the
502      instructions used in little endian mode, and causes an alignment
503      trap.  The 750 does not cause an alignment trap (except when the
504      target is unaligned).  */
505
506   if (! BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
507     {
508       if (TARGET_MULTIPLE)
509         {
510           target_flags &= ~MASK_MULTIPLE;
511           if (TARGET_MULTIPLE_SET)
512             warning ("-mmultiple is not supported on little endian systems");
513         }
514
515       if (TARGET_STRING)
516         {
517           target_flags &= ~MASK_STRING;
518           if (TARGET_STRING_SET)
519             warning ("-mstring is not supported on little endian systems");
520         }
521     }
522
523   if (flag_pic != 0 && DEFAULT_ABI == ABI_AIX)
524     {
525       flag_pic = 0;
526
527       if (extra_warnings)
528         warning ("-f%s ignored (all code is position independent)",
529                  (flag_pic > 1) ? "PIC" : "pic");
530     }
531
532 #ifdef XCOFF_DEBUGGING_INFO
533   if (flag_function_sections && (write_symbols != NO_DEBUG)
534       && DEFAULT_ABI == ABI_AIX)
535     {
536       warning ("-ffunction-sections disabled on AIX when debugging");
537       flag_function_sections = 0;
538     }
539
540   if (flag_data_sections && (DEFAULT_ABI == ABI_AIX))
541     {
542       warning ("-fdata-sections not supported on AIX");
543       flag_data_sections = 0;
544     }
545 #endif
546
547 #ifdef SUBTARGET_OVERRIDE_OPTIONS
548   SUBTARGET_OVERRIDE_OPTIONS;
549 #endif
550 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
551   SUBSUBTARGET_OVERRIDE_OPTIONS;
552 #endif
553
554   /* Set debug flags */
555   if (rs6000_debug_name)
556     {
557       if (! strcmp (rs6000_debug_name, "all"))
558         rs6000_debug_stack = rs6000_debug_arg = 1;
559       else if (! strcmp (rs6000_debug_name, "stack"))
560         rs6000_debug_stack = 1;
561       else if (! strcmp (rs6000_debug_name, "arg"))
562         rs6000_debug_arg = 1;
563       else
564         error ("unknown -mdebug-%s switch", rs6000_debug_name);
565     }
566
567   /* Set size of long double */
568   rs6000_long_double_type_size = 64;
569   if (rs6000_long_double_size_string)
570     {
571       char *tail;
572       int size = strtol (rs6000_long_double_size_string, &tail, 10);
573       if (*tail != '\0' || (size != 64 && size != 128))
574         error ("Unknown switch -mlong-double-%s",
575                rs6000_long_double_size_string);
576       else
577         rs6000_long_double_type_size = size;
578     }
579
580   /* Handle -mabi= options.  */
581   rs6000_parse_abi_options ();
582
583   /* Handle -mvrsave= option.  */
584   rs6000_parse_vrsave_option ();
585
586   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
587      using TARGET_OPTIONS to handle a toggle switch, but we're out of
588      bits in target_flags so TARGET_SWITCHES cannot be used.
589      Assumption here is that rs6000_longcall_switch points into the
590      text of the complete option, rather than being a copy, so we can
591      scan back for the presence or absence of the no- modifier.  */
592   if (rs6000_longcall_switch)
593     {
594       const char *base = rs6000_longcall_switch;
595       while (base[-1] != 'm') base--;
596
597       if (*rs6000_longcall_switch != '\0')
598         error ("invalid option `%s'", base);
599       rs6000_default_long_calls = (base[0] != 'n');
600     }
601
602 #ifdef TARGET_REGNAMES
603   /* If the user desires alternate register names, copy in the
604      alternate names now.  */
605   if (TARGET_REGNAMES)
606     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
607 #endif
608
609   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
610      If -maix-struct-return or -msvr4-struct-return was explicitly
611      used, don't override with the ABI default.  */
612   if (!(target_flags & MASK_AIX_STRUCT_RET_SET))
613     {
614       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
615         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
616       else
617         target_flags |= MASK_AIX_STRUCT_RET;
618     }
619
620   /* Register global variables with the garbage collector.  */
621   rs6000_add_gc_roots ();
622
623   /* Allocate an alias set for register saves & restores from stack.  */
624   rs6000_sr_alias_set = new_alias_set ();
625
626   if (TARGET_TOC) 
627     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
628
629   /* We can only guarantee the availability of DI pseudo-ops when
630      assembling for 64-bit targets.  */
631   if (!TARGET_64BIT)
632     {
633       targetm.asm_out.aligned_op.di = NULL;
634       targetm.asm_out.unaligned_op.di = NULL;
635     }
636
637   /* Arrange to save and restore machine status around nested functions.  */
638   init_machine_status = rs6000_init_machine_status;
639 }
640
641 /* Handle -mvrsave= options.  */
642 static void
643 rs6000_parse_vrsave_option ()
644 {
645   /* Generate VRSAVE instructions by default.  */
646   if (rs6000_altivec_vrsave_string == 0
647       || ! strcmp (rs6000_altivec_vrsave_string, "yes"))
648     rs6000_altivec_vrsave = 1;
649   else if (! strcmp (rs6000_altivec_vrsave_string, "no"))
650     rs6000_altivec_vrsave = 0;
651   else
652     error ("unknown -mvrsave= option specified: '%s'",
653            rs6000_altivec_vrsave_string);
654 }
655
656 /* Handle -mabi= options.  */
657 static void
658 rs6000_parse_abi_options ()
659 {
660   if (rs6000_abi_string == 0)
661     return;
662   else if (! strcmp (rs6000_abi_string, "altivec"))
663     rs6000_altivec_abi = 1;
664   else if (! strcmp (rs6000_abi_string, "no-altivec"))
665     rs6000_altivec_abi = 0;
666   else
667     error ("unknown ABI specified: '%s'", rs6000_abi_string);
668 }
669
670 void
671 optimization_options (level, size)
672      int level ATTRIBUTE_UNUSED;
673      int size ATTRIBUTE_UNUSED;
674 {
675 }
676 \f
677 /* Do anything needed at the start of the asm file.  */
678
679 void
680 rs6000_file_start (file, default_cpu)
681      FILE *file;
682      const char *default_cpu;
683 {
684   size_t i;
685   char buffer[80];
686   const char *start = buffer;
687   struct rs6000_cpu_select *ptr;
688
689   if (flag_verbose_asm)
690     {
691       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
692       rs6000_select[0].string = default_cpu;
693
694       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
695         {
696           ptr = &rs6000_select[i];
697           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
698             {
699               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
700               start = "";
701             }
702         }
703
704 #ifdef USING_ELFOS_H
705       switch (rs6000_sdata)
706         {
707         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
708         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
709         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
710         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
711         }
712
713       if (rs6000_sdata && g_switch_value)
714         {
715           fprintf (file, "%s -G %d", start, g_switch_value);
716           start = "";
717         }
718 #endif
719
720       if (*start == '\0')
721         putc ('\n', file);
722     }
723 }
724 \f
725 /* Return non-zero if this function is known to have a null epilogue.  */
726
727 int
728 direct_return ()
729 {
730   if (reload_completed)
731     {
732       rs6000_stack_t *info = rs6000_stack_info ();
733
734       if (info->first_gp_reg_save == 32
735           && info->first_fp_reg_save == 64
736           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
737           && ! info->lr_save_p
738           && ! info->cr_save_p
739           && info->vrsave_mask == 0
740           && ! info->push_p)
741         return 1;
742     }
743
744   return 0;
745 }
746
747 /* Returns 1 always.  */
748
749 int
750 any_operand (op, mode)
751      rtx op ATTRIBUTE_UNUSED;
752      enum machine_mode mode ATTRIBUTE_UNUSED;
753 {
754   return 1;
755 }
756
757 /* Returns 1 if op is the count register.  */
758 int
759 count_register_operand (op, mode)
760      rtx op;
761      enum machine_mode mode ATTRIBUTE_UNUSED;
762 {
763   if (GET_CODE (op) != REG)
764     return 0;
765
766   if (REGNO (op) == COUNT_REGISTER_REGNUM)
767     return 1;
768
769   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
770     return 1;
771
772   return 0;
773 }
774
775 /* Returns 1 if op is an altivec register.  */
776 int
777 altivec_register_operand (op, mode)
778      rtx op;
779      enum machine_mode mode ATTRIBUTE_UNUSED;
780 {
781   
782   return (register_operand (op, mode)
783           && (GET_CODE (op) != REG
784               || REGNO (op) > FIRST_PSEUDO_REGISTER
785               || ALTIVEC_REGNO_P (REGNO (op))));
786 }
787
788 int
789 xer_operand (op, mode)
790      rtx op;
791      enum machine_mode mode ATTRIBUTE_UNUSED;
792 {
793   if (GET_CODE (op) != REG)
794     return 0;
795
796   if (XER_REGNO_P (REGNO (op)))
797     return 1;
798
799   return 0;
800 }
801
802 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
803    by such constants completes more quickly.  */
804
805 int
806 s8bit_cint_operand (op, mode)
807      rtx op;
808      enum machine_mode mode ATTRIBUTE_UNUSED;
809 {
810   return ( GET_CODE (op) == CONST_INT
811           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
812 }
813
814 /* Return 1 if OP is a constant that can fit in a D field.  */
815
816 int
817 short_cint_operand (op, mode)
818      rtx op;
819      enum machine_mode mode ATTRIBUTE_UNUSED;
820 {
821   return (GET_CODE (op) == CONST_INT
822           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
823 }
824
825 /* Similar for an unsigned D field.  */
826
827 int
828 u_short_cint_operand (op, mode)
829      rtx op;
830      enum machine_mode mode ATTRIBUTE_UNUSED;
831 {
832   return (GET_CODE (op) == CONST_INT
833           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
834 }
835
836 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
837
838 int
839 non_short_cint_operand (op, mode)
840      rtx op;
841      enum machine_mode mode ATTRIBUTE_UNUSED;
842 {
843   return (GET_CODE (op) == CONST_INT
844           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
845 }
846
847 /* Returns 1 if OP is a CONST_INT that is a positive value
848    and an exact power of 2.  */
849
850 int
851 exact_log2_cint_operand (op, mode)
852      rtx op;
853      enum machine_mode mode ATTRIBUTE_UNUSED;
854 {
855   return (GET_CODE (op) == CONST_INT
856           && INTVAL (op) > 0
857           && exact_log2 (INTVAL (op)) >= 0);
858 }
859
860 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
861    ctr, or lr).  */
862
863 int
864 gpc_reg_operand (op, mode)
865      rtx op;
866      enum machine_mode mode;
867 {
868   return (register_operand (op, mode)
869           && (GET_CODE (op) != REG
870               || (REGNO (op) >= ARG_POINTER_REGNUM 
871                   && !XER_REGNO_P (REGNO (op)))
872               || REGNO (op) < MQ_REGNO));
873 }
874
875 /* Returns 1 if OP is either a pseudo-register or a register denoting a
876    CR field.  */
877
878 int
879 cc_reg_operand (op, mode)
880      rtx op;
881      enum machine_mode mode;
882 {
883   return (register_operand (op, mode)
884           && (GET_CODE (op) != REG
885               || REGNO (op) >= FIRST_PSEUDO_REGISTER
886               || CR_REGNO_P (REGNO (op))));
887 }
888
889 /* Returns 1 if OP is either a pseudo-register or a register denoting a
890    CR field that isn't CR0.  */
891
892 int
893 cc_reg_not_cr0_operand (op, mode)
894      rtx op;
895      enum machine_mode mode;
896 {
897   return (register_operand (op, mode)
898           && (GET_CODE (op) != REG
899               || REGNO (op) >= FIRST_PSEUDO_REGISTER
900               || CR_REGNO_NOT_CR0_P (REGNO (op))));
901 }
902
903 /* Returns 1 if OP is either a constant integer valid for a D-field or
904    a non-special register.  If a register, it must be in the proper
905    mode unless MODE is VOIDmode.  */
906
907 int
908 reg_or_short_operand (op, mode)
909       rtx op;
910       enum machine_mode mode;
911 {
912   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
913 }
914
915 /* Similar, except check if the negation of the constant would be
916    valid for a D-field.  */
917
918 int
919 reg_or_neg_short_operand (op, mode)
920       rtx op;
921       enum machine_mode mode;
922 {
923   if (GET_CODE (op) == CONST_INT)
924     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
925
926   return gpc_reg_operand (op, mode);
927 }
928
929 /* Returns 1 if OP is either a constant integer valid for a DS-field or
930    a non-special register.  If a register, it must be in the proper
931    mode unless MODE is VOIDmode.  */
932
933 int
934 reg_or_aligned_short_operand (op, mode)
935       rtx op;
936       enum machine_mode mode;
937 {
938   if (gpc_reg_operand (op, mode))
939     return 1;
940   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
941     return 1;
942
943   return 0;
944 }
945
946
947 /* Return 1 if the operand is either a register or an integer whose
948    high-order 16 bits are zero.  */
949
950 int
951 reg_or_u_short_operand (op, mode)
952      rtx op;
953      enum machine_mode mode;
954 {
955   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
956 }
957
958 /* Return 1 is the operand is either a non-special register or ANY
959    constant integer.  */
960
961 int
962 reg_or_cint_operand (op, mode)
963     rtx op;
964     enum machine_mode mode;
965 {
966   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
967 }
968
969 /* Return 1 is the operand is either a non-special register or ANY
970    32-bit signed constant integer.  */
971
972 int
973 reg_or_arith_cint_operand (op, mode)
974     rtx op;
975     enum machine_mode mode;
976 {
977   return (gpc_reg_operand (op, mode)
978           || (GET_CODE (op) == CONST_INT
979 #if HOST_BITS_PER_WIDE_INT != 32
980               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
981                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
982 #endif
983               ));
984 }
985
986 /* Return 1 is the operand is either a non-special register or a 32-bit
987    signed constant integer valid for 64-bit addition.  */
988
989 int
990 reg_or_add_cint64_operand (op, mode)
991     rtx op;
992     enum machine_mode mode;
993 {
994   return (gpc_reg_operand (op, mode)
995           || (GET_CODE (op) == CONST_INT
996 #if HOST_BITS_PER_WIDE_INT == 32
997               && INTVAL (op) < 0x7fff8000
998 #else
999               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1000                   < 0x100000000ll)
1001 #endif
1002               ));
1003 }
1004
1005 /* Return 1 is the operand is either a non-special register or a 32-bit
1006    signed constant integer valid for 64-bit subtraction.  */
1007
1008 int
1009 reg_or_sub_cint64_operand (op, mode)
1010     rtx op;
1011     enum machine_mode mode;
1012 {
1013   return (gpc_reg_operand (op, mode)
1014           || (GET_CODE (op) == CONST_INT
1015 #if HOST_BITS_PER_WIDE_INT == 32
1016               && (- INTVAL (op)) < 0x7fff8000
1017 #else
1018               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1019                   < 0x100000000ll)
1020 #endif
1021               ));
1022 }
1023
1024 /* Return 1 is the operand is either a non-special register or ANY
1025    32-bit unsigned constant integer.  */
1026
1027 int
1028 reg_or_logical_cint_operand (op, mode)
1029     rtx op;
1030     enum machine_mode mode;
1031 {
1032   if (GET_CODE (op) == CONST_INT)
1033     {
1034       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1035         {
1036           if (GET_MODE_BITSIZE (mode) <= 32)
1037             abort ();
1038
1039           if (INTVAL (op) < 0)
1040             return 0;
1041         }
1042
1043       return ((INTVAL (op) & GET_MODE_MASK (mode)
1044                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1045     }
1046   else if (GET_CODE (op) == CONST_DOUBLE)
1047     {
1048       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1049           || mode != DImode)
1050         abort ();
1051
1052       return CONST_DOUBLE_HIGH (op) == 0;
1053     }
1054   else 
1055     return gpc_reg_operand (op, mode);
1056 }
1057
1058 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1059
1060 int
1061 got_operand (op, mode)
1062      rtx op;
1063      enum machine_mode mode ATTRIBUTE_UNUSED;
1064 {
1065   return (GET_CODE (op) == SYMBOL_REF
1066           || GET_CODE (op) == CONST
1067           || GET_CODE (op) == LABEL_REF);
1068 }
1069
1070 /* Return 1 if the operand is a simple references that can be loaded via
1071    the GOT (labels involving addition aren't allowed).  */
1072
1073 int
1074 got_no_const_operand (op, mode)
1075      rtx op;
1076      enum machine_mode mode ATTRIBUTE_UNUSED;
1077 {
1078   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1079 }
1080
1081 /* Return the number of instructions it takes to form a constant in an
1082    integer register.  */
1083
1084 static int
1085 num_insns_constant_wide (value)
1086      HOST_WIDE_INT value;
1087 {
1088   /* signed constant loadable with {cal|addi} */
1089   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1090     return 1;
1091
1092   /* constant loadable with {cau|addis} */
1093   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1094     return 1;
1095
1096 #if HOST_BITS_PER_WIDE_INT == 64
1097   else if (TARGET_POWERPC64)
1098     {
1099       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1100       HOST_WIDE_INT high = value >> 31;
1101
1102       if (high == 0 || high == -1)
1103         return 2;
1104
1105       high >>= 1;
1106
1107       if (low == 0)
1108         return num_insns_constant_wide (high) + 1;
1109       else
1110         return (num_insns_constant_wide (high)
1111                 + num_insns_constant_wide (low) + 1);
1112     }
1113 #endif
1114
1115   else
1116     return 2;
1117 }
1118
1119 int
1120 num_insns_constant (op, mode)
1121      rtx op;
1122      enum machine_mode mode;
1123 {
1124   if (GET_CODE (op) == CONST_INT)
1125     {
1126 #if HOST_BITS_PER_WIDE_INT == 64
1127       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1128           && mask64_operand (op, mode))
1129             return 2;
1130       else
1131 #endif
1132         return num_insns_constant_wide (INTVAL (op));
1133     }
1134
1135   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1136     {
1137       long l;
1138       REAL_VALUE_TYPE rv;
1139
1140       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1141       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1142       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1143     }
1144
1145   else if (GET_CODE (op) == CONST_DOUBLE)
1146     {
1147       HOST_WIDE_INT low;
1148       HOST_WIDE_INT high;
1149       long l[2];
1150       REAL_VALUE_TYPE rv;
1151       int endian = (WORDS_BIG_ENDIAN == 0);
1152
1153       if (mode == VOIDmode || mode == DImode)
1154         {
1155           high = CONST_DOUBLE_HIGH (op);
1156           low  = CONST_DOUBLE_LOW (op);
1157         }
1158       else
1159         {
1160           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1161           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1162           high = l[endian];
1163           low  = l[1 - endian];
1164         }
1165
1166       if (TARGET_32BIT)
1167         return (num_insns_constant_wide (low)
1168                 + num_insns_constant_wide (high));
1169
1170       else
1171         {
1172           if (high == 0 && low >= 0)
1173             return num_insns_constant_wide (low);
1174
1175           else if (high == -1 && low < 0)
1176             return num_insns_constant_wide (low);
1177
1178           else if (mask64_operand (op, mode))
1179             return 2;
1180
1181           else if (low == 0)
1182             return num_insns_constant_wide (high) + 1;
1183
1184           else
1185             return (num_insns_constant_wide (high)
1186                     + num_insns_constant_wide (low) + 1);
1187         }
1188     }
1189
1190   else
1191     abort ();
1192 }
1193
1194 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1195    register with one instruction per word.  We only do this if we can
1196    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1197
1198 int
1199 easy_fp_constant (op, mode)
1200      rtx op;
1201      enum machine_mode mode;
1202 {
1203   if (GET_CODE (op) != CONST_DOUBLE
1204       || GET_MODE (op) != mode
1205       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1206     return 0;
1207
1208   /* Consider all constants with -msoft-float to be easy.  */
1209   if (TARGET_SOFT_FLOAT && mode != DImode)
1210     return 1;
1211
1212   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1213   if (flag_pic && DEFAULT_ABI == ABI_V4)
1214     return 0;
1215
1216 #ifdef TARGET_RELOCATABLE
1217   /* Similarly if we are using -mrelocatable, consider all constants
1218      to be hard.  */
1219   if (TARGET_RELOCATABLE)
1220     return 0;
1221 #endif
1222
1223   if (mode == DFmode)
1224     {
1225       long k[2];
1226       REAL_VALUE_TYPE rv;
1227
1228       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1229       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1230
1231       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1232               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1233     }
1234
1235   else if (mode == SFmode)
1236     {
1237       long l;
1238       REAL_VALUE_TYPE rv;
1239
1240       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1241       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1242
1243       return num_insns_constant_wide (l) == 1;
1244     }
1245
1246   else if (mode == DImode)
1247     return ((TARGET_POWERPC64
1248              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1249             || (num_insns_constant (op, DImode) <= 2));
1250
1251   else if (mode == SImode)
1252     return 1;
1253   else
1254     abort ();
1255 }
1256
1257 /* Return 1 if the operand is a CONST_INT and can be put into a
1258    register with one instruction.  */
1259
1260 static int
1261 easy_vector_constant (op)
1262      rtx op;
1263 {
1264   rtx elt;
1265   int units, i;
1266
1267   if (GET_CODE (op) != CONST_VECTOR)
1268     return 0;
1269
1270   units = CONST_VECTOR_NUNITS (op);
1271
1272   /* We can generate 0 easily.  Look for that.  */
1273   for (i = 0; i < units; ++i)
1274     {
1275       elt = CONST_VECTOR_ELT (op, i);
1276
1277       /* We could probably simplify this by just checking for equality
1278          with CONST0_RTX for the current mode, but let's be safe
1279          instead.  */
1280
1281       switch (GET_CODE (elt))
1282         {
1283         case CONST_INT:
1284           if (INTVAL (elt) != 0)
1285             return 0;
1286           break;
1287         case CONST_DOUBLE:
1288           if (CONST_DOUBLE_LOW (elt) != 0 || CONST_DOUBLE_HIGH (elt) != 0)
1289             return 0;
1290           break;
1291         default:
1292           return 0;
1293         }
1294     }
1295
1296   /* We could probably generate a few other constants trivially, but
1297      gcc doesn't generate them yet.  FIXME later.  */
1298   return 1;
1299 }
1300
1301 /* Return 1 if the operand is the constant 0.  This works for scalars
1302    as well as vectors.  */
1303 int
1304 zero_constant (op, mode)
1305      rtx op;
1306      enum machine_mode mode;
1307 {
1308   return op == CONST0_RTX (mode);
1309 }
1310
1311 /* Return 1 if the operand is 0.0.  */
1312 int
1313 zero_fp_constant (op, mode)
1314      rtx op;
1315      enum machine_mode mode;
1316 {
1317   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1318 }
1319
1320 /* Return 1 if the operand is in volatile memory.  Note that during
1321    the RTL generation phase, memory_operand does not return TRUE for
1322    volatile memory references.  So this function allows us to
1323    recognize volatile references where its safe.  */
1324
1325 int
1326 volatile_mem_operand (op, mode)
1327      rtx op;
1328      enum machine_mode mode;
1329 {
1330   if (GET_CODE (op) != MEM)
1331     return 0;
1332
1333   if (!MEM_VOLATILE_P (op))
1334     return 0;
1335
1336   if (mode != GET_MODE (op))
1337     return 0;
1338
1339   if (reload_completed)
1340     return memory_operand (op, mode);
1341
1342   if (reload_in_progress)
1343     return strict_memory_address_p (mode, XEXP (op, 0));
1344
1345   return memory_address_p (mode, XEXP (op, 0));
1346 }
1347
1348 /* Return 1 if the operand is an offsettable memory operand.  */
1349
1350 int
1351 offsettable_mem_operand (op, mode)
1352      rtx op;
1353      enum machine_mode mode;
1354 {
1355   return ((GET_CODE (op) == MEM)
1356           && offsettable_address_p (reload_completed || reload_in_progress,
1357                                     mode, XEXP (op, 0)));
1358 }
1359
1360 /* Return 1 if the operand is either an easy FP constant (see above) or
1361    memory.  */
1362
1363 int
1364 mem_or_easy_const_operand (op, mode)
1365      rtx op;
1366      enum machine_mode mode;
1367 {
1368   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1369 }
1370
1371 /* Return 1 if the operand is either a non-special register or an item
1372    that can be used as the operand of a `mode' add insn.  */
1373
1374 int
1375 add_operand (op, mode)
1376     rtx op;
1377     enum machine_mode mode;
1378 {
1379   if (GET_CODE (op) == CONST_INT)
1380     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1381             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1382
1383   return gpc_reg_operand (op, mode);
1384 }
1385
1386 /* Return 1 if OP is a constant but not a valid add_operand.  */
1387
1388 int
1389 non_add_cint_operand (op, mode)
1390      rtx op;
1391      enum machine_mode mode ATTRIBUTE_UNUSED;
1392 {
1393   return (GET_CODE (op) == CONST_INT
1394           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1395           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1396 }
1397
1398 /* Return 1 if the operand is a non-special register or a constant that
1399    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1400
1401 int
1402 logical_operand (op, mode)
1403      rtx op;
1404      enum machine_mode mode;
1405 {
1406   HOST_WIDE_INT opl, oph;
1407
1408   if (gpc_reg_operand (op, mode))
1409     return 1;
1410
1411   if (GET_CODE (op) == CONST_INT)
1412     {
1413       opl = INTVAL (op) & GET_MODE_MASK (mode);
1414
1415 #if HOST_BITS_PER_WIDE_INT <= 32
1416       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1417         return 0;
1418 #endif
1419     }
1420   else if (GET_CODE (op) == CONST_DOUBLE)
1421     {
1422       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1423         abort ();
1424
1425       opl = CONST_DOUBLE_LOW (op);
1426       oph = CONST_DOUBLE_HIGH (op);
1427       if (oph != 0)
1428         return 0;
1429     }
1430   else
1431     return 0;
1432
1433   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1434           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1435 }
1436
1437 /* Return 1 if C is a constant that is not a logical operand (as
1438    above), but could be split into one.  */
1439
1440 int
1441 non_logical_cint_operand (op, mode)
1442      rtx op;
1443      enum machine_mode mode;
1444 {
1445   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1446           && ! logical_operand (op, mode)
1447           && reg_or_logical_cint_operand (op, mode));
1448 }
1449
1450 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1451    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1452    Reject all ones and all zeros, since these should have been optimized
1453    away and confuse the making of MB and ME.  */
1454
1455 int
1456 mask_operand (op, mode)
1457      rtx op;
1458      enum machine_mode mode ATTRIBUTE_UNUSED;
1459 {
1460   HOST_WIDE_INT c, lsb;
1461
1462   if (GET_CODE (op) != CONST_INT)
1463     return 0;
1464
1465   c = INTVAL (op);
1466
1467   /* Fail in 64-bit mode if the mask wraps around because the upper
1468      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
1469   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
1470     return 0;
1471
1472   /* We don't change the number of transitions by inverting,
1473      so make sure we start with the LS bit zero.  */
1474   if (c & 1)
1475     c = ~c;
1476
1477   /* Reject all zeros or all ones.  */
1478   if (c == 0)
1479     return 0;
1480
1481   /* Find the first transition.  */
1482   lsb = c & -c;
1483
1484   /* Invert to look for a second transition.  */
1485   c = ~c;
1486
1487   /* Erase first transition.  */
1488   c &= -lsb;
1489
1490   /* Find the second transition (if any).  */
1491   lsb = c & -c;
1492
1493   /* Match if all the bits above are 1's (or c is zero).  */
1494   return c == -lsb;
1495 }
1496
1497 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1498    It is if there are no more than one 1->0 or 0->1 transitions.
1499    Reject all ones and all zeros, since these should have been optimized
1500    away and confuse the making of MB and ME.  */
1501
1502 int
1503 mask64_operand (op, mode)
1504      rtx op;
1505      enum machine_mode mode;
1506 {
1507   if (GET_CODE (op) == CONST_INT)
1508     {
1509       HOST_WIDE_INT c, lsb;
1510
1511       /* We don't change the number of transitions by inverting,
1512          so make sure we start with the LS bit zero.  */
1513       c = INTVAL (op);
1514       if (c & 1)
1515         c = ~c;
1516
1517       /* Reject all zeros or all ones.  */
1518       if (c == 0)
1519         return 0;
1520
1521       /* Find the transition, and check that all bits above are 1's.  */
1522       lsb = c & -c;
1523       return c == -lsb;
1524     }
1525   else if (GET_CODE (op) == CONST_DOUBLE
1526            && (mode == VOIDmode || mode == DImode))
1527     {
1528       HOST_WIDE_INT low, high, lsb;
1529
1530       if (HOST_BITS_PER_WIDE_INT < 64)
1531         high = CONST_DOUBLE_HIGH (op);
1532
1533       low = CONST_DOUBLE_LOW (op);
1534       if (low & 1)
1535         {
1536           if (HOST_BITS_PER_WIDE_INT < 64)
1537             high = ~high;
1538           low = ~low;
1539         }
1540
1541       if (low == 0)
1542         {
1543           if (HOST_BITS_PER_WIDE_INT >= 64 || high == 0)
1544             return 0;
1545
1546           lsb = high & -high;
1547           return high == -lsb;
1548         }
1549
1550       lsb = low & -low;
1551       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
1552     }
1553   else
1554     return 0;
1555 }
1556
1557 /* Return 1 if the operand is either a non-special register or a constant
1558    that can be used as the operand of a PowerPC64 logical AND insn.  */
1559
1560 int
1561 and64_operand (op, mode)
1562     rtx op;
1563     enum machine_mode mode;
1564 {
1565   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1566     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
1567
1568   return (logical_operand (op, mode) || mask64_operand (op, mode));
1569 }
1570
1571 /* Return 1 if the operand is either a non-special register or a
1572    constant that can be used as the operand of an RS/6000 logical AND insn.  */
1573
1574 int
1575 and_operand (op, mode)
1576     rtx op;
1577     enum machine_mode mode;
1578 {
1579   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1580     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
1581
1582   return (logical_operand (op, mode) || mask_operand (op, mode));
1583 }
1584
1585 /* Return 1 if the operand is a general register or memory operand.  */
1586
1587 int
1588 reg_or_mem_operand (op, mode)
1589      rtx op;
1590      enum machine_mode mode;
1591 {
1592   return (gpc_reg_operand (op, mode)
1593           || memory_operand (op, mode)
1594           || volatile_mem_operand (op, mode));
1595 }
1596
1597 /* Return 1 if the operand is a general register or memory operand without
1598    pre_inc or pre_dec which produces invalid form of PowerPC lwa
1599    instruction.  */
1600
1601 int
1602 lwa_operand (op, mode)
1603      rtx op;
1604      enum machine_mode mode;
1605 {
1606   rtx inner = op;
1607
1608   if (reload_completed && GET_CODE (inner) == SUBREG)
1609     inner = SUBREG_REG (inner);
1610     
1611   return gpc_reg_operand (inner, mode)
1612     || (memory_operand (inner, mode)
1613         && GET_CODE (XEXP (inner, 0)) != PRE_INC
1614         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
1615         && (GET_CODE (XEXP (inner, 0)) != PLUS
1616             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
1617             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
1618 }
1619
1620 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
1621
1622 int
1623 symbol_ref_operand (op, mode)
1624      rtx op;
1625      enum machine_mode mode;
1626 {
1627   if (mode != VOIDmode && GET_MODE (op) != mode)
1628     return 0;
1629
1630   return (GET_CODE (op) == SYMBOL_REF);
1631 }
1632
1633 /* Return 1 if the operand, used inside a MEM, is a valid first argument
1634    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
1635
1636 int
1637 call_operand (op, mode)
1638      rtx op;
1639      enum machine_mode mode;
1640 {
1641   if (mode != VOIDmode && GET_MODE (op) != mode)
1642     return 0;
1643
1644   return (GET_CODE (op) == SYMBOL_REF
1645           || (GET_CODE (op) == REG
1646               && (REGNO (op) == LINK_REGISTER_REGNUM
1647                   || REGNO (op) == COUNT_REGISTER_REGNUM
1648                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
1649 }
1650
1651 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
1652    this file and the function is not weakly defined.  */
1653
1654 int
1655 current_file_function_operand (op, mode)
1656      rtx op;
1657      enum machine_mode mode ATTRIBUTE_UNUSED;
1658 {
1659   return (GET_CODE (op) == SYMBOL_REF
1660           && (SYMBOL_REF_FLAG (op)
1661               || (op == XEXP (DECL_RTL (current_function_decl), 0)
1662                   && ! DECL_WEAK (current_function_decl))));
1663 }
1664
1665 /* Return 1 if this operand is a valid input for a move insn.  */
1666
1667 int
1668 input_operand (op, mode)
1669      rtx op;
1670      enum machine_mode mode;
1671 {
1672   /* Memory is always valid.  */
1673   if (memory_operand (op, mode))
1674     return 1;
1675
1676   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
1677   if (GET_CODE (op) == CONSTANT_P_RTX)
1678     return 1;
1679
1680   /* For floating-point, easy constants are valid.  */
1681   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1682       && CONSTANT_P (op)
1683       && easy_fp_constant (op, mode))
1684     return 1;
1685
1686   /* Allow any integer constant.  */
1687   if (GET_MODE_CLASS (mode) == MODE_INT
1688       && (GET_CODE (op) == CONST_INT
1689           || GET_CODE (op) == CONST_DOUBLE))
1690     return 1;
1691
1692   /* For floating-point or multi-word mode, the only remaining valid type
1693      is a register.  */
1694   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1695       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1696     return register_operand (op, mode);
1697
1698   /* The only cases left are integral modes one word or smaller (we
1699      do not get called for MODE_CC values).  These can be in any
1700      register.  */
1701   if (register_operand (op, mode))
1702     return 1;
1703
1704   /* A SYMBOL_REF referring to the TOC is valid.  */
1705   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (op))
1706     return 1;
1707
1708   /* A constant pool expression (relative to the TOC) is valid */
1709   if (TOC_RELATIVE_EXPR_P (op))
1710     return 1;
1711
1712   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
1713      to be valid.  */
1714   if (DEFAULT_ABI == ABI_V4
1715       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
1716       && small_data_operand (op, Pmode))
1717     return 1;
1718
1719   return 0;
1720 }
1721
1722 /* Return 1 for an operand in small memory on V.4/eabi.  */
1723
1724 int
1725 small_data_operand (op, mode)
1726      rtx op ATTRIBUTE_UNUSED;
1727      enum machine_mode mode ATTRIBUTE_UNUSED;
1728 {
1729 #if TARGET_ELF
1730   rtx sym_ref;
1731
1732   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
1733     return 0;
1734
1735   if (DEFAULT_ABI != ABI_V4)
1736     return 0;
1737
1738   if (GET_CODE (op) == SYMBOL_REF)
1739     sym_ref = op;
1740
1741   else if (GET_CODE (op) != CONST
1742            || GET_CODE (XEXP (op, 0)) != PLUS
1743            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
1744            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
1745     return 0;
1746
1747   else
1748     {
1749       rtx sum = XEXP (op, 0);
1750       HOST_WIDE_INT summand;
1751
1752       /* We have to be careful here, because it is the referenced address
1753         that must be 32k from _SDA_BASE_, not just the symbol.  */
1754       summand = INTVAL (XEXP (sum, 1));
1755       if (summand < 0 || summand > g_switch_value)
1756        return 0;
1757
1758       sym_ref = XEXP (sum, 0);
1759     }
1760
1761   if (*XSTR (sym_ref, 0) != '@')
1762     return 0;
1763
1764   return 1;
1765
1766 #else
1767   return 0;
1768 #endif
1769 }
1770 \f
1771 static int 
1772 constant_pool_expr_1 (op, have_sym, have_toc) 
1773     rtx op;
1774     int *have_sym;
1775     int *have_toc;
1776 {
1777   switch (GET_CODE(op)) 
1778     {
1779     case SYMBOL_REF:
1780       if (CONSTANT_POOL_ADDRESS_P (op))
1781         {
1782           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
1783             {
1784               *have_sym = 1;
1785               return 1;
1786             }
1787           else
1788             return 0;
1789         }
1790       else if (! strcmp (XSTR (op, 0), toc_label_name))
1791         {
1792           *have_toc = 1;
1793           return 1;
1794         }
1795       else
1796         return 0;
1797     case PLUS:
1798     case MINUS:
1799       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
1800               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
1801     case CONST:
1802       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
1803     case CONST_INT:
1804       return 1;
1805     default:
1806       return 0;
1807     }
1808 }
1809
1810 int
1811 constant_pool_expr_p (op)
1812     rtx op;
1813 {
1814   int have_sym = 0;
1815   int have_toc = 0;
1816   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
1817 }
1818
1819 int
1820 toc_relative_expr_p (op)
1821     rtx op;
1822 {
1823     int have_sym = 0;
1824     int have_toc = 0;
1825     return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
1826 }
1827
1828 /* Try machine-dependent ways of modifying an illegitimate address
1829    to be legitimate.  If we find one, return the new, valid address.
1830    This is used from only one place: `memory_address' in explow.c.
1831
1832    OLDX is the address as it was before break_out_memory_refs was
1833    called.  In some cases it is useful to look at this to decide what
1834    needs to be done.
1835
1836    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
1837
1838    It is always safe for this function to do nothing.  It exists to
1839    recognize opportunities to optimize the output.
1840
1841    On RS/6000, first check for the sum of a register with a constant
1842    integer that is out of range.  If so, generate code to add the
1843    constant with the low-order 16 bits masked to the register and force
1844    this result into another register (this can be done with `cau').
1845    Then generate an address of REG+(CONST&0xffff), allowing for the
1846    possibility of bit 16 being a one.
1847
1848    Then check for the sum of a register and something not constant, try to
1849    load the other things into a register and return the sum.  */
1850 rtx
1851 rs6000_legitimize_address (x, oldx, mode)
1852      rtx x;
1853      rtx oldx ATTRIBUTE_UNUSED;
1854      enum machine_mode mode;
1855 {
1856   if (GET_CODE (x) == PLUS 
1857       && GET_CODE (XEXP (x, 0)) == REG
1858       && GET_CODE (XEXP (x, 1)) == CONST_INT
1859       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
1860     { 
1861       HOST_WIDE_INT high_int, low_int;
1862       rtx sum;
1863       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
1864       high_int = INTVAL (XEXP (x, 1)) - low_int;
1865       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
1866                                          GEN_INT (high_int)), 0);
1867       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
1868     }
1869   else if (GET_CODE (x) == PLUS 
1870            && GET_CODE (XEXP (x, 0)) == REG
1871            && GET_CODE (XEXP (x, 1)) != CONST_INT
1872            && GET_MODE_NUNITS (mode) == 1
1873            && (TARGET_HARD_FLOAT || TARGET_POWERPC64 || mode != DFmode)
1874            && (TARGET_POWERPC64 || mode != DImode)
1875            && mode != TImode)
1876     {
1877       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
1878                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
1879     }
1880   else if (ALTIVEC_VECTOR_MODE (mode))
1881     {
1882       rtx reg;
1883
1884       /* Make sure both operands are registers.  */
1885       if (GET_CODE (x) == PLUS)
1886         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
1887                              force_reg (Pmode, XEXP (x, 1)));
1888
1889       reg = force_reg (Pmode, x);
1890       return reg;
1891     }
1892   else if (TARGET_ELF && TARGET_32BIT && TARGET_NO_TOC && ! flag_pic
1893            && GET_CODE (x) != CONST_INT
1894            && GET_CODE (x) != CONST_DOUBLE 
1895            && CONSTANT_P (x)
1896            && GET_MODE_NUNITS (mode) == 1
1897            && (GET_MODE_BITSIZE (mode) <= 32
1898                || (TARGET_HARD_FLOAT && mode == DFmode)))
1899     {
1900       rtx reg = gen_reg_rtx (Pmode);
1901       emit_insn (gen_elf_high (reg, (x)));
1902       return gen_rtx_LO_SUM (Pmode, reg, (x));
1903     }
1904   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
1905            && ! flag_pic
1906            && GET_CODE (x) != CONST_INT
1907            && GET_CODE (x) != CONST_DOUBLE 
1908            && CONSTANT_P (x)
1909            && (TARGET_HARD_FLOAT || mode != DFmode)
1910            && mode != DImode 
1911            && mode != TImode)
1912     {
1913       rtx reg = gen_reg_rtx (Pmode);
1914       emit_insn (gen_macho_high (reg, (x)));
1915       return gen_rtx_LO_SUM (Pmode, reg, (x));
1916     }
1917   else if (TARGET_TOC 
1918            && CONSTANT_POOL_EXPR_P (x)
1919            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
1920     {
1921       return create_TOC_reference (x);
1922     }
1923   else
1924     return NULL_RTX;
1925 }
1926
1927 /* The convention appears to be to define this wherever it is used.
1928    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
1929    is now used here.  */
1930 #ifndef REG_MODE_OK_FOR_BASE_P
1931 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
1932 #endif
1933
1934 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
1935    replace the input X, or the original X if no replacement is called for.
1936    The output parameter *WIN is 1 if the calling macro should goto WIN,
1937    0 if it should not.
1938
1939    For RS/6000, we wish to handle large displacements off a base
1940    register by splitting the addend across an addiu/addis and the mem insn.
1941    This cuts number of extra insns needed from 3 to 1.
1942
1943    On Darwin, we use this to generate code for floating point constants.
1944    A movsf_low is generated so we wind up with 2 instructions rather than 3.
1945    The Darwin code is inside #if TARGET_MACHO because only then is
1946    machopic_function_base_name() defined.  */
1947 rtx
1948 rs6000_legitimize_reload_address (x, mode, opnum, type, ind_levels, win)
1949     rtx x;
1950     enum machine_mode mode;
1951     int opnum;
1952     int type;
1953     int ind_levels ATTRIBUTE_UNUSED;
1954     int *win;
1955 {
1956   /* We must recognize output that we have already generated ourselves.  */ 
1957   if (GET_CODE (x) == PLUS
1958       && GET_CODE (XEXP (x, 0)) == PLUS
1959       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
1960       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1961       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1962     {
1963       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1964                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
1965                    opnum, (enum reload_type)type);
1966       *win = 1;
1967       return x;
1968     }
1969
1970 #if TARGET_MACHO
1971   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
1972       && GET_CODE (x) == LO_SUM
1973       && GET_CODE (XEXP (x, 0)) == PLUS
1974       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
1975       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
1976       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
1977       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
1978       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
1979       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
1980       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
1981     {
1982       /* Result of previous invocation of this function on Darwin
1983          floating point constant.  */
1984       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1985                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
1986                 opnum, (enum reload_type)type);
1987       *win = 1;
1988       return x;
1989     }
1990 #endif
1991   if (GET_CODE (x) == PLUS
1992       && GET_CODE (XEXP (x, 0)) == REG
1993       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
1994       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
1995       && GET_CODE (XEXP (x, 1)) == CONST_INT
1996       && !ALTIVEC_VECTOR_MODE (mode))
1997     {
1998       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
1999       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
2000       HOST_WIDE_INT high
2001         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
2002
2003       /* Check for 32-bit overflow.  */
2004       if (high + low != val)
2005         {
2006           *win = 0;
2007           return x;
2008         }
2009
2010       /* Reload the high part into a base reg; leave the low part
2011          in the mem directly.  */
2012
2013       x = gen_rtx_PLUS (GET_MODE (x),
2014                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
2015                                       GEN_INT (high)),
2016                         GEN_INT (low));
2017
2018       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2019                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2020                    opnum, (enum reload_type)type);
2021       *win = 1;
2022       return x;
2023     }
2024 #if TARGET_MACHO
2025   if (GET_CODE (x) == SYMBOL_REF
2026       && DEFAULT_ABI == ABI_DARWIN
2027       && !ALTIVEC_VECTOR_MODE (mode)
2028       && flag_pic)
2029     {
2030       /* Darwin load of floating point constant.  */
2031       rtx offset = gen_rtx (CONST, Pmode,
2032                     gen_rtx (MINUS, Pmode, x,
2033                     gen_rtx (SYMBOL_REF, Pmode,
2034                         machopic_function_base_name ())));
2035       x = gen_rtx (LO_SUM, GET_MODE (x),
2036             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
2037                 gen_rtx (HIGH, Pmode, offset)), offset);
2038       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2039                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2040                 opnum, (enum reload_type)type);
2041       *win = 1;
2042       return x;
2043     }
2044 #endif
2045   if (TARGET_TOC
2046       && CONSTANT_POOL_EXPR_P (x)
2047       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
2048     {
2049       (x) = create_TOC_reference (x);
2050       *win = 1;
2051       return x;
2052     }
2053   *win = 0;
2054   return x;
2055 }    
2056
2057 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
2058    that is a valid memory address for an instruction.
2059    The MODE argument is the machine mode for the MEM expression
2060    that wants to use this address.
2061
2062    On the RS/6000, there are four valid address: a SYMBOL_REF that
2063    refers to a constant pool entry of an address (or the sum of it
2064    plus a constant), a short (16-bit signed) constant plus a register,
2065    the sum of two registers, or a register indirect, possibly with an
2066    auto-increment.  For DFmode and DImode with an constant plus register,
2067    we must ensure that both words are addressable or PowerPC64 with offset
2068    word aligned.
2069
2070    For modes spanning multiple registers (DFmode in 32-bit GPRs,
2071    32-bit DImode, TImode), indexed addressing cannot be used because
2072    adjacent memory cells are accessed by adding word-sized offsets
2073    during assembly output.  */
2074 int
2075 rs6000_legitimate_address (mode, x, reg_ok_strict)
2076     enum machine_mode mode;
2077     rtx x;
2078     int reg_ok_strict;
2079 {
2080   if (LEGITIMATE_INDIRECT_ADDRESS_P (x, reg_ok_strict))
2081     return 1;
2082   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
2083       && !ALTIVEC_VECTOR_MODE (mode)
2084       && TARGET_UPDATE
2085       && LEGITIMATE_INDIRECT_ADDRESS_P (XEXP (x, 0), reg_ok_strict))
2086     return 1;
2087   if (LEGITIMATE_SMALL_DATA_P (mode, x))
2088     return 1;
2089   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
2090     return 1;
2091   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
2092   if (! reg_ok_strict
2093       && GET_CODE (x) == PLUS
2094       && GET_CODE (XEXP (x, 0)) == REG
2095       && XEXP (x, 0) == virtual_stack_vars_rtx
2096       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2097     return 1;
2098   if (LEGITIMATE_OFFSET_ADDRESS_P (mode, x, reg_ok_strict))
2099     return 1;
2100   if (mode != TImode
2101       && (TARGET_HARD_FLOAT || TARGET_POWERPC64 || mode != DFmode)
2102       && (TARGET_POWERPC64 || mode != DImode)
2103       && LEGITIMATE_INDEXED_ADDRESS_P (x, reg_ok_strict))
2104     return 1;
2105   if (LEGITIMATE_LO_SUM_ADDRESS_P (mode, x, reg_ok_strict))
2106     return 1;
2107   return 0;
2108 }
2109 \f
2110 /* Try to output insns to set TARGET equal to the constant C if it can
2111    be done in less than N insns.  Do all computations in MODE.
2112    Returns the place where the output has been placed if it can be
2113    done and the insns have been emitted.  If it would take more than N
2114    insns, zero is returned and no insns and emitted.  */
2115
2116 rtx
2117 rs6000_emit_set_const (dest, mode, source, n)
2118      rtx dest, source;
2119      enum machine_mode mode;
2120      int n ATTRIBUTE_UNUSED;
2121 {
2122   HOST_WIDE_INT c0, c1;
2123
2124   if (mode == QImode || mode == HImode || mode == SImode)
2125     {
2126       if (dest == NULL)
2127         dest = gen_reg_rtx (mode);
2128       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
2129       return dest;
2130     }
2131
2132   if (GET_CODE (source) == CONST_INT)
2133     {
2134       c0 = INTVAL (source);
2135       c1 = -(c0 < 0);
2136     }
2137   else if (GET_CODE (source) == CONST_DOUBLE)
2138     {
2139 #if HOST_BITS_PER_WIDE_INT >= 64
2140       c0 = CONST_DOUBLE_LOW (source);
2141       c1 = -(c0 < 0);
2142 #else
2143       c0 = CONST_DOUBLE_LOW (source);
2144       c1 = CONST_DOUBLE_HIGH (source);
2145 #endif
2146     }
2147   else
2148     abort ();
2149
2150   return rs6000_emit_set_long_const (dest, c0, c1);
2151 }
2152
2153 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
2154    fall back to a straight forward decomposition.  We do this to avoid
2155    exponential run times encountered when looking for longer sequences
2156    with rs6000_emit_set_const.  */
2157 static rtx
2158 rs6000_emit_set_long_const (dest, c1, c2)
2159      rtx dest;
2160      HOST_WIDE_INT c1, c2;
2161 {
2162   if (!TARGET_POWERPC64)
2163     {
2164       rtx operand1, operand2;
2165
2166       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
2167                                         DImode);
2168       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
2169                                         DImode);
2170       emit_move_insn (operand1, GEN_INT (c1));
2171       emit_move_insn (operand2, GEN_INT (c2));
2172     }
2173   else
2174     {
2175       HOST_WIDE_INT ud1, ud2, ud3, ud4;
2176
2177       ud1 = c1 & 0xffff;
2178       ud2 = (c1 & 0xffff0000) >> 16;
2179 #if HOST_BITS_PER_WIDE_INT >= 64
2180       c2 = c1 >> 32;
2181 #endif
2182       ud3 = c2 & 0xffff;
2183       ud4 = (c2 & 0xffff0000) >> 16;
2184
2185       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
2186           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
2187         {
2188           if (ud1 & 0x8000)
2189             emit_move_insn (dest, GEN_INT (((ud1  ^ 0x8000) -  0x8000)));
2190           else
2191             emit_move_insn (dest, GEN_INT (ud1));
2192         }
2193
2194       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
2195                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
2196         {
2197           if (ud2 & 0x8000)
2198             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
2199                                            - 0x80000000));
2200           else
2201             emit_move_insn (dest, GEN_INT (ud2 << 16));
2202           if (ud1 != 0)
2203             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2204         }
2205       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
2206                || (ud4 == 0 && ! (ud3 & 0x8000)))
2207         {
2208           if (ud3 & 0x8000)
2209             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
2210                                            - 0x80000000));
2211           else
2212             emit_move_insn (dest, GEN_INT (ud3 << 16));
2213
2214           if (ud2 != 0)
2215             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
2216           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
2217           if (ud1 != 0)
2218             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2219         }
2220       else 
2221         {
2222           if (ud4 & 0x8000)
2223             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
2224                                            - 0x80000000));
2225           else
2226             emit_move_insn (dest, GEN_INT (ud4 << 16));
2227
2228           if (ud3 != 0)
2229             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
2230
2231           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
2232           if (ud2 != 0)
2233             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
2234                                                GEN_INT (ud2 << 16)));   
2235           if (ud1 != 0)
2236             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2237         }
2238     }
2239   return dest;
2240 }
2241
2242 /* Emit a move from SOURCE to DEST in mode MODE.  */
2243 void
2244 rs6000_emit_move (dest, source, mode)
2245      rtx dest;
2246      rtx source;
2247      enum machine_mode mode;
2248 {
2249   rtx operands[2];
2250   operands[0] = dest;
2251   operands[1] = source;
2252   
2253   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
2254   if (GET_CODE (operands[1]) == CONST_DOUBLE
2255       && ! FLOAT_MODE_P (mode)
2256       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
2257     {
2258       /* FIXME.  This should never happen.  */
2259       /* Since it seems that it does, do the safe thing and convert
2260          to a CONST_INT.  */
2261       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
2262     }
2263   if (GET_CODE (operands[1]) == CONST_DOUBLE
2264       && ! FLOAT_MODE_P (mode)
2265       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
2266            && CONST_DOUBLE_LOW (operands[1]) >= 0)
2267           || (CONST_DOUBLE_HIGH (operands[1]) == -1
2268               && CONST_DOUBLE_LOW (operands[1]) < 0)))
2269     abort ();
2270
2271   /* Check if GCC is setting up a block move that will end up using FP
2272      registers as temporaries.  We must make sure this is acceptable.  */
2273   if (GET_CODE (operands[0]) == MEM
2274       && GET_CODE (operands[1]) == MEM
2275       && mode == DImode
2276       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
2277           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
2278       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
2279                                             ? 32 : MEM_ALIGN (operands[0])))
2280             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
2281                                                ? 32 
2282                                                : MEM_ALIGN (operands[1]))))
2283       && ! MEM_VOLATILE_P (operands [0])
2284       && ! MEM_VOLATILE_P (operands [1]))
2285     {
2286       emit_move_insn (adjust_address (operands[0], SImode, 0),
2287                       adjust_address (operands[1], SImode, 0));
2288       emit_move_insn (adjust_address (operands[0], SImode, 4),
2289                       adjust_address (operands[1], SImode, 4));
2290       return;
2291     }
2292   
2293   if (! no_new_pseudos && GET_CODE (operands[0]) != REG)
2294     operands[1] = force_reg (mode, operands[1]);
2295
2296   if (mode == SFmode && ! TARGET_POWERPC && TARGET_HARD_FLOAT
2297       && GET_CODE (operands[0]) == MEM)
2298     {
2299       int regnum;
2300
2301       if (reload_in_progress || reload_completed)
2302         regnum = true_regnum (operands[1]);
2303       else if (GET_CODE (operands[1]) == REG)
2304         regnum = REGNO (operands[1]);
2305       else
2306         regnum = -1;
2307       
2308       /* If operands[1] is a register, on POWER it may have
2309          double-precision data in it, so truncate it to single
2310          precision.  */
2311       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
2312         {
2313           rtx newreg;
2314           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
2315           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
2316           operands[1] = newreg;
2317         }
2318     }
2319
2320   /* Handle the case where reload calls us with an invalid address;
2321      and the case of CONSTANT_P_RTX.  */
2322   if (!ALTIVEC_VECTOR_MODE (mode)
2323       && (! general_operand (operands[1], mode)
2324           || ! nonimmediate_operand (operands[0], mode)
2325           || GET_CODE (operands[1]) == CONSTANT_P_RTX))
2326     {
2327       emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2328       return;
2329     }
2330   
2331   /* FIXME:  In the long term, this switch statement should go away
2332      and be replaced by a sequence of tests based on things like
2333      mode == Pmode.  */
2334   switch (mode)
2335     {
2336     case HImode:
2337     case QImode:
2338       if (CONSTANT_P (operands[1])
2339           && GET_CODE (operands[1]) != CONST_INT)
2340         operands[1] = force_const_mem (mode, operands[1]);
2341       break;
2342
2343     case TFmode:
2344     case DFmode:
2345     case SFmode:
2346       if (CONSTANT_P (operands[1]) 
2347           && ! easy_fp_constant (operands[1], mode))
2348         operands[1] = force_const_mem (mode, operands[1]);
2349       break;
2350       
2351     case V16QImode:
2352     case V8HImode:
2353     case V4SFmode:
2354     case V4SImode:
2355       if (CONSTANT_P (operands[1])
2356           && !easy_vector_constant (operands[1]))
2357         operands[1] = force_const_mem (mode, operands[1]);
2358       break;
2359       
2360     case SImode:
2361     case DImode:
2362       /* Use default pattern for address of ELF small data */
2363       if (TARGET_ELF
2364           && mode == Pmode
2365           && DEFAULT_ABI == ABI_V4
2366           && (GET_CODE (operands[1]) == SYMBOL_REF 
2367               || GET_CODE (operands[1]) == CONST)
2368           && small_data_operand (operands[1], mode))
2369         {
2370           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2371           return;
2372         }
2373
2374       if (DEFAULT_ABI == ABI_V4
2375           && mode == Pmode && mode == SImode
2376           && flag_pic == 1 && got_operand (operands[1], mode))
2377         {
2378           emit_insn (gen_movsi_got (operands[0], operands[1]));
2379           return;
2380         }
2381
2382       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
2383           && TARGET_NO_TOC && ! flag_pic
2384           && mode == Pmode
2385           && CONSTANT_P (operands[1])
2386           && GET_CODE (operands[1]) != HIGH
2387           && GET_CODE (operands[1]) != CONST_INT)
2388         {
2389           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
2390
2391           /* If this is a function address on -mcall-aixdesc,
2392              convert it to the address of the descriptor.  */
2393           if (DEFAULT_ABI == ABI_AIX
2394               && GET_CODE (operands[1]) == SYMBOL_REF
2395               && XSTR (operands[1], 0)[0] == '.')
2396             {
2397               const char *name = XSTR (operands[1], 0);
2398               rtx new_ref;
2399               while (*name == '.')
2400                 name++;
2401               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
2402               CONSTANT_POOL_ADDRESS_P (new_ref)
2403                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
2404               SYMBOL_REF_FLAG (new_ref) = SYMBOL_REF_FLAG (operands[1]);
2405               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
2406               operands[1] = new_ref;
2407             }
2408
2409           if (DEFAULT_ABI == ABI_DARWIN)
2410             {
2411               emit_insn (gen_macho_high (target, operands[1]));
2412               emit_insn (gen_macho_low (operands[0], target, operands[1]));
2413               return;
2414             }
2415
2416           emit_insn (gen_elf_high (target, operands[1]));
2417           emit_insn (gen_elf_low (operands[0], target, operands[1]));
2418           return;
2419         }
2420
2421       /* If this is a SYMBOL_REF that refers to a constant pool entry,
2422          and we have put it in the TOC, we just need to make a TOC-relative
2423          reference to it.  */
2424       if (TARGET_TOC
2425           && GET_CODE (operands[1]) == SYMBOL_REF
2426           && CONSTANT_POOL_EXPR_P (operands[1])
2427           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
2428                                               get_pool_mode (operands[1])))
2429         {
2430           operands[1] = create_TOC_reference (operands[1]);
2431         }
2432       else if (mode == Pmode
2433                && CONSTANT_P (operands[1])
2434                && ((GET_CODE (operands[1]) != CONST_INT
2435                     && ! easy_fp_constant (operands[1], mode))
2436                    || (GET_CODE (operands[1]) == CONST_INT
2437                        && num_insns_constant (operands[1], mode) > 2)
2438                    || (GET_CODE (operands[0]) == REG
2439                        && FP_REGNO_P (REGNO (operands[0]))))
2440                && GET_CODE (operands[1]) != HIGH
2441                && ! LEGITIMATE_CONSTANT_POOL_ADDRESS_P (operands[1])
2442                && ! TOC_RELATIVE_EXPR_P (operands[1]))
2443         {
2444           /* Emit a USE operation so that the constant isn't deleted if
2445              expensive optimizations are turned on because nobody
2446              references it.  This should only be done for operands that
2447              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
2448              This should not be done for operands that contain LABEL_REFs.
2449              For now, we just handle the obvious case.  */
2450           if (GET_CODE (operands[1]) != LABEL_REF)
2451             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
2452
2453 #if TARGET_MACHO
2454           /* Darwin uses a special PIC legitimizer.  */
2455           if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
2456             {
2457               operands[1] =
2458                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
2459                                                         operands[0]);
2460               if (operands[0] != operands[1])
2461                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2462               return;
2463             }
2464 #endif
2465
2466           /* If we are to limit the number of things we put in the TOC and
2467              this is a symbol plus a constant we can add in one insn,
2468              just put the symbol in the TOC and add the constant.  Don't do
2469              this if reload is in progress.  */
2470           if (GET_CODE (operands[1]) == CONST
2471               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
2472               && GET_CODE (XEXP (operands[1], 0)) == PLUS
2473               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
2474               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
2475                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
2476               && ! side_effects_p (operands[0]))
2477             {
2478               rtx sym =
2479                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
2480               rtx other = XEXP (XEXP (operands[1], 0), 1);
2481
2482               sym = force_reg (mode, sym);
2483               if (mode == SImode)
2484                 emit_insn (gen_addsi3 (operands[0], sym, other));
2485               else
2486                 emit_insn (gen_adddi3 (operands[0], sym, other));
2487               return;
2488             }
2489
2490           operands[1] = force_const_mem (mode, operands[1]);
2491
2492           if (TARGET_TOC 
2493               && CONSTANT_POOL_EXPR_P (XEXP (operands[1], 0))
2494               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
2495                         get_pool_constant (XEXP (operands[1], 0)),
2496                         get_pool_mode (XEXP (operands[1], 0))))
2497             {
2498               operands[1]
2499                 = gen_rtx_MEM (mode,
2500                                create_TOC_reference (XEXP (operands[1], 0)));
2501               set_mem_alias_set (operands[1], get_TOC_alias_set ());
2502               RTX_UNCHANGING_P (operands[1]) = 1;
2503             }
2504         }
2505       break;
2506
2507     case TImode:
2508       if (GET_CODE (operands[0]) == MEM
2509           && GET_CODE (XEXP (operands[0], 0)) != REG
2510           && ! reload_in_progress)
2511         operands[0]
2512           = replace_equiv_address (operands[0],
2513                                    copy_addr_to_reg (XEXP (operands[0], 0)));
2514
2515       if (GET_CODE (operands[1]) == MEM
2516           && GET_CODE (XEXP (operands[1], 0)) != REG
2517           && ! reload_in_progress)
2518         operands[1]
2519           = replace_equiv_address (operands[1],
2520                                    copy_addr_to_reg (XEXP (operands[1], 0)));
2521       break;
2522
2523     default:
2524       abort ();
2525     }
2526
2527   /* Above, we may have called force_const_mem which may have returned
2528      an invalid address.  If we can, fix this up; otherwise, reload will
2529      have to deal with it.  */
2530   if (GET_CODE (operands[1]) == MEM
2531       && ! memory_address_p (mode, XEXP (operands[1], 0))
2532       && ! reload_in_progress)
2533     operands[1] = adjust_address (operands[1], mode, 0);
2534
2535   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2536   return;
2537 }
2538 \f
2539 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2540    for a call to a function whose data type is FNTYPE.
2541    For a library call, FNTYPE is 0.
2542
2543    For incoming args we set the number of arguments in the prototype large
2544    so we never return a PARALLEL.  */
2545
2546 void
2547 init_cumulative_args (cum, fntype, libname, incoming)
2548      CUMULATIVE_ARGS *cum;
2549      tree fntype;
2550      rtx libname ATTRIBUTE_UNUSED;
2551      int incoming;
2552 {
2553   static CUMULATIVE_ARGS zero_cumulative;
2554
2555   *cum = zero_cumulative;
2556   cum->words = 0;
2557   cum->fregno = FP_ARG_MIN_REG;
2558   cum->vregno = ALTIVEC_ARG_MIN_REG;
2559   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
2560   cum->call_cookie = CALL_NORMAL;
2561   cum->sysv_gregno = GP_ARG_MIN_REG;
2562
2563   if (incoming)
2564     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
2565
2566   else if (cum->prototype)
2567     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
2568                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
2569                                || RETURN_IN_MEMORY (TREE_TYPE (fntype))));
2570
2571   else
2572     cum->nargs_prototype = 0;
2573
2574   cum->orig_nargs = cum->nargs_prototype;
2575
2576   /* Check for a longcall attribute.  */
2577   if (fntype
2578       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
2579       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
2580     cum->call_cookie = CALL_LONG;
2581
2582   if (TARGET_DEBUG_ARG)
2583     {
2584       fprintf (stderr, "\ninit_cumulative_args:");
2585       if (fntype)
2586         {
2587           tree ret_type = TREE_TYPE (fntype);
2588           fprintf (stderr, " ret code = %s,",
2589                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
2590         }
2591
2592       if (cum->call_cookie & CALL_LONG)
2593         fprintf (stderr, " longcall,");
2594
2595       fprintf (stderr, " proto = %d, nargs = %d\n",
2596                cum->prototype, cum->nargs_prototype);
2597     }
2598 }
2599 \f
2600 /* If defined, a C expression which determines whether, and in which
2601    direction, to pad out an argument with extra space.  The value
2602    should be of type `enum direction': either `upward' to pad above
2603    the argument, `downward' to pad below, or `none' to inhibit
2604    padding.
2605
2606    For the AIX ABI structs are always stored left shifted in their
2607    argument slot.  */
2608
2609 enum direction
2610 function_arg_padding (mode, type)
2611      enum machine_mode mode;
2612      tree type;
2613 {
2614   if (type != 0 && AGGREGATE_TYPE_P (type))
2615     return upward;
2616
2617   /* This is the default definition.  */
2618   return (! BYTES_BIG_ENDIAN
2619           ? upward
2620           : ((mode == BLKmode
2621               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2622                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
2623               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
2624              ? downward : upward));
2625 }
2626
2627 /* If defined, a C expression that gives the alignment boundary, in bits,
2628    of an argument with the specified mode and type.  If it is not defined, 
2629    PARM_BOUNDARY is used for all arguments.
2630    
2631    V.4 wants long longs to be double word aligned.  */
2632
2633 int
2634 function_arg_boundary (mode, type)
2635      enum machine_mode mode;
2636      tree type ATTRIBUTE_UNUSED;
2637 {
2638   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
2639     return 64;
2640   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2641     return 128;
2642   else
2643     return PARM_BOUNDARY;
2644 }
2645 \f
2646 /* Update the data in CUM to advance over an argument
2647    of mode MODE and data type TYPE.
2648    (TYPE is null for libcalls where that information may not be available.)  */
2649
2650 void
2651 function_arg_advance (cum, mode, type, named)
2652      CUMULATIVE_ARGS *cum;
2653      enum machine_mode mode;
2654      tree type;
2655      int named;
2656 {
2657   cum->nargs_prototype--;
2658
2659   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2660     {
2661       if (cum->vregno <= ALTIVEC_ARG_MAX_REG && cum->nargs_prototype >= 0)
2662         cum->vregno++;
2663       else
2664         cum->words += RS6000_ARG_SIZE (mode, type);
2665     }
2666   else if (DEFAULT_ABI == ABI_V4)
2667     {
2668       if (TARGET_HARD_FLOAT
2669           && (mode == SFmode || mode == DFmode))
2670         {
2671           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2672             cum->fregno++;
2673           else
2674             {
2675               if (mode == DFmode)
2676                 cum->words += cum->words & 1;
2677               cum->words += RS6000_ARG_SIZE (mode, type);
2678             }
2679         }
2680       else
2681         {
2682           int n_words;
2683           int gregno = cum->sysv_gregno;
2684
2685           /* Aggregates and IEEE quad get passed by reference.  */
2686           if ((type && AGGREGATE_TYPE_P (type))
2687               || mode == TFmode)
2688             n_words = 1;
2689           else 
2690             n_words = RS6000_ARG_SIZE (mode, type);
2691
2692           /* Long long is put in odd registers.  */
2693           if (n_words == 2 && (gregno & 1) == 0)
2694             gregno += 1;
2695
2696           /* Long long is not split between registers and stack.  */
2697           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
2698             {
2699               /* Long long is aligned on the stack.  */
2700               if (n_words == 2)
2701                 cum->words += cum->words & 1;
2702               cum->words += n_words;
2703             }
2704
2705           /* Note: continuing to accumulate gregno past when we've started
2706              spilling to the stack indicates the fact that we've started
2707              spilling to the stack to expand_builtin_saveregs.  */
2708           cum->sysv_gregno = gregno + n_words;
2709         }
2710
2711       if (TARGET_DEBUG_ARG)
2712         {
2713           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2714                    cum->words, cum->fregno);
2715           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
2716                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
2717           fprintf (stderr, "mode = %4s, named = %d\n",
2718                    GET_MODE_NAME (mode), named);
2719         }
2720     }
2721   else
2722     {
2723       int align = (TARGET_32BIT && (cum->words & 1) != 0
2724                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2725
2726       cum->words += align + RS6000_ARG_SIZE (mode, type);
2727
2728       if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_HARD_FLOAT)
2729         cum->fregno++;
2730
2731       if (TARGET_DEBUG_ARG)
2732         {
2733           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2734                    cum->words, cum->fregno);
2735           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
2736                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
2737           fprintf (stderr, "named = %d, align = %d\n", named, align);
2738         }
2739     }
2740 }
2741 \f
2742 /* Determine where to put an argument to a function.
2743    Value is zero to push the argument on the stack,
2744    or a hard register in which to store the argument.
2745
2746    MODE is the argument's machine mode.
2747    TYPE is the data type of the argument (as a tree).
2748     This is null for libcalls where that information may
2749     not be available.
2750    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2751     the preceding args and about the function being called.
2752    NAMED is nonzero if this argument is a named parameter
2753     (otherwise it is an extra parameter matching an ellipsis).
2754
2755    On RS/6000 the first eight words of non-FP are normally in registers
2756    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
2757    Under V.4, the first 8 FP args are in registers.
2758
2759    If this is floating-point and no prototype is specified, we use
2760    both an FP and integer register (or possibly FP reg and stack).  Library
2761    functions (when TYPE is zero) always have the proper types for args,
2762    so we can pass the FP value just in one register.  emit_library_function
2763    doesn't support PARALLEL anyway.  */
2764
2765 struct rtx_def *
2766 function_arg (cum, mode, type, named)
2767      CUMULATIVE_ARGS *cum;
2768      enum machine_mode mode;
2769      tree type;
2770      int named;
2771 {
2772   enum rs6000_abi abi = DEFAULT_ABI;
2773
2774   /* Return a marker to indicate whether CR1 needs to set or clear the
2775      bit that V.4 uses to say fp args were passed in registers.
2776      Assume that we don't need the marker for software floating point,
2777      or compiler generated library calls.  */
2778   if (mode == VOIDmode)
2779     {
2780       if (abi == ABI_V4
2781           && TARGET_HARD_FLOAT
2782           && cum->nargs_prototype < 0
2783           && type && (cum->prototype || TARGET_NO_PROTOTYPE))
2784         {
2785           return GEN_INT (cum->call_cookie
2786                           | ((cum->fregno == FP_ARG_MIN_REG)
2787                              ? CALL_V4_SET_FP_ARGS
2788                              : CALL_V4_CLEAR_FP_ARGS));
2789         }
2790
2791       return GEN_INT (cum->call_cookie);
2792     }
2793
2794   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2795     {
2796       if (named && cum->vregno <= ALTIVEC_ARG_MAX_REG)
2797         return gen_rtx_REG (mode, cum->vregno);
2798       else
2799         return NULL;
2800     }
2801   else if (abi == ABI_V4)
2802     {
2803       if (TARGET_HARD_FLOAT
2804           && (mode == SFmode || mode == DFmode))
2805         {
2806           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2807             return gen_rtx_REG (mode, cum->fregno);
2808           else
2809             return NULL;
2810         }
2811       else
2812         {
2813           int n_words;
2814           int gregno = cum->sysv_gregno;
2815
2816           /* Aggregates and IEEE quad get passed by reference.  */
2817           if ((type && AGGREGATE_TYPE_P (type))
2818               || mode == TFmode)
2819             n_words = 1;
2820           else 
2821             n_words = RS6000_ARG_SIZE (mode, type);
2822
2823           /* Long long is put in odd registers.  */
2824           if (n_words == 2 && (gregno & 1) == 0)
2825             gregno += 1;
2826
2827           /* Long long is not split between registers and stack.  */
2828           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
2829             return gen_rtx_REG (mode, gregno);
2830           else
2831             return NULL;
2832         }
2833     }
2834   else
2835     {
2836       int align = (TARGET_32BIT && (cum->words & 1) != 0
2837                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2838       int align_words = cum->words + align;
2839
2840       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2841         return NULL_RTX;
2842
2843       if (USE_FP_FOR_ARG_P (*cum, mode, type))
2844         {
2845           if (! type
2846               || ((cum->nargs_prototype > 0)
2847                   /* IBM AIX extended its linkage convention definition always
2848                      to require FP args after register save area hole on the
2849                      stack.  */
2850                   && (DEFAULT_ABI != ABI_AIX
2851                       || ! TARGET_XL_CALL
2852                       || (align_words < GP_ARG_NUM_REG))))
2853             return gen_rtx_REG (mode, cum->fregno);
2854
2855           return gen_rtx_PARALLEL (mode,
2856             gen_rtvec (2,
2857                        gen_rtx_EXPR_LIST (VOIDmode,
2858                                 ((align_words >= GP_ARG_NUM_REG)
2859                                  ? NULL_RTX
2860                                  : (align_words
2861                                     + RS6000_ARG_SIZE (mode, type)
2862                                     > GP_ARG_NUM_REG
2863                                     /* If this is partially on the stack, then
2864                                        we only include the portion actually
2865                                        in registers here.  */
2866                                     ? gen_rtx_REG (SImode,
2867                                                GP_ARG_MIN_REG + align_words)
2868                                     : gen_rtx_REG (mode,
2869                                                GP_ARG_MIN_REG + align_words))),
2870                                 const0_rtx),
2871                        gen_rtx_EXPR_LIST (VOIDmode,
2872                                 gen_rtx_REG (mode, cum->fregno),
2873                                 const0_rtx)));
2874         }
2875       else if (align_words < GP_ARG_NUM_REG)
2876         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
2877       else
2878         return NULL_RTX;
2879     }
2880 }
2881 \f
2882 /* For an arg passed partly in registers and partly in memory,
2883    this is the number of registers used.
2884    For args passed entirely in registers or entirely in memory, zero.  */
2885
2886 int
2887 function_arg_partial_nregs (cum, mode, type, named)
2888      CUMULATIVE_ARGS *cum;
2889      enum machine_mode mode;
2890      tree type;
2891      int named ATTRIBUTE_UNUSED;
2892 {
2893   if (DEFAULT_ABI == ABI_V4)
2894     return 0;
2895
2896   if (USE_FP_FOR_ARG_P (*cum, mode, type)
2897       || USE_ALTIVEC_FOR_ARG_P (*cum, mode, type))
2898     {
2899       if (cum->nargs_prototype >= 0)
2900         return 0;
2901     }
2902
2903   if (cum->words < GP_ARG_NUM_REG
2904       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
2905     {
2906       int ret = GP_ARG_NUM_REG - cum->words;
2907       if (ret && TARGET_DEBUG_ARG)
2908         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
2909
2910       return ret;
2911     }
2912
2913   return 0;
2914 }
2915 \f
2916 /* A C expression that indicates when an argument must be passed by
2917    reference.  If nonzero for an argument, a copy of that argument is
2918    made in memory and a pointer to the argument is passed instead of
2919    the argument itself.  The pointer is passed in whatever way is
2920    appropriate for passing a pointer to that type.
2921
2922    Under V.4, structures and unions are passed by reference.  */
2923
2924 int
2925 function_arg_pass_by_reference (cum, mode, type, named)
2926      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
2927      enum machine_mode mode ATTRIBUTE_UNUSED;
2928      tree type;
2929      int named ATTRIBUTE_UNUSED;
2930 {
2931   if (DEFAULT_ABI == ABI_V4
2932       && ((type && AGGREGATE_TYPE_P (type))
2933           || mode == TFmode))
2934     {
2935       if (TARGET_DEBUG_ARG)
2936         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
2937
2938       return 1;
2939     }
2940
2941   return 0;
2942 }
2943 \f
2944 /* Perform any needed actions needed for a function that is receiving a
2945    variable number of arguments. 
2946
2947    CUM is as above.
2948
2949    MODE and TYPE are the mode and type of the current parameter.
2950
2951    PRETEND_SIZE is a variable that should be set to the amount of stack
2952    that must be pushed by the prolog to pretend that our caller pushed
2953    it.
2954
2955    Normally, this macro will push all remaining incoming registers on the
2956    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2957
2958 void
2959 setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2960      CUMULATIVE_ARGS *cum;
2961      enum machine_mode mode;
2962      tree type;
2963      int *pretend_size;
2964      int no_rtl;
2965
2966 {
2967   CUMULATIVE_ARGS next_cum;
2968   int reg_size = TARGET_32BIT ? 4 : 8;
2969   rtx save_area = NULL_RTX, mem;
2970   int first_reg_offset, set;
2971   tree fntype;
2972   int stdarg_p;
2973
2974   fntype = TREE_TYPE (current_function_decl);
2975   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
2976               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2977                   != void_type_node));
2978
2979   /* For varargs, we do not want to skip the dummy va_dcl argument.
2980      For stdargs, we do want to skip the last named argument.  */
2981   next_cum = *cum;
2982   if (stdarg_p)
2983     function_arg_advance (&next_cum, mode, type, 1);
2984
2985   if (DEFAULT_ABI == ABI_V4)
2986     {
2987       /* Indicate to allocate space on the stack for varargs save area.  */
2988       cfun->machine->sysv_varargs_p = 1;
2989       if (! no_rtl)
2990         save_area = plus_constant (virtual_stack_vars_rtx,
2991                                    - RS6000_VARARGS_SIZE);
2992
2993       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
2994     }
2995   else
2996     {
2997       first_reg_offset = next_cum.words;
2998       save_area = virtual_incoming_args_rtx;
2999       cfun->machine->sysv_varargs_p = 0;
3000
3001       if (MUST_PASS_IN_STACK (mode, type))
3002         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
3003     }
3004
3005   set = get_varargs_alias_set ();
3006   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
3007     {
3008       mem = gen_rtx_MEM (BLKmode,
3009                          plus_constant (save_area,
3010                                         first_reg_offset * reg_size)),
3011       set_mem_alias_set (mem, set);
3012       set_mem_align (mem, BITS_PER_WORD);
3013
3014       move_block_from_reg
3015         (GP_ARG_MIN_REG + first_reg_offset, mem,
3016          GP_ARG_NUM_REG - first_reg_offset,
3017          (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD);
3018
3019       /* ??? Does ABI_V4 need this at all?  */
3020       *pretend_size = (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD;
3021     }
3022
3023   /* Save FP registers if needed.  */
3024   if (DEFAULT_ABI == ABI_V4
3025       && TARGET_HARD_FLOAT && ! no_rtl
3026       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
3027     {
3028       int fregno = next_cum.fregno;
3029       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
3030       rtx lab = gen_label_rtx ();
3031       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
3032
3033       emit_jump_insn (gen_rtx_SET (VOIDmode,
3034                                    pc_rtx,
3035                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
3036                                             gen_rtx_NE (VOIDmode, cr1,
3037                                                         const0_rtx),
3038                                             gen_rtx_LABEL_REF (VOIDmode, lab),
3039                                             pc_rtx)));
3040
3041       while (fregno <= FP_ARG_V4_MAX_REG)
3042         {
3043           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
3044           set_mem_alias_set (mem, set);
3045           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
3046           fregno++;
3047           off += 8;
3048         }
3049
3050       emit_label (lab);
3051     }
3052 }
3053
3054 /* Create the va_list data type.  */
3055
3056 tree
3057 rs6000_build_va_list ()
3058 {
3059   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3060
3061   /* For AIX, prefer 'char *' because that's what the system
3062      header files like.  */
3063   if (DEFAULT_ABI != ABI_V4)
3064     return build_pointer_type (char_type_node);
3065
3066   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3067   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3068
3069   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
3070                       unsigned_char_type_node);
3071   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
3072                       unsigned_char_type_node);
3073   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3074                       ptr_type_node);
3075   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3076                       ptr_type_node);
3077
3078   DECL_FIELD_CONTEXT (f_gpr) = record;
3079   DECL_FIELD_CONTEXT (f_fpr) = record;
3080   DECL_FIELD_CONTEXT (f_ovf) = record;
3081   DECL_FIELD_CONTEXT (f_sav) = record;
3082
3083   TREE_CHAIN (record) = type_decl;
3084   TYPE_NAME (record) = type_decl;
3085   TYPE_FIELDS (record) = f_gpr;
3086   TREE_CHAIN (f_gpr) = f_fpr;
3087   TREE_CHAIN (f_fpr) = f_ovf;
3088   TREE_CHAIN (f_ovf) = f_sav;
3089
3090   layout_type (record);
3091
3092   /* The correct type is an array type of one element.  */
3093   return build_array_type (record, build_index_type (size_zero_node));
3094 }
3095
3096 /* Implement va_start.  */
3097
3098 void
3099 rs6000_va_start (stdarg_p, valist, nextarg)
3100      int stdarg_p;
3101      tree valist;
3102      rtx nextarg;
3103 {
3104   HOST_WIDE_INT words, n_gpr, n_fpr;
3105   tree f_gpr, f_fpr, f_ovf, f_sav;
3106   tree gpr, fpr, ovf, sav, t;
3107
3108   /* Only SVR4 needs something special.  */
3109   if (DEFAULT_ABI != ABI_V4)
3110     {
3111       std_expand_builtin_va_start (stdarg_p, valist, nextarg);
3112       return;
3113     }
3114
3115   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3116   f_fpr = TREE_CHAIN (f_gpr);
3117   f_ovf = TREE_CHAIN (f_fpr);
3118   f_sav = TREE_CHAIN (f_ovf);
3119
3120   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3121   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3122   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3123   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3124   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3125
3126   /* Count number of gp and fp argument registers used.  */
3127   words = current_function_args_info.words;
3128   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
3129   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
3130
3131   if (TARGET_DEBUG_ARG)
3132     {
3133       fputs ("va_start: words = ", stderr);
3134       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, words);
3135       fputs (", n_gpr = ", stderr);
3136       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_gpr);
3137       fputs (", n_fpr = ", stderr);
3138       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_fpr);
3139       putc ('\n', stderr);
3140     }
3141
3142   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
3143   TREE_SIDE_EFFECTS (t) = 1;
3144   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3145
3146   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
3147   TREE_SIDE_EFFECTS (t) = 1;
3148   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3149
3150   /* Find the overflow area.  */
3151   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3152   if (words != 0)
3153     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
3154                build_int_2 (words * UNITS_PER_WORD, 0));
3155   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3156   TREE_SIDE_EFFECTS (t) = 1;
3157   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3158
3159   /* Find the register save area.  */
3160   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
3161   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
3162              build_int_2 (-RS6000_VARARGS_SIZE, -1));
3163   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3164   TREE_SIDE_EFFECTS (t) = 1;
3165   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3166 }
3167
3168 /* Implement va_arg.  */
3169
3170 rtx
3171 rs6000_va_arg (valist, type)
3172      tree valist, type;
3173 {
3174   tree f_gpr, f_fpr, f_ovf, f_sav;
3175   tree gpr, fpr, ovf, sav, reg, t, u;
3176   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
3177   rtx lab_false, lab_over, addr_rtx, r;
3178
3179   if (DEFAULT_ABI != ABI_V4)
3180     return std_expand_builtin_va_arg (valist, type);
3181
3182   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3183   f_fpr = TREE_CHAIN (f_gpr);
3184   f_ovf = TREE_CHAIN (f_fpr);
3185   f_sav = TREE_CHAIN (f_ovf);
3186
3187   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3188   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3189   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3190   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3191   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3192
3193   size = int_size_in_bytes (type);
3194   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3195
3196   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
3197     {
3198       /* Aggregates and long doubles are passed by reference.  */
3199       indirect_p = 1;
3200       reg = gpr;
3201       n_reg = 1;
3202       sav_ofs = 0;
3203       sav_scale = 4;
3204       size = UNITS_PER_WORD;
3205       rsize = 1;
3206     }
3207   else if (FLOAT_TYPE_P (type) && ! TARGET_SOFT_FLOAT)
3208     {
3209       /* FP args go in FP registers, if present.  */
3210       indirect_p = 0;
3211       reg = fpr;
3212       n_reg = 1;
3213       sav_ofs = 8*4;
3214       sav_scale = 8;
3215     }
3216   else
3217     {
3218       /* Otherwise into GP registers.  */
3219       indirect_p = 0;
3220       reg = gpr;
3221       n_reg = rsize;
3222       sav_ofs = 0;
3223       sav_scale = 4;
3224     }
3225
3226   /* Pull the value out of the saved registers ...  */
3227
3228   lab_false = gen_label_rtx ();
3229   lab_over = gen_label_rtx ();
3230   addr_rtx = gen_reg_rtx (Pmode);
3231
3232   /*  AltiVec vectors never go in registers.  */
3233   if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
3234     {
3235       TREE_THIS_VOLATILE (reg) = 1;
3236       emit_cmp_and_jump_insns
3237         (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
3238          GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
3239          lab_false);
3240
3241       /* Long long is aligned in the registers.  */
3242       if (n_reg > 1)
3243         {
3244           u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
3245                      build_int_2 (n_reg - 1, 0));
3246           u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
3247           u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
3248           TREE_SIDE_EFFECTS (u) = 1;
3249           expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
3250         }
3251
3252       if (sav_ofs)
3253         t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
3254       else
3255         t = sav;
3256
3257       u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
3258                  build_int_2 (n_reg, 0));
3259       TREE_SIDE_EFFECTS (u) = 1;
3260
3261       u = build1 (CONVERT_EXPR, integer_type_node, u);
3262       TREE_SIDE_EFFECTS (u) = 1;
3263
3264       u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
3265       TREE_SIDE_EFFECTS (u) = 1;
3266
3267       t = build (PLUS_EXPR, ptr_type_node, t, u);
3268       TREE_SIDE_EFFECTS (t) = 1;
3269
3270       r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3271       if (r != addr_rtx)
3272         emit_move_insn (addr_rtx, r);
3273
3274       emit_jump_insn (gen_jump (lab_over));
3275       emit_barrier ();
3276     }
3277
3278   emit_label (lab_false);
3279
3280   /* ... otherwise out of the overflow area.  */
3281
3282   /* Make sure we don't find reg 7 for the next int arg.
3283
3284      All AltiVec vectors go in the overflow area.  So in the AltiVec
3285      case we need to get the vectors from the overflow area, but
3286      remember where the GPRs and FPRs are.  */
3287   if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
3288                     || !TARGET_ALTIVEC))
3289     {
3290       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
3291       TREE_SIDE_EFFECTS (t) = 1;
3292       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3293     }
3294
3295   /* Care for on-stack alignment if needed.  */
3296   if (rsize <= 1)
3297     t = ovf;
3298   else
3299     {
3300       int align;
3301
3302       /* AltiVec vectors are 16 byte aligned.  */
3303       if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
3304         align = 15;
3305       else
3306         align = 7;
3307
3308       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
3309       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
3310     }
3311   t = save_expr (t);
3312
3313   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3314   if (r != addr_rtx)
3315     emit_move_insn (addr_rtx, r);
3316
3317   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
3318   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3319   TREE_SIDE_EFFECTS (t) = 1;
3320   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3321
3322   emit_label (lab_over);
3323
3324   if (indirect_p)
3325     {
3326       r = gen_rtx_MEM (Pmode, addr_rtx);
3327       set_mem_alias_set (r, get_varargs_alias_set ());
3328       emit_move_insn (addr_rtx, r);
3329     }
3330
3331   return addr_rtx;
3332 }
3333
3334 /* Builtins.  */
3335
3336 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
3337 do {                                                                    \
3338   if ((MASK) & target_flags)                                            \
3339     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL);       \
3340 } while (0)
3341
3342 struct builtin_description
3343 {
3344   const unsigned int mask;
3345   const enum insn_code icode;
3346   const char *const name;
3347   const enum rs6000_builtins code;
3348 };
3349
3350 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
3351
3352 static const struct builtin_description bdesc_3arg[] =
3353 {
3354   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
3355   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
3356   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
3357   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
3358   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
3359   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
3360   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
3361   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
3362   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
3363   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
3364   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
3365   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
3366   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
3367   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
3368   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
3369   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
3370   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
3371   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
3372   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
3373   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
3374   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
3375   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
3376   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
3377 };
3378
3379 /* DST operations: void foo (void *, const int, const char).  */
3380
3381 static const struct builtin_description bdesc_dst[] =
3382 {
3383   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
3384   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
3385   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
3386   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
3387 };
3388
3389 /* Simple binary operations: VECc = foo (VECa, VECb).  */
3390
3391 static const struct builtin_description bdesc_2arg[] =
3392 {
3393   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
3394   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
3395   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
3396   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
3397   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
3398   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
3399   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
3400   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
3401   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
3402   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
3403   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
3404   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
3405   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
3406   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
3407   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
3408   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
3409   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
3410   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
3411   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
3412   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
3413   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
3414   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
3415   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
3416   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
3417   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
3418   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
3419   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
3420   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
3421   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
3422   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
3423   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
3424   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
3425   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
3426   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
3427   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
3428   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
3429   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
3430   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
3431   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
3432   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
3433   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
3434   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
3435   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
3436   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
3437   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
3438   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
3439   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
3440   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
3441   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
3442   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
3443   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
3444   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
3445   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
3446   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
3447   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
3448   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
3449   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
3450   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
3451   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
3452   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
3453   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
3454   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
3455   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
3456   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
3457   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
3458   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
3459   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
3460   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
3461   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
3462   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
3463   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
3464   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
3465   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
3466   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
3467   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
3468   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
3469   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
3470   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
3471   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
3472   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
3473   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
3474   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
3475   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
3476   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
3477   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
3478   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
3479   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
3480   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
3481   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
3482   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
3483   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
3484   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
3485   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
3486   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
3487   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
3488   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
3489   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
3490   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
3491   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
3492   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
3493   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
3494   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
3495   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
3496   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
3497   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
3498   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
3499   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
3500   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
3501   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
3502   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
3503   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
3504   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
3505   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
3506 };
3507
3508 /* AltiVec predicates.  */
3509
3510 struct builtin_description_predicates
3511 {
3512   const unsigned int mask;
3513   const enum insn_code icode;
3514   const char *opcode;
3515   const char *const name;
3516   const enum rs6000_builtins code;
3517 };
3518
3519 static const struct builtin_description_predicates bdesc_altivec_preds[] =
3520 {
3521   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
3522   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
3523   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
3524   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
3525   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
3526   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
3527   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
3528   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
3529   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
3530   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
3531   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
3532   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
3533   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
3534 };
3535
3536 /* ABS* opreations.  */
3537
3538 static const struct builtin_description bdesc_abs[] =
3539 {
3540   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
3541   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
3542   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
3543   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
3544   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
3545   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
3546   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
3547 };
3548
3549 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
3550    foo (VECa).  */
3551
3552 static const struct builtin_description bdesc_1arg[] =
3553 {
3554   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
3555   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
3556   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
3557   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
3558   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
3559   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
3560   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
3561   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
3562   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
3563   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
3564   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
3565   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
3566   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
3567   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
3568   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
3569   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
3570   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
3571 };
3572
3573 static rtx
3574 rs6000_expand_unop_builtin (icode, arglist, target)
3575      enum insn_code icode;
3576      tree arglist;
3577      rtx target;
3578 {
3579   rtx pat;
3580   tree arg0 = TREE_VALUE (arglist);
3581   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3582   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3583   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3584
3585   /* If we got invalid arguments bail out before generating bad rtl.  */
3586   if (arg0 == error_mark_node)
3587     return const0_rtx;
3588
3589   switch (icode)
3590     {
3591       /* Only allow 5-bit *signed* literals.  */
3592     case CODE_FOR_altivec_vspltisb:
3593     case CODE_FOR_altivec_vspltish:
3594     case CODE_FOR_altivec_vspltisw:
3595       if (GET_CODE (op0) != CONST_INT
3596           || INTVAL (op0) > 0x1f
3597           || INTVAL (op0) < -0x1f)
3598         {
3599           error ("argument 1 must be a 5-bit signed literal");
3600           return const0_rtx;
3601         }
3602       break;
3603     default:
3604       break;
3605     }
3606
3607   if (target == 0
3608       || GET_MODE (target) != tmode
3609       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3610     target = gen_reg_rtx (tmode);
3611
3612   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3613     op0 = copy_to_mode_reg (mode0, op0);
3614
3615   pat = GEN_FCN (icode) (target, op0);
3616   if (! pat)
3617     return 0;
3618   emit_insn (pat);
3619
3620   return target;
3621 }
3622
3623 static rtx
3624 altivec_expand_abs_builtin (icode, arglist, target)
3625      enum insn_code icode;
3626      tree arglist;
3627      rtx target;
3628 {
3629   rtx pat, scratch1, scratch2;
3630   tree arg0 = TREE_VALUE (arglist);
3631   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3632   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3633   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3634
3635   /* If we have invalid arguments, bail out before generating bad rtl.  */
3636   if (arg0 == error_mark_node)
3637     return const0_rtx;
3638
3639   if (target == 0
3640       || GET_MODE (target) != tmode
3641       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3642     target = gen_reg_rtx (tmode);
3643
3644   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3645     op0 = copy_to_mode_reg (mode0, op0);
3646
3647   scratch1 = gen_reg_rtx (mode0);
3648   scratch2 = gen_reg_rtx (mode0);
3649
3650   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
3651   if (! pat)
3652     return 0;
3653   emit_insn (pat);
3654
3655   return target;
3656 }
3657
3658 static rtx
3659 rs6000_expand_binop_builtin (icode, arglist, target)
3660      enum insn_code icode;
3661      tree arglist;
3662      rtx target;
3663 {
3664   rtx pat;
3665   tree arg0 = TREE_VALUE (arglist);
3666   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3667   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3668   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3669   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3670   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3671   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3672
3673   /* If we got invalid arguments bail out before generating bad rtl.  */
3674   if (arg0 == error_mark_node || arg1 == error_mark_node)
3675     return const0_rtx;
3676
3677   switch (icode)
3678     {
3679       /* Only allow 5-bit unsigned literals.  */
3680     case CODE_FOR_altivec_vcfux:
3681     case CODE_FOR_altivec_vcfsx:
3682     case CODE_FOR_altivec_vctsxs:
3683     case CODE_FOR_altivec_vctuxs:
3684     case CODE_FOR_altivec_vspltb:
3685     case CODE_FOR_altivec_vsplth:
3686     case CODE_FOR_altivec_vspltw:
3687       if (TREE_CODE (arg1) != INTEGER_CST
3688           || TREE_INT_CST_LOW (arg1) & ~0x1f)
3689         {
3690           error ("argument 2 must be a 5-bit unsigned literal");
3691           return const0_rtx;
3692         }
3693       break;
3694     default:
3695       break;
3696     }
3697
3698   if (target == 0
3699       || GET_MODE (target) != tmode
3700       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3701     target = gen_reg_rtx (tmode);
3702
3703   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3704     op0 = copy_to_mode_reg (mode0, op0);
3705   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3706     op1 = copy_to_mode_reg (mode1, op1);
3707
3708   pat = GEN_FCN (icode) (target, op0, op1);
3709   if (! pat)
3710     return 0;
3711   emit_insn (pat);
3712
3713   return target;
3714 }
3715
3716 static rtx
3717 altivec_expand_predicate_builtin (icode, opcode, arglist, target)
3718      enum insn_code icode;
3719      const char *opcode;
3720      tree arglist;
3721      rtx target;
3722 {
3723   rtx pat, scratch;
3724   tree cr6_form = TREE_VALUE (arglist);
3725   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
3726   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3727   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3728   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3729   enum machine_mode tmode = SImode;
3730   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3731   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3732   int cr6_form_int;
3733
3734   if (TREE_CODE (cr6_form) != INTEGER_CST)
3735     {
3736       error ("argument 1 of __builtin_altivec_predicate must be a constant");
3737       return const0_rtx;
3738     }
3739   else
3740     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
3741
3742   if (mode0 != mode1)
3743     abort ();
3744
3745   /* If we have invalid arguments, bail out before generating bad rtl.  */
3746   if (arg0 == error_mark_node || arg1 == error_mark_node)
3747     return const0_rtx;
3748
3749   if (target == 0
3750       || GET_MODE (target) != tmode
3751       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3752     target = gen_reg_rtx (tmode);
3753
3754   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3755     op0 = copy_to_mode_reg (mode0, op0);
3756   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3757     op1 = copy_to_mode_reg (mode1, op1);
3758
3759   scratch = gen_reg_rtx (mode0);
3760
3761   pat = GEN_FCN (icode) (scratch, op0, op1,
3762                          gen_rtx (SYMBOL_REF, Pmode, opcode));
3763   if (! pat)
3764     return 0;
3765   emit_insn (pat);
3766
3767   /* The vec_any* and vec_all* predicates use the same opcodes for two
3768      different operations, but the bits in CR6 will be different
3769      depending on what information we want.  So we have to play tricks
3770      with CR6 to get the right bits out.
3771
3772      If you think this is disgusting, look at the specs for the
3773      AltiVec predicates.  */
3774
3775      switch (cr6_form_int)
3776        {
3777        case 0:
3778          emit_insn (gen_cr6_test_for_zero (target));
3779          break;
3780        case 1:
3781          emit_insn (gen_cr6_test_for_zero_reverse (target));
3782          break;
3783        case 2:
3784          emit_insn (gen_cr6_test_for_lt (target));
3785          break;
3786        case 3:
3787          emit_insn (gen_cr6_test_for_lt_reverse (target));
3788          break;
3789        default:
3790          error ("argument 1 of __builtin_altivec_predicate is out of range");
3791          break;
3792        }
3793
3794   return target;
3795 }
3796
3797 static rtx
3798 altivec_expand_stv_builtin (icode, arglist)
3799      enum insn_code icode;
3800      tree arglist;
3801 {
3802   tree arg0 = TREE_VALUE (arglist);
3803   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3804   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3805   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3806   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3807   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
3808   rtx pat;
3809   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
3810   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
3811   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
3812
3813   /* Invalid arguments.  Bail before doing anything stoopid!  */
3814   if (arg0 == error_mark_node
3815       || arg1 == error_mark_node
3816       || arg2 == error_mark_node)
3817     return const0_rtx;
3818
3819   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
3820     op0 = copy_to_mode_reg (mode2, op0);
3821   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
3822     op1 = copy_to_mode_reg (mode0, op1);
3823   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
3824     op2 = copy_to_mode_reg (mode1, op2);
3825
3826   pat = GEN_FCN (icode) (op1, op2, op0);
3827   if (pat)
3828     emit_insn (pat);
3829   return NULL_RTX;
3830 }
3831
3832 static rtx
3833 rs6000_expand_ternop_builtin (icode, arglist, target)
3834      enum insn_code icode;
3835      tree arglist;
3836      rtx target;
3837 {
3838   rtx pat;
3839   tree arg0 = TREE_VALUE (arglist);
3840   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3841   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3842   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3843   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3844   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
3845   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3846   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3847   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3848   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
3849
3850   /* If we got invalid arguments bail out before generating bad rtl.  */
3851   if (arg0 == error_mark_node
3852       || arg1 == error_mark_node
3853       || arg2 == error_mark_node)
3854     return const0_rtx;
3855
3856   switch (icode)
3857     {
3858       /* Only allow 4-bit unsigned literals.  */
3859     case CODE_FOR_altivec_vsldoi_4sf:
3860     case CODE_FOR_altivec_vsldoi_4si:
3861     case CODE_FOR_altivec_vsldoi_8hi:
3862     case CODE_FOR_altivec_vsldoi_16qi:
3863       if (TREE_CODE (arg2) != INTEGER_CST
3864           || TREE_INT_CST_LOW (arg2) & ~0xf)
3865         {
3866           error ("argument 3 must be a 4-bit unsigned literal");
3867           return const0_rtx;
3868         }
3869       break;
3870     default:
3871       break;
3872     }
3873
3874   if (target == 0
3875       || GET_MODE (target) != tmode
3876       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3877     target = gen_reg_rtx (tmode);
3878
3879   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3880     op0 = copy_to_mode_reg (mode0, op0);
3881   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3882     op1 = copy_to_mode_reg (mode1, op1);
3883   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
3884     op2 = copy_to_mode_reg (mode2, op2);
3885
3886   pat = GEN_FCN (icode) (target, op0, op1, op2);
3887   if (! pat)
3888     return 0;
3889   emit_insn (pat);
3890
3891   return target;
3892 }
3893
3894 /* Expand the lvx builtins.  */
3895 static rtx
3896 altivec_expand_ld_builtin (exp, target, expandedp)
3897      tree exp;
3898      rtx target;
3899      bool *expandedp;
3900 {
3901   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3902   tree arglist = TREE_OPERAND (exp, 1);
3903   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3904   tree arg0;
3905   enum machine_mode tmode, mode0;
3906   rtx pat, target, op0;
3907   enum insn_code icode;
3908
3909   switch (fcode)
3910     {
3911     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
3912       icode = CODE_FOR_altivec_lvx_16qi;
3913       break;
3914     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
3915       icode = CODE_FOR_altivec_lvx_8hi;
3916       break;
3917     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
3918       icode = CODE_FOR_altivec_lvx_4si;
3919       break;
3920     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
3921       icode = CODE_FOR_altivec_lvx_4sf;
3922       break;
3923     default:
3924       *expandedp = false;
3925       return NULL_RTX;
3926     }
3927
3928   *expandedp = true;
3929
3930   arg0 = TREE_VALUE (arglist);
3931   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3932   tmode = insn_data[icode].operand[0].mode;
3933   mode0 = insn_data[icode].operand[1].mode;
3934
3935   if (target == 0
3936       || GET_MODE (target) != tmode
3937       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3938     target = gen_reg_rtx (tmode);
3939
3940   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3941     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3942
3943   pat = GEN_FCN (icode) (target, op0);
3944   if (! pat)
3945     return 0;
3946   emit_insn (pat);
3947   return target;
3948 }
3949
3950 /* Expand the stvx builtins.  */
3951 static rtx
3952 altivec_expand_st_builtin (exp, target, expandedp)
3953      tree exp;
3954      rtx target;
3955      bool *expandedp;
3956 {
3957   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3958   tree arglist = TREE_OPERAND (exp, 1);
3959   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3960   tree arg0, arg1;
3961   enum machine_mode mode0, mode1;
3962   rtx pat, target, op0, op1;
3963   enum insn_code icode;
3964
3965   switch (fcode)
3966     {
3967     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
3968       icode = CODE_FOR_altivec_stvx_16qi;
3969       break;
3970     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
3971       icode = CODE_FOR_altivec_stvx_8hi;
3972       break;
3973     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
3974       icode = CODE_FOR_altivec_stvx_4si;
3975       break;
3976     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
3977       icode = CODE_FOR_altivec_stvx_4sf;
3978       break;
3979     default:
3980       *expandedp = false;
3981       return NULL_RTX;
3982     }
3983
3984   arg0 = TREE_VALUE (arglist);
3985   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3986   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3987   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3988   mode0 = insn_data[icode].operand[0].mode;
3989   mode1 = insn_data[icode].operand[1].mode;
3990
3991   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3992     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3993   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3994     op1 = copy_to_mode_reg (mode1, op1);
3995
3996   pat = GEN_FCN (icode) (op0, op1);
3997   if (pat)
3998     emit_insn (pat);
3999
4000   *expandedp = true;
4001   return NULL_RTX;
4002 }
4003
4004 /* Expand the dst builtins.  */
4005 static rtx
4006 altivec_expand_dst_builtin (exp, target, expandedp)
4007      tree exp;
4008      rtx target;
4009      bool *expandedp;
4010 {
4011   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4012   tree arglist = TREE_OPERAND (exp, 1);
4013   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4014   tree arg0, arg1, arg2;
4015   enum machine_mode mode0, mode1, mode2;
4016   rtx pat, target, op0, op1, op2;
4017   struct builtin_description *d;
4018   int i;
4019
4020   *expandedp = false;
4021
4022   /* Handle DST variants.  */
4023   d = (struct builtin_description *) bdesc_dst;
4024   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
4025     if (d->code == fcode)
4026       {
4027         arg0 = TREE_VALUE (arglist);
4028         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4029         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4030         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4031         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4032         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4033         mode0 = insn_data[d->icode].operand[0].mode;
4034         mode1 = insn_data[d->icode].operand[1].mode;
4035         mode2 = insn_data[d->icode].operand[2].mode;
4036
4037         /* Invalid arguments, bail out before generating bad rtl.  */
4038         if (arg0 == error_mark_node
4039             || arg1 == error_mark_node
4040             || arg2 == error_mark_node)
4041           return const0_rtx;
4042
4043         if (TREE_CODE (arg2) != INTEGER_CST
4044             || TREE_INT_CST_LOW (arg2) & ~0x3)
4045           {
4046             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
4047             return const0_rtx;
4048           }
4049
4050         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
4051           op0 = copy_to_mode_reg (mode0, op0);
4052         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
4053           op1 = copy_to_mode_reg (mode1, op1);
4054
4055         pat = GEN_FCN (d->icode) (op0, op1, op2);
4056         if (pat != 0)
4057           emit_insn (pat);
4058
4059         *expandedp = true;
4060         return NULL_RTX;
4061       }
4062
4063   return NULL_RTX;
4064 }
4065
4066 /* Expand the builtin in EXP and store the result in TARGET.  Store
4067    true in *EXPANDEDP if we found a builtin to expand.  */
4068 static rtx
4069 altivec_expand_builtin (exp, target, expandedp)
4070      tree exp;
4071      rtx target;
4072      bool *expandedp;
4073 {
4074   struct builtin_description *d;
4075   struct builtin_description_predicates *dp;
4076   size_t i;
4077   enum insn_code icode;
4078   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4079   tree arglist = TREE_OPERAND (exp, 1);
4080   tree arg0, arg1, arg2;
4081   rtx op0, op1, op2, pat;
4082   enum machine_mode tmode, mode0, mode1, mode2;
4083   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4084
4085   target = altivec_expand_ld_builtin (exp, target, expandedp);
4086   if (*expandedp)
4087     return target;
4088
4089   target = altivec_expand_st_builtin (exp, target, expandedp);
4090   if (*expandedp)
4091     return target;
4092
4093   target = altivec_expand_dst_builtin (exp, target, expandedp);
4094   if (*expandedp)
4095     return target;
4096
4097   *expandedp = true;
4098
4099   switch (fcode)
4100     {
4101     case ALTIVEC_BUILTIN_STVX:
4102       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
4103     case ALTIVEC_BUILTIN_STVEBX:
4104       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
4105     case ALTIVEC_BUILTIN_STVEHX:
4106       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
4107     case ALTIVEC_BUILTIN_STVEWX:
4108       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
4109     case ALTIVEC_BUILTIN_STVXL:
4110       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
4111
4112     case ALTIVEC_BUILTIN_MFVSCR:
4113       icode = CODE_FOR_altivec_mfvscr;
4114       tmode = insn_data[icode].operand[0].mode;
4115
4116       if (target == 0
4117           || GET_MODE (target) != tmode
4118           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4119         target = gen_reg_rtx (tmode);
4120       
4121       pat = GEN_FCN (icode) (target);
4122       if (! pat)
4123         return 0;
4124       emit_insn (pat);
4125       return target;
4126
4127     case ALTIVEC_BUILTIN_MTVSCR:
4128       icode = CODE_FOR_altivec_mtvscr;
4129       arg0 = TREE_VALUE (arglist);
4130       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4131       mode0 = insn_data[icode].operand[0].mode;
4132
4133       /* If we got invalid arguments bail out before generating bad rtl.  */
4134       if (arg0 == error_mark_node)
4135         return const0_rtx;
4136
4137       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4138         op0 = copy_to_mode_reg (mode0, op0);
4139
4140       pat = GEN_FCN (icode) (op0);
4141       if (pat)
4142         emit_insn (pat);
4143       return NULL_RTX;
4144
4145     case ALTIVEC_BUILTIN_DSSALL:
4146       emit_insn (gen_altivec_dssall ());
4147       return NULL_RTX;
4148
4149     case ALTIVEC_BUILTIN_DSS:
4150       icode = CODE_FOR_altivec_dss;
4151       arg0 = TREE_VALUE (arglist);
4152       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4153       mode0 = insn_data[icode].operand[0].mode;
4154
4155       /* If we got invalid arguments bail out before generating bad rtl.  */
4156       if (arg0 == error_mark_node)
4157         return const0_rtx;
4158
4159       if (TREE_CODE (arg0) != INTEGER_CST
4160           || TREE_INT_CST_LOW (arg0) & ~0x3)
4161         {
4162           error ("argument to dss must be a 2-bit unsigned literal");
4163           return const0_rtx;
4164         }
4165
4166       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4167         op0 = copy_to_mode_reg (mode0, op0);
4168
4169       emit_insn (gen_altivec_dss (op0));
4170       return NULL_RTX;
4171     }
4172
4173   /* Expand abs* operations.  */
4174   d = (struct builtin_description *) bdesc_abs;
4175   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
4176     if (d->code == fcode)
4177       return altivec_expand_abs_builtin (d->icode, arglist, target);
4178
4179   /* Expand the AltiVec predicates.  */
4180   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
4181   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
4182     if (dp->code == fcode)
4183       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
4184
4185   /* LV* are funky.  We initialized them differently.  */
4186   switch (fcode)
4187     {
4188     case ALTIVEC_BUILTIN_LVSL:
4189       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsl,
4190                                            arglist, target);
4191     case ALTIVEC_BUILTIN_LVSR:
4192       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsr,
4193                                           arglist, target);
4194     case ALTIVEC_BUILTIN_LVEBX:
4195       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvebx,
4196                                           arglist, target);
4197     case ALTIVEC_BUILTIN_LVEHX:
4198       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvehx,
4199                                           arglist, target);
4200     case ALTIVEC_BUILTIN_LVEWX:
4201       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvewx,
4202                                           arglist, target);
4203     case ALTIVEC_BUILTIN_LVXL:
4204       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvxl,
4205                                           arglist, target);
4206     case ALTIVEC_BUILTIN_LVX:
4207       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvx,
4208                                           arglist, target);
4209     default:
4210       break;
4211       /* Fall through.  */
4212     }
4213
4214   *expandedp = false;
4215   return NULL_RTX;
4216 }
4217
4218 /* Expand an expression EXP that calls a built-in function,
4219    with result going to TARGET if that's convenient
4220    (and in mode MODE if that's convenient).
4221    SUBTARGET may be used as the target for computing one of EXP's operands.
4222    IGNORE is nonzero if the value is to be ignored.  */
4223
4224 static rtx
4225 rs6000_expand_builtin (exp, target, subtarget, mode, ignore)
4226      tree exp;
4227      rtx target;
4228      rtx subtarget ATTRIBUTE_UNUSED;
4229      enum machine_mode mode ATTRIBUTE_UNUSED;
4230      int ignore ATTRIBUTE_UNUSED;
4231 {
4232   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4233   tree arglist = TREE_OPERAND (exp, 1);
4234   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4235   struct builtin_description *d;
4236   size_t i;
4237   rtx ret;
4238   bool success;
4239   
4240   if (TARGET_ALTIVEC)
4241     {
4242       ret = altivec_expand_builtin (exp, target, &success);
4243
4244       if (success)
4245         return ret;
4246     }
4247
4248   /* Handle simple unary operations.  */
4249   d = (struct builtin_description *) bdesc_1arg;
4250   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
4251     if (d->code == fcode)
4252       return rs6000_expand_unop_builtin (d->icode, arglist, target);
4253
4254   /* Handle simple binary operations.  */
4255   d = (struct builtin_description *) bdesc_2arg;
4256   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
4257     if (d->code == fcode)
4258       return rs6000_expand_binop_builtin (d->icode, arglist, target);
4259
4260   /* Handle simple ternary operations.  */
4261   d = (struct builtin_description *) bdesc_3arg;
4262   for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
4263     if (d->code == fcode)
4264       return rs6000_expand_ternop_builtin (d->icode, arglist, target);
4265
4266   abort ();
4267   return NULL_RTX;
4268 }
4269
4270 static void
4271 rs6000_init_builtins ()
4272 {
4273   if (TARGET_ALTIVEC)
4274     altivec_init_builtins ();
4275 }
4276
4277 static void
4278 altivec_init_builtins (void)
4279 {
4280   struct builtin_description *d;
4281   struct builtin_description_predicates *dp;
4282   size_t i;
4283
4284   tree endlink = void_list_node;
4285
4286   tree pint_type_node = build_pointer_type (integer_type_node);
4287   tree pvoid_type_node = build_pointer_type (void_type_node);
4288   tree pshort_type_node = build_pointer_type (short_integer_type_node);
4289   tree pchar_type_node = build_pointer_type (char_type_node);
4290   tree pfloat_type_node = build_pointer_type (float_type_node);
4291
4292   tree v4sf_ftype_v4sf_v4sf_v16qi
4293     = build_function_type (V4SF_type_node,
4294                            tree_cons (NULL_TREE, V4SF_type_node,
4295                                       tree_cons (NULL_TREE, V4SF_type_node,
4296                                                  tree_cons (NULL_TREE, 
4297                                                             V16QI_type_node,
4298                                                             endlink))));
4299   tree v4si_ftype_v4si_v4si_v16qi
4300     = build_function_type (V4SI_type_node,
4301                            tree_cons (NULL_TREE, V4SI_type_node,
4302                                       tree_cons (NULL_TREE, V4SI_type_node,
4303                                                  tree_cons (NULL_TREE, 
4304                                                             V16QI_type_node,
4305                                                             endlink))));
4306   tree v8hi_ftype_v8hi_v8hi_v16qi
4307     = build_function_type (V8HI_type_node,
4308                            tree_cons (NULL_TREE, V8HI_type_node,
4309                                       tree_cons (NULL_TREE, V8HI_type_node,
4310                                                  tree_cons (NULL_TREE, 
4311                                                             V16QI_type_node,
4312                                                             endlink))));
4313   tree v16qi_ftype_v16qi_v16qi_v16qi
4314     = build_function_type (V16QI_type_node,
4315                            tree_cons (NULL_TREE, V16QI_type_node,
4316                                       tree_cons (NULL_TREE, V16QI_type_node,
4317                                                  tree_cons (NULL_TREE, 
4318                                                             V16QI_type_node,
4319                                                             endlink))));
4320
4321   /* V4SI foo (char).  */
4322   tree v4si_ftype_char
4323     = build_function_type (V4SI_type_node,
4324                            tree_cons (NULL_TREE, char_type_node, endlink));
4325
4326   /* V8HI foo (char).  */
4327   tree v8hi_ftype_char
4328     = build_function_type (V8HI_type_node,
4329                            tree_cons (NULL_TREE, char_type_node, endlink));
4330
4331   /* V16QI foo (char).  */
4332   tree v16qi_ftype_char
4333     = build_function_type (V16QI_type_node,
4334                            tree_cons (NULL_TREE, char_type_node, endlink));
4335   /* V4SF foo (V4SF).  */
4336   tree v4sf_ftype_v4sf
4337     = build_function_type (V4SF_type_node,
4338                            tree_cons (NULL_TREE, V4SF_type_node, endlink));
4339
4340   /* V4SI foo (int *).  */
4341   tree v4si_ftype_pint
4342     = build_function_type (V4SI_type_node,
4343                            tree_cons (NULL_TREE, pint_type_node, endlink));
4344   /* V8HI foo (short *).  */
4345   tree v8hi_ftype_pshort
4346     = build_function_type (V8HI_type_node,
4347                            tree_cons (NULL_TREE, pshort_type_node, endlink));
4348   /* V16QI foo (char *).  */
4349   tree v16qi_ftype_pchar
4350     = build_function_type (V16QI_type_node,
4351                            tree_cons (NULL_TREE, pchar_type_node, endlink));
4352   /* V4SF foo (float *).  */
4353   tree v4sf_ftype_pfloat
4354     = build_function_type (V4SF_type_node,
4355                            tree_cons (NULL_TREE, pfloat_type_node, endlink));
4356
4357   /* V8HI foo (V16QI).  */
4358   tree v8hi_ftype_v16qi
4359     = build_function_type (V8HI_type_node,
4360                            tree_cons (NULL_TREE, V16QI_type_node, endlink));
4361
4362   /* void foo (void *, int, char/literal).  */
4363   tree void_ftype_pvoid_int_char
4364     = build_function_type (void_type_node,
4365                            tree_cons (NULL_TREE, pvoid_type_node,
4366                                       tree_cons (NULL_TREE, integer_type_node,
4367                                                  tree_cons (NULL_TREE,
4368                                                             char_type_node,
4369                                                             endlink))));
4370
4371   /* void foo (int *, V4SI).  */
4372   tree void_ftype_pint_v4si
4373     = build_function_type (void_type_node,
4374                            tree_cons (NULL_TREE, pint_type_node,
4375                                       tree_cons (NULL_TREE, V4SI_type_node,
4376                                                  endlink)));
4377   /* void foo (short *, V8HI).  */
4378   tree void_ftype_pshort_v8hi
4379     = build_function_type (void_type_node,
4380                            tree_cons (NULL_TREE, pshort_type_node,
4381                                       tree_cons (NULL_TREE, V8HI_type_node,
4382                                                  endlink)));
4383   /* void foo (char *, V16QI).  */
4384   tree void_ftype_pchar_v16qi
4385     = build_function_type (void_type_node,
4386                            tree_cons (NULL_TREE, pchar_type_node,
4387                                       tree_cons (NULL_TREE, V16QI_type_node,
4388                                                  endlink)));
4389   /* void foo (float *, V4SF).  */
4390   tree void_ftype_pfloat_v4sf
4391     = build_function_type (void_type_node,
4392                            tree_cons (NULL_TREE, pfloat_type_node,
4393                                       tree_cons (NULL_TREE, V4SF_type_node,
4394                                                  endlink)));
4395
4396   /* void foo (V4SI).  */
4397   tree void_ftype_v4si
4398     = build_function_type (void_type_node,
4399                            tree_cons (NULL_TREE, V4SI_type_node,
4400                                       endlink));
4401
4402   /* void foo (vint, int, void *).  */
4403   tree void_ftype_v4si_int_pvoid
4404     = build_function_type (void_type_node,
4405                            tree_cons (NULL_TREE, V4SI_type_node,
4406                                       tree_cons (NULL_TREE, integer_type_node,
4407                                                  tree_cons (NULL_TREE,
4408                                                             pvoid_type_node,
4409                                                             endlink))));
4410
4411   /* void foo (vchar, int, void *).  */
4412   tree void_ftype_v16qi_int_pvoid
4413     = build_function_type (void_type_node,
4414                            tree_cons (NULL_TREE, V16QI_type_node,
4415                                       tree_cons (NULL_TREE, integer_type_node,
4416                                                  tree_cons (NULL_TREE,
4417                                                             pvoid_type_node,
4418                                                             endlink))));
4419
4420   /* void foo (vshort, int, void *).  */
4421   tree void_ftype_v8hi_int_pvoid
4422     = build_function_type (void_type_node,
4423                            tree_cons (NULL_TREE, V8HI_type_node,
4424                                       tree_cons (NULL_TREE, integer_type_node,
4425                                                  tree_cons (NULL_TREE,
4426                                                             pvoid_type_node,
4427                                                             endlink))));
4428
4429   /* void foo (char).  */
4430   tree void_ftype_qi
4431     = build_function_type (void_type_node,
4432                            tree_cons (NULL_TREE, char_type_node,
4433                                       endlink));
4434
4435   /* void foo (void).  */
4436   tree void_ftype_void
4437     = build_function_type (void_type_node, void_list_node);
4438
4439   /* vshort foo (void).  */
4440   tree v8hi_ftype_void
4441     = build_function_type (V8HI_type_node, void_list_node);
4442
4443   tree v4si_ftype_v4si_v4si
4444     = build_function_type (V4SI_type_node,
4445                            tree_cons (NULL_TREE, V4SI_type_node,
4446                                       tree_cons (NULL_TREE, V4SI_type_node,
4447                                                  endlink)));
4448
4449   /* These are for the unsigned 5 bit literals.  */
4450
4451   tree v4sf_ftype_v4si_char
4452     = build_function_type (V4SF_type_node,
4453                            tree_cons (NULL_TREE, V4SI_type_node,
4454                                       tree_cons (NULL_TREE, char_type_node,
4455                                                  endlink)));
4456   tree v4si_ftype_v4sf_char
4457     = build_function_type (V4SI_type_node,
4458                            tree_cons (NULL_TREE, V4SF_type_node,
4459                                       tree_cons (NULL_TREE, char_type_node,
4460                                                  endlink)));
4461   tree v4si_ftype_v4si_char
4462     = build_function_type (V4SI_type_node,
4463                            tree_cons (NULL_TREE, V4SI_type_node,
4464                                       tree_cons (NULL_TREE, char_type_node,
4465                                                  endlink)));
4466   tree v8hi_ftype_v8hi_char
4467     = build_function_type (V8HI_type_node,
4468                            tree_cons (NULL_TREE, V8HI_type_node,
4469                                       tree_cons (NULL_TREE, char_type_node,
4470                                                  endlink)));
4471   tree v16qi_ftype_v16qi_char
4472     = build_function_type (V16QI_type_node,
4473                            tree_cons (NULL_TREE, V16QI_type_node,
4474                                       tree_cons (NULL_TREE, char_type_node,
4475                                                  endlink)));
4476
4477   /* These are for the unsigned 4 bit literals.  */
4478
4479   tree v16qi_ftype_v16qi_v16qi_char
4480     = build_function_type (V16QI_type_node,
4481                            tree_cons (NULL_TREE, V16QI_type_node,
4482                                       tree_cons (NULL_TREE, V16QI_type_node,
4483                                                  tree_cons (NULL_TREE,
4484                                                             char_type_node,
4485                                                             endlink))));
4486
4487   tree v8hi_ftype_v8hi_v8hi_char
4488     = build_function_type (V8HI_type_node,
4489                            tree_cons (NULL_TREE, V8HI_type_node,
4490                                       tree_cons (NULL_TREE, V8HI_type_node,
4491                                                  tree_cons (NULL_TREE,
4492                                                             char_type_node,
4493                                                             endlink))));
4494
4495   tree v4si_ftype_v4si_v4si_char
4496     = build_function_type (V4SI_type_node,
4497                            tree_cons (NULL_TREE, V4SI_type_node,
4498                                       tree_cons (NULL_TREE, V4SI_type_node,
4499                                                  tree_cons (NULL_TREE,
4500                                                             char_type_node,
4501                                                             endlink))));
4502
4503   tree v4sf_ftype_v4sf_v4sf_char
4504     = build_function_type (V4SF_type_node,
4505                            tree_cons (NULL_TREE, V4SF_type_node,
4506                                       tree_cons (NULL_TREE, V4SF_type_node,
4507                                                  tree_cons (NULL_TREE,
4508                                                             char_type_node,
4509                                                             endlink))));
4510
4511   /* End of 4 bit literals.  */
4512
4513   tree v4sf_ftype_v4sf_v4sf
4514     = build_function_type (V4SF_type_node,
4515                            tree_cons (NULL_TREE, V4SF_type_node,
4516                                       tree_cons (NULL_TREE, V4SF_type_node,
4517                                                  endlink)));
4518   tree v4sf_ftype_v4sf_v4sf_v4si
4519     = build_function_type (V4SF_type_node,
4520                            tree_cons (NULL_TREE, V4SF_type_node,
4521                                       tree_cons (NULL_TREE, V4SF_type_node,
4522                                                  tree_cons (NULL_TREE,
4523                                                             V4SI_type_node,
4524                                                             endlink))));
4525   tree v4sf_ftype_v4sf_v4sf_v4sf
4526     = build_function_type (V4SF_type_node,
4527                            tree_cons (NULL_TREE, V4SF_type_node,
4528                                       tree_cons (NULL_TREE, V4SF_type_node,
4529                                                  tree_cons (NULL_TREE, 
4530                                                             V4SF_type_node,
4531                                                             endlink))));
4532   tree v4si_ftype_v4si_v4si_v4si 
4533     = build_function_type (V4SI_type_node,
4534                            tree_cons (NULL_TREE, V4SI_type_node,
4535                                       tree_cons (NULL_TREE, V4SI_type_node,
4536                                                  tree_cons (NULL_TREE,
4537                                                             V4SI_type_node,
4538                                                             endlink))));
4539
4540   tree v8hi_ftype_v8hi_v8hi
4541     = build_function_type (V8HI_type_node,
4542                            tree_cons (NULL_TREE, V8HI_type_node,
4543                                       tree_cons (NULL_TREE, V8HI_type_node,
4544                                                  endlink)));
4545   tree v8hi_ftype_v8hi_v8hi_v8hi
4546     = build_function_type (V8HI_type_node,
4547                            tree_cons (NULL_TREE, V8HI_type_node,
4548                                       tree_cons (NULL_TREE, V8HI_type_node,
4549                                                  tree_cons (NULL_TREE, 
4550                                                             V8HI_type_node,
4551                                                             endlink))));
4552  tree v4si_ftype_v8hi_v8hi_v4si
4553     = build_function_type (V4SI_type_node,
4554                            tree_cons (NULL_TREE, V8HI_type_node,
4555                                       tree_cons (NULL_TREE, V8HI_type_node,
4556                                                  tree_cons (NULL_TREE,
4557                                                             V4SI_type_node,
4558                                                             endlink))));
4559  tree v4si_ftype_v16qi_v16qi_v4si
4560     = build_function_type (V4SI_type_node,
4561                            tree_cons (NULL_TREE, V16QI_type_node,
4562                                       tree_cons (NULL_TREE, V16QI_type_node,
4563                                                  tree_cons (NULL_TREE,
4564                                                             V4SI_type_node,
4565                                                             endlink))));
4566   
4567   tree v16qi_ftype_v16qi_v16qi
4568     = build_function_type (V16QI_type_node,
4569                            tree_cons (NULL_TREE, V16QI_type_node,
4570                                       tree_cons (NULL_TREE, V16QI_type_node,
4571                                                  endlink)));
4572   
4573   tree v4si_ftype_v4sf_v4sf
4574     = build_function_type (V4SI_type_node,
4575                            tree_cons (NULL_TREE, V4SF_type_node,
4576                                       tree_cons (NULL_TREE, V4SF_type_node,
4577                                                  endlink)));
4578
4579   tree v4si_ftype_v4si
4580     = build_function_type (V4SI_type_node,
4581                            tree_cons (NULL_TREE, V4SI_type_node, endlink));
4582
4583   tree v8hi_ftype_v8hi
4584     = build_function_type (V8HI_type_node,
4585                            tree_cons (NULL_TREE, V8HI_type_node, endlink));
4586
4587   tree v16qi_ftype_v16qi
4588     = build_function_type (V16QI_type_node,
4589                            tree_cons (NULL_TREE, V16QI_type_node, endlink));
4590
4591   tree v8hi_ftype_v16qi_v16qi
4592     = build_function_type (V8HI_type_node,
4593                            tree_cons (NULL_TREE, V16QI_type_node,
4594                                       tree_cons (NULL_TREE, V16QI_type_node,
4595                                                  endlink)));
4596
4597   tree v4si_ftype_v8hi_v8hi
4598     = build_function_type (V4SI_type_node,
4599                            tree_cons (NULL_TREE, V8HI_type_node,
4600                                       tree_cons (NULL_TREE, V8HI_type_node,
4601                                                  endlink)));
4602
4603   tree v8hi_ftype_v4si_v4si
4604     = build_function_type (V8HI_type_node,
4605                            tree_cons (NULL_TREE, V4SI_type_node,
4606                                       tree_cons (NULL_TREE, V4SI_type_node,
4607                                                  endlink)));
4608
4609   tree v16qi_ftype_v8hi_v8hi
4610     = build_function_type (V16QI_type_node,
4611                            tree_cons (NULL_TREE, V8HI_type_node,
4612                                       tree_cons (NULL_TREE, V8HI_type_node,
4613                                                  endlink)));
4614
4615   tree v4si_ftype_v16qi_v4si
4616     = build_function_type (V4SI_type_node,
4617                            tree_cons (NULL_TREE, V16QI_type_node,
4618                                       tree_cons (NULL_TREE, V4SI_type_node,
4619                                                  endlink)));
4620
4621   tree v4si_ftype_v16qi_v16qi
4622     = build_function_type (V4SI_type_node,
4623                            tree_cons (NULL_TREE, V16QI_type_node,
4624                                       tree_cons (NULL_TREE, V16QI_type_node,
4625                                                  endlink)));
4626
4627   tree v4si_ftype_v8hi_v4si
4628     = build_function_type (V4SI_type_node,
4629                            tree_cons (NULL_TREE, V8HI_type_node,
4630                                       tree_cons (NULL_TREE, V4SI_type_node,
4631                                                  endlink)));
4632
4633   tree v4si_ftype_v8hi
4634     = build_function_type (V4SI_type_node,
4635                            tree_cons (NULL_TREE, V8HI_type_node, endlink));
4636
4637   tree int_ftype_v4si_v4si
4638     = build_function_type (integer_type_node,
4639                            tree_cons (NULL_TREE, V4SI_type_node,
4640                                       tree_cons (NULL_TREE, V4SI_type_node,
4641                                                  endlink)));
4642
4643   tree int_ftype_v4sf_v4sf
4644     = build_function_type (integer_type_node,
4645                            tree_cons (NULL_TREE, V4SF_type_node,
4646                                       tree_cons (NULL_TREE, V4SF_type_node,
4647                                                  endlink)));
4648
4649   tree int_ftype_v16qi_v16qi
4650     = build_function_type (integer_type_node,
4651                            tree_cons (NULL_TREE, V16QI_type_node,
4652                                       tree_cons (NULL_TREE, V16QI_type_node,
4653                                                  endlink)));
4654
4655   tree int_ftype_int_v4si_v4si
4656     = build_function_type
4657     (integer_type_node,
4658      tree_cons (NULL_TREE, integer_type_node,
4659                 tree_cons (NULL_TREE, V4SI_type_node,
4660                            tree_cons (NULL_TREE, V4SI_type_node,
4661                                       endlink))));
4662
4663   tree int_ftype_int_v4sf_v4sf
4664     = build_function_type
4665     (integer_type_node,
4666      tree_cons (NULL_TREE, integer_type_node,
4667                 tree_cons (NULL_TREE, V4SF_type_node,
4668                            tree_cons (NULL_TREE, V4SF_type_node,
4669                                       endlink))));
4670
4671   tree int_ftype_int_v8hi_v8hi
4672     = build_function_type
4673     (integer_type_node,
4674      tree_cons (NULL_TREE, integer_type_node,
4675                  tree_cons (NULL_TREE, V8HI_type_node,
4676                             tree_cons (NULL_TREE, V8HI_type_node,
4677                                        endlink))));
4678
4679   tree int_ftype_int_v16qi_v16qi
4680     = build_function_type
4681     (integer_type_node,
4682      tree_cons (NULL_TREE, integer_type_node,
4683                 tree_cons (NULL_TREE, V16QI_type_node,
4684                            tree_cons (NULL_TREE, V16QI_type_node,
4685                                       endlink))));
4686
4687   tree v16qi_ftype_int_pvoid
4688     = build_function_type (V16QI_type_node,
4689                            tree_cons (NULL_TREE, integer_type_node,
4690                                       tree_cons (NULL_TREE, pvoid_type_node,
4691                                                  endlink)));
4692
4693   tree v4si_ftype_int_pvoid
4694     = build_function_type (V4SI_type_node,
4695                            tree_cons (NULL_TREE, integer_type_node,
4696                                       tree_cons (NULL_TREE, pvoid_type_node,
4697                                                  endlink)));
4698
4699   tree v8hi_ftype_int_pvoid
4700     = build_function_type (V8HI_type_node,
4701                            tree_cons (NULL_TREE, integer_type_node,
4702                                       tree_cons (NULL_TREE, pvoid_type_node,
4703                                                  endlink)));
4704
4705   tree int_ftype_v8hi_v8hi
4706     = build_function_type (integer_type_node,
4707                            tree_cons (NULL_TREE, V8HI_type_node,
4708                                       tree_cons (NULL_TREE, V8HI_type_node,
4709                                                  endlink)));
4710
4711   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pfloat, ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
4712   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf, ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
4713   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pint, ALTIVEC_BUILTIN_LD_INTERNAL_4si);
4714   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si, ALTIVEC_BUILTIN_ST_INTERNAL_4si);
4715   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pshort, ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
4716   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi, ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
4717   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pchar, ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
4718   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi, ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
4719   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
4720   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
4721   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
4722   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
4723   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVSL);
4724   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVSR);
4725   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEBX);
4726   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEHX);
4727   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEWX);
4728   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVXL);
4729   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVX);
4730   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVX);
4731   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_int_pvoid, ALTIVEC_BUILTIN_STVEBX);
4732   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_int_pvoid, ALTIVEC_BUILTIN_STVEHX);
4733   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVEWX);
4734   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVXL);
4735
4736   /* Add the simple ternary operators.  */
4737   d = (struct builtin_description *) bdesc_3arg;
4738   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
4739     {
4740       
4741       enum machine_mode mode0, mode1, mode2, mode3;
4742       tree type;
4743
4744       if (d->name == 0)
4745         continue;
4746       
4747       mode0 = insn_data[d->icode].operand[0].mode;
4748       mode1 = insn_data[d->icode].operand[1].mode;
4749       mode2 = insn_data[d->icode].operand[2].mode;
4750       mode3 = insn_data[d->icode].operand[3].mode;
4751       
4752       /* When all four are of the same mode.  */
4753       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
4754         {
4755           switch (mode0)
4756             {
4757             case V4SImode:
4758               type = v4si_ftype_v4si_v4si_v4si;
4759               break;
4760             case V4SFmode:
4761               type = v4sf_ftype_v4sf_v4sf_v4sf;
4762               break;
4763             case V8HImode:
4764               type = v8hi_ftype_v8hi_v8hi_v8hi;
4765               break;          
4766             case V16QImode:
4767               type = v16qi_ftype_v16qi_v16qi_v16qi;
4768               break;          
4769             default:
4770               abort();        
4771             }
4772         }
4773       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
4774         {
4775           switch (mode0)
4776             {
4777             case V4SImode:
4778               type = v4si_ftype_v4si_v4si_v16qi;
4779               break;
4780             case V4SFmode:
4781               type = v4sf_ftype_v4sf_v4sf_v16qi;
4782               break;
4783             case V8HImode:
4784               type = v8hi_ftype_v8hi_v8hi_v16qi;
4785               break;          
4786             case V16QImode:
4787               type = v16qi_ftype_v16qi_v16qi_v16qi;
4788               break;          
4789             default:
4790               abort();        
4791             }
4792         }
4793       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
4794                && mode3 == V4SImode)
4795         type = v4si_ftype_v16qi_v16qi_v4si;
4796       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
4797                && mode3 == V4SImode)
4798         type = v4si_ftype_v8hi_v8hi_v4si;
4799       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
4800                && mode3 == V4SImode)
4801         type = v4sf_ftype_v4sf_v4sf_v4si;
4802
4803       /* vchar, vchar, vchar, 4 bit literal.  */
4804       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
4805                && mode3 == QImode)
4806         type = v16qi_ftype_v16qi_v16qi_char;
4807
4808       /* vshort, vshort, vshort, 4 bit literal.  */
4809       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
4810                && mode3 == QImode)
4811         type = v8hi_ftype_v8hi_v8hi_char;
4812
4813       /* vint, vint, vint, 4 bit literal.  */
4814       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
4815                && mode3 == QImode)
4816         type = v4si_ftype_v4si_v4si_char;
4817
4818       /* vfloat, vfloat, vfloat, 4 bit literal.  */
4819       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
4820                && mode3 == QImode)
4821         type = v4sf_ftype_v4sf_v4sf_char;
4822
4823       else
4824         abort ();
4825
4826       def_builtin (d->mask, d->name, type, d->code);
4827     }
4828
4829   /* Add the DST variants.  */
4830   d = (struct builtin_description *) bdesc_dst;
4831   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
4832     def_builtin (d->mask, d->name, void_ftype_pvoid_int_char, d->code);
4833
4834   /* Initialize the predicates.  */
4835   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
4836   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
4837     {
4838       enum machine_mode mode1;
4839       tree type;
4840
4841       mode1 = insn_data[dp->icode].operand[1].mode;
4842
4843       switch (mode1)
4844         {
4845         case V4SImode:
4846           type = int_ftype_int_v4si_v4si;
4847           break;
4848         case V8HImode:
4849           type = int_ftype_int_v8hi_v8hi;
4850           break;
4851         case V16QImode:
4852           type = int_ftype_int_v16qi_v16qi;
4853           break;
4854         case V4SFmode:
4855           type = int_ftype_int_v4sf_v4sf;
4856           break;
4857         default:
4858           abort ();
4859         }
4860       
4861       def_builtin (dp->mask, dp->name, type, dp->code);
4862     }
4863
4864   /* Add the simple binary operators.  */
4865   d = (struct builtin_description *) bdesc_2arg;
4866   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
4867     {
4868       enum machine_mode mode0, mode1, mode2;
4869       tree type;
4870
4871       if (d->name == 0)
4872         continue;
4873       
4874       mode0 = insn_data[d->icode].operand[0].mode;
4875       mode1 = insn_data[d->icode].operand[1].mode;
4876       mode2 = insn_data[d->icode].operand[2].mode;
4877
4878       /* When all three operands are of the same mode.  */
4879       if (mode0 == mode1 && mode1 == mode2)
4880         {
4881           switch (mode0)
4882             {
4883             case V4SFmode:
4884               type = v4sf_ftype_v4sf_v4sf;
4885               break;
4886             case V4SImode:
4887               type = v4si_ftype_v4si_v4si;
4888               break;
4889             case V16QImode:
4890               type = v16qi_ftype_v16qi_v16qi;
4891               break;
4892             case V8HImode:
4893               type = v8hi_ftype_v8hi_v8hi;
4894               break;
4895             default:
4896               abort ();
4897             }
4898         }
4899
4900       /* A few other combos we really don't want to do manually.  */
4901
4902       /* vint, vfloat, vfloat.  */
4903       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
4904         type = v4si_ftype_v4sf_v4sf;
4905
4906       /* vshort, vchar, vchar.  */
4907       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
4908         type = v8hi_ftype_v16qi_v16qi;
4909
4910       /* vint, vshort, vshort.  */
4911       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
4912         type = v4si_ftype_v8hi_v8hi;
4913
4914       /* vshort, vint, vint.  */
4915       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
4916         type = v8hi_ftype_v4si_v4si;
4917
4918       /* vchar, vshort, vshort.  */
4919       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
4920         type = v16qi_ftype_v8hi_v8hi;
4921
4922       /* vint, vchar, vint.  */
4923       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
4924         type = v4si_ftype_v16qi_v4si;
4925
4926       /* vint, vchar, vchar.  */
4927       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
4928         type = v4si_ftype_v16qi_v16qi;
4929
4930       /* vint, vshort, vint.  */
4931       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
4932         type = v4si_ftype_v8hi_v4si;
4933       
4934       /* vint, vint, 5 bit literal.  */
4935       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
4936         type = v4si_ftype_v4si_char;
4937       
4938       /* vshort, vshort, 5 bit literal.  */
4939       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
4940         type = v8hi_ftype_v8hi_char;
4941       
4942       /* vchar, vchar, 5 bit literal.  */
4943       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
4944         type = v16qi_ftype_v16qi_char;
4945
4946       /* vfloat, vint, 5 bit literal.  */
4947       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
4948         type = v4sf_ftype_v4si_char;
4949       
4950       /* vint, vfloat, 5 bit literal.  */
4951       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
4952         type = v4si_ftype_v4sf_char;
4953
4954       /* int, x, x.  */
4955       else if (mode0 == SImode)
4956         {
4957           switch (mode1)
4958             {
4959             case V4SImode:
4960               type = int_ftype_v4si_v4si;
4961               break;
4962             case V4SFmode:
4963               type = int_ftype_v4sf_v4sf;
4964               break;
4965             case V16QImode:
4966               type = int_ftype_v16qi_v16qi;
4967               break;
4968             case V8HImode:
4969               type = int_ftype_v8hi_v8hi;
4970               break;
4971             default:
4972               abort ();
4973             }
4974         }
4975
4976       else
4977         abort ();
4978
4979       def_builtin (d->mask, d->name, type, d->code);
4980     }
4981
4982   /* Initialize the abs* operators.  */
4983   d = (struct builtin_description *) bdesc_abs;
4984   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
4985     {
4986       enum machine_mode mode0;
4987       tree type;
4988
4989       mode0 = insn_data[d->icode].operand[0].mode;
4990
4991       switch (mode0)
4992         {
4993         case V4SImode:
4994           type = v4si_ftype_v4si;
4995           break;
4996         case V8HImode:
4997           type = v8hi_ftype_v8hi;
4998           break;
4999         case V16QImode:
5000           type = v16qi_ftype_v16qi;
5001           break;
5002         case V4SFmode:
5003           type = v4sf_ftype_v4sf;
5004           break;
5005         default:
5006           abort ();
5007         }
5008       
5009       def_builtin (d->mask, d->name, type, d->code);
5010     }
5011
5012   /* Add the simple unary operators.  */
5013   d = (struct builtin_description *) bdesc_1arg;
5014   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5015     {
5016       enum machine_mode mode0, mode1;
5017       tree type;
5018
5019       if (d->name == 0)
5020         continue;
5021       
5022       mode0 = insn_data[d->icode].operand[0].mode;
5023       mode1 = insn_data[d->icode].operand[1].mode;
5024
5025       if (mode0 == V4SImode && mode1 == QImode)
5026         type = v4si_ftype_char;
5027       else if (mode0 == V8HImode && mode1 == QImode)
5028         type = v8hi_ftype_char;
5029       else if (mode0 == V16QImode && mode1 == QImode)
5030         type = v16qi_ftype_char;
5031       else if (mode0 == V4SFmode && mode1 == V4SFmode)
5032         type = v4sf_ftype_v4sf;
5033       else if (mode0 == V8HImode && mode1 == V16QImode)
5034         type = v8hi_ftype_v16qi;
5035       else if (mode0 == V4SImode && mode1 == V8HImode)
5036         type = v4si_ftype_v8hi;
5037       else
5038         abort ();
5039
5040       def_builtin (d->mask, d->name, type, d->code);
5041     }
5042 }
5043
5044 \f
5045 /* Generate a memory reference for expand_block_move, copying volatile,
5046    and other bits from an original memory reference.  */
5047
5048 static rtx
5049 expand_block_move_mem (mode, addr, orig_mem)
5050      enum machine_mode mode;
5051      rtx addr;
5052      rtx orig_mem;
5053 {
5054   rtx mem = gen_rtx_MEM (mode, addr);
5055
5056   MEM_COPY_ATTRIBUTES (mem, orig_mem);
5057   return mem;
5058 }
5059
5060 /* Expand a block move operation, and return 1 if successful.  Return 0
5061    if we should let the compiler generate normal code.
5062
5063    operands[0] is the destination
5064    operands[1] is the source
5065    operands[2] is the length
5066    operands[3] is the alignment */
5067
5068 #define MAX_MOVE_REG 4
5069
5070 int
5071 expand_block_move (operands)
5072      rtx operands[];
5073 {
5074   rtx orig_dest = operands[0];
5075   rtx orig_src  = operands[1];
5076   rtx bytes_rtx = operands[2];
5077   rtx align_rtx = operands[3];
5078   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
5079   int align;
5080   int bytes;
5081   int offset;
5082   int num_reg;
5083   int i;
5084   rtx src_reg;
5085   rtx dest_reg;
5086   rtx src_addr;
5087   rtx dest_addr;
5088   rtx tmp_reg;
5089   rtx stores[MAX_MOVE_REG];
5090   int move_bytes;
5091
5092   /* If this is not a fixed size move, just call memcpy */
5093   if (! constp)
5094     return 0;
5095
5096   /* If this is not a fixed size alignment, abort */
5097   if (GET_CODE (align_rtx) != CONST_INT)
5098     abort ();
5099   align = INTVAL (align_rtx);
5100
5101   /* Anything to move? */
5102   bytes = INTVAL (bytes_rtx);
5103   if (bytes <= 0)
5104     return 1;
5105
5106   /* store_one_arg depends on expand_block_move to handle at least the size of
5107      reg_parm_stack_space.  */  
5108   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
5109     return 0;
5110
5111   /* Move the address into scratch registers.  */
5112   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
5113   src_reg  = copy_addr_to_reg (XEXP (orig_src,  0));
5114
5115   if (TARGET_STRING)    /* string instructions are available */
5116     {
5117       for ( ; bytes > 0; bytes -= move_bytes)
5118         {
5119           if (bytes > 24                /* move up to 32 bytes at a time */
5120               && ! fixed_regs[5]
5121               && ! fixed_regs[6]
5122               && ! fixed_regs[7]
5123               && ! fixed_regs[8]
5124               && ! fixed_regs[9]
5125               && ! fixed_regs[10]
5126               && ! fixed_regs[11]
5127               && ! fixed_regs[12])
5128             {
5129               move_bytes = (bytes > 32) ? 32 : bytes;
5130               emit_insn (gen_movstrsi_8reg (expand_block_move_mem (BLKmode,
5131                                                                    dest_reg,
5132                                                                    orig_dest),
5133                                             expand_block_move_mem (BLKmode,
5134                                                                    src_reg,
5135                                                                    orig_src),
5136                                             GEN_INT ((move_bytes == 32)
5137                                                      ? 0 : move_bytes),
5138                                             align_rtx));
5139             }
5140           else if (bytes > 16   /* move up to 24 bytes at a time */
5141                    && ! fixed_regs[5]
5142                    && ! fixed_regs[6]
5143                    && ! fixed_regs[7]
5144                    && ! fixed_regs[8]
5145                    && ! fixed_regs[9]
5146                    && ! fixed_regs[10])
5147             {
5148               move_bytes = (bytes > 24) ? 24 : bytes;
5149               emit_insn (gen_movstrsi_6reg (expand_block_move_mem (BLKmode,
5150                                                                    dest_reg,
5151                                                                    orig_dest),
5152                                             expand_block_move_mem (BLKmode,
5153                                                                    src_reg,
5154                                                                    orig_src),
5155                                             GEN_INT (move_bytes),
5156                                             align_rtx));
5157             }
5158           else if (bytes > 8    /* move up to 16 bytes at a time */
5159                    && ! fixed_regs[5]
5160                    && ! fixed_regs[6]
5161                    && ! fixed_regs[7]
5162                    && ! fixed_regs[8])
5163             {
5164               move_bytes = (bytes > 16) ? 16 : bytes;
5165               emit_insn (gen_movstrsi_4reg (expand_block_move_mem (BLKmode,
5166                                                                    dest_reg,
5167                                                                    orig_dest),
5168                                             expand_block_move_mem (BLKmode,
5169                                                                    src_reg,
5170                                                                    orig_src),
5171                                             GEN_INT (move_bytes),
5172                                             align_rtx));
5173             }
5174           else if (bytes >= 8 && TARGET_POWERPC64
5175                    /* 64-bit loads and stores require word-aligned
5176                       displacements.  */
5177                    && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
5178             {
5179               move_bytes = 8;
5180               tmp_reg = gen_reg_rtx (DImode);
5181               emit_move_insn (tmp_reg,
5182                               expand_block_move_mem (DImode,
5183                                                      src_reg, orig_src));
5184               emit_move_insn (expand_block_move_mem (DImode,
5185                                                      dest_reg, orig_dest),
5186                               tmp_reg);
5187             }
5188           else if (bytes > 4 && !TARGET_POWERPC64)
5189             {                   /* move up to 8 bytes at a time */
5190               move_bytes = (bytes > 8) ? 8 : bytes;
5191               emit_insn (gen_movstrsi_2reg (expand_block_move_mem (BLKmode,
5192                                                                    dest_reg,
5193                                                                    orig_dest),
5194                                             expand_block_move_mem (BLKmode,
5195                                                                    src_reg,
5196                                                                    orig_src),
5197                                             GEN_INT (move_bytes),
5198                                             align_rtx));
5199             }
5200           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
5201             {                   /* move 4 bytes */
5202               move_bytes = 4;
5203               tmp_reg = gen_reg_rtx (SImode);
5204               emit_move_insn (tmp_reg,
5205                               expand_block_move_mem (SImode,
5206                                                      src_reg, orig_src));
5207               emit_move_insn (expand_block_move_mem (SImode,
5208                                                      dest_reg, orig_dest),
5209                               tmp_reg);
5210             }
5211           else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
5212             {                   /* move 2 bytes */
5213               move_bytes = 2;
5214               tmp_reg = gen_reg_rtx (HImode);
5215               emit_move_insn (tmp_reg,
5216                               expand_block_move_mem (HImode,
5217                                                      src_reg, orig_src));
5218               emit_move_insn (expand_block_move_mem (HImode,
5219                                                      dest_reg, orig_dest),
5220                               tmp_reg);
5221             }
5222           else if (bytes == 1)  /* move 1 byte */
5223             {
5224               move_bytes = 1;
5225               tmp_reg = gen_reg_rtx (QImode);
5226               emit_move_insn (tmp_reg,
5227                               expand_block_move_mem (QImode,
5228                                                      src_reg, orig_src));
5229               emit_move_insn (expand_block_move_mem (QImode,
5230                                                      dest_reg, orig_dest),
5231                               tmp_reg);
5232             }
5233           else
5234             {                   /* move up to 4 bytes at a time */
5235               move_bytes = (bytes > 4) ? 4 : bytes;
5236               emit_insn (gen_movstrsi_1reg (expand_block_move_mem (BLKmode,
5237                                                                    dest_reg,
5238                                                                    orig_dest),
5239                                             expand_block_move_mem (BLKmode,
5240                                                                    src_reg,
5241                                                                    orig_src),
5242                                             GEN_INT (move_bytes),
5243                                             align_rtx));
5244             }
5245
5246           if (bytes > move_bytes)
5247             {
5248               if (! TARGET_POWERPC64)
5249                 {
5250                   emit_insn (gen_addsi3 (src_reg, src_reg,
5251                                          GEN_INT (move_bytes)));
5252                   emit_insn (gen_addsi3 (dest_reg, dest_reg,
5253                                          GEN_INT (move_bytes)));
5254                 }
5255               else
5256                 {
5257                   emit_insn (gen_adddi3 (src_reg, src_reg,
5258                                          GEN_INT (move_bytes)));
5259                   emit_insn (gen_adddi3 (dest_reg, dest_reg,
5260                                          GEN_INT (move_bytes)));
5261                 }
5262             }
5263         }
5264     }
5265
5266   else                  /* string instructions not available */
5267     {
5268       num_reg = offset = 0;
5269       for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
5270         {
5271           /* Calculate the correct offset for src/dest */
5272           if (offset == 0)
5273             {
5274               src_addr  = src_reg;
5275               dest_addr = dest_reg;
5276             }
5277           else
5278             {
5279               src_addr = plus_constant (src_reg, offset);
5280               dest_addr = plus_constant (dest_reg, offset);
5281             }
5282
5283           /* Generate the appropriate load and store, saving the stores
5284              for later.  */
5285           if (bytes >= 8 && TARGET_POWERPC64
5286               /* 64-bit loads and stores require word-aligned
5287                  displacements.  */
5288               && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
5289             {
5290               move_bytes = 8;
5291               tmp_reg = gen_reg_rtx (DImode);
5292               emit_insn (gen_movdi (tmp_reg,
5293                                     expand_block_move_mem (DImode,
5294                                                            src_addr,
5295                                                            orig_src)));
5296               stores[num_reg++] = gen_movdi (expand_block_move_mem (DImode,
5297                                                                     dest_addr,
5298                                                                     orig_dest),
5299                                              tmp_reg);
5300             }
5301           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
5302             {
5303               move_bytes = 4;
5304               tmp_reg = gen_reg_rtx (SImode);
5305               emit_insn (gen_movsi (tmp_reg,
5306                                     expand_block_move_mem (SImode,
5307                                                            src_addr,
5308                                                            orig_src)));
5309               stores[num_reg++] = gen_movsi (expand_block_move_mem (SImode,
5310                                                                     dest_addr,
5311                                                                     orig_dest),
5312                                              tmp_reg);
5313             }
5314           else if (bytes >= 2 && (align >= 2 || ! STRICT_ALIGNMENT))
5315             {
5316               move_bytes = 2;
5317               tmp_reg = gen_reg_rtx (HImode);
5318               emit_insn (gen_movhi (tmp_reg,
5319                                     expand_block_move_mem (HImode,
5320                                                            src_addr,
5321                                                            orig_src)));
5322               stores[num_reg++] = gen_movhi (expand_block_move_mem (HImode,
5323                                                                     dest_addr,
5324                                                                     orig_dest),
5325                                              tmp_reg);
5326             }
5327           else
5328             {
5329               move_bytes = 1;
5330               tmp_reg = gen_reg_rtx (QImode);
5331               emit_insn (gen_movqi (tmp_reg,
5332                                     expand_block_move_mem (QImode,
5333                                                            src_addr,
5334                                                            orig_src)));
5335               stores[num_reg++] = gen_movqi (expand_block_move_mem (QImode,
5336                                                                     dest_addr,
5337                                                                     orig_dest),
5338                                              tmp_reg);
5339             }
5340
5341           if (num_reg >= MAX_MOVE_REG)
5342             {
5343               for (i = 0; i < num_reg; i++)
5344                 emit_insn (stores[i]);
5345               num_reg = 0;
5346             }
5347         }
5348
5349       for (i = 0; i < num_reg; i++)
5350         emit_insn (stores[i]);
5351     }
5352
5353   return 1;
5354 }
5355
5356 \f
5357 /* Return 1 if OP is a load multiple operation.  It is known to be a
5358    PARALLEL and the first section will be tested.  */
5359
5360 int
5361 load_multiple_operation (op, mode)
5362      rtx op;
5363      enum machine_mode mode ATTRIBUTE_UNUSED;
5364 {
5365   int count = XVECLEN (op, 0);
5366   unsigned int dest_regno;
5367   rtx src_addr;
5368   int i;
5369
5370   /* Perform a quick check so we don't blow up below.  */
5371   if (count <= 1
5372       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5373       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
5374       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
5375     return 0;
5376
5377   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
5378   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
5379
5380   for (i = 1; i < count; i++)
5381     {
5382       rtx elt = XVECEXP (op, 0, i);
5383
5384       if (GET_CODE (elt) != SET
5385           || GET_CODE (SET_DEST (elt)) != REG
5386           || GET_MODE (SET_DEST (elt)) != SImode
5387           || REGNO (SET_DEST (elt)) != dest_regno + i
5388           || GET_CODE (SET_SRC (elt)) != MEM
5389           || GET_MODE (SET_SRC (elt)) != SImode
5390           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5391           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5392           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5393           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
5394         return 0;
5395     }
5396
5397   return 1;
5398 }
5399
5400 /* Similar, but tests for store multiple.  Here, the second vector element
5401    is a CLOBBER.  It will be tested later.  */
5402
5403 int
5404 store_multiple_operation (op, mode)
5405      rtx op;
5406      enum machine_mode mode ATTRIBUTE_UNUSED;
5407 {
5408   int count = XVECLEN (op, 0) - 1;
5409   unsigned int src_regno;
5410   rtx dest_addr;
5411   int i;
5412
5413   /* Perform a quick check so we don't blow up below.  */
5414   if (count <= 1
5415       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5416       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
5417       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
5418     return 0;
5419
5420   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
5421   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
5422
5423   for (i = 1; i < count; i++)
5424     {
5425       rtx elt = XVECEXP (op, 0, i + 1);
5426
5427       if (GET_CODE (elt) != SET
5428           || GET_CODE (SET_SRC (elt)) != REG
5429           || GET_MODE (SET_SRC (elt)) != SImode
5430           || REGNO (SET_SRC (elt)) != src_regno + i
5431           || GET_CODE (SET_DEST (elt)) != MEM
5432           || GET_MODE (SET_DEST (elt)) != SImode
5433           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5434           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5435           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5436           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
5437         return 0;
5438     }
5439
5440   return 1;
5441 }
5442
5443 /* Return 1 for a parallel vrsave operation.  */
5444
5445 int
5446 vrsave_operation (op, mode)
5447      rtx op;
5448      enum machine_mode mode ATTRIBUTE_UNUSED;
5449 {
5450   int count = XVECLEN (op, 0);
5451   unsigned int dest_regno, src_regno;
5452   int i;
5453
5454   if (count <= 1
5455       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5456       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
5457       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
5458     return 0;
5459
5460   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
5461   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
5462
5463   if (dest_regno != VRSAVE_REGNO
5464       && src_regno != VRSAVE_REGNO)
5465     return 0;
5466
5467   for (i = 1; i < count; i++)
5468     {
5469       rtx elt = XVECEXP (op, 0, i);
5470
5471       if (GET_CODE (elt) != CLOBBER
5472           && GET_CODE (elt) != SET)
5473         return 0;
5474     }
5475
5476   return 1;
5477 }
5478
5479 /* Return 1 for an PARALLEL suitable for mtcrf.  */
5480
5481 int
5482 mtcrf_operation (op, mode)
5483      rtx op;
5484      enum machine_mode mode ATTRIBUTE_UNUSED;
5485 {
5486   int count = XVECLEN (op, 0);
5487   int i;
5488   rtx src_reg;
5489
5490   /* Perform a quick check so we don't blow up below.  */
5491   if (count < 1
5492       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5493       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
5494       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
5495     return 0;
5496   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
5497   
5498   if (GET_CODE (src_reg) != REG
5499       || GET_MODE (src_reg) != SImode
5500       || ! INT_REGNO_P (REGNO (src_reg)))
5501     return 0;
5502
5503   for (i = 0; i < count; i++)
5504     {
5505       rtx exp = XVECEXP (op, 0, i);
5506       rtx unspec;
5507       int maskval;
5508       
5509       if (GET_CODE (exp) != SET
5510           || GET_CODE (SET_DEST (exp)) != REG
5511           || GET_MODE (SET_DEST (exp)) != CCmode
5512           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
5513         return 0;
5514       unspec = SET_SRC (exp);
5515       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
5516       
5517       if (GET_CODE (unspec) != UNSPEC
5518           || XINT (unspec, 1) != 20
5519           || XVECLEN (unspec, 0) != 2
5520           || XVECEXP (unspec, 0, 0) != src_reg
5521           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
5522           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
5523         return 0;
5524     }
5525   return 1;
5526 }
5527
5528 /* Return 1 for an PARALLEL suitable for lmw.  */
5529
5530 int
5531 lmw_operation (op, mode)
5532      rtx op;
5533      enum machine_mode mode ATTRIBUTE_UNUSED;
5534 {
5535   int count = XVECLEN (op, 0);
5536   unsigned int dest_regno;
5537   rtx src_addr;
5538   unsigned int base_regno;
5539   HOST_WIDE_INT offset;
5540   int i;
5541
5542   /* Perform a quick check so we don't blow up below.  */
5543   if (count <= 1
5544       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5545       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
5546       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
5547     return 0;
5548
5549   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
5550   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
5551
5552   if (dest_regno > 31
5553       || count != 32 - (int) dest_regno)
5554     return 0;
5555
5556   if (LEGITIMATE_INDIRECT_ADDRESS_P (src_addr, 0))
5557     {
5558       offset = 0;
5559       base_regno = REGNO (src_addr);
5560       if (base_regno == 0)
5561         return 0;
5562     }
5563   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, src_addr, 0))
5564     {
5565       offset = INTVAL (XEXP (src_addr, 1));
5566       base_regno = REGNO (XEXP (src_addr, 0));
5567     }
5568   else
5569     return 0;
5570
5571   for (i = 0; i < count; i++)
5572     {
5573       rtx elt = XVECEXP (op, 0, i);
5574       rtx newaddr;
5575       rtx addr_reg;
5576       HOST_WIDE_INT newoffset;
5577
5578       if (GET_CODE (elt) != SET
5579           || GET_CODE (SET_DEST (elt)) != REG
5580           || GET_MODE (SET_DEST (elt)) != SImode
5581           || REGNO (SET_DEST (elt)) != dest_regno + i
5582           || GET_CODE (SET_SRC (elt)) != MEM
5583           || GET_MODE (SET_SRC (elt)) != SImode)
5584         return 0;
5585       newaddr = XEXP (SET_SRC (elt), 0);
5586       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
5587         {
5588           newoffset = 0;
5589           addr_reg = newaddr;
5590         }
5591       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
5592         {
5593           addr_reg = XEXP (newaddr, 0);
5594           newoffset = INTVAL (XEXP (newaddr, 1));
5595         }
5596       else
5597         return 0;
5598       if (REGNO (addr_reg) != base_regno
5599           || newoffset != offset + 4 * i)
5600         return 0;
5601     }
5602
5603   return 1;
5604 }
5605
5606 /* Return 1 for an PARALLEL suitable for stmw.  */
5607
5608 int
5609 stmw_operation (op, mode)
5610      rtx op;
5611      enum machine_mode mode ATTRIBUTE_UNUSED;
5612 {
5613   int count = XVECLEN (op, 0);
5614   unsigned int src_regno;
5615   rtx dest_addr;
5616   unsigned int base_regno;
5617   HOST_WIDE_INT offset;
5618   int i;
5619
5620   /* Perform a quick check so we don't blow up below.  */
5621   if (count <= 1
5622       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5623       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
5624       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
5625     return 0;
5626
5627   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
5628   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
5629
5630   if (src_regno > 31
5631       || count != 32 - (int) src_regno)
5632     return 0;
5633
5634   if (LEGITIMATE_INDIRECT_ADDRESS_P (dest_addr, 0))
5635     {
5636       offset = 0;
5637       base_regno = REGNO (dest_addr);
5638       if (base_regno == 0)
5639         return 0;
5640     }
5641   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, dest_addr, 0))
5642     {
5643       offset = INTVAL (XEXP (dest_addr, 1));
5644       base_regno = REGNO (XEXP (dest_addr, 0));
5645     }
5646   else
5647     return 0;
5648
5649   for (i = 0; i < count; i++)
5650     {
5651       rtx elt = XVECEXP (op, 0, i);
5652       rtx newaddr;
5653       rtx addr_reg;
5654       HOST_WIDE_INT newoffset;
5655
5656       if (GET_CODE (elt) != SET
5657           || GET_CODE (SET_SRC (elt)) != REG
5658           || GET_MODE (SET_SRC (elt)) != SImode
5659           || REGNO (SET_SRC (elt)) != src_regno + i
5660           || GET_CODE (SET_DEST (elt)) != MEM
5661           || GET_MODE (SET_DEST (elt)) != SImode)
5662         return 0;
5663       newaddr = XEXP (SET_DEST (elt), 0);
5664       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
5665         {
5666           newoffset = 0;
5667           addr_reg = newaddr;
5668         }
5669       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
5670         {
5671           addr_reg = XEXP (newaddr, 0);
5672           newoffset = INTVAL (XEXP (newaddr, 1));
5673         }
5674       else
5675         return 0;
5676       if (REGNO (addr_reg) != base_regno
5677           || newoffset != offset + 4 * i)
5678         return 0;
5679     }
5680
5681   return 1;
5682 }
5683 \f
5684 /* A validation routine: say whether CODE, a condition code, and MODE
5685    match.  The other alternatives either don't make sense or should
5686    never be generated.  */
5687
5688 static void
5689 validate_condition_mode (code, mode)
5690      enum rtx_code code;
5691      enum machine_mode mode;
5692 {
5693   if (GET_RTX_CLASS (code) != '<' 
5694       || GET_MODE_CLASS (mode) != MODE_CC)
5695     abort ();
5696
5697   /* These don't make sense.  */
5698   if ((code == GT || code == LT || code == GE || code == LE)
5699       && mode == CCUNSmode)
5700     abort ();
5701
5702   if ((code == GTU || code == LTU || code == GEU || code == LEU)
5703       && mode != CCUNSmode)
5704     abort ();
5705
5706   if (mode != CCFPmode
5707       && (code == ORDERED || code == UNORDERED
5708           || code == UNEQ || code == LTGT
5709           || code == UNGT || code == UNLT
5710           || code == UNGE || code == UNLE))
5711     abort ();
5712   
5713   /* These should never be generated except for 
5714      flag_unsafe_math_optimizations.  */
5715   if (mode == CCFPmode
5716       && ! flag_unsafe_math_optimizations
5717       && (code == LE || code == GE
5718           || code == UNEQ || code == LTGT
5719           || code == UNGT || code == UNLT))
5720     abort ();
5721
5722   /* These are invalid; the information is not there.  */
5723   if (mode == CCEQmode 
5724       && code != EQ && code != NE)
5725     abort ();
5726 }
5727
5728 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
5729    We only check the opcode against the mode of the CC value here.  */
5730
5731 int
5732 branch_comparison_operator (op, mode)
5733      rtx op;
5734      enum machine_mode mode ATTRIBUTE_UNUSED;
5735 {
5736   enum rtx_code code = GET_CODE (op);
5737   enum machine_mode cc_mode;
5738
5739   if (GET_RTX_CLASS (code) != '<')
5740     return 0;
5741
5742   cc_mode = GET_MODE (XEXP (op, 0));
5743   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
5744     return 0;
5745
5746   validate_condition_mode (code, cc_mode);
5747
5748   return 1;
5749 }
5750
5751 /* Return 1 if OP is a comparison operation that is valid for a branch
5752    insn and which is true if the corresponding bit in the CC register
5753    is set.  */
5754
5755 int
5756 branch_positive_comparison_operator (op, mode)
5757      rtx op;
5758      enum machine_mode mode;
5759 {
5760   enum rtx_code code;
5761
5762   if (! branch_comparison_operator (op, mode))
5763     return 0;
5764
5765   code = GET_CODE (op);
5766   return (code == EQ || code == LT || code == GT
5767           || code == LTU || code == GTU
5768           || code == UNORDERED);
5769 }
5770
5771 /* Return 1 if OP is a comparison operation that is valid for an scc insn.
5772    We check the opcode against the mode of the CC value and disallow EQ or
5773    NE comparisons for integers.  */
5774
5775 int
5776 scc_comparison_operator (op, mode)
5777      rtx op;
5778      enum machine_mode mode;
5779 {
5780   enum rtx_code code = GET_CODE (op);
5781   enum machine_mode cc_mode;
5782
5783   if (GET_MODE (op) != mode && mode != VOIDmode)
5784     return 0;
5785
5786   if (GET_RTX_CLASS (code) != '<')
5787     return 0;
5788
5789   cc_mode = GET_MODE (XEXP (op, 0));
5790   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
5791     return 0;
5792
5793   validate_condition_mode (code, cc_mode);
5794
5795   if (code == NE && cc_mode != CCFPmode)
5796     return 0;
5797
5798   return 1;
5799 }
5800
5801 int
5802 trap_comparison_operator (op, mode)
5803     rtx op;
5804     enum machine_mode mode;
5805 {
5806   if (mode != VOIDmode && mode != GET_MODE (op))
5807     return 0;
5808   return GET_RTX_CLASS (GET_CODE (op)) == '<';
5809 }
5810
5811 int
5812 boolean_operator (op, mode)
5813     rtx op;
5814     enum machine_mode mode ATTRIBUTE_UNUSED;
5815 {
5816   enum rtx_code code = GET_CODE (op);
5817   return (code == AND || code == IOR || code == XOR);
5818 }
5819
5820 int
5821 boolean_or_operator (op, mode)
5822     rtx op;
5823     enum machine_mode mode ATTRIBUTE_UNUSED;
5824 {
5825   enum rtx_code code = GET_CODE (op);
5826   return (code == IOR || code == XOR);
5827 }
5828
5829 int
5830 min_max_operator (op, mode)
5831     rtx op;
5832     enum machine_mode mode ATTRIBUTE_UNUSED;
5833 {
5834   enum rtx_code code = GET_CODE (op);
5835   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
5836 }
5837 \f
5838 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
5839    mask required to convert the result of a rotate insn into a shift
5840    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
5841
5842 int
5843 includes_lshift_p (shiftop, andop)
5844      rtx shiftop;
5845      rtx andop;
5846 {
5847   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
5848
5849   shift_mask <<= INTVAL (shiftop);
5850
5851   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
5852 }
5853
5854 /* Similar, but for right shift.  */
5855
5856 int
5857 includes_rshift_p (shiftop, andop)
5858      rtx shiftop;
5859      rtx andop;
5860 {
5861   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
5862
5863   shift_mask >>= INTVAL (shiftop);
5864
5865   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
5866 }
5867
5868 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
5869    to perform a left shift.  It must have exactly SHIFTOP least
5870    signifigant 0's, then one or more 1's, then zero or more 0's.  */
5871
5872 int
5873 includes_rldic_lshift_p (shiftop, andop)
5874      rtx shiftop;
5875      rtx andop;
5876 {
5877   if (GET_CODE (andop) == CONST_INT)
5878     {
5879       HOST_WIDE_INT c, lsb, shift_mask;
5880
5881       c = INTVAL (andop);
5882       if (c == 0 || c == ~0)
5883         return 0;
5884
5885       shift_mask = ~0;
5886       shift_mask <<= INTVAL (shiftop);
5887
5888       /* Find the least signifigant one bit.  */
5889       lsb = c & -c;
5890
5891       /* It must coincide with the LSB of the shift mask.  */
5892       if (-lsb != shift_mask)
5893         return 0;
5894
5895       /* Invert to look for the next transition (if any).  */
5896       c = ~c;
5897
5898       /* Remove the low group of ones (originally low group of zeros).  */
5899       c &= -lsb;
5900
5901       /* Again find the lsb, and check we have all 1's above.  */
5902       lsb = c & -c;
5903       return c == -lsb;
5904     }
5905   else if (GET_CODE (andop) == CONST_DOUBLE
5906            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
5907     {
5908       HOST_WIDE_INT low, high, lsb;
5909       HOST_WIDE_INT shift_mask_low, shift_mask_high;
5910
5911       low = CONST_DOUBLE_LOW (andop);
5912       if (HOST_BITS_PER_WIDE_INT < 64)
5913         high = CONST_DOUBLE_HIGH (andop);
5914
5915       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
5916           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
5917         return 0;
5918
5919       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
5920         {
5921           shift_mask_high = ~0;
5922           if (INTVAL (shiftop) > 32)
5923             shift_mask_high <<= INTVAL (shiftop) - 32;
5924
5925           lsb = high & -high;
5926
5927           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
5928             return 0;
5929
5930           high = ~high;
5931           high &= -lsb;
5932
5933           lsb = high & -high;
5934           return high == -lsb;
5935         }
5936
5937       shift_mask_low = ~0;
5938       shift_mask_low <<= INTVAL (shiftop);
5939
5940       lsb = low & -low;
5941
5942       if (-lsb != shift_mask_low)
5943         return 0;
5944
5945       if (HOST_BITS_PER_WIDE_INT < 64)
5946         high = ~high;
5947       low = ~low;
5948       low &= -lsb;
5949
5950       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
5951         {
5952           lsb = high & -high;
5953           return high == -lsb;
5954         }
5955
5956       lsb = low & -low;
5957       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
5958     }
5959   else
5960     return 0;
5961 }
5962
5963 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
5964    to perform a left shift.  It must have SHIFTOP or more least
5965    signifigant 0's, with the remainder of the word 1's.  */
5966
5967 int
5968 includes_rldicr_lshift_p (shiftop, andop)
5969      rtx shiftop;
5970      rtx andop;
5971 {
5972   if (GET_CODE (andop) == CONST_INT)
5973     {
5974       HOST_WIDE_INT c, lsb, shift_mask;
5975
5976       shift_mask = ~0;
5977       shift_mask <<= INTVAL (shiftop);
5978       c = INTVAL (andop);
5979
5980       /* Find the least signifigant one bit.  */
5981       lsb = c & -c;
5982
5983       /* It must be covered by the shift mask.
5984          This test also rejects c == 0.  */
5985       if ((lsb & shift_mask) == 0)
5986         return 0;
5987
5988       /* Check we have all 1's above the transition, and reject all 1's.  */
5989       return c == -lsb && lsb != 1;
5990     }
5991   else if (GET_CODE (andop) == CONST_DOUBLE
5992            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
5993     {
5994       HOST_WIDE_INT low, lsb, shift_mask_low;
5995
5996       low = CONST_DOUBLE_LOW (andop);
5997
5998       if (HOST_BITS_PER_WIDE_INT < 64)
5999         {
6000           HOST_WIDE_INT high, shift_mask_high;
6001
6002           high = CONST_DOUBLE_HIGH (andop);
6003
6004           if (low == 0)
6005             {
6006               shift_mask_high = ~0;
6007               if (INTVAL (shiftop) > 32)
6008                 shift_mask_high <<= INTVAL (shiftop) - 32;
6009
6010               lsb = high & -high;
6011
6012               if ((lsb & shift_mask_high) == 0)
6013                 return 0;
6014
6015               return high == -lsb;
6016             }
6017           if (high != ~0)
6018             return 0;
6019         }
6020
6021       shift_mask_low = ~0;
6022       shift_mask_low <<= INTVAL (shiftop);
6023
6024       lsb = low & -low;
6025
6026       if ((lsb & shift_mask_low) == 0)
6027         return 0;
6028
6029       return low == -lsb && lsb != 1;
6030     }
6031   else
6032     return 0;
6033 }
6034
6035 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
6036    for lfq and stfq insns.
6037
6038    Note reg1 and reg2 *must* be hard registers.  To be sure we will
6039    abort if we are passed pseudo registers.  */
6040
6041 int
6042 registers_ok_for_quad_peep (reg1, reg2)
6043      rtx reg1, reg2;
6044 {
6045   /* We might have been passed a SUBREG.  */
6046   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
6047     return 0;
6048
6049   return (REGNO (reg1) == REGNO (reg2) - 1);
6050 }
6051
6052 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
6053    addr1 and addr2 must be in consecutive memory locations
6054    (addr2 == addr1 + 8).  */
6055
6056 int
6057 addrs_ok_for_quad_peep (addr1, addr2)
6058      rtx addr1;
6059      rtx addr2;
6060 {
6061   unsigned int reg1;
6062   int offset1;
6063
6064   /* Extract an offset (if used) from the first addr.  */
6065   if (GET_CODE (addr1) == PLUS)
6066     {
6067       /* If not a REG, return zero.  */
6068       if (GET_CODE (XEXP (addr1, 0)) != REG)
6069         return 0;
6070       else
6071         {
6072           reg1 = REGNO (XEXP (addr1, 0));
6073           /* The offset must be constant!  */
6074           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
6075             return 0;
6076           offset1 = INTVAL (XEXP (addr1, 1));
6077         }
6078     }
6079   else if (GET_CODE (addr1) != REG)
6080     return 0;
6081   else
6082     {
6083       reg1 = REGNO (addr1);
6084       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
6085       offset1 = 0;
6086     }
6087
6088   /* Make sure the second address is a (mem (plus (reg) (const_int))).  */
6089   if (GET_CODE (addr2) != PLUS)
6090     return 0;
6091
6092   if (GET_CODE (XEXP (addr2, 0)) != REG
6093       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
6094     return 0;
6095
6096   if (reg1 != REGNO (XEXP (addr2, 0)))
6097     return 0;
6098
6099   /* The offset for the second addr must be 8 more than the first addr.  */
6100   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
6101     return 0;
6102
6103   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
6104      instructions.  */
6105   return 1;
6106 }
6107 \f
6108 /* Return the register class of a scratch register needed to copy IN into
6109    or out of a register in CLASS in MODE.  If it can be done directly,
6110    NO_REGS is returned.  */
6111
6112 enum reg_class
6113 secondary_reload_class (class, mode, in)
6114      enum reg_class class;
6115      enum machine_mode mode ATTRIBUTE_UNUSED;
6116      rtx in;
6117 {
6118   int regno;
6119
6120   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN && flag_pic))
6121     {
6122       /* We cannot copy a symbolic operand directly into anything
6123          other than BASE_REGS for TARGET_ELF.  So indicate that a
6124          register from BASE_REGS is needed as an intermediate
6125          register.
6126          
6127          On Darwin, pic addresses require a load from memory, which
6128          needs a base register.  */
6129       if (class != BASE_REGS
6130           && (GET_CODE (in) == SYMBOL_REF
6131               || GET_CODE (in) == HIGH
6132               || GET_CODE (in) == LABEL_REF
6133               || GET_CODE (in) == CONST))
6134         return BASE_REGS;
6135     }
6136
6137   if (GET_CODE (in) == REG)
6138     {
6139       regno = REGNO (in);
6140       if (regno >= FIRST_PSEUDO_REGISTER)
6141         {
6142           regno = true_regnum (in);
6143           if (regno >= FIRST_PSEUDO_REGISTER)
6144             regno = -1;
6145         }
6146     }
6147   else if (GET_CODE (in) == SUBREG)
6148     {
6149       regno = true_regnum (in);
6150       if (regno >= FIRST_PSEUDO_REGISTER)
6151         regno = -1;
6152     }
6153   else
6154     regno = -1;
6155
6156   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
6157      into anything.  */
6158   if (class == GENERAL_REGS || class == BASE_REGS
6159       || (regno >= 0 && INT_REGNO_P (regno)))
6160     return NO_REGS;
6161
6162   /* Constants, memory, and FP registers can go into FP registers.  */
6163   if ((regno == -1 || FP_REGNO_P (regno))
6164       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
6165     return NO_REGS;
6166
6167   /* Memory, and AltiVec registers can go into AltiVec registers.  */
6168   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
6169       && class == ALTIVEC_REGS)
6170     return NO_REGS;
6171
6172   /* We can copy among the CR registers.  */
6173   if ((class == CR_REGS || class == CR0_REGS)
6174       && regno >= 0 && CR_REGNO_P (regno))
6175     return NO_REGS;
6176
6177   /* Otherwise, we need GENERAL_REGS.  */
6178   return GENERAL_REGS;
6179 }
6180 \f
6181 /* Given a comparison operation, return the bit number in CCR to test.  We
6182    know this is a valid comparison.  
6183
6184    SCC_P is 1 if this is for an scc.  That means that %D will have been
6185    used instead of %C, so the bits will be in different places.
6186
6187    Return -1 if OP isn't a valid comparison for some reason.  */
6188
6189 int
6190 ccr_bit (op, scc_p)
6191      rtx op;
6192      int scc_p;
6193 {
6194   enum rtx_code code = GET_CODE (op);
6195   enum machine_mode cc_mode;
6196   int cc_regnum;
6197   int base_bit;
6198   rtx reg;
6199
6200   if (GET_RTX_CLASS (code) != '<')
6201     return -1;
6202
6203   reg = XEXP (op, 0);
6204
6205   if (GET_CODE (reg) != REG
6206       || ! CR_REGNO_P (REGNO (reg)))
6207     abort ();
6208
6209   cc_mode = GET_MODE (reg);
6210   cc_regnum = REGNO (reg);
6211   base_bit = 4 * (cc_regnum - CR0_REGNO);
6212
6213   validate_condition_mode (code, cc_mode);
6214
6215   switch (code)
6216     {
6217     case NE:
6218       return scc_p ? base_bit + 3 : base_bit + 2;
6219     case EQ:
6220       return base_bit + 2;
6221     case GT:  case GTU:  case UNLE:
6222       return base_bit + 1;
6223     case LT:  case LTU:  case UNGE:
6224       return base_bit;
6225     case ORDERED:  case UNORDERED:
6226       return base_bit + 3;
6227
6228     case GE:  case GEU:
6229       /* If scc, we will have done a cror to put the bit in the
6230          unordered position.  So test that bit.  For integer, this is ! LT
6231          unless this is an scc insn.  */
6232       return scc_p ? base_bit + 3 : base_bit;
6233
6234     case LE:  case LEU:
6235       return scc_p ? base_bit + 3 : base_bit + 1;
6236
6237     default:
6238       abort ();
6239     }
6240 }
6241 \f
6242 /* Return the GOT register.  */
6243
6244 struct rtx_def *
6245 rs6000_got_register (value)
6246      rtx value ATTRIBUTE_UNUSED;
6247 {
6248   /* The second flow pass currently (June 1999) can't update
6249      regs_ever_live without disturbing other parts of the compiler, so
6250      update it here to make the prolog/epilogue code happy.  */
6251   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
6252     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6253
6254   current_function_uses_pic_offset_table = 1;
6255
6256   return pic_offset_table_rtx;
6257 }
6258 \f
6259 /* Function to init struct machine_function.
6260    This will be called, via a pointer variable,
6261    from push_function_context.  */
6262
6263 static struct machine_function *
6264 rs6000_init_machine_status ()
6265 {
6266   return ggc_alloc_cleared (sizeof (machine_function));
6267 }
6268 \f
6269 /* Print an operand.  Recognize special options, documented below.  */
6270
6271 #if TARGET_ELF
6272 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
6273 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
6274 #else
6275 #define SMALL_DATA_RELOC "sda21"
6276 #define SMALL_DATA_REG 0
6277 #endif
6278
6279 void
6280 print_operand (file, x, code)
6281     FILE *file;
6282     rtx x;
6283     int code;
6284 {
6285   int i;
6286   HOST_WIDE_INT val;
6287
6288   /* These macros test for integers and extract the low-order bits.  */
6289 #define INT_P(X)  \
6290 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
6291  && GET_MODE (X) == VOIDmode)
6292
6293 #define INT_LOWPART(X) \
6294   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
6295
6296   switch (code)
6297     {
6298     case '.':
6299       /* Write out an instruction after the call which may be replaced
6300          with glue code by the loader.  This depends on the AIX version.  */
6301       asm_fprintf (file, RS6000_CALL_GLUE);
6302       return;
6303
6304       /* %a is output_address.  */
6305
6306     case 'A':
6307       /* If X is a constant integer whose low-order 5 bits are zero,
6308          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
6309          in the AIX assembler where "sri" with a zero shift count
6310          writes a trash instruction.  */
6311       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
6312         putc ('l', file);
6313       else
6314         putc ('r', file);
6315       return;
6316
6317     case 'b':
6318       /* If constant, low-order 16 bits of constant, unsigned.
6319          Otherwise, write normally.  */
6320       if (INT_P (x))
6321         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
6322       else
6323         print_operand (file, x, 0);
6324       return;
6325
6326     case 'B':
6327       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
6328          for 64-bit mask direction.  */
6329       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
6330       return;
6331
6332       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
6333          output_operand.  */
6334
6335     case 'D':
6336       /* There used to be a comment for 'C' reading "This is an
6337            optional cror needed for certain floating-point
6338            comparisons.  Otherwise write nothing."  */
6339
6340       /* Similar, except that this is for an scc, so we must be able to
6341          encode the test in a single bit that is one.  We do the above
6342          for any LE, GE, GEU, or LEU and invert the bit for NE.  */
6343       if (GET_CODE (x) == LE || GET_CODE (x) == GE
6344           || GET_CODE (x) == LEU || GET_CODE (x) == GEU)
6345         {
6346           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
6347
6348           fprintf (file, "cror %d,%d,%d\n\t", base_bit + 3,
6349                    base_bit + 2,
6350                    base_bit + (GET_CODE (x) == GE || GET_CODE (x) == GEU));
6351         }
6352
6353       else if (GET_CODE (x) == NE)
6354         {
6355           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
6356
6357           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 3,
6358                    base_bit + 2, base_bit + 2);
6359         }
6360       return;
6361
6362     case 'E':
6363       /* X is a CR register.  Print the number of the EQ bit of the CR */
6364       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6365         output_operand_lossage ("invalid %%E value");
6366       else
6367         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
6368       return;
6369
6370     case 'f':
6371       /* X is a CR register.  Print the shift count needed to move it
6372          to the high-order four bits.  */
6373       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6374         output_operand_lossage ("invalid %%f value");
6375       else
6376         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
6377       return;
6378
6379     case 'F':
6380       /* Similar, but print the count for the rotate in the opposite
6381          direction.  */
6382       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6383         output_operand_lossage ("invalid %%F value");
6384       else
6385         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
6386       return;
6387
6388     case 'G':
6389       /* X is a constant integer.  If it is negative, print "m",
6390          otherwise print "z".  This is to make a aze or ame insn.  */
6391       if (GET_CODE (x) != CONST_INT)
6392         output_operand_lossage ("invalid %%G value");
6393       else if (INTVAL (x) >= 0)
6394         putc ('z', file);
6395       else
6396         putc ('m', file);
6397       return;
6398
6399     case 'h':
6400       /* If constant, output low-order five bits.  Otherwise, write
6401          normally.  */
6402       if (INT_P (x))
6403         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
6404       else
6405         print_operand (file, x, 0);
6406       return;
6407
6408     case 'H':
6409       /* If constant, output low-order six bits.  Otherwise, write
6410          normally.  */
6411       if (INT_P (x))
6412         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
6413       else
6414         print_operand (file, x, 0);
6415       return;
6416
6417     case 'I':
6418       /* Print `i' if this is a constant, else nothing.  */
6419       if (INT_P (x))
6420         putc ('i', file);
6421       return;
6422
6423     case 'j':
6424       /* Write the bit number in CCR for jump.  */
6425       i = ccr_bit (x, 0);
6426       if (i == -1)
6427         output_operand_lossage ("invalid %%j code");
6428       else
6429         fprintf (file, "%d", i);
6430       return;
6431
6432     case 'J':
6433       /* Similar, but add one for shift count in rlinm for scc and pass
6434          scc flag to `ccr_bit'.  */
6435       i = ccr_bit (x, 1);
6436       if (i == -1)
6437         output_operand_lossage ("invalid %%J code");
6438       else
6439         /* If we want bit 31, write a shift count of zero, not 32.  */
6440         fprintf (file, "%d", i == 31 ? 0 : i + 1);
6441       return;
6442
6443     case 'k':
6444       /* X must be a constant.  Write the 1's complement of the
6445          constant.  */
6446       if (! INT_P (x))
6447         output_operand_lossage ("invalid %%k value");
6448       else
6449         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
6450       return;
6451
6452     case 'K':
6453       /* X must be a symbolic constant on ELF.  Write an
6454          expression suitable for an 'addi' that adds in the low 16
6455          bits of the MEM.  */
6456       if (GET_CODE (x) != CONST)
6457         {
6458           print_operand_address (file, x);
6459           fputs ("@l", file);
6460         }
6461       else
6462         {
6463           if (GET_CODE (XEXP (x, 0)) != PLUS
6464               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
6465                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
6466               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6467             output_operand_lossage ("invalid %%K value");
6468           print_operand_address (file, XEXP (XEXP (x, 0), 0));
6469           fputs ("@l", file);
6470           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
6471         }
6472       return;
6473
6474       /* %l is output_asm_label.  */
6475
6476     case 'L':
6477       /* Write second word of DImode or DFmode reference.  Works on register
6478          or non-indexed memory only.  */
6479       if (GET_CODE (x) == REG)
6480         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
6481       else if (GET_CODE (x) == MEM)
6482         {
6483           /* Handle possible auto-increment.  Since it is pre-increment and
6484              we have already done it, we can just use an offset of word.  */
6485           if (GET_CODE (XEXP (x, 0)) == PRE_INC
6486               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
6487             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
6488                                            UNITS_PER_WORD));
6489           else
6490             output_address (XEXP (adjust_address_nv (x, SImode,
6491                                                      UNITS_PER_WORD),
6492                                   0));
6493
6494           if (small_data_operand (x, GET_MODE (x)))
6495             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6496                      reg_names[SMALL_DATA_REG]);
6497         }
6498       return;
6499                             
6500     case 'm':
6501       /* MB value for a mask operand.  */
6502       if (! mask_operand (x, SImode))
6503         output_operand_lossage ("invalid %%m value");
6504
6505       val = INT_LOWPART (x);
6506
6507       /* If the high bit is set and the low bit is not, the value is zero.
6508          If the high bit is zero, the value is the first 1 bit we find from
6509          the left.  */
6510       if ((val & 0x80000000) && ((val & 1) == 0))
6511         {
6512           putc ('0', file);
6513           return;
6514         }
6515       else if ((val & 0x80000000) == 0)
6516         {
6517           for (i = 1; i < 32; i++)
6518             if ((val <<= 1) & 0x80000000)
6519               break;
6520           fprintf (file, "%d", i);
6521           return;
6522         }
6523           
6524       /* Otherwise, look for the first 0 bit from the right.  The result is its
6525          number plus 1. We know the low-order bit is one.  */
6526       for (i = 0; i < 32; i++)
6527         if (((val >>= 1) & 1) == 0)
6528           break;
6529
6530       /* If we ended in ...01, i would be 0.  The correct value is 31, so
6531          we want 31 - i.  */
6532       fprintf (file, "%d", 31 - i);
6533       return;
6534
6535     case 'M':
6536       /* ME value for a mask operand.  */
6537       if (! mask_operand (x, SImode))
6538         output_operand_lossage ("invalid %%M value");
6539
6540       val = INT_LOWPART (x);
6541
6542       /* If the low bit is set and the high bit is not, the value is 31.
6543          If the low bit is zero, the value is the first 1 bit we find from
6544          the right.  */
6545       if ((val & 1) && ((val & 0x80000000) == 0))
6546         {
6547           fputs ("31", file);
6548           return;
6549         }
6550       else if ((val & 1) == 0)
6551         {
6552           for (i = 0; i < 32; i++)
6553             if ((val >>= 1) & 1)
6554               break;
6555
6556           /* If we had ....10, i would be 0.  The result should be
6557              30, so we need 30 - i.  */
6558           fprintf (file, "%d", 30 - i);
6559           return;
6560         }
6561           
6562       /* Otherwise, look for the first 0 bit from the left.  The result is its
6563          number minus 1. We know the high-order bit is one.  */
6564       for (i = 0; i < 32; i++)
6565         if (((val <<= 1) & 0x80000000) == 0)
6566           break;
6567
6568       fprintf (file, "%d", i);
6569       return;
6570
6571       /* %n outputs the negative of its operand.  */
6572
6573     case 'N':
6574       /* Write the number of elements in the vector times 4.  */
6575       if (GET_CODE (x) != PARALLEL)
6576         output_operand_lossage ("invalid %%N value");
6577       else
6578         fprintf (file, "%d", XVECLEN (x, 0) * 4);
6579       return;
6580
6581     case 'O':
6582       /* Similar, but subtract 1 first.  */
6583       if (GET_CODE (x) != PARALLEL)
6584         output_operand_lossage ("invalid %%O value");
6585       else
6586         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
6587       return;
6588
6589     case 'p':
6590       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
6591       if (! INT_P (x)
6592           || INT_LOWPART (x) < 0
6593           || (i = exact_log2 (INT_LOWPART (x))) < 0)
6594         output_operand_lossage ("invalid %%p value");
6595       else
6596         fprintf (file, "%d", i);
6597       return;
6598
6599     case 'P':
6600       /* The operand must be an indirect memory reference.  The result
6601          is the register number.  */
6602       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
6603           || REGNO (XEXP (x, 0)) >= 32)
6604         output_operand_lossage ("invalid %%P value");
6605       else
6606         fprintf (file, "%d", REGNO (XEXP (x, 0)));
6607       return;
6608
6609     case 'q':
6610       /* This outputs the logical code corresponding to a boolean
6611          expression.  The expression may have one or both operands
6612          negated (if one, only the first one).  For condition register
6613          logical operations, it will also treat the negated
6614          CR codes as NOTs, but not handle NOTs of them.  */
6615       {
6616         const char *const *t = 0;
6617         const char *s;
6618         enum rtx_code code = GET_CODE (x);
6619         static const char * const tbl[3][3] = {
6620           { "and", "andc", "nor" },
6621           { "or", "orc", "nand" },
6622           { "xor", "eqv", "xor" } };
6623
6624         if (code == AND)
6625           t = tbl[0];
6626         else if (code == IOR)
6627           t = tbl[1];
6628         else if (code == XOR)
6629           t = tbl[2];
6630         else
6631           output_operand_lossage ("invalid %%q value");
6632
6633         if (GET_CODE (XEXP (x, 0)) != NOT)
6634           s = t[0];
6635         else
6636           {
6637             if (GET_CODE (XEXP (x, 1)) == NOT)
6638               s = t[2];
6639             else
6640               s = t[1];
6641           }
6642         
6643         fputs (s, file);
6644       }
6645       return;
6646
6647     case 'R':
6648       /* X is a CR register.  Print the mask for `mtcrf'.  */
6649       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6650         output_operand_lossage ("invalid %%R value");
6651       else
6652         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
6653       return;
6654
6655     case 's':
6656       /* Low 5 bits of 32 - value */
6657       if (! INT_P (x))
6658         output_operand_lossage ("invalid %%s value");
6659       else
6660         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
6661       return;
6662
6663     case 'S':
6664       /* PowerPC64 mask position.  All 0's and all 1's are excluded.
6665          CONST_INT 32-bit mask is considered sign-extended so any
6666          transition must occur within the CONST_INT, not on the boundary.  */
6667       if (! mask64_operand (x, DImode))
6668         output_operand_lossage ("invalid %%S value");
6669
6670       val = INT_LOWPART (x);
6671
6672       if (val & 1)      /* Clear Left */
6673         {
6674           for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
6675             if (!((val >>= 1) & 1))
6676               break;
6677
6678 #if HOST_BITS_PER_WIDE_INT == 32
6679           if (GET_CODE (x) == CONST_DOUBLE && i == 32)
6680             {
6681               val = CONST_DOUBLE_HIGH (x);
6682
6683               if (val == 0)
6684                 --i;
6685               else
6686                 for (i = 32; i < 64; i++)
6687                   if (!((val >>= 1) & 1))
6688                     break;
6689             }
6690 #endif
6691         /* i = index of last set bit from right
6692            mask begins at 63 - i from left */
6693           if (i > 63)
6694             output_operand_lossage ("%%S computed all 1's mask");
6695
6696           fprintf (file, "%d", 63 - i);
6697           return;
6698         }
6699       else      /* Clear Right */
6700         {
6701           for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
6702             if ((val >>= 1) & 1)
6703               break;
6704
6705 #if HOST_BITS_PER_WIDE_INT == 32
6706         if (GET_CODE (x) == CONST_DOUBLE && i == 32)
6707           {
6708             val = CONST_DOUBLE_HIGH (x);
6709
6710             if (val == (HOST_WIDE_INT) -1)
6711               --i;
6712             else
6713               for (i = 32; i < 64; i++)
6714                 if ((val >>= 1) & 1)
6715                   break;
6716           }
6717 #endif
6718         /* i = index of last clear bit from right
6719            mask ends at 62 - i from left */
6720           if (i > 62)
6721             output_operand_lossage ("%%S computed all 0's mask");
6722
6723           fprintf (file, "%d", 62 - i);
6724           return;
6725         }
6726
6727     case 'T':
6728       /* Print the symbolic name of a branch target register.  */
6729       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
6730                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
6731         output_operand_lossage ("invalid %%T value");
6732       else if (REGNO (x) == LINK_REGISTER_REGNUM)
6733         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
6734       else
6735         fputs ("ctr", file);
6736       return;
6737
6738     case 'u':
6739       /* High-order 16 bits of constant for use in unsigned operand.  */
6740       if (! INT_P (x))
6741         output_operand_lossage ("invalid %%u value");
6742       else
6743         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
6744                  (INT_LOWPART (x) >> 16) & 0xffff);
6745       return;
6746
6747     case 'v':
6748       /* High-order 16 bits of constant for use in signed operand.  */
6749       if (! INT_P (x))
6750         output_operand_lossage ("invalid %%v value");
6751       else
6752         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
6753                  (INT_LOWPART (x) >> 16) & 0xffff);
6754       return;
6755
6756     case 'U':
6757       /* Print `u' if this has an auto-increment or auto-decrement.  */
6758       if (GET_CODE (x) == MEM
6759           && (GET_CODE (XEXP (x, 0)) == PRE_INC
6760               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
6761         putc ('u', file);
6762       return;
6763
6764     case 'V':
6765       /* Print the trap code for this operand.  */
6766       switch (GET_CODE (x))
6767         {
6768         case EQ:
6769           fputs ("eq", file);   /* 4 */
6770           break;
6771         case NE:
6772           fputs ("ne", file);   /* 24 */
6773           break;
6774         case LT:
6775           fputs ("lt", file);   /* 16 */
6776           break;
6777         case LE:
6778           fputs ("le", file);   /* 20 */
6779           break;
6780         case GT:
6781           fputs ("gt", file);   /* 8 */
6782           break;
6783         case GE:
6784           fputs ("ge", file);   /* 12 */
6785           break;
6786         case LTU:
6787           fputs ("llt", file);  /* 2 */
6788           break;
6789         case LEU:
6790           fputs ("lle", file);  /* 6 */
6791           break;
6792         case GTU:
6793           fputs ("lgt", file);  /* 1 */
6794           break;
6795         case GEU:
6796           fputs ("lge", file);  /* 5 */
6797           break;
6798         default:
6799           abort ();
6800         }
6801       break;
6802
6803     case 'w':
6804       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
6805          normally.  */
6806       if (INT_P (x))
6807         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
6808                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
6809       else
6810         print_operand (file, x, 0);
6811       return;
6812
6813     case 'W':
6814       /* MB value for a PowerPC64 rldic operand.  */
6815       val = (GET_CODE (x) == CONST_INT
6816              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
6817
6818       if (val < 0)
6819         i = -1;
6820       else
6821         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
6822           if ((val <<= 1) < 0)
6823             break;
6824
6825 #if HOST_BITS_PER_WIDE_INT == 32
6826       if (GET_CODE (x) == CONST_INT && i >= 0)
6827         i += 32;  /* zero-extend high-part was all 0's */
6828       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
6829         {
6830           val = CONST_DOUBLE_LOW (x);
6831
6832           if (val == 0)
6833             abort ();
6834           else if (val < 0)
6835             --i;
6836           else
6837             for ( ; i < 64; i++)
6838               if ((val <<= 1) < 0)
6839                 break;
6840         }
6841 #endif
6842
6843       fprintf (file, "%d", i + 1);
6844       return;
6845
6846     case 'X':
6847       if (GET_CODE (x) == MEM
6848           && LEGITIMATE_INDEXED_ADDRESS_P (XEXP (x, 0), 0))
6849         putc ('x', file);
6850       return;
6851
6852     case 'Y':
6853       /* Like 'L', for third word of TImode  */
6854       if (GET_CODE (x) == REG)
6855         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
6856       else if (GET_CODE (x) == MEM)
6857         {
6858           if (GET_CODE (XEXP (x, 0)) == PRE_INC
6859               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
6860             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
6861           else
6862             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
6863           if (small_data_operand (x, GET_MODE (x)))
6864             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6865                      reg_names[SMALL_DATA_REG]);
6866         }
6867       return;
6868                             
6869     case 'z':
6870       /* X is a SYMBOL_REF.  Write out the name preceded by a
6871          period and without any trailing data in brackets.  Used for function
6872          names.  If we are configured for System V (or the embedded ABI) on
6873          the PowerPC, do not emit the period, since those systems do not use
6874          TOCs and the like.  */
6875       if (GET_CODE (x) != SYMBOL_REF)
6876         abort ();
6877
6878       if (XSTR (x, 0)[0] != '.')
6879         {
6880           switch (DEFAULT_ABI)
6881             {
6882             default:
6883               abort ();
6884
6885             case ABI_AIX:
6886               putc ('.', file);
6887               break;
6888
6889             case ABI_V4:
6890             case ABI_AIX_NODESC:
6891             case ABI_DARWIN:
6892               break;
6893             }
6894         }
6895 #if TARGET_AIX
6896       RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
6897 #else
6898       assemble_name (file, XSTR (x, 0));
6899 #endif
6900       return;
6901
6902     case 'Z':
6903       /* Like 'L', for last word of TImode.  */
6904       if (GET_CODE (x) == REG)
6905         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
6906       else if (GET_CODE (x) == MEM)
6907         {
6908           if (GET_CODE (XEXP (x, 0)) == PRE_INC
6909               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
6910             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
6911           else
6912             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
6913           if (small_data_operand (x, GET_MODE (x)))
6914             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6915                      reg_names[SMALL_DATA_REG]);
6916         }
6917       return;
6918
6919       /* Print AltiVec memory operand.  */
6920     case 'y':
6921       {
6922         rtx tmp;
6923
6924         if (GET_CODE (x) != MEM)
6925           abort ();
6926
6927         tmp = XEXP (x, 0);
6928
6929         if (GET_CODE (tmp) == REG)
6930           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
6931         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
6932           {
6933             if (REGNO (XEXP (tmp, 0)) == 0)
6934               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
6935                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
6936             else
6937               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
6938                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
6939           }
6940         else
6941           abort ();
6942         break;
6943       }
6944                             
6945     case 0:
6946       if (GET_CODE (x) == REG)
6947         fprintf (file, "%s", reg_names[REGNO (x)]);
6948       else if (GET_CODE (x) == MEM)
6949         {
6950           /* We need to handle PRE_INC and PRE_DEC here, since we need to
6951              know the width from the mode.  */
6952           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
6953             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
6954                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
6955           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
6956             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
6957                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
6958           else
6959             output_address (XEXP (x, 0));
6960         }
6961       else
6962         output_addr_const (file, x);
6963       return;
6964
6965     default:
6966       output_operand_lossage ("invalid %%xn code");
6967     }
6968 }
6969 \f
6970 /* Print the address of an operand.  */
6971
6972 void
6973 print_operand_address (file, x)
6974      FILE *file;
6975      rtx x;
6976 {
6977   if (GET_CODE (x) == REG)
6978     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
6979   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
6980            || GET_CODE (x) == LABEL_REF)
6981     {
6982       output_addr_const (file, x);
6983       if (small_data_operand (x, GET_MODE (x)))
6984         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6985                  reg_names[SMALL_DATA_REG]);
6986       else if (TARGET_TOC)
6987         abort ();
6988     }
6989   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
6990     {
6991       if (REGNO (XEXP (x, 0)) == 0)
6992         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
6993                  reg_names[ REGNO (XEXP (x, 0)) ]);
6994       else
6995         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
6996                  reg_names[ REGNO (XEXP (x, 1)) ]);
6997     }
6998   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
6999     {
7000       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (x, 1)));
7001       fprintf (file, "(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
7002     }
7003 #if TARGET_ELF
7004   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
7005            && CONSTANT_P (XEXP (x, 1)))
7006     {
7007       output_addr_const (file, XEXP (x, 1));
7008       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
7009     }
7010 #endif
7011 #if TARGET_MACHO
7012   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
7013            && CONSTANT_P (XEXP (x, 1)))
7014     {
7015       fprintf (file, "lo16(");
7016       output_addr_const (file, XEXP (x, 1));
7017       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
7018     }
7019 #endif
7020   else if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
7021     {
7022       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
7023         {
7024           rtx contains_minus = XEXP (x, 1);
7025           rtx minus, symref;
7026           const char *name;
7027           
7028           /* Find the (minus (sym) (toc)) buried in X, and temporarily
7029              turn it into (sym) for output_addr_const.  */
7030           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
7031             contains_minus = XEXP (contains_minus, 0);
7032
7033           minus = XEXP (contains_minus, 0);
7034           symref = XEXP (minus, 0);
7035           XEXP (contains_minus, 0) = symref;
7036           if (TARGET_ELF)
7037             {
7038               char *newname;
7039
7040               name = XSTR (symref, 0);
7041               newname = alloca (strlen (name) + sizeof ("@toc"));
7042               strcpy (newname, name);
7043               strcat (newname, "@toc");
7044               XSTR (symref, 0) = newname;
7045             }
7046           output_addr_const (file, XEXP (x, 1));
7047           if (TARGET_ELF)
7048             XSTR (symref, 0) = name;
7049           XEXP (contains_minus, 0) = minus;
7050         }
7051       else
7052         output_addr_const (file, XEXP (x, 1));
7053
7054       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
7055     }
7056   else
7057     abort ();
7058 }
7059 \f
7060 /* Target hook for assembling integer objects.  The powerpc version has
7061    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
7062    is defined.  It also needs to handle DI-mode objects on 64-bit
7063    targets.  */
7064
7065 static bool
7066 rs6000_assemble_integer (x, size, aligned_p)
7067      rtx x;
7068      unsigned int size;
7069      int aligned_p;
7070 {
7071 #ifdef RELOCATABLE_NEEDS_FIXUP
7072   /* Special handling for SI values.  */
7073   if (size == 4 && aligned_p)
7074     {
7075       extern int in_toc_section PARAMS ((void));
7076       static int recurse = 0;
7077       
7078       /* For -mrelocatable, we mark all addresses that need to be fixed up
7079          in the .fixup section.  */
7080       if (TARGET_RELOCATABLE
7081           && !in_toc_section ()
7082           && !in_text_section ()
7083           && !recurse
7084           && GET_CODE (x) != CONST_INT
7085           && GET_CODE (x) != CONST_DOUBLE
7086           && CONSTANT_P (x))
7087         {
7088           char buf[256];
7089
7090           recurse = 1;
7091           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
7092           fixuplabelno++;
7093           ASM_OUTPUT_LABEL (asm_out_file, buf);
7094           fprintf (asm_out_file, "\t.long\t(");
7095           output_addr_const (asm_out_file, x);
7096           fprintf (asm_out_file, ")@fixup\n");
7097           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
7098           ASM_OUTPUT_ALIGN (asm_out_file, 2);
7099           fprintf (asm_out_file, "\t.long\t");
7100           assemble_name (asm_out_file, buf);
7101           fprintf (asm_out_file, "\n\t.previous\n");
7102           recurse = 0;
7103           return true;
7104         }
7105       /* Remove initial .'s to turn a -mcall-aixdesc function
7106          address into the address of the descriptor, not the function
7107          itself.  */
7108       else if (GET_CODE (x) == SYMBOL_REF
7109                && XSTR (x, 0)[0] == '.'
7110                && DEFAULT_ABI == ABI_AIX)
7111         {
7112           const char *name = XSTR (x, 0);
7113           while (*name == '.')
7114             name++;
7115
7116           fprintf (asm_out_file, "\t.long\t%s\n", name);
7117           return true;
7118         }
7119     }
7120 #endif /* RELOCATABLE_NEEDS_FIXUP */
7121   return default_assemble_integer (x, size, aligned_p);
7122 }
7123 \f
7124 enum rtx_code
7125 rs6000_reverse_condition (mode, code)
7126      enum machine_mode mode;
7127      enum rtx_code code;
7128 {
7129   /* Reversal of FP compares takes care -- an ordered compare
7130      becomes an unordered compare and vice versa.  */
7131   if (mode == CCFPmode)
7132     return reverse_condition_maybe_unordered (code);
7133   else
7134     return reverse_condition (code);
7135 }
7136
7137 /* Generate a compare for CODE.  Return a brand-new rtx that
7138    represents the result of the compare.  */
7139
7140 static rtx
7141 rs6000_generate_compare (code)
7142      enum rtx_code code;
7143 {
7144   enum machine_mode comp_mode;
7145   rtx compare_result;
7146
7147   if (rs6000_compare_fp_p)
7148     comp_mode = CCFPmode;
7149   else if (code == GTU || code == LTU
7150           || code == GEU || code == LEU)
7151     comp_mode = CCUNSmode;
7152   else
7153     comp_mode = CCmode;
7154
7155   /* First, the compare.  */
7156   compare_result = gen_reg_rtx (comp_mode);
7157   emit_insn (gen_rtx_SET (VOIDmode, compare_result,
7158                           gen_rtx_COMPARE (comp_mode,
7159                                            rs6000_compare_op0, 
7160                                            rs6000_compare_op1)));
7161   
7162   /* Some kinds of FP comparisons need an OR operation;
7163      except for flag_unsafe_math_optimizations we don't bother.  */
7164   if (rs6000_compare_fp_p
7165       && ! flag_unsafe_math_optimizations
7166       && (code == LE || code == GE
7167           || code == UNEQ || code == LTGT
7168           || code == UNGT || code == UNLT))
7169     {
7170       enum rtx_code or1, or2;
7171       rtx or1_rtx, or2_rtx, compare2_rtx;
7172       rtx or_result = gen_reg_rtx (CCEQmode);
7173       
7174       switch (code)
7175         {
7176         case LE: or1 = LT;  or2 = EQ;  break;
7177         case GE: or1 = GT;  or2 = EQ;  break;
7178         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
7179         case LTGT: or1 = LT;  or2 = GT;  break;
7180         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
7181         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
7182         default:  abort ();
7183         }
7184       validate_condition_mode (or1, comp_mode);
7185       validate_condition_mode (or2, comp_mode);
7186       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
7187       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
7188       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
7189                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
7190                                       const_true_rtx);
7191       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
7192
7193       compare_result = or_result;
7194       code = EQ;
7195     }
7196
7197   validate_condition_mode (code, GET_MODE (compare_result));
7198   
7199   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
7200 }
7201
7202
7203 /* Emit the RTL for an sCOND pattern.  */
7204
7205 void
7206 rs6000_emit_sCOND (code, result)
7207      enum rtx_code code;
7208      rtx result;
7209 {
7210   rtx condition_rtx;
7211   enum machine_mode op_mode;
7212
7213   condition_rtx = rs6000_generate_compare (code);
7214
7215   op_mode = GET_MODE (rs6000_compare_op0);
7216   if (op_mode == VOIDmode)
7217     op_mode = GET_MODE (rs6000_compare_op1);
7218
7219   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
7220     {
7221       PUT_MODE (condition_rtx, DImode);
7222       convert_move (result, condition_rtx, 0);
7223     }
7224   else
7225     {
7226       PUT_MODE (condition_rtx, SImode);
7227       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
7228     }
7229 }
7230
7231 /* Emit a branch of kind CODE to location LOC.  */
7232
7233 void
7234 rs6000_emit_cbranch (code, loc)
7235      enum rtx_code code;
7236      rtx loc;
7237 {
7238   rtx condition_rtx, loc_ref;
7239
7240   condition_rtx = rs6000_generate_compare (code);
7241   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
7242   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
7243                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
7244                                                      loc_ref, pc_rtx)));
7245 }
7246
7247 /* Return the string to output a conditional branch to LABEL, which is
7248    the operand number of the label, or -1 if the branch is really a
7249    conditional return.  
7250
7251    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
7252    condition code register and its mode specifies what kind of
7253    comparison we made.
7254
7255    REVERSED is non-zero if we should reverse the sense of the comparison.
7256
7257    INSN is the insn.  */
7258
7259 char *
7260 output_cbranch (op, label, reversed, insn)
7261      rtx op;
7262      const char * label;
7263      int reversed;
7264      rtx insn;
7265 {
7266   static char string[64];
7267   enum rtx_code code = GET_CODE (op);
7268   rtx cc_reg = XEXP (op, 0);
7269   enum machine_mode mode = GET_MODE (cc_reg);
7270   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
7271   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
7272   int really_reversed = reversed ^ need_longbranch;
7273   char *s = string;
7274   const char *ccode;
7275   const char *pred;
7276   rtx note;
7277
7278   validate_condition_mode (code, mode);
7279
7280   /* Work out which way this really branches.  We could use
7281      reverse_condition_maybe_unordered here always but this
7282      makes the resulting assembler clearer.  */
7283   if (really_reversed)
7284     code = rs6000_reverse_condition (mode, code);
7285
7286   switch (code)
7287     {
7288       /* Not all of these are actually distinct opcodes, but
7289          we distinguish them for clarity of the resulting assembler.  */
7290     case NE: case LTGT:
7291       ccode = "ne"; break;
7292     case EQ: case UNEQ:
7293       ccode = "eq"; break;
7294     case GE: case GEU: 
7295       ccode = "ge"; break;
7296     case GT: case GTU: case UNGT: 
7297       ccode = "gt"; break;
7298     case LE: case LEU: 
7299       ccode = "le"; break;
7300     case LT: case LTU: case UNLT: 
7301       ccode = "lt"; break;
7302     case UNORDERED: ccode = "un"; break;
7303     case ORDERED: ccode = "nu"; break;
7304     case UNGE: ccode = "nl"; break;
7305     case UNLE: ccode = "ng"; break;
7306     default:
7307       abort ();
7308     }
7309   
7310   /* Maybe we have a guess as to how likely the branch is.  
7311      The old mnemonics don't have a way to specify this information.  */
7312   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
7313   if (note != NULL_RTX)
7314     {
7315       /* PROB is the difference from 50%.  */
7316       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
7317       
7318       /* For branches that are very close to 50%, assume not-taken.  */
7319       if (abs (prob) > REG_BR_PROB_BASE / 20
7320           && ((prob > 0) ^ need_longbranch))
7321         pred = "+";
7322       else
7323         pred = "-";
7324     }
7325   else
7326     pred = "";
7327
7328   if (label == NULL)
7329     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
7330   else
7331     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
7332
7333   /* We need to escape any '%' characters in the reg_names string.
7334      Assume they'd only be the first character...  */
7335   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
7336     *s++ = '%';
7337   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
7338
7339   if (label != NULL)
7340     {
7341       /* If the branch distance was too far, we may have to use an
7342          unconditional branch to go the distance.  */
7343       if (need_longbranch)
7344         s += sprintf (s, ",$+8\n\tb %s", label);
7345       else
7346         s += sprintf (s, ",%s", label);
7347     }
7348
7349   return string;
7350 }
7351
7352 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
7353    operands of the last comparison is nonzero/true, FALSE_COND if it
7354    is zero/false.  Return 0 if the hardware has no such operation.  */
7355
7356 int
7357 rs6000_emit_cmove (dest, op, true_cond, false_cond)
7358      rtx dest;
7359      rtx op;
7360      rtx true_cond;
7361      rtx false_cond;
7362 {
7363   enum rtx_code code = GET_CODE (op);
7364   rtx op0 = rs6000_compare_op0;
7365   rtx op1 = rs6000_compare_op1;
7366   REAL_VALUE_TYPE c1;
7367   enum machine_mode compare_mode = GET_MODE (op0);
7368   enum machine_mode result_mode = GET_MODE (dest);
7369   rtx temp;
7370
7371   /* These modes should always match. */
7372   if (GET_MODE (op1) != compare_mode)
7373     return 0;
7374   if (GET_MODE (true_cond) != result_mode)
7375     return 0;
7376   if (GET_MODE (false_cond) != result_mode)
7377     return 0;
7378
7379   /* First, work out if the hardware can do this at all, or
7380      if it's too slow...  */
7381   /* If the comparison is an integer one, since we only have fsel
7382      it'll be cheaper to use a branch.  */
7383   if (! rs6000_compare_fp_p)
7384     return 0;
7385
7386   /* Eliminate half of the comparisons by switching operands, this
7387      makes the remaining code simpler.  */
7388   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
7389       || code == LTGT || code == LT)
7390     {
7391       code = reverse_condition_maybe_unordered (code);
7392       temp = true_cond;
7393       true_cond = false_cond;
7394       false_cond = temp;
7395     }
7396
7397   /* UNEQ and LTGT take four instructions for a comparison with zero,
7398      it'll probably be faster to use a branch here too.  */
7399   if (code == UNEQ)
7400     return 0;
7401   
7402   if (GET_CODE (op1) == CONST_DOUBLE)
7403     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
7404     
7405   /* We're going to try to implement comparions by performing
7406      a subtract, then comparing against zero.  Unfortunately,
7407      Inf - Inf is NaN which is not zero, and so if we don't
7408      know that the the operand is finite and the comparison
7409      would treat EQ different to UNORDERED, we can't do it.  */
7410   if (! flag_unsafe_math_optimizations
7411       && code != GT && code != UNGE
7412       && (GET_CODE (op1) != CONST_DOUBLE || target_isinf (c1))
7413       /* Constructs of the form (a OP b ? a : b) are safe.  */
7414       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
7415           || (! rtx_equal_p (op0, true_cond) 
7416               && ! rtx_equal_p (op1, true_cond))))
7417     return 0;
7418   /* At this point we know we can use fsel.  */
7419
7420   /* Reduce the comparison to a comparison against zero.  */
7421   temp = gen_reg_rtx (compare_mode);
7422   emit_insn (gen_rtx_SET (VOIDmode, temp,
7423                           gen_rtx_MINUS (compare_mode, op0, op1)));
7424   op0 = temp;
7425   op1 = CONST0_RTX (compare_mode);
7426
7427   /* If we don't care about NaNs we can reduce some of the comparisons
7428      down to faster ones.  */
7429   if (flag_unsafe_math_optimizations)
7430     switch (code)
7431       {
7432       case GT:
7433         code = LE;
7434         temp = true_cond;
7435         true_cond = false_cond;
7436         false_cond = temp;
7437         break;
7438       case UNGE:
7439         code = GE;
7440         break;
7441       case UNEQ:
7442         code = EQ;
7443         break;
7444       default:
7445         break;
7446       }
7447
7448   /* Now, reduce everything down to a GE.  */
7449   switch (code)
7450     {
7451     case GE:
7452       break;
7453
7454     case LE:
7455       temp = gen_reg_rtx (compare_mode);
7456       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
7457       op0 = temp;
7458       break;
7459
7460     case ORDERED:
7461       temp = gen_reg_rtx (compare_mode);
7462       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
7463       op0 = temp;
7464       break;
7465
7466     case EQ:
7467       temp = gen_reg_rtx (compare_mode);
7468       emit_insn (gen_rtx_SET (VOIDmode, temp, 
7469                               gen_rtx_NEG (compare_mode,
7470                                            gen_rtx_ABS (compare_mode, op0))));
7471       op0 = temp;
7472       break;
7473
7474     case UNGE:
7475       temp = gen_reg_rtx (result_mode);
7476       emit_insn (gen_rtx_SET (VOIDmode, temp,
7477                               gen_rtx_IF_THEN_ELSE (result_mode,
7478                                                     gen_rtx_GE (VOIDmode,
7479                                                                 op0, op1),
7480                                                     true_cond, false_cond)));
7481       false_cond = temp;
7482       true_cond = false_cond;
7483
7484       temp = gen_reg_rtx (compare_mode);
7485       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
7486       op0 = temp;
7487       break;
7488
7489     case GT:
7490       temp = gen_reg_rtx (result_mode);
7491       emit_insn (gen_rtx_SET (VOIDmode, temp,
7492                               gen_rtx_IF_THEN_ELSE (result_mode, 
7493                                                     gen_rtx_GE (VOIDmode,
7494                                                                 op0, op1),
7495                                                     true_cond, false_cond)));
7496       true_cond = temp;
7497       false_cond = true_cond;
7498
7499       temp = gen_reg_rtx (compare_mode);
7500       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
7501       op0 = temp;
7502       break;
7503
7504     default:
7505       abort ();
7506     }
7507
7508   emit_insn (gen_rtx_SET (VOIDmode, dest,
7509                           gen_rtx_IF_THEN_ELSE (result_mode,
7510                                                 gen_rtx_GE (VOIDmode,
7511                                                             op0, op1),
7512                                                 true_cond, false_cond)));
7513   return 1;
7514 }
7515
7516 void
7517 rs6000_emit_minmax (dest, code, op0, op1)
7518      rtx dest;
7519      enum rtx_code code;
7520      rtx op0;
7521      rtx op1;
7522 {
7523   enum machine_mode mode = GET_MODE (op0);
7524   enum rtx_code c;
7525   rtx target;
7526
7527   if (code == SMAX || code == SMIN)
7528     c = GE;
7529   else
7530     c = GEU;
7531
7532   if (code == SMAX || code == UMAX)
7533     target = emit_conditional_move (dest, c, op0, op1, mode, 
7534                                     op0, op1, mode, 0);
7535   else
7536     target = emit_conditional_move (dest, c, op0, op1, mode, 
7537                                     op1, op0, mode, 0);
7538   if (target == NULL_RTX)
7539     abort ();
7540   if (target != dest)
7541     emit_move_insn (dest, target);
7542 }
7543 \f
7544 /* This page contains routines that are used to determine what the
7545    function prologue and epilogue code will do and write them out.  */
7546
7547 /* Return the first fixed-point register that is required to be
7548    saved. 32 if none.  */
7549
7550 int
7551 first_reg_to_save ()
7552 {
7553   int first_reg;
7554
7555   /* Find lowest numbered live register.  */
7556   for (first_reg = 13; first_reg <= 31; first_reg++)
7557     if (regs_ever_live[first_reg] 
7558         && (! call_used_regs[first_reg]
7559             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
7560                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
7561                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
7562       break;
7563
7564   if (current_function_profile)
7565     {
7566       /* AIX must save/restore every register that contains a parameter
7567          before/after the .__mcount call plus an additional register
7568          for the static chain, if needed; use registers from 30 down to 22
7569          to do this.  */
7570       if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN)
7571         {
7572           int last_parm_reg, profile_first_reg;
7573
7574           /* Figure out last used parameter register.  The proper thing
7575              to do is to walk incoming args of the function.  A function
7576              might have live parameter registers even if it has no
7577              incoming args.  */
7578           for (last_parm_reg = 10;
7579                last_parm_reg > 2 && ! regs_ever_live [last_parm_reg];
7580                last_parm_reg--)
7581             ;
7582
7583           /* Calculate first reg for saving parameter registers
7584              and static chain.
7585              Skip reg 31 which may contain the frame pointer.  */
7586           profile_first_reg = (33 - last_parm_reg
7587                                - (current_function_needs_context ? 1 : 0));
7588 #if TARGET_MACHO
7589           /* Need to skip another reg to account for R31 being PICBASE
7590              (when flag_pic is set) or R30 being used as the frame
7591              pointer (when flag_pic is not set).  */
7592           --profile_first_reg;
7593 #endif
7594           /* Do not save frame pointer if no parameters needs to be saved.  */
7595           if (profile_first_reg == 31)
7596             profile_first_reg = 32;
7597
7598           if (first_reg > profile_first_reg)
7599             first_reg = profile_first_reg;
7600         }
7601
7602       /* SVR4 may need one register to preserve the static chain.  */
7603       else if (current_function_needs_context)
7604         {
7605           /* Skip reg 31 which may contain the frame pointer.  */
7606           if (first_reg > 30)
7607             first_reg = 30;
7608         }
7609     }
7610
7611 #if TARGET_MACHO
7612   if (flag_pic && current_function_uses_pic_offset_table &&
7613       (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM))
7614     return RS6000_PIC_OFFSET_TABLE_REGNUM;
7615 #endif
7616
7617   return first_reg;
7618 }
7619
7620 /* Similar, for FP regs.  */
7621
7622 int
7623 first_fp_reg_to_save ()
7624 {
7625   int first_reg;
7626
7627   /* Find lowest numbered live register.  */
7628   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
7629     if (regs_ever_live[first_reg])
7630       break;
7631
7632   return first_reg;
7633 }
7634
7635 /* Similar, for AltiVec regs.  */
7636
7637 static int
7638 first_altivec_reg_to_save ()
7639 {
7640   int i;
7641
7642   /* Stack frame remains as is unless we are in AltiVec ABI.  */
7643   if (! TARGET_ALTIVEC_ABI)
7644     return LAST_ALTIVEC_REGNO + 1;
7645
7646   /* Find lowest numbered live register.  */
7647   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
7648     if (regs_ever_live[i])
7649       break;
7650
7651   return i;
7652 }
7653
7654 /* Return a 32-bit mask of the AltiVec registers we need to set in
7655    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
7656    the 32-bit word is 0.  */
7657
7658 static unsigned int
7659 compute_vrsave_mask ()
7660 {
7661   unsigned int i, mask = 0;
7662
7663   /* First, find out if we use _any_ altivec registers.  */
7664   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7665     if (regs_ever_live[i])
7666       mask |= ALTIVEC_REG_BIT (i);
7667
7668   if (mask == 0)
7669     return mask;
7670
7671   /* Next, add all registers that are call-clobbered.  We do this
7672      because post-reload register optimizers such as regrename_optimize
7673      may choose to use them.  They never change the register class
7674      chosen by reload, so cannot create new uses of altivec registers
7675      if there were none before, so the early exit above is safe.  */
7676   /* ??? Alternately, we could define HARD_REGNO_RENAME_OK to disallow
7677      altivec registers not saved in the mask, which might well make the
7678      adjustments below more effective in eliding the save/restore of
7679      VRSAVE in small functions.  */
7680   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7681     if (call_used_regs[i])
7682       mask |= ALTIVEC_REG_BIT (i);
7683
7684   /* Next, remove the argument registers from the set.  These must
7685      be in the VRSAVE mask set by the caller, so we don't need to add
7686      them in again.  More importantly, the mask we compute here is
7687      used to generate CLOBBERs in the set_vrsave insn, and we do not
7688      wish the argument registers to die.  */
7689   for (i = cfun->args_info.vregno; i >= ALTIVEC_ARG_MIN_REG; --i)
7690     mask &= ~ALTIVEC_REG_BIT (i);
7691
7692   /* Similarly, remove the return value from the set.  */
7693   {
7694     bool yes = false;
7695     diddle_return_value (is_altivec_return_reg, &yes);
7696     if (yes)
7697       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
7698   }
7699
7700   return mask;
7701 }
7702
7703 static void
7704 is_altivec_return_reg (reg, xyes)
7705      rtx reg;
7706      void *xyes;
7707 {
7708   bool *yes = (bool *) xyes;
7709   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
7710     *yes = true;
7711 }
7712
7713 \f
7714 /* Calculate the stack information for the current function.  This is
7715    complicated by having two separate calling sequences, the AIX calling
7716    sequence and the V.4 calling sequence.
7717
7718    AIX (and Darwin/Mac OS X) stack frames look like:
7719                                                           32-bit  64-bit
7720         SP----> +---------------------------------------+
7721                 | back chain to caller                  | 0       0
7722                 +---------------------------------------+
7723                 | saved CR                              | 4       8 (8-11)
7724                 +---------------------------------------+
7725                 | saved LR                              | 8       16
7726                 +---------------------------------------+
7727                 | reserved for compilers                | 12      24
7728                 +---------------------------------------+
7729                 | reserved for binders                  | 16      32
7730                 +---------------------------------------+
7731                 | saved TOC pointer                     | 20      40
7732                 +---------------------------------------+
7733                 | Parameter save area (P)               | 24      48
7734                 +---------------------------------------+
7735                 | Alloca space (A)                      | 24+P    etc.
7736                 +---------------------------------------+
7737                 | Local variable space (L)              | 24+P+A
7738                 +---------------------------------------+
7739                 | Float/int conversion temporary (X)    | 24+P+A+L
7740                 +---------------------------------------+
7741                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
7742                 +---------------------------------------+
7743                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
7744                 +---------------------------------------+
7745                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
7746                 +---------------------------------------+
7747                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
7748                 +---------------------------------------+
7749                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
7750                 +---------------------------------------+
7751         old SP->| back chain to caller's caller         |
7752                 +---------------------------------------+
7753
7754    The required alignment for AIX configurations is two words (i.e., 8
7755    or 16 bytes).
7756
7757
7758    V.4 stack frames look like:
7759
7760         SP----> +---------------------------------------+
7761                 | back chain to caller                  | 0
7762                 +---------------------------------------+
7763                 | caller's saved LR                     | 4
7764                 +---------------------------------------+
7765                 | Parameter save area (P)               | 8
7766                 +---------------------------------------+
7767                 | Alloca space (A)                      | 8+P
7768                 +---------------------------------------+    
7769                 | Varargs save area (V)                 | 8+P+A
7770                 +---------------------------------------+    
7771                 | Local variable space (L)              | 8+P+A+V
7772                 +---------------------------------------+    
7773                 | Float/int conversion temporary (X)    | 8+P+A+V+L
7774                 +---------------------------------------+
7775                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
7776                 +---------------------------------------+
7777                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
7778                 +---------------------------------------+
7779                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
7780                 +---------------------------------------+
7781                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
7782                 +---------------------------------------+    
7783                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
7784                 +---------------------------------------+    
7785                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
7786                 +---------------------------------------+
7787         old SP->| back chain to caller's caller         |
7788                 +---------------------------------------+
7789
7790    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
7791    given.  (But note below and in sysv4.h that we require only 8 and
7792    may round up the size of our stack frame anyways.  The historical
7793    reason is early versions of powerpc-linux which didn't properly
7794    align the stack at program startup.  A happy side-effect is that
7795    -mno-eabi libraries can be used with -meabi programs.)
7796
7797    The EABI configuration defaults to the V.4 layout, unless
7798    -mcall-aix is used, in which case the AIX layout is used.  However,
7799    the stack alignment requirements may differ.  If -mno-eabi is not
7800    given, the required stack alignment is 8 bytes; if -mno-eabi is
7801    given, the required alignment is 16 bytes.  (But see V.4 comment
7802    above.)  */
7803
7804 #ifndef ABI_STACK_BOUNDARY
7805 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
7806 #endif
7807
7808 rs6000_stack_t *
7809 rs6000_stack_info ()
7810 {
7811   static rs6000_stack_t info, zero_info;
7812   rs6000_stack_t *info_ptr = &info;
7813   int reg_size = TARGET_POWERPC64 ? 8 : 4;
7814   enum rs6000_abi abi;
7815   int ehrd_size;
7816   int total_raw_size;
7817
7818   /* Zero all fields portably.  */
7819   info = zero_info;
7820
7821   /* Select which calling sequence.  */
7822   info_ptr->abi = abi = DEFAULT_ABI;
7823
7824   /* Calculate which registers need to be saved & save area size.  */
7825   info_ptr->first_gp_reg_save = first_reg_to_save ();
7826   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
7827      even if it currently looks like we won't.  */
7828   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
7829        || (flag_pic == 1 && abi == ABI_V4)
7830        || (flag_pic && abi == ABI_DARWIN))
7831       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
7832     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
7833   else
7834     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
7835
7836   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
7837   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
7838
7839   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
7840   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
7841                                  - info_ptr->first_altivec_reg_save);
7842
7843   /* Does this function call anything?  */
7844   info_ptr->calls_p = (! current_function_is_leaf
7845                        || cfun->machine->ra_needs_full_frame);
7846
7847   /* Determine if we need to save the link register.  */
7848   if (rs6000_ra_ever_killed ()
7849       || (DEFAULT_ABI == ABI_AIX && current_function_profile)
7850 #ifdef TARGET_RELOCATABLE
7851       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
7852 #endif
7853       || (info_ptr->first_fp_reg_save != 64
7854           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
7855       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
7856       || (abi == ABI_V4 && current_function_calls_alloca)
7857       || (DEFAULT_ABI == ABI_DARWIN
7858           && flag_pic
7859           && current_function_uses_pic_offset_table)
7860       || info_ptr->calls_p)
7861     {
7862       info_ptr->lr_save_p = 1;
7863       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
7864     }
7865
7866   /* Determine if we need to save the condition code registers.  */
7867   if (regs_ever_live[CR2_REGNO] 
7868       || regs_ever_live[CR3_REGNO]
7869       || regs_ever_live[CR4_REGNO])
7870     {
7871       info_ptr->cr_save_p = 1;
7872       if (abi == ABI_V4)
7873         info_ptr->cr_size = reg_size;
7874     }
7875
7876   /* If the current function calls __builtin_eh_return, then we need
7877      to allocate stack space for registers that will hold data for
7878      the exception handler.  */
7879   if (current_function_calls_eh_return)
7880     {
7881       unsigned int i;
7882       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
7883         continue;
7884       ehrd_size = i * UNITS_PER_WORD;
7885     }
7886   else
7887     ehrd_size = 0;
7888
7889   /* Determine various sizes.  */
7890   info_ptr->reg_size     = reg_size;
7891   info_ptr->fixed_size   = RS6000_SAVE_AREA;
7892   info_ptr->varargs_size = RS6000_VARARGS_AREA;
7893   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
7894   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
7895                                          8);
7896
7897   if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE)
7898     {
7899       info_ptr->vrsave_mask = compute_vrsave_mask ();
7900       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
7901     }
7902   else
7903     {
7904       info_ptr->vrsave_mask = 0;
7905       info_ptr->vrsave_size = 0;
7906     }
7907
7908   /* Calculate the offsets.  */
7909   switch (abi)
7910     {
7911     case ABI_NONE:
7912     default:
7913       abort ();
7914
7915     case ABI_AIX:
7916     case ABI_AIX_NODESC:
7917     case ABI_DARWIN:
7918       info_ptr->fp_save_offset   = - info_ptr->fp_size;
7919       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
7920
7921       if (TARGET_ALTIVEC_ABI)
7922         {
7923           info_ptr->vrsave_save_offset
7924             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
7925
7926           /* Align stack so vector save area is on a quadword boundary.  */
7927           if (info_ptr->altivec_size != 0)
7928             info_ptr->altivec_padding_size
7929               = 16 - (-info_ptr->vrsave_save_offset % 16);
7930           else
7931             info_ptr->altivec_padding_size = 0;
7932
7933           info_ptr->altivec_save_offset
7934             = info_ptr->vrsave_save_offset
7935             - info_ptr->altivec_padding_size
7936             - info_ptr->altivec_size;
7937
7938           /* Adjust for AltiVec case.  */
7939           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
7940         }
7941       else
7942         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
7943       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
7944       info_ptr->lr_save_offset   = 2*reg_size;
7945       break;
7946
7947     case ABI_V4:
7948       info_ptr->fp_save_offset   = - info_ptr->fp_size;
7949       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
7950       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
7951
7952       if (TARGET_ALTIVEC_ABI)
7953         {
7954           info_ptr->vrsave_save_offset
7955             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
7956
7957           /* Align stack so vector save area is on a quadword boundary.  */
7958           if (info_ptr->altivec_size != 0)
7959             info_ptr->altivec_padding_size
7960               = 16 - (-info_ptr->vrsave_save_offset % 16);
7961           else
7962             info_ptr->altivec_padding_size = 0;
7963
7964           info_ptr->altivec_save_offset
7965             = info_ptr->vrsave_save_offset
7966             - info_ptr->altivec_padding_size
7967             - info_ptr->altivec_size;
7968
7969           /* Adjust for AltiVec case.  */
7970           info_ptr->toc_save_offset
7971             = info_ptr->altivec_save_offset - info_ptr->toc_size;
7972         }
7973       else
7974         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
7975       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
7976       info_ptr->lr_save_offset   = reg_size;
7977       break;
7978     }
7979
7980   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
7981                                          + info_ptr->gp_size
7982                                          + info_ptr->altivec_size
7983                                          + info_ptr->altivec_padding_size
7984                                          + info_ptr->vrsave_size
7985                                          + ehrd_size
7986                                          + info_ptr->cr_size
7987                                          + info_ptr->lr_size
7988                                          + info_ptr->vrsave_size
7989                                          + info_ptr->toc_size,
7990                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
7991                                          ? 16 : 8);
7992
7993   total_raw_size         = (info_ptr->vars_size
7994                             + info_ptr->parm_size
7995                             + info_ptr->save_size
7996                             + info_ptr->varargs_size
7997                             + info_ptr->fixed_size);
7998
7999   info_ptr->total_size =
8000     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
8001
8002   /* Determine if we need to allocate any stack frame:
8003
8004      For AIX we need to push the stack if a frame pointer is needed
8005      (because the stack might be dynamically adjusted), if we are
8006      debugging, if we make calls, or if the sum of fp_save, gp_save,
8007      and local variables are more than the space needed to save all
8008      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
8009      + 18*8 = 288 (GPR13 reserved).
8010
8011      For V.4 we don't have the stack cushion that AIX uses, but assume
8012      that the debugger can handle stackless frames.  */
8013
8014   if (info_ptr->calls_p)
8015     info_ptr->push_p = 1;
8016
8017   else if (abi == ABI_V4)
8018     info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
8019
8020   else
8021     info_ptr->push_p = (frame_pointer_needed
8022                         || (abi != ABI_DARWIN && write_symbols != NO_DEBUG)
8023                         || ((total_raw_size - info_ptr->fixed_size)
8024                             > (TARGET_32BIT ? 220 : 288)));
8025
8026   /* Zero offsets if we're not saving those registers.  */
8027   if (info_ptr->fp_size == 0)
8028     info_ptr->fp_save_offset = 0;
8029
8030   if (info_ptr->gp_size == 0)
8031     info_ptr->gp_save_offset = 0;
8032
8033   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
8034     info_ptr->altivec_save_offset = 0;
8035
8036   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
8037     info_ptr->vrsave_save_offset = 0;
8038
8039   if (! info_ptr->lr_save_p)
8040     info_ptr->lr_save_offset = 0;
8041
8042   if (! info_ptr->cr_save_p)
8043     info_ptr->cr_save_offset = 0;
8044
8045   if (! info_ptr->toc_save_p)
8046     info_ptr->toc_save_offset = 0;
8047
8048   return info_ptr;
8049 }
8050
8051 void
8052 debug_stack_info (info)
8053      rs6000_stack_t *info;
8054 {
8055   const char *abi_string;
8056
8057   if (! info)
8058     info = rs6000_stack_info ();
8059
8060   fprintf (stderr, "\nStack information for function %s:\n",
8061            ((current_function_decl && DECL_NAME (current_function_decl))
8062             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
8063             : "<unknown>"));
8064
8065   switch (info->abi)
8066     {
8067     default:             abi_string = "Unknown";        break;
8068     case ABI_NONE:       abi_string = "NONE";           break;
8069     case ABI_AIX:
8070     case ABI_AIX_NODESC: abi_string = "AIX";            break;
8071     case ABI_DARWIN:     abi_string = "Darwin";         break;
8072     case ABI_V4:         abi_string = "V.4";            break;
8073     }
8074
8075   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
8076
8077   if (TARGET_ALTIVEC_ABI)
8078     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
8079
8080   if (info->first_gp_reg_save != 32)
8081     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
8082
8083   if (info->first_fp_reg_save != 64)
8084     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
8085
8086   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
8087     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
8088              info->first_altivec_reg_save);
8089
8090   if (info->lr_save_p)
8091     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
8092
8093   if (info->cr_save_p)
8094     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
8095
8096   if (info->toc_save_p)
8097     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
8098
8099   if (info->vrsave_mask)
8100     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
8101
8102   if (info->push_p)
8103     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
8104
8105   if (info->calls_p)
8106     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
8107
8108   if (info->gp_save_offset)
8109     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
8110
8111   if (info->fp_save_offset)
8112     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
8113
8114   if (info->altivec_save_offset)
8115     fprintf (stderr, "\taltivec_save_offset = %5d\n",
8116              info->altivec_save_offset);
8117
8118   if (info->vrsave_save_offset)
8119     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
8120              info->vrsave_save_offset);
8121
8122   if (info->lr_save_offset)
8123     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
8124
8125   if (info->cr_save_offset)
8126     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
8127
8128   if (info->toc_save_offset)
8129     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
8130
8131   if (info->varargs_save_offset)
8132     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
8133
8134   if (info->total_size)
8135     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
8136
8137   if (info->varargs_size)
8138     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
8139
8140   if (info->vars_size)
8141     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
8142
8143   if (info->parm_size)
8144     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
8145
8146   if (info->fixed_size)
8147     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
8148
8149   if (info->gp_size)
8150     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
8151
8152   if (info->fp_size)
8153     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
8154
8155   if (info->altivec_size)
8156     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
8157
8158   if (info->vrsave_size)
8159     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
8160
8161   if (info->altivec_padding_size)
8162     fprintf (stderr, "\taltivec_padding_size= %5d\n",
8163              info->altivec_padding_size);
8164
8165   if (info->lr_size)
8166     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
8167
8168   if (info->cr_size)
8169     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
8170
8171   if (info->toc_size)
8172     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
8173
8174   if (info->save_size)
8175     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
8176
8177   if (info->reg_size != 4)
8178     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
8179
8180   fprintf (stderr, "\n");
8181 }
8182
8183 rtx
8184 rs6000_return_addr (count, frame)
8185      int count;
8186      rtx frame;
8187 {
8188   /* Currently we don't optimize very well between prolog and body
8189      code and for PIC code the code can be actually quite bad, so
8190      don't try to be too clever here.  */
8191   if (count != 0 || flag_pic != 0)
8192     {
8193       cfun->machine->ra_needs_full_frame = 1;
8194
8195       return
8196         gen_rtx_MEM
8197           (Pmode,
8198            memory_address
8199            (Pmode,
8200             plus_constant (copy_to_reg
8201                            (gen_rtx_MEM (Pmode,
8202                                          memory_address (Pmode, frame))),
8203                            RETURN_ADDRESS_OFFSET)));
8204     }
8205
8206   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
8207 }
8208
8209 static int
8210 rs6000_ra_ever_killed ()
8211 {
8212   rtx top;
8213
8214 #ifdef ASM_OUTPUT_MI_THUNK
8215   if (current_function_is_thunk)
8216     return 0;
8217 #endif
8218   if (!has_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM)
8219       || cfun->machine->ra_needs_full_frame)
8220     return regs_ever_live[LINK_REGISTER_REGNUM];
8221
8222   push_topmost_sequence ();
8223   top = get_insns ();
8224   pop_topmost_sequence ();
8225
8226   return reg_set_between_p (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
8227                             top, NULL_RTX);
8228 }
8229 \f
8230 /* Add a REG_MAYBE_DEAD note to the insn.  */
8231 static void
8232 rs6000_maybe_dead (insn)
8233      rtx insn;
8234 {
8235   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
8236                                         const0_rtx,
8237                                         REG_NOTES (insn));
8238 }
8239
8240 /* Emit instructions needed to load the TOC register.
8241    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
8242    a constant pool; or for SVR4 -fpic.  */
8243
8244 void
8245 rs6000_emit_load_toc_table (fromprolog)
8246      int fromprolog;
8247 {
8248   rtx dest;
8249   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
8250
8251   if (TARGET_ELF && DEFAULT_ABI != ABI_AIX)
8252     {
8253       if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8254         {
8255           rtx temp = (fromprolog 
8256                       ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
8257                       : gen_reg_rtx (Pmode));
8258           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_pic_si (temp)));
8259           rs6000_maybe_dead (emit_move_insn (dest, temp));
8260         }
8261       else if (flag_pic == 2)
8262         {
8263           char buf[30];
8264           rtx tempLR = (fromprolog 
8265                         ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
8266                         : gen_reg_rtx (Pmode));
8267           rtx temp0 = (fromprolog
8268                         ? gen_rtx_REG (Pmode, 0)
8269                         : gen_reg_rtx (Pmode));
8270           rtx symF;
8271
8272           /* possibly create the toc section */
8273           if (! toc_initialized)
8274             {
8275               toc_section ();
8276               function_section (current_function_decl);
8277             }
8278   
8279           if (fromprolog)
8280             {
8281               rtx symL;
8282           
8283               ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
8284               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8285
8286               ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
8287               symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8288
8289               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR, 
8290                                                                    symF)));
8291               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
8292               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
8293                                                                    symL,
8294                                                                    symF)));
8295             }
8296           else
8297             {
8298               rtx tocsym;
8299               static int reload_toc_labelno = 0;
8300
8301               tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
8302
8303               ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
8304               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8305
8306               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1b (tempLR, 
8307                                                                     symF, 
8308                                                                     tocsym)));
8309               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
8310               rs6000_maybe_dead (emit_move_insn (temp0, 
8311                                                  gen_rtx_MEM (Pmode, dest)));
8312             }
8313           rs6000_maybe_dead (emit_insn (gen_addsi3 (dest, temp0, dest)));
8314         }
8315       else if (flag_pic == 0 && TARGET_MINIMAL_TOC)
8316         {
8317           /* This is for AIX code running in non-PIC ELF.  */
8318           char buf[30];
8319           rtx realsym;
8320           ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
8321           realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8322           
8323           rs6000_maybe_dead (emit_insn (gen_elf_high (dest, realsym)));
8324           rs6000_maybe_dead (emit_insn (gen_elf_low (dest, dest, realsym)));
8325         }
8326       else
8327         abort ();
8328     }
8329   else
8330     {
8331       if (TARGET_32BIT)
8332         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_si (dest)));
8333       else
8334         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_di (dest)));
8335     }
8336 }
8337
8338 int   
8339 get_TOC_alias_set ()
8340 {
8341     static int set = -1;
8342     if (set == -1)
8343       set = new_alias_set ();
8344     return set;
8345 }   
8346
8347 /* This retuns nonzero if the current function uses the TOC.  This is
8348    determined by the presence of (unspec ... 7), which is generated by
8349    the various load_toc_* patterns.  */
8350
8351 int
8352 uses_TOC () 
8353 {
8354     rtx insn;
8355
8356     for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8357       if (INSN_P (insn))
8358         {
8359           rtx pat = PATTERN (insn);
8360           int i;
8361
8362           if (GET_CODE (pat) == PARALLEL) 
8363             for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
8364               if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == UNSPEC 
8365                  && XINT (XVECEXP (PATTERN (insn), 0, i), 1) == 7)
8366                   return 1;
8367         }
8368     return 0;
8369 }
8370
8371 rtx
8372 create_TOC_reference (symbol) 
8373     rtx symbol;
8374 {
8375   return gen_rtx_PLUS (Pmode, 
8376            gen_rtx_REG (Pmode, TOC_REGISTER),
8377              gen_rtx_CONST (Pmode, 
8378                gen_rtx_MINUS (Pmode, symbol, 
8379                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
8380 }
8381
8382 #if TARGET_AIX
8383 /* __throw will restore its own return address to be the same as the
8384    return address of the function that the throw is being made to.
8385    This is unfortunate, because we want to check the original
8386    return address to see if we need to restore the TOC.
8387    So we have to squirrel it away here.  
8388    This is used only in compiling __throw and __rethrow. 
8389
8390    Most of this code should be removed by CSE.  */
8391 static rtx insn_after_throw;
8392
8393 /* This does the saving...  */
8394 void
8395 rs6000_aix_emit_builtin_unwind_init ()
8396 {
8397   rtx mem;
8398   rtx stack_top = gen_reg_rtx (Pmode);
8399   rtx opcode_addr = gen_reg_rtx (Pmode);
8400
8401   insn_after_throw = gen_reg_rtx (SImode);
8402
8403   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
8404   emit_move_insn (stack_top, mem);
8405
8406   mem = gen_rtx_MEM (Pmode, 
8407                      gen_rtx_PLUS (Pmode, stack_top, 
8408                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
8409   emit_move_insn (opcode_addr, mem);
8410   emit_move_insn (insn_after_throw, gen_rtx_MEM (SImode, opcode_addr));
8411 }
8412
8413 /* Emit insns to _restore_ the TOC register, at runtime (specifically
8414    in _eh.o).  Only used on AIX.
8415
8416    The idea is that on AIX, function calls look like this:
8417         bl  somefunction-trampoline
8418         lwz r2,20(sp)
8419
8420    and later,
8421         somefunction-trampoline:
8422         stw r2,20(sp)
8423          ... load function address in the count register ...
8424         bctr
8425    or like this, if the linker determines that this is not a cross-module call
8426    and so the TOC need not be restored:
8427         bl  somefunction
8428         nop
8429    or like this, if the compiler could determine that this is not a
8430    cross-module call:
8431         bl  somefunction
8432    now, the tricky bit here is that register 2 is saved and restored
8433    by the _linker_, so we can't readily generate debugging information
8434    for it.  So we need to go back up the call chain looking at the
8435    insns at return addresses to see which calls saved the TOC register
8436    and so see where it gets restored from.
8437
8438    Oh, and all this gets done in RTL inside the eh_epilogue pattern,
8439    just before the actual epilogue.
8440
8441    On the bright side, this incurs no space or time overhead unless an
8442    exception is thrown, except for the extra code in libgcc.a.  
8443
8444    The parameter STACKSIZE is a register containing (at runtime)
8445    the amount to be popped off the stack in addition to the stack frame
8446    of this routine (which will be __throw or __rethrow, and so is
8447    guaranteed to have a stack frame).  */
8448
8449 void
8450 rs6000_emit_eh_toc_restore (stacksize)
8451      rtx stacksize;
8452 {
8453   rtx top_of_stack;
8454   rtx bottom_of_stack = gen_reg_rtx (Pmode);
8455   rtx tocompare = gen_reg_rtx (SImode);
8456   rtx opcode = gen_reg_rtx (SImode);
8457   rtx opcode_addr = gen_reg_rtx (Pmode);
8458   rtx mem;
8459   rtx loop_start = gen_label_rtx ();
8460   rtx no_toc_restore_needed = gen_label_rtx ();
8461   rtx loop_exit = gen_label_rtx ();
8462   
8463   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
8464   set_mem_alias_set (mem, rs6000_sr_alias_set);
8465   emit_move_insn (bottom_of_stack, mem);
8466
8467   top_of_stack = expand_binop (Pmode, add_optab, 
8468                                bottom_of_stack, stacksize,
8469                                NULL_RTX, 1, OPTAB_WIDEN);
8470
8471   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014 
8472                                            : 0xE8410028, SImode));
8473
8474   if (insn_after_throw == NULL_RTX)
8475     abort ();
8476   emit_move_insn (opcode, insn_after_throw);
8477   
8478   emit_note (NULL, NOTE_INSN_LOOP_BEG);
8479   emit_label (loop_start);
8480   
8481   do_compare_rtx_and_jump (opcode, tocompare, NE, 1,
8482                            SImode, NULL_RTX, NULL_RTX,
8483                            no_toc_restore_needed);
8484   
8485   mem = gen_rtx_MEM (Pmode, 
8486                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
8487                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
8488   emit_move_insn (gen_rtx_REG (Pmode, 2), mem);
8489
8490   emit_label (no_toc_restore_needed);
8491   do_compare_rtx_and_jump (top_of_stack, bottom_of_stack, EQ, 1,
8492                            Pmode, NULL_RTX, NULL_RTX,
8493                            loop_exit);
8494
8495   mem = gen_rtx_MEM (Pmode, bottom_of_stack);
8496   set_mem_alias_set (mem, rs6000_sr_alias_set);
8497   emit_move_insn (bottom_of_stack, mem);
8498   
8499   mem = gen_rtx_MEM (Pmode, 
8500                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
8501                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
8502   emit_move_insn (opcode_addr, mem);
8503   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
8504
8505   emit_note (NULL, NOTE_INSN_LOOP_CONT);
8506   emit_jump (loop_start);
8507   emit_note (NULL, NOTE_INSN_LOOP_END);
8508   emit_label (loop_exit);
8509 }
8510 #endif /* TARGET_AIX */
8511 \f
8512 /* This ties together stack memory (MEM with an alias set of
8513    rs6000_sr_alias_set) and the change to the stack pointer.  */
8514
8515 static void
8516 rs6000_emit_stack_tie ()
8517 {
8518   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
8519
8520   set_mem_alias_set (mem, rs6000_sr_alias_set);
8521   emit_insn (gen_stack_tie (mem));
8522 }
8523
8524 /* Emit the correct code for allocating stack space, as insns.
8525    If COPY_R12, make sure a copy of the old frame is left in r12.
8526    The generated code may use hard register 0 as a temporary.  */
8527
8528 static void
8529 rs6000_emit_allocate_stack (size, copy_r12)
8530      HOST_WIDE_INT size;
8531      int copy_r12;
8532 {
8533   rtx insn;
8534   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
8535   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
8536   rtx todec = GEN_INT (-size);
8537
8538   if (current_function_limit_stack)
8539     {
8540       if (REG_P (stack_limit_rtx)
8541           && REGNO (stack_limit_rtx) > 1 
8542           && REGNO (stack_limit_rtx) <= 31)
8543         {
8544           emit_insn (Pmode == SImode
8545                      ? gen_addsi3 (tmp_reg,
8546                                    stack_limit_rtx,
8547                                    GEN_INT (size))
8548                      : gen_adddi3 (tmp_reg,
8549                                    stack_limit_rtx,
8550                                    GEN_INT (size)));
8551           
8552           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
8553                                     const0_rtx));
8554         }
8555       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
8556                && TARGET_32BIT
8557                && DEFAULT_ABI == ABI_V4)
8558         {
8559           rtx toload = gen_rtx_CONST (VOIDmode,
8560                                       gen_rtx_PLUS (Pmode, 
8561                                                     stack_limit_rtx, 
8562                                                     GEN_INT (size)));
8563           
8564           emit_insn (gen_elf_high (tmp_reg, toload));
8565           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
8566           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
8567                                     const0_rtx));
8568         }
8569       else
8570         warning ("stack limit expression is not supported");
8571     }
8572
8573   if (copy_r12 || ! TARGET_UPDATE)
8574     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
8575
8576   if (TARGET_UPDATE)
8577     {
8578       if (size > 32767)
8579         {
8580           /* Need a note here so that try_split doesn't get confused.  */
8581           if (get_last_insn() == NULL_RTX)
8582             emit_note (0, NOTE_INSN_DELETED);
8583           insn = emit_move_insn (tmp_reg, todec);
8584           try_split (PATTERN (insn), insn, 0);
8585           todec = tmp_reg;
8586         }
8587       
8588       if (Pmode == SImode)
8589         insn = emit_insn (gen_movsi_update (stack_reg, stack_reg, 
8590                                             todec, stack_reg));
8591       else
8592         insn = emit_insn (gen_movdi_update (stack_reg, stack_reg, 
8593                                             todec, stack_reg));
8594     }
8595   else
8596     {
8597       if (Pmode == SImode)
8598         insn = emit_insn (gen_addsi3 (stack_reg, stack_reg, todec));
8599       else
8600         insn = emit_insn (gen_adddi3 (stack_reg, stack_reg, todec));
8601       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
8602                       gen_rtx_REG (Pmode, 12));
8603     }
8604   
8605   RTX_FRAME_RELATED_P (insn) = 1;
8606   REG_NOTES (insn) = 
8607     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8608                        gen_rtx_SET (VOIDmode, stack_reg, 
8609                                     gen_rtx_PLUS (Pmode, stack_reg,
8610                                                   GEN_INT (-size))),
8611                        REG_NOTES (insn));
8612 }
8613
8614 /* Add a RTX_FRAME_RELATED note so that dwarf2out_frame_debug_expr
8615    knows that:
8616
8617      (mem (plus (blah) (regXX)))
8618
8619    is really:
8620
8621      (mem (plus (blah) (const VALUE_OF_REGXX))).  */
8622
8623 static void
8624 altivec_frame_fixup (insn, reg, val)
8625      rtx insn, reg;
8626      HOST_WIDE_INT val;
8627 {
8628   rtx real;
8629
8630   real = copy_rtx (PATTERN (insn));
8631
8632   real = replace_rtx (real, reg, GEN_INT (val));
8633
8634   RTX_FRAME_RELATED_P (insn) = 1;
8635   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8636                                         real,
8637                                         REG_NOTES (insn));
8638 }
8639
8640 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
8641    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
8642    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
8643    deduce these equivalences by itself so it wasn't necessary to hold
8644    its hand so much.  */
8645
8646 static void
8647 rs6000_frame_related (insn, reg, val, reg2, rreg)
8648      rtx insn;
8649      rtx reg;
8650      HOST_WIDE_INT val;
8651      rtx reg2;
8652      rtx rreg;
8653 {
8654   rtx real, temp;
8655
8656   /* copy_rtx will not make unique copies of registers, so we need to
8657      ensure we don't have unwanted sharing here.  */
8658   if (reg == reg2)
8659     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
8660
8661   if (reg == rreg)
8662     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
8663
8664   real = copy_rtx (PATTERN (insn));
8665
8666   real = replace_rtx (real, reg, 
8667                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
8668                                                         STACK_POINTER_REGNUM),
8669                                     GEN_INT (val)));
8670   
8671   /* We expect that 'real' is either a SET or a PARALLEL containing
8672      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
8673      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
8674
8675   if (GET_CODE (real) == SET)
8676     {
8677       rtx set = real;
8678       
8679       temp = simplify_rtx (SET_SRC (set));
8680       if (temp)
8681         SET_SRC (set) = temp;
8682       temp = simplify_rtx (SET_DEST (set));
8683       if (temp)
8684         SET_DEST (set) = temp;
8685       if (GET_CODE (SET_DEST (set)) == MEM)
8686         {
8687           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
8688           if (temp)
8689             XEXP (SET_DEST (set), 0) = temp;
8690         }
8691     }
8692   else if (GET_CODE (real) == PARALLEL)
8693     {
8694       int i;
8695       for (i = 0; i < XVECLEN (real, 0); i++)
8696         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
8697           {
8698             rtx set = XVECEXP (real, 0, i);
8699             
8700             temp = simplify_rtx (SET_SRC (set));
8701             if (temp)
8702               SET_SRC (set) = temp;
8703             temp = simplify_rtx (SET_DEST (set));
8704             if (temp)
8705               SET_DEST (set) = temp;
8706             if (GET_CODE (SET_DEST (set)) == MEM)
8707               {
8708                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
8709                 if (temp)
8710                   XEXP (SET_DEST (set), 0) = temp;
8711               }
8712             RTX_FRAME_RELATED_P (set) = 1;
8713           }
8714     }
8715   else
8716     abort ();
8717   
8718   if (reg2 != NULL_RTX)
8719     real = replace_rtx (real, reg2, rreg);
8720   
8721   RTX_FRAME_RELATED_P (insn) = 1;
8722   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8723                                         real,
8724                                         REG_NOTES (insn));
8725 }
8726
8727 /* Returns an insn that has a vrsave set operation with the
8728    appropriate CLOBBERs.  */
8729
8730 static rtx
8731 generate_set_vrsave (reg, info, epiloguep)
8732      rtx reg;
8733      rs6000_stack_t *info;
8734      int epiloguep;
8735 {
8736   int nclobs, i;
8737   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
8738   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
8739
8740   clobs[0]
8741     = gen_rtx_SET (VOIDmode,
8742                    vrsave,
8743                    gen_rtx_UNSPEC_VOLATILE (SImode,
8744                                             gen_rtvec (2, reg, vrsave),
8745                                             30));
8746
8747   nclobs = 1;
8748
8749   /* We need to clobber the registers in the mask so the scheduler
8750      does not move sets to VRSAVE before sets of AltiVec registers.
8751
8752      However, if the function receives nonlocal gotos, reload will set
8753      all call saved registers live.  We will end up with:
8754
8755         (set (reg 999) (mem))
8756         (parallel [ (set (reg vrsave) (unspec blah))
8757                     (clobber (reg 999))])
8758
8759      The clobber will cause the store into reg 999 to be dead, and
8760      flow will attempt to delete an epilogue insn.  In this case, we
8761      need an unspec use/set of the register.  */
8762
8763   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8764     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
8765       {
8766         if (!epiloguep || call_used_regs [i])
8767           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
8768                                              gen_rtx_REG (V4SImode, i));
8769         else
8770           {
8771             rtx reg = gen_rtx_REG (V4SImode, i);
8772
8773             clobs[nclobs++]
8774               = gen_rtx_SET (VOIDmode,
8775                              reg,
8776                              gen_rtx_UNSPEC (V4SImode,
8777                                              gen_rtvec (1, reg), 27));
8778           }
8779       }
8780
8781   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
8782
8783   for (i = 0; i < nclobs; ++i)
8784     XVECEXP (insn, 0, i) = clobs[i];
8785
8786   return insn;
8787 }
8788
8789 /* Emit function prologue as insns.  */
8790
8791 void
8792 rs6000_emit_prologue ()
8793 {
8794   rs6000_stack_t *info = rs6000_stack_info ();
8795   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
8796   int reg_size = TARGET_POWERPC64 ? 8 : 4;
8797   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
8798   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
8799   rtx frame_reg_rtx = sp_reg_rtx;
8800   rtx cr_save_rtx = NULL;
8801   rtx insn;
8802   int saving_FPRs_inline;
8803   int using_store_multiple;
8804   HOST_WIDE_INT sp_offset = 0;
8805   
8806   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
8807                           && info->first_gp_reg_save < 31);
8808   saving_FPRs_inline = (info->first_fp_reg_save == 64
8809                         || FP_SAVE_INLINE (info->first_fp_reg_save));
8810
8811   /* For V.4, update stack before we do any saving and set back pointer.  */
8812   if (info->push_p && DEFAULT_ABI == ABI_V4)
8813     {
8814       if (info->total_size < 32767)
8815         sp_offset = info->total_size;
8816       else
8817         frame_reg_rtx = frame_ptr_rtx;
8818       rs6000_emit_allocate_stack (info->total_size, 
8819                                   (frame_reg_rtx != sp_reg_rtx
8820                                    && (info->cr_save_p
8821                                        || info->lr_save_p
8822                                        || info->first_fp_reg_save < 64
8823                                        || info->first_gp_reg_save < 32
8824                                        )));
8825       if (frame_reg_rtx != sp_reg_rtx)
8826         rs6000_emit_stack_tie ();
8827     }
8828
8829   /* Save AltiVec registers if needed.  */
8830   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
8831     {
8832       int i;
8833
8834       /* There should be a non inline version of this, for when we
8835          are saving lots of vector registers.  */
8836       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
8837         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
8838           {
8839             rtx areg, savereg, mem;
8840             int offset;
8841
8842             offset = info->altivec_save_offset + sp_offset
8843               + 16 * (i - info->first_altivec_reg_save);
8844
8845             savereg = gen_rtx_REG (V4SImode, i);
8846
8847             areg = gen_rtx_REG (Pmode, 0);
8848             emit_move_insn (areg, GEN_INT (offset));
8849
8850             /* AltiVec addressing mode is [reg+reg].  */
8851             mem = gen_rtx_MEM (V4SImode,
8852                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
8853                                
8854             set_mem_alias_set (mem, rs6000_sr_alias_set);
8855
8856             insn = emit_move_insn (mem, savereg);
8857
8858             altivec_frame_fixup (insn, areg, offset);
8859           }
8860     }
8861
8862   /* VRSAVE is a bit vector representing which AltiVec registers
8863      are used.  The OS uses this to determine which vector
8864      registers to save on a context switch.  We need to save
8865      VRSAVE on the stack frame, add whatever AltiVec registers we
8866      used in this function, and do the corresponding magic in the
8867      epilogue.  */
8868
8869   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
8870     {
8871       rtx reg, mem, vrsave;
8872       int offset;
8873
8874       /* Get VRSAVE onto a GPR.  */
8875       reg = gen_rtx_REG (SImode, 12);
8876       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
8877       if (TARGET_MACHO)
8878         emit_insn (gen_get_vrsave_internal (reg));
8879       else
8880         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
8881
8882       /* Save VRSAVE.  */
8883       offset = info->vrsave_save_offset + sp_offset;
8884       mem
8885         = gen_rtx_MEM (SImode,
8886                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
8887       set_mem_alias_set (mem, rs6000_sr_alias_set);
8888       insn = emit_move_insn (mem, reg);
8889
8890       /* Include the registers in the mask.  */
8891       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
8892
8893       insn = emit_insn (generate_set_vrsave (reg, info, 0));
8894     }
8895
8896   /* If we use the link register, get it into r0.  */
8897   if (info->lr_save_p)
8898     emit_move_insn (gen_rtx_REG (Pmode, 0),
8899                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
8900
8901   /* If we need to save CR, put it into r12.  */
8902   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
8903     {
8904       cr_save_rtx = gen_rtx_REG (SImode, 12);
8905       emit_insn (gen_movesi_from_cr (cr_save_rtx));
8906     }
8907
8908   /* Do any required saving of fpr's.  If only one or two to save, do
8909      it ourselves.  Otherwise, call function.  */
8910   if (saving_FPRs_inline)
8911     {
8912       int i;
8913       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
8914         if ((regs_ever_live[info->first_fp_reg_save+i] 
8915              && ! call_used_regs[info->first_fp_reg_save+i]))
8916           {
8917             rtx addr, reg, mem;
8918             reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
8919             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8920                                  GEN_INT (info->fp_save_offset 
8921                                           + sp_offset 
8922                                           + 8 * i));
8923             mem = gen_rtx_MEM (DFmode, addr);
8924             set_mem_alias_set (mem, rs6000_sr_alias_set);
8925
8926             insn = emit_move_insn (mem, reg);
8927             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8928                                   NULL_RTX, NULL_RTX);
8929           }
8930     }
8931   else if (info->first_fp_reg_save != 64)
8932     {
8933       int i;
8934       char rname[30];
8935       const char *alloc_rname;
8936       rtvec p;
8937       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
8938       
8939       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
8940                                           gen_rtx_REG (Pmode, 
8941                                                        LINK_REGISTER_REGNUM));
8942       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
8943                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
8944       alloc_rname = ggc_strdup (rname);
8945       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
8946                                       gen_rtx_SYMBOL_REF (Pmode,
8947                                                           alloc_rname));
8948       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
8949         {
8950           rtx addr, reg, mem;
8951           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
8952           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8953                                GEN_INT (info->fp_save_offset 
8954                                         + sp_offset + 8*i));
8955           mem = gen_rtx_MEM (DFmode, addr);
8956           set_mem_alias_set (mem, rs6000_sr_alias_set);
8957
8958           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
8959         }
8960       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
8961       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8962                             NULL_RTX, NULL_RTX);
8963     }
8964
8965   /* Save GPRs.  This is done as a PARALLEL if we are using
8966      the store-multiple instructions.  */
8967   if (using_store_multiple)
8968     {
8969       rtvec p, dwarfp;
8970       int i;
8971       p = rtvec_alloc (32 - info->first_gp_reg_save);
8972       dwarfp = rtvec_alloc (32 - info->first_gp_reg_save);
8973       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
8974         {
8975           rtx addr, reg, mem;
8976           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
8977           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
8978                                GEN_INT (info->gp_save_offset 
8979                                         + sp_offset 
8980                                         + reg_size * i));
8981           mem = gen_rtx_MEM (reg_mode, addr);
8982           set_mem_alias_set (mem, rs6000_sr_alias_set);
8983
8984           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
8985         }
8986       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
8987       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8988                             NULL_RTX, NULL_RTX);
8989     }
8990   else
8991     {
8992       int i;
8993       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
8994         if ((regs_ever_live[info->first_gp_reg_save+i] 
8995              && ! call_used_regs[info->first_gp_reg_save+i])
8996             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
8997                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8998                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
8999           {
9000             rtx addr, reg, mem;
9001             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
9002             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
9003                                  GEN_INT (info->gp_save_offset 
9004                                           + sp_offset 
9005                                           + reg_size * i));
9006             mem = gen_rtx_MEM (reg_mode, addr);
9007             set_mem_alias_set (mem, rs6000_sr_alias_set);
9008
9009             insn = emit_move_insn (mem, reg);
9010             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
9011                                   NULL_RTX, NULL_RTX);
9012           }
9013     }
9014
9015   /* ??? There's no need to emit actual instructions here, but it's the
9016      easiest way to get the frame unwind information emitted.  */
9017   if (current_function_calls_eh_return)
9018     {
9019       unsigned int i, regno;
9020
9021       for (i = 0; ; ++i)
9022         {
9023           rtx addr, reg, mem;
9024
9025           regno = EH_RETURN_DATA_REGNO (i);
9026           if (regno == INVALID_REGNUM)
9027             break;
9028
9029           reg = gen_rtx_REG (reg_mode, regno);
9030           addr = plus_constant (frame_reg_rtx,
9031                                 info->ehrd_offset + sp_offset
9032                                 + reg_size * (int) i);
9033           mem = gen_rtx_MEM (reg_mode, addr);
9034           set_mem_alias_set (mem, rs6000_sr_alias_set);
9035
9036           insn = emit_move_insn (mem, reg);
9037           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
9038                                 NULL_RTX, NULL_RTX);
9039         }
9040     }
9041
9042   /* Save lr if we used it.  */
9043   if (info->lr_save_p)
9044     {
9045       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9046                                GEN_INT (info->lr_save_offset + sp_offset));
9047       rtx reg = gen_rtx_REG (Pmode, 0);
9048       rtx mem = gen_rtx_MEM (Pmode, addr);
9049       /* This should not be of rs6000_sr_alias_set, because of
9050          __builtin_return_address.  */
9051       
9052       insn = emit_move_insn (mem, reg);
9053       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
9054                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
9055     }
9056
9057   /* Save CR if we use any that must be preserved.  */
9058   if (info->cr_save_p)
9059     {
9060       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9061                                GEN_INT (info->cr_save_offset + sp_offset));
9062       rtx mem = gen_rtx_MEM (SImode, addr);
9063
9064       set_mem_alias_set (mem, rs6000_sr_alias_set);
9065
9066       /* If r12 was used to hold the original sp, copy cr into r0 now
9067          that it's free.  */
9068       if (REGNO (frame_reg_rtx) == 12)
9069         {
9070           cr_save_rtx = gen_rtx_REG (SImode, 0);
9071           emit_insn (gen_movesi_from_cr (cr_save_rtx));
9072         }
9073       insn = emit_move_insn (mem, cr_save_rtx);
9074
9075       /* Now, there's no way that dwarf2out_frame_debug_expr is going
9076          to understand '(unspec:SI [(reg:CC 68) ...] 19)'.  But that's
9077          OK.  All we have to do is specify that _one_ condition code
9078          register is saved in this stack slot.  The thrower's epilogue
9079          will then restore all the call-saved registers.
9080          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
9081       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
9082                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
9083     }
9084
9085   /* Update stack and set back pointer unless this is V.4, 
9086      for which it was done previously.  */
9087   if (info->push_p && DEFAULT_ABI != ABI_V4)
9088     rs6000_emit_allocate_stack (info->total_size, FALSE);
9089
9090   /* Set frame pointer, if needed.  */
9091   if (frame_pointer_needed)
9092     {
9093       insn = emit_move_insn (gen_rtx_REG (reg_mode, FRAME_POINTER_REGNUM), 
9094                              sp_reg_rtx);
9095       RTX_FRAME_RELATED_P (insn) = 1;
9096     }
9097
9098   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
9099   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
9100       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
9101           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
9102   {
9103     /* If emit_load_toc_table will use the link register, we need to save
9104        it.  We use R11 for this purpose because emit_load_toc_table
9105        can use register 0.  This allows us to use a plain 'blr' to return
9106        from the procedure more often.  */
9107     int save_LR_around_toc_setup = (TARGET_ELF && flag_pic != 0
9108                                     && ! info->lr_save_p
9109                                     && EXIT_BLOCK_PTR->pred != NULL);
9110     if (save_LR_around_toc_setup)
9111       emit_move_insn (gen_rtx_REG (Pmode, 11), 
9112                       gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
9113     
9114     rs6000_emit_load_toc_table (TRUE);
9115
9116     if (save_LR_around_toc_setup)
9117       emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
9118                       gen_rtx_REG (Pmode, 11));
9119   }
9120
9121   if (DEFAULT_ABI == ABI_DARWIN
9122       && flag_pic && current_function_uses_pic_offset_table)
9123     {
9124       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
9125
9126       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest)));
9127
9128       rs6000_maybe_dead (
9129         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
9130                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
9131     }
9132 }
9133
9134 /* Write function prologue.  */
9135
9136 static void
9137 rs6000_output_function_prologue (file, size)
9138      FILE *file;
9139      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
9140 {
9141   rs6000_stack_t *info = rs6000_stack_info ();
9142
9143   if (TARGET_DEBUG_STACK)
9144     debug_stack_info (info);
9145
9146   /* Write .extern for any function we will call to save and restore
9147      fp values.  */
9148   if (info->first_fp_reg_save < 64
9149       && !FP_SAVE_INLINE (info->first_fp_reg_save))
9150     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
9151              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
9152              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
9153              RESTORE_FP_SUFFIX);
9154
9155   /* Write .extern for AIX common mode routines, if needed.  */
9156   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
9157     {
9158       fputs ("\t.extern __mulh\n", file);
9159       fputs ("\t.extern __mull\n", file);
9160       fputs ("\t.extern __divss\n", file);
9161       fputs ("\t.extern __divus\n", file);
9162       fputs ("\t.extern __quoss\n", file);
9163       fputs ("\t.extern __quous\n", file);
9164       common_mode_defined = 1;
9165     }
9166
9167   if (! HAVE_prologue)
9168     {
9169       start_sequence ();
9170
9171       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
9172          the "toplevel" insn chain.  */
9173       emit_note (0, NOTE_INSN_DELETED);
9174       rs6000_emit_prologue ();
9175       emit_note (0, NOTE_INSN_DELETED);
9176
9177       /* Expand INSN_ADDRESSES so final() doesn't crash. */
9178       {
9179         rtx insn;
9180         unsigned addr = 0;
9181         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
9182           {
9183             INSN_ADDRESSES_NEW (insn, addr);
9184             addr += 4;
9185           }
9186       }
9187
9188       if (TARGET_DEBUG_STACK)
9189         debug_rtx_list (get_insns (), 100);
9190       final (get_insns (), file, FALSE, FALSE);
9191       end_sequence ();
9192     }
9193
9194   rs6000_pic_labelno++;
9195 }
9196   
9197 /* Emit function epilogue as insns.
9198
9199    At present, dwarf2out_frame_debug_expr doesn't understand
9200    register restores, so we don't bother setting RTX_FRAME_RELATED_P
9201    anywhere in the epilogue.  Most of the insns below would in any case
9202    need special notes to explain where r11 is in relation to the stack.  */
9203
9204 void
9205 rs6000_emit_epilogue (sibcall)
9206      int sibcall;
9207 {
9208   rs6000_stack_t *info;
9209   int restoring_FPRs_inline;
9210   int using_load_multiple;
9211   int using_mfcr_multiple;
9212   int use_backchain_to_restore_sp;
9213   int sp_offset = 0;
9214   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
9215   rtx frame_reg_rtx = sp_reg_rtx;
9216   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
9217   int reg_size = TARGET_POWERPC64 ? 8 : 4;
9218   int i;
9219
9220   info = rs6000_stack_info ();
9221   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
9222                          && info->first_gp_reg_save < 31);
9223   restoring_FPRs_inline = (sibcall
9224                            || current_function_calls_eh_return
9225                            || info->first_fp_reg_save == 64
9226                            || FP_SAVE_INLINE (info->first_fp_reg_save));
9227   use_backchain_to_restore_sp = (frame_pointer_needed 
9228                                  || current_function_calls_alloca
9229                                  || info->total_size > 32767);
9230   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
9231                          || rs6000_cpu == PROCESSOR_PPC603
9232                          || rs6000_cpu == PROCESSOR_PPC750
9233                          || optimize_size);
9234
9235   /* If we have a frame pointer, a call to alloca,  or a large stack
9236      frame, restore the old stack pointer using the backchain.  Otherwise,
9237      we know what size to update it with.  */
9238   if (use_backchain_to_restore_sp)
9239     {
9240       /* Under V.4, don't reset the stack pointer until after we're done
9241          loading the saved registers.  */
9242       if (DEFAULT_ABI == ABI_V4)
9243         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
9244
9245       emit_move_insn (frame_reg_rtx,
9246                       gen_rtx_MEM (Pmode, sp_reg_rtx));
9247       
9248     }
9249   else if (info->push_p)
9250     {
9251       if (DEFAULT_ABI == ABI_V4)
9252         sp_offset = info->total_size;
9253       else
9254         {
9255           emit_insn (TARGET_32BIT
9256                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
9257                                    GEN_INT (info->total_size))
9258                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
9259                                    GEN_INT (info->total_size)));
9260         }
9261     }
9262   
9263   /* Restore AltiVec registers if needed.  */
9264   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
9265     {
9266       int i;
9267
9268       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
9269         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
9270           {
9271             rtx addr, areg, mem;
9272
9273             areg = gen_rtx_REG (Pmode, 0);
9274             emit_move_insn
9275               (areg, GEN_INT (info->altivec_save_offset
9276                               + sp_offset
9277                               + 16 * (i - info->first_altivec_reg_save)));
9278
9279             /* AltiVec addressing mode is [reg+reg].  */
9280             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
9281             mem = gen_rtx_MEM (V4SImode, addr);
9282             set_mem_alias_set (mem, rs6000_sr_alias_set);
9283
9284             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
9285           }
9286     }
9287
9288   /* Restore VRSAVE if needed.  */
9289   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
9290     {
9291       rtx addr, mem, reg;
9292
9293       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9294                            GEN_INT (info->vrsave_save_offset + sp_offset));
9295       mem = gen_rtx_MEM (SImode, addr);
9296       set_mem_alias_set (mem, rs6000_sr_alias_set);
9297       reg = gen_rtx_REG (SImode, 12);
9298       emit_move_insn (reg, mem);
9299
9300       emit_insn (generate_set_vrsave (reg, info, 1));
9301     }
9302
9303   /* Get the old lr if we saved it.  */
9304   if (info->lr_save_p)
9305     {
9306       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9307                                GEN_INT (info->lr_save_offset + sp_offset));
9308       rtx mem = gen_rtx_MEM (Pmode, addr);
9309
9310       set_mem_alias_set (mem, rs6000_sr_alias_set);
9311
9312       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
9313     }
9314   
9315   /* Get the old cr if we saved it.  */
9316   if (info->cr_save_p)
9317     {
9318       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9319                                GEN_INT (info->cr_save_offset + sp_offset));
9320       rtx mem = gen_rtx_MEM (SImode, addr);
9321
9322       set_mem_alias_set (mem, rs6000_sr_alias_set);
9323
9324       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
9325     }
9326   
9327   /* Set LR here to try to overlap restores below.  */
9328   if (info->lr_save_p)
9329     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
9330                     gen_rtx_REG (Pmode, 0));
9331   
9332   /* Load exception handler data registers, if needed.  */
9333   if (current_function_calls_eh_return)
9334     {
9335       unsigned int i, regno;
9336
9337       for (i = 0; ; ++i)
9338         {
9339           rtx addr, mem;
9340
9341           regno = EH_RETURN_DATA_REGNO (i);
9342           if (regno == INVALID_REGNUM)
9343             break;
9344
9345           addr = plus_constant (frame_reg_rtx,
9346                                 info->ehrd_offset + sp_offset
9347                                 + reg_size * (int) i);
9348           mem = gen_rtx_MEM (reg_mode, addr);
9349           set_mem_alias_set (mem, rs6000_sr_alias_set);
9350
9351           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
9352         }
9353     }
9354   
9355   /* Restore GPRs.  This is done as a PARALLEL if we are using
9356      the load-multiple instructions.  */
9357   if (using_load_multiple)
9358     {
9359       rtvec p;
9360       p = rtvec_alloc (32 - info->first_gp_reg_save);
9361       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
9362         {
9363           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
9364                                    GEN_INT (info->gp_save_offset 
9365                                             + sp_offset 
9366                                             + reg_size * i));
9367           rtx mem = gen_rtx_MEM (reg_mode, addr);
9368
9369           set_mem_alias_set (mem, rs6000_sr_alias_set);
9370
9371           RTVEC_ELT (p, i) = 
9372             gen_rtx_SET (VOIDmode,
9373                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
9374                          mem);
9375         }
9376       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
9377     }
9378   else
9379     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
9380       if ((regs_ever_live[info->first_gp_reg_save+i] 
9381            && ! call_used_regs[info->first_gp_reg_save+i])
9382           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
9383               && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
9384                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
9385         {
9386           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
9387                                    GEN_INT (info->gp_save_offset 
9388                                             + sp_offset 
9389                                             + reg_size * i));
9390           rtx mem = gen_rtx_MEM (reg_mode, addr);
9391
9392           set_mem_alias_set (mem, rs6000_sr_alias_set);
9393
9394           emit_move_insn (gen_rtx_REG (reg_mode, 
9395                                        info->first_gp_reg_save + i),
9396                           mem);
9397         }
9398
9399   /* Restore fpr's if we need to do it without calling a function.  */
9400   if (restoring_FPRs_inline)
9401     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
9402       if ((regs_ever_live[info->first_fp_reg_save+i] 
9403            && ! call_used_regs[info->first_fp_reg_save+i]))
9404         {
9405           rtx addr, mem;
9406           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9407                                GEN_INT (info->fp_save_offset 
9408                                         + sp_offset 
9409                                         + 8 * i));
9410           mem = gen_rtx_MEM (DFmode, addr);
9411           set_mem_alias_set (mem, rs6000_sr_alias_set);
9412
9413           emit_move_insn (gen_rtx_REG (DFmode, 
9414                                        info->first_fp_reg_save + i),
9415                           mem);
9416         }
9417
9418   /* If we saved cr, restore it here.  Just those that were used.  */
9419   if (info->cr_save_p)
9420     {
9421       rtx r12_rtx = gen_rtx_REG (SImode, 12);
9422       int count = 0;
9423       
9424       if (using_mfcr_multiple)
9425         {
9426           for (i = 0; i < 8; i++)
9427             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
9428               count++;
9429           if (count == 0)
9430             abort ();
9431         }
9432
9433       if (using_mfcr_multiple && count > 1)
9434         {
9435           rtvec p;
9436           int ndx;
9437           
9438           p = rtvec_alloc (count);
9439
9440           ndx = 0;
9441           for (i = 0; i < 8; i++)
9442             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
9443               {
9444                 rtvec r = rtvec_alloc (2);
9445                 RTVEC_ELT (r, 0) = r12_rtx;
9446                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
9447                 RTVEC_ELT (p, ndx) =
9448                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
9449                                gen_rtx_UNSPEC (CCmode, r, 20));
9450                 ndx++;
9451               }
9452           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
9453           if (ndx != count)
9454             abort ();
9455         }
9456       else
9457         for (i = 0; i < 8; i++)
9458           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
9459             {
9460               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
9461                                                            CR0_REGNO+i),
9462                                               r12_rtx));
9463             }
9464     }
9465
9466   /* If this is V.4, unwind the stack pointer after all of the loads
9467      have been done.  We need to emit a block here so that sched
9468      doesn't decide to move the sp change before the register restores
9469      (which may not have any obvious dependency on the stack).  This
9470      doesn't hurt performance, because there is no scheduling that can
9471      be done after this point.  */
9472   if (DEFAULT_ABI == ABI_V4)
9473     {
9474       if (frame_reg_rtx != sp_reg_rtx)
9475           rs6000_emit_stack_tie ();
9476
9477       if (use_backchain_to_restore_sp)
9478         {
9479           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
9480         }
9481       else if (sp_offset != 0)
9482         {
9483           emit_insn (Pmode == SImode
9484                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
9485                                    GEN_INT (sp_offset))
9486                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
9487                                    GEN_INT (sp_offset)));
9488         }
9489     }
9490
9491   if (current_function_calls_eh_return)
9492     {
9493       rtx sa = EH_RETURN_STACKADJ_RTX;
9494       emit_insn (Pmode == SImode
9495                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
9496                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
9497     }
9498
9499   if (!sibcall)
9500     {
9501       rtvec p;
9502       if (! restoring_FPRs_inline)
9503         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
9504       else
9505         p = rtvec_alloc (2);
9506
9507       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
9508       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
9509                                       gen_rtx_REG (Pmode, 
9510                                                    LINK_REGISTER_REGNUM));
9511
9512       /* If we have to restore more than two FP registers, branch to the
9513          restore function.  It will return to our caller.  */
9514       if (! restoring_FPRs_inline)
9515         {
9516           int i;
9517           char rname[30];
9518           const char *alloc_rname;
9519
9520           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
9521                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
9522           alloc_rname = ggc_strdup (rname);
9523           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
9524                                           gen_rtx_SYMBOL_REF (Pmode,
9525                                                               alloc_rname));
9526
9527           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
9528             {
9529               rtx addr, mem;
9530               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
9531                                    GEN_INT (info->fp_save_offset + 8*i));
9532               mem = gen_rtx_MEM (DFmode, addr);
9533               set_mem_alias_set (mem, rs6000_sr_alias_set);
9534
9535               RTVEC_ELT (p, i+3) = 
9536                 gen_rtx_SET (VOIDmode,
9537                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
9538                              mem);
9539             }
9540         }
9541       
9542       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
9543     }
9544 }
9545
9546 /* Write function epilogue.  */
9547
9548 static void
9549 rs6000_output_function_epilogue (file, size)
9550      FILE *file;
9551      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
9552 {
9553   rs6000_stack_t *info = rs6000_stack_info ();
9554   int optional_tbtab = (optimize_size || TARGET_ELF) ? 0 : 1;
9555
9556   if (! HAVE_epilogue)
9557     {
9558       rtx insn = get_last_insn ();
9559       /* If the last insn was a BARRIER, we don't have to write anything except
9560          the trace table.  */
9561       if (GET_CODE (insn) == NOTE)
9562         insn = prev_nonnote_insn (insn);
9563       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
9564         {
9565           /* This is slightly ugly, but at least we don't have two
9566              copies of the epilogue-emitting code.  */
9567           start_sequence ();
9568
9569           /* A NOTE_INSN_DELETED is supposed to be at the start
9570              and end of the "toplevel" insn chain.  */
9571           emit_note (0, NOTE_INSN_DELETED);
9572           rs6000_emit_epilogue (FALSE);
9573           emit_note (0, NOTE_INSN_DELETED);
9574
9575           /* Expand INSN_ADDRESSES so final() doesn't crash. */
9576           {
9577             rtx insn;
9578             unsigned addr = 0;
9579             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
9580               {
9581                 INSN_ADDRESSES_NEW (insn, addr);
9582                 addr += 4;
9583               }
9584           }
9585
9586           if (TARGET_DEBUG_STACK)
9587             debug_rtx_list (get_insns (), 100);
9588           final (get_insns (), file, FALSE, FALSE);
9589           end_sequence ();
9590         }
9591     }
9592
9593   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
9594      on its format.
9595
9596      We don't output a traceback table if -finhibit-size-directive was
9597      used.  The documentation for -finhibit-size-directive reads
9598      ``don't output a @code{.size} assembler directive, or anything
9599      else that would cause trouble if the function is split in the
9600      middle, and the two halves are placed at locations far apart in
9601      memory.''  The traceback table has this property, since it
9602      includes the offset from the start of the function to the
9603      traceback table itself.
9604
9605      System V.4 Powerpc's (and the embedded ABI derived from it) use a
9606      different traceback table.  */
9607   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive)
9608     {
9609       const char *fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9610       const char *language_string = lang_hooks.name;
9611       int fixed_parms = 0, float_parms = 0, parm_info = 0;
9612       int i;
9613
9614       while (*fname == '.')     /* V.4 encodes . in the name */
9615         fname++;
9616
9617       /* Need label immediately before tbtab, so we can compute its offset
9618          from the function start.  */
9619       if (*fname == '*')
9620         ++fname;
9621       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
9622       ASM_OUTPUT_LABEL (file, fname);
9623
9624       /* The .tbtab pseudo-op can only be used for the first eight
9625          expressions, since it can't handle the possibly variable
9626          length fields that follow.  However, if you omit the optional
9627          fields, the assembler outputs zeros for all optional fields
9628          anyways, giving each variable length field is minimum length
9629          (as defined in sys/debug.h).  Thus we can not use the .tbtab
9630          pseudo-op at all.  */
9631
9632       /* An all-zero word flags the start of the tbtab, for debuggers
9633          that have to find it by searching forward from the entry
9634          point or from the current pc.  */
9635       fputs ("\t.long 0\n", file);
9636
9637       /* Tbtab format type.  Use format type 0.  */
9638       fputs ("\t.byte 0,", file);
9639
9640       /* Language type.  Unfortunately, there doesn't seem to be any
9641          official way to get this info, so we use language_string.  C
9642          is 0.  C++ is 9.  No number defined for Obj-C, so use the
9643          value for C for now.  There is no official value for Java,
9644          although IBM appears to be using 13.  There is no official value
9645          for Chill, so we've chosen 44 pseudo-randomly.  */
9646       if (! strcmp (language_string, "GNU C")
9647           || ! strcmp (language_string, "GNU Objective-C"))
9648         i = 0;
9649       else if (! strcmp (language_string, "GNU F77"))
9650         i = 1;
9651       else if (! strcmp (language_string, "GNU Ada"))
9652         i = 3;
9653       else if (! strcmp (language_string, "GNU Pascal"))
9654         i = 2;
9655       else if (! strcmp (language_string, "GNU C++"))
9656         i = 9;
9657       else if (! strcmp (language_string, "GNU Java"))
9658         i = 13;
9659       else if (! strcmp (language_string, "GNU CHILL"))
9660         i = 44;
9661       else
9662         abort ();
9663       fprintf (file, "%d,", i);
9664
9665       /* 8 single bit fields: global linkage (not set for C extern linkage,
9666          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
9667          from start of procedure stored in tbtab, internal function, function
9668          has controlled storage, function has no toc, function uses fp,
9669          function logs/aborts fp operations.  */
9670       /* Assume that fp operations are used if any fp reg must be saved.  */
9671       fprintf (file, "%d,",
9672                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
9673
9674       /* 6 bitfields: function is interrupt handler, name present in
9675          proc table, function calls alloca, on condition directives
9676          (controls stack walks, 3 bits), saves condition reg, saves
9677          link reg.  */
9678       /* The `function calls alloca' bit seems to be set whenever reg 31 is
9679          set up as a frame pointer, even when there is no alloca call.  */
9680       fprintf (file, "%d,",
9681                ((optional_tbtab << 6)
9682                 | ((optional_tbtab & frame_pointer_needed) << 5)
9683                 | (info->cr_save_p << 1)
9684                 | (info->lr_save_p)));
9685
9686       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
9687          (6 bits).  */
9688       fprintf (file, "%d,",
9689                (info->push_p << 7) | (64 - info->first_fp_reg_save));
9690
9691       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
9692       fprintf (file, "%d,", (32 - first_reg_to_save ()));
9693
9694       if (optional_tbtab)
9695         {
9696           /* Compute the parameter info from the function decl argument
9697              list.  */
9698           tree decl;
9699           int next_parm_info_bit = 31;
9700
9701           for (decl = DECL_ARGUMENTS (current_function_decl);
9702                decl; decl = TREE_CHAIN (decl))
9703             {
9704               rtx parameter = DECL_INCOMING_RTL (decl);
9705               enum machine_mode mode = GET_MODE (parameter);
9706
9707               if (GET_CODE (parameter) == REG)
9708                 {
9709                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9710                     {
9711                       int bits;
9712
9713                       float_parms++;
9714
9715                       if (mode == SFmode)
9716                         bits = 0x2;
9717                       else if (mode == DFmode)
9718                         bits = 0x3;
9719                       else
9720                         abort ();
9721
9722                       /* If only one bit will fit, don't or in this entry.  */
9723                       if (next_parm_info_bit > 0)
9724                         parm_info |= (bits << (next_parm_info_bit - 1));
9725                       next_parm_info_bit -= 2;
9726                     }
9727                   else
9728                     {
9729                       fixed_parms += ((GET_MODE_SIZE (mode)
9730                                        + (UNITS_PER_WORD - 1))
9731                                       / UNITS_PER_WORD);
9732                       next_parm_info_bit -= 1;
9733                     }
9734                 }
9735             }
9736         }
9737
9738       /* Number of fixed point parameters.  */
9739       /* This is actually the number of words of fixed point parameters; thus
9740          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
9741       fprintf (file, "%d,", fixed_parms);
9742
9743       /* 2 bitfields: number of floating point parameters (7 bits), parameters
9744          all on stack.  */
9745       /* This is actually the number of fp registers that hold parameters;
9746          and thus the maximum value is 13.  */
9747       /* Set parameters on stack bit if parameters are not in their original
9748          registers, regardless of whether they are on the stack?  Xlc
9749          seems to set the bit when not optimizing.  */
9750       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
9751
9752       if (! optional_tbtab)
9753         return;
9754
9755       /* Optional fields follow.  Some are variable length.  */
9756
9757       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
9758          11 double float.  */
9759       /* There is an entry for each parameter in a register, in the order that
9760          they occur in the parameter list.  Any intervening arguments on the
9761          stack are ignored.  If the list overflows a long (max possible length
9762          34 bits) then completely leave off all elements that don't fit.  */
9763       /* Only emit this long if there was at least one parameter.  */
9764       if (fixed_parms || float_parms)
9765         fprintf (file, "\t.long %d\n", parm_info);
9766
9767       /* Offset from start of code to tb table.  */
9768       fputs ("\t.long ", file);
9769       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
9770 #if TARGET_AIX
9771       RS6000_OUTPUT_BASENAME (file, fname);
9772 #else
9773       assemble_name (file, fname);
9774 #endif
9775       fputs ("-.", file);
9776 #if TARGET_AIX
9777       RS6000_OUTPUT_BASENAME (file, fname);
9778 #else
9779       assemble_name (file, fname);
9780 #endif
9781       putc ('\n', file);
9782
9783       /* Interrupt handler mask.  */
9784       /* Omit this long, since we never set the interrupt handler bit
9785          above.  */
9786
9787       /* Number of CTL (controlled storage) anchors.  */
9788       /* Omit this long, since the has_ctl bit is never set above.  */
9789
9790       /* Displacement into stack of each CTL anchor.  */
9791       /* Omit this list of longs, because there are no CTL anchors.  */
9792
9793       /* Length of function name.  */
9794       fprintf (file, "\t.short %d\n", (int) strlen (fname));
9795
9796       /* Function name.  */
9797       assemble_string (fname, strlen (fname));
9798
9799       /* Register for alloca automatic storage; this is always reg 31.
9800          Only emit this if the alloca bit was set above.  */
9801       if (frame_pointer_needed)
9802         fputs ("\t.byte 31\n", file);
9803
9804       fputs ("\t.align 2\n", file);
9805     }
9806   return;
9807 }
9808 \f
9809 /* A C compound statement that outputs the assembler code for a thunk
9810    function, used to implement C++ virtual function calls with
9811    multiple inheritance.  The thunk acts as a wrapper around a virtual
9812    function, adjusting the implicit object parameter before handing
9813    control off to the real function.
9814
9815    First, emit code to add the integer DELTA to the location that
9816    contains the incoming first argument.  Assume that this argument
9817    contains a pointer, and is the one used to pass the `this' pointer
9818    in C++.  This is the incoming argument *before* the function
9819    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
9820    values of all other incoming arguments.
9821
9822    After the addition, emit code to jump to FUNCTION, which is a
9823    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
9824    not touch the return address.  Hence returning from FUNCTION will
9825    return to whoever called the current `thunk'.
9826
9827    The effect must be as if FUNCTION had been called directly with the
9828    adjusted first argument.  This macro is responsible for emitting
9829    all of the code for a thunk function; output_function_prologue()
9830    and output_function_epilogue() are not invoked.
9831
9832    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
9833    been extracted from it.)  It might possibly be useful on some
9834    targets, but probably not.
9835
9836    If you do not define this macro, the target-independent code in the
9837    C++ frontend will generate a less efficient heavyweight thunk that
9838    calls FUNCTION instead of jumping to it.  The generic approach does
9839    not support varargs.  */
9840
9841 void
9842 output_mi_thunk (file, thunk_fndecl, delta, function)
9843      FILE *file;
9844      tree thunk_fndecl ATTRIBUTE_UNUSED;
9845      int delta;
9846      tree function;
9847 {
9848   const char *this_reg =
9849     reg_names[ aggregate_value_p (TREE_TYPE (TREE_TYPE (function))) ? 4 : 3 ];
9850   const char *prefix;
9851   const char *fname;
9852   const char *r0         = reg_names[0];
9853   const char *toc        = reg_names[2];
9854   const char *schain     = reg_names[11];
9855   const char *r12        = reg_names[12];
9856   char buf[512];
9857   static int labelno = 0;
9858
9859   /* Small constants that can be done by one add instruction.  */
9860   if (delta >= -32768 && delta <= 32767)
9861     {
9862       if (! TARGET_NEW_MNEMONICS)
9863         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta, this_reg);
9864       else
9865         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta);
9866     }
9867
9868   /* Large constants that can be done by one addis instruction.  */
9869   else if ((delta & 0xffff) == 0 && num_insns_constant_wide (delta) == 1)
9870     asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
9871                  delta >> 16);
9872
9873   /* 32-bit constants that can be done by an add and addis instruction.  */
9874   else if (TARGET_32BIT || num_insns_constant_wide (delta) == 1)
9875     {
9876       /* Break into two pieces, propagating the sign bit from the low
9877          word to the upper word.  */
9878       int delta_high = delta >> 16;
9879       int delta_low  = delta & 0xffff;
9880       if ((delta_low & 0x8000) != 0)
9881         {
9882           delta_high++;
9883           delta_low = (delta_low ^ 0x8000) - 0x8000;    /* sign extend */
9884         }
9885
9886       asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
9887                    delta_high);
9888
9889       if (! TARGET_NEW_MNEMONICS)
9890         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta_low, this_reg);
9891       else
9892         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta_low);
9893     }
9894
9895   /* 64-bit constants, fixme */
9896   else
9897     abort ();
9898
9899   /* Get the prefix in front of the names.  */
9900   switch (DEFAULT_ABI)
9901     {
9902     default:
9903       abort ();
9904
9905     case ABI_AIX:
9906       prefix = ".";
9907       break;
9908
9909     case ABI_V4:
9910     case ABI_AIX_NODESC:
9911       prefix = "";
9912       break;
9913     }
9914
9915   /* If the function is compiled in this module, jump to it directly.
9916      Otherwise, load up its address and jump to it.  */
9917
9918   fname = XSTR (XEXP (DECL_RTL (function), 0), 0);
9919
9920   if (current_file_function_operand (XEXP (DECL_RTL (function), 0), VOIDmode)
9921       && (! lookup_attribute ("longcall",
9922                               TYPE_ATTRIBUTES (TREE_TYPE (function)))
9923           || lookup_attribute ("shortcall",
9924                                TYPE_ATTRIBUTES (TREE_TYPE (function)))))
9925
9926     {
9927       fprintf (file, "\tb %s", prefix);
9928       assemble_name (file, fname);
9929       if (DEFAULT_ABI == ABI_V4 && flag_pic) fputs ("@local", file);
9930       putc ('\n', file);
9931     }
9932
9933   else
9934     {
9935       switch (DEFAULT_ABI)
9936         {
9937         default:
9938           abort ();
9939
9940         case ABI_AIX:
9941           /* Set up a TOC entry for the function.  */
9942           ASM_GENERATE_INTERNAL_LABEL (buf, "Lthunk", labelno);
9943           toc_section ();
9944           ASM_OUTPUT_INTERNAL_LABEL (file, "Lthunk", labelno);
9945           labelno++;
9946
9947           if (TARGET_MINIMAL_TOC)
9948             fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
9949           else
9950             {
9951               fputs ("\t.tc ", file);
9952               assemble_name (file, fname);
9953               fputs ("[TC],", file);
9954             }
9955           assemble_name (file, fname);
9956           putc ('\n', file);
9957           text_section ();
9958           if (TARGET_MINIMAL_TOC)
9959             asm_fprintf (file, (TARGET_32BIT)
9960                          ? "\t{l|lwz} %s,%s(%s)\n" : "\tld %s,%s(%s)\n", r12,
9961                          TARGET_ELF ? ".LCTOC0@toc" : ".LCTOC..1", toc);
9962           asm_fprintf (file, (TARGET_32BIT) ? "\t{l|lwz} %s," : "\tld %s,", r12);
9963           assemble_name (file, buf);
9964           if (TARGET_ELF && TARGET_MINIMAL_TOC)
9965             fputs ("-(.LCTOC1)", file);
9966           asm_fprintf (file, "(%s)\n", TARGET_MINIMAL_TOC ? r12 : toc);
9967           asm_fprintf (file,
9968                        (TARGET_32BIT) ? "\t{l|lwz} %s,0(%s)\n" : "\tld %s,0(%s)\n",
9969                        r0, r12);
9970
9971           asm_fprintf (file,
9972                        (TARGET_32BIT) ? "\t{l|lwz} %s,4(%s)\n" : "\tld %s,8(%s)\n",
9973                        toc, r12);
9974
9975           asm_fprintf (file, "\tmtctr %s\n", r0);
9976           asm_fprintf (file,
9977                        (TARGET_32BIT) ? "\t{l|lwz} %s,8(%s)\n" : "\tld %s,16(%s)\n",
9978                        schain, r12);
9979
9980           asm_fprintf (file, "\tbctr\n");
9981           break;
9982
9983         case ABI_AIX_NODESC:
9984         case ABI_V4:
9985           fprintf (file, "\tb %s", prefix);
9986           assemble_name (file, fname);
9987           if (flag_pic) fputs ("@plt", file);
9988           putc ('\n', file);
9989           break;
9990
9991 #if TARGET_MACHO
9992         case ABI_DARWIN:
9993           fprintf (file, "\tb %s", prefix);
9994           if (flag_pic && !machopic_name_defined_p (fname))
9995             assemble_name (file, machopic_stub_name (fname));
9996           else
9997             assemble_name (file, fname);
9998           putc ('\n', file);
9999           break;
10000 #endif
10001         }
10002     }
10003 }
10004
10005 \f
10006 /* A quick summary of the various types of 'constant-pool tables'
10007    under PowerPC:
10008
10009    Target       Flags           Name            One table per   
10010    AIX          (none)          AIX TOC         object file
10011    AIX          -mfull-toc      AIX TOC         object file
10012    AIX          -mminimal-toc   AIX minimal TOC translation unit
10013    SVR4/EABI    (none)          SVR4 SDATA      object file
10014    SVR4/EABI    -fpic           SVR4 pic        object file
10015    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
10016    SVR4/EABI    -mrelocatable   EABI TOC        function
10017    SVR4/EABI    -maix           AIX TOC         object file
10018    SVR4/EABI    -maix -mminimal-toc 
10019                                 AIX minimal TOC translation unit
10020
10021    Name                 Reg.    Set by  entries       contains:
10022                                         made by  addrs? fp?     sum?
10023
10024    AIX TOC              2       crt0    as       Y      option  option
10025    AIX minimal TOC      30      prolog  gcc      Y      Y       option
10026    SVR4 SDATA           13      crt0    gcc      N      Y       N
10027    SVR4 pic             30      prolog  ld       Y      not yet N
10028    SVR4 PIC             30      prolog  gcc      Y      option  option
10029    EABI TOC             30      prolog  gcc      Y      option  option
10030
10031 */
10032
10033 /* Hash table stuff for keeping track of TOC entries.  */
10034
10035 struct toc_hash_struct 
10036 {
10037   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
10038      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
10039   rtx key;
10040   enum machine_mode key_mode;
10041   int labelno;
10042 };
10043
10044 static htab_t toc_hash_table;
10045
10046 /* Hash functions for the hash table.  */
10047
10048 static unsigned
10049 rs6000_hash_constant (k)
10050      rtx k;
10051 {
10052   unsigned result = (GET_CODE (k) << 3) ^ GET_MODE (k);
10053   const char *format = GET_RTX_FORMAT (GET_CODE (k));
10054   int flen = strlen (format);
10055   int fidx;
10056   
10057   if (GET_CODE (k) == LABEL_REF)
10058     return result * 1231 + X0INT (XEXP (k, 0), 3);
10059
10060   if (GET_CODE (k) == CODE_LABEL)
10061     fidx = 3;
10062   else
10063     fidx = 0;
10064
10065   for (; fidx < flen; fidx++)
10066     switch (format[fidx])
10067       {
10068       case 's':
10069         {
10070           unsigned i, len;
10071           const char *str = XSTR (k, fidx);
10072           len = strlen (str);
10073           result = result * 613 + len;
10074           for (i = 0; i < len; i++)
10075             result = result * 613 + (unsigned) str[i];
10076           break;
10077         }
10078       case 'u':
10079       case 'e':
10080         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
10081         break;
10082       case 'i':
10083       case 'n':
10084         result = result * 613 + (unsigned) XINT (k, fidx);
10085         break;
10086       case 'w':
10087         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
10088           result = result * 613 + (unsigned) XWINT (k, fidx);
10089         else
10090           {
10091             size_t i;
10092             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
10093               result = result * 613 + (unsigned) (XWINT (k, fidx)
10094                                                   >> CHAR_BIT * i);
10095           }
10096         break;
10097       default:
10098         abort ();
10099       }
10100   return result;
10101 }
10102
10103 static unsigned
10104 toc_hash_function (hash_entry)
10105      const void * hash_entry;
10106 {
10107   const struct toc_hash_struct *thc = 
10108     (const struct toc_hash_struct *) hash_entry;
10109   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
10110 }
10111
10112 /* Compare H1 and H2 for equivalence.  */
10113
10114 static int
10115 toc_hash_eq (h1, h2)
10116      const void * h1;
10117      const void * h2;
10118 {
10119   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
10120   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
10121
10122   if (((const struct toc_hash_struct *) h1)->key_mode
10123       != ((const struct toc_hash_struct *) h2)->key_mode)
10124     return 0;
10125
10126   return rtx_equal_p (r1, r2);
10127 }
10128
10129 /* Mark the hash table-entry HASH_ENTRY.  */
10130
10131 static int
10132 toc_hash_mark_entry (hash_slot, unused)
10133      void ** hash_slot;
10134      void * unused ATTRIBUTE_UNUSED;
10135 {
10136   const struct toc_hash_struct * hash_entry = 
10137     *(const struct toc_hash_struct **) hash_slot;
10138   rtx r = hash_entry->key;
10139   ggc_set_mark (hash_entry);
10140   /* For CODE_LABELS, we don't want to drag in the whole insn chain...  */
10141   if (GET_CODE (r) == LABEL_REF)
10142     {
10143       ggc_set_mark (r);
10144       ggc_set_mark (XEXP (r, 0));
10145     }
10146   else
10147     ggc_mark_rtx (r);
10148   return 1;
10149 }
10150
10151 /* Mark all the elements of the TOC hash-table *HT.  */
10152
10153 static void
10154 toc_hash_mark_table (vht)
10155      void *vht;
10156 {
10157   htab_t *ht = vht;
10158   
10159   htab_traverse (*ht, toc_hash_mark_entry, (void *)0);
10160 }
10161
10162 /* These are the names given by the C++ front-end to vtables, and
10163    vtable-like objects.  Ideally, this logic should not be here;
10164    instead, there should be some programmatic way of inquiring as
10165    to whether or not an object is a vtable.  */
10166
10167 #define VTABLE_NAME_P(NAME)                             \
10168   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
10169   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
10170   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
10171   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
10172
10173 void
10174 rs6000_output_symbol_ref (file, x)
10175      FILE *file;
10176      rtx x;
10177 {
10178   /* Currently C++ toc references to vtables can be emitted before it
10179      is decided whether the vtable is public or private.  If this is
10180      the case, then the linker will eventually complain that there is
10181      a reference to an unknown section.  Thus, for vtables only, 
10182      we emit the TOC reference to reference the symbol and not the
10183      section.  */
10184   const char *name = XSTR (x, 0);
10185
10186   if (VTABLE_NAME_P (name)) 
10187     {
10188       RS6000_OUTPUT_BASENAME (file, name);
10189     }
10190   else
10191     assemble_name (file, name);
10192 }
10193
10194 /* Output a TOC entry.  We derive the entry name from what is being
10195    written.  */
10196
10197 void
10198 output_toc (file, x, labelno, mode)
10199      FILE *file;
10200      rtx x;
10201      int labelno;
10202      enum machine_mode mode;
10203 {
10204   char buf[256];
10205   const char *name = buf;
10206   const char *real_name;
10207   rtx base = x;
10208   int offset = 0;
10209
10210   if (TARGET_NO_TOC)
10211     abort ();
10212
10213   /* When the linker won't eliminate them, don't output duplicate
10214      TOC entries (this happens on AIX if there is any kind of TOC,
10215      and on SVR4 under -fPIC or -mrelocatable).  */
10216   if (TARGET_TOC)
10217     {
10218       struct toc_hash_struct *h;
10219       void * * found;
10220       
10221       h = ggc_alloc (sizeof (*h));
10222       h->key = x;
10223       h->key_mode = mode;
10224       h->labelno = labelno;
10225       
10226       found = htab_find_slot (toc_hash_table, h, 1);
10227       if (*found == NULL)
10228         *found = h;
10229       else  /* This is indeed a duplicate.  
10230                Set this label equal to that label.  */
10231         {
10232           fputs ("\t.set ", file);
10233           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
10234           fprintf (file, "%d,", labelno);
10235           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
10236           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
10237                                               found)->labelno));
10238           return;
10239         }
10240     }
10241
10242   /* If we're going to put a double constant in the TOC, make sure it's
10243      aligned properly when strict alignment is on.  */
10244   if (GET_CODE (x) == CONST_DOUBLE
10245       && STRICT_ALIGNMENT
10246       && GET_MODE_BITSIZE (mode) >= 64
10247       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
10248     ASM_OUTPUT_ALIGN (file, 3);
10249   }
10250
10251   ASM_OUTPUT_INTERNAL_LABEL (file, "LC", labelno);
10252
10253   /* Handle FP constants specially.  Note that if we have a minimal
10254      TOC, things we put here aren't actually in the TOC, so we can allow
10255      FP constants.  */
10256   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
10257     {
10258       REAL_VALUE_TYPE rv;
10259       long k[2];
10260
10261       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
10262       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
10263
10264       if (TARGET_64BIT)
10265         {
10266           if (TARGET_MINIMAL_TOC)
10267             fputs (DOUBLE_INT_ASM_OP, file);
10268           else
10269             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
10270                      k[0] & 0xffffffff, k[1] & 0xffffffff);
10271           fprintf (file, "0x%lx%08lx\n",
10272                    k[0] & 0xffffffff, k[1] & 0xffffffff);
10273           return;
10274         }
10275       else
10276         {
10277           if (TARGET_MINIMAL_TOC)
10278             fputs ("\t.long ", file);
10279           else
10280             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
10281                      k[0] & 0xffffffff, k[1] & 0xffffffff);
10282           fprintf (file, "0x%lx,0x%lx\n",
10283                    k[0] & 0xffffffff, k[1] & 0xffffffff);
10284           return;
10285         }
10286     }
10287   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
10288     {
10289       REAL_VALUE_TYPE rv;
10290       long l;
10291
10292       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
10293       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
10294
10295       if (TARGET_64BIT)
10296         {
10297           if (TARGET_MINIMAL_TOC)
10298             fputs (DOUBLE_INT_ASM_OP, file);
10299           else
10300             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
10301           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
10302           return;
10303         }
10304       else
10305         {
10306           if (TARGET_MINIMAL_TOC)
10307             fputs ("\t.long ", file);
10308           else
10309             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
10310           fprintf (file, "0x%lx\n", l & 0xffffffff);
10311           return;
10312         }
10313     }
10314   else if (GET_MODE (x) == VOIDmode
10315            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
10316     {
10317       unsigned HOST_WIDE_INT low;
10318       HOST_WIDE_INT high;
10319
10320       if (GET_CODE (x) == CONST_DOUBLE)
10321         {
10322           low = CONST_DOUBLE_LOW (x);
10323           high = CONST_DOUBLE_HIGH (x);
10324         }
10325       else
10326 #if HOST_BITS_PER_WIDE_INT == 32
10327         {
10328           low = INTVAL (x);
10329           high = (low & 0x80000000) ? ~0 : 0;
10330         }
10331 #else
10332         {
10333           low = INTVAL (x) & 0xffffffff;
10334           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
10335         }
10336 #endif
10337
10338       /* TOC entries are always Pmode-sized, but since this
10339          is a bigendian machine then if we're putting smaller
10340          integer constants in the TOC we have to pad them.
10341          (This is still a win over putting the constants in
10342          a separate constant pool, because then we'd have
10343          to have both a TOC entry _and_ the actual constant.)
10344
10345          For a 32-bit target, CONST_INT values are loaded and shifted
10346          entirely within `low' and can be stored in one TOC entry.  */
10347
10348       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
10349         abort ();/* It would be easy to make this work, but it doesn't now.  */
10350
10351       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
10352         lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
10353                        POINTER_SIZE, &low, &high, 0);
10354
10355       if (TARGET_64BIT)
10356         {
10357           if (TARGET_MINIMAL_TOC)
10358             fputs (DOUBLE_INT_ASM_OP, file);
10359           else
10360             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
10361                      (long) high & 0xffffffff, (long) low & 0xffffffff);
10362           fprintf (file, "0x%lx%08lx\n",
10363                    (long) high & 0xffffffff, (long) low & 0xffffffff);
10364           return;
10365         }
10366       else
10367         {
10368           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
10369             {
10370               if (TARGET_MINIMAL_TOC)
10371                 fputs ("\t.long ", file);
10372               else
10373                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
10374                          (long) high & 0xffffffff, (long) low & 0xffffffff);
10375               fprintf (file, "0x%lx,0x%lx\n",
10376                        (long) high & 0xffffffff, (long) low & 0xffffffff);
10377             }
10378           else
10379             {
10380               if (TARGET_MINIMAL_TOC)
10381                 fputs ("\t.long ", file);
10382               else
10383                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
10384               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
10385             }
10386           return;
10387         }
10388     }
10389
10390   if (GET_CODE (x) == CONST)
10391     {
10392       if (GET_CODE (XEXP (x, 0)) != PLUS)
10393         abort ();
10394
10395       base = XEXP (XEXP (x, 0), 0);
10396       offset = INTVAL (XEXP (XEXP (x, 0), 1));
10397     }
10398   
10399   if (GET_CODE (base) == SYMBOL_REF)
10400     name = XSTR (base, 0);
10401   else if (GET_CODE (base) == LABEL_REF)
10402     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
10403   else if (GET_CODE (base) == CODE_LABEL)
10404     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
10405   else
10406     abort ();
10407
10408   real_name = (*targetm.strip_name_encoding) (name);
10409   if (TARGET_MINIMAL_TOC)
10410     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
10411   else
10412     {
10413       fprintf (file, "\t.tc %s", real_name);
10414
10415       if (offset < 0)
10416         fprintf (file, ".N%d", - offset);
10417       else if (offset)
10418         fprintf (file, ".P%d", offset);
10419
10420       fputs ("[TC],", file);
10421     }
10422
10423   /* Currently C++ toc references to vtables can be emitted before it
10424      is decided whether the vtable is public or private.  If this is
10425      the case, then the linker will eventually complain that there is
10426      a TOC reference to an unknown section.  Thus, for vtables only,
10427      we emit the TOC reference to reference the symbol and not the
10428      section.  */
10429   if (VTABLE_NAME_P (name))
10430     {
10431       RS6000_OUTPUT_BASENAME (file, name);
10432       if (offset < 0)
10433         fprintf (file, "%d", offset);
10434       else if (offset > 0)
10435         fprintf (file, "+%d", offset);
10436     }
10437   else
10438     output_addr_const (file, x);
10439   putc ('\n', file);
10440 }
10441 \f
10442 /* Output an assembler pseudo-op to write an ASCII string of N characters
10443    starting at P to FILE.
10444
10445    On the RS/6000, we have to do this using the .byte operation and
10446    write out special characters outside the quoted string.
10447    Also, the assembler is broken; very long strings are truncated,
10448    so we must artificially break them up early.  */
10449
10450 void
10451 output_ascii (file, p, n)
10452      FILE *file;
10453      const char *p;
10454      int n;
10455 {
10456   char c;
10457   int i, count_string;
10458   const char *for_string = "\t.byte \"";
10459   const char *for_decimal = "\t.byte ";
10460   const char *to_close = NULL;
10461
10462   count_string = 0;
10463   for (i = 0; i < n; i++)
10464     {
10465       c = *p++;
10466       if (c >= ' ' && c < 0177)
10467         {
10468           if (for_string)
10469             fputs (for_string, file);
10470           putc (c, file);
10471
10472           /* Write two quotes to get one.  */
10473           if (c == '"')
10474             {
10475               putc (c, file);
10476               ++count_string;
10477             }
10478
10479           for_string = NULL;
10480           for_decimal = "\"\n\t.byte ";
10481           to_close = "\"\n";
10482           ++count_string;
10483
10484           if (count_string >= 512)
10485             {
10486               fputs (to_close, file);
10487
10488               for_string = "\t.byte \"";
10489               for_decimal = "\t.byte ";
10490               to_close = NULL;
10491               count_string = 0;
10492             }
10493         }
10494       else
10495         {
10496           if (for_decimal)
10497             fputs (for_decimal, file);
10498           fprintf (file, "%d", c);
10499
10500           for_string = "\n\t.byte \"";
10501           for_decimal = ", ";
10502           to_close = "\n";
10503           count_string = 0;
10504         }
10505     }
10506
10507   /* Now close the string if we have written one.  Then end the line.  */
10508   if (to_close)
10509     fputs (to_close, file);
10510 }
10511 \f
10512 /* Generate a unique section name for FILENAME for a section type
10513    represented by SECTION_DESC.  Output goes into BUF.
10514
10515    SECTION_DESC can be any string, as long as it is different for each
10516    possible section type.
10517
10518    We name the section in the same manner as xlc.  The name begins with an
10519    underscore followed by the filename (after stripping any leading directory
10520    names) with the last period replaced by the string SECTION_DESC.  If
10521    FILENAME does not contain a period, SECTION_DESC is appended to the end of
10522    the name.  */
10523
10524 void
10525 rs6000_gen_section_name (buf, filename, section_desc)
10526      char **buf;
10527      const char *filename;
10528      const char *section_desc;
10529 {
10530   const char *q, *after_last_slash, *last_period = 0;
10531   char *p;
10532   int len;
10533
10534   after_last_slash = filename;
10535   for (q = filename; *q; q++)
10536     {
10537       if (*q == '/')
10538         after_last_slash = q + 1;
10539       else if (*q == '.')
10540         last_period = q;
10541     }
10542
10543   len = strlen (after_last_slash) + strlen (section_desc) + 2;
10544   *buf = (char *) permalloc (len);
10545
10546   p = *buf;
10547   *p++ = '_';
10548
10549   for (q = after_last_slash; *q; q++)
10550     {
10551       if (q == last_period)
10552         {
10553           strcpy (p, section_desc);
10554           p += strlen (section_desc);
10555         }
10556
10557       else if (ISALNUM (*q))
10558         *p++ = *q;
10559     }
10560
10561   if (last_period == 0)
10562     strcpy (p, section_desc);
10563   else
10564     *p = '\0';
10565 }
10566 \f
10567 /* Emit profile function.  */
10568
10569 void
10570 output_profile_hook (labelno)
10571      int labelno;
10572 {
10573   if (DEFAULT_ABI == ABI_AIX)
10574     {
10575       char buf[30];
10576       const char *label_name;
10577       rtx fun;
10578
10579       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10580       label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
10581       fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
10582
10583       emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
10584                          fun, Pmode);
10585     }
10586   else if (DEFAULT_ABI == ABI_DARWIN)
10587     {
10588       const char *mcount_name = RS6000_MCOUNT;
10589       int caller_addr_regno = LINK_REGISTER_REGNUM;
10590
10591       /* Be conservative and always set this, at least for now.  */
10592       current_function_uses_pic_offset_table = 1;
10593
10594 #if TARGET_MACHO
10595       /* For PIC code, set up a stub and collect the caller's address
10596          from r0, which is where the prologue puts it.  */
10597       if (flag_pic)
10598         {
10599           mcount_name = machopic_stub_name (mcount_name);
10600           if (current_function_uses_pic_offset_table)
10601             caller_addr_regno = 0;
10602         }
10603 #endif
10604       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
10605                          0, VOIDmode, 1,
10606                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
10607     }
10608 }
10609
10610 /* Write function profiler code.  */
10611
10612 void
10613 output_function_profiler (file, labelno)
10614   FILE *file;
10615   int labelno;
10616 {
10617   char buf[100];
10618
10619   ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10620   switch (DEFAULT_ABI)
10621     {
10622     default:
10623       abort ();
10624
10625     case ABI_V4:
10626     case ABI_AIX_NODESC:
10627       fprintf (file, "\tmflr %s\n", reg_names[0]);
10628       if (flag_pic == 1)
10629         {
10630           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
10631           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
10632                        reg_names[0], reg_names[1]);
10633           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
10634           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
10635           assemble_name (file, buf);
10636           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
10637         }
10638       else if (flag_pic > 1)
10639         {
10640           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
10641                        reg_names[0], reg_names[1]);
10642           /* Now, we need to get the address of the label.  */
10643           fputs ("\tbl 1f\n\t.long ", file);
10644           assemble_name (file, buf);
10645           fputs ("-.\n1:", file);
10646           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
10647           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
10648                        reg_names[0], reg_names[11]);
10649           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
10650                        reg_names[0], reg_names[0], reg_names[11]);
10651         }
10652       else
10653         {
10654           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
10655           assemble_name (file, buf);
10656           fputs ("@ha\n", file);
10657           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
10658                        reg_names[0], reg_names[1]);
10659           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
10660           assemble_name (file, buf);
10661           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
10662         }
10663
10664       if (current_function_needs_context)
10665         asm_fprintf (file, "\tmr %s,%s\n",
10666                      reg_names[30], reg_names[STATIC_CHAIN_REGNUM]);
10667       fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
10668       if (current_function_needs_context)
10669         asm_fprintf (file, "\tmr %s,%s\n",
10670                      reg_names[STATIC_CHAIN_REGNUM], reg_names[30]);
10671       break;
10672
10673     case ABI_AIX:
10674     case ABI_DARWIN:
10675       /* Don't do anything, done in output_profile_hook ().  */
10676       break;
10677
10678     }
10679 }
10680
10681 /* Adjust the cost of a scheduling dependency.  Return the new cost of
10682    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
10683
10684 static int
10685 rs6000_adjust_cost (insn, link, dep_insn, cost)
10686      rtx insn;
10687      rtx link;
10688      rtx dep_insn ATTRIBUTE_UNUSED;
10689      int cost;
10690 {
10691   if (! recog_memoized (insn))
10692     return 0;
10693
10694   if (REG_NOTE_KIND (link) != 0)
10695     return 0;
10696
10697   if (REG_NOTE_KIND (link) == 0)
10698     {
10699       /* Data dependency; DEP_INSN writes a register that INSN reads
10700          some cycles later.  */
10701       switch (get_attr_type (insn))
10702         {
10703         case TYPE_JMPREG:
10704           /* Tell the first scheduling pass about the latency between
10705              a mtctr and bctr (and mtlr and br/blr).  The first
10706              scheduling pass will not know about this latency since
10707              the mtctr instruction, which has the latency associated
10708              to it, will be generated by reload.  */
10709           return TARGET_POWER ? 5 : 4;
10710         case TYPE_BRANCH:
10711           /* Leave some extra cycles between a compare and its
10712              dependent branch, to inhibit expensive mispredicts.  */
10713           if ((rs6000_cpu_attr == CPU_PPC603
10714                || rs6000_cpu_attr == CPU_PPC604
10715                || rs6000_cpu_attr == CPU_PPC604E
10716                || rs6000_cpu_attr == CPU_PPC620
10717                || rs6000_cpu_attr == CPU_PPC630
10718                || rs6000_cpu_attr == CPU_PPC750
10719                || rs6000_cpu_attr == CPU_PPC7400
10720                || rs6000_cpu_attr == CPU_PPC7450
10721                || rs6000_cpu_attr == CPU_POWER4)
10722               && recog_memoized (dep_insn)
10723               && (INSN_CODE (dep_insn) >= 0)
10724               && (get_attr_type (dep_insn) == TYPE_COMPARE
10725                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
10726                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
10727                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL))
10728             return cost + 2;
10729         default:
10730           break;
10731         }
10732       /* Fall out to return default cost.  */
10733     }
10734
10735   return cost;
10736 }
10737
10738 /* A C statement (sans semicolon) to update the integer scheduling
10739    priority INSN_PRIORITY (INSN).  Reduce the priority to execute the
10740    INSN earlier, increase the priority to execute INSN later.  Do not
10741    define this macro if you do not need to adjust the scheduling
10742    priorities of insns.  */
10743
10744 static int
10745 rs6000_adjust_priority (insn, priority)
10746      rtx insn ATTRIBUTE_UNUSED;
10747      int priority;
10748 {
10749   /* On machines (like the 750) which have asymmetric integer units,
10750      where one integer unit can do multiply and divides and the other
10751      can't, reduce the priority of multiply/divide so it is scheduled
10752      before other integer operations.  */
10753
10754 #if 0
10755   if (! INSN_P (insn))
10756     return priority;
10757
10758   if (GET_CODE (PATTERN (insn)) == USE)
10759     return priority;
10760
10761   switch (rs6000_cpu_attr) {
10762   case CPU_PPC750:
10763     switch (get_attr_type (insn))
10764       {
10765       default:
10766         break;
10767
10768       case TYPE_IMUL:
10769       case TYPE_IDIV:
10770         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
10771                  priority, priority);
10772         if (priority >= 0 && priority < 0x01000000)
10773           priority >>= 3;
10774         break;
10775       }
10776   }
10777 #endif
10778
10779   return priority;
10780 }
10781
10782 /* Return how many instructions the machine can issue per cycle.  */
10783
10784 static int
10785 rs6000_issue_rate ()
10786 {
10787   switch (rs6000_cpu_attr) {
10788   case CPU_RIOS1:  /* ? */
10789   case CPU_RS64A:
10790   case CPU_PPC601: /* ? */
10791   case CPU_PPC7450:
10792     return 3;
10793   case CPU_PPC603:
10794   case CPU_PPC750:
10795   case CPU_PPC7400:
10796     return 2; 
10797   case CPU_RIOS2:
10798   case CPU_PPC604:
10799   case CPU_PPC604E:
10800   case CPU_PPC620:
10801   case CPU_PPC630:
10802   case CPU_POWER4:
10803     return 4;
10804   default:
10805     return 1;
10806   }
10807 }
10808
10809 \f
10810 /* Length in units of the trampoline for entering a nested function.  */
10811
10812 int
10813 rs6000_trampoline_size ()
10814 {
10815   int ret = 0;
10816
10817   switch (DEFAULT_ABI)
10818     {
10819     default:
10820       abort ();
10821
10822     case ABI_AIX:
10823       ret = (TARGET_32BIT) ? 12 : 24;
10824       break;
10825
10826     case ABI_DARWIN:
10827     case ABI_V4:
10828     case ABI_AIX_NODESC:
10829       ret = (TARGET_32BIT) ? 40 : 48;
10830       break;
10831     }
10832
10833   return ret;
10834 }
10835
10836 /* Emit RTL insns to initialize the variable parts of a trampoline.
10837    FNADDR is an RTX for the address of the function's pure code.
10838    CXT is an RTX for the static chain value for the function.  */
10839
10840 void
10841 rs6000_initialize_trampoline (addr, fnaddr, cxt)
10842      rtx addr;
10843      rtx fnaddr;
10844      rtx cxt;
10845 {
10846   enum machine_mode pmode = Pmode;
10847   int regsize = (TARGET_32BIT) ? 4 : 8;
10848   rtx ctx_reg = force_reg (pmode, cxt);
10849
10850   switch (DEFAULT_ABI)
10851     {
10852     default:
10853       abort ();
10854
10855 /* Macros to shorten the code expansions below.  */
10856 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
10857 #define MEM_PLUS(addr,offset) \
10858   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
10859
10860     /* Under AIX, just build the 3 word function descriptor */
10861     case ABI_AIX:
10862       {
10863         rtx fn_reg = gen_reg_rtx (pmode);
10864         rtx toc_reg = gen_reg_rtx (pmode);
10865         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
10866         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
10867         emit_move_insn (MEM_DEREF (addr), fn_reg);
10868         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
10869         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
10870       }
10871       break;
10872
10873     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
10874     case ABI_DARWIN:
10875     case ABI_V4:
10876     case ABI_AIX_NODESC:
10877       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
10878                          FALSE, VOIDmode, 4,
10879                          addr, pmode,
10880                          GEN_INT (rs6000_trampoline_size ()), SImode,
10881                          fnaddr, pmode,
10882                          ctx_reg, pmode);
10883       break;
10884     }
10885
10886   return;
10887 }
10888
10889 \f
10890 /* Table of valid machine attributes.  */
10891
10892 const struct attribute_spec rs6000_attribute_table[] =
10893 {
10894   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
10895   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
10896   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
10897   { NULL,        0, 0, false, false, false, NULL }
10898 };
10899
10900 /* Handle a "longcall" or "shortcall" attribute; arguments as in
10901    struct attribute_spec.handler.  */
10902
10903 static tree
10904 rs6000_handle_longcall_attribute (node, name, args, flags, no_add_attrs)
10905      tree *node;
10906      tree name;
10907      tree args ATTRIBUTE_UNUSED;
10908      int flags ATTRIBUTE_UNUSED;
10909      bool *no_add_attrs;
10910 {
10911   if (TREE_CODE (*node) != FUNCTION_TYPE
10912       && TREE_CODE (*node) != FIELD_DECL
10913       && TREE_CODE (*node) != TYPE_DECL)
10914     {
10915       warning ("`%s' attribute only applies to functions",
10916                IDENTIFIER_POINTER (name));
10917       *no_add_attrs = true;
10918     }
10919
10920   return NULL_TREE;
10921 }
10922
10923 /* Set longcall attributes on all functions declared when
10924    rs6000_default_long_calls is true.  */
10925 static void
10926 rs6000_set_default_type_attributes (type)
10927      tree type;
10928 {
10929   if (rs6000_default_long_calls
10930       && (TREE_CODE (type) == FUNCTION_TYPE
10931           || TREE_CODE (type) == METHOD_TYPE))
10932     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
10933                                         NULL_TREE,
10934                                         TYPE_ATTRIBUTES (type));
10935 }
10936
10937 /* Return a reference suitable for calling a function with the
10938    longcall attribute.  */
10939
10940 struct rtx_def *
10941 rs6000_longcall_ref (call_ref)
10942      rtx call_ref;
10943 {
10944   const char *call_name;
10945   tree node;
10946
10947   if (GET_CODE (call_ref) != SYMBOL_REF)
10948     return call_ref;
10949
10950   /* System V adds '.' to the internal name, so skip them.  */
10951   call_name = XSTR (call_ref, 0);
10952   if (*call_name == '.')
10953     {
10954       while (*call_name == '.')
10955         call_name++;
10956
10957       node = get_identifier (call_name);
10958       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
10959     }
10960
10961   return force_reg (Pmode, call_ref);
10962 }
10963
10964 \f
10965 #ifdef USING_ELFOS_H
10966
10967 /* A C statement or statements to switch to the appropriate section
10968    for output of RTX in mode MODE.  You can assume that RTX is some
10969    kind of constant in RTL.  The argument MODE is redundant except in
10970    the case of a `const_int' rtx.  Select the section by calling
10971    `text_section' or one of the alternatives for other sections.
10972
10973    Do not define this macro if you put all constants in the read-only
10974    data section.  */
10975
10976 static void
10977 rs6000_elf_select_rtx_section (mode, x, align)
10978      enum machine_mode mode;
10979      rtx x;
10980      unsigned HOST_WIDE_INT align;
10981 {
10982   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
10983     toc_section ();
10984   else
10985     default_elf_select_rtx_section (mode, x, align);
10986 }
10987
10988 /* A C statement or statements to switch to the appropriate
10989    section for output of DECL.  DECL is either a `VAR_DECL' node
10990    or a constant of some sort.  RELOC indicates whether forming
10991    the initial value of DECL requires link-time relocations.  */
10992
10993 static void
10994 rs6000_elf_select_section (decl, reloc, align)
10995      tree decl;
10996      int reloc;
10997      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
10998 {
10999   int size = int_size_in_bytes (TREE_TYPE (decl));
11000   int needs_sdata;
11001   int readonly;
11002   static void (* const sec_funcs[4]) PARAMS ((void)) = {
11003     &readonly_data_section,
11004     &sdata2_section,
11005     &data_section,
11006     &sdata_section
11007   };
11008   
11009   needs_sdata = (size > 0 
11010                  && size <= g_switch_value
11011                  && rs6000_sdata != SDATA_NONE
11012                  && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
11013
11014   if (TREE_CODE (decl) == STRING_CST)
11015     readonly = ! flag_writable_strings;
11016   else if (TREE_CODE (decl) == VAR_DECL)
11017     readonly = (! (flag_pic && reloc)
11018                 && TREE_READONLY (decl)
11019                 && ! TREE_SIDE_EFFECTS (decl)
11020                 && DECL_INITIAL (decl)
11021                 && DECL_INITIAL (decl) != error_mark_node
11022                 && TREE_CONSTANT (DECL_INITIAL (decl)));
11023   else if (TREE_CODE (decl) == CONSTRUCTOR)
11024     readonly = (! (flag_pic && reloc)
11025                 && ! TREE_SIDE_EFFECTS (decl)
11026                 && TREE_CONSTANT (decl));
11027   else
11028     readonly = 1;
11029   if (needs_sdata && rs6000_sdata != SDATA_EABI)
11030     readonly = 0;
11031   
11032   (*sec_funcs[(readonly ? 0 : 2) + (needs_sdata ? 1 : 0)])();
11033 }
11034
11035 /* A C statement to build up a unique section name, expressed as a
11036    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
11037    RELOC indicates whether the initial value of EXP requires
11038    link-time relocations.  If you do not define this macro, GCC will use
11039    the symbol name prefixed by `.' as the section name.  Note - this
11040    macro can now be called for uninitialized data items as well as
11041    initialised data and functions.  */
11042
11043 static void
11044 rs6000_elf_unique_section (decl, reloc)
11045      tree decl;
11046      int reloc;
11047 {
11048   int len;
11049   int sec;
11050   const char *name;
11051   char *string;
11052   const char *prefix;
11053
11054   static const char *const prefixes[7][2] =
11055   {
11056     { ".rodata.", ".gnu.linkonce.r." },
11057     { ".sdata2.", ".gnu.linkonce.s2." },
11058     { ".data.",   ".gnu.linkonce.d." },
11059     { ".sdata.",  ".gnu.linkonce.s." },
11060     { ".bss.",    ".gnu.linkonce.b." },
11061     { ".sbss.",   ".gnu.linkonce.sb." },
11062     { ".text.",   ".gnu.linkonce.t." }
11063   };
11064
11065   if (TREE_CODE (decl) == FUNCTION_DECL)
11066     sec = 6;
11067   else
11068     {
11069       int readonly;
11070       int needs_sdata;
11071       int size;
11072
11073       readonly = 1;
11074       if (TREE_CODE (decl) == STRING_CST)
11075         readonly = ! flag_writable_strings;
11076       else if (TREE_CODE (decl) == VAR_DECL)
11077         readonly = (! (flag_pic && reloc)
11078                     && TREE_READONLY (decl)
11079                     && ! TREE_SIDE_EFFECTS (decl)
11080                     && TREE_CONSTANT (DECL_INITIAL (decl)));
11081
11082       size = int_size_in_bytes (TREE_TYPE (decl));
11083       needs_sdata = (size > 0 
11084                      && size <= g_switch_value
11085                      && rs6000_sdata != SDATA_NONE
11086                      && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
11087
11088       if (DECL_INITIAL (decl) == 0
11089           || DECL_INITIAL (decl) == error_mark_node)
11090         sec = 4;
11091       else if (! readonly)
11092         sec = 2;
11093       else
11094         sec = 0;
11095
11096       if (needs_sdata)
11097         {
11098           /* .sdata2 is only for EABI.  */
11099           if (sec == 0 && rs6000_sdata != SDATA_EABI)
11100             sec = 2;
11101           sec += 1;
11102         }
11103     }
11104
11105   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11106   name = (*targetm.strip_name_encoding) (name);
11107   prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
11108   len    = strlen (name) + strlen (prefix);
11109   string = alloca (len + 1);
11110   
11111   sprintf (string, "%s%s", prefix, name);
11112   
11113   DECL_SECTION_NAME (decl) = build_string (len, string);
11114 }
11115
11116 \f
11117 /* If we are referencing a function that is static or is known to be
11118    in this file, make the SYMBOL_REF special.  We can use this to indicate
11119    that we can branch to this function without emitting a no-op after the
11120    call.  For real AIX calling sequences, we also replace the
11121    function name with the real name (1 or 2 leading .'s), rather than
11122    the function descriptor name.  This saves a lot of overriding code
11123    to read the prefixes.  */
11124
11125 static void
11126 rs6000_elf_encode_section_info (decl, first)
11127      tree decl;
11128      int first;
11129 {
11130   if (!first)
11131     return;
11132
11133   if (TREE_CODE (decl) == FUNCTION_DECL)
11134     {
11135       rtx sym_ref = XEXP (DECL_RTL (decl), 0);
11136       if ((TREE_ASM_WRITTEN (decl) || ! TREE_PUBLIC (decl))
11137           && ! DECL_WEAK (decl))
11138         SYMBOL_REF_FLAG (sym_ref) = 1;
11139
11140       if (DEFAULT_ABI == ABI_AIX)
11141         {
11142           size_t len1 = (DEFAULT_ABI == ABI_AIX) ? 1 : 2;
11143           size_t len2 = strlen (XSTR (sym_ref, 0));
11144           char *str = alloca (len1 + len2 + 1);
11145           str[0] = '.';
11146           str[1] = '.';
11147           memcpy (str + len1, XSTR (sym_ref, 0), len2 + 1);
11148
11149           XSTR (sym_ref, 0) = ggc_alloc_string (str, len1 + len2);
11150         }
11151     }
11152   else if (rs6000_sdata != SDATA_NONE
11153            && DEFAULT_ABI == ABI_V4
11154            && TREE_CODE (decl) == VAR_DECL)
11155     {
11156       int size = int_size_in_bytes (TREE_TYPE (decl));
11157       tree section_name = DECL_SECTION_NAME (decl);
11158       const char *name = (char *)0;
11159       int len = 0;
11160
11161       if (section_name)
11162         {
11163           if (TREE_CODE (section_name) == STRING_CST)
11164             {
11165               name = TREE_STRING_POINTER (section_name);
11166               len = TREE_STRING_LENGTH (section_name);
11167             }
11168           else
11169             abort ();
11170         }
11171
11172       if ((size > 0 && size <= g_switch_value)
11173           || (name
11174               && ((len == sizeof (".sdata") - 1
11175                    && strcmp (name, ".sdata") == 0)
11176                   || (len == sizeof (".sdata2") - 1
11177                       && strcmp (name, ".sdata2") == 0)
11178                   || (len == sizeof (".sbss") - 1
11179                       && strcmp (name, ".sbss") == 0)
11180                   || (len == sizeof (".sbss2") - 1
11181                       && strcmp (name, ".sbss2") == 0)
11182                   || (len == sizeof (".PPC.EMB.sdata0") - 1
11183                       && strcmp (name, ".PPC.EMB.sdata0") == 0)
11184                   || (len == sizeof (".PPC.EMB.sbss0") - 1
11185                       && strcmp (name, ".PPC.EMB.sbss0") == 0))))
11186         {
11187           rtx sym_ref = XEXP (DECL_RTL (decl), 0);
11188           size_t len = strlen (XSTR (sym_ref, 0));
11189           char *str = alloca (len + 2);
11190
11191           str[0] = '@';
11192           memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
11193           XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
11194         }
11195     }
11196 }
11197
11198 static const char *
11199 rs6000_elf_strip_name_encoding (str)
11200      const char *str;
11201 {
11202   while (*str == '*' || *str == '@')
11203     str++;
11204   return str;
11205 }
11206
11207 #endif /* USING_ELFOS_H */
11208
11209 \f
11210 /* Return a REG that occurs in ADDR with coefficient 1.
11211    ADDR can be effectively incremented by incrementing REG.
11212
11213    r0 is special and we must not select it as an address
11214    register by this routine since our caller will try to
11215    increment the returned register via an "la" instruction.  */
11216
11217 struct rtx_def *
11218 find_addr_reg (addr)
11219      rtx addr;
11220 {
11221   while (GET_CODE (addr) == PLUS)
11222     {
11223       if (GET_CODE (XEXP (addr, 0)) == REG
11224           && REGNO (XEXP (addr, 0)) != 0)
11225         addr = XEXP (addr, 0);
11226       else if (GET_CODE (XEXP (addr, 1)) == REG
11227                && REGNO (XEXP (addr, 1)) != 0)
11228         addr = XEXP (addr, 1);
11229       else if (CONSTANT_P (XEXP (addr, 0)))
11230         addr = XEXP (addr, 1);
11231       else if (CONSTANT_P (XEXP (addr, 1)))
11232         addr = XEXP (addr, 0);
11233       else
11234         abort ();
11235     }
11236   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
11237     return addr;
11238   abort ();
11239 }
11240
11241 void
11242 rs6000_fatal_bad_address (op)
11243   rtx op;
11244 {
11245   fatal_insn ("bad address", op);
11246 }
11247
11248 /* Called to register all of our global variables with the garbage
11249    collector.  */
11250
11251 static void
11252 rs6000_add_gc_roots ()
11253 {
11254   toc_hash_table = htab_create (1021, toc_hash_function, toc_hash_eq, NULL);
11255   ggc_add_root (&toc_hash_table, 1, sizeof (toc_hash_table), 
11256                 toc_hash_mark_table);
11257 }
11258
11259 #if TARGET_MACHO
11260
11261 #if 0
11262 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
11263    reference and a constant.  */
11264
11265 int
11266 symbolic_operand (op)
11267      rtx op;
11268 {
11269   switch (GET_CODE (op))
11270     {
11271     case SYMBOL_REF:
11272     case LABEL_REF:
11273       return 1;
11274     case CONST:
11275       op = XEXP (op, 0);
11276       return (GET_CODE (op) == SYMBOL_REF ||
11277               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
11278                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
11279               && GET_CODE (XEXP (op, 1)) == CONST_INT);
11280     default:
11281       return 0;
11282     }
11283 }
11284 #endif
11285
11286 #ifdef RS6000_LONG_BRANCH
11287
11288 static tree stub_list = 0;
11289
11290 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
11291    procedure calls to the linked list.  */
11292
11293 void 
11294 add_compiler_stub (label_name, function_name, line_number)
11295      tree label_name;
11296      tree function_name;
11297      int line_number;
11298 {
11299   tree stub = build_tree_list (function_name, label_name);
11300   TREE_TYPE (stub) = build_int_2 (line_number, 0);
11301   TREE_CHAIN (stub) = stub_list;
11302   stub_list = stub;
11303 }
11304
11305 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
11306 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
11307 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
11308
11309 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
11310    handling procedure calls from the linked list and initializes the
11311    linked list.  */
11312
11313 void
11314 output_compiler_stub ()
11315 {
11316   char tmp_buf[256];
11317   char label_buf[256];
11318   char *label;
11319   tree tmp_stub, stub;
11320
11321   if (!flag_pic)
11322     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
11323       {
11324         fprintf (asm_out_file,
11325                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
11326
11327 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
11328         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
11329           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
11330 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
11331
11332         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
11333           strcpy (label_buf,
11334                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
11335         else
11336           {
11337             label_buf[0] = '_';
11338             strcpy (label_buf+1,
11339                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
11340           }
11341
11342         strcpy (tmp_buf, "lis r12,hi16(");
11343         strcat (tmp_buf, label_buf);
11344         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
11345         strcat (tmp_buf, label_buf);
11346         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
11347         output_asm_insn (tmp_buf, 0);
11348
11349 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
11350         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
11351           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
11352 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
11353       }
11354
11355   stub_list = 0;
11356 }
11357
11358 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
11359    already there or not.  */
11360
11361 int
11362 no_previous_def (function_name)
11363      tree function_name;
11364 {
11365   tree stub;
11366   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
11367     if (function_name == STUB_FUNCTION_NAME (stub))
11368       return 0;
11369   return 1;
11370 }
11371
11372 /* GET_PREV_LABEL gets the label name from the previous definition of
11373    the function.  */
11374
11375 tree
11376 get_prev_label (function_name)
11377      tree function_name;
11378 {
11379   tree stub;
11380   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
11381     if (function_name == STUB_FUNCTION_NAME (stub))
11382       return STUB_LABEL_NAME (stub);
11383   return 0;
11384 }
11385
11386 /* INSN is either a function call or a millicode call.  It may have an
11387    unconditional jump in its delay slot.  
11388
11389    CALL_DEST is the routine we are calling.  */
11390
11391 char *
11392 output_call (insn, call_dest, operand_number)
11393      rtx insn;
11394      rtx call_dest;
11395      int operand_number;
11396 {
11397   static char buf[256];
11398   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
11399     {
11400       tree labelname;
11401       tree funname = get_identifier (XSTR (call_dest, 0));
11402       
11403       if (no_previous_def (funname))
11404         {
11405           int line_number;
11406           rtx label_rtx = gen_label_rtx ();
11407           char *label_buf, temp_buf[256];
11408           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
11409                                        CODE_LABEL_NUMBER (label_rtx));
11410           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
11411           labelname = get_identifier (label_buf);
11412           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
11413           if (insn)
11414             line_number = NOTE_LINE_NUMBER (insn);
11415           add_compiler_stub (labelname, funname, line_number);
11416         }
11417       else
11418         labelname = get_prev_label (funname);
11419
11420       sprintf (buf, "jbsr %%z%d,%.246s",
11421                operand_number, IDENTIFIER_POINTER (labelname));
11422       return buf;
11423     }
11424   else
11425     {
11426       sprintf (buf, "bl %%z%d", operand_number);
11427       return buf;
11428     }
11429 }
11430
11431 #endif /* RS6000_LONG_BRANCH */
11432
11433 #define GEN_LOCAL_LABEL_FOR_SYMBOL(BUF,SYMBOL,LENGTH,N)         \
11434   do {                                                          \
11435     const char *const symbol_ = (SYMBOL);                       \
11436     char *buffer_ = (BUF);                                      \
11437     if (symbol_[0] == '"')                                      \
11438       {                                                         \
11439         sprintf(buffer_, "\"L%d$%s", (N), symbol_+1);           \
11440       }                                                         \
11441     else if (name_needs_quotes(symbol_))                        \
11442       {                                                         \
11443         sprintf(buffer_, "\"L%d$%s\"", (N), symbol_);           \
11444       }                                                         \
11445     else                                                        \
11446       {                                                         \
11447         sprintf(buffer_, "L%d$%s", (N), symbol_);               \
11448       }                                                         \
11449   } while (0)
11450
11451
11452 /* Generate PIC and indirect symbol stubs.  */
11453
11454 void
11455 machopic_output_stub (file, symb, stub)
11456      FILE *file;
11457      const char *symb, *stub;
11458 {
11459   unsigned int length;
11460   char *symbol_name, *lazy_ptr_name;
11461   char *local_label_0;
11462   static int label = 0;
11463
11464   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
11465   symb = (*targetm.strip_name_encoding) (symb);
11466
11467   label += 1;
11468
11469   length = strlen (symb);
11470   symbol_name = alloca (length + 32);
11471   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
11472
11473   lazy_ptr_name = alloca (length + 32);
11474   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
11475
11476   local_label_0 = alloca (length + 32);
11477   GEN_LOCAL_LABEL_FOR_SYMBOL (local_label_0, symb, length, 0);
11478
11479   if (flag_pic == 2)
11480     machopic_picsymbol_stub_section ();
11481   else
11482     machopic_symbol_stub_section ();
11483
11484   fprintf (file, "%s:\n", stub);
11485   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
11486
11487   if (flag_pic == 2)
11488     {
11489       fprintf (file, "\tmflr r0\n");
11490       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
11491       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
11492       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
11493                lazy_ptr_name, local_label_0);
11494       fprintf (file, "\tmtlr r0\n");
11495       fprintf (file, "\tlwz r12,lo16(%s-%s)(r11)\n",
11496                lazy_ptr_name, local_label_0);
11497       fprintf (file, "\tmtctr r12\n");
11498       fprintf (file, "\taddi r11,r11,lo16(%s-%s)\n",
11499                lazy_ptr_name, local_label_0);
11500       fprintf (file, "\tbctr\n");
11501     }
11502   else
11503     fprintf (file, "non-pure not supported\n");
11504   
11505   machopic_lazy_symbol_ptr_section ();
11506   fprintf (file, "%s:\n", lazy_ptr_name);
11507   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
11508   fprintf (file, "\t.long dyld_stub_binding_helper\n");
11509 }
11510
11511 /* Legitimize PIC addresses.  If the address is already
11512    position-independent, we return ORIG.  Newly generated
11513    position-independent addresses go into a reg.  This is REG if non
11514    zero, otherwise we allocate register(s) as necessary.  */
11515
11516 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
11517
11518 rtx
11519 rs6000_machopic_legitimize_pic_address (orig, mode, reg)
11520      rtx orig;
11521      enum machine_mode mode;
11522      rtx reg;
11523 {
11524   rtx base, offset;
11525
11526   if (reg == NULL && ! reload_in_progress && ! reload_completed)
11527     reg = gen_reg_rtx (Pmode);
11528
11529   if (GET_CODE (orig) == CONST)
11530     {
11531       if (GET_CODE (XEXP (orig, 0)) == PLUS
11532           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
11533         return orig;
11534
11535       if (GET_CODE (XEXP (orig, 0)) == PLUS)
11536         {
11537           base =
11538             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
11539                                                     Pmode, reg);
11540           offset =
11541             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
11542                                                     Pmode, reg);
11543         }
11544       else
11545         abort ();
11546
11547       if (GET_CODE (offset) == CONST_INT)
11548         {
11549           if (SMALL_INT (offset))
11550             return plus_constant (base, INTVAL (offset));
11551           else if (! reload_in_progress && ! reload_completed)
11552             offset = force_reg (Pmode, offset);
11553           else
11554             {
11555               rtx mem = force_const_mem (Pmode, orig);
11556               return machopic_legitimize_pic_address (mem, Pmode, reg);
11557             }
11558         }
11559       return gen_rtx (PLUS, Pmode, base, offset);
11560     }
11561
11562   /* Fall back on generic machopic code.  */
11563   return machopic_legitimize_pic_address (orig, mode, reg);
11564 }
11565
11566 /* This is just a placeholder to make linking work without having to
11567    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
11568    ever needed for Darwin (not too likely!) this would have to get a
11569    real definition.  */
11570
11571 void
11572 toc_section ()
11573 {
11574 }
11575
11576 #endif /* TARGET_MACHO */
11577
11578 #if TARGET_ELF
11579 static unsigned int
11580 rs6000_elf_section_type_flags (decl, name, reloc)
11581      tree decl;
11582      const char *name;
11583      int reloc;
11584 {
11585   unsigned int flags = default_section_type_flags (decl, name, reloc);
11586
11587   if (TARGET_RELOCATABLE)
11588     flags |= SECTION_WRITE;
11589
11590   return flags;
11591 }
11592
11593 /* Record an element in the table of global constructors.  SYMBOL is
11594    a SYMBOL_REF of the function to be called; PRIORITY is a number
11595    between 0 and MAX_INIT_PRIORITY.
11596
11597    This differs from default_named_section_asm_out_constructor in
11598    that we have special handling for -mrelocatable.  */
11599
11600 static void
11601 rs6000_elf_asm_out_constructor (symbol, priority)
11602      rtx symbol;
11603      int priority;
11604 {
11605   const char *section = ".ctors";
11606   char buf[16];
11607
11608   if (priority != DEFAULT_INIT_PRIORITY)
11609     {
11610       sprintf (buf, ".ctors.%.5u",
11611                /* Invert the numbering so the linker puts us in the proper
11612                   order; constructors are run from right to left, and the
11613                   linker sorts in increasing order.  */
11614                MAX_INIT_PRIORITY - priority);
11615       section = buf;
11616     }
11617
11618   named_section_flags (section, SECTION_WRITE);
11619   assemble_align (POINTER_SIZE);
11620
11621   if (TARGET_RELOCATABLE)
11622     {
11623       fputs ("\t.long (", asm_out_file);
11624       output_addr_const (asm_out_file, symbol);
11625       fputs (")@fixup\n", asm_out_file);
11626     }
11627   else
11628     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
11629 }
11630
11631 static void
11632 rs6000_elf_asm_out_destructor (symbol, priority)
11633      rtx symbol;
11634      int priority;
11635 {
11636   const char *section = ".dtors";
11637   char buf[16];
11638
11639   if (priority != DEFAULT_INIT_PRIORITY)
11640     {
11641       sprintf (buf, ".dtors.%.5u",
11642                /* Invert the numbering so the linker puts us in the proper
11643                   order; constructors are run from right to left, and the
11644                   linker sorts in increasing order.  */
11645                MAX_INIT_PRIORITY - priority);
11646       section = buf;
11647     }
11648
11649   named_section_flags (section, SECTION_WRITE);
11650   assemble_align (POINTER_SIZE);
11651
11652   if (TARGET_RELOCATABLE)
11653     {
11654       fputs ("\t.long (", asm_out_file);
11655       output_addr_const (asm_out_file, symbol);
11656       fputs (")@fixup\n", asm_out_file);
11657     }
11658   else
11659     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
11660 }
11661 #endif
11662
11663 #if TARGET_XCOFF
11664 static void
11665 xcoff_asm_named_section (name, flags)
11666      const char *name;
11667      unsigned int flags ATTRIBUTE_UNUSED;
11668 {
11669   fprintf (asm_out_file, "\t.csect %s\n", name);
11670 }
11671
11672 static void
11673 rs6000_xcoff_select_section (exp, reloc, align)
11674      tree exp;
11675      int reloc;
11676      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
11677 {
11678   if ((TREE_CODE (exp) == STRING_CST
11679        && ! flag_writable_strings)
11680       || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
11681           && TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp)
11682           && DECL_INITIAL (exp)
11683           && (DECL_INITIAL (exp) == error_mark_node
11684               || TREE_CONSTANT (DECL_INITIAL (exp)))
11685           && ! (reloc)))
11686     {
11687       if (TREE_PUBLIC (exp))
11688         read_only_data_section ();
11689       else
11690         read_only_private_data_section ();
11691     }
11692   else
11693     {
11694       if (TREE_PUBLIC (exp))
11695         data_section ();
11696       else
11697         private_data_section ();
11698     }
11699 }
11700
11701 static void
11702 rs6000_xcoff_unique_section (decl, reloc)
11703      tree decl;
11704      int reloc ATTRIBUTE_UNUSED;
11705 {
11706   const char *name;
11707   char *string;
11708   size_t len;
11709
11710   if (TREE_CODE (decl) == FUNCTION_DECL)
11711     {
11712       name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11713       len = strlen (name) + 5;
11714       string = alloca (len + 1);
11715       sprintf (string, ".%s[PR]", name);
11716       DECL_SECTION_NAME (decl) = build_string (len, string);
11717     }
11718 }
11719
11720 /* Select section for constant in constant pool.
11721
11722    On RS/6000, all constants are in the private read-only data area.
11723    However, if this is being placed in the TOC it must be output as a
11724    toc entry.  */
11725
11726 static void
11727 rs6000_xcoff_select_rtx_section (mode, x, align)
11728      enum machine_mode mode;
11729      rtx x;
11730      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
11731 {
11732   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
11733     toc_section ();
11734   else
11735     read_only_private_data_section ();
11736 }
11737
11738 /* Remove any trailing [DS] or the like from the symbol name.  */
11739
11740 static const char *
11741 rs6000_xcoff_strip_name_encoding (name)
11742      const char *name;
11743 {
11744   size_t len;
11745   if (*name == '*')
11746     name++;
11747   len = strlen (name);
11748   if (name[len - 1] == ']')
11749     return ggc_alloc_string (name, len - 4);
11750   else
11751     return name;
11752 }
11753
11754 #endif /* TARGET_XCOFF */
11755
11756 /* Note that this is also used for ELF64.  */
11757
11758 static void
11759 rs6000_xcoff_encode_section_info (decl, first)
11760      tree decl;
11761      int first ATTRIBUTE_UNUSED;
11762 {
11763   if (TREE_CODE (decl) == FUNCTION_DECL
11764       && (TREE_ASM_WRITTEN (decl) || ! TREE_PUBLIC (decl))
11765       && ! DECL_WEAK (decl))
11766     SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
11767 }