OSDN Git Service

2010-06-28 Philipp Tomsich <philipp.tomsich@theobroma-systems.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, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6
7    This file is part of GCC.
8
9    GCC is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published
11    by the Free Software Foundation; either version 3, or (at your
12    option) any later version.
13
14    GCC is distributed in the hope that it will be useful, but WITHOUT
15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17    License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with GCC; see the file COPYING3.  If not see
21    <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "obstack.h"
36 #include "tree.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "except.h"
40 #include "function.h"
41 #include "output.h"
42 #include "basic-block.h"
43 #include "integrate.h"
44 #include "toplev.h"
45 #include "ggc.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "cfglayout.h"
53 #include "sched-int.h"
54 #include "gimple.h"
55 #include "tree-flow.h"
56 #include "intl.h"
57 #include "params.h"
58 #include "tm-constrs.h"
59 #if TARGET_XCOFF
60 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
61 #endif
62 #if TARGET_MACHO
63 #include "gstab.h"  /* for N_SLINE */
64 #endif
65
66 #ifndef TARGET_NO_PROTOTYPE
67 #define TARGET_NO_PROTOTYPE 0
68 #endif
69
70 #define min(A,B)        ((A) < (B) ? (A) : (B))
71 #define max(A,B)        ((A) > (B) ? (A) : (B))
72
73 /* Structure used to define the rs6000 stack */
74 typedef struct rs6000_stack {
75   int first_gp_reg_save;        /* first callee saved GP register used */
76   int first_fp_reg_save;        /* first callee saved FP register used */
77   int first_altivec_reg_save;   /* first callee saved AltiVec register used */
78   int lr_save_p;                /* true if the link reg needs to be saved */
79   int cr_save_p;                /* true if the CR reg needs to be saved */
80   unsigned int vrsave_mask;     /* mask of vec registers to save */
81   int push_p;                   /* true if we need to allocate stack space */
82   int calls_p;                  /* true if the function makes any calls */
83   int world_save_p;             /* true if we're saving *everything*:
84                                    r13-r31, cr, f14-f31, vrsave, v20-v31  */
85   enum rs6000_abi abi;          /* which ABI to use */
86   int gp_save_offset;           /* offset to save GP regs from initial SP */
87   int fp_save_offset;           /* offset to save FP regs from initial SP */
88   int altivec_save_offset;      /* offset to save AltiVec regs from initial SP */
89   int lr_save_offset;           /* offset to save LR from initial SP */
90   int cr_save_offset;           /* offset to save CR from initial SP */
91   int vrsave_save_offset;       /* offset to save VRSAVE from initial SP */
92   int spe_gp_save_offset;       /* offset to save spe 64-bit gprs  */
93   int varargs_save_offset;      /* offset to save the varargs registers */
94   int ehrd_offset;              /* offset to EH return data */
95   int reg_size;                 /* register size (4 or 8) */
96   HOST_WIDE_INT vars_size;      /* variable save area size */
97   int parm_size;                /* outgoing parameter size */
98   int save_size;                /* save area size */
99   int fixed_size;               /* fixed size of stack frame */
100   int gp_size;                  /* size of saved GP registers */
101   int fp_size;                  /* size of saved FP registers */
102   int altivec_size;             /* size of saved AltiVec registers */
103   int cr_size;                  /* size to hold CR if not in save_size */
104   int vrsave_size;              /* size to hold VRSAVE if not in save_size */
105   int altivec_padding_size;     /* size of altivec alignment padding if
106                                    not in save_size */
107   int spe_gp_size;              /* size of 64-bit GPR save size for SPE */
108   int spe_padding_size;
109   HOST_WIDE_INT total_size;     /* total bytes allocated for stack */
110   int spe_64bit_regs_used;
111 } rs6000_stack_t;
112
113 /* A C structure for machine-specific, per-function data.
114    This is added to the cfun structure.  */
115 typedef struct GTY(()) machine_function
116 {
117   /* Some local-dynamic symbol.  */
118   const char *some_ld_name;
119   /* Whether the instruction chain has been scanned already.  */
120   int insn_chain_scanned_p;
121   /* Flags if __builtin_return_address (n) with n >= 1 was used.  */
122   int ra_needs_full_frame;
123   /* Flags if __builtin_return_address (0) was used.  */
124   int ra_need_lr;
125   /* Cache lr_save_p after expansion of builtin_eh_return.  */
126   int lr_save_state;
127   /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
128      varargs save area.  */
129   HOST_WIDE_INT varargs_save_offset;
130   /* Temporary stack slot to use for SDmode copies.  This slot is
131      64-bits wide and is allocated early enough so that the offset
132      does not overflow the 16-bit load/store offset field.  */
133   rtx sdmode_stack_slot;
134 } machine_function;
135
136 /* Target cpu type */
137
138 enum processor_type rs6000_cpu;
139 struct rs6000_cpu_select rs6000_select[3] =
140 {
141   /* switch             name,                   tune    arch */
142   { (const char *)0,    "--with-cpu=",          1,      1 },
143   { (const char *)0,    "-mcpu=",               1,      1 },
144   { (const char *)0,    "-mtune=",              1,      0 },
145 };
146
147 /* Always emit branch hint bits.  */
148 static GTY(()) bool rs6000_always_hint;
149
150 /* Schedule instructions for group formation.  */
151 static GTY(()) bool rs6000_sched_groups;
152
153 /* Align branch targets.  */
154 static GTY(()) bool rs6000_align_branch_targets;
155
156 /* Support for -msched-costly-dep option.  */
157 const char *rs6000_sched_costly_dep_str;
158 enum rs6000_dependence_cost rs6000_sched_costly_dep;
159
160 /* Support for -minsert-sched-nops option.  */
161 const char *rs6000_sched_insert_nops_str;
162 enum rs6000_nop_insertion rs6000_sched_insert_nops;
163
164 /* Support targetm.vectorize.builtin_mask_for_load.  */
165 static GTY(()) tree altivec_builtin_mask_for_load;
166
167 /* Size of long double.  */
168 int rs6000_long_double_type_size;
169
170 /* IEEE quad extended precision long double. */
171 int rs6000_ieeequad;
172
173 /* Nonzero to use AltiVec ABI.  */
174 int rs6000_altivec_abi;
175
176 /* Nonzero if we want SPE SIMD instructions.  */
177 int rs6000_spe;
178
179 /* Nonzero if we want SPE ABI extensions.  */
180 int rs6000_spe_abi;
181
182 /* Nonzero if floating point operations are done in the GPRs.  */
183 int rs6000_float_gprs = 0;
184
185 /* Nonzero if we want Darwin's struct-by-value-in-regs ABI.  */
186 int rs6000_darwin64_abi;
187
188 /* Set to nonzero once AIX common-mode calls have been defined.  */
189 static GTY(()) int common_mode_defined;
190
191 /* Label number of label created for -mrelocatable, to call to so we can
192    get the address of the GOT section */
193 int rs6000_pic_labelno;
194
195 #ifdef USING_ELFOS_H
196 /* Which abi to adhere to */
197 const char *rs6000_abi_name;
198
199 /* Semantics of the small data area */
200 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
201
202 /* Which small data model to use */
203 const char *rs6000_sdata_name = (char *)0;
204
205 /* Counter for labels which are to be placed in .fixup.  */
206 int fixuplabelno = 0;
207 #endif
208
209 /* Bit size of immediate TLS offsets and string from which it is decoded.  */
210 int rs6000_tls_size = 32;
211 const char *rs6000_tls_size_string;
212
213 /* ABI enumeration available for subtarget to use.  */
214 enum rs6000_abi rs6000_current_abi;
215
216 /* Whether to use variant of AIX ABI for PowerPC64 Linux.  */
217 int dot_symbols;
218
219 /* Debug flags */
220 const char *rs6000_debug_name;
221 int rs6000_debug_stack;         /* debug stack applications */
222 int rs6000_debug_arg;           /* debug argument handling */
223 int rs6000_debug_reg;           /* debug register classes */
224 int rs6000_debug_addr;          /* debug memory addressing */
225 int rs6000_debug_cost;          /* debug rtx_costs */
226
227 /* Specify the machine mode that pointers have.  After generation of rtl, the
228    compiler makes no further distinction between pointers and any other objects
229    of this machine mode.  The type is unsigned since not all things that
230    include rs6000.h also include machmode.h.  */
231 unsigned rs6000_pmode;
232
233 /* Width in bits of a pointer.  */
234 unsigned rs6000_pointer_size;
235
236
237 /* Value is TRUE if register/mode pair is acceptable.  */
238 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
239
240 /* Maximum number of registers needed for a given register class and mode.  */
241 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
242
243 /* How many registers are needed for a given register and mode.  */
244 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
245
246 /* Map register number to register class.  */
247 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
248
249 /* Reload functions based on the type and the vector unit.  */
250 static enum insn_code rs6000_vector_reload[NUM_MACHINE_MODES][2];
251
252 /* Built in types.  */
253 tree rs6000_builtin_types[RS6000_BTI_MAX];
254 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
255
256 const char *rs6000_traceback_name;
257 static enum {
258   traceback_default = 0,
259   traceback_none,
260   traceback_part,
261   traceback_full
262 } rs6000_traceback;
263
264 /* Flag to say the TOC is initialized */
265 int toc_initialized;
266 char toc_label_name[10];
267
268 /* Cached value of rs6000_variable_issue. This is cached in
269    rs6000_variable_issue hook and returned from rs6000_sched_reorder2.  */
270 static short cached_can_issue_more;
271
272 static GTY(()) section *read_only_data_section;
273 static GTY(()) section *private_data_section;
274 static GTY(()) section *read_only_private_data_section;
275 static GTY(()) section *sdata2_section;
276 static GTY(()) section *toc_section;
277
278 /* Control alignment for fields within structures.  */
279 /* String from -malign-XXXXX.  */
280 int rs6000_alignment_flags;
281
282 /* Code model for 64-bit linux.  */
283 enum rs6000_cmodel cmodel;
284
285 /* True for any options that were explicitly set.  */
286 static struct {
287   bool aix_struct_ret;          /* True if -maix-struct-ret was used.  */
288   bool alignment;               /* True if -malign- was used.  */
289   bool spe_abi;                 /* True if -mabi=spe/no-spe was used.  */
290   bool altivec_abi;             /* True if -mabi=altivec/no-altivec used.  */
291   bool spe;                     /* True if -mspe= was used.  */
292   bool float_gprs;              /* True if -mfloat-gprs= was used.  */
293   bool long_double;             /* True if -mlong-double- was used.  */
294   bool ieee;                    /* True if -mabi=ieee/ibmlongdouble used.  */
295   bool vrsave;                  /* True if -mvrsave was used.  */
296   bool cmodel;                  /* True if -mcmodel was used.  */
297 } rs6000_explicit_options;
298
299 struct builtin_description
300 {
301   /* mask is not const because we're going to alter it below.  This
302      nonsense will go away when we rewrite the -march infrastructure
303      to give us more target flag bits.  */
304   unsigned int mask;
305   const enum insn_code icode;
306   const char *const name;
307   const enum rs6000_builtins code;
308 };
309
310 /* Describe the vector unit used for modes.  */
311 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
312 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
313
314 /* Register classes for various constraints that are based on the target
315    switches.  */
316 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
317
318 /* Describe the alignment of a vector.  */
319 int rs6000_vector_align[NUM_MACHINE_MODES];
320
321 /* Map selected modes to types for builtins.  */
322 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
323
324 /* What modes to automatically generate reciprocal divide estimate (fre) and
325    reciprocal sqrt (frsqrte) for.  */
326 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
327
328 /* Masks to determine which reciprocal esitmate instructions to generate
329    automatically.  */
330 enum rs6000_recip_mask {
331   RECIP_SF_DIV          = 0x001,        /* Use divide estimate */
332   RECIP_DF_DIV          = 0x002,
333   RECIP_V4SF_DIV        = 0x004,
334   RECIP_V2DF_DIV        = 0x008,
335
336   RECIP_SF_RSQRT        = 0x010,        /* Use reciprocal sqrt estimate.  */
337   RECIP_DF_RSQRT        = 0x020,
338   RECIP_V4SF_RSQRT      = 0x040,
339   RECIP_V2DF_RSQRT      = 0x080,
340
341   /* Various combination of flags for -mrecip=xxx.  */
342   RECIP_NONE            = 0,
343   RECIP_ALL             = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
344                            | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
345                            | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
346
347   RECIP_HIGH_PRECISION  = RECIP_ALL,
348
349   /* On low precision machines like the power5, don't enable double precision
350      reciprocal square root estimate, since it isn't accurate enough.  */
351   RECIP_LOW_PRECISION   = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
352 };
353
354 static unsigned int rs6000_recip_control;
355 static const char *rs6000_recip_name;
356
357 /* -mrecip options.  */
358 static struct
359 {
360   const char *string;           /* option name */
361   unsigned int mask;            /* mask bits to set */
362 } recip_options[] = {
363   { "all",       RECIP_ALL },
364   { "none",      RECIP_NONE },
365   { "div",       (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
366                   | RECIP_V2DF_DIV) },
367   { "divf",      (RECIP_SF_DIV | RECIP_V4SF_DIV) },
368   { "divd",      (RECIP_DF_DIV | RECIP_V2DF_DIV) },
369   { "rsqrt",     (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
370                   | RECIP_V2DF_RSQRT) },
371   { "rsqrtf",    (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
372   { "rsqrtd",    (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
373 };
374
375 /* 2 argument gen function typedef.  */
376 typedef rtx (*gen_2arg_fn_t) (rtx, rtx, rtx);
377
378 \f
379 /* Target cpu costs.  */
380
381 struct processor_costs {
382   const int mulsi;        /* cost of SImode multiplication.  */
383   const int mulsi_const;  /* cost of SImode multiplication by constant.  */
384   const int mulsi_const9; /* cost of SImode mult by short constant.  */
385   const int muldi;        /* cost of DImode multiplication.  */
386   const int divsi;        /* cost of SImode division.  */
387   const int divdi;        /* cost of DImode division.  */
388   const int fp;           /* cost of simple SFmode and DFmode insns.  */
389   const int dmul;         /* cost of DFmode multiplication (and fmadd).  */
390   const int sdiv;         /* cost of SFmode division (fdivs).  */
391   const int ddiv;         /* cost of DFmode division (fdiv).  */
392   const int cache_line_size;    /* cache line size in bytes. */
393   const int l1_cache_size;      /* size of l1 cache, in kilobytes.  */
394   const int l2_cache_size;      /* size of l2 cache, in kilobytes.  */
395   const int simultaneous_prefetches; /* number of parallel prefetch
396                                         operations.  */
397 };
398
399 const struct processor_costs *rs6000_cost;
400
401 /* Processor costs (relative to an add) */
402
403 /* Instruction size costs on 32bit processors.  */
404 static const
405 struct processor_costs size32_cost = {
406   COSTS_N_INSNS (1),    /* mulsi */
407   COSTS_N_INSNS (1),    /* mulsi_const */
408   COSTS_N_INSNS (1),    /* mulsi_const9 */
409   COSTS_N_INSNS (1),    /* muldi */
410   COSTS_N_INSNS (1),    /* divsi */
411   COSTS_N_INSNS (1),    /* divdi */
412   COSTS_N_INSNS (1),    /* fp */
413   COSTS_N_INSNS (1),    /* dmul */
414   COSTS_N_INSNS (1),    /* sdiv */
415   COSTS_N_INSNS (1),    /* ddiv */
416   32,
417   0,
418   0,
419   0,
420 };
421
422 /* Instruction size costs on 64bit processors.  */
423 static const
424 struct processor_costs size64_cost = {
425   COSTS_N_INSNS (1),    /* mulsi */
426   COSTS_N_INSNS (1),    /* mulsi_const */
427   COSTS_N_INSNS (1),    /* mulsi_const9 */
428   COSTS_N_INSNS (1),    /* muldi */
429   COSTS_N_INSNS (1),    /* divsi */
430   COSTS_N_INSNS (1),    /* divdi */
431   COSTS_N_INSNS (1),    /* fp */
432   COSTS_N_INSNS (1),    /* dmul */
433   COSTS_N_INSNS (1),    /* sdiv */
434   COSTS_N_INSNS (1),    /* ddiv */
435   128,
436   0,
437   0,
438   0,
439 };
440
441 /* Instruction costs on RIOS1 processors.  */
442 static const
443 struct processor_costs rios1_cost = {
444   COSTS_N_INSNS (5),    /* mulsi */
445   COSTS_N_INSNS (4),    /* mulsi_const */
446   COSTS_N_INSNS (3),    /* mulsi_const9 */
447   COSTS_N_INSNS (5),    /* muldi */
448   COSTS_N_INSNS (19),   /* divsi */
449   COSTS_N_INSNS (19),   /* divdi */
450   COSTS_N_INSNS (2),    /* fp */
451   COSTS_N_INSNS (2),    /* dmul */
452   COSTS_N_INSNS (19),   /* sdiv */
453   COSTS_N_INSNS (19),   /* ddiv */
454   128,                  /* cache line size */
455   64,                   /* l1 cache */
456   512,                  /* l2 cache */
457   0,                    /* streams */
458 };
459
460 /* Instruction costs on RIOS2 processors.  */
461 static const
462 struct processor_costs rios2_cost = {
463   COSTS_N_INSNS (2),    /* mulsi */
464   COSTS_N_INSNS (2),    /* mulsi_const */
465   COSTS_N_INSNS (2),    /* mulsi_const9 */
466   COSTS_N_INSNS (2),    /* muldi */
467   COSTS_N_INSNS (13),   /* divsi */
468   COSTS_N_INSNS (13),   /* divdi */
469   COSTS_N_INSNS (2),    /* fp */
470   COSTS_N_INSNS (2),    /* dmul */
471   COSTS_N_INSNS (17),   /* sdiv */
472   COSTS_N_INSNS (17),   /* ddiv */
473   256,                  /* cache line size */
474   256,                  /* l1 cache */
475   1024,                 /* l2 cache */
476   0,                    /* streams */
477 };
478
479 /* Instruction costs on RS64A processors.  */
480 static const
481 struct processor_costs rs64a_cost = {
482   COSTS_N_INSNS (20),   /* mulsi */
483   COSTS_N_INSNS (12),   /* mulsi_const */
484   COSTS_N_INSNS (8),    /* mulsi_const9 */
485   COSTS_N_INSNS (34),   /* muldi */
486   COSTS_N_INSNS (65),   /* divsi */
487   COSTS_N_INSNS (67),   /* divdi */
488   COSTS_N_INSNS (4),    /* fp */
489   COSTS_N_INSNS (4),    /* dmul */
490   COSTS_N_INSNS (31),   /* sdiv */
491   COSTS_N_INSNS (31),   /* ddiv */
492   128,                  /* cache line size */
493   128,                  /* l1 cache */
494   2048,                 /* l2 cache */
495   1,                    /* streams */
496 };
497
498 /* Instruction costs on MPCCORE processors.  */
499 static const
500 struct processor_costs mpccore_cost = {
501   COSTS_N_INSNS (2),    /* mulsi */
502   COSTS_N_INSNS (2),    /* mulsi_const */
503   COSTS_N_INSNS (2),    /* mulsi_const9 */
504   COSTS_N_INSNS (2),    /* muldi */
505   COSTS_N_INSNS (6),    /* divsi */
506   COSTS_N_INSNS (6),    /* divdi */
507   COSTS_N_INSNS (4),    /* fp */
508   COSTS_N_INSNS (5),    /* dmul */
509   COSTS_N_INSNS (10),   /* sdiv */
510   COSTS_N_INSNS (17),   /* ddiv */
511   32,                   /* cache line size */
512   4,                    /* l1 cache */
513   16,                   /* l2 cache */
514   1,                    /* streams */
515 };
516
517 /* Instruction costs on PPC403 processors.  */
518 static const
519 struct processor_costs ppc403_cost = {
520   COSTS_N_INSNS (4),    /* mulsi */
521   COSTS_N_INSNS (4),    /* mulsi_const */
522   COSTS_N_INSNS (4),    /* mulsi_const9 */
523   COSTS_N_INSNS (4),    /* muldi */
524   COSTS_N_INSNS (33),   /* divsi */
525   COSTS_N_INSNS (33),   /* divdi */
526   COSTS_N_INSNS (11),   /* fp */
527   COSTS_N_INSNS (11),   /* dmul */
528   COSTS_N_INSNS (11),   /* sdiv */
529   COSTS_N_INSNS (11),   /* ddiv */
530   32,                   /* cache line size */
531   4,                    /* l1 cache */
532   16,                   /* l2 cache */
533   1,                    /* streams */
534 };
535
536 /* Instruction costs on PPC405 processors.  */
537 static const
538 struct processor_costs ppc405_cost = {
539   COSTS_N_INSNS (5),    /* mulsi */
540   COSTS_N_INSNS (4),    /* mulsi_const */
541   COSTS_N_INSNS (3),    /* mulsi_const9 */
542   COSTS_N_INSNS (5),    /* muldi */
543   COSTS_N_INSNS (35),   /* divsi */
544   COSTS_N_INSNS (35),   /* divdi */
545   COSTS_N_INSNS (11),   /* fp */
546   COSTS_N_INSNS (11),   /* dmul */
547   COSTS_N_INSNS (11),   /* sdiv */
548   COSTS_N_INSNS (11),   /* ddiv */
549   32,                   /* cache line size */
550   16,                   /* l1 cache */
551   128,                  /* l2 cache */
552   1,                    /* streams */
553 };
554
555 /* Instruction costs on PPC440 processors.  */
556 static const
557 struct processor_costs ppc440_cost = {
558   COSTS_N_INSNS (3),    /* mulsi */
559   COSTS_N_INSNS (2),    /* mulsi_const */
560   COSTS_N_INSNS (2),    /* mulsi_const9 */
561   COSTS_N_INSNS (3),    /* muldi */
562   COSTS_N_INSNS (34),   /* divsi */
563   COSTS_N_INSNS (34),   /* divdi */
564   COSTS_N_INSNS (5),    /* fp */
565   COSTS_N_INSNS (5),    /* dmul */
566   COSTS_N_INSNS (19),   /* sdiv */
567   COSTS_N_INSNS (33),   /* ddiv */
568   32,                   /* cache line size */
569   32,                   /* l1 cache */
570   256,                  /* l2 cache */
571   1,                    /* streams */
572 };
573
574 /* Instruction costs on PPC476 processors.  */
575 static const
576 struct processor_costs ppc476_cost = {
577   COSTS_N_INSNS (4),    /* mulsi */
578   COSTS_N_INSNS (4),    /* mulsi_const */
579   COSTS_N_INSNS (4),    /* mulsi_const9 */
580   COSTS_N_INSNS (4),    /* muldi */
581   COSTS_N_INSNS (11),   /* divsi */
582   COSTS_N_INSNS (11),   /* divdi */
583   COSTS_N_INSNS (6),    /* fp */
584   COSTS_N_INSNS (6),    /* dmul */
585   COSTS_N_INSNS (19),   /* sdiv */
586   COSTS_N_INSNS (33),   /* ddiv */
587   32,                   /* l1 cache line size */
588   32,                   /* l1 cache */
589   512,                  /* l2 cache */
590   1,                    /* streams */
591 };
592
593 /* Instruction costs on PPC601 processors.  */
594 static const
595 struct processor_costs ppc601_cost = {
596   COSTS_N_INSNS (5),    /* mulsi */
597   COSTS_N_INSNS (5),    /* mulsi_const */
598   COSTS_N_INSNS (5),    /* mulsi_const9 */
599   COSTS_N_INSNS (5),    /* muldi */
600   COSTS_N_INSNS (36),   /* divsi */
601   COSTS_N_INSNS (36),   /* divdi */
602   COSTS_N_INSNS (4),    /* fp */
603   COSTS_N_INSNS (5),    /* dmul */
604   COSTS_N_INSNS (17),   /* sdiv */
605   COSTS_N_INSNS (31),   /* ddiv */
606   32,                   /* cache line size */
607   32,                   /* l1 cache */
608   256,                  /* l2 cache */
609   1,                    /* streams */
610 };
611
612 /* Instruction costs on PPC603 processors.  */
613 static const
614 struct processor_costs ppc603_cost = {
615   COSTS_N_INSNS (5),    /* mulsi */
616   COSTS_N_INSNS (3),    /* mulsi_const */
617   COSTS_N_INSNS (2),    /* mulsi_const9 */
618   COSTS_N_INSNS (5),    /* muldi */
619   COSTS_N_INSNS (37),   /* divsi */
620   COSTS_N_INSNS (37),   /* divdi */
621   COSTS_N_INSNS (3),    /* fp */
622   COSTS_N_INSNS (4),    /* dmul */
623   COSTS_N_INSNS (18),   /* sdiv */
624   COSTS_N_INSNS (33),   /* ddiv */
625   32,                   /* cache line size */
626   8,                    /* l1 cache */
627   64,                   /* l2 cache */
628   1,                    /* streams */
629 };
630
631 /* Instruction costs on PPC604 processors.  */
632 static const
633 struct processor_costs ppc604_cost = {
634   COSTS_N_INSNS (4),    /* mulsi */
635   COSTS_N_INSNS (4),    /* mulsi_const */
636   COSTS_N_INSNS (4),    /* mulsi_const9 */
637   COSTS_N_INSNS (4),    /* muldi */
638   COSTS_N_INSNS (20),   /* divsi */
639   COSTS_N_INSNS (20),   /* divdi */
640   COSTS_N_INSNS (3),    /* fp */
641   COSTS_N_INSNS (3),    /* dmul */
642   COSTS_N_INSNS (18),   /* sdiv */
643   COSTS_N_INSNS (32),   /* ddiv */
644   32,                   /* cache line size */
645   16,                   /* l1 cache */
646   512,                  /* l2 cache */
647   1,                    /* streams */
648 };
649
650 /* Instruction costs on PPC604e processors.  */
651 static const
652 struct processor_costs ppc604e_cost = {
653   COSTS_N_INSNS (2),    /* mulsi */
654   COSTS_N_INSNS (2),    /* mulsi_const */
655   COSTS_N_INSNS (2),    /* mulsi_const9 */
656   COSTS_N_INSNS (2),    /* muldi */
657   COSTS_N_INSNS (20),   /* divsi */
658   COSTS_N_INSNS (20),   /* divdi */
659   COSTS_N_INSNS (3),    /* fp */
660   COSTS_N_INSNS (3),    /* dmul */
661   COSTS_N_INSNS (18),   /* sdiv */
662   COSTS_N_INSNS (32),   /* ddiv */
663   32,                   /* cache line size */
664   32,                   /* l1 cache */
665   1024,                 /* l2 cache */
666   1,                    /* streams */
667 };
668
669 /* Instruction costs on PPC620 processors.  */
670 static const
671 struct processor_costs ppc620_cost = {
672   COSTS_N_INSNS (5),    /* mulsi */
673   COSTS_N_INSNS (4),    /* mulsi_const */
674   COSTS_N_INSNS (3),    /* mulsi_const9 */
675   COSTS_N_INSNS (7),    /* muldi */
676   COSTS_N_INSNS (21),   /* divsi */
677   COSTS_N_INSNS (37),   /* divdi */
678   COSTS_N_INSNS (3),    /* fp */
679   COSTS_N_INSNS (3),    /* dmul */
680   COSTS_N_INSNS (18),   /* sdiv */
681   COSTS_N_INSNS (32),   /* ddiv */
682   128,                  /* cache line size */
683   32,                   /* l1 cache */
684   1024,                 /* l2 cache */
685   1,                    /* streams */
686 };
687
688 /* Instruction costs on PPC630 processors.  */
689 static const
690 struct processor_costs ppc630_cost = {
691   COSTS_N_INSNS (5),    /* mulsi */
692   COSTS_N_INSNS (4),    /* mulsi_const */
693   COSTS_N_INSNS (3),    /* mulsi_const9 */
694   COSTS_N_INSNS (7),    /* muldi */
695   COSTS_N_INSNS (21),   /* divsi */
696   COSTS_N_INSNS (37),   /* divdi */
697   COSTS_N_INSNS (3),    /* fp */
698   COSTS_N_INSNS (3),    /* dmul */
699   COSTS_N_INSNS (17),   /* sdiv */
700   COSTS_N_INSNS (21),   /* ddiv */
701   128,                  /* cache line size */
702   64,                   /* l1 cache */
703   1024,                 /* l2 cache */
704   1,                    /* streams */
705 };
706
707 /* Instruction costs on Cell processor.  */
708 /* COSTS_N_INSNS (1) ~ one add.  */
709 static const
710 struct processor_costs ppccell_cost = {
711   COSTS_N_INSNS (9/2)+2,    /* mulsi */
712   COSTS_N_INSNS (6/2),    /* mulsi_const */
713   COSTS_N_INSNS (6/2),    /* mulsi_const9 */
714   COSTS_N_INSNS (15/2)+2,   /* muldi */
715   COSTS_N_INSNS (38/2),   /* divsi */
716   COSTS_N_INSNS (70/2),   /* divdi */
717   COSTS_N_INSNS (10/2),   /* fp */
718   COSTS_N_INSNS (10/2),   /* dmul */
719   COSTS_N_INSNS (74/2),   /* sdiv */
720   COSTS_N_INSNS (74/2),   /* ddiv */
721   128,                  /* cache line size */
722   32,                   /* l1 cache */
723   512,                  /* l2 cache */
724   6,                    /* streams */
725 };
726
727 /* Instruction costs on PPC750 and PPC7400 processors.  */
728 static const
729 struct processor_costs ppc750_cost = {
730   COSTS_N_INSNS (5),    /* mulsi */
731   COSTS_N_INSNS (3),    /* mulsi_const */
732   COSTS_N_INSNS (2),    /* mulsi_const9 */
733   COSTS_N_INSNS (5),    /* muldi */
734   COSTS_N_INSNS (17),   /* divsi */
735   COSTS_N_INSNS (17),   /* divdi */
736   COSTS_N_INSNS (3),    /* fp */
737   COSTS_N_INSNS (3),    /* dmul */
738   COSTS_N_INSNS (17),   /* sdiv */
739   COSTS_N_INSNS (31),   /* ddiv */
740   32,                   /* cache line size */
741   32,                   /* l1 cache */
742   512,                  /* l2 cache */
743   1,                    /* streams */
744 };
745
746 /* Instruction costs on PPC7450 processors.  */
747 static const
748 struct processor_costs ppc7450_cost = {
749   COSTS_N_INSNS (4),    /* mulsi */
750   COSTS_N_INSNS (3),    /* mulsi_const */
751   COSTS_N_INSNS (3),    /* mulsi_const9 */
752   COSTS_N_INSNS (4),    /* muldi */
753   COSTS_N_INSNS (23),   /* divsi */
754   COSTS_N_INSNS (23),   /* divdi */
755   COSTS_N_INSNS (5),    /* fp */
756   COSTS_N_INSNS (5),    /* dmul */
757   COSTS_N_INSNS (21),   /* sdiv */
758   COSTS_N_INSNS (35),   /* ddiv */
759   32,                   /* cache line size */
760   32,                   /* l1 cache */
761   1024,                 /* l2 cache */
762   1,                    /* streams */
763 };
764
765 /* Instruction costs on PPC8540 processors.  */
766 static const
767 struct processor_costs ppc8540_cost = {
768   COSTS_N_INSNS (4),    /* mulsi */
769   COSTS_N_INSNS (4),    /* mulsi_const */
770   COSTS_N_INSNS (4),    /* mulsi_const9 */
771   COSTS_N_INSNS (4),    /* muldi */
772   COSTS_N_INSNS (19),   /* divsi */
773   COSTS_N_INSNS (19),   /* divdi */
774   COSTS_N_INSNS (4),    /* fp */
775   COSTS_N_INSNS (4),    /* dmul */
776   COSTS_N_INSNS (29),   /* sdiv */
777   COSTS_N_INSNS (29),   /* ddiv */
778   32,                   /* cache line size */
779   32,                   /* l1 cache */
780   256,                  /* l2 cache */
781   1,                    /* prefetch streams /*/
782 };
783
784 /* Instruction costs on E300C2 and E300C3 cores.  */
785 static const
786 struct processor_costs ppce300c2c3_cost = {
787   COSTS_N_INSNS (4),    /* mulsi */
788   COSTS_N_INSNS (4),    /* mulsi_const */
789   COSTS_N_INSNS (4),    /* mulsi_const9 */
790   COSTS_N_INSNS (4),    /* muldi */
791   COSTS_N_INSNS (19),   /* divsi */
792   COSTS_N_INSNS (19),   /* divdi */
793   COSTS_N_INSNS (3),    /* fp */
794   COSTS_N_INSNS (4),    /* dmul */
795   COSTS_N_INSNS (18),   /* sdiv */
796   COSTS_N_INSNS (33),   /* ddiv */
797   32,
798   16,                   /* l1 cache */
799   16,                   /* l2 cache */
800   1,                    /* prefetch streams /*/
801 };
802
803 /* Instruction costs on PPCE500MC processors.  */
804 static const
805 struct processor_costs ppce500mc_cost = {
806   COSTS_N_INSNS (4),    /* mulsi */
807   COSTS_N_INSNS (4),    /* mulsi_const */
808   COSTS_N_INSNS (4),    /* mulsi_const9 */
809   COSTS_N_INSNS (4),    /* muldi */
810   COSTS_N_INSNS (14),   /* divsi */
811   COSTS_N_INSNS (14),   /* divdi */
812   COSTS_N_INSNS (8),    /* fp */
813   COSTS_N_INSNS (10),   /* dmul */
814   COSTS_N_INSNS (36),   /* sdiv */
815   COSTS_N_INSNS (66),   /* ddiv */
816   64,                   /* cache line size */
817   32,                   /* l1 cache */
818   128,                  /* l2 cache */
819   1,                    /* prefetch streams /*/
820 };
821
822 /* Instruction costs on PPCE500MC64 processors.  */
823 static const
824 struct processor_costs ppce500mc64_cost = {
825   COSTS_N_INSNS (4),    /* mulsi */
826   COSTS_N_INSNS (4),    /* mulsi_const */
827   COSTS_N_INSNS (4),    /* mulsi_const9 */
828   COSTS_N_INSNS (4),    /* muldi */
829   COSTS_N_INSNS (14),   /* divsi */
830   COSTS_N_INSNS (14),   /* divdi */
831   COSTS_N_INSNS (4),    /* fp */
832   COSTS_N_INSNS (10),   /* dmul */
833   COSTS_N_INSNS (36),   /* sdiv */
834   COSTS_N_INSNS (66),   /* ddiv */
835   64,                   /* cache line size */
836   32,                   /* l1 cache */
837   128,                  /* l2 cache */
838   1,                    /* prefetch streams /*/
839 };
840
841 /* Instruction costs on AppliedMicro Titan processors.  */
842 static const
843 struct processor_costs titan_cost = {
844   COSTS_N_INSNS (5),    /* mulsi */
845   COSTS_N_INSNS (5),    /* mulsi_const */
846   COSTS_N_INSNS (5),    /* mulsi_const9 */
847   COSTS_N_INSNS (5),    /* muldi */
848   COSTS_N_INSNS (18),   /* divsi */
849   COSTS_N_INSNS (18),   /* divdi */
850   COSTS_N_INSNS (10),   /* fp */
851   COSTS_N_INSNS (10),   /* dmul */
852   COSTS_N_INSNS (46),   /* sdiv */
853   COSTS_N_INSNS (72),   /* ddiv */
854   32,                   /* cache line size */
855   32,                   /* l1 cache */
856   512,                  /* l2 cache */
857   1,                    /* prefetch streams /*/
858 };
859
860 /* Instruction costs on POWER4 and POWER5 processors.  */
861 static const
862 struct processor_costs power4_cost = {
863   COSTS_N_INSNS (3),    /* mulsi */
864   COSTS_N_INSNS (2),    /* mulsi_const */
865   COSTS_N_INSNS (2),    /* mulsi_const9 */
866   COSTS_N_INSNS (4),    /* muldi */
867   COSTS_N_INSNS (18),   /* divsi */
868   COSTS_N_INSNS (34),   /* divdi */
869   COSTS_N_INSNS (3),    /* fp */
870   COSTS_N_INSNS (3),    /* dmul */
871   COSTS_N_INSNS (17),   /* sdiv */
872   COSTS_N_INSNS (17),   /* ddiv */
873   128,                  /* cache line size */
874   32,                   /* l1 cache */
875   1024,                 /* l2 cache */
876   8,                    /* prefetch streams /*/
877 };
878
879 /* Instruction costs on POWER6 processors.  */
880 static const
881 struct processor_costs power6_cost = {
882   COSTS_N_INSNS (8),    /* mulsi */
883   COSTS_N_INSNS (8),    /* mulsi_const */
884   COSTS_N_INSNS (8),    /* mulsi_const9 */
885   COSTS_N_INSNS (8),    /* muldi */
886   COSTS_N_INSNS (22),   /* divsi */
887   COSTS_N_INSNS (28),   /* divdi */
888   COSTS_N_INSNS (3),    /* fp */
889   COSTS_N_INSNS (3),    /* dmul */
890   COSTS_N_INSNS (13),   /* sdiv */
891   COSTS_N_INSNS (16),   /* ddiv */
892   128,                  /* cache line size */
893   64,                   /* l1 cache */
894   2048,                 /* l2 cache */
895   16,                   /* prefetch streams */
896 };
897
898 /* Instruction costs on POWER7 processors.  */
899 static const
900 struct processor_costs power7_cost = {
901   COSTS_N_INSNS (2),    /* mulsi */
902   COSTS_N_INSNS (2),    /* mulsi_const */
903   COSTS_N_INSNS (2),    /* mulsi_const9 */
904   COSTS_N_INSNS (2),    /* muldi */
905   COSTS_N_INSNS (18),   /* divsi */
906   COSTS_N_INSNS (34),   /* divdi */
907   COSTS_N_INSNS (3),    /* fp */
908   COSTS_N_INSNS (3),    /* dmul */
909   COSTS_N_INSNS (13),   /* sdiv */
910   COSTS_N_INSNS (16),   /* ddiv */
911   128,                  /* cache line size */
912   32,                   /* l1 cache */
913   256,                  /* l2 cache */
914   12,                   /* prefetch streams */
915 };
916
917 /* Instruction costs on POWER A2 processors.  */
918 static const
919 struct processor_costs ppca2_cost = {
920   COSTS_N_INSNS (16),    /* mulsi */
921   COSTS_N_INSNS (16),    /* mulsi_const */
922   COSTS_N_INSNS (16),    /* mulsi_const9 */
923   COSTS_N_INSNS (16),   /* muldi */
924   COSTS_N_INSNS (22),   /* divsi */
925   COSTS_N_INSNS (28),   /* divdi */
926   COSTS_N_INSNS (3),    /* fp */
927   COSTS_N_INSNS (3),    /* dmul */
928   COSTS_N_INSNS (59),   /* sdiv */
929   COSTS_N_INSNS (72),   /* ddiv */
930   64,
931   16,                   /* l1 cache */
932   2048,                 /* l2 cache */
933   16,                   /* prefetch streams */
934 };
935
936 \f
937 /* Table that classifies rs6000 builtin functions (pure, const, etc.).  */
938 #undef RS6000_BUILTIN
939 #undef RS6000_BUILTIN_EQUATE
940 #define RS6000_BUILTIN(NAME, TYPE) TYPE,
941 #define RS6000_BUILTIN_EQUATE(NAME, VALUE)
942
943 static const enum rs6000_btc builtin_classify[(int)RS6000_BUILTIN_COUNT] =
944 {
945 #include "rs6000-builtin.def"
946 };
947
948 #undef RS6000_BUILTIN
949 #undef RS6000_BUILTIN_EQUATE
950
951 \f
952 static bool rs6000_function_ok_for_sibcall (tree, tree);
953 static const char *rs6000_invalid_within_doloop (const_rtx);
954 static bool rs6000_legitimate_address_p (enum machine_mode, rtx, bool);
955 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
956 static rtx rs6000_generate_compare (rtx, enum machine_mode);
957 static void rs6000_emit_stack_tie (void);
958 static void rs6000_frame_related (rtx, rtx, HOST_WIDE_INT, rtx, rtx);
959 static bool spe_func_has_64bit_regs_p (void);
960 static void emit_frame_save (rtx, rtx, enum machine_mode, unsigned int,
961                              int, HOST_WIDE_INT);
962 static rtx gen_frame_mem_offset (enum machine_mode, rtx, int);
963 static unsigned rs6000_hash_constant (rtx);
964 static unsigned toc_hash_function (const void *);
965 static int toc_hash_eq (const void *, const void *);
966 static bool reg_offset_addressing_ok_p (enum machine_mode);
967 static bool virtual_stack_registers_memory_p (rtx);
968 static bool constant_pool_expr_p (rtx);
969 static bool legitimate_small_data_p (enum machine_mode, rtx);
970 static bool legitimate_lo_sum_address_p (enum machine_mode, rtx, int);
971 static struct machine_function * rs6000_init_machine_status (void);
972 static bool rs6000_assemble_integer (rtx, unsigned int, int);
973 static bool no_global_regs_above (int, bool);
974 #ifdef HAVE_GAS_HIDDEN
975 static void rs6000_assemble_visibility (tree, int);
976 #endif
977 static int rs6000_ra_ever_killed (void);
978 static bool rs6000_attribute_takes_identifier_p (const_tree);
979 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
980 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
981 static bool rs6000_ms_bitfield_layout_p (const_tree);
982 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
983 static void rs6000_eliminate_indexed_memrefs (rtx operands[2]);
984 static const char *rs6000_mangle_type (const_tree);
985 static void rs6000_set_default_type_attributes (tree);
986 static rtx rs6000_savres_routine_sym (rs6000_stack_t *, bool, bool, bool);
987 static rtx rs6000_emit_stack_reset (rs6000_stack_t *, rtx, rtx, int, bool);
988 static rtx rs6000_make_savres_rtx (rs6000_stack_t *, rtx, int,
989                                    enum machine_mode, bool, bool, bool);
990 static bool rs6000_reg_live_or_pic_offset_p (int);
991 static tree rs6000_builtin_vectorized_function (tree, tree, tree);
992 static int rs6000_savres_strategy (rs6000_stack_t *, bool, int, int);
993 static void rs6000_restore_saved_cr (rtx, int);
994 static void rs6000_output_function_prologue (FILE *, HOST_WIDE_INT);
995 static void rs6000_output_function_epilogue (FILE *, HOST_WIDE_INT);
996 static void rs6000_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
997                                     tree);
998 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
999 static bool rs6000_return_in_memory (const_tree, const_tree);
1000 static rtx rs6000_function_value (const_tree, const_tree, bool);
1001 static void rs6000_file_start (void);
1002 #if TARGET_ELF
1003 static int rs6000_elf_reloc_rw_mask (void);
1004 static void rs6000_elf_asm_out_constructor (rtx, int);
1005 static void rs6000_elf_asm_out_destructor (rtx, int);
1006 static void rs6000_elf_end_indicate_exec_stack (void) ATTRIBUTE_UNUSED;
1007 static void rs6000_elf_asm_init_sections (void);
1008 static section *rs6000_elf_select_rtx_section (enum machine_mode, rtx,
1009                                                unsigned HOST_WIDE_INT);
1010 static void rs6000_elf_encode_section_info (tree, rtx, int)
1011      ATTRIBUTE_UNUSED;
1012 #endif
1013 static bool rs6000_use_blocks_for_constant_p (enum machine_mode, const_rtx);
1014 static void rs6000_alloc_sdmode_stack_slot (void);
1015 static void rs6000_instantiate_decls (void);
1016 #if TARGET_XCOFF
1017 static void rs6000_xcoff_asm_output_anchor (rtx);
1018 static void rs6000_xcoff_asm_globalize_label (FILE *, const char *);
1019 static void rs6000_xcoff_asm_init_sections (void);
1020 static int rs6000_xcoff_reloc_rw_mask (void);
1021 static void rs6000_xcoff_asm_named_section (const char *, unsigned int, tree);
1022 static section *rs6000_xcoff_select_section (tree, int,
1023                                              unsigned HOST_WIDE_INT);
1024 static void rs6000_xcoff_unique_section (tree, int);
1025 static section *rs6000_xcoff_select_rtx_section
1026   (enum machine_mode, rtx, unsigned HOST_WIDE_INT);
1027 static const char * rs6000_xcoff_strip_name_encoding (const char *);
1028 static unsigned int rs6000_xcoff_section_type_flags (tree, const char *, int);
1029 static void rs6000_xcoff_file_start (void);
1030 static void rs6000_xcoff_file_end (void);
1031 #endif
1032 static int rs6000_variable_issue (FILE *, int, rtx, int);
1033 static bool rs6000_rtx_costs (rtx, int, int, int *, bool);
1034 static bool rs6000_debug_rtx_costs (rtx, int, int, int *, bool);
1035 static int rs6000_debug_address_cost (rtx, bool);
1036 static int rs6000_adjust_cost (rtx, rtx, rtx, int);
1037 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
1038 static void rs6000_sched_init (FILE *, int, int);
1039 static bool is_microcoded_insn (rtx);
1040 static bool is_nonpipeline_insn (rtx);
1041 static bool is_cracked_insn (rtx);
1042 static bool is_branch_slot_insn (rtx);
1043 static bool is_load_insn (rtx);
1044 static rtx get_store_dest (rtx pat);
1045 static bool is_store_insn (rtx);
1046 static bool set_to_load_agen (rtx,rtx);
1047 static bool adjacent_mem_locations (rtx,rtx);
1048 static int rs6000_adjust_priority (rtx, int);
1049 static int rs6000_issue_rate (void);
1050 static bool rs6000_is_costly_dependence (dep_t, int, int);
1051 static rtx get_next_active_insn (rtx, rtx);
1052 static bool insn_terminates_group_p (rtx , enum group_termination);
1053 static bool insn_must_be_first_in_group (rtx);
1054 static bool insn_must_be_last_in_group (rtx);
1055 static bool is_costly_group (rtx *, rtx);
1056 static int force_new_group (int, FILE *, rtx *, rtx, bool *, int, int *);
1057 static int redefine_groups (FILE *, int, rtx, rtx);
1058 static int pad_groups (FILE *, int, rtx, rtx);
1059 static void rs6000_sched_finish (FILE *, int);
1060 static int rs6000_sched_reorder (FILE *, int, rtx *, int *, int);
1061 static int rs6000_sched_reorder2 (FILE *, int, rtx *, int *, int);
1062 static int rs6000_use_sched_lookahead (void);
1063 static int rs6000_use_sched_lookahead_guard (rtx);
1064 static void * rs6000_alloc_sched_context (void);
1065 static void rs6000_init_sched_context (void *, bool);
1066 static void rs6000_set_sched_context (void *);
1067 static void rs6000_free_sched_context (void *);
1068 static tree rs6000_builtin_reciprocal (unsigned int, bool, bool);
1069 static tree rs6000_builtin_mask_for_load (void);
1070 static tree rs6000_builtin_mul_widen_even (tree);
1071 static tree rs6000_builtin_mul_widen_odd (tree);
1072 static tree rs6000_builtin_conversion (unsigned int, tree, tree);
1073 static tree rs6000_builtin_vec_perm (tree, tree *);
1074 static bool rs6000_builtin_support_vector_misalignment (enum
1075                                                         machine_mode,
1076                                                         const_tree,
1077                                                         int, bool);
1078
1079 static void def_builtin (int, const char *, tree, int);
1080 static bool rs6000_vector_alignment_reachable (const_tree, bool);
1081 static void rs6000_init_builtins (void);
1082 static tree rs6000_builtin_decl (unsigned, bool);
1083
1084 static rtx rs6000_expand_unop_builtin (enum insn_code, tree, rtx);
1085 static rtx rs6000_expand_binop_builtin (enum insn_code, tree, rtx);
1086 static rtx rs6000_expand_ternop_builtin (enum insn_code, tree, rtx);
1087 static rtx rs6000_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
1088 static void altivec_init_builtins (void);
1089 static unsigned builtin_hash_function (const void *);
1090 static int builtin_hash_eq (const void *, const void *);
1091 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1092                                    enum machine_mode, enum machine_mode,
1093                                    enum rs6000_builtins, const char *name);
1094 static void rs6000_common_init_builtins (void);
1095 static void rs6000_init_libfuncs (void);
1096
1097 static void paired_init_builtins (void);
1098 static rtx paired_expand_builtin (tree, rtx, bool *);
1099 static rtx paired_expand_lv_builtin (enum insn_code, tree, rtx);
1100 static rtx paired_expand_stv_builtin (enum insn_code, tree);
1101 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1102
1103 static void enable_mask_for_builtins (struct builtin_description *, int,
1104                                       enum rs6000_builtins,
1105                                       enum rs6000_builtins);
1106 static void spe_init_builtins (void);
1107 static rtx spe_expand_builtin (tree, rtx, bool *);
1108 static rtx spe_expand_stv_builtin (enum insn_code, tree);
1109 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1110 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1111 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1112 static rs6000_stack_t *rs6000_stack_info (void);
1113 static void debug_stack_info (rs6000_stack_t *);
1114
1115 static rtx altivec_expand_builtin (tree, rtx, bool *);
1116 static rtx altivec_expand_ld_builtin (tree, rtx, bool *);
1117 static rtx altivec_expand_st_builtin (tree, rtx, bool *);
1118 static rtx altivec_expand_dst_builtin (tree, rtx, bool *);
1119 static rtx altivec_expand_abs_builtin (enum insn_code, tree, rtx);
1120 static rtx altivec_expand_predicate_builtin (enum insn_code, tree, rtx);
1121 static rtx altivec_expand_stv_builtin (enum insn_code, tree);
1122 static rtx altivec_expand_vec_init_builtin (tree, tree, rtx);
1123 static rtx altivec_expand_vec_set_builtin (tree);
1124 static rtx altivec_expand_vec_ext_builtin (tree, rtx);
1125 static int get_element_number (tree, tree);
1126 static bool rs6000_handle_option (size_t, const char *, int);
1127 static void rs6000_parse_tls_size_option (void);
1128 static void rs6000_parse_yes_no_option (const char *, const char *, int *);
1129 static int first_altivec_reg_to_save (void);
1130 static unsigned int compute_vrsave_mask (void);
1131 static void compute_save_world_info (rs6000_stack_t *info_ptr);
1132 static void is_altivec_return_reg (rtx, void *);
1133 static rtx generate_set_vrsave (rtx, rs6000_stack_t *, int);
1134 int easy_vector_constant (rtx, enum machine_mode);
1135 static rtx rs6000_dwarf_register_span (rtx);
1136 static void rs6000_init_dwarf_reg_sizes_extra (tree);
1137 static rtx rs6000_legitimize_address (rtx, rtx, enum machine_mode);
1138 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1139 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1140 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
1141 static rtx rs6000_delegitimize_address (rtx);
1142 static rtx rs6000_tls_get_addr (void);
1143 static rtx rs6000_got_sym (void);
1144 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1145 static const char *rs6000_get_some_local_dynamic_name (void);
1146 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1147 static rtx rs6000_complex_function_value (enum machine_mode);
1148 static rtx rs6000_spe_function_arg (CUMULATIVE_ARGS *,
1149                                     enum machine_mode, tree);
1150 static void rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *,
1151                                                       HOST_WIDE_INT);
1152 static void rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *,
1153                                                         tree, HOST_WIDE_INT);
1154 static void rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *,
1155                                               HOST_WIDE_INT,
1156                                               rtx[], int *);
1157 static void rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *,
1158                                                 const_tree, HOST_WIDE_INT,
1159                                                 rtx[], int *);
1160 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree, int, bool);
1161 static rtx rs6000_mixed_function_arg (enum machine_mode, tree, int);
1162 static void rs6000_move_block_from_reg (int regno, rtx x, int nregs);
1163 static void setup_incoming_varargs (CUMULATIVE_ARGS *,
1164                                     enum machine_mode, tree,
1165                                     int *, int);
1166 static bool rs6000_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
1167                                       const_tree, bool);
1168 static int rs6000_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
1169                                      tree, bool);
1170 static const char *invalid_arg_for_unprototyped_fn (const_tree, const_tree, const_tree);
1171 #if TARGET_MACHO
1172 static void macho_branch_islands (void);
1173 static int no_previous_def (tree function_name);
1174 static tree get_prev_label (tree function_name);
1175 static void rs6000_darwin_file_start (void);
1176 #endif
1177
1178 static tree rs6000_build_builtin_va_list (void);
1179 static void rs6000_va_start (tree, rtx);
1180 static tree rs6000_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
1181 static bool rs6000_must_pass_in_stack (enum machine_mode, const_tree);
1182 static bool rs6000_scalar_mode_supported_p (enum machine_mode);
1183 static bool rs6000_vector_mode_supported_p (enum machine_mode);
1184 static rtx rs6000_emit_vector_compare_inner (enum rtx_code, rtx, rtx);
1185 static rtx rs6000_emit_vector_compare (enum rtx_code, rtx, rtx,
1186                                        enum machine_mode);
1187 static tree rs6000_stack_protect_fail (void);
1188
1189 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1190                                              int, int *);
1191
1192 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1193                                                    int, int, int *);
1194
1195 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1196                                              int, int *)
1197   = rs6000_legitimize_reload_address;
1198
1199 static bool rs6000_mode_dependent_address_p (const_rtx);
1200 static bool rs6000_mode_dependent_address (const_rtx);
1201 static bool rs6000_debug_mode_dependent_address (const_rtx);
1202 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1203   = rs6000_mode_dependent_address;
1204
1205 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1206                                                      enum machine_mode, rtx);
1207 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1208                                                            enum machine_mode,
1209                                                            rtx);
1210 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1211                                                      enum machine_mode, rtx)
1212   = rs6000_secondary_reload_class;
1213
1214 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1215 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1216                                                            enum reg_class);
1217 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1218   = rs6000_preferred_reload_class;
1219
1220 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1221                                             enum machine_mode);
1222
1223 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1224                                                   enum reg_class,
1225                                                   enum machine_mode);
1226
1227 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1228                                             enum machine_mode)
1229   = rs6000_secondary_memory_needed;
1230
1231 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1232                                              enum machine_mode,
1233                                              enum reg_class);
1234 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1235                                                    enum machine_mode,
1236                                                    enum reg_class);
1237
1238 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1239                                              enum machine_mode,
1240                                              enum reg_class)
1241   = rs6000_cannot_change_mode_class;
1242
1243 static enum reg_class rs6000_secondary_reload (bool, rtx, enum reg_class,
1244                                                enum machine_mode,
1245                                                struct secondary_reload_info *);
1246
1247 static const enum reg_class *rs6000_ira_cover_classes (void);
1248
1249 const int INSN_NOT_AVAILABLE = -1;
1250 static enum machine_mode rs6000_eh_return_filter_mode (void);
1251 static bool rs6000_can_eliminate (const int, const int);
1252 static void rs6000_trampoline_init (rtx, tree, rtx);
1253
1254 /* Hash table stuff for keeping track of TOC entries.  */
1255
1256 struct GTY(()) toc_hash_struct
1257 {
1258   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1259      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
1260   rtx key;
1261   enum machine_mode key_mode;
1262   int labelno;
1263 };
1264
1265 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1266
1267 /* Hash table to keep track of the argument types for builtin functions.  */
1268
1269 struct GTY(()) builtin_hash_struct
1270 {
1271   tree type;
1272   enum machine_mode mode[4];    /* return value + 3 arguments.  */
1273   unsigned char uns_p[4];       /* and whether the types are unsigned.  */
1274 };
1275
1276 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1277 \f
1278 /* Default register names.  */
1279 char rs6000_reg_names[][8] =
1280 {
1281       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1282       "8",  "9", "10", "11", "12", "13", "14", "15",
1283      "16", "17", "18", "19", "20", "21", "22", "23",
1284      "24", "25", "26", "27", "28", "29", "30", "31",
1285       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1286       "8",  "9", "10", "11", "12", "13", "14", "15",
1287      "16", "17", "18", "19", "20", "21", "22", "23",
1288      "24", "25", "26", "27", "28", "29", "30", "31",
1289      "mq", "lr", "ctr","ap",
1290       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1291       "ca",
1292       /* AltiVec registers.  */
1293       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
1294       "8",  "9",  "10", "11", "12", "13", "14", "15",
1295       "16", "17", "18", "19", "20", "21", "22", "23",
1296       "24", "25", "26", "27", "28", "29", "30", "31",
1297       "vrsave", "vscr",
1298       /* SPE registers.  */
1299       "spe_acc", "spefscr",
1300       /* Soft frame pointer.  */
1301       "sfp"
1302 };
1303
1304 #ifdef TARGET_REGNAMES
1305 static const char alt_reg_names[][8] =
1306 {
1307    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
1308    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1309   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1310   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1311    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
1312    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1313   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1314   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1315     "mq",    "lr",  "ctr",   "ap",
1316   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1317    "ca",
1318   /* AltiVec registers.  */
1319    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
1320    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1321   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1322   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1323   "vrsave", "vscr",
1324   /* SPE registers.  */
1325   "spe_acc", "spefscr",
1326   /* Soft frame pointer.  */
1327   "sfp"
1328 };
1329 #endif
1330
1331 /* Table of valid machine attributes.  */
1332
1333 static const struct attribute_spec rs6000_attribute_table[] =
1334 {
1335   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1336   { "altivec",   1, 1, false, true,  false, rs6000_handle_altivec_attribute },
1337   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
1338   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
1339   { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute },
1340   { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute },
1341 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1342   SUBTARGET_ATTRIBUTE_TABLE,
1343 #endif
1344   { NULL,        0, 0, false, false, false, NULL }
1345 };
1346 \f
1347 #ifndef MASK_STRICT_ALIGN
1348 #define MASK_STRICT_ALIGN 0
1349 #endif
1350 #ifndef TARGET_PROFILE_KERNEL
1351 #define TARGET_PROFILE_KERNEL 0
1352 #endif
1353
1354 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
1355 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1356 \f
1357 /* Initialize the GCC target structure.  */
1358 #undef TARGET_ATTRIBUTE_TABLE
1359 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1360 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1361 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1362 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1363 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1364
1365 #undef TARGET_ASM_ALIGNED_DI_OP
1366 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1367
1368 /* Default unaligned ops are only provided for ELF.  Find the ops needed
1369    for non-ELF systems.  */
1370 #ifndef OBJECT_FORMAT_ELF
1371 #if TARGET_XCOFF
1372 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
1373    64-bit targets.  */
1374 #undef TARGET_ASM_UNALIGNED_HI_OP
1375 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1376 #undef TARGET_ASM_UNALIGNED_SI_OP
1377 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1378 #undef TARGET_ASM_UNALIGNED_DI_OP
1379 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1380 #else
1381 /* For Darwin.  */
1382 #undef TARGET_ASM_UNALIGNED_HI_OP
1383 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1384 #undef TARGET_ASM_UNALIGNED_SI_OP
1385 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1386 #undef TARGET_ASM_UNALIGNED_DI_OP
1387 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1388 #undef TARGET_ASM_ALIGNED_DI_OP
1389 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1390 #endif
1391 #endif
1392
1393 /* This hook deals with fixups for relocatable code and DI-mode objects
1394    in 64-bit code.  */
1395 #undef TARGET_ASM_INTEGER
1396 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1397
1398 #ifdef HAVE_GAS_HIDDEN
1399 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1400 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1401 #endif
1402
1403 #undef TARGET_HAVE_TLS
1404 #define TARGET_HAVE_TLS HAVE_AS_TLS
1405
1406 #undef TARGET_CANNOT_FORCE_CONST_MEM
1407 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_tls_referenced_p
1408
1409 #undef TARGET_DELEGITIMIZE_ADDRESS
1410 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1411
1412 #undef TARGET_ASM_FUNCTION_PROLOGUE
1413 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1414 #undef TARGET_ASM_FUNCTION_EPILOGUE
1415 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1416
1417 #undef TARGET_LEGITIMIZE_ADDRESS
1418 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1419
1420 #undef  TARGET_SCHED_VARIABLE_ISSUE
1421 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1422
1423 #undef TARGET_SCHED_ISSUE_RATE
1424 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1425 #undef TARGET_SCHED_ADJUST_COST
1426 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1427 #undef TARGET_SCHED_ADJUST_PRIORITY
1428 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1429 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1430 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1431 #undef TARGET_SCHED_INIT
1432 #define TARGET_SCHED_INIT rs6000_sched_init
1433 #undef TARGET_SCHED_FINISH
1434 #define TARGET_SCHED_FINISH rs6000_sched_finish
1435 #undef TARGET_SCHED_REORDER
1436 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1437 #undef TARGET_SCHED_REORDER2
1438 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1439
1440 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1441 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1442
1443 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1444 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1445
1446 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1447 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1448 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1449 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1450 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1451 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1452 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1453 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1454
1455 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1456 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1457 #undef TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_EVEN
1458 #define TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_EVEN rs6000_builtin_mul_widen_even
1459 #undef TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_ODD
1460 #define TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_ODD rs6000_builtin_mul_widen_odd
1461 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
1462 #define TARGET_VECTORIZE_BUILTIN_CONVERSION rs6000_builtin_conversion
1463 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM
1464 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM rs6000_builtin_vec_perm
1465 #undef TARGET_SUPPORT_VECTOR_MISALIGNMENT
1466 #define TARGET_SUPPORT_VECTOR_MISALIGNMENT              \
1467   rs6000_builtin_support_vector_misalignment
1468 #undef TARGET_VECTOR_ALIGNMENT_REACHABLE
1469 #define TARGET_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1470
1471 #undef TARGET_INIT_BUILTINS
1472 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1473 #undef TARGET_BUILTIN_DECL
1474 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1475
1476 #undef TARGET_EXPAND_BUILTIN
1477 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1478
1479 #undef TARGET_MANGLE_TYPE
1480 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1481
1482 #undef TARGET_INIT_LIBFUNCS
1483 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1484
1485 #if TARGET_MACHO
1486 #undef TARGET_BINDS_LOCAL_P
1487 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1488 #endif
1489
1490 #undef TARGET_MS_BITFIELD_LAYOUT_P
1491 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1492
1493 #undef TARGET_ASM_OUTPUT_MI_THUNK
1494 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1495
1496 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1497 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1498
1499 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1500 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1501
1502 #undef TARGET_INVALID_WITHIN_DOLOOP
1503 #define TARGET_INVALID_WITHIN_DOLOOP rs6000_invalid_within_doloop
1504
1505 #undef TARGET_RTX_COSTS
1506 #define TARGET_RTX_COSTS rs6000_rtx_costs
1507 #undef TARGET_ADDRESS_COST
1508 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
1509
1510 #undef TARGET_DWARF_REGISTER_SPAN
1511 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1512
1513 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1514 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1515
1516 /* On rs6000, function arguments are promoted, as are function return
1517    values.  */
1518 #undef TARGET_PROMOTE_FUNCTION_MODE
1519 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1520
1521 #undef TARGET_RETURN_IN_MEMORY
1522 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1523
1524 #undef TARGET_SETUP_INCOMING_VARARGS
1525 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1526
1527 /* Always strict argument naming on rs6000.  */
1528 #undef TARGET_STRICT_ARGUMENT_NAMING
1529 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1530 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1531 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1532 #undef TARGET_SPLIT_COMPLEX_ARG
1533 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1534 #undef TARGET_MUST_PASS_IN_STACK
1535 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1536 #undef TARGET_PASS_BY_REFERENCE
1537 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1538 #undef TARGET_ARG_PARTIAL_BYTES
1539 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1540
1541 #undef TARGET_BUILD_BUILTIN_VA_LIST
1542 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1543
1544 #undef TARGET_EXPAND_BUILTIN_VA_START
1545 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1546
1547 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1548 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1549
1550 #undef TARGET_EH_RETURN_FILTER_MODE
1551 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1552
1553 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1554 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1555
1556 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1557 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1558
1559 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1560 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1561
1562 #undef TARGET_HANDLE_OPTION
1563 #define TARGET_HANDLE_OPTION rs6000_handle_option
1564
1565 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1566 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1567   rs6000_builtin_vectorized_function
1568
1569 #undef TARGET_DEFAULT_TARGET_FLAGS
1570 #define TARGET_DEFAULT_TARGET_FLAGS \
1571   (TARGET_DEFAULT)
1572
1573 #undef TARGET_STACK_PROTECT_FAIL
1574 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1575
1576 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1577    The PowerPC architecture requires only weak consistency among
1578    processors--that is, memory accesses between processors need not be
1579    sequentially consistent and memory accesses among processors can occur
1580    in any order. The ability to order memory accesses weakly provides
1581    opportunities for more efficient use of the system bus. Unless a
1582    dependency exists, the 604e allows read operations to precede store
1583    operations.  */
1584 #undef TARGET_RELAXED_ORDERING
1585 #define TARGET_RELAXED_ORDERING true
1586
1587 #ifdef HAVE_AS_TLS
1588 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1589 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1590 #endif
1591
1592 /* Use a 32-bit anchor range.  This leads to sequences like:
1593
1594         addis   tmp,anchor,high
1595         add     dest,tmp,low
1596
1597    where tmp itself acts as an anchor, and can be shared between
1598    accesses to the same 64k page.  */
1599 #undef TARGET_MIN_ANCHOR_OFFSET
1600 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1601 #undef TARGET_MAX_ANCHOR_OFFSET
1602 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1603 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1604 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1605
1606 #undef TARGET_BUILTIN_RECIPROCAL
1607 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1608
1609 #undef TARGET_EXPAND_TO_RTL_HOOK
1610 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1611
1612 #undef TARGET_INSTANTIATE_DECLS
1613 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1614
1615 #undef TARGET_SECONDARY_RELOAD
1616 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1617
1618 #undef TARGET_IRA_COVER_CLASSES
1619 #define TARGET_IRA_COVER_CLASSES rs6000_ira_cover_classes
1620
1621 #undef TARGET_LEGITIMATE_ADDRESS_P
1622 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1623
1624 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1625 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1626
1627 #undef TARGET_CAN_ELIMINATE
1628 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1629
1630 #undef TARGET_TRAMPOLINE_INIT
1631 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1632
1633 #undef TARGET_FUNCTION_VALUE
1634 #define TARGET_FUNCTION_VALUE rs6000_function_value
1635
1636 struct gcc_target targetm = TARGET_INITIALIZER;
1637 \f
1638 /* Return number of consecutive hard regs needed starting at reg REGNO
1639    to hold something of mode MODE.
1640    This is ordinarily the length in words of a value of mode MODE
1641    but can be less for certain modes in special long registers.
1642
1643    For the SPE, GPRs are 64 bits but only 32 bits are visible in
1644    scalar instructions.  The upper 32 bits are only available to the
1645    SIMD instructions.
1646
1647    POWER and PowerPC GPRs hold 32 bits worth;
1648    PowerPC64 GPRs and FPRs point register holds 64 bits worth.  */
1649
1650 static int
1651 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1652 {
1653   unsigned HOST_WIDE_INT reg_size;
1654
1655   if (FP_REGNO_P (regno))
1656     reg_size = (VECTOR_MEM_VSX_P (mode)
1657                 ? UNITS_PER_VSX_WORD
1658                 : UNITS_PER_FP_WORD);
1659
1660   else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1661     reg_size = UNITS_PER_SPE_WORD;
1662
1663   else if (ALTIVEC_REGNO_P (regno))
1664     reg_size = UNITS_PER_ALTIVEC_WORD;
1665
1666   /* The value returned for SCmode in the E500 double case is 2 for
1667      ABI compatibility; storing an SCmode value in a single register
1668      would require function_arg and rs6000_spe_function_arg to handle
1669      SCmode so as to pass the value correctly in a pair of
1670      registers.  */
1671   else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1672            && !DECIMAL_FLOAT_MODE_P (mode))
1673     reg_size = UNITS_PER_FP_WORD;
1674
1675   else
1676     reg_size = UNITS_PER_WORD;
1677
1678   return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1679 }
1680
1681 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1682    MODE.  */
1683 static int
1684 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1685 {
1686   int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1687
1688   /* VSX registers that overlap the FPR registers are larger than for non-VSX
1689      implementations.  Don't allow an item to be split between a FP register
1690      and an Altivec register.  */
1691   if (VECTOR_MEM_VSX_P (mode))
1692     {
1693       if (FP_REGNO_P (regno))
1694         return FP_REGNO_P (last_regno);
1695
1696       if (ALTIVEC_REGNO_P (regno))
1697         return ALTIVEC_REGNO_P (last_regno);
1698     }
1699
1700   /* The GPRs can hold any mode, but values bigger than one register
1701      cannot go past R31.  */
1702   if (INT_REGNO_P (regno))
1703     return INT_REGNO_P (last_regno);
1704
1705   /* The float registers (except for VSX vector modes) can only hold floating
1706      modes and DImode.  This excludes the 32-bit decimal float mode for
1707      now.  */
1708   if (FP_REGNO_P (regno))
1709     {
1710       if (SCALAR_FLOAT_MODE_P (mode)
1711           && (mode != TDmode || (regno % 2) == 0)
1712           && FP_REGNO_P (last_regno))
1713         return 1;
1714
1715       if (GET_MODE_CLASS (mode) == MODE_INT
1716           && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1717         return 1;
1718
1719       if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1720           && PAIRED_VECTOR_MODE (mode))
1721         return 1;
1722
1723       return 0;
1724     }
1725
1726   /* The CR register can only hold CC modes.  */
1727   if (CR_REGNO_P (regno))
1728     return GET_MODE_CLASS (mode) == MODE_CC;
1729
1730   if (CA_REGNO_P (regno))
1731     return mode == BImode;
1732
1733   /* AltiVec only in AldyVec registers.  */
1734   if (ALTIVEC_REGNO_P (regno))
1735     return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1736
1737   /* ...but GPRs can hold SIMD data on the SPE in one register.  */
1738   if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1739     return 1;
1740
1741   /* We cannot put TImode anywhere except general register and it must be able
1742      to fit within the register set.  In the future, allow TImode in the
1743      Altivec or VSX registers.  */
1744
1745   return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1746 }
1747
1748 /* Print interesting facts about registers.  */
1749 static void
1750 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1751 {
1752   int r, m;
1753
1754   for (r = first_regno; r <= last_regno; ++r)
1755     {
1756       const char *comma = "";
1757       int len;
1758
1759       if (first_regno == last_regno)
1760         fprintf (stderr, "%s:\t", reg_name);
1761       else
1762         fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1763
1764       len = 8;
1765       for (m = 0; m < NUM_MACHINE_MODES; ++m)
1766         if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1767           {
1768             if (len > 70)
1769               {
1770                 fprintf (stderr, ",\n\t");
1771                 len = 8;
1772                 comma = "";
1773               }
1774
1775             if (rs6000_hard_regno_nregs[m][r] > 1)
1776               len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1777                              rs6000_hard_regno_nregs[m][r]);
1778             else
1779               len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1780
1781             comma = ", ";
1782           }
1783
1784       if (call_used_regs[r])
1785         {
1786           if (len > 70)
1787             {
1788               fprintf (stderr, ",\n\t");
1789               len = 8;
1790               comma = "";
1791             }
1792
1793           len += fprintf (stderr, "%s%s", comma, "call-used");
1794           comma = ", ";
1795         }
1796
1797       if (fixed_regs[r])
1798         {
1799           if (len > 70)
1800             {
1801               fprintf (stderr, ",\n\t");
1802               len = 8;
1803               comma = "";
1804             }
1805
1806           len += fprintf (stderr, "%s%s", comma, "fixed");
1807           comma = ", ";
1808         }
1809
1810       if (len > 70)
1811         {
1812           fprintf (stderr, ",\n\t");
1813           comma = "";
1814         }
1815
1816       fprintf (stderr, "%sregno = %d\n", comma, r);
1817     }
1818 }
1819
1820 /* Print various interesting information with -mdebug=reg.  */
1821 static void
1822 rs6000_debug_reg_global (void)
1823 {
1824   const char *nl = (const char *)0;
1825   int m;
1826   char costly_num[20];
1827   char nop_num[20];
1828   const char *costly_str;
1829   const char *nop_str;
1830
1831   /* Map enum rs6000_vector to string.  */
1832   static const char *rs6000_debug_vector_unit[] = {
1833     "none",
1834     "altivec",
1835     "vsx",
1836     "paired",
1837     "spe",
1838     "other"
1839   };
1840
1841   fprintf (stderr, "Register information: (last virtual reg = %d)\n",
1842            LAST_VIRTUAL_REGISTER);
1843   rs6000_debug_reg_print (0, 31, "gr");
1844   rs6000_debug_reg_print (32, 63, "fp");
1845   rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1846                           LAST_ALTIVEC_REGNO,
1847                           "vs");
1848   rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1849   rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1850   rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1851   rs6000_debug_reg_print (MQ_REGNO, MQ_REGNO, "mq");
1852   rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1853   rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1854   rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1855   rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1856   rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1857
1858   fprintf (stderr,
1859            "\n"
1860            "d  reg_class = %s\n"
1861            "f  reg_class = %s\n"
1862            "v  reg_class = %s\n"
1863            "wa reg_class = %s\n"
1864            "wd reg_class = %s\n"
1865            "wf reg_class = %s\n"
1866            "ws reg_class = %s\n\n",
1867            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1868            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1869            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1870            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1871            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1872            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1873            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]]);
1874
1875   for (m = 0; m < NUM_MACHINE_MODES; ++m)
1876     if (rs6000_vector_unit[m] || rs6000_vector_mem[m])
1877       {
1878         nl = "\n";
1879         fprintf (stderr, "Vector mode: %-5s arithmetic: %-8s move: %-8s\n",
1880                  GET_MODE_NAME (m),
1881                  rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
1882                  rs6000_debug_vector_unit[ rs6000_vector_mem[m] ]);
1883       }
1884
1885   if (nl)
1886     fputs (nl, stderr);
1887
1888   if (rs6000_recip_control)
1889     {
1890       fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
1891
1892       for (m = 0; m < NUM_MACHINE_MODES; ++m)
1893         if (rs6000_recip_bits[m])
1894           {
1895             fprintf (stderr,
1896                      "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
1897                      GET_MODE_NAME (m),
1898                      (RS6000_RECIP_AUTO_RE_P (m)
1899                       ? "auto"
1900                       : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
1901                      (RS6000_RECIP_AUTO_RSQRTE_P (m)
1902                       ? "auto"
1903                       : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
1904           }
1905
1906       fputs ("\n", stderr);
1907     }
1908
1909   switch (rs6000_sched_costly_dep)
1910     {
1911     case max_dep_latency:
1912       costly_str = "max_dep_latency";
1913       break;
1914
1915     case no_dep_costly:
1916       costly_str = "no_dep_costly";
1917       break;
1918
1919     case all_deps_costly:
1920       costly_str = "all_deps_costly";
1921       break;
1922
1923     case true_store_to_load_dep_costly:
1924       costly_str = "true_store_to_load_dep_costly";
1925       break;
1926
1927     case store_to_load_dep_costly:
1928       costly_str = "store_to_load_dep_costly";
1929       break;
1930
1931     default:
1932       costly_str = costly_num;
1933       sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
1934       break;
1935     }
1936
1937   switch (rs6000_sched_insert_nops)
1938     {
1939     case sched_finish_regroup_exact:
1940       nop_str = "sched_finish_regroup_exact";
1941       break;
1942
1943     case sched_finish_pad_groups:
1944       nop_str = "sched_finish_pad_groups";
1945       break;
1946
1947     case sched_finish_none:
1948       nop_str = "sched_finish_none";
1949       break;
1950
1951     default:
1952       nop_str = nop_num;
1953       sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
1954       break;
1955     }
1956
1957   fprintf (stderr,
1958            "always_hint                     = %s\n"
1959            "align_branch_targets            = %s\n"
1960            "sched_restricted_insns_priority = %d\n"
1961            "sched_costly_dep                = %s\n"
1962            "sched_insert_nops               = %s\n\n",
1963            rs6000_always_hint ? "true" : "false",
1964            rs6000_align_branch_targets ? "true" : "false",
1965            (int)rs6000_sched_restricted_insns_priority,
1966            costly_str, nop_str);
1967 }
1968
1969 /* Initialize the various global tables that are based on register size.  */
1970 static void
1971 rs6000_init_hard_regno_mode_ok (void)
1972 {
1973   int r, m, c;
1974   int align64;
1975   int align32;
1976
1977   /* Precalculate REGNO_REG_CLASS.  */
1978   rs6000_regno_regclass[0] = GENERAL_REGS;
1979   for (r = 1; r < 32; ++r)
1980     rs6000_regno_regclass[r] = BASE_REGS;
1981
1982   for (r = 32; r < 64; ++r)
1983     rs6000_regno_regclass[r] = FLOAT_REGS;
1984
1985   for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
1986     rs6000_regno_regclass[r] = NO_REGS;
1987
1988   for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
1989     rs6000_regno_regclass[r] = ALTIVEC_REGS;
1990
1991   rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
1992   for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
1993     rs6000_regno_regclass[r] = CR_REGS;
1994
1995   rs6000_regno_regclass[MQ_REGNO] = MQ_REGS;
1996   rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
1997   rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
1998   rs6000_regno_regclass[CA_REGNO] = CA_REGS;
1999   rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2000   rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2001   rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2002   rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2003   rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2004   rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2005
2006   /* Precalculate vector information, this must be set up before the
2007      rs6000_hard_regno_nregs_internal below.  */
2008   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2009     {
2010       rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
2011       rs6000_vector_reload[m][0] = CODE_FOR_nothing;
2012       rs6000_vector_reload[m][1] = CODE_FOR_nothing;
2013     }
2014
2015   for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
2016     rs6000_constraints[c] = NO_REGS;
2017
2018   /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2019      believes it can use native alignment or still uses 128-bit alignment.  */
2020   if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2021     {
2022       align64 = 64;
2023       align32 = 32;
2024     }
2025   else
2026     {
2027       align64 = 128;
2028       align32 = 128;
2029     }
2030
2031   /* V2DF mode, VSX only.  */
2032   if (TARGET_VSX)
2033     {
2034       rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2035       rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2036       rs6000_vector_align[V2DFmode] = align64;
2037     }
2038
2039   /* V4SF mode, either VSX or Altivec.  */
2040   if (TARGET_VSX)
2041     {
2042       rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2043       rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2044       rs6000_vector_align[V4SFmode] = align32;
2045     }
2046   else if (TARGET_ALTIVEC)
2047     {
2048       rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2049       rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2050       rs6000_vector_align[V4SFmode] = align32;
2051     }
2052
2053   /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2054      and stores. */
2055   if (TARGET_ALTIVEC)
2056     {
2057       rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2058       rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2059       rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2060       rs6000_vector_align[V4SImode] = align32;
2061       rs6000_vector_align[V8HImode] = align32;
2062       rs6000_vector_align[V16QImode] = align32;
2063
2064       if (TARGET_VSX)
2065         {
2066           rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2067           rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2068           rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2069         }
2070       else
2071         {
2072           rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2073           rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2074           rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2075         }
2076     }
2077
2078   /* V2DImode, only allow under VSX, which can do V2DI insert/splat/extract.
2079      Altivec doesn't have 64-bit support.  */
2080   if (TARGET_VSX)
2081     {
2082       rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2083       rs6000_vector_unit[V2DImode] = VECTOR_NONE;
2084       rs6000_vector_align[V2DImode] = align64;
2085     }
2086
2087   /* DFmode, see if we want to use the VSX unit.  */
2088   if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2089     {
2090       rs6000_vector_unit[DFmode] = VECTOR_VSX;
2091       rs6000_vector_mem[DFmode]
2092         = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2093       rs6000_vector_align[DFmode] = align64;
2094     }
2095
2096   /* TODO add SPE and paired floating point vector support.  */
2097
2098   /* Register class constaints for the constraints that depend on compile
2099      switches.  */
2100   if (TARGET_HARD_FLOAT && TARGET_FPRS)
2101     rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2102
2103   if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2104     rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2105
2106   if (TARGET_VSX)
2107     {
2108       /* At present, we just use VSX_REGS, but we have different constraints
2109          based on the use, in case we want to fine tune the default register
2110          class used.  wa = any VSX register, wf = register class to use for
2111          V4SF, wd = register class to use for V2DF, and ws = register classs to
2112          use for DF scalars.  */
2113       rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2114       rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2115       rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2116       rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2117                                                   ? VSX_REGS
2118                                                   : FLOAT_REGS);
2119     }
2120
2121   if (TARGET_ALTIVEC)
2122     rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2123
2124   /* Set up the reload helper functions.  */
2125   if (TARGET_VSX || TARGET_ALTIVEC)
2126     {
2127       if (TARGET_64BIT)
2128         {
2129           rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2130           rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2131           rs6000_vector_reload[V8HImode][0]  = CODE_FOR_reload_v8hi_di_store;
2132           rs6000_vector_reload[V8HImode][1]  = CODE_FOR_reload_v8hi_di_load;
2133           rs6000_vector_reload[V4SImode][0]  = CODE_FOR_reload_v4si_di_store;
2134           rs6000_vector_reload[V4SImode][1]  = CODE_FOR_reload_v4si_di_load;
2135           rs6000_vector_reload[V2DImode][0]  = CODE_FOR_reload_v2di_di_store;
2136           rs6000_vector_reload[V2DImode][1]  = CODE_FOR_reload_v2di_di_load;
2137           rs6000_vector_reload[V4SFmode][0]  = CODE_FOR_reload_v4sf_di_store;
2138           rs6000_vector_reload[V4SFmode][1]  = CODE_FOR_reload_v4sf_di_load;
2139           rs6000_vector_reload[V2DFmode][0]  = CODE_FOR_reload_v2df_di_store;
2140           rs6000_vector_reload[V2DFmode][1]  = CODE_FOR_reload_v2df_di_load;
2141         }
2142       else
2143         {
2144           rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2145           rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2146           rs6000_vector_reload[V8HImode][0]  = CODE_FOR_reload_v8hi_si_store;
2147           rs6000_vector_reload[V8HImode][1]  = CODE_FOR_reload_v8hi_si_load;
2148           rs6000_vector_reload[V4SImode][0]  = CODE_FOR_reload_v4si_si_store;
2149           rs6000_vector_reload[V4SImode][1]  = CODE_FOR_reload_v4si_si_load;
2150           rs6000_vector_reload[V2DImode][0]  = CODE_FOR_reload_v2di_si_store;
2151           rs6000_vector_reload[V2DImode][1]  = CODE_FOR_reload_v2di_si_load;
2152           rs6000_vector_reload[V4SFmode][0]  = CODE_FOR_reload_v4sf_si_store;
2153           rs6000_vector_reload[V4SFmode][1]  = CODE_FOR_reload_v4sf_si_load;
2154           rs6000_vector_reload[V2DFmode][0]  = CODE_FOR_reload_v2df_si_store;
2155           rs6000_vector_reload[V2DFmode][1]  = CODE_FOR_reload_v2df_si_load;
2156         }
2157     }
2158
2159   /* Precalculate HARD_REGNO_NREGS.  */
2160   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2161     for (m = 0; m < NUM_MACHINE_MODES; ++m)
2162       rs6000_hard_regno_nregs[m][r]
2163         = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2164
2165   /* Precalculate HARD_REGNO_MODE_OK.  */
2166   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2167     for (m = 0; m < NUM_MACHINE_MODES; ++m)
2168       if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2169         rs6000_hard_regno_mode_ok_p[m][r] = true;
2170
2171   /* Precalculate CLASS_MAX_NREGS sizes.  */
2172   for (c = 0; c < LIM_REG_CLASSES; ++c)
2173     {
2174       int reg_size;
2175
2176       if (TARGET_VSX && VSX_REG_CLASS_P (c))
2177         reg_size = UNITS_PER_VSX_WORD;
2178
2179       else if (c == ALTIVEC_REGS)
2180         reg_size = UNITS_PER_ALTIVEC_WORD;
2181
2182       else if (c == FLOAT_REGS)
2183         reg_size = UNITS_PER_FP_WORD;
2184
2185       else
2186         reg_size = UNITS_PER_WORD;
2187
2188       for (m = 0; m < NUM_MACHINE_MODES; ++m)
2189         rs6000_class_max_nregs[m][c]
2190           = (GET_MODE_SIZE (m) + reg_size - 1) / reg_size;
2191     }
2192
2193   if (TARGET_E500_DOUBLE)
2194     rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2195
2196   /* Calculate which modes to automatically generate code to use a the
2197      reciprocal divide and square root instructions.  In the future, possibly
2198      automatically generate the instructions even if the user did not specify
2199      -mrecip.  The older machines double precision reciprocal sqrt estimate is
2200      not accurate enough.  */
2201   memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2202   if (TARGET_FRES)
2203     rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2204   if (TARGET_FRE)
2205     rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2206   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2207     rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2208   if (VECTOR_UNIT_VSX_P (V2DFmode))
2209     rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2210
2211   if (TARGET_FRSQRTES)
2212     rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2213   if (TARGET_FRSQRTE)
2214     rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2215   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2216     rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2217   if (VECTOR_UNIT_VSX_P (V2DFmode))
2218     rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2219
2220   if (rs6000_recip_control)
2221     {
2222       if (!TARGET_FUSED_MADD)
2223         warning (0, "-mrecip requires -mfused-madd");
2224       if (!flag_finite_math_only)
2225         warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2226       if (flag_trapping_math)
2227         warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2228       if (!flag_reciprocal_math)
2229         warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2230       if (TARGET_FUSED_MADD && flag_finite_math_only && !flag_trapping_math
2231           && flag_reciprocal_math)
2232         {
2233           if (RS6000_RECIP_HAVE_RE_P (SFmode)
2234               && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2235             rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2236
2237           if (RS6000_RECIP_HAVE_RE_P (DFmode)
2238               && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2239             rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2240
2241           if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2242               && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2243             rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2244
2245           if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2246               && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2247             rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2248
2249           if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2250               && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2251             rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2252
2253           if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2254               && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2255             rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2256
2257           if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2258               && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2259             rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2260
2261           if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2262               && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2263             rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2264         }
2265     }
2266
2267   if (TARGET_DEBUG_REG)
2268     rs6000_debug_reg_global ();
2269
2270   if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2271     fprintf (stderr,
2272              "SImode variable mult cost       = %d\n"
2273              "SImode constant mult cost       = %d\n"
2274              "SImode short constant mult cost = %d\n"
2275              "DImode multipliciation cost     = %d\n"
2276              "SImode division cost            = %d\n"
2277              "DImode division cost            = %d\n"
2278              "Simple fp operation cost        = %d\n"
2279              "DFmode multiplication cost      = %d\n"
2280              "SFmode division cost            = %d\n"
2281              "DFmode division cost            = %d\n"
2282              "cache line size                 = %d\n"
2283              "l1 cache size                   = %d\n"
2284              "l2 cache size                   = %d\n"
2285              "simultaneous prefetches         = %d\n"
2286              "\n",
2287              rs6000_cost->mulsi,
2288              rs6000_cost->mulsi_const,
2289              rs6000_cost->mulsi_const9,
2290              rs6000_cost->muldi,
2291              rs6000_cost->divsi,
2292              rs6000_cost->divdi,
2293              rs6000_cost->fp,
2294              rs6000_cost->dmul,
2295              rs6000_cost->sdiv,
2296              rs6000_cost->ddiv,
2297              rs6000_cost->cache_line_size,
2298              rs6000_cost->l1_cache_size,
2299              rs6000_cost->l2_cache_size,
2300              rs6000_cost->simultaneous_prefetches);
2301 }
2302
2303 #if TARGET_MACHO
2304 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS.  */
2305
2306 static void
2307 darwin_rs6000_override_options (void)
2308 {
2309   /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2310      off.  */
2311   rs6000_altivec_abi = 1;
2312   TARGET_ALTIVEC_VRSAVE = 1;
2313   if (DEFAULT_ABI == ABI_DARWIN)
2314   {
2315     if (MACHO_DYNAMIC_NO_PIC_P)
2316       {
2317         if (flag_pic)
2318             warning (0, "-mdynamic-no-pic overrides -fpic or -fPIC");
2319         flag_pic = 0;
2320       }
2321     else if (flag_pic == 1)
2322       {
2323         flag_pic = 2;
2324       }
2325   }
2326   if (TARGET_64BIT && ! TARGET_POWERPC64)
2327     {
2328       target_flags |= MASK_POWERPC64;
2329       warning (0, "-m64 requires PowerPC64 architecture, enabling");
2330     }
2331   if (flag_mkernel)
2332     {
2333       rs6000_default_long_calls = 1;
2334       target_flags |= MASK_SOFT_FLOAT;
2335     }
2336
2337   /* Make -m64 imply -maltivec.  Darwin's 64-bit ABI includes
2338      Altivec.  */
2339   if (!flag_mkernel && !flag_apple_kext
2340       && TARGET_64BIT
2341       && ! (target_flags_explicit & MASK_ALTIVEC))
2342     target_flags |= MASK_ALTIVEC;
2343
2344   /* Unless the user (not the configurer) has explicitly overridden
2345      it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2346      G4 unless targetting the kernel.  */
2347   if (!flag_mkernel
2348       && !flag_apple_kext
2349       && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2350       && ! (target_flags_explicit & MASK_ALTIVEC)
2351       && ! rs6000_select[1].string)
2352     {
2353       target_flags |= MASK_ALTIVEC;
2354     }
2355 }
2356 #endif
2357
2358 /* If not otherwise specified by a target, make 'long double' equivalent to
2359    'double'.  */
2360
2361 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2362 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2363 #endif
2364
2365 /* Override command line options.  Mostly we process the processor
2366    type and sometimes adjust other TARGET_ options.  */
2367
2368 void
2369 rs6000_override_options (const char *default_cpu)
2370 {
2371   size_t i, j;
2372   struct rs6000_cpu_select *ptr;
2373   int set_masks;
2374
2375   /* Simplifications for entries below.  */
2376
2377   enum {
2378     POWERPC_BASE_MASK = MASK_POWERPC | MASK_NEW_MNEMONICS,
2379     POWERPC_7400_MASK = POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_ALTIVEC
2380   };
2381
2382   /* This table occasionally claims that a processor does not support
2383      a particular feature even though it does, but the feature is slower
2384      than the alternative.  Thus, it shouldn't be relied on as a
2385      complete description of the processor's support.
2386
2387      Please keep this list in order, and don't forget to update the
2388      documentation in invoke.texi when adding a new processor or
2389      flag.  */
2390   static struct ptt
2391     {
2392       const char *const name;           /* Canonical processor name.  */
2393       const enum processor_type processor; /* Processor type enum value.  */
2394       const int target_enable;  /* Target flags to enable.  */
2395     } const processor_target_table[]
2396       = {{"401", PROCESSOR_PPC403, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2397          {"403", PROCESSOR_PPC403,
2398           POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_STRICT_ALIGN},
2399          {"405", PROCESSOR_PPC405,
2400           POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_MULHW | MASK_DLMZB},
2401          {"405fp", PROCESSOR_PPC405,
2402           POWERPC_BASE_MASK | MASK_MULHW | MASK_DLMZB},
2403          {"440", PROCESSOR_PPC440,
2404           POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_MULHW | MASK_DLMZB},
2405          {"440fp", PROCESSOR_PPC440,
2406           POWERPC_BASE_MASK | MASK_MULHW | MASK_DLMZB},
2407          {"464", PROCESSOR_PPC440,
2408           POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_MULHW | MASK_DLMZB},
2409          {"464fp", PROCESSOR_PPC440,
2410           POWERPC_BASE_MASK | MASK_MULHW | MASK_DLMZB},
2411          {"476", PROCESSOR_PPC476,
2412           POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_PPC_GFXOPT | MASK_MFCRF
2413           | MASK_POPCNTB | MASK_FPRND | MASK_CMPB | MASK_MULHW | MASK_DLMZB},
2414          {"476fp", PROCESSOR_PPC476,
2415           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_MFCRF | MASK_POPCNTB
2416           | MASK_FPRND | MASK_CMPB | MASK_MULHW | MASK_DLMZB},
2417          {"505", PROCESSOR_MPCCORE, POWERPC_BASE_MASK},
2418          {"601", PROCESSOR_PPC601,
2419           MASK_POWER | POWERPC_BASE_MASK | MASK_MULTIPLE | MASK_STRING},
2420          {"602", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2421          {"603", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2422          {"603e", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2423          {"604", PROCESSOR_PPC604, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2424          {"604e", PROCESSOR_PPC604e, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2425          {"620", PROCESSOR_PPC620,
2426           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
2427          {"630", PROCESSOR_PPC630,
2428           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
2429          {"740", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2430          {"7400", PROCESSOR_PPC7400, POWERPC_7400_MASK},
2431          {"7450", PROCESSOR_PPC7450, POWERPC_7400_MASK},
2432          {"750", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2433          {"801", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2434          {"821", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2435          {"823", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2436          {"8540", PROCESSOR_PPC8540, POWERPC_BASE_MASK | MASK_STRICT_ALIGN
2437           | MASK_ISEL},
2438          /* 8548 has a dummy entry for now.  */
2439          {"8548", PROCESSOR_PPC8540, POWERPC_BASE_MASK | MASK_STRICT_ALIGN
2440           | MASK_ISEL},
2441          {"a2", PROCESSOR_PPCA2,
2442           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64 | MASK_POPCNTB
2443           | MASK_CMPB | MASK_NO_UPDATE },
2444          {"e300c2", PROCESSOR_PPCE300C2, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2445          {"e300c3", PROCESSOR_PPCE300C3, POWERPC_BASE_MASK},
2446          {"e500mc", PROCESSOR_PPCE500MC, POWERPC_BASE_MASK | MASK_PPC_GFXOPT
2447           | MASK_ISEL},
2448          {"e500mc64", PROCESSOR_PPCE500MC64, POWERPC_BASE_MASK | MASK_POWERPC64
2449           | MASK_PPC_GFXOPT | MASK_ISEL},
2450          {"860", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2451          {"970", PROCESSOR_POWER4,
2452           POWERPC_7400_MASK | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
2453          {"cell", PROCESSOR_CELL,
2454           POWERPC_7400_MASK  | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
2455          {"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS},
2456          {"ec603e", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
2457          {"G3", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
2458          {"G4",  PROCESSOR_PPC7450, POWERPC_7400_MASK},
2459          {"G5", PROCESSOR_POWER4,
2460           POWERPC_7400_MASK | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
2461          {"titan", PROCESSOR_TITAN,
2462           POWERPC_BASE_MASK | MASK_MULHW | MASK_DLMZB},
2463          {"power", PROCESSOR_POWER, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
2464          {"power2", PROCESSOR_POWER,
2465           MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING},
2466          {"power3", PROCESSOR_PPC630,
2467           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
2468          {"power4", PROCESSOR_POWER4,
2469           POWERPC_BASE_MASK | MASK_POWERPC64 | MASK_PPC_GPOPT | MASK_PPC_GFXOPT
2470           | MASK_MFCRF},
2471          {"power5", PROCESSOR_POWER5,
2472           POWERPC_BASE_MASK | MASK_POWERPC64 | MASK_PPC_GPOPT | MASK_PPC_GFXOPT
2473           | MASK_MFCRF | MASK_POPCNTB},
2474          {"power5+", PROCESSOR_POWER5,
2475           POWERPC_BASE_MASK | MASK_POWERPC64 | MASK_PPC_GPOPT | MASK_PPC_GFXOPT
2476           | MASK_MFCRF | MASK_POPCNTB | MASK_FPRND},
2477          {"power6", PROCESSOR_POWER6,
2478           POWERPC_BASE_MASK | MASK_POWERPC64 | MASK_PPC_GPOPT | MASK_PPC_GFXOPT
2479           | MASK_MFCRF | MASK_POPCNTB | MASK_FPRND | MASK_CMPB | MASK_DFP
2480           | MASK_RECIP_PRECISION},
2481          {"power6x", PROCESSOR_POWER6,
2482           POWERPC_BASE_MASK | MASK_POWERPC64 | MASK_PPC_GPOPT | MASK_PPC_GFXOPT
2483           | MASK_MFCRF | MASK_POPCNTB | MASK_FPRND | MASK_CMPB | MASK_DFP
2484           | MASK_MFPGPR | MASK_RECIP_PRECISION},
2485          {"power7", PROCESSOR_POWER7,
2486           POWERPC_7400_MASK | MASK_POWERPC64 | MASK_PPC_GPOPT | MASK_MFCRF
2487           | MASK_POPCNTB | MASK_FPRND | MASK_CMPB | MASK_DFP | MASK_POPCNTD
2488           | MASK_VSX| MASK_RECIP_PRECISION},    /* Don't add MASK_ISEL by default */
2489          {"powerpc", PROCESSOR_POWERPC, POWERPC_BASE_MASK},
2490          {"powerpc64", PROCESSOR_POWERPC64,
2491           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
2492          {"rios", PROCESSOR_RIOS1, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
2493          {"rios1", PROCESSOR_RIOS1, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
2494          {"rios2", PROCESSOR_RIOS2,
2495           MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING},
2496          {"rsc", PROCESSOR_PPC601, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
2497          {"rsc1", PROCESSOR_PPC601, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
2498          {"rs64", PROCESSOR_RS64A,
2499           POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64}
2500       };
2501
2502   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
2503
2504   /* Some OSs don't support saving the high part of 64-bit registers on
2505      context switch.  Other OSs don't support saving Altivec registers.
2506      On those OSs, we don't touch the MASK_POWERPC64 or MASK_ALTIVEC
2507      settings; if the user wants either, the user must explicitly specify
2508      them and we won't interfere with the user's specification.  */
2509
2510   enum {
2511     POWER_MASKS = MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
2512     POWERPC_MASKS = (POWERPC_BASE_MASK | MASK_PPC_GPOPT | MASK_STRICT_ALIGN
2513                      | MASK_PPC_GFXOPT | MASK_POWERPC64 | MASK_ALTIVEC
2514                      | MASK_MFCRF | MASK_POPCNTB | MASK_FPRND | MASK_MULHW
2515                      | MASK_DLMZB | MASK_CMPB | MASK_MFPGPR | MASK_DFP
2516                      | MASK_POPCNTD | MASK_VSX | MASK_ISEL | MASK_NO_UPDATE
2517                      | MASK_RECIP_PRECISION)
2518   };
2519
2520   /* Masks for instructions set at various powerpc ISAs.  */
2521   enum {
2522     ISA_2_1_MASKS = MASK_MFCRF,
2523     ISA_2_2_MASKS = (ISA_2_1_MASKS | MASK_POPCNTB | MASK_FPRND),
2524
2525     /* For ISA 2.05, do not add MFPGPR, since it isn't in ISA 2.06, and
2526        don't add ALTIVEC, since in general it isn't a win on power6.  */
2527     ISA_2_5_MASKS = (ISA_2_2_MASKS | MASK_CMPB | MASK_RECIP_PRECISION
2528                      | MASK_DFP),
2529
2530     /* For ISA 2.06, don't add ISEL, since in general it isn't a win, but
2531        altivec is a win so enable it.  */
2532     ISA_2_6_MASKS = (ISA_2_5_MASKS | MASK_ALTIVEC | MASK_POPCNTD
2533                      | MASK_VSX | MASK_RECIP_PRECISION)
2534   };
2535
2536   /* Numerous experiment shows that IRA based loop pressure
2537      calculation works better for RTL loop invariant motion on targets
2538      with enough (>= 32) registers.  It is an expensive optimization.
2539      So it is on only for peak performance.  */
2540   if (optimize >= 3)
2541     flag_ira_loop_pressure = 1;
2542
2543   /* Set the pointer size.  */
2544   if (TARGET_64BIT)
2545     {
2546       rs6000_pmode = (int)DImode;
2547       rs6000_pointer_size = 64;
2548     }
2549   else
2550     {
2551       rs6000_pmode = (int)SImode;
2552       rs6000_pointer_size = 32;
2553     }
2554
2555   set_masks = POWER_MASKS | POWERPC_MASKS | MASK_SOFT_FLOAT;
2556 #ifdef OS_MISSING_POWERPC64
2557   if (OS_MISSING_POWERPC64)
2558     set_masks &= ~MASK_POWERPC64;
2559 #endif
2560 #ifdef OS_MISSING_ALTIVEC
2561   if (OS_MISSING_ALTIVEC)
2562     set_masks &= ~MASK_ALTIVEC;
2563 #endif
2564
2565   /* Don't override by the processor default if given explicitly.  */
2566   set_masks &= ~target_flags_explicit;
2567
2568   /* Identify the processor type.  */
2569   rs6000_select[0].string = default_cpu;
2570   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
2571
2572   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
2573     {
2574       ptr = &rs6000_select[i];
2575       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
2576         {
2577           for (j = 0; j < ptt_size; j++)
2578             if (! strcmp (ptr->string, processor_target_table[j].name))
2579               {
2580                 if (ptr->set_tune_p)
2581                   rs6000_cpu = processor_target_table[j].processor;
2582
2583                 if (ptr->set_arch_p)
2584                   {
2585                     target_flags &= ~set_masks;
2586                     target_flags |= (processor_target_table[j].target_enable
2587                                      & set_masks);
2588                   }
2589                 break;
2590               }
2591
2592           if (j == ptt_size)
2593             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
2594         }
2595     }
2596
2597   if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2598       || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64)
2599     {
2600       if (TARGET_ALTIVEC)
2601         error ("AltiVec not supported in this target");
2602       if (TARGET_SPE)
2603         error ("Spe not supported in this target");
2604     }
2605
2606   /* Disable Cell microcode if we are optimizing for the Cell
2607      and not optimizing for size.  */
2608   if (rs6000_gen_cell_microcode == -1)
2609     rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2610                                   && !optimize_size);
2611
2612   /* If we are optimizing big endian systems for space and it's OK to
2613      use instructions that would be microcoded on the Cell, use the
2614      load/store multiple and string instructions.  */
2615   if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2616     target_flags |= ~target_flags_explicit & (MASK_MULTIPLE | MASK_STRING);
2617
2618   /* Don't allow -mmultiple or -mstring on little endian systems
2619      unless the cpu is a 750, because the hardware doesn't support the
2620      instructions used in little endian mode, and causes an alignment
2621      trap.  The 750 does not cause an alignment trap (except when the
2622      target is unaligned).  */
2623
2624   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2625     {
2626       if (TARGET_MULTIPLE)
2627         {
2628           target_flags &= ~MASK_MULTIPLE;
2629           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
2630             warning (0, "-mmultiple is not supported on little endian systems");
2631         }
2632
2633       if (TARGET_STRING)
2634         {
2635           target_flags &= ~MASK_STRING;
2636           if ((target_flags_explicit & MASK_STRING) != 0)
2637             warning (0, "-mstring is not supported on little endian systems");
2638         }
2639     }
2640
2641   /* Add some warnings for VSX.  */
2642   if (TARGET_VSX)
2643     {
2644       const char *msg = NULL;
2645       if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2646           || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2647         {
2648           if (target_flags_explicit & MASK_VSX)
2649             msg = N_("-mvsx requires hardware floating point");
2650           else
2651             target_flags &= ~ MASK_VSX;
2652         }
2653       else if (TARGET_PAIRED_FLOAT)
2654         msg = N_("-mvsx and -mpaired are incompatible");
2655       /* The hardware will allow VSX and little endian, but until we make sure
2656          things like vector select, etc. work don't allow VSX on little endian
2657          systems at this point.  */
2658       else if (!BYTES_BIG_ENDIAN)
2659         msg = N_("-mvsx used with little endian code");
2660       else if (TARGET_AVOID_XFORM > 0)
2661         msg = N_("-mvsx needs indexed addressing");
2662       else if (!TARGET_ALTIVEC && (target_flags_explicit & MASK_ALTIVEC))
2663         {
2664           if (target_flags_explicit & MASK_VSX)
2665             msg = N_("-mvsx and -mno-altivec are incompatible");
2666           else
2667             msg = N_("-mno-altivec disables vsx");
2668         }
2669
2670       if (msg)
2671         {
2672           warning (0, msg);
2673           target_flags &= ~ MASK_VSX;
2674         }
2675     }
2676
2677   /* For the newer switches (vsx, dfp, etc.) set some of the older options,
2678      unless the user explicitly used the -mno-<option> to disable the code.  */
2679   if (TARGET_VSX)
2680     target_flags |= (ISA_2_6_MASKS & (target_flags_explicit & ~ISA_2_6_MASKS));
2681   else if (TARGET_DFP)
2682     target_flags |= (ISA_2_5_MASKS & (target_flags_explicit & ~ISA_2_5_MASKS));
2683   else if (TARGET_ALTIVEC)
2684     target_flags |= (MASK_PPC_GFXOPT & (target_flags_explicit & ~MASK_PPC_GFXOPT));
2685
2686   /* Set debug flags */
2687   if (rs6000_debug_name)
2688     {
2689       if (! strcmp (rs6000_debug_name, "all"))
2690         rs6000_debug_stack = rs6000_debug_arg = rs6000_debug_reg
2691           = rs6000_debug_addr = rs6000_debug_cost = 1;
2692       else if (! strcmp (rs6000_debug_name, "stack"))
2693         rs6000_debug_stack = 1;
2694       else if (! strcmp (rs6000_debug_name, "arg"))
2695         rs6000_debug_arg = 1;
2696       else if (! strcmp (rs6000_debug_name, "reg"))
2697         rs6000_debug_reg = 1;
2698       else if (! strcmp (rs6000_debug_name, "addr"))
2699         rs6000_debug_addr = 1;
2700       else if (! strcmp (rs6000_debug_name, "cost"))
2701         rs6000_debug_cost = 1;
2702       else
2703         error ("unknown -mdebug-%s switch", rs6000_debug_name);
2704
2705       /* If the appropriate debug option is enabled, replace the target hooks
2706          with debug versions that call the real version and then prints
2707          debugging information.  */
2708       if (TARGET_DEBUG_COST)
2709         {
2710           targetm.rtx_costs = rs6000_debug_rtx_costs;
2711           targetm.address_cost = rs6000_debug_address_cost;
2712           targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
2713         }
2714
2715       if (TARGET_DEBUG_ADDR)
2716         {
2717           targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
2718           targetm.legitimize_address = rs6000_debug_legitimize_address;
2719           rs6000_secondary_reload_class_ptr
2720             = rs6000_debug_secondary_reload_class;
2721           rs6000_secondary_memory_needed_ptr
2722             = rs6000_debug_secondary_memory_needed;
2723           rs6000_cannot_change_mode_class_ptr
2724             = rs6000_debug_cannot_change_mode_class;
2725           rs6000_preferred_reload_class_ptr
2726             = rs6000_debug_preferred_reload_class;
2727           rs6000_legitimize_reload_address_ptr
2728             = rs6000_debug_legitimize_reload_address;
2729           rs6000_mode_dependent_address_ptr
2730             = rs6000_debug_mode_dependent_address;
2731         }
2732     }
2733
2734   if (rs6000_traceback_name)
2735     {
2736       if (! strncmp (rs6000_traceback_name, "full", 4))
2737         rs6000_traceback = traceback_full;
2738       else if (! strncmp (rs6000_traceback_name, "part", 4))
2739         rs6000_traceback = traceback_part;
2740       else if (! strncmp (rs6000_traceback_name, "no", 2))
2741         rs6000_traceback = traceback_none;
2742       else
2743         error ("unknown -mtraceback arg %qs; expecting %<full%>, %<partial%> or %<none%>",
2744                rs6000_traceback_name);
2745     }
2746
2747   if (!rs6000_explicit_options.long_double)
2748     rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
2749
2750 #ifndef POWERPC_LINUX
2751   if (!rs6000_explicit_options.ieee)
2752     rs6000_ieeequad = 1;
2753 #endif
2754
2755   /* Enable Altivec ABI for AIX -maltivec.  */
2756   if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
2757     rs6000_altivec_abi = 1;
2758
2759   /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux.  For
2760      PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI.  It can
2761      be explicitly overridden in either case.  */
2762   if (TARGET_ELF)
2763     {
2764       if (!rs6000_explicit_options.altivec_abi
2765           && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
2766         rs6000_altivec_abi = 1;
2767
2768       /* Enable VRSAVE for AltiVec ABI, unless explicitly overridden.  */
2769       if (!rs6000_explicit_options.vrsave)
2770         TARGET_ALTIVEC_VRSAVE = rs6000_altivec_abi;
2771     }
2772
2773   /* Set the Darwin64 ABI as default for 64-bit Darwin.  */
2774   if (DEFAULT_ABI == ABI_DARWIN && TARGET_64BIT)
2775     {
2776       rs6000_darwin64_abi = 1;
2777 #if TARGET_MACHO
2778       darwin_one_byte_bool = 1;
2779 #endif
2780       /* Default to natural alignment, for better performance.  */
2781       rs6000_alignment_flags = MASK_ALIGN_NATURAL;
2782     }
2783
2784   /* Place FP constants in the constant pool instead of TOC
2785      if section anchors enabled.  */
2786   if (flag_section_anchors)
2787     TARGET_NO_FP_IN_TOC = 1;
2788
2789   /* Handle -mtls-size option.  */
2790   rs6000_parse_tls_size_option ();
2791
2792 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2793   SUBTARGET_OVERRIDE_OPTIONS;
2794 #endif
2795 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2796   SUBSUBTARGET_OVERRIDE_OPTIONS;
2797 #endif
2798 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
2799   SUB3TARGET_OVERRIDE_OPTIONS;
2800 #endif
2801
2802   if (TARGET_E500 || rs6000_cpu == PROCESSOR_PPCE500MC
2803       || rs6000_cpu == PROCESSOR_PPCE500MC64)
2804     {
2805       /* The e500 and e500mc do not have string instructions, and we set
2806          MASK_STRING above when optimizing for size.  */
2807       if ((target_flags & MASK_STRING) != 0)
2808         target_flags = target_flags & ~MASK_STRING;
2809     }
2810   else if (rs6000_select[1].string != NULL)
2811     {
2812       /* For the powerpc-eabispe configuration, we set all these by
2813          default, so let's unset them if we manually set another
2814          CPU that is not the E500.  */
2815       if (!rs6000_explicit_options.spe_abi)
2816         rs6000_spe_abi = 0;
2817       if (!rs6000_explicit_options.spe)
2818         rs6000_spe = 0;
2819       if (!rs6000_explicit_options.float_gprs)
2820         rs6000_float_gprs = 0;
2821       if (!(target_flags_explicit & MASK_ISEL))
2822         target_flags &= ~MASK_ISEL;
2823     }
2824
2825   /* Detect invalid option combinations with E500.  */
2826   CHECK_E500_OPTIONS;
2827
2828   rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
2829                         && rs6000_cpu != PROCESSOR_POWER5
2830                         && rs6000_cpu != PROCESSOR_POWER6
2831                         && rs6000_cpu != PROCESSOR_POWER7
2832                         && rs6000_cpu != PROCESSOR_PPCA2
2833                         && rs6000_cpu != PROCESSOR_CELL);
2834   rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
2835                          || rs6000_cpu == PROCESSOR_POWER5
2836                          || rs6000_cpu == PROCESSOR_POWER7);
2837   rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
2838                                  || rs6000_cpu == PROCESSOR_POWER5
2839                                  || rs6000_cpu == PROCESSOR_POWER6
2840                                  || rs6000_cpu == PROCESSOR_POWER7
2841                                  || rs6000_cpu == PROCESSOR_PPCE500MC
2842                                  || rs6000_cpu == PROCESSOR_PPCE500MC64);
2843
2844   /* Allow debug switches to override the above settings.  */
2845   if (TARGET_ALWAYS_HINT > 0)
2846     rs6000_always_hint = TARGET_ALWAYS_HINT;
2847
2848   if (TARGET_SCHED_GROUPS > 0)
2849     rs6000_sched_groups = TARGET_SCHED_GROUPS;
2850
2851   if (TARGET_ALIGN_BRANCH_TARGETS > 0)
2852     rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
2853
2854   rs6000_sched_restricted_insns_priority
2855     = (rs6000_sched_groups ? 1 : 0);
2856
2857   /* Handle -msched-costly-dep option.  */
2858   rs6000_sched_costly_dep
2859     = (rs6000_sched_groups ? store_to_load_dep_costly : no_dep_costly);
2860
2861   if (rs6000_sched_costly_dep_str)
2862     {
2863       if (! strcmp (rs6000_sched_costly_dep_str, "no"))
2864         rs6000_sched_costly_dep = no_dep_costly;
2865       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
2866         rs6000_sched_costly_dep = all_deps_costly;
2867       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
2868         rs6000_sched_costly_dep = true_store_to_load_dep_costly;
2869       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
2870         rs6000_sched_costly_dep = store_to_load_dep_costly;
2871       else
2872         rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
2873                                    atoi (rs6000_sched_costly_dep_str));
2874     }
2875
2876   /* Handle -minsert-sched-nops option.  */
2877   rs6000_sched_insert_nops
2878     = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
2879
2880   if (rs6000_sched_insert_nops_str)
2881     {
2882       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
2883         rs6000_sched_insert_nops = sched_finish_none;
2884       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
2885         rs6000_sched_insert_nops = sched_finish_pad_groups;
2886       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
2887         rs6000_sched_insert_nops = sched_finish_regroup_exact;
2888       else
2889         rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
2890                                     atoi (rs6000_sched_insert_nops_str));
2891     }
2892
2893 #ifdef TARGET_REGNAMES
2894   /* If the user desires alternate register names, copy in the
2895      alternate names now.  */
2896   if (TARGET_REGNAMES)
2897     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
2898 #endif
2899
2900   /* Set aix_struct_return last, after the ABI is determined.
2901      If -maix-struct-return or -msvr4-struct-return was explicitly
2902      used, don't override with the ABI default.  */
2903   if (!rs6000_explicit_options.aix_struct_ret)
2904     aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
2905
2906 #if 0
2907   /* IBM XL compiler defaults to unsigned bitfields.  */
2908   if (TARGET_XL_COMPAT)
2909     flag_signed_bitfields = 0;
2910 #endif
2911
2912   if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
2913     REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
2914
2915   if (TARGET_TOC)
2916     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
2917
2918   /* We can only guarantee the availability of DI pseudo-ops when
2919      assembling for 64-bit targets.  */
2920   if (!TARGET_64BIT)
2921     {
2922       targetm.asm_out.aligned_op.di = NULL;
2923       targetm.asm_out.unaligned_op.di = NULL;
2924     }
2925
2926   /* Set branch target alignment, if not optimizing for size.  */
2927   if (!optimize_size)
2928     {
2929       /* Cell wants to be aligned 8byte for dual issue.  Titan wants to be
2930          aligned 8byte to avoid misprediction by the branch predictor.  */
2931       if (rs6000_cpu == PROCESSOR_TITAN
2932           || rs6000_cpu == PROCESSOR_CELL)
2933         {
2934           if (align_functions <= 0)
2935             align_functions = 8;
2936           if (align_jumps <= 0)
2937             align_jumps = 8;
2938           if (align_loops <= 0)
2939             align_loops = 8;
2940         }
2941       if (rs6000_align_branch_targets)
2942         {
2943           if (align_functions <= 0)
2944             align_functions = 16;
2945           if (align_jumps <= 0)
2946             align_jumps = 16;
2947           if (align_loops <= 0)
2948             align_loops = 16;
2949         }
2950       if (align_jumps_max_skip <= 0)
2951         align_jumps_max_skip = 15;
2952       if (align_loops_max_skip <= 0)
2953         align_loops_max_skip = 15;
2954     }
2955
2956   /* Arrange to save and restore machine status around nested functions.  */
2957   init_machine_status = rs6000_init_machine_status;
2958
2959   /* We should always be splitting complex arguments, but we can't break
2960      Linux and Darwin ABIs at the moment.  For now, only AIX is fixed.  */
2961   if (DEFAULT_ABI != ABI_AIX)
2962     targetm.calls.split_complex_arg = NULL;
2963
2964   /* Initialize rs6000_cost with the appropriate target costs.  */
2965   if (optimize_size)
2966     rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
2967   else
2968     switch (rs6000_cpu)
2969       {
2970       case PROCESSOR_RIOS1:
2971         rs6000_cost = &rios1_cost;
2972         break;
2973
2974       case PROCESSOR_RIOS2:
2975         rs6000_cost = &rios2_cost;
2976         break;
2977
2978       case PROCESSOR_RS64A:
2979         rs6000_cost = &rs64a_cost;
2980         break;
2981
2982       case PROCESSOR_MPCCORE:
2983         rs6000_cost = &mpccore_cost;
2984         break;
2985
2986       case PROCESSOR_PPC403:
2987         rs6000_cost = &ppc403_cost;
2988         break;
2989
2990       case PROCESSOR_PPC405:
2991         rs6000_cost = &ppc405_cost;
2992         break;
2993
2994       case PROCESSOR_PPC440:
2995         rs6000_cost = &ppc440_cost;
2996         break;
2997
2998       case PROCESSOR_PPC476:
2999         rs6000_cost = &ppc476_cost;
3000         break;
3001
3002       case PROCESSOR_PPC601:
3003         rs6000_cost = &ppc601_cost;
3004         break;
3005
3006       case PROCESSOR_PPC603:
3007         rs6000_cost = &ppc603_cost;
3008         break;
3009
3010       case PROCESSOR_PPC604:
3011         rs6000_cost = &ppc604_cost;
3012         break;
3013
3014       case PROCESSOR_PPC604e:
3015         rs6000_cost = &ppc604e_cost;
3016         break;
3017
3018       case PROCESSOR_PPC620:
3019         rs6000_cost = &ppc620_cost;
3020         break;
3021
3022       case PROCESSOR_PPC630:
3023         rs6000_cost = &ppc630_cost;
3024         break;
3025
3026       case PROCESSOR_CELL:
3027         rs6000_cost = &ppccell_cost;
3028         break;
3029
3030       case PROCESSOR_PPC750:
3031       case PROCESSOR_PPC7400:
3032         rs6000_cost = &ppc750_cost;
3033         break;
3034
3035       case PROCESSOR_PPC7450:
3036         rs6000_cost = &ppc7450_cost;
3037         break;
3038
3039       case PROCESSOR_PPC8540:
3040         rs6000_cost = &ppc8540_cost;
3041         break;
3042
3043       case PROCESSOR_PPCE300C2:
3044       case PROCESSOR_PPCE300C3:
3045         rs6000_cost = &ppce300c2c3_cost;
3046         break;
3047
3048       case PROCESSOR_PPCE500MC:
3049         rs6000_cost = &ppce500mc_cost;
3050         break;
3051
3052       case PROCESSOR_PPCE500MC64:
3053         rs6000_cost = &ppce500mc64_cost;
3054         break;
3055
3056       case PROCESSOR_TITAN:
3057         rs6000_cost = &titan_cost;
3058         break;
3059
3060       case PROCESSOR_POWER4:
3061       case PROCESSOR_POWER5:
3062         rs6000_cost = &power4_cost;
3063         break;
3064
3065       case PROCESSOR_POWER6:
3066         rs6000_cost = &power6_cost;
3067         break;
3068
3069       case PROCESSOR_POWER7:
3070         rs6000_cost = &power7_cost;
3071         break;
3072
3073       case PROCESSOR_PPCA2:
3074         rs6000_cost = &ppca2_cost;
3075         break;
3076
3077       default:
3078         gcc_unreachable ();
3079       }
3080
3081   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
3082     set_param_value ("simultaneous-prefetches",
3083                      rs6000_cost->simultaneous_prefetches);
3084   if (!PARAM_SET_P (PARAM_L1_CACHE_SIZE))
3085     set_param_value ("l1-cache-size", rs6000_cost->l1_cache_size);
3086   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
3087     set_param_value ("l1-cache-line-size", rs6000_cost->cache_line_size);
3088   if (!PARAM_SET_P (PARAM_L2_CACHE_SIZE))
3089     set_param_value ("l2-cache-size", rs6000_cost->l2_cache_size);
3090
3091   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3092      can be optimized to ap = __builtin_next_arg (0).  */
3093   if (DEFAULT_ABI != ABI_V4)
3094     targetm.expand_builtin_va_start = NULL;
3095
3096   /* Set up single/double float flags.  
3097      If TARGET_HARD_FLOAT is set, but neither single or double is set, 
3098      then set both flags. */
3099   if (TARGET_HARD_FLOAT && TARGET_FPRS 
3100       && rs6000_single_float == 0 && rs6000_double_float == 0)
3101     rs6000_single_float = rs6000_double_float = 1;
3102
3103   /* Reset single and double FP flags if target is E500. */
3104   if (TARGET_E500) 
3105   {
3106     rs6000_single_float = rs6000_double_float = 0;
3107     if (TARGET_E500_SINGLE)
3108       rs6000_single_float = 1; 
3109     if (TARGET_E500_DOUBLE)
3110       rs6000_single_float = rs6000_double_float = 1;
3111   }
3112
3113   /* If not explicitly specified via option, decide whether to generate indexed
3114      load/store instructions.  */
3115   if (TARGET_AVOID_XFORM == -1)
3116     /* Avoid indexed addressing when targeting Power6 in order to avoid
3117      the DERAT mispredict penalty.  */
3118     TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB);
3119
3120   /* Set the -mrecip options.  */
3121   if (rs6000_recip_name)
3122     {
3123       char *p = ASTRDUP (rs6000_recip_name);
3124       char *q;
3125       unsigned int mask, i;
3126       bool invert;
3127
3128       while ((q = strtok (p, ",")) != NULL)
3129         {
3130           p = NULL;
3131           if (*q == '!')
3132             {
3133               invert = true;
3134               q++;
3135             }
3136           else
3137             invert = false;
3138
3139           if (!strcmp (q, "default"))
3140             mask = ((TARGET_RECIP_PRECISION)
3141                     ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3142           else
3143             {
3144               for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3145                 if (!strcmp (q, recip_options[i].string))
3146                   {
3147                     mask = recip_options[i].mask;
3148                     break;
3149                   }
3150
3151               if (i == ARRAY_SIZE (recip_options))
3152                 {
3153                   error ("Unknown option for -mrecip=%s", q);
3154                   invert = false;
3155                   mask = 0;
3156                 }
3157             }
3158
3159           if (invert)
3160             rs6000_recip_control &= ~mask;
3161           else
3162             rs6000_recip_control |= mask;
3163         }
3164     }
3165
3166   rs6000_init_hard_regno_mode_ok ();
3167 }
3168
3169 /* Implement targetm.vectorize.builtin_mask_for_load.  */
3170 static tree
3171 rs6000_builtin_mask_for_load (void)
3172 {
3173   if (TARGET_ALTIVEC || TARGET_VSX)
3174     return altivec_builtin_mask_for_load;
3175   else
3176     return 0;
3177 }
3178
3179 /* Implement targetm.vectorize.builtin_conversion.
3180    Returns a decl of a function that implements conversion of an integer vector
3181    into a floating-point vector, or vice-versa.  DEST_TYPE is the
3182    destination type and SRC_TYPE the source type of the conversion.
3183    Return NULL_TREE if it is not available.  */
3184 static tree
3185 rs6000_builtin_conversion (unsigned int tcode, tree dest_type, tree src_type)
3186 {
3187   enum tree_code code = (enum tree_code) tcode;
3188
3189   switch (code)
3190     {
3191     case FIX_TRUNC_EXPR:
3192       switch (TYPE_MODE (dest_type))
3193         {
3194         case V2DImode:
3195           if (!VECTOR_UNIT_VSX_P (V2DFmode))
3196             return NULL_TREE;
3197
3198           return TYPE_UNSIGNED (dest_type)
3199             ? rs6000_builtin_decls[VSX_BUILTIN_XVCVDPUXDS_UNS]
3200             : rs6000_builtin_decls[VSX_BUILTIN_XVCVDPSXDS];
3201
3202         case V4SImode:
3203           if (VECTOR_UNIT_NONE_P (V4SImode) || VECTOR_UNIT_NONE_P (V4SFmode))
3204             return NULL_TREE;
3205
3206           return TYPE_UNSIGNED (dest_type)
3207             ? rs6000_builtin_decls[VECTOR_BUILTIN_FIXUNS_V4SF_V4SI]
3208             : rs6000_builtin_decls[VECTOR_BUILTIN_FIX_V4SF_V4SI];
3209
3210         default:
3211           return NULL_TREE;
3212         }
3213
3214     case FLOAT_EXPR:
3215       switch (TYPE_MODE (src_type))
3216         {
3217         case V2DImode:
3218           if (!VECTOR_UNIT_VSX_P (V2DFmode))
3219             return NULL_TREE;
3220
3221           return TYPE_UNSIGNED (src_type)
3222             ? rs6000_builtin_decls[VSX_BUILTIN_XVCVUXDDP]
3223             : rs6000_builtin_decls[VSX_BUILTIN_XVCVSXDDP];
3224
3225         case V4SImode:
3226           if (VECTOR_UNIT_NONE_P (V4SImode) || VECTOR_UNIT_NONE_P (V4SFmode))
3227             return NULL_TREE;
3228
3229           return TYPE_UNSIGNED (src_type)
3230             ? rs6000_builtin_decls[VECTOR_BUILTIN_UNSFLOAT_V4SI_V4SF]
3231             : rs6000_builtin_decls[VECTOR_BUILTIN_FLOAT_V4SI_V4SF];
3232
3233         default:
3234           return NULL_TREE;
3235         }
3236
3237     default:
3238       return NULL_TREE;
3239     }
3240 }
3241
3242 /* Implement targetm.vectorize.builtin_mul_widen_even.  */
3243 static tree
3244 rs6000_builtin_mul_widen_even (tree type)
3245 {
3246   if (!TARGET_ALTIVEC)
3247     return NULL_TREE;
3248
3249   switch (TYPE_MODE (type))
3250     {
3251     case V8HImode:
3252       return TYPE_UNSIGNED (type)
3253             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULEUH_UNS]
3254             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULESH];
3255
3256     case V16QImode:
3257       return TYPE_UNSIGNED (type)
3258             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULEUB_UNS]
3259             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULESB];
3260     default:
3261       return NULL_TREE;
3262     }
3263 }
3264
3265 /* Implement targetm.vectorize.builtin_mul_widen_odd.  */
3266 static tree
3267 rs6000_builtin_mul_widen_odd (tree type)
3268 {
3269   if (!TARGET_ALTIVEC)
3270     return NULL_TREE;
3271
3272   switch (TYPE_MODE (type))
3273     {
3274     case V8HImode:
3275       return TYPE_UNSIGNED (type)
3276             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOUH_UNS]
3277             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOSH];
3278
3279     case V16QImode:
3280       return TYPE_UNSIGNED (type)
3281             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOUB_UNS]
3282             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOSB];
3283     default:
3284       return NULL_TREE;
3285     }
3286 }
3287
3288
3289 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3290    after applying N number of iterations.  This routine does not determine
3291    how may iterations are required to reach desired alignment.  */
3292
3293 static bool
3294 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3295 {
3296   if (is_packed)
3297     return false;
3298
3299   if (TARGET_32BIT)
3300     {
3301       if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3302         return true;
3303
3304       if (rs6000_alignment_flags ==  MASK_ALIGN_POWER)
3305         return true;
3306
3307       return false;
3308     }
3309   else
3310     {
3311       if (TARGET_MACHO)
3312         return false;
3313
3314       /* Assuming that all other types are naturally aligned. CHECKME!  */
3315       return true;
3316     }
3317 }
3318
3319 /* Return true if the vector misalignment factor is supported by the
3320    target.  */ 
3321 bool
3322 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3323                                             const_tree type,
3324                                             int misalignment,
3325                                             bool is_packed)
3326 {
3327   if (TARGET_VSX)
3328     {
3329       /* Return if movmisalign pattern is not supported for this mode.  */
3330       if (optab_handler (movmisalign_optab, mode)->insn_code ==
3331           CODE_FOR_nothing)
3332         return false;
3333
3334       if (misalignment == -1)
3335         {
3336           /* misalignment factor is unknown at compile time but we know
3337              it's word aligned.  */
3338           if (rs6000_vector_alignment_reachable (type, is_packed))
3339             return true;
3340           return false;
3341         }
3342       /* VSX supports word-aligned vector.  */
3343       if (misalignment % 4 == 0)
3344         return true;
3345     }
3346   return false;
3347 }
3348
3349 /* Implement targetm.vectorize.builtin_vec_perm.  */
3350 tree
3351 rs6000_builtin_vec_perm (tree type, tree *mask_element_type)
3352 {
3353   tree inner_type = TREE_TYPE (type);
3354   bool uns_p = TYPE_UNSIGNED (inner_type);
3355   tree d;
3356
3357   *mask_element_type = unsigned_char_type_node;
3358
3359   switch (TYPE_MODE (type))
3360     {
3361     case V16QImode:
3362       d = (uns_p
3363            ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_16QI_UNS]
3364            : rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_16QI]);
3365       break;
3366
3367     case V8HImode:
3368       d = (uns_p
3369            ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_8HI_UNS]
3370            : rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_8HI]);
3371       break;
3372
3373     case V4SImode:
3374       d = (uns_p
3375            ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_4SI_UNS]
3376            : rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_4SI]);
3377       break;
3378
3379     case V4SFmode:
3380       d = rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_4SF];
3381       break;
3382
3383     case V2DFmode:
3384       if (!TARGET_ALLOW_DF_PERMUTE)
3385         return NULL_TREE;
3386
3387       d = rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_2DF];
3388       break;
3389
3390     case V2DImode:
3391       if (!TARGET_ALLOW_DF_PERMUTE)
3392         return NULL_TREE;
3393
3394       d = (uns_p
3395            ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_2DI_UNS]
3396            : rs6000_builtin_decls[ALTIVEC_BUILTIN_VPERM_2DI]);
3397       break;
3398
3399     default:
3400       return NULL_TREE;
3401     }
3402
3403   gcc_assert (d);
3404   return d;
3405 }
3406
3407 /* Handle generic options of the form -mfoo=yes/no.
3408    NAME is the option name.
3409    VALUE is the option value.
3410    FLAG is the pointer to the flag where to store a 1 or 0, depending on
3411    whether the option value is 'yes' or 'no' respectively.  */
3412 static void
3413 rs6000_parse_yes_no_option (const char *name, const char *value, int *flag)
3414 {
3415   if (value == 0)
3416     return;
3417   else if (!strcmp (value, "yes"))
3418     *flag = 1;
3419   else if (!strcmp (value, "no"))
3420     *flag = 0;
3421   else
3422     error ("unknown -m%s= option specified: '%s'", name, value);
3423 }
3424
3425 /* Validate and record the size specified with the -mtls-size option.  */
3426
3427 static void
3428 rs6000_parse_tls_size_option (void)
3429 {
3430   if (rs6000_tls_size_string == 0)
3431     return;
3432   else if (strcmp (rs6000_tls_size_string, "16") == 0)
3433     rs6000_tls_size = 16;
3434   else if (strcmp (rs6000_tls_size_string, "32") == 0)
3435     rs6000_tls_size = 32;
3436   else if (strcmp (rs6000_tls_size_string, "64") == 0)
3437     rs6000_tls_size = 64;
3438   else
3439     error ("bad value %qs for -mtls-size switch", rs6000_tls_size_string);
3440 }
3441
3442 void
3443 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
3444 {
3445   if (DEFAULT_ABI == ABI_DARWIN)
3446     /* The Darwin libraries never set errno, so we might as well
3447        avoid calling them when that's the only reason we would.  */
3448     flag_errno_math = 0;
3449
3450   /* Double growth factor to counter reduced min jump length.  */
3451   set_param_value ("max-grow-copy-bb-insns", 16);
3452
3453   /* Enable section anchors by default.
3454      Skip section anchors for Objective C and Objective C++
3455      until front-ends fixed.  */
3456   if (!TARGET_MACHO && lang_hooks.name[4] != 'O')
3457     flag_section_anchors = 2;
3458 }
3459
3460 static enum fpu_type_t
3461 rs6000_parse_fpu_option (const char *option)
3462 {
3463   if (!strcmp("none", option)) return FPU_NONE;
3464   if (!strcmp("sp_lite", option)) return FPU_SF_LITE;
3465   if (!strcmp("dp_lite", option)) return FPU_DF_LITE;
3466   if (!strcmp("sp_full", option)) return FPU_SF_FULL;
3467   if (!strcmp("dp_full", option)) return FPU_DF_FULL;
3468   error("unknown value %s for -mfpu", option);
3469   return FPU_NONE;
3470 }
3471
3472 /* Returns a function decl for a vectorized version of the builtin function
3473    with builtin function code FN and the result vector type TYPE, or NULL_TREE
3474    if it is not available.  */
3475
3476 static tree
3477 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
3478                                     tree type_in)
3479 {
3480   enum machine_mode in_mode, out_mode;
3481   int in_n, out_n;
3482
3483   if (TREE_CODE (type_out) != VECTOR_TYPE
3484       || TREE_CODE (type_in) != VECTOR_TYPE
3485       || !TARGET_VECTORIZE_BUILTINS)
3486     return NULL_TREE;
3487
3488   out_mode = TYPE_MODE (TREE_TYPE (type_out));
3489   out_n = TYPE_VECTOR_SUBPARTS (type_out);
3490   in_mode = TYPE_MODE (TREE_TYPE (type_in));
3491   in_n = TYPE_VECTOR_SUBPARTS (type_in);
3492
3493   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3494     {
3495       enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3496       switch (fn)
3497         {
3498         case BUILT_IN_COPYSIGN:
3499           if (VECTOR_UNIT_VSX_P (V2DFmode)
3500               && out_mode == DFmode && out_n == 2
3501               && in_mode == DFmode && in_n == 2)
3502             return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
3503           break;
3504         case BUILT_IN_COPYSIGNF:
3505           if (out_mode != SFmode || out_n != 4
3506               || in_mode != SFmode || in_n != 4)
3507             break;
3508           if (VECTOR_UNIT_VSX_P (V4SFmode))
3509             return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
3510           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3511             return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
3512           break;
3513         case BUILT_IN_SQRT:
3514           if (VECTOR_UNIT_VSX_P (V2DFmode)
3515               && out_mode == DFmode && out_n == 2
3516               && in_mode == DFmode && in_n == 2)
3517             return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
3518           break;
3519         case BUILT_IN_SQRTF:
3520           if (VECTOR_UNIT_VSX_P (V4SFmode)
3521               && out_mode == SFmode && out_n == 4
3522               && in_mode == SFmode && in_n == 4)
3523             return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
3524           break;
3525         case BUILT_IN_CEIL:
3526           if (VECTOR_UNIT_VSX_P (V2DFmode)
3527               && out_mode == DFmode && out_n == 2
3528               && in_mode == DFmode && in_n == 2)
3529             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
3530           break;
3531         case BUILT_IN_CEILF:
3532           if (out_mode != SFmode || out_n != 4
3533               || in_mode != SFmode || in_n != 4)
3534             break;
3535           if (VECTOR_UNIT_VSX_P (V4SFmode))
3536             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
3537           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3538             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
3539           break;
3540         case BUILT_IN_FLOOR:
3541           if (VECTOR_UNIT_VSX_P (V2DFmode)
3542               && out_mode == DFmode && out_n == 2
3543               && in_mode == DFmode && in_n == 2)
3544             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
3545           break;
3546         case BUILT_IN_FLOORF:
3547           if (out_mode != SFmode || out_n != 4
3548               || in_mode != SFmode || in_n != 4)
3549             break;
3550           if (VECTOR_UNIT_VSX_P (V4SFmode))
3551             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
3552           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3553             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
3554           break;
3555         case BUILT_IN_TRUNC:
3556           if (VECTOR_UNIT_VSX_P (V2DFmode)
3557               && out_mode == DFmode && out_n == 2
3558               && in_mode == DFmode && in_n == 2)
3559             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
3560           break;
3561         case BUILT_IN_TRUNCF:
3562           if (out_mode != SFmode || out_n != 4
3563               || in_mode != SFmode || in_n != 4)
3564             break;
3565           if (VECTOR_UNIT_VSX_P (V4SFmode))
3566             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
3567           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3568             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
3569           break;
3570         case BUILT_IN_NEARBYINT:
3571           if (VECTOR_UNIT_VSX_P (V2DFmode)
3572               && flag_unsafe_math_optimizations
3573               && out_mode == DFmode && out_n == 2
3574               && in_mode == DFmode && in_n == 2)
3575             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
3576           break;
3577         case BUILT_IN_NEARBYINTF:
3578           if (VECTOR_UNIT_VSX_P (V4SFmode)
3579               && flag_unsafe_math_optimizations
3580               && out_mode == SFmode && out_n == 4
3581               && in_mode == SFmode && in_n == 4)
3582             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
3583           break;
3584         case BUILT_IN_RINT:
3585           if (VECTOR_UNIT_VSX_P (V2DFmode)
3586               && !flag_trapping_math
3587               && out_mode == DFmode && out_n == 2
3588               && in_mode == DFmode && in_n == 2)
3589             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
3590           break;
3591         case BUILT_IN_RINTF:
3592           if (VECTOR_UNIT_VSX_P (V4SFmode)
3593               && !flag_trapping_math
3594               && out_mode == SFmode && out_n == 4
3595               && in_mode == SFmode && in_n == 4)
3596             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
3597           break;
3598         default:
3599           break;
3600         }
3601     }
3602
3603   else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3604     {
3605       enum rs6000_builtins fn
3606         = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
3607       switch (fn)
3608         {
3609         case RS6000_BUILTIN_RSQRTF:
3610           if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3611               && out_mode == SFmode && out_n == 4
3612               && in_mode == SFmode && in_n == 4)
3613             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
3614           break;
3615         case RS6000_BUILTIN_RSQRT:
3616           if (VECTOR_UNIT_VSX_P (V2DFmode)
3617               && out_mode == DFmode && out_n == 2
3618               && in_mode == DFmode && in_n == 2)
3619             return rs6000_builtin_decls[VSX_BUILTIN_VEC_RSQRT_V2DF];
3620           break;
3621         case RS6000_BUILTIN_RECIPF:
3622           if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3623               && out_mode == SFmode && out_n == 4
3624               && in_mode == SFmode && in_n == 4)
3625             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
3626           break;
3627         case RS6000_BUILTIN_RECIP:
3628           if (VECTOR_UNIT_VSX_P (V2DFmode)
3629               && out_mode == DFmode && out_n == 2
3630               && in_mode == DFmode && in_n == 2)
3631             return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
3632           break;
3633         default:
3634           break;
3635         }
3636     }
3637
3638   return NULL_TREE;
3639 }
3640
3641
3642 /* Implement TARGET_HANDLE_OPTION.  */
3643
3644 static bool
3645 rs6000_handle_option (size_t code, const char *arg, int value)
3646 {
3647   enum fpu_type_t fpu_type = FPU_NONE;
3648   int isel;
3649
3650   switch (code)
3651     {
3652     case OPT_mno_power:
3653       target_flags &= ~(MASK_POWER | MASK_POWER2
3654                         | MASK_MULTIPLE | MASK_STRING);
3655       target_flags_explicit |= (MASK_POWER | MASK_POWER2
3656                                 | MASK_MULTIPLE | MASK_STRING);
3657       break;
3658     case OPT_mno_powerpc:
3659       target_flags &= ~(MASK_POWERPC | MASK_PPC_GPOPT
3660                         | MASK_PPC_GFXOPT | MASK_POWERPC64);
3661       target_flags_explicit |= (MASK_POWERPC | MASK_PPC_GPOPT
3662                                 | MASK_PPC_GFXOPT | MASK_POWERPC64);
3663       break;
3664     case OPT_mfull_toc:
3665       target_flags &= ~MASK_MINIMAL_TOC;
3666       TARGET_NO_FP_IN_TOC = 0;
3667       TARGET_NO_SUM_IN_TOC = 0;
3668       target_flags_explicit |= MASK_MINIMAL_TOC;
3669 #ifdef TARGET_USES_SYSV4_OPT
3670       /* Note, V.4 no longer uses a normal TOC, so make -mfull-toc, be
3671          just the same as -mminimal-toc.  */
3672       target_flags |= MASK_MINIMAL_TOC;
3673       target_flags_explicit |= MASK_MINIMAL_TOC;
3674 #endif
3675       break;
3676
3677 #ifdef TARGET_USES_SYSV4_OPT
3678     case OPT_mtoc:
3679       /* Make -mtoc behave like -mminimal-toc.  */
3680       target_flags |= MASK_MINIMAL_TOC;
3681       target_flags_explicit |= MASK_MINIMAL_TOC;
3682       break;
3683 #endif
3684
3685 #if defined (HAVE_LD_LARGE_TOC) && defined (TARGET_USES_LINUX64_OPT)
3686     case OPT_mcmodel_:
3687       if (strcmp (arg, "small") == 0)
3688         cmodel = CMODEL_SMALL;
3689       else if (strcmp (arg, "large") == 0)
3690         cmodel = CMODEL_LARGE;
3691       else
3692         {
3693           error ("invalid option for -mcmodel: '%s'", arg);
3694           return false;
3695         }
3696       rs6000_explicit_options.cmodel = true;
3697 #endif
3698
3699 #ifdef TARGET_USES_AIX64_OPT
3700     case OPT_maix64:
3701 #else
3702     case OPT_m64:
3703 #endif
3704       target_flags |= MASK_POWERPC64 | MASK_POWERPC;
3705       target_flags |= ~target_flags_explicit & MASK_PPC_GFXOPT;
3706       target_flags_explicit |= MASK_POWERPC64 | MASK_POWERPC;
3707       break;
3708
3709 #ifdef TARGET_USES_AIX64_OPT
3710     case OPT_maix32:
3711 #else
3712     case OPT_m32:
3713 #endif
3714       target_flags &= ~MASK_POWERPC64;
3715       target_flags_explicit |= MASK_POWERPC64;
3716       break;
3717
3718     case OPT_minsert_sched_nops_:
3719       rs6000_sched_insert_nops_str = arg;
3720       break;
3721
3722     case OPT_mminimal_toc:
3723       if (value == 1)
3724         {
3725           TARGET_NO_FP_IN_TOC = 0;
3726           TARGET_NO_SUM_IN_TOC = 0;
3727         }
3728       break;
3729
3730     case OPT_mpower:
3731       if (value == 1)
3732         {
3733           target_flags |= (MASK_MULTIPLE | MASK_STRING);
3734           target_flags_explicit |= (MASK_MULTIPLE | MASK_STRING);
3735         }
3736       break;
3737
3738     case OPT_mpower2:
3739       if (value == 1)
3740         {
3741           target_flags |= (MASK_POWER | MASK_MULTIPLE | MASK_STRING);
3742           target_flags_explicit |= (MASK_POWER | MASK_MULTIPLE | MASK_STRING);
3743         }
3744       break;
3745
3746     case OPT_mpowerpc_gpopt:
3747     case OPT_mpowerpc_gfxopt:
3748       if (value == 1)
3749         {
3750           target_flags |= MASK_POWERPC;
3751           target_flags_explicit |= MASK_POWERPC;
3752         }
3753       break;
3754
3755     case OPT_maix_struct_return:
3756     case OPT_msvr4_struct_return:
3757       rs6000_explicit_options.aix_struct_ret = true;
3758       break;
3759
3760     case OPT_mvrsave:
3761       rs6000_explicit_options.vrsave = true;
3762       TARGET_ALTIVEC_VRSAVE = value;
3763       break;
3764
3765     case OPT_mvrsave_:
3766       rs6000_explicit_options.vrsave = true;
3767       rs6000_parse_yes_no_option ("vrsave", arg, &(TARGET_ALTIVEC_VRSAVE));
3768       break;
3769
3770     case OPT_misel_:
3771       target_flags_explicit |= MASK_ISEL;
3772       isel = 0;
3773       rs6000_parse_yes_no_option ("isel", arg, &isel);
3774       if (isel)
3775         target_flags |= MASK_ISEL;
3776       else
3777         target_flags &= ~MASK_ISEL;
3778       break;
3779
3780     case OPT_mspe:
3781       rs6000_explicit_options.spe = true;
3782       rs6000_spe = value;
3783       break;
3784
3785     case OPT_mspe_:
3786       rs6000_explicit_options.spe = true;
3787       rs6000_parse_yes_no_option ("spe", arg, &(rs6000_spe));
3788       break;
3789
3790     case OPT_mdebug_:
3791       rs6000_debug_name = arg;
3792       break;
3793
3794 #ifdef TARGET_USES_SYSV4_OPT
3795     case OPT_mcall_:
3796       rs6000_abi_name = arg;
3797       break;
3798
3799     case OPT_msdata_:
3800       rs6000_sdata_name = arg;
3801       break;
3802
3803     case OPT_mtls_size_:
3804       rs6000_tls_size_string = arg;
3805       break;
3806
3807     case OPT_mrelocatable:
3808       if (value == 1)
3809         {
3810           target_flags |= MASK_MINIMAL_TOC;
3811           target_flags_explicit |= MASK_MINIMAL_TOC;
3812           TARGET_NO_FP_IN_TOC = 1;
3813         }
3814       break;
3815
3816     case OPT_mrelocatable_lib:
3817       if (value == 1)
3818         {
3819           target_flags |= MASK_RELOCATABLE | MASK_MINIMAL_TOC;
3820           target_flags_explicit |= MASK_RELOCATABLE | MASK_MINIMAL_TOC;
3821           TARGET_NO_FP_IN_TOC = 1;
3822         }
3823       else
3824         {
3825           target_flags &= ~MASK_RELOCATABLE;
3826           target_flags_explicit |= MASK_RELOCATABLE;
3827         }
3828       break;
3829 #endif
3830
3831     case OPT_mabi_:
3832       if (!strcmp (arg, "altivec"))
3833         {
3834           rs6000_explicit_options.altivec_abi = true;
3835           rs6000_altivec_abi = 1;
3836
3837           /* Enabling the AltiVec ABI turns off the SPE ABI.  */
3838           rs6000_spe_abi = 0;
3839         }
3840       else if (! strcmp (arg, "no-altivec"))
3841         {
3842           rs6000_explicit_options.altivec_abi = true;
3843           rs6000_altivec_abi = 0;
3844         }
3845       else if (! strcmp (arg, "spe"))
3846         {
3847           rs6000_explicit_options.spe_abi = true;
3848           rs6000_spe_abi = 1;
3849           rs6000_altivec_abi = 0;
3850           if (!TARGET_SPE_ABI)
3851             error ("not configured for ABI: '%s'", arg);
3852         }
3853       else if (! strcmp (arg, "no-spe"))
3854         {
3855           rs6000_explicit_options.spe_abi = true;
3856           rs6000_spe_abi = 0;
3857         }
3858
3859       /* These are here for testing during development only, do not
3860          document in the manual please.  */
3861       else if (! strcmp (arg, "d64"))
3862         {
3863           rs6000_darwin64_abi = 1;
3864           warning (0, "Using darwin64 ABI");
3865         }
3866       else if (! strcmp (arg, "d32"))
3867         {
3868           rs6000_darwin64_abi = 0;
3869           warning (0, "Using old darwin ABI");
3870         }
3871
3872       else if (! strcmp (arg, "ibmlongdouble"))
3873         {
3874           rs6000_explicit_options.ieee = true;
3875           rs6000_ieeequad = 0;
3876           warning (0, "Using IBM extended precision long double");
3877         }
3878       else if (! strcmp (arg, "ieeelongdouble"))
3879         {
3880           rs6000_explicit_options.ieee = true;
3881           rs6000_ieeequad = 1;
3882           warning (0, "Using IEEE extended precision long double");
3883         }
3884
3885       else
3886         {
3887           error ("unknown ABI specified: '%s'", arg);
3888           return false;
3889         }
3890       break;
3891
3892     case OPT_mcpu_:
3893       rs6000_select[1].string = arg;
3894       break;
3895
3896     case OPT_mtune_:
3897       rs6000_select[2].string = arg;
3898       break;
3899
3900     case OPT_mtraceback_:
3901       rs6000_traceback_name = arg;
3902       break;
3903
3904     case OPT_mfloat_gprs_:
3905       rs6000_explicit_options.float_gprs = true;
3906       if (! strcmp (arg, "yes") || ! strcmp (arg, "single"))
3907         rs6000_float_gprs = 1;
3908       else if (! strcmp (arg, "double"))
3909         rs6000_float_gprs = 2;
3910       else if (! strcmp (arg, "no"))
3911         rs6000_float_gprs = 0;
3912       else
3913         {
3914           error ("invalid option for -mfloat-gprs: '%s'", arg);
3915           return false;
3916         }
3917       break;
3918
3919     case OPT_mlong_double_:
3920       rs6000_explicit_options.long_double = true;
3921       rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3922       if (value != 64 && value != 128)
3923         {
3924           error ("Unknown switch -mlong-double-%s", arg);
3925           rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3926           return false;
3927         }
3928       else
3929         rs6000_long_double_type_size = value;
3930       break;
3931
3932     case OPT_msched_costly_dep_:
3933       rs6000_sched_costly_dep_str = arg;
3934       break;
3935
3936     case OPT_malign_:
3937       rs6000_explicit_options.alignment = true;
3938       if (! strcmp (arg, "power"))
3939         {
3940           /* On 64-bit Darwin, power alignment is ABI-incompatible with
3941              some C library functions, so warn about it. The flag may be
3942              useful for performance studies from time to time though, so
3943              don't disable it entirely.  */
3944           if (DEFAULT_ABI == ABI_DARWIN && TARGET_64BIT)
3945             warning (0, "-malign-power is not supported for 64-bit Darwin;"
3946                      " it is incompatible with the installed C and C++ libraries");
3947           rs6000_alignment_flags = MASK_ALIGN_POWER;
3948         }
3949       else if (! strcmp (arg, "natural"))
3950         rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3951       else
3952         {
3953           error ("unknown -malign-XXXXX option specified: '%s'", arg);
3954           return false;
3955         }
3956       break;
3957
3958     case OPT_msingle_float:
3959       if (!TARGET_SINGLE_FPU) 
3960         warning (0, "-msingle-float option equivalent to -mhard-float");
3961       /* -msingle-float implies -mno-double-float and TARGET_HARD_FLOAT. */
3962       rs6000_double_float = 0;
3963       target_flags &= ~MASK_SOFT_FLOAT;
3964       target_flags_explicit |= MASK_SOFT_FLOAT;
3965       break;
3966
3967     case OPT_mdouble_float:
3968       /* -mdouble-float implies -msingle-float and TARGET_HARD_FLOAT. */
3969       rs6000_single_float = 1;
3970       target_flags &= ~MASK_SOFT_FLOAT;
3971       target_flags_explicit |= MASK_SOFT_FLOAT;
3972       break;
3973
3974     case OPT_msimple_fpu:
3975       if (!TARGET_SINGLE_FPU) 
3976         warning (0, "-msimple-fpu option ignored");
3977       break;
3978
3979     case OPT_mhard_float:
3980       /* -mhard_float implies -msingle-float and -mdouble-float. */
3981       rs6000_single_float = rs6000_double_float = 1;
3982       break;
3983
3984     case OPT_msoft_float:
3985       /* -msoft_float implies -mnosingle-float and -mnodouble-float. */
3986       rs6000_single_float = rs6000_double_float = 0;
3987       break;
3988
3989     case OPT_mfpu_:
3990       fpu_type = rs6000_parse_fpu_option(arg);
3991       if (fpu_type != FPU_NONE) 
3992       /* If -mfpu is not none, then turn off SOFT_FLOAT, turn on HARD_FLOAT. */
3993       {
3994         target_flags &= ~MASK_SOFT_FLOAT;
3995         target_flags_explicit |= MASK_SOFT_FLOAT;
3996         rs6000_xilinx_fpu = 1;
3997         if (fpu_type == FPU_SF_LITE || fpu_type == FPU_SF_FULL) 
3998         rs6000_single_float = 1;
3999         if (fpu_type == FPU_DF_LITE || fpu_type == FPU_DF_FULL) 
4000           rs6000_single_float = rs6000_double_float = 1;
4001         if (fpu_type == FPU_SF_LITE || fpu_type == FPU_DF_LITE) 
4002           rs6000_simple_fpu = 1;
4003       }
4004       else
4005       {
4006         /* -mfpu=none is equivalent to -msoft-float */
4007         target_flags |= MASK_SOFT_FLOAT;
4008         target_flags_explicit |= MASK_SOFT_FLOAT;
4009         rs6000_single_float = rs6000_double_float = 0;
4010       }
4011
4012     case OPT_mrecip:
4013       rs6000_recip_name = (value) ? "default" : "none";
4014       break;
4015
4016     case OPT_mrecip_:
4017       rs6000_recip_name = arg;
4018       break;
4019     }
4020   return true;
4021 }
4022 \f
4023 /* Do anything needed at the start of the asm file.  */
4024
4025 static void
4026 rs6000_file_start (void)
4027 {
4028   size_t i;
4029   char buffer[80];
4030   const char *start = buffer;
4031   struct rs6000_cpu_select *ptr;
4032   const char *default_cpu = TARGET_CPU_DEFAULT;
4033   FILE *file = asm_out_file;
4034
4035   default_file_start ();
4036
4037 #ifdef TARGET_BI_ARCH
4038   if ((TARGET_DEFAULT ^ target_flags) & MASK_64BIT)
4039     default_cpu = 0;
4040 #endif
4041
4042   if (flag_verbose_asm)
4043     {
4044       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4045       rs6000_select[0].string = default_cpu;
4046
4047       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
4048         {
4049           ptr = &rs6000_select[i];
4050           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
4051             {
4052               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
4053               start = "";
4054             }
4055         }
4056
4057       if (PPC405_ERRATUM77)
4058         {
4059           fprintf (file, "%s PPC405CR_ERRATUM77", start);
4060           start = "";
4061         }
4062
4063 #ifdef USING_ELFOS_H
4064       switch (rs6000_sdata)
4065         {
4066         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4067         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4068         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4069         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4070         }
4071
4072       if (rs6000_sdata && g_switch_value)
4073         {
4074           fprintf (file, "%s -G " HOST_WIDE_INT_PRINT_UNSIGNED, start,
4075                    g_switch_value);
4076           start = "";
4077         }
4078 #endif
4079
4080       if (*start == '\0')
4081         putc ('\n', file);
4082     }
4083
4084 #ifdef HAVE_AS_GNU_ATTRIBUTE
4085   if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
4086     {
4087       fprintf (file, "\t.gnu_attribute 4, %d\n",
4088                ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1 
4089                 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3 
4090                 : 2));
4091       fprintf (file, "\t.gnu_attribute 8, %d\n",
4092                (TARGET_ALTIVEC_ABI ? 2
4093                 : TARGET_SPE_ABI ? 3
4094                 : 1));
4095       fprintf (file, "\t.gnu_attribute 12, %d\n",
4096                aix_struct_return ? 2 : 1);
4097
4098     }
4099 #endif
4100
4101   if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4102     {
4103       switch_to_section (toc_section);
4104       switch_to_section (text_section);
4105     }
4106 }
4107
4108 \f
4109 /* Return nonzero if this function is known to have a null epilogue.  */
4110
4111 int
4112 direct_return (void)
4113 {
4114   if (reload_completed)
4115     {
4116       rs6000_stack_t *info = rs6000_stack_info ();
4117
4118       if (info->first_gp_reg_save == 32
4119           && info->first_fp_reg_save == 64
4120           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4121           && ! info->lr_save_p
4122           && ! info->cr_save_p
4123           && info->vrsave_mask == 0
4124           && ! info->push_p)
4125         return 1;
4126     }
4127
4128   return 0;
4129 }
4130
4131 /* Return the number of instructions it takes to form a constant in an
4132    integer register.  */
4133
4134 int
4135 num_insns_constant_wide (HOST_WIDE_INT value)
4136 {
4137   /* signed constant loadable with {cal|addi} */
4138   if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4139     return 1;
4140
4141   /* constant loadable with {cau|addis} */
4142   else if ((value & 0xffff) == 0
4143            && (value >> 31 == -1 || value >> 31 == 0))
4144     return 1;
4145
4146 #if HOST_BITS_PER_WIDE_INT == 64
4147   else if (TARGET_POWERPC64)
4148     {
4149       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4150       HOST_WIDE_INT high = value >> 31;
4151
4152       if (high == 0 || high == -1)
4153         return 2;
4154
4155       high >>= 1;
4156
4157       if (low == 0)
4158         return num_insns_constant_wide (high) + 1;
4159       else if (high == 0)
4160         return num_insns_constant_wide (low) + 1;
4161       else
4162         return (num_insns_constant_wide (high)
4163                 + num_insns_constant_wide (low) + 1);
4164     }
4165 #endif
4166
4167   else
4168     return 2;
4169 }
4170
4171 int
4172 num_insns_constant (rtx op, enum machine_mode mode)
4173 {
4174   HOST_WIDE_INT low, high;
4175
4176   switch (GET_CODE (op))
4177     {
4178     case CONST_INT:
4179 #if HOST_BITS_PER_WIDE_INT == 64
4180       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4181           && mask64_operand (op, mode))
4182         return 2;
4183       else
4184 #endif
4185         return num_insns_constant_wide (INTVAL (op));
4186
4187       case CONST_DOUBLE:
4188         if (mode == SFmode || mode == SDmode)
4189           {
4190             long l;
4191             REAL_VALUE_TYPE rv;
4192
4193             REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4194             if (DECIMAL_FLOAT_MODE_P (mode))
4195               REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4196             else
4197               REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4198             return num_insns_constant_wide ((HOST_WIDE_INT) l);
4199           }
4200
4201         if (mode == VOIDmode || mode == DImode)
4202           {
4203             high = CONST_DOUBLE_HIGH (op);
4204             low  = CONST_DOUBLE_LOW (op);
4205           }
4206         else
4207           {
4208             long l[2];
4209             REAL_VALUE_TYPE rv;
4210
4211             REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4212             if (DECIMAL_FLOAT_MODE_P (mode))
4213               REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4214             else
4215               REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4216             high = l[WORDS_BIG_ENDIAN == 0];
4217             low  = l[WORDS_BIG_ENDIAN != 0];
4218           }
4219
4220         if (TARGET_32BIT)
4221           return (num_insns_constant_wide (low)
4222                   + num_insns_constant_wide (high));
4223         else
4224           {
4225             if ((high == 0 && low >= 0)
4226                 || (high == -1 && low < 0))
4227               return num_insns_constant_wide (low);
4228
4229             else if (mask64_operand (op, mode))
4230               return 2;
4231
4232             else if (low == 0)
4233               return num_insns_constant_wide (high) + 1;
4234
4235             else
4236               return (num_insns_constant_wide (high)
4237                       + num_insns_constant_wide (low) + 1);
4238           }
4239
4240     default:
4241       gcc_unreachable ();
4242     }
4243 }
4244
4245 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4246    If the mode of OP is MODE_VECTOR_INT, this simply returns the
4247    corresponding element of the vector, but for V4SFmode and V2SFmode,
4248    the corresponding "float" is interpreted as an SImode integer.  */
4249
4250 HOST_WIDE_INT
4251 const_vector_elt_as_int (rtx op, unsigned int elt)
4252 {
4253   rtx tmp = CONST_VECTOR_ELT (op, elt);
4254   if (GET_MODE (op) == V4SFmode
4255       || GET_MODE (op) == V2SFmode)
4256     tmp = gen_lowpart (SImode, tmp);
4257   return INTVAL (tmp);
4258 }
4259
4260 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4261    or vspltisw instruction.  OP is a CONST_VECTOR.  Which instruction is used
4262    depends on STEP and COPIES, one of which will be 1.  If COPIES > 1,
4263    all items are set to the same value and contain COPIES replicas of the
4264    vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4265    operand and the others are set to the value of the operand's msb.  */
4266
4267 static bool
4268 vspltis_constant (rtx op, unsigned step, unsigned copies)
4269 {
4270   enum machine_mode mode = GET_MODE (op);
4271   enum machine_mode inner = GET_MODE_INNER (mode);
4272
4273   unsigned i;
4274   unsigned nunits = GET_MODE_NUNITS (mode);
4275   unsigned bitsize = GET_MODE_BITSIZE (inner);
4276   unsigned mask = GET_MODE_MASK (inner);
4277
4278   HOST_WIDE_INT val = const_vector_elt_as_int (op, nunits - 1);
4279   HOST_WIDE_INT splat_val = val;
4280   HOST_WIDE_INT msb_val = val > 0 ? 0 : -1;
4281
4282   /* Construct the value to be splatted, if possible.  If not, return 0.  */
4283   for (i = 2; i <= copies; i *= 2)
4284     {
4285       HOST_WIDE_INT small_val;
4286       bitsize /= 2;
4287       small_val = splat_val >> bitsize;
4288       mask >>= bitsize;
4289       if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4290         return false;
4291       splat_val = small_val;
4292     }
4293
4294   /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw].  */
4295   if (EASY_VECTOR_15 (splat_val))
4296     ;
4297
4298   /* Also check if we can splat, and then add the result to itself.  Do so if
4299      the value is positive, of if the splat instruction is using OP's mode;
4300      for splat_val < 0, the splat and the add should use the same mode.  */
4301   else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4302            && (splat_val >= 0 || (step == 1 && copies == 1)))
4303     ;
4304
4305   /* Also check if are loading up the most significant bit which can be done by
4306      loading up -1 and shifting the value left by -1.  */
4307   else if (EASY_VECTOR_MSB (splat_val, inner))
4308     ;
4309
4310   else
4311     return false;
4312
4313   /* Check if VAL is present in every STEP-th element, and the
4314      other elements are filled with its most significant bit.  */
4315   for (i = 0; i < nunits - 1; ++i)
4316     {
4317       HOST_WIDE_INT desired_val;
4318       if (((i + 1) & (step - 1)) == 0)
4319         desired_val = val;
4320       else
4321         desired_val = msb_val;
4322
4323       if (desired_val != const_vector_elt_as_int (op, i))
4324         return false;
4325     }
4326
4327   return true;
4328 }
4329
4330
4331 /* Return true if OP is of the given MODE and can be synthesized
4332    with a vspltisb, vspltish or vspltisw.  */
4333
4334 bool
4335 easy_altivec_constant (rtx op, enum machine_mode mode)
4336 {
4337   unsigned step, copies;
4338
4339   if (mode == VOIDmode)
4340     mode = GET_MODE (op);
4341   else if (mode != GET_MODE (op))
4342     return false;
4343
4344   /* Start with a vspltisw.  */
4345   step = GET_MODE_NUNITS (mode) / 4;
4346   copies = 1;
4347
4348   if (vspltis_constant (op, step, copies))
4349     return true;
4350
4351   /* Then try with a vspltish.  */
4352   if (step == 1)
4353     copies <<= 1;
4354   else
4355     step >>= 1;
4356
4357   if (vspltis_constant (op, step, copies))
4358     return true;
4359
4360   /* And finally a vspltisb.  */
4361   if (step == 1)
4362     copies <<= 1;
4363   else
4364     step >>= 1;
4365
4366   if (vspltis_constant (op, step, copies))
4367     return true;
4368
4369   return false;
4370 }
4371
4372 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4373    result is OP.  Abort if it is not possible.  */
4374
4375 rtx
4376 gen_easy_altivec_constant (rtx op)
4377 {
4378   enum machine_mode mode = GET_MODE (op);
4379   int nunits = GET_MODE_NUNITS (mode);
4380   rtx last = CONST_VECTOR_ELT (op, nunits - 1);
4381   unsigned step = nunits / 4;
4382   unsigned copies = 1;
4383
4384   /* Start with a vspltisw.  */
4385   if (vspltis_constant (op, step, copies))
4386     return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, last));
4387
4388   /* Then try with a vspltish.  */
4389   if (step == 1)
4390     copies <<= 1;
4391   else
4392     step >>= 1;
4393
4394   if (vspltis_constant (op, step, copies))
4395     return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, last));
4396
4397   /* And finally a vspltisb.  */
4398   if (step == 1)
4399     copies <<= 1;
4400   else
4401     step >>= 1;
4402
4403   if (vspltis_constant (op, step, copies))
4404     return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, last));
4405
4406   gcc_unreachable ();
4407 }
4408
4409 const char *
4410 output_vec_const_move (rtx *operands)
4411 {
4412   int cst, cst2;
4413   enum machine_mode mode;
4414   rtx dest, vec;
4415
4416   dest = operands[0];
4417   vec = operands[1];
4418   mode = GET_MODE (dest);
4419
4420   if (TARGET_VSX && zero_constant (vec, mode))
4421     return "xxlxor %x0,%x0,%x0";
4422
4423   if (TARGET_ALTIVEC)
4424     {
4425       rtx splat_vec;
4426       if (zero_constant (vec, mode))
4427         return "vxor %0,%0,%0";
4428
4429       splat_vec = gen_easy_altivec_constant (vec);
4430       gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4431       operands[1] = XEXP (splat_vec, 0);
4432       if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4433         return "#";
4434
4435       switch (GET_MODE (splat_vec))
4436         {
4437         case V4SImode:
4438           return "vspltisw %0,%1";
4439
4440         case V8HImode:
4441           return "vspltish %0,%1";
4442
4443         case V16QImode:
4444           return "vspltisb %0,%1";
4445
4446         default:
4447           gcc_unreachable ();
4448         }
4449     }
4450
4451   gcc_assert (TARGET_SPE);
4452
4453   /* Vector constant 0 is handled as a splitter of V2SI, and in the
4454      pattern of V1DI, V4HI, and V2SF.
4455
4456      FIXME: We should probably return # and add post reload
4457      splitters for these, but this way is so easy ;-).  */
4458   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4459   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4460   operands[1] = CONST_VECTOR_ELT (vec, 0);
4461   operands[2] = CONST_VECTOR_ELT (vec, 1);
4462   if (cst == cst2)
4463     return "li %0,%1\n\tevmergelo %0,%0,%0";
4464   else
4465     return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4466 }
4467
4468 /* Initialize TARGET of vector PAIRED to VALS.  */
4469
4470 void
4471 paired_expand_vector_init (rtx target, rtx vals)
4472 {
4473   enum machine_mode mode = GET_MODE (target);
4474   int n_elts = GET_MODE_NUNITS (mode);
4475   int n_var = 0;
4476   rtx x, new_rtx, tmp, constant_op, op1, op2;
4477   int i;
4478
4479   for (i = 0; i < n_elts; ++i)
4480     {
4481       x = XVECEXP (vals, 0, i);
4482       if (!CONSTANT_P (x))
4483         ++n_var;
4484     }
4485   if (n_var == 0)
4486     {
4487       /* Load from constant pool.  */
4488       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4489       return;
4490     }
4491
4492   if (n_var == 2)
4493     {
4494       /* The vector is initialized only with non-constants.  */
4495       new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4496                                 XVECEXP (vals, 0, 1));
4497
4498       emit_move_insn (target, new_rtx);
4499       return;
4500     }
4501   
4502   /* One field is non-constant and the other one is a constant.  Load the
4503      constant from the constant pool and use ps_merge instruction to
4504      construct the whole vector.  */
4505   op1 = XVECEXP (vals, 0, 0);
4506   op2 = XVECEXP (vals, 0, 1);
4507
4508   constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4509
4510   tmp = gen_reg_rtx (GET_MODE (constant_op));
4511   emit_move_insn (tmp, constant_op);
4512
4513   if (CONSTANT_P (op1))
4514     new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4515   else
4516     new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4517
4518   emit_move_insn (target, new_rtx);
4519 }
4520
4521 void
4522 paired_expand_vector_move (rtx operands[])
4523 {
4524   rtx op0 = operands[0], op1 = operands[1];
4525
4526   emit_move_insn (op0, op1);
4527 }
4528
4529 /* Emit vector compare for code RCODE.  DEST is destination, OP1 and
4530    OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4531    operands for the relation operation COND.  This is a recursive
4532    function.  */
4533
4534 static void
4535 paired_emit_vector_compare (enum rtx_code rcode,
4536                             rtx dest, rtx op0, rtx op1,
4537                             rtx cc_op0, rtx cc_op1)
4538 {
4539   rtx tmp = gen_reg_rtx (V2SFmode);
4540   rtx tmp1, max, min;
4541
4542   gcc_assert (TARGET_PAIRED_FLOAT);
4543   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
4544
4545   switch (rcode)
4546     {
4547     case LT:
4548     case LTU:
4549       paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4550       return;
4551     case GE:
4552     case GEU:
4553       emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4554       emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
4555       return;
4556     case LE:
4557     case LEU:
4558       paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
4559       return;
4560     case GT:
4561       paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4562       return;
4563     case EQ:
4564       tmp1 = gen_reg_rtx (V2SFmode);
4565       max = gen_reg_rtx (V2SFmode);
4566       min = gen_reg_rtx (V2SFmode);
4567       gen_reg_rtx (V2SFmode);
4568       
4569       emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4570       emit_insn (gen_selv2sf4
4571                  (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4572       emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
4573       emit_insn (gen_selv2sf4
4574                  (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4575       emit_insn (gen_subv2sf3 (tmp1, min, max));
4576       emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
4577       return;
4578     case NE:
4579       paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
4580       return;
4581     case UNLE:
4582       paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4583       return;
4584     case UNLT:
4585       paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
4586       return;
4587     case UNGE:
4588       paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4589       return;
4590     case UNGT:
4591       paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
4592       return;
4593     default:
4594       gcc_unreachable ();
4595     }
4596
4597   return;
4598 }
4599
4600 /* Emit vector conditional expression.
4601    DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
4602    CC_OP0 and CC_OP1 are the two operands for the relation operation COND.  */
4603
4604 int
4605 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
4606                               rtx cond, rtx cc_op0, rtx cc_op1)
4607 {
4608   enum rtx_code rcode = GET_CODE (cond);
4609
4610   if (!TARGET_PAIRED_FLOAT)
4611     return 0;
4612
4613   paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
4614
4615   return 1;
4616 }
4617
4618 /* Initialize vector TARGET to VALS.  */
4619
4620 void
4621 rs6000_expand_vector_init (rtx target, rtx vals)
4622 {
4623   enum machine_mode mode = GET_MODE (target);
4624   enum machine_mode inner_mode = GET_MODE_INNER (mode);
4625   int n_elts = GET_MODE_NUNITS (mode);
4626   int n_var = 0, one_var = -1;
4627   bool all_same = true, all_const_zero = true;
4628   rtx x, mem;
4629   int i;
4630
4631   for (i = 0; i < n_elts; ++i)
4632     {
4633       x = XVECEXP (vals, 0, i);
4634       if (!CONSTANT_P (x))
4635         ++n_var, one_var = i;
4636       else if (x != CONST0_RTX (inner_mode))
4637         all_const_zero = false;
4638
4639       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
4640         all_same = false;
4641     }
4642
4643   if (n_var == 0)
4644     {
4645       rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
4646       bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
4647       if ((int_vector_p || TARGET_VSX) && all_const_zero)
4648         {
4649           /* Zero register.  */
4650           emit_insn (gen_rtx_SET (VOIDmode, target,
4651                                   gen_rtx_XOR (mode, target, target)));
4652           return;
4653         }
4654       else if (int_vector_p && easy_vector_constant (const_vec, mode))
4655         {
4656           /* Splat immediate.  */
4657           emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
4658           return;
4659         }
4660       else
4661         {
4662           /* Load from constant pool.  */
4663           emit_move_insn (target, const_vec);
4664           return;
4665         }
4666     }
4667
4668   /* Double word values on VSX can use xxpermdi or lxvdsx.  */
4669   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4670     {
4671       if (all_same)
4672         {
4673           rtx element = XVECEXP (vals, 0, 0);
4674           if (mode == V2DFmode)
4675             emit_insn (gen_vsx_splat_v2df (target, element));
4676           else
4677             emit_insn (gen_vsx_splat_v2di (target, element));
4678         }
4679       else
4680         {
4681           rtx op0 = copy_to_reg (XVECEXP (vals, 0, 0));
4682           rtx op1 = copy_to_reg (XVECEXP (vals, 0, 1));
4683           if (mode == V2DFmode)
4684             emit_insn (gen_vsx_concat_v2df (target, op0, op1));
4685           else
4686             emit_insn (gen_vsx_concat_v2di (target, op0, op1));
4687         }
4688       return;
4689     }
4690
4691   /* With single precision floating point on VSX, know that internally single
4692      precision is actually represented as a double, and either make 2 V2DF
4693      vectors, and convert these vectors to single precision, or do one
4694      conversion, and splat the result to the other elements.  */
4695   if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
4696     {
4697       if (all_same)
4698         {
4699           rtx freg = gen_reg_rtx (V4SFmode);
4700           rtx sreg = copy_to_reg (XVECEXP (vals, 0, 0));
4701
4702           emit_insn (gen_vsx_xscvdpsp_scalar (freg, sreg));
4703           emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
4704         }
4705       else
4706         {
4707           rtx dbl_even = gen_reg_rtx (V2DFmode);
4708           rtx dbl_odd  = gen_reg_rtx (V2DFmode);
4709           rtx flt_even = gen_reg_rtx (V4SFmode);
4710           rtx flt_odd  = gen_reg_rtx (V4SFmode);
4711
4712           emit_insn (gen_vsx_concat_v2sf (dbl_even,
4713                                           copy_to_reg (XVECEXP (vals, 0, 0)),
4714                                           copy_to_reg (XVECEXP (vals, 0, 1))));
4715           emit_insn (gen_vsx_concat_v2sf (dbl_odd,
4716                                           copy_to_reg (XVECEXP (vals, 0, 2)),
4717                                           copy_to_reg (XVECEXP (vals, 0, 3))));
4718           emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
4719           emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
4720           emit_insn (gen_vec_extract_evenv4sf (target, flt_even, flt_odd));
4721         }
4722       return;
4723     }
4724
4725   /* Store value to stack temp.  Load vector element.  Splat.  However, splat
4726      of 64-bit items is not supported on Altivec.  */
4727   if (all_same && GET_MODE_SIZE (mode) <= 4)
4728     {
4729       mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode), 0);
4730       emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
4731                       XVECEXP (vals, 0, 0));
4732       x = gen_rtx_UNSPEC (VOIDmode,
4733                           gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4734       emit_insn (gen_rtx_PARALLEL (VOIDmode,
4735                                    gen_rtvec (2,
4736                                               gen_rtx_SET (VOIDmode,
4737                                                            target, mem),
4738                                               x)));
4739       x = gen_rtx_VEC_SELECT (inner_mode, target,
4740                               gen_rtx_PARALLEL (VOIDmode,
4741                                                 gen_rtvec (1, const0_rtx)));
4742       emit_insn (gen_rtx_SET (VOIDmode, target,
4743                               gen_rtx_VEC_DUPLICATE (mode, x)));
4744       return;
4745     }
4746
4747   /* One field is non-constant.  Load constant then overwrite
4748      varying field.  */
4749   if (n_var == 1)
4750     {
4751       rtx copy = copy_rtx (vals);
4752
4753       /* Load constant part of vector, substitute neighboring value for
4754          varying element.  */
4755       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
4756       rs6000_expand_vector_init (target, copy);
4757
4758       /* Insert variable.  */
4759       rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
4760       return;
4761     }
4762
4763   /* Construct the vector in memory one field at a time
4764      and load the whole vector.  */
4765   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
4766   for (i = 0; i < n_elts; i++)
4767     emit_move_insn (adjust_address_nv (mem, inner_mode,
4768                                     i * GET_MODE_SIZE (inner_mode)),
4769                     XVECEXP (vals, 0, i));
4770   emit_move_insn (target, mem);
4771 }
4772
4773 /* Set field ELT of TARGET to VAL.  */
4774
4775 void
4776 rs6000_expand_vector_set (rtx target, rtx val, int elt)
4777 {
4778   enum machine_mode mode = GET_MODE (target);
4779   enum machine_mode inner_mode = GET_MODE_INNER (mode);
4780   rtx reg = gen_reg_rtx (mode);
4781   rtx mask, mem, x;
4782   int width = GET_MODE_SIZE (inner_mode);
4783   int i;
4784
4785   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4786     {
4787       rtx (*set_func) (rtx, rtx, rtx, rtx)
4788         = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
4789       emit_insn (set_func (target, target, val, GEN_INT (elt)));
4790       return;
4791     }
4792
4793   /* Load single variable value.  */
4794   mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode), 0);
4795   emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
4796   x = gen_rtx_UNSPEC (VOIDmode,
4797                       gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4798   emit_insn (gen_rtx_PARALLEL (VOIDmode,
4799                                gen_rtvec (2,
4800                                           gen_rtx_SET (VOIDmode,
4801                                                        reg, mem),
4802                                           x)));
4803
4804   /* Linear sequence.  */
4805   mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
4806   for (i = 0; i < 16; ++i)
4807     XVECEXP (mask, 0, i) = GEN_INT (i);
4808
4809   /* Set permute mask to insert element into target.  */
4810   for (i = 0; i < width; ++i)
4811     XVECEXP (mask, 0, elt*width + i)
4812       = GEN_INT (i + 0x10);
4813   x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
4814   x = gen_rtx_UNSPEC (mode,
4815                       gen_rtvec (3, target, reg,
4816                                  force_reg (V16QImode, x)),
4817                       UNSPEC_VPERM);
4818   emit_insn (gen_rtx_SET (VOIDmode, target, x));
4819 }
4820
4821 /* Extract field ELT from VEC into TARGET.  */
4822
4823 void
4824 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
4825 {
4826   enum machine_mode mode = GET_MODE (vec);
4827   enum machine_mode inner_mode = GET_MODE_INNER (mode);
4828   rtx mem, x;
4829
4830   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4831     {
4832       rtx (*extract_func) (rtx, rtx, rtx)
4833         = ((mode == V2DFmode) ? gen_vsx_extract_v2df : gen_vsx_extract_v2di);
4834       emit_insn (extract_func (target, vec, GEN_INT (elt)));
4835       return;
4836     }
4837
4838   /* Allocate mode-sized buffer.  */
4839   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
4840
4841   /* Add offset to field within buffer matching vector element.  */
4842   mem = adjust_address_nv (mem, mode, elt * GET_MODE_SIZE (inner_mode));
4843
4844   /* Store single field into mode-sized buffer.  */
4845   x = gen_rtx_UNSPEC (VOIDmode,
4846                       gen_rtvec (1, const0_rtx), UNSPEC_STVE);
4847   emit_insn (gen_rtx_PARALLEL (VOIDmode,
4848                                gen_rtvec (2,
4849                                           gen_rtx_SET (VOIDmode,
4850                                                        mem, vec),
4851                                           x)));
4852   emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
4853 }
4854
4855 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
4856    implement ANDing by the mask IN.  */
4857 void
4858 build_mask64_2_operands (rtx in, rtx *out)
4859 {
4860 #if HOST_BITS_PER_WIDE_INT >= 64
4861   unsigned HOST_WIDE_INT c, lsb, m1, m2;
4862   int shift;
4863
4864   gcc_assert (GET_CODE (in) == CONST_INT);
4865
4866   c = INTVAL (in);
4867   if (c & 1)
4868     {
4869       /* Assume c initially something like 0x00fff000000fffff.  The idea
4870          is to rotate the word so that the middle ^^^^^^ group of zeros
4871          is at the MS end and can be cleared with an rldicl mask.  We then
4872          rotate back and clear off the MS    ^^ group of zeros with a
4873          second rldicl.  */
4874       c = ~c;                   /*   c == 0xff000ffffff00000 */
4875       lsb = c & -c;             /* lsb == 0x0000000000100000 */
4876       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
4877       c = ~c;                   /*   c == 0x00fff000000fffff */
4878       c &= -lsb;                /*   c == 0x00fff00000000000 */
4879       lsb = c & -c;             /* lsb == 0x0000100000000000 */
4880       c = ~c;                   /*   c == 0xff000fffffffffff */
4881       c &= -lsb;                /*   c == 0xff00000000000000 */
4882       shift = 0;
4883       while ((lsb >>= 1) != 0)
4884         shift++;                /* shift == 44 on exit from loop */
4885       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
4886       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
4887       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
4888     }
4889   else
4890     {
4891       /* Assume c initially something like 0xff000f0000000000.  The idea
4892          is to rotate the word so that the     ^^^  middle group of zeros
4893          is at the LS end and can be cleared with an rldicr mask.  We then
4894          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
4895          a second rldicr.  */
4896       lsb = c & -c;             /* lsb == 0x0000010000000000 */
4897       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
4898       c = ~c;                   /*   c == 0x00fff0ffffffffff */
4899       c &= -lsb;                /*   c == 0x00fff00000000000 */
4900       lsb = c & -c;             /* lsb == 0x0000100000000000 */
4901       c = ~c;                   /*   c == 0xff000fffffffffff */
4902       c &= -lsb;                /*   c == 0xff00000000000000 */
4903       shift = 0;
4904       while ((lsb >>= 1) != 0)
4905         shift++;                /* shift == 44 on exit from loop */
4906       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
4907       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
4908       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
4909     }
4910
4911   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
4912      masks will be all 1's.  We are guaranteed more than one transition.  */
4913   out[0] = GEN_INT (64 - shift);
4914   out[1] = GEN_INT (m1);
4915   out[2] = GEN_INT (shift);
4916   out[3] = GEN_INT (m2);
4917 #else
4918   (void)in;
4919   (void)out;
4920   gcc_unreachable ();
4921 #endif
4922 }
4923
4924 /* Return TRUE if OP is an invalid SUBREG operation on the e500.  */
4925
4926 bool
4927 invalid_e500_subreg (rtx op, enum machine_mode mode)
4928 {
4929   if (TARGET_E500_DOUBLE)
4930     {
4931       /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
4932          subreg:TI and reg:TF.  Decimal float modes are like integer
4933          modes (only low part of each register used) for this
4934          purpose.  */
4935       if (GET_CODE (op) == SUBREG
4936           && (mode == SImode || mode == DImode || mode == TImode
4937               || mode == DDmode || mode == TDmode)
4938           && REG_P (SUBREG_REG (op))
4939           && (GET_MODE (SUBREG_REG (op)) == DFmode
4940               || GET_MODE (SUBREG_REG (op)) == TFmode))
4941         return true;
4942
4943       /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
4944          reg:TI.  */
4945       if (GET_CODE (op) == SUBREG
4946           && (mode == DFmode || mode == TFmode)
4947           && REG_P (SUBREG_REG (op))
4948           && (GET_MODE (SUBREG_REG (op)) == DImode
4949               || GET_MODE (SUBREG_REG (op)) == TImode
4950               || GET_MODE (SUBREG_REG (op)) == DDmode
4951               || GET_MODE (SUBREG_REG (op)) == TDmode))
4952         return true;
4953     }
4954
4955   if (TARGET_SPE
4956       && GET_CODE (op) == SUBREG
4957       && mode == SImode
4958       && REG_P (SUBREG_REG (op))
4959       && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
4960     return true;
4961
4962   return false;
4963 }
4964
4965 /* AIX increases natural record alignment to doubleword if the first
4966    field is an FP double while the FP fields remain word aligned.  */
4967
4968 unsigned int
4969 rs6000_special_round_type_align (tree type, unsigned int computed,
4970                                  unsigned int specified)
4971 {
4972   unsigned int align = MAX (computed, specified);
4973   tree field = TYPE_FIELDS (type);
4974
4975   /* Skip all non field decls */
4976   while (field != NULL && TREE_CODE (field) != FIELD_DECL)
4977     field = TREE_CHAIN (field);
4978
4979   if (field != NULL && field != type)
4980     {
4981       type = TREE_TYPE (field);
4982       while (TREE_CODE (type) == ARRAY_TYPE)
4983         type = TREE_TYPE (type);
4984
4985       if (type != error_mark_node && TYPE_MODE (type) == DFmode)
4986         align = MAX (align, 64);
4987     }
4988
4989   return align;
4990 }
4991
4992 /* Darwin increases record alignment to the natural alignment of
4993    the first field.  */
4994
4995 unsigned int
4996 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
4997                                         unsigned int specified)
4998 {
4999   unsigned int align = MAX (computed, specified);
5000
5001   if (TYPE_PACKED (type))
5002     return align;
5003
5004   /* Find the first field, looking down into aggregates.  */
5005   do {
5006     tree field = TYPE_FIELDS (type);
5007     /* Skip all non field decls */
5008     while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5009       field = TREE_CHAIN (field);
5010     if (! field)
5011       break;
5012     /* A packed field does not contribute any extra alignment.  */
5013     if (DECL_PACKED (field))
5014       return align;
5015     type = TREE_TYPE (field);
5016     while (TREE_CODE (type) == ARRAY_TYPE)
5017       type = TREE_TYPE (type);
5018   } while (AGGREGATE_TYPE_P (type));
5019
5020   if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5021     align = MAX (align, TYPE_ALIGN (type));
5022
5023   return align;
5024 }
5025
5026 /* Return 1 for an operand in small memory on V.4/eabi.  */
5027
5028 int
5029 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5030                     enum machine_mode mode ATTRIBUTE_UNUSED)
5031 {
5032 #if TARGET_ELF
5033   rtx sym_ref;
5034
5035   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5036     return 0;
5037
5038   if (DEFAULT_ABI != ABI_V4)
5039     return 0;
5040
5041   /* Vector and float memory instructions have a limited offset on the
5042      SPE, so using a vector or float variable directly as an operand is
5043      not useful.  */
5044   if (TARGET_SPE
5045       && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5046     return 0;
5047
5048   if (GET_CODE (op) == SYMBOL_REF)
5049     sym_ref = op;
5050
5051   else if (GET_CODE (op) != CONST
5052            || GET_CODE (XEXP (op, 0)) != PLUS
5053            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5054            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5055     return 0;
5056
5057   else
5058     {
5059       rtx sum = XEXP (op, 0);
5060       HOST_WIDE_INT summand;
5061
5062       /* We have to be careful here, because it is the referenced address
5063          that must be 32k from _SDA_BASE_, not just the symbol.  */
5064       summand = INTVAL (XEXP (sum, 1));
5065       if (summand < 0 || (unsigned HOST_WIDE_INT) summand > g_switch_value)
5066         return 0;
5067
5068       sym_ref = XEXP (sum, 0);
5069     }
5070
5071   return SYMBOL_REF_SMALL_P (sym_ref);
5072 #else
5073   return 0;
5074 #endif
5075 }
5076
5077 /* Return true if either operand is a general purpose register.  */
5078
5079 bool
5080 gpr_or_gpr_p (rtx op0, rtx op1)
5081 {
5082   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5083           || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5084 }
5085
5086 \f
5087 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p.  */
5088
5089 static bool
5090 reg_offset_addressing_ok_p (enum machine_mode mode)
5091 {
5092   switch (mode)
5093     {
5094     case V16QImode:
5095     case V8HImode:
5096     case V4SFmode:
5097     case V4SImode:
5098     case V2DFmode:
5099     case V2DImode:
5100       /* AltiVec/VSX vector modes.  Only reg+reg addressing is valid.  */
5101       if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5102         return false;
5103       break;
5104
5105     case V4HImode:
5106     case V2SImode:
5107     case V1DImode:
5108     case V2SFmode:
5109        /* Paired vector modes.  Only reg+reg addressing is valid.  */
5110       if (TARGET_PAIRED_FLOAT)
5111         return false;
5112       break;
5113
5114     default:
5115       break;
5116     }
5117
5118   return true;
5119 }
5120
5121 static bool
5122 virtual_stack_registers_memory_p (rtx op)
5123 {
5124   int regnum;
5125
5126   if (GET_CODE (op) == REG)
5127     regnum = REGNO (op);
5128
5129   else if (GET_CODE (op) == PLUS
5130            && GET_CODE (XEXP (op, 0)) == REG
5131            && GET_CODE (XEXP (op, 1)) == CONST_INT)
5132     regnum = REGNO (XEXP (op, 0));
5133
5134   else
5135     return false;
5136
5137   return (regnum >= FIRST_VIRTUAL_REGISTER
5138           && regnum <= LAST_VIRTUAL_REGISTER);
5139 }
5140
5141 static bool
5142 constant_pool_expr_p (rtx op)
5143 {
5144   rtx base, offset;
5145
5146   split_const (op, &base, &offset);
5147   return (GET_CODE (base) == SYMBOL_REF
5148           && CONSTANT_POOL_ADDRESS_P (base)
5149           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5150 }
5151
5152 static rtx tocrel_base, tocrel_offset;
5153
5154 bool
5155 toc_relative_expr_p (rtx op)
5156 {
5157   if (GET_CODE (op) != CONST)
5158     return false;
5159
5160   split_const (op, &tocrel_base, &tocrel_offset);
5161   return (GET_CODE (tocrel_base) == UNSPEC
5162           && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5163 }
5164
5165 bool
5166 legitimate_constant_pool_address_p (const_rtx x, bool strict)
5167 {
5168   return (TARGET_TOC
5169           && (GET_CODE (x) == PLUS || GET_CODE (x) == LO_SUM)
5170           && GET_CODE (XEXP (x, 0)) == REG
5171           && (REGNO (XEXP (x, 0)) == TOC_REGISTER
5172               || ((TARGET_MINIMAL_TOC
5173                    || TARGET_CMODEL != CMODEL_SMALL)
5174                   && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict)))
5175           && toc_relative_expr_p (XEXP (x, 1)));
5176 }
5177
5178 static bool
5179 legitimate_small_data_p (enum machine_mode mode, rtx x)
5180 {
5181   return (DEFAULT_ABI == ABI_V4
5182           && !flag_pic && !TARGET_TOC
5183           && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5184           && small_data_operand (x, mode));
5185 }
5186
5187 /* SPE offset addressing is limited to 5-bits worth of double words.  */
5188 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5189
5190 bool
5191 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x, int strict)
5192 {
5193   unsigned HOST_WIDE_INT offset, extra;
5194
5195   if (GET_CODE (x) != PLUS)
5196     return false;
5197   if (GET_CODE (XEXP (x, 0)) != REG)
5198     return false;
5199   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5200     return false;
5201   if (!reg_offset_addressing_ok_p (mode))
5202     return virtual_stack_registers_memory_p (x);
5203   if (legitimate_constant_pool_address_p (x, strict))
5204     return true;
5205   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5206     return false;
5207
5208   offset = INTVAL (XEXP (x, 1));
5209   extra = 0;
5210   switch (mode)
5211     {
5212     case V4HImode:
5213     case V2SImode:
5214     case V1DImode:
5215     case V2SFmode:
5216       /* SPE vector modes.  */
5217       return SPE_CONST_OFFSET_OK (offset);
5218
5219     case DFmode:
5220       if (TARGET_E500_DOUBLE)
5221         return SPE_CONST_OFFSET_OK (offset);
5222
5223       /* If we are using VSX scalar loads, restrict ourselves to reg+reg
5224          addressing.  */
5225       if (VECTOR_MEM_VSX_P (DFmode))
5226         return false;
5227
5228     case DDmode:
5229     case DImode:
5230       /* On e500v2, we may have:
5231
5232            (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
5233
5234          Which gets addressed with evldd instructions.  */
5235       if (TARGET_E500_DOUBLE)
5236         return SPE_CONST_OFFSET_OK (offset);
5237
5238       if (mode == DFmode || mode == DDmode || !TARGET_POWERPC64)
5239         extra = 4;
5240       else if (offset & 3)
5241         return false;
5242       break;
5243
5244     case TFmode:
5245       if (TARGET_E500_DOUBLE)
5246         return (SPE_CONST_OFFSET_OK (offset)
5247                 && SPE_CONST_OFFSET_OK (offset + 8));
5248
5249     case TDmode:
5250     case TImode:
5251       if (mode == TFmode || mode == TDmode || !TARGET_POWERPC64)
5252         extra = 12;
5253       else if (offset & 3)
5254         return false;
5255       else
5256         extra = 8;
5257       break;
5258
5259     default:
5260       break;
5261     }
5262
5263   offset += 0x8000;
5264   return (offset < 0x10000) && (offset + extra < 0x10000);
5265 }
5266
5267 bool
5268 legitimate_indexed_address_p (rtx x, int strict)
5269 {
5270   rtx op0, op1;
5271
5272   if (GET_CODE (x) != PLUS)
5273     return false;
5274
5275   op0 = XEXP (x, 0);
5276   op1 = XEXP (x, 1);
5277
5278   /* Recognize the rtl generated by reload which we know will later be
5279      replaced with proper base and index regs.  */
5280   if (!strict
5281       && reload_in_progress
5282       && (REG_P (op0) || GET_CODE (op0) == PLUS)
5283       && REG_P (op1))
5284     return true;
5285
5286   return (REG_P (op0) && REG_P (op1)
5287           && ((INT_REG_OK_FOR_BASE_P (op0, strict)
5288                && INT_REG_OK_FOR_INDEX_P (op1, strict))
5289               || (INT_REG_OK_FOR_BASE_P (op1, strict)
5290                   && INT_REG_OK_FOR_INDEX_P (op0, strict))));
5291 }
5292
5293 bool
5294 avoiding_indexed_address_p (enum machine_mode mode)
5295 {
5296   /* Avoid indexed addressing for modes that have non-indexed
5297      load/store instruction forms.  */
5298   return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
5299 }
5300
5301 inline bool
5302 legitimate_indirect_address_p (rtx x, int strict)
5303 {
5304   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
5305 }
5306
5307 bool
5308 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
5309 {
5310   if (!TARGET_MACHO || !flag_pic
5311       || mode != SImode || GET_CODE (x) != MEM)
5312     return false;
5313   x = XEXP (x, 0);
5314
5315   if (GET_CODE (x) != LO_SUM)
5316     return false;
5317   if (GET_CODE (XEXP (x, 0)) != REG)
5318     return false;
5319   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
5320     return false;
5321   x = XEXP (x, 1);
5322
5323   return CONSTANT_P (x);
5324 }
5325
5326 static bool
5327 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
5328 {
5329   if (GET_CODE (x) != LO_SUM)
5330     return false;
5331   if (GET_CODE (XEXP (x, 0)) != REG)
5332     return false;
5333   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5334     return false;
5335   /* Restrict addressing for DI because of our SUBREG hackery.  */
5336   if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
5337                              || mode == DDmode || mode == TDmode
5338                              || mode == DImode))
5339     return false;
5340   x = XEXP (x, 1);
5341
5342   if (TARGET_ELF || TARGET_MACHO)
5343     {
5344       if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
5345         return false;
5346       if (TARGET_TOC)
5347         return false;
5348       if (GET_MODE_NUNITS (mode) != 1)
5349         return false;
5350       if (GET_MODE_BITSIZE (mode) > 64
5351           || (GET_MODE_BITSIZE (mode) > 32 && !TARGET_POWERPC64
5352               && !(TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
5353                    && (mode == DFmode || mode == DDmode))))
5354         return false;
5355
5356       return CONSTANT_P (x);
5357     }
5358
5359   return false;
5360 }
5361
5362
5363 /* Try machine-dependent ways of modifying an illegitimate address
5364    to be legitimate.  If we find one, return the new, valid address.
5365    This is used from only one place: `memory_address' in explow.c.
5366
5367    OLDX is the address as it was before break_out_memory_refs was
5368    called.  In some cases it is useful to look at this to decide what
5369    needs to be done.
5370
5371    It is always safe for this function to do nothing.  It exists to
5372    recognize opportunities to optimize the output.
5373
5374    On RS/6000, first check for the sum of a register with a constant
5375    integer that is out of range.  If so, generate code to add the
5376    constant with the low-order 16 bits masked to the register and force
5377    this result into another register (this can be done with `cau').
5378    Then generate an address of REG+(CONST&0xffff), allowing for the
5379    possibility of bit 16 being a one.
5380
5381    Then check for the sum of a register and something not constant, try to
5382    load the other things into a register and return the sum.  */
5383
5384 static rtx
5385 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5386                            enum machine_mode mode)
5387 {
5388   unsigned int extra = 0;
5389
5390   if (!reg_offset_addressing_ok_p (mode))
5391     {
5392       if (virtual_stack_registers_memory_p (x))
5393         return x;
5394
5395       /* In theory we should not be seeing addresses of the form reg+0,
5396          but just in case it is generated, optimize it away.  */
5397       if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
5398         return force_reg (Pmode, XEXP (x, 0));
5399
5400       /* Make sure both operands are registers.  */
5401       else if (GET_CODE (x) == PLUS)
5402         return gen_rtx_PLUS (Pmode,
5403                              force_reg (Pmode, XEXP (x, 0)),
5404                              force_reg (Pmode, XEXP (x, 1)));
5405       else
5406         return force_reg (Pmode, x);
5407     }
5408   if (GET_CODE (x) == SYMBOL_REF)
5409     {
5410       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
5411       if (model != 0)
5412         return rs6000_legitimize_tls_address (x, model);
5413     }
5414
5415   switch (mode)
5416     {
5417     case DFmode:
5418     case DDmode:
5419       extra = 4;
5420       break;
5421     case DImode:
5422       if (!TARGET_POWERPC64)
5423         extra = 4;
5424       break;
5425     case TFmode:
5426     case TDmode:
5427       extra = 12;
5428       break;
5429     case TImode:
5430       extra = TARGET_POWERPC64 ? 8 : 12;
5431       break;
5432     default:
5433       break;
5434     }
5435
5436   if (GET_CODE (x) == PLUS
5437       && GET_CODE (XEXP (x, 0)) == REG
5438       && GET_CODE (XEXP (x, 1)) == CONST_INT
5439       && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
5440           >= 0x10000 - extra)
5441       && !((TARGET_POWERPC64
5442             && (mode == DImode || mode == TImode)
5443             && (INTVAL (XEXP (x, 1)) & 3) != 0)
5444            || SPE_VECTOR_MODE (mode)
5445            || (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
5446                                       || mode == DImode || mode == DDmode
5447                                       || mode == TDmode))))
5448     {
5449       HOST_WIDE_INT high_int, low_int;
5450       rtx sum;
5451       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
5452       if (low_int >= 0x8000 - extra)
5453         low_int = 0;
5454       high_int = INTVAL (XEXP (x, 1)) - low_int;
5455       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
5456                                          GEN_INT (high_int)), 0);
5457       return plus_constant (sum, low_int);
5458     }
5459   else if (GET_CODE (x) == PLUS
5460            && GET_CODE (XEXP (x, 0)) == REG
5461            && GET_CODE (XEXP (x, 1)) != CONST_INT
5462            && GET_MODE_NUNITS (mode) == 1
5463            && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5464                || TARGET_POWERPC64
5465                || ((mode != DImode && mode != DFmode && mode != DDmode)
5466                    || (TARGET_E500_DOUBLE && mode != DDmode)))
5467            && (TARGET_POWERPC64 || mode != DImode)
5468            && !avoiding_indexed_address_p (mode)
5469            && mode != TImode
5470            && mode != TFmode
5471            && mode != TDmode)
5472     {
5473       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
5474                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
5475     }
5476   else if (SPE_VECTOR_MODE (mode)
5477            || (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
5478                                       || mode == DDmode || mode == TDmode
5479                                       || mode == DImode)))
5480     {
5481       if (mode == DImode)
5482         return x;
5483       /* We accept [reg + reg] and [reg + OFFSET].  */
5484
5485       if (GET_CODE (x) == PLUS)
5486        {
5487          rtx op1 = XEXP (x, 0);
5488          rtx op2 = XEXP (x, 1);
5489          rtx y;
5490
5491          op1 = force_reg (Pmode, op1);
5492
5493          if (GET_CODE (op2) != REG
5494              && (GET_CODE (op2) != CONST_INT
5495                  || !SPE_CONST_OFFSET_OK (INTVAL (op2))
5496                  || (GET_MODE_SIZE (mode) > 8
5497                      && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
5498            op2 = force_reg (Pmode, op2);
5499
5500          /* We can't always do [reg + reg] for these, because [reg +
5501             reg + offset] is not a legitimate addressing mode.  */
5502          y = gen_rtx_PLUS (Pmode, op1, op2);
5503
5504          if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
5505            return force_reg (Pmode, y);
5506          else
5507            return y;
5508        }
5509
5510       return force_reg (Pmode, x);
5511     }
5512   else if (TARGET_ELF
5513            && TARGET_32BIT
5514            && TARGET_NO_TOC
5515            && ! flag_pic
5516            && GET_CODE (x) != CONST_INT
5517            && GET_CODE (x) != CONST_DOUBLE
5518            && CONSTANT_P (x)
5519            && GET_MODE_NUNITS (mode) == 1
5520            && (GET_MODE_BITSIZE (mode) <= 32
5521                || ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5522                    && (mode == DFmode || mode == DDmode))))
5523     {
5524       rtx reg = gen_reg_rtx (Pmode);
5525       emit_insn (gen_elf_high (reg, x));
5526       return gen_rtx_LO_SUM (Pmode, reg, x);
5527     }
5528   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
5529            && ! flag_pic
5530 #if TARGET_MACHO
5531            && ! MACHO_DYNAMIC_NO_PIC_P
5532 #endif
5533            && GET_CODE (x) != CONST_INT
5534            && GET_CODE (x) != CONST_DOUBLE
5535            && CONSTANT_P (x)
5536            && GET_MODE_NUNITS (mode) == 1
5537            && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5538                || (mode != DFmode && mode != DDmode))
5539            && mode != DImode
5540            && mode != TImode)
5541     {
5542       rtx reg = gen_reg_rtx (Pmode);
5543       emit_insn (gen_macho_high (reg, x));
5544       return gen_rtx_LO_SUM (Pmode, reg, x);
5545     }
5546   else if (TARGET_TOC
5547            && GET_CODE (x) == SYMBOL_REF
5548            && constant_pool_expr_p (x)
5549            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
5550     {
5551       rtx reg = TARGET_CMODEL != CMODEL_SMALL ? gen_reg_rtx (Pmode) : NULL_RTX;
5552       return create_TOC_reference (x, reg);
5553     }
5554   else
5555     return x;
5556 }
5557
5558 /* Debug version of rs6000_legitimize_address.  */
5559 static rtx
5560 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
5561 {
5562   rtx ret;
5563   rtx insns;
5564
5565   start_sequence ();
5566   ret = rs6000_legitimize_address (x, oldx, mode);
5567   insns = get_insns ();
5568   end_sequence ();
5569
5570   if (ret != x)
5571     {
5572       fprintf (stderr,
5573                "\nrs6000_legitimize_address: mode %s, old code %s, "
5574                "new code %s, modified\n",
5575                GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
5576                GET_RTX_NAME (GET_CODE (ret)));
5577
5578       fprintf (stderr, "Original address:\n");
5579       debug_rtx (x);
5580
5581       fprintf (stderr, "oldx:\n");
5582       debug_rtx (oldx);
5583
5584       fprintf (stderr, "New address:\n");
5585       debug_rtx (ret);
5586
5587       if (insns)
5588         {
5589           fprintf (stderr, "Insns added:\n");
5590           debug_rtx_list (insns, 20);
5591         }
5592     }
5593   else
5594     {
5595       fprintf (stderr,
5596                "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
5597                GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
5598
5599       debug_rtx (x);
5600     }
5601
5602   if (insns)
5603     emit_insn (insns);
5604
5605   return ret;
5606 }
5607
5608 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5609    We need to emit DTP-relative relocations.  */
5610
5611 static void
5612 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
5613 {
5614   switch (size)
5615     {
5616     case 4:
5617       fputs ("\t.long\t", file);
5618       break;
5619     case 8:
5620       fputs (DOUBLE_INT_ASM_OP, file);
5621       break;
5622     default:
5623       gcc_unreachable ();
5624     }
5625   output_addr_const (file, x);
5626   fputs ("@dtprel+0x8000", file);
5627 }
5628
5629 /* In the name of slightly smaller debug output, and to cater to
5630    general assembler lossage, recognize various UNSPEC sequences
5631    and turn them back into a direct symbol reference.  */
5632
5633 static rtx
5634 rs6000_delegitimize_address (rtx orig_x)
5635 {
5636   rtx x, y;
5637
5638   orig_x = delegitimize_mem_from_attrs (orig_x);
5639   x = orig_x;
5640   if (MEM_P (x))
5641     x = XEXP (x, 0);
5642
5643   if ((GET_CODE (x) == PLUS
5644        || GET_CODE (x) == LO_SUM)
5645       && GET_CODE (XEXP (x, 0)) == REG
5646       && (REGNO (XEXP (x, 0)) == TOC_REGISTER
5647           || TARGET_MINIMAL_TOC
5648           || TARGET_CMODEL != CMODEL_SMALL)
5649       && GET_CODE (XEXP (x, 1)) == CONST)
5650     {
5651       y = XEXP (XEXP (x, 1), 0);
5652       if (GET_CODE (y) == UNSPEC
5653           && XINT (y, 1) == UNSPEC_TOCREL)
5654         {
5655           y = XVECEXP (y, 0, 0);
5656           if (!MEM_P (orig_x))
5657             return y;
5658           else
5659             return replace_equiv_address_nv (orig_x, y);
5660         }
5661     }
5662
5663   if (TARGET_MACHO
5664       && GET_CODE (orig_x) == LO_SUM
5665       && GET_CODE (XEXP (x, 1)) == CONST)
5666     {
5667       y = XEXP (XEXP (x, 1), 0);
5668       if (GET_CODE (y) == UNSPEC
5669           && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
5670         return XVECEXP (y, 0, 0);
5671     }
5672
5673   return orig_x;
5674 }
5675
5676 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
5677
5678 static GTY(()) rtx rs6000_tls_symbol;
5679 static rtx
5680 rs6000_tls_get_addr (void)
5681 {
5682   if (!rs6000_tls_symbol)
5683     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
5684
5685   return rs6000_tls_symbol;
5686 }
5687
5688 /* Construct the SYMBOL_REF for TLS GOT references.  */
5689
5690 static GTY(()) rtx rs6000_got_symbol;
5691 static rtx
5692 rs6000_got_sym (void)
5693 {
5694   if (!rs6000_got_symbol)
5695     {
5696       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
5697       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
5698       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
5699     }
5700
5701   return rs6000_got_symbol;
5702 }
5703
5704 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
5705    this (thread-local) address.  */
5706
5707 static rtx
5708 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
5709 {
5710   rtx dest, insn;
5711
5712   dest = gen_reg_rtx (Pmode);
5713   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
5714     {
5715       rtx tlsreg;
5716
5717       if (TARGET_64BIT)
5718         {
5719           tlsreg = gen_rtx_REG (Pmode, 13);
5720           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
5721         }
5722       else
5723         {
5724           tlsreg = gen_rtx_REG (Pmode, 2);
5725           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
5726         }
5727       emit_insn (insn);
5728     }
5729   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
5730     {
5731       rtx tlsreg, tmp;
5732
5733       tmp = gen_reg_rtx (Pmode);
5734       if (TARGET_64BIT)
5735         {
5736           tlsreg = gen_rtx_REG (Pmode, 13);
5737           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
5738         }
5739       else
5740         {
5741           tlsreg = gen_rtx_REG (Pmode, 2);
5742           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
5743         }
5744       emit_insn (insn);
5745       if (TARGET_64BIT)
5746         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
5747       else
5748         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
5749       emit_insn (insn);
5750     }
5751   else
5752     {
5753       rtx r3, got, tga, tmp1, tmp2, call_insn;
5754
5755       /* We currently use relocations like @got@tlsgd for tls, which
5756          means the linker will handle allocation of tls entries, placing
5757          them in the .got section.  So use a pointer to the .got section,
5758          not one to secondary TOC sections used by 64-bit -mminimal-toc,
5759          or to secondary GOT sections used by 32-bit -fPIC.  */
5760       if (TARGET_64BIT)
5761         got = gen_rtx_REG (Pmode, 2);
5762       else
5763         {
5764           if (flag_pic == 1)
5765             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
5766           else
5767             {
5768               rtx gsym = rs6000_got_sym ();
5769               got = gen_reg_rtx (Pmode);
5770               if (flag_pic == 0)
5771                 rs6000_emit_move (got, gsym, Pmode);
5772               else
5773                 {
5774                   rtx mem, lab, last;
5775
5776                   tmp1 = gen_reg_rtx (Pmode);
5777                   tmp2 = gen_reg_rtx (Pmode);
5778                   mem = gen_const_mem (Pmode, tmp1);
5779                   lab = gen_label_rtx ();
5780                   emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
5781                   emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
5782                   emit_move_insn (tmp2, mem);
5783                   last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
5784                   set_unique_reg_note (last, REG_EQUAL, gsym);
5785                 }
5786             }
5787         }
5788
5789       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
5790         {
5791           r3 = gen_rtx_REG (Pmode, 3);
5792           tga = rs6000_tls_get_addr ();
5793           emit_library_call_value (tga, dest, LCT_CONST, Pmode, 1, r3, Pmode);
5794
5795           if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
5796             insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
5797           else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
5798             insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
5799           else if (DEFAULT_ABI == ABI_V4)
5800             insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
5801           else
5802             gcc_unreachable ();
5803           call_insn = last_call_insn ();
5804           PATTERN (call_insn) = insn;
5805           if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
5806             use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
5807                      pic_offset_table_rtx);
5808         }
5809       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
5810         {
5811           r3 = gen_rtx_REG (Pmode, 3);
5812           tga = rs6000_tls_get_addr ();
5813           tmp1 = gen_reg_rtx (Pmode);
5814           emit_library_call_value (tga, tmp1, LCT_CONST, Pmode, 1, r3, Pmode);
5815
5816           if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
5817             insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
5818           else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
5819             insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
5820           else if (DEFAULT_ABI == ABI_V4)
5821             insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
5822           else
5823             gcc_unreachable ();
5824           call_insn = last_call_insn ();
5825           PATTERN (call_insn) = insn;
5826           if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
5827             use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
5828                      pic_offset_table_rtx);
5829
5830           if (rs6000_tls_size == 16)
5831             {
5832               if (TARGET_64BIT)
5833                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
5834               else
5835                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
5836             }
5837           else if (rs6000_tls_size == 32)
5838             {
5839               tmp2 = gen_reg_rtx (Pmode);
5840               if (TARGET_64BIT)
5841                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
5842               else
5843                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
5844               emit_insn (insn);
5845               if (TARGET_64BIT)
5846                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
5847               else
5848                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
5849             }
5850           else
5851             {
5852               tmp2 = gen_reg_rtx (Pmode);
5853               if (TARGET_64BIT)
5854                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
5855               else
5856                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
5857               emit_insn (insn);
5858               insn = gen_rtx_SET (Pmode, dest,
5859                                   gen_rtx_PLUS (Pmode, tmp2, tmp1));
5860             }
5861           emit_insn (insn);
5862         }
5863       else
5864         {
5865           /* IE, or 64-bit offset LE.  */
5866           tmp2 = gen_reg_rtx (Pmode);
5867           if (TARGET_64BIT)
5868             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
5869           else
5870             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
5871           emit_insn (insn);
5872           if (TARGET_64BIT)
5873             insn = gen_tls_tls_64 (dest, tmp2, addr);
5874           else
5875             insn = gen_tls_tls_32 (dest, tmp2, addr);
5876           emit_insn (insn);
5877         }
5878     }
5879
5880   return dest;
5881 }
5882
5883 /* Return 1 if X contains a thread-local symbol.  */
5884
5885 bool
5886 rs6000_tls_referenced_p (rtx x)
5887 {
5888   if (! TARGET_HAVE_TLS)
5889     return false;
5890
5891   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
5892 }
5893
5894 /* Return 1 if *X is a thread-local symbol.  This is the same as
5895    rs6000_tls_symbol_ref except for the type of the unused argument.  */
5896
5897 static int
5898 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
5899 {
5900   return RS6000_SYMBOL_REF_TLS_P (*x);
5901 }
5902
5903 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
5904    replace the input X, or the original X if no replacement is called for.
5905    The output parameter *WIN is 1 if the calling macro should goto WIN,
5906    0 if it should not.
5907
5908    For RS/6000, we wish to handle large displacements off a base
5909    register by splitting the addend across an addiu/addis and the mem insn.
5910    This cuts number of extra insns needed from 3 to 1.
5911
5912    On Darwin, we use this to generate code for floating point constants.
5913    A movsf_low is generated so we wind up with 2 instructions rather than 3.
5914    The Darwin code is inside #if TARGET_MACHO because only then are the
5915    machopic_* functions defined.  */
5916 static rtx
5917 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
5918                                   int opnum, int type,
5919                                   int ind_levels ATTRIBUTE_UNUSED, int *win)
5920 {
5921   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
5922
5923   /* We must recognize output that we have already generated ourselves.  */
5924   if (GET_CODE (x) == PLUS
5925       && GET_CODE (XEXP (x, 0)) == PLUS
5926       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
5927       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5928       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5929     {
5930       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
5931                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
5932                    opnum, (enum reload_type)type);
5933       *win = 1;
5934       return x;
5935     }
5936
5937 #if TARGET_MACHO
5938   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
5939       && GET_CODE (x) == LO_SUM
5940       && GET_CODE (XEXP (x, 0)) == PLUS
5941       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
5942       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
5943       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
5944       && machopic_operand_p (XEXP (x, 1)))
5945     {
5946       /* Result of previous invocation of this function on Darwin
5947          floating point constant.  */
5948       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
5949                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
5950                    opnum, (enum reload_type)type);
5951       *win = 1;
5952       return x;
5953     }
5954 #endif
5955
5956   if (TARGET_CMODEL != CMODEL_SMALL
5957       && GET_CODE (x) == LO_SUM
5958       && GET_CODE (XEXP (x, 0)) == PLUS
5959       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
5960       && REGNO (XEXP (XEXP (x, 0), 0)) == TOC_REGISTER
5961       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
5962       && GET_CODE (XEXP (x, 1)) == CONST
5963       && GET_CODE (XEXP (XEXP (x, 1), 0)) == UNSPEC
5964       && XINT (XEXP (XEXP (x, 1), 0), 1) == UNSPEC_TOCREL
5965       && rtx_equal_p (XEXP (XEXP (XEXP (x, 0), 1), 0), XEXP (x, 1)))
5966     {
5967       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
5968                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
5969                    opnum, (enum reload_type) type);
5970       *win = 1;
5971       return x;
5972     }
5973
5974   /* Force ld/std non-word aligned offset into base register by wrapping
5975      in offset 0.  */
5976   if (GET_CODE (x) == PLUS
5977       && GET_CODE (XEXP (x, 0)) == REG
5978       && REGNO (XEXP (x, 0)) < 32
5979       && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
5980       && GET_CODE (XEXP (x, 1)) == CONST_INT
5981       && reg_offset_p
5982       && (INTVAL (XEXP (x, 1)) & 3) != 0
5983       && VECTOR_MEM_NONE_P (mode)
5984       && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
5985       && TARGET_POWERPC64)
5986     {
5987       x = gen_rtx_PLUS (GET_MODE (x), x, GEN_INT (0));
5988       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
5989                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
5990                    opnum, (enum reload_type) type);
5991       *win = 1;
5992       return x;
5993     }
5994
5995   if (GET_CODE (x) == PLUS
5996       && GET_CODE (XEXP (x, 0)) == REG
5997       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
5998       && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
5999       && GET_CODE (XEXP (x, 1)) == CONST_INT
6000       && reg_offset_p
6001       && !SPE_VECTOR_MODE (mode)
6002       && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
6003                                   || mode == DDmode || mode == TDmode
6004                                   || mode == DImode))
6005       && VECTOR_MEM_NONE_P (mode))
6006     {
6007       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6008       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6009       HOST_WIDE_INT high
6010         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6011
6012       /* Check for 32-bit overflow.  */
6013       if (high + low != val)
6014         {
6015           *win = 0;
6016           return x;
6017         }
6018
6019       /* Reload the high part into a base reg; leave the low part
6020          in the mem directly.  */
6021
6022       x = gen_rtx_PLUS (GET_MODE (x),
6023                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6024                                       GEN_INT (high)),
6025                         GEN_INT (low));
6026
6027       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6028                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6029                    opnum, (enum reload_type)type);
6030       *win = 1;
6031       return x;
6032     }
6033
6034   if (GET_CODE (x) == SYMBOL_REF
6035       && reg_offset_p
6036       && VECTOR_MEM_NONE_P (mode)
6037       && !SPE_VECTOR_MODE (mode)
6038 #if TARGET_MACHO
6039       && DEFAULT_ABI == ABI_DARWIN
6040       && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6041 #else
6042       && DEFAULT_ABI == ABI_V4
6043       && !flag_pic
6044 #endif
6045       /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6046          The same goes for DImode without 64-bit gprs and DFmode and DDmode
6047          without fprs.  */
6048       && mode != TFmode
6049       && mode != TDmode
6050       && (mode != DImode || TARGET_POWERPC64)
6051       && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
6052           || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
6053     {
6054 #if TARGET_MACHO
6055       if (flag_pic)
6056         {
6057           rtx offset = machopic_gen_offset (x);
6058           x = gen_rtx_LO_SUM (GET_MODE (x),
6059                 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
6060                   gen_rtx_HIGH (Pmode, offset)), offset);
6061         }
6062       else
6063 #endif
6064         x = gen_rtx_LO_SUM (GET_MODE (x),
6065               gen_rtx_HIGH (Pmode, x), x);
6066
6067       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6068                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6069                    opnum, (enum reload_type)type);
6070       *win = 1;
6071       return x;
6072     }
6073
6074   /* Reload an offset address wrapped by an AND that represents the
6075      masking of the lower bits.  Strip the outer AND and let reload
6076      convert the offset address into an indirect address.  For VSX,
6077      force reload to create the address with an AND in a separate
6078      register, because we can't guarantee an altivec register will
6079      be used.  */
6080   if (VECTOR_MEM_ALTIVEC_P (mode)
6081       && GET_CODE (x) == AND
6082       && GET_CODE (XEXP (x, 0)) == PLUS
6083       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6084       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6085       && GET_CODE (XEXP (x, 1)) == CONST_INT
6086       && INTVAL (XEXP (x, 1)) == -16)
6087     {
6088       x = XEXP (x, 0);
6089       *win = 1;
6090       return x;
6091     }
6092
6093   if (TARGET_TOC
6094       && reg_offset_p
6095       && GET_CODE (x) == SYMBOL_REF
6096       && constant_pool_expr_p (x)
6097       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
6098     {
6099       x = create_TOC_reference (x, NULL_RTX);
6100       if (TARGET_CMODEL != CMODEL_SMALL)
6101         push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6102                      BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6103                      opnum, (enum reload_type) type);
6104       *win = 1;
6105       return x;
6106     }
6107   *win = 0;
6108   return x;
6109 }
6110
6111 /* Debug version of rs6000_legitimize_reload_address.  */
6112 static rtx
6113 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
6114                                         int opnum, int type,
6115                                         int ind_levels, int *win)
6116 {
6117   rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
6118                                               ind_levels, win);
6119   fprintf (stderr,
6120            "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
6121            "type = %d, ind_levels = %d, win = %d, original addr:\n",
6122            GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
6123   debug_rtx (x);
6124
6125   if (x == ret)
6126     fprintf (stderr, "Same address returned\n");
6127   else if (!ret)
6128     fprintf (stderr, "NULL returned\n");
6129   else
6130     {
6131       fprintf (stderr, "New address:\n");
6132       debug_rtx (ret);
6133     }
6134
6135   return ret;
6136 }
6137
6138 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
6139    that is a valid memory address for an instruction.
6140    The MODE argument is the machine mode for the MEM expression
6141    that wants to use this address.
6142
6143    On the RS/6000, there are four valid address: a SYMBOL_REF that
6144    refers to a constant pool entry of an address (or the sum of it
6145    plus a constant), a short (16-bit signed) constant plus a register,
6146    the sum of two registers, or a register indirect, possibly with an
6147    auto-increment.  For DFmode, DDmode and DImode with a constant plus
6148    register, we must ensure that both words are addressable or PowerPC64
6149    with offset word aligned.
6150
6151    For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
6152    32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
6153    because adjacent memory cells are accessed by adding word-sized offsets
6154    during assembly output.  */
6155 bool
6156 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
6157 {
6158   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6159
6160   /* If this is an unaligned stvx/ldvx type address, discard the outer AND.  */
6161   if (VECTOR_MEM_ALTIVEC_P (mode)
6162       && GET_CODE (x) == AND
6163       && GET_CODE (XEXP (x, 1)) == CONST_INT
6164       && INTVAL (XEXP (x, 1)) == -16)
6165     x = XEXP (x, 0);
6166
6167   if (RS6000_SYMBOL_REF_TLS_P (x))
6168     return 0;
6169   if (legitimate_indirect_address_p (x, reg_ok_strict))
6170     return 1;
6171   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
6172       && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6173       && !SPE_VECTOR_MODE (mode)
6174       && mode != TFmode
6175       && mode != TDmode
6176       /* Restrict addressing for DI because of our SUBREG hackery.  */
6177       && !(TARGET_E500_DOUBLE
6178            && (mode == DFmode || mode == DDmode || mode == DImode))
6179       && TARGET_UPDATE
6180       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
6181     return 1;
6182   if (virtual_stack_registers_memory_p (x))
6183     return 1;
6184   if (reg_offset_p && legitimate_small_data_p (mode, x))
6185     return 1;
6186   if (reg_offset_p && legitimate_constant_pool_address_p (x, reg_ok_strict))
6187     return 1;
6188   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
6189   if (! reg_ok_strict
6190       && reg_offset_p
6191       && GET_CODE (x) == PLUS
6192       && GET_CODE (XEXP (x, 0)) == REG
6193       && (XEXP (x, 0) == virtual_stack_vars_rtx
6194           || XEXP (x, 0) == arg_pointer_rtx)
6195       && GET_CODE (XEXP (x, 1)) == CONST_INT)
6196     return 1;
6197   if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict))
6198     return 1;
6199   if (mode != TImode
6200       && mode != TFmode
6201       && mode != TDmode
6202       && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6203           || TARGET_POWERPC64
6204           || (mode != DFmode && mode != DDmode)
6205           || (TARGET_E500_DOUBLE && mode != DDmode))
6206       && (TARGET_POWERPC64 || mode != DImode)
6207       && !avoiding_indexed_address_p (mode)
6208       && legitimate_indexed_address_p (x, reg_ok_strict))
6209     return 1;
6210   if (GET_CODE (x) == PRE_MODIFY
6211       && mode != TImode
6212       && mode != TFmode
6213       && mode != TDmode
6214       && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6215           || TARGET_POWERPC64
6216           || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
6217       && (TARGET_POWERPC64 || mode != DImode)
6218       && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6219       && !SPE_VECTOR_MODE (mode)
6220       /* Restrict addressing for DI because of our SUBREG hackery.  */
6221       && !(TARGET_E500_DOUBLE
6222            && (mode == DFmode || mode == DDmode || mode == DImode))
6223       && TARGET_UPDATE
6224       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
6225       && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1), reg_ok_strict)
6226           || (!avoiding_indexed_address_p (mode)
6227               && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
6228       && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6229     return 1;
6230   if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
6231     return 1;
6232   return 0;
6233 }
6234
6235 /* Debug version of rs6000_legitimate_address_p.  */
6236 static bool
6237 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
6238                                    bool reg_ok_strict)
6239 {
6240   bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
6241   fprintf (stderr,
6242            "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
6243            "strict = %d, code = %s\n",
6244            ret ? "true" : "false",
6245            GET_MODE_NAME (mode),
6246            reg_ok_strict,
6247            GET_RTX_NAME (GET_CODE (x)));
6248   debug_rtx (x);
6249
6250   return ret;
6251 }
6252
6253 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P.  */
6254
6255 static bool
6256 rs6000_mode_dependent_address_p (const_rtx addr)
6257 {
6258   return rs6000_mode_dependent_address_ptr (addr);
6259 }
6260
6261 /* Go to LABEL if ADDR (a legitimate address expression)
6262    has an effect that depends on the machine mode it is used for.
6263
6264    On the RS/6000 this is true of all integral offsets (since AltiVec
6265    and VSX modes don't allow them) or is a pre-increment or decrement.
6266
6267    ??? Except that due to conceptual problems in offsettable_address_p
6268    we can't really report the problems of integral offsets.  So leave
6269    this assuming that the adjustable offset must be valid for the
6270    sub-words of a TFmode operand, which is what we had before.  */
6271
6272 static bool
6273 rs6000_mode_dependent_address (const_rtx addr)
6274 {
6275   switch (GET_CODE (addr))
6276     {
6277     case PLUS:
6278       /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
6279          is considered a legitimate address before reload, so there
6280          are no offset restrictions in that case.  Note that this
6281          condition is safe in strict mode because any address involving
6282          virtual_stack_vars_rtx or arg_pointer_rtx would already have
6283          been rejected as illegitimate.  */
6284       if (XEXP (addr, 0) != virtual_stack_vars_rtx
6285           && XEXP (addr, 0) != arg_pointer_rtx
6286           && GET_CODE (XEXP (addr, 1)) == CONST_INT)
6287         {
6288           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
6289           return val + 12 + 0x8000 >= 0x10000;
6290         }
6291       break;
6292
6293     case LO_SUM:
6294       /* Anything in the constant pool is sufficiently aligned that
6295          all bytes have the same high part address.  */
6296       return !legitimate_constant_pool_address_p (addr, false);
6297
6298     /* Auto-increment cases are now treated generically in recog.c.  */
6299     case PRE_MODIFY:
6300       return TARGET_UPDATE;
6301
6302     /* AND is only allowed in Altivec loads.  */
6303     case AND:
6304       return true;
6305
6306     default:
6307       break;
6308     }
6309
6310   return false;
6311 }
6312
6313 /* Debug version of rs6000_mode_dependent_address.  */
6314 static bool
6315 rs6000_debug_mode_dependent_address (const_rtx addr)
6316 {
6317   bool ret = rs6000_mode_dependent_address (addr);
6318
6319   fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
6320            ret ? "true" : "false");
6321   debug_rtx (addr);
6322
6323   return ret;
6324 }
6325
6326 /* Implement FIND_BASE_TERM.  */
6327
6328 rtx
6329 rs6000_find_base_term (rtx op)
6330 {
6331   rtx base, offset;
6332
6333   split_const (op, &base, &offset);
6334   if (GET_CODE (base) == UNSPEC)
6335     switch (XINT (base, 1))
6336       {
6337       case UNSPEC_TOCREL:
6338       case UNSPEC_MACHOPIC_OFFSET:
6339         /* OP represents SYM [+ OFFSET] - ANCHOR.  SYM is the base term
6340            for aliasing purposes.  */
6341         return XVECEXP (base, 0, 0);
6342       }
6343
6344   return op;
6345 }
6346
6347 /* More elaborate version of recog's offsettable_memref_p predicate
6348    that works around the ??? note of rs6000_mode_dependent_address.
6349    In particular it accepts
6350
6351      (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
6352
6353    in 32-bit mode, that the recog predicate rejects.  */
6354
6355 bool
6356 rs6000_offsettable_memref_p (rtx op)
6357 {
6358   if (!MEM_P (op))
6359     return false;
6360
6361   /* First mimic offsettable_memref_p.  */
6362   if (offsettable_address_p (1, GET_MODE (op), XEXP (op, 0)))
6363     return true;
6364
6365   /* offsettable_address_p invokes rs6000_mode_dependent_address, but
6366      the latter predicate knows nothing about the mode of the memory
6367      reference and, therefore, assumes that it is the largest supported
6368      mode (TFmode).  As a consequence, legitimate offsettable memory
6369      references are rejected.  rs6000_legitimate_offset_address_p contains
6370      the correct logic for the PLUS case of rs6000_mode_dependent_address.  */
6371   return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0), 1);
6372 }
6373
6374 /* Change register usage conditional on target flags.  */
6375 void
6376 rs6000_conditional_register_usage (void)
6377 {
6378   int i;
6379
6380   /* Set MQ register fixed (already call_used) if not POWER
6381      architecture (RIOS1, RIOS2, RSC, and PPC601) so that it will not
6382      be allocated.  */
6383   if (! TARGET_POWER)
6384     fixed_regs[64] = 1;
6385
6386   /* 64-bit AIX and Linux reserve GPR13 for thread-private data.  */
6387   if (TARGET_64BIT)
6388     fixed_regs[13] = call_used_regs[13]
6389       = call_really_used_regs[13] = 1;
6390
6391   /* Conditionally disable FPRs.  */
6392   if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
6393     for (i = 32; i < 64; i++)
6394       fixed_regs[i] = call_used_regs[i]
6395         = call_really_used_regs[i] = 1;
6396
6397   /* The TOC register is not killed across calls in a way that is
6398      visible to the compiler.  */
6399   if (DEFAULT_ABI == ABI_AIX)
6400     call_really_used_regs[2] = 0;
6401
6402   if (DEFAULT_ABI == ABI_V4
6403       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6404       && flag_pic == 2)
6405     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6406
6407   if (DEFAULT_ABI == ABI_V4
6408       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6409       && flag_pic == 1)
6410     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6411       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6412       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6413
6414   if (DEFAULT_ABI == ABI_DARWIN
6415       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
6416       fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6417       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6418       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6419
6420   if (TARGET_TOC && TARGET_MINIMAL_TOC)
6421     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6422       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6423
6424   if (TARGET_SPE)
6425     {
6426       global_regs[SPEFSCR_REGNO] = 1;
6427       /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
6428          registers in prologues and epilogues.  We no longer use r14
6429          for FIXED_SCRATCH, but we're keeping r14 out of the allocation
6430          pool for link-compatibility with older versions of GCC.  Once
6431          "old" code has died out, we can return r14 to the allocation
6432          pool.  */
6433       fixed_regs[14]
6434         = call_used_regs[14]
6435         = call_really_used_regs[14] = 1;
6436     }
6437
6438   if (!TARGET_ALTIVEC && !TARGET_VSX)
6439     {
6440       for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6441         fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6442       call_really_used_regs[VRSAVE_REGNO] = 1;
6443     }
6444
6445   if (TARGET_ALTIVEC || TARGET_VSX)
6446     global_regs[VSCR_REGNO] = 1;
6447
6448   if (TARGET_ALTIVEC_ABI)
6449     {
6450       for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
6451         call_used_regs[i] = call_really_used_regs[i] = 1;
6452
6453       /* AIX reserves VR20:31 in non-extended ABI mode.  */
6454       if (TARGET_XCOFF)
6455         for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
6456           fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6457     }
6458 }
6459 \f
6460 /* Try to output insns to set TARGET equal to the constant C if it can
6461    be done in less than N insns.  Do all computations in MODE.
6462    Returns the place where the output has been placed if it can be
6463    done and the insns have been emitted.  If it would take more than N
6464    insns, zero is returned and no insns and emitted.  */
6465
6466 rtx
6467 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
6468                        rtx source, int n ATTRIBUTE_UNUSED)
6469 {
6470   rtx result, insn, set;
6471   HOST_WIDE_INT c0, c1;
6472
6473   switch (mode)
6474     {
6475       case  QImode:
6476     case HImode:
6477       if (dest == NULL)
6478         dest = gen_reg_rtx (mode);
6479       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
6480       return dest;
6481
6482     case SImode:
6483       result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
6484
6485       emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
6486                               GEN_INT (INTVAL (source)
6487                                        & (~ (HOST_WIDE_INT) 0xffff))));
6488       emit_insn (gen_rtx_SET (VOIDmode, dest,
6489                               gen_rtx_IOR (SImode, copy_rtx (result),
6490                                            GEN_INT (INTVAL (source) & 0xffff))));
6491       result = dest;
6492       break;
6493
6494     case DImode:
6495       switch (GET_CODE (source))
6496         {
6497         case CONST_INT:
6498           c0 = INTVAL (source);
6499           c1 = -(c0 < 0);
6500           break;
6501
6502         case CONST_DOUBLE:
6503 #if HOST_BITS_PER_WIDE_INT >= 64
6504           c0 = CONST_DOUBLE_LOW (source);
6505           c1 = -(c0 < 0);
6506 #else
6507           c0 = CONST_DOUBLE_LOW (source);
6508           c1 = CONST_DOUBLE_HIGH (source);
6509 #endif
6510           break;
6511
6512         default:
6513           gcc_unreachable ();
6514         }
6515
6516       result = rs6000_emit_set_long_const (dest, c0, c1);
6517       break;
6518
6519     default:
6520       gcc_unreachable ();
6521     }
6522
6523   insn = get_last_insn ();
6524   set = single_set (insn);
6525   if (! CONSTANT_P (SET_SRC (set)))
6526     set_unique_reg_note (insn, REG_EQUAL, source);
6527
6528   return result;
6529 }
6530
6531 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
6532    fall back to a straight forward decomposition.  We do this to avoid
6533    exponential run times encountered when looking for longer sequences
6534    with rs6000_emit_set_const.  */
6535 static rtx
6536 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
6537 {
6538   if (!TARGET_POWERPC64)
6539     {
6540       rtx operand1, operand2;
6541
6542       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
6543                                         DImode);
6544       operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
6545                                         DImode);
6546       emit_move_insn (operand1, GEN_INT (c1));
6547       emit_move_insn (operand2, GEN_INT (c2));
6548     }
6549   else
6550     {
6551       HOST_WIDE_INT ud1, ud2, ud3, ud4;
6552
6553       ud1 = c1 & 0xffff;
6554       ud2 = (c1 & 0xffff0000) >> 16;
6555 #if HOST_BITS_PER_WIDE_INT >= 64
6556       c2 = c1 >> 32;
6557 #endif
6558       ud3 = c2 & 0xffff;
6559       ud4 = (c2 & 0xffff0000) >> 16;
6560
6561       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
6562           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
6563         {
6564           if (ud1 & 0x8000)
6565             emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) -  0x8000)));
6566           else
6567             emit_move_insn (dest, GEN_INT (ud1));
6568         }
6569
6570       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
6571                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
6572         {
6573           if (ud2 & 0x8000)
6574             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6575                                            - 0x80000000));
6576           else
6577             emit_move_insn (dest, GEN_INT (ud2 << 16));
6578           if (ud1 != 0)
6579             emit_move_insn (copy_rtx (dest),
6580                             gen_rtx_IOR (DImode, copy_rtx (dest),
6581                                          GEN_INT (ud1)));
6582         }
6583       else if (ud3 == 0 && ud4 == 0)
6584         {
6585           gcc_assert (ud2 & 0x8000);
6586           emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6587                                          - 0x80000000));
6588           if (ud1 != 0)
6589             emit_move_insn (copy_rtx (dest),
6590                             gen_rtx_IOR (DImode, copy_rtx (dest),
6591                                          GEN_INT (ud1)));
6592           emit_move_insn (copy_rtx (dest),
6593                           gen_rtx_ZERO_EXTEND (DImode,
6594                                                gen_lowpart (SImode,
6595                                                             copy_rtx (dest))));
6596         }
6597       else if ((ud4 == 0xffff && (ud3 & 0x8000))
6598                || (ud4 == 0 && ! (ud3 & 0x8000)))
6599         {
6600           if (ud3 & 0x8000)
6601             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
6602                                            - 0x80000000));
6603           else
6604             emit_move_insn (dest, GEN_INT (ud3 << 16));
6605
6606           if (ud2 != 0)
6607             emit_move_insn (copy_rtx (dest),
6608                             gen_rtx_IOR (DImode, copy_rtx (dest),
6609                                          GEN_INT (ud2)));
6610           emit_move_insn (copy_rtx (dest),
6611                           gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6612                                           GEN_INT (16)));
6613           if (ud1 != 0)
6614             emit_move_insn (copy_rtx (dest),
6615                             gen_rtx_IOR (DImode, copy_rtx (dest),
6616                                          GEN_INT (ud1)));
6617         }
6618       else
6619         {
6620           if (ud4 & 0x8000)
6621             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
6622                                            - 0x80000000));
6623           else
6624             emit_move_insn (dest, GEN_INT (ud4 << 16));
6625
6626           if (ud3 != 0)
6627             emit_move_insn (copy_rtx (dest),
6628                             gen_rtx_IOR (DImode, copy_rtx (dest),
6629                                          GEN_INT (ud3)));
6630
6631           emit_move_insn (copy_rtx (dest),
6632                           gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6633                                           GEN_INT (32)));
6634           if (ud2 != 0)
6635             emit_move_insn (copy_rtx (dest),
6636                             gen_rtx_IOR (DImode, copy_rtx (dest),
6637                                          GEN_INT (ud2 << 16)));
6638           if (ud1 != 0)
6639             emit_move_insn (copy_rtx (dest),
6640                             gen_rtx_IOR (DImode, copy_rtx (dest), GEN_INT (ud1)));
6641         }
6642     }
6643   return dest;
6644 }
6645
6646 /* Helper for the following.  Get rid of [r+r] memory refs
6647    in cases where it won't work (TImode, TFmode, TDmode).  */
6648
6649 static void
6650 rs6000_eliminate_indexed_memrefs (rtx operands[2])
6651 {
6652   if (reload_in_progress)
6653     return;
6654
6655   if (GET_CODE (operands[0]) == MEM
6656       && GET_CODE (XEXP (operands[0], 0)) != REG
6657       && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0), false))
6658     operands[0]
6659       = replace_equiv_address (operands[0],
6660                                copy_addr_to_reg (XEXP (operands[0], 0)));
6661
6662   if (GET_CODE (operands[1]) == MEM
6663       && GET_CODE (XEXP (operands[1], 0)) != REG
6664       && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0), false))
6665     operands[1]
6666       = replace_equiv_address (operands[1],
6667                                copy_addr_to_reg (XEXP (operands[1], 0)));
6668 }
6669
6670 /* Emit a move from SOURCE to DEST in mode MODE.  */
6671 void
6672 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
6673 {
6674   rtx operands[2];
6675   operands[0] = dest;
6676   operands[1] = source;
6677
6678   if (TARGET_DEBUG_ADDR)
6679     {
6680       fprintf (stderr,
6681                "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
6682                "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
6683                GET_MODE_NAME (mode),
6684                reload_in_progress,
6685                reload_completed,
6686                can_create_pseudo_p ());
6687       debug_rtx (dest);
6688       fprintf (stderr, "source:\n");
6689       debug_rtx (source);
6690     }
6691
6692   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
6693   if (GET_CODE (operands[1]) == CONST_DOUBLE
6694       && ! FLOAT_MODE_P (mode)
6695       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6696     {
6697       /* FIXME.  This should never happen.  */
6698       /* Since it seems that it does, do the safe thing and convert
6699          to a CONST_INT.  */
6700       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
6701     }
6702   gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
6703               || FLOAT_MODE_P (mode)
6704               || ((CONST_DOUBLE_HIGH (operands[1]) != 0
6705                    || CONST_DOUBLE_LOW (operands[1]) < 0)
6706                   && (CONST_DOUBLE_HIGH (operands[1]) != -1
6707                       || CONST_DOUBLE_LOW (operands[1]) >= 0)));
6708
6709   /* Check if GCC is setting up a block move that will end up using FP
6710      registers as temporaries.  We must make sure this is acceptable.  */
6711   if (GET_CODE (operands[0]) == MEM
6712       && GET_CODE (operands[1]) == MEM
6713       && mode == DImode
6714       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
6715           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
6716       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
6717                                             ? 32 : MEM_ALIGN (operands[0])))
6718             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
6719                                                ? 32
6720                                                : MEM_ALIGN (operands[1]))))
6721       && ! MEM_VOLATILE_P (operands [0])
6722       && ! MEM_VOLATILE_P (operands [1]))
6723     {
6724       emit_move_insn (adjust_address (operands[0], SImode, 0),
6725                       adjust_address (operands[1], SImode, 0));
6726       emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
6727                       adjust_address (copy_rtx (operands[1]), SImode, 4));
6728       return;
6729     }
6730
6731   if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
6732       && !gpc_reg_operand (operands[1], mode))
6733     operands[1] = force_reg (mode, operands[1]);
6734
6735   if (mode == SFmode && ! TARGET_POWERPC
6736       && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT 
6737       && GET_CODE (operands[0]) == MEM)
6738     {
6739       int regnum;
6740
6741       if (reload_in_progress || reload_completed)
6742         regnum = true_regnum (operands[1]);
6743       else if (GET_CODE (operands[1]) == REG)
6744         regnum = REGNO (operands[1]);
6745       else
6746         regnum = -1;
6747
6748       /* If operands[1] is a register, on POWER it may have
6749          double-precision data in it, so truncate it to single
6750          precision.  */
6751       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
6752         {
6753           rtx newreg;
6754           newreg = (!can_create_pseudo_p () ? copy_rtx (operands[1])
6755                     : gen_reg_rtx (mode));
6756           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
6757           operands[1] = newreg;
6758         }
6759     }
6760
6761   /* Recognize the case where operand[1] is a reference to thread-local
6762      data and load its address to a register.  */
6763   if (rs6000_tls_referenced_p (operands[1]))
6764     {
6765       enum tls_model model;
6766       rtx tmp = operands[1];
6767       rtx addend = NULL;
6768
6769       if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
6770         {
6771           addend = XEXP (XEXP (tmp, 0), 1);
6772           tmp = XEXP (XEXP (tmp, 0), 0);
6773         }
6774
6775       gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
6776       model = SYMBOL_REF_TLS_MODEL (tmp);
6777       gcc_assert (model != 0);
6778
6779       tmp = rs6000_legitimize_tls_address (tmp, model);
6780       if (addend)
6781         {
6782           tmp = gen_rtx_PLUS (mode, tmp, addend);
6783           tmp = force_operand (tmp, operands[0]);
6784         }
6785       operands[1] = tmp;
6786     }
6787
6788   /* Handle the case where reload calls us with an invalid address.  */
6789   if (reload_in_progress && mode == Pmode
6790       && (! general_operand (operands[1], mode)
6791           || ! nonimmediate_operand (operands[0], mode)))
6792     goto emit_set;
6793
6794   /* 128-bit constant floating-point values on Darwin should really be
6795      loaded as two parts.  */
6796   if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
6797       && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
6798     {
6799       /* DImode is used, not DFmode, because simplify_gen_subreg doesn't
6800          know how to get a DFmode SUBREG of a TFmode.  */
6801       enum machine_mode imode = (TARGET_E500_DOUBLE ? DFmode : DImode);
6802       rs6000_emit_move (simplify_gen_subreg (imode, operands[0], mode, 0),
6803                         simplify_gen_subreg (imode, operands[1], mode, 0),
6804                         imode);
6805       rs6000_emit_move (simplify_gen_subreg (imode, operands[0], mode,
6806                                              GET_MODE_SIZE (imode)),
6807                         simplify_gen_subreg (imode, operands[1], mode,
6808                                              GET_MODE_SIZE (imode)),
6809                         imode);
6810       return;
6811     }
6812
6813   if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
6814     cfun->machine->sdmode_stack_slot =
6815       eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
6816
6817   if (reload_in_progress
6818       && mode == SDmode
6819       && MEM_P (operands[0])
6820       && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
6821       && REG_P (operands[1]))
6822     {
6823       if (FP_REGNO_P (REGNO (operands[1])))
6824         {
6825           rtx mem = adjust_address_nv (operands[0], DDmode, 0);
6826           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6827           emit_insn (gen_movsd_store (mem, operands[1]));
6828         }
6829       else if (INT_REGNO_P (REGNO (operands[1])))
6830         {
6831           rtx mem = adjust_address_nv (operands[0], mode, 4);
6832           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6833           emit_insn (gen_movsd_hardfloat (mem, operands[1]));
6834         }
6835       else
6836         gcc_unreachable();
6837       return;
6838     }
6839   if (reload_in_progress
6840       && mode == SDmode
6841       && REG_P (operands[0])
6842       && MEM_P (operands[1])
6843       && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
6844     {
6845       if (FP_REGNO_P (REGNO (operands[0])))
6846         {
6847           rtx mem = adjust_address_nv (operands[1], DDmode, 0);
6848           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6849           emit_insn (gen_movsd_load (operands[0], mem));
6850         }
6851       else if (INT_REGNO_P (REGNO (operands[0])))
6852         {
6853           rtx mem = adjust_address_nv (operands[1], mode, 4);
6854           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6855           emit_insn (gen_movsd_hardfloat (operands[0], mem));
6856         }
6857       else
6858         gcc_unreachable();
6859       return;
6860     }
6861
6862   /* FIXME:  In the long term, this switch statement should go away
6863      and be replaced by a sequence of tests based on things like
6864      mode == Pmode.  */
6865   switch (mode)
6866     {
6867     case HImode:
6868     case QImode:
6869       if (CONSTANT_P (operands[1])
6870           && GET_CODE (operands[1]) != CONST_INT)
6871         operands[1] = force_const_mem (mode, operands[1]);
6872       break;
6873
6874     case TFmode:
6875     case TDmode:
6876       rs6000_eliminate_indexed_memrefs (operands);
6877       /* fall through */
6878
6879     case DFmode:
6880     case DDmode:
6881     case SFmode:
6882     case SDmode:
6883       if (CONSTANT_P (operands[1])
6884           && ! easy_fp_constant (operands[1], mode))
6885         operands[1] = force_const_mem (mode, operands[1]);
6886       break;
6887
6888     case V16QImode:
6889     case V8HImode:
6890     case V4SFmode:
6891     case V4SImode:
6892     case V4HImode:
6893     case V2SFmode:
6894     case V2SImode:
6895     case V1DImode:
6896     case V2DFmode:
6897     case V2DImode:
6898       if (CONSTANT_P (operands[1])
6899           && !easy_vector_constant (operands[1], mode))
6900         operands[1] = force_const_mem (mode, operands[1]);
6901       break;
6902
6903     case SImode:
6904     case DImode:
6905       /* Use default pattern for address of ELF small data */
6906       if (TARGET_ELF
6907           && mode == Pmode
6908           && DEFAULT_ABI == ABI_V4
6909           && (GET_CODE (operands[1]) == SYMBOL_REF
6910               || GET_CODE (operands[1]) == CONST)
6911           && small_data_operand (operands[1], mode))
6912         {
6913           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
6914           return;
6915         }
6916
6917       if (DEFAULT_ABI == ABI_V4
6918           && mode == Pmode && mode == SImode
6919           && flag_pic == 1 && got_operand (operands[1], mode))
6920         {
6921           emit_insn (gen_movsi_got (operands[0], operands[1]));
6922           return;
6923         }
6924
6925       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
6926           && TARGET_NO_TOC
6927           && ! flag_pic
6928           && mode == Pmode
6929           && CONSTANT_P (operands[1])
6930           && GET_CODE (operands[1]) != HIGH
6931           && GET_CODE (operands[1]) != CONST_INT)
6932         {
6933           rtx target = (!can_create_pseudo_p ()
6934                         ? operands[0]
6935                         : gen_reg_rtx (mode));
6936
6937           /* If this is a function address on -mcall-aixdesc,
6938              convert it to the address of the descriptor.  */
6939           if (DEFAULT_ABI == ABI_AIX
6940               && GET_CODE (operands[1]) == SYMBOL_REF
6941               && XSTR (operands[1], 0)[0] == '.')
6942             {
6943               const char *name = XSTR (operands[1], 0);
6944               rtx new_ref;
6945               while (*name == '.')
6946                 name++;
6947               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
6948               CONSTANT_POOL_ADDRESS_P (new_ref)
6949                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
6950               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
6951               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
6952               SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
6953               operands[1] = new_ref;
6954             }
6955
6956           if (DEFAULT_ABI == ABI_DARWIN)
6957             {
6958 #if TARGET_MACHO
6959               if (MACHO_DYNAMIC_NO_PIC_P)
6960                 {
6961                   /* Take care of any required data indirection.  */
6962                   operands[1] = rs6000_machopic_legitimize_pic_address (
6963                                   operands[1], mode, operands[0]);
6964                   if (operands[0] != operands[1])
6965                     emit_insn (gen_rtx_SET (VOIDmode,
6966                                             operands[0], operands[1]));
6967                   return;
6968                 }
6969 #endif
6970               emit_insn (gen_macho_high (target, operands[1]));
6971               emit_insn (gen_macho_low (operands[0], target, operands[1]));
6972               return;
6973             }
6974
6975           emit_insn (gen_elf_high (target, operands[1]));
6976           emit_insn (gen_elf_low (operands[0], target, operands[1]));
6977           return;
6978         }
6979
6980       /* If this is a SYMBOL_REF that refers to a constant pool entry,
6981          and we have put it in the TOC, we just need to make a TOC-relative
6982          reference to it.  */
6983       if (TARGET_TOC
6984           && GET_CODE (operands[1]) == SYMBOL_REF
6985           && constant_pool_expr_p (operands[1])
6986           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
6987                                               get_pool_mode (operands[1])))
6988         {
6989           rtx reg = NULL_RTX;
6990           if (TARGET_CMODEL != CMODEL_SMALL)
6991             {
6992               if (can_create_pseudo_p ())
6993                 reg = gen_reg_rtx (Pmode);
6994               else
6995                 reg = operands[0];
6996             }
6997           operands[1] = create_TOC_reference (operands[1], reg);
6998         }
6999       else if (mode == Pmode
7000                && CONSTANT_P (operands[1])
7001                && ((GET_CODE (operands[1]) != CONST_INT
7002                     && ! easy_fp_constant (operands[1], mode))
7003                    || (GET_CODE (operands[1]) == CONST_INT
7004                        && (num_insns_constant (operands[1], mode)
7005                            > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7006                    || (GET_CODE (operands[0]) == REG
7007                        && FP_REGNO_P (REGNO (operands[0]))))
7008                && GET_CODE (operands[1]) != HIGH
7009                && ! legitimate_constant_pool_address_p (operands[1], false)
7010                && ! toc_relative_expr_p (operands[1])
7011                && (TARGET_CMODEL == CMODEL_SMALL
7012                    || can_create_pseudo_p ()
7013                    || (REG_P (operands[0])
7014                        && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7015         {
7016
7017 #if TARGET_MACHO
7018           /* Darwin uses a special PIC legitimizer.  */
7019           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7020             {
7021               operands[1] =
7022                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7023                                                         operands[0]);
7024               if (operands[0] != operands[1])
7025                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7026               return;
7027             }
7028 #endif
7029
7030           /* If we are to limit the number of things we put in the TOC and
7031              this is a symbol plus a constant we can add in one insn,
7032              just put the symbol in the TOC and add the constant.  Don't do
7033              this if reload is in progress.  */
7034           if (GET_CODE (operands[1]) == CONST
7035               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7036               && GET_CODE (XEXP (operands[1], 0)) == PLUS
7037               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7038               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7039                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7040               && ! side_effects_p (operands[0]))
7041             {
7042               rtx sym =
7043                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7044               rtx other = XEXP (XEXP (operands[1], 0), 1);
7045
7046               sym = force_reg (mode, sym);
7047               emit_insn (gen_add3_insn (operands[0], sym, other));
7048               return;
7049             }
7050
7051           operands[1] = force_const_mem (mode, operands[1]);
7052
7053           if (TARGET_TOC
7054               && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7055               && constant_pool_expr_p (XEXP (operands[1], 0))
7056               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7057                         get_pool_constant (XEXP (operands[1], 0)),
7058                         get_pool_mode (XEXP (operands[1], 0))))
7059             {
7060               rtx tocref;
7061               rtx reg = NULL_RTX;
7062               if (TARGET_CMODEL != CMODEL_SMALL)
7063                 {
7064                   if (can_create_pseudo_p ())
7065                     reg = gen_reg_rtx (Pmode);
7066                   else
7067                     reg = operands[0];
7068                 }
7069               tocref = create_TOC_reference (XEXP (operands[1], 0), reg);
7070               operands[1] = gen_const_mem (mode, tocref);
7071               set_mem_alias_set (operands[1], get_TOC_alias_set ());
7072             }
7073         }
7074       break;
7075
7076     case TImode:
7077       rs6000_eliminate_indexed_memrefs (operands);
7078
7079       if (TARGET_POWER)
7080         {
7081           emit_insn (gen_rtx_PARALLEL (VOIDmode,
7082                        gen_rtvec (2,
7083                                   gen_rtx_SET (VOIDmode,
7084                                                operands[0], operands[1]),
7085                                   gen_rtx_CLOBBER (VOIDmode,
7086                                                    gen_rtx_SCRATCH (SImode)))));
7087           return;
7088         }
7089       break;
7090
7091     default:
7092       fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7093     }
7094
7095   /* Above, we may have called force_const_mem which may have returned
7096      an invalid address.  If we can, fix this up; otherwise, reload will
7097      have to deal with it.  */
7098   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7099     operands[1] = validize_mem (operands[1]);
7100
7101  emit_set:
7102   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7103 }
7104 \f
7105 /* Nonzero if we can use a floating-point register to pass this arg.  */
7106 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE)         \
7107   (SCALAR_FLOAT_MODE_P (MODE)                   \
7108    && (CUM)->fregno <= FP_ARG_MAX_REG           \
7109    && TARGET_HARD_FLOAT && TARGET_FPRS)
7110
7111 /* Nonzero if we can use an AltiVec register to pass this arg.  */
7112 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED)              \
7113   ((ALTIVEC_VECTOR_MODE (MODE) || VSX_VECTOR_MODE (MODE))       \
7114    && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG                      \
7115    && TARGET_ALTIVEC_ABI                                        \
7116    && (NAMED))
7117
7118 /* Return a nonzero value to say to return the function value in
7119    memory, just as large structures are always returned.  TYPE will be
7120    the data type of the value, and FNTYPE will be the type of the
7121    function doing the returning, or @code{NULL} for libcalls.
7122
7123    The AIX ABI for the RS/6000 specifies that all structures are
7124    returned in memory.  The Darwin ABI does the same.  The SVR4 ABI
7125    specifies that structures <= 8 bytes are returned in r3/r4, but a
7126    draft put them in memory, and GCC used to implement the draft
7127    instead of the final standard.  Therefore, aix_struct_return
7128    controls this instead of DEFAULT_ABI; V.4 targets needing backward
7129    compatibility can change DRAFT_V4_STRUCT_RET to override the
7130    default, and -m switches get the final word.  See
7131    rs6000_override_options for more details.
7132
7133    The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
7134    long double support is enabled.  These values are returned in memory.
7135
7136    int_size_in_bytes returns -1 for variable size objects, which go in
7137    memory always.  The cast to unsigned makes -1 > 8.  */
7138
7139 static bool
7140 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7141 {
7142   /* In the darwin64 abi, try to use registers for larger structs
7143      if possible.  */
7144   if (rs6000_darwin64_abi
7145       && TREE_CODE (type) == RECORD_TYPE
7146       && int_size_in_bytes (type) > 0)
7147     {
7148       CUMULATIVE_ARGS valcum;
7149       rtx valret;
7150
7151       valcum.words = 0;
7152       valcum.fregno = FP_ARG_MIN_REG;
7153       valcum.vregno = ALTIVEC_ARG_MIN_REG;
7154       /* Do a trial code generation as if this were going to be passed
7155          as an argument; if any part goes in memory, we return NULL.  */
7156       valret = rs6000_darwin64_record_arg (&valcum, type, 1, true);
7157       if (valret)
7158         return false;
7159       /* Otherwise fall through to more conventional ABI rules.  */
7160     }
7161
7162   if (AGGREGATE_TYPE_P (type)
7163       && (aix_struct_return
7164           || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
7165     return true;
7166
7167   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
7168      modes only exist for GCC vector types if -maltivec.  */
7169   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
7170       && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
7171     return false;
7172
7173   /* Return synthetic vectors in memory.  */
7174   if (TREE_CODE (type) == VECTOR_TYPE
7175       && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
7176     {
7177       static bool warned_for_return_big_vectors = false;
7178       if (!warned_for_return_big_vectors)
7179         {
7180           warning (0, "GCC vector returned by reference: "
7181                    "non-standard ABI extension with no compatibility guarantee");
7182           warned_for_return_big_vectors = true;
7183         }
7184       return true;
7185     }
7186
7187   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
7188     return true;
7189
7190   return false;
7191 }
7192
7193 /* Initialize a variable CUM of type CUMULATIVE_ARGS
7194    for a call to a function whose data type is FNTYPE.
7195    For a library call, FNTYPE is 0.
7196
7197    For incoming args we set the number of arguments in the prototype large
7198    so we never return a PARALLEL.  */
7199
7200 void
7201 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
7202                       rtx libname ATTRIBUTE_UNUSED, int incoming,
7203                       int libcall, int n_named_args)
7204 {
7205   static CUMULATIVE_ARGS zero_cumulative;
7206
7207   *cum = zero_cumulative;
7208   cum->words = 0;
7209   cum->fregno = FP_ARG_MIN_REG;
7210   cum->vregno = ALTIVEC_ARG_MIN_REG;
7211   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
7212   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
7213                       ? CALL_LIBCALL : CALL_NORMAL);
7214   cum->sysv_gregno = GP_ARG_MIN_REG;
7215   cum->stdarg = fntype
7216     && (TYPE_ARG_TYPES (fntype) != 0
7217         && (TREE_VALUE (tree_last  (TYPE_ARG_TYPES (fntype)))
7218             != void_type_node));
7219
7220   cum->nargs_prototype = 0;
7221   if (incoming || cum->prototype)
7222     cum->nargs_prototype = n_named_args;
7223
7224   /* Check for a longcall attribute.  */
7225   if ((!fntype && rs6000_default_long_calls)
7226       || (fntype
7227           && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
7228           && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
7229     cum->call_cookie |= CALL_LONG;
7230
7231   if (TARGET_DEBUG_ARG)
7232     {
7233       fprintf (stderr, "\ninit_cumulative_args:");
7234       if (fntype)
7235         {
7236           tree ret_type = TREE_TYPE (fntype);
7237           fprintf (stderr, " ret code = %s,",
7238                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
7239         }
7240
7241       if (cum->call_cookie & CALL_LONG)
7242         fprintf (stderr, " longcall,");
7243
7244       fprintf (stderr, " proto = %d, nargs = %d\n",
7245                cum->prototype, cum->nargs_prototype);
7246     }
7247
7248   if (fntype
7249       && !TARGET_ALTIVEC
7250       && TARGET_ALTIVEC_ABI
7251       && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
7252     {
7253       error ("cannot return value in vector register because"
7254              " altivec instructions are disabled, use -maltivec"
7255              " to enable them");
7256     }
7257 }
7258 \f
7259 /* Return true if TYPE must be passed on the stack and not in registers.  */
7260
7261 static bool
7262 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
7263 {
7264   if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
7265     return must_pass_in_stack_var_size (mode, type);
7266   else
7267     return must_pass_in_stack_var_size_or_pad (mode, type);
7268 }
7269
7270 /* If defined, a C expression which determines whether, and in which
7271    direction, to pad out an argument with extra space.  The value
7272    should be of type `enum direction': either `upward' to pad above
7273    the argument, `downward' to pad below, or `none' to inhibit
7274    padding.
7275
7276    For the AIX ABI structs are always stored left shifted in their
7277    argument slot.  */
7278
7279 enum direction
7280 function_arg_padding (enum machine_mode mode, const_tree type)
7281 {
7282 #ifndef AGGREGATE_PADDING_FIXED
7283 #define AGGREGATE_PADDING_FIXED 0
7284 #endif
7285 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
7286 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
7287 #endif
7288
7289   if (!AGGREGATE_PADDING_FIXED)
7290     {
7291       /* GCC used to pass structures of the same size as integer types as
7292          if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
7293          i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
7294          passed padded downward, except that -mstrict-align further
7295          muddied the water in that multi-component structures of 2 and 4
7296          bytes in size were passed padded upward.
7297
7298          The following arranges for best compatibility with previous
7299          versions of gcc, but removes the -mstrict-align dependency.  */
7300       if (BYTES_BIG_ENDIAN)
7301         {
7302           HOST_WIDE_INT size = 0;
7303
7304           if (mode == BLKmode)
7305             {
7306               if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7307                 size = int_size_in_bytes (type);
7308             }
7309           else
7310             size = GET_MODE_SIZE (mode);
7311
7312           if (size == 1 || size == 2 || size == 4)
7313             return downward;
7314         }
7315       return upward;
7316     }
7317
7318   if (AGGREGATES_PAD_UPWARD_ALWAYS)
7319     {
7320       if (type != 0 && AGGREGATE_TYPE_P (type))
7321         return upward;
7322     }
7323
7324   /* Fall back to the default.  */
7325   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
7326 }
7327
7328 /* If defined, a C expression that gives the alignment boundary, in bits,
7329    of an argument with the specified mode and type.  If it is not defined,
7330    PARM_BOUNDARY is used for all arguments.
7331
7332    V.4 wants long longs and doubles to be double word aligned.  Just
7333    testing the mode size is a boneheaded way to do this as it means
7334    that other types such as complex int are also double word aligned.
7335    However, we're stuck with this because changing the ABI might break
7336    existing library interfaces.
7337
7338    Doubleword align SPE vectors.
7339    Quadword align Altivec vectors.
7340    Quadword align large synthetic vector types.   */
7341
7342 int
7343 function_arg_boundary (enum machine_mode mode, tree type)
7344 {
7345   if (DEFAULT_ABI == ABI_V4
7346       && (GET_MODE_SIZE (mode) == 8
7347           || (TARGET_HARD_FLOAT
7348               && TARGET_FPRS
7349               && (mode == TFmode || mode == TDmode))))
7350     return 64;
7351   else if (SPE_VECTOR_MODE (mode)
7352            || (type && TREE_CODE (type) == VECTOR_TYPE
7353                && int_size_in_bytes (type) >= 8
7354                && int_size_in_bytes (type) < 16))
7355     return 64;
7356   else if ((ALTIVEC_VECTOR_MODE (mode) || VSX_VECTOR_MODE (mode))
7357            || (type && TREE_CODE (type) == VECTOR_TYPE
7358                && int_size_in_bytes (type) >= 16))
7359     return 128;
7360   else if (rs6000_darwin64_abi && mode == BLKmode
7361            && type && TYPE_ALIGN (type) > 64)
7362     return 128;
7363   else
7364     return PARM_BOUNDARY;
7365 }
7366
7367 /* For a function parm of MODE and TYPE, return the starting word in
7368    the parameter area.  NWORDS of the parameter area are already used.  */
7369
7370 static unsigned int
7371 rs6000_parm_start (enum machine_mode mode, tree type, unsigned int nwords)
7372 {
7373   unsigned int align;
7374   unsigned int parm_offset;
7375
7376   align = function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
7377   parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
7378   return nwords + (-(parm_offset + nwords) & align);
7379 }
7380
7381 /* Compute the size (in words) of a function argument.  */
7382
7383 static unsigned long
7384 rs6000_arg_size (enum machine_mode mode, tree type)
7385 {
7386   unsigned long size;
7387
7388   if (mode != BLKmode)
7389     size = GET_MODE_SIZE (mode);
7390   else
7391     size = int_size_in_bytes (type);
7392
7393   if (TARGET_32BIT)
7394     return (size + 3) >> 2;
7395   else
7396     return (size + 7) >> 3;
7397 }
7398 \f
7399 /* Use this to flush pending int fields.  */
7400
7401 static void
7402 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
7403                                           HOST_WIDE_INT bitpos)
7404 {
7405   unsigned int startbit, endbit;
7406   int intregs, intoffset;
7407   enum machine_mode mode;
7408
7409   if (cum->intoffset == -1)
7410     return;
7411
7412   intoffset = cum->intoffset;
7413   cum->intoffset = -1;
7414
7415   if (intoffset % BITS_PER_WORD != 0)
7416     {
7417       mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
7418                             MODE_INT, 0);
7419       if (mode == BLKmode)
7420         {
7421           /* We couldn't find an appropriate mode, which happens,
7422              e.g., in packed structs when there are 3 bytes to load.
7423              Back intoffset back to the beginning of the word in this
7424              case.  */
7425           intoffset = intoffset & -BITS_PER_WORD;
7426         }
7427     }
7428
7429   startbit = intoffset & -BITS_PER_WORD;
7430   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
7431   intregs = (endbit - startbit) / BITS_PER_WORD;
7432   cum->words += intregs;
7433 }
7434
7435 /* The darwin64 ABI calls for us to recurse down through structs,
7436    looking for elements passed in registers.  Unfortunately, we have
7437    to track int register count here also because of misalignments
7438    in powerpc alignment mode.  */
7439
7440 static void
7441 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
7442                                             tree type,
7443                                             HOST_WIDE_INT startbitpos)
7444 {
7445   tree f;
7446
7447   for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
7448     if (TREE_CODE (f) == FIELD_DECL)
7449       {
7450         HOST_WIDE_INT bitpos = startbitpos;
7451         tree ftype = TREE_TYPE (f);
7452         enum machine_mode mode;
7453         if (ftype == error_mark_node)
7454           continue;
7455         mode = TYPE_MODE (ftype);
7456
7457         if (DECL_SIZE (f) != 0
7458             && host_integerp (bit_position (f), 1))
7459           bitpos += int_bit_position (f);
7460
7461         /* ??? FIXME: else assume zero offset.  */
7462
7463         if (TREE_CODE (ftype) == RECORD_TYPE)
7464           rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
7465         else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
7466           {
7467             rs6000_darwin64_record_arg_advance_flush (cum, bitpos);
7468             cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7469             cum->words += (GET_MODE_SIZE (mode) + 7) >> 3;
7470           }
7471         else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
7472           {
7473             rs6000_darwin64_record_arg_advance_flush (cum, bitpos);
7474             cum->vregno++;
7475             cum->words += 2;
7476           }
7477         else if (cum->intoffset == -1)
7478           cum->intoffset = bitpos;
7479       }
7480 }
7481
7482 /* Update the data in CUM to advance over an argument
7483    of mode MODE and data type TYPE.
7484    (TYPE is null for libcalls where that information may not be available.)
7485
7486    Note that for args passed by reference, function_arg will be called
7487    with MODE and TYPE set to that of the pointer to the arg, not the arg
7488    itself.  */
7489
7490 void
7491 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7492                       tree type, int named, int depth)
7493 {
7494   int size;
7495
7496   /* Only tick off an argument if we're not recursing.  */
7497   if (depth == 0)
7498     cum->nargs_prototype--;
7499
7500   if (TARGET_ALTIVEC_ABI
7501       && (ALTIVEC_VECTOR_MODE (mode)
7502           || VSX_VECTOR_MODE (mode)
7503           || (type && TREE_CODE (type) == VECTOR_TYPE
7504               && int_size_in_bytes (type) == 16)))
7505     {
7506       bool stack = false;
7507
7508       if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
7509         {
7510           cum->vregno++;
7511           if (!TARGET_ALTIVEC)
7512             error ("cannot pass argument in vector register because"
7513                    " altivec instructions are disabled, use -maltivec"
7514                    " to enable them");
7515
7516           /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
7517              even if it is going to be passed in a vector register.
7518              Darwin does the same for variable-argument functions.  */
7519           if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
7520               || (cum->stdarg && DEFAULT_ABI != ABI_V4))
7521             stack = true;
7522         }
7523       else
7524         stack = true;
7525
7526       if (stack)
7527         {
7528           int align;
7529
7530           /* Vector parameters must be 16-byte aligned.  This places
7531              them at 2 mod 4 in terms of words in 32-bit mode, since
7532              the parameter save area starts at offset 24 from the
7533              stack.  In 64-bit mode, they just have to start on an
7534              even word, since the parameter save area is 16-byte
7535              aligned.  Space for GPRs is reserved even if the argument
7536              will be passed in memory.  */
7537           if (TARGET_32BIT)
7538             align = (2 - cum->words) & 3;
7539           else
7540             align = cum->words & 1;
7541           cum->words += align + rs6000_arg_size (mode, type);
7542
7543           if (TARGET_DEBUG_ARG)
7544             {
7545               fprintf (stderr, "function_adv: words = %2d, align=%d, ",
7546                        cum->words, align);
7547               fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
7548                        cum->nargs_prototype, cum->prototype,
7549                        GET_MODE_NAME (mode));
7550             }
7551         }
7552     }
7553   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
7554            && !cum->stdarg
7555            && cum->sysv_gregno <= GP_ARG_MAX_REG)
7556     cum->sysv_gregno++;
7557
7558   else if (rs6000_darwin64_abi
7559            && mode == BLKmode
7560            && TREE_CODE (type) == RECORD_TYPE
7561            && (size = int_size_in_bytes (type)) > 0)
7562     {
7563       /* Variable sized types have size == -1 and are
7564          treated as if consisting entirely of ints.
7565          Pad to 16 byte boundary if needed.  */
7566       if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
7567           && (cum->words % 2) != 0)
7568         cum->words++;
7569       /* For varargs, we can just go up by the size of the struct. */
7570       if (!named)
7571         cum->words += (size + 7) / 8;
7572       else
7573         {
7574           /* It is tempting to say int register count just goes up by
7575              sizeof(type)/8, but this is wrong in a case such as
7576              { int; double; int; } [powerpc alignment].  We have to
7577              grovel through the fields for these too.  */
7578           cum->intoffset = 0;
7579           rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
7580           rs6000_darwin64_record_arg_advance_flush (cum,
7581                                                     size * BITS_PER_UNIT);
7582         }
7583     }
7584   else if (DEFAULT_ABI == ABI_V4)
7585     {
7586       if (TARGET_HARD_FLOAT && TARGET_FPRS
7587           && ((TARGET_SINGLE_FLOAT && mode == SFmode)
7588               || (TARGET_DOUBLE_FLOAT && mode == DFmode)
7589               || (mode == TFmode && !TARGET_IEEEQUAD)
7590               || mode == SDmode || mode == DDmode || mode == TDmode))
7591         {
7592           /* _Decimal128 must use an even/odd register pair.  This assumes
7593              that the register number is odd when fregno is odd.  */
7594           if (mode == TDmode && (cum->fregno % 2) == 1)
7595             cum->fregno++;
7596
7597           if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
7598               <= FP_ARG_V4_MAX_REG)
7599             cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7600           else
7601             {
7602               cum->fregno = FP_ARG_V4_MAX_REG + 1;
7603               if (mode == DFmode || mode == TFmode
7604                   || mode == DDmode || mode == TDmode)
7605                 cum->words += cum->words & 1;
7606               cum->words += rs6000_arg_size (mode, type);
7607             }
7608         }
7609       else
7610         {
7611           int n_words = rs6000_arg_size (mode, type);
7612           int gregno = cum->sysv_gregno;
7613
7614           /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
7615              (r7,r8) or (r9,r10).  As does any other 2 word item such
7616              as complex int due to a historical mistake.  */
7617           if (n_words == 2)
7618             gregno += (1 - gregno) & 1;
7619
7620           /* Multi-reg args are not split between registers and stack.  */
7621           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
7622             {
7623               /* Long long and SPE vectors are aligned on the stack.
7624                  So are other 2 word items such as complex int due to
7625                  a historical mistake.  */
7626               if (n_words == 2)
7627                 cum->words += cum->words & 1;
7628               cum->words += n_words;
7629             }
7630
7631           /* Note: continuing to accumulate gregno past when we've started
7632              spilling to the stack indicates the fact that we've started
7633              spilling to the stack to expand_builtin_saveregs.  */
7634           cum->sysv_gregno = gregno + n_words;
7635         }
7636
7637       if (TARGET_DEBUG_ARG)
7638         {
7639           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
7640                    cum->words, cum->fregno);
7641           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
7642                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
7643           fprintf (stderr, "mode = %4s, named = %d\n",
7644                    GET_MODE_NAME (mode), named);
7645         }
7646     }
7647   else
7648     {
7649       int n_words = rs6000_arg_size (mode, type);
7650       int start_words = cum->words;
7651       int align_words = rs6000_parm_start (mode, type, start_words);
7652
7653       cum->words = align_words + n_words;
7654
7655       if (SCALAR_FLOAT_MODE_P (mode)
7656           && TARGET_HARD_FLOAT && TARGET_FPRS)
7657         {
7658           /* _Decimal128 must be passed in an even/odd float register pair.
7659              This assumes that the register number is odd when fregno is
7660              odd.  */
7661           if (mode == TDmode && (cum->fregno % 2) == 1)
7662             cum->fregno++;
7663           cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7664         }
7665
7666       if (TARGET_DEBUG_ARG)
7667         {
7668           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
7669                    cum->words, cum->fregno);
7670           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
7671                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
7672           fprintf (stderr, "named = %d, align = %d, depth = %d\n",
7673                    named, align_words - start_words, depth);
7674         }
7675     }
7676 }
7677
7678 static rtx
7679 spe_build_register_parallel (enum machine_mode mode, int gregno)
7680 {
7681   rtx r1, r3, r5, r7;
7682
7683   switch (mode)
7684     {
7685     case DFmode:
7686       r1 = gen_rtx_REG (DImode, gregno);
7687       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
7688       return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
7689
7690     case DCmode:
7691     case TFmode:
7692       r1 = gen_rtx_REG (DImode, gregno);
7693       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
7694       r3 = gen_rtx_REG (DImode, gregno + 2);
7695       r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
7696       return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
7697
7698     case TCmode:
7699       r1 = gen_rtx_REG (DImode, gregno);
7700       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
7701       r3 = gen_rtx_REG (DImode, gregno + 2);
7702       r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
7703       r5 = gen_rtx_REG (DImode, gregno + 4);
7704       r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
7705       r7 = gen_rtx_REG (DImode, gregno + 6);
7706       r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
7707       return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
7708
7709     default:
7710       gcc_unreachable ();
7711     }
7712 }
7713
7714 /* Determine where to put a SIMD argument on the SPE.  */
7715 static rtx
7716 rs6000_spe_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7717                          tree type)
7718 {
7719   int gregno = cum->sysv_gregno;
7720
7721   /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
7722      are passed and returned in a pair of GPRs for ABI compatibility.  */
7723   if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
7724                              || mode == DCmode || mode == TCmode))
7725     {
7726       int n_words = rs6000_arg_size (mode, type);
7727
7728       /* Doubles go in an odd/even register pair (r5/r6, etc).  */
7729       if (mode == DFmode)
7730         gregno += (1 - gregno) & 1;
7731
7732       /* Multi-reg args are not split between registers and stack.  */
7733       if (gregno + n_words - 1 > GP_ARG_MAX_REG)
7734         return NULL_RTX;
7735
7736       return spe_build_register_parallel (mode, gregno);
7737     }
7738   if (cum->stdarg)
7739     {
7740       int n_words = rs6000_arg_size (mode, type);
7741
7742       /* SPE vectors are put in odd registers.  */
7743       if (n_words == 2 && (gregno & 1) == 0)
7744         gregno += 1;
7745
7746       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
7747         {
7748           rtx r1, r2;
7749           enum machine_mode m = SImode;
7750
7751           r1 = gen_rtx_REG (m, gregno);
7752           r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
7753           r2 = gen_rtx_REG (m, gregno + 1);
7754           r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
7755           return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
7756         }
7757       else
7758         return NULL_RTX;
7759     }
7760   else
7761     {
7762       if (gregno <= GP_ARG_MAX_REG)
7763         return gen_rtx_REG (mode, gregno);
7764       else
7765         return NULL_RTX;
7766     }
7767 }
7768
7769 /* A subroutine of rs6000_darwin64_record_arg.  Assign the bits of the
7770    structure between cum->intoffset and bitpos to integer registers.  */
7771
7772 static void
7773 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
7774                                   HOST_WIDE_INT bitpos, rtx rvec[], int *k)
7775 {
7776   enum machine_mode mode;
7777   unsigned int regno;
7778   unsigned int startbit, endbit;
7779   int this_regno, intregs, intoffset;
7780   rtx reg;
7781
7782   if (cum->intoffset == -1)
7783     return;
7784
7785   intoffset = cum->intoffset;
7786   cum->intoffset = -1;
7787
7788   /* If this is the trailing part of a word, try to only load that
7789      much into the register.  Otherwise load the whole register.  Note
7790      that in the latter case we may pick up unwanted bits.  It's not a
7791      problem at the moment but may wish to revisit.  */
7792
7793   if (intoffset % BITS_PER_WORD != 0)
7794     {
7795       mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
7796                           MODE_INT, 0);
7797       if (mode == BLKmode)
7798         {
7799           /* We couldn't find an appropriate mode, which happens,
7800              e.g., in packed structs when there are 3 bytes to load.
7801              Back intoffset back to the beginning of the word in this
7802              case.  */
7803          intoffset = intoffset & -BITS_PER_WORD;
7804          mode = word_mode;
7805         }
7806     }
7807   else
7808     mode = word_mode;
7809
7810   startbit = intoffset & -BITS_PER_WORD;
7811   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
7812   intregs = (endbit - startbit) / BITS_PER_WORD;
7813   this_regno = cum->words + intoffset / BITS_PER_WORD;
7814
7815   if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
7816     cum->use_stack = 1;
7817
7818   intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
7819   if (intregs <= 0)
7820     return;
7821
7822   intoffset /= BITS_PER_UNIT;
7823   do
7824     {
7825       regno = GP_ARG_MIN_REG + this_regno;
7826       reg = gen_rtx_REG (mode, regno);
7827       rvec[(*k)++] =
7828         gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
7829
7830       this_regno += 1;
7831       intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
7832       mode = word_mode;
7833       intregs -= 1;
7834     }
7835   while (intregs > 0);
7836 }
7837
7838 /* Recursive workhorse for the following.  */
7839
7840 static void
7841 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
7842                                     HOST_WIDE_INT startbitpos, rtx rvec[],
7843                                     int *k)
7844 {
7845   tree f;
7846
7847   for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
7848     if (TREE_CODE (f) == FIELD_DECL)
7849       {
7850         HOST_WIDE_INT bitpos = startbitpos;
7851         tree ftype = TREE_TYPE (f);
7852         enum machine_mode mode;
7853         if (ftype == error_mark_node)
7854           continue;
7855         mode = TYPE_MODE (ftype);
7856
7857         if (DECL_SIZE (f) != 0
7858             && host_integerp (bit_position (f), 1))
7859           bitpos += int_bit_position (f);
7860
7861         /* ??? FIXME: else assume zero offset.  */
7862
7863         if (TREE_CODE (ftype) == RECORD_TYPE)
7864           rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
7865         else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
7866           {
7867 #if 0
7868             switch (mode)
7869               {
7870               case SCmode: mode = SFmode; break;
7871               case DCmode: mode = DFmode; break;
7872               case TCmode: mode = TFmode; break;
7873               default: break;
7874               }
7875 #endif
7876             rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
7877             rvec[(*k)++]
7878               = gen_rtx_EXPR_LIST (VOIDmode,
7879                                    gen_rtx_REG (mode, cum->fregno++),
7880                                    GEN_INT (bitpos / BITS_PER_UNIT));
7881             if (mode == TFmode || mode == TDmode)
7882               cum->fregno++;
7883           }
7884         else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
7885           {
7886             rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
7887             rvec[(*k)++]
7888               = gen_rtx_EXPR_LIST (VOIDmode,
7889                                    gen_rtx_REG (mode, cum->vregno++),
7890                                    GEN_INT (bitpos / BITS_PER_UNIT));
7891           }
7892         else if (cum->intoffset == -1)
7893           cum->intoffset = bitpos;
7894       }
7895 }
7896
7897 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
7898    the register(s) to be used for each field and subfield of a struct
7899    being passed by value, along with the offset of where the
7900    register's value may be found in the block.  FP fields go in FP
7901    register, vector fields go in vector registers, and everything
7902    else goes in int registers, packed as in memory.
7903
7904    This code is also used for function return values.  RETVAL indicates
7905    whether this is the case.
7906
7907    Much of this is taken from the SPARC V9 port, which has a similar
7908    calling convention.  */
7909
7910 static rtx
7911 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
7912                             int named, bool retval)
7913 {
7914   rtx rvec[FIRST_PSEUDO_REGISTER];
7915   int k = 1, kbase = 1;
7916   HOST_WIDE_INT typesize = int_size_in_bytes (type);
7917   /* This is a copy; modifications are not visible to our caller.  */
7918   CUMULATIVE_ARGS copy_cum = *orig_cum;
7919   CUMULATIVE_ARGS *cum = &copy_cum;
7920
7921   /* Pad to 16 byte boundary if needed.  */
7922   if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
7923       && (cum->words % 2) != 0)
7924     cum->words++;
7925
7926   cum->intoffset = 0;
7927   cum->use_stack = 0;
7928   cum->named = named;
7929
7930   /* Put entries into rvec[] for individual FP and vector fields, and
7931      for the chunks of memory that go in int regs.  Note we start at
7932      element 1; 0 is reserved for an indication of using memory, and
7933      may or may not be filled in below. */
7934   rs6000_darwin64_record_arg_recurse (cum, type, 0, rvec, &k);
7935   rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
7936
7937   /* If any part of the struct went on the stack put all of it there.
7938      This hack is because the generic code for
7939      FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
7940      parts of the struct are not at the beginning.  */
7941   if (cum->use_stack)
7942     {
7943       if (retval)
7944         return NULL_RTX;    /* doesn't go in registers at all */
7945       kbase = 0;
7946       rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
7947     }
7948   if (k > 1 || cum->use_stack)
7949     return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
7950   else
7951     return NULL_RTX;
7952 }
7953
7954 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
7955
7956 static rtx
7957 rs6000_mixed_function_arg (enum machine_mode mode, tree type, int align_words)
7958 {
7959   int n_units;
7960   int i, k;
7961   rtx rvec[GP_ARG_NUM_REG + 1];
7962
7963   if (align_words >= GP_ARG_NUM_REG)
7964     return NULL_RTX;
7965
7966   n_units = rs6000_arg_size (mode, type);
7967
7968   /* Optimize the simple case where the arg fits in one gpr, except in
7969      the case of BLKmode due to assign_parms assuming that registers are
7970      BITS_PER_WORD wide.  */
7971   if (n_units == 0
7972       || (n_units == 1 && mode != BLKmode))
7973     return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
7974
7975   k = 0;
7976   if (align_words + n_units > GP_ARG_NUM_REG)
7977     /* Not all of the arg fits in gprs.  Say that it goes in memory too,
7978        using a magic NULL_RTX component.
7979        This is not strictly correct.  Only some of the arg belongs in
7980        memory, not all of it.  However, the normal scheme using
7981        function_arg_partial_nregs can result in unusual subregs, eg.
7982        (subreg:SI (reg:DF) 4), which are not handled well.  The code to
7983        store the whole arg to memory is often more efficient than code
7984        to store pieces, and we know that space is available in the right
7985        place for the whole arg.  */
7986     rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
7987
7988   i = 0;
7989   do
7990     {
7991       rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
7992       rtx off = GEN_INT (i++ * 4);
7993       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
7994     }
7995   while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
7996
7997   return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
7998 }
7999
8000 /* Determine where to put an argument to a function.
8001    Value is zero to push the argument on the stack,
8002    or a hard register in which to store the argument.
8003
8004    MODE is the argument's machine mode.
8005    TYPE is the data type of the argument (as a tree).
8006     This is null for libcalls where that information may
8007     not be available.
8008    CUM is a variable of type CUMULATIVE_ARGS which gives info about
8009     the preceding args and about the function being called.  It is
8010     not modified in this routine.
8011    NAMED is nonzero if this argument is a named parameter
8012     (otherwise it is an extra parameter matching an ellipsis).
8013
8014    On RS/6000 the first eight words of non-FP are normally in registers
8015    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
8016    Under V.4, the first 8 FP args are in registers.
8017
8018    If this is floating-point and no prototype is specified, we use
8019    both an FP and integer register (or possibly FP reg and stack).  Library
8020    functions (when CALL_LIBCALL is set) always have the proper types for args,
8021    so we can pass the FP value just in one register.  emit_library_function
8022    doesn't support PARALLEL anyway.
8023
8024    Note that for args passed by reference, function_arg will be called
8025    with MODE and TYPE set to that of the pointer to the arg, not the arg
8026    itself.  */
8027
8028 rtx
8029 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8030               tree type, int named)
8031 {
8032   enum rs6000_abi abi = DEFAULT_ABI;
8033
8034   /* Return a marker to indicate whether CR1 needs to set or clear the
8035      bit that V.4 uses to say fp args were passed in registers.
8036      Assume that we don't need the marker for software floating point,
8037      or compiler generated library calls.  */
8038   if (mode == VOIDmode)
8039     {
8040       if (abi == ABI_V4
8041           && (cum->call_cookie & CALL_LIBCALL) == 0
8042           && (cum->stdarg
8043               || (cum->nargs_prototype < 0
8044                   && (cum->prototype || TARGET_NO_PROTOTYPE))))
8045         {
8046           /* For the SPE, we need to crxor CR6 always.  */
8047           if (TARGET_SPE_ABI)
8048             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
8049           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
8050             return GEN_INT (cum->call_cookie
8051                             | ((cum->fregno == FP_ARG_MIN_REG)
8052                                ? CALL_V4_SET_FP_ARGS
8053                                : CALL_V4_CLEAR_FP_ARGS));
8054         }
8055
8056       return GEN_INT (cum->call_cookie);
8057     }
8058
8059   if (rs6000_darwin64_abi && mode == BLKmode
8060       && TREE_CODE (type) == RECORD_TYPE)
8061     {
8062       rtx rslt = rs6000_darwin64_record_arg (cum, type, named, false);
8063       if (rslt != NULL_RTX)
8064         return rslt;
8065       /* Else fall through to usual handling.  */
8066     }
8067
8068   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8069     if (TARGET_64BIT && ! cum->prototype)
8070       {
8071         /* Vector parameters get passed in vector register
8072            and also in GPRs or memory, in absence of prototype.  */
8073         int align_words;
8074         rtx slot;
8075         align_words = (cum->words + 1) & ~1;
8076
8077         if (align_words >= GP_ARG_NUM_REG)
8078           {
8079             slot = NULL_RTX;
8080           }
8081         else
8082           {
8083             slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8084           }
8085         return gen_rtx_PARALLEL (mode,
8086                  gen_rtvec (2,
8087                             gen_rtx_EXPR_LIST (VOIDmode,
8088                                                slot, const0_rtx),
8089                             gen_rtx_EXPR_LIST (VOIDmode,
8090                                                gen_rtx_REG (mode, cum->vregno),
8091                                                const0_rtx)));
8092       }
8093     else
8094       return gen_rtx_REG (mode, cum->vregno);
8095   else if (TARGET_ALTIVEC_ABI
8096            && (ALTIVEC_VECTOR_MODE (mode)
8097                || VSX_VECTOR_MODE (mode)
8098                || (type && TREE_CODE (type) == VECTOR_TYPE
8099                    && int_size_in_bytes (type) == 16)))
8100     {
8101       if (named || abi == ABI_V4)
8102         return NULL_RTX;
8103       else
8104         {
8105           /* Vector parameters to varargs functions under AIX or Darwin
8106              get passed in memory and possibly also in GPRs.  */
8107           int align, align_words, n_words;
8108           enum machine_mode part_mode;
8109
8110           /* Vector parameters must be 16-byte aligned.  This places them at
8111              2 mod 4 in terms of words in 32-bit mode, since the parameter
8112              save area starts at offset 24 from the stack.  In 64-bit mode,
8113              they just have to start on an even word, since the parameter
8114              save area is 16-byte aligned.  */
8115           if (TARGET_32BIT)
8116             align = (2 - cum->words) & 3;
8117           else
8118             align = cum->words & 1;
8119           align_words = cum->words + align;
8120
8121           /* Out of registers?  Memory, then.  */
8122           if (align_words >= GP_ARG_NUM_REG)
8123             return NULL_RTX;
8124
8125           if (TARGET_32BIT && TARGET_POWERPC64)
8126             return rs6000_mixed_function_arg (mode, type, align_words);
8127
8128           /* The vector value goes in GPRs.  Only the part of the
8129              value in GPRs is reported here.  */
8130           part_mode = mode;
8131           n_words = rs6000_arg_size (mode, type);
8132           if (align_words + n_words > GP_ARG_NUM_REG)
8133             /* Fortunately, there are only two possibilities, the value
8134                is either wholly in GPRs or half in GPRs and half not.  */
8135             part_mode = DImode;
8136
8137           return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
8138         }
8139     }
8140   else if (TARGET_SPE_ABI && TARGET_SPE
8141            && (SPE_VECTOR_MODE (mode)
8142                || (TARGET_E500_DOUBLE && (mode == DFmode
8143                                           || mode == DCmode
8144                                           || mode == TFmode
8145                                           || mode == TCmode))))
8146     return rs6000_spe_function_arg (cum, mode, type);
8147
8148   else if (abi == ABI_V4)
8149     {
8150       if (TARGET_HARD_FLOAT && TARGET_FPRS
8151           && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8152               || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8153               || (mode == TFmode && !TARGET_IEEEQUAD)
8154               || mode == SDmode || mode == DDmode || mode == TDmode))
8155         {
8156           /* _Decimal128 must use an even/odd register pair.  This assumes
8157              that the register number is odd when fregno is odd.  */
8158           if (mode == TDmode && (cum->fregno % 2) == 1)
8159             cum->fregno++;
8160
8161           if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8162               <= FP_ARG_V4_MAX_REG)
8163             return gen_rtx_REG (mode, cum->fregno);
8164           else
8165             return NULL_RTX;
8166         }
8167       else
8168         {
8169           int n_words = rs6000_arg_size (mode, type);
8170           int gregno = cum->sysv_gregno;
8171
8172           /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8173              (r7,r8) or (r9,r10).  As does any other 2 word item such
8174              as complex int due to a historical mistake.  */
8175           if (n_words == 2)
8176             gregno += (1 - gregno) & 1;
8177
8178           /* Multi-reg args are not split between registers and stack.  */
8179           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8180             return NULL_RTX;
8181
8182           if (TARGET_32BIT && TARGET_POWERPC64)
8183             return rs6000_mixed_function_arg (mode, type,
8184                                               gregno - GP_ARG_MIN_REG);
8185           return gen_rtx_REG (mode, gregno);
8186         }
8187     }
8188   else
8189     {
8190       int align_words = rs6000_parm_start (mode, type, cum->words);
8191
8192       /* _Decimal128 must be passed in an even/odd float register pair.
8193          This assumes that the register number is odd when fregno is odd.  */
8194       if (mode == TDmode && (cum->fregno % 2) == 1)
8195         cum->fregno++;
8196
8197       if (USE_FP_FOR_ARG_P (cum, mode, type))
8198         {
8199           rtx rvec[GP_ARG_NUM_REG + 1];
8200           rtx r;
8201           int k;
8202           bool needs_psave;
8203           enum machine_mode fmode = mode;
8204           unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8205
8206           if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8207             {
8208               /* Currently, we only ever need one reg here because complex
8209                  doubles are split.  */
8210               gcc_assert (cum->fregno == FP_ARG_MAX_REG
8211                           && (fmode == TFmode || fmode == TDmode));
8212
8213               /* Long double or _Decimal128 split over regs and memory.  */
8214               fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
8215             }
8216
8217           /* Do we also need to pass this arg in the parameter save
8218              area?  */
8219           needs_psave = (type
8220                          && (cum->nargs_prototype <= 0
8221                              || (DEFAULT_ABI == ABI_AIX
8222                                  && TARGET_XL_COMPAT
8223                                  && align_words >= GP_ARG_NUM_REG)));
8224
8225           if (!needs_psave && mode == fmode)
8226             return gen_rtx_REG (fmode, cum->fregno);
8227
8228           k = 0;
8229           if (needs_psave)
8230             {
8231               /* Describe the part that goes in gprs or the stack.
8232                  This piece must come first, before the fprs.  */
8233               if (align_words < GP_ARG_NUM_REG)
8234                 {
8235                   unsigned long n_words = rs6000_arg_size (mode, type);
8236
8237                   if (align_words + n_words > GP_ARG_NUM_REG
8238                       || (TARGET_32BIT && TARGET_POWERPC64))
8239                     {
8240                       /* If this is partially on the stack, then we only
8241                          include the portion actually in registers here.  */
8242                       enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
8243                       rtx off;
8244                       int i = 0;
8245                       if (align_words + n_words > GP_ARG_NUM_REG)
8246                         /* Not all of the arg fits in gprs.  Say that it
8247                            goes in memory too, using a magic NULL_RTX
8248                            component.  Also see comment in
8249                            rs6000_mixed_function_arg for why the normal
8250                            function_arg_partial_nregs scheme doesn't work
8251                            in this case. */
8252                         rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
8253                                                        const0_rtx);
8254                       do
8255                         {
8256                           r = gen_rtx_REG (rmode,
8257                                            GP_ARG_MIN_REG + align_words);
8258                           off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
8259                           rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8260                         }
8261                       while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
8262                     }
8263                   else
8264                     {
8265                       /* The whole arg fits in gprs.  */
8266                       r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8267                       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8268                     }
8269                 }
8270               else
8271                 /* It's entirely in memory.  */
8272                 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8273             }
8274
8275           /* Describe where this piece goes in the fprs.  */
8276           r = gen_rtx_REG (fmode, cum->fregno);
8277           rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8278
8279           return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8280         }
8281       else if (align_words < GP_ARG_NUM_REG)
8282         {
8283           if (TARGET_32BIT && TARGET_POWERPC64)
8284             return rs6000_mixed_function_arg (mode, type, align_words);
8285
8286           if (mode == BLKmode)
8287             mode = Pmode;
8288
8289           return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8290         }
8291       else
8292         return NULL_RTX;
8293     }
8294 }
8295 \f
8296 /* For an arg passed partly in registers and partly in memory, this is
8297    the number of bytes passed in registers.  For args passed entirely in
8298    registers or entirely in memory, zero.  When an arg is described by a
8299    PARALLEL, perhaps using more than one register type, this function
8300    returns the number of bytes used by the first element of the PARALLEL.  */
8301
8302 static int
8303 rs6000_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8304                           tree type, bool named)
8305 {
8306   int ret = 0;
8307   int align_words;
8308
8309   if (DEFAULT_ABI == ABI_V4)
8310     return 0;
8311
8312   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
8313       && cum->nargs_prototype >= 0)
8314     return 0;
8315
8316   /* In this complicated case we just disable the partial_nregs code.  */
8317   if (rs6000_darwin64_abi && mode == BLKmode
8318       && TREE_CODE (type) == RECORD_TYPE
8319       && int_size_in_bytes (type) > 0)
8320     return 0;
8321
8322   align_words = rs6000_parm_start (mode, type, cum->words);
8323
8324   if (USE_FP_FOR_ARG_P (cum, mode, type))
8325     {
8326       /* If we are passing this arg in the fixed parameter save area
8327          (gprs or memory) as well as fprs, then this function should
8328          return the number of partial bytes passed in the parameter
8329          save area rather than partial bytes passed in fprs.  */
8330       if (type
8331           && (cum->nargs_prototype <= 0
8332               || (DEFAULT_ABI == ABI_AIX
8333                   && TARGET_XL_COMPAT
8334                   && align_words >= GP_ARG_NUM_REG)))
8335         return 0;
8336       else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
8337                > FP_ARG_MAX_REG + 1)
8338         ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
8339       else if (cum->nargs_prototype >= 0)
8340         return 0;
8341     }
8342
8343   if (align_words < GP_ARG_NUM_REG
8344       && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
8345     ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
8346
8347   if (ret != 0 && TARGET_DEBUG_ARG)
8348     fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
8349
8350   return ret;
8351 }
8352 \f
8353 /* A C expression that indicates when an argument must be passed by
8354    reference.  If nonzero for an argument, a copy of that argument is
8355    made in memory and a pointer to the argument is passed instead of
8356    the argument itself.  The pointer is passed in whatever way is
8357    appropriate for passing a pointer to that type.
8358
8359    Under V.4, aggregates and long double are passed by reference.
8360
8361    As an extension to all 32-bit ABIs, AltiVec vectors are passed by
8362    reference unless the AltiVec vector extension ABI is in force.
8363
8364    As an extension to all ABIs, variable sized types are passed by
8365    reference.  */
8366
8367 static bool
8368 rs6000_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
8369                           enum machine_mode mode, const_tree type,
8370                           bool named ATTRIBUTE_UNUSED)
8371 {
8372   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
8373     {
8374       if (TARGET_DEBUG_ARG)
8375         fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
8376       return 1;
8377     }
8378
8379   if (!type)
8380     return 0;
8381
8382   if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
8383     {
8384       if (TARGET_DEBUG_ARG)
8385         fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
8386       return 1;
8387     }
8388
8389   if (int_size_in_bytes (type) < 0)
8390     {
8391       if (TARGET_DEBUG_ARG)
8392         fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
8393       return 1;
8394     }
8395
8396   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
8397      modes only exist for GCC vector types if -maltivec.  */
8398   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
8399     {
8400       if (TARGET_DEBUG_ARG)
8401         fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
8402       return 1;
8403     }
8404
8405   /* Pass synthetic vectors in memory.  */
8406   if (TREE_CODE (type) == VECTOR_TYPE
8407       && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8408     {
8409       static bool warned_for_pass_big_vectors = false;
8410       if (TARGET_DEBUG_ARG)
8411         fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
8412       if (!warned_for_pass_big_vectors)
8413         {
8414           warning (0, "GCC vector passed by reference: "
8415                    "non-standard ABI extension with no compatibility guarantee");
8416           warned_for_pass_big_vectors = true;
8417         }
8418       return 1;
8419     }
8420
8421   return 0;
8422 }
8423
8424 static void
8425 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
8426 {
8427   int i;
8428   enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
8429
8430   if (nregs == 0)
8431     return;
8432
8433   for (i = 0; i < nregs; i++)
8434     {
8435       rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
8436       if (reload_completed)
8437         {
8438           if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
8439             tem = NULL_RTX;
8440           else
8441             tem = simplify_gen_subreg (reg_mode, x, BLKmode,
8442                                        i * GET_MODE_SIZE (reg_mode));
8443         }
8444       else
8445         tem = replace_equiv_address (tem, XEXP (tem, 0));
8446
8447       gcc_assert (tem);
8448
8449       emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
8450     }
8451 }
8452 \f
8453 /* Perform any needed actions needed for a function that is receiving a
8454    variable number of arguments.
8455
8456    CUM is as above.
8457
8458    MODE and TYPE are the mode and type of the current parameter.
8459
8460    PRETEND_SIZE is a variable that should be set to the amount of stack
8461    that must be pushed by the prolog to pretend that our caller pushed
8462    it.
8463
8464    Normally, this macro will push all remaining incoming registers on the
8465    stack and set PRETEND_SIZE to the length of the registers pushed.  */
8466
8467 static void
8468 setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8469                         tree type, int *pretend_size ATTRIBUTE_UNUSED,
8470                         int no_rtl)
8471 {
8472   CUMULATIVE_ARGS next_cum;
8473   int reg_size = TARGET_32BIT ? 4 : 8;
8474   rtx save_area = NULL_RTX, mem;
8475   int first_reg_offset;
8476   alias_set_type set;
8477
8478   /* Skip the last named argument.  */
8479   next_cum = *cum;
8480   function_arg_advance (&next_cum, mode, type, 1, 0);
8481
8482   if (DEFAULT_ABI == ABI_V4)
8483     {
8484       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
8485
8486       if (! no_rtl)
8487         {
8488           int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
8489           HOST_WIDE_INT offset = 0;
8490
8491           /* Try to optimize the size of the varargs save area.
8492              The ABI requires that ap.reg_save_area is doubleword
8493              aligned, but we don't need to allocate space for all
8494              the bytes, only those to which we actually will save
8495              anything.  */
8496           if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
8497             gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
8498           if (TARGET_HARD_FLOAT && TARGET_FPRS
8499               && next_cum.fregno <= FP_ARG_V4_MAX_REG
8500               && cfun->va_list_fpr_size)
8501             {
8502               if (gpr_reg_num)
8503                 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
8504                            * UNITS_PER_FP_WORD;
8505               if (cfun->va_list_fpr_size
8506                   < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8507                 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
8508               else
8509                 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8510                             * UNITS_PER_FP_WORD;
8511             }
8512           if (gpr_reg_num)
8513             {
8514               offset = -((first_reg_offset * reg_size) & ~7);
8515               if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
8516                 {
8517                   gpr_reg_num = cfun->va_list_gpr_size;
8518                   if (reg_size == 4 && (first_reg_offset & 1))
8519                     gpr_reg_num++;
8520                 }
8521               gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
8522             }
8523           else if (fpr_size)
8524             offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
8525                        * UNITS_PER_FP_WORD
8526                      - (int) (GP_ARG_NUM_REG * reg_size);
8527
8528           if (gpr_size + fpr_size)
8529             {
8530               rtx reg_save_area
8531                 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
8532               gcc_assert (GET_CODE (reg_save_area) == MEM);
8533               reg_save_area = XEXP (reg_save_area, 0);
8534               if (GET_CODE (reg_save_area) == PLUS)
8535                 {
8536                   gcc_assert (XEXP (reg_save_area, 0)
8537                               == virtual_stack_vars_rtx);
8538                   gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
8539                   offset += INTVAL (XEXP (reg_save_area, 1));
8540                 }
8541               else
8542                 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
8543             }
8544
8545           cfun->machine->varargs_save_offset = offset;
8546           save_area = plus_constant (virtual_stack_vars_rtx, offset);
8547         }
8548     }
8549   else
8550     {
8551       first_reg_offset = next_cum.words;
8552       save_area = virtual_incoming_args_rtx;
8553
8554       if (targetm.calls.must_pass_in_stack (mode, type))
8555         first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
8556     }
8557
8558   set = get_varargs_alias_set ();
8559   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
8560       && cfun->va_list_gpr_size)
8561     {
8562       int nregs = GP_ARG_NUM_REG - first_reg_offset;
8563
8564       if (va_list_gpr_counter_field)
8565         {
8566           /* V4 va_list_gpr_size counts number of registers needed.  */
8567           if (nregs > cfun->va_list_gpr_size)
8568             nregs = cfun->va_list_gpr_size;
8569         }
8570       else
8571         {
8572           /* char * va_list instead counts number of bytes needed.  */
8573           if (nregs > cfun->va_list_gpr_size / reg_size)
8574             nregs = cfun->va_list_gpr_size / reg_size;
8575         }
8576
8577       mem = gen_rtx_MEM (BLKmode,
8578                          plus_constant (save_area,
8579                                         first_reg_offset * reg_size));
8580       MEM_NOTRAP_P (mem) = 1;
8581       set_mem_alias_set (mem, set);
8582       set_mem_align (mem, BITS_PER_WORD);
8583
8584       rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
8585                                   nregs);
8586     }
8587
8588   /* Save FP registers if needed.  */
8589   if (DEFAULT_ABI == ABI_V4
8590       && TARGET_HARD_FLOAT && TARGET_FPRS
8591       && ! no_rtl
8592       && next_cum.fregno <= FP_ARG_V4_MAX_REG
8593       && cfun->va_list_fpr_size)
8594     {
8595       int fregno = next_cum.fregno, nregs;
8596       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
8597       rtx lab = gen_label_rtx ();
8598       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
8599                                                * UNITS_PER_FP_WORD);
8600
8601       emit_jump_insn
8602         (gen_rtx_SET (VOIDmode,
8603                       pc_rtx,
8604                       gen_rtx_IF_THEN_ELSE (VOIDmode,
8605                                             gen_rtx_NE (VOIDmode, cr1,
8606                                                         const0_rtx),
8607                                             gen_rtx_LABEL_REF (VOIDmode, lab),
8608                                             pc_rtx)));
8609
8610       for (nregs = 0;
8611            fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
8612            fregno++, off += UNITS_PER_FP_WORD, nregs++)
8613         {
8614           mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8615                               ? DFmode : SFmode, 
8616                              plus_constant (save_area, off));
8617           MEM_NOTRAP_P (mem) = 1;
8618           set_mem_alias_set (mem, set);
8619           set_mem_align (mem, GET_MODE_ALIGNMENT (
8620                          (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8621                           ? DFmode : SFmode));
8622           emit_move_insn (mem, gen_rtx_REG (
8623                           (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8624                            ? DFmode : SFmode, fregno));
8625         }
8626
8627       emit_label (lab);
8628     }
8629 }
8630
8631 /* Create the va_list data type.  */
8632
8633 static tree
8634 rs6000_build_builtin_va_list (void)
8635 {
8636   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
8637
8638   /* For AIX, prefer 'char *' because that's what the system
8639      header files like.  */
8640   if (DEFAULT_ABI != ABI_V4)
8641     return build_pointer_type (char_type_node);
8642
8643   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
8644   type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
8645                           get_identifier ("__va_list_tag"), record);
8646
8647   f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
8648                       unsigned_char_type_node);
8649   f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
8650                       unsigned_char_type_node);
8651   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
8652      every user file.  */
8653   f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
8654                       get_identifier ("reserved"), short_unsigned_type_node);
8655   f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
8656                       get_identifier ("overflow_arg_area"),
8657                       ptr_type_node);
8658   f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
8659                       get_identifier ("reg_save_area"),
8660                       ptr_type_node);
8661
8662   va_list_gpr_counter_field = f_gpr;
8663   va_list_fpr_counter_field = f_fpr;
8664
8665   DECL_FIELD_CONTEXT (f_gpr) = record;
8666   DECL_FIELD_CONTEXT (f_fpr) = record;
8667   DECL_FIELD_CONTEXT (f_res) = record;
8668   DECL_FIELD_CONTEXT (f_ovf) = record;
8669   DECL_FIELD_CONTEXT (f_sav) = record;
8670
8671   TREE_CHAIN (record) = type_decl;
8672   TYPE_NAME (record) = type_decl;
8673   TYPE_FIELDS (record) = f_gpr;
8674   TREE_CHAIN (f_gpr) = f_fpr;
8675   TREE_CHAIN (f_fpr) = f_res;
8676   TREE_CHAIN (f_res) = f_ovf;
8677   TREE_CHAIN (f_ovf) = f_sav;
8678
8679   layout_type (record);
8680
8681   /* The correct type is an array type of one element.  */
8682   return build_array_type (record, build_index_type (size_zero_node));
8683 }
8684
8685 /* Implement va_start.  */
8686
8687 static void
8688 rs6000_va_start (tree valist, rtx nextarg)
8689 {
8690   HOST_WIDE_INT words, n_gpr, n_fpr;
8691   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
8692   tree gpr, fpr, ovf, sav, t;
8693
8694   /* Only SVR4 needs something special.  */
8695   if (DEFAULT_ABI != ABI_V4)
8696     {
8697       std_expand_builtin_va_start (valist, nextarg);
8698       return;
8699     }
8700
8701   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
8702   f_fpr = TREE_CHAIN (f_gpr);
8703   f_res = TREE_CHAIN (f_fpr);
8704   f_ovf = TREE_CHAIN (f_res);
8705   f_sav = TREE_CHAIN (f_ovf);
8706
8707   valist = build_va_arg_indirect_ref (valist);
8708   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
8709   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
8710                 f_fpr, NULL_TREE);
8711   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
8712                 f_ovf, NULL_TREE);
8713   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
8714                 f_sav, NULL_TREE);
8715
8716   /* Count number of gp and fp argument registers used.  */
8717   words = crtl->args.info.words;
8718   n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
8719                GP_ARG_NUM_REG);
8720   n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
8721                FP_ARG_NUM_REG);
8722
8723   if (TARGET_DEBUG_ARG)
8724     fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
8725              HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
8726              words, n_gpr, n_fpr);
8727
8728   if (cfun->va_list_gpr_size)
8729     {
8730       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
8731                   build_int_cst (NULL_TREE, n_gpr));
8732       TREE_SIDE_EFFECTS (t) = 1;
8733       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8734     }
8735
8736   if (cfun->va_list_fpr_size)
8737     {
8738       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
8739                   build_int_cst (NULL_TREE, n_fpr));
8740       TREE_SIDE_EFFECTS (t) = 1;
8741       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8742     }
8743
8744   /* Find the overflow area.  */
8745   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
8746   if (words != 0)
8747     t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), t,
8748                 size_int (words * UNITS_PER_WORD));
8749   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
8750   TREE_SIDE_EFFECTS (t) = 1;
8751   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8752
8753   /* If there were no va_arg invocations, don't set up the register
8754      save area.  */
8755   if (!cfun->va_list_gpr_size
8756       && !cfun->va_list_fpr_size
8757       && n_gpr < GP_ARG_NUM_REG
8758       && n_fpr < FP_ARG_V4_MAX_REG)
8759     return;
8760
8761   /* Find the register save area.  */
8762   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
8763   if (cfun->machine->varargs_save_offset)
8764     t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (sav), t,
8765                 size_int (cfun->machine->varargs_save_offset));
8766   t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
8767   TREE_SIDE_EFFECTS (t) = 1;
8768   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8769 }
8770
8771 /* Implement va_arg.  */
8772
8773 tree
8774 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
8775                         gimple_seq *post_p)
8776 {
8777   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
8778   tree gpr, fpr, ovf, sav, reg, t, u;
8779   int size, rsize, n_reg, sav_ofs, sav_scale;
8780   tree lab_false, lab_over, addr;
8781   int align;
8782   tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
8783   int regalign = 0;
8784   gimple stmt;
8785
8786   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
8787     {
8788       t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
8789       return build_va_arg_indirect_ref (t);
8790     }
8791
8792   if (DEFAULT_ABI != ABI_V4)
8793     {
8794       if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
8795         {
8796           tree elem_type = TREE_TYPE (type);
8797           enum machine_mode elem_mode = TYPE_MODE (elem_type);
8798           int elem_size = GET_MODE_SIZE (elem_mode);
8799
8800           if (elem_size < UNITS_PER_WORD)
8801             {
8802               tree real_part, imag_part;
8803               gimple_seq post = NULL;
8804
8805               real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
8806                                                   &post);
8807               /* Copy the value into a temporary, lest the formal temporary
8808                  be reused out from under us.  */
8809               real_part = get_initialized_tmp_var (real_part, pre_p, &post);
8810               gimple_seq_add_seq (pre_p, post);
8811
8812               imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
8813                                                   post_p);
8814
8815               return build2 (COMPLEX_EXPR, type, real_part, imag_part);
8816             }
8817         }
8818
8819       return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
8820     }
8821
8822   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
8823   f_fpr = TREE_CHAIN (f_gpr);
8824   f_res = TREE_CHAIN (f_fpr);
8825   f_ovf = TREE_CHAIN (f_res);
8826   f_sav = TREE_CHAIN (f_ovf);
8827
8828   valist = build_va_arg_indirect_ref (valist);
8829   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
8830   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
8831                 f_fpr, NULL_TREE);
8832   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
8833                 f_ovf, NULL_TREE);
8834   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
8835                 f_sav, NULL_TREE);
8836
8837   size = int_size_in_bytes (type);
8838   rsize = (size + 3) / 4;
8839   align = 1;
8840
8841   if (TARGET_HARD_FLOAT && TARGET_FPRS
8842       && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
8843           || (TARGET_DOUBLE_FLOAT 
8844               && (TYPE_MODE (type) == DFmode 
8845                   || TYPE_MODE (type) == TFmode
8846                   || TYPE_MODE (type) == SDmode
8847                   || TYPE_MODE (type) == DDmode
8848                   || TYPE_MODE (type) == TDmode))))
8849     {
8850       /* FP args go in FP registers, if present.  */
8851       reg = fpr;
8852       n_reg = (size + 7) / 8;
8853       sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
8854       sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
8855       if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
8856         align = 8;
8857     }
8858   else
8859     {
8860       /* Otherwise into GP registers.  */
8861       reg = gpr;
8862       n_reg = rsize;
8863       sav_ofs = 0;
8864       sav_scale = 4;
8865       if (n_reg == 2)
8866         align = 8;
8867     }
8868
8869   /* Pull the value out of the saved registers....  */
8870
8871   lab_over = NULL;
8872   addr = create_tmp_var (ptr_type_node, "addr");
8873
8874   /*  AltiVec vectors never go in registers when -mabi=altivec.  */
8875   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
8876     align = 16;
8877   else
8878     {
8879       lab_false = create_artificial_label (input_location);
8880       lab_over = create_artificial_label (input_location);
8881
8882       /* Long long and SPE vectors are aligned in the registers.
8883          As are any other 2 gpr item such as complex int due to a
8884          historical mistake.  */
8885       u = reg;
8886       if (n_reg == 2 && reg == gpr)
8887         {
8888           regalign = 1;
8889           u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
8890                      build_int_cst (TREE_TYPE (reg), n_reg - 1));
8891           u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
8892                       unshare_expr (reg), u);
8893         }
8894       /* _Decimal128 is passed in even/odd fpr pairs; the stored
8895          reg number is 0 for f1, so we want to make it odd.  */
8896       else if (reg == fpr && TYPE_MODE (type) == TDmode)
8897         {
8898           t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
8899                       build_int_cst (TREE_TYPE (reg), 1));
8900           u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
8901         }
8902
8903       t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
8904       t = build2 (GE_EXPR, boolean_type_node, u, t);
8905       u = build1 (GOTO_EXPR, void_type_node, lab_false);
8906       t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
8907       gimplify_and_add (t, pre_p);
8908
8909       t = sav;
8910       if (sav_ofs)
8911         t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, size_int (sav_ofs));
8912
8913       u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
8914                   build_int_cst (TREE_TYPE (reg), n_reg));
8915       u = fold_convert (sizetype, u);
8916       u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
8917       t = build2 (POINTER_PLUS_EXPR, ptr_type_node, t, u);
8918
8919       /* _Decimal32 varargs are located in the second word of the 64-bit
8920          FP register for 32-bit binaries.  */
8921       if (!TARGET_POWERPC64
8922           && TARGET_HARD_FLOAT && TARGET_FPRS
8923           && TYPE_MODE (type) == SDmode)
8924         t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, size_int (size));
8925
8926       gimplify_assign (addr, t, pre_p);
8927
8928       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
8929
8930       stmt = gimple_build_label (lab_false);
8931       gimple_seq_add_stmt (pre_p, stmt);
8932
8933       if ((n_reg == 2 && !regalign) || n_reg > 2)
8934         {
8935           /* Ensure that we don't find any more args in regs.
8936              Alignment has taken care of for special cases.  */
8937           gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
8938         }
8939     }
8940
8941   /* ... otherwise out of the overflow area.  */
8942
8943   /* Care for on-stack alignment if needed.  */
8944   t = ovf;
8945   if (align != 1)
8946     {
8947       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, size_int (align - 1));
8948       t = fold_convert (sizetype, t);
8949       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
8950                   size_int (-align));
8951       t = fold_convert (TREE_TYPE (ovf), t);
8952     }
8953   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
8954
8955   gimplify_assign (unshare_expr (addr), t, pre_p);
8956
8957   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, size_int (size));
8958   gimplify_assign (unshare_expr (ovf), t, pre_p);
8959
8960   if (lab_over)
8961     {
8962       stmt = gimple_build_label (lab_over);
8963       gimple_seq_add_stmt (pre_p, stmt);
8964     }
8965
8966   if (STRICT_ALIGNMENT
8967       && (TYPE_ALIGN (type)
8968           > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
8969     {
8970       /* The value (of type complex double, for example) may not be
8971          aligned in memory in the saved registers, so copy via a
8972          temporary.  (This is the same code as used for SPARC.)  */
8973       tree tmp = create_tmp_var (type, "va_arg_tmp");
8974       tree dest_addr = build_fold_addr_expr (tmp);
8975
8976       tree copy = build_call_expr (implicit_built_in_decls[BUILT_IN_MEMCPY],
8977                                    3, dest_addr, addr, size_int (rsize * 4));
8978
8979       gimplify_and_add (copy, pre_p);
8980       addr = dest_addr;
8981     }
8982
8983   addr = fold_convert (ptrtype, addr);
8984   return build_va_arg_indirect_ref (addr);
8985 }
8986
8987 /* Builtins.  */
8988
8989 static void
8990 def_builtin (int mask, const char *name, tree type, int code)
8991 {
8992   if ((mask & target_flags) || TARGET_PAIRED_FLOAT)
8993     {
8994       tree t;
8995       if (rs6000_builtin_decls[code])
8996         fatal_error ("internal error: builtin function to %s already processed.",
8997                      name);
8998
8999       rs6000_builtin_decls[code] = t =
9000         add_builtin_function (name, type, code, BUILT_IN_MD,
9001                               NULL, NULL_TREE);
9002
9003       gcc_assert (code >= 0 && code < (int)RS6000_BUILTIN_COUNT);
9004       switch (builtin_classify[code])
9005         {
9006         default:
9007           gcc_unreachable ();
9008
9009           /* assume builtin can do anything.  */
9010         case RS6000_BTC_MISC:
9011           break;
9012
9013           /* const function, function only depends on the inputs.  */
9014         case RS6000_BTC_CONST:
9015           TREE_READONLY (t) = 1;
9016           TREE_NOTHROW (t) = 1;
9017           break;
9018
9019           /* pure function, function can read global memory.  */
9020         case RS6000_BTC_PURE:
9021           DECL_PURE_P (t) = 1;
9022           TREE_NOTHROW (t) = 1;
9023           break;
9024
9025           /* Function is a math function.  If rounding mode is on, then treat
9026              the function as not reading global memory, but it can have
9027              arbitrary side effects.  If it is off, then assume the function is
9028              a const function.  This mimics the ATTR_MATHFN_FPROUNDING
9029              attribute in builtin-attribute.def that is used for the math
9030              functions. */
9031         case RS6000_BTC_FP_PURE:
9032           TREE_NOTHROW (t) = 1;
9033           if (flag_rounding_math)
9034             {
9035               DECL_PURE_P (t) = 1;
9036               DECL_IS_NOVOPS (t) = 1;
9037             }
9038           else
9039             TREE_READONLY (t) = 1;
9040           break;
9041         }
9042     }
9043 }
9044
9045 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
9046
9047 static const struct builtin_description bdesc_3arg[] =
9048 {
9049   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
9050   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
9051   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
9052   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
9053   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
9054   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
9055   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
9056   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
9057   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
9058   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
9059   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP },
9060   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v2df, "__builtin_altivec_vperm_2df", ALTIVEC_BUILTIN_VPERM_2DF },
9061   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v2di, "__builtin_altivec_vperm_2di", ALTIVEC_BUILTIN_VPERM_2DI },
9062   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
9063   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
9064   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
9065   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v16qi_uns, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
9066   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v2di_uns, "__builtin_altivec_vperm_2di_uns", ALTIVEC_BUILTIN_VPERM_2DI_UNS },
9067   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v4si_uns, "__builtin_altivec_vperm_4si_uns", ALTIVEC_BUILTIN_VPERM_4SI_UNS },
9068   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v8hi_uns, "__builtin_altivec_vperm_8hi_uns", ALTIVEC_BUILTIN_VPERM_8HI_UNS },
9069   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_v16qi_uns, "__builtin_altivec_vperm_16qi_uns", ALTIVEC_BUILTIN_VPERM_16QI_UNS },
9070   { MASK_ALTIVEC, CODE_FOR_vector_select_v4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
9071   { MASK_ALTIVEC, CODE_FOR_vector_select_v4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
9072   { MASK_ALTIVEC, CODE_FOR_vector_select_v8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
9073   { MASK_ALTIVEC, CODE_FOR_vector_select_v16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
9074   { MASK_ALTIVEC, CODE_FOR_vector_select_v2df, "__builtin_altivec_vsel_2df", ALTIVEC_BUILTIN_VSEL_2DF },
9075   { MASK_ALTIVEC, CODE_FOR_vector_select_v2di, "__builtin_altivec_vsel_2di", ALTIVEC_BUILTIN_VSEL_2DI },
9076   { MASK_ALTIVEC, CODE_FOR_vector_select_v4si_uns, "__builtin_altivec_vsel_4si_uns", ALTIVEC_BUILTIN_VSEL_4SI_UNS },
9077   { MASK_ALTIVEC, CODE_FOR_vector_select_v8hi_uns, "__builtin_altivec_vsel_8hi_uns", ALTIVEC_BUILTIN_VSEL_8HI_UNS },
9078   { MASK_ALTIVEC, CODE_FOR_vector_select_v16qi_uns, "__builtin_altivec_vsel_16qi_uns", ALTIVEC_BUILTIN_VSEL_16QI_UNS },
9079   { MASK_ALTIVEC, CODE_FOR_vector_select_v2di_uns, "__builtin_altivec_vsel_2di_uns", ALTIVEC_BUILTIN_VSEL_2DI_UNS },
9080   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_v16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
9081   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_v8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
9082   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_v4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
9083   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_v4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
9084
9085   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_madd", ALTIVEC_BUILTIN_VEC_MADD },
9086   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_madds", ALTIVEC_BUILTIN_VEC_MADDS },
9087   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mladd", ALTIVEC_BUILTIN_VEC_MLADD },
9088   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mradds", ALTIVEC_BUILTIN_VEC_MRADDS },
9089   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_msum", ALTIVEC_BUILTIN_VEC_MSUM },
9090   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmsumshm", ALTIVEC_BUILTIN_VEC_VMSUMSHM },
9091   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmsumuhm", ALTIVEC_BUILTIN_VEC_VMSUMUHM },
9092   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmsummbm", ALTIVEC_BUILTIN_VEC_VMSUMMBM },
9093   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmsumubm", ALTIVEC_BUILTIN_VEC_VMSUMUBM },
9094   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_msums", ALTIVEC_BUILTIN_VEC_MSUMS },
9095   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmsumshs", ALTIVEC_BUILTIN_VEC_VMSUMSHS },
9096   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmsumuhs", ALTIVEC_BUILTIN_VEC_VMSUMUHS },
9097   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_nmsub", ALTIVEC_BUILTIN_VEC_NMSUB },
9098   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_perm", ALTIVEC_BUILTIN_VEC_PERM },
9099   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sel", ALTIVEC_BUILTIN_VEC_SEL },
9100
9101   { MASK_VSX, CODE_FOR_vsx_fmaddv2df4, "__builtin_vsx_xvmadddp", VSX_BUILTIN_XVMADDDP },
9102   { MASK_VSX, CODE_FOR_vsx_fmsubv2df4, "__builtin_vsx_xvmsubdp", VSX_BUILTIN_XVMSUBDP },
9103   { MASK_VSX, CODE_FOR_vsx_fnmaddv2df4, "__builtin_vsx_xvnmadddp", VSX_BUILTIN_XVNMADDDP },
9104   { MASK_VSX, CODE_FOR_vsx_fnmsubv2df4, "__builtin_vsx_xvnmsubdp", VSX_BUILTIN_XVNMSUBDP },
9105
9106   { MASK_VSX, CODE_FOR_vsx_fmaddv4sf4, "__builtin_vsx_xvmaddsp", VSX_BUILTIN_XVMADDSP },
9107   { MASK_VSX, CODE_FOR_vsx_fmsubv4sf4, "__builtin_vsx_xvmsubsp", VSX_BUILTIN_XVMSUBSP },
9108   { MASK_VSX, CODE_FOR_vsx_fnmaddv4sf4, "__builtin_vsx_xvnmaddsp", VSX_BUILTIN_XVNMADDSP },
9109   { MASK_VSX, CODE_FOR_vsx_fnmsubv4sf4, "__builtin_vsx_xvnmsubsp", VSX_BUILTIN_XVNMSUBSP },
9110
9111   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_msub", VSX_BUILTIN_VEC_MSUB },
9112   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_nmadd", VSX_BUILTIN_VEC_NMADD },
9113
9114   { MASK_VSX, CODE_FOR_vector_select_v2di, "__builtin_vsx_xxsel_2di", VSX_BUILTIN_XXSEL_2DI },
9115   { MASK_VSX, CODE_FOR_vector_select_v2df, "__builtin_vsx_xxsel_2df", VSX_BUILTIN_XXSEL_2DF },
9116   { MASK_VSX, CODE_FOR_vector_select_v4sf, "__builtin_vsx_xxsel_4sf", VSX_BUILTIN_XXSEL_4SF },
9117   { MASK_VSX, CODE_FOR_vector_select_v4si, "__builtin_vsx_xxsel_4si", VSX_BUILTIN_XXSEL_4SI },
9118   { MASK_VSX, CODE_FOR_vector_select_v8hi, "__builtin_vsx_xxsel_8hi", VSX_BUILTIN_XXSEL_8HI },
9119   { MASK_VSX, CODE_FOR_vector_select_v16qi, "__builtin_vsx_xxsel_16qi", VSX_BUILTIN_XXSEL_16QI },
9120   { MASK_VSX, CODE_FOR_vector_select_v2di_uns, "__builtin_vsx_xxsel_2di_uns", VSX_BUILTIN_XXSEL_2DI_UNS },
9121   { MASK_VSX, CODE_FOR_vector_select_v4si_uns, "__builtin_vsx_xxsel_4si_uns", VSX_BUILTIN_XXSEL_4SI_UNS },
9122   { MASK_VSX, CODE_FOR_vector_select_v8hi_uns, "__builtin_vsx_xxsel_8hi_uns", VSX_BUILTIN_XXSEL_8HI_UNS },
9123   { MASK_VSX, CODE_FOR_vector_select_v16qi_uns, "__builtin_vsx_xxsel_16qi_uns", VSX_BUILTIN_XXSEL_16QI_UNS },
9124
9125   { MASK_VSX, CODE_FOR_altivec_vperm_v2di, "__builtin_vsx_vperm_2di", VSX_BUILTIN_VPERM_2DI },
9126   { MASK_VSX, CODE_FOR_altivec_vperm_v2df, "__builtin_vsx_vperm_2df", VSX_BUILTIN_VPERM_2DF },
9127   { MASK_VSX, CODE_FOR_altivec_vperm_v4sf, "__builtin_vsx_vperm_4sf", VSX_BUILTIN_VPERM_4SF },
9128   { MASK_VSX, CODE_FOR_altivec_vperm_v4si, "__builtin_vsx_vperm_4si", VSX_BUILTIN_VPERM_4SI },
9129   { MASK_VSX, CODE_FOR_altivec_vperm_v8hi, "__builtin_vsx_vperm_8hi", VSX_BUILTIN_VPERM_8HI },
9130   { MASK_VSX, CODE_FOR_altivec_vperm_v16qi, "__builtin_vsx_vperm_16qi", VSX_BUILTIN_VPERM_16QI },
9131   { MASK_VSX, CODE_FOR_altivec_vperm_v2di_uns, "__builtin_vsx_vperm_2di_uns", VSX_BUILTIN_VPERM_2DI_UNS },
9132   { MASK_VSX, CODE_FOR_altivec_vperm_v4si_uns, "__builtin_vsx_vperm_4si_uns", VSX_BUILTIN_VPERM_4SI_UNS },
9133   { MASK_VSX, CODE_FOR_altivec_vperm_v8hi_uns, "__builtin_vsx_vperm_8hi_uns", VSX_BUILTIN_VPERM_8HI_UNS },
9134   { MASK_VSX, CODE_FOR_altivec_vperm_v16qi_uns, "__builtin_vsx_vperm_16qi_uns", VSX_BUILTIN_VPERM_16QI_UNS },
9135
9136   { MASK_VSX, CODE_FOR_vsx_xxpermdi_v2df, "__builtin_vsx_xxpermdi_2df", VSX_BUILTIN_XXPERMDI_2DF },
9137   { MASK_VSX, CODE_FOR_vsx_xxpermdi_v2di, "__builtin_vsx_xxpermdi_2di", VSX_BUILTIN_XXPERMDI_2DI },
9138   { MASK_VSX, CODE_FOR_vsx_xxpermdi_v4sf, "__builtin_vsx_xxpermdi_4sf", VSX_BUILTIN_XXPERMDI_4SF },
9139   { MASK_VSX, CODE_FOR_vsx_xxpermdi_v4si, "__builtin_vsx_xxpermdi_4si", VSX_BUILTIN_XXPERMDI_4SI },
9140   { MASK_VSX, CODE_FOR_vsx_xxpermdi_v8hi, "__builtin_vsx_xxpermdi_8hi", VSX_BUILTIN_XXPERMDI_8HI },
9141   { MASK_VSX, CODE_FOR_vsx_xxpermdi_v16qi, "__builtin_vsx_xxpermdi_16qi", VSX_BUILTIN_XXPERMDI_16QI },
9142   { MASK_VSX, CODE_FOR_nothing, "__builtin_vsx_xxpermdi", VSX_BUILTIN_VEC_XXPERMDI },
9143   { MASK_VSX, CODE_FOR_vsx_set_v2df, "__builtin_vsx_set_2df", VSX_BUILTIN_SET_2DF },
9144   { MASK_VSX, CODE_FOR_vsx_set_v2di, "__builtin_vsx_set_2di", VSX_BUILTIN_SET_2DI },
9145
9146   { MASK_VSX, CODE_FOR_vsx_xxsldwi_v2di, "__builtin_vsx_xxsldwi_2di", VSX_BUILTIN_XXSLDWI_2DI },
9147   { MASK_VSX, CODE_FOR_vsx_xxsldwi_v2df, "__builtin_vsx_xxsldwi_2df", VSX_BUILTIN_XXSLDWI_2DF },
9148   { MASK_VSX, CODE_FOR_vsx_xxsldwi_v4sf, "__builtin_vsx_xxsldwi_4sf", VSX_BUILTIN_XXSLDWI_4SF },
9149   { MASK_VSX, CODE_FOR_vsx_xxsldwi_v4si, "__builtin_vsx_xxsldwi_4si", VSX_BUILTIN_XXSLDWI_4SI },
9150   { MASK_VSX, CODE_FOR_vsx_xxsldwi_v8hi, "__builtin_vsx_xxsldwi_8hi", VSX_BUILTIN_XXSLDWI_8HI },
9151   { MASK_VSX, CODE_FOR_vsx_xxsldwi_v16qi, "__builtin_vsx_xxsldwi_16qi", VSX_BUILTIN_XXSLDWI_16QI },
9152   { MASK_VSX, CODE_FOR_nothing, "__builtin_vsx_xxsldwi", VSX_BUILTIN_VEC_XXSLDWI },
9153
9154   { 0, CODE_FOR_paired_msub, "__builtin_paired_msub", PAIRED_BUILTIN_MSUB },
9155   { 0, CODE_FOR_paired_madd, "__builtin_paired_madd", PAIRED_BUILTIN_MADD },
9156   { 0, CODE_FOR_paired_madds0, "__builtin_paired_madds0", PAIRED_BUILTIN_MADDS0 },
9157   { 0, CODE_FOR_paired_madds1, "__builtin_paired_madds1", PAIRED_BUILTIN_MADDS1 },
9158   { 0, CODE_FOR_paired_nmsub, "__builtin_paired_nmsub", PAIRED_BUILTIN_NMSUB },
9159   { 0, CODE_FOR_paired_nmadd, "__builtin_paired_nmadd", PAIRED_BUILTIN_NMADD },
9160   { 0, CODE_FOR_paired_sum0, "__builtin_paired_sum0", PAIRED_BUILTIN_SUM0 },
9161   { 0, CODE_FOR_paired_sum1, "__builtin_paired_sum1", PAIRED_BUILTIN_SUM1 },
9162   { 0, CODE_FOR_selv2sf4, "__builtin_paired_selv2sf4", PAIRED_BUILTIN_SELV2SF4 },
9163 };
9164
9165 /* DST operations: void foo (void *, const int, const char).  */
9166
9167 static const struct builtin_description bdesc_dst[] =
9168 {
9169   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
9170   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
9171   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
9172   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT },
9173
9174   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_dst", ALTIVEC_BUILTIN_VEC_DST },
9175   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_dstt", ALTIVEC_BUILTIN_VEC_DSTT },
9176   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_dstst", ALTIVEC_BUILTIN_VEC_DSTST },
9177   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_dststt", ALTIVEC_BUILTIN_VEC_DSTSTT }
9178 };
9179
9180 /* Simple binary operations: VECc = foo (VECa, VECb).  */
9181
9182 static struct builtin_description bdesc_2arg[] =
9183 {
9184   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
9185   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
9186   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
9187   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
9188   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
9189   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
9190   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
9191   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
9192   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
9193   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
9194   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
9195   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
9196   { MASK_ALTIVEC, CODE_FOR_andcv4si3, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
9197   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
9198   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
9199   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
9200   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
9201   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
9202   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
9203   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
9204   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
9205   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
9206   { MASK_ALTIVEC, CODE_FOR_vector_eqv16qi, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
9207   { MASK_ALTIVEC, CODE_FOR_vector_eqv8hi, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
9208   { MASK_ALTIVEC, CODE_FOR_vector_eqv4si, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
9209   { MASK_ALTIVEC, CODE_FOR_vector_eqv4sf, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
9210   { MASK_ALTIVEC, CODE_FOR_vector_gev4sf, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
9211   { MASK_ALTIVEC, CODE_FOR_vector_gtuv16qi, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
9212   { MASK_ALTIVEC, CODE_FOR_vector_gtv16qi, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
9213   { MASK_ALTIVEC, CODE_FOR_vector_gtuv8hi, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
9214   { MASK_ALTIVEC, CODE_FOR_vector_gtv8hi, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
9215   { MASK_ALTIVEC, CODE_FOR_vector_gtuv4si, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
9216   { MASK_ALTIVEC, CODE_FOR_vector_gtv4si, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
9217   { MASK_ALTIVEC, CODE_FOR_vector_gtv4sf, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
9218   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
9219   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
9220   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
9221   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
9222   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
9223   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
9224   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
9225   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
9226   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
9227   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
9228   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
9229   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
9230   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
9231   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
9232   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
9233   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
9234   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
9235   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
9236   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
9237   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
9238   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
9239   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
9240   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
9241   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub_uns", ALTIVEC_BUILTIN_VMULEUB_UNS },
9242   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
9243   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
9244   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh_uns", ALTIVEC_BUILTIN_VMULEUH_UNS },
9245   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
9246   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
9247   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub_uns", ALTIVEC_BUILTIN_VMULOUB_UNS },
9248   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
9249   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
9250   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh_uns", ALTIVEC_BUILTIN_VMULOUH_UNS },
9251   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
9252   { MASK_ALTIVEC, CODE_FOR_norv4si3, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
9253   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
9254   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
9255   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
9256   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
9257   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
9258   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
9259   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
9260   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
9261   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
9262   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
9263   { MASK_ALTIVEC, CODE_FOR_recipv4sf3, "__builtin_altivec_vrecipdivfp", ALTIVEC_BUILTIN_VRECIPFP },
9264   { MASK_ALTIVEC, CODE_FOR_vrotlv16qi3, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
9265   { MASK_ALTIVEC, CODE_FOR_vrotlv8hi3, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
9266   { MASK_ALTIVEC, CODE_FOR_vrotlv4si3, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
9267   { MASK_ALTIVEC, CODE_FOR_vashlv16qi3, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
9268   { MASK_ALTIVEC, CODE_FOR_vashlv8hi3, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
9269   { MASK_ALTIVEC, CODE_FOR_vashlv4si3, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
9270   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
9271   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
9272   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
9273   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
9274   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
9275   { MASK_ALTIVEC, CODE_FOR_vlshrv16qi3, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
9276   { MASK_ALTIVEC, CODE_FOR_vlshrv8hi3, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
9277   { MASK_ALTIVEC, CODE_FOR_vlshrv4si3, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
9278   { MASK_ALTIVEC, CODE_FOR_vashrv16qi3, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
9279   { MASK_ALTIVEC, CODE_FOR_vashrv8hi3, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
9280   { MASK_ALTIVEC, CODE_FOR_vashrv4si3, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
9281   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
9282   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
9283   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
9284   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
9285   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
9286   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
9287   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
9288   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
9289   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
9290   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
9291   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
9292   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
9293   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
9294   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
9295   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
9296   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
9297   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
9298   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
9299   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
9300   { MASK_ALTIVEC, CODE_FOR_vector_copysignv4sf3, "__builtin_altivec_copysignfp", ALTIVEC_BUILTIN_COPYSIGN_V4SF },
9301
9302   { MASK_VSX, CODE_FOR_addv2df3, "__builtin_vsx_xvadddp", VSX_BUILTIN_XVADDDP },
9303   { MASK_VSX, CODE_FOR_subv2df3, "__builtin_vsx_xvsubdp", VSX_BUILTIN_XVSUBDP },
9304   { MASK_VSX, CODE_FOR_mulv2df3, "__builtin_vsx_xvmuldp", VSX_BUILTIN_XVMULDP },
9305   { MASK_VSX, CODE_FOR_divv2df3, "__builtin_vsx_xvdivdp", VSX_BUILTIN_XVDIVDP },
9306   { MASK_VSX, CODE_FOR_recipv2df3, "__builtin_vsx_xvrecipdivdp", VSX_BUILTIN_RECIP_V2DF },
9307   { MASK_VSX, CODE_FOR_sminv2df3, "__builtin_vsx_xvmindp", VSX_BUILTIN_XVMINDP },
9308   { MASK_VSX, CODE_FOR_smaxv2df3, "__builtin_vsx_xvmaxdp", VSX_BUILTIN_XVMAXDP },
9309   { MASK_VSX, CODE_FOR_vsx_tdivv2df3_fe, "__builtin_vsx_xvtdivdp_fe", VSX_BUILTIN_XVTDIVDP_FE },
9310   { MASK_VSX, CODE_FOR_vsx_tdivv2df3_fg, "__builtin_vsx_xvtdivdp_fg", VSX_BUILTIN_XVTDIVDP_FG },
9311   { MASK_VSX, CODE_FOR_vector_eqv2df, "__builtin_vsx_xvcmpeqdp", VSX_BUILTIN_XVCMPEQDP },
9312   { MASK_VSX, CODE_FOR_vector_gtv2df, "__builtin_vsx_xvcmpgtdp", VSX_BUILTIN_XVCMPGTDP },
9313   { MASK_VSX, CODE_FOR_vector_gev2df, "__builtin_vsx_xvcmpgedp", VSX_BUILTIN_XVCMPGEDP },
9314
9315   { MASK_VSX, CODE_FOR_addv4sf3, "__builtin_vsx_xvaddsp", VSX_BUILTIN_XVADDSP },
9316   { MASK_VSX, CODE_FOR_subv4sf3, "__builtin_vsx_xvsubsp", VSX_BUILTIN_XVSUBSP },
9317   { MASK_VSX, CODE_FOR_mulv4sf3, "__builtin_vsx_xvmulsp", VSX_BUILTIN_XVMULSP },
9318   { MASK_VSX, CODE_FOR_divv4sf3, "__builtin_vsx_xvdivsp", VSX_BUILTIN_XVDIVSP },
9319   { MASK_VSX, CODE_FOR_recipv4sf3, "__builtin_vsx_xvrecipdivsp", VSX_BUILTIN_RECIP_V4SF },
9320   { MASK_VSX, CODE_FOR_sminv4sf3, "__builtin_vsx_xvminsp", VSX_BUILTIN_XVMINSP },
9321   { MASK_VSX, CODE_FOR_smaxv4sf3, "__builtin_vsx_xvmaxsp", VSX_BUILTIN_XVMAXSP },
9322   { MASK_VSX, CODE_FOR_vsx_tdivv4sf3_fe, "__builtin_vsx_xvtdivsp_fe", VSX_BUILTIN_XVTDIVSP_FE },
9323   { MASK_VSX, CODE_FOR_vsx_tdivv4sf3_fg, "__builtin_vsx_xvtdivsp_fg", VSX_BUILTIN_XVTDIVSP_FG },
9324   { MASK_VSX, CODE_FOR_vector_eqv4sf, "__builtin_vsx_xvcmpeqsp", VSX_BUILTIN_XVCMPEQSP },
9325   { MASK_VSX, CODE_FOR_vector_gtv4sf, "__builtin_vsx_xvcmpgtsp", VSX_BUILTIN_XVCMPGTSP },
9326   { MASK_VSX, CODE_FOR_vector_gev4sf, "__builtin_vsx_xvcmpgesp", VSX_BUILTIN_XVCMPGESP },
9327
9328   { MASK_VSX, CODE_FOR_smindf3, "__builtin_vsx_xsmindp", VSX_BUILTIN_XSMINDP },
9329   { MASK_VSX, CODE_FOR_smaxdf3, "__builtin_vsx_xsmaxdp", VSX_BUILTIN_XSMAXDP },
9330   { MASK_VSX, CODE_FOR_vsx_tdivdf3_fe, "__builtin_vsx_xstdivdp_fe", VSX_BUILTIN_XSTDIVDP_FE },
9331   { MASK_VSX, CODE_FOR_vsx_tdivdf3_fg, "__builtin_vsx_xstdivdp_fg", VSX_BUILTIN_XSTDIVDP_FG },
9332   { MASK_VSX, CODE_FOR_vector_copysignv2df3, "__builtin_vsx_cpsgndp", VSX_BUILTIN_CPSGNDP },
9333   { MASK_VSX, CODE_FOR_vector_copysignv4sf3, "__builtin_vsx_cpsgnsp", VSX_BUILTIN_CPSGNSP },
9334
9335   { MASK_VSX, CODE_FOR_vsx_concat_v2df, "__builtin_vsx_concat_2df", VSX_BUILTIN_CONCAT_2DF },
9336   { MASK_VSX, CODE_FOR_vsx_concat_v2di, "__builtin_vsx_concat_2di", VSX_BUILTIN_CONCAT_2DI },
9337   { MASK_VSX, CODE_FOR_vsx_splat_v2df, "__builtin_vsx_splat_2df", VSX_BUILTIN_SPLAT_2DF },
9338   { MASK_VSX, CODE_FOR_vsx_splat_v2di, "__builtin_vsx_splat_2di", VSX_BUILTIN_SPLAT_2DI },
9339   { MASK_VSX, CODE_FOR_vsx_xxmrghw_v4sf, "__builtin_vsx_xxmrghw", VSX_BUILTIN_XXMRGHW_4SF },
9340   { MASK_VSX, CODE_FOR_vsx_xxmrghw_v4si, "__builtin_vsx_xxmrghw_4si", VSX_BUILTIN_XXMRGHW_4SI },
9341   { MASK_VSX, CODE_FOR_vsx_xxmrglw_v4sf, "__builtin_vsx_xxmrglw", VSX_BUILTIN_XXMRGLW_4SF },
9342   { MASK_VSX, CODE_FOR_vsx_xxmrglw_v4si, "__builtin_vsx_xxmrglw_4si", VSX_BUILTIN_XXMRGLW_4SI },
9343   { MASK_VSX, CODE_FOR_vec_interleave_lowv2df, "__builtin_vsx_mergel_2df", VSX_BUILTIN_VEC_MERGEL_V2DF },
9344   { MASK_VSX, CODE_FOR_vec_interleave_lowv2di, "__builtin_vsx_mergel_2di", VSX_BUILTIN_VEC_MERGEL_V2DI },
9345   { MASK_VSX, CODE_FOR_vec_interleave_highv2df, "__builtin_vsx_mergeh_2df", VSX_BUILTIN_VEC_MERGEH_V2DF },
9346   { MASK_VSX, CODE_FOR_vec_interleave_highv2di, "__builtin_vsx_mergeh_2di", VSX_BUILTIN_VEC_MERGEH_V2DI },
9347
9348   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_add", ALTIVEC_BUILTIN_VEC_ADD },
9349   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vaddfp", ALTIVEC_BUILTIN_VEC_VADDFP },
9350   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vadduwm", ALTIVEC_BUILTIN_VEC_VADDUWM },
9351   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vadduhm", ALTIVEC_BUILTIN_VEC_VADDUHM },
9352   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vaddubm", ALTIVEC_BUILTIN_VEC_VADDUBM },
9353   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_addc", ALTIVEC_BUILTIN_VEC_ADDC },
9354   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_adds", ALTIVEC_BUILTIN_VEC_ADDS },
9355   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vaddsws", ALTIVEC_BUILTIN_VEC_VADDSWS },
9356   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vadduws", ALTIVEC_BUILTIN_VEC_VADDUWS },
9357   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vaddshs", ALTIVEC_BUILTIN_VEC_VADDSHS },
9358   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vadduhs", ALTIVEC_BUILTIN_VEC_VADDUHS },
9359   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vaddsbs", ALTIVEC_BUILTIN_VEC_VADDSBS },
9360   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vaddubs", ALTIVEC_BUILTIN_VEC_VADDUBS },
9361   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_and", ALTIVEC_BUILTIN_VEC_AND },
9362   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_andc", ALTIVEC_BUILTIN_VEC_ANDC },
9363   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_avg", ALTIVEC_BUILTIN_VEC_AVG },
9364   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vavgsw", ALTIVEC_BUILTIN_VEC_VAVGSW },
9365   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vavguw", ALTIVEC_BUILTIN_VEC_VAVGUW },
9366   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vavgsh", ALTIVEC_BUILTIN_VEC_VAVGSH },
9367   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vavguh", ALTIVEC_BUILTIN_VEC_VAVGUH },
9368   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vavgsb", ALTIVEC_BUILTIN_VEC_VAVGSB },
9369   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vavgub", ALTIVEC_BUILTIN_VEC_VAVGUB },
9370   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_cmpb", ALTIVEC_BUILTIN_VEC_CMPB },
9371   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_cmpeq", ALTIVEC_BUILTIN_VEC_CMPEQ },
9372   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpeqfp", ALTIVEC_BUILTIN_VEC_VCMPEQFP },
9373   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpequw", ALTIVEC_BUILTIN_VEC_VCMPEQUW },
9374   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpequh", ALTIVEC_BUILTIN_VEC_VCMPEQUH },
9375   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpequb", ALTIVEC_BUILTIN_VEC_VCMPEQUB },
9376   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_cmpge", ALTIVEC_BUILTIN_VEC_CMPGE },
9377   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_cmpgt", ALTIVEC_BUILTIN_VEC_CMPGT },
9378   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtfp", ALTIVEC_BUILTIN_VEC_VCMPGTFP },
9379   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtsw", ALTIVEC_BUILTIN_VEC_VCMPGTSW },
9380   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtuw", ALTIVEC_BUILTIN_VEC_VCMPGTUW },
9381   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtsh", ALTIVEC_BUILTIN_VEC_VCMPGTSH },
9382   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtuh", ALTIVEC_BUILTIN_VEC_VCMPGTUH },
9383   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtsb", ALTIVEC_BUILTIN_VEC_VCMPGTSB },
9384   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vcmpgtub", ALTIVEC_BUILTIN_VEC_VCMPGTUB },
9385   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_cmple", ALTIVEC_BUILTIN_VEC_CMPLE },
9386   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_cmplt", ALTIVEC_BUILTIN_VEC_CMPLT },
9387   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_copysign", ALTIVEC_BUILTIN_VEC_COPYSIGN },
9388   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_max", ALTIVEC_BUILTIN_VEC_MAX },
9389   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vmaxfp", ALTIVEC_BUILTIN_VEC_VMAXFP },
9390   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmaxsw", ALTIVEC_BUILTIN_VEC_VMAXSW },
9391   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmaxuw", ALTIVEC_BUILTIN_VEC_VMAXUW },
9392   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmaxsh", ALTIVEC_BUILTIN_VEC_VMAXSH },
9393   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmaxuh", ALTIVEC_BUILTIN_VEC_VMAXUH },
9394   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmaxsb", ALTIVEC_BUILTIN_VEC_VMAXSB },
9395   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmaxub", ALTIVEC_BUILTIN_VEC_VMAXUB },
9396   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mergeh", ALTIVEC_BUILTIN_VEC_MERGEH },
9397   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmrghw", ALTIVEC_BUILTIN_VEC_VMRGHW },
9398   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmrghh", ALTIVEC_BUILTIN_VEC_VMRGHH },
9399   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmrghb", ALTIVEC_BUILTIN_VEC_VMRGHB },
9400   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mergel", ALTIVEC_BUILTIN_VEC_MERGEL },
9401   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmrglw", ALTIVEC_BUILTIN_VEC_VMRGLW },
9402   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmrglh", ALTIVEC_BUILTIN_VEC_VMRGLH },
9403   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmrglb", ALTIVEC_BUILTIN_VEC_VMRGLB },
9404   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_min", ALTIVEC_BUILTIN_VEC_MIN },
9405   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vminfp", ALTIVEC_BUILTIN_VEC_VMINFP },
9406   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vminsw", ALTIVEC_BUILTIN_VEC_VMINSW },
9407   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vminuw", ALTIVEC_BUILTIN_VEC_VMINUW },
9408   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vminsh", ALTIVEC_BUILTIN_VEC_VMINSH },
9409   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vminuh", ALTIVEC_BUILTIN_VEC_VMINUH },
9410   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vminsb", ALTIVEC_BUILTIN_VEC_VMINSB },
9411   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vminub", ALTIVEC_BUILTIN_VEC_VMINUB },
9412   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mule", ALTIVEC_BUILTIN_VEC_MULE },
9413   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmuleub", ALTIVEC_BUILTIN_VEC_VMULEUB },
9414   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmulesb", ALTIVEC_BUILTIN_VEC_VMULESB },
9415   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmuleuh", ALTIVEC_BUILTIN_VEC_VMULEUH },
9416   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmulesh", ALTIVEC_BUILTIN_VEC_VMULESH },
9417   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mulo", ALTIVEC_BUILTIN_VEC_MULO },
9418   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmulosh", ALTIVEC_BUILTIN_VEC_VMULOSH },
9419   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmulouh", ALTIVEC_BUILTIN_VEC_VMULOUH },
9420   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmulosb", ALTIVEC_BUILTIN_VEC_VMULOSB },
9421   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vmuloub", ALTIVEC_BUILTIN_VEC_VMULOUB },
9422   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_nor", ALTIVEC_BUILTIN_VEC_NOR },
9423   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_or", ALTIVEC_BUILTIN_VEC_OR },
9424   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_pack", ALTIVEC_BUILTIN_VEC_PACK },
9425   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkuwum", ALTIVEC_BUILTIN_VEC_VPKUWUM },
9426   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkuhum", ALTIVEC_BUILTIN_VEC_VPKUHUM },
9427   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_packpx", ALTIVEC_BUILTIN_VEC_PACKPX },
9428   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_packs", ALTIVEC_BUILTIN_VEC_PACKS },
9429   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkswss", ALTIVEC_BUILTIN_VEC_VPKSWSS },
9430   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkuwus", ALTIVEC_BUILTIN_VEC_VPKUWUS },
9431   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkshss", ALTIVEC_BUILTIN_VEC_VPKSHSS },
9432   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkuhus", ALTIVEC_BUILTIN_VEC_VPKUHUS },
9433   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_packsu", ALTIVEC_BUILTIN_VEC_PACKSU },
9434   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkswus", ALTIVEC_BUILTIN_VEC_VPKSWUS },
9435   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vpkshus", ALTIVEC_BUILTIN_VEC_VPKSHUS },
9436   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_recipdiv", ALTIVEC_BUILTIN_VEC_RECIP },
9437   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_rl", ALTIVEC_BUILTIN_VEC_RL },
9438   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vrlw", ALTIVEC_BUILTIN_VEC_VRLW },
9439   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vrlh", ALTIVEC_BUILTIN_VEC_VRLH },
9440   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vrlb", ALTIVEC_BUILTIN_VEC_VRLB },
9441   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sl", ALTIVEC_BUILTIN_VEC_SL },
9442   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vslw", ALTIVEC_BUILTIN_VEC_VSLW },
9443   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vslh", ALTIVEC_BUILTIN_VEC_VSLH },
9444   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vslb", ALTIVEC_BUILTIN_VEC_VSLB },
9445   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sll", ALTIVEC_BUILTIN_VEC_SLL },
9446   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_slo", ALTIVEC_BUILTIN_VEC_SLO },
9447   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sr", ALTIVEC_BUILTIN_VEC_SR },
9448   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsrw", ALTIVEC_BUILTIN_VEC_VSRW },
9449   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsrh", ALTIVEC_BUILTIN_VEC_VSRH },
9450   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsrb", ALTIVEC_BUILTIN_VEC_VSRB },
9451   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sra", ALTIVEC_BUILTIN_VEC_SRA },
9452   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsraw", ALTIVEC_BUILTIN_VEC_VSRAW },
9453   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsrah", ALTIVEC_BUILTIN_VEC_VSRAH },
9454   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsrab", ALTIVEC_BUILTIN_VEC_VSRAB },
9455   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_srl", ALTIVEC_BUILTIN_VEC_SRL },
9456   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sro", ALTIVEC_BUILTIN_VEC_SRO },
9457   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_sub", ALTIVEC_BUILTIN_VEC_SUB },
9458   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vsubfp", ALTIVEC_BUILTIN_VEC_VSUBFP },
9459   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubuwm", ALTIVEC_BUILTIN_VEC_VSUBUWM },
9460   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubuhm", ALTIVEC_BUILTIN_VEC_VSUBUHM },
9461   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsububm", ALTIVEC_BUILTIN_VEC_VSUBUBM },
9462   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_subc", ALTIVEC_BUILTIN_VEC_SUBC },
9463   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_subs", ALTIVEC_BUILTIN_VEC_SUBS },
9464   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubsws", ALTIVEC_BUILTIN_VEC_VSUBSWS },
9465   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubuws", ALTIVEC_BUILTIN_VEC_VSUBUWS },
9466   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubshs", ALTIVEC_BUILTIN_VEC_VSUBSHS },
9467   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubuhs", ALTIVEC_BUILTIN_VEC_VSUBUHS },
9468   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsubsbs", ALTIVEC_BUILTIN_VEC_VSUBSBS },
9469   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsububs", ALTIVEC_BUILTIN_VEC_VSUBUBS },
9470   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sum4s", ALTIVEC_BUILTIN_VEC_SUM4S },
9471   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsum4shs", ALTIVEC_BUILTIN_VEC_VSUM4SHS },
9472   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsum4sbs", ALTIVEC_BUILTIN_VEC_VSUM4SBS },
9473   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vsum4ubs", ALTIVEC_BUILTIN_VEC_VSUM4UBS },
9474   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sum2s", ALTIVEC_BUILTIN_VEC_SUM2S },
9475   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_sums", ALTIVEC_BUILTIN_VEC_SUMS },
9476   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_xor", ALTIVEC_BUILTIN_VEC_XOR },
9477
9478   { MASK_VSX, CODE_FOR_nothing, "__builtin_vec_mul", VSX_BUILTIN_VEC_MUL },
9479   { MASK_VSX, CODE_FOR_nothing, "__builtin_vec_div", VSX_BUILTIN_VEC_DIV },
9480
9481   { 0, CODE_FOR_paired_divv2sf3, "__builtin_paired_divv2sf3", PAIRED_BUILTIN_DIVV2SF3 },
9482   { 0, CODE_FOR_paired_addv2sf3, "__builtin_paired_addv2sf3", PAIRED_BUILTIN_ADDV2SF3 },
9483   { 0, CODE_FOR_paired_subv2sf3, "__builtin_paired_subv2sf3", PAIRED_BUILTIN_SUBV2SF3 },
9484   { 0, CODE_FOR_paired_mulv2sf3, "__builtin_paired_mulv2sf3", PAIRED_BUILTIN_MULV2SF3 },
9485   { 0, CODE_FOR_paired_muls0, "__builtin_paired_muls0", PAIRED_BUILTIN_MULS0 },
9486   { 0, CODE_FOR_paired_muls1, "__builtin_paired_muls1", PAIRED_BUILTIN_MULS1 },
9487   { 0, CODE_FOR_paired_merge00, "__builtin_paired_merge00", PAIRED_BUILTIN_MERGE00 },
9488   { 0, CODE_FOR_paired_merge01, "__builtin_paired_merge01", PAIRED_BUILTIN_MERGE01 },
9489   { 0, CODE_FOR_paired_merge10, "__builtin_paired_merge10", PAIRED_BUILTIN_MERGE10 },
9490   { 0, CODE_FOR_paired_merge11, "__builtin_paired_merge11", PAIRED_BUILTIN_MERGE11 },
9491
9492   /* Place holder, leave as first spe builtin.  */
9493   { 0, CODE_FOR_addv2si3, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
9494   { 0, CODE_FOR_andv2si3, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
9495   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
9496   { 0, CODE_FOR_divv2si3, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
9497   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
9498   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
9499   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
9500   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
9501   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
9502   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
9503   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
9504   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
9505   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
9506   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
9507   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
9508   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
9509   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
9510   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
9511   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
9512   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
9513   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
9514   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
9515   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
9516   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
9517   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
9518   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
9519   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
9520   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
9521   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
9522   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
9523   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
9524   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
9525   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
9526   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
9527   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
9528   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
9529   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
9530   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
9531   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
9532   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
9533   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
9534   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
9535   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
9536   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
9537   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
9538   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
9539   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
9540   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
9541   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
9542   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
9543   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
9544   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
9545   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
9546   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
9547   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
9548   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
9549   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
9550   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
9551   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
9552   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
9553   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
9554   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
9555   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
9556   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
9557   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
9558   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
9559   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
9560   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
9561   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
9562   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
9563   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
9564   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
9565   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
9566   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
9567   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
9568   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
9569   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
9570   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
9571   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
9572   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
9573   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
9574   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
9575   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
9576   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
9577   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
9578   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
9579   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
9580   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
9581   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
9582   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
9583   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
9584   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
9585   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
9586   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
9587   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
9588   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
9589   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
9590   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
9591   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
9592   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
9593   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
9594   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
9595   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
9596   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
9597   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
9598   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
9599   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
9600   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
9601   { 0, CODE_FOR_subv2si3, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
9602
9603   /* SPE binary operations expecting a 5-bit unsigned literal.  */
9604   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
9605
9606   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
9607   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
9608   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
9609   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
9610   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
9611   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
9612   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
9613   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
9614   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
9615   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
9616   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
9617   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
9618   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
9619   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
9620   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
9621   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
9622   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
9623   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
9624   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
9625   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
9626   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
9627   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
9628   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
9629   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
9630   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
9631   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
9632
9633   /* Place-holder.  Leave as last binary SPE builtin.  */
9634   { 0, CODE_FOR_xorv2si3, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR }
9635 };
9636
9637 /* AltiVec predicates.  */
9638
9639 struct builtin_description_predicates
9640 {
9641   const unsigned int mask;
9642   const enum insn_code icode;
9643   const char *const name;
9644   const enum rs6000_builtins code;
9645 };
9646
9647 static const struct builtin_description_predicates bdesc_altivec_preds[] =
9648 {
9649   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp_p, "__builtin_altivec_vcmpbfp_p",
9650     ALTIVEC_BUILTIN_VCMPBFP_P },
9651   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_vector_eq_v4sf_p,
9652     "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
9653   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_vector_ge_v4sf_p,
9654     "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
9655   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_vector_gt_v4sf_p,
9656     "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
9657   { MASK_ALTIVEC, CODE_FOR_vector_eq_v4si_p, "__builtin_altivec_vcmpequw_p",
9658     ALTIVEC_BUILTIN_VCMPEQUW_P },
9659   { MASK_ALTIVEC, CODE_FOR_vector_gt_v4si_p, "__builtin_altivec_vcmpgtsw_p",
9660     ALTIVEC_BUILTIN_VCMPGTSW_P },
9661   { MASK_ALTIVEC, CODE_FOR_vector_gtu_v4si_p, "__builtin_altivec_vcmpgtuw_p",
9662     ALTIVEC_BUILTIN_VCMPGTUW_P },
9663   { MASK_ALTIVEC, CODE_FOR_vector_eq_v8hi_p, "__builtin_altivec_vcmpequh_p",
9664     ALTIVEC_BUILTIN_VCMPEQUH_P },
9665   { MASK_ALTIVEC, CODE_FOR_vector_gt_v8hi_p, "__builtin_altivec_vcmpgtsh_p",
9666     ALTIVEC_BUILTIN_VCMPGTSH_P },
9667   { MASK_ALTIVEC, CODE_FOR_vector_gtu_v8hi_p, "__builtin_altivec_vcmpgtuh_p",
9668     ALTIVEC_BUILTIN_VCMPGTUH_P },
9669   { MASK_ALTIVEC, CODE_FOR_vector_eq_v16qi_p, "__builtin_altivec_vcmpequb_p",
9670     ALTIVEC_BUILTIN_VCMPEQUB_P },
9671   { MASK_ALTIVEC, CODE_FOR_vector_gt_v16qi_p, "__builtin_altivec_vcmpgtsb_p",
9672     ALTIVEC_BUILTIN_VCMPGTSB_P },
9673   { MASK_ALTIVEC, CODE_FOR_vector_gtu_v16qi_p, "__builtin_altivec_vcmpgtub_p",
9674     ALTIVEC_BUILTIN_VCMPGTUB_P },
9675
9676   { MASK_VSX, CODE_FOR_vector_eq_v4sf_p, "__builtin_vsx_xvcmpeqsp_p",
9677     VSX_BUILTIN_XVCMPEQSP_P },
9678   { MASK_VSX, CODE_FOR_vector_ge_v4sf_p, "__builtin_vsx_xvcmpgesp_p",
9679     VSX_BUILTIN_XVCMPGESP_P },
9680   { MASK_VSX, CODE_FOR_vector_gt_v4sf_p, "__builtin_vsx_xvcmpgtsp_p",
9681     VSX_BUILTIN_XVCMPGTSP_P },
9682   { MASK_VSX, CODE_FOR_vector_eq_v2df_p, "__builtin_vsx_xvcmpeqdp_p",
9683     VSX_BUILTIN_XVCMPEQDP_P },
9684   { MASK_VSX, CODE_FOR_vector_ge_v2df_p, "__builtin_vsx_xvcmpgedp_p",
9685     VSX_BUILTIN_XVCMPGEDP_P },
9686   { MASK_VSX, CODE_FOR_vector_gt_v2df_p, "__builtin_vsx_xvcmpgtdp_p",
9687     VSX_BUILTIN_XVCMPGTDP_P },
9688
9689   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vcmpeq_p",
9690     ALTIVEC_BUILTIN_VCMPEQ_P },
9691   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vcmpgt_p",
9692     ALTIVEC_BUILTIN_VCMPGT_P },
9693   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_nothing, "__builtin_vec_vcmpge_p",
9694     ALTIVEC_BUILTIN_VCMPGE_P }
9695 };
9696
9697 /* SPE predicates.  */
9698 static struct builtin_description bdesc_spe_predicates[] =
9699 {
9700   /* Place-holder.  Leave as first.  */
9701   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
9702   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
9703   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
9704   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
9705   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
9706   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
9707   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
9708   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
9709   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
9710   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
9711   /* Place-holder.  Leave as last.  */
9712   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
9713 };
9714
9715 /* SPE evsel predicates.  */
9716 static struct builtin_description bdesc_spe_evsel[] =
9717 {
9718   /* Place-holder.  Leave as first.  */
9719   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
9720   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
9721   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
9722   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
9723   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
9724   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
9725   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
9726   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
9727   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
9728   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
9729   /* Place-holder.  Leave as last.  */
9730   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
9731 };
9732
9733 /* PAIRED predicates.  */
9734 static const struct builtin_description bdesc_paired_preds[] =
9735 {
9736   /* Place-holder.  Leave as first.  */
9737   { 0, CODE_FOR_paired_cmpu0, "__builtin_paired_cmpu0", PAIRED_BUILTIN_CMPU0 },
9738   /* Place-holder.  Leave as last.  */
9739   { 0, CODE_FOR_paired_cmpu1, "__builtin_paired_cmpu1", PAIRED_BUILTIN_CMPU1 },
9740 };
9741
9742 /* ABS* operations.  */
9743
9744 static const struct builtin_description bdesc_abs[] =
9745 {
9746   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
9747   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
9748   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
9749   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
9750   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
9751   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
9752   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI },
9753   { MASK_VSX, CODE_FOR_absv2df2, "__builtin_vsx_xvabsdp", VSX_BUILTIN_XVABSDP },
9754   { MASK_VSX, CODE_FOR_vsx_nabsv2df2, "__builtin_vsx_xvnabsdp", VSX_BUILTIN_XVNABSDP },
9755   { MASK_VSX, CODE_FOR_absv4sf2, "__builtin_vsx_xvabssp", VSX_BUILTIN_XVABSSP },
9756   { MASK_VSX, CODE_FOR_vsx_nabsv4sf2, "__builtin_vsx_xvnabssp", VSX_BUILTIN_XVNABSSP },
9757 };
9758
9759 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
9760    foo (VECa).  */
9761
9762 static struct builtin_description bdesc_1arg[] =
9763 {
9764   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
9765   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
9766   { MASK_ALTIVEC, CODE_FOR_rev4sf2, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
9767   { MASK_ALTIVEC, CODE_FOR_vector_floorv4sf2, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
9768   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
9769   { MASK_ALTIVEC, CODE_FOR_vector_ceilv4sf2, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
9770   { MASK_ALTIVEC, CODE_FOR_vector_btruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
9771   { MASK_ALTIVEC, CODE_FOR_rsqrtv4sf2, "__builtin_altivec_vrsqrtfp", ALTIVEC_BUILTIN_VRSQRTFP },
9772   { MASK_ALTIVEC, CODE_FOR_rsqrtev4sf2, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
9773   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
9774   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
9775   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
9776   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
9777   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
9778   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
9779   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
9780   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
9781   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
9782
9783   { MASK_VSX, CODE_FOR_negv2df2, "__builtin_vsx_xvnegdp", VSX_BUILTIN_XVNEGDP },
9784   { MASK_VSX, CODE_FOR_sqrtv2df2, "__builtin_vsx_xvsqrtdp", VSX_BUILTIN_XVSQRTDP },
9785   { MASK_VSX, CODE_FOR_rsqrtv2df2, "__builtin_vsx_xvrsqrtdp", VSX_BUILTIN_VEC_RSQRT_V2DF },
9786   { MASK_VSX, CODE_FOR_rsqrtev2df2, "__builtin_vsx_xvrsqrtedp", VSX_BUILTIN_XVRSQRTEDP },
9787   { MASK_VSX, CODE_FOR_vsx_tsqrtv2df2_fe, "__builtin_vsx_xvtsqrtdp_fe", VSX_BUILTIN_XVTSQRTDP_FE },
9788   { MASK_VSX, CODE_FOR_vsx_tsqrtv2df2_fg, "__builtin_vsx_xvtsqrtdp_fg", VSX_BUILTIN_XVTSQRTDP_FG },
9789   { MASK_VSX, CODE_FOR_vsx_frev2df2, "__builtin_vsx_xvredp", VSX_BUILTIN_XVREDP },
9790
9791   { MASK_VSX, CODE_FOR_negv4sf2, "__builtin_vsx_xvnegsp", VSX_BUILTIN_XVNEGSP },
9792   { MASK_VSX, CODE_FOR_sqrtv4sf2, "__builtin_vsx_xvsqrtsp", VSX_BUILTIN_XVSQRTSP },
9793   { MASK_VSX, CODE_FOR_rsqrtv4sf2, "__builtin_vsx_xvrsqrtsp", VSX_BUILTIN_VEC_RSQRT_V4SF },
9794   { MASK_VSX, CODE_FOR_rsqrtev4sf2, "__builtin_vsx_xvrsqrtesp", VSX_BUILTIN_XVRSQRTESP },
9795   { MASK_VSX, CODE_FOR_vsx_tsqrtv4sf2_fe, "__builtin_vsx_xvtsqrtsp_fe", VSX_BUILTIN_XVTSQRTSP_FE },
9796   { MASK_VSX, CODE_FOR_vsx_tsqrtv4sf2_fg, "__builtin_vsx_xvtsqrtsp_fg", VSX_BUILTIN_XVTSQRTSP_FG },
9797   { MASK_VSX, CODE_FOR_vsx_frev4sf2, "__builtin_vsx_xvresp", VSX_BUILTIN_XVRESP },
9798
9799   { MASK_VSX, CODE_FOR_vsx_xscvdpsp, "__builtin_vsx_xscvdpsp", VSX_BUILTIN_XSCVDPSP },
9800   { MASK_VSX, CODE_FOR_vsx_xscvdpsp, "__builtin_vsx_xscvspdp", VSX_BUILTIN_XSCVSPDP },
9801   { MASK_VSX, CODE_FOR_vsx_xvcvdpsp, "__builtin_vsx_xvcvdpsp", VSX_BUILTIN_XVCVDPSP },
9802   { MASK_VSX, CODE_FOR_vsx_xvcvspdp, "__builtin_vsx_xvcvspdp", VSX_BUILTIN_XVCVSPDP },
9803   { MASK_VSX, CODE_FOR_vsx_tsqrtdf2_fe, "__builtin_vsx_xstsqrtdp_fe", VSX_BUILTIN_XSTSQRTDP_FE },
9804   { MASK_VSX, CODE_FOR_vsx_tsqrtdf2_fg, "__builtin_vsx_xstsqrtdp_fg", VSX_BUILTIN_XSTSQRTDP_FG },
9805
9806   { MASK_VSX, CODE_FOR_vsx_fix_truncv2dfv2di2, "__builtin_vsx_xvcvdpsxds", VSX_BUILTIN_XVCVDPSXDS },
9807   { MASK_VSX, CODE_FOR_vsx_fixuns_truncv2dfv2di2, "__builtin_vsx_xvcvdpuxds", VSX_BUILTIN_XVCVDPUXDS },
9808   { MASK_VSX, CODE_FOR_vsx_fixuns_truncv2dfv2di2, "__builtin_vsx_xvcvdpuxds_uns", VSX_BUILTIN_XVCVDPUXDS_UNS },
9809   { MASK_VSX, CODE_FOR_vsx_floatv2div2df2, "__builtin_vsx_xvcvsxddp", VSX_BUILTIN_XVCVSXDDP },
9810   { MASK_VSX, CODE_FOR_vsx_floatunsv2div2df2, "__builtin_vsx_xvcvuxddp", VSX_BUILTIN_XVCVUXDDP },
9811   { MASK_VSX, CODE_FOR_vsx_floatunsv2div2df2, "__builtin_vsx_xvcvuxddp_uns", VSX_BUILTIN_XVCVUXDDP_UNS },
9812
9813   { MASK_VSX, CODE_FOR_vsx_fix_truncv4sfv4si2, "__builtin_vsx_xvcvspsxws", VSX_BUILTIN_XVCVSPSXWS },
9814   { MASK_VSX, CODE_FOR_vsx_fixuns_truncv4sfv4si2, "__builtin_vsx_xvcvspuxws", VSX_BUILTIN_XVCVSPUXWS },
9815   { MASK_VSX, CODE_FOR_vsx_floatv4siv4sf2, "__builtin_vsx_xvcvsxwsp", VSX_BUILTIN_XVCVSXWSP },
9816   { MASK_VSX, CODE_FOR_vsx_floatunsv4siv4sf2, "__builtin_vsx_xvcvuxwsp", VSX_BUILTIN_XVCVUXWSP },
9817
9818   { MASK_VSX, CODE_FOR_vsx_xvcvdpsxws, "__builtin_vsx_xvcvdpsxws", VSX_BUILTIN_XVCVDPSXWS },
9819   { MASK_VSX, CODE_FOR_vsx_xvcvdpuxws, "__builtin_vsx_xvcvdpuxws", VSX_BUILTIN_XVCVDPUXWS },
9820   { MASK_VSX, CODE_FOR_vsx_xvcvsxwdp, "__builtin_vsx_xvcvsxwdp", VSX_BUILTIN_XVCVSXWDP },
9821   { MASK_VSX, CODE_FOR_vsx_xvcvuxwdp, "__builtin_vsx_xvcvuxwdp", VSX_BUILTIN_XVCVUXWDP },
9822   { MASK_VSX, CODE_FOR_vsx_xvrdpi, "__builtin_vsx_xvrdpi", VSX_BUILTIN_XVRDPI },
9823   { MASK_VSX, CODE_FOR_vsx_xvrdpic, "__builtin_vsx_xvrdpic", VSX_BUILTIN_XVRDPIC },
9824   { MASK_VSX, CODE_FOR_vsx_floorv2df2, "__builtin_vsx_xvrdpim", VSX_BUILTIN_XVRDPIM },
9825   { MASK_VSX, CODE_FOR_vsx_ceilv2df2, "__builtin_vsx_xvrdpip", VSX_BUILTIN_XVRDPIP },
9826   { MASK_VSX, CODE_FOR_vsx_btruncv2df2, "__builtin_vsx_xvrdpiz", VSX_BUILTIN_XVRDPIZ },
9827
9828   { MASK_VSX, CODE_FOR_vsx_xvcvspsxds, "__builtin_vsx_xvcvspsxds", VSX_BUILTIN_XVCVSPSXDS },
9829   { MASK_VSX, CODE_FOR_vsx_xvcvspuxds, "__builtin_vsx_xvcvspuxds", VSX_BUILTIN_XVCVSPUXDS },
9830   { MASK_VSX, CODE_FOR_vsx_xvcvsxdsp, "__builtin_vsx_xvcvsxdsp", VSX_BUILTIN_XVCVSXDSP },
9831   { MASK_VSX, CODE_FOR_vsx_xvcvuxdsp, "__builtin_vsx_xvcvuxdsp", VSX_BUILTIN_XVCVUXDSP },
9832   { MASK_VSX, CODE_FOR_vsx_xvrspi, "__builtin_vsx_xvrspi", VSX_BUILTIN_XVRSPI },
9833   { MASK_VSX, CODE_FOR_vsx_xvrspic, "__builtin_vsx_xvrspic", VSX_BUILTIN_XVRSPIC },
9834   { MASK_VSX, CODE_FOR_vsx_floorv4sf2, "__builtin_vsx_xvrspim", VSX_BUILTIN_XVRSPIM },
9835   { MASK_VSX, CODE_FOR_vsx_ceilv4sf2, "__builtin_vsx_xvrspip", VSX_BUILTIN_XVRSPIP },
9836   { MASK_VSX, CODE_FOR_vsx_btruncv4sf2, "__builtin_vsx_xvrspiz", VSX_BUILTIN_XVRSPIZ },
9837
9838   { MASK_VSX, CODE_FOR_vsx_xsrdpi, "__builtin_vsx_xsrdpi", VSX_BUILTIN_XSRDPI },
9839   { MASK_VSX, CODE_FOR_vsx_xsrdpic, "__builtin_vsx_xsrdpic", VSX_BUILTIN_XSRDPIC },
9840   { MASK_VSX, CODE_FOR_vsx_floordf2, "__builtin_vsx_xsrdpim", VSX_BUILTIN_XSRDPIM },
9841   { MASK_VSX, CODE_FOR_vsx_ceildf2, "__builtin_vsx_xsrdpip", VSX_BUILTIN_XSRDPIP },
9842   { MASK_VSX, CODE_FOR_vsx_btruncdf2, "__builtin_vsx_xsrdpiz", VSX_BUILTIN_XSRDPIZ },
9843
9844   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_abs", ALTIVEC_BUILTIN_VEC_ABS },
9845   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_abss", ALTIVEC_BUILTIN_VEC_ABSS },
9846   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_ceil", ALTIVEC_BUILTIN_VEC_CEIL },
9847   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_expte", ALTIVEC_BUILTIN_VEC_EXPTE },
9848   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_floor", ALTIVEC_BUILTIN_VEC_FLOOR },
9849   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_loge", ALTIVEC_BUILTIN_VEC_LOGE },
9850   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_mtvscr", ALTIVEC_BUILTIN_VEC_MTVSCR },
9851   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_re", ALTIVEC_BUILTIN_VEC_RE },
9852   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_round", ALTIVEC_BUILTIN_VEC_ROUND },
9853   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_rsqrt", ALTIVEC_BUILTIN_VEC_RSQRT },
9854   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_rsqrte", ALTIVEC_BUILTIN_VEC_RSQRTE },
9855   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_trunc", ALTIVEC_BUILTIN_VEC_TRUNC },
9856   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_unpackh", ALTIVEC_BUILTIN_VEC_UNPACKH },
9857   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vupkhsh", ALTIVEC_BUILTIN_VEC_VUPKHSH },
9858   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vupkhpx", ALTIVEC_BUILTIN_VEC_VUPKHPX },
9859   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vupkhsb", ALTIVEC_BUILTIN_VEC_VUPKHSB },
9860   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_unpackl", ALTIVEC_BUILTIN_VEC_UNPACKL },
9861   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vupklpx", ALTIVEC_BUILTIN_VEC_VUPKLPX },
9862   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vupklsh", ALTIVEC_BUILTIN_VEC_VUPKLSH },
9863   { MASK_ALTIVEC, CODE_FOR_nothing, "__builtin_vec_vupklsb", ALTIVEC_BUILTIN_VEC_VUPKLSB },
9864
9865   { MASK_VSX, CODE_FOR_nothing, "__builtin_vec_nearbyint", ALTIVEC_BUILTIN_VEC_NEARBYINT },
9866   { MASK_VSX, CODE_FOR_nothing, "__builtin_vec_rint", ALTIVEC_BUILTIN_VEC_RINT },
9867   { MASK_VSX, CODE_FOR_nothing, "__builtin_vec_sqrt", ALTIVEC_BUILTIN_VEC_SQRT },
9868
9869   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_floatv4siv4sf2, "__builtin_vec_float_sisf", VECTOR_BUILTIN_FLOAT_V4SI_V4SF },
9870   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_unsigned_floatv4siv4sf2, "__builtin_vec_uns_float_sisf", VECTOR_BUILTIN_UNSFLOAT_V4SI_V4SF },
9871   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_fix_truncv4sfv4si2, "__builtin_vec_fix_sfsi", VECTOR_BUILTIN_FIX_V4SF_V4SI },
9872   { MASK_ALTIVEC|MASK_VSX, CODE_FOR_fixuns_truncv4sfv4si2, "__builtin_vec_fixuns_sfsi", VECTOR_BUILTIN_FIXUNS_V4SF_V4SI },
9873
9874   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
9875      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
9876   { 0, CODE_FOR_absv2si2, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
9877   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
9878   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
9879   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
9880   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
9881   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
9882   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
9883   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
9884   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
9885   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
9886   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
9887   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
9888   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
9889   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
9890   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
9891   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
9892   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
9893   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
9894   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
9895   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
9896   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
9897   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
9898   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
9899   { 0, CODE_FOR_negv2si2, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
9900   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
9901   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
9902   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
9903   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
9904
9905   /* Place-holder.  Leave as last unary SPE builtin.  */
9906   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
9907
9908   { 0, CODE_FOR_paired_absv2sf2, "__builtin_paired_absv2sf2", PAIRED_BUILTIN_ABSV2SF2 },
9909   { 0, CODE_FOR_nabsv2sf2, "__builtin_paired_nabsv2sf2", PAIRED_BUILTIN_NABSV2SF2 },
9910   { 0, CODE_FOR_paired_negv2sf2, "__builtin_paired_negv2sf2", PAIRED_BUILTIN_NEGV2SF2 },
9911   { 0, CODE_FOR_sqrtv2sf2, "__builtin_paired_sqrtv2sf2", PAIRED_BUILTIN_SQRTV2SF2 },
9912   { 0, CODE_FOR_resv2sf2, "__builtin_paired_resv2sf2", PAIRED_BUILTIN_RESV2SF2 }
9913 };
9914
9915 static rtx
9916 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
9917 {
9918   rtx pat;
9919   tree arg0 = CALL_EXPR_ARG (exp, 0);
9920   rtx op0 = expand_normal (arg0);
9921   enum machine_mode tmode = insn_data[icode].operand[0].mode;
9922   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9923
9924   if (icode == CODE_FOR_nothing)
9925     /* Builtin not supported on this processor.  */
9926     return 0;
9927
9928   /* If we got invalid arguments bail out before generating bad rtl.  */
9929   if (arg0 == error_mark_node)
9930     return const0_rtx;
9931
9932   if (icode == CODE_FOR_altivec_vspltisb
9933       || icode == CODE_FOR_altivec_vspltish
9934       || icode == CODE_FOR_altivec_vspltisw
9935       || icode == CODE_FOR_spe_evsplatfi
9936       || icode == CODE_FOR_spe_evsplati)
9937     {
9938       /* Only allow 5-bit *signed* literals.  */
9939       if (GET_CODE (op0) != CONST_INT
9940           || INTVAL (op0) > 15
9941           || INTVAL (op0) < -16)
9942         {
9943           error ("argument 1 must be a 5-bit signed literal");
9944           return const0_rtx;
9945         }
9946     }
9947
9948   if (target == 0
9949       || GET_MODE (target) != tmode
9950       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9951     target = gen_reg_rtx (tmode);
9952
9953   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9954     op0 = copy_to_mode_reg (mode0, op0);
9955
9956   pat = GEN_FCN (icode) (target, op0);
9957   if (! pat)
9958     return 0;
9959   emit_insn (pat);
9960
9961   return target;
9962 }
9963
9964 static rtx
9965 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
9966 {
9967   rtx pat, scratch1, scratch2;
9968   tree arg0 = CALL_EXPR_ARG (exp, 0);
9969   rtx op0 = expand_normal (arg0);
9970   enum machine_mode tmode = insn_data[icode].operand[0].mode;
9971   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9972
9973   /* If we have invalid arguments, bail out before generating bad rtl.  */
9974   if (arg0 == error_mark_node)
9975     return const0_rtx;
9976
9977   if (target == 0
9978       || GET_MODE (target) != tmode
9979       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9980     target = gen_reg_rtx (tmode);
9981
9982   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9983     op0 = copy_to_mode_reg (mode0, op0);
9984
9985   scratch1 = gen_reg_rtx (mode0);
9986   scratch2 = gen_reg_rtx (mode0);
9987
9988   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
9989   if (! pat)
9990     return 0;
9991   emit_insn (pat);
9992
9993   return target;
9994 }
9995
9996 static rtx
9997 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
9998 {
9999   rtx pat;
10000   tree arg0 = CALL_EXPR_ARG (exp, 0);
10001   tree arg1 = CALL_EXPR_ARG (exp, 1);
10002   rtx op0 = expand_normal (arg0);
10003   rtx op1 = expand_normal (arg1);
10004   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10005   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10006   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10007
10008   if (icode == CODE_FOR_nothing)
10009     /* Builtin not supported on this processor.  */
10010     return 0;
10011
10012   /* If we got invalid arguments bail out before generating bad rtl.  */
10013   if (arg0 == error_mark_node || arg1 == error_mark_node)
10014     return const0_rtx;
10015
10016   if (icode == CODE_FOR_altivec_vcfux
10017       || icode == CODE_FOR_altivec_vcfsx
10018       || icode == CODE_FOR_altivec_vctsxs
10019       || icode == CODE_FOR_altivec_vctuxs
10020       || icode == CODE_FOR_altivec_vspltb
10021       || icode == CODE_FOR_altivec_vsplth
10022       || icode == CODE_FOR_altivec_vspltw
10023       || icode == CODE_FOR_spe_evaddiw
10024       || icode == CODE_FOR_spe_evldd
10025       || icode == CODE_FOR_spe_evldh
10026       || icode == CODE_FOR_spe_evldw
10027       || icode == CODE_FOR_spe_evlhhesplat
10028       || icode == CODE_FOR_spe_evlhhossplat
10029       || icode == CODE_FOR_spe_evlhhousplat
10030       || icode == CODE_FOR_spe_evlwhe
10031       || icode == CODE_FOR_spe_evlwhos
10032       || icode == CODE_FOR_spe_evlwhou
10033       || icode == CODE_FOR_spe_evlwhsplat
10034       || icode == CODE_FOR_spe_evlwwsplat
10035       || icode == CODE_FOR_spe_evrlwi
10036       || icode == CODE_FOR_spe_evslwi
10037       || icode == CODE_FOR_spe_evsrwis
10038       || icode == CODE_FOR_spe_evsubifw
10039       || icode == CODE_FOR_spe_evsrwiu)
10040     {
10041       /* Only allow 5-bit unsigned literals.  */
10042       STRIP_NOPS (arg1);
10043       if (TREE_CODE (arg1) != INTEGER_CST
10044           || TREE_INT_CST_LOW (arg1) & ~0x1f)
10045         {
10046           error ("argument 2 must be a 5-bit unsigned literal");
10047           return const0_rtx;
10048         }
10049     }
10050
10051   if (target == 0
10052       || GET_MODE (target) != tmode
10053       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10054     target = gen_reg_rtx (tmode);
10055
10056   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10057     op0 = copy_to_mode_reg (mode0, op0);
10058   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10059     op1 = copy_to_mode_reg (mode1, op1);
10060
10061   pat = GEN_FCN (icode) (target, op0, op1);
10062   if (! pat)
10063     return 0;
10064   emit_insn (pat);
10065
10066   return target;
10067 }
10068
10069 static rtx
10070 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
10071 {
10072   rtx pat, scratch;
10073   tree cr6_form = CALL_EXPR_ARG (exp, 0);
10074   tree arg0 = CALL_EXPR_ARG (exp, 1);
10075   tree arg1 = CALL_EXPR_ARG (exp, 2);
10076   rtx op0 = expand_normal (arg0);
10077   rtx op1 = expand_normal (arg1);
10078   enum machine_mode tmode = SImode;
10079   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10080   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10081   int cr6_form_int;
10082
10083   if (TREE_CODE (cr6_form) != INTEGER_CST)
10084     {
10085       error ("argument 1 of __builtin_altivec_predicate must be a constant");
10086       return const0_rtx;
10087     }
10088   else
10089     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
10090
10091   gcc_assert (mode0 == mode1);
10092
10093   /* If we have invalid arguments, bail out before generating bad rtl.  */
10094   if (arg0 == error_mark_node || arg1 == error_mark_node)
10095     return const0_rtx;
10096
10097   if (target == 0
10098       || GET_MODE (target) != tmode
10099       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10100     target = gen_reg_rtx (tmode);
10101
10102   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10103     op0 = copy_to_mode_reg (mode0, op0);
10104   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10105     op1 = copy_to_mode_reg (mode1, op1);
10106
10107   scratch = gen_reg_rtx (mode0);
10108
10109   pat = GEN_FCN (icode) (scratch, op0, op1);
10110   if (! pat)
10111     return 0;
10112   emit_insn (pat);
10113
10114   /* The vec_any* and vec_all* predicates use the same opcodes for two
10115      different operations, but the bits in CR6 will be different
10116      depending on what information we want.  So we have to play tricks
10117      with CR6 to get the right bits out.
10118
10119      If you think this is disgusting, look at the specs for the
10120      AltiVec predicates.  */
10121
10122   switch (cr6_form_int)
10123     {
10124     case 0:
10125       emit_insn (gen_cr6_test_for_zero (target));
10126       break;
10127     case 1:
10128       emit_insn (gen_cr6_test_for_zero_reverse (target));
10129       break;
10130     case 2:
10131       emit_insn (gen_cr6_test_for_lt (target));
10132       break;
10133     case 3:
10134       emit_insn (gen_cr6_test_for_lt_reverse (target));
10135       break;
10136     default:
10137       error ("argument 1 of __builtin_altivec_predicate is out of range");
10138       break;
10139     }
10140
10141   return target;
10142 }
10143
10144 static rtx
10145 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
10146 {
10147   rtx pat, addr;
10148   tree arg0 = CALL_EXPR_ARG (exp, 0);
10149   tree arg1 = CALL_EXPR_ARG (exp, 1);
10150   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10151   enum machine_mode mode0 = Pmode;
10152   enum machine_mode mode1 = Pmode;
10153   rtx op0 = expand_normal (arg0);
10154   rtx op1 = expand_normal (arg1);
10155
10156   if (icode == CODE_FOR_nothing)
10157     /* Builtin not supported on this processor.  */
10158     return 0;
10159
10160   /* If we got invalid arguments bail out before generating bad rtl.  */
10161   if (arg0 == error_mark_node || arg1 == error_mark_node)
10162     return const0_rtx;
10163
10164   if (target == 0
10165       || GET_MODE (target) != tmode
10166       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10167     target = gen_reg_rtx (tmode);
10168
10169   op1 = copy_to_mode_reg (mode1, op1);
10170
10171   if (op0 == const0_rtx)
10172     {
10173       addr = gen_rtx_MEM (tmode, op1);
10174     }
10175   else
10176     {
10177       op0 = copy_to_mode_reg (mode0, op0);
10178       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
10179     }
10180
10181   pat = GEN_FCN (icode) (target, addr);
10182
10183   if (! pat)
10184     return 0;
10185   emit_insn (pat);
10186
10187   return target;
10188 }
10189
10190 static rtx
10191 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
10192 {
10193   rtx pat, addr;
10194   tree arg0 = CALL_EXPR_ARG (exp, 0);
10195   tree arg1 = CALL_EXPR_ARG (exp, 1);
10196   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10197   enum machine_mode mode0 = Pmode;
10198   enum machine_mode mode1 = Pmode;
10199   rtx op0 = expand_normal (arg0);
10200   rtx op1 = expand_normal (arg1);
10201
10202   if (icode == CODE_FOR_nothing)
10203     /* Builtin not supported on this processor.  */
10204     return 0;
10205
10206   /* If we got invalid arguments bail out before generating bad rtl.  */
10207   if (arg0 == error_mark_node || arg1 == error_mark_node)
10208     return const0_rtx;
10209
10210   if (target == 0
10211       || GET_MODE (target) != tmode
10212       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10213     target = gen_reg_rtx (tmode);
10214
10215   op1 = copy_to_mode_reg (mode1, op1);
10216
10217   if (op0 == const0_rtx)
10218     {
10219       addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10220     }
10221   else
10222     {
10223       op0 = copy_to_mode_reg (mode0, op0);
10224       addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10225     }
10226
10227   pat = GEN_FCN (icode) (target, addr);
10228
10229   if (! pat)
10230     return 0;
10231   emit_insn (pat);
10232
10233   return target;
10234 }
10235
10236 static rtx
10237 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10238 {
10239   tree arg0 = CALL_EXPR_ARG (exp, 0);
10240   tree arg1 = CALL_EXPR_ARG (exp, 1);
10241   tree arg2 = CALL_EXPR_ARG (exp, 2);
10242   rtx op0 = expand_normal (arg0);
10243   rtx op1 = expand_normal (arg1);
10244   rtx op2 = expand_normal (arg2);
10245   rtx pat;
10246   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10247   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10248   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10249
10250   /* Invalid arguments.  Bail before doing anything stoopid!  */
10251   if (arg0 == error_mark_node
10252       || arg1 == error_mark_node
10253       || arg2 == error_mark_node)
10254     return const0_rtx;
10255
10256   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10257     op0 = copy_to_mode_reg (mode2, op0);
10258   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10259     op1 = copy_to_mode_reg (mode0, op1);
10260   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10261     op2 = copy_to_mode_reg (mode1, op2);
10262
10263   pat = GEN_FCN (icode) (op1, op2, op0);
10264   if (pat)
10265     emit_insn (pat);
10266   return NULL_RTX;
10267 }
10268
10269 static rtx
10270 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10271 {
10272   tree arg0 = CALL_EXPR_ARG (exp, 0);
10273   tree arg1 = CALL_EXPR_ARG (exp, 1);
10274   tree arg2 = CALL_EXPR_ARG (exp, 2);
10275   rtx op0 = expand_normal (arg0);
10276   rtx op1 = expand_normal (arg1);
10277   rtx op2 = expand_normal (arg2);
10278   rtx pat, addr;
10279   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10280   enum machine_mode mode1 = Pmode;
10281   enum machine_mode mode2 = Pmode;
10282
10283   /* Invalid arguments.  Bail before doing anything stoopid!  */
10284   if (arg0 == error_mark_node
10285       || arg1 == error_mark_node
10286       || arg2 == error_mark_node)
10287     return const0_rtx;
10288
10289   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10290     op0 = copy_to_mode_reg (tmode, op0);
10291
10292   op2 = copy_to_mode_reg (mode2, op2);
10293
10294   if (op1 == const0_rtx)
10295     {
10296       addr = gen_rtx_MEM (tmode, op2);
10297     }
10298   else
10299     {
10300       op1 = copy_to_mode_reg (mode1, op1);
10301       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10302     }
10303
10304   pat = GEN_FCN (icode) (addr, op0);
10305   if (pat)
10306     emit_insn (pat);
10307   return NULL_RTX;
10308 }
10309
10310 static rtx
10311 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10312 {
10313   tree arg0 = CALL_EXPR_ARG (exp, 0);
10314   tree arg1 = CALL_EXPR_ARG (exp, 1);
10315   tree arg2 = CALL_EXPR_ARG (exp, 2);
10316   rtx op0 = expand_normal (arg0);
10317   rtx op1 = expand_normal (arg1);
10318   rtx op2 = expand_normal (arg2);
10319   rtx pat, addr;
10320   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10321   enum machine_mode mode1 = Pmode;
10322   enum machine_mode mode2 = Pmode;
10323
10324   /* Invalid arguments.  Bail before doing anything stoopid!  */
10325   if (arg0 == error_mark_node
10326       || arg1 == error_mark_node
10327       || arg2 == error_mark_node)
10328     return const0_rtx;
10329
10330   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10331     op0 = copy_to_mode_reg (tmode, op0);
10332
10333   op2 = copy_to_mode_reg (mode2, op2);
10334
10335   if (op1 == const0_rtx)
10336     {
10337       addr = gen_rtx_MEM (tmode, op2);
10338     }
10339   else
10340     {
10341       op1 = copy_to_mode_reg (mode1, op1);
10342       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10343     }
10344
10345   pat = GEN_FCN (icode) (addr, op0);
10346   if (pat)
10347     emit_insn (pat);
10348   return NULL_RTX;
10349 }
10350
10351 static rtx
10352 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
10353 {
10354   rtx pat;
10355   tree arg0 = CALL_EXPR_ARG (exp, 0);
10356   tree arg1 = CALL_EXPR_ARG (exp, 1);
10357   tree arg2 = CALL_EXPR_ARG (exp, 2);
10358   rtx op0 = expand_normal (arg0);
10359   rtx op1 = expand_normal (arg1);
10360   rtx op2 = expand_normal (arg2);
10361   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10362   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10363   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10364   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
10365
10366   if (icode == CODE_FOR_nothing)
10367     /* Builtin not supported on this processor.  */
10368     return 0;
10369
10370   /* If we got invalid arguments bail out before generating bad rtl.  */
10371   if (arg0 == error_mark_node
10372       || arg1 == error_mark_node
10373       || arg2 == error_mark_node)
10374     return const0_rtx;
10375
10376   switch (icode)
10377     {
10378     case CODE_FOR_altivec_vsldoi_v4sf:
10379     case CODE_FOR_altivec_vsldoi_v4si:
10380     case CODE_FOR_altivec_vsldoi_v8hi:
10381     case CODE_FOR_altivec_vsldoi_v16qi:
10382       /* Only allow 4-bit unsigned literals.  */
10383       STRIP_NOPS (arg2);
10384       if (TREE_CODE (arg2) != INTEGER_CST
10385           || TREE_INT_CST_LOW (arg2) & ~0xf)
10386         {
10387           error ("argument 3 must be a 4-bit unsigned literal");
10388           return const0_rtx;
10389         }
10390       break;
10391
10392     case CODE_FOR_vsx_xxpermdi_v2df:
10393     case CODE_FOR_vsx_xxpermdi_v2di:
10394     case CODE_FOR_vsx_xxsldwi_v16qi:
10395     case CODE_FOR_vsx_xxsldwi_v8hi:
10396     case CODE_FOR_vsx_xxsldwi_v4si:
10397     case CODE_FOR_vsx_xxsldwi_v4sf:
10398     case CODE_FOR_vsx_xxsldwi_v2di:
10399     case CODE_FOR_vsx_xxsldwi_v2df:
10400       /* Only allow 2-bit unsigned literals.  */
10401       STRIP_NOPS (arg2);
10402       if (TREE_CODE (arg2) != INTEGER_CST
10403           || TREE_INT_CST_LOW (arg2) & ~0x3)
10404         {
10405           error ("argument 3 must be a 2-bit unsigned literal");
10406           return const0_rtx;
10407         }
10408       break;
10409
10410     case CODE_FOR_vsx_set_v2df:
10411     case CODE_FOR_vsx_set_v2di:
10412       /* Only allow 1-bit unsigned literals.  */
10413       STRIP_NOPS (arg2);
10414       if (TREE_CODE (arg2) != INTEGER_CST
10415           || TREE_INT_CST_LOW (arg2) & ~0x1)
10416         {
10417           error ("argument 3 must be a 1-bit unsigned literal");
10418           return const0_rtx;
10419         }
10420       break;
10421
10422     default:
10423       break;
10424     }
10425
10426   if (target == 0
10427       || GET_MODE (target) != tmode
10428       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10429     target = gen_reg_rtx (tmode);
10430
10431   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10432     op0 = copy_to_mode_reg (mode0, op0);
10433   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10434     op1 = copy_to_mode_reg (mode1, op1);
10435   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
10436     op2 = copy_to_mode_reg (mode2, op2);
10437
10438   if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
10439     pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
10440   else 
10441     pat = GEN_FCN (icode) (target, op0, op1, op2);
10442   if (! pat)
10443     return 0;
10444   emit_insn (pat);
10445
10446   return target;
10447 }
10448
10449 /* Expand the lvx builtins.  */
10450 static rtx
10451 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
10452 {
10453   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10454   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10455   tree arg0;
10456   enum machine_mode tmode, mode0;
10457   rtx pat, op0;
10458   enum insn_code icode;
10459
10460   switch (fcode)
10461     {
10462     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
10463       icode = CODE_FOR_vector_load_v16qi;
10464       break;
10465     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
10466       icode = CODE_FOR_vector_load_v8hi;
10467       break;
10468     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
10469       icode = CODE_FOR_vector_load_v4si;
10470       break;
10471     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
10472       icode = CODE_FOR_vector_load_v4sf;
10473       break;
10474     default:
10475       *expandedp = false;
10476       return NULL_RTX;
10477     }
10478
10479   *expandedp = true;
10480
10481   arg0 = CALL_EXPR_ARG (exp, 0);
10482   op0 = expand_normal (arg0);
10483   tmode = insn_data[icode].operand[0].mode;
10484   mode0 = insn_data[icode].operand[1].mode;
10485
10486   if (target == 0
10487       || GET_MODE (target) != tmode
10488       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10489     target = gen_reg_rtx (tmode);
10490
10491   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10492     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10493
10494   pat = GEN_FCN (icode) (target, op0);
10495   if (! pat)
10496     return 0;
10497   emit_insn (pat);
10498   return target;
10499 }
10500
10501 /* Expand the stvx builtins.  */
10502 static rtx
10503 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10504                            bool *expandedp)
10505 {
10506   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10507   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10508   tree arg0, arg1;
10509   enum machine_mode mode0, mode1;
10510   rtx pat, op0, op1;
10511   enum insn_code icode;
10512
10513   switch (fcode)
10514     {
10515     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
10516       icode = CODE_FOR_vector_store_v16qi;
10517       break;
10518     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
10519       icode = CODE_FOR_vector_store_v8hi;
10520       break;
10521     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
10522       icode = CODE_FOR_vector_store_v4si;
10523       break;
10524     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
10525       icode = CODE_FOR_vector_store_v4sf;
10526       break;
10527     default:
10528       *expandedp = false;
10529       return NULL_RTX;
10530     }
10531
10532   arg0 = CALL_EXPR_ARG (exp, 0);
10533   arg1 = CALL_EXPR_ARG (exp, 1);
10534   op0 = expand_normal (arg0);
10535   op1 = expand_normal (arg1);
10536   mode0 = insn_data[icode].operand[0].mode;
10537   mode1 = insn_data[icode].operand[1].mode;
10538
10539   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10540     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10541   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
10542     op1 = copy_to_mode_reg (mode1, op1);
10543
10544   pat = GEN_FCN (icode) (op0, op1);
10545   if (pat)
10546     emit_insn (pat);
10547
10548   *expandedp = true;
10549   return NULL_RTX;
10550 }
10551
10552 /* Expand the dst builtins.  */
10553 static rtx
10554 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10555                             bool *expandedp)
10556 {
10557   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10558   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10559   tree arg0, arg1, arg2;
10560   enum machine_mode mode0, mode1;
10561   rtx pat, op0, op1, op2;
10562   const struct builtin_description *d;
10563   size_t i;
10564
10565   *expandedp = false;
10566
10567   /* Handle DST variants.  */
10568   d = bdesc_dst;
10569   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
10570     if (d->code == fcode)
10571       {
10572         arg0 = CALL_EXPR_ARG (exp, 0);
10573         arg1 = CALL_EXPR_ARG (exp, 1);
10574         arg2 = CALL_EXPR_ARG (exp, 2);
10575         op0 = expand_normal (arg0);
10576         op1 = expand_normal (arg1);
10577         op2 = expand_normal (arg2);
10578         mode0 = insn_data[d->icode].operand[0].mode;
10579         mode1 = insn_data[d->icode].operand[1].mode;
10580
10581         /* Invalid arguments, bail out before generating bad rtl.  */
10582         if (arg0 == error_mark_node
10583             || arg1 == error_mark_node
10584             || arg2 == error_mark_node)
10585           return const0_rtx;
10586
10587         *expandedp = true;
10588         STRIP_NOPS (arg2);
10589         if (TREE_CODE (arg2) != INTEGER_CST
10590             || TREE_INT_CST_LOW (arg2) & ~0x3)
10591           {
10592             error ("argument to %qs must be a 2-bit unsigned literal", d->name);
10593             return const0_rtx;
10594           }
10595
10596         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
10597           op0 = copy_to_mode_reg (Pmode, op0);
10598         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
10599           op1 = copy_to_mode_reg (mode1, op1);
10600
10601         pat = GEN_FCN (d->icode) (op0, op1, op2);
10602         if (pat != 0)
10603           emit_insn (pat);
10604
10605         return NULL_RTX;
10606       }
10607
10608   return NULL_RTX;
10609 }
10610
10611 /* Expand vec_init builtin.  */
10612 static rtx
10613 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
10614 {
10615   enum machine_mode tmode = TYPE_MODE (type);
10616   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
10617   int i, n_elt = GET_MODE_NUNITS (tmode);
10618   rtvec v = rtvec_alloc (n_elt);
10619
10620   gcc_assert (VECTOR_MODE_P (tmode));
10621   gcc_assert (n_elt == call_expr_nargs (exp));
10622
10623   for (i = 0; i < n_elt; ++i)
10624     {
10625       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
10626       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
10627     }
10628
10629   if (!target || !register_operand (target, tmode))
10630     target = gen_reg_rtx (tmode);
10631
10632   rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
10633   return target;
10634 }
10635
10636 /* Return the integer constant in ARG.  Constrain it to be in the range
10637    of the subparts of VEC_TYPE; issue an error if not.  */
10638
10639 static int
10640 get_element_number (tree vec_type, tree arg)
10641 {
10642   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
10643
10644   if (!host_integerp (arg, 1)
10645       || (elt = tree_low_cst (arg, 1), elt > max))
10646     {
10647       error ("selector must be an integer constant in the range 0..%wi", max);
10648       return 0;
10649     }
10650
10651   return elt;
10652 }
10653
10654 /* Expand vec_set builtin.  */
10655 static rtx
10656 altivec_expand_vec_set_builtin (tree exp)
10657 {
10658   enum machine_mode tmode, mode1;
10659   tree arg0, arg1, arg2;
10660   int elt;
10661   rtx op0, op1;
10662
10663   arg0 = CALL_EXPR_ARG (exp, 0);
10664   arg1 = CALL_EXPR_ARG (exp, 1);
10665   arg2 = CALL_EXPR_ARG (exp, 2);
10666
10667   tmode = TYPE_MODE (TREE_TYPE (arg0));
10668   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10669   gcc_assert (VECTOR_MODE_P (tmode));
10670
10671   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
10672   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
10673   elt = get_element_number (TREE_TYPE (arg0), arg2);
10674
10675   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
10676     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
10677
10678   op0 = force_reg (tmode, op0);
10679   op1 = force_reg (mode1, op1);
10680
10681   rs6000_expand_vector_set (op0, op1, elt);
10682
10683   return op0;
10684 }
10685
10686 /* Expand vec_ext builtin.  */
10687 static rtx
10688 altivec_expand_vec_ext_builtin (tree exp, rtx target)
10689 {
10690   enum machine_mode tmode, mode0;
10691   tree arg0, arg1;
10692   int elt;
10693   rtx op0;
10694
10695   arg0 = CALL_EXPR_ARG (exp, 0);
10696   arg1 = CALL_EXPR_ARG (exp, 1);
10697
10698   op0 = expand_normal (arg0);
10699   elt = get_element_number (TREE_TYPE (arg0), arg1);
10700
10701   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10702   mode0 = TYPE_MODE (TREE_TYPE (arg0));
10703   gcc_assert (VECTOR_MODE_P (mode0));
10704
10705   op0 = force_reg (mode0, op0);
10706
10707   if (optimize || !target || !register_operand (target, tmode))
10708     target = gen_reg_rtx (tmode);
10709
10710   rs6000_expand_vector_extract (target, op0, elt);
10711
10712   return target;
10713 }
10714
10715 /* Expand the builtin in EXP and store the result in TARGET.  Store
10716    true in *EXPANDEDP if we found a builtin to expand.  */
10717 static rtx
10718 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
10719 {
10720   const struct builtin_description *d;
10721   const struct builtin_description_predicates *dp;
10722   size_t i;
10723   enum insn_code icode;
10724   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10725   tree arg0;
10726   rtx op0, pat;
10727   enum machine_mode tmode, mode0;
10728   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10729
10730   if ((fcode >= ALTIVEC_BUILTIN_OVERLOADED_FIRST
10731        && fcode <= ALTIVEC_BUILTIN_OVERLOADED_LAST)
10732       || (fcode >= VSX_BUILTIN_OVERLOADED_FIRST
10733           && fcode <= VSX_BUILTIN_OVERLOADED_LAST))
10734     {
10735       *expandedp = true;
10736       error ("unresolved overload for Altivec builtin %qF", fndecl);
10737       return const0_rtx;
10738     }
10739
10740   target = altivec_expand_ld_builtin (exp, target, expandedp);
10741   if (*expandedp)
10742     return target;
10743
10744   target = altivec_expand_st_builtin (exp, target, expandedp);
10745   if (*expandedp)
10746     return target;
10747
10748   target = altivec_expand_dst_builtin (exp, target, expandedp);
10749   if (*expandedp)
10750     return target;
10751
10752   *expandedp = true;
10753
10754   switch (fcode)
10755     {
10756     case ALTIVEC_BUILTIN_STVX:
10757       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, exp);
10758     case ALTIVEC_BUILTIN_STVEBX:
10759       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
10760     case ALTIVEC_BUILTIN_STVEHX:
10761       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
10762     case ALTIVEC_BUILTIN_STVEWX:
10763       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
10764     case ALTIVEC_BUILTIN_STVXL:
10765       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
10766
10767     case ALTIVEC_BUILTIN_STVLX:
10768       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
10769     case ALTIVEC_BUILTIN_STVLXL:
10770       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
10771     case ALTIVEC_BUILTIN_STVRX:
10772       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
10773     case ALTIVEC_BUILTIN_STVRXL:
10774       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
10775
10776     case ALTIVEC_BUILTIN_MFVSCR:
10777       icode = CODE_FOR_altivec_mfvscr;
10778       tmode = insn_data[icode].operand[0].mode;
10779
10780       if (target == 0
10781           || GET_MODE (target) != tmode
10782           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10783         target = gen_reg_rtx (tmode);
10784
10785       pat = GEN_FCN (icode) (target);
10786       if (! pat)
10787         return 0;
10788       emit_insn (pat);
10789       return target;
10790
10791     case ALTIVEC_BUILTIN_MTVSCR:
10792       icode = CODE_FOR_altivec_mtvscr;
10793       arg0 = CALL_EXPR_ARG (exp, 0);
10794       op0 = expand_normal (arg0);
10795       mode0 = insn_data[icode].operand[0].mode;
10796
10797       /* If we got invalid arguments bail out before generating bad rtl.  */
10798       if (arg0 == error_mark_node)
10799         return const0_rtx;
10800
10801       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10802         op0 = copy_to_mode_reg (mode0, op0);
10803
10804       pat = GEN_FCN (icode) (op0);
10805       if (pat)
10806         emit_insn (pat);
10807       return NULL_RTX;
10808
10809     case ALTIVEC_BUILTIN_DSSALL:
10810       emit_insn (gen_altivec_dssall ());
10811       return NULL_RTX;
10812
10813     case ALTIVEC_BUILTIN_DSS:
10814       icode = CODE_FOR_altivec_dss;
10815       arg0 = CALL_EXPR_ARG (exp, 0);
10816       STRIP_NOPS (arg0);
10817       op0 = expand_normal (arg0);
10818       mode0 = insn_data[icode].operand[0].mode;
10819
10820       /* If we got invalid arguments bail out before generating bad rtl.  */
10821       if (arg0 == error_mark_node)
10822         return const0_rtx;
10823
10824       if (TREE_CODE (arg0) != INTEGER_CST
10825           || TREE_INT_CST_LOW (arg0) & ~0x3)
10826         {
10827           error ("argument to dss must be a 2-bit unsigned literal");
10828           return const0_rtx;
10829         }
10830
10831       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10832         op0 = copy_to_mode_reg (mode0, op0);
10833
10834       emit_insn (gen_altivec_dss (op0));
10835       return NULL_RTX;
10836
10837     case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
10838     case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
10839     case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
10840     case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
10841     case VSX_BUILTIN_VEC_INIT_V2DF:
10842     case VSX_BUILTIN_VEC_INIT_V2DI:
10843       return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
10844
10845     case ALTIVEC_BUILTIN_VEC_SET_V4SI:
10846     case ALTIVEC_BUILTIN_VEC_SET_V8HI:
10847     case ALTIVEC_BUILTIN_VEC_SET_V16QI:
10848     case ALTIVEC_BUILTIN_VEC_SET_V4SF:
10849     case VSX_BUILTIN_VEC_SET_V2DF:
10850     case VSX_BUILTIN_VEC_SET_V2DI:
10851       return altivec_expand_vec_set_builtin (exp);
10852
10853     case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
10854     case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
10855     case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
10856     case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
10857     case VSX_BUILTIN_VEC_EXT_V2DF:
10858     case VSX_BUILTIN_VEC_EXT_V2DI:
10859       return altivec_expand_vec_ext_builtin (exp, target);
10860
10861     default:
10862       break;
10863       /* Fall through.  */
10864     }
10865
10866   /* Expand abs* operations.  */
10867   d = bdesc_abs;
10868   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
10869     if (d->code == fcode)
10870       return altivec_expand_abs_builtin (d->icode, exp, target);
10871
10872   /* Expand the AltiVec predicates.  */
10873   dp = bdesc_altivec_preds;
10874   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
10875     if (dp->code == fcode)
10876       return altivec_expand_predicate_builtin (dp->icode, exp, target);
10877
10878   /* LV* are funky.  We initialized them differently.  */
10879   switch (fcode)
10880     {
10881     case ALTIVEC_BUILTIN_LVSL:
10882       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
10883                                         exp, target, false);
10884     case ALTIVEC_BUILTIN_LVSR:
10885       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
10886                                         exp, target, false);
10887     case ALTIVEC_BUILTIN_LVEBX:
10888       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
10889                                         exp, target, false);
10890     case ALTIVEC_BUILTIN_LVEHX:
10891       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
10892                                         exp, target, false);
10893     case ALTIVEC_BUILTIN_LVEWX:
10894       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
10895                                         exp, target, false);
10896     case ALTIVEC_BUILTIN_LVXL:
10897       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
10898                                         exp, target, false);
10899     case ALTIVEC_BUILTIN_LVX:
10900       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx,
10901                                         exp, target, false);
10902     case ALTIVEC_BUILTIN_LVLX:
10903       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
10904                                         exp, target, true);
10905     case ALTIVEC_BUILTIN_LVLXL:
10906       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
10907                                         exp, target, true);
10908     case ALTIVEC_BUILTIN_LVRX:
10909       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
10910                                         exp, target, true);
10911     case ALTIVEC_BUILTIN_LVRXL:
10912       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
10913                                         exp, target, true);
10914     default:
10915       break;
10916       /* Fall through.  */
10917     }
10918
10919   *expandedp = false;
10920   return NULL_RTX;
10921 }
10922
10923 /* Expand the builtin in EXP and store the result in TARGET.  Store
10924    true in *EXPANDEDP if we found a builtin to expand.  */
10925 static rtx
10926 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
10927 {
10928   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10929   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10930   const struct builtin_description *d;
10931   size_t i;
10932
10933   *expandedp = true;
10934
10935   switch (fcode)
10936     {
10937     case PAIRED_BUILTIN_STX:
10938       return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
10939     case PAIRED_BUILTIN_LX:
10940       return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
10941     default:
10942       break;
10943       /* Fall through.  */
10944     }
10945
10946   /* Expand the paired predicates.  */
10947   d = bdesc_paired_preds;
10948   for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
10949     if (d->code == fcode)
10950       return paired_expand_predicate_builtin (d->icode, exp, target);
10951
10952   *expandedp = false;
10953   return NULL_RTX;
10954 }
10955
10956 /* Binops that need to be initialized manually, but can be expanded
10957    automagically by rs6000_expand_binop_builtin.  */
10958 static struct builtin_description bdesc_2arg_spe[] =
10959 {
10960   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
10961   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
10962   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
10963   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
10964   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
10965   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
10966   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
10967   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
10968   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
10969   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
10970   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
10971   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
10972   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
10973   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
10974   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
10975   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
10976   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
10977   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
10978   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
10979   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
10980   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
10981   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
10982 };
10983
10984 /* Expand the builtin in EXP and store the result in TARGET.  Store
10985    true in *EXPANDEDP if we found a builtin to expand.
10986
10987    This expands the SPE builtins that are not simple unary and binary
10988    operations.  */
10989 static rtx
10990 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
10991 {
10992   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10993   tree arg1, arg0;
10994   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10995   enum insn_code icode;
10996   enum machine_mode tmode, mode0;
10997   rtx pat, op0;
10998   struct builtin_description *d;
10999   size_t i;
11000
11001   *expandedp = true;
11002
11003   /* Syntax check for a 5-bit unsigned immediate.  */
11004   switch (fcode)
11005     {
11006     case SPE_BUILTIN_EVSTDD:
11007     case SPE_BUILTIN_EVSTDH:
11008     case SPE_BUILTIN_EVSTDW:
11009     case SPE_BUILTIN_EVSTWHE:
11010     case SPE_BUILTIN_EVSTWHO:
11011     case SPE_BUILTIN_EVSTWWE:
11012     case SPE_BUILTIN_EVSTWWO:
11013       arg1 = CALL_EXPR_ARG (exp, 2);
11014       if (TREE_CODE (arg1) != INTEGER_CST
11015           || TREE_INT_CST_LOW (arg1) & ~0x1f)
11016         {
11017           error ("argument 2 must be a 5-bit unsigned literal");
11018           return const0_rtx;
11019         }
11020       break;
11021     default:
11022       break;
11023     }
11024
11025   /* The evsplat*i instructions are not quite generic.  */
11026   switch (fcode)
11027     {
11028     case SPE_BUILTIN_EVSPLATFI:
11029       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
11030                                          exp, target);
11031     case SPE_BUILTIN_EVSPLATI:
11032       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
11033                                          exp, target);
11034     default:
11035       break;
11036     }
11037
11038   d = (struct builtin_description *) bdesc_2arg_spe;
11039   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
11040     if (d->code == fcode)
11041       return rs6000_expand_binop_builtin (d->icode, exp, target);
11042
11043   d = (struct builtin_description *) bdesc_spe_predicates;
11044   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
11045     if (d->code == fcode)
11046       return spe_expand_predicate_builtin (d->icode, exp, target);
11047
11048   d = (struct builtin_description *) bdesc_spe_evsel;
11049   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
11050     if (d->code == fcode)
11051       return spe_expand_evsel_builtin (d->icode, exp, target);
11052
11053   switch (fcode)
11054     {
11055     case SPE_BUILTIN_EVSTDDX:
11056       return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
11057     case SPE_BUILTIN_EVSTDHX:
11058       return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
11059     case SPE_BUILTIN_EVSTDWX:
11060       return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
11061     case SPE_BUILTIN_EVSTWHEX:
11062       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
11063     case SPE_BUILTIN_EVSTWHOX:
11064       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
11065     case SPE_BUILTIN_EVSTWWEX:
11066       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
11067     case SPE_BUILTIN_EVSTWWOX:
11068       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
11069     case SPE_BUILTIN_EVSTDD:
11070       return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
11071     case SPE_BUILTIN_EVSTDH:
11072       return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
11073     case SPE_BUILTIN_EVSTDW:
11074       return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
11075     case SPE_BUILTIN_EVSTWHE:
11076       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
11077     case SPE_BUILTIN_EVSTWHO:
11078       return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
11079     case SPE_BUILTIN_EVSTWWE:
11080       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
11081     case SPE_BUILTIN_EVSTWWO:
11082       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
11083     case SPE_BUILTIN_MFSPEFSCR:
11084       icode = CODE_FOR_spe_mfspefscr;
11085       tmode = insn_data[icode].operand[0].mode;
11086
11087       if (target == 0
11088           || GET_MODE (target) != tmode
11089           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11090         target = gen_reg_rtx (tmode);
11091
11092       pat = GEN_FCN (icode) (target);
11093       if (! pat)
11094         return 0;
11095       emit_insn (pat);
11096       return target;
11097     case SPE_BUILTIN_MTSPEFSCR:
11098       icode = CODE_FOR_spe_mtspefscr;
11099       arg0 = CALL_EXPR_ARG (exp, 0);
11100       op0 = expand_normal (arg0);
11101       mode0 = insn_data[icode].operand[0].mode;
11102
11103       if (arg0 == error_mark_node)
11104         return const0_rtx;
11105
11106       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11107         op0 = copy_to_mode_reg (mode0, op0);
11108
11109       pat = GEN_FCN (icode) (op0);
11110       if (pat)
11111         emit_insn (pat);
11112       return NULL_RTX;
11113     default:
11114       break;
11115     }
11116
11117   *expandedp = false;
11118   return NULL_RTX;
11119 }
11120
11121 static rtx
11122 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11123 {
11124   rtx pat, scratch, tmp;
11125   tree form = CALL_EXPR_ARG (exp, 0);
11126   tree arg0 = CALL_EXPR_ARG (exp, 1);
11127   tree arg1 = CALL_EXPR_ARG (exp, 2);
11128   rtx op0 = expand_normal (arg0);
11129   rtx op1 = expand_normal (arg1);
11130   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11131   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11132   int form_int;
11133   enum rtx_code code;
11134
11135   if (TREE_CODE (form) != INTEGER_CST)
11136     {
11137       error ("argument 1 of __builtin_paired_predicate must be a constant");
11138       return const0_rtx;
11139     }
11140   else
11141     form_int = TREE_INT_CST_LOW (form);
11142
11143   gcc_assert (mode0 == mode1);
11144
11145   if (arg0 == error_mark_node || arg1 == error_mark_node)
11146     return const0_rtx;
11147
11148   if (target == 0
11149       || GET_MODE (target) != SImode
11150       || !(*insn_data[icode].operand[0].predicate) (target, SImode))
11151     target = gen_reg_rtx (SImode);
11152   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
11153     op0 = copy_to_mode_reg (mode0, op0);
11154   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
11155     op1 = copy_to_mode_reg (mode1, op1);
11156
11157   scratch = gen_reg_rtx (CCFPmode);
11158
11159   pat = GEN_FCN (icode) (scratch, op0, op1);
11160   if (!pat)
11161     return const0_rtx;
11162
11163   emit_insn (pat);
11164
11165   switch (form_int)
11166     {
11167       /* LT bit.  */
11168     case 0:
11169       code = LT;
11170       break;
11171       /* GT bit.  */
11172     case 1:
11173       code = GT;
11174       break;
11175       /* EQ bit.  */
11176     case 2:
11177       code = EQ;
11178       break;
11179       /* UN bit.  */
11180     case 3:
11181       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11182       return target;
11183     default:
11184       error ("argument 1 of __builtin_paired_predicate is out of range");
11185       return const0_rtx;
11186     }
11187
11188   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11189   emit_move_insn (target, tmp);
11190   return target;
11191 }
11192
11193 static rtx
11194 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11195 {
11196   rtx pat, scratch, tmp;
11197   tree form = CALL_EXPR_ARG (exp, 0);
11198   tree arg0 = CALL_EXPR_ARG (exp, 1);
11199   tree arg1 = CALL_EXPR_ARG (exp, 2);
11200   rtx op0 = expand_normal (arg0);
11201   rtx op1 = expand_normal (arg1);
11202   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11203   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11204   int form_int;
11205   enum rtx_code code;
11206
11207   if (TREE_CODE (form) != INTEGER_CST)
11208     {
11209       error ("argument 1 of __builtin_spe_predicate must be a constant");
11210       return const0_rtx;
11211     }
11212   else
11213     form_int = TREE_INT_CST_LOW (form);
11214
11215   gcc_assert (mode0 == mode1);
11216
11217   if (arg0 == error_mark_node || arg1 == error_mark_node)
11218     return const0_rtx;
11219
11220   if (target == 0
11221       || GET_MODE (target) != SImode
11222       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
11223     target = gen_reg_rtx (SImode);
11224
11225   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11226     op0 = copy_to_mode_reg (mode0, op0);
11227   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11228     op1 = copy_to_mode_reg (mode1, op1);
11229
11230   scratch = gen_reg_rtx (CCmode);
11231
11232   pat = GEN_FCN (icode) (scratch, op0, op1);
11233   if (! pat)
11234     return const0_rtx;
11235   emit_insn (pat);
11236
11237   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
11238      _lower_.  We use one compare, but look in different bits of the
11239      CR for each variant.
11240
11241      There are 2 elements in each SPE simd type (upper/lower).  The CR
11242      bits are set as follows:
11243
11244      BIT0  | BIT 1  | BIT 2   | BIT 3
11245      U     |   L    | (U | L) | (U & L)
11246
11247      So, for an "all" relationship, BIT 3 would be set.
11248      For an "any" relationship, BIT 2 would be set.  Etc.
11249
11250      Following traditional nomenclature, these bits map to:
11251
11252      BIT0  | BIT 1  | BIT 2   | BIT 3
11253      LT    | GT     | EQ      | OV
11254
11255      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
11256   */
11257
11258   switch (form_int)
11259     {
11260       /* All variant.  OV bit.  */
11261     case 0:
11262       /* We need to get to the OV bit, which is the ORDERED bit.  We
11263          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
11264          that's ugly and will make validate_condition_mode die.
11265          So let's just use another pattern.  */
11266       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11267       return target;
11268       /* Any variant.  EQ bit.  */
11269     case 1:
11270       code = EQ;
11271       break;
11272       /* Upper variant.  LT bit.  */
11273     case 2:
11274       code = LT;
11275       break;
11276       /* Lower variant.  GT bit.  */
11277     case 3:
11278       code = GT;
11279       break;
11280     default:
11281       error ("argument 1 of __builtin_spe_predicate is out of range");
11282       return const0_rtx;
11283     }
11284
11285   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11286   emit_move_insn (target, tmp);
11287
11288   return target;
11289 }
11290
11291 /* The evsel builtins look like this:
11292
11293      e = __builtin_spe_evsel_OP (a, b, c, d);
11294
11295    and work like this:
11296
11297      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
11298      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
11299 */
11300
11301 static rtx
11302 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
11303 {
11304   rtx pat, scratch;
11305   tree arg0 = CALL_EXPR_ARG (exp, 0);
11306   tree arg1 = CALL_EXPR_ARG (exp, 1);
11307   tree arg2 = CALL_EXPR_ARG (exp, 2);
11308   tree arg3 = CALL_EXPR_ARG (exp, 3);
11309   rtx op0 = expand_normal (arg0);
11310   rtx op1 = expand_normal (arg1);
11311   rtx op2 = expand_normal (arg2);
11312   rtx op3 = expand_normal (arg3);
11313   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11314   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11315
11316   gcc_assert (mode0 == mode1);
11317
11318   if (arg0 == error_mark_node || arg1 == error_mark_node
11319       || arg2 == error_mark_node || arg3 == error_mark_node)
11320     return const0_rtx;
11321
11322   if (target == 0
11323       || GET_MODE (target) != mode0
11324       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
11325     target = gen_reg_rtx (mode0);
11326
11327   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11328     op0 = copy_to_mode_reg (mode0, op0);
11329   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11330     op1 = copy_to_mode_reg (mode0, op1);
11331   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
11332     op2 = copy_to_mode_reg (mode0, op2);
11333   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
11334     op3 = copy_to_mode_reg (mode0, op3);
11335
11336   /* Generate the compare.  */
11337   scratch = gen_reg_rtx (CCmode);
11338   pat = GEN_FCN (icode) (scratch, op0, op1);
11339   if (! pat)
11340     return const0_rtx;
11341   emit_insn (pat);
11342
11343   if (mode0 == V2SImode)
11344     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
11345   else
11346     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
11347
11348   return target;
11349 }
11350
11351 /* Expand an expression EXP that calls a built-in function,
11352    with result going to TARGET if that's convenient
11353    (and in mode MODE if that's convenient).
11354    SUBTARGET may be used as the target for computing one of EXP's operands.
11355    IGNORE is nonzero if the value is to be ignored.  */
11356
11357 static rtx
11358 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
11359                        enum machine_mode mode ATTRIBUTE_UNUSED,
11360                        int ignore ATTRIBUTE_UNUSED)
11361 {
11362   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11363   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11364   const struct builtin_description *d;
11365   size_t i;
11366   rtx ret;
11367   bool success;
11368
11369   switch (fcode)
11370     {
11371     case RS6000_BUILTIN_RECIP:
11372       return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
11373
11374     case RS6000_BUILTIN_RECIPF:
11375       return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
11376
11377     case RS6000_BUILTIN_RSQRTF:
11378       return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
11379
11380     case RS6000_BUILTIN_RSQRT:
11381       return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
11382
11383     case RS6000_BUILTIN_BSWAP_HI:
11384       return rs6000_expand_unop_builtin (CODE_FOR_bswaphi2, exp, target);
11385
11386     case POWER7_BUILTIN_BPERMD:
11387       return rs6000_expand_binop_builtin (((TARGET_64BIT)
11388                                            ? CODE_FOR_bpermd_di
11389                                            : CODE_FOR_bpermd_si), exp, target);
11390
11391     case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
11392     case ALTIVEC_BUILTIN_MASK_FOR_STORE:
11393       {
11394         int icode = (int) CODE_FOR_altivec_lvsr;
11395         enum machine_mode tmode = insn_data[icode].operand[0].mode;
11396         enum machine_mode mode = insn_data[icode].operand[1].mode;
11397         tree arg;
11398         rtx op, addr, pat;
11399
11400         gcc_assert (TARGET_ALTIVEC);
11401
11402         arg = CALL_EXPR_ARG (exp, 0);
11403         gcc_assert (TREE_CODE (TREE_TYPE (arg)) == POINTER_TYPE);
11404         op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
11405         addr = memory_address (mode, op);
11406         if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
11407           op = addr;
11408         else
11409           {
11410             /* For the load case need to negate the address.  */
11411             op = gen_reg_rtx (GET_MODE (addr));
11412             emit_insn (gen_rtx_SET (VOIDmode, op,
11413                                     gen_rtx_NEG (GET_MODE (addr), addr)));
11414           }
11415         op = gen_rtx_MEM (mode, op);
11416
11417         if (target == 0
11418             || GET_MODE (target) != tmode
11419             || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11420           target = gen_reg_rtx (tmode);
11421
11422         /*pat = gen_altivec_lvsr (target, op);*/
11423         pat = GEN_FCN (icode) (target, op);
11424         if (!pat)
11425           return 0;
11426         emit_insn (pat);
11427
11428         return target;
11429       }
11430
11431     case ALTIVEC_BUILTIN_VCFUX:
11432     case ALTIVEC_BUILTIN_VCFSX:
11433     case ALTIVEC_BUILTIN_VCTUXS:
11434     case ALTIVEC_BUILTIN_VCTSXS:
11435   /* FIXME: There's got to be a nicer way to handle this case than
11436      constructing a new CALL_EXPR.  */
11437       if (call_expr_nargs (exp) == 1)
11438         {
11439           exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
11440                                  2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
11441         }
11442       break;
11443
11444     default:
11445       break;
11446     }
11447
11448   if (TARGET_ALTIVEC)
11449     {
11450       ret = altivec_expand_builtin (exp, target, &success);
11451
11452       if (success)
11453         return ret;
11454     }
11455   if (TARGET_SPE)
11456     {
11457       ret = spe_expand_builtin (exp, target, &success);
11458
11459       if (success)
11460         return ret;
11461     }
11462   if (TARGET_PAIRED_FLOAT)
11463     {
11464       ret = paired_expand_builtin (exp, target, &success);
11465
11466       if (success)
11467         return ret;
11468     }  
11469
11470   gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
11471
11472   /* Handle simple unary operations.  */
11473   d = (struct builtin_description *) bdesc_1arg;
11474   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
11475     if (d->code == fcode)
11476       return rs6000_expand_unop_builtin (d->icode, exp, target);
11477
11478   /* Handle simple binary operations.  */
11479   d = (struct builtin_description *) bdesc_2arg;
11480   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11481     if (d->code == fcode)
11482       return rs6000_expand_binop_builtin (d->icode, exp, target);
11483
11484   /* Handle simple ternary operations.  */
11485   d = bdesc_3arg;
11486   for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
11487     if (d->code == fcode)
11488       return rs6000_expand_ternop_builtin (d->icode, exp, target);
11489
11490   gcc_unreachable ();
11491 }
11492
11493 static void
11494 rs6000_init_builtins (void)
11495 {
11496   tree tdecl;
11497   tree ftype;
11498
11499   V2SI_type_node = build_vector_type (intSI_type_node, 2);
11500   V2SF_type_node = build_vector_type (float_type_node, 2);
11501   V2DI_type_node = build_vector_type (intDI_type_node, 2);
11502   V2DF_type_node = build_vector_type (double_type_node, 2);
11503   V4HI_type_node = build_vector_type (intHI_type_node, 4);
11504   V4SI_type_node = build_vector_type (intSI_type_node, 4);
11505   V4SF_type_node = build_vector_type (float_type_node, 4);
11506   V8HI_type_node = build_vector_type (intHI_type_node, 8);
11507   V16QI_type_node = build_vector_type (intQI_type_node, 16);
11508
11509   unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
11510   unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
11511   unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
11512   unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
11513
11514   opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
11515   opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
11516   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
11517   opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
11518
11519   /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
11520      types, especially in C++ land.  Similarly, 'vector pixel' is distinct from
11521      'vector unsigned short'.  */
11522
11523   bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
11524   bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11525   bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
11526   bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
11527   pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11528
11529   long_integer_type_internal_node = long_integer_type_node;
11530   long_unsigned_type_internal_node = long_unsigned_type_node;
11531   intQI_type_internal_node = intQI_type_node;
11532   uintQI_type_internal_node = unsigned_intQI_type_node;
11533   intHI_type_internal_node = intHI_type_node;
11534   uintHI_type_internal_node = unsigned_intHI_type_node;
11535   intSI_type_internal_node = intSI_type_node;
11536   uintSI_type_internal_node = unsigned_intSI_type_node;
11537   intDI_type_internal_node = intDI_type_node;
11538   uintDI_type_internal_node = unsigned_intDI_type_node;
11539   float_type_internal_node = float_type_node;
11540   double_type_internal_node = float_type_node;
11541   void_type_internal_node = void_type_node;
11542
11543   /* Initialize the modes for builtin_function_type, mapping a machine mode to
11544      tree type node.  */
11545   builtin_mode_to_type[QImode][0] = integer_type_node;
11546   builtin_mode_to_type[HImode][0] = integer_type_node;
11547   builtin_mode_to_type[SImode][0] = intSI_type_node;
11548   builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
11549   builtin_mode_to_type[DImode][0] = intDI_type_node;
11550   builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
11551   builtin_mode_to_type[SFmode][0] = float_type_node;
11552   builtin_mode_to_type[DFmode][0] = double_type_node;
11553   builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
11554   builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
11555   builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
11556   builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
11557   builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
11558   builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
11559   builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
11560   builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
11561   builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
11562   builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
11563   builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
11564   builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
11565   builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
11566
11567   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11568                       get_identifier ("__bool char"),
11569                       bool_char_type_node);
11570   TYPE_NAME (bool_char_type_node) = tdecl;
11571   (*lang_hooks.decls.pushdecl) (tdecl);
11572   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11573                       get_identifier ("__bool short"),
11574                       bool_short_type_node);
11575   TYPE_NAME (bool_short_type_node) = tdecl;
11576   (*lang_hooks.decls.pushdecl) (tdecl);
11577   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11578                       get_identifier ("__bool int"),
11579                       bool_int_type_node);
11580   TYPE_NAME (bool_int_type_node) = tdecl;
11581   (*lang_hooks.decls.pushdecl) (tdecl);
11582   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, get_identifier ("__pixel"),
11583                       pixel_type_node);
11584   TYPE_NAME (pixel_type_node) = tdecl;
11585   (*lang_hooks.decls.pushdecl) (tdecl);
11586
11587   bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
11588   bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
11589   bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
11590   bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
11591   pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
11592
11593   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11594                       get_identifier ("__vector unsigned char"),
11595                       unsigned_V16QI_type_node);
11596   TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
11597   (*lang_hooks.decls.pushdecl) (tdecl);
11598   tdecl = build_decl (BUILTINS_LOCATION,
11599                       TYPE_DECL, get_identifier ("__vector signed char"),
11600                       V16QI_type_node);
11601   TYPE_NAME (V16QI_type_node) = tdecl;
11602   (*lang_hooks.decls.pushdecl) (tdecl);
11603   tdecl = build_decl (BUILTINS_LOCATION,
11604                       TYPE_DECL, get_identifier ("__vector __bool char"),
11605                       bool_V16QI_type_node);
11606   TYPE_NAME ( bool_V16QI_type_node) = tdecl;
11607   (*lang_hooks.decls.pushdecl) (tdecl);
11608
11609   tdecl = build_decl (BUILTINS_LOCATION,
11610                       TYPE_DECL, get_identifier ("__vector unsigned short"),
11611                       unsigned_V8HI_type_node);
11612   TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
11613   (*lang_hooks.decls.pushdecl) (tdecl);
11614   tdecl = build_decl (BUILTINS_LOCATION,
11615                       TYPE_DECL, get_identifier ("__vector signed short"),
11616                       V8HI_type_node);
11617   TYPE_NAME (V8HI_type_node) = tdecl;
11618   (*lang_hooks.decls.pushdecl) (tdecl);
11619   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11620                       get_identifier ("__vector __bool short"),
11621                       bool_V8HI_type_node);
11622   TYPE_NAME (bool_V8HI_type_node) = tdecl;
11623   (*lang_hooks.decls.pushdecl) (tdecl);
11624
11625   tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11626                       get_identifier ("__vector unsigned int"),
11627                       unsigned_V4SI_type_node);
11628   TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
11629   (*lang_hooks.decls.pushdecl) (tdecl);
11630   tdecl = build_decl (BUILTINS_LOCATION,
11631                       TYPE_DECL, get_identifier ("__vector signed int"),
11632                       V4SI_type_node);
11633   TYPE_NAME (V4SI_type_node) = tdecl;
11634   (*lang_hooks.decls.pushdecl) (tdecl);
11635   tdecl = build_decl (BUILTINS_LOCATION,
11636                       TYPE_DECL, get_identifier ("__vector __bool int"),
11637                       bool_V4SI_type_node);
11638   TYPE_NAME (bool_V4SI_type_node) = tdecl;
11639   (*lang_hooks.decls.pushdecl) (tdecl);
11640
11641   tdecl = build_decl (BUILTINS_LOCATION,
11642                       TYPE_DECL, get_identifier ("__vector float"),
11643                       V4SF_type_node);
11644   TYPE_NAME (V4SF_type_node) = tdecl;
11645   (*lang_hooks.decls.pushdecl) (tdecl);
11646   tdecl = build_decl (BUILTINS_LOCATION,
11647                       TYPE_DECL, get_identifier ("__vector __pixel"),
11648                       pixel_V8HI_type_node);
11649   TYPE_NAME (pixel_V8HI_type_node) = tdecl;
11650   (*lang_hooks.decls.pushdecl) (tdecl);
11651
11652   if (TARGET_VSX)
11653     {
11654       tdecl = build_decl (BUILTINS_LOCATION,
11655                           TYPE_DECL, get_identifier ("__vector double"),
11656                           V2DF_type_node);
11657       TYPE_NAME (V2DF_type_node) = tdecl;
11658       (*lang_hooks.decls.pushdecl) (tdecl);
11659
11660       tdecl = build_decl (BUILTINS_LOCATION,
11661                           TYPE_DECL, get_identifier ("__vector long"),
11662                           V2DI_type_node);
11663       TYPE_NAME (V2DI_type_node) = tdecl;
11664       (*lang_hooks.decls.pushdecl) (tdecl);
11665
11666       tdecl = build_decl (BUILTINS_LOCATION,
11667                           TYPE_DECL, get_identifier ("__vector unsigned long"),
11668                           unsigned_V2DI_type_node);
11669       TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
11670       (*lang_hooks.decls.pushdecl) (tdecl);
11671
11672       tdecl = build_decl (BUILTINS_LOCATION,
11673                           TYPE_DECL, get_identifier ("__vector __bool long"),
11674                           bool_V2DI_type_node);
11675       TYPE_NAME (bool_V2DI_type_node) = tdecl;
11676       (*lang_hooks.decls.pushdecl) (tdecl);
11677     }
11678
11679   if (TARGET_PAIRED_FLOAT)
11680     paired_init_builtins ();
11681   if (TARGET_SPE)
11682     spe_init_builtins ();
11683   if (TARGET_ALTIVEC)
11684     altivec_init_builtins ();
11685   if (TARGET_ALTIVEC || TARGET_SPE || TARGET_PAIRED_FLOAT || TARGET_VSX)
11686     rs6000_common_init_builtins ();
11687   if (TARGET_FRE)
11688     {
11689       ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
11690                                      RS6000_BUILTIN_RECIP,
11691                                      "__builtin_recipdiv");
11692       def_builtin (MASK_POPCNTB, "__builtin_recipdiv", ftype,
11693                    RS6000_BUILTIN_RECIP);
11694     }
11695   if (TARGET_FRES)
11696     {
11697       ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
11698                                      RS6000_BUILTIN_RECIPF,
11699                                      "__builtin_recipdivf");
11700       def_builtin (MASK_PPC_GFXOPT, "__builtin_recipdivf", ftype,
11701                    RS6000_BUILTIN_RECIPF);
11702     }
11703   if (TARGET_FRSQRTE)
11704     {
11705       ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
11706                                      RS6000_BUILTIN_RSQRT,
11707                                      "__builtin_rsqrt");
11708       def_builtin (MASK_PPC_GFXOPT, "__builtin_rsqrt", ftype,
11709                    RS6000_BUILTIN_RSQRT);
11710     }
11711   if (TARGET_FRSQRTES)
11712     {
11713       ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
11714                                      RS6000_BUILTIN_RSQRTF,
11715                                      "__builtin_rsqrtf");
11716       def_builtin (MASK_PPC_GFXOPT, "__builtin_rsqrtf", ftype,
11717                    RS6000_BUILTIN_RSQRTF);
11718     }
11719   if (TARGET_POPCNTD)
11720     {
11721       enum machine_mode mode = (TARGET_64BIT) ? DImode : SImode;
11722       tree ftype = builtin_function_type (mode, mode, mode, VOIDmode,
11723                                           POWER7_BUILTIN_BPERMD,
11724                                           "__builtin_bpermd");
11725       def_builtin (MASK_POPCNTD, "__builtin_bpermd", ftype,
11726                    POWER7_BUILTIN_BPERMD);
11727     }
11728   if (TARGET_POWERPC)
11729     {
11730       /* Don't use builtin_function_type here, as it maps HI/QI to SI.  */
11731       tree ftype = build_function_type_list (unsigned_intHI_type_node,
11732                                              unsigned_intHI_type_node,
11733                                              NULL_TREE);
11734       def_builtin (MASK_POWERPC, "__builtin_bswap16", ftype,
11735                    RS6000_BUILTIN_BSWAP_HI);
11736     }
11737
11738 #if TARGET_XCOFF
11739   /* AIX libm provides clog as __clog.  */
11740   if (built_in_decls [BUILT_IN_CLOG])
11741     set_user_assembler_name (built_in_decls [BUILT_IN_CLOG], "__clog");
11742 #endif
11743
11744 #ifdef SUBTARGET_INIT_BUILTINS
11745   SUBTARGET_INIT_BUILTINS;
11746 #endif
11747 }
11748
11749 /* Returns the rs6000 builtin decl for CODE.  */
11750
11751 static tree
11752 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11753 {
11754   if (code >= RS6000_BUILTIN_COUNT)
11755     return error_mark_node;
11756
11757   return rs6000_builtin_decls[code];
11758 }
11759
11760 /* Search through a set of builtins and enable the mask bits.
11761    DESC is an array of builtins.
11762    SIZE is the total number of builtins.
11763    START is the builtin enum at which to start.
11764    END is the builtin enum at which to end.  */
11765 static void
11766 enable_mask_for_builtins (struct builtin_description *desc, int size,
11767                           enum rs6000_builtins start,
11768                           enum rs6000_builtins end)
11769 {
11770   int i;
11771
11772   for (i = 0; i < size; ++i)
11773     if (desc[i].code == start)
11774       break;
11775
11776   if (i == size)
11777     return;
11778
11779   for (; i < size; ++i)
11780     {
11781       /* Flip all the bits on.  */
11782       desc[i].mask = target_flags;
11783       if (desc[i].code == end)
11784         break;
11785     }
11786 }
11787
11788 static void
11789 spe_init_builtins (void)
11790 {
11791   tree endlink = void_list_node;
11792   tree puint_type_node = build_pointer_type (unsigned_type_node);
11793   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
11794   struct builtin_description *d;
11795   size_t i;
11796
11797   tree v2si_ftype_4_v2si
11798     = build_function_type
11799     (opaque_V2SI_type_node,
11800      tree_cons (NULL_TREE, opaque_V2SI_type_node,
11801                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
11802                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
11803                                       tree_cons (NULL_TREE, opaque_V2SI_type_node,
11804                                                  endlink)))));
11805
11806   tree v2sf_ftype_4_v2sf
11807     = build_function_type
11808     (opaque_V2SF_type_node,
11809      tree_cons (NULL_TREE, opaque_V2SF_type_node,
11810                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
11811                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
11812                                       tree_cons (NULL_TREE, opaque_V2SF_type_node,
11813                                                  endlink)))));
11814
11815   tree int_ftype_int_v2si_v2si
11816     = build_function_type
11817     (integer_type_node,
11818      tree_cons (NULL_TREE, integer_type_node,
11819                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
11820                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
11821                                       endlink))));
11822
11823   tree int_ftype_int_v2sf_v2sf
11824     = build_function_type
11825     (integer_type_node,
11826      tree_cons (NULL_TREE, integer_type_node,
11827                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
11828                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
11829                                       endlink))));
11830
11831   tree void_ftype_v2si_puint_int
11832     = build_function_type (void_type_node,
11833                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
11834                                       tree_cons (NULL_TREE, puint_type_node,
11835                                                  tree_cons (NULL_TREE,
11836                                                             integer_type_node,
11837                                                             endlink))));
11838
11839   tree void_ftype_v2si_puint_char
11840     = build_function_type (void_type_node,
11841                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
11842                                       tree_cons (NULL_TREE, puint_type_node,
11843                                                  tree_cons (NULL_TREE,
11844                                                             char_type_node,
11845                                                             endlink))));
11846
11847   tree void_ftype_v2si_pv2si_int
11848     = build_function_type (void_type_node,
11849                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
11850                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
11851                                                  tree_cons (NULL_TREE,
11852                                                             integer_type_node,
11853                                                             endlink))));
11854
11855   tree void_ftype_v2si_pv2si_char
11856     = build_function_type (void_type_node,
11857                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
11858                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
11859                                                  tree_cons (NULL_TREE,
11860                                                             char_type_node,
11861                                                             endlink))));
11862
11863   tree void_ftype_int
11864     = build_function_type (void_type_node,
11865                            tree_cons (NULL_TREE, integer_type_node, endlink));
11866
11867   tree int_ftype_void
11868     = build_function_type (integer_type_node, endlink);
11869
11870   tree v2si_ftype_pv2si_int
11871     = build_function_type (opaque_V2SI_type_node,
11872                            tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
11873                                       tree_cons (NULL_TREE, integer_type_node,
11874                                                  endlink)));
11875
11876   tree v2si_ftype_puint_int
11877     = build_function_type (opaque_V2SI_type_node,
11878                            tree_cons (NULL_TREE, puint_type_node,
11879                                       tree_cons (NULL_TREE, integer_type_node,
11880                                                  endlink)));
11881
11882   tree v2si_ftype_pushort_int
11883     = build_function_type (opaque_V2SI_type_node,
11884                            tree_cons (NULL_TREE, pushort_type_node,
11885                                       tree_cons (NULL_TREE, integer_type_node,
11886                                                  endlink)));
11887
11888   tree v2si_ftype_signed_char
11889     = build_function_type (opaque_V2SI_type_node,
11890                            tree_cons (NULL_TREE, signed_char_type_node,
11891                                       endlink));
11892
11893   /* The initialization of the simple binary and unary builtins is
11894      done in rs6000_common_init_builtins, but we have to enable the
11895      mask bits here manually because we have run out of `target_flags'
11896      bits.  We really need to redesign this mask business.  */
11897
11898   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
11899                             ARRAY_SIZE (bdesc_2arg),
11900                             SPE_BUILTIN_EVADDW,
11901                             SPE_BUILTIN_EVXOR);
11902   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
11903                             ARRAY_SIZE (bdesc_1arg),
11904                             SPE_BUILTIN_EVABS,
11905                             SPE_BUILTIN_EVSUBFUSIAAW);
11906   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
11907                             ARRAY_SIZE (bdesc_spe_predicates),
11908                             SPE_BUILTIN_EVCMPEQ,
11909                             SPE_BUILTIN_EVFSTSTLT);
11910   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
11911                             ARRAY_SIZE (bdesc_spe_evsel),
11912                             SPE_BUILTIN_EVSEL_CMPGTS,
11913                             SPE_BUILTIN_EVSEL_FSTSTEQ);
11914
11915   (*lang_hooks.decls.pushdecl)
11916     (build_decl (BUILTINS_LOCATION, TYPE_DECL,
11917                  get_identifier ("__ev64_opaque__"),
11918                  opaque_V2SI_type_node));
11919
11920   /* Initialize irregular SPE builtins.  */
11921
11922   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
11923   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
11924   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
11925   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
11926   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
11927   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
11928   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
11929   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
11930   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
11931   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
11932   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
11933   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
11934   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
11935   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
11936   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
11937   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
11938   def_builtin (target_flags, "__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
11939   def_builtin (target_flags, "__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
11940
11941   /* Loads.  */
11942   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
11943   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
11944   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
11945   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
11946   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
11947   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
11948   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
11949   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
11950   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
11951   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
11952   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
11953   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
11954   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
11955   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
11956   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
11957   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
11958   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
11959   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
11960   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
11961   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
11962   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
11963   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
11964
11965   /* Predicates.  */
11966   d = (struct builtin_description *) bdesc_spe_predicates;
11967   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
11968     {
11969       tree type;
11970
11971       switch (insn_data[d->icode].operand[1].mode)
11972         {
11973         case V2SImode:
11974           type = int_ftype_int_v2si_v2si;
11975           break;
11976         case V2SFmode:
11977           type = int_ftype_int_v2sf_v2sf;
11978           break;
11979         default:
11980           gcc_unreachable ();
11981         }
11982
11983       def_builtin (d->mask, d->name, type, d->code);
11984     }
11985
11986   /* Evsel predicates.  */
11987   d = (struct builtin_description *) bdesc_spe_evsel;
11988   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
11989     {
11990       tree type;
11991
11992       switch (insn_data[d->icode].operand[1].mode)
11993         {
11994         case V2SImode:
11995           type = v2si_ftype_4_v2si;
11996           break;
11997         case V2SFmode:
11998           type = v2sf_ftype_4_v2sf;
11999           break;
12000         default:
12001           gcc_unreachable ();
12002         }
12003
12004       def_builtin (d->mask, d->name, type, d->code);
12005     }
12006 }
12007
12008 static void
12009 paired_init_builtins (void)
12010 {
12011   const struct builtin_description *d;
12012   size_t i;
12013   tree endlink = void_list_node;
12014
12015    tree int_ftype_int_v2sf_v2sf
12016     = build_function_type
12017     (integer_type_node,
12018      tree_cons (NULL_TREE, integer_type_node,
12019                 tree_cons (NULL_TREE, V2SF_type_node,
12020                            tree_cons (NULL_TREE, V2SF_type_node,
12021                                       endlink))));
12022   tree pcfloat_type_node =
12023     build_pointer_type (build_qualified_type
12024                         (float_type_node, TYPE_QUAL_CONST));
12025
12026   tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
12027                                                            long_integer_type_node,
12028                                                            pcfloat_type_node,
12029                                                            NULL_TREE);
12030   tree void_ftype_v2sf_long_pcfloat =
12031     build_function_type_list (void_type_node,
12032                               V2SF_type_node,
12033                               long_integer_type_node,
12034                               pcfloat_type_node,
12035                               NULL_TREE);
12036
12037
12038   def_builtin (0, "__builtin_paired_lx", v2sf_ftype_long_pcfloat,
12039                PAIRED_BUILTIN_LX);
12040
12041
12042   def_builtin (0, "__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
12043                PAIRED_BUILTIN_STX);
12044
12045   /* Predicates.  */
12046   d = bdesc_paired_preds;
12047   for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
12048     {
12049       tree type;
12050
12051       switch (insn_data[d->icode].operand[1].mode)
12052         {
12053         case V2SFmode:
12054           type = int_ftype_int_v2sf_v2sf;
12055           break;
12056         default:
12057           gcc_unreachable ();
12058         }
12059
12060       def_builtin (d->mask, d->name, type, d->code);
12061     }
12062 }
12063
12064 static void
12065 altivec_init_builtins (void)
12066 {
12067   const struct builtin_description *d;
12068   const struct builtin_description_predicates *dp;
12069   size_t i;
12070   tree ftype;
12071
12072   tree pfloat_type_node = build_pointer_type (float_type_node);
12073   tree pint_type_node = build_pointer_type (integer_type_node);
12074   tree pshort_type_node = build_pointer_type (short_integer_type_node);
12075   tree pchar_type_node = build_pointer_type (char_type_node);
12076
12077   tree pvoid_type_node = build_pointer_type (void_type_node);
12078
12079   tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
12080   tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
12081   tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
12082   tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
12083
12084   tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
12085
12086   tree int_ftype_opaque
12087     = build_function_type_list (integer_type_node,
12088                                 opaque_V4SI_type_node, NULL_TREE);
12089   tree opaque_ftype_opaque
12090     = build_function_type (integer_type_node,
12091                                 NULL_TREE);
12092   tree opaque_ftype_opaque_int
12093     = build_function_type_list (opaque_V4SI_type_node,
12094                                 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
12095   tree opaque_ftype_opaque_opaque_int
12096     = build_function_type_list (opaque_V4SI_type_node,
12097                                 opaque_V4SI_type_node, opaque_V4SI_type_node,
12098                                 integer_type_node, NULL_TREE);
12099   tree int_ftype_int_opaque_opaque
12100     = build_function_type_list (integer_type_node,
12101                                 integer_type_node, opaque_V4SI_type_node,
12102                                 opaque_V4SI_type_node, NULL_TREE);
12103   tree int_ftype_int_v4si_v4si
12104     = build_function_type_list (integer_type_node,
12105                                 integer_type_node, V4SI_type_node,
12106                                 V4SI_type_node, NULL_TREE);
12107   tree v4sf_ftype_pcfloat
12108     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
12109   tree void_ftype_pfloat_v4sf
12110     = build_function_type_list (void_type_node,
12111                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
12112   tree v4si_ftype_pcint
12113     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
12114   tree void_ftype_pint_v4si
12115     = build_function_type_list (void_type_node,
12116                                 pint_type_node, V4SI_type_node, NULL_TREE);
12117   tree v8hi_ftype_pcshort
12118     = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
12119   tree void_ftype_pshort_v8hi
12120     = build_function_type_list (void_type_node,
12121                                 pshort_type_node, V8HI_type_node, NULL_TREE);
12122   tree v16qi_ftype_pcchar
12123     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
12124   tree void_ftype_pchar_v16qi
12125     = build_function_type_list (void_type_node,
12126                                 pchar_type_node, V16QI_type_node, NULL_TREE);
12127   tree void_ftype_v4si
12128     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
12129   tree v8hi_ftype_void
12130     = build_function_type (V8HI_type_node, void_list_node);
12131   tree void_ftype_void
12132     = build_function_type (void_type_node, void_list_node);
12133   tree void_ftype_int
12134     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12135
12136   tree opaque_ftype_long_pcvoid
12137     = build_function_type_list (opaque_V4SI_type_node,
12138                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
12139   tree v16qi_ftype_long_pcvoid
12140     = build_function_type_list (V16QI_type_node,
12141                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
12142   tree v8hi_ftype_long_pcvoid
12143     = build_function_type_list (V8HI_type_node,
12144                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
12145   tree v4si_ftype_long_pcvoid
12146     = build_function_type_list (V4SI_type_node,
12147                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
12148
12149   tree void_ftype_opaque_long_pvoid
12150     = build_function_type_list (void_type_node,
12151                                 opaque_V4SI_type_node, long_integer_type_node,
12152                                 pvoid_type_node, NULL_TREE);
12153   tree void_ftype_v4si_long_pvoid
12154     = build_function_type_list (void_type_node,
12155                                 V4SI_type_node, long_integer_type_node,
12156                                 pvoid_type_node, NULL_TREE);
12157   tree void_ftype_v16qi_long_pvoid
12158     = build_function_type_list (void_type_node,
12159                                 V16QI_type_node, long_integer_type_node,
12160                                 pvoid_type_node, NULL_TREE);
12161   tree void_ftype_v8hi_long_pvoid
12162     = build_function_type_list (void_type_node,
12163                                 V8HI_type_node, long_integer_type_node,
12164                                 pvoid_type_node, NULL_TREE);
12165   tree int_ftype_int_v8hi_v8hi
12166     = build_function_type_list (integer_type_node,
12167                                 integer_type_node, V8HI_type_node,
12168                                 V8HI_type_node, NULL_TREE);
12169   tree int_ftype_int_v16qi_v16qi
12170     = build_function_type_list (integer_type_node,
12171                                 integer_type_node, V16QI_type_node,
12172                                 V16QI_type_node, NULL_TREE);
12173   tree int_ftype_int_v4sf_v4sf
12174     = build_function_type_list (integer_type_node,
12175                                 integer_type_node, V4SF_type_node,
12176                                 V4SF_type_node, NULL_TREE);
12177   tree int_ftype_int_v2df_v2df
12178     = build_function_type_list (integer_type_node,
12179                                 integer_type_node, V2DF_type_node,
12180                                 V2DF_type_node, NULL_TREE);
12181   tree v4si_ftype_v4si
12182     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
12183   tree v8hi_ftype_v8hi
12184     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
12185   tree v16qi_ftype_v16qi
12186     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
12187   tree v4sf_ftype_v4sf
12188     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
12189   tree v2df_ftype_v2df
12190     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
12191   tree void_ftype_pcvoid_int_int
12192     = build_function_type_list (void_type_node,
12193                                 pcvoid_type_node, integer_type_node,
12194                                 integer_type_node, NULL_TREE);
12195
12196   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
12197                ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
12198   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
12199                ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
12200   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
12201                ALTIVEC_BUILTIN_LD_INTERNAL_4si);
12202   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
12203                ALTIVEC_BUILTIN_ST_INTERNAL_4si);
12204   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
12205                ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
12206   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
12207                ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
12208   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
12209                ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
12210   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
12211                ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
12212   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
12213   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
12214   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
12215   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
12216   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
12217   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
12218   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
12219   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
12220   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
12221   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
12222   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
12223   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
12224   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
12225   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
12226   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
12227   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
12228   def_builtin (MASK_ALTIVEC, "__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
12229   def_builtin (MASK_ALTIVEC, "__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
12230   def_builtin (MASK_ALTIVEC, "__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
12231   def_builtin (MASK_ALTIVEC, "__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
12232   def_builtin (MASK_ALTIVEC, "__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
12233   def_builtin (MASK_ALTIVEC, "__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
12234   def_builtin (MASK_ALTIVEC, "__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
12235   def_builtin (MASK_ALTIVEC, "__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
12236   def_builtin (MASK_ALTIVEC, "__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
12237   def_builtin (MASK_ALTIVEC, "__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
12238   def_builtin (MASK_ALTIVEC, "__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
12239   def_builtin (MASK_ALTIVEC, "__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
12240   def_builtin (MASK_ALTIVEC, "__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
12241   def_builtin (MASK_ALTIVEC, "__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
12242
12243   if (rs6000_cpu == PROCESSOR_CELL)
12244     {
12245       def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvlx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
12246       def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
12247       def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvrx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
12248       def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
12249
12250       def_builtin (MASK_ALTIVEC, "__builtin_vec_lvlx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
12251       def_builtin (MASK_ALTIVEC, "__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
12252       def_builtin (MASK_ALTIVEC, "__builtin_vec_lvrx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
12253       def_builtin (MASK_ALTIVEC, "__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
12254
12255       def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvlx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
12256       def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
12257       def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvrx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
12258       def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
12259
12260       def_builtin (MASK_ALTIVEC, "__builtin_vec_stvlx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
12261       def_builtin (MASK_ALTIVEC, "__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
12262       def_builtin (MASK_ALTIVEC, "__builtin_vec_stvrx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
12263       def_builtin (MASK_ALTIVEC, "__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
12264     }
12265   def_builtin (MASK_ALTIVEC, "__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
12266   def_builtin (MASK_ALTIVEC, "__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
12267   def_builtin (MASK_ALTIVEC, "__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
12268
12269   def_builtin (MASK_ALTIVEC, "__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
12270   def_builtin (MASK_ALTIVEC, "__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
12271   def_builtin (MASK_ALTIVEC, "__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
12272   def_builtin (MASK_ALTIVEC, "__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
12273   def_builtin (MASK_ALTIVEC, "__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
12274   def_builtin (MASK_ALTIVEC, "__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
12275   def_builtin (MASK_ALTIVEC, "__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
12276   def_builtin (MASK_ALTIVEC, "__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
12277   def_builtin (MASK_ALTIVEC, "__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
12278   def_builtin (MASK_ALTIVEC, "__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
12279   def_builtin (MASK_ALTIVEC, "__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
12280   def_builtin (MASK_ALTIVEC, "__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
12281
12282   /* Add the DST variants.  */
12283   d = bdesc_dst;
12284   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12285     def_builtin (d->mask, d->name, void_ftype_pcvoid_int_int, d->code);
12286
12287   /* Initialize the predicates.  */
12288   dp = bdesc_altivec_preds;
12289   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
12290     {
12291       enum machine_mode mode1;
12292       tree type;
12293       bool is_overloaded = ((dp->code >= ALTIVEC_BUILTIN_OVERLOADED_FIRST
12294                              && dp->code <= ALTIVEC_BUILTIN_OVERLOADED_LAST)
12295                             || (dp->code >= VSX_BUILTIN_OVERLOADED_FIRST
12296                                 && dp->code <= VSX_BUILTIN_OVERLOADED_LAST));
12297
12298       if (is_overloaded)
12299         mode1 = VOIDmode;
12300       else
12301         mode1 = insn_data[dp->icode].operand[1].mode;
12302
12303       switch (mode1)
12304         {
12305         case VOIDmode:
12306           type = int_ftype_int_opaque_opaque;
12307           break;
12308         case V4SImode:
12309           type = int_ftype_int_v4si_v4si;
12310           break;
12311         case V8HImode:
12312           type = int_ftype_int_v8hi_v8hi;
12313           break;
12314         case V16QImode:
12315           type = int_ftype_int_v16qi_v16qi;
12316           break;
12317         case V4SFmode:
12318           type = int_ftype_int_v4sf_v4sf;
12319           break;
12320         case V2DFmode:
12321           type = int_ftype_int_v2df_v2df;
12322           break;
12323         default:
12324           gcc_unreachable ();
12325         }
12326
12327       def_builtin (dp->mask, dp->name, type, dp->code);
12328     }
12329
12330   /* Initialize the abs* operators.  */
12331   d = bdesc_abs;
12332   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12333     {
12334       enum machine_mode mode0;
12335       tree type;
12336
12337       mode0 = insn_data[d->icode].operand[0].mode;
12338
12339       switch (mode0)
12340         {
12341         case V4SImode:
12342           type = v4si_ftype_v4si;
12343           break;
12344         case V8HImode:
12345           type = v8hi_ftype_v8hi;
12346           break;
12347         case V16QImode:
12348           type = v16qi_ftype_v16qi;
12349           break;
12350         case V4SFmode:
12351           type = v4sf_ftype_v4sf;
12352           break;
12353         case V2DFmode:
12354           type = v2df_ftype_v2df;
12355           break;
12356         default:
12357           gcc_unreachable ();
12358         }
12359
12360       def_builtin (d->mask, d->name, type, d->code);
12361     }
12362
12363   if (TARGET_ALTIVEC)
12364     {
12365       tree decl;
12366
12367       /* Initialize target builtin that implements
12368          targetm.vectorize.builtin_mask_for_load.  */
12369
12370       decl = add_builtin_function ("__builtin_altivec_mask_for_load",
12371                                    v16qi_ftype_long_pcvoid,
12372                                    ALTIVEC_BUILTIN_MASK_FOR_LOAD,
12373                                    BUILT_IN_MD, NULL, NULL_TREE);
12374       TREE_READONLY (decl) = 1;
12375       /* Record the decl. Will be used by rs6000_builtin_mask_for_load.  */
12376       altivec_builtin_mask_for_load = decl;
12377     }
12378
12379   /* Access to the vec_init patterns.  */
12380   ftype = build_function_type_list (V4SI_type_node, integer_type_node,
12381                                     integer_type_node, integer_type_node,
12382                                     integer_type_node, NULL_TREE);
12383   def_builtin (MASK_ALTIVEC, "__builtin_vec_init_v4si", ftype,
12384                ALTIVEC_BUILTIN_VEC_INIT_V4SI);
12385
12386   ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
12387                                     short_integer_type_node,
12388                                     short_integer_type_node,
12389                                     short_integer_type_node,
12390                                     short_integer_type_node,
12391                                     short_integer_type_node,
12392                                     short_integer_type_node,
12393                                     short_integer_type_node, NULL_TREE);
12394   def_builtin (MASK_ALTIVEC, "__builtin_vec_init_v8hi", ftype,
12395                ALTIVEC_BUILTIN_VEC_INIT_V8HI);
12396
12397   ftype = build_function_type_list (V16QI_type_node, char_type_node,
12398                                     char_type_node, char_type_node,
12399                                     char_type_node, char_type_node,
12400                                     char_type_node, char_type_node,
12401                                     char_type_node, char_type_node,
12402                                     char_type_node, char_type_node,
12403                                     char_type_node, char_type_node,
12404                                     char_type_node, char_type_node,
12405                                     char_type_node, NULL_TREE);
12406   def_builtin (MASK_ALTIVEC, "__builtin_vec_init_v16qi", ftype,
12407                ALTIVEC_BUILTIN_VEC_INIT_V16QI);
12408
12409   ftype = build_function_type_list (V4SF_type_node, float_type_node,
12410                                     float_type_node, float_type_node,
12411                                     float_type_node, NULL_TREE);
12412   def_builtin (MASK_ALTIVEC, "__builtin_vec_init_v4sf", ftype,
12413                ALTIVEC_BUILTIN_VEC_INIT_V4SF);
12414
12415   if (TARGET_VSX)
12416     {
12417       ftype = build_function_type_list (V2DF_type_node, double_type_node,
12418                                         double_type_node, NULL_TREE);
12419       def_builtin (MASK_VSX, "__builtin_vec_init_v2df", ftype,
12420                    VSX_BUILTIN_VEC_INIT_V2DF);
12421
12422       ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
12423                                         intDI_type_node, NULL_TREE);
12424       def_builtin (MASK_VSX, "__builtin_vec_init_v2di", ftype,
12425                    VSX_BUILTIN_VEC_INIT_V2DI);
12426     }
12427
12428   /* Access to the vec_set patterns.  */
12429   ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
12430                                     intSI_type_node,
12431                                     integer_type_node, NULL_TREE);
12432   def_builtin (MASK_ALTIVEC, "__builtin_vec_set_v4si", ftype,
12433                ALTIVEC_BUILTIN_VEC_SET_V4SI);
12434
12435   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
12436                                     intHI_type_node,
12437                                     integer_type_node, NULL_TREE);
12438   def_builtin (MASK_ALTIVEC, "__builtin_vec_set_v8hi", ftype,
12439                ALTIVEC_BUILTIN_VEC_SET_V8HI);
12440
12441   ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
12442                                     intQI_type_node,
12443                                     integer_type_node, NULL_TREE);
12444   def_builtin (MASK_ALTIVEC, "__builtin_vec_set_v16qi", ftype,
12445                ALTIVEC_BUILTIN_VEC_SET_V16QI);
12446
12447   ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
12448                                     float_type_node,
12449                                     integer_type_node, NULL_TREE);
12450   def_builtin (MASK_ALTIVEC|MASK_VSX, "__builtin_vec_set_v4sf", ftype,
12451                ALTIVEC_BUILTIN_VEC_SET_V4SF);
12452
12453   if (TARGET_VSX)
12454     {
12455       ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
12456                                         double_type_node,
12457                                         integer_type_node, NULL_TREE);
12458       def_builtin (MASK_VSX, "__builtin_vec_set_v2df", ftype,
12459                    VSX_BUILTIN_VEC_SET_V2DF);
12460
12461       ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
12462                                         intDI_type_node,
12463                                         integer_type_node, NULL_TREE);
12464       def_builtin (MASK_VSX, "__builtin_vec_set_v2di", ftype,
12465                    VSX_BUILTIN_VEC_SET_V2DI);
12466     }
12467
12468   /* Access to the vec_extract patterns.  */
12469   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
12470                                     integer_type_node, NULL_TREE);
12471   def_builtin (MASK_ALTIVEC, "__builtin_vec_ext_v4si", ftype,
12472                ALTIVEC_BUILTIN_VEC_EXT_V4SI);
12473
12474   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
12475                                     integer_type_node, NULL_TREE);
12476   def_builtin (MASK_ALTIVEC, "__builtin_vec_ext_v8hi", ftype,
12477                ALTIVEC_BUILTIN_VEC_EXT_V8HI);
12478
12479   ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
12480                                     integer_type_node, NULL_TREE);
12481   def_builtin (MASK_ALTIVEC, "__builtin_vec_ext_v16qi", ftype,
12482                ALTIVEC_BUILTIN_VEC_EXT_V16QI);
12483
12484   ftype = build_function_type_list (float_type_node, V4SF_type_node,
12485                                     integer_type_node, NULL_TREE);
12486   def_builtin (MASK_ALTIVEC|MASK_VSX, "__builtin_vec_ext_v4sf", ftype,
12487                ALTIVEC_BUILTIN_VEC_EXT_V4SF);
12488
12489   if (TARGET_VSX)
12490     {
12491       ftype = build_function_type_list (double_type_node, V2DF_type_node,
12492                                         integer_type_node, NULL_TREE);
12493       def_builtin (MASK_VSX, "__builtin_vec_ext_v2df", ftype,
12494                    VSX_BUILTIN_VEC_EXT_V2DF);
12495
12496       ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
12497                                         integer_type_node, NULL_TREE);
12498       def_builtin (MASK_VSX, "__builtin_vec_ext_v2di", ftype,
12499                    VSX_BUILTIN_VEC_EXT_V2DI);
12500     }
12501 }
12502
12503 /* Hash function for builtin functions with up to 3 arguments and a return
12504    type.  */
12505 static unsigned
12506 builtin_hash_function (const void *hash_entry)
12507 {
12508   unsigned ret = 0;
12509   int i;
12510   const struct builtin_hash_struct *bh =
12511     (const struct builtin_hash_struct *) hash_entry;
12512
12513   for (i = 0; i < 4; i++)
12514     {
12515       ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
12516       ret = (ret * 2) + bh->uns_p[i];
12517     }
12518
12519   return ret;
12520 }
12521
12522 /* Compare builtin hash entries H1 and H2 for equivalence.  */
12523 static int
12524 builtin_hash_eq (const void *h1, const void *h2)
12525 {
12526   const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
12527   const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
12528
12529   return ((p1->mode[0] == p2->mode[0])
12530           && (p1->mode[1] == p2->mode[1])
12531           && (p1->mode[2] == p2->mode[2])
12532           && (p1->mode[3] == p2->mode[3])
12533           && (p1->uns_p[0] == p2->uns_p[0])
12534           && (p1->uns_p[1] == p2->uns_p[1])
12535           && (p1->uns_p[2] == p2->uns_p[2])
12536           && (p1->uns_p[3] == p2->uns_p[3]));
12537 }
12538
12539 /* Map types for builtin functions with an explicit return type and up to 3
12540    arguments.  Functions with fewer than 3 arguments use VOIDmode as the type
12541    of the argument.  */
12542 static tree
12543 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
12544                        enum machine_mode mode_arg1, enum machine_mode mode_arg2,
12545                        enum rs6000_builtins builtin, const char *name)
12546 {
12547   struct builtin_hash_struct h;
12548   struct builtin_hash_struct *h2;
12549   void **found;
12550   int num_args = 3;
12551   int i;
12552   tree ret_type = NULL_TREE;
12553   tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
12554   tree args;
12555
12556   /* Create builtin_hash_table.  */
12557   if (builtin_hash_table == NULL)
12558     builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
12559                                           builtin_hash_eq, NULL);
12560
12561   h.type = NULL_TREE;
12562   h.mode[0] = mode_ret;
12563   h.mode[1] = mode_arg0;
12564   h.mode[2] = mode_arg1;
12565   h.mode[3] = mode_arg2;
12566   h.uns_p[0] = 0;
12567   h.uns_p[1] = 0;
12568   h.uns_p[2] = 0;
12569   h.uns_p[3] = 0;
12570
12571   /* If the builtin is a type that produces unsigned results or takes unsigned
12572      arguments, and it is returned as a decl for the vectorizer (such as
12573      widening multiplies, permute), make sure the arguments and return value
12574      are type correct.  */
12575   switch (builtin)
12576     {
12577       /* unsigned 2 argument functions.  */
12578     case ALTIVEC_BUILTIN_VMULEUB_UNS:
12579     case ALTIVEC_BUILTIN_VMULEUH_UNS:
12580     case ALTIVEC_BUILTIN_VMULOUB_UNS:
12581     case ALTIVEC_BUILTIN_VMULOUH_UNS:
12582       h.uns_p[0] = 1;
12583       h.uns_p[1] = 1;
12584       h.uns_p[2] = 1;
12585       break;
12586
12587       /* unsigned 3 argument functions.  */
12588     case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
12589     case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
12590     case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
12591     case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
12592     case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
12593     case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
12594     case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
12595     case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
12596     case VSX_BUILTIN_VPERM_16QI_UNS:
12597     case VSX_BUILTIN_VPERM_8HI_UNS:
12598     case VSX_BUILTIN_VPERM_4SI_UNS:
12599     case VSX_BUILTIN_VPERM_2DI_UNS:
12600     case VSX_BUILTIN_XXSEL_16QI_UNS:
12601     case VSX_BUILTIN_XXSEL_8HI_UNS:
12602     case VSX_BUILTIN_XXSEL_4SI_UNS:
12603     case VSX_BUILTIN_XXSEL_2DI_UNS:
12604       h.uns_p[0] = 1;
12605       h.uns_p[1] = 1;
12606       h.uns_p[2] = 1;
12607       h.uns_p[3] = 1;
12608       break;
12609
12610       /* signed permute functions with unsigned char mask.  */
12611     case ALTIVEC_BUILTIN_VPERM_16QI:
12612     case ALTIVEC_BUILTIN_VPERM_8HI:
12613     case ALTIVEC_BUILTIN_VPERM_4SI:
12614     case ALTIVEC_BUILTIN_VPERM_4SF:
12615     case ALTIVEC_BUILTIN_VPERM_2DI:
12616     case ALTIVEC_BUILTIN_VPERM_2DF:
12617     case VSX_BUILTIN_VPERM_16QI:
12618     case VSX_BUILTIN_VPERM_8HI:
12619     case VSX_BUILTIN_VPERM_4SI:
12620     case VSX_BUILTIN_VPERM_4SF:
12621     case VSX_BUILTIN_VPERM_2DI:
12622     case VSX_BUILTIN_VPERM_2DF:
12623       h.uns_p[3] = 1;
12624       break;
12625
12626       /* unsigned args, signed return.  */
12627     case VSX_BUILTIN_XVCVUXDDP_UNS:
12628     case VECTOR_BUILTIN_UNSFLOAT_V4SI_V4SF:
12629       h.uns_p[1] = 1;
12630       break;
12631
12632       /* signed args, unsigned return.  */
12633     case VSX_BUILTIN_XVCVDPUXDS_UNS:
12634     case VECTOR_BUILTIN_FIXUNS_V4SF_V4SI:
12635       h.uns_p[0] = 1;
12636       break;
12637
12638     default:
12639       break;
12640     }
12641
12642   /* Figure out how many args are present.  */
12643   while (num_args > 0 && h.mode[num_args] == VOIDmode)
12644     num_args--;
12645
12646   if (num_args == 0)
12647     fatal_error ("internal error: builtin function %s had no type", name);
12648
12649   ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
12650   if (!ret_type && h.uns_p[0])
12651     ret_type = builtin_mode_to_type[h.mode[0]][0];
12652
12653   if (!ret_type)
12654     fatal_error ("internal error: builtin function %s had an unexpected "
12655                  "return type %s", name, GET_MODE_NAME (h.mode[0]));
12656
12657   for (i = 0; i < num_args; i++)
12658     {
12659       int m = (int) h.mode[i+1];
12660       int uns_p = h.uns_p[i+1];
12661
12662       arg_type[i] = builtin_mode_to_type[m][uns_p];
12663       if (!arg_type[i] && uns_p)
12664         arg_type[i] = builtin_mode_to_type[m][0];
12665
12666       if (!arg_type[i])
12667         fatal_error ("internal error: builtin function %s, argument %d "
12668                      "had unexpected argument type %s", name, i,
12669                      GET_MODE_NAME (m));
12670     }
12671
12672   found = htab_find_slot (builtin_hash_table, &h, INSERT);
12673   if (*found == NULL)
12674     {
12675       h2 = ggc_alloc_builtin_hash_struct ();
12676       *h2 = h;
12677       *found = (void *)h2;
12678       args = void_list_node;
12679
12680       for (i = num_args - 1; i >= 0; i--)
12681         args = tree_cons (NULL_TREE, arg_type[i], args);
12682
12683       h2->type = build_function_type (ret_type, args);
12684     }
12685
12686   return ((struct builtin_hash_struct *)(*found))->type;
12687 }
12688
12689 static void
12690 rs6000_common_init_builtins (void)
12691 {
12692   const struct builtin_description *d;
12693   size_t i;
12694
12695   tree opaque_ftype_opaque = NULL_TREE;
12696   tree opaque_ftype_opaque_opaque = NULL_TREE;
12697   tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
12698   tree v2si_ftype_qi = NULL_TREE;
12699   tree v2si_ftype_v2si_qi = NULL_TREE;
12700   tree v2si_ftype_int_qi = NULL_TREE;
12701
12702   if (!TARGET_PAIRED_FLOAT)
12703     {
12704       builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
12705       builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
12706     }
12707
12708   /* Add the ternary operators.  */
12709   d = bdesc_3arg;
12710   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12711     {
12712       tree type;
12713       int mask = d->mask;
12714
12715       if ((mask != 0 && (mask & target_flags) == 0)
12716           || (mask == 0 && !TARGET_PAIRED_FLOAT))
12717         continue;
12718
12719       if ((d->code >= ALTIVEC_BUILTIN_OVERLOADED_FIRST
12720            && d->code <= ALTIVEC_BUILTIN_OVERLOADED_LAST)
12721           || (d->code >= VSX_BUILTIN_OVERLOADED_FIRST
12722               && d->code <= VSX_BUILTIN_OVERLOADED_LAST))
12723         {
12724           if (! (type = opaque_ftype_opaque_opaque_opaque))
12725             type = opaque_ftype_opaque_opaque_opaque
12726               = build_function_type_list (opaque_V4SI_type_node,
12727                                           opaque_V4SI_type_node,
12728                                           opaque_V4SI_type_node,
12729                                           opaque_V4SI_type_node,
12730                                           NULL_TREE);
12731         }
12732       else
12733         {
12734           enum insn_code icode = d->icode;
12735           if (d->name == 0 || icode == CODE_FOR_nothing)
12736             continue;
12737
12738           type = builtin_function_type (insn_data[icode].operand[0].mode,
12739                                         insn_data[icode].operand[1].mode,
12740                                         insn_data[icode].operand[2].mode,
12741                                         insn_data[icode].operand[3].mode,
12742                                         d->code, d->name);
12743         }
12744
12745       def_builtin (d->mask, d->name, type, d->code);
12746     }
12747
12748   /* Add the binary operators.  */
12749   d = bdesc_2arg;
12750   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12751     {
12752       enum machine_mode mode0, mode1, mode2;
12753       tree type;
12754       int mask = d->mask;
12755
12756       if ((mask != 0 && (mask & target_flags) == 0)
12757           || (mask == 0 && !TARGET_PAIRED_FLOAT))
12758         continue;
12759
12760       if ((d->code >= ALTIVEC_BUILTIN_OVERLOADED_FIRST
12761            && d->code <= ALTIVEC_BUILTIN_OVERLOADED_LAST)
12762           || (d->code >= VSX_BUILTIN_OVERLOADED_FIRST
12763               && d->code <= VSX_BUILTIN_OVERLOADED_LAST))
12764         {
12765           if (! (type = opaque_ftype_opaque_opaque))
12766             type = opaque_ftype_opaque_opaque
12767               = build_function_type_list (opaque_V4SI_type_node,
12768                                           opaque_V4SI_type_node,
12769                                           opaque_V4SI_type_node,
12770                                           NULL_TREE);
12771         }
12772       else
12773         {
12774           enum insn_code icode = d->icode;
12775           if (d->name == 0 || icode == CODE_FOR_nothing)
12776             continue;
12777
12778           mode0 = insn_data[icode].operand[0].mode;
12779           mode1 = insn_data[icode].operand[1].mode;
12780           mode2 = insn_data[icode].operand[2].mode;
12781
12782           if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
12783             {
12784               if (! (type = v2si_ftype_v2si_qi))
12785                 type = v2si_ftype_v2si_qi
12786                   = build_function_type_list (opaque_V2SI_type_node,
12787                                               opaque_V2SI_type_node,
12788                                               char_type_node,
12789                                               NULL_TREE);
12790             }
12791
12792           else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
12793                    && mode2 == QImode)
12794             {
12795               if (! (type = v2si_ftype_int_qi))
12796                 type = v2si_ftype_int_qi
12797                   = build_function_type_list (opaque_V2SI_type_node,
12798                                               integer_type_node,
12799                                               char_type_node,
12800                                               NULL_TREE);
12801             }
12802
12803           else
12804             type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
12805                                           d->code, d->name);
12806         }
12807
12808       def_builtin (d->mask, d->name, type, d->code);
12809     }
12810
12811   /* Add the simple unary operators.  */
12812   d = (struct builtin_description *) bdesc_1arg;
12813   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12814     {
12815       enum machine_mode mode0, mode1;
12816       tree type;
12817       int mask = d->mask;
12818
12819       if ((mask != 0 && (mask & target_flags) == 0)
12820           || (mask == 0 && !TARGET_PAIRED_FLOAT))
12821         continue;
12822
12823       if ((d->code >= ALTIVEC_BUILTIN_OVERLOADED_FIRST
12824            && d->code <= ALTIVEC_BUILTIN_OVERLOADED_LAST)
12825           || (d->code >= VSX_BUILTIN_OVERLOADED_FIRST
12826               && d->code <= VSX_BUILTIN_OVERLOADED_LAST))
12827         {
12828           if (! (type = opaque_ftype_opaque))
12829             type = opaque_ftype_opaque
12830               = build_function_type_list (opaque_V4SI_type_node,
12831                                           opaque_V4SI_type_node,
12832                                           NULL_TREE);
12833         }
12834       else
12835         {
12836           enum insn_code icode = d->icode;
12837           if (d->name == 0 || icode == CODE_FOR_nothing)
12838             continue;
12839
12840           mode0 = insn_data[icode].operand[0].mode;
12841           mode1 = insn_data[icode].operand[1].mode;
12842
12843           if (mode0 == V2SImode && mode1 == QImode)
12844             {
12845               if (! (type = v2si_ftype_qi))
12846                 type = v2si_ftype_qi
12847                   = build_function_type_list (opaque_V2SI_type_node,
12848                                               char_type_node,
12849                                               NULL_TREE);
12850             }
12851
12852           else
12853             type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
12854                                           d->code, d->name);
12855         }
12856
12857       def_builtin (d->mask, d->name, type, d->code);
12858     }
12859 }
12860
12861 static void
12862 rs6000_init_libfuncs (void)
12863 {
12864   if (DEFAULT_ABI != ABI_V4 && TARGET_XCOFF
12865       && !TARGET_POWER2 && !TARGET_POWERPC)
12866     {
12867       /* AIX library routines for float->int conversion.  */
12868       set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
12869       set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
12870       set_conv_libfunc (sfix_optab, SImode, TFmode, "_qitrunc");
12871       set_conv_libfunc (ufix_optab, SImode, TFmode, "_quitrunc");
12872     }
12873
12874   if (!TARGET_IEEEQUAD)
12875       /* AIX/Darwin/64-bit Linux quad floating point routines.  */
12876     if (!TARGET_XL_COMPAT)
12877       {
12878         set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
12879         set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
12880         set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
12881         set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
12882
12883         if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
12884           {
12885             set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
12886             set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
12887             set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
12888             set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
12889             set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
12890             set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
12891             set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
12892
12893             set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
12894             set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
12895             set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
12896             set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
12897             set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
12898             set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
12899             set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
12900             set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
12901           }
12902
12903         if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
12904           set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
12905       }
12906     else
12907       {
12908         set_optab_libfunc (add_optab, TFmode, "_xlqadd");
12909         set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
12910         set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
12911         set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
12912       }
12913   else
12914     {
12915       /* 32-bit SVR4 quad floating point routines.  */
12916
12917       set_optab_libfunc (add_optab, TFmode, "_q_add");
12918       set_optab_libfunc (sub_optab, TFmode, "_q_sub");
12919       set_optab_libfunc (neg_optab, TFmode, "_q_neg");
12920       set_optab_libfunc (smul_optab, TFmode, "_q_mul");
12921       set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
12922       if (TARGET_PPC_GPOPT || TARGET_POWER2)
12923         set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
12924
12925       set_optab_libfunc (eq_optab, TFmode, "_q_feq");
12926       set_optab_libfunc (ne_optab, TFmode, "_q_fne");
12927       set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
12928       set_optab_libfunc (ge_optab, TFmode, "_q_fge");
12929       set_optab_libfunc (lt_optab, TFmode, "_q_flt");
12930       set_optab_libfunc (le_optab, TFmode, "_q_fle");
12931
12932       set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
12933       set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
12934       set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
12935       set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
12936       set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
12937       set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
12938       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
12939       set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
12940     }
12941 }
12942
12943 \f
12944 /* Expand a block clear operation, and return 1 if successful.  Return 0
12945    if we should let the compiler generate normal code.
12946
12947    operands[0] is the destination
12948    operands[1] is the length
12949    operands[3] is the alignment */
12950
12951 int
12952 expand_block_clear (rtx operands[])
12953 {
12954   rtx orig_dest = operands[0];
12955   rtx bytes_rtx = operands[1];
12956   rtx align_rtx = operands[3];
12957   bool constp   = (GET_CODE (bytes_rtx) == CONST_INT);
12958   HOST_WIDE_INT align;
12959   HOST_WIDE_INT bytes;
12960   int offset;
12961   int clear_bytes;
12962   int clear_step;
12963
12964   /* If this is not a fixed size move, just call memcpy */
12965   if (! constp)
12966     return 0;
12967
12968   /* This must be a fixed size alignment  */
12969   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12970   align = INTVAL (align_rtx) * BITS_PER_UNIT;
12971
12972   /* Anything to clear? */
12973   bytes = INTVAL (bytes_rtx);
12974   if (bytes <= 0)
12975     return 1;
12976
12977   /* Use the builtin memset after a point, to avoid huge code bloat.
12978      When optimize_size, avoid any significant code bloat; calling
12979      memset is about 4 instructions, so allow for one instruction to
12980      load zero and three to do clearing.  */
12981   if (TARGET_ALTIVEC && align >= 128)
12982     clear_step = 16;
12983   else if (TARGET_POWERPC64 && align >= 32)
12984     clear_step = 8;
12985   else if (TARGET_SPE && align >= 64)
12986     clear_step = 8;
12987   else
12988     clear_step = 4;
12989
12990   if (optimize_size && bytes > 3 * clear_step)
12991     return 0;
12992   if (! optimize_size && bytes > 8 * clear_step)
12993     return 0;
12994
12995   for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
12996     {
12997       enum machine_mode mode = BLKmode;
12998       rtx dest;
12999
13000       if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
13001         {
13002           clear_bytes = 16;
13003           mode = V4SImode;
13004         }
13005       else if (bytes >= 8 && TARGET_SPE && align >= 64)
13006         {
13007           clear_bytes = 8;
13008           mode = V2SImode;
13009         }
13010       else if (bytes >= 8 && TARGET_POWERPC64
13011                /* 64-bit loads and stores require word-aligned
13012                   displacements.  */
13013                && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
13014         {
13015           clear_bytes = 8;
13016           mode = DImode;
13017         }
13018       else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
13019         {                       /* move 4 bytes */
13020           clear_bytes = 4;
13021           mode = SImode;
13022         }
13023       else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
13024         {                       /* move 2 bytes */
13025           clear_bytes = 2;
13026           mode = HImode;
13027         }
13028       else /* move 1 byte at a time */
13029         {
13030           clear_bytes = 1;
13031           mode = QImode;
13032         }
13033
13034       dest = adjust_address (orig_dest, mode, offset);
13035
13036       emit_move_insn (dest, CONST0_RTX (mode));
13037     }
13038
13039   return 1;
13040 }
13041
13042 \f
13043 /* Expand a block move operation, and return 1 if successful.  Return 0
13044    if we should let the compiler generate normal code.
13045
13046    operands[0] is the destination
13047    operands[1] is the source
13048    operands[2] is the length
13049    operands[3] is the alignment */
13050
13051 #define MAX_MOVE_REG 4
13052
13053 int
13054 expand_block_move (rtx operands[])
13055 {
13056   rtx orig_dest = operands[0];
13057   rtx orig_src  = operands[1];
13058   rtx bytes_rtx = operands[2];
13059   rtx align_rtx = operands[3];
13060   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
13061   int align;
13062   int bytes;
13063   int offset;
13064   int move_bytes;
13065   rtx stores[MAX_MOVE_REG];
13066   int num_reg = 0;
13067
13068   /* If this is not a fixed size move, just call memcpy */
13069   if (! constp)
13070     return 0;
13071
13072   /* This must be a fixed size alignment */
13073   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
13074   align = INTVAL (align_rtx) * BITS_PER_UNIT;
13075
13076   /* Anything to move? */
13077   bytes = INTVAL (bytes_rtx);
13078   if (bytes <= 0)
13079     return 1;
13080
13081   /* store_one_arg depends on expand_block_move to handle at least the size of
13082      reg_parm_stack_space.  */
13083   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
13084     return 0;
13085
13086   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
13087     {
13088       union {
13089         rtx (*movmemsi) (rtx, rtx, rtx, rtx);
13090         rtx (*mov) (rtx, rtx);
13091       } gen_func;
13092       enum machine_mode mode = BLKmode;
13093       rtx src, dest;
13094
13095       /* Altivec first, since it will be faster than a string move
13096          when it applies, and usually not significantly larger.  */
13097       if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
13098         {
13099           move_bytes = 16;
13100           mode = V4SImode;
13101           gen_func.mov = gen_movv4si;
13102         }
13103       else if (TARGET_SPE && bytes >= 8 && align >= 64)
13104         {
13105           move_bytes = 8;
13106           mode = V2SImode;
13107           gen_func.mov = gen_movv2si;
13108         }
13109       else if (TARGET_STRING
13110           && bytes > 24         /* move up to 32 bytes at a time */
13111           && ! fixed_regs[5]
13112           && ! fixed_regs[6]
13113           && ! fixed_regs[7]
13114           && ! fixed_regs[8]
13115           && ! fixed_regs[9]
13116           && ! fixed_regs[10]
13117           && ! fixed_regs[11]
13118           && ! fixed_regs[12])
13119         {
13120           move_bytes = (bytes > 32) ? 32 : bytes;
13121           gen_func.movmemsi = gen_movmemsi_8reg;
13122         }
13123       else if (TARGET_STRING
13124                && bytes > 16    /* move up to 24 bytes at a time */
13125                && ! fixed_regs[5]
13126                && ! fixed_regs[6]
13127                && ! fixed_regs[7]
13128                && ! fixed_regs[8]
13129                && ! fixed_regs[9]
13130                && ! fixed_regs[10])
13131         {
13132           move_bytes = (bytes > 24) ? 24 : bytes;
13133           gen_func.movmemsi = gen_movmemsi_6reg;
13134         }
13135       else if (TARGET_STRING
13136                && bytes > 8     /* move up to 16 bytes at a time */
13137                && ! fixed_regs[5]
13138                && ! fixed_regs[6]
13139                && ! fixed_regs[7]
13140                && ! fixed_regs[8])
13141         {
13142           move_bytes = (bytes > 16) ? 16 : bytes;
13143           gen_func.movmemsi = gen_movmemsi_4reg;
13144         }
13145       else if (bytes >= 8 && TARGET_POWERPC64
13146                /* 64-bit loads and stores require word-aligned
13147                   displacements.  */
13148                && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
13149         {
13150           move_bytes = 8;
13151           mode = DImode;
13152           gen_func.mov = gen_movdi;
13153         }
13154       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
13155         {                       /* move up to 8 bytes at a time */
13156           move_bytes = (bytes > 8) ? 8 : bytes;
13157           gen_func.movmemsi = gen_movmemsi_2reg;
13158         }
13159       else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
13160         {                       /* move 4 bytes */
13161           move_bytes = 4;
13162           mode = SImode;
13163           gen_func.mov = gen_movsi;
13164         }
13165       else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
13166         {                       /* move 2 bytes */
13167           move_bytes = 2;
13168           mode = HImode;
13169           gen_func.mov = gen_movhi;
13170         }
13171       else if (TARGET_STRING && bytes > 1)
13172         {                       /* move up to 4 bytes at a time */
13173           move_bytes = (bytes > 4) ? 4 : bytes;
13174           gen_func.movmemsi = gen_movmemsi_1reg;
13175         }
13176       else /* move 1 byte at a time */
13177         {
13178           move_bytes = 1;
13179           mode = QImode;
13180           gen_func.mov = gen_movqi;
13181         }
13182
13183       src = adjust_address (orig_src, mode, offset);
13184       dest = adjust_address (orig_dest, mode, offset);
13185
13186       if (mode != BLKmode)
13187         {
13188           rtx tmp_reg = gen_reg_rtx (mode);
13189
13190           emit_insn ((*gen_func.mov) (tmp_reg, src));
13191           stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
13192         }
13193
13194       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
13195         {
13196           int i;
13197           for (i = 0; i < num_reg; i++)
13198             emit_insn (stores[i]);
13199           num_reg = 0;
13200         }
13201
13202       if (mode == BLKmode)
13203         {
13204           /* Move the address into scratch registers.  The movmemsi
13205              patterns require zero offset.  */
13206           if (!REG_P (XEXP (src, 0)))
13207             {
13208               rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
13209               src = replace_equiv_address (src, src_reg);
13210             }
13211           set_mem_size (src, GEN_INT (move_bytes));
13212
13213           if (!REG_P (XEXP (dest, 0)))
13214             {
13215               rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
13216               dest = replace_equiv_address (dest, dest_reg);
13217             }
13218           set_mem_size (dest, GEN_INT (move_bytes));
13219
13220           emit_insn ((*gen_func.movmemsi) (dest, src,
13221                                            GEN_INT (move_bytes & 31),
13222                                            align_rtx));
13223         }
13224     }
13225
13226   return 1;
13227 }
13228
13229 \f
13230 /* Return a string to perform a load_multiple operation.
13231    operands[0] is the vector.
13232    operands[1] is the source address.
13233    operands[2] is the first destination register.  */
13234
13235 const char *
13236 rs6000_output_load_multiple (rtx operands[3])
13237 {
13238   /* We have to handle the case where the pseudo used to contain the address
13239      is assigned to one of the output registers.  */
13240   int i, j;
13241   int words = XVECLEN (operands[0], 0);
13242   rtx xop[10];
13243
13244   if (XVECLEN (operands[0], 0) == 1)
13245     return "{l|lwz} %2,0(%1)";
13246
13247   for (i = 0; i < words; i++)
13248     if (refers_to_regno_p (REGNO (operands[2]) + i,
13249                            REGNO (operands[2]) + i + 1, operands[1], 0))
13250       {
13251         if (i == words-1)
13252           {
13253             xop[0] = GEN_INT (4 * (words-1));
13254             xop[1] = operands[1];
13255             xop[2] = operands[2];
13256             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
13257             return "";
13258           }
13259         else if (i == 0)
13260           {
13261             xop[0] = GEN_INT (4 * (words-1));
13262             xop[1] = operands[1];
13263             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
13264             output_asm_insn ("{cal %1,4(%1)|addi %1,%1,4}\n\t{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,-4(%1)", xop);
13265             return "";
13266           }
13267         else
13268           {
13269             for (j = 0; j < words; j++)
13270               if (j != i)
13271                 {
13272                   xop[0] = GEN_INT (j * 4);
13273                   xop[1] = operands[1];
13274                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
13275                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
13276                 }
13277             xop[0] = GEN_INT (i * 4);
13278             xop[1] = operands[1];
13279             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
13280             return "";
13281           }
13282       }
13283
13284   return "{lsi|lswi} %2,%1,%N0";
13285 }
13286
13287 \f
13288 /* A validation routine: say whether CODE, a condition code, and MODE
13289    match.  The other alternatives either don't make sense or should
13290    never be generated.  */
13291
13292 void
13293 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
13294 {
13295   gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
13296                || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
13297               && GET_MODE_CLASS (mode) == MODE_CC);
13298
13299   /* These don't make sense.  */
13300   gcc_assert ((code != GT && code != LT && code != GE && code != LE)
13301               || mode != CCUNSmode);
13302
13303   gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
13304               || mode == CCUNSmode);
13305
13306   gcc_assert (mode == CCFPmode
13307               || (code != ORDERED && code != UNORDERED
13308                   && code != UNEQ && code != LTGT
13309                   && code != UNGT && code != UNLT
13310                   && code != UNGE && code != UNLE));
13311
13312   /* These should never be generated except for
13313      flag_finite_math_only.  */
13314   gcc_assert (mode != CCFPmode
13315               || flag_finite_math_only
13316               || (code != LE && code != GE
13317                   && code != UNEQ && code != LTGT
13318                   && code != UNGT && code != UNLT));
13319
13320   /* These are invalid; the information is not there.  */
13321   gcc_assert (mode != CCEQmode || code == EQ || code == NE);
13322 }
13323
13324 \f
13325 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
13326    mask required to convert the result of a rotate insn into a shift
13327    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
13328
13329 int
13330 includes_lshift_p (rtx shiftop, rtx andop)
13331 {
13332   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13333
13334   shift_mask <<= INTVAL (shiftop);
13335
13336   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13337 }
13338
13339 /* Similar, but for right shift.  */
13340
13341 int
13342 includes_rshift_p (rtx shiftop, rtx andop)
13343 {
13344   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13345
13346   shift_mask >>= INTVAL (shiftop);
13347
13348   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13349 }
13350
13351 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
13352    to perform a left shift.  It must have exactly SHIFTOP least
13353    significant 0's, then one or more 1's, then zero or more 0's.  */
13354
13355 int
13356 includes_rldic_lshift_p (rtx shiftop, rtx andop)
13357 {
13358   if (GET_CODE (andop) == CONST_INT)
13359     {
13360       HOST_WIDE_INT c, lsb, shift_mask;
13361
13362       c = INTVAL (andop);
13363       if (c == 0 || c == ~0)
13364         return 0;
13365
13366       shift_mask = ~0;
13367       shift_mask <<= INTVAL (shiftop);
13368
13369       /* Find the least significant one bit.  */
13370       lsb = c & -c;
13371
13372       /* It must coincide with the LSB of the shift mask.  */
13373       if (-lsb != shift_mask)
13374         return 0;
13375
13376       /* Invert to look for the next transition (if any).  */
13377       c = ~c;
13378
13379       /* Remove the low group of ones (originally low group of zeros).  */
13380       c &= -lsb;
13381
13382       /* Again find the lsb, and check we have all 1's above.  */
13383       lsb = c & -c;
13384       return c == -lsb;
13385     }
13386   else if (GET_CODE (andop) == CONST_DOUBLE
13387            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13388     {
13389       HOST_WIDE_INT low, high, lsb;
13390       HOST_WIDE_INT shift_mask_low, shift_mask_high;
13391
13392       low = CONST_DOUBLE_LOW (andop);
13393       if (HOST_BITS_PER_WIDE_INT < 64)
13394         high = CONST_DOUBLE_HIGH (andop);
13395
13396       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
13397           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
13398         return 0;
13399
13400       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13401         {
13402           shift_mask_high = ~0;
13403           if (INTVAL (shiftop) > 32)
13404             shift_mask_high <<= INTVAL (shiftop) - 32;
13405
13406           lsb = high & -high;
13407
13408           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
13409             return 0;
13410
13411           high = ~high;
13412           high &= -lsb;
13413
13414           lsb = high & -high;
13415           return high == -lsb;
13416         }
13417
13418       shift_mask_low = ~0;
13419       shift_mask_low <<= INTVAL (shiftop);
13420
13421       lsb = low & -low;
13422
13423       if (-lsb != shift_mask_low)
13424         return 0;
13425
13426       if (HOST_BITS_PER_WIDE_INT < 64)
13427         high = ~high;
13428       low = ~low;
13429       low &= -lsb;
13430
13431       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13432         {
13433           lsb = high & -high;
13434           return high == -lsb;
13435         }
13436
13437       lsb = low & -low;
13438       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
13439     }
13440   else
13441     return 0;
13442 }
13443
13444 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
13445    to perform a left shift.  It must have SHIFTOP or more least
13446    significant 0's, with the remainder of the word 1's.  */
13447
13448 int
13449 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
13450 {
13451   if (GET_CODE (andop) == CONST_INT)
13452     {
13453       HOST_WIDE_INT c, lsb, shift_mask;
13454
13455       shift_mask = ~0;
13456       shift_mask <<= INTVAL (shiftop);
13457       c = INTVAL (andop);
13458
13459       /* Find the least significant one bit.  */
13460       lsb = c & -c;
13461
13462       /* It must be covered by the shift mask.
13463          This test also rejects c == 0.  */
13464       if ((lsb & shift_mask) == 0)
13465         return 0;
13466
13467       /* Check we have all 1's above the transition, and reject all 1's.  */
13468       return c == -lsb && lsb != 1;
13469     }
13470   else if (GET_CODE (andop) == CONST_DOUBLE
13471            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13472     {
13473       HOST_WIDE_INT low, lsb, shift_mask_low;
13474
13475       low = CONST_DOUBLE_LOW (andop);
13476
13477       if (HOST_BITS_PER_WIDE_INT < 64)
13478         {
13479           HOST_WIDE_INT high, shift_mask_high;
13480
13481           high = CONST_DOUBLE_HIGH (andop);
13482
13483           if (low == 0)
13484             {
13485               shift_mask_high = ~0;
13486               if (INTVAL (shiftop) > 32)
13487                 shift_mask_high <<= INTVAL (shiftop) - 32;
13488
13489               lsb = high & -high;
13490
13491               if ((lsb & shift_mask_high) == 0)
13492                 return 0;
13493
13494               return high == -lsb;
13495             }
13496           if (high != ~0)
13497             return 0;
13498         }
13499
13500       shift_mask_low = ~0;
13501       shift_mask_low <<= INTVAL (shiftop);
13502
13503       lsb = low & -low;
13504
13505       if ((lsb & shift_mask_low) == 0)
13506         return 0;
13507
13508       return low == -lsb && lsb != 1;
13509     }
13510   else
13511     return 0;
13512 }
13513
13514 /* Return 1 if operands will generate a valid arguments to rlwimi
13515 instruction for insert with right shift in 64-bit mode.  The mask may
13516 not start on the first bit or stop on the last bit because wrap-around
13517 effects of instruction do not correspond to semantics of RTL insn.  */
13518
13519 int
13520 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
13521 {
13522   if (INTVAL (startop) > 32
13523       && INTVAL (startop) < 64
13524       && INTVAL (sizeop) > 1
13525       && INTVAL (sizeop) + INTVAL (startop) < 64
13526       && INTVAL (shiftop) > 0
13527       && INTVAL (sizeop) + INTVAL (shiftop) < 32
13528       && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
13529     return 1;
13530
13531   return 0;
13532 }
13533
13534 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
13535    for lfq and stfq insns iff the registers are hard registers.   */
13536
13537 int
13538 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
13539 {
13540   /* We might have been passed a SUBREG.  */
13541   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
13542     return 0;
13543
13544   /* We might have been passed non floating point registers.  */
13545   if (!FP_REGNO_P (REGNO (reg1))
13546       || !FP_REGNO_P (REGNO (reg2)))
13547     return 0;
13548
13549   return (REGNO (reg1) == REGNO (reg2) - 1);
13550 }
13551
13552 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
13553    addr1 and addr2 must be in consecutive memory locations
13554    (addr2 == addr1 + 8).  */
13555
13556 int
13557 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
13558 {
13559   rtx addr1, addr2;
13560   unsigned int reg1, reg2;
13561   int offset1, offset2;
13562
13563   /* The mems cannot be volatile.  */
13564   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
13565     return 0;
13566
13567   addr1 = XEXP (mem1, 0);
13568   addr2 = XEXP (mem2, 0);
13569
13570   /* Extract an offset (if used) from the first addr.  */
13571   if (GET_CODE (addr1) == PLUS)
13572     {
13573       /* If not a REG, return zero.  */
13574       if (GET_CODE (XEXP (addr1, 0)) != REG)
13575         return 0;
13576       else
13577         {
13578           reg1 = REGNO (XEXP (addr1, 0));
13579           /* The offset must be constant!  */
13580           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
13581             return 0;
13582           offset1 = INTVAL (XEXP (addr1, 1));
13583         }
13584     }
13585   else if (GET_CODE (addr1) != REG)
13586     return 0;
13587   else
13588     {
13589       reg1 = REGNO (addr1);
13590       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
13591       offset1 = 0;
13592     }
13593
13594   /* And now for the second addr.  */
13595   if (GET_CODE (addr2) == PLUS)
13596     {
13597       /* If not a REG, return zero.  */
13598       if (GET_CODE (XEXP (addr2, 0)) != REG)
13599         return 0;
13600       else
13601         {
13602           reg2 = REGNO (XEXP (addr2, 0));
13603           /* The offset must be constant. */
13604           if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
13605             return 0;
13606           offset2 = INTVAL (XEXP (addr2, 1));
13607         }
13608     }
13609   else if (GET_CODE (addr2) != REG)
13610     return 0;
13611   else
13612     {
13613       reg2 = REGNO (addr2);
13614       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
13615       offset2 = 0;
13616     }
13617
13618   /* Both of these must have the same base register.  */
13619   if (reg1 != reg2)
13620     return 0;
13621
13622   /* The offset for the second addr must be 8 more than the first addr.  */
13623   if (offset2 != offset1 + 8)
13624     return 0;
13625
13626   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
13627      instructions.  */
13628   return 1;
13629 }
13630 \f
13631
13632 rtx
13633 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
13634 {
13635   static bool eliminated = false;
13636   rtx ret;
13637
13638   if (mode != SDmode)
13639     ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13640   else
13641     {
13642       rtx mem = cfun->machine->sdmode_stack_slot;
13643       gcc_assert (mem != NULL_RTX);
13644
13645       if (!eliminated)
13646         {
13647           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
13648           cfun->machine->sdmode_stack_slot = mem;
13649           eliminated = true;
13650         }
13651       ret = mem;
13652     }
13653
13654   if (TARGET_DEBUG_ADDR)
13655     {
13656       fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
13657                GET_MODE_NAME (mode));
13658       if (!ret)
13659         fprintf (stderr, "\tNULL_RTX\n");
13660       else
13661         debug_rtx (ret);
13662     }
13663
13664   return ret;
13665 }
13666
13667 static tree
13668 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13669 {
13670   /* Don't walk into types.  */
13671   if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
13672     {
13673       *walk_subtrees = 0;
13674       return NULL_TREE;
13675     }
13676
13677   switch (TREE_CODE (*tp))
13678     {
13679     case VAR_DECL:
13680     case PARM_DECL:
13681     case FIELD_DECL:
13682     case RESULT_DECL:
13683     case SSA_NAME:
13684     case REAL_CST:
13685     case INDIRECT_REF:
13686     case ALIGN_INDIRECT_REF:
13687     case MISALIGNED_INDIRECT_REF:
13688     case VIEW_CONVERT_EXPR:
13689       if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
13690         return *tp;
13691       break;
13692     default:
13693       break;
13694     }
13695
13696   return NULL_TREE;
13697 }
13698
13699 enum reload_reg_type {
13700   GPR_REGISTER_TYPE,
13701   VECTOR_REGISTER_TYPE,
13702   OTHER_REGISTER_TYPE
13703 };
13704
13705 static enum reload_reg_type
13706 rs6000_reload_register_type (enum reg_class rclass)
13707 {
13708   switch (rclass)
13709     {
13710     case GENERAL_REGS:
13711     case BASE_REGS:
13712       return GPR_REGISTER_TYPE;
13713
13714     case FLOAT_REGS:
13715     case ALTIVEC_REGS:
13716     case VSX_REGS:
13717       return VECTOR_REGISTER_TYPE;
13718
13719     default:
13720       return OTHER_REGISTER_TYPE;
13721     }
13722 }
13723
13724 /* Inform reload about cases where moving X with a mode MODE to a register in
13725    RCLASS requires an extra scratch or immediate register.  Return the class
13726    needed for the immediate register.
13727
13728    For VSX and Altivec, we may need a register to convert sp+offset into
13729    reg+sp.  */
13730
13731 static enum reg_class
13732 rs6000_secondary_reload (bool in_p,
13733                          rtx x,
13734                          enum reg_class rclass,
13735                          enum machine_mode mode,
13736                          secondary_reload_info *sri)
13737 {
13738   enum reg_class ret = ALL_REGS;
13739   enum insn_code icode;
13740   bool default_p = false;
13741
13742   sri->icode = CODE_FOR_nothing;
13743
13744   /* Convert vector loads and stores into gprs to use an additional base
13745      register.  */
13746   icode = rs6000_vector_reload[mode][in_p != false];
13747   if (icode != CODE_FOR_nothing)
13748     {
13749       ret = NO_REGS;
13750       sri->icode = CODE_FOR_nothing;
13751       sri->extra_cost = 0;
13752
13753       if (GET_CODE (x) == MEM)
13754         {
13755           rtx addr = XEXP (x, 0);
13756
13757           /* Loads to and stores from gprs can do reg+offset, and wouldn't need
13758              an extra register in that case, but it would need an extra
13759              register if the addressing is reg+reg or (reg+reg)&(-16).  */
13760           if (rclass == GENERAL_REGS || rclass == BASE_REGS)
13761             {
13762               if (!legitimate_indirect_address_p (addr, false)
13763                   && !rs6000_legitimate_offset_address_p (TImode, addr, false))
13764                 {
13765                   sri->icode = icode;
13766                   /* account for splitting the loads, and converting the
13767                      address from reg+reg to reg.  */
13768                   sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
13769                                      + ((GET_CODE (addr) == AND) ? 1 : 0));
13770                 }
13771             }
13772           /* Loads to and stores from vector registers can only do reg+reg
13773              addressing.  Altivec registers can also do (reg+reg)&(-16).  */
13774           else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
13775                    || rclass == FLOAT_REGS || rclass == NO_REGS)
13776             {
13777               if (!VECTOR_MEM_ALTIVEC_P (mode)
13778                   && GET_CODE (addr) == AND
13779                   && GET_CODE (XEXP (addr, 1)) == CONST_INT
13780                   && INTVAL (XEXP (addr, 1)) == -16
13781                   && (legitimate_indirect_address_p (XEXP (addr, 0), false)
13782                       || legitimate_indexed_address_p (XEXP (addr, 0), false)))
13783                 {
13784                   sri->icode = icode;
13785                   sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
13786                                      ? 2 : 1);
13787                 }
13788               else if (!legitimate_indirect_address_p (addr, false)
13789                        && (rclass == NO_REGS
13790                            || !legitimate_indexed_address_p (addr, false)))
13791                 {
13792                   sri->icode = icode;
13793                   sri->extra_cost = 1;
13794                 }
13795               else
13796                 icode = CODE_FOR_nothing;
13797             }
13798           /* Any other loads, including to pseudo registers which haven't been
13799              assigned to a register yet, default to require a scratch
13800              register.  */
13801           else
13802             {
13803               sri->icode = icode;
13804               sri->extra_cost = 2;
13805             }
13806         }
13807       else if (REG_P (x))
13808         {
13809           int regno = true_regnum (x);
13810
13811           icode = CODE_FOR_nothing;
13812           if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
13813             default_p = true;
13814           else
13815             {
13816               enum reg_class xclass = REGNO_REG_CLASS (regno);
13817               enum reload_reg_type rtype1 = rs6000_reload_register_type (rclass);
13818               enum reload_reg_type rtype2 = rs6000_reload_register_type (xclass);
13819
13820               /* If memory is needed, use default_secondary_reload to create the
13821                  stack slot.  */
13822               if (rtype1 != rtype2 || rtype1 == OTHER_REGISTER_TYPE)
13823                 default_p = true;
13824               else
13825                 ret = NO_REGS;
13826             }
13827         }
13828       else
13829         default_p = true;
13830     }
13831   else
13832     default_p = true;
13833
13834   if (default_p)
13835     ret = default_secondary_reload (in_p, x, rclass, mode, sri);
13836
13837   gcc_assert (ret != ALL_REGS);
13838
13839   if (TARGET_DEBUG_ADDR)
13840     {
13841       fprintf (stderr,
13842                "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
13843                "mode = %s",
13844                reg_class_names[ret],
13845                in_p ? "true" : "false",
13846                reg_class_names[rclass],
13847                GET_MODE_NAME (mode));
13848
13849       if (default_p)
13850         fprintf (stderr, ", default secondary reload");
13851
13852       if (sri->icode != CODE_FOR_nothing)
13853         fprintf (stderr, ", reload func = %s, extra cost = %d\n",
13854                  insn_data[sri->icode].name, sri->extra_cost);
13855       else
13856         fprintf (stderr, "\n");
13857
13858       debug_rtx (x);
13859     }
13860
13861   return ret;
13862 }
13863
13864 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
13865    to SP+reg addressing.  */
13866
13867 void
13868 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
13869 {
13870   int regno = true_regnum (reg);
13871   enum machine_mode mode = GET_MODE (reg);
13872   enum reg_class rclass;
13873   rtx addr;
13874   rtx and_op2 = NULL_RTX;
13875   rtx addr_op1;
13876   rtx addr_op2;
13877   rtx scratch_or_premodify = scratch;
13878   rtx and_rtx;
13879   rtx cc_clobber;
13880
13881   if (TARGET_DEBUG_ADDR)
13882     {
13883       fprintf (stderr, "\nrs6000_secondary_reload_inner, type = %s\n",
13884                store_p ? "store" : "load");
13885       fprintf (stderr, "reg:\n");
13886       debug_rtx (reg);
13887       fprintf (stderr, "mem:\n");
13888       debug_rtx (mem);
13889       fprintf (stderr, "scratch:\n");
13890       debug_rtx (scratch);
13891     }
13892
13893   gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
13894   gcc_assert (GET_CODE (mem) == MEM);
13895   rclass = REGNO_REG_CLASS (regno);
13896   addr = XEXP (mem, 0);
13897
13898   switch (rclass)
13899     {
13900       /* GPRs can handle reg + small constant, all other addresses need to use
13901          the scratch register.  */
13902     case GENERAL_REGS:
13903     case BASE_REGS:
13904       if (GET_CODE (addr) == AND)
13905         {
13906           and_op2 = XEXP (addr, 1);
13907           addr = XEXP (addr, 0);
13908         }
13909
13910       if (GET_CODE (addr) == PRE_MODIFY)
13911         {
13912           scratch_or_premodify = XEXP (addr, 0);
13913           gcc_assert (REG_P (scratch_or_premodify));
13914           gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13915           addr = XEXP (addr, 1);
13916         }
13917
13918       if (GET_CODE (addr) == PLUS
13919           && (!rs6000_legitimate_offset_address_p (TImode, addr, false)
13920               || and_op2 != NULL_RTX))
13921         {
13922           addr_op1 = XEXP (addr, 0);
13923           addr_op2 = XEXP (addr, 1);
13924           gcc_assert (legitimate_indirect_address_p (addr_op1, false));
13925
13926           if (!REG_P (addr_op2)
13927               && (GET_CODE (addr_op2) != CONST_INT
13928                   || !satisfies_constraint_I (addr_op2)))
13929             {
13930               if (TARGET_DEBUG_ADDR)
13931                 {
13932                   fprintf (stderr,
13933                            "\nMove plus addr to register %s, mode = %s: ",
13934                            rs6000_reg_names[REGNO (scratch)],
13935                            GET_MODE_NAME (mode));
13936                   debug_rtx (addr_op2);
13937                 }
13938               rs6000_emit_move (scratch, addr_op2, Pmode);
13939               addr_op2 = scratch;
13940             }
13941
13942           emit_insn (gen_rtx_SET (VOIDmode,
13943                                   scratch_or_premodify,
13944                                   gen_rtx_PLUS (Pmode,
13945                                                 addr_op1,
13946                                                 addr_op2)));
13947
13948           addr = scratch_or_premodify;
13949           scratch_or_premodify = scratch;
13950         }
13951       else if (!legitimate_indirect_address_p (addr, false)
13952                && !rs6000_legitimate_offset_address_p (TImode, addr, false))
13953         {
13954           if (TARGET_DEBUG_ADDR)
13955             {
13956               fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13957                        rs6000_reg_names[REGNO (scratch_or_premodify)],
13958                        GET_MODE_NAME (mode));
13959               debug_rtx (addr);
13960             }
13961           rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13962           addr = scratch_or_premodify;
13963           scratch_or_premodify = scratch;
13964         }
13965       break;
13966
13967       /* Float/Altivec registers can only handle reg+reg addressing.  Move
13968          other addresses into a scratch register.  */
13969     case FLOAT_REGS:
13970     case VSX_REGS:
13971     case ALTIVEC_REGS:
13972
13973       /* With float regs, we need to handle the AND ourselves, since we can't
13974          use the Altivec instruction with an implicit AND -16.  Allow scalar
13975          loads to float registers to use reg+offset even if VSX.  */
13976       if (GET_CODE (addr) == AND
13977           && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
13978               || GET_CODE (XEXP (addr, 1)) != CONST_INT
13979               || INTVAL (XEXP (addr, 1)) != -16
13980               || !VECTOR_MEM_ALTIVEC_P (mode)))
13981         {
13982           and_op2 = XEXP (addr, 1);
13983           addr = XEXP (addr, 0);
13984         }
13985
13986       /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
13987          as the address later.  */
13988       if (GET_CODE (addr) == PRE_MODIFY
13989           && (!VECTOR_MEM_VSX_P (mode)
13990               || and_op2 != NULL_RTX
13991               || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
13992         {
13993           scratch_or_premodify = XEXP (addr, 0);
13994           gcc_assert (legitimate_indirect_address_p (scratch_or_premodify,
13995                                                      false));
13996           gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13997           addr = XEXP (addr, 1);
13998         }
13999
14000       if (legitimate_indirect_address_p (addr, false)   /* reg */
14001           || legitimate_indexed_address_p (addr, false) /* reg+reg */
14002           || GET_CODE (addr) == PRE_MODIFY              /* VSX pre-modify */
14003           || (GET_CODE (addr) == AND                    /* Altivec memory */
14004               && GET_CODE (XEXP (addr, 1)) == CONST_INT
14005               && INTVAL (XEXP (addr, 1)) == -16
14006               && VECTOR_MEM_ALTIVEC_P (mode))
14007           || (rclass == FLOAT_REGS                      /* legacy float mem */
14008               && GET_MODE_SIZE (mode) == 8
14009               && and_op2 == NULL_RTX
14010               && scratch_or_premodify == scratch
14011               && rs6000_legitimate_offset_address_p (mode, addr, false)))
14012         ;
14013
14014       else if (GET_CODE (addr) == PLUS)
14015         {
14016           addr_op1 = XEXP (addr, 0);
14017           addr_op2 = XEXP (addr, 1);
14018           gcc_assert (REG_P (addr_op1));
14019
14020           if (TARGET_DEBUG_ADDR)
14021             {
14022               fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
14023                        rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
14024               debug_rtx (addr_op2);
14025             }
14026           rs6000_emit_move (scratch, addr_op2, Pmode);
14027           emit_insn (gen_rtx_SET (VOIDmode,
14028                                   scratch_or_premodify,
14029                                   gen_rtx_PLUS (Pmode,
14030                                                 addr_op1,
14031                                                 scratch)));
14032           addr = scratch_or_premodify;
14033           scratch_or_premodify = scratch;
14034         }
14035
14036       else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
14037                || GET_CODE (addr) == CONST_INT || REG_P (addr))
14038         {
14039           if (TARGET_DEBUG_ADDR)
14040             {
14041               fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
14042                        rs6000_reg_names[REGNO (scratch_or_premodify)],
14043                        GET_MODE_NAME (mode));
14044               debug_rtx (addr);
14045             }
14046
14047           rs6000_emit_move (scratch_or_premodify, addr, Pmode);
14048           addr = scratch_or_premodify;
14049           scratch_or_premodify = scratch;
14050         }
14051
14052       else
14053         gcc_unreachable ();
14054
14055       break;
14056
14057     default:
14058       gcc_unreachable ();
14059     }
14060
14061   /* If the original address involved a pre-modify that we couldn't use the VSX
14062      memory instruction with update, and we haven't taken care of already,
14063      store the address in the pre-modify register and use that as the
14064      address.  */
14065   if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
14066     {
14067       emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
14068       addr = scratch_or_premodify;
14069     }
14070
14071   /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
14072      memory instruction, recreate the AND now, including the clobber which is
14073      generated by the general ANDSI3/ANDDI3 patterns for the
14074      andi. instruction.  */
14075   if (and_op2 != NULL_RTX)
14076     {
14077       if (! legitimate_indirect_address_p (addr, false))
14078         {
14079           emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
14080           addr = scratch;
14081         }
14082
14083       if (TARGET_DEBUG_ADDR)
14084         {
14085           fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
14086                    rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
14087           debug_rtx (and_op2);
14088         }
14089
14090       and_rtx = gen_rtx_SET (VOIDmode,
14091                              scratch,
14092                              gen_rtx_AND (Pmode,
14093                                           addr,
14094                                           and_op2));
14095
14096       cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
14097       emit_insn (gen_rtx_PARALLEL (VOIDmode,
14098                                    gen_rtvec (2, and_rtx, cc_clobber)));
14099       addr = scratch;
14100     }
14101
14102   /* Adjust the address if it changed.  */
14103   if (addr != XEXP (mem, 0))
14104     {
14105       mem = change_address (mem, mode, addr);
14106       if (TARGET_DEBUG_ADDR)
14107         fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
14108     }
14109
14110   /* Now create the move.  */
14111   if (store_p)
14112     emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14113   else
14114     emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14115
14116   return;
14117 }
14118
14119 /* Target hook to return the cover classes for Integrated Register Allocator.
14120    Cover classes is a set of non-intersected register classes covering all hard
14121    registers used for register allocation purpose.  Any move between two
14122    registers of a cover class should be cheaper than load or store of the
14123    registers.  The value is array of register classes with LIM_REG_CLASSES used
14124    as the end marker.
14125
14126    We need two IRA_COVER_CLASSES, one for pre-VSX, and the other for VSX to
14127    account for the Altivec and Floating registers being subsets of the VSX
14128    register set under VSX, but distinct register sets on pre-VSX machines.  */
14129
14130 static const enum reg_class *
14131 rs6000_ira_cover_classes (void)
14132 {
14133   static const enum reg_class cover_pre_vsx[] = IRA_COVER_CLASSES_PRE_VSX;
14134   static const enum reg_class cover_vsx[]     = IRA_COVER_CLASSES_VSX;
14135
14136   return (TARGET_VSX) ? cover_vsx : cover_pre_vsx;
14137 }
14138
14139 /* Allocate a 64-bit stack slot to be used for copying SDmode
14140    values through if this function has any SDmode references.  */
14141
14142 static void
14143 rs6000_alloc_sdmode_stack_slot (void)
14144 {
14145   tree t;
14146   basic_block bb;
14147   gimple_stmt_iterator gsi;
14148
14149   gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
14150
14151   FOR_EACH_BB (bb)
14152     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
14153       {
14154         tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
14155         if (ret)
14156           {
14157             rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14158             cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14159                                                                   SDmode, 0);
14160             return;
14161           }
14162       }
14163
14164   /* Check for any SDmode parameters of the function.  */
14165   for (t = DECL_ARGUMENTS (cfun->decl); t; t = TREE_CHAIN (t))
14166     {
14167       if (TREE_TYPE (t) == error_mark_node)
14168         continue;
14169
14170       if (TYPE_MODE (TREE_TYPE (t)) == SDmode
14171           || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
14172         {
14173           rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14174           cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14175                                                                 SDmode, 0);
14176           return;
14177         }
14178     }
14179 }
14180
14181 static void
14182 rs6000_instantiate_decls (void)
14183 {
14184   if (cfun->machine->sdmode_stack_slot != NULL_RTX)
14185     instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
14186 }
14187
14188 /* Given an rtx X being reloaded into a reg required to be
14189    in class CLASS, return the class of reg to actually use.
14190    In general this is just CLASS; but on some machines
14191    in some cases it is preferable to use a more restrictive class.
14192
14193    On the RS/6000, we have to return NO_REGS when we want to reload a
14194    floating-point CONST_DOUBLE to force it to be copied to memory.
14195
14196    We also don't want to reload integer values into floating-point
14197    registers if we can at all help it.  In fact, this can
14198    cause reload to die, if it tries to generate a reload of CTR
14199    into a FP register and discovers it doesn't have the memory location
14200    required.
14201
14202    ??? Would it be a good idea to have reload do the converse, that is
14203    try to reload floating modes into FP registers if possible?
14204  */
14205
14206 static enum reg_class
14207 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
14208 {
14209   enum machine_mode mode = GET_MODE (x);
14210
14211   if (VECTOR_UNIT_VSX_P (mode)
14212       && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
14213     return rclass;
14214
14215   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
14216       && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
14217       && easy_vector_constant (x, mode))
14218     return ALTIVEC_REGS;
14219
14220   if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
14221     return NO_REGS;
14222
14223   if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
14224     return GENERAL_REGS;
14225
14226   /* For VSX, prefer the traditional registers for 64-bit values because we can
14227      use the non-VSX loads.  Prefer the Altivec registers if Altivec is
14228      handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
14229      prefer Altivec loads..  */
14230   if (rclass == VSX_REGS)
14231     {
14232       if (GET_MODE_SIZE (mode) <= 8)
14233         return FLOAT_REGS;
14234
14235       if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
14236         return ALTIVEC_REGS;
14237
14238       return rclass;
14239     }
14240
14241   return rclass;
14242 }
14243
14244 /* Debug version of rs6000_preferred_reload_class.  */
14245 static enum reg_class
14246 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
14247 {
14248   enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
14249
14250   fprintf (stderr,
14251            "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
14252            "mode = %s, x:\n",
14253            reg_class_names[ret], reg_class_names[rclass],
14254            GET_MODE_NAME (GET_MODE (x)));
14255   debug_rtx (x);
14256
14257   return ret;
14258 }
14259
14260 /* If we are copying between FP or AltiVec registers and anything else, we need
14261    a memory location.  The exception is when we are targeting ppc64 and the
14262    move to/from fpr to gpr instructions are available.  Also, under VSX, you
14263    can copy vector registers from the FP register set to the Altivec register
14264    set and vice versa.  */
14265
14266 static bool
14267 rs6000_secondary_memory_needed (enum reg_class class1,
14268                                 enum reg_class class2,
14269                                 enum machine_mode mode)
14270 {
14271   if (class1 == class2)
14272     return false;
14273
14274   /* Under VSX, there are 3 register classes that values could be in (VSX_REGS,
14275      ALTIVEC_REGS, and FLOAT_REGS).  We don't need to use memory to copy
14276      between these classes.  But we need memory for other things that can go in
14277      FLOAT_REGS like SFmode.  */
14278   if (TARGET_VSX
14279       && (VECTOR_MEM_VSX_P (mode) || VECTOR_UNIT_VSX_P (mode))
14280       && (class1 == VSX_REGS || class1 == ALTIVEC_REGS
14281           || class1 == FLOAT_REGS))
14282     return (class2 != VSX_REGS && class2 != ALTIVEC_REGS
14283             && class2 != FLOAT_REGS);
14284
14285   if (class1 == VSX_REGS || class2 == VSX_REGS)
14286     return true;
14287
14288   if (class1 == FLOAT_REGS
14289       && (!TARGET_MFPGPR || !TARGET_POWERPC64
14290           || ((mode != DFmode)
14291               && (mode != DDmode)
14292               && (mode != DImode))))
14293     return true;
14294
14295   if (class2 == FLOAT_REGS
14296       && (!TARGET_MFPGPR || !TARGET_POWERPC64
14297           || ((mode != DFmode)
14298               && (mode != DDmode)
14299               && (mode != DImode))))
14300     return true;
14301
14302   if (class1 == ALTIVEC_REGS || class2 == ALTIVEC_REGS)
14303     return true;
14304
14305   return false;
14306 }
14307
14308 /* Debug version of rs6000_secondary_memory_needed.  */
14309 static bool
14310 rs6000_debug_secondary_memory_needed (enum reg_class class1,
14311                                       enum reg_class class2,
14312                                       enum machine_mode mode)
14313 {
14314   bool ret = rs6000_secondary_memory_needed (class1, class2, mode);
14315
14316   fprintf (stderr,
14317            "rs6000_secondary_memory_needed, return: %s, class1 = %s, "
14318            "class2 = %s, mode = %s\n",
14319            ret ? "true" : "false", reg_class_names[class1],
14320            reg_class_names[class2], GET_MODE_NAME (mode));
14321
14322   return ret;
14323 }
14324
14325 /* Return the register class of a scratch register needed to copy IN into
14326    or out of a register in RCLASS in MODE.  If it can be done directly,
14327    NO_REGS is returned.  */
14328
14329 static enum reg_class
14330 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
14331                                rtx in)
14332 {
14333   int regno;
14334
14335   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
14336 #if TARGET_MACHO
14337                      && MACHOPIC_INDIRECT
14338 #endif
14339                      ))
14340     {
14341       /* We cannot copy a symbolic operand directly into anything
14342          other than BASE_REGS for TARGET_ELF.  So indicate that a
14343          register from BASE_REGS is needed as an intermediate
14344          register.
14345
14346          On Darwin, pic addresses require a load from memory, which
14347          needs a base register.  */
14348       if (rclass != BASE_REGS
14349           && (GET_CODE (in) == SYMBOL_REF
14350               || GET_CODE (in) == HIGH
14351               || GET_CODE (in) == LABEL_REF
14352               || GET_CODE (in) == CONST))
14353         return BASE_REGS;
14354     }
14355
14356   if (GET_CODE (in) == REG)
14357     {
14358       regno = REGNO (in);
14359       if (regno >= FIRST_PSEUDO_REGISTER)
14360         {
14361           regno = true_regnum (in);
14362           if (regno >= FIRST_PSEUDO_REGISTER)
14363             regno = -1;
14364         }
14365     }
14366   else if (GET_CODE (in) == SUBREG)
14367     {
14368       regno = true_regnum (in);
14369       if (regno >= FIRST_PSEUDO_REGISTER)
14370         regno = -1;
14371     }
14372   else
14373     regno = -1;
14374
14375   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
14376      into anything.  */
14377   if (rclass == GENERAL_REGS || rclass == BASE_REGS
14378       || (regno >= 0 && INT_REGNO_P (regno)))
14379     return NO_REGS;
14380
14381   /* Constants, memory, and FP registers can go into FP registers.  */
14382   if ((regno == -1 || FP_REGNO_P (regno))
14383       && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
14384     return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
14385
14386   /* Memory, and FP/altivec registers can go into fp/altivec registers under
14387      VSX.  */
14388   if (TARGET_VSX
14389       && (regno == -1 || VSX_REGNO_P (regno))
14390       && VSX_REG_CLASS_P (rclass))
14391     return NO_REGS;
14392
14393   /* Memory, and AltiVec registers can go into AltiVec registers.  */
14394   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
14395       && rclass == ALTIVEC_REGS)
14396     return NO_REGS;
14397
14398   /* We can copy among the CR registers.  */
14399   if ((rclass == CR_REGS || rclass == CR0_REGS)
14400       && regno >= 0 && CR_REGNO_P (regno))
14401     return NO_REGS;
14402
14403   /* Otherwise, we need GENERAL_REGS.  */
14404   return GENERAL_REGS;
14405 }
14406
14407 /* Debug version of rs6000_secondary_reload_class.  */
14408 static enum reg_class
14409 rs6000_debug_secondary_reload_class (enum reg_class rclass,
14410                                      enum machine_mode mode, rtx in)
14411 {
14412   enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
14413   fprintf (stderr,
14414            "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
14415            "mode = %s, input rtx:\n",
14416            reg_class_names[ret], reg_class_names[rclass],
14417            GET_MODE_NAME (mode));
14418   debug_rtx (in);
14419
14420   return ret;
14421 }
14422
14423 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid.  */
14424
14425 static bool
14426 rs6000_cannot_change_mode_class (enum machine_mode from,
14427                                  enum machine_mode to,
14428                                  enum reg_class rclass)
14429 {
14430   unsigned from_size = GET_MODE_SIZE (from);
14431   unsigned to_size = GET_MODE_SIZE (to);
14432
14433   if (from_size != to_size)
14434     {
14435       enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
14436       return ((from_size < 8 || to_size < 8 || TARGET_IEEEQUAD)
14437               && reg_classes_intersect_p (xclass, rclass));
14438     }
14439
14440   if (TARGET_E500_DOUBLE
14441       && ((((to) == DFmode) + ((from) == DFmode)) == 1
14442           || (((to) == TFmode) + ((from) == TFmode)) == 1
14443           || (((to) == DDmode) + ((from) == DDmode)) == 1
14444           || (((to) == TDmode) + ((from) == TDmode)) == 1
14445           || (((to) == DImode) + ((from) == DImode)) == 1))
14446     return true;
14447
14448   /* Since the VSX register set includes traditional floating point registers
14449      and altivec registers, just check for the size being different instead of
14450      trying to check whether the modes are vector modes.  Otherwise it won't
14451      allow say DF and DI to change classes.  */
14452   if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
14453     return (from_size != 8 && from_size != 16);
14454
14455   if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
14456       && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
14457     return true;
14458
14459   if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
14460       && reg_classes_intersect_p (GENERAL_REGS, rclass))
14461     return true;
14462
14463   return false;
14464 }
14465
14466 /* Debug version of rs6000_cannot_change_mode_class.  */
14467 static bool
14468 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
14469                                        enum machine_mode to,
14470                                        enum reg_class rclass)
14471 {
14472   bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
14473
14474   fprintf (stderr,
14475            "rs6000_cannot_change_mode_class, return %s, from = %s, "
14476            "to = %s, rclass = %s\n",
14477            ret ? "true" : "false",
14478            GET_MODE_NAME (from), GET_MODE_NAME (to),
14479            reg_class_names[rclass]);
14480
14481   return ret;
14482 }
14483 \f
14484 /* Given a comparison operation, return the bit number in CCR to test.  We
14485    know this is a valid comparison.
14486
14487    SCC_P is 1 if this is for an scc.  That means that %D will have been
14488    used instead of %C, so the bits will be in different places.
14489
14490    Return -1 if OP isn't a valid comparison for some reason.  */
14491
14492 int
14493 ccr_bit (rtx op, int scc_p)
14494 {
14495   enum rtx_code code = GET_CODE (op);
14496   enum machine_mode cc_mode;
14497   int cc_regnum;
14498   int base_bit;
14499   rtx reg;
14500
14501   if (!COMPARISON_P (op))
14502     return -1;
14503
14504   reg = XEXP (op, 0);
14505
14506   gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
14507
14508   cc_mode = GET_MODE (reg);
14509   cc_regnum = REGNO (reg);
14510   base_bit = 4 * (cc_regnum - CR0_REGNO);
14511
14512   validate_condition_mode (code, cc_mode);
14513
14514   /* When generating a sCOND operation, only positive conditions are
14515      allowed.  */
14516   gcc_assert (!scc_p
14517               || code == EQ || code == GT || code == LT || code == UNORDERED
14518               || code == GTU || code == LTU);
14519
14520   switch (code)
14521     {
14522     case NE:
14523       return scc_p ? base_bit + 3 : base_bit + 2;
14524     case EQ:
14525       return base_bit + 2;
14526     case GT:  case GTU:  case UNLE:
14527       return base_bit + 1;
14528     case LT:  case LTU:  case UNGE:
14529       return base_bit;
14530     case ORDERED:  case UNORDERED:
14531       return base_bit + 3;
14532
14533     case GE:  case GEU:
14534       /* If scc, we will have done a cror to put the bit in the
14535          unordered position.  So test that bit.  For integer, this is ! LT
14536          unless this is an scc insn.  */
14537       return scc_p ? base_bit + 3 : base_bit;
14538
14539     case LE:  case LEU:
14540       return scc_p ? base_bit + 3 : base_bit + 1;
14541
14542     default:
14543       gcc_unreachable ();
14544     }
14545 }
14546 \f
14547 /* Return the GOT register.  */
14548
14549 rtx
14550 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
14551 {
14552   /* The second flow pass currently (June 1999) can't update
14553      regs_ever_live without disturbing other parts of the compiler, so
14554      update it here to make the prolog/epilogue code happy.  */
14555   if (!can_create_pseudo_p ()
14556       && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
14557     df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
14558
14559   crtl->uses_pic_offset_table = 1;
14560
14561   return pic_offset_table_rtx;
14562 }
14563 \f
14564 /* Function to init struct machine_function.
14565    This will be called, via a pointer variable,
14566    from push_function_context.  */
14567
14568 static struct machine_function *
14569 rs6000_init_machine_status (void)
14570 {
14571   return ggc_alloc_cleared_machine_function ();
14572 }
14573 \f
14574 /* These macros test for integers and extract the low-order bits.  */
14575 #define INT_P(X)  \
14576 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
14577  && GET_MODE (X) == VOIDmode)
14578
14579 #define INT_LOWPART(X) \
14580   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
14581
14582 int
14583 extract_MB (rtx op)
14584 {
14585   int i;
14586   unsigned long val = INT_LOWPART (op);
14587
14588   /* If the high bit is zero, the value is the first 1 bit we find
14589      from the left.  */
14590   if ((val & 0x80000000) == 0)
14591     {
14592       gcc_assert (val & 0xffffffff);
14593
14594       i = 1;
14595       while (((val <<= 1) & 0x80000000) == 0)
14596         ++i;
14597       return i;
14598     }
14599
14600   /* If the high bit is set and the low bit is not, or the mask is all
14601      1's, the value is zero.  */
14602   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
14603     return 0;
14604
14605   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
14606      from the right.  */
14607   i = 31;
14608   while (((val >>= 1) & 1) != 0)
14609     --i;
14610
14611   return i;
14612 }
14613
14614 int
14615 extract_ME (rtx op)
14616 {
14617   int i;
14618   unsigned long val = INT_LOWPART (op);
14619
14620   /* If the low bit is zero, the value is the first 1 bit we find from
14621      the right.  */
14622   if ((val & 1) == 0)
14623     {
14624       gcc_assert (val & 0xffffffff);
14625
14626       i = 30;
14627       while (((val >>= 1) & 1) == 0)
14628         --i;
14629
14630       return i;
14631     }
14632
14633   /* If the low bit is set and the high bit is not, or the mask is all
14634      1's, the value is 31.  */
14635   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
14636     return 31;
14637
14638   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
14639      from the left.  */
14640   i = 0;
14641   while (((val <<= 1) & 0x80000000) != 0)
14642     ++i;
14643
14644   return i;
14645 }
14646
14647 /* Locate some local-dynamic symbol still in use by this function
14648    so that we can print its name in some tls_ld pattern.  */
14649
14650 static const char *
14651 rs6000_get_some_local_dynamic_name (void)
14652 {
14653   rtx insn;
14654
14655   if (cfun->machine->some_ld_name)
14656     return cfun->machine->some_ld_name;
14657
14658   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
14659     if (INSN_P (insn)
14660         && for_each_rtx (&PATTERN (insn),
14661                          rs6000_get_some_local_dynamic_name_1, 0))
14662       return cfun->machine->some_ld_name;
14663
14664   gcc_unreachable ();
14665 }
14666
14667 /* Helper function for rs6000_get_some_local_dynamic_name.  */
14668
14669 static int
14670 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
14671 {
14672   rtx x = *px;
14673
14674   if (GET_CODE (x) == SYMBOL_REF)
14675     {
14676       const char *str = XSTR (x, 0);
14677       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
14678         {
14679           cfun->machine->some_ld_name = str;
14680           return 1;
14681         }
14682     }
14683
14684   return 0;
14685 }
14686
14687 /* Write out a function code label.  */
14688
14689 void
14690 rs6000_output_function_entry (FILE *file, const char *fname)
14691 {
14692   if (fname[0] != '.')
14693     {
14694       switch (DEFAULT_ABI)
14695         {
14696         default:
14697           gcc_unreachable ();
14698
14699         case ABI_AIX:
14700           if (DOT_SYMBOLS)
14701             putc ('.', file);
14702           else
14703             ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
14704           break;
14705
14706         case ABI_V4:
14707         case ABI_DARWIN:
14708           break;
14709         }
14710     }
14711
14712   RS6000_OUTPUT_BASENAME (file, fname);
14713 }
14714
14715 /* Print an operand.  Recognize special options, documented below.  */
14716
14717 #if TARGET_ELF
14718 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
14719 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
14720 #else
14721 #define SMALL_DATA_RELOC "sda21"
14722 #define SMALL_DATA_REG 0
14723 #endif
14724
14725 void
14726 print_operand (FILE *file, rtx x, int code)
14727 {
14728   int i;
14729   HOST_WIDE_INT val;
14730   unsigned HOST_WIDE_INT uval;
14731
14732   switch (code)
14733     {
14734     case '.':
14735       /* Write out an instruction after the call which may be replaced
14736          with glue code by the loader.  This depends on the AIX version.  */
14737       asm_fprintf (file, RS6000_CALL_GLUE);
14738       return;
14739
14740       /* %a is output_address.  */
14741
14742     case 'A':
14743       /* If X is a constant integer whose low-order 5 bits are zero,
14744          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
14745          in the AIX assembler where "sri" with a zero shift count
14746          writes a trash instruction.  */
14747       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
14748         putc ('l', file);
14749       else
14750         putc ('r', file);
14751       return;
14752
14753     case 'b':
14754       /* If constant, low-order 16 bits of constant, unsigned.
14755          Otherwise, write normally.  */
14756       if (INT_P (x))
14757         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
14758       else
14759         print_operand (file, x, 0);
14760       return;
14761
14762     case 'B':
14763       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
14764          for 64-bit mask direction.  */
14765       putc (((INT_LOWPART (x) & 1) == 0 ? 'r' : 'l'), file);
14766       return;
14767
14768       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
14769          output_operand.  */
14770
14771     case 'c':
14772       /* X is a CR register.  Print the number of the GT bit of the CR.  */
14773       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14774         output_operand_lossage ("invalid %%c value");
14775       else
14776         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 1);
14777       return;
14778
14779     case 'D':
14780       /* Like 'J' but get to the GT bit only.  */
14781       gcc_assert (GET_CODE (x) == REG);
14782
14783       /* Bit 1 is GT bit.  */
14784       i = 4 * (REGNO (x) - CR0_REGNO) + 1;
14785
14786       /* Add one for shift count in rlinm for scc.  */
14787       fprintf (file, "%d", i + 1);
14788       return;
14789
14790     case 'E':
14791       /* X is a CR register.  Print the number of the EQ bit of the CR */
14792       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14793         output_operand_lossage ("invalid %%E value");
14794       else
14795         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
14796       return;
14797
14798     case 'f':
14799       /* X is a CR register.  Print the shift count needed to move it
14800          to the high-order four bits.  */
14801       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14802         output_operand_lossage ("invalid %%f value");
14803       else
14804         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
14805       return;
14806
14807     case 'F':
14808       /* Similar, but print the count for the rotate in the opposite
14809          direction.  */
14810       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14811         output_operand_lossage ("invalid %%F value");
14812       else
14813         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
14814       return;
14815
14816     case 'G':
14817       /* X is a constant integer.  If it is negative, print "m",
14818          otherwise print "z".  This is to make an aze or ame insn.  */
14819       if (GET_CODE (x) != CONST_INT)
14820         output_operand_lossage ("invalid %%G value");
14821       else if (INTVAL (x) >= 0)
14822         putc ('z', file);
14823       else
14824         putc ('m', file);
14825       return;
14826
14827     case 'h':
14828       /* If constant, output low-order five bits.  Otherwise, write
14829          normally.  */
14830       if (INT_P (x))
14831         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
14832       else
14833         print_operand (file, x, 0);
14834       return;
14835
14836     case 'H':
14837       /* If constant, output low-order six bits.  Otherwise, write
14838          normally.  */
14839       if (INT_P (x))
14840         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
14841       else
14842         print_operand (file, x, 0);
14843       return;
14844
14845     case 'I':
14846       /* Print `i' if this is a constant, else nothing.  */
14847       if (INT_P (x))
14848         putc ('i', file);
14849       return;
14850
14851     case 'j':
14852       /* Write the bit number in CCR for jump.  */
14853       i = ccr_bit (x, 0);
14854       if (i == -1)
14855         output_operand_lossage ("invalid %%j code");
14856       else
14857         fprintf (file, "%d", i);
14858       return;
14859
14860     case 'J':
14861       /* Similar, but add one for shift count in rlinm for scc and pass
14862          scc flag to `ccr_bit'.  */
14863       i = ccr_bit (x, 1);
14864       if (i == -1)
14865         output_operand_lossage ("invalid %%J code");
14866       else
14867         /* If we want bit 31, write a shift count of zero, not 32.  */
14868         fprintf (file, "%d", i == 31 ? 0 : i + 1);
14869       return;
14870
14871     case 'k':
14872       /* X must be a constant.  Write the 1's complement of the
14873          constant.  */
14874       if (! INT_P (x))
14875         output_operand_lossage ("invalid %%k value");
14876       else
14877         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
14878       return;
14879
14880     case 'K':
14881       /* X must be a symbolic constant on ELF.  Write an
14882          expression suitable for an 'addi' that adds in the low 16
14883          bits of the MEM.  */
14884       if (GET_CODE (x) == CONST)
14885         {
14886           if (GET_CODE (XEXP (x, 0)) != PLUS
14887               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
14888                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
14889               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
14890             output_operand_lossage ("invalid %%K value");
14891         }
14892       print_operand_address (file, x);
14893       fputs ("@l", file);
14894       return;
14895
14896       /* %l is output_asm_label.  */
14897
14898     case 'L':
14899       /* Write second word of DImode or DFmode reference.  Works on register
14900          or non-indexed memory only.  */
14901       if (GET_CODE (x) == REG)
14902         fputs (reg_names[REGNO (x) + 1], file);
14903       else if (GET_CODE (x) == MEM)
14904         {
14905           /* Handle possible auto-increment.  Since it is pre-increment and
14906              we have already done it, we can just use an offset of word.  */
14907           if (GET_CODE (XEXP (x, 0)) == PRE_INC
14908               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
14909             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
14910                                            UNITS_PER_WORD));
14911           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
14912             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
14913                                            UNITS_PER_WORD));
14914           else
14915             output_address (XEXP (adjust_address_nv (x, SImode,
14916                                                      UNITS_PER_WORD),
14917                                   0));
14918
14919           if (small_data_operand (x, GET_MODE (x)))
14920             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
14921                      reg_names[SMALL_DATA_REG]);
14922         }
14923       return;
14924
14925     case 'm':
14926       /* MB value for a mask operand.  */
14927       if (! mask_operand (x, SImode))
14928         output_operand_lossage ("invalid %%m value");
14929
14930       fprintf (file, "%d", extract_MB (x));
14931       return;
14932
14933     case 'M':
14934       /* ME value for a mask operand.  */
14935       if (! mask_operand (x, SImode))
14936         output_operand_lossage ("invalid %%M value");
14937
14938       fprintf (file, "%d", extract_ME (x));
14939       return;
14940
14941       /* %n outputs the negative of its operand.  */
14942
14943     case 'N':
14944       /* Write the number of elements in the vector times 4.  */
14945       if (GET_CODE (x) != PARALLEL)
14946         output_operand_lossage ("invalid %%N value");
14947       else
14948         fprintf (file, "%d", XVECLEN (x, 0) * 4);
14949       return;
14950
14951     case 'O':
14952       /* Similar, but subtract 1 first.  */
14953       if (GET_CODE (x) != PARALLEL)
14954         output_operand_lossage ("invalid %%O value");
14955       else
14956         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
14957       return;
14958
14959     case 'p':
14960       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
14961       if (! INT_P (x)
14962           || INT_LOWPART (x) < 0
14963           || (i = exact_log2 (INT_LOWPART (x))) < 0)
14964         output_operand_lossage ("invalid %%p value");
14965       else
14966         fprintf (file, "%d", i);
14967       return;
14968
14969     case 'P':
14970       /* The operand must be an indirect memory reference.  The result
14971          is the register name.  */
14972       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
14973           || REGNO (XEXP (x, 0)) >= 32)
14974         output_operand_lossage ("invalid %%P value");
14975       else
14976         fputs (reg_names[REGNO (XEXP (x, 0))], file);
14977       return;
14978
14979     case 'q':
14980       /* This outputs the logical code corresponding to a boolean
14981          expression.  The expression may have one or both operands
14982          negated (if one, only the first one).  For condition register
14983          logical operations, it will also treat the negated
14984          CR codes as NOTs, but not handle NOTs of them.  */
14985       {
14986         const char *const *t = 0;
14987         const char *s;
14988         enum rtx_code code = GET_CODE (x);
14989         static const char * const tbl[3][3] = {
14990           { "and", "andc", "nor" },
14991           { "or", "orc", "nand" },
14992           { "xor", "eqv", "xor" } };
14993
14994         if (code == AND)
14995           t = tbl[0];
14996         else if (code == IOR)
14997           t = tbl[1];
14998         else if (code == XOR)
14999           t = tbl[2];
15000         else
15001           output_operand_lossage ("invalid %%q value");
15002
15003         if (GET_CODE (XEXP (x, 0)) != NOT)
15004           s = t[0];
15005         else
15006           {
15007             if (GET_CODE (XEXP (x, 1)) == NOT)
15008               s = t[2];
15009             else
15010               s = t[1];
15011           }
15012
15013         fputs (s, file);
15014       }
15015       return;
15016
15017     case 'Q':
15018       if (TARGET_MFCRF)
15019         fputc (',', file);
15020         /* FALLTHRU */
15021       else
15022         return;
15023
15024     case 'R':
15025       /* X is a CR register.  Print the mask for `mtcrf'.  */
15026       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
15027         output_operand_lossage ("invalid %%R value");
15028       else
15029         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
15030       return;
15031
15032     case 's':
15033       /* Low 5 bits of 32 - value */
15034       if (! INT_P (x))
15035         output_operand_lossage ("invalid %%s value");
15036       else
15037         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
15038       return;
15039
15040     case 'S':
15041       /* PowerPC64 mask position.  All 0's is excluded.
15042          CONST_INT 32-bit mask is considered sign-extended so any
15043          transition must occur within the CONST_INT, not on the boundary.  */
15044       if (! mask64_operand (x, DImode))
15045         output_operand_lossage ("invalid %%S value");
15046
15047       uval = INT_LOWPART (x);
15048
15049       if (uval & 1)     /* Clear Left */
15050         {
15051 #if HOST_BITS_PER_WIDE_INT > 64
15052           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
15053 #endif
15054           i = 64;
15055         }
15056       else              /* Clear Right */
15057         {
15058           uval = ~uval;
15059 #if HOST_BITS_PER_WIDE_INT > 64
15060           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
15061 #endif
15062           i = 63;
15063         }
15064       while (uval != 0)
15065         --i, uval >>= 1;
15066       gcc_assert (i >= 0);
15067       fprintf (file, "%d", i);
15068       return;
15069
15070     case 't':
15071       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
15072       gcc_assert (GET_CODE (x) == REG && GET_MODE (x) == CCmode);
15073
15074       /* Bit 3 is OV bit.  */
15075       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
15076
15077       /* If we want bit 31, write a shift count of zero, not 32.  */
15078       fprintf (file, "%d", i == 31 ? 0 : i + 1);
15079       return;
15080
15081     case 'T':
15082       /* Print the symbolic name of a branch target register.  */
15083       if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
15084                                   && REGNO (x) != CTR_REGNO))
15085         output_operand_lossage ("invalid %%T value");
15086       else if (REGNO (x) == LR_REGNO)
15087         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
15088       else
15089         fputs ("ctr", file);
15090       return;
15091
15092     case 'u':
15093       /* High-order 16 bits of constant for use in unsigned operand.  */
15094       if (! INT_P (x))
15095         output_operand_lossage ("invalid %%u value");
15096       else
15097         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15098                  (INT_LOWPART (x) >> 16) & 0xffff);
15099       return;
15100
15101     case 'v':
15102       /* High-order 16 bits of constant for use in signed operand.  */
15103       if (! INT_P (x))
15104         output_operand_lossage ("invalid %%v value");
15105       else
15106         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15107                  (INT_LOWPART (x) >> 16) & 0xffff);
15108       return;
15109
15110     case 'U':
15111       /* Print `u' if this has an auto-increment or auto-decrement.  */
15112       if (GET_CODE (x) == MEM
15113           && (GET_CODE (XEXP (x, 0)) == PRE_INC
15114               || GET_CODE (XEXP (x, 0)) == PRE_DEC
15115               || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
15116         putc ('u', file);
15117       return;
15118
15119     case 'V':
15120       /* Print the trap code for this operand.  */
15121       switch (GET_CODE (x))
15122         {
15123         case EQ:
15124           fputs ("eq", file);   /* 4 */
15125           break;
15126         case NE:
15127           fputs ("ne", file);   /* 24 */
15128           break;
15129         case LT:
15130           fputs ("lt", file);   /* 16 */
15131           break;
15132         case LE:
15133           fputs ("le", file);   /* 20 */
15134           break;
15135         case GT:
15136           fputs ("gt", file);   /* 8 */
15137           break;
15138         case GE:
15139           fputs ("ge", file);   /* 12 */
15140           break;
15141         case LTU:
15142           fputs ("llt", file);  /* 2 */
15143           break;
15144         case LEU:
15145           fputs ("lle", file);  /* 6 */
15146           break;
15147         case GTU:
15148           fputs ("lgt", file);  /* 1 */
15149           break;
15150         case GEU:
15151           fputs ("lge", file);  /* 5 */
15152           break;
15153         default:
15154           gcc_unreachable ();
15155         }
15156       break;
15157
15158     case 'w':
15159       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
15160          normally.  */
15161       if (INT_P (x))
15162         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
15163                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
15164       else
15165         print_operand (file, x, 0);
15166       return;
15167
15168     case 'W':
15169       /* MB value for a PowerPC64 rldic operand.  */
15170       val = (GET_CODE (x) == CONST_INT
15171              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
15172
15173       if (val < 0)
15174         i = -1;
15175       else
15176         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
15177           if ((val <<= 1) < 0)
15178             break;
15179
15180 #if HOST_BITS_PER_WIDE_INT == 32
15181       if (GET_CODE (x) == CONST_INT && i >= 0)
15182         i += 32;  /* zero-extend high-part was all 0's */
15183       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
15184         {
15185           val = CONST_DOUBLE_LOW (x);
15186
15187           gcc_assert (val);
15188           if (val < 0)
15189             --i;
15190           else
15191             for ( ; i < 64; i++)
15192               if ((val <<= 1) < 0)
15193                 break;
15194         }
15195 #endif
15196
15197       fprintf (file, "%d", i + 1);
15198       return;
15199
15200     case 'x':
15201       /* X is a FPR or Altivec register used in a VSX context.  */
15202       if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
15203         output_operand_lossage ("invalid %%x value");
15204       else
15205         {
15206           int reg = REGNO (x);
15207           int vsx_reg = (FP_REGNO_P (reg)
15208                          ? reg - 32
15209                          : reg - FIRST_ALTIVEC_REGNO + 32);
15210
15211 #ifdef TARGET_REGNAMES      
15212           if (TARGET_REGNAMES)
15213             fprintf (file, "%%vs%d", vsx_reg);
15214           else
15215 #endif
15216             fprintf (file, "%d", vsx_reg);
15217         }
15218       return;
15219
15220     case 'X':
15221       if (GET_CODE (x) == MEM
15222           && (legitimate_indexed_address_p (XEXP (x, 0), 0)
15223               || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
15224                   && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
15225         putc ('x', file);
15226       return;
15227
15228     case 'Y':
15229       /* Like 'L', for third word of TImode  */
15230       if (GET_CODE (x) == REG)
15231         fputs (reg_names[REGNO (x) + 2], file);
15232       else if (GET_CODE (x) == MEM)
15233         {
15234           if (GET_CODE (XEXP (x, 0)) == PRE_INC
15235               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15236             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
15237           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15238             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
15239           else
15240             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
15241           if (small_data_operand (x, GET_MODE (x)))
15242             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15243                      reg_names[SMALL_DATA_REG]);
15244         }
15245       return;
15246
15247     case 'z':
15248       /* X is a SYMBOL_REF.  Write out the name preceded by a
15249          period and without any trailing data in brackets.  Used for function
15250          names.  If we are configured for System V (or the embedded ABI) on
15251          the PowerPC, do not emit the period, since those systems do not use
15252          TOCs and the like.  */
15253       gcc_assert (GET_CODE (x) == SYMBOL_REF);
15254
15255       /* Mark the decl as referenced so that cgraph will output the
15256          function.  */
15257       if (SYMBOL_REF_DECL (x))
15258         mark_decl_referenced (SYMBOL_REF_DECL (x));
15259
15260       /* For macho, check to see if we need a stub.  */
15261       if (TARGET_MACHO)
15262         {
15263           const char *name = XSTR (x, 0);
15264 #if TARGET_MACHO
15265           if (MACHOPIC_INDIRECT
15266               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
15267             name = machopic_indirection_name (x, /*stub_p=*/true);
15268 #endif
15269           assemble_name (file, name);
15270         }
15271       else if (!DOT_SYMBOLS)
15272         assemble_name (file, XSTR (x, 0));
15273       else
15274         rs6000_output_function_entry (file, XSTR (x, 0));
15275       return;
15276
15277     case 'Z':
15278       /* Like 'L', for last word of TImode.  */
15279       if (GET_CODE (x) == REG)
15280         fputs (reg_names[REGNO (x) + 3], file);
15281       else if (GET_CODE (x) == MEM)
15282         {
15283           if (GET_CODE (XEXP (x, 0)) == PRE_INC
15284               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15285             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
15286           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15287             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
15288           else
15289             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
15290           if (small_data_operand (x, GET_MODE (x)))
15291             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15292                      reg_names[SMALL_DATA_REG]);
15293         }
15294       return;
15295
15296       /* Print AltiVec or SPE memory operand.  */
15297     case 'y':
15298       {
15299         rtx tmp;
15300
15301         gcc_assert (GET_CODE (x) == MEM);
15302
15303         tmp = XEXP (x, 0);
15304
15305         /* Ugly hack because %y is overloaded.  */
15306         if ((TARGET_SPE || TARGET_E500_DOUBLE)
15307             && (GET_MODE_SIZE (GET_MODE (x)) == 8
15308                 || GET_MODE (x) == TFmode
15309                 || GET_MODE (x) == TImode))
15310           {
15311             /* Handle [reg].  */
15312             if (GET_CODE (tmp) == REG)
15313               {
15314                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
15315                 break;
15316               }
15317             /* Handle [reg+UIMM].  */
15318             else if (GET_CODE (tmp) == PLUS &&
15319                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
15320               {
15321                 int x;
15322
15323                 gcc_assert (GET_CODE (XEXP (tmp, 0)) == REG);
15324
15325                 x = INTVAL (XEXP (tmp, 1));
15326                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
15327                 break;
15328               }
15329
15330             /* Fall through.  Must be [reg+reg].  */
15331           }
15332         if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
15333             && GET_CODE (tmp) == AND
15334             && GET_CODE (XEXP (tmp, 1)) == CONST_INT
15335             && INTVAL (XEXP (tmp, 1)) == -16)
15336           tmp = XEXP (tmp, 0);
15337         else if (VECTOR_MEM_VSX_P (GET_MODE (x))
15338                  && GET_CODE (tmp) == PRE_MODIFY)
15339           tmp = XEXP (tmp, 1);
15340         if (GET_CODE (tmp) == REG)
15341           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
15342         else
15343           {
15344             if (!GET_CODE (tmp) == PLUS
15345                 || !REG_P (XEXP (tmp, 0))
15346                 || !REG_P (XEXP (tmp, 1)))
15347               {
15348                 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
15349                 break;
15350               }
15351
15352             if (REGNO (XEXP (tmp, 0)) == 0)
15353               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
15354                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
15355             else
15356               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
15357                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
15358           }
15359         break;
15360       }
15361
15362     case 0:
15363       if (GET_CODE (x) == REG)
15364         fprintf (file, "%s", reg_names[REGNO (x)]);
15365       else if (GET_CODE (x) == MEM)
15366         {
15367           /* We need to handle PRE_INC and PRE_DEC here, since we need to
15368              know the width from the mode.  */
15369           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
15370             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
15371                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15372           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
15373             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
15374                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15375           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15376             output_address (XEXP (XEXP (x, 0), 1));
15377           else
15378             output_address (XEXP (x, 0));
15379         }
15380       else
15381         output_addr_const (file, x);
15382       return;
15383
15384     case '&':
15385       assemble_name (file, rs6000_get_some_local_dynamic_name ());
15386       return;
15387
15388     default:
15389       output_operand_lossage ("invalid %%xn code");
15390     }
15391 }
15392 \f
15393 /* Print the address of an operand.  */
15394
15395 void
15396 print_operand_address (FILE *file, rtx x)
15397 {
15398   if (GET_CODE (x) == REG)
15399     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
15400   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
15401            || GET_CODE (x) == LABEL_REF)
15402     {
15403       output_addr_const (file, x);
15404       if (small_data_operand (x, GET_MODE (x)))
15405         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15406                  reg_names[SMALL_DATA_REG]);
15407       else
15408         gcc_assert (!TARGET_TOC);
15409     }
15410   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
15411     {
15412       gcc_assert (REG_P (XEXP (x, 0)));
15413       if (REGNO (XEXP (x, 0)) == 0)
15414         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
15415                  reg_names[ REGNO (XEXP (x, 0)) ]);
15416       else
15417         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
15418                  reg_names[ REGNO (XEXP (x, 1)) ]);
15419     }
15420   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
15421     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
15422              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
15423 #if TARGET_MACHO
15424   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
15425            && CONSTANT_P (XEXP (x, 1)))
15426     {
15427       fprintf (file, "lo16(");
15428       output_addr_const (file, XEXP (x, 1));
15429       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15430     }
15431 #endif
15432   else if (legitimate_constant_pool_address_p (x, true))
15433     {
15434       /* This hack along with a corresponding hack in
15435          rs6000_output_addr_const_extra arranges to output addends
15436          where the assembler expects to find them.  eg.
15437          (lo_sum (reg 9)
15438          .       (const (plus (unspec [symbol_ref ("x") tocrel]) 8)))
15439          without this hack would be output as "x@toc+8@l(9)".  We
15440          want "x+8@toc@l(9)".  */
15441       output_addr_const (file, tocrel_base);
15442       if (GET_CODE (x) == LO_SUM)
15443         fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15444       else
15445         fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
15446     }
15447 #if TARGET_ELF
15448   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
15449            && CONSTANT_P (XEXP (x, 1)))
15450     {
15451       output_addr_const (file, XEXP (x, 1));
15452       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15453     }
15454 #endif
15455   else
15456     gcc_unreachable ();
15457 }
15458 \f
15459 /* Implement OUTPUT_ADDR_CONST_EXTRA for address X.  */
15460
15461 bool
15462 rs6000_output_addr_const_extra (FILE *file, rtx x)
15463 {
15464   if (GET_CODE (x) == UNSPEC)
15465     switch (XINT (x, 1))
15466       {
15467       case UNSPEC_TOCREL:
15468         gcc_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF);
15469         output_addr_const (file, XVECEXP (x, 0, 0));
15470         if (x == tocrel_base && tocrel_offset != const0_rtx)
15471           {
15472             if (INTVAL (tocrel_offset) >= 0)
15473               fprintf (file, "+");
15474             output_addr_const (file, tocrel_offset);
15475           }
15476         if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
15477           {
15478             putc ('-', file);
15479             assemble_name (file, toc_label_name);
15480           }
15481         else if (TARGET_ELF)
15482           fputs ("@toc", file);
15483         return true;
15484
15485 #if TARGET_MACHO
15486       case UNSPEC_MACHOPIC_OFFSET:
15487         output_addr_const (file, XVECEXP (x, 0, 0));
15488         putc ('-', file);
15489         machopic_output_function_base_name (file);
15490         return true;
15491 #endif
15492       }
15493   return false;
15494 }
15495 \f
15496 /* Target hook for assembling integer objects.  The PowerPC version has
15497    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
15498    is defined.  It also needs to handle DI-mode objects on 64-bit
15499    targets.  */
15500
15501 static bool
15502 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
15503 {
15504 #ifdef RELOCATABLE_NEEDS_FIXUP
15505   /* Special handling for SI values.  */
15506   if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
15507     {
15508       static int recurse = 0;
15509
15510       /* For -mrelocatable, we mark all addresses that need to be fixed up
15511          in the .fixup section.  */
15512       if (TARGET_RELOCATABLE
15513           && in_section != toc_section
15514           && in_section != text_section
15515           && !unlikely_text_section_p (in_section)
15516           && !recurse
15517           && GET_CODE (x) != CONST_INT
15518           && GET_CODE (x) != CONST_DOUBLE
15519           && CONSTANT_P (x))
15520         {
15521           char buf[256];
15522
15523           recurse = 1;
15524           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
15525           fixuplabelno++;
15526           ASM_OUTPUT_LABEL (asm_out_file, buf);
15527           fprintf (asm_out_file, "\t.long\t(");
15528           output_addr_const (asm_out_file, x);
15529           fprintf (asm_out_file, ")@fixup\n");
15530           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
15531           ASM_OUTPUT_ALIGN (asm_out_file, 2);
15532           fprintf (asm_out_file, "\t.long\t");
15533           assemble_name (asm_out_file, buf);
15534           fprintf (asm_out_file, "\n\t.previous\n");
15535           recurse = 0;
15536           return true;
15537         }
15538       /* Remove initial .'s to turn a -mcall-aixdesc function
15539          address into the address of the descriptor, not the function
15540          itself.  */
15541       else if (GET_CODE (x) == SYMBOL_REF
15542                && XSTR (x, 0)[0] == '.'
15543                && DEFAULT_ABI == ABI_AIX)
15544         {
15545           const char *name = XSTR (x, 0);
15546           while (*name == '.')
15547             name++;
15548
15549           fprintf (asm_out_file, "\t.long\t%s\n", name);
15550           return true;
15551         }
15552     }
15553 #endif /* RELOCATABLE_NEEDS_FIXUP */
15554   return default_assemble_integer (x, size, aligned_p);
15555 }
15556
15557 #ifdef HAVE_GAS_HIDDEN
15558 /* Emit an assembler directive to set symbol visibility for DECL to
15559    VISIBILITY_TYPE.  */
15560
15561 static void
15562 rs6000_assemble_visibility (tree decl, int vis)
15563 {
15564   /* Functions need to have their entry point symbol visibility set as
15565      well as their descriptor symbol visibility.  */
15566   if (DEFAULT_ABI == ABI_AIX
15567       && DOT_SYMBOLS
15568       && TREE_CODE (decl) == FUNCTION_DECL)
15569     {
15570       static const char * const visibility_types[] = {
15571         NULL, "internal", "hidden", "protected"
15572       };
15573
15574       const char *name, *type;
15575
15576       name = ((* targetm.strip_name_encoding)
15577               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
15578       type = visibility_types[vis];
15579
15580       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
15581       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
15582     }
15583   else
15584     default_assemble_visibility (decl, vis);
15585 }
15586 #endif
15587 \f
15588 enum rtx_code
15589 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
15590 {
15591   /* Reversal of FP compares takes care -- an ordered compare
15592      becomes an unordered compare and vice versa.  */
15593   if (mode == CCFPmode
15594       && (!flag_finite_math_only
15595           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
15596           || code == UNEQ || code == LTGT))
15597     return reverse_condition_maybe_unordered (code);
15598   else
15599     return reverse_condition (code);
15600 }
15601
15602 /* Generate a compare for CODE.  Return a brand-new rtx that
15603    represents the result of the compare.  */
15604
15605 static rtx
15606 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
15607 {
15608   enum machine_mode comp_mode;
15609   rtx compare_result;
15610   enum rtx_code code = GET_CODE (cmp);
15611   rtx op0 = XEXP (cmp, 0);
15612   rtx op1 = XEXP (cmp, 1);
15613
15614   if (FLOAT_MODE_P (mode))
15615     comp_mode = CCFPmode;
15616   else if (code == GTU || code == LTU
15617            || code == GEU || code == LEU)
15618     comp_mode = CCUNSmode;
15619   else if ((code == EQ || code == NE)
15620            && GET_CODE (op0) == SUBREG
15621            && GET_CODE (op1) == SUBREG
15622            && SUBREG_PROMOTED_UNSIGNED_P (op0)
15623            && SUBREG_PROMOTED_UNSIGNED_P (op1))
15624     /* These are unsigned values, perhaps there will be a later
15625        ordering compare that can be shared with this one.
15626        Unfortunately we cannot detect the signedness of the operands
15627        for non-subregs.  */
15628     comp_mode = CCUNSmode;
15629   else
15630     comp_mode = CCmode;
15631
15632   /* First, the compare.  */
15633   compare_result = gen_reg_rtx (comp_mode);
15634
15635   /* E500 FP compare instructions on the GPRs.  Yuck!  */
15636   if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
15637       && FLOAT_MODE_P (mode))
15638     {
15639       rtx cmp, or_result, compare_result2;
15640       enum machine_mode op_mode = GET_MODE (op0);
15641
15642       if (op_mode == VOIDmode)
15643         op_mode = GET_MODE (op1);
15644
15645       /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
15646          This explains the following mess.  */
15647
15648       switch (code)
15649         {
15650         case EQ: case UNEQ: case NE: case LTGT:
15651           switch (op_mode)
15652             {
15653             case SFmode:
15654               cmp = (flag_finite_math_only && !flag_trapping_math)
15655                 ? gen_tstsfeq_gpr (compare_result, op0, op1)
15656                 : gen_cmpsfeq_gpr (compare_result, op0, op1);
15657               break;
15658
15659             case DFmode:
15660               cmp = (flag_finite_math_only && !flag_trapping_math)
15661                 ? gen_tstdfeq_gpr (compare_result, op0, op1)
15662                 : gen_cmpdfeq_gpr (compare_result, op0, op1);
15663               break;
15664
15665             case TFmode:
15666               cmp = (flag_finite_math_only && !flag_trapping_math)
15667                 ? gen_tsttfeq_gpr (compare_result, op0, op1)
15668                 : gen_cmptfeq_gpr (compare_result, op0, op1);
15669               break;
15670
15671             default:
15672               gcc_unreachable ();
15673             }
15674           break;
15675
15676         case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
15677           switch (op_mode)
15678             {
15679             case SFmode:
15680               cmp = (flag_finite_math_only && !flag_trapping_math)
15681                 ? gen_tstsfgt_gpr (compare_result, op0, op1)
15682                 : gen_cmpsfgt_gpr (compare_result, op0, op1);
15683               break;
15684
15685             case DFmode:
15686               cmp = (flag_finite_math_only && !flag_trapping_math)
15687                 ? gen_tstdfgt_gpr (compare_result, op0, op1)
15688                 : gen_cmpdfgt_gpr (compare_result, op0, op1);
15689               break;
15690
15691             case TFmode:
15692               cmp = (flag_finite_math_only && !flag_trapping_math)
15693                 ? gen_tsttfgt_gpr (compare_result, op0, op1)
15694                 : gen_cmptfgt_gpr (compare_result, op0, op1);
15695               break;
15696
15697             default:
15698               gcc_unreachable ();
15699             }
15700           break;
15701
15702         case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
15703           switch (op_mode)
15704             {
15705             case SFmode:
15706               cmp = (flag_finite_math_only && !flag_trapping_math)
15707                 ? gen_tstsflt_gpr (compare_result, op0, op1)
15708                 : gen_cmpsflt_gpr (compare_result, op0, op1);
15709               break;
15710
15711             case DFmode:
15712               cmp = (flag_finite_math_only && !flag_trapping_math)
15713                 ? gen_tstdflt_gpr (compare_result, op0, op1)
15714                 : gen_cmpdflt_gpr (compare_result, op0, op1);
15715               break;
15716
15717             case TFmode:
15718               cmp = (flag_finite_math_only && !flag_trapping_math)
15719                 ? gen_tsttflt_gpr (compare_result, op0, op1)
15720                 : gen_cmptflt_gpr (compare_result, op0, op1);
15721               break;
15722
15723             default:
15724               gcc_unreachable ();
15725             }
15726           break;
15727         default:
15728           gcc_unreachable ();
15729         }
15730
15731       /* Synthesize LE and GE from LT/GT || EQ.  */
15732       if (code == LE || code == GE || code == LEU || code == GEU)
15733         {
15734           emit_insn (cmp);
15735
15736           switch (code)
15737             {
15738             case LE: code = LT; break;
15739             case GE: code = GT; break;
15740             case LEU: code = LT; break;
15741             case GEU: code = GT; break;
15742             default: gcc_unreachable ();
15743             }
15744
15745           compare_result2 = gen_reg_rtx (CCFPmode);
15746
15747           /* Do the EQ.  */
15748           switch (op_mode)
15749             {
15750             case SFmode:
15751               cmp = (flag_finite_math_only && !flag_trapping_math)
15752                 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
15753                 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
15754               break;
15755
15756             case DFmode:
15757               cmp = (flag_finite_math_only && !flag_trapping_math)
15758                 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
15759                 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
15760               break;
15761
15762             case TFmode:
15763               cmp = (flag_finite_math_only && !flag_trapping_math)
15764                 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
15765                 : gen_cmptfeq_gpr (compare_result2, op0, op1);
15766               break;
15767
15768             default:
15769               gcc_unreachable ();
15770             }
15771           emit_insn (cmp);
15772
15773           /* OR them together.  */
15774           or_result = gen_reg_rtx (CCFPmode);
15775           cmp = gen_e500_cr_ior_compare (or_result, compare_result,
15776                                            compare_result2);
15777           compare_result = or_result;
15778           code = EQ;
15779         }
15780       else
15781         {
15782           if (code == NE || code == LTGT)
15783             code = NE;
15784           else
15785             code = EQ;
15786         }
15787
15788       emit_insn (cmp);
15789     }
15790   else
15791     {
15792       /* Generate XLC-compatible TFmode compare as PARALLEL with extra
15793          CLOBBERs to match cmptf_internal2 pattern.  */
15794       if (comp_mode == CCFPmode && TARGET_XL_COMPAT
15795           && GET_MODE (op0) == TFmode
15796           && !TARGET_IEEEQUAD
15797           && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
15798         emit_insn (gen_rtx_PARALLEL (VOIDmode,
15799           gen_rtvec (10,
15800                      gen_rtx_SET (VOIDmode,
15801                                   compare_result,
15802                                   gen_rtx_COMPARE (comp_mode, op0, op1)),
15803                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15804                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15805                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15806                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15807                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15808                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15809                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15810                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15811                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
15812       else if (GET_CODE (op1) == UNSPEC
15813                && XINT (op1, 1) == UNSPEC_SP_TEST)
15814         {
15815           rtx op1b = XVECEXP (op1, 0, 0);
15816           comp_mode = CCEQmode;
15817           compare_result = gen_reg_rtx (CCEQmode);
15818           if (TARGET_64BIT)
15819             emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
15820           else
15821             emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
15822         }
15823       else
15824         emit_insn (gen_rtx_SET (VOIDmode, compare_result,
15825                                 gen_rtx_COMPARE (comp_mode, op0, op1)));
15826     }
15827
15828   /* Some kinds of FP comparisons need an OR operation;
15829      under flag_finite_math_only we don't bother.  */
15830   if (FLOAT_MODE_P (mode)
15831       && !flag_finite_math_only
15832       && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
15833       && (code == LE || code == GE
15834           || code == UNEQ || code == LTGT
15835           || code == UNGT || code == UNLT))
15836     {
15837       enum rtx_code or1, or2;
15838       rtx or1_rtx, or2_rtx, compare2_rtx;
15839       rtx or_result = gen_reg_rtx (CCEQmode);
15840
15841       switch (code)
15842         {
15843         case LE: or1 = LT;  or2 = EQ;  break;
15844         case GE: or1 = GT;  or2 = EQ;  break;
15845         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
15846         case LTGT: or1 = LT;  or2 = GT;  break;
15847         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
15848         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
15849         default:  gcc_unreachable ();
15850         }
15851       validate_condition_mode (or1, comp_mode);
15852       validate_condition_mode (or2, comp_mode);
15853       or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
15854       or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
15855       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
15856                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
15857                                       const_true_rtx);
15858       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
15859
15860       compare_result = or_result;
15861       code = EQ;
15862     }
15863
15864   validate_condition_mode (code, GET_MODE (compare_result));
15865
15866   return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
15867 }
15868
15869
15870 /* Emit the RTL for an sCOND pattern.  */
15871
15872 void
15873 rs6000_emit_sISEL (enum machine_mode mode, rtx operands[])
15874 {
15875   rtx condition_rtx;
15876   enum machine_mode op_mode;
15877   enum rtx_code cond_code;
15878   rtx result = operands[0];
15879
15880   condition_rtx = rs6000_generate_compare (operands[1], mode);
15881   cond_code = GET_CODE (condition_rtx);
15882
15883   op_mode = GET_MODE (XEXP (operands[1], 0));
15884   if (op_mode == VOIDmode)
15885     op_mode = GET_MODE (XEXP (operands[1], 1));
15886
15887   if (TARGET_POWERPC64 && GET_MODE (result) == DImode)
15888     {
15889       PUT_MODE (condition_rtx, DImode);
15890       if (cond_code == GEU || cond_code == GTU || cond_code == LEU
15891          || cond_code == LTU)
15892        emit_insn (gen_isel_unsigned_di (result, condition_rtx,
15893                                         force_reg (DImode, const1_rtx),
15894                                         force_reg (DImode, const0_rtx),
15895                                         XEXP (condition_rtx, 0)));
15896       else
15897        emit_insn (gen_isel_signed_di (result, condition_rtx,
15898                                       force_reg (DImode, const1_rtx),
15899                                       force_reg (DImode, const0_rtx),
15900                                       XEXP (condition_rtx, 0)));
15901     }
15902   else
15903     {
15904       PUT_MODE (condition_rtx, SImode);
15905       if (cond_code == GEU || cond_code == GTU || cond_code == LEU
15906          || cond_code == LTU)
15907        emit_insn (gen_isel_unsigned_si (result, condition_rtx,
15908                                         force_reg (SImode, const1_rtx),
15909                                         force_reg (SImode, const0_rtx),
15910                                         XEXP (condition_rtx, 0)));
15911       else
15912        emit_insn (gen_isel_signed_si (result, condition_rtx,
15913                                       force_reg (SImode, const1_rtx),
15914                                       force_reg (SImode, const0_rtx),
15915                                       XEXP (condition_rtx, 0)));
15916     }
15917 }
15918
15919 void
15920 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
15921 {
15922   rtx condition_rtx;
15923   enum machine_mode op_mode;
15924   enum rtx_code cond_code;
15925   rtx result = operands[0];
15926
15927   if (TARGET_ISEL && (mode == SImode || mode == DImode))
15928     {
15929       rs6000_emit_sISEL (mode, operands);
15930       return;
15931     }
15932
15933   condition_rtx = rs6000_generate_compare (operands[1], mode);
15934   cond_code = GET_CODE (condition_rtx);
15935
15936   if (FLOAT_MODE_P (mode)
15937       && !TARGET_FPRS && TARGET_HARD_FLOAT)
15938     {
15939       rtx t;
15940
15941       PUT_MODE (condition_rtx, SImode);
15942       t = XEXP (condition_rtx, 0);
15943
15944       gcc_assert (cond_code == NE || cond_code == EQ);
15945
15946       if (cond_code == NE)
15947         emit_insn (gen_e500_flip_gt_bit (t, t));
15948
15949       emit_insn (gen_move_from_CR_gt_bit (result, t));
15950       return;
15951     }
15952
15953   if (cond_code == NE
15954       || cond_code == GE || cond_code == LE
15955       || cond_code == GEU || cond_code == LEU
15956       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
15957     {
15958       rtx not_result = gen_reg_rtx (CCEQmode);
15959       rtx not_op, rev_cond_rtx;
15960       enum machine_mode cc_mode;
15961
15962       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
15963
15964       rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
15965                                      SImode, XEXP (condition_rtx, 0), const0_rtx);
15966       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
15967       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
15968       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
15969     }
15970
15971   op_mode = GET_MODE (XEXP (operands[1], 0));
15972   if (op_mode == VOIDmode)
15973     op_mode = GET_MODE (XEXP (operands[1], 1));
15974
15975   if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
15976     {
15977       PUT_MODE (condition_rtx, DImode);
15978       convert_move (result, condition_rtx, 0);
15979     }
15980   else
15981     {
15982       PUT_MODE (condition_rtx, SImode);
15983       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
15984     }
15985 }
15986
15987 /* Emit a branch of kind CODE to location LOC.  */
15988
15989 void
15990 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
15991 {
15992   rtx condition_rtx, loc_ref;
15993
15994   condition_rtx = rs6000_generate_compare (operands[0], mode);
15995   loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
15996   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
15997                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
15998                                                      loc_ref, pc_rtx)));
15999 }
16000
16001 /* Return the string to output a conditional branch to LABEL, which is
16002    the operand number of the label, or -1 if the branch is really a
16003    conditional return.
16004
16005    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
16006    condition code register and its mode specifies what kind of
16007    comparison we made.
16008
16009    REVERSED is nonzero if we should reverse the sense of the comparison.
16010
16011    INSN is the insn.  */
16012
16013 char *
16014 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
16015 {
16016   static char string[64];
16017   enum rtx_code code = GET_CODE (op);
16018   rtx cc_reg = XEXP (op, 0);
16019   enum machine_mode mode = GET_MODE (cc_reg);
16020   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
16021   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
16022   int really_reversed = reversed ^ need_longbranch;
16023   char *s = string;
16024   const char *ccode;
16025   const char *pred;
16026   rtx note;
16027
16028   validate_condition_mode (code, mode);
16029
16030   /* Work out which way this really branches.  We could use
16031      reverse_condition_maybe_unordered here always but this
16032      makes the resulting assembler clearer.  */
16033   if (really_reversed)
16034     {
16035       /* Reversal of FP compares takes care -- an ordered compare
16036          becomes an unordered compare and vice versa.  */
16037       if (mode == CCFPmode)
16038         code = reverse_condition_maybe_unordered (code);
16039       else
16040         code = reverse_condition (code);
16041     }
16042
16043   if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
16044     {
16045       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
16046          to the GT bit.  */
16047       switch (code)
16048         {
16049         case EQ:
16050           /* Opposite of GT.  */
16051           code = GT;
16052           break;
16053
16054         case NE:
16055           code = UNLE;
16056           break;
16057
16058         default:
16059           gcc_unreachable ();
16060         }
16061     }
16062
16063   switch (code)
16064     {
16065       /* Not all of these are actually distinct opcodes, but
16066          we distinguish them for clarity of the resulting assembler.  */
16067     case NE: case LTGT:
16068       ccode = "ne"; break;
16069     case EQ: case UNEQ:
16070       ccode = "eq"; break;
16071     case GE: case GEU:
16072       ccode = "ge"; break;
16073     case GT: case GTU: case UNGT:
16074       ccode = "gt"; break;
16075     case LE: case LEU:
16076       ccode = "le"; break;
16077     case LT: case LTU: case UNLT:
16078       ccode = "lt"; break;
16079     case UNORDERED: ccode = "un"; break;
16080     case ORDERED: ccode = "nu"; break;
16081     case UNGE: ccode = "nl"; break;
16082     case UNLE: ccode = "ng"; break;
16083     default:
16084       gcc_unreachable ();
16085     }
16086
16087   /* Maybe we have a guess as to how likely the branch is.
16088      The old mnemonics don't have a way to specify this information.  */
16089   pred = "";
16090   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
16091   if (note != NULL_RTX)
16092     {
16093       /* PROB is the difference from 50%.  */
16094       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
16095
16096       /* Only hint for highly probable/improbable branches on newer
16097          cpus as static prediction overrides processor dynamic
16098          prediction.  For older cpus we may as well always hint, but
16099          assume not taken for branches that are very close to 50% as a
16100          mispredicted taken branch is more expensive than a
16101          mispredicted not-taken branch.  */
16102       if (rs6000_always_hint
16103           || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
16104               && br_prob_note_reliable_p (note)))
16105         {
16106           if (abs (prob) > REG_BR_PROB_BASE / 20
16107               && ((prob > 0) ^ need_longbranch))
16108             pred = "+";
16109           else
16110             pred = "-";
16111         }
16112     }
16113
16114   if (label == NULL)
16115     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
16116   else
16117     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
16118
16119   /* We need to escape any '%' characters in the reg_names string.
16120      Assume they'd only be the first character....  */
16121   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
16122     *s++ = '%';
16123   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
16124
16125   if (label != NULL)
16126     {
16127       /* If the branch distance was too far, we may have to use an
16128          unconditional branch to go the distance.  */
16129       if (need_longbranch)
16130         s += sprintf (s, ",$+8\n\tb %s", label);
16131       else
16132         s += sprintf (s, ",%s", label);
16133     }
16134
16135   return string;
16136 }
16137
16138 /* Return the string to flip the GT bit on a CR.  */
16139 char *
16140 output_e500_flip_gt_bit (rtx dst, rtx src)
16141 {
16142   static char string[64];
16143   int a, b;
16144
16145   gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
16146               && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
16147
16148   /* GT bit.  */
16149   a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
16150   b = 4 * (REGNO (src) - CR0_REGNO) + 1;
16151
16152   sprintf (string, "crnot %d,%d", a, b);
16153   return string;
16154 }
16155
16156 /* Return insn for VSX or Altivec comparisons.  */
16157
16158 static rtx
16159 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
16160 {
16161   rtx mask;
16162   enum machine_mode mode = GET_MODE (op0);
16163
16164   switch (code)
16165     {
16166     default:
16167       break;
16168
16169     case GE:
16170       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16171         return NULL_RTX;
16172
16173     case EQ:
16174     case GT:
16175     case GTU:
16176       mask = gen_reg_rtx (mode);
16177       emit_insn (gen_rtx_SET (VOIDmode,
16178                               mask,
16179                               gen_rtx_fmt_ee (code, mode, op0, op1)));
16180       return mask;
16181     }
16182
16183   return NULL_RTX;
16184 }
16185
16186 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
16187    DMODE is expected destination mode. This is a recursive function.  */
16188
16189 static rtx
16190 rs6000_emit_vector_compare (enum rtx_code rcode,
16191                             rtx op0, rtx op1,
16192                             enum machine_mode dmode)
16193 {
16194   rtx mask;
16195   bool swap_operands = false;
16196   bool try_again = false;
16197
16198   gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
16199   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
16200
16201   /* See if the comparison works as is.  */
16202   mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16203   if (mask)
16204     return mask;
16205
16206   switch (rcode)
16207     {
16208     case LT:
16209       rcode = GT;
16210       swap_operands = true;
16211       try_again = true;
16212       break;
16213     case LTU:
16214       rcode = GTU;
16215       swap_operands = true;
16216       try_again = true;
16217       break;
16218     case NE:
16219     case UNLE:
16220     case UNLT:
16221     case UNGE:
16222     case UNGT:
16223       /* Invert condition and try again.
16224          e.g., A != B becomes ~(A==B).  */
16225       {
16226         enum rtx_code rev_code;
16227         enum insn_code nor_code;
16228         rtx mask2;
16229
16230         rev_code = reverse_condition_maybe_unordered (rcode);
16231         if (rev_code == UNKNOWN)
16232           return NULL_RTX;
16233
16234         nor_code = optab_handler (one_cmpl_optab, (int)dmode)->insn_code;
16235         if (nor_code == CODE_FOR_nothing)
16236           return NULL_RTX;
16237
16238         mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
16239         if (!mask2)
16240           return NULL_RTX;
16241
16242         mask = gen_reg_rtx (dmode);
16243         emit_insn (GEN_FCN (nor_code) (mask, mask2));
16244         return mask;
16245       }
16246       break;
16247     case GE:
16248     case GEU:
16249     case LE:
16250     case LEU:
16251       /* Try GT/GTU/LT/LTU OR EQ */
16252       {
16253         rtx c_rtx, eq_rtx;
16254         enum insn_code ior_code;
16255         enum rtx_code new_code;
16256
16257         switch (rcode)
16258           {
16259           case  GE:
16260             new_code = GT;
16261             break;
16262
16263           case GEU:
16264             new_code = GTU;
16265             break;
16266
16267           case LE:
16268             new_code = LT;
16269             break;
16270
16271           case LEU:
16272             new_code = LTU;
16273             break;
16274
16275           default:
16276             gcc_unreachable ();
16277           }
16278
16279         ior_code = optab_handler (ior_optab, (int)dmode)->insn_code;
16280         if (ior_code == CODE_FOR_nothing)
16281           return NULL_RTX;
16282
16283         c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
16284         if (!c_rtx)
16285           return NULL_RTX;
16286
16287         eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
16288         if (!eq_rtx)
16289           return NULL_RTX;
16290
16291         mask = gen_reg_rtx (dmode);
16292         emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
16293         return mask;
16294       }
16295       break;
16296     default:
16297       return NULL_RTX;
16298     }
16299
16300   if (try_again)
16301     {
16302       if (swap_operands)
16303         {
16304           rtx tmp;
16305           tmp = op0;
16306           op0 = op1;
16307           op1 = tmp;
16308         }
16309
16310       mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16311       if (mask)
16312         return mask;
16313     }
16314
16315   /* You only get two chances.  */
16316   return NULL_RTX;
16317 }
16318
16319 /* Emit vector conditional expression.  DEST is destination. OP_TRUE and
16320    OP_FALSE are two VEC_COND_EXPR operands.  CC_OP0 and CC_OP1 are the two
16321    operands for the relation operation COND.  */
16322
16323 int
16324 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
16325                               rtx cond, rtx cc_op0, rtx cc_op1)
16326 {
16327   enum machine_mode dest_mode = GET_MODE (dest);
16328   enum rtx_code rcode = GET_CODE (cond);
16329   enum machine_mode cc_mode = CCmode;
16330   rtx mask;
16331   rtx cond2;
16332   rtx tmp;
16333   bool invert_move = false;
16334
16335   if (VECTOR_UNIT_NONE_P (dest_mode))
16336     return 0;
16337
16338   switch (rcode)
16339     {
16340       /* Swap operands if we can, and fall back to doing the operation as
16341          specified, and doing a NOR to invert the test.  */
16342     case NE:
16343     case UNLE:
16344     case UNLT:
16345     case UNGE:
16346     case UNGT:
16347       /* Invert condition and try again.
16348          e.g., A  = (B != C) ? D : E becomes A = (B == C) ? E : D.  */
16349       invert_move = true;
16350       rcode = reverse_condition_maybe_unordered (rcode);
16351       if (rcode == UNKNOWN)
16352         return 0;
16353       break;
16354
16355       /* Mark unsigned tests with CCUNSmode.  */
16356     case GTU:
16357     case GEU:
16358     case LTU:
16359     case LEU:
16360       cc_mode = CCUNSmode;
16361       break;
16362
16363     default:
16364       break;
16365     }
16366
16367   /* Get the vector mask for the given relational operations.  */
16368   mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, dest_mode);
16369
16370   if (!mask)
16371     return 0;
16372
16373   if (invert_move)
16374     {
16375       tmp = op_true;
16376       op_true = op_false;
16377       op_false = tmp;
16378     }
16379
16380   cond2 = gen_rtx_fmt_ee (NE, cc_mode, mask, const0_rtx);
16381   emit_insn (gen_rtx_SET (VOIDmode,
16382                           dest,
16383                           gen_rtx_IF_THEN_ELSE (dest_mode,
16384                                                 cond2,
16385                                                 op_true,
16386                                                 op_false)));
16387   return 1;
16388 }
16389
16390 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
16391    operands of the last comparison is nonzero/true, FALSE_COND if it
16392    is zero/false.  Return 0 if the hardware has no such operation.  */
16393
16394 int
16395 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16396 {
16397   enum rtx_code code = GET_CODE (op);
16398   rtx op0 = XEXP (op, 0);
16399   rtx op1 = XEXP (op, 1);
16400   REAL_VALUE_TYPE c1;
16401   enum machine_mode compare_mode = GET_MODE (op0);
16402   enum machine_mode result_mode = GET_MODE (dest);
16403   rtx temp;
16404   bool is_against_zero;
16405
16406   /* These modes should always match.  */
16407   if (GET_MODE (op1) != compare_mode
16408       /* In the isel case however, we can use a compare immediate, so
16409          op1 may be a small constant.  */
16410       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
16411     return 0;
16412   if (GET_MODE (true_cond) != result_mode)
16413     return 0;
16414   if (GET_MODE (false_cond) != result_mode)
16415     return 0;
16416
16417   /* First, work out if the hardware can do this at all, or
16418      if it's too slow....  */
16419   if (!FLOAT_MODE_P (compare_mode))
16420     {
16421       if (TARGET_ISEL)
16422         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
16423       return 0;
16424     }
16425   else if (TARGET_HARD_FLOAT && !TARGET_FPRS
16426            && SCALAR_FLOAT_MODE_P (compare_mode))
16427     return 0;
16428
16429   is_against_zero = op1 == CONST0_RTX (compare_mode);
16430
16431   /* A floating-point subtract might overflow, underflow, or produce
16432      an inexact result, thus changing the floating-point flags, so it
16433      can't be generated if we care about that.  It's safe if one side
16434      of the construct is zero, since then no subtract will be
16435      generated.  */
16436   if (SCALAR_FLOAT_MODE_P (compare_mode)
16437       && flag_trapping_math && ! is_against_zero)
16438     return 0;
16439
16440   /* Eliminate half of the comparisons by switching operands, this
16441      makes the remaining code simpler.  */
16442   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
16443       || code == LTGT || code == LT || code == UNLE)
16444     {
16445       code = reverse_condition_maybe_unordered (code);
16446       temp = true_cond;
16447       true_cond = false_cond;
16448       false_cond = temp;
16449     }
16450
16451   /* UNEQ and LTGT take four instructions for a comparison with zero,
16452      it'll probably be faster to use a branch here too.  */
16453   if (code == UNEQ && HONOR_NANS (compare_mode))
16454     return 0;
16455
16456   if (GET_CODE (op1) == CONST_DOUBLE)
16457     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
16458
16459   /* We're going to try to implement comparisons by performing
16460      a subtract, then comparing against zero.  Unfortunately,
16461      Inf - Inf is NaN which is not zero, and so if we don't
16462      know that the operand is finite and the comparison
16463      would treat EQ different to UNORDERED, we can't do it.  */
16464   if (HONOR_INFINITIES (compare_mode)
16465       && code != GT && code != UNGE
16466       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
16467       /* Constructs of the form (a OP b ? a : b) are safe.  */
16468       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
16469           || (! rtx_equal_p (op0, true_cond)
16470               && ! rtx_equal_p (op1, true_cond))))
16471     return 0;
16472
16473   /* At this point we know we can use fsel.  */
16474
16475   /* Reduce the comparison to a comparison against zero.  */
16476   if (! is_against_zero)
16477     {
16478       temp = gen_reg_rtx (compare_mode);
16479       emit_insn (gen_rtx_SET (VOIDmode, temp,
16480                               gen_rtx_MINUS (compare_mode, op0, op1)));
16481       op0 = temp;
16482       op1 = CONST0_RTX (compare_mode);
16483     }
16484
16485   /* If we don't care about NaNs we can reduce some of the comparisons
16486      down to faster ones.  */
16487   if (! HONOR_NANS (compare_mode))
16488     switch (code)
16489       {
16490       case GT:
16491         code = LE;
16492         temp = true_cond;
16493         true_cond = false_cond;
16494         false_cond = temp;
16495         break;
16496       case UNGE:
16497         code = GE;
16498         break;
16499       case UNEQ:
16500         code = EQ;
16501         break;
16502       default:
16503         break;
16504       }
16505
16506   /* Now, reduce everything down to a GE.  */
16507   switch (code)
16508     {
16509     case GE:
16510       break;
16511
16512     case LE:
16513       temp = gen_reg_rtx (compare_mode);
16514       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16515       op0 = temp;
16516       break;
16517
16518     case ORDERED:
16519       temp = gen_reg_rtx (compare_mode);
16520       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
16521       op0 = temp;
16522       break;
16523
16524     case EQ:
16525       temp = gen_reg_rtx (compare_mode);
16526       emit_insn (gen_rtx_SET (VOIDmode, temp,
16527                               gen_rtx_NEG (compare_mode,
16528                                            gen_rtx_ABS (compare_mode, op0))));
16529       op0 = temp;
16530       break;
16531
16532     case UNGE:
16533       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
16534       temp = gen_reg_rtx (result_mode);
16535       emit_insn (gen_rtx_SET (VOIDmode, temp,
16536                               gen_rtx_IF_THEN_ELSE (result_mode,
16537                                                     gen_rtx_GE (VOIDmode,
16538                                                                 op0, op1),
16539                                                     true_cond, false_cond)));
16540       false_cond = true_cond;
16541       true_cond = temp;
16542
16543       temp = gen_reg_rtx (compare_mode);
16544       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16545       op0 = temp;
16546       break;
16547
16548     case GT:
16549       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
16550       temp = gen_reg_rtx (result_mode);
16551       emit_insn (gen_rtx_SET (VOIDmode, temp,
16552                               gen_rtx_IF_THEN_ELSE (result_mode,
16553                                                     gen_rtx_GE (VOIDmode,
16554                                                                 op0, op1),
16555                                                     true_cond, false_cond)));
16556       true_cond = false_cond;
16557       false_cond = temp;
16558
16559       temp = gen_reg_rtx (compare_mode);
16560       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16561       op0 = temp;
16562       break;
16563
16564     default:
16565       gcc_unreachable ();
16566     }
16567
16568   emit_insn (gen_rtx_SET (VOIDmode, dest,
16569                           gen_rtx_IF_THEN_ELSE (result_mode,
16570                                                 gen_rtx_GE (VOIDmode,
16571                                                             op0, op1),
16572                                                 true_cond, false_cond)));
16573   return 1;
16574 }
16575
16576 /* Same as above, but for ints (isel).  */
16577
16578 static int
16579 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16580 {
16581   rtx condition_rtx, cr;
16582   enum machine_mode mode = GET_MODE (dest);
16583
16584   if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
16585     return 0;
16586
16587   /* We still have to do the compare, because isel doesn't do a
16588      compare, it just looks at the CRx bits set by a previous compare
16589      instruction.  */
16590   condition_rtx = rs6000_generate_compare (op, mode);
16591   cr = XEXP (condition_rtx, 0);
16592
16593   if (mode == SImode)
16594     {
16595       if (GET_MODE (cr) == CCmode)
16596         emit_insn (gen_isel_signed_si (dest, condition_rtx,
16597                                        true_cond, false_cond, cr));
16598       else
16599         emit_insn (gen_isel_unsigned_si (dest, condition_rtx,
16600                                          true_cond, false_cond, cr));
16601     }
16602   else
16603     {
16604       if (GET_MODE (cr) == CCmode)
16605         emit_insn (gen_isel_signed_di (dest, condition_rtx,
16606                                        true_cond, false_cond, cr));
16607       else
16608         emit_insn (gen_isel_unsigned_di (dest, condition_rtx,
16609                                          true_cond, false_cond, cr));
16610     }
16611
16612   return 1;
16613 }
16614
16615 const char *
16616 output_isel (rtx *operands)
16617 {
16618   enum rtx_code code;
16619
16620   code = GET_CODE (operands[1]);
16621   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
16622     {
16623       PUT_CODE (operands[1], reverse_condition (code));
16624       return "isel %0,%3,%2,%j1";
16625     }
16626   else
16627     return "isel %0,%2,%3,%j1";
16628 }
16629
16630 void
16631 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16632 {
16633   enum machine_mode mode = GET_MODE (op0);
16634   enum rtx_code c;
16635   rtx target;
16636
16637   /* VSX/altivec have direct min/max insns.  */
16638   if ((code == SMAX || code == SMIN) && VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode))
16639     {
16640       emit_insn (gen_rtx_SET (VOIDmode,
16641                               dest,
16642                               gen_rtx_fmt_ee (code, mode, op0, op1)));
16643       return;
16644     }
16645
16646   if (code == SMAX || code == SMIN)
16647     c = GE;
16648   else
16649     c = GEU;
16650
16651   if (code == SMAX || code == UMAX)
16652     target = emit_conditional_move (dest, c, op0, op1, mode,
16653                                     op0, op1, mode, 0);
16654   else
16655     target = emit_conditional_move (dest, c, op0, op1, mode,
16656                                     op1, op0, mode, 0);
16657   gcc_assert (target);
16658   if (target != dest)
16659     emit_move_insn (dest, target);
16660 }
16661
16662 /* Emit instructions to perform a load-reserved/store-conditional operation.
16663    The operation performed is an atomic
16664    (set M (CODE:MODE M OP))
16665    If not NULL, BEFORE is atomically set to M before the operation, and
16666    AFTER is set to M after the operation (that is, (CODE:MODE M OP)).
16667    If SYNC_P then a memory barrier is emitted before the operation.
16668    Either OP or M may be wrapped in a NOT operation.  */
16669
16670 void
16671 rs6000_emit_sync (enum rtx_code code, enum machine_mode mode,
16672                   rtx m, rtx op, rtx before_param, rtx after_param,
16673                   bool sync_p)
16674 {
16675   enum machine_mode used_mode;
16676   rtx the_op, set_before, set_after, set_atomic, cc_scratch, before, after;
16677   rtx used_m;
16678   rtvec vec;
16679   HOST_WIDE_INT imask = GET_MODE_MASK (mode);
16680   rtx shift = NULL_RTX;
16681
16682   if (sync_p)
16683     emit_insn (gen_lwsync ());
16684
16685     used_m = m;
16686
16687   /* If this is smaller than SImode, we'll have to use SImode with
16688      adjustments.  */
16689   if (mode == QImode || mode == HImode)
16690     {
16691       rtx newop, oldop;
16692
16693       if (MEM_ALIGN (used_m) >= 32)
16694         {
16695           int ishift = 0;
16696           if (BYTES_BIG_ENDIAN)
16697             ishift = GET_MODE_BITSIZE (SImode) - GET_MODE_BITSIZE (mode);
16698
16699           shift = GEN_INT (ishift);
16700           used_m = change_address (used_m, SImode, 0);
16701         }
16702       else
16703         {
16704           rtx addrSI, aligned_addr;
16705           int shift_mask = mode == QImode ? 0x18 : 0x10;
16706
16707           addrSI = gen_lowpart_common (SImode,
16708                                        force_reg (Pmode, XEXP (used_m, 0)));
16709           addrSI = force_reg (SImode, addrSI);
16710           shift = gen_reg_rtx (SImode);
16711
16712           emit_insn (gen_rlwinm (shift, addrSI, GEN_INT (3),
16713                                  GEN_INT (shift_mask)));
16714           emit_insn (gen_xorsi3 (shift, shift, GEN_INT (shift_mask)));
16715
16716           aligned_addr = expand_binop (Pmode, and_optab,
16717                                        XEXP (used_m, 0),
16718                                        GEN_INT (-4), NULL_RTX,
16719                                        1, OPTAB_LIB_WIDEN);
16720           used_m = change_address (used_m, SImode, aligned_addr);
16721           set_mem_align (used_m, 32);
16722         }
16723       /* It's safe to keep the old alias set of USED_M, because
16724          the operation is atomic and only affects the original
16725          USED_M.  */
16726       m = used_m;
16727
16728       if (GET_CODE (op) == NOT)
16729         {
16730           oldop = lowpart_subreg (SImode, XEXP (op, 0), mode);
16731           oldop = gen_rtx_NOT (SImode, oldop);
16732         }
16733       else
16734         oldop = lowpart_subreg (SImode, op, mode);
16735
16736       switch (code)
16737         {
16738         case IOR:
16739         case XOR:
16740           newop = expand_binop (SImode, and_optab,
16741                                 oldop, GEN_INT (imask), NULL_RTX,
16742                                 1, OPTAB_LIB_WIDEN);
16743           emit_insn (gen_ashlsi3 (newop, newop, shift));
16744           break;
16745
16746         case NOT: /* NAND */
16747           newop = expand_binop (SImode, ior_optab,
16748                                 oldop, GEN_INT (~imask), NULL_RTX,
16749                                 1, OPTAB_LIB_WIDEN);
16750           emit_insn (gen_rotlsi3 (newop, newop, shift));
16751           break;
16752
16753         case AND:
16754           newop = expand_binop (SImode, ior_optab,
16755                                 oldop, GEN_INT (~imask), NULL_RTX,
16756                                 1, OPTAB_LIB_WIDEN);
16757           emit_insn (gen_rotlsi3 (newop, newop, shift));
16758           break;
16759
16760         case PLUS:
16761         case MINUS:
16762           {
16763             rtx mask;
16764
16765             newop = expand_binop (SImode, and_optab,
16766                                   oldop, GEN_INT (imask), NULL_RTX,
16767                                   1, OPTAB_LIB_WIDEN);
16768             emit_insn (gen_ashlsi3 (newop, newop, shift));
16769
16770             mask = gen_reg_rtx (SImode);
16771             emit_move_insn (mask, GEN_INT (imask));
16772             emit_insn (gen_ashlsi3 (mask, mask, shift));
16773
16774             if (code == PLUS)
16775               newop = gen_rtx_PLUS (SImode, m, newop);
16776             else
16777               newop = gen_rtx_MINUS (SImode, m, newop);
16778             newop = gen_rtx_AND (SImode, newop, mask);
16779             newop = gen_rtx_IOR (SImode, newop,
16780                                  gen_rtx_AND (SImode,
16781                                               gen_rtx_NOT (SImode, mask),
16782                                               m));
16783             break;
16784           }
16785
16786         default:
16787           gcc_unreachable ();
16788         }
16789
16790       op = newop;
16791       used_mode = SImode;
16792       before = gen_reg_rtx (used_mode);
16793       after = gen_reg_rtx (used_mode);
16794     }
16795   else
16796     {
16797       used_mode = mode;
16798       before = before_param;
16799       after = after_param;
16800
16801       if (before == NULL_RTX)
16802         before = gen_reg_rtx (used_mode);
16803       if (after == NULL_RTX)
16804         after = gen_reg_rtx (used_mode);
16805     }
16806
16807   if ((code == PLUS || code == MINUS)
16808       && used_mode != mode)
16809     the_op = op;  /* Computed above.  */
16810   else if (GET_CODE (op) == NOT && GET_CODE (m) != NOT)
16811     the_op = gen_rtx_fmt_ee (code, used_mode, op, m);
16812   else if (code == NOT)
16813     the_op = gen_rtx_fmt_ee (IOR, used_mode,
16814                              gen_rtx_NOT (used_mode, m),
16815                              gen_rtx_NOT (used_mode, op));
16816   else
16817     the_op = gen_rtx_fmt_ee (code, used_mode, m, op);
16818
16819   set_after = gen_rtx_SET (VOIDmode, after, the_op);
16820   set_before = gen_rtx_SET (VOIDmode, before, used_m);
16821   set_atomic = gen_rtx_SET (VOIDmode, used_m,
16822                             gen_rtx_UNSPEC (used_mode,
16823                                             gen_rtvec (1, the_op),
16824                                             UNSPEC_SYNC_OP));
16825   cc_scratch = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (CCmode));
16826
16827   if ((code == PLUS || code == MINUS) && used_mode != mode)
16828     vec = gen_rtvec (5, set_after, set_before, set_atomic, cc_scratch,
16829                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (SImode)));
16830   else
16831     vec = gen_rtvec (4, set_after, set_before, set_atomic, cc_scratch);
16832   emit_insn (gen_rtx_PARALLEL (VOIDmode, vec));
16833
16834   /* Shift and mask the return values properly.  */
16835   if (used_mode != mode && before_param)
16836     {
16837       emit_insn (gen_lshrsi3 (before, before, shift));
16838       convert_move (before_param, before, 1);
16839     }
16840
16841   if (used_mode != mode && after_param)
16842     {
16843       emit_insn (gen_lshrsi3 (after, after, shift));
16844       convert_move (after_param, after, 1);
16845     }
16846
16847   /* The previous sequence will end with a branch that's dependent on
16848      the conditional store, so placing an isync will ensure that no
16849      other instructions (especially, no load or store instructions)
16850      can start before the atomic operation completes.  */
16851   if (sync_p)
16852     emit_insn (gen_isync ());
16853 }
16854
16855 /* A subroutine of the atomic operation splitters.  Jump to LABEL if
16856    COND is true.  Mark the jump as unlikely to be taken.  */
16857
16858 static void
16859 emit_unlikely_jump (rtx cond, rtx label)
16860 {
16861   rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
16862   rtx x;
16863
16864   x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
16865   x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
16866   add_reg_note (x, REG_BR_PROB, very_unlikely);
16867 }
16868
16869 /* A subroutine of the atomic operation splitters.  Emit a load-locked
16870    instruction in MODE.  */
16871
16872 static void
16873 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
16874 {
16875   rtx (*fn) (rtx, rtx) = NULL;
16876   if (mode == SImode)
16877     fn = gen_load_locked_si;
16878   else if (mode == DImode)
16879     fn = gen_load_locked_di;
16880   emit_insn (fn (reg, mem));
16881 }
16882
16883 /* A subroutine of the atomic operation splitters.  Emit a store-conditional
16884    instruction in MODE.  */
16885
16886 static void
16887 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
16888 {
16889   rtx (*fn) (rtx, rtx, rtx) = NULL;
16890   if (mode == SImode)
16891     fn = gen_store_conditional_si;
16892   else if (mode == DImode)
16893     fn = gen_store_conditional_di;
16894
16895   /* Emit sync before stwcx. to address PPC405 Erratum.  */
16896   if (PPC405_ERRATUM77)
16897     emit_insn (gen_memory_barrier ());
16898
16899   emit_insn (fn (res, mem, val));
16900 }
16901
16902 /* Expand an atomic fetch-and-operate pattern.  CODE is the binary operation
16903    to perform.  MEM is the memory on which to operate.  VAL is the second
16904    operand of the binary operator.  BEFORE and AFTER are optional locations to
16905    return the value of MEM either before of after the operation.  SCRATCH is
16906    a scratch register.  */
16907
16908 void
16909 rs6000_split_atomic_op (enum rtx_code code, rtx mem, rtx val,
16910                        rtx before, rtx after, rtx scratch)
16911 {
16912   enum machine_mode mode = GET_MODE (mem);
16913   rtx label, x, cond = gen_rtx_REG (CCmode, CR0_REGNO);
16914
16915   emit_insn (gen_lwsync ());
16916
16917   label = gen_label_rtx ();
16918   emit_label (label);
16919   label = gen_rtx_LABEL_REF (VOIDmode, label);
16920
16921   if (before == NULL_RTX)
16922     before = scratch;
16923   emit_load_locked (mode, before, mem);
16924
16925   if (code == NOT)
16926     x = gen_rtx_IOR (mode,
16927                      gen_rtx_NOT (mode, before),
16928                      gen_rtx_NOT (mode, val));
16929   else if (code == AND)
16930     x = gen_rtx_UNSPEC (mode, gen_rtvec (2, before, val), UNSPEC_AND);
16931   else
16932     x = gen_rtx_fmt_ee (code, mode, before, val);
16933
16934   if (after != NULL_RTX)
16935     emit_insn (gen_rtx_SET (VOIDmode, after, copy_rtx (x)));
16936   emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
16937
16938   emit_store_conditional (mode, cond, mem, scratch);
16939
16940   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16941   emit_unlikely_jump (x, label);
16942
16943   emit_insn (gen_isync ());
16944 }
16945
16946 /* Expand an atomic compare and swap operation.  MEM is the memory on which
16947    to operate.  OLDVAL is the old value to be compared.  NEWVAL is the new
16948    value to be stored.  SCRATCH is a scratch GPR.  */
16949
16950 void
16951 rs6000_split_compare_and_swap (rtx retval, rtx mem, rtx oldval, rtx newval,
16952                                rtx scratch)
16953 {
16954   enum machine_mode mode = GET_MODE (mem);
16955   rtx label1, label2, x, cond = gen_rtx_REG (CCmode, CR0_REGNO);
16956
16957   emit_insn (gen_lwsync ());
16958
16959   label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16960   label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16961   emit_label (XEXP (label1, 0));
16962
16963   emit_load_locked (mode, retval, mem);
16964
16965   x = gen_rtx_COMPARE (CCmode, retval, oldval);
16966   emit_insn (gen_rtx_SET (VOIDmode, cond, x));
16967
16968   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16969   emit_unlikely_jump (x, label2);
16970
16971   emit_move_insn (scratch, newval);
16972   emit_store_conditional (mode, cond, mem, scratch);
16973
16974   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16975   emit_unlikely_jump (x, label1);
16976
16977   emit_insn (gen_isync ());
16978   emit_label (XEXP (label2, 0));
16979 }
16980
16981 /* Expand an atomic test and set operation.  MEM is the memory on which
16982    to operate.  VAL is the value set.  SCRATCH is a scratch GPR.  */
16983
16984 void
16985 rs6000_split_lock_test_and_set (rtx retval, rtx mem, rtx val, rtx scratch)
16986 {
16987   enum machine_mode mode = GET_MODE (mem);
16988   rtx label, x, cond = gen_rtx_REG (CCmode, CR0_REGNO);
16989
16990   label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16991   emit_label (XEXP (label, 0));
16992
16993   emit_load_locked (mode, retval, mem);
16994   emit_move_insn (scratch, val);
16995   emit_store_conditional (mode, cond, mem, scratch);
16996
16997   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16998   emit_unlikely_jump (x, label);
16999
17000   emit_insn (gen_isync ());
17001 }
17002
17003 void
17004 rs6000_expand_compare_and_swapqhi (rtx dst, rtx mem, rtx oldval, rtx newval)
17005 {
17006   enum machine_mode mode = GET_MODE (mem);
17007   rtx addrSI, align, wdst, shift, mask;
17008   HOST_WIDE_INT shift_mask = mode == QImode ? 0x18 : 0x10;
17009   HOST_WIDE_INT imask = GET_MODE_MASK (mode);
17010
17011   /* Shift amount for subword relative to aligned word.  */
17012   addrSI = force_reg (GET_MODE (XEXP (mem, 0)), XEXP (mem, 0));
17013   addrSI = force_reg (SImode, gen_lowpart_common (SImode, addrSI));
17014   shift = gen_reg_rtx (SImode);
17015   emit_insn (gen_rlwinm (shift, addrSI, GEN_INT (3),
17016                          GEN_INT (shift_mask)));
17017   emit_insn (gen_xorsi3 (shift, shift, GEN_INT (shift_mask)));
17018
17019   /* Shift and mask old value into position within word.  */
17020   oldval = convert_modes (SImode, mode, oldval, 1);
17021   oldval = expand_binop (SImode, and_optab,
17022                          oldval, GEN_INT (imask), NULL_RTX,
17023                          1, OPTAB_LIB_WIDEN);
17024   emit_insn (gen_ashlsi3 (oldval, oldval, shift));
17025
17026   /* Shift and mask new value into position within word.  */
17027   newval = convert_modes (SImode, mode, newval, 1);
17028   newval = expand_binop (SImode, and_optab,
17029                          newval, GEN_INT (imask), NULL_RTX,
17030                          1, OPTAB_LIB_WIDEN);
17031   emit_insn (gen_ashlsi3 (newval, newval, shift));
17032
17033   /* Mask for insertion.  */
17034   mask = gen_reg_rtx (SImode);
17035   emit_move_insn (mask, GEN_INT (imask));
17036   emit_insn (gen_ashlsi3 (mask, mask, shift));
17037
17038   /* Address of aligned word containing subword.  */
17039   align = expand_binop (Pmode, and_optab, XEXP (mem, 0), GEN_INT (-4),
17040                         NULL_RTX, 1, OPTAB_LIB_WIDEN);
17041   mem = change_address (mem, SImode, align);
17042   set_mem_align (mem, 32);
17043   MEM_VOLATILE_P (mem) = 1;
17044
17045   wdst = gen_reg_rtx (SImode);
17046   emit_insn (gen_sync_compare_and_swapqhi_internal (wdst, mask,
17047                                                     oldval, newval, mem));
17048
17049   /* Shift the result back.  */
17050   emit_insn (gen_lshrsi3 (wdst, wdst, shift));
17051
17052   emit_move_insn (dst, gen_lowpart (mode, wdst));
17053 }
17054
17055 void
17056 rs6000_split_compare_and_swapqhi (rtx dest, rtx mask,
17057                                   rtx oldval, rtx newval, rtx mem,
17058                                   rtx scratch)
17059 {
17060   rtx label1, label2, x, cond = gen_rtx_REG (CCmode, CR0_REGNO);
17061
17062   emit_insn (gen_lwsync ());
17063   label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
17064   label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
17065   emit_label (XEXP (label1, 0));
17066
17067   emit_load_locked (SImode, scratch, mem);
17068
17069   /* Mask subword within loaded value for comparison with oldval.
17070      Use UNSPEC_AND to avoid clobber.*/
17071   emit_insn (gen_rtx_SET (SImode, dest,
17072                           gen_rtx_UNSPEC (SImode,
17073                                           gen_rtvec (2, scratch, mask),
17074                                           UNSPEC_AND)));
17075
17076   x = gen_rtx_COMPARE (CCmode, dest, oldval);
17077   emit_insn (gen_rtx_SET (VOIDmode, cond, x));
17078
17079   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17080   emit_unlikely_jump (x, label2);
17081
17082   /* Clear subword within loaded value for insertion of new value.  */
17083   emit_insn (gen_rtx_SET (SImode, scratch,
17084                           gen_rtx_AND (SImode,
17085                                        gen_rtx_NOT (SImode, mask), scratch)));
17086   emit_insn (gen_iorsi3 (scratch, scratch, newval));
17087   emit_store_conditional (SImode, cond, mem, scratch);
17088
17089   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17090   emit_unlikely_jump (x, label1);
17091
17092   emit_insn (gen_isync ());
17093   emit_label (XEXP (label2, 0));
17094 }
17095
17096
17097   /* Emit instructions to move SRC to DST.  Called by splitters for
17098    multi-register moves.  It will emit at most one instruction for
17099    each register that is accessed; that is, it won't emit li/lis pairs
17100    (or equivalent for 64-bit code).  One of SRC or DST must be a hard
17101    register.  */
17102
17103 void
17104 rs6000_split_multireg_move (rtx dst, rtx src)
17105 {
17106   /* The register number of the first register being moved.  */
17107   int reg;
17108   /* The mode that is to be moved.  */
17109   enum machine_mode mode;
17110   /* The mode that the move is being done in, and its size.  */
17111   enum machine_mode reg_mode;
17112   int reg_mode_size;
17113   /* The number of registers that will be moved.  */
17114   int nregs;
17115
17116   reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
17117   mode = GET_MODE (dst);
17118   nregs = hard_regno_nregs[reg][mode];
17119   if (FP_REGNO_P (reg))
17120     reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : 
17121         ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
17122   else if (ALTIVEC_REGNO_P (reg))
17123     reg_mode = V16QImode;
17124   else if (TARGET_E500_DOUBLE && mode == TFmode)
17125     reg_mode = DFmode;
17126   else
17127     reg_mode = word_mode;
17128   reg_mode_size = GET_MODE_SIZE (reg_mode);
17129
17130   gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
17131
17132   if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
17133     {
17134       /* Move register range backwards, if we might have destructive
17135          overlap.  */
17136       int i;
17137       for (i = nregs - 1; i >= 0; i--)
17138         emit_insn (gen_rtx_SET (VOIDmode,
17139                                 simplify_gen_subreg (reg_mode, dst, mode,
17140                                                      i * reg_mode_size),
17141                                 simplify_gen_subreg (reg_mode, src, mode,
17142                                                      i * reg_mode_size)));
17143     }
17144   else
17145     {
17146       int i;
17147       int j = -1;
17148       bool used_update = false;
17149       rtx restore_basereg = NULL_RTX;
17150
17151       if (MEM_P (src) && INT_REGNO_P (reg))
17152         {
17153           rtx breg;
17154
17155           if (GET_CODE (XEXP (src, 0)) == PRE_INC
17156               || GET_CODE (XEXP (src, 0)) == PRE_DEC)
17157             {
17158               rtx delta_rtx;
17159               breg = XEXP (XEXP (src, 0), 0);
17160               delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
17161                            ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
17162                            : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
17163               emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17164               src = replace_equiv_address (src, breg);
17165             }
17166           else if (! rs6000_offsettable_memref_p (src))
17167             {
17168               if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
17169                 {
17170                   rtx basereg = XEXP (XEXP (src, 0), 0);
17171                   if (TARGET_UPDATE)
17172                     {
17173                       rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
17174                       emit_insn (gen_rtx_SET (VOIDmode, ndst,
17175                                  gen_rtx_MEM (reg_mode, XEXP (src, 0))));
17176                       used_update = true;
17177                     }
17178                   else
17179                     emit_insn (gen_rtx_SET (VOIDmode, basereg,
17180                                XEXP (XEXP (src, 0), 1)));
17181                   src = replace_equiv_address (src, basereg);
17182                 }
17183               else
17184                 {
17185                   rtx basereg = gen_rtx_REG (Pmode, reg);
17186                   emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
17187                   src = replace_equiv_address (src, basereg);
17188                 }
17189             }
17190
17191           breg = XEXP (src, 0);
17192           if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
17193             breg = XEXP (breg, 0);
17194
17195           /* If the base register we are using to address memory is
17196              also a destination reg, then change that register last.  */
17197           if (REG_P (breg)
17198               && REGNO (breg) >= REGNO (dst)
17199               && REGNO (breg) < REGNO (dst) + nregs)
17200             j = REGNO (breg) - REGNO (dst);
17201         }
17202       else if (MEM_P (dst) && INT_REGNO_P (reg))
17203         {
17204           rtx breg;
17205
17206           if (GET_CODE (XEXP (dst, 0)) == PRE_INC
17207               || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
17208             {
17209               rtx delta_rtx;
17210               breg = XEXP (XEXP (dst, 0), 0);
17211               delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
17212                            ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
17213                            : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
17214
17215               /* We have to update the breg before doing the store.
17216                  Use store with update, if available.  */
17217
17218               if (TARGET_UPDATE)
17219                 {
17220                   rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17221                   emit_insn (TARGET_32BIT
17222                              ? (TARGET_POWERPC64
17223                                 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
17224                                 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
17225                              : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
17226                   used_update = true;
17227                 }
17228               else
17229                 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17230               dst = replace_equiv_address (dst, breg);
17231             }
17232           else if (!rs6000_offsettable_memref_p (dst)
17233                    && GET_CODE (XEXP (dst, 0)) != LO_SUM)
17234             {
17235               if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
17236                 {
17237                   rtx basereg = XEXP (XEXP (dst, 0), 0);
17238                   if (TARGET_UPDATE)
17239                     {
17240                       rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17241                       emit_insn (gen_rtx_SET (VOIDmode,
17242                                  gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
17243                       used_update = true;
17244                     }
17245                   else
17246                     emit_insn (gen_rtx_SET (VOIDmode, basereg,
17247                                XEXP (XEXP (dst, 0), 1)));
17248                   dst = replace_equiv_address (dst, basereg);
17249                 }
17250               else
17251                 {
17252                   rtx basereg = XEXP (XEXP (dst, 0), 0);
17253                   rtx offsetreg = XEXP (XEXP (dst, 0), 1);
17254                   gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
17255                               && REG_P (basereg)
17256                               && REG_P (offsetreg)
17257                               && REGNO (basereg) != REGNO (offsetreg));
17258                   if (REGNO (basereg) == 0)
17259                     {
17260                       rtx tmp = offsetreg;
17261                       offsetreg = basereg;
17262                       basereg = tmp;
17263                     }
17264                   emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
17265                   restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
17266                   dst = replace_equiv_address (dst, basereg);
17267                 }
17268             }
17269           else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
17270             gcc_assert (rs6000_offsettable_memref_p (dst));
17271         }
17272
17273       for (i = 0; i < nregs; i++)
17274         {
17275           /* Calculate index to next subword.  */
17276           ++j;
17277           if (j == nregs)
17278             j = 0;
17279
17280           /* If compiler already emitted move of first word by
17281              store with update, no need to do anything.  */
17282           if (j == 0 && used_update)
17283             continue;
17284
17285           emit_insn (gen_rtx_SET (VOIDmode,
17286                                   simplify_gen_subreg (reg_mode, dst, mode,
17287                                                        j * reg_mode_size),
17288                                   simplify_gen_subreg (reg_mode, src, mode,
17289                                                        j * reg_mode_size)));
17290         }
17291       if (restore_basereg != NULL_RTX)
17292         emit_insn (restore_basereg);
17293     }
17294 }
17295
17296 \f
17297 /* This page contains routines that are used to determine what the
17298    function prologue and epilogue code will do and write them out.  */
17299
17300 /* Return the first fixed-point register that is required to be
17301    saved. 32 if none.  */
17302
17303 int
17304 first_reg_to_save (void)
17305 {
17306   int first_reg;
17307
17308   /* Find lowest numbered live register.  */
17309   for (first_reg = 13; first_reg <= 31; first_reg++)
17310     if (df_regs_ever_live_p (first_reg)
17311         && (! call_used_regs[first_reg]
17312             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
17313                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
17314                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
17315                     || (TARGET_TOC && TARGET_MINIMAL_TOC)))))
17316       break;
17317
17318 #if TARGET_MACHO
17319   if (flag_pic
17320       && crtl->uses_pic_offset_table
17321       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
17322     return RS6000_PIC_OFFSET_TABLE_REGNUM;
17323 #endif
17324
17325   return first_reg;
17326 }
17327
17328 /* Similar, for FP regs.  */
17329
17330 int
17331 first_fp_reg_to_save (void)
17332 {
17333   int first_reg;
17334
17335   /* Find lowest numbered live register.  */
17336   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
17337     if (df_regs_ever_live_p (first_reg))
17338       break;
17339
17340   return first_reg;
17341 }
17342
17343 /* Similar, for AltiVec regs.  */
17344
17345 static int
17346 first_altivec_reg_to_save (void)
17347 {
17348   int i;
17349
17350   /* Stack frame remains as is unless we are in AltiVec ABI.  */
17351   if (! TARGET_ALTIVEC_ABI)
17352     return LAST_ALTIVEC_REGNO + 1;
17353
17354   /* On Darwin, the unwind routines are compiled without
17355      TARGET_ALTIVEC, and use save_world to save/restore the
17356      altivec registers when necessary.  */
17357   if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17358       && ! TARGET_ALTIVEC)
17359     return FIRST_ALTIVEC_REGNO + 20;
17360
17361   /* Find lowest numbered live register.  */
17362   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
17363     if (df_regs_ever_live_p (i))
17364       break;
17365
17366   return i;
17367 }
17368
17369 /* Return a 32-bit mask of the AltiVec registers we need to set in
17370    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
17371    the 32-bit word is 0.  */
17372
17373 static unsigned int
17374 compute_vrsave_mask (void)
17375 {
17376   unsigned int i, mask = 0;
17377
17378   /* On Darwin, the unwind routines are compiled without
17379      TARGET_ALTIVEC, and use save_world to save/restore the
17380      call-saved altivec registers when necessary.  */
17381   if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17382       && ! TARGET_ALTIVEC)
17383     mask |= 0xFFF;
17384
17385   /* First, find out if we use _any_ altivec registers.  */
17386   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
17387     if (df_regs_ever_live_p (i))
17388       mask |= ALTIVEC_REG_BIT (i);
17389
17390   if (mask == 0)
17391     return mask;
17392
17393   /* Next, remove the argument registers from the set.  These must
17394      be in the VRSAVE mask set by the caller, so we don't need to add
17395      them in again.  More importantly, the mask we compute here is
17396      used to generate CLOBBERs in the set_vrsave insn, and we do not
17397      wish the argument registers to die.  */
17398   for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
17399     mask &= ~ALTIVEC_REG_BIT (i);
17400
17401   /* Similarly, remove the return value from the set.  */
17402   {
17403     bool yes = false;
17404     diddle_return_value (is_altivec_return_reg, &yes);
17405     if (yes)
17406       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
17407   }
17408
17409   return mask;
17410 }
17411
17412 /* For a very restricted set of circumstances, we can cut down the
17413    size of prologues/epilogues by calling our own save/restore-the-world
17414    routines.  */
17415
17416 static void
17417 compute_save_world_info (rs6000_stack_t *info_ptr)
17418 {
17419   info_ptr->world_save_p = 1;
17420   info_ptr->world_save_p
17421     = (WORLD_SAVE_P (info_ptr)
17422        && DEFAULT_ABI == ABI_DARWIN
17423        && ! (cfun->calls_setjmp && flag_exceptions)
17424        && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
17425        && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
17426        && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
17427        && info_ptr->cr_save_p);
17428
17429   /* This will not work in conjunction with sibcalls.  Make sure there
17430      are none.  (This check is expensive, but seldom executed.) */
17431   if (WORLD_SAVE_P (info_ptr))
17432     {
17433       rtx insn;
17434       for ( insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
17435         if ( GET_CODE (insn) == CALL_INSN
17436              && SIBLING_CALL_P (insn))
17437           {
17438             info_ptr->world_save_p = 0;
17439             break;
17440           }
17441     }
17442
17443   if (WORLD_SAVE_P (info_ptr))
17444     {
17445       /* Even if we're not touching VRsave, make sure there's room on the
17446          stack for it, if it looks like we're calling SAVE_WORLD, which
17447          will attempt to save it. */
17448       info_ptr->vrsave_size  = 4;
17449
17450       /* If we are going to save the world, we need to save the link register too.  */
17451       info_ptr->lr_save_p = 1;
17452
17453       /* "Save" the VRsave register too if we're saving the world.  */
17454       if (info_ptr->vrsave_mask == 0)
17455         info_ptr->vrsave_mask = compute_vrsave_mask ();
17456
17457       /* Because the Darwin register save/restore routines only handle
17458          F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
17459          check.  */
17460       gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
17461                   && (info_ptr->first_altivec_reg_save
17462                       >= FIRST_SAVED_ALTIVEC_REGNO));
17463     }
17464   return;
17465 }
17466
17467
17468 static void
17469 is_altivec_return_reg (rtx reg, void *xyes)
17470 {
17471   bool *yes = (bool *) xyes;
17472   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
17473     *yes = true;
17474 }
17475
17476 \f
17477 /* Calculate the stack information for the current function.  This is
17478    complicated by having two separate calling sequences, the AIX calling
17479    sequence and the V.4 calling sequence.
17480
17481    AIX (and Darwin/Mac OS X) stack frames look like:
17482                                                           32-bit  64-bit
17483         SP----> +---------------------------------------+
17484                 | back chain to caller                  | 0       0
17485                 +---------------------------------------+
17486                 | saved CR                              | 4       8 (8-11)
17487                 +---------------------------------------+
17488                 | saved LR                              | 8       16
17489                 +---------------------------------------+
17490                 | reserved for compilers                | 12      24
17491                 +---------------------------------------+
17492                 | reserved for binders                  | 16      32
17493                 +---------------------------------------+
17494                 | saved TOC pointer                     | 20      40
17495                 +---------------------------------------+
17496                 | Parameter save area (P)               | 24      48
17497                 +---------------------------------------+
17498                 | Alloca space (A)                      | 24+P    etc.
17499                 +---------------------------------------+
17500                 | Local variable space (L)              | 24+P+A
17501                 +---------------------------------------+
17502                 | Float/int conversion temporary (X)    | 24+P+A+L
17503                 +---------------------------------------+
17504                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
17505                 +---------------------------------------+
17506                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
17507                 +---------------------------------------+
17508                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
17509                 +---------------------------------------+
17510                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
17511                 +---------------------------------------+
17512                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
17513                 +---------------------------------------+
17514         old SP->| back chain to caller's caller         |
17515                 +---------------------------------------+
17516
17517    The required alignment for AIX configurations is two words (i.e., 8
17518    or 16 bytes).
17519
17520
17521    V.4 stack frames look like:
17522
17523         SP----> +---------------------------------------+
17524                 | back chain to caller                  | 0
17525                 +---------------------------------------+
17526                 | caller's saved LR                     | 4
17527                 +---------------------------------------+
17528                 | Parameter save area (P)               | 8
17529                 +---------------------------------------+
17530                 | Alloca space (A)                      | 8+P
17531                 +---------------------------------------+
17532                 | Varargs save area (V)                 | 8+P+A
17533                 +---------------------------------------+
17534                 | Local variable space (L)              | 8+P+A+V
17535                 +---------------------------------------+
17536                 | Float/int conversion temporary (X)    | 8+P+A+V+L
17537                 +---------------------------------------+
17538                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
17539                 +---------------------------------------+
17540                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
17541                 +---------------------------------------+
17542                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
17543                 +---------------------------------------+
17544                 | SPE: area for 64-bit GP registers     |
17545                 +---------------------------------------+
17546                 | SPE alignment padding                 |
17547                 +---------------------------------------+
17548                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
17549                 +---------------------------------------+
17550                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
17551                 +---------------------------------------+
17552                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
17553                 +---------------------------------------+
17554         old SP->| back chain to caller's caller         |
17555                 +---------------------------------------+
17556
17557    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
17558    given.  (But note below and in sysv4.h that we require only 8 and
17559    may round up the size of our stack frame anyways.  The historical
17560    reason is early versions of powerpc-linux which didn't properly
17561    align the stack at program startup.  A happy side-effect is that
17562    -mno-eabi libraries can be used with -meabi programs.)
17563
17564    The EABI configuration defaults to the V.4 layout.  However,
17565    the stack alignment requirements may differ.  If -mno-eabi is not
17566    given, the required stack alignment is 8 bytes; if -mno-eabi is
17567    given, the required alignment is 16 bytes.  (But see V.4 comment
17568    above.)  */
17569
17570 #ifndef ABI_STACK_BOUNDARY
17571 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
17572 #endif
17573
17574 static rs6000_stack_t *
17575 rs6000_stack_info (void)
17576 {
17577   static rs6000_stack_t info;
17578   rs6000_stack_t *info_ptr = &info;
17579   int reg_size = TARGET_32BIT ? 4 : 8;
17580   int ehrd_size;
17581   int save_align;
17582   int first_gp;
17583   HOST_WIDE_INT non_fixed_size;
17584
17585   memset (&info, 0, sizeof (info));
17586
17587   if (TARGET_SPE)
17588     {
17589       /* Cache value so we don't rescan instruction chain over and over.  */
17590       if (cfun->machine->insn_chain_scanned_p == 0)
17591         cfun->machine->insn_chain_scanned_p
17592           = spe_func_has_64bit_regs_p () + 1;
17593       info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
17594     }
17595
17596   /* Select which calling sequence.  */
17597   info_ptr->abi = DEFAULT_ABI;
17598
17599   /* Calculate which registers need to be saved & save area size.  */
17600   info_ptr->first_gp_reg_save = first_reg_to_save ();
17601   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
17602      even if it currently looks like we won't.  Reload may need it to
17603      get at a constant; if so, it will have already created a constant
17604      pool entry for it.  */
17605   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
17606        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
17607        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
17608       && crtl->uses_const_pool
17609       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
17610     first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
17611   else
17612     first_gp = info_ptr->first_gp_reg_save;
17613
17614   info_ptr->gp_size = reg_size * (32 - first_gp);
17615
17616   /* For the SPE, we have an additional upper 32-bits on each GPR.
17617      Ideally we should save the entire 64-bits only when the upper
17618      half is used in SIMD instructions.  Since we only record
17619      registers live (not the size they are used in), this proves
17620      difficult because we'd have to traverse the instruction chain at
17621      the right time, taking reload into account.  This is a real pain,
17622      so we opt to save the GPRs in 64-bits always if but one register
17623      gets used in 64-bits.  Otherwise, all the registers in the frame
17624      get saved in 32-bits.
17625
17626      So... since when we save all GPRs (except the SP) in 64-bits, the
17627      traditional GP save area will be empty.  */
17628   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17629     info_ptr->gp_size = 0;
17630
17631   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
17632   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
17633
17634   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
17635   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
17636                                  - info_ptr->first_altivec_reg_save);
17637
17638   /* Does this function call anything?  */
17639   info_ptr->calls_p = (! current_function_is_leaf
17640                        || cfun->machine->ra_needs_full_frame);
17641
17642   /* Determine if we need to save the link register.  */
17643   if ((DEFAULT_ABI == ABI_AIX
17644        && crtl->profile
17645        && !TARGET_PROFILE_KERNEL)
17646 #ifdef TARGET_RELOCATABLE
17647       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
17648 #endif
17649       || (info_ptr->first_fp_reg_save != 64
17650           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
17651       || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
17652       || info_ptr->calls_p
17653       || rs6000_ra_ever_killed ())
17654     {
17655       info_ptr->lr_save_p = 1;
17656       df_set_regs_ever_live (LR_REGNO, true);
17657     }
17658
17659   /* Determine if we need to save the condition code registers.  */
17660   if (df_regs_ever_live_p (CR2_REGNO)
17661       || df_regs_ever_live_p (CR3_REGNO)
17662       || df_regs_ever_live_p (CR4_REGNO))
17663     {
17664       info_ptr->cr_save_p = 1;
17665       if (DEFAULT_ABI == ABI_V4)
17666         info_ptr->cr_size = reg_size;
17667     }
17668
17669   /* If the current function calls __builtin_eh_return, then we need
17670      to allocate stack space for registers that will hold data for
17671      the exception handler.  */
17672   if (crtl->calls_eh_return)
17673     {
17674       unsigned int i;
17675       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
17676         continue;
17677
17678       /* SPE saves EH registers in 64-bits.  */
17679       ehrd_size = i * (TARGET_SPE_ABI
17680                        && info_ptr->spe_64bit_regs_used != 0
17681                        ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
17682     }
17683   else
17684     ehrd_size = 0;
17685
17686   /* Determine various sizes.  */
17687   info_ptr->reg_size     = reg_size;
17688   info_ptr->fixed_size   = RS6000_SAVE_AREA;
17689   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
17690   info_ptr->parm_size    = RS6000_ALIGN (crtl->outgoing_args_size,
17691                                          TARGET_ALTIVEC ? 16 : 8);
17692   if (FRAME_GROWS_DOWNWARD)
17693     info_ptr->vars_size
17694       += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
17695                        + info_ptr->parm_size,
17696                        ABI_STACK_BOUNDARY / BITS_PER_UNIT)
17697          - (info_ptr->fixed_size + info_ptr->vars_size
17698             + info_ptr->parm_size);
17699
17700   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17701     info_ptr->spe_gp_size = 8 * (32 - first_gp);
17702   else
17703     info_ptr->spe_gp_size = 0;
17704
17705   if (TARGET_ALTIVEC_ABI)
17706     info_ptr->vrsave_mask = compute_vrsave_mask ();
17707   else
17708     info_ptr->vrsave_mask = 0;
17709
17710   if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
17711     info_ptr->vrsave_size  = 4;
17712   else
17713     info_ptr->vrsave_size  = 0;
17714
17715   compute_save_world_info (info_ptr);
17716
17717   /* Calculate the offsets.  */
17718   switch (DEFAULT_ABI)
17719     {
17720     case ABI_NONE:
17721     default:
17722       gcc_unreachable ();
17723
17724     case ABI_AIX:
17725     case ABI_DARWIN:
17726       info_ptr->fp_save_offset   = - info_ptr->fp_size;
17727       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
17728
17729       if (TARGET_ALTIVEC_ABI)
17730         {
17731           info_ptr->vrsave_save_offset
17732             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
17733
17734           /* Align stack so vector save area is on a quadword boundary.
17735              The padding goes above the vectors.  */
17736           if (info_ptr->altivec_size != 0)
17737             info_ptr->altivec_padding_size
17738               = info_ptr->vrsave_save_offset & 0xF;
17739           else
17740             info_ptr->altivec_padding_size = 0;
17741
17742           info_ptr->altivec_save_offset
17743             = info_ptr->vrsave_save_offset
17744             - info_ptr->altivec_padding_size
17745             - info_ptr->altivec_size;
17746           gcc_assert (info_ptr->altivec_size == 0
17747                       || info_ptr->altivec_save_offset % 16 == 0);
17748
17749           /* Adjust for AltiVec case.  */
17750           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
17751         }
17752       else
17753         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
17754       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
17755       info_ptr->lr_save_offset   = 2*reg_size;
17756       break;
17757
17758     case ABI_V4:
17759       info_ptr->fp_save_offset   = - info_ptr->fp_size;
17760       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
17761       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
17762
17763       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17764         {
17765           /* Align stack so SPE GPR save area is aligned on a
17766              double-word boundary.  */
17767           if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
17768             info_ptr->spe_padding_size
17769               = 8 - (-info_ptr->cr_save_offset % 8);
17770           else
17771             info_ptr->spe_padding_size = 0;
17772
17773           info_ptr->spe_gp_save_offset
17774             = info_ptr->cr_save_offset
17775             - info_ptr->spe_padding_size
17776             - info_ptr->spe_gp_size;
17777
17778           /* Adjust for SPE case.  */
17779           info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
17780         }
17781       else if (TARGET_ALTIVEC_ABI)
17782         {
17783           info_ptr->vrsave_save_offset
17784             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
17785
17786           /* Align stack so vector save area is on a quadword boundary.  */
17787           if (info_ptr->altivec_size != 0)
17788             info_ptr->altivec_padding_size
17789               = 16 - (-info_ptr->vrsave_save_offset % 16);
17790           else
17791             info_ptr->altivec_padding_size = 0;
17792
17793           info_ptr->altivec_save_offset
17794             = info_ptr->vrsave_save_offset
17795             - info_ptr->altivec_padding_size
17796             - info_ptr->altivec_size;
17797
17798           /* Adjust for AltiVec case.  */
17799           info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
17800         }
17801       else
17802         info_ptr->ehrd_offset    = info_ptr->cr_save_offset;
17803       info_ptr->ehrd_offset      -= ehrd_size;
17804       info_ptr->lr_save_offset   = reg_size;
17805       break;
17806     }
17807
17808   save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
17809   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
17810                                          + info_ptr->gp_size
17811                                          + info_ptr->altivec_size
17812                                          + info_ptr->altivec_padding_size
17813                                          + info_ptr->spe_gp_size
17814                                          + info_ptr->spe_padding_size
17815                                          + ehrd_size
17816                                          + info_ptr->cr_size
17817                                          + info_ptr->vrsave_size,
17818                                          save_align);
17819
17820   non_fixed_size         = (info_ptr->vars_size
17821                             + info_ptr->parm_size
17822                             + info_ptr->save_size);
17823
17824   info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
17825                                        ABI_STACK_BOUNDARY / BITS_PER_UNIT);
17826
17827   /* Determine if we need to allocate any stack frame:
17828
17829      For AIX we need to push the stack if a frame pointer is needed
17830      (because the stack might be dynamically adjusted), if we are
17831      debugging, if we make calls, or if the sum of fp_save, gp_save,
17832      and local variables are more than the space needed to save all
17833      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
17834      + 18*8 = 288 (GPR13 reserved).
17835
17836      For V.4 we don't have the stack cushion that AIX uses, but assume
17837      that the debugger can handle stackless frames.  */
17838
17839   if (info_ptr->calls_p)
17840     info_ptr->push_p = 1;
17841
17842   else if (DEFAULT_ABI == ABI_V4)
17843     info_ptr->push_p = non_fixed_size != 0;
17844
17845   else if (frame_pointer_needed)
17846     info_ptr->push_p = 1;
17847
17848   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
17849     info_ptr->push_p = 1;
17850
17851   else
17852     info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
17853
17854   /* Zero offsets if we're not saving those registers.  */
17855   if (info_ptr->fp_size == 0)
17856     info_ptr->fp_save_offset = 0;
17857
17858   if (info_ptr->gp_size == 0)
17859     info_ptr->gp_save_offset = 0;
17860
17861   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
17862     info_ptr->altivec_save_offset = 0;
17863
17864   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
17865     info_ptr->vrsave_save_offset = 0;
17866
17867   if (! TARGET_SPE_ABI
17868       || info_ptr->spe_64bit_regs_used == 0
17869       || info_ptr->spe_gp_size == 0)
17870     info_ptr->spe_gp_save_offset = 0;
17871
17872   if (! info_ptr->lr_save_p)
17873     info_ptr->lr_save_offset = 0;
17874
17875   if (! info_ptr->cr_save_p)
17876     info_ptr->cr_save_offset = 0;
17877
17878   return info_ptr;
17879 }
17880
17881 /* Return true if the current function uses any GPRs in 64-bit SIMD
17882    mode.  */
17883
17884 static bool
17885 spe_func_has_64bit_regs_p (void)
17886 {
17887   rtx insns, insn;
17888
17889   /* Functions that save and restore all the call-saved registers will
17890      need to save/restore the registers in 64-bits.  */
17891   if (crtl->calls_eh_return
17892       || cfun->calls_setjmp
17893       || crtl->has_nonlocal_goto)
17894     return true;
17895
17896   insns = get_insns ();
17897
17898   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
17899     {
17900       if (INSN_P (insn))
17901         {
17902           rtx i;
17903
17904           /* FIXME: This should be implemented with attributes...
17905
17906                  (set_attr "spe64" "true")....then,
17907                  if (get_spe64(insn)) return true;
17908
17909              It's the only reliable way to do the stuff below.  */
17910
17911           i = PATTERN (insn);
17912           if (GET_CODE (i) == SET)
17913             {
17914               enum machine_mode mode = GET_MODE (SET_SRC (i));
17915
17916               if (SPE_VECTOR_MODE (mode))
17917                 return true;
17918               if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
17919                 return true;
17920             }
17921         }
17922     }
17923
17924   return false;
17925 }
17926
17927 static void
17928 debug_stack_info (rs6000_stack_t *info)
17929 {
17930   const char *abi_string;
17931
17932   if (! info)
17933     info = rs6000_stack_info ();
17934
17935   fprintf (stderr, "\nStack information for function %s:\n",
17936            ((current_function_decl && DECL_NAME (current_function_decl))
17937             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
17938             : "<unknown>"));
17939
17940   switch (info->abi)
17941     {
17942     default:             abi_string = "Unknown";        break;
17943     case ABI_NONE:       abi_string = "NONE";           break;
17944     case ABI_AIX:        abi_string = "AIX";            break;
17945     case ABI_DARWIN:     abi_string = "Darwin";         break;
17946     case ABI_V4:         abi_string = "V.4";            break;
17947     }
17948
17949   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
17950
17951   if (TARGET_ALTIVEC_ABI)
17952     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
17953
17954   if (TARGET_SPE_ABI)
17955     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
17956
17957   if (info->first_gp_reg_save != 32)
17958     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
17959
17960   if (info->first_fp_reg_save != 64)
17961     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
17962
17963   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
17964     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
17965              info->first_altivec_reg_save);
17966
17967   if (info->lr_save_p)
17968     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
17969
17970   if (info->cr_save_p)
17971     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
17972
17973   if (info->vrsave_mask)
17974     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
17975
17976   if (info->push_p)
17977     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
17978
17979   if (info->calls_p)
17980     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
17981
17982   if (info->gp_save_offset)
17983     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
17984
17985   if (info->fp_save_offset)
17986     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
17987
17988   if (info->altivec_save_offset)
17989     fprintf (stderr, "\taltivec_save_offset = %5d\n",
17990              info->altivec_save_offset);
17991
17992   if (info->spe_gp_save_offset)
17993     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
17994              info->spe_gp_save_offset);
17995
17996   if (info->vrsave_save_offset)
17997     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
17998              info->vrsave_save_offset);
17999
18000   if (info->lr_save_offset)
18001     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
18002
18003   if (info->cr_save_offset)
18004     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
18005
18006   if (info->varargs_save_offset)
18007     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
18008
18009   if (info->total_size)
18010     fprintf (stderr, "\ttotal_size          = "HOST_WIDE_INT_PRINT_DEC"\n",
18011              info->total_size);
18012
18013   if (info->vars_size)
18014     fprintf (stderr, "\tvars_size           = "HOST_WIDE_INT_PRINT_DEC"\n",
18015              info->vars_size);
18016
18017   if (info->parm_size)
18018     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
18019
18020   if (info->fixed_size)
18021     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
18022
18023   if (info->gp_size)
18024     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
18025
18026   if (info->spe_gp_size)
18027     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
18028
18029   if (info->fp_size)
18030     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
18031
18032   if (info->altivec_size)
18033     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
18034
18035   if (info->vrsave_size)
18036     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
18037
18038   if (info->altivec_padding_size)
18039     fprintf (stderr, "\taltivec_padding_size= %5d\n",
18040              info->altivec_padding_size);
18041
18042   if (info->spe_padding_size)
18043     fprintf (stderr, "\tspe_padding_size    = %5d\n",
18044              info->spe_padding_size);
18045
18046   if (info->cr_size)
18047     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
18048
18049   if (info->save_size)
18050     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
18051
18052   if (info->reg_size != 4)
18053     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
18054
18055   fprintf (stderr, "\n");
18056 }
18057
18058 rtx
18059 rs6000_return_addr (int count, rtx frame)
18060 {
18061   /* Currently we don't optimize very well between prolog and body
18062      code and for PIC code the code can be actually quite bad, so
18063      don't try to be too clever here.  */
18064   if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
18065     {
18066       cfun->machine->ra_needs_full_frame = 1;
18067
18068       return
18069         gen_rtx_MEM
18070           (Pmode,
18071            memory_address
18072            (Pmode,
18073             plus_constant (copy_to_reg
18074                            (gen_rtx_MEM (Pmode,
18075                                          memory_address (Pmode, frame))),
18076                            RETURN_ADDRESS_OFFSET)));
18077     }
18078
18079   cfun->machine->ra_need_lr = 1;
18080   return get_hard_reg_initial_val (Pmode, LR_REGNO);
18081 }
18082
18083 /* Say whether a function is a candidate for sibcall handling or not.
18084    We do not allow indirect calls to be optimized into sibling calls.
18085    Also, we can't do it if there are any vector parameters; there's
18086    nowhere to put the VRsave code so it works; note that functions with
18087    vector parameters are required to have a prototype, so the argument
18088    type info must be available here.  (The tail recursion case can work
18089    with vector parameters, but there's no way to distinguish here.) */
18090 static bool
18091 rs6000_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
18092 {
18093   tree type;
18094   if (decl)
18095     {
18096       if (TARGET_ALTIVEC_VRSAVE)
18097         {
18098           for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
18099                type; type = TREE_CHAIN (type))
18100             {
18101               if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
18102                 return false;
18103             }
18104         }
18105       if (DEFAULT_ABI == ABI_DARWIN
18106           || ((*targetm.binds_local_p) (decl)
18107               && (DEFAULT_ABI != ABI_AIX || !DECL_EXTERNAL (decl))))
18108         {
18109           tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
18110
18111           if (!lookup_attribute ("longcall", attr_list)
18112               || lookup_attribute ("shortcall", attr_list))
18113             return true;
18114         }
18115     }
18116   return false;
18117 }
18118
18119 /* NULL if INSN insn is valid within a low-overhead loop.
18120    Otherwise return why doloop cannot be applied.
18121    PowerPC uses the COUNT register for branch on table instructions.  */
18122
18123 static const char *
18124 rs6000_invalid_within_doloop (const_rtx insn)
18125 {
18126   if (CALL_P (insn))
18127     return "Function call in the loop.";
18128
18129   if (JUMP_P (insn)
18130       && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
18131           || GET_CODE (PATTERN (insn)) == ADDR_VEC))
18132     return "Computed branch in the loop.";
18133
18134   return NULL;
18135 }
18136
18137 static int
18138 rs6000_ra_ever_killed (void)
18139 {
18140   rtx top;
18141   rtx reg;
18142   rtx insn;
18143
18144   if (cfun->is_thunk)
18145     return 0;
18146
18147   if (cfun->machine->lr_save_state)
18148     return cfun->machine->lr_save_state - 1;
18149
18150   /* regs_ever_live has LR marked as used if any sibcalls are present,
18151      but this should not force saving and restoring in the
18152      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
18153      clobbers LR, so that is inappropriate.  */
18154
18155   /* Also, the prologue can generate a store into LR that
18156      doesn't really count, like this:
18157
18158         move LR->R0
18159         bcl to set PIC register
18160         move LR->R31
18161         move R0->LR
18162
18163      When we're called from the epilogue, we need to avoid counting
18164      this as a store.  */
18165
18166   push_topmost_sequence ();
18167   top = get_insns ();
18168   pop_topmost_sequence ();
18169   reg = gen_rtx_REG (Pmode, LR_REGNO);
18170
18171   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
18172     {
18173       if (INSN_P (insn))
18174         {
18175           if (CALL_P (insn))
18176             {
18177               if (!SIBLING_CALL_P (insn))
18178                 return 1;
18179             }
18180           else if (find_regno_note (insn, REG_INC, LR_REGNO))
18181             return 1;
18182           else if (set_of (reg, insn) != NULL_RTX
18183                    && !prologue_epilogue_contains (insn))
18184             return 1;
18185         }
18186     }
18187   return 0;
18188 }
18189 \f
18190 /* Emit instructions needed to load the TOC register.
18191    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
18192    a constant pool; or for SVR4 -fpic.  */
18193
18194 void
18195 rs6000_emit_load_toc_table (int fromprolog)
18196 {
18197   rtx dest;
18198   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
18199
18200   if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
18201     {
18202       char buf[30];
18203       rtx lab, tmp1, tmp2, got;
18204
18205       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
18206       lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18207       if (flag_pic == 2)
18208         got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18209       else
18210         got = rs6000_got_sym ();
18211       tmp1 = tmp2 = dest;
18212       if (!fromprolog)
18213         {
18214           tmp1 = gen_reg_rtx (Pmode);
18215           tmp2 = gen_reg_rtx (Pmode);
18216         }
18217       emit_insn (gen_load_toc_v4_PIC_1 (lab));
18218       emit_move_insn (tmp1,
18219                              gen_rtx_REG (Pmode, LR_REGNO));
18220       emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
18221       emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
18222     }
18223   else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
18224     {
18225       emit_insn (gen_load_toc_v4_pic_si ());
18226       emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18227     }
18228   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
18229     {
18230       char buf[30];
18231       rtx temp0 = (fromprolog
18232                    ? gen_rtx_REG (Pmode, 0)
18233                    : gen_reg_rtx (Pmode));
18234
18235       if (fromprolog)
18236         {
18237           rtx symF, symL;
18238
18239           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
18240           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18241
18242           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
18243           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18244
18245           emit_insn (gen_load_toc_v4_PIC_1 (symF));
18246           emit_move_insn (dest,
18247                           gen_rtx_REG (Pmode, LR_REGNO));
18248           emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
18249         }
18250       else
18251         {
18252           rtx tocsym, lab;
18253
18254           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18255           lab = gen_label_rtx ();
18256           emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
18257           emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18258           emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
18259         }
18260       emit_insn (gen_addsi3 (dest, temp0, dest));
18261     }
18262   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
18263     {
18264       /* This is for AIX code running in non-PIC ELF32.  */
18265       char buf[30];
18266       rtx realsym;
18267       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
18268       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18269
18270       emit_insn (gen_elf_high (dest, realsym));
18271       emit_insn (gen_elf_low (dest, dest, realsym));
18272     }
18273   else
18274     {
18275       gcc_assert (DEFAULT_ABI == ABI_AIX);
18276
18277       if (TARGET_32BIT)
18278         emit_insn (gen_load_toc_aix_si (dest));
18279       else
18280         emit_insn (gen_load_toc_aix_di (dest));
18281     }
18282 }
18283
18284 /* Emit instructions to restore the link register after determining where
18285    its value has been stored.  */
18286
18287 void
18288 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
18289 {
18290   rs6000_stack_t *info = rs6000_stack_info ();
18291   rtx operands[2];
18292
18293   operands[0] = source;
18294   operands[1] = scratch;
18295
18296   if (info->lr_save_p)
18297     {
18298       rtx frame_rtx = stack_pointer_rtx;
18299       HOST_WIDE_INT sp_offset = 0;
18300       rtx tmp;
18301
18302       if (frame_pointer_needed
18303           || cfun->calls_alloca
18304           || info->total_size > 32767)
18305         {
18306           tmp = gen_frame_mem (Pmode, frame_rtx);
18307           emit_move_insn (operands[1], tmp);
18308           frame_rtx = operands[1];
18309         }
18310       else if (info->push_p)
18311         sp_offset = info->total_size;
18312
18313       tmp = plus_constant (frame_rtx, info->lr_save_offset + sp_offset);
18314       tmp = gen_frame_mem (Pmode, tmp);
18315       emit_move_insn (tmp, operands[0]);
18316     }
18317   else
18318     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
18319
18320   /* Freeze lr_save_p.  We've just emitted rtl that depends on the
18321      state of lr_save_p so any change from here on would be a bug.  In
18322      particular, stop rs6000_ra_ever_killed from considering the SET
18323      of lr we may have added just above.  */ 
18324   cfun->machine->lr_save_state = info->lr_save_p + 1;
18325 }
18326
18327 static GTY(()) alias_set_type set = -1;
18328
18329 alias_set_type
18330 get_TOC_alias_set (void)
18331 {
18332   if (set == -1)
18333     set = new_alias_set ();
18334   return set;
18335 }
18336
18337 /* This returns nonzero if the current function uses the TOC.  This is
18338    determined by the presence of (use (unspec ... UNSPEC_TOC)), which
18339    is generated by the ABI_V4 load_toc_* patterns.  */
18340 #if TARGET_ELF
18341 static int
18342 uses_TOC (void)
18343 {
18344   rtx insn;
18345
18346   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18347     if (INSN_P (insn))
18348       {
18349         rtx pat = PATTERN (insn);
18350         int i;
18351
18352         if (GET_CODE (pat) == PARALLEL)
18353           for (i = 0; i < XVECLEN (pat, 0); i++)
18354             {
18355               rtx sub = XVECEXP (pat, 0, i);
18356               if (GET_CODE (sub) == USE)
18357                 {
18358                   sub = XEXP (sub, 0);
18359                   if (GET_CODE (sub) == UNSPEC
18360                       && XINT (sub, 1) == UNSPEC_TOC)
18361                     return 1;
18362                 }
18363             }
18364       }
18365   return 0;
18366 }
18367 #endif
18368
18369 rtx
18370 create_TOC_reference (rtx symbol, rtx largetoc_reg)
18371 {
18372   rtx tocrel, tocreg;
18373
18374   if (TARGET_DEBUG_ADDR)
18375     {
18376       if (GET_CODE (symbol) == SYMBOL_REF)
18377         fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
18378                  XSTR (symbol, 0));
18379       else
18380         {
18381           fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
18382                    GET_RTX_NAME (GET_CODE (symbol)));
18383           debug_rtx (symbol);
18384         }
18385     }
18386
18387   if (!can_create_pseudo_p ())
18388     df_set_regs_ever_live (TOC_REGISTER, true);
18389
18390   tocrel = gen_rtx_CONST (Pmode,
18391                           gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol),
18392                                           UNSPEC_TOCREL));
18393   tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
18394   if (TARGET_CMODEL != CMODEL_SMALL)
18395     {
18396       rtx hi = gen_rtx_PLUS (Pmode, tocreg, gen_rtx_HIGH (Pmode, tocrel));
18397       if (largetoc_reg != NULL)
18398         {
18399           emit_move_insn (largetoc_reg, hi);
18400           hi = largetoc_reg;
18401         }
18402       return gen_rtx_LO_SUM (Pmode, hi, copy_rtx (tocrel));
18403     }
18404   else
18405     return gen_rtx_PLUS (Pmode, tocreg, tocrel);
18406 }
18407
18408 /* Issue assembly directives that create a reference to the given DWARF
18409    FRAME_TABLE_LABEL from the current function section.  */
18410 void
18411 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
18412 {
18413   fprintf (asm_out_file, "\t.ref %s\n",
18414            TARGET_STRIP_NAME_ENCODING (frame_table_label));
18415 }
18416
18417 /* If _Unwind_* has been called from within the same module,
18418    toc register is not guaranteed to be saved to 40(1) on function
18419    entry.  Save it there in that case.  */
18420
18421 void
18422 rs6000_aix_emit_builtin_unwind_init (void)
18423 {
18424   rtx mem;
18425   rtx stack_top = gen_reg_rtx (Pmode);
18426   rtx opcode_addr = gen_reg_rtx (Pmode);
18427   rtx opcode = gen_reg_rtx (SImode);
18428   rtx tocompare = gen_reg_rtx (SImode);
18429   rtx no_toc_save_needed = gen_label_rtx ();
18430
18431   mem = gen_frame_mem (Pmode, hard_frame_pointer_rtx);
18432   emit_move_insn (stack_top, mem);
18433
18434   mem = gen_frame_mem (Pmode,
18435                        gen_rtx_PLUS (Pmode, stack_top,
18436                                      GEN_INT (2 * GET_MODE_SIZE (Pmode))));
18437   emit_move_insn (opcode_addr, mem);
18438   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
18439   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014
18440                                            : 0xE8410028, SImode));
18441
18442   do_compare_rtx_and_jump (opcode, tocompare, EQ, 1,
18443                            SImode, NULL_RTX, NULL_RTX,
18444                            no_toc_save_needed, -1);
18445
18446   mem = gen_frame_mem (Pmode,
18447                        gen_rtx_PLUS (Pmode, stack_top,
18448                                      GEN_INT (5 * GET_MODE_SIZE (Pmode))));
18449   emit_move_insn (mem, gen_rtx_REG (Pmode, 2));
18450   emit_label (no_toc_save_needed);
18451 }
18452 \f
18453 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
18454    and the change to the stack pointer.  */
18455
18456 static void
18457 rs6000_emit_stack_tie (void)
18458 {
18459   rtx mem = gen_frame_mem (BLKmode,
18460                            gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
18461
18462   emit_insn (gen_stack_tie (mem));
18463 }
18464
18465 /* Emit the correct code for allocating stack space, as insns.
18466    If COPY_REG, make sure a copy of the old frame is left there.
18467    The generated code may use hard register 0 as a temporary.  */
18468
18469 static void
18470 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg)
18471 {
18472   rtx insn;
18473   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18474   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
18475   rtx todec = gen_int_mode (-size, Pmode);
18476   rtx par, set, mem;
18477
18478   if (INTVAL (todec) != -size)
18479     {
18480       warning (0, "stack frame too large");
18481       emit_insn (gen_trap ());
18482       return;
18483     }
18484
18485   if (crtl->limit_stack)
18486     {
18487       if (REG_P (stack_limit_rtx)
18488           && REGNO (stack_limit_rtx) > 1
18489           && REGNO (stack_limit_rtx) <= 31)
18490         {
18491           emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
18492           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18493                                     const0_rtx));
18494         }
18495       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
18496                && TARGET_32BIT
18497                && DEFAULT_ABI == ABI_V4)
18498         {
18499           rtx toload = gen_rtx_CONST (VOIDmode,
18500                                       gen_rtx_PLUS (Pmode,
18501                                                     stack_limit_rtx,
18502                                                     GEN_INT (size)));
18503
18504           emit_insn (gen_elf_high (tmp_reg, toload));
18505           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
18506           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18507                                     const0_rtx));
18508         }
18509       else
18510         warning (0, "stack limit expression is not supported");
18511     }
18512
18513   if (copy_reg)
18514     emit_move_insn (copy_reg, stack_reg);
18515
18516   if (size > 32767)
18517     {
18518       /* Need a note here so that try_split doesn't get confused.  */
18519       if (get_last_insn () == NULL_RTX)
18520         emit_note (NOTE_INSN_DELETED);
18521       insn = emit_move_insn (tmp_reg, todec);
18522       try_split (PATTERN (insn), insn, 0);
18523       todec = tmp_reg;
18524     }
18525   
18526   insn = emit_insn (TARGET_32BIT
18527                     ? gen_movsi_update_stack (stack_reg, stack_reg,
18528                                         todec, stack_reg)
18529                     : gen_movdi_di_update_stack (stack_reg, stack_reg,
18530                                            todec, stack_reg));
18531   /* Since we didn't use gen_frame_mem to generate the MEM, grab
18532      it now and set the alias set/attributes. The above gen_*_update
18533      calls will generate a PARALLEL with the MEM set being the first
18534      operation. */
18535   par = PATTERN (insn);
18536   gcc_assert (GET_CODE (par) == PARALLEL);
18537   set = XVECEXP (par, 0, 0);
18538   gcc_assert (GET_CODE (set) == SET);
18539   mem = SET_DEST (set);
18540   gcc_assert (MEM_P (mem));
18541   MEM_NOTRAP_P (mem) = 1;
18542   set_mem_alias_set (mem, get_frame_alias_set ());
18543
18544   RTX_FRAME_RELATED_P (insn) = 1;
18545   add_reg_note (insn, REG_FRAME_RELATED_EXPR,
18546                 gen_rtx_SET (VOIDmode, stack_reg,
18547                              gen_rtx_PLUS (Pmode, stack_reg,
18548                                            GEN_INT (-size))));
18549 }
18550
18551 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
18552    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
18553    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
18554    deduce these equivalences by itself so it wasn't necessary to hold
18555    its hand so much.  */
18556
18557 static void
18558 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
18559                       rtx reg2, rtx rreg)
18560 {
18561   rtx real, temp;
18562
18563   /* copy_rtx will not make unique copies of registers, so we need to
18564      ensure we don't have unwanted sharing here.  */
18565   if (reg == reg2)
18566     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18567
18568   if (reg == rreg)
18569     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18570
18571   real = copy_rtx (PATTERN (insn));
18572
18573   if (reg2 != NULL_RTX)
18574     real = replace_rtx (real, reg2, rreg);
18575
18576   real = replace_rtx (real, reg,
18577                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
18578                                                         STACK_POINTER_REGNUM),
18579                                     GEN_INT (val)));
18580
18581   /* We expect that 'real' is either a SET or a PARALLEL containing
18582      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
18583      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
18584
18585   if (GET_CODE (real) == SET)
18586     {
18587       rtx set = real;
18588
18589       temp = simplify_rtx (SET_SRC (set));
18590       if (temp)
18591         SET_SRC (set) = temp;
18592       temp = simplify_rtx (SET_DEST (set));
18593       if (temp)
18594         SET_DEST (set) = temp;
18595       if (GET_CODE (SET_DEST (set)) == MEM)
18596         {
18597           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18598           if (temp)
18599             XEXP (SET_DEST (set), 0) = temp;
18600         }
18601     }
18602   else
18603     {
18604       int i;
18605
18606       gcc_assert (GET_CODE (real) == PARALLEL);
18607       for (i = 0; i < XVECLEN (real, 0); i++)
18608         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
18609           {
18610             rtx set = XVECEXP (real, 0, i);
18611
18612             temp = simplify_rtx (SET_SRC (set));
18613             if (temp)
18614               SET_SRC (set) = temp;
18615             temp = simplify_rtx (SET_DEST (set));
18616             if (temp)
18617               SET_DEST (set) = temp;
18618             if (GET_CODE (SET_DEST (set)) == MEM)
18619               {
18620                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18621                 if (temp)
18622                   XEXP (SET_DEST (set), 0) = temp;
18623               }
18624             RTX_FRAME_RELATED_P (set) = 1;
18625           }
18626     }
18627
18628   RTX_FRAME_RELATED_P (insn) = 1;
18629   add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
18630 }
18631
18632 /* Returns an insn that has a vrsave set operation with the
18633    appropriate CLOBBERs.  */
18634
18635 static rtx
18636 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
18637 {
18638   int nclobs, i;
18639   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
18640   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
18641
18642   clobs[0]
18643     = gen_rtx_SET (VOIDmode,
18644                    vrsave,
18645                    gen_rtx_UNSPEC_VOLATILE (SImode,
18646                                             gen_rtvec (2, reg, vrsave),
18647                                             UNSPECV_SET_VRSAVE));
18648
18649   nclobs = 1;
18650
18651   /* We need to clobber the registers in the mask so the scheduler
18652      does not move sets to VRSAVE before sets of AltiVec registers.
18653
18654      However, if the function receives nonlocal gotos, reload will set
18655      all call saved registers live.  We will end up with:
18656
18657         (set (reg 999) (mem))
18658         (parallel [ (set (reg vrsave) (unspec blah))
18659                     (clobber (reg 999))])
18660
18661      The clobber will cause the store into reg 999 to be dead, and
18662      flow will attempt to delete an epilogue insn.  In this case, we
18663      need an unspec use/set of the register.  */
18664
18665   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
18666     if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
18667       {
18668         if (!epiloguep || call_used_regs [i])
18669           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
18670                                              gen_rtx_REG (V4SImode, i));
18671         else
18672           {
18673             rtx reg = gen_rtx_REG (V4SImode, i);
18674
18675             clobs[nclobs++]
18676               = gen_rtx_SET (VOIDmode,
18677                              reg,
18678                              gen_rtx_UNSPEC (V4SImode,
18679                                              gen_rtvec (1, reg), 27));
18680           }
18681       }
18682
18683   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
18684
18685   for (i = 0; i < nclobs; ++i)
18686     XVECEXP (insn, 0, i) = clobs[i];
18687
18688   return insn;
18689 }
18690
18691 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
18692    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
18693
18694 static void
18695 emit_frame_save (rtx frame_reg, rtx frame_ptr, enum machine_mode mode,
18696                  unsigned int regno, int offset, HOST_WIDE_INT total_size)
18697 {
18698   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
18699   rtx replacea, replaceb;
18700
18701   int_rtx = GEN_INT (offset);
18702
18703   /* Some cases that need register indexed addressing.  */
18704   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
18705       || (TARGET_VSX && VSX_VECTOR_MODE (mode))
18706       || (TARGET_E500_DOUBLE && mode == DFmode)
18707       || (TARGET_SPE_ABI
18708           && SPE_VECTOR_MODE (mode)
18709           && !SPE_CONST_OFFSET_OK (offset)))
18710     {
18711       /* Whomever calls us must make sure r11 is available in the
18712          flow path of instructions in the prologue.  */
18713       offset_rtx = gen_rtx_REG (Pmode, 11);
18714       emit_move_insn (offset_rtx, int_rtx);
18715
18716       replacea = offset_rtx;
18717       replaceb = int_rtx;
18718     }
18719   else
18720     {
18721       offset_rtx = int_rtx;
18722       replacea = NULL_RTX;
18723       replaceb = NULL_RTX;
18724     }
18725
18726   reg = gen_rtx_REG (mode, regno);
18727   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
18728   mem = gen_frame_mem (mode, addr);
18729
18730   insn = emit_move_insn (mem, reg);
18731
18732   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
18733 }
18734
18735 /* Emit an offset memory reference suitable for a frame store, while
18736    converting to a valid addressing mode.  */
18737
18738 static rtx
18739 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
18740 {
18741   rtx int_rtx, offset_rtx;
18742
18743   int_rtx = GEN_INT (offset);
18744
18745   if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
18746       || (TARGET_E500_DOUBLE && mode == DFmode))
18747     {
18748       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
18749       emit_move_insn (offset_rtx, int_rtx);
18750     }
18751   else
18752     offset_rtx = int_rtx;
18753
18754   return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
18755 }
18756
18757 /* Look for user-defined global regs.  We should not save and restore these,
18758    and cannot use stmw/lmw if there are any in its range.  */
18759
18760 static bool
18761 no_global_regs_above (int first, bool gpr)
18762 {
18763   int i;
18764   int last = gpr ? 32 : 64;
18765   for (i = first; i < last; i++)
18766     if (global_regs[i])
18767       return false;
18768   return true;
18769 }
18770
18771 #ifndef TARGET_FIX_AND_CONTINUE
18772 #define TARGET_FIX_AND_CONTINUE 0
18773 #endif
18774
18775 /* It's really GPR 13 and FPR 14, but we need the smaller of the two.  */
18776 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
18777 #define LAST_SAVRES_REGISTER 31
18778 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
18779
18780 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][8];
18781
18782 /* Temporary holding space for an out-of-line register save/restore
18783    routine name.  */
18784 static char savres_routine_name[30];
18785
18786 /* Return the name for an out-of-line register save/restore routine.
18787    We are saving/restoring GPRs if GPR is true.  */
18788
18789 static char *
18790 rs6000_savres_routine_name (rs6000_stack_t *info, int regno,
18791                             bool savep, bool gpr, bool lr)
18792 {
18793   const char *prefix = "";
18794   const char *suffix = "";
18795
18796   /* Different targets are supposed to define
18797      {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
18798      routine name could be defined with:
18799
18800      sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
18801
18802      This is a nice idea in practice, but in reality, things are
18803      complicated in several ways:
18804
18805      - ELF targets have save/restore routines for GPRs.
18806
18807      - SPE targets use different prefixes for 32/64-bit registers, and
18808        neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
18809
18810      - PPC64 ELF targets have routines for save/restore of GPRs that
18811        differ in what they do with the link register, so having a set
18812        prefix doesn't work.  (We only use one of the save routines at
18813        the moment, though.)
18814
18815      - PPC32 elf targets have "exit" versions of the restore routines
18816        that restore the link register and can save some extra space.
18817        These require an extra suffix.  (There are also "tail" versions
18818        of the restore routines and "GOT" versions of the save routines,
18819        but we don't generate those at present.  Same problems apply,
18820        though.)
18821
18822      We deal with all this by synthesizing our own prefix/suffix and
18823      using that for the simple sprintf call shown above.  */
18824   if (TARGET_SPE)
18825     {
18826       /* No floating point saves on the SPE.  */
18827       gcc_assert (gpr);
18828
18829       if (savep)
18830         prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
18831       else
18832         prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
18833
18834       if (lr)
18835         suffix = "_x";
18836     }
18837   else if (DEFAULT_ABI == ABI_V4)
18838     {
18839       if (TARGET_64BIT)
18840         goto aix_names;
18841
18842       if (gpr)
18843         prefix = savep ? "_savegpr_" : "_restgpr_";
18844       else
18845         prefix = savep ? "_savefpr_" : "_restfpr_";
18846
18847       if (lr)
18848         suffix = "_x";
18849     }
18850   else if (DEFAULT_ABI == ABI_AIX)
18851     {
18852 #ifndef POWERPC_LINUX
18853       /* No out-of-line save/restore routines for GPRs on AIX.  */
18854       gcc_assert (!TARGET_AIX || !gpr);
18855 #endif
18856
18857     aix_names:
18858       if (gpr)
18859         prefix = (savep
18860                   ? (lr ? "_savegpr0_" : "_savegpr1_")
18861                   : (lr ? "_restgpr0_" : "_restgpr1_"));
18862 #ifdef POWERPC_LINUX
18863       else if (lr)
18864         prefix = (savep ? "_savefpr_" : "_restfpr_");
18865 #endif
18866       else
18867         {
18868           prefix = savep ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
18869           suffix = savep ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
18870         }
18871     }
18872   else if (DEFAULT_ABI == ABI_DARWIN)
18873     sorry ("Out-of-line save/restore routines not supported on Darwin");
18874
18875   sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
18876
18877   return savres_routine_name;
18878 }
18879
18880 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
18881    We are saving/restoring GPRs if GPR is true.  */
18882
18883 static rtx
18884 rs6000_savres_routine_sym (rs6000_stack_t *info, bool savep,
18885                            bool gpr, bool lr)
18886 {
18887   int regno = gpr ? info->first_gp_reg_save : (info->first_fp_reg_save - 32);
18888   rtx sym;
18889   int select = ((savep ? 1 : 0) << 2
18890                 | ((TARGET_SPE_ABI
18891                     /* On the SPE, we never have any FPRs, but we do have
18892                        32/64-bit versions of the routines.  */
18893                     ? (info->spe_64bit_regs_used ? 1 : 0)
18894                     : (gpr ? 1 : 0)) << 1)
18895                 | (lr ? 1: 0));
18896
18897   /* Don't generate bogus routine names.  */
18898   gcc_assert (FIRST_SAVRES_REGISTER <= regno
18899               && regno <= LAST_SAVRES_REGISTER);
18900
18901   sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
18902
18903   if (sym == NULL)
18904     {
18905       char *name;
18906
18907       name = rs6000_savres_routine_name (info, regno, savep, gpr, lr);
18908
18909       sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
18910         = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
18911       SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
18912     }
18913
18914   return sym;
18915 }
18916
18917 /* Emit a sequence of insns, including a stack tie if needed, for
18918    resetting the stack pointer.  If SAVRES is true, then don't reset the
18919    stack pointer, but move the base of the frame into r11 for use by
18920    out-of-line register restore routines.  */
18921
18922 static rtx
18923 rs6000_emit_stack_reset (rs6000_stack_t *info,
18924                          rtx sp_reg_rtx, rtx frame_reg_rtx,
18925                          int sp_offset, bool savres)
18926 {
18927   /* This blockage is needed so that sched doesn't decide to move
18928      the sp change before the register restores.  */
18929   if (frame_reg_rtx != sp_reg_rtx
18930       || (TARGET_SPE_ABI
18931           && info->spe_64bit_regs_used != 0
18932           && info->first_gp_reg_save != 32))
18933     rs6000_emit_stack_tie ();
18934   
18935   if (frame_reg_rtx != sp_reg_rtx)
18936     {
18937       if (sp_offset != 0)
18938         {
18939           rtx dest_reg = savres ? gen_rtx_REG (Pmode, 11) : sp_reg_rtx;
18940           return emit_insn (gen_add3_insn (dest_reg, frame_reg_rtx,
18941                                            GEN_INT (sp_offset)));
18942         }
18943       else if (!savres)
18944         return emit_move_insn (sp_reg_rtx, frame_reg_rtx);
18945     }
18946   else if (sp_offset != 0)
18947     {
18948       /* If we are restoring registers out-of-line, we will be using the
18949          "exit" variants of the restore routines, which will reset the
18950          stack for us.  But we do need to point r11 into the right place
18951          for those routines.  */
18952       rtx dest_reg = (savres
18953                       ? gen_rtx_REG (Pmode, 11)
18954                       : sp_reg_rtx);
18955
18956       rtx insn = emit_insn (gen_add3_insn (dest_reg, sp_reg_rtx,
18957                                            GEN_INT (sp_offset)));
18958       if (!savres)
18959         return insn;
18960     }
18961   return NULL_RTX;
18962 }
18963
18964 /* Construct a parallel rtx describing the effect of a call to an
18965    out-of-line register save/restore routine.  */
18966
18967 static rtx
18968 rs6000_make_savres_rtx (rs6000_stack_t *info,
18969                         rtx frame_reg_rtx, int save_area_offset,
18970                         enum machine_mode reg_mode,
18971                         bool savep, bool gpr, bool lr)
18972 {
18973   int i;
18974   int offset, start_reg, end_reg, n_regs;
18975   int reg_size = GET_MODE_SIZE (reg_mode);
18976   rtx sym;
18977   rtvec p;
18978
18979   offset = 0;
18980   start_reg = (gpr
18981                ? info->first_gp_reg_save
18982                : info->first_fp_reg_save);
18983   end_reg = gpr ? 32 : 64;
18984   n_regs = end_reg - start_reg;
18985   p = rtvec_alloc ((lr ? 4 : 3) + n_regs);
18986
18987   if (!savep && lr)
18988     RTVEC_ELT (p, offset++) = gen_rtx_RETURN (VOIDmode);
18989
18990   RTVEC_ELT (p, offset++)
18991     = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 65));
18992
18993   sym = rs6000_savres_routine_sym (info, savep, gpr, lr);
18994   RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
18995   RTVEC_ELT (p, offset++)
18996     = gen_rtx_USE (VOIDmode,
18997                    gen_rtx_REG (Pmode, DEFAULT_ABI != ABI_AIX ? 11
18998                                        : gpr && !lr ? 12
18999                                        : 1));
19000
19001   for (i = 0; i < end_reg - start_reg; i++)
19002     {
19003       rtx addr, reg, mem;
19004       reg = gen_rtx_REG (reg_mode, start_reg + i);
19005       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19006                            GEN_INT (save_area_offset + reg_size*i));
19007       mem = gen_frame_mem (reg_mode, addr);
19008
19009       RTVEC_ELT (p, i + offset) = gen_rtx_SET (VOIDmode,
19010                                                savep ? mem : reg,
19011                                                savep ? reg : mem);
19012     }
19013
19014   if (savep && lr)
19015     {
19016       rtx addr, reg, mem;
19017       reg = gen_rtx_REG (Pmode, 0);
19018       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19019                            GEN_INT (info->lr_save_offset));
19020       mem = gen_frame_mem (Pmode, addr);
19021       RTVEC_ELT (p, i + offset) = gen_rtx_SET (VOIDmode, mem, reg);
19022     }
19023
19024   return gen_rtx_PARALLEL (VOIDmode, p);
19025 }
19026
19027 /* Determine whether the gp REG is really used.  */
19028
19029 static bool
19030 rs6000_reg_live_or_pic_offset_p (int reg)
19031 {
19032   return ((df_regs_ever_live_p (reg)
19033            && (!call_used_regs[reg]
19034                || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19035                    && TARGET_TOC && TARGET_MINIMAL_TOC)))
19036           || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19037               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
19038                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
19039 }
19040
19041 enum {
19042   SAVRES_MULTIPLE = 0x1,
19043   SAVRES_INLINE_FPRS = 0x2,
19044   SAVRES_INLINE_GPRS = 0x4,
19045   SAVRES_NOINLINE_GPRS_SAVES_LR = 0x8,
19046   SAVRES_NOINLINE_FPRS_SAVES_LR = 0x10,
19047   SAVRES_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x20
19048 };
19049
19050 /* Determine the strategy for savings/restoring registers.  */
19051
19052 static int
19053 rs6000_savres_strategy (rs6000_stack_t *info, bool savep,
19054                         int using_static_chain_p, int sibcall)
19055 {
19056   bool using_multiple_p;
19057   bool common;
19058   bool savres_fprs_inline;
19059   bool savres_gprs_inline;
19060   bool noclobber_global_gprs
19061     = no_global_regs_above (info->first_gp_reg_save, /*gpr=*/true);
19062   int strategy;
19063
19064   using_multiple_p = (TARGET_MULTIPLE && ! TARGET_POWERPC64
19065                       && (!TARGET_SPE_ABI
19066                           || info->spe_64bit_regs_used == 0)
19067                       && info->first_gp_reg_save < 31
19068                       && noclobber_global_gprs);
19069   /* Don't bother to try to save things out-of-line if r11 is occupied
19070      by the static chain.  It would require too much fiddling and the
19071      static chain is rarely used anyway.  */
19072   common = (using_static_chain_p
19073             || sibcall
19074             || crtl->calls_eh_return
19075             || !info->lr_save_p
19076             || cfun->machine->ra_need_lr
19077             || info->total_size > 32767);
19078   savres_fprs_inline = (common
19079                         || info->first_fp_reg_save == 64
19080                         || !no_global_regs_above (info->first_fp_reg_save,
19081                                                   /*gpr=*/false)
19082                         /* The out-of-line FP routines use
19083                            double-precision stores; we can't use those
19084                            routines if we don't have such stores.  */
19085                         || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
19086                         || FP_SAVE_INLINE (info->first_fp_reg_save));
19087   savres_gprs_inline = (common
19088                         /* Saving CR interferes with the exit routines
19089                            used on the SPE, so just punt here.  */
19090                         || (!savep
19091                             && TARGET_SPE_ABI
19092                             && info->spe_64bit_regs_used != 0
19093                             && info->cr_save_p != 0)
19094                         || info->first_gp_reg_save == 32
19095                         || !noclobber_global_gprs
19096                         || GP_SAVE_INLINE (info->first_gp_reg_save));
19097
19098   if (savep)
19099     /* If we are going to use store multiple, then don't even bother
19100      with the out-of-line routines, since the store-multiple instruction
19101      will always be smaller.  */
19102     savres_gprs_inline = savres_gprs_inline || using_multiple_p;
19103   else
19104     {
19105       /* The situation is more complicated with load multiple.  We'd
19106          prefer to use the out-of-line routines for restores, since the
19107          "exit" out-of-line routines can handle the restore of LR and
19108          the frame teardown.  But we can only use the out-of-line
19109          routines if we know that we've used store multiple or
19110          out-of-line routines in the prologue, i.e. if we've saved all
19111          the registers from first_gp_reg_save.  Otherwise, we risk
19112          loading garbage from the stack.  Furthermore, we can only use
19113          the "exit" out-of-line gpr restore if we haven't saved any
19114          fprs.  */
19115       bool saved_all = !savres_gprs_inline || using_multiple_p;
19116
19117       if (saved_all && info->first_fp_reg_save != 64)
19118         /* We can't use the exit routine; use load multiple if it's
19119            available.  */
19120         savres_gprs_inline = savres_gprs_inline || using_multiple_p;
19121     }
19122
19123   strategy = (using_multiple_p
19124               | (savres_fprs_inline << 1)
19125               | (savres_gprs_inline << 2));
19126 #ifdef POWERPC_LINUX
19127   if (TARGET_64BIT)
19128     {
19129       if (!savres_fprs_inline)
19130         strategy |= SAVRES_NOINLINE_FPRS_SAVES_LR;
19131       else if (!savres_gprs_inline && info->first_fp_reg_save == 64)
19132         strategy |= SAVRES_NOINLINE_GPRS_SAVES_LR;
19133     }
19134 #else
19135   if (TARGET_AIX && !savres_fprs_inline)
19136     strategy |= SAVRES_NOINLINE_FPRS_DOESNT_RESTORE_LR;
19137 #endif
19138   return strategy;
19139 }
19140
19141 /* Emit function prologue as insns.  */
19142
19143 void
19144 rs6000_emit_prologue (void)
19145 {
19146   rs6000_stack_t *info = rs6000_stack_info ();
19147   enum machine_mode reg_mode = Pmode;
19148   int reg_size = TARGET_32BIT ? 4 : 8;
19149   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
19150   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
19151   rtx frame_reg_rtx = sp_reg_rtx;
19152   rtx cr_save_rtx = NULL_RTX;
19153   rtx insn;
19154   int strategy;
19155   int saving_FPRs_inline;
19156   int saving_GPRs_inline;
19157   int using_store_multiple;
19158   int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19159                               && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19160                               && call_used_regs[STATIC_CHAIN_REGNUM]);
19161   HOST_WIDE_INT sp_offset = 0;
19162
19163   if (TARGET_FIX_AND_CONTINUE)
19164     {
19165       /* gdb on darwin arranges to forward a function from the old
19166          address by modifying the first 5 instructions of the function
19167          to branch to the overriding function.  This is necessary to
19168          permit function pointers that point to the old function to
19169          actually forward to the new function.  */
19170       emit_insn (gen_nop ());
19171       emit_insn (gen_nop ());
19172       emit_insn (gen_nop ());
19173       emit_insn (gen_nop ());
19174       emit_insn (gen_nop ());
19175     }
19176
19177   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
19178     {
19179       reg_mode = V2SImode;
19180       reg_size = 8;
19181     }
19182
19183   strategy = rs6000_savres_strategy (info, /*savep=*/true,
19184                                      /*static_chain_p=*/using_static_chain_p,
19185                                      /*sibcall=*/0);
19186   using_store_multiple = strategy & SAVRES_MULTIPLE;
19187   saving_FPRs_inline = strategy & SAVRES_INLINE_FPRS;
19188   saving_GPRs_inline = strategy & SAVRES_INLINE_GPRS;
19189
19190   /* For V.4, update stack before we do any saving and set back pointer.  */
19191   if (! WORLD_SAVE_P (info)
19192       && info->push_p
19193       && (DEFAULT_ABI == ABI_V4
19194           || crtl->calls_eh_return))
19195     {
19196       bool need_r11 = (TARGET_SPE
19197                        ? (!saving_GPRs_inline
19198                           && info->spe_64bit_regs_used == 0)
19199                        : (!saving_FPRs_inline || !saving_GPRs_inline));
19200       rtx copy_reg = need_r11 ? gen_rtx_REG (Pmode, 11) : NULL;
19201
19202       if (info->total_size < 32767)
19203         sp_offset = info->total_size;
19204       else if (need_r11)
19205         frame_reg_rtx = copy_reg;
19206       else if (info->cr_save_p
19207                || info->lr_save_p
19208                || info->first_fp_reg_save < 64
19209                || info->first_gp_reg_save < 32
19210                || info->altivec_size != 0
19211                || info->vrsave_mask != 0
19212                || crtl->calls_eh_return)
19213         {
19214           copy_reg = frame_ptr_rtx;
19215           frame_reg_rtx = copy_reg;
19216         }
19217       else
19218         {
19219           /* The prologue won't be saving any regs so there is no need
19220              to set up a frame register to access any frame save area.
19221              We also won't be using sp_offset anywhere below, but set
19222              the correct value anyway to protect against future
19223              changes to this function.  */
19224           sp_offset = info->total_size;
19225         }
19226       rs6000_emit_allocate_stack (info->total_size, copy_reg);
19227       if (frame_reg_rtx != sp_reg_rtx)
19228         rs6000_emit_stack_tie ();
19229     }
19230
19231   /* Handle world saves specially here.  */
19232   if (WORLD_SAVE_P (info))
19233     {
19234       int i, j, sz;
19235       rtx treg;
19236       rtvec p;
19237       rtx reg0;
19238
19239       /* save_world expects lr in r0. */
19240       reg0 = gen_rtx_REG (Pmode, 0);
19241       if (info->lr_save_p)
19242         {
19243           insn = emit_move_insn (reg0,
19244                                  gen_rtx_REG (Pmode, LR_REGNO));
19245           RTX_FRAME_RELATED_P (insn) = 1;
19246         }
19247
19248       /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
19249          assumptions about the offsets of various bits of the stack
19250          frame.  */
19251       gcc_assert (info->gp_save_offset == -220
19252                   && info->fp_save_offset == -144
19253                   && info->lr_save_offset == 8
19254                   && info->cr_save_offset == 4
19255                   && info->push_p
19256                   && info->lr_save_p
19257                   && (!crtl->calls_eh_return
19258                        || info->ehrd_offset == -432)
19259                   && info->vrsave_save_offset == -224
19260                   && info->altivec_save_offset == -416);
19261
19262       treg = gen_rtx_REG (SImode, 11);
19263       emit_move_insn (treg, GEN_INT (-info->total_size));
19264
19265       /* SAVE_WORLD takes the caller's LR in R0 and the frame size
19266          in R11.  It also clobbers R12, so beware!  */
19267
19268       /* Preserve CR2 for save_world prologues */
19269       sz = 5;
19270       sz += 32 - info->first_gp_reg_save;
19271       sz += 64 - info->first_fp_reg_save;
19272       sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
19273       p = rtvec_alloc (sz);
19274       j = 0;
19275       RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
19276                                             gen_rtx_REG (SImode,
19277                                                          LR_REGNO));
19278       RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
19279                                         gen_rtx_SYMBOL_REF (Pmode,
19280                                                             "*save_world"));
19281       /* We do floats first so that the instruction pattern matches
19282          properly.  */
19283       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19284         {
19285           rtx reg = gen_rtx_REG (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
19286                                    ? DFmode : SFmode), 
19287                                  info->first_fp_reg_save + i);
19288           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19289                                    GEN_INT (info->fp_save_offset
19290                                             + sp_offset + 8 * i));
19291           rtx mem = gen_frame_mem (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
19292                                      ? DFmode : SFmode), addr);
19293
19294           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19295         }
19296       for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
19297         {
19298           rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
19299           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19300                                    GEN_INT (info->altivec_save_offset
19301                                             + sp_offset + 16 * i));
19302           rtx mem = gen_frame_mem (V4SImode, addr);
19303
19304           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19305         }
19306       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19307         {
19308           rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19309           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19310                                    GEN_INT (info->gp_save_offset
19311                                             + sp_offset + reg_size * i));
19312           rtx mem = gen_frame_mem (reg_mode, addr);
19313
19314           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19315         }
19316
19317       {
19318         /* CR register traditionally saved as CR2.  */
19319         rtx reg = gen_rtx_REG (reg_mode, CR2_REGNO);
19320         rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19321                                  GEN_INT (info->cr_save_offset
19322                                           + sp_offset));
19323         rtx mem = gen_frame_mem (reg_mode, addr);
19324
19325         RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19326       }
19327       /* Explain about use of R0.  */
19328       if (info->lr_save_p)
19329         {
19330           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19331                                    GEN_INT (info->lr_save_offset
19332                                             + sp_offset));
19333           rtx mem = gen_frame_mem (reg_mode, addr);
19334
19335           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg0);
19336         }
19337       /* Explain what happens to the stack pointer.  */
19338       {
19339         rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
19340         RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
19341       }
19342
19343       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19344       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19345                             treg, GEN_INT (-info->total_size));
19346       sp_offset = info->total_size;
19347     }
19348
19349   /* If we use the link register, get it into r0.  */
19350   if (!WORLD_SAVE_P (info) && info->lr_save_p)
19351     {
19352       rtx addr, reg, mem;
19353
19354       insn = emit_move_insn (gen_rtx_REG (Pmode, 0),
19355                              gen_rtx_REG (Pmode, LR_REGNO));
19356       RTX_FRAME_RELATED_P (insn) = 1;
19357
19358       if (!(strategy & (SAVRES_NOINLINE_GPRS_SAVES_LR
19359                         | SAVRES_NOINLINE_FPRS_SAVES_LR)))
19360         {
19361           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19362                                GEN_INT (info->lr_save_offset + sp_offset));
19363           reg = gen_rtx_REG (Pmode, 0);
19364           mem = gen_rtx_MEM (Pmode, addr);
19365           /* This should not be of rs6000_sr_alias_set, because of
19366              __builtin_return_address.  */
19367
19368           insn = emit_move_insn (mem, reg);
19369           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19370                                 NULL_RTX, NULL_RTX);
19371         }
19372     }
19373
19374   /* If we need to save CR, put it into r12 or r11.  */
19375   if (!WORLD_SAVE_P (info) && info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
19376     {
19377       rtx set;
19378
19379       cr_save_rtx
19380         = gen_rtx_REG (SImode, DEFAULT_ABI == ABI_AIX && !saving_GPRs_inline
19381                        ? 11 : 12);
19382       insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19383       RTX_FRAME_RELATED_P (insn) = 1;
19384       /* Now, there's no way that dwarf2out_frame_debug_expr is going
19385          to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
19386          But that's OK.  All we have to do is specify that _one_ condition
19387          code register is saved in this stack slot.  The thrower's epilogue
19388          will then restore all the call-saved registers.
19389          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
19390       set = gen_rtx_SET (VOIDmode, cr_save_rtx,
19391                          gen_rtx_REG (SImode, CR2_REGNO));
19392       add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19393     }
19394
19395   /* Do any required saving of fpr's.  If only one or two to save, do
19396      it ourselves.  Otherwise, call function.  */
19397   if (!WORLD_SAVE_P (info) && saving_FPRs_inline)
19398     {
19399       int i;
19400       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19401         if ((df_regs_ever_live_p (info->first_fp_reg_save+i)
19402              && ! call_used_regs[info->first_fp_reg_save+i]))
19403           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, 
19404                            (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
19405                             ? DFmode : SFmode,
19406                            info->first_fp_reg_save + i,
19407                            info->fp_save_offset + sp_offset + 8 * i,
19408                            info->total_size);
19409     }
19410   else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
19411     {
19412       rtx par;
19413
19414       par = rs6000_make_savres_rtx (info, frame_reg_rtx,
19415                                     info->fp_save_offset + sp_offset,
19416                                     DFmode,
19417                                     /*savep=*/true, /*gpr=*/false,
19418                                     /*lr=*/(strategy
19419                                             & SAVRES_NOINLINE_FPRS_SAVES_LR)
19420                                            != 0);
19421       insn = emit_insn (par);
19422       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19423                             NULL_RTX, NULL_RTX);
19424     }
19425
19426   /* Save GPRs.  This is done as a PARALLEL if we are using
19427      the store-multiple instructions.  */
19428   if (!WORLD_SAVE_P (info)
19429       && TARGET_SPE_ABI
19430       && info->spe_64bit_regs_used != 0
19431       && info->first_gp_reg_save != 32)
19432     {
19433       int i;
19434       rtx spe_save_area_ptr;
19435  
19436       /* Determine whether we can address all of the registers that need
19437          to be saved with an offset from the stack pointer that fits in
19438          the small const field for SPE memory instructions.  */
19439       int spe_regs_addressable_via_sp
19440         = (SPE_CONST_OFFSET_OK(info->spe_gp_save_offset + sp_offset
19441                                + (32 - info->first_gp_reg_save - 1) * reg_size)
19442            && saving_GPRs_inline);
19443       int spe_offset;
19444  
19445       if (spe_regs_addressable_via_sp)
19446         {
19447           spe_save_area_ptr = frame_reg_rtx;
19448           spe_offset = info->spe_gp_save_offset + sp_offset;
19449         }
19450       else
19451         {
19452           /* Make r11 point to the start of the SPE save area.  We need
19453              to be careful here if r11 is holding the static chain.  If
19454              it is, then temporarily save it in r0.  We would use r0 as
19455              our base register here, but using r0 as a base register in
19456              loads and stores means something different from what we
19457              would like.  */
19458           int ool_adjust = (saving_GPRs_inline
19459                             ? 0
19460                             : (info->first_gp_reg_save
19461                                - (FIRST_SAVRES_REGISTER+1))*8);
19462           HOST_WIDE_INT offset = (info->spe_gp_save_offset
19463                                   + sp_offset - ool_adjust);
19464
19465           if (using_static_chain_p)
19466             {
19467               rtx r0 = gen_rtx_REG (Pmode, 0);
19468               gcc_assert (info->first_gp_reg_save > 11);
19469  
19470               emit_move_insn (r0, gen_rtx_REG (Pmode, 11));
19471             }
19472  
19473           spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
19474           insn = emit_insn (gen_addsi3 (spe_save_area_ptr,
19475                                         frame_reg_rtx,
19476                                         GEN_INT (offset)));
19477           /* We need to make sure the move to r11 gets noted for
19478              properly outputting unwind information.  */
19479           if (!saving_GPRs_inline)
19480             rs6000_frame_related (insn, frame_reg_rtx, offset,
19481                                   NULL_RTX, NULL_RTX);
19482           spe_offset = 0;
19483         }
19484  
19485       if (saving_GPRs_inline)
19486         {
19487           for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19488             if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19489               {
19490                 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19491                 rtx offset, addr, mem;
19492
19493                 /* We're doing all this to ensure that the offset fits into
19494                    the immediate offset of 'evstdd'.  */
19495                 gcc_assert (SPE_CONST_OFFSET_OK (reg_size * i + spe_offset));
19496  
19497                 offset = GEN_INT (reg_size * i + spe_offset);
19498                 addr = gen_rtx_PLUS (Pmode, spe_save_area_ptr, offset);
19499                 mem = gen_rtx_MEM (V2SImode, addr);
19500   
19501                 insn = emit_move_insn (mem, reg);
19502            
19503                 rs6000_frame_related (insn, spe_save_area_ptr,
19504                                       info->spe_gp_save_offset
19505                                       + sp_offset + reg_size * i,
19506                                       offset, const0_rtx);
19507               }
19508         }
19509       else
19510         {
19511           rtx par;
19512
19513           par = rs6000_make_savres_rtx (info, gen_rtx_REG (Pmode, 11),
19514                                         0, reg_mode,
19515                                         /*savep=*/true, /*gpr=*/true,
19516                                         /*lr=*/false);
19517           insn = emit_insn (par);
19518           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19519                                 NULL_RTX, NULL_RTX);
19520         }
19521                                         
19522  
19523       /* Move the static chain pointer back.  */
19524       if (using_static_chain_p && !spe_regs_addressable_via_sp)
19525         emit_move_insn (gen_rtx_REG (Pmode, 11), gen_rtx_REG (Pmode, 0));
19526     }
19527   else if (!WORLD_SAVE_P (info) && !saving_GPRs_inline)
19528     {
19529       rtx par;
19530
19531       /* Need to adjust r11 (r12) if we saved any FPRs.  */
19532       if (info->first_fp_reg_save != 64)
19533         {
19534           rtx dest_reg = gen_rtx_REG (reg_mode, DEFAULT_ABI == ABI_AIX
19535                                       ? 12 : 11);
19536           rtx offset = GEN_INT (sp_offset
19537                                 + (-8 * (64-info->first_fp_reg_save)));
19538           emit_insn (gen_add3_insn (dest_reg, frame_reg_rtx, offset));
19539         }
19540
19541       par = rs6000_make_savres_rtx (info, frame_reg_rtx,
19542                                     info->gp_save_offset + sp_offset,
19543                                     reg_mode,
19544                                     /*savep=*/true, /*gpr=*/true,
19545                                     /*lr=*/(strategy
19546                                             & SAVRES_NOINLINE_GPRS_SAVES_LR)
19547                                            != 0);
19548       insn = emit_insn (par);
19549       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19550                             NULL_RTX, NULL_RTX);
19551     }
19552   else if (!WORLD_SAVE_P (info) && using_store_multiple)
19553     {
19554       rtvec p;
19555       int i;
19556       p = rtvec_alloc (32 - info->first_gp_reg_save);
19557       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19558         {
19559           rtx addr, reg, mem;
19560           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19561           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19562                                GEN_INT (info->gp_save_offset
19563                                         + sp_offset
19564                                         + reg_size * i));
19565           mem = gen_frame_mem (reg_mode, addr);
19566
19567           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
19568         }
19569       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19570       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19571                             NULL_RTX, NULL_RTX);
19572     }
19573   else if (!WORLD_SAVE_P (info))
19574     {
19575       int i;
19576       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19577         if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19578           {
19579             rtx addr, reg, mem;
19580             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19581
19582             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19583                                  GEN_INT (info->gp_save_offset
19584                                           + sp_offset
19585                                           + reg_size * i));
19586             mem = gen_frame_mem (reg_mode, addr);
19587
19588             insn = emit_move_insn (mem, reg);
19589             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19590                                   NULL_RTX, NULL_RTX);
19591           }
19592     }
19593
19594   /* ??? There's no need to emit actual instructions here, but it's the
19595      easiest way to get the frame unwind information emitted.  */
19596   if (crtl->calls_eh_return)
19597     {
19598       unsigned int i, regno;
19599
19600       /* In AIX ABI we need to pretend we save r2 here.  */
19601       if (TARGET_AIX)
19602         {
19603           rtx addr, reg, mem;
19604
19605           reg = gen_rtx_REG (reg_mode, 2);
19606           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19607                                GEN_INT (sp_offset + 5 * reg_size));
19608           mem = gen_frame_mem (reg_mode, addr);
19609
19610           insn = emit_move_insn (mem, reg);
19611           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19612                                 NULL_RTX, NULL_RTX);
19613           PATTERN (insn) = gen_blockage ();
19614         }
19615
19616       for (i = 0; ; ++i)
19617         {
19618           regno = EH_RETURN_DATA_REGNO (i);
19619           if (regno == INVALID_REGNUM)
19620             break;
19621
19622           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
19623                            info->ehrd_offset + sp_offset
19624                            + reg_size * (int) i,
19625                            info->total_size);
19626         }
19627     }
19628
19629   /* Save CR if we use any that must be preserved.  */
19630   if (!WORLD_SAVE_P (info) && info->cr_save_p)
19631     {
19632       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19633                                GEN_INT (info->cr_save_offset + sp_offset));
19634       rtx mem = gen_frame_mem (SImode, addr);
19635       /* See the large comment above about why CR2_REGNO is used.  */
19636       rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
19637
19638       /* If r12 was used to hold the original sp, copy cr into r0 now
19639          that it's free.  */
19640       if (REGNO (frame_reg_rtx) == 12)
19641         {
19642           rtx set;
19643
19644           cr_save_rtx = gen_rtx_REG (SImode, 0);
19645           insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19646           RTX_FRAME_RELATED_P (insn) = 1;
19647           set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
19648           add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19649         }
19650       insn = emit_move_insn (mem, cr_save_rtx);
19651
19652       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19653                             NULL_RTX, NULL_RTX);
19654     }
19655
19656   /* Update stack and set back pointer unless this is V.4,
19657      for which it was done previously.  */
19658   if (!WORLD_SAVE_P (info) && info->push_p
19659       && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
19660     {
19661       rtx copy_reg = NULL;
19662
19663       if (info->total_size < 32767)
19664         sp_offset = info->total_size;
19665       else if (info->altivec_size != 0
19666                || info->vrsave_mask != 0)
19667         {
19668           copy_reg = frame_ptr_rtx;
19669           frame_reg_rtx = copy_reg;
19670         }
19671       else
19672         sp_offset = info->total_size;
19673       rs6000_emit_allocate_stack (info->total_size, copy_reg);
19674       if (frame_reg_rtx != sp_reg_rtx)
19675         rs6000_emit_stack_tie ();
19676     }
19677
19678   /* Set frame pointer, if needed.  */
19679   if (frame_pointer_needed)
19680     {
19681       insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
19682                              sp_reg_rtx);
19683       RTX_FRAME_RELATED_P (insn) = 1;
19684     }
19685
19686   /* Save AltiVec registers if needed.  Save here because the red zone does
19687      not include AltiVec registers.  */
19688   if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI && info->altivec_size != 0)
19689     {
19690       int i;
19691
19692       /* There should be a non inline version of this, for when we
19693          are saving lots of vector registers.  */
19694       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
19695         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
19696           {
19697             rtx areg, savereg, mem;
19698             int offset;
19699
19700             offset = info->altivec_save_offset + sp_offset
19701               + 16 * (i - info->first_altivec_reg_save);
19702
19703             savereg = gen_rtx_REG (V4SImode, i);
19704
19705             areg = gen_rtx_REG (Pmode, 0);
19706             emit_move_insn (areg, GEN_INT (offset));
19707
19708             /* AltiVec addressing mode is [reg+reg].  */
19709             mem = gen_frame_mem (V4SImode,
19710                                  gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
19711
19712             insn = emit_move_insn (mem, savereg);
19713
19714             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19715                                   areg, GEN_INT (offset));
19716           }
19717     }
19718
19719   /* VRSAVE is a bit vector representing which AltiVec registers
19720      are used.  The OS uses this to determine which vector
19721      registers to save on a context switch.  We need to save
19722      VRSAVE on the stack frame, add whatever AltiVec registers we
19723      used in this function, and do the corresponding magic in the
19724      epilogue.  */
19725
19726   if (TARGET_ALTIVEC && TARGET_ALTIVEC_VRSAVE
19727       && info->vrsave_mask != 0)
19728     {
19729       rtx reg, mem, vrsave;
19730       int offset;
19731
19732       /* Get VRSAVE onto a GPR.  Note that ABI_V4 might be using r12
19733          as frame_reg_rtx and r11 as the static chain pointer for
19734          nested functions.  */
19735       reg = gen_rtx_REG (SImode, 0);
19736       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
19737       if (TARGET_MACHO)
19738         emit_insn (gen_get_vrsave_internal (reg));
19739       else
19740         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
19741
19742       if (!WORLD_SAVE_P (info))
19743         {
19744           /* Save VRSAVE.  */
19745           offset = info->vrsave_save_offset + sp_offset;
19746           mem = gen_frame_mem (SImode,
19747                                gen_rtx_PLUS (Pmode, frame_reg_rtx,
19748                                              GEN_INT (offset)));
19749           insn = emit_move_insn (mem, reg);
19750         }
19751
19752       /* Include the registers in the mask.  */
19753       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
19754
19755       insn = emit_insn (generate_set_vrsave (reg, info, 0));
19756     }
19757
19758   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
19759   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
19760       || (DEFAULT_ABI == ABI_V4
19761           && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
19762           && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM)))
19763     {
19764       /* If emit_load_toc_table will use the link register, we need to save
19765          it.  We use R12 for this purpose because emit_load_toc_table
19766          can use register 0.  This allows us to use a plain 'blr' to return
19767          from the procedure more often.  */
19768       int save_LR_around_toc_setup = (TARGET_ELF
19769                                       && DEFAULT_ABI != ABI_AIX
19770                                       && flag_pic
19771                                       && ! info->lr_save_p
19772                                       && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
19773       if (save_LR_around_toc_setup)
19774         {
19775           rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
19776
19777           insn = emit_move_insn (frame_ptr_rtx, lr);
19778           RTX_FRAME_RELATED_P (insn) = 1;
19779
19780           rs6000_emit_load_toc_table (TRUE);
19781
19782           insn = emit_move_insn (lr, frame_ptr_rtx);
19783           RTX_FRAME_RELATED_P (insn) = 1;
19784         }
19785       else
19786         rs6000_emit_load_toc_table (TRUE);
19787     }
19788
19789 #if TARGET_MACHO
19790   if (DEFAULT_ABI == ABI_DARWIN
19791       && flag_pic && crtl->uses_pic_offset_table)
19792     {
19793       rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
19794       rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
19795
19796       /* Save and restore LR locally around this call (in R0).  */
19797       if (!info->lr_save_p)
19798         emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
19799
19800       emit_insn (gen_load_macho_picbase (src));
19801
19802       emit_move_insn (gen_rtx_REG (Pmode,
19803                                    RS6000_PIC_OFFSET_TABLE_REGNUM),
19804                       lr);
19805
19806       if (!info->lr_save_p)
19807         emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
19808     }
19809 #endif
19810 }
19811
19812 /* Write function prologue.  */
19813
19814 static void
19815 rs6000_output_function_prologue (FILE *file,
19816                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
19817 {
19818   rs6000_stack_t *info = rs6000_stack_info ();
19819
19820   if (TARGET_DEBUG_STACK)
19821     debug_stack_info (info);
19822
19823   /* Write .extern for any function we will call to save and restore
19824      fp values.  */
19825   if (info->first_fp_reg_save < 64
19826       && !FP_SAVE_INLINE (info->first_fp_reg_save))
19827     {
19828       char *name;
19829       int regno = info->first_fp_reg_save - 32;
19830
19831       name = rs6000_savres_routine_name (info, regno, /*savep=*/true,
19832                                          /*gpr=*/false, /*lr=*/false);
19833       fprintf (file, "\t.extern %s\n", name);
19834
19835       name = rs6000_savres_routine_name (info, regno, /*savep=*/false,
19836                                          /*gpr=*/false, /*lr=*/true);
19837       fprintf (file, "\t.extern %s\n", name);
19838     }
19839
19840   /* Write .extern for AIX common mode routines, if needed.  */
19841   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
19842     {
19843       fputs ("\t.extern __mulh\n", file);
19844       fputs ("\t.extern __mull\n", file);
19845       fputs ("\t.extern __divss\n", file);
19846       fputs ("\t.extern __divus\n", file);
19847       fputs ("\t.extern __quoss\n", file);
19848       fputs ("\t.extern __quous\n", file);
19849       common_mode_defined = 1;
19850     }
19851
19852   if (! HAVE_prologue)
19853     {
19854       rtx prologue;
19855
19856       start_sequence ();
19857
19858       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
19859          the "toplevel" insn chain.  */
19860       emit_note (NOTE_INSN_DELETED);
19861       rs6000_emit_prologue ();
19862       emit_note (NOTE_INSN_DELETED);
19863
19864       /* Expand INSN_ADDRESSES so final() doesn't crash.  */
19865       {
19866         rtx insn;
19867         unsigned addr = 0;
19868         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
19869           {
19870             INSN_ADDRESSES_NEW (insn, addr);
19871             addr += 4;
19872           }
19873       }
19874
19875       prologue = get_insns ();
19876       end_sequence ();
19877
19878       if (TARGET_DEBUG_STACK)
19879         debug_rtx_list (prologue, 100);
19880
19881       emit_insn_before_noloc (prologue, BB_HEAD (ENTRY_BLOCK_PTR->next_bb),
19882                               ENTRY_BLOCK_PTR);
19883     }
19884
19885   rs6000_pic_labelno++;
19886 }
19887
19888 /* Non-zero if vmx regs are restored before the frame pop, zero if
19889    we restore after the pop when possible.  */
19890 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
19891
19892 /* Reload CR from REG.  */
19893
19894 static void
19895 rs6000_restore_saved_cr (rtx reg, int using_mfcr_multiple)
19896 {
19897   int count = 0;
19898   int i;
19899
19900   if (using_mfcr_multiple)
19901     {
19902       for (i = 0; i < 8; i++)
19903         if (df_regs_ever_live_p (CR0_REGNO+i) && ! call_used_regs[CR0_REGNO+i])
19904           count++;
19905       gcc_assert (count);
19906     }
19907
19908   if (using_mfcr_multiple && count > 1)
19909     {
19910       rtvec p;
19911       int ndx;
19912
19913       p = rtvec_alloc (count);
19914
19915       ndx = 0;
19916       for (i = 0; i < 8; i++)
19917         if (df_regs_ever_live_p (CR0_REGNO+i) && ! call_used_regs[CR0_REGNO+i])
19918           {
19919             rtvec r = rtvec_alloc (2);
19920             RTVEC_ELT (r, 0) = reg;
19921             RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
19922             RTVEC_ELT (p, ndx) =
19923               gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i),
19924                            gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
19925             ndx++;
19926           }
19927       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19928       gcc_assert (ndx == count);
19929     }
19930   else
19931     for (i = 0; i < 8; i++)
19932       if (df_regs_ever_live_p (CR0_REGNO+i) && ! call_used_regs[CR0_REGNO+i])
19933         {
19934           emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode,
19935                                                        CR0_REGNO+i),
19936                                           reg));
19937         }
19938 }
19939
19940 /* Return true if OFFSET from stack pointer can be clobbered by signals.
19941    V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
19942    below stack pointer not cloberred by signals.  */
19943
19944 static inline bool
19945 offset_below_red_zone_p (HOST_WIDE_INT offset)
19946 {
19947   return offset < (DEFAULT_ABI == ABI_V4
19948                    ? 0
19949                    : TARGET_32BIT ? -220 : -288);
19950 }
19951
19952 /* Emit function epilogue as insns.  */
19953
19954 void
19955 rs6000_emit_epilogue (int sibcall)
19956 {
19957   rs6000_stack_t *info;
19958   int restoring_GPRs_inline;
19959   int restoring_FPRs_inline;
19960   int using_load_multiple;
19961   int using_mtcr_multiple;
19962   int use_backchain_to_restore_sp;
19963   int restore_lr;
19964   int strategy;
19965   int sp_offset = 0;
19966   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
19967   rtx frame_reg_rtx = sp_reg_rtx;
19968   rtx cfa_restores = NULL_RTX;
19969   rtx insn;
19970   rtx cr_save_reg = NULL_RTX;
19971   enum machine_mode reg_mode = Pmode;
19972   int reg_size = TARGET_32BIT ? 4 : 8;
19973   int i;
19974
19975   info = rs6000_stack_info ();
19976
19977   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
19978     {
19979       reg_mode = V2SImode;
19980       reg_size = 8;
19981     }
19982
19983   strategy = rs6000_savres_strategy (info, /*savep=*/false,
19984                                      /*static_chain_p=*/0, sibcall);
19985   using_load_multiple = strategy & SAVRES_MULTIPLE;
19986   restoring_FPRs_inline = strategy & SAVRES_INLINE_FPRS;
19987   restoring_GPRs_inline = strategy & SAVRES_INLINE_GPRS;
19988   using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
19989                          || rs6000_cpu == PROCESSOR_PPC603
19990                          || rs6000_cpu == PROCESSOR_PPC750
19991                          || optimize_size);
19992   /* Restore via the backchain when we have a large frame, since this
19993      is more efficient than an addis, addi pair.  The second condition
19994      here will not trigger at the moment;  We don't actually need a
19995      frame pointer for alloca, but the generic parts of the compiler
19996      give us one anyway.  */
19997   use_backchain_to_restore_sp = (info->total_size > 32767
19998                                  || info->total_size
19999                                      + (info->lr_save_p ? info->lr_save_offset : 0)
20000                                        > 32767
20001                                  || (cfun->calls_alloca
20002                                      && !frame_pointer_needed));
20003   restore_lr = (info->lr_save_p
20004                 && (restoring_FPRs_inline
20005                     || (strategy & SAVRES_NOINLINE_FPRS_DOESNT_RESTORE_LR))
20006                 && (restoring_GPRs_inline
20007                     || info->first_fp_reg_save < 64));
20008
20009   if (WORLD_SAVE_P (info))
20010     {
20011       int i, j;
20012       char rname[30];
20013       const char *alloc_rname;
20014       rtvec p;
20015
20016       /* eh_rest_world_r10 will return to the location saved in the LR
20017          stack slot (which is not likely to be our caller.)
20018          Input: R10 -- stack adjustment.  Clobbers R0, R11, R12, R7, R8.
20019          rest_world is similar, except any R10 parameter is ignored.
20020          The exception-handling stuff that was here in 2.95 is no
20021          longer necessary.  */
20022
20023       p = rtvec_alloc (9
20024                        + 1
20025                        + 32 - info->first_gp_reg_save
20026                        + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
20027                        + 63 + 1 - info->first_fp_reg_save);
20028
20029       strcpy (rname, ((crtl->calls_eh_return) ?
20030                       "*eh_rest_world_r10" : "*rest_world"));
20031       alloc_rname = ggc_strdup (rname);
20032
20033       j = 0;
20034       RTVEC_ELT (p, j++) = gen_rtx_RETURN (VOIDmode);
20035       RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
20036                                         gen_rtx_REG (Pmode,
20037                                                      LR_REGNO));
20038       RTVEC_ELT (p, j++)
20039         = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
20040       /* The instruction pattern requires a clobber here;
20041          it is shared with the restVEC helper. */
20042       RTVEC_ELT (p, j++)
20043         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
20044
20045       {
20046         /* CR register traditionally saved as CR2.  */
20047         rtx reg = gen_rtx_REG (reg_mode, CR2_REGNO);
20048         rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20049                                  GEN_INT (info->cr_save_offset));
20050         rtx mem = gen_frame_mem (reg_mode, addr);
20051
20052         RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20053       }
20054
20055       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20056         {
20057           rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20058           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20059                                    GEN_INT (info->gp_save_offset
20060                                             + reg_size * i));
20061           rtx mem = gen_frame_mem (reg_mode, addr);
20062
20063           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20064         }
20065       for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
20066         {
20067           rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
20068           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20069                                    GEN_INT (info->altivec_save_offset
20070                                             + 16 * i));
20071           rtx mem = gen_frame_mem (V4SImode, addr);
20072
20073           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20074         }
20075       for (i = 0; info->first_fp_reg_save + i <= 63; i++)
20076         {
20077           rtx reg = gen_rtx_REG (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20078                                    ? DFmode : SFmode), 
20079                                  info->first_fp_reg_save + i);
20080           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20081                                    GEN_INT (info->fp_save_offset
20082                                             + 8 * i));
20083           rtx mem = gen_frame_mem (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20084                                      ? DFmode : SFmode), addr);
20085
20086           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20087         }
20088       RTVEC_ELT (p, j++)
20089         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
20090       RTVEC_ELT (p, j++)
20091         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
20092       RTVEC_ELT (p, j++)
20093         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
20094       RTVEC_ELT (p, j++)
20095         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
20096       RTVEC_ELT (p, j++)
20097         = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
20098       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20099
20100       return;
20101     }
20102
20103   /* frame_reg_rtx + sp_offset points to the top of this stack frame.  */
20104   if (info->push_p)
20105     sp_offset = info->total_size;
20106
20107   /* Restore AltiVec registers if we must do so before adjusting the
20108      stack.  */
20109   if (TARGET_ALTIVEC_ABI
20110       && info->altivec_size != 0
20111       && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20112           || (DEFAULT_ABI != ABI_V4
20113               && offset_below_red_zone_p (info->altivec_save_offset))))
20114     {
20115       int i;
20116
20117       if (use_backchain_to_restore_sp)
20118         {
20119           frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20120           emit_move_insn (frame_reg_rtx,
20121                           gen_rtx_MEM (Pmode, sp_reg_rtx));
20122           sp_offset = 0;
20123         }
20124       else if (frame_pointer_needed)
20125         frame_reg_rtx = hard_frame_pointer_rtx;
20126
20127       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20128         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20129           {
20130             rtx addr, areg, mem, reg;
20131
20132             areg = gen_rtx_REG (Pmode, 0);
20133             emit_move_insn
20134               (areg, GEN_INT (info->altivec_save_offset
20135                               + sp_offset
20136                               + 16 * (i - info->first_altivec_reg_save)));
20137
20138             /* AltiVec addressing mode is [reg+reg].  */
20139             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20140             mem = gen_frame_mem (V4SImode, addr);
20141
20142             reg = gen_rtx_REG (V4SImode, i);
20143             emit_move_insn (reg, mem);
20144             if (offset_below_red_zone_p (info->altivec_save_offset
20145                                          + (i - info->first_altivec_reg_save)
20146                                            * 16))
20147               cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20148                                              cfa_restores);
20149           }
20150     }
20151
20152   /* Restore VRSAVE if we must do so before adjusting the stack.  */
20153   if (TARGET_ALTIVEC
20154       && TARGET_ALTIVEC_VRSAVE
20155       && info->vrsave_mask != 0
20156       && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20157           || (DEFAULT_ABI != ABI_V4
20158               && offset_below_red_zone_p (info->vrsave_save_offset))))
20159     {
20160       rtx addr, mem, reg;
20161
20162       if (frame_reg_rtx == sp_reg_rtx)
20163         {
20164           if (use_backchain_to_restore_sp)
20165             {
20166               frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20167               emit_move_insn (frame_reg_rtx,
20168                               gen_rtx_MEM (Pmode, sp_reg_rtx));
20169               sp_offset = 0;
20170             }
20171           else if (frame_pointer_needed)
20172             frame_reg_rtx = hard_frame_pointer_rtx;
20173         }
20174
20175       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20176                            GEN_INT (info->vrsave_save_offset + sp_offset));
20177       mem = gen_frame_mem (SImode, addr);
20178       reg = gen_rtx_REG (SImode, 12);
20179       emit_move_insn (reg, mem);
20180
20181       emit_insn (generate_set_vrsave (reg, info, 1));
20182     }
20183
20184   insn = NULL_RTX;
20185   /* If we have a large stack frame, restore the old stack pointer
20186      using the backchain.  */
20187   if (use_backchain_to_restore_sp)
20188     {
20189       if (frame_reg_rtx == sp_reg_rtx)
20190         {
20191           /* Under V.4, don't reset the stack pointer until after we're done
20192              loading the saved registers.  */
20193           if (DEFAULT_ABI == ABI_V4)
20194             frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20195
20196           insn = emit_move_insn (frame_reg_rtx,
20197                                  gen_rtx_MEM (Pmode, sp_reg_rtx));
20198           sp_offset = 0;
20199         }
20200       else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20201                && DEFAULT_ABI == ABI_V4)
20202         /* frame_reg_rtx has been set up by the altivec restore.  */
20203         ;
20204       else
20205         {
20206           insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
20207           frame_reg_rtx = sp_reg_rtx;
20208         }
20209     }
20210   /* If we have a frame pointer, we can restore the old stack pointer
20211      from it.  */
20212   else if (frame_pointer_needed)
20213     {
20214       frame_reg_rtx = sp_reg_rtx;
20215       if (DEFAULT_ABI == ABI_V4)
20216         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20217       /* Prevent reordering memory accesses against stack pointer restore.  */
20218       else if (cfun->calls_alloca
20219                || offset_below_red_zone_p (-info->total_size))
20220         {
20221           rtx mem1 = gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx);
20222           rtx mem2 = gen_rtx_MEM (BLKmode, sp_reg_rtx);
20223           MEM_NOTRAP_P (mem1) = 1;
20224           MEM_NOTRAP_P (mem2) = 1;
20225           emit_insn (gen_frame_tie (mem1, mem2));
20226         }
20227
20228       insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
20229                                        GEN_INT (info->total_size)));
20230       sp_offset = 0;
20231     }
20232   else if (info->push_p
20233            && DEFAULT_ABI != ABI_V4
20234            && !crtl->calls_eh_return)
20235     {
20236       /* Prevent reordering memory accesses against stack pointer restore.  */
20237       if (cfun->calls_alloca
20238           || offset_below_red_zone_p (-info->total_size))
20239         {
20240           rtx mem = gen_rtx_MEM (BLKmode, sp_reg_rtx);
20241           MEM_NOTRAP_P (mem) = 1;
20242           emit_insn (gen_stack_tie (mem));
20243         }
20244       insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
20245                                        GEN_INT (info->total_size)));
20246       sp_offset = 0;
20247     }
20248   if (insn && frame_reg_rtx == sp_reg_rtx)
20249     {
20250       if (cfa_restores)
20251         {
20252           REG_NOTES (insn) = cfa_restores;
20253           cfa_restores = NULL_RTX;
20254         }
20255       add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20256       RTX_FRAME_RELATED_P (insn) = 1;
20257     }
20258
20259   /* Restore AltiVec registers if we have not done so already.  */
20260   if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20261       && TARGET_ALTIVEC_ABI
20262       && info->altivec_size != 0
20263       && (DEFAULT_ABI == ABI_V4
20264           || !offset_below_red_zone_p (info->altivec_save_offset)))
20265     {
20266       int i;
20267
20268       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20269         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20270           {
20271             rtx addr, areg, mem, reg;
20272
20273             areg = gen_rtx_REG (Pmode, 0);
20274             emit_move_insn
20275               (areg, GEN_INT (info->altivec_save_offset
20276                               + sp_offset
20277                               + 16 * (i - info->first_altivec_reg_save)));
20278
20279             /* AltiVec addressing mode is [reg+reg].  */
20280             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20281             mem = gen_frame_mem (V4SImode, addr);
20282
20283             reg = gen_rtx_REG (V4SImode, i);
20284             emit_move_insn (reg, mem);
20285             if (DEFAULT_ABI == ABI_V4)
20286               cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20287                                              cfa_restores);
20288           }
20289     }
20290
20291   /* Restore VRSAVE if we have not done so already.  */
20292   if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20293       && TARGET_ALTIVEC
20294       && TARGET_ALTIVEC_VRSAVE
20295       && info->vrsave_mask != 0
20296       && (DEFAULT_ABI == ABI_V4
20297           || !offset_below_red_zone_p (info->vrsave_save_offset)))
20298     {
20299       rtx addr, mem, reg;
20300
20301       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20302                            GEN_INT (info->vrsave_save_offset + sp_offset));
20303       mem = gen_frame_mem (SImode, addr);
20304       reg = gen_rtx_REG (SImode, 12);
20305       emit_move_insn (reg, mem);
20306
20307       emit_insn (generate_set_vrsave (reg, info, 1));
20308     }
20309
20310   /* Get the old lr if we saved it.  If we are restoring registers
20311      out-of-line, then the out-of-line routines can do this for us.  */
20312   if (restore_lr && restoring_GPRs_inline)
20313     {
20314       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
20315                                       info->lr_save_offset + sp_offset);
20316
20317       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
20318     }
20319
20320   /* Get the old cr if we saved it.  */
20321   if (info->cr_save_p)
20322     {
20323       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20324                                GEN_INT (info->cr_save_offset + sp_offset));
20325       rtx mem = gen_frame_mem (SImode, addr);
20326
20327       cr_save_reg = gen_rtx_REG (SImode,
20328                                  DEFAULT_ABI == ABI_AIX
20329                                  && !restoring_GPRs_inline
20330                                  && info->first_fp_reg_save < 64
20331                                  ? 11 : 12);
20332       emit_move_insn (cr_save_reg, mem);
20333     }
20334
20335   /* Set LR here to try to overlap restores below.  LR is always saved
20336      above incoming stack, so it never needs REG_CFA_RESTORE.  */
20337   if (restore_lr && restoring_GPRs_inline)
20338     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO),
20339                     gen_rtx_REG (Pmode, 0));
20340
20341   /* Load exception handler data registers, if needed.  */
20342   if (crtl->calls_eh_return)
20343     {
20344       unsigned int i, regno;
20345
20346       if (TARGET_AIX)
20347         {
20348           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20349                                    GEN_INT (sp_offset + 5 * reg_size));
20350           rtx mem = gen_frame_mem (reg_mode, addr);
20351
20352           emit_move_insn (gen_rtx_REG (reg_mode, 2), mem);
20353         }
20354
20355       for (i = 0; ; ++i)
20356         {
20357           rtx mem;
20358
20359           regno = EH_RETURN_DATA_REGNO (i);
20360           if (regno == INVALID_REGNUM)
20361             break;
20362
20363           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
20364                                       info->ehrd_offset + sp_offset
20365                                       + reg_size * (int) i);
20366
20367           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
20368         }
20369     }
20370
20371   /* Restore GPRs.  This is done as a PARALLEL if we are using
20372      the load-multiple instructions.  */
20373   if (TARGET_SPE_ABI
20374       && info->spe_64bit_regs_used != 0
20375       && info->first_gp_reg_save != 32)
20376     {
20377       /* Determine whether we can address all of the registers that need
20378          to be saved with an offset from the stack pointer that fits in
20379          the small const field for SPE memory instructions.  */
20380       int spe_regs_addressable_via_sp
20381         = (SPE_CONST_OFFSET_OK(info->spe_gp_save_offset + sp_offset
20382                                + (32 - info->first_gp_reg_save - 1) * reg_size)
20383            && restoring_GPRs_inline);
20384       int spe_offset;
20385
20386       if (spe_regs_addressable_via_sp)
20387         spe_offset = info->spe_gp_save_offset + sp_offset;
20388       else
20389         {
20390           rtx old_frame_reg_rtx = frame_reg_rtx;
20391           /* Make r11 point to the start of the SPE save area.  We worried about
20392              not clobbering it when we were saving registers in the prologue.
20393              There's no need to worry here because the static chain is passed
20394              anew to every function.  */
20395           int ool_adjust = (restoring_GPRs_inline
20396                             ? 0
20397                             : (info->first_gp_reg_save
20398                                - (FIRST_SAVRES_REGISTER+1))*8);
20399
20400           if (frame_reg_rtx == sp_reg_rtx)
20401             frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20402           emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
20403                                  GEN_INT (info->spe_gp_save_offset
20404                                           + sp_offset
20405                                           - ool_adjust)));
20406           /* Keep the invariant that frame_reg_rtx + sp_offset points
20407              at the top of the stack frame.  */
20408           sp_offset = -info->spe_gp_save_offset;
20409
20410           spe_offset = 0;
20411         }
20412
20413       if (restoring_GPRs_inline)
20414         {
20415           for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20416             if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20417               {
20418                 rtx offset, addr, mem, reg;
20419
20420                 /* We're doing all this to ensure that the immediate offset
20421                    fits into the immediate field of 'evldd'.  */
20422                 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
20423
20424                 offset = GEN_INT (spe_offset + reg_size * i);
20425                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
20426                 mem = gen_rtx_MEM (V2SImode, addr);
20427                 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20428
20429                 insn = emit_move_insn (reg, mem);
20430                 if (DEFAULT_ABI == ABI_V4)
20431                   {
20432                     if (frame_pointer_needed
20433                         && info->first_gp_reg_save + i
20434                            == HARD_FRAME_POINTER_REGNUM)
20435                       {
20436                         add_reg_note (insn, REG_CFA_DEF_CFA,
20437                                       plus_constant (frame_reg_rtx,
20438                                                      sp_offset));
20439                         RTX_FRAME_RELATED_P (insn) = 1;
20440                       }
20441
20442                     cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20443                                                    cfa_restores);
20444                   }
20445               }
20446         }
20447       else
20448         {
20449           rtx par;
20450
20451           par = rs6000_make_savres_rtx (info, gen_rtx_REG (Pmode, 11),
20452                                         0, reg_mode,
20453                                         /*savep=*/false, /*gpr=*/true,
20454                                         /*lr=*/true);
20455           emit_jump_insn (par);
20456           /* We don't want anybody else emitting things after we jumped
20457              back.  */
20458           return;
20459         }
20460     }
20461   else if (!restoring_GPRs_inline)
20462     {
20463       /* We are jumping to an out-of-line function.  */
20464       bool can_use_exit = info->first_fp_reg_save == 64;
20465       rtx par;
20466
20467       /* Emit stack reset code if we need it.  */
20468       if (can_use_exit)
20469         rs6000_emit_stack_reset (info, sp_reg_rtx, frame_reg_rtx,
20470                                  sp_offset, can_use_exit);
20471       else
20472         {
20473           emit_insn (gen_add3_insn (gen_rtx_REG (Pmode, DEFAULT_ABI == ABI_AIX
20474                                                         ? 12 : 11),
20475                                     frame_reg_rtx,
20476                                     GEN_INT (sp_offset - info->fp_size)));
20477           if (REGNO (frame_reg_rtx) == 11)
20478             sp_offset += info->fp_size;
20479         }
20480
20481       par = rs6000_make_savres_rtx (info, frame_reg_rtx,
20482                                     info->gp_save_offset, reg_mode,
20483                                     /*savep=*/false, /*gpr=*/true,
20484                                     /*lr=*/can_use_exit);
20485
20486       if (can_use_exit)
20487         {
20488           if (info->cr_save_p)
20489             {
20490               rs6000_restore_saved_cr (cr_save_reg, using_mtcr_multiple);
20491               if (DEFAULT_ABI == ABI_V4)
20492                 cfa_restores
20493                   = alloc_reg_note (REG_CFA_RESTORE,
20494                                     gen_rtx_REG (SImode, CR2_REGNO),
20495                                     cfa_restores);
20496             }
20497
20498           emit_jump_insn (par);
20499
20500           /* We don't want anybody else emitting things after we jumped
20501              back.  */
20502           return;
20503         }
20504
20505       insn = emit_insn (par);
20506       if (DEFAULT_ABI == ABI_V4)
20507         {
20508           if (frame_pointer_needed)
20509             {
20510               add_reg_note (insn, REG_CFA_DEF_CFA,
20511                             plus_constant (frame_reg_rtx, sp_offset));
20512               RTX_FRAME_RELATED_P (insn) = 1;
20513             }
20514
20515           for (i = info->first_gp_reg_save; i < 32; i++)
20516             cfa_restores
20517               = alloc_reg_note (REG_CFA_RESTORE,
20518                                 gen_rtx_REG (reg_mode, i), cfa_restores);
20519         }
20520     }
20521   else if (using_load_multiple)
20522     {
20523       rtvec p;
20524       p = rtvec_alloc (32 - info->first_gp_reg_save);
20525       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20526         {
20527           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20528                                    GEN_INT (info->gp_save_offset
20529                                             + sp_offset
20530                                             + reg_size * i));
20531           rtx mem = gen_frame_mem (reg_mode, addr);
20532           rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20533
20534           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, reg, mem);
20535           if (DEFAULT_ABI == ABI_V4)
20536             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20537                                            cfa_restores);
20538         }
20539       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20540       if (DEFAULT_ABI == ABI_V4 && frame_pointer_needed)
20541         {
20542           add_reg_note (insn, REG_CFA_DEF_CFA,
20543                         plus_constant (frame_reg_rtx, sp_offset));
20544           RTX_FRAME_RELATED_P (insn) = 1;
20545         }
20546     }
20547   else
20548     {
20549       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20550         if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20551           {
20552             rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20553                                      GEN_INT (info->gp_save_offset
20554                                               + sp_offset
20555                                               + reg_size * i));
20556             rtx mem = gen_frame_mem (reg_mode, addr);
20557             rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20558
20559             insn = emit_move_insn (reg, mem);
20560             if (DEFAULT_ABI == ABI_V4)
20561               {
20562                 if (frame_pointer_needed
20563                     && info->first_gp_reg_save + i
20564                        == HARD_FRAME_POINTER_REGNUM)
20565                   {
20566                     add_reg_note (insn, REG_CFA_DEF_CFA,
20567                                   plus_constant (frame_reg_rtx, sp_offset));
20568                     RTX_FRAME_RELATED_P (insn) = 1;
20569                   }
20570
20571                 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20572                                                cfa_restores);
20573               }
20574           }
20575     }
20576
20577   if (restore_lr && !restoring_GPRs_inline)
20578     {
20579       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
20580                                      info->lr_save_offset + sp_offset);
20581
20582       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
20583       emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO),
20584                       gen_rtx_REG (Pmode, 0));
20585     }
20586
20587   /* Restore fpr's if we need to do it without calling a function.  */
20588   if (restoring_FPRs_inline)
20589     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
20590       if ((df_regs_ever_live_p (info->first_fp_reg_save+i)
20591            && ! call_used_regs[info->first_fp_reg_save+i]))
20592         {
20593           rtx addr, mem, reg;
20594           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20595                                GEN_INT (info->fp_save_offset
20596                                         + sp_offset
20597                                         + 8 * i));
20598           mem = gen_frame_mem (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20599                                  ? DFmode : SFmode), addr);
20600           reg = gen_rtx_REG (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20601                                ? DFmode : SFmode),
20602                              info->first_fp_reg_save + i);
20603
20604           emit_move_insn (reg, mem);
20605           if (DEFAULT_ABI == ABI_V4)
20606             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20607                                            cfa_restores);
20608         }
20609
20610   /* If we saved cr, restore it here.  Just those that were used.  */
20611   if (info->cr_save_p)
20612     {
20613       rs6000_restore_saved_cr (cr_save_reg, using_mtcr_multiple);
20614       if (DEFAULT_ABI == ABI_V4)
20615         cfa_restores
20616           = alloc_reg_note (REG_CFA_RESTORE, gen_rtx_REG (SImode, CR2_REGNO),
20617                             cfa_restores);
20618     }
20619
20620   /* If this is V.4, unwind the stack pointer after all of the loads
20621      have been done.  */
20622   insn = rs6000_emit_stack_reset (info, sp_reg_rtx, frame_reg_rtx,
20623                                   sp_offset, !restoring_FPRs_inline);
20624   if (insn)
20625     {
20626       if (cfa_restores)
20627         {
20628           REG_NOTES (insn) = cfa_restores;
20629           cfa_restores = NULL_RTX;
20630         }
20631       add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20632       RTX_FRAME_RELATED_P (insn) = 1;
20633     }
20634
20635   if (crtl->calls_eh_return)
20636     {
20637       rtx sa = EH_RETURN_STACKADJ_RTX;
20638       emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
20639     }
20640
20641   if (!sibcall)
20642     {
20643       rtvec p;
20644       bool lr = (strategy & SAVRES_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
20645       if (! restoring_FPRs_inline)
20646         p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
20647       else
20648         p = rtvec_alloc (2);
20649
20650       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
20651       RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
20652                           ? gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 65))
20653                           : gen_rtx_CLOBBER (VOIDmode,
20654                                              gen_rtx_REG (Pmode, 65)));
20655
20656       /* If we have to restore more than two FP registers, branch to the
20657          restore function.  It will return to our caller.  */
20658       if (! restoring_FPRs_inline)
20659         {
20660           int i;
20661           rtx sym;
20662
20663           sym = rs6000_savres_routine_sym (info,
20664                                            /*savep=*/false,
20665                                            /*gpr=*/false,
20666                                            /*lr=*/lr);
20667           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
20668           RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
20669                                           gen_rtx_REG (Pmode,
20670                                                        DEFAULT_ABI == ABI_AIX
20671                                                        ? 1 : 11));
20672           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
20673             {
20674               rtx addr, mem;
20675               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
20676                                    GEN_INT (info->fp_save_offset + 8*i));
20677               mem = gen_frame_mem (DFmode, addr);
20678
20679               RTVEC_ELT (p, i+4) =
20680                 gen_rtx_SET (VOIDmode,
20681                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
20682                              mem);
20683             }
20684         }
20685
20686       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20687     }
20688 }
20689
20690 /* Write function epilogue.  */
20691
20692 static void
20693 rs6000_output_function_epilogue (FILE *file,
20694                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20695 {
20696   if (! HAVE_epilogue)
20697     {
20698       rtx insn = get_last_insn ();
20699       /* If the last insn was a BARRIER, we don't have to write anything except
20700          the trace table.  */
20701       if (GET_CODE (insn) == NOTE)
20702         insn = prev_nonnote_insn (insn);
20703       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
20704         {
20705           /* This is slightly ugly, but at least we don't have two
20706              copies of the epilogue-emitting code.  */
20707           start_sequence ();
20708
20709           /* A NOTE_INSN_DELETED is supposed to be at the start
20710              and end of the "toplevel" insn chain.  */
20711           emit_note (NOTE_INSN_DELETED);
20712           rs6000_emit_epilogue (FALSE);
20713           emit_note (NOTE_INSN_DELETED);
20714
20715           /* Expand INSN_ADDRESSES so final() doesn't crash.  */
20716           {
20717             rtx insn;
20718             unsigned addr = 0;
20719             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
20720               {
20721                 INSN_ADDRESSES_NEW (insn, addr);
20722                 addr += 4;
20723               }
20724           }
20725
20726           if (TARGET_DEBUG_STACK)
20727             debug_rtx_list (get_insns (), 100);
20728           final (get_insns (), file, FALSE);
20729           end_sequence ();
20730         }
20731     }
20732
20733 #if TARGET_MACHO
20734   macho_branch_islands ();
20735   /* Mach-O doesn't support labels at the end of objects, so if
20736      it looks like we might want one, insert a NOP.  */
20737   {
20738     rtx insn = get_last_insn ();
20739     while (insn
20740            && NOTE_P (insn)
20741            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
20742       insn = PREV_INSN (insn);
20743     if (insn
20744         && (LABEL_P (insn)
20745             || (NOTE_P (insn)
20746                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
20747       fputs ("\tnop\n", file);
20748   }
20749 #endif
20750
20751   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
20752      on its format.
20753
20754      We don't output a traceback table if -finhibit-size-directive was
20755      used.  The documentation for -finhibit-size-directive reads
20756      ``don't output a @code{.size} assembler directive, or anything
20757      else that would cause trouble if the function is split in the
20758      middle, and the two halves are placed at locations far apart in
20759      memory.''  The traceback table has this property, since it
20760      includes the offset from the start of the function to the
20761      traceback table itself.
20762
20763      System V.4 Powerpc's (and the embedded ABI derived from it) use a
20764      different traceback table.  */
20765   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
20766       && rs6000_traceback != traceback_none && !cfun->is_thunk)
20767     {
20768       const char *fname = NULL;
20769       const char *language_string = lang_hooks.name;
20770       int fixed_parms = 0, float_parms = 0, parm_info = 0;
20771       int i;
20772       int optional_tbtab;
20773       rs6000_stack_t *info = rs6000_stack_info ();
20774
20775       if (rs6000_traceback == traceback_full)
20776         optional_tbtab = 1;
20777       else if (rs6000_traceback == traceback_part)
20778         optional_tbtab = 0;
20779       else
20780         optional_tbtab = !optimize_size && !TARGET_ELF;
20781
20782       if (optional_tbtab)
20783         {
20784           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
20785           while (*fname == '.') /* V.4 encodes . in the name */
20786             fname++;
20787
20788           /* Need label immediately before tbtab, so we can compute
20789              its offset from the function start.  */
20790           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
20791           ASM_OUTPUT_LABEL (file, fname);
20792         }
20793
20794       /* The .tbtab pseudo-op can only be used for the first eight
20795          expressions, since it can't handle the possibly variable
20796          length fields that follow.  However, if you omit the optional
20797          fields, the assembler outputs zeros for all optional fields
20798          anyways, giving each variable length field is minimum length
20799          (as defined in sys/debug.h).  Thus we can not use the .tbtab
20800          pseudo-op at all.  */
20801
20802       /* An all-zero word flags the start of the tbtab, for debuggers
20803          that have to find it by searching forward from the entry
20804          point or from the current pc.  */
20805       fputs ("\t.long 0\n", file);
20806
20807       /* Tbtab format type.  Use format type 0.  */
20808       fputs ("\t.byte 0,", file);
20809
20810       /* Language type.  Unfortunately, there does not seem to be any
20811          official way to discover the language being compiled, so we
20812          use language_string.
20813          C is 0.  Fortran is 1.  Pascal is 2.  Ada is 3.  C++ is 9.
20814          Java is 13.  Objective-C is 14.  Objective-C++ isn't assigned
20815          a number, so for now use 9.  LTO isn't assigned a number either,
20816          so for now use 0.  */
20817       if (! strcmp (language_string, "GNU C")
20818           || ! strcmp (language_string, "GNU GIMPLE"))
20819         i = 0;
20820       else if (! strcmp (language_string, "GNU F77")
20821                || ! strcmp (language_string, "GNU Fortran"))
20822         i = 1;
20823       else if (! strcmp (language_string, "GNU Pascal"))
20824         i = 2;
20825       else if (! strcmp (language_string, "GNU Ada"))
20826         i = 3;
20827       else if (! strcmp (language_string, "GNU C++")
20828                || ! strcmp (language_string, "GNU Objective-C++"))
20829         i = 9;
20830       else if (! strcmp (language_string, "GNU Java"))
20831         i = 13;
20832       else if (! strcmp (language_string, "GNU Objective-C"))
20833         i = 14;
20834       else
20835         gcc_unreachable ();
20836       fprintf (file, "%d,", i);
20837
20838       /* 8 single bit fields: global linkage (not set for C extern linkage,
20839          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
20840          from start of procedure stored in tbtab, internal function, function
20841          has controlled storage, function has no toc, function uses fp,
20842          function logs/aborts fp operations.  */
20843       /* Assume that fp operations are used if any fp reg must be saved.  */
20844       fprintf (file, "%d,",
20845                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
20846
20847       /* 6 bitfields: function is interrupt handler, name present in
20848          proc table, function calls alloca, on condition directives
20849          (controls stack walks, 3 bits), saves condition reg, saves
20850          link reg.  */
20851       /* The `function calls alloca' bit seems to be set whenever reg 31 is
20852          set up as a frame pointer, even when there is no alloca call.  */
20853       fprintf (file, "%d,",
20854                ((optional_tbtab << 6)
20855                 | ((optional_tbtab & frame_pointer_needed) << 5)
20856                 | (info->cr_save_p << 1)
20857                 | (info->lr_save_p)));
20858
20859       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
20860          (6 bits).  */
20861       fprintf (file, "%d,",
20862                (info->push_p << 7) | (64 - info->first_fp_reg_save));
20863
20864       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
20865       fprintf (file, "%d,", (32 - first_reg_to_save ()));
20866
20867       if (optional_tbtab)
20868         {
20869           /* Compute the parameter info from the function decl argument
20870              list.  */
20871           tree decl;
20872           int next_parm_info_bit = 31;
20873
20874           for (decl = DECL_ARGUMENTS (current_function_decl);
20875                decl; decl = TREE_CHAIN (decl))
20876             {
20877               rtx parameter = DECL_INCOMING_RTL (decl);
20878               enum machine_mode mode = GET_MODE (parameter);
20879
20880               if (GET_CODE (parameter) == REG)
20881                 {
20882                   if (SCALAR_FLOAT_MODE_P (mode))
20883                     {
20884                       int bits;
20885
20886                       float_parms++;
20887
20888                       switch (mode)
20889                         {
20890                         case SFmode:
20891                         case SDmode:
20892                           bits = 0x2;
20893                           break;
20894
20895                         case DFmode:
20896                         case DDmode:
20897                         case TFmode:
20898                         case TDmode:
20899                           bits = 0x3;
20900                           break;
20901
20902                         default:
20903                           gcc_unreachable ();
20904                         }
20905
20906                       /* If only one bit will fit, don't or in this entry.  */
20907                       if (next_parm_info_bit > 0)
20908                         parm_info |= (bits << (next_parm_info_bit - 1));
20909                       next_parm_info_bit -= 2;
20910                     }
20911                   else
20912                     {
20913                       fixed_parms += ((GET_MODE_SIZE (mode)
20914                                        + (UNITS_PER_WORD - 1))
20915                                       / UNITS_PER_WORD);
20916                       next_parm_info_bit -= 1;
20917                     }
20918                 }
20919             }
20920         }
20921
20922       /* Number of fixed point parameters.  */
20923       /* This is actually the number of words of fixed point parameters; thus
20924          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
20925       fprintf (file, "%d,", fixed_parms);
20926
20927       /* 2 bitfields: number of floating point parameters (7 bits), parameters
20928          all on stack.  */
20929       /* This is actually the number of fp registers that hold parameters;
20930          and thus the maximum value is 13.  */
20931       /* Set parameters on stack bit if parameters are not in their original
20932          registers, regardless of whether they are on the stack?  Xlc
20933          seems to set the bit when not optimizing.  */
20934       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
20935
20936       if (! optional_tbtab)
20937         return;
20938
20939       /* Optional fields follow.  Some are variable length.  */
20940
20941       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
20942          11 double float.  */
20943       /* There is an entry for each parameter in a register, in the order that
20944          they occur in the parameter list.  Any intervening arguments on the
20945          stack are ignored.  If the list overflows a long (max possible length
20946          34 bits) then completely leave off all elements that don't fit.  */
20947       /* Only emit this long if there was at least one parameter.  */
20948       if (fixed_parms || float_parms)
20949         fprintf (file, "\t.long %d\n", parm_info);
20950
20951       /* Offset from start of code to tb table.  */
20952       fputs ("\t.long ", file);
20953       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
20954       RS6000_OUTPUT_BASENAME (file, fname);
20955       putc ('-', file);
20956       rs6000_output_function_entry (file, fname);
20957       putc ('\n', file);
20958
20959       /* Interrupt handler mask.  */
20960       /* Omit this long, since we never set the interrupt handler bit
20961          above.  */
20962
20963       /* Number of CTL (controlled storage) anchors.  */
20964       /* Omit this long, since the has_ctl bit is never set above.  */
20965
20966       /* Displacement into stack of each CTL anchor.  */
20967       /* Omit this list of longs, because there are no CTL anchors.  */
20968
20969       /* Length of function name.  */
20970       if (*fname == '*')
20971         ++fname;
20972       fprintf (file, "\t.short %d\n", (int) strlen (fname));
20973
20974       /* Function name.  */
20975       assemble_string (fname, strlen (fname));
20976
20977       /* Register for alloca automatic storage; this is always reg 31.
20978          Only emit this if the alloca bit was set above.  */
20979       if (frame_pointer_needed)
20980         fputs ("\t.byte 31\n", file);
20981
20982       fputs ("\t.align 2\n", file);
20983     }
20984 }
20985 \f
20986 /* A C compound statement that outputs the assembler code for a thunk
20987    function, used to implement C++ virtual function calls with
20988    multiple inheritance.  The thunk acts as a wrapper around a virtual
20989    function, adjusting the implicit object parameter before handing
20990    control off to the real function.
20991
20992    First, emit code to add the integer DELTA to the location that
20993    contains the incoming first argument.  Assume that this argument
20994    contains a pointer, and is the one used to pass the `this' pointer
20995    in C++.  This is the incoming argument *before* the function
20996    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
20997    values of all other incoming arguments.
20998
20999    After the addition, emit code to jump to FUNCTION, which is a
21000    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
21001    not touch the return address.  Hence returning from FUNCTION will
21002    return to whoever called the current `thunk'.
21003
21004    The effect must be as if FUNCTION had been called directly with the
21005    adjusted first argument.  This macro is responsible for emitting
21006    all of the code for a thunk function; output_function_prologue()
21007    and output_function_epilogue() are not invoked.
21008
21009    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
21010    been extracted from it.)  It might possibly be useful on some
21011    targets, but probably not.
21012
21013    If you do not define this macro, the target-independent code in the
21014    C++ frontend will generate a less efficient heavyweight thunk that
21015    calls FUNCTION instead of jumping to it.  The generic approach does
21016    not support varargs.  */
21017
21018 static void
21019 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
21020                         HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
21021                         tree function)
21022 {
21023   rtx this_rtx, insn, funexp;
21024
21025   reload_completed = 1;
21026   epilogue_completed = 1;
21027
21028   /* Mark the end of the (empty) prologue.  */
21029   emit_note (NOTE_INSN_PROLOGUE_END);
21030
21031   /* Find the "this" pointer.  If the function returns a structure,
21032      the structure return pointer is in r3.  */
21033   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
21034     this_rtx = gen_rtx_REG (Pmode, 4);
21035   else
21036     this_rtx = gen_rtx_REG (Pmode, 3);
21037
21038   /* Apply the constant offset, if required.  */
21039   if (delta)
21040     emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
21041
21042   /* Apply the offset from the vtable, if required.  */
21043   if (vcall_offset)
21044     {
21045       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
21046       rtx tmp = gen_rtx_REG (Pmode, 12);
21047
21048       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
21049       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
21050         {
21051           emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
21052           emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
21053         }
21054       else
21055         {
21056           rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
21057
21058           emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
21059         }
21060       emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
21061     }
21062
21063   /* Generate a tail call to the target function.  */
21064   if (!TREE_USED (function))
21065     {
21066       assemble_external (function);
21067       TREE_USED (function) = 1;
21068     }
21069   funexp = XEXP (DECL_RTL (function), 0);
21070   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
21071
21072 #if TARGET_MACHO
21073   if (MACHOPIC_INDIRECT)
21074     funexp = machopic_indirect_call_target (funexp);
21075 #endif
21076
21077   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
21078      generate sibcall RTL explicitly.  */
21079   insn = emit_call_insn (
21080            gen_rtx_PARALLEL (VOIDmode,
21081              gen_rtvec (4,
21082                         gen_rtx_CALL (VOIDmode,
21083                                       funexp, const0_rtx),
21084                         gen_rtx_USE (VOIDmode, const0_rtx),
21085                         gen_rtx_USE (VOIDmode,
21086                                      gen_rtx_REG (SImode,
21087                                                   LR_REGNO)),
21088                         gen_rtx_RETURN (VOIDmode))));
21089   SIBLING_CALL_P (insn) = 1;
21090   emit_barrier ();
21091
21092   /* Run just enough of rest_of_compilation to get the insns emitted.
21093      There's not really enough bulk here to make other passes such as
21094      instruction scheduling worth while.  Note that use_thunk calls
21095      assemble_start_function and assemble_end_function.  */
21096   insn = get_insns ();
21097   insn_locators_alloc ();
21098   shorten_branches (insn);
21099   final_start_function (insn, file, 1);
21100   final (insn, file, 1);
21101   final_end_function ();
21102
21103   reload_completed = 0;
21104   epilogue_completed = 0;
21105 }
21106 \f
21107 /* A quick summary of the various types of 'constant-pool tables'
21108    under PowerPC:
21109
21110    Target       Flags           Name            One table per
21111    AIX          (none)          AIX TOC         object file
21112    AIX          -mfull-toc      AIX TOC         object file
21113    AIX          -mminimal-toc   AIX minimal TOC translation unit
21114    SVR4/EABI    (none)          SVR4 SDATA      object file
21115    SVR4/EABI    -fpic           SVR4 pic        object file
21116    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
21117    SVR4/EABI    -mrelocatable   EABI TOC        function
21118    SVR4/EABI    -maix           AIX TOC         object file
21119    SVR4/EABI    -maix -mminimal-toc
21120                                 AIX minimal TOC translation unit
21121
21122    Name                 Reg.    Set by  entries       contains:
21123                                         made by  addrs? fp?     sum?
21124
21125    AIX TOC              2       crt0    as       Y      option  option
21126    AIX minimal TOC      30      prolog  gcc      Y      Y       option
21127    SVR4 SDATA           13      crt0    gcc      N      Y       N
21128    SVR4 pic             30      prolog  ld       Y      not yet N
21129    SVR4 PIC             30      prolog  gcc      Y      option  option
21130    EABI TOC             30      prolog  gcc      Y      option  option
21131
21132 */
21133
21134 /* Hash functions for the hash table.  */
21135
21136 static unsigned
21137 rs6000_hash_constant (rtx k)
21138 {
21139   enum rtx_code code = GET_CODE (k);
21140   enum machine_mode mode = GET_MODE (k);
21141   unsigned result = (code << 3) ^ mode;
21142   const char *format;
21143   int flen, fidx;
21144
21145   format = GET_RTX_FORMAT (code);
21146   flen = strlen (format);
21147   fidx = 0;
21148
21149   switch (code)
21150     {
21151     case LABEL_REF:
21152       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
21153
21154     case CONST_DOUBLE:
21155       if (mode != VOIDmode)
21156         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
21157       flen = 2;
21158       break;
21159
21160     case CODE_LABEL:
21161       fidx = 3;
21162       break;
21163
21164     default:
21165       break;
21166     }
21167
21168   for (; fidx < flen; fidx++)
21169     switch (format[fidx])
21170       {
21171       case 's':
21172         {
21173           unsigned i, len;
21174           const char *str = XSTR (k, fidx);
21175           len = strlen (str);
21176           result = result * 613 + len;
21177           for (i = 0; i < len; i++)
21178             result = result * 613 + (unsigned) str[i];
21179           break;
21180         }
21181       case 'u':
21182       case 'e':
21183         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
21184         break;
21185       case 'i':
21186       case 'n':
21187         result = result * 613 + (unsigned) XINT (k, fidx);
21188         break;
21189       case 'w':
21190         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
21191           result = result * 613 + (unsigned) XWINT (k, fidx);
21192         else
21193           {
21194             size_t i;
21195             for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
21196               result = result * 613 + (unsigned) (XWINT (k, fidx)
21197                                                   >> CHAR_BIT * i);
21198           }
21199         break;
21200       case '0':
21201         break;
21202       default:
21203         gcc_unreachable ();
21204       }
21205
21206   return result;
21207 }
21208
21209 static unsigned
21210 toc_hash_function (const void *hash_entry)
21211 {
21212   const struct toc_hash_struct *thc =
21213     (const struct toc_hash_struct *) hash_entry;
21214   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
21215 }
21216
21217 /* Compare H1 and H2 for equivalence.  */
21218
21219 static int
21220 toc_hash_eq (const void *h1, const void *h2)
21221 {
21222   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
21223   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
21224
21225   if (((const struct toc_hash_struct *) h1)->key_mode
21226       != ((const struct toc_hash_struct *) h2)->key_mode)
21227     return 0;
21228
21229   return rtx_equal_p (r1, r2);
21230 }
21231
21232 /* These are the names given by the C++ front-end to vtables, and
21233    vtable-like objects.  Ideally, this logic should not be here;
21234    instead, there should be some programmatic way of inquiring as
21235    to whether or not an object is a vtable.  */
21236
21237 #define VTABLE_NAME_P(NAME)                             \
21238   (strncmp ("_vt.", name, strlen ("_vt.")) == 0         \
21239   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
21240   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
21241   || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0       \
21242   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
21243
21244 #ifdef NO_DOLLAR_IN_LABEL
21245 /* Return a GGC-allocated character string translating dollar signs in
21246    input NAME to underscores.  Used by XCOFF ASM_OUTPUT_LABELREF.  */
21247
21248 const char *
21249 rs6000_xcoff_strip_dollar (const char *name)
21250 {
21251   char *strip, *p;
21252   int len;
21253
21254   p = strchr (name, '$');
21255
21256   if (p == 0 || p == name)
21257     return name;
21258
21259   len = strlen (name);
21260   strip = (char *) alloca (len + 1);
21261   strcpy (strip, name);
21262   p = strchr (strip, '$');
21263   while (p)
21264     {
21265       *p = '_';
21266       p = strchr (p + 1, '$');
21267     }
21268
21269   return ggc_alloc_string (strip, len);
21270 }
21271 #endif
21272
21273 void
21274 rs6000_output_symbol_ref (FILE *file, rtx x)
21275 {
21276   /* Currently C++ toc references to vtables can be emitted before it
21277      is decided whether the vtable is public or private.  If this is
21278      the case, then the linker will eventually complain that there is
21279      a reference to an unknown section.  Thus, for vtables only,
21280      we emit the TOC reference to reference the symbol and not the
21281      section.  */
21282   const char *name = XSTR (x, 0);
21283
21284   if (VTABLE_NAME_P (name))
21285     {
21286       RS6000_OUTPUT_BASENAME (file, name);
21287     }
21288   else
21289     assemble_name (file, name);
21290 }
21291
21292 /* Output a TOC entry.  We derive the entry name from what is being
21293    written.  */
21294
21295 void
21296 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
21297 {
21298   char buf[256];
21299   const char *name = buf;
21300   rtx base = x;
21301   HOST_WIDE_INT offset = 0;
21302
21303   gcc_assert (!TARGET_NO_TOC);
21304
21305   /* When the linker won't eliminate them, don't output duplicate
21306      TOC entries (this happens on AIX if there is any kind of TOC,
21307      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
21308      CODE_LABELs.  */
21309   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
21310     {
21311       struct toc_hash_struct *h;
21312       void * * found;
21313
21314       /* Create toc_hash_table.  This can't be done at OVERRIDE_OPTIONS
21315          time because GGC is not initialized at that point.  */
21316       if (toc_hash_table == NULL)
21317         toc_hash_table = htab_create_ggc (1021, toc_hash_function,
21318                                           toc_hash_eq, NULL);
21319
21320       h = ggc_alloc_toc_hash_struct ();
21321       h->key = x;
21322       h->key_mode = mode;
21323       h->labelno = labelno;
21324
21325       found = htab_find_slot (toc_hash_table, h, INSERT);
21326       if (*found == NULL)
21327         *found = h;
21328       else  /* This is indeed a duplicate.
21329                Set this label equal to that label.  */
21330         {
21331           fputs ("\t.set ", file);
21332           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21333           fprintf (file, "%d,", labelno);
21334           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21335           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
21336                                               found)->labelno));
21337           return;
21338         }
21339     }
21340
21341   /* If we're going to put a double constant in the TOC, make sure it's
21342      aligned properly when strict alignment is on.  */
21343   if (GET_CODE (x) == CONST_DOUBLE
21344       && STRICT_ALIGNMENT
21345       && GET_MODE_BITSIZE (mode) >= 64
21346       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
21347     ASM_OUTPUT_ALIGN (file, 3);
21348   }
21349
21350   (*targetm.asm_out.internal_label) (file, "LC", labelno);
21351
21352   /* Handle FP constants specially.  Note that if we have a minimal
21353      TOC, things we put here aren't actually in the TOC, so we can allow
21354      FP constants.  */
21355   if (GET_CODE (x) == CONST_DOUBLE &&
21356       (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
21357     {
21358       REAL_VALUE_TYPE rv;
21359       long k[4];
21360
21361       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21362       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21363         REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
21364       else
21365         REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
21366
21367       if (TARGET_64BIT)
21368         {
21369           if (TARGET_MINIMAL_TOC)
21370             fputs (DOUBLE_INT_ASM_OP, file);
21371           else
21372             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21373                      k[0] & 0xffffffff, k[1] & 0xffffffff,
21374                      k[2] & 0xffffffff, k[3] & 0xffffffff);
21375           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
21376                    k[0] & 0xffffffff, k[1] & 0xffffffff,
21377                    k[2] & 0xffffffff, k[3] & 0xffffffff);
21378           return;
21379         }
21380       else
21381         {
21382           if (TARGET_MINIMAL_TOC)
21383             fputs ("\t.long ", file);
21384           else
21385             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21386                      k[0] & 0xffffffff, k[1] & 0xffffffff,
21387                      k[2] & 0xffffffff, k[3] & 0xffffffff);
21388           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
21389                    k[0] & 0xffffffff, k[1] & 0xffffffff,
21390                    k[2] & 0xffffffff, k[3] & 0xffffffff);
21391           return;
21392         }
21393     }
21394   else if (GET_CODE (x) == CONST_DOUBLE &&
21395            (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
21396     {
21397       REAL_VALUE_TYPE rv;
21398       long k[2];
21399
21400       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21401
21402       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21403         REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
21404       else
21405         REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
21406
21407       if (TARGET_64BIT)
21408         {
21409           if (TARGET_MINIMAL_TOC)
21410             fputs (DOUBLE_INT_ASM_OP, file);
21411           else
21412             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
21413                      k[0] & 0xffffffff, k[1] & 0xffffffff);
21414           fprintf (file, "0x%lx%08lx\n",
21415                    k[0] & 0xffffffff, k[1] & 0xffffffff);
21416           return;
21417         }
21418       else
21419         {
21420           if (TARGET_MINIMAL_TOC)
21421             fputs ("\t.long ", file);
21422           else
21423             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
21424                      k[0] & 0xffffffff, k[1] & 0xffffffff);
21425           fprintf (file, "0x%lx,0x%lx\n",
21426                    k[0] & 0xffffffff, k[1] & 0xffffffff);
21427           return;
21428         }
21429     }
21430   else if (GET_CODE (x) == CONST_DOUBLE &&
21431            (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
21432     {
21433       REAL_VALUE_TYPE rv;
21434       long l;
21435
21436       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21437       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21438         REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
21439       else
21440         REAL_VALUE_TO_TARGET_SINGLE (rv, l);
21441
21442       if (TARGET_64BIT)
21443         {
21444           if (TARGET_MINIMAL_TOC)
21445             fputs (DOUBLE_INT_ASM_OP, file);
21446           else
21447             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
21448           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
21449           return;
21450         }
21451       else
21452         {
21453           if (TARGET_MINIMAL_TOC)
21454             fputs ("\t.long ", file);
21455           else
21456             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
21457           fprintf (file, "0x%lx\n", l & 0xffffffff);
21458           return;
21459         }
21460     }
21461   else if (GET_MODE (x) == VOIDmode
21462            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
21463     {
21464       unsigned HOST_WIDE_INT low;
21465       HOST_WIDE_INT high;
21466
21467       if (GET_CODE (x) == CONST_DOUBLE)
21468         {
21469           low = CONST_DOUBLE_LOW (x);
21470           high = CONST_DOUBLE_HIGH (x);
21471         }
21472       else
21473 #if HOST_BITS_PER_WIDE_INT == 32
21474         {
21475           low = INTVAL (x);
21476           high = (low & 0x80000000) ? ~0 : 0;
21477         }
21478 #else
21479         {
21480           low = INTVAL (x) & 0xffffffff;
21481           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
21482         }
21483 #endif
21484
21485       /* TOC entries are always Pmode-sized, but since this
21486          is a bigendian machine then if we're putting smaller
21487          integer constants in the TOC we have to pad them.
21488          (This is still a win over putting the constants in
21489          a separate constant pool, because then we'd have
21490          to have both a TOC entry _and_ the actual constant.)
21491
21492          For a 32-bit target, CONST_INT values are loaded and shifted
21493          entirely within `low' and can be stored in one TOC entry.  */
21494
21495       /* It would be easy to make this work, but it doesn't now.  */
21496       gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
21497
21498       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
21499         {
21500 #if HOST_BITS_PER_WIDE_INT == 32
21501           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
21502                          POINTER_SIZE, &low, &high, 0);
21503 #else
21504           low |= high << 32;
21505           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
21506           high = (HOST_WIDE_INT) low >> 32;
21507           low &= 0xffffffff;
21508 #endif
21509         }
21510
21511       if (TARGET_64BIT)
21512         {
21513           if (TARGET_MINIMAL_TOC)
21514             fputs (DOUBLE_INT_ASM_OP, file);
21515           else
21516             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
21517                      (long) high & 0xffffffff, (long) low & 0xffffffff);
21518           fprintf (file, "0x%lx%08lx\n",
21519                    (long) high & 0xffffffff, (long) low & 0xffffffff);
21520           return;
21521         }
21522       else
21523         {
21524           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
21525             {
21526               if (TARGET_MINIMAL_TOC)
21527                 fputs ("\t.long ", file);
21528               else
21529                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
21530                          (long) high & 0xffffffff, (long) low & 0xffffffff);
21531               fprintf (file, "0x%lx,0x%lx\n",
21532                        (long) high & 0xffffffff, (long) low & 0xffffffff);
21533             }
21534           else
21535             {
21536               if (TARGET_MINIMAL_TOC)
21537                 fputs ("\t.long ", file);
21538               else
21539                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
21540               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
21541             }
21542           return;
21543         }
21544     }
21545
21546   if (GET_CODE (x) == CONST)
21547     {
21548       gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
21549                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
21550
21551       base = XEXP (XEXP (x, 0), 0);
21552       offset = INTVAL (XEXP (XEXP (x, 0), 1));
21553     }
21554
21555   switch (GET_CODE (base))
21556     {
21557     case SYMBOL_REF:
21558       name = XSTR (base, 0);
21559       break;
21560
21561     case LABEL_REF:
21562       ASM_GENERATE_INTERNAL_LABEL (buf, "L",
21563                                    CODE_LABEL_NUMBER (XEXP (base, 0)));
21564       break;
21565
21566     case CODE_LABEL:
21567       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
21568       break;
21569
21570     default:
21571       gcc_unreachable ();
21572     }
21573
21574   if (TARGET_MINIMAL_TOC)
21575     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
21576   else
21577     {
21578       fputs ("\t.tc ", file);
21579       RS6000_OUTPUT_BASENAME (file, name);
21580
21581       if (offset < 0)
21582         fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
21583       else if (offset)
21584         fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
21585
21586       fputs ("[TC],", file);
21587     }
21588
21589   /* Currently C++ toc references to vtables can be emitted before it
21590      is decided whether the vtable is public or private.  If this is
21591      the case, then the linker will eventually complain that there is
21592      a TOC reference to an unknown section.  Thus, for vtables only,
21593      we emit the TOC reference to reference the symbol and not the
21594      section.  */
21595   if (VTABLE_NAME_P (name))
21596     {
21597       RS6000_OUTPUT_BASENAME (file, name);
21598       if (offset < 0)
21599         fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
21600       else if (offset > 0)
21601         fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
21602     }
21603   else
21604     output_addr_const (file, x);
21605   putc ('\n', file);
21606 }
21607 \f
21608 /* Output an assembler pseudo-op to write an ASCII string of N characters
21609    starting at P to FILE.
21610
21611    On the RS/6000, we have to do this using the .byte operation and
21612    write out special characters outside the quoted string.
21613    Also, the assembler is broken; very long strings are truncated,
21614    so we must artificially break them up early.  */
21615
21616 void
21617 output_ascii (FILE *file, const char *p, int n)
21618 {
21619   char c;
21620   int i, count_string;
21621   const char *for_string = "\t.byte \"";
21622   const char *for_decimal = "\t.byte ";
21623   const char *to_close = NULL;
21624
21625   count_string = 0;
21626   for (i = 0; i < n; i++)
21627     {
21628       c = *p++;
21629       if (c >= ' ' && c < 0177)
21630         {
21631           if (for_string)
21632             fputs (for_string, file);
21633           putc (c, file);
21634
21635           /* Write two quotes to get one.  */
21636           if (c == '"')
21637             {
21638               putc (c, file);
21639               ++count_string;
21640             }
21641
21642           for_string = NULL;
21643           for_decimal = "\"\n\t.byte ";
21644           to_close = "\"\n";
21645           ++count_string;
21646
21647           if (count_string >= 512)
21648             {
21649               fputs (to_close, file);
21650
21651               for_string = "\t.byte \"";
21652               for_decimal = "\t.byte ";
21653               to_close = NULL;
21654               count_string = 0;
21655             }
21656         }
21657       else
21658         {
21659           if (for_decimal)
21660             fputs (for_decimal, file);
21661           fprintf (file, "%d", c);
21662
21663           for_string = "\n\t.byte \"";
21664           for_decimal = ", ";
21665           to_close = "\n";
21666           count_string = 0;
21667         }
21668     }
21669
21670   /* Now close the string if we have written one.  Then end the line.  */
21671   if (to_close)
21672     fputs (to_close, file);
21673 }
21674 \f
21675 /* Generate a unique section name for FILENAME for a section type
21676    represented by SECTION_DESC.  Output goes into BUF.
21677
21678    SECTION_DESC can be any string, as long as it is different for each
21679    possible section type.
21680
21681    We name the section in the same manner as xlc.  The name begins with an
21682    underscore followed by the filename (after stripping any leading directory
21683    names) with the last period replaced by the string SECTION_DESC.  If
21684    FILENAME does not contain a period, SECTION_DESC is appended to the end of
21685    the name.  */
21686
21687 void
21688 rs6000_gen_section_name (char **buf, const char *filename,
21689                          const char *section_desc)
21690 {
21691   const char *q, *after_last_slash, *last_period = 0;
21692   char *p;
21693   int len;
21694
21695   after_last_slash = filename;
21696   for (q = filename; *q; q++)
21697     {
21698       if (*q == '/')
21699         after_last_slash = q + 1;
21700       else if (*q == '.')
21701         last_period = q;
21702     }
21703
21704   len = strlen (after_last_slash) + strlen (section_desc) + 2;
21705   *buf = (char *) xmalloc (len);
21706
21707   p = *buf;
21708   *p++ = '_';
21709
21710   for (q = after_last_slash; *q; q++)
21711     {
21712       if (q == last_period)
21713         {
21714           strcpy (p, section_desc);
21715           p += strlen (section_desc);
21716           break;
21717         }
21718
21719       else if (ISALNUM (*q))
21720         *p++ = *q;
21721     }
21722
21723   if (last_period == 0)
21724     strcpy (p, section_desc);
21725   else
21726     *p = '\0';
21727 }
21728 \f
21729 /* Emit profile function.  */
21730
21731 void
21732 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
21733 {
21734   /* Non-standard profiling for kernels, which just saves LR then calls
21735      _mcount without worrying about arg saves.  The idea is to change
21736      the function prologue as little as possible as it isn't easy to
21737      account for arg save/restore code added just for _mcount.  */
21738   if (TARGET_PROFILE_KERNEL)
21739     return;
21740
21741   if (DEFAULT_ABI == ABI_AIX)
21742     {
21743 #ifndef NO_PROFILE_COUNTERS
21744 # define NO_PROFILE_COUNTERS 0
21745 #endif
21746       if (NO_PROFILE_COUNTERS)
21747         emit_library_call (init_one_libfunc (RS6000_MCOUNT),
21748                            LCT_NORMAL, VOIDmode, 0);
21749       else
21750         {
21751           char buf[30];
21752           const char *label_name;
21753           rtx fun;
21754
21755           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
21756           label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
21757           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
21758
21759           emit_library_call (init_one_libfunc (RS6000_MCOUNT),
21760                              LCT_NORMAL, VOIDmode, 1, fun, Pmode);
21761         }
21762     }
21763   else if (DEFAULT_ABI == ABI_DARWIN)
21764     {
21765       const char *mcount_name = RS6000_MCOUNT;
21766       int caller_addr_regno = LR_REGNO;
21767
21768       /* Be conservative and always set this, at least for now.  */
21769       crtl->uses_pic_offset_table = 1;
21770
21771 #if TARGET_MACHO
21772       /* For PIC code, set up a stub and collect the caller's address
21773          from r0, which is where the prologue puts it.  */
21774       if (MACHOPIC_INDIRECT
21775           && crtl->uses_pic_offset_table)
21776         caller_addr_regno = 0;
21777 #endif
21778       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
21779                          LCT_NORMAL, VOIDmode, 1,
21780                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
21781     }
21782 }
21783
21784 /* Write function profiler code.  */
21785
21786 void
21787 output_function_profiler (FILE *file, int labelno)
21788 {
21789   char buf[100];
21790
21791   switch (DEFAULT_ABI)
21792     {
21793     default:
21794       gcc_unreachable ();
21795
21796     case ABI_V4:
21797       if (!TARGET_32BIT)
21798         {
21799           warning (0, "no profiling of 64-bit code for this ABI");
21800           return;
21801         }
21802       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
21803       fprintf (file, "\tmflr %s\n", reg_names[0]);
21804       if (NO_PROFILE_COUNTERS)
21805         {
21806           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
21807                        reg_names[0], reg_names[1]);
21808         }
21809       else if (TARGET_SECURE_PLT && flag_pic)
21810         {
21811           asm_fprintf (file, "\tbcl 20,31,1f\n1:\n\t{st|stw} %s,4(%s)\n",
21812                        reg_names[0], reg_names[1]);
21813           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
21814           asm_fprintf (file, "\t{cau|addis} %s,%s,",
21815                        reg_names[12], reg_names[12]);
21816           assemble_name (file, buf);
21817           asm_fprintf (file, "-1b@ha\n\t{cal|la} %s,", reg_names[0]);
21818           assemble_name (file, buf);
21819           asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
21820         }
21821       else if (flag_pic == 1)
21822         {
21823           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
21824           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
21825                        reg_names[0], reg_names[1]);
21826           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
21827           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
21828           assemble_name (file, buf);
21829           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
21830         }
21831       else if (flag_pic > 1)
21832         {
21833           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
21834                        reg_names[0], reg_names[1]);
21835           /* Now, we need to get the address of the label.  */
21836           fputs ("\tbcl 20,31,1f\n\t.long ", file);
21837           assemble_name (file, buf);
21838           fputs ("-.\n1:", file);
21839           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
21840           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n",
21841                        reg_names[0], reg_names[11]);
21842           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
21843                        reg_names[0], reg_names[0], reg_names[11]);
21844         }
21845       else
21846         {
21847           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
21848           assemble_name (file, buf);
21849           fputs ("@ha\n", file);
21850           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
21851                        reg_names[0], reg_names[1]);
21852           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
21853           assemble_name (file, buf);
21854           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
21855         }
21856
21857       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
21858       fprintf (file, "\tbl %s%s\n",
21859                RS6000_MCOUNT, flag_pic ? "@plt" : "");
21860       break;
21861
21862     case ABI_AIX:
21863     case ABI_DARWIN:
21864       if (!TARGET_PROFILE_KERNEL)
21865         {
21866           /* Don't do anything, done in output_profile_hook ().  */
21867         }
21868       else
21869         {
21870           gcc_assert (!TARGET_32BIT);
21871
21872           asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
21873           asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
21874
21875           if (cfun->static_chain_decl != NULL)
21876             {
21877               asm_fprintf (file, "\tstd %s,24(%s)\n",
21878                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
21879               fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
21880               asm_fprintf (file, "\tld %s,24(%s)\n",
21881                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
21882             }
21883           else
21884             fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
21885         }
21886       break;
21887     }
21888 }
21889
21890 \f
21891
21892 /* The following variable value is the last issued insn.  */
21893
21894 static rtx last_scheduled_insn;
21895
21896 /* The following variable helps to balance issuing of load and
21897    store instructions */
21898
21899 static int load_store_pendulum;
21900
21901 /* Power4 load update and store update instructions are cracked into a
21902    load or store and an integer insn which are executed in the same cycle.
21903    Branches have their own dispatch slot which does not count against the
21904    GCC issue rate, but it changes the program flow so there are no other
21905    instructions to issue in this cycle.  */
21906
21907 static int
21908 rs6000_variable_issue_1 (rtx insn, int more)
21909 {
21910   last_scheduled_insn = insn;
21911   if (GET_CODE (PATTERN (insn)) == USE
21912       || GET_CODE (PATTERN (insn)) == CLOBBER)
21913     {
21914       cached_can_issue_more = more;
21915       return cached_can_issue_more;
21916     }
21917
21918   if (insn_terminates_group_p (insn, current_group))
21919     {
21920       cached_can_issue_more = 0;
21921       return cached_can_issue_more;
21922     }
21923
21924   /* If no reservation, but reach here */
21925   if (recog_memoized (insn) < 0)
21926     return more;
21927
21928   if (rs6000_sched_groups)
21929     {
21930       if (is_microcoded_insn (insn))
21931         cached_can_issue_more = 0;
21932       else if (is_cracked_insn (insn))
21933         cached_can_issue_more = more > 2 ? more - 2 : 0;
21934       else
21935         cached_can_issue_more = more - 1;
21936
21937       return cached_can_issue_more;
21938     }
21939
21940   if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
21941     return 0;
21942
21943   cached_can_issue_more = more - 1;
21944   return cached_can_issue_more;
21945 }
21946
21947 static int
21948 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
21949 {
21950   int r = rs6000_variable_issue_1 (insn, more);
21951   if (verbose)
21952     fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
21953   return r;
21954 }
21955
21956 /* Adjust the cost of a scheduling dependency.  Return the new cost of
21957    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
21958
21959 static int
21960 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
21961 {
21962   enum attr_type attr_type;
21963
21964   if (! recog_memoized (insn))
21965     return 0;
21966
21967   switch (REG_NOTE_KIND (link))
21968     {
21969     case REG_DEP_TRUE:
21970       {
21971         /* Data dependency; DEP_INSN writes a register that INSN reads
21972            some cycles later.  */
21973
21974         /* Separate a load from a narrower, dependent store.  */
21975         if (rs6000_sched_groups
21976             && GET_CODE (PATTERN (insn)) == SET
21977             && GET_CODE (PATTERN (dep_insn)) == SET
21978             && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
21979             && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
21980             && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
21981                 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
21982           return cost + 14;
21983
21984         attr_type = get_attr_type (insn);
21985
21986         switch (attr_type)
21987           {
21988           case TYPE_JMPREG:
21989             /* Tell the first scheduling pass about the latency between
21990                a mtctr and bctr (and mtlr and br/blr).  The first
21991                scheduling pass will not know about this latency since
21992                the mtctr instruction, which has the latency associated
21993                to it, will be generated by reload.  */
21994             return TARGET_POWER ? 5 : 4;
21995           case TYPE_BRANCH:
21996             /* Leave some extra cycles between a compare and its
21997                dependent branch, to inhibit expensive mispredicts.  */
21998             if ((rs6000_cpu_attr == CPU_PPC603
21999                  || rs6000_cpu_attr == CPU_PPC604
22000                  || rs6000_cpu_attr == CPU_PPC604E
22001                  || rs6000_cpu_attr == CPU_PPC620
22002                  || rs6000_cpu_attr == CPU_PPC630
22003                  || rs6000_cpu_attr == CPU_PPC750
22004                  || rs6000_cpu_attr == CPU_PPC7400
22005                  || rs6000_cpu_attr == CPU_PPC7450
22006                  || rs6000_cpu_attr == CPU_POWER4
22007                  || rs6000_cpu_attr == CPU_POWER5
22008                  || rs6000_cpu_attr == CPU_POWER7
22009                  || rs6000_cpu_attr == CPU_CELL)
22010                 && recog_memoized (dep_insn)
22011                 && (INSN_CODE (dep_insn) >= 0))
22012
22013               switch (get_attr_type (dep_insn))
22014                 {
22015                 case TYPE_CMP:
22016                 case TYPE_COMPARE:
22017                 case TYPE_DELAYED_COMPARE:
22018                 case TYPE_IMUL_COMPARE:
22019                 case TYPE_LMUL_COMPARE:
22020                 case TYPE_FPCOMPARE:
22021                 case TYPE_CR_LOGICAL:
22022                 case TYPE_DELAYED_CR:
22023                   return cost + 2;
22024                 default:
22025                   break;
22026                 }
22027             break;
22028
22029           case TYPE_STORE:
22030           case TYPE_STORE_U:
22031           case TYPE_STORE_UX:
22032           case TYPE_FPSTORE:
22033           case TYPE_FPSTORE_U:
22034           case TYPE_FPSTORE_UX:
22035             if ((rs6000_cpu == PROCESSOR_POWER6)
22036                 && recog_memoized (dep_insn)
22037                 && (INSN_CODE (dep_insn) >= 0))
22038               {
22039
22040                 if (GET_CODE (PATTERN (insn)) != SET)
22041                   /* If this happens, we have to extend this to schedule
22042                      optimally.  Return default for now.  */
22043                   return cost;
22044
22045                 /* Adjust the cost for the case where the value written
22046                    by a fixed point operation is used as the address
22047                    gen value on a store. */
22048                 switch (get_attr_type (dep_insn))
22049                   {
22050                   case TYPE_LOAD:
22051                   case TYPE_LOAD_U:
22052                   case TYPE_LOAD_UX:
22053                   case TYPE_CNTLZ:
22054                     {
22055                       if (! store_data_bypass_p (dep_insn, insn))
22056                         return 4;
22057                       break;
22058                     }
22059                   case TYPE_LOAD_EXT:
22060                   case TYPE_LOAD_EXT_U:
22061                   case TYPE_LOAD_EXT_UX:
22062                   case TYPE_VAR_SHIFT_ROTATE:
22063                   case TYPE_VAR_DELAYED_COMPARE:
22064                     {
22065                       if (! store_data_bypass_p (dep_insn, insn))
22066                         return 6;
22067                       break;
22068                     }
22069                   case TYPE_INTEGER:
22070                   case TYPE_COMPARE:
22071                   case TYPE_FAST_COMPARE:
22072                   case TYPE_EXTS:
22073                   case TYPE_SHIFT:
22074                   case TYPE_INSERT_WORD:
22075                   case TYPE_INSERT_DWORD:
22076                   case TYPE_FPLOAD_U:
22077                   case TYPE_FPLOAD_UX:
22078                   case TYPE_STORE_U:
22079                   case TYPE_STORE_UX:
22080                   case TYPE_FPSTORE_U:
22081                   case TYPE_FPSTORE_UX:
22082                     {
22083                       if (! store_data_bypass_p (dep_insn, insn))
22084                         return 3;
22085                       break;
22086                     }
22087                   case TYPE_IMUL:
22088                   case TYPE_IMUL2:
22089                   case TYPE_IMUL3:
22090                   case TYPE_LMUL:
22091                   case TYPE_IMUL_COMPARE:
22092                   case TYPE_LMUL_COMPARE:
22093                     {
22094                       if (! store_data_bypass_p (dep_insn, insn))
22095                         return 17;
22096                       break;
22097                     }
22098                   case TYPE_IDIV:
22099                     {
22100                       if (! store_data_bypass_p (dep_insn, insn))
22101                         return 45;
22102                       break;
22103                     }
22104                   case TYPE_LDIV:
22105                     {
22106                       if (! store_data_bypass_p (dep_insn, insn))
22107                         return 57;
22108                       break;
22109                     }
22110                   default:
22111                     break;
22112                   }
22113               }
22114             break;
22115
22116           case TYPE_LOAD:
22117           case TYPE_LOAD_U:
22118           case TYPE_LOAD_UX:
22119           case TYPE_LOAD_EXT:
22120           case TYPE_LOAD_EXT_U:
22121           case TYPE_LOAD_EXT_UX:
22122             if ((rs6000_cpu == PROCESSOR_POWER6)
22123                 && recog_memoized (dep_insn)
22124                 && (INSN_CODE (dep_insn) >= 0))
22125               {
22126
22127                 /* Adjust the cost for the case where the value written
22128                    by a fixed point instruction is used within the address
22129                    gen portion of a subsequent load(u)(x) */
22130                 switch (get_attr_type (dep_insn))
22131                   {
22132                   case TYPE_LOAD:
22133                   case TYPE_LOAD_U:
22134                   case TYPE_LOAD_UX:
22135                   case TYPE_CNTLZ:
22136                     {
22137                       if (set_to_load_agen (dep_insn, insn))
22138                         return 4;
22139                       break;
22140                     }
22141                   case TYPE_LOAD_EXT:
22142                   case TYPE_LOAD_EXT_U:
22143                   case TYPE_LOAD_EXT_UX:
22144                   case TYPE_VAR_SHIFT_ROTATE:
22145                   case TYPE_VAR_DELAYED_COMPARE:
22146                     {
22147                       if (set_to_load_agen (dep_insn, insn))
22148                         return 6;
22149                       break;
22150                     }
22151                   case TYPE_INTEGER:
22152                   case TYPE_COMPARE:
22153                   case TYPE_FAST_COMPARE:
22154                   case TYPE_EXTS:
22155                   case TYPE_SHIFT:
22156                   case TYPE_INSERT_WORD:
22157                   case TYPE_INSERT_DWORD:
22158                   case TYPE_FPLOAD_U:
22159                   case TYPE_FPLOAD_UX:
22160                   case TYPE_STORE_U:
22161                   case TYPE_STORE_UX:
22162                   case TYPE_FPSTORE_U:
22163                   case TYPE_FPSTORE_UX:
22164                     {
22165                       if (set_to_load_agen (dep_insn, insn))
22166                         return 3;
22167                       break;
22168                     }
22169                   case TYPE_IMUL:
22170                   case TYPE_IMUL2:
22171                   case TYPE_IMUL3:
22172                   case TYPE_LMUL:
22173                   case TYPE_IMUL_COMPARE:
22174                   case TYPE_LMUL_COMPARE:
22175                     {
22176                       if (set_to_load_agen (dep_insn, insn))
22177                         return 17;
22178                       break;
22179                     }
22180                   case TYPE_IDIV:
22181                     {
22182                       if (set_to_load_agen (dep_insn, insn))
22183                         return 45;
22184                       break;
22185                     }
22186                   case TYPE_LDIV:
22187                     {
22188                       if (set_to_load_agen (dep_insn, insn))
22189                         return 57;
22190                       break;
22191                     }
22192                   default:
22193                     break;
22194                   }
22195               }
22196             break;
22197
22198           case TYPE_FPLOAD:
22199             if ((rs6000_cpu == PROCESSOR_POWER6)
22200                 && recog_memoized (dep_insn)
22201                 && (INSN_CODE (dep_insn) >= 0)
22202                 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
22203               return 2;
22204
22205           default:
22206             break;
22207           }
22208
22209         /* Fall out to return default cost.  */
22210       }
22211       break;
22212
22213     case REG_DEP_OUTPUT:
22214       /* Output dependency; DEP_INSN writes a register that INSN writes some
22215          cycles later.  */
22216       if ((rs6000_cpu == PROCESSOR_POWER6)
22217           && recog_memoized (dep_insn)
22218           && (INSN_CODE (dep_insn) >= 0))
22219         {
22220           attr_type = get_attr_type (insn);
22221
22222           switch (attr_type)
22223             {
22224             case TYPE_FP:
22225               if (get_attr_type (dep_insn) == TYPE_FP)
22226                 return 1;
22227               break;
22228             case TYPE_FPLOAD:
22229               if (get_attr_type (dep_insn) == TYPE_MFFGPR)
22230                 return 2;
22231               break;
22232             default:
22233               break;
22234             }
22235         }
22236     case REG_DEP_ANTI:
22237       /* Anti dependency; DEP_INSN reads a register that INSN writes some
22238          cycles later.  */
22239       return 0;
22240
22241     default:
22242       gcc_unreachable ();
22243     }
22244
22245   return cost;
22246 }
22247
22248 /* Debug version of rs6000_adjust_cost.  */
22249
22250 static int
22251 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22252 {
22253   int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
22254
22255   if (ret != cost)
22256     {
22257       const char *dep;
22258
22259       switch (REG_NOTE_KIND (link))
22260         {
22261         default:             dep = "unknown depencency"; break;
22262         case REG_DEP_TRUE:   dep = "data dependency";    break;
22263         case REG_DEP_OUTPUT: dep = "output dependency";  break;
22264         case REG_DEP_ANTI:   dep = "anti depencency";    break;
22265         }
22266
22267       fprintf (stderr,
22268                "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
22269                "%s, insn:\n", ret, cost, dep);
22270
22271       debug_rtx (insn);
22272     }
22273
22274   return ret;
22275 }
22276
22277 /* The function returns a true if INSN is microcoded.
22278    Return false otherwise.  */
22279
22280 static bool
22281 is_microcoded_insn (rtx insn)
22282 {
22283   if (!insn || !NONDEBUG_INSN_P (insn)
22284       || GET_CODE (PATTERN (insn)) == USE
22285       || GET_CODE (PATTERN (insn)) == CLOBBER)
22286     return false;
22287
22288   if (rs6000_cpu_attr == CPU_CELL)
22289     return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
22290
22291   if (rs6000_sched_groups)
22292     {
22293       enum attr_type type = get_attr_type (insn);
22294       if (type == TYPE_LOAD_EXT_U
22295           || type == TYPE_LOAD_EXT_UX
22296           || type == TYPE_LOAD_UX
22297           || type == TYPE_STORE_UX
22298           || type == TYPE_MFCR)
22299         return true;
22300     }
22301
22302   return false;
22303 }
22304
22305 /* The function returns true if INSN is cracked into 2 instructions
22306    by the processor (and therefore occupies 2 issue slots).  */
22307
22308 static bool
22309 is_cracked_insn (rtx insn)
22310 {
22311   if (!insn || !NONDEBUG_INSN_P (insn)
22312       || GET_CODE (PATTERN (insn)) == USE
22313       || GET_CODE (PATTERN (insn)) == CLOBBER)
22314     return false;
22315
22316   if (rs6000_sched_groups)
22317     {
22318       enum attr_type type = get_attr_type (insn);
22319       if (type == TYPE_LOAD_U || type == TYPE_STORE_U
22320           || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
22321           || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
22322           || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
22323           || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
22324           || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
22325           || type == TYPE_IDIV || type == TYPE_LDIV
22326           || type == TYPE_INSERT_WORD)
22327         return true;
22328     }
22329
22330   return false;
22331 }
22332
22333 /* The function returns true if INSN can be issued only from
22334    the branch slot.  */
22335
22336 static bool
22337 is_branch_slot_insn (rtx insn)
22338 {
22339   if (!insn || !NONDEBUG_INSN_P (insn)
22340       || GET_CODE (PATTERN (insn)) == USE
22341       || GET_CODE (PATTERN (insn)) == CLOBBER)
22342     return false;
22343
22344   if (rs6000_sched_groups)
22345     {
22346       enum attr_type type = get_attr_type (insn);
22347       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
22348         return true;
22349       return false;
22350     }
22351
22352   return false;
22353 }
22354
22355 /* The function returns true if out_inst sets a value that is
22356    used in the address generation computation of in_insn */
22357 static bool
22358 set_to_load_agen (rtx out_insn, rtx in_insn)
22359 {
22360   rtx out_set, in_set;
22361
22362   /* For performance reasons, only handle the simple case where
22363      both loads are a single_set. */
22364   out_set = single_set (out_insn);
22365   if (out_set)
22366     {
22367       in_set = single_set (in_insn);
22368       if (in_set)
22369         return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
22370     }
22371
22372   return false;
22373 }
22374
22375 /* The function returns true if the target storage location of
22376    out_insn is adjacent to the target storage location of in_insn */
22377 /* Return 1 if memory locations are adjacent.  */
22378
22379 static bool
22380 adjacent_mem_locations (rtx insn1, rtx insn2)
22381 {
22382
22383   rtx a = get_store_dest (PATTERN (insn1));
22384   rtx b = get_store_dest (PATTERN (insn2));
22385
22386   if ((GET_CODE (XEXP (a, 0)) == REG
22387        || (GET_CODE (XEXP (a, 0)) == PLUS
22388            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
22389       && (GET_CODE (XEXP (b, 0)) == REG
22390           || (GET_CODE (XEXP (b, 0)) == PLUS
22391               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
22392     {
22393       HOST_WIDE_INT val0 = 0, val1 = 0, val_diff;
22394       rtx reg0, reg1;
22395
22396       if (GET_CODE (XEXP (a, 0)) == PLUS)
22397         {
22398           reg0 = XEXP (XEXP (a, 0), 0);
22399           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
22400         }
22401       else
22402         reg0 = XEXP (a, 0);
22403
22404       if (GET_CODE (XEXP (b, 0)) == PLUS)
22405         {
22406           reg1 = XEXP (XEXP (b, 0), 0);
22407           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
22408         }
22409       else
22410         reg1 = XEXP (b, 0);
22411
22412       val_diff = val1 - val0;
22413
22414       return ((REGNO (reg0) == REGNO (reg1))
22415               && ((MEM_SIZE (a) && val_diff == INTVAL (MEM_SIZE (a)))
22416                   || (MEM_SIZE (b) && val_diff == -INTVAL (MEM_SIZE (b)))));
22417     }
22418
22419   return false;
22420 }
22421
22422 /* A C statement (sans semicolon) to update the integer scheduling
22423    priority INSN_PRIORITY (INSN). Increase the priority to execute the
22424    INSN earlier, reduce the priority to execute INSN later.  Do not
22425    define this macro if you do not need to adjust the scheduling
22426    priorities of insns.  */
22427
22428 static int
22429 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
22430 {
22431   /* On machines (like the 750) which have asymmetric integer units,
22432      where one integer unit can do multiply and divides and the other
22433      can't, reduce the priority of multiply/divide so it is scheduled
22434      before other integer operations.  */
22435
22436 #if 0
22437   if (! INSN_P (insn))
22438     return priority;
22439
22440   if (GET_CODE (PATTERN (insn)) == USE)
22441     return priority;
22442
22443   switch (rs6000_cpu_attr) {
22444   case CPU_PPC750:
22445     switch (get_attr_type (insn))
22446       {
22447       default:
22448         break;
22449
22450       case TYPE_IMUL:
22451       case TYPE_IDIV:
22452         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
22453                  priority, priority);
22454         if (priority >= 0 && priority < 0x01000000)
22455           priority >>= 3;
22456         break;
22457       }
22458   }
22459 #endif
22460
22461   if (insn_must_be_first_in_group (insn)
22462       && reload_completed
22463       && current_sched_info->sched_max_insns_priority
22464       && rs6000_sched_restricted_insns_priority)
22465     {
22466
22467       /* Prioritize insns that can be dispatched only in the first
22468          dispatch slot.  */
22469       if (rs6000_sched_restricted_insns_priority == 1)
22470         /* Attach highest priority to insn. This means that in
22471            haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
22472            precede 'priority' (critical path) considerations.  */
22473         return current_sched_info->sched_max_insns_priority;
22474       else if (rs6000_sched_restricted_insns_priority == 2)
22475         /* Increase priority of insn by a minimal amount. This means that in
22476            haifa-sched.c:ready_sort(), only 'priority' (critical path)
22477            considerations precede dispatch-slot restriction considerations.  */
22478         return (priority + 1);
22479     }
22480
22481   if (rs6000_cpu == PROCESSOR_POWER6
22482       && ((load_store_pendulum == -2 && is_load_insn (insn))
22483           || (load_store_pendulum == 2 && is_store_insn (insn))))
22484     /* Attach highest priority to insn if the scheduler has just issued two
22485        stores and this instruction is a load, or two loads and this instruction
22486        is a store. Power6 wants loads and stores scheduled alternately
22487        when possible */
22488     return current_sched_info->sched_max_insns_priority;
22489
22490   return priority;
22491 }
22492
22493 /* Return true if the instruction is nonpipelined on the Cell. */
22494 static bool
22495 is_nonpipeline_insn (rtx insn)
22496 {
22497   enum attr_type type;
22498   if (!insn || !NONDEBUG_INSN_P (insn)
22499       || GET_CODE (PATTERN (insn)) == USE
22500       || GET_CODE (PATTERN (insn)) == CLOBBER)
22501     return false;
22502
22503   type = get_attr_type (insn);
22504   if (type == TYPE_IMUL
22505       || type == TYPE_IMUL2
22506       || type == TYPE_IMUL3
22507       || type == TYPE_LMUL
22508       || type == TYPE_IDIV
22509       || type == TYPE_LDIV
22510       || type == TYPE_SDIV
22511       || type == TYPE_DDIV
22512       || type == TYPE_SSQRT
22513       || type == TYPE_DSQRT
22514       || type == TYPE_MFCR
22515       || type == TYPE_MFCRF
22516       || type == TYPE_MFJMPR)
22517     {
22518       return true;
22519     }
22520   return false;
22521 }
22522
22523
22524 /* Return how many instructions the machine can issue per cycle.  */
22525
22526 static int
22527 rs6000_issue_rate (void)
22528 {
22529   /* Unless scheduling for register pressure, use issue rate of 1 for
22530      first scheduling pass to decrease degradation.  */
22531   if (!reload_completed && !flag_sched_pressure)
22532     return 1;
22533
22534   switch (rs6000_cpu_attr) {
22535   case CPU_RIOS1:  /* ? */
22536   case CPU_RS64A:
22537   case CPU_PPC601: /* ? */
22538   case CPU_PPC7450:
22539     return 3;
22540   case CPU_PPC440:
22541   case CPU_PPC603:
22542   case CPU_PPC750:
22543   case CPU_PPC7400:
22544   case CPU_PPC8540:
22545   case CPU_CELL:
22546   case CPU_PPCE300C2:
22547   case CPU_PPCE300C3:
22548   case CPU_PPCE500MC:
22549   case CPU_PPCE500MC64:
22550   case CPU_TITAN:
22551     return 2;
22552   case CPU_RIOS2:
22553   case CPU_PPC476:
22554   case CPU_PPC604:
22555   case CPU_PPC604E:
22556   case CPU_PPC620:
22557   case CPU_PPC630:
22558     return 4;
22559   case CPU_POWER4:
22560   case CPU_POWER5:
22561   case CPU_POWER6:
22562   case CPU_POWER7:
22563     return 5;
22564   default:
22565     return 1;
22566   }
22567 }
22568
22569 /* Return how many instructions to look ahead for better insn
22570    scheduling.  */
22571
22572 static int
22573 rs6000_use_sched_lookahead (void)
22574 {
22575   if (rs6000_cpu_attr == CPU_PPC8540)
22576     return 4;
22577   if (rs6000_cpu_attr == CPU_CELL)
22578     return (reload_completed ? 8 : 0);
22579   return 0;
22580 }
22581
22582 /* We are choosing insn from the ready queue.  Return nonzero if INSN can be chosen.  */
22583 static int
22584 rs6000_use_sched_lookahead_guard (rtx insn)
22585 {
22586   if (rs6000_cpu_attr != CPU_CELL)
22587     return 1;
22588
22589    if (insn == NULL_RTX || !INSN_P (insn))
22590      abort ();
22591
22592   if (!reload_completed
22593       || is_nonpipeline_insn (insn)
22594       || is_microcoded_insn (insn))
22595     return 0;
22596
22597   return 1;
22598 }
22599
22600 /* Determine is PAT refers to memory.  */
22601
22602 static bool
22603 is_mem_ref (rtx pat)
22604 {
22605   const char * fmt;
22606   int i, j;
22607   bool ret = false;
22608
22609   /* stack_tie does not produce any real memory traffic.  */
22610   if (GET_CODE (pat) == UNSPEC
22611       && XINT (pat, 1) == UNSPEC_TIE)
22612     return false;
22613
22614   if (GET_CODE (pat) == MEM)
22615     return true;
22616
22617   /* Recursively process the pattern.  */
22618   fmt = GET_RTX_FORMAT (GET_CODE (pat));
22619
22620   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0 && !ret; i--)
22621     {
22622       if (fmt[i] == 'e')
22623         ret |= is_mem_ref (XEXP (pat, i));
22624       else if (fmt[i] == 'E')
22625         for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
22626           ret |= is_mem_ref (XVECEXP (pat, i, j));
22627     }
22628
22629   return ret;
22630 }
22631
22632 /* Determine if PAT is a PATTERN of a load insn.  */
22633
22634 static bool
22635 is_load_insn1 (rtx pat)
22636 {
22637   if (!pat || pat == NULL_RTX)
22638     return false;
22639
22640   if (GET_CODE (pat) == SET)
22641     return is_mem_ref (SET_SRC (pat));
22642
22643   if (GET_CODE (pat) == PARALLEL)
22644     {
22645       int i;
22646
22647       for (i = 0; i < XVECLEN (pat, 0); i++)
22648         if (is_load_insn1 (XVECEXP (pat, 0, i)))
22649           return true;
22650     }
22651
22652   return false;
22653 }
22654
22655 /* Determine if INSN loads from memory.  */
22656
22657 static bool
22658 is_load_insn (rtx insn)
22659 {
22660   if (!insn || !INSN_P (insn))
22661     return false;
22662
22663   if (GET_CODE (insn) == CALL_INSN)
22664     return false;
22665
22666   return is_load_insn1 (PATTERN (insn));
22667 }
22668
22669 /* Determine if PAT is a PATTERN of a store insn.  */
22670
22671 static bool
22672 is_store_insn1 (rtx pat)
22673 {
22674   if (!pat || pat == NULL_RTX)
22675     return false;
22676
22677   if (GET_CODE (pat) == SET)
22678     return is_mem_ref (SET_DEST (pat));
22679
22680   if (GET_CODE (pat) == PARALLEL)
22681     {
22682       int i;
22683
22684       for (i = 0; i < XVECLEN (pat, 0); i++)
22685         if (is_store_insn1 (XVECEXP (pat, 0, i)))
22686           return true;
22687     }
22688
22689   return false;
22690 }
22691
22692 /* Determine if INSN stores to memory.  */
22693
22694 static bool
22695 is_store_insn (rtx insn)
22696 {
22697   if (!insn || !INSN_P (insn))
22698     return false;
22699
22700   return is_store_insn1 (PATTERN (insn));
22701 }
22702
22703 /* Return the dest of a store insn.  */
22704
22705 static rtx
22706 get_store_dest (rtx pat)
22707 {
22708   gcc_assert (is_store_insn1 (pat));
22709
22710   if (GET_CODE (pat) == SET)
22711     return SET_DEST (pat);
22712   else if (GET_CODE (pat) == PARALLEL)
22713     {
22714       int i;
22715
22716       for (i = 0; i < XVECLEN (pat, 0); i++)
22717         {
22718           rtx inner_pat = XVECEXP (pat, 0, i);
22719           if (GET_CODE (inner_pat) == SET
22720               && is_mem_ref (SET_DEST (inner_pat)))
22721             return inner_pat;
22722         }
22723     }
22724   /* We shouldn't get here, because we should have either a simple
22725      store insn or a store with update which are covered above.  */
22726   gcc_unreachable();
22727 }
22728
22729 /* Returns whether the dependence between INSN and NEXT is considered
22730    costly by the given target.  */
22731
22732 static bool
22733 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
22734 {
22735   rtx insn;
22736   rtx next;
22737
22738   /* If the flag is not enabled - no dependence is considered costly;
22739      allow all dependent insns in the same group.
22740      This is the most aggressive option.  */
22741   if (rs6000_sched_costly_dep == no_dep_costly)
22742     return false;
22743
22744   /* If the flag is set to 1 - a dependence is always considered costly;
22745      do not allow dependent instructions in the same group.
22746      This is the most conservative option.  */
22747   if (rs6000_sched_costly_dep == all_deps_costly)
22748     return true;
22749
22750   insn = DEP_PRO (dep);
22751   next = DEP_CON (dep);
22752
22753   if (rs6000_sched_costly_dep == store_to_load_dep_costly
22754       && is_load_insn (next)
22755       && is_store_insn (insn))
22756     /* Prevent load after store in the same group.  */
22757     return true;
22758
22759   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
22760       && is_load_insn (next)
22761       && is_store_insn (insn)
22762       && DEP_TYPE (dep) == REG_DEP_TRUE)
22763      /* Prevent load after store in the same group if it is a true
22764         dependence.  */
22765      return true;
22766
22767   /* The flag is set to X; dependences with latency >= X are considered costly,
22768      and will not be scheduled in the same group.  */
22769   if (rs6000_sched_costly_dep <= max_dep_latency
22770       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
22771     return true;
22772
22773   return false;
22774 }
22775
22776 /* Return the next insn after INSN that is found before TAIL is reached,
22777    skipping any "non-active" insns - insns that will not actually occupy
22778    an issue slot.  Return NULL_RTX if such an insn is not found.  */
22779
22780 static rtx
22781 get_next_active_insn (rtx insn, rtx tail)
22782 {
22783   if (insn == NULL_RTX || insn == tail)
22784     return NULL_RTX;
22785
22786   while (1)
22787     {
22788       insn = NEXT_INSN (insn);
22789       if (insn == NULL_RTX || insn == tail)
22790         return NULL_RTX;
22791
22792       if (CALL_P (insn)
22793           || JUMP_P (insn)
22794           || (NONJUMP_INSN_P (insn)
22795               && GET_CODE (PATTERN (insn)) != USE
22796               && GET_CODE (PATTERN (insn)) != CLOBBER
22797               && INSN_CODE (insn) != CODE_FOR_stack_tie))
22798         break;
22799     }
22800   return insn;
22801 }
22802
22803 /* We are about to begin issuing insns for this clock cycle. */
22804
22805 static int
22806 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
22807                         rtx *ready ATTRIBUTE_UNUSED,
22808                         int *pn_ready ATTRIBUTE_UNUSED,
22809                         int clock_var ATTRIBUTE_UNUSED)
22810 {
22811   int n_ready = *pn_ready;
22812
22813   if (sched_verbose)
22814     fprintf (dump, "// rs6000_sched_reorder :\n");
22815
22816   /* Reorder the ready list, if the second to last ready insn
22817      is a nonepipeline insn.  */
22818   if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
22819   {
22820     if (is_nonpipeline_insn (ready[n_ready - 1])
22821         && (recog_memoized (ready[n_ready - 2]) > 0))
22822       /* Simply swap first two insns.  */
22823       {
22824         rtx tmp = ready[n_ready - 1];
22825         ready[n_ready - 1] = ready[n_ready - 2];
22826         ready[n_ready - 2] = tmp;
22827       }
22828   }
22829
22830   if (rs6000_cpu == PROCESSOR_POWER6)
22831     load_store_pendulum = 0;
22832
22833   return rs6000_issue_rate ();
22834 }
22835
22836 /* Like rs6000_sched_reorder, but called after issuing each insn.  */
22837
22838 static int
22839 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
22840                          int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
22841 {
22842   if (sched_verbose)
22843     fprintf (dump, "// rs6000_sched_reorder2 :\n");
22844
22845   /* For Power6, we need to handle some special cases to try and keep the
22846      store queue from overflowing and triggering expensive flushes.
22847
22848      This code monitors how load and store instructions are being issued
22849      and skews the ready list one way or the other to increase the likelihood
22850      that a desired instruction is issued at the proper time.
22851
22852      A couple of things are done.  First, we maintain a "load_store_pendulum"
22853      to track the current state of load/store issue.
22854
22855        - If the pendulum is at zero, then no loads or stores have been
22856          issued in the current cycle so we do nothing.
22857
22858        - If the pendulum is 1, then a single load has been issued in this
22859          cycle and we attempt to locate another load in the ready list to
22860          issue with it.
22861
22862        - If the pendulum is -2, then two stores have already been
22863          issued in this cycle, so we increase the priority of the first load
22864          in the ready list to increase it's likelihood of being chosen first
22865          in the next cycle.
22866
22867        - If the pendulum is -1, then a single store has been issued in this
22868          cycle and we attempt to locate another store in the ready list to
22869          issue with it, preferring a store to an adjacent memory location to
22870          facilitate store pairing in the store queue.
22871
22872        - If the pendulum is 2, then two loads have already been
22873          issued in this cycle, so we increase the priority of the first store
22874          in the ready list to increase it's likelihood of being chosen first
22875          in the next cycle.
22876
22877        - If the pendulum < -2 or > 2, then do nothing.
22878
22879        Note: This code covers the most common scenarios.  There exist non
22880              load/store instructions which make use of the LSU and which
22881              would need to be accounted for to strictly model the behavior
22882              of the machine.  Those instructions are currently unaccounted
22883              for to help minimize compile time overhead of this code.
22884    */
22885   if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
22886     {
22887       int pos;
22888       int i;
22889       rtx tmp;
22890
22891       if (is_store_insn (last_scheduled_insn))
22892         /* Issuing a store, swing the load_store_pendulum to the left */
22893         load_store_pendulum--;
22894       else if (is_load_insn (last_scheduled_insn))
22895         /* Issuing a load, swing the load_store_pendulum to the right */
22896         load_store_pendulum++;
22897       else
22898         return cached_can_issue_more;
22899
22900       /* If the pendulum is balanced, or there is only one instruction on
22901          the ready list, then all is well, so return. */
22902       if ((load_store_pendulum == 0) || (*pn_ready <= 1))
22903         return cached_can_issue_more;
22904
22905       if (load_store_pendulum == 1)
22906         {
22907           /* A load has been issued in this cycle.  Scan the ready list
22908              for another load to issue with it */
22909           pos = *pn_ready-1;
22910
22911           while (pos >= 0)
22912             {
22913               if (is_load_insn (ready[pos]))
22914                 {
22915                   /* Found a load.  Move it to the head of the ready list,
22916                      and adjust it's priority so that it is more likely to
22917                      stay there */
22918                   tmp = ready[pos];
22919                   for (i=pos; i<*pn_ready-1; i++)
22920                     ready[i] = ready[i + 1];
22921                   ready[*pn_ready-1] = tmp;
22922
22923                   if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
22924                     INSN_PRIORITY (tmp)++;
22925                   break;
22926                 }
22927               pos--;
22928             }
22929         }
22930       else if (load_store_pendulum == -2)
22931         {
22932           /* Two stores have been issued in this cycle.  Increase the
22933              priority of the first load in the ready list to favor it for
22934              issuing in the next cycle. */
22935           pos = *pn_ready-1;
22936
22937           while (pos >= 0)
22938             {
22939               if (is_load_insn (ready[pos])
22940                   && !sel_sched_p ()
22941                   && INSN_PRIORITY_KNOWN (ready[pos]))
22942                 {
22943                   INSN_PRIORITY (ready[pos])++;
22944
22945                   /* Adjust the pendulum to account for the fact that a load
22946                      was found and increased in priority.  This is to prevent
22947                      increasing the priority of multiple loads */
22948                   load_store_pendulum--;
22949
22950                   break;
22951                 }
22952               pos--;
22953             }
22954         }
22955       else if (load_store_pendulum == -1)
22956         {
22957           /* A store has been issued in this cycle.  Scan the ready list for
22958              another store to issue with it, preferring a store to an adjacent
22959              memory location */
22960           int first_store_pos = -1;
22961
22962           pos = *pn_ready-1;
22963
22964           while (pos >= 0)
22965             {
22966               if (is_store_insn (ready[pos]))
22967                 {
22968                   /* Maintain the index of the first store found on the
22969                      list */
22970                   if (first_store_pos == -1)
22971                     first_store_pos = pos;
22972
22973                   if (is_store_insn (last_scheduled_insn)
22974                       && adjacent_mem_locations (last_scheduled_insn,ready[pos]))
22975                     {
22976                       /* Found an adjacent store.  Move it to the head of the
22977                          ready list, and adjust it's priority so that it is
22978                          more likely to stay there */
22979                       tmp = ready[pos];
22980                       for (i=pos; i<*pn_ready-1; i++)
22981                         ready[i] = ready[i + 1];
22982                       ready[*pn_ready-1] = tmp;
22983
22984                       if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
22985                         INSN_PRIORITY (tmp)++;
22986
22987                       first_store_pos = -1;
22988
22989                       break;
22990                     };
22991                 }
22992               pos--;
22993             }
22994
22995           if (first_store_pos >= 0)
22996             {
22997               /* An adjacent store wasn't found, but a non-adjacent store was,
22998                  so move the non-adjacent store to the front of the ready
22999                  list, and adjust its priority so that it is more likely to
23000                  stay there. */
23001               tmp = ready[first_store_pos];
23002               for (i=first_store_pos; i<*pn_ready-1; i++)
23003                 ready[i] = ready[i + 1];
23004               ready[*pn_ready-1] = tmp;
23005               if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23006                 INSN_PRIORITY (tmp)++;
23007             }
23008         }
23009       else if (load_store_pendulum == 2)
23010        {
23011            /* Two loads have been issued in this cycle.  Increase the priority
23012               of the first store in the ready list to favor it for issuing in
23013               the next cycle. */
23014           pos = *pn_ready-1;
23015
23016           while (pos >= 0)
23017             {
23018               if (is_store_insn (ready[pos])
23019                   && !sel_sched_p ()
23020                   && INSN_PRIORITY_KNOWN (ready[pos]))
23021                 {
23022                   INSN_PRIORITY (ready[pos])++;
23023
23024                   /* Adjust the pendulum to account for the fact that a store
23025                      was found and increased in priority.  This is to prevent
23026                      increasing the priority of multiple stores */
23027                   load_store_pendulum++;
23028
23029                   break;
23030                 }
23031               pos--;
23032             }
23033         }
23034     }
23035
23036   return cached_can_issue_more;
23037 }
23038
23039 /* Return whether the presence of INSN causes a dispatch group termination
23040    of group WHICH_GROUP.
23041
23042    If WHICH_GROUP == current_group, this function will return true if INSN
23043    causes the termination of the current group (i.e, the dispatch group to
23044    which INSN belongs). This means that INSN will be the last insn in the
23045    group it belongs to.
23046
23047    If WHICH_GROUP == previous_group, this function will return true if INSN
23048    causes the termination of the previous group (i.e, the dispatch group that
23049    precedes the group to which INSN belongs).  This means that INSN will be
23050    the first insn in the group it belongs to).  */
23051
23052 static bool
23053 insn_terminates_group_p (rtx insn, enum group_termination which_group)
23054 {
23055   bool first, last;
23056
23057   if (! insn)
23058     return false;
23059
23060   first = insn_must_be_first_in_group (insn);
23061   last = insn_must_be_last_in_group (insn);
23062
23063   if (first && last)
23064     return true;
23065
23066   if (which_group == current_group)
23067     return last;
23068   else if (which_group == previous_group)
23069     return first;
23070
23071   return false;
23072 }
23073
23074
23075 static bool
23076 insn_must_be_first_in_group (rtx insn)
23077 {
23078   enum attr_type type;
23079
23080   if (!insn
23081       || GET_CODE (insn) == NOTE
23082       || DEBUG_INSN_P (insn)
23083       || GET_CODE (PATTERN (insn)) == USE
23084       || GET_CODE (PATTERN (insn)) == CLOBBER)
23085     return false;
23086
23087   switch (rs6000_cpu)
23088     {
23089     case PROCESSOR_POWER5:
23090       if (is_cracked_insn (insn))
23091         return true;
23092     case PROCESSOR_POWER4:
23093       if (is_microcoded_insn (insn))
23094         return true;
23095
23096       if (!rs6000_sched_groups)
23097         return false;
23098
23099       type = get_attr_type (insn);
23100
23101       switch (type)
23102         {
23103         case TYPE_MFCR:
23104         case TYPE_MFCRF:
23105         case TYPE_MTCR:
23106         case TYPE_DELAYED_CR:
23107         case TYPE_CR_LOGICAL:
23108         case TYPE_MTJMPR:
23109         case TYPE_MFJMPR:
23110         case TYPE_IDIV:
23111         case TYPE_LDIV:
23112         case TYPE_LOAD_L:
23113         case TYPE_STORE_C:
23114         case TYPE_ISYNC:
23115         case TYPE_SYNC:
23116           return true;
23117         default:
23118           break;
23119         }
23120       break;
23121     case PROCESSOR_POWER6:
23122       type = get_attr_type (insn);
23123
23124       switch (type)
23125         {
23126         case TYPE_INSERT_DWORD:
23127         case TYPE_EXTS:
23128         case TYPE_CNTLZ:
23129         case TYPE_SHIFT:
23130         case TYPE_VAR_SHIFT_ROTATE:
23131         case TYPE_TRAP:
23132         case TYPE_IMUL:
23133         case TYPE_IMUL2:
23134         case TYPE_IMUL3:
23135         case TYPE_LMUL:
23136         case TYPE_IDIV:
23137         case TYPE_INSERT_WORD:
23138         case TYPE_DELAYED_COMPARE:
23139         case TYPE_IMUL_COMPARE:
23140         case TYPE_LMUL_COMPARE:
23141         case TYPE_FPCOMPARE:
23142         case TYPE_MFCR:
23143         case TYPE_MTCR:
23144         case TYPE_MFJMPR:
23145         case TYPE_MTJMPR:
23146         case TYPE_ISYNC:
23147         case TYPE_SYNC:
23148         case TYPE_LOAD_L:
23149         case TYPE_STORE_C:
23150         case TYPE_LOAD_U:
23151         case TYPE_LOAD_UX:
23152         case TYPE_LOAD_EXT_UX:
23153         case TYPE_STORE_U:
23154         case TYPE_STORE_UX:
23155         case TYPE_FPLOAD_U:
23156         case TYPE_FPLOAD_UX:
23157         case TYPE_FPSTORE_U:
23158         case TYPE_FPSTORE_UX:
23159           return true;
23160         default:
23161           break;
23162         }
23163       break;
23164     case PROCESSOR_POWER7:
23165       type = get_attr_type (insn);
23166
23167       switch (type)
23168         {
23169         case TYPE_CR_LOGICAL:
23170         case TYPE_MFCR:
23171         case TYPE_MFCRF:
23172         case TYPE_MTCR:
23173         case TYPE_IDIV:
23174         case TYPE_LDIV:
23175         case TYPE_COMPARE:
23176         case TYPE_DELAYED_COMPARE:
23177         case TYPE_VAR_DELAYED_COMPARE:
23178         case TYPE_ISYNC:
23179         case TYPE_LOAD_L:
23180         case TYPE_STORE_C:
23181         case TYPE_LOAD_U:
23182         case TYPE_LOAD_UX:
23183         case TYPE_LOAD_EXT:
23184         case TYPE_LOAD_EXT_U:
23185         case TYPE_LOAD_EXT_UX:
23186         case TYPE_STORE_U:
23187         case TYPE_STORE_UX:
23188         case TYPE_FPLOAD_U:
23189         case TYPE_FPLOAD_UX:
23190         case TYPE_FPSTORE_U:
23191         case TYPE_FPSTORE_UX:
23192         case TYPE_MFJMPR:
23193         case TYPE_MTJMPR:
23194           return true;
23195         default:
23196           break;
23197         }
23198       break;
23199     default:
23200       break;
23201     }
23202
23203   return false;
23204 }
23205
23206 static bool
23207 insn_must_be_last_in_group (rtx insn)
23208 {
23209   enum attr_type type;
23210
23211   if (!insn
23212       || GET_CODE (insn) == NOTE
23213       || DEBUG_INSN_P (insn)
23214       || GET_CODE (PATTERN (insn)) == USE
23215       || GET_CODE (PATTERN (insn)) == CLOBBER)
23216     return false;
23217
23218   switch (rs6000_cpu) {
23219   case PROCESSOR_POWER4:
23220   case PROCESSOR_POWER5:
23221     if (is_microcoded_insn (insn))
23222       return true;
23223
23224     if (is_branch_slot_insn (insn))
23225       return true;
23226
23227     break;
23228   case PROCESSOR_POWER6:
23229     type = get_attr_type (insn);
23230
23231     switch (type)
23232       {
23233       case TYPE_EXTS:
23234       case TYPE_CNTLZ:
23235       case TYPE_SHIFT:
23236       case TYPE_VAR_SHIFT_ROTATE:
23237       case TYPE_TRAP:
23238       case TYPE_IMUL:
23239       case TYPE_IMUL2:
23240       case TYPE_IMUL3:
23241       case TYPE_LMUL:
23242       case TYPE_IDIV:
23243       case TYPE_DELAYED_COMPARE:
23244       case TYPE_IMUL_COMPARE:
23245       case TYPE_LMUL_COMPARE:
23246       case TYPE_FPCOMPARE:
23247       case TYPE_MFCR:
23248       case TYPE_MTCR:
23249       case TYPE_MFJMPR:
23250       case TYPE_MTJMPR:
23251       case TYPE_ISYNC:
23252       case TYPE_SYNC:
23253       case TYPE_LOAD_L:
23254       case TYPE_STORE_C:
23255         return true;
23256       default:
23257         break;
23258     }
23259     break;
23260   case PROCESSOR_POWER7:
23261     type = get_attr_type (insn);
23262
23263     switch (type)
23264       {
23265       case TYPE_ISYNC:
23266       case TYPE_SYNC:
23267       case TYPE_LOAD_L:
23268       case TYPE_STORE_C:
23269       case TYPE_LOAD_EXT_U:
23270       case TYPE_LOAD_EXT_UX:
23271       case TYPE_STORE_UX:
23272         return true;
23273       default:
23274         break;
23275     }
23276     break;
23277   default:
23278     break;
23279   }
23280
23281   return false;
23282 }
23283
23284 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
23285    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
23286
23287 static bool
23288 is_costly_group (rtx *group_insns, rtx next_insn)
23289 {
23290   int i;
23291   int issue_rate = rs6000_issue_rate ();
23292
23293   for (i = 0; i < issue_rate; i++)
23294     {
23295       sd_iterator_def sd_it;
23296       dep_t dep;
23297       rtx insn = group_insns[i];
23298
23299       if (!insn)
23300         continue;
23301
23302       FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
23303         {
23304           rtx next = DEP_CON (dep);
23305
23306           if (next == next_insn
23307               && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
23308             return true;
23309         }
23310     }
23311
23312   return false;
23313 }
23314
23315 /* Utility of the function redefine_groups.
23316    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
23317    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
23318    to keep it "far" (in a separate group) from GROUP_INSNS, following
23319    one of the following schemes, depending on the value of the flag
23320    -minsert_sched_nops = X:
23321    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
23322        in order to force NEXT_INSN into a separate group.
23323    (2) X < sched_finish_regroup_exact: insert exactly X nops.
23324    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
23325    insertion (has a group just ended, how many vacant issue slots remain in the
23326    last group, and how many dispatch groups were encountered so far).  */
23327
23328 static int
23329 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
23330                  rtx next_insn, bool *group_end, int can_issue_more,
23331                  int *group_count)
23332 {
23333   rtx nop;
23334   bool force;
23335   int issue_rate = rs6000_issue_rate ();
23336   bool end = *group_end;
23337   int i;
23338
23339   if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
23340     return can_issue_more;
23341
23342   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
23343     return can_issue_more;
23344
23345   force = is_costly_group (group_insns, next_insn);
23346   if (!force)
23347     return can_issue_more;
23348
23349   if (sched_verbose > 6)
23350     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
23351              *group_count ,can_issue_more);
23352
23353   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
23354     {
23355       if (*group_end)
23356         can_issue_more = 0;
23357
23358       /* Since only a branch can be issued in the last issue_slot, it is
23359          sufficient to insert 'can_issue_more - 1' nops if next_insn is not
23360          a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
23361          in this case the last nop will start a new group and the branch
23362          will be forced to the new group.  */
23363       if (can_issue_more && !is_branch_slot_insn (next_insn))
23364         can_issue_more--;
23365
23366       while (can_issue_more > 0)
23367         {
23368           nop = gen_nop ();
23369           emit_insn_before (nop, next_insn);
23370           can_issue_more--;
23371         }
23372
23373       *group_end = true;
23374       return 0;
23375     }
23376
23377   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
23378     {
23379       int n_nops = rs6000_sched_insert_nops;
23380
23381       /* Nops can't be issued from the branch slot, so the effective
23382          issue_rate for nops is 'issue_rate - 1'.  */
23383       if (can_issue_more == 0)
23384         can_issue_more = issue_rate;
23385       can_issue_more--;
23386       if (can_issue_more == 0)
23387         {
23388           can_issue_more = issue_rate - 1;
23389           (*group_count)++;
23390           end = true;
23391           for (i = 0; i < issue_rate; i++)
23392             {
23393               group_insns[i] = 0;
23394             }
23395         }
23396
23397       while (n_nops > 0)
23398         {
23399           nop = gen_nop ();
23400           emit_insn_before (nop, next_insn);
23401           if (can_issue_more == issue_rate - 1) /* new group begins */
23402             end = false;
23403           can_issue_more--;
23404           if (can_issue_more == 0)
23405             {
23406               can_issue_more = issue_rate - 1;
23407               (*group_count)++;
23408               end = true;
23409               for (i = 0; i < issue_rate; i++)
23410                 {
23411                   group_insns[i] = 0;
23412                 }
23413             }
23414           n_nops--;
23415         }
23416
23417       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
23418       can_issue_more++;
23419
23420       /* Is next_insn going to start a new group?  */
23421       *group_end
23422         = (end
23423            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
23424            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
23425            || (can_issue_more < issue_rate &&
23426                insn_terminates_group_p (next_insn, previous_group)));
23427       if (*group_end && end)
23428         (*group_count)--;
23429
23430       if (sched_verbose > 6)
23431         fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
23432                  *group_count, can_issue_more);
23433       return can_issue_more;
23434     }
23435
23436   return can_issue_more;
23437 }
23438
23439 /* This function tries to synch the dispatch groups that the compiler "sees"
23440    with the dispatch groups that the processor dispatcher is expected to
23441    form in practice.  It tries to achieve this synchronization by forcing the
23442    estimated processor grouping on the compiler (as opposed to the function
23443    'pad_goups' which tries to force the scheduler's grouping on the processor).
23444
23445    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
23446    examines the (estimated) dispatch groups that will be formed by the processor
23447    dispatcher.  It marks these group boundaries to reflect the estimated
23448    processor grouping, overriding the grouping that the scheduler had marked.
23449    Depending on the value of the flag '-minsert-sched-nops' this function can
23450    force certain insns into separate groups or force a certain distance between
23451    them by inserting nops, for example, if there exists a "costly dependence"
23452    between the insns.
23453
23454    The function estimates the group boundaries that the processor will form as
23455    follows:  It keeps track of how many vacant issue slots are available after
23456    each insn.  A subsequent insn will start a new group if one of the following
23457    4 cases applies:
23458    - no more vacant issue slots remain in the current dispatch group.
23459    - only the last issue slot, which is the branch slot, is vacant, but the next
23460      insn is not a branch.
23461    - only the last 2 or less issue slots, including the branch slot, are vacant,
23462      which means that a cracked insn (which occupies two issue slots) can't be
23463      issued in this group.
23464    - less than 'issue_rate' slots are vacant, and the next insn always needs to
23465      start a new group.  */
23466
23467 static int
23468 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
23469 {
23470   rtx insn, next_insn;
23471   int issue_rate;
23472   int can_issue_more;
23473   int slot, i;
23474   bool group_end;
23475   int group_count = 0;
23476   rtx *group_insns;
23477
23478   /* Initialize.  */
23479   issue_rate = rs6000_issue_rate ();
23480   group_insns = XALLOCAVEC (rtx, issue_rate);
23481   for (i = 0; i < issue_rate; i++)
23482     {
23483       group_insns[i] = 0;
23484     }
23485   can_issue_more = issue_rate;
23486   slot = 0;
23487   insn = get_next_active_insn (prev_head_insn, tail);
23488   group_end = false;
23489
23490   while (insn != NULL_RTX)
23491     {
23492       slot = (issue_rate - can_issue_more);
23493       group_insns[slot] = insn;
23494       can_issue_more =
23495         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
23496       if (insn_terminates_group_p (insn, current_group))
23497         can_issue_more = 0;
23498
23499       next_insn = get_next_active_insn (insn, tail);
23500       if (next_insn == NULL_RTX)
23501         return group_count + 1;
23502
23503       /* Is next_insn going to start a new group?  */
23504       group_end
23505         = (can_issue_more == 0
23506            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
23507            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
23508            || (can_issue_more < issue_rate &&
23509                insn_terminates_group_p (next_insn, previous_group)));
23510
23511       can_issue_more = force_new_group (sched_verbose, dump, group_insns,
23512                                         next_insn, &group_end, can_issue_more,
23513                                         &group_count);
23514
23515       if (group_end)
23516         {
23517           group_count++;
23518           can_issue_more = 0;
23519           for (i = 0; i < issue_rate; i++)
23520             {
23521               group_insns[i] = 0;
23522             }
23523         }
23524
23525       if (GET_MODE (next_insn) == TImode && can_issue_more)
23526         PUT_MODE (next_insn, VOIDmode);
23527       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
23528         PUT_MODE (next_insn, TImode);
23529
23530       insn = next_insn;
23531       if (can_issue_more == 0)
23532         can_issue_more = issue_rate;
23533     } /* while */
23534
23535   return group_count;
23536 }
23537
23538 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
23539    dispatch group boundaries that the scheduler had marked.  Pad with nops
23540    any dispatch groups which have vacant issue slots, in order to force the
23541    scheduler's grouping on the processor dispatcher.  The function
23542    returns the number of dispatch groups found.  */
23543
23544 static int
23545 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
23546 {
23547   rtx insn, next_insn;
23548   rtx nop;
23549   int issue_rate;
23550   int can_issue_more;
23551   int group_end;
23552   int group_count = 0;
23553
23554   /* Initialize issue_rate.  */
23555   issue_rate = rs6000_issue_rate ();
23556   can_issue_more = issue_rate;
23557
23558   insn = get_next_active_insn (prev_head_insn, tail);
23559   next_insn = get_next_active_insn (insn, tail);
23560
23561   while (insn != NULL_RTX)
23562     {
23563       can_issue_more =
23564         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
23565
23566       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
23567
23568       if (next_insn == NULL_RTX)
23569         break;
23570
23571       if (group_end)
23572         {
23573           /* If the scheduler had marked group termination at this location
23574              (between insn and next_insn), and neither insn nor next_insn will
23575              force group termination, pad the group with nops to force group
23576              termination.  */
23577           if (can_issue_more
23578               && (rs6000_sched_insert_nops == sched_finish_pad_groups)
23579               && !insn_terminates_group_p (insn, current_group)
23580               && !insn_terminates_group_p (next_insn, previous_group))
23581             {
23582               if (!is_branch_slot_insn (next_insn))
23583                 can_issue_more--;
23584
23585               while (can_issue_more)
23586                 {
23587                   nop = gen_nop ();
23588                   emit_insn_before (nop, next_insn);
23589                   can_issue_more--;
23590                 }
23591             }
23592
23593           can_issue_more = issue_rate;
23594           group_count++;
23595         }
23596
23597       insn = next_insn;
23598       next_insn = get_next_active_insn (insn, tail);
23599     }
23600
23601   return group_count;
23602 }
23603
23604 /* We're beginning a new block.  Initialize data structures as necessary.  */
23605
23606 static void
23607 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
23608                      int sched_verbose ATTRIBUTE_UNUSED,
23609                      int max_ready ATTRIBUTE_UNUSED)
23610 {
23611   last_scheduled_insn = NULL_RTX;
23612   load_store_pendulum = 0;
23613 }
23614
23615 /* The following function is called at the end of scheduling BB.
23616    After reload, it inserts nops at insn group bundling.  */
23617
23618 static void
23619 rs6000_sched_finish (FILE *dump, int sched_verbose)
23620 {
23621   int n_groups;
23622
23623   if (sched_verbose)
23624     fprintf (dump, "=== Finishing schedule.\n");
23625
23626   if (reload_completed && rs6000_sched_groups)
23627     {
23628       /* Do not run sched_finish hook when selective scheduling enabled.  */
23629       if (sel_sched_p ())
23630         return;
23631
23632       if (rs6000_sched_insert_nops == sched_finish_none)
23633         return;
23634
23635       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
23636         n_groups = pad_groups (dump, sched_verbose,
23637                                current_sched_info->prev_head,
23638                                current_sched_info->next_tail);
23639       else
23640         n_groups = redefine_groups (dump, sched_verbose,
23641                                     current_sched_info->prev_head,
23642                                     current_sched_info->next_tail);
23643
23644       if (sched_verbose >= 6)
23645         {
23646           fprintf (dump, "ngroups = %d\n", n_groups);
23647           print_rtl (dump, current_sched_info->prev_head);
23648           fprintf (dump, "Done finish_sched\n");
23649         }
23650     }
23651 }
23652
23653 struct _rs6000_sched_context
23654 {
23655   short cached_can_issue_more;
23656   rtx last_scheduled_insn;
23657   int load_store_pendulum;
23658 };
23659
23660 typedef struct _rs6000_sched_context rs6000_sched_context_def;
23661 typedef rs6000_sched_context_def *rs6000_sched_context_t;
23662
23663 /* Allocate store for new scheduling context.  */
23664 static void *
23665 rs6000_alloc_sched_context (void)
23666 {
23667   return xmalloc (sizeof (rs6000_sched_context_def));
23668 }
23669
23670 /* If CLEAN_P is true then initializes _SC with clean data,
23671    and from the global context otherwise.  */
23672 static void
23673 rs6000_init_sched_context (void *_sc, bool clean_p)
23674 {
23675   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
23676
23677   if (clean_p)
23678     {
23679       sc->cached_can_issue_more = 0;
23680       sc->last_scheduled_insn = NULL_RTX;
23681       sc->load_store_pendulum = 0;
23682     }
23683   else
23684     {
23685       sc->cached_can_issue_more = cached_can_issue_more;
23686       sc->last_scheduled_insn = last_scheduled_insn;
23687       sc->load_store_pendulum = load_store_pendulum;
23688     }
23689 }
23690
23691 /* Sets the global scheduling context to the one pointed to by _SC.  */
23692 static void
23693 rs6000_set_sched_context (void *_sc)
23694 {
23695   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
23696
23697   gcc_assert (sc != NULL);
23698
23699   cached_can_issue_more = sc->cached_can_issue_more;
23700   last_scheduled_insn = sc->last_scheduled_insn;
23701   load_store_pendulum = sc->load_store_pendulum;
23702 }
23703
23704 /* Free _SC.  */
23705 static void
23706 rs6000_free_sched_context (void *_sc)
23707 {
23708   gcc_assert (_sc != NULL);
23709
23710   free (_sc);
23711 }
23712
23713 \f
23714 /* Length in units of the trampoline for entering a nested function.  */
23715
23716 int
23717 rs6000_trampoline_size (void)
23718 {
23719   int ret = 0;
23720
23721   switch (DEFAULT_ABI)
23722     {
23723     default:
23724       gcc_unreachable ();
23725
23726     case ABI_AIX:
23727       ret = (TARGET_32BIT) ? 12 : 24;
23728       break;
23729
23730     case ABI_DARWIN:
23731     case ABI_V4:
23732       ret = (TARGET_32BIT) ? 40 : 48;
23733       break;
23734     }
23735
23736   return ret;
23737 }
23738
23739 /* Emit RTL insns to initialize the variable parts of a trampoline.
23740    FNADDR is an RTX for the address of the function's pure code.
23741    CXT is an RTX for the static chain value for the function.  */
23742
23743 static void
23744 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
23745 {
23746   int regsize = (TARGET_32BIT) ? 4 : 8;
23747   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
23748   rtx ctx_reg = force_reg (Pmode, cxt);
23749   rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
23750
23751   switch (DEFAULT_ABI)
23752     {
23753     default:
23754       gcc_unreachable ();
23755
23756     /* Under AIX, just build the 3 word function descriptor */
23757     case ABI_AIX:
23758       {
23759         rtx fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
23760         rtx fn_reg = gen_reg_rtx (Pmode);
23761         rtx toc_reg = gen_reg_rtx (Pmode);
23762
23763   /* Macro to shorten the code expansions below.  */
23764 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
23765
23766         m_tramp = replace_equiv_address (m_tramp, addr);
23767
23768         emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
23769         emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
23770         emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
23771         emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
23772         emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
23773
23774 # undef MEM_PLUS
23775       }
23776       break;
23777
23778     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
23779     case ABI_DARWIN:
23780     case ABI_V4:
23781       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
23782                          LCT_NORMAL, VOIDmode, 4,
23783                          addr, Pmode,
23784                          GEN_INT (rs6000_trampoline_size ()), SImode,
23785                          fnaddr, Pmode,
23786                          ctx_reg, Pmode);
23787       break;
23788     }
23789 }
23790
23791 \f
23792 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
23793    identifier as an argument, so the front end shouldn't look it up.  */
23794
23795 static bool
23796 rs6000_attribute_takes_identifier_p (const_tree attr_id)
23797 {
23798   return is_attribute_p ("altivec", attr_id);
23799 }
23800
23801 /* Handle the "altivec" attribute.  The attribute may have
23802    arguments as follows:
23803
23804         __attribute__((altivec(vector__)))
23805         __attribute__((altivec(pixel__)))       (always followed by 'unsigned short')
23806         __attribute__((altivec(bool__)))        (always followed by 'unsigned')
23807
23808   and may appear more than once (e.g., 'vector bool char') in a
23809   given declaration.  */
23810
23811 static tree
23812 rs6000_handle_altivec_attribute (tree *node,
23813                                  tree name ATTRIBUTE_UNUSED,
23814                                  tree args,
23815                                  int flags ATTRIBUTE_UNUSED,
23816                                  bool *no_add_attrs)
23817 {
23818   tree type = *node, result = NULL_TREE;
23819   enum machine_mode mode;
23820   int unsigned_p;
23821   char altivec_type
23822     = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
23823         && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
23824        ? *IDENTIFIER_POINTER (TREE_VALUE (args))
23825        : '?');
23826
23827   while (POINTER_TYPE_P (type)
23828          || TREE_CODE (type) == FUNCTION_TYPE
23829          || TREE_CODE (type) == METHOD_TYPE
23830          || TREE_CODE (type) == ARRAY_TYPE)
23831     type = TREE_TYPE (type);
23832
23833   mode = TYPE_MODE (type);
23834
23835   /* Check for invalid AltiVec type qualifiers.  */
23836   if (type == long_double_type_node)
23837     error ("use of %<long double%> in AltiVec types is invalid");
23838   else if (type == boolean_type_node)
23839     error ("use of boolean types in AltiVec types is invalid");
23840   else if (TREE_CODE (type) == COMPLEX_TYPE)
23841     error ("use of %<complex%> in AltiVec types is invalid");
23842   else if (DECIMAL_FLOAT_MODE_P (mode))
23843     error ("use of decimal floating point types in AltiVec types is invalid");
23844   else if (!TARGET_VSX)
23845     {
23846       if (type == long_unsigned_type_node || type == long_integer_type_node)
23847         {
23848           if (TARGET_64BIT)
23849             error ("use of %<long%> in AltiVec types is invalid for "
23850                    "64-bit code without -mvsx");
23851           else if (rs6000_warn_altivec_long)
23852             warning (0, "use of %<long%> in AltiVec types is deprecated; "
23853                      "use %<int%>");
23854         }
23855       else if (type == long_long_unsigned_type_node
23856                || type == long_long_integer_type_node)
23857         error ("use of %<long long%> in AltiVec types is invalid without "
23858                "-mvsx");
23859       else if (type == double_type_node)
23860         error ("use of %<double%> in AltiVec types is invalid without -mvsx");
23861     }
23862
23863   switch (altivec_type)
23864     {
23865     case 'v':
23866       unsigned_p = TYPE_UNSIGNED (type);
23867       switch (mode)
23868         {
23869         case DImode:
23870           result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
23871           break;
23872         case SImode:
23873           result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
23874           break;
23875         case HImode:
23876           result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
23877           break;
23878         case QImode:
23879           result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
23880           break;
23881         case SFmode: result = V4SF_type_node; break;
23882         case DFmode: result = V2DF_type_node; break;
23883           /* If the user says 'vector int bool', we may be handed the 'bool'
23884              attribute _before_ the 'vector' attribute, and so select the
23885              proper type in the 'b' case below.  */
23886         case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
23887         case V2DImode: case V2DFmode:
23888           result = type;
23889         default: break;
23890         }
23891       break;
23892     case 'b':
23893       switch (mode)
23894         {
23895         case DImode: case V2DImode: result = bool_V2DI_type_node; break;
23896         case SImode: case V4SImode: result = bool_V4SI_type_node; break;
23897         case HImode: case V8HImode: result = bool_V8HI_type_node; break;
23898         case QImode: case V16QImode: result = bool_V16QI_type_node;
23899         default: break;
23900         }
23901       break;
23902     case 'p':
23903       switch (mode)
23904         {
23905         case V8HImode: result = pixel_V8HI_type_node;
23906         default: break;
23907         }
23908     default: break;
23909     }
23910
23911   /* Propagate qualifiers attached to the element type
23912      onto the vector type.  */
23913   if (result && result != type && TYPE_QUALS (type))
23914     result = build_qualified_type (result, TYPE_QUALS (type));
23915
23916   *no_add_attrs = true;  /* No need to hang on to the attribute.  */
23917
23918   if (result)
23919     *node = lang_hooks.types.reconstruct_complex_type (*node, result);
23920
23921   return NULL_TREE;
23922 }
23923
23924 /* AltiVec defines four built-in scalar types that serve as vector
23925    elements; we must teach the compiler how to mangle them.  */
23926
23927 static const char *
23928 rs6000_mangle_type (const_tree type)
23929 {
23930   type = TYPE_MAIN_VARIANT (type);
23931
23932   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
23933       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
23934     return NULL;
23935
23936   if (type == bool_char_type_node) return "U6__boolc";
23937   if (type == bool_short_type_node) return "U6__bools";
23938   if (type == pixel_type_node) return "u7__pixel";
23939   if (type == bool_int_type_node) return "U6__booli";
23940   if (type == bool_long_type_node) return "U6__booll";
23941
23942   /* Mangle IBM extended float long double as `g' (__float128) on
23943      powerpc*-linux where long-double-64 previously was the default.  */
23944   if (TYPE_MAIN_VARIANT (type) == long_double_type_node
23945       && TARGET_ELF
23946       && TARGET_LONG_DOUBLE_128
23947       && !TARGET_IEEEQUAD)
23948     return "g";
23949
23950   /* For all other types, use normal C++ mangling.  */
23951   return NULL;
23952 }
23953
23954 /* Handle a "longcall" or "shortcall" attribute; arguments as in
23955    struct attribute_spec.handler.  */
23956
23957 static tree
23958 rs6000_handle_longcall_attribute (tree *node, tree name,
23959                                   tree args ATTRIBUTE_UNUSED,
23960                                   int flags ATTRIBUTE_UNUSED,
23961                                   bool *no_add_attrs)
23962 {
23963   if (TREE_CODE (*node) != FUNCTION_TYPE
23964       && TREE_CODE (*node) != FIELD_DECL
23965       && TREE_CODE (*node) != TYPE_DECL)
23966     {
23967       warning (OPT_Wattributes, "%qE attribute only applies to functions",
23968                name);
23969       *no_add_attrs = true;
23970     }
23971
23972   return NULL_TREE;
23973 }
23974
23975 /* Set longcall attributes on all functions declared when
23976    rs6000_default_long_calls is true.  */
23977 static void
23978 rs6000_set_default_type_attributes (tree type)
23979 {
23980   if (rs6000_default_long_calls
23981       && (TREE_CODE (type) == FUNCTION_TYPE
23982           || TREE_CODE (type) == METHOD_TYPE))
23983     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
23984                                         NULL_TREE,
23985                                         TYPE_ATTRIBUTES (type));
23986
23987 #if TARGET_MACHO
23988   darwin_set_default_type_attributes (type);
23989 #endif
23990 }
23991
23992 /* Return a reference suitable for calling a function with the
23993    longcall attribute.  */
23994
23995 rtx
23996 rs6000_longcall_ref (rtx call_ref)
23997 {
23998   const char *call_name;
23999   tree node;
24000
24001   if (GET_CODE (call_ref) != SYMBOL_REF)
24002     return call_ref;
24003
24004   /* System V adds '.' to the internal name, so skip them.  */
24005   call_name = XSTR (call_ref, 0);
24006   if (*call_name == '.')
24007     {
24008       while (*call_name == '.')
24009         call_name++;
24010
24011       node = get_identifier (call_name);
24012       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
24013     }
24014
24015   return force_reg (Pmode, call_ref);
24016 }
24017 \f
24018 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
24019 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
24020 #endif
24021
24022 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
24023    struct attribute_spec.handler.  */
24024 static tree
24025 rs6000_handle_struct_attribute (tree *node, tree name,
24026                                 tree args ATTRIBUTE_UNUSED,
24027                                 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
24028 {
24029   tree *type = NULL;
24030   if (DECL_P (*node))
24031     {
24032       if (TREE_CODE (*node) == TYPE_DECL)
24033         type = &TREE_TYPE (*node);
24034     }
24035   else
24036     type = node;
24037
24038   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
24039                  || TREE_CODE (*type) == UNION_TYPE)))
24040     {
24041       warning (OPT_Wattributes, "%qE attribute ignored", name);
24042       *no_add_attrs = true;
24043     }
24044
24045   else if ((is_attribute_p ("ms_struct", name)
24046             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
24047            || ((is_attribute_p ("gcc_struct", name)
24048                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
24049     {
24050       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
24051                name);
24052       *no_add_attrs = true;
24053     }
24054
24055   return NULL_TREE;
24056 }
24057
24058 static bool
24059 rs6000_ms_bitfield_layout_p (const_tree record_type)
24060 {
24061   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
24062           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
24063     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
24064 }
24065 \f
24066 #ifdef USING_ELFOS_H
24067
24068 /* A get_unnamed_section callback, used for switching to toc_section.  */
24069
24070 static void
24071 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
24072 {
24073   if (DEFAULT_ABI == ABI_AIX
24074       && TARGET_MINIMAL_TOC
24075       && !TARGET_RELOCATABLE)
24076     {
24077       if (!toc_initialized)
24078         {
24079           toc_initialized = 1;
24080           fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24081           (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
24082           fprintf (asm_out_file, "\t.tc ");
24083           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
24084           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24085           fprintf (asm_out_file, "\n");
24086
24087           fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24088           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24089           fprintf (asm_out_file, " = .+32768\n");
24090         }
24091       else
24092         fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24093     }
24094   else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
24095     fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24096   else
24097     {
24098       fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24099       if (!toc_initialized)
24100         {
24101           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24102           fprintf (asm_out_file, " = .+32768\n");
24103           toc_initialized = 1;
24104         }
24105     }
24106 }
24107
24108 /* Implement TARGET_ASM_INIT_SECTIONS.  */
24109
24110 static void
24111 rs6000_elf_asm_init_sections (void)
24112 {
24113   toc_section
24114     = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
24115
24116   sdata2_section
24117     = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
24118                            SDATA2_SECTION_ASM_OP);
24119 }
24120
24121 /* Implement TARGET_SELECT_RTX_SECTION.  */
24122
24123 static section *
24124 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
24125                                unsigned HOST_WIDE_INT align)
24126 {
24127   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
24128     return toc_section;
24129   else
24130     return default_elf_select_rtx_section (mode, x, align);
24131 }
24132 \f
24133 /* For a SYMBOL_REF, set generic flags and then perform some
24134    target-specific processing.
24135
24136    When the AIX ABI is requested on a non-AIX system, replace the
24137    function name with the real name (with a leading .) rather than the
24138    function descriptor name.  This saves a lot of overriding code to
24139    read the prefixes.  */
24140
24141 static void
24142 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
24143 {
24144   default_encode_section_info (decl, rtl, first);
24145
24146   if (first
24147       && TREE_CODE (decl) == FUNCTION_DECL
24148       && !TARGET_AIX
24149       && DEFAULT_ABI == ABI_AIX)
24150     {
24151       rtx sym_ref = XEXP (rtl, 0);
24152       size_t len = strlen (XSTR (sym_ref, 0));
24153       char *str = XALLOCAVEC (char, len + 2);
24154       str[0] = '.';
24155       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
24156       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
24157     }
24158 }
24159
24160 static inline bool
24161 compare_section_name (const char *section, const char *templ)
24162 {
24163   int len;
24164
24165   len = strlen (templ);
24166   return (strncmp (section, templ, len) == 0
24167           && (section[len] == 0 || section[len] == '.'));
24168 }
24169
24170 bool
24171 rs6000_elf_in_small_data_p (const_tree decl)
24172 {
24173   if (rs6000_sdata == SDATA_NONE)
24174     return false;
24175
24176   /* We want to merge strings, so we never consider them small data.  */
24177   if (TREE_CODE (decl) == STRING_CST)
24178     return false;
24179
24180   /* Functions are never in the small data area.  */
24181   if (TREE_CODE (decl) == FUNCTION_DECL)
24182     return false;
24183
24184   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
24185     {
24186       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
24187       if (compare_section_name (section, ".sdata")
24188           || compare_section_name (section, ".sdata2")
24189           || compare_section_name (section, ".gnu.linkonce.s")
24190           || compare_section_name (section, ".sbss")
24191           || compare_section_name (section, ".sbss2")
24192           || compare_section_name (section, ".gnu.linkonce.sb")
24193           || strcmp (section, ".PPC.EMB.sdata0") == 0
24194           || strcmp (section, ".PPC.EMB.sbss0") == 0)
24195         return true;
24196     }
24197   else
24198     {
24199       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
24200
24201       if (size > 0
24202           && (unsigned HOST_WIDE_INT) size <= g_switch_value
24203           /* If it's not public, and we're not going to reference it there,
24204              there's no need to put it in the small data section.  */
24205           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
24206         return true;
24207     }
24208
24209   return false;
24210 }
24211
24212 #endif /* USING_ELFOS_H */
24213 \f
24214 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  */
24215
24216 static bool
24217 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
24218 {
24219   return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
24220 }
24221 \f
24222 /* Return a REG that occurs in ADDR with coefficient 1.
24223    ADDR can be effectively incremented by incrementing REG.
24224
24225    r0 is special and we must not select it as an address
24226    register by this routine since our caller will try to
24227    increment the returned register via an "la" instruction.  */
24228
24229 rtx
24230 find_addr_reg (rtx addr)
24231 {
24232   while (GET_CODE (addr) == PLUS)
24233     {
24234       if (GET_CODE (XEXP (addr, 0)) == REG
24235           && REGNO (XEXP (addr, 0)) != 0)
24236         addr = XEXP (addr, 0);
24237       else if (GET_CODE (XEXP (addr, 1)) == REG
24238                && REGNO (XEXP (addr, 1)) != 0)
24239         addr = XEXP (addr, 1);
24240       else if (CONSTANT_P (XEXP (addr, 0)))
24241         addr = XEXP (addr, 1);
24242       else if (CONSTANT_P (XEXP (addr, 1)))
24243         addr = XEXP (addr, 0);
24244       else
24245         gcc_unreachable ();
24246     }
24247   gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
24248   return addr;
24249 }
24250
24251 void
24252 rs6000_fatal_bad_address (rtx op)
24253 {
24254   fatal_insn ("bad address", op);
24255 }
24256
24257 #if TARGET_MACHO
24258
24259 static tree branch_island_list = 0;
24260
24261 /* Remember to generate a branch island for far calls to the given
24262    function.  */
24263
24264 static void
24265 add_compiler_branch_island (tree label_name, tree function_name,
24266                             int line_number)
24267 {
24268   tree branch_island = build_tree_list (function_name, label_name);
24269   TREE_TYPE (branch_island) = build_int_cst (NULL_TREE, line_number);
24270   TREE_CHAIN (branch_island) = branch_island_list;
24271   branch_island_list = branch_island;
24272 }
24273
24274 #define BRANCH_ISLAND_LABEL_NAME(BRANCH_ISLAND)     TREE_VALUE (BRANCH_ISLAND)
24275 #define BRANCH_ISLAND_FUNCTION_NAME(BRANCH_ISLAND)  TREE_PURPOSE (BRANCH_ISLAND)
24276 #define BRANCH_ISLAND_LINE_NUMBER(BRANCH_ISLAND)    \
24277                 TREE_INT_CST_LOW (TREE_TYPE (BRANCH_ISLAND))
24278
24279 /* Generate far-jump branch islands for everything on the
24280    branch_island_list.  Invoked immediately after the last instruction
24281    of the epilogue has been emitted; the branch-islands must be
24282    appended to, and contiguous with, the function body.  Mach-O stubs
24283    are generated in machopic_output_stub().  */
24284
24285 static void
24286 macho_branch_islands (void)
24287 {
24288   char tmp_buf[512];
24289   tree branch_island;
24290
24291   for (branch_island = branch_island_list;
24292        branch_island;
24293        branch_island = TREE_CHAIN (branch_island))
24294     {
24295       const char *label =
24296         IDENTIFIER_POINTER (BRANCH_ISLAND_LABEL_NAME (branch_island));
24297       const char *name  =
24298         IDENTIFIER_POINTER (BRANCH_ISLAND_FUNCTION_NAME (branch_island));
24299       char name_buf[512];
24300       /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF().  */
24301       if (name[0] == '*' || name[0] == '&')
24302         strcpy (name_buf, name+1);
24303       else
24304         {
24305           name_buf[0] = '_';
24306           strcpy (name_buf+1, name);
24307         }
24308       strcpy (tmp_buf, "\n");
24309       strcat (tmp_buf, label);
24310 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24311       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24312         dbxout_stabd (N_SLINE, BRANCH_ISLAND_LINE_NUMBER (branch_island));
24313 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24314       if (flag_pic)
24315         {
24316           strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
24317           strcat (tmp_buf, label);
24318           strcat (tmp_buf, "_pic\n");
24319           strcat (tmp_buf, label);
24320           strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24321
24322           strcat (tmp_buf, "\taddis r11,r11,ha16(");
24323           strcat (tmp_buf, name_buf);
24324           strcat (tmp_buf, " - ");
24325           strcat (tmp_buf, label);
24326           strcat (tmp_buf, "_pic)\n");
24327
24328           strcat (tmp_buf, "\tmtlr r0\n");
24329
24330           strcat (tmp_buf, "\taddi r12,r11,lo16(");
24331           strcat (tmp_buf, name_buf);
24332           strcat (tmp_buf, " - ");
24333           strcat (tmp_buf, label);
24334           strcat (tmp_buf, "_pic)\n");
24335
24336           strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
24337         }
24338       else
24339         {
24340           strcat (tmp_buf, ":\nlis r12,hi16(");
24341           strcat (tmp_buf, name_buf);
24342           strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
24343           strcat (tmp_buf, name_buf);
24344           strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
24345         }
24346       output_asm_insn (tmp_buf, 0);
24347 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24348       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24349         dbxout_stabd (N_SLINE, BRANCH_ISLAND_LINE_NUMBER (branch_island));
24350 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24351     }
24352
24353   branch_island_list = 0;
24354 }
24355
24356 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
24357    already there or not.  */
24358
24359 static int
24360 no_previous_def (tree function_name)
24361 {
24362   tree branch_island;
24363   for (branch_island = branch_island_list;
24364        branch_island;
24365        branch_island = TREE_CHAIN (branch_island))
24366     if (function_name == BRANCH_ISLAND_FUNCTION_NAME (branch_island))
24367       return 0;
24368   return 1;
24369 }
24370
24371 /* GET_PREV_LABEL gets the label name from the previous definition of
24372    the function.  */
24373
24374 static tree
24375 get_prev_label (tree function_name)
24376 {
24377   tree branch_island;
24378   for (branch_island = branch_island_list;
24379        branch_island;
24380        branch_island = TREE_CHAIN (branch_island))
24381     if (function_name == BRANCH_ISLAND_FUNCTION_NAME (branch_island))
24382       return BRANCH_ISLAND_LABEL_NAME (branch_island);
24383   return 0;
24384 }
24385
24386 #ifndef DARWIN_LINKER_GENERATES_ISLANDS
24387 #define DARWIN_LINKER_GENERATES_ISLANDS 0
24388 #endif
24389
24390 /* KEXTs still need branch islands.  */
24391 #define DARWIN_GENERATE_ISLANDS (!DARWIN_LINKER_GENERATES_ISLANDS \
24392                                  || flag_mkernel || flag_apple_kext)
24393
24394 /* INSN is either a function call or a millicode call.  It may have an
24395    unconditional jump in its delay slot.
24396
24397    CALL_DEST is the routine we are calling.  */
24398
24399 char *
24400 output_call (rtx insn, rtx *operands, int dest_operand_number,
24401              int cookie_operand_number)
24402 {
24403   static char buf[256];
24404   if (DARWIN_GENERATE_ISLANDS
24405       && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
24406       && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
24407     {
24408       tree labelname;
24409       tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
24410
24411       if (no_previous_def (funname))
24412         {
24413           rtx label_rtx = gen_label_rtx ();
24414           char *label_buf, temp_buf[256];
24415           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
24416                                        CODE_LABEL_NUMBER (label_rtx));
24417           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
24418           labelname = get_identifier (label_buf);
24419           add_compiler_branch_island (labelname, funname, insn_line (insn));
24420         }
24421       else
24422         labelname = get_prev_label (funname);
24423
24424       /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
24425          instruction will reach 'foo', otherwise link as 'bl L42'".
24426          "L42" should be a 'branch island', that will do a far jump to
24427          'foo'.  Branch islands are generated in
24428          macho_branch_islands().  */
24429       sprintf (buf, "jbsr %%z%d,%.246s",
24430                dest_operand_number, IDENTIFIER_POINTER (labelname));
24431     }
24432   else
24433     sprintf (buf, "bl %%z%d", dest_operand_number);
24434   return buf;
24435 }
24436
24437 /* Generate PIC and indirect symbol stubs.  */
24438
24439 void
24440 machopic_output_stub (FILE *file, const char *symb, const char *stub)
24441 {
24442   unsigned int length;
24443   char *symbol_name, *lazy_ptr_name;
24444   char *local_label_0;
24445   static int label = 0;
24446
24447   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
24448   symb = (*targetm.strip_name_encoding) (symb);
24449
24450
24451   length = strlen (symb);
24452   symbol_name = XALLOCAVEC (char, length + 32);
24453   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
24454
24455   lazy_ptr_name = XALLOCAVEC (char, length + 32);
24456   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
24457
24458   if (flag_pic == 2)
24459     switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
24460   else
24461     switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
24462
24463   if (flag_pic == 2)
24464     {
24465       fprintf (file, "\t.align 5\n");
24466
24467       fprintf (file, "%s:\n", stub);
24468       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
24469
24470       label++;
24471       local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
24472       sprintf (local_label_0, "\"L%011d$spb\"", label);
24473
24474       fprintf (file, "\tmflr r0\n");
24475       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
24476       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
24477       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
24478                lazy_ptr_name, local_label_0);
24479       fprintf (file, "\tmtlr r0\n");
24480       fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
24481                (TARGET_64BIT ? "ldu" : "lwzu"),
24482                lazy_ptr_name, local_label_0);
24483       fprintf (file, "\tmtctr r12\n");
24484       fprintf (file, "\tbctr\n");
24485     }
24486   else
24487     {
24488       fprintf (file, "\t.align 4\n");
24489
24490       fprintf (file, "%s:\n", stub);
24491       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
24492
24493       fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
24494       fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
24495                (TARGET_64BIT ? "ldu" : "lwzu"),
24496                lazy_ptr_name);
24497       fprintf (file, "\tmtctr r12\n");
24498       fprintf (file, "\tbctr\n");
24499     }
24500
24501   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
24502   fprintf (file, "%s:\n", lazy_ptr_name);
24503   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
24504   fprintf (file, "%sdyld_stub_binding_helper\n",
24505            (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
24506 }
24507
24508 /* Legitimize PIC addresses.  If the address is already
24509    position-independent, we return ORIG.  Newly generated
24510    position-independent addresses go into a reg.  This is REG if non
24511    zero, otherwise we allocate register(s) as necessary.  */
24512
24513 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
24514
24515 rtx
24516 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
24517                                         rtx reg)
24518 {
24519   rtx base, offset;
24520
24521   if (reg == NULL && ! reload_in_progress && ! reload_completed)
24522     reg = gen_reg_rtx (Pmode);
24523
24524   if (GET_CODE (orig) == CONST)
24525     {
24526       rtx reg_temp;
24527
24528       if (GET_CODE (XEXP (orig, 0)) == PLUS
24529           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
24530         return orig;
24531
24532       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
24533
24534       /* Use a different reg for the intermediate value, as
24535          it will be marked UNCHANGING.  */
24536       reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
24537       base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
24538                                                      Pmode, reg_temp);
24539       offset =
24540         rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
24541                                                 Pmode, reg);
24542
24543       if (GET_CODE (offset) == CONST_INT)
24544         {
24545           if (SMALL_INT (offset))
24546             return plus_constant (base, INTVAL (offset));
24547           else if (! reload_in_progress && ! reload_completed)
24548             offset = force_reg (Pmode, offset);
24549           else
24550             {
24551               rtx mem = force_const_mem (Pmode, orig);
24552               return machopic_legitimize_pic_address (mem, Pmode, reg);
24553             }
24554         }
24555       return gen_rtx_PLUS (Pmode, base, offset);
24556     }
24557
24558   /* Fall back on generic machopic code.  */
24559   return machopic_legitimize_pic_address (orig, mode, reg);
24560 }
24561
24562 /* Output a .machine directive for the Darwin assembler, and call
24563    the generic start_file routine.  */
24564
24565 static void
24566 rs6000_darwin_file_start (void)
24567 {
24568   static const struct
24569   {
24570     const char *arg;
24571     const char *name;
24572     int if_set;
24573   } mapping[] = {
24574     { "ppc64", "ppc64", MASK_64BIT },
24575     { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
24576     { "power4", "ppc970", 0 },
24577     { "G5", "ppc970", 0 },
24578     { "7450", "ppc7450", 0 },
24579     { "7400", "ppc7400", MASK_ALTIVEC },
24580     { "G4", "ppc7400", 0 },
24581     { "750", "ppc750", 0 },
24582     { "740", "ppc750", 0 },
24583     { "G3", "ppc750", 0 },
24584     { "604e", "ppc604e", 0 },
24585     { "604", "ppc604", 0 },
24586     { "603e", "ppc603", 0 },
24587     { "603", "ppc603", 0 },
24588     { "601", "ppc601", 0 },
24589     { NULL, "ppc", 0 } };
24590   const char *cpu_id = "";
24591   size_t i;
24592
24593   rs6000_file_start ();
24594   darwin_file_start ();
24595
24596   /* Determine the argument to -mcpu=.  Default to G3 if not specified.  */
24597   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
24598     if (rs6000_select[i].set_arch_p && rs6000_select[i].string
24599         && rs6000_select[i].string[0] != '\0')
24600       cpu_id = rs6000_select[i].string;
24601
24602   /* Look through the mapping array.  Pick the first name that either
24603      matches the argument, has a bit set in IF_SET that is also set
24604      in the target flags, or has a NULL name.  */
24605
24606   i = 0;
24607   while (mapping[i].arg != NULL
24608          && strcmp (mapping[i].arg, cpu_id) != 0
24609          && (mapping[i].if_set & target_flags) == 0)
24610     i++;
24611
24612   fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
24613 }
24614
24615 #endif /* TARGET_MACHO */
24616
24617 #if TARGET_ELF
24618 static int
24619 rs6000_elf_reloc_rw_mask (void)
24620 {
24621   if (flag_pic)
24622     return 3;
24623   else if (DEFAULT_ABI == ABI_AIX)
24624     return 2;
24625   else
24626     return 0;
24627 }
24628
24629 /* Record an element in the table of global constructors.  SYMBOL is
24630    a SYMBOL_REF of the function to be called; PRIORITY is a number
24631    between 0 and MAX_INIT_PRIORITY.
24632
24633    This differs from default_named_section_asm_out_constructor in
24634    that we have special handling for -mrelocatable.  */
24635
24636 static void
24637 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
24638 {
24639   const char *section = ".ctors";
24640   char buf[16];
24641
24642   if (priority != DEFAULT_INIT_PRIORITY)
24643     {
24644       sprintf (buf, ".ctors.%.5u",
24645                /* Invert the numbering so the linker puts us in the proper
24646                   order; constructors are run from right to left, and the
24647                   linker sorts in increasing order.  */
24648                MAX_INIT_PRIORITY - priority);
24649       section = buf;
24650     }
24651
24652   switch_to_section (get_section (section, SECTION_WRITE, NULL));
24653   assemble_align (POINTER_SIZE);
24654
24655   if (TARGET_RELOCATABLE)
24656     {
24657       fputs ("\t.long (", asm_out_file);
24658       output_addr_const (asm_out_file, symbol);
24659       fputs (")@fixup\n", asm_out_file);
24660     }
24661   else
24662     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
24663 }
24664
24665 static void
24666 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
24667 {
24668   const char *section = ".dtors";
24669   char buf[16];
24670
24671   if (priority != DEFAULT_INIT_PRIORITY)
24672     {
24673       sprintf (buf, ".dtors.%.5u",
24674                /* Invert the numbering so the linker puts us in the proper
24675                   order; constructors are run from right to left, and the
24676                   linker sorts in increasing order.  */
24677                MAX_INIT_PRIORITY - priority);
24678       section = buf;
24679     }
24680
24681   switch_to_section (get_section (section, SECTION_WRITE, NULL));
24682   assemble_align (POINTER_SIZE);
24683
24684   if (TARGET_RELOCATABLE)
24685     {
24686       fputs ("\t.long (", asm_out_file);
24687       output_addr_const (asm_out_file, symbol);
24688       fputs (")@fixup\n", asm_out_file);
24689     }
24690   else
24691     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
24692 }
24693
24694 void
24695 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
24696 {
24697   if (TARGET_64BIT)
24698     {
24699       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
24700       ASM_OUTPUT_LABEL (file, name);
24701       fputs (DOUBLE_INT_ASM_OP, file);
24702       rs6000_output_function_entry (file, name);
24703       fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
24704       if (DOT_SYMBOLS)
24705         {
24706           fputs ("\t.size\t", file);
24707           assemble_name (file, name);
24708           fputs (",24\n\t.type\t.", file);
24709           assemble_name (file, name);
24710           fputs (",@function\n", file);
24711           if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
24712             {
24713               fputs ("\t.globl\t.", file);
24714               assemble_name (file, name);
24715               putc ('\n', file);
24716             }
24717         }
24718       else
24719         ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
24720       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
24721       rs6000_output_function_entry (file, name);
24722       fputs (":\n", file);
24723       return;
24724     }
24725
24726   if (TARGET_RELOCATABLE
24727       && !TARGET_SECURE_PLT
24728       && (get_pool_size () != 0 || crtl->profile)
24729       && uses_TOC ())
24730     {
24731       char buf[256];
24732
24733       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
24734
24735       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
24736       fprintf (file, "\t.long ");
24737       assemble_name (file, buf);
24738       putc ('-', file);
24739       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
24740       assemble_name (file, buf);
24741       putc ('\n', file);
24742     }
24743
24744   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
24745   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
24746
24747   if (DEFAULT_ABI == ABI_AIX)
24748     {
24749       const char *desc_name, *orig_name;
24750
24751       orig_name = (*targetm.strip_name_encoding) (name);
24752       desc_name = orig_name;
24753       while (*desc_name == '.')
24754         desc_name++;
24755
24756       if (TREE_PUBLIC (decl))
24757         fprintf (file, "\t.globl %s\n", desc_name);
24758
24759       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24760       fprintf (file, "%s:\n", desc_name);
24761       fprintf (file, "\t.long %s\n", orig_name);
24762       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
24763       if (DEFAULT_ABI == ABI_AIX)
24764         fputs ("\t.long 0\n", file);
24765       fprintf (file, "\t.previous\n");
24766     }
24767   ASM_OUTPUT_LABEL (file, name);
24768 }
24769
24770 static void
24771 rs6000_elf_end_indicate_exec_stack (void)
24772 {
24773   if (TARGET_32BIT)
24774     file_end_indicate_exec_stack ();
24775 }
24776 #endif
24777
24778 #if TARGET_XCOFF
24779 static void
24780 rs6000_xcoff_asm_output_anchor (rtx symbol)
24781 {
24782   char buffer[100];
24783
24784   sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
24785            SYMBOL_REF_BLOCK_OFFSET (symbol));
24786   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
24787 }
24788
24789 static void
24790 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
24791 {
24792   fputs (GLOBAL_ASM_OP, stream);
24793   RS6000_OUTPUT_BASENAME (stream, name);
24794   putc ('\n', stream);
24795 }
24796
24797 /* A get_unnamed_decl callback, used for read-only sections.  PTR
24798    points to the section string variable.  */
24799
24800 static void
24801 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
24802 {
24803   fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
24804            *(const char *const *) directive,
24805            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
24806 }
24807
24808 /* Likewise for read-write sections.  */
24809
24810 static void
24811 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
24812 {
24813   fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
24814            *(const char *const *) directive,
24815            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
24816 }
24817
24818 /* A get_unnamed_section callback, used for switching to toc_section.  */
24819
24820 static void
24821 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
24822 {
24823   if (TARGET_MINIMAL_TOC)
24824     {
24825       /* toc_section is always selected at least once from
24826          rs6000_xcoff_file_start, so this is guaranteed to
24827          always be defined once and only once in each file.  */
24828       if (!toc_initialized)
24829         {
24830           fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
24831           fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
24832           toc_initialized = 1;
24833         }
24834       fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
24835                (TARGET_32BIT ? "" : ",3"));
24836     }
24837   else
24838     fputs ("\t.toc\n", asm_out_file);
24839 }
24840
24841 /* Implement TARGET_ASM_INIT_SECTIONS.  */
24842
24843 static void
24844 rs6000_xcoff_asm_init_sections (void)
24845 {
24846   read_only_data_section
24847     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
24848                            &xcoff_read_only_section_name);
24849
24850   private_data_section
24851     = get_unnamed_section (SECTION_WRITE,
24852                            rs6000_xcoff_output_readwrite_section_asm_op,
24853                            &xcoff_private_data_section_name);
24854
24855   read_only_private_data_section
24856     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
24857                            &xcoff_private_data_section_name);
24858
24859   toc_section
24860     = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
24861
24862   readonly_data_section = read_only_data_section;
24863   exception_section = data_section;
24864 }
24865
24866 static int
24867 rs6000_xcoff_reloc_rw_mask (void)
24868 {
24869   return 3;
24870 }
24871
24872 static void
24873 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
24874                                 tree decl ATTRIBUTE_UNUSED)
24875 {
24876   int smclass;
24877   static const char * const suffix[3] = { "PR", "RO", "RW" };
24878
24879   if (flags & SECTION_CODE)
24880     smclass = 0;
24881   else if (flags & SECTION_WRITE)
24882     smclass = 2;
24883   else
24884     smclass = 1;
24885
24886   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
24887            (flags & SECTION_CODE) ? "." : "",
24888            name, suffix[smclass], flags & SECTION_ENTSIZE);
24889 }
24890
24891 static section *
24892 rs6000_xcoff_select_section (tree decl, int reloc,
24893                              unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
24894 {
24895   if (decl_readonly_section (decl, reloc))
24896     {
24897       if (TREE_PUBLIC (decl))
24898         return read_only_data_section;
24899       else
24900         return read_only_private_data_section;
24901     }
24902   else
24903     {
24904       if (TREE_PUBLIC (decl))
24905         return data_section;
24906       else
24907         return private_data_section;
24908     }
24909 }
24910
24911 static void
24912 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
24913 {
24914   const char *name;
24915
24916   /* Use select_section for private and uninitialized data.  */
24917   if (!TREE_PUBLIC (decl)
24918       || DECL_COMMON (decl)
24919       || DECL_INITIAL (decl) == NULL_TREE
24920       || DECL_INITIAL (decl) == error_mark_node
24921       || (flag_zero_initialized_in_bss
24922           && initializer_zerop (DECL_INITIAL (decl))))
24923     return;
24924
24925   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
24926   name = (*targetm.strip_name_encoding) (name);
24927   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
24928 }
24929
24930 /* Select section for constant in constant pool.
24931
24932    On RS/6000, all constants are in the private read-only data area.
24933    However, if this is being placed in the TOC it must be output as a
24934    toc entry.  */
24935
24936 static section *
24937 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
24938                                  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
24939 {
24940   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
24941     return toc_section;
24942   else
24943     return read_only_private_data_section;
24944 }
24945
24946 /* Remove any trailing [DS] or the like from the symbol name.  */
24947
24948 static const char *
24949 rs6000_xcoff_strip_name_encoding (const char *name)
24950 {
24951   size_t len;
24952   if (*name == '*')
24953     name++;
24954   len = strlen (name);
24955   if (name[len - 1] == ']')
24956     return ggc_alloc_string (name, len - 4);
24957   else
24958     return name;
24959 }
24960
24961 /* Section attributes.  AIX is always PIC.  */
24962
24963 static unsigned int
24964 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
24965 {
24966   unsigned int align;
24967   unsigned int flags = default_section_type_flags (decl, name, reloc);
24968
24969   /* Align to at least UNIT size.  */
24970   if (flags & SECTION_CODE)
24971     align = MIN_UNITS_PER_WORD;
24972   else
24973     /* Increase alignment of large objects if not already stricter.  */
24974     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
24975                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
24976                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
24977
24978   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
24979 }
24980
24981 /* Output at beginning of assembler file.
24982
24983    Initialize the section names for the RS/6000 at this point.
24984
24985    Specify filename, including full path, to assembler.
24986
24987    We want to go into the TOC section so at least one .toc will be emitted.
24988    Also, in order to output proper .bs/.es pairs, we need at least one static
24989    [RW] section emitted.
24990
24991    Finally, declare mcount when profiling to make the assembler happy.  */
24992
24993 static void
24994 rs6000_xcoff_file_start (void)
24995 {
24996   rs6000_gen_section_name (&xcoff_bss_section_name,
24997                            main_input_filename, ".bss_");
24998   rs6000_gen_section_name (&xcoff_private_data_section_name,
24999                            main_input_filename, ".rw_");
25000   rs6000_gen_section_name (&xcoff_read_only_section_name,
25001                            main_input_filename, ".ro_");
25002
25003   fputs ("\t.file\t", asm_out_file);
25004   output_quoted_string (asm_out_file, main_input_filename);
25005   fputc ('\n', asm_out_file);
25006   if (write_symbols != NO_DEBUG)
25007     switch_to_section (private_data_section);
25008   switch_to_section (text_section);
25009   if (profile_flag)
25010     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
25011   rs6000_file_start ();
25012 }
25013
25014 /* Output at end of assembler file.
25015    On the RS/6000, referencing data should automatically pull in text.  */
25016
25017 static void
25018 rs6000_xcoff_file_end (void)
25019 {
25020   switch_to_section (text_section);
25021   fputs ("_section_.text:\n", asm_out_file);
25022   switch_to_section (data_section);
25023   fputs (TARGET_32BIT
25024          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
25025          asm_out_file);
25026 }
25027 #endif /* TARGET_XCOFF */
25028
25029 /* Compute a (partial) cost for rtx X.  Return true if the complete
25030    cost has been computed, and false if subexpressions should be
25031    scanned.  In either case, *TOTAL contains the cost result.  */
25032
25033 static bool
25034 rs6000_rtx_costs (rtx x, int code, int outer_code, int *total,
25035                   bool speed)
25036 {
25037   enum machine_mode mode = GET_MODE (x);
25038
25039   switch (code)
25040     {
25041       /* On the RS/6000, if it is valid in the insn, it is free.  */
25042     case CONST_INT:
25043       if (((outer_code == SET
25044             || outer_code == PLUS
25045             || outer_code == MINUS)
25046            && (satisfies_constraint_I (x)
25047                || satisfies_constraint_L (x)))
25048           || (outer_code == AND
25049               && (satisfies_constraint_K (x)
25050                   || (mode == SImode
25051                       ? satisfies_constraint_L (x)
25052                       : satisfies_constraint_J (x))
25053                   || mask_operand (x, mode)
25054                   || (mode == DImode
25055                       && mask64_operand (x, DImode))))
25056           || ((outer_code == IOR || outer_code == XOR)
25057               && (satisfies_constraint_K (x)
25058                   || (mode == SImode
25059                       ? satisfies_constraint_L (x)
25060                       : satisfies_constraint_J (x))))
25061           || outer_code == ASHIFT
25062           || outer_code == ASHIFTRT
25063           || outer_code == LSHIFTRT
25064           || outer_code == ROTATE
25065           || outer_code == ROTATERT
25066           || outer_code == ZERO_EXTRACT
25067           || (outer_code == MULT
25068               && satisfies_constraint_I (x))
25069           || ((outer_code == DIV || outer_code == UDIV
25070                || outer_code == MOD || outer_code == UMOD)
25071               && exact_log2 (INTVAL (x)) >= 0)
25072           || (outer_code == COMPARE
25073               && (satisfies_constraint_I (x)
25074                   || satisfies_constraint_K (x)))
25075           || (outer_code == EQ
25076               && (satisfies_constraint_I (x)
25077                   || satisfies_constraint_K (x)
25078                   || (mode == SImode
25079                       ? satisfies_constraint_L (x)
25080                       : satisfies_constraint_J (x))))
25081           || (outer_code == GTU
25082               && satisfies_constraint_I (x))
25083           || (outer_code == LTU
25084               && satisfies_constraint_P (x)))
25085         {
25086           *total = 0;
25087           return true;
25088         }
25089       else if ((outer_code == PLUS
25090                 && reg_or_add_cint_operand (x, VOIDmode))
25091                || (outer_code == MINUS
25092                    && reg_or_sub_cint_operand (x, VOIDmode))
25093                || ((outer_code == SET
25094                     || outer_code == IOR
25095                     || outer_code == XOR)
25096                    && (INTVAL (x)
25097                        & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
25098         {
25099           *total = COSTS_N_INSNS (1);
25100           return true;
25101         }
25102       /* FALLTHRU */
25103
25104     case CONST_DOUBLE:
25105       if (mode == DImode && code == CONST_DOUBLE)
25106         {
25107           if ((outer_code == IOR || outer_code == XOR)
25108               && CONST_DOUBLE_HIGH (x) == 0
25109               && (CONST_DOUBLE_LOW (x)
25110                   & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0)
25111             {
25112               *total = 0;
25113               return true;
25114             }
25115           else if ((outer_code == AND && and64_2_operand (x, DImode))
25116                    || ((outer_code == SET
25117                         || outer_code == IOR
25118                         || outer_code == XOR)
25119                        && CONST_DOUBLE_HIGH (x) == 0))
25120             {
25121               *total = COSTS_N_INSNS (1);
25122               return true;
25123             }
25124         }
25125       /* FALLTHRU */
25126
25127     case CONST:
25128     case HIGH:
25129     case SYMBOL_REF:
25130     case MEM:
25131       /* When optimizing for size, MEM should be slightly more expensive
25132          than generating address, e.g., (plus (reg) (const)).
25133          L1 cache latency is about two instructions.  */
25134       *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
25135       return true;
25136
25137     case LABEL_REF:
25138       *total = 0;
25139       return true;
25140
25141     case PLUS:
25142       if (mode == DFmode)
25143         {
25144           if (GET_CODE (XEXP (x, 0)) == MULT)
25145             {
25146               /* FNMA accounted in outer NEG.  */
25147               if (outer_code == NEG)
25148                 *total = rs6000_cost->dmul - rs6000_cost->fp;
25149               else
25150                 *total = rs6000_cost->dmul;
25151             }
25152           else
25153             *total = rs6000_cost->fp;
25154         }
25155       else if (mode == SFmode)
25156         {
25157           /* FNMA accounted in outer NEG.  */
25158           if (outer_code == NEG && GET_CODE (XEXP (x, 0)) == MULT)
25159             *total = 0;
25160           else
25161             *total = rs6000_cost->fp;
25162         }
25163       else
25164         *total = COSTS_N_INSNS (1);
25165       return false;
25166
25167     case MINUS:
25168       if (mode == DFmode)
25169         {
25170           if (GET_CODE (XEXP (x, 0)) == MULT
25171               || GET_CODE (XEXP (x, 1)) == MULT)
25172             {
25173               /* FNMA accounted in outer NEG.  */
25174               if (outer_code == NEG)
25175                 *total = rs6000_cost->dmul - rs6000_cost->fp;
25176               else
25177                 *total = rs6000_cost->dmul;
25178             }
25179           else
25180             *total = rs6000_cost->fp;
25181         }
25182       else if (mode == SFmode)
25183         {
25184           /* FNMA accounted in outer NEG.  */
25185           if (outer_code == NEG && GET_CODE (XEXP (x, 0)) == MULT)
25186             *total = 0;
25187           else
25188             *total = rs6000_cost->fp;
25189         }
25190       else
25191         *total = COSTS_N_INSNS (1);
25192       return false;
25193
25194     case MULT:
25195       if (GET_CODE (XEXP (x, 1)) == CONST_INT
25196           && satisfies_constraint_I (XEXP (x, 1)))
25197         {
25198           if (INTVAL (XEXP (x, 1)) >= -256
25199               && INTVAL (XEXP (x, 1)) <= 255)
25200             *total = rs6000_cost->mulsi_const9;
25201           else
25202             *total = rs6000_cost->mulsi_const;
25203         }
25204       /* FMA accounted in outer PLUS/MINUS.  */
25205       else if ((mode == DFmode || mode == SFmode)
25206                && (outer_code == PLUS || outer_code == MINUS))
25207         *total = 0;
25208       else if (mode == DFmode)
25209         *total = rs6000_cost->dmul;
25210       else if (mode == SFmode)
25211         *total = rs6000_cost->fp;
25212       else if (mode == DImode)
25213         *total = rs6000_cost->muldi;
25214       else
25215         *total = rs6000_cost->mulsi;
25216       return false;
25217
25218     case DIV:
25219     case MOD:
25220       if (FLOAT_MODE_P (mode))
25221         {
25222           *total = mode == DFmode ? rs6000_cost->ddiv
25223                                   : rs6000_cost->sdiv;
25224           return false;
25225         }
25226       /* FALLTHRU */
25227
25228     case UDIV:
25229     case UMOD:
25230       if (GET_CODE (XEXP (x, 1)) == CONST_INT
25231           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
25232         {
25233           if (code == DIV || code == MOD)
25234             /* Shift, addze */
25235             *total = COSTS_N_INSNS (2);
25236           else
25237             /* Shift */
25238             *total = COSTS_N_INSNS (1);
25239         }
25240       else
25241         {
25242           if (GET_MODE (XEXP (x, 1)) == DImode)
25243             *total = rs6000_cost->divdi;
25244           else
25245             *total = rs6000_cost->divsi;
25246         }
25247       /* Add in shift and subtract for MOD. */
25248       if (code == MOD || code == UMOD)
25249         *total += COSTS_N_INSNS (2);
25250       return false;
25251
25252     case CTZ:
25253     case FFS:
25254       *total = COSTS_N_INSNS (4);
25255       return false;
25256
25257     case POPCOUNT:
25258       *total = COSTS_N_INSNS (6);
25259       return false;
25260
25261     case NOT:
25262       if (outer_code == AND || outer_code == IOR || outer_code == XOR)
25263         {
25264           *total = 0;
25265           return false;
25266         }
25267       /* FALLTHRU */
25268
25269     case AND:
25270     case CLZ:
25271     case IOR:
25272     case XOR:
25273     case ZERO_EXTRACT:
25274       *total = COSTS_N_INSNS (1);
25275       return false;
25276
25277     case ASHIFT:
25278     case ASHIFTRT:
25279     case LSHIFTRT:
25280     case ROTATE:
25281     case ROTATERT:
25282       /* Handle mul_highpart.  */
25283       if (outer_code == TRUNCATE
25284           && GET_CODE (XEXP (x, 0)) == MULT)
25285         {
25286           if (mode == DImode)
25287             *total = rs6000_cost->muldi;
25288           else
25289             *total = rs6000_cost->mulsi;
25290           return true;
25291         }
25292       else if (outer_code == AND)
25293         *total = 0;
25294       else
25295         *total = COSTS_N_INSNS (1);
25296       return false;
25297
25298     case SIGN_EXTEND:
25299     case ZERO_EXTEND:
25300       if (GET_CODE (XEXP (x, 0)) == MEM)
25301         *total = 0;
25302       else
25303         *total = COSTS_N_INSNS (1);
25304       return false;
25305
25306     case COMPARE:
25307     case NEG:
25308     case ABS:
25309       if (!FLOAT_MODE_P (mode))
25310         {
25311           *total = COSTS_N_INSNS (1);
25312           return false;
25313         }
25314       /* FALLTHRU */
25315
25316     case FLOAT:
25317     case UNSIGNED_FLOAT:
25318     case FIX:
25319     case UNSIGNED_FIX:
25320     case FLOAT_TRUNCATE:
25321       *total = rs6000_cost->fp;
25322       return false;
25323
25324     case FLOAT_EXTEND:
25325       if (mode == DFmode)
25326         *total = 0;
25327       else
25328         *total = rs6000_cost->fp;
25329       return false;
25330
25331     case UNSPEC:
25332       switch (XINT (x, 1))
25333         {
25334         case UNSPEC_FRSP:
25335           *total = rs6000_cost->fp;
25336           return true;
25337
25338         default:
25339           break;
25340         }
25341       break;
25342
25343     case CALL:
25344     case IF_THEN_ELSE:
25345       if (!speed)
25346         {
25347           *total = COSTS_N_INSNS (1);
25348           return true;
25349         }
25350       else if (FLOAT_MODE_P (mode)
25351                && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
25352         {
25353           *total = rs6000_cost->fp;
25354           return false;
25355         }
25356       break;
25357
25358     case EQ:
25359     case GTU:
25360     case LTU:
25361       /* Carry bit requires mode == Pmode.
25362          NEG or PLUS already counted so only add one.  */
25363       if (mode == Pmode
25364           && (outer_code == NEG || outer_code == PLUS))
25365         {
25366           *total = COSTS_N_INSNS (1);
25367           return true;
25368         }
25369       if (outer_code == SET)
25370         {
25371           if (XEXP (x, 1) == const0_rtx)
25372             {
25373               if (TARGET_ISEL && !TARGET_MFCRF)
25374                 *total = COSTS_N_INSNS (8);
25375               else
25376                 *total = COSTS_N_INSNS (2);
25377               return true;
25378             }
25379           else if (mode == Pmode)
25380             {
25381               *total = COSTS_N_INSNS (3);
25382               return false;
25383             }
25384         }
25385       /* FALLTHRU */
25386
25387     case GT:
25388     case LT:
25389     case UNORDERED:
25390       if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
25391         {
25392           if (TARGET_ISEL && !TARGET_MFCRF)
25393             *total = COSTS_N_INSNS (8);
25394           else
25395             *total = COSTS_N_INSNS (2);
25396           return true;
25397         }
25398       /* CC COMPARE.  */
25399       if (outer_code == COMPARE)
25400         {
25401           *total = 0;
25402           return true;
25403         }
25404       break;
25405
25406     default:
25407       break;
25408     }
25409
25410   return false;
25411 }
25412
25413 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost.  */
25414
25415 static bool
25416 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int *total,
25417                         bool speed)
25418 {
25419   bool ret = rs6000_rtx_costs (x, code, outer_code, total, speed);
25420
25421   fprintf (stderr,
25422            "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
25423            "total = %d, speed = %s, x:\n",
25424            ret ? "complete" : "scan inner",
25425            GET_RTX_NAME (code),
25426            GET_RTX_NAME (outer_code),
25427            *total,
25428            speed ? "true" : "false");
25429
25430   debug_rtx (x);
25431
25432   return ret;
25433 }
25434
25435 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost.  */
25436
25437 static int
25438 rs6000_debug_address_cost (rtx x, bool speed)
25439 {
25440   int ret = TARGET_ADDRESS_COST (x, speed);
25441
25442   fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
25443            ret, speed ? "true" : "false");
25444   debug_rtx (x);
25445
25446   return ret;
25447 }
25448
25449
25450 /* A C expression returning the cost of moving data from a register of class
25451    CLASS1 to one of CLASS2.  */
25452
25453 int
25454 rs6000_register_move_cost (enum machine_mode mode,
25455                            enum reg_class from, enum reg_class to)
25456 {
25457   int ret;
25458
25459   /*  Moves from/to GENERAL_REGS.  */
25460   if (reg_classes_intersect_p (to, GENERAL_REGS)
25461       || reg_classes_intersect_p (from, GENERAL_REGS))
25462     {
25463       if (! reg_classes_intersect_p (to, GENERAL_REGS))
25464         from = to;
25465
25466       if (from == FLOAT_REGS || from == ALTIVEC_REGS || from == VSX_REGS)
25467         ret = (rs6000_memory_move_cost (mode, from, 0)
25468                + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
25469
25470       /* It's more expensive to move CR_REGS than CR0_REGS because of the
25471          shift.  */
25472       else if (from == CR_REGS)
25473         ret = 4;
25474
25475       /* Power6 has slower LR/CTR moves so make them more expensive than
25476          memory in order to bias spills to memory .*/
25477       else if (rs6000_cpu == PROCESSOR_POWER6
25478                && reg_classes_intersect_p (from, LINK_OR_CTR_REGS))
25479         ret = 6 * hard_regno_nregs[0][mode];
25480
25481       else
25482         /* A move will cost one instruction per GPR moved.  */
25483         ret = 2 * hard_regno_nregs[0][mode];
25484     }
25485
25486   /* If we have VSX, we can easily move between FPR or Altivec registers.  */
25487   else if (VECTOR_UNIT_VSX_P (mode)
25488            && reg_classes_intersect_p (to, VSX_REGS)
25489            && reg_classes_intersect_p (from, VSX_REGS))
25490     ret = 2 * hard_regno_nregs[32][mode];
25491
25492   /* Moving between two similar registers is just one instruction.  */
25493   else if (reg_classes_intersect_p (to, from))
25494     ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
25495
25496   /* Everything else has to go through GENERAL_REGS.  */
25497   else
25498     ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
25499            + rs6000_register_move_cost (mode, from, GENERAL_REGS));
25500
25501   if (TARGET_DEBUG_COST)
25502     fprintf (stderr,
25503              "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
25504              ret, GET_MODE_NAME (mode), reg_class_names[from],
25505              reg_class_names[to]);
25506
25507   return ret;
25508 }
25509
25510 /* A C expressions returning the cost of moving data of MODE from a register to
25511    or from memory.  */
25512
25513 int
25514 rs6000_memory_move_cost (enum machine_mode mode, enum reg_class rclass,
25515                          int in ATTRIBUTE_UNUSED)
25516 {
25517   int ret;
25518
25519   if (reg_classes_intersect_p (rclass, GENERAL_REGS))
25520     ret = 4 * hard_regno_nregs[0][mode];
25521   else if (reg_classes_intersect_p (rclass, FLOAT_REGS))
25522     ret = 4 * hard_regno_nregs[32][mode];
25523   else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
25524     ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
25525   else
25526     ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
25527
25528   if (TARGET_DEBUG_COST)
25529     fprintf (stderr,
25530              "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
25531              ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
25532
25533   return ret;
25534 }
25535
25536 /* Returns a code for a target-specific builtin that implements
25537    reciprocal of the function, or NULL_TREE if not available.  */
25538
25539 static tree
25540 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
25541                            bool sqrt ATTRIBUTE_UNUSED)
25542 {
25543   if (optimize_insn_for_size_p ())
25544     return NULL_TREE;
25545
25546   if (md_fn)
25547     switch (fn)
25548       {
25549       case VSX_BUILTIN_XVSQRTDP:
25550         if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
25551           return NULL_TREE;
25552
25553         return rs6000_builtin_decls[VSX_BUILTIN_VEC_RSQRT_V2DF];
25554
25555       case VSX_BUILTIN_XVSQRTSP:
25556         if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
25557           return NULL_TREE;
25558
25559         return rs6000_builtin_decls[VSX_BUILTIN_VEC_RSQRT_V4SF];
25560
25561       default:
25562         return NULL_TREE;
25563       }
25564
25565   else
25566     switch (fn)
25567       {
25568       case BUILT_IN_SQRT:
25569         if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
25570           return NULL_TREE;
25571
25572         return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
25573
25574       case BUILT_IN_SQRTF:
25575         if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
25576           return NULL_TREE;
25577
25578         return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
25579
25580       default:
25581         return NULL_TREE;
25582       }
25583 }
25584
25585 /* Load up a constant.  If the mode is a vector mode, splat the value across
25586    all of the vector elements.  */
25587
25588 static rtx
25589 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
25590 {
25591   rtx reg;
25592
25593   if (mode == SFmode || mode == DFmode)
25594     {
25595       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
25596       reg = force_reg (mode, d);
25597     }
25598   else if (mode == V4SFmode)
25599     {
25600       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
25601       rtvec v = gen_rtvec (4, d, d, d, d);
25602       reg = gen_reg_rtx (mode);
25603       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
25604     }
25605   else if (mode == V2DFmode)
25606     {
25607       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
25608       rtvec v = gen_rtvec (2, d, d);
25609       reg = gen_reg_rtx (mode);
25610       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
25611     }
25612   else
25613     gcc_unreachable ();
25614
25615   return reg;
25616 }
25617
25618 /* Generate a FMADD instruction:
25619         dst = (m1 * m2) + a
25620
25621    generating different RTL based on the fused multiply/add switch.  */
25622
25623 static void
25624 rs6000_emit_madd (rtx dst, rtx m1, rtx m2, rtx a)
25625 {
25626   enum machine_mode mode = GET_MODE (dst);
25627
25628   if (!TARGET_FUSED_MADD)
25629     {
25630       /* For the simple ops, use the generator function, rather than assuming
25631          that the RTL is standard.  */
25632       enum insn_code mcode = optab_handler (smul_optab, mode)->insn_code;
25633       enum insn_code acode = optab_handler (add_optab, mode)->insn_code;
25634       gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (mcode);
25635       gen_2arg_fn_t gen_add = (gen_2arg_fn_t) GEN_FCN (acode);
25636       rtx mreg = gen_reg_rtx (mode);
25637
25638       gcc_assert (mcode != CODE_FOR_nothing && acode != CODE_FOR_nothing);
25639       emit_insn (gen_mul (mreg, m1, m2));
25640       emit_insn (gen_add (dst, mreg, a));
25641     }
25642
25643   else
25644     emit_insn (gen_rtx_SET (VOIDmode, dst,
25645                             gen_rtx_PLUS (mode,
25646                                           gen_rtx_MULT (mode, m1, m2),
25647                                           a)));
25648 }
25649
25650 /* Generate a FMSUB instruction:
25651         dst = (m1 * m2) - a
25652
25653    generating different RTL based on the fused multiply/add switch.  */
25654
25655 static void
25656 rs6000_emit_msub (rtx dst, rtx m1, rtx m2, rtx a)
25657 {
25658   enum machine_mode mode = GET_MODE (dst);
25659
25660   if (!TARGET_FUSED_MADD
25661       || (mode == V4SFmode && VECTOR_UNIT_ALTIVEC_P (V4SFmode)))
25662     {
25663       /* For the simple ops, use the generator function, rather than assuming
25664          that the RTL is standard.  */
25665       enum insn_code mcode = optab_handler (smul_optab, mode)->insn_code;
25666       enum insn_code scode = optab_handler (add_optab, mode)->insn_code;
25667       gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (mcode);
25668       gen_2arg_fn_t gen_sub = (gen_2arg_fn_t) GEN_FCN (scode);
25669       rtx mreg = gen_reg_rtx (mode);
25670
25671       gcc_assert (mcode != CODE_FOR_nothing && scode != CODE_FOR_nothing);
25672       emit_insn (gen_mul (mreg, m1, m2));
25673       emit_insn (gen_sub (dst, mreg, a));
25674     }
25675
25676   else
25677     emit_insn (gen_rtx_SET (VOIDmode, dst,
25678                             gen_rtx_MINUS (mode,
25679                                            gen_rtx_MULT (mode, m1, m2),
25680                                            a)));
25681 }
25682
25683 /* Generate a FNMSUB instruction:
25684         dst = - ((m1 * m2) - a)
25685
25686    Which is equivalent to (except in the prescence of -0.0):
25687         dst = a - (m1 * m2)
25688
25689    generating different RTL based on the fast-math and fused multiply/add
25690    switches.  */
25691
25692 static void
25693 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
25694 {
25695   enum machine_mode mode = GET_MODE (dst);
25696
25697   if (!TARGET_FUSED_MADD)
25698     {
25699       /* For the simple ops, use the generator function, rather than assuming
25700          that the RTL is standard.  */
25701       enum insn_code mcode = optab_handler (smul_optab, mode)->insn_code;
25702       enum insn_code scode = optab_handler (sub_optab, mode)->insn_code;
25703       gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (mcode);
25704       gen_2arg_fn_t gen_sub = (gen_2arg_fn_t) GEN_FCN (scode);
25705       rtx mreg = gen_reg_rtx (mode);
25706
25707       gcc_assert (mcode != CODE_FOR_nothing && scode != CODE_FOR_nothing);
25708       emit_insn (gen_mul (mreg, m1, m2));
25709       emit_insn (gen_sub (dst, a, mreg));
25710     }
25711
25712   else
25713     {
25714       rtx m = gen_rtx_MULT (mode, m1, m2);
25715
25716       if (!HONOR_SIGNED_ZEROS (mode))
25717         emit_insn (gen_rtx_SET (VOIDmode, dst, gen_rtx_MINUS (mode, a, m)));
25718
25719       else
25720         emit_insn (gen_rtx_SET (VOIDmode, dst,
25721                                 gen_rtx_NEG (mode,
25722                                              gen_rtx_MINUS (mode, m, a))));
25723     }
25724 }
25725
25726 /* Newton-Raphson approximation of floating point divide with just 2 passes
25727    (either single precision floating point, or newer machines with higher
25728    accuracy estimates).  Support both scalar and vector divide.  Assumes no
25729    trapping math and finite arguments.  */
25730
25731 static void
25732 rs6000_emit_swdiv_high_precision (rtx dst, rtx n, rtx d)
25733 {
25734   enum machine_mode mode = GET_MODE (dst);
25735   rtx x0, e0, e1, y1, u0, v0;
25736   enum insn_code code = optab_handler (smul_optab, mode)->insn_code;
25737   gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
25738   rtx one = rs6000_load_constant_and_splat (mode, dconst1);
25739
25740   gcc_assert (code != CODE_FOR_nothing);
25741
25742   /* x0 = 1./d estimate */
25743   x0 = gen_reg_rtx (mode);
25744   emit_insn (gen_rtx_SET (VOIDmode, x0,
25745                           gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
25746                                           UNSPEC_FRES)));
25747
25748   e0 = gen_reg_rtx (mode);
25749   rs6000_emit_nmsub (e0, d, x0, one);           /* e0 = 1. - (d * x0) */
25750
25751   e1 = gen_reg_rtx (mode);
25752   rs6000_emit_madd (e1, e0, e0, e0);            /* e1 = (e0 * e0) + e0 */
25753
25754   y1 = gen_reg_rtx (mode);
25755   rs6000_emit_madd (y1, e1, x0, x0);            /* y1 = (e1 * x0) + x0 */
25756
25757   u0 = gen_reg_rtx (mode);
25758   emit_insn (gen_mul (u0, n, y1));              /* u0 = n * y1 */
25759
25760   v0 = gen_reg_rtx (mode);
25761   rs6000_emit_nmsub (v0, d, u0, n);             /* v0 = n - (d * u0) */
25762
25763   rs6000_emit_madd (dst, v0, y1, u0);           /* dst = (v0 * y1) + u0 */
25764 }
25765
25766 /* Newton-Raphson approximation of floating point divide that has a low
25767    precision estimate.  Assumes no trapping math and finite arguments.  */
25768
25769 static void
25770 rs6000_emit_swdiv_low_precision (rtx dst, rtx n, rtx d)
25771 {
25772   enum machine_mode mode = GET_MODE (dst);
25773   rtx x0, e0, e1, e2, y1, y2, y3, u0, v0, one;
25774   enum insn_code code = optab_handler (smul_optab, mode)->insn_code;
25775   gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
25776
25777   gcc_assert (code != CODE_FOR_nothing);
25778
25779   one = rs6000_load_constant_and_splat (mode, dconst1);
25780
25781   /* x0 = 1./d estimate */
25782   x0 = gen_reg_rtx (mode);
25783   emit_insn (gen_rtx_SET (VOIDmode, x0,
25784                           gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
25785                                           UNSPEC_FRES)));
25786
25787   e0 = gen_reg_rtx (mode);
25788   rs6000_emit_nmsub (e0, d, x0, one);           /* e0 = 1. - d * x0 */
25789
25790   y1 = gen_reg_rtx (mode);
25791   rs6000_emit_madd (y1, e0, x0, x0);            /* y1 = x0 + e0 * x0 */
25792
25793   e1 = gen_reg_rtx (mode);
25794   emit_insn (gen_mul (e1, e0, e0));             /* e1 = e0 * e0 */
25795
25796   y2 = gen_reg_rtx (mode);
25797   rs6000_emit_madd (y2, e1, y1, y1);            /* y2 = y1 + e1 * y1 */
25798
25799   e2 = gen_reg_rtx (mode);
25800   emit_insn (gen_mul (e2, e1, e1));             /* e2 = e1 * e1 */
25801
25802   y3 = gen_reg_rtx (mode);
25803   rs6000_emit_madd (y3, e2, y2, y2);            /* y3 = y2 + e2 * y2 */
25804
25805   u0 = gen_reg_rtx (mode);
25806   emit_insn (gen_mul (u0, n, y3));              /* u0 = n * y3 */
25807
25808   v0 = gen_reg_rtx (mode);
25809   rs6000_emit_nmsub (v0, d, u0, n);             /* v0 = n - d * u0 */
25810
25811   rs6000_emit_madd (dst, v0, y3, u0);           /* dst = u0 + v0 * y3 */
25812 }
25813
25814 /* Newton-Raphson approximation of floating point divide DST = N/D.  If NOTE_P,
25815    add a reg_note saying that this was a division.  Support both scalar and
25816    vector divide.  Assumes no trapping math and finite arguments.  */
25817
25818 void
25819 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
25820 {
25821   enum machine_mode mode = GET_MODE (dst);
25822
25823   if (RS6000_RECIP_HIGH_PRECISION_P (mode))
25824     rs6000_emit_swdiv_high_precision (dst, n, d);
25825   else
25826     rs6000_emit_swdiv_low_precision (dst, n, d);
25827
25828   if (note_p)
25829     add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
25830 }
25831
25832 /* Newton-Raphson approximation of single/double-precision floating point
25833    rsqrt.  Assumes no trapping math and finite arguments.  */
25834
25835 void
25836 rs6000_emit_swrsqrt (rtx dst, rtx src)
25837 {
25838   enum machine_mode mode = GET_MODE (src);
25839   rtx x0 = gen_reg_rtx (mode);
25840   rtx y = gen_reg_rtx (mode);
25841   int passes = (TARGET_RECIP_PRECISION) ? 2 : 3;
25842   REAL_VALUE_TYPE dconst3_2;
25843   int i;
25844   rtx halfthree;
25845   enum insn_code code = optab_handler (smul_optab, mode)->insn_code;
25846   gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
25847
25848   gcc_assert (code != CODE_FOR_nothing);
25849
25850   /* Load up the constant 1.5 either as a scalar, or as a vector.  */
25851   real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
25852   SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
25853
25854   halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
25855
25856   /* x0 = rsqrt estimate */
25857   emit_insn (gen_rtx_SET (VOIDmode, x0,
25858                           gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
25859                                           UNSPEC_RSQRT)));
25860
25861   /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
25862   rs6000_emit_msub (y, src, halfthree, src);
25863
25864   for (i = 0; i < passes; i++)
25865     {
25866       rtx x1 = gen_reg_rtx (mode);
25867       rtx u = gen_reg_rtx (mode);
25868       rtx v = gen_reg_rtx (mode);
25869
25870       /* x1 = x0 * (1.5 - y * (x0 * x0)) */
25871       emit_insn (gen_mul (u, x0, x0));
25872       rs6000_emit_nmsub (v, y, u, halfthree);
25873       emit_insn (gen_mul (x1, x0, v));
25874       x0 = x1;
25875     }
25876
25877   emit_move_insn (dst, x0);
25878   return;
25879 }
25880
25881 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
25882    (Power7) targets.  DST is the target, and SRC is the argument operand.  */
25883
25884 void
25885 rs6000_emit_popcount (rtx dst, rtx src)
25886 {
25887   enum machine_mode mode = GET_MODE (dst);
25888   rtx tmp1, tmp2;
25889
25890   /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can.  */
25891   if (TARGET_POPCNTD)
25892     {
25893       if (mode == SImode)
25894         emit_insn (gen_popcntwsi2 (dst, src));
25895       else
25896         emit_insn (gen_popcntddi2 (dst, src));
25897       return;
25898     }
25899
25900   tmp1 = gen_reg_rtx (mode);
25901
25902   if (mode == SImode)
25903     {
25904       emit_insn (gen_popcntbsi2 (tmp1, src));
25905       tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
25906                            NULL_RTX, 0);
25907       tmp2 = force_reg (SImode, tmp2);
25908       emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
25909     }
25910   else
25911     {
25912       emit_insn (gen_popcntbdi2 (tmp1, src));
25913       tmp2 = expand_mult (DImode, tmp1,
25914                           GEN_INT ((HOST_WIDE_INT)
25915                                    0x01010101 << 32 | 0x01010101),
25916                           NULL_RTX, 0);
25917       tmp2 = force_reg (DImode, tmp2);
25918       emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
25919     }
25920 }
25921
25922
25923 /* Emit parity intrinsic on TARGET_POPCNTB targets.  DST is the
25924    target, and SRC is the argument operand.  */
25925
25926 void
25927 rs6000_emit_parity (rtx dst, rtx src)
25928 {
25929   enum machine_mode mode = GET_MODE (dst);
25930   rtx tmp;
25931
25932   tmp = gen_reg_rtx (mode);
25933   if (mode == SImode)
25934     {
25935       /* Is mult+shift >= shift+xor+shift+xor?  */
25936       if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
25937         {
25938           rtx tmp1, tmp2, tmp3, tmp4;
25939
25940           tmp1 = gen_reg_rtx (SImode);
25941           emit_insn (gen_popcntbsi2 (tmp1, src));
25942
25943           tmp2 = gen_reg_rtx (SImode);
25944           emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
25945           tmp3 = gen_reg_rtx (SImode);
25946           emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
25947
25948           tmp4 = gen_reg_rtx (SImode);
25949           emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
25950           emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
25951         }
25952       else
25953         rs6000_emit_popcount (tmp, src);
25954       emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
25955     }
25956   else
25957     {
25958       /* Is mult+shift >= shift+xor+shift+xor+shift+xor?  */
25959       if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
25960         {
25961           rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
25962
25963           tmp1 = gen_reg_rtx (DImode);
25964           emit_insn (gen_popcntbdi2 (tmp1, src));
25965
25966           tmp2 = gen_reg_rtx (DImode);
25967           emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
25968           tmp3 = gen_reg_rtx (DImode);
25969           emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
25970
25971           tmp4 = gen_reg_rtx (DImode);
25972           emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
25973           tmp5 = gen_reg_rtx (DImode);
25974           emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
25975
25976           tmp6 = gen_reg_rtx (DImode);
25977           emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
25978           emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
25979         }
25980       else
25981         rs6000_emit_popcount (tmp, src);
25982       emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
25983     }
25984 }
25985
25986 /* Return an RTX representing where to find the function value of a
25987    function returning MODE.  */
25988 static rtx
25989 rs6000_complex_function_value (enum machine_mode mode)
25990 {
25991   unsigned int regno;
25992   rtx r1, r2;
25993   enum machine_mode inner = GET_MODE_INNER (mode);
25994   unsigned int inner_bytes = GET_MODE_SIZE (inner);
25995
25996   if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
25997     regno = FP_ARG_RETURN;
25998   else
25999     {
26000       regno = GP_ARG_RETURN;
26001
26002       /* 32-bit is OK since it'll go in r3/r4.  */
26003       if (TARGET_32BIT && inner_bytes >= 4)
26004         return gen_rtx_REG (mode, regno);
26005     }
26006
26007   if (inner_bytes >= 8)
26008     return gen_rtx_REG (mode, regno);
26009
26010   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
26011                           const0_rtx);
26012   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
26013                           GEN_INT (inner_bytes));
26014   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
26015 }
26016
26017 /* Target hook for TARGET_FUNCTION_VALUE.
26018
26019    On the SPE, both FPs and vectors are returned in r3.
26020
26021    On RS/6000 an integer value is in r3 and a floating-point value is in
26022    fp1, unless -msoft-float.  */
26023
26024 rtx
26025 rs6000_function_value (const_tree valtype,
26026                        const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
26027                        bool outgoing ATTRIBUTE_UNUSED)
26028 {
26029   enum machine_mode mode;
26030   unsigned int regno;
26031
26032   /* Special handling for structs in darwin64.  */
26033   if (rs6000_darwin64_abi
26034       && TYPE_MODE (valtype) == BLKmode
26035       && TREE_CODE (valtype) == RECORD_TYPE
26036       && int_size_in_bytes (valtype) > 0)
26037     {
26038       CUMULATIVE_ARGS valcum;
26039       rtx valret;
26040
26041       valcum.words = 0;
26042       valcum.fregno = FP_ARG_MIN_REG;
26043       valcum.vregno = ALTIVEC_ARG_MIN_REG;
26044       /* Do a trial code generation as if this were going to be passed as
26045          an argument; if any part goes in memory, we return NULL.  */
26046       valret = rs6000_darwin64_record_arg (&valcum, valtype, 1, true);
26047       if (valret)
26048         return valret;
26049       /* Otherwise fall through to standard ABI rules.  */
26050     }
26051
26052   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
26053     {
26054       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
26055       return gen_rtx_PARALLEL (DImode,
26056         gen_rtvec (2,
26057                    gen_rtx_EXPR_LIST (VOIDmode,
26058                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
26059                                       const0_rtx),
26060                    gen_rtx_EXPR_LIST (VOIDmode,
26061                                       gen_rtx_REG (SImode,
26062                                                    GP_ARG_RETURN + 1),
26063                                       GEN_INT (4))));
26064     }
26065   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
26066     {
26067       return gen_rtx_PARALLEL (DCmode,
26068         gen_rtvec (4,
26069                    gen_rtx_EXPR_LIST (VOIDmode,
26070                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
26071                                       const0_rtx),
26072                    gen_rtx_EXPR_LIST (VOIDmode,
26073                                       gen_rtx_REG (SImode,
26074                                                    GP_ARG_RETURN + 1),
26075                                       GEN_INT (4)),
26076                    gen_rtx_EXPR_LIST (VOIDmode,
26077                                       gen_rtx_REG (SImode,
26078                                                    GP_ARG_RETURN + 2),
26079                                       GEN_INT (8)),
26080                    gen_rtx_EXPR_LIST (VOIDmode,
26081                                       gen_rtx_REG (SImode,
26082                                                    GP_ARG_RETURN + 3),
26083                                       GEN_INT (12))));
26084     }
26085
26086   mode = TYPE_MODE (valtype);
26087   if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
26088       || POINTER_TYPE_P (valtype))
26089     mode = TARGET_32BIT ? SImode : DImode;
26090
26091   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26092     /* _Decimal128 must use an even/odd register pair.  */
26093     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
26094   else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
26095            && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
26096     regno = FP_ARG_RETURN;
26097   else if (TREE_CODE (valtype) == COMPLEX_TYPE
26098            && targetm.calls.split_complex_arg)
26099     return rs6000_complex_function_value (mode);
26100   else if (TREE_CODE (valtype) == VECTOR_TYPE
26101            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
26102            && ALTIVEC_VECTOR_MODE (mode))
26103     regno = ALTIVEC_ARG_RETURN;
26104   else if (TREE_CODE (valtype) == VECTOR_TYPE
26105            && TARGET_VSX && TARGET_ALTIVEC_ABI
26106            && VSX_VECTOR_MODE (mode))
26107     regno = ALTIVEC_ARG_RETURN;
26108   else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
26109            && (mode == DFmode || mode == DCmode
26110                || mode == TFmode || mode == TCmode))
26111     return spe_build_register_parallel (mode, GP_ARG_RETURN);
26112   else
26113     regno = GP_ARG_RETURN;
26114
26115   return gen_rtx_REG (mode, regno);
26116 }
26117
26118 /* Define how to find the value returned by a library function
26119    assuming the value has mode MODE.  */
26120 rtx
26121 rs6000_libcall_value (enum machine_mode mode)
26122 {
26123   unsigned int regno;
26124
26125   if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
26126     {
26127       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
26128       return gen_rtx_PARALLEL (DImode,
26129         gen_rtvec (2,
26130                    gen_rtx_EXPR_LIST (VOIDmode,
26131                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
26132                                       const0_rtx),
26133                    gen_rtx_EXPR_LIST (VOIDmode,
26134                                       gen_rtx_REG (SImode,
26135                                                    GP_ARG_RETURN + 1),
26136                                       GEN_INT (4))));
26137     }
26138
26139   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26140     /* _Decimal128 must use an even/odd register pair.  */
26141     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
26142   else if (SCALAR_FLOAT_MODE_P (mode)
26143            && TARGET_HARD_FLOAT && TARGET_FPRS
26144            && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
26145     regno = FP_ARG_RETURN;
26146   else if (ALTIVEC_VECTOR_MODE (mode)
26147            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
26148     regno = ALTIVEC_ARG_RETURN;
26149   else if (VSX_VECTOR_MODE (mode)
26150            && TARGET_VSX && TARGET_ALTIVEC_ABI)
26151     regno = ALTIVEC_ARG_RETURN;
26152   else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
26153     return rs6000_complex_function_value (mode);
26154   else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
26155            && (mode == DFmode || mode == DCmode
26156                || mode == TFmode || mode == TCmode))
26157     return spe_build_register_parallel (mode, GP_ARG_RETURN);
26158   else
26159     regno = GP_ARG_RETURN;
26160
26161   return gen_rtx_REG (mode, regno);
26162 }
26163
26164
26165 /* Given FROM and TO register numbers, say whether this elimination is allowed.
26166    Frame pointer elimination is automatically handled.
26167
26168    For the RS/6000, if frame pointer elimination is being done, we would like
26169    to convert ap into fp, not sp.
26170
26171    We need r30 if -mminimal-toc was specified, and there are constant pool
26172    references.  */
26173
26174 bool
26175 rs6000_can_eliminate (const int from, const int to)
26176 {
26177   return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
26178           ? ! frame_pointer_needed
26179           : from == RS6000_PIC_OFFSET_TABLE_REGNUM
26180             ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
26181             : true);
26182 }
26183
26184 /* Define the offset between two registers, FROM to be eliminated and its
26185    replacement TO, at the start of a routine.  */
26186 HOST_WIDE_INT
26187 rs6000_initial_elimination_offset (int from, int to)
26188 {
26189   rs6000_stack_t *info = rs6000_stack_info ();
26190   HOST_WIDE_INT offset;
26191
26192   if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
26193     offset = info->push_p ? 0 : -info->total_size;
26194   else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
26195     {
26196       offset = info->push_p ? 0 : -info->total_size;
26197       if (FRAME_GROWS_DOWNWARD)
26198         offset += info->fixed_size + info->vars_size + info->parm_size;
26199     }
26200   else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
26201     offset = FRAME_GROWS_DOWNWARD
26202              ? info->fixed_size + info->vars_size + info->parm_size
26203              : 0;
26204   else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
26205     offset = info->total_size;
26206   else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
26207     offset = info->push_p ? info->total_size : 0;
26208   else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
26209     offset = 0;
26210   else
26211     gcc_unreachable ();
26212
26213   return offset;
26214 }
26215
26216 static rtx
26217 rs6000_dwarf_register_span (rtx reg)
26218 {
26219   rtx parts[8];
26220   int i, words;
26221   unsigned regno = REGNO (reg);
26222   enum machine_mode mode = GET_MODE (reg);
26223
26224   if (TARGET_SPE
26225       && regno < 32
26226       && (SPE_VECTOR_MODE (GET_MODE (reg))
26227           || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
26228               && mode != SFmode && mode != SDmode && mode != SCmode)))
26229     ;
26230   else
26231     return NULL_RTX;
26232
26233   regno = REGNO (reg);
26234
26235   /* The duality of the SPE register size wreaks all kinds of havoc.
26236      This is a way of distinguishing r0 in 32-bits from r0 in
26237      64-bits.  */
26238   words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
26239   gcc_assert (words <= 4);
26240   for (i = 0; i < words; i++, regno++)
26241     {
26242       if (BYTES_BIG_ENDIAN)
26243         {
26244           parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
26245           parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
26246         }
26247       else
26248         {
26249           parts[2 * i] = gen_rtx_REG (SImode, regno);
26250           parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
26251         }
26252     }
26253
26254   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
26255 }
26256
26257 /* Fill in sizes for SPE register high parts in table used by unwinder.  */
26258
26259 static void
26260 rs6000_init_dwarf_reg_sizes_extra (tree address)
26261 {
26262   if (TARGET_SPE)
26263     {
26264       int i;
26265       enum machine_mode mode = TYPE_MODE (char_type_node);
26266       rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
26267       rtx mem = gen_rtx_MEM (BLKmode, addr);
26268       rtx value = gen_int_mode (4, mode);
26269
26270       for (i = 1201; i < 1232; i++)
26271         {
26272           int column = DWARF_REG_TO_UNWIND_COLUMN (i);
26273           HOST_WIDE_INT offset
26274             = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
26275
26276           emit_move_insn (adjust_address (mem, mode, offset), value);
26277         }
26278     }
26279 }
26280
26281 /* Map internal gcc register numbers to DWARF2 register numbers.  */
26282
26283 unsigned int
26284 rs6000_dbx_register_number (unsigned int regno)
26285 {
26286   if (regno <= 63 || write_symbols != DWARF2_DEBUG)
26287     return regno;
26288   if (regno == MQ_REGNO)
26289     return 100;
26290   if (regno == LR_REGNO)
26291     return 108;
26292   if (regno == CTR_REGNO)
26293     return 109;
26294   if (CR_REGNO_P (regno))
26295     return regno - CR0_REGNO + 86;
26296   if (regno == CA_REGNO)
26297     return 101;  /* XER */
26298   if (ALTIVEC_REGNO_P (regno))
26299     return regno - FIRST_ALTIVEC_REGNO + 1124;
26300   if (regno == VRSAVE_REGNO)
26301     return 356;
26302   if (regno == VSCR_REGNO)
26303     return 67;
26304   if (regno == SPE_ACC_REGNO)
26305     return 99;
26306   if (regno == SPEFSCR_REGNO)
26307     return 612;
26308   /* SPE high reg number.  We get these values of regno from
26309      rs6000_dwarf_register_span.  */
26310   gcc_assert (regno >= 1200 && regno < 1232);
26311   return regno;
26312 }
26313
26314 /* target hook eh_return_filter_mode */
26315 static enum machine_mode
26316 rs6000_eh_return_filter_mode (void)
26317 {
26318   return TARGET_32BIT ? SImode : word_mode;
26319 }
26320
26321 /* Target hook for scalar_mode_supported_p.  */
26322 static bool
26323 rs6000_scalar_mode_supported_p (enum machine_mode mode)
26324 {
26325   if (DECIMAL_FLOAT_MODE_P (mode))
26326     return default_decimal_float_supported_p ();
26327   else
26328     return default_scalar_mode_supported_p (mode);
26329 }
26330
26331 /* Target hook for vector_mode_supported_p.  */
26332 static bool
26333 rs6000_vector_mode_supported_p (enum machine_mode mode)
26334 {
26335
26336   if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
26337     return true;
26338
26339   if (TARGET_SPE && SPE_VECTOR_MODE (mode))
26340     return true;
26341
26342   else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
26343     return true;
26344
26345   else
26346     return false;
26347 }
26348
26349 /* Target hook for invalid_arg_for_unprototyped_fn. */
26350 static const char *
26351 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
26352 {
26353   return (!rs6000_darwin64_abi
26354           && typelist == 0
26355           && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
26356           && (funcdecl == NULL_TREE
26357               || (TREE_CODE (funcdecl) == FUNCTION_DECL
26358                   && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
26359           ? N_("AltiVec argument passed to unprototyped function")
26360           : NULL;
26361 }
26362
26363 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
26364    setup by using __stack_chk_fail_local hidden function instead of
26365    calling __stack_chk_fail directly.  Otherwise it is better to call
26366    __stack_chk_fail directly.  */
26367
26368 static tree
26369 rs6000_stack_protect_fail (void)
26370 {
26371   return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
26372          ? default_hidden_stack_protect_fail ()
26373          : default_external_stack_protect_fail ();
26374 }
26375
26376 void
26377 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
26378                            int num_operands ATTRIBUTE_UNUSED)
26379 {
26380   if (rs6000_warn_cell_microcode)
26381     {
26382       const char *temp;
26383       int insn_code_number = recog_memoized (insn);
26384       location_t location = locator_location (INSN_LOCATOR (insn));
26385
26386       /* Punt on insns we cannot recognize.  */
26387       if (insn_code_number < 0)
26388         return;
26389
26390       temp = get_insn_template (insn_code_number, insn);
26391
26392       if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
26393         warning_at (location, OPT_mwarn_cell_microcode,
26394                     "emitting microcode insn %s\t[%s] #%d",
26395                     temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn)); 
26396       else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
26397         warning_at (location, OPT_mwarn_cell_microcode,
26398                     "emitting conditional microcode insn %s\t[%s] #%d",
26399                     temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
26400     }
26401 }
26402
26403 #include "gt-rs6000.h"