linux64.h (OPTION_TARGET_CPU_DEFAULT): Do not redefine to be NULL if the current...
[platform/upstream/gcc.git] / gcc / config / rs6000 / rs6000.c
1 /* Subroutines used for code generation on IBM RS/6000.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4    2012
5    Free Software Foundation, Inc.
6    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 3, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING3.  If not see
22    <http://www.gnu.org/licenses/>.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "obstack.h"
37 #include "tree.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "except.h"
41 #include "function.h"
42 #include "output.h"
43 #include "basic-block.h"
44 #include "integrate.h"
45 #include "diagnostic-core.h"
46 #include "toplev.h"
47 #include "ggc.h"
48 #include "hashtab.h"
49 #include "tm_p.h"
50 #include "target.h"
51 #include "target-def.h"
52 #include "common/common-target.h"
53 #include "langhooks.h"
54 #include "reload.h"
55 #include "cfglayout.h"
56 #include "cfgloop.h"
57 #include "sched-int.h"
58 #include "gimple.h"
59 #include "tree-flow.h"
60 #include "intl.h"
61 #include "params.h"
62 #include "tm-constrs.h"
63 #include "opts.h"
64 #if TARGET_XCOFF
65 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
66 #endif
67 #if TARGET_MACHO
68 #include "gstab.h"  /* for N_SLINE */
69 #endif
70
71 #ifndef TARGET_NO_PROTOTYPE
72 #define TARGET_NO_PROTOTYPE 0
73 #endif
74
75 #define min(A,B)        ((A) < (B) ? (A) : (B))
76 #define max(A,B)        ((A) > (B) ? (A) : (B))
77
78 /* Structure used to define the rs6000 stack */
79 typedef struct rs6000_stack {
80   int reload_completed;         /* stack info won't change from here on */
81   int first_gp_reg_save;        /* first callee saved GP register used */
82   int first_fp_reg_save;        /* first callee saved FP register used */
83   int first_altivec_reg_save;   /* first callee saved AltiVec register used */
84   int lr_save_p;                /* true if the link reg needs to be saved */
85   int cr_save_p;                /* true if the CR reg needs to be saved */
86   unsigned int vrsave_mask;     /* mask of vec registers to save */
87   int push_p;                   /* true if we need to allocate stack space */
88   int calls_p;                  /* true if the function makes any calls */
89   int world_save_p;             /* true if we're saving *everything*:
90                                    r13-r31, cr, f14-f31, vrsave, v20-v31  */
91   enum rs6000_abi abi;          /* which ABI to use */
92   int gp_save_offset;           /* offset to save GP regs from initial SP */
93   int fp_save_offset;           /* offset to save FP regs from initial SP */
94   int altivec_save_offset;      /* offset to save AltiVec regs from initial SP */
95   int lr_save_offset;           /* offset to save LR from initial SP */
96   int cr_save_offset;           /* offset to save CR from initial SP */
97   int vrsave_save_offset;       /* offset to save VRSAVE from initial SP */
98   int spe_gp_save_offset;       /* offset to save spe 64-bit gprs  */
99   int varargs_save_offset;      /* offset to save the varargs registers */
100   int ehrd_offset;              /* offset to EH return data */
101   int reg_size;                 /* register size (4 or 8) */
102   HOST_WIDE_INT vars_size;      /* variable save area size */
103   int parm_size;                /* outgoing parameter size */
104   int save_size;                /* save area size */
105   int fixed_size;               /* fixed size of stack frame */
106   int gp_size;                  /* size of saved GP registers */
107   int fp_size;                  /* size of saved FP registers */
108   int altivec_size;             /* size of saved AltiVec registers */
109   int cr_size;                  /* size to hold CR if not in save_size */
110   int vrsave_size;              /* size to hold VRSAVE if not in save_size */
111   int altivec_padding_size;     /* size of altivec alignment padding if
112                                    not in save_size */
113   int spe_gp_size;              /* size of 64-bit GPR save size for SPE */
114   int spe_padding_size;
115   HOST_WIDE_INT total_size;     /* total bytes allocated for stack */
116   int spe_64bit_regs_used;
117   int savres_strategy;
118 } rs6000_stack_t;
119
120 /* A C structure for machine-specific, per-function data.
121    This is added to the cfun structure.  */
122 typedef struct GTY(()) machine_function
123 {
124   /* Some local-dynamic symbol.  */
125   const char *some_ld_name;
126   /* Whether the instruction chain has been scanned already.  */
127   int insn_chain_scanned_p;
128   /* Flags if __builtin_return_address (n) with n >= 1 was used.  */
129   int ra_needs_full_frame;
130   /* Flags if __builtin_return_address (0) was used.  */
131   int ra_need_lr;
132   /* Cache lr_save_p after expansion of builtin_eh_return.  */
133   int lr_save_state;
134   /* Whether we need to save the TOC to the reserved stack location in the
135      function prologue.  */
136   bool save_toc_in_prologue;
137   /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
138      varargs save area.  */
139   HOST_WIDE_INT varargs_save_offset;
140   /* Temporary stack slot to use for SDmode copies.  This slot is
141      64-bits wide and is allocated early enough so that the offset
142      does not overflow the 16-bit load/store offset field.  */
143   rtx sdmode_stack_slot;
144 } machine_function;
145
146 /* Support targetm.vectorize.builtin_mask_for_load.  */
147 static GTY(()) tree altivec_builtin_mask_for_load;
148
149 /* Set to nonzero once AIX common-mode calls have been defined.  */
150 static GTY(()) int common_mode_defined;
151
152 /* Label number of label created for -mrelocatable, to call to so we can
153    get the address of the GOT section */
154 static int rs6000_pic_labelno;
155
156 #ifdef USING_ELFOS_H
157 /* Counter for labels which are to be placed in .fixup.  */
158 int fixuplabelno = 0;
159 #endif
160
161 /* Whether to use variant of AIX ABI for PowerPC64 Linux.  */
162 int dot_symbols;
163
164 /* Specify the machine mode that pointers have.  After generation of rtl, the
165    compiler makes no further distinction between pointers and any other objects
166    of this machine mode.  The type is unsigned since not all things that
167    include rs6000.h also include machmode.h.  */
168 unsigned rs6000_pmode;
169
170 /* Width in bits of a pointer.  */
171 unsigned rs6000_pointer_size;
172
173 #ifdef HAVE_AS_GNU_ATTRIBUTE
174 /* Flag whether floating point values have been passed/returned.  */
175 static bool rs6000_passes_float;
176 /* Flag whether vector values have been passed/returned.  */
177 static bool rs6000_passes_vector;
178 /* Flag whether small (<= 8 byte) structures have been returned.  */
179 static bool rs6000_returns_struct;
180 #endif
181
182 /* Value is TRUE if register/mode pair is acceptable.  */
183 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
184
185 /* Maximum number of registers needed for a given register class and mode.  */
186 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
187
188 /* How many registers are needed for a given register and mode.  */
189 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
190
191 /* Map register number to register class.  */
192 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
193
194 /* Reload functions based on the type and the vector unit.  */
195 static enum insn_code rs6000_vector_reload[NUM_MACHINE_MODES][2];
196
197 static int dbg_cost_ctrl;
198
199 /* Built in types.  */
200 tree rs6000_builtin_types[RS6000_BTI_MAX];
201 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
202
203 /* Flag to say the TOC is initialized */
204 int toc_initialized;
205 char toc_label_name[10];
206
207 /* Cached value of rs6000_variable_issue. This is cached in
208    rs6000_variable_issue hook and returned from rs6000_sched_reorder2.  */
209 static short cached_can_issue_more;
210
211 static GTY(()) section *read_only_data_section;
212 static GTY(()) section *private_data_section;
213 static GTY(()) section *read_only_private_data_section;
214 static GTY(()) section *sdata2_section;
215 static GTY(()) section *toc_section;
216
217 struct builtin_description
218 {
219   const unsigned int mask;
220   const enum insn_code icode;
221   const char *const name;
222   const enum rs6000_builtins code;
223 };
224
225 /* Describe the vector unit used for modes.  */
226 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
227 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
228
229 /* Register classes for various constraints that are based on the target
230    switches.  */
231 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
232
233 /* Describe the alignment of a vector.  */
234 int rs6000_vector_align[NUM_MACHINE_MODES];
235
236 /* Map selected modes to types for builtins.  */
237 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
238
239 /* What modes to automatically generate reciprocal divide estimate (fre) and
240    reciprocal sqrt (frsqrte) for.  */
241 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
242
243 /* Masks to determine which reciprocal esitmate instructions to generate
244    automatically.  */
245 enum rs6000_recip_mask {
246   RECIP_SF_DIV          = 0x001,        /* Use divide estimate */
247   RECIP_DF_DIV          = 0x002,
248   RECIP_V4SF_DIV        = 0x004,
249   RECIP_V2DF_DIV        = 0x008,
250
251   RECIP_SF_RSQRT        = 0x010,        /* Use reciprocal sqrt estimate.  */
252   RECIP_DF_RSQRT        = 0x020,
253   RECIP_V4SF_RSQRT      = 0x040,
254   RECIP_V2DF_RSQRT      = 0x080,
255
256   /* Various combination of flags for -mrecip=xxx.  */
257   RECIP_NONE            = 0,
258   RECIP_ALL             = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
259                            | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
260                            | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
261
262   RECIP_HIGH_PRECISION  = RECIP_ALL,
263
264   /* On low precision machines like the power5, don't enable double precision
265      reciprocal square root estimate, since it isn't accurate enough.  */
266   RECIP_LOW_PRECISION   = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
267 };
268
269 /* -mrecip options.  */
270 static struct
271 {
272   const char *string;           /* option name */
273   unsigned int mask;            /* mask bits to set */
274 } recip_options[] = {
275   { "all",       RECIP_ALL },
276   { "none",      RECIP_NONE },
277   { "div",       (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
278                   | RECIP_V2DF_DIV) },
279   { "divf",      (RECIP_SF_DIV | RECIP_V4SF_DIV) },
280   { "divd",      (RECIP_DF_DIV | RECIP_V2DF_DIV) },
281   { "rsqrt",     (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
282                   | RECIP_V2DF_RSQRT) },
283   { "rsqrtf",    (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
284   { "rsqrtd",    (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
285 };
286
287 /* 2 argument gen function typedef.  */
288 typedef rtx (*gen_2arg_fn_t) (rtx, rtx, rtx);
289
290 /* Pointer to function (in rs6000-c.c) that can define or undefine target
291    macros that have changed.  Languages that don't support the preprocessor
292    don't link in rs6000-c.c, so we can't call it directly.  */
293 void (*rs6000_target_modify_macros_ptr) (bool, int, unsigned);
294
295 \f
296 /* Target cpu costs.  */
297
298 struct processor_costs {
299   const int mulsi;        /* cost of SImode multiplication.  */
300   const int mulsi_const;  /* cost of SImode multiplication by constant.  */
301   const int mulsi_const9; /* cost of SImode mult by short constant.  */
302   const int muldi;        /* cost of DImode multiplication.  */
303   const int divsi;        /* cost of SImode division.  */
304   const int divdi;        /* cost of DImode division.  */
305   const int fp;           /* cost of simple SFmode and DFmode insns.  */
306   const int dmul;         /* cost of DFmode multiplication (and fmadd).  */
307   const int sdiv;         /* cost of SFmode division (fdivs).  */
308   const int ddiv;         /* cost of DFmode division (fdiv).  */
309   const int cache_line_size;    /* cache line size in bytes. */
310   const int l1_cache_size;      /* size of l1 cache, in kilobytes.  */
311   const int l2_cache_size;      /* size of l2 cache, in kilobytes.  */
312   const int simultaneous_prefetches; /* number of parallel prefetch
313                                         operations.  */
314 };
315
316 const struct processor_costs *rs6000_cost;
317
318 /* Processor costs (relative to an add) */
319
320 /* Instruction size costs on 32bit processors.  */
321 static const
322 struct processor_costs size32_cost = {
323   COSTS_N_INSNS (1),    /* mulsi */
324   COSTS_N_INSNS (1),    /* mulsi_const */
325   COSTS_N_INSNS (1),    /* mulsi_const9 */
326   COSTS_N_INSNS (1),    /* muldi */
327   COSTS_N_INSNS (1),    /* divsi */
328   COSTS_N_INSNS (1),    /* divdi */
329   COSTS_N_INSNS (1),    /* fp */
330   COSTS_N_INSNS (1),    /* dmul */
331   COSTS_N_INSNS (1),    /* sdiv */
332   COSTS_N_INSNS (1),    /* ddiv */
333   32,
334   0,
335   0,
336   0,
337 };
338
339 /* Instruction size costs on 64bit processors.  */
340 static const
341 struct processor_costs size64_cost = {
342   COSTS_N_INSNS (1),    /* mulsi */
343   COSTS_N_INSNS (1),    /* mulsi_const */
344   COSTS_N_INSNS (1),    /* mulsi_const9 */
345   COSTS_N_INSNS (1),    /* muldi */
346   COSTS_N_INSNS (1),    /* divsi */
347   COSTS_N_INSNS (1),    /* divdi */
348   COSTS_N_INSNS (1),    /* fp */
349   COSTS_N_INSNS (1),    /* dmul */
350   COSTS_N_INSNS (1),    /* sdiv */
351   COSTS_N_INSNS (1),    /* ddiv */
352   128,
353   0,
354   0,
355   0,
356 };
357
358 /* Instruction costs on RIOS1 processors.  */
359 static const
360 struct processor_costs rios1_cost = {
361   COSTS_N_INSNS (5),    /* mulsi */
362   COSTS_N_INSNS (4),    /* mulsi_const */
363   COSTS_N_INSNS (3),    /* mulsi_const9 */
364   COSTS_N_INSNS (5),    /* muldi */
365   COSTS_N_INSNS (19),   /* divsi */
366   COSTS_N_INSNS (19),   /* divdi */
367   COSTS_N_INSNS (2),    /* fp */
368   COSTS_N_INSNS (2),    /* dmul */
369   COSTS_N_INSNS (19),   /* sdiv */
370   COSTS_N_INSNS (19),   /* ddiv */
371   128,                  /* cache line size */
372   64,                   /* l1 cache */
373   512,                  /* l2 cache */
374   0,                    /* streams */
375 };
376
377 /* Instruction costs on RIOS2 processors.  */
378 static const
379 struct processor_costs rios2_cost = {
380   COSTS_N_INSNS (2),    /* mulsi */
381   COSTS_N_INSNS (2),    /* mulsi_const */
382   COSTS_N_INSNS (2),    /* mulsi_const9 */
383   COSTS_N_INSNS (2),    /* muldi */
384   COSTS_N_INSNS (13),   /* divsi */
385   COSTS_N_INSNS (13),   /* divdi */
386   COSTS_N_INSNS (2),    /* fp */
387   COSTS_N_INSNS (2),    /* dmul */
388   COSTS_N_INSNS (17),   /* sdiv */
389   COSTS_N_INSNS (17),   /* ddiv */
390   256,                  /* cache line size */
391   256,                  /* l1 cache */
392   1024,                 /* l2 cache */
393   0,                    /* streams */
394 };
395
396 /* Instruction costs on RS64A processors.  */
397 static const
398 struct processor_costs rs64a_cost = {
399   COSTS_N_INSNS (20),   /* mulsi */
400   COSTS_N_INSNS (12),   /* mulsi_const */
401   COSTS_N_INSNS (8),    /* mulsi_const9 */
402   COSTS_N_INSNS (34),   /* muldi */
403   COSTS_N_INSNS (65),   /* divsi */
404   COSTS_N_INSNS (67),   /* divdi */
405   COSTS_N_INSNS (4),    /* fp */
406   COSTS_N_INSNS (4),    /* dmul */
407   COSTS_N_INSNS (31),   /* sdiv */
408   COSTS_N_INSNS (31),   /* ddiv */
409   128,                  /* cache line size */
410   128,                  /* l1 cache */
411   2048,                 /* l2 cache */
412   1,                    /* streams */
413 };
414
415 /* Instruction costs on MPCCORE processors.  */
416 static const
417 struct processor_costs mpccore_cost = {
418   COSTS_N_INSNS (2),    /* mulsi */
419   COSTS_N_INSNS (2),    /* mulsi_const */
420   COSTS_N_INSNS (2),    /* mulsi_const9 */
421   COSTS_N_INSNS (2),    /* muldi */
422   COSTS_N_INSNS (6),    /* divsi */
423   COSTS_N_INSNS (6),    /* divdi */
424   COSTS_N_INSNS (4),    /* fp */
425   COSTS_N_INSNS (5),    /* dmul */
426   COSTS_N_INSNS (10),   /* sdiv */
427   COSTS_N_INSNS (17),   /* ddiv */
428   32,                   /* cache line size */
429   4,                    /* l1 cache */
430   16,                   /* l2 cache */
431   1,                    /* streams */
432 };
433
434 /* Instruction costs on PPC403 processors.  */
435 static const
436 struct processor_costs ppc403_cost = {
437   COSTS_N_INSNS (4),    /* mulsi */
438   COSTS_N_INSNS (4),    /* mulsi_const */
439   COSTS_N_INSNS (4),    /* mulsi_const9 */
440   COSTS_N_INSNS (4),    /* muldi */
441   COSTS_N_INSNS (33),   /* divsi */
442   COSTS_N_INSNS (33),   /* divdi */
443   COSTS_N_INSNS (11),   /* fp */
444   COSTS_N_INSNS (11),   /* dmul */
445   COSTS_N_INSNS (11),   /* sdiv */
446   COSTS_N_INSNS (11),   /* ddiv */
447   32,                   /* cache line size */
448   4,                    /* l1 cache */
449   16,                   /* l2 cache */
450   1,                    /* streams */
451 };
452
453 /* Instruction costs on PPC405 processors.  */
454 static const
455 struct processor_costs ppc405_cost = {
456   COSTS_N_INSNS (5),    /* mulsi */
457   COSTS_N_INSNS (4),    /* mulsi_const */
458   COSTS_N_INSNS (3),    /* mulsi_const9 */
459   COSTS_N_INSNS (5),    /* muldi */
460   COSTS_N_INSNS (35),   /* divsi */
461   COSTS_N_INSNS (35),   /* divdi */
462   COSTS_N_INSNS (11),   /* fp */
463   COSTS_N_INSNS (11),   /* dmul */
464   COSTS_N_INSNS (11),   /* sdiv */
465   COSTS_N_INSNS (11),   /* ddiv */
466   32,                   /* cache line size */
467   16,                   /* l1 cache */
468   128,                  /* l2 cache */
469   1,                    /* streams */
470 };
471
472 /* Instruction costs on PPC440 processors.  */
473 static const
474 struct processor_costs ppc440_cost = {
475   COSTS_N_INSNS (3),    /* mulsi */
476   COSTS_N_INSNS (2),    /* mulsi_const */
477   COSTS_N_INSNS (2),    /* mulsi_const9 */
478   COSTS_N_INSNS (3),    /* muldi */
479   COSTS_N_INSNS (34),   /* divsi */
480   COSTS_N_INSNS (34),   /* divdi */
481   COSTS_N_INSNS (5),    /* fp */
482   COSTS_N_INSNS (5),    /* dmul */
483   COSTS_N_INSNS (19),   /* sdiv */
484   COSTS_N_INSNS (33),   /* ddiv */
485   32,                   /* cache line size */
486   32,                   /* l1 cache */
487   256,                  /* l2 cache */
488   1,                    /* streams */
489 };
490
491 /* Instruction costs on PPC476 processors.  */
492 static const
493 struct processor_costs ppc476_cost = {
494   COSTS_N_INSNS (4),    /* mulsi */
495   COSTS_N_INSNS (4),    /* mulsi_const */
496   COSTS_N_INSNS (4),    /* mulsi_const9 */
497   COSTS_N_INSNS (4),    /* muldi */
498   COSTS_N_INSNS (11),   /* divsi */
499   COSTS_N_INSNS (11),   /* divdi */
500   COSTS_N_INSNS (6),    /* fp */
501   COSTS_N_INSNS (6),    /* dmul */
502   COSTS_N_INSNS (19),   /* sdiv */
503   COSTS_N_INSNS (33),   /* ddiv */
504   32,                   /* l1 cache line size */
505   32,                   /* l1 cache */
506   512,                  /* l2 cache */
507   1,                    /* streams */
508 };
509
510 /* Instruction costs on PPC601 processors.  */
511 static const
512 struct processor_costs ppc601_cost = {
513   COSTS_N_INSNS (5),    /* mulsi */
514   COSTS_N_INSNS (5),    /* mulsi_const */
515   COSTS_N_INSNS (5),    /* mulsi_const9 */
516   COSTS_N_INSNS (5),    /* muldi */
517   COSTS_N_INSNS (36),   /* divsi */
518   COSTS_N_INSNS (36),   /* divdi */
519   COSTS_N_INSNS (4),    /* fp */
520   COSTS_N_INSNS (5),    /* dmul */
521   COSTS_N_INSNS (17),   /* sdiv */
522   COSTS_N_INSNS (31),   /* ddiv */
523   32,                   /* cache line size */
524   32,                   /* l1 cache */
525   256,                  /* l2 cache */
526   1,                    /* streams */
527 };
528
529 /* Instruction costs on PPC603 processors.  */
530 static const
531 struct processor_costs ppc603_cost = {
532   COSTS_N_INSNS (5),    /* mulsi */
533   COSTS_N_INSNS (3),    /* mulsi_const */
534   COSTS_N_INSNS (2),    /* mulsi_const9 */
535   COSTS_N_INSNS (5),    /* muldi */
536   COSTS_N_INSNS (37),   /* divsi */
537   COSTS_N_INSNS (37),   /* divdi */
538   COSTS_N_INSNS (3),    /* fp */
539   COSTS_N_INSNS (4),    /* dmul */
540   COSTS_N_INSNS (18),   /* sdiv */
541   COSTS_N_INSNS (33),   /* ddiv */
542   32,                   /* cache line size */
543   8,                    /* l1 cache */
544   64,                   /* l2 cache */
545   1,                    /* streams */
546 };
547
548 /* Instruction costs on PPC604 processors.  */
549 static const
550 struct processor_costs ppc604_cost = {
551   COSTS_N_INSNS (4),    /* mulsi */
552   COSTS_N_INSNS (4),    /* mulsi_const */
553   COSTS_N_INSNS (4),    /* mulsi_const9 */
554   COSTS_N_INSNS (4),    /* muldi */
555   COSTS_N_INSNS (20),   /* divsi */
556   COSTS_N_INSNS (20),   /* divdi */
557   COSTS_N_INSNS (3),    /* fp */
558   COSTS_N_INSNS (3),    /* dmul */
559   COSTS_N_INSNS (18),   /* sdiv */
560   COSTS_N_INSNS (32),   /* ddiv */
561   32,                   /* cache line size */
562   16,                   /* l1 cache */
563   512,                  /* l2 cache */
564   1,                    /* streams */
565 };
566
567 /* Instruction costs on PPC604e processors.  */
568 static const
569 struct processor_costs ppc604e_cost = {
570   COSTS_N_INSNS (2),    /* mulsi */
571   COSTS_N_INSNS (2),    /* mulsi_const */
572   COSTS_N_INSNS (2),    /* mulsi_const9 */
573   COSTS_N_INSNS (2),    /* muldi */
574   COSTS_N_INSNS (20),   /* divsi */
575   COSTS_N_INSNS (20),   /* divdi */
576   COSTS_N_INSNS (3),    /* fp */
577   COSTS_N_INSNS (3),    /* dmul */
578   COSTS_N_INSNS (18),   /* sdiv */
579   COSTS_N_INSNS (32),   /* ddiv */
580   32,                   /* cache line size */
581   32,                   /* l1 cache */
582   1024,                 /* l2 cache */
583   1,                    /* streams */
584 };
585
586 /* Instruction costs on PPC620 processors.  */
587 static const
588 struct processor_costs ppc620_cost = {
589   COSTS_N_INSNS (5),    /* mulsi */
590   COSTS_N_INSNS (4),    /* mulsi_const */
591   COSTS_N_INSNS (3),    /* mulsi_const9 */
592   COSTS_N_INSNS (7),    /* muldi */
593   COSTS_N_INSNS (21),   /* divsi */
594   COSTS_N_INSNS (37),   /* divdi */
595   COSTS_N_INSNS (3),    /* fp */
596   COSTS_N_INSNS (3),    /* dmul */
597   COSTS_N_INSNS (18),   /* sdiv */
598   COSTS_N_INSNS (32),   /* ddiv */
599   128,                  /* cache line size */
600   32,                   /* l1 cache */
601   1024,                 /* l2 cache */
602   1,                    /* streams */
603 };
604
605 /* Instruction costs on PPC630 processors.  */
606 static const
607 struct processor_costs ppc630_cost = {
608   COSTS_N_INSNS (5),    /* mulsi */
609   COSTS_N_INSNS (4),    /* mulsi_const */
610   COSTS_N_INSNS (3),    /* mulsi_const9 */
611   COSTS_N_INSNS (7),    /* muldi */
612   COSTS_N_INSNS (21),   /* divsi */
613   COSTS_N_INSNS (37),   /* divdi */
614   COSTS_N_INSNS (3),    /* fp */
615   COSTS_N_INSNS (3),    /* dmul */
616   COSTS_N_INSNS (17),   /* sdiv */
617   COSTS_N_INSNS (21),   /* ddiv */
618   128,                  /* cache line size */
619   64,                   /* l1 cache */
620   1024,                 /* l2 cache */
621   1,                    /* streams */
622 };
623
624 /* Instruction costs on Cell processor.  */
625 /* COSTS_N_INSNS (1) ~ one add.  */
626 static const
627 struct processor_costs ppccell_cost = {
628   COSTS_N_INSNS (9/2)+2,    /* mulsi */
629   COSTS_N_INSNS (6/2),    /* mulsi_const */
630   COSTS_N_INSNS (6/2),    /* mulsi_const9 */
631   COSTS_N_INSNS (15/2)+2,   /* muldi */
632   COSTS_N_INSNS (38/2),   /* divsi */
633   COSTS_N_INSNS (70/2),   /* divdi */
634   COSTS_N_INSNS (10/2),   /* fp */
635   COSTS_N_INSNS (10/2),   /* dmul */
636   COSTS_N_INSNS (74/2),   /* sdiv */
637   COSTS_N_INSNS (74/2),   /* ddiv */
638   128,                  /* cache line size */
639   32,                   /* l1 cache */
640   512,                  /* l2 cache */
641   6,                    /* streams */
642 };
643
644 /* Instruction costs on PPC750 and PPC7400 processors.  */
645 static const
646 struct processor_costs ppc750_cost = {
647   COSTS_N_INSNS (5),    /* mulsi */
648   COSTS_N_INSNS (3),    /* mulsi_const */
649   COSTS_N_INSNS (2),    /* mulsi_const9 */
650   COSTS_N_INSNS (5),    /* muldi */
651   COSTS_N_INSNS (17),   /* divsi */
652   COSTS_N_INSNS (17),   /* divdi */
653   COSTS_N_INSNS (3),    /* fp */
654   COSTS_N_INSNS (3),    /* dmul */
655   COSTS_N_INSNS (17),   /* sdiv */
656   COSTS_N_INSNS (31),   /* ddiv */
657   32,                   /* cache line size */
658   32,                   /* l1 cache */
659   512,                  /* l2 cache */
660   1,                    /* streams */
661 };
662
663 /* Instruction costs on PPC7450 processors.  */
664 static const
665 struct processor_costs ppc7450_cost = {
666   COSTS_N_INSNS (4),    /* mulsi */
667   COSTS_N_INSNS (3),    /* mulsi_const */
668   COSTS_N_INSNS (3),    /* mulsi_const9 */
669   COSTS_N_INSNS (4),    /* muldi */
670   COSTS_N_INSNS (23),   /* divsi */
671   COSTS_N_INSNS (23),   /* divdi */
672   COSTS_N_INSNS (5),    /* fp */
673   COSTS_N_INSNS (5),    /* dmul */
674   COSTS_N_INSNS (21),   /* sdiv */
675   COSTS_N_INSNS (35),   /* ddiv */
676   32,                   /* cache line size */
677   32,                   /* l1 cache */
678   1024,                 /* l2 cache */
679   1,                    /* streams */
680 };
681
682 /* Instruction costs on PPC8540 processors.  */
683 static const
684 struct processor_costs ppc8540_cost = {
685   COSTS_N_INSNS (4),    /* mulsi */
686   COSTS_N_INSNS (4),    /* mulsi_const */
687   COSTS_N_INSNS (4),    /* mulsi_const9 */
688   COSTS_N_INSNS (4),    /* muldi */
689   COSTS_N_INSNS (19),   /* divsi */
690   COSTS_N_INSNS (19),   /* divdi */
691   COSTS_N_INSNS (4),    /* fp */
692   COSTS_N_INSNS (4),    /* dmul */
693   COSTS_N_INSNS (29),   /* sdiv */
694   COSTS_N_INSNS (29),   /* ddiv */
695   32,                   /* cache line size */
696   32,                   /* l1 cache */
697   256,                  /* l2 cache */
698   1,                    /* prefetch streams /*/
699 };
700
701 /* Instruction costs on E300C2 and E300C3 cores.  */
702 static const
703 struct processor_costs ppce300c2c3_cost = {
704   COSTS_N_INSNS (4),    /* mulsi */
705   COSTS_N_INSNS (4),    /* mulsi_const */
706   COSTS_N_INSNS (4),    /* mulsi_const9 */
707   COSTS_N_INSNS (4),    /* muldi */
708   COSTS_N_INSNS (19),   /* divsi */
709   COSTS_N_INSNS (19),   /* divdi */
710   COSTS_N_INSNS (3),    /* fp */
711   COSTS_N_INSNS (4),    /* dmul */
712   COSTS_N_INSNS (18),   /* sdiv */
713   COSTS_N_INSNS (33),   /* ddiv */
714   32,
715   16,                   /* l1 cache */
716   16,                   /* l2 cache */
717   1,                    /* prefetch streams /*/
718 };
719
720 /* Instruction costs on PPCE500MC processors.  */
721 static const
722 struct processor_costs ppce500mc_cost = {
723   COSTS_N_INSNS (4),    /* mulsi */
724   COSTS_N_INSNS (4),    /* mulsi_const */
725   COSTS_N_INSNS (4),    /* mulsi_const9 */
726   COSTS_N_INSNS (4),    /* muldi */
727   COSTS_N_INSNS (14),   /* divsi */
728   COSTS_N_INSNS (14),   /* divdi */
729   COSTS_N_INSNS (8),    /* fp */
730   COSTS_N_INSNS (10),   /* dmul */
731   COSTS_N_INSNS (36),   /* sdiv */
732   COSTS_N_INSNS (66),   /* ddiv */
733   64,                   /* cache line size */
734   32,                   /* l1 cache */
735   128,                  /* l2 cache */
736   1,                    /* prefetch streams /*/
737 };
738
739 /* Instruction costs on PPCE500MC64 processors.  */
740 static const
741 struct processor_costs ppce500mc64_cost = {
742   COSTS_N_INSNS (4),    /* mulsi */
743   COSTS_N_INSNS (4),    /* mulsi_const */
744   COSTS_N_INSNS (4),    /* mulsi_const9 */
745   COSTS_N_INSNS (4),    /* muldi */
746   COSTS_N_INSNS (14),   /* divsi */
747   COSTS_N_INSNS (14),   /* divdi */
748   COSTS_N_INSNS (4),    /* fp */
749   COSTS_N_INSNS (10),   /* dmul */
750   COSTS_N_INSNS (36),   /* sdiv */
751   COSTS_N_INSNS (66),   /* ddiv */
752   64,                   /* cache line size */
753   32,                   /* l1 cache */
754   128,                  /* l2 cache */
755   1,                    /* prefetch streams /*/
756 };
757
758 /* Instruction costs on AppliedMicro Titan processors.  */
759 static const
760 struct processor_costs titan_cost = {
761   COSTS_N_INSNS (5),    /* mulsi */
762   COSTS_N_INSNS (5),    /* mulsi_const */
763   COSTS_N_INSNS (5),    /* mulsi_const9 */
764   COSTS_N_INSNS (5),    /* muldi */
765   COSTS_N_INSNS (18),   /* divsi */
766   COSTS_N_INSNS (18),   /* divdi */
767   COSTS_N_INSNS (10),   /* fp */
768   COSTS_N_INSNS (10),   /* dmul */
769   COSTS_N_INSNS (46),   /* sdiv */
770   COSTS_N_INSNS (72),   /* ddiv */
771   32,                   /* cache line size */
772   32,                   /* l1 cache */
773   512,                  /* l2 cache */
774   1,                    /* prefetch streams /*/
775 };
776
777 /* Instruction costs on POWER4 and POWER5 processors.  */
778 static const
779 struct processor_costs power4_cost = {
780   COSTS_N_INSNS (3),    /* mulsi */
781   COSTS_N_INSNS (2),    /* mulsi_const */
782   COSTS_N_INSNS (2),    /* mulsi_const9 */
783   COSTS_N_INSNS (4),    /* muldi */
784   COSTS_N_INSNS (18),   /* divsi */
785   COSTS_N_INSNS (34),   /* divdi */
786   COSTS_N_INSNS (3),    /* fp */
787   COSTS_N_INSNS (3),    /* dmul */
788   COSTS_N_INSNS (17),   /* sdiv */
789   COSTS_N_INSNS (17),   /* ddiv */
790   128,                  /* cache line size */
791   32,                   /* l1 cache */
792   1024,                 /* l2 cache */
793   8,                    /* prefetch streams /*/
794 };
795
796 /* Instruction costs on POWER6 processors.  */
797 static const
798 struct processor_costs power6_cost = {
799   COSTS_N_INSNS (8),    /* mulsi */
800   COSTS_N_INSNS (8),    /* mulsi_const */
801   COSTS_N_INSNS (8),    /* mulsi_const9 */
802   COSTS_N_INSNS (8),    /* muldi */
803   COSTS_N_INSNS (22),   /* divsi */
804   COSTS_N_INSNS (28),   /* divdi */
805   COSTS_N_INSNS (3),    /* fp */
806   COSTS_N_INSNS (3),    /* dmul */
807   COSTS_N_INSNS (13),   /* sdiv */
808   COSTS_N_INSNS (16),   /* ddiv */
809   128,                  /* cache line size */
810   64,                   /* l1 cache */
811   2048,                 /* l2 cache */
812   16,                   /* prefetch streams */
813 };
814
815 /* Instruction costs on POWER7 processors.  */
816 static const
817 struct processor_costs power7_cost = {
818   COSTS_N_INSNS (2),    /* mulsi */
819   COSTS_N_INSNS (2),    /* mulsi_const */
820   COSTS_N_INSNS (2),    /* mulsi_const9 */
821   COSTS_N_INSNS (2),    /* muldi */
822   COSTS_N_INSNS (18),   /* divsi */
823   COSTS_N_INSNS (34),   /* divdi */
824   COSTS_N_INSNS (3),    /* fp */
825   COSTS_N_INSNS (3),    /* dmul */
826   COSTS_N_INSNS (13),   /* sdiv */
827   COSTS_N_INSNS (16),   /* ddiv */
828   128,                  /* cache line size */
829   32,                   /* l1 cache */
830   256,                  /* l2 cache */
831   12,                   /* prefetch streams */
832 };
833
834 /* Instruction costs on POWER A2 processors.  */
835 static const
836 struct processor_costs ppca2_cost = {
837   COSTS_N_INSNS (16),    /* mulsi */
838   COSTS_N_INSNS (16),    /* mulsi_const */
839   COSTS_N_INSNS (16),    /* mulsi_const9 */
840   COSTS_N_INSNS (16),   /* muldi */
841   COSTS_N_INSNS (22),   /* divsi */
842   COSTS_N_INSNS (28),   /* divdi */
843   COSTS_N_INSNS (3),    /* fp */
844   COSTS_N_INSNS (3),    /* dmul */
845   COSTS_N_INSNS (59),   /* sdiv */
846   COSTS_N_INSNS (72),   /* ddiv */
847   64,
848   16,                   /* l1 cache */
849   2048,                 /* l2 cache */
850   16,                   /* prefetch streams */
851 };
852
853 \f
854 /* Table that classifies rs6000 builtin functions (pure, const, etc.).  */
855 #undef RS6000_BUILTIN_1
856 #undef RS6000_BUILTIN_2
857 #undef RS6000_BUILTIN_3
858 #undef RS6000_BUILTIN_A
859 #undef RS6000_BUILTIN_D
860 #undef RS6000_BUILTIN_E
861 #undef RS6000_BUILTIN_P
862 #undef RS6000_BUILTIN_Q
863 #undef RS6000_BUILTIN_S
864 #undef RS6000_BUILTIN_X
865
866 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
867   { NAME, ICODE, MASK, ATTR },
868
869 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)  \
870   { NAME, ICODE, MASK, ATTR },
871
872 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)  \
873   { NAME, ICODE, MASK, ATTR },
874
875 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)  \
876   { NAME, ICODE, MASK, ATTR },
877
878 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)  \
879   { NAME, ICODE, MASK, ATTR },
880
881 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)  \
882   { NAME, ICODE, MASK, ATTR },
883
884 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)  \
885   { NAME, ICODE, MASK, ATTR },
886
887 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)  \
888   { NAME, ICODE, MASK, ATTR },
889
890 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)  \
891   { NAME, ICODE, MASK, ATTR },
892
893 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)  \
894   { NAME, ICODE, MASK, ATTR },
895
896 struct rs6000_builtin_info_type {
897   const char *name;
898   const enum insn_code icode;
899   const unsigned mask;
900   const unsigned attr;
901 };
902
903 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
904 {
905 #include "rs6000-builtin.def"
906 };
907
908 #undef RS6000_BUILTIN_1
909 #undef RS6000_BUILTIN_2
910 #undef RS6000_BUILTIN_3
911 #undef RS6000_BUILTIN_A
912 #undef RS6000_BUILTIN_D
913 #undef RS6000_BUILTIN_E
914 #undef RS6000_BUILTIN_P
915 #undef RS6000_BUILTIN_Q
916 #undef RS6000_BUILTIN_S
917 #undef RS6000_BUILTIN_X
918
919 /* Support for -mveclibabi=<xxx> to control which vector library to use.  */
920 static tree (*rs6000_veclib_handler) (tree, tree, tree);
921
922 \f
923 static bool rs6000_function_ok_for_sibcall (tree, tree);
924 static const char *rs6000_invalid_within_doloop (const_rtx);
925 static bool rs6000_legitimate_address_p (enum machine_mode, rtx, bool);
926 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
927 static rtx rs6000_generate_compare (rtx, enum machine_mode);
928 static void rs6000_emit_stack_tie (void);
929 static bool spe_func_has_64bit_regs_p (void);
930 static rtx gen_frame_mem_offset (enum machine_mode, rtx, int);
931 static unsigned rs6000_hash_constant (rtx);
932 static unsigned toc_hash_function (const void *);
933 static int toc_hash_eq (const void *, const void *);
934 static bool reg_offset_addressing_ok_p (enum machine_mode);
935 static bool virtual_stack_registers_memory_p (rtx);
936 static bool constant_pool_expr_p (rtx);
937 static bool legitimate_small_data_p (enum machine_mode, rtx);
938 static bool legitimate_lo_sum_address_p (enum machine_mode, rtx, int);
939 static struct machine_function * rs6000_init_machine_status (void);
940 static bool rs6000_assemble_integer (rtx, unsigned int, int);
941 static bool no_global_regs_above (int, bool);
942 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
943 static void rs6000_assemble_visibility (tree, int);
944 #endif
945 static int rs6000_ra_ever_killed (void);
946 static bool rs6000_attribute_takes_identifier_p (const_tree);
947 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
948 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
949 static bool rs6000_ms_bitfield_layout_p (const_tree);
950 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
951 static void rs6000_eliminate_indexed_memrefs (rtx operands[2]);
952 static const char *rs6000_mangle_type (const_tree);
953 static void rs6000_set_default_type_attributes (tree);
954 static rtx rs6000_savres_routine_sym (rs6000_stack_t *, bool, bool, bool);
955 static rtx rs6000_emit_stack_reset (rs6000_stack_t *, rtx, rtx, int, bool);
956 static bool rs6000_reg_live_or_pic_offset_p (int);
957 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
958 static tree rs6000_builtin_vectorized_function (tree, tree, tree);
959 static void rs6000_restore_saved_cr (rtx, int);
960 static bool rs6000_output_addr_const_extra (FILE *, rtx);
961 static void rs6000_output_function_prologue (FILE *, HOST_WIDE_INT);
962 static void rs6000_output_function_epilogue (FILE *, HOST_WIDE_INT);
963 static void rs6000_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
964                                     tree);
965 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
966 static bool rs6000_return_in_memory (const_tree, const_tree);
967 static rtx rs6000_function_value (const_tree, const_tree, bool);
968 static void rs6000_file_start (void);
969 #if TARGET_ELF
970 static int rs6000_elf_reloc_rw_mask (void);
971 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
972 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
973 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
974 static void rs6000_elf_asm_init_sections (void);
975 static section *rs6000_elf_select_rtx_section (enum machine_mode, rtx,
976                                                unsigned HOST_WIDE_INT);
977 static void rs6000_elf_encode_section_info (tree, rtx, int)
978      ATTRIBUTE_UNUSED;
979 #endif
980 static bool rs6000_use_blocks_for_constant_p (enum machine_mode, const_rtx);
981 static void rs6000_alloc_sdmode_stack_slot (void);
982 static void rs6000_instantiate_decls (void);
983 #if TARGET_XCOFF
984 static void rs6000_xcoff_asm_output_anchor (rtx);
985 static void rs6000_xcoff_asm_globalize_label (FILE *, const char *);
986 static void rs6000_xcoff_asm_init_sections (void);
987 static int rs6000_xcoff_reloc_rw_mask (void);
988 static void rs6000_xcoff_asm_named_section (const char *, unsigned int, tree);
989 static section *rs6000_xcoff_select_section (tree, int,
990                                              unsigned HOST_WIDE_INT);
991 static void rs6000_xcoff_unique_section (tree, int);
992 static section *rs6000_xcoff_select_rtx_section
993   (enum machine_mode, rtx, unsigned HOST_WIDE_INT);
994 static const char * rs6000_xcoff_strip_name_encoding (const char *);
995 static unsigned int rs6000_xcoff_section_type_flags (tree, const char *, int);
996 static void rs6000_xcoff_file_start (void);
997 static void rs6000_xcoff_file_end (void);
998 #endif
999 static int rs6000_variable_issue (FILE *, int, rtx, int);
1000 static int rs6000_register_move_cost (enum machine_mode,
1001                                       reg_class_t, reg_class_t);
1002 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
1003 static bool rs6000_rtx_costs (rtx, int, int, int, int *, bool);
1004 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
1005 static int rs6000_debug_address_cost (rtx, bool);
1006 static int rs6000_adjust_cost (rtx, rtx, rtx, int);
1007 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
1008 static void rs6000_sched_init (FILE *, int, int);
1009 static bool is_microcoded_insn (rtx);
1010 static bool is_nonpipeline_insn (rtx);
1011 static bool is_cracked_insn (rtx);
1012 static bool is_branch_slot_insn (rtx);
1013 static bool is_load_insn (rtx);
1014 static rtx get_store_dest (rtx pat);
1015 static bool is_store_insn (rtx);
1016 static bool set_to_load_agen (rtx,rtx);
1017 static bool adjacent_mem_locations (rtx,rtx);
1018 static int rs6000_adjust_priority (rtx, int);
1019 static int rs6000_issue_rate (void);
1020 static bool rs6000_is_costly_dependence (dep_t, int, int);
1021 static rtx get_next_active_insn (rtx, rtx);
1022 static bool insn_terminates_group_p (rtx , enum group_termination);
1023 static bool insn_must_be_first_in_group (rtx);
1024 static bool insn_must_be_last_in_group (rtx);
1025 static bool is_costly_group (rtx *, rtx);
1026 static int force_new_group (int, FILE *, rtx *, rtx, bool *, int, int *);
1027 static int redefine_groups (FILE *, int, rtx, rtx);
1028 static int pad_groups (FILE *, int, rtx, rtx);
1029 static void rs6000_sched_finish (FILE *, int);
1030 static int rs6000_sched_reorder (FILE *, int, rtx *, int *, int);
1031 static int rs6000_sched_reorder2 (FILE *, int, rtx *, int *, int);
1032 static int rs6000_use_sched_lookahead (void);
1033 static int rs6000_use_sched_lookahead_guard (rtx);
1034 static void * rs6000_alloc_sched_context (void);
1035 static void rs6000_init_sched_context (void *, bool);
1036 static void rs6000_set_sched_context (void *);
1037 static void rs6000_free_sched_context (void *);
1038 static tree rs6000_builtin_reciprocal (unsigned int, bool, bool);
1039 static tree rs6000_builtin_mask_for_load (void);
1040 static tree rs6000_builtin_mul_widen_even (tree);
1041 static tree rs6000_builtin_mul_widen_odd (tree);
1042 static bool rs6000_builtin_support_vector_misalignment (enum
1043                                                         machine_mode,
1044                                                         const_tree,
1045                                                         int, bool);
1046 static int rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt,
1047                                               tree, int);
1048 static enum machine_mode rs6000_preferred_simd_mode (enum machine_mode);
1049
1050 static void def_builtin (const char *, tree, enum rs6000_builtins);
1051 static bool rs6000_vector_alignment_reachable (const_tree, bool);
1052 static void rs6000_init_builtins (void);
1053 static tree rs6000_builtin_decl (unsigned, bool);
1054
1055 static rtx rs6000_expand_unop_builtin (enum insn_code, tree, rtx);
1056 static rtx rs6000_expand_binop_builtin (enum insn_code, tree, rtx);
1057 static rtx rs6000_expand_ternop_builtin (enum insn_code, tree, rtx);
1058 static rtx rs6000_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
1059 static void altivec_init_builtins (void);
1060 static unsigned builtin_hash_function (const void *);
1061 static int builtin_hash_eq (const void *, const void *);
1062 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1063                                    enum machine_mode, enum machine_mode,
1064                                    enum rs6000_builtins, const char *name);
1065 static void rs6000_common_init_builtins (void);
1066 static void rs6000_init_libfuncs (void);
1067
1068 static void paired_init_builtins (void);
1069 static rtx paired_expand_builtin (tree, rtx, bool *);
1070 static rtx paired_expand_lv_builtin (enum insn_code, tree, rtx);
1071 static rtx paired_expand_stv_builtin (enum insn_code, tree);
1072 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1073
1074 static void spe_init_builtins (void);
1075 static rtx spe_expand_builtin (tree, rtx, bool *);
1076 static rtx spe_expand_stv_builtin (enum insn_code, tree);
1077 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1078 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1079 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1080 static rs6000_stack_t *rs6000_stack_info (void);
1081 static void debug_stack_info (rs6000_stack_t *);
1082
1083 static rtx altivec_expand_builtin (tree, rtx, bool *);
1084 static rtx altivec_expand_ld_builtin (tree, rtx, bool *);
1085 static rtx altivec_expand_st_builtin (tree, rtx, bool *);
1086 static rtx altivec_expand_dst_builtin (tree, rtx, bool *);
1087 static rtx altivec_expand_abs_builtin (enum insn_code, tree, rtx);
1088 static rtx altivec_expand_predicate_builtin (enum insn_code, tree, rtx);
1089 static rtx altivec_expand_stv_builtin (enum insn_code, tree);
1090 static rtx altivec_expand_vec_init_builtin (tree, tree, rtx);
1091 static rtx altivec_expand_vec_set_builtin (tree);
1092 static rtx altivec_expand_vec_ext_builtin (tree, rtx);
1093 static int get_element_number (tree, tree);
1094 static void rs6000_option_override (void);
1095 static int rs6000_loop_align_max_skip (rtx);
1096 static int first_altivec_reg_to_save (void);
1097 static unsigned int compute_vrsave_mask (void);
1098 static void compute_save_world_info (rs6000_stack_t *info_ptr);
1099 static void is_altivec_return_reg (rtx, void *);
1100 static rtx generate_set_vrsave (rtx, rs6000_stack_t *, int);
1101 int easy_vector_constant (rtx, enum machine_mode);
1102 static rtx rs6000_dwarf_register_span (rtx);
1103 static void rs6000_init_dwarf_reg_sizes_extra (tree);
1104 static rtx rs6000_legitimize_address (rtx, rtx, enum machine_mode);
1105 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1106 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1107 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
1108 static rtx rs6000_delegitimize_address (rtx);
1109 static bool rs6000_const_not_ok_for_debug_p (rtx);
1110 static rtx rs6000_tls_get_addr (void);
1111 static rtx rs6000_got_sym (void);
1112 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1113 static const char *rs6000_get_some_local_dynamic_name (void);
1114 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1115 static rtx rs6000_complex_function_value (enum machine_mode);
1116 static rtx rs6000_spe_function_arg (const CUMULATIVE_ARGS *,
1117                                     enum machine_mode, const_tree);
1118 static void rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *,
1119                                                       HOST_WIDE_INT, int);
1120 static void rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *,
1121                                                         const_tree,
1122                                                         HOST_WIDE_INT);
1123 static void rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *,
1124                                               HOST_WIDE_INT,
1125                                               rtx[], int *);
1126 static void rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *,
1127                                                 const_tree, HOST_WIDE_INT,
1128                                                 rtx[], int *);
1129 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree, bool, bool);
1130 static rtx rs6000_mixed_function_arg (enum machine_mode, const_tree, int);
1131 static void rs6000_function_arg_advance (cumulative_args_t, enum machine_mode,
1132                                          const_tree, bool);
1133 static rtx rs6000_function_arg (cumulative_args_t, enum machine_mode,
1134                                 const_tree, bool);
1135 static unsigned int rs6000_function_arg_boundary (enum machine_mode,
1136                                                   const_tree);
1137 static void rs6000_move_block_from_reg (int regno, rtx x, int nregs);
1138 static void setup_incoming_varargs (cumulative_args_t,
1139                                     enum machine_mode, tree,
1140                                     int *, int);
1141 static bool rs6000_pass_by_reference (cumulative_args_t, enum machine_mode,
1142                                       const_tree, bool);
1143 static int rs6000_arg_partial_bytes (cumulative_args_t, enum machine_mode,
1144                                      tree, bool);
1145 static const char *invalid_arg_for_unprototyped_fn (const_tree, const_tree, const_tree);
1146 #if TARGET_MACHO
1147 static void macho_branch_islands (void);
1148 static int no_previous_def (tree function_name);
1149 static tree get_prev_label (tree function_name);
1150 static void rs6000_darwin_file_start (void);
1151 #endif
1152
1153 static tree rs6000_build_builtin_va_list (void);
1154 static void rs6000_va_start (tree, rtx);
1155 static tree rs6000_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
1156 static bool rs6000_must_pass_in_stack (enum machine_mode, const_tree);
1157 static bool rs6000_scalar_mode_supported_p (enum machine_mode);
1158 static bool rs6000_vector_mode_supported_p (enum machine_mode);
1159 static rtx rs6000_emit_vector_compare_inner (enum rtx_code, rtx, rtx);
1160 static rtx rs6000_emit_vector_compare (enum rtx_code, rtx, rtx,
1161                                        enum machine_mode);
1162 static tree rs6000_stack_protect_fail (void);
1163
1164 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1165                                              int, int *);
1166
1167 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1168                                                    int, int, int *);
1169
1170 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1171                                              int, int *)
1172   = rs6000_legitimize_reload_address;
1173
1174 static bool rs6000_mode_dependent_address_p (const_rtx);
1175 static bool rs6000_mode_dependent_address (const_rtx);
1176 static bool rs6000_debug_mode_dependent_address (const_rtx);
1177 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1178   = rs6000_mode_dependent_address;
1179
1180 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1181                                                      enum machine_mode, rtx);
1182 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1183                                                            enum machine_mode,
1184                                                            rtx);
1185 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1186                                                      enum machine_mode, rtx)
1187   = rs6000_secondary_reload_class;
1188
1189 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1190 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1191                                                            enum reg_class);
1192 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1193   = rs6000_preferred_reload_class;
1194
1195 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1196                                             enum machine_mode);
1197
1198 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1199                                                   enum reg_class,
1200                                                   enum machine_mode);
1201
1202 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1203                                             enum machine_mode)
1204   = rs6000_secondary_memory_needed;
1205
1206 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1207                                              enum machine_mode,
1208                                              enum reg_class);
1209 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1210                                                    enum machine_mode,
1211                                                    enum reg_class);
1212
1213 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1214                                              enum machine_mode,
1215                                              enum reg_class)
1216   = rs6000_cannot_change_mode_class;
1217
1218 static reg_class_t rs6000_secondary_reload (bool, rtx, reg_class_t,
1219                                             enum machine_mode,
1220                                             struct secondary_reload_info *);
1221
1222 const int INSN_NOT_AVAILABLE = -1;
1223 static enum machine_mode rs6000_eh_return_filter_mode (void);
1224 static bool rs6000_can_eliminate (const int, const int);
1225 static void rs6000_conditional_register_usage (void);
1226 static void rs6000_trampoline_init (rtx, tree, rtx);
1227 static bool rs6000_cannot_force_const_mem (enum machine_mode, rtx);
1228 static bool rs6000_legitimate_constant_p (enum machine_mode, rtx);
1229 static bool rs6000_save_toc_in_prologue_p (void);
1230 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
1231 static void rs6000_set_up_by_prologue (struct hard_reg_set_container *);
1232
1233 /* Hash table stuff for keeping track of TOC entries.  */
1234
1235 struct GTY(()) toc_hash_struct
1236 {
1237   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1238      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
1239   rtx key;
1240   enum machine_mode key_mode;
1241   int labelno;
1242 };
1243
1244 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1245
1246 /* Hash table to keep track of the argument types for builtin functions.  */
1247
1248 struct GTY(()) builtin_hash_struct
1249 {
1250   tree type;
1251   enum machine_mode mode[4];    /* return value + 3 arguments.  */
1252   unsigned char uns_p[4];       /* and whether the types are unsigned.  */
1253 };
1254
1255 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1256
1257 static bool rs6000_valid_attribute_p (tree, tree, tree, int);
1258 static void rs6000_function_specific_save (struct cl_target_option *);
1259 static void rs6000_function_specific_restore (struct cl_target_option *);
1260 static void rs6000_function_specific_print (FILE *, int,
1261                                             struct cl_target_option *);
1262 static bool rs6000_can_inline_p (tree, tree);
1263 static void rs6000_set_current_function (tree);
1264
1265 \f
1266 /* Default register names.  */
1267 char rs6000_reg_names[][8] =
1268 {
1269       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1270       "8",  "9", "10", "11", "12", "13", "14", "15",
1271      "16", "17", "18", "19", "20", "21", "22", "23",
1272      "24", "25", "26", "27", "28", "29", "30", "31",
1273       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1274       "8",  "9", "10", "11", "12", "13", "14", "15",
1275      "16", "17", "18", "19", "20", "21", "22", "23",
1276      "24", "25", "26", "27", "28", "29", "30", "31",
1277      "mq", "lr", "ctr","ap",
1278       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1279       "ca",
1280       /* AltiVec registers.  */
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       "vrsave", "vscr",
1286       /* SPE registers.  */
1287       "spe_acc", "spefscr",
1288       /* Soft frame pointer.  */
1289       "sfp"
1290 };
1291
1292 #ifdef TARGET_REGNAMES
1293 static const char alt_reg_names[][8] =
1294 {
1295    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
1296    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1297   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1298   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1299    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
1300    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1301   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1302   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1303     "mq",    "lr",  "ctr",   "ap",
1304   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1305    "ca",
1306   /* AltiVec registers.  */
1307    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
1308    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1309   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1310   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1311   "vrsave", "vscr",
1312   /* SPE registers.  */
1313   "spe_acc", "spefscr",
1314   /* Soft frame pointer.  */
1315   "sfp"
1316 };
1317 #endif
1318
1319 /* Table of valid machine attributes.  */
1320
1321 static const struct attribute_spec rs6000_attribute_table[] =
1322 {
1323   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1324        affects_type_identity } */
1325   { "altivec",   1, 1, false, true,  false, rs6000_handle_altivec_attribute,
1326     false },
1327   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute,
1328     false },
1329   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute,
1330     false },
1331   { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1332     false },
1333   { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1334     false },
1335 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1336   SUBTARGET_ATTRIBUTE_TABLE,
1337 #endif
1338   { NULL,        0, 0, false, false, false, NULL, false }
1339 };
1340 \f
1341 #ifndef MASK_STRICT_ALIGN
1342 #define MASK_STRICT_ALIGN 0
1343 #endif
1344 #ifndef TARGET_PROFILE_KERNEL
1345 #define TARGET_PROFILE_KERNEL 0
1346 #endif
1347
1348 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
1349 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1350 \f
1351 /* Initialize the GCC target structure.  */
1352 #undef TARGET_ATTRIBUTE_TABLE
1353 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1354 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1355 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1356 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1357 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1358
1359 #undef TARGET_ASM_ALIGNED_DI_OP
1360 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1361
1362 /* Default unaligned ops are only provided for ELF.  Find the ops needed
1363    for non-ELF systems.  */
1364 #ifndef OBJECT_FORMAT_ELF
1365 #if TARGET_XCOFF
1366 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
1367    64-bit targets.  */
1368 #undef TARGET_ASM_UNALIGNED_HI_OP
1369 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1370 #undef TARGET_ASM_UNALIGNED_SI_OP
1371 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1372 #undef TARGET_ASM_UNALIGNED_DI_OP
1373 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1374 #else
1375 /* For Darwin.  */
1376 #undef TARGET_ASM_UNALIGNED_HI_OP
1377 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1378 #undef TARGET_ASM_UNALIGNED_SI_OP
1379 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1380 #undef TARGET_ASM_UNALIGNED_DI_OP
1381 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1382 #undef TARGET_ASM_ALIGNED_DI_OP
1383 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1384 #endif
1385 #endif
1386
1387 /* This hook deals with fixups for relocatable code and DI-mode objects
1388    in 64-bit code.  */
1389 #undef TARGET_ASM_INTEGER
1390 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1391
1392 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1393 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1394 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1395 #endif
1396
1397 #undef TARGET_SET_UP_BY_PROLOGUE
1398 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1399
1400 #undef TARGET_HAVE_TLS
1401 #define TARGET_HAVE_TLS HAVE_AS_TLS
1402
1403 #undef TARGET_CANNOT_FORCE_CONST_MEM
1404 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1405
1406 #undef TARGET_DELEGITIMIZE_ADDRESS
1407 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1408
1409 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1410 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
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_ASM_OUTPUT_ADDR_CONST_EXTRA
1418 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1419
1420 #undef TARGET_LEGITIMIZE_ADDRESS
1421 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1422
1423 #undef  TARGET_SCHED_VARIABLE_ISSUE
1424 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1425
1426 #undef TARGET_SCHED_ISSUE_RATE
1427 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1428 #undef TARGET_SCHED_ADJUST_COST
1429 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1430 #undef TARGET_SCHED_ADJUST_PRIORITY
1431 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1432 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1433 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1434 #undef TARGET_SCHED_INIT
1435 #define TARGET_SCHED_INIT rs6000_sched_init
1436 #undef TARGET_SCHED_FINISH
1437 #define TARGET_SCHED_FINISH rs6000_sched_finish
1438 #undef TARGET_SCHED_REORDER
1439 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1440 #undef TARGET_SCHED_REORDER2
1441 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1442
1443 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1444 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1445
1446 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1447 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1448
1449 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1450 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1451 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1452 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1453 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1454 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1455 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1456 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1457
1458 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1459 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1460 #undef TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_EVEN
1461 #define TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_EVEN rs6000_builtin_mul_widen_even
1462 #undef TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_ODD
1463 #define TARGET_VECTORIZE_BUILTIN_MUL_WIDEN_ODD rs6000_builtin_mul_widen_odd
1464 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1465 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT            \
1466   rs6000_builtin_support_vector_misalignment
1467 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1468 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1469 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1470 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1471   rs6000_builtin_vectorization_cost
1472 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1473 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1474   rs6000_preferred_simd_mode
1475
1476 #undef TARGET_INIT_BUILTINS
1477 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1478 #undef TARGET_BUILTIN_DECL
1479 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1480
1481 #undef TARGET_EXPAND_BUILTIN
1482 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1483
1484 #undef TARGET_MANGLE_TYPE
1485 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1486
1487 #undef TARGET_INIT_LIBFUNCS
1488 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1489
1490 #if TARGET_MACHO
1491 #undef TARGET_BINDS_LOCAL_P
1492 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1493 #endif
1494
1495 #undef TARGET_MS_BITFIELD_LAYOUT_P
1496 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1497
1498 #undef TARGET_ASM_OUTPUT_MI_THUNK
1499 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1500
1501 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1502 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1503
1504 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1505 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1506
1507 #undef TARGET_INVALID_WITHIN_DOLOOP
1508 #define TARGET_INVALID_WITHIN_DOLOOP rs6000_invalid_within_doloop
1509
1510 #undef TARGET_REGISTER_MOVE_COST
1511 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1512 #undef TARGET_MEMORY_MOVE_COST
1513 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1514 #undef TARGET_RTX_COSTS
1515 #define TARGET_RTX_COSTS rs6000_rtx_costs
1516 #undef TARGET_ADDRESS_COST
1517 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
1518
1519 #undef TARGET_DWARF_REGISTER_SPAN
1520 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1521
1522 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1523 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1524
1525 /* On rs6000, function arguments are promoted, as are function return
1526    values.  */
1527 #undef TARGET_PROMOTE_FUNCTION_MODE
1528 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1529
1530 #undef TARGET_RETURN_IN_MEMORY
1531 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1532
1533 #undef TARGET_SETUP_INCOMING_VARARGS
1534 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1535
1536 /* Always strict argument naming on rs6000.  */
1537 #undef TARGET_STRICT_ARGUMENT_NAMING
1538 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1539 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1540 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1541 #undef TARGET_SPLIT_COMPLEX_ARG
1542 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1543 #undef TARGET_MUST_PASS_IN_STACK
1544 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1545 #undef TARGET_PASS_BY_REFERENCE
1546 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1547 #undef TARGET_ARG_PARTIAL_BYTES
1548 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1549 #undef TARGET_FUNCTION_ARG_ADVANCE
1550 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1551 #undef TARGET_FUNCTION_ARG
1552 #define TARGET_FUNCTION_ARG rs6000_function_arg
1553 #undef TARGET_FUNCTION_ARG_BOUNDARY
1554 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1555
1556 #undef TARGET_BUILD_BUILTIN_VA_LIST
1557 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1558
1559 #undef TARGET_EXPAND_BUILTIN_VA_START
1560 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1561
1562 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1563 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1564
1565 #undef TARGET_EH_RETURN_FILTER_MODE
1566 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1567
1568 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1569 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1570
1571 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1572 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1573
1574 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1575 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1576
1577 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1578 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1579
1580 #undef TARGET_OPTION_OVERRIDE
1581 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1582
1583 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1584 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1585   rs6000_builtin_vectorized_function
1586
1587 #if !TARGET_MACHO
1588 #undef TARGET_STACK_PROTECT_FAIL
1589 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1590 #endif
1591
1592 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1593    The PowerPC architecture requires only weak consistency among
1594    processors--that is, memory accesses between processors need not be
1595    sequentially consistent and memory accesses among processors can occur
1596    in any order. The ability to order memory accesses weakly provides
1597    opportunities for more efficient use of the system bus. Unless a
1598    dependency exists, the 604e allows read operations to precede store
1599    operations.  */
1600 #undef TARGET_RELAXED_ORDERING
1601 #define TARGET_RELAXED_ORDERING true
1602
1603 #ifdef HAVE_AS_TLS
1604 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1605 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1606 #endif
1607
1608 /* Use a 32-bit anchor range.  This leads to sequences like:
1609
1610         addis   tmp,anchor,high
1611         add     dest,tmp,low
1612
1613    where tmp itself acts as an anchor, and can be shared between
1614    accesses to the same 64k page.  */
1615 #undef TARGET_MIN_ANCHOR_OFFSET
1616 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1617 #undef TARGET_MAX_ANCHOR_OFFSET
1618 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1619 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1620 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1621
1622 #undef TARGET_BUILTIN_RECIPROCAL
1623 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1624
1625 #undef TARGET_EXPAND_TO_RTL_HOOK
1626 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1627
1628 #undef TARGET_INSTANTIATE_DECLS
1629 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1630
1631 #undef TARGET_SECONDARY_RELOAD
1632 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1633
1634 #undef TARGET_LEGITIMATE_ADDRESS_P
1635 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1636
1637 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1638 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1639
1640 #undef TARGET_CAN_ELIMINATE
1641 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1642
1643 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1644 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1645
1646 #undef TARGET_TRAMPOLINE_INIT
1647 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1648
1649 #undef TARGET_FUNCTION_VALUE
1650 #define TARGET_FUNCTION_VALUE rs6000_function_value
1651
1652 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1653 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1654
1655 #undef TARGET_OPTION_SAVE
1656 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1657
1658 #undef TARGET_OPTION_RESTORE
1659 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1660
1661 #undef TARGET_OPTION_PRINT
1662 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1663
1664 #undef TARGET_CAN_INLINE_P
1665 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1666
1667 #undef TARGET_SET_CURRENT_FUNCTION
1668 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1669
1670 #undef TARGET_LEGITIMATE_CONSTANT_P
1671 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1672
1673 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1674 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1675 \f
1676
1677 /* Simplifications for entries below.  */
1678
1679 enum {
1680   POWERPC_BASE_MASK = MASK_POWERPC | MASK_NEW_MNEMONICS,
1681   POWERPC_7400_MASK = POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_ALTIVEC
1682 };
1683
1684 /* Some OSs don't support saving the high part of 64-bit registers on context
1685    switch.  Other OSs don't support saving Altivec registers.  On those OSs, we
1686    don't touch the MASK_POWERPC64 or MASK_ALTIVEC settings; if the user wants
1687    either, the user must explicitly specify them and we won't interfere with
1688    the user's specification.  */
1689
1690 enum {
1691   POWER_MASKS = MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
1692   POWERPC_MASKS = (POWERPC_BASE_MASK | MASK_PPC_GPOPT | MASK_STRICT_ALIGN
1693                    | MASK_PPC_GFXOPT | MASK_POWERPC64 | MASK_ALTIVEC
1694                    | MASK_MFCRF | MASK_POPCNTB | MASK_FPRND | MASK_MULHW
1695                    | MASK_DLMZB | MASK_CMPB | MASK_MFPGPR | MASK_DFP
1696                    | MASK_POPCNTD | MASK_VSX | MASK_ISEL | MASK_NO_UPDATE
1697                    | MASK_RECIP_PRECISION)
1698 };
1699
1700 /* Masks for instructions set at various powerpc ISAs.  */
1701 enum {
1702   ISA_2_1_MASKS = MASK_MFCRF,
1703   ISA_2_2_MASKS = (ISA_2_1_MASKS | MASK_POPCNTB),
1704   ISA_2_4_MASKS = (ISA_2_2_MASKS | MASK_FPRND),
1705
1706   /* For ISA 2.05, do not add MFPGPR, since it isn't in ISA 2.06, and don't add
1707      ALTIVEC, since in general it isn't a win on power6.  In ISA 2.04, fsel,
1708      fre, fsqrt, etc. were no longer documented as optional.  Group masks by
1709      server and embedded. */
1710   ISA_2_5_MASKS_EMBEDDED = (ISA_2_2_MASKS | MASK_CMPB | MASK_RECIP_PRECISION
1711                             | MASK_PPC_GFXOPT | MASK_PPC_GPOPT),
1712   ISA_2_5_MASKS_SERVER = (ISA_2_5_MASKS_EMBEDDED | MASK_DFP),
1713
1714   /* For ISA 2.06, don't add ISEL, since in general it isn't a win, but
1715      altivec is a win so enable it.  */
1716   ISA_2_6_MASKS_EMBEDDED = (ISA_2_5_MASKS_EMBEDDED | MASK_POPCNTD),
1717   ISA_2_6_MASKS_SERVER = (ISA_2_5_MASKS_SERVER | MASK_POPCNTD | MASK_ALTIVEC
1718                           | MASK_VSX)
1719 };
1720
1721 struct rs6000_ptt
1722 {
1723   const char *const name;               /* Canonical processor name.  */
1724   const enum processor_type processor;  /* Processor type enum value.  */
1725   const int target_enable;              /* Target flags to enable.  */
1726 };
1727
1728 static struct rs6000_ptt const processor_target_table[] =
1729 {
1730 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1731 #include "rs6000-cpus.def"
1732 #undef RS6000_CPU
1733 };
1734
1735 /* Look up a processor name for -mcpu=xxx and -mtune=xxx.  Return -1 if the
1736    name is invalid.  */
1737
1738 static int
1739 rs6000_cpu_name_lookup (const char *name)
1740 {
1741   size_t i;
1742
1743   if (name != NULL)
1744     {
1745       for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1746         if (! strcmp (name, processor_target_table[i].name))
1747           return (int)i;
1748     }
1749
1750   return -1;
1751 }
1752
1753 \f
1754 /* Return number of consecutive hard regs needed starting at reg REGNO
1755    to hold something of mode MODE.
1756    This is ordinarily the length in words of a value of mode MODE
1757    but can be less for certain modes in special long registers.
1758
1759    For the SPE, GPRs are 64 bits but only 32 bits are visible in
1760    scalar instructions.  The upper 32 bits are only available to the
1761    SIMD instructions.
1762
1763    POWER and PowerPC GPRs hold 32 bits worth;
1764    PowerPC64 GPRs and FPRs point register holds 64 bits worth.  */
1765
1766 static int
1767 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1768 {
1769   unsigned HOST_WIDE_INT reg_size;
1770
1771   if (FP_REGNO_P (regno))
1772     reg_size = (VECTOR_MEM_VSX_P (mode)
1773                 ? UNITS_PER_VSX_WORD
1774                 : UNITS_PER_FP_WORD);
1775
1776   else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1777     reg_size = UNITS_PER_SPE_WORD;
1778
1779   else if (ALTIVEC_REGNO_P (regno))
1780     reg_size = UNITS_PER_ALTIVEC_WORD;
1781
1782   /* The value returned for SCmode in the E500 double case is 2 for
1783      ABI compatibility; storing an SCmode value in a single register
1784      would require function_arg and rs6000_spe_function_arg to handle
1785      SCmode so as to pass the value correctly in a pair of
1786      registers.  */
1787   else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1788            && !DECIMAL_FLOAT_MODE_P (mode))
1789     reg_size = UNITS_PER_FP_WORD;
1790
1791   else
1792     reg_size = UNITS_PER_WORD;
1793
1794   return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1795 }
1796
1797 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1798    MODE.  */
1799 static int
1800 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1801 {
1802   int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1803
1804   /* VSX registers that overlap the FPR registers are larger than for non-VSX
1805      implementations.  Don't allow an item to be split between a FP register
1806      and an Altivec register.  */
1807   if (VECTOR_MEM_VSX_P (mode))
1808     {
1809       if (FP_REGNO_P (regno))
1810         return FP_REGNO_P (last_regno);
1811
1812       if (ALTIVEC_REGNO_P (regno))
1813         return ALTIVEC_REGNO_P (last_regno);
1814     }
1815
1816   /* The GPRs can hold any mode, but values bigger than one register
1817      cannot go past R31.  */
1818   if (INT_REGNO_P (regno))
1819     return INT_REGNO_P (last_regno);
1820
1821   /* The float registers (except for VSX vector modes) can only hold floating
1822      modes and DImode.  This excludes the 32-bit decimal float mode for
1823      now.  */
1824   if (FP_REGNO_P (regno))
1825     {
1826       if (SCALAR_FLOAT_MODE_P (mode)
1827           && (mode != TDmode || (regno % 2) == 0)
1828           && FP_REGNO_P (last_regno))
1829         return 1;
1830
1831       if (GET_MODE_CLASS (mode) == MODE_INT
1832           && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1833         return 1;
1834
1835       if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1836           && PAIRED_VECTOR_MODE (mode))
1837         return 1;
1838
1839       return 0;
1840     }
1841
1842   /* The CR register can only hold CC modes.  */
1843   if (CR_REGNO_P (regno))
1844     return GET_MODE_CLASS (mode) == MODE_CC;
1845
1846   if (CA_REGNO_P (regno))
1847     return mode == BImode;
1848
1849   /* AltiVec only in AldyVec registers.  */
1850   if (ALTIVEC_REGNO_P (regno))
1851     return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1852
1853   /* ...but GPRs can hold SIMD data on the SPE in one register.  */
1854   if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1855     return 1;
1856
1857   /* We cannot put TImode anywhere except general register and it must be able
1858      to fit within the register set.  In the future, allow TImode in the
1859      Altivec or VSX registers.  */
1860
1861   return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1862 }
1863
1864 /* Print interesting facts about registers.  */
1865 static void
1866 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1867 {
1868   int r, m;
1869
1870   for (r = first_regno; r <= last_regno; ++r)
1871     {
1872       const char *comma = "";
1873       int len;
1874
1875       if (first_regno == last_regno)
1876         fprintf (stderr, "%s:\t", reg_name);
1877       else
1878         fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1879
1880       len = 8;
1881       for (m = 0; m < NUM_MACHINE_MODES; ++m)
1882         if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1883           {
1884             if (len > 70)
1885               {
1886                 fprintf (stderr, ",\n\t");
1887                 len = 8;
1888                 comma = "";
1889               }
1890
1891             if (rs6000_hard_regno_nregs[m][r] > 1)
1892               len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1893                              rs6000_hard_regno_nregs[m][r]);
1894             else
1895               len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1896
1897             comma = ", ";
1898           }
1899
1900       if (call_used_regs[r])
1901         {
1902           if (len > 70)
1903             {
1904               fprintf (stderr, ",\n\t");
1905               len = 8;
1906               comma = "";
1907             }
1908
1909           len += fprintf (stderr, "%s%s", comma, "call-used");
1910           comma = ", ";
1911         }
1912
1913       if (fixed_regs[r])
1914         {
1915           if (len > 70)
1916             {
1917               fprintf (stderr, ",\n\t");
1918               len = 8;
1919               comma = "";
1920             }
1921
1922           len += fprintf (stderr, "%s%s", comma, "fixed");
1923           comma = ", ";
1924         }
1925
1926       if (len > 70)
1927         {
1928           fprintf (stderr, ",\n\t");
1929           comma = "";
1930         }
1931
1932       fprintf (stderr, "%sregno = %d\n", comma, r);
1933     }
1934 }
1935
1936 #define DEBUG_FMT_D "%-32s= %d\n"
1937 #define DEBUG_FMT_X "%-32s= 0x%x\n"
1938 #define DEBUG_FMT_S "%-32s= %s\n"
1939
1940 /* Print various interesting information with -mdebug=reg.  */
1941 static void
1942 rs6000_debug_reg_global (void)
1943 {
1944   static const char *const tf[2] = { "false", "true" };
1945   const char *nl = (const char *)0;
1946   int m;
1947   char costly_num[20];
1948   char nop_num[20];
1949   const char *costly_str;
1950   const char *nop_str;
1951   const char *trace_str;
1952   const char *abi_str;
1953   const char *cmodel_str;
1954
1955   /* Map enum rs6000_vector to string.  */
1956   static const char *rs6000_debug_vector_unit[] = {
1957     "none",
1958     "altivec",
1959     "vsx",
1960     "paired",
1961     "spe",
1962     "other"
1963   };
1964
1965   fprintf (stderr, "Register information: (last virtual reg = %d)\n",
1966            LAST_VIRTUAL_REGISTER);
1967   rs6000_debug_reg_print (0, 31, "gr");
1968   rs6000_debug_reg_print (32, 63, "fp");
1969   rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1970                           LAST_ALTIVEC_REGNO,
1971                           "vs");
1972   rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1973   rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1974   rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1975   rs6000_debug_reg_print (MQ_REGNO, MQ_REGNO, "mq");
1976   rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1977   rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1978   rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1979   rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1980   rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1981
1982   fprintf (stderr,
1983            "\n"
1984            "d  reg_class = %s\n"
1985            "f  reg_class = %s\n"
1986            "v  reg_class = %s\n"
1987            "wa reg_class = %s\n"
1988            "wd reg_class = %s\n"
1989            "wf reg_class = %s\n"
1990            "ws reg_class = %s\n\n",
1991            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1992            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1993            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1994            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1995            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1996            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1997            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]]);
1998
1999   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2000     if (rs6000_vector_unit[m] || rs6000_vector_mem[m])
2001       {
2002         nl = "\n";
2003         fprintf (stderr, "Vector mode: %-5s arithmetic: %-8s move: %-8s\n",
2004                  GET_MODE_NAME (m),
2005                  rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
2006                  rs6000_debug_vector_unit[ rs6000_vector_mem[m] ]);
2007       }
2008
2009   if (nl)
2010     fputs (nl, stderr);
2011
2012   if (rs6000_recip_control)
2013     {
2014       fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2015
2016       for (m = 0; m < NUM_MACHINE_MODES; ++m)
2017         if (rs6000_recip_bits[m])
2018           {
2019             fprintf (stderr,
2020                      "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2021                      GET_MODE_NAME (m),
2022                      (RS6000_RECIP_AUTO_RE_P (m)
2023                       ? "auto"
2024                       : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2025                      (RS6000_RECIP_AUTO_RSQRTE_P (m)
2026                       ? "auto"
2027                       : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2028           }
2029
2030       fputs ("\n", stderr);
2031     }
2032
2033   if (rs6000_cpu_index >= 0)
2034     fprintf (stderr, DEBUG_FMT_S, "cpu",
2035              processor_target_table[rs6000_cpu_index].name);
2036
2037   if (rs6000_tune_index >= 0)
2038     fprintf (stderr, DEBUG_FMT_S, "tune",
2039              processor_target_table[rs6000_tune_index].name);
2040
2041   switch (rs6000_sched_costly_dep)
2042     {
2043     case max_dep_latency:
2044       costly_str = "max_dep_latency";
2045       break;
2046
2047     case no_dep_costly:
2048       costly_str = "no_dep_costly";
2049       break;
2050
2051     case all_deps_costly:
2052       costly_str = "all_deps_costly";
2053       break;
2054
2055     case true_store_to_load_dep_costly:
2056       costly_str = "true_store_to_load_dep_costly";
2057       break;
2058
2059     case store_to_load_dep_costly:
2060       costly_str = "store_to_load_dep_costly";
2061       break;
2062
2063     default:
2064       costly_str = costly_num;
2065       sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2066       break;
2067     }
2068
2069   fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2070
2071   switch (rs6000_sched_insert_nops)
2072     {
2073     case sched_finish_regroup_exact:
2074       nop_str = "sched_finish_regroup_exact";
2075       break;
2076
2077     case sched_finish_pad_groups:
2078       nop_str = "sched_finish_pad_groups";
2079       break;
2080
2081     case sched_finish_none:
2082       nop_str = "sched_finish_none";
2083       break;
2084
2085     default:
2086       nop_str = nop_num;
2087       sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2088       break;
2089     }
2090
2091   fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2092
2093   switch (rs6000_sdata)
2094     {
2095     default:
2096     case SDATA_NONE:
2097       break;
2098
2099     case SDATA_DATA:
2100       fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2101       break;
2102
2103     case SDATA_SYSV:
2104       fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2105       break;
2106
2107     case SDATA_EABI:
2108       fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2109       break;
2110
2111     }
2112
2113   switch (rs6000_traceback)
2114     {
2115     case traceback_default:     trace_str = "default";  break;
2116     case traceback_none:        trace_str = "none";     break;
2117     case traceback_part:        trace_str = "part";     break;
2118     case traceback_full:        trace_str = "full";     break;
2119     default:                    trace_str = "unknown";  break;
2120     }
2121
2122   fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2123
2124   switch (rs6000_current_cmodel)
2125     {
2126     case CMODEL_SMALL:  cmodel_str = "small";   break;
2127     case CMODEL_MEDIUM: cmodel_str = "medium";  break;
2128     case CMODEL_LARGE:  cmodel_str = "large";   break;
2129     default:            cmodel_str = "unknown"; break;
2130     }
2131
2132   fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2133
2134   switch (rs6000_current_abi)
2135     {
2136     case ABI_NONE:      abi_str = "none";       break;
2137     case ABI_AIX:       abi_str = "aix";        break;
2138     case ABI_V4:        abi_str = "V4";         break;
2139     case ABI_DARWIN:    abi_str = "darwin";     break;
2140     default:            abi_str = "unknown";    break;
2141     }
2142
2143   fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2144
2145   if (rs6000_altivec_abi)
2146     fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2147
2148   if (rs6000_spe_abi)
2149     fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2150
2151   if (rs6000_darwin64_abi)
2152     fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2153
2154   if (rs6000_float_gprs)
2155     fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2156
2157   fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2158   fprintf (stderr, DEBUG_FMT_S, "align_branch",
2159            tf[!!rs6000_align_branch_targets]);
2160   fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2161   fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2162            rs6000_long_double_type_size);
2163   fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2164            (int)rs6000_sched_restricted_insns_priority);
2165   fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2166            (int)END_BUILTINS);
2167   fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2168            (int)RS6000_BUILTIN_COUNT);
2169   fprintf (stderr, DEBUG_FMT_X, "Builtin mask", rs6000_builtin_mask);
2170 }
2171
2172 /* Initialize the various global tables that are based on register size.  */
2173 static void
2174 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2175 {
2176   int r, m, c;
2177   int align64;
2178   int align32;
2179
2180   /* Precalculate REGNO_REG_CLASS.  */
2181   rs6000_regno_regclass[0] = GENERAL_REGS;
2182   for (r = 1; r < 32; ++r)
2183     rs6000_regno_regclass[r] = BASE_REGS;
2184
2185   for (r = 32; r < 64; ++r)
2186     rs6000_regno_regclass[r] = FLOAT_REGS;
2187
2188   for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2189     rs6000_regno_regclass[r] = NO_REGS;
2190
2191   for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2192     rs6000_regno_regclass[r] = ALTIVEC_REGS;
2193
2194   rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2195   for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2196     rs6000_regno_regclass[r] = CR_REGS;
2197
2198   rs6000_regno_regclass[MQ_REGNO] = MQ_REGS;
2199   rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2200   rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2201   rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2202   rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2203   rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2204   rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2205   rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2206   rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2207   rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2208
2209   /* Precalculate vector information, this must be set up before the
2210      rs6000_hard_regno_nregs_internal below.  */
2211   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2212     {
2213       rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
2214       rs6000_vector_reload[m][0] = CODE_FOR_nothing;
2215       rs6000_vector_reload[m][1] = CODE_FOR_nothing;
2216     }
2217
2218   for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
2219     rs6000_constraints[c] = NO_REGS;
2220
2221   /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2222      believes it can use native alignment or still uses 128-bit alignment.  */
2223   if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2224     {
2225       align64 = 64;
2226       align32 = 32;
2227     }
2228   else
2229     {
2230       align64 = 128;
2231       align32 = 128;
2232     }
2233
2234   /* V2DF mode, VSX only.  */
2235   if (TARGET_VSX)
2236     {
2237       rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2238       rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2239       rs6000_vector_align[V2DFmode] = align64;
2240     }
2241
2242   /* V4SF mode, either VSX or Altivec.  */
2243   if (TARGET_VSX)
2244     {
2245       rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2246       rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2247       rs6000_vector_align[V4SFmode] = align32;
2248     }
2249   else if (TARGET_ALTIVEC)
2250     {
2251       rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2252       rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2253       rs6000_vector_align[V4SFmode] = align32;
2254     }
2255
2256   /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2257      and stores. */
2258   if (TARGET_ALTIVEC)
2259     {
2260       rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2261       rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2262       rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2263       rs6000_vector_align[V4SImode] = align32;
2264       rs6000_vector_align[V8HImode] = align32;
2265       rs6000_vector_align[V16QImode] = align32;
2266
2267       if (TARGET_VSX)
2268         {
2269           rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2270           rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2271           rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2272         }
2273       else
2274         {
2275           rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2276           rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2277           rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2278         }
2279     }
2280
2281   /* V2DImode, only allow under VSX, which can do V2DI insert/splat/extract.
2282      Altivec doesn't have 64-bit support.  */
2283   if (TARGET_VSX)
2284     {
2285       rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2286       rs6000_vector_unit[V2DImode] = VECTOR_NONE;
2287       rs6000_vector_align[V2DImode] = align64;
2288     }
2289
2290   /* DFmode, see if we want to use the VSX unit.  */
2291   if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2292     {
2293       rs6000_vector_unit[DFmode] = VECTOR_VSX;
2294       rs6000_vector_mem[DFmode]
2295         = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2296       rs6000_vector_align[DFmode] = align64;
2297     }
2298
2299   /* TODO add SPE and paired floating point vector support.  */
2300
2301   /* Register class constaints for the constraints that depend on compile
2302      switches.  */
2303   if (TARGET_HARD_FLOAT && TARGET_FPRS)
2304     rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2305
2306   if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2307     rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2308
2309   if (TARGET_VSX)
2310     {
2311       /* At present, we just use VSX_REGS, but we have different constraints
2312          based on the use, in case we want to fine tune the default register
2313          class used.  wa = any VSX register, wf = register class to use for
2314          V4SF, wd = register class to use for V2DF, and ws = register classs to
2315          use for DF scalars.  */
2316       rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2317       rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2318       rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2319       rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2320                                                   ? VSX_REGS
2321                                                   : FLOAT_REGS);
2322     }
2323
2324   if (TARGET_ALTIVEC)
2325     rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2326
2327   /* Set up the reload helper functions.  */
2328   if (TARGET_VSX || TARGET_ALTIVEC)
2329     {
2330       if (TARGET_64BIT)
2331         {
2332           rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2333           rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2334           rs6000_vector_reload[V8HImode][0]  = CODE_FOR_reload_v8hi_di_store;
2335           rs6000_vector_reload[V8HImode][1]  = CODE_FOR_reload_v8hi_di_load;
2336           rs6000_vector_reload[V4SImode][0]  = CODE_FOR_reload_v4si_di_store;
2337           rs6000_vector_reload[V4SImode][1]  = CODE_FOR_reload_v4si_di_load;
2338           rs6000_vector_reload[V2DImode][0]  = CODE_FOR_reload_v2di_di_store;
2339           rs6000_vector_reload[V2DImode][1]  = CODE_FOR_reload_v2di_di_load;
2340           rs6000_vector_reload[V4SFmode][0]  = CODE_FOR_reload_v4sf_di_store;
2341           rs6000_vector_reload[V4SFmode][1]  = CODE_FOR_reload_v4sf_di_load;
2342           rs6000_vector_reload[V2DFmode][0]  = CODE_FOR_reload_v2df_di_store;
2343           rs6000_vector_reload[V2DFmode][1]  = CODE_FOR_reload_v2df_di_load;
2344           if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2345             {
2346               rs6000_vector_reload[DFmode][0]  = CODE_FOR_reload_df_di_store;
2347               rs6000_vector_reload[DFmode][1]  = CODE_FOR_reload_df_di_load;
2348             }
2349         }
2350       else
2351         {
2352           rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2353           rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2354           rs6000_vector_reload[V8HImode][0]  = CODE_FOR_reload_v8hi_si_store;
2355           rs6000_vector_reload[V8HImode][1]  = CODE_FOR_reload_v8hi_si_load;
2356           rs6000_vector_reload[V4SImode][0]  = CODE_FOR_reload_v4si_si_store;
2357           rs6000_vector_reload[V4SImode][1]  = CODE_FOR_reload_v4si_si_load;
2358           rs6000_vector_reload[V2DImode][0]  = CODE_FOR_reload_v2di_si_store;
2359           rs6000_vector_reload[V2DImode][1]  = CODE_FOR_reload_v2di_si_load;
2360           rs6000_vector_reload[V4SFmode][0]  = CODE_FOR_reload_v4sf_si_store;
2361           rs6000_vector_reload[V4SFmode][1]  = CODE_FOR_reload_v4sf_si_load;
2362           rs6000_vector_reload[V2DFmode][0]  = CODE_FOR_reload_v2df_si_store;
2363           rs6000_vector_reload[V2DFmode][1]  = CODE_FOR_reload_v2df_si_load;
2364           if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2365             {
2366               rs6000_vector_reload[DFmode][0]  = CODE_FOR_reload_df_si_store;
2367               rs6000_vector_reload[DFmode][1]  = CODE_FOR_reload_df_si_load;
2368             }
2369         }
2370     }
2371
2372   /* Precalculate HARD_REGNO_NREGS.  */
2373   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2374     for (m = 0; m < NUM_MACHINE_MODES; ++m)
2375       rs6000_hard_regno_nregs[m][r]
2376         = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2377
2378   /* Precalculate HARD_REGNO_MODE_OK.  */
2379   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2380     for (m = 0; m < NUM_MACHINE_MODES; ++m)
2381       if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2382         rs6000_hard_regno_mode_ok_p[m][r] = true;
2383
2384   /* Precalculate CLASS_MAX_NREGS sizes.  */
2385   for (c = 0; c < LIM_REG_CLASSES; ++c)
2386     {
2387       int reg_size;
2388
2389       if (TARGET_VSX && VSX_REG_CLASS_P (c))
2390         reg_size = UNITS_PER_VSX_WORD;
2391
2392       else if (c == ALTIVEC_REGS)
2393         reg_size = UNITS_PER_ALTIVEC_WORD;
2394
2395       else if (c == FLOAT_REGS)
2396         reg_size = UNITS_PER_FP_WORD;
2397
2398       else
2399         reg_size = UNITS_PER_WORD;
2400
2401       for (m = 0; m < NUM_MACHINE_MODES; ++m)
2402         rs6000_class_max_nregs[m][c]
2403           = (GET_MODE_SIZE (m) + reg_size - 1) / reg_size;
2404     }
2405
2406   if (TARGET_E500_DOUBLE)
2407     rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2408
2409   /* Calculate which modes to automatically generate code to use a the
2410      reciprocal divide and square root instructions.  In the future, possibly
2411      automatically generate the instructions even if the user did not specify
2412      -mrecip.  The older machines double precision reciprocal sqrt estimate is
2413      not accurate enough.  */
2414   memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2415   if (TARGET_FRES)
2416     rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2417   if (TARGET_FRE)
2418     rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2419   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2420     rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2421   if (VECTOR_UNIT_VSX_P (V2DFmode))
2422     rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2423
2424   if (TARGET_FRSQRTES)
2425     rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2426   if (TARGET_FRSQRTE)
2427     rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2428   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2429     rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2430   if (VECTOR_UNIT_VSX_P (V2DFmode))
2431     rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2432
2433   if (rs6000_recip_control)
2434     {
2435       if (!flag_finite_math_only)
2436         warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2437       if (flag_trapping_math)
2438         warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2439       if (!flag_reciprocal_math)
2440         warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2441       if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2442         {
2443           if (RS6000_RECIP_HAVE_RE_P (SFmode)
2444               && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2445             rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2446
2447           if (RS6000_RECIP_HAVE_RE_P (DFmode)
2448               && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2449             rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2450
2451           if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2452               && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2453             rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2454
2455           if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2456               && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2457             rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2458
2459           if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2460               && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2461             rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2462
2463           if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2464               && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2465             rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2466
2467           if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2468               && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2469             rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2470
2471           if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2472               && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2473             rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2474         }
2475     }
2476
2477   if (global_init_p || TARGET_DEBUG_TARGET)
2478     {
2479       if (TARGET_DEBUG_REG)
2480         rs6000_debug_reg_global ();
2481
2482       if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2483         fprintf (stderr,
2484                  "SImode variable mult cost       = %d\n"
2485                  "SImode constant mult cost       = %d\n"
2486                  "SImode short constant mult cost = %d\n"
2487                  "DImode multipliciation cost     = %d\n"
2488                  "SImode division cost            = %d\n"
2489                  "DImode division cost            = %d\n"
2490                  "Simple fp operation cost        = %d\n"
2491                  "DFmode multiplication cost      = %d\n"
2492                  "SFmode division cost            = %d\n"
2493                  "DFmode division cost            = %d\n"
2494                  "cache line size                 = %d\n"
2495                  "l1 cache size                   = %d\n"
2496                  "l2 cache size                   = %d\n"
2497                  "simultaneous prefetches         = %d\n"
2498                  "\n",
2499                  rs6000_cost->mulsi,
2500                  rs6000_cost->mulsi_const,
2501                  rs6000_cost->mulsi_const9,
2502                  rs6000_cost->muldi,
2503                  rs6000_cost->divsi,
2504                  rs6000_cost->divdi,
2505                  rs6000_cost->fp,
2506                  rs6000_cost->dmul,
2507                  rs6000_cost->sdiv,
2508                  rs6000_cost->ddiv,
2509                  rs6000_cost->cache_line_size,
2510                  rs6000_cost->l1_cache_size,
2511                  rs6000_cost->l2_cache_size,
2512                  rs6000_cost->simultaneous_prefetches);
2513     }
2514 }
2515
2516 #if TARGET_MACHO
2517 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS.  */
2518
2519 static void
2520 darwin_rs6000_override_options (void)
2521 {
2522   /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2523      off.  */
2524   rs6000_altivec_abi = 1;
2525   TARGET_ALTIVEC_VRSAVE = 1;
2526   rs6000_current_abi = ABI_DARWIN;
2527
2528   if (DEFAULT_ABI == ABI_DARWIN
2529       && TARGET_64BIT)
2530       darwin_one_byte_bool = 1;
2531
2532   if (TARGET_64BIT && ! TARGET_POWERPC64)
2533     {
2534       target_flags |= MASK_POWERPC64;
2535       warning (0, "-m64 requires PowerPC64 architecture, enabling");
2536     }
2537   if (flag_mkernel)
2538     {
2539       rs6000_default_long_calls = 1;
2540       target_flags |= MASK_SOFT_FLOAT;
2541     }
2542
2543   /* Make -m64 imply -maltivec.  Darwin's 64-bit ABI includes
2544      Altivec.  */
2545   if (!flag_mkernel && !flag_apple_kext
2546       && TARGET_64BIT
2547       && ! (target_flags_explicit & MASK_ALTIVEC))
2548     target_flags |= MASK_ALTIVEC;
2549
2550   /* Unless the user (not the configurer) has explicitly overridden
2551      it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2552      G4 unless targetting the kernel.  */
2553   if (!flag_mkernel
2554       && !flag_apple_kext
2555       && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2556       && ! (target_flags_explicit & MASK_ALTIVEC)
2557       && ! global_options_set.x_rs6000_cpu_index)
2558     {
2559       target_flags |= MASK_ALTIVEC;
2560     }
2561 }
2562 #endif
2563
2564 /* If not otherwise specified by a target, make 'long double' equivalent to
2565    'double'.  */
2566
2567 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2568 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2569 #endif
2570
2571 /* Return the builtin mask of the various options used that could affect which
2572    builtins were used.  In the past we used target_flags, but we've run out of
2573    bits, and some options like SPE and PAIRED are no longer in
2574    target_flags.  */
2575
2576 unsigned
2577 rs6000_builtin_mask_calculate (void)
2578 {
2579   return (((TARGET_ALTIVEC)                 ? RS6000_BTM_ALTIVEC  : 0)
2580           | ((TARGET_VSX)                   ? RS6000_BTM_VSX      : 0)
2581           | ((TARGET_SPE)                   ? RS6000_BTM_SPE      : 0)
2582           | ((TARGET_PAIRED_FLOAT)          ? RS6000_BTM_PAIRED   : 0)
2583           | ((TARGET_FRE)                   ? RS6000_BTM_FRE      : 0)
2584           | ((TARGET_FRES)                  ? RS6000_BTM_FRES     : 0)
2585           | ((TARGET_FRSQRTE)               ? RS6000_BTM_FRSQRTE  : 0)
2586           | ((TARGET_FRSQRTES)              ? RS6000_BTM_FRSQRTES : 0)
2587           | ((TARGET_POPCNTD)               ? RS6000_BTM_POPCNTD  : 0)
2588           | ((TARGET_POWERPC)               ? RS6000_BTM_POWERPC  : 0)
2589           | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL     : 0));
2590 }
2591
2592 /* Override command line options.  Mostly we process the processor type and
2593    sometimes adjust other TARGET_ options.  */
2594
2595 static bool
2596 rs6000_option_override_internal (bool global_init_p)
2597 {
2598   bool ret = true;
2599   bool have_cpu = false;
2600   const char *default_cpu = OPTION_TARGET_CPU_DEFAULT;
2601   int set_masks;
2602   int cpu_index;
2603   int tune_index;
2604   struct cl_target_option *main_target_opt
2605     = ((global_init_p || target_option_default_node == NULL)
2606        ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2607
2608   /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
2609      library functions, so warn about it. The flag may be useful for
2610      performance studies from time to time though, so don't disable it
2611      entirely.  */
2612   if (global_options_set.x_rs6000_alignment_flags
2613       && rs6000_alignment_flags == MASK_ALIGN_POWER
2614       && DEFAULT_ABI == ABI_DARWIN
2615       && TARGET_64BIT)
2616     warning (0, "-malign-power is not supported for 64-bit Darwin;"
2617              " it is incompatible with the installed C and C++ libraries");
2618
2619   if (global_options_set.x_rs6000_spe_abi
2620       && rs6000_spe_abi
2621       && !TARGET_SPE_ABI)
2622     error ("not configured for SPE ABI");
2623
2624   /* Numerous experiment shows that IRA based loop pressure
2625      calculation works better for RTL loop invariant motion on targets
2626      with enough (>= 32) registers.  It is an expensive optimization.
2627      So it is on only for peak performance.  */
2628   if (optimize >= 3 && global_init_p)
2629     flag_ira_loop_pressure = 1;
2630
2631   /* Set the pointer size.  */
2632   if (TARGET_64BIT)
2633     {
2634       rs6000_pmode = (int)DImode;
2635       rs6000_pointer_size = 64;
2636     }
2637   else
2638     {
2639       rs6000_pmode = (int)SImode;
2640       rs6000_pointer_size = 32;
2641     }
2642
2643   set_masks = POWER_MASKS | POWERPC_MASKS | MASK_SOFT_FLOAT;
2644 #ifdef OS_MISSING_POWERPC64
2645   if (OS_MISSING_POWERPC64)
2646     set_masks &= ~MASK_POWERPC64;
2647 #endif
2648 #ifdef OS_MISSING_ALTIVEC
2649   if (OS_MISSING_ALTIVEC)
2650     set_masks &= ~MASK_ALTIVEC;
2651 #endif
2652
2653   /* Don't override by the processor default if given explicitly.  */
2654   set_masks &= ~target_flags_explicit;
2655
2656   /* Process the -mcpu=<xxx> and -mtune=<xxx> argument.  If the user changed
2657      the cpu in a target attribute or pragma, but did not specify a tuning
2658      option, use the cpu for the tuning option rather than the option specified
2659      with -mtune on the command line.  */
2660   if (rs6000_cpu_index >= 0)
2661     {
2662       cpu_index = rs6000_cpu_index;
2663       have_cpu = true;
2664     }
2665   else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
2666     {
2667       rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
2668       have_cpu = true;
2669     }
2670   else
2671     {
2672       if (!default_cpu)
2673         default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
2674
2675       rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
2676     }
2677
2678   gcc_assert (cpu_index >= 0);
2679
2680   target_flags &= ~set_masks;
2681   target_flags |= (processor_target_table[cpu_index].target_enable
2682                    & set_masks);
2683
2684   if (rs6000_tune_index >= 0)
2685     tune_index = rs6000_tune_index;
2686   else if (have_cpu)
2687     rs6000_tune_index = tune_index = cpu_index;
2688   else
2689     {
2690       size_t i;
2691       enum processor_type tune_proc
2692         = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
2693
2694       tune_index = -1;
2695       for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2696         if (processor_target_table[i].processor == tune_proc)
2697           {
2698             rs6000_tune_index = tune_index = i;
2699             break;
2700           }
2701     }
2702
2703   gcc_assert (tune_index >= 0);
2704   rs6000_cpu = processor_target_table[tune_index].processor;
2705
2706   if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2707       || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64)
2708     {
2709       if (TARGET_ALTIVEC)
2710         error ("AltiVec not supported in this target");
2711       if (TARGET_SPE)
2712         error ("SPE not supported in this target");
2713     }
2714
2715   /* Disable Cell microcode if we are optimizing for the Cell
2716      and not optimizing for size.  */
2717   if (rs6000_gen_cell_microcode == -1)
2718     rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2719                                   && !optimize_size);
2720
2721   /* If we are optimizing big endian systems for space and it's OK to
2722      use instructions that would be microcoded on the Cell, use the
2723      load/store multiple and string instructions.  */
2724   if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2725     target_flags |= ~target_flags_explicit & (MASK_MULTIPLE | MASK_STRING);
2726
2727   /* Don't allow -mmultiple or -mstring on little endian systems
2728      unless the cpu is a 750, because the hardware doesn't support the
2729      instructions used in little endian mode, and causes an alignment
2730      trap.  The 750 does not cause an alignment trap (except when the
2731      target is unaligned).  */
2732
2733   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2734     {
2735       if (TARGET_MULTIPLE)
2736         {
2737           target_flags &= ~MASK_MULTIPLE;
2738           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
2739             warning (0, "-mmultiple is not supported on little endian systems");
2740         }
2741
2742       if (TARGET_STRING)
2743         {
2744           target_flags &= ~MASK_STRING;
2745           if ((target_flags_explicit & MASK_STRING) != 0)
2746             warning (0, "-mstring is not supported on little endian systems");
2747         }
2748     }
2749
2750   /* Add some warnings for VSX.  */
2751   if (TARGET_VSX)
2752     {
2753       const char *msg = NULL;
2754       if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2755           || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2756         {
2757           if (target_flags_explicit & MASK_VSX)
2758             msg = N_("-mvsx requires hardware floating point");
2759           else
2760             target_flags &= ~ MASK_VSX;
2761         }
2762       else if (TARGET_PAIRED_FLOAT)
2763         msg = N_("-mvsx and -mpaired are incompatible");
2764       /* The hardware will allow VSX and little endian, but until we make sure
2765          things like vector select, etc. work don't allow VSX on little endian
2766          systems at this point.  */
2767       else if (!BYTES_BIG_ENDIAN)
2768         msg = N_("-mvsx used with little endian code");
2769       else if (TARGET_AVOID_XFORM > 0)
2770         msg = N_("-mvsx needs indexed addressing");
2771       else if (!TARGET_ALTIVEC && (target_flags_explicit & MASK_ALTIVEC))
2772         {
2773           if (target_flags_explicit & MASK_VSX)
2774             msg = N_("-mvsx and -mno-altivec are incompatible");
2775           else
2776             msg = N_("-mno-altivec disables vsx");
2777         }
2778
2779       if (msg)
2780         {
2781           warning (0, msg);
2782           target_flags &= ~ MASK_VSX;
2783           target_flags_explicit |= MASK_VSX;
2784         }
2785     }
2786
2787   /* For the newer switches (vsx, dfp, etc.) set some of the older options,
2788      unless the user explicitly used the -mno-<option> to disable the code.  */
2789   if (TARGET_VSX)
2790     target_flags |= (ISA_2_6_MASKS_SERVER & ~target_flags_explicit);
2791   else if (TARGET_POPCNTD)
2792     target_flags |= (ISA_2_6_MASKS_EMBEDDED & ~target_flags_explicit);
2793   else if (TARGET_DFP)
2794     target_flags |= (ISA_2_5_MASKS_SERVER & ~target_flags_explicit);
2795   else if (TARGET_CMPB)
2796     target_flags |= (ISA_2_5_MASKS_EMBEDDED & ~target_flags_explicit);
2797   else if (TARGET_FPRND)
2798     target_flags |= (ISA_2_4_MASKS & ~target_flags_explicit);
2799   else if (TARGET_POPCNTB)
2800     target_flags |= (ISA_2_2_MASKS & ~target_flags_explicit);
2801   else if (TARGET_ALTIVEC)
2802     target_flags |= (MASK_PPC_GFXOPT & ~target_flags_explicit);
2803
2804   /* E500mc does "better" if we inline more aggressively.  Respect the
2805      user's opinion, though.  */
2806   if (rs6000_block_move_inline_limit == 0
2807       && (rs6000_cpu == PROCESSOR_PPCE500MC
2808           || rs6000_cpu == PROCESSOR_PPCE500MC64))
2809     rs6000_block_move_inline_limit = 128;
2810
2811   /* store_one_arg depends on expand_block_move to handle at least the
2812      size of reg_parm_stack_space.  */
2813   if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
2814     rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
2815
2816   if (global_init_p)
2817     {
2818       /* If the appropriate debug option is enabled, replace the target hooks
2819          with debug versions that call the real version and then prints
2820          debugging information.  */
2821       if (TARGET_DEBUG_COST)
2822         {
2823           targetm.rtx_costs = rs6000_debug_rtx_costs;
2824           targetm.address_cost = rs6000_debug_address_cost;
2825           targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
2826         }
2827
2828       if (TARGET_DEBUG_ADDR)
2829         {
2830           targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
2831           targetm.legitimize_address = rs6000_debug_legitimize_address;
2832           rs6000_secondary_reload_class_ptr
2833             = rs6000_debug_secondary_reload_class;
2834           rs6000_secondary_memory_needed_ptr
2835             = rs6000_debug_secondary_memory_needed;
2836           rs6000_cannot_change_mode_class_ptr
2837             = rs6000_debug_cannot_change_mode_class;
2838           rs6000_preferred_reload_class_ptr
2839             = rs6000_debug_preferred_reload_class;
2840           rs6000_legitimize_reload_address_ptr
2841             = rs6000_debug_legitimize_reload_address;
2842           rs6000_mode_dependent_address_ptr
2843             = rs6000_debug_mode_dependent_address;
2844         }
2845
2846       if (rs6000_veclibabi_name)
2847         {
2848           if (strcmp (rs6000_veclibabi_name, "mass") == 0)
2849             rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
2850           else
2851             {
2852               error ("unknown vectorization library ABI type (%s) for "
2853                      "-mveclibabi= switch", rs6000_veclibabi_name);
2854               ret = false;
2855             }
2856         }
2857     }
2858
2859   if (!global_options_set.x_rs6000_long_double_type_size)
2860     {
2861       if (main_target_opt != NULL
2862           && (main_target_opt->x_rs6000_long_double_type_size
2863               != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
2864         error ("target attribute or pragma changes long double size");
2865       else
2866         rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
2867     }
2868
2869 #ifndef POWERPC_LINUX
2870   if (!global_options_set.x_rs6000_ieeequad)
2871     rs6000_ieeequad = 1;
2872 #endif
2873
2874   /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
2875      target attribute or pragma which automatically enables both options,
2876      unless the altivec ABI was set.  This is set by default for 64-bit, but
2877      not for 32-bit.  */
2878   if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2879     target_flags &= ~((MASK_VSX | MASK_ALTIVEC) & ~target_flags_explicit);
2880
2881   /* Enable Altivec ABI for AIX -maltivec.  */
2882   if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
2883     {
2884       if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2885         error ("target attribute or pragma changes AltiVec ABI");
2886       else
2887         rs6000_altivec_abi = 1;
2888     }
2889
2890   /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux.  For
2891      PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI.  It can
2892      be explicitly overridden in either case.  */
2893   if (TARGET_ELF)
2894     {
2895       if (!global_options_set.x_rs6000_altivec_abi
2896           && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
2897         {
2898           if (main_target_opt != NULL &&
2899               !main_target_opt->x_rs6000_altivec_abi)
2900             error ("target attribute or pragma changes AltiVec ABI");
2901           else
2902             rs6000_altivec_abi = 1;
2903         }
2904
2905       /* Enable VRSAVE for AltiVec ABI, unless explicitly overridden.  */
2906       if (!global_options_set.x_TARGET_ALTIVEC_VRSAVE)
2907         TARGET_ALTIVEC_VRSAVE = rs6000_altivec_abi;
2908     }
2909
2910   /* Set the Darwin64 ABI as default for 64-bit Darwin.  
2911      So far, the only darwin64 targets are also MACH-O.  */
2912   if (TARGET_MACHO
2913       && DEFAULT_ABI == ABI_DARWIN 
2914       && TARGET_64BIT)
2915     {
2916       if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
2917         error ("target attribute or pragma changes darwin64 ABI");
2918       else
2919         {
2920           rs6000_darwin64_abi = 1;
2921           /* Default to natural alignment, for better performance.  */
2922           rs6000_alignment_flags = MASK_ALIGN_NATURAL;
2923         }
2924     }
2925
2926   /* Place FP constants in the constant pool instead of TOC
2927      if section anchors enabled.  */
2928   if (flag_section_anchors)
2929     TARGET_NO_FP_IN_TOC = 1;
2930
2931 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2932   SUBTARGET_OVERRIDE_OPTIONS;
2933 #endif
2934 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2935   SUBSUBTARGET_OVERRIDE_OPTIONS;
2936 #endif
2937 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
2938   SUB3TARGET_OVERRIDE_OPTIONS;
2939 #endif
2940
2941   if (TARGET_E500 || rs6000_cpu == PROCESSOR_PPCE500MC
2942       || rs6000_cpu == PROCESSOR_PPCE500MC64)
2943     {
2944       /* The e500 and e500mc do not have string instructions, and we set
2945          MASK_STRING above when optimizing for size.  */
2946       if ((target_flags & MASK_STRING) != 0)
2947         target_flags = target_flags & ~MASK_STRING;
2948     }
2949   else if (global_options_set.x_rs6000_cpu_index)
2950     {
2951       /* For the powerpc-eabispe configuration, we set all these by
2952          default, so let's unset them if we manually set another
2953          CPU that is not the E500.  */
2954       if (main_target_opt != NULL
2955           && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
2956               || (main_target_opt->x_rs6000_spe != rs6000_spe)
2957               || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
2958         error ("target attribute or pragma changes SPE ABI");
2959       else
2960         {
2961           if (!global_options_set.x_rs6000_spe_abi)
2962             rs6000_spe_abi = 0;
2963           if (!global_options_set.x_rs6000_spe)
2964             rs6000_spe = 0;
2965           if (!global_options_set.x_rs6000_float_gprs)
2966             rs6000_float_gprs = 0;
2967         }
2968       if (!(target_flags_explicit & MASK_ISEL))
2969         target_flags &= ~MASK_ISEL;
2970     }
2971
2972   /* Detect invalid option combinations with E500.  */
2973   CHECK_E500_OPTIONS;
2974
2975   rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
2976                         && rs6000_cpu != PROCESSOR_POWER5
2977                         && rs6000_cpu != PROCESSOR_POWER6
2978                         && rs6000_cpu != PROCESSOR_POWER7
2979                         && rs6000_cpu != PROCESSOR_PPCA2
2980                         && rs6000_cpu != PROCESSOR_CELL
2981                         && rs6000_cpu != PROCESSOR_PPC476);
2982   rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
2983                          || rs6000_cpu == PROCESSOR_POWER5
2984                          || rs6000_cpu == PROCESSOR_POWER7);
2985   rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
2986                                  || rs6000_cpu == PROCESSOR_POWER5
2987                                  || rs6000_cpu == PROCESSOR_POWER6
2988                                  || rs6000_cpu == PROCESSOR_POWER7
2989                                  || rs6000_cpu == PROCESSOR_PPCE500MC
2990                                  || rs6000_cpu == PROCESSOR_PPCE500MC64);
2991
2992   /* Allow debug switches to override the above settings.  These are set to -1
2993      in rs6000.opt to indicate the user hasn't directly set the switch.  */
2994   if (TARGET_ALWAYS_HINT >= 0)
2995     rs6000_always_hint = TARGET_ALWAYS_HINT;
2996
2997   if (TARGET_SCHED_GROUPS >= 0)
2998     rs6000_sched_groups = TARGET_SCHED_GROUPS;
2999
3000   if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3001     rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3002
3003   rs6000_sched_restricted_insns_priority
3004     = (rs6000_sched_groups ? 1 : 0);
3005
3006   /* Handle -msched-costly-dep option.  */
3007   rs6000_sched_costly_dep
3008     = (rs6000_sched_groups ? store_to_load_dep_costly : no_dep_costly);
3009
3010   if (rs6000_sched_costly_dep_str)
3011     {
3012       if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3013         rs6000_sched_costly_dep = no_dep_costly;
3014       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3015         rs6000_sched_costly_dep = all_deps_costly;
3016       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3017         rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3018       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3019         rs6000_sched_costly_dep = store_to_load_dep_costly;
3020       else
3021         rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3022                                    atoi (rs6000_sched_costly_dep_str));
3023     }
3024
3025   /* Handle -minsert-sched-nops option.  */
3026   rs6000_sched_insert_nops
3027     = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3028
3029   if (rs6000_sched_insert_nops_str)
3030     {
3031       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3032         rs6000_sched_insert_nops = sched_finish_none;
3033       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3034         rs6000_sched_insert_nops = sched_finish_pad_groups;
3035       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3036         rs6000_sched_insert_nops = sched_finish_regroup_exact;
3037       else
3038         rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3039                                     atoi (rs6000_sched_insert_nops_str));
3040     }
3041
3042   if (global_init_p)
3043     {
3044 #ifdef TARGET_REGNAMES
3045       /* If the user desires alternate register names, copy in the
3046          alternate names now.  */
3047       if (TARGET_REGNAMES)
3048         memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3049 #endif
3050
3051       /* Set aix_struct_return last, after the ABI is determined.
3052          If -maix-struct-return or -msvr4-struct-return was explicitly
3053          used, don't override with the ABI default.  */
3054       if (!global_options_set.x_aix_struct_return)
3055         aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3056
3057 #if 0
3058       /* IBM XL compiler defaults to unsigned bitfields.  */
3059       if (TARGET_XL_COMPAT)
3060         flag_signed_bitfields = 0;
3061 #endif
3062
3063       if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3064         REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3065
3066       if (TARGET_TOC)
3067         ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3068
3069       /* We can only guarantee the availability of DI pseudo-ops when
3070          assembling for 64-bit targets.  */
3071       if (!TARGET_64BIT)
3072         {
3073           targetm.asm_out.aligned_op.di = NULL;
3074           targetm.asm_out.unaligned_op.di = NULL;
3075         }
3076
3077
3078       /* Set branch target alignment, if not optimizing for size.  */
3079       if (!optimize_size)
3080         {
3081           /* Cell wants to be aligned 8byte for dual issue.  Titan wants to be
3082              aligned 8byte to avoid misprediction by the branch predictor.  */
3083           if (rs6000_cpu == PROCESSOR_TITAN
3084               || rs6000_cpu == PROCESSOR_CELL)
3085             {
3086               if (align_functions <= 0)
3087                 align_functions = 8;
3088               if (align_jumps <= 0)
3089                 align_jumps = 8;
3090               if (align_loops <= 0)
3091                 align_loops = 8;
3092             }
3093           if (rs6000_align_branch_targets)
3094             {
3095               if (align_functions <= 0)
3096                 align_functions = 16;
3097               if (align_jumps <= 0)
3098                 align_jumps = 16;
3099               if (align_loops <= 0)
3100                 {
3101                   can_override_loop_align = 1;
3102                   align_loops = 16;
3103                 }
3104             }
3105           if (align_jumps_max_skip <= 0)
3106             align_jumps_max_skip = 15;
3107           if (align_loops_max_skip <= 0)
3108             align_loops_max_skip = 15;
3109         }
3110
3111       /* Arrange to save and restore machine status around nested functions.  */
3112       init_machine_status = rs6000_init_machine_status;
3113
3114       /* We should always be splitting complex arguments, but we can't break
3115          Linux and Darwin ABIs at the moment.  For now, only AIX is fixed.  */
3116       if (DEFAULT_ABI != ABI_AIX)
3117         targetm.calls.split_complex_arg = NULL;
3118     }
3119
3120   /* Initialize rs6000_cost with the appropriate target costs.  */
3121   if (optimize_size)
3122     rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3123   else
3124     switch (rs6000_cpu)
3125       {
3126       case PROCESSOR_RIOS1:
3127         rs6000_cost = &rios1_cost;
3128         break;
3129
3130       case PROCESSOR_RIOS2:
3131         rs6000_cost = &rios2_cost;
3132         break;
3133
3134       case PROCESSOR_RS64A:
3135         rs6000_cost = &rs64a_cost;
3136         break;
3137
3138       case PROCESSOR_MPCCORE:
3139         rs6000_cost = &mpccore_cost;
3140         break;
3141
3142       case PROCESSOR_PPC403:
3143         rs6000_cost = &ppc403_cost;
3144         break;
3145
3146       case PROCESSOR_PPC405:
3147         rs6000_cost = &ppc405_cost;
3148         break;
3149
3150       case PROCESSOR_PPC440:
3151         rs6000_cost = &ppc440_cost;
3152         break;
3153
3154       case PROCESSOR_PPC476:
3155         rs6000_cost = &ppc476_cost;
3156         break;
3157
3158       case PROCESSOR_PPC601:
3159         rs6000_cost = &ppc601_cost;
3160         break;
3161
3162       case PROCESSOR_PPC603:
3163         rs6000_cost = &ppc603_cost;
3164         break;
3165
3166       case PROCESSOR_PPC604:
3167         rs6000_cost = &ppc604_cost;
3168         break;
3169
3170       case PROCESSOR_PPC604e:
3171         rs6000_cost = &ppc604e_cost;
3172         break;
3173
3174       case PROCESSOR_PPC620:
3175         rs6000_cost = &ppc620_cost;
3176         break;
3177
3178       case PROCESSOR_PPC630:
3179         rs6000_cost = &ppc630_cost;
3180         break;
3181
3182       case PROCESSOR_CELL:
3183         rs6000_cost = &ppccell_cost;
3184         break;
3185
3186       case PROCESSOR_PPC750:
3187       case PROCESSOR_PPC7400:
3188         rs6000_cost = &ppc750_cost;
3189         break;
3190
3191       case PROCESSOR_PPC7450:
3192         rs6000_cost = &ppc7450_cost;
3193         break;
3194
3195       case PROCESSOR_PPC8540:
3196         rs6000_cost = &ppc8540_cost;
3197         break;
3198
3199       case PROCESSOR_PPCE300C2:
3200       case PROCESSOR_PPCE300C3:
3201         rs6000_cost = &ppce300c2c3_cost;
3202         break;
3203
3204       case PROCESSOR_PPCE500MC:
3205         rs6000_cost = &ppce500mc_cost;
3206         break;
3207
3208       case PROCESSOR_PPCE500MC64:
3209         rs6000_cost = &ppce500mc64_cost;
3210         break;
3211
3212       case PROCESSOR_TITAN:
3213         rs6000_cost = &titan_cost;
3214         break;
3215
3216       case PROCESSOR_POWER4:
3217       case PROCESSOR_POWER5:
3218         rs6000_cost = &power4_cost;
3219         break;
3220
3221       case PROCESSOR_POWER6:
3222         rs6000_cost = &power6_cost;
3223         break;
3224
3225       case PROCESSOR_POWER7:
3226         rs6000_cost = &power7_cost;
3227         break;
3228
3229       case PROCESSOR_PPCA2:
3230         rs6000_cost = &ppca2_cost;
3231         break;
3232
3233       default:
3234         gcc_unreachable ();
3235       }
3236
3237   if (global_init_p)
3238     {
3239       maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3240                              rs6000_cost->simultaneous_prefetches,
3241                              global_options.x_param_values,
3242                              global_options_set.x_param_values);
3243       maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3244                              global_options.x_param_values,
3245                              global_options_set.x_param_values);
3246       maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3247                              rs6000_cost->cache_line_size,
3248                              global_options.x_param_values,
3249                              global_options_set.x_param_values);
3250       maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3251                              global_options.x_param_values,
3252                              global_options_set.x_param_values);
3253
3254       /* If using typedef char *va_list, signal that
3255          __builtin_va_start (&ap, 0) can be optimized to
3256          ap = __builtin_next_arg (0).  */
3257       if (DEFAULT_ABI != ABI_V4)
3258         targetm.expand_builtin_va_start = NULL;
3259     }
3260
3261   /* Set up single/double float flags.  
3262      If TARGET_HARD_FLOAT is set, but neither single or double is set, 
3263      then set both flags. */
3264   if (TARGET_HARD_FLOAT && TARGET_FPRS 
3265       && rs6000_single_float == 0 && rs6000_double_float == 0)
3266     rs6000_single_float = rs6000_double_float = 1;
3267
3268   /* Reset single and double FP flags if target is E500. */
3269   if (TARGET_E500) 
3270   {
3271     rs6000_single_float = rs6000_double_float = 0;
3272     if (TARGET_E500_SINGLE)
3273       rs6000_single_float = 1; 
3274     if (TARGET_E500_DOUBLE)
3275       rs6000_single_float = rs6000_double_float = 1;
3276   }
3277
3278   if (main_target_opt)
3279     {
3280       if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3281         error ("target attribute or pragma changes single precision floating "
3282                "point");
3283       if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3284         error ("target attribute or pragma changes double precision floating "
3285                "point");
3286     }
3287
3288   /* If not explicitly specified via option, decide whether to generate indexed
3289      load/store instructions.  */
3290   if (TARGET_AVOID_XFORM == -1)
3291     /* Avoid indexed addressing when targeting Power6 in order to avoid the
3292      DERAT mispredict penalty.  However the LVE and STVE altivec instructions
3293      need indexed accesses and the type used is the scalar type of the element
3294      being loaded or stored.  */
3295     TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3296                           && !TARGET_ALTIVEC);
3297
3298   /* Set the -mrecip options.  */
3299   if (rs6000_recip_name)
3300     {
3301       char *p = ASTRDUP (rs6000_recip_name);
3302       char *q;
3303       unsigned int mask, i;
3304       bool invert;
3305
3306       while ((q = strtok (p, ",")) != NULL)
3307         {
3308           p = NULL;
3309           if (*q == '!')
3310             {
3311               invert = true;
3312               q++;
3313             }
3314           else
3315             invert = false;
3316
3317           if (!strcmp (q, "default"))
3318             mask = ((TARGET_RECIP_PRECISION)
3319                     ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3320           else
3321             {
3322               for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3323                 if (!strcmp (q, recip_options[i].string))
3324                   {
3325                     mask = recip_options[i].mask;
3326                     break;
3327                   }
3328
3329               if (i == ARRAY_SIZE (recip_options))
3330                 {
3331                   error ("unknown option for -mrecip=%s", q);
3332                   invert = false;
3333                   mask = 0;
3334                   ret = false;
3335                 }
3336             }
3337
3338           if (invert)
3339             rs6000_recip_control &= ~mask;
3340           else
3341             rs6000_recip_control |= mask;
3342         }
3343     }
3344
3345   /* Set the builtin mask of the various options used that could affect which
3346      builtins were used.  In the past we used target_flags, but we've run out
3347      of bits, and some options like SPE and PAIRED are no longer in
3348      target_flags.  */
3349   rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3350   if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3351     fprintf (stderr, "new builtin mask = 0x%x%s%s%s%s\n", rs6000_builtin_mask,
3352              (rs6000_builtin_mask & RS6000_BTM_ALTIVEC) ? ", altivec" : "",
3353              (rs6000_builtin_mask & RS6000_BTM_VSX)     ? ", vsx"     : "",
3354              (rs6000_builtin_mask & RS6000_BTM_PAIRED)  ? ", paired"  : "",
3355              (rs6000_builtin_mask & RS6000_BTM_SPE)     ? ", spe" : "");
3356
3357   /* Initialize all of the registers.  */
3358   rs6000_init_hard_regno_mode_ok (global_init_p);
3359
3360   /* Save the initial options in case the user does function specific options */
3361   if (global_init_p)
3362     target_option_default_node = target_option_current_node
3363       = build_target_option_node ();
3364
3365   /* If not explicitly specified via option, decide whether to generate the
3366      extra blr's required to preserve the link stack on some cpus (eg, 476).  */
3367   if (TARGET_LINK_STACK == -1)
3368     SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3369
3370   return ret;
3371 }
3372
3373 /* Implement TARGET_OPTION_OVERRIDE.  On the RS/6000 this is used to
3374    define the target cpu type.  */
3375
3376 static void
3377 rs6000_option_override (void)
3378 {
3379   (void) rs6000_option_override_internal (true);
3380 }
3381
3382 \f
3383 /* Implement targetm.vectorize.builtin_mask_for_load.  */
3384 static tree
3385 rs6000_builtin_mask_for_load (void)
3386 {
3387   if (TARGET_ALTIVEC || TARGET_VSX)
3388     return altivec_builtin_mask_for_load;
3389   else
3390     return 0;
3391 }
3392
3393 /* Implement LOOP_ALIGN. */
3394 int
3395 rs6000_loop_align (rtx label)
3396 {
3397   basic_block bb;
3398   int ninsns;
3399
3400   /* Don't override loop alignment if -falign-loops was specified. */
3401   if (!can_override_loop_align)
3402     return align_loops_log;
3403
3404   bb = BLOCK_FOR_INSN (label);
3405   ninsns = num_loop_insns(bb->loop_father);
3406
3407   /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3408   if (ninsns > 4 && ninsns <= 8
3409       && (rs6000_cpu == PROCESSOR_POWER4
3410           || rs6000_cpu == PROCESSOR_POWER5
3411           || rs6000_cpu == PROCESSOR_POWER6
3412           || rs6000_cpu == PROCESSOR_POWER7))
3413     return 5;
3414   else
3415     return align_loops_log;
3416 }
3417
3418 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3419 static int
3420 rs6000_loop_align_max_skip (rtx label)
3421 {
3422   return (1 << rs6000_loop_align (label)) - 1;
3423 }
3424
3425 /* Implement targetm.vectorize.builtin_mul_widen_even.  */
3426 static tree
3427 rs6000_builtin_mul_widen_even (tree type)
3428 {
3429   if (!TARGET_ALTIVEC)
3430     return NULL_TREE;
3431
3432   switch (TYPE_MODE (type))
3433     {
3434     case V8HImode:
3435       return TYPE_UNSIGNED (type)
3436             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULEUH_UNS]
3437             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULESH];
3438
3439     case V16QImode:
3440       return TYPE_UNSIGNED (type)
3441             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULEUB_UNS]
3442             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULESB];
3443     default:
3444       return NULL_TREE;
3445     }
3446 }
3447
3448 /* Implement targetm.vectorize.builtin_mul_widen_odd.  */
3449 static tree
3450 rs6000_builtin_mul_widen_odd (tree type)
3451 {
3452   if (!TARGET_ALTIVEC)
3453     return NULL_TREE;
3454
3455   switch (TYPE_MODE (type))
3456     {
3457     case V8HImode:
3458       return TYPE_UNSIGNED (type)
3459             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOUH_UNS]
3460             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOSH];
3461
3462     case V16QImode:
3463       return TYPE_UNSIGNED (type)
3464             ? rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOUB_UNS]
3465             : rs6000_builtin_decls[ALTIVEC_BUILTIN_VMULOSB];
3466     default:
3467       return NULL_TREE;
3468     }
3469 }
3470
3471
3472 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3473    after applying N number of iterations.  This routine does not determine
3474    how may iterations are required to reach desired alignment.  */
3475
3476 static bool
3477 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3478 {
3479   if (is_packed)
3480     return false;
3481
3482   if (TARGET_32BIT)
3483     {
3484       if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3485         return true;
3486
3487       if (rs6000_alignment_flags ==  MASK_ALIGN_POWER)
3488         return true;
3489
3490       return false;
3491     }
3492   else
3493     {
3494       if (TARGET_MACHO)
3495         return false;
3496
3497       /* Assuming that all other types are naturally aligned. CHECKME!  */
3498       return true;
3499     }
3500 }
3501
3502 /* Return true if the vector misalignment factor is supported by the
3503    target.  */ 
3504 bool
3505 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3506                                             const_tree type,
3507                                             int misalignment,
3508                                             bool is_packed)
3509 {
3510   if (TARGET_VSX)
3511     {
3512       /* Return if movmisalign pattern is not supported for this mode.  */
3513       if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
3514         return false;
3515
3516       if (misalignment == -1)
3517         {
3518           /* Misalignment factor is unknown at compile time but we know
3519              it's word aligned.  */
3520           if (rs6000_vector_alignment_reachable (type, is_packed))
3521             {
3522               int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
3523
3524               if (element_size == 64 || element_size == 32)
3525                return true;
3526             }
3527
3528           return false;
3529         }
3530
3531       /* VSX supports word-aligned vector.  */
3532       if (misalignment % 4 == 0)
3533         return true;
3534     }
3535   return false;
3536 }
3537
3538 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
3539 static int
3540 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3541                                    tree vectype, int misalign)
3542 {
3543   unsigned elements;
3544
3545   switch (type_of_cost)
3546     {
3547       case scalar_stmt:
3548       case scalar_load:
3549       case scalar_store:
3550       case vector_stmt:
3551       case vector_load:
3552       case vector_store:
3553       case vec_to_scalar:
3554       case scalar_to_vec:
3555       case cond_branch_not_taken:
3556         return 1;
3557
3558       case vec_perm:
3559         if (TARGET_VSX)
3560           return 4;
3561         else
3562           return 1;
3563
3564       case vec_promote_demote:
3565         if (TARGET_VSX)
3566           return 5;
3567         else
3568           return 1;
3569
3570       case cond_branch_taken:
3571         return 3;
3572
3573       case unaligned_load:
3574         if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3575           {
3576             elements = TYPE_VECTOR_SUBPARTS (vectype);
3577             if (elements == 2)
3578               /* Double word aligned.  */
3579               return 2;
3580
3581             if (elements == 4)
3582               {
3583                 switch (misalign)
3584                   {
3585                     case 8:
3586                       /* Double word aligned.  */
3587                       return 2;
3588
3589                     case -1:
3590                       /* Unknown misalignment.  */
3591                     case 4:
3592                     case 12:
3593                       /* Word aligned.  */
3594                       return 22;
3595
3596                     default:
3597                       gcc_unreachable ();
3598                   }
3599               }
3600           }
3601
3602         if (TARGET_ALTIVEC)
3603           /* Misaligned loads are not supported.  */
3604           gcc_unreachable ();
3605
3606         return 2;
3607
3608       case unaligned_store:
3609         if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3610           {
3611             elements = TYPE_VECTOR_SUBPARTS (vectype);
3612             if (elements == 2)
3613               /* Double word aligned.  */
3614               return 2;
3615
3616             if (elements == 4)
3617               {
3618                 switch (misalign)
3619                   {
3620                     case 8:
3621                       /* Double word aligned.  */
3622                       return 2;
3623
3624                     case -1:
3625                       /* Unknown misalignment.  */
3626                     case 4:
3627                     case 12:
3628                       /* Word aligned.  */
3629                       return 23;
3630
3631                     default:
3632                       gcc_unreachable ();
3633                   }
3634               }
3635           }
3636
3637         if (TARGET_ALTIVEC)
3638           /* Misaligned stores are not supported.  */
3639           gcc_unreachable ();
3640
3641         return 2;
3642
3643       default:
3644         gcc_unreachable ();
3645     }
3646 }
3647
3648 /* Implement targetm.vectorize.preferred_simd_mode.  */
3649
3650 static enum machine_mode
3651 rs6000_preferred_simd_mode (enum machine_mode mode)
3652 {
3653   if (TARGET_VSX)
3654     switch (mode)
3655       {
3656       case DFmode:
3657         return V2DFmode;
3658       default:;
3659       }
3660   if (TARGET_ALTIVEC || TARGET_VSX)
3661     switch (mode)
3662       {
3663       case SFmode:
3664         return V4SFmode;
3665       case DImode:
3666         return V2DImode;
3667       case SImode:
3668         return V4SImode;
3669       case HImode:
3670         return V8HImode;
3671       case QImode:
3672         return V16QImode;
3673       default:;
3674       }
3675   if (TARGET_SPE)
3676     switch (mode)
3677       {
3678       case SFmode:
3679         return V2SFmode;
3680       case SImode:
3681         return V2SImode;
3682       default:;
3683       }
3684   if (TARGET_PAIRED_FLOAT
3685       && mode == SFmode)
3686     return V2SFmode;
3687   return word_mode;
3688 }
3689
3690 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
3691    library with vectorized intrinsics.  */
3692
3693 static tree
3694 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
3695 {
3696   char name[32];
3697   const char *suffix = NULL;
3698   tree fntype, new_fndecl, bdecl = NULL_TREE;
3699   int n_args = 1;
3700   const char *bname;
3701   enum machine_mode el_mode, in_mode;
3702   int n, in_n;
3703
3704   /* Libmass is suitable for unsafe math only as it does not correctly support
3705      parts of IEEE with the required precision such as denormals.  Only support
3706      it if we have VSX to use the simd d2 or f4 functions.
3707      XXX: Add variable length support.  */
3708   if (!flag_unsafe_math_optimizations || !TARGET_VSX)
3709     return NULL_TREE;
3710
3711   el_mode = TYPE_MODE (TREE_TYPE (type_out));
3712   n = TYPE_VECTOR_SUBPARTS (type_out);
3713   in_mode = TYPE_MODE (TREE_TYPE (type_in));
3714   in_n = TYPE_VECTOR_SUBPARTS (type_in);
3715   if (el_mode != in_mode
3716       || n != in_n)
3717     return NULL_TREE;
3718
3719   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3720     {
3721       enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3722       switch (fn)
3723         {
3724         case BUILT_IN_ATAN2:
3725         case BUILT_IN_HYPOT:
3726         case BUILT_IN_POW:
3727           n_args = 2;
3728           /* fall through */
3729
3730         case BUILT_IN_ACOS:
3731         case BUILT_IN_ACOSH:
3732         case BUILT_IN_ASIN:
3733         case BUILT_IN_ASINH:
3734         case BUILT_IN_ATAN:
3735         case BUILT_IN_ATANH:
3736         case BUILT_IN_CBRT:
3737         case BUILT_IN_COS:
3738         case BUILT_IN_COSH:
3739         case BUILT_IN_ERF:
3740         case BUILT_IN_ERFC:
3741         case BUILT_IN_EXP2:
3742         case BUILT_IN_EXP:
3743         case BUILT_IN_EXPM1:
3744         case BUILT_IN_LGAMMA:
3745         case BUILT_IN_LOG10:
3746         case BUILT_IN_LOG1P:
3747         case BUILT_IN_LOG2:
3748         case BUILT_IN_LOG:
3749         case BUILT_IN_SIN:
3750         case BUILT_IN_SINH:
3751         case BUILT_IN_SQRT:
3752         case BUILT_IN_TAN:
3753         case BUILT_IN_TANH:
3754           bdecl = builtin_decl_implicit (fn);
3755           suffix = "d2";                                /* pow -> powd2 */
3756           if (el_mode != DFmode
3757               || n != 2)
3758             return NULL_TREE;
3759           break;
3760
3761         case BUILT_IN_ATAN2F:
3762         case BUILT_IN_HYPOTF:
3763         case BUILT_IN_POWF:
3764           n_args = 2;
3765           /* fall through */
3766
3767         case BUILT_IN_ACOSF:
3768         case BUILT_IN_ACOSHF:
3769         case BUILT_IN_ASINF:
3770         case BUILT_IN_ASINHF:
3771         case BUILT_IN_ATANF:
3772         case BUILT_IN_ATANHF:
3773         case BUILT_IN_CBRTF:
3774         case BUILT_IN_COSF:
3775         case BUILT_IN_COSHF:
3776         case BUILT_IN_ERFF:
3777         case BUILT_IN_ERFCF:
3778         case BUILT_IN_EXP2F:
3779         case BUILT_IN_EXPF:
3780         case BUILT_IN_EXPM1F:
3781         case BUILT_IN_LGAMMAF:
3782         case BUILT_IN_LOG10F:
3783         case BUILT_IN_LOG1PF:
3784         case BUILT_IN_LOG2F:
3785         case BUILT_IN_LOGF:
3786         case BUILT_IN_SINF:
3787         case BUILT_IN_SINHF:
3788         case BUILT_IN_SQRTF:
3789         case BUILT_IN_TANF:
3790         case BUILT_IN_TANHF:
3791           bdecl = builtin_decl_implicit (fn);
3792           suffix = "4";                                 /* powf -> powf4 */
3793           if (el_mode != SFmode
3794               || n != 4)
3795             return NULL_TREE;
3796           break;
3797
3798         default:
3799           return NULL_TREE;
3800         }
3801     }
3802   else
3803     return NULL_TREE;
3804
3805   gcc_assert (suffix != NULL);
3806   bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
3807   strcpy (name, bname + sizeof ("__builtin_") - 1);
3808   strcat (name, suffix);
3809
3810   if (n_args == 1)
3811     fntype = build_function_type_list (type_out, type_in, NULL);
3812   else if (n_args == 2)
3813     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
3814   else
3815     gcc_unreachable ();
3816
3817   /* Build a function declaration for the vectorized function.  */
3818   new_fndecl = build_decl (BUILTINS_LOCATION,
3819                            FUNCTION_DECL, get_identifier (name), fntype);
3820   TREE_PUBLIC (new_fndecl) = 1;
3821   DECL_EXTERNAL (new_fndecl) = 1;
3822   DECL_IS_NOVOPS (new_fndecl) = 1;
3823   TREE_READONLY (new_fndecl) = 1;
3824
3825   return new_fndecl;
3826 }
3827
3828 /* Returns a function decl for a vectorized version of the builtin function
3829    with builtin function code FN and the result vector type TYPE, or NULL_TREE
3830    if it is not available.  */
3831
3832 static tree
3833 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
3834                                     tree type_in)
3835 {
3836   enum machine_mode in_mode, out_mode;
3837   int in_n, out_n;
3838
3839   if (TARGET_DEBUG_BUILTIN)
3840     fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
3841              IDENTIFIER_POINTER (DECL_NAME (fndecl)),
3842              GET_MODE_NAME (TYPE_MODE (type_out)),
3843              GET_MODE_NAME (TYPE_MODE (type_in)));
3844
3845   if (TREE_CODE (type_out) != VECTOR_TYPE
3846       || TREE_CODE (type_in) != VECTOR_TYPE
3847       || !TARGET_VECTORIZE_BUILTINS)
3848     return NULL_TREE;
3849
3850   out_mode = TYPE_MODE (TREE_TYPE (type_out));
3851   out_n = TYPE_VECTOR_SUBPARTS (type_out);
3852   in_mode = TYPE_MODE (TREE_TYPE (type_in));
3853   in_n = TYPE_VECTOR_SUBPARTS (type_in);
3854
3855   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3856     {
3857       enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3858       switch (fn)
3859         {
3860         case BUILT_IN_COPYSIGN:
3861           if (VECTOR_UNIT_VSX_P (V2DFmode)
3862               && out_mode == DFmode && out_n == 2
3863               && in_mode == DFmode && in_n == 2)
3864             return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
3865           break;
3866         case BUILT_IN_COPYSIGNF:
3867           if (out_mode != SFmode || out_n != 4
3868               || in_mode != SFmode || in_n != 4)
3869             break;
3870           if (VECTOR_UNIT_VSX_P (V4SFmode))
3871             return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
3872           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3873             return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
3874           break;
3875         case BUILT_IN_SQRT:
3876           if (VECTOR_UNIT_VSX_P (V2DFmode)
3877               && out_mode == DFmode && out_n == 2
3878               && in_mode == DFmode && in_n == 2)
3879             return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
3880           break;
3881         case BUILT_IN_SQRTF:
3882           if (VECTOR_UNIT_VSX_P (V4SFmode)
3883               && out_mode == SFmode && out_n == 4
3884               && in_mode == SFmode && in_n == 4)
3885             return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
3886           break;
3887         case BUILT_IN_CEIL:
3888           if (VECTOR_UNIT_VSX_P (V2DFmode)
3889               && out_mode == DFmode && out_n == 2
3890               && in_mode == DFmode && in_n == 2)
3891             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
3892           break;
3893         case BUILT_IN_CEILF:
3894           if (out_mode != SFmode || out_n != 4
3895               || in_mode != SFmode || in_n != 4)
3896             break;
3897           if (VECTOR_UNIT_VSX_P (V4SFmode))
3898             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
3899           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3900             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
3901           break;
3902         case BUILT_IN_FLOOR:
3903           if (VECTOR_UNIT_VSX_P (V2DFmode)
3904               && out_mode == DFmode && out_n == 2
3905               && in_mode == DFmode && in_n == 2)
3906             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
3907           break;
3908         case BUILT_IN_FLOORF:
3909           if (out_mode != SFmode || out_n != 4
3910               || in_mode != SFmode || in_n != 4)
3911             break;
3912           if (VECTOR_UNIT_VSX_P (V4SFmode))
3913             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
3914           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3915             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
3916           break;
3917         case BUILT_IN_FMA:
3918           if (VECTOR_UNIT_VSX_P (V2DFmode)
3919               && out_mode == DFmode && out_n == 2
3920               && in_mode == DFmode && in_n == 2)
3921             return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
3922           break;
3923         case BUILT_IN_FMAF:
3924           if (VECTOR_UNIT_VSX_P (V4SFmode)
3925               && out_mode == SFmode && out_n == 4
3926               && in_mode == SFmode && in_n == 4)
3927             return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
3928           else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
3929               && out_mode == SFmode && out_n == 4
3930               && in_mode == SFmode && in_n == 4)
3931             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
3932           break;
3933         case BUILT_IN_TRUNC:
3934           if (VECTOR_UNIT_VSX_P (V2DFmode)
3935               && out_mode == DFmode && out_n == 2
3936               && in_mode == DFmode && in_n == 2)
3937             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
3938           break;
3939         case BUILT_IN_TRUNCF:
3940           if (out_mode != SFmode || out_n != 4
3941               || in_mode != SFmode || in_n != 4)
3942             break;
3943           if (VECTOR_UNIT_VSX_P (V4SFmode))
3944             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
3945           if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3946             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
3947           break;
3948         case BUILT_IN_NEARBYINT:
3949           if (VECTOR_UNIT_VSX_P (V2DFmode)
3950               && flag_unsafe_math_optimizations
3951               && out_mode == DFmode && out_n == 2
3952               && in_mode == DFmode && in_n == 2)
3953             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
3954           break;
3955         case BUILT_IN_NEARBYINTF:
3956           if (VECTOR_UNIT_VSX_P (V4SFmode)
3957               && flag_unsafe_math_optimizations
3958               && out_mode == SFmode && out_n == 4
3959               && in_mode == SFmode && in_n == 4)
3960             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
3961           break;
3962         case BUILT_IN_RINT:
3963           if (VECTOR_UNIT_VSX_P (V2DFmode)
3964               && !flag_trapping_math
3965               && out_mode == DFmode && out_n == 2
3966               && in_mode == DFmode && in_n == 2)
3967             return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
3968           break;
3969         case BUILT_IN_RINTF:
3970           if (VECTOR_UNIT_VSX_P (V4SFmode)
3971               && !flag_trapping_math
3972               && out_mode == SFmode && out_n == 4
3973               && in_mode == SFmode && in_n == 4)
3974             return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
3975           break;
3976         default:
3977           break;
3978         }
3979     }
3980
3981   else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3982     {
3983       enum rs6000_builtins fn
3984         = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
3985       switch (fn)
3986         {
3987         case RS6000_BUILTIN_RSQRTF:
3988           if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3989               && out_mode == SFmode && out_n == 4
3990               && in_mode == SFmode && in_n == 4)
3991             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
3992           break;
3993         case RS6000_BUILTIN_RSQRT:
3994           if (VECTOR_UNIT_VSX_P (V2DFmode)
3995               && out_mode == DFmode && out_n == 2
3996               && in_mode == DFmode && in_n == 2)
3997             return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
3998           break;
3999         case RS6000_BUILTIN_RECIPF:
4000           if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4001               && out_mode == SFmode && out_n == 4
4002               && in_mode == SFmode && in_n == 4)
4003             return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4004           break;
4005         case RS6000_BUILTIN_RECIP:
4006           if (VECTOR_UNIT_VSX_P (V2DFmode)
4007               && out_mode == DFmode && out_n == 2
4008               && in_mode == DFmode && in_n == 2)
4009             return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4010           break;
4011         default:
4012           break;
4013         }
4014     }
4015
4016   /* Generate calls to libmass if appropriate.  */
4017   if (rs6000_veclib_handler)
4018     return rs6000_veclib_handler (fndecl, type_out, type_in);
4019
4020   return NULL_TREE;
4021 }
4022 \f
4023 /* Default CPU string for rs6000*_file_start functions.  */
4024 static const char *rs6000_default_cpu;
4025
4026 /* Do anything needed at the start of the asm file.  */
4027
4028 static void
4029 rs6000_file_start (void)
4030 {
4031   char buffer[80];
4032   const char *start = buffer;
4033   FILE *file = asm_out_file;
4034
4035   rs6000_default_cpu = TARGET_CPU_DEFAULT;
4036
4037   default_file_start ();
4038
4039   if (flag_verbose_asm)
4040     {
4041       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4042
4043       if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4044         {
4045           fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4046           start = "";
4047         }
4048
4049       if (global_options_set.x_rs6000_cpu_index)
4050         {
4051           fprintf (file, "%s -mcpu=%s", start,
4052                    processor_target_table[rs6000_cpu_index].name);
4053           start = "";
4054         }
4055
4056       if (global_options_set.x_rs6000_tune_index)
4057         {
4058           fprintf (file, "%s -mtune=%s", start,
4059                    processor_target_table[rs6000_tune_index].name);
4060           start = "";
4061         }
4062
4063       if (PPC405_ERRATUM77)
4064         {
4065           fprintf (file, "%s PPC405CR_ERRATUM77", start);
4066           start = "";
4067         }
4068
4069 #ifdef USING_ELFOS_H
4070       switch (rs6000_sdata)
4071         {
4072         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4073         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4074         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4075         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4076         }
4077
4078       if (rs6000_sdata && g_switch_value)
4079         {
4080           fprintf (file, "%s -G %d", start,
4081                    g_switch_value);
4082           start = "";
4083         }
4084 #endif
4085
4086       if (*start == '\0')
4087         putc ('\n', file);
4088     }
4089
4090   if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4091     {
4092       switch_to_section (toc_section);
4093       switch_to_section (text_section);
4094     }
4095 }
4096
4097 \f
4098 /* Return nonzero if this function is known to have a null epilogue.  */
4099
4100 int
4101 direct_return (void)
4102 {
4103   if (reload_completed)
4104     {
4105       rs6000_stack_t *info = rs6000_stack_info ();
4106
4107       if (info->first_gp_reg_save == 32
4108           && info->first_fp_reg_save == 64
4109           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4110           && ! info->lr_save_p
4111           && ! info->cr_save_p
4112           && info->vrsave_mask == 0
4113           && ! info->push_p)
4114         return 1;
4115     }
4116
4117   return 0;
4118 }
4119
4120 /* Return the number of instructions it takes to form a constant in an
4121    integer register.  */
4122
4123 int
4124 num_insns_constant_wide (HOST_WIDE_INT value)
4125 {
4126   /* signed constant loadable with {cal|addi} */
4127   if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4128     return 1;
4129
4130   /* constant loadable with {cau|addis} */
4131   else if ((value & 0xffff) == 0
4132            && (value >> 31 == -1 || value >> 31 == 0))
4133     return 1;
4134
4135 #if HOST_BITS_PER_WIDE_INT == 64
4136   else if (TARGET_POWERPC64)
4137     {
4138       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4139       HOST_WIDE_INT high = value >> 31;
4140
4141       if (high == 0 || high == -1)
4142         return 2;
4143
4144       high >>= 1;
4145
4146       if (low == 0)
4147         return num_insns_constant_wide (high) + 1;
4148       else if (high == 0)
4149         return num_insns_constant_wide (low) + 1;
4150       else
4151         return (num_insns_constant_wide (high)
4152                 + num_insns_constant_wide (low) + 1);
4153     }
4154 #endif
4155
4156   else
4157     return 2;
4158 }
4159
4160 int
4161 num_insns_constant (rtx op, enum machine_mode mode)
4162 {
4163   HOST_WIDE_INT low, high;
4164
4165   switch (GET_CODE (op))
4166     {
4167     case CONST_INT:
4168 #if HOST_BITS_PER_WIDE_INT == 64
4169       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4170           && mask64_operand (op, mode))
4171         return 2;
4172       else
4173 #endif
4174         return num_insns_constant_wide (INTVAL (op));
4175
4176       case CONST_DOUBLE:
4177         if (mode == SFmode || mode == SDmode)
4178           {
4179             long l;
4180             REAL_VALUE_TYPE rv;
4181
4182             REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4183             if (DECIMAL_FLOAT_MODE_P (mode))
4184               REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4185             else
4186               REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4187             return num_insns_constant_wide ((HOST_WIDE_INT) l);
4188           }
4189
4190         if (mode == VOIDmode || mode == DImode)
4191           {
4192             high = CONST_DOUBLE_HIGH (op);
4193             low  = CONST_DOUBLE_LOW (op);
4194           }
4195         else
4196           {
4197             long l[2];
4198             REAL_VALUE_TYPE rv;
4199
4200             REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4201             if (DECIMAL_FLOAT_MODE_P (mode))
4202               REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4203             else
4204               REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4205             high = l[WORDS_BIG_ENDIAN == 0];
4206             low  = l[WORDS_BIG_ENDIAN != 0];
4207           }
4208
4209         if (TARGET_32BIT)
4210           return (num_insns_constant_wide (low)
4211                   + num_insns_constant_wide (high));
4212         else
4213           {
4214             if ((high == 0 && low >= 0)
4215                 || (high == -1 && low < 0))
4216               return num_insns_constant_wide (low);
4217
4218             else if (mask64_operand (op, mode))
4219               return 2;
4220
4221             else if (low == 0)
4222               return num_insns_constant_wide (high) + 1;
4223
4224             else
4225               return (num_insns_constant_wide (high)
4226                       + num_insns_constant_wide (low) + 1);
4227           }
4228
4229     default:
4230       gcc_unreachable ();
4231     }
4232 }
4233
4234 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4235    If the mode of OP is MODE_VECTOR_INT, this simply returns the
4236    corresponding element of the vector, but for V4SFmode and V2SFmode,
4237    the corresponding "float" is interpreted as an SImode integer.  */
4238
4239 HOST_WIDE_INT
4240 const_vector_elt_as_int (rtx op, unsigned int elt)
4241 {
4242   rtx tmp;
4243
4244   /* We can't handle V2DImode and V2DFmode vector constants here yet.  */
4245   gcc_assert (GET_MODE (op) != V2DImode
4246               && GET_MODE (op) != V2DFmode);
4247
4248   tmp = CONST_VECTOR_ELT (op, elt);
4249   if (GET_MODE (op) == V4SFmode
4250       || GET_MODE (op) == V2SFmode)
4251     tmp = gen_lowpart (SImode, tmp);
4252   return INTVAL (tmp);
4253 }
4254
4255 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4256    or vspltisw instruction.  OP is a CONST_VECTOR.  Which instruction is used
4257    depends on STEP and COPIES, one of which will be 1.  If COPIES > 1,
4258    all items are set to the same value and contain COPIES replicas of the
4259    vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4260    operand and the others are set to the value of the operand's msb.  */
4261
4262 static bool
4263 vspltis_constant (rtx op, unsigned step, unsigned copies)
4264 {
4265   enum machine_mode mode = GET_MODE (op);
4266   enum machine_mode inner = GET_MODE_INNER (mode);
4267
4268   unsigned i;
4269   unsigned nunits;
4270   unsigned bitsize;
4271   unsigned mask;
4272
4273   HOST_WIDE_INT val;
4274   HOST_WIDE_INT splat_val;
4275   HOST_WIDE_INT msb_val;
4276
4277   if (mode == V2DImode || mode == V2DFmode)
4278     return false;
4279
4280   nunits = GET_MODE_NUNITS (mode);
4281   bitsize = GET_MODE_BITSIZE (inner);
4282   mask = GET_MODE_MASK (inner);
4283
4284   val = const_vector_elt_as_int (op, nunits - 1);
4285   splat_val = val;
4286   msb_val = val > 0 ? 0 : -1;
4287
4288   /* Construct the value to be splatted, if possible.  If not, return 0.  */
4289   for (i = 2; i <= copies; i *= 2)
4290     {
4291       HOST_WIDE_INT small_val;
4292       bitsize /= 2;
4293       small_val = splat_val >> bitsize;
4294       mask >>= bitsize;
4295       if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4296         return false;
4297       splat_val = small_val;
4298     }
4299
4300   /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw].  */
4301   if (EASY_VECTOR_15 (splat_val))
4302     ;
4303
4304   /* Also check if we can splat, and then add the result to itself.  Do so if
4305      the value is positive, of if the splat instruction is using OP's mode;
4306      for splat_val < 0, the splat and the add should use the same mode.  */
4307   else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4308            && (splat_val >= 0 || (step == 1 && copies == 1)))
4309     ;
4310
4311   /* Also check if are loading up the most significant bit which can be done by
4312      loading up -1 and shifting the value left by -1.  */
4313   else if (EASY_VECTOR_MSB (splat_val, inner))
4314     ;
4315
4316   else
4317     return false;
4318
4319   /* Check if VAL is present in every STEP-th element, and the
4320      other elements are filled with its most significant bit.  */
4321   for (i = 0; i < nunits - 1; ++i)
4322     {
4323       HOST_WIDE_INT desired_val;
4324       if (((i + 1) & (step - 1)) == 0)
4325         desired_val = val;
4326       else
4327         desired_val = msb_val;
4328
4329       if (desired_val != const_vector_elt_as_int (op, i))
4330         return false;
4331     }
4332
4333   return true;
4334 }
4335
4336
4337 /* Return true if OP is of the given MODE and can be synthesized
4338    with a vspltisb, vspltish or vspltisw.  */
4339
4340 bool
4341 easy_altivec_constant (rtx op, enum machine_mode mode)
4342 {
4343   unsigned step, copies;
4344
4345   if (mode == VOIDmode)
4346     mode = GET_MODE (op);
4347   else if (mode != GET_MODE (op))
4348     return false;
4349
4350   /* V2DI/V2DF was added with VSX.  Only allow 0 and all 1's as easy
4351      constants.  */
4352   if (mode == V2DFmode)
4353     return zero_constant (op, mode);
4354
4355   if (mode == V2DImode)
4356     {
4357       /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
4358          easy.  */
4359       if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
4360           || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
4361         return false;
4362
4363       if (zero_constant (op, mode))
4364         return true;
4365
4366       if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
4367           && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
4368         return true;
4369
4370       return false;
4371     }
4372
4373   /* Start with a vspltisw.  */
4374   step = GET_MODE_NUNITS (mode) / 4;
4375   copies = 1;
4376
4377   if (vspltis_constant (op, step, copies))
4378     return true;
4379
4380   /* Then try with a vspltish.  */
4381   if (step == 1)
4382     copies <<= 1;
4383   else
4384     step >>= 1;
4385
4386   if (vspltis_constant (op, step, copies))
4387     return true;
4388
4389   /* And finally a vspltisb.  */
4390   if (step == 1)
4391     copies <<= 1;
4392   else
4393     step >>= 1;
4394
4395   if (vspltis_constant (op, step, copies))
4396     return true;
4397
4398   return false;
4399 }
4400
4401 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4402    result is OP.  Abort if it is not possible.  */
4403
4404 rtx
4405 gen_easy_altivec_constant (rtx op)
4406 {
4407   enum machine_mode mode = GET_MODE (op);
4408   int nunits = GET_MODE_NUNITS (mode);
4409   rtx last = CONST_VECTOR_ELT (op, nunits - 1);
4410   unsigned step = nunits / 4;
4411   unsigned copies = 1;
4412
4413   /* Start with a vspltisw.  */
4414   if (vspltis_constant (op, step, copies))
4415     return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, last));
4416
4417   /* Then try with a vspltish.  */
4418   if (step == 1)
4419     copies <<= 1;
4420   else
4421     step >>= 1;
4422
4423   if (vspltis_constant (op, step, copies))
4424     return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, last));
4425
4426   /* And finally a vspltisb.  */
4427   if (step == 1)
4428     copies <<= 1;
4429   else
4430     step >>= 1;
4431
4432   if (vspltis_constant (op, step, copies))
4433     return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, last));
4434
4435   gcc_unreachable ();
4436 }
4437
4438 const char *
4439 output_vec_const_move (rtx *operands)
4440 {
4441   int cst, cst2;
4442   enum machine_mode mode;
4443   rtx dest, vec;
4444
4445   dest = operands[0];
4446   vec = operands[1];
4447   mode = GET_MODE (dest);
4448
4449   if (TARGET_VSX)
4450     {
4451       if (zero_constant (vec, mode))
4452         return "xxlxor %x0,%x0,%x0";
4453
4454       if (mode == V2DImode
4455           && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
4456           && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
4457         return "vspltisw %0,-1";
4458     }
4459
4460   if (TARGET_ALTIVEC)
4461     {
4462       rtx splat_vec;
4463       if (zero_constant (vec, mode))
4464         return "vxor %0,%0,%0";
4465
4466       splat_vec = gen_easy_altivec_constant (vec);
4467       gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4468       operands[1] = XEXP (splat_vec, 0);
4469       if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4470         return "#";
4471
4472       switch (GET_MODE (splat_vec))
4473         {
4474         case V4SImode:
4475           return "vspltisw %0,%1";
4476
4477         case V8HImode:
4478           return "vspltish %0,%1";
4479
4480         case V16QImode:
4481           return "vspltisb %0,%1";
4482
4483         default:
4484           gcc_unreachable ();
4485         }
4486     }
4487
4488   gcc_assert (TARGET_SPE);
4489
4490   /* Vector constant 0 is handled as a splitter of V2SI, and in the
4491      pattern of V1DI, V4HI, and V2SF.
4492
4493      FIXME: We should probably return # and add post reload
4494      splitters for these, but this way is so easy ;-).  */
4495   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4496   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4497   operands[1] = CONST_VECTOR_ELT (vec, 0);
4498   operands[2] = CONST_VECTOR_ELT (vec, 1);
4499   if (cst == cst2)
4500     return "li %0,%1\n\tevmergelo %0,%0,%0";
4501   else
4502     return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4503 }
4504
4505 /* Initialize TARGET of vector PAIRED to VALS.  */
4506
4507 void
4508 paired_expand_vector_init (rtx target, rtx vals)
4509 {
4510   enum machine_mode mode = GET_MODE (target);
4511   int n_elts = GET_MODE_NUNITS (mode);
4512   int n_var = 0;
4513   rtx x, new_rtx, tmp, constant_op, op1, op2;
4514   int i;
4515
4516   for (i = 0; i < n_elts; ++i)
4517     {
4518       x = XVECEXP (vals, 0, i);
4519       if (!(CONST_INT_P (x)
4520             || GET_CODE (x) == CONST_DOUBLE
4521             || GET_CODE (x) == CONST_FIXED))
4522         ++n_var;
4523     }
4524   if (n_var == 0)
4525     {
4526       /* Load from constant pool.  */
4527       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4528       return;
4529     }
4530
4531   if (n_var == 2)
4532     {
4533       /* The vector is initialized only with non-constants.  */
4534       new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4535                                 XVECEXP (vals, 0, 1));
4536
4537       emit_move_insn (target, new_rtx);
4538       return;
4539     }
4540   
4541   /* One field is non-constant and the other one is a constant.  Load the
4542      constant from the constant pool and use ps_merge instruction to
4543      construct the whole vector.  */
4544   op1 = XVECEXP (vals, 0, 0);
4545   op2 = XVECEXP (vals, 0, 1);
4546
4547   constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4548
4549   tmp = gen_reg_rtx (GET_MODE (constant_op));
4550   emit_move_insn (tmp, constant_op);
4551
4552   if (CONSTANT_P (op1))
4553     new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4554   else
4555     new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4556
4557   emit_move_insn (target, new_rtx);
4558 }
4559
4560 void
4561 paired_expand_vector_move (rtx operands[])
4562 {
4563   rtx op0 = operands[0], op1 = operands[1];
4564
4565   emit_move_insn (op0, op1);
4566 }
4567
4568 /* Emit vector compare for code RCODE.  DEST is destination, OP1 and
4569    OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4570    operands for the relation operation COND.  This is a recursive
4571    function.  */
4572
4573 static void
4574 paired_emit_vector_compare (enum rtx_code rcode,
4575                             rtx dest, rtx op0, rtx op1,
4576                             rtx cc_op0, rtx cc_op1)
4577 {
4578   rtx tmp = gen_reg_rtx (V2SFmode);
4579   rtx tmp1, max, min;
4580
4581   gcc_assert (TARGET_PAIRED_FLOAT);
4582   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
4583
4584   switch (rcode)
4585     {
4586     case LT:
4587     case LTU:
4588       paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4589       return;
4590     case GE:
4591     case GEU:
4592       emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4593       emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
4594       return;
4595     case LE:
4596     case LEU:
4597       paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
4598       return;
4599     case GT:
4600       paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4601       return;
4602     case EQ:
4603       tmp1 = gen_reg_rtx (V2SFmode);
4604       max = gen_reg_rtx (V2SFmode);
4605       min = gen_reg_rtx (V2SFmode);
4606       gen_reg_rtx (V2SFmode);
4607       
4608       emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4609       emit_insn (gen_selv2sf4
4610                  (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4611       emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
4612       emit_insn (gen_selv2sf4
4613                  (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4614       emit_insn (gen_subv2sf3 (tmp1, min, max));
4615       emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
4616       return;
4617     case NE:
4618       paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
4619       return;
4620     case UNLE:
4621       paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4622       return;
4623     case UNLT:
4624       paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
4625       return;
4626     case UNGE:
4627       paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4628       return;
4629     case UNGT:
4630       paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
4631       return;
4632     default:
4633       gcc_unreachable ();
4634     }
4635
4636   return;
4637 }
4638
4639 /* Emit vector conditional expression.
4640    DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
4641    CC_OP0 and CC_OP1 are the two operands for the relation operation COND.  */
4642
4643 int
4644 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
4645                               rtx cond, rtx cc_op0, rtx cc_op1)
4646 {
4647   enum rtx_code rcode = GET_CODE (cond);
4648
4649   if (!TARGET_PAIRED_FLOAT)
4650     return 0;
4651
4652   paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
4653
4654   return 1;
4655 }
4656
4657 /* Initialize vector TARGET to VALS.  */
4658
4659 void
4660 rs6000_expand_vector_init (rtx target, rtx vals)
4661 {
4662   enum machine_mode mode = GET_MODE (target);
4663   enum machine_mode inner_mode = GET_MODE_INNER (mode);
4664   int n_elts = GET_MODE_NUNITS (mode);
4665   int n_var = 0, one_var = -1;
4666   bool all_same = true, all_const_zero = true;
4667   rtx x, mem;
4668   int i;
4669
4670   for (i = 0; i < n_elts; ++i)
4671     {
4672       x = XVECEXP (vals, 0, i);
4673       if (!(CONST_INT_P (x)
4674             || GET_CODE (x) == CONST_DOUBLE
4675             || GET_CODE (x) == CONST_FIXED))
4676         ++n_var, one_var = i;
4677       else if (x != CONST0_RTX (inner_mode))
4678         all_const_zero = false;
4679
4680       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
4681         all_same = false;
4682     }
4683
4684   if (n_var == 0)
4685     {
4686       rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
4687       bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
4688       if ((int_vector_p || TARGET_VSX) && all_const_zero)
4689         {
4690           /* Zero register.  */
4691           emit_insn (gen_rtx_SET (VOIDmode, target,
4692                                   gen_rtx_XOR (mode, target, target)));
4693           return;
4694         }
4695       else if (int_vector_p && easy_vector_constant (const_vec, mode))
4696         {
4697           /* Splat immediate.  */
4698           emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
4699           return;
4700         }
4701       else
4702         {
4703           /* Load from constant pool.  */
4704           emit_move_insn (target, const_vec);
4705           return;
4706         }
4707     }
4708
4709   /* Double word values on VSX can use xxpermdi or lxvdsx.  */
4710   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4711     {
4712       rtx op0 = XVECEXP (vals, 0, 0);
4713       rtx op1 = XVECEXP (vals, 0, 1);
4714       if (all_same)
4715         {
4716           if (!MEM_P (op0) && !REG_P (op0))
4717             op0 = force_reg (inner_mode, op0);
4718           if (mode == V2DFmode)
4719             emit_insn (gen_vsx_splat_v2df (target, op0));
4720           else
4721             emit_insn (gen_vsx_splat_v2di (target, op0));
4722         }
4723       else
4724         {
4725           op0 = force_reg (inner_mode, op0);
4726           op1 = force_reg (inner_mode, op1);
4727           if (mode == V2DFmode)
4728             emit_insn (gen_vsx_concat_v2df (target, op0, op1));
4729           else
4730             emit_insn (gen_vsx_concat_v2di (target, op0, op1));
4731         }
4732       return;
4733     }
4734
4735   /* With single precision floating point on VSX, know that internally single
4736      precision is actually represented as a double, and either make 2 V2DF
4737      vectors, and convert these vectors to single precision, or do one
4738      conversion, and splat the result to the other elements.  */
4739   if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
4740     {
4741       if (all_same)
4742         {
4743           rtx freg = gen_reg_rtx (V4SFmode);
4744           rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
4745
4746           emit_insn (gen_vsx_xscvdpsp_scalar (freg, sreg));
4747           emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
4748         }
4749       else
4750         {
4751           rtx dbl_even = gen_reg_rtx (V2DFmode);
4752           rtx dbl_odd  = gen_reg_rtx (V2DFmode);
4753           rtx flt_even = gen_reg_rtx (V4SFmode);
4754           rtx flt_odd  = gen_reg_rtx (V4SFmode);
4755           rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
4756           rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
4757           rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
4758           rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
4759
4760           emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
4761           emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
4762           emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
4763           emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
4764           rs6000_expand_extract_even (target, flt_even, flt_odd);
4765         }
4766       return;
4767     }
4768
4769   /* Store value to stack temp.  Load vector element.  Splat.  However, splat
4770      of 64-bit items is not supported on Altivec.  */
4771   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
4772     {
4773       mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode), 0);
4774       emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
4775                       XVECEXP (vals, 0, 0));
4776       x = gen_rtx_UNSPEC (VOIDmode,
4777                           gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4778       emit_insn (gen_rtx_PARALLEL (VOIDmode,
4779                                    gen_rtvec (2,
4780                                               gen_rtx_SET (VOIDmode,
4781                                                            target, mem),
4782                                               x)));
4783       x = gen_rtx_VEC_SELECT (inner_mode, target,
4784                               gen_rtx_PARALLEL (VOIDmode,
4785                                                 gen_rtvec (1, const0_rtx)));
4786       emit_insn (gen_rtx_SET (VOIDmode, target,
4787                               gen_rtx_VEC_DUPLICATE (mode, x)));
4788       return;
4789     }
4790
4791   /* One field is non-constant.  Load constant then overwrite
4792      varying field.  */
4793   if (n_var == 1)
4794     {
4795       rtx copy = copy_rtx (vals);
4796
4797       /* Load constant part of vector, substitute neighboring value for
4798          varying element.  */
4799       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
4800       rs6000_expand_vector_init (target, copy);
4801
4802       /* Insert variable.  */
4803       rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
4804       return;
4805     }
4806
4807   /* Construct the vector in memory one field at a time
4808      and load the whole vector.  */
4809   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
4810   for (i = 0; i < n_elts; i++)
4811     emit_move_insn (adjust_address_nv (mem, inner_mode,
4812                                     i * GET_MODE_SIZE (inner_mode)),
4813                     XVECEXP (vals, 0, i));
4814   emit_move_insn (target, mem);
4815 }
4816
4817 /* Set field ELT of TARGET to VAL.  */
4818
4819 void
4820 rs6000_expand_vector_set (rtx target, rtx val, int elt)
4821 {
4822   enum machine_mode mode = GET_MODE (target);
4823   enum machine_mode inner_mode = GET_MODE_INNER (mode);
4824   rtx reg = gen_reg_rtx (mode);
4825   rtx mask, mem, x;
4826   int width = GET_MODE_SIZE (inner_mode);
4827   int i;
4828
4829   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4830     {
4831       rtx (*set_func) (rtx, rtx, rtx, rtx)
4832         = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
4833       emit_insn (set_func (target, target, val, GEN_INT (elt)));
4834       return;
4835     }
4836
4837   /* Load single variable value.  */
4838   mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode), 0);
4839   emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
4840   x = gen_rtx_UNSPEC (VOIDmode,
4841                       gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4842   emit_insn (gen_rtx_PARALLEL (VOIDmode,
4843                                gen_rtvec (2,
4844                                           gen_rtx_SET (VOIDmode,
4845                                                        reg, mem),
4846                                           x)));
4847
4848   /* Linear sequence.  */
4849   mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
4850   for (i = 0; i < 16; ++i)
4851     XVECEXP (mask, 0, i) = GEN_INT (i);
4852
4853   /* Set permute mask to insert element into target.  */
4854   for (i = 0; i < width; ++i)
4855     XVECEXP (mask, 0, elt*width + i)
4856       = GEN_INT (i + 0x10);
4857   x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
4858   x = gen_rtx_UNSPEC (mode,
4859                       gen_rtvec (3, target, reg,
4860                                  force_reg (V16QImode, x)),
4861                       UNSPEC_VPERM);
4862   emit_insn (gen_rtx_SET (VOIDmode, target, x));
4863 }
4864
4865 /* Extract field ELT from VEC into TARGET.  */
4866
4867 void
4868 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
4869 {
4870   enum machine_mode mode = GET_MODE (vec);
4871   enum machine_mode inner_mode = GET_MODE_INNER (mode);
4872   rtx mem;
4873
4874   if (VECTOR_MEM_VSX_P (mode))
4875     {
4876       switch (mode)
4877         {
4878         default:
4879           break;
4880         case V2DFmode:
4881           emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
4882           return;
4883         case V2DImode:
4884           emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
4885           return;
4886         case V4SFmode:
4887           emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
4888           return;
4889         }
4890     }
4891
4892   /* Allocate mode-sized buffer.  */
4893   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
4894
4895   emit_move_insn (mem, vec);
4896
4897   /* Add offset to field within buffer matching vector element.  */
4898   mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
4899
4900   emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
4901 }
4902
4903 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
4904    implement ANDing by the mask IN.  */
4905 void
4906 build_mask64_2_operands (rtx in, rtx *out)
4907 {
4908 #if HOST_BITS_PER_WIDE_INT >= 64
4909   unsigned HOST_WIDE_INT c, lsb, m1, m2;
4910   int shift;
4911
4912   gcc_assert (GET_CODE (in) == CONST_INT);
4913
4914   c = INTVAL (in);
4915   if (c & 1)
4916     {
4917       /* Assume c initially something like 0x00fff000000fffff.  The idea
4918          is to rotate the word so that the middle ^^^^^^ group of zeros
4919          is at the MS end and can be cleared with an rldicl mask.  We then
4920          rotate back and clear off the MS    ^^ group of zeros with a
4921          second rldicl.  */
4922       c = ~c;                   /*   c == 0xff000ffffff00000 */
4923       lsb = c & -c;             /* lsb == 0x0000000000100000 */
4924       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
4925       c = ~c;                   /*   c == 0x00fff000000fffff */
4926       c &= -lsb;                /*   c == 0x00fff00000000000 */
4927       lsb = c & -c;             /* lsb == 0x0000100000000000 */
4928       c = ~c;                   /*   c == 0xff000fffffffffff */
4929       c &= -lsb;                /*   c == 0xff00000000000000 */
4930       shift = 0;
4931       while ((lsb >>= 1) != 0)
4932         shift++;                /* shift == 44 on exit from loop */
4933       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
4934       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
4935       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
4936     }
4937   else
4938     {
4939       /* Assume c initially something like 0xff000f0000000000.  The idea
4940          is to rotate the word so that the     ^^^  middle group of zeros
4941          is at the LS end and can be cleared with an rldicr mask.  We then
4942          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
4943          a second rldicr.  */
4944       lsb = c & -c;             /* lsb == 0x0000010000000000 */
4945       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
4946       c = ~c;                   /*   c == 0x00fff0ffffffffff */
4947       c &= -lsb;                /*   c == 0x00fff00000000000 */
4948       lsb = c & -c;             /* lsb == 0x0000100000000000 */
4949       c = ~c;                   /*   c == 0xff000fffffffffff */
4950       c &= -lsb;                /*   c == 0xff00000000000000 */
4951       shift = 0;
4952       while ((lsb >>= 1) != 0)
4953         shift++;                /* shift == 44 on exit from loop */
4954       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
4955       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
4956       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
4957     }
4958
4959   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
4960      masks will be all 1's.  We are guaranteed more than one transition.  */
4961   out[0] = GEN_INT (64 - shift);
4962   out[1] = GEN_INT (m1);
4963   out[2] = GEN_INT (shift);
4964   out[3] = GEN_INT (m2);
4965 #else
4966   (void)in;
4967   (void)out;
4968   gcc_unreachable ();
4969 #endif
4970 }
4971
4972 /* Return TRUE if OP is an invalid SUBREG operation on the e500.  */
4973
4974 bool
4975 invalid_e500_subreg (rtx op, enum machine_mode mode)
4976 {
4977   if (TARGET_E500_DOUBLE)
4978     {
4979       /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
4980          subreg:TI and reg:TF.  Decimal float modes are like integer
4981          modes (only low part of each register used) for this
4982          purpose.  */
4983       if (GET_CODE (op) == SUBREG
4984           && (mode == SImode || mode == DImode || mode == TImode
4985               || mode == DDmode || mode == TDmode)
4986           && REG_P (SUBREG_REG (op))
4987           && (GET_MODE (SUBREG_REG (op)) == DFmode
4988               || GET_MODE (SUBREG_REG (op)) == TFmode))
4989         return true;
4990
4991       /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
4992          reg:TI.  */
4993       if (GET_CODE (op) == SUBREG
4994           && (mode == DFmode || mode == TFmode)
4995           && REG_P (SUBREG_REG (op))
4996           && (GET_MODE (SUBREG_REG (op)) == DImode
4997               || GET_MODE (SUBREG_REG (op)) == TImode
4998               || GET_MODE (SUBREG_REG (op)) == DDmode
4999               || GET_MODE (SUBREG_REG (op)) == TDmode))
5000         return true;
5001     }
5002
5003   if (TARGET_SPE
5004       && GET_CODE (op) == SUBREG
5005       && mode == SImode
5006       && REG_P (SUBREG_REG (op))
5007       && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5008     return true;
5009
5010   return false;
5011 }
5012
5013 /* AIX increases natural record alignment to doubleword if the first
5014    field is an FP double while the FP fields remain word aligned.  */
5015
5016 unsigned int
5017 rs6000_special_round_type_align (tree type, unsigned int computed,
5018                                  unsigned int specified)
5019 {
5020   unsigned int align = MAX (computed, specified);
5021   tree field = TYPE_FIELDS (type);
5022
5023   /* Skip all non field decls */
5024   while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5025     field = DECL_CHAIN (field);
5026
5027   if (field != NULL && field != type)
5028     {
5029       type = TREE_TYPE (field);
5030       while (TREE_CODE (type) == ARRAY_TYPE)
5031         type = TREE_TYPE (type);
5032
5033       if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5034         align = MAX (align, 64);
5035     }
5036
5037   return align;
5038 }
5039
5040 /* Darwin increases record alignment to the natural alignment of
5041    the first field.  */
5042
5043 unsigned int
5044 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5045                                         unsigned int specified)
5046 {
5047   unsigned int align = MAX (computed, specified);
5048
5049   if (TYPE_PACKED (type))
5050     return align;
5051
5052   /* Find the first field, looking down into aggregates.  */
5053   do {
5054     tree field = TYPE_FIELDS (type);
5055     /* Skip all non field decls */
5056     while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5057       field = DECL_CHAIN (field);
5058     if (! field)
5059       break;
5060     /* A packed field does not contribute any extra alignment.  */
5061     if (DECL_PACKED (field))
5062       return align;
5063     type = TREE_TYPE (field);
5064     while (TREE_CODE (type) == ARRAY_TYPE)
5065       type = TREE_TYPE (type);
5066   } while (AGGREGATE_TYPE_P (type));
5067
5068   if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5069     align = MAX (align, TYPE_ALIGN (type));
5070
5071   return align;
5072 }
5073
5074 /* Return 1 for an operand in small memory on V.4/eabi.  */
5075
5076 int
5077 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5078                     enum machine_mode mode ATTRIBUTE_UNUSED)
5079 {
5080 #if TARGET_ELF
5081   rtx sym_ref;
5082
5083   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5084     return 0;
5085
5086   if (DEFAULT_ABI != ABI_V4)
5087     return 0;
5088
5089   /* Vector and float memory instructions have a limited offset on the
5090      SPE, so using a vector or float variable directly as an operand is
5091      not useful.  */
5092   if (TARGET_SPE
5093       && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5094     return 0;
5095
5096   if (GET_CODE (op) == SYMBOL_REF)
5097     sym_ref = op;
5098
5099   else if (GET_CODE (op) != CONST
5100            || GET_CODE (XEXP (op, 0)) != PLUS
5101            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5102            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5103     return 0;
5104
5105   else
5106     {
5107       rtx sum = XEXP (op, 0);
5108       HOST_WIDE_INT summand;
5109
5110       /* We have to be careful here, because it is the referenced address
5111          that must be 32k from _SDA_BASE_, not just the symbol.  */
5112       summand = INTVAL (XEXP (sum, 1));
5113       if (summand < 0 || summand > g_switch_value)
5114         return 0;
5115
5116       sym_ref = XEXP (sum, 0);
5117     }
5118
5119   return SYMBOL_REF_SMALL_P (sym_ref);
5120 #else
5121   return 0;
5122 #endif
5123 }
5124
5125 /* Return true if either operand is a general purpose register.  */
5126
5127 bool
5128 gpr_or_gpr_p (rtx op0, rtx op1)
5129 {
5130   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5131           || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5132 }
5133
5134 \f
5135 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p.  */
5136
5137 static bool
5138 reg_offset_addressing_ok_p (enum machine_mode mode)
5139 {
5140   switch (mode)
5141     {
5142     case V16QImode:
5143     case V8HImode:
5144     case V4SFmode:
5145     case V4SImode:
5146     case V2DFmode:
5147     case V2DImode:
5148       /* AltiVec/VSX vector modes.  Only reg+reg addressing is valid.  */
5149       if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5150         return false;
5151       break;
5152
5153     case V4HImode:
5154     case V2SImode:
5155     case V1DImode:
5156     case V2SFmode:
5157        /* Paired vector modes.  Only reg+reg addressing is valid.  */
5158       if (TARGET_PAIRED_FLOAT)
5159         return false;
5160       break;
5161
5162     default:
5163       break;
5164     }
5165
5166   return true;
5167 }
5168
5169 static bool
5170 virtual_stack_registers_memory_p (rtx op)
5171 {
5172   int regnum;
5173
5174   if (GET_CODE (op) == REG)
5175     regnum = REGNO (op);
5176
5177   else if (GET_CODE (op) == PLUS
5178            && GET_CODE (XEXP (op, 0)) == REG
5179            && GET_CODE (XEXP (op, 1)) == CONST_INT)
5180     regnum = REGNO (XEXP (op, 0));
5181
5182   else
5183     return false;
5184
5185   return (regnum >= FIRST_VIRTUAL_REGISTER
5186           && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
5187 }
5188
5189 /* Return true if memory accesses to OP are known to never straddle
5190    a 32k boundary.  */
5191
5192 static bool
5193 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
5194                              enum machine_mode mode)
5195 {
5196   tree decl, type;
5197   unsigned HOST_WIDE_INT dsize, dalign;
5198
5199   if (GET_CODE (op) != SYMBOL_REF)
5200     return false;
5201
5202   decl = SYMBOL_REF_DECL (op);
5203   if (!decl)
5204     {
5205       if (GET_MODE_SIZE (mode) == 0)
5206         return false;
5207
5208       /* -fsection-anchors loses the original SYMBOL_REF_DECL when
5209          replacing memory addresses with an anchor plus offset.  We
5210          could find the decl by rummaging around in the block->objects
5211          VEC for the given offset but that seems like too much work.  */
5212       dalign = 1;
5213       if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
5214           && SYMBOL_REF_ANCHOR_P (op)
5215           && SYMBOL_REF_BLOCK (op) != NULL)
5216         {
5217           struct object_block *block = SYMBOL_REF_BLOCK (op);
5218           HOST_WIDE_INT lsb, mask;
5219
5220           /* Given the alignment of the block..  */
5221           dalign = block->alignment;
5222           mask = dalign / BITS_PER_UNIT - 1;
5223
5224           /* ..and the combined offset of the anchor and any offset
5225              to this block object..  */
5226           offset += SYMBOL_REF_BLOCK_OFFSET (op);
5227           lsb = offset & -offset;
5228
5229           /* ..find how many bits of the alignment we know for the
5230              object.  */
5231           mask &= lsb - 1;
5232           dalign = mask + 1;
5233         }
5234       return dalign >= GET_MODE_SIZE (mode);
5235     }
5236
5237   if (DECL_P (decl))
5238     {
5239       if (TREE_CODE (decl) == FUNCTION_DECL)
5240         return true;
5241
5242       if (!DECL_SIZE_UNIT (decl))
5243         return false;
5244
5245       if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
5246         return false;
5247
5248       dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
5249       if (dsize > 32768)
5250         return false;
5251
5252       dalign = DECL_ALIGN_UNIT (decl);
5253       return dalign >= dsize;
5254     }
5255
5256   type = TREE_TYPE (decl);
5257
5258   if (TREE_CODE (decl) == STRING_CST)
5259     dsize = TREE_STRING_LENGTH (decl);
5260   else if (TYPE_SIZE_UNIT (type)
5261            && host_integerp (TYPE_SIZE_UNIT (type), 1))
5262     dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5263   else
5264     return false;
5265   if (dsize > 32768)
5266     return false;
5267
5268   dalign = TYPE_ALIGN (type);
5269   if (CONSTANT_CLASS_P (decl))
5270     dalign = CONSTANT_ALIGNMENT (decl, dalign);
5271   else
5272     dalign = DATA_ALIGNMENT (decl, dalign);
5273   dalign /= BITS_PER_UNIT;
5274   return dalign >= dsize;
5275 }
5276
5277 static bool
5278 constant_pool_expr_p (rtx op)
5279 {
5280   rtx base, offset;
5281
5282   split_const (op, &base, &offset);
5283   return (GET_CODE (base) == SYMBOL_REF
5284           && CONSTANT_POOL_ADDRESS_P (base)
5285           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5286 }
5287
5288 static rtx tocrel_base, tocrel_offset;
5289
5290 bool
5291 toc_relative_expr_p (rtx op)
5292 {
5293   if (GET_CODE (op) != CONST)
5294     return false;
5295
5296   split_const (op, &tocrel_base, &tocrel_offset);
5297   return (GET_CODE (tocrel_base) == UNSPEC
5298           && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5299 }
5300
5301 /* Return true if X is a constant pool address, and also for cmodel=medium
5302    if X is a toc-relative address known to be offsettable within MODE.  */
5303
5304 bool
5305 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
5306                                     bool strict)
5307 {
5308   return (TARGET_TOC
5309           && (GET_CODE (x) == PLUS || GET_CODE (x) == LO_SUM)
5310           && GET_CODE (XEXP (x, 0)) == REG
5311           && (REGNO (XEXP (x, 0)) == TOC_REGISTER
5312               || ((TARGET_MINIMAL_TOC
5313                    || TARGET_CMODEL != CMODEL_SMALL)
5314                   && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict)))
5315           && toc_relative_expr_p (XEXP (x, 1))
5316           && (TARGET_CMODEL != CMODEL_MEDIUM
5317               || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
5318               || mode == QImode
5319               || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
5320                                               INTVAL (tocrel_offset), mode)));
5321 }
5322
5323 static bool
5324 legitimate_small_data_p (enum machine_mode mode, rtx x)
5325 {
5326   return (DEFAULT_ABI == ABI_V4
5327           && !flag_pic && !TARGET_TOC
5328           && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5329           && small_data_operand (x, mode));
5330 }
5331
5332 /* SPE offset addressing is limited to 5-bits worth of double words.  */
5333 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5334
5335 bool
5336 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x, int strict)
5337 {
5338   unsigned HOST_WIDE_INT offset, extra;
5339
5340   if (GET_CODE (x) != PLUS)
5341     return false;
5342   if (GET_CODE (XEXP (x, 0)) != REG)
5343     return false;
5344   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5345     return false;
5346   if (!reg_offset_addressing_ok_p (mode))
5347     return virtual_stack_registers_memory_p (x);
5348   if (legitimate_constant_pool_address_p (x, mode, strict))
5349     return true;
5350   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5351     return false;
5352
5353   offset = INTVAL (XEXP (x, 1));
5354   extra = 0;
5355   switch (mode)
5356     {
5357     case V4HImode:
5358     case V2SImode:
5359     case V1DImode:
5360     case V2SFmode:
5361       /* SPE vector modes.  */
5362       return SPE_CONST_OFFSET_OK (offset);
5363
5364     case DFmode:
5365       if (TARGET_E500_DOUBLE)
5366         return SPE_CONST_OFFSET_OK (offset);
5367
5368       /* If we are using VSX scalar loads, restrict ourselves to reg+reg
5369          addressing.  */
5370       if (VECTOR_MEM_VSX_P (DFmode))
5371         return false;
5372
5373     case DDmode:
5374     case DImode:
5375       /* On e500v2, we may have:
5376
5377            (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
5378
5379          Which gets addressed with evldd instructions.  */
5380       if (TARGET_E500_DOUBLE)
5381         return SPE_CONST_OFFSET_OK (offset);
5382
5383       if (mode == DFmode || mode == DDmode || !TARGET_POWERPC64)
5384         extra = 4;
5385       else if (offset & 3)
5386         return false;
5387       break;
5388
5389     case TFmode:
5390       if (TARGET_E500_DOUBLE)
5391         return (SPE_CONST_OFFSET_OK (offset)
5392                 && SPE_CONST_OFFSET_OK (offset + 8));
5393
5394     case TDmode:
5395     case TImode:
5396       if (mode == TFmode || mode == TDmode || !TARGET_POWERPC64)
5397         extra = 12;
5398       else if (offset & 3)
5399         return false;
5400       else
5401         extra = 8;
5402       break;
5403
5404     default:
5405       break;
5406     }
5407
5408   offset += 0x8000;
5409   return offset < 0x10000 - extra;
5410 }
5411
5412 bool
5413 legitimate_indexed_address_p (rtx x, int strict)
5414 {
5415   rtx op0, op1;
5416
5417   if (GET_CODE (x) != PLUS)
5418     return false;
5419
5420   op0 = XEXP (x, 0);
5421   op1 = XEXP (x, 1);
5422
5423   /* Recognize the rtl generated by reload which we know will later be
5424      replaced with proper base and index regs.  */
5425   if (!strict
5426       && reload_in_progress
5427       && (REG_P (op0) || GET_CODE (op0) == PLUS)
5428       && REG_P (op1))
5429     return true;
5430
5431   return (REG_P (op0) && REG_P (op1)
5432           && ((INT_REG_OK_FOR_BASE_P (op0, strict)
5433                && INT_REG_OK_FOR_INDEX_P (op1, strict))
5434               || (INT_REG_OK_FOR_BASE_P (op1, strict)
5435                   && INT_REG_OK_FOR_INDEX_P (op0, strict))));
5436 }
5437
5438 bool
5439 avoiding_indexed_address_p (enum machine_mode mode)
5440 {
5441   /* Avoid indexed addressing for modes that have non-indexed
5442      load/store instruction forms.  */
5443   return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
5444 }
5445
5446 inline bool
5447 legitimate_indirect_address_p (rtx x, int strict)
5448 {
5449   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
5450 }
5451
5452 bool
5453 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
5454 {
5455   if (!TARGET_MACHO || !flag_pic
5456       || mode != SImode || GET_CODE (x) != MEM)
5457     return false;
5458   x = XEXP (x, 0);
5459
5460   if (GET_CODE (x) != LO_SUM)
5461     return false;
5462   if (GET_CODE (XEXP (x, 0)) != REG)
5463     return false;
5464   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
5465     return false;
5466   x = XEXP (x, 1);
5467
5468   return CONSTANT_P (x);
5469 }
5470
5471 static bool
5472 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
5473 {
5474   if (GET_CODE (x) != LO_SUM)
5475     return false;
5476   if (GET_CODE (XEXP (x, 0)) != REG)
5477     return false;
5478   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5479     return false;
5480   /* Restrict addressing for DI because of our SUBREG hackery.  */
5481   if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
5482                              || mode == DDmode || mode == TDmode
5483                              || mode == DImode))
5484     return false;
5485   x = XEXP (x, 1);
5486
5487   if (TARGET_ELF || TARGET_MACHO)
5488     {
5489       if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
5490         return false;
5491       if (TARGET_TOC)
5492         return false;
5493       if (GET_MODE_NUNITS (mode) != 1)
5494         return false;
5495       if (GET_MODE_BITSIZE (mode) > 64
5496           || (GET_MODE_BITSIZE (mode) > 32 && !TARGET_POWERPC64
5497               && !(TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
5498                    && (mode == DFmode || mode == DDmode))))
5499         return false;
5500
5501       return CONSTANT_P (x);
5502     }
5503
5504   return false;
5505 }
5506
5507
5508 /* Try machine-dependent ways of modifying an illegitimate address
5509    to be legitimate.  If we find one, return the new, valid address.
5510    This is used from only one place: `memory_address' in explow.c.
5511
5512    OLDX is the address as it was before break_out_memory_refs was
5513    called.  In some cases it is useful to look at this to decide what
5514    needs to be done.
5515
5516    It is always safe for this function to do nothing.  It exists to
5517    recognize opportunities to optimize the output.
5518
5519    On RS/6000, first check for the sum of a register with a constant
5520    integer that is out of range.  If so, generate code to add the
5521    constant with the low-order 16 bits masked to the register and force
5522    this result into another register (this can be done with `cau').
5523    Then generate an address of REG+(CONST&0xffff), allowing for the
5524    possibility of bit 16 being a one.
5525
5526    Then check for the sum of a register and something not constant, try to
5527    load the other things into a register and return the sum.  */
5528
5529 static rtx
5530 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5531                            enum machine_mode mode)
5532 {
5533   unsigned int extra = 0;
5534
5535   if (!reg_offset_addressing_ok_p (mode))
5536     {
5537       if (virtual_stack_registers_memory_p (x))
5538         return x;
5539
5540       /* In theory we should not be seeing addresses of the form reg+0,
5541          but just in case it is generated, optimize it away.  */
5542       if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
5543         return force_reg (Pmode, XEXP (x, 0));
5544
5545       /* Make sure both operands are registers.  */
5546       else if (GET_CODE (x) == PLUS)
5547         return gen_rtx_PLUS (Pmode,
5548                              force_reg (Pmode, XEXP (x, 0)),
5549                              force_reg (Pmode, XEXP (x, 1)));
5550       else
5551         return force_reg (Pmode, x);
5552     }
5553   if (GET_CODE (x) == SYMBOL_REF)
5554     {
5555       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
5556       if (model != 0)
5557         return rs6000_legitimize_tls_address (x, model);
5558     }
5559
5560   switch (mode)
5561     {
5562     case DFmode:
5563     case DDmode:
5564       extra = 4;
5565       break;
5566     case DImode:
5567       if (!TARGET_POWERPC64)
5568         extra = 4;
5569       break;
5570     case TFmode:
5571     case TDmode:
5572       extra = 12;
5573       break;
5574     case TImode:
5575       extra = TARGET_POWERPC64 ? 8 : 12;
5576       break;
5577     default:
5578       break;
5579     }
5580
5581   if (GET_CODE (x) == PLUS
5582       && GET_CODE (XEXP (x, 0)) == REG
5583       && GET_CODE (XEXP (x, 1)) == CONST_INT
5584       && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
5585           >= 0x10000 - extra)
5586       && !((TARGET_POWERPC64
5587             && (mode == DImode || mode == TImode)
5588             && (INTVAL (XEXP (x, 1)) & 3) != 0)
5589            || SPE_VECTOR_MODE (mode)
5590            || (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
5591                                       || mode == DImode || mode == DDmode
5592                                       || mode == TDmode))))
5593     {
5594       HOST_WIDE_INT high_int, low_int;
5595       rtx sum;
5596       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
5597       if (low_int >= 0x8000 - extra)
5598         low_int = 0;
5599       high_int = INTVAL (XEXP (x, 1)) - low_int;
5600       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
5601                                          GEN_INT (high_int)), 0);
5602       return plus_constant (sum, low_int);
5603     }
5604   else if (GET_CODE (x) == PLUS
5605            && GET_CODE (XEXP (x, 0)) == REG
5606            && GET_CODE (XEXP (x, 1)) != CONST_INT
5607            && GET_MODE_NUNITS (mode) == 1
5608            && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5609                || TARGET_POWERPC64
5610                || ((mode != DImode && mode != DFmode && mode != DDmode)
5611                    || (TARGET_E500_DOUBLE && mode != DDmode)))
5612            && (TARGET_POWERPC64 || mode != DImode)
5613            && !avoiding_indexed_address_p (mode)
5614            && mode != TImode
5615            && mode != TFmode
5616            && mode != TDmode)
5617     {
5618       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
5619                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
5620     }
5621   else if (SPE_VECTOR_MODE (mode)
5622            || (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
5623                                       || mode == DDmode || mode == TDmode
5624                                       || mode == DImode)))
5625     {
5626       if (mode == DImode)
5627         return x;
5628       /* We accept [reg + reg] and [reg + OFFSET].  */
5629
5630       if (GET_CODE (x) == PLUS)
5631        {
5632          rtx op1 = XEXP (x, 0);
5633          rtx op2 = XEXP (x, 1);
5634          rtx y;
5635
5636          op1 = force_reg (Pmode, op1);
5637
5638          if (GET_CODE (op2) != REG
5639              && (GET_CODE (op2) != CONST_INT
5640                  || !SPE_CONST_OFFSET_OK (INTVAL (op2))
5641                  || (GET_MODE_SIZE (mode) > 8
5642                      && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
5643            op2 = force_reg (Pmode, op2);
5644
5645          /* We can't always do [reg + reg] for these, because [reg +
5646             reg + offset] is not a legitimate addressing mode.  */
5647          y = gen_rtx_PLUS (Pmode, op1, op2);
5648
5649          if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
5650            return force_reg (Pmode, y);
5651          else
5652            return y;
5653        }
5654
5655       return force_reg (Pmode, x);
5656     }
5657   else if (TARGET_ELF
5658            && TARGET_32BIT
5659            && TARGET_NO_TOC
5660            && ! flag_pic
5661            && GET_CODE (x) != CONST_INT
5662            && GET_CODE (x) != CONST_DOUBLE
5663            && CONSTANT_P (x)
5664            && GET_MODE_NUNITS (mode) == 1
5665            && (GET_MODE_BITSIZE (mode) <= 32
5666                || ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5667                    && (mode == DFmode || mode == DDmode))))
5668     {
5669       rtx reg = gen_reg_rtx (Pmode);
5670       emit_insn (gen_elf_high (reg, x));
5671       return gen_rtx_LO_SUM (Pmode, reg, x);
5672     }
5673   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
5674            && ! flag_pic
5675 #if TARGET_MACHO
5676            && ! MACHO_DYNAMIC_NO_PIC_P
5677 #endif
5678            && GET_CODE (x) != CONST_INT
5679            && GET_CODE (x) != CONST_DOUBLE
5680            && CONSTANT_P (x)
5681            && GET_MODE_NUNITS (mode) == 1
5682            && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5683                || (mode != DFmode && mode != DDmode))
5684            && mode != DImode
5685            && mode != TImode)
5686     {
5687       rtx reg = gen_reg_rtx (Pmode);
5688       emit_insn (gen_macho_high (reg, x));
5689       return gen_rtx_LO_SUM (Pmode, reg, x);
5690     }
5691   else if (TARGET_TOC
5692            && GET_CODE (x) == SYMBOL_REF
5693            && constant_pool_expr_p (x)
5694            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
5695     {
5696       rtx reg = TARGET_CMODEL != CMODEL_SMALL ? gen_reg_rtx (Pmode) : NULL_RTX;
5697       return create_TOC_reference (x, reg);
5698     }
5699   else
5700     return x;
5701 }
5702
5703 /* Debug version of rs6000_legitimize_address.  */
5704 static rtx
5705 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
5706 {
5707   rtx ret;
5708   rtx insns;
5709
5710   start_sequence ();
5711   ret = rs6000_legitimize_address (x, oldx, mode);
5712   insns = get_insns ();
5713   end_sequence ();
5714
5715   if (ret != x)
5716     {
5717       fprintf (stderr,
5718                "\nrs6000_legitimize_address: mode %s, old code %s, "
5719                "new code %s, modified\n",
5720                GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
5721                GET_RTX_NAME (GET_CODE (ret)));
5722
5723       fprintf (stderr, "Original address:\n");
5724       debug_rtx (x);
5725
5726       fprintf (stderr, "oldx:\n");
5727       debug_rtx (oldx);
5728
5729       fprintf (stderr, "New address:\n");
5730       debug_rtx (ret);
5731
5732       if (insns)
5733         {
5734           fprintf (stderr, "Insns added:\n");
5735           debug_rtx_list (insns, 20);
5736         }
5737     }
5738   else
5739     {
5740       fprintf (stderr,
5741                "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
5742                GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
5743
5744       debug_rtx (x);
5745     }
5746
5747   if (insns)
5748     emit_insn (insns);
5749
5750   return ret;
5751 }
5752
5753 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5754    We need to emit DTP-relative relocations.  */
5755
5756 static void
5757 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
5758 {
5759   switch (size)
5760     {
5761     case 4:
5762       fputs ("\t.long\t", file);
5763       break;
5764     case 8:
5765       fputs (DOUBLE_INT_ASM_OP, file);
5766       break;
5767     default:
5768       gcc_unreachable ();
5769     }
5770   output_addr_const (file, x);
5771   fputs ("@dtprel+0x8000", file);
5772 }
5773
5774 /* In the name of slightly smaller debug output, and to cater to
5775    general assembler lossage, recognize various UNSPEC sequences
5776    and turn them back into a direct symbol reference.  */
5777
5778 static rtx
5779 rs6000_delegitimize_address (rtx orig_x)
5780 {
5781   rtx x, y;
5782
5783   orig_x = delegitimize_mem_from_attrs (orig_x);
5784   x = orig_x;
5785   if (MEM_P (x))
5786     x = XEXP (x, 0);
5787
5788   if (GET_CODE (x) == (TARGET_CMODEL != CMODEL_SMALL ? LO_SUM : PLUS)
5789       && GET_CODE (XEXP (x, 1)) == CONST)
5790     {
5791       rtx offset = NULL_RTX;
5792
5793       y = XEXP (XEXP (x, 1), 0);
5794       if (GET_CODE (y) == PLUS
5795           && GET_MODE (y) == Pmode
5796           && CONST_INT_P (XEXP (y, 1)))
5797         {
5798           offset = XEXP (y, 1);
5799           y = XEXP (y, 0);
5800         }
5801       if (GET_CODE (y) == UNSPEC
5802           && XINT (y, 1) == UNSPEC_TOCREL
5803           && ((GET_CODE (XEXP (x, 0)) == REG
5804                && (REGNO (XEXP (x, 0)) == TOC_REGISTER
5805                    || TARGET_MINIMAL_TOC
5806                    || TARGET_CMODEL != CMODEL_SMALL))
5807               || (TARGET_CMODEL != CMODEL_SMALL
5808                   && GET_CODE (XEXP (x, 0)) == CONST
5809                   && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5810                   && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == REG
5811                   && REGNO (XEXP (XEXP (XEXP (x, 0), 0), 0)) == TOC_REGISTER
5812                   && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == HIGH
5813                   && rtx_equal_p (XEXP (x, 1),
5814                                   XEXP (XEXP (XEXP (XEXP (x, 0), 0), 1), 0)))))
5815         {
5816           y = XVECEXP (y, 0, 0);
5817           if (offset != NULL_RTX)
5818             y = gen_rtx_PLUS (Pmode, y, offset);
5819           if (!MEM_P (orig_x))
5820             return y;
5821           else
5822             return replace_equiv_address_nv (orig_x, y);
5823         }
5824     }
5825
5826   if (TARGET_MACHO
5827       && GET_CODE (orig_x) == LO_SUM
5828       && GET_CODE (XEXP (orig_x, 1)) == CONST)
5829     {
5830       y = XEXP (XEXP (orig_x, 1), 0);
5831       if (GET_CODE (y) == UNSPEC
5832           && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
5833         return XVECEXP (y, 0, 0);
5834     }
5835
5836   return orig_x;
5837 }
5838
5839 /* Return true if X shouldn't be emitted into the debug info.
5840    The linker doesn't like .toc section references from
5841    .debug_* sections, so reject .toc section symbols.  */
5842
5843 static bool
5844 rs6000_const_not_ok_for_debug_p (rtx x)
5845 {
5846   if (GET_CODE (x) == SYMBOL_REF
5847       && CONSTANT_POOL_ADDRESS_P (x))
5848     {
5849       rtx c = get_pool_constant (x);
5850       enum machine_mode cmode = get_pool_mode (x);
5851       if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
5852         return true;
5853     }
5854
5855   return false;
5856 }
5857
5858 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
5859
5860 static GTY(()) rtx rs6000_tls_symbol;
5861 static rtx
5862 rs6000_tls_get_addr (void)
5863 {
5864   if (!rs6000_tls_symbol)
5865     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
5866
5867   return rs6000_tls_symbol;
5868 }
5869
5870 /* Construct the SYMBOL_REF for TLS GOT references.  */
5871
5872 static GTY(()) rtx rs6000_got_symbol;
5873 static rtx
5874 rs6000_got_sym (void)
5875 {
5876   if (!rs6000_got_symbol)
5877     {
5878       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
5879       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
5880       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
5881     }
5882
5883   return rs6000_got_symbol;
5884 }
5885
5886 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
5887    this (thread-local) address.  */
5888
5889 static rtx
5890 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
5891 {
5892   rtx dest, insn;
5893
5894   dest = gen_reg_rtx (Pmode);
5895   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
5896     {
5897       rtx tlsreg;
5898
5899       if (TARGET_64BIT)
5900         {
5901           tlsreg = gen_rtx_REG (Pmode, 13);
5902           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
5903         }
5904       else
5905         {
5906           tlsreg = gen_rtx_REG (Pmode, 2);
5907           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
5908         }
5909       emit_insn (insn);
5910     }
5911   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
5912     {
5913       rtx tlsreg, tmp;
5914
5915       tmp = gen_reg_rtx (Pmode);
5916       if (TARGET_64BIT)
5917         {
5918           tlsreg = gen_rtx_REG (Pmode, 13);
5919           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
5920         }
5921       else
5922         {
5923           tlsreg = gen_rtx_REG (Pmode, 2);
5924           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
5925         }
5926       emit_insn (insn);
5927       if (TARGET_64BIT)
5928         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
5929       else
5930         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
5931       emit_insn (insn);
5932     }
5933   else
5934     {
5935       rtx r3, got, tga, tmp1, tmp2, call_insn;
5936
5937       /* We currently use relocations like @got@tlsgd for tls, which
5938          means the linker will handle allocation of tls entries, placing
5939          them in the .got section.  So use a pointer to the .got section,
5940          not one to secondary TOC sections used by 64-bit -mminimal-toc,
5941          or to secondary GOT sections used by 32-bit -fPIC.  */
5942       if (TARGET_64BIT)
5943         got = gen_rtx_REG (Pmode, 2);
5944       else
5945         {
5946           if (flag_pic == 1)
5947             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
5948           else
5949             {
5950               rtx gsym = rs6000_got_sym ();
5951               got = gen_reg_rtx (Pmode);
5952               if (flag_pic == 0)
5953                 rs6000_emit_move (got, gsym, Pmode);
5954               else
5955                 {
5956                   rtx mem, lab, last;
5957
5958                   tmp1 = gen_reg_rtx (Pmode);
5959                   tmp2 = gen_reg_rtx (Pmode);
5960                   mem = gen_const_mem (Pmode, tmp1);
5961                   lab = gen_label_rtx ();
5962                   emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
5963                   emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
5964                   if (TARGET_LINK_STACK)
5965                     emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
5966                   emit_move_insn (tmp2, mem);
5967                   last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
5968                   set_unique_reg_note (last, REG_EQUAL, gsym);
5969                 }
5970             }
5971         }
5972
5973       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
5974         {
5975           tga = rs6000_tls_get_addr ();
5976           emit_library_call_value (tga, dest, LCT_CONST, Pmode,
5977                                    1, const0_rtx, Pmode);
5978
5979           r3 = gen_rtx_REG (Pmode, 3);
5980           if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
5981             insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
5982           else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
5983             insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
5984           else if (DEFAULT_ABI == ABI_V4)
5985             insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
5986           else
5987             gcc_unreachable ();
5988           call_insn = last_call_insn ();
5989           PATTERN (call_insn) = insn;
5990           if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
5991             use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
5992                      pic_offset_table_rtx);
5993         }
5994       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
5995         {
5996           tga = rs6000_tls_get_addr ();
5997           tmp1 = gen_reg_rtx (Pmode);
5998           emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
5999                                    1, const0_rtx, Pmode);
6000
6001           r3 = gen_rtx_REG (Pmode, 3);
6002           if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6003             insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
6004           else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6005             insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
6006           else if (DEFAULT_ABI == ABI_V4)
6007             insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
6008           else
6009             gcc_unreachable ();
6010           call_insn = last_call_insn ();
6011           PATTERN (call_insn) = insn;
6012           if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6013             use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6014                      pic_offset_table_rtx);
6015
6016           if (rs6000_tls_size == 16)
6017             {
6018               if (TARGET_64BIT)
6019                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
6020               else
6021                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
6022             }
6023           else if (rs6000_tls_size == 32)
6024             {
6025               tmp2 = gen_reg_rtx (Pmode);
6026               if (TARGET_64BIT)
6027                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
6028               else
6029                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
6030               emit_insn (insn);
6031               if (TARGET_64BIT)
6032                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
6033               else
6034                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
6035             }
6036           else
6037             {
6038               tmp2 = gen_reg_rtx (Pmode);
6039               if (TARGET_64BIT)
6040                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
6041               else
6042                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
6043               emit_insn (insn);
6044               insn = gen_rtx_SET (Pmode, dest,
6045                                   gen_rtx_PLUS (Pmode, tmp2, tmp1));
6046             }
6047           emit_insn (insn);
6048         }
6049       else
6050         {
6051           /* IE, or 64-bit offset LE.  */
6052           tmp2 = gen_reg_rtx (Pmode);
6053           if (TARGET_64BIT)
6054             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
6055           else
6056             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
6057           emit_insn (insn);
6058           if (TARGET_64BIT)
6059             insn = gen_tls_tls_64 (dest, tmp2, addr);
6060           else
6061             insn = gen_tls_tls_32 (dest, tmp2, addr);
6062           emit_insn (insn);
6063         }
6064     }
6065
6066   return dest;
6067 }
6068
6069 /* Return 1 if X contains a thread-local symbol.  */
6070
6071 static bool
6072 rs6000_tls_referenced_p (rtx x)
6073 {
6074   if (! TARGET_HAVE_TLS)
6075     return false;
6076
6077   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
6078 }
6079
6080 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
6081
6082 static bool
6083 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6084 {
6085   if (GET_CODE (x) == CONST
6086       && GET_CODE (XEXP (x, 0)) == PLUS
6087       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH)
6088     return true;
6089
6090   return rs6000_tls_referenced_p (x);
6091 }
6092
6093 /* Return 1 if *X is a thread-local symbol.  This is the same as
6094    rs6000_tls_symbol_ref except for the type of the unused argument.  */
6095
6096 static int
6097 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6098 {
6099   return RS6000_SYMBOL_REF_TLS_P (*x);
6100 }
6101
6102 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
6103    replace the input X, or the original X if no replacement is called for.
6104    The output parameter *WIN is 1 if the calling macro should goto WIN,
6105    0 if it should not.
6106
6107    For RS/6000, we wish to handle large displacements off a base
6108    register by splitting the addend across an addiu/addis and the mem insn.
6109    This cuts number of extra insns needed from 3 to 1.
6110
6111    On Darwin, we use this to generate code for floating point constants.
6112    A movsf_low is generated so we wind up with 2 instructions rather than 3.
6113    The Darwin code is inside #if TARGET_MACHO because only then are the
6114    machopic_* functions defined.  */
6115 static rtx
6116 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
6117                                   int opnum, int type,
6118                                   int ind_levels ATTRIBUTE_UNUSED, int *win)
6119 {
6120   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6121
6122   /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
6123      DFmode/DImode MEM.  */
6124   if (reg_offset_p
6125       && opnum == 1
6126       && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
6127           || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
6128     reg_offset_p = false;
6129
6130   /* We must recognize output that we have already generated ourselves.  */
6131   if (GET_CODE (x) == PLUS
6132       && GET_CODE (XEXP (x, 0)) == PLUS
6133       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6134       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6135       && GET_CODE (XEXP (x, 1)) == CONST_INT)
6136     {
6137       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6138                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6139                    opnum, (enum reload_type)type);
6140       *win = 1;
6141       return x;
6142     }
6143
6144   /* Likewise for (lo_sum (high ...) ...) output we have generated.  */
6145   if (GET_CODE (x) == LO_SUM
6146       && GET_CODE (XEXP (x, 0)) == HIGH)
6147     {
6148       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6149                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6150                    opnum, (enum reload_type)type);
6151       *win = 1;
6152       return x;
6153     }
6154
6155 #if TARGET_MACHO
6156   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
6157       && GET_CODE (x) == LO_SUM
6158       && GET_CODE (XEXP (x, 0)) == PLUS
6159       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
6160       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
6161       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
6162       && machopic_operand_p (XEXP (x, 1)))
6163     {
6164       /* Result of previous invocation of this function on Darwin
6165          floating point constant.  */
6166       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6167                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6168                    opnum, (enum reload_type)type);
6169       *win = 1;
6170       return x;
6171     }
6172 #endif
6173
6174   if (TARGET_CMODEL != CMODEL_SMALL
6175       && GET_CODE (x) == LO_SUM
6176       && GET_CODE (XEXP (x, 0)) == PLUS
6177       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6178       && REGNO (XEXP (XEXP (x, 0), 0)) == TOC_REGISTER
6179       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST
6180       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == HIGH
6181       && GET_CODE (XEXP (x, 1)) == CONST
6182       && GET_CODE (XEXP (XEXP (x, 1), 0)) == UNSPEC
6183       && XINT (XEXP (XEXP (x, 1), 0), 1) == UNSPEC_TOCREL
6184       && rtx_equal_p (XEXP (XEXP (XEXP (XEXP (x, 0), 1), 0), 0), XEXP (x, 1)))
6185     {
6186       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6187                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6188                    opnum, (enum reload_type) type);
6189       *win = 1;
6190       return x;
6191     }
6192
6193   /* Force ld/std non-word aligned offset into base register by wrapping
6194      in offset 0.  */
6195   if (GET_CODE (x) == PLUS
6196       && GET_CODE (XEXP (x, 0)) == REG
6197       && REGNO (XEXP (x, 0)) < 32
6198       && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6199       && GET_CODE (XEXP (x, 1)) == CONST_INT
6200       && reg_offset_p
6201       && (INTVAL (XEXP (x, 1)) & 3) != 0
6202       && VECTOR_MEM_NONE_P (mode)
6203       && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
6204       && TARGET_POWERPC64)
6205     {
6206       x = gen_rtx_PLUS (GET_MODE (x), x, GEN_INT (0));
6207       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6208                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6209                    opnum, (enum reload_type) type);
6210       *win = 1;
6211       return x;
6212     }
6213
6214   if (GET_CODE (x) == PLUS
6215       && GET_CODE (XEXP (x, 0)) == REG
6216       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
6217       && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6218       && GET_CODE (XEXP (x, 1)) == CONST_INT
6219       && reg_offset_p
6220       && !SPE_VECTOR_MODE (mode)
6221       && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
6222                                   || mode == DDmode || mode == TDmode
6223                                   || mode == DImode))
6224       && VECTOR_MEM_NONE_P (mode))
6225     {
6226       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6227       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6228       HOST_WIDE_INT high
6229         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6230
6231       /* Check for 32-bit overflow.  */
6232       if (high + low != val)
6233         {
6234           *win = 0;
6235           return x;
6236         }
6237
6238       /* Reload the high part into a base reg; leave the low part
6239          in the mem directly.  */
6240
6241       x = gen_rtx_PLUS (GET_MODE (x),
6242                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6243                                       GEN_INT (high)),
6244                         GEN_INT (low));
6245
6246       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6247                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6248                    opnum, (enum reload_type)type);
6249       *win = 1;
6250       return x;
6251     }
6252
6253   if (GET_CODE (x) == SYMBOL_REF
6254       && reg_offset_p
6255       && VECTOR_MEM_NONE_P (mode)
6256       && !SPE_VECTOR_MODE (mode)
6257 #if TARGET_MACHO
6258       && DEFAULT_ABI == ABI_DARWIN
6259       && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6260       && machopic_symbol_defined_p (x)
6261 #else
6262       && DEFAULT_ABI == ABI_V4
6263       && !flag_pic
6264 #endif
6265       /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6266          The same goes for DImode without 64-bit gprs and DFmode and DDmode
6267          without fprs.  */
6268       && mode != TFmode
6269       && mode != TDmode
6270       && (mode != DImode || TARGET_POWERPC64)
6271       && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
6272           || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
6273     {
6274 #if TARGET_MACHO
6275       if (flag_pic)
6276         {
6277           rtx offset = machopic_gen_offset (x);
6278           x = gen_rtx_LO_SUM (GET_MODE (x),
6279                 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
6280                   gen_rtx_HIGH (Pmode, offset)), offset);
6281         }
6282       else
6283 #endif
6284         x = gen_rtx_LO_SUM (GET_MODE (x),
6285               gen_rtx_HIGH (Pmode, x), x);
6286
6287       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6288                    BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6289                    opnum, (enum reload_type)type);
6290       *win = 1;
6291       return x;
6292     }
6293
6294   /* Reload an offset address wrapped by an AND that represents the
6295      masking of the lower bits.  Strip the outer AND and let reload
6296      convert the offset address into an indirect address.  For VSX,
6297      force reload to create the address with an AND in a separate
6298      register, because we can't guarantee an altivec register will
6299      be used.  */
6300   if (VECTOR_MEM_ALTIVEC_P (mode)
6301       && GET_CODE (x) == AND
6302       && GET_CODE (XEXP (x, 0)) == PLUS
6303       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6304       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6305       && GET_CODE (XEXP (x, 1)) == CONST_INT
6306       && INTVAL (XEXP (x, 1)) == -16)
6307     {
6308       x = XEXP (x, 0);
6309       *win = 1;
6310       return x;
6311     }
6312
6313   if (TARGET_TOC
6314       && reg_offset_p
6315       && GET_CODE (x) == SYMBOL_REF
6316       && constant_pool_expr_p (x)
6317       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
6318     {
6319       x = create_TOC_reference (x, NULL_RTX);
6320       if (TARGET_CMODEL != CMODEL_SMALL)
6321         push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6322                      BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6323                      opnum, (enum reload_type) type);
6324       *win = 1;
6325       return x;
6326     }
6327   *win = 0;
6328   return x;
6329 }
6330
6331 /* Debug version of rs6000_legitimize_reload_address.  */
6332 static rtx
6333 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
6334                                         int opnum, int type,
6335                                         int ind_levels, int *win)
6336 {
6337   rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
6338                                               ind_levels, win);
6339   fprintf (stderr,
6340            "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
6341            "type = %d, ind_levels = %d, win = %d, original addr:\n",
6342            GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
6343   debug_rtx (x);
6344
6345   if (x == ret)
6346     fprintf (stderr, "Same address returned\n");
6347   else if (!ret)
6348     fprintf (stderr, "NULL returned\n");
6349   else
6350     {
6351       fprintf (stderr, "New address:\n");
6352       debug_rtx (ret);
6353     }
6354
6355   return ret;
6356 }
6357
6358 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
6359    that is a valid memory address for an instruction.
6360    The MODE argument is the machine mode for the MEM expression
6361    that wants to use this address.
6362
6363    On the RS/6000, there are four valid address: a SYMBOL_REF that
6364    refers to a constant pool entry of an address (or the sum of it
6365    plus a constant), a short (16-bit signed) constant plus a register,
6366    the sum of two registers, or a register indirect, possibly with an
6367    auto-increment.  For DFmode, DDmode and DImode with a constant plus
6368    register, we must ensure that both words are addressable or PowerPC64
6369    with offset word aligned.
6370
6371    For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
6372    32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
6373    because adjacent memory cells are accessed by adding word-sized offsets
6374    during assembly output.  */
6375 bool
6376 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
6377 {
6378   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6379
6380   /* If this is an unaligned stvx/ldvx type address, discard the outer AND.  */
6381   if (VECTOR_MEM_ALTIVEC_P (mode)
6382       && GET_CODE (x) == AND
6383       && GET_CODE (XEXP (x, 1)) == CONST_INT
6384       && INTVAL (XEXP (x, 1)) == -16)
6385     x = XEXP (x, 0);
6386
6387   if (RS6000_SYMBOL_REF_TLS_P (x))
6388     return 0;
6389   if (legitimate_indirect_address_p (x, reg_ok_strict))
6390     return 1;
6391   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
6392       && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6393       && !SPE_VECTOR_MODE (mode)
6394       && mode != TFmode
6395       && mode != TDmode
6396       /* Restrict addressing for DI because of our SUBREG hackery.  */
6397       && !(TARGET_E500_DOUBLE
6398            && (mode == DFmode || mode == DDmode || mode == DImode))
6399       && TARGET_UPDATE
6400       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
6401     return 1;
6402   if (virtual_stack_registers_memory_p (x))
6403     return 1;
6404   if (reg_offset_p && legitimate_small_data_p (mode, x))
6405     return 1;
6406   if (reg_offset_p
6407       && legitimate_constant_pool_address_p (x, mode, reg_ok_strict))
6408     return 1;
6409   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
6410   if (! reg_ok_strict
6411       && reg_offset_p
6412       && GET_CODE (x) == PLUS
6413       && GET_CODE (XEXP (x, 0)) == REG
6414       && (XEXP (x, 0) == virtual_stack_vars_rtx
6415           || XEXP (x, 0) == arg_pointer_rtx)
6416       && GET_CODE (XEXP (x, 1)) == CONST_INT)
6417     return 1;
6418   if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict))
6419     return 1;
6420   if (mode != TImode
6421       && mode != TFmode
6422       && mode != TDmode
6423       && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6424           || TARGET_POWERPC64
6425           || (mode != DFmode && mode != DDmode)
6426           || (TARGET_E500_DOUBLE && mode != DDmode))
6427       && (TARGET_POWERPC64 || mode != DImode)
6428       && !avoiding_indexed_address_p (mode)
6429       && legitimate_indexed_address_p (x, reg_ok_strict))
6430     return 1;
6431   if (GET_CODE (x) == PRE_MODIFY
6432       && mode != TImode
6433       && mode != TFmode
6434       && mode != TDmode
6435       && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6436           || TARGET_POWERPC64
6437           || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
6438       && (TARGET_POWERPC64 || mode != DImode)
6439       && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6440       && !SPE_VECTOR_MODE (mode)
6441       /* Restrict addressing for DI because of our SUBREG hackery.  */
6442       && !(TARGET_E500_DOUBLE
6443            && (mode == DFmode || mode == DDmode || mode == DImode))
6444       && TARGET_UPDATE
6445       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
6446       && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1), reg_ok_strict)
6447           || (!avoiding_indexed_address_p (mode)
6448               && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
6449       && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6450     return 1;
6451   if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
6452     return 1;
6453   return 0;
6454 }
6455
6456 /* Debug version of rs6000_legitimate_address_p.  */
6457 static bool
6458 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
6459                                    bool reg_ok_strict)
6460 {
6461   bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
6462   fprintf (stderr,
6463            "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
6464            "strict = %d, code = %s\n",
6465            ret ? "true" : "false",
6466            GET_MODE_NAME (mode),
6467            reg_ok_strict,
6468            GET_RTX_NAME (GET_CODE (x)));
6469   debug_rtx (x);
6470
6471   return ret;
6472 }
6473
6474 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P.  */
6475
6476 static bool
6477 rs6000_mode_dependent_address_p (const_rtx addr)
6478 {
6479   return rs6000_mode_dependent_address_ptr (addr);
6480 }
6481
6482 /* Go to LABEL if ADDR (a legitimate address expression)
6483    has an effect that depends on the machine mode it is used for.
6484
6485    On the RS/6000 this is true of all integral offsets (since AltiVec
6486    and VSX modes don't allow them) or is a pre-increment or decrement.
6487
6488    ??? Except that due to conceptual problems in offsettable_address_p
6489    we can't really report the problems of integral offsets.  So leave
6490    this assuming that the adjustable offset must be valid for the
6491    sub-words of a TFmode operand, which is what we had before.  */
6492
6493 static bool
6494 rs6000_mode_dependent_address (const_rtx addr)
6495 {
6496   switch (GET_CODE (addr))
6497     {
6498     case PLUS:
6499       /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
6500          is considered a legitimate address before reload, so there
6501          are no offset restrictions in that case.  Note that this
6502          condition is safe in strict mode because any address involving
6503          virtual_stack_vars_rtx or arg_pointer_rtx would already have
6504          been rejected as illegitimate.  */
6505       if (XEXP (addr, 0) != virtual_stack_vars_rtx
6506           && XEXP (addr, 0) != arg_pointer_rtx
6507           && GET_CODE (XEXP (addr, 1)) == CONST_INT)
6508         {
6509           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
6510           return val + 12 + 0x8000 >= 0x10000;
6511         }
6512       break;
6513
6514     case LO_SUM:
6515       /* Anything in the constant pool is sufficiently aligned that
6516          all bytes have the same high part address.  */
6517       return !legitimate_constant_pool_address_p (addr, QImode, false);
6518
6519     /* Auto-increment cases are now treated generically in recog.c.  */
6520     case PRE_MODIFY:
6521       return TARGET_UPDATE;
6522
6523     /* AND is only allowed in Altivec loads.  */
6524     case AND:
6525       return true;
6526
6527     default:
6528       break;
6529     }
6530
6531   return false;
6532 }
6533
6534 /* Debug version of rs6000_mode_dependent_address.  */
6535 static bool
6536 rs6000_debug_mode_dependent_address (const_rtx addr)
6537 {
6538   bool ret = rs6000_mode_dependent_address (addr);
6539
6540   fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
6541            ret ? "true" : "false");
6542   debug_rtx (addr);
6543
6544   return ret;
6545 }
6546
6547 /* Implement FIND_BASE_TERM.  */
6548
6549 rtx
6550 rs6000_find_base_term (rtx op)
6551 {
6552   rtx base, offset;
6553
6554   split_const (op, &base, &offset);
6555   if (GET_CODE (base) == UNSPEC)
6556     switch (XINT (base, 1))
6557       {
6558       case UNSPEC_TOCREL:
6559       case UNSPEC_MACHOPIC_OFFSET:
6560         /* OP represents SYM [+ OFFSET] - ANCHOR.  SYM is the base term
6561            for aliasing purposes.  */
6562         return XVECEXP (base, 0, 0);
6563       }
6564
6565   return op;
6566 }
6567
6568 /* More elaborate version of recog's offsettable_memref_p predicate
6569    that works around the ??? note of rs6000_mode_dependent_address.
6570    In particular it accepts
6571
6572      (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
6573
6574    in 32-bit mode, that the recog predicate rejects.  */
6575
6576 bool
6577 rs6000_offsettable_memref_p (rtx op)
6578 {
6579   if (!MEM_P (op))
6580     return false;
6581
6582   /* First mimic offsettable_memref_p.  */
6583   if (offsettable_address_p (1, GET_MODE (op), XEXP (op, 0)))
6584     return true;
6585
6586   /* offsettable_address_p invokes rs6000_mode_dependent_address, but
6587      the latter predicate knows nothing about the mode of the memory
6588      reference and, therefore, assumes that it is the largest supported
6589      mode (TFmode).  As a consequence, legitimate offsettable memory
6590      references are rejected.  rs6000_legitimate_offset_address_p contains
6591      the correct logic for the PLUS case of rs6000_mode_dependent_address.  */
6592   return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0), 1);
6593 }
6594
6595 /* Change register usage conditional on target flags.  */
6596 static void
6597 rs6000_conditional_register_usage (void)
6598 {
6599   int i;
6600
6601   if (TARGET_DEBUG_TARGET)
6602     fprintf (stderr, "rs6000_conditional_register_usage called\n");
6603
6604   /* Set MQ register fixed (already call_used) if not POWER
6605      architecture (RIOS1, RIOS2, RSC, and PPC601) so that it will not
6606      be allocated.  */
6607   if (! TARGET_POWER)
6608     fixed_regs[64] = 1;
6609
6610   /* 64-bit AIX and Linux reserve GPR13 for thread-private data.  */
6611   if (TARGET_64BIT)
6612     fixed_regs[13] = call_used_regs[13]
6613       = call_really_used_regs[13] = 1;
6614
6615   /* Conditionally disable FPRs.  */
6616   if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
6617     for (i = 32; i < 64; i++)
6618       fixed_regs[i] = call_used_regs[i]
6619         = call_really_used_regs[i] = 1;
6620
6621   /* The TOC register is not killed across calls in a way that is
6622      visible to the compiler.  */
6623   if (DEFAULT_ABI == ABI_AIX)
6624     call_really_used_regs[2] = 0;
6625
6626   if (DEFAULT_ABI == ABI_V4
6627       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6628       && flag_pic == 2)
6629     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6630
6631   if (DEFAULT_ABI == ABI_V4
6632       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6633       && flag_pic == 1)
6634     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6635       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6636       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6637
6638   if (DEFAULT_ABI == ABI_DARWIN
6639       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
6640       fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6641       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6642       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6643
6644   if (TARGET_TOC && TARGET_MINIMAL_TOC)
6645     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6646       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6647
6648   if (TARGET_SPE)
6649     {
6650       global_regs[SPEFSCR_REGNO] = 1;
6651       /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
6652          registers in prologues and epilogues.  We no longer use r14
6653          for FIXED_SCRATCH, but we're keeping r14 out of the allocation
6654          pool for link-compatibility with older versions of GCC.  Once
6655          "old" code has died out, we can return r14 to the allocation
6656          pool.  */
6657       fixed_regs[14]
6658         = call_used_regs[14]
6659         = call_really_used_regs[14] = 1;
6660     }
6661
6662   if (!TARGET_ALTIVEC && !TARGET_VSX)
6663     {
6664       for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6665         fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6666       call_really_used_regs[VRSAVE_REGNO] = 1;
6667     }
6668
6669   if (TARGET_ALTIVEC || TARGET_VSX)
6670     global_regs[VSCR_REGNO] = 1;
6671
6672   if (TARGET_ALTIVEC_ABI)
6673     {
6674       for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
6675         call_used_regs[i] = call_really_used_regs[i] = 1;
6676
6677       /* AIX reserves VR20:31 in non-extended ABI mode.  */
6678       if (TARGET_XCOFF)
6679         for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
6680           fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6681     }
6682 }
6683 \f
6684 /* Try to output insns to set TARGET equal to the constant C if it can
6685    be done in less than N insns.  Do all computations in MODE.
6686    Returns the place where the output has been placed if it can be
6687    done and the insns have been emitted.  If it would take more than N
6688    insns, zero is returned and no insns and emitted.  */
6689
6690 rtx
6691 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
6692                        rtx source, int n ATTRIBUTE_UNUSED)
6693 {
6694   rtx result, insn, set;
6695   HOST_WIDE_INT c0, c1;
6696
6697   switch (mode)
6698     {
6699       case  QImode:
6700     case HImode:
6701       if (dest == NULL)
6702         dest = gen_reg_rtx (mode);
6703       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
6704       return dest;
6705
6706     case SImode:
6707       result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
6708
6709       emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
6710                               GEN_INT (INTVAL (source)
6711                                        & (~ (HOST_WIDE_INT) 0xffff))));
6712       emit_insn (gen_rtx_SET (VOIDmode, dest,
6713                               gen_rtx_IOR (SImode, copy_rtx (result),
6714                                            GEN_INT (INTVAL (source) & 0xffff))));
6715       result = dest;
6716       break;
6717
6718     case DImode:
6719       switch (GET_CODE (source))
6720         {
6721         case CONST_INT:
6722           c0 = INTVAL (source);
6723           c1 = -(c0 < 0);
6724           break;
6725
6726         case CONST_DOUBLE:
6727 #if HOST_BITS_PER_WIDE_INT >= 64
6728           c0 = CONST_DOUBLE_LOW (source);
6729           c1 = -(c0 < 0);
6730 #else
6731           c0 = CONST_DOUBLE_LOW (source);
6732           c1 = CONST_DOUBLE_HIGH (source);
6733 #endif
6734           break;
6735
6736         default:
6737           gcc_unreachable ();
6738         }
6739
6740       result = rs6000_emit_set_long_const (dest, c0, c1);
6741       break;
6742
6743     default:
6744       gcc_unreachable ();
6745     }
6746
6747   insn = get_last_insn ();
6748   set = single_set (insn);
6749   if (! CONSTANT_P (SET_SRC (set)))
6750     set_unique_reg_note (insn, REG_EQUAL, source);
6751
6752   return result;
6753 }
6754
6755 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
6756    fall back to a straight forward decomposition.  We do this to avoid
6757    exponential run times encountered when looking for longer sequences
6758    with rs6000_emit_set_const.  */
6759 static rtx
6760 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
6761 {
6762   if (!TARGET_POWERPC64)
6763     {
6764       rtx operand1, operand2;
6765
6766       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
6767                                         DImode);
6768       operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
6769                                         DImode);
6770       emit_move_insn (operand1, GEN_INT (c1));
6771       emit_move_insn (operand2, GEN_INT (c2));
6772     }
6773   else
6774     {
6775       HOST_WIDE_INT ud1, ud2, ud3, ud4;
6776
6777       ud1 = c1 & 0xffff;
6778       ud2 = (c1 & 0xffff0000) >> 16;
6779 #if HOST_BITS_PER_WIDE_INT >= 64
6780       c2 = c1 >> 32;
6781 #endif
6782       ud3 = c2 & 0xffff;
6783       ud4 = (c2 & 0xffff0000) >> 16;
6784
6785       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
6786           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
6787         {
6788           if (ud1 & 0x8000)
6789             emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) -  0x8000)));
6790           else
6791             emit_move_insn (dest, GEN_INT (ud1));
6792         }
6793
6794       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
6795                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
6796         {
6797           if (ud2 & 0x8000)
6798             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6799                                            - 0x80000000));
6800           else
6801             emit_move_insn (dest, GEN_INT (ud2 << 16));
6802           if (ud1 != 0)
6803             emit_move_insn (copy_rtx (dest),
6804                             gen_rtx_IOR (DImode, copy_rtx (dest),
6805                                          GEN_INT (ud1)));
6806         }
6807       else if (ud3 == 0 && ud4 == 0)
6808         {
6809           gcc_assert (ud2 & 0x8000);
6810           emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6811                                          - 0x80000000));
6812           if (ud1 != 0)
6813             emit_move_insn (copy_rtx (dest),
6814                             gen_rtx_IOR (DImode, copy_rtx (dest),
6815                                          GEN_INT (ud1)));
6816           emit_move_insn (copy_rtx (dest),
6817                           gen_rtx_ZERO_EXTEND (DImode,
6818                                                gen_lowpart (SImode,
6819                                                             copy_rtx (dest))));
6820         }
6821       else if ((ud4 == 0xffff && (ud3 & 0x8000))
6822                || (ud4 == 0 && ! (ud3 & 0x8000)))
6823         {
6824           if (ud3 & 0x8000)
6825             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
6826                                            - 0x80000000));
6827           else
6828             emit_move_insn (dest, GEN_INT (ud3 << 16));
6829
6830           if (ud2 != 0)
6831             emit_move_insn (copy_rtx (dest),
6832                             gen_rtx_IOR (DImode, copy_rtx (dest),
6833                                          GEN_INT (ud2)));
6834           emit_move_insn (copy_rtx (dest),
6835                           gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6836                                           GEN_INT (16)));
6837           if (ud1 != 0)
6838             emit_move_insn (copy_rtx (dest),
6839                             gen_rtx_IOR (DImode, copy_rtx (dest),
6840                                          GEN_INT (ud1)));
6841         }
6842       else
6843         {
6844           if (ud4 & 0x8000)
6845             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
6846                                            - 0x80000000));
6847           else
6848             emit_move_insn (dest, GEN_INT (ud4 << 16));
6849
6850           if (ud3 != 0)
6851             emit_move_insn (copy_rtx (dest),
6852                             gen_rtx_IOR (DImode, copy_rtx (dest),
6853                                          GEN_INT (ud3)));
6854
6855           emit_move_insn (copy_rtx (dest),
6856                           gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6857                                           GEN_INT (32)));
6858           if (ud2 != 0)
6859             emit_move_insn (copy_rtx (dest),
6860                             gen_rtx_IOR (DImode, copy_rtx (dest),
6861                                          GEN_INT (ud2 << 16)));
6862           if (ud1 != 0)
6863             emit_move_insn (copy_rtx (dest),
6864                             gen_rtx_IOR (DImode, copy_rtx (dest), GEN_INT (ud1)));
6865         }
6866     }
6867   return dest;
6868 }
6869
6870 /* Helper for the following.  Get rid of [r+r] memory refs
6871    in cases where it won't work (TImode, TFmode, TDmode).  */
6872
6873 static void
6874 rs6000_eliminate_indexed_memrefs (rtx operands[2])
6875 {
6876   if (reload_in_progress)
6877     return;
6878
6879   if (GET_CODE (operands[0]) == MEM
6880       && GET_CODE (XEXP (operands[0], 0)) != REG
6881       && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
6882                                                GET_MODE (operands[0]), false))
6883     operands[0]
6884       = replace_equiv_address (operands[0],
6885                                copy_addr_to_reg (XEXP (operands[0], 0)));
6886
6887   if (GET_CODE (operands[1]) == MEM
6888       && GET_CODE (XEXP (operands[1], 0)) != REG
6889       && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
6890                                                GET_MODE (operands[1]), false))
6891     operands[1]
6892       = replace_equiv_address (operands[1],
6893                                copy_addr_to_reg (XEXP (operands[1], 0)));
6894 }
6895
6896 /* Emit a move from SOURCE to DEST in mode MODE.  */
6897 void
6898 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
6899 {
6900   rtx operands[2];
6901   operands[0] = dest;
6902   operands[1] = source;
6903
6904   if (TARGET_DEBUG_ADDR)
6905     {
6906       fprintf (stderr,
6907                "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
6908                "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
6909                GET_MODE_NAME (mode),
6910                reload_in_progress,
6911                reload_completed,
6912                can_create_pseudo_p ());
6913       debug_rtx (dest);
6914       fprintf (stderr, "source:\n");
6915       debug_rtx (source);
6916     }
6917
6918   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
6919   if (GET_CODE (operands[1]) == CONST_DOUBLE
6920       && ! FLOAT_MODE_P (mode)
6921       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6922     {
6923       /* FIXME.  This should never happen.  */
6924       /* Since it seems that it does, do the safe thing and convert
6925          to a CONST_INT.  */
6926       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
6927     }
6928   gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
6929               || FLOAT_MODE_P (mode)
6930               || ((CONST_DOUBLE_HIGH (operands[1]) != 0
6931                    || CONST_DOUBLE_LOW (operands[1]) < 0)
6932                   && (CONST_DOUBLE_HIGH (operands[1]) != -1
6933                       || CONST_DOUBLE_LOW (operands[1]) >= 0)));
6934
6935   /* Check if GCC is setting up a block move that will end up using FP
6936      registers as temporaries.  We must make sure this is acceptable.  */
6937   if (GET_CODE (operands[0]) == MEM
6938       && GET_CODE (operands[1]) == MEM
6939       && mode == DImode
6940       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
6941           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
6942       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
6943                                             ? 32 : MEM_ALIGN (operands[0])))
6944             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
6945                                                ? 32
6946                                                : MEM_ALIGN (operands[1]))))
6947       && ! MEM_VOLATILE_P (operands [0])
6948       && ! MEM_VOLATILE_P (operands [1]))
6949     {
6950       emit_move_insn (adjust_address (operands[0], SImode, 0),
6951                       adjust_address (operands[1], SImode, 0));
6952       emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
6953                       adjust_address (copy_rtx (operands[1]), SImode, 4));
6954       return;
6955     }
6956
6957   if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
6958       && !gpc_reg_operand (operands[1], mode))
6959     operands[1] = force_reg (mode, operands[1]);
6960
6961   if (mode == SFmode && ! TARGET_POWERPC
6962       && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT 
6963       && GET_CODE (operands[0]) == MEM)
6964     {
6965       int regnum;
6966
6967       if (reload_in_progress || reload_completed)
6968         regnum = true_regnum (operands[1]);
6969       else if (GET_CODE (operands[1]) == REG)
6970         regnum = REGNO (operands[1]);
6971       else
6972         regnum = -1;
6973
6974       /* If operands[1] is a register, on POWER it may have
6975          double-precision data in it, so truncate it to single
6976          precision.  */
6977       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
6978         {
6979           rtx newreg;
6980           newreg = (!can_create_pseudo_p () ? copy_rtx (operands[1])
6981                     : gen_reg_rtx (mode));
6982           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
6983           operands[1] = newreg;
6984         }
6985     }
6986
6987   /* Recognize the case where operand[1] is a reference to thread-local
6988      data and load its address to a register.  */
6989   if (rs6000_tls_referenced_p (operands[1]))
6990     {
6991       enum tls_model model;
6992       rtx tmp = operands[1];
6993       rtx addend = NULL;
6994
6995       if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
6996         {
6997           addend = XEXP (XEXP (tmp, 0), 1);
6998           tmp = XEXP (XEXP (tmp, 0), 0);
6999         }
7000
7001       gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
7002       model = SYMBOL_REF_TLS_MODEL (tmp);
7003       gcc_assert (model != 0);
7004
7005       tmp = rs6000_legitimize_tls_address (tmp, model);
7006       if (addend)
7007         {
7008           tmp = gen_rtx_PLUS (mode, tmp, addend);
7009           tmp = force_operand (tmp, operands[0]);
7010         }
7011       operands[1] = tmp;
7012     }
7013
7014   /* Handle the case where reload calls us with an invalid address.  */
7015   if (reload_in_progress && mode == Pmode
7016       && (! general_operand (operands[1], mode)
7017           || ! nonimmediate_operand (operands[0], mode)))
7018     goto emit_set;
7019
7020   /* 128-bit constant floating-point values on Darwin should really be
7021      loaded as two parts.  */
7022   if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
7023       && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
7024     {
7025       rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
7026                         simplify_gen_subreg (DFmode, operands[1], mode, 0),
7027                         DFmode);
7028       rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
7029                                              GET_MODE_SIZE (DFmode)),
7030                         simplify_gen_subreg (DFmode, operands[1], mode,
7031                                              GET_MODE_SIZE (DFmode)),
7032                         DFmode);
7033       return;
7034     }
7035
7036   if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
7037     cfun->machine->sdmode_stack_slot =
7038       eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
7039
7040   if (reload_in_progress
7041       && mode == SDmode
7042       && MEM_P (operands[0])
7043       && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
7044       && REG_P (operands[1]))
7045     {
7046       if (FP_REGNO_P (REGNO (operands[1])))
7047         {
7048           rtx mem = adjust_address_nv (operands[0], DDmode, 0);
7049           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7050           emit_insn (gen_movsd_store (mem, operands[1]));
7051         }
7052       else if (INT_REGNO_P (REGNO (operands[1])))
7053         {
7054           rtx mem = adjust_address_nv (operands[0], mode, 4);
7055           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7056           emit_insn (gen_movsd_hardfloat (mem, operands[1]));
7057         }
7058       else
7059         gcc_unreachable();
7060       return;
7061     }
7062   if (reload_in_progress
7063       && mode == SDmode
7064       && REG_P (operands[0])
7065       && MEM_P (operands[1])
7066       && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
7067     {
7068       if (FP_REGNO_P (REGNO (operands[0])))
7069         {
7070           rtx mem = adjust_address_nv (operands[1], DDmode, 0);
7071           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7072           emit_insn (gen_movsd_load (operands[0], mem));
7073         }
7074       else if (INT_REGNO_P (REGNO (operands[0])))
7075         {
7076           rtx mem = adjust_address_nv (operands[1], mode, 4);
7077           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7078           emit_insn (gen_movsd_hardfloat (operands[0], mem));
7079         }
7080       else
7081         gcc_unreachable();
7082       return;
7083     }
7084
7085   /* FIXME:  In the long term, this switch statement should go away
7086      and be replaced by a sequence of tests based on things like
7087      mode == Pmode.  */
7088   switch (mode)
7089     {
7090     case HImode:
7091     case QImode:
7092       if (CONSTANT_P (operands[1])
7093           && GET_CODE (operands[1]) != CONST_INT)
7094         operands[1] = force_const_mem (mode, operands[1]);
7095       break;
7096
7097     case TFmode:
7098     case TDmode:
7099       rs6000_eliminate_indexed_memrefs (operands);
7100       /* fall through */
7101
7102     case DFmode:
7103     case DDmode:
7104     case SFmode:
7105     case SDmode:
7106       if (CONSTANT_P (operands[1])
7107           && ! easy_fp_constant (operands[1], mode))
7108         operands[1] = force_const_mem (mode, operands[1]);
7109       break;
7110
7111     case V16QImode:
7112     case V8HImode:
7113     case V4SFmode:
7114     case V4SImode:
7115     case V4HImode:
7116     case V2SFmode:
7117     case V2SImode:
7118     case V1DImode:
7119     case V2DFmode:
7120     case V2DImode:
7121       if (CONSTANT_P (operands[1])
7122           && !easy_vector_constant (operands[1], mode))
7123         operands[1] = force_const_mem (mode, operands[1]);
7124       break;
7125
7126     case SImode:
7127     case DImode:
7128       /* Use default pattern for address of ELF small data */
7129       if (TARGET_ELF
7130           && mode == Pmode
7131           && DEFAULT_ABI == ABI_V4
7132           && (GET_CODE (operands[1]) == SYMBOL_REF
7133               || GET_CODE (operands[1]) == CONST)
7134           && small_data_operand (operands[1], mode))
7135         {
7136           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7137           return;
7138         }
7139
7140       if (DEFAULT_ABI == ABI_V4
7141           && mode == Pmode && mode == SImode
7142           && flag_pic == 1 && got_operand (operands[1], mode))
7143         {
7144           emit_insn (gen_movsi_got (operands[0], operands[1]));
7145           return;
7146         }
7147
7148       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
7149           && TARGET_NO_TOC
7150           && ! flag_pic
7151           && mode == Pmode
7152           && CONSTANT_P (operands[1])
7153           && GET_CODE (operands[1]) != HIGH
7154           && GET_CODE (operands[1]) != CONST_INT)
7155         {
7156           rtx target = (!can_create_pseudo_p ()
7157                         ? operands[0]
7158                         : gen_reg_rtx (mode));
7159
7160           /* If this is a function address on -mcall-aixdesc,
7161              convert it to the address of the descriptor.  */
7162           if (DEFAULT_ABI == ABI_AIX
7163               && GET_CODE (operands[1]) == SYMBOL_REF
7164               && XSTR (operands[1], 0)[0] == '.')
7165             {
7166               const char *name = XSTR (operands[1], 0);
7167               rtx new_ref;
7168               while (*name == '.')
7169                 name++;
7170               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
7171               CONSTANT_POOL_ADDRESS_P (new_ref)
7172                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
7173               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
7174               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
7175               SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
7176               operands[1] = new_ref;
7177             }
7178
7179           if (DEFAULT_ABI == ABI_DARWIN)
7180             {
7181 #if TARGET_MACHO
7182               if (MACHO_DYNAMIC_NO_PIC_P)
7183                 {
7184                   /* Take care of any required data indirection.  */
7185                   operands[1] = rs6000_machopic_legitimize_pic_address (
7186                                   operands[1], mode, operands[0]);
7187                   if (operands[0] != operands[1])
7188                     emit_insn (gen_rtx_SET (VOIDmode,
7189                                             operands[0], operands[1]));
7190                   return;
7191                 }
7192 #endif
7193               emit_insn (gen_macho_high (target, operands[1]));
7194               emit_insn (gen_macho_low (operands[0], target, operands[1]));
7195               return;
7196             }
7197
7198           emit_insn (gen_elf_high (target, operands[1]));
7199           emit_insn (gen_elf_low (operands[0], target, operands[1]));
7200           return;
7201         }
7202
7203       /* If this is a SYMBOL_REF that refers to a constant pool entry,
7204          and we have put it in the TOC, we just need to make a TOC-relative
7205          reference to it.  */
7206       if ((TARGET_TOC
7207            && GET_CODE (operands[1]) == SYMBOL_REF
7208            && constant_pool_expr_p (operands[1])
7209            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
7210                                                get_pool_mode (operands[1])))
7211           || (TARGET_CMODEL == CMODEL_MEDIUM
7212               && GET_CODE (operands[1]) == SYMBOL_REF
7213               && !CONSTANT_POOL_ADDRESS_P (operands[1])
7214               && SYMBOL_REF_LOCAL_P (operands[1])))
7215         {
7216           rtx reg = NULL_RTX;
7217           if (TARGET_CMODEL != CMODEL_SMALL)
7218             {
7219               if (can_create_pseudo_p ())
7220                 reg = gen_reg_rtx (Pmode);
7221               else
7222                 reg = operands[0];
7223             }
7224           operands[1] = create_TOC_reference (operands[1], reg);
7225         }
7226       else if (mode == Pmode
7227                && CONSTANT_P (operands[1])
7228                && GET_CODE (operands[1]) != HIGH
7229                && !(TARGET_CMODEL != CMODEL_SMALL
7230                     && GET_CODE (operands[1]) == CONST
7231                     && GET_CODE (XEXP (operands[1], 0)) == PLUS
7232                     && GET_CODE (XEXP (XEXP (operands[1], 0), 1)) == HIGH)
7233                && ((GET_CODE (operands[1]) != CONST_INT
7234                     && ! easy_fp_constant (operands[1], mode))
7235                    || (GET_CODE (operands[1]) == CONST_INT
7236                        && (num_insns_constant (operands[1], mode)
7237                            > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7238                    || (GET_CODE (operands[0]) == REG
7239                        && FP_REGNO_P (REGNO (operands[0]))))
7240                && ! legitimate_constant_pool_address_p (operands[1], mode,
7241                                                         false)
7242                && ! toc_relative_expr_p (operands[1])
7243                && (TARGET_CMODEL == CMODEL_SMALL
7244                    || can_create_pseudo_p ()
7245                    || (REG_P (operands[0])
7246                        && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7247         {
7248
7249 #if TARGET_MACHO
7250           /* Darwin uses a special PIC legitimizer.  */
7251           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7252             {
7253               operands[1] =
7254                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7255                                                         operands[0]);
7256               if (operands[0] != operands[1])
7257                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7258               return;
7259             }
7260 #endif
7261
7262           /* If we are to limit the number of things we put in the TOC and
7263              this is a symbol plus a constant we can add in one insn,
7264              just put the symbol in the TOC and add the constant.  Don't do
7265              this if reload is in progress.  */
7266           if (GET_CODE (operands[1]) == CONST
7267               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7268               && GET_CODE (XEXP (operands[1], 0)) == PLUS
7269               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7270               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7271                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7272               && ! side_effects_p (operands[0]))
7273             {
7274               rtx sym =
7275                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7276               rtx other = XEXP (XEXP (operands[1], 0), 1);
7277
7278               sym = force_reg (mode, sym);
7279               emit_insn (gen_add3_insn (operands[0], sym, other));
7280               return;
7281             }
7282
7283           operands[1] = force_const_mem (mode, operands[1]);
7284
7285           if (TARGET_TOC
7286               && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7287               && constant_pool_expr_p (XEXP (operands[1], 0))
7288               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7289                         get_pool_constant (XEXP (operands[1], 0)),
7290                         get_pool_mode (XEXP (operands[1], 0))))
7291             {
7292               rtx tocref;
7293               rtx reg = NULL_RTX;
7294               if (TARGET_CMODEL != CMODEL_SMALL)
7295                 {
7296                   if (can_create_pseudo_p ())
7297                     reg = gen_reg_rtx (Pmode);
7298                   else
7299                     reg = operands[0];
7300                 }
7301               tocref = create_TOC_reference (XEXP (operands[1], 0), reg);
7302               operands[1] = gen_const_mem (mode, tocref);
7303               set_mem_alias_set (operands[1], get_TOC_alias_set ());
7304             }
7305         }
7306       break;
7307
7308     case TImode:
7309       rs6000_eliminate_indexed_memrefs (operands);
7310
7311       if (TARGET_POWER)
7312         {
7313           emit_insn (gen_rtx_PARALLEL (VOIDmode,
7314                        gen_rtvec (2,
7315                                   gen_rtx_SET (VOIDmode,
7316                                                operands[0], operands[1]),
7317                                   gen_rtx_CLOBBER (VOIDmode,
7318                                                    gen_rtx_SCRATCH (SImode)))));
7319           return;
7320         }
7321       break;
7322
7323     default:
7324       fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7325     }
7326
7327   /* Above, we may have called force_const_mem which may have returned
7328      an invalid address.  If we can, fix this up; otherwise, reload will
7329      have to deal with it.  */
7330   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7331     operands[1] = validize_mem (operands[1]);
7332
7333  emit_set:
7334   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7335 }
7336 \f
7337 /* Nonzero if we can use a floating-point register to pass this arg.  */
7338 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE)         \
7339   (SCALAR_FLOAT_MODE_P (MODE)                   \
7340    && (CUM)->fregno <= FP_ARG_MAX_REG           \
7341    && TARGET_HARD_FLOAT && TARGET_FPRS)
7342
7343 /* Nonzero if we can use an AltiVec register to pass this arg.  */
7344 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED)              \
7345   (ALTIVEC_OR_VSX_VECTOR_MODE (MODE)                            \
7346    && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG                      \
7347    && TARGET_ALTIVEC_ABI                                        \
7348    && (NAMED))
7349
7350 /* Return a nonzero value to say to return the function value in
7351    memory, just as large structures are always returned.  TYPE will be
7352    the data type of the value, and FNTYPE will be the type of the
7353    function doing the returning, or @code{NULL} for libcalls.
7354
7355    The AIX ABI for the RS/6000 specifies that all structures are
7356    returned in memory.  The Darwin ABI does the same.
7357    
7358    For the Darwin 64 Bit ABI, a function result can be returned in
7359    registers or in memory, depending on the size of the return data
7360    type.  If it is returned in registers, the value occupies the same
7361    registers as it would if it were the first and only function
7362    argument.  Otherwise, the function places its result in memory at
7363    the location pointed to by GPR3.
7364    
7365    The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4, 
7366    but a draft put them in memory, and GCC used to implement the draft
7367    instead of the final standard.  Therefore, aix_struct_return
7368    controls this instead of DEFAULT_ABI; V.4 targets needing backward
7369    compatibility can change DRAFT_V4_STRUCT_RET to override the
7370    default, and -m switches get the final word.  See
7371    rs6000_option_override_internal for more details.
7372
7373    The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
7374    long double support is enabled.  These values are returned in memory.
7375
7376    int_size_in_bytes returns -1 for variable size objects, which go in
7377    memory always.  The cast to unsigned makes -1 > 8.  */
7378
7379 static bool
7380 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7381 {
7382   /* For the Darwin64 ABI, test if we can fit the return value in regs.  */
7383   if (TARGET_MACHO
7384       && rs6000_darwin64_abi
7385       && TREE_CODE (type) == RECORD_TYPE
7386       && int_size_in_bytes (type) > 0)
7387     {
7388       CUMULATIVE_ARGS valcum;
7389       rtx valret;
7390
7391       valcum.words = 0;
7392       valcum.fregno = FP_ARG_MIN_REG;
7393       valcum.vregno = ALTIVEC_ARG_MIN_REG;
7394       /* Do a trial code generation as if this were going to be passed
7395          as an argument; if any part goes in memory, we return NULL.  */
7396       valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
7397       if (valret)
7398         return false;
7399       /* Otherwise fall through to more conventional ABI rules.  */
7400     }
7401
7402   if (AGGREGATE_TYPE_P (type)
7403       && (aix_struct_return
7404           || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
7405     return true;
7406
7407   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
7408      modes only exist for GCC vector types if -maltivec.  */
7409   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
7410       && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
7411     return false;
7412
7413   /* Return synthetic vectors in memory.  */
7414   if (TREE_CODE (type) == VECTOR_TYPE
7415       && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
7416     {
7417       static bool warned_for_return_big_vectors = false;
7418       if (!warned_for_return_big_vectors)
7419         {
7420           warning (0, "GCC vector returned by reference: "
7421                    "non-standard ABI extension with no compatibility guarantee");
7422           warned_for_return_big_vectors = true;
7423         }
7424       return true;
7425     }
7426
7427   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
7428     return true;
7429
7430   return false;
7431 }
7432
7433 #ifdef HAVE_AS_GNU_ATTRIBUTE
7434 /* Return TRUE if a call to function FNDECL may be one that
7435    potentially affects the function calling ABI of the object file.  */
7436
7437 static bool
7438 call_ABI_of_interest (tree fndecl)
7439 {
7440   if (cgraph_state == CGRAPH_STATE_EXPANSION)
7441     {
7442       struct cgraph_node *c_node;
7443
7444       /* Libcalls are always interesting.  */
7445       if (fndecl == NULL_TREE)
7446         return true;
7447
7448       /* Any call to an external function is interesting.  */
7449       if (DECL_EXTERNAL (fndecl))
7450         return true;
7451
7452       /* Interesting functions that we are emitting in this object file.  */
7453       c_node = cgraph_get_node (fndecl);
7454       c_node = cgraph_function_or_thunk_node (c_node, NULL);
7455       return !cgraph_only_called_directly_p (c_node);
7456     }
7457   return false;
7458 }
7459 #endif
7460
7461 /* Initialize a variable CUM of type CUMULATIVE_ARGS
7462    for a call to a function whose data type is FNTYPE.
7463    For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
7464
7465    For incoming args we set the number of arguments in the prototype large
7466    so we never return a PARALLEL.  */
7467
7468 void
7469 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
7470                       rtx libname ATTRIBUTE_UNUSED, int incoming,
7471                       int libcall, int n_named_args,
7472                       tree fndecl ATTRIBUTE_UNUSED,
7473                       enum machine_mode return_mode ATTRIBUTE_UNUSED)
7474 {
7475   static CUMULATIVE_ARGS zero_cumulative;
7476
7477   *cum = zero_cumulative;
7478   cum->words = 0;
7479   cum->fregno = FP_ARG_MIN_REG;
7480   cum->vregno = ALTIVEC_ARG_MIN_REG;
7481   cum->prototype = (fntype && prototype_p (fntype));
7482   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
7483                       ? CALL_LIBCALL : CALL_NORMAL);
7484   cum->sysv_gregno = GP_ARG_MIN_REG;
7485   cum->stdarg = stdarg_p (fntype);
7486
7487   cum->nargs_prototype = 0;
7488   if (incoming || cum->prototype)
7489     cum->nargs_prototype = n_named_args;
7490
7491   /* Check for a longcall attribute.  */
7492   if ((!fntype && rs6000_default_long_calls)
7493       || (fntype
7494           && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
7495           && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
7496     cum->call_cookie |= CALL_LONG;
7497
7498   if (TARGET_DEBUG_ARG)
7499     {
7500       fprintf (stderr, "\ninit_cumulative_args:");
7501       if (fntype)
7502         {
7503           tree ret_type = TREE_TYPE (fntype);
7504           fprintf (stderr, " ret code = %s,",
7505                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
7506         }
7507
7508       if (cum->call_cookie & CALL_LONG)
7509         fprintf (stderr, " longcall,");
7510
7511       fprintf (stderr, " proto = %d, nargs = %d\n",
7512                cum->prototype, cum->nargs_prototype);
7513     }
7514
7515 #ifdef HAVE_AS_GNU_ATTRIBUTE
7516   if (DEFAULT_ABI == ABI_V4)
7517     {
7518       cum->escapes = call_ABI_of_interest (fndecl);
7519       if (cum->escapes)
7520         {
7521           tree return_type;
7522
7523           if (fntype)
7524             {
7525               return_type = TREE_TYPE (fntype);
7526               return_mode = TYPE_MODE (return_type);
7527             }
7528           else
7529             return_type = lang_hooks.types.type_for_mode (return_mode, 0);
7530
7531           if (return_type != NULL)
7532             {
7533               if (TREE_CODE (return_type) == RECORD_TYPE
7534                   && TYPE_TRANSPARENT_AGGR (return_type))
7535                 {
7536                   return_type = TREE_TYPE (first_field (return_type));
7537                   return_mode = TYPE_MODE (return_type);
7538                 }
7539               if (AGGREGATE_TYPE_P (return_type)
7540                   && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
7541                       <= 8))
7542                 rs6000_returns_struct = true;
7543             }
7544           if (SCALAR_FLOAT_MODE_P (return_mode))
7545             rs6000_passes_float = true;
7546           else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
7547                    || SPE_VECTOR_MODE (return_mode))
7548             rs6000_passes_vector = true;
7549         }
7550     }
7551 #endif
7552
7553   if (fntype
7554       && !TARGET_ALTIVEC
7555       && TARGET_ALTIVEC_ABI
7556       && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
7557     {
7558       error ("cannot return value in vector register because"
7559              " altivec instructions are disabled, use -maltivec"
7560              " to enable them");
7561     }
7562 }
7563 \f
7564 /* Return true if TYPE must be passed on the stack and not in registers.  */
7565
7566 static bool
7567 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
7568 {
7569   if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
7570     return must_pass_in_stack_var_size (mode, type);
7571   else
7572     return must_pass_in_stack_var_size_or_pad (mode, type);
7573 }
7574
7575 /* If defined, a C expression which determines whether, and in which
7576    direction, to pad out an argument with extra space.  The value
7577    should be of type `enum direction': either `upward' to pad above
7578    the argument, `downward' to pad below, or `none' to inhibit
7579    padding.
7580
7581    For the AIX ABI structs are always stored left shifted in their
7582    argument slot.  */
7583
7584 enum direction
7585 function_arg_padding (enum machine_mode mode, const_tree type)
7586 {
7587 #ifndef AGGREGATE_PADDING_FIXED
7588 #define AGGREGATE_PADDING_FIXED 0
7589 #endif
7590 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
7591 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
7592 #endif
7593
7594   if (!AGGREGATE_PADDING_FIXED)
7595     {
7596       /* GCC used to pass structures of the same size as integer types as
7597          if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
7598          i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
7599          passed padded downward, except that -mstrict-align further
7600          muddied the water in that multi-component structures of 2 and 4
7601          bytes in size were passed padded upward.
7602
7603          The following arranges for best compatibility with previous
7604          versions of gcc, but removes the -mstrict-align dependency.  */
7605       if (BYTES_BIG_ENDIAN)
7606         {
7607           HOST_WIDE_INT size = 0;
7608
7609           if (mode == BLKmode)
7610             {
7611               if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7612                 size = int_size_in_bytes (type);
7613             }
7614           else
7615             size = GET_MODE_SIZE (mode);
7616
7617           if (size == 1 || size == 2 || size == 4)
7618             return downward;
7619         }
7620       return upward;
7621     }
7622
7623   if (AGGREGATES_PAD_UPWARD_ALWAYS)
7624     {
7625       if (type != 0 && AGGREGATE_TYPE_P (type))
7626         return upward;
7627     }
7628
7629   /* Fall back to the default.  */
7630   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
7631 }
7632
7633 /* If defined, a C expression that gives the alignment boundary, in bits,
7634    of an argument with the specified mode and type.  If it is not defined,
7635    PARM_BOUNDARY is used for all arguments.
7636
7637    V.4 wants long longs and doubles to be double word aligned.  Just
7638    testing the mode size is a boneheaded way to do this as it means
7639    that other types such as complex int are also double word aligned.
7640    However, we're stuck with this because changing the ABI might break
7641    existing library interfaces.
7642
7643    Doubleword align SPE vectors.
7644    Quadword align Altivec/VSX vectors.
7645    Quadword align large synthetic vector types.   */
7646
7647 static unsigned int
7648 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
7649 {
7650   if (DEFAULT_ABI == ABI_V4
7651       && (GET_MODE_SIZE (mode) == 8
7652           || (TARGET_HARD_FLOAT
7653               && TARGET_FPRS
7654               && (mode == TFmode || mode == TDmode))))
7655     return 64;
7656   else if (SPE_VECTOR_MODE (mode)
7657            || (type && TREE_CODE (type) == VECTOR_TYPE
7658                && int_size_in_bytes (type) >= 8
7659                && int_size_in_bytes (type) < 16))
7660     return 64;
7661   else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7662            || (type && TREE_CODE (type) == VECTOR_TYPE
7663                && int_size_in_bytes (type) >= 16))
7664     return 128;
7665   else if (TARGET_MACHO
7666            && rs6000_darwin64_abi
7667            && mode == BLKmode
7668            && type && TYPE_ALIGN (type) > 64)
7669     return 128;
7670   else
7671     return PARM_BOUNDARY;
7672 }
7673
7674 /* For a function parm of MODE and TYPE, return the starting word in
7675    the parameter area.  NWORDS of the parameter area are already used.  */
7676
7677 static unsigned int
7678 rs6000_parm_start (enum machine_mode mode, const_tree type,
7679                    unsigned int nwords)
7680 {
7681   unsigned int align;
7682   unsigned int parm_offset;
7683
7684   align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
7685   parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
7686   return nwords + (-(parm_offset + nwords) & align);
7687 }
7688
7689 /* Compute the size (in words) of a function argument.  */
7690
7691 static unsigned long
7692 rs6000_arg_size (enum machine_mode mode, const_tree type)
7693 {
7694   unsigned long size;
7695
7696   if (mode != BLKmode)
7697     size = GET_MODE_SIZE (mode);
7698   else
7699     size = int_size_in_bytes (type);
7700
7701   if (TARGET_32BIT)
7702     return (size + 3) >> 2;
7703   else
7704     return (size + 7) >> 3;
7705 }
7706 \f
7707 /* Use this to flush pending int fields.  */
7708
7709 static void
7710 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
7711                                           HOST_WIDE_INT bitpos, int final)
7712 {
7713   unsigned int startbit, endbit;
7714   int intregs, intoffset;
7715   enum machine_mode mode;
7716
7717   /* Handle the situations where a float is taking up the first half
7718      of the GPR, and the other half is empty (typically due to
7719      alignment restrictions). We can detect this by a 8-byte-aligned
7720      int field, or by seeing that this is the final flush for this
7721      argument. Count the word and continue on.  */
7722   if (cum->floats_in_gpr == 1
7723       && (cum->intoffset % 64 == 0
7724           || (cum->intoffset == -1 && final)))
7725     {
7726       cum->words++;
7727       cum->floats_in_gpr = 0;
7728     }
7729
7730   if (cum->intoffset == -1)
7731     return;
7732
7733   intoffset = cum->intoffset;
7734   cum->intoffset = -1;
7735   cum->floats_in_gpr = 0;
7736
7737   if (intoffset % BITS_PER_WORD != 0)
7738     {
7739       mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
7740                             MODE_INT, 0);
7741       if (mode == BLKmode)
7742         {
7743           /* We couldn't find an appropriate mode, which happens,
7744              e.g., in packed structs when there are 3 bytes to load.
7745              Back intoffset back to the beginning of the word in this
7746              case.  */
7747           intoffset = intoffset & -BITS_PER_WORD;
7748         }
7749     }
7750
7751   startbit = intoffset & -BITS_PER_WORD;
7752   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
7753   intregs = (endbit - startbit) / BITS_PER_WORD;
7754   cum->words += intregs;
7755   /* words should be unsigned. */
7756   if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
7757     {
7758       int pad = (endbit/BITS_PER_WORD) - cum->words;
7759       cum->words += pad;
7760     }
7761 }
7762
7763 /* The darwin64 ABI calls for us to recurse down through structs,
7764    looking for elements passed in registers.  Unfortunately, we have
7765    to track int register count here also because of misalignments
7766    in powerpc alignment mode.  */
7767
7768 static void
7769 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
7770                                             const_tree type,
7771                                             HOST_WIDE_INT startbitpos)
7772 {
7773   tree f;
7774
7775   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
7776     if (TREE_CODE (f) == FIELD_DECL)
7777       {
7778         HOST_WIDE_INT bitpos = startbitpos;
7779         tree ftype = TREE_TYPE (f);
7780         enum machine_mode mode;
7781         if (ftype == error_mark_node)
7782           continue;
7783         mode = TYPE_MODE (ftype);
7784
7785         if (DECL_SIZE (f) != 0
7786             && host_integerp (bit_position (f), 1))
7787           bitpos += int_bit_position (f);
7788
7789         /* ??? FIXME: else assume zero offset.  */
7790
7791         if (TREE_CODE (ftype) == RECORD_TYPE)
7792           rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
7793         else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
7794           {
7795             unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
7796             rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
7797             cum->fregno += n_fpregs;
7798             /* Single-precision floats present a special problem for
7799                us, because they are smaller than an 8-byte GPR, and so
7800                the structure-packing rules combined with the standard
7801                varargs behavior mean that we want to pack float/float
7802                and float/int combinations into a single register's
7803                space. This is complicated by the arg advance flushing,
7804                which works on arbitrarily large groups of int-type
7805                fields.  */
7806             if (mode == SFmode)
7807               {
7808                 if (cum->floats_in_gpr == 1)
7809                   {
7810                     /* Two floats in a word; count the word and reset
7811                        the float count.  */
7812                     cum->words++;
7813                     cum->floats_in_gpr = 0;
7814                   }
7815                 else if (bitpos % 64 == 0)
7816                   {
7817                     /* A float at the beginning of an 8-byte word;
7818                        count it and put off adjusting cum->words until
7819                        we see if a arg advance flush is going to do it
7820                        for us.  */
7821                     cum->floats_in_gpr++;
7822                   }
7823                 else
7824                   {
7825                     /* The float is at the end of a word, preceded
7826                        by integer fields, so the arg advance flush
7827                        just above has already set cum->words and
7828                        everything is taken care of.  */
7829                   }
7830               }
7831             else
7832               cum->words += n_fpregs;
7833           }
7834         else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
7835           {
7836             rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
7837             cum->vregno++;
7838             cum->words += 2;
7839           }
7840         else if (cum->intoffset == -1)
7841           cum->intoffset = bitpos;
7842       }
7843 }
7844
7845 /* Check for an item that needs to be considered specially under the darwin 64
7846    bit ABI.  These are record types where the mode is BLK or the structure is
7847    8 bytes in size.  */
7848 static int
7849 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
7850 {
7851   return rs6000_darwin64_abi
7852          && ((mode == BLKmode 
7853               && TREE_CODE (type) == RECORD_TYPE 
7854               && int_size_in_bytes (type) > 0)
7855           || (type && TREE_CODE (type) == RECORD_TYPE 
7856               && int_size_in_bytes (type) == 8)) ? 1 : 0;
7857 }
7858
7859 /* Update the data in CUM to advance over an argument
7860    of mode MODE and data type TYPE.
7861    (TYPE is null for libcalls where that information may not be available.)
7862
7863    Note that for args passed by reference, function_arg will be called
7864    with MODE and TYPE set to that of the pointer to the arg, not the arg
7865    itself.  */
7866
7867 static void
7868 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7869                                const_tree type, bool named, int depth)
7870 {
7871   /* Only tick off an argument if we're not recursing.  */
7872   if (depth == 0)
7873     cum->nargs_prototype--;
7874
7875 #ifdef HAVE_AS_GNU_ATTRIBUTE
7876   if (DEFAULT_ABI == ABI_V4
7877       && cum->escapes)
7878     {
7879       if (SCALAR_FLOAT_MODE_P (mode))
7880         rs6000_passes_float = true;
7881       else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
7882         rs6000_passes_vector = true;
7883       else if (SPE_VECTOR_MODE (mode)
7884                && !cum->stdarg
7885                && cum->sysv_gregno <= GP_ARG_MAX_REG)
7886         rs6000_passes_vector = true;
7887     }
7888 #endif
7889
7890   if (TARGET_ALTIVEC_ABI
7891       && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7892           || (type && TREE_CODE (type) == VECTOR_TYPE
7893               && int_size_in_bytes (type) == 16)))
7894     {
7895       bool stack = false;
7896
7897       if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
7898         {
7899           cum->vregno++;
7900           if (!TARGET_ALTIVEC)
7901             error ("cannot pass argument in vector register because"
7902                    " altivec instructions are disabled, use -maltivec"
7903                    " to enable them");
7904
7905           /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
7906              even if it is going to be passed in a vector register.
7907              Darwin does the same for variable-argument functions.  */
7908           if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
7909               || (cum->stdarg && DEFAULT_ABI != ABI_V4))
7910             stack = true;
7911         }
7912       else
7913         stack = true;
7914
7915       if (stack)
7916         {
7917           int align;
7918
7919           /* Vector parameters must be 16-byte aligned.  This places
7920              them at 2 mod 4 in terms of words in 32-bit mode, since
7921              the parameter save area starts at offset 24 from the
7922              stack.  In 64-bit mode, they just have to start on an
7923              even word, since the parameter save area is 16-byte
7924              aligned.  Space for GPRs is reserved even if the argument
7925              will be passed in memory.  */
7926           if (TARGET_32BIT)
7927             align = (2 - cum->words) & 3;
7928           else
7929             align = cum->words & 1;
7930           cum->words += align + rs6000_arg_size (mode, type);
7931
7932           if (TARGET_DEBUG_ARG)
7933             {
7934               fprintf (stderr, "function_adv: words = %2d, align=%d, ",
7935                        cum->words, align);
7936               fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
7937                        cum->nargs_prototype, cum->prototype,
7938                        GET_MODE_NAME (mode));
7939             }
7940         }
7941     }
7942   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
7943            && !cum->stdarg
7944            && cum->sysv_gregno <= GP_ARG_MAX_REG)
7945     cum->sysv_gregno++;
7946
7947   else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
7948     {
7949       int size = int_size_in_bytes (type);
7950       /* Variable sized types have size == -1 and are
7951          treated as if consisting entirely of ints.
7952          Pad to 16 byte boundary if needed.  */
7953       if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
7954           && (cum->words % 2) != 0)
7955         cum->words++;
7956       /* For varargs, we can just go up by the size of the struct. */
7957       if (!named)
7958         cum->words += (size + 7) / 8;
7959       else
7960         {
7961           /* It is tempting to say int register count just goes up by
7962              sizeof(type)/8, but this is wrong in a case such as
7963              { int; double; int; } [powerpc alignment].  We have to
7964              grovel through the fields for these too.  */
7965           cum->intoffset = 0;
7966           cum->floats_in_gpr = 0;
7967           rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
7968           rs6000_darwin64_record_arg_advance_flush (cum,
7969                                                     size * BITS_PER_UNIT, 1);
7970         }
7971           if (TARGET_DEBUG_ARG)
7972             {
7973               fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
7974                        cum->words, TYPE_ALIGN (type), size);
7975               fprintf (stderr, 
7976                    "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
7977                        cum->nargs_prototype, cum->prototype,
7978                        GET_MODE_NAME (mode));
7979             }
7980     }
7981   else if (DEFAULT_ABI == ABI_V4)
7982     {
7983       if (TARGET_HARD_FLOAT && TARGET_FPRS
7984           && ((TARGET_SINGLE_FLOAT && mode == SFmode)
7985               || (TARGET_DOUBLE_FLOAT && mode == DFmode)
7986               || (mode == TFmode && !TARGET_IEEEQUAD)
7987               || mode == SDmode || mode == DDmode || mode == TDmode))
7988         {
7989           /* _Decimal128 must use an even/odd register pair.  This assumes
7990              that the register number is odd when fregno is odd.  */
7991           if (mode == TDmode && (cum->fregno % 2) == 1)
7992             cum->fregno++;
7993
7994           if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
7995               <= FP_ARG_V4_MAX_REG)
7996             cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7997           else
7998             {
7999               cum->fregno = FP_ARG_V4_MAX_REG + 1;
8000               if (mode == DFmode || mode == TFmode
8001                   || mode == DDmode || mode == TDmode)
8002                 cum->words += cum->words & 1;
8003               cum->words += rs6000_arg_size (mode, type);
8004             }
8005         }
8006       else
8007         {
8008           int n_words = rs6000_arg_size (mode, type);
8009           int gregno = cum->sysv_gregno;
8010
8011           /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8012              (r7,r8) or (r9,r10).  As does any other 2 word item such
8013              as complex int due to a historical mistake.  */
8014           if (n_words == 2)
8015             gregno += (1 - gregno) & 1;
8016
8017           /* Multi-reg args are not split between registers and stack.  */
8018           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8019             {
8020               /* Long long and SPE vectors are aligned on the stack.
8021                  So are other 2 word items such as complex int due to
8022                  a historical mistake.  */
8023               if (n_words == 2)
8024                 cum->words += cum->words & 1;
8025               cum->words += n_words;
8026             }
8027
8028           /* Note: continuing to accumulate gregno past when we've started
8029              spilling to the stack indicates the fact that we've started
8030              spilling to the stack to expand_builtin_saveregs.  */
8031           cum->sysv_gregno = gregno + n_words;
8032         }
8033
8034       if (TARGET_DEBUG_ARG)
8035         {
8036           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8037                    cum->words, cum->fregno);
8038           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
8039                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
8040           fprintf (stderr, "mode = %4s, named = %d\n",
8041                    GET_MODE_NAME (mode), named);
8042         }
8043     }
8044   else
8045     {
8046       int n_words = rs6000_arg_size (mode, type);
8047       int start_words = cum->words;
8048       int align_words = rs6000_parm_start (mode, type, start_words);
8049
8050       cum->words = align_words + n_words;
8051
8052       if (SCALAR_FLOAT_MODE_P (mode)
8053           && TARGET_HARD_FLOAT && TARGET_FPRS)
8054         {
8055           /* _Decimal128 must be passed in an even/odd float register pair.
8056              This assumes that the register number is odd when fregno is
8057              odd.  */
8058           if (mode == TDmode && (cum->fregno % 2) == 1)
8059             cum->fregno++;
8060           cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8061         }
8062
8063       if (TARGET_DEBUG_ARG)
8064         {
8065           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8066                    cum->words, cum->fregno);
8067           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
8068                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
8069           fprintf (stderr, "named = %d, align = %d, depth = %d\n",
8070                    named, align_words - start_words, depth);
8071         }
8072     }
8073 }
8074
8075 static void
8076 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
8077                              const_tree type, bool named)
8078 {
8079   rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
8080                                  0);
8081 }
8082
8083 static rtx
8084 spe_build_register_parallel (enum machine_mode mode, int gregno)
8085 {
8086   rtx r1, r3, r5, r7;
8087
8088   switch (mode)
8089     {
8090     case DFmode:
8091       r1 = gen_rtx_REG (DImode, gregno);
8092       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8093       return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
8094
8095     case DCmode:
8096     case TFmode:
8097       r1 = gen_rtx_REG (DImode, gregno);
8098       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8099       r3 = gen_rtx_REG (DImode, gregno + 2);
8100       r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8101       return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
8102
8103     case TCmode:
8104       r1 = gen_rtx_REG (DImode, gregno);
8105       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8106       r3 = gen_rtx_REG (DImode, gregno + 2);
8107       r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8108       r5 = gen_rtx_REG (DImode, gregno + 4);
8109       r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
8110       r7 = gen_rtx_REG (DImode, gregno + 6);
8111       r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
8112       return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
8113
8114     default:
8115       gcc_unreachable ();
8116     }
8117 }
8118
8119 /* Determine where to put a SIMD argument on the SPE.  */
8120 static rtx
8121 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8122                          const_tree type)
8123 {
8124   int gregno = cum->sysv_gregno;
8125
8126   /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
8127      are passed and returned in a pair of GPRs for ABI compatibility.  */
8128   if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
8129                              || mode == DCmode || mode == TCmode))
8130     {
8131       int n_words = rs6000_arg_size (mode, type);
8132
8133       /* Doubles go in an odd/even register pair (r5/r6, etc).  */
8134       if (mode == DFmode)
8135         gregno += (1 - gregno) & 1;
8136
8137       /* Multi-reg args are not split between registers and stack.  */
8138       if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8139         return NULL_RTX;
8140
8141       return spe_build_register_parallel (mode, gregno);
8142     }
8143   if (cum->stdarg)
8144     {
8145       int n_words = rs6000_arg_size (mode, type);
8146
8147       /* SPE vectors are put in odd registers.  */
8148       if (n_words == 2 && (gregno & 1) == 0)
8149         gregno += 1;
8150
8151       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
8152         {
8153           rtx r1, r2;
8154           enum machine_mode m = SImode;
8155
8156           r1 = gen_rtx_REG (m, gregno);
8157           r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
8158           r2 = gen_rtx_REG (m, gregno + 1);
8159           r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
8160           return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
8161         }
8162       else
8163         return NULL_RTX;
8164     }
8165   else
8166     {
8167       if (gregno <= GP_ARG_MAX_REG)
8168         return gen_rtx_REG (mode, gregno);
8169       else
8170         return NULL_RTX;
8171     }
8172 }
8173
8174 /* A subroutine of rs6000_darwin64_record_arg.  Assign the bits of the
8175    structure between cum->intoffset and bitpos to integer registers.  */
8176
8177 static void
8178 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
8179                                   HOST_WIDE_INT bitpos, rtx rvec[], int *k)
8180 {
8181   enum machine_mode mode;
8182   unsigned int regno;
8183   unsigned int startbit, endbit;
8184   int this_regno, intregs, intoffset;
8185   rtx reg;
8186
8187   if (cum->intoffset == -1)
8188     return;
8189
8190   intoffset = cum->intoffset;
8191   cum->intoffset = -1;
8192
8193   /* If this is the trailing part of a word, try to only load that
8194      much into the register.  Otherwise load the whole register.  Note
8195      that in the latter case we may pick up unwanted bits.  It's not a
8196      problem at the moment but may wish to revisit.  */
8197
8198   if (intoffset % BITS_PER_WORD != 0)
8199     {
8200       mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8201                           MODE_INT, 0);
8202       if (mode == BLKmode)
8203         {
8204           /* We couldn't find an appropriate mode, which happens,
8205              e.g., in packed structs when there are 3 bytes to load.
8206              Back intoffset back to the beginning of the word in this
8207              case.  */
8208          intoffset = intoffset & -BITS_PER_WORD;
8209          mode = word_mode;
8210         }
8211     }
8212   else
8213     mode = word_mode;
8214
8215   startbit = intoffset & -BITS_PER_WORD;
8216   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8217   intregs = (endbit - startbit) / BITS_PER_WORD;
8218   this_regno = cum->words + intoffset / BITS_PER_WORD;
8219
8220   if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
8221     cum->use_stack = 1;
8222
8223   intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
8224   if (intregs <= 0)
8225     return;
8226
8227   intoffset /= BITS_PER_UNIT;
8228   do
8229     {
8230       regno = GP_ARG_MIN_REG + this_regno;
8231       reg = gen_rtx_REG (mode, regno);
8232       rvec[(*k)++] =
8233         gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
8234
8235       this_regno += 1;
8236       intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
8237       mode = word_mode;
8238       intregs -= 1;
8239     }
8240   while (intregs > 0);
8241 }
8242
8243 /* Recursive workhorse for the following.  */
8244
8245 static void
8246 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
8247                                     HOST_WIDE_INT startbitpos, rtx rvec[],
8248                                     int *k)
8249 {
8250   tree f;
8251
8252   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8253     if (TREE_CODE (f) == FIELD_DECL)
8254       {
8255         HOST_WIDE_INT bitpos = startbitpos;
8256         tree ftype = TREE_TYPE (f);
8257         enum machine_mode mode;
8258         if (ftype == error_mark_node)
8259           continue;
8260         mode = TYPE_MODE (ftype);
8261
8262         if (DECL_SIZE (f) != 0
8263             && host_integerp (bit_position (f), 1))
8264           bitpos += int_bit_position (f);
8265
8266         /* ??? FIXME: else assume zero offset.  */
8267
8268         if (TREE_CODE (ftype) == RECORD_TYPE)
8269           rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
8270         else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
8271           {
8272             unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8273 #if 0
8274             switch (mode)
8275               {
8276               case SCmode: mode = SFmode; break;
8277               case DCmode: mode = DFmode; break;
8278               case TCmode: mode = TFmode; break;
8279               default: break;
8280               }
8281 #endif
8282             rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8283             if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8284               {
8285                 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8286                             && (mode == TFmode || mode == TDmode));
8287                 /* Long double or _Decimal128 split over regs and memory.  */
8288                 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
8289                 cum->use_stack=1;
8290               }
8291             rvec[(*k)++]
8292               = gen_rtx_EXPR_LIST (VOIDmode,
8293                                    gen_rtx_REG (mode, cum->fregno++),
8294                                    GEN_INT (bitpos / BITS_PER_UNIT));
8295             if (mode == TFmode || mode == TDmode)
8296               cum->fregno++;
8297           }
8298         else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
8299           {
8300             rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8301             rvec[(*k)++]
8302               = gen_rtx_EXPR_LIST (VOIDmode,
8303                                    gen_rtx_REG (mode, cum->vregno++),
8304                                    GEN_INT (bitpos / BITS_PER_UNIT));
8305           }
8306         else if (cum->intoffset == -1)
8307           cum->intoffset = bitpos;
8308       }
8309 }
8310
8311 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
8312    the register(s) to be used for each field and subfield of a struct
8313    being passed by value, along with the offset of where the
8314    register's value may be found in the block.  FP fields go in FP
8315    register, vector fields go in vector registers, and everything
8316    else goes in int registers, packed as in memory.
8317
8318    This code is also used for function return values.  RETVAL indicates
8319    whether this is the case.
8320
8321    Much of this is taken from the SPARC V9 port, which has a similar
8322    calling convention.  */
8323
8324 static rtx
8325 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
8326                             bool named, bool retval)
8327 {
8328   rtx rvec[FIRST_PSEUDO_REGISTER];
8329   int k = 1, kbase = 1;
8330   HOST_WIDE_INT typesize = int_size_in_bytes (type);
8331   /* This is a copy; modifications are not visible to our caller.  */
8332   CUMULATIVE_ARGS copy_cum = *orig_cum;
8333   CUMULATIVE_ARGS *cum = &copy_cum;
8334
8335   /* Pad to 16 byte boundary if needed.  */
8336   if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8337       && (cum->words % 2) != 0)
8338     cum->words++;
8339
8340   cum->intoffset = 0;
8341   cum->use_stack = 0;
8342   cum->named = named;
8343
8344   /* Put entries into rvec[] for individual FP and vector fields, and
8345      for the chunks of memory that go in int regs.  Note we start at
8346      element 1; 0 is reserved for an indication of using memory, and
8347      may or may not be filled in below. */
8348   rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
8349   rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
8350
8351   /* If any part of the struct went on the stack put all of it there.
8352      This hack is because the generic code for
8353      FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
8354      parts of the struct are not at the beginning.  */
8355   if (cum->use_stack)
8356     {
8357       if (retval)
8358         return NULL_RTX;    /* doesn't go in registers at all */
8359       kbase = 0;
8360       rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8361     }
8362   if (k > 1 || cum->use_stack)
8363     return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
8364   else
8365     return NULL_RTX;
8366 }
8367
8368 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
8369
8370 static rtx
8371 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
8372                            int align_words)
8373 {
8374   int n_units;
8375   int i, k;
8376   rtx rvec[GP_ARG_NUM_REG + 1];
8377
8378   if (align_words >= GP_ARG_NUM_REG)
8379     return NULL_RTX;
8380
8381   n_units = rs6000_arg_size (mode, type);
8382
8383   /* Optimize the simple case where the arg fits in one gpr, except in
8384      the case of BLKmode due to assign_parms assuming that registers are
8385      BITS_PER_WORD wide.  */
8386   if (n_units == 0
8387       || (n_units == 1 && mode != BLKmode))
8388     return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8389
8390   k = 0;
8391   if (align_words + n_units > GP_ARG_NUM_REG)
8392     /* Not all of the arg fits in gprs.  Say that it goes in memory too,
8393        using a magic NULL_RTX component.
8394        This is not strictly correct.  Only some of the arg belongs in
8395        memory, not all of it.  However, the normal scheme using
8396        function_arg_partial_nregs can result in unusual subregs, eg.
8397        (subreg:SI (reg:DF) 4), which are not handled well.  The code to
8398        store the whole arg to memory is often more efficient than code
8399        to store pieces, and we know that space is available in the right
8400        place for the whole arg.  */
8401     rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8402
8403   i = 0;
8404   do
8405     {
8406       rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
8407       rtx off = GEN_INT (i++ * 4);
8408       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8409     }
8410   while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
8411
8412   return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8413 }
8414
8415 /* Determine where to put an argument to a function.
8416    Value is zero to push the argument on the stack,
8417    or a hard register in which to store the argument.
8418
8419    MODE is the argument's machine mode.
8420    TYPE is the data type of the argument (as a tree).
8421     This is null for libcalls where that information may
8422     not be available.
8423    CUM is a variable of type CUMULATIVE_ARGS which gives info about
8424     the preceding args and about the function being called.  It is
8425     not modified in this routine.
8426    NAMED is nonzero if this argument is a named parameter
8427     (otherwise it is an extra parameter matching an ellipsis).
8428
8429    On RS/6000 the first eight words of non-FP are normally in registers
8430    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
8431    Under V.4, the first 8 FP args are in registers.
8432
8433    If this is floating-point and no prototype is specified, we use
8434    both an FP and integer register (or possibly FP reg and stack).  Library
8435    functions (when CALL_LIBCALL is set) always have the proper types for args,
8436    so we can pass the FP value just in one register.  emit_library_function
8437    doesn't support PARALLEL anyway.
8438
8439    Note that for args passed by reference, function_arg will be called
8440    with MODE and TYPE set to that of the pointer to the arg, not the arg
8441    itself.  */
8442
8443 static rtx
8444 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
8445                      const_tree type, bool named)
8446 {
8447   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8448   enum rs6000_abi abi = DEFAULT_ABI;
8449
8450   /* Return a marker to indicate whether CR1 needs to set or clear the
8451      bit that V.4 uses to say fp args were passed in registers.
8452      Assume that we don't need the marker for software floating point,
8453      or compiler generated library calls.  */
8454   if (mode == VOIDmode)
8455     {
8456       if (abi == ABI_V4
8457           && (cum->call_cookie & CALL_LIBCALL) == 0
8458           && (cum->stdarg
8459               || (cum->nargs_prototype < 0
8460                   && (cum->prototype || TARGET_NO_PROTOTYPE))))
8461         {
8462           /* For the SPE, we need to crxor CR6 always.  */
8463           if (TARGET_SPE_ABI)
8464             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
8465           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
8466             return GEN_INT (cum->call_cookie
8467                             | ((cum->fregno == FP_ARG_MIN_REG)
8468                                ? CALL_V4_SET_FP_ARGS
8469                                : CALL_V4_CLEAR_FP_ARGS));
8470         }
8471
8472       return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
8473     }
8474
8475   if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8476     {
8477       rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
8478       if (rslt != NULL_RTX)
8479         return rslt;
8480       /* Else fall through to usual handling.  */
8481     }
8482
8483   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8484     if (TARGET_64BIT && ! cum->prototype)
8485       {
8486         /* Vector parameters get passed in vector register
8487            and also in GPRs or memory, in absence of prototype.  */
8488         int align_words;
8489         rtx slot;
8490         align_words = (cum->words + 1) & ~1;
8491
8492         if (align_words >= GP_ARG_NUM_REG)
8493           {
8494             slot = NULL_RTX;
8495           }
8496         else
8497           {
8498             slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8499           }
8500         return gen_rtx_PARALLEL (mode,
8501                  gen_rtvec (2,
8502                             gen_rtx_EXPR_LIST (VOIDmode,
8503                                                slot, const0_rtx),
8504                             gen_rtx_EXPR_LIST (VOIDmode,
8505                                                gen_rtx_REG (mode, cum->vregno),
8506                                                const0_rtx)));
8507       }
8508     else
8509       return gen_rtx_REG (mode, cum->vregno);
8510   else if (TARGET_ALTIVEC_ABI
8511            && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8512                || (type && TREE_CODE (type) == VECTOR_TYPE
8513                    && int_size_in_bytes (type) == 16)))
8514     {
8515       if (named || abi == ABI_V4)
8516         return NULL_RTX;
8517       else
8518         {
8519           /* Vector parameters to varargs functions under AIX or Darwin
8520              get passed in memory and possibly also in GPRs.  */
8521           int align, align_words, n_words;
8522           enum machine_mode part_mode;
8523
8524           /* Vector parameters must be 16-byte aligned.  This places them at
8525              2 mod 4 in terms of words in 32-bit mode, since the parameter
8526              save area starts at offset 24 from the stack.  In 64-bit mode,
8527              they just have to start on an even word, since the parameter
8528              save area is 16-byte aligned.  */
8529           if (TARGET_32BIT)
8530             align = (2 - cum->words) & 3;
8531           else
8532             align = cum->words & 1;
8533           align_words = cum->words + align;
8534
8535           /* Out of registers?  Memory, then.  */
8536           if (align_words >= GP_ARG_NUM_REG)
8537             return NULL_RTX;
8538
8539           if (TARGET_32BIT && TARGET_POWERPC64)
8540             return rs6000_mixed_function_arg (mode, type, align_words);
8541
8542           /* The vector value goes in GPRs.  Only the part of the
8543              value in GPRs is reported here.  */
8544           part_mode = mode;
8545           n_words = rs6000_arg_size (mode, type);
8546           if (align_words + n_words > GP_ARG_NUM_REG)
8547             /* Fortunately, there are only two possibilities, the value
8548                is either wholly in GPRs or half in GPRs and half not.  */
8549             part_mode = DImode;
8550
8551           return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
8552         }
8553     }
8554   else if (TARGET_SPE_ABI && TARGET_SPE
8555            && (SPE_VECTOR_MODE (mode)
8556                || (TARGET_E500_DOUBLE && (mode == DFmode
8557                                           || mode == DCmode
8558                                           || mode == TFmode
8559                                           || mode == TCmode))))
8560     return rs6000_spe_function_arg (cum, mode, type);
8561
8562   else if (abi == ABI_V4)
8563     {
8564       if (TARGET_HARD_FLOAT && TARGET_FPRS
8565           && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8566               || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8567               || (mode == TFmode && !TARGET_IEEEQUAD)
8568               || mode == SDmode || mode == DDmode || mode == TDmode))
8569         {
8570           /* _Decimal128 must use an even/odd register pair.  This assumes
8571              that the register number is odd when fregno is odd.  */
8572           if (mode == TDmode && (cum->fregno % 2) == 1)
8573             cum->fregno++;
8574
8575           if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8576               <= FP_ARG_V4_MAX_REG)
8577             return gen_rtx_REG (mode, cum->fregno);
8578           else
8579             return NULL_RTX;
8580         }
8581       else
8582         {
8583           int n_words = rs6000_arg_size (mode, type);
8584           int gregno = cum->sysv_gregno;
8585
8586           /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8587              (r7,r8) or (r9,r10).  As does any other 2 word item such
8588              as complex int due to a historical mistake.  */
8589           if (n_words == 2)
8590             gregno += (1 - gregno) & 1;
8591
8592           /* Multi-reg args are not split between registers and stack.  */
8593           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8594             return NULL_RTX;
8595
8596           if (TARGET_32BIT && TARGET_POWERPC64)
8597             return rs6000_mixed_function_arg (mode, type,
8598                                               gregno - GP_ARG_MIN_REG);
8599           return gen_rtx_REG (mode, gregno);
8600         }
8601     }
8602   else
8603     {
8604       int align_words = rs6000_parm_start (mode, type, cum->words);
8605
8606       /* _Decimal128 must be passed in an even/odd float register pair.
8607          This assumes that the register number is odd when fregno is odd.  */
8608       if (mode == TDmode && (cum->fregno % 2) == 1)
8609         cum->fregno++;
8610
8611       if (USE_FP_FOR_ARG_P (cum, mode, type))
8612         {
8613           rtx rvec[GP_ARG_NUM_REG + 1];
8614           rtx r;
8615           int k;
8616           bool needs_psave;
8617           enum machine_mode fmode = mode;
8618           unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8619
8620           if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8621             {
8622               /* Currently, we only ever need one reg here because complex
8623                  doubles are split.  */
8624               gcc_assert (cum->fregno == FP_ARG_MAX_REG
8625                           && (fmode == TFmode || fmode == TDmode));
8626
8627               /* Long double or _Decimal128 split over regs and memory.  */
8628               fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
8629             }
8630
8631           /* Do we also need to pass this arg in the parameter save
8632              area?  */
8633           needs_psave = (type
8634                          && (cum->nargs_prototype <= 0
8635                              || (DEFAULT_ABI == ABI_AIX
8636                                  && TARGET_XL_COMPAT
8637                                  && align_words >= GP_ARG_NUM_REG)));
8638
8639           if (!needs_psave && mode == fmode)
8640             return gen_rtx_REG (fmode, cum->fregno);
8641
8642           k = 0;
8643           if (needs_psave)
8644             {
8645               /* Describe the part that goes in gprs or the stack.
8646                  This piece must come first, before the fprs.  */
8647               if (align_words < GP_ARG_NUM_REG)
8648                 {
8649                   unsigned long n_words = rs6000_arg_size (mode, type);
8650
8651                   if (align_words + n_words > GP_ARG_NUM_REG
8652                       || (TARGET_32BIT && TARGET_POWERPC64))
8653                     {
8654                       /* If this is partially on the stack, then we only
8655                          include the portion actually in registers here.  */
8656                       enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
8657                       rtx off;
8658                       int i = 0;
8659                       if (align_words + n_words > GP_ARG_NUM_REG)
8660                         /* Not all of the arg fits in gprs.  Say that it
8661                            goes in memory too, using a magic NULL_RTX
8662                            component.  Also see comment in
8663                            rs6000_mixed_function_arg for why the normal
8664                            function_arg_partial_nregs scheme doesn't work
8665                            in this case. */
8666                         rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
8667                                                        const0_rtx);
8668                       do
8669                         {
8670                           r = gen_rtx_REG (rmode,
8671                                            GP_ARG_MIN_REG + align_words);
8672                           off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
8673                           rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8674                         }
8675                       while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
8676                     }
8677                   else
8678                     {
8679                       /* The whole arg fits in gprs.  */
8680                       r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8681                       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8682                     }
8683                 }
8684               else
8685                 /* It's entirely in memory.  */
8686                 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8687             }
8688
8689           /* Describe where this piece goes in the fprs.  */
8690           r = gen_rtx_REG (fmode, cum->fregno);
8691           rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8692
8693           return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8694         }
8695       else if (align_words < GP_ARG_NUM_REG)
8696         {
8697           if (TARGET_32BIT && TARGET_POWERPC64)
8698             return rs6000_mixed_function_arg (mode, type, align_words);
8699
8700           if (mode == BLKmode)
8701             mode = Pmode;
8702
8703           return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8704         }
8705       else
8706         return NULL_RTX;
8707     }
8708 }
8709 \f
8710 /* For an arg passed partly in registers and partly in memory, this is
8711    the number of bytes passed in registers.  For args passed entirely in
8712    registers or entirely in memory, zero.  When an arg is described by a
8713    PARALLEL, perhaps using more than one register type, this function
8714    returns the number of bytes used by the first element of the PARALLEL.  */
8715
8716 static int
8717 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
8718                           tree type, bool named)
8719 {
8720   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8721   int ret = 0;
8722   int align_words;
8723
8724   if (DEFAULT_ABI == ABI_V4)
8725     return 0;
8726
8727   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
8728       && cum->nargs_prototype >= 0)
8729     return 0;
8730
8731   /* In this complicated case we just disable the partial_nregs code.  */
8732   if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8733     return 0;
8734
8735   align_words = rs6000_parm_start (mode, type, cum->words);
8736
8737   if (USE_FP_FOR_ARG_P (cum, mode, type))
8738     {
8739       /* If we are passing this arg in the fixed parameter save area
8740          (gprs or memory) as well as fprs, then this function should
8741          return the number of partial bytes passed in the parameter
8742          save area rather than partial bytes passed in fprs.  */
8743       if (type
8744           && (cum->nargs_prototype <= 0
8745               || (DEFAULT_ABI == ABI_AIX
8746                   && TARGET_XL_COMPAT
8747                   && align_words >= GP_ARG_NUM_REG)))
8748         return 0;
8749       else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
8750                > FP_ARG_MAX_REG + 1)
8751         ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
8752       else if (cum->nargs_prototype >= 0)
8753         return 0;
8754     }
8755
8756   if (align_words < GP_ARG_NUM_REG
8757       && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
8758     ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
8759
8760   if (ret != 0 && TARGET_DEBUG_ARG)
8761     fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
8762
8763   return ret;
8764 }
8765 \f
8766 /* A C expression that indicates when an argument must be passed by
8767    reference.  If nonzero for an argument, a copy of that argument is
8768    made in memory and a pointer to the argument is passed instead of
8769    the argument itself.  The pointer is passed in whatever way is
8770    appropriate for passing a pointer to that type.
8771
8772    Under V.4, aggregates and long double are passed by reference.
8773
8774    As an extension to all 32-bit ABIs, AltiVec vectors are passed by
8775    reference unless the AltiVec vector extension ABI is in force.
8776
8777    As an extension to all ABIs, variable sized types are passed by
8778    reference.  */
8779
8780 static bool
8781 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
8782                           enum machine_mode mode, const_tree type,
8783                           bool named ATTRIBUTE_UNUSED)
8784 {
8785   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
8786     {
8787       if (TARGET_DEBUG_ARG)
8788         fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
8789       return 1;
8790     }
8791
8792   if (!type)
8793     return 0;
8794
8795   if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
8796     {
8797       if (TARGET_DEBUG_ARG)
8798         fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
8799       return 1;
8800     }
8801
8802   if (int_size_in_bytes (type) < 0)
8803     {
8804       if (TARGET_DEBUG_ARG)
8805         fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
8806       return 1;
8807     }
8808
8809   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
8810      modes only exist for GCC vector types if -maltivec.  */
8811   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
8812     {
8813       if (TARGET_DEBUG_ARG)
8814         fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
8815       return 1;
8816     }
8817
8818   /* Pass synthetic vectors in memory.  */
8819   if (TREE_CODE (type) == VECTOR_TYPE
8820       && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8821     {
8822       static bool warned_for_pass_big_vectors = false;
8823       if (TARGET_DEBUG_ARG)
8824         fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
8825       if (!warned_for_pass_big_vectors)
8826         {
8827           warning (0, "GCC vector passed by reference: "
8828                    "non-standard ABI extension with no compatibility guarantee");
8829           warned_for_pass_big_vectors = true;
8830         }
8831       return 1;
8832     }
8833
8834   return 0;
8835 }
8836
8837 static void
8838 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
8839 {
8840   int i;
8841   enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
8842
8843   if (nregs == 0)
8844     return;
8845
8846   for (i = 0; i < nregs; i++)
8847     {
8848       rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
8849       if (reload_completed)
8850         {
8851           if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
8852             tem = NULL_RTX;
8853           else
8854             tem = simplify_gen_subreg (reg_mode, x, BLKmode,
8855                                        i * GET_MODE_SIZE (reg_mode));
8856         }
8857       else
8858         tem = replace_equiv_address (tem, XEXP (tem, 0));
8859
8860       gcc_assert (tem);
8861
8862       emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
8863     }
8864 }
8865 \f
8866 /* Perform any needed actions needed for a function that is receiving a
8867    variable number of arguments.
8868
8869    CUM is as above.
8870
8871    MODE and TYPE are the mode and type of the current parameter.
8872
8873    PRETEND_SIZE is a variable that should be set to the amount of stack
8874    that must be pushed by the prolog to pretend that our caller pushed
8875    it.
8876
8877    Normally, this macro will push all remaining incoming registers on the
8878    stack and set PRETEND_SIZE to the length of the registers pushed.  */
8879
8880 static void
8881 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
8882                         tree type, int *pretend_size ATTRIBUTE_UNUSED,
8883                         int no_rtl)
8884 {
8885   CUMULATIVE_ARGS next_cum;
8886   int reg_size = TARGET_32BIT ? 4 : 8;
8887   rtx save_area = NULL_RTX, mem;
8888   int first_reg_offset;
8889   alias_set_type set;
8890
8891   /* Skip the last named argument.  */
8892   next_cum = *get_cumulative_args (cum);
8893   rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
8894
8895   if (DEFAULT_ABI == ABI_V4)
8896     {
8897       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
8898
8899       if (! no_rtl)
8900         {
8901           int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
8902           HOST_WIDE_INT offset = 0;
8903
8904           /* Try to optimize the size of the varargs save area.
8905              The ABI requires that ap.reg_save_area is doubleword
8906              aligned, but we don't need to allocate space for all
8907              the bytes, only those to which we actually will save
8908              anything.  */
8909           if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
8910             gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
8911           if (TARGET_HARD_FLOAT && TARGET_FPRS
8912               && next_cum.fregno <= FP_ARG_V4_MAX_REG
8913               && cfun->va_list_fpr_size)
8914             {
8915               if (gpr_reg_num)
8916                 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
8917                            * UNITS_PER_FP_WORD;
8918               if (cfun->va_list_fpr_size
8919                   < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8920                 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
8921               else
8922                 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8923                             * UNITS_PER_FP_WORD;
8924             }
8925           if (gpr_reg_num)
8926             {
8927               offset = -((first_reg_offset * reg_size) & ~7);
8928               if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
8929                 {
8930                   gpr_reg_num = cfun->va_list_gpr_size;
8931                   if (reg_size == 4 && (first_reg_offset & 1))
8932                     gpr_reg_num++;
8933                 }
8934               gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
8935             }
8936           else if (fpr_size)
8937             offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
8938                        * UNITS_PER_FP_WORD
8939                      - (int) (GP_ARG_NUM_REG * reg_size);
8940
8941           if (gpr_size + fpr_size)
8942             {
8943               rtx reg_save_area
8944                 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
8945               gcc_assert (GET_CODE (reg_save_area) == MEM);
8946               reg_save_area = XEXP (reg_save_area, 0);
8947               if (GET_CODE (reg_save_area) == PLUS)
8948                 {
8949                   gcc_assert (XEXP (reg_save_area, 0)
8950                               == virtual_stack_vars_rtx);
8951                   gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
8952                   offset += INTVAL (XEXP (reg_save_area, 1));
8953                 }
8954               else
8955                 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
8956             }
8957
8958           cfun->machine->varargs_save_offset = offset;
8959           save_area = plus_constant (virtual_stack_vars_rtx, offset);
8960         }
8961     }
8962   else
8963     {
8964       first_reg_offset = next_cum.words;
8965       save_area = virtual_incoming_args_rtx;
8966
8967       if (targetm.calls.must_pass_in_stack (mode, type))
8968         first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
8969     }
8970
8971   set = get_varargs_alias_set ();
8972   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
8973       && cfun->va_list_gpr_size)
8974     {
8975       int nregs = GP_ARG_NUM_REG - first_reg_offset;
8976
8977       if (va_list_gpr_counter_field)
8978         {
8979           /* V4 va_list_gpr_size counts number of registers needed.  */
8980           if (nregs > cfun->va_list_gpr_size)
8981             nregs = cfun->va_list_gpr_size;
8982         }
8983       else
8984         {
8985           /* char * va_list instead counts number of bytes needed.  */
8986           if (nregs > cfun->va_list_gpr_size / reg_size)
8987             nregs = cfun->va_list_gpr_size / reg_size;
8988         }
8989
8990       mem = gen_rtx_MEM (BLKmode,
8991                          plus_constant (save_area,
8992                                         first_reg_offset * reg_size));
8993       MEM_NOTRAP_P (mem) = 1;
8994       set_mem_alias_set (mem, set);
8995       set_mem_align (mem, BITS_PER_WORD);
8996
8997       rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
8998                                   nregs);
8999     }
9000
9001   /* Save FP registers if needed.  */
9002   if (DEFAULT_ABI == ABI_V4
9003       && TARGET_HARD_FLOAT && TARGET_FPRS
9004       && ! no_rtl
9005       && next_cum.fregno <= FP_ARG_V4_MAX_REG
9006       && cfun->va_list_fpr_size)
9007     {
9008       int fregno = next_cum.fregno, nregs;
9009       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
9010       rtx lab = gen_label_rtx ();
9011       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
9012                                                * UNITS_PER_FP_WORD);
9013
9014       emit_jump_insn
9015         (gen_rtx_SET (VOIDmode,
9016                       pc_rtx,
9017                       gen_rtx_IF_THEN_ELSE (VOIDmode,
9018                                             gen_rtx_NE (VOIDmode, cr1,
9019                                                         const0_rtx),
9020                                             gen_rtx_LABEL_REF (VOIDmode, lab),
9021                                             pc_rtx)));
9022
9023       for (nregs = 0;
9024            fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
9025            fregno++, off += UNITS_PER_FP_WORD, nregs++)
9026         {
9027           mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9028                               ? DFmode : SFmode, 
9029                              plus_constant (save_area, off));
9030           MEM_NOTRAP_P (mem) = 1;
9031           set_mem_alias_set (mem, set);
9032           set_mem_align (mem, GET_MODE_ALIGNMENT (
9033                          (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9034                           ? DFmode : SFmode));
9035           emit_move_insn (mem, gen_rtx_REG (
9036                           (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9037                            ? DFmode : SFmode, fregno));
9038         }
9039
9040       emit_label (lab);
9041     }
9042 }
9043
9044 /* Create the va_list data type.  */
9045
9046 static tree
9047 rs6000_build_builtin_va_list (void)
9048 {
9049   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
9050
9051   /* For AIX, prefer 'char *' because that's what the system
9052      header files like.  */
9053   if (DEFAULT_ABI != ABI_V4)
9054     return build_pointer_type (char_type_node);
9055
9056   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
9057   type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
9058                           get_identifier ("__va_list_tag"), record);
9059
9060   f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
9061                       unsigned_char_type_node);
9062   f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
9063                       unsigned_char_type_node);
9064   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
9065      every user file.  */
9066   f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9067                       get_identifier ("reserved"), short_unsigned_type_node);
9068   f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9069                       get_identifier ("overflow_arg_area"),
9070                       ptr_type_node);
9071   f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9072                       get_identifier ("reg_save_area"),
9073                       ptr_type_node);
9074
9075   va_list_gpr_counter_field = f_gpr;
9076   va_list_fpr_counter_field = f_fpr;
9077
9078   DECL_FIELD_CONTEXT (f_gpr) = record;
9079   DECL_FIELD_CONTEXT (f_fpr) = record;
9080   DECL_FIELD_CONTEXT (f_res) = record;
9081   DECL_FIELD_CONTEXT (f_ovf) = record;
9082   DECL_FIELD_CONTEXT (f_sav) = record;
9083
9084   TYPE_STUB_DECL (record) = type_decl;
9085   TYPE_NAME (record) = type_decl;
9086   TYPE_FIELDS (record) = f_gpr;
9087   DECL_CHAIN (f_gpr) = f_fpr;
9088   DECL_CHAIN (f_fpr) = f_res;
9089   DECL_CHAIN (f_res) = f_ovf;
9090   DECL_CHAIN (f_ovf) = f_sav;
9091
9092   layout_type (record);
9093
9094   /* The correct type is an array type of one element.  */
9095   return build_array_type (record, build_index_type (size_zero_node));
9096 }
9097
9098 /* Implement va_start.  */
9099
9100 static void
9101 rs6000_va_start (tree valist, rtx nextarg)
9102 {
9103   HOST_WIDE_INT words, n_gpr, n_fpr;
9104   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9105   tree gpr, fpr, ovf, sav, t;
9106
9107   /* Only SVR4 needs something special.  */
9108   if (DEFAULT_ABI != ABI_V4)
9109     {
9110       std_expand_builtin_va_start (valist, nextarg);
9111       return;
9112     }
9113
9114   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9115   f_fpr = DECL_CHAIN (f_gpr);
9116   f_res = DECL_CHAIN (f_fpr);
9117   f_ovf = DECL_CHAIN (f_res);
9118   f_sav = DECL_CHAIN (f_ovf);
9119
9120   valist = build_simple_mem_ref (valist);
9121   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9122   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9123                 f_fpr, NULL_TREE);
9124   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9125                 f_ovf, NULL_TREE);
9126   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9127                 f_sav, NULL_TREE);
9128
9129   /* Count number of gp and fp argument registers used.  */
9130   words = crtl->args.info.words;
9131   n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
9132                GP_ARG_NUM_REG);
9133   n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
9134                FP_ARG_NUM_REG);
9135
9136   if (TARGET_DEBUG_ARG)
9137     fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
9138              HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
9139              words, n_gpr, n_fpr);
9140
9141   if (cfun->va_list_gpr_size)
9142     {
9143       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9144                   build_int_cst (NULL_TREE, n_gpr));
9145       TREE_SIDE_EFFECTS (t) = 1;
9146       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9147     }
9148
9149   if (cfun->va_list_fpr_size)
9150     {
9151       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9152                   build_int_cst (NULL_TREE, n_fpr));
9153       TREE_SIDE_EFFECTS (t) = 1;
9154       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9155
9156 #ifdef HAVE_AS_GNU_ATTRIBUTE
9157       if (call_ABI_of_interest (cfun->decl))
9158         rs6000_passes_float = true;
9159 #endif
9160     }
9161
9162   /* Find the overflow area.  */
9163   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9164   if (words != 0)
9165     t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
9166   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9167   TREE_SIDE_EFFECTS (t) = 1;
9168   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9169
9170   /* If there were no va_arg invocations, don't set up the register
9171      save area.  */
9172   if (!cfun->va_list_gpr_size
9173       && !cfun->va_list_fpr_size
9174       && n_gpr < GP_ARG_NUM_REG
9175       && n_fpr < FP_ARG_V4_MAX_REG)
9176     return;
9177
9178   /* Find the register save area.  */
9179   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
9180   if (cfun->machine->varargs_save_offset)
9181     t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
9182   t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9183   TREE_SIDE_EFFECTS (t) = 1;
9184   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9185 }
9186
9187 /* Implement va_arg.  */
9188
9189 tree
9190 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9191                         gimple_seq *post_p)
9192 {
9193   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9194   tree gpr, fpr, ovf, sav, reg, t, u;
9195   int size, rsize, n_reg, sav_ofs, sav_scale;
9196   tree lab_false, lab_over, addr;
9197   int align;
9198   tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
9199   int regalign = 0;
9200   gimple stmt;
9201
9202   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9203     {
9204       t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
9205       return build_va_arg_indirect_ref (t);
9206     }
9207
9208   /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
9209      earlier version of gcc, with the property that it always applied alignment
9210      adjustments to the va-args (even for zero-sized types).  The cheapest way
9211      to deal with this is to replicate the effect of the part of 
9212      std_gimplify_va_arg_expr that carries out the align adjust, for the case 
9213      of relevance.  
9214      We don't need to check for pass-by-reference because of the test above.
9215      We can return a simplifed answer, since we know there's no offset to add.  */
9216
9217   if (TARGET_MACHO
9218       && rs6000_darwin64_abi 
9219       && integer_zerop (TYPE_SIZE (type)))
9220     {
9221       unsigned HOST_WIDE_INT align, boundary;
9222       tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9223       align = PARM_BOUNDARY / BITS_PER_UNIT;
9224       boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
9225       if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9226         boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9227       boundary /= BITS_PER_UNIT;
9228       if (boundary > align)
9229         {
9230           tree t ;
9231           /* This updates arg ptr by the amount that would be necessary
9232              to align the zero-sized (but not zero-alignment) item.  */
9233           t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9234                       fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9235           gimplify_and_add (t, pre_p);
9236
9237           t = fold_convert (sizetype, valist_tmp);
9238           t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9239                   fold_convert (TREE_TYPE (valist),
9240                                 fold_build2 (BIT_AND_EXPR, sizetype, t,
9241                                              size_int (-boundary))));
9242           t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9243           gimplify_and_add (t, pre_p);
9244         }
9245       /* Since it is zero-sized there's no increment for the item itself. */
9246       valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
9247       return build_va_arg_indirect_ref (valist_tmp);
9248     }
9249
9250   if (DEFAULT_ABI != ABI_V4)
9251     {
9252       if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
9253         {
9254           tree elem_type = TREE_TYPE (type);
9255           enum machine_mode elem_mode = TYPE_MODE (elem_type);
9256           int elem_size = GET_MODE_SIZE (elem_mode);
9257
9258           if (elem_size < UNITS_PER_WORD)
9259             {
9260               tree real_part, imag_part;
9261               gimple_seq post = NULL;
9262
9263               real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9264                                                   &post);
9265               /* Copy the value into a temporary, lest the formal temporary
9266                  be reused out from under us.  */
9267               real_part = get_initialized_tmp_var (real_part, pre_p, &post);
9268               gimple_seq_add_seq (pre_p, post);
9269
9270               imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9271                                                   post_p);
9272
9273               return build2 (COMPLEX_EXPR, type, real_part, imag_part);
9274             }
9275         }
9276
9277       return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
9278     }
9279
9280   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9281   f_fpr = DECL_CHAIN (f_gpr);
9282   f_res = DECL_CHAIN (f_fpr);
9283   f_ovf = DECL_CHAIN (f_res);
9284   f_sav = DECL_CHAIN (f_ovf);
9285
9286   valist = build_va_arg_indirect_ref (valist);
9287   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9288   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9289                 f_fpr, NULL_TREE);
9290   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9291                 f_ovf, NULL_TREE);
9292   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9293                 f_sav, NULL_TREE);
9294
9295   size = int_size_in_bytes (type);
9296   rsize = (size + 3) / 4;
9297   align = 1;
9298
9299   if (TARGET_HARD_FLOAT && TARGET_FPRS
9300       && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
9301           || (TARGET_DOUBLE_FLOAT 
9302               && (TYPE_MODE (type) == DFmode 
9303                   || TYPE_MODE (type) == TFmode
9304                   || TYPE_MODE (type) == SDmode
9305                   || TYPE_MODE (type) == DDmode
9306                   || TYPE_MODE (type) == TDmode))))
9307     {
9308       /* FP args go in FP registers, if present.  */
9309       reg = fpr;
9310       n_reg = (size + 7) / 8;
9311       sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
9312       sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
9313       if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
9314         align = 8;
9315     }
9316   else
9317     {
9318       /* Otherwise into GP registers.  */
9319       reg = gpr;
9320       n_reg = rsize;
9321       sav_ofs = 0;
9322       sav_scale = 4;
9323       if (n_reg == 2)
9324         align = 8;
9325     }
9326
9327   /* Pull the value out of the saved registers....  */
9328
9329   lab_over = NULL;
9330   addr = create_tmp_var (ptr_type_node, "addr");
9331
9332   /*  AltiVec vectors never go in registers when -mabi=altivec.  */
9333   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
9334     align = 16;
9335   else
9336     {
9337       lab_false = create_artificial_label (input_location);
9338       lab_over = create_artificial_label (input_location);
9339
9340       /* Long long and SPE vectors are aligned in the registers.
9341          As are any other 2 gpr item such as complex int due to a
9342          historical mistake.  */
9343       u = reg;
9344       if (n_reg == 2 && reg == gpr)
9345         {
9346           regalign = 1;
9347           u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9348                      build_int_cst (TREE_TYPE (reg), n_reg - 1));
9349           u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
9350                       unshare_expr (reg), u);
9351         }
9352       /* _Decimal128 is passed in even/odd fpr pairs; the stored
9353          reg number is 0 for f1, so we want to make it odd.  */
9354       else if (reg == fpr && TYPE_MODE (type) == TDmode)
9355         {
9356           t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9357                       build_int_cst (TREE_TYPE (reg), 1));
9358           u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
9359         }
9360
9361       t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
9362       t = build2 (GE_EXPR, boolean_type_node, u, t);
9363       u = build1 (GOTO_EXPR, void_type_node, lab_false);
9364       t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
9365       gimplify_and_add (t, pre_p);
9366
9367       t = sav;
9368       if (sav_ofs)
9369         t = fold_build_pointer_plus_hwi (sav, sav_ofs);
9370
9371       u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9372                   build_int_cst (TREE_TYPE (reg), n_reg));
9373       u = fold_convert (sizetype, u);
9374       u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
9375       t = fold_build_pointer_plus (t, u);
9376
9377       /* _Decimal32 varargs are located in the second word of the 64-bit
9378          FP register for 32-bit binaries.  */
9379       if (!TARGET_POWERPC64
9380           && TARGET_HARD_FLOAT && TARGET_FPRS
9381           && TYPE_MODE (type) == SDmode)
9382         t = fold_build_pointer_plus_hwi (t, size);
9383
9384       gimplify_assign (addr, t, pre_p);
9385
9386       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
9387
9388       stmt = gimple_build_label (lab_false);
9389       gimple_seq_add_stmt (pre_p, stmt);
9390
9391       if ((n_reg == 2 && !regalign) || n_reg > 2)
9392         {
9393           /* Ensure that we don't find any more args in regs.
9394              Alignment has taken care of for special cases.  */
9395           gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
9396         }
9397     }
9398
9399   /* ... otherwise out of the overflow area.  */
9400
9401   /* Care for on-stack alignment if needed.  */
9402   t = ovf;
9403   if (align != 1)
9404     {
9405       t = fold_build_pointer_plus_hwi (t, align - 1);
9406       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
9407                   build_int_cst (TREE_TYPE (t), -align));
9408     }
9409   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
9410
9411   gimplify_assign (unshare_expr (addr), t, pre_p);
9412
9413   t = fold_build_pointer_plus_hwi (t, size);
9414   gimplify_assign (unshare_expr (ovf), t, pre_p);
9415
9416   if (lab_over)
9417     {
9418       stmt = gimple_build_label (lab_over);
9419       gimple_seq_add_stmt (pre_p, stmt);
9420     }
9421
9422   if (STRICT_ALIGNMENT
9423       && (TYPE_ALIGN (type)
9424           > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
9425     {
9426       /* The value (of type complex double, for example) may not be
9427          aligned in memory in the saved registers, so copy via a
9428          temporary.  (This is the same code as used for SPARC.)  */
9429       tree tmp = create_tmp_var (type, "va_arg_tmp");
9430       tree dest_addr = build_fold_addr_expr (tmp);
9431
9432       tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
9433                                    3, dest_addr, addr, size_int (rsize * 4));
9434
9435       gimplify_and_add (copy, pre_p);
9436       addr = dest_addr;
9437     }
9438
9439   addr = fold_convert (ptrtype, addr);
9440   return build_va_arg_indirect_ref (addr);
9441 }
9442
9443 /* Builtins.  */
9444
9445 static void
9446 def_builtin (const char *name, tree type, enum rs6000_builtins code)
9447 {
9448   tree t;
9449   unsigned classify = rs6000_builtin_info[(int)code].attr;
9450   const char *attr_string = "";
9451
9452   gcc_assert (name != NULL);
9453   gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
9454
9455   if (rs6000_builtin_decls[(int)code])
9456     fatal_error ("internal error: builtin function %s already processed", name);
9457
9458   rs6000_builtin_decls[(int)code] = t =
9459     add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
9460
9461   /* Set any special attributes.  */
9462   if ((classify & RS6000_BTC_CONST) != 0)
9463     {
9464       /* const function, function only depends on the inputs.  */
9465       TREE_READONLY (t) = 1;
9466       TREE_NOTHROW (t) = 1;
9467       attr_string = ", pure";
9468     }
9469   else if ((classify & RS6000_BTC_PURE) != 0)
9470     {
9471       /* pure function, function can read global memory, but does not set any
9472          external state.  */
9473       DECL_PURE_P (t) = 1;
9474       TREE_NOTHROW (t) = 1;
9475       attr_string = ", const";
9476     }
9477   else if ((classify & RS6000_BTC_FP) != 0)
9478     {
9479       /* Function is a math function.  If rounding mode is on, then treat the
9480          function as not reading global memory, but it can have arbitrary side
9481          effects.  If it is off, then assume the function is a const function.
9482          This mimics the ATTR_MATHFN_FPROUNDING attribute in
9483          builtin-attribute.def that is used for the math functions. */
9484       TREE_NOTHROW (t) = 1;
9485       if (flag_rounding_math)
9486         {
9487           DECL_PURE_P (t) = 1;
9488           DECL_IS_NOVOPS (t) = 1;
9489           attr_string = ", fp, pure";
9490         }
9491       else
9492         {
9493           TREE_READONLY (t) = 1;
9494           attr_string = ", fp, const";
9495         }
9496     }
9497   else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
9498     gcc_unreachable ();
9499
9500   if (TARGET_DEBUG_BUILTIN)
9501     fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
9502              (int)code, name, attr_string);
9503 }
9504
9505 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
9506
9507 #undef RS6000_BUILTIN_1
9508 #undef RS6000_BUILTIN_2
9509 #undef RS6000_BUILTIN_3
9510 #undef RS6000_BUILTIN_A
9511 #undef RS6000_BUILTIN_D
9512 #undef RS6000_BUILTIN_E
9513 #undef RS6000_BUILTIN_P
9514 #undef RS6000_BUILTIN_Q
9515 #undef RS6000_BUILTIN_S
9516 #undef RS6000_BUILTIN_X
9517
9518 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9519 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9520 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
9521   { MASK, ICODE, NAME, ENUM },
9522
9523 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9524 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9525 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9526 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9527 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9528 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9529 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9530
9531 static const struct builtin_description bdesc_3arg[] =
9532 {
9533 #include "rs6000-builtin.def"
9534 };
9535
9536 /* DST operations: void foo (void *, const int, const char).  */
9537
9538 #undef RS6000_BUILTIN_1
9539 #undef RS6000_BUILTIN_2
9540 #undef RS6000_BUILTIN_3
9541 #undef RS6000_BUILTIN_A
9542 #undef RS6000_BUILTIN_D
9543 #undef RS6000_BUILTIN_E
9544 #undef RS6000_BUILTIN_P
9545 #undef RS6000_BUILTIN_Q
9546 #undef RS6000_BUILTIN_S
9547 #undef RS6000_BUILTIN_X
9548
9549 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9550 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9551 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9552 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9553 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
9554   { MASK, ICODE, NAME, ENUM },
9555
9556 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9557 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9558 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9559 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9560 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9561
9562 static const struct builtin_description bdesc_dst[] =
9563 {
9564 #include "rs6000-builtin.def"
9565 };
9566
9567 /* Simple binary operations: VECc = foo (VECa, VECb).  */
9568
9569 #undef RS6000_BUILTIN_1
9570 #undef RS6000_BUILTIN_2
9571 #undef RS6000_BUILTIN_3
9572 #undef RS6000_BUILTIN_A
9573 #undef RS6000_BUILTIN_D
9574 #undef RS6000_BUILTIN_E
9575 #undef RS6000_BUILTIN_P
9576 #undef RS6000_BUILTIN_Q
9577 #undef RS6000_BUILTIN_S
9578 #undef RS6000_BUILTIN_X
9579
9580 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9581 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
9582   { MASK, ICODE, NAME, ENUM },
9583
9584 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9585 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9586 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9587 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9588 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9589 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9590 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9591 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9592
9593 static const struct builtin_description bdesc_2arg[] =
9594 {
9595 #include "rs6000-builtin.def"
9596 };
9597
9598 #undef RS6000_BUILTIN_1
9599 #undef RS6000_BUILTIN_2
9600 #undef RS6000_BUILTIN_3
9601 #undef RS6000_BUILTIN_A
9602 #undef RS6000_BUILTIN_D
9603 #undef RS6000_BUILTIN_E
9604 #undef RS6000_BUILTIN_P
9605 #undef RS6000_BUILTIN_Q
9606 #undef RS6000_BUILTIN_S
9607 #undef RS6000_BUILTIN_X
9608
9609 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9610 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9611 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9612 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9613 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9614 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9615 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
9616   { MASK, ICODE, NAME, ENUM },
9617
9618 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9619 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9620 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9621
9622 /* AltiVec predicates.  */
9623
9624 static const struct builtin_description bdesc_altivec_preds[] =
9625 {
9626 #include "rs6000-builtin.def"
9627 };
9628
9629 /* SPE predicates.  */
9630 #undef RS6000_BUILTIN_1
9631 #undef RS6000_BUILTIN_2
9632 #undef RS6000_BUILTIN_3
9633 #undef RS6000_BUILTIN_A
9634 #undef RS6000_BUILTIN_D
9635 #undef RS6000_BUILTIN_E
9636 #undef RS6000_BUILTIN_P
9637 #undef RS6000_BUILTIN_Q
9638 #undef RS6000_BUILTIN_S
9639 #undef RS6000_BUILTIN_X
9640
9641 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9642 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9643 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9644 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9645 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9646 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9647 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9648 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9649 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
9650   { MASK, ICODE, NAME, ENUM },
9651
9652 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9653
9654 static const struct builtin_description bdesc_spe_predicates[] =
9655 {
9656 #include "rs6000-builtin.def"
9657 };
9658
9659 /* SPE evsel predicates.  */
9660 #undef RS6000_BUILTIN_1
9661 #undef RS6000_BUILTIN_2
9662 #undef RS6000_BUILTIN_3
9663 #undef RS6000_BUILTIN_A
9664 #undef RS6000_BUILTIN_D
9665 #undef RS6000_BUILTIN_E
9666 #undef RS6000_BUILTIN_P
9667 #undef RS6000_BUILTIN_Q
9668 #undef RS6000_BUILTIN_S
9669 #undef RS6000_BUILTIN_X
9670
9671 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9672 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9673 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9674 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9675 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9676 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
9677   { MASK, ICODE, NAME, ENUM },
9678
9679 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9680 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9681 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9682 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9683
9684 static const struct builtin_description bdesc_spe_evsel[] =
9685 {
9686 #include "rs6000-builtin.def"
9687 };
9688
9689 /* PAIRED predicates.  */
9690 #undef RS6000_BUILTIN_1
9691 #undef RS6000_BUILTIN_2
9692 #undef RS6000_BUILTIN_3
9693 #undef RS6000_BUILTIN_A
9694 #undef RS6000_BUILTIN_D
9695 #undef RS6000_BUILTIN_E
9696 #undef RS6000_BUILTIN_P
9697 #undef RS6000_BUILTIN_Q
9698 #undef RS6000_BUILTIN_S
9699 #undef RS6000_BUILTIN_X
9700
9701 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9702 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9703 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9704 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9705 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9706 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9707 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9708 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
9709   { MASK, ICODE, NAME, ENUM },
9710
9711 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9712 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9713
9714 static const struct builtin_description bdesc_paired_preds[] =
9715 {
9716 #include "rs6000-builtin.def"
9717 };
9718
9719 /* ABS* operations.  */
9720
9721 #undef RS6000_BUILTIN_1
9722 #undef RS6000_BUILTIN_2
9723 #undef RS6000_BUILTIN_3
9724 #undef RS6000_BUILTIN_A
9725 #undef RS6000_BUILTIN_D
9726 #undef RS6000_BUILTIN_E
9727 #undef RS6000_BUILTIN_P
9728 #undef RS6000_BUILTIN_Q
9729 #undef RS6000_BUILTIN_S
9730 #undef RS6000_BUILTIN_X
9731
9732 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9733 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9734 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9735 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
9736   { MASK, ICODE, NAME, ENUM },
9737
9738 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9739 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9740 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9741 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9742 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9743 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9744
9745 static const struct builtin_description bdesc_abs[] =
9746 {
9747 #include "rs6000-builtin.def"
9748 };
9749
9750 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
9751    foo (VECa).  */
9752
9753 #undef RS6000_BUILTIN_1
9754 #undef RS6000_BUILTIN_2
9755 #undef RS6000_BUILTIN_3
9756 #undef RS6000_BUILTIN_A
9757 #undef RS6000_BUILTIN_E
9758 #undef RS6000_BUILTIN_D
9759 #undef RS6000_BUILTIN_P
9760 #undef RS6000_BUILTIN_Q
9761 #undef RS6000_BUILTIN_S
9762 #undef RS6000_BUILTIN_X
9763
9764 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
9765   { MASK, ICODE, NAME, ENUM },
9766
9767 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9768 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9769 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9770 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9771 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9772 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9773 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9774 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9775 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9776
9777 static const struct builtin_description bdesc_1arg[] =
9778 {
9779 #include "rs6000-builtin.def"
9780 };
9781
9782 #undef RS6000_BUILTIN_1
9783 #undef RS6000_BUILTIN_2
9784 #undef RS6000_BUILTIN_3
9785 #undef RS6000_BUILTIN_A
9786 #undef RS6000_BUILTIN_D
9787 #undef RS6000_BUILTIN_E
9788 #undef RS6000_BUILTIN_P
9789 #undef RS6000_BUILTIN_Q
9790 #undef RS6000_BUILTIN_S
9791 #undef RS6000_BUILTIN_X
9792
9793 /* Return true if a builtin function is overloaded.  */
9794 bool
9795 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
9796 {
9797   return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
9798 }
9799
9800
9801 static rtx
9802 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
9803 {
9804   rtx pat;
9805   tree arg0 = CALL_EXPR_ARG (exp, 0);
9806   rtx op0 = expand_normal (arg0);
9807   enum machine_mode tmode = insn_data[icode].operand[0].mode;
9808   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9809
9810   if (icode == CODE_FOR_nothing)
9811     /* Builtin not supported on this processor.  */
9812     return 0;
9813
9814   /* If we got invalid arguments bail out before generating bad rtl.  */
9815   if (arg0 == error_mark_node)
9816     return const0_rtx;
9817
9818   if (icode == CODE_FOR_altivec_vspltisb
9819       || icode == CODE_FOR_altivec_vspltish
9820       || icode == CODE_FOR_altivec_vspltisw
9821       || icode == CODE_FOR_spe_evsplatfi
9822       || icode == CODE_FOR_spe_evsplati)
9823     {
9824       /* Only allow 5-bit *signed* literals.  */
9825       if (GET_CODE (op0) != CONST_INT
9826           || INTVAL (op0) > 15
9827           || INTVAL (op0) < -16)
9828         {
9829           error ("argument 1 must be a 5-bit signed literal");
9830           return const0_rtx;
9831         }
9832     }
9833
9834   if (target == 0
9835       || GET_MODE (target) != tmode
9836       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9837     target = gen_reg_rtx (tmode);
9838
9839   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9840     op0 = copy_to_mode_reg (mode0, op0);
9841
9842   pat = GEN_FCN (icode) (target, op0);
9843   if (! pat)
9844     return 0;
9845   emit_insn (pat);
9846
9847   return target;
9848 }
9849
9850 static rtx
9851 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
9852 {
9853   rtx pat, scratch1, scratch2;
9854   tree arg0 = CALL_EXPR_ARG (exp, 0);
9855   rtx op0 = expand_normal (arg0);
9856   enum machine_mode tmode = insn_data[icode].operand[0].mode;
9857   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9858
9859   /* If we have invalid arguments, bail out before generating bad rtl.  */
9860   if (arg0 == error_mark_node)
9861     return const0_rtx;
9862
9863   if (target == 0
9864       || GET_MODE (target) != tmode
9865       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9866     target = gen_reg_rtx (tmode);
9867
9868   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9869     op0 = copy_to_mode_reg (mode0, op0);
9870
9871   scratch1 = gen_reg_rtx (mode0);
9872   scratch2 = gen_reg_rtx (mode0);
9873
9874   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
9875   if (! pat)
9876     return 0;
9877   emit_insn (pat);
9878
9879   return target;
9880 }
9881
9882 static rtx
9883 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
9884 {
9885   rtx pat;
9886   tree arg0 = CALL_EXPR_ARG (exp, 0);
9887   tree arg1 = CALL_EXPR_ARG (exp, 1);
9888   rtx op0 = expand_normal (arg0);
9889   rtx op1 = expand_normal (arg1);
9890   enum machine_mode tmode = insn_data[icode].operand[0].mode;
9891   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9892   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
9893
9894   if (icode == CODE_FOR_nothing)
9895     /* Builtin not supported on this processor.  */
9896     return 0;
9897
9898   /* If we got invalid arguments bail out before generating bad rtl.  */
9899   if (arg0 == error_mark_node || arg1 == error_mark_node)
9900     return const0_rtx;
9901
9902   if (icode == CODE_FOR_altivec_vcfux
9903       || icode == CODE_FOR_altivec_vcfsx
9904       || icode == CODE_FOR_altivec_vctsxs
9905       || icode == CODE_FOR_altivec_vctuxs
9906       || icode == CODE_FOR_altivec_vspltb
9907       || icode == CODE_FOR_altivec_vsplth
9908       || icode == CODE_FOR_altivec_vspltw
9909       || icode == CODE_FOR_spe_evaddiw
9910       || icode == CODE_FOR_spe_evldd
9911       || icode == CODE_FOR_spe_evldh
9912       || icode == CODE_FOR_spe_evldw
9913       || icode == CODE_FOR_spe_evlhhesplat
9914       || icode == CODE_FOR_spe_evlhhossplat
9915       || icode == CODE_FOR_spe_evlhhousplat
9916       || icode == CODE_FOR_spe_evlwhe
9917       || icode == CODE_FOR_spe_evlwhos
9918       || icode == CODE_FOR_spe_evlwhou
9919       || icode == CODE_FOR_spe_evlwhsplat
9920       || icode == CODE_FOR_spe_evlwwsplat
9921       || icode == CODE_FOR_spe_evrlwi
9922       || icode == CODE_FOR_spe_evslwi
9923       || icode == CODE_FOR_spe_evsrwis
9924       || icode == CODE_FOR_spe_evsubifw
9925       || icode == CODE_FOR_spe_evsrwiu)
9926     {
9927       /* Only allow 5-bit unsigned literals.  */
9928       STRIP_NOPS (arg1);
9929       if (TREE_CODE (arg1) != INTEGER_CST
9930           || TREE_INT_CST_LOW (arg1) & ~0x1f)
9931         {
9932           error ("argument 2 must be a 5-bit unsigned literal");
9933           return const0_rtx;
9934         }
9935     }
9936
9937   if (target == 0
9938       || GET_MODE (target) != tmode
9939       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9940     target = gen_reg_rtx (tmode);
9941
9942   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9943     op0 = copy_to_mode_reg (mode0, op0);
9944   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
9945     op1 = copy_to_mode_reg (mode1, op1);
9946
9947   pat = GEN_FCN (icode) (target, op0, op1);
9948   if (! pat)
9949     return 0;
9950   emit_insn (pat);
9951
9952   return target;
9953 }
9954
9955 static rtx
9956 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
9957 {
9958   rtx pat, scratch;
9959   tree cr6_form = CALL_EXPR_ARG (exp, 0);
9960   tree arg0 = CALL_EXPR_ARG (exp, 1);
9961   tree arg1 = CALL_EXPR_ARG (exp, 2);
9962   rtx op0 = expand_normal (arg0);
9963   rtx op1 = expand_normal (arg1);
9964   enum machine_mode tmode = SImode;
9965   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9966   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
9967   int cr6_form_int;
9968
9969   if (TREE_CODE (cr6_form) != INTEGER_CST)
9970     {
9971       error ("argument 1 of __builtin_altivec_predicate must be a constant");
9972       return const0_rtx;
9973     }
9974   else
9975     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
9976
9977   gcc_assert (mode0 == mode1);
9978
9979   /* If we have invalid arguments, bail out before generating bad rtl.  */
9980   if (arg0 == error_mark_node || arg1 == error_mark_node)
9981     return const0_rtx;
9982
9983   if (target == 0
9984       || GET_MODE (target) != tmode
9985       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9986     target = gen_reg_rtx (tmode);
9987
9988   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9989     op0 = copy_to_mode_reg (mode0, op0);
9990   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
9991     op1 = copy_to_mode_reg (mode1, op1);
9992
9993   scratch = gen_reg_rtx (mode0);
9994
9995   pat = GEN_FCN (icode) (scratch, op0, op1);
9996   if (! pat)
9997     return 0;
9998   emit_insn (pat);
9999
10000   /* The vec_any* and vec_all* predicates use the same opcodes for two
10001      different operations, but the bits in CR6 will be different
10002      depending on what information we want.  So we have to play tricks
10003      with CR6 to get the right bits out.
10004
10005      If you think this is disgusting, look at the specs for the
10006      AltiVec predicates.  */
10007
10008   switch (cr6_form_int)
10009     {
10010     case 0:
10011       emit_insn (gen_cr6_test_for_zero (target));
10012       break;
10013     case 1:
10014       emit_insn (gen_cr6_test_for_zero_reverse (target));
10015       break;
10016     case 2:
10017       emit_insn (gen_cr6_test_for_lt (target));
10018       break;
10019     case 3:
10020       emit_insn (gen_cr6_test_for_lt_reverse (target));
10021       break;
10022     default:
10023       error ("argument 1 of __builtin_altivec_predicate is out of range");
10024       break;
10025     }
10026
10027   return target;
10028 }
10029
10030 static rtx
10031 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
10032 {
10033   rtx pat, addr;
10034   tree arg0 = CALL_EXPR_ARG (exp, 0);
10035   tree arg1 = CALL_EXPR_ARG (exp, 1);
10036   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10037   enum machine_mode mode0 = Pmode;
10038   enum machine_mode mode1 = Pmode;
10039   rtx op0 = expand_normal (arg0);
10040   rtx op1 = expand_normal (arg1);
10041
10042   if (icode == CODE_FOR_nothing)
10043     /* Builtin not supported on this processor.  */
10044     return 0;
10045
10046   /* If we got invalid arguments bail out before generating bad rtl.  */
10047   if (arg0 == error_mark_node || arg1 == error_mark_node)
10048     return const0_rtx;
10049
10050   if (target == 0
10051       || GET_MODE (target) != tmode
10052       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10053     target = gen_reg_rtx (tmode);
10054
10055   op1 = copy_to_mode_reg (mode1, op1);
10056
10057   if (op0 == const0_rtx)
10058     {
10059       addr = gen_rtx_MEM (tmode, op1);
10060     }
10061   else
10062     {
10063       op0 = copy_to_mode_reg (mode0, op0);
10064       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
10065     }
10066
10067   pat = GEN_FCN (icode) (target, addr);
10068
10069   if (! pat)
10070     return 0;
10071   emit_insn (pat);
10072
10073   return target;
10074 }
10075
10076 static rtx
10077 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
10078 {
10079   rtx pat, addr;
10080   tree arg0 = CALL_EXPR_ARG (exp, 0);
10081   tree arg1 = CALL_EXPR_ARG (exp, 1);
10082   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10083   enum machine_mode mode0 = Pmode;
10084   enum machine_mode mode1 = Pmode;
10085   rtx op0 = expand_normal (arg0);
10086   rtx op1 = expand_normal (arg1);
10087
10088   if (icode == CODE_FOR_nothing)
10089     /* Builtin not supported on this processor.  */
10090     return 0;
10091
10092   /* If we got invalid arguments bail out before generating bad rtl.  */
10093   if (arg0 == error_mark_node || arg1 == error_mark_node)
10094     return const0_rtx;
10095
10096   if (target == 0
10097       || GET_MODE (target) != tmode
10098       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10099     target = gen_reg_rtx (tmode);
10100
10101   op1 = copy_to_mode_reg (mode1, op1);
10102
10103   if (op0 == const0_rtx)
10104     {
10105       addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10106     }
10107   else
10108     {
10109       op0 = copy_to_mode_reg (mode0, op0);
10110       addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10111     }
10112
10113   pat = GEN_FCN (icode) (target, addr);
10114
10115   if (! pat)
10116     return 0;
10117   emit_insn (pat);
10118
10119   return target;
10120 }
10121
10122 static rtx
10123 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10124 {
10125   tree arg0 = CALL_EXPR_ARG (exp, 0);
10126   tree arg1 = CALL_EXPR_ARG (exp, 1);
10127   tree arg2 = CALL_EXPR_ARG (exp, 2);
10128   rtx op0 = expand_normal (arg0);
10129   rtx op1 = expand_normal (arg1);
10130   rtx op2 = expand_normal (arg2);
10131   rtx pat;
10132   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10133   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10134   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10135
10136   /* Invalid arguments.  Bail before doing anything stoopid!  */
10137   if (arg0 == error_mark_node
10138       || arg1 == error_mark_node
10139       || arg2 == error_mark_node)
10140     return const0_rtx;
10141
10142   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10143     op0 = copy_to_mode_reg (mode2, op0);
10144   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10145     op1 = copy_to_mode_reg (mode0, op1);
10146   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10147     op2 = copy_to_mode_reg (mode1, op2);
10148
10149   pat = GEN_FCN (icode) (op1, op2, op0);
10150   if (pat)
10151     emit_insn (pat);
10152   return NULL_RTX;
10153 }
10154
10155 static rtx
10156 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10157 {
10158   tree arg0 = CALL_EXPR_ARG (exp, 0);
10159   tree arg1 = CALL_EXPR_ARG (exp, 1);
10160   tree arg2 = CALL_EXPR_ARG (exp, 2);
10161   rtx op0 = expand_normal (arg0);
10162   rtx op1 = expand_normal (arg1);
10163   rtx op2 = expand_normal (arg2);
10164   rtx pat, addr;
10165   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10166   enum machine_mode mode1 = Pmode;
10167   enum machine_mode mode2 = Pmode;
10168
10169   /* Invalid arguments.  Bail before doing anything stoopid!  */
10170   if (arg0 == error_mark_node
10171       || arg1 == error_mark_node
10172       || arg2 == error_mark_node)
10173     return const0_rtx;
10174
10175   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10176     op0 = copy_to_mode_reg (tmode, op0);
10177
10178   op2 = copy_to_mode_reg (mode2, op2);
10179
10180   if (op1 == const0_rtx)
10181     {
10182       addr = gen_rtx_MEM (tmode, op2);
10183     }
10184   else
10185     {
10186       op1 = copy_to_mode_reg (mode1, op1);
10187       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10188     }
10189
10190   pat = GEN_FCN (icode) (addr, op0);
10191   if (pat)
10192     emit_insn (pat);
10193   return NULL_RTX;
10194 }
10195
10196 static rtx
10197 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10198 {
10199   tree arg0 = CALL_EXPR_ARG (exp, 0);
10200   tree arg1 = CALL_EXPR_ARG (exp, 1);
10201   tree arg2 = CALL_EXPR_ARG (exp, 2);
10202   rtx op0 = expand_normal (arg0);
10203   rtx op1 = expand_normal (arg1);
10204   rtx op2 = expand_normal (arg2);
10205   rtx pat, addr;
10206   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10207   enum machine_mode smode = insn_data[icode].operand[1].mode;
10208   enum machine_mode mode1 = Pmode;
10209   enum machine_mode mode2 = Pmode;
10210
10211   /* Invalid arguments.  Bail before doing anything stoopid!  */
10212   if (arg0 == error_mark_node
10213       || arg1 == error_mark_node
10214       || arg2 == error_mark_node)
10215     return const0_rtx;
10216
10217   if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
10218     op0 = copy_to_mode_reg (smode, op0);
10219
10220   op2 = copy_to_mode_reg (mode2, op2);
10221
10222   if (op1 == const0_rtx)
10223     {
10224       addr = gen_rtx_MEM (tmode, op2);
10225     }
10226   else
10227     {
10228       op1 = copy_to_mode_reg (mode1, op1);
10229       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10230     }
10231
10232   pat = GEN_FCN (icode) (addr, op0);
10233   if (pat)
10234     emit_insn (pat);
10235   return NULL_RTX;
10236 }
10237
10238 static rtx
10239 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
10240 {
10241   rtx pat;
10242   tree arg0 = CALL_EXPR_ARG (exp, 0);
10243   tree arg1 = CALL_EXPR_ARG (exp, 1);
10244   tree arg2 = CALL_EXPR_ARG (exp, 2);
10245   rtx op0 = expand_normal (arg0);
10246   rtx op1 = expand_normal (arg1);
10247   rtx op2 = expand_normal (arg2);
10248   enum machine_mode tmode = insn_data[icode].operand[0].mode;
10249   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10250   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10251   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
10252
10253   if (icode == CODE_FOR_nothing)
10254     /* Builtin not supported on this processor.  */
10255     return 0;
10256
10257   /* If we got invalid arguments bail out before generating bad rtl.  */
10258   if (arg0 == error_mark_node
10259       || arg1 == error_mark_node
10260       || arg2 == error_mark_node)
10261     return const0_rtx;
10262
10263   /* Check and prepare argument depending on the instruction code.
10264
10265      Note that a switch statement instead of the sequence of tests
10266      would be incorrect as many of the CODE_FOR values could be
10267      CODE_FOR_nothing and that would yield multiple alternatives
10268      with identical values.  We'd never reach here at runtime in
10269      this case.  */
10270   if (icode == CODE_FOR_altivec_vsldoi_v4sf
10271       || icode == CODE_FOR_altivec_vsldoi_v4si
10272       || icode == CODE_FOR_altivec_vsldoi_v8hi
10273       || icode == CODE_FOR_altivec_vsldoi_v16qi)
10274     {
10275       /* Only allow 4-bit unsigned literals.  */
10276       STRIP_NOPS (arg2);
10277       if (TREE_CODE (arg2) != INTEGER_CST
10278           || TREE_INT_CST_LOW (arg2) & ~0xf)
10279         {
10280           error ("argument 3 must be a 4-bit unsigned literal");
10281           return const0_rtx;
10282         }
10283     }
10284   else if (icode == CODE_FOR_vsx_xxpermdi_v2df
10285            || icode == CODE_FOR_vsx_xxpermdi_v2di
10286            || icode == CODE_FOR_vsx_xxsldwi_v16qi
10287            || icode == CODE_FOR_vsx_xxsldwi_v8hi
10288            || icode == CODE_FOR_vsx_xxsldwi_v4si
10289            || icode == CODE_FOR_vsx_xxsldwi_v4sf
10290            || icode == CODE_FOR_vsx_xxsldwi_v2di
10291            || icode == CODE_FOR_vsx_xxsldwi_v2df)
10292     {
10293       /* Only allow 2-bit unsigned literals.  */
10294       STRIP_NOPS (arg2);
10295       if (TREE_CODE (arg2) != INTEGER_CST
10296           || TREE_INT_CST_LOW (arg2) & ~0x3)
10297         {
10298           error ("argument 3 must be a 2-bit unsigned literal");
10299           return const0_rtx;
10300         }
10301     }
10302   else if (icode == CODE_FOR_vsx_set_v2df
10303            || icode == CODE_FOR_vsx_set_v2di)
10304     {
10305       /* Only allow 1-bit unsigned literals.  */
10306       STRIP_NOPS (arg2);
10307       if (TREE_CODE (arg2) != INTEGER_CST
10308           || TREE_INT_CST_LOW (arg2) & ~0x1)
10309         {
10310           error ("argument 3 must be a 1-bit unsigned literal");
10311           return const0_rtx;
10312         }
10313     }
10314
10315   if (target == 0
10316       || GET_MODE (target) != tmode
10317       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10318     target = gen_reg_rtx (tmode);
10319
10320   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10321     op0 = copy_to_mode_reg (mode0, op0);
10322   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10323     op1 = copy_to_mode_reg (mode1, op1);
10324   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
10325     op2 = copy_to_mode_reg (mode2, op2);
10326
10327   if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
10328     pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
10329   else 
10330     pat = GEN_FCN (icode) (target, op0, op1, op2);
10331   if (! pat)
10332     return 0;
10333   emit_insn (pat);
10334
10335   return target;
10336 }
10337
10338 /* Expand the lvx builtins.  */
10339 static rtx
10340 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
10341 {
10342   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10343   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10344   tree arg0;
10345   enum machine_mode tmode, mode0;
10346   rtx pat, op0;
10347   enum insn_code icode;
10348
10349   switch (fcode)
10350     {
10351     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
10352       icode = CODE_FOR_vector_altivec_load_v16qi;
10353       break;
10354     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
10355       icode = CODE_FOR_vector_altivec_load_v8hi;
10356       break;
10357     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
10358       icode = CODE_FOR_vector_altivec_load_v4si;
10359       break;
10360     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
10361       icode = CODE_FOR_vector_altivec_load_v4sf;
10362       break;
10363     case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
10364       icode = CODE_FOR_vector_altivec_load_v2df;
10365       break;
10366     case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
10367       icode = CODE_FOR_vector_altivec_load_v2di;
10368       break;
10369     default:
10370       *expandedp = false;
10371       return NULL_RTX;
10372     }
10373
10374   *expandedp = true;
10375
10376   arg0 = CALL_EXPR_ARG (exp, 0);
10377   op0 = expand_normal (arg0);
10378   tmode = insn_data[icode].operand[0].mode;
10379   mode0 = insn_data[icode].operand[1].mode;
10380
10381   if (target == 0
10382       || GET_MODE (target) != tmode
10383       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10384     target = gen_reg_rtx (tmode);
10385
10386   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10387     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10388
10389   pat = GEN_FCN (icode) (target, op0);
10390   if (! pat)
10391     return 0;
10392   emit_insn (pat);
10393   return target;
10394 }
10395
10396 /* Expand the stvx builtins.  */
10397 static rtx
10398 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10399                            bool *expandedp)
10400 {
10401   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10402   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10403   tree arg0, arg1;
10404   enum machine_mode mode0, mode1;
10405   rtx pat, op0, op1;
10406   enum insn_code icode;
10407
10408   switch (fcode)
10409     {
10410     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
10411       icode = CODE_FOR_vector_altivec_store_v16qi;
10412       break;
10413     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
10414       icode = CODE_FOR_vector_altivec_store_v8hi;
10415       break;
10416     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
10417       icode = CODE_FOR_vector_altivec_store_v4si;
10418       break;
10419     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
10420       icode = CODE_FOR_vector_altivec_store_v4sf;
10421       break;
10422     case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
10423       icode = CODE_FOR_vector_altivec_store_v2df;
10424       break;
10425     case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
10426       icode = CODE_FOR_vector_altivec_store_v2di;
10427       break;
10428     default:
10429       *expandedp = false;
10430       return NULL_RTX;
10431     }
10432
10433   arg0 = CALL_EXPR_ARG (exp, 0);
10434   arg1 = CALL_EXPR_ARG (exp, 1);
10435   op0 = expand_normal (arg0);
10436   op1 = expand_normal (arg1);
10437   mode0 = insn_data[icode].operand[0].mode;
10438   mode1 = insn_data[icode].operand[1].mode;
10439
10440   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10441     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10442   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
10443     op1 = copy_to_mode_reg (mode1, op1);
10444
10445   pat = GEN_FCN (icode) (op0, op1);
10446   if (pat)
10447     emit_insn (pat);
10448
10449   *expandedp = true;
10450   return NULL_RTX;
10451 }
10452
10453 /* Expand the dst builtins.  */
10454 static rtx
10455 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10456                             bool *expandedp)
10457 {
10458   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10459   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10460   tree arg0, arg1, arg2;
10461   enum machine_mode mode0, mode1;
10462   rtx pat, op0, op1, op2;
10463   const struct builtin_description *d;
10464   size_t i;
10465
10466   *expandedp = false;
10467
10468   /* Handle DST variants.  */
10469   d = bdesc_dst;
10470   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
10471     if (d->code == fcode)
10472       {
10473         arg0 = CALL_EXPR_ARG (exp, 0);
10474         arg1 = CALL_EXPR_ARG (exp, 1);
10475         arg2 = CALL_EXPR_ARG (exp, 2);
10476         op0 = expand_normal (arg0);
10477         op1 = expand_normal (arg1);
10478         op2 = expand_normal (arg2);
10479         mode0 = insn_data[d->icode].operand[0].mode;
10480         mode1 = insn_data[d->icode].operand[1].mode;
10481
10482         /* Invalid arguments, bail out before generating bad rtl.  */
10483         if (arg0 == error_mark_node
10484             || arg1 == error_mark_node
10485             || arg2 == error_mark_node)
10486           return const0_rtx;
10487
10488         *expandedp = true;
10489         STRIP_NOPS (arg2);
10490         if (TREE_CODE (arg2) != INTEGER_CST
10491             || TREE_INT_CST_LOW (arg2) & ~0x3)
10492           {
10493             error ("argument to %qs must be a 2-bit unsigned literal", d->name);
10494             return const0_rtx;
10495           }
10496
10497         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
10498           op0 = copy_to_mode_reg (Pmode, op0);
10499         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
10500           op1 = copy_to_mode_reg (mode1, op1);
10501
10502         pat = GEN_FCN (d->icode) (op0, op1, op2);
10503         if (pat != 0)
10504           emit_insn (pat);
10505
10506         return NULL_RTX;
10507       }
10508
10509   return NULL_RTX;
10510 }
10511
10512 /* Expand vec_init builtin.  */
10513 static rtx
10514 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
10515 {
10516   enum machine_mode tmode = TYPE_MODE (type);
10517   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
10518   int i, n_elt = GET_MODE_NUNITS (tmode);
10519   rtvec v = rtvec_alloc (n_elt);
10520
10521   gcc_assert (VECTOR_MODE_P (tmode));
10522   gcc_assert (n_elt == call_expr_nargs (exp));
10523
10524   for (i = 0; i < n_elt; ++i)
10525     {
10526       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
10527       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
10528     }
10529
10530   if (!target || !register_operand (target, tmode))
10531     target = gen_reg_rtx (tmode);
10532
10533   rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
10534   return target;
10535 }
10536
10537 /* Return the integer constant in ARG.  Constrain it to be in the range
10538    of the subparts of VEC_TYPE; issue an error if not.  */
10539
10540 static int
10541 get_element_number (tree vec_type, tree arg)
10542 {
10543   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
10544
10545   if (!host_integerp (arg, 1)
10546       || (elt = tree_low_cst (arg, 1), elt > max))
10547     {
10548       error ("selector must be an integer constant in the range 0..%wi", max);
10549       return 0;
10550     }
10551
10552   return elt;
10553 }
10554
10555 /* Expand vec_set builtin.  */
10556 static rtx
10557 altivec_expand_vec_set_builtin (tree exp)
10558 {
10559   enum machine_mode tmode, mode1;
10560   tree arg0, arg1, arg2;
10561   int elt;
10562   rtx op0, op1;
10563
10564   arg0 = CALL_EXPR_ARG (exp, 0);
10565   arg1 = CALL_EXPR_ARG (exp, 1);
10566   arg2 = CALL_EXPR_ARG (exp, 2);
10567
10568   tmode = TYPE_MODE (TREE_TYPE (arg0));
10569   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10570   gcc_assert (VECTOR_MODE_P (tmode));
10571
10572   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
10573   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
10574   elt = get_element_number (TREE_TYPE (arg0), arg2);
10575
10576   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
10577     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
10578
10579   op0 = force_reg (tmode, op0);
10580   op1 = force_reg (mode1, op1);
10581
10582   rs6000_expand_vector_set (op0, op1, elt);
10583
10584   return op0;
10585 }
10586
10587 /* Expand vec_ext builtin.  */
10588 static rtx
10589 altivec_expand_vec_ext_builtin (tree exp, rtx target)
10590 {
10591   enum machine_mode tmode, mode0;
10592   tree arg0, arg1;
10593   int elt;
10594   rtx op0;
10595
10596   arg0 = CALL_EXPR_ARG (exp, 0);
10597   arg1 = CALL_EXPR_ARG (exp, 1);
10598
10599   op0 = expand_normal (arg0);
10600   elt = get_element_number (TREE_TYPE (arg0), arg1);
10601
10602   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10603   mode0 = TYPE_MODE (TREE_TYPE (arg0));
10604   gcc_assert (VECTOR_MODE_P (mode0));
10605
10606   op0 = force_reg (mode0, op0);
10607
10608   if (optimize || !target || !register_operand (target, tmode))
10609     target = gen_reg_rtx (tmode);
10610
10611   rs6000_expand_vector_extract (target, op0, elt);
10612
10613   return target;
10614 }
10615
10616 /* Expand the builtin in EXP and store the result in TARGET.  Store
10617    true in *EXPANDEDP if we found a builtin to expand.  */
10618 static rtx
10619 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
10620 {
10621   const struct builtin_description *d;
10622   size_t i;
10623   enum insn_code icode;
10624   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10625   tree arg0;
10626   rtx op0, pat;
10627   enum machine_mode tmode, mode0;
10628   enum rs6000_builtins fcode
10629     = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10630
10631   if (rs6000_overloaded_builtin_p (fcode))
10632     {
10633       *expandedp = true;
10634       error ("unresolved overload for Altivec builtin %qF", fndecl);
10635
10636       /* Given it is invalid, just generate a normal call.  */
10637       return expand_call (exp, target, false);
10638     }
10639
10640   target = altivec_expand_ld_builtin (exp, target, expandedp);
10641   if (*expandedp)
10642     return target;
10643
10644   target = altivec_expand_st_builtin (exp, target, expandedp);
10645   if (*expandedp)
10646     return target;
10647
10648   target = altivec_expand_dst_builtin (exp, target, expandedp);
10649   if (*expandedp)
10650     return target;
10651
10652   *expandedp = true;
10653
10654   switch (fcode)
10655     {
10656     case ALTIVEC_BUILTIN_STVX:
10657       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
10658     case ALTIVEC_BUILTIN_STVEBX:
10659       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
10660     case ALTIVEC_BUILTIN_STVEHX:
10661       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
10662     case ALTIVEC_BUILTIN_STVEWX:
10663       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
10664     case ALTIVEC_BUILTIN_STVXL:
10665       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
10666
10667     case ALTIVEC_BUILTIN_STVLX:
10668       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
10669     case ALTIVEC_BUILTIN_STVLXL:
10670       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
10671     case ALTIVEC_BUILTIN_STVRX:
10672       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
10673     case ALTIVEC_BUILTIN_STVRXL:
10674       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
10675
10676     case VSX_BUILTIN_STXVD2X_V2DF:
10677       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
10678     case VSX_BUILTIN_STXVD2X_V2DI:
10679       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
10680     case VSX_BUILTIN_STXVW4X_V4SF:
10681       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
10682     case VSX_BUILTIN_STXVW4X_V4SI:
10683       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
10684     case VSX_BUILTIN_STXVW4X_V8HI:
10685       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
10686     case VSX_BUILTIN_STXVW4X_V16QI:
10687       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
10688
10689     case ALTIVEC_BUILTIN_MFVSCR:
10690       icode = CODE_FOR_altivec_mfvscr;
10691       tmode = insn_data[icode].operand[0].mode;
10692
10693       if (target == 0
10694           || GET_MODE (target) != tmode
10695           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10696         target = gen_reg_rtx (tmode);
10697
10698       pat = GEN_FCN (icode) (target);
10699       if (! pat)
10700         return 0;
10701       emit_insn (pat);
10702       return target;
10703
10704     case ALTIVEC_BUILTIN_MTVSCR:
10705       icode = CODE_FOR_altivec_mtvscr;
10706       arg0 = CALL_EXPR_ARG (exp, 0);
10707       op0 = expand_normal (arg0);
10708       mode0 = insn_data[icode].operand[0].mode;
10709
10710       /* If we got invalid arguments bail out before generating bad rtl.  */
10711       if (arg0 == error_mark_node)
10712         return const0_rtx;
10713
10714       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10715         op0 = copy_to_mode_reg (mode0, op0);
10716
10717       pat = GEN_FCN (icode) (op0);
10718       if (pat)
10719         emit_insn (pat);
10720       return NULL_RTX;
10721
10722     case ALTIVEC_BUILTIN_DSSALL:
10723       emit_insn (gen_altivec_dssall ());
10724       return NULL_RTX;
10725
10726     case ALTIVEC_BUILTIN_DSS:
10727       icode = CODE_FOR_altivec_dss;
10728       arg0 = CALL_EXPR_ARG (exp, 0);
10729       STRIP_NOPS (arg0);
10730       op0 = expand_normal (arg0);
10731       mode0 = insn_data[icode].operand[0].mode;
10732
10733       /* If we got invalid arguments bail out before generating bad rtl.  */
10734       if (arg0 == error_mark_node)
10735         return const0_rtx;
10736
10737       if (TREE_CODE (arg0) != INTEGER_CST
10738           || TREE_INT_CST_LOW (arg0) & ~0x3)
10739         {
10740           error ("argument to dss must be a 2-bit unsigned literal");
10741           return const0_rtx;
10742         }
10743
10744       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10745         op0 = copy_to_mode_reg (mode0, op0);
10746
10747       emit_insn (gen_altivec_dss (op0));
10748       return NULL_RTX;
10749
10750     case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
10751     case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
10752     case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
10753     case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
10754     case VSX_BUILTIN_VEC_INIT_V2DF:
10755     case VSX_BUILTIN_VEC_INIT_V2DI:
10756       return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
10757
10758     case ALTIVEC_BUILTIN_VEC_SET_V4SI:
10759     case ALTIVEC_BUILTIN_VEC_SET_V8HI:
10760     case ALTIVEC_BUILTIN_VEC_SET_V16QI:
10761     case ALTIVEC_BUILTIN_VEC_SET_V4SF:
10762     case VSX_BUILTIN_VEC_SET_V2DF:
10763     case VSX_BUILTIN_VEC_SET_V2DI:
10764       return altivec_expand_vec_set_builtin (exp);
10765
10766     case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
10767     case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
10768     case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
10769     case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
10770     case VSX_BUILTIN_VEC_EXT_V2DF:
10771     case VSX_BUILTIN_VEC_EXT_V2DI:
10772       return altivec_expand_vec_ext_builtin (exp, target);
10773
10774     default:
10775       break;
10776       /* Fall through.  */
10777     }
10778
10779   /* Expand abs* operations.  */
10780   d = bdesc_abs;
10781   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
10782     if (d->code == fcode)
10783       return altivec_expand_abs_builtin (d->icode, exp, target);
10784
10785   /* Expand the AltiVec predicates.  */
10786   d = bdesc_altivec_preds;
10787   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
10788     if (d->code == fcode)
10789       return altivec_expand_predicate_builtin (d->icode, exp, target);
10790
10791   /* LV* are funky.  We initialized them differently.  */
10792   switch (fcode)
10793     {
10794     case ALTIVEC_BUILTIN_LVSL:
10795       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
10796                                         exp, target, false);
10797     case ALTIVEC_BUILTIN_LVSR:
10798       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
10799                                         exp, target, false);
10800     case ALTIVEC_BUILTIN_LVEBX:
10801       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
10802                                         exp, target, false);
10803     case ALTIVEC_BUILTIN_LVEHX:
10804       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
10805                                         exp, target, false);
10806     case ALTIVEC_BUILTIN_LVEWX:
10807       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
10808                                         exp, target, false);
10809     case ALTIVEC_BUILTIN_LVXL:
10810       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
10811                                         exp, target, false);
10812     case ALTIVEC_BUILTIN_LVX:
10813       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
10814                                         exp, target, false);
10815     case ALTIVEC_BUILTIN_LVLX:
10816       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
10817                                         exp, target, true);
10818     case ALTIVEC_BUILTIN_LVLXL:
10819       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
10820                                         exp, target, true);
10821     case ALTIVEC_BUILTIN_LVRX:
10822       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
10823                                         exp, target, true);
10824     case ALTIVEC_BUILTIN_LVRXL:
10825       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
10826                                         exp, target, true);
10827     case VSX_BUILTIN_LXVD2X_V2DF:
10828       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
10829                                         exp, target, false);
10830     case VSX_BUILTIN_LXVD2X_V2DI:
10831       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
10832                                         exp, target, false);
10833     case VSX_BUILTIN_LXVW4X_V4SF:
10834       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
10835                                         exp, target, false);
10836     case VSX_BUILTIN_LXVW4X_V4SI:
10837       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
10838                                         exp, target, false);
10839     case VSX_BUILTIN_LXVW4X_V8HI:
10840       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
10841                                         exp, target, false);
10842     case VSX_BUILTIN_LXVW4X_V16QI:
10843       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
10844                                         exp, target, false);
10845       break;
10846     default:
10847       break;
10848       /* Fall through.  */
10849     }
10850
10851   *expandedp = false;
10852   return NULL_RTX;
10853 }
10854
10855 /* Expand the builtin in EXP and store the result in TARGET.  Store
10856    true in *EXPANDEDP if we found a builtin to expand.  */
10857 static rtx
10858 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
10859 {
10860   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10861   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10862   const struct builtin_description *d;
10863   size_t i;
10864
10865   *expandedp = true;
10866
10867   switch (fcode)
10868     {
10869     case PAIRED_BUILTIN_STX:
10870       return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
10871     case PAIRED_BUILTIN_LX:
10872       return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
10873     default:
10874       break;
10875       /* Fall through.  */
10876     }
10877
10878   /* Expand the paired predicates.  */
10879   d = bdesc_paired_preds;
10880   for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
10881     if (d->code == fcode)
10882       return paired_expand_predicate_builtin (d->icode, exp, target);
10883
10884   *expandedp = false;
10885   return NULL_RTX;
10886 }
10887
10888 /* Binops that need to be initialized manually, but can be expanded
10889    automagically by rs6000_expand_binop_builtin.  */
10890 static const struct builtin_description bdesc_2arg_spe[] =
10891 {
10892   { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
10893   { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
10894   { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
10895   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
10896   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
10897   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
10898   { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
10899   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
10900   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
10901   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
10902   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
10903   { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
10904   { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
10905   { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
10906   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
10907   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
10908   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
10909   { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
10910   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
10911   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
10912   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
10913   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
10914 };
10915
10916 /* Expand the builtin in EXP and store the result in TARGET.  Store
10917    true in *EXPANDEDP if we found a builtin to expand.
10918
10919    This expands the SPE builtins that are not simple unary and binary
10920    operations.  */
10921 static rtx
10922 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
10923 {
10924   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10925   tree arg1, arg0;
10926   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10927   enum insn_code icode;
10928   enum machine_mode tmode, mode0;
10929   rtx pat, op0;
10930   const struct builtin_description *d;
10931   size_t i;
10932
10933   *expandedp = true;
10934
10935   /* Syntax check for a 5-bit unsigned immediate.  */
10936   switch (fcode)
10937     {
10938     case SPE_BUILTIN_EVSTDD:
10939     case SPE_BUILTIN_EVSTDH:
10940     case SPE_BUILTIN_EVSTDW:
10941     case SPE_BUILTIN_EVSTWHE:
10942     case SPE_BUILTIN_EVSTWHO:
10943     case SPE_BUILTIN_EVSTWWE:
10944     case SPE_BUILTIN_EVSTWWO:
10945       arg1 = CALL_EXPR_ARG (exp, 2);
10946       if (TREE_CODE (arg1) != INTEGER_CST
10947           || TREE_INT_CST_LOW (arg1) & ~0x1f)
10948         {
10949           error ("argument 2 must be a 5-bit unsigned literal");
10950           return const0_rtx;
10951         }
10952       break;
10953     default:
10954       break;
10955     }
10956
10957   /* The evsplat*i instructions are not quite generic.  */
10958   switch (fcode)
10959     {
10960     case SPE_BUILTIN_EVSPLATFI:
10961       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
10962                                          exp, target);
10963     case SPE_BUILTIN_EVSPLATI:
10964       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
10965                                          exp, target);
10966     default:
10967       break;
10968     }
10969
10970   d = bdesc_2arg_spe;
10971   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
10972     if (d->code == fcode)
10973       return rs6000_expand_binop_builtin (d->icode, exp, target);
10974
10975   d = bdesc_spe_predicates;
10976   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
10977     if (d->code == fcode)
10978       return spe_expand_predicate_builtin (d->icode, exp, target);
10979
10980   d = bdesc_spe_evsel;
10981   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
10982     if (d->code == fcode)
10983       return spe_expand_evsel_builtin (d->icode, exp, target);
10984
10985   switch (fcode)
10986     {
10987     case SPE_BUILTIN_EVSTDDX:
10988       return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
10989     case SPE_BUILTIN_EVSTDHX:
10990       return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
10991     case SPE_BUILTIN_EVSTDWX:
10992       return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
10993     case SPE_BUILTIN_EVSTWHEX:
10994       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
10995     case SPE_BUILTIN_EVSTWHOX:
10996       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
10997     case SPE_BUILTIN_EVSTWWEX:
10998       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
10999     case SPE_BUILTIN_EVSTWWOX:
11000       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
11001     case SPE_BUILTIN_EVSTDD:
11002       return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
11003     case SPE_BUILTIN_EVSTDH:
11004       return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
11005     case SPE_BUILTIN_EVSTDW:
11006       return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
11007     case SPE_BUILTIN_EVSTWHE:
11008       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
11009     case SPE_BUILTIN_EVSTWHO:
11010       return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
11011     case SPE_BUILTIN_EVSTWWE:
11012       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
11013     case SPE_BUILTIN_EVSTWWO:
11014       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
11015     case SPE_BUILTIN_MFSPEFSCR:
11016       icode = CODE_FOR_spe_mfspefscr;
11017       tmode = insn_data[icode].operand[0].mode;
11018
11019       if (target == 0
11020           || GET_MODE (target) != tmode
11021           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11022         target = gen_reg_rtx (tmode);
11023
11024       pat = GEN_FCN (icode) (target);
11025       if (! pat)
11026         return 0;
11027       emit_insn (pat);
11028       return target;
11029     case SPE_BUILTIN_MTSPEFSCR:
11030       icode = CODE_FOR_spe_mtspefscr;
11031       arg0 = CALL_EXPR_ARG (exp, 0);
11032       op0 = expand_normal (arg0);
11033       mode0 = insn_data[icode].operand[0].mode;
11034
11035       if (arg0 == error_mark_node)
11036         return const0_rtx;
11037
11038       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11039         op0 = copy_to_mode_reg (mode0, op0);
11040
11041       pat = GEN_FCN (icode) (op0);
11042       if (pat)
11043         emit_insn (pat);
11044       return NULL_RTX;
11045     default:
11046       break;
11047     }
11048
11049   *expandedp = false;
11050   return NULL_RTX;
11051 }
11052
11053 static rtx
11054 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11055 {
11056   rtx pat, scratch, tmp;
11057   tree form = CALL_EXPR_ARG (exp, 0);
11058   tree arg0 = CALL_EXPR_ARG (exp, 1);
11059   tree arg1 = CALL_EXPR_ARG (exp, 2);
11060   rtx op0 = expand_normal (arg0);
11061   rtx op1 = expand_normal (arg1);
11062   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11063   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11064   int form_int;
11065   enum rtx_code code;
11066
11067   if (TREE_CODE (form) != INTEGER_CST)
11068     {
11069       error ("argument 1 of __builtin_paired_predicate must be a constant");
11070       return const0_rtx;
11071     }
11072   else
11073     form_int = TREE_INT_CST_LOW (form);
11074
11075   gcc_assert (mode0 == mode1);
11076
11077   if (arg0 == error_mark_node || arg1 == error_mark_node)
11078     return const0_rtx;
11079
11080   if (target == 0
11081       || GET_MODE (target) != SImode
11082       || !(*insn_data[icode].operand[0].predicate) (target, SImode))
11083     target = gen_reg_rtx (SImode);
11084   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
11085     op0 = copy_to_mode_reg (mode0, op0);
11086   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
11087     op1 = copy_to_mode_reg (mode1, op1);
11088
11089   scratch = gen_reg_rtx (CCFPmode);
11090
11091   pat = GEN_FCN (icode) (scratch, op0, op1);
11092   if (!pat)
11093     return const0_rtx;
11094
11095   emit_insn (pat);
11096
11097   switch (form_int)
11098     {
11099       /* LT bit.  */
11100     case 0:
11101       code = LT;
11102       break;
11103       /* GT bit.  */
11104     case 1:
11105       code = GT;
11106       break;
11107       /* EQ bit.  */
11108     case 2:
11109       code = EQ;
11110       break;
11111       /* UN bit.  */
11112     case 3:
11113       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11114       return target;
11115     default:
11116       error ("argument 1 of __builtin_paired_predicate is out of range");
11117       return const0_rtx;
11118     }
11119
11120   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11121   emit_move_insn (target, tmp);
11122   return target;
11123 }
11124
11125 static rtx
11126 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11127 {
11128   rtx pat, scratch, tmp;
11129   tree form = CALL_EXPR_ARG (exp, 0);
11130   tree arg0 = CALL_EXPR_ARG (exp, 1);
11131   tree arg1 = CALL_EXPR_ARG (exp, 2);
11132   rtx op0 = expand_normal (arg0);
11133   rtx op1 = expand_normal (arg1);
11134   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11135   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11136   int form_int;
11137   enum rtx_code code;
11138
11139   if (TREE_CODE (form) != INTEGER_CST)
11140     {
11141       error ("argument 1 of __builtin_spe_predicate must be a constant");
11142       return const0_rtx;
11143     }
11144   else
11145     form_int = TREE_INT_CST_LOW (form);
11146
11147   gcc_assert (mode0 == mode1);
11148
11149   if (arg0 == error_mark_node || arg1 == error_mark_node)
11150     return const0_rtx;
11151
11152   if (target == 0
11153       || GET_MODE (target) != SImode
11154       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
11155     target = gen_reg_rtx (SImode);
11156
11157   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11158     op0 = copy_to_mode_reg (mode0, op0);
11159   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11160     op1 = copy_to_mode_reg (mode1, op1);
11161
11162   scratch = gen_reg_rtx (CCmode);
11163
11164   pat = GEN_FCN (icode) (scratch, op0, op1);
11165   if (! pat)
11166     return const0_rtx;
11167   emit_insn (pat);
11168
11169   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
11170      _lower_.  We use one compare, but look in different bits of the
11171      CR for each variant.
11172
11173      There are 2 elements in each SPE simd type (upper/lower).  The CR
11174      bits are set as follows:
11175
11176      BIT0  | BIT 1  | BIT 2   | BIT 3
11177      U     |   L    | (U | L) | (U & L)
11178
11179      So, for an "all" relationship, BIT 3 would be set.
11180      For an "any" relationship, BIT 2 would be set.  Etc.
11181
11182      Following traditional nomenclature, these bits map to:
11183
11184      BIT0  | BIT 1  | BIT 2   | BIT 3
11185      LT    | GT     | EQ      | OV
11186
11187      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
11188   */
11189
11190   switch (form_int)
11191     {
11192       /* All variant.  OV bit.  */
11193     case 0:
11194       /* We need to get to the OV bit, which is the ORDERED bit.  We
11195          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
11196          that's ugly and will make validate_condition_mode die.
11197          So let's just use another pattern.  */
11198       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11199       return target;
11200       /* Any variant.  EQ bit.  */
11201     case 1:
11202       code = EQ;
11203       break;
11204       /* Upper variant.  LT bit.  */
11205     case 2:
11206       code = LT;
11207       break;
11208       /* Lower variant.  GT bit.  */
11209     case 3:
11210       code = GT;
11211       break;
11212     default:
11213       error ("argument 1 of __builtin_spe_predicate is out of range");
11214       return const0_rtx;
11215     }
11216
11217   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11218   emit_move_insn (target, tmp);
11219
11220   return target;
11221 }
11222
11223 /* The evsel builtins look like this:
11224
11225      e = __builtin_spe_evsel_OP (a, b, c, d);
11226
11227    and work like this:
11228
11229      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
11230      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
11231 */
11232
11233 static rtx
11234 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
11235 {
11236   rtx pat, scratch;
11237   tree arg0 = CALL_EXPR_ARG (exp, 0);
11238   tree arg1 = CALL_EXPR_ARG (exp, 1);
11239   tree arg2 = CALL_EXPR_ARG (exp, 2);
11240   tree arg3 = CALL_EXPR_ARG (exp, 3);
11241   rtx op0 = expand_normal (arg0);
11242   rtx op1 = expand_normal (arg1);
11243   rtx op2 = expand_normal (arg2);
11244   rtx op3 = expand_normal (arg3);
11245   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11246   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11247
11248   gcc_assert (mode0 == mode1);
11249
11250   if (arg0 == error_mark_node || arg1 == error_mark_node
11251       || arg2 == error_mark_node || arg3 == error_mark_node)
11252     return const0_rtx;
11253
11254   if (target == 0
11255       || GET_MODE (target) != mode0
11256       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
11257     target = gen_reg_rtx (mode0);
11258
11259   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11260     op0 = copy_to_mode_reg (mode0, op0);
11261   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11262     op1 = copy_to_mode_reg (mode0, op1);
11263   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
11264     op2 = copy_to_mode_reg (mode0, op2);
11265   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
11266     op3 = copy_to_mode_reg (mode0, op3);
11267
11268   /* Generate the compare.  */
11269   scratch = gen_reg_rtx (CCmode);
11270   pat = GEN_FCN (icode) (scratch, op0, op1);
11271   if (! pat)
11272     return const0_rtx;
11273   emit_insn (pat);
11274
11275   if (mode0 == V2SImode)
11276     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
11277   else
11278     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
11279
11280   return target;
11281 }
11282
11283 /* Raise an error message for a builtin function that is called without the
11284    appropriate target options being set.  */
11285
11286 static void
11287 rs6000_invalid_builtin (enum rs6000_builtins fncode)
11288 {
11289   size_t uns_fncode = (size_t)fncode;
11290   const char *name = rs6000_builtin_info[uns_fncode].name;
11291   unsigned fnmask = rs6000_builtin_info[uns_fncode].mask;
11292
11293   gcc_assert (name != NULL);
11294   if ((fnmask & RS6000_BTM_CELL) != 0)
11295     error ("Builtin function %s is only valid for the cell processor", name);
11296   else if ((fnmask & RS6000_BTM_VSX) != 0)
11297     error ("Builtin function %s requires the -mvsx option", name);
11298   else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
11299     error ("Builtin function %s requires the -maltivec option", name);
11300   else if ((fnmask & RS6000_BTM_PAIRED) != 0)
11301     error ("Builtin function %s requires the -mpaired option", name);
11302   else if ((fnmask & RS6000_BTM_SPE) != 0)
11303     error ("Builtin function %s requires the -mspe option", name);
11304   else
11305     error ("Builtin function %s is not supported with the current options",
11306            name);
11307 }
11308
11309 /* Expand an expression EXP that calls a built-in function,
11310    with result going to TARGET if that's convenient
11311    (and in mode MODE if that's convenient).
11312    SUBTARGET may be used as the target for computing one of EXP's operands.
11313    IGNORE is nonzero if the value is to be ignored.  */
11314
11315 static rtx
11316 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
11317                        enum machine_mode mode ATTRIBUTE_UNUSED,
11318                        int ignore ATTRIBUTE_UNUSED)
11319 {
11320   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11321   enum rs6000_builtins fcode
11322     = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
11323   size_t uns_fcode = (size_t)fcode;
11324   const struct builtin_description *d;
11325   size_t i;
11326   rtx ret;
11327   bool success;
11328   unsigned mask = rs6000_builtin_info[uns_fcode].mask;
11329   bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
11330
11331   if (TARGET_DEBUG_BUILTIN)
11332     {
11333       enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
11334       const char *name1 = rs6000_builtin_info[uns_fcode].name;
11335       const char *name2 = ((icode != CODE_FOR_nothing)
11336                            ? get_insn_name ((int)icode)
11337                            : "nothing");
11338       const char *name3;
11339
11340       switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
11341         {
11342         default:                   name3 = "unknown";   break;
11343         case RS6000_BTC_SPECIAL:   name3 = "special";   break;
11344         case RS6000_BTC_UNARY:     name3 = "unary";     break;
11345         case RS6000_BTC_BINARY:    name3 = "binary";    break;
11346         case RS6000_BTC_TERNARY:   name3 = "ternary";   break;
11347         case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
11348         case RS6000_BTC_ABS:       name3 = "abs";       break;
11349         case RS6000_BTC_EVSEL:     name3 = "evsel";     break;
11350         case RS6000_BTC_DST:       name3 = "dst";       break;
11351         }
11352
11353
11354       fprintf (stderr,
11355                "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
11356                (name1) ? name1 : "---", fcode,
11357                (name2) ? name2 : "---", (int)icode,
11358                name3,
11359                func_valid_p ? "" : ", not valid");
11360     }        
11361
11362   if (!func_valid_p)
11363     {
11364       rs6000_invalid_builtin (fcode);
11365
11366       /* Given it is invalid, just generate a normal call.  */
11367       return expand_call (exp, target, ignore);
11368     }
11369
11370   switch (fcode)
11371     {
11372     case RS6000_BUILTIN_RECIP:
11373       return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
11374
11375     case RS6000_BUILTIN_RECIPF:
11376       return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
11377
11378     case RS6000_BUILTIN_RSQRTF:
11379       return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
11380
11381     case RS6000_BUILTIN_RSQRT:
11382       return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
11383
11384     case RS6000_BUILTIN_BSWAP_HI:
11385       return rs6000_expand_unop_builtin (CODE_FOR_bswaphi2, exp, target);
11386
11387     case POWER7_BUILTIN_BPERMD:
11388       return rs6000_expand_binop_builtin (((TARGET_64BIT)
11389                                            ? CODE_FOR_bpermd_di
11390                                            : CODE_FOR_bpermd_si), exp, target);
11391
11392     case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
11393     case ALTIVEC_BUILTIN_MASK_FOR_STORE:
11394       {
11395         int icode = (int) CODE_FOR_altivec_lvsr;
11396         enum machine_mode tmode = insn_data[icode].operand[0].mode;
11397         enum machine_mode mode = insn_data[icode].operand[1].mode;
11398         tree arg;
11399         rtx op, addr, pat;
11400
11401         gcc_assert (TARGET_ALTIVEC);
11402
11403         arg = CALL_EXPR_ARG (exp, 0);
11404         gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
11405         op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
11406         addr = memory_address (mode, op);
11407         if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
11408           op = addr;
11409         else
11410           {
11411             /* For the load case need to negate the address.  */
11412             op = gen_reg_rtx (GET_MODE (addr));
11413             emit_insn (gen_rtx_SET (VOIDmode, op,
11414                                     gen_rtx_NEG (GET_MODE (addr), addr)));
11415           }
11416         op = gen_rtx_MEM (mode, op);
11417
11418         if (target == 0
11419             || GET_MODE (target) != tmode
11420             || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11421           target = gen_reg_rtx (tmode);
11422
11423         /*pat = gen_altivec_lvsr (target, op);*/
11424         pat = GEN_FCN (icode) (target, op);
11425         if (!pat)
11426           return 0;
11427         emit_insn (pat);
11428
11429         return target;
11430       }
11431
11432     case ALTIVEC_BUILTIN_VCFUX:
11433     case ALTIVEC_BUILTIN_VCFSX:
11434     case ALTIVEC_BUILTIN_VCTUXS:
11435     case ALTIVEC_BUILTIN_VCTSXS:
11436   /* FIXME: There's got to be a nicer way to handle this case than
11437      constructing a new CALL_EXPR.  */
11438       if (call_expr_nargs (exp) == 1)
11439         {
11440           exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
11441                                  2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
11442         }
11443       break;
11444
11445     default:
11446       break;
11447     }
11448
11449   if (TARGET_ALTIVEC)
11450     {
11451       ret = altivec_expand_builtin (exp, target, &success);
11452
11453       if (success)
11454         return ret;
11455     }
11456   if (TARGET_SPE)
11457     {
11458       ret = spe_expand_builtin (exp, target, &success);
11459
11460       if (success)
11461         return ret;
11462     }
11463   if (TARGET_PAIRED_FLOAT)
11464     {
11465       ret = paired_expand_builtin (exp, target, &success);
11466
11467       if (success)
11468         return ret;
11469     }  
11470
11471   gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
11472
11473   /* Handle simple unary operations.  */
11474   d = bdesc_1arg;
11475   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
11476     if (d->code == fcode)
11477       return rs6000_expand_unop_builtin (d->icode, exp, target);
11478
11479   /* Handle simple binary operations.  */
11480   d = bdesc_2arg;
11481   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11482     if (d->code == fcode)
11483       return rs6000_expand_binop_builtin (d->icode, exp, target);
11484
11485   /* Handle simple ternary operations.  */
11486   d = bdesc_3arg;
11487   for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
11488     if (d->code == fcode)
11489       return rs6000_expand_ternop_builtin (d->icode, exp, target);
11490
11491   gcc_unreachable ();
11492 }
11493
11494 static void
11495 rs6000_init_builtins (void)
11496 {
11497   tree tdecl;
11498   tree ftype;
11499   enum machine_mode mode;
11500
11501   if (TARGET_DEBUG_BUILTIN)
11502     fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
11503              (TARGET_PAIRED_FLOAT) ? ", paired"  : "",
11504              (TARGET_SPE)          ? ", spe"     : "",
11505              (TARGET_ALTIVEC)      ? ", altivec" : "",
11506              (TARGET_VSX)          ? ", vsx"     : "");
11507
11508   V2SI_type_node = build_vector_type (intSI_type_node, 2);
11509   V2SF_type_node = build_vector_type (float_type_node, 2);
11510   V2DI_type_node = build_vector_type (intDI_type_node, 2);
11511   V2DF_type_node = build_vector_type (double_type_node, 2);
11512   V4HI_type_node = build_vector_type (intHI_type_node, 4);
11513   V4SI_type_node = build_vector_type (intSI_type_node, 4);
11514   V4SF_type_node = build_vector_type (float_type_node, 4);
11515   V8HI_type_node = build_vector_type (intHI_type_node, 8);
11516   V16QI_type_node = build_vector_type (intQI_type_node, 16);
11517
11518   unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
11519   unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
11520   unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
11521   unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
11522
11523   opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
11524   opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
11525   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
11526   opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
11527
11528   /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
11529      types, especially in C++ land.  Similarly, 'vector pixel' is distinct from
11530      'vector unsigned short'.  */
11531
11532   bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
11533   bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11534   bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
11535   bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
11536   pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11537
11538   long_integer_type_internal_node = long_integer_type_node;
11539   long_unsigned_type_internal_node = long_unsigned_type_node;
11540   long_long_integer_type_internal_node = long_long_integer_type_node;
11541   long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
11542   intQI_type_internal_node = intQI_type_node;
11543   uintQI_type_internal_node = unsigned_intQI_type_node;
11544   intHI_type_internal_node = intHI_type_node;
11545   uintHI_type_internal_node = unsigned_intHI_type_node;
11546   intSI_type_internal_node = intSI_type_node;
11547   uintSI_type_internal_node = unsigned_intSI_type_node;
11548   intDI_type_internal_node = intDI_type_node;
11549   uintDI_type_internal_node = unsigned_intDI_type_node;
11550   float_type_internal_node = float_type_node;
11551   double_type_internal_node = double_type_node;
11552   void_type_internal_node = void_type_node;
11553
11554   /* Initialize the modes for builtin_function_type, mapping a machine mode to
11555      tree type node.  */
11556   builtin_mode_to_type[QImode][0] = integer_type_node;
11557   builtin_mode_to_type[HImode][0] = integer_type_node;
11558   builtin_mode_to_type[SImode][0] = intSI_type_node;
11559   builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
11560   builtin_mode_to_type[DImode][0] = intDI_type_node;
11561   builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
11562   builtin_mode_to_type[SFmode][0] = float_type_node;
11563   builtin_mode_to_type[DFmode][0] = double_type_node;
11564   builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
11565   builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
11566   builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
11567   builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
11568   builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
11569   builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
11570   builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
11571   builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
11572   builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
11573   builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
11574   builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
11575   builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
11576   builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
11577
11578   tdecl = add_builtin_type ("__bool char", bool_char_type_node);
11579   TYPE_NAME (bool_char_type_node) = tdecl;
11580
11581   tdecl = add_builtin_type ("__bool short", bool_short_type_node);
11582   TYPE_NAME (bool_short_type_node) = tdecl;
11583
11584   tdecl = add_builtin_type ("__bool int", bool_int_type_node);
11585   TYPE_NAME (bool_int_type_node) = tdecl;
11586
11587   tdecl = add_builtin_type ("__pixel", pixel_type_node);
11588   TYPE_NAME (pixel_type_node) = tdecl;
11589
11590   bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
11591   bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
11592   bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
11593   bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
11594   pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
11595
11596   tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
11597   TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
11598
11599   tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
11600   TYPE_NAME (V16QI_type_node) = tdecl;
11601
11602   tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
11603   TYPE_NAME ( bool_V16QI_type_node) = tdecl;
11604
11605   tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
11606   TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
11607
11608   tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
11609   TYPE_NAME (V8HI_type_node) = tdecl;
11610
11611   tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
11612   TYPE_NAME (bool_V8HI_type_node) = tdecl;
11613
11614   tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
11615   TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
11616
11617   tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
11618   TYPE_NAME (V4SI_type_node) = tdecl;
11619
11620   tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
11621   TYPE_NAME (bool_V4SI_type_node) = tdecl;
11622
11623   tdecl = add_builtin_type ("__vector float", V4SF_type_node);
11624   TYPE_NAME (V4SF_type_node) = tdecl;
11625
11626   tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
11627   TYPE_NAME (pixel_V8HI_type_node) = tdecl;
11628
11629   tdecl = add_builtin_type ("__vector double", V2DF_type_node);
11630   TYPE_NAME (V2DF_type_node) = tdecl;
11631
11632   tdecl = add_builtin_type ("__vector long", V2DI_type_node);
11633   TYPE_NAME (V2DI_type_node) = tdecl;
11634
11635   tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
11636   TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
11637
11638   tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
11639   TYPE_NAME (bool_V2DI_type_node) = tdecl;
11640
11641   /* Paired and SPE builtins are only available if you build a compiler with
11642      the appropriate options, so only create those builtins with the
11643      appropriate compiler option.  Create Altivec and VSX builtins on machines
11644      with at least the general purpose extensions (970 and newer) to allow the
11645      use of the target attribute.  */
11646   if (TARGET_PAIRED_FLOAT)
11647     paired_init_builtins ();
11648   if (TARGET_SPE)
11649     spe_init_builtins ();
11650   if (TARGET_EXTRA_BUILTINS)
11651     altivec_init_builtins ();
11652   if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
11653     rs6000_common_init_builtins ();
11654
11655   ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
11656                                  RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
11657   def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
11658
11659   ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
11660                                  RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
11661   def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
11662
11663   ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
11664                                  RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
11665   def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
11666
11667   ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
11668                                  RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
11669   def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
11670
11671   mode = (TARGET_64BIT) ? DImode : SImode;
11672   ftype = builtin_function_type (mode, mode, mode, VOIDmode,
11673                                  POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
11674   def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
11675
11676       /* Don't use builtin_function_type here, as it maps HI/QI to SI.  */
11677   ftype = build_function_type_list (unsigned_intHI_type_node,
11678                                     unsigned_intHI_type_node,
11679                                     NULL_TREE);
11680   def_builtin ("__builtin_bswap16", ftype, RS6000_BUILTIN_BSWAP_HI);
11681
11682 #if TARGET_XCOFF
11683   /* AIX libm provides clog as __clog.  */
11684   if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
11685     set_user_assembler_name (tdecl, "__clog");
11686 #endif
11687
11688 #ifdef SUBTARGET_INIT_BUILTINS
11689   SUBTARGET_INIT_BUILTINS;
11690 #endif
11691 }
11692
11693 /* Returns the rs6000 builtin decl for CODE.  */
11694
11695 static tree
11696 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11697 {
11698   unsigned fnmask;
11699
11700   if (code >= RS6000_BUILTIN_COUNT)
11701     return error_mark_node;
11702
11703   fnmask = rs6000_builtin_info[code].mask;
11704   if ((fnmask & rs6000_builtin_mask) != fnmask)
11705     {
11706       rs6000_invalid_builtin ((enum rs6000_builtins)code);
11707       return error_mark_node;
11708     }
11709
11710   return rs6000_builtin_decls[code];
11711 }
11712
11713 static void
11714 spe_init_builtins (void)
11715 {
11716   tree puint_type_node = build_pointer_type (unsigned_type_node);
11717   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
11718   const struct builtin_description *d;
11719   size_t i;
11720
11721   tree v2si_ftype_4_v2si
11722     = build_function_type_list (opaque_V2SI_type_node,
11723                                 opaque_V2SI_type_node,
11724                                 opaque_V2SI_type_node,
11725                                 opaque_V2SI_type_node,
11726                                 opaque_V2SI_type_node,
11727                                 NULL_TREE);
11728
11729   tree v2sf_ftype_4_v2sf
11730     = build_function_type_list (opaque_V2SF_type_node,
11731                                 opaque_V2SF_type_node,
11732                                 opaque_V2SF_type_node,
11733                                 opaque_V2SF_type_node,
11734                                 opaque_V2SF_type_node,
11735                                 NULL_TREE);
11736
11737   tree int_ftype_int_v2si_v2si
11738     = build_function_type_list (integer_type_node,
11739                                 integer_type_node,
11740                                 opaque_V2SI_type_node,
11741                                 opaque_V2SI_type_node,
11742                                 NULL_TREE);
11743
11744   tree int_ftype_int_v2sf_v2sf
11745     = build_function_type_list (integer_type_node,
11746                                 integer_type_node,
11747                                 opaque_V2SF_type_node,
11748                                 opaque_V2SF_type_node,
11749                                 NULL_TREE);
11750
11751   tree void_ftype_v2si_puint_int
11752     = build_function_type_list (void_type_node,
11753                                 opaque_V2SI_type_node,
11754                                 puint_type_node,
11755                                 integer_type_node,
11756                                 NULL_TREE);
11757
11758   tree void_ftype_v2si_puint_char
11759     = build_function_type_list (void_type_node,
11760                                 opaque_V2SI_type_node,
11761                                 puint_type_node,
11762                                 char_type_node,
11763                                 NULL_TREE);
11764
11765   tree void_ftype_v2si_pv2si_int
11766     = build_function_type_list (void_type_node,
11767                                 opaque_V2SI_type_node,
11768                                 opaque_p_V2SI_type_node,
11769                                 integer_type_node,
11770                                 NULL_TREE);
11771
11772   tree void_ftype_v2si_pv2si_char
11773     = build_function_type_list (void_type_node,
11774                                 opaque_V2SI_type_node,
11775                                 opaque_p_V2SI_type_node,
11776                                 char_type_node,
11777                                 NULL_TREE);
11778
11779   tree void_ftype_int
11780     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
11781
11782   tree int_ftype_void
11783     = build_function_type_list (integer_type_node, NULL_TREE);
11784
11785   tree v2si_ftype_pv2si_int
11786     = build_function_type_list (opaque_V2SI_type_node,
11787                                 opaque_p_V2SI_type_node,
11788                                 integer_type_node,
11789                                 NULL_TREE);
11790
11791   tree v2si_ftype_puint_int
11792     = build_function_type_list (opaque_V2SI_type_node,
11793                                 puint_type_node,
11794                                 integer_type_node,
11795                                 NULL_TREE);
11796
11797   tree v2si_ftype_pushort_int
11798     = build_function_type_list (opaque_V2SI_type_node,
11799                                 pushort_type_node,
11800                                 integer_type_node,
11801                                 NULL_TREE);
11802
11803   tree v2si_ftype_signed_char
11804     = build_function_type_list (opaque_V2SI_type_node,
11805                                 signed_char_type_node,
11806                                 NULL_TREE);
11807
11808   add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
11809
11810   /* Initialize irregular SPE builtins.  */
11811
11812   def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
11813   def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
11814   def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
11815   def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
11816   def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
11817   def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
11818   def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
11819   def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
11820   def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
11821   def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
11822   def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
11823   def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
11824   def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
11825   def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
11826   def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
11827   def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
11828   def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
11829   def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
11830
11831   /* Loads.  */
11832   def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
11833   def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
11834   def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
11835   def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
11836   def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
11837   def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
11838   def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
11839   def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
11840   def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
11841   def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
11842   def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
11843   def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
11844   def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
11845   def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
11846   def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
11847   def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
11848   def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
11849   def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
11850   def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
11851   def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
11852   def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
11853   def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
11854
11855   /* Predicates.  */
11856   d = bdesc_spe_predicates;
11857   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
11858     {
11859       tree type;
11860
11861       switch (insn_data[d->icode].operand[1].mode)
11862         {
11863         case V2SImode:
11864           type = int_ftype_int_v2si_v2si;
11865           break;
11866         case V2SFmode:
11867           type = int_ftype_int_v2sf_v2sf;
11868           break;
11869         default:
11870           gcc_unreachable ();
11871         }
11872
11873       def_builtin (d->name, type, d->code);
11874     }
11875
11876   /* Evsel predicates.  */
11877   d = bdesc_spe_evsel;
11878   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
11879     {
11880       tree type;
11881
11882       switch (insn_data[d->icode].operand[1].mode)
11883         {
11884         case V2SImode:
11885           type = v2si_ftype_4_v2si;
11886           break;
11887         case V2SFmode:
11888           type = v2sf_ftype_4_v2sf;
11889           break;
11890         default:
11891           gcc_unreachable ();
11892         }
11893
11894       def_builtin (d->name, type, d->code);
11895     }
11896 }
11897
11898 static void
11899 paired_init_builtins (void)
11900 {
11901   const struct builtin_description *d;
11902   size_t i;
11903
11904    tree int_ftype_int_v2sf_v2sf
11905     = build_function_type_list (integer_type_node,
11906                                 integer_type_node,
11907                                 V2SF_type_node,
11908                                 V2SF_type_node,
11909                                 NULL_TREE);
11910   tree pcfloat_type_node =
11911     build_pointer_type (build_qualified_type
11912                         (float_type_node, TYPE_QUAL_CONST));
11913
11914   tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
11915                                                            long_integer_type_node,
11916                                                            pcfloat_type_node,
11917                                                            NULL_TREE);
11918   tree void_ftype_v2sf_long_pcfloat =
11919     build_function_type_list (void_type_node,
11920                               V2SF_type_node,
11921                               long_integer_type_node,
11922                               pcfloat_type_node,
11923                               NULL_TREE);
11924
11925
11926   def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
11927                PAIRED_BUILTIN_LX);
11928
11929
11930   def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
11931                PAIRED_BUILTIN_STX);
11932
11933   /* Predicates.  */
11934   d = bdesc_paired_preds;
11935   for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
11936     {
11937       tree type;
11938
11939       if (TARGET_DEBUG_BUILTIN)
11940         fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
11941                  (int)i, get_insn_name (d->icode), (int)d->icode,
11942                  GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
11943
11944       switch (insn_data[d->icode].operand[1].mode)
11945         {
11946         case V2SFmode:
11947           type = int_ftype_int_v2sf_v2sf;
11948           break;
11949         default:
11950           gcc_unreachable ();
11951         }
11952
11953       def_builtin (d->name, type, d->code);
11954     }
11955 }
11956
11957 static void
11958 altivec_init_builtins (void)
11959 {
11960   const struct builtin_description *d;
11961   size_t i;
11962   tree ftype;
11963   tree decl;
11964
11965   tree pvoid_type_node = build_pointer_type (void_type_node);
11966
11967   tree pcvoid_type_node
11968     = build_pointer_type (build_qualified_type (void_type_node,
11969                                                 TYPE_QUAL_CONST));
11970
11971   tree int_ftype_opaque
11972     = build_function_type_list (integer_type_node,
11973                                 opaque_V4SI_type_node, NULL_TREE);
11974   tree opaque_ftype_opaque
11975     = build_function_type_list (integer_type_node, NULL_TREE);
11976   tree opaque_ftype_opaque_int
11977     = build_function_type_list (opaque_V4SI_type_node,
11978                                 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
11979   tree opaque_ftype_opaque_opaque_int
11980     = build_function_type_list (opaque_V4SI_type_node,
11981                                 opaque_V4SI_type_node, opaque_V4SI_type_node,
11982                                 integer_type_node, NULL_TREE);
11983   tree int_ftype_int_opaque_opaque
11984     = build_function_type_list (integer_type_node,
11985                                 integer_type_node, opaque_V4SI_type_node,
11986                                 opaque_V4SI_type_node, NULL_TREE);
11987   tree int_ftype_int_v4si_v4si
11988     = build_function_type_list (integer_type_node,
11989                                 integer_type_node, V4SI_type_node,
11990                                 V4SI_type_node, NULL_TREE);
11991   tree void_ftype_v4si
11992     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
11993   tree v8hi_ftype_void
11994     = build_function_type_list (V8HI_type_node, NULL_TREE);
11995   tree void_ftype_void
11996     = build_function_type_list (void_type_node, NULL_TREE);
11997   tree void_ftype_int
11998     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
11999
12000   tree opaque_ftype_long_pcvoid
12001     = build_function_type_list (opaque_V4SI_type_node,
12002                                 long_integer_type_node, pcvoid_type_node,
12003                                 NULL_TREE);
12004   tree v16qi_ftype_long_pcvoid
12005     = build_function_type_list (V16QI_type_node,
12006                                 long_integer_type_node, pcvoid_type_node,
12007                                 NULL_TREE);
12008   tree v8hi_ftype_long_pcvoid
12009     = build_function_type_list (V8HI_type_node,
12010                                 long_integer_type_node, pcvoid_type_node,
12011                                 NULL_TREE);
12012   tree v4si_ftype_long_pcvoid
12013     = build_function_type_list (V4SI_type_node,
12014                                 long_integer_type_node, pcvoid_type_node,
12015                                 NULL_TREE);
12016   tree v4sf_ftype_long_pcvoid
12017     = build_function_type_list (V4SF_type_node,
12018                                 long_integer_type_node, pcvoid_type_node,
12019                                 NULL_TREE);
12020   tree v2df_ftype_long_pcvoid
12021     = build_function_type_list (V2DF_type_node,
12022                                 long_integer_type_node, pcvoid_type_node,
12023                                 NULL_TREE);
12024   tree v2di_ftype_long_pcvoid
12025     = build_function_type_list (V2DI_type_node,
12026                                 long_integer_type_node, pcvoid_type_node,
12027                                 NULL_TREE);
12028
12029   tree void_ftype_opaque_long_pvoid
12030     = build_function_type_list (void_type_node,
12031                                 opaque_V4SI_type_node, long_integer_type_node,
12032                                 pvoid_type_node, NULL_TREE);
12033   tree void_ftype_v4si_long_pvoid
12034     = build_function_type_list (void_type_node,
12035                                 V4SI_type_node, long_integer_type_node,
12036                                 pvoid_type_node, NULL_TREE);
12037   tree void_ftype_v16qi_long_pvoid
12038     = build_function_type_list (void_type_node,
12039                                 V16QI_type_node, long_integer_type_node,
12040                                 pvoid_type_node, NULL_TREE);
12041   tree void_ftype_v8hi_long_pvoid
12042     = build_function_type_list (void_type_node,
12043                                 V8HI_type_node, long_integer_type_node,
12044                                 pvoid_type_node, NULL_TREE);
12045   tree void_ftype_v4sf_long_pvoid
12046     = build_function_type_list (void_type_node,
12047                                 V4SF_type_node, long_integer_type_node,
12048                                 pvoid_type_node, NULL_TREE);
12049   tree void_ftype_v2df_long_pvoid
12050     = build_function_type_list (void_type_node,
12051                                 V2DF_type_node, long_integer_type_node,
12052                                 pvoid_type_node, NULL_TREE);
12053   tree void_ftype_v2di_long_pvoid
12054     = build_function_type_list (void_type_node,
12055                                 V2DI_type_node, long_integer_type_node,
12056                                 pvoid_type_node, NULL_TREE);
12057   tree int_ftype_int_v8hi_v8hi
12058     = build_function_type_list (integer_type_node,
12059                                 integer_type_node, V8HI_type_node,
12060                                 V8HI_type_node, NULL_TREE);
12061   tree int_ftype_int_v16qi_v16qi
12062     = build_function_type_list (integer_type_node,
12063                                 integer_type_node, V16QI_type_node,
12064                                 V16QI_type_node, NULL_TREE);
12065   tree int_ftype_int_v4sf_v4sf
12066     = build_function_type_list (integer_type_node,
12067                                 integer_type_node, V4SF_type_node,
12068                                 V4SF_type_node, NULL_TREE);
12069   tree int_ftype_int_v2df_v2df
12070     = build_function_type_list (integer_type_node,
12071                                 integer_type_node, V2DF_type_node,
12072                                 V2DF_type_node, NULL_TREE);
12073   tree v4si_ftype_v4si
12074     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
12075   tree v8hi_ftype_v8hi
12076     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
12077   tree v16qi_ftype_v16qi
12078     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
12079   tree v4sf_ftype_v4sf
12080     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
12081   tree v2df_ftype_v2df
12082     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
12083   tree void_ftype_pcvoid_int_int
12084     = build_function_type_list (void_type_node,
12085                                 pcvoid_type_node, integer_type_node,
12086                                 integer_type_node, NULL_TREE);
12087
12088   def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
12089   def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
12090   def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
12091   def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
12092   def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
12093   def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
12094   def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
12095   def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
12096   def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
12097   def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
12098   def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
12099   def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
12100   def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
12101   def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
12102   def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
12103   def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
12104   def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
12105   def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
12106   def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
12107   def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
12108   def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
12109   def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
12110   def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
12111   def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
12112   def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
12113   def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
12114   def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
12115   def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
12116   def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
12117   def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
12118
12119   def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
12120                VSX_BUILTIN_LXVD2X_V2DF);
12121   def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
12122                VSX_BUILTIN_LXVD2X_V2DI);
12123   def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
12124                VSX_BUILTIN_LXVW4X_V4SF);
12125   def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
12126                VSX_BUILTIN_LXVW4X_V4SI);
12127   def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
12128                VSX_BUILTIN_LXVW4X_V8HI);
12129   def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
12130                VSX_BUILTIN_LXVW4X_V16QI);
12131   def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
12132                VSX_BUILTIN_STXVD2X_V2DF);
12133   def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
12134                VSX_BUILTIN_STXVD2X_V2DI);
12135   def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
12136                VSX_BUILTIN_STXVW4X_V4SF);
12137   def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
12138                VSX_BUILTIN_STXVW4X_V4SI);
12139   def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
12140                VSX_BUILTIN_STXVW4X_V8HI);
12141   def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
12142                VSX_BUILTIN_STXVW4X_V16QI);
12143   def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
12144                VSX_BUILTIN_VEC_LD);
12145   def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
12146                VSX_BUILTIN_VEC_ST);
12147
12148   def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
12149   def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
12150   def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
12151
12152   def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
12153   def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
12154   def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
12155   def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
12156   def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
12157   def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
12158   def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
12159   def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
12160   def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
12161   def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
12162   def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
12163   def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
12164
12165   /* Cell builtins.  */
12166   def_builtin ("__builtin_altivec_lvlx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
12167   def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
12168   def_builtin ("__builtin_altivec_lvrx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
12169   def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
12170
12171   def_builtin ("__builtin_vec_lvlx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
12172   def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
12173   def_builtin ("__builtin_vec_lvrx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
12174   def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
12175
12176   def_builtin ("__builtin_altivec_stvlx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
12177   def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
12178   def_builtin ("__builtin_altivec_stvrx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
12179   def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
12180
12181   def_builtin ("__builtin_vec_stvlx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
12182   def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
12183   def_builtin ("__builtin_vec_stvrx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
12184   def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
12185
12186   /* Add the DST variants.  */
12187   d = bdesc_dst;
12188   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12189     def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
12190
12191   /* Initialize the predicates.  */
12192   d = bdesc_altivec_preds;
12193   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
12194     {
12195       enum machine_mode mode1;
12196       tree type;
12197
12198       if (rs6000_overloaded_builtin_p (d->code))
12199         mode1 = VOIDmode;
12200       else
12201         mode1 = insn_data[d->icode].operand[1].mode;
12202
12203       switch (mode1)
12204         {
12205         case VOIDmode:
12206           type = int_ftype_int_opaque_opaque;
12207           break;
12208         case V4SImode:
12209           type = int_ftype_int_v4si_v4si;
12210           break;
12211         case V8HImode:
12212           type = int_ftype_int_v8hi_v8hi;
12213           break;
12214         case V16QImode:
12215           type = int_ftype_int_v16qi_v16qi;
12216           break;
12217         case V4SFmode:
12218           type = int_ftype_int_v4sf_v4sf;
12219           break;
12220         case V2DFmode:
12221           type = int_ftype_int_v2df_v2df;
12222           break;
12223         default:
12224           gcc_unreachable ();
12225         }
12226
12227       def_builtin (d->name, type, d->code);
12228     }
12229
12230   /* Initialize the abs* operators.  */
12231   d = bdesc_abs;
12232   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12233     {
12234       enum machine_mode mode0;
12235       tree type;
12236
12237       mode0 = insn_data[d->icode].operand[0].mode;
12238
12239       switch (mode0)
12240         {
12241         case V4SImode:
12242           type = v4si_ftype_v4si;
12243           break;
12244         case V8HImode:
12245           type = v8hi_ftype_v8hi;
12246           break;
12247         case V16QImode:
12248           type = v16qi_ftype_v16qi;
12249           break;
12250         case V4SFmode:
12251           type = v4sf_ftype_v4sf;
12252           break;
12253         case V2DFmode:
12254           type = v2df_ftype_v2df;
12255           break;
12256         default:
12257           gcc_unreachable ();
12258         }
12259
12260       def_builtin (d->name, type, d->code);
12261     }
12262
12263   /* Initialize target builtin that implements
12264      targetm.vectorize.builtin_mask_for_load.  */
12265
12266   decl = add_builtin_function ("__builtin_altivec_mask_for_load",
12267                                v16qi_ftype_long_pcvoid,
12268                                ALTIVEC_BUILTIN_MASK_FOR_LOAD,
12269                                BUILT_IN_MD, NULL, NULL_TREE);
12270   TREE_READONLY (decl) = 1;
12271   /* Record the decl. Will be used by rs6000_builtin_mask_for_load.  */
12272   altivec_builtin_mask_for_load = decl;
12273
12274   /* Access to the vec_init patterns.  */
12275   ftype = build_function_type_list (V4SI_type_node, integer_type_node,
12276                                     integer_type_node, integer_type_node,
12277                                     integer_type_node, NULL_TREE);
12278   def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
12279
12280   ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
12281                                     short_integer_type_node,
12282                                     short_integer_type_node,
12283                                     short_integer_type_node,
12284                                     short_integer_type_node,
12285                                     short_integer_type_node,
12286                                     short_integer_type_node,
12287                                     short_integer_type_node, NULL_TREE);
12288   def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
12289
12290   ftype = build_function_type_list (V16QI_type_node, char_type_node,
12291                                     char_type_node, char_type_node,
12292                                     char_type_node, char_type_node,
12293                                     char_type_node, char_type_node,
12294                                     char_type_node, char_type_node,
12295                                     char_type_node, char_type_node,
12296                                     char_type_node, char_type_node,
12297                                     char_type_node, char_type_node,
12298                                     char_type_node, NULL_TREE);
12299   def_builtin ("__builtin_vec_init_v16qi", ftype,
12300                ALTIVEC_BUILTIN_VEC_INIT_V16QI);
12301
12302   ftype = build_function_type_list (V4SF_type_node, float_type_node,
12303                                     float_type_node, float_type_node,
12304                                     float_type_node, NULL_TREE);
12305   def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
12306
12307   /* VSX builtins.  */
12308   ftype = build_function_type_list (V2DF_type_node, double_type_node,
12309                                     double_type_node, NULL_TREE);
12310   def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
12311
12312   ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
12313                                     intDI_type_node, NULL_TREE);
12314   def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
12315
12316   /* Access to the vec_set patterns.  */
12317   ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
12318                                     intSI_type_node,
12319                                     integer_type_node, NULL_TREE);
12320   def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
12321
12322   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
12323                                     intHI_type_node,
12324                                     integer_type_node, NULL_TREE);
12325   def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
12326
12327   ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
12328                                     intQI_type_node,
12329                                     integer_type_node, NULL_TREE);
12330   def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
12331
12332   ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
12333                                     float_type_node,
12334                                     integer_type_node, NULL_TREE);
12335   def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
12336
12337   ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
12338                                     double_type_node,
12339                                     integer_type_node, NULL_TREE);
12340   def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
12341
12342   ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
12343                                     intDI_type_node,
12344                                     integer_type_node, NULL_TREE);
12345   def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
12346
12347   /* Access to the vec_extract patterns.  */
12348   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
12349                                     integer_type_node, NULL_TREE);
12350   def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
12351
12352   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
12353                                     integer_type_node, NULL_TREE);
12354   def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
12355
12356   ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
12357                                     integer_type_node, NULL_TREE);
12358   def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
12359
12360   ftype = build_function_type_list (float_type_node, V4SF_type_node,
12361                                     integer_type_node, NULL_TREE);
12362   def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
12363
12364   ftype = build_function_type_list (double_type_node, V2DF_type_node,
12365                                     integer_type_node, NULL_TREE);
12366   def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
12367
12368   ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
12369                                     integer_type_node, NULL_TREE);
12370   def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
12371 }
12372
12373 /* Hash function for builtin functions with up to 3 arguments and a return
12374    type.  */
12375 static unsigned
12376 builtin_hash_function (const void *hash_entry)
12377 {
12378   unsigned ret = 0;
12379   int i;
12380   const struct builtin_hash_struct *bh =
12381     (const struct builtin_hash_struct *) hash_entry;
12382
12383   for (i = 0; i < 4; i++)
12384     {
12385       ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
12386       ret = (ret * 2) + bh->uns_p[i];
12387     }
12388
12389   return ret;
12390 }
12391
12392 /* Compare builtin hash entries H1 and H2 for equivalence.  */
12393 static int
12394 builtin_hash_eq (const void *h1, const void *h2)
12395 {
12396   const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
12397   const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
12398
12399   return ((p1->mode[0] == p2->mode[0])
12400           && (p1->mode[1] == p2->mode[1])
12401           && (p1->mode[2] == p2->mode[2])
12402           && (p1->mode[3] == p2->mode[3])
12403           && (p1->uns_p[0] == p2->uns_p[0])
12404           && (p1->uns_p[1] == p2->uns_p[1])
12405           && (p1->uns_p[2] == p2->uns_p[2])
12406           && (p1->uns_p[3] == p2->uns_p[3]));
12407 }
12408
12409 /* Map types for builtin functions with an explicit return type and up to 3
12410    arguments.  Functions with fewer than 3 arguments use VOIDmode as the type
12411    of the argument.  */
12412 static tree
12413 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
12414                        enum machine_mode mode_arg1, enum machine_mode mode_arg2,
12415                        enum rs6000_builtins builtin, const char *name)
12416 {
12417   struct builtin_hash_struct h;
12418   struct builtin_hash_struct *h2;
12419   void **found;
12420   int num_args = 3;
12421   int i;
12422   tree ret_type = NULL_TREE;
12423   tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
12424
12425   /* Create builtin_hash_table.  */
12426   if (builtin_hash_table == NULL)
12427     builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
12428                                           builtin_hash_eq, NULL);
12429
12430   h.type = NULL_TREE;
12431   h.mode[0] = mode_ret;
12432   h.mode[1] = mode_arg0;
12433   h.mode[2] = mode_arg1;
12434   h.mode[3] = mode_arg2;
12435   h.uns_p[0] = 0;
12436   h.uns_p[1] = 0;
12437   h.uns_p[2] = 0;
12438   h.uns_p[3] = 0;
12439
12440   /* If the builtin is a type that produces unsigned results or takes unsigned
12441      arguments, and it is returned as a decl for the vectorizer (such as
12442      widening multiplies, permute), make sure the arguments and return value
12443      are type correct.  */
12444   switch (builtin)
12445     {
12446       /* unsigned 2 argument functions.  */
12447     case ALTIVEC_BUILTIN_VMULEUB_UNS:
12448     case ALTIVEC_BUILTIN_VMULEUH_UNS:
12449     case ALTIVEC_BUILTIN_VMULOUB_UNS:
12450     case ALTIVEC_BUILTIN_VMULOUH_UNS:
12451       h.uns_p[0] = 1;
12452       h.uns_p[1] = 1;
12453       h.uns_p[2] = 1;
12454       break;
12455
12456       /* unsigned 3 argument functions.  */
12457     case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
12458     case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
12459     case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
12460     case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
12461     case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
12462     case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
12463     case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
12464     case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
12465     case VSX_BUILTIN_VPERM_16QI_UNS:
12466     case VSX_BUILTIN_VPERM_8HI_UNS:
12467     case VSX_BUILTIN_VPERM_4SI_UNS:
12468     case VSX_BUILTIN_VPERM_2DI_UNS:
12469     case VSX_BUILTIN_XXSEL_16QI_UNS:
12470     case VSX_BUILTIN_XXSEL_8HI_UNS:
12471     case VSX_BUILTIN_XXSEL_4SI_UNS:
12472     case VSX_BUILTIN_XXSEL_2DI_UNS:
12473       h.uns_p[0] = 1;
12474       h.uns_p[1] = 1;
12475       h.uns_p[2] = 1;
12476       h.uns_p[3] = 1;
12477       break;
12478
12479       /* signed permute functions with unsigned char mask.  */
12480     case ALTIVEC_BUILTIN_VPERM_16QI:
12481     case ALTIVEC_BUILTIN_VPERM_8HI:
12482     case ALTIVEC_BUILTIN_VPERM_4SI:
12483     case ALTIVEC_BUILTIN_VPERM_4SF:
12484     case ALTIVEC_BUILTIN_VPERM_2DI:
12485     case ALTIVEC_BUILTIN_VPERM_2DF:
12486     case VSX_BUILTIN_VPERM_16QI:
12487     case VSX_BUILTIN_VPERM_8HI:
12488     case VSX_BUILTIN_VPERM_4SI:
12489     case VSX_BUILTIN_VPERM_4SF:
12490     case VSX_BUILTIN_VPERM_2DI:
12491     case VSX_BUILTIN_VPERM_2DF:
12492       h.uns_p[3] = 1;
12493       break;
12494
12495       /* unsigned args, signed return.  */
12496     case VSX_BUILTIN_XVCVUXDDP_UNS:
12497     case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
12498       h.uns_p[1] = 1;
12499       break;
12500
12501       /* signed args, unsigned return.  */
12502     case VSX_BUILTIN_XVCVDPUXDS_UNS:
12503     case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
12504       h.uns_p[0] = 1;
12505       break;
12506
12507     default:
12508       break;
12509     }
12510
12511   /* Figure out how many args are present.  */
12512   while (num_args > 0 && h.mode[num_args] == VOIDmode)
12513     num_args--;
12514
12515   if (num_args == 0)
12516     fatal_error ("internal error: builtin function %s had no type", name);
12517
12518   ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
12519   if (!ret_type && h.uns_p[0])
12520     ret_type = builtin_mode_to_type[h.mode[0]][0];
12521
12522   if (!ret_type)
12523     fatal_error ("internal error: builtin function %s had an unexpected "
12524                  "return type %s", name, GET_MODE_NAME (h.mode[0]));
12525
12526   for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
12527     arg_type[i] = NULL_TREE;
12528
12529   for (i = 0; i < num_args; i++)
12530     {
12531       int m = (int) h.mode[i+1];
12532       int uns_p = h.uns_p[i+1];
12533
12534       arg_type[i] = builtin_mode_to_type[m][uns_p];
12535       if (!arg_type[i] && uns_p)
12536         arg_type[i] = builtin_mode_to_type[m][0];
12537
12538       if (!arg_type[i])
12539         fatal_error ("internal error: builtin function %s, argument %d "
12540                      "had unexpected argument type %s", name, i,
12541                      GET_MODE_NAME (m));
12542     }
12543
12544   found = htab_find_slot (builtin_hash_table, &h, INSERT);
12545   if (*found == NULL)
12546     {
12547       h2 = ggc_alloc_builtin_hash_struct ();
12548       *h2 = h;
12549       *found = (void *)h2;
12550
12551       h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
12552                                            arg_type[2], NULL_TREE);
12553     }
12554
12555   return ((struct builtin_hash_struct *)(*found))->type;
12556 }
12557
12558 static void
12559 rs6000_common_init_builtins (void)
12560 {
12561   const struct builtin_description *d;
12562   size_t i;
12563
12564   tree opaque_ftype_opaque = NULL_TREE;
12565   tree opaque_ftype_opaque_opaque = NULL_TREE;
12566   tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
12567   tree v2si_ftype_qi = NULL_TREE;
12568   tree v2si_ftype_v2si_qi = NULL_TREE;
12569   tree v2si_ftype_int_qi = NULL_TREE;
12570   unsigned builtin_mask = rs6000_builtin_mask;
12571
12572   if (!TARGET_PAIRED_FLOAT)
12573     {
12574       builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
12575       builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
12576     }
12577
12578   /* Paired and SPE builtins are only available if you build a compiler with
12579      the appropriate options, so only create those builtins with the
12580      appropriate compiler option.  Create Altivec and VSX builtins on machines
12581      with at least the general purpose extensions (970 and newer) to allow the
12582      use of the target attribute..  */
12583
12584   if (TARGET_EXTRA_BUILTINS)
12585     builtin_mask |= RS6000_BTM_COMMON;
12586
12587   /* Add the ternary operators.  */
12588   d = bdesc_3arg;
12589   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12590     {
12591       tree type;
12592       unsigned mask = d->mask;
12593
12594       if ((mask & builtin_mask) != mask)
12595         {
12596           if (TARGET_DEBUG_BUILTIN)
12597             fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
12598           continue;
12599         }
12600
12601       if (rs6000_overloaded_builtin_p (d->code))
12602         {
12603           if (! (type = opaque_ftype_opaque_opaque_opaque))
12604             type = opaque_ftype_opaque_opaque_opaque
12605               = build_function_type_list (opaque_V4SI_type_node,
12606                                           opaque_V4SI_type_node,
12607                                           opaque_V4SI_type_node,
12608                                           opaque_V4SI_type_node,
12609                                           NULL_TREE);
12610         }
12611       else
12612         {
12613           enum insn_code icode = d->icode;
12614           if (d->name == 0 || icode == CODE_FOR_nothing)
12615             continue;
12616
12617           type = builtin_function_type (insn_data[icode].operand[0].mode,
12618                                         insn_data[icode].operand[1].mode,
12619                                         insn_data[icode].operand[2].mode,
12620                                         insn_data[icode].operand[3].mode,
12621                                         d->code, d->name);
12622         }
12623
12624       def_builtin (d->name, type, d->code);
12625     }
12626
12627   /* Add the binary operators.  */
12628   d = bdesc_2arg;
12629   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12630     {
12631       enum machine_mode mode0, mode1, mode2;
12632       tree type;
12633       unsigned mask = d->mask;
12634
12635       if ((mask & builtin_mask) != mask)
12636         {
12637           if (TARGET_DEBUG_BUILTIN)
12638             fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
12639           continue;
12640         }
12641
12642       if (rs6000_overloaded_builtin_p (d->code))
12643         {
12644           if (! (type = opaque_ftype_opaque_opaque))
12645             type = opaque_ftype_opaque_opaque
12646               = build_function_type_list (opaque_V4SI_type_node,
12647                                           opaque_V4SI_type_node,
12648                                           opaque_V4SI_type_node,
12649                                           NULL_TREE);
12650         }
12651       else
12652         {
12653           enum insn_code icode = d->icode;
12654           if (d->name == 0 || icode == CODE_FOR_nothing)
12655             continue;
12656
12657           mode0 = insn_data[icode].operand[0].mode;
12658           mode1 = insn_data[icode].operand[1].mode;
12659           mode2 = insn_data[icode].operand[2].mode;
12660
12661           if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
12662             {
12663               if (! (type = v2si_ftype_v2si_qi))
12664                 type = v2si_ftype_v2si_qi
12665                   = build_function_type_list (opaque_V2SI_type_node,
12666                                               opaque_V2SI_type_node,
12667                                               char_type_node,
12668                                               NULL_TREE);
12669             }
12670
12671           else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
12672                    && mode2 == QImode)
12673             {
12674               if (! (type = v2si_ftype_int_qi))
12675                 type = v2si_ftype_int_qi
12676                   = build_function_type_list (opaque_V2SI_type_node,
12677                                               integer_type_node,
12678                                               char_type_node,
12679                                               NULL_TREE);
12680             }
12681
12682           else
12683             type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
12684                                           d->code, d->name);
12685         }
12686
12687       def_builtin (d->name, type, d->code);
12688     }
12689
12690   /* Add the simple unary operators.  */
12691   d = bdesc_1arg;
12692   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12693     {
12694       enum machine_mode mode0, mode1;
12695       tree type;
12696       unsigned mask = d->mask;
12697
12698       if ((mask & builtin_mask) != mask)
12699         {
12700           if (TARGET_DEBUG_BUILTIN)
12701             fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
12702           continue;
12703         }
12704
12705       if (rs6000_overloaded_builtin_p (d->code))
12706         {
12707           if (! (type = opaque_ftype_opaque))
12708             type = opaque_ftype_opaque
12709               = build_function_type_list (opaque_V4SI_type_node,
12710                                           opaque_V4SI_type_node,
12711                                           NULL_TREE);
12712         }
12713       else
12714         {
12715           enum insn_code icode = d->icode;
12716           if (d->name == 0 || icode == CODE_FOR_nothing)
12717             continue;
12718
12719           mode0 = insn_data[icode].operand[0].mode;
12720           mode1 = insn_data[icode].operand[1].mode;
12721
12722           if (mode0 == V2SImode && mode1 == QImode)
12723             {
12724               if (! (type = v2si_ftype_qi))
12725                 type = v2si_ftype_qi
12726                   = build_function_type_list (opaque_V2SI_type_node,
12727                                               char_type_node,
12728                                               NULL_TREE);
12729             }
12730
12731           else
12732             type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
12733                                           d->code, d->name);
12734         }
12735
12736       def_builtin (d->name, type, d->code);
12737     }
12738 }
12739
12740 static void
12741 rs6000_init_libfuncs (void)
12742 {
12743   if (DEFAULT_ABI != ABI_V4 && TARGET_XCOFF
12744       && !TARGET_POWER2 && !TARGET_POWERPC)
12745     {
12746       /* AIX library routines for float->int conversion.  */
12747       set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
12748       set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
12749       set_conv_libfunc (sfix_optab, SImode, TFmode, "_qitrunc");
12750       set_conv_libfunc (ufix_optab, SImode, TFmode, "_quitrunc");
12751     }
12752
12753   if (!TARGET_IEEEQUAD)
12754       /* AIX/Darwin/64-bit Linux quad floating point routines.  */
12755     if (!TARGET_XL_COMPAT)
12756       {
12757         set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
12758         set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
12759         set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
12760         set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
12761
12762         if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
12763           {
12764             set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
12765             set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
12766             set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
12767             set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
12768             set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
12769             set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
12770             set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
12771
12772             set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
12773             set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
12774             set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
12775             set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
12776             set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
12777             set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
12778             set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
12779             set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
12780           }
12781
12782         if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
12783           set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
12784       }
12785     else
12786       {
12787         set_optab_libfunc (add_optab, TFmode, "_xlqadd");
12788         set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
12789         set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
12790         set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
12791       }
12792   else
12793     {
12794       /* 32-bit SVR4 quad floating point routines.  */
12795
12796       set_optab_libfunc (add_optab, TFmode, "_q_add");
12797       set_optab_libfunc (sub_optab, TFmode, "_q_sub");
12798       set_optab_libfunc (neg_optab, TFmode, "_q_neg");
12799       set_optab_libfunc (smul_optab, TFmode, "_q_mul");
12800       set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
12801       if (TARGET_PPC_GPOPT || TARGET_POWER2)
12802         set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
12803
12804       set_optab_libfunc (eq_optab, TFmode, "_q_feq");
12805       set_optab_libfunc (ne_optab, TFmode, "_q_fne");
12806       set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
12807       set_optab_libfunc (ge_optab, TFmode, "_q_fge");
12808       set_optab_libfunc (lt_optab, TFmode, "_q_flt");
12809       set_optab_libfunc (le_optab, TFmode, "_q_fle");
12810
12811       set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
12812       set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
12813       set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
12814       set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
12815       set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
12816       set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
12817       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
12818       set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
12819     }
12820 }
12821
12822 \f
12823 /* Expand a block clear operation, and return 1 if successful.  Return 0
12824    if we should let the compiler generate normal code.
12825
12826    operands[0] is the destination
12827    operands[1] is the length
12828    operands[3] is the alignment */
12829
12830 int
12831 expand_block_clear (rtx operands[])
12832 {
12833   rtx orig_dest = operands[0];
12834   rtx bytes_rtx = operands[1];
12835   rtx align_rtx = operands[3];
12836   bool constp   = (GET_CODE (bytes_rtx) == CONST_INT);
12837   HOST_WIDE_INT align;
12838   HOST_WIDE_INT bytes;
12839   int offset;
12840   int clear_bytes;
12841   int clear_step;
12842
12843   /* If this is not a fixed size move, just call memcpy */
12844   if (! constp)
12845     return 0;
12846
12847   /* This must be a fixed size alignment  */
12848   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12849   align = INTVAL (align_rtx) * BITS_PER_UNIT;
12850
12851   /* Anything to clear? */
12852   bytes = INTVAL (bytes_rtx);
12853   if (bytes <= 0)
12854     return 1;
12855
12856   /* Use the builtin memset after a point, to avoid huge code bloat.
12857      When optimize_size, avoid any significant code bloat; calling
12858      memset is about 4 instructions, so allow for one instruction to
12859      load zero and three to do clearing.  */
12860   if (TARGET_ALTIVEC && align >= 128)
12861     clear_step = 16;
12862   else if (TARGET_POWERPC64 && align >= 32)
12863     clear_step = 8;
12864   else if (TARGET_SPE && align >= 64)
12865     clear_step = 8;
12866   else
12867     clear_step = 4;
12868
12869   if (optimize_size && bytes > 3 * clear_step)
12870     return 0;
12871   if (! optimize_size && bytes > 8 * clear_step)
12872     return 0;
12873
12874   for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
12875     {
12876       enum machine_mode mode = BLKmode;
12877       rtx dest;
12878
12879       if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
12880         {
12881           clear_bytes = 16;
12882           mode = V4SImode;
12883         }
12884       else if (bytes >= 8 && TARGET_SPE && align >= 64)
12885         {
12886           clear_bytes = 8;
12887           mode = V2SImode;
12888         }
12889       else if (bytes >= 8 && TARGET_POWERPC64
12890                /* 64-bit loads and stores require word-aligned
12891                   displacements.  */
12892                && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
12893         {
12894           clear_bytes = 8;
12895           mode = DImode;
12896         }
12897       else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
12898         {                       /* move 4 bytes */
12899           clear_bytes = 4;
12900           mode = SImode;
12901         }
12902       else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
12903         {                       /* move 2 bytes */
12904           clear_bytes = 2;
12905           mode = HImode;
12906         }
12907       else /* move 1 byte at a time */
12908         {
12909           clear_bytes = 1;
12910           mode = QImode;
12911         }
12912
12913       dest = adjust_address (orig_dest, mode, offset);
12914
12915       emit_move_insn (dest, CONST0_RTX (mode));
12916     }
12917
12918   return 1;
12919 }
12920
12921 \f
12922 /* Expand a block move operation, and return 1 if successful.  Return 0
12923    if we should let the compiler generate normal code.
12924
12925    operands[0] is the destination
12926    operands[1] is the source
12927    operands[2] is the length
12928    operands[3] is the alignment */
12929
12930 #define MAX_MOVE_REG 4
12931
12932 int
12933 expand_block_move (rtx operands[])
12934 {
12935   rtx orig_dest = operands[0];
12936   rtx orig_src  = operands[1];
12937   rtx bytes_rtx = operands[2];
12938   rtx align_rtx = operands[3];
12939   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
12940   int align;
12941   int bytes;
12942   int offset;
12943   int move_bytes;
12944   rtx stores[MAX_MOVE_REG];
12945   int num_reg = 0;
12946
12947   /* If this is not a fixed size move, just call memcpy */
12948   if (! constp)
12949     return 0;
12950
12951   /* This must be a fixed size alignment */
12952   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12953   align = INTVAL (align_rtx) * BITS_PER_UNIT;
12954
12955   /* Anything to move? */
12956   bytes = INTVAL (bytes_rtx);
12957   if (bytes <= 0)
12958     return 1;
12959
12960   if (bytes > rs6000_block_move_inline_limit)
12961     return 0;
12962
12963   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
12964     {
12965       union {
12966         rtx (*movmemsi) (rtx, rtx, rtx, rtx);
12967         rtx (*mov) (rtx, rtx);
12968       } gen_func;
12969       enum machine_mode mode = BLKmode;
12970       rtx src, dest;
12971
12972       /* Altivec first, since it will be faster than a string move
12973          when it applies, and usually not significantly larger.  */
12974       if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
12975         {
12976           move_bytes = 16;
12977           mode = V4SImode;
12978           gen_func.mov = gen_movv4si;
12979         }
12980       else if (TARGET_SPE && bytes >= 8 && align >= 64)
12981         {
12982           move_bytes = 8;
12983           mode = V2SImode;
12984           gen_func.mov = gen_movv2si;
12985         }
12986       else if (TARGET_STRING
12987           && bytes > 24         /* move up to 32 bytes at a time */
12988           && ! fixed_regs[5]
12989           && ! fixed_regs[6]
12990           && ! fixed_regs[7]
12991           && ! fixed_regs[8]
12992           && ! fixed_regs[9]
12993           && ! fixed_regs[10]
12994           && ! fixed_regs[11]
12995           && ! fixed_regs[12])
12996         {
12997           move_bytes = (bytes > 32) ? 32 : bytes;
12998           gen_func.movmemsi = gen_movmemsi_8reg;
12999         }
13000       else if (TARGET_STRING
13001                && bytes > 16    /* move up to 24 bytes at a time */
13002                && ! fixed_regs[5]
13003                && ! fixed_regs[6]
13004                && ! fixed_regs[7]
13005                && ! fixed_regs[8]
13006                && ! fixed_regs[9]
13007                && ! fixed_regs[10])
13008         {
13009           move_bytes = (bytes > 24) ? 24 : bytes;
13010           gen_func.movmemsi = gen_movmemsi_6reg;
13011         }
13012       else if (TARGET_STRING
13013                && bytes > 8     /* move up to 16 bytes at a time */
13014                && ! fixed_regs[5]
13015                && ! fixed_regs[6]
13016                && ! fixed_regs[7]
13017                && ! fixed_regs[8])
13018         {
13019           move_bytes = (bytes > 16) ? 16 : bytes;
13020           gen_func.movmemsi = gen_movmemsi_4reg;
13021         }
13022       else if (bytes >= 8 && TARGET_POWERPC64
13023                /* 64-bit loads and stores require word-aligned
13024                   displacements.  */
13025                && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
13026         {
13027           move_bytes = 8;
13028           mode = DImode;
13029           gen_func.mov = gen_movdi;
13030         }
13031       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
13032         {                       /* move up to 8 bytes at a time */
13033           move_bytes = (bytes > 8) ? 8 : bytes;
13034           gen_func.movmemsi = gen_movmemsi_2reg;
13035         }
13036       else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
13037         {                       /* move 4 bytes */
13038           move_bytes = 4;
13039           mode = SImode;
13040           gen_func.mov = gen_movsi;
13041         }
13042       else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
13043         {                       /* move 2 bytes */
13044           move_bytes = 2;
13045           mode = HImode;
13046           gen_func.mov = gen_movhi;
13047         }
13048       else if (TARGET_STRING && bytes > 1)
13049         {                       /* move up to 4 bytes at a time */
13050           move_bytes = (bytes > 4) ? 4 : bytes;
13051           gen_func.movmemsi = gen_movmemsi_1reg;
13052         }
13053       else /* move 1 byte at a time */
13054         {
13055           move_bytes = 1;
13056           mode = QImode;
13057           gen_func.mov = gen_movqi;
13058         }
13059
13060       src = adjust_address (orig_src, mode, offset);
13061       dest = adjust_address (orig_dest, mode, offset);
13062
13063       if (mode != BLKmode)
13064         {
13065           rtx tmp_reg = gen_reg_rtx (mode);
13066
13067           emit_insn ((*gen_func.mov) (tmp_reg, src));
13068           stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
13069         }
13070
13071       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
13072         {
13073           int i;
13074           for (i = 0; i < num_reg; i++)
13075             emit_insn (stores[i]);
13076           num_reg = 0;
13077         }
13078
13079       if (mode == BLKmode)
13080         {
13081           /* Move the address into scratch registers.  The movmemsi
13082              patterns require zero offset.  */
13083           if (!REG_P (XEXP (src, 0)))
13084             {
13085               rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
13086               src = replace_equiv_address (src, src_reg);
13087             }
13088           set_mem_size (src, move_bytes);
13089
13090           if (!REG_P (XEXP (dest, 0)))
13091             {
13092               rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
13093               dest = replace_equiv_address (dest, dest_reg);
13094             }
13095           set_mem_size (dest, move_bytes);
13096
13097           emit_insn ((*gen_func.movmemsi) (dest, src,
13098                                            GEN_INT (move_bytes & 31),
13099                                            align_rtx));
13100         }
13101     }
13102
13103   return 1;
13104 }
13105
13106 \f
13107 /* Return a string to perform a load_multiple operation.
13108    operands[0] is the vector.
13109    operands[1] is the source address.
13110    operands[2] is the first destination register.  */
13111
13112 const char *
13113 rs6000_output_load_multiple (rtx operands[3])
13114 {
13115   /* We have to handle the case where the pseudo used to contain the address
13116      is assigned to one of the output registers.  */
13117   int i, j;
13118   int words = XVECLEN (operands[0], 0);
13119   rtx xop[10];
13120
13121   if (XVECLEN (operands[0], 0) == 1)
13122     return "{l|lwz} %2,0(%1)";
13123
13124   for (i = 0; i < words; i++)
13125     if (refers_to_regno_p (REGNO (operands[2]) + i,
13126                            REGNO (operands[2]) + i + 1, operands[1], 0))
13127       {
13128         if (i == words-1)
13129           {
13130             xop[0] = GEN_INT (4 * (words-1));
13131             xop[1] = operands[1];
13132             xop[2] = operands[2];
13133             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
13134             return "";
13135           }
13136         else if (i == 0)
13137           {
13138             xop[0] = GEN_INT (4 * (words-1));
13139             xop[1] = operands[1];
13140             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
13141             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);
13142             return "";
13143           }
13144         else
13145           {
13146             for (j = 0; j < words; j++)
13147               if (j != i)
13148                 {
13149                   xop[0] = GEN_INT (j * 4);
13150                   xop[1] = operands[1];
13151                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
13152                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
13153                 }
13154             xop[0] = GEN_INT (i * 4);
13155             xop[1] = operands[1];
13156             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
13157             return "";
13158           }
13159       }
13160
13161   return "{lsi|lswi} %2,%1,%N0";
13162 }
13163
13164 \f
13165 /* A validation routine: say whether CODE, a condition code, and MODE
13166    match.  The other alternatives either don't make sense or should
13167    never be generated.  */
13168
13169 void
13170 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
13171 {
13172   gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
13173                || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
13174               && GET_MODE_CLASS (mode) == MODE_CC);
13175
13176   /* These don't make sense.  */
13177   gcc_assert ((code != GT && code != LT && code != GE && code != LE)
13178               || mode != CCUNSmode);
13179
13180   gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
13181               || mode == CCUNSmode);
13182
13183   gcc_assert (mode == CCFPmode
13184               || (code != ORDERED && code != UNORDERED
13185                   && code != UNEQ && code != LTGT
13186                   && code != UNGT && code != UNLT
13187                   && code != UNGE && code != UNLE));
13188
13189   /* These should never be generated except for
13190      flag_finite_math_only.  */
13191   gcc_assert (mode != CCFPmode
13192               || flag_finite_math_only
13193               || (code != LE && code != GE
13194                   && code != UNEQ && code != LTGT
13195                   && code != UNGT && code != UNLT));
13196
13197   /* These are invalid; the information is not there.  */
13198   gcc_assert (mode != CCEQmode || code == EQ || code == NE);
13199 }
13200
13201 \f
13202 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
13203    mask required to convert the result of a rotate insn into a shift
13204    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
13205
13206 int
13207 includes_lshift_p (rtx shiftop, rtx andop)
13208 {
13209   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13210
13211   shift_mask <<= INTVAL (shiftop);
13212
13213   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13214 }
13215
13216 /* Similar, but for right shift.  */
13217
13218 int
13219 includes_rshift_p (rtx shiftop, rtx andop)
13220 {
13221   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13222
13223   shift_mask >>= INTVAL (shiftop);
13224
13225   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13226 }
13227
13228 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
13229    to perform a left shift.  It must have exactly SHIFTOP least
13230    significant 0's, then one or more 1's, then zero or more 0's.  */
13231
13232 int
13233 includes_rldic_lshift_p (rtx shiftop, rtx andop)
13234 {
13235   if (GET_CODE (andop) == CONST_INT)
13236     {
13237       HOST_WIDE_INT c, lsb, shift_mask;
13238
13239       c = INTVAL (andop);
13240       if (c == 0 || c == ~0)
13241         return 0;
13242
13243       shift_mask = ~0;
13244       shift_mask <<= INTVAL (shiftop);
13245
13246       /* Find the least significant one bit.  */
13247       lsb = c & -c;
13248
13249       /* It must coincide with the LSB of the shift mask.  */
13250       if (-lsb != shift_mask)
13251         return 0;
13252
13253       /* Invert to look for the next transition (if any).  */
13254       c = ~c;
13255
13256       /* Remove the low group of ones (originally low group of zeros).  */
13257       c &= -lsb;
13258
13259       /* Again find the lsb, and check we have all 1's above.  */
13260       lsb = c & -c;
13261       return c == -lsb;
13262     }
13263   else if (GET_CODE (andop) == CONST_DOUBLE
13264            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13265     {
13266       HOST_WIDE_INT low, high, lsb;
13267       HOST_WIDE_INT shift_mask_low, shift_mask_high;
13268
13269       low = CONST_DOUBLE_LOW (andop);
13270       if (HOST_BITS_PER_WIDE_INT < 64)
13271         high = CONST_DOUBLE_HIGH (andop);
13272
13273       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
13274           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
13275         return 0;
13276
13277       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13278         {
13279           shift_mask_high = ~0;
13280           if (INTVAL (shiftop) > 32)
13281             shift_mask_high <<= INTVAL (shiftop) - 32;
13282
13283           lsb = high & -high;
13284
13285           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
13286             return 0;
13287
13288           high = ~high;
13289           high &= -lsb;
13290
13291           lsb = high & -high;
13292           return high == -lsb;
13293         }
13294
13295       shift_mask_low = ~0;
13296       shift_mask_low <<= INTVAL (shiftop);
13297
13298       lsb = low & -low;
13299
13300       if (-lsb != shift_mask_low)
13301         return 0;
13302
13303       if (HOST_BITS_PER_WIDE_INT < 64)
13304         high = ~high;
13305       low = ~low;
13306       low &= -lsb;
13307
13308       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13309         {
13310           lsb = high & -high;
13311           return high == -lsb;
13312         }
13313
13314       lsb = low & -low;
13315       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
13316     }
13317   else
13318     return 0;
13319 }
13320
13321 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
13322    to perform a left shift.  It must have SHIFTOP or more least
13323    significant 0's, with the remainder of the word 1's.  */
13324
13325 int
13326 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
13327 {
13328   if (GET_CODE (andop) == CONST_INT)
13329     {
13330       HOST_WIDE_INT c, lsb, shift_mask;
13331
13332       shift_mask = ~0;
13333       shift_mask <<= INTVAL (shiftop);
13334       c = INTVAL (andop);
13335
13336       /* Find the least significant one bit.  */
13337       lsb = c & -c;
13338
13339       /* It must be covered by the shift mask.
13340          This test also rejects c == 0.  */
13341       if ((lsb & shift_mask) == 0)
13342         return 0;
13343
13344       /* Check we have all 1's above the transition, and reject all 1's.  */
13345       return c == -lsb && lsb != 1;
13346     }
13347   else if (GET_CODE (andop) == CONST_DOUBLE
13348            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13349     {
13350       HOST_WIDE_INT low, lsb, shift_mask_low;
13351
13352       low = CONST_DOUBLE_LOW (andop);
13353
13354       if (HOST_BITS_PER_WIDE_INT < 64)
13355         {
13356           HOST_WIDE_INT high, shift_mask_high;
13357
13358           high = CONST_DOUBLE_HIGH (andop);
13359
13360           if (low == 0)
13361             {
13362               shift_mask_high = ~0;
13363               if (INTVAL (shiftop) > 32)
13364                 shift_mask_high <<= INTVAL (shiftop) - 32;
13365
13366               lsb = high & -high;
13367
13368               if ((lsb & shift_mask_high) == 0)
13369                 return 0;
13370
13371               return high == -lsb;
13372             }
13373           if (high != ~0)
13374             return 0;
13375         }
13376
13377       shift_mask_low = ~0;
13378       shift_mask_low <<= INTVAL (shiftop);
13379
13380       lsb = low & -low;
13381
13382       if ((lsb & shift_mask_low) == 0)
13383         return 0;
13384
13385       return low == -lsb && lsb != 1;
13386     }
13387   else
13388     return 0;
13389 }
13390
13391 /* Return 1 if operands will generate a valid arguments to rlwimi
13392 instruction for insert with right shift in 64-bit mode.  The mask may
13393 not start on the first bit or stop on the last bit because wrap-around
13394 effects of instruction do not correspond to semantics of RTL insn.  */
13395
13396 int
13397 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
13398 {
13399   if (INTVAL (startop) > 32
13400       && INTVAL (startop) < 64
13401       && INTVAL (sizeop) > 1
13402       && INTVAL (sizeop) + INTVAL (startop) < 64
13403       && INTVAL (shiftop) > 0
13404       && INTVAL (sizeop) + INTVAL (shiftop) < 32
13405       && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
13406     return 1;
13407
13408   return 0;
13409 }
13410
13411 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
13412    for lfq and stfq insns iff the registers are hard registers.   */
13413
13414 int
13415 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
13416 {
13417   /* We might have been passed a SUBREG.  */
13418   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
13419     return 0;
13420
13421   /* We might have been passed non floating point registers.  */
13422   if (!FP_REGNO_P (REGNO (reg1))
13423       || !FP_REGNO_P (REGNO (reg2)))
13424     return 0;
13425
13426   return (REGNO (reg1) == REGNO (reg2) - 1);
13427 }
13428
13429 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
13430    addr1 and addr2 must be in consecutive memory locations
13431    (addr2 == addr1 + 8).  */
13432
13433 int
13434 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
13435 {
13436   rtx addr1, addr2;
13437   unsigned int reg1, reg2;
13438   int offset1, offset2;
13439
13440   /* The mems cannot be volatile.  */
13441   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
13442     return 0;
13443
13444   addr1 = XEXP (mem1, 0);
13445   addr2 = XEXP (mem2, 0);
13446
13447   /* Extract an offset (if used) from the first addr.  */
13448   if (GET_CODE (addr1) == PLUS)
13449     {
13450       /* If not a REG, return zero.  */
13451       if (GET_CODE (XEXP (addr1, 0)) != REG)
13452         return 0;
13453       else
13454         {
13455           reg1 = REGNO (XEXP (addr1, 0));
13456           /* The offset must be constant!  */
13457           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
13458             return 0;
13459           offset1 = INTVAL (XEXP (addr1, 1));
13460         }
13461     }
13462   else if (GET_CODE (addr1) != REG)
13463     return 0;
13464   else
13465     {
13466       reg1 = REGNO (addr1);
13467       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
13468       offset1 = 0;
13469     }
13470
13471   /* And now for the second addr.  */
13472   if (GET_CODE (addr2) == PLUS)
13473     {
13474       /* If not a REG, return zero.  */
13475       if (GET_CODE (XEXP (addr2, 0)) != REG)
13476         return 0;
13477       else
13478         {
13479           reg2 = REGNO (XEXP (addr2, 0));
13480           /* The offset must be constant. */
13481           if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
13482             return 0;
13483           offset2 = INTVAL (XEXP (addr2, 1));
13484         }
13485     }
13486   else if (GET_CODE (addr2) != REG)
13487     return 0;
13488   else
13489     {
13490       reg2 = REGNO (addr2);
13491       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
13492       offset2 = 0;
13493     }
13494
13495   /* Both of these must have the same base register.  */
13496   if (reg1 != reg2)
13497     return 0;
13498
13499   /* The offset for the second addr must be 8 more than the first addr.  */
13500   if (offset2 != offset1 + 8)
13501     return 0;
13502
13503   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
13504      instructions.  */
13505   return 1;
13506 }
13507 \f
13508
13509 rtx
13510 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
13511 {
13512   static bool eliminated = false;
13513   rtx ret;
13514
13515   if (mode != SDmode)
13516     ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13517   else
13518     {
13519       rtx mem = cfun->machine->sdmode_stack_slot;
13520       gcc_assert (mem != NULL_RTX);
13521
13522       if (!eliminated)
13523         {
13524           mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
13525           cfun->machine->sdmode_stack_slot = mem;
13526           eliminated = true;
13527         }
13528       ret = mem;
13529     }
13530
13531   if (TARGET_DEBUG_ADDR)
13532     {
13533       fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
13534                GET_MODE_NAME (mode));
13535       if (!ret)
13536         fprintf (stderr, "\tNULL_RTX\n");
13537       else
13538         debug_rtx (ret);
13539     }
13540
13541   return ret;
13542 }
13543
13544 static tree
13545 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13546 {
13547   /* Don't walk into types.  */
13548   if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
13549     {
13550       *walk_subtrees = 0;
13551       return NULL_TREE;
13552     }
13553
13554   switch (TREE_CODE (*tp))
13555     {
13556     case VAR_DECL:
13557     case PARM_DECL:
13558     case FIELD_DECL:
13559     case RESULT_DECL:
13560     case SSA_NAME:
13561     case REAL_CST:
13562     case MEM_REF:
13563     case VIEW_CONVERT_EXPR:
13564       if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
13565         return *tp;
13566       break;
13567     default:
13568       break;
13569     }
13570
13571   return NULL_TREE;
13572 }
13573
13574 enum reload_reg_type {
13575   GPR_REGISTER_TYPE,
13576   VECTOR_REGISTER_TYPE,
13577   OTHER_REGISTER_TYPE
13578 };
13579
13580 static enum reload_reg_type
13581 rs6000_reload_register_type (enum reg_class rclass)
13582 {
13583   switch (rclass)
13584     {
13585     case GENERAL_REGS:
13586     case BASE_REGS:
13587       return GPR_REGISTER_TYPE;
13588
13589     case FLOAT_REGS:
13590     case ALTIVEC_REGS:
13591     case VSX_REGS:
13592       return VECTOR_REGISTER_TYPE;
13593
13594     default:
13595       return OTHER_REGISTER_TYPE;
13596     }
13597 }
13598
13599 /* Inform reload about cases where moving X with a mode MODE to a register in
13600    RCLASS requires an extra scratch or immediate register.  Return the class
13601    needed for the immediate register.
13602
13603    For VSX and Altivec, we may need a register to convert sp+offset into
13604    reg+sp.
13605
13606    For misaligned 64-bit gpr loads and stores we need a register to
13607    convert an offset address to indirect.  */
13608
13609 static reg_class_t
13610 rs6000_secondary_reload (bool in_p,
13611                          rtx x,
13612                          reg_class_t rclass_i,
13613                          enum machine_mode mode,
13614                          secondary_reload_info *sri)
13615 {
13616   enum reg_class rclass = (enum reg_class) rclass_i;
13617   reg_class_t ret = ALL_REGS;
13618   enum insn_code icode;
13619   bool default_p = false;
13620
13621   sri->icode = CODE_FOR_nothing;
13622
13623   /* Convert vector loads and stores into gprs to use an additional base
13624      register.  */
13625   icode = rs6000_vector_reload[mode][in_p != false];
13626   if (icode != CODE_FOR_nothing)
13627     {
13628       ret = NO_REGS;
13629       sri->icode = CODE_FOR_nothing;
13630       sri->extra_cost = 0;
13631
13632       if (GET_CODE (x) == MEM)
13633         {
13634           rtx addr = XEXP (x, 0);
13635
13636           /* Loads to and stores from gprs can do reg+offset, and wouldn't need
13637              an extra register in that case, but it would need an extra
13638              register if the addressing is reg+reg or (reg+reg)&(-16).  */
13639           if (rclass == GENERAL_REGS || rclass == BASE_REGS)
13640             {
13641               if (!legitimate_indirect_address_p (addr, false)
13642                   && !rs6000_legitimate_offset_address_p (TImode, addr, false))
13643                 {
13644                   sri->icode = icode;
13645                   /* account for splitting the loads, and converting the
13646                      address from reg+reg to reg.  */
13647                   sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
13648                                      + ((GET_CODE (addr) == AND) ? 1 : 0));
13649                 }
13650             }
13651           /* Loads to and stores from vector registers can only do reg+reg
13652              addressing.  Altivec registers can also do (reg+reg)&(-16).  */
13653           else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
13654                    || rclass == FLOAT_REGS || rclass == NO_REGS)
13655             {
13656               if (!VECTOR_MEM_ALTIVEC_P (mode)
13657                   && GET_CODE (addr) == AND
13658                   && GET_CODE (XEXP (addr, 1)) == CONST_INT
13659                   && INTVAL (XEXP (addr, 1)) == -16
13660                   && (legitimate_indirect_address_p (XEXP (addr, 0), false)
13661                       || legitimate_indexed_address_p (XEXP (addr, 0), false)))
13662                 {
13663                   sri->icode = icode;
13664                   sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
13665                                      ? 2 : 1);
13666                 }
13667               else if (!legitimate_indirect_address_p (addr, false)
13668                        && (rclass == NO_REGS
13669                            || !legitimate_indexed_address_p (addr, false)))
13670                 {
13671                   sri->icode = icode;
13672                   sri->extra_cost = 1;
13673                 }
13674               else
13675                 icode = CODE_FOR_nothing;
13676             }
13677           /* Any other loads, including to pseudo registers which haven't been
13678              assigned to a register yet, default to require a scratch
13679              register.  */
13680           else
13681             {
13682               sri->icode = icode;
13683               sri->extra_cost = 2;
13684             }
13685         }
13686       else if (REG_P (x))
13687         {
13688           int regno = true_regnum (x);
13689
13690           icode = CODE_FOR_nothing;
13691           if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
13692             default_p = true;
13693           else
13694             {
13695               enum reg_class xclass = REGNO_REG_CLASS (regno);
13696               enum reload_reg_type rtype1 = rs6000_reload_register_type (rclass);
13697               enum reload_reg_type rtype2 = rs6000_reload_register_type (xclass);
13698
13699               /* If memory is needed, use default_secondary_reload to create the
13700                  stack slot.  */
13701               if (rtype1 != rtype2 || rtype1 == OTHER_REGISTER_TYPE)
13702                 default_p = true;
13703               else
13704                 ret = NO_REGS;
13705             }
13706         }
13707       else
13708         default_p = true;
13709     }
13710   else if (TARGET_POWERPC64
13711            && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13712            && MEM_P (x)
13713            && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
13714     {
13715       rtx addr = XEXP (x, 0);
13716
13717       if (GET_CODE (addr) == PRE_MODIFY)
13718         addr = XEXP (addr, 1);
13719       else if (GET_CODE (addr) == LO_SUM
13720                && GET_CODE (XEXP (addr, 0)) == REG
13721                && GET_CODE (XEXP (addr, 1)) == CONST)
13722         addr = XEXP (XEXP (addr, 1), 0);
13723
13724       if (GET_CODE (addr) == PLUS
13725           && GET_CODE (XEXP (addr, 1)) == CONST_INT
13726           && (INTVAL (XEXP (addr, 1)) & 3) != 0)
13727         {
13728           if (in_p)
13729             sri->icode = CODE_FOR_reload_di_load;
13730           else
13731             sri->icode = CODE_FOR_reload_di_store;
13732           sri->extra_cost = 2;
13733           ret = NO_REGS;
13734         }
13735       else
13736         default_p = true;
13737     }
13738   else
13739     default_p = true;
13740
13741   if (default_p)
13742     ret = default_secondary_reload (in_p, x, rclass, mode, sri);
13743
13744   gcc_assert (ret != ALL_REGS);
13745
13746   if (TARGET_DEBUG_ADDR)
13747     {
13748       fprintf (stderr,
13749                "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
13750                "mode = %s",
13751                reg_class_names[ret],
13752                in_p ? "true" : "false",
13753                reg_class_names[rclass],
13754                GET_MODE_NAME (mode));
13755
13756       if (default_p)
13757         fprintf (stderr, ", default secondary reload");
13758
13759       if (sri->icode != CODE_FOR_nothing)
13760         fprintf (stderr, ", reload func = %s, extra cost = %d\n",
13761                  insn_data[sri->icode].name, sri->extra_cost);
13762       else
13763         fprintf (stderr, "\n");
13764
13765       debug_rtx (x);
13766     }
13767
13768   return ret;
13769 }
13770
13771 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
13772    to SP+reg addressing.  */
13773
13774 void
13775 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
13776 {
13777   int regno = true_regnum (reg);
13778   enum machine_mode mode = GET_MODE (reg);
13779   enum reg_class rclass;
13780   rtx addr;
13781   rtx and_op2 = NULL_RTX;
13782   rtx addr_op1;
13783   rtx addr_op2;
13784   rtx scratch_or_premodify = scratch;
13785   rtx and_rtx;
13786   rtx cc_clobber;
13787
13788   if (TARGET_DEBUG_ADDR)
13789     {
13790       fprintf (stderr, "\nrs6000_secondary_reload_inner, type = %s\n",
13791                store_p ? "store" : "load");
13792       fprintf (stderr, "reg:\n");
13793       debug_rtx (reg);
13794       fprintf (stderr, "mem:\n");
13795       debug_rtx (mem);
13796       fprintf (stderr, "scratch:\n");
13797       debug_rtx (scratch);
13798     }
13799
13800   gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
13801   gcc_assert (GET_CODE (mem) == MEM);
13802   rclass = REGNO_REG_CLASS (regno);
13803   addr = XEXP (mem, 0);
13804
13805   switch (rclass)
13806     {
13807       /* GPRs can handle reg + small constant, all other addresses need to use
13808          the scratch register.  */
13809     case GENERAL_REGS:
13810     case BASE_REGS:
13811       if (GET_CODE (addr) == AND)
13812         {
13813           and_op2 = XEXP (addr, 1);
13814           addr = XEXP (addr, 0);
13815         }
13816
13817       if (GET_CODE (addr) == PRE_MODIFY)
13818         {
13819           scratch_or_premodify = XEXP (addr, 0);
13820           gcc_assert (REG_P (scratch_or_premodify));
13821           gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13822           addr = XEXP (addr, 1);
13823         }
13824
13825       if (GET_CODE (addr) == PLUS
13826           && (!rs6000_legitimate_offset_address_p (TImode, addr, false)
13827               || and_op2 != NULL_RTX))
13828         {
13829           addr_op1 = XEXP (addr, 0);
13830           addr_op2 = XEXP (addr, 1);
13831           gcc_assert (legitimate_indirect_address_p (addr_op1, false));
13832
13833           if (!REG_P (addr_op2)
13834               && (GET_CODE (addr_op2) != CONST_INT
13835                   || !satisfies_constraint_I (addr_op2)))
13836             {
13837               if (TARGET_DEBUG_ADDR)
13838                 {
13839                   fprintf (stderr,
13840                            "\nMove plus addr to register %s, mode = %s: ",
13841                            rs6000_reg_names[REGNO (scratch)],
13842                            GET_MODE_NAME (mode));
13843                   debug_rtx (addr_op2);
13844                 }
13845               rs6000_emit_move (scratch, addr_op2, Pmode);
13846               addr_op2 = scratch;
13847             }
13848
13849           emit_insn (gen_rtx_SET (VOIDmode,
13850                                   scratch_or_premodify,
13851                                   gen_rtx_PLUS (Pmode,
13852                                                 addr_op1,
13853                                                 addr_op2)));
13854
13855           addr = scratch_or_premodify;
13856           scratch_or_premodify = scratch;
13857         }
13858       else if (!legitimate_indirect_address_p (addr, false)
13859                && !rs6000_legitimate_offset_address_p (TImode, addr, false))
13860         {
13861           if (TARGET_DEBUG_ADDR)
13862             {
13863               fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13864                        rs6000_reg_names[REGNO (scratch_or_premodify)],
13865                        GET_MODE_NAME (mode));
13866               debug_rtx (addr);
13867             }
13868           rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13869           addr = scratch_or_premodify;
13870           scratch_or_premodify = scratch;
13871         }
13872       break;
13873
13874       /* Float/Altivec registers can only handle reg+reg addressing.  Move
13875          other addresses into a scratch register.  */
13876     case FLOAT_REGS:
13877     case VSX_REGS:
13878     case ALTIVEC_REGS:
13879
13880       /* With float regs, we need to handle the AND ourselves, since we can't
13881          use the Altivec instruction with an implicit AND -16.  Allow scalar
13882          loads to float registers to use reg+offset even if VSX.  */
13883       if (GET_CODE (addr) == AND
13884           && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
13885               || GET_CODE (XEXP (addr, 1)) != CONST_INT
13886               || INTVAL (XEXP (addr, 1)) != -16
13887               || !VECTOR_MEM_ALTIVEC_P (mode)))
13888         {
13889           and_op2 = XEXP (addr, 1);
13890           addr = XEXP (addr, 0);
13891         }
13892
13893       /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
13894          as the address later.  */
13895       if (GET_CODE (addr) == PRE_MODIFY
13896           && (!VECTOR_MEM_VSX_P (mode)
13897               || and_op2 != NULL_RTX
13898               || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
13899         {
13900           scratch_or_premodify = XEXP (addr, 0);
13901           gcc_assert (legitimate_indirect_address_p (scratch_or_premodify,
13902                                                      false));
13903           gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13904           addr = XEXP (addr, 1);
13905         }
13906
13907       if (legitimate_indirect_address_p (addr, false)   /* reg */
13908           || legitimate_indexed_address_p (addr, false) /* reg+reg */
13909           || GET_CODE (addr) == PRE_MODIFY              /* VSX pre-modify */
13910           || (GET_CODE (addr) == AND                    /* Altivec memory */
13911               && GET_CODE (XEXP (addr, 1)) == CONST_INT
13912               && INTVAL (XEXP (addr, 1)) == -16
13913               && VECTOR_MEM_ALTIVEC_P (mode))
13914           || (rclass == FLOAT_REGS                      /* legacy float mem */
13915               && GET_MODE_SIZE (mode) == 8
13916               && and_op2 == NULL_RTX
13917               && scratch_or_premodify == scratch
13918               && rs6000_legitimate_offset_address_p (mode, addr, false)))
13919         ;
13920
13921       else if (GET_CODE (addr) == PLUS)
13922         {
13923           addr_op1 = XEXP (addr, 0);
13924           addr_op2 = XEXP (addr, 1);
13925           gcc_assert (REG_P (addr_op1));
13926
13927           if (TARGET_DEBUG_ADDR)
13928             {
13929               fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
13930                        rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
13931               debug_rtx (addr_op2);
13932             }
13933           rs6000_emit_move (scratch, addr_op2, Pmode);
13934           emit_insn (gen_rtx_SET (VOIDmode,
13935                                   scratch_or_premodify,
13936                                   gen_rtx_PLUS (Pmode,
13937                                                 addr_op1,
13938                                                 scratch)));
13939           addr = scratch_or_premodify;
13940           scratch_or_premodify = scratch;
13941         }
13942
13943       else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
13944                || GET_CODE (addr) == CONST_INT || REG_P (addr))
13945         {
13946           if (TARGET_DEBUG_ADDR)
13947             {
13948               fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13949                        rs6000_reg_names[REGNO (scratch_or_premodify)],
13950                        GET_MODE_NAME (mode));
13951               debug_rtx (addr);
13952             }
13953
13954           rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13955           addr = scratch_or_premodify;
13956           scratch_or_premodify = scratch;
13957         }
13958
13959       else
13960         gcc_unreachable ();
13961
13962       break;
13963
13964     default:
13965       gcc_unreachable ();
13966     }
13967
13968   /* If the original address involved a pre-modify that we couldn't use the VSX
13969      memory instruction with update, and we haven't taken care of already,
13970      store the address in the pre-modify register and use that as the
13971      address.  */
13972   if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
13973     {
13974       emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
13975       addr = scratch_or_premodify;
13976     }
13977
13978   /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
13979      memory instruction, recreate the AND now, including the clobber which is
13980      generated by the general ANDSI3/ANDDI3 patterns for the
13981      andi. instruction.  */
13982   if (and_op2 != NULL_RTX)
13983     {
13984       if (! legitimate_indirect_address_p (addr, false))
13985         {
13986           emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
13987           addr = scratch;
13988         }
13989
13990       if (TARGET_DEBUG_ADDR)
13991         {
13992           fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
13993                    rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
13994           debug_rtx (and_op2);
13995         }
13996
13997       and_rtx = gen_rtx_SET (VOIDmode,
13998                              scratch,
13999                              gen_rtx_AND (Pmode,
14000                                           addr,
14001                                           and_op2));
14002
14003       cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
14004       emit_insn (gen_rtx_PARALLEL (VOIDmode,
14005                                    gen_rtvec (2, and_rtx, cc_clobber)));
14006       addr = scratch;
14007     }
14008
14009   /* Adjust the address if it changed.  */
14010   if (addr != XEXP (mem, 0))
14011     {
14012       mem = change_address (mem, mode, addr);
14013       if (TARGET_DEBUG_ADDR)
14014         fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
14015     }
14016
14017   /* Now create the move.  */
14018   if (store_p)
14019     emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14020   else
14021     emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14022
14023   return;
14024 }
14025
14026 /* Convert reloads involving 64-bit gprs and misaligned offset
14027    addressing to use indirect addressing.  */
14028
14029 void
14030 rs6000_secondary_reload_ppc64 (rtx reg, rtx mem, rtx scratch, bool store_p)
14031 {
14032   int regno = true_regnum (reg);
14033   enum reg_class rclass;
14034   rtx addr;
14035   rtx scratch_or_premodify = scratch;
14036
14037   if (TARGET_DEBUG_ADDR)
14038     {
14039       fprintf (stderr, "\nrs6000_secondary_reload_ppc64, type = %s\n",
14040                store_p ? "store" : "load");
14041       fprintf (stderr, "reg:\n");
14042       debug_rtx (reg);
14043       fprintf (stderr, "mem:\n");
14044       debug_rtx (mem);
14045       fprintf (stderr, "scratch:\n");
14046       debug_rtx (scratch);
14047     }
14048
14049   gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
14050   gcc_assert (GET_CODE (mem) == MEM);
14051   rclass = REGNO_REG_CLASS (regno);
14052   gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
14053   addr = XEXP (mem, 0);
14054
14055   if (GET_CODE (addr) == PRE_MODIFY)
14056     {
14057       scratch_or_premodify = XEXP (addr, 0);
14058       gcc_assert (REG_P (scratch_or_premodify));
14059       addr = XEXP (addr, 1);
14060     }
14061   gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
14062
14063   rs6000_emit_move (scratch_or_premodify, addr, Pmode);
14064
14065   mem = replace_equiv_address_nv (mem, scratch_or_premodify);
14066
14067   /* Now create the move.  */
14068   if (store_p)
14069     emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14070   else
14071     emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14072
14073   return;
14074 }
14075
14076 /* Allocate a 64-bit stack slot to be used for copying SDmode
14077    values through if this function has any SDmode references.  */
14078
14079 static void
14080 rs6000_alloc_sdmode_stack_slot (void)
14081 {
14082   tree t;
14083   basic_block bb;
14084   gimple_stmt_iterator gsi;
14085
14086   gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
14087
14088   FOR_EACH_BB (bb)
14089     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
14090       {
14091         tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
14092         if (ret)
14093           {
14094             rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14095             cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14096                                                                   SDmode, 0);
14097             return;
14098           }
14099       }
14100
14101   /* Check for any SDmode parameters of the function.  */
14102   for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
14103     {
14104       if (TREE_TYPE (t) == error_mark_node)
14105         continue;
14106
14107       if (TYPE_MODE (TREE_TYPE (t)) == SDmode
14108           || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
14109         {
14110           rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14111           cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14112                                                                 SDmode, 0);
14113           return;
14114         }
14115     }
14116 }
14117
14118 static void
14119 rs6000_instantiate_decls (void)
14120 {
14121   if (cfun->machine->sdmode_stack_slot != NULL_RTX)
14122     instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
14123 }
14124
14125 /* Given an rtx X being reloaded into a reg required to be
14126    in class CLASS, return the class of reg to actually use.
14127    In general this is just CLASS; but on some machines
14128    in some cases it is preferable to use a more restrictive class.
14129
14130    On the RS/6000, we have to return NO_REGS when we want to reload a
14131    floating-point CONST_DOUBLE to force it to be copied to memory.
14132
14133    We also don't want to reload integer values into floating-point
14134    registers if we can at all help it.  In fact, this can
14135    cause reload to die, if it tries to generate a reload of CTR
14136    into a FP register and discovers it doesn't have the memory location
14137    required.
14138
14139    ??? Would it be a good idea to have reload do the converse, that is
14140    try to reload floating modes into FP registers if possible?
14141  */
14142
14143 static enum reg_class
14144 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
14145 {
14146   enum machine_mode mode = GET_MODE (x);
14147
14148   if (VECTOR_UNIT_VSX_P (mode)
14149       && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
14150     return rclass;
14151
14152   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
14153       && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
14154       && easy_vector_constant (x, mode))
14155     return ALTIVEC_REGS;
14156
14157   if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
14158     return NO_REGS;
14159
14160   if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
14161     return GENERAL_REGS;
14162
14163   /* For VSX, prefer the traditional registers for 64-bit values because we can
14164      use the non-VSX loads.  Prefer the Altivec registers if Altivec is
14165      handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
14166      prefer Altivec loads..  */
14167   if (rclass == VSX_REGS)
14168     {
14169       if (GET_MODE_SIZE (mode) <= 8)
14170         return FLOAT_REGS;
14171
14172       if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
14173         return ALTIVEC_REGS;
14174
14175       return rclass;
14176     }
14177
14178   return rclass;
14179 }
14180
14181 /* Debug version of rs6000_preferred_reload_class.  */
14182 static enum reg_class
14183 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
14184 {
14185   enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
14186
14187   fprintf (stderr,
14188            "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
14189            "mode = %s, x:\n",
14190            reg_class_names[ret], reg_class_names[rclass],
14191            GET_MODE_NAME (GET_MODE (x)));
14192   debug_rtx (x);
14193
14194   return ret;
14195 }
14196
14197 /* If we are copying between FP or AltiVec registers and anything else, we need
14198    a memory location.  The exception is when we are targeting ppc64 and the
14199    move to/from fpr to gpr instructions are available.  Also, under VSX, you
14200    can copy vector registers from the FP register set to the Altivec register
14201    set and vice versa.  */
14202
14203 static bool
14204 rs6000_secondary_memory_needed (enum reg_class class1,
14205                                 enum reg_class class2,
14206                                 enum machine_mode mode)
14207 {
14208   if (class1 == class2)
14209     return false;
14210
14211   /* Under VSX, there are 3 register classes that values could be in (VSX_REGS,
14212      ALTIVEC_REGS, and FLOAT_REGS).  We don't need to use memory to copy
14213      between these classes.  But we need memory for other things that can go in
14214      FLOAT_REGS like SFmode.  */
14215   if (TARGET_VSX
14216       && (VECTOR_MEM_VSX_P (mode) || VECTOR_UNIT_VSX_P (mode))
14217       && (class1 == VSX_REGS || class1 == ALTIVEC_REGS
14218           || class1 == FLOAT_REGS))
14219     return (class2 != VSX_REGS && class2 != ALTIVEC_REGS
14220             && class2 != FLOAT_REGS);
14221
14222   if (class1 == VSX_REGS || class2 == VSX_REGS)
14223     return true;
14224
14225   if (class1 == FLOAT_REGS
14226       && (!TARGET_MFPGPR || !TARGET_POWERPC64
14227           || ((mode != DFmode)
14228               && (mode != DDmode)
14229               && (mode != DImode))))
14230     return true;
14231
14232   if (class2 == FLOAT_REGS
14233       && (!TARGET_MFPGPR || !TARGET_POWERPC64
14234           || ((mode != DFmode)
14235               && (mode != DDmode)
14236               && (mode != DImode))))
14237     return true;
14238
14239   if (class1 == ALTIVEC_REGS || class2 == ALTIVEC_REGS)
14240     return true;
14241
14242   return false;
14243 }
14244
14245 /* Debug version of rs6000_secondary_memory_needed.  */
14246 static bool
14247 rs6000_debug_secondary_memory_needed (enum reg_class class1,
14248                                       enum reg_class class2,
14249                                       enum machine_mode mode)
14250 {
14251   bool ret = rs6000_secondary_memory_needed (class1, class2, mode);
14252
14253   fprintf (stderr,
14254            "rs6000_secondary_memory_needed, return: %s, class1 = %s, "
14255            "class2 = %s, mode = %s\n",
14256            ret ? "true" : "false", reg_class_names[class1],
14257            reg_class_names[class2], GET_MODE_NAME (mode));
14258
14259   return ret;
14260 }
14261
14262 /* Return the register class of a scratch register needed to copy IN into
14263    or out of a register in RCLASS in MODE.  If it can be done directly,
14264    NO_REGS is returned.  */
14265
14266 static enum reg_class
14267 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
14268                                rtx in)
14269 {
14270   int regno;
14271
14272   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
14273 #if TARGET_MACHO
14274                      && MACHOPIC_INDIRECT
14275 #endif
14276                      ))
14277     {
14278       /* We cannot copy a symbolic operand directly into anything
14279          other than BASE_REGS for TARGET_ELF.  So indicate that a
14280          register from BASE_REGS is needed as an intermediate
14281          register.
14282
14283          On Darwin, pic addresses require a load from memory, which
14284          needs a base register.  */
14285       if (rclass != BASE_REGS
14286           && (GET_CODE (in) == SYMBOL_REF
14287               || GET_CODE (in) == HIGH
14288               || GET_CODE (in) == LABEL_REF
14289               || GET_CODE (in) == CONST))
14290         return BASE_REGS;
14291     }
14292
14293   if (GET_CODE (in) == REG)
14294     {
14295       regno = REGNO (in);
14296       if (regno >= FIRST_PSEUDO_REGISTER)
14297         {
14298           regno = true_regnum (in);
14299           if (regno >= FIRST_PSEUDO_REGISTER)
14300             regno = -1;
14301         }
14302     }
14303   else if (GET_CODE (in) == SUBREG)
14304     {
14305       regno = true_regnum (in);
14306       if (regno >= FIRST_PSEUDO_REGISTER)
14307         regno = -1;
14308     }
14309   else
14310     regno = -1;
14311
14312   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
14313      into anything.  */
14314   if (rclass == GENERAL_REGS || rclass == BASE_REGS
14315       || (regno >= 0 && INT_REGNO_P (regno)))
14316     return NO_REGS;
14317
14318   /* Constants, memory, and FP registers can go into FP registers.  */
14319   if ((regno == -1 || FP_REGNO_P (regno))
14320       && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
14321     return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
14322
14323   /* Memory, and FP/altivec registers can go into fp/altivec registers under
14324      VSX.  */
14325   if (TARGET_VSX
14326       && (regno == -1 || VSX_REGNO_P (regno))
14327       && VSX_REG_CLASS_P (rclass))
14328     return NO_REGS;
14329
14330   /* Memory, and AltiVec registers can go into AltiVec registers.  */
14331   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
14332       && rclass == ALTIVEC_REGS)
14333     return NO_REGS;
14334
14335   /* We can copy among the CR registers.  */
14336   if ((rclass == CR_REGS || rclass == CR0_REGS)
14337       && regno >= 0 && CR_REGNO_P (regno))
14338     return NO_REGS;
14339
14340   /* Otherwise, we need GENERAL_REGS.  */
14341   return GENERAL_REGS;
14342 }
14343
14344 /* Debug version of rs6000_secondary_reload_class.  */
14345 static enum reg_class
14346 rs6000_debug_secondary_reload_class (enum reg_class rclass,
14347                                      enum machine_mode mode, rtx in)
14348 {
14349   enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
14350   fprintf (stderr,
14351            "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
14352            "mode = %s, input rtx:\n",
14353            reg_class_names[ret], reg_class_names[rclass],
14354            GET_MODE_NAME (mode));
14355   debug_rtx (in);
14356
14357   return ret;
14358 }
14359
14360 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid.  */
14361
14362 static bool
14363 rs6000_cannot_change_mode_class (enum machine_mode from,
14364                                  enum machine_mode to,
14365                                  enum reg_class rclass)
14366 {
14367   unsigned from_size = GET_MODE_SIZE (from);
14368   unsigned to_size = GET_MODE_SIZE (to);
14369
14370   if (from_size != to_size)
14371     {
14372       enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
14373       return ((from_size < 8 || to_size < 8 || TARGET_IEEEQUAD)
14374               && reg_classes_intersect_p (xclass, rclass));
14375     }
14376
14377   if (TARGET_E500_DOUBLE
14378       && ((((to) == DFmode) + ((from) == DFmode)) == 1
14379           || (((to) == TFmode) + ((from) == TFmode)) == 1
14380           || (((to) == DDmode) + ((from) == DDmode)) == 1
14381           || (((to) == TDmode) + ((from) == TDmode)) == 1
14382           || (((to) == DImode) + ((from) == DImode)) == 1))
14383     return true;
14384
14385   /* Since the VSX register set includes traditional floating point registers
14386      and altivec registers, just check for the size being different instead of
14387      trying to check whether the modes are vector modes.  Otherwise it won't
14388      allow say DF and DI to change classes.  */
14389   if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
14390     return (from_size != 8 && from_size != 16);
14391
14392   if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
14393       && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
14394     return true;
14395
14396   if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
14397       && reg_classes_intersect_p (GENERAL_REGS, rclass))
14398     return true;
14399
14400   return false;
14401 }
14402
14403 /* Debug version of rs6000_cannot_change_mode_class.  */
14404 static bool
14405 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
14406                                        enum machine_mode to,
14407                                        enum reg_class rclass)
14408 {
14409   bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
14410
14411   fprintf (stderr,
14412            "rs6000_cannot_change_mode_class, return %s, from = %s, "
14413            "to = %s, rclass = %s\n",
14414            ret ? "true" : "false",
14415            GET_MODE_NAME (from), GET_MODE_NAME (to),
14416            reg_class_names[rclass]);
14417
14418   return ret;
14419 }
14420 \f
14421 /* Given a comparison operation, return the bit number in CCR to test.  We
14422    know this is a valid comparison.
14423
14424    SCC_P is 1 if this is for an scc.  That means that %D will have been
14425    used instead of %C, so the bits will be in different places.
14426
14427    Return -1 if OP isn't a valid comparison for some reason.  */
14428
14429 int
14430 ccr_bit (rtx op, int scc_p)
14431 {
14432   enum rtx_code code = GET_CODE (op);
14433   enum machine_mode cc_mode;
14434   int cc_regnum;
14435   int base_bit;
14436   rtx reg;
14437
14438   if (!COMPARISON_P (op))
14439     return -1;
14440
14441   reg = XEXP (op, 0);
14442
14443   gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
14444
14445   cc_mode = GET_MODE (reg);
14446   cc_regnum = REGNO (reg);
14447   base_bit = 4 * (cc_regnum - CR0_REGNO);
14448
14449   validate_condition_mode (code, cc_mode);
14450
14451   /* When generating a sCOND operation, only positive conditions are
14452      allowed.  */
14453   gcc_assert (!scc_p
14454               || code == EQ || code == GT || code == LT || code == UNORDERED
14455               || code == GTU || code == LTU);
14456
14457   switch (code)
14458     {
14459     case NE:
14460       return scc_p ? base_bit + 3 : base_bit + 2;
14461     case EQ:
14462       return base_bit + 2;
14463     case GT:  case GTU:  case UNLE:
14464       return base_bit + 1;
14465     case LT:  case LTU:  case UNGE:
14466       return base_bit;
14467     case ORDERED:  case UNORDERED:
14468       return base_bit + 3;
14469
14470     case GE:  case GEU:
14471       /* If scc, we will have done a cror to put the bit in the
14472          unordered position.  So test that bit.  For integer, this is ! LT
14473          unless this is an scc insn.  */
14474       return scc_p ? base_bit + 3 : base_bit;
14475
14476     case LE:  case LEU:
14477       return scc_p ? base_bit + 3 : base_bit + 1;
14478
14479     default:
14480       gcc_unreachable ();
14481     }
14482 }
14483 \f
14484 /* Return the GOT register.  */
14485
14486 rtx
14487 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
14488 {
14489   /* The second flow pass currently (June 1999) can't update
14490      regs_ever_live without disturbing other parts of the compiler, so
14491      update it here to make the prolog/epilogue code happy.  */
14492   if (!can_create_pseudo_p ()
14493       && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
14494     df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
14495
14496   crtl->uses_pic_offset_table = 1;
14497
14498   return pic_offset_table_rtx;
14499 }
14500 \f
14501 static rs6000_stack_t stack_info;
14502
14503 /* Function to init struct machine_function.
14504    This will be called, via a pointer variable,
14505    from push_function_context.  */
14506
14507 static struct machine_function *
14508 rs6000_init_machine_status (void)
14509 {
14510   stack_info.reload_completed = 0;
14511   return ggc_alloc_cleared_machine_function ();
14512 }
14513 \f
14514 /* These macros test for integers and extract the low-order bits.  */
14515 #define INT_P(X)  \
14516 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
14517  && GET_MODE (X) == VOIDmode)
14518
14519 #define INT_LOWPART(X) \
14520   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
14521
14522 int
14523 extract_MB (rtx op)
14524 {
14525   int i;
14526   unsigned long val = INT_LOWPART (op);
14527
14528   /* If the high bit is zero, the value is the first 1 bit we find
14529      from the left.  */
14530   if ((val & 0x80000000) == 0)
14531     {
14532       gcc_assert (val & 0xffffffff);
14533
14534       i = 1;
14535       while (((val <<= 1) & 0x80000000) == 0)
14536         ++i;
14537       return i;
14538     }
14539
14540   /* If the high bit is set and the low bit is not, or the mask is all
14541      1's, the value is zero.  */
14542   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
14543     return 0;
14544
14545   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
14546      from the right.  */
14547   i = 31;
14548   while (((val >>= 1) & 1) != 0)
14549     --i;
14550
14551   return i;
14552 }
14553
14554 int
14555 extract_ME (rtx op)
14556 {
14557   int i;
14558   unsigned long val = INT_LOWPART (op);
14559
14560   /* If the low bit is zero, the value is the first 1 bit we find from
14561      the right.  */
14562   if ((val & 1) == 0)
14563     {
14564       gcc_assert (val & 0xffffffff);
14565
14566       i = 30;
14567       while (((val >>= 1) & 1) == 0)
14568         --i;
14569
14570       return i;
14571     }
14572
14573   /* If the low bit is set and the high bit is not, or the mask is all
14574      1's, the value is 31.  */
14575   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
14576     return 31;
14577
14578   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
14579      from the left.  */
14580   i = 0;
14581   while (((val <<= 1) & 0x80000000) != 0)
14582     ++i;
14583
14584   return i;
14585 }
14586
14587 /* Locate some local-dynamic symbol still in use by this function
14588    so that we can print its name in some tls_ld pattern.  */
14589
14590 static const char *
14591 rs6000_get_some_local_dynamic_name (void)
14592 {
14593   rtx insn;
14594
14595   if (cfun->machine->some_ld_name)
14596     return cfun->machine->some_ld_name;
14597
14598   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
14599     if (INSN_P (insn)
14600         && for_each_rtx (&PATTERN (insn),
14601                          rs6000_get_some_local_dynamic_name_1, 0))
14602       return cfun->machine->some_ld_name;
14603
14604   gcc_unreachable ();
14605 }
14606
14607 /* Helper function for rs6000_get_some_local_dynamic_name.  */
14608
14609 static int
14610 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
14611 {
14612   rtx x = *px;
14613
14614   if (GET_CODE (x) == SYMBOL_REF)
14615     {
14616       const char *str = XSTR (x, 0);
14617       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
14618         {
14619           cfun->machine->some_ld_name = str;
14620           return 1;
14621         }
14622     }
14623
14624   return 0;
14625 }
14626
14627 /* Write out a function code label.  */
14628
14629 void
14630 rs6000_output_function_entry (FILE *file, const char *fname)
14631 {
14632   if (fname[0] != '.')
14633     {
14634       switch (DEFAULT_ABI)
14635         {
14636         default:
14637           gcc_unreachable ();
14638
14639         case ABI_AIX:
14640           if (DOT_SYMBOLS)
14641             putc ('.', file);
14642           else
14643             ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
14644           break;
14645
14646         case ABI_V4:
14647         case ABI_DARWIN:
14648           break;
14649         }
14650     }
14651
14652   RS6000_OUTPUT_BASENAME (file, fname);
14653 }
14654
14655 /* Print an operand.  Recognize special options, documented below.  */
14656
14657 #if TARGET_ELF
14658 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
14659 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
14660 #else
14661 #define SMALL_DATA_RELOC "sda21"
14662 #define SMALL_DATA_REG 0
14663 #endif
14664
14665 void
14666 print_operand (FILE *file, rtx x, int code)
14667 {
14668   int i;
14669   HOST_WIDE_INT val;
14670   unsigned HOST_WIDE_INT uval;
14671
14672   switch (code)
14673     {
14674     case '.':
14675       /* Write out an instruction after the call which may be replaced
14676          with glue code by the loader.  This depends on the AIX version.  */
14677       asm_fprintf (file, RS6000_CALL_GLUE);
14678       return;
14679
14680       /* %a is output_address.  */
14681
14682     case 'A':
14683       /* If X is a constant integer whose low-order 5 bits are zero,
14684          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
14685          in the AIX assembler where "sri" with a zero shift count
14686          writes a trash instruction.  */
14687       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
14688         putc ('l', file);
14689       else
14690         putc ('r', file);
14691       return;
14692
14693     case 'b':
14694       /* If constant, low-order 16 bits of constant, unsigned.
14695          Otherwise, write normally.  */
14696       if (INT_P (x))
14697         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
14698       else
14699         print_operand (file, x, 0);
14700       return;
14701
14702     case 'B':
14703       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
14704          for 64-bit mask direction.  */
14705       putc (((INT_LOWPART (x) & 1) == 0 ? 'r' : 'l'), file);
14706       return;
14707
14708       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
14709          output_operand.  */
14710
14711     case 'c':
14712       /* X is a CR register.  Print the number of the GT bit of the CR.  */
14713       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14714         output_operand_lossage ("invalid %%c value");
14715       else
14716         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 1);
14717       return;
14718
14719     case 'D':
14720       /* Like 'J' but get to the GT bit only.  */
14721       gcc_assert (GET_CODE (x) == REG);
14722
14723       /* Bit 1 is GT bit.  */
14724       i = 4 * (REGNO (x) - CR0_REGNO) + 1;
14725
14726       /* Add one for shift count in rlinm for scc.  */
14727       fprintf (file, "%d", i + 1);
14728       return;
14729
14730     case 'E':
14731       /* X is a CR register.  Print the number of the EQ bit of the CR */
14732       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14733         output_operand_lossage ("invalid %%E value");
14734       else
14735         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
14736       return;
14737
14738     case 'f':
14739       /* X is a CR register.  Print the shift count needed to move it
14740          to the high-order four bits.  */
14741       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14742         output_operand_lossage ("invalid %%f value");
14743       else
14744         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
14745       return;
14746
14747     case 'F':
14748       /* Similar, but print the count for the rotate in the opposite
14749          direction.  */
14750       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14751         output_operand_lossage ("invalid %%F value");
14752       else
14753         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
14754       return;
14755
14756     case 'G':
14757       /* X is a constant integer.  If it is negative, print "m",
14758          otherwise print "z".  This is to make an aze or ame insn.  */
14759       if (GET_CODE (x) != CONST_INT)
14760         output_operand_lossage ("invalid %%G value");
14761       else if (INTVAL (x) >= 0)
14762         putc ('z', file);
14763       else
14764         putc ('m', file);
14765       return;
14766
14767     case 'h':
14768       /* If constant, output low-order five bits.  Otherwise, write
14769          normally.  */
14770       if (INT_P (x))
14771         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
14772       else
14773         print_operand (file, x, 0);
14774       return;
14775
14776     case 'H':
14777       /* If constant, output low-order six bits.  Otherwise, write
14778          normally.  */
14779       if (INT_P (x))
14780         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
14781       else
14782         print_operand (file, x, 0);
14783       return;
14784
14785     case 'I':
14786       /* Print `i' if this is a constant, else nothing.  */
14787       if (INT_P (x))
14788         putc ('i', file);
14789       return;
14790
14791     case 'j':
14792       /* Write the bit number in CCR for jump.  */
14793       i = ccr_bit (x, 0);
14794       if (i == -1)
14795         output_operand_lossage ("invalid %%j code");
14796       else
14797         fprintf (file, "%d", i);
14798       return;
14799
14800     case 'J':
14801       /* Similar, but add one for shift count in rlinm for scc and pass
14802          scc flag to `ccr_bit'.  */
14803       i = ccr_bit (x, 1);
14804       if (i == -1)
14805         output_operand_lossage ("invalid %%J code");
14806       else
14807         /* If we want bit 31, write a shift count of zero, not 32.  */
14808         fprintf (file, "%d", i == 31 ? 0 : i + 1);
14809       return;
14810
14811     case 'k':
14812       /* X must be a constant.  Write the 1's complement of the
14813          constant.  */
14814       if (! INT_P (x))
14815         output_operand_lossage ("invalid %%k value");
14816       else
14817         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
14818       return;
14819
14820     case 'K':
14821       /* X must be a symbolic constant on ELF.  Write an
14822          expression suitable for an 'addi' that adds in the low 16
14823          bits of the MEM.  */
14824       if (GET_CODE (x) == CONST)
14825         {
14826           if (GET_CODE (XEXP (x, 0)) != PLUS
14827               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
14828                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
14829               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
14830             output_operand_lossage ("invalid %%K value");
14831         }
14832       print_operand_address (file, x);
14833       fputs ("@l", file);
14834       return;
14835
14836       /* %l is output_asm_label.  */
14837
14838     case 'L':
14839       /* Write second word of DImode or DFmode reference.  Works on register
14840          or non-indexed memory only.  */
14841       if (GET_CODE (x) == REG)
14842         fputs (reg_names[REGNO (x) + 1], file);
14843       else if (GET_CODE (x) == MEM)
14844         {
14845           /* Handle possible auto-increment.  Since it is pre-increment and
14846              we have already done it, we can just use an offset of word.  */
14847           if (GET_CODE (XEXP (x, 0)) == PRE_INC
14848               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
14849             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
14850                                            UNITS_PER_WORD));
14851           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
14852             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
14853                                            UNITS_PER_WORD));
14854           else
14855             output_address (XEXP (adjust_address_nv (x, SImode,
14856                                                      UNITS_PER_WORD),
14857                                   0));
14858
14859           if (small_data_operand (x, GET_MODE (x)))
14860             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
14861                      reg_names[SMALL_DATA_REG]);
14862         }
14863       return;
14864
14865     case 'm':
14866       /* MB value for a mask operand.  */
14867       if (! mask_operand (x, SImode))
14868         output_operand_lossage ("invalid %%m value");
14869
14870       fprintf (file, "%d", extract_MB (x));
14871       return;
14872
14873     case 'M':
14874       /* ME value for a mask operand.  */
14875       if (! mask_operand (x, SImode))
14876         output_operand_lossage ("invalid %%M value");
14877
14878       fprintf (file, "%d", extract_ME (x));
14879       return;
14880
14881       /* %n outputs the negative of its operand.  */
14882
14883     case 'N':
14884       /* Write the number of elements in the vector times 4.  */
14885       if (GET_CODE (x) != PARALLEL)
14886         output_operand_lossage ("invalid %%N value");
14887       else
14888         fprintf (file, "%d", XVECLEN (x, 0) * 4);
14889       return;
14890
14891     case 'O':
14892       /* Similar, but subtract 1 first.  */
14893       if (GET_CODE (x) != PARALLEL)
14894         output_operand_lossage ("invalid %%O value");
14895       else
14896         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
14897       return;
14898
14899     case 'p':
14900       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
14901       if (! INT_P (x)
14902           || INT_LOWPART (x) < 0
14903           || (i = exact_log2 (INT_LOWPART (x))) < 0)
14904         output_operand_lossage ("invalid %%p value");
14905       else
14906         fprintf (file, "%d", i);
14907       return;
14908
14909     case 'P':
14910       /* The operand must be an indirect memory reference.  The result
14911          is the register name.  */
14912       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
14913           || REGNO (XEXP (x, 0)) >= 32)
14914         output_operand_lossage ("invalid %%P value");
14915       else
14916         fputs (reg_names[REGNO (XEXP (x, 0))], file);
14917       return;
14918
14919     case 'q':
14920       /* This outputs the logical code corresponding to a boolean
14921          expression.  The expression may have one or both operands
14922          negated (if one, only the first one).  For condition register
14923          logical operations, it will also treat the negated
14924          CR codes as NOTs, but not handle NOTs of them.  */
14925       {
14926         const char *const *t = 0;
14927         const char *s;
14928         enum rtx_code code = GET_CODE (x);
14929         static const char * const tbl[3][3] = {
14930           { "and", "andc", "nor" },
14931           { "or", "orc", "nand" },
14932           { "xor", "eqv", "xor" } };
14933
14934         if (code == AND)
14935           t = tbl[0];
14936         else if (code == IOR)
14937           t = tbl[1];
14938         else if (code == XOR)
14939           t = tbl[2];
14940         else
14941           output_operand_lossage ("invalid %%q value");
14942
14943         if (GET_CODE (XEXP (x, 0)) != NOT)
14944           s = t[0];
14945         else
14946           {
14947             if (GET_CODE (XEXP (x, 1)) == NOT)
14948               s = t[2];
14949             else
14950               s = t[1];
14951           }
14952
14953         fputs (s, file);
14954       }
14955       return;
14956
14957     case 'Q':
14958       if (TARGET_MFCRF)
14959         fputc (',', file);
14960         /* FALLTHRU */
14961       else
14962         return;
14963
14964     case 'R':
14965       /* X is a CR register.  Print the mask for `mtcrf'.  */
14966       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14967         output_operand_lossage ("invalid %%R value");
14968       else
14969         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
14970       return;
14971
14972     case 's':
14973       /* Low 5 bits of 32 - value */
14974       if (! INT_P (x))
14975         output_operand_lossage ("invalid %%s value");
14976       else
14977         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
14978       return;
14979
14980     case 'S':
14981       /* PowerPC64 mask position.  All 0's is excluded.
14982          CONST_INT 32-bit mask is considered sign-extended so any
14983          transition must occur within the CONST_INT, not on the boundary.  */
14984       if (! mask64_operand (x, DImode))
14985         output_operand_lossage ("invalid %%S value");
14986
14987       uval = INT_LOWPART (x);
14988
14989       if (uval & 1)     /* Clear Left */
14990         {
14991 #if HOST_BITS_PER_WIDE_INT > 64
14992           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
14993 #endif
14994           i = 64;
14995         }
14996       else              /* Clear Right */
14997         {
14998           uval = ~uval;
14999 #if HOST_BITS_PER_WIDE_INT > 64
15000           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
15001 #endif
15002           i = 63;
15003         }
15004       while (uval != 0)
15005         --i, uval >>= 1;
15006       gcc_assert (i >= 0);
15007       fprintf (file, "%d", i);
15008       return;
15009
15010     case 't':
15011       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
15012       gcc_assert (GET_CODE (x) == REG && GET_MODE (x) == CCmode);
15013
15014       /* Bit 3 is OV bit.  */
15015       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
15016
15017       /* If we want bit 31, write a shift count of zero, not 32.  */
15018       fprintf (file, "%d", i == 31 ? 0 : i + 1);
15019       return;
15020
15021     case 'T':
15022       /* Print the symbolic name of a branch target register.  */
15023       if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
15024                                   && REGNO (x) != CTR_REGNO))
15025         output_operand_lossage ("invalid %%T value");
15026       else if (REGNO (x) == LR_REGNO)
15027         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
15028       else
15029         fputs ("ctr", file);
15030       return;
15031
15032     case 'u':
15033       /* High-order 16 bits of constant for use in unsigned operand.  */
15034       if (! INT_P (x))
15035         output_operand_lossage ("invalid %%u value");
15036       else
15037         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15038                  (INT_LOWPART (x) >> 16) & 0xffff);
15039       return;
15040
15041     case 'v':
15042       /* High-order 16 bits of constant for use in signed operand.  */
15043       if (! INT_P (x))
15044         output_operand_lossage ("invalid %%v value");
15045       else
15046         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15047                  (INT_LOWPART (x) >> 16) & 0xffff);
15048       return;
15049
15050     case 'U':
15051       /* Print `u' if this has an auto-increment or auto-decrement.  */
15052       if (GET_CODE (x) == MEM
15053           && (GET_CODE (XEXP (x, 0)) == PRE_INC
15054               || GET_CODE (XEXP (x, 0)) == PRE_DEC
15055               || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
15056         putc ('u', file);
15057       return;
15058
15059     case 'V':
15060       /* Print the trap code for this operand.  */
15061       switch (GET_CODE (x))
15062         {
15063         case EQ:
15064           fputs ("eq", file);   /* 4 */
15065           break;
15066         case NE:
15067           fputs ("ne", file);   /* 24 */
15068           break;
15069         case LT:
15070           fputs ("lt", file);   /* 16 */
15071           break;
15072         case LE:
15073           fputs ("le", file);   /* 20 */
15074           break;
15075         case GT:
15076           fputs ("gt", file);   /* 8 */
15077           break;
15078         case GE:
15079           fputs ("ge", file);   /* 12 */
15080           break;
15081         case LTU:
15082           fputs ("llt", file);  /* 2 */
15083           break;
15084         case LEU:
15085           fputs ("lle", file);  /* 6 */
15086           break;
15087         case GTU:
15088           fputs ("lgt", file);  /* 1 */
15089           break;
15090         case GEU:
15091           fputs ("lge", file);  /* 5 */
15092           break;
15093         default:
15094           gcc_unreachable ();
15095         }
15096       break;
15097
15098     case 'w':
15099       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
15100          normally.  */
15101       if (INT_P (x))
15102         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
15103                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
15104       else
15105         print_operand (file, x, 0);
15106       return;
15107
15108     case 'W':
15109       /* MB value for a PowerPC64 rldic operand.  */
15110       val = (GET_CODE (x) == CONST_INT
15111              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
15112
15113       if (val < 0)
15114         i = -1;
15115       else
15116         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
15117           if ((val <<= 1) < 0)
15118             break;
15119
15120 #if HOST_BITS_PER_WIDE_INT == 32
15121       if (GET_CODE (x) == CONST_INT && i >= 0)
15122         i += 32;  /* zero-extend high-part was all 0's */
15123       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
15124         {
15125           val = CONST_DOUBLE_LOW (x);
15126
15127           gcc_assert (val);
15128           if (val < 0)
15129             --i;
15130           else
15131             for ( ; i < 64; i++)
15132               if ((val <<= 1) < 0)
15133                 break;
15134         }
15135 #endif
15136
15137       fprintf (file, "%d", i + 1);
15138       return;
15139
15140     case 'x':
15141       /* X is a FPR or Altivec register used in a VSX context.  */
15142       if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
15143         output_operand_lossage ("invalid %%x value");
15144       else
15145         {
15146           int reg = REGNO (x);
15147           int vsx_reg = (FP_REGNO_P (reg)
15148                          ? reg - 32
15149                          : reg - FIRST_ALTIVEC_REGNO + 32);
15150
15151 #ifdef TARGET_REGNAMES      
15152           if (TARGET_REGNAMES)
15153             fprintf (file, "%%vs%d", vsx_reg);
15154           else
15155 #endif
15156             fprintf (file, "%d", vsx_reg);
15157         }
15158       return;
15159
15160     case 'X':
15161       if (GET_CODE (x) == MEM
15162           && (legitimate_indexed_address_p (XEXP (x, 0), 0)
15163               || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
15164                   && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
15165         putc ('x', file);
15166       return;
15167
15168     case 'Y':
15169       /* Like 'L', for third word of TImode  */
15170       if (GET_CODE (x) == REG)
15171         fputs (reg_names[REGNO (x) + 2], file);
15172       else if (GET_CODE (x) == MEM)
15173         {
15174           if (GET_CODE (XEXP (x, 0)) == PRE_INC
15175               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15176             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
15177           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15178             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
15179           else
15180             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
15181           if (small_data_operand (x, GET_MODE (x)))
15182             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15183                      reg_names[SMALL_DATA_REG]);
15184         }
15185       return;
15186
15187     case 'z':
15188       /* X is a SYMBOL_REF.  Write out the name preceded by a
15189          period and without any trailing data in brackets.  Used for function
15190          names.  If we are configured for System V (or the embedded ABI) on
15191          the PowerPC, do not emit the period, since those systems do not use
15192          TOCs and the like.  */
15193       gcc_assert (GET_CODE (x) == SYMBOL_REF);
15194
15195       /* Mark the decl as referenced so that cgraph will output the
15196          function.  */
15197       if (SYMBOL_REF_DECL (x))
15198         mark_decl_referenced (SYMBOL_REF_DECL (x));
15199
15200       /* For macho, check to see if we need a stub.  */
15201       if (TARGET_MACHO)
15202         {
15203           const char *name = XSTR (x, 0);
15204 #if TARGET_MACHO
15205           if (darwin_emit_branch_islands
15206               && MACHOPIC_INDIRECT
15207               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
15208             name = machopic_indirection_name (x, /*stub_p=*/true);
15209 #endif
15210           assemble_name (file, name);
15211         }
15212       else if (!DOT_SYMBOLS)
15213         assemble_name (file, XSTR (x, 0));
15214       else
15215         rs6000_output_function_entry (file, XSTR (x, 0));
15216       return;
15217
15218     case 'Z':
15219       /* Like 'L', for last word of TImode.  */
15220       if (GET_CODE (x) == REG)
15221         fputs (reg_names[REGNO (x) + 3], file);
15222       else if (GET_CODE (x) == MEM)
15223         {
15224           if (GET_CODE (XEXP (x, 0)) == PRE_INC
15225               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15226             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
15227           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15228             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
15229           else
15230             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
15231           if (small_data_operand (x, GET_MODE (x)))
15232             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15233                      reg_names[SMALL_DATA_REG]);
15234         }
15235       return;
15236
15237       /* Print AltiVec or SPE memory operand.  */
15238     case 'y':
15239       {
15240         rtx tmp;
15241
15242         gcc_assert (GET_CODE (x) == MEM);
15243
15244         tmp = XEXP (x, 0);
15245
15246         /* Ugly hack because %y is overloaded.  */
15247         if ((TARGET_SPE || TARGET_E500_DOUBLE)
15248             && (GET_MODE_SIZE (GET_MODE (x)) == 8
15249                 || GET_MODE (x) == TFmode
15250                 || GET_MODE (x) == TImode))
15251           {
15252             /* Handle [reg].  */
15253             if (GET_CODE (tmp) == REG)
15254               {
15255                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
15256                 break;
15257               }
15258             /* Handle [reg+UIMM].  */
15259             else if (GET_CODE (tmp) == PLUS &&
15260                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
15261               {
15262                 int x;
15263
15264                 gcc_assert (GET_CODE (XEXP (tmp, 0)) == REG);
15265
15266                 x = INTVAL (XEXP (tmp, 1));
15267                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
15268                 break;
15269               }
15270
15271             /* Fall through.  Must be [reg+reg].  */
15272           }
15273         if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
15274             && GET_CODE (tmp) == AND
15275             && GET_CODE (XEXP (tmp, 1)) == CONST_INT
15276             && INTVAL (XEXP (tmp, 1)) == -16)
15277           tmp = XEXP (tmp, 0);
15278         else if (VECTOR_MEM_VSX_P (GET_MODE (x))
15279                  && GET_CODE (tmp) == PRE_MODIFY)
15280           tmp = XEXP (tmp, 1);
15281         if (GET_CODE (tmp) == REG)
15282           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
15283         else
15284           {
15285             if (!GET_CODE (tmp) == PLUS
15286                 || !REG_P (XEXP (tmp, 0))
15287                 || !REG_P (XEXP (tmp, 1)))
15288               {
15289                 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
15290                 break;
15291               }
15292
15293             if (REGNO (XEXP (tmp, 0)) == 0)
15294               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
15295                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
15296             else
15297               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
15298                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
15299           }
15300         break;
15301       }
15302
15303     case 0:
15304       if (GET_CODE (x) == REG)
15305         fprintf (file, "%s", reg_names[REGNO (x)]);
15306       else if (GET_CODE (x) == MEM)
15307         {
15308           /* We need to handle PRE_INC and PRE_DEC here, since we need to
15309              know the width from the mode.  */
15310           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
15311             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
15312                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15313           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
15314             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
15315                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15316           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15317             output_address (XEXP (XEXP (x, 0), 1));
15318           else
15319             output_address (XEXP (x, 0));
15320         }
15321       else
15322         {
15323           if (toc_relative_expr_p (x))
15324             /* This hack along with a corresponding hack in
15325                rs6000_output_addr_const_extra arranges to output addends
15326                where the assembler expects to find them.  eg.
15327                (const (plus (unspec [symbol_ref ("x") tocrel]) 4))
15328                without this hack would be output as "x@toc+4".  We
15329                want "x+4@toc".  */
15330             output_addr_const (file, tocrel_base);
15331           else
15332             output_addr_const (file, x);
15333         }
15334       return;
15335
15336     case '&':
15337       assemble_name (file, rs6000_get_some_local_dynamic_name ());
15338       return;
15339
15340     default:
15341       output_operand_lossage ("invalid %%xn code");
15342     }
15343 }
15344 \f
15345 /* Print the address of an operand.  */
15346
15347 void
15348 print_operand_address (FILE *file, rtx x)
15349 {
15350   if (GET_CODE (x) == REG)
15351     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
15352   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
15353            || GET_CODE (x) == LABEL_REF)
15354     {
15355       output_addr_const (file, x);
15356       if (small_data_operand (x, GET_MODE (x)))
15357         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15358                  reg_names[SMALL_DATA_REG]);
15359       else
15360         gcc_assert (!TARGET_TOC);
15361     }
15362   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
15363     {
15364       gcc_assert (REG_P (XEXP (x, 0)));
15365       if (REGNO (XEXP (x, 0)) == 0)
15366         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
15367                  reg_names[ REGNO (XEXP (x, 0)) ]);
15368       else
15369         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
15370                  reg_names[ REGNO (XEXP (x, 1)) ]);
15371     }
15372   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
15373     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
15374              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
15375 #if TARGET_MACHO
15376   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
15377            && CONSTANT_P (XEXP (x, 1)))
15378     {
15379       fprintf (file, "lo16(");
15380       output_addr_const (file, XEXP (x, 1));
15381       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15382     }
15383 #endif
15384   else if (legitimate_constant_pool_address_p (x, QImode, true))
15385     {
15386       /* This hack along with a corresponding hack in
15387          rs6000_output_addr_const_extra arranges to output addends
15388          where the assembler expects to find them.  eg.
15389          (lo_sum (reg 9)
15390          .       (const (plus (unspec [symbol_ref ("x") tocrel]) 8)))
15391          without this hack would be output as "x@toc+8@l(9)".  We
15392          want "x+8@toc@l(9)".  */
15393       output_addr_const (file, tocrel_base);
15394       if (GET_CODE (x) == LO_SUM)
15395         fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15396       else
15397         fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
15398     }
15399 #if TARGET_ELF
15400   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
15401            && CONSTANT_P (XEXP (x, 1)))
15402     {
15403       output_addr_const (file, XEXP (x, 1));
15404       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15405     }
15406 #endif
15407   else
15408     gcc_unreachable ();
15409 }
15410 \f
15411 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA.  */
15412
15413 static bool
15414 rs6000_output_addr_const_extra (FILE *file, rtx x)
15415 {
15416   if (GET_CODE (x) == UNSPEC)
15417     switch (XINT (x, 1))
15418       {
15419       case UNSPEC_TOCREL:
15420         gcc_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF);
15421         output_addr_const (file, XVECEXP (x, 0, 0));
15422         if (x == tocrel_base && tocrel_offset != const0_rtx)
15423           {
15424             if (INTVAL (tocrel_offset) >= 0)
15425               fprintf (file, "+");
15426             output_addr_const (file, tocrel_offset);
15427           }
15428         if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
15429           {
15430             putc ('-', file);
15431             assemble_name (file, toc_label_name);
15432           }
15433         else if (TARGET_ELF)
15434           fputs ("@toc", file);
15435         return true;
15436
15437 #if TARGET_MACHO
15438       case UNSPEC_MACHOPIC_OFFSET:
15439         output_addr_const (file, XVECEXP (x, 0, 0));
15440         putc ('-', file);
15441         machopic_output_function_base_name (file);
15442         return true;
15443 #endif
15444       }
15445   return false;
15446 }
15447 \f
15448 /* Target hook for assembling integer objects.  The PowerPC version has
15449    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
15450    is defined.  It also needs to handle DI-mode objects on 64-bit
15451    targets.  */
15452
15453 static bool
15454 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
15455 {
15456 #ifdef RELOCATABLE_NEEDS_FIXUP
15457   /* Special handling for SI values.  */
15458   if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
15459     {
15460       static int recurse = 0;
15461
15462       /* For -mrelocatable, we mark all addresses that need to be fixed up in
15463          the .fixup section.  Since the TOC section is already relocated, we
15464          don't need to mark it here.  We used to skip the text section, but it
15465          should never be valid for relocated addresses to be placed in the text
15466          section.  */
15467       if (TARGET_RELOCATABLE
15468           && in_section != toc_section
15469           && !recurse
15470           && GET_CODE (x) != CONST_INT
15471           && GET_CODE (x) != CONST_DOUBLE
15472           && CONSTANT_P (x))
15473         {
15474           char buf[256];
15475
15476           recurse = 1;
15477           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
15478           fixuplabelno++;
15479           ASM_OUTPUT_LABEL (asm_out_file, buf);
15480           fprintf (asm_out_file, "\t.long\t(");
15481           output_addr_const (asm_out_file, x);
15482           fprintf (asm_out_file, ")@fixup\n");
15483           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
15484           ASM_OUTPUT_ALIGN (asm_out_file, 2);
15485           fprintf (asm_out_file, "\t.long\t");
15486           assemble_name (asm_out_file, buf);
15487           fprintf (asm_out_file, "\n\t.previous\n");
15488           recurse = 0;
15489           return true;
15490         }
15491       /* Remove initial .'s to turn a -mcall-aixdesc function
15492          address into the address of the descriptor, not the function
15493          itself.  */
15494       else if (GET_CODE (x) == SYMBOL_REF
15495                && XSTR (x, 0)[0] == '.'
15496                && DEFAULT_ABI == ABI_AIX)
15497         {
15498           const char *name = XSTR (x, 0);
15499           while (*name == '.')
15500             name++;
15501
15502           fprintf (asm_out_file, "\t.long\t%s\n", name);
15503           return true;
15504         }
15505     }
15506 #endif /* RELOCATABLE_NEEDS_FIXUP */
15507   return default_assemble_integer (x, size, aligned_p);
15508 }
15509
15510 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
15511 /* Emit an assembler directive to set symbol visibility for DECL to
15512    VISIBILITY_TYPE.  */
15513
15514 static void
15515 rs6000_assemble_visibility (tree decl, int vis)
15516 {
15517   /* Functions need to have their entry point symbol visibility set as
15518      well as their descriptor symbol visibility.  */
15519   if (DEFAULT_ABI == ABI_AIX
15520       && DOT_SYMBOLS
15521       && TREE_CODE (decl) == FUNCTION_DECL)
15522     {
15523       static const char * const visibility_types[] = {
15524         NULL, "internal", "hidden", "protected"
15525       };
15526
15527       const char *name, *type;
15528
15529       name = ((* targetm.strip_name_encoding)
15530               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
15531       type = visibility_types[vis];
15532
15533       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
15534       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
15535     }
15536   else
15537     default_assemble_visibility (decl, vis);
15538 }
15539 #endif
15540 \f
15541 enum rtx_code
15542 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
15543 {
15544   /* Reversal of FP compares takes care -- an ordered compare
15545      becomes an unordered compare and vice versa.  */
15546   if (mode == CCFPmode
15547       && (!flag_finite_math_only
15548           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
15549           || code == UNEQ || code == LTGT))
15550     return reverse_condition_maybe_unordered (code);
15551   else
15552     return reverse_condition (code);
15553 }
15554
15555 /* Generate a compare for CODE.  Return a brand-new rtx that
15556    represents the result of the compare.  */
15557
15558 static rtx
15559 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
15560 {
15561   enum machine_mode comp_mode;
15562   rtx compare_result;
15563   enum rtx_code code = GET_CODE (cmp);
15564   rtx op0 = XEXP (cmp, 0);
15565   rtx op1 = XEXP (cmp, 1);
15566
15567   if (FLOAT_MODE_P (mode))
15568     comp_mode = CCFPmode;
15569   else if (code == GTU || code == LTU
15570            || code == GEU || code == LEU)
15571     comp_mode = CCUNSmode;
15572   else if ((code == EQ || code == NE)
15573            && GET_CODE (op0) == SUBREG
15574            && GET_CODE (op1) == SUBREG
15575            && SUBREG_PROMOTED_UNSIGNED_P (op0)
15576            && SUBREG_PROMOTED_UNSIGNED_P (op1))
15577     /* These are unsigned values, perhaps there will be a later
15578        ordering compare that can be shared with this one.
15579        Unfortunately we cannot detect the signedness of the operands
15580        for non-subregs.  */
15581     comp_mode = CCUNSmode;
15582   else
15583     comp_mode = CCmode;
15584
15585   /* First, the compare.  */
15586   compare_result = gen_reg_rtx (comp_mode);
15587
15588   /* E500 FP compare instructions on the GPRs.  Yuck!  */
15589   if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
15590       && FLOAT_MODE_P (mode))
15591     {
15592       rtx cmp, or_result, compare_result2;
15593       enum machine_mode op_mode = GET_MODE (op0);
15594
15595       if (op_mode == VOIDmode)
15596         op_mode = GET_MODE (op1);
15597
15598       /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
15599          This explains the following mess.  */
15600
15601       switch (code)
15602         {
15603         case EQ: case UNEQ: case NE: case LTGT:
15604           switch (op_mode)
15605             {
15606             case SFmode:
15607               cmp = (flag_finite_math_only && !flag_trapping_math)
15608                 ? gen_tstsfeq_gpr (compare_result, op0, op1)
15609                 : gen_cmpsfeq_gpr (compare_result, op0, op1);
15610               break;
15611
15612             case DFmode:
15613               cmp = (flag_finite_math_only && !flag_trapping_math)
15614                 ? gen_tstdfeq_gpr (compare_result, op0, op1)
15615                 : gen_cmpdfeq_gpr (compare_result, op0, op1);
15616               break;
15617
15618             case TFmode:
15619               cmp = (flag_finite_math_only && !flag_trapping_math)
15620                 ? gen_tsttfeq_gpr (compare_result, op0, op1)
15621                 : gen_cmptfeq_gpr (compare_result, op0, op1);
15622               break;
15623
15624             default:
15625               gcc_unreachable ();
15626             }
15627           break;
15628
15629         case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
15630           switch (op_mode)
15631             {
15632             case SFmode:
15633               cmp = (flag_finite_math_only && !flag_trapping_math)
15634                 ? gen_tstsfgt_gpr (compare_result, op0, op1)
15635                 : gen_cmpsfgt_gpr (compare_result, op0, op1);
15636               break;
15637
15638             case DFmode:
15639               cmp = (flag_finite_math_only && !flag_trapping_math)
15640                 ? gen_tstdfgt_gpr (compare_result, op0, op1)
15641                 : gen_cmpdfgt_gpr (compare_result, op0, op1);
15642               break;
15643
15644             case TFmode:
15645               cmp = (flag_finite_math_only && !flag_trapping_math)
15646                 ? gen_tsttfgt_gpr (compare_result, op0, op1)
15647                 : gen_cmptfgt_gpr (compare_result, op0, op1);
15648               break;
15649
15650             default:
15651               gcc_unreachable ();
15652             }
15653           break;
15654
15655         case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
15656           switch (op_mode)
15657             {
15658             case SFmode:
15659               cmp = (flag_finite_math_only && !flag_trapping_math)
15660                 ? gen_tstsflt_gpr (compare_result, op0, op1)
15661                 : gen_cmpsflt_gpr (compare_result, op0, op1);
15662               break;
15663
15664             case DFmode:
15665               cmp = (flag_finite_math_only && !flag_trapping_math)
15666                 ? gen_tstdflt_gpr (compare_result, op0, op1)
15667                 : gen_cmpdflt_gpr (compare_result, op0, op1);
15668               break;
15669
15670             case TFmode:
15671               cmp = (flag_finite_math_only && !flag_trapping_math)
15672                 ? gen_tsttflt_gpr (compare_result, op0, op1)
15673                 : gen_cmptflt_gpr (compare_result, op0, op1);
15674               break;
15675
15676             default:
15677               gcc_unreachable ();
15678             }
15679           break;
15680         default:
15681           gcc_unreachable ();
15682         }
15683
15684       /* Synthesize LE and GE from LT/GT || EQ.  */
15685       if (code == LE || code == GE || code == LEU || code == GEU)
15686         {
15687           emit_insn (cmp);
15688
15689           switch (code)
15690             {
15691             case LE: code = LT; break;
15692             case GE: code = GT; break;
15693             case LEU: code = LT; break;
15694             case GEU: code = GT; break;
15695             default: gcc_unreachable ();
15696             }
15697
15698           compare_result2 = gen_reg_rtx (CCFPmode);
15699
15700           /* Do the EQ.  */
15701           switch (op_mode)
15702             {
15703             case SFmode:
15704               cmp = (flag_finite_math_only && !flag_trapping_math)
15705                 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
15706                 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
15707               break;
15708
15709             case DFmode:
15710               cmp = (flag_finite_math_only && !flag_trapping_math)
15711                 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
15712                 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
15713               break;
15714
15715             case TFmode:
15716               cmp = (flag_finite_math_only && !flag_trapping_math)
15717                 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
15718                 : gen_cmptfeq_gpr (compare_result2, op0, op1);
15719               break;
15720
15721             default:
15722               gcc_unreachable ();
15723             }
15724           emit_insn (cmp);
15725
15726           /* OR them together.  */
15727           or_result = gen_reg_rtx (CCFPmode);
15728           cmp = gen_e500_cr_ior_compare (or_result, compare_result,
15729                                            compare_result2);
15730           compare_result = or_result;
15731           code = EQ;
15732         }
15733       else
15734         {
15735           if (code == NE || code == LTGT)
15736             code = NE;
15737           else
15738             code = EQ;
15739         }
15740
15741       emit_insn (cmp);
15742     }
15743   else
15744     {
15745       /* Generate XLC-compatible TFmode compare as PARALLEL with extra
15746          CLOBBERs to match cmptf_internal2 pattern.  */
15747       if (comp_mode == CCFPmode && TARGET_XL_COMPAT
15748           && GET_MODE (op0) == TFmode
15749           && !TARGET_IEEEQUAD
15750           && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
15751         emit_insn (gen_rtx_PARALLEL (VOIDmode,
15752           gen_rtvec (10,
15753                      gen_rtx_SET (VOIDmode,
15754                                   compare_result,
15755                                   gen_rtx_COMPARE (comp_mode, op0, op1)),
15756                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15757                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15758                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15759                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15760                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15761                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15762                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15763                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15764                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
15765       else if (GET_CODE (op1) == UNSPEC
15766                && XINT (op1, 1) == UNSPEC_SP_TEST)
15767         {
15768           rtx op1b = XVECEXP (op1, 0, 0);
15769           comp_mode = CCEQmode;
15770           compare_result = gen_reg_rtx (CCEQmode);
15771           if (TARGET_64BIT)
15772             emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
15773           else
15774             emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
15775         }
15776       else
15777         emit_insn (gen_rtx_SET (VOIDmode, compare_result,
15778                                 gen_rtx_COMPARE (comp_mode, op0, op1)));
15779     }
15780
15781   /* Some kinds of FP comparisons need an OR operation;
15782      under flag_finite_math_only we don't bother.  */
15783   if (FLOAT_MODE_P (mode)
15784       && !flag_finite_math_only
15785       && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
15786       && (code == LE || code == GE
15787           || code == UNEQ || code == LTGT
15788           || code == UNGT || code == UNLT))
15789     {
15790       enum rtx_code or1, or2;
15791       rtx or1_rtx, or2_rtx, compare2_rtx;
15792       rtx or_result = gen_reg_rtx (CCEQmode);
15793
15794       switch (code)
15795         {
15796         case LE: or1 = LT;  or2 = EQ;  break;
15797         case GE: or1 = GT;  or2 = EQ;  break;
15798         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
15799         case LTGT: or1 = LT;  or2 = GT;  break;
15800         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
15801         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
15802         default:  gcc_unreachable ();
15803         }
15804       validate_condition_mode (or1, comp_mode);
15805       validate_condition_mode (or2, comp_mode);
15806       or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
15807       or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
15808       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
15809                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
15810                                       const_true_rtx);
15811       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
15812
15813       compare_result = or_result;
15814       code = EQ;
15815     }
15816
15817   validate_condition_mode (code, GET_MODE (compare_result));
15818
15819   return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
15820 }
15821
15822
15823 /* Emit the RTL for an sISEL pattern.  */
15824
15825 void
15826 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
15827 {
15828   rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
15829 }
15830
15831 void
15832 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
15833 {
15834   rtx condition_rtx;
15835   enum machine_mode op_mode;
15836   enum rtx_code cond_code;
15837   rtx result = operands[0];
15838
15839   if (TARGET_ISEL && (mode == SImode || mode == DImode))
15840     {
15841       rs6000_emit_sISEL (mode, operands);
15842       return;
15843     }
15844
15845   condition_rtx = rs6000_generate_compare (operands[1], mode);
15846   cond_code = GET_CODE (condition_rtx);
15847
15848   if (FLOAT_MODE_P (mode)
15849       && !TARGET_FPRS && TARGET_HARD_FLOAT)
15850     {
15851       rtx t;
15852
15853       PUT_MODE (condition_rtx, SImode);
15854       t = XEXP (condition_rtx, 0);
15855
15856       gcc_assert (cond_code == NE || cond_code == EQ);
15857
15858       if (cond_code == NE)
15859         emit_insn (gen_e500_flip_gt_bit (t, t));
15860
15861       emit_insn (gen_move_from_CR_gt_bit (result, t));
15862       return;
15863     }
15864
15865   if (cond_code == NE
15866       || cond_code == GE || cond_code == LE
15867       || cond_code == GEU || cond_code == LEU
15868       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
15869     {
15870       rtx not_result = gen_reg_rtx (CCEQmode);
15871       rtx not_op, rev_cond_rtx;
15872       enum machine_mode cc_mode;
15873
15874       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
15875
15876       rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
15877                                      SImode, XEXP (condition_rtx, 0), const0_rtx);
15878       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
15879       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
15880       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
15881     }
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 && (op_mode == DImode || FLOAT_MODE_P (mode)))
15888     {
15889       PUT_MODE (condition_rtx, DImode);
15890       convert_move (result, condition_rtx, 0);
15891     }
15892   else
15893     {
15894       PUT_MODE (condition_rtx, SImode);
15895       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
15896     }
15897 }
15898
15899 /* Emit a branch of kind CODE to location LOC.  */
15900
15901 void
15902 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
15903 {
15904   rtx condition_rtx, loc_ref;
15905
15906   condition_rtx = rs6000_generate_compare (operands[0], mode);
15907   loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
15908   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
15909                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
15910                                                      loc_ref, pc_rtx)));
15911 }
15912
15913 /* Return the string to output a conditional branch to LABEL, which is
15914    the operand number of the label, or -1 if the branch is really a
15915    conditional return.
15916
15917    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
15918    condition code register and its mode specifies what kind of
15919    comparison we made.
15920
15921    REVERSED is nonzero if we should reverse the sense of the comparison.
15922
15923    INSN is the insn.  */
15924
15925 char *
15926 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
15927 {
15928   static char string[64];
15929   enum rtx_code code = GET_CODE (op);
15930   rtx cc_reg = XEXP (op, 0);
15931   enum machine_mode mode = GET_MODE (cc_reg);
15932   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
15933   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
15934   int really_reversed = reversed ^ need_longbranch;
15935   char *s = string;
15936   const char *ccode;
15937   const char *pred;
15938   rtx note;
15939
15940   validate_condition_mode (code, mode);
15941
15942   /* Work out which way this really branches.  We could use
15943      reverse_condition_maybe_unordered here always but this
15944      makes the resulting assembler clearer.  */
15945   if (really_reversed)
15946     {
15947       /* Reversal of FP compares takes care -- an ordered compare
15948          becomes an unordered compare and vice versa.  */
15949       if (mode == CCFPmode)
15950         code = reverse_condition_maybe_unordered (code);
15951       else
15952         code = reverse_condition (code);
15953     }
15954
15955   if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
15956     {
15957       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
15958          to the GT bit.  */
15959       switch (code)
15960         {
15961         case EQ:
15962           /* Opposite of GT.  */
15963           code = GT;
15964           break;
15965
15966         case NE:
15967           code = UNLE;
15968           break;
15969
15970         default:
15971           gcc_unreachable ();
15972         }
15973     }
15974
15975   switch (code)
15976     {
15977       /* Not all of these are actually distinct opcodes, but
15978          we distinguish them for clarity of the resulting assembler.  */
15979     case NE: case LTGT:
15980       ccode = "ne"; break;
15981     case EQ: case UNEQ:
15982       ccode = "eq"; break;
15983     case GE: case GEU:
15984       ccode = "ge"; break;
15985     case GT: case GTU: case UNGT:
15986       ccode = "gt"; break;
15987     case LE: case LEU:
15988       ccode = "le"; break;
15989     case LT: case LTU: case UNLT:
15990       ccode = "lt"; break;
15991     case UNORDERED: ccode = "un"; break;
15992     case ORDERED: ccode = "nu"; break;
15993     case UNGE: ccode = "nl"; break;
15994     case UNLE: ccode = "ng"; break;
15995     default:
15996       gcc_unreachable ();
15997     }
15998
15999   /* Maybe we have a guess as to how likely the branch is.
16000      The old mnemonics don't have a way to specify this information.  */
16001   pred = "";
16002   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
16003   if (note != NULL_RTX)
16004     {
16005       /* PROB is the difference from 50%.  */
16006       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
16007
16008       /* Only hint for highly probable/improbable branches on newer
16009          cpus as static prediction overrides processor dynamic
16010          prediction.  For older cpus we may as well always hint, but
16011          assume not taken for branches that are very close to 50% as a
16012          mispredicted taken branch is more expensive than a
16013          mispredicted not-taken branch.  */
16014       if (rs6000_always_hint
16015           || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
16016               && br_prob_note_reliable_p (note)))
16017         {
16018           if (abs (prob) > REG_BR_PROB_BASE / 20
16019               && ((prob > 0) ^ need_longbranch))
16020             pred = "+";
16021           else
16022             pred = "-";
16023         }
16024     }
16025
16026   if (label == NULL)
16027     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
16028   else
16029     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
16030
16031   /* We need to escape any '%' characters in the reg_names string.
16032      Assume they'd only be the first character....  */
16033   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
16034     *s++ = '%';
16035   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
16036
16037   if (label != NULL)
16038     {
16039       /* If the branch distance was too far, we may have to use an
16040          unconditional branch to go the distance.  */
16041       if (need_longbranch)
16042         s += sprintf (s, ",$+8\n\tb %s", label);
16043       else
16044         s += sprintf (s, ",%s", label);
16045     }
16046
16047   return string;
16048 }
16049
16050 /* Return the string to flip the GT bit on a CR.  */
16051 char *
16052 output_e500_flip_gt_bit (rtx dst, rtx src)
16053 {
16054   static char string[64];
16055   int a, b;
16056
16057   gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
16058               && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
16059
16060   /* GT bit.  */
16061   a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
16062   b = 4 * (REGNO (src) - CR0_REGNO) + 1;
16063
16064   sprintf (string, "crnot %d,%d", a, b);
16065   return string;
16066 }
16067
16068 /* Return insn for VSX or Altivec comparisons.  */
16069
16070 static rtx
16071 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
16072 {
16073   rtx mask;
16074   enum machine_mode mode = GET_MODE (op0);
16075
16076   switch (code)
16077     {
16078     default:
16079       break;
16080
16081     case GE:
16082       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16083         return NULL_RTX;
16084
16085     case EQ:
16086     case GT:
16087     case GTU:
16088     case ORDERED:
16089     case UNORDERED:
16090     case UNEQ:
16091     case LTGT:
16092       mask = gen_reg_rtx (mode);
16093       emit_insn (gen_rtx_SET (VOIDmode,
16094                               mask,
16095                               gen_rtx_fmt_ee (code, mode, op0, op1)));
16096       return mask;
16097     }
16098
16099   return NULL_RTX;
16100 }
16101
16102 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
16103    DMODE is expected destination mode. This is a recursive function.  */
16104
16105 static rtx
16106 rs6000_emit_vector_compare (enum rtx_code rcode,
16107                             rtx op0, rtx op1,
16108                             enum machine_mode dmode)
16109 {
16110   rtx mask;
16111   bool swap_operands = false;
16112   bool try_again = false;
16113
16114   gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
16115   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
16116
16117   /* See if the comparison works as is.  */
16118   mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16119   if (mask)
16120     return mask;
16121
16122   switch (rcode)
16123     {
16124     case LT:
16125       rcode = GT;
16126       swap_operands = true;
16127       try_again = true;
16128       break;
16129     case LTU:
16130       rcode = GTU;
16131       swap_operands = true;
16132       try_again = true;
16133       break;
16134     case NE:
16135     case UNLE:
16136     case UNLT:
16137     case UNGE:
16138     case UNGT:
16139       /* Invert condition and try again.
16140          e.g., A != B becomes ~(A==B).  */
16141       {
16142         enum rtx_code rev_code;
16143         enum insn_code nor_code;
16144         rtx mask2;
16145
16146         rev_code = reverse_condition_maybe_unordered (rcode);
16147         if (rev_code == UNKNOWN)
16148           return NULL_RTX;
16149
16150         nor_code = optab_handler (one_cmpl_optab, dmode);
16151         if (nor_code == CODE_FOR_nothing)
16152           return NULL_RTX;
16153
16154         mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
16155         if (!mask2)
16156           return NULL_RTX;
16157
16158         mask = gen_reg_rtx (dmode);
16159         emit_insn (GEN_FCN (nor_code) (mask, mask2));
16160         return mask;
16161       }
16162       break;
16163     case GE:
16164     case GEU:
16165     case LE:
16166     case LEU:
16167       /* Try GT/GTU/LT/LTU OR EQ */
16168       {
16169         rtx c_rtx, eq_rtx;
16170         enum insn_code ior_code;
16171         enum rtx_code new_code;
16172
16173         switch (rcode)
16174           {
16175           case  GE:
16176             new_code = GT;
16177             break;
16178
16179           case GEU:
16180             new_code = GTU;
16181             break;
16182
16183           case LE:
16184             new_code = LT;
16185             break;
16186
16187           case LEU:
16188             new_code = LTU;
16189             break;
16190
16191           default:
16192             gcc_unreachable ();
16193           }
16194
16195         ior_code = optab_handler (ior_optab, dmode);
16196         if (ior_code == CODE_FOR_nothing)
16197           return NULL_RTX;
16198
16199         c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
16200         if (!c_rtx)
16201           return NULL_RTX;
16202
16203         eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
16204         if (!eq_rtx)
16205           return NULL_RTX;
16206
16207         mask = gen_reg_rtx (dmode);
16208         emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
16209         return mask;
16210       }
16211       break;
16212     default:
16213       return NULL_RTX;
16214     }
16215
16216   if (try_again)
16217     {
16218       if (swap_operands)
16219         {
16220           rtx tmp;
16221           tmp = op0;
16222           op0 = op1;
16223           op1 = tmp;
16224         }
16225
16226       mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16227       if (mask)
16228         return mask;
16229     }
16230
16231   /* You only get two chances.  */
16232   return NULL_RTX;
16233 }
16234
16235 /* Emit vector conditional expression.  DEST is destination. OP_TRUE and
16236    OP_FALSE are two VEC_COND_EXPR operands.  CC_OP0 and CC_OP1 are the two
16237    operands for the relation operation COND.  */
16238
16239 int
16240 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
16241                               rtx cond, rtx cc_op0, rtx cc_op1)
16242 {
16243   enum machine_mode dest_mode = GET_MODE (dest);
16244   enum machine_mode mask_mode = GET_MODE (cc_op0);
16245   enum rtx_code rcode = GET_CODE (cond);
16246   enum machine_mode cc_mode = CCmode;
16247   rtx mask;
16248   rtx cond2;
16249   rtx tmp;
16250   bool invert_move = false;
16251
16252   if (VECTOR_UNIT_NONE_P (dest_mode))
16253     return 0;
16254
16255   gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
16256               && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
16257
16258   switch (rcode)
16259     {
16260       /* Swap operands if we can, and fall back to doing the operation as
16261          specified, and doing a NOR to invert the test.  */
16262     case NE:
16263     case UNLE:
16264     case UNLT:
16265     case UNGE:
16266     case UNGT:
16267       /* Invert condition and try again.
16268          e.g., A  = (B != C) ? D : E becomes A = (B == C) ? E : D.  */
16269       invert_move = true;
16270       rcode = reverse_condition_maybe_unordered (rcode);
16271       if (rcode == UNKNOWN)
16272         return 0;
16273       break;
16274
16275       /* Mark unsigned tests with CCUNSmode.  */
16276     case GTU:
16277     case GEU:
16278     case LTU:
16279     case LEU:
16280       cc_mode = CCUNSmode;
16281       break;
16282
16283     default:
16284       break;
16285     }
16286
16287   /* Get the vector mask for the given relational operations.  */
16288   mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
16289
16290   if (!mask)
16291     return 0;
16292
16293   if (invert_move)
16294     {
16295       tmp = op_true;
16296       op_true = op_false;
16297       op_false = tmp;
16298     }
16299
16300   cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
16301                           CONST0_RTX (dest_mode));
16302   emit_insn (gen_rtx_SET (VOIDmode,
16303                           dest,
16304                           gen_rtx_IF_THEN_ELSE (dest_mode,
16305                                                 cond2,
16306                                                 op_true,
16307                                                 op_false)));
16308   return 1;
16309 }
16310
16311 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
16312    operands of the last comparison is nonzero/true, FALSE_COND if it
16313    is zero/false.  Return 0 if the hardware has no such operation.  */
16314
16315 int
16316 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16317 {
16318   enum rtx_code code = GET_CODE (op);
16319   rtx op0 = XEXP (op, 0);
16320   rtx op1 = XEXP (op, 1);
16321   REAL_VALUE_TYPE c1;
16322   enum machine_mode compare_mode = GET_MODE (op0);
16323   enum machine_mode result_mode = GET_MODE (dest);
16324   rtx temp;
16325   bool is_against_zero;
16326
16327   /* These modes should always match.  */
16328   if (GET_MODE (op1) != compare_mode
16329       /* In the isel case however, we can use a compare immediate, so
16330          op1 may be a small constant.  */
16331       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
16332     return 0;
16333   if (GET_MODE (true_cond) != result_mode)
16334     return 0;
16335   if (GET_MODE (false_cond) != result_mode)
16336     return 0;
16337
16338   /* First, work out if the hardware can do this at all, or
16339      if it's too slow....  */
16340   if (!FLOAT_MODE_P (compare_mode))
16341     {
16342       if (TARGET_ISEL)
16343         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
16344       return 0;
16345     }
16346   else if (TARGET_HARD_FLOAT && !TARGET_FPRS
16347            && SCALAR_FLOAT_MODE_P (compare_mode))
16348     return 0;
16349
16350   is_against_zero = op1 == CONST0_RTX (compare_mode);
16351
16352   /* A floating-point subtract might overflow, underflow, or produce
16353      an inexact result, thus changing the floating-point flags, so it
16354      can't be generated if we care about that.  It's safe if one side
16355      of the construct is zero, since then no subtract will be
16356      generated.  */
16357   if (SCALAR_FLOAT_MODE_P (compare_mode)
16358       && flag_trapping_math && ! is_against_zero)
16359     return 0;
16360
16361   /* Eliminate half of the comparisons by switching operands, this
16362      makes the remaining code simpler.  */
16363   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
16364       || code == LTGT || code == LT || code == UNLE)
16365     {
16366       code = reverse_condition_maybe_unordered (code);
16367       temp = true_cond;
16368       true_cond = false_cond;
16369       false_cond = temp;
16370     }
16371
16372   /* UNEQ and LTGT take four instructions for a comparison with zero,
16373      it'll probably be faster to use a branch here too.  */
16374   if (code == UNEQ && HONOR_NANS (compare_mode))
16375     return 0;
16376
16377   if (GET_CODE (op1) == CONST_DOUBLE)
16378     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
16379
16380   /* We're going to try to implement comparisons by performing
16381      a subtract, then comparing against zero.  Unfortunately,
16382      Inf - Inf is NaN which is not zero, and so if we don't
16383      know that the operand is finite and the comparison
16384      would treat EQ different to UNORDERED, we can't do it.  */
16385   if (HONOR_INFINITIES (compare_mode)
16386       && code != GT && code != UNGE
16387       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
16388       /* Constructs of the form (a OP b ? a : b) are safe.  */
16389       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
16390           || (! rtx_equal_p (op0, true_cond)
16391               && ! rtx_equal_p (op1, true_cond))))
16392     return 0;
16393
16394   /* At this point we know we can use fsel.  */
16395
16396   /* Reduce the comparison to a comparison against zero.  */
16397   if (! is_against_zero)
16398     {
16399       temp = gen_reg_rtx (compare_mode);
16400       emit_insn (gen_rtx_SET (VOIDmode, temp,
16401                               gen_rtx_MINUS (compare_mode, op0, op1)));
16402       op0 = temp;
16403       op1 = CONST0_RTX (compare_mode);
16404     }
16405
16406   /* If we don't care about NaNs we can reduce some of the comparisons
16407      down to faster ones.  */
16408   if (! HONOR_NANS (compare_mode))
16409     switch (code)
16410       {
16411       case GT:
16412         code = LE;
16413         temp = true_cond;
16414         true_cond = false_cond;
16415         false_cond = temp;
16416         break;
16417       case UNGE:
16418         code = GE;
16419         break;
16420       case UNEQ:
16421         code = EQ;
16422         break;
16423       default:
16424         break;
16425       }
16426
16427   /* Now, reduce everything down to a GE.  */
16428   switch (code)
16429     {
16430     case GE:
16431       break;
16432
16433     case LE:
16434       temp = gen_reg_rtx (compare_mode);
16435       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16436       op0 = temp;
16437       break;
16438
16439     case ORDERED:
16440       temp = gen_reg_rtx (compare_mode);
16441       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
16442       op0 = temp;
16443       break;
16444
16445     case EQ:
16446       temp = gen_reg_rtx (compare_mode);
16447       emit_insn (gen_rtx_SET (VOIDmode, temp,
16448                               gen_rtx_NEG (compare_mode,
16449                                            gen_rtx_ABS (compare_mode, op0))));
16450       op0 = temp;
16451       break;
16452
16453     case UNGE:
16454       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
16455       temp = gen_reg_rtx (result_mode);
16456       emit_insn (gen_rtx_SET (VOIDmode, temp,
16457                               gen_rtx_IF_THEN_ELSE (result_mode,
16458                                                     gen_rtx_GE (VOIDmode,
16459                                                                 op0, op1),
16460                                                     true_cond, false_cond)));
16461       false_cond = true_cond;
16462       true_cond = temp;
16463
16464       temp = gen_reg_rtx (compare_mode);
16465       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16466       op0 = temp;
16467       break;
16468
16469     case GT:
16470       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
16471       temp = gen_reg_rtx (result_mode);
16472       emit_insn (gen_rtx_SET (VOIDmode, temp,
16473                               gen_rtx_IF_THEN_ELSE (result_mode,
16474                                                     gen_rtx_GE (VOIDmode,
16475                                                                 op0, op1),
16476                                                     true_cond, false_cond)));
16477       true_cond = false_cond;
16478       false_cond = temp;
16479
16480       temp = gen_reg_rtx (compare_mode);
16481       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16482       op0 = temp;
16483       break;
16484
16485     default:
16486       gcc_unreachable ();
16487     }
16488
16489   emit_insn (gen_rtx_SET (VOIDmode, dest,
16490                           gen_rtx_IF_THEN_ELSE (result_mode,
16491                                                 gen_rtx_GE (VOIDmode,
16492                                                             op0, op1),
16493                                                 true_cond, false_cond)));
16494   return 1;
16495 }
16496
16497 /* Same as above, but for ints (isel).  */
16498
16499 static int
16500 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16501 {
16502   rtx condition_rtx, cr;
16503   enum machine_mode mode = GET_MODE (dest);
16504   enum rtx_code cond_code;
16505   rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
16506   bool signedp;
16507
16508   if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
16509     return 0;
16510
16511   /* We still have to do the compare, because isel doesn't do a
16512      compare, it just looks at the CRx bits set by a previous compare
16513      instruction.  */
16514   condition_rtx = rs6000_generate_compare (op, mode);
16515   cond_code = GET_CODE (condition_rtx);
16516   cr = XEXP (condition_rtx, 0);
16517   signedp = GET_MODE (cr) == CCmode;
16518
16519   isel_func = (mode == SImode
16520                ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
16521                : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
16522
16523   switch (cond_code)
16524     {
16525     case LT: case GT: case LTU: case GTU: case EQ:
16526       /* isel handles these directly.  */
16527       break;
16528
16529     default:
16530       /* We need to swap the sense of the comparison.  */
16531       {
16532         rtx t = true_cond;
16533         true_cond = false_cond;
16534         false_cond = t;
16535         PUT_CODE (condition_rtx, reverse_condition (cond_code));
16536       }
16537       break;
16538     }
16539
16540   false_cond = force_reg (mode, false_cond);
16541   if (true_cond != const0_rtx)
16542     true_cond = force_reg (mode, true_cond);
16543
16544   emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
16545
16546   return 1;
16547 }
16548
16549 const char *
16550 output_isel (rtx *operands)
16551 {
16552   enum rtx_code code;
16553
16554   code = GET_CODE (operands[1]);
16555
16556   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
16557     {
16558       gcc_assert (GET_CODE (operands[2]) == REG
16559                   && GET_CODE (operands[3]) == REG);
16560       PUT_CODE (operands[1], reverse_condition (code));
16561       return "isel %0,%3,%2,%j1";
16562     }
16563
16564   return "isel %0,%2,%3,%j1";
16565 }
16566
16567 void
16568 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16569 {
16570   enum machine_mode mode = GET_MODE (op0);
16571   enum rtx_code c;
16572   rtx target;
16573
16574   /* VSX/altivec have direct min/max insns.  */
16575   if ((code == SMAX || code == SMIN)
16576       && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
16577           || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
16578     {
16579       emit_insn (gen_rtx_SET (VOIDmode,
16580                               dest,
16581                               gen_rtx_fmt_ee (code, mode, op0, op1)));
16582       return;
16583     }
16584
16585   if (code == SMAX || code == SMIN)
16586     c = GE;
16587   else
16588     c = GEU;
16589
16590   if (code == SMAX || code == UMAX)
16591     target = emit_conditional_move (dest, c, op0, op1, mode,
16592                                     op0, op1, mode, 0);
16593   else
16594     target = emit_conditional_move (dest, c, op0, op1, mode,
16595                                     op1, op0, mode, 0);
16596   gcc_assert (target);
16597   if (target != dest)
16598     emit_move_insn (dest, target);
16599 }
16600
16601 /* A subroutine of the atomic operation splitters.  Jump to LABEL if
16602    COND is true.  Mark the jump as unlikely to be taken.  */
16603
16604 static void
16605 emit_unlikely_jump (rtx cond, rtx label)
16606 {
16607   rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
16608   rtx x;
16609
16610   x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
16611   x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
16612   add_reg_note (x, REG_BR_PROB, very_unlikely);
16613 }
16614
16615 /* A subroutine of the atomic operation splitters.  Emit a load-locked
16616    instruction in MODE.  */
16617
16618 static void
16619 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
16620 {
16621   rtx (*fn) (rtx, rtx) = NULL;
16622
16623   switch (mode)
16624     {
16625     case SImode:
16626       fn = gen_load_lockedsi;
16627       break;
16628     case DImode:
16629       fn = gen_load_lockeddi;
16630       break;
16631     default:
16632       gcc_unreachable ();
16633     }
16634   emit_insn (fn (reg, mem));
16635 }
16636
16637 /* A subroutine of the atomic operation splitters.  Emit a store-conditional
16638    instruction in MODE.  */
16639
16640 static void
16641 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
16642 {
16643   rtx (*fn) (rtx, rtx, rtx) = NULL;
16644
16645   switch (mode)
16646     {
16647     case SImode:
16648       fn = gen_store_conditionalsi;
16649       break;
16650     case DImode:
16651       fn = gen_store_conditionaldi;
16652       break;
16653     default:
16654       gcc_unreachable ();
16655     }
16656
16657   /* Emit sync before stwcx. to address PPC405 Erratum.  */
16658   if (PPC405_ERRATUM77)
16659     emit_insn (gen_hwsync ());
16660
16661   emit_insn (fn (res, mem, val));
16662 }
16663
16664 /* Expand barriers before and after a load_locked/store_cond sequence.  */
16665
16666 static void
16667 rs6000_pre_atomic_barrier (enum memmodel model)
16668 {
16669   switch (model)
16670     {
16671     case MEMMODEL_RELAXED:
16672     case MEMMODEL_CONSUME:
16673     case MEMMODEL_ACQUIRE:
16674       break;
16675     case MEMMODEL_RELEASE:
16676     case MEMMODEL_ACQ_REL:
16677       emit_insn (gen_lwsync ());
16678       break;
16679     case MEMMODEL_SEQ_CST:
16680       emit_insn (gen_hwsync ());
16681       break;
16682     default:
16683       gcc_unreachable ();
16684     }
16685 }
16686
16687 static void
16688 rs6000_post_atomic_barrier (enum memmodel model)
16689 {
16690   switch (model)
16691     {
16692     case MEMMODEL_RELAXED:
16693     case MEMMODEL_CONSUME:
16694     case MEMMODEL_RELEASE:
16695       break;
16696     case MEMMODEL_ACQUIRE:
16697     case MEMMODEL_ACQ_REL:
16698     case MEMMODEL_SEQ_CST:
16699       emit_insn (gen_isync ());
16700       break;
16701     default:
16702       gcc_unreachable ();
16703     }
16704 }
16705
16706 /* A subroutine of the various atomic expanders.  For sub-word operations,
16707    we must adjust things to operate on SImode.  Given the original MEM,
16708    return a new aligned memory.  Also build and return the quantities by
16709    which to shift and mask.  */
16710
16711 static rtx
16712 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
16713 {
16714   rtx addr, align, shift, mask, mem;
16715   HOST_WIDE_INT shift_mask;
16716   enum machine_mode mode = GET_MODE (orig_mem);
16717
16718   /* For smaller modes, we have to implement this via SImode.  */
16719   shift_mask = (mode == QImode ? 0x18 : 0x10);
16720
16721   addr = XEXP (orig_mem, 0);
16722   addr = force_reg (GET_MODE (addr), addr);
16723
16724   /* Aligned memory containing subword.  Generate a new memory.  We
16725      do not want any of the existing MEM_ATTR data, as we're now
16726      accessing memory outside the original object.  */
16727   align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
16728                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
16729   mem = gen_rtx_MEM (SImode, align);
16730   MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
16731   if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
16732     set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
16733
16734   /* Shift amount for subword relative to aligned word.  */
16735   shift = gen_reg_rtx (SImode);
16736   addr = gen_lowpart (SImode, addr);
16737   emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
16738   shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
16739                                shift, 1, OPTAB_LIB_WIDEN);
16740   *pshift = shift;
16741
16742   /* Mask for insertion.  */
16743   mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
16744                               shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
16745   *pmask = mask;
16746
16747   return mem;
16748 }
16749
16750 /* A subroutine of the various atomic expanders.  For sub-word operands,
16751    combine OLDVAL and NEWVAL via MASK.  Returns a new pseduo.  */
16752
16753 static rtx
16754 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
16755 {
16756   rtx x;
16757
16758   x = gen_reg_rtx (SImode);
16759   emit_insn (gen_rtx_SET (VOIDmode, x,
16760                           gen_rtx_AND (SImode,
16761                                        gen_rtx_NOT (SImode, mask),
16762                                        oldval)));
16763
16764   x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
16765
16766   return x;
16767 }
16768
16769 /* A subroutine of the various atomic expanders.  For sub-word operands,
16770    extract WIDE to NARROW via SHIFT.  */
16771
16772 static void
16773 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
16774 {
16775   wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
16776                               wide, 1, OPTAB_LIB_WIDEN);
16777   emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
16778 }
16779
16780 /* Expand an atomic compare and swap operation.  */
16781
16782 void
16783 rs6000_expand_atomic_compare_and_swap (rtx operands[])
16784 {
16785   rtx boolval, retval, mem, oldval, newval, cond;
16786   rtx label1, label2, x, mask, shift;
16787   enum machine_mode mode;
16788   enum memmodel mod_s, mod_f;
16789   bool is_weak;
16790
16791   boolval = operands[0];
16792   retval = operands[1];
16793   mem = operands[2];
16794   oldval = operands[3];
16795   newval = operands[4];
16796   is_weak = (INTVAL (operands[5]) != 0);
16797   mod_s = (enum memmodel) INTVAL (operands[6]);
16798   mod_f = (enum memmodel) INTVAL (operands[7]);
16799   mode = GET_MODE (mem);
16800
16801   mask = shift = NULL_RTX;
16802   if (mode == QImode || mode == HImode)
16803     {
16804       mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16805
16806       /* Shift and mask OLDVAL into position with the word.  */
16807       oldval = convert_modes (SImode, mode, oldval, 1);
16808       oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
16809                                     NULL_RTX, 1, OPTAB_LIB_WIDEN);
16810
16811       /* Shift and mask NEWVAL into position within the word.  */
16812       newval = convert_modes (SImode, mode, newval, 1);
16813       newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
16814                                     NULL_RTX, 1, OPTAB_LIB_WIDEN);
16815
16816       /* Prepare to adjust the return value.  */
16817       retval = gen_reg_rtx (SImode);
16818       mode = SImode;
16819     }
16820   else if (reg_overlap_mentioned_p (retval, oldval))
16821     oldval = copy_to_reg (oldval);
16822
16823   rs6000_pre_atomic_barrier (mod_s);
16824
16825   label1 = NULL_RTX;
16826   if (!is_weak)
16827     {
16828       label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16829       emit_label (XEXP (label1, 0));
16830     }
16831   label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16832
16833   emit_load_locked (mode, retval, mem);
16834
16835   x = retval;
16836   if (mask)
16837     {
16838       x = expand_simple_binop (SImode, AND, retval, mask,
16839                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
16840     }
16841
16842   cond = gen_reg_rtx (CCmode);
16843   x = gen_rtx_COMPARE (CCmode, x, oldval);
16844   emit_insn (gen_rtx_SET (VOIDmode, cond, x));
16845
16846   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16847   emit_unlikely_jump (x, label2);
16848
16849   x = newval;
16850   if (mask)
16851     x = rs6000_mask_atomic_subword (retval, newval, mask);
16852
16853   emit_store_conditional (mode, cond, mem, x);
16854
16855   if (!is_weak)
16856     {
16857       x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16858       emit_unlikely_jump (x, label1);
16859     }
16860
16861   if (mod_f != MEMMODEL_RELAXED)
16862     emit_label (XEXP (label2, 0));
16863
16864   rs6000_post_atomic_barrier (mod_s);
16865
16866   if (mod_f == MEMMODEL_RELAXED)
16867     emit_label (XEXP (label2, 0));
16868
16869   if (shift)
16870     rs6000_finish_atomic_subword (operands[1], retval, shift);
16871
16872   /* In all cases, CR0 contains EQ on success, and NE on failure.  */
16873   x = gen_rtx_EQ (SImode, cond, const0_rtx);
16874   emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
16875 }
16876
16877 /* Expand an atomic exchange operation.  */
16878
16879 void
16880 rs6000_expand_atomic_exchange (rtx operands[])
16881 {
16882   rtx retval, mem, val, cond;
16883   enum machine_mode mode;
16884   enum memmodel model;
16885   rtx label, x, mask, shift;
16886
16887   retval = operands[0];
16888   mem = operands[1];
16889   val = operands[2];
16890   model = (enum memmodel) INTVAL (operands[3]);
16891   mode = GET_MODE (mem);
16892
16893   mask = shift = NULL_RTX;
16894   if (mode == QImode || mode == HImode)
16895     {
16896       mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16897
16898       /* Shift and mask VAL into position with the word.  */
16899       val = convert_modes (SImode, mode, val, 1);
16900       val = expand_simple_binop (SImode, ASHIFT, val, shift,
16901                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
16902
16903       /* Prepare to adjust the return value.  */
16904       retval = gen_reg_rtx (SImode);
16905       mode = SImode;
16906     }
16907
16908   rs6000_pre_atomic_barrier (model);
16909
16910   label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16911   emit_label (XEXP (label, 0));
16912
16913   emit_load_locked (mode, retval, mem);
16914
16915   x = val;
16916   if (mask)
16917     x = rs6000_mask_atomic_subword (retval, val, mask);
16918
16919   cond = gen_reg_rtx (CCmode);
16920   emit_store_conditional (mode, cond, mem, x);
16921
16922   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16923   emit_unlikely_jump (x, label);
16924
16925   rs6000_post_atomic_barrier (model);
16926
16927   if (shift)
16928     rs6000_finish_atomic_subword (operands[0], retval, shift);
16929 }
16930
16931 /* Expand an atomic fetch-and-operate pattern.  CODE is the binary operation
16932    to perform.  MEM is the memory on which to operate.  VAL is the second
16933    operand of the binary operator.  BEFORE and AFTER are optional locations to
16934    return the value of MEM either before of after the operation.  MODEL_RTX
16935    is a CONST_INT containing the memory model to use.  */
16936
16937 void
16938 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
16939                          rtx orig_before, rtx orig_after, rtx model_rtx)
16940 {
16941   enum memmodel model = (enum memmodel) INTVAL (model_rtx);
16942   enum machine_mode mode = GET_MODE (mem);
16943   rtx label, x, cond, mask, shift;
16944   rtx before = orig_before, after = orig_after;
16945
16946   mask = shift = NULL_RTX;
16947   if (mode == QImode || mode == HImode)
16948     {
16949       mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16950
16951       /* Shift and mask VAL into position with the word.  */
16952       val = convert_modes (SImode, mode, val, 1);
16953       val = expand_simple_binop (SImode, ASHIFT, val, shift,
16954                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
16955
16956       switch (code)
16957         {
16958         case IOR:
16959         case XOR:
16960           /* We've already zero-extended VAL.  That is sufficient to
16961              make certain that it does not affect other bits.  */
16962           mask = NULL;
16963           break;
16964
16965         case AND:
16966           /* If we make certain that all of the other bits in VAL are
16967              set, that will be sufficient to not affect other bits.  */
16968           x = gen_rtx_NOT (SImode, mask);
16969           x = gen_rtx_IOR (SImode, x, val);
16970           emit_insn (gen_rtx_SET (VOIDmode, val, x));
16971           mask = NULL;
16972           break;
16973
16974         case NOT:
16975         case PLUS:
16976         case MINUS:
16977           /* These will all affect bits outside the field and need
16978              adjustment via MASK within the loop.  */
16979           break;
16980
16981         default:
16982           gcc_unreachable ();
16983         }
16984
16985       /* Prepare to adjust the return value.  */
16986       before = gen_reg_rtx (SImode);
16987       if (after)
16988         after = gen_reg_rtx (SImode);
16989       mode = SImode;
16990     }
16991
16992   rs6000_pre_atomic_barrier (model);
16993
16994   label = gen_label_rtx ();
16995   emit_label (label);
16996   label = gen_rtx_LABEL_REF (VOIDmode, label);
16997
16998   if (before == NULL_RTX)
16999     before = gen_reg_rtx (mode);
17000
17001   emit_load_locked (mode, before, mem);
17002
17003   if (code == NOT)
17004     {
17005       x = expand_simple_binop (mode, AND, before, val,
17006                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
17007       after = expand_simple_unop (mode, NOT, x, after, 1);
17008     }
17009   else
17010     {
17011       after = expand_simple_binop (mode, code, before, val,
17012                                    after, 1, OPTAB_LIB_WIDEN);
17013     }
17014
17015   x = after;
17016   if (mask)
17017     {
17018       x = expand_simple_binop (SImode, AND, after, mask,
17019                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
17020       x = rs6000_mask_atomic_subword (before, x, mask);
17021     }
17022
17023   cond = gen_reg_rtx (CCmode);
17024   emit_store_conditional (mode, cond, mem, x);
17025
17026   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17027   emit_unlikely_jump (x, label);
17028
17029   rs6000_post_atomic_barrier (model);
17030
17031   if (shift)
17032     {
17033       if (orig_before)
17034         rs6000_finish_atomic_subword (orig_before, before, shift);
17035       if (orig_after)
17036         rs6000_finish_atomic_subword (orig_after, after, shift);
17037     }
17038   else if (orig_after && after != orig_after)
17039     emit_move_insn (orig_after, after);
17040 }
17041
17042 /* Emit instructions to move SRC to DST.  Called by splitters for
17043    multi-register moves.  It will emit at most one instruction for
17044    each register that is accessed; that is, it won't emit li/lis pairs
17045    (or equivalent for 64-bit code).  One of SRC or DST must be a hard
17046    register.  */
17047
17048 void
17049 rs6000_split_multireg_move (rtx dst, rtx src)
17050 {
17051   /* The register number of the first register being moved.  */
17052   int reg;
17053   /* The mode that is to be moved.  */
17054   enum machine_mode mode;
17055   /* The mode that the move is being done in, and its size.  */
17056   enum machine_mode reg_mode;
17057   int reg_mode_size;
17058   /* The number of registers that will be moved.  */
17059   int nregs;
17060
17061   reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
17062   mode = GET_MODE (dst);
17063   nregs = hard_regno_nregs[reg][mode];
17064   if (FP_REGNO_P (reg))
17065     reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : 
17066         ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
17067   else if (ALTIVEC_REGNO_P (reg))
17068     reg_mode = V16QImode;
17069   else if (TARGET_E500_DOUBLE && mode == TFmode)
17070     reg_mode = DFmode;
17071   else
17072     reg_mode = word_mode;
17073   reg_mode_size = GET_MODE_SIZE (reg_mode);
17074
17075   gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
17076
17077   if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
17078     {
17079       /* Move register range backwards, if we might have destructive
17080          overlap.  */
17081       int i;
17082       for (i = nregs - 1; i >= 0; i--)
17083         emit_insn (gen_rtx_SET (VOIDmode,
17084                                 simplify_gen_subreg (reg_mode, dst, mode,
17085                                                      i * reg_mode_size),
17086                                 simplify_gen_subreg (reg_mode, src, mode,
17087                                                      i * reg_mode_size)));
17088     }
17089   else
17090     {
17091       int i;
17092       int j = -1;
17093       bool used_update = false;
17094       rtx restore_basereg = NULL_RTX;
17095
17096       if (MEM_P (src) && INT_REGNO_P (reg))
17097         {
17098           rtx breg;
17099
17100           if (GET_CODE (XEXP (src, 0)) == PRE_INC
17101               || GET_CODE (XEXP (src, 0)) == PRE_DEC)
17102             {
17103               rtx delta_rtx;
17104               breg = XEXP (XEXP (src, 0), 0);
17105               delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
17106                            ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
17107                            : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
17108               emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17109               src = replace_equiv_address (src, breg);
17110             }
17111           else if (! rs6000_offsettable_memref_p (src))
17112             {
17113               if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
17114                 {
17115                   rtx basereg = XEXP (XEXP (src, 0), 0);
17116                   if (TARGET_UPDATE)
17117                     {
17118                       rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
17119                       emit_insn (gen_rtx_SET (VOIDmode, ndst,
17120                                  gen_rtx_MEM (reg_mode, XEXP (src, 0))));
17121                       used_update = true;
17122                     }
17123                   else
17124                     emit_insn (gen_rtx_SET (VOIDmode, basereg,
17125                                XEXP (XEXP (src, 0), 1)));
17126                   src = replace_equiv_address (src, basereg);
17127                 }
17128               else
17129                 {
17130                   rtx basereg = gen_rtx_REG (Pmode, reg);
17131                   emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
17132                   src = replace_equiv_address (src, basereg);
17133                 }
17134             }
17135
17136           breg = XEXP (src, 0);
17137           if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
17138             breg = XEXP (breg, 0);
17139
17140           /* If the base register we are using to address memory is
17141              also a destination reg, then change that register last.  */
17142           if (REG_P (breg)
17143               && REGNO (breg) >= REGNO (dst)
17144               && REGNO (breg) < REGNO (dst) + nregs)
17145             j = REGNO (breg) - REGNO (dst);
17146         }
17147       else if (MEM_P (dst) && INT_REGNO_P (reg))
17148         {
17149           rtx breg;
17150
17151           if (GET_CODE (XEXP (dst, 0)) == PRE_INC
17152               || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
17153             {
17154               rtx delta_rtx;
17155               breg = XEXP (XEXP (dst, 0), 0);
17156               delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
17157                            ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
17158                            : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
17159
17160               /* We have to update the breg before doing the store.
17161                  Use store with update, if available.  */
17162
17163               if (TARGET_UPDATE)
17164                 {
17165                   rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17166                   emit_insn (TARGET_32BIT
17167                              ? (TARGET_POWERPC64
17168                                 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
17169                                 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
17170                              : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
17171                   used_update = true;
17172                 }
17173               else
17174                 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17175               dst = replace_equiv_address (dst, breg);
17176             }
17177           else if (!rs6000_offsettable_memref_p (dst)
17178                    && GET_CODE (XEXP (dst, 0)) != LO_SUM)
17179             {
17180               if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
17181                 {
17182                   rtx basereg = XEXP (XEXP (dst, 0), 0);
17183                   if (TARGET_UPDATE)
17184                     {
17185                       rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17186                       emit_insn (gen_rtx_SET (VOIDmode,
17187                                  gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
17188                       used_update = true;
17189                     }
17190                   else
17191                     emit_insn (gen_rtx_SET (VOIDmode, basereg,
17192                                XEXP (XEXP (dst, 0), 1)));
17193                   dst = replace_equiv_address (dst, basereg);
17194                 }
17195               else
17196                 {
17197                   rtx basereg = XEXP (XEXP (dst, 0), 0);
17198                   rtx offsetreg = XEXP (XEXP (dst, 0), 1);
17199                   gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
17200                               && REG_P (basereg)
17201                               && REG_P (offsetreg)
17202                               && REGNO (basereg) != REGNO (offsetreg));
17203                   if (REGNO (basereg) == 0)
17204                     {
17205                       rtx tmp = offsetreg;
17206                       offsetreg = basereg;
17207                       basereg = tmp;
17208                     }
17209                   emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
17210                   restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
17211                   dst = replace_equiv_address (dst, basereg);
17212                 }
17213             }
17214           else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
17215             gcc_assert (rs6000_offsettable_memref_p (dst));
17216         }
17217
17218       for (i = 0; i < nregs; i++)
17219         {
17220           /* Calculate index to next subword.  */
17221           ++j;
17222           if (j == nregs)
17223             j = 0;
17224
17225           /* If compiler already emitted move of first word by
17226              store with update, no need to do anything.  */
17227           if (j == 0 && used_update)
17228             continue;
17229
17230           emit_insn (gen_rtx_SET (VOIDmode,
17231                                   simplify_gen_subreg (reg_mode, dst, mode,
17232                                                        j * reg_mode_size),
17233                                   simplify_gen_subreg (reg_mode, src, mode,
17234                                                        j * reg_mode_size)));
17235         }
17236       if (restore_basereg != NULL_RTX)
17237         emit_insn (restore_basereg);
17238     }
17239 }
17240
17241 \f
17242 /* This page contains routines that are used to determine what the
17243    function prologue and epilogue code will do and write them out.  */
17244
17245 /* Return the first fixed-point register that is required to be
17246    saved. 32 if none.  */
17247
17248 int
17249 first_reg_to_save (void)
17250 {
17251   int first_reg;
17252
17253   /* Find lowest numbered live register.  */
17254   for (first_reg = 13; first_reg <= 31; first_reg++)
17255     if (df_regs_ever_live_p (first_reg)
17256         && (! call_used_regs[first_reg]
17257             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
17258                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
17259                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
17260                     || (TARGET_TOC && TARGET_MINIMAL_TOC)))))
17261       break;
17262
17263 #if TARGET_MACHO
17264   if (flag_pic
17265       && crtl->uses_pic_offset_table
17266       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
17267     return RS6000_PIC_OFFSET_TABLE_REGNUM;
17268 #endif
17269
17270   return first_reg;
17271 }
17272
17273 /* Similar, for FP regs.  */
17274
17275 int
17276 first_fp_reg_to_save (void)
17277 {
17278   int first_reg;
17279
17280   /* Find lowest numbered live register.  */
17281   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
17282     if (df_regs_ever_live_p (first_reg))
17283       break;
17284
17285   return first_reg;
17286 }
17287
17288 /* Similar, for AltiVec regs.  */
17289
17290 static int
17291 first_altivec_reg_to_save (void)
17292 {
17293   int i;
17294
17295   /* Stack frame remains as is unless we are in AltiVec ABI.  */
17296   if (! TARGET_ALTIVEC_ABI)
17297     return LAST_ALTIVEC_REGNO + 1;
17298
17299   /* On Darwin, the unwind routines are compiled without
17300      TARGET_ALTIVEC, and use save_world to save/restore the
17301      altivec registers when necessary.  */
17302   if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17303       && ! TARGET_ALTIVEC)
17304     return FIRST_ALTIVEC_REGNO + 20;
17305
17306   /* Find lowest numbered live register.  */
17307   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
17308     if (df_regs_ever_live_p (i))
17309       break;
17310
17311   return i;
17312 }
17313
17314 /* Return a 32-bit mask of the AltiVec registers we need to set in
17315    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
17316    the 32-bit word is 0.  */
17317
17318 static unsigned int
17319 compute_vrsave_mask (void)
17320 {
17321   unsigned int i, mask = 0;
17322
17323   /* On Darwin, the unwind routines are compiled without
17324      TARGET_ALTIVEC, and use save_world to save/restore the
17325      call-saved altivec registers when necessary.  */
17326   if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17327       && ! TARGET_ALTIVEC)
17328     mask |= 0xFFF;
17329
17330   /* First, find out if we use _any_ altivec registers.  */
17331   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
17332     if (df_regs_ever_live_p (i))
17333       mask |= ALTIVEC_REG_BIT (i);
17334
17335   if (mask == 0)
17336     return mask;
17337
17338   /* Next, remove the argument registers from the set.  These must
17339      be in the VRSAVE mask set by the caller, so we don't need to add
17340      them in again.  More importantly, the mask we compute here is
17341      used to generate CLOBBERs in the set_vrsave insn, and we do not
17342      wish the argument registers to die.  */
17343   for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
17344     mask &= ~ALTIVEC_REG_BIT (i);
17345
17346   /* Similarly, remove the return value from the set.  */
17347   {
17348     bool yes = false;
17349     diddle_return_value (is_altivec_return_reg, &yes);
17350     if (yes)
17351       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
17352   }
17353
17354   return mask;
17355 }
17356
17357 /* For a very restricted set of circumstances, we can cut down the
17358    size of prologues/epilogues by calling our own save/restore-the-world
17359    routines.  */
17360
17361 static void
17362 compute_save_world_info (rs6000_stack_t *info_ptr)
17363 {
17364   info_ptr->world_save_p = 1;
17365   info_ptr->world_save_p
17366     = (WORLD_SAVE_P (info_ptr)
17367        && DEFAULT_ABI == ABI_DARWIN
17368        && !cfun->has_nonlocal_label
17369        && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
17370        && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
17371        && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
17372        && info_ptr->cr_save_p);
17373
17374   /* This will not work in conjunction with sibcalls.  Make sure there
17375      are none.  (This check is expensive, but seldom executed.) */
17376   if (WORLD_SAVE_P (info_ptr))
17377     {
17378       rtx insn;
17379       for ( insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
17380         if ( GET_CODE (insn) == CALL_INSN
17381              && SIBLING_CALL_P (insn))
17382           {
17383             info_ptr->world_save_p = 0;
17384             break;
17385           }
17386     }
17387
17388   if (WORLD_SAVE_P (info_ptr))
17389     {
17390       /* Even if we're not touching VRsave, make sure there's room on the
17391          stack for it, if it looks like we're calling SAVE_WORLD, which
17392          will attempt to save it. */
17393       info_ptr->vrsave_size  = 4;
17394
17395       /* If we are going to save the world, we need to save the link register too.  */
17396       info_ptr->lr_save_p = 1;
17397
17398       /* "Save" the VRsave register too if we're saving the world.  */
17399       if (info_ptr->vrsave_mask == 0)
17400         info_ptr->vrsave_mask = compute_vrsave_mask ();
17401
17402       /* Because the Darwin register save/restore routines only handle
17403          F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
17404          check.  */
17405       gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
17406                   && (info_ptr->first_altivec_reg_save
17407                       >= FIRST_SAVED_ALTIVEC_REGNO));
17408     }
17409   return;
17410 }
17411
17412
17413 static void
17414 is_altivec_return_reg (rtx reg, void *xyes)
17415 {
17416   bool *yes = (bool *) xyes;
17417   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
17418     *yes = true;
17419 }
17420
17421 \f
17422 /* Determine the strategy for savings/restoring registers.  */
17423
17424 enum {
17425   SAVRES_MULTIPLE = 0x1,
17426   SAVE_INLINE_FPRS = 0x2,
17427   SAVE_INLINE_GPRS = 0x4,
17428   REST_INLINE_FPRS = 0x8,
17429   REST_INLINE_GPRS = 0x10,
17430   SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
17431   SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
17432   REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80
17433 };
17434
17435 static int
17436 rs6000_savres_strategy (rs6000_stack_t *info,
17437                         bool using_static_chain_p)
17438 {
17439   int strategy = 0;
17440
17441   if (TARGET_MULTIPLE
17442       && !TARGET_POWERPC64
17443       && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
17444       && info->first_gp_reg_save < 31
17445       && no_global_regs_above (info->first_gp_reg_save, /*gpr=*/true))
17446     strategy |= SAVRES_MULTIPLE;
17447
17448   if (crtl->calls_eh_return
17449       || cfun->machine->ra_need_lr
17450       || info->total_size > 32767)
17451     strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
17452                  | SAVE_INLINE_GPRS | REST_INLINE_GPRS);
17453
17454   if (info->first_fp_reg_save == 64
17455       || FP_SAVE_INLINE (info->first_fp_reg_save)
17456       /* The out-of-line FP routines use double-precision stores;
17457          we can't use those routines if we don't have such stores.  */
17458       || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
17459       || !no_global_regs_above (info->first_fp_reg_save, /*gpr=*/false))
17460     strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17461
17462   if (info->first_gp_reg_save == 32
17463       || GP_SAVE_INLINE (info->first_gp_reg_save)
17464       || !((strategy & SAVRES_MULTIPLE)
17465            || no_global_regs_above (info->first_gp_reg_save, /*gpr=*/true)))
17466     strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17467
17468   /* Don't bother to try to save things out-of-line if r11 is occupied
17469      by the static chain.  It would require too much fiddling and the
17470      static chain is rarely used anyway.  FPRs are saved w.r.t the stack
17471      pointer on Darwin.  */
17472   if (using_static_chain_p)
17473     strategy |= (DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
17474                 | SAVE_INLINE_GPRS;
17475
17476   /* If we are going to use store multiple, then don't even bother
17477      with the out-of-line routines, since the store-multiple
17478      instruction will always be smaller.  */
17479   if ((strategy & SAVRES_MULTIPLE))
17480     strategy |= SAVE_INLINE_GPRS;
17481
17482   /* The situation is more complicated with load multiple.  We'd
17483      prefer to use the out-of-line routines for restores, since the
17484      "exit" out-of-line routines can handle the restore of LR and the
17485      frame teardown.  However if doesn't make sense to use the
17486      out-of-line routine if that is the only reason we'd need to save
17487      LR, and we can't use the "exit" out-of-line gpr restore if we
17488      have saved some fprs; In those cases it is advantageous to use
17489      load multiple when available.  */
17490   if ((strategy & SAVRES_MULTIPLE)
17491       && (!info->lr_save_p
17492           || info->first_fp_reg_save != 64))
17493     strategy |= REST_INLINE_GPRS;
17494
17495   /* We can only use load multiple or the out-of-line routines to
17496      restore if we've used store multiple or out-of-line routines
17497      in the prologue, i.e. if we've saved all the registers from
17498      first_gp_reg_save.  Otherwise, we risk loading garbage.  */
17499   if ((strategy & (SAVE_INLINE_GPRS | SAVRES_MULTIPLE)) == SAVE_INLINE_GPRS)
17500     strategy |= REST_INLINE_GPRS;
17501
17502   /* Saving CR interferes with the exit routines used on the SPE, so
17503      just punt here.  */
17504   if (TARGET_SPE_ABI
17505       && info->spe_64bit_regs_used
17506       && info->cr_save_p)
17507     strategy |= REST_INLINE_GPRS;
17508
17509 #ifdef POWERPC_LINUX
17510   if (TARGET_64BIT)
17511     {
17512       if (!(strategy & SAVE_INLINE_FPRS))
17513         strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17514       else if (!(strategy & SAVE_INLINE_GPRS)
17515                && info->first_fp_reg_save == 64)
17516         strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
17517     }
17518 #else
17519   if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
17520     strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
17521 #endif
17522   if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
17523     strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17524
17525   return strategy;
17526 }
17527
17528 /* Calculate the stack information for the current function.  This is
17529    complicated by having two separate calling sequences, the AIX calling
17530    sequence and the V.4 calling sequence.
17531
17532    AIX (and Darwin/Mac OS X) stack frames look like:
17533                                                           32-bit  64-bit
17534         SP----> +---------------------------------------+
17535                 | back chain to caller                  | 0       0
17536                 +---------------------------------------+
17537                 | saved CR                              | 4       8 (8-11)
17538                 +---------------------------------------+
17539                 | saved LR                              | 8       16
17540                 +---------------------------------------+
17541                 | reserved for compilers                | 12      24
17542                 +---------------------------------------+
17543                 | reserved for binders                  | 16      32
17544                 +---------------------------------------+
17545                 | saved TOC pointer                     | 20      40
17546                 +---------------------------------------+
17547                 | Parameter save area (P)               | 24      48
17548                 +---------------------------------------+
17549                 | Alloca space (A)                      | 24+P    etc.
17550                 +---------------------------------------+
17551                 | Local variable space (L)              | 24+P+A
17552                 +---------------------------------------+
17553                 | Float/int conversion temporary (X)    | 24+P+A+L
17554                 +---------------------------------------+
17555                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
17556                 +---------------------------------------+
17557                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
17558                 +---------------------------------------+
17559                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
17560                 +---------------------------------------+
17561                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
17562                 +---------------------------------------+
17563                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
17564                 +---------------------------------------+
17565         old SP->| back chain to caller's caller         |
17566                 +---------------------------------------+
17567
17568    The required alignment for AIX configurations is two words (i.e., 8
17569    or 16 bytes).
17570
17571
17572    V.4 stack frames look like:
17573
17574         SP----> +---------------------------------------+
17575                 | back chain to caller                  | 0
17576                 +---------------------------------------+
17577                 | caller's saved LR                     | 4
17578                 +---------------------------------------+
17579                 | Parameter save area (P)               | 8
17580                 +---------------------------------------+
17581                 | Alloca space (A)                      | 8+P
17582                 +---------------------------------------+
17583                 | Varargs save area (V)                 | 8+P+A
17584                 +---------------------------------------+
17585                 | Local variable space (L)              | 8+P+A+V
17586                 +---------------------------------------+
17587                 | Float/int conversion temporary (X)    | 8+P+A+V+L
17588                 +---------------------------------------+
17589                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
17590                 +---------------------------------------+
17591                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
17592                 +---------------------------------------+
17593                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
17594                 +---------------------------------------+
17595                 | SPE: area for 64-bit GP registers     |
17596                 +---------------------------------------+
17597                 | SPE alignment padding                 |
17598                 +---------------------------------------+
17599                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
17600                 +---------------------------------------+
17601                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
17602                 +---------------------------------------+
17603                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
17604                 +---------------------------------------+
17605         old SP->| back chain to caller's caller         |
17606                 +---------------------------------------+
17607
17608    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
17609    given.  (But note below and in sysv4.h that we require only 8 and
17610    may round up the size of our stack frame anyways.  The historical
17611    reason is early versions of powerpc-linux which didn't properly
17612    align the stack at program startup.  A happy side-effect is that
17613    -mno-eabi libraries can be used with -meabi programs.)
17614
17615    The EABI configuration defaults to the V.4 layout.  However,
17616    the stack alignment requirements may differ.  If -mno-eabi is not
17617    given, the required stack alignment is 8 bytes; if -mno-eabi is
17618    given, the required alignment is 16 bytes.  (But see V.4 comment
17619    above.)  */
17620
17621 #ifndef ABI_STACK_BOUNDARY
17622 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
17623 #endif
17624
17625 static rs6000_stack_t *
17626 rs6000_stack_info (void)
17627 {
17628   rs6000_stack_t *info_ptr = &stack_info;
17629   int reg_size = TARGET_32BIT ? 4 : 8;
17630   int ehrd_size;
17631   int save_align;
17632   int first_gp;
17633   HOST_WIDE_INT non_fixed_size;
17634   bool using_static_chain_p;
17635
17636   if (reload_completed && info_ptr->reload_completed)
17637     return info_ptr;
17638
17639   memset (info_ptr, 0, sizeof (*info_ptr));
17640   info_ptr->reload_completed = reload_completed;
17641
17642   if (TARGET_SPE)
17643     {
17644       /* Cache value so we don't rescan instruction chain over and over.  */
17645       if (cfun->machine->insn_chain_scanned_p == 0)
17646         cfun->machine->insn_chain_scanned_p
17647           = spe_func_has_64bit_regs_p () + 1;
17648       info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
17649     }
17650
17651   /* Select which calling sequence.  */
17652   info_ptr->abi = DEFAULT_ABI;
17653
17654   /* Calculate which registers need to be saved & save area size.  */
17655   info_ptr->first_gp_reg_save = first_reg_to_save ();
17656   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
17657      even if it currently looks like we won't.  Reload may need it to
17658      get at a constant; if so, it will have already created a constant
17659      pool entry for it.  */
17660   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
17661        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
17662        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
17663       && crtl->uses_const_pool
17664       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
17665     first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
17666   else
17667     first_gp = info_ptr->first_gp_reg_save;
17668
17669   info_ptr->gp_size = reg_size * (32 - first_gp);
17670
17671   /* For the SPE, we have an additional upper 32-bits on each GPR.
17672      Ideally we should save the entire 64-bits only when the upper
17673      half is used in SIMD instructions.  Since we only record
17674      registers live (not the size they are used in), this proves
17675      difficult because we'd have to traverse the instruction chain at
17676      the right time, taking reload into account.  This is a real pain,
17677      so we opt to save the GPRs in 64-bits always if but one register
17678      gets used in 64-bits.  Otherwise, all the registers in the frame
17679      get saved in 32-bits.
17680
17681      So... since when we save all GPRs (except the SP) in 64-bits, the
17682      traditional GP save area will be empty.  */
17683   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17684     info_ptr->gp_size = 0;
17685
17686   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
17687   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
17688
17689   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
17690   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
17691                                  - info_ptr->first_altivec_reg_save);
17692
17693   /* Does this function call anything?  */
17694   info_ptr->calls_p = (! current_function_is_leaf
17695                        || cfun->machine->ra_needs_full_frame);
17696
17697   /* Determine if we need to save the condition code registers.  */
17698   if (df_regs_ever_live_p (CR2_REGNO)
17699       || df_regs_ever_live_p (CR3_REGNO)
17700       || df_regs_ever_live_p (CR4_REGNO))
17701     {
17702       info_ptr->cr_save_p = 1;
17703       if (DEFAULT_ABI == ABI_V4)
17704         info_ptr->cr_size = reg_size;
17705     }
17706
17707   /* If the current function calls __builtin_eh_return, then we need
17708      to allocate stack space for registers that will hold data for
17709      the exception handler.  */
17710   if (crtl->calls_eh_return)
17711     {
17712       unsigned int i;
17713       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
17714         continue;
17715
17716       /* SPE saves EH registers in 64-bits.  */
17717       ehrd_size = i * (TARGET_SPE_ABI
17718                        && info_ptr->spe_64bit_regs_used != 0
17719                        ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
17720     }
17721   else
17722     ehrd_size = 0;
17723
17724   /* Determine various sizes.  */
17725   info_ptr->reg_size     = reg_size;
17726   info_ptr->fixed_size   = RS6000_SAVE_AREA;
17727   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
17728   info_ptr->parm_size    = RS6000_ALIGN (crtl->outgoing_args_size,
17729                                          TARGET_ALTIVEC ? 16 : 8);
17730   if (FRAME_GROWS_DOWNWARD)
17731     info_ptr->vars_size
17732       += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
17733                        + info_ptr->parm_size,
17734                        ABI_STACK_BOUNDARY / BITS_PER_UNIT)
17735          - (info_ptr->fixed_size + info_ptr->vars_size
17736             + info_ptr->parm_size);
17737
17738   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17739     info_ptr->spe_gp_size = 8 * (32 - first_gp);
17740   else
17741     info_ptr->spe_gp_size = 0;
17742
17743   if (TARGET_ALTIVEC_ABI)
17744     info_ptr->vrsave_mask = compute_vrsave_mask ();
17745   else
17746     info_ptr->vrsave_mask = 0;
17747
17748   if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
17749     info_ptr->vrsave_size  = 4;
17750   else
17751     info_ptr->vrsave_size  = 0;
17752
17753   compute_save_world_info (info_ptr);
17754
17755   /* Calculate the offsets.  */
17756   switch (DEFAULT_ABI)
17757     {
17758     case ABI_NONE:
17759     default:
17760       gcc_unreachable ();
17761
17762     case ABI_AIX:
17763     case ABI_DARWIN:
17764       info_ptr->fp_save_offset   = - info_ptr->fp_size;
17765       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
17766
17767       if (TARGET_ALTIVEC_ABI)
17768         {
17769           info_ptr->vrsave_save_offset
17770             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
17771
17772           /* Align stack so vector save area is on a quadword boundary.
17773              The padding goes above the vectors.  */
17774           if (info_ptr->altivec_size != 0)
17775             info_ptr->altivec_padding_size
17776               = info_ptr->vrsave_save_offset & 0xF;
17777           else
17778             info_ptr->altivec_padding_size = 0;
17779
17780           info_ptr->altivec_save_offset
17781             = info_ptr->vrsave_save_offset
17782             - info_ptr->altivec_padding_size
17783             - info_ptr->altivec_size;
17784           gcc_assert (info_ptr->altivec_size == 0
17785                       || info_ptr->altivec_save_offset % 16 == 0);
17786
17787           /* Adjust for AltiVec case.  */
17788           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
17789         }
17790       else
17791         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
17792       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
17793       info_ptr->lr_save_offset   = 2*reg_size;
17794       break;
17795
17796     case ABI_V4:
17797       info_ptr->fp_save_offset   = - info_ptr->fp_size;
17798       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
17799       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
17800
17801       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17802         {
17803           /* Align stack so SPE GPR save area is aligned on a
17804              double-word boundary.  */
17805           if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
17806             info_ptr->spe_padding_size
17807               = 8 - (-info_ptr->cr_save_offset % 8);
17808           else
17809             info_ptr->spe_padding_size = 0;
17810
17811           info_ptr->spe_gp_save_offset
17812             = info_ptr->cr_save_offset
17813             - info_ptr->spe_padding_size
17814             - info_ptr->spe_gp_size;
17815
17816           /* Adjust for SPE case.  */
17817           info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
17818         }
17819       else if (TARGET_ALTIVEC_ABI)
17820         {
17821           info_ptr->vrsave_save_offset
17822             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
17823
17824           /* Align stack so vector save area is on a quadword boundary.  */
17825           if (info_ptr->altivec_size != 0)
17826             info_ptr->altivec_padding_size
17827               = 16 - (-info_ptr->vrsave_save_offset % 16);
17828           else
17829             info_ptr->altivec_padding_size = 0;
17830
17831           info_ptr->altivec_save_offset
17832             = info_ptr->vrsave_save_offset
17833             - info_ptr->altivec_padding_size
17834             - info_ptr->altivec_size;
17835
17836           /* Adjust for AltiVec case.  */
17837           info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
17838         }
17839       else
17840         info_ptr->ehrd_offset    = info_ptr->cr_save_offset;
17841       info_ptr->ehrd_offset      -= ehrd_size;
17842       info_ptr->lr_save_offset   = reg_size;
17843       break;
17844     }
17845
17846   save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
17847   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
17848                                          + info_ptr->gp_size
17849                                          + info_ptr->altivec_size
17850                                          + info_ptr->altivec_padding_size
17851                                          + info_ptr->spe_gp_size
17852                                          + info_ptr->spe_padding_size
17853                                          + ehrd_size
17854                                          + info_ptr->cr_size
17855                                          + info_ptr->vrsave_size,
17856                                          save_align);
17857
17858   non_fixed_size         = (info_ptr->vars_size
17859                             + info_ptr->parm_size
17860                             + info_ptr->save_size);
17861
17862   info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
17863                                        ABI_STACK_BOUNDARY / BITS_PER_UNIT);
17864
17865   /* Determine if we need to save the link register.  */
17866   if (info_ptr->calls_p
17867       || (DEFAULT_ABI == ABI_AIX
17868           && crtl->profile
17869           && !TARGET_PROFILE_KERNEL)
17870       || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
17871 #ifdef TARGET_RELOCATABLE
17872       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
17873 #endif
17874       || rs6000_ra_ever_killed ())
17875     info_ptr->lr_save_p = 1;
17876
17877   using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
17878                           && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
17879                           && call_used_regs[STATIC_CHAIN_REGNUM]);
17880   info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
17881                                                       using_static_chain_p);
17882
17883   if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
17884       || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
17885       || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
17886       || !(info_ptr->savres_strategy & REST_INLINE_FPRS))
17887     info_ptr->lr_save_p = 1;
17888
17889   if (info_ptr->lr_save_p)
17890     df_set_regs_ever_live (LR_REGNO, true);
17891
17892   /* Determine if we need to allocate any stack frame:
17893
17894      For AIX we need to push the stack if a frame pointer is needed
17895      (because the stack might be dynamically adjusted), if we are
17896      debugging, if we make calls, or if the sum of fp_save, gp_save,
17897      and local variables are more than the space needed to save all
17898      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
17899      + 18*8 = 288 (GPR13 reserved).
17900
17901      For V.4 we don't have the stack cushion that AIX uses, but assume
17902      that the debugger can handle stackless frames.  */
17903
17904   if (info_ptr->calls_p)
17905     info_ptr->push_p = 1;
17906
17907   else if (DEFAULT_ABI == ABI_V4)
17908     info_ptr->push_p = non_fixed_size != 0;
17909
17910   else if (frame_pointer_needed)
17911     info_ptr->push_p = 1;
17912
17913   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
17914     info_ptr->push_p = 1;
17915
17916   else
17917     info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
17918
17919   /* Zero offsets if we're not saving those registers.  */
17920   if (info_ptr->fp_size == 0)
17921     info_ptr->fp_save_offset = 0;
17922
17923   if (info_ptr->gp_size == 0)
17924     info_ptr->gp_save_offset = 0;
17925
17926   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
17927     info_ptr->altivec_save_offset = 0;
17928
17929   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
17930     info_ptr->vrsave_save_offset = 0;
17931
17932   if (! TARGET_SPE_ABI
17933       || info_ptr->spe_64bit_regs_used == 0
17934       || info_ptr->spe_gp_size == 0)
17935     info_ptr->spe_gp_save_offset = 0;
17936
17937   if (! info_ptr->lr_save_p)
17938     info_ptr->lr_save_offset = 0;
17939
17940   if (! info_ptr->cr_save_p)
17941     info_ptr->cr_save_offset = 0;
17942
17943   return info_ptr;
17944 }
17945
17946 /* Return true if the current function uses any GPRs in 64-bit SIMD
17947    mode.  */
17948
17949 static bool
17950 spe_func_has_64bit_regs_p (void)
17951 {
17952   rtx insns, insn;
17953
17954   /* Functions that save and restore all the call-saved registers will
17955      need to save/restore the registers in 64-bits.  */
17956   if (crtl->calls_eh_return
17957       || cfun->calls_setjmp
17958       || crtl->has_nonlocal_goto)
17959     return true;
17960
17961   insns = get_insns ();
17962
17963   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
17964     {
17965       if (INSN_P (insn))
17966         {
17967           rtx i;
17968
17969           /* FIXME: This should be implemented with attributes...
17970
17971                  (set_attr "spe64" "true")....then,
17972                  if (get_spe64(insn)) return true;
17973
17974              It's the only reliable way to do the stuff below.  */
17975
17976           i = PATTERN (insn);
17977           if (GET_CODE (i) == SET)
17978             {
17979               enum machine_mode mode = GET_MODE (SET_SRC (i));
17980
17981               if (SPE_VECTOR_MODE (mode))
17982                 return true;
17983               if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
17984                 return true;
17985             }
17986         }
17987     }
17988
17989   return false;
17990 }
17991
17992 static void
17993 debug_stack_info (rs6000_stack_t *info)
17994 {
17995   const char *abi_string;
17996
17997   if (! info)
17998     info = rs6000_stack_info ();
17999
18000   fprintf (stderr, "\nStack information for function %s:\n",
18001            ((current_function_decl && DECL_NAME (current_function_decl))
18002             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
18003             : "<unknown>"));
18004
18005   switch (info->abi)
18006     {
18007     default:             abi_string = "Unknown";        break;
18008     case ABI_NONE:       abi_string = "NONE";           break;
18009     case ABI_AIX:        abi_string = "AIX";            break;
18010     case ABI_DARWIN:     abi_string = "Darwin";         break;
18011     case ABI_V4:         abi_string = "V.4";            break;
18012     }
18013
18014   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
18015
18016   if (TARGET_ALTIVEC_ABI)
18017     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
18018
18019   if (TARGET_SPE_ABI)
18020     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
18021
18022   if (info->first_gp_reg_save != 32)
18023     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
18024
18025   if (info->first_fp_reg_save != 64)
18026     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
18027
18028   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
18029     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
18030              info->first_altivec_reg_save);
18031
18032   if (info->lr_save_p)
18033     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
18034
18035   if (info->cr_save_p)
18036     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
18037
18038   if (info->vrsave_mask)
18039     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
18040
18041   if (info->push_p)
18042     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
18043
18044   if (info->calls_p)
18045     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
18046
18047   if (info->gp_save_offset)
18048     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
18049
18050   if (info->fp_save_offset)
18051     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
18052
18053   if (info->altivec_save_offset)
18054     fprintf (stderr, "\taltivec_save_offset = %5d\n",
18055              info->altivec_save_offset);
18056
18057   if (info->spe_gp_save_offset)
18058     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
18059              info->spe_gp_save_offset);
18060
18061   if (info->vrsave_save_offset)
18062     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
18063              info->vrsave_save_offset);
18064
18065   if (info->lr_save_offset)
18066     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
18067
18068   if (info->cr_save_offset)
18069     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
18070
18071   if (info->varargs_save_offset)
18072     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
18073
18074   if (info->total_size)
18075     fprintf (stderr, "\ttotal_size          = "HOST_WIDE_INT_PRINT_DEC"\n",
18076              info->total_size);
18077
18078   if (info->vars_size)
18079     fprintf (stderr, "\tvars_size           = "HOST_WIDE_INT_PRINT_DEC"\n",
18080              info->vars_size);
18081
18082   if (info->parm_size)
18083     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
18084
18085   if (info->fixed_size)
18086     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
18087
18088   if (info->gp_size)
18089     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
18090
18091   if (info->spe_gp_size)
18092     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
18093
18094   if (info->fp_size)
18095     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
18096
18097   if (info->altivec_size)
18098     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
18099
18100   if (info->vrsave_size)
18101     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
18102
18103   if (info->altivec_padding_size)
18104     fprintf (stderr, "\taltivec_padding_size= %5d\n",
18105              info->altivec_padding_size);
18106
18107   if (info->spe_padding_size)
18108     fprintf (stderr, "\tspe_padding_size    = %5d\n",
18109              info->spe_padding_size);
18110
18111   if (info->cr_size)
18112     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
18113
18114   if (info->save_size)
18115     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
18116
18117   if (info->reg_size != 4)
18118     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
18119
18120     fprintf (stderr, "\tsave-strategy       =  %04x\n", info->savres_strategy);
18121
18122   fprintf (stderr, "\n");
18123 }
18124
18125 rtx
18126 rs6000_return_addr (int count, rtx frame)
18127 {
18128   /* Currently we don't optimize very well between prolog and body
18129      code and for PIC code the code can be actually quite bad, so
18130      don't try to be too clever here.  */
18131   if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
18132     {
18133       cfun->machine->ra_needs_full_frame = 1;
18134
18135       return
18136         gen_rtx_MEM
18137           (Pmode,
18138            memory_address
18139            (Pmode,
18140             plus_constant (copy_to_reg
18141                            (gen_rtx_MEM (Pmode,
18142                                          memory_address (Pmode, frame))),
18143                            RETURN_ADDRESS_OFFSET)));
18144     }
18145
18146   cfun->machine->ra_need_lr = 1;
18147   return get_hard_reg_initial_val (Pmode, LR_REGNO);
18148 }
18149
18150 /* Say whether a function is a candidate for sibcall handling or not.  */
18151
18152 static bool
18153 rs6000_function_ok_for_sibcall (tree decl, tree exp)
18154 {
18155   tree fntype;
18156
18157   if (decl)
18158     fntype = TREE_TYPE (decl);
18159   else
18160     fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
18161
18162   /* We can't do it if the called function has more vector parameters
18163      than the current function; there's nowhere to put the VRsave code.  */
18164   if (TARGET_ALTIVEC_ABI
18165       && TARGET_ALTIVEC_VRSAVE
18166       && !(decl && decl == current_function_decl))
18167     {
18168       function_args_iterator args_iter;
18169       tree type;
18170       int nvreg = 0;
18171
18172       /* Functions with vector parameters are required to have a
18173          prototype, so the argument type info must be available
18174          here.  */
18175       FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
18176         if (TREE_CODE (type) == VECTOR_TYPE
18177             && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18178           nvreg++;
18179
18180       FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
18181         if (TREE_CODE (type) == VECTOR_TYPE
18182             && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18183           nvreg--;
18184
18185       if (nvreg > 0)
18186         return false;
18187     }
18188
18189   /* Under the AIX ABI we can't allow calls to non-local functions,
18190      because the callee may have a different TOC pointer to the
18191      caller and there's no way to ensure we restore the TOC when we
18192      return.  With the secure-plt SYSV ABI we can't make non-local
18193      calls when -fpic/PIC because the plt call stubs use r30.  */
18194   if (DEFAULT_ABI == ABI_DARWIN
18195       || (DEFAULT_ABI == ABI_AIX
18196           && decl
18197           && !DECL_EXTERNAL (decl)
18198           && (*targetm.binds_local_p) (decl))
18199       || (DEFAULT_ABI == ABI_V4
18200           && (!TARGET_SECURE_PLT
18201               || !flag_pic
18202               || (decl
18203                   && (*targetm.binds_local_p) (decl)))))
18204     {
18205       tree attr_list = TYPE_ATTRIBUTES (fntype);
18206
18207       if (!lookup_attribute ("longcall", attr_list)
18208           || lookup_attribute ("shortcall", attr_list))
18209         return true;
18210     }
18211
18212   return false;
18213 }
18214
18215 /* NULL if INSN insn is valid within a low-overhead loop.
18216    Otherwise return why doloop cannot be applied.
18217    PowerPC uses the COUNT register for branch on table instructions.  */
18218
18219 static const char *
18220 rs6000_invalid_within_doloop (const_rtx insn)
18221 {
18222   if (CALL_P (insn))
18223     return "Function call in the loop.";
18224
18225   if (JUMP_P (insn)
18226       && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
18227           || GET_CODE (PATTERN (insn)) == ADDR_VEC))
18228     return "Computed branch in the loop.";
18229
18230   return NULL;
18231 }
18232
18233 static int
18234 rs6000_ra_ever_killed (void)
18235 {
18236   rtx top;
18237   rtx reg;
18238   rtx insn;
18239
18240   if (cfun->is_thunk)
18241     return 0;
18242
18243   if (cfun->machine->lr_save_state)
18244     return cfun->machine->lr_save_state - 1;
18245
18246   /* regs_ever_live has LR marked as used if any sibcalls are present,
18247      but this should not force saving and restoring in the
18248      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
18249      clobbers LR, so that is inappropriate.  */
18250
18251   /* Also, the prologue can generate a store into LR that
18252      doesn't really count, like this:
18253
18254         move LR->R0
18255         bcl to set PIC register
18256         move LR->R31
18257         move R0->LR
18258
18259      When we're called from the epilogue, we need to avoid counting
18260      this as a store.  */
18261
18262   push_topmost_sequence ();
18263   top = get_insns ();
18264   pop_topmost_sequence ();
18265   reg = gen_rtx_REG (Pmode, LR_REGNO);
18266
18267   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
18268     {
18269       if (INSN_P (insn))
18270         {
18271           if (CALL_P (insn))
18272             {
18273               if (!SIBLING_CALL_P (insn))
18274                 return 1;
18275             }
18276           else if (find_regno_note (insn, REG_INC, LR_REGNO))
18277             return 1;
18278           else if (set_of (reg, insn) != NULL_RTX
18279                    && !prologue_epilogue_contains (insn))
18280             return 1;
18281         }
18282     }
18283   return 0;
18284 }
18285 \f
18286 /* Emit instructions needed to load the TOC register.
18287    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
18288    a constant pool; or for SVR4 -fpic.  */
18289
18290 void
18291 rs6000_emit_load_toc_table (int fromprolog)
18292 {
18293   rtx dest;
18294   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
18295
18296   if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
18297     {
18298       char buf[30];
18299       rtx lab, tmp1, tmp2, got;
18300
18301       lab = gen_label_rtx ();
18302       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
18303       lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18304       if (flag_pic == 2)
18305         got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18306       else
18307         got = rs6000_got_sym ();
18308       tmp1 = tmp2 = dest;
18309       if (!fromprolog)
18310         {
18311           tmp1 = gen_reg_rtx (Pmode);
18312           tmp2 = gen_reg_rtx (Pmode);
18313         }
18314       emit_insn (gen_load_toc_v4_PIC_1 (lab));
18315       emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
18316       emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
18317       emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
18318     }
18319   else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
18320     {
18321       emit_insn (gen_load_toc_v4_pic_si ());
18322       emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18323     }
18324   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
18325     {
18326       char buf[30];
18327       rtx temp0 = (fromprolog
18328                    ? gen_rtx_REG (Pmode, 0)
18329                    : gen_reg_rtx (Pmode));
18330
18331       if (fromprolog)
18332         {
18333           rtx symF, symL;
18334
18335           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
18336           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18337
18338           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
18339           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18340
18341           emit_insn (gen_load_toc_v4_PIC_1 (symF));
18342           emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18343           emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
18344         }
18345       else
18346         {
18347           rtx tocsym, lab;
18348
18349           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18350           lab = gen_label_rtx ();
18351           emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
18352           emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18353           if (TARGET_LINK_STACK)
18354             emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
18355           emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
18356         }
18357       emit_insn (gen_addsi3 (dest, temp0, dest));
18358     }
18359   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
18360     {
18361       /* This is for AIX code running in non-PIC ELF32.  */
18362       char buf[30];
18363       rtx realsym;
18364       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
18365       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18366
18367       emit_insn (gen_elf_high (dest, realsym));
18368       emit_insn (gen_elf_low (dest, dest, realsym));
18369     }
18370   else
18371     {
18372       gcc_assert (DEFAULT_ABI == ABI_AIX);
18373
18374       if (TARGET_32BIT)
18375         emit_insn (gen_load_toc_aix_si (dest));
18376       else
18377         emit_insn (gen_load_toc_aix_di (dest));
18378     }
18379 }
18380
18381 /* Emit instructions to restore the link register after determining where
18382    its value has been stored.  */
18383
18384 void
18385 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
18386 {
18387   rs6000_stack_t *info = rs6000_stack_info ();
18388   rtx operands[2];
18389
18390   operands[0] = source;
18391   operands[1] = scratch;
18392
18393   if (info->lr_save_p)
18394     {
18395       rtx frame_rtx = stack_pointer_rtx;
18396       HOST_WIDE_INT sp_offset = 0;
18397       rtx tmp;
18398
18399       if (frame_pointer_needed
18400           || cfun->calls_alloca
18401           || info->total_size > 32767)
18402         {
18403           tmp = gen_frame_mem (Pmode, frame_rtx);
18404           emit_move_insn (operands[1], tmp);
18405           frame_rtx = operands[1];
18406         }
18407       else if (info->push_p)
18408         sp_offset = info->total_size;
18409
18410       tmp = plus_constant (frame_rtx, info->lr_save_offset + sp_offset);
18411       tmp = gen_frame_mem (Pmode, tmp);
18412       emit_move_insn (tmp, operands[0]);
18413     }
18414   else
18415     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
18416
18417   /* Freeze lr_save_p.  We've just emitted rtl that depends on the
18418      state of lr_save_p so any change from here on would be a bug.  In
18419      particular, stop rs6000_ra_ever_killed from considering the SET
18420      of lr we may have added just above.  */ 
18421   cfun->machine->lr_save_state = info->lr_save_p + 1;
18422 }
18423
18424 static GTY(()) alias_set_type set = -1;
18425
18426 alias_set_type
18427 get_TOC_alias_set (void)
18428 {
18429   if (set == -1)
18430     set = new_alias_set ();
18431   return set;
18432 }
18433
18434 /* This returns nonzero if the current function uses the TOC.  This is
18435    determined by the presence of (use (unspec ... UNSPEC_TOC)), which
18436    is generated by the ABI_V4 load_toc_* patterns.  */
18437 #if TARGET_ELF
18438 static int
18439 uses_TOC (void)
18440 {
18441   rtx insn;
18442
18443   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18444     if (INSN_P (insn))
18445       {
18446         rtx pat = PATTERN (insn);
18447         int i;
18448
18449         if (GET_CODE (pat) == PARALLEL)
18450           for (i = 0; i < XVECLEN (pat, 0); i++)
18451             {
18452               rtx sub = XVECEXP (pat, 0, i);
18453               if (GET_CODE (sub) == USE)
18454                 {
18455                   sub = XEXP (sub, 0);
18456                   if (GET_CODE (sub) == UNSPEC
18457                       && XINT (sub, 1) == UNSPEC_TOC)
18458                     return 1;
18459                 }
18460             }
18461       }
18462   return 0;
18463 }
18464 #endif
18465
18466 rtx
18467 create_TOC_reference (rtx symbol, rtx largetoc_reg)
18468 {
18469   rtx tocrel, tocreg;
18470
18471   if (TARGET_DEBUG_ADDR)
18472     {
18473       if (GET_CODE (symbol) == SYMBOL_REF)
18474         fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
18475                  XSTR (symbol, 0));
18476       else
18477         {
18478           fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
18479                    GET_RTX_NAME (GET_CODE (symbol)));
18480           debug_rtx (symbol);
18481         }
18482     }
18483
18484   if (!can_create_pseudo_p ())
18485     df_set_regs_ever_live (TOC_REGISTER, true);
18486
18487   tocrel = gen_rtx_CONST (Pmode,
18488                           gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol),
18489                                           UNSPEC_TOCREL));
18490   tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
18491   if (TARGET_CMODEL != CMODEL_SMALL)
18492     {
18493       rtx hi = gen_rtx_CONST (Pmode,
18494                               gen_rtx_PLUS (Pmode, tocreg, 
18495                                             gen_rtx_HIGH (Pmode, tocrel)));
18496       if (largetoc_reg != NULL)
18497         {
18498           emit_move_insn (largetoc_reg, hi);
18499           hi = largetoc_reg;
18500         }
18501       return gen_rtx_LO_SUM (Pmode, hi, copy_rtx (tocrel));
18502     }
18503   else
18504     return gen_rtx_PLUS (Pmode, tocreg, tocrel);
18505 }
18506
18507 /* Issue assembly directives that create a reference to the given DWARF
18508    FRAME_TABLE_LABEL from the current function section.  */
18509 void
18510 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
18511 {
18512   fprintf (asm_out_file, "\t.ref %s\n",
18513            TARGET_STRIP_NAME_ENCODING (frame_table_label));
18514 }
18515 \f
18516 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
18517    and the change to the stack pointer.  */
18518
18519 static void
18520 rs6000_emit_stack_tie (void)
18521 {
18522   rtx mem = gen_frame_mem (BLKmode,
18523                            gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
18524
18525   emit_insn (gen_stack_tie (mem));
18526 }
18527
18528 /* Emit the correct code for allocating stack space, as insns.
18529    If COPY_REG, make sure a copy of the old frame is left there.
18530    The generated code may use hard register 0 as a temporary.  */
18531
18532 static void
18533 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg)
18534 {
18535   rtx insn;
18536   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18537   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
18538   rtx todec = gen_int_mode (-size, Pmode);
18539   rtx par, set, mem;
18540
18541   if (INTVAL (todec) != -size)
18542     {
18543       warning (0, "stack frame too large");
18544       emit_insn (gen_trap ());
18545       return;
18546     }
18547
18548   if (crtl->limit_stack)
18549     {
18550       if (REG_P (stack_limit_rtx)
18551           && REGNO (stack_limit_rtx) > 1
18552           && REGNO (stack_limit_rtx) <= 31)
18553         {
18554           emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
18555           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18556                                     const0_rtx));
18557         }
18558       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
18559                && TARGET_32BIT
18560                && DEFAULT_ABI == ABI_V4)
18561         {
18562           rtx toload = gen_rtx_CONST (VOIDmode,
18563                                       gen_rtx_PLUS (Pmode,
18564                                                     stack_limit_rtx,
18565                                                     GEN_INT (size)));
18566
18567           emit_insn (gen_elf_high (tmp_reg, toload));
18568           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
18569           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18570                                     const0_rtx));
18571         }
18572       else
18573         warning (0, "stack limit expression is not supported");
18574     }
18575
18576   if (copy_reg)
18577     emit_move_insn (copy_reg, stack_reg);
18578
18579   if (size > 32767)
18580     {
18581       /* Need a note here so that try_split doesn't get confused.  */
18582       if (get_last_insn () == NULL_RTX)
18583         emit_note (NOTE_INSN_DELETED);
18584       insn = emit_move_insn (tmp_reg, todec);
18585       try_split (PATTERN (insn), insn, 0);
18586       todec = tmp_reg;
18587     }
18588   
18589   insn = emit_insn (TARGET_32BIT
18590                     ? gen_movsi_update_stack (stack_reg, stack_reg,
18591                                         todec, stack_reg)
18592                     : gen_movdi_di_update_stack (stack_reg, stack_reg,
18593                                            todec, stack_reg));
18594   /* Since we didn't use gen_frame_mem to generate the MEM, grab
18595      it now and set the alias set/attributes. The above gen_*_update
18596      calls will generate a PARALLEL with the MEM set being the first
18597      operation. */
18598   par = PATTERN (insn);
18599   gcc_assert (GET_CODE (par) == PARALLEL);
18600   set = XVECEXP (par, 0, 0);
18601   gcc_assert (GET_CODE (set) == SET);
18602   mem = SET_DEST (set);
18603   gcc_assert (MEM_P (mem));
18604   MEM_NOTRAP_P (mem) = 1;
18605   set_mem_alias_set (mem, get_frame_alias_set ());
18606
18607   RTX_FRAME_RELATED_P (insn) = 1;
18608   add_reg_note (insn, REG_FRAME_RELATED_EXPR,
18609                 gen_rtx_SET (VOIDmode, stack_reg,
18610                              gen_rtx_PLUS (Pmode, stack_reg,
18611                                            GEN_INT (-size))));
18612 }
18613
18614 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
18615
18616 #if PROBE_INTERVAL > 32768
18617 #error Cannot use indexed addressing mode for stack probing
18618 #endif
18619
18620 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
18621    inclusive.  These are offsets from the current stack pointer.  */
18622
18623 static void
18624 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
18625 {
18626   /* See if we have a constant small number of probes to generate.  If so,
18627      that's the easy case.  */
18628   if (first + size <= 32768)
18629     {
18630       HOST_WIDE_INT i;
18631
18632       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
18633          it exceeds SIZE.  If only one probe is needed, this will not
18634          generate any code.  Then probe at FIRST + SIZE.  */
18635       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
18636         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
18637
18638       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
18639     }
18640
18641   /* Otherwise, do the same as above, but in a loop.  Note that we must be
18642      extra careful with variables wrapping around because we might be at
18643      the very top (or the very bottom) of the address space and we have
18644      to be able to handle this case properly; in particular, we use an
18645      equality test for the loop condition.  */
18646   else
18647     {
18648       HOST_WIDE_INT rounded_size;
18649       rtx r12 = gen_rtx_REG (Pmode, 12);
18650       rtx r0 = gen_rtx_REG (Pmode, 0);
18651
18652       /* Sanity check for the addressing mode we're going to use.  */
18653       gcc_assert (first <= 32768);
18654
18655       /* Step 1: round SIZE to the previous multiple of the interval.  */
18656
18657       rounded_size = size & -PROBE_INTERVAL;
18658
18659
18660       /* Step 2: compute initial and final value of the loop counter.  */
18661
18662       /* TEST_ADDR = SP + FIRST.  */
18663       emit_insn (gen_rtx_SET (VOIDmode, r12,
18664                               plus_constant (stack_pointer_rtx, -first)));
18665
18666       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
18667       if (rounded_size > 32768)
18668         {
18669           emit_move_insn (r0, GEN_INT (-rounded_size));
18670           emit_insn (gen_rtx_SET (VOIDmode, r0,
18671                                   gen_rtx_PLUS (Pmode, r12, r0)));
18672         }
18673       else
18674         emit_insn (gen_rtx_SET (VOIDmode, r0,
18675                                 plus_constant (r12, -rounded_size)));
18676
18677
18678       /* Step 3: the loop
18679
18680          while (TEST_ADDR != LAST_ADDR)
18681            {
18682              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
18683              probe at TEST_ADDR
18684            }
18685
18686          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
18687          until it is equal to ROUNDED_SIZE.  */
18688
18689       if (TARGET_64BIT)
18690         emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
18691       else
18692         emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
18693
18694
18695       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
18696          that SIZE is equal to ROUNDED_SIZE.  */
18697
18698       if (size != rounded_size)
18699         emit_stack_probe (plus_constant (r12, rounded_size - size));
18700     }
18701 }
18702
18703 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
18704    absolute addresses.  */
18705
18706 const char *
18707 output_probe_stack_range (rtx reg1, rtx reg2)
18708 {
18709   static int labelno = 0;
18710   char loop_lab[32], end_lab[32];
18711   rtx xops[2];
18712
18713   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
18714   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
18715
18716   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
18717
18718   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
18719   xops[0] = reg1;
18720   xops[1] = reg2;
18721   if (TARGET_64BIT)
18722     output_asm_insn ("{cmp|cmpd} 0,%0,%1", xops);
18723   else
18724     output_asm_insn ("{cmp|cmpw} 0,%0,%1", xops);
18725
18726   fputs ("\tbeq 0,", asm_out_file);
18727   assemble_name_raw (asm_out_file, end_lab);
18728   fputc ('\n', asm_out_file);
18729
18730   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
18731   xops[1] = GEN_INT (-PROBE_INTERVAL);
18732   output_asm_insn ("{cal %0,%1(%0)|addi %0,%0,%1}", xops);
18733
18734   /* Probe at TEST_ADDR and branch.  */
18735   xops[1] = gen_rtx_REG (Pmode, 0);
18736   output_asm_insn ("{st|stw} %1,0(%0)", xops);
18737   fprintf (asm_out_file, "\tb ");
18738   assemble_name_raw (asm_out_file, loop_lab);
18739   fputc ('\n', asm_out_file);
18740
18741   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
18742
18743   return "";
18744 }
18745
18746 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
18747    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
18748    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
18749    deduce these equivalences by itself so it wasn't necessary to hold
18750    its hand so much.  */
18751
18752 static rtx
18753 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
18754                       rtx reg2, rtx rreg)
18755 {
18756   rtx real, temp;
18757
18758   /* copy_rtx will not make unique copies of registers, so we need to
18759      ensure we don't have unwanted sharing here.  */
18760   if (reg == reg2)
18761     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18762
18763   if (reg == rreg)
18764     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18765
18766   real = copy_rtx (PATTERN (insn));
18767
18768   if (reg2 != NULL_RTX)
18769     real = replace_rtx (real, reg2, rreg);
18770
18771   real = replace_rtx (real, reg,
18772                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
18773                                                         STACK_POINTER_REGNUM),
18774                                     GEN_INT (val)));
18775
18776   /* We expect that 'real' is either a SET or a PARALLEL containing
18777      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
18778      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
18779
18780   if (GET_CODE (real) == SET)
18781     {
18782       rtx set = real;
18783
18784       temp = simplify_rtx (SET_SRC (set));
18785       if (temp)
18786         SET_SRC (set) = temp;
18787       temp = simplify_rtx (SET_DEST (set));
18788       if (temp)
18789         SET_DEST (set) = temp;
18790       if (GET_CODE (SET_DEST (set)) == MEM)
18791         {
18792           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18793           if (temp)
18794             XEXP (SET_DEST (set), 0) = temp;
18795         }
18796     }
18797   else
18798     {
18799       int i;
18800
18801       gcc_assert (GET_CODE (real) == PARALLEL);
18802       for (i = 0; i < XVECLEN (real, 0); i++)
18803         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
18804           {
18805             rtx set = XVECEXP (real, 0, i);
18806
18807             temp = simplify_rtx (SET_SRC (set));
18808             if (temp)
18809               SET_SRC (set) = temp;
18810             temp = simplify_rtx (SET_DEST (set));
18811             if (temp)
18812               SET_DEST (set) = temp;
18813             if (GET_CODE (SET_DEST (set)) == MEM)
18814               {
18815                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18816                 if (temp)
18817                   XEXP (SET_DEST (set), 0) = temp;
18818               }
18819             RTX_FRAME_RELATED_P (set) = 1;
18820           }
18821     }
18822
18823   RTX_FRAME_RELATED_P (insn) = 1;
18824   add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
18825
18826   return insn;
18827 }
18828
18829 /* Returns an insn that has a vrsave set operation with the
18830    appropriate CLOBBERs.  */
18831
18832 static rtx
18833 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
18834 {
18835   int nclobs, i;
18836   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
18837   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
18838
18839   clobs[0]
18840     = gen_rtx_SET (VOIDmode,
18841                    vrsave,
18842                    gen_rtx_UNSPEC_VOLATILE (SImode,
18843                                             gen_rtvec (2, reg, vrsave),
18844                                             UNSPECV_SET_VRSAVE));
18845
18846   nclobs = 1;
18847
18848   /* We need to clobber the registers in the mask so the scheduler
18849      does not move sets to VRSAVE before sets of AltiVec registers.
18850
18851      However, if the function receives nonlocal gotos, reload will set
18852      all call saved registers live.  We will end up with:
18853
18854         (set (reg 999) (mem))
18855         (parallel [ (set (reg vrsave) (unspec blah))
18856                     (clobber (reg 999))])
18857
18858      The clobber will cause the store into reg 999 to be dead, and
18859      flow will attempt to delete an epilogue insn.  In this case, we
18860      need an unspec use/set of the register.  */
18861
18862   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
18863     if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
18864       {
18865         if (!epiloguep || call_used_regs [i])
18866           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
18867                                              gen_rtx_REG (V4SImode, i));
18868         else
18869           {
18870             rtx reg = gen_rtx_REG (V4SImode, i);
18871
18872             clobs[nclobs++]
18873               = gen_rtx_SET (VOIDmode,
18874                              reg,
18875                              gen_rtx_UNSPEC (V4SImode,
18876                                              gen_rtvec (1, reg), 27));
18877           }
18878       }
18879
18880   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
18881
18882   for (i = 0; i < nclobs; ++i)
18883     XVECEXP (insn, 0, i) = clobs[i];
18884
18885   return insn;
18886 }
18887
18888 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
18889    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
18890
18891 static rtx
18892 emit_frame_save (rtx frame_reg, rtx frame_ptr, enum machine_mode mode,
18893                  unsigned int regno, int offset, HOST_WIDE_INT total_size)
18894 {
18895   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
18896   rtx replacea, replaceb;
18897
18898   int_rtx = GEN_INT (offset);
18899
18900   /* Some cases that need register indexed addressing.  */
18901   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
18902       || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
18903       || (TARGET_E500_DOUBLE && mode == DFmode)
18904       || (TARGET_SPE_ABI
18905           && SPE_VECTOR_MODE (mode)
18906           && !SPE_CONST_OFFSET_OK (offset)))
18907     {
18908       /* Whomever calls us must make sure r11 is available in the
18909          flow path of instructions in the prologue.  */
18910       offset_rtx = gen_rtx_REG (Pmode, 11);
18911       emit_move_insn (offset_rtx, int_rtx);
18912
18913       replacea = offset_rtx;
18914       replaceb = int_rtx;
18915     }
18916   else
18917     {
18918       offset_rtx = int_rtx;
18919       replacea = NULL_RTX;
18920       replaceb = NULL_RTX;
18921     }
18922
18923   reg = gen_rtx_REG (mode, regno);
18924   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
18925   mem = gen_frame_mem (mode, addr);
18926
18927   insn = emit_move_insn (mem, reg);
18928
18929   return rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
18930 }
18931
18932 /* Emit an offset memory reference suitable for a frame store, while
18933    converting to a valid addressing mode.  */
18934
18935 static rtx
18936 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
18937 {
18938   rtx int_rtx, offset_rtx;
18939
18940   int_rtx = GEN_INT (offset);
18941
18942   if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
18943       || (TARGET_E500_DOUBLE && mode == DFmode))
18944     {
18945       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
18946       emit_move_insn (offset_rtx, int_rtx);
18947     }
18948   else
18949     offset_rtx = int_rtx;
18950
18951   return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
18952 }
18953
18954 /* Look for user-defined global regs.  We should not save and restore these,
18955    and cannot use stmw/lmw if there are any in its range.  */
18956
18957 static bool
18958 no_global_regs_above (int first, bool gpr)
18959 {
18960   int i;
18961   int last = gpr ? 32 : 64;
18962   for (i = first; i < last; i++)
18963     if (global_regs[i])
18964       return false;
18965   return true;
18966 }
18967
18968 #ifndef TARGET_FIX_AND_CONTINUE
18969 #define TARGET_FIX_AND_CONTINUE 0
18970 #endif
18971
18972 /* It's really GPR 13 and FPR 14, but we need the smaller of the two.  */
18973 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
18974 #define LAST_SAVRES_REGISTER 31
18975 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
18976
18977 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][8];
18978
18979 /* Temporary holding space for an out-of-line register save/restore
18980    routine name.  */
18981 static char savres_routine_name[30];
18982
18983 /* Return the name for an out-of-line register save/restore routine.
18984    We are saving/restoring GPRs if GPR is true.  */
18985
18986 static char *
18987 rs6000_savres_routine_name (rs6000_stack_t *info, int regno,
18988                             bool savep, bool gpr, bool lr)
18989 {
18990   const char *prefix = "";
18991   const char *suffix = "";
18992
18993   /* Different targets are supposed to define
18994      {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
18995      routine name could be defined with:
18996
18997      sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
18998
18999      This is a nice idea in practice, but in reality, things are
19000      complicated in several ways:
19001
19002      - ELF targets have save/restore routines for GPRs.
19003
19004      - SPE targets use different prefixes for 32/64-bit registers, and
19005        neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
19006
19007      - PPC64 ELF targets have routines for save/restore of GPRs that
19008        differ in what they do with the link register, so having a set
19009        prefix doesn't work.  (We only use one of the save routines at
19010        the moment, though.)
19011
19012      - PPC32 elf targets have "exit" versions of the restore routines
19013        that restore the link register and can save some extra space.
19014        These require an extra suffix.  (There are also "tail" versions
19015        of the restore routines and "GOT" versions of the save routines,
19016        but we don't generate those at present.  Same problems apply,
19017        though.)
19018
19019      We deal with all this by synthesizing our own prefix/suffix and
19020      using that for the simple sprintf call shown above.  */
19021   if (TARGET_SPE)
19022     {
19023       /* No floating point saves on the SPE.  */
19024       gcc_assert (gpr);
19025
19026       if (savep)
19027         prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
19028       else
19029         prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
19030
19031       if (lr)
19032         suffix = "_x";
19033     }
19034   else if (DEFAULT_ABI == ABI_V4)
19035     {
19036       if (TARGET_64BIT)
19037         goto aix_names;
19038
19039       if (gpr)
19040         prefix = savep ? "_savegpr_" : "_restgpr_";
19041       else
19042         prefix = savep ? "_savefpr_" : "_restfpr_";
19043
19044       if (lr)
19045         suffix = "_x";
19046     }
19047   else if (DEFAULT_ABI == ABI_AIX)
19048     {
19049 #ifndef POWERPC_LINUX
19050       /* No out-of-line save/restore routines for GPRs on AIX.  */
19051       gcc_assert (!TARGET_AIX || !gpr);
19052 #endif
19053
19054     aix_names:
19055       if (gpr)
19056         prefix = (savep
19057                   ? (lr ? "_savegpr0_" : "_savegpr1_")
19058                   : (lr ? "_restgpr0_" : "_restgpr1_"));
19059 #ifdef POWERPC_LINUX
19060       else if (lr)
19061         prefix = (savep ? "_savefpr_" : "_restfpr_");
19062 #endif
19063       else
19064         {
19065           prefix = savep ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
19066           suffix = savep ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
19067         }
19068     }
19069
19070    if (DEFAULT_ABI == ABI_DARWIN)
19071     {
19072       /* The Darwin approach is (slightly) different, in order to be
19073          compatible with code generated by the system toolchain.  There is a
19074          single symbol for the start of save sequence, and the code here
19075          embeds an offset into that code on the basis of the first register
19076          to be saved.  */
19077       prefix = savep ? "save" : "rest" ;
19078       if (gpr)
19079         sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31",
19080                prefix, (lr ? "x" : ""), (regno == 13 ? "" : "+"),
19081                (regno-13) * 4, prefix, regno);
19082       else
19083         sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31",
19084                prefix, (regno == 14 ? "" : "+"),  (regno-14) * 4, prefix, regno);
19085     }
19086   else
19087     sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
19088
19089   return savres_routine_name;
19090 }
19091
19092 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
19093    We are saving/restoring GPRs if GPR is true.  */
19094
19095 static rtx
19096 rs6000_savres_routine_sym (rs6000_stack_t *info, bool savep,
19097                            bool gpr, bool lr)
19098 {
19099   int regno = gpr ? info->first_gp_reg_save : (info->first_fp_reg_save - 32);
19100   rtx sym;
19101   int select = ((savep ? 1 : 0) << 2
19102                 | ((TARGET_SPE_ABI
19103                     /* On the SPE, we never have any FPRs, but we do have
19104                        32/64-bit versions of the routines.  */
19105                     ? (info->spe_64bit_regs_used ? 1 : 0)
19106                     : (gpr ? 1 : 0)) << 1)
19107                 | (lr ? 1: 0));
19108
19109   /* Don't generate bogus routine names.  */
19110   gcc_assert (FIRST_SAVRES_REGISTER <= regno
19111               && regno <= LAST_SAVRES_REGISTER);
19112
19113   sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
19114
19115   if (sym == NULL)
19116     {
19117       char *name;
19118
19119       name = rs6000_savres_routine_name (info, regno, savep, gpr, lr);
19120
19121       sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
19122         = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
19123       SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
19124     }
19125
19126   return sym;
19127 }
19128
19129 /* Emit a sequence of insns, including a stack tie if needed, for
19130    resetting the stack pointer.  If SAVRES is true, then don't reset the
19131    stack pointer, but move the base of the frame into r11 for use by
19132    out-of-line register restore routines.  */
19133
19134 static rtx
19135 rs6000_emit_stack_reset (rs6000_stack_t *info,
19136                          rtx sp_reg_rtx, rtx frame_reg_rtx,
19137                          int sp_offset, bool savres)
19138 {
19139   /* This blockage is needed so that sched doesn't decide to move
19140      the sp change before the register restores.  */
19141   if (DEFAULT_ABI == ABI_V4
19142       || (TARGET_SPE_ABI
19143           && info->spe_64bit_regs_used != 0
19144           && info->first_gp_reg_save != 32))
19145     rs6000_emit_stack_tie ();
19146   
19147   if (frame_reg_rtx != sp_reg_rtx)
19148     {
19149       if (sp_offset != 0)
19150         {
19151           rtx dest_reg = savres ? gen_rtx_REG (Pmode, 11) : sp_reg_rtx;
19152           rtx insn = emit_insn (gen_add3_insn (dest_reg, frame_reg_rtx,
19153                                                GEN_INT (sp_offset)));
19154           if (!savres)
19155             return insn;
19156         }
19157       else if (!savres)
19158         return emit_move_insn (sp_reg_rtx, frame_reg_rtx);
19159     }
19160   else if (sp_offset != 0)
19161     {
19162       /* If we are restoring registers out-of-line, we will be using the
19163          "exit" variants of the restore routines, which will reset the
19164          stack for us.  But we do need to point r11 into the right place
19165          for those routines.  */
19166       rtx dest_reg = (savres
19167                       ? gen_rtx_REG (Pmode, 11)
19168                       : sp_reg_rtx);
19169
19170       rtx insn = emit_insn (gen_add3_insn (dest_reg, sp_reg_rtx,
19171                                            GEN_INT (sp_offset)));
19172       if (!savres)
19173         return insn;
19174     }
19175   return NULL_RTX;
19176 }
19177
19178 /* Construct a parallel rtx describing the effect of a call to an
19179    out-of-line register save/restore routine, and emit the insn
19180    or jump_insn as appropriate.  */
19181
19182 static rtx
19183 rs6000_emit_savres_rtx (rs6000_stack_t *info,
19184                         rtx frame_reg_rtx, int save_area_offset,
19185                         enum machine_mode reg_mode,
19186                         bool savep, bool gpr, bool lr)
19187 {
19188   int i;
19189   int offset, start_reg, end_reg, n_regs, use_reg;
19190   int reg_size = GET_MODE_SIZE (reg_mode);
19191   rtx sym;
19192   rtvec p;
19193   rtx par, insn;
19194
19195   offset = 0;
19196   start_reg = (gpr
19197                ? info->first_gp_reg_save
19198                : info->first_fp_reg_save);
19199   end_reg = gpr ? 32 : 64;
19200   n_regs = end_reg - start_reg;
19201   p = rtvec_alloc ((lr ? 4 : 3) + n_regs);
19202
19203   if (!savep && lr)
19204     RTVEC_ELT (p, offset++) = ret_rtx;
19205
19206   RTVEC_ELT (p, offset++)
19207     = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
19208
19209   sym = rs6000_savres_routine_sym (info, savep, gpr, lr);
19210   RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
19211   use_reg = DEFAULT_ABI == ABI_AIX ? (gpr && !lr ? 12 : 1)
19212                                    : DEFAULT_ABI == ABI_DARWIN && !gpr ? 1
19213                                                                        : 11;
19214   RTVEC_ELT (p, offset++)
19215     = gen_rtx_USE (VOIDmode,
19216                    gen_rtx_REG (Pmode, use_reg));
19217
19218   for (i = 0; i < end_reg - start_reg; i++)
19219     {
19220       rtx addr, reg, mem;
19221       reg = gen_rtx_REG (reg_mode, start_reg + i);
19222       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19223                            GEN_INT (save_area_offset + reg_size*i));
19224       mem = gen_frame_mem (reg_mode, addr);
19225
19226       RTVEC_ELT (p, i + offset) = gen_rtx_SET (VOIDmode,
19227                                                savep ? mem : reg,
19228                                                savep ? reg : mem);
19229     }
19230
19231   if (savep && lr)
19232     {
19233       rtx addr, reg, mem;
19234       reg = gen_rtx_REG (Pmode, 0);
19235       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19236                            GEN_INT (info->lr_save_offset));
19237       mem = gen_frame_mem (Pmode, addr);
19238       RTVEC_ELT (p, i + offset) = gen_rtx_SET (VOIDmode, mem, reg);
19239     }
19240
19241   par = gen_rtx_PARALLEL (VOIDmode, p);
19242
19243   if (!savep && lr)
19244     {
19245       insn = emit_jump_insn (par);
19246       JUMP_LABEL (insn) = ret_rtx;
19247     }
19248   else
19249     insn = emit_insn (par);
19250   return insn;
19251 }
19252
19253 /* Determine whether the gp REG is really used.  */
19254
19255 static bool
19256 rs6000_reg_live_or_pic_offset_p (int reg)
19257 {
19258   /* If the function calls eh_return, claim used all the registers that would
19259      be checked for liveness otherwise.  This is required for the PIC offset
19260      register with -mminimal-toc on AIX, as it is advertised as "fixed" for
19261      register allocation purposes in this case.  */
19262
19263   return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
19264            && (!call_used_regs[reg]
19265                || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19266                    && !TARGET_SINGLE_PIC_BASE
19267                    && TARGET_TOC && TARGET_MINIMAL_TOC)))
19268           || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19269               && !TARGET_SINGLE_PIC_BASE
19270               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
19271                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
19272 }
19273
19274 /* Emit function prologue as insns.  */
19275
19276 void
19277 rs6000_emit_prologue (void)
19278 {
19279   rs6000_stack_t *info = rs6000_stack_info ();
19280   enum machine_mode reg_mode = Pmode;
19281   int reg_size = TARGET_32BIT ? 4 : 8;
19282   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
19283   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
19284   rtx frame_reg_rtx = sp_reg_rtx;
19285   rtx cr_save_rtx = NULL_RTX;
19286   rtx insn;
19287   int strategy;
19288   int saving_FPRs_inline;
19289   int saving_GPRs_inline;
19290   int using_store_multiple;
19291   int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19292                               && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19293                               && call_used_regs[STATIC_CHAIN_REGNUM]);
19294   HOST_WIDE_INT sp_offset = 0;
19295
19296   if (flag_stack_usage_info)
19297     current_function_static_stack_size = info->total_size;
19298
19299   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
19300     rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
19301
19302   if (TARGET_FIX_AND_CONTINUE)
19303     {
19304       /* gdb on darwin arranges to forward a function from the old
19305          address by modifying the first 5 instructions of the function
19306          to branch to the overriding function.  This is necessary to
19307          permit function pointers that point to the old function to
19308          actually forward to the new function.  */
19309       emit_insn (gen_nop ());
19310       emit_insn (gen_nop ());
19311       emit_insn (gen_nop ());
19312       emit_insn (gen_nop ());
19313       emit_insn (gen_nop ());
19314     }
19315
19316   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
19317     {
19318       reg_mode = V2SImode;
19319       reg_size = 8;
19320     }
19321
19322   strategy = info->savres_strategy;
19323   using_store_multiple = strategy & SAVRES_MULTIPLE;
19324   saving_FPRs_inline = strategy & SAVE_INLINE_FPRS;
19325   saving_GPRs_inline = strategy & SAVE_INLINE_GPRS;
19326
19327   /* For V.4, update stack before we do any saving and set back pointer.  */
19328   if (! WORLD_SAVE_P (info)
19329       && info->push_p
19330       && (DEFAULT_ABI == ABI_V4
19331           || crtl->calls_eh_return))
19332     {
19333       bool need_r11 = (TARGET_SPE
19334                        ? (!saving_GPRs_inline
19335                           && info->spe_64bit_regs_used == 0)
19336                        : (!saving_FPRs_inline || !saving_GPRs_inline));
19337       rtx copy_reg = need_r11 ? gen_rtx_REG (Pmode, 11) : NULL;
19338
19339       if (info->total_size < 32767)
19340         sp_offset = info->total_size;
19341       else if (need_r11)
19342         frame_reg_rtx = copy_reg;
19343       else if (info->cr_save_p
19344                || info->lr_save_p
19345                || info->first_fp_reg_save < 64
19346                || info->first_gp_reg_save < 32
19347                || info->altivec_size != 0
19348                || info->vrsave_mask != 0
19349                || crtl->calls_eh_return)
19350         {
19351           copy_reg = frame_ptr_rtx;
19352           frame_reg_rtx = copy_reg;
19353         }
19354       else
19355         {
19356           /* The prologue won't be saving any regs so there is no need
19357              to set up a frame register to access any frame save area.
19358              We also won't be using sp_offset anywhere below, but set
19359              the correct value anyway to protect against future
19360              changes to this function.  */
19361           sp_offset = info->total_size;
19362         }
19363       rs6000_emit_allocate_stack (info->total_size, copy_reg);
19364       if (frame_reg_rtx != sp_reg_rtx)
19365         rs6000_emit_stack_tie ();
19366     }
19367
19368   /* Handle world saves specially here.  */
19369   if (WORLD_SAVE_P (info))
19370     {
19371       int i, j, sz;
19372       rtx treg;
19373       rtvec p;
19374       rtx reg0;
19375
19376       /* save_world expects lr in r0. */
19377       reg0 = gen_rtx_REG (Pmode, 0);
19378       if (info->lr_save_p)
19379         {
19380           insn = emit_move_insn (reg0,
19381                                  gen_rtx_REG (Pmode, LR_REGNO));
19382           RTX_FRAME_RELATED_P (insn) = 1;
19383         }
19384
19385       /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
19386          assumptions about the offsets of various bits of the stack
19387          frame.  */
19388       gcc_assert (info->gp_save_offset == -220
19389                   && info->fp_save_offset == -144
19390                   && info->lr_save_offset == 8
19391                   && info->cr_save_offset == 4
19392                   && info->push_p
19393                   && info->lr_save_p
19394                   && (!crtl->calls_eh_return
19395                        || info->ehrd_offset == -432)
19396                   && info->vrsave_save_offset == -224
19397                   && info->altivec_save_offset == -416);
19398
19399       treg = gen_rtx_REG (SImode, 11);
19400       emit_move_insn (treg, GEN_INT (-info->total_size));
19401
19402       /* SAVE_WORLD takes the caller's LR in R0 and the frame size
19403          in R11.  It also clobbers R12, so beware!  */
19404
19405       /* Preserve CR2 for save_world prologues */
19406       sz = 5;
19407       sz += 32 - info->first_gp_reg_save;
19408       sz += 64 - info->first_fp_reg_save;
19409       sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
19410       p = rtvec_alloc (sz);
19411       j = 0;
19412       RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
19413                                             gen_rtx_REG (SImode,
19414                                                          LR_REGNO));
19415       RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
19416                                         gen_rtx_SYMBOL_REF (Pmode,
19417                                                             "*save_world"));
19418       /* We do floats first so that the instruction pattern matches
19419          properly.  */
19420       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19421         {
19422           rtx reg = gen_rtx_REG (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
19423                                    ? DFmode : SFmode), 
19424                                  info->first_fp_reg_save + i);
19425           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19426                                    GEN_INT (info->fp_save_offset
19427                                             + sp_offset + 8 * i));
19428           rtx mem = gen_frame_mem (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
19429                                      ? DFmode : SFmode), addr);
19430
19431           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19432         }
19433       for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
19434         {
19435           rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
19436           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19437                                    GEN_INT (info->altivec_save_offset
19438                                             + sp_offset + 16 * i));
19439           rtx mem = gen_frame_mem (V4SImode, addr);
19440
19441           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19442         }
19443       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19444         {
19445           rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19446           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19447                                    GEN_INT (info->gp_save_offset
19448                                             + sp_offset + reg_size * i));
19449           rtx mem = gen_frame_mem (reg_mode, addr);
19450
19451           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19452         }
19453
19454       {
19455         /* CR register traditionally saved as CR2.  */
19456         rtx reg = gen_rtx_REG (reg_mode, CR2_REGNO);
19457         rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19458                                  GEN_INT (info->cr_save_offset
19459                                           + sp_offset));
19460         rtx mem = gen_frame_mem (reg_mode, addr);
19461
19462         RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg);
19463       }
19464       /* Explain about use of R0.  */
19465       if (info->lr_save_p)
19466         {
19467           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19468                                    GEN_INT (info->lr_save_offset
19469                                             + sp_offset));
19470           rtx mem = gen_frame_mem (reg_mode, addr);
19471
19472           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, mem, reg0);
19473         }
19474       /* Explain what happens to the stack pointer.  */
19475       {
19476         rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
19477         RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
19478       }
19479
19480       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19481       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19482                             treg, GEN_INT (-info->total_size));
19483       sp_offset = info->total_size;
19484     }
19485
19486   /* If we use the link register, get it into r0.  */
19487   if (!WORLD_SAVE_P (info) && info->lr_save_p)
19488     {
19489       rtx addr, reg, mem;
19490
19491       insn = emit_move_insn (gen_rtx_REG (Pmode, 0),
19492                              gen_rtx_REG (Pmode, LR_REGNO));
19493       RTX_FRAME_RELATED_P (insn) = 1;
19494
19495       if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
19496                         | SAVE_NOINLINE_FPRS_SAVES_LR)))
19497         {
19498           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19499                                GEN_INT (info->lr_save_offset + sp_offset));
19500           reg = gen_rtx_REG (Pmode, 0);
19501           mem = gen_rtx_MEM (Pmode, addr);
19502           /* This should not be of rs6000_sr_alias_set, because of
19503              __builtin_return_address.  */
19504
19505           insn = emit_move_insn (mem, reg);
19506           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19507                                 NULL_RTX, NULL_RTX);
19508         }
19509     }
19510
19511   /* If we need to save CR, put it into r12 or r11.  */
19512   if (!WORLD_SAVE_P (info) && info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
19513     {
19514       rtx set;
19515
19516       cr_save_rtx
19517         = gen_rtx_REG (SImode, DEFAULT_ABI == ABI_AIX && !saving_GPRs_inline
19518                        ? 11 : 12);
19519       insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19520       RTX_FRAME_RELATED_P (insn) = 1;
19521       /* Now, there's no way that dwarf2out_frame_debug_expr is going
19522          to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
19523          But that's OK.  All we have to do is specify that _one_ condition
19524          code register is saved in this stack slot.  The thrower's epilogue
19525          will then restore all the call-saved registers.
19526          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
19527       set = gen_rtx_SET (VOIDmode, cr_save_rtx,
19528                          gen_rtx_REG (SImode, CR2_REGNO));
19529       add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19530     }
19531
19532   /* Do any required saving of fpr's.  If only one or two to save, do
19533      it ourselves.  Otherwise, call function.  */
19534   if (!WORLD_SAVE_P (info) && saving_FPRs_inline)
19535     {
19536       int i;
19537       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19538         if ((df_regs_ever_live_p (info->first_fp_reg_save+i)
19539              && ! call_used_regs[info->first_fp_reg_save+i]))
19540           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, 
19541                            (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
19542                             ? DFmode : SFmode,
19543                            info->first_fp_reg_save + i,
19544                            info->fp_save_offset + sp_offset + 8 * i,
19545                            info->total_size);
19546     }
19547   else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
19548     {
19549       insn = rs6000_emit_savres_rtx (info, frame_reg_rtx,
19550                                      info->fp_save_offset + sp_offset,
19551                                      DFmode,
19552                                      /*savep=*/true, /*gpr=*/false,
19553                                      /*lr=*/((strategy
19554                                               & SAVE_NOINLINE_FPRS_SAVES_LR)
19555                                              != 0));
19556       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19557                             NULL_RTX, NULL_RTX);
19558     }
19559
19560   /* Save GPRs.  This is done as a PARALLEL if we are using
19561      the store-multiple instructions.  */
19562   if (!WORLD_SAVE_P (info)
19563       && TARGET_SPE_ABI
19564       && info->spe_64bit_regs_used != 0
19565       && info->first_gp_reg_save != 32)
19566     {
19567       int i;
19568       rtx spe_save_area_ptr;
19569       int save_ptr_to_sp;
19570       int ool_adjust = 0;
19571
19572       /* Determine whether we can address all of the registers that need
19573          to be saved with an offset from frame_reg_rtx that fits in
19574          the small const field for SPE memory instructions.  */
19575       int spe_regs_addressable
19576         = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + sp_offset
19577                                 + reg_size * (32 - info->first_gp_reg_save - 1))
19578            && saving_GPRs_inline);
19579       int spe_offset;
19580
19581       if (spe_regs_addressable)
19582         {
19583           spe_save_area_ptr = frame_reg_rtx;
19584           save_ptr_to_sp = info->total_size - sp_offset;
19585           spe_offset = info->spe_gp_save_offset + sp_offset;
19586         }
19587       else
19588         {
19589           /* Make r11 point to the start of the SPE save area.  We need
19590              to be careful here if r11 is holding the static chain.  If
19591              it is, then temporarily save it in r0.  */
19592           int offset;
19593
19594           if (!saving_GPRs_inline)
19595             ool_adjust = 8 * (info->first_gp_reg_save
19596                               - (FIRST_SAVRES_REGISTER + 1));
19597           offset = info->spe_gp_save_offset + sp_offset - ool_adjust;
19598           spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
19599           save_ptr_to_sp = info->total_size - sp_offset + offset;
19600           spe_offset = 0;
19601
19602           if (using_static_chain_p)
19603             {
19604               rtx r0 = gen_rtx_REG (Pmode, 0);
19605               gcc_assert (info->first_gp_reg_save > 11);
19606
19607               emit_move_insn (r0, spe_save_area_ptr);
19608             }
19609           emit_insn (gen_addsi3 (spe_save_area_ptr,
19610                                  frame_reg_rtx, GEN_INT (offset)));
19611           if (REGNO (frame_reg_rtx) == 11)
19612             sp_offset = -info->spe_gp_save_offset + ool_adjust;
19613         }
19614
19615       if (saving_GPRs_inline)
19616         {
19617           for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19618             if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19619               {
19620                 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19621                 rtx offset, addr, mem;
19622
19623                 /* We're doing all this to ensure that the offset fits into
19624                    the immediate offset of 'evstdd'.  */
19625                 gcc_assert (SPE_CONST_OFFSET_OK (reg_size * i + spe_offset));
19626
19627                 offset = GEN_INT (reg_size * i + spe_offset);
19628                 addr = gen_rtx_PLUS (Pmode, spe_save_area_ptr, offset);
19629                 mem = gen_rtx_MEM (V2SImode, addr);
19630
19631                 insn = emit_move_insn (mem, reg);
19632
19633                 rs6000_frame_related (insn,
19634                                       spe_save_area_ptr, save_ptr_to_sp,
19635                                       NULL_RTX, NULL_RTX);
19636               }
19637         }
19638       else
19639         {
19640           insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
19641                                          ool_adjust, reg_mode,
19642                                          /*savep=*/true, /*gpr=*/true,
19643                                          /*lr=*/false);
19644
19645           rs6000_frame_related (insn, spe_save_area_ptr, save_ptr_to_sp,
19646                                 NULL_RTX, NULL_RTX);
19647         }
19648
19649       /* Move the static chain pointer back.  */
19650       if (using_static_chain_p && !spe_regs_addressable)
19651         emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
19652     }
19653   else if (!WORLD_SAVE_P (info) && !saving_GPRs_inline)
19654     {
19655       if (DEFAULT_ABI == ABI_DARWIN)
19656         {
19657           rtx dest_reg = gen_rtx_REG (Pmode, 11);
19658           if (info->first_fp_reg_save == 64)
19659             {
19660               /* we only need a copy, no fprs were saved.  */
19661               if (dest_reg != frame_reg_rtx)
19662                 emit_move_insn (dest_reg, frame_reg_rtx);
19663             }
19664           else
19665             {
19666               int save_off = 8 * (64 - info->first_fp_reg_save);
19667               rtx offset = GEN_INT (sp_offset - save_off);
19668
19669               if (REGNO (dest_reg) == REGNO (frame_reg_rtx))
19670                 sp_offset = save_off;
19671               emit_insn (gen_add3_insn (dest_reg, frame_reg_rtx, offset));
19672             }
19673         }
19674       /* Need to adjust r11 (r12) if we saved any FPRs.  */
19675       else if (info->first_fp_reg_save != 64)
19676         {
19677           rtx dest_reg = gen_rtx_REG (Pmode, DEFAULT_ABI == ABI_AIX ? 12 : 11);
19678           int save_off = 8 * (64 - info->first_fp_reg_save);
19679           rtx offset = GEN_INT (sp_offset - save_off);
19680
19681           if (REGNO (dest_reg) == REGNO (frame_reg_rtx))
19682             sp_offset = save_off;
19683           emit_insn (gen_add3_insn (dest_reg, frame_reg_rtx, offset));
19684         }
19685
19686       insn = rs6000_emit_savres_rtx (info, frame_reg_rtx,
19687                                      info->gp_save_offset + sp_offset,
19688                                      reg_mode,
19689                                      /*savep=*/true, /*gpr=*/true,
19690                                      /*lr=*/((strategy
19691                                               & SAVE_NOINLINE_GPRS_SAVES_LR)
19692                                              != 0));
19693       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19694                             NULL_RTX, NULL_RTX);
19695     }
19696   else if (!WORLD_SAVE_P (info) && using_store_multiple)
19697     {
19698       rtvec p;
19699       int i;
19700       p = rtvec_alloc (32 - info->first_gp_reg_save);
19701       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19702         {
19703           rtx addr, reg, mem;
19704           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19705           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19706                                GEN_INT (info->gp_save_offset
19707                                         + sp_offset
19708                                         + reg_size * i));
19709           mem = gen_frame_mem (reg_mode, addr);
19710
19711           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
19712         }
19713       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19714       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19715                             NULL_RTX, NULL_RTX);
19716     }
19717   else if (!WORLD_SAVE_P (info))
19718     {
19719       int i;
19720       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19721         if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19722           {
19723             rtx addr, reg, mem;
19724             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
19725
19726             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19727                                  GEN_INT (info->gp_save_offset
19728                                           + sp_offset
19729                                           + reg_size * i));
19730             mem = gen_frame_mem (reg_mode, addr);
19731
19732             insn = emit_move_insn (mem, reg);
19733             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19734                                   NULL_RTX, NULL_RTX);
19735           }
19736     }
19737
19738   /* ??? There's no need to emit actual instructions here, but it's the
19739      easiest way to get the frame unwind information emitted.  */
19740   if (crtl->calls_eh_return)
19741     {
19742       unsigned int i, regno;
19743
19744       for (i = 0; ; ++i)
19745         {
19746           regno = EH_RETURN_DATA_REGNO (i);
19747           if (regno == INVALID_REGNUM)
19748             break;
19749
19750           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
19751                            info->ehrd_offset + sp_offset
19752                            + reg_size * (int) i,
19753                            info->total_size);
19754         }
19755     }
19756
19757   /* In AIX ABI we need to make sure r2 is really saved.  */
19758   if (TARGET_AIX && crtl->calls_eh_return)
19759     {
19760       rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
19761       rtx save_insn, join_insn, note;
19762       long toc_restore_insn;
19763
19764       gcc_assert (frame_reg_rtx == frame_ptr_rtx
19765                   || frame_reg_rtx == sp_reg_rtx);
19766       tmp_reg = gen_rtx_REG (Pmode, 11);
19767       tmp_reg_si = gen_rtx_REG (SImode, 11);
19768       if (using_static_chain_p)
19769         emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
19770       gcc_assert (saving_GPRs_inline && saving_FPRs_inline);
19771       emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
19772       /* Peek at instruction to which this function returns.  If it's
19773          restoring r2, then we know we've already saved r2.  We can't
19774          unconditionally save r2 because the value we have will already
19775          be updated if we arrived at this function via a plt call or
19776          toc adjusting stub.  */
19777       emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
19778       toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
19779       hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
19780       emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
19781       compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
19782       validate_condition_mode (EQ, CCUNSmode);
19783       lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
19784       emit_insn (gen_rtx_SET (VOIDmode, compare_result,
19785                               gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
19786       toc_save_done = gen_label_rtx ();
19787       jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
19788                                    gen_rtx_EQ (VOIDmode, compare_result,
19789                                                const0_rtx),
19790                                    gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
19791                                    pc_rtx);
19792       jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
19793       JUMP_LABEL (jump) = toc_save_done;
19794       LABEL_NUSES (toc_save_done) += 1;
19795
19796       save_insn = emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode,
19797                                    TOC_REGNUM, sp_offset + 5 * reg_size,
19798                                    info->total_size);
19799
19800       emit_label (toc_save_done);
19801
19802       /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
19803          have a CFG that has different saves along different paths.
19804          Move the note to a dummy blockage insn, which describes that
19805          R2 is unconditionally saved after the label.  */
19806       /* ??? An alternate representation might be a special insn pattern
19807          containing both the branch and the store.  That might let the
19808          code that minimizes the number of DW_CFA_advance opcodes better
19809          freedom in placing the annotations.  */
19810       note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
19811       gcc_assert (note);
19812       remove_note (save_insn, note);
19813       RTX_FRAME_RELATED_P (save_insn) = 0;
19814
19815       join_insn = emit_insn (gen_blockage ());
19816       REG_NOTES (join_insn) = note;
19817       RTX_FRAME_RELATED_P (join_insn) = 1;
19818
19819       if (using_static_chain_p)
19820         emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
19821     }
19822
19823   /* Save CR if we use any that must be preserved.  */
19824   if (!WORLD_SAVE_P (info) && info->cr_save_p)
19825     {
19826       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19827                                GEN_INT (info->cr_save_offset + sp_offset));
19828       rtx mem = gen_frame_mem (SImode, addr);
19829       /* See the large comment above about why CR2_REGNO is used.  */
19830       rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
19831
19832       /* If r12 was used to hold the original sp, copy cr into r0 now
19833          that it's free.  */
19834       if (REGNO (frame_reg_rtx) == 12)
19835         {
19836           rtx set;
19837
19838           cr_save_rtx = gen_rtx_REG (SImode, 0);
19839           insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19840           RTX_FRAME_RELATED_P (insn) = 1;
19841           set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
19842           add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19843         }
19844       insn = emit_move_insn (mem, cr_save_rtx);
19845
19846       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19847                             NULL_RTX, NULL_RTX);
19848     }
19849
19850   /* Update stack and set back pointer unless this is V.4,
19851      for which it was done previously.  */
19852   if (!WORLD_SAVE_P (info) && info->push_p
19853       && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
19854     {
19855       rtx copy_reg = NULL;
19856
19857       if (info->total_size < 32767)
19858         sp_offset = info->total_size;
19859       else if (info->altivec_size != 0
19860                || info->vrsave_mask != 0)
19861         {
19862           copy_reg = frame_ptr_rtx;
19863           frame_reg_rtx = copy_reg;
19864         }
19865       else
19866         sp_offset = info->total_size;
19867       rs6000_emit_allocate_stack (info->total_size, copy_reg);
19868       if (frame_reg_rtx != sp_reg_rtx)
19869         rs6000_emit_stack_tie ();
19870     }
19871
19872   /* Set frame pointer, if needed.  */
19873   if (frame_pointer_needed)
19874     {
19875       insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
19876                              sp_reg_rtx);
19877       RTX_FRAME_RELATED_P (insn) = 1;
19878     }
19879
19880   /* Save AltiVec registers if needed.  Save here because the red zone does
19881      not include AltiVec registers.  */
19882   if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI && info->altivec_size != 0)
19883     {
19884       int i;
19885
19886       /* There should be a non inline version of this, for when we
19887          are saving lots of vector registers.  */
19888       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
19889         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
19890           {
19891             rtx areg, savereg, mem;
19892             int offset;
19893
19894             offset = info->altivec_save_offset + sp_offset
19895               + 16 * (i - info->first_altivec_reg_save);
19896
19897             savereg = gen_rtx_REG (V4SImode, i);
19898
19899             areg = gen_rtx_REG (Pmode, 0);
19900             emit_move_insn (areg, GEN_INT (offset));
19901
19902             /* AltiVec addressing mode is [reg+reg].  */
19903             mem = gen_frame_mem (V4SImode,
19904                                  gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
19905
19906             insn = emit_move_insn (mem, savereg);
19907
19908             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
19909                                   areg, GEN_INT (offset));
19910           }
19911     }
19912
19913   /* VRSAVE is a bit vector representing which AltiVec registers
19914      are used.  The OS uses this to determine which vector
19915      registers to save on a context switch.  We need to save
19916      VRSAVE on the stack frame, add whatever AltiVec registers we
19917      used in this function, and do the corresponding magic in the
19918      epilogue.  */
19919
19920   if (!WORLD_SAVE_P (info)
19921       && TARGET_ALTIVEC
19922       && TARGET_ALTIVEC_VRSAVE
19923       && info->vrsave_mask != 0)
19924     {
19925       rtx reg, mem, vrsave;
19926       int offset;
19927
19928       /* Get VRSAVE onto a GPR.  Note that ABI_V4 might be using r12
19929          as frame_reg_rtx and r11 as the static chain pointer for
19930          nested functions.  */
19931       reg = gen_rtx_REG (SImode, 0);
19932       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
19933       if (TARGET_MACHO)
19934         emit_insn (gen_get_vrsave_internal (reg));
19935       else
19936         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
19937
19938       /* Save VRSAVE.  */
19939       offset = info->vrsave_save_offset + sp_offset;
19940       mem = gen_frame_mem (SImode,
19941                            gen_rtx_PLUS (Pmode, frame_reg_rtx, 
19942                                          GEN_INT (offset)));
19943       insn = emit_move_insn (mem, reg);
19944
19945       /* Include the registers in the mask.  */
19946       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
19947
19948       insn = emit_insn (generate_set_vrsave (reg, info, 0));
19949     }
19950
19951   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
19952   if (!TARGET_SINGLE_PIC_BASE
19953       && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
19954           || (DEFAULT_ABI == ABI_V4
19955               && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
19956               && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
19957     {
19958       /* If emit_load_toc_table will use the link register, we need to save
19959          it.  We use R12 for this purpose because emit_load_toc_table
19960          can use register 0.  This allows us to use a plain 'blr' to return
19961          from the procedure more often.  */
19962       int save_LR_around_toc_setup = (TARGET_ELF
19963                                       && DEFAULT_ABI != ABI_AIX
19964                                       && flag_pic
19965                                       && ! info->lr_save_p
19966                                       && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
19967       if (save_LR_around_toc_setup)
19968         {
19969           rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
19970
19971           insn = emit_move_insn (frame_ptr_rtx, lr);
19972           RTX_FRAME_RELATED_P (insn) = 1;
19973
19974           rs6000_emit_load_toc_table (TRUE);
19975
19976           insn = emit_move_insn (lr, frame_ptr_rtx);
19977           add_reg_note (insn, REG_CFA_RESTORE, lr);
19978           RTX_FRAME_RELATED_P (insn) = 1;
19979         }
19980       else
19981         rs6000_emit_load_toc_table (TRUE);
19982     }
19983
19984 #if TARGET_MACHO
19985   if (!TARGET_SINGLE_PIC_BASE
19986       && DEFAULT_ABI == ABI_DARWIN
19987       && flag_pic && crtl->uses_pic_offset_table)
19988     {
19989       rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
19990       rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
19991
19992       /* Save and restore LR locally around this call (in R0).  */
19993       if (!info->lr_save_p)
19994         emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
19995
19996       emit_insn (gen_load_macho_picbase (src));
19997
19998       emit_move_insn (gen_rtx_REG (Pmode,
19999                                    RS6000_PIC_OFFSET_TABLE_REGNUM),
20000                       lr);
20001
20002       if (!info->lr_save_p)
20003         emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
20004     }
20005 #endif
20006
20007   /* If we need to, save the TOC register after doing the stack setup.
20008      Do not emit eh frame info for this save.  The unwinder wants info,
20009      conceptually attached to instructions in this function, about
20010      register values in the caller of this function.  This R2 may have
20011      already been changed from the value in the caller.
20012      We don't attempt to write accurate DWARF EH frame info for R2
20013      because code emitted by gcc for a (non-pointer) function call
20014      doesn't save and restore R2.  Instead, R2 is managed out-of-line
20015      by a linker generated plt call stub when the function resides in
20016      a shared library.  This behaviour is costly to describe in DWARF,
20017      both in terms of the size of DWARF info and the time taken in the
20018      unwinder to interpret it.  R2 changes, apart from the
20019      calls_eh_return case earlier in this function, are handled by
20020      linux-unwind.h frob_update_context.  */ 
20021   if (rs6000_save_toc_in_prologue_p ())
20022     {
20023       rtx addr = gen_rtx_PLUS (Pmode, sp_reg_rtx, GEN_INT (5 * reg_size));
20024       rtx mem = gen_frame_mem (reg_mode, addr);
20025       emit_move_insn (mem, gen_rtx_REG (reg_mode, TOC_REGNUM));
20026     }
20027 }
20028
20029 /* Write function prologue.  */
20030
20031 static void
20032 rs6000_output_function_prologue (FILE *file,
20033                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20034 {
20035   rs6000_stack_t *info = rs6000_stack_info ();
20036
20037   if (TARGET_DEBUG_STACK)
20038     debug_stack_info (info);
20039
20040   /* Write .extern for any function we will call to save and restore
20041      fp values.  */
20042   if (info->first_fp_reg_save < 64
20043       && !TARGET_MACHO)
20044     {
20045       char *name;
20046       int regno = info->first_fp_reg_save - 32;
20047
20048       if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
20049         {
20050           name = rs6000_savres_routine_name (info, regno, /*savep=*/true,
20051                                              /*gpr=*/false, /*lr=*/false);
20052           fprintf (file, "\t.extern %s\n", name);
20053         }
20054       if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
20055         {
20056           name = rs6000_savres_routine_name (info, regno, /*savep=*/false,
20057                                              /*gpr=*/false, /*lr=*/true);
20058           fprintf (file, "\t.extern %s\n", name);
20059         }
20060     }
20061
20062   /* Write .extern for AIX common mode routines, if needed.  */
20063   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
20064     {
20065       fputs ("\t.extern __mulh\n", file);
20066       fputs ("\t.extern __mull\n", file);
20067       fputs ("\t.extern __divss\n", file);
20068       fputs ("\t.extern __divus\n", file);
20069       fputs ("\t.extern __quoss\n", file);
20070       fputs ("\t.extern __quous\n", file);
20071       common_mode_defined = 1;
20072     }
20073
20074   rs6000_pic_labelno++;
20075 }
20076
20077 /* Non-zero if vmx regs are restored before the frame pop, zero if
20078    we restore after the pop when possible.  */
20079 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
20080
20081 /* Reload CR from REG.  */
20082
20083 static void
20084 rs6000_restore_saved_cr (rtx reg, int using_mfcr_multiple)
20085 {
20086   int count = 0;
20087   int i;
20088
20089   if (using_mfcr_multiple)
20090     {
20091       for (i = 0; i < 8; i++)
20092         if (df_regs_ever_live_p (CR0_REGNO+i) && ! call_used_regs[CR0_REGNO+i])
20093           count++;
20094       gcc_assert (count);
20095     }
20096
20097   if (using_mfcr_multiple && count > 1)
20098     {
20099       rtvec p;
20100       int ndx;
20101
20102       p = rtvec_alloc (count);
20103
20104       ndx = 0;
20105       for (i = 0; i < 8; i++)
20106         if (df_regs_ever_live_p (CR0_REGNO+i) && ! call_used_regs[CR0_REGNO+i])
20107           {
20108             rtvec r = rtvec_alloc (2);
20109             RTVEC_ELT (r, 0) = reg;
20110             RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
20111             RTVEC_ELT (p, ndx) =
20112               gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i),
20113                            gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
20114             ndx++;
20115           }
20116       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20117       gcc_assert (ndx == count);
20118     }
20119   else
20120     for (i = 0; i < 8; i++)
20121       if (df_regs_ever_live_p (CR0_REGNO+i) && ! call_used_regs[CR0_REGNO+i])
20122         {
20123           emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode,
20124                                                        CR0_REGNO+i),
20125                                           reg));
20126         }
20127 }
20128
20129 /* Return true if OFFSET from stack pointer can be clobbered by signals.
20130    V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
20131    below stack pointer not cloberred by signals.  */
20132
20133 static inline bool
20134 offset_below_red_zone_p (HOST_WIDE_INT offset)
20135 {
20136   return offset < (DEFAULT_ABI == ABI_V4
20137                    ? 0
20138                    : TARGET_32BIT ? -220 : -288);
20139 }
20140
20141 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn.  */
20142
20143 static void
20144 emit_cfa_restores (rtx cfa_restores)
20145 {
20146   rtx insn = get_last_insn ();
20147   rtx *loc = &REG_NOTES (insn);
20148
20149   while (*loc)
20150     loc = &XEXP (*loc, 1);
20151   *loc = cfa_restores;
20152   RTX_FRAME_RELATED_P (insn) = 1;
20153 }
20154
20155 /* Emit function epilogue as insns.  */
20156
20157 void
20158 rs6000_emit_epilogue (int sibcall)
20159 {
20160   rs6000_stack_t *info;
20161   int restoring_GPRs_inline;
20162   int restoring_FPRs_inline;
20163   int using_load_multiple;
20164   int using_mtcr_multiple;
20165   int use_backchain_to_restore_sp;
20166   int restore_lr;
20167   int strategy;
20168   int sp_offset = 0;
20169   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
20170   rtx frame_reg_rtx = sp_reg_rtx;
20171   rtx cfa_restores = NULL_RTX;
20172   rtx insn;
20173   rtx cr_save_reg = NULL_RTX;
20174   enum machine_mode reg_mode = Pmode;
20175   int reg_size = TARGET_32BIT ? 4 : 8;
20176   int i;
20177
20178   info = rs6000_stack_info ();
20179
20180   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
20181     {
20182       reg_mode = V2SImode;
20183       reg_size = 8;
20184     }
20185
20186   strategy = info->savres_strategy;
20187   using_load_multiple = strategy & SAVRES_MULTIPLE;
20188   restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
20189   restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
20190   using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
20191                          || rs6000_cpu == PROCESSOR_PPC603
20192                          || rs6000_cpu == PROCESSOR_PPC750
20193                          || optimize_size);
20194   /* Restore via the backchain when we have a large frame, since this
20195      is more efficient than an addis, addi pair.  The second condition
20196      here will not trigger at the moment;  We don't actually need a
20197      frame pointer for alloca, but the generic parts of the compiler
20198      give us one anyway.  */
20199   use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
20200                                  || (cfun->calls_alloca
20201                                      && !frame_pointer_needed));
20202   restore_lr = (info->lr_save_p
20203                 && (restoring_FPRs_inline
20204                     || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
20205                 && (restoring_GPRs_inline
20206                     || info->first_fp_reg_save < 64));
20207
20208   if (WORLD_SAVE_P (info))
20209     {
20210       int i, j;
20211       char rname[30];
20212       const char *alloc_rname;
20213       rtvec p;
20214
20215       /* eh_rest_world_r10 will return to the location saved in the LR
20216          stack slot (which is not likely to be our caller.)
20217          Input: R10 -- stack adjustment.  Clobbers R0, R11, R12, R7, R8.
20218          rest_world is similar, except any R10 parameter is ignored.
20219          The exception-handling stuff that was here in 2.95 is no
20220          longer necessary.  */
20221
20222       p = rtvec_alloc (9
20223                        + 1
20224                        + 32 - info->first_gp_reg_save
20225                        + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
20226                        + 63 + 1 - info->first_fp_reg_save);
20227
20228       strcpy (rname, ((crtl->calls_eh_return) ?
20229                       "*eh_rest_world_r10" : "*rest_world"));
20230       alloc_rname = ggc_strdup (rname);
20231
20232       j = 0;
20233       RTVEC_ELT (p, j++) = ret_rtx;
20234       RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
20235                                         gen_rtx_REG (Pmode,
20236                                                      LR_REGNO));
20237       RTVEC_ELT (p, j++)
20238         = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
20239       /* The instruction pattern requires a clobber here;
20240          it is shared with the restVEC helper. */
20241       RTVEC_ELT (p, j++)
20242         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
20243
20244       {
20245         /* CR register traditionally saved as CR2.  */
20246         rtx reg = gen_rtx_REG (reg_mode, CR2_REGNO);
20247         rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20248                                  GEN_INT (info->cr_save_offset));
20249         rtx mem = gen_frame_mem (reg_mode, addr);
20250
20251         RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20252
20253         if (flag_shrink_wrap)
20254           {
20255             cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20256                                            gen_rtx_REG (Pmode, LR_REGNO),
20257                                            cfa_restores);
20258             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20259           }
20260       }
20261
20262       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20263         {
20264           rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20265           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20266                                    GEN_INT (info->gp_save_offset
20267                                             + reg_size * i));
20268           rtx mem = gen_frame_mem (reg_mode, addr);
20269
20270           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20271           if (flag_shrink_wrap)
20272             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20273         }
20274       for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
20275         {
20276           rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
20277           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20278                                    GEN_INT (info->altivec_save_offset
20279                                             + 16 * i));
20280           rtx mem = gen_frame_mem (V4SImode, addr);
20281
20282           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20283           if (flag_shrink_wrap)
20284             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20285         }
20286       for (i = 0; info->first_fp_reg_save + i <= 63; i++)
20287         {
20288           rtx reg = gen_rtx_REG (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20289                                    ? DFmode : SFmode), 
20290                                  info->first_fp_reg_save + i);
20291           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20292                                    GEN_INT (info->fp_save_offset
20293                                             + 8 * i));
20294           rtx mem = gen_frame_mem (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20295                                      ? DFmode : SFmode), addr);
20296
20297           RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, reg, mem);
20298           if (flag_shrink_wrap)
20299             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20300         }
20301       RTVEC_ELT (p, j++)
20302         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
20303       RTVEC_ELT (p, j++)
20304         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
20305       RTVEC_ELT (p, j++)
20306         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
20307       RTVEC_ELT (p, j++)
20308         = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
20309       RTVEC_ELT (p, j++)
20310         = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
20311       insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20312
20313       if (flag_shrink_wrap)
20314         {
20315           REG_NOTES (insn) = cfa_restores;
20316           add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20317           RTX_FRAME_RELATED_P (insn) = 1;
20318         }
20319       return;
20320     }
20321
20322   /* frame_reg_rtx + sp_offset points to the top of this stack frame.  */
20323   if (info->push_p)
20324     sp_offset = info->total_size;
20325
20326   /* Restore AltiVec registers if we must do so before adjusting the
20327      stack.  */
20328   if (TARGET_ALTIVEC_ABI
20329       && info->altivec_size != 0
20330       && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20331           || (DEFAULT_ABI != ABI_V4
20332               && offset_below_red_zone_p (info->altivec_save_offset))))
20333     {
20334       int i;
20335
20336       if (use_backchain_to_restore_sp)
20337         {
20338           frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20339           emit_move_insn (frame_reg_rtx,
20340                           gen_rtx_MEM (Pmode, sp_reg_rtx));
20341           sp_offset = 0;
20342         }
20343       else if (frame_pointer_needed)
20344         frame_reg_rtx = hard_frame_pointer_rtx;
20345
20346       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20347         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20348           {
20349             rtx addr, areg, mem, reg;
20350
20351             areg = gen_rtx_REG (Pmode, 0);
20352             emit_move_insn
20353               (areg, GEN_INT (info->altivec_save_offset
20354                               + sp_offset
20355                               + 16 * (i - info->first_altivec_reg_save)));
20356
20357             /* AltiVec addressing mode is [reg+reg].  */
20358             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20359             mem = gen_frame_mem (V4SImode, addr);
20360
20361             reg = gen_rtx_REG (V4SImode, i);
20362             emit_move_insn (reg, mem);
20363             if (flag_shrink_wrap
20364                 || offset_below_red_zone_p (info->altivec_save_offset
20365                                             + (i - info->first_altivec_reg_save)
20366                                             * 16))
20367               cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20368                                              cfa_restores);
20369           }
20370     }
20371
20372   /* Restore VRSAVE if we must do so before adjusting the stack.  */
20373   if (TARGET_ALTIVEC
20374       && TARGET_ALTIVEC_VRSAVE
20375       && info->vrsave_mask != 0
20376       && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20377           || (DEFAULT_ABI != ABI_V4
20378               && offset_below_red_zone_p (info->vrsave_save_offset))))
20379     {
20380       rtx addr, mem, reg;
20381
20382       if (frame_reg_rtx == sp_reg_rtx)
20383         {
20384           if (use_backchain_to_restore_sp)
20385             {
20386               frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20387               emit_move_insn (frame_reg_rtx,
20388                               gen_rtx_MEM (Pmode, sp_reg_rtx));
20389               sp_offset = 0;
20390             }
20391           else if (frame_pointer_needed)
20392             frame_reg_rtx = hard_frame_pointer_rtx;
20393         }
20394
20395       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20396                            GEN_INT (info->vrsave_save_offset + sp_offset));
20397       mem = gen_frame_mem (SImode, addr);
20398       reg = gen_rtx_REG (SImode, 12);
20399       emit_move_insn (reg, mem);
20400
20401       emit_insn (generate_set_vrsave (reg, info, 1));
20402     }
20403
20404   insn = NULL_RTX;
20405   /* If we have a large stack frame, restore the old stack pointer
20406      using the backchain.  */
20407   if (use_backchain_to_restore_sp)
20408     {
20409       if (frame_reg_rtx == sp_reg_rtx)
20410         {
20411           /* Under V.4, don't reset the stack pointer until after we're done
20412              loading the saved registers.  */
20413           if (DEFAULT_ABI == ABI_V4)
20414             frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20415
20416           insn = emit_move_insn (frame_reg_rtx,
20417                                  gen_rtx_MEM (Pmode, sp_reg_rtx));
20418           sp_offset = 0;
20419         }
20420       else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20421                && DEFAULT_ABI == ABI_V4)
20422         /* frame_reg_rtx has been set up by the altivec restore.  */
20423         ;
20424       else
20425         {
20426           insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
20427           frame_reg_rtx = sp_reg_rtx;
20428         }
20429     }
20430   /* If we have a frame pointer, we can restore the old stack pointer
20431      from it.  */
20432   else if (frame_pointer_needed)
20433     {
20434       frame_reg_rtx = sp_reg_rtx;
20435       if (DEFAULT_ABI == ABI_V4)
20436         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20437       /* Prevent reordering memory accesses against stack pointer restore.  */
20438       else if (cfun->calls_alloca
20439                || offset_below_red_zone_p (-info->total_size))
20440         {
20441           rtx mem1 = gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx);
20442           rtx mem2 = gen_rtx_MEM (BLKmode, sp_reg_rtx);
20443           MEM_NOTRAP_P (mem1) = 1;
20444           MEM_NOTRAP_P (mem2) = 1;
20445           emit_insn (gen_frame_tie (mem1, mem2));
20446         }
20447
20448       insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
20449                                        GEN_INT (info->total_size)));
20450       sp_offset = 0;
20451     }
20452   else if (info->push_p
20453            && DEFAULT_ABI != ABI_V4
20454            && !crtl->calls_eh_return)
20455     {
20456       /* Prevent reordering memory accesses against stack pointer restore.  */
20457       if (cfun->calls_alloca
20458           || offset_below_red_zone_p (-info->total_size))
20459         {
20460           rtx mem = gen_rtx_MEM (BLKmode, sp_reg_rtx);
20461           MEM_NOTRAP_P (mem) = 1;
20462           emit_insn (gen_stack_tie (mem));
20463         }
20464       insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
20465                                        GEN_INT (info->total_size)));
20466       sp_offset = 0;
20467     }
20468   if (insn && frame_reg_rtx == sp_reg_rtx)
20469     {
20470       if (cfa_restores)
20471         {
20472           REG_NOTES (insn) = cfa_restores;
20473           cfa_restores = NULL_RTX;
20474         }
20475       add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20476       RTX_FRAME_RELATED_P (insn) = 1;
20477     }
20478
20479   /* Restore AltiVec registers if we have not done so already.  */
20480   if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20481       && TARGET_ALTIVEC_ABI
20482       && info->altivec_size != 0
20483       && (DEFAULT_ABI == ABI_V4
20484           || !offset_below_red_zone_p (info->altivec_save_offset)))
20485     {
20486       int i;
20487
20488       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20489         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20490           {
20491             rtx addr, areg, mem, reg;
20492
20493             areg = gen_rtx_REG (Pmode, 0);
20494             emit_move_insn
20495               (areg, GEN_INT (info->altivec_save_offset
20496                               + sp_offset
20497                               + 16 * (i - info->first_altivec_reg_save)));
20498
20499             /* AltiVec addressing mode is [reg+reg].  */
20500             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20501             mem = gen_frame_mem (V4SImode, addr);
20502
20503             reg = gen_rtx_REG (V4SImode, i);
20504             emit_move_insn (reg, mem);
20505             if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
20506               cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20507                                              cfa_restores);
20508           }
20509     }
20510
20511   /* Restore VRSAVE if we have not done so already.  */
20512   if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20513       && TARGET_ALTIVEC
20514       && TARGET_ALTIVEC_VRSAVE
20515       && info->vrsave_mask != 0
20516       && (DEFAULT_ABI == ABI_V4
20517           || !offset_below_red_zone_p (info->vrsave_save_offset)))
20518     {
20519       rtx addr, mem, reg;
20520
20521       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20522                            GEN_INT (info->vrsave_save_offset + sp_offset));
20523       mem = gen_frame_mem (SImode, addr);
20524       reg = gen_rtx_REG (SImode, 12);
20525       emit_move_insn (reg, mem);
20526
20527       emit_insn (generate_set_vrsave (reg, info, 1));
20528     }
20529
20530   /* Get the old lr if we saved it.  If we are restoring registers
20531      out-of-line, then the out-of-line routines can do this for us.  */
20532   if (restore_lr && restoring_GPRs_inline)
20533     {
20534       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
20535                                       info->lr_save_offset + sp_offset);
20536
20537       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
20538     }
20539
20540   /* Get the old cr if we saved it.  */
20541   if (info->cr_save_p)
20542     {
20543       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20544                                GEN_INT (info->cr_save_offset + sp_offset));
20545       rtx mem = gen_frame_mem (SImode, addr);
20546
20547       cr_save_reg = gen_rtx_REG (SImode,
20548                                  DEFAULT_ABI == ABI_AIX
20549                                  && !restoring_GPRs_inline
20550                                  && info->first_fp_reg_save < 64
20551                                  ? 11 : 12);
20552       emit_move_insn (cr_save_reg, mem);
20553     }
20554
20555   /* Set LR here to try to overlap restores below.  */
20556   if (restore_lr && restoring_GPRs_inline)
20557     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO),
20558                     gen_rtx_REG (Pmode, 0));
20559
20560   /* Load exception handler data registers, if needed.  */
20561   if (crtl->calls_eh_return)
20562     {
20563       unsigned int i, regno;
20564
20565       if (TARGET_AIX)
20566         {
20567           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20568                                    GEN_INT (sp_offset + 5 * reg_size));
20569           rtx mem = gen_frame_mem (reg_mode, addr);
20570
20571           emit_move_insn (gen_rtx_REG (reg_mode, 2), mem);
20572         }
20573
20574       for (i = 0; ; ++i)
20575         {
20576           rtx mem;
20577
20578           regno = EH_RETURN_DATA_REGNO (i);
20579           if (regno == INVALID_REGNUM)
20580             break;
20581
20582           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
20583                                       info->ehrd_offset + sp_offset
20584                                       + reg_size * (int) i);
20585
20586           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
20587         }
20588     }
20589
20590   /* Restore GPRs.  This is done as a PARALLEL if we are using
20591      the load-multiple instructions.  */
20592   if (TARGET_SPE_ABI
20593       && info->spe_64bit_regs_used
20594       && info->first_gp_reg_save != 32)
20595     {
20596       /* Determine whether we can address all of the registers that need
20597          to be saved with an offset from frame_reg_rtx that fits in
20598          the small const field for SPE memory instructions.  */
20599       int spe_regs_addressable
20600         = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + sp_offset
20601                                 + reg_size * (32 - info->first_gp_reg_save - 1))
20602            && restoring_GPRs_inline);
20603       int spe_offset;
20604       int ool_adjust = 0;
20605
20606       if (spe_regs_addressable)
20607         spe_offset = info->spe_gp_save_offset + sp_offset;
20608       else
20609         {
20610           rtx old_frame_reg_rtx = frame_reg_rtx;
20611           /* Make r11 point to the start of the SPE save area.  We worried about
20612              not clobbering it when we were saving registers in the prologue.
20613              There's no need to worry here because the static chain is passed
20614              anew to every function.  */
20615
20616           if (!restoring_GPRs_inline)
20617             ool_adjust = 8 * (info->first_gp_reg_save
20618                               - (FIRST_SAVRES_REGISTER + 1));
20619           frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20620           emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
20621                                  GEN_INT (info->spe_gp_save_offset
20622                                           + sp_offset
20623                                           - ool_adjust)));
20624           /* Keep the invariant that frame_reg_rtx + sp_offset points
20625              at the top of the stack frame.  */
20626           sp_offset = -info->spe_gp_save_offset + ool_adjust;
20627
20628           spe_offset = 0;
20629         }
20630
20631       if (restoring_GPRs_inline)
20632         {
20633           for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20634             if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20635               {
20636                 rtx offset, addr, mem, reg;
20637
20638                 /* We're doing all this to ensure that the immediate offset
20639                    fits into the immediate field of 'evldd'.  */
20640                 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
20641
20642                 offset = GEN_INT (spe_offset + reg_size * i);
20643                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
20644                 mem = gen_rtx_MEM (V2SImode, addr);
20645                 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20646
20647                 emit_move_insn (reg, mem);
20648               }
20649         }
20650       else
20651         rs6000_emit_savres_rtx (info, frame_reg_rtx,
20652                                 ool_adjust, reg_mode,
20653                                 /*savep=*/false, /*gpr=*/true,
20654                                 /*lr=*/true);
20655     }
20656   else if (!restoring_GPRs_inline)
20657     {
20658       /* We are jumping to an out-of-line function.  */
20659       bool can_use_exit = info->first_fp_reg_save == 64;
20660
20661       /* Emit stack reset code if we need it.  */
20662       if (can_use_exit)
20663         {
20664           rs6000_emit_stack_reset (info, sp_reg_rtx, frame_reg_rtx,
20665                                    sp_offset, can_use_exit);
20666           if (DEFAULT_ABI == ABI_DARWIN)
20667             /* we only need a copy, no fprs were saved.  */
20668             emit_move_insn (gen_rtx_REG (Pmode, 11), frame_reg_rtx);
20669
20670           if (info->cr_save_p)
20671             rs6000_restore_saved_cr (cr_save_reg, using_mtcr_multiple);
20672         }
20673       else
20674         {
20675           rtx src_reg = gen_rtx_REG (Pmode, DEFAULT_ABI == ABI_AIX ? 12 : 11);
20676
20677           emit_insn (gen_add3_insn (src_reg, frame_reg_rtx,
20678                                     GEN_INT (sp_offset - info->fp_size)));
20679           if (REGNO (frame_reg_rtx) == REGNO (src_reg))
20680             sp_offset = info->fp_size;
20681         }
20682
20683       rs6000_emit_savres_rtx (info, frame_reg_rtx,
20684                               info->gp_save_offset, reg_mode,
20685                               /*savep=*/false, /*gpr=*/true,
20686                               /*lr=*/can_use_exit);
20687     }
20688   else if (using_load_multiple)
20689     {
20690       rtvec p;
20691       p = rtvec_alloc (32 - info->first_gp_reg_save);
20692       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20693         {
20694           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20695                                    GEN_INT (info->gp_save_offset
20696                                             + sp_offset
20697                                             + reg_size * i));
20698           rtx mem = gen_frame_mem (reg_mode, addr);
20699           rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20700
20701           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, reg, mem);
20702         }
20703       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20704     }
20705   else
20706     {
20707       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20708         if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20709           {
20710             rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20711                                      GEN_INT (info->gp_save_offset
20712                                               + sp_offset
20713                                               + reg_size * i));
20714             rtx mem = gen_frame_mem (reg_mode, addr);
20715             rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20716
20717             emit_move_insn (reg, mem);
20718           }
20719     }
20720
20721   if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
20722     {
20723       /* If the frame pointer was used then we can't delay emitting
20724          a REG_CFA_DEF_CFA note.  This must happen on the insn that
20725          restores the frame pointer, r31.  We may have already emitted
20726          a REG_CFA_DEF_CFA note, but that's OK;  A duplicate is
20727          discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
20728          be harmless if emitted.  */
20729       if (frame_pointer_needed)
20730         {
20731           insn = get_last_insn ();
20732           add_reg_note (insn, REG_CFA_DEF_CFA,
20733                         plus_constant (frame_reg_rtx, sp_offset));
20734           RTX_FRAME_RELATED_P (insn) = 1;
20735         }
20736
20737       /* Set up cfa_restores.  We always need these when
20738          shrink-wrapping.  If not shrink-wrapping then we only need
20739          the cfa_restore when the stack location is no longer valid.
20740          The cfa_restores must be emitted on or before the insn that
20741          invalidates the stack, and of course must not be emitted
20742          before the insn that actually does the restore.  The latter
20743          is why the LR cfa_restore condition below is a little
20744          complicated.  It's also why it is a bad idea to emit the
20745          cfa_restores as a group on the last instruction here that
20746          actually does a restore: That insn may be reordered with
20747          respect to others doing restores.  */
20748       if (info->cr_save_p)
20749         cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20750                                        gen_rtx_REG (SImode, CR2_REGNO),
20751                                        cfa_restores);
20752       if (flag_shrink_wrap
20753           && (restore_lr
20754               || (info->lr_save_p
20755                   && !restoring_GPRs_inline
20756                   && info->first_fp_reg_save == 64)))
20757         cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20758                                        gen_rtx_REG (Pmode, LR_REGNO),
20759                                        cfa_restores);
20760
20761       for (i = info->first_gp_reg_save; i < 32; i++)
20762         if (!restoring_GPRs_inline
20763             || using_load_multiple
20764             || rs6000_reg_live_or_pic_offset_p (i))
20765           {
20766             rtx reg = gen_rtx_REG (reg_mode, i);
20767
20768             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20769           }
20770     }
20771
20772   if (!restoring_GPRs_inline
20773       && info->first_fp_reg_save == 64)
20774     {
20775       /* We are jumping to an out-of-line function.  */
20776       if (cfa_restores)
20777         emit_cfa_restores (cfa_restores);
20778       return;
20779     }
20780
20781   if (restore_lr && !restoring_GPRs_inline)
20782     {
20783       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
20784                                      info->lr_save_offset + sp_offset);
20785
20786       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
20787       emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO),
20788                       gen_rtx_REG (Pmode, 0));
20789     }
20790
20791   /* Restore fpr's if we need to do it without calling a function.  */
20792   if (restoring_FPRs_inline)
20793     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
20794       if ((df_regs_ever_live_p (info->first_fp_reg_save + i)
20795            && !call_used_regs[info->first_fp_reg_save + i]))
20796         {
20797           rtx addr, mem, reg;
20798           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20799                                GEN_INT (info->fp_save_offset
20800                                         + sp_offset
20801                                         + 8 * i));
20802           mem = gen_frame_mem (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20803                                  ? DFmode : SFmode), addr);
20804           reg = gen_rtx_REG (((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
20805                                ? DFmode : SFmode),
20806                              info->first_fp_reg_save + i);
20807
20808           emit_move_insn (reg, mem);
20809           if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
20810             cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20811         }
20812
20813   /* If we saved cr, restore it here.  Just those that were used.  */
20814   if (info->cr_save_p)
20815     rs6000_restore_saved_cr (cr_save_reg, using_mtcr_multiple);
20816
20817   /* If this is V.4, unwind the stack pointer after all of the loads
20818      have been done.  */
20819   insn = rs6000_emit_stack_reset (info, sp_reg_rtx, frame_reg_rtx,
20820                                   sp_offset, !restoring_FPRs_inline);
20821   if (insn)
20822     {
20823       if (cfa_restores)
20824         {
20825           REG_NOTES (insn) = cfa_restores;
20826           cfa_restores = NULL_RTX;
20827         }
20828       add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20829       RTX_FRAME_RELATED_P (insn) = 1;
20830     }
20831
20832   if (crtl->calls_eh_return)
20833     {
20834       rtx sa = EH_RETURN_STACKADJ_RTX;
20835       emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
20836     }
20837
20838   if (!sibcall)
20839     {
20840       rtvec p;
20841       bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
20842       if (! restoring_FPRs_inline)
20843         {
20844           p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
20845           RTVEC_ELT (p, 0) = ret_rtx;
20846         }
20847       else
20848         {
20849           if (cfa_restores)
20850             {
20851               /* We can't hang the cfa_restores off a simple return,
20852                  since the shrink-wrap code sometimes uses an existing
20853                  return.  This means there might be a path from
20854                  pre-prologue code to this return, and dwarf2cfi code
20855                  wants the eh_frame unwinder state to be the same on
20856                  all paths to any point.  So we need to emit the
20857                  cfa_restores before the return.  For -m64 we really
20858                  don't need epilogue cfa_restores at all, except for
20859                  this irritating dwarf2cfi with shrink-wrap
20860                  requirement;  The stack red-zone means eh_frame info
20861                  from the prologue telling the unwinder to restore
20862                  from the stack is perfectly good right to the end of
20863                  the function.  */
20864               emit_insn (gen_blockage ());
20865               emit_cfa_restores (cfa_restores);
20866               cfa_restores = NULL_RTX;
20867             }
20868           p = rtvec_alloc (2);
20869           RTVEC_ELT (p, 0) = simple_return_rtx;
20870         }
20871
20872       RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
20873                           ? gen_rtx_USE (VOIDmode,
20874                                          gen_rtx_REG (Pmode, LR_REGNO))
20875                           : gen_rtx_CLOBBER (VOIDmode,
20876                                              gen_rtx_REG (Pmode, LR_REGNO)));
20877
20878       /* If we have to restore more than two FP registers, branch to the
20879          restore function.  It will return to our caller.  */
20880       if (! restoring_FPRs_inline)
20881         {
20882           int i;
20883           rtx sym;
20884
20885           if ((DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
20886               && lr)
20887             cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20888                                            gen_rtx_REG (Pmode, LR_REGNO),
20889                                            cfa_restores);
20890
20891           sym = rs6000_savres_routine_sym (info,
20892                                            /*savep=*/false,
20893                                            /*gpr=*/false,
20894                                            /*lr=*/lr);
20895           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
20896           RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
20897                                           gen_rtx_REG (Pmode,
20898                                                        DEFAULT_ABI == ABI_AIX
20899                                                        ? 1 : 11));
20900           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
20901             {
20902               rtx addr, mem, reg;
20903
20904               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
20905                                    GEN_INT (info->fp_save_offset + 8 * i));
20906               mem = gen_frame_mem (DFmode, addr);
20907               reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
20908
20909               RTVEC_ELT (p, i + 4) = gen_rtx_SET (VOIDmode, reg, mem);
20910               if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
20911                 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
20912                                                cfa_restores);
20913             }
20914         }
20915
20916       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20917     }
20918
20919   if (cfa_restores)
20920     {
20921       if (sibcall)
20922         /* Ensure the cfa_restores are hung off an insn that won't
20923            be reordered above other restores.  */
20924         emit_insn (gen_blockage ());
20925
20926       emit_cfa_restores (cfa_restores);
20927     }
20928 }
20929
20930 /* Write function epilogue.  */
20931
20932 static void
20933 rs6000_output_function_epilogue (FILE *file,
20934                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20935 {
20936 #if TARGET_MACHO
20937   macho_branch_islands ();
20938   /* Mach-O doesn't support labels at the end of objects, so if
20939      it looks like we might want one, insert a NOP.  */
20940   {
20941     rtx insn = get_last_insn ();
20942     rtx deleted_debug_label = NULL_RTX;
20943     while (insn
20944            && NOTE_P (insn)
20945            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
20946       {
20947         /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
20948            notes only, instead set their CODE_LABEL_NUMBER to -1,
20949            otherwise there would be code generation differences
20950            in between -g and -g0.  */
20951         if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
20952           deleted_debug_label = insn;
20953         insn = PREV_INSN (insn);
20954       }
20955     if (insn
20956         && (LABEL_P (insn)
20957             || (NOTE_P (insn)
20958                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
20959       fputs ("\tnop\n", file);
20960     else if (deleted_debug_label)
20961       for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
20962         if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
20963           CODE_LABEL_NUMBER (insn) = -1;
20964   }
20965 #endif
20966
20967   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
20968      on its format.
20969
20970      We don't output a traceback table if -finhibit-size-directive was
20971      used.  The documentation for -finhibit-size-directive reads
20972      ``don't output a @code{.size} assembler directive, or anything
20973      else that would cause trouble if the function is split in the
20974      middle, and the two halves are placed at locations far apart in
20975      memory.''  The traceback table has this property, since it
20976      includes the offset from the start of the function to the
20977      traceback table itself.
20978
20979      System V.4 Powerpc's (and the embedded ABI derived from it) use a
20980      different traceback table.  */
20981   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
20982       && rs6000_traceback != traceback_none && !cfun->is_thunk)
20983     {
20984       const char *fname = NULL;
20985       const char *language_string = lang_hooks.name;
20986       int fixed_parms = 0, float_parms = 0, parm_info = 0;
20987       int i;
20988       int optional_tbtab;
20989       rs6000_stack_t *info = rs6000_stack_info ();
20990
20991       if (rs6000_traceback == traceback_full)
20992         optional_tbtab = 1;
20993       else if (rs6000_traceback == traceback_part)
20994         optional_tbtab = 0;
20995       else
20996         optional_tbtab = !optimize_size && !TARGET_ELF;
20997
20998       if (optional_tbtab)
20999         {
21000           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
21001           while (*fname == '.') /* V.4 encodes . in the name */
21002             fname++;
21003
21004           /* Need label immediately before tbtab, so we can compute
21005              its offset from the function start.  */
21006           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21007           ASM_OUTPUT_LABEL (file, fname);
21008         }
21009
21010       /* The .tbtab pseudo-op can only be used for the first eight
21011          expressions, since it can't handle the possibly variable
21012          length fields that follow.  However, if you omit the optional
21013          fields, the assembler outputs zeros for all optional fields
21014          anyways, giving each variable length field is minimum length
21015          (as defined in sys/debug.h).  Thus we can not use the .tbtab
21016          pseudo-op at all.  */
21017
21018       /* An all-zero word flags the start of the tbtab, for debuggers
21019          that have to find it by searching forward from the entry
21020          point or from the current pc.  */
21021       fputs ("\t.long 0\n", file);
21022
21023       /* Tbtab format type.  Use format type 0.  */
21024       fputs ("\t.byte 0,", file);
21025
21026       /* Language type.  Unfortunately, there does not seem to be any
21027          official way to discover the language being compiled, so we
21028          use language_string.
21029          C is 0.  Fortran is 1.  Pascal is 2.  Ada is 3.  C++ is 9.
21030          Java is 13.  Objective-C is 14.  Objective-C++ isn't assigned
21031          a number, so for now use 9.  LTO and Go aren't assigned numbers
21032          either, so for now use 0.  */
21033       if (! strcmp (language_string, "GNU C")
21034           || ! strcmp (language_string, "GNU GIMPLE")
21035           || ! strcmp (language_string, "GNU Go"))
21036         i = 0;
21037       else if (! strcmp (language_string, "GNU F77")
21038                || ! strcmp (language_string, "GNU Fortran"))
21039         i = 1;
21040       else if (! strcmp (language_string, "GNU Pascal"))
21041         i = 2;
21042       else if (! strcmp (language_string, "GNU Ada"))
21043         i = 3;
21044       else if (! strcmp (language_string, "GNU C++")
21045                || ! strcmp (language_string, "GNU Objective-C++"))
21046         i = 9;
21047       else if (! strcmp (language_string, "GNU Java"))
21048         i = 13;
21049       else if (! strcmp (language_string, "GNU Objective-C"))
21050         i = 14;
21051       else
21052         gcc_unreachable ();
21053       fprintf (file, "%d,", i);
21054
21055       /* 8 single bit fields: global linkage (not set for C extern linkage,
21056          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
21057          from start of procedure stored in tbtab, internal function, function
21058          has controlled storage, function has no toc, function uses fp,
21059          function logs/aborts fp operations.  */
21060       /* Assume that fp operations are used if any fp reg must be saved.  */
21061       fprintf (file, "%d,",
21062                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
21063
21064       /* 6 bitfields: function is interrupt handler, name present in
21065          proc table, function calls alloca, on condition directives
21066          (controls stack walks, 3 bits), saves condition reg, saves
21067          link reg.  */
21068       /* The `function calls alloca' bit seems to be set whenever reg 31 is
21069          set up as a frame pointer, even when there is no alloca call.  */
21070       fprintf (file, "%d,",
21071                ((optional_tbtab << 6)
21072                 | ((optional_tbtab & frame_pointer_needed) << 5)
21073                 | (info->cr_save_p << 1)
21074                 | (info->lr_save_p)));
21075
21076       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
21077          (6 bits).  */
21078       fprintf (file, "%d,",
21079                (info->push_p << 7) | (64 - info->first_fp_reg_save));
21080
21081       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
21082       fprintf (file, "%d,", (32 - first_reg_to_save ()));
21083
21084       if (optional_tbtab)
21085         {
21086           /* Compute the parameter info from the function decl argument
21087              list.  */
21088           tree decl;
21089           int next_parm_info_bit = 31;
21090
21091           for (decl = DECL_ARGUMENTS (current_function_decl);
21092                decl; decl = DECL_CHAIN (decl))
21093             {
21094               rtx parameter = DECL_INCOMING_RTL (decl);
21095               enum machine_mode mode = GET_MODE (parameter);
21096
21097               if (GET_CODE (parameter) == REG)
21098                 {
21099                   if (SCALAR_FLOAT_MODE_P (mode))
21100                     {
21101                       int bits;
21102
21103                       float_parms++;
21104
21105                       switch (mode)
21106                         {
21107                         case SFmode:
21108                         case SDmode:
21109                           bits = 0x2;
21110                           break;
21111
21112                         case DFmode:
21113                         case DDmode:
21114                         case TFmode:
21115                         case TDmode:
21116                           bits = 0x3;
21117                           break;
21118
21119                         default:
21120                           gcc_unreachable ();
21121                         }
21122
21123                       /* If only one bit will fit, don't or in this entry.  */
21124                       if (next_parm_info_bit > 0)
21125                         parm_info |= (bits << (next_parm_info_bit - 1));
21126                       next_parm_info_bit -= 2;
21127                     }
21128                   else
21129                     {
21130                       fixed_parms += ((GET_MODE_SIZE (mode)
21131                                        + (UNITS_PER_WORD - 1))
21132                                       / UNITS_PER_WORD);
21133                       next_parm_info_bit -= 1;
21134                     }
21135                 }
21136             }
21137         }
21138
21139       /* Number of fixed point parameters.  */
21140       /* This is actually the number of words of fixed point parameters; thus
21141          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
21142       fprintf (file, "%d,", fixed_parms);
21143
21144       /* 2 bitfields: number of floating point parameters (7 bits), parameters
21145          all on stack.  */
21146       /* This is actually the number of fp registers that hold parameters;
21147          and thus the maximum value is 13.  */
21148       /* Set parameters on stack bit if parameters are not in their original
21149          registers, regardless of whether they are on the stack?  Xlc
21150          seems to set the bit when not optimizing.  */
21151       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
21152
21153       if (! optional_tbtab)
21154         return;
21155
21156       /* Optional fields follow.  Some are variable length.  */
21157
21158       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
21159          11 double float.  */
21160       /* There is an entry for each parameter in a register, in the order that
21161          they occur in the parameter list.  Any intervening arguments on the
21162          stack are ignored.  If the list overflows a long (max possible length
21163          34 bits) then completely leave off all elements that don't fit.  */
21164       /* Only emit this long if there was at least one parameter.  */
21165       if (fixed_parms || float_parms)
21166         fprintf (file, "\t.long %d\n", parm_info);
21167
21168       /* Offset from start of code to tb table.  */
21169       fputs ("\t.long ", file);
21170       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21171       RS6000_OUTPUT_BASENAME (file, fname);
21172       putc ('-', file);
21173       rs6000_output_function_entry (file, fname);
21174       putc ('\n', file);
21175
21176       /* Interrupt handler mask.  */
21177       /* Omit this long, since we never set the interrupt handler bit
21178          above.  */
21179
21180       /* Number of CTL (controlled storage) anchors.  */
21181       /* Omit this long, since the has_ctl bit is never set above.  */
21182
21183       /* Displacement into stack of each CTL anchor.  */
21184       /* Omit this list of longs, because there are no CTL anchors.  */
21185
21186       /* Length of function name.  */
21187       if (*fname == '*')
21188         ++fname;
21189       fprintf (file, "\t.short %d\n", (int) strlen (fname));
21190
21191       /* Function name.  */
21192       assemble_string (fname, strlen (fname));
21193
21194       /* Register for alloca automatic storage; this is always reg 31.
21195          Only emit this if the alloca bit was set above.  */
21196       if (frame_pointer_needed)
21197         fputs ("\t.byte 31\n", file);
21198
21199       fputs ("\t.align 2\n", file);
21200     }
21201 }
21202 \f
21203 /* A C compound statement that outputs the assembler code for a thunk
21204    function, used to implement C++ virtual function calls with
21205    multiple inheritance.  The thunk acts as a wrapper around a virtual
21206    function, adjusting the implicit object parameter before handing
21207    control off to the real function.
21208
21209    First, emit code to add the integer DELTA to the location that
21210    contains the incoming first argument.  Assume that this argument
21211    contains a pointer, and is the one used to pass the `this' pointer
21212    in C++.  This is the incoming argument *before* the function
21213    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
21214    values of all other incoming arguments.
21215
21216    After the addition, emit code to jump to FUNCTION, which is a
21217    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
21218    not touch the return address.  Hence returning from FUNCTION will
21219    return to whoever called the current `thunk'.
21220
21221    The effect must be as if FUNCTION had been called directly with the
21222    adjusted first argument.  This macro is responsible for emitting
21223    all of the code for a thunk function; output_function_prologue()
21224    and output_function_epilogue() are not invoked.
21225
21226    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
21227    been extracted from it.)  It might possibly be useful on some
21228    targets, but probably not.
21229
21230    If you do not define this macro, the target-independent code in the
21231    C++ frontend will generate a less efficient heavyweight thunk that
21232    calls FUNCTION instead of jumping to it.  The generic approach does
21233    not support varargs.  */
21234
21235 static void
21236 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
21237                         HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
21238                         tree function)
21239 {
21240   rtx this_rtx, insn, funexp;
21241
21242   reload_completed = 1;
21243   epilogue_completed = 1;
21244
21245   /* Mark the end of the (empty) prologue.  */
21246   emit_note (NOTE_INSN_PROLOGUE_END);
21247
21248   /* Find the "this" pointer.  If the function returns a structure,
21249      the structure return pointer is in r3.  */
21250   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
21251     this_rtx = gen_rtx_REG (Pmode, 4);
21252   else
21253     this_rtx = gen_rtx_REG (Pmode, 3);
21254
21255   /* Apply the constant offset, if required.  */
21256   if (delta)
21257     emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
21258
21259   /* Apply the offset from the vtable, if required.  */
21260   if (vcall_offset)
21261     {
21262       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
21263       rtx tmp = gen_rtx_REG (Pmode, 12);
21264
21265       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
21266       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
21267         {
21268           emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
21269           emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
21270         }
21271       else
21272         {
21273           rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
21274
21275           emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
21276         }
21277       emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
21278     }
21279
21280   /* Generate a tail call to the target function.  */
21281   if (!TREE_USED (function))
21282     {
21283       assemble_external (function);
21284       TREE_USED (function) = 1;
21285     }
21286   funexp = XEXP (DECL_RTL (function), 0);
21287   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
21288
21289 #if TARGET_MACHO
21290   if (MACHOPIC_INDIRECT)
21291     funexp = machopic_indirect_call_target (funexp);
21292 #endif
21293
21294   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
21295      generate sibcall RTL explicitly.  */
21296   insn = emit_call_insn (
21297            gen_rtx_PARALLEL (VOIDmode,
21298              gen_rtvec (4,
21299                         gen_rtx_CALL (VOIDmode,
21300                                       funexp, const0_rtx),
21301                         gen_rtx_USE (VOIDmode, const0_rtx),
21302                         gen_rtx_USE (VOIDmode,
21303                                      gen_rtx_REG (SImode,
21304                                                   LR_REGNO)),
21305                         simple_return_rtx)));
21306   SIBLING_CALL_P (insn) = 1;
21307   emit_barrier ();
21308
21309   /* Run just enough of rest_of_compilation to get the insns emitted.
21310      There's not really enough bulk here to make other passes such as
21311      instruction scheduling worth while.  Note that use_thunk calls
21312      assemble_start_function and assemble_end_function.  */
21313   insn = get_insns ();
21314   insn_locators_alloc ();
21315   shorten_branches (insn);
21316   final_start_function (insn, file, 1);
21317   final (insn, file, 1);
21318   final_end_function ();
21319
21320   reload_completed = 0;
21321   epilogue_completed = 0;
21322 }
21323 \f
21324 /* A quick summary of the various types of 'constant-pool tables'
21325    under PowerPC:
21326
21327    Target       Flags           Name            One table per
21328    AIX          (none)          AIX TOC         object file
21329    AIX          -mfull-toc      AIX TOC         object file
21330    AIX          -mminimal-toc   AIX minimal TOC translation unit
21331    SVR4/EABI    (none)          SVR4 SDATA      object file
21332    SVR4/EABI    -fpic           SVR4 pic        object file
21333    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
21334    SVR4/EABI    -mrelocatable   EABI TOC        function
21335    SVR4/EABI    -maix           AIX TOC         object file
21336    SVR4/EABI    -maix -mminimal-toc
21337                                 AIX minimal TOC translation unit
21338
21339    Name                 Reg.    Set by  entries       contains:
21340                                         made by  addrs? fp?     sum?
21341
21342    AIX TOC              2       crt0    as       Y      option  option
21343    AIX minimal TOC      30      prolog  gcc      Y      Y       option
21344    SVR4 SDATA           13      crt0    gcc      N      Y       N
21345    SVR4 pic             30      prolog  ld       Y      not yet N
21346    SVR4 PIC             30      prolog  gcc      Y      option  option
21347    EABI TOC             30      prolog  gcc      Y      option  option
21348
21349 */
21350
21351 /* Hash functions for the hash table.  */
21352
21353 static unsigned
21354 rs6000_hash_constant (rtx k)
21355 {
21356   enum rtx_code code = GET_CODE (k);
21357   enum machine_mode mode = GET_MODE (k);
21358   unsigned result = (code << 3) ^ mode;
21359   const char *format;
21360   int flen, fidx;
21361
21362   format = GET_RTX_FORMAT (code);
21363   flen = strlen (format);
21364   fidx = 0;
21365
21366   switch (code)
21367     {
21368     case LABEL_REF:
21369       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
21370
21371     case CONST_DOUBLE:
21372       if (mode != VOIDmode)
21373         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
21374       flen = 2;
21375       break;
21376
21377     case CODE_LABEL:
21378       fidx = 3;
21379       break;
21380
21381     default:
21382       break;
21383     }
21384
21385   for (; fidx < flen; fidx++)
21386     switch (format[fidx])
21387       {
21388       case 's':
21389         {
21390           unsigned i, len;
21391           const char *str = XSTR (k, fidx);
21392           len = strlen (str);
21393           result = result * 613 + len;
21394           for (i = 0; i < len; i++)
21395             result = result * 613 + (unsigned) str[i];
21396           break;
21397         }
21398       case 'u':
21399       case 'e':
21400         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
21401         break;
21402       case 'i':
21403       case 'n':
21404         result = result * 613 + (unsigned) XINT (k, fidx);
21405         break;
21406       case 'w':
21407         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
21408           result = result * 613 + (unsigned) XWINT (k, fidx);
21409         else
21410           {
21411             size_t i;
21412             for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
21413               result = result * 613 + (unsigned) (XWINT (k, fidx)
21414                                                   >> CHAR_BIT * i);
21415           }
21416         break;
21417       case '0':
21418         break;
21419       default:
21420         gcc_unreachable ();
21421       }
21422
21423   return result;
21424 }
21425
21426 static unsigned
21427 toc_hash_function (const void *hash_entry)
21428 {
21429   const struct toc_hash_struct *thc =
21430     (const struct toc_hash_struct *) hash_entry;
21431   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
21432 }
21433
21434 /* Compare H1 and H2 for equivalence.  */
21435
21436 static int
21437 toc_hash_eq (const void *h1, const void *h2)
21438 {
21439   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
21440   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
21441
21442   if (((const struct toc_hash_struct *) h1)->key_mode
21443       != ((const struct toc_hash_struct *) h2)->key_mode)
21444     return 0;
21445
21446   return rtx_equal_p (r1, r2);
21447 }
21448
21449 /* These are the names given by the C++ front-end to vtables, and
21450    vtable-like objects.  Ideally, this logic should not be here;
21451    instead, there should be some programmatic way of inquiring as
21452    to whether or not an object is a vtable.  */
21453
21454 #define VTABLE_NAME_P(NAME)                             \
21455   (strncmp ("_vt.", name, strlen ("_vt.")) == 0         \
21456   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
21457   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
21458   || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0       \
21459   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
21460
21461 #ifdef NO_DOLLAR_IN_LABEL
21462 /* Return a GGC-allocated character string translating dollar signs in
21463    input NAME to underscores.  Used by XCOFF ASM_OUTPUT_LABELREF.  */
21464
21465 const char *
21466 rs6000_xcoff_strip_dollar (const char *name)
21467 {
21468   char *strip, *p;
21469   const char *q;
21470   size_t len;
21471
21472   q = (const char *) strchr (name, '$');
21473
21474   if (q == 0 || q == name)
21475     return name;
21476
21477   len = strlen (name);
21478   strip = XALLOCAVEC (char, len + 1);
21479   strcpy (strip, name);
21480   p = strip + (q - name);
21481   while (p)
21482     {
21483       *p = '_';
21484       p = strchr (p + 1, '$');
21485     }
21486
21487   return ggc_alloc_string (strip, len);
21488 }
21489 #endif
21490
21491 void
21492 rs6000_output_symbol_ref (FILE *file, rtx x)
21493 {
21494   /* Currently C++ toc references to vtables can be emitted before it
21495      is decided whether the vtable is public or private.  If this is
21496      the case, then the linker will eventually complain that there is
21497      a reference to an unknown section.  Thus, for vtables only,
21498      we emit the TOC reference to reference the symbol and not the
21499      section.  */
21500   const char *name = XSTR (x, 0);
21501
21502   if (VTABLE_NAME_P (name))
21503     {
21504       RS6000_OUTPUT_BASENAME (file, name);
21505     }
21506   else
21507     assemble_name (file, name);
21508 }
21509
21510 /* Output a TOC entry.  We derive the entry name from what is being
21511    written.  */
21512
21513 void
21514 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
21515 {
21516   char buf[256];
21517   const char *name = buf;
21518   rtx base = x;
21519   HOST_WIDE_INT offset = 0;
21520
21521   gcc_assert (!TARGET_NO_TOC);
21522
21523   /* When the linker won't eliminate them, don't output duplicate
21524      TOC entries (this happens on AIX if there is any kind of TOC,
21525      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
21526      CODE_LABELs.  */
21527   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
21528     {
21529       struct toc_hash_struct *h;
21530       void * * found;
21531
21532       /* Create toc_hash_table.  This can't be done at TARGET_OPTION_OVERRIDE
21533          time because GGC is not initialized at that point.  */
21534       if (toc_hash_table == NULL)
21535         toc_hash_table = htab_create_ggc (1021, toc_hash_function,
21536                                           toc_hash_eq, NULL);
21537
21538       h = ggc_alloc_toc_hash_struct ();
21539       h->key = x;
21540       h->key_mode = mode;
21541       h->labelno = labelno;
21542
21543       found = htab_find_slot (toc_hash_table, h, INSERT);
21544       if (*found == NULL)
21545         *found = h;
21546       else  /* This is indeed a duplicate.
21547                Set this label equal to that label.  */
21548         {
21549           fputs ("\t.set ", file);
21550           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21551           fprintf (file, "%d,", labelno);
21552           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21553           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
21554                                               found)->labelno));
21555           return;
21556         }
21557     }
21558
21559   /* If we're going to put a double constant in the TOC, make sure it's
21560      aligned properly when strict alignment is on.  */
21561   if (GET_CODE (x) == CONST_DOUBLE
21562       && STRICT_ALIGNMENT
21563       && GET_MODE_BITSIZE (mode) >= 64
21564       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
21565     ASM_OUTPUT_ALIGN (file, 3);
21566   }
21567
21568   (*targetm.asm_out.internal_label) (file, "LC", labelno);
21569
21570   /* Handle FP constants specially.  Note that if we have a minimal
21571      TOC, things we put here aren't actually in the TOC, so we can allow
21572      FP constants.  */
21573   if (GET_CODE (x) == CONST_DOUBLE &&
21574       (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
21575     {
21576       REAL_VALUE_TYPE rv;
21577       long k[4];
21578
21579       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21580       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21581         REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
21582       else
21583         REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
21584
21585       if (TARGET_64BIT)
21586         {
21587           if (TARGET_MINIMAL_TOC)
21588             fputs (DOUBLE_INT_ASM_OP, file);
21589           else
21590             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21591                      k[0] & 0xffffffff, k[1] & 0xffffffff,
21592                      k[2] & 0xffffffff, k[3] & 0xffffffff);
21593           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
21594                    k[0] & 0xffffffff, k[1] & 0xffffffff,
21595                    k[2] & 0xffffffff, k[3] & 0xffffffff);
21596           return;
21597         }
21598       else
21599         {
21600           if (TARGET_MINIMAL_TOC)
21601             fputs ("\t.long ", file);
21602           else
21603             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21604                      k[0] & 0xffffffff, k[1] & 0xffffffff,
21605                      k[2] & 0xffffffff, k[3] & 0xffffffff);
21606           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
21607                    k[0] & 0xffffffff, k[1] & 0xffffffff,
21608                    k[2] & 0xffffffff, k[3] & 0xffffffff);
21609           return;
21610         }
21611     }
21612   else if (GET_CODE (x) == CONST_DOUBLE &&
21613            (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
21614     {
21615       REAL_VALUE_TYPE rv;
21616       long k[2];
21617
21618       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21619
21620       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21621         REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
21622       else
21623         REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
21624
21625       if (TARGET_64BIT)
21626         {
21627           if (TARGET_MINIMAL_TOC)
21628             fputs (DOUBLE_INT_ASM_OP, file);
21629           else
21630             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
21631                      k[0] & 0xffffffff, k[1] & 0xffffffff);
21632           fprintf (file, "0x%lx%08lx\n",
21633                    k[0] & 0xffffffff, k[1] & 0xffffffff);
21634           return;
21635         }
21636       else
21637         {
21638           if (TARGET_MINIMAL_TOC)
21639             fputs ("\t.long ", file);
21640           else
21641             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
21642                      k[0] & 0xffffffff, k[1] & 0xffffffff);
21643           fprintf (file, "0x%lx,0x%lx\n",
21644                    k[0] & 0xffffffff, k[1] & 0xffffffff);
21645           return;
21646         }
21647     }
21648   else if (GET_CODE (x) == CONST_DOUBLE &&
21649            (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
21650     {
21651       REAL_VALUE_TYPE rv;
21652       long l;
21653
21654       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21655       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21656         REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
21657       else
21658         REAL_VALUE_TO_TARGET_SINGLE (rv, l);
21659
21660       if (TARGET_64BIT)
21661         {
21662           if (TARGET_MINIMAL_TOC)
21663             fputs (DOUBLE_INT_ASM_OP, file);
21664           else
21665             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
21666           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
21667           return;
21668         }
21669       else
21670         {
21671           if (TARGET_MINIMAL_TOC)
21672             fputs ("\t.long ", file);
21673           else
21674             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
21675           fprintf (file, "0x%lx\n", l & 0xffffffff);
21676           return;
21677         }
21678     }
21679   else if (GET_MODE (x) == VOIDmode
21680            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
21681     {
21682       unsigned HOST_WIDE_INT low;
21683       HOST_WIDE_INT high;
21684
21685       if (GET_CODE (x) == CONST_DOUBLE)
21686         {
21687           low = CONST_DOUBLE_LOW (x);
21688           high = CONST_DOUBLE_HIGH (x);
21689         }
21690       else
21691 #if HOST_BITS_PER_WIDE_INT == 32
21692         {
21693           low = INTVAL (x);
21694           high = (low & 0x80000000) ? ~0 : 0;
21695         }
21696 #else
21697         {
21698           low = INTVAL (x) & 0xffffffff;
21699           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
21700         }
21701 #endif
21702
21703       /* TOC entries are always Pmode-sized, but since this
21704          is a bigendian machine then if we're putting smaller
21705          integer constants in the TOC we have to pad them.
21706          (This is still a win over putting the constants in
21707          a separate constant pool, because then we'd have
21708          to have both a TOC entry _and_ the actual constant.)
21709
21710          For a 32-bit target, CONST_INT values are loaded and shifted
21711          entirely within `low' and can be stored in one TOC entry.  */
21712
21713       /* It would be easy to make this work, but it doesn't now.  */
21714       gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
21715
21716       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
21717         {
21718 #if HOST_BITS_PER_WIDE_INT == 32
21719           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
21720                          POINTER_SIZE, &low, &high, 0);
21721 #else
21722           low |= high << 32;
21723           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
21724           high = (HOST_WIDE_INT) low >> 32;
21725           low &= 0xffffffff;
21726 #endif
21727         }
21728
21729       if (TARGET_64BIT)
21730         {
21731           if (TARGET_MINIMAL_TOC)
21732             fputs (DOUBLE_INT_ASM_OP, file);
21733           else
21734             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
21735                      (long) high & 0xffffffff, (long) low & 0xffffffff);
21736           fprintf (file, "0x%lx%08lx\n",
21737                    (long) high & 0xffffffff, (long) low & 0xffffffff);
21738           return;
21739         }
21740       else
21741         {
21742           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
21743             {
21744               if (TARGET_MINIMAL_TOC)
21745                 fputs ("\t.long ", file);
21746               else
21747                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
21748                          (long) high & 0xffffffff, (long) low & 0xffffffff);
21749               fprintf (file, "0x%lx,0x%lx\n",
21750                        (long) high & 0xffffffff, (long) low & 0xffffffff);
21751             }
21752           else
21753             {
21754               if (TARGET_MINIMAL_TOC)
21755                 fputs ("\t.long ", file);
21756               else
21757                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
21758               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
21759             }
21760           return;
21761         }
21762     }
21763
21764   if (GET_CODE (x) == CONST)
21765     {
21766       gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
21767                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
21768
21769       base = XEXP (XEXP (x, 0), 0);
21770       offset = INTVAL (XEXP (XEXP (x, 0), 1));
21771     }
21772
21773   switch (GET_CODE (base))
21774     {
21775     case SYMBOL_REF:
21776       name = XSTR (base, 0);
21777       break;
21778
21779     case LABEL_REF:
21780       ASM_GENERATE_INTERNAL_LABEL (buf, "L",
21781                                    CODE_LABEL_NUMBER (XEXP (base, 0)));
21782       break;
21783
21784     case CODE_LABEL:
21785       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
21786       break;
21787
21788     default:
21789       gcc_unreachable ();
21790     }
21791
21792   if (TARGET_MINIMAL_TOC)
21793     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
21794   else
21795     {
21796       fputs ("\t.tc ", file);
21797       RS6000_OUTPUT_BASENAME (file, name);
21798
21799       if (offset < 0)
21800         fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
21801       else if (offset)
21802         fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
21803
21804       fputs ("[TC],", file);
21805     }
21806
21807   /* Currently C++ toc references to vtables can be emitted before it
21808      is decided whether the vtable is public or private.  If this is
21809      the case, then the linker will eventually complain that there is
21810      a TOC reference to an unknown section.  Thus, for vtables only,
21811      we emit the TOC reference to reference the symbol and not the
21812      section.  */
21813   if (VTABLE_NAME_P (name))
21814     {
21815       RS6000_OUTPUT_BASENAME (file, name);
21816       if (offset < 0)
21817         fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
21818       else if (offset > 0)
21819         fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
21820     }
21821   else
21822     output_addr_const (file, x);
21823   putc ('\n', file);
21824 }
21825 \f
21826 /* Output an assembler pseudo-op to write an ASCII string of N characters
21827    starting at P to FILE.
21828
21829    On the RS/6000, we have to do this using the .byte operation and
21830    write out special characters outside the quoted string.
21831    Also, the assembler is broken; very long strings are truncated,
21832    so we must artificially break them up early.  */
21833
21834 void
21835 output_ascii (FILE *file, const char *p, int n)
21836 {
21837   char c;
21838   int i, count_string;
21839   const char *for_string = "\t.byte \"";
21840   const char *for_decimal = "\t.byte ";
21841   const char *to_close = NULL;
21842
21843   count_string = 0;
21844   for (i = 0; i < n; i++)
21845     {
21846       c = *p++;
21847       if (c >= ' ' && c < 0177)
21848         {
21849           if (for_string)
21850             fputs (for_string, file);
21851           putc (c, file);
21852
21853           /* Write two quotes to get one.  */
21854           if (c == '"')
21855             {
21856               putc (c, file);
21857               ++count_string;
21858             }
21859
21860           for_string = NULL;
21861           for_decimal = "\"\n\t.byte ";
21862           to_close = "\"\n";
21863           ++count_string;
21864
21865           if (count_string >= 512)
21866             {
21867               fputs (to_close, file);
21868
21869               for_string = "\t.byte \"";
21870               for_decimal = "\t.byte ";
21871               to_close = NULL;
21872               count_string = 0;
21873             }
21874         }
21875       else
21876         {
21877           if (for_decimal)
21878             fputs (for_decimal, file);
21879           fprintf (file, "%d", c);
21880
21881           for_string = "\n\t.byte \"";
21882           for_decimal = ", ";
21883           to_close = "\n";
21884           count_string = 0;
21885         }
21886     }
21887
21888   /* Now close the string if we have written one.  Then end the line.  */
21889   if (to_close)
21890     fputs (to_close, file);
21891 }
21892 \f
21893 /* Generate a unique section name for FILENAME for a section type
21894    represented by SECTION_DESC.  Output goes into BUF.
21895
21896    SECTION_DESC can be any string, as long as it is different for each
21897    possible section type.
21898
21899    We name the section in the same manner as xlc.  The name begins with an
21900    underscore followed by the filename (after stripping any leading directory
21901    names) with the last period replaced by the string SECTION_DESC.  If
21902    FILENAME does not contain a period, SECTION_DESC is appended to the end of
21903    the name.  */
21904
21905 void
21906 rs6000_gen_section_name (char **buf, const char *filename,
21907                          const char *section_desc)
21908 {
21909   const char *q, *after_last_slash, *last_period = 0;
21910   char *p;
21911   int len;
21912
21913   after_last_slash = filename;
21914   for (q = filename; *q; q++)
21915     {
21916       if (*q == '/')
21917         after_last_slash = q + 1;
21918       else if (*q == '.')
21919         last_period = q;
21920     }
21921
21922   len = strlen (after_last_slash) + strlen (section_desc) + 2;
21923   *buf = (char *) xmalloc (len);
21924
21925   p = *buf;
21926   *p++ = '_';
21927
21928   for (q = after_last_slash; *q; q++)
21929     {
21930       if (q == last_period)
21931         {
21932           strcpy (p, section_desc);
21933           p += strlen (section_desc);
21934           break;
21935         }
21936
21937       else if (ISALNUM (*q))
21938         *p++ = *q;
21939     }
21940
21941   if (last_period == 0)
21942     strcpy (p, section_desc);
21943   else
21944     *p = '\0';
21945 }
21946 \f
21947 /* Emit profile function.  */
21948
21949 void
21950 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
21951 {
21952   /* Non-standard profiling for kernels, which just saves LR then calls
21953      _mcount without worrying about arg saves.  The idea is to change
21954      the function prologue as little as possible as it isn't easy to
21955      account for arg save/restore code added just for _mcount.  */
21956   if (TARGET_PROFILE_KERNEL)
21957     return;
21958
21959   if (DEFAULT_ABI == ABI_AIX)
21960     {
21961 #ifndef NO_PROFILE_COUNTERS
21962 # define NO_PROFILE_COUNTERS 0
21963 #endif
21964       if (NO_PROFILE_COUNTERS)
21965         emit_library_call (init_one_libfunc (RS6000_MCOUNT),
21966                            LCT_NORMAL, VOIDmode, 0);
21967       else
21968         {
21969           char buf[30];
21970           const char *label_name;
21971           rtx fun;
21972
21973           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
21974           label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
21975           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
21976
21977           emit_library_call (init_one_libfunc (RS6000_MCOUNT),
21978                              LCT_NORMAL, VOIDmode, 1, fun, Pmode);
21979         }
21980     }
21981   else if (DEFAULT_ABI == ABI_DARWIN)
21982     {
21983       const char *mcount_name = RS6000_MCOUNT;
21984       int caller_addr_regno = LR_REGNO;
21985
21986       /* Be conservative and always set this, at least for now.  */
21987       crtl->uses_pic_offset_table = 1;
21988
21989 #if TARGET_MACHO
21990       /* For PIC code, set up a stub and collect the caller's address
21991          from r0, which is where the prologue puts it.  */
21992       if (MACHOPIC_INDIRECT
21993           && crtl->uses_pic_offset_table)
21994         caller_addr_regno = 0;
21995 #endif
21996       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
21997                          LCT_NORMAL, VOIDmode, 1,
21998                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
21999     }
22000 }
22001
22002 /* Write function profiler code.  */
22003
22004 void
22005 output_function_profiler (FILE *file, int labelno)
22006 {
22007   char buf[100];
22008
22009   switch (DEFAULT_ABI)
22010     {
22011     default:
22012       gcc_unreachable ();
22013
22014     case ABI_V4:
22015       if (!TARGET_32BIT)
22016         {
22017           warning (0, "no profiling of 64-bit code for this ABI");
22018           return;
22019         }
22020       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22021       fprintf (file, "\tmflr %s\n", reg_names[0]);
22022       if (NO_PROFILE_COUNTERS)
22023         {
22024           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22025                        reg_names[0], reg_names[1]);
22026         }
22027       else if (TARGET_SECURE_PLT && flag_pic)
22028         {
22029           if (TARGET_LINK_STACK)
22030             {
22031               char name[32];
22032               get_ppc476_thunk_name (name);
22033               asm_fprintf (file, "\tbl %s\n", name);
22034             }
22035           else
22036             asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
22037           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22038                        reg_names[0], reg_names[1]);
22039           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22040           asm_fprintf (file, "\t{cau|addis} %s,%s,",
22041                        reg_names[12], reg_names[12]);
22042           assemble_name (file, buf);
22043           asm_fprintf (file, "-1b@ha\n\t{cal|la} %s,", reg_names[0]);
22044           assemble_name (file, buf);
22045           asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
22046         }
22047       else if (flag_pic == 1)
22048         {
22049           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
22050           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22051                        reg_names[0], reg_names[1]);
22052           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22053           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
22054           assemble_name (file, buf);
22055           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
22056         }
22057       else if (flag_pic > 1)
22058         {
22059           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22060                        reg_names[0], reg_names[1]);
22061           /* Now, we need to get the address of the label.  */
22062           if (TARGET_LINK_STACK)
22063             {
22064               char name[32];
22065               get_ppc476_thunk_name (name);
22066               asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
22067               assemble_name (file, buf);
22068               fputs ("-.\n1:", file);
22069               asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22070               asm_fprintf (file, "\taddi %s,%s,4\n",
22071                            reg_names[11], reg_names[11]);
22072             }
22073           else
22074             {
22075               fputs ("\tbcl 20,31,1f\n\t.long ", file);
22076               assemble_name (file, buf);
22077               fputs ("-.\n1:", file);
22078               asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22079             }
22080           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n",
22081                        reg_names[0], reg_names[11]);
22082           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
22083                        reg_names[0], reg_names[0], reg_names[11]);
22084         }
22085       else
22086         {
22087           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
22088           assemble_name (file, buf);
22089           fputs ("@ha\n", file);
22090           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22091                        reg_names[0], reg_names[1]);
22092           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
22093           assemble_name (file, buf);
22094           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
22095         }
22096
22097       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
22098       fprintf (file, "\tbl %s%s\n",
22099                RS6000_MCOUNT, flag_pic ? "@plt" : "");
22100       break;
22101
22102     case ABI_AIX:
22103     case ABI_DARWIN:
22104       if (!TARGET_PROFILE_KERNEL)
22105         {
22106           /* Don't do anything, done in output_profile_hook ().  */
22107         }
22108       else
22109         {
22110           gcc_assert (!TARGET_32BIT);
22111
22112           asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
22113           asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
22114
22115           if (cfun->static_chain_decl != NULL)
22116             {
22117               asm_fprintf (file, "\tstd %s,24(%s)\n",
22118                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22119               fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22120               asm_fprintf (file, "\tld %s,24(%s)\n",
22121                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22122             }
22123           else
22124             fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22125         }
22126       break;
22127     }
22128 }
22129
22130 \f
22131
22132 /* The following variable value is the last issued insn.  */
22133
22134 static rtx last_scheduled_insn;
22135
22136 /* The following variable helps to balance issuing of load and
22137    store instructions */
22138
22139 static int load_store_pendulum;
22140
22141 /* Power4 load update and store update instructions are cracked into a
22142    load or store and an integer insn which are executed in the same cycle.
22143    Branches have their own dispatch slot which does not count against the
22144    GCC issue rate, but it changes the program flow so there are no other
22145    instructions to issue in this cycle.  */
22146
22147 static int
22148 rs6000_variable_issue_1 (rtx insn, int more)
22149 {
22150   last_scheduled_insn = insn;
22151   if (GET_CODE (PATTERN (insn)) == USE
22152       || GET_CODE (PATTERN (insn)) == CLOBBER)
22153     {
22154       cached_can_issue_more = more;
22155       return cached_can_issue_more;
22156     }
22157
22158   if (insn_terminates_group_p (insn, current_group))
22159     {
22160       cached_can_issue_more = 0;
22161       return cached_can_issue_more;
22162     }
22163
22164   /* If no reservation, but reach here */
22165   if (recog_memoized (insn) < 0)
22166     return more;
22167
22168   if (rs6000_sched_groups)
22169     {
22170       if (is_microcoded_insn (insn))
22171         cached_can_issue_more = 0;
22172       else if (is_cracked_insn (insn))
22173         cached_can_issue_more = more > 2 ? more - 2 : 0;
22174       else
22175         cached_can_issue_more = more - 1;
22176
22177       return cached_can_issue_more;
22178     }
22179
22180   if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
22181     return 0;
22182
22183   cached_can_issue_more = more - 1;
22184   return cached_can_issue_more;
22185 }
22186
22187 static int
22188 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
22189 {
22190   int r = rs6000_variable_issue_1 (insn, more);
22191   if (verbose)
22192     fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
22193   return r;
22194 }
22195
22196 /* Adjust the cost of a scheduling dependency.  Return the new cost of
22197    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
22198
22199 static int
22200 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22201 {
22202   enum attr_type attr_type;
22203
22204   if (! recog_memoized (insn))
22205     return 0;
22206
22207   switch (REG_NOTE_KIND (link))
22208     {
22209     case REG_DEP_TRUE:
22210       {
22211         /* Data dependency; DEP_INSN writes a register that INSN reads
22212            some cycles later.  */
22213
22214         /* Separate a load from a narrower, dependent store.  */
22215         if (rs6000_sched_groups
22216             && GET_CODE (PATTERN (insn)) == SET
22217             && GET_CODE (PATTERN (dep_insn)) == SET
22218             && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
22219             && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
22220             && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
22221                 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
22222           return cost + 14;
22223
22224         attr_type = get_attr_type (insn);
22225
22226         switch (attr_type)
22227           {
22228           case TYPE_JMPREG:
22229             /* Tell the first scheduling pass about the latency between
22230                a mtctr and bctr (and mtlr and br/blr).  The first
22231                scheduling pass will not know about this latency since
22232                the mtctr instruction, which has the latency associated
22233                to it, will be generated by reload.  */
22234             return TARGET_POWER ? 5 : 4;
22235           case TYPE_BRANCH:
22236             /* Leave some extra cycles between a compare and its
22237                dependent branch, to inhibit expensive mispredicts.  */
22238             if ((rs6000_cpu_attr == CPU_PPC603
22239                  || rs6000_cpu_attr == CPU_PPC604
22240                  || rs6000_cpu_attr == CPU_PPC604E
22241                  || rs6000_cpu_attr == CPU_PPC620
22242                  || rs6000_cpu_attr == CPU_PPC630
22243                  || rs6000_cpu_attr == CPU_PPC750
22244                  || rs6000_cpu_attr == CPU_PPC7400
22245                  || rs6000_cpu_attr == CPU_PPC7450
22246                  || rs6000_cpu_attr == CPU_POWER4
22247                  || rs6000_cpu_attr == CPU_POWER5
22248                  || rs6000_cpu_attr == CPU_POWER7
22249                  || rs6000_cpu_attr == CPU_CELL)
22250                 && recog_memoized (dep_insn)
22251                 && (INSN_CODE (dep_insn) >= 0))
22252
22253               switch (get_attr_type (dep_insn))
22254                 {
22255                 case TYPE_CMP:
22256                 case TYPE_COMPARE:
22257                 case TYPE_DELAYED_COMPARE:
22258                 case TYPE_IMUL_COMPARE:
22259                 case TYPE_LMUL_COMPARE:
22260                 case TYPE_FPCOMPARE:
22261                 case TYPE_CR_LOGICAL:
22262                 case TYPE_DELAYED_CR:
22263                   return cost + 2;
22264                 default:
22265                   break;
22266                 }
22267             break;
22268
22269           case TYPE_STORE:
22270           case TYPE_STORE_U:
22271           case TYPE_STORE_UX:
22272           case TYPE_FPSTORE:
22273           case TYPE_FPSTORE_U:
22274           case TYPE_FPSTORE_UX:
22275             if ((rs6000_cpu == PROCESSOR_POWER6)
22276                 && recog_memoized (dep_insn)
22277                 && (INSN_CODE (dep_insn) >= 0))
22278               {
22279
22280                 if (GET_CODE (PATTERN (insn)) != SET)
22281                   /* If this happens, we have to extend this to schedule
22282                      optimally.  Return default for now.  */
22283                   return cost;
22284
22285                 /* Adjust the cost for the case where the value written
22286                    by a fixed point operation is used as the address
22287                    gen value on a store. */
22288                 switch (get_attr_type (dep_insn))
22289                   {
22290                   case TYPE_LOAD:
22291                   case TYPE_LOAD_U:
22292                   case TYPE_LOAD_UX:
22293                   case TYPE_CNTLZ:
22294                     {
22295                       if (! store_data_bypass_p (dep_insn, insn))
22296                         return 4;
22297                       break;
22298                     }
22299                   case TYPE_LOAD_EXT:
22300                   case TYPE_LOAD_EXT_U:
22301                   case TYPE_LOAD_EXT_UX:
22302                   case TYPE_VAR_SHIFT_ROTATE:
22303                   case TYPE_VAR_DELAYED_COMPARE:
22304                     {
22305                       if (! store_data_bypass_p (dep_insn, insn))
22306                         return 6;
22307                       break;
22308                     }
22309                   case TYPE_INTEGER:
22310                   case TYPE_COMPARE:
22311                   case TYPE_FAST_COMPARE:
22312                   case TYPE_EXTS:
22313                   case TYPE_SHIFT:
22314                   case TYPE_INSERT_WORD:
22315                   case TYPE_INSERT_DWORD:
22316                   case TYPE_FPLOAD_U:
22317                   case TYPE_FPLOAD_UX:
22318                   case TYPE_STORE_U:
22319                   case TYPE_STORE_UX:
22320                   case TYPE_FPSTORE_U:
22321                   case TYPE_FPSTORE_UX:
22322                     {
22323                       if (! store_data_bypass_p (dep_insn, insn))
22324                         return 3;
22325                       break;
22326                     }
22327                   case TYPE_IMUL:
22328                   case TYPE_IMUL2:
22329                   case TYPE_IMUL3:
22330                   case TYPE_LMUL:
22331                   case TYPE_IMUL_COMPARE:
22332                   case TYPE_LMUL_COMPARE:
22333                     {
22334                       if (! store_data_bypass_p (dep_insn, insn))
22335                         return 17;
22336                       break;
22337                     }
22338                   case TYPE_IDIV:
22339                     {
22340                       if (! store_data_bypass_p (dep_insn, insn))
22341                         return 45;
22342                       break;
22343                     }
22344                   case TYPE_LDIV:
22345                     {
22346                       if (! store_data_bypass_p (dep_insn, insn))
22347                         return 57;
22348                       break;
22349                     }
22350                   default:
22351                     break;
22352                   }
22353               }
22354             break;
22355
22356           case TYPE_LOAD:
22357           case TYPE_LOAD_U:
22358           case TYPE_LOAD_UX:
22359           case TYPE_LOAD_EXT:
22360           case TYPE_LOAD_EXT_U:
22361           case TYPE_LOAD_EXT_UX:
22362             if ((rs6000_cpu == PROCESSOR_POWER6)
22363                 && recog_memoized (dep_insn)
22364                 && (INSN_CODE (dep_insn) >= 0))
22365               {
22366
22367                 /* Adjust the cost for the case where the value written
22368                    by a fixed point instruction is used within the address
22369                    gen portion of a subsequent load(u)(x) */
22370                 switch (get_attr_type (dep_insn))
22371                   {
22372                   case TYPE_LOAD:
22373                   case TYPE_LOAD_U:
22374                   case TYPE_LOAD_UX:
22375                   case TYPE_CNTLZ:
22376                     {
22377                       if (set_to_load_agen (dep_insn, insn))
22378                         return 4;
22379                       break;
22380                     }
22381                   case TYPE_LOAD_EXT:
22382                   case TYPE_LOAD_EXT_U:
22383                   case TYPE_LOAD_EXT_UX:
22384                   case TYPE_VAR_SHIFT_ROTATE:
22385                   case TYPE_VAR_DELAYED_COMPARE:
22386                     {
22387                       if (set_to_load_agen (dep_insn, insn))
22388                         return 6;
22389                       break;
22390                     }
22391                   case TYPE_INTEGER:
22392                   case TYPE_COMPARE:
22393                   case TYPE_FAST_COMPARE:
22394                   case TYPE_EXTS:
22395                   case TYPE_SHIFT:
22396                   case TYPE_INSERT_WORD:
22397                   case TYPE_INSERT_DWORD:
22398                   case TYPE_FPLOAD_U:
22399                   case TYPE_FPLOAD_UX:
22400                   case TYPE_STORE_U:
22401                   case TYPE_STORE_UX:
22402                   case TYPE_FPSTORE_U:
22403                   case TYPE_FPSTORE_UX:
22404                     {
22405                       if (set_to_load_agen (dep_insn, insn))
22406                         return 3;
22407                       break;
22408                     }
22409                   case TYPE_IMUL:
22410                   case TYPE_IMUL2:
22411                   case TYPE_IMUL3:
22412                   case TYPE_LMUL:
22413                   case TYPE_IMUL_COMPARE:
22414                   case TYPE_LMUL_COMPARE:
22415                     {
22416                       if (set_to_load_agen (dep_insn, insn))
22417                         return 17;
22418                       break;
22419                     }
22420                   case TYPE_IDIV:
22421                     {
22422                       if (set_to_load_agen (dep_insn, insn))
22423                         return 45;
22424                       break;
22425                     }
22426                   case TYPE_LDIV:
22427                     {
22428                       if (set_to_load_agen (dep_insn, insn))
22429                         return 57;
22430                       break;
22431                     }
22432                   default:
22433                     break;
22434                   }
22435               }
22436             break;
22437
22438           case TYPE_FPLOAD:
22439             if ((rs6000_cpu == PROCESSOR_POWER6)
22440                 && recog_memoized (dep_insn)
22441                 && (INSN_CODE (dep_insn) >= 0)
22442                 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
22443               return 2;
22444
22445           default:
22446             break;
22447           }
22448
22449         /* Fall out to return default cost.  */
22450       }
22451       break;
22452
22453     case REG_DEP_OUTPUT:
22454       /* Output dependency; DEP_INSN writes a register that INSN writes some
22455          cycles later.  */
22456       if ((rs6000_cpu == PROCESSOR_POWER6)
22457           && recog_memoized (dep_insn)
22458           && (INSN_CODE (dep_insn) >= 0))
22459         {
22460           attr_type = get_attr_type (insn);
22461
22462           switch (attr_type)
22463             {
22464             case TYPE_FP:
22465               if (get_attr_type (dep_insn) == TYPE_FP)
22466                 return 1;
22467               break;
22468             case TYPE_FPLOAD:
22469               if (get_attr_type (dep_insn) == TYPE_MFFGPR)
22470                 return 2;
22471               break;
22472             default:
22473               break;
22474             }
22475         }
22476     case REG_DEP_ANTI:
22477       /* Anti dependency; DEP_INSN reads a register that INSN writes some
22478          cycles later.  */
22479       return 0;
22480
22481     default:
22482       gcc_unreachable ();
22483     }
22484
22485   return cost;
22486 }
22487
22488 /* Debug version of rs6000_adjust_cost.  */
22489
22490 static int
22491 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22492 {
22493   int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
22494
22495   if (ret != cost)
22496     {
22497       const char *dep;
22498
22499       switch (REG_NOTE_KIND (link))
22500         {
22501         default:             dep = "unknown depencency"; break;
22502         case REG_DEP_TRUE:   dep = "data dependency";    break;
22503         case REG_DEP_OUTPUT: dep = "output dependency";  break;
22504         case REG_DEP_ANTI:   dep = "anti depencency";    break;
22505         }
22506
22507       fprintf (stderr,
22508                "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
22509                "%s, insn:\n", ret, cost, dep);
22510
22511       debug_rtx (insn);
22512     }
22513
22514   return ret;
22515 }
22516
22517 /* The function returns a true if INSN is microcoded.
22518    Return false otherwise.  */
22519
22520 static bool
22521 is_microcoded_insn (rtx insn)
22522 {
22523   if (!insn || !NONDEBUG_INSN_P (insn)
22524       || GET_CODE (PATTERN (insn)) == USE
22525       || GET_CODE (PATTERN (insn)) == CLOBBER)
22526     return false;
22527
22528   if (rs6000_cpu_attr == CPU_CELL)
22529     return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
22530
22531   if (rs6000_sched_groups)
22532     {
22533       enum attr_type type = get_attr_type (insn);
22534       if (type == TYPE_LOAD_EXT_U
22535           || type == TYPE_LOAD_EXT_UX
22536           || type == TYPE_LOAD_UX
22537           || type == TYPE_STORE_UX
22538           || type == TYPE_MFCR)
22539         return true;
22540     }
22541
22542   return false;
22543 }
22544
22545 /* The function returns true if INSN is cracked into 2 instructions
22546    by the processor (and therefore occupies 2 issue slots).  */
22547
22548 static bool
22549 is_cracked_insn (rtx insn)
22550 {
22551   if (!insn || !NONDEBUG_INSN_P (insn)
22552       || GET_CODE (PATTERN (insn)) == USE
22553       || GET_CODE (PATTERN (insn)) == CLOBBER)
22554     return false;
22555
22556   if (rs6000_sched_groups)
22557     {
22558       enum attr_type type = get_attr_type (insn);
22559       if (type == TYPE_LOAD_U || type == TYPE_STORE_U
22560           || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
22561           || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
22562           || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
22563           || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
22564           || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
22565           || type == TYPE_IDIV || type == TYPE_LDIV
22566           || type == TYPE_INSERT_WORD)
22567         return true;
22568     }
22569
22570   return false;
22571 }
22572
22573 /* The function returns true if INSN can be issued only from
22574    the branch slot.  */
22575
22576 static bool
22577 is_branch_slot_insn (rtx insn)
22578 {
22579   if (!insn || !NONDEBUG_INSN_P (insn)
22580       || GET_CODE (PATTERN (insn)) == USE
22581       || GET_CODE (PATTERN (insn)) == CLOBBER)
22582     return false;
22583
22584   if (rs6000_sched_groups)
22585     {
22586       enum attr_type type = get_attr_type (insn);
22587       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
22588         return true;
22589       return false;
22590     }
22591
22592   return false;
22593 }
22594
22595 /* The function returns true if out_inst sets a value that is
22596    used in the address generation computation of in_insn */
22597 static bool
22598 set_to_load_agen (rtx out_insn, rtx in_insn)
22599 {
22600   rtx out_set, in_set;
22601
22602   /* For performance reasons, only handle the simple case where
22603      both loads are a single_set. */
22604   out_set = single_set (out_insn);
22605   if (out_set)
22606     {
22607       in_set = single_set (in_insn);
22608       if (in_set)
22609         return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
22610     }
22611
22612   return false;
22613 }
22614
22615 /* The function returns true if the target storage location of
22616    out_insn is adjacent to the target storage location of in_insn */
22617 /* Return 1 if memory locations are adjacent.  */
22618
22619 static bool
22620 adjacent_mem_locations (rtx insn1, rtx insn2)
22621 {
22622
22623   rtx a = get_store_dest (PATTERN (insn1));
22624   rtx b = get_store_dest (PATTERN (insn2));
22625
22626   if ((GET_CODE (XEXP (a, 0)) == REG
22627        || (GET_CODE (XEXP (a, 0)) == PLUS
22628            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
22629       && (GET_CODE (XEXP (b, 0)) == REG
22630           || (GET_CODE (XEXP (b, 0)) == PLUS
22631               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
22632     {
22633       HOST_WIDE_INT val0 = 0, val1 = 0, val_diff;
22634       rtx reg0, reg1;
22635
22636       if (GET_CODE (XEXP (a, 0)) == PLUS)
22637         {
22638           reg0 = XEXP (XEXP (a, 0), 0);
22639           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
22640         }
22641       else
22642         reg0 = XEXP (a, 0);
22643
22644       if (GET_CODE (XEXP (b, 0)) == PLUS)
22645         {
22646           reg1 = XEXP (XEXP (b, 0), 0);
22647           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
22648         }
22649       else
22650         reg1 = XEXP (b, 0);
22651
22652       val_diff = val1 - val0;
22653
22654       return ((REGNO (reg0) == REGNO (reg1))
22655               && ((MEM_SIZE_KNOWN_P (a) && val_diff == MEM_SIZE (a))
22656                   || (MEM_SIZE_KNOWN_P (b) && val_diff == -MEM_SIZE (b))));
22657     }
22658
22659   return false;
22660 }
22661
22662 /* A C statement (sans semicolon) to update the integer scheduling
22663    priority INSN_PRIORITY (INSN). Increase the priority to execute the
22664    INSN earlier, reduce the priority to execute INSN later.  Do not
22665    define this macro if you do not need to adjust the scheduling
22666    priorities of insns.  */
22667
22668 static int
22669 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
22670 {
22671   /* On machines (like the 750) which have asymmetric integer units,
22672      where one integer unit can do multiply and divides and the other
22673      can't, reduce the priority of multiply/divide so it is scheduled
22674      before other integer operations.  */
22675
22676 #if 0
22677   if (! INSN_P (insn))
22678     return priority;
22679
22680   if (GET_CODE (PATTERN (insn)) == USE)
22681     return priority;
22682
22683   switch (rs6000_cpu_attr) {
22684   case CPU_PPC750:
22685     switch (get_attr_type (insn))
22686       {
22687       default:
22688         break;
22689
22690       case TYPE_IMUL:
22691       case TYPE_IDIV:
22692         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
22693                  priority, priority);
22694         if (priority >= 0 && priority < 0x01000000)
22695           priority >>= 3;
22696         break;
22697       }
22698   }
22699 #endif
22700
22701   if (insn_must_be_first_in_group (insn)
22702       && reload_completed
22703       && current_sched_info->sched_max_insns_priority
22704       && rs6000_sched_restricted_insns_priority)
22705     {
22706
22707       /* Prioritize insns that can be dispatched only in the first
22708          dispatch slot.  */
22709       if (rs6000_sched_restricted_insns_priority == 1)
22710         /* Attach highest priority to insn. This means that in
22711            haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
22712            precede 'priority' (critical path) considerations.  */
22713         return current_sched_info->sched_max_insns_priority;
22714       else if (rs6000_sched_restricted_insns_priority == 2)
22715         /* Increase priority of insn by a minimal amount. This means that in
22716            haifa-sched.c:ready_sort(), only 'priority' (critical path)
22717            considerations precede dispatch-slot restriction considerations.  */
22718         return (priority + 1);
22719     }
22720
22721   if (rs6000_cpu == PROCESSOR_POWER6
22722       && ((load_store_pendulum == -2 && is_load_insn (insn))
22723           || (load_store_pendulum == 2 && is_store_insn (insn))))
22724     /* Attach highest priority to insn if the scheduler has just issued two
22725        stores and this instruction is a load, or two loads and this instruction
22726        is a store. Power6 wants loads and stores scheduled alternately
22727        when possible */
22728     return current_sched_info->sched_max_insns_priority;
22729
22730   return priority;
22731 }
22732
22733 /* Return true if the instruction is nonpipelined on the Cell. */
22734 static bool
22735 is_nonpipeline_insn (rtx insn)
22736 {
22737   enum attr_type type;
22738   if (!insn || !NONDEBUG_INSN_P (insn)
22739       || GET_CODE (PATTERN (insn)) == USE
22740       || GET_CODE (PATTERN (insn)) == CLOBBER)
22741     return false;
22742
22743   type = get_attr_type (insn);
22744   if (type == TYPE_IMUL
22745       || type == TYPE_IMUL2
22746       || type == TYPE_IMUL3
22747       || type == TYPE_LMUL
22748       || type == TYPE_IDIV
22749       || type == TYPE_LDIV
22750       || type == TYPE_SDIV
22751       || type == TYPE_DDIV
22752       || type == TYPE_SSQRT
22753       || type == TYPE_DSQRT
22754       || type == TYPE_MFCR
22755       || type == TYPE_MFCRF
22756       || type == TYPE_MFJMPR)
22757     {
22758       return true;
22759     }
22760   return false;
22761 }
22762
22763
22764 /* Return how many instructions the machine can issue per cycle.  */
22765
22766 static int
22767 rs6000_issue_rate (void)
22768 {
22769   /* Unless scheduling for register pressure, use issue rate of 1 for
22770      first scheduling pass to decrease degradation.  */
22771   if (!reload_completed && !flag_sched_pressure)
22772     return 1;
22773
22774   switch (rs6000_cpu_attr) {
22775   case CPU_RIOS1:  /* ? */
22776   case CPU_RS64A:
22777   case CPU_PPC601: /* ? */
22778   case CPU_PPC7450:
22779     return 3;
22780   case CPU_PPC440:
22781   case CPU_PPC603:
22782   case CPU_PPC750:
22783   case CPU_PPC7400:
22784   case CPU_PPC8540:
22785   case CPU_CELL:
22786   case CPU_PPCE300C2:
22787   case CPU_PPCE300C3:
22788   case CPU_PPCE500MC:
22789   case CPU_PPCE500MC64:
22790   case CPU_TITAN:
22791     return 2;
22792   case CPU_RIOS2:
22793   case CPU_PPC476:
22794   case CPU_PPC604:
22795   case CPU_PPC604E:
22796   case CPU_PPC620:
22797   case CPU_PPC630:
22798     return 4;
22799   case CPU_POWER4:
22800   case CPU_POWER5:
22801   case CPU_POWER6:
22802   case CPU_POWER7:
22803     return 5;
22804   default:
22805     return 1;
22806   }
22807 }
22808
22809 /* Return how many instructions to look ahead for better insn
22810    scheduling.  */
22811
22812 static int
22813 rs6000_use_sched_lookahead (void)
22814 {
22815   if (rs6000_cpu_attr == CPU_PPC8540)
22816     return 4;
22817   if (rs6000_cpu_attr == CPU_CELL)
22818     return (reload_completed ? 8 : 0);
22819   return 0;
22820 }
22821
22822 /* We are choosing insn from the ready queue.  Return nonzero if INSN can be chosen.  */
22823 static int
22824 rs6000_use_sched_lookahead_guard (rtx insn)
22825 {
22826   if (rs6000_cpu_attr != CPU_CELL)
22827     return 1;
22828
22829    if (insn == NULL_RTX || !INSN_P (insn))
22830      abort ();
22831
22832   if (!reload_completed
22833       || is_nonpipeline_insn (insn)
22834       || is_microcoded_insn (insn))
22835     return 0;
22836
22837   return 1;
22838 }
22839
22840 /* Determine is PAT refers to memory.  */
22841
22842 static bool
22843 is_mem_ref (rtx pat)
22844 {
22845   const char * fmt;
22846   int i, j;
22847   bool ret = false;
22848
22849   /* stack_tie does not produce any real memory traffic.  */
22850   if (GET_CODE (pat) == UNSPEC
22851       && XINT (pat, 1) == UNSPEC_TIE)
22852     return false;
22853
22854   if (GET_CODE (pat) == MEM)
22855     return true;
22856
22857   /* Recursively process the pattern.  */
22858   fmt = GET_RTX_FORMAT (GET_CODE (pat));
22859
22860   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0 && !ret; i--)
22861     {
22862       if (fmt[i] == 'e')
22863         ret |= is_mem_ref (XEXP (pat, i));
22864       else if (fmt[i] == 'E')
22865         for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
22866           ret |= is_mem_ref (XVECEXP (pat, i, j));
22867     }
22868
22869   return ret;
22870 }
22871
22872 /* Determine if PAT is a PATTERN of a load insn.  */
22873
22874 static bool
22875 is_load_insn1 (rtx pat)
22876 {
22877   if (!pat || pat == NULL_RTX)
22878     return false;
22879
22880   if (GET_CODE (pat) == SET)
22881     return is_mem_ref (SET_SRC (pat));
22882
22883   if (GET_CODE (pat) == PARALLEL)
22884     {
22885       int i;
22886
22887       for (i = 0; i < XVECLEN (pat, 0); i++)
22888         if (is_load_insn1 (XVECEXP (pat, 0, i)))
22889           return true;
22890     }
22891
22892   return false;
22893 }
22894
22895 /* Determine if INSN loads from memory.  */
22896
22897 static bool
22898 is_load_insn (rtx insn)
22899 {
22900   if (!insn || !INSN_P (insn))
22901     return false;
22902
22903   if (GET_CODE (insn) == CALL_INSN)
22904     return false;
22905
22906   return is_load_insn1 (PATTERN (insn));
22907 }
22908
22909 /* Determine if PAT is a PATTERN of a store insn.  */
22910
22911 static bool
22912 is_store_insn1 (rtx pat)
22913 {
22914   if (!pat || pat == NULL_RTX)
22915     return false;
22916
22917   if (GET_CODE (pat) == SET)
22918     return is_mem_ref (SET_DEST (pat));
22919
22920   if (GET_CODE (pat) == PARALLEL)
22921     {
22922       int i;
22923
22924       for (i = 0; i < XVECLEN (pat, 0); i++)
22925         if (is_store_insn1 (XVECEXP (pat, 0, i)))
22926           return true;
22927     }
22928
22929   return false;
22930 }
22931
22932 /* Determine if INSN stores to memory.  */
22933
22934 static bool
22935 is_store_insn (rtx insn)
22936 {
22937   if (!insn || !INSN_P (insn))
22938     return false;
22939
22940   return is_store_insn1 (PATTERN (insn));
22941 }
22942
22943 /* Return the dest of a store insn.  */
22944
22945 static rtx
22946 get_store_dest (rtx pat)
22947 {
22948   gcc_assert (is_store_insn1 (pat));
22949
22950   if (GET_CODE (pat) == SET)
22951     return SET_DEST (pat);
22952   else if (GET_CODE (pat) == PARALLEL)
22953     {
22954       int i;
22955
22956       for (i = 0; i < XVECLEN (pat, 0); i++)
22957         {
22958           rtx inner_pat = XVECEXP (pat, 0, i);
22959           if (GET_CODE (inner_pat) == SET
22960               && is_mem_ref (SET_DEST (inner_pat)))
22961             return inner_pat;
22962         }
22963     }
22964   /* We shouldn't get here, because we should have either a simple
22965      store insn or a store with update which are covered above.  */
22966   gcc_unreachable();
22967 }
22968
22969 /* Returns whether the dependence between INSN and NEXT is considered
22970    costly by the given target.  */
22971
22972 static bool
22973 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
22974 {
22975   rtx insn;
22976   rtx next;
22977
22978   /* If the flag is not enabled - no dependence is considered costly;
22979      allow all dependent insns in the same group.
22980      This is the most aggressive option.  */
22981   if (rs6000_sched_costly_dep == no_dep_costly)
22982     return false;
22983
22984   /* If the flag is set to 1 - a dependence is always considered costly;
22985      do not allow dependent instructions in the same group.
22986      This is the most conservative option.  */
22987   if (rs6000_sched_costly_dep == all_deps_costly)
22988     return true;
22989
22990   insn = DEP_PRO (dep);
22991   next = DEP_CON (dep);
22992
22993   if (rs6000_sched_costly_dep == store_to_load_dep_costly
22994       && is_load_insn (next)
22995       && is_store_insn (insn))
22996     /* Prevent load after store in the same group.  */
22997     return true;
22998
22999   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
23000       && is_load_insn (next)
23001       && is_store_insn (insn)
23002       && DEP_TYPE (dep) == REG_DEP_TRUE)
23003      /* Prevent load after store in the same group if it is a true
23004         dependence.  */
23005      return true;
23006
23007   /* The flag is set to X; dependences with latency >= X are considered costly,
23008      and will not be scheduled in the same group.  */
23009   if (rs6000_sched_costly_dep <= max_dep_latency
23010       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
23011     return true;
23012
23013   return false;
23014 }
23015
23016 /* Return the next insn after INSN that is found before TAIL is reached,
23017    skipping any "non-active" insns - insns that will not actually occupy
23018    an issue slot.  Return NULL_RTX if such an insn is not found.  */
23019
23020 static rtx
23021 get_next_active_insn (rtx insn, rtx tail)
23022 {
23023   if (insn == NULL_RTX || insn == tail)
23024     return NULL_RTX;
23025
23026   while (1)
23027     {
23028       insn = NEXT_INSN (insn);
23029       if (insn == NULL_RTX || insn == tail)
23030         return NULL_RTX;
23031
23032       if (CALL_P (insn)
23033           || JUMP_P (insn)
23034           || (NONJUMP_INSN_P (insn)
23035               && GET_CODE (PATTERN (insn)) != USE
23036               && GET_CODE (PATTERN (insn)) != CLOBBER
23037               && INSN_CODE (insn) != CODE_FOR_stack_tie))
23038         break;
23039     }
23040   return insn;
23041 }
23042
23043 /* We are about to begin issuing insns for this clock cycle. */
23044
23045 static int
23046 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
23047                         rtx *ready ATTRIBUTE_UNUSED,
23048                         int *pn_ready ATTRIBUTE_UNUSED,
23049                         int clock_var ATTRIBUTE_UNUSED)
23050 {
23051   int n_ready = *pn_ready;
23052
23053   if (sched_verbose)
23054     fprintf (dump, "// rs6000_sched_reorder :\n");
23055
23056   /* Reorder the ready list, if the second to last ready insn
23057      is a nonepipeline insn.  */
23058   if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
23059   {
23060     if (is_nonpipeline_insn (ready[n_ready - 1])
23061         && (recog_memoized (ready[n_ready - 2]) > 0))
23062       /* Simply swap first two insns.  */
23063       {
23064         rtx tmp = ready[n_ready - 1];
23065         ready[n_ready - 1] = ready[n_ready - 2];
23066         ready[n_ready - 2] = tmp;
23067       }
23068   }
23069
23070   if (rs6000_cpu == PROCESSOR_POWER6)
23071     load_store_pendulum = 0;
23072
23073   return rs6000_issue_rate ();
23074 }
23075
23076 /* Like rs6000_sched_reorder, but called after issuing each insn.  */
23077
23078 static int
23079 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
23080                          int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
23081 {
23082   if (sched_verbose)
23083     fprintf (dump, "// rs6000_sched_reorder2 :\n");
23084
23085   /* For Power6, we need to handle some special cases to try and keep the
23086      store queue from overflowing and triggering expensive flushes.
23087
23088      This code monitors how load and store instructions are being issued
23089      and skews the ready list one way or the other to increase the likelihood
23090      that a desired instruction is issued at the proper time.
23091
23092      A couple of things are done.  First, we maintain a "load_store_pendulum"
23093      to track the current state of load/store issue.
23094
23095        - If the pendulum is at zero, then no loads or stores have been
23096          issued in the current cycle so we do nothing.
23097
23098        - If the pendulum is 1, then a single load has been issued in this
23099          cycle and we attempt to locate another load in the ready list to
23100          issue with it.
23101
23102        - If the pendulum is -2, then two stores have already been
23103          issued in this cycle, so we increase the priority of the first load
23104          in the ready list to increase it's likelihood of being chosen first
23105          in the next cycle.
23106
23107        - If the pendulum is -1, then a single store has been issued in this
23108          cycle and we attempt to locate another store in the ready list to
23109          issue with it, preferring a store to an adjacent memory location to
23110          facilitate store pairing in the store queue.
23111
23112        - If the pendulum is 2, then two loads have already been
23113          issued in this cycle, so we increase the priority of the first store
23114          in the ready list to increase it's likelihood of being chosen first
23115          in the next cycle.
23116
23117        - If the pendulum < -2 or > 2, then do nothing.
23118
23119        Note: This code covers the most common scenarios.  There exist non
23120              load/store instructions which make use of the LSU and which
23121              would need to be accounted for to strictly model the behavior
23122              of the machine.  Those instructions are currently unaccounted
23123              for to help minimize compile time overhead of this code.
23124    */
23125   if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
23126     {
23127       int pos;
23128       int i;
23129       rtx tmp;
23130
23131       if (is_store_insn (last_scheduled_insn))
23132         /* Issuing a store, swing the load_store_pendulum to the left */
23133         load_store_pendulum--;
23134       else if (is_load_insn (last_scheduled_insn))
23135         /* Issuing a load, swing the load_store_pendulum to the right */
23136         load_store_pendulum++;
23137       else
23138         return cached_can_issue_more;
23139
23140       /* If the pendulum is balanced, or there is only one instruction on
23141          the ready list, then all is well, so return. */
23142       if ((load_store_pendulum == 0) || (*pn_ready <= 1))
23143         return cached_can_issue_more;
23144
23145       if (load_store_pendulum == 1)
23146         {
23147           /* A load has been issued in this cycle.  Scan the ready list
23148              for another load to issue with it */
23149           pos = *pn_ready-1;
23150
23151           while (pos >= 0)
23152             {
23153               if (is_load_insn (ready[pos]))
23154                 {
23155                   /* Found a load.  Move it to the head of the ready list,
23156                      and adjust it's priority so that it is more likely to
23157                      stay there */
23158                   tmp = ready[pos];
23159                   for (i=pos; i<*pn_ready-1; i++)
23160                     ready[i] = ready[i + 1];
23161                   ready[*pn_ready-1] = tmp;
23162
23163                   if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23164                     INSN_PRIORITY (tmp)++;
23165                   break;
23166                 }
23167               pos--;
23168             }
23169         }
23170       else if (load_store_pendulum == -2)
23171         {
23172           /* Two stores have been issued in this cycle.  Increase the
23173              priority of the first load in the ready list to favor it for
23174              issuing in the next cycle. */
23175           pos = *pn_ready-1;
23176
23177           while (pos >= 0)
23178             {
23179               if (is_load_insn (ready[pos])
23180                   && !sel_sched_p ()
23181                   && INSN_PRIORITY_KNOWN (ready[pos]))
23182                 {
23183                   INSN_PRIORITY (ready[pos])++;
23184
23185                   /* Adjust the pendulum to account for the fact that a load
23186                      was found and increased in priority.  This is to prevent
23187                      increasing the priority of multiple loads */
23188                   load_store_pendulum--;
23189
23190                   break;
23191                 }
23192               pos--;
23193             }
23194         }
23195       else if (load_store_pendulum == -1)
23196         {
23197           /* A store has been issued in this cycle.  Scan the ready list for
23198              another store to issue with it, preferring a store to an adjacent
23199              memory location */
23200           int first_store_pos = -1;
23201
23202           pos = *pn_ready-1;
23203
23204           while (pos >= 0)
23205             {
23206               if (is_store_insn (ready[pos]))
23207                 {
23208                   /* Maintain the index of the first store found on the
23209                      list */
23210                   if (first_store_pos == -1)
23211                     first_store_pos = pos;
23212
23213                   if (is_store_insn (last_scheduled_insn)
23214                       && adjacent_mem_locations (last_scheduled_insn,ready[pos]))
23215                     {
23216                       /* Found an adjacent store.  Move it to the head of the
23217                          ready list, and adjust it's priority so that it is
23218                          more likely to stay there */
23219                       tmp = ready[pos];
23220                       for (i=pos; i<*pn_ready-1; i++)
23221                         ready[i] = ready[i + 1];
23222                       ready[*pn_ready-1] = tmp;
23223
23224                       if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23225                         INSN_PRIORITY (tmp)++;
23226
23227                       first_store_pos = -1;
23228
23229                       break;
23230                     };
23231                 }
23232               pos--;
23233             }
23234
23235           if (first_store_pos >= 0)
23236             {
23237               /* An adjacent store wasn't found, but a non-adjacent store was,
23238                  so move the non-adjacent store to the front of the ready
23239                  list, and adjust its priority so that it is more likely to
23240                  stay there. */
23241               tmp = ready[first_store_pos];
23242               for (i=first_store_pos; i<*pn_ready-1; i++)
23243                 ready[i] = ready[i + 1];
23244               ready[*pn_ready-1] = tmp;
23245               if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23246                 INSN_PRIORITY (tmp)++;
23247             }
23248         }
23249       else if (load_store_pendulum == 2)
23250        {
23251            /* Two loads have been issued in this cycle.  Increase the priority
23252               of the first store in the ready list to favor it for issuing in
23253               the next cycle. */
23254           pos = *pn_ready-1;
23255
23256           while (pos >= 0)
23257             {
23258               if (is_store_insn (ready[pos])
23259                   && !sel_sched_p ()
23260                   && INSN_PRIORITY_KNOWN (ready[pos]))
23261                 {
23262                   INSN_PRIORITY (ready[pos])++;
23263
23264                   /* Adjust the pendulum to account for the fact that a store
23265                      was found and increased in priority.  This is to prevent
23266                      increasing the priority of multiple stores */
23267                   load_store_pendulum++;
23268
23269                   break;
23270                 }
23271               pos--;
23272             }
23273         }
23274     }
23275
23276   return cached_can_issue_more;
23277 }
23278
23279 /* Return whether the presence of INSN causes a dispatch group termination
23280    of group WHICH_GROUP.
23281
23282    If WHICH_GROUP == current_group, this function will return true if INSN
23283    causes the termination of the current group (i.e, the dispatch group to
23284    which INSN belongs). This means that INSN will be the last insn in the
23285    group it belongs to.
23286
23287    If WHICH_GROUP == previous_group, this function will return true if INSN
23288    causes the termination of the previous group (i.e, the dispatch group that
23289    precedes the group to which INSN belongs).  This means that INSN will be
23290    the first insn in the group it belongs to).  */
23291
23292 static bool
23293 insn_terminates_group_p (rtx insn, enum group_termination which_group)
23294 {
23295   bool first, last;
23296
23297   if (! insn)
23298     return false;
23299
23300   first = insn_must_be_first_in_group (insn);
23301   last = insn_must_be_last_in_group (insn);
23302
23303   if (first && last)
23304     return true;
23305
23306   if (which_group == current_group)
23307     return last;
23308   else if (which_group == previous_group)
23309     return first;
23310
23311   return false;
23312 }
23313
23314
23315 static bool
23316 insn_must_be_first_in_group (rtx insn)
23317 {
23318   enum attr_type type;
23319
23320   if (!insn
23321       || GET_CODE (insn) == NOTE
23322       || DEBUG_INSN_P (insn)
23323       || GET_CODE (PATTERN (insn)) == USE
23324       || GET_CODE (PATTERN (insn)) == CLOBBER)
23325     return false;
23326
23327   switch (rs6000_cpu)
23328     {
23329     case PROCESSOR_POWER5:
23330       if (is_cracked_insn (insn))
23331         return true;
23332     case PROCESSOR_POWER4:
23333       if (is_microcoded_insn (insn))
23334         return true;
23335
23336       if (!rs6000_sched_groups)
23337         return false;
23338
23339       type = get_attr_type (insn);
23340
23341       switch (type)
23342         {
23343         case TYPE_MFCR:
23344         case TYPE_MFCRF:
23345         case TYPE_MTCR:
23346         case TYPE_DELAYED_CR:
23347         case TYPE_CR_LOGICAL:
23348         case TYPE_MTJMPR:
23349         case TYPE_MFJMPR:
23350         case TYPE_IDIV:
23351         case TYPE_LDIV:
23352         case TYPE_LOAD_L:
23353         case TYPE_STORE_C:
23354         case TYPE_ISYNC:
23355         case TYPE_SYNC:
23356           return true;
23357         default:
23358           break;
23359         }
23360       break;
23361     case PROCESSOR_POWER6:
23362       type = get_attr_type (insn);
23363
23364       switch (type)
23365         {
23366         case TYPE_INSERT_DWORD:
23367         case TYPE_EXTS:
23368         case TYPE_CNTLZ:
23369         case TYPE_SHIFT:
23370         case TYPE_VAR_SHIFT_ROTATE:
23371         case TYPE_TRAP:
23372         case TYPE_IMUL:
23373         case TYPE_IMUL2:
23374         case TYPE_IMUL3:
23375         case TYPE_LMUL:
23376         case TYPE_IDIV:
23377         case TYPE_INSERT_WORD:
23378         case TYPE_DELAYED_COMPARE:
23379         case TYPE_IMUL_COMPARE:
23380         case TYPE_LMUL_COMPARE:
23381         case TYPE_FPCOMPARE:
23382         case TYPE_MFCR:
23383         case TYPE_MTCR:
23384         case TYPE_MFJMPR:
23385         case TYPE_MTJMPR:
23386         case TYPE_ISYNC:
23387         case TYPE_SYNC:
23388         case TYPE_LOAD_L:
23389         case TYPE_STORE_C:
23390         case TYPE_LOAD_U:
23391         case TYPE_LOAD_UX:
23392         case TYPE_LOAD_EXT_UX:
23393         case TYPE_STORE_U:
23394         case TYPE_STORE_UX:
23395         case TYPE_FPLOAD_U:
23396         case TYPE_FPLOAD_UX:
23397         case TYPE_FPSTORE_U:
23398         case TYPE_FPSTORE_UX:
23399           return true;
23400         default:
23401           break;
23402         }
23403       break;
23404     case PROCESSOR_POWER7:
23405       type = get_attr_type (insn);
23406
23407       switch (type)
23408         {
23409         case TYPE_CR_LOGICAL:
23410         case TYPE_MFCR:
23411         case TYPE_MFCRF:
23412         case TYPE_MTCR:
23413         case TYPE_IDIV:
23414         case TYPE_LDIV:
23415         case TYPE_COMPARE:
23416         case TYPE_DELAYED_COMPARE:
23417         case TYPE_VAR_DELAYED_COMPARE:
23418         case TYPE_ISYNC:
23419         case TYPE_LOAD_L:
23420         case TYPE_STORE_C:
23421         case TYPE_LOAD_U:
23422         case TYPE_LOAD_UX:
23423         case TYPE_LOAD_EXT:
23424         case TYPE_LOAD_EXT_U:
23425         case TYPE_LOAD_EXT_UX:
23426         case TYPE_STORE_U:
23427         case TYPE_STORE_UX:
23428         case TYPE_FPLOAD_U:
23429         case TYPE_FPLOAD_UX:
23430         case TYPE_FPSTORE_U:
23431         case TYPE_FPSTORE_UX:
23432         case TYPE_MFJMPR:
23433         case TYPE_MTJMPR:
23434           return true;
23435         default:
23436           break;
23437         }
23438       break;
23439     default:
23440       break;
23441     }
23442
23443   return false;
23444 }
23445
23446 static bool
23447 insn_must_be_last_in_group (rtx insn)
23448 {
23449   enum attr_type type;
23450
23451   if (!insn
23452       || GET_CODE (insn) == NOTE
23453       || DEBUG_INSN_P (insn)
23454       || GET_CODE (PATTERN (insn)) == USE
23455       || GET_CODE (PATTERN (insn)) == CLOBBER)
23456     return false;
23457
23458   switch (rs6000_cpu) {
23459   case PROCESSOR_POWER4:
23460   case PROCESSOR_POWER5:
23461     if (is_microcoded_insn (insn))
23462       return true;
23463
23464     if (is_branch_slot_insn (insn))
23465       return true;
23466
23467     break;
23468   case PROCESSOR_POWER6:
23469     type = get_attr_type (insn);
23470
23471     switch (type)
23472       {
23473       case TYPE_EXTS:
23474       case TYPE_CNTLZ:
23475       case TYPE_SHIFT:
23476       case TYPE_VAR_SHIFT_ROTATE:
23477       case TYPE_TRAP:
23478       case TYPE_IMUL:
23479       case TYPE_IMUL2:
23480       case TYPE_IMUL3:
23481       case TYPE_LMUL:
23482       case TYPE_IDIV:
23483       case TYPE_DELAYED_COMPARE:
23484       case TYPE_IMUL_COMPARE:
23485       case TYPE_LMUL_COMPARE:
23486       case TYPE_FPCOMPARE:
23487       case TYPE_MFCR:
23488       case TYPE_MTCR:
23489       case TYPE_MFJMPR:
23490       case TYPE_MTJMPR:
23491       case TYPE_ISYNC:
23492       case TYPE_SYNC:
23493       case TYPE_LOAD_L:
23494       case TYPE_STORE_C:
23495         return true;
23496       default:
23497         break;
23498     }
23499     break;
23500   case PROCESSOR_POWER7:
23501     type = get_attr_type (insn);
23502
23503     switch (type)
23504       {
23505       case TYPE_ISYNC:
23506       case TYPE_SYNC:
23507       case TYPE_LOAD_L:
23508       case TYPE_STORE_C:
23509       case TYPE_LOAD_EXT_U:
23510       case TYPE_LOAD_EXT_UX:
23511       case TYPE_STORE_UX:
23512         return true;
23513       default:
23514         break;
23515     }
23516     break;
23517   default:
23518     break;
23519   }
23520
23521   return false;
23522 }
23523
23524 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
23525    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
23526
23527 static bool
23528 is_costly_group (rtx *group_insns, rtx next_insn)
23529 {
23530   int i;
23531   int issue_rate = rs6000_issue_rate ();
23532
23533   for (i = 0; i < issue_rate; i++)
23534     {
23535       sd_iterator_def sd_it;
23536       dep_t dep;
23537       rtx insn = group_insns[i];
23538
23539       if (!insn)
23540         continue;
23541
23542       FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
23543         {
23544           rtx next = DEP_CON (dep);
23545
23546           if (next == next_insn
23547               && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
23548             return true;
23549         }
23550     }
23551
23552   return false;
23553 }
23554
23555 /* Utility of the function redefine_groups.
23556    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
23557    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
23558    to keep it "far" (in a separate group) from GROUP_INSNS, following
23559    one of the following schemes, depending on the value of the flag
23560    -minsert_sched_nops = X:
23561    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
23562        in order to force NEXT_INSN into a separate group.
23563    (2) X < sched_finish_regroup_exact: insert exactly X nops.
23564    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
23565    insertion (has a group just ended, how many vacant issue slots remain in the
23566    last group, and how many dispatch groups were encountered so far).  */
23567
23568 static int
23569 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
23570                  rtx next_insn, bool *group_end, int can_issue_more,
23571                  int *group_count)
23572 {
23573   rtx nop;
23574   bool force;
23575   int issue_rate = rs6000_issue_rate ();
23576   bool end = *group_end;
23577   int i;
23578
23579   if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
23580     return can_issue_more;
23581
23582   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
23583     return can_issue_more;
23584
23585   force = is_costly_group (group_insns, next_insn);
23586   if (!force)
23587     return can_issue_more;
23588
23589   if (sched_verbose > 6)
23590     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
23591              *group_count ,can_issue_more);
23592
23593   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
23594     {
23595       if (*group_end)
23596         can_issue_more = 0;
23597
23598       /* Since only a branch can be issued in the last issue_slot, it is
23599          sufficient to insert 'can_issue_more - 1' nops if next_insn is not
23600          a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
23601          in this case the last nop will start a new group and the branch
23602          will be forced to the new group.  */
23603       if (can_issue_more && !is_branch_slot_insn (next_insn))
23604         can_issue_more--;
23605
23606       while (can_issue_more > 0)
23607         {
23608           nop = gen_nop ();
23609           emit_insn_before (nop, next_insn);
23610           can_issue_more--;
23611         }
23612
23613       *group_end = true;
23614       return 0;
23615     }
23616
23617   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
23618     {
23619       int n_nops = rs6000_sched_insert_nops;
23620
23621       /* Nops can't be issued from the branch slot, so the effective
23622          issue_rate for nops is 'issue_rate - 1'.  */
23623       if (can_issue_more == 0)
23624         can_issue_more = issue_rate;
23625       can_issue_more--;
23626       if (can_issue_more == 0)
23627         {
23628           can_issue_more = issue_rate - 1;
23629           (*group_count)++;
23630           end = true;
23631           for (i = 0; i < issue_rate; i++)
23632             {
23633               group_insns[i] = 0;
23634             }
23635         }
23636
23637       while (n_nops > 0)
23638         {
23639           nop = gen_nop ();
23640           emit_insn_before (nop, next_insn);
23641           if (can_issue_more == issue_rate - 1) /* new group begins */
23642             end = false;
23643           can_issue_more--;
23644           if (can_issue_more == 0)
23645             {
23646               can_issue_more = issue_rate - 1;
23647               (*group_count)++;
23648               end = true;
23649               for (i = 0; i < issue_rate; i++)
23650                 {
23651                   group_insns[i] = 0;
23652                 }
23653             }
23654           n_nops--;
23655         }
23656
23657       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
23658       can_issue_more++;
23659
23660       /* Is next_insn going to start a new group?  */
23661       *group_end
23662         = (end
23663            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
23664            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
23665            || (can_issue_more < issue_rate &&
23666                insn_terminates_group_p (next_insn, previous_group)));
23667       if (*group_end && end)
23668         (*group_count)--;
23669
23670       if (sched_verbose > 6)
23671         fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
23672                  *group_count, can_issue_more);
23673       return can_issue_more;
23674     }
23675
23676   return can_issue_more;
23677 }
23678
23679 /* This function tries to synch the dispatch groups that the compiler "sees"
23680    with the dispatch groups that the processor dispatcher is expected to
23681    form in practice.  It tries to achieve this synchronization by forcing the
23682    estimated processor grouping on the compiler (as opposed to the function
23683    'pad_goups' which tries to force the scheduler's grouping on the processor).
23684
23685    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
23686    examines the (estimated) dispatch groups that will be formed by the processor
23687    dispatcher.  It marks these group boundaries to reflect the estimated
23688    processor grouping, overriding the grouping that the scheduler had marked.
23689    Depending on the value of the flag '-minsert-sched-nops' this function can
23690    force certain insns into separate groups or force a certain distance between
23691    them by inserting nops, for example, if there exists a "costly dependence"
23692    between the insns.
23693
23694    The function estimates the group boundaries that the processor will form as
23695    follows:  It keeps track of how many vacant issue slots are available after
23696    each insn.  A subsequent insn will start a new group if one of the following
23697    4 cases applies:
23698    - no more vacant issue slots remain in the current dispatch group.
23699    - only the last issue slot, which is the branch slot, is vacant, but the next
23700      insn is not a branch.
23701    - only the last 2 or less issue slots, including the branch slot, are vacant,
23702      which means that a cracked insn (which occupies two issue slots) can't be
23703      issued in this group.
23704    - less than 'issue_rate' slots are vacant, and the next insn always needs to
23705      start a new group.  */
23706
23707 static int
23708 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
23709 {
23710   rtx insn, next_insn;
23711   int issue_rate;
23712   int can_issue_more;
23713   int slot, i;
23714   bool group_end;
23715   int group_count = 0;
23716   rtx *group_insns;
23717
23718   /* Initialize.  */
23719   issue_rate = rs6000_issue_rate ();
23720   group_insns = XALLOCAVEC (rtx, issue_rate);
23721   for (i = 0; i < issue_rate; i++)
23722     {
23723       group_insns[i] = 0;
23724     }
23725   can_issue_more = issue_rate;
23726   slot = 0;
23727   insn = get_next_active_insn (prev_head_insn, tail);
23728   group_end = false;
23729
23730   while (insn != NULL_RTX)
23731     {
23732       slot = (issue_rate - can_issue_more);
23733       group_insns[slot] = insn;
23734       can_issue_more =
23735         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
23736       if (insn_terminates_group_p (insn, current_group))
23737         can_issue_more = 0;
23738
23739       next_insn = get_next_active_insn (insn, tail);
23740       if (next_insn == NULL_RTX)
23741         return group_count + 1;
23742
23743       /* Is next_insn going to start a new group?  */
23744       group_end
23745         = (can_issue_more == 0
23746            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
23747            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
23748            || (can_issue_more < issue_rate &&
23749                insn_terminates_group_p (next_insn, previous_group)));
23750
23751       can_issue_more = force_new_group (sched_verbose, dump, group_insns,
23752                                         next_insn, &group_end, can_issue_more,
23753                                         &group_count);
23754
23755       if (group_end)
23756         {
23757           group_count++;
23758           can_issue_more = 0;
23759           for (i = 0; i < issue_rate; i++)
23760             {
23761               group_insns[i] = 0;
23762             }
23763         }
23764
23765       if (GET_MODE (next_insn) == TImode && can_issue_more)
23766         PUT_MODE (next_insn, VOIDmode);
23767       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
23768         PUT_MODE (next_insn, TImode);
23769
23770       insn = next_insn;
23771       if (can_issue_more == 0)
23772         can_issue_more = issue_rate;
23773     } /* while */
23774
23775   return group_count;
23776 }
23777
23778 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
23779    dispatch group boundaries that the scheduler had marked.  Pad with nops
23780    any dispatch groups which have vacant issue slots, in order to force the
23781    scheduler's grouping on the processor dispatcher.  The function
23782    returns the number of dispatch groups found.  */
23783
23784 static int
23785 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
23786 {
23787   rtx insn, next_insn;
23788   rtx nop;
23789   int issue_rate;
23790   int can_issue_more;
23791   int group_end;
23792   int group_count = 0;
23793
23794   /* Initialize issue_rate.  */
23795   issue_rate = rs6000_issue_rate ();
23796   can_issue_more = issue_rate;
23797
23798   insn = get_next_active_insn (prev_head_insn, tail);
23799   next_insn = get_next_active_insn (insn, tail);
23800
23801   while (insn != NULL_RTX)
23802     {
23803       can_issue_more =
23804         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
23805
23806       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
23807
23808       if (next_insn == NULL_RTX)
23809         break;
23810
23811       if (group_end)
23812         {
23813           /* If the scheduler had marked group termination at this location
23814              (between insn and next_insn), and neither insn nor next_insn will
23815              force group termination, pad the group with nops to force group
23816              termination.  */
23817           if (can_issue_more
23818               && (rs6000_sched_insert_nops == sched_finish_pad_groups)
23819               && !insn_terminates_group_p (insn, current_group)
23820               && !insn_terminates_group_p (next_insn, previous_group))
23821             {
23822               if (!is_branch_slot_insn (next_insn))
23823                 can_issue_more--;
23824
23825               while (can_issue_more)
23826                 {
23827                   nop = gen_nop ();
23828                   emit_insn_before (nop, next_insn);
23829                   can_issue_more--;
23830                 }
23831             }
23832
23833           can_issue_more = issue_rate;
23834           group_count++;
23835         }
23836
23837       insn = next_insn;
23838       next_insn = get_next_active_insn (insn, tail);
23839     }
23840
23841   return group_count;
23842 }
23843
23844 /* We're beginning a new block.  Initialize data structures as necessary.  */
23845
23846 static void
23847 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
23848                      int sched_verbose ATTRIBUTE_UNUSED,
23849                      int max_ready ATTRIBUTE_UNUSED)
23850 {
23851   last_scheduled_insn = NULL_RTX;
23852   load_store_pendulum = 0;
23853 }
23854
23855 /* The following function is called at the end of scheduling BB.
23856    After reload, it inserts nops at insn group bundling.  */
23857
23858 static void
23859 rs6000_sched_finish (FILE *dump, int sched_verbose)
23860 {
23861   int n_groups;
23862
23863   if (sched_verbose)
23864     fprintf (dump, "=== Finishing schedule.\n");
23865
23866   if (reload_completed && rs6000_sched_groups)
23867     {
23868       /* Do not run sched_finish hook when selective scheduling enabled.  */
23869       if (sel_sched_p ())
23870         return;
23871
23872       if (rs6000_sched_insert_nops == sched_finish_none)
23873         return;
23874
23875       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
23876         n_groups = pad_groups (dump, sched_verbose,
23877                                current_sched_info->prev_head,
23878                                current_sched_info->next_tail);
23879       else
23880         n_groups = redefine_groups (dump, sched_verbose,
23881                                     current_sched_info->prev_head,
23882                                     current_sched_info->next_tail);
23883
23884       if (sched_verbose >= 6)
23885         {
23886           fprintf (dump, "ngroups = %d\n", n_groups);
23887           print_rtl (dump, current_sched_info->prev_head);
23888           fprintf (dump, "Done finish_sched\n");
23889         }
23890     }
23891 }
23892
23893 struct _rs6000_sched_context
23894 {
23895   short cached_can_issue_more;
23896   rtx last_scheduled_insn;
23897   int load_store_pendulum;
23898 };
23899
23900 typedef struct _rs6000_sched_context rs6000_sched_context_def;
23901 typedef rs6000_sched_context_def *rs6000_sched_context_t;
23902
23903 /* Allocate store for new scheduling context.  */
23904 static void *
23905 rs6000_alloc_sched_context (void)
23906 {
23907   return xmalloc (sizeof (rs6000_sched_context_def));
23908 }
23909
23910 /* If CLEAN_P is true then initializes _SC with clean data,
23911    and from the global context otherwise.  */
23912 static void
23913 rs6000_init_sched_context (void *_sc, bool clean_p)
23914 {
23915   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
23916
23917   if (clean_p)
23918     {
23919       sc->cached_can_issue_more = 0;
23920       sc->last_scheduled_insn = NULL_RTX;
23921       sc->load_store_pendulum = 0;
23922     }
23923   else
23924     {
23925       sc->cached_can_issue_more = cached_can_issue_more;
23926       sc->last_scheduled_insn = last_scheduled_insn;
23927       sc->load_store_pendulum = load_store_pendulum;
23928     }
23929 }
23930
23931 /* Sets the global scheduling context to the one pointed to by _SC.  */
23932 static void
23933 rs6000_set_sched_context (void *_sc)
23934 {
23935   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
23936
23937   gcc_assert (sc != NULL);
23938
23939   cached_can_issue_more = sc->cached_can_issue_more;
23940   last_scheduled_insn = sc->last_scheduled_insn;
23941   load_store_pendulum = sc->load_store_pendulum;
23942 }
23943
23944 /* Free _SC.  */
23945 static void
23946 rs6000_free_sched_context (void *_sc)
23947 {
23948   gcc_assert (_sc != NULL);
23949
23950   free (_sc);
23951 }
23952
23953 \f
23954 /* Length in units of the trampoline for entering a nested function.  */
23955
23956 int
23957 rs6000_trampoline_size (void)
23958 {
23959   int ret = 0;
23960
23961   switch (DEFAULT_ABI)
23962     {
23963     default:
23964       gcc_unreachable ();
23965
23966     case ABI_AIX:
23967       ret = (TARGET_32BIT) ? 12 : 24;
23968       break;
23969
23970     case ABI_DARWIN:
23971     case ABI_V4:
23972       ret = (TARGET_32BIT) ? 40 : 48;
23973       break;
23974     }
23975
23976   return ret;
23977 }
23978
23979 /* Emit RTL insns to initialize the variable parts of a trampoline.
23980    FNADDR is an RTX for the address of the function's pure code.
23981    CXT is an RTX for the static chain value for the function.  */
23982
23983 static void
23984 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
23985 {
23986   int regsize = (TARGET_32BIT) ? 4 : 8;
23987   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
23988   rtx ctx_reg = force_reg (Pmode, cxt);
23989   rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
23990
23991   switch (DEFAULT_ABI)
23992     {
23993     default:
23994       gcc_unreachable ();
23995
23996     /* Under AIX, just build the 3 word function descriptor */
23997     case ABI_AIX:
23998       {
23999         rtx fnmem, fn_reg, toc_reg;
24000
24001         if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
24002           error ("You cannot take the address of a nested function if you use "
24003                  "the -mno-pointers-to-nested-functions option.");
24004
24005         fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
24006         fn_reg = gen_reg_rtx (Pmode);
24007         toc_reg = gen_reg_rtx (Pmode);
24008
24009   /* Macro to shorten the code expansions below.  */
24010 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
24011
24012         m_tramp = replace_equiv_address (m_tramp, addr);
24013
24014         emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
24015         emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
24016         emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
24017         emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
24018         emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
24019
24020 # undef MEM_PLUS
24021       }
24022       break;
24023
24024     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
24025     case ABI_DARWIN:
24026     case ABI_V4:
24027       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
24028                          LCT_NORMAL, VOIDmode, 4,
24029                          addr, Pmode,
24030                          GEN_INT (rs6000_trampoline_size ()), SImode,
24031                          fnaddr, Pmode,
24032                          ctx_reg, Pmode);
24033       break;
24034     }
24035 }
24036
24037 \f
24038 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
24039    identifier as an argument, so the front end shouldn't look it up.  */
24040
24041 static bool
24042 rs6000_attribute_takes_identifier_p (const_tree attr_id)
24043 {
24044   return is_attribute_p ("altivec", attr_id);
24045 }
24046
24047 /* Handle the "altivec" attribute.  The attribute may have
24048    arguments as follows:
24049
24050         __attribute__((altivec(vector__)))
24051         __attribute__((altivec(pixel__)))       (always followed by 'unsigned short')
24052         __attribute__((altivec(bool__)))        (always followed by 'unsigned')
24053
24054   and may appear more than once (e.g., 'vector bool char') in a
24055   given declaration.  */
24056
24057 static tree
24058 rs6000_handle_altivec_attribute (tree *node,
24059                                  tree name ATTRIBUTE_UNUSED,
24060                                  tree args,
24061                                  int flags ATTRIBUTE_UNUSED,
24062                                  bool *no_add_attrs)
24063 {
24064   tree type = *node, result = NULL_TREE;
24065   enum machine_mode mode;
24066   int unsigned_p;
24067   char altivec_type
24068     = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
24069         && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
24070        ? *IDENTIFIER_POINTER (TREE_VALUE (args))
24071        : '?');
24072
24073   while (POINTER_TYPE_P (type)
24074          || TREE_CODE (type) == FUNCTION_TYPE
24075          || TREE_CODE (type) == METHOD_TYPE
24076          || TREE_CODE (type) == ARRAY_TYPE)
24077     type = TREE_TYPE (type);
24078
24079   mode = TYPE_MODE (type);
24080
24081   /* Check for invalid AltiVec type qualifiers.  */
24082   if (type == long_double_type_node)
24083     error ("use of %<long double%> in AltiVec types is invalid");
24084   else if (type == boolean_type_node)
24085     error ("use of boolean types in AltiVec types is invalid");
24086   else if (TREE_CODE (type) == COMPLEX_TYPE)
24087     error ("use of %<complex%> in AltiVec types is invalid");
24088   else if (DECIMAL_FLOAT_MODE_P (mode))
24089     error ("use of decimal floating point types in AltiVec types is invalid");
24090   else if (!TARGET_VSX)
24091     {
24092       if (type == long_unsigned_type_node || type == long_integer_type_node)
24093         {
24094           if (TARGET_64BIT)
24095             error ("use of %<long%> in AltiVec types is invalid for "
24096                    "64-bit code without -mvsx");
24097           else if (rs6000_warn_altivec_long)
24098             warning (0, "use of %<long%> in AltiVec types is deprecated; "
24099                      "use %<int%>");
24100         }
24101       else if (type == long_long_unsigned_type_node
24102                || type == long_long_integer_type_node)
24103         error ("use of %<long long%> in AltiVec types is invalid without "
24104                "-mvsx");
24105       else if (type == double_type_node)
24106         error ("use of %<double%> in AltiVec types is invalid without -mvsx");
24107     }
24108
24109   switch (altivec_type)
24110     {
24111     case 'v':
24112       unsigned_p = TYPE_UNSIGNED (type);
24113       switch (mode)
24114         {
24115         case DImode:
24116           result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
24117           break;
24118         case SImode:
24119           result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
24120           break;
24121         case HImode:
24122           result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
24123           break;
24124         case QImode:
24125           result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
24126           break;
24127         case SFmode: result = V4SF_type_node; break;
24128         case DFmode: result = V2DF_type_node; break;
24129           /* If the user says 'vector int bool', we may be handed the 'bool'
24130              attribute _before_ the 'vector' attribute, and so select the
24131              proper type in the 'b' case below.  */
24132         case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
24133         case V2DImode: case V2DFmode:
24134           result = type;
24135         default: break;
24136         }
24137       break;
24138     case 'b':
24139       switch (mode)
24140         {
24141         case DImode: case V2DImode: result = bool_V2DI_type_node; break;
24142         case SImode: case V4SImode: result = bool_V4SI_type_node; break;
24143         case HImode: case V8HImode: result = bool_V8HI_type_node; break;
24144         case QImode: case V16QImode: result = bool_V16QI_type_node;
24145         default: break;
24146         }
24147       break;
24148     case 'p':
24149       switch (mode)
24150         {
24151         case V8HImode: result = pixel_V8HI_type_node;
24152         default: break;
24153         }
24154     default: break;
24155     }
24156
24157   /* Propagate qualifiers attached to the element type
24158      onto the vector type.  */
24159   if (result && result != type && TYPE_QUALS (type))
24160     result = build_qualified_type (result, TYPE_QUALS (type));
24161
24162   *no_add_attrs = true;  /* No need to hang on to the attribute.  */
24163
24164   if (result)
24165     *node = lang_hooks.types.reconstruct_complex_type (*node, result);
24166
24167   return NULL_TREE;
24168 }
24169
24170 /* AltiVec defines four built-in scalar types that serve as vector
24171    elements; we must teach the compiler how to mangle them.  */
24172
24173 static const char *
24174 rs6000_mangle_type (const_tree type)
24175 {
24176   type = TYPE_MAIN_VARIANT (type);
24177
24178   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
24179       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
24180     return NULL;
24181
24182   if (type == bool_char_type_node) return "U6__boolc";
24183   if (type == bool_short_type_node) return "U6__bools";
24184   if (type == pixel_type_node) return "u7__pixel";
24185   if (type == bool_int_type_node) return "U6__booli";
24186   if (type == bool_long_type_node) return "U6__booll";
24187
24188   /* Mangle IBM extended float long double as `g' (__float128) on
24189      powerpc*-linux where long-double-64 previously was the default.  */
24190   if (TYPE_MAIN_VARIANT (type) == long_double_type_node
24191       && TARGET_ELF
24192       && TARGET_LONG_DOUBLE_128
24193       && !TARGET_IEEEQUAD)
24194     return "g";
24195
24196   /* For all other types, use normal C++ mangling.  */
24197   return NULL;
24198 }
24199
24200 /* Handle a "longcall" or "shortcall" attribute; arguments as in
24201    struct attribute_spec.handler.  */
24202
24203 static tree
24204 rs6000_handle_longcall_attribute (tree *node, tree name,
24205                                   tree args ATTRIBUTE_UNUSED,
24206                                   int flags ATTRIBUTE_UNUSED,
24207                                   bool *no_add_attrs)
24208 {
24209   if (TREE_CODE (*node) != FUNCTION_TYPE
24210       && TREE_CODE (*node) != FIELD_DECL
24211       && TREE_CODE (*node) != TYPE_DECL)
24212     {
24213       warning (OPT_Wattributes, "%qE attribute only applies to functions",
24214                name);
24215       *no_add_attrs = true;
24216     }
24217
24218   return NULL_TREE;
24219 }
24220
24221 /* Set longcall attributes on all functions declared when
24222    rs6000_default_long_calls is true.  */
24223 static void
24224 rs6000_set_default_type_attributes (tree type)
24225 {
24226   if (rs6000_default_long_calls
24227       && (TREE_CODE (type) == FUNCTION_TYPE
24228           || TREE_CODE (type) == METHOD_TYPE))
24229     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
24230                                         NULL_TREE,
24231                                         TYPE_ATTRIBUTES (type));
24232
24233 #if TARGET_MACHO
24234   darwin_set_default_type_attributes (type);
24235 #endif
24236 }
24237
24238 /* Return a reference suitable for calling a function with the
24239    longcall attribute.  */
24240
24241 rtx
24242 rs6000_longcall_ref (rtx call_ref)
24243 {
24244   const char *call_name;
24245   tree node;
24246
24247   if (GET_CODE (call_ref) != SYMBOL_REF)
24248     return call_ref;
24249
24250   /* System V adds '.' to the internal name, so skip them.  */
24251   call_name = XSTR (call_ref, 0);
24252   if (*call_name == '.')
24253     {
24254       while (*call_name == '.')
24255         call_name++;
24256
24257       node = get_identifier (call_name);
24258       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
24259     }
24260
24261   return force_reg (Pmode, call_ref);
24262 }
24263 \f
24264 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
24265 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
24266 #endif
24267
24268 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
24269    struct attribute_spec.handler.  */
24270 static tree
24271 rs6000_handle_struct_attribute (tree *node, tree name,
24272                                 tree args ATTRIBUTE_UNUSED,
24273                                 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
24274 {
24275   tree *type = NULL;
24276   if (DECL_P (*node))
24277     {
24278       if (TREE_CODE (*node) == TYPE_DECL)
24279         type = &TREE_TYPE (*node);
24280     }
24281   else
24282     type = node;
24283
24284   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
24285                  || TREE_CODE (*type) == UNION_TYPE)))
24286     {
24287       warning (OPT_Wattributes, "%qE attribute ignored", name);
24288       *no_add_attrs = true;
24289     }
24290
24291   else if ((is_attribute_p ("ms_struct", name)
24292             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
24293            || ((is_attribute_p ("gcc_struct", name)
24294                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
24295     {
24296       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
24297                name);
24298       *no_add_attrs = true;
24299     }
24300
24301   return NULL_TREE;
24302 }
24303
24304 static bool
24305 rs6000_ms_bitfield_layout_p (const_tree record_type)
24306 {
24307   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
24308           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
24309     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
24310 }
24311 \f
24312 #ifdef USING_ELFOS_H
24313
24314 /* A get_unnamed_section callback, used for switching to toc_section.  */
24315
24316 static void
24317 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
24318 {
24319   if (DEFAULT_ABI == ABI_AIX
24320       && TARGET_MINIMAL_TOC
24321       && !TARGET_RELOCATABLE)
24322     {
24323       if (!toc_initialized)
24324         {
24325           toc_initialized = 1;
24326           fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24327           (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
24328           fprintf (asm_out_file, "\t.tc ");
24329           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
24330           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24331           fprintf (asm_out_file, "\n");
24332
24333           fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24334           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24335           fprintf (asm_out_file, " = .+32768\n");
24336         }
24337       else
24338         fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24339     }
24340   else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
24341     fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24342   else
24343     {
24344       fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24345       if (!toc_initialized)
24346         {
24347           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24348           fprintf (asm_out_file, " = .+32768\n");
24349           toc_initialized = 1;
24350         }
24351     }
24352 }
24353
24354 /* Implement TARGET_ASM_INIT_SECTIONS.  */
24355
24356 static void
24357 rs6000_elf_asm_init_sections (void)
24358 {
24359   toc_section
24360     = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
24361
24362   sdata2_section
24363     = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
24364                            SDATA2_SECTION_ASM_OP);
24365 }
24366
24367 /* Implement TARGET_SELECT_RTX_SECTION.  */
24368
24369 static section *
24370 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
24371                                unsigned HOST_WIDE_INT align)
24372 {
24373   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
24374     return toc_section;
24375   else
24376     return default_elf_select_rtx_section (mode, x, align);
24377 }
24378 \f
24379 /* For a SYMBOL_REF, set generic flags and then perform some
24380    target-specific processing.
24381
24382    When the AIX ABI is requested on a non-AIX system, replace the
24383    function name with the real name (with a leading .) rather than the
24384    function descriptor name.  This saves a lot of overriding code to
24385    read the prefixes.  */
24386
24387 static void
24388 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
24389 {
24390   default_encode_section_info (decl, rtl, first);
24391
24392   if (first
24393       && TREE_CODE (decl) == FUNCTION_DECL
24394       && !TARGET_AIX
24395       && DEFAULT_ABI == ABI_AIX)
24396     {
24397       rtx sym_ref = XEXP (rtl, 0);
24398       size_t len = strlen (XSTR (sym_ref, 0));
24399       char *str = XALLOCAVEC (char, len + 2);
24400       str[0] = '.';
24401       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
24402       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
24403     }
24404 }
24405
24406 static inline bool
24407 compare_section_name (const char *section, const char *templ)
24408 {
24409   int len;
24410
24411   len = strlen (templ);
24412   return (strncmp (section, templ, len) == 0
24413           && (section[len] == 0 || section[len] == '.'));
24414 }
24415
24416 bool
24417 rs6000_elf_in_small_data_p (const_tree decl)
24418 {
24419   if (rs6000_sdata == SDATA_NONE)
24420     return false;
24421
24422   /* We want to merge strings, so we never consider them small data.  */
24423   if (TREE_CODE (decl) == STRING_CST)
24424     return false;
24425
24426   /* Functions are never in the small data area.  */
24427   if (TREE_CODE (decl) == FUNCTION_DECL)
24428     return false;
24429
24430   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
24431     {
24432       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
24433       if (compare_section_name (section, ".sdata")
24434           || compare_section_name (section, ".sdata2")
24435           || compare_section_name (section, ".gnu.linkonce.s")
24436           || compare_section_name (section, ".sbss")
24437           || compare_section_name (section, ".sbss2")
24438           || compare_section_name (section, ".gnu.linkonce.sb")
24439           || strcmp (section, ".PPC.EMB.sdata0") == 0
24440           || strcmp (section, ".PPC.EMB.sbss0") == 0)
24441         return true;
24442     }
24443   else
24444     {
24445       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
24446
24447       if (size > 0
24448           && size <= g_switch_value
24449           /* If it's not public, and we're not going to reference it there,
24450              there's no need to put it in the small data section.  */
24451           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
24452         return true;
24453     }
24454
24455   return false;
24456 }
24457
24458 #endif /* USING_ELFOS_H */
24459 \f
24460 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  */
24461
24462 static bool
24463 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
24464 {
24465   return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
24466 }
24467 \f
24468 /* Return a REG that occurs in ADDR with coefficient 1.
24469    ADDR can be effectively incremented by incrementing REG.
24470
24471    r0 is special and we must not select it as an address
24472    register by this routine since our caller will try to
24473    increment the returned register via an "la" instruction.  */
24474
24475 rtx
24476 find_addr_reg (rtx addr)
24477 {
24478   while (GET_CODE (addr) == PLUS)
24479     {
24480       if (GET_CODE (XEXP (addr, 0)) == REG
24481           && REGNO (XEXP (addr, 0)) != 0)
24482         addr = XEXP (addr, 0);
24483       else if (GET_CODE (XEXP (addr, 1)) == REG
24484                && REGNO (XEXP (addr, 1)) != 0)
24485         addr = XEXP (addr, 1);
24486       else if (CONSTANT_P (XEXP (addr, 0)))
24487         addr = XEXP (addr, 1);
24488       else if (CONSTANT_P (XEXP (addr, 1)))
24489         addr = XEXP (addr, 0);
24490       else
24491         gcc_unreachable ();
24492     }
24493   gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
24494   return addr;
24495 }
24496
24497 void
24498 rs6000_fatal_bad_address (rtx op)
24499 {
24500   fatal_insn ("bad address", op);
24501 }
24502
24503 #if TARGET_MACHO
24504
24505 typedef struct branch_island_d {
24506   tree function_name;
24507   tree label_name;
24508   int line_number;
24509 } branch_island;
24510
24511 DEF_VEC_O(branch_island);
24512 DEF_VEC_ALLOC_O(branch_island,gc);
24513
24514 static VEC(branch_island,gc) *branch_islands;
24515
24516 /* Remember to generate a branch island for far calls to the given
24517    function.  */
24518
24519 static void
24520 add_compiler_branch_island (tree label_name, tree function_name,
24521                             int line_number)
24522 {
24523   branch_island *bi = VEC_safe_push (branch_island, gc, branch_islands, NULL);
24524
24525   bi->function_name = function_name;
24526   bi->label_name = label_name;
24527   bi->line_number = line_number;
24528 }
24529
24530 /* Generate far-jump branch islands for everything recorded in
24531    branch_islands.  Invoked immediately after the last instruction of
24532    the epilogue has been emitted; the branch islands must be appended
24533    to, and contiguous with, the function body.  Mach-O stubs are
24534    generated in machopic_output_stub().  */
24535
24536 static void
24537 macho_branch_islands (void)
24538 {
24539   char tmp_buf[512];
24540
24541   while (!VEC_empty (branch_island, branch_islands))
24542     {
24543       branch_island *bi = VEC_last (branch_island, branch_islands);
24544       const char *label = IDENTIFIER_POINTER (bi->label_name);
24545       const char *name = IDENTIFIER_POINTER (bi->function_name);
24546       char name_buf[512];
24547       /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF().  */
24548       if (name[0] == '*' || name[0] == '&')
24549         strcpy (name_buf, name+1);
24550       else
24551         {
24552           name_buf[0] = '_';
24553           strcpy (name_buf+1, name);
24554         }
24555       strcpy (tmp_buf, "\n");
24556       strcat (tmp_buf, label);
24557 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24558       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24559         dbxout_stabd (N_SLINE, bi->line_number);
24560 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24561       if (flag_pic)
24562         {
24563           if (TARGET_LINK_STACK)
24564             {
24565               char name[32];
24566               get_ppc476_thunk_name (name);
24567               strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
24568               strcat (tmp_buf, name);
24569               strcat (tmp_buf, "\n");
24570               strcat (tmp_buf, label);
24571               strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24572             }
24573           else
24574             {
24575               strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
24576               strcat (tmp_buf, label);
24577               strcat (tmp_buf, "_pic\n");
24578               strcat (tmp_buf, label);
24579               strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24580             }
24581
24582           strcat (tmp_buf, "\taddis r11,r11,ha16(");
24583           strcat (tmp_buf, name_buf);
24584           strcat (tmp_buf, " - ");
24585           strcat (tmp_buf, label);
24586           strcat (tmp_buf, "_pic)\n");
24587
24588           strcat (tmp_buf, "\tmtlr r0\n");
24589
24590           strcat (tmp_buf, "\taddi r12,r11,lo16(");
24591           strcat (tmp_buf, name_buf);
24592           strcat (tmp_buf, " - ");
24593           strcat (tmp_buf, label);
24594           strcat (tmp_buf, "_pic)\n");
24595
24596           strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
24597         }
24598       else
24599         {
24600           strcat (tmp_buf, ":\nlis r12,hi16(");
24601           strcat (tmp_buf, name_buf);
24602           strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
24603           strcat (tmp_buf, name_buf);
24604           strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
24605         }
24606       output_asm_insn (tmp_buf, 0);
24607 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24608       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24609         dbxout_stabd (N_SLINE, bi->line_number);
24610 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24611       VEC_pop (branch_island, branch_islands);
24612     }
24613 }
24614
24615 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
24616    already there or not.  */
24617
24618 static int
24619 no_previous_def (tree function_name)
24620 {
24621   branch_island *bi;
24622   unsigned ix;
24623
24624   FOR_EACH_VEC_ELT (branch_island, branch_islands, ix, bi)
24625     if (function_name == bi->function_name)
24626       return 0;
24627   return 1;
24628 }
24629
24630 /* GET_PREV_LABEL gets the label name from the previous definition of
24631    the function.  */
24632
24633 static tree
24634 get_prev_label (tree function_name)
24635 {
24636   branch_island *bi;
24637   unsigned ix;
24638
24639   FOR_EACH_VEC_ELT (branch_island, branch_islands, ix, bi)
24640     if (function_name == bi->function_name)
24641       return bi->label_name;
24642   return NULL_TREE;
24643 }
24644
24645 /* INSN is either a function call or a millicode call.  It may have an
24646    unconditional jump in its delay slot.
24647
24648    CALL_DEST is the routine we are calling.  */
24649
24650 char *
24651 output_call (rtx insn, rtx *operands, int dest_operand_number,
24652              int cookie_operand_number)
24653 {
24654   static char buf[256];
24655   if (darwin_emit_branch_islands
24656       && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
24657       && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
24658     {
24659       tree labelname;
24660       tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
24661
24662       if (no_previous_def (funname))
24663         {
24664           rtx label_rtx = gen_label_rtx ();
24665           char *label_buf, temp_buf[256];
24666           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
24667                                        CODE_LABEL_NUMBER (label_rtx));
24668           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
24669           labelname = get_identifier (label_buf);
24670           add_compiler_branch_island (labelname, funname, insn_line (insn));
24671         }
24672       else
24673         labelname = get_prev_label (funname);
24674
24675       /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
24676          instruction will reach 'foo', otherwise link as 'bl L42'".
24677          "L42" should be a 'branch island', that will do a far jump to
24678          'foo'.  Branch islands are generated in
24679          macho_branch_islands().  */
24680       sprintf (buf, "jbsr %%z%d,%.246s",
24681                dest_operand_number, IDENTIFIER_POINTER (labelname));
24682     }
24683   else
24684     sprintf (buf, "bl %%z%d", dest_operand_number);
24685   return buf;
24686 }
24687
24688 /* Generate PIC and indirect symbol stubs.  */
24689
24690 void
24691 machopic_output_stub (FILE *file, const char *symb, const char *stub)
24692 {
24693   unsigned int length;
24694   char *symbol_name, *lazy_ptr_name;
24695   char *local_label_0;
24696   static int label = 0;
24697
24698   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
24699   symb = (*targetm.strip_name_encoding) (symb);
24700
24701
24702   length = strlen (symb);
24703   symbol_name = XALLOCAVEC (char, length + 32);
24704   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
24705
24706   lazy_ptr_name = XALLOCAVEC (char, length + 32);
24707   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
24708
24709   if (flag_pic == 2)
24710     switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
24711   else
24712     switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
24713
24714   if (flag_pic == 2)
24715     {
24716       fprintf (file, "\t.align 5\n");
24717
24718       fprintf (file, "%s:\n", stub);
24719       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
24720
24721       label++;
24722       local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
24723       sprintf (local_label_0, "\"L%011d$spb\"", label);
24724
24725       fprintf (file, "\tmflr r0\n");
24726       if (TARGET_LINK_STACK)
24727         {
24728           char name[32];
24729           get_ppc476_thunk_name (name);
24730           fprintf (file, "\tbl %s\n", name);
24731           fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
24732         }
24733       else
24734         {
24735           fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
24736           fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
24737         }
24738       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
24739                lazy_ptr_name, local_label_0);
24740       fprintf (file, "\tmtlr r0\n");
24741       fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
24742                (TARGET_64BIT ? "ldu" : "lwzu"),
24743                lazy_ptr_name, local_label_0);
24744       fprintf (file, "\tmtctr r12\n");
24745       fprintf (file, "\tbctr\n");
24746     }
24747   else
24748     {
24749       fprintf (file, "\t.align 4\n");
24750
24751       fprintf (file, "%s:\n", stub);
24752       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
24753
24754       fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
24755       fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
24756                (TARGET_64BIT ? "ldu" : "lwzu"),
24757                lazy_ptr_name);
24758       fprintf (file, "\tmtctr r12\n");
24759       fprintf (file, "\tbctr\n");
24760     }
24761
24762   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
24763   fprintf (file, "%s:\n", lazy_ptr_name);
24764   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
24765   fprintf (file, "%sdyld_stub_binding_helper\n",
24766            (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
24767 }
24768
24769 /* Legitimize PIC addresses.  If the address is already
24770    position-independent, we return ORIG.  Newly generated
24771    position-independent addresses go into a reg.  This is REG if non
24772    zero, otherwise we allocate register(s) as necessary.  */
24773
24774 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
24775
24776 rtx
24777 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
24778                                         rtx reg)
24779 {
24780   rtx base, offset;
24781
24782   if (reg == NULL && ! reload_in_progress && ! reload_completed)
24783     reg = gen_reg_rtx (Pmode);
24784
24785   if (GET_CODE (orig) == CONST)
24786     {
24787       rtx reg_temp;
24788
24789       if (GET_CODE (XEXP (orig, 0)) == PLUS
24790           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
24791         return orig;
24792
24793       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
24794
24795       /* Use a different reg for the intermediate value, as
24796          it will be marked UNCHANGING.  */
24797       reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
24798       base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
24799                                                      Pmode, reg_temp);
24800       offset =
24801         rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
24802                                                 Pmode, reg);
24803
24804       if (GET_CODE (offset) == CONST_INT)
24805         {
24806           if (SMALL_INT (offset))
24807             return plus_constant (base, INTVAL (offset));
24808           else if (! reload_in_progress && ! reload_completed)
24809             offset = force_reg (Pmode, offset);
24810           else
24811             {
24812               rtx mem = force_const_mem (Pmode, orig);
24813               return machopic_legitimize_pic_address (mem, Pmode, reg);
24814             }
24815         }
24816       return gen_rtx_PLUS (Pmode, base, offset);
24817     }
24818
24819   /* Fall back on generic machopic code.  */
24820   return machopic_legitimize_pic_address (orig, mode, reg);
24821 }
24822
24823 /* Output a .machine directive for the Darwin assembler, and call
24824    the generic start_file routine.  */
24825
24826 static void
24827 rs6000_darwin_file_start (void)
24828 {
24829   static const struct
24830   {
24831     const char *arg;
24832     const char *name;
24833     int if_set;
24834   } mapping[] = {
24835     { "ppc64", "ppc64", MASK_64BIT },
24836     { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
24837     { "power4", "ppc970", 0 },
24838     { "G5", "ppc970", 0 },
24839     { "7450", "ppc7450", 0 },
24840     { "7400", "ppc7400", MASK_ALTIVEC },
24841     { "G4", "ppc7400", 0 },
24842     { "750", "ppc750", 0 },
24843     { "740", "ppc750", 0 },
24844     { "G3", "ppc750", 0 },
24845     { "604e", "ppc604e", 0 },
24846     { "604", "ppc604", 0 },
24847     { "603e", "ppc603", 0 },
24848     { "603", "ppc603", 0 },
24849     { "601", "ppc601", 0 },
24850     { NULL, "ppc", 0 } };
24851   const char *cpu_id = "";
24852   size_t i;
24853
24854   rs6000_file_start ();
24855   darwin_file_start ();
24856
24857   /* Determine the argument to -mcpu=.  Default to G3 if not specified.  */
24858   
24859   if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
24860     cpu_id = rs6000_default_cpu;
24861
24862   if (global_options_set.x_rs6000_cpu_index)
24863     cpu_id = processor_target_table[rs6000_cpu_index].name;
24864
24865   /* Look through the mapping array.  Pick the first name that either
24866      matches the argument, has a bit set in IF_SET that is also set
24867      in the target flags, or has a NULL name.  */
24868
24869   i = 0;
24870   while (mapping[i].arg != NULL
24871          && strcmp (mapping[i].arg, cpu_id) != 0
24872          && (mapping[i].if_set & target_flags) == 0)
24873     i++;
24874
24875   fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
24876 }
24877
24878 #endif /* TARGET_MACHO */
24879
24880 #if TARGET_ELF
24881 static int
24882 rs6000_elf_reloc_rw_mask (void)
24883 {
24884   if (flag_pic)
24885     return 3;
24886   else if (DEFAULT_ABI == ABI_AIX)
24887     return 2;
24888   else
24889     return 0;
24890 }
24891
24892 /* Record an element in the table of global constructors.  SYMBOL is
24893    a SYMBOL_REF of the function to be called; PRIORITY is a number
24894    between 0 and MAX_INIT_PRIORITY.
24895
24896    This differs from default_named_section_asm_out_constructor in
24897    that we have special handling for -mrelocatable.  */
24898
24899 static void
24900 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
24901 {
24902   const char *section = ".ctors";
24903   char buf[16];
24904
24905   if (priority != DEFAULT_INIT_PRIORITY)
24906     {
24907       sprintf (buf, ".ctors.%.5u",
24908                /* Invert the numbering so the linker puts us in the proper
24909                   order; constructors are run from right to left, and the
24910                   linker sorts in increasing order.  */
24911                MAX_INIT_PRIORITY - priority);
24912       section = buf;
24913     }
24914
24915   switch_to_section (get_section (section, SECTION_WRITE, NULL));
24916   assemble_align (POINTER_SIZE);
24917
24918   if (TARGET_RELOCATABLE)
24919     {
24920       fputs ("\t.long (", asm_out_file);
24921       output_addr_const (asm_out_file, symbol);
24922       fputs (")@fixup\n", asm_out_file);
24923     }
24924   else
24925     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
24926 }
24927
24928 static void
24929 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
24930 {
24931   const char *section = ".dtors";
24932   char buf[16];
24933
24934   if (priority != DEFAULT_INIT_PRIORITY)
24935     {
24936       sprintf (buf, ".dtors.%.5u",
24937                /* Invert the numbering so the linker puts us in the proper
24938                   order; constructors are run from right to left, and the
24939                   linker sorts in increasing order.  */
24940                MAX_INIT_PRIORITY - priority);
24941       section = buf;
24942     }
24943
24944   switch_to_section (get_section (section, SECTION_WRITE, NULL));
24945   assemble_align (POINTER_SIZE);
24946
24947   if (TARGET_RELOCATABLE)
24948     {
24949       fputs ("\t.long (", asm_out_file);
24950       output_addr_const (asm_out_file, symbol);
24951       fputs (")@fixup\n", asm_out_file);
24952     }
24953   else
24954     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
24955 }
24956
24957 void
24958 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
24959 {
24960   if (TARGET_64BIT)
24961     {
24962       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
24963       ASM_OUTPUT_LABEL (file, name);
24964       fputs (DOUBLE_INT_ASM_OP, file);
24965       rs6000_output_function_entry (file, name);
24966       fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
24967       if (DOT_SYMBOLS)
24968         {
24969           fputs ("\t.size\t", file);
24970           assemble_name (file, name);
24971           fputs (",24\n\t.type\t.", file);
24972           assemble_name (file, name);
24973           fputs (",@function\n", file);
24974           if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
24975             {
24976               fputs ("\t.globl\t.", file);
24977               assemble_name (file, name);
24978               putc ('\n', file);
24979             }
24980         }
24981       else
24982         ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
24983       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
24984       rs6000_output_function_entry (file, name);
24985       fputs (":\n", file);
24986       return;
24987     }
24988
24989   if (TARGET_RELOCATABLE
24990       && !TARGET_SECURE_PLT
24991       && (get_pool_size () != 0 || crtl->profile)
24992       && uses_TOC ())
24993     {
24994       char buf[256];
24995
24996       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
24997
24998       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
24999       fprintf (file, "\t.long ");
25000       assemble_name (file, buf);
25001       putc ('-', file);
25002       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
25003       assemble_name (file, buf);
25004       putc ('\n', file);
25005     }
25006
25007   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25008   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25009
25010   if (DEFAULT_ABI == ABI_AIX)
25011     {
25012       const char *desc_name, *orig_name;
25013
25014       orig_name = (*targetm.strip_name_encoding) (name);
25015       desc_name = orig_name;
25016       while (*desc_name == '.')
25017         desc_name++;
25018
25019       if (TREE_PUBLIC (decl))
25020         fprintf (file, "\t.globl %s\n", desc_name);
25021
25022       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25023       fprintf (file, "%s:\n", desc_name);
25024       fprintf (file, "\t.long %s\n", orig_name);
25025       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
25026       if (DEFAULT_ABI == ABI_AIX)
25027         fputs ("\t.long 0\n", file);
25028       fprintf (file, "\t.previous\n");
25029     }
25030   ASM_OUTPUT_LABEL (file, name);
25031 }
25032
25033 static void
25034 rs6000_elf_file_end (void)
25035 {
25036 #ifdef HAVE_AS_GNU_ATTRIBUTE
25037   if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
25038     {
25039       if (rs6000_passes_float)
25040         fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
25041                  ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1 
25042                   : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3 
25043                   : 2));
25044       if (rs6000_passes_vector)
25045         fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
25046                  (TARGET_ALTIVEC_ABI ? 2
25047                   : TARGET_SPE_ABI ? 3
25048                   : 1));
25049       if (rs6000_returns_struct)
25050         fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
25051                  aix_struct_return ? 2 : 1);
25052     }
25053 #endif
25054 #ifdef POWERPC_LINUX
25055   if (TARGET_32BIT)
25056     file_end_indicate_exec_stack ();
25057 #endif
25058 }
25059 #endif
25060
25061 #if TARGET_XCOFF
25062 static void
25063 rs6000_xcoff_asm_output_anchor (rtx symbol)
25064 {
25065   char buffer[100];
25066
25067   sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
25068            SYMBOL_REF_BLOCK_OFFSET (symbol));
25069   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
25070 }
25071
25072 static void
25073 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
25074 {
25075   fputs (GLOBAL_ASM_OP, stream);
25076   RS6000_OUTPUT_BASENAME (stream, name);
25077   putc ('\n', stream);
25078 }
25079
25080 /* A get_unnamed_decl callback, used for read-only sections.  PTR
25081    points to the section string variable.  */
25082
25083 static void
25084 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
25085 {
25086   fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
25087            *(const char *const *) directive,
25088            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25089 }
25090
25091 /* Likewise for read-write sections.  */
25092
25093 static void
25094 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
25095 {
25096   fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
25097            *(const char *const *) directive,
25098            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25099 }
25100
25101 /* A get_unnamed_section callback, used for switching to toc_section.  */
25102
25103 static void
25104 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
25105 {
25106   if (TARGET_MINIMAL_TOC)
25107     {
25108       /* toc_section is always selected at least once from
25109          rs6000_xcoff_file_start, so this is guaranteed to
25110          always be defined once and only once in each file.  */
25111       if (!toc_initialized)
25112         {
25113           fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
25114           fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
25115           toc_initialized = 1;
25116         }
25117       fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
25118                (TARGET_32BIT ? "" : ",3"));
25119     }
25120   else
25121     fputs ("\t.toc\n", asm_out_file);
25122 }
25123
25124 /* Implement TARGET_ASM_INIT_SECTIONS.  */
25125
25126 static void
25127 rs6000_xcoff_asm_init_sections (void)
25128 {
25129   read_only_data_section
25130     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25131                            &xcoff_read_only_section_name);
25132
25133   private_data_section
25134     = get_unnamed_section (SECTION_WRITE,
25135                            rs6000_xcoff_output_readwrite_section_asm_op,
25136                            &xcoff_private_data_section_name);
25137
25138   read_only_private_data_section
25139     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25140                            &xcoff_private_data_section_name);
25141
25142   toc_section
25143     = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
25144
25145   readonly_data_section = read_only_data_section;
25146   exception_section = data_section;
25147 }
25148
25149 static int
25150 rs6000_xcoff_reloc_rw_mask (void)
25151 {
25152   return 3;
25153 }
25154
25155 static void
25156 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
25157                                 tree decl ATTRIBUTE_UNUSED)
25158 {
25159   int smclass;
25160   static const char * const suffix[3] = { "PR", "RO", "RW" };
25161
25162   if (flags & SECTION_CODE)
25163     smclass = 0;
25164   else if (flags & SECTION_WRITE)
25165     smclass = 2;
25166   else
25167     smclass = 1;
25168
25169   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
25170            (flags & SECTION_CODE) ? "." : "",
25171            name, suffix[smclass], flags & SECTION_ENTSIZE);
25172 }
25173
25174 static section *
25175 rs6000_xcoff_select_section (tree decl, int reloc,
25176                              unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25177 {
25178   if (decl_readonly_section (decl, reloc))
25179     {
25180       if (TREE_PUBLIC (decl))
25181         return read_only_data_section;
25182       else
25183         return read_only_private_data_section;
25184     }
25185   else
25186     {
25187       if (TREE_PUBLIC (decl))
25188         return data_section;
25189       else
25190         return private_data_section;
25191     }
25192 }
25193
25194 static void
25195 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
25196 {
25197   const char *name;
25198
25199   /* Use select_section for private and uninitialized data.  */
25200   if (!TREE_PUBLIC (decl)
25201       || DECL_COMMON (decl)
25202       || DECL_INITIAL (decl) == NULL_TREE
25203       || DECL_INITIAL (decl) == error_mark_node
25204       || (flag_zero_initialized_in_bss
25205           && initializer_zerop (DECL_INITIAL (decl))))
25206     return;
25207
25208   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
25209   name = (*targetm.strip_name_encoding) (name);
25210   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
25211 }
25212
25213 /* Select section for constant in constant pool.
25214
25215    On RS/6000, all constants are in the private read-only data area.
25216    However, if this is being placed in the TOC it must be output as a
25217    toc entry.  */
25218
25219 static section *
25220 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
25221                                  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25222 {
25223   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
25224     return toc_section;
25225   else
25226     return read_only_private_data_section;
25227 }
25228
25229 /* Remove any trailing [DS] or the like from the symbol name.  */
25230
25231 static const char *
25232 rs6000_xcoff_strip_name_encoding (const char *name)
25233 {
25234   size_t len;
25235   if (*name == '*')
25236     name++;
25237   len = strlen (name);
25238   if (name[len - 1] == ']')
25239     return ggc_alloc_string (name, len - 4);
25240   else
25241     return name;
25242 }
25243
25244 /* Section attributes.  AIX is always PIC.  */
25245
25246 static unsigned int
25247 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
25248 {
25249   unsigned int align;
25250   unsigned int flags = default_section_type_flags (decl, name, reloc);
25251
25252   /* Align to at least UNIT size.  */
25253   if (flags & SECTION_CODE || !decl)
25254     align = MIN_UNITS_PER_WORD;
25255   else
25256     /* Increase alignment of large objects if not already stricter.  */
25257     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
25258                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
25259                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
25260
25261   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
25262 }
25263
25264 /* Output at beginning of assembler file.
25265
25266    Initialize the section names for the RS/6000 at this point.
25267
25268    Specify filename, including full path, to assembler.
25269
25270    We want to go into the TOC section so at least one .toc will be emitted.
25271    Also, in order to output proper .bs/.es pairs, we need at least one static
25272    [RW] section emitted.
25273
25274    Finally, declare mcount when profiling to make the assembler happy.  */
25275
25276 static void
25277 rs6000_xcoff_file_start (void)
25278 {
25279   rs6000_gen_section_name (&xcoff_bss_section_name,
25280                            main_input_filename, ".bss_");
25281   rs6000_gen_section_name (&xcoff_private_data_section_name,
25282                            main_input_filename, ".rw_");
25283   rs6000_gen_section_name (&xcoff_read_only_section_name,
25284                            main_input_filename, ".ro_");
25285
25286   fputs ("\t.file\t", asm_out_file);
25287   output_quoted_string (asm_out_file, main_input_filename);
25288   fputc ('\n', asm_out_file);
25289   if (write_symbols != NO_DEBUG)
25290     switch_to_section (private_data_section);
25291   switch_to_section (text_section);
25292   if (profile_flag)
25293     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
25294   rs6000_file_start ();
25295 }
25296
25297 /* Output at end of assembler file.
25298    On the RS/6000, referencing data should automatically pull in text.  */
25299
25300 static void
25301 rs6000_xcoff_file_end (void)
25302 {
25303   switch_to_section (text_section);
25304   fputs ("_section_.text:\n", asm_out_file);
25305   switch_to_section (data_section);
25306   fputs (TARGET_32BIT
25307          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
25308          asm_out_file);
25309 }
25310 #endif /* TARGET_XCOFF */
25311
25312 /* Compute a (partial) cost for rtx X.  Return true if the complete
25313    cost has been computed, and false if subexpressions should be
25314    scanned.  In either case, *TOTAL contains the cost result.  */
25315
25316 static bool
25317 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
25318                   int *total, bool speed)
25319 {
25320   enum machine_mode mode = GET_MODE (x);
25321
25322   switch (code)
25323     {
25324       /* On the RS/6000, if it is valid in the insn, it is free.  */
25325     case CONST_INT:
25326       if (((outer_code == SET
25327             || outer_code == PLUS
25328             || outer_code == MINUS)
25329            && (satisfies_constraint_I (x)
25330                || satisfies_constraint_L (x)))
25331           || (outer_code == AND
25332               && (satisfies_constraint_K (x)
25333                   || (mode == SImode
25334                       ? satisfies_constraint_L (x)
25335                       : satisfies_constraint_J (x))
25336                   || mask_operand (x, mode)
25337                   || (mode == DImode
25338                       && mask64_operand (x, DImode))))
25339           || ((outer_code == IOR || outer_code == XOR)
25340               && (satisfies_constraint_K (x)
25341                   || (mode == SImode
25342                       ? satisfies_constraint_L (x)
25343                       : satisfies_constraint_J (x))))
25344           || outer_code == ASHIFT
25345           || outer_code == ASHIFTRT
25346           || outer_code == LSHIFTRT
25347           || outer_code == ROTATE
25348           || outer_code == ROTATERT
25349           || outer_code == ZERO_EXTRACT
25350           || (outer_code == MULT
25351               && satisfies_constraint_I (x))
25352           || ((outer_code == DIV || outer_code == UDIV
25353                || outer_code == MOD || outer_code == UMOD)
25354               && exact_log2 (INTVAL (x)) >= 0)
25355           || (outer_code == COMPARE
25356               && (satisfies_constraint_I (x)
25357                   || satisfies_constraint_K (x)))
25358           || ((outer_code == EQ || outer_code == NE)
25359               && (satisfies_constraint_I (x)
25360                   || satisfies_constraint_K (x)
25361                   || (mode == SImode
25362                       ? satisfies_constraint_L (x)
25363                       : satisfies_constraint_J (x))))
25364           || (outer_code == GTU
25365               && satisfies_constraint_I (x))
25366           || (outer_code == LTU
25367               && satisfies_constraint_P (x)))
25368         {
25369           *total = 0;
25370           return true;
25371         }
25372       else if ((outer_code == PLUS
25373                 && reg_or_add_cint_operand (x, VOIDmode))
25374                || (outer_code == MINUS
25375                    && reg_or_sub_cint_operand (x, VOIDmode))
25376                || ((outer_code == SET
25377                     || outer_code == IOR
25378                     || outer_code == XOR)
25379                    && (INTVAL (x)
25380                        & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
25381         {
25382           *total = COSTS_N_INSNS (1);
25383           return true;
25384         }
25385       /* FALLTHRU */
25386
25387     case CONST_DOUBLE:
25388       if (mode == DImode && code == CONST_DOUBLE)
25389         {
25390           if ((outer_code == IOR || outer_code == XOR)
25391               && CONST_DOUBLE_HIGH (x) == 0
25392               && (CONST_DOUBLE_LOW (x)
25393                   & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0)
25394             {
25395               *total = 0;
25396               return true;
25397             }
25398           else if ((outer_code == AND && and64_2_operand (x, DImode))
25399                    || ((outer_code == SET
25400                         || outer_code == IOR
25401                         || outer_code == XOR)
25402                        && CONST_DOUBLE_HIGH (x) == 0))
25403             {
25404               *total = COSTS_N_INSNS (1);
25405               return true;
25406             }
25407         }
25408       /* FALLTHRU */
25409
25410     case CONST:
25411     case HIGH:
25412     case SYMBOL_REF:
25413     case MEM:
25414       /* When optimizing for size, MEM should be slightly more expensive
25415          than generating address, e.g., (plus (reg) (const)).
25416          L1 cache latency is about two instructions.  */
25417       *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
25418       return true;
25419
25420     case LABEL_REF:
25421       *total = 0;
25422       return true;
25423
25424     case PLUS:
25425     case MINUS:
25426       if (FLOAT_MODE_P (mode))
25427         *total = rs6000_cost->fp;
25428       else
25429         *total = COSTS_N_INSNS (1);
25430       return false;
25431
25432     case MULT:
25433       if (GET_CODE (XEXP (x, 1)) == CONST_INT
25434           && satisfies_constraint_I (XEXP (x, 1)))
25435         {
25436           if (INTVAL (XEXP (x, 1)) >= -256
25437               && INTVAL (XEXP (x, 1)) <= 255)
25438             *total = rs6000_cost->mulsi_const9;
25439           else
25440             *total = rs6000_cost->mulsi_const;
25441         }
25442       else if (mode == SFmode)
25443         *total = rs6000_cost->fp;
25444       else if (FLOAT_MODE_P (mode))
25445         *total = rs6000_cost->dmul;
25446       else if (mode == DImode)
25447         *total = rs6000_cost->muldi;
25448       else
25449         *total = rs6000_cost->mulsi;
25450       return false;
25451
25452     case FMA:
25453       if (mode == SFmode)
25454         *total = rs6000_cost->fp;
25455       else
25456         *total = rs6000_cost->dmul;
25457       break;
25458
25459     case DIV:
25460     case MOD:
25461       if (FLOAT_MODE_P (mode))
25462         {
25463           *total = mode == DFmode ? rs6000_cost->ddiv
25464                                   : rs6000_cost->sdiv;
25465           return false;
25466         }
25467       /* FALLTHRU */
25468
25469     case UDIV:
25470     case UMOD:
25471       if (GET_CODE (XEXP (x, 1)) == CONST_INT
25472           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
25473         {
25474           if (code == DIV || code == MOD)
25475             /* Shift, addze */
25476             *total = COSTS_N_INSNS (2);
25477           else
25478             /* Shift */
25479             *total = COSTS_N_INSNS (1);
25480         }
25481       else
25482         {
25483           if (GET_MODE (XEXP (x, 1)) == DImode)
25484             *total = rs6000_cost->divdi;
25485           else
25486             *total = rs6000_cost->divsi;
25487         }
25488       /* Add in shift and subtract for MOD. */
25489       if (code == MOD || code == UMOD)
25490         *total += COSTS_N_INSNS (2);
25491       return false;
25492
25493     case CTZ:
25494     case FFS:
25495       *total = COSTS_N_INSNS (4);
25496       return false;
25497
25498     case POPCOUNT:
25499       *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
25500       return false;
25501
25502     case PARITY:
25503       *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
25504       return false;
25505
25506     case NOT:
25507       if (outer_code == AND || outer_code == IOR || outer_code == XOR)
25508         {
25509           *total = 0;
25510           return false;
25511         }
25512       /* FALLTHRU */
25513
25514     case AND:
25515     case CLZ:
25516     case IOR:
25517     case XOR:
25518     case ZERO_EXTRACT:
25519       *total = COSTS_N_INSNS (1);
25520       return false;
25521
25522     case ASHIFT:
25523     case ASHIFTRT:
25524     case LSHIFTRT:
25525     case ROTATE:
25526     case ROTATERT:
25527       /* Handle mul_highpart.  */
25528       if (outer_code == TRUNCATE
25529           && GET_CODE (XEXP (x, 0)) == MULT)
25530         {
25531           if (mode == DImode)
25532             *total = rs6000_cost->muldi;
25533           else
25534             *total = rs6000_cost->mulsi;
25535           return true;
25536         }
25537       else if (outer_code == AND)
25538         *total = 0;
25539       else
25540         *total = COSTS_N_INSNS (1);
25541       return false;
25542
25543     case SIGN_EXTEND:
25544     case ZERO_EXTEND:
25545       if (GET_CODE (XEXP (x, 0)) == MEM)
25546         *total = 0;
25547       else
25548         *total = COSTS_N_INSNS (1);
25549       return false;
25550
25551     case COMPARE:
25552     case NEG:
25553     case ABS:
25554       if (!FLOAT_MODE_P (mode))
25555         {
25556           *total = COSTS_N_INSNS (1);
25557           return false;
25558         }
25559       /* FALLTHRU */
25560
25561     case FLOAT:
25562     case UNSIGNED_FLOAT:
25563     case FIX:
25564     case UNSIGNED_FIX:
25565     case FLOAT_TRUNCATE:
25566       *total = rs6000_cost->fp;
25567       return false;
25568
25569     case FLOAT_EXTEND:
25570       if (mode == DFmode)
25571         *total = 0;
25572       else
25573         *total = rs6000_cost->fp;
25574       return false;
25575
25576     case UNSPEC:
25577       switch (XINT (x, 1))
25578         {
25579         case UNSPEC_FRSP:
25580           *total = rs6000_cost->fp;
25581           return true;
25582
25583         default:
25584           break;
25585         }
25586       break;
25587
25588     case CALL:
25589     case IF_THEN_ELSE:
25590       if (!speed)
25591         {
25592           *total = COSTS_N_INSNS (1);
25593           return true;
25594         }
25595       else if (FLOAT_MODE_P (mode)
25596                && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
25597         {
25598           *total = rs6000_cost->fp;
25599           return false;
25600         }
25601       break;
25602
25603     case EQ:
25604     case GTU:
25605     case LTU:
25606       /* Carry bit requires mode == Pmode.
25607          NEG or PLUS already counted so only add one.  */
25608       if (mode == Pmode
25609           && (outer_code == NEG || outer_code == PLUS))
25610         {
25611           *total = COSTS_N_INSNS (1);
25612           return true;
25613         }
25614       if (outer_code == SET)
25615         {
25616           if (XEXP (x, 1) == const0_rtx)
25617             {
25618               if (TARGET_ISEL && !TARGET_MFCRF)
25619                 *total = COSTS_N_INSNS (8);
25620               else
25621                 *total = COSTS_N_INSNS (2);
25622               return true;
25623             }
25624           else if (mode == Pmode)
25625             {
25626               *total = COSTS_N_INSNS (3);
25627               return false;
25628             }
25629         }
25630       /* FALLTHRU */
25631
25632     case GT:
25633     case LT:
25634     case UNORDERED:
25635       if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
25636         {
25637           if (TARGET_ISEL && !TARGET_MFCRF)
25638             *total = COSTS_N_INSNS (8);
25639           else
25640             *total = COSTS_N_INSNS (2);
25641           return true;
25642         }
25643       /* CC COMPARE.  */
25644       if (outer_code == COMPARE)
25645         {
25646           *total = 0;
25647           return true;
25648         }
25649       break;
25650
25651     default:
25652       break;
25653     }
25654
25655   return false;
25656 }
25657
25658 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost.  */
25659
25660 static bool
25661 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
25662                         bool speed)
25663 {
25664   bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
25665
25666   fprintf (stderr,
25667            "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
25668            "opno = %d, total = %d, speed = %s, x:\n",
25669            ret ? "complete" : "scan inner",
25670            GET_RTX_NAME (code),
25671            GET_RTX_NAME (outer_code),
25672            opno,
25673            *total,
25674            speed ? "true" : "false");
25675
25676   debug_rtx (x);
25677
25678   return ret;
25679 }
25680
25681 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost.  */
25682
25683 static int
25684 rs6000_debug_address_cost (rtx x, bool speed)
25685 {
25686   int ret = TARGET_ADDRESS_COST (x, speed);
25687
25688   fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
25689            ret, speed ? "true" : "false");
25690   debug_rtx (x);
25691
25692   return ret;
25693 }
25694
25695
25696 /* A C expression returning the cost of moving data from a register of class
25697    CLASS1 to one of CLASS2.  */
25698
25699 static int
25700 rs6000_register_move_cost (enum machine_mode mode,
25701                            reg_class_t from, reg_class_t to)
25702 {
25703   int ret;
25704
25705   if (TARGET_DEBUG_COST)
25706     dbg_cost_ctrl++;
25707
25708   /*  Moves from/to GENERAL_REGS.  */
25709   if (reg_classes_intersect_p (to, GENERAL_REGS)
25710       || reg_classes_intersect_p (from, GENERAL_REGS))
25711     {
25712       reg_class_t rclass = from;
25713
25714       if (! reg_classes_intersect_p (to, GENERAL_REGS))
25715         rclass = to;
25716
25717       if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
25718         ret = (rs6000_memory_move_cost (mode, rclass, false)
25719                + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
25720
25721       /* It's more expensive to move CR_REGS than CR0_REGS because of the
25722          shift.  */
25723       else if (rclass == CR_REGS)
25724         ret = 4;
25725
25726       /* For those processors that have slow LR/CTR moves, make them more
25727          expensive than memory in order to bias spills to memory .*/
25728       else if ((rs6000_cpu == PROCESSOR_POWER6
25729                 || rs6000_cpu == PROCESSOR_POWER7)
25730                && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
25731         ret = 6 * hard_regno_nregs[0][mode];
25732
25733       else
25734         /* A move will cost one instruction per GPR moved.  */
25735         ret = 2 * hard_regno_nregs[0][mode];
25736     }
25737
25738   /* If we have VSX, we can easily move between FPR or Altivec registers.  */
25739   else if (VECTOR_UNIT_VSX_P (mode)
25740            && reg_classes_intersect_p (to, VSX_REGS)
25741            && reg_classes_intersect_p (from, VSX_REGS))
25742     ret = 2 * hard_regno_nregs[32][mode];
25743
25744   /* Moving between two similar registers is just one instruction.  */
25745   else if (reg_classes_intersect_p (to, from))
25746     ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
25747
25748   /* Everything else has to go through GENERAL_REGS.  */
25749   else
25750     ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
25751            + rs6000_register_move_cost (mode, from, GENERAL_REGS));
25752
25753   if (TARGET_DEBUG_COST)
25754     {
25755       if (dbg_cost_ctrl == 1)
25756         fprintf (stderr,
25757                  "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
25758                  ret, GET_MODE_NAME (mode), reg_class_names[from],
25759                  reg_class_names[to]);
25760       dbg_cost_ctrl--;
25761     }
25762
25763   return ret;
25764 }
25765
25766 /* A C expressions returning the cost of moving data of MODE from a register to
25767    or from memory.  */
25768
25769 static int
25770 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
25771                          bool in ATTRIBUTE_UNUSED)
25772 {
25773   int ret;
25774
25775   if (TARGET_DEBUG_COST)
25776     dbg_cost_ctrl++;
25777
25778   if (reg_classes_intersect_p (rclass, GENERAL_REGS))
25779     ret = 4 * hard_regno_nregs[0][mode];
25780   else if (reg_classes_intersect_p (rclass, FLOAT_REGS))
25781     ret = 4 * hard_regno_nregs[32][mode];
25782   else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
25783     ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
25784   else
25785     ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
25786
25787   if (TARGET_DEBUG_COST)
25788     {
25789       if (dbg_cost_ctrl == 1)
25790         fprintf (stderr,
25791                  "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
25792                  ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
25793       dbg_cost_ctrl--;
25794     }
25795
25796   return ret;
25797 }
25798
25799 /* Returns a code for a target-specific builtin that implements
25800    reciprocal of the function, or NULL_TREE if not available.  */
25801
25802 static tree
25803 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
25804                            bool sqrt ATTRIBUTE_UNUSED)
25805 {
25806   if (optimize_insn_for_size_p ())
25807     return NULL_TREE;
25808
25809   if (md_fn)
25810     switch (fn)
25811       {
25812       case VSX_BUILTIN_XVSQRTDP:
25813         if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
25814           return NULL_TREE;
25815
25816         return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
25817
25818       case VSX_BUILTIN_XVSQRTSP:
25819         if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
25820           return NULL_TREE;
25821
25822         return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
25823
25824       default:
25825         return NULL_TREE;
25826       }
25827
25828   else
25829     switch (fn)
25830       {
25831       case BUILT_IN_SQRT:
25832         if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
25833           return NULL_TREE;
25834
25835         return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
25836
25837       case BUILT_IN_SQRTF:
25838         if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
25839           return NULL_TREE;
25840
25841         return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
25842
25843       default:
25844         return NULL_TREE;
25845       }
25846 }
25847
25848 /* Load up a constant.  If the mode is a vector mode, splat the value across
25849    all of the vector elements.  */
25850
25851 static rtx
25852 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
25853 {
25854   rtx reg;
25855
25856   if (mode == SFmode || mode == DFmode)
25857     {
25858       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
25859       reg = force_reg (mode, d);
25860     }
25861   else if (mode == V4SFmode)
25862     {
25863       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
25864       rtvec v = gen_rtvec (4, d, d, d, d);
25865       reg = gen_reg_rtx (mode);
25866       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
25867     }
25868   else if (mode == V2DFmode)
25869     {
25870       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
25871       rtvec v = gen_rtvec (2, d, d);
25872       reg = gen_reg_rtx (mode);
25873       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
25874     }
25875   else
25876     gcc_unreachable ();
25877
25878   return reg;
25879 }
25880
25881 /* Generate an FMA instruction.  */
25882
25883 static void
25884 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
25885 {
25886   enum machine_mode mode = GET_MODE (target);
25887   rtx dst;
25888
25889   dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
25890   gcc_assert (dst != NULL);
25891
25892   if (dst != target)
25893     emit_move_insn (target, dst);
25894 }
25895
25896 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a).  */
25897
25898 static void
25899 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
25900 {
25901   enum machine_mode mode = GET_MODE (target);
25902   rtx dst;
25903
25904   /* Altivec does not support fms directly;
25905      generate in terms of fma in that case.  */
25906   if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
25907     dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
25908   else
25909     {
25910       a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
25911       dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
25912     }
25913   gcc_assert (dst != NULL);
25914
25915   if (dst != target)
25916     emit_move_insn (target, dst);
25917 }
25918     
25919 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a).  */
25920
25921 static void
25922 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
25923 {
25924   enum machine_mode mode = GET_MODE (dst);
25925   rtx r;
25926
25927   /* This is a tad more complicated, since the fnma_optab is for
25928      a different expression: fma(-m1, m2, a), which is the same
25929      thing except in the case of signed zeros.
25930
25931      Fortunately we know that if FMA is supported that FNMSUB is
25932      also supported in the ISA.  Just expand it directly.  */
25933
25934   gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
25935
25936   r = gen_rtx_NEG (mode, a);
25937   r = gen_rtx_FMA (mode, m1, m2, r);
25938   r = gen_rtx_NEG (mode, r);
25939   emit_insn (gen_rtx_SET (VOIDmode, dst, r));
25940 }
25941
25942 /* Newton-Raphson approximation of floating point divide with just 2 passes
25943    (either single precision floating point, or newer machines with higher
25944    accuracy estimates).  Support both scalar and vector divide.  Assumes no
25945    trapping math and finite arguments.  */
25946
25947 static void
25948 rs6000_emit_swdiv_high_precision (rtx dst, rtx n, rtx d)
25949 {
25950   enum machine_mode mode = GET_MODE (dst);
25951   rtx x0, e0, e1, y1, u0, v0;
25952   enum insn_code code = optab_handler (smul_optab, mode);
25953   gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
25954   rtx one = rs6000_load_constant_and_splat (mode, dconst1);
25955
25956   gcc_assert (code != CODE_FOR_nothing);
25957
25958   /* x0 = 1./d estimate */
25959   x0 = gen_reg_rtx (mode);
25960   emit_insn (gen_rtx_SET (VOIDmode, x0,
25961                           gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
25962                                           UNSPEC_FRES)));
25963
25964   e0 = gen_reg_rtx (mode);
25965   rs6000_emit_nmsub (e0, d, x0, one);           /* e0 = 1. - (d * x0) */
25966
25967   e1 = gen_reg_rtx (mode);
25968   rs6000_emit_madd (e1, e0, e0, e0);            /* e1 = (e0 * e0) + e0 */
25969
25970   y1 = gen_reg_rtx (mode);
25971   rs6000_emit_madd (y1, e1, x0, x0);            /* y1 = (e1 * x0) + x0 */
25972
25973   u0 = gen_reg_rtx (mode);
25974   emit_insn (gen_mul (u0, n, y1));              /* u0 = n * y1 */
25975
25976   v0 = gen_reg_rtx (mode);
25977   rs6000_emit_nmsub (v0, d, u0, n);             /* v0 = n - (d * u0) */
25978
25979   rs6000_emit_madd (dst, v0, y1, u0);           /* dst = (v0 * y1) + u0 */
25980 }
25981
25982 /* Newton-Raphson approximation of floating point divide that has a low
25983    precision estimate.  Assumes no trapping math and finite arguments.  */
25984
25985 static void
25986 rs6000_emit_swdiv_low_precision (rtx dst, rtx n, rtx d)
25987 {
25988   enum machine_mode mode = GET_MODE (dst);
25989   rtx x0, e0, e1, e2, y1, y2, y3, u0, v0, one;
25990   enum insn_code code = optab_handler (smul_optab, mode);
25991   gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
25992
25993   gcc_assert (code != CODE_FOR_nothing);
25994
25995   one = rs6000_load_constant_and_splat (mode, dconst1);
25996
25997   /* x0 = 1./d estimate */
25998   x0 = gen_reg_rtx (mode);
25999   emit_insn (gen_rtx_SET (VOIDmode, x0,
26000                           gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
26001                                           UNSPEC_FRES)));
26002
26003   e0 = gen_reg_rtx (mode);
26004   rs6000_emit_nmsub (e0, d, x0, one);           /* e0 = 1. - d * x0 */
26005
26006   y1 = gen_reg_rtx (mode);
26007   rs6000_emit_madd (y1, e0, x0, x0);            /* y1 = x0 + e0 * x0 */
26008
26009   e1 = gen_reg_rtx (mode);
26010   emit_insn (gen_mul (e1, e0, e0));             /* e1 = e0 * e0 */
26011
26012   y2 = gen_reg_rtx (mode);
26013   rs6000_emit_madd (y2, e1, y1, y1);            /* y2 = y1 + e1 * y1 */
26014
26015   e2 = gen_reg_rtx (mode);
26016   emit_insn (gen_mul (e2, e1, e1));             /* e2 = e1 * e1 */
26017
26018   y3 = gen_reg_rtx (mode);
26019   rs6000_emit_madd (y3, e2, y2, y2);            /* y3 = y2 + e2 * y2 */
26020
26021   u0 = gen_reg_rtx (mode);
26022   emit_insn (gen_mul (u0, n, y3));              /* u0 = n * y3 */
26023
26024   v0 = gen_reg_rtx (mode);
26025   rs6000_emit_nmsub (v0, d, u0, n);             /* v0 = n - d * u0 */
26026
26027   rs6000_emit_madd (dst, v0, y3, u0);           /* dst = u0 + v0 * y3 */
26028 }
26029
26030 /* Newton-Raphson approximation of floating point divide DST = N/D.  If NOTE_P,
26031    add a reg_note saying that this was a division.  Support both scalar and
26032    vector divide.  Assumes no trapping math and finite arguments.  */
26033
26034 void
26035 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
26036 {
26037   enum machine_mode mode = GET_MODE (dst);
26038
26039   if (RS6000_RECIP_HIGH_PRECISION_P (mode))
26040     rs6000_emit_swdiv_high_precision (dst, n, d);
26041   else
26042     rs6000_emit_swdiv_low_precision (dst, n, d);
26043
26044   if (note_p)
26045     add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
26046 }
26047
26048 /* Newton-Raphson approximation of single/double-precision floating point
26049    rsqrt.  Assumes no trapping math and finite arguments.  */
26050
26051 void
26052 rs6000_emit_swrsqrt (rtx dst, rtx src)
26053 {
26054   enum machine_mode mode = GET_MODE (src);
26055   rtx x0 = gen_reg_rtx (mode);
26056   rtx y = gen_reg_rtx (mode);
26057   int passes = (TARGET_RECIP_PRECISION) ? 2 : 3;
26058   REAL_VALUE_TYPE dconst3_2;
26059   int i;
26060   rtx halfthree;
26061   enum insn_code code = optab_handler (smul_optab, mode);
26062   gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26063
26064   gcc_assert (code != CODE_FOR_nothing);
26065
26066   /* Load up the constant 1.5 either as a scalar, or as a vector.  */
26067   real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
26068   SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
26069
26070   halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
26071
26072   /* x0 = rsqrt estimate */
26073   emit_insn (gen_rtx_SET (VOIDmode, x0,
26074                           gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
26075                                           UNSPEC_RSQRT)));
26076
26077   /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
26078   rs6000_emit_msub (y, src, halfthree, src);
26079
26080   for (i = 0; i < passes; i++)
26081     {
26082       rtx x1 = gen_reg_rtx (mode);
26083       rtx u = gen_reg_rtx (mode);
26084       rtx v = gen_reg_rtx (mode);
26085
26086       /* x1 = x0 * (1.5 - y * (x0 * x0)) */
26087       emit_insn (gen_mul (u, x0, x0));
26088       rs6000_emit_nmsub (v, y, u, halfthree);
26089       emit_insn (gen_mul (x1, x0, v));
26090       x0 = x1;
26091     }
26092
26093   emit_move_insn (dst, x0);
26094   return;
26095 }
26096
26097 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
26098    (Power7) targets.  DST is the target, and SRC is the argument operand.  */
26099
26100 void
26101 rs6000_emit_popcount (rtx dst, rtx src)
26102 {
26103   enum machine_mode mode = GET_MODE (dst);
26104   rtx tmp1, tmp2;
26105
26106   /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can.  */
26107   if (TARGET_POPCNTD)
26108     {
26109       if (mode == SImode)
26110         emit_insn (gen_popcntdsi2 (dst, src));
26111       else
26112         emit_insn (gen_popcntddi2 (dst, src));
26113       return;
26114     }
26115
26116   tmp1 = gen_reg_rtx (mode);
26117
26118   if (mode == SImode)
26119     {
26120       emit_insn (gen_popcntbsi2 (tmp1, src));
26121       tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
26122                            NULL_RTX, 0);
26123       tmp2 = force_reg (SImode, tmp2);
26124       emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
26125     }
26126   else
26127     {
26128       emit_insn (gen_popcntbdi2 (tmp1, src));
26129       tmp2 = expand_mult (DImode, tmp1,
26130                           GEN_INT ((HOST_WIDE_INT)
26131                                    0x01010101 << 32 | 0x01010101),
26132                           NULL_RTX, 0);
26133       tmp2 = force_reg (DImode, tmp2);
26134       emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
26135     }
26136 }
26137
26138
26139 /* Emit parity intrinsic on TARGET_POPCNTB targets.  DST is the
26140    target, and SRC is the argument operand.  */
26141
26142 void
26143 rs6000_emit_parity (rtx dst, rtx src)
26144 {
26145   enum machine_mode mode = GET_MODE (dst);
26146   rtx tmp;
26147
26148   tmp = gen_reg_rtx (mode);
26149
26150   /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can.  */
26151   if (TARGET_CMPB)
26152     {
26153       if (mode == SImode)
26154         {
26155           emit_insn (gen_popcntbsi2 (tmp, src));
26156           emit_insn (gen_paritysi2_cmpb (dst, tmp));
26157         }
26158       else
26159         {
26160           emit_insn (gen_popcntbdi2 (tmp, src));
26161           emit_insn (gen_paritydi2_cmpb (dst, tmp));
26162         }
26163       return;
26164     }
26165
26166   if (mode == SImode)
26167     {
26168       /* Is mult+shift >= shift+xor+shift+xor?  */
26169       if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
26170         {
26171           rtx tmp1, tmp2, tmp3, tmp4;
26172
26173           tmp1 = gen_reg_rtx (SImode);
26174           emit_insn (gen_popcntbsi2 (tmp1, src));
26175
26176           tmp2 = gen_reg_rtx (SImode);
26177           emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
26178           tmp3 = gen_reg_rtx (SImode);
26179           emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
26180
26181           tmp4 = gen_reg_rtx (SImode);
26182           emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
26183           emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
26184         }
26185       else
26186         rs6000_emit_popcount (tmp, src);
26187       emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
26188     }
26189   else
26190     {
26191       /* Is mult+shift >= shift+xor+shift+xor+shift+xor?  */
26192       if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
26193         {
26194           rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
26195
26196           tmp1 = gen_reg_rtx (DImode);
26197           emit_insn (gen_popcntbdi2 (tmp1, src));
26198
26199           tmp2 = gen_reg_rtx (DImode);
26200           emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
26201           tmp3 = gen_reg_rtx (DImode);
26202           emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
26203
26204           tmp4 = gen_reg_rtx (DImode);
26205           emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
26206           tmp5 = gen_reg_rtx (DImode);
26207           emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
26208
26209           tmp6 = gen_reg_rtx (DImode);
26210           emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
26211           emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
26212         }
26213       else
26214         rs6000_emit_popcount (tmp, src);
26215       emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
26216     }
26217 }
26218
26219 /* Expand an Altivec constant permutation.  Return true if we match
26220    an efficient implementation; false to fall back to VPERM.  */
26221
26222 bool
26223 altivec_expand_vec_perm_const (rtx operands[4])
26224 {
26225   struct altivec_perm_insn {
26226     enum insn_code impl;
26227     unsigned char perm[16];
26228   };
26229   static const struct altivec_perm_insn patterns[] = {
26230     { CODE_FOR_altivec_vpkuhum,
26231       {  1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
26232     { CODE_FOR_altivec_vpkuwum,
26233       {  2,  3,  6,  7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
26234     { CODE_FOR_altivec_vmrghb,
26235       {  0, 16,  1, 17,  2, 18,  3, 19,  4, 20,  5, 21,  6, 22,  7, 23 } },
26236     { CODE_FOR_altivec_vmrghh,
26237       {  0,  1, 16, 17,  2,  3, 18, 19,  4,  5, 20, 21,  6,  7, 22, 23 } },
26238     { CODE_FOR_altivec_vmrghw,
26239       {  0,  1,  2,  3, 16, 17, 18, 19,  4,  5,  6,  7, 20, 21, 22, 23 } },
26240     { CODE_FOR_altivec_vmrglb,
26241       {  8, 24,  9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
26242     { CODE_FOR_altivec_vmrglh,
26243       {  8,  9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
26244     { CODE_FOR_altivec_vmrglw,
26245       {  8,  9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }
26246   };
26247
26248   unsigned int i, j, elt, which;
26249   unsigned char perm[16];
26250   rtx target, op0, op1, sel, x;
26251   bool one_vec;
26252
26253   target = operands[0];
26254   op0 = operands[1];
26255   op1 = operands[2];
26256   sel = operands[3];
26257
26258   /* Unpack the constant selector.  */
26259   for (i = which = 0; i < 16; ++i)
26260     {
26261       rtx e = XVECEXP (sel, 0, i);
26262       elt = INTVAL (e) & 31;
26263       which |= (elt < 16 ? 1 : 2);
26264       perm[i] = elt;
26265     }
26266
26267   /* Simplify the constant selector based on operands.  */
26268   switch (which)
26269     {
26270     default:
26271       gcc_unreachable ();
26272
26273     case 3:
26274       one_vec = false;
26275       if (!rtx_equal_p (op0, op1))
26276         break;
26277       /* FALLTHRU */
26278
26279     case 2:
26280       for (i = 0; i < 16; ++i)
26281         perm[i] &= 15;
26282       op0 = op1;
26283       one_vec = true;
26284       break;
26285
26286     case 1:
26287       op1 = op0;
26288       one_vec = true;
26289       break;
26290     }
26291  
26292   /* Look for splat patterns.  */
26293   if (one_vec)
26294     {
26295       elt = perm[0];
26296
26297       for (i = 0; i < 16; ++i)
26298         if (perm[i] != elt)
26299           break;
26300       if (i == 16)
26301         {
26302           emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
26303           return true;
26304         }
26305
26306       if (elt % 2 == 0)
26307         {
26308           for (i = 0; i < 16; i += 2)
26309             if (perm[i] != elt || perm[i + 1] != elt + 1)
26310               break;
26311           if (i == 16)
26312             {
26313               x = gen_reg_rtx (V8HImode);
26314               emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
26315                                              GEN_INT (elt / 2)));
26316               emit_move_insn (target, gen_lowpart (V16QImode, x));
26317               return true;
26318             }
26319         }
26320
26321       if (elt % 4 == 0)
26322         {
26323           for (i = 0; i < 16; i += 4)
26324             if (perm[i] != elt
26325                 || perm[i + 1] != elt + 1
26326                 || perm[i + 2] != elt + 2
26327                 || perm[i + 3] != elt + 3)
26328               break;
26329           if (i == 16)
26330             {
26331               x = gen_reg_rtx (V4SImode);
26332               emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
26333                                              GEN_INT (elt / 4)));
26334               emit_move_insn (target, gen_lowpart (V16QImode, x));
26335               return true;
26336             }
26337         }
26338     }
26339
26340   /* Look for merge and pack patterns.  */
26341   for (j = 0; j < ARRAY_SIZE (patterns); ++j)
26342     {
26343       bool swapped;
26344
26345       elt = patterns[j].perm[0];
26346       if (perm[0] == elt)
26347         swapped = false;
26348       else if (perm[0] == elt + 16)
26349         swapped = true;
26350       else
26351         continue;
26352       for (i = 1; i < 16; ++i)
26353         {
26354           elt = patterns[j].perm[i];
26355           if (swapped)
26356             elt = (elt >= 16 ? elt - 16 : elt + 16);
26357           else if (one_vec && elt >= 16)
26358             elt -= 16;
26359           if (perm[i] != elt)
26360             break;
26361         }
26362       if (i == 16)
26363         {
26364           enum insn_code icode = patterns[j].impl;
26365           enum machine_mode omode = insn_data[icode].operand[0].mode;
26366           enum machine_mode imode = insn_data[icode].operand[1].mode;
26367
26368           if (swapped)
26369             x = op0, op0 = op1, op1 = x;
26370           if (imode != V16QImode)
26371             {
26372               op0 = gen_lowpart (imode, op0);
26373               op1 = gen_lowpart (imode, op1);
26374             }
26375           if (omode == V16QImode)
26376             x = target;
26377           else
26378             x = gen_reg_rtx (omode);
26379           emit_insn (GEN_FCN (icode) (x, op0, op1));
26380           if (omode != V16QImode)
26381             emit_move_insn (target, gen_lowpart (V16QImode, x));
26382           return true;
26383         }
26384     }
26385
26386   return false;
26387 }
26388
26389 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
26390    Return true if we match an efficient implementation.  */
26391
26392 static bool
26393 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
26394                                 unsigned char perm0, unsigned char perm1)
26395 {
26396   rtx x;
26397
26398   /* If both selectors come from the same operand, fold to single op.  */
26399   if ((perm0 & 2) == (perm1 & 2))
26400     {
26401       if (perm0 & 2)
26402         op0 = op1;
26403       else
26404         op1 = op0;
26405     }
26406   /* If both operands are equal, fold to simpler permutation.  */
26407   if (rtx_equal_p (op0, op1))
26408     {
26409       perm0 = perm0 & 1;
26410       perm1 = (perm1 & 1) + 2;
26411     }
26412   /* If the first selector comes from the second operand, swap.  */
26413   else if (perm0 & 2)
26414     {
26415       if (perm1 & 2)
26416         return false;
26417       perm0 -= 2;
26418       perm1 += 2;
26419       x = op0, op0 = op1, op1 = x;
26420     }
26421   /* If the second selector does not come from the second operand, fail.  */
26422   else if ((perm1 & 2) == 0)
26423     return false;
26424
26425   /* Success! */
26426   if (target != NULL)
26427     {
26428       enum machine_mode vmode, dmode;
26429       rtvec v;
26430
26431       vmode = GET_MODE (target);
26432       gcc_assert (GET_MODE_NUNITS (vmode) == 2);
26433       dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
26434
26435       x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
26436       v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
26437       x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
26438       emit_insn (gen_rtx_SET (VOIDmode, target, x));
26439     }
26440   return true;
26441 }
26442
26443 bool
26444 rs6000_expand_vec_perm_const (rtx operands[4])
26445 {
26446   rtx target, op0, op1, sel;
26447   unsigned char perm0, perm1;
26448
26449   target = operands[0];
26450   op0 = operands[1];
26451   op1 = operands[2];
26452   sel = operands[3];
26453
26454   /* Unpack the constant selector.  */
26455   perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
26456   perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
26457
26458   return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
26459 }
26460
26461 /* Test whether a constant permutation is supported.  */
26462
26463 static bool
26464 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
26465                                     const unsigned char *sel)
26466 {
26467   /* AltiVec (and thus VSX) can handle arbitrary permutations.  */
26468   if (TARGET_ALTIVEC)
26469     return true;
26470
26471   /* Check for ps_merge* or evmerge* insns.  */
26472   if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
26473       || (TARGET_SPE && vmode == V2SImode))
26474     {
26475       rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
26476       rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
26477       return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
26478     }
26479
26480   return false;
26481 }
26482
26483 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave.  */
26484
26485 static void
26486 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
26487                            enum machine_mode vmode, unsigned nelt, rtx perm[])
26488 {
26489   enum machine_mode imode;
26490   rtx x;
26491
26492   imode = vmode;
26493   if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
26494     {
26495       imode = GET_MODE_INNER (vmode);
26496       imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
26497       imode = mode_for_vector (imode, nelt);
26498     }
26499
26500   x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
26501   x = expand_vec_perm (vmode, op0, op1, x, target);
26502   if (x != target)
26503     emit_move_insn (target, x);
26504 }
26505
26506 /* Expand an extract even operation.  */
26507
26508 void
26509 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
26510 {
26511   enum machine_mode vmode = GET_MODE (target);
26512   unsigned i, nelt = GET_MODE_NUNITS (vmode);
26513   rtx perm[16];
26514
26515   for (i = 0; i < nelt; i++)
26516     perm[i] = GEN_INT (i * 2);
26517
26518   rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
26519 }
26520
26521 /* Expand a vector interleave operation.  */
26522
26523 void
26524 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
26525 {
26526   enum machine_mode vmode = GET_MODE (target);
26527   unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
26528   rtx perm[16];
26529
26530   high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
26531   for (i = 0; i < nelt / 2; i++)
26532     {
26533       perm[i * 2] = GEN_INT (i + high);
26534       perm[i * 2 + 1] = GEN_INT (i + nelt + high);
26535     }
26536
26537   rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
26538 }
26539
26540 /* Return an RTX representing where to find the function value of a
26541    function returning MODE.  */
26542 static rtx
26543 rs6000_complex_function_value (enum machine_mode mode)
26544 {
26545   unsigned int regno;
26546   rtx r1, r2;
26547   enum machine_mode inner = GET_MODE_INNER (mode);
26548   unsigned int inner_bytes = GET_MODE_SIZE (inner);
26549
26550   if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26551     regno = FP_ARG_RETURN;
26552   else
26553     {
26554       regno = GP_ARG_RETURN;
26555
26556       /* 32-bit is OK since it'll go in r3/r4.  */
26557       if (TARGET_32BIT && inner_bytes >= 4)
26558         return gen_rtx_REG (mode, regno);
26559     }
26560
26561   if (inner_bytes >= 8)
26562     return gen_rtx_REG (mode, regno);
26563
26564   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
26565                           const0_rtx);
26566   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
26567                           GEN_INT (inner_bytes));
26568   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
26569 }
26570
26571 /* Target hook for TARGET_FUNCTION_VALUE.
26572
26573    On the SPE, both FPs and vectors are returned in r3.
26574
26575    On RS/6000 an integer value is in r3 and a floating-point value is in
26576    fp1, unless -msoft-float.  */
26577
26578 rtx
26579 rs6000_function_value (const_tree valtype,
26580                        const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
26581                        bool outgoing ATTRIBUTE_UNUSED)
26582 {
26583   enum machine_mode mode;
26584   unsigned int regno;
26585
26586   /* Special handling for structs in darwin64.  */
26587   if (TARGET_MACHO 
26588       && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
26589     {
26590       CUMULATIVE_ARGS valcum;
26591       rtx valret;
26592
26593       valcum.words = 0;
26594       valcum.fregno = FP_ARG_MIN_REG;
26595       valcum.vregno = ALTIVEC_ARG_MIN_REG;
26596       /* Do a trial code generation as if this were going to be passed as
26597          an argument; if any part goes in memory, we return NULL.  */
26598       valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
26599       if (valret)
26600         return valret;
26601       /* Otherwise fall through to standard ABI rules.  */
26602     }
26603
26604   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
26605     {
26606       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
26607       return gen_rtx_PARALLEL (DImode,
26608         gen_rtvec (2,
26609                    gen_rtx_EXPR_LIST (VOIDmode,
26610                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
26611                                       const0_rtx),
26612                    gen_rtx_EXPR_LIST (VOIDmode,
26613                                       gen_rtx_REG (SImode,
26614                                                    GP_ARG_RETURN + 1),
26615                                       GEN_INT (4))));
26616     }
26617   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
26618     {
26619       return gen_rtx_PARALLEL (DCmode,
26620         gen_rtvec (4,
26621                    gen_rtx_EXPR_LIST (VOIDmode,
26622                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
26623                                       const0_rtx),
26624                    gen_rtx_EXPR_LIST (VOIDmode,
26625                                       gen_rtx_REG (SImode,
26626                                                    GP_ARG_RETURN + 1),
26627                                       GEN_INT (4)),
26628                    gen_rtx_EXPR_LIST (VOIDmode,
26629                                       gen_rtx_REG (SImode,
26630                                                    GP_ARG_RETURN + 2),
26631                                       GEN_INT (8)),
26632                    gen_rtx_EXPR_LIST (VOIDmode,
26633                                       gen_rtx_REG (SImode,
26634                                                    GP_ARG_RETURN + 3),
26635                                       GEN_INT (12))));
26636     }
26637
26638   mode = TYPE_MODE (valtype);
26639   if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
26640       || POINTER_TYPE_P (valtype))
26641     mode = TARGET_32BIT ? SImode : DImode;
26642
26643   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26644     /* _Decimal128 must use an even/odd register pair.  */
26645     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
26646   else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
26647            && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
26648     regno = FP_ARG_RETURN;
26649   else if (TREE_CODE (valtype) == COMPLEX_TYPE
26650            && targetm.calls.split_complex_arg)
26651     return rs6000_complex_function_value (mode);
26652   /* VSX is a superset of Altivec and adds V2DImode/V2DFmode.  Since the same
26653      return register is used in both cases, and we won't see V2DImode/V2DFmode
26654      for pure altivec, combine the two cases.  */
26655   else if (TREE_CODE (valtype) == VECTOR_TYPE
26656            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
26657            && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
26658     regno = ALTIVEC_ARG_RETURN;
26659   else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
26660            && (mode == DFmode || mode == DCmode
26661                || mode == TFmode || mode == TCmode))
26662     return spe_build_register_parallel (mode, GP_ARG_RETURN);
26663   else
26664     regno = GP_ARG_RETURN;
26665
26666   return gen_rtx_REG (mode, regno);
26667 }
26668
26669 /* Define how to find the value returned by a library function
26670    assuming the value has mode MODE.  */
26671 rtx
26672 rs6000_libcall_value (enum machine_mode mode)
26673 {
26674   unsigned int regno;
26675
26676   if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
26677     {
26678       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
26679       return gen_rtx_PARALLEL (DImode,
26680         gen_rtvec (2,
26681                    gen_rtx_EXPR_LIST (VOIDmode,
26682                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
26683                                       const0_rtx),
26684                    gen_rtx_EXPR_LIST (VOIDmode,
26685                                       gen_rtx_REG (SImode,
26686                                                    GP_ARG_RETURN + 1),
26687                                       GEN_INT (4))));
26688     }
26689
26690   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26691     /* _Decimal128 must use an even/odd register pair.  */
26692     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
26693   else if (SCALAR_FLOAT_MODE_P (mode)
26694            && TARGET_HARD_FLOAT && TARGET_FPRS
26695            && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
26696     regno = FP_ARG_RETURN;
26697   /* VSX is a superset of Altivec and adds V2DImode/V2DFmode.  Since the same
26698      return register is used in both cases, and we won't see V2DImode/V2DFmode
26699      for pure altivec, combine the two cases.  */
26700   else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
26701            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
26702     regno = ALTIVEC_ARG_RETURN;
26703   else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
26704     return rs6000_complex_function_value (mode);
26705   else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
26706            && (mode == DFmode || mode == DCmode
26707                || mode == TFmode || mode == TCmode))
26708     return spe_build_register_parallel (mode, GP_ARG_RETURN);
26709   else
26710     regno = GP_ARG_RETURN;
26711
26712   return gen_rtx_REG (mode, regno);
26713 }
26714
26715
26716 /* Given FROM and TO register numbers, say whether this elimination is allowed.
26717    Frame pointer elimination is automatically handled.
26718
26719    For the RS/6000, if frame pointer elimination is being done, we would like
26720    to convert ap into fp, not sp.
26721
26722    We need r30 if -mminimal-toc was specified, and there are constant pool
26723    references.  */
26724
26725 bool
26726 rs6000_can_eliminate (const int from, const int to)
26727 {
26728   return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
26729           ? ! frame_pointer_needed
26730           : from == RS6000_PIC_OFFSET_TABLE_REGNUM
26731             ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
26732             : true);
26733 }
26734
26735 /* Define the offset between two registers, FROM to be eliminated and its
26736    replacement TO, at the start of a routine.  */
26737 HOST_WIDE_INT
26738 rs6000_initial_elimination_offset (int from, int to)
26739 {
26740   rs6000_stack_t *info = rs6000_stack_info ();
26741   HOST_WIDE_INT offset;
26742
26743   if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
26744     offset = info->push_p ? 0 : -info->total_size;
26745   else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
26746     {
26747       offset = info->push_p ? 0 : -info->total_size;
26748       if (FRAME_GROWS_DOWNWARD)
26749         offset += info->fixed_size + info->vars_size + info->parm_size;
26750     }
26751   else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
26752     offset = FRAME_GROWS_DOWNWARD
26753              ? info->fixed_size + info->vars_size + info->parm_size
26754              : 0;
26755   else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
26756     offset = info->total_size;
26757   else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
26758     offset = info->push_p ? info->total_size : 0;
26759   else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
26760     offset = 0;
26761   else
26762     gcc_unreachable ();
26763
26764   return offset;
26765 }
26766
26767 static rtx
26768 rs6000_dwarf_register_span (rtx reg)
26769 {
26770   rtx parts[8];
26771   int i, words;
26772   unsigned regno = REGNO (reg);
26773   enum machine_mode mode = GET_MODE (reg);
26774
26775   if (TARGET_SPE
26776       && regno < 32
26777       && (SPE_VECTOR_MODE (GET_MODE (reg))
26778           || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
26779               && mode != SFmode && mode != SDmode && mode != SCmode)))
26780     ;
26781   else
26782     return NULL_RTX;
26783
26784   regno = REGNO (reg);
26785
26786   /* The duality of the SPE register size wreaks all kinds of havoc.
26787      This is a way of distinguishing r0 in 32-bits from r0 in
26788      64-bits.  */
26789   words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
26790   gcc_assert (words <= 4);
26791   for (i = 0; i < words; i++, regno++)
26792     {
26793       if (BYTES_BIG_ENDIAN)
26794         {
26795           parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
26796           parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
26797         }
26798       else
26799         {
26800           parts[2 * i] = gen_rtx_REG (SImode, regno);
26801           parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
26802         }
26803     }
26804
26805   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
26806 }
26807
26808 /* Fill in sizes for SPE register high parts in table used by unwinder.  */
26809
26810 static void
26811 rs6000_init_dwarf_reg_sizes_extra (tree address)
26812 {
26813   if (TARGET_SPE)
26814     {
26815       int i;
26816       enum machine_mode mode = TYPE_MODE (char_type_node);
26817       rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
26818       rtx mem = gen_rtx_MEM (BLKmode, addr);
26819       rtx value = gen_int_mode (4, mode);
26820
26821       for (i = 1201; i < 1232; i++)
26822         {
26823           int column = DWARF_REG_TO_UNWIND_COLUMN (i);
26824           HOST_WIDE_INT offset
26825             = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
26826
26827           emit_move_insn (adjust_address (mem, mode, offset), value);
26828         }
26829     }
26830 }
26831
26832 /* Map internal gcc register numbers to DWARF2 register numbers.  */
26833
26834 unsigned int
26835 rs6000_dbx_register_number (unsigned int regno)
26836 {
26837   if (regno <= 63 || write_symbols != DWARF2_DEBUG)
26838     return regno;
26839   if (regno == MQ_REGNO)
26840     return 100;
26841   if (regno == LR_REGNO)
26842     return 108;
26843   if (regno == CTR_REGNO)
26844     return 109;
26845   if (CR_REGNO_P (regno))
26846     return regno - CR0_REGNO + 86;
26847   if (regno == CA_REGNO)
26848     return 101;  /* XER */
26849   if (ALTIVEC_REGNO_P (regno))
26850     return regno - FIRST_ALTIVEC_REGNO + 1124;
26851   if (regno == VRSAVE_REGNO)
26852     return 356;
26853   if (regno == VSCR_REGNO)
26854     return 67;
26855   if (regno == SPE_ACC_REGNO)
26856     return 99;
26857   if (regno == SPEFSCR_REGNO)
26858     return 612;
26859   /* SPE high reg number.  We get these values of regno from
26860      rs6000_dwarf_register_span.  */
26861   gcc_assert (regno >= 1200 && regno < 1232);
26862   return regno;
26863 }
26864
26865 /* target hook eh_return_filter_mode */
26866 static enum machine_mode
26867 rs6000_eh_return_filter_mode (void)
26868 {
26869   return TARGET_32BIT ? SImode : word_mode;
26870 }
26871
26872 /* Target hook for scalar_mode_supported_p.  */
26873 static bool
26874 rs6000_scalar_mode_supported_p (enum machine_mode mode)
26875 {
26876   if (DECIMAL_FLOAT_MODE_P (mode))
26877     return default_decimal_float_supported_p ();
26878   else
26879     return default_scalar_mode_supported_p (mode);
26880 }
26881
26882 /* Target hook for vector_mode_supported_p.  */
26883 static bool
26884 rs6000_vector_mode_supported_p (enum machine_mode mode)
26885 {
26886
26887   if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
26888     return true;
26889
26890   if (TARGET_SPE && SPE_VECTOR_MODE (mode))
26891     return true;
26892
26893   else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
26894     return true;
26895
26896   else
26897     return false;
26898 }
26899
26900 /* Target hook for invalid_arg_for_unprototyped_fn. */
26901 static const char *
26902 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
26903 {
26904   return (!rs6000_darwin64_abi
26905           && typelist == 0
26906           && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
26907           && (funcdecl == NULL_TREE
26908               || (TREE_CODE (funcdecl) == FUNCTION_DECL
26909                   && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
26910           ? N_("AltiVec argument passed to unprototyped function")
26911           : NULL;
26912 }
26913
26914 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
26915    setup by using __stack_chk_fail_local hidden function instead of
26916    calling __stack_chk_fail directly.  Otherwise it is better to call
26917    __stack_chk_fail directly.  */
26918
26919 static tree ATTRIBUTE_UNUSED
26920 rs6000_stack_protect_fail (void)
26921 {
26922   return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
26923          ? default_hidden_stack_protect_fail ()
26924          : default_external_stack_protect_fail ();
26925 }
26926
26927 void
26928 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
26929                            int num_operands ATTRIBUTE_UNUSED)
26930 {
26931   if (rs6000_warn_cell_microcode)
26932     {
26933       const char *temp;
26934       int insn_code_number = recog_memoized (insn);
26935       location_t location = locator_location (INSN_LOCATOR (insn));
26936
26937       /* Punt on insns we cannot recognize.  */
26938       if (insn_code_number < 0)
26939         return;
26940
26941       temp = get_insn_template (insn_code_number, insn);
26942
26943       if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
26944         warning_at (location, OPT_mwarn_cell_microcode,
26945                     "emitting microcode insn %s\t[%s] #%d",
26946                     temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn)); 
26947       else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
26948         warning_at (location, OPT_mwarn_cell_microcode,
26949                     "emitting conditional microcode insn %s\t[%s] #%d",
26950                     temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
26951     }
26952 }
26953
26954 \f
26955 /* Mask options that we want to support inside of attribute((target)) and
26956    #pragma GCC target operations.  Note, we do not include things like
26957    64/32-bit, endianess, hard/soft floating point, etc. that would have
26958    different calling sequences.  */
26959
26960 struct rs6000_opt_mask {
26961   const char *name;             /* option name */
26962   int mask;                     /* mask to set */
26963   bool invert;                  /* invert sense of mask */
26964   bool valid_target;            /* option is a target option */
26965 };
26966
26967 static struct rs6000_opt_mask const rs6000_opt_masks[] =
26968 {
26969   { "altivec",          MASK_ALTIVEC,           false, true  },
26970   { "cmpb",             MASK_CMPB,              false, true  },
26971   { "dlmzb",            MASK_DLMZB,             false, true  },
26972   { "fprnd",            MASK_FPRND,             false, true  },
26973   { "hard-dfp",         MASK_DFP,               false, true  },
26974   { "isel",             MASK_ISEL,              false, true  },
26975   { "mfcrf",            MASK_MFCRF,             false, true  },
26976   { "mfpgpr",           MASK_MFPGPR,            false, true  },
26977   { "mulhw",            MASK_MULHW,             false, true  },
26978   { "multiple",         MASK_MULTIPLE,          false, true  },
26979   { "update",           MASK_NO_UPDATE,         true , true  },
26980   { "popcntb",          MASK_POPCNTB,           false, true  },
26981   { "popcntd",          MASK_POPCNTD,           false, true  },
26982   { "powerpc-gfxopt",   MASK_PPC_GFXOPT,        false, true  },
26983   { "powerpc-gpopt",    MASK_PPC_GPOPT,         false, true  },
26984   { "recip-precision",  MASK_RECIP_PRECISION,   false, true  },
26985   { "string",           MASK_STRING,            false, true  },
26986   { "vsx",              MASK_VSX,               false, true  },
26987 #ifdef MASK_64BIT
26988 #if TARGET_AIX_OS
26989   { "aix64",            MASK_64BIT,             false, false },
26990   { "aix32",            MASK_64BIT,             true,  false },
26991 #else
26992   { "64",               MASK_64BIT,             false, false },
26993   { "32",               MASK_64BIT,             true,  false },
26994 #endif
26995 #endif
26996 #ifdef MASK_EABI
26997   { "eabi",             MASK_EABI,              false, false },
26998 #endif
26999 #ifdef MASK_LITTLE_ENDIAN
27000   { "little",           MASK_LITTLE_ENDIAN,     false, false },
27001   { "big",              MASK_LITTLE_ENDIAN,     true,  false },
27002 #endif
27003 #ifdef MASK_RELOCATABLE
27004   { "relocatable",      MASK_RELOCATABLE,       false, false },
27005 #endif
27006 #ifdef MASK_STRICT_ALIGN
27007   { "strict-align",     MASK_STRICT_ALIGN,      false, false },
27008 #endif
27009   { "power",            MASK_POWER,             false, false },
27010   { "power2",           MASK_POWER2,            false, false },
27011   { "powerpc",          MASK_POWERPC,           false, false },
27012   { "soft-float",       MASK_SOFT_FLOAT,        false, false },
27013   { "string",           MASK_STRING,            false, false },
27014 };
27015
27016 /* Builtin mask mapping for printing the flags.  */
27017 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
27018 {
27019   { "altivec",           RS6000_BTM_ALTIVEC,    false, false },
27020   { "vsx",               RS6000_BTM_VSX,        false, false },
27021   { "spe",               RS6000_BTM_SPE,        false, false },
27022   { "paired",            RS6000_BTM_PAIRED,     false, false },
27023   { "fre",               RS6000_BTM_FRE,        false, false },
27024   { "fres",              RS6000_BTM_FRES,       false, false },
27025   { "frsqrte",           RS6000_BTM_FRSQRTE,    false, false },
27026   { "frsqrtes",          RS6000_BTM_FRSQRTES,   false, false },
27027   { "popcntd",           RS6000_BTM_POPCNTD,    false, false },
27028   { "powerpc",           RS6000_BTM_POWERPC,    false, false },
27029   { "cell",              RS6000_BTM_CELL,       false, false },
27030 };
27031
27032 /* Option variables that we want to support inside attribute((target)) and
27033    #pragma GCC target operations.  */
27034
27035 struct rs6000_opt_var {
27036   const char *name;             /* option name */
27037   size_t global_offset;         /* offset of the option in global_options.  */
27038   size_t target_offset;         /* offset of the option in target optiosn.  */
27039 };
27040
27041 static struct rs6000_opt_var const rs6000_opt_vars[] =
27042 {
27043   { "friz",
27044     offsetof (struct gcc_options, x_TARGET_FRIZ),
27045     offsetof (struct cl_target_option, x_TARGET_FRIZ), },
27046   { "avoid-indexed-addresses",
27047     offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
27048     offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
27049   { "paired",
27050     offsetof (struct gcc_options, x_rs6000_paired_float),
27051     offsetof (struct cl_target_option, x_rs6000_paired_float), },
27052   { "longcall",
27053     offsetof (struct gcc_options, x_rs6000_default_long_calls),
27054     offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
27055 };
27056
27057 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
27058    parsing.  Return true if there were no errors.  */
27059
27060 static bool
27061 rs6000_inner_target_options (tree args, bool attr_p)
27062 {
27063   bool ret = true;
27064
27065   if (args == NULL_TREE)
27066     ;
27067
27068   else if (TREE_CODE (args) == STRING_CST)
27069     {
27070       char *p = ASTRDUP (TREE_STRING_POINTER (args));
27071       char *q;
27072
27073       while ((q = strtok (p, ",")) != NULL)
27074         {
27075           bool error_p = false;
27076           bool not_valid_p = false;
27077           const char *cpu_opt = NULL;
27078
27079           p = NULL;
27080           if (strncmp (q, "cpu=", 4) == 0)
27081             {
27082               int cpu_index = rs6000_cpu_name_lookup (q+4);
27083               if (cpu_index >= 0)
27084                 rs6000_cpu_index = cpu_index;
27085               else
27086                 {
27087                   error_p = true;
27088                   cpu_opt = q+4;
27089                 }
27090             }
27091           else if (strncmp (q, "tune=", 5) == 0)
27092             {
27093               int tune_index = rs6000_cpu_name_lookup (q+5);
27094               if (tune_index >= 0)
27095                 rs6000_tune_index = tune_index;
27096               else
27097                 {
27098                   error_p = true;
27099                   cpu_opt = q+5;
27100                 }
27101             }
27102           else
27103             {
27104               size_t i;
27105               bool invert = false;
27106               char *r = q;
27107
27108               error_p = true;
27109               if (strncmp (r, "no-", 3) == 0)
27110                 {
27111                   invert = true;
27112                   r += 3;
27113                 }
27114
27115               for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
27116                 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
27117                   {
27118                     int mask = rs6000_opt_masks[i].mask;
27119
27120                     if (!rs6000_opt_masks[i].valid_target)
27121                       not_valid_p = true;
27122                     else
27123                       {
27124                         error_p = false;
27125                         target_flags_explicit |= mask;
27126
27127                         /* VSX needs altivec, so -mvsx automagically sets
27128                            altivec.  */
27129                         if (mask == MASK_VSX && !invert)
27130                           mask |= MASK_ALTIVEC;
27131
27132                         if (rs6000_opt_masks[i].invert)
27133                           invert = !invert;
27134
27135                         if (invert)
27136                           target_flags &= ~mask;
27137                         else
27138                           target_flags |= mask;
27139                       }
27140                     break;
27141                   }
27142
27143               if (error_p && !not_valid_p)
27144                 {
27145                   for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
27146                     if (strcmp (r, rs6000_opt_vars[i].name) == 0)
27147                       {
27148                         size_t j = rs6000_opt_vars[i].global_offset;
27149                         *((int *) ((char *)&global_options + j)) = !invert;
27150                         error_p = false;
27151                         break;
27152                       }
27153                 }
27154             }
27155
27156           if (error_p)
27157             {
27158               const char *eprefix, *esuffix;
27159
27160               ret = false;
27161               if (attr_p)
27162                 {
27163                   eprefix = "__attribute__((__target__(";
27164                   esuffix = ")))";
27165                 }
27166               else
27167                 {
27168                   eprefix = "#pragma GCC target ";
27169                   esuffix = "";
27170                 }
27171
27172               if (cpu_opt)
27173                 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
27174                        q, esuffix);
27175               else if (not_valid_p)
27176                 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
27177               else
27178                 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
27179             }
27180         }
27181     }
27182
27183   else if (TREE_CODE (args) == TREE_LIST)
27184     {
27185       do
27186         {
27187           tree value = TREE_VALUE (args);
27188           if (value)
27189             {
27190               bool ret2 = rs6000_inner_target_options (value, attr_p);
27191               if (!ret2)
27192                 ret = false;
27193             }
27194           args = TREE_CHAIN (args);
27195         }
27196       while (args != NULL_TREE);
27197     }
27198
27199   else
27200     gcc_unreachable ();
27201
27202   return ret;
27203 }
27204
27205 /* Print out the target options as a list for -mdebug=target.  */
27206
27207 static void
27208 rs6000_debug_target_options (tree args, const char *prefix)
27209 {
27210   if (args == NULL_TREE)
27211     fprintf (stderr, "%s<NULL>", prefix);
27212
27213   else if (TREE_CODE (args) == STRING_CST)
27214     {
27215       char *p = ASTRDUP (TREE_STRING_POINTER (args));
27216       char *q;
27217
27218       while ((q = strtok (p, ",")) != NULL)
27219         {
27220           p = NULL;
27221           fprintf (stderr, "%s\"%s\"", prefix, q);
27222           prefix = ", ";
27223         }
27224     }
27225
27226   else if (TREE_CODE (args) == TREE_LIST)
27227     {
27228       do
27229         {
27230           tree value = TREE_VALUE (args);
27231           if (value)
27232             {
27233               rs6000_debug_target_options (value, prefix);
27234               prefix = ", ";
27235             }
27236           args = TREE_CHAIN (args);
27237         }
27238       while (args != NULL_TREE);
27239     }
27240
27241   else
27242     gcc_unreachable ();
27243
27244   return;
27245 }
27246
27247 \f
27248 /* Hook to validate attribute((target("..."))).  */
27249
27250 static bool
27251 rs6000_valid_attribute_p (tree fndecl,
27252                           tree ARG_UNUSED (name),
27253                           tree args,
27254                           int flags)
27255 {
27256   struct cl_target_option cur_target;
27257   bool ret;
27258   tree old_optimize = build_optimization_node ();
27259   tree new_target, new_optimize;
27260   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
27261
27262   gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
27263
27264   if (TARGET_DEBUG_TARGET)
27265     {
27266       tree tname = DECL_NAME (fndecl);
27267       fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
27268       if (tname)
27269         fprintf (stderr, "function: %.*s\n",
27270                  (int) IDENTIFIER_LENGTH (tname),
27271                  IDENTIFIER_POINTER (tname));
27272       else
27273         fprintf (stderr, "function: unknown\n");
27274   
27275       fprintf (stderr, "args:");
27276       rs6000_debug_target_options (args, " ");
27277       fprintf (stderr, "\n");
27278
27279       if (flags)
27280         fprintf (stderr, "flags: 0x%x\n", flags);
27281
27282       fprintf (stderr, "--------------------\n");
27283     }
27284
27285   old_optimize = build_optimization_node ();
27286   func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
27287
27288   /* If the function changed the optimization levels as well as setting target
27289      options, start with the optimizations specified.  */
27290   if (func_optimize && func_optimize != old_optimize)
27291     cl_optimization_restore (&global_options,
27292                              TREE_OPTIMIZATION (func_optimize));
27293
27294   /* The target attributes may also change some optimization flags, so update
27295      the optimization options if necessary.  */
27296   cl_target_option_save (&cur_target, &global_options);
27297   rs6000_cpu_index = rs6000_tune_index = -1;
27298   ret = rs6000_inner_target_options (args, true);
27299
27300   /* Set up any additional state.  */
27301   if (ret)
27302     {
27303       ret = rs6000_option_override_internal (false);
27304       new_target = build_target_option_node ();
27305     }
27306   else
27307     new_target = NULL;
27308
27309   new_optimize = build_optimization_node ();
27310
27311   if (!new_target)
27312     ret = false;
27313
27314   else if (fndecl)
27315     {
27316       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
27317
27318       if (old_optimize != new_optimize)
27319         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
27320     }
27321
27322   cl_target_option_restore (&global_options, &cur_target);
27323
27324   if (old_optimize != new_optimize)
27325     cl_optimization_restore (&global_options,
27326                              TREE_OPTIMIZATION (old_optimize));
27327
27328   return ret;
27329 }
27330
27331 \f
27332 /* Hook to validate the current #pragma GCC target and set the state, and
27333    update the macros based on what was changed.  If ARGS is NULL, then
27334    POP_TARGET is used to reset the options.  */
27335
27336 bool
27337 rs6000_pragma_target_parse (tree args, tree pop_target)
27338 {
27339   tree prev_tree = build_target_option_node ();
27340   tree cur_tree;
27341   struct cl_target_option *prev_opt, *cur_opt;
27342   unsigned prev_bumask, cur_bumask, diff_bumask;
27343   int prev_flags, cur_flags, diff_flags;
27344
27345   if (TARGET_DEBUG_TARGET)
27346     {
27347       fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
27348       fprintf (stderr, "args:");
27349       rs6000_debug_target_options (args, " ");
27350       fprintf (stderr, "\n");
27351
27352       if (pop_target)
27353         {
27354           fprintf (stderr, "pop_target:\n");
27355           debug_tree (pop_target);
27356         }
27357       else
27358         fprintf (stderr, "pop_target: <NULL>\n");
27359
27360       fprintf (stderr, "--------------------\n");
27361     }
27362
27363   if (! args)
27364     {
27365       cur_tree = ((pop_target)
27366                   ? pop_target
27367                   : target_option_default_node);
27368       cl_target_option_restore (&global_options,
27369                                 TREE_TARGET_OPTION (cur_tree));
27370     }
27371   else
27372     {
27373       rs6000_cpu_index = rs6000_tune_index = -1;
27374       if (!rs6000_inner_target_options (args, false)
27375           || !rs6000_option_override_internal (false)
27376           || (cur_tree = build_target_option_node ()) == NULL_TREE)
27377         {
27378           if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
27379             fprintf (stderr, "invalid pragma\n");
27380
27381           return false;
27382         }
27383     }
27384
27385   target_option_current_node = cur_tree;
27386
27387   /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
27388      change the macros that are defined.  */
27389   if (rs6000_target_modify_macros_ptr)
27390     {
27391       prev_opt    = TREE_TARGET_OPTION (prev_tree);
27392       prev_bumask = prev_opt->x_rs6000_builtin_mask;
27393       prev_flags  = prev_opt->x_target_flags;
27394
27395       cur_opt     = TREE_TARGET_OPTION (cur_tree);
27396       cur_flags   = cur_opt->x_target_flags;
27397       cur_bumask  = cur_opt->x_rs6000_builtin_mask;
27398
27399       diff_bumask = (prev_bumask ^ cur_bumask);
27400       diff_flags  = (prev_flags  ^ cur_flags);
27401
27402       if ((diff_flags != 0) || (diff_bumask != 0))
27403         {
27404           /* Delete old macros.  */
27405           rs6000_target_modify_macros_ptr (false,
27406                                            prev_flags & diff_flags,
27407                                            prev_bumask & diff_bumask);
27408
27409           /* Define new macros.  */
27410           rs6000_target_modify_macros_ptr (true,
27411                                            cur_flags & diff_flags,
27412                                            cur_bumask & diff_bumask);
27413         }
27414     }
27415
27416   return true;
27417 }
27418
27419 \f
27420 /* Remember the last target of rs6000_set_current_function.  */
27421 static GTY(()) tree rs6000_previous_fndecl;
27422
27423 /* Establish appropriate back-end context for processing the function
27424    FNDECL.  The argument might be NULL to indicate processing at top
27425    level, outside of any function scope.  */
27426 static void
27427 rs6000_set_current_function (tree fndecl)
27428 {
27429   tree old_tree = (rs6000_previous_fndecl
27430                    ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
27431                    : NULL_TREE);
27432
27433   tree new_tree = (fndecl
27434                    ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
27435                    : NULL_TREE);
27436
27437   if (TARGET_DEBUG_TARGET)
27438     {
27439       bool print_final = false;
27440       fprintf (stderr, "\n==================== rs6000_set_current_function");
27441
27442       if (fndecl)
27443         fprintf (stderr, ", fndecl %s (%p)",
27444                  (DECL_NAME (fndecl)
27445                   ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
27446                   : "<unknown>"), (void *)fndecl);
27447
27448       if (rs6000_previous_fndecl)
27449         fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
27450
27451       fprintf (stderr, "\n");
27452       if (new_tree)
27453         {
27454           fprintf (stderr, "\nnew fndecl target specific options:\n");
27455           debug_tree (new_tree);
27456           print_final = true;
27457         }
27458
27459       if (old_tree)
27460         {
27461           fprintf (stderr, "\nold fndecl target specific options:\n");
27462           debug_tree (old_tree);
27463           print_final = true;
27464         }
27465
27466       if (print_final)
27467         fprintf (stderr, "--------------------\n");
27468     }
27469
27470   /* Only change the context if the function changes.  This hook is called
27471      several times in the course of compiling a function, and we don't want to
27472      slow things down too much or call target_reinit when it isn't safe.  */
27473   if (fndecl && fndecl != rs6000_previous_fndecl)
27474     {
27475       rs6000_previous_fndecl = fndecl;
27476       if (old_tree == new_tree)
27477         ;
27478
27479       else if (new_tree)
27480         {
27481           cl_target_option_restore (&global_options,
27482                                     TREE_TARGET_OPTION (new_tree));
27483           target_reinit ();
27484         }
27485
27486       else if (old_tree)
27487         {
27488           struct cl_target_option *def
27489             = TREE_TARGET_OPTION (target_option_current_node);
27490
27491           cl_target_option_restore (&global_options, def);
27492           target_reinit ();
27493         }
27494     }
27495 }
27496
27497 \f
27498 /* Save the current options */
27499
27500 static void
27501 rs6000_function_specific_save (struct cl_target_option *ptr)
27502 {
27503   ptr->rs6000_target_flags_explicit = target_flags_explicit;
27504 }
27505
27506 /* Restore the current options */
27507
27508 static void
27509 rs6000_function_specific_restore (struct cl_target_option *ptr)
27510 {
27511   target_flags_explicit = ptr->rs6000_target_flags_explicit;
27512   (void) rs6000_option_override_internal (false);
27513 }
27514
27515 /* Print the current options */
27516
27517 static void
27518 rs6000_function_specific_print (FILE *file, int indent,
27519                                 struct cl_target_option *ptr)
27520 {
27521   size_t i;
27522   int flags = ptr->x_target_flags;
27523   unsigned bu_mask = ptr->x_rs6000_builtin_mask;
27524
27525   /* Print the various mask options.  */
27526   for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
27527     if ((flags & rs6000_opt_masks[i].mask) != 0)
27528       {
27529         flags &= ~ rs6000_opt_masks[i].mask;
27530         fprintf (file, "%*s-m%s%s\n", indent, "",
27531                  rs6000_opt_masks[i].invert ? "no-" : "",
27532                  rs6000_opt_masks[i].name);
27533       }
27534
27535   /* Print the various options that are variables.  */
27536   for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
27537     {
27538       size_t j = rs6000_opt_vars[i].target_offset;
27539       if (((signed char *) ptr)[j])
27540         fprintf (file, "%*s-m%s\n", indent, "",
27541                  rs6000_opt_vars[i].name);
27542     }
27543
27544   /* Print the various builtin flags.  */
27545   fprintf (file, "%*sbuiltin mask = 0x%x\n", indent, "", bu_mask);
27546   for (i = 0; i < ARRAY_SIZE (rs6000_builtin_mask_names); i++)
27547     if ((bu_mask & rs6000_builtin_mask_names[i].mask) != 0)
27548       {
27549         fprintf (file, "%*s%s builtins supported\n", indent, "",
27550                  rs6000_builtin_mask_names[i].name);
27551       }
27552 }
27553
27554 \f
27555 /* Hook to determine if one function can safely inline another.  */
27556
27557 static bool
27558 rs6000_can_inline_p (tree caller, tree callee)
27559 {
27560   bool ret = false;
27561   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
27562   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
27563
27564   /* If callee has no option attributes, then it is ok to inline.  */
27565   if (!callee_tree)
27566     ret = true;
27567
27568   /* If caller has no option attributes, but callee does then it is not ok to
27569      inline.  */
27570   else if (!caller_tree)
27571     ret = false;
27572
27573   else
27574     {
27575       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
27576       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
27577
27578       /* Callee's options should a subset of the caller's, i.e. a vsx function
27579          can inline an altivec function but a non-vsx function can't inline a
27580          vsx function.  */
27581       if ((caller_opts->x_target_flags & callee_opts->x_target_flags)
27582           == callee_opts->x_target_flags)
27583         ret = true;
27584     }
27585
27586   if (TARGET_DEBUG_TARGET)
27587     fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
27588              (DECL_NAME (caller)
27589               ? IDENTIFIER_POINTER (DECL_NAME (caller))
27590               : "<unknown>"),
27591              (DECL_NAME (callee)
27592               ? IDENTIFIER_POINTER (DECL_NAME (callee))
27593               : "<unknown>"),
27594              (ret ? "can" : "cannot"));
27595
27596   return ret;
27597 }
27598 \f
27599 /* Allocate a stack temp and fixup the address so it meets the particular
27600    memory requirements (either offetable or REG+REG addressing).  */
27601
27602 rtx
27603 rs6000_allocate_stack_temp (enum machine_mode mode,
27604                             bool offsettable_p,
27605                             bool reg_reg_p)
27606 {
27607   rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
27608   rtx addr = XEXP (stack, 0);
27609   int strict_p = (reload_in_progress || reload_completed);
27610
27611   if (!legitimate_indirect_address_p (addr, strict_p))
27612     {
27613       if (offsettable_p
27614           && !rs6000_legitimate_offset_address_p (mode, addr, strict_p))
27615         stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
27616
27617       else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
27618         stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
27619     }
27620
27621   return stack;
27622 }
27623
27624 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
27625    to such a form to deal with memory reference instructions like STFIWX that
27626    only take reg+reg addressing.  */
27627
27628 rtx
27629 rs6000_address_for_fpconvert (rtx x)
27630 {
27631   int strict_p = (reload_in_progress || reload_completed);
27632   rtx addr;
27633
27634   gcc_assert (MEM_P (x));
27635   addr = XEXP (x, 0);
27636   if (! legitimate_indirect_address_p (addr, strict_p)
27637       && ! legitimate_indexed_address_p (addr, strict_p))
27638     {
27639       if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
27640         {
27641           rtx reg = XEXP (addr, 0);
27642           HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
27643           rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
27644           gcc_assert (REG_P (reg));
27645           emit_insn (gen_add3_insn (reg, reg, size_rtx));
27646           addr = reg;
27647         }
27648       else if (GET_CODE (addr) == PRE_MODIFY)
27649         {
27650           rtx reg = XEXP (addr, 0);
27651           rtx expr = XEXP (addr, 1);
27652           gcc_assert (REG_P (reg));
27653           gcc_assert (GET_CODE (expr) == PLUS);
27654           emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
27655           addr = reg;
27656         }
27657
27658       x = replace_equiv_address (x, copy_addr_to_reg (addr));
27659     }
27660
27661   return x;
27662 }
27663
27664 /* Given a memory reference, if it is not in the form for altivec memory
27665    reference instructions (i.e. reg or reg+reg addressing with AND of -16),
27666    convert to the altivec format.  */
27667
27668 rtx
27669 rs6000_address_for_altivec (rtx x)
27670 {
27671   gcc_assert (MEM_P (x));
27672   if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
27673     {
27674       rtx addr = XEXP (x, 0);
27675       int strict_p = (reload_in_progress || reload_completed);
27676
27677       if (!legitimate_indexed_address_p (addr, strict_p)
27678           && !legitimate_indirect_address_p (addr, strict_p))
27679         addr = copy_to_mode_reg (Pmode, addr);
27680
27681       addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
27682       x = change_address (x, GET_MODE (x), addr);
27683     }
27684
27685   return x;
27686 }
27687
27688 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
27689
27690    On the RS/6000, all integer constants are acceptable, most won't be valid
27691    for particular insns, though.  Only easy FP constants are acceptable.  */
27692
27693 static bool
27694 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
27695 {
27696   if (rs6000_tls_referenced_p (x))
27697     return false;
27698
27699   return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
27700           || GET_MODE (x) == VOIDmode
27701           || (TARGET_POWERPC64 && mode == DImode)
27702           || easy_fp_constant (x, mode)
27703           || easy_vector_constant (x, mode));
27704 }
27705
27706 \f
27707 /* A function pointer under AIX is a pointer to a data area whose first word
27708    contains the actual address of the function, whose second word contains a
27709    pointer to its TOC, and whose third word contains a value to place in the
27710    static chain register (r11).  Note that if we load the static chain, our
27711    "trampoline" need not have any executable code.  */
27712
27713 void
27714 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
27715 {
27716   rtx func_addr;
27717   rtx toc_reg;
27718   rtx sc_reg;
27719   rtx stack_ptr;
27720   rtx stack_toc_offset;
27721   rtx stack_toc_mem;
27722   rtx func_toc_offset;
27723   rtx func_toc_mem;
27724   rtx func_sc_offset;
27725   rtx func_sc_mem;
27726   rtx insn;
27727   rtx (*call_func) (rtx, rtx, rtx, rtx);
27728   rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
27729
27730   stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
27731   toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
27732
27733   /* Load up address of the actual function.  */
27734   func_desc = force_reg (Pmode, func_desc);
27735   func_addr = gen_reg_rtx (Pmode);
27736   emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
27737
27738   if (TARGET_32BIT)
27739     {
27740
27741       stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
27742       func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
27743       func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
27744       if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
27745         {
27746           call_func = gen_call_indirect_aix32bit;
27747           call_value_func = gen_call_value_indirect_aix32bit;
27748         }
27749       else
27750         {
27751           call_func = gen_call_indirect_aix32bit_nor11;
27752           call_value_func = gen_call_value_indirect_aix32bit_nor11;
27753         }
27754     }
27755   else
27756     {
27757       stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
27758       func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
27759       func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
27760       if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
27761         {
27762           call_func = gen_call_indirect_aix64bit;
27763           call_value_func = gen_call_value_indirect_aix64bit;
27764         }
27765       else
27766         {
27767           call_func = gen_call_indirect_aix64bit_nor11;
27768           call_value_func = gen_call_value_indirect_aix64bit_nor11;
27769         }
27770     }
27771
27772   /* Reserved spot to store the TOC.  */
27773   stack_toc_mem = gen_frame_mem (Pmode,
27774                                  gen_rtx_PLUS (Pmode,
27775                                                stack_ptr,
27776                                                stack_toc_offset));
27777
27778   gcc_assert (cfun);
27779   gcc_assert (cfun->machine);
27780
27781   /* Can we optimize saving the TOC in the prologue or do we need to do it at
27782      every call?  */
27783   if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
27784     cfun->machine->save_toc_in_prologue = true;
27785
27786   else
27787     {
27788       MEM_VOLATILE_P (stack_toc_mem) = 1;
27789       emit_move_insn (stack_toc_mem, toc_reg);
27790     }
27791
27792   /* Calculate the address to load the TOC of the called function.  We don't
27793      actually load this until the split after reload.  */
27794   func_toc_mem = gen_rtx_MEM (Pmode,
27795                               gen_rtx_PLUS (Pmode,
27796                                             func_desc,
27797                                             func_toc_offset));
27798
27799   /* If we have a static chain, load it up.  */
27800   if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
27801     {
27802       func_sc_mem = gen_rtx_MEM (Pmode,
27803                                  gen_rtx_PLUS (Pmode,
27804                                                func_desc,
27805                                                func_sc_offset));
27806
27807       sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
27808       emit_move_insn (sc_reg, func_sc_mem);
27809     }
27810
27811   /* Create the call.  */
27812   if (value)
27813     insn = call_value_func (value, func_addr, flag, func_toc_mem,
27814                             stack_toc_mem);
27815   else
27816     insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
27817
27818   emit_call_insn (insn);
27819 }
27820
27821 /* Return whether we need to always update the saved TOC pointer when we update
27822    the stack pointer.  */
27823
27824 static bool
27825 rs6000_save_toc_in_prologue_p (void)
27826 {
27827   return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
27828 }
27829
27830 #ifdef HAVE_GAS_HIDDEN
27831 # define USE_HIDDEN_LINKONCE 1
27832 #else
27833 # define USE_HIDDEN_LINKONCE 0
27834 #endif
27835
27836 /* Fills in the label name that should be used for a 476 link stack thunk.  */
27837
27838 void
27839 get_ppc476_thunk_name (char name[32])
27840 {
27841   gcc_assert (TARGET_LINK_STACK);
27842
27843   if (USE_HIDDEN_LINKONCE)
27844     sprintf (name, "__ppc476.get_thunk");
27845   else
27846     ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
27847 }
27848
27849 /* This function emits the simple thunk routine that is used to preserve
27850    the link stack on the 476 cpu.  */
27851
27852 static void
27853 rs6000_code_end (void)
27854 {
27855   char name[32];
27856   tree decl;
27857
27858   if (!TARGET_LINK_STACK)
27859     return;
27860
27861   get_ppc476_thunk_name (name);
27862
27863   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
27864                      build_function_type_list (void_type_node, NULL_TREE));
27865   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
27866                                    NULL_TREE, void_type_node);
27867   TREE_PUBLIC (decl) = 1;
27868   TREE_STATIC (decl) = 1;
27869
27870   if (USE_HIDDEN_LINKONCE)
27871     {
27872       DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
27873       targetm.asm_out.unique_section (decl, 0);
27874       switch_to_section (get_named_section (decl, NULL, 0));
27875       DECL_WEAK (decl) = 1;
27876       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
27877       targetm.asm_out.globalize_label (asm_out_file, name);
27878       targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
27879       ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
27880     }
27881   else
27882     {
27883       switch_to_section (text_section);
27884       ASM_OUTPUT_LABEL (asm_out_file, name);
27885     }
27886
27887   DECL_INITIAL (decl) = make_node (BLOCK);
27888   current_function_decl = decl;
27889   init_function_start (decl);
27890   first_function_block_is_cold = false;
27891   /* Make sure unwind info is emitted for the thunk if needed.  */
27892   final_start_function (emit_barrier (), asm_out_file, 1);
27893
27894   fputs ("\tblr\n", asm_out_file);
27895
27896   final_end_function ();
27897   init_insn_lengths ();
27898   free_after_compilation (cfun);
27899   set_cfun (NULL);
27900   current_function_decl = NULL;
27901 }
27902
27903 /* Add r30 to hard reg set if the prologue sets it up and it is not
27904    pic_offset_table_rtx.  */
27905
27906 static void
27907 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
27908 {
27909   if (!TARGET_SINGLE_PIC_BASE
27910       && TARGET_TOC
27911       && TARGET_MINIMAL_TOC
27912       && get_pool_size () != 0)
27913     add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
27914 }
27915
27916 struct gcc_target targetm = TARGET_INITIALIZER;
27917
27918 #include "gt-rs6000.h"