1 /* Subroutines used for code generation on IBM RS/6000.
2    Copyright (C) 1991-2015 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 3, or (at your
10    option) any later version.
11 
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-attr.h"
31 #include "flags.h"
32 #include "recog.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "except.h"
38 #include "function.h"
39 #include "output.h"
40 #include "dbxout.h"
41 #include "basic-block.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "cfgloop.h"
53 #include "sched-int.h"
54 #include "gimple.h"
55 #include "tree-flow.h"
56 #include "intl.h"
57 #include "params.h"
58 #include "tm-constrs.h"
59 #include "ira.h"
60 #include "opts.h"
61 #include "tree-vectorizer.h"
62 #include "dumpfile.h"
63 #include "real.h"
64 #include "tree-pass.h"
65 #if TARGET_XCOFF
66 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
67 #endif
68 #if TARGET_MACHO
69 #include "gstab.h"  /* for N_SLINE */
70 #endif
71 
72 #ifndef TARGET_NO_PROTOTYPE
73 #define TARGET_NO_PROTOTYPE 0
74 #endif
75 
76 #define min(A,B)	((A) < (B) ? (A) : (B))
77 #define max(A,B)	((A) > (B) ? (A) : (B))
78 
79 /* Structure used to define the rs6000 stack */
80 typedef struct rs6000_stack {
81   int reload_completed;		/* stack info won't change from here on */
82   int first_gp_reg_save;	/* first callee saved GP register used */
83   int first_fp_reg_save;	/* first callee saved FP register used */
84   int first_altivec_reg_save;	/* first callee saved AltiVec register used */
85   int lr_save_p;		/* true if the link reg needs to be saved */
86   int cr_save_p;		/* true if the CR reg needs to be saved */
87   unsigned int vrsave_mask;	/* mask of vec registers to save */
88   int push_p;			/* true if we need to allocate stack space */
89   int calls_p;			/* true if the function makes any calls */
90   int world_save_p;		/* true if we're saving *everything*:
91 				   r13-r31, cr, f14-f31, vrsave, v20-v31  */
92   enum rs6000_abi abi;		/* which ABI to use */
93   int gp_save_offset;		/* offset to save GP regs from initial SP */
94   int fp_save_offset;		/* offset to save FP regs from initial SP */
95   int altivec_save_offset;	/* offset to save AltiVec regs from initial SP */
96   int lr_save_offset;		/* offset to save LR from initial SP */
97   int cr_save_offset;		/* offset to save CR from initial SP */
98   int vrsave_save_offset;	/* offset to save VRSAVE from initial SP */
99   int spe_gp_save_offset;	/* offset to save spe 64-bit gprs  */
100   int varargs_save_offset;	/* offset to save the varargs registers */
101   int ehrd_offset;		/* offset to EH return data */
102   int ehcr_offset;		/* offset to EH CR field data */
103   int reg_size;			/* register size (4 or 8) */
104   HOST_WIDE_INT vars_size;	/* variable save area size */
105   int parm_size;		/* outgoing parameter size */
106   int save_size;		/* save area size */
107   int fixed_size;		/* fixed size of stack frame */
108   int gp_size;			/* size of saved GP registers */
109   int fp_size;			/* size of saved FP registers */
110   int altivec_size;		/* size of saved AltiVec registers */
111   int cr_size;			/* size to hold CR if not in save_size */
112   int vrsave_size;		/* size to hold VRSAVE if not in save_size */
113   int altivec_padding_size;	/* size of altivec alignment padding if
114 				   not in save_size */
115   int spe_gp_size;		/* size of 64-bit GPR save size for SPE */
116   int spe_padding_size;
117   HOST_WIDE_INT total_size;	/* total bytes allocated for stack */
118   int spe_64bit_regs_used;
119   int savres_strategy;
120 } rs6000_stack_t;
121 
122 /* A C structure for machine-specific, per-function data.
123    This is added to the cfun structure.  */
124 typedef struct GTY(()) machine_function
125 {
126   /* Some local-dynamic symbol.  */
127   const char *some_ld_name;
128   /* Whether the instruction chain has been scanned already.  */
129   int insn_chain_scanned_p;
130   /* Flags if __builtin_return_address (n) with n >= 1 was used.  */
131   int ra_needs_full_frame;
132   /* Flags if __builtin_return_address (0) was used.  */
133   int ra_need_lr;
134   /* Cache lr_save_p after expansion of builtin_eh_return.  */
135   int lr_save_state;
136   /* Whether we need to save the TOC to the reserved stack location in the
137      function prologue.  */
138   bool save_toc_in_prologue;
139   /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
140      varargs save area.  */
141   HOST_WIDE_INT varargs_save_offset;
142   /* Temporary stack slot to use for SDmode copies.  This slot is
143      64-bits wide and is allocated early enough so that the offset
144      does not overflow the 16-bit load/store offset field.  */
145   rtx sdmode_stack_slot;
146   /* Flag if r2 setup is needed with ELFv2 ABI.  */
147   bool r2_setup_needed;
148 } machine_function;
149 
150 /* Support targetm.vectorize.builtin_mask_for_load.  */
151 static GTY(()) tree altivec_builtin_mask_for_load;
152 
153 /* Set to nonzero once AIX common-mode calls have been defined.  */
154 static GTY(()) int common_mode_defined;
155 
156 /* Label number of label created for -mrelocatable, to call to so we can
157    get the address of the GOT section */
158 static int rs6000_pic_labelno;
159 
160 #ifdef USING_ELFOS_H
161 /* Counter for labels which are to be placed in .fixup.  */
162 int fixuplabelno = 0;
163 #endif
164 
165 /* Whether to use variant of AIX ABI for PowerPC64 Linux.  */
166 int dot_symbols;
167 
168 /* Specify the machine mode that pointers have.  After generation of rtl, the
169    compiler makes no further distinction between pointers and any other objects
170    of this machine mode.  The type is unsigned since not all things that
171    include rs6000.h also include machmode.h.  */
172 unsigned rs6000_pmode;
173 
174 /* Width in bits of a pointer.  */
175 unsigned rs6000_pointer_size;
176 
177 #ifdef HAVE_AS_GNU_ATTRIBUTE
178 /* Flag whether floating point values have been passed/returned.  */
179 static bool rs6000_passes_float;
180 /* Flag whether vector values have been passed/returned.  */
181 static bool rs6000_passes_vector;
182 /* Flag whether small (<= 8 byte) structures have been returned.  */
183 static bool rs6000_returns_struct;
184 #endif
185 
186 /* Value is TRUE if register/mode pair is acceptable.  */
187 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
188 
189 /* Maximum number of registers needed for a given register class and mode.  */
190 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
191 
192 /* How many registers are needed for a given register and mode.  */
193 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
194 
195 /* Map register number to register class.  */
196 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
197 
198 static int dbg_cost_ctrl;
199 
200 /* Built in types.  */
201 tree rs6000_builtin_types[RS6000_BTI_MAX];
202 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
203 
204 /* Flag to say the TOC is initialized */
205 int toc_initialized;
206 char toc_label_name[10];
207 
208 /* Cached value of rs6000_variable_issue. This is cached in
209    rs6000_variable_issue hook and returned from rs6000_sched_reorder2.  */
210 static short cached_can_issue_more;
211 
212 static GTY(()) section *read_only_data_section;
213 static GTY(()) section *private_data_section;
214 static GTY(()) section *tls_data_section;
215 static GTY(()) section *tls_private_data_section;
216 static GTY(()) section *read_only_private_data_section;
217 static GTY(()) section *sdata2_section;
218 static GTY(()) section *toc_section;
219 
220 struct builtin_description
221 {
222   const HOST_WIDE_INT mask;
223   const enum insn_code icode;
224   const char *const name;
225   const enum rs6000_builtins code;
226 };
227 
228 /* Describe the vector unit used for modes.  */
229 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
230 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
231 
232 /* Register classes for various constraints that are based on the target
233    switches.  */
234 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
235 
236 /* Describe the alignment of a vector.  */
237 int rs6000_vector_align[NUM_MACHINE_MODES];
238 
239 /* Map selected modes to types for builtins.  */
240 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
241 
242 /* What modes to automatically generate reciprocal divide estimate (fre) and
243    reciprocal sqrt (frsqrte) for.  */
244 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
245 
246 /* Masks to determine which reciprocal esitmate instructions to generate
247    automatically.  */
248 enum rs6000_recip_mask {
249   RECIP_SF_DIV		= 0x001,	/* Use divide estimate */
250   RECIP_DF_DIV		= 0x002,
251   RECIP_V4SF_DIV	= 0x004,
252   RECIP_V2DF_DIV	= 0x008,
253 
254   RECIP_SF_RSQRT	= 0x010,	/* Use reciprocal sqrt estimate.  */
255   RECIP_DF_RSQRT	= 0x020,
256   RECIP_V4SF_RSQRT	= 0x040,
257   RECIP_V2DF_RSQRT	= 0x080,
258 
259   /* Various combination of flags for -mrecip=xxx.  */
260   RECIP_NONE		= 0,
261   RECIP_ALL		= (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
262 			   | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
263 			   | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
264 
265   RECIP_HIGH_PRECISION	= RECIP_ALL,
266 
267   /* On low precision machines like the power5, don't enable double precision
268      reciprocal square root estimate, since it isn't accurate enough.  */
269   RECIP_LOW_PRECISION	= (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
270 };
271 
272 /* -mrecip options.  */
273 static struct
274 {
275   const char *string;		/* option name */
276   unsigned int mask;		/* mask bits to set */
277 } recip_options[] = {
278   { "all",	 RECIP_ALL },
279   { "none",	 RECIP_NONE },
280   { "div",	 (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
281 		  | RECIP_V2DF_DIV) },
282   { "divf",	 (RECIP_SF_DIV | RECIP_V4SF_DIV) },
283   { "divd",	 (RECIP_DF_DIV | RECIP_V2DF_DIV) },
284   { "rsqrt",	 (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
285 		  | RECIP_V2DF_RSQRT) },
286   { "rsqrtf",	 (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
287   { "rsqrtd",	 (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
288 };
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, HOST_WIDE_INT, HOST_WIDE_INT);
294 
295 /* Simplfy register classes into simpler classifications.  We assume
296    GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
297    check for standard register classes (gpr/floating/altivec/vsx) and
298    floating/vector classes (float/altivec/vsx).  */
299 
300 enum rs6000_reg_type {
301   NO_REG_TYPE,
302   PSEUDO_REG_TYPE,
303   GPR_REG_TYPE,
304   VSX_REG_TYPE,
305   ALTIVEC_REG_TYPE,
306   FPR_REG_TYPE,
307   SPR_REG_TYPE,
308   CR_REG_TYPE,
309   SPE_ACC_TYPE,
310   SPEFSCR_REG_TYPE
311 };
312 
313 /* Map register class to register type.  */
314 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
315 
316 /* First/last register type for the 'normal' register types (i.e. general
317    purpose, floating point, altivec, and VSX registers).  */
318 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
319 
320 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
321 
322 
323 /* Register classes we care about in secondary reload or go if legitimate
324    address.  We only need to worry about GPR, FPR, and Altivec registers here,
325    along an ANY field that is the OR of the 3 register classes.  */
326 
327 enum rs6000_reload_reg_type {
328   RELOAD_REG_GPR,			/* General purpose registers.  */
329   RELOAD_REG_FPR,			/* Traditional floating point regs.  */
330   RELOAD_REG_VMX,			/* Altivec (VMX) registers.  */
331   RELOAD_REG_ANY,			/* OR of GPR, FPR, Altivec masks.  */
332   N_RELOAD_REG
333 };
334 
335 /* For setting up register classes, loop through the 3 register classes mapping
336    into real registers, and skip the ANY class, which is just an OR of the
337    bits.  */
338 #define FIRST_RELOAD_REG_CLASS	RELOAD_REG_GPR
339 #define LAST_RELOAD_REG_CLASS	RELOAD_REG_VMX
340 
341 /* Map reload register type to a register in the register class.  */
342 struct reload_reg_map_type {
343   const char *name;			/* Register class name.  */
344   int reg;				/* Register in the register class.  */
345 };
346 
347 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
348   { "Gpr",	FIRST_GPR_REGNO },	/* RELOAD_REG_GPR.  */
349   { "Fpr",	FIRST_FPR_REGNO },	/* RELOAD_REG_FPR.  */
350   { "VMX",	FIRST_ALTIVEC_REGNO },	/* RELOAD_REG_VMX.  */
351   { "Any",	-1 },			/* RELOAD_REG_ANY.  */
352 };
353 
354 /* Mask bits for each register class, indexed per mode.  Historically the
355    compiler has been more restrictive which types can do PRE_MODIFY instead of
356    PRE_INC and PRE_DEC, so keep track of sepaate bits for these two.  */
357 typedef unsigned char addr_mask_type;
358 
359 #define RELOAD_REG_VALID	0x01	/* Mode valid in register..  */
360 #define RELOAD_REG_MULTIPLE	0x02	/* Mode takes multiple registers.  */
361 #define RELOAD_REG_INDEXED	0x04	/* Reg+reg addressing.  */
362 #define RELOAD_REG_OFFSET	0x08	/* Reg+offset addressing. */
363 #define RELOAD_REG_PRE_INCDEC	0x10	/* PRE_INC/PRE_DEC valid.  */
364 #define RELOAD_REG_PRE_MODIFY	0x20	/* PRE_MODIFY valid.  */
365 
366 /* Register type masks based on the type, of valid addressing modes.  */
367 struct rs6000_reg_addr {
368   enum insn_code reload_load;		/* INSN to reload for loading. */
369   enum insn_code reload_store;		/* INSN to reload for storing.  */
370   enum insn_code reload_fpr_gpr;	/* INSN to move from FPR to GPR.  */
371   enum insn_code reload_gpr_vsx;	/* INSN to move from GPR to VSX.  */
372   enum insn_code reload_vsx_gpr;	/* INSN to move from VSX to GPR.  */
373   addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks.  */
374   bool scalar_in_vmx_p;			/* Scalar value can go in VMX.  */
375 };
376 
377 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
378 
379 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC.  */
380 static inline bool
mode_supports_pre_incdec_p(enum machine_mode mode)381 mode_supports_pre_incdec_p (enum machine_mode mode)
382 {
383   return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
384 	  != 0);
385 }
386 
387 /* Helper function to say whether a mode supports PRE_MODIFY.  */
388 static inline bool
mode_supports_pre_modify_p(enum machine_mode mode)389 mode_supports_pre_modify_p (enum machine_mode mode)
390 {
391   return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
392 	  != 0);
393 }
394 
395 
396 /* Target cpu costs.  */
397 
398 struct processor_costs {
399   const int mulsi;	  /* cost of SImode multiplication.  */
400   const int mulsi_const;  /* cost of SImode multiplication by constant.  */
401   const int mulsi_const9; /* cost of SImode mult by short constant.  */
402   const int muldi;	  /* cost of DImode multiplication.  */
403   const int divsi;	  /* cost of SImode division.  */
404   const int divdi;	  /* cost of DImode division.  */
405   const int fp;		  /* cost of simple SFmode and DFmode insns.  */
406   const int dmul;	  /* cost of DFmode multiplication (and fmadd).  */
407   const int sdiv;	  /* cost of SFmode division (fdivs).  */
408   const int ddiv;	  /* cost of DFmode division (fdiv).  */
409   const int cache_line_size;    /* cache line size in bytes. */
410   const int l1_cache_size;	/* size of l1 cache, in kilobytes.  */
411   const int l2_cache_size;	/* size of l2 cache, in kilobytes.  */
412   const int simultaneous_prefetches; /* number of parallel prefetch
413 					operations.  */
414 };
415 
416 const struct processor_costs *rs6000_cost;
417 
418 /* Processor costs (relative to an add) */
419 
420 /* Instruction size costs on 32bit processors.  */
421 static const
422 struct processor_costs size32_cost = {
423   COSTS_N_INSNS (1),    /* mulsi */
424   COSTS_N_INSNS (1),    /* mulsi_const */
425   COSTS_N_INSNS (1),    /* mulsi_const9 */
426   COSTS_N_INSNS (1),    /* muldi */
427   COSTS_N_INSNS (1),    /* divsi */
428   COSTS_N_INSNS (1),    /* divdi */
429   COSTS_N_INSNS (1),    /* fp */
430   COSTS_N_INSNS (1),    /* dmul */
431   COSTS_N_INSNS (1),    /* sdiv */
432   COSTS_N_INSNS (1),    /* ddiv */
433   32,
434   0,
435   0,
436   0,
437 };
438 
439 /* Instruction size costs on 64bit processors.  */
440 static const
441 struct processor_costs size64_cost = {
442   COSTS_N_INSNS (1),    /* mulsi */
443   COSTS_N_INSNS (1),    /* mulsi_const */
444   COSTS_N_INSNS (1),    /* mulsi_const9 */
445   COSTS_N_INSNS (1),    /* muldi */
446   COSTS_N_INSNS (1),    /* divsi */
447   COSTS_N_INSNS (1),    /* divdi */
448   COSTS_N_INSNS (1),    /* fp */
449   COSTS_N_INSNS (1),    /* dmul */
450   COSTS_N_INSNS (1),    /* sdiv */
451   COSTS_N_INSNS (1),    /* ddiv */
452   128,
453   0,
454   0,
455   0,
456 };
457 
458 /* Instruction costs on RS64A processors.  */
459 static const
460 struct processor_costs rs64a_cost = {
461   COSTS_N_INSNS (20),   /* mulsi */
462   COSTS_N_INSNS (12),   /* mulsi_const */
463   COSTS_N_INSNS (8),    /* mulsi_const9 */
464   COSTS_N_INSNS (34),   /* muldi */
465   COSTS_N_INSNS (65),   /* divsi */
466   COSTS_N_INSNS (67),   /* divdi */
467   COSTS_N_INSNS (4),    /* fp */
468   COSTS_N_INSNS (4),    /* dmul */
469   COSTS_N_INSNS (31),   /* sdiv */
470   COSTS_N_INSNS (31),   /* ddiv */
471   128,			/* cache line size */
472   128,			/* l1 cache */
473   2048,			/* l2 cache */
474   1,			/* streams */
475 };
476 
477 /* Instruction costs on MPCCORE processors.  */
478 static const
479 struct processor_costs mpccore_cost = {
480   COSTS_N_INSNS (2),    /* mulsi */
481   COSTS_N_INSNS (2),    /* mulsi_const */
482   COSTS_N_INSNS (2),    /* mulsi_const9 */
483   COSTS_N_INSNS (2),    /* muldi */
484   COSTS_N_INSNS (6),    /* divsi */
485   COSTS_N_INSNS (6),    /* divdi */
486   COSTS_N_INSNS (4),    /* fp */
487   COSTS_N_INSNS (5),    /* dmul */
488   COSTS_N_INSNS (10),   /* sdiv */
489   COSTS_N_INSNS (17),   /* ddiv */
490   32,			/* cache line size */
491   4,			/* l1 cache */
492   16,			/* l2 cache */
493   1,			/* streams */
494 };
495 
496 /* Instruction costs on PPC403 processors.  */
497 static const
498 struct processor_costs ppc403_cost = {
499   COSTS_N_INSNS (4),    /* mulsi */
500   COSTS_N_INSNS (4),    /* mulsi_const */
501   COSTS_N_INSNS (4),    /* mulsi_const9 */
502   COSTS_N_INSNS (4),    /* muldi */
503   COSTS_N_INSNS (33),   /* divsi */
504   COSTS_N_INSNS (33),   /* divdi */
505   COSTS_N_INSNS (11),   /* fp */
506   COSTS_N_INSNS (11),   /* dmul */
507   COSTS_N_INSNS (11),   /* sdiv */
508   COSTS_N_INSNS (11),   /* ddiv */
509   32,			/* cache line size */
510   4,			/* l1 cache */
511   16,			/* l2 cache */
512   1,			/* streams */
513 };
514 
515 /* Instruction costs on PPC405 processors.  */
516 static const
517 struct processor_costs ppc405_cost = {
518   COSTS_N_INSNS (5),    /* mulsi */
519   COSTS_N_INSNS (4),    /* mulsi_const */
520   COSTS_N_INSNS (3),    /* mulsi_const9 */
521   COSTS_N_INSNS (5),    /* muldi */
522   COSTS_N_INSNS (35),   /* divsi */
523   COSTS_N_INSNS (35),   /* divdi */
524   COSTS_N_INSNS (11),   /* fp */
525   COSTS_N_INSNS (11),   /* dmul */
526   COSTS_N_INSNS (11),   /* sdiv */
527   COSTS_N_INSNS (11),   /* ddiv */
528   32,			/* cache line size */
529   16,			/* l1 cache */
530   128,			/* l2 cache */
531   1,			/* streams */
532 };
533 
534 /* Instruction costs on PPC440 processors.  */
535 static const
536 struct processor_costs ppc440_cost = {
537   COSTS_N_INSNS (3),    /* mulsi */
538   COSTS_N_INSNS (2),    /* mulsi_const */
539   COSTS_N_INSNS (2),    /* mulsi_const9 */
540   COSTS_N_INSNS (3),    /* muldi */
541   COSTS_N_INSNS (34),   /* divsi */
542   COSTS_N_INSNS (34),   /* divdi */
543   COSTS_N_INSNS (5),    /* fp */
544   COSTS_N_INSNS (5),    /* dmul */
545   COSTS_N_INSNS (19),   /* sdiv */
546   COSTS_N_INSNS (33),   /* ddiv */
547   32,			/* cache line size */
548   32,			/* l1 cache */
549   256,			/* l2 cache */
550   1,			/* streams */
551 };
552 
553 /* Instruction costs on PPC476 processors.  */
554 static const
555 struct processor_costs ppc476_cost = {
556   COSTS_N_INSNS (4),    /* mulsi */
557   COSTS_N_INSNS (4),    /* mulsi_const */
558   COSTS_N_INSNS (4),    /* mulsi_const9 */
559   COSTS_N_INSNS (4),    /* muldi */
560   COSTS_N_INSNS (11),   /* divsi */
561   COSTS_N_INSNS (11),   /* divdi */
562   COSTS_N_INSNS (6),    /* fp */
563   COSTS_N_INSNS (6),    /* dmul */
564   COSTS_N_INSNS (19),   /* sdiv */
565   COSTS_N_INSNS (33),   /* ddiv */
566   32,			/* l1 cache line size */
567   32,			/* l1 cache */
568   512,			/* l2 cache */
569   1,			/* streams */
570 };
571 
572 /* Instruction costs on PPC601 processors.  */
573 static const
574 struct processor_costs ppc601_cost = {
575   COSTS_N_INSNS (5),    /* mulsi */
576   COSTS_N_INSNS (5),    /* mulsi_const */
577   COSTS_N_INSNS (5),    /* mulsi_const9 */
578   COSTS_N_INSNS (5),    /* muldi */
579   COSTS_N_INSNS (36),   /* divsi */
580   COSTS_N_INSNS (36),   /* divdi */
581   COSTS_N_INSNS (4),    /* fp */
582   COSTS_N_INSNS (5),    /* dmul */
583   COSTS_N_INSNS (17),   /* sdiv */
584   COSTS_N_INSNS (31),   /* ddiv */
585   32,			/* cache line size */
586   32,			/* l1 cache */
587   256,			/* l2 cache */
588   1,			/* streams */
589 };
590 
591 /* Instruction costs on PPC603 processors.  */
592 static const
593 struct processor_costs ppc603_cost = {
594   COSTS_N_INSNS (5),    /* mulsi */
595   COSTS_N_INSNS (3),    /* mulsi_const */
596   COSTS_N_INSNS (2),    /* mulsi_const9 */
597   COSTS_N_INSNS (5),    /* muldi */
598   COSTS_N_INSNS (37),   /* divsi */
599   COSTS_N_INSNS (37),   /* divdi */
600   COSTS_N_INSNS (3),    /* fp */
601   COSTS_N_INSNS (4),    /* dmul */
602   COSTS_N_INSNS (18),   /* sdiv */
603   COSTS_N_INSNS (33),   /* ddiv */
604   32,			/* cache line size */
605   8,			/* l1 cache */
606   64,			/* l2 cache */
607   1,			/* streams */
608 };
609 
610 /* Instruction costs on PPC604 processors.  */
611 static const
612 struct processor_costs ppc604_cost = {
613   COSTS_N_INSNS (4),    /* mulsi */
614   COSTS_N_INSNS (4),    /* mulsi_const */
615   COSTS_N_INSNS (4),    /* mulsi_const9 */
616   COSTS_N_INSNS (4),    /* muldi */
617   COSTS_N_INSNS (20),   /* divsi */
618   COSTS_N_INSNS (20),   /* divdi */
619   COSTS_N_INSNS (3),    /* fp */
620   COSTS_N_INSNS (3),    /* dmul */
621   COSTS_N_INSNS (18),   /* sdiv */
622   COSTS_N_INSNS (32),   /* ddiv */
623   32,			/* cache line size */
624   16,			/* l1 cache */
625   512,			/* l2 cache */
626   1,			/* streams */
627 };
628 
629 /* Instruction costs on PPC604e processors.  */
630 static const
631 struct processor_costs ppc604e_cost = {
632   COSTS_N_INSNS (2),    /* mulsi */
633   COSTS_N_INSNS (2),    /* mulsi_const */
634   COSTS_N_INSNS (2),    /* mulsi_const9 */
635   COSTS_N_INSNS (2),    /* muldi */
636   COSTS_N_INSNS (20),   /* divsi */
637   COSTS_N_INSNS (20),   /* divdi */
638   COSTS_N_INSNS (3),    /* fp */
639   COSTS_N_INSNS (3),    /* dmul */
640   COSTS_N_INSNS (18),   /* sdiv */
641   COSTS_N_INSNS (32),   /* ddiv */
642   32,			/* cache line size */
643   32,			/* l1 cache */
644   1024,			/* l2 cache */
645   1,			/* streams */
646 };
647 
648 /* Instruction costs on PPC620 processors.  */
649 static const
650 struct processor_costs ppc620_cost = {
651   COSTS_N_INSNS (5),    /* mulsi */
652   COSTS_N_INSNS (4),    /* mulsi_const */
653   COSTS_N_INSNS (3),    /* mulsi_const9 */
654   COSTS_N_INSNS (7),    /* muldi */
655   COSTS_N_INSNS (21),   /* divsi */
656   COSTS_N_INSNS (37),   /* divdi */
657   COSTS_N_INSNS (3),    /* fp */
658   COSTS_N_INSNS (3),    /* dmul */
659   COSTS_N_INSNS (18),   /* sdiv */
660   COSTS_N_INSNS (32),   /* ddiv */
661   128,			/* cache line size */
662   32,			/* l1 cache */
663   1024,			/* l2 cache */
664   1,			/* streams */
665 };
666 
667 /* Instruction costs on PPC630 processors.  */
668 static const
669 struct processor_costs ppc630_cost = {
670   COSTS_N_INSNS (5),    /* mulsi */
671   COSTS_N_INSNS (4),    /* mulsi_const */
672   COSTS_N_INSNS (3),    /* mulsi_const9 */
673   COSTS_N_INSNS (7),    /* muldi */
674   COSTS_N_INSNS (21),   /* divsi */
675   COSTS_N_INSNS (37),   /* divdi */
676   COSTS_N_INSNS (3),    /* fp */
677   COSTS_N_INSNS (3),    /* dmul */
678   COSTS_N_INSNS (17),   /* sdiv */
679   COSTS_N_INSNS (21),   /* ddiv */
680   128,			/* cache line size */
681   64,			/* l1 cache */
682   1024,			/* l2 cache */
683   1,			/* streams */
684 };
685 
686 /* Instruction costs on Cell processor.  */
687 /* COSTS_N_INSNS (1) ~ one add.  */
688 static const
689 struct processor_costs ppccell_cost = {
690   COSTS_N_INSNS (9/2)+2,    /* mulsi */
691   COSTS_N_INSNS (6/2),    /* mulsi_const */
692   COSTS_N_INSNS (6/2),    /* mulsi_const9 */
693   COSTS_N_INSNS (15/2)+2,   /* muldi */
694   COSTS_N_INSNS (38/2),   /* divsi */
695   COSTS_N_INSNS (70/2),   /* divdi */
696   COSTS_N_INSNS (10/2),   /* fp */
697   COSTS_N_INSNS (10/2),   /* dmul */
698   COSTS_N_INSNS (74/2),   /* sdiv */
699   COSTS_N_INSNS (74/2),   /* ddiv */
700   128,			/* cache line size */
701   32,			/* l1 cache */
702   512,			/* l2 cache */
703   6,			/* streams */
704 };
705 
706 /* Instruction costs on PPC750 and PPC7400 processors.  */
707 static const
708 struct processor_costs ppc750_cost = {
709   COSTS_N_INSNS (5),    /* mulsi */
710   COSTS_N_INSNS (3),    /* mulsi_const */
711   COSTS_N_INSNS (2),    /* mulsi_const9 */
712   COSTS_N_INSNS (5),    /* muldi */
713   COSTS_N_INSNS (17),   /* divsi */
714   COSTS_N_INSNS (17),   /* divdi */
715   COSTS_N_INSNS (3),    /* fp */
716   COSTS_N_INSNS (3),    /* dmul */
717   COSTS_N_INSNS (17),   /* sdiv */
718   COSTS_N_INSNS (31),   /* ddiv */
719   32,			/* cache line size */
720   32,			/* l1 cache */
721   512,			/* l2 cache */
722   1,			/* streams */
723 };
724 
725 /* Instruction costs on PPC7450 processors.  */
726 static const
727 struct processor_costs ppc7450_cost = {
728   COSTS_N_INSNS (4),    /* mulsi */
729   COSTS_N_INSNS (3),    /* mulsi_const */
730   COSTS_N_INSNS (3),    /* mulsi_const9 */
731   COSTS_N_INSNS (4),    /* muldi */
732   COSTS_N_INSNS (23),   /* divsi */
733   COSTS_N_INSNS (23),   /* divdi */
734   COSTS_N_INSNS (5),    /* fp */
735   COSTS_N_INSNS (5),    /* dmul */
736   COSTS_N_INSNS (21),   /* sdiv */
737   COSTS_N_INSNS (35),   /* ddiv */
738   32,			/* cache line size */
739   32,			/* l1 cache */
740   1024,			/* l2 cache */
741   1,			/* streams */
742 };
743 
744 /* Instruction costs on PPC8540 processors.  */
745 static const
746 struct processor_costs ppc8540_cost = {
747   COSTS_N_INSNS (4),    /* mulsi */
748   COSTS_N_INSNS (4),    /* mulsi_const */
749   COSTS_N_INSNS (4),    /* mulsi_const9 */
750   COSTS_N_INSNS (4),    /* muldi */
751   COSTS_N_INSNS (19),   /* divsi */
752   COSTS_N_INSNS (19),   /* divdi */
753   COSTS_N_INSNS (4),    /* fp */
754   COSTS_N_INSNS (4),    /* dmul */
755   COSTS_N_INSNS (29),   /* sdiv */
756   COSTS_N_INSNS (29),   /* ddiv */
757   32,			/* cache line size */
758   32,			/* l1 cache */
759   256,			/* l2 cache */
760   1,			/* prefetch streams /*/
761 };
762 
763 /* Instruction costs on E300C2 and E300C3 cores.  */
764 static const
765 struct processor_costs ppce300c2c3_cost = {
766   COSTS_N_INSNS (4),    /* mulsi */
767   COSTS_N_INSNS (4),    /* mulsi_const */
768   COSTS_N_INSNS (4),    /* mulsi_const9 */
769   COSTS_N_INSNS (4),    /* muldi */
770   COSTS_N_INSNS (19),   /* divsi */
771   COSTS_N_INSNS (19),   /* divdi */
772   COSTS_N_INSNS (3),    /* fp */
773   COSTS_N_INSNS (4),    /* dmul */
774   COSTS_N_INSNS (18),   /* sdiv */
775   COSTS_N_INSNS (33),   /* ddiv */
776   32,
777   16,			/* l1 cache */
778   16,			/* l2 cache */
779   1,			/* prefetch streams /*/
780 };
781 
782 /* Instruction costs on PPCE500MC processors.  */
783 static const
784 struct processor_costs ppce500mc_cost = {
785   COSTS_N_INSNS (4),    /* mulsi */
786   COSTS_N_INSNS (4),    /* mulsi_const */
787   COSTS_N_INSNS (4),    /* mulsi_const9 */
788   COSTS_N_INSNS (4),    /* muldi */
789   COSTS_N_INSNS (14),   /* divsi */
790   COSTS_N_INSNS (14),   /* divdi */
791   COSTS_N_INSNS (8),    /* fp */
792   COSTS_N_INSNS (10),   /* dmul */
793   COSTS_N_INSNS (36),   /* sdiv */
794   COSTS_N_INSNS (66),   /* ddiv */
795   64,			/* cache line size */
796   32,			/* l1 cache */
797   128,			/* l2 cache */
798   1,			/* prefetch streams /*/
799 };
800 
801 /* Instruction costs on PPCE500MC64 processors.  */
802 static const
803 struct processor_costs ppce500mc64_cost = {
804   COSTS_N_INSNS (4),    /* mulsi */
805   COSTS_N_INSNS (4),    /* mulsi_const */
806   COSTS_N_INSNS (4),    /* mulsi_const9 */
807   COSTS_N_INSNS (4),    /* muldi */
808   COSTS_N_INSNS (14),   /* divsi */
809   COSTS_N_INSNS (14),   /* divdi */
810   COSTS_N_INSNS (4),    /* fp */
811   COSTS_N_INSNS (10),   /* dmul */
812   COSTS_N_INSNS (36),   /* sdiv */
813   COSTS_N_INSNS (66),   /* ddiv */
814   64,			/* cache line size */
815   32,			/* l1 cache */
816   128,			/* l2 cache */
817   1,			/* prefetch streams /*/
818 };
819 
820 /* Instruction costs on PPCE5500 processors.  */
821 static const
822 struct processor_costs ppce5500_cost = {
823   COSTS_N_INSNS (5),    /* mulsi */
824   COSTS_N_INSNS (5),    /* mulsi_const */
825   COSTS_N_INSNS (4),    /* mulsi_const9 */
826   COSTS_N_INSNS (5),    /* muldi */
827   COSTS_N_INSNS (14),   /* divsi */
828   COSTS_N_INSNS (14),   /* divdi */
829   COSTS_N_INSNS (7),    /* fp */
830   COSTS_N_INSNS (10),   /* dmul */
831   COSTS_N_INSNS (36),   /* sdiv */
832   COSTS_N_INSNS (66),   /* ddiv */
833   64,			/* cache line size */
834   32,			/* l1 cache */
835   128,			/* l2 cache */
836   1,			/* prefetch streams /*/
837 };
838 
839 /* Instruction costs on PPCE6500 processors.  */
840 static const
841 struct processor_costs ppce6500_cost = {
842   COSTS_N_INSNS (5),    /* mulsi */
843   COSTS_N_INSNS (5),    /* mulsi_const */
844   COSTS_N_INSNS (4),    /* mulsi_const9 */
845   COSTS_N_INSNS (5),    /* muldi */
846   COSTS_N_INSNS (14),   /* divsi */
847   COSTS_N_INSNS (14),   /* divdi */
848   COSTS_N_INSNS (7),    /* fp */
849   COSTS_N_INSNS (10),   /* dmul */
850   COSTS_N_INSNS (36),   /* sdiv */
851   COSTS_N_INSNS (66),   /* ddiv */
852   64,			/* cache line size */
853   32,			/* l1 cache */
854   128,			/* l2 cache */
855   1,			/* prefetch streams /*/
856 };
857 
858 /* Instruction costs on AppliedMicro Titan processors.  */
859 static const
860 struct processor_costs titan_cost = {
861   COSTS_N_INSNS (5),    /* mulsi */
862   COSTS_N_INSNS (5),    /* mulsi_const */
863   COSTS_N_INSNS (5),    /* mulsi_const9 */
864   COSTS_N_INSNS (5),    /* muldi */
865   COSTS_N_INSNS (18),   /* divsi */
866   COSTS_N_INSNS (18),   /* divdi */
867   COSTS_N_INSNS (10),   /* fp */
868   COSTS_N_INSNS (10),   /* dmul */
869   COSTS_N_INSNS (46),   /* sdiv */
870   COSTS_N_INSNS (72),   /* ddiv */
871   32,			/* cache line size */
872   32,			/* l1 cache */
873   512,			/* l2 cache */
874   1,			/* prefetch streams /*/
875 };
876 
877 /* Instruction costs on POWER4 and POWER5 processors.  */
878 static const
879 struct processor_costs power4_cost = {
880   COSTS_N_INSNS (3),    /* mulsi */
881   COSTS_N_INSNS (2),    /* mulsi_const */
882   COSTS_N_INSNS (2),    /* mulsi_const9 */
883   COSTS_N_INSNS (4),    /* muldi */
884   COSTS_N_INSNS (18),   /* divsi */
885   COSTS_N_INSNS (34),   /* divdi */
886   COSTS_N_INSNS (3),    /* fp */
887   COSTS_N_INSNS (3),    /* dmul */
888   COSTS_N_INSNS (17),   /* sdiv */
889   COSTS_N_INSNS (17),   /* ddiv */
890   128,			/* cache line size */
891   32,			/* l1 cache */
892   1024,			/* l2 cache */
893   8,			/* prefetch streams /*/
894 };
895 
896 /* Instruction costs on POWER6 processors.  */
897 static const
898 struct processor_costs power6_cost = {
899   COSTS_N_INSNS (8),    /* mulsi */
900   COSTS_N_INSNS (8),    /* mulsi_const */
901   COSTS_N_INSNS (8),    /* mulsi_const9 */
902   COSTS_N_INSNS (8),    /* muldi */
903   COSTS_N_INSNS (22),   /* divsi */
904   COSTS_N_INSNS (28),   /* divdi */
905   COSTS_N_INSNS (3),    /* fp */
906   COSTS_N_INSNS (3),    /* dmul */
907   COSTS_N_INSNS (13),   /* sdiv */
908   COSTS_N_INSNS (16),   /* ddiv */
909   128,			/* cache line size */
910   64,			/* l1 cache */
911   2048,			/* l2 cache */
912   16,			/* prefetch streams */
913 };
914 
915 /* Instruction costs on POWER7 processors.  */
916 static const
917 struct processor_costs power7_cost = {
918   COSTS_N_INSNS (2),	/* mulsi */
919   COSTS_N_INSNS (2),	/* mulsi_const */
920   COSTS_N_INSNS (2),	/* mulsi_const9 */
921   COSTS_N_INSNS (2),	/* muldi */
922   COSTS_N_INSNS (18),	/* divsi */
923   COSTS_N_INSNS (34),	/* divdi */
924   COSTS_N_INSNS (3),	/* fp */
925   COSTS_N_INSNS (3),	/* dmul */
926   COSTS_N_INSNS (13),	/* sdiv */
927   COSTS_N_INSNS (16),	/* ddiv */
928   128,			/* cache line size */
929   32,			/* l1 cache */
930   256,			/* l2 cache */
931   12,			/* prefetch streams */
932 };
933 
934 /* Instruction costs on POWER8 processors.  */
935 static const
936 struct processor_costs power8_cost = {
937   COSTS_N_INSNS (3),	/* mulsi */
938   COSTS_N_INSNS (3),	/* mulsi_const */
939   COSTS_N_INSNS (3),	/* mulsi_const9 */
940   COSTS_N_INSNS (3),	/* muldi */
941   COSTS_N_INSNS (19),	/* divsi */
942   COSTS_N_INSNS (35),	/* divdi */
943   COSTS_N_INSNS (3),	/* fp */
944   COSTS_N_INSNS (3),	/* dmul */
945   COSTS_N_INSNS (14),	/* sdiv */
946   COSTS_N_INSNS (17),	/* ddiv */
947   128,			/* cache line size */
948   32,			/* l1 cache */
949   256,			/* l2 cache */
950   12,			/* prefetch streams */
951 };
952 
953 /* Instruction costs on POWER A2 processors.  */
954 static const
955 struct processor_costs ppca2_cost = {
956   COSTS_N_INSNS (16),    /* mulsi */
957   COSTS_N_INSNS (16),    /* mulsi_const */
958   COSTS_N_INSNS (16),    /* mulsi_const9 */
959   COSTS_N_INSNS (16),   /* muldi */
960   COSTS_N_INSNS (22),   /* divsi */
961   COSTS_N_INSNS (28),   /* divdi */
962   COSTS_N_INSNS (3),    /* fp */
963   COSTS_N_INSNS (3),    /* dmul */
964   COSTS_N_INSNS (59),   /* sdiv */
965   COSTS_N_INSNS (72),   /* ddiv */
966   64,
967   16,			/* l1 cache */
968   2048,			/* l2 cache */
969   16,			/* prefetch streams */
970 };
971 
972 
973 /* Table that classifies rs6000 builtin functions (pure, const, etc.).  */
974 #undef RS6000_BUILTIN_1
975 #undef RS6000_BUILTIN_2
976 #undef RS6000_BUILTIN_3
977 #undef RS6000_BUILTIN_A
978 #undef RS6000_BUILTIN_D
979 #undef RS6000_BUILTIN_E
980 #undef RS6000_BUILTIN_H
981 #undef RS6000_BUILTIN_P
982 #undef RS6000_BUILTIN_Q
983 #undef RS6000_BUILTIN_S
984 #undef RS6000_BUILTIN_X
985 
986 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
987   { NAME, ICODE, MASK, ATTR },
988 
989 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)  \
990   { NAME, ICODE, MASK, ATTR },
991 
992 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)  \
993   { NAME, ICODE, MASK, ATTR },
994 
995 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)  \
996   { NAME, ICODE, MASK, ATTR },
997 
998 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)  \
999   { NAME, ICODE, MASK, ATTR },
1000 
1001 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)  \
1002   { NAME, ICODE, MASK, ATTR },
1003 
1004 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)  \
1005   { NAME, ICODE, MASK, ATTR },
1006 
1007 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)  \
1008   { NAME, ICODE, MASK, ATTR },
1009 
1010 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)  \
1011   { NAME, ICODE, MASK, ATTR },
1012 
1013 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)  \
1014   { NAME, ICODE, MASK, ATTR },
1015 
1016 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)  \
1017   { NAME, ICODE, MASK, ATTR },
1018 
1019 struct rs6000_builtin_info_type {
1020   const char *name;
1021   const enum insn_code icode;
1022   const HOST_WIDE_INT mask;
1023   const unsigned attr;
1024 };
1025 
1026 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1027 {
1028 #include "rs6000-builtin.def"
1029 };
1030 
1031 #undef RS6000_BUILTIN_1
1032 #undef RS6000_BUILTIN_2
1033 #undef RS6000_BUILTIN_3
1034 #undef RS6000_BUILTIN_A
1035 #undef RS6000_BUILTIN_D
1036 #undef RS6000_BUILTIN_E
1037 #undef RS6000_BUILTIN_H
1038 #undef RS6000_BUILTIN_P
1039 #undef RS6000_BUILTIN_Q
1040 #undef RS6000_BUILTIN_S
1041 #undef RS6000_BUILTIN_X
1042 
1043 /* Support for -mveclibabi=<xxx> to control which vector library to use.  */
1044 static tree (*rs6000_veclib_handler) (tree, tree, tree);
1045 
1046 
1047 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
1048 static bool spe_func_has_64bit_regs_p (void);
1049 static struct machine_function * rs6000_init_machine_status (void);
1050 static int rs6000_ra_ever_killed (void);
1051 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1052 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1053 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1054 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
1055 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
1056 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
1057 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
1058 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
1059 				      bool);
1060 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
1061 static bool is_microcoded_insn (rtx);
1062 static bool is_nonpipeline_insn (rtx);
1063 static bool is_cracked_insn (rtx);
1064 static bool is_load_insn (rtx, rtx *);
1065 static bool is_store_insn (rtx, rtx *);
1066 static bool set_to_load_agen (rtx,rtx);
1067 static bool insn_terminates_group_p (rtx , enum group_termination);
1068 static bool insn_must_be_first_in_group (rtx);
1069 static bool insn_must_be_last_in_group (rtx);
1070 static void altivec_init_builtins (void);
1071 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1072 				   enum machine_mode, enum machine_mode,
1073 				   enum rs6000_builtins, const char *name);
1074 static void rs6000_common_init_builtins (void);
1075 static void paired_init_builtins (void);
1076 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1077 static void spe_init_builtins (void);
1078 static void htm_init_builtins (void);
1079 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1080 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1081 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1082 static rs6000_stack_t *rs6000_stack_info (void);
1083 static void is_altivec_return_reg (rtx, void *);
1084 int easy_vector_constant (rtx, enum machine_mode);
1085 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1086 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1087 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1088 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1089 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1090 				       bool, bool);
1091 #if TARGET_MACHO
1092 static void macho_branch_islands (void);
1093 #endif
1094 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1095 					     int, int *);
1096 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1097 						   int, int, int *);
1098 static bool rs6000_mode_dependent_address (const_rtx);
1099 static bool rs6000_debug_mode_dependent_address (const_rtx);
1100 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1101 						     enum machine_mode, rtx);
1102 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1103 							   enum machine_mode,
1104 							   rtx);
1105 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1106 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1107 							   enum reg_class);
1108 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1109 					    enum machine_mode);
1110 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1111 						  enum reg_class,
1112 						  enum machine_mode);
1113 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1114 					     enum machine_mode,
1115 					     enum reg_class);
1116 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1117 						   enum machine_mode,
1118 						   enum reg_class);
1119 static bool rs6000_save_toc_in_prologue_p (void);
1120 
1121 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1122 					     int, int *)
1123   = rs6000_legitimize_reload_address;
1124 
1125 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1126   = rs6000_mode_dependent_address;
1127 
1128 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1129 						     enum machine_mode, rtx)
1130   = rs6000_secondary_reload_class;
1131 
1132 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1133   = rs6000_preferred_reload_class;
1134 
1135 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1136 					    enum machine_mode)
1137   = rs6000_secondary_memory_needed;
1138 
1139 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1140 					     enum machine_mode,
1141 					     enum reg_class)
1142   = rs6000_cannot_change_mode_class;
1143 
1144 const int INSN_NOT_AVAILABLE = -1;
1145 
1146 static void rs6000_print_isa_options (FILE *, int, const char *,
1147 				      HOST_WIDE_INT);
1148 static void rs6000_print_builtin_options (FILE *, int, const char *,
1149 					  HOST_WIDE_INT);
1150 
1151 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1152 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1153 					  enum rs6000_reg_type,
1154 					  enum machine_mode,
1155 					  secondary_reload_info *,
1156 					  bool);
1157 static unsigned int rs6000_analyze_swaps (function *);
1158 
1159 /* Hash table stuff for keeping track of TOC entries.  */
1160 
1161 struct GTY(()) toc_hash_struct
1162 {
1163   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1164      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
1165   rtx key;
1166   enum machine_mode key_mode;
1167   int labelno;
1168 };
1169 
1170 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1171 
1172 /* Hash table to keep track of the argument types for builtin functions.  */
1173 
1174 struct GTY(()) builtin_hash_struct
1175 {
1176   tree type;
1177   enum machine_mode mode[4];	/* return value + 3 arguments.  */
1178   unsigned char uns_p[4];	/* and whether the types are unsigned.  */
1179 };
1180 
1181 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1182 
1183 
1184 /* Default register names.  */
1185 char rs6000_reg_names[][8] =
1186 {
1187       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1188       "8",  "9", "10", "11", "12", "13", "14", "15",
1189      "16", "17", "18", "19", "20", "21", "22", "23",
1190      "24", "25", "26", "27", "28", "29", "30", "31",
1191       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1192       "8",  "9", "10", "11", "12", "13", "14", "15",
1193      "16", "17", "18", "19", "20", "21", "22", "23",
1194      "24", "25", "26", "27", "28", "29", "30", "31",
1195      "mq", "lr", "ctr","ap",
1196       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1197       "ca",
1198       /* AltiVec registers.  */
1199       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
1200       "8",  "9",  "10", "11", "12", "13", "14", "15",
1201       "16", "17", "18", "19", "20", "21", "22", "23",
1202       "24", "25", "26", "27", "28", "29", "30", "31",
1203       "vrsave", "vscr",
1204       /* SPE registers.  */
1205       "spe_acc", "spefscr",
1206       /* Soft frame pointer.  */
1207       "sfp",
1208       /* HTM SPR registers.  */
1209       "tfhar", "tfiar", "texasr"
1210 };
1211 
1212 #ifdef TARGET_REGNAMES
1213 static const char alt_reg_names[][8] =
1214 {
1215    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
1216    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1217   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1218   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1219    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
1220    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1221   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1222   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1223     "mq",    "lr",  "ctr",   "ap",
1224   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1225    "ca",
1226   /* AltiVec registers.  */
1227    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
1228    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1229   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1230   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1231   "vrsave", "vscr",
1232   /* SPE registers.  */
1233   "spe_acc", "spefscr",
1234   /* Soft frame pointer.  */
1235   "sfp",
1236   /* HTM SPR registers.  */
1237   "tfhar", "tfiar", "texasr"
1238 };
1239 #endif
1240 
1241 /* Table of valid machine attributes.  */
1242 
1243 static const struct attribute_spec rs6000_attribute_table[] =
1244 {
1245   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1246        affects_type_identity } */
1247   { "altivec",   1, 1, false, true,  false, rs6000_handle_altivec_attribute,
1248     false },
1249   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute,
1250     false },
1251   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute,
1252     false },
1253   { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1254     false },
1255   { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1256     false },
1257 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1258   SUBTARGET_ATTRIBUTE_TABLE,
1259 #endif
1260   { NULL,        0, 0, false, false, false, NULL, false }
1261 };
1262 
1263 #ifndef TARGET_PROFILE_KERNEL
1264 #define TARGET_PROFILE_KERNEL 0
1265 #endif
1266 
1267 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
1268 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1269 
1270 /* Initialize the GCC target structure.  */
1271 #undef TARGET_ATTRIBUTE_TABLE
1272 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1273 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1274 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1275 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1276 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1277 
1278 #undef TARGET_ASM_ALIGNED_DI_OP
1279 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1280 
1281 /* Default unaligned ops are only provided for ELF.  Find the ops needed
1282    for non-ELF systems.  */
1283 #ifndef OBJECT_FORMAT_ELF
1284 #if TARGET_XCOFF
1285 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
1286    64-bit targets.  */
1287 #undef TARGET_ASM_UNALIGNED_HI_OP
1288 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1289 #undef TARGET_ASM_UNALIGNED_SI_OP
1290 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1291 #undef TARGET_ASM_UNALIGNED_DI_OP
1292 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1293 #else
1294 /* For Darwin.  */
1295 #undef TARGET_ASM_UNALIGNED_HI_OP
1296 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1297 #undef TARGET_ASM_UNALIGNED_SI_OP
1298 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1299 #undef TARGET_ASM_UNALIGNED_DI_OP
1300 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1301 #undef TARGET_ASM_ALIGNED_DI_OP
1302 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1303 #endif
1304 #endif
1305 
1306 /* This hook deals with fixups for relocatable code and DI-mode objects
1307    in 64-bit code.  */
1308 #undef TARGET_ASM_INTEGER
1309 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1310 
1311 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1312 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1313 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1314 #endif
1315 
1316 #undef TARGET_SET_UP_BY_PROLOGUE
1317 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1318 
1319 #undef TARGET_HAVE_TLS
1320 #define TARGET_HAVE_TLS HAVE_AS_TLS
1321 
1322 #undef TARGET_CANNOT_FORCE_CONST_MEM
1323 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1324 
1325 #undef TARGET_DELEGITIMIZE_ADDRESS
1326 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1327 
1328 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1329 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1330 
1331 #undef TARGET_ASM_FUNCTION_PROLOGUE
1332 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1333 #undef TARGET_ASM_FUNCTION_EPILOGUE
1334 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1335 
1336 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1337 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1338 
1339 #undef TARGET_LEGITIMIZE_ADDRESS
1340 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1341 
1342 #undef  TARGET_SCHED_VARIABLE_ISSUE
1343 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1344 
1345 #undef TARGET_SCHED_ISSUE_RATE
1346 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1347 #undef TARGET_SCHED_ADJUST_COST
1348 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1349 #undef TARGET_SCHED_ADJUST_PRIORITY
1350 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1351 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1352 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1353 #undef TARGET_SCHED_INIT
1354 #define TARGET_SCHED_INIT rs6000_sched_init
1355 #undef TARGET_SCHED_FINISH
1356 #define TARGET_SCHED_FINISH rs6000_sched_finish
1357 #undef TARGET_SCHED_REORDER
1358 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1359 #undef TARGET_SCHED_REORDER2
1360 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1361 
1362 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1363 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1364 
1365 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1366 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1367 
1368 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1369 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1370 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1371 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1372 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1373 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1374 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1375 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1376 
1377 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1378 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1379 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1380 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT		\
1381   rs6000_builtin_support_vector_misalignment
1382 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1383 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1384 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1385 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1386   rs6000_builtin_vectorization_cost
1387 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1388 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1389   rs6000_preferred_simd_mode
1390 #undef TARGET_VECTORIZE_INIT_COST
1391 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1392 #undef TARGET_VECTORIZE_ADD_STMT_COST
1393 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1394 #undef TARGET_VECTORIZE_FINISH_COST
1395 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1396 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1397 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1398 
1399 #undef TARGET_INIT_BUILTINS
1400 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1401 #undef TARGET_BUILTIN_DECL
1402 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1403 
1404 #undef TARGET_EXPAND_BUILTIN
1405 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1406 
1407 #undef TARGET_MANGLE_TYPE
1408 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1409 
1410 #undef TARGET_INIT_LIBFUNCS
1411 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1412 
1413 #if TARGET_MACHO
1414 #undef TARGET_BINDS_LOCAL_P
1415 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1416 #endif
1417 
1418 #undef TARGET_MS_BITFIELD_LAYOUT_P
1419 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1420 
1421 #undef TARGET_ASM_OUTPUT_MI_THUNK
1422 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1423 
1424 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1425 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1426 
1427 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1428 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1429 
1430 #undef TARGET_INVALID_WITHIN_DOLOOP
1431 #define TARGET_INVALID_WITHIN_DOLOOP rs6000_invalid_within_doloop
1432 
1433 #undef TARGET_REGISTER_MOVE_COST
1434 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1435 #undef TARGET_MEMORY_MOVE_COST
1436 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1437 #undef TARGET_RTX_COSTS
1438 #define TARGET_RTX_COSTS rs6000_rtx_costs
1439 #undef TARGET_ADDRESS_COST
1440 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1441 
1442 #undef TARGET_DWARF_REGISTER_SPAN
1443 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1444 
1445 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1446 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1447 
1448 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1449 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1450 
1451 /* On rs6000, function arguments are promoted, as are function return
1452    values.  */
1453 #undef TARGET_PROMOTE_FUNCTION_MODE
1454 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1455 
1456 #undef TARGET_RETURN_IN_MEMORY
1457 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1458 
1459 #undef TARGET_RETURN_IN_MSB
1460 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1461 
1462 #undef TARGET_SETUP_INCOMING_VARARGS
1463 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1464 
1465 /* Always strict argument naming on rs6000.  */
1466 #undef TARGET_STRICT_ARGUMENT_NAMING
1467 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1468 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1469 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1470 #undef TARGET_SPLIT_COMPLEX_ARG
1471 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1472 #undef TARGET_MUST_PASS_IN_STACK
1473 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1474 #undef TARGET_PASS_BY_REFERENCE
1475 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1476 #undef TARGET_ARG_PARTIAL_BYTES
1477 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1478 #undef TARGET_FUNCTION_ARG_ADVANCE
1479 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1480 #undef TARGET_FUNCTION_ARG
1481 #define TARGET_FUNCTION_ARG rs6000_function_arg
1482 #undef TARGET_FUNCTION_ARG_BOUNDARY
1483 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1484 
1485 #undef TARGET_BUILD_BUILTIN_VA_LIST
1486 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1487 
1488 #undef TARGET_EXPAND_BUILTIN_VA_START
1489 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1490 
1491 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1492 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1493 
1494 #undef TARGET_EH_RETURN_FILTER_MODE
1495 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1496 
1497 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1498 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1499 
1500 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1501 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1502 
1503 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1504 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1505 
1506 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1507 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1508 
1509 #undef TARGET_OPTION_OVERRIDE
1510 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1511 
1512 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1513 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1514   rs6000_builtin_vectorized_function
1515 
1516 #if !TARGET_MACHO
1517 #undef TARGET_STACK_PROTECT_FAIL
1518 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1519 #endif
1520 
1521 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1522    The PowerPC architecture requires only weak consistency among
1523    processors--that is, memory accesses between processors need not be
1524    sequentially consistent and memory accesses among processors can occur
1525    in any order. The ability to order memory accesses weakly provides
1526    opportunities for more efficient use of the system bus. Unless a
1527    dependency exists, the 604e allows read operations to precede store
1528    operations.  */
1529 #undef TARGET_RELAXED_ORDERING
1530 #define TARGET_RELAXED_ORDERING true
1531 
1532 #ifdef HAVE_AS_TLS
1533 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1534 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1535 #endif
1536 
1537 /* Use a 32-bit anchor range.  This leads to sequences like:
1538 
1539 	addis	tmp,anchor,high
1540 	add	dest,tmp,low
1541 
1542    where tmp itself acts as an anchor, and can be shared between
1543    accesses to the same 64k page.  */
1544 #undef TARGET_MIN_ANCHOR_OFFSET
1545 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1546 #undef TARGET_MAX_ANCHOR_OFFSET
1547 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1548 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1549 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1550 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1551 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1552 
1553 #undef TARGET_BUILTIN_RECIPROCAL
1554 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1555 
1556 #undef TARGET_EXPAND_TO_RTL_HOOK
1557 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1558 
1559 #undef TARGET_INSTANTIATE_DECLS
1560 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1561 
1562 #undef TARGET_SECONDARY_RELOAD
1563 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1564 
1565 #undef TARGET_LEGITIMATE_ADDRESS_P
1566 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1567 
1568 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1569 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1570 
1571 #undef TARGET_LRA_P
1572 #define TARGET_LRA_P rs6000_lra_p
1573 
1574 #undef TARGET_CAN_ELIMINATE
1575 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1576 
1577 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1578 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1579 
1580 #undef TARGET_TRAMPOLINE_INIT
1581 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1582 
1583 #undef TARGET_FUNCTION_VALUE
1584 #define TARGET_FUNCTION_VALUE rs6000_function_value
1585 
1586 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1587 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1588 
1589 #undef TARGET_OPTION_SAVE
1590 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1591 
1592 #undef TARGET_OPTION_RESTORE
1593 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1594 
1595 #undef TARGET_OPTION_PRINT
1596 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1597 
1598 #undef TARGET_CAN_INLINE_P
1599 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1600 
1601 #undef TARGET_SET_CURRENT_FUNCTION
1602 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1603 
1604 #undef TARGET_LEGITIMATE_CONSTANT_P
1605 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1606 
1607 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1608 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1609 
1610 
1611 /* Processor table.  */
1612 struct rs6000_ptt
1613 {
1614   const char *const name;		/* Canonical processor name.  */
1615   const enum processor_type processor;	/* Processor type enum value.  */
1616   const HOST_WIDE_INT target_enable;	/* Target flags to enable.  */
1617 };
1618 
1619 static struct rs6000_ptt const processor_target_table[] =
1620 {
1621 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1622 #include "rs6000-cpus.def"
1623 #undef RS6000_CPU
1624 };
1625 
1626 /* Look up a processor name for -mcpu=xxx and -mtune=xxx.  Return -1 if the
1627    name is invalid.  */
1628 
1629 static int
rs6000_cpu_name_lookup(const char * name)1630 rs6000_cpu_name_lookup (const char *name)
1631 {
1632   size_t i;
1633 
1634   if (name != NULL)
1635     {
1636       for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1637 	if (! strcmp (name, processor_target_table[i].name))
1638 	  return (int)i;
1639     }
1640 
1641   return -1;
1642 }
1643 
1644 
1645 /* Return number of consecutive hard regs needed starting at reg REGNO
1646    to hold something of mode MODE.
1647    This is ordinarily the length in words of a value of mode MODE
1648    but can be less for certain modes in special long registers.
1649 
1650    For the SPE, GPRs are 64 bits but only 32 bits are visible in
1651    scalar instructions.  The upper 32 bits are only available to the
1652    SIMD instructions.
1653 
1654    POWER and PowerPC GPRs hold 32 bits worth;
1655    PowerPC64 GPRs and FPRs point register holds 64 bits worth.  */
1656 
1657 static int
rs6000_hard_regno_nregs_internal(int regno,enum machine_mode mode)1658 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1659 {
1660   unsigned HOST_WIDE_INT reg_size;
1661 
1662   /* TF/TD modes are special in that they always take 2 registers.  */
1663   if (FP_REGNO_P (regno))
1664     reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1665 		? UNITS_PER_VSX_WORD
1666 		: UNITS_PER_FP_WORD);
1667 
1668   else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1669     reg_size = UNITS_PER_SPE_WORD;
1670 
1671   else if (ALTIVEC_REGNO_P (regno))
1672     reg_size = UNITS_PER_ALTIVEC_WORD;
1673 
1674   /* The value returned for SCmode in the E500 double case is 2 for
1675      ABI compatibility; storing an SCmode value in a single register
1676      would require function_arg and rs6000_spe_function_arg to handle
1677      SCmode so as to pass the value correctly in a pair of
1678      registers.  */
1679   else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1680 	   && !DECIMAL_FLOAT_MODE_P (mode))
1681     reg_size = UNITS_PER_FP_WORD;
1682 
1683   else
1684     reg_size = UNITS_PER_WORD;
1685 
1686   return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1687 }
1688 
1689 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1690    MODE.  */
1691 static int
rs6000_hard_regno_mode_ok(int regno,enum machine_mode mode)1692 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1693 {
1694   int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1695 
1696   /* PTImode can only go in GPRs.  Quad word memory operations require even/odd
1697      register combinations, and use PTImode where we need to deal with quad
1698      word memory operations.  Don't allow quad words in the argument or frame
1699      pointer registers, just registers 0..31.  */
1700   if (mode == PTImode)
1701     return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1702 	    && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1703 	    && ((regno & 1) == 0));
1704 
1705   /* VSX registers that overlap the FPR registers are larger than for non-VSX
1706      implementations.  Don't allow an item to be split between a FP register
1707      and an Altivec register.  Allow TImode in all VSX registers if the user
1708      asked for it.  */
1709   if (TARGET_VSX && VSX_REGNO_P (regno)
1710       && (VECTOR_MEM_VSX_P (mode)
1711 	  || reg_addr[mode].scalar_in_vmx_p
1712 	  || (TARGET_VSX_TIMODE && mode == TImode)
1713 	  || (TARGET_VADDUQM && mode == V1TImode)))
1714     {
1715       if (FP_REGNO_P (regno))
1716 	return FP_REGNO_P (last_regno);
1717 
1718       if (ALTIVEC_REGNO_P (regno))
1719 	{
1720 	  if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
1721 	    return 0;
1722 
1723 	  return ALTIVEC_REGNO_P (last_regno);
1724 	}
1725     }
1726 
1727   /* The GPRs can hold any mode, but values bigger than one register
1728      cannot go past R31.  */
1729   if (INT_REGNO_P (regno))
1730     return INT_REGNO_P (last_regno);
1731 
1732   /* The float registers (except for VSX vector modes) can only hold floating
1733      modes and DImode.  */
1734   if (FP_REGNO_P (regno))
1735     {
1736       if (SCALAR_FLOAT_MODE_P (mode)
1737 	  && (mode != TDmode || (regno % 2) == 0)
1738 	  && FP_REGNO_P (last_regno))
1739 	return 1;
1740 
1741       if (GET_MODE_CLASS (mode) == MODE_INT
1742 	  && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1743 	return 1;
1744 
1745       if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1746 	  && PAIRED_VECTOR_MODE (mode))
1747 	return 1;
1748 
1749       return 0;
1750     }
1751 
1752   /* The CR register can only hold CC modes.  */
1753   if (CR_REGNO_P (regno))
1754     return GET_MODE_CLASS (mode) == MODE_CC;
1755 
1756   if (CA_REGNO_P (regno))
1757     return mode == BImode;
1758 
1759   /* AltiVec only in AldyVec registers.  */
1760   if (ALTIVEC_REGNO_P (regno))
1761     return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
1762 	    || mode == V1TImode);
1763 
1764   /* ...but GPRs can hold SIMD data on the SPE in one register.  */
1765   if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1766     return 1;
1767 
1768   /* We cannot put non-VSX TImode or PTImode anywhere except general register
1769      and it must be able to fit within the register set.  */
1770 
1771   return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1772 }
1773 
1774 /* Print interesting facts about registers.  */
1775 static void
rs6000_debug_reg_print(int first_regno,int last_regno,const char * reg_name)1776 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1777 {
1778   int r, m;
1779 
1780   for (r = first_regno; r <= last_regno; ++r)
1781     {
1782       const char *comma = "";
1783       int len;
1784 
1785       if (first_regno == last_regno)
1786 	fprintf (stderr, "%s:\t", reg_name);
1787       else
1788 	fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1789 
1790       len = 8;
1791       for (m = 0; m < NUM_MACHINE_MODES; ++m)
1792 	if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1793 	  {
1794 	    if (len > 70)
1795 	      {
1796 		fprintf (stderr, ",\n\t");
1797 		len = 8;
1798 		comma = "";
1799 	      }
1800 
1801 	    if (rs6000_hard_regno_nregs[m][r] > 1)
1802 	      len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1803 			     rs6000_hard_regno_nregs[m][r]);
1804 	    else
1805 	      len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1806 
1807 	    comma = ", ";
1808 	  }
1809 
1810       if (call_used_regs[r])
1811 	{
1812 	  if (len > 70)
1813 	    {
1814 	      fprintf (stderr, ",\n\t");
1815 	      len = 8;
1816 	      comma = "";
1817 	    }
1818 
1819 	  len += fprintf (stderr, "%s%s", comma, "call-used");
1820 	  comma = ", ";
1821 	}
1822 
1823       if (fixed_regs[r])
1824 	{
1825 	  if (len > 70)
1826 	    {
1827 	      fprintf (stderr, ",\n\t");
1828 	      len = 8;
1829 	      comma = "";
1830 	    }
1831 
1832 	  len += fprintf (stderr, "%s%s", comma, "fixed");
1833 	  comma = ", ";
1834 	}
1835 
1836       if (len > 70)
1837 	{
1838 	  fprintf (stderr, ",\n\t");
1839 	  comma = "";
1840 	}
1841 
1842       len += fprintf (stderr, "%sreg-class = %s", comma,
1843 		      reg_class_names[(int)rs6000_regno_regclass[r]]);
1844       comma = ", ";
1845 
1846       if (len > 70)
1847 	{
1848 	  fprintf (stderr, ",\n\t");
1849 	  comma = "";
1850 	}
1851 
1852       fprintf (stderr, "%sregno = %d\n", comma, r);
1853     }
1854 }
1855 
1856 static const char *
rs6000_debug_vector_unit(enum rs6000_vector v)1857 rs6000_debug_vector_unit (enum rs6000_vector v)
1858 {
1859   const char *ret;
1860 
1861   switch (v)
1862     {
1863     case VECTOR_NONE:	   ret = "none";      break;
1864     case VECTOR_ALTIVEC:   ret = "altivec";   break;
1865     case VECTOR_VSX:	   ret = "vsx";       break;
1866     case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
1867     case VECTOR_PAIRED:	   ret = "paired";    break;
1868     case VECTOR_SPE:	   ret = "spe";       break;
1869     case VECTOR_OTHER:	   ret = "other";     break;
1870     default:		   ret = "unknown";   break;
1871     }
1872 
1873   return ret;
1874 }
1875 
1876 /* Print the address masks in a human readble fashion.  */
1877 DEBUG_FUNCTION void
rs6000_debug_print_mode(ssize_t m)1878 rs6000_debug_print_mode (ssize_t m)
1879 {
1880   ssize_t rc;
1881 
1882   fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
1883   for (rc = 0; rc < N_RELOAD_REG; rc++)
1884     {
1885       addr_mask_type mask = reg_addr[m].addr_mask[rc];
1886       fprintf (stderr,
1887 	       "  %s: %c%c%c%c%c%c",
1888 	       reload_reg_map[rc].name,
1889 	       (mask & RELOAD_REG_VALID)      != 0 ? 'v' : ' ',
1890 	       (mask & RELOAD_REG_MULTIPLE)   != 0 ? 'm' : ' ',
1891 	       (mask & RELOAD_REG_INDEXED)    != 0 ? 'i' : ' ',
1892 	       (mask & RELOAD_REG_OFFSET)     != 0 ? 'o' : ' ',
1893 	       (mask & RELOAD_REG_PRE_INCDEC) != 0 ? '+' : ' ',
1894 	       (mask & RELOAD_REG_PRE_MODIFY) != 0 ? '+' : ' ');
1895     }
1896 
1897   if (rs6000_vector_unit[m] != VECTOR_NONE
1898       || rs6000_vector_mem[m] != VECTOR_NONE
1899       || (reg_addr[m].reload_store != CODE_FOR_nothing)
1900       || (reg_addr[m].reload_load != CODE_FOR_nothing)
1901       || reg_addr[m].scalar_in_vmx_p)
1902     {
1903       fprintf (stderr,
1904 	       "  Vector-arith=%-10s Vector-mem=%-10s Reload=%c%c Upper=%c",
1905 	       rs6000_debug_vector_unit (rs6000_vector_unit[m]),
1906 	       rs6000_debug_vector_unit (rs6000_vector_mem[m]),
1907 	       (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
1908 	       (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*',
1909 	       (reg_addr[m].scalar_in_vmx_p) ? 'y' : 'n');
1910     }
1911 
1912   fputs ("\n", stderr);
1913 }
1914 
1915 #define DEBUG_FMT_ID "%-32s= "
1916 #define DEBUG_FMT_D   DEBUG_FMT_ID "%d\n"
1917 #define DEBUG_FMT_WX  DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1918 #define DEBUG_FMT_S   DEBUG_FMT_ID "%s\n"
1919 
1920 /* Print various interesting information with -mdebug=reg.  */
1921 static void
rs6000_debug_reg_global(void)1922 rs6000_debug_reg_global (void)
1923 {
1924   static const char *const tf[2] = { "false", "true" };
1925   const char *nl = (const char *)0;
1926   int m;
1927   size_t m1, m2, v;
1928   char costly_num[20];
1929   char nop_num[20];
1930   char flags_buffer[40];
1931   const char *costly_str;
1932   const char *nop_str;
1933   const char *trace_str;
1934   const char *abi_str;
1935   const char *cmodel_str;
1936   struct cl_target_option cl_opts;
1937 
1938   /* Modes we want tieable information on.  */
1939   static const enum machine_mode print_tieable_modes[] = {
1940     QImode,
1941     HImode,
1942     SImode,
1943     DImode,
1944     TImode,
1945     PTImode,
1946     SFmode,
1947     DFmode,
1948     TFmode,
1949     SDmode,
1950     DDmode,
1951     TDmode,
1952     V8QImode,
1953     V4HImode,
1954     V2SImode,
1955     V16QImode,
1956     V8HImode,
1957     V4SImode,
1958     V2DImode,
1959     V1TImode,
1960     V32QImode,
1961     V16HImode,
1962     V8SImode,
1963     V4DImode,
1964     V2TImode,
1965     V2SFmode,
1966     V4SFmode,
1967     V2DFmode,
1968     V8SFmode,
1969     V4DFmode,
1970     CCmode,
1971     CCUNSmode,
1972     CCEQmode,
1973   };
1974 
1975   /* Virtual regs we are interested in.  */
1976   const static struct {
1977     int regno;			/* register number.  */
1978     const char *name;		/* register name.  */
1979   } virtual_regs[] = {
1980     { STACK_POINTER_REGNUM,			"stack pointer:" },
1981     { TOC_REGNUM,				"toc:          " },
1982     { STATIC_CHAIN_REGNUM,			"static chain: " },
1983     { RS6000_PIC_OFFSET_TABLE_REGNUM,		"pic offset:   " },
1984     { HARD_FRAME_POINTER_REGNUM,		"hard frame:   " },
1985     { ARG_POINTER_REGNUM,			"arg pointer:  " },
1986     { FRAME_POINTER_REGNUM,			"frame pointer:" },
1987     { FIRST_PSEUDO_REGISTER,			"first pseudo: " },
1988     { FIRST_VIRTUAL_REGISTER,			"first virtual:" },
1989     { VIRTUAL_INCOMING_ARGS_REGNUM,		"incoming_args:" },
1990     { VIRTUAL_STACK_VARS_REGNUM,		"stack_vars:   " },
1991     { VIRTUAL_STACK_DYNAMIC_REGNUM,		"stack_dynamic:" },
1992     { VIRTUAL_OUTGOING_ARGS_REGNUM,		"outgoing_args:" },
1993     { VIRTUAL_CFA_REGNUM,			"cfa (frame):  " },
1994     { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM,	"stack boundry:" },
1995     { LAST_VIRTUAL_REGISTER,			"last virtual: " },
1996   };
1997 
1998   fputs ("\nHard register information:\n", stderr);
1999   rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2000   rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2001   rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2002 			  LAST_ALTIVEC_REGNO,
2003 			  "vs");
2004   rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2005   rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2006   rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2007   rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2008   rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2009   rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2010   rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
2011   rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
2012 
2013   fputs ("\nVirtual/stack/frame registers:\n", stderr);
2014   for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2015     fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2016 
2017   fprintf (stderr,
2018 	   "\n"
2019 	   "d  reg_class = %s\n"
2020 	   "f  reg_class = %s\n"
2021 	   "v  reg_class = %s\n"
2022 	   "wa reg_class = %s\n"
2023 	   "wd reg_class = %s\n"
2024 	   "wf reg_class = %s\n"
2025 	   "wg reg_class = %s\n"
2026 	   "wh reg_class = %s\n"
2027 	   "wi reg_class = %s\n"
2028 	   "wj reg_class = %s\n"
2029 	   "wk reg_class = %s\n"
2030 	   "wl reg_class = %s\n"
2031 	   "wm reg_class = %s\n"
2032 	   "wr reg_class = %s\n"
2033 	   "ws reg_class = %s\n"
2034 	   "wt reg_class = %s\n"
2035 	   "wu reg_class = %s\n"
2036 	   "wv reg_class = %s\n"
2037 	   "ww reg_class = %s\n"
2038 	   "wx reg_class = %s\n"
2039 	   "wy reg_class = %s\n"
2040 	   "wz reg_class = %s\n"
2041 	   "\n",
2042 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2043 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2044 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2045 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2046 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2047 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2048 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2049 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wh]],
2050 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wi]],
2051 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wj]],
2052 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wk]],
2053 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2054 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2055 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2056 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2057 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2058 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2059 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2060 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2061 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2062 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2063 	   reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
2064 
2065   nl = "\n";
2066   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2067     rs6000_debug_print_mode (m);
2068 
2069   fputs ("\n", stderr);
2070 
2071   for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2072     {
2073       enum machine_mode mode1 = print_tieable_modes[m1];
2074       bool first_time = true;
2075 
2076       nl = (const char *)0;
2077       for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2078 	{
2079 	  enum machine_mode mode2 = print_tieable_modes[m2];
2080 	  if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
2081 	    {
2082 	      if (first_time)
2083 		{
2084 		  fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2085 		  nl = "\n";
2086 		  first_time = false;
2087 		}
2088 
2089 	      fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2090 	    }
2091 	}
2092 
2093       if (!first_time)
2094 	fputs ("\n", stderr);
2095     }
2096 
2097   if (nl)
2098     fputs (nl, stderr);
2099 
2100   if (rs6000_recip_control)
2101     {
2102       fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2103 
2104       for (m = 0; m < NUM_MACHINE_MODES; ++m)
2105 	if (rs6000_recip_bits[m])
2106 	  {
2107 	    fprintf (stderr,
2108 		     "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2109 		     GET_MODE_NAME (m),
2110 		     (RS6000_RECIP_AUTO_RE_P (m)
2111 		      ? "auto"
2112 		      : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2113 		     (RS6000_RECIP_AUTO_RSQRTE_P (m)
2114 		      ? "auto"
2115 		      : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2116 	  }
2117 
2118       fputs ("\n", stderr);
2119     }
2120 
2121   if (rs6000_cpu_index >= 0)
2122     {
2123       const char *name = processor_target_table[rs6000_cpu_index].name;
2124       HOST_WIDE_INT flags
2125 	= processor_target_table[rs6000_cpu_index].target_enable;
2126 
2127       sprintf (flags_buffer, "-mcpu=%s flags", name);
2128       rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2129     }
2130   else
2131     fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2132 
2133   if (rs6000_tune_index >= 0)
2134     {
2135       const char *name = processor_target_table[rs6000_tune_index].name;
2136       HOST_WIDE_INT flags
2137 	= processor_target_table[rs6000_tune_index].target_enable;
2138 
2139       sprintf (flags_buffer, "-mtune=%s flags", name);
2140       rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2141     }
2142   else
2143     fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2144 
2145   cl_target_option_save (&cl_opts, &global_options);
2146   rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2147 			    rs6000_isa_flags);
2148 
2149   rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2150 			    rs6000_isa_flags_explicit);
2151 
2152   rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2153 				rs6000_builtin_mask);
2154 
2155   rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2156 
2157   fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2158 	   OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2159 
2160   switch (rs6000_sched_costly_dep)
2161     {
2162     case max_dep_latency:
2163       costly_str = "max_dep_latency";
2164       break;
2165 
2166     case no_dep_costly:
2167       costly_str = "no_dep_costly";
2168       break;
2169 
2170     case all_deps_costly:
2171       costly_str = "all_deps_costly";
2172       break;
2173 
2174     case true_store_to_load_dep_costly:
2175       costly_str = "true_store_to_load_dep_costly";
2176       break;
2177 
2178     case store_to_load_dep_costly:
2179       costly_str = "store_to_load_dep_costly";
2180       break;
2181 
2182     default:
2183       costly_str = costly_num;
2184       sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2185       break;
2186     }
2187 
2188   fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2189 
2190   switch (rs6000_sched_insert_nops)
2191     {
2192     case sched_finish_regroup_exact:
2193       nop_str = "sched_finish_regroup_exact";
2194       break;
2195 
2196     case sched_finish_pad_groups:
2197       nop_str = "sched_finish_pad_groups";
2198       break;
2199 
2200     case sched_finish_none:
2201       nop_str = "sched_finish_none";
2202       break;
2203 
2204     default:
2205       nop_str = nop_num;
2206       sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2207       break;
2208     }
2209 
2210   fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2211 
2212   switch (rs6000_sdata)
2213     {
2214     default:
2215     case SDATA_NONE:
2216       break;
2217 
2218     case SDATA_DATA:
2219       fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2220       break;
2221 
2222     case SDATA_SYSV:
2223       fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2224       break;
2225 
2226     case SDATA_EABI:
2227       fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2228       break;
2229 
2230     }
2231 
2232   switch (rs6000_traceback)
2233     {
2234     case traceback_default:	trace_str = "default";	break;
2235     case traceback_none:	trace_str = "none";	break;
2236     case traceback_part:	trace_str = "part";	break;
2237     case traceback_full:	trace_str = "full";	break;
2238     default:			trace_str = "unknown";	break;
2239     }
2240 
2241   fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2242 
2243   switch (rs6000_current_cmodel)
2244     {
2245     case CMODEL_SMALL:	cmodel_str = "small";	break;
2246     case CMODEL_MEDIUM:	cmodel_str = "medium";	break;
2247     case CMODEL_LARGE:	cmodel_str = "large";	break;
2248     default:		cmodel_str = "unknown";	break;
2249     }
2250 
2251   fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2252 
2253   switch (rs6000_current_abi)
2254     {
2255     case ABI_NONE:	abi_str = "none";	break;
2256     case ABI_AIX:	abi_str = "aix";	break;
2257     case ABI_ELFv2:	abi_str = "ELFv2";	break;
2258     case ABI_V4:	abi_str = "V4";		break;
2259     case ABI_DARWIN:	abi_str = "darwin";	break;
2260     default:		abi_str = "unknown";	break;
2261     }
2262 
2263   fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2264 
2265   if (rs6000_altivec_abi)
2266     fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2267 
2268   if (rs6000_spe_abi)
2269     fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2270 
2271   if (rs6000_darwin64_abi)
2272     fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2273 
2274   if (rs6000_float_gprs)
2275     fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2276 
2277   fprintf (stderr, DEBUG_FMT_S, "fprs",
2278 	   (TARGET_FPRS ? "true" : "false"));
2279 
2280   fprintf (stderr, DEBUG_FMT_S, "single_float",
2281 	   (TARGET_SINGLE_FLOAT ? "true" : "false"));
2282 
2283   fprintf (stderr, DEBUG_FMT_S, "double_float",
2284 	   (TARGET_DOUBLE_FLOAT ? "true" : "false"));
2285 
2286   fprintf (stderr, DEBUG_FMT_S, "soft_float",
2287 	   (TARGET_SOFT_FLOAT ? "true" : "false"));
2288 
2289   fprintf (stderr, DEBUG_FMT_S, "e500_single",
2290 	   (TARGET_E500_SINGLE ? "true" : "false"));
2291 
2292   fprintf (stderr, DEBUG_FMT_S, "e500_double",
2293 	   (TARGET_E500_DOUBLE ? "true" : "false"));
2294 
2295   if (TARGET_LINK_STACK)
2296     fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2297 
2298   if (targetm.lra_p ())
2299     fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2300 
2301   if (TARGET_P8_FUSION)
2302     fprintf (stderr, DEBUG_FMT_S, "p8 fusion",
2303 	     (TARGET_P8_FUSION_SIGN) ? "zero+sign" : "zero");
2304 
2305   fprintf (stderr, DEBUG_FMT_S, "plt-format",
2306 	   TARGET_SECURE_PLT ? "secure" : "bss");
2307   fprintf (stderr, DEBUG_FMT_S, "struct-return",
2308 	   aix_struct_return ? "aix" : "sysv");
2309   fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2310   fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2311   fprintf (stderr, DEBUG_FMT_S, "align_branch",
2312 	   tf[!!rs6000_align_branch_targets]);
2313   fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2314   fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2315 	   rs6000_long_double_type_size);
2316   fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2317 	   (int)rs6000_sched_restricted_insns_priority);
2318   fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2319 	   (int)END_BUILTINS);
2320   fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2321 	   (int)RS6000_BUILTIN_COUNT);
2322 }
2323 
2324 
2325 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2326    legitimate address support to figure out the appropriate addressing to
2327    use.  */
2328 
2329 static void
rs6000_setup_reg_addr_masks(void)2330 rs6000_setup_reg_addr_masks (void)
2331 {
2332   ssize_t rc, reg, m, nregs;
2333   addr_mask_type any_addr_mask, addr_mask;
2334 
2335   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2336     {
2337       enum machine_mode m2 = (enum machine_mode)m;
2338 
2339       /* SDmode is special in that we want to access it only via REG+REG
2340 	 addressing on power7 and above, since we want to use the LFIWZX and
2341 	 STFIWZX instructions to load it.  */
2342       bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2343 
2344       any_addr_mask = 0;
2345       for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2346 	{
2347 	  addr_mask = 0;
2348 	  reg = reload_reg_map[rc].reg;
2349 
2350 	  /* Can mode values go in the GPR/FPR/Altivec registers?  */
2351 	  if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2352 	    {
2353 	      nregs = rs6000_hard_regno_nregs[m][reg];
2354 	      addr_mask |= RELOAD_REG_VALID;
2355 
2356 	      /* Indicate if the mode takes more than 1 physical register.  If
2357 		 it takes a single register, indicate it can do REG+REG
2358 		 addressing.  */
2359 	      if (nregs > 1 || m == BLKmode)
2360 		addr_mask |= RELOAD_REG_MULTIPLE;
2361 	      else
2362 		addr_mask |= RELOAD_REG_INDEXED;
2363 
2364 	      /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2365 		 addressing.  Restrict addressing on SPE for 64-bit types
2366 		 because of the SUBREG hackery used to address 64-bit floats in
2367 		 '32-bit' GPRs.  To simplify secondary reload, don't allow
2368 		 update forms on scalar floating point types that can go in the
2369 		 upper registers.  */
2370 
2371 	      if (TARGET_UPDATE
2372 		  && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2373 		  && GET_MODE_SIZE (m2) <= 8
2374 		  && !VECTOR_MODE_P (m2)
2375 		  && !COMPLEX_MODE_P (m2)
2376 		  && !indexed_only_p
2377 		  && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (m2) == 8)
2378 		  && !reg_addr[m2].scalar_in_vmx_p)
2379 		{
2380 		  addr_mask |= RELOAD_REG_PRE_INCDEC;
2381 
2382 		  /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2383 		     we don't allow PRE_MODIFY for some multi-register
2384 		     operations.  */
2385 		  switch (m)
2386 		    {
2387 		    default:
2388 		      addr_mask |= RELOAD_REG_PRE_MODIFY;
2389 		      break;
2390 
2391 		    case DImode:
2392 		      if (TARGET_POWERPC64)
2393 			addr_mask |= RELOAD_REG_PRE_MODIFY;
2394 		      break;
2395 
2396 		    case DFmode:
2397 		    case DDmode:
2398 		      if (TARGET_DF_INSN)
2399 			addr_mask |= RELOAD_REG_PRE_MODIFY;
2400 		      break;
2401 		    }
2402 		}
2403 	    }
2404 
2405 	  /* GPR and FPR registers can do REG+OFFSET addressing, except
2406 	     possibly for SDmode.  */
2407 	  if ((addr_mask != 0) && !indexed_only_p
2408 	      && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR))
2409 	    addr_mask |= RELOAD_REG_OFFSET;
2410 
2411 	  reg_addr[m].addr_mask[rc] = addr_mask;
2412 	  any_addr_mask |= addr_mask;
2413 	}
2414 
2415       reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2416     }
2417 }
2418 
2419 
2420 /* Initialize the various global tables that are based on register size.  */
2421 static void
rs6000_init_hard_regno_mode_ok(bool global_init_p)2422 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2423 {
2424   ssize_t r, m, c;
2425   int align64;
2426   int align32;
2427 
2428   /* Precalculate REGNO_REG_CLASS.  */
2429   rs6000_regno_regclass[0] = GENERAL_REGS;
2430   for (r = 1; r < 32; ++r)
2431     rs6000_regno_regclass[r] = BASE_REGS;
2432 
2433   for (r = 32; r < 64; ++r)
2434     rs6000_regno_regclass[r] = FLOAT_REGS;
2435 
2436   for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2437     rs6000_regno_regclass[r] = NO_REGS;
2438 
2439   for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2440     rs6000_regno_regclass[r] = ALTIVEC_REGS;
2441 
2442   rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2443   for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2444     rs6000_regno_regclass[r] = CR_REGS;
2445 
2446   rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2447   rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2448   rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2449   rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2450   rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2451   rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2452   rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2453   rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2454   rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2455   rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2456   rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2457   rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2458 
2459   /* Precalculate register class to simpler reload register class.  We don't
2460      need all of the register classes that are combinations of different
2461      classes, just the simple ones that have constraint letters.  */
2462   for (c = 0; c < N_REG_CLASSES; c++)
2463     reg_class_to_reg_type[c] = NO_REG_TYPE;
2464 
2465   reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2466   reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2467   reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2468   reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2469   reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2470   reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2471   reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2472   reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2473   reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2474   reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2475   reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2476   reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2477 
2478   if (TARGET_VSX)
2479     {
2480       reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2481       reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2482     }
2483   else
2484     {
2485       reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2486       reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2487     }
2488 
2489   /* Precalculate the valid memory formats as well as the vector information,
2490      this must be set up before the rs6000_hard_regno_nregs_internal calls
2491      below.  */
2492   gcc_assert ((int)VECTOR_NONE == 0);
2493   memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
2494   memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
2495 
2496   gcc_assert ((int)CODE_FOR_nothing == 0);
2497   memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
2498 
2499   gcc_assert ((int)NO_REGS == 0);
2500   memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
2501 
2502   /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2503      believes it can use native alignment or still uses 128-bit alignment.  */
2504   if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2505     {
2506       align64 = 64;
2507       align32 = 32;
2508     }
2509   else
2510     {
2511       align64 = 128;
2512       align32 = 128;
2513     }
2514 
2515   /* V2DF mode, VSX only.  */
2516   if (TARGET_VSX)
2517     {
2518       rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2519       rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2520       rs6000_vector_align[V2DFmode] = align64;
2521     }
2522 
2523   /* V4SF mode, either VSX or Altivec.  */
2524   if (TARGET_VSX)
2525     {
2526       rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2527       rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2528       rs6000_vector_align[V4SFmode] = align32;
2529     }
2530   else if (TARGET_ALTIVEC)
2531     {
2532       rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2533       rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2534       rs6000_vector_align[V4SFmode] = align32;
2535     }
2536 
2537   /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2538      and stores. */
2539   if (TARGET_ALTIVEC)
2540     {
2541       rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2542       rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2543       rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2544       rs6000_vector_align[V4SImode] = align32;
2545       rs6000_vector_align[V8HImode] = align32;
2546       rs6000_vector_align[V16QImode] = align32;
2547 
2548       if (TARGET_VSX)
2549 	{
2550 	  rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2551 	  rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2552 	  rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2553 	}
2554       else
2555 	{
2556 	  rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2557 	  rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2558 	  rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2559 	}
2560     }
2561 
2562   /* V2DImode, full mode depends on ISA 2.07 vector mode.  Allow under VSX to
2563      do insert/splat/extract.  Altivec doesn't have 64-bit integer support.  */
2564   if (TARGET_VSX)
2565     {
2566       rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2567       rs6000_vector_unit[V2DImode]
2568 	= (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2569       rs6000_vector_align[V2DImode] = align64;
2570 
2571       rs6000_vector_mem[V1TImode] = VECTOR_VSX;
2572       rs6000_vector_unit[V1TImode]
2573 	= (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2574       rs6000_vector_align[V1TImode] = 128;
2575     }
2576 
2577   /* DFmode, see if we want to use the VSX unit.  */
2578   if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2579     {
2580       rs6000_vector_unit[DFmode] = VECTOR_VSX;
2581       rs6000_vector_mem[DFmode]
2582 	= (TARGET_UPPER_REGS_DF ? VECTOR_VSX : VECTOR_NONE);
2583       rs6000_vector_align[DFmode] = align64;
2584     }
2585 
2586   /* Allow TImode in VSX register and set the VSX memory macros.  */
2587   if (TARGET_VSX && TARGET_VSX_TIMODE)
2588     {
2589       rs6000_vector_mem[TImode] = VECTOR_VSX;
2590       rs6000_vector_align[TImode] = align64;
2591     }
2592 
2593   /* TODO add SPE and paired floating point vector support.  */
2594 
2595   /* Register class constraints for the constraints that depend on compile
2596      switches. When the VSX code was added, different constraints were added
2597      based on the type (DFmode, V2DFmode, V4SFmode).  For the vector types, all
2598      of the VSX registers are used.  The register classes for scalar floating
2599      point types is set, based on whether we allow that type into the upper
2600      (Altivec) registers.  GCC has register classes to target the Altivec
2601      registers for load/store operations, to select using a VSX memory
2602      operation instead of the traditional floating point operation.  The
2603      constraints are:
2604 
2605 	d  - Register class to use with traditional DFmode instructions.
2606 	f  - Register class to use with traditional SFmode instructions.
2607 	v  - Altivec register.
2608 	wa - Any VSX register.
2609 	wc - Reserved to represent individual CR bits (used in LLVM).
2610 	wd - Preferred register class for V2DFmode.
2611 	wf - Preferred register class for V4SFmode.
2612 	wg - Float register for power6x move insns.
2613 	wh - FP register for direct move instructions.
2614 	wi - FP or VSX register to hold 64-bit integers for VSX insns.
2615 	wj - FP or VSX register to hold 64-bit integers for direct moves.
2616 	wk - FP or VSX register to hold 64-bit doubles for direct moves.
2617 	wl - Float register if we can do 32-bit signed int loads.
2618 	wm - VSX register for ISA 2.07 direct move operations.
2619 	wn - always NO_REGS.
2620 	wr - GPR if 64-bit mode is permitted.
2621 	ws - Register class to do ISA 2.06 DF operations.
2622 	wt - VSX register for TImode in VSX registers.
2623 	wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
2624 	wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
2625 	ww - Register class to do SF conversions in with VSX operations.
2626 	wx - Float register if we can do 32-bit int stores.
2627 	wy - Register class to do ISA 2.07 SF operations.
2628 	wz - Float register if we can do 32-bit unsigned int loads.  */
2629 
2630   if (TARGET_HARD_FLOAT && TARGET_FPRS)
2631     rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;	/* SFmode  */
2632 
2633   if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2634     rs6000_constraints[RS6000_CONSTRAINT_d]  = FLOAT_REGS;	/* DFmode  */
2635 
2636   if (TARGET_VSX)
2637     {
2638       rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2639       rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;	/* V2DFmode  */
2640       rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;	/* V4SFmode  */
2641       rs6000_constraints[RS6000_CONSTRAINT_wi] = FLOAT_REGS;	/* DImode  */
2642 
2643       if (TARGET_VSX_TIMODE)
2644 	rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;	/* TImode  */
2645 
2646       if (TARGET_UPPER_REGS_DF)					/* DFmode  */
2647 	{
2648 	  rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
2649 	  rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2650 	}
2651       else
2652 	rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
2653     }
2654 
2655   /* Add conditional constraints based on various options, to allow us to
2656      collapse multiple insn patterns.  */
2657   if (TARGET_ALTIVEC)
2658     rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2659 
2660   if (TARGET_MFPGPR)						/* DFmode  */
2661     rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2662 
2663   if (TARGET_LFIWAX)
2664     rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;	/* DImode  */
2665 
2666   if (TARGET_DIRECT_MOVE)
2667     {
2668       rs6000_constraints[RS6000_CONSTRAINT_wh] = FLOAT_REGS;
2669       rs6000_constraints[RS6000_CONSTRAINT_wj]			/* DImode  */
2670 	= rs6000_constraints[RS6000_CONSTRAINT_wi];
2671       rs6000_constraints[RS6000_CONSTRAINT_wk]			/* DFmode  */
2672 	= rs6000_constraints[RS6000_CONSTRAINT_ws];
2673       rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2674     }
2675 
2676   if (TARGET_POWERPC64)
2677     rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2678 
2679   if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF)			/* SFmode  */
2680     {
2681       rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
2682       rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
2683       rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
2684     }
2685   else if (TARGET_P8_VECTOR)
2686     {
2687       rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
2688       rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2689     }
2690   else if (TARGET_VSX)
2691     rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2692 
2693   if (TARGET_STFIWX)
2694     rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;	/* DImode  */
2695 
2696   if (TARGET_LFIWZX)
2697     rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;	/* DImode  */
2698 
2699   /* Set up the reload helper and direct move functions.  */
2700   if (TARGET_VSX || TARGET_ALTIVEC)
2701     {
2702       if (TARGET_64BIT)
2703 	{
2704 	  reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
2705 	  reg_addr[V16QImode].reload_load  = CODE_FOR_reload_v16qi_di_load;
2706 	  reg_addr[V8HImode].reload_store  = CODE_FOR_reload_v8hi_di_store;
2707 	  reg_addr[V8HImode].reload_load   = CODE_FOR_reload_v8hi_di_load;
2708 	  reg_addr[V4SImode].reload_store  = CODE_FOR_reload_v4si_di_store;
2709 	  reg_addr[V4SImode].reload_load   = CODE_FOR_reload_v4si_di_load;
2710 	  reg_addr[V2DImode].reload_store  = CODE_FOR_reload_v2di_di_store;
2711 	  reg_addr[V2DImode].reload_load   = CODE_FOR_reload_v2di_di_load;
2712 	  reg_addr[V1TImode].reload_store  = CODE_FOR_reload_v1ti_di_store;
2713 	  reg_addr[V1TImode].reload_load   = CODE_FOR_reload_v1ti_di_load;
2714 	  reg_addr[V4SFmode].reload_store  = CODE_FOR_reload_v4sf_di_store;
2715 	  reg_addr[V4SFmode].reload_load   = CODE_FOR_reload_v4sf_di_load;
2716 	  reg_addr[V2DFmode].reload_store  = CODE_FOR_reload_v2df_di_store;
2717 	  reg_addr[V2DFmode].reload_load   = CODE_FOR_reload_v2df_di_load;
2718 	  if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2719 	    {
2720 	      reg_addr[DFmode].reload_store    = CODE_FOR_reload_df_di_store;
2721 	      reg_addr[DFmode].reload_load     = CODE_FOR_reload_df_di_load;
2722 	      reg_addr[DFmode].scalar_in_vmx_p = true;
2723 	      reg_addr[DDmode].reload_store    = CODE_FOR_reload_dd_di_store;
2724 	      reg_addr[DDmode].reload_load     = CODE_FOR_reload_dd_di_load;
2725 	    }
2726 	  if (TARGET_P8_VECTOR)
2727 	    {
2728 	      reg_addr[SFmode].reload_store  = CODE_FOR_reload_sf_di_store;
2729 	      reg_addr[SFmode].reload_load   = CODE_FOR_reload_sf_di_load;
2730 	      reg_addr[SDmode].reload_store  = CODE_FOR_reload_sd_di_store;
2731 	      reg_addr[SDmode].reload_load   = CODE_FOR_reload_sd_di_load;
2732 	      if (TARGET_UPPER_REGS_SF)
2733 		reg_addr[SFmode].scalar_in_vmx_p = true;
2734 	    }
2735 	  if (TARGET_VSX_TIMODE)
2736 	    {
2737 	      reg_addr[TImode].reload_store  = CODE_FOR_reload_ti_di_store;
2738 	      reg_addr[TImode].reload_load   = CODE_FOR_reload_ti_di_load;
2739 	    }
2740 	  if (TARGET_DIRECT_MOVE)
2741 	    {
2742 	      if (TARGET_POWERPC64)
2743 		{
2744 		  reg_addr[TImode].reload_gpr_vsx    = CODE_FOR_reload_gpr_from_vsxti;
2745 		  reg_addr[V1TImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv1ti;
2746 		  reg_addr[V2DFmode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv2df;
2747 		  reg_addr[V2DImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv2di;
2748 		  reg_addr[V4SFmode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv4sf;
2749 		  reg_addr[V4SImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv4si;
2750 		  reg_addr[V8HImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv8hi;
2751 		  reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
2752 		  reg_addr[SFmode].reload_gpr_vsx    = CODE_FOR_reload_gpr_from_vsxsf;
2753 
2754 		  reg_addr[TImode].reload_vsx_gpr    = CODE_FOR_reload_vsx_from_gprti;
2755 		  reg_addr[V1TImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv1ti;
2756 		  reg_addr[V2DFmode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv2df;
2757 		  reg_addr[V2DImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv2di;
2758 		  reg_addr[V4SFmode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv4sf;
2759 		  reg_addr[V4SImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv4si;
2760 		  reg_addr[V8HImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv8hi;
2761 		  reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
2762 		  reg_addr[SFmode].reload_vsx_gpr    = CODE_FOR_reload_vsx_from_gprsf;
2763 		}
2764 	      else
2765 		{
2766 		  reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
2767 		  reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
2768 		  reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
2769 		}
2770 	    }
2771 	}
2772       else
2773 	{
2774 	  reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
2775 	  reg_addr[V16QImode].reload_load  = CODE_FOR_reload_v16qi_si_load;
2776 	  reg_addr[V8HImode].reload_store  = CODE_FOR_reload_v8hi_si_store;
2777 	  reg_addr[V8HImode].reload_load   = CODE_FOR_reload_v8hi_si_load;
2778 	  reg_addr[V4SImode].reload_store  = CODE_FOR_reload_v4si_si_store;
2779 	  reg_addr[V4SImode].reload_load   = CODE_FOR_reload_v4si_si_load;
2780 	  reg_addr[V2DImode].reload_store  = CODE_FOR_reload_v2di_si_store;
2781 	  reg_addr[V2DImode].reload_load   = CODE_FOR_reload_v2di_si_load;
2782 	  reg_addr[V1TImode].reload_store  = CODE_FOR_reload_v1ti_si_store;
2783 	  reg_addr[V1TImode].reload_load   = CODE_FOR_reload_v1ti_si_load;
2784 	  reg_addr[V4SFmode].reload_store  = CODE_FOR_reload_v4sf_si_store;
2785 	  reg_addr[V4SFmode].reload_load   = CODE_FOR_reload_v4sf_si_load;
2786 	  reg_addr[V2DFmode].reload_store  = CODE_FOR_reload_v2df_si_store;
2787 	  reg_addr[V2DFmode].reload_load   = CODE_FOR_reload_v2df_si_load;
2788 	  if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2789 	    {
2790 	      reg_addr[DFmode].reload_store    = CODE_FOR_reload_df_si_store;
2791 	      reg_addr[DFmode].reload_load     = CODE_FOR_reload_df_si_load;
2792 	      reg_addr[DFmode].scalar_in_vmx_p = true;
2793 	      reg_addr[DDmode].reload_store    = CODE_FOR_reload_dd_si_store;
2794 	      reg_addr[DDmode].reload_load     = CODE_FOR_reload_dd_si_load;
2795 	    }
2796 	  if (TARGET_P8_VECTOR)
2797 	    {
2798 	      reg_addr[SFmode].reload_store  = CODE_FOR_reload_sf_si_store;
2799 	      reg_addr[SFmode].reload_load   = CODE_FOR_reload_sf_si_load;
2800 	      reg_addr[SDmode].reload_store  = CODE_FOR_reload_sd_si_store;
2801 	      reg_addr[SDmode].reload_load   = CODE_FOR_reload_sd_si_load;
2802 	      if (TARGET_UPPER_REGS_SF)
2803 		reg_addr[SFmode].scalar_in_vmx_p = true;
2804 	    }
2805 	  if (TARGET_VSX_TIMODE)
2806 	    {
2807 	      reg_addr[TImode].reload_store  = CODE_FOR_reload_ti_si_store;
2808 	      reg_addr[TImode].reload_load   = CODE_FOR_reload_ti_si_load;
2809 	    }
2810 	}
2811     }
2812 
2813   /* Precalculate HARD_REGNO_NREGS.  */
2814   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2815     for (m = 0; m < NUM_MACHINE_MODES; ++m)
2816       rs6000_hard_regno_nregs[m][r]
2817 	= rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2818 
2819   /* Precalculate HARD_REGNO_MODE_OK.  */
2820   for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2821     for (m = 0; m < NUM_MACHINE_MODES; ++m)
2822       if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2823 	rs6000_hard_regno_mode_ok_p[m][r] = true;
2824 
2825   /* Precalculate CLASS_MAX_NREGS sizes.  */
2826   for (c = 0; c < LIM_REG_CLASSES; ++c)
2827     {
2828       int reg_size;
2829 
2830       if (TARGET_VSX && VSX_REG_CLASS_P (c))
2831 	reg_size = UNITS_PER_VSX_WORD;
2832 
2833       else if (c == ALTIVEC_REGS)
2834 	reg_size = UNITS_PER_ALTIVEC_WORD;
2835 
2836       else if (c == FLOAT_REGS)
2837 	reg_size = UNITS_PER_FP_WORD;
2838 
2839       else
2840 	reg_size = UNITS_PER_WORD;
2841 
2842       for (m = 0; m < NUM_MACHINE_MODES; ++m)
2843 	{
2844 	  enum machine_mode m2 = (enum machine_mode)m;
2845 	  int reg_size2 = reg_size;
2846 
2847 	  /* TFmode/TDmode always takes 2 registers, even in VSX.  */
2848 	  if (TARGET_VSX && VSX_REG_CLASS_P (c)
2849 	      && (m == TDmode || m == TFmode))
2850 	    reg_size2 = UNITS_PER_FP_WORD;
2851 
2852 	  rs6000_class_max_nregs[m][c]
2853 	    = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
2854 	}
2855     }
2856 
2857   if (TARGET_E500_DOUBLE)
2858     rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2859 
2860   /* Calculate which modes to automatically generate code to use a the
2861      reciprocal divide and square root instructions.  In the future, possibly
2862      automatically generate the instructions even if the user did not specify
2863      -mrecip.  The older machines double precision reciprocal sqrt estimate is
2864      not accurate enough.  */
2865   memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2866   if (TARGET_FRES)
2867     rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2868   if (TARGET_FRE)
2869     rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2870   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2871     rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2872   if (VECTOR_UNIT_VSX_P (V2DFmode))
2873     rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2874 
2875   if (TARGET_FRSQRTES)
2876     rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2877   if (TARGET_FRSQRTE)
2878     rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2879   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2880     rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2881   if (VECTOR_UNIT_VSX_P (V2DFmode))
2882     rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2883 
2884   if (rs6000_recip_control)
2885     {
2886       if (!flag_finite_math_only)
2887 	warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2888       if (flag_trapping_math)
2889 	warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2890       if (!flag_reciprocal_math)
2891 	warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2892       if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2893 	{
2894 	  if (RS6000_RECIP_HAVE_RE_P (SFmode)
2895 	      && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2896 	    rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2897 
2898 	  if (RS6000_RECIP_HAVE_RE_P (DFmode)
2899 	      && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2900 	    rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2901 
2902 	  if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2903 	      && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2904 	    rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2905 
2906 	  if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2907 	      && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2908 	    rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2909 
2910 	  if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2911 	      && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2912 	    rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2913 
2914 	  if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2915 	      && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2916 	    rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2917 
2918 	  if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2919 	      && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2920 	    rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2921 
2922 	  if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2923 	      && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2924 	    rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2925 	}
2926     }
2927 
2928   /* Update the addr mask bits in reg_addr to help secondary reload and go if
2929      legitimate address support to figure out the appropriate addressing to
2930      use.  */
2931   rs6000_setup_reg_addr_masks ();
2932 
2933   if (global_init_p || TARGET_DEBUG_TARGET)
2934     {
2935       if (TARGET_DEBUG_REG)
2936 	rs6000_debug_reg_global ();
2937 
2938       if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2939 	fprintf (stderr,
2940 		 "SImode variable mult cost       = %d\n"
2941 		 "SImode constant mult cost       = %d\n"
2942 		 "SImode short constant mult cost = %d\n"
2943 		 "DImode multipliciation cost     = %d\n"
2944 		 "SImode division cost            = %d\n"
2945 		 "DImode division cost            = %d\n"
2946 		 "Simple fp operation cost        = %d\n"
2947 		 "DFmode multiplication cost      = %d\n"
2948 		 "SFmode division cost            = %d\n"
2949 		 "DFmode division cost            = %d\n"
2950 		 "cache line size                 = %d\n"
2951 		 "l1 cache size                   = %d\n"
2952 		 "l2 cache size                   = %d\n"
2953 		 "simultaneous prefetches         = %d\n"
2954 		 "\n",
2955 		 rs6000_cost->mulsi,
2956 		 rs6000_cost->mulsi_const,
2957 		 rs6000_cost->mulsi_const9,
2958 		 rs6000_cost->muldi,
2959 		 rs6000_cost->divsi,
2960 		 rs6000_cost->divdi,
2961 		 rs6000_cost->fp,
2962 		 rs6000_cost->dmul,
2963 		 rs6000_cost->sdiv,
2964 		 rs6000_cost->ddiv,
2965 		 rs6000_cost->cache_line_size,
2966 		 rs6000_cost->l1_cache_size,
2967 		 rs6000_cost->l2_cache_size,
2968 		 rs6000_cost->simultaneous_prefetches);
2969     }
2970 }
2971 
2972 #if TARGET_MACHO
2973 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS.  */
2974 
2975 static void
darwin_rs6000_override_options(void)2976 darwin_rs6000_override_options (void)
2977 {
2978   /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2979      off.  */
2980   rs6000_altivec_abi = 1;
2981   TARGET_ALTIVEC_VRSAVE = 1;
2982   rs6000_current_abi = ABI_DARWIN;
2983 
2984   if (DEFAULT_ABI == ABI_DARWIN
2985       && TARGET_64BIT)
2986       darwin_one_byte_bool = 1;
2987 
2988   if (TARGET_64BIT && ! TARGET_POWERPC64)
2989     {
2990       rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2991       warning (0, "-m64 requires PowerPC64 architecture, enabling");
2992     }
2993   if (flag_mkernel)
2994     {
2995       rs6000_default_long_calls = 1;
2996       rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2997     }
2998 
2999   /* Make -m64 imply -maltivec.  Darwin's 64-bit ABI includes
3000      Altivec.  */
3001   if (!flag_mkernel && !flag_apple_kext
3002       && TARGET_64BIT
3003       && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3004     rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3005 
3006   /* Unless the user (not the configurer) has explicitly overridden
3007      it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3008      G4 unless targeting the kernel.  */
3009   if (!flag_mkernel
3010       && !flag_apple_kext
3011       && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3012       && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3013       && ! global_options_set.x_rs6000_cpu_index)
3014     {
3015       rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3016     }
3017 }
3018 #endif
3019 
3020 /* If not otherwise specified by a target, make 'long double' equivalent to
3021    'double'.  */
3022 
3023 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3024 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3025 #endif
3026 
3027 /* Return the builtin mask of the various options used that could affect which
3028    builtins were used.  In the past we used target_flags, but we've run out of
3029    bits, and some options like SPE and PAIRED are no longer in
3030    target_flags.  */
3031 
3032 HOST_WIDE_INT
rs6000_builtin_mask_calculate(void)3033 rs6000_builtin_mask_calculate (void)
3034 {
3035   return (((TARGET_ALTIVEC)		    ? RS6000_BTM_ALTIVEC   : 0)
3036 	  | ((TARGET_VSX)		    ? RS6000_BTM_VSX	   : 0)
3037 	  | ((TARGET_SPE)		    ? RS6000_BTM_SPE	   : 0)
3038 	  | ((TARGET_PAIRED_FLOAT)	    ? RS6000_BTM_PAIRED	   : 0)
3039 	  | ((TARGET_FRE)		    ? RS6000_BTM_FRE	   : 0)
3040 	  | ((TARGET_FRES)		    ? RS6000_BTM_FRES	   : 0)
3041 	  | ((TARGET_FRSQRTE)		    ? RS6000_BTM_FRSQRTE   : 0)
3042 	  | ((TARGET_FRSQRTES)		    ? RS6000_BTM_FRSQRTES  : 0)
3043 	  | ((TARGET_POPCNTD)		    ? RS6000_BTM_POPCNTD   : 0)
3044 	  | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL      : 0)
3045 	  | ((TARGET_P8_VECTOR)		    ? RS6000_BTM_P8_VECTOR : 0)
3046 	  | ((TARGET_CRYPTO)		    ? RS6000_BTM_CRYPTO	   : 0)
3047 	  | ((TARGET_HTM)		    ? RS6000_BTM_HTM	   : 0)
3048 	  | ((TARGET_DFP)		    ? RS6000_BTM_DFP	   : 0)
3049 	  | ((TARGET_HARD_FLOAT)	    ? RS6000_BTM_HARD_FLOAT : 0)
3050 	  | ((TARGET_LONG_DOUBLE_128)	    ? RS6000_BTM_LDBL128 : 0));
3051 }
3052 
3053 /* Override command line options.  Mostly we process the processor type and
3054    sometimes adjust other TARGET_ options.  */
3055 
3056 static bool
rs6000_option_override_internal(bool global_init_p)3057 rs6000_option_override_internal (bool global_init_p)
3058 {
3059   bool ret = true;
3060   bool have_cpu = false;
3061 
3062   /* The default cpu requested at configure time, if any.  */
3063   const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
3064 
3065   HOST_WIDE_INT set_masks;
3066   int cpu_index;
3067   int tune_index;
3068   struct cl_target_option *main_target_opt
3069     = ((global_init_p || target_option_default_node == NULL)
3070        ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3071 
3072   /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3073      library functions, so warn about it. The flag may be useful for
3074      performance studies from time to time though, so don't disable it
3075      entirely.  */
3076   if (global_options_set.x_rs6000_alignment_flags
3077       && rs6000_alignment_flags == MASK_ALIGN_POWER
3078       && DEFAULT_ABI == ABI_DARWIN
3079       && TARGET_64BIT)
3080     warning (0, "-malign-power is not supported for 64-bit Darwin;"
3081 	     " it is incompatible with the installed C and C++ libraries");
3082 
3083   /* Numerous experiment shows that IRA based loop pressure
3084      calculation works better for RTL loop invariant motion on targets
3085      with enough (>= 32) registers.  It is an expensive optimization.
3086      So it is on only for peak performance.  */
3087   if (optimize >= 3 && global_init_p
3088       && !global_options_set.x_flag_ira_loop_pressure)
3089     flag_ira_loop_pressure = 1;
3090 
3091   /* Set the pointer size.  */
3092   if (TARGET_64BIT)
3093     {
3094       rs6000_pmode = (int)DImode;
3095       rs6000_pointer_size = 64;
3096     }
3097   else
3098     {
3099       rs6000_pmode = (int)SImode;
3100       rs6000_pointer_size = 32;
3101     }
3102 
3103   /* Some OSs don't support saving the high part of 64-bit registers on context
3104      switch.  Other OSs don't support saving Altivec registers.  On those OSs,
3105      we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3106      if the user wants either, the user must explicitly specify them and we
3107      won't interfere with the user's specification.  */
3108 
3109   set_masks = POWERPC_MASKS;
3110 #ifdef OS_MISSING_POWERPC64
3111   if (OS_MISSING_POWERPC64)
3112     set_masks &= ~OPTION_MASK_POWERPC64;
3113 #endif
3114 #ifdef OS_MISSING_ALTIVEC
3115   if (OS_MISSING_ALTIVEC)
3116     set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
3117 #endif
3118 
3119   /* Don't override by the processor default if given explicitly.  */
3120   set_masks &= ~rs6000_isa_flags_explicit;
3121 
3122   /* Process the -mcpu=<xxx> and -mtune=<xxx> argument.  If the user changed
3123      the cpu in a target attribute or pragma, but did not specify a tuning
3124      option, use the cpu for the tuning option rather than the option specified
3125      with -mtune on the command line.  Process a '--with-cpu' configuration
3126      request as an implicit --cpu.  */
3127   if (rs6000_cpu_index >= 0)
3128     {
3129       cpu_index = rs6000_cpu_index;
3130       have_cpu = true;
3131     }
3132   else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3133     {
3134       rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
3135       have_cpu = true;
3136     }
3137   else if (implicit_cpu)
3138     {
3139       rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
3140       have_cpu = true;
3141     }
3142   else
3143     {
3144       /* PowerPC 64-bit LE requires at least ISA 2.07.  */
3145       const char *default_cpu = ((!TARGET_POWERPC64)
3146 				 ? "powerpc"
3147 				 : ((BYTES_BIG_ENDIAN)
3148 				    ? "powerpc64"
3149 				    : "powerpc64le"));
3150 
3151       rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
3152       have_cpu = false;
3153     }
3154 
3155   gcc_assert (cpu_index >= 0);
3156 
3157   /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3158      compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3159      with those from the cpu, except for options that were explicitly set.  If
3160      we don't have a cpu, do not override the target bits set in
3161      TARGET_DEFAULT.  */
3162   if (have_cpu)
3163     {
3164       rs6000_isa_flags &= ~set_masks;
3165       rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3166 			   & set_masks);
3167     }
3168   else
3169     {
3170       /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3171 	 POWERPC_MASKS.  Originally, TARGET_DEFAULT was used to initialize
3172 	 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook.  When we switched
3173 	 to using rs6000_isa_flags, we need to do the initialization here.
3174 
3175 	 If there is a TARGET_DEFAULT, use that.  Otherwise fall back to using
3176 	 -mcpu=powerpc, -mcpu=powerpc64, or -mcpu=powerpc64le defaults.  */
3177       HOST_WIDE_INT flags = ((TARGET_DEFAULT) ? TARGET_DEFAULT
3178 			     : processor_target_table[cpu_index].target_enable);
3179       rs6000_isa_flags |= (flags & ~rs6000_isa_flags_explicit);
3180     }
3181 
3182   if (rs6000_tune_index >= 0)
3183     tune_index = rs6000_tune_index;
3184   else if (have_cpu)
3185     rs6000_tune_index = tune_index = cpu_index;
3186   else
3187     {
3188       size_t i;
3189       enum processor_type tune_proc
3190 	= (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3191 
3192       tune_index = -1;
3193       for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3194 	if (processor_target_table[i].processor == tune_proc)
3195 	  {
3196 	    rs6000_tune_index = tune_index = i;
3197 	    break;
3198 	  }
3199     }
3200 
3201   gcc_assert (tune_index >= 0);
3202   rs6000_cpu = processor_target_table[tune_index].processor;
3203 
3204   /* Pick defaults for SPE related control flags.  Do this early to make sure
3205      that the TARGET_ macros are representative ASAP.  */
3206   {
3207     int spe_capable_cpu =
3208       (rs6000_cpu == PROCESSOR_PPC8540
3209        || rs6000_cpu == PROCESSOR_PPC8548);
3210 
3211     if (!global_options_set.x_rs6000_spe_abi)
3212       rs6000_spe_abi = spe_capable_cpu;
3213 
3214     if (!global_options_set.x_rs6000_spe)
3215       rs6000_spe = spe_capable_cpu;
3216 
3217     if (!global_options_set.x_rs6000_float_gprs)
3218       rs6000_float_gprs =
3219         (rs6000_cpu == PROCESSOR_PPC8540 ? 1
3220          : rs6000_cpu == PROCESSOR_PPC8548 ? 2
3221          : 0);
3222   }
3223 
3224   if (global_options_set.x_rs6000_spe_abi
3225       && rs6000_spe_abi
3226       && !TARGET_SPE_ABI)
3227     error ("not configured for SPE ABI");
3228 
3229   if (global_options_set.x_rs6000_spe
3230       && rs6000_spe
3231       && !TARGET_SPE)
3232     error ("not configured for SPE instruction set");
3233 
3234   if (main_target_opt != NULL
3235       && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
3236           || (main_target_opt->x_rs6000_spe != rs6000_spe)
3237           || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
3238     error ("target attribute or pragma changes SPE ABI");
3239 
3240   if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3241       || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3242       || rs6000_cpu == PROCESSOR_PPCE5500)
3243     {
3244       if (TARGET_ALTIVEC)
3245 	error ("AltiVec not supported in this target");
3246       if (TARGET_SPE)
3247 	error ("SPE not supported in this target");
3248     }
3249   if (rs6000_cpu == PROCESSOR_PPCE6500)
3250     {
3251       if (TARGET_SPE)
3252 	error ("SPE not supported in this target");
3253     }
3254 
3255   /* Disable Cell microcode if we are optimizing for the Cell
3256      and not optimizing for size.  */
3257   if (rs6000_gen_cell_microcode == -1)
3258     rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
3259                                   && !optimize_size);
3260 
3261   /* If we are optimizing big endian systems for space and it's OK to
3262      use instructions that would be microcoded on the Cell, use the
3263      load/store multiple and string instructions.  */
3264   if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
3265     rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
3266 						      | OPTION_MASK_STRING);
3267 
3268   /* Don't allow -mmultiple or -mstring on little endian systems
3269      unless the cpu is a 750, because the hardware doesn't support the
3270      instructions used in little endian mode, and causes an alignment
3271      trap.  The 750 does not cause an alignment trap (except when the
3272      target is unaligned).  */
3273 
3274   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
3275     {
3276       if (TARGET_MULTIPLE)
3277 	{
3278 	  rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3279 	  if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3280 	    warning (0, "-mmultiple is not supported on little endian systems");
3281 	}
3282 
3283       if (TARGET_STRING)
3284 	{
3285 	  rs6000_isa_flags &= ~OPTION_MASK_STRING;
3286 	  if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
3287 	    warning (0, "-mstring is not supported on little endian systems");
3288 	}
3289     }
3290 
3291   /* If little-endian, default to -mstrict-align on older processors.
3292      Testing for htm matches power8 and later.  */
3293   if (!BYTES_BIG_ENDIAN
3294       && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
3295     rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
3296 
3297   /* -maltivec={le,be} implies -maltivec.  */
3298   if (rs6000_altivec_element_order != 0)
3299     rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3300 
3301   /* Disallow -maltivec=le in big endian mode for now.  This is not
3302      known to be useful for anyone.  */
3303   if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
3304     {
3305       warning (0, N_("-maltivec=le not allowed for big-endian targets"));
3306       rs6000_altivec_element_order = 0;
3307     }
3308 
3309   /* Add some warnings for VSX.  */
3310   if (TARGET_VSX)
3311     {
3312       const char *msg = NULL;
3313       if (!TARGET_HARD_FLOAT || !TARGET_FPRS
3314 	  || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
3315 	{
3316 	  if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3317 	    msg = N_("-mvsx requires hardware floating point");
3318 	  else
3319 	    {
3320 	      rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3321 	      rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3322 	    }
3323 	}
3324       else if (TARGET_PAIRED_FLOAT)
3325 	msg = N_("-mvsx and -mpaired are incompatible");
3326       else if (TARGET_AVOID_XFORM > 0)
3327 	msg = N_("-mvsx needs indexed addressing");
3328       else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3329 				   & OPTION_MASK_ALTIVEC))
3330         {
3331 	  if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3332 	    msg = N_("-mvsx and -mno-altivec are incompatible");
3333 	  else
3334 	    msg = N_("-mno-altivec disables vsx");
3335         }
3336 
3337       if (msg)
3338 	{
3339 	  warning (0, msg);
3340 	  rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3341 	  rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3342 	}
3343     }
3344 
3345   /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3346      the -mcpu setting to enable options that conflict. */
3347   if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3348       && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3349 				       | OPTION_MASK_ALTIVEC
3350 				       | OPTION_MASK_VSX)) != 0)
3351     rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3352 			   | OPTION_MASK_DIRECT_MOVE)
3353 		         & ~rs6000_isa_flags_explicit);
3354 
3355   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3356     rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3357 
3358   /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3359      unless the user explicitly used the -mno-<option> to disable the code.  */
3360   if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3361     rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3362   else if (TARGET_VSX)
3363     rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3364   else if (TARGET_POPCNTD)
3365     rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3366   else if (TARGET_DFP)
3367     rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3368   else if (TARGET_CMPB)
3369     rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3370   else if (TARGET_FPRND)
3371     rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3372   else if (TARGET_POPCNTB)
3373     rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3374   else if (TARGET_ALTIVEC)
3375     rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3376 
3377   if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3378     {
3379       if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3380 	error ("-mcrypto requires -maltivec");
3381       rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3382     }
3383 
3384   if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3385     {
3386       if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3387 	error ("-mdirect-move requires -mvsx");
3388       rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3389     }
3390 
3391   if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3392     {
3393       if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3394 	error ("-mpower8-vector requires -maltivec");
3395       rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3396     }
3397 
3398   if (TARGET_P8_VECTOR && !TARGET_VSX)
3399     {
3400       if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3401 	error ("-mpower8-vector requires -mvsx");
3402       rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3403     }
3404 
3405   if (TARGET_VSX_TIMODE && !TARGET_VSX)
3406     {
3407       if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3408 	error ("-mvsx-timode requires -mvsx");
3409       rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3410     }
3411 
3412   if (TARGET_DFP && !TARGET_HARD_FLOAT)
3413     {
3414       if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
3415 	error ("-mhard-dfp requires -mhard-float");
3416       rs6000_isa_flags &= ~OPTION_MASK_DFP;
3417     }
3418 
3419   /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3420      silently turn off quad memory mode.  */
3421   if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
3422     {
3423       if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3424 	warning (0, N_("-mquad-memory requires 64-bit mode"));
3425 
3426       if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
3427 	warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
3428 
3429       rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
3430 			    | OPTION_MASK_QUAD_MEMORY_ATOMIC);
3431     }
3432 
3433   /* Non-atomic quad memory load/store are disabled for little endian, since
3434      the words are reversed, but atomic operations can still be done by
3435      swapping the words.  */
3436   if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
3437     {
3438       if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3439 	warning (0, N_("-mquad-memory is not available in little endian mode"));
3440 
3441       rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3442     }
3443 
3444   /* Assume if the user asked for normal quad memory instructions, they want
3445      the atomic versions as well, unless they explicity told us not to use quad
3446      word atomic instructions.  */
3447   if (TARGET_QUAD_MEMORY
3448       && !TARGET_QUAD_MEMORY_ATOMIC
3449       && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
3450     rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
3451 
3452   /* Enable power8 fusion if we are tuning for power8, even if we aren't
3453      generating power8 instructions.  */
3454   if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3455     rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3456 			 & OPTION_MASK_P8_FUSION);
3457 
3458   /* Power8 does not fuse sign extended loads with the addis.  If we are
3459      optimizing at high levels for speed, convert a sign extended load into a
3460      zero extending load, and an explicit sign extension.  */
3461   if (TARGET_P8_FUSION
3462       && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3463       && optimize_function_for_speed_p (cfun)
3464       && optimize >= 3)
3465     rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3466 
3467   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3468     rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3469 
3470   /* E500mc does "better" if we inline more aggressively.  Respect the
3471      user's opinion, though.  */
3472   if (rs6000_block_move_inline_limit == 0
3473       && (rs6000_cpu == PROCESSOR_PPCE500MC
3474 	  || rs6000_cpu == PROCESSOR_PPCE500MC64
3475 	  || rs6000_cpu == PROCESSOR_PPCE5500
3476 	  || rs6000_cpu == PROCESSOR_PPCE6500))
3477     rs6000_block_move_inline_limit = 128;
3478 
3479   /* store_one_arg depends on expand_block_move to handle at least the
3480      size of reg_parm_stack_space.  */
3481   if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3482     rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3483 
3484   if (global_init_p)
3485     {
3486       /* If the appropriate debug option is enabled, replace the target hooks
3487 	 with debug versions that call the real version and then prints
3488 	 debugging information.  */
3489       if (TARGET_DEBUG_COST)
3490 	{
3491 	  targetm.rtx_costs = rs6000_debug_rtx_costs;
3492 	  targetm.address_cost = rs6000_debug_address_cost;
3493 	  targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3494 	}
3495 
3496       if (TARGET_DEBUG_ADDR)
3497 	{
3498 	  targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3499 	  targetm.legitimize_address = rs6000_debug_legitimize_address;
3500 	  rs6000_secondary_reload_class_ptr
3501 	    = rs6000_debug_secondary_reload_class;
3502 	  rs6000_secondary_memory_needed_ptr
3503 	    = rs6000_debug_secondary_memory_needed;
3504 	  rs6000_cannot_change_mode_class_ptr
3505 	    = rs6000_debug_cannot_change_mode_class;
3506 	  rs6000_preferred_reload_class_ptr
3507 	    = rs6000_debug_preferred_reload_class;
3508 	  rs6000_legitimize_reload_address_ptr
3509 	    = rs6000_debug_legitimize_reload_address;
3510 	  rs6000_mode_dependent_address_ptr
3511 	    = rs6000_debug_mode_dependent_address;
3512 	}
3513 
3514       if (rs6000_veclibabi_name)
3515 	{
3516 	  if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3517 	    rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3518 	  else
3519 	    {
3520 	      error ("unknown vectorization library ABI type (%s) for "
3521 		     "-mveclibabi= switch", rs6000_veclibabi_name);
3522 	      ret = false;
3523 	    }
3524 	}
3525     }
3526 
3527   if (!global_options_set.x_rs6000_long_double_type_size)
3528     {
3529       if (main_target_opt != NULL
3530 	  && (main_target_opt->x_rs6000_long_double_type_size
3531 	      != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3532 	error ("target attribute or pragma changes long double size");
3533       else
3534 	rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3535     }
3536 
3537 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3538   if (!global_options_set.x_rs6000_ieeequad)
3539     rs6000_ieeequad = 1;
3540 #endif
3541 
3542   /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3543      target attribute or pragma which automatically enables both options,
3544      unless the altivec ABI was set.  This is set by default for 64-bit, but
3545      not for 32-bit.  */
3546   if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3547     rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3548 			  & ~rs6000_isa_flags_explicit);
3549 
3550   /* Enable Altivec ABI for AIX -maltivec.  */
3551   if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3552     {
3553       if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3554 	error ("target attribute or pragma changes AltiVec ABI");
3555       else
3556 	rs6000_altivec_abi = 1;
3557     }
3558 
3559   /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux.  For
3560      PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI.  It can
3561      be explicitly overridden in either case.  */
3562   if (TARGET_ELF)
3563     {
3564       if (!global_options_set.x_rs6000_altivec_abi
3565 	  && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3566 	{
3567 	  if (main_target_opt != NULL &&
3568 	      !main_target_opt->x_rs6000_altivec_abi)
3569 	    error ("target attribute or pragma changes AltiVec ABI");
3570 	  else
3571 	    rs6000_altivec_abi = 1;
3572 	}
3573     }
3574 
3575   /* Set the Darwin64 ABI as default for 64-bit Darwin.
3576      So far, the only darwin64 targets are also MACH-O.  */
3577   if (TARGET_MACHO
3578       && DEFAULT_ABI == ABI_DARWIN
3579       && TARGET_64BIT)
3580     {
3581       if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3582 	error ("target attribute or pragma changes darwin64 ABI");
3583       else
3584 	{
3585 	  rs6000_darwin64_abi = 1;
3586 	  /* Default to natural alignment, for better performance.  */
3587 	  rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3588 	}
3589     }
3590 
3591   /* Place FP constants in the constant pool instead of TOC
3592      if section anchors enabled.  */
3593   if (flag_section_anchors
3594       && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3595     TARGET_NO_FP_IN_TOC = 1;
3596 
3597   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3598     rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3599 
3600 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3601   SUBTARGET_OVERRIDE_OPTIONS;
3602 #endif
3603 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3604   SUBSUBTARGET_OVERRIDE_OPTIONS;
3605 #endif
3606 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3607   SUB3TARGET_OVERRIDE_OPTIONS;
3608 #endif
3609 
3610   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3611     rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3612 
3613   /* For the E500 family of cores, reset the single/double FP flags to let us
3614      check that they remain constant across attributes or pragmas.  Also,
3615      clear a possible request for string instructions, not supported and which
3616      we might have silently queried above for -Os.
3617 
3618      For other families, clear ISEL in case it was set implicitly.
3619   */
3620 
3621   switch (rs6000_cpu)
3622     {
3623     case PROCESSOR_PPC8540:
3624     case PROCESSOR_PPC8548:
3625     case PROCESSOR_PPCE500MC:
3626     case PROCESSOR_PPCE500MC64:
3627     case PROCESSOR_PPCE5500:
3628     case PROCESSOR_PPCE6500:
3629 
3630       rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3631       rs6000_double_float = TARGET_E500_DOUBLE;
3632 
3633       rs6000_isa_flags &= ~OPTION_MASK_STRING;
3634 
3635       break;
3636 
3637     default:
3638 
3639       if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3640 	rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3641 
3642       break;
3643     }
3644 
3645   if (main_target_opt)
3646     {
3647       if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3648 	error ("target attribute or pragma changes single precision floating "
3649 	       "point");
3650       if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3651 	error ("target attribute or pragma changes double precision floating "
3652 	       "point");
3653     }
3654 
3655   /* Detect invalid option combinations with E500.  */
3656   CHECK_E500_OPTIONS;
3657 
3658   rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3659 			&& rs6000_cpu != PROCESSOR_POWER5
3660 			&& rs6000_cpu != PROCESSOR_POWER6
3661 			&& rs6000_cpu != PROCESSOR_POWER7
3662 			&& rs6000_cpu != PROCESSOR_POWER8
3663 			&& rs6000_cpu != PROCESSOR_PPCA2
3664 			&& rs6000_cpu != PROCESSOR_CELL
3665 			&& rs6000_cpu != PROCESSOR_PPC476);
3666   rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3667 			 || rs6000_cpu == PROCESSOR_POWER5
3668 			 || rs6000_cpu == PROCESSOR_POWER7
3669 			 || rs6000_cpu == PROCESSOR_POWER8);
3670   rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3671 				 || rs6000_cpu == PROCESSOR_POWER5
3672 				 || rs6000_cpu == PROCESSOR_POWER6
3673 				 || rs6000_cpu == PROCESSOR_POWER7
3674 				 || rs6000_cpu == PROCESSOR_POWER8
3675 				 || rs6000_cpu == PROCESSOR_PPCE500MC
3676 				 || rs6000_cpu == PROCESSOR_PPCE500MC64
3677 				 || rs6000_cpu == PROCESSOR_PPCE5500
3678 				 || rs6000_cpu == PROCESSOR_PPCE6500);
3679 
3680   /* Allow debug switches to override the above settings.  These are set to -1
3681      in rs6000.opt to indicate the user hasn't directly set the switch.  */
3682   if (TARGET_ALWAYS_HINT >= 0)
3683     rs6000_always_hint = TARGET_ALWAYS_HINT;
3684 
3685   if (TARGET_SCHED_GROUPS >= 0)
3686     rs6000_sched_groups = TARGET_SCHED_GROUPS;
3687 
3688   if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3689     rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3690 
3691   rs6000_sched_restricted_insns_priority
3692     = (rs6000_sched_groups ? 1 : 0);
3693 
3694   /* Handle -msched-costly-dep option.  */
3695   rs6000_sched_costly_dep
3696     = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3697 
3698   if (rs6000_sched_costly_dep_str)
3699     {
3700       if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3701 	rs6000_sched_costly_dep = no_dep_costly;
3702       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3703 	rs6000_sched_costly_dep = all_deps_costly;
3704       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3705 	rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3706       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3707 	rs6000_sched_costly_dep = store_to_load_dep_costly;
3708       else
3709 	rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3710 				   atoi (rs6000_sched_costly_dep_str));
3711     }
3712 
3713   /* Handle -minsert-sched-nops option.  */
3714   rs6000_sched_insert_nops
3715     = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3716 
3717   if (rs6000_sched_insert_nops_str)
3718     {
3719       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3720 	rs6000_sched_insert_nops = sched_finish_none;
3721       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3722 	rs6000_sched_insert_nops = sched_finish_pad_groups;
3723       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3724 	rs6000_sched_insert_nops = sched_finish_regroup_exact;
3725       else
3726 	rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3727 				    atoi (rs6000_sched_insert_nops_str));
3728     }
3729 
3730   if (global_init_p)
3731     {
3732 #ifdef TARGET_REGNAMES
3733       /* If the user desires alternate register names, copy in the
3734 	 alternate names now.  */
3735       if (TARGET_REGNAMES)
3736 	memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3737 #endif
3738 
3739       /* Set aix_struct_return last, after the ABI is determined.
3740 	 If -maix-struct-return or -msvr4-struct-return was explicitly
3741 	 used, don't override with the ABI default.  */
3742       if (!global_options_set.x_aix_struct_return)
3743 	aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3744 
3745 #if 0
3746       /* IBM XL compiler defaults to unsigned bitfields.  */
3747       if (TARGET_XL_COMPAT)
3748 	flag_signed_bitfields = 0;
3749 #endif
3750 
3751       if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3752 	REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3753 
3754       if (TARGET_TOC)
3755 	ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3756 
3757       /* We can only guarantee the availability of DI pseudo-ops when
3758 	 assembling for 64-bit targets.  */
3759       if (!TARGET_64BIT)
3760 	{
3761 	  targetm.asm_out.aligned_op.di = NULL;
3762 	  targetm.asm_out.unaligned_op.di = NULL;
3763 	}
3764 
3765 
3766       /* Set branch target alignment, if not optimizing for size.  */
3767       if (!optimize_size)
3768 	{
3769 	  /* Cell wants to be aligned 8byte for dual issue.  Titan wants to be
3770 	     aligned 8byte to avoid misprediction by the branch predictor.  */
3771 	  if (rs6000_cpu == PROCESSOR_TITAN
3772 	      || rs6000_cpu == PROCESSOR_CELL)
3773 	    {
3774 	      if (align_functions <= 0)
3775 		align_functions = 8;
3776 	      if (align_jumps <= 0)
3777 		align_jumps = 8;
3778 	      if (align_loops <= 0)
3779 		align_loops = 8;
3780 	    }
3781 	  if (rs6000_align_branch_targets)
3782 	    {
3783 	      if (align_functions <= 0)
3784 		align_functions = 16;
3785 	      if (align_jumps <= 0)
3786 		align_jumps = 16;
3787 	      if (align_loops <= 0)
3788 		{
3789 		  can_override_loop_align = 1;
3790 		  align_loops = 16;
3791 		}
3792 	    }
3793 	  if (align_jumps_max_skip <= 0)
3794 	    align_jumps_max_skip = 15;
3795 	  if (align_loops_max_skip <= 0)
3796 	    align_loops_max_skip = 15;
3797 	}
3798 
3799       /* Arrange to save and restore machine status around nested functions.  */
3800       init_machine_status = rs6000_init_machine_status;
3801 
3802       /* We should always be splitting complex arguments, but we can't break
3803 	 Linux and Darwin ABIs at the moment.  For now, only AIX is fixed.  */
3804       if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
3805 	targetm.calls.split_complex_arg = NULL;
3806     }
3807 
3808   /* Initialize rs6000_cost with the appropriate target costs.  */
3809   if (optimize_size)
3810     rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3811   else
3812     switch (rs6000_cpu)
3813       {
3814       case PROCESSOR_RS64A:
3815 	rs6000_cost = &rs64a_cost;
3816 	break;
3817 
3818       case PROCESSOR_MPCCORE:
3819 	rs6000_cost = &mpccore_cost;
3820 	break;
3821 
3822       case PROCESSOR_PPC403:
3823 	rs6000_cost = &ppc403_cost;
3824 	break;
3825 
3826       case PROCESSOR_PPC405:
3827 	rs6000_cost = &ppc405_cost;
3828 	break;
3829 
3830       case PROCESSOR_PPC440:
3831 	rs6000_cost = &ppc440_cost;
3832 	break;
3833 
3834       case PROCESSOR_PPC476:
3835 	rs6000_cost = &ppc476_cost;
3836 	break;
3837 
3838       case PROCESSOR_PPC601:
3839 	rs6000_cost = &ppc601_cost;
3840 	break;
3841 
3842       case PROCESSOR_PPC603:
3843 	rs6000_cost = &ppc603_cost;
3844 	break;
3845 
3846       case PROCESSOR_PPC604:
3847 	rs6000_cost = &ppc604_cost;
3848 	break;
3849 
3850       case PROCESSOR_PPC604e:
3851 	rs6000_cost = &ppc604e_cost;
3852 	break;
3853 
3854       case PROCESSOR_PPC620:
3855 	rs6000_cost = &ppc620_cost;
3856 	break;
3857 
3858       case PROCESSOR_PPC630:
3859 	rs6000_cost = &ppc630_cost;
3860 	break;
3861 
3862       case PROCESSOR_CELL:
3863 	rs6000_cost = &ppccell_cost;
3864 	break;
3865 
3866       case PROCESSOR_PPC750:
3867       case PROCESSOR_PPC7400:
3868 	rs6000_cost = &ppc750_cost;
3869 	break;
3870 
3871       case PROCESSOR_PPC7450:
3872 	rs6000_cost = &ppc7450_cost;
3873 	break;
3874 
3875       case PROCESSOR_PPC8540:
3876       case PROCESSOR_PPC8548:
3877 	rs6000_cost = &ppc8540_cost;
3878 	break;
3879 
3880       case PROCESSOR_PPCE300C2:
3881       case PROCESSOR_PPCE300C3:
3882 	rs6000_cost = &ppce300c2c3_cost;
3883 	break;
3884 
3885       case PROCESSOR_PPCE500MC:
3886 	rs6000_cost = &ppce500mc_cost;
3887 	break;
3888 
3889       case PROCESSOR_PPCE500MC64:
3890 	rs6000_cost = &ppce500mc64_cost;
3891 	break;
3892 
3893       case PROCESSOR_PPCE5500:
3894 	rs6000_cost = &ppce5500_cost;
3895 	break;
3896 
3897       case PROCESSOR_PPCE6500:
3898 	rs6000_cost = &ppce6500_cost;
3899 	break;
3900 
3901       case PROCESSOR_TITAN:
3902 	rs6000_cost = &titan_cost;
3903 	break;
3904 
3905       case PROCESSOR_POWER4:
3906       case PROCESSOR_POWER5:
3907 	rs6000_cost = &power4_cost;
3908 	break;
3909 
3910       case PROCESSOR_POWER6:
3911 	rs6000_cost = &power6_cost;
3912 	break;
3913 
3914       case PROCESSOR_POWER7:
3915 	rs6000_cost = &power7_cost;
3916 	break;
3917 
3918       case PROCESSOR_POWER8:
3919 	rs6000_cost = &power8_cost;
3920 	break;
3921 
3922       case PROCESSOR_PPCA2:
3923 	rs6000_cost = &ppca2_cost;
3924 	break;
3925 
3926       default:
3927 	gcc_unreachable ();
3928       }
3929 
3930   if (global_init_p)
3931     {
3932       maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3933 			     rs6000_cost->simultaneous_prefetches,
3934 			     global_options.x_param_values,
3935 			     global_options_set.x_param_values);
3936       maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3937 			     global_options.x_param_values,
3938 			     global_options_set.x_param_values);
3939       maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3940 			     rs6000_cost->cache_line_size,
3941 			     global_options.x_param_values,
3942 			     global_options_set.x_param_values);
3943       maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3944 			     global_options.x_param_values,
3945 			     global_options_set.x_param_values);
3946 
3947       /* Increase loop peeling limits based on performance analysis. */
3948       maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3949 			     global_options.x_param_values,
3950 			     global_options_set.x_param_values);
3951       maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3952 			     global_options.x_param_values,
3953 			     global_options_set.x_param_values);
3954 
3955       /* If using typedef char *va_list, signal that
3956 	 __builtin_va_start (&ap, 0) can be optimized to
3957 	 ap = __builtin_next_arg (0).  */
3958       if (DEFAULT_ABI != ABI_V4)
3959 	targetm.expand_builtin_va_start = NULL;
3960     }
3961 
3962   /* Set up single/double float flags.
3963      If TARGET_HARD_FLOAT is set, but neither single or double is set,
3964      then set both flags. */
3965   if (TARGET_HARD_FLOAT && TARGET_FPRS
3966       && rs6000_single_float == 0 && rs6000_double_float == 0)
3967     rs6000_single_float = rs6000_double_float = 1;
3968 
3969   /* If not explicitly specified via option, decide whether to generate indexed
3970      load/store instructions.  */
3971   if (TARGET_AVOID_XFORM == -1)
3972     /* Avoid indexed addressing when targeting Power6 in order to avoid the
3973      DERAT mispredict penalty.  However the LVE and STVE altivec instructions
3974      need indexed accesses and the type used is the scalar type of the element
3975      being loaded or stored.  */
3976     TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3977 			  && !TARGET_ALTIVEC);
3978 
3979   /* Set the -mrecip options.  */
3980   if (rs6000_recip_name)
3981     {
3982       char *p = ASTRDUP (rs6000_recip_name);
3983       char *q;
3984       unsigned int mask, i;
3985       bool invert;
3986 
3987       while ((q = strtok (p, ",")) != NULL)
3988 	{
3989 	  p = NULL;
3990 	  if (*q == '!')
3991 	    {
3992 	      invert = true;
3993 	      q++;
3994 	    }
3995 	  else
3996 	    invert = false;
3997 
3998 	  if (!strcmp (q, "default"))
3999 	    mask = ((TARGET_RECIP_PRECISION)
4000 		    ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
4001 	  else
4002 	    {
4003 	      for (i = 0; i < ARRAY_SIZE (recip_options); i++)
4004 		if (!strcmp (q, recip_options[i].string))
4005 		  {
4006 		    mask = recip_options[i].mask;
4007 		    break;
4008 		  }
4009 
4010 	      if (i == ARRAY_SIZE (recip_options))
4011 		{
4012 		  error ("unknown option for -mrecip=%s", q);
4013 		  invert = false;
4014 		  mask = 0;
4015 		  ret = false;
4016 		}
4017 	    }
4018 
4019 	  if (invert)
4020 	    rs6000_recip_control &= ~mask;
4021 	  else
4022 	    rs6000_recip_control |= mask;
4023 	}
4024     }
4025 
4026   /* Determine when unaligned vector accesses are permitted, and when
4027      they are preferred over masked Altivec loads.  Note that if
4028      TARGET_ALLOW_MOVMISALIGN has been disabled by the user, then
4029      TARGET_EFFICIENT_UNALIGNED_VSX must be as well.  The converse is
4030      not true.  */
4031   if (TARGET_EFFICIENT_UNALIGNED_VSX == -1) {
4032     if (TARGET_VSX && rs6000_cpu == PROCESSOR_POWER8
4033 	&& TARGET_ALLOW_MOVMISALIGN != 0)
4034       TARGET_EFFICIENT_UNALIGNED_VSX = 1;
4035     else
4036       TARGET_EFFICIENT_UNALIGNED_VSX = 0;
4037   }
4038 
4039   if (TARGET_ALLOW_MOVMISALIGN == -1 && rs6000_cpu == PROCESSOR_POWER8)
4040     TARGET_ALLOW_MOVMISALIGN = 1;
4041 
4042   /* Set the builtin mask of the various options used that could affect which
4043      builtins were used.  In the past we used target_flags, but we've run out
4044      of bits, and some options like SPE and PAIRED are no longer in
4045      target_flags.  */
4046   rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
4047   if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
4048     rs6000_print_builtin_options (stderr, 0, "builtin mask",
4049 				  rs6000_builtin_mask);
4050 
4051   /* Initialize all of the registers.  */
4052   rs6000_init_hard_regno_mode_ok (global_init_p);
4053 
4054   /* Save the initial options in case the user does function specific options */
4055   if (global_init_p)
4056     target_option_default_node = target_option_current_node
4057       = build_target_option_node ();
4058 
4059   /* If not explicitly specified via option, decide whether to generate the
4060      extra blr's required to preserve the link stack on some cpus (eg, 476).  */
4061   if (TARGET_LINK_STACK == -1)
4062     SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
4063 
4064   return ret;
4065 }
4066 
4067 static bool
gate_analyze_swaps(void)4068 gate_analyze_swaps (void)
4069 {
4070   return (optimize > 0 && !BYTES_BIG_ENDIAN && TARGET_VSX
4071 	  && rs6000_optimize_swaps);
4072 }
4073 
4074 static unsigned int
execute_analyze_swaps(void)4075 execute_analyze_swaps (void)
4076 {
4077   return rs6000_analyze_swaps (cfun);
4078 }
4079 
4080 struct rtl_opt_pass pass_analyze_swaps =
4081 {
4082   RTL_PASS,
4083   "swaps", /* name */
4084   OPTGROUP_NONE, /* optinfo_flags */
4085   gate_analyze_swaps, /* has_gate */
4086   execute_analyze_swaps, /* has_execute */
4087   NULL, /* sub */
4088   NULL, /* next */
4089   0, /* static_pass_number */
4090   TV_NONE, /* tv_id */
4091   0, /* properties_required */
4092   0, /* properties_provided */
4093   0, /* properties_destroyed */
4094   0, /* todo_flags_start */
4095   TODO_df_finish, /* todo_flags_finish */
4096 };
4097 
4098 /* Implement TARGET_OPTION_OVERRIDE.  On the RS/6000 this is used to
4099    define the target cpu type.  */
4100 
4101 static void
rs6000_option_override(void)4102 rs6000_option_override (void)
4103 {
4104   (void) rs6000_option_override_internal (true);
4105 
4106   /* Register machine-specific passes.  This needs to be done at start-up.
4107      It's convenient to do it here (like i386 does).  */
4108   static struct register_pass_info analyze_swaps_info
4109     = { &pass_analyze_swaps.pass, "cse1", 1, PASS_POS_INSERT_BEFORE };
4110 
4111   register_pass (&analyze_swaps_info);
4112 }
4113 
4114 
4115 /* Implement targetm.vectorize.builtin_mask_for_load.  */
4116 static tree
rs6000_builtin_mask_for_load(void)4117 rs6000_builtin_mask_for_load (void)
4118 {
4119   /* Don't use lvsl/vperm for P8 and similarly efficient machines.  */
4120   if ((TARGET_ALTIVEC && !TARGET_VSX)
4121       || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
4122     return altivec_builtin_mask_for_load;
4123   else
4124     return 0;
4125 }
4126 
4127 /* Implement LOOP_ALIGN. */
4128 int
rs6000_loop_align(rtx label)4129 rs6000_loop_align (rtx label)
4130 {
4131   basic_block bb;
4132   int ninsns;
4133 
4134   /* Don't override loop alignment if -falign-loops was specified. */
4135   if (!can_override_loop_align)
4136     return align_loops_log;
4137 
4138   bb = BLOCK_FOR_INSN (label);
4139   ninsns = num_loop_insns(bb->loop_father);
4140 
4141   /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
4142   if (ninsns > 4 && ninsns <= 8
4143       && (rs6000_cpu == PROCESSOR_POWER4
4144 	  || rs6000_cpu == PROCESSOR_POWER5
4145 	  || rs6000_cpu == PROCESSOR_POWER6
4146 	  || rs6000_cpu == PROCESSOR_POWER7
4147 	  || rs6000_cpu == PROCESSOR_POWER8))
4148     return 5;
4149   else
4150     return align_loops_log;
4151 }
4152 
4153 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
4154 static int
rs6000_loop_align_max_skip(rtx label)4155 rs6000_loop_align_max_skip (rtx label)
4156 {
4157   return (1 << rs6000_loop_align (label)) - 1;
4158 }
4159 
4160 /* Return true iff, data reference of TYPE can reach vector alignment (16)
4161    after applying N number of iterations.  This routine does not determine
4162    how may iterations are required to reach desired alignment.  */
4163 
4164 static bool
rs6000_vector_alignment_reachable(const_tree type ATTRIBUTE_UNUSED,bool is_packed)4165 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
4166 {
4167   if (is_packed)
4168     return false;
4169 
4170   if (TARGET_32BIT)
4171     {
4172       if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
4173         return true;
4174 
4175       if (rs6000_alignment_flags ==  MASK_ALIGN_POWER)
4176         return true;
4177 
4178       return false;
4179     }
4180   else
4181     {
4182       if (TARGET_MACHO)
4183         return false;
4184 
4185       /* Assuming that all other types are naturally aligned. CHECKME!  */
4186       return true;
4187     }
4188 }
4189 
4190 /* Return true if the vector misalignment factor is supported by the
4191    target.  */
4192 static bool
rs6000_builtin_support_vector_misalignment(enum machine_mode mode,const_tree type,int misalignment,bool is_packed)4193 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
4194 					    const_tree type,
4195 					    int misalignment,
4196 					    bool is_packed)
4197 {
4198   if (TARGET_VSX)
4199     {
4200       if (TARGET_EFFICIENT_UNALIGNED_VSX)
4201 	return true;
4202 
4203       /* Return if movmisalign pattern is not supported for this mode.  */
4204       if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
4205         return false;
4206 
4207       if (misalignment == -1)
4208 	{
4209 	  /* Misalignment factor is unknown at compile time but we know
4210 	     it's word aligned.  */
4211 	  if (rs6000_vector_alignment_reachable (type, is_packed))
4212             {
4213               int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4214 
4215               if (element_size == 64 || element_size == 32)
4216                return true;
4217             }
4218 
4219 	  return false;
4220 	}
4221 
4222       /* VSX supports word-aligned vector.  */
4223       if (misalignment % 4 == 0)
4224 	return true;
4225     }
4226   return false;
4227 }
4228 
4229 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
4230 static int
rs6000_builtin_vectorization_cost(enum vect_cost_for_stmt type_of_cost,tree vectype,int misalign)4231 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4232                                    tree vectype, int misalign)
4233 {
4234   unsigned elements;
4235   tree elem_type;
4236 
4237   switch (type_of_cost)
4238     {
4239       case scalar_stmt:
4240       case scalar_load:
4241       case scalar_store:
4242       case vector_stmt:
4243       case vector_load:
4244       case vector_store:
4245       case vec_to_scalar:
4246       case scalar_to_vec:
4247       case cond_branch_not_taken:
4248         return 1;
4249 
4250       case vec_perm:
4251 	if (TARGET_VSX)
4252 	  return 3;
4253 	else
4254 	  return 1;
4255 
4256       case vec_promote_demote:
4257         if (TARGET_VSX)
4258           return 4;
4259         else
4260           return 1;
4261 
4262       case cond_branch_taken:
4263         return 3;
4264 
4265       case unaligned_load:
4266 	if (TARGET_EFFICIENT_UNALIGNED_VSX)
4267 	  return 1;
4268 
4269         if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4270           {
4271             elements = TYPE_VECTOR_SUBPARTS (vectype);
4272             if (elements == 2)
4273               /* Double word aligned.  */
4274               return 2;
4275 
4276             if (elements == 4)
4277               {
4278                 switch (misalign)
4279                   {
4280                     case 8:
4281                       /* Double word aligned.  */
4282                       return 2;
4283 
4284                     case -1:
4285                       /* Unknown misalignment.  */
4286                     case 4:
4287                     case 12:
4288                       /* Word aligned.  */
4289                       return 22;
4290 
4291                     default:
4292                       gcc_unreachable ();
4293                   }
4294               }
4295           }
4296 
4297         if (TARGET_ALTIVEC)
4298           /* Misaligned loads are not supported.  */
4299           gcc_unreachable ();
4300 
4301         return 2;
4302 
4303       case unaligned_store:
4304 	if (TARGET_EFFICIENT_UNALIGNED_VSX)
4305 	  return 1;
4306 
4307         if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4308           {
4309             elements = TYPE_VECTOR_SUBPARTS (vectype);
4310             if (elements == 2)
4311               /* Double word aligned.  */
4312               return 2;
4313 
4314             if (elements == 4)
4315               {
4316                 switch (misalign)
4317                   {
4318                     case 8:
4319                       /* Double word aligned.  */
4320                       return 2;
4321 
4322                     case -1:
4323                       /* Unknown misalignment.  */
4324                     case 4:
4325                     case 12:
4326                       /* Word aligned.  */
4327                       return 23;
4328 
4329                     default:
4330                       gcc_unreachable ();
4331                   }
4332               }
4333           }
4334 
4335         if (TARGET_ALTIVEC)
4336           /* Misaligned stores are not supported.  */
4337           gcc_unreachable ();
4338 
4339         return 2;
4340 
4341       case vec_construct:
4342 	elements = TYPE_VECTOR_SUBPARTS (vectype);
4343 	elem_type = TREE_TYPE (vectype);
4344 	/* 32-bit vectors loaded into registers are stored as double
4345 	   precision, so we need n/2 converts in addition to the usual
4346 	   n/2 merges to construct a vector of short floats from them.  */
4347 	if (SCALAR_FLOAT_TYPE_P (elem_type)
4348 	    && TYPE_PRECISION (elem_type) == 32)
4349 	  return elements + 1;
4350 	else
4351 	  return elements / 2 + 1;
4352 
4353       default:
4354         gcc_unreachable ();
4355     }
4356 }
4357 
4358 /* Implement targetm.vectorize.preferred_simd_mode.  */
4359 
4360 static enum machine_mode
rs6000_preferred_simd_mode(enum machine_mode mode)4361 rs6000_preferred_simd_mode (enum machine_mode mode)
4362 {
4363   if (TARGET_VSX)
4364     switch (mode)
4365       {
4366       case DFmode:
4367 	return V2DFmode;
4368       default:;
4369       }
4370   if (TARGET_ALTIVEC || TARGET_VSX)
4371     switch (mode)
4372       {
4373       case SFmode:
4374 	return V4SFmode;
4375       case TImode:
4376 	return V1TImode;
4377       case DImode:
4378 	return V2DImode;
4379       case SImode:
4380 	return V4SImode;
4381       case HImode:
4382 	return V8HImode;
4383       case QImode:
4384 	return V16QImode;
4385       default:;
4386       }
4387   if (TARGET_SPE)
4388     switch (mode)
4389       {
4390       case SFmode:
4391 	return V2SFmode;
4392       case SImode:
4393 	return V2SImode;
4394       default:;
4395       }
4396   if (TARGET_PAIRED_FLOAT
4397       && mode == SFmode)
4398     return V2SFmode;
4399   return word_mode;
4400 }
4401 
4402 typedef struct _rs6000_cost_data
4403 {
4404   struct loop *loop_info;
4405   unsigned cost[3];
4406 } rs6000_cost_data;
4407 
4408 /* Test for likely overcommitment of vector hardware resources.  If a
4409    loop iteration is relatively large, and too large a percentage of
4410    instructions in the loop are vectorized, the cost model may not
4411    adequately reflect delays from unavailable vector resources.
4412    Penalize the loop body cost for this case.  */
4413 
4414 static void
rs6000_density_test(rs6000_cost_data * data)4415 rs6000_density_test (rs6000_cost_data *data)
4416 {
4417   const int DENSITY_PCT_THRESHOLD = 85;
4418   const int DENSITY_SIZE_THRESHOLD = 70;
4419   const int DENSITY_PENALTY = 10;
4420   struct loop *loop = data->loop_info;
4421   basic_block *bbs = get_loop_body (loop);
4422   int nbbs = loop->num_nodes;
4423   int vec_cost = data->cost[vect_body], not_vec_cost = 0;
4424   int i, density_pct;
4425 
4426   for (i = 0; i < nbbs; i++)
4427     {
4428       basic_block bb = bbs[i];
4429       gimple_stmt_iterator gsi;
4430 
4431       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4432 	{
4433 	  gimple stmt = gsi_stmt (gsi);
4434 	  stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4435 
4436 	  if (!STMT_VINFO_RELEVANT_P (stmt_info)
4437 	      && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4438 	    not_vec_cost++;
4439 	}
4440     }
4441 
4442   free (bbs);
4443   density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4444 
4445   if (density_pct > DENSITY_PCT_THRESHOLD
4446       && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4447     {
4448       data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4449       if (dump_enabled_p ())
4450 	dump_printf_loc (MSG_NOTE, vect_location,
4451 			 "density %d%%, cost %d exceeds threshold, penalizing "
4452 			 "loop body cost by %d%%", density_pct,
4453 			 vec_cost + not_vec_cost, DENSITY_PENALTY);
4454     }
4455 }
4456 
4457 /* Implement targetm.vectorize.init_cost.  */
4458 
4459 static void *
rs6000_init_cost(struct loop * loop_info)4460 rs6000_init_cost (struct loop *loop_info)
4461 {
4462   rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4463   data->loop_info = loop_info;
4464   data->cost[vect_prologue] = 0;
4465   data->cost[vect_body]     = 0;
4466   data->cost[vect_epilogue] = 0;
4467   return data;
4468 }
4469 
4470 /* Implement targetm.vectorize.add_stmt_cost.  */
4471 
4472 static unsigned
rs6000_add_stmt_cost(void * data,int count,enum vect_cost_for_stmt kind,struct _stmt_vec_info * stmt_info,int misalign,enum vect_cost_model_location where)4473 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4474 		      struct _stmt_vec_info *stmt_info, int misalign,
4475 		      enum vect_cost_model_location where)
4476 {
4477   rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4478   unsigned retval = 0;
4479 
4480   if (flag_vect_cost_model)
4481     {
4482       tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4483       int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4484 							 misalign);
4485       /* Statements in an inner loop relative to the loop being
4486 	 vectorized are weighted more heavily.  The value here is
4487 	 arbitrary and could potentially be improved with analysis.  */
4488       if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4489 	count *= 50;  /* FIXME.  */
4490 
4491       retval = (unsigned) (count * stmt_cost);
4492       cost_data->cost[where] += retval;
4493     }
4494 
4495   return retval;
4496 }
4497 
4498 /* Implement targetm.vectorize.finish_cost.  */
4499 
4500 static void
rs6000_finish_cost(void * data,unsigned * prologue_cost,unsigned * body_cost,unsigned * epilogue_cost)4501 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4502 		    unsigned *body_cost, unsigned *epilogue_cost)
4503 {
4504   rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4505 
4506   if (cost_data->loop_info)
4507     rs6000_density_test (cost_data);
4508 
4509   *prologue_cost = cost_data->cost[vect_prologue];
4510   *body_cost     = cost_data->cost[vect_body];
4511   *epilogue_cost = cost_data->cost[vect_epilogue];
4512 }
4513 
4514 /* Implement targetm.vectorize.destroy_cost_data.  */
4515 
4516 static void
rs6000_destroy_cost_data(void * data)4517 rs6000_destroy_cost_data (void *data)
4518 {
4519   free (data);
4520 }
4521 
4522 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4523    library with vectorized intrinsics.  */
4524 
4525 static tree
rs6000_builtin_vectorized_libmass(tree fndecl,tree type_out,tree type_in)4526 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4527 {
4528   char name[32];
4529   const char *suffix = NULL;
4530   tree fntype, new_fndecl, bdecl = NULL_TREE;
4531   int n_args = 1;
4532   const char *bname;
4533   enum machine_mode el_mode, in_mode;
4534   int n, in_n;
4535 
4536   /* Libmass is suitable for unsafe math only as it does not correctly support
4537      parts of IEEE with the required precision such as denormals.  Only support
4538      it if we have VSX to use the simd d2 or f4 functions.
4539      XXX: Add variable length support.  */
4540   if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4541     return NULL_TREE;
4542 
4543   el_mode = TYPE_MODE (TREE_TYPE (type_out));
4544   n = TYPE_VECTOR_SUBPARTS (type_out);
4545   in_mode = TYPE_MODE (TREE_TYPE (type_in));
4546   in_n = TYPE_VECTOR_SUBPARTS (type_in);
4547   if (el_mode != in_mode
4548       || n != in_n)
4549     return NULL_TREE;
4550 
4551   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4552     {
4553       enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4554       switch (fn)
4555 	{
4556 	case BUILT_IN_ATAN2:
4557 	case BUILT_IN_HYPOT:
4558 	case BUILT_IN_POW:
4559 	  n_args = 2;
4560 	  /* fall through */
4561 
4562 	case BUILT_IN_ACOS:
4563 	case BUILT_IN_ACOSH:
4564 	case BUILT_IN_ASIN:
4565 	case BUILT_IN_ASINH:
4566 	case BUILT_IN_ATAN:
4567 	case BUILT_IN_ATANH:
4568 	case BUILT_IN_CBRT:
4569 	case BUILT_IN_COS:
4570 	case BUILT_IN_COSH:
4571 	case BUILT_IN_ERF:
4572 	case BUILT_IN_ERFC:
4573 	case BUILT_IN_EXP2:
4574 	case BUILT_IN_EXP:
4575 	case BUILT_IN_EXPM1:
4576 	case BUILT_IN_LGAMMA:
4577 	case BUILT_IN_LOG10:
4578 	case BUILT_IN_LOG1P:
4579 	case BUILT_IN_LOG2:
4580 	case BUILT_IN_LOG:
4581 	case BUILT_IN_SIN:
4582 	case BUILT_IN_SINH:
4583 	case BUILT_IN_SQRT:
4584 	case BUILT_IN_TAN:
4585 	case BUILT_IN_TANH:
4586 	  bdecl = builtin_decl_implicit (fn);
4587 	  suffix = "d2";				/* pow -> powd2 */
4588 	  if (el_mode != DFmode
4589 	      || n != 2
4590 	      || !bdecl)
4591 	    return NULL_TREE;
4592 	  break;
4593 
4594 	case BUILT_IN_ATAN2F:
4595 	case BUILT_IN_HYPOTF:
4596 	case BUILT_IN_POWF:
4597 	  n_args = 2;
4598 	  /* fall through */
4599 
4600 	case BUILT_IN_ACOSF:
4601 	case BUILT_IN_ACOSHF:
4602 	case BUILT_IN_ASINF:
4603 	case BUILT_IN_ASINHF:
4604 	case BUILT_IN_ATANF:
4605 	case BUILT_IN_ATANHF:
4606 	case BUILT_IN_CBRTF:
4607 	case BUILT_IN_COSF:
4608 	case BUILT_IN_COSHF:
4609 	case BUILT_IN_ERFF:
4610 	case BUILT_IN_ERFCF:
4611 	case BUILT_IN_EXP2F:
4612 	case BUILT_IN_EXPF:
4613 	case BUILT_IN_EXPM1F:
4614 	case BUILT_IN_LGAMMAF:
4615 	case BUILT_IN_LOG10F:
4616 	case BUILT_IN_LOG1PF:
4617 	case BUILT_IN_LOG2F:
4618 	case BUILT_IN_LOGF:
4619 	case BUILT_IN_SINF:
4620 	case BUILT_IN_SINHF:
4621 	case BUILT_IN_SQRTF:
4622 	case BUILT_IN_TANF:
4623 	case BUILT_IN_TANHF:
4624 	  bdecl = builtin_decl_implicit (fn);
4625 	  suffix = "4";					/* powf -> powf4 */
4626 	  if (el_mode != SFmode
4627 	      || n != 4
4628 	      || !bdecl)
4629 	    return NULL_TREE;
4630 	  break;
4631 
4632 	default:
4633 	  return NULL_TREE;
4634 	}
4635     }
4636   else
4637     return NULL_TREE;
4638 
4639   gcc_assert (suffix != NULL);
4640   bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4641   if (!bname)
4642     return NULL_TREE;
4643 
4644   strcpy (name, bname + sizeof ("__builtin_") - 1);
4645   strcat (name, suffix);
4646 
4647   if (n_args == 1)
4648     fntype = build_function_type_list (type_out, type_in, NULL);
4649   else if (n_args == 2)
4650     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4651   else
4652     gcc_unreachable ();
4653 
4654   /* Build a function declaration for the vectorized function.  */
4655   new_fndecl = build_decl (BUILTINS_LOCATION,
4656 			   FUNCTION_DECL, get_identifier (name), fntype);
4657   TREE_PUBLIC (new_fndecl) = 1;
4658   DECL_EXTERNAL (new_fndecl) = 1;
4659   DECL_IS_NOVOPS (new_fndecl) = 1;
4660   TREE_READONLY (new_fndecl) = 1;
4661 
4662   return new_fndecl;
4663 }
4664 
4665 /* Returns a function decl for a vectorized version of the builtin function
4666    with builtin function code FN and the result vector type TYPE, or NULL_TREE
4667    if it is not available.  */
4668 
4669 static tree
rs6000_builtin_vectorized_function(tree fndecl,tree type_out,tree type_in)4670 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4671 				    tree type_in)
4672 {
4673   enum machine_mode in_mode, out_mode;
4674   int in_n, out_n;
4675 
4676   if (TARGET_DEBUG_BUILTIN)
4677     fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4678 	     IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4679 	     GET_MODE_NAME (TYPE_MODE (type_out)),
4680 	     GET_MODE_NAME (TYPE_MODE (type_in)));
4681 
4682   if (TREE_CODE (type_out) != VECTOR_TYPE
4683       || TREE_CODE (type_in) != VECTOR_TYPE
4684       || !TARGET_VECTORIZE_BUILTINS)
4685     return NULL_TREE;
4686 
4687   out_mode = TYPE_MODE (TREE_TYPE (type_out));
4688   out_n = TYPE_VECTOR_SUBPARTS (type_out);
4689   in_mode = TYPE_MODE (TREE_TYPE (type_in));
4690   in_n = TYPE_VECTOR_SUBPARTS (type_in);
4691 
4692   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4693     {
4694       enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4695       switch (fn)
4696 	{
4697 	case BUILT_IN_CLZIMAX:
4698 	case BUILT_IN_CLZLL:
4699 	case BUILT_IN_CLZL:
4700 	case BUILT_IN_CLZ:
4701 	  if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4702 	    {
4703 	      if (out_mode == QImode && out_n == 16)
4704 		return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4705 	      else if (out_mode == HImode && out_n == 8)
4706 		return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4707 	      else if (out_mode == SImode && out_n == 4)
4708 		return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4709 	      else if (out_mode == DImode && out_n == 2)
4710 		return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4711 	    }
4712 	  break;
4713 	case BUILT_IN_COPYSIGN:
4714 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4715 	      && out_mode == DFmode && out_n == 2
4716 	      && in_mode == DFmode && in_n == 2)
4717 	    return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4718 	  break;
4719 	case BUILT_IN_COPYSIGNF:
4720 	  if (out_mode != SFmode || out_n != 4
4721 	      || in_mode != SFmode || in_n != 4)
4722 	    break;
4723 	  if (VECTOR_UNIT_VSX_P (V4SFmode))
4724 	    return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4725 	  if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4726 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4727 	  break;
4728 	case BUILT_IN_POPCOUNTIMAX:
4729 	case BUILT_IN_POPCOUNTLL:
4730 	case BUILT_IN_POPCOUNTL:
4731 	case BUILT_IN_POPCOUNT:
4732 	  if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4733 	    {
4734 	      if (out_mode == QImode && out_n == 16)
4735 		return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4736 	      else if (out_mode == HImode && out_n == 8)
4737 		return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4738 	      else if (out_mode == SImode && out_n == 4)
4739 		return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4740 	      else if (out_mode == DImode && out_n == 2)
4741 		return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4742 	    }
4743 	  break;
4744 	case BUILT_IN_SQRT:
4745 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4746 	      && out_mode == DFmode && out_n == 2
4747 	      && in_mode == DFmode && in_n == 2)
4748 	    return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4749 	  break;
4750 	case BUILT_IN_SQRTF:
4751 	  if (VECTOR_UNIT_VSX_P (V4SFmode)
4752 	      && out_mode == SFmode && out_n == 4
4753 	      && in_mode == SFmode && in_n == 4)
4754 	    return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4755 	  break;
4756 	case BUILT_IN_CEIL:
4757 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4758 	      && out_mode == DFmode && out_n == 2
4759 	      && in_mode == DFmode && in_n == 2)
4760 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4761 	  break;
4762 	case BUILT_IN_CEILF:
4763 	  if (out_mode != SFmode || out_n != 4
4764 	      || in_mode != SFmode || in_n != 4)
4765 	    break;
4766 	  if (VECTOR_UNIT_VSX_P (V4SFmode))
4767 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4768 	  if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4769 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4770 	  break;
4771 	case BUILT_IN_FLOOR:
4772 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4773 	      && out_mode == DFmode && out_n == 2
4774 	      && in_mode == DFmode && in_n == 2)
4775 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4776 	  break;
4777 	case BUILT_IN_FLOORF:
4778 	  if (out_mode != SFmode || out_n != 4
4779 	      || in_mode != SFmode || in_n != 4)
4780 	    break;
4781 	  if (VECTOR_UNIT_VSX_P (V4SFmode))
4782 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4783 	  if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4784 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4785 	  break;
4786 	case BUILT_IN_FMA:
4787 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4788 	      && out_mode == DFmode && out_n == 2
4789 	      && in_mode == DFmode && in_n == 2)
4790 	    return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4791 	  break;
4792 	case BUILT_IN_FMAF:
4793 	  if (VECTOR_UNIT_VSX_P (V4SFmode)
4794 	      && out_mode == SFmode && out_n == 4
4795 	      && in_mode == SFmode && in_n == 4)
4796 	    return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4797 	  else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4798 	      && out_mode == SFmode && out_n == 4
4799 	      && in_mode == SFmode && in_n == 4)
4800 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4801 	  break;
4802 	case BUILT_IN_TRUNC:
4803 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4804 	      && out_mode == DFmode && out_n == 2
4805 	      && in_mode == DFmode && in_n == 2)
4806 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4807 	  break;
4808 	case BUILT_IN_TRUNCF:
4809 	  if (out_mode != SFmode || out_n != 4
4810 	      || in_mode != SFmode || in_n != 4)
4811 	    break;
4812 	  if (VECTOR_UNIT_VSX_P (V4SFmode))
4813 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4814 	  if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4815 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4816 	  break;
4817 	case BUILT_IN_NEARBYINT:
4818 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4819 	      && flag_unsafe_math_optimizations
4820 	      && out_mode == DFmode && out_n == 2
4821 	      && in_mode == DFmode && in_n == 2)
4822 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4823 	  break;
4824 	case BUILT_IN_NEARBYINTF:
4825 	  if (VECTOR_UNIT_VSX_P (V4SFmode)
4826 	      && flag_unsafe_math_optimizations
4827 	      && out_mode == SFmode && out_n == 4
4828 	      && in_mode == SFmode && in_n == 4)
4829 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4830 	  break;
4831 	case BUILT_IN_RINT:
4832 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4833 	      && !flag_trapping_math
4834 	      && out_mode == DFmode && out_n == 2
4835 	      && in_mode == DFmode && in_n == 2)
4836 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4837 	  break;
4838 	case BUILT_IN_RINTF:
4839 	  if (VECTOR_UNIT_VSX_P (V4SFmode)
4840 	      && !flag_trapping_math
4841 	      && out_mode == SFmode && out_n == 4
4842 	      && in_mode == SFmode && in_n == 4)
4843 	    return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4844 	  break;
4845 	default:
4846 	  break;
4847 	}
4848     }
4849 
4850   else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4851     {
4852       enum rs6000_builtins fn
4853 	= (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4854       switch (fn)
4855 	{
4856 	case RS6000_BUILTIN_RSQRTF:
4857 	  if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4858 	      && out_mode == SFmode && out_n == 4
4859 	      && in_mode == SFmode && in_n == 4)
4860 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4861 	  break;
4862 	case RS6000_BUILTIN_RSQRT:
4863 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4864 	      && out_mode == DFmode && out_n == 2
4865 	      && in_mode == DFmode && in_n == 2)
4866 	    return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4867 	  break;
4868 	case RS6000_BUILTIN_RECIPF:
4869 	  if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4870 	      && out_mode == SFmode && out_n == 4
4871 	      && in_mode == SFmode && in_n == 4)
4872 	    return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4873 	  break;
4874 	case RS6000_BUILTIN_RECIP:
4875 	  if (VECTOR_UNIT_VSX_P (V2DFmode)
4876 	      && out_mode == DFmode && out_n == 2
4877 	      && in_mode == DFmode && in_n == 2)
4878 	    return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4879 	  break;
4880 	default:
4881 	  break;
4882 	}
4883     }
4884 
4885   /* Generate calls to libmass if appropriate.  */
4886   if (rs6000_veclib_handler)
4887     return rs6000_veclib_handler (fndecl, type_out, type_in);
4888 
4889   return NULL_TREE;
4890 }
4891 
4892 /* Default CPU string for rs6000*_file_start functions.  */
4893 static const char *rs6000_default_cpu;
4894 
4895 /* Do anything needed at the start of the asm file.  */
4896 
4897 static void
rs6000_file_start(void)4898 rs6000_file_start (void)
4899 {
4900   char buffer[80];
4901   const char *start = buffer;
4902   FILE *file = asm_out_file;
4903 
4904   rs6000_default_cpu = TARGET_CPU_DEFAULT;
4905 
4906   default_file_start ();
4907 
4908   if (flag_verbose_asm)
4909     {
4910       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4911 
4912       if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4913 	{
4914 	  fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4915 	  start = "";
4916 	}
4917 
4918       if (global_options_set.x_rs6000_cpu_index)
4919 	{
4920 	  fprintf (file, "%s -mcpu=%s", start,
4921 		   processor_target_table[rs6000_cpu_index].name);
4922 	  start = "";
4923 	}
4924 
4925       if (global_options_set.x_rs6000_tune_index)
4926 	{
4927 	  fprintf (file, "%s -mtune=%s", start,
4928 		   processor_target_table[rs6000_tune_index].name);
4929 	  start = "";
4930 	}
4931 
4932       if (PPC405_ERRATUM77)
4933 	{
4934 	  fprintf (file, "%s PPC405CR_ERRATUM77", start);
4935 	  start = "";
4936 	}
4937 
4938 #ifdef USING_ELFOS_H
4939       switch (rs6000_sdata)
4940 	{
4941 	case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4942 	case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4943 	case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4944 	case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4945 	}
4946 
4947       if (rs6000_sdata && g_switch_value)
4948 	{
4949 	  fprintf (file, "%s -G %d", start,
4950 		   g_switch_value);
4951 	  start = "";
4952 	}
4953 #endif
4954 
4955       if (*start == '\0')
4956 	putc ('\n', file);
4957     }
4958 
4959   if (DEFAULT_ABI == ABI_ELFv2)
4960     fprintf (file, "\t.abiversion 2\n");
4961 
4962   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2
4963       || (TARGET_ELF && flag_pic == 2))
4964     {
4965       switch_to_section (toc_section);
4966       switch_to_section (text_section);
4967     }
4968 
4969 #ifdef USING_ELFOS_H
4970   if (rs6000_default_cpu == 0 || rs6000_default_cpu[0] == '\0'
4971       || !global_options_set.x_rs6000_cpu_index)
4972     {
4973       fputs ("\t.machine ", asm_out_file);
4974       if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
4975 	fputs ("power8\n", asm_out_file);
4976       else if ((rs6000_isa_flags & OPTION_MASK_POPCNTD) != 0)
4977 	fputs ("power7\n", asm_out_file);
4978       else if ((rs6000_isa_flags & OPTION_MASK_CMPB) != 0)
4979 	fputs ("power6\n", asm_out_file);
4980       else if ((rs6000_isa_flags & OPTION_MASK_POPCNTB) != 0)
4981 	fputs ("power5\n", asm_out_file);
4982       else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
4983 	fputs ("power4\n", asm_out_file);
4984       else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
4985 	fputs ("ppc64\n", asm_out_file);
4986       else
4987 	fputs ("ppc\n", asm_out_file);
4988     }
4989 #endif
4990 }
4991 
4992 
4993 /* Return nonzero if this function is known to have a null epilogue.  */
4994 
4995 int
direct_return(void)4996 direct_return (void)
4997 {
4998   if (reload_completed)
4999     {
5000       rs6000_stack_t *info = rs6000_stack_info ();
5001 
5002       if (info->first_gp_reg_save == 32
5003 	  && info->first_fp_reg_save == 64
5004 	  && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
5005 	  && ! info->lr_save_p
5006 	  && ! info->cr_save_p
5007 	  && info->vrsave_mask == 0
5008 	  && ! info->push_p)
5009 	return 1;
5010     }
5011 
5012   return 0;
5013 }
5014 
5015 /* Return the number of instructions it takes to form a constant in an
5016    integer register.  */
5017 
5018 int
num_insns_constant_wide(HOST_WIDE_INT value)5019 num_insns_constant_wide (HOST_WIDE_INT value)
5020 {
5021   /* signed constant loadable with addi */
5022   if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
5023     return 1;
5024 
5025   /* constant loadable with addis */
5026   else if ((value & 0xffff) == 0
5027 	   && (value >> 31 == -1 || value >> 31 == 0))
5028     return 1;
5029 
5030 #if HOST_BITS_PER_WIDE_INT == 64
5031   else if (TARGET_POWERPC64)
5032     {
5033       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
5034       HOST_WIDE_INT high = value >> 31;
5035 
5036       if (high == 0 || high == -1)
5037 	return 2;
5038 
5039       high >>= 1;
5040 
5041       if (low == 0)
5042 	return num_insns_constant_wide (high) + 1;
5043       else if (high == 0)
5044 	return num_insns_constant_wide (low) + 1;
5045       else
5046 	return (num_insns_constant_wide (high)
5047 		+ num_insns_constant_wide (low) + 1);
5048     }
5049 #endif
5050 
5051   else
5052     return 2;
5053 }
5054 
5055 int
num_insns_constant(rtx op,enum machine_mode mode)5056 num_insns_constant (rtx op, enum machine_mode mode)
5057 {
5058   HOST_WIDE_INT low, high;
5059 
5060   switch (GET_CODE (op))
5061     {
5062     case CONST_INT:
5063 #if HOST_BITS_PER_WIDE_INT == 64
5064       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
5065 	  && mask64_operand (op, mode))
5066 	return 2;
5067       else
5068 #endif
5069 	return num_insns_constant_wide (INTVAL (op));
5070 
5071       case CONST_DOUBLE:
5072 	if (mode == SFmode || mode == SDmode)
5073 	  {
5074 	    long l;
5075 	    REAL_VALUE_TYPE rv;
5076 
5077 	    REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
5078 	    if (DECIMAL_FLOAT_MODE_P (mode))
5079 	      REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
5080 	    else
5081 	      REAL_VALUE_TO_TARGET_SINGLE (rv, l);
5082 	    return num_insns_constant_wide ((HOST_WIDE_INT) l);
5083 	  }
5084 
5085 	if (mode == VOIDmode || mode == DImode)
5086 	  {
5087 	    high = CONST_DOUBLE_HIGH (op);
5088 	    low  = CONST_DOUBLE_LOW (op);
5089 	  }
5090 	else
5091 	  {
5092 	    long l[2];
5093 	    REAL_VALUE_TYPE rv;
5094 
5095 	    REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
5096 	    if (DECIMAL_FLOAT_MODE_P (mode))
5097 	      REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
5098 	    else
5099 	      REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
5100 	    high = l[WORDS_BIG_ENDIAN == 0];
5101 	    low  = l[WORDS_BIG_ENDIAN != 0];
5102 	  }
5103 
5104 	if (TARGET_32BIT)
5105 	  return (num_insns_constant_wide (low)
5106 		  + num_insns_constant_wide (high));
5107 	else
5108 	  {
5109 	    if ((high == 0 && low >= 0)
5110 		|| (high == -1 && low < 0))
5111 	      return num_insns_constant_wide (low);
5112 
5113 	    else if (mask64_operand (op, mode))
5114 	      return 2;
5115 
5116 	    else if (low == 0)
5117 	      return num_insns_constant_wide (high) + 1;
5118 
5119 	    else
5120 	      return (num_insns_constant_wide (high)
5121 		      + num_insns_constant_wide (low) + 1);
5122 	  }
5123 
5124     default:
5125       gcc_unreachable ();
5126     }
5127 }
5128 
5129 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
5130    If the mode of OP is MODE_VECTOR_INT, this simply returns the
5131    corresponding element of the vector, but for V4SFmode and V2SFmode,
5132    the corresponding "float" is interpreted as an SImode integer.  */
5133 
5134 HOST_WIDE_INT
const_vector_elt_as_int(rtx op,unsigned int elt)5135 const_vector_elt_as_int (rtx op, unsigned int elt)
5136 {
5137   rtx tmp;
5138 
5139   /* We can't handle V2DImode and V2DFmode vector constants here yet.  */
5140   gcc_assert (GET_MODE (op) != V2DImode
5141 	      && GET_MODE (op) != V2DFmode);
5142 
5143   tmp = CONST_VECTOR_ELT (op, elt);
5144   if (GET_MODE (op) == V4SFmode
5145       || GET_MODE (op) == V2SFmode)
5146     tmp = gen_lowpart (SImode, tmp);
5147   return INTVAL (tmp);
5148 }
5149 
5150 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
5151    or vspltisw instruction.  OP is a CONST_VECTOR.  Which instruction is used
5152    depends on STEP and COPIES, one of which will be 1.  If COPIES > 1,
5153    all items are set to the same value and contain COPIES replicas of the
5154    vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
5155    operand and the others are set to the value of the operand's msb.  */
5156 
5157 static bool
vspltis_constant(rtx op,unsigned step,unsigned copies)5158 vspltis_constant (rtx op, unsigned step, unsigned copies)
5159 {
5160   enum machine_mode mode = GET_MODE (op);
5161   enum machine_mode inner = GET_MODE_INNER (mode);
5162 
5163   unsigned i;
5164   unsigned nunits;
5165   unsigned bitsize;
5166   unsigned mask;
5167 
5168   HOST_WIDE_INT val;
5169   HOST_WIDE_INT splat_val;
5170   HOST_WIDE_INT msb_val;
5171 
5172   if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
5173     return false;
5174 
5175   nunits = GET_MODE_NUNITS (mode);
5176   bitsize = GET_MODE_BITSIZE (inner);
5177   mask = GET_MODE_MASK (inner);
5178 
5179   val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5180   splat_val = val;
5181   msb_val = val >= 0 ? 0 : -1;
5182 
5183   /* Construct the value to be splatted, if possible.  If not, return 0.  */
5184   for (i = 2; i <= copies; i *= 2)
5185     {
5186       HOST_WIDE_INT small_val;
5187       bitsize /= 2;
5188       small_val = splat_val >> bitsize;
5189       mask >>= bitsize;
5190       if (splat_val != ((small_val << bitsize) | (small_val & mask)))
5191 	return false;
5192       splat_val = small_val;
5193     }
5194 
5195   /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw].  */
5196   if (EASY_VECTOR_15 (splat_val))
5197     ;
5198 
5199   /* Also check if we can splat, and then add the result to itself.  Do so if
5200      the value is positive, of if the splat instruction is using OP's mode;
5201      for splat_val < 0, the splat and the add should use the same mode.  */
5202   else if (EASY_VECTOR_15_ADD_SELF (splat_val)
5203            && (splat_val >= 0 || (step == 1 && copies == 1)))
5204     ;
5205 
5206   /* Also check if are loading up the most significant bit which can be done by
5207      loading up -1 and shifting the value left by -1.  */
5208   else if (EASY_VECTOR_MSB (splat_val, inner))
5209     ;
5210 
5211   else
5212     return false;
5213 
5214   /* Check if VAL is present in every STEP-th element, and the
5215      other elements are filled with its most significant bit.  */
5216   for (i = 1; i < nunits; ++i)
5217     {
5218       HOST_WIDE_INT desired_val;
5219       unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
5220       if ((i & (step - 1)) == 0)
5221 	desired_val = val;
5222       else
5223 	desired_val = msb_val;
5224 
5225       if (desired_val != const_vector_elt_as_int (op, elt))
5226 	return false;
5227     }
5228 
5229   return true;
5230 }
5231 
5232 
5233 /* Return true if OP is of the given MODE and can be synthesized
5234    with a vspltisb, vspltish or vspltisw.  */
5235 
5236 bool
easy_altivec_constant(rtx op,enum machine_mode mode)5237 easy_altivec_constant (rtx op, enum machine_mode mode)
5238 {
5239   unsigned step, copies;
5240 
5241   if (mode == VOIDmode)
5242     mode = GET_MODE (op);
5243   else if (mode != GET_MODE (op))
5244     return false;
5245 
5246   /* V2DI/V2DF was added with VSX.  Only allow 0 and all 1's as easy
5247      constants.  */
5248   if (mode == V2DFmode)
5249     return zero_constant (op, mode);
5250 
5251   else if (mode == V2DImode)
5252     {
5253       /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
5254 	 easy.  */
5255       if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
5256 	  || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
5257 	return false;
5258 
5259       if (zero_constant (op, mode))
5260 	return true;
5261 
5262       if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
5263 	  && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
5264 	return true;
5265 
5266       return false;
5267     }
5268 
5269   /* V1TImode is a special container for TImode.  Ignore for now.  */
5270   else if (mode == V1TImode)
5271     return false;
5272 
5273   /* Start with a vspltisw.  */
5274   step = GET_MODE_NUNITS (mode) / 4;
5275   copies = 1;
5276 
5277   if (vspltis_constant (op, step, copies))
5278     return true;
5279 
5280   /* Then try with a vspltish.  */
5281   if (step == 1)
5282     copies <<= 1;
5283   else
5284     step >>= 1;
5285 
5286   if (vspltis_constant (op, step, copies))
5287     return true;
5288 
5289   /* And finally a vspltisb.  */
5290   if (step == 1)
5291     copies <<= 1;
5292   else
5293     step >>= 1;
5294 
5295   if (vspltis_constant (op, step, copies))
5296     return true;
5297 
5298   return false;
5299 }
5300 
5301 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
5302    result is OP.  Abort if it is not possible.  */
5303 
5304 rtx
gen_easy_altivec_constant(rtx op)5305 gen_easy_altivec_constant (rtx op)
5306 {
5307   enum machine_mode mode = GET_MODE (op);
5308   int nunits = GET_MODE_NUNITS (mode);
5309   rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5310   unsigned step = nunits / 4;
5311   unsigned copies = 1;
5312 
5313   /* Start with a vspltisw.  */
5314   if (vspltis_constant (op, step, copies))
5315     return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
5316 
5317   /* Then try with a vspltish.  */
5318   if (step == 1)
5319     copies <<= 1;
5320   else
5321     step >>= 1;
5322 
5323   if (vspltis_constant (op, step, copies))
5324     return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
5325 
5326   /* And finally a vspltisb.  */
5327   if (step == 1)
5328     copies <<= 1;
5329   else
5330     step >>= 1;
5331 
5332   if (vspltis_constant (op, step, copies))
5333     return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
5334 
5335   gcc_unreachable ();
5336 }
5337 
5338 const char *
output_vec_const_move(rtx * operands)5339 output_vec_const_move (rtx *operands)
5340 {
5341   int cst, cst2;
5342   enum machine_mode mode;
5343   rtx dest, vec;
5344 
5345   dest = operands[0];
5346   vec = operands[1];
5347   mode = GET_MODE (dest);
5348 
5349   if (TARGET_VSX)
5350     {
5351       if (zero_constant (vec, mode))
5352 	return "xxlxor %x0,%x0,%x0";
5353 
5354       if ((mode == V2DImode || mode == V1TImode)
5355 	  && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
5356 	  && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
5357 	return "vspltisw %0,-1";
5358     }
5359 
5360   if (TARGET_ALTIVEC)
5361     {
5362       rtx splat_vec;
5363       if (zero_constant (vec, mode))
5364 	return "vxor %0,%0,%0";
5365 
5366       splat_vec = gen_easy_altivec_constant (vec);
5367       gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
5368       operands[1] = XEXP (splat_vec, 0);
5369       if (!EASY_VECTOR_15 (INTVAL (operands[1])))
5370 	return "#";
5371 
5372       switch (GET_MODE (splat_vec))
5373 	{
5374 	case V4SImode:
5375 	  return "vspltisw %0,%1";
5376 
5377 	case V8HImode:
5378 	  return "vspltish %0,%1";
5379 
5380 	case V16QImode:
5381 	  return "vspltisb %0,%1";
5382 
5383 	default:
5384 	  gcc_unreachable ();
5385 	}
5386     }
5387 
5388   gcc_assert (TARGET_SPE);
5389 
5390   /* Vector constant 0 is handled as a splitter of V2SI, and in the
5391      pattern of V1DI, V4HI, and V2SF.
5392 
5393      FIXME: We should probably return # and add post reload
5394      splitters for these, but this way is so easy ;-).  */
5395   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
5396   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
5397   operands[1] = CONST_VECTOR_ELT (vec, 0);
5398   operands[2] = CONST_VECTOR_ELT (vec, 1);
5399   if (cst == cst2)
5400     return "li %0,%1\n\tevmergelo %0,%0,%0";
5401   else
5402     return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
5403 }
5404 
5405 /* Initialize TARGET of vector PAIRED to VALS.  */
5406 
5407 void
paired_expand_vector_init(rtx target,rtx vals)5408 paired_expand_vector_init (rtx target, rtx vals)
5409 {
5410   enum machine_mode mode = GET_MODE (target);
5411   int n_elts = GET_MODE_NUNITS (mode);
5412   int n_var = 0;
5413   rtx x, new_rtx, tmp, constant_op, op1, op2;
5414   int i;
5415 
5416   for (i = 0; i < n_elts; ++i)
5417     {
5418       x = XVECEXP (vals, 0, i);
5419       if (!(CONST_INT_P (x)
5420 	    || GET_CODE (x) == CONST_DOUBLE
5421 	    || GET_CODE (x) == CONST_FIXED))
5422 	++n_var;
5423     }
5424   if (n_var == 0)
5425     {
5426       /* Load from constant pool.  */
5427       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
5428       return;
5429     }
5430 
5431   if (n_var == 2)
5432     {
5433       /* The vector is initialized only with non-constants.  */
5434       new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
5435 				XVECEXP (vals, 0, 1));
5436 
5437       emit_move_insn (target, new_rtx);
5438       return;
5439     }
5440 
5441   /* One field is non-constant and the other one is a constant.  Load the
5442      constant from the constant pool and use ps_merge instruction to
5443      construct the whole vector.  */
5444   op1 = XVECEXP (vals, 0, 0);
5445   op2 = XVECEXP (vals, 0, 1);
5446 
5447   constant_op = (CONSTANT_P (op1)) ? op1 : op2;
5448 
5449   tmp = gen_reg_rtx (GET_MODE (constant_op));
5450   emit_move_insn (tmp, constant_op);
5451 
5452   if (CONSTANT_P (op1))
5453     new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
5454   else
5455     new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
5456 
5457   emit_move_insn (target, new_rtx);
5458 }
5459 
5460 void
paired_expand_vector_move(rtx operands[])5461 paired_expand_vector_move (rtx operands[])
5462 {
5463   rtx op0 = operands[0], op1 = operands[1];
5464 
5465   emit_move_insn (op0, op1);
5466 }
5467 
5468 /* Emit vector compare for code RCODE.  DEST is destination, OP1 and
5469    OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
5470    operands for the relation operation COND.  This is a recursive
5471    function.  */
5472 
5473 static void
paired_emit_vector_compare(enum rtx_code rcode,rtx dest,rtx op0,rtx op1,rtx cc_op0,rtx cc_op1)5474 paired_emit_vector_compare (enum rtx_code rcode,
5475                             rtx dest, rtx op0, rtx op1,
5476                             rtx cc_op0, rtx cc_op1)
5477 {
5478   rtx tmp = gen_reg_rtx (V2SFmode);
5479   rtx tmp1, max, min;
5480 
5481   gcc_assert (TARGET_PAIRED_FLOAT);
5482   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5483 
5484   switch (rcode)
5485     {
5486     case LT:
5487     case LTU:
5488       paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5489       return;
5490     case GE:
5491     case GEU:
5492       emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5493       emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5494       return;
5495     case LE:
5496     case LEU:
5497       paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5498       return;
5499     case GT:
5500       paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5501       return;
5502     case EQ:
5503       tmp1 = gen_reg_rtx (V2SFmode);
5504       max = gen_reg_rtx (V2SFmode);
5505       min = gen_reg_rtx (V2SFmode);
5506       gen_reg_rtx (V2SFmode);
5507 
5508       emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5509       emit_insn (gen_selv2sf4
5510                  (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5511       emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5512       emit_insn (gen_selv2sf4
5513                  (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5514       emit_insn (gen_subv2sf3 (tmp1, min, max));
5515       emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5516       return;
5517     case NE:
5518       paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5519       return;
5520     case UNLE:
5521       paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5522       return;
5523     case UNLT:
5524       paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5525       return;
5526     case UNGE:
5527       paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5528       return;
5529     case UNGT:
5530       paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5531       return;
5532     default:
5533       gcc_unreachable ();
5534     }
5535 
5536   return;
5537 }
5538 
5539 /* Emit vector conditional expression.
5540    DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5541    CC_OP0 and CC_OP1 are the two operands for the relation operation COND.  */
5542 
5543 int
paired_emit_vector_cond_expr(rtx dest,rtx op1,rtx op2,rtx cond,rtx cc_op0,rtx cc_op1)5544 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5545 			      rtx cond, rtx cc_op0, rtx cc_op1)
5546 {
5547   enum rtx_code rcode = GET_CODE (cond);
5548 
5549   if (!TARGET_PAIRED_FLOAT)
5550     return 0;
5551 
5552   paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5553 
5554   return 1;
5555 }
5556 
5557 /* Initialize vector TARGET to VALS.  */
5558 
5559 void
rs6000_expand_vector_init(rtx target,rtx vals)5560 rs6000_expand_vector_init (rtx target, rtx vals)
5561 {
5562   enum machine_mode mode = GET_MODE (target);
5563   enum machine_mode inner_mode = GET_MODE_INNER (mode);
5564   int n_elts = GET_MODE_NUNITS (mode);
5565   int n_var = 0, one_var = -1;
5566   bool all_same = true, all_const_zero = true;
5567   rtx x, mem;
5568   int i;
5569 
5570   for (i = 0; i < n_elts; ++i)
5571     {
5572       x = XVECEXP (vals, 0, i);
5573       if (!(CONST_INT_P (x)
5574 	    || GET_CODE (x) == CONST_DOUBLE
5575 	    || GET_CODE (x) == CONST_FIXED))
5576 	++n_var, one_var = i;
5577       else if (x != CONST0_RTX (inner_mode))
5578 	all_const_zero = false;
5579 
5580       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5581 	all_same = false;
5582     }
5583 
5584   if (n_var == 0)
5585     {
5586       rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5587       bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5588       if ((int_vector_p || TARGET_VSX) && all_const_zero)
5589 	{
5590 	  /* Zero register.  */
5591 	  emit_insn (gen_rtx_SET (VOIDmode, target,
5592 				  gen_rtx_XOR (mode, target, target)));
5593 	  return;
5594 	}
5595       else if (int_vector_p && easy_vector_constant (const_vec, mode))
5596 	{
5597 	  /* Splat immediate.  */
5598 	  emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5599 	  return;
5600 	}
5601       else
5602 	{
5603 	  /* Load from constant pool.  */
5604 	  emit_move_insn (target, const_vec);
5605 	  return;
5606 	}
5607     }
5608 
5609   /* Double word values on VSX can use xxpermdi or lxvdsx.  */
5610   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5611     {
5612       rtx op0 = XVECEXP (vals, 0, 0);
5613       rtx op1 = XVECEXP (vals, 0, 1);
5614       if (all_same)
5615 	{
5616 	  if (!MEM_P (op0) && !REG_P (op0))
5617 	    op0 = force_reg (inner_mode, op0);
5618 	  if (mode == V2DFmode)
5619 	    emit_insn (gen_vsx_splat_v2df (target, op0));
5620 	  else
5621 	    emit_insn (gen_vsx_splat_v2di (target, op0));
5622 	}
5623       else
5624 	{
5625 	  op0 = force_reg (inner_mode, op0);
5626 	  op1 = force_reg (inner_mode, op1);
5627 	  if (mode == V2DFmode)
5628 	    emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5629 	  else
5630 	    emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5631 	}
5632       return;
5633     }
5634 
5635   /* With single precision floating point on VSX, know that internally single
5636      precision is actually represented as a double, and either make 2 V2DF
5637      vectors, and convert these vectors to single precision, or do one
5638      conversion, and splat the result to the other elements.  */
5639   if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5640     {
5641       if (all_same)
5642 	{
5643 	  rtx freg = gen_reg_rtx (V4SFmode);
5644 	  rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5645 	  rtx cvt  = ((TARGET_XSCVDPSPN)
5646 		      ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5647 		      : gen_vsx_xscvdpsp_scalar (freg, sreg));
5648 
5649 	  emit_insn (cvt);
5650 	  emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg, const0_rtx));
5651 	}
5652       else
5653 	{
5654 	  rtx dbl_even = gen_reg_rtx (V2DFmode);
5655 	  rtx dbl_odd  = gen_reg_rtx (V2DFmode);
5656 	  rtx flt_even = gen_reg_rtx (V4SFmode);
5657 	  rtx flt_odd  = gen_reg_rtx (V4SFmode);
5658 	  rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5659 	  rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5660 	  rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5661 	  rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5662 
5663 	  emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5664 	  emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5665 	  emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5666 	  emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5667 	  rs6000_expand_extract_even (target, flt_even, flt_odd);
5668 	}
5669       return;
5670     }
5671 
5672   /* Store value to stack temp.  Load vector element.  Splat.  However, splat
5673      of 64-bit items is not supported on Altivec.  */
5674   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5675     {
5676       mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5677       emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5678 		      XVECEXP (vals, 0, 0));
5679       x = gen_rtx_UNSPEC (VOIDmode,
5680 			  gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5681       emit_insn (gen_rtx_PARALLEL (VOIDmode,
5682 				   gen_rtvec (2,
5683 					      gen_rtx_SET (VOIDmode,
5684 							   target, mem),
5685 					      x)));
5686       x = gen_rtx_VEC_SELECT (inner_mode, target,
5687 			      gen_rtx_PARALLEL (VOIDmode,
5688 						gen_rtvec (1, const0_rtx)));
5689       emit_insn (gen_rtx_SET (VOIDmode, target,
5690 			      gen_rtx_VEC_DUPLICATE (mode, x)));
5691       return;
5692     }
5693 
5694   /* One field is non-constant.  Load constant then overwrite
5695      varying field.  */
5696   if (n_var == 1)
5697     {
5698       rtx copy = copy_rtx (vals);
5699 
5700       /* Load constant part of vector, substitute neighboring value for
5701 	 varying element.  */
5702       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5703       rs6000_expand_vector_init (target, copy);
5704 
5705       /* Insert variable.  */
5706       rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5707       return;
5708     }
5709 
5710   /* Construct the vector in memory one field at a time
5711      and load the whole vector.  */
5712   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5713   for (i = 0; i < n_elts; i++)
5714     emit_move_insn (adjust_address_nv (mem, inner_mode,
5715 				    i * GET_MODE_SIZE (inner_mode)),
5716 		    XVECEXP (vals, 0, i));
5717   emit_move_insn (target, mem);
5718 }
5719 
5720 /* Set field ELT of TARGET to VAL.  */
5721 
5722 void
rs6000_expand_vector_set(rtx target,rtx val,int elt)5723 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5724 {
5725   enum machine_mode mode = GET_MODE (target);
5726   enum machine_mode inner_mode = GET_MODE_INNER (mode);
5727   rtx reg = gen_reg_rtx (mode);
5728   rtx mask, mem, x;
5729   int width = GET_MODE_SIZE (inner_mode);
5730   int i;
5731 
5732   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5733     {
5734       rtx (*set_func) (rtx, rtx, rtx, rtx)
5735 	= ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5736       emit_insn (set_func (target, target, val, GEN_INT (elt)));
5737       return;
5738     }
5739 
5740   /* Simplify setting single element vectors like V1TImode.  */
5741   if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
5742     {
5743       emit_move_insn (target, gen_lowpart (mode, val));
5744       return;
5745     }
5746 
5747   /* Load single variable value.  */
5748   mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5749   emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5750   x = gen_rtx_UNSPEC (VOIDmode,
5751 		      gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5752   emit_insn (gen_rtx_PARALLEL (VOIDmode,
5753 			       gen_rtvec (2,
5754 					  gen_rtx_SET (VOIDmode,
5755 						       reg, mem),
5756 					  x)));
5757 
5758   /* Linear sequence.  */
5759   mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5760   for (i = 0; i < 16; ++i)
5761     XVECEXP (mask, 0, i) = GEN_INT (i);
5762 
5763   /* Set permute mask to insert element into target.  */
5764   for (i = 0; i < width; ++i)
5765     XVECEXP (mask, 0, elt*width + i)
5766       = GEN_INT (i + 0x10);
5767   x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5768 
5769   if (BYTES_BIG_ENDIAN)
5770     x = gen_rtx_UNSPEC (mode,
5771 			gen_rtvec (3, target, reg,
5772 				   force_reg (V16QImode, x)),
5773 			UNSPEC_VPERM);
5774   else
5775     {
5776       /* Invert selector.  We prefer to generate VNAND on P8 so
5777 	 that future fusion opportunities can kick in, but must
5778 	 generate VNOR elsewhere.  */
5779       rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
5780       rtx iorx = (TARGET_P8_VECTOR
5781 		  ? gen_rtx_IOR (V16QImode, notx, notx)
5782 		  : gen_rtx_AND (V16QImode, notx, notx));
5783       rtx tmp = gen_reg_rtx (V16QImode);
5784       emit_insn (gen_rtx_SET (VOIDmode, tmp, iorx));
5785 
5786       /* Permute with operands reversed and adjusted selector.  */
5787       x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
5788 			  UNSPEC_VPERM);
5789     }
5790 
5791   emit_insn (gen_rtx_SET (VOIDmode, target, x));
5792 }
5793 
5794 /* Extract field ELT from VEC into TARGET.  */
5795 
5796 void
rs6000_expand_vector_extract(rtx target,rtx vec,int elt)5797 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5798 {
5799   enum machine_mode mode = GET_MODE (vec);
5800   enum machine_mode inner_mode = GET_MODE_INNER (mode);
5801   rtx mem;
5802 
5803   if (VECTOR_MEM_VSX_P (mode))
5804     {
5805       switch (mode)
5806 	{
5807 	default:
5808 	  break;
5809 	case V1TImode:
5810 	  gcc_assert (elt == 0 && inner_mode == TImode);
5811 	  emit_move_insn (target, gen_lowpart (TImode, vec));
5812 	  break;
5813 	case V2DFmode:
5814 	  emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5815 	  return;
5816 	case V2DImode:
5817 	  emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5818 	  return;
5819 	case V4SFmode:
5820 	  emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5821 	  return;
5822 	}
5823     }
5824 
5825   /* Allocate mode-sized buffer.  */
5826   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5827 
5828   emit_move_insn (mem, vec);
5829 
5830   /* Add offset to field within buffer matching vector element.  */
5831   mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5832 
5833   emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5834 }
5835 
5836 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5837    implement ANDing by the mask IN.  */
5838 void
build_mask64_2_operands(rtx in,rtx * out)5839 build_mask64_2_operands (rtx in, rtx *out)
5840 {
5841 #if HOST_BITS_PER_WIDE_INT >= 64
5842   unsigned HOST_WIDE_INT c, lsb, m1, m2;
5843   int shift;
5844 
5845   gcc_assert (GET_CODE (in) == CONST_INT);
5846 
5847   c = INTVAL (in);
5848   if (c & 1)
5849     {
5850       /* Assume c initially something like 0x00fff000000fffff.  The idea
5851 	 is to rotate the word so that the middle ^^^^^^ group of zeros
5852 	 is at the MS end and can be cleared with an rldicl mask.  We then
5853 	 rotate back and clear off the MS    ^^ group of zeros with a
5854 	 second rldicl.  */
5855       c = ~c;			/*   c == 0xff000ffffff00000 */
5856       lsb = c & -c;		/* lsb == 0x0000000000100000 */
5857       m1 = -lsb;		/*  m1 == 0xfffffffffff00000 */
5858       c = ~c;			/*   c == 0x00fff000000fffff */
5859       c &= -lsb;		/*   c == 0x00fff00000000000 */
5860       lsb = c & -c;		/* lsb == 0x0000100000000000 */
5861       c = ~c;			/*   c == 0xff000fffffffffff */
5862       c &= -lsb;		/*   c == 0xff00000000000000 */
5863       shift = 0;
5864       while ((lsb >>= 1) != 0)
5865 	shift++;		/* shift == 44 on exit from loop */
5866       m1 <<= 64 - shift;	/*  m1 == 0xffffff0000000000 */
5867       m1 = ~m1;			/*  m1 == 0x000000ffffffffff */
5868       m2 = ~c;			/*  m2 == 0x00ffffffffffffff */
5869     }
5870   else
5871     {
5872       /* Assume c initially something like 0xff000f0000000000.  The idea
5873 	 is to rotate the word so that the     ^^^  middle group of zeros
5874 	 is at the LS end and can be cleared with an rldicr mask.  We then
5875 	 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5876 	 a second rldicr.  */
5877       lsb = c & -c;		/* lsb == 0x0000010000000000 */
5878       m2 = -lsb;		/*  m2 == 0xffffff0000000000 */
5879       c = ~c;			/*   c == 0x00fff0ffffffffff */
5880       c &= -lsb;		/*   c == 0x00fff00000000000 */
5881       lsb = c & -c;		/* lsb == 0x0000100000000000 */
5882       c = ~c;			/*   c == 0xff000fffffffffff */
5883       c &= -lsb;		/*   c == 0xff00000000000000 */
5884       shift = 0;
5885       while ((lsb >>= 1) != 0)
5886 	shift++;		/* shift == 44 on exit from loop */
5887       m1 = ~c;			/*  m1 == 0x00ffffffffffffff */
5888       m1 >>= shift;		/*  m1 == 0x0000000000000fff */
5889       m1 = ~m1;			/*  m1 == 0xfffffffffffff000 */
5890     }
5891 
5892   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5893      masks will be all 1's.  We are guaranteed more than one transition.  */
5894   out[0] = GEN_INT (64 - shift);
5895   out[1] = GEN_INT (m1);
5896   out[2] = GEN_INT (shift);
5897   out[3] = GEN_INT (m2);
5898 #else
5899   (void)in;
5900   (void)out;
5901   gcc_unreachable ();
5902 #endif
5903 }
5904 
5905 /* Return TRUE if OP is an invalid SUBREG operation on the e500.  */
5906 
5907 bool
invalid_e500_subreg(rtx op,enum machine_mode mode)5908 invalid_e500_subreg (rtx op, enum machine_mode mode)
5909 {
5910   if (TARGET_E500_DOUBLE)
5911     {
5912       /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5913 	 subreg:TI and reg:TF.  Decimal float modes are like integer
5914 	 modes (only low part of each register used) for this
5915 	 purpose.  */
5916       if (GET_CODE (op) == SUBREG
5917 	  && (mode == SImode || mode == DImode || mode == TImode
5918 	      || mode == DDmode || mode == TDmode || mode == PTImode)
5919 	  && REG_P (SUBREG_REG (op))
5920 	  && (GET_MODE (SUBREG_REG (op)) == DFmode
5921 	      || GET_MODE (SUBREG_REG (op)) == TFmode))
5922 	return true;
5923 
5924       /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5925 	 reg:TI.  */
5926       if (GET_CODE (op) == SUBREG
5927 	  && (mode == DFmode || mode == TFmode)
5928 	  && REG_P (SUBREG_REG (op))
5929 	  && (GET_MODE (SUBREG_REG (op)) == DImode
5930 	      || GET_MODE (SUBREG_REG (op)) == TImode
5931 	      || GET_MODE (SUBREG_REG (op)) == PTImode
5932 	      || GET_MODE (SUBREG_REG (op)) == DDmode
5933 	      || GET_MODE (SUBREG_REG (op)) == TDmode))
5934 	return true;
5935     }
5936 
5937   if (TARGET_SPE
5938       && GET_CODE (op) == SUBREG
5939       && mode == SImode
5940       && REG_P (SUBREG_REG (op))
5941       && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5942     return true;
5943 
5944   return false;
5945 }
5946 
5947 /* Return alignment of TYPE.  Existing alignment is ALIGN.  HOW
5948    selects whether the alignment is abi mandated, optional, or
5949    both abi and optional alignment.  */
5950 
5951 unsigned int
rs6000_data_alignment(tree type,unsigned int align,enum data_align how)5952 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5953 {
5954   if (how != align_opt)
5955     {
5956       if (TREE_CODE (type) == VECTOR_TYPE)
5957 	{
5958 	  if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5959 	      || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5960 	    {
5961 	      if (align < 64)
5962 		align = 64;
5963 	    }
5964 	  else if (align < 128)
5965 	    align = 128;
5966 	}
5967       else if (TARGET_E500_DOUBLE
5968 	       && TREE_CODE (type) == REAL_TYPE
5969 	       && TYPE_MODE (type) == DFmode)
5970 	{
5971 	  if (align < 64)
5972 	    align = 64;
5973 	}
5974     }
5975 
5976   if (how != align_abi)
5977     {
5978       if (TREE_CODE (type) == ARRAY_TYPE
5979 	  && TYPE_MODE (TREE_TYPE (type)) == QImode)
5980 	{
5981 	  if (align < BITS_PER_WORD)
5982 	    align = BITS_PER_WORD;
5983 	}
5984     }
5985 
5986   return align;
5987 }
5988 
5989 /* Previous GCC releases forced all vector types to have 16-byte alignment.  */
5990 
5991 bool
rs6000_special_adjust_field_align_p(tree field,unsigned int computed)5992 rs6000_special_adjust_field_align_p (tree field, unsigned int computed)
5993 {
5994   if (TARGET_ALTIVEC && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
5995     {
5996       if (computed != 128)
5997 	{
5998 	  static bool warned;
5999 	  if (!warned && warn_psabi)
6000 	    {
6001 	      warned = true;
6002 	      inform (input_location,
6003 		      "the layout of aggregates containing vectors with"
6004 		      " %d-byte alignment will change in a future GCC release",
6005 		      computed / BITS_PER_UNIT);
6006 	    }
6007 	}
6008       /* GCC 4.8/4.9 Note: To avoid any ABI change on a release branch, we
6009 	 keep the special treatment of vector types, but warn if there will
6010 	 be differences in future GCC releases.  */
6011       return true;
6012     }
6013 
6014   return false;
6015 }
6016 
6017 /* AIX increases natural record alignment to doubleword if the first
6018    field is an FP double while the FP fields remain word aligned.  */
6019 
6020 unsigned int
rs6000_special_round_type_align(tree type,unsigned int computed,unsigned int specified)6021 rs6000_special_round_type_align (tree type, unsigned int computed,
6022 				 unsigned int specified)
6023 {
6024   unsigned int align = MAX (computed, specified);
6025   tree field = TYPE_FIELDS (type);
6026 
6027   /* Skip all non field decls */
6028   while (field != NULL && TREE_CODE (field) != FIELD_DECL)
6029     field = DECL_CHAIN (field);
6030 
6031   if (field != NULL && field != type)
6032     {
6033       type = TREE_TYPE (field);
6034       while (TREE_CODE (type) == ARRAY_TYPE)
6035 	type = TREE_TYPE (type);
6036 
6037       if (type != error_mark_node && TYPE_MODE (type) == DFmode)
6038 	align = MAX (align, 64);
6039     }
6040 
6041   return align;
6042 }
6043 
6044 /* Darwin increases record alignment to the natural alignment of
6045    the first field.  */
6046 
6047 unsigned int
darwin_rs6000_special_round_type_align(tree type,unsigned int computed,unsigned int specified)6048 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
6049 					unsigned int specified)
6050 {
6051   unsigned int align = MAX (computed, specified);
6052 
6053   if (TYPE_PACKED (type))
6054     return align;
6055 
6056   /* Find the first field, looking down into aggregates.  */
6057   do {
6058     tree field = TYPE_FIELDS (type);
6059     /* Skip all non field decls */
6060     while (field != NULL && TREE_CODE (field) != FIELD_DECL)
6061       field = DECL_CHAIN (field);
6062     if (! field)
6063       break;
6064     /* A packed field does not contribute any extra alignment.  */
6065     if (DECL_PACKED (field))
6066       return align;
6067     type = TREE_TYPE (field);
6068     while (TREE_CODE (type) == ARRAY_TYPE)
6069       type = TREE_TYPE (type);
6070   } while (AGGREGATE_TYPE_P (type));
6071 
6072   if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
6073     align = MAX (align, TYPE_ALIGN (type));
6074 
6075   return align;
6076 }
6077 
6078 /* Return 1 for an operand in small memory on V.4/eabi.  */
6079 
6080 int
small_data_operand(rtx op ATTRIBUTE_UNUSED,enum machine_mode mode ATTRIBUTE_UNUSED)6081 small_data_operand (rtx op ATTRIBUTE_UNUSED,
6082 		    enum machine_mode mode ATTRIBUTE_UNUSED)
6083 {
6084 #if TARGET_ELF
6085   rtx sym_ref;
6086 
6087   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
6088     return 0;
6089 
6090   if (DEFAULT_ABI != ABI_V4)
6091     return 0;
6092 
6093   /* Vector and float memory instructions have a limited offset on the
6094      SPE, so using a vector or float variable directly as an operand is
6095      not useful.  */
6096   if (TARGET_SPE
6097       && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
6098     return 0;
6099 
6100   if (GET_CODE (op) == SYMBOL_REF)
6101     sym_ref = op;
6102 
6103   else if (GET_CODE (op) != CONST
6104 	   || GET_CODE (XEXP (op, 0)) != PLUS
6105 	   || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
6106 	   || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
6107     return 0;
6108 
6109   else
6110     {
6111       rtx sum = XEXP (op, 0);
6112       HOST_WIDE_INT summand;
6113 
6114       /* We have to be careful here, because it is the referenced address
6115 	 that must be 32k from _SDA_BASE_, not just the symbol.  */
6116       summand = INTVAL (XEXP (sum, 1));
6117       if (summand < 0 || summand > g_switch_value)
6118 	return 0;
6119 
6120       sym_ref = XEXP (sum, 0);
6121     }
6122 
6123   return SYMBOL_REF_SMALL_P (sym_ref);
6124 #else
6125   return 0;
6126 #endif
6127 }
6128 
6129 /* Return true if either operand is a general purpose register.  */
6130 
6131 bool
gpr_or_gpr_p(rtx op0,rtx op1)6132 gpr_or_gpr_p (rtx op0, rtx op1)
6133 {
6134   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
6135 	  || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
6136 }
6137 
6138 /* Return true if this is a move direct operation between GPR registers and
6139    floating point/VSX registers.  */
6140 
6141 bool
direct_move_p(rtx op0,rtx op1)6142 direct_move_p (rtx op0, rtx op1)
6143 {
6144   int regno0, regno1;
6145 
6146   if (!REG_P (op0) || !REG_P (op1))
6147     return false;
6148 
6149   if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
6150     return false;
6151 
6152   regno0 = REGNO (op0);
6153   regno1 = REGNO (op1);
6154   if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
6155     return false;
6156 
6157   if (INT_REGNO_P (regno0))
6158     return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
6159 
6160   else if (INT_REGNO_P (regno1))
6161     {
6162       if (TARGET_MFPGPR && FP_REGNO_P (regno0))
6163 	return true;
6164 
6165       else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
6166 	return true;
6167     }
6168 
6169   return false;
6170 }
6171 
6172 /* Return true if this is a load or store quad operation.  This function does
6173    not handle the atomic quad memory instructions.  */
6174 
6175 bool
quad_load_store_p(rtx op0,rtx op1)6176 quad_load_store_p (rtx op0, rtx op1)
6177 {
6178   bool ret;
6179 
6180   if (!TARGET_QUAD_MEMORY)
6181     ret = false;
6182 
6183   else if (REG_P (op0) && MEM_P (op1))
6184     ret = (quad_int_reg_operand (op0, GET_MODE (op0))
6185 	   && quad_memory_operand (op1, GET_MODE (op1))
6186 	   && !reg_overlap_mentioned_p (op0, op1));
6187 
6188   else if (MEM_P (op0) && REG_P (op1))
6189     ret = (quad_memory_operand (op0, GET_MODE (op0))
6190 	   && quad_int_reg_operand (op1, GET_MODE (op1)));
6191 
6192   else
6193     ret = false;
6194 
6195   if (TARGET_DEBUG_ADDR)
6196     {
6197       fprintf (stderr, "\n========== quad_load_store, return %s\n",
6198 	       ret ? "true" : "false");
6199       debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
6200     }
6201 
6202   return ret;
6203 }
6204 
6205 /* Given an address, return a constant offset term if one exists.  */
6206 
6207 static rtx
address_offset(rtx op)6208 address_offset (rtx op)
6209 {
6210   if (GET_CODE (op) == PRE_INC
6211       || GET_CODE (op) == PRE_DEC)
6212     op = XEXP (op, 0);
6213   else if (GET_CODE (op) == PRE_MODIFY
6214 	   || GET_CODE (op) == LO_SUM)
6215     op = XEXP (op, 1);
6216 
6217   if (GET_CODE (op) == CONST)
6218     op = XEXP (op, 0);
6219 
6220   if (GET_CODE (op) == PLUS)
6221     op = XEXP (op, 1);
6222 
6223   if (CONST_INT_P (op))
6224     return op;
6225 
6226   return NULL_RTX;
6227 }
6228 
6229 /* Return true if the MEM operand is a memory operand suitable for use
6230    with a (full width, possibly multiple) gpr load/store.  On
6231    powerpc64 this means the offset must be divisible by 4.
6232    Implements 'Y' constraint.
6233 
6234    Accept direct, indexed, offset, lo_sum and tocref.  Since this is
6235    a constraint function we know the operand has satisfied a suitable
6236    memory predicate.  Also accept some odd rtl generated by reload
6237    (see rs6000_legitimize_reload_address for various forms).  It is
6238    important that reload rtl be accepted by appropriate constraints
6239    but not by the operand predicate.
6240 
6241    Offsetting a lo_sum should not be allowed, except where we know by
6242    alignment that a 32k boundary is not crossed, but see the ???
6243    comment in rs6000_legitimize_reload_address.  Note that by
6244    "offsetting" here we mean a further offset to access parts of the
6245    MEM.  It's fine to have a lo_sum where the inner address is offset
6246    from a sym, since the same sym+offset will appear in the high part
6247    of the address calculation.  */
6248 
6249 bool
mem_operand_gpr(rtx op,enum machine_mode mode)6250 mem_operand_gpr (rtx op, enum machine_mode mode)
6251 {
6252   unsigned HOST_WIDE_INT offset;
6253   int extra;
6254   rtx addr = XEXP (op, 0);
6255 
6256   op = address_offset (addr);
6257   if (op == NULL_RTX)
6258     return true;
6259 
6260   offset = INTVAL (op);
6261   if (TARGET_POWERPC64 && (offset & 3) != 0)
6262     return false;
6263 
6264   extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
6265   if (extra < 0)
6266     extra = 0;
6267 
6268   if (GET_CODE (addr) == LO_SUM)
6269     /* For lo_sum addresses, we must allow any offset except one that
6270        causes a wrap, so test only the low 16 bits.  */
6271     offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
6272 
6273   return offset + 0x8000 < 0x10000u - extra;
6274 }
6275 
6276 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p.  */
6277 
6278 static bool
reg_offset_addressing_ok_p(enum machine_mode mode)6279 reg_offset_addressing_ok_p (enum machine_mode mode)
6280 {
6281   switch (mode)
6282     {
6283     case V16QImode:
6284     case V8HImode:
6285     case V4SFmode:
6286     case V4SImode:
6287     case V2DFmode:
6288     case V2DImode:
6289     case V1TImode:
6290     case TImode:
6291       /* AltiVec/VSX vector modes.  Only reg+reg addressing is valid.  While
6292 	 TImode is not a vector mode, if we want to use the VSX registers to
6293 	 move it around, we need to restrict ourselves to reg+reg
6294 	 addressing.  */
6295       if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
6296 	return false;
6297       break;
6298 
6299     case V4HImode:
6300     case V2SImode:
6301     case V1DImode:
6302     case V2SFmode:
6303        /* Paired vector modes.  Only reg+reg addressing is valid.  */
6304       if (TARGET_PAIRED_FLOAT)
6305         return false;
6306       break;
6307 
6308     case SDmode:
6309       /* If we can do direct load/stores of SDmode, restrict it to reg+reg
6310 	 addressing for the LFIWZX and STFIWX instructions.  */
6311       if (TARGET_NO_SDMODE_STACK)
6312 	return false;
6313       break;
6314 
6315     default:
6316       break;
6317     }
6318 
6319   return true;
6320 }
6321 
6322 static bool
virtual_stack_registers_memory_p(rtx op)6323 virtual_stack_registers_memory_p (rtx op)
6324 {
6325   int regnum;
6326 
6327   if (GET_CODE (op) == REG)
6328     regnum = REGNO (op);
6329 
6330   else if (GET_CODE (op) == PLUS
6331 	   && GET_CODE (XEXP (op, 0)) == REG
6332 	   && GET_CODE (XEXP (op, 1)) == CONST_INT)
6333     regnum = REGNO (XEXP (op, 0));
6334 
6335   else
6336     return false;
6337 
6338   return (regnum >= FIRST_VIRTUAL_REGISTER
6339 	  && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
6340 }
6341 
6342 /* Return true if a MODE sized memory accesses to OP plus OFFSET
6343    is known to not straddle a 32k boundary.  */
6344 
6345 static bool
offsettable_ok_by_alignment(rtx op,HOST_WIDE_INT offset,enum machine_mode mode)6346 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
6347 			     enum machine_mode mode)
6348 {
6349   tree decl, type;
6350   unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
6351 
6352   if (GET_CODE (op) != SYMBOL_REF)
6353     return false;
6354 
6355   dsize = GET_MODE_SIZE (mode);
6356   decl = SYMBOL_REF_DECL (op);
6357   if (!decl)
6358     {
6359       if (dsize == 0)
6360 	return false;
6361 
6362       /* -fsection-anchors loses the original SYMBOL_REF_DECL when
6363 	 replacing memory addresses with an anchor plus offset.  We
6364 	 could find the decl by rummaging around in the block->objects
6365 	 VEC for the given offset but that seems like too much work.  */
6366       dalign = BITS_PER_UNIT;
6367       if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
6368 	  && SYMBOL_REF_ANCHOR_P (op)
6369 	  && SYMBOL_REF_BLOCK (op) != NULL)
6370 	{
6371 	  struct object_block *block = SYMBOL_REF_BLOCK (op);
6372 
6373 	  dalign = block->alignment;
6374 	  offset += SYMBOL_REF_BLOCK_OFFSET (op);
6375 	}
6376       else if (CONSTANT_POOL_ADDRESS_P (op))
6377 	{
6378 	  /* It would be nice to have get_pool_align()..  */
6379 	  enum machine_mode cmode = get_pool_mode (op);
6380 
6381 	  dalign = GET_MODE_ALIGNMENT (cmode);
6382 	}
6383     }
6384   else if (DECL_P (decl))
6385     {
6386       dalign = DECL_ALIGN (decl);
6387 
6388       if (dsize == 0)
6389 	{
6390 	  /* Allow BLKmode when the entire object is known to not
6391 	     cross a 32k boundary.  */
6392 	  if (!DECL_SIZE_UNIT (decl))
6393 	    return false;
6394 
6395 	  if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
6396 	    return false;
6397 
6398 	  dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6399 	  if (dsize > 32768)
6400 	    return false;
6401 
6402 	  return dalign / BITS_PER_UNIT >= dsize;
6403 	}
6404     }
6405   else
6406     {
6407       type = TREE_TYPE (decl);
6408 
6409       dalign = TYPE_ALIGN (type);
6410       if (CONSTANT_CLASS_P (decl))
6411 	dalign = CONSTANT_ALIGNMENT (decl, dalign);
6412       else
6413 	dalign = DATA_ALIGNMENT (decl, dalign);
6414 
6415       if (dsize == 0)
6416 	{
6417 	  /* BLKmode, check the entire object.  */
6418 	  if (TREE_CODE (decl) == STRING_CST)
6419 	    dsize = TREE_STRING_LENGTH (decl);
6420 	  else if (TYPE_SIZE_UNIT (type)
6421 		   && host_integerp (TYPE_SIZE_UNIT (type), 1))
6422 	    dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
6423 	  else
6424 	    return false;
6425 	  if (dsize > 32768)
6426 	    return false;
6427 
6428 	  return dalign / BITS_PER_UNIT >= dsize;
6429 	}
6430     }
6431 
6432   /* Find how many bits of the alignment we know for this access.  */
6433   mask = dalign / BITS_PER_UNIT - 1;
6434   lsb = offset & -offset;
6435   mask &= lsb - 1;
6436   dalign = mask + 1;
6437 
6438   return dalign >= dsize;
6439 }
6440 
6441 static bool
constant_pool_expr_p(rtx op)6442 constant_pool_expr_p (rtx op)
6443 {
6444   rtx base, offset;
6445 
6446   split_const (op, &base, &offset);
6447   return (GET_CODE (base) == SYMBOL_REF
6448 	  && CONSTANT_POOL_ADDRESS_P (base)
6449 	  && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
6450 }
6451 
6452 static const_rtx tocrel_base, tocrel_offset;
6453 
6454 /* Return true if OP is a toc pointer relative address (the output
6455    of create_TOC_reference).  If STRICT, do not match high part or
6456    non-split -mcmodel=large/medium toc pointer relative addresses.  */
6457 
6458 bool
toc_relative_expr_p(const_rtx op,bool strict)6459 toc_relative_expr_p (const_rtx op, bool strict)
6460 {
6461   if (!TARGET_TOC)
6462     return false;
6463 
6464   if (TARGET_CMODEL != CMODEL_SMALL)
6465     {
6466       /* Only match the low part.  */
6467       if (GET_CODE (op) == LO_SUM
6468 	  && REG_P (XEXP (op, 0))
6469 	  && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
6470 	op = XEXP (op, 1);
6471       else if (strict)
6472 	return false;
6473     }
6474 
6475   tocrel_base = op;
6476   tocrel_offset = const0_rtx;
6477   if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
6478     {
6479       tocrel_base = XEXP (op, 0);
6480       tocrel_offset = XEXP (op, 1);
6481     }
6482 
6483   return (GET_CODE (tocrel_base) == UNSPEC
6484 	  && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
6485 }
6486 
6487 /* Return true if X is a constant pool address, and also for cmodel=medium
6488    if X is a toc-relative address known to be offsettable within MODE.  */
6489 
6490 bool
legitimate_constant_pool_address_p(const_rtx x,enum machine_mode mode,bool strict)6491 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
6492 				    bool strict)
6493 {
6494   return (toc_relative_expr_p (x, strict)
6495 	  && (TARGET_CMODEL != CMODEL_MEDIUM
6496 	      || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
6497 	      || mode == QImode
6498 	      || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
6499 					      INTVAL (tocrel_offset), mode)));
6500 }
6501 
6502 static bool
legitimate_small_data_p(enum machine_mode mode,rtx x)6503 legitimate_small_data_p (enum machine_mode mode, rtx x)
6504 {
6505   return (DEFAULT_ABI == ABI_V4
6506 	  && !flag_pic && !TARGET_TOC
6507 	  && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
6508 	  && small_data_operand (x, mode));
6509 }
6510 
6511 /* SPE offset addressing is limited to 5-bits worth of double words.  */
6512 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
6513 
6514 bool
rs6000_legitimate_offset_address_p(enum machine_mode mode,rtx x,bool strict,bool worst_case)6515 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
6516 				    bool strict, bool worst_case)
6517 {
6518   unsigned HOST_WIDE_INT offset;
6519   unsigned int extra;
6520 
6521   if (GET_CODE (x) != PLUS)
6522     return false;
6523   if (!REG_P (XEXP (x, 0)))
6524     return false;
6525   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6526     return false;
6527   if (!reg_offset_addressing_ok_p (mode))
6528     return virtual_stack_registers_memory_p (x);
6529   if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
6530     return true;
6531   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6532     return false;
6533 
6534   offset = INTVAL (XEXP (x, 1));
6535   extra = 0;
6536   switch (mode)
6537     {
6538     case V4HImode:
6539     case V2SImode:
6540     case V1DImode:
6541     case V2SFmode:
6542       /* SPE vector modes.  */
6543       return SPE_CONST_OFFSET_OK (offset);
6544 
6545     case DFmode:
6546     case DDmode:
6547     case DImode:
6548       /* On e500v2, we may have:
6549 
6550 	   (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6551 
6552          Which gets addressed with evldd instructions.  */
6553       if (TARGET_E500_DOUBLE)
6554 	return SPE_CONST_OFFSET_OK (offset);
6555 
6556       /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6557 	 addressing.  */
6558       if (VECTOR_MEM_VSX_P (mode))
6559 	return false;
6560 
6561       if (!worst_case)
6562 	break;
6563       if (!TARGET_POWERPC64)
6564 	extra = 4;
6565       else if (offset & 3)
6566 	return false;
6567       break;
6568 
6569     case TFmode:
6570       if (TARGET_E500_DOUBLE)
6571 	return (SPE_CONST_OFFSET_OK (offset)
6572 		&& SPE_CONST_OFFSET_OK (offset + 8));
6573       /* fall through */
6574 
6575     case TDmode:
6576     case TImode:
6577     case PTImode:
6578       extra = 8;
6579       if (!worst_case)
6580 	break;
6581       if (!TARGET_POWERPC64)
6582 	extra = 12;
6583       else if (offset & 3)
6584 	return false;
6585       break;
6586 
6587     default:
6588       break;
6589     }
6590 
6591   offset += 0x8000;
6592   return offset < 0x10000 - extra;
6593 }
6594 
6595 bool
legitimate_indexed_address_p(rtx x,int strict)6596 legitimate_indexed_address_p (rtx x, int strict)
6597 {
6598   rtx op0, op1;
6599 
6600   if (GET_CODE (x) != PLUS)
6601     return false;
6602 
6603   op0 = XEXP (x, 0);
6604   op1 = XEXP (x, 1);
6605 
6606   /* Recognize the rtl generated by reload which we know will later be
6607      replaced with proper base and index regs.  */
6608   if (!strict
6609       && reload_in_progress
6610       && (REG_P (op0) || GET_CODE (op0) == PLUS)
6611       && REG_P (op1))
6612     return true;
6613 
6614   return (REG_P (op0) && REG_P (op1)
6615 	  && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6616 	       && INT_REG_OK_FOR_INDEX_P (op1, strict))
6617 	      || (INT_REG_OK_FOR_BASE_P (op1, strict)
6618 		  && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6619 }
6620 
6621 bool
avoiding_indexed_address_p(enum machine_mode mode)6622 avoiding_indexed_address_p (enum machine_mode mode)
6623 {
6624   /* Avoid indexed addressing for modes that have non-indexed
6625      load/store instruction forms.  */
6626   return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6627 }
6628 
6629 bool
legitimate_indirect_address_p(rtx x,int strict)6630 legitimate_indirect_address_p (rtx x, int strict)
6631 {
6632   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6633 }
6634 
6635 bool
macho_lo_sum_memory_operand(rtx x,enum machine_mode mode)6636 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6637 {
6638   if (!TARGET_MACHO || !flag_pic
6639       || mode != SImode || GET_CODE (x) != MEM)
6640     return false;
6641   x = XEXP (x, 0);
6642 
6643   if (GET_CODE (x) != LO_SUM)
6644     return false;
6645   if (GET_CODE (XEXP (x, 0)) != REG)
6646     return false;
6647   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6648     return false;
6649   x = XEXP (x, 1);
6650 
6651   return CONSTANT_P (x);
6652 }
6653 
6654 static bool
legitimate_lo_sum_address_p(enum machine_mode mode,rtx x,int strict)6655 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6656 {
6657   if (GET_CODE (x) != LO_SUM)
6658     return false;
6659   if (GET_CODE (XEXP (x, 0)) != REG)
6660     return false;
6661   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6662     return false;
6663   /* Restrict addressing for DI because of our SUBREG hackery.  */
6664   if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6665     return false;
6666   x = XEXP (x, 1);
6667 
6668   if (TARGET_ELF || TARGET_MACHO)
6669     {
6670       bool large_toc_ok;
6671 
6672       if (DEFAULT_ABI == ABI_V4 && flag_pic)
6673 	return false;
6674       /* LRA don't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
6675 	 push_reload from reload pass code.  LEGITIMIZE_RELOAD_ADDRESS
6676 	 recognizes some LO_SUM addresses as valid although this
6677 	 function says opposite.  In most cases, LRA through different
6678 	 transformations can generate correct code for address reloads.
6679 	 It can not manage only some LO_SUM cases.  So we need to add
6680 	 code analogous to one in rs6000_legitimize_reload_address for
6681 	 LOW_SUM here saying that some addresses are still valid.  */
6682       large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
6683 		      && small_toc_ref (x, VOIDmode));
6684       if (TARGET_TOC && ! large_toc_ok)
6685 	return false;
6686       if (GET_MODE_NUNITS (mode) != 1)
6687 	return false;
6688       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6689 	  && !(/* ??? Assume floating point reg based on mode?  */
6690 	       TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6691 	       && (mode == DFmode || mode == DDmode)))
6692 	return false;
6693 
6694       return CONSTANT_P (x) || large_toc_ok;
6695     }
6696 
6697   return false;
6698 }
6699 
6700 
6701 /* Try machine-dependent ways of modifying an illegitimate address
6702    to be legitimate.  If we find one, return the new, valid address.
6703    This is used from only one place: `memory_address' in explow.c.
6704 
6705    OLDX is the address as it was before break_out_memory_refs was
6706    called.  In some cases it is useful to look at this to decide what
6707    needs to be done.
6708 
6709    It is always safe for this function to do nothing.  It exists to
6710    recognize opportunities to optimize the output.
6711 
6712    On RS/6000, first check for the sum of a register with a constant
6713    integer that is out of range.  If so, generate code to add the
6714    constant with the low-order 16 bits masked to the register and force
6715    this result into another register (this can be done with `cau').
6716    Then generate an address of REG+(CONST&0xffff), allowing for the
6717    possibility of bit 16 being a one.
6718 
6719    Then check for the sum of a register and something not constant, try to
6720    load the other things into a register and return the sum.  */
6721 
6722 static rtx
rs6000_legitimize_address(rtx x,rtx oldx ATTRIBUTE_UNUSED,enum machine_mode mode)6723 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6724 			   enum machine_mode mode)
6725 {
6726   unsigned int extra;
6727 
6728   if (!reg_offset_addressing_ok_p (mode))
6729     {
6730       if (virtual_stack_registers_memory_p (x))
6731 	return x;
6732 
6733       /* In theory we should not be seeing addresses of the form reg+0,
6734 	 but just in case it is generated, optimize it away.  */
6735       if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6736 	return force_reg (Pmode, XEXP (x, 0));
6737 
6738       /* For TImode with load/store quad, restrict addresses to just a single
6739 	 pointer, so it works with both GPRs and VSX registers.  */
6740       /* Make sure both operands are registers.  */
6741       else if (GET_CODE (x) == PLUS
6742 	       && (mode != TImode || !TARGET_QUAD_MEMORY))
6743 	return gen_rtx_PLUS (Pmode,
6744 			     force_reg (Pmode, XEXP (x, 0)),
6745 			     force_reg (Pmode, XEXP (x, 1)));
6746       else
6747 	return force_reg (Pmode, x);
6748     }
6749   if (GET_CODE (x) == SYMBOL_REF)
6750     {
6751       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6752       if (model != 0)
6753 	return rs6000_legitimize_tls_address (x, model);
6754     }
6755 
6756   extra = 0;
6757   switch (mode)
6758     {
6759     case TFmode:
6760     case TDmode:
6761     case TImode:
6762     case PTImode:
6763       /* As in legitimate_offset_address_p we do not assume
6764 	 worst-case.  The mode here is just a hint as to the registers
6765 	 used.  A TImode is usually in gprs, but may actually be in
6766 	 fprs.  Leave worst-case scenario for reload to handle via
6767 	 insn constraints.  PTImode is only GPRs.  */
6768       extra = 8;
6769       break;
6770     default:
6771       break;
6772     }
6773 
6774   if (GET_CODE (x) == PLUS
6775       && GET_CODE (XEXP (x, 0)) == REG
6776       && GET_CODE (XEXP (x, 1)) == CONST_INT
6777       && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6778 	  >= 0x10000 - extra)
6779       && !(SPE_VECTOR_MODE (mode)
6780 	   || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6781     {
6782       HOST_WIDE_INT high_int, low_int;
6783       rtx sum;
6784       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6785       if (low_int >= 0x8000 - extra)
6786 	low_int = 0;
6787       high_int = INTVAL (XEXP (x, 1)) - low_int;
6788       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6789 					 GEN_INT (high_int)), 0);
6790       return plus_constant (Pmode, sum, low_int);
6791     }
6792   else if (GET_CODE (x) == PLUS
6793 	   && GET_CODE (XEXP (x, 0)) == REG
6794 	   && GET_CODE (XEXP (x, 1)) != CONST_INT
6795 	   && GET_MODE_NUNITS (mode) == 1
6796 	   && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6797 	       || (/* ??? Assume floating point reg based on mode?  */
6798 		   (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6799 		   && (mode == DFmode || mode == DDmode)))
6800 	   && !avoiding_indexed_address_p (mode))
6801     {
6802       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6803 			   force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6804     }
6805   else if (SPE_VECTOR_MODE (mode)
6806 	   || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6807     {
6808       if (mode == DImode)
6809 	return x;
6810       /* We accept [reg + reg] and [reg + OFFSET].  */
6811 
6812       if (GET_CODE (x) == PLUS)
6813        {
6814          rtx op1 = XEXP (x, 0);
6815          rtx op2 = XEXP (x, 1);
6816          rtx y;
6817 
6818          op1 = force_reg (Pmode, op1);
6819 
6820          if (GET_CODE (op2) != REG
6821              && (GET_CODE (op2) != CONST_INT
6822                  || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6823                  || (GET_MODE_SIZE (mode) > 8
6824                      && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6825            op2 = force_reg (Pmode, op2);
6826 
6827          /* We can't always do [reg + reg] for these, because [reg +
6828             reg + offset] is not a legitimate addressing mode.  */
6829          y = gen_rtx_PLUS (Pmode, op1, op2);
6830 
6831          if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6832            return force_reg (Pmode, y);
6833          else
6834            return y;
6835        }
6836 
6837       return force_reg (Pmode, x);
6838     }
6839   else if ((TARGET_ELF
6840 #if TARGET_MACHO
6841 	    || !MACHO_DYNAMIC_NO_PIC_P
6842 #endif
6843 	    )
6844 	   && TARGET_32BIT
6845 	   && TARGET_NO_TOC
6846 	   && ! flag_pic
6847 	   && GET_CODE (x) != CONST_INT
6848 	   && GET_CODE (x) != CONST_DOUBLE
6849 	   && CONSTANT_P (x)
6850 	   && GET_MODE_NUNITS (mode) == 1
6851 	   && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6852 	       || (/* ??? Assume floating point reg based on mode?  */
6853 		   (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6854 		   && (mode == DFmode || mode == DDmode))))
6855     {
6856       rtx reg = gen_reg_rtx (Pmode);
6857       if (TARGET_ELF)
6858 	emit_insn (gen_elf_high (reg, x));
6859       else
6860 	emit_insn (gen_macho_high (reg, x));
6861       return gen_rtx_LO_SUM (Pmode, reg, x);
6862     }
6863   else if (TARGET_TOC
6864 	   && GET_CODE (x) == SYMBOL_REF
6865 	   && constant_pool_expr_p (x)
6866 	   && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6867     return create_TOC_reference (x, NULL_RTX);
6868   else
6869     return x;
6870 }
6871 
6872 /* Debug version of rs6000_legitimize_address.  */
6873 static rtx
rs6000_debug_legitimize_address(rtx x,rtx oldx,enum machine_mode mode)6874 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6875 {
6876   rtx ret;
6877   rtx insns;
6878 
6879   start_sequence ();
6880   ret = rs6000_legitimize_address (x, oldx, mode);
6881   insns = get_insns ();
6882   end_sequence ();
6883 
6884   if (ret != x)
6885     {
6886       fprintf (stderr,
6887 	       "\nrs6000_legitimize_address: mode %s, old code %s, "
6888 	       "new code %s, modified\n",
6889 	       GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6890 	       GET_RTX_NAME (GET_CODE (ret)));
6891 
6892       fprintf (stderr, "Original address:\n");
6893       debug_rtx (x);
6894 
6895       fprintf (stderr, "oldx:\n");
6896       debug_rtx (oldx);
6897 
6898       fprintf (stderr, "New address:\n");
6899       debug_rtx (ret);
6900 
6901       if (insns)
6902 	{
6903 	  fprintf (stderr, "Insns added:\n");
6904 	  debug_rtx_list (insns, 20);
6905 	}
6906     }
6907   else
6908     {
6909       fprintf (stderr,
6910 	       "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6911 	       GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6912 
6913       debug_rtx (x);
6914     }
6915 
6916   if (insns)
6917     emit_insn (insns);
6918 
6919   return ret;
6920 }
6921 
6922 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6923    We need to emit DTP-relative relocations.  */
6924 
6925 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6926 static void
rs6000_output_dwarf_dtprel(FILE * file,int size,rtx x)6927 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6928 {
6929   switch (size)
6930     {
6931     case 4:
6932       fputs ("\t.long\t", file);
6933       break;
6934     case 8:
6935       fputs (DOUBLE_INT_ASM_OP, file);
6936       break;
6937     default:
6938       gcc_unreachable ();
6939     }
6940   output_addr_const (file, x);
6941   fputs ("@dtprel+0x8000", file);
6942 }
6943 
6944 /* In the name of slightly smaller debug output, and to cater to
6945    general assembler lossage, recognize various UNSPEC sequences
6946    and turn them back into a direct symbol reference.  */
6947 
6948 static rtx
rs6000_delegitimize_address(rtx orig_x)6949 rs6000_delegitimize_address (rtx orig_x)
6950 {
6951   rtx x, y, offset;
6952 
6953   orig_x = delegitimize_mem_from_attrs (orig_x);
6954   x = orig_x;
6955   if (MEM_P (x))
6956     x = XEXP (x, 0);
6957 
6958   y = x;
6959   if (TARGET_CMODEL != CMODEL_SMALL
6960       && GET_CODE (y) == LO_SUM)
6961     y = XEXP (y, 1);
6962 
6963   offset = NULL_RTX;
6964   if (GET_CODE (y) == PLUS
6965       && GET_MODE (y) == Pmode
6966       && CONST_INT_P (XEXP (y, 1)))
6967     {
6968       offset = XEXP (y, 1);
6969       y = XEXP (y, 0);
6970     }
6971 
6972   if (GET_CODE (y) == UNSPEC
6973       && XINT (y, 1) == UNSPEC_TOCREL)
6974     {
6975       y = XVECEXP (y, 0, 0);
6976 
6977 #ifdef HAVE_AS_TLS
6978       /* Do not associate thread-local symbols with the original
6979 	 constant pool symbol.  */
6980       if (TARGET_XCOFF
6981 	  && GET_CODE (y) == SYMBOL_REF
6982 	  && CONSTANT_POOL_ADDRESS_P (y)
6983 	  && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6984 	return orig_x;
6985 #endif
6986 
6987       if (offset != NULL_RTX)
6988 	y = gen_rtx_PLUS (Pmode, y, offset);
6989       if (!MEM_P (orig_x))
6990 	return y;
6991       else
6992 	return replace_equiv_address_nv (orig_x, y);
6993     }
6994 
6995   if (TARGET_MACHO
6996       && GET_CODE (orig_x) == LO_SUM
6997       && GET_CODE (XEXP (orig_x, 1)) == CONST)
6998     {
6999       y = XEXP (XEXP (orig_x, 1), 0);
7000       if (GET_CODE (y) == UNSPEC
7001 	  && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
7002 	return XVECEXP (y, 0, 0);
7003     }
7004 
7005   return orig_x;
7006 }
7007 
7008 /* Return true if X shouldn't be emitted into the debug info.
7009    The linker doesn't like .toc section references from
7010    .debug_* sections, so reject .toc section symbols.  */
7011 
7012 static bool
rs6000_const_not_ok_for_debug_p(rtx x)7013 rs6000_const_not_ok_for_debug_p (rtx x)
7014 {
7015   if (GET_CODE (x) == SYMBOL_REF
7016       && CONSTANT_POOL_ADDRESS_P (x))
7017     {
7018       rtx c = get_pool_constant (x);
7019       enum machine_mode cmode = get_pool_mode (x);
7020       if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
7021 	return true;
7022     }
7023 
7024   return false;
7025 }
7026 
7027 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
7028 
7029 static GTY(()) rtx rs6000_tls_symbol;
7030 static rtx
rs6000_tls_get_addr(void)7031 rs6000_tls_get_addr (void)
7032 {
7033   if (!rs6000_tls_symbol)
7034     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
7035 
7036   return rs6000_tls_symbol;
7037 }
7038 
7039 /* Construct the SYMBOL_REF for TLS GOT references.  */
7040 
7041 static GTY(()) rtx rs6000_got_symbol;
7042 static rtx
rs6000_got_sym(void)7043 rs6000_got_sym (void)
7044 {
7045   if (!rs6000_got_symbol)
7046     {
7047       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
7048       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
7049       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
7050     }
7051 
7052   return rs6000_got_symbol;
7053 }
7054 
7055 /* AIX Thread-Local Address support.  */
7056 
7057 static rtx
rs6000_legitimize_tls_address_aix(rtx addr,enum tls_model model)7058 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
7059 {
7060   rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
7061   const char *name;
7062   char *tlsname;
7063 
7064   name = XSTR (addr, 0);
7065   /* Append TLS CSECT qualifier, unless the symbol already is qualified
7066      or the symbol will be in TLS private data section.  */
7067   if (name[strlen (name) - 1] != ']'
7068       && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
7069 	  || bss_initializer_p (SYMBOL_REF_DECL (addr))))
7070     {
7071       tlsname = XALLOCAVEC (char, strlen (name) + 4);
7072       strcpy (tlsname, name);
7073       strcat (tlsname,
7074 	      bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
7075       tlsaddr = copy_rtx (addr);
7076       XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
7077     }
7078   else
7079     tlsaddr = addr;
7080 
7081   /* Place addr into TOC constant pool.  */
7082   sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
7083 
7084   /* Output the TOC entry and create the MEM referencing the value.  */
7085   if (constant_pool_expr_p (XEXP (sym, 0))
7086       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
7087     {
7088       tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
7089       mem = gen_const_mem (Pmode, tocref);
7090       set_mem_alias_set (mem, get_TOC_alias_set ());
7091     }
7092   else
7093     return sym;
7094 
7095   /* Use global-dynamic for local-dynamic.  */
7096   if (model == TLS_MODEL_GLOBAL_DYNAMIC
7097       || model == TLS_MODEL_LOCAL_DYNAMIC)
7098     {
7099       /* Create new TOC reference for @m symbol.  */
7100       name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
7101       tlsname = XALLOCAVEC (char, strlen (name) + 1);
7102       strcpy (tlsname, "*LCM");
7103       strcat (tlsname, name + 3);
7104       rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
7105       SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
7106       tocref = create_TOC_reference (modaddr, NULL_RTX);
7107       rtx modmem = gen_const_mem (Pmode, tocref);
7108       set_mem_alias_set (modmem, get_TOC_alias_set ());
7109 
7110       rtx modreg = gen_reg_rtx (Pmode);
7111       emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
7112 
7113       tmpreg = gen_reg_rtx (Pmode);
7114       emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
7115 
7116       dest = gen_reg_rtx (Pmode);
7117       if (TARGET_32BIT)
7118 	emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
7119       else
7120 	emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
7121       return dest;
7122     }
7123   /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13.  */
7124   else if (TARGET_32BIT)
7125     {
7126       tlsreg = gen_reg_rtx (SImode);
7127       emit_insn (gen_tls_get_tpointer (tlsreg));
7128     }
7129   else
7130     tlsreg = gen_rtx_REG (DImode, 13);
7131 
7132   /* Load the TOC value into temporary register.  */
7133   tmpreg = gen_reg_rtx (Pmode);
7134   emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
7135   set_unique_reg_note (get_last_insn (), REG_EQUAL,
7136 		       gen_rtx_MINUS (Pmode, addr, tlsreg));
7137 
7138   /* Add TOC symbol value to TLS pointer.  */
7139   dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
7140 
7141   return dest;
7142 }
7143 
7144 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
7145    this (thread-local) address.  */
7146 
7147 static rtx
rs6000_legitimize_tls_address(rtx addr,enum tls_model model)7148 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
7149 {
7150   rtx dest, insn;
7151 
7152   if (TARGET_XCOFF)
7153     return rs6000_legitimize_tls_address_aix (addr, model);
7154 
7155   dest = gen_reg_rtx (Pmode);
7156   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
7157     {
7158       rtx tlsreg;
7159 
7160       if (TARGET_64BIT)
7161 	{
7162 	  tlsreg = gen_rtx_REG (Pmode, 13);
7163 	  insn = gen_tls_tprel_64 (dest, tlsreg, addr);
7164 	}
7165       else
7166 	{
7167 	  tlsreg = gen_rtx_REG (Pmode, 2);
7168 	  insn = gen_tls_tprel_32 (dest, tlsreg, addr);
7169 	}
7170       emit_insn (insn);
7171     }
7172   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
7173     {
7174       rtx tlsreg, tmp;
7175 
7176       tmp = gen_reg_rtx (Pmode);
7177       if (TARGET_64BIT)
7178 	{
7179 	  tlsreg = gen_rtx_REG (Pmode, 13);
7180 	  insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
7181 	}
7182       else
7183 	{
7184 	  tlsreg = gen_rtx_REG (Pmode, 2);
7185 	  insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
7186 	}
7187       emit_insn (insn);
7188       if (TARGET_64BIT)
7189 	insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
7190       else
7191 	insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
7192       emit_insn (insn);
7193     }
7194   else
7195     {
7196       rtx r3, got, tga, tmp1, tmp2, call_insn;
7197 
7198       /* We currently use relocations like @got@tlsgd for tls, which
7199 	 means the linker will handle allocation of tls entries, placing
7200 	 them in the .got section.  So use a pointer to the .got section,
7201 	 not one to secondary TOC sections used by 64-bit -mminimal-toc,
7202 	 or to secondary GOT sections used by 32-bit -fPIC.  */
7203       if (TARGET_64BIT)
7204 	got = gen_rtx_REG (Pmode, 2);
7205       else
7206 	{
7207 	  if (flag_pic == 1)
7208 	    got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
7209 	  else
7210 	    {
7211 	      rtx gsym = rs6000_got_sym ();
7212 	      got = gen_reg_rtx (Pmode);
7213 	      if (flag_pic == 0)
7214 		rs6000_emit_move (got, gsym, Pmode);
7215 	      else
7216 		{
7217 		  rtx mem, lab, last;
7218 
7219 		  tmp1 = gen_reg_rtx (Pmode);
7220 		  tmp2 = gen_reg_rtx (Pmode);
7221 		  mem = gen_const_mem (Pmode, tmp1);
7222 		  lab = gen_label_rtx ();
7223 		  emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
7224 		  emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
7225 		  if (TARGET_LINK_STACK)
7226 		    emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
7227 		  emit_move_insn (tmp2, mem);
7228 		  last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
7229 		  set_unique_reg_note (last, REG_EQUAL, gsym);
7230 		}
7231 	    }
7232 	}
7233 
7234       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
7235 	{
7236 	  tga = rs6000_tls_get_addr ();
7237 	  emit_library_call_value (tga, dest, LCT_CONST, Pmode,
7238 				   1, const0_rtx, Pmode);
7239 
7240 	  r3 = gen_rtx_REG (Pmode, 3);
7241 	  if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7242 	    {
7243 	      if (TARGET_64BIT)
7244 		insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
7245 	      else
7246 		insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
7247 	    }
7248 	  else if (DEFAULT_ABI == ABI_V4)
7249 	    insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
7250 	  else
7251 	    gcc_unreachable ();
7252 	  call_insn = last_call_insn ();
7253 	  PATTERN (call_insn) = insn;
7254 	  if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7255 	    use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7256 		     pic_offset_table_rtx);
7257 	}
7258       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
7259 	{
7260 	  tga = rs6000_tls_get_addr ();
7261 	  tmp1 = gen_reg_rtx (Pmode);
7262 	  emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
7263 				   1, const0_rtx, Pmode);
7264 
7265 	  r3 = gen_rtx_REG (Pmode, 3);
7266 	  if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7267 	    {
7268 	      if (TARGET_64BIT)
7269 		insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
7270 	      else
7271 		insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
7272 	    }
7273 	  else if (DEFAULT_ABI == ABI_V4)
7274 	    insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
7275 	  else
7276 	    gcc_unreachable ();
7277 	  call_insn = last_call_insn ();
7278 	  PATTERN (call_insn) = insn;
7279 	  if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7280 	    use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7281 		     pic_offset_table_rtx);
7282 
7283 	  if (rs6000_tls_size == 16)
7284 	    {
7285 	      if (TARGET_64BIT)
7286 		insn = gen_tls_dtprel_64 (dest, tmp1, addr);
7287 	      else
7288 		insn = gen_tls_dtprel_32 (dest, tmp1, addr);
7289 	    }
7290 	  else if (rs6000_tls_size == 32)
7291 	    {
7292 	      tmp2 = gen_reg_rtx (Pmode);
7293 	      if (TARGET_64BIT)
7294 		insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
7295 	      else
7296 		insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
7297 	      emit_insn (insn);
7298 	      if (TARGET_64BIT)
7299 		insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
7300 	      else
7301 		insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
7302 	    }
7303 	  else
7304 	    {
7305 	      tmp2 = gen_reg_rtx (Pmode);
7306 	      if (TARGET_64BIT)
7307 		insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
7308 	      else
7309 		insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
7310 	      emit_insn (insn);
7311 	      insn = gen_rtx_SET (Pmode, dest,
7312 				  gen_rtx_PLUS (Pmode, tmp2, tmp1));
7313 	    }
7314 	  emit_insn (insn);
7315 	}
7316       else
7317 	{
7318 	  /* IE, or 64-bit offset LE.  */
7319 	  tmp2 = gen_reg_rtx (Pmode);
7320 	  if (TARGET_64BIT)
7321 	    insn = gen_tls_got_tprel_64 (tmp2, got, addr);
7322 	  else
7323 	    insn = gen_tls_got_tprel_32 (tmp2, got, addr);
7324 	  emit_insn (insn);
7325 	  if (TARGET_64BIT)
7326 	    insn = gen_tls_tls_64 (dest, tmp2, addr);
7327 	  else
7328 	    insn = gen_tls_tls_32 (dest, tmp2, addr);
7329 	  emit_insn (insn);
7330 	}
7331     }
7332 
7333   return dest;
7334 }
7335 
7336 /* Return 1 if X contains a thread-local symbol.  */
7337 
7338 static bool
rs6000_tls_referenced_p(rtx x)7339 rs6000_tls_referenced_p (rtx x)
7340 {
7341   if (! TARGET_HAVE_TLS)
7342     return false;
7343 
7344   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
7345 }
7346 
7347 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
7348 
7349 static bool
rs6000_cannot_force_const_mem(enum machine_mode mode ATTRIBUTE_UNUSED,rtx x)7350 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7351 {
7352   if (GET_CODE (x) == HIGH
7353       && GET_CODE (XEXP (x, 0)) == UNSPEC)
7354     return true;
7355 
7356   /* A TLS symbol in the TOC cannot contain a sum.  */
7357   if (GET_CODE (x) == CONST
7358       && GET_CODE (XEXP (x, 0)) == PLUS
7359       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7360       && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
7361     return true;
7362 
7363   /* Do not place an ELF TLS symbol in the constant pool.  */
7364   return TARGET_ELF && rs6000_tls_referenced_p (x);
7365 }
7366 
7367 /* Return 1 if *X is a thread-local symbol.  This is the same as
7368    rs6000_tls_symbol_ref except for the type of the unused argument.  */
7369 
7370 static int
rs6000_tls_symbol_ref_1(rtx * x,void * data ATTRIBUTE_UNUSED)7371 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
7372 {
7373   return RS6000_SYMBOL_REF_TLS_P (*x);
7374 }
7375 
7376 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
7377    that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
7378    can be addressed relative to the toc pointer.  */
7379 
7380 static bool
use_toc_relative_ref(rtx sym)7381 use_toc_relative_ref (rtx sym)
7382 {
7383   return ((constant_pool_expr_p (sym)
7384 	   && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
7385 					       get_pool_mode (sym)))
7386 	  || (TARGET_CMODEL == CMODEL_MEDIUM
7387 	      && SYMBOL_REF_LOCAL_P (sym)));
7388 }
7389 
7390 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
7391    replace the input X, or the original X if no replacement is called for.
7392    The output parameter *WIN is 1 if the calling macro should goto WIN,
7393    0 if it should not.
7394 
7395    For RS/6000, we wish to handle large displacements off a base
7396    register by splitting the addend across an addiu/addis and the mem insn.
7397    This cuts number of extra insns needed from 3 to 1.
7398 
7399    On Darwin, we use this to generate code for floating point constants.
7400    A movsf_low is generated so we wind up with 2 instructions rather than 3.
7401    The Darwin code is inside #if TARGET_MACHO because only then are the
7402    machopic_* functions defined.  */
7403 static rtx
rs6000_legitimize_reload_address(rtx x,enum machine_mode mode,int opnum,int type,int ind_levels ATTRIBUTE_UNUSED,int * win)7404 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
7405 				  int opnum, int type,
7406 				  int ind_levels ATTRIBUTE_UNUSED, int *win)
7407 {
7408   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7409 
7410   /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
7411      DFmode/DImode MEM.  */
7412   if (reg_offset_p
7413       && opnum == 1
7414       && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
7415 	  || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
7416     reg_offset_p = false;
7417 
7418   /* We must recognize output that we have already generated ourselves.  */
7419   if (GET_CODE (x) == PLUS
7420       && GET_CODE (XEXP (x, 0)) == PLUS
7421       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7422       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7423       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7424     {
7425       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7426 		   BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7427 		   opnum, (enum reload_type) type);
7428       *win = 1;
7429       return x;
7430     }
7431 
7432   /* Likewise for (lo_sum (high ...) ...) output we have generated.  */
7433   if (GET_CODE (x) == LO_SUM
7434       && GET_CODE (XEXP (x, 0)) == HIGH)
7435     {
7436       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7437 		   BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7438 		   opnum, (enum reload_type) type);
7439       *win = 1;
7440       return x;
7441     }
7442 
7443 #if TARGET_MACHO
7444   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
7445       && GET_CODE (x) == LO_SUM
7446       && GET_CODE (XEXP (x, 0)) == PLUS
7447       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
7448       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
7449       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
7450       && machopic_operand_p (XEXP (x, 1)))
7451     {
7452       /* Result of previous invocation of this function on Darwin
7453 	 floating point constant.  */
7454       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7455 		   BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7456 		   opnum, (enum reload_type) type);
7457       *win = 1;
7458       return x;
7459     }
7460 #endif
7461 
7462   if (TARGET_CMODEL != CMODEL_SMALL
7463       && reg_offset_p
7464       && small_toc_ref (x, VOIDmode))
7465     {
7466       rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
7467       x = gen_rtx_LO_SUM (Pmode, hi, x);
7468       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7469 		   BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7470 		   opnum, (enum reload_type) type);
7471       *win = 1;
7472       return x;
7473     }
7474 
7475   if (GET_CODE (x) == PLUS
7476       && GET_CODE (XEXP (x, 0)) == REG
7477       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
7478       && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
7479       && GET_CODE (XEXP (x, 1)) == CONST_INT
7480       && reg_offset_p
7481       && !SPE_VECTOR_MODE (mode)
7482       && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
7483 				  || mode == DDmode || mode == TDmode
7484 				  || mode == DImode))
7485       && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
7486     {
7487       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
7488       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
7489       HOST_WIDE_INT high
7490 	= (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
7491 
7492       /* Check for 32-bit overflow.  */
7493       if (high + low != val)
7494 	{
7495 	  *win = 0;
7496 	  return x;
7497 	}
7498 
7499       /* Reload the high part into a base reg; leave the low part
7500 	 in the mem directly.  */
7501 
7502       x = gen_rtx_PLUS (GET_MODE (x),
7503 			gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
7504 				      GEN_INT (high)),
7505 			GEN_INT (low));
7506 
7507       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7508 		   BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7509 		   opnum, (enum reload_type) type);
7510       *win = 1;
7511       return x;
7512     }
7513 
7514   if (GET_CODE (x) == SYMBOL_REF
7515       && reg_offset_p
7516       && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
7517       && !SPE_VECTOR_MODE (mode)
7518 #if TARGET_MACHO
7519       && DEFAULT_ABI == ABI_DARWIN
7520       && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
7521       && machopic_symbol_defined_p (x)
7522 #else
7523       && DEFAULT_ABI == ABI_V4
7524       && !flag_pic
7525 #endif
7526       /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
7527 	 The same goes for DImode without 64-bit gprs and DFmode and DDmode
7528 	 without fprs.
7529 	 ??? Assume floating point reg based on mode?  This assumption is
7530 	 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
7531 	 where reload ends up doing a DFmode load of a constant from
7532 	 mem using two gprs.  Unfortunately, at this point reload
7533 	 hasn't yet selected regs so poking around in reload data
7534 	 won't help and even if we could figure out the regs reliably,
7535 	 we'd still want to allow this transformation when the mem is
7536 	 naturally aligned.  Since we say the address is good here, we
7537 	 can't disable offsets from LO_SUMs in mem_operand_gpr.
7538 	 FIXME: Allow offset from lo_sum for other modes too, when
7539 	 mem is sufficiently aligned.  */
7540       && mode != TFmode
7541       && mode != TDmode
7542       && (mode != TImode || !TARGET_VSX_TIMODE)
7543       && mode != PTImode
7544       && (mode != DImode || TARGET_POWERPC64)
7545       && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
7546 	  || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
7547     {
7548 #if TARGET_MACHO
7549       if (flag_pic)
7550 	{
7551 	  rtx offset = machopic_gen_offset (x);
7552 	  x = gen_rtx_LO_SUM (GET_MODE (x),
7553 		gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7554 		  gen_rtx_HIGH (Pmode, offset)), offset);
7555 	}
7556       else
7557 #endif
7558 	x = gen_rtx_LO_SUM (GET_MODE (x),
7559 	      gen_rtx_HIGH (Pmode, x), x);
7560 
7561       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7562 		   BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7563 		   opnum, (enum reload_type) type);
7564       *win = 1;
7565       return x;
7566     }
7567 
7568   /* Reload an offset address wrapped by an AND that represents the
7569      masking of the lower bits.  Strip the outer AND and let reload
7570      convert the offset address into an indirect address.  For VSX,
7571      force reload to create the address with an AND in a separate
7572      register, because we can't guarantee an altivec register will
7573      be used.  */
7574   if (VECTOR_MEM_ALTIVEC_P (mode)
7575       && GET_CODE (x) == AND
7576       && GET_CODE (XEXP (x, 0)) == PLUS
7577       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7578       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7579       && GET_CODE (XEXP (x, 1)) == CONST_INT
7580       && INTVAL (XEXP (x, 1)) == -16)
7581     {
7582       x = XEXP (x, 0);
7583       *win = 1;
7584       return x;
7585     }
7586 
7587   if (TARGET_TOC
7588       && reg_offset_p
7589       && GET_CODE (x) == SYMBOL_REF
7590       && use_toc_relative_ref (x))
7591     {
7592       x = create_TOC_reference (x, NULL_RTX);
7593       if (TARGET_CMODEL != CMODEL_SMALL)
7594 	push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7595 		     BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7596 		     opnum, (enum reload_type) type);
7597       *win = 1;
7598       return x;
7599     }
7600   *win = 0;
7601   return x;
7602 }
7603 
7604 /* Debug version of rs6000_legitimize_reload_address.  */
7605 static rtx
rs6000_debug_legitimize_reload_address(rtx x,enum machine_mode mode,int opnum,int type,int ind_levels,int * win)7606 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7607 					int opnum, int type,
7608 					int ind_levels, int *win)
7609 {
7610   rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7611 					      ind_levels, win);
7612   fprintf (stderr,
7613 	   "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7614 	   "type = %d, ind_levels = %d, win = %d, original addr:\n",
7615 	   GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7616   debug_rtx (x);
7617 
7618   if (x == ret)
7619     fprintf (stderr, "Same address returned\n");
7620   else if (!ret)
7621     fprintf (stderr, "NULL returned\n");
7622   else
7623     {
7624       fprintf (stderr, "New address:\n");
7625       debug_rtx (ret);
7626     }
7627 
7628   return ret;
7629 }
7630 
7631 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7632    that is a valid memory address for an instruction.
7633    The MODE argument is the machine mode for the MEM expression
7634    that wants to use this address.
7635 
7636    On the RS/6000, there are four valid address: a SYMBOL_REF that
7637    refers to a constant pool entry of an address (or the sum of it
7638    plus a constant), a short (16-bit signed) constant plus a register,
7639    the sum of two registers, or a register indirect, possibly with an
7640    auto-increment.  For DFmode, DDmode and DImode with a constant plus
7641    register, we must ensure that both words are addressable or PowerPC64
7642    with offset word aligned.
7643 
7644    For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7645    32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7646    because adjacent memory cells are accessed by adding word-sized offsets
7647    during assembly output.  */
7648 static bool
rs6000_legitimate_address_p(enum machine_mode mode,rtx x,bool reg_ok_strict)7649 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7650 {
7651   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7652 
7653   /* If this is an unaligned stvx/ldvx type address, discard the outer AND.  */
7654   if (VECTOR_MEM_ALTIVEC_P (mode)
7655       && GET_CODE (x) == AND
7656       && GET_CODE (XEXP (x, 1)) == CONST_INT
7657       && INTVAL (XEXP (x, 1)) == -16)
7658     x = XEXP (x, 0);
7659 
7660   if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7661     return 0;
7662   if (legitimate_indirect_address_p (x, reg_ok_strict))
7663     return 1;
7664   if (TARGET_UPDATE
7665       && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7666       && mode_supports_pre_incdec_p (mode)
7667       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7668     return 1;
7669   if (virtual_stack_registers_memory_p (x))
7670     return 1;
7671   if (reg_offset_p && legitimate_small_data_p (mode, x))
7672     return 1;
7673   if (reg_offset_p
7674       && legitimate_constant_pool_address_p (x, mode,
7675 					     reg_ok_strict || lra_in_progress))
7676     return 1;
7677   /* For TImode, if we have load/store quad and TImode in VSX registers, only
7678      allow register indirect addresses.  This will allow the values to go in
7679      either GPRs or VSX registers without reloading.  The vector types would
7680      tend to go into VSX registers, so we allow REG+REG, while TImode seems
7681      somewhat split, in that some uses are GPR based, and some VSX based.  */
7682   if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
7683     return 0;
7684   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
7685   if (! reg_ok_strict
7686       && reg_offset_p
7687       && GET_CODE (x) == PLUS
7688       && GET_CODE (XEXP (x, 0)) == REG
7689       && (XEXP (x, 0) == virtual_stack_vars_rtx
7690 	  || XEXP (x, 0) == arg_pointer_rtx)
7691       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7692     return 1;
7693   if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7694     return 1;
7695   if (mode != TFmode
7696       && mode != TDmode
7697       && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7698 	  || TARGET_POWERPC64
7699 	  || (mode != DFmode && mode != DDmode)
7700 	  || (TARGET_E500_DOUBLE && mode != DDmode))
7701       && (TARGET_POWERPC64 || mode != DImode)
7702       && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7703       && mode != PTImode
7704       && !avoiding_indexed_address_p (mode)
7705       && legitimate_indexed_address_p (x, reg_ok_strict))
7706     return 1;
7707   if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
7708       && mode_supports_pre_modify_p (mode)
7709       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7710       && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7711 					      reg_ok_strict, false)
7712 	  || (!avoiding_indexed_address_p (mode)
7713 	      && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7714       && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7715     return 1;
7716   if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7717     return 1;
7718   return 0;
7719 }
7720 
7721 /* Debug version of rs6000_legitimate_address_p.  */
7722 static bool
rs6000_debug_legitimate_address_p(enum machine_mode mode,rtx x,bool reg_ok_strict)7723 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7724 				   bool reg_ok_strict)
7725 {
7726   bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7727   fprintf (stderr,
7728 	   "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7729 	   "strict = %d, reload = %s, code = %s\n",
7730 	   ret ? "true" : "false",
7731 	   GET_MODE_NAME (mode),
7732 	   reg_ok_strict,
7733 	   (reload_completed
7734 	    ? "after"
7735 	    : (reload_in_progress ? "progress" : "before")),
7736 	   GET_RTX_NAME (GET_CODE (x)));
7737   debug_rtx (x);
7738 
7739   return ret;
7740 }
7741 
7742 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P.  */
7743 
7744 static bool
rs6000_mode_dependent_address_p(const_rtx addr,addr_space_t as ATTRIBUTE_UNUSED)7745 rs6000_mode_dependent_address_p (const_rtx addr,
7746 				 addr_space_t as ATTRIBUTE_UNUSED)
7747 {
7748   return rs6000_mode_dependent_address_ptr (addr);
7749 }
7750 
7751 /* Go to LABEL if ADDR (a legitimate address expression)
7752    has an effect that depends on the machine mode it is used for.
7753 
7754    On the RS/6000 this is true of all integral offsets (since AltiVec
7755    and VSX modes don't allow them) or is a pre-increment or decrement.
7756 
7757    ??? Except that due to conceptual problems in offsettable_address_p
7758    we can't really report the problems of integral offsets.  So leave
7759    this assuming that the adjustable offset must be valid for the
7760    sub-words of a TFmode operand, which is what we had before.  */
7761 
7762 static bool
rs6000_mode_dependent_address(const_rtx addr)7763 rs6000_mode_dependent_address (const_rtx addr)
7764 {
7765   switch (GET_CODE (addr))
7766     {
7767     case PLUS:
7768       /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7769 	 is considered a legitimate address before reload, so there
7770 	 are no offset restrictions in that case.  Note that this
7771 	 condition is safe in strict mode because any address involving
7772 	 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7773 	 been rejected as illegitimate.  */
7774       if (XEXP (addr, 0) != virtual_stack_vars_rtx
7775 	  && XEXP (addr, 0) != arg_pointer_rtx
7776 	  && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7777 	{
7778 	  unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7779 	  return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7780 	}
7781       break;
7782 
7783     case LO_SUM:
7784       /* Anything in the constant pool is sufficiently aligned that
7785 	 all bytes have the same high part address.  */
7786       return !legitimate_constant_pool_address_p (addr, QImode, false);
7787 
7788     /* Auto-increment cases are now treated generically in recog.c.  */
7789     case PRE_MODIFY:
7790       return TARGET_UPDATE;
7791 
7792     /* AND is only allowed in Altivec loads.  */
7793     case AND:
7794       return true;
7795 
7796     default:
7797       break;
7798     }
7799 
7800   return false;
7801 }
7802 
7803 /* Debug version of rs6000_mode_dependent_address.  */
7804 static bool
rs6000_debug_mode_dependent_address(const_rtx addr)7805 rs6000_debug_mode_dependent_address (const_rtx addr)
7806 {
7807   bool ret = rs6000_mode_dependent_address (addr);
7808 
7809   fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7810 	   ret ? "true" : "false");
7811   debug_rtx (addr);
7812 
7813   return ret;
7814 }
7815 
7816 /* Implement FIND_BASE_TERM.  */
7817 
7818 rtx
rs6000_find_base_term(rtx op)7819 rs6000_find_base_term (rtx op)
7820 {
7821   rtx base;
7822 
7823   base = op;
7824   if (GET_CODE (base) == CONST)
7825     base = XEXP (base, 0);
7826   if (GET_CODE (base) == PLUS)
7827     base = XEXP (base, 0);
7828   if (GET_CODE (base) == UNSPEC)
7829     switch (XINT (base, 1))
7830       {
7831       case UNSPEC_TOCREL:
7832       case UNSPEC_MACHOPIC_OFFSET:
7833 	/* OP represents SYM [+ OFFSET] - ANCHOR.  SYM is the base term
7834 	   for aliasing purposes.  */
7835 	return XVECEXP (base, 0, 0);
7836       }
7837 
7838   return op;
7839 }
7840 
7841 /* More elaborate version of recog's offsettable_memref_p predicate
7842    that works around the ??? note of rs6000_mode_dependent_address.
7843    In particular it accepts
7844 
7845      (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7846 
7847    in 32-bit mode, that the recog predicate rejects.  */
7848 
7849 static bool
rs6000_offsettable_memref_p(rtx op,enum machine_mode reg_mode)7850 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7851 {
7852   bool worst_case;
7853 
7854   if (!MEM_P (op))
7855     return false;
7856 
7857   /* First mimic offsettable_memref_p.  */
7858   if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7859     return true;
7860 
7861   /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7862      the latter predicate knows nothing about the mode of the memory
7863      reference and, therefore, assumes that it is the largest supported
7864      mode (TFmode).  As a consequence, legitimate offsettable memory
7865      references are rejected.  rs6000_legitimate_offset_address_p contains
7866      the correct logic for the PLUS case of rs6000_mode_dependent_address,
7867      at least with a little bit of help here given that we know the
7868      actual registers used.  */
7869   worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7870 		|| GET_MODE_SIZE (reg_mode) == 4);
7871   return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7872 					     true, worst_case);
7873 }
7874 
7875 /* Change register usage conditional on target flags.  */
7876 static void
rs6000_conditional_register_usage(void)7877 rs6000_conditional_register_usage (void)
7878 {
7879   int i;
7880 
7881   if (TARGET_DEBUG_TARGET)
7882     fprintf (stderr, "rs6000_conditional_register_usage called\n");
7883 
7884   /* Set MQ register fixed (already call_used) so that it will not be
7885      allocated.  */
7886   fixed_regs[64] = 1;
7887 
7888   /* 64-bit AIX and Linux reserve GPR13 for thread-private data.  */
7889   if (TARGET_64BIT)
7890     fixed_regs[13] = call_used_regs[13]
7891       = call_really_used_regs[13] = 1;
7892 
7893   /* Conditionally disable FPRs.  */
7894   if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7895     for (i = 32; i < 64; i++)
7896       fixed_regs[i] = call_used_regs[i]
7897 	= call_really_used_regs[i] = 1;
7898 
7899   /* The TOC register is not killed across calls in a way that is
7900      visible to the compiler.  */
7901   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7902     call_really_used_regs[2] = 0;
7903 
7904   if (DEFAULT_ABI == ABI_V4
7905       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7906       && flag_pic == 2)
7907     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7908 
7909   if (DEFAULT_ABI == ABI_V4
7910       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7911       && flag_pic == 1)
7912     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7913       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7914       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7915 
7916   if (DEFAULT_ABI == ABI_DARWIN
7917       && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7918       fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7919       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7920       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7921 
7922   if (TARGET_TOC && TARGET_MINIMAL_TOC)
7923     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7924       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7925 
7926   if (TARGET_SPE)
7927     {
7928       global_regs[SPEFSCR_REGNO] = 1;
7929       /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7930          registers in prologues and epilogues.  We no longer use r14
7931          for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7932          pool for link-compatibility with older versions of GCC.  Once
7933          "old" code has died out, we can return r14 to the allocation
7934          pool.  */
7935       fixed_regs[14]
7936 	= call_used_regs[14]
7937 	= call_really_used_regs[14] = 1;
7938     }
7939 
7940   if (!TARGET_ALTIVEC && !TARGET_VSX)
7941     {
7942       for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7943 	fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7944       call_really_used_regs[VRSAVE_REGNO] = 1;
7945     }
7946 
7947   if (TARGET_ALTIVEC || TARGET_VSX)
7948     global_regs[VSCR_REGNO] = 1;
7949 
7950   if (TARGET_ALTIVEC_ABI)
7951     {
7952       for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7953 	call_used_regs[i] = call_really_used_regs[i] = 1;
7954 
7955       /* AIX reserves VR20:31 in non-extended ABI mode.  */
7956       if (TARGET_XCOFF)
7957 	for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7958 	  fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7959     }
7960 }
7961 
7962 
7963 /* Try to output insns to set TARGET equal to the constant C if it can
7964    be done in less than N insns.  Do all computations in MODE.
7965    Returns the place where the output has been placed if it can be
7966    done and the insns have been emitted.  If it would take more than N
7967    insns, zero is returned and no insns and emitted.  */
7968 
7969 rtx
rs6000_emit_set_const(rtx dest,enum machine_mode mode,rtx source,int n ATTRIBUTE_UNUSED)7970 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7971 		       rtx source, int n ATTRIBUTE_UNUSED)
7972 {
7973   rtx result, insn, set;
7974   HOST_WIDE_INT c0, c1;
7975 
7976   switch (mode)
7977     {
7978     case  QImode:
7979     case HImode:
7980       if (dest == NULL)
7981 	dest = gen_reg_rtx (mode);
7982       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7983       return dest;
7984 
7985     case SImode:
7986       result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7987 
7988       emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7989 			      GEN_INT (INTVAL (source)
7990 				       & (~ (HOST_WIDE_INT) 0xffff))));
7991       emit_insn (gen_rtx_SET (VOIDmode, dest,
7992 			      gen_rtx_IOR (SImode, copy_rtx (result),
7993 					   GEN_INT (INTVAL (source) & 0xffff))));
7994       result = dest;
7995       break;
7996 
7997     case DImode:
7998       switch (GET_CODE (source))
7999 	{
8000 	case CONST_INT:
8001 	  c0 = INTVAL (source);
8002 	  c1 = -(c0 < 0);
8003 	  break;
8004 
8005 	case CONST_DOUBLE:
8006 #if HOST_BITS_PER_WIDE_INT >= 64
8007 	  c0 = CONST_DOUBLE_LOW (source);
8008 	  c1 = -(c0 < 0);
8009 #else
8010 	  c0 = CONST_DOUBLE_LOW (source);
8011 	  c1 = CONST_DOUBLE_HIGH (source);
8012 #endif
8013 	  break;
8014 
8015 	default:
8016 	  gcc_unreachable ();
8017 	}
8018 
8019       result = rs6000_emit_set_long_const (dest, c0, c1);
8020       break;
8021 
8022     default:
8023       gcc_unreachable ();
8024     }
8025 
8026   insn = get_last_insn ();
8027   set = single_set (insn);
8028   if (! CONSTANT_P (SET_SRC (set)))
8029     set_unique_reg_note (insn, REG_EQUAL, source);
8030 
8031   return result;
8032 }
8033 
8034 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
8035    fall back to a straight forward decomposition.  We do this to avoid
8036    exponential run times encountered when looking for longer sequences
8037    with rs6000_emit_set_const.  */
8038 static rtx
rs6000_emit_set_long_const(rtx dest,HOST_WIDE_INT c1,HOST_WIDE_INT c2)8039 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
8040 {
8041   if (!TARGET_POWERPC64)
8042     {
8043       rtx operand1, operand2;
8044 
8045       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
8046 					DImode);
8047       operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
8048 					DImode);
8049       emit_move_insn (operand1, GEN_INT (c1));
8050       emit_move_insn (operand2, GEN_INT (c2));
8051     }
8052   else
8053     {
8054       HOST_WIDE_INT ud1, ud2, ud3, ud4;
8055 
8056       ud1 = c1 & 0xffff;
8057       ud2 = (c1 & 0xffff0000) >> 16;
8058 #if HOST_BITS_PER_WIDE_INT >= 64
8059       c2 = c1 >> 32;
8060 #endif
8061       ud3 = c2 & 0xffff;
8062       ud4 = (c2 & 0xffff0000) >> 16;
8063 
8064       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
8065 	  || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
8066 	{
8067 	  if (ud1 & 0x8000)
8068 	    emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) -  0x8000)));
8069 	  else
8070 	    emit_move_insn (dest, GEN_INT (ud1));
8071 	}
8072 
8073       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
8074 	       || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
8075 	{
8076 	  if (ud2 & 0x8000)
8077 	    emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
8078 					   - 0x80000000));
8079 	  else
8080 	    emit_move_insn (dest, GEN_INT (ud2 << 16));
8081 	  if (ud1 != 0)
8082 	    emit_move_insn (copy_rtx (dest),
8083 			    gen_rtx_IOR (DImode, copy_rtx (dest),
8084 					 GEN_INT (ud1)));
8085 	}
8086       else if (ud3 == 0 && ud4 == 0)
8087 	{
8088 	  gcc_assert (ud2 & 0x8000);
8089 	  emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
8090 					 - 0x80000000));
8091 	  if (ud1 != 0)
8092 	    emit_move_insn (copy_rtx (dest),
8093 			    gen_rtx_IOR (DImode, copy_rtx (dest),
8094 					 GEN_INT (ud1)));
8095 	  emit_move_insn (copy_rtx (dest),
8096 			  gen_rtx_ZERO_EXTEND (DImode,
8097 					       gen_lowpart (SImode,
8098 							    copy_rtx (dest))));
8099 	}
8100       else if ((ud4 == 0xffff && (ud3 & 0x8000))
8101 	       || (ud4 == 0 && ! (ud3 & 0x8000)))
8102 	{
8103 	  if (ud3 & 0x8000)
8104 	    emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
8105 					   - 0x80000000));
8106 	  else
8107 	    emit_move_insn (dest, GEN_INT (ud3 << 16));
8108 
8109 	  if (ud2 != 0)
8110 	    emit_move_insn (copy_rtx (dest),
8111 			    gen_rtx_IOR (DImode, copy_rtx (dest),
8112 					 GEN_INT (ud2)));
8113 	  emit_move_insn (copy_rtx (dest),
8114 			  gen_rtx_ASHIFT (DImode, copy_rtx (dest),
8115 					  GEN_INT (16)));
8116 	  if (ud1 != 0)
8117 	    emit_move_insn (copy_rtx (dest),
8118 			    gen_rtx_IOR (DImode, copy_rtx (dest),
8119 					 GEN_INT (ud1)));
8120 	}
8121       else
8122 	{
8123 	  if (ud4 & 0x8000)
8124 	    emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
8125 					   - 0x80000000));
8126 	  else
8127 	    emit_move_insn (dest, GEN_INT (ud4 << 16));
8128 
8129 	  if (ud3 != 0)
8130 	    emit_move_insn (copy_rtx (dest),
8131 			    gen_rtx_IOR (DImode, copy_rtx (dest),
8132 					 GEN_INT (ud3)));
8133 
8134 	  emit_move_insn (copy_rtx (dest),
8135 			  gen_rtx_ASHIFT (DImode, copy_rtx (dest),
8136 					  GEN_INT (32)));
8137 	  if (ud2 != 0)
8138 	    emit_move_insn (copy_rtx (dest),
8139 			    gen_rtx_IOR (DImode, copy_rtx (dest),
8140 					 GEN_INT (ud2 << 16)));
8141 	  if (ud1 != 0)
8142 	    emit_move_insn (copy_rtx (dest),
8143 			    gen_rtx_IOR (DImode, copy_rtx (dest), GEN_INT (ud1)));
8144 	}
8145     }
8146   return dest;
8147 }
8148 
8149 /* Helper for the following.  Get rid of [r+r] memory refs
8150    in cases where it won't work (TImode, TFmode, TDmode, PTImode).  */
8151 
8152 static void
rs6000_eliminate_indexed_memrefs(rtx operands[2])8153 rs6000_eliminate_indexed_memrefs (rtx operands[2])
8154 {
8155   if (reload_in_progress)
8156     return;
8157 
8158   if (GET_CODE (operands[0]) == MEM
8159       && GET_CODE (XEXP (operands[0], 0)) != REG
8160       && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
8161 					       GET_MODE (operands[0]), false))
8162     operands[0]
8163       = replace_equiv_address (operands[0],
8164 			       copy_addr_to_reg (XEXP (operands[0], 0)));
8165 
8166   if (GET_CODE (operands[1]) == MEM
8167       && GET_CODE (XEXP (operands[1], 0)) != REG
8168       && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
8169 					       GET_MODE (operands[1]), false))
8170     operands[1]
8171       = replace_equiv_address (operands[1],
8172 			       copy_addr_to_reg (XEXP (operands[1], 0)));
8173 }
8174 
8175 /* Generate a vector of constants to permute MODE for a little-endian
8176    storage operation by swapping the two halves of a vector.  */
8177 static rtvec
rs6000_const_vec(enum machine_mode mode)8178 rs6000_const_vec (enum machine_mode mode)
8179 {
8180   int i, subparts;
8181   rtvec v;
8182 
8183   switch (mode)
8184     {
8185     case V1TImode:
8186       subparts = 1;
8187       break;
8188     case V2DFmode:
8189     case V2DImode:
8190       subparts = 2;
8191       break;
8192     case V4SFmode:
8193     case V4SImode:
8194       subparts = 4;
8195       break;
8196     case V8HImode:
8197       subparts = 8;
8198       break;
8199     case V16QImode:
8200       subparts = 16;
8201       break;
8202     default:
8203       gcc_unreachable();
8204     }
8205 
8206   v = rtvec_alloc (subparts);
8207 
8208   for (i = 0; i < subparts / 2; ++i)
8209     RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
8210   for (i = subparts / 2; i < subparts; ++i)
8211     RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
8212 
8213   return v;
8214 }
8215 
8216 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
8217    for a VSX load or store operation.  */
8218 rtx
rs6000_gen_le_vsx_permute(rtx source,enum machine_mode mode)8219 rs6000_gen_le_vsx_permute (rtx source, enum machine_mode mode)
8220 {
8221   rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
8222   return gen_rtx_VEC_SELECT (mode, source, par);
8223 }
8224 
8225 /* Emit a little-endian load from vector memory location SOURCE to VSX
8226    register DEST in mode MODE.  The load is done with two permuting
8227    insn's that represent an lxvd2x and xxpermdi.  */
8228 void
rs6000_emit_le_vsx_load(rtx dest,rtx source,enum machine_mode mode)8229 rs6000_emit_le_vsx_load (rtx dest, rtx source, enum machine_mode mode)
8230 {
8231   rtx tmp, permute_mem, permute_reg;
8232 
8233   /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8234      V1TImode).  */
8235   if (mode == TImode || mode == V1TImode)
8236     {
8237       mode = V2DImode;
8238       dest = gen_lowpart (V2DImode, dest);
8239       source = adjust_address (source, V2DImode, 0);
8240     }
8241 
8242   tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
8243   permute_mem = rs6000_gen_le_vsx_permute (source, mode);
8244   permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
8245   emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_mem));
8246   emit_insn (gen_rtx_SET (VOIDmode, dest, permute_reg));
8247 }
8248 
8249 /* Emit a little-endian store to vector memory location DEST from VSX
8250    register SOURCE in mode MODE.  The store is done with two permuting
8251    insn's that represent an xxpermdi and an stxvd2x.  */
8252 void
rs6000_emit_le_vsx_store(rtx dest,rtx source,enum machine_mode mode)8253 rs6000_emit_le_vsx_store (rtx dest, rtx source, enum machine_mode mode)
8254 {
8255   rtx tmp, permute_src, permute_tmp;
8256 
8257   /* This should never be called during or after reload, because it does
8258      not re-permute the source register.  It is intended only for use
8259      during expand.  */
8260   gcc_assert (!reload_in_progress && !lra_in_progress && !reload_completed);
8261 
8262   /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8263      V1TImode).  */
8264   if (mode == TImode || mode == V1TImode)
8265     {
8266       mode = V2DImode;
8267       dest = adjust_address (dest, V2DImode, 0);
8268       source = gen_lowpart (V2DImode, source);
8269     }
8270 
8271   tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
8272   permute_src = rs6000_gen_le_vsx_permute (source, mode);
8273   permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
8274   emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_src));
8275   emit_insn (gen_rtx_SET (VOIDmode, dest, permute_tmp));
8276 }
8277 
8278 /* Emit a sequence representing a little-endian VSX load or store,
8279    moving data from SOURCE to DEST in mode MODE.  This is done
8280    separately from rs6000_emit_move to ensure it is called only
8281    during expand.  LE VSX loads and stores introduced later are
8282    handled with a split.  The expand-time RTL generation allows
8283    us to optimize away redundant pairs of register-permutes.  */
8284 void
rs6000_emit_le_vsx_move(rtx dest,rtx source,enum machine_mode mode)8285 rs6000_emit_le_vsx_move (rtx dest, rtx source, enum machine_mode mode)
8286 {
8287   gcc_assert (!BYTES_BIG_ENDIAN
8288 	      && VECTOR_MEM_VSX_P (mode)
8289 	      && !gpr_or_gpr_p (dest, source)
8290 	      && (MEM_P (source) ^ MEM_P (dest)));
8291 
8292   if (MEM_P (source))
8293     {
8294       gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
8295       rs6000_emit_le_vsx_load (dest, source, mode);
8296     }
8297   else
8298     {
8299       if (!REG_P (source))
8300 	source = force_reg (mode, source);
8301       rs6000_emit_le_vsx_store (dest, source, mode);
8302     }
8303 }
8304 
8305 /* Emit a move from SOURCE to DEST in mode MODE.  */
8306 void
rs6000_emit_move(rtx dest,rtx source,enum machine_mode mode)8307 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
8308 {
8309   rtx operands[2];
8310   operands[0] = dest;
8311   operands[1] = source;
8312 
8313   if (TARGET_DEBUG_ADDR)
8314     {
8315       fprintf (stderr,
8316 	       "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
8317 	       "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
8318 	       GET_MODE_NAME (mode),
8319 	       reload_in_progress,
8320 	       reload_completed,
8321 	       can_create_pseudo_p ());
8322       debug_rtx (dest);
8323       fprintf (stderr, "source:\n");
8324       debug_rtx (source);
8325     }
8326 
8327   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
8328   if (GET_CODE (operands[1]) == CONST_DOUBLE
8329       && ! FLOAT_MODE_P (mode)
8330       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8331     {
8332       /* FIXME.  This should never happen.  */
8333       /* Since it seems that it does, do the safe thing and convert
8334 	 to a CONST_INT.  */
8335       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
8336     }
8337   gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
8338 	      || FLOAT_MODE_P (mode)
8339 	      || ((CONST_DOUBLE_HIGH (operands[1]) != 0
8340 		   || CONST_DOUBLE_LOW (operands[1]) < 0)
8341 		  && (CONST_DOUBLE_HIGH (operands[1]) != -1
8342 		      || CONST_DOUBLE_LOW (operands[1]) >= 0)));
8343 
8344   /* Check if GCC is setting up a block move that will end up using FP
8345      registers as temporaries.  We must make sure this is acceptable.  */
8346   if (GET_CODE (operands[0]) == MEM
8347       && GET_CODE (operands[1]) == MEM
8348       && mode == DImode
8349       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
8350 	  || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
8351       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
8352 					    ? 32 : MEM_ALIGN (operands[0])))
8353 	    || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
8354 					       ? 32
8355 					       : MEM_ALIGN (operands[1]))))
8356       && ! MEM_VOLATILE_P (operands [0])
8357       && ! MEM_VOLATILE_P (operands [1]))
8358     {
8359       emit_move_insn (adjust_address (operands[0], SImode, 0),
8360 		      adjust_address (operands[1], SImode, 0));
8361       emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
8362 		      adjust_address (copy_rtx (operands[1]), SImode, 4));
8363       return;
8364     }
8365 
8366   if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
8367       && !gpc_reg_operand (operands[1], mode))
8368     operands[1] = force_reg (mode, operands[1]);
8369 
8370   /* Recognize the case where operand[1] is a reference to thread-local
8371      data and load its address to a register.  */
8372   if (rs6000_tls_referenced_p (operands[1]))
8373     {
8374       enum tls_model model;
8375       rtx tmp = operands[1];
8376       rtx addend = NULL;
8377 
8378       if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
8379 	{
8380           addend = XEXP (XEXP (tmp, 0), 1);
8381 	  tmp = XEXP (XEXP (tmp, 0), 0);
8382 	}
8383 
8384       gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
8385       model = SYMBOL_REF_TLS_MODEL (tmp);
8386       gcc_assert (model != 0);
8387 
8388       tmp = rs6000_legitimize_tls_address (tmp, model);
8389       if (addend)
8390 	{
8391 	  tmp = gen_rtx_PLUS (mode, tmp, addend);
8392 	  tmp = force_operand (tmp, operands[0]);
8393 	}
8394       operands[1] = tmp;
8395     }
8396 
8397   /* Handle the case where reload calls us with an invalid address.  */
8398   if (reload_in_progress && mode == Pmode
8399       && (! general_operand (operands[1], mode)
8400 	  || ! nonimmediate_operand (operands[0], mode)))
8401     goto emit_set;
8402 
8403   /* 128-bit constant floating-point values on Darwin should really be
8404      loaded as two parts.  */
8405   if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
8406       && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
8407     {
8408       rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
8409 			simplify_gen_subreg (DFmode, operands[1], mode, 0),
8410 			DFmode);
8411       rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
8412 					     GET_MODE_SIZE (DFmode)),
8413 			simplify_gen_subreg (DFmode, operands[1], mode,
8414 					     GET_MODE_SIZE (DFmode)),
8415 			DFmode);
8416       return;
8417     }
8418 
8419   if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
8420     cfun->machine->sdmode_stack_slot =
8421       eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
8422 
8423 
8424   if (lra_in_progress
8425       && mode == SDmode
8426       && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
8427       && reg_preferred_class (REGNO (operands[0])) == NO_REGS
8428       && (REG_P (operands[1])
8429 	  || (GET_CODE (operands[1]) == SUBREG
8430 	      && REG_P (SUBREG_REG (operands[1])))))
8431     {
8432       int regno = REGNO (GET_CODE (operands[1]) == SUBREG
8433 			 ? SUBREG_REG (operands[1]) : operands[1]);
8434       enum reg_class cl;
8435 
8436       if (regno >= FIRST_PSEUDO_REGISTER)
8437 	{
8438 	  cl = reg_preferred_class (regno);
8439 	  gcc_assert (cl != NO_REGS);
8440 	  regno = ira_class_hard_regs[cl][0];
8441 	}
8442       if (FP_REGNO_P (regno))
8443 	{
8444 	  if (GET_MODE (operands[0]) != DDmode)
8445 	    operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
8446 	  emit_insn (gen_movsd_store (operands[0], operands[1]));
8447 	}
8448       else if (INT_REGNO_P (regno))
8449 	emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8450       else
8451 	gcc_unreachable();
8452       return;
8453     }
8454   if (lra_in_progress
8455       && mode == SDmode
8456       && (REG_P (operands[0])
8457 	  || (GET_CODE (operands[0]) == SUBREG
8458 	      && REG_P (SUBREG_REG (operands[0]))))
8459       && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
8460       && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
8461     {
8462       int regno = REGNO (GET_CODE (operands[0]) == SUBREG
8463 			 ? SUBREG_REG (operands[0]) : operands[0]);
8464       enum reg_class cl;
8465 
8466       if (regno >= FIRST_PSEUDO_REGISTER)
8467 	{
8468 	  cl = reg_preferred_class (regno);
8469 	  gcc_assert (cl != NO_REGS);
8470 	  regno = ira_class_hard_regs[cl][0];
8471 	}
8472       if (FP_REGNO_P (regno))
8473 	{
8474 	  if (GET_MODE (operands[1]) != DDmode)
8475 	    operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
8476 	  emit_insn (gen_movsd_load (operands[0], operands[1]));
8477 	}
8478       else if (INT_REGNO_P (regno))
8479 	emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8480       else
8481 	gcc_unreachable();
8482       return;
8483     }
8484 
8485   if (reload_in_progress
8486       && mode == SDmode
8487       && cfun->machine->sdmode_stack_slot != NULL_RTX
8488       && MEM_P (operands[0])
8489       && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
8490       && REG_P (operands[1]))
8491     {
8492       if (FP_REGNO_P (REGNO (operands[1])))
8493 	{
8494 	  rtx mem = adjust_address_nv (operands[0], DDmode, 0);
8495 	  mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8496 	  emit_insn (gen_movsd_store (mem, operands[1]));
8497 	}
8498       else if (INT_REGNO_P (REGNO (operands[1])))
8499 	{
8500 	  rtx mem = operands[0];
8501 	  if (BYTES_BIG_ENDIAN)
8502 	    mem = adjust_address_nv (mem, mode, 4);
8503 	  mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8504 	  emit_insn (gen_movsd_hardfloat (mem, operands[1]));
8505 	}
8506       else
8507 	gcc_unreachable();
8508       return;
8509     }
8510   if (reload_in_progress
8511       && mode == SDmode
8512       && REG_P (operands[0])
8513       && MEM_P (operands[1])
8514       && cfun->machine->sdmode_stack_slot != NULL_RTX
8515       && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
8516     {
8517       if (FP_REGNO_P (REGNO (operands[0])))
8518 	{
8519 	  rtx mem = adjust_address_nv (operands[1], DDmode, 0);
8520 	  mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8521 	  emit_insn (gen_movsd_load (operands[0], mem));
8522 	}
8523       else if (INT_REGNO_P (REGNO (operands[0])))
8524 	{
8525 	  rtx mem = operands[1];
8526 	  if (BYTES_BIG_ENDIAN)
8527 	    mem = adjust_address_nv (mem, mode, 4);
8528 	  mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8529 	  emit_insn (gen_movsd_hardfloat (operands[0], mem));
8530 	}
8531       else
8532 	gcc_unreachable();
8533       return;
8534     }
8535 
8536   /* FIXME:  In the long term, this switch statement should go away
8537      and be replaced by a sequence of tests based on things like
8538      mode == Pmode.  */
8539   switch (mode)
8540     {
8541     case HImode:
8542     case QImode:
8543       if (CONSTANT_P (operands[1])
8544 	  && GET_CODE (operands[1]) != CONST_INT)
8545 	operands[1] = force_const_mem (mode, operands[1]);
8546       break;
8547 
8548     case TFmode:
8549     case TDmode:
8550       rs6000_eliminate_indexed_memrefs (operands);
8551       /* fall through */
8552 
8553     case DFmode:
8554     case DDmode:
8555     case SFmode:
8556     case SDmode:
8557       if (CONSTANT_P (operands[1])
8558 	  && ! easy_fp_constant (operands[1], mode))
8559 	operands[1] = force_const_mem (mode, operands[1]);
8560       break;
8561 
8562     case V16QImode:
8563     case V8HImode:
8564     case V4SFmode:
8565     case V4SImode:
8566     case V4HImode:
8567     case V2SFmode:
8568     case V2SImode:
8569     case V1DImode:
8570     case V2DFmode:
8571     case V2DImode:
8572     case V1TImode:
8573       if (CONSTANT_P (operands[1])
8574 	  && !easy_vector_constant (operands[1], mode))
8575 	operands[1] = force_const_mem (mode, operands[1]);
8576       break;
8577 
8578     case SImode:
8579     case DImode:
8580       /* Use default pattern for address of ELF small data */
8581       if (TARGET_ELF
8582 	  && mode == Pmode
8583 	  && DEFAULT_ABI == ABI_V4
8584 	  && (GET_CODE (operands[1]) == SYMBOL_REF
8585 	      || GET_CODE (operands[1]) == CONST)
8586 	  && small_data_operand (operands[1], mode))
8587 	{
8588 	  emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8589 	  return;
8590 	}
8591 
8592       if (DEFAULT_ABI == ABI_V4
8593 	  && mode == Pmode && mode == SImode
8594 	  && flag_pic == 1 && got_operand (operands[1], mode))
8595 	{
8596 	  emit_insn (gen_movsi_got (operands[0], operands[1]));
8597 	  return;
8598 	}
8599 
8600       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
8601 	  && TARGET_NO_TOC
8602 	  && ! flag_pic
8603 	  && mode == Pmode
8604 	  && CONSTANT_P (operands[1])
8605 	  && GET_CODE (operands[1]) != HIGH
8606 	  && GET_CODE (operands[1]) != CONST_INT)
8607 	{
8608 	  rtx target = (!can_create_pseudo_p ()
8609 			? operands[0]
8610 			: gen_reg_rtx (mode));
8611 
8612 	  /* If this is a function address on -mcall-aixdesc,
8613 	     convert it to the address of the descriptor.  */
8614 	  if (DEFAULT_ABI == ABI_AIX
8615 	      && GET_CODE (operands[1]) == SYMBOL_REF
8616 	      && XSTR (operands[1], 0)[0] == '.')
8617 	    {
8618 	      const char *name = XSTR (operands[1], 0);
8619 	      rtx new_ref;
8620 	      while (*name == '.')
8621 		name++;
8622 	      new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
8623 	      CONSTANT_POOL_ADDRESS_P (new_ref)
8624 		= CONSTANT_POOL_ADDRESS_P (operands[1]);
8625 	      SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
8626 	      SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
8627 	      SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
8628 	      operands[1] = new_ref;
8629 	    }
8630 
8631 	  if (DEFAULT_ABI == ABI_DARWIN)
8632 	    {
8633 #if TARGET_MACHO
8634 	      if (MACHO_DYNAMIC_NO_PIC_P)
8635 		{
8636 		  /* Take care of any required data indirection.  */
8637 		  operands[1] = rs6000_machopic_legitimize_pic_address (
8638 				  operands[1], mode, operands[0]);
8639 		  if (operands[0] != operands[1])
8640 		    emit_insn (gen_rtx_SET (VOIDmode,
8641 					    operands[0], operands[1]));
8642 		  return;
8643 		}
8644 #endif
8645 	      emit_insn (gen_macho_high (target, operands[1]));
8646 	      emit_insn (gen_macho_low (operands[0], target, operands[1]));
8647 	      return;
8648 	    }
8649 
8650 	  emit_insn (gen_elf_high (target, operands[1]));
8651 	  emit_insn (gen_elf_low (operands[0], target, operands[1]));
8652 	  return;
8653 	}
8654 
8655       /* If this is a SYMBOL_REF that refers to a constant pool entry,
8656 	 and we have put it in the TOC, we just need to make a TOC-relative
8657 	 reference to it.  */
8658       if (TARGET_TOC
8659 	  && GET_CODE (operands[1]) == SYMBOL_REF
8660 	  && use_toc_relative_ref (operands[1]))
8661 	operands[1] = create_TOC_reference (operands[1], operands[0]);
8662       else if (mode == Pmode
8663 	       && CONSTANT_P (operands[1])
8664 	       && GET_CODE (operands[1]) != HIGH
8665 	       && ((GET_CODE (operands[1]) != CONST_INT
8666 		    && ! easy_fp_constant (operands[1], mode))
8667 		   || (GET_CODE (operands[1]) == CONST_INT
8668 		       && (num_insns_constant (operands[1], mode)
8669 			   > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
8670 		   || (GET_CODE (operands[0]) == REG
8671 		       && FP_REGNO_P (REGNO (operands[0]))))
8672 	       && !toc_relative_expr_p (operands[1], false)
8673 	       && (TARGET_CMODEL == CMODEL_SMALL
8674 		   || can_create_pseudo_p ()
8675 		   || (REG_P (operands[0])
8676 		       && INT_REG_OK_FOR_BASE_P (operands[0], true))))
8677 	{
8678 
8679 #if TARGET_MACHO
8680 	  /* Darwin uses a special PIC legitimizer.  */
8681 	  if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
8682 	    {
8683 	      operands[1] =
8684 		rs6000_machopic_legitimize_pic_address (operands[1], mode,
8685 							operands[0]);
8686 	      if (operands[0] != operands[1])
8687 		emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8688 	      return;
8689 	    }
8690 #endif
8691 
8692 	  /* If we are to limit the number of things we put in the TOC and
8693 	     this is a symbol plus a constant we can add in one insn,
8694 	     just put the symbol in the TOC and add the constant.  Don't do
8695 	     this if reload is in progress.  */
8696 	  if (GET_CODE (operands[1]) == CONST
8697 	      && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
8698 	      && GET_CODE (XEXP (operands[1], 0)) == PLUS
8699 	      && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
8700 	      && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
8701 		  || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
8702 	      && ! side_effects_p (operands[0]))
8703 	    {
8704 	      rtx sym =
8705 		force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
8706 	      rtx other = XEXP (XEXP (operands[1], 0), 1);
8707 
8708 	      sym = force_reg (mode, sym);
8709 	      emit_insn (gen_add3_insn (operands[0], sym, other));
8710 	      return;
8711 	    }
8712 
8713 	  operands[1] = force_const_mem (mode, operands[1]);
8714 
8715 	  if (TARGET_TOC
8716 	      && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
8717 	      && constant_pool_expr_p (XEXP (operands[1], 0))
8718 	      && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
8719 			get_pool_constant (XEXP (operands[1], 0)),
8720 			get_pool_mode (XEXP (operands[1], 0))))
8721 	    {
8722 	      rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
8723 						 operands[0]);
8724 	      operands[1] = gen_const_mem (mode, tocref);
8725 	      set_mem_alias_set (operands[1], get_TOC_alias_set ());
8726 	    }
8727 	}
8728       break;
8729 
8730     case TImode:
8731       if (!VECTOR_MEM_VSX_P (TImode))
8732 	rs6000_eliminate_indexed_memrefs (operands);
8733       break;
8734 
8735     case PTImode:
8736       rs6000_eliminate_indexed_memrefs (operands);
8737       break;
8738 
8739     default:
8740       fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
8741     }
8742 
8743   /* Above, we may have called force_const_mem which may have returned
8744      an invalid address.  If we can, fix this up; otherwise, reload will
8745      have to deal with it.  */
8746   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
8747     operands[1] = validize_mem (operands[1]);
8748 
8749  emit_set:
8750   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8751 }
8752 
8753 /* Return true if a structure, union or array containing FIELD should be
8754    accessed using `BLKMODE'.
8755 
8756    For the SPE, simd types are V2SI, and gcc can be tempted to put the
8757    entire thing in a DI and use subregs to access the internals.
8758    store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8759    back-end.  Because a single GPR can hold a V2SI, but not a DI, the
8760    best thing to do is set structs to BLKmode and avoid Severe Tire
8761    Damage.
8762 
8763    On e500 v2, DF and DI modes suffer from the same anomaly.  DF can
8764    fit into 1, whereas DI still needs two.  */
8765 
8766 static bool
rs6000_member_type_forces_blk(const_tree field,enum machine_mode mode)8767 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8768 {
8769   return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8770 	  || (TARGET_E500_DOUBLE && mode == DFmode));
8771 }
8772 
8773 /* Nonzero if we can use a floating-point register to pass this arg.  */
8774 #define USE_FP_FOR_ARG_P(CUM,MODE)		\
8775   (SCALAR_FLOAT_MODE_P (MODE)			\
8776    && (CUM)->fregno <= FP_ARG_MAX_REG		\
8777    && TARGET_HARD_FLOAT && TARGET_FPRS)
8778 
8779 /* Nonzero if we can use an AltiVec register to pass this arg.  */
8780 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED)			\
8781   (ALTIVEC_OR_VSX_VECTOR_MODE (MODE)				\
8782    && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG			\
8783    && TARGET_ALTIVEC_ABI					\
8784    && (NAMED))
8785 
8786 /* Walk down the type tree of TYPE counting consecutive base elements.
8787    If *MODEP is VOIDmode, then set it to the first valid floating point
8788    or vector type.  If a non-floating point or vector type is found, or
8789    if a floating point or vector type that doesn't match a non-VOIDmode
8790    *MODEP is found, then return -1, otherwise return the count in the
8791    sub-tree.  */
8792 
8793 static int
rs6000_aggregate_candidate(const_tree type,enum machine_mode * modep)8794 rs6000_aggregate_candidate (const_tree type, enum machine_mode *modep)
8795 {
8796   enum machine_mode mode;
8797   HOST_WIDE_INT size;
8798 
8799   switch (TREE_CODE (type))
8800     {
8801     case REAL_TYPE:
8802       mode = TYPE_MODE (type);
8803       if (!SCALAR_FLOAT_MODE_P (mode))
8804 	return -1;
8805 
8806       if (*modep == VOIDmode)
8807 	*modep = mode;
8808 
8809       if (*modep == mode)
8810 	return 1;
8811 
8812       break;
8813 
8814     case COMPLEX_TYPE:
8815       mode = TYPE_MODE (TREE_TYPE (type));
8816       if (!SCALAR_FLOAT_MODE_P (mode))
8817 	return -1;
8818 
8819       if (*modep == VOIDmode)
8820 	*modep = mode;
8821 
8822       if (*modep == mode)
8823 	return 2;
8824 
8825       break;
8826 
8827     case VECTOR_TYPE:
8828       if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
8829 	return -1;
8830 
8831       /* Use V4SImode as representative of all 128-bit vector types.  */
8832       size = int_size_in_bytes (type);
8833       switch (size)
8834 	{
8835 	case 16:
8836 	  mode = V4SImode;
8837 	  break;
8838 	default:
8839 	  return -1;
8840 	}
8841 
8842       if (*modep == VOIDmode)
8843 	*modep = mode;
8844 
8845       /* Vector modes are considered to be opaque: two vectors are
8846 	 equivalent for the purposes of being homogeneous aggregates
8847 	 if they are the same size.  */
8848       if (*modep == mode)
8849 	return 1;
8850 
8851       break;
8852 
8853     case ARRAY_TYPE:
8854       {
8855 	int count;
8856 	tree index = TYPE_DOMAIN (type);
8857 
8858 	/* Can't handle incomplete types.  */
8859 	if (!COMPLETE_TYPE_P (type))
8860 	  return -1;
8861 
8862 	count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
8863 	if (count == -1
8864 	    || !index
8865 	    || !TYPE_MAX_VALUE (index)
8866 	    || !host_integerp (TYPE_MAX_VALUE (index), 1)
8867 	    || !TYPE_MIN_VALUE (index)
8868 	    || !host_integerp (TYPE_MIN_VALUE (index), 1)
8869 	    || count < 0)
8870 	  return -1;
8871 
8872 	count *= (1 + tree_low_cst (TYPE_MAX_VALUE (index), 1)
8873 		      - tree_low_cst (TYPE_MIN_VALUE (index), 1));
8874 
8875 	/* There must be no padding.  */
8876 	if (!host_integerp (TYPE_SIZE (type), 1)
8877 	    || (tree_low_cst (TYPE_SIZE (type), 1)
8878 		!= count * GET_MODE_BITSIZE (*modep)))
8879 	  return -1;
8880 
8881 	return count;
8882       }
8883 
8884     case RECORD_TYPE:
8885       {
8886 	int count = 0;
8887 	int sub_count;
8888 	tree field;
8889 
8890 	/* Can't handle incomplete types.  */
8891 	if (!COMPLETE_TYPE_P (type))
8892 	  return -1;
8893 
8894 	for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8895 	  {
8896 	    if (TREE_CODE (field) != FIELD_DECL)
8897 	      continue;
8898 
8899 	    sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8900 	    if (sub_count < 0)
8901 	      return -1;
8902 	    count += sub_count;
8903 	  }
8904 
8905 	/* There must be no padding.  */
8906 	if (!host_integerp (TYPE_SIZE (type), 1)
8907 	    || (tree_low_cst (TYPE_SIZE (type), 1)
8908 		!= count * GET_MODE_BITSIZE (*modep)))
8909 	  return -1;
8910 
8911 	return count;
8912       }
8913 
8914     case UNION_TYPE:
8915     case QUAL_UNION_TYPE:
8916       {
8917 	/* These aren't very interesting except in a degenerate case.  */
8918 	int count = 0;
8919 	int sub_count;
8920 	tree field;
8921 
8922 	/* Can't handle incomplete types.  */
8923 	if (!COMPLETE_TYPE_P (type))
8924 	  return -1;
8925 
8926 	for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8927 	  {
8928 	    if (TREE_CODE (field) != FIELD_DECL)
8929 	      continue;
8930 
8931 	    sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8932 	    if (sub_count < 0)
8933 	      return -1;
8934 	    count = count > sub_count ? count : sub_count;
8935 	  }
8936 
8937 	/* There must be no padding.  */
8938 	if (!host_integerp (TYPE_SIZE (type), 1)
8939 	    || (tree_low_cst (TYPE_SIZE (type), 1)
8940 		!= count * GET_MODE_BITSIZE (*modep)))
8941 	  return -1;
8942 
8943 	return count;
8944       }
8945 
8946     default:
8947       break;
8948     }
8949 
8950   return -1;
8951 }
8952 
8953 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
8954    float or vector aggregate that shall be passed in FP/vector registers
8955    according to the ELFv2 ABI, return the homogeneous element mode in
8956    *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
8957 
8958    Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE.  */
8959 
8960 static bool
rs6000_discover_homogeneous_aggregate(enum machine_mode mode,const_tree type,enum machine_mode * elt_mode,int * n_elts)8961 rs6000_discover_homogeneous_aggregate (enum machine_mode mode, const_tree type,
8962 				       enum machine_mode *elt_mode,
8963 				       int *n_elts)
8964 {
8965   /* Note that we do not accept complex types at the top level as
8966      homogeneous aggregates; these types are handled via the
8967      targetm.calls.split_complex_arg mechanism.  Complex types
8968      can be elements of homogeneous aggregates, however.  */
8969   if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
8970     {
8971       enum machine_mode field_mode = VOIDmode;
8972       int field_count = rs6000_aggregate_candidate (type, &field_mode);
8973 
8974       if (field_count > 0)
8975 	{
8976 	  int n_regs = (SCALAR_FLOAT_MODE_P (field_mode)?
8977 			(GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
8978 
8979 	  /* The ELFv2 ABI allows homogeneous aggregates to occupy
8980 	     up to AGGR_ARG_NUM_REG registers.  */
8981 	  if (field_count * n_regs <= AGGR_ARG_NUM_REG)
8982 	    {
8983 	      if (elt_mode)
8984 		*elt_mode = field_mode;
8985 	      if (n_elts)
8986 		*n_elts = field_count;
8987 	      return true;
8988 	    }
8989 	}
8990     }
8991 
8992   if (elt_mode)
8993     *elt_mode = mode;
8994   if (n_elts)
8995     *n_elts = 1;
8996   return false;
8997 }
8998 
8999 /* Return a nonzero value to say to return the function value in
9000    memory, just as large structures are always returned.  TYPE will be
9001    the data type of the value, and FNTYPE will be the type of the
9002    function doing the returning, or @code{NULL} for libcalls.
9003 
9004    The AIX ABI for the RS/6000 specifies that all structures are
9005    returned in memory.  The Darwin ABI does the same.
9006 
9007    For the Darwin 64 Bit ABI, a function result can be returned in
9008    registers or in memory, depending on the size of the return data
9009    type.  If it is returned in registers, the value occupies the same
9010    registers as it would if it were the first and only function
9011    argument.  Otherwise, the function places its result in memory at
9012    the location pointed to by GPR3.
9013 
9014    The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
9015    but a draft put them in memory, and GCC used to implement the draft
9016    instead of the final standard.  Therefore, aix_struct_return
9017    controls this instead of DEFAULT_ABI; V.4 targets needing backward
9018    compatibility can change DRAFT_V4_STRUCT_RET to override the
9019    default, and -m switches get the final word.  See
9020    rs6000_option_override_internal for more details.
9021 
9022    The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
9023    long double support is enabled.  These values are returned in memory.
9024 
9025    int_size_in_bytes returns -1 for variable size objects, which go in
9026    memory always.  The cast to unsigned makes -1 > 8.  */
9027 
9028 static bool
rs6000_return_in_memory(const_tree type,const_tree fntype ATTRIBUTE_UNUSED)9029 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
9030 {
9031   /* For the Darwin64 ABI, test if we can fit the return value in regs.  */
9032   if (TARGET_MACHO
9033       && rs6000_darwin64_abi
9034       && TREE_CODE (type) == RECORD_TYPE
9035       && int_size_in_bytes (type) > 0)
9036     {
9037       CUMULATIVE_ARGS valcum;
9038       rtx valret;
9039 
9040       valcum.words = 0;
9041       valcum.fregno = FP_ARG_MIN_REG;
9042       valcum.vregno = ALTIVEC_ARG_MIN_REG;
9043       /* Do a trial code generation as if this were going to be passed
9044 	 as an argument; if any part goes in memory, we return NULL.  */
9045       valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
9046       if (valret)
9047 	return false;
9048       /* Otherwise fall through to more conventional ABI rules.  */
9049     }
9050 
9051   /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
9052   if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
9053 					     NULL, NULL))
9054     return false;
9055 
9056   /* The ELFv2 ABI returns aggregates up to 16B in registers */
9057   if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
9058       && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
9059     return false;
9060 
9061   if (AGGREGATE_TYPE_P (type)
9062       && (aix_struct_return
9063 	  || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
9064     return true;
9065 
9066   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
9067      modes only exist for GCC vector types if -maltivec.  */
9068   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
9069       && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
9070     return false;
9071 
9072   /* Return synthetic vectors in memory.  */
9073   if (TREE_CODE (type) == VECTOR_TYPE
9074       && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
9075     {
9076       static bool warned_for_return_big_vectors = false;
9077       if (!warned_for_return_big_vectors)
9078 	{
9079 	  warning (0, "GCC vector returned by reference: "
9080 		   "non-standard ABI extension with no compatibility guarantee");
9081 	  warned_for_return_big_vectors = true;
9082 	}
9083       return true;
9084     }
9085 
9086   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
9087     return true;
9088 
9089   return false;
9090 }
9091 
9092 /* Specify whether values returned in registers should be at the most
9093    significant end of a register.  We want aggregates returned by
9094    value to match the way aggregates are passed to functions.  */
9095 
9096 static bool
rs6000_return_in_msb(const_tree valtype)9097 rs6000_return_in_msb (const_tree valtype)
9098 {
9099   return (DEFAULT_ABI == ABI_ELFv2
9100 	  && BYTES_BIG_ENDIAN
9101 	  && AGGREGATE_TYPE_P (valtype)
9102 	  && FUNCTION_ARG_PADDING (TYPE_MODE (valtype), valtype) == upward);
9103 }
9104 
9105 #ifdef HAVE_AS_GNU_ATTRIBUTE
9106 /* Return TRUE if a call to function FNDECL may be one that
9107    potentially affects the function calling ABI of the object file.  */
9108 
9109 static bool
call_ABI_of_interest(tree fndecl)9110 call_ABI_of_interest (tree fndecl)
9111 {
9112   if (cgraph_state == CGRAPH_STATE_EXPANSION)
9113     {
9114       struct cgraph_node *c_node;
9115 
9116       /* Libcalls are always interesting.  */
9117       if (fndecl == NULL_TREE)
9118 	return true;
9119 
9120       /* Any call to an external function is interesting.  */
9121       if (DECL_EXTERNAL (fndecl))
9122 	return true;
9123 
9124       /* Interesting functions that we are emitting in this object file.  */
9125       c_node = cgraph_get_node (fndecl);
9126       c_node = cgraph_function_or_thunk_node (c_node, NULL);
9127       return !cgraph_only_called_directly_p (c_node);
9128     }
9129   return false;
9130 }
9131 #endif
9132 
9133 /* Initialize a variable CUM of type CUMULATIVE_ARGS
9134    for a call to a function whose data type is FNTYPE.
9135    For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
9136 
9137    For incoming args we set the number of arguments in the prototype large
9138    so we never return a PARALLEL.  */
9139 
9140 void
init_cumulative_args(CUMULATIVE_ARGS * cum,tree fntype,rtx libname ATTRIBUTE_UNUSED,int incoming,int libcall,int n_named_args,tree fndecl ATTRIBUTE_UNUSED,enum machine_mode return_mode ATTRIBUTE_UNUSED)9141 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
9142 		      rtx libname ATTRIBUTE_UNUSED, int incoming,
9143 		      int libcall, int n_named_args,
9144 		      tree fndecl ATTRIBUTE_UNUSED,
9145 		      enum machine_mode return_mode ATTRIBUTE_UNUSED)
9146 {
9147   static CUMULATIVE_ARGS zero_cumulative;
9148 
9149   *cum = zero_cumulative;
9150   cum->words = 0;
9151   cum->fregno = FP_ARG_MIN_REG;
9152   cum->vregno = ALTIVEC_ARG_MIN_REG;
9153   cum->prototype = (fntype && prototype_p (fntype));
9154   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
9155 		      ? CALL_LIBCALL : CALL_NORMAL);
9156   cum->sysv_gregno = GP_ARG_MIN_REG;
9157   cum->stdarg = stdarg_p (fntype);
9158 
9159   cum->nargs_prototype = 0;
9160   if (incoming || cum->prototype)
9161     cum->nargs_prototype = n_named_args;
9162 
9163   /* Check for a longcall attribute.  */
9164   if ((!fntype && rs6000_default_long_calls)
9165       || (fntype
9166 	  && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
9167 	  && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
9168     cum->call_cookie |= CALL_LONG;
9169 
9170   if (TARGET_DEBUG_ARG)
9171     {
9172       fprintf (stderr, "\ninit_cumulative_args:");
9173       if (fntype)
9174 	{
9175 	  tree ret_type = TREE_TYPE (fntype);
9176 	  fprintf (stderr, " ret code = %s,",
9177 		   tree_code_name[ (int)TREE_CODE (ret_type) ]);
9178 	}
9179 
9180       if (cum->call_cookie & CALL_LONG)
9181 	fprintf (stderr, " longcall,");
9182 
9183       fprintf (stderr, " proto = %d, nargs = %d\n",
9184 	       cum->prototype, cum->nargs_prototype);
9185     }
9186 
9187 #ifdef HAVE_AS_GNU_ATTRIBUTE
9188   if (DEFAULT_ABI == ABI_V4)
9189     {
9190       cum->escapes = call_ABI_of_interest (fndecl);
9191       if (cum->escapes)
9192 	{
9193 	  tree return_type;
9194 
9195 	  if (fntype)
9196 	    {
9197 	      return_type = TREE_TYPE (fntype);
9198 	      return_mode = TYPE_MODE (return_type);
9199 	    }
9200 	  else
9201 	    return_type = lang_hooks.types.type_for_mode (return_mode, 0);
9202 
9203 	  if (return_type != NULL)
9204 	    {
9205 	      if (TREE_CODE (return_type) == RECORD_TYPE
9206 		  && TYPE_TRANSPARENT_AGGR (return_type))
9207 		{
9208 		  return_type = TREE_TYPE (first_field (return_type));
9209 		  return_mode = TYPE_MODE (return_type);
9210 		}
9211 	      if (AGGREGATE_TYPE_P (return_type)
9212 		  && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
9213 		      <= 8))
9214 		rs6000_returns_struct = true;
9215 	    }
9216 	  if (SCALAR_FLOAT_MODE_P (return_mode))
9217 	    rs6000_passes_float = true;
9218 	  else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
9219 		   || SPE_VECTOR_MODE (return_mode))
9220 	    rs6000_passes_vector = true;
9221 	}
9222     }
9223 #endif
9224 
9225   if (fntype
9226       && !TARGET_ALTIVEC
9227       && TARGET_ALTIVEC_ABI
9228       && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
9229     {
9230       error ("cannot return value in vector register because"
9231 	     " altivec instructions are disabled, use -maltivec"
9232 	     " to enable them");
9233     }
9234 }
9235 
9236 /* Return true if TYPE must be passed on the stack and not in registers.  */
9237 
9238 static bool
rs6000_must_pass_in_stack(enum machine_mode mode,const_tree type)9239 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
9240 {
9241   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
9242     return must_pass_in_stack_var_size (mode, type);
9243   else
9244     return must_pass_in_stack_var_size_or_pad (mode, type);
9245 }
9246 
9247 /* If defined, a C expression which determines whether, and in which
9248    direction, to pad out an argument with extra space.  The value
9249    should be of type `enum direction': either `upward' to pad above
9250    the argument, `downward' to pad below, or `none' to inhibit
9251    padding.
9252 
9253    For the AIX ABI structs are always stored left shifted in their
9254    argument slot.  */
9255 
9256 enum direction
function_arg_padding(enum machine_mode mode,const_tree type)9257 function_arg_padding (enum machine_mode mode, const_tree type)
9258 {
9259 #ifndef AGGREGATE_PADDING_FIXED
9260 #define AGGREGATE_PADDING_FIXED 0
9261 #endif
9262 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
9263 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
9264 #endif
9265 
9266   if (!AGGREGATE_PADDING_FIXED)
9267     {
9268       /* GCC used to pass structures of the same size as integer types as
9269 	 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
9270 	 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
9271 	 passed padded downward, except that -mstrict-align further
9272 	 muddied the water in that multi-component structures of 2 and 4
9273 	 bytes in size were passed padded upward.
9274 
9275 	 The following arranges for best compatibility with previous
9276 	 versions of gcc, but removes the -mstrict-align dependency.  */
9277       if (BYTES_BIG_ENDIAN)
9278 	{
9279 	  HOST_WIDE_INT size = 0;
9280 
9281 	  if (mode == BLKmode)
9282 	    {
9283 	      if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9284 		size = int_size_in_bytes (type);
9285 	    }
9286 	  else
9287 	    size = GET_MODE_SIZE (mode);
9288 
9289 	  if (size == 1 || size == 2 || size == 4)
9290 	    return downward;
9291 	}
9292       return upward;
9293     }
9294 
9295   if (AGGREGATES_PAD_UPWARD_ALWAYS)
9296     {
9297       if (type != 0 && AGGREGATE_TYPE_P (type))
9298 	return upward;
9299     }
9300 
9301   /* Fall back to the default.  */
9302   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
9303 }
9304 
9305 /* If defined, a C expression that gives the alignment boundary, in bits,
9306    of an argument with the specified mode and type.  If it is not defined,
9307    PARM_BOUNDARY is used for all arguments.
9308 
9309    V.4 wants long longs and doubles to be double word aligned.  Just
9310    testing the mode size is a boneheaded way to do this as it means
9311    that other types such as complex int are also double word aligned.
9312    However, we're stuck with this because changing the ABI might break
9313    existing library interfaces.
9314 
9315    Doubleword align SPE vectors.
9316    Quadword align Altivec/VSX vectors.
9317    Quadword align large synthetic vector types.   */
9318 
9319 static unsigned int
rs6000_function_arg_boundary(enum machine_mode mode,const_tree type)9320 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
9321 {
9322   enum machine_mode elt_mode;
9323   int n_elts;
9324 
9325   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9326 
9327   if (DEFAULT_ABI == ABI_V4
9328       && (GET_MODE_SIZE (mode) == 8
9329 	  || (TARGET_HARD_FLOAT
9330 	      && TARGET_FPRS
9331 	      && (mode == TFmode || mode == TDmode))))
9332     return 64;
9333   else if (SPE_VECTOR_MODE (mode)
9334 	   || (type && TREE_CODE (type) == VECTOR_TYPE
9335 	       && int_size_in_bytes (type) >= 8
9336 	       && int_size_in_bytes (type) < 16))
9337     return 64;
9338   else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9339 	   || (type && TREE_CODE (type) == VECTOR_TYPE
9340 	       && int_size_in_bytes (type) >= 16))
9341     return 128;
9342 
9343   /* Aggregate types that need > 8 byte alignment are quadword-aligned
9344      in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
9345      -mcompat-align-parm is used.  */
9346   if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
9347        || DEFAULT_ABI == ABI_ELFv2)
9348       && type && TYPE_ALIGN (type) > 64)
9349     {
9350       /* "Aggregate" means any AGGREGATE_TYPE except for single-element
9351          or homogeneous float/vector aggregates here.  We already handled
9352          vector aggregates above, but still need to check for float here. */
9353       bool aggregate_p = (AGGREGATE_TYPE_P (type)
9354 			  && !SCALAR_FLOAT_MODE_P (elt_mode));
9355 
9356       /* We used to check for BLKmode instead of the above aggregate type
9357 	 check.  Warn when this results in any difference to the ABI.  */
9358       if (aggregate_p != (mode == BLKmode))
9359 	{
9360 	  static bool warned;
9361 	  if (!warned && warn_psabi)
9362 	    {
9363 	      warned = true;
9364 	      inform (input_location,
9365 		      "the ABI of passing aggregates with %d-byte alignment"
9366 		      " will change in a future GCC release",
9367 		      (int) TYPE_ALIGN (type) / BITS_PER_UNIT);
9368 	    }
9369 	}
9370 
9371       /* GCC 4.8/4.9 Note: To avoid any ABI change on a release branch, we
9372 	 keep using the BLKmode check, but warn if there will be differences
9373 	 in future GCC releases.  */
9374       if (mode == BLKmode)
9375 	return 128;
9376     }
9377 
9378   /* Similar for the Darwin64 ABI.  Note that for historical reasons we
9379      implement the "aggregate type" check as a BLKmode check here; this
9380      means certain aggregate types are in fact not aligned.  */
9381   if (TARGET_MACHO && rs6000_darwin64_abi
9382       && mode == BLKmode
9383       && type && TYPE_ALIGN (type) > 64)
9384     return 128;
9385 
9386   return PARM_BOUNDARY;
9387 }
9388 
9389 /* The offset in words to the start of the parameter save area.  */
9390 
9391 static unsigned int
rs6000_parm_offset(void)9392 rs6000_parm_offset (void)
9393 {
9394   return (DEFAULT_ABI == ABI_V4 ? 2
9395 	  : DEFAULT_ABI == ABI_ELFv2 ? 4
9396 	  : 6);
9397 }
9398 
9399 /* For a function parm of MODE and TYPE, return the starting word in
9400    the parameter area.  NWORDS of the parameter area are already used.  */
9401 
9402 static unsigned int
rs6000_parm_start(enum machine_mode mode,const_tree type,unsigned int nwords)9403 rs6000_parm_start (enum machine_mode mode, const_tree type,
9404 		   unsigned int nwords)
9405 {
9406   unsigned int align;
9407 
9408   align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
9409   return nwords + (-(rs6000_parm_offset () + nwords) & align);
9410 }
9411 
9412 /* Compute the size (in words) of a function argument.  */
9413 
9414 static unsigned long
rs6000_arg_size(enum machine_mode mode,const_tree type)9415 rs6000_arg_size (enum machine_mode mode, const_tree type)
9416 {
9417   unsigned long size;
9418 
9419   if (mode != BLKmode)
9420     size = GET_MODE_SIZE (mode);
9421   else
9422     size = int_size_in_bytes (type);
9423 
9424   if (TARGET_32BIT)
9425     return (size + 3) >> 2;
9426   else
9427     return (size + 7) >> 3;
9428 }
9429 
9430 /* Use this to flush pending int fields.  */
9431 
9432 static void
rs6000_darwin64_record_arg_advance_flush(CUMULATIVE_ARGS * cum,HOST_WIDE_INT bitpos,int final)9433 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
9434 					  HOST_WIDE_INT bitpos, int final)
9435 {
9436   unsigned int startbit, endbit;
9437   int intregs, intoffset;
9438   enum machine_mode mode;
9439 
9440   /* Handle the situations where a float is taking up the first half
9441      of the GPR, and the other half is empty (typically due to
9442      alignment restrictions). We can detect this by a 8-byte-aligned
9443      int field, or by seeing that this is the final flush for this
9444      argument. Count the word and continue on.  */
9445   if (cum->floats_in_gpr == 1
9446       && (cum->intoffset % 64 == 0
9447 	  || (cum->intoffset == -1 && final)))
9448     {
9449       cum->words++;
9450       cum->floats_in_gpr = 0;
9451     }
9452 
9453   if (cum->intoffset == -1)
9454     return;
9455 
9456   intoffset = cum->intoffset;
9457   cum->intoffset = -1;
9458   cum->floats_in_gpr = 0;
9459 
9460   if (intoffset % BITS_PER_WORD != 0)
9461     {
9462       mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9463 			    MODE_INT, 0);
9464       if (mode == BLKmode)
9465 	{
9466 	  /* We couldn't find an appropriate mode, which happens,
9467 	     e.g., in packed structs when there are 3 bytes to load.
9468 	     Back intoffset back to the beginning of the word in this
9469 	     case.  */
9470 	  intoffset = intoffset & -BITS_PER_WORD;
9471 	}
9472     }
9473 
9474   startbit = intoffset & -BITS_PER_WORD;
9475   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9476   intregs = (endbit - startbit) / BITS_PER_WORD;
9477   cum->words += intregs;
9478   /* words should be unsigned. */
9479   if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
9480     {
9481       int pad = (endbit/BITS_PER_WORD) - cum->words;
9482       cum->words += pad;
9483     }
9484 }
9485 
9486 /* The darwin64 ABI calls for us to recurse down through structs,
9487    looking for elements passed in registers.  Unfortunately, we have
9488    to track int register count here also because of misalignments
9489    in powerpc alignment mode.  */
9490 
9491 static void
rs6000_darwin64_record_arg_advance_recurse(CUMULATIVE_ARGS * cum,const_tree type,HOST_WIDE_INT startbitpos)9492 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
9493 					    const_tree type,
9494 					    HOST_WIDE_INT startbitpos)
9495 {
9496   tree f;
9497 
9498   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9499     if (TREE_CODE (f) == FIELD_DECL)
9500       {
9501 	HOST_WIDE_INT bitpos = startbitpos;
9502 	tree ftype = TREE_TYPE (f);
9503 	enum machine_mode mode;
9504 	if (ftype == error_mark_node)
9505 	  continue;
9506 	mode = TYPE_MODE (ftype);
9507 
9508 	if (DECL_SIZE (f) != 0
9509 	    && host_integerp (bit_position (f), 1))
9510 	  bitpos += int_bit_position (f);
9511 
9512 	/* ??? FIXME: else assume zero offset.  */
9513 
9514 	if (TREE_CODE (ftype) == RECORD_TYPE)
9515 	  rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
9516 	else if (USE_FP_FOR_ARG_P (cum, mode))
9517 	  {
9518 	    unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
9519 	    rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9520 	    cum->fregno += n_fpregs;
9521 	    /* Single-precision floats present a special problem for
9522 	       us, because they are smaller than an 8-byte GPR, and so
9523 	       the structure-packing rules combined with the standard
9524 	       varargs behavior mean that we want to pack float/float
9525 	       and float/int combinations into a single register's
9526 	       space. This is complicated by the arg advance flushing,
9527 	       which works on arbitrarily large groups of int-type
9528 	       fields.  */
9529 	    if (mode == SFmode)
9530 	      {
9531 		if (cum->floats_in_gpr == 1)
9532 		  {
9533 		    /* Two floats in a word; count the word and reset
9534 		       the float count.  */
9535 		    cum->words++;
9536 		    cum->floats_in_gpr = 0;
9537 		  }
9538 		else if (bitpos % 64 == 0)
9539 		  {
9540 		    /* A float at the beginning of an 8-byte word;
9541 		       count it and put off adjusting cum->words until
9542 		       we see if a arg advance flush is going to do it
9543 		       for us.  */
9544 		    cum->floats_in_gpr++;
9545 		  }
9546 		else
9547 		  {
9548 		    /* The float is at the end of a word, preceded
9549 		       by integer fields, so the arg advance flush
9550 		       just above has already set cum->words and
9551 		       everything is taken care of.  */
9552 		  }
9553 	      }
9554 	    else
9555 	      cum->words += n_fpregs;
9556 	  }
9557 	else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9558 	  {
9559 	    rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9560 	    cum->vregno++;
9561 	    cum->words += 2;
9562 	  }
9563 	else if (cum->intoffset == -1)
9564 	  cum->intoffset = bitpos;
9565       }
9566 }
9567 
9568 /* Check for an item that needs to be considered specially under the darwin 64
9569    bit ABI.  These are record types where the mode is BLK or the structure is
9570    8 bytes in size.  */
9571 static int
rs6000_darwin64_struct_check_p(enum machine_mode mode,const_tree type)9572 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
9573 {
9574   return rs6000_darwin64_abi
9575 	 && ((mode == BLKmode
9576 	      && TREE_CODE (type) == RECORD_TYPE
9577 	      && int_size_in_bytes (type) > 0)
9578 	  || (type && TREE_CODE (type) == RECORD_TYPE
9579 	      && int_size_in_bytes (type) == 8)) ? 1 : 0;
9580 }
9581 
9582 /* Update the data in CUM to advance over an argument
9583    of mode MODE and data type TYPE.
9584    (TYPE is null for libcalls where that information may not be available.)
9585 
9586    Note that for args passed by reference, function_arg will be called
9587    with MODE and TYPE set to that of the pointer to the arg, not the arg
9588    itself.  */
9589 
9590 static void
rs6000_function_arg_advance_1(CUMULATIVE_ARGS * cum,enum machine_mode mode,const_tree type,bool named,int depth)9591 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
9592 			       const_tree type, bool named, int depth)
9593 {
9594   enum machine_mode elt_mode;
9595   int n_elts;
9596 
9597   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9598 
9599   /* Only tick off an argument if we're not recursing.  */
9600   if (depth == 0)
9601     cum->nargs_prototype--;
9602 
9603 #ifdef HAVE_AS_GNU_ATTRIBUTE
9604   if (DEFAULT_ABI == ABI_V4
9605       && cum->escapes)
9606     {
9607       if (SCALAR_FLOAT_MODE_P (mode))
9608 	rs6000_passes_float = true;
9609       else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
9610 	rs6000_passes_vector = true;
9611       else if (SPE_VECTOR_MODE (mode)
9612 	       && !cum->stdarg
9613 	       && cum->sysv_gregno <= GP_ARG_MAX_REG)
9614 	rs6000_passes_vector = true;
9615     }
9616 #endif
9617 
9618   if (TARGET_ALTIVEC_ABI
9619       && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9620 	  || (type && TREE_CODE (type) == VECTOR_TYPE
9621 	      && int_size_in_bytes (type) == 16)))
9622     {
9623       bool stack = false;
9624 
9625       if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
9626 	{
9627 	  cum->vregno += n_elts;
9628 
9629 	  if (!TARGET_ALTIVEC)
9630 	    error ("cannot pass argument in vector register because"
9631 		   " altivec instructions are disabled, use -maltivec"
9632 		   " to enable them");
9633 
9634 	  /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
9635 	     even if it is going to be passed in a vector register.
9636 	     Darwin does the same for variable-argument functions.  */
9637 	  if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9638 	       && TARGET_64BIT)
9639 	      || (cum->stdarg && DEFAULT_ABI != ABI_V4))
9640 	    stack = true;
9641 	}
9642       else
9643 	stack = true;
9644 
9645       if (stack)
9646 	{
9647 	  int align;
9648 
9649 	  /* Vector parameters must be 16-byte aligned.  In 32-bit
9650 	     mode this means we need to take into account the offset
9651 	     to the parameter save area.  In 64-bit mode, they just
9652 	     have to start on an even word, since the parameter save
9653 	     area is 16-byte aligned.  */
9654 	  if (TARGET_32BIT)
9655 	    align = -(rs6000_parm_offset () + cum->words) & 3;
9656 	  else
9657 	    align = cum->words & 1;
9658 	  cum->words += align + rs6000_arg_size (mode, type);
9659 
9660 	  if (TARGET_DEBUG_ARG)
9661 	    {
9662 	      fprintf (stderr, "function_adv: words = %2d, align=%d, ",
9663 		       cum->words, align);
9664 	      fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
9665 		       cum->nargs_prototype, cum->prototype,
9666 		       GET_MODE_NAME (mode));
9667 	    }
9668 	}
9669     }
9670   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
9671 	   && !cum->stdarg
9672 	   && cum->sysv_gregno <= GP_ARG_MAX_REG)
9673     cum->sysv_gregno++;
9674 
9675   else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9676     {
9677       int size = int_size_in_bytes (type);
9678       /* Variable sized types have size == -1 and are
9679 	 treated as if consisting entirely of ints.
9680 	 Pad to 16 byte boundary if needed.  */
9681       if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9682 	  && (cum->words % 2) != 0)
9683 	cum->words++;
9684       /* For varargs, we can just go up by the size of the struct. */
9685       if (!named)
9686 	cum->words += (size + 7) / 8;
9687       else
9688 	{
9689 	  /* It is tempting to say int register count just goes up by
9690 	     sizeof(type)/8, but this is wrong in a case such as
9691 	     { int; double; int; } [powerpc alignment].  We have to
9692 	     grovel through the fields for these too.  */
9693 	  cum->intoffset = 0;
9694 	  cum->floats_in_gpr = 0;
9695 	  rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
9696 	  rs6000_darwin64_record_arg_advance_flush (cum,
9697 						    size * BITS_PER_UNIT, 1);
9698 	}
9699 	  if (TARGET_DEBUG_ARG)
9700 	    {
9701 	      fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
9702 		       cum->words, TYPE_ALIGN (type), size);
9703 	      fprintf (stderr,
9704 	           "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
9705 		       cum->nargs_prototype, cum->prototype,
9706 		       GET_MODE_NAME (mode));
9707 	    }
9708     }
9709   else if (DEFAULT_ABI == ABI_V4)
9710     {
9711       if (TARGET_HARD_FLOAT && TARGET_FPRS
9712 	  && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9713 	      || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9714 	      || (mode == TFmode && !TARGET_IEEEQUAD)
9715 	      || mode == SDmode || mode == DDmode || mode == TDmode))
9716 	{
9717 	  /* _Decimal128 must use an even/odd register pair.  This assumes
9718 	     that the register number is odd when fregno is odd.  */
9719 	  if (mode == TDmode && (cum->fregno % 2) == 1)
9720 	    cum->fregno++;
9721 
9722 	  if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9723 	      <= FP_ARG_V4_MAX_REG)
9724 	    cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
9725 	  else
9726 	    {
9727 	      cum->fregno = FP_ARG_V4_MAX_REG + 1;
9728 	      if (mode == DFmode || mode == TFmode
9729 		  || mode == DDmode || mode == TDmode)
9730 		cum->words += cum->words & 1;
9731 	      cum->words += rs6000_arg_size (mode, type);
9732 	    }
9733 	}
9734       else
9735 	{
9736 	  int n_words = rs6000_arg_size (mode, type);
9737 	  int gregno = cum->sysv_gregno;
9738 
9739 	  /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9740 	     (r7,r8) or (r9,r10).  As does any other 2 word item such
9741 	     as complex int due to a historical mistake.  */
9742 	  if (n_words == 2)
9743 	    gregno += (1 - gregno) & 1;
9744 
9745 	  /* Multi-reg args are not split between registers and stack.  */
9746 	  if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9747 	    {
9748 	      /* Long long and SPE vectors are aligned on the stack.
9749 		 So are other 2 word items such as complex int due to
9750 		 a historical mistake.  */
9751 	      if (n_words == 2)
9752 		cum->words += cum->words & 1;
9753 	      cum->words += n_words;
9754 	    }
9755 
9756 	  /* Note: continuing to accumulate gregno past when we've started
9757 	     spilling to the stack indicates the fact that we've started
9758 	     spilling to the stack to expand_builtin_saveregs.  */
9759 	  cum->sysv_gregno = gregno + n_words;
9760 	}
9761 
9762       if (TARGET_DEBUG_ARG)
9763 	{
9764 	  fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9765 		   cum->words, cum->fregno);
9766 	  fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
9767 		   cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
9768 	  fprintf (stderr, "mode = %4s, named = %d\n",
9769 		   GET_MODE_NAME (mode), named);
9770 	}
9771     }
9772   else
9773     {
9774       int n_words = rs6000_arg_size (mode, type);
9775       int start_words = cum->words;
9776       int align_words = rs6000_parm_start (mode, type, start_words);
9777 
9778       cum->words = align_words + n_words;
9779 
9780       if (SCALAR_FLOAT_MODE_P (elt_mode)
9781 	  && TARGET_HARD_FLOAT && TARGET_FPRS)
9782 	{
9783 	  /* _Decimal128 must be passed in an even/odd float register pair.
9784 	     This assumes that the register number is odd when fregno is
9785 	     odd.  */
9786 	  if (elt_mode == TDmode && (cum->fregno % 2) == 1)
9787 	    cum->fregno++;
9788 	  cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
9789 	}
9790 
9791       if (TARGET_DEBUG_ARG)
9792 	{
9793 	  fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9794 		   cum->words, cum->fregno);
9795 	  fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
9796 		   cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
9797 	  fprintf (stderr, "named = %d, align = %d, depth = %d\n",
9798 		   named, align_words - start_words, depth);
9799 	}
9800     }
9801 }
9802 
9803 static void
rs6000_function_arg_advance(cumulative_args_t cum,enum machine_mode mode,const_tree type,bool named)9804 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
9805 			     const_tree type, bool named)
9806 {
9807   rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
9808 				 0);
9809 }
9810 
9811 static rtx
spe_build_register_parallel(enum machine_mode mode,int gregno)9812 spe_build_register_parallel (enum machine_mode mode, int gregno)
9813 {
9814   rtx r1, r3, r5, r7;
9815 
9816   switch (mode)
9817     {
9818     case DFmode:
9819       r1 = gen_rtx_REG (DImode, gregno);
9820       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9821       return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
9822 
9823     case DCmode:
9824     case TFmode:
9825       r1 = gen_rtx_REG (DImode, gregno);
9826       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9827       r3 = gen_rtx_REG (DImode, gregno + 2);
9828       r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9829       return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
9830 
9831     case TCmode:
9832       r1 = gen_rtx_REG (DImode, gregno);
9833       r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9834       r3 = gen_rtx_REG (DImode, gregno + 2);
9835       r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9836       r5 = gen_rtx_REG (DImode, gregno + 4);
9837       r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
9838       r7 = gen_rtx_REG (DImode, gregno + 6);
9839       r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
9840       return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
9841 
9842     default:
9843       gcc_unreachable ();
9844     }
9845 }
9846 
9847 /* Determine where to put a SIMD argument on the SPE.  */
9848 static rtx
rs6000_spe_function_arg(const CUMULATIVE_ARGS * cum,enum machine_mode mode,const_tree type)9849 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
9850 			 const_tree type)
9851 {
9852   int gregno = cum->sysv_gregno;
9853 
9854   /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
9855      are passed and returned in a pair of GPRs for ABI compatibility.  */
9856   if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
9857 			     || mode == DCmode || mode == TCmode))
9858     {
9859       int n_words = rs6000_arg_size (mode, type);
9860 
9861       /* Doubles go in an odd/even register pair (r5/r6, etc).  */
9862       if (mode == DFmode)
9863 	gregno += (1 - gregno) & 1;
9864 
9865       /* Multi-reg args are not split between registers and stack.  */
9866       if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9867 	return NULL_RTX;
9868 
9869       return spe_build_register_parallel (mode, gregno);
9870     }
9871   if (cum->stdarg)
9872     {
9873       int n_words = rs6000_arg_size (mode, type);
9874 
9875       /* SPE vectors are put in odd registers.  */
9876       if (n_words == 2 && (gregno & 1) == 0)
9877 	gregno += 1;
9878 
9879       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
9880 	{
9881 	  rtx r1, r2;
9882 	  enum machine_mode m = SImode;
9883 
9884 	  r1 = gen_rtx_REG (m, gregno);
9885 	  r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
9886 	  r2 = gen_rtx_REG (m, gregno + 1);
9887 	  r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
9888 	  return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
9889 	}
9890       else
9891 	return NULL_RTX;
9892     }
9893   else
9894     {
9895       if (gregno <= GP_ARG_MAX_REG)
9896 	return gen_rtx_REG (mode, gregno);
9897       else
9898 	return NULL_RTX;
9899     }
9900 }
9901 
9902 /* A subroutine of rs6000_darwin64_record_arg.  Assign the bits of the
9903    structure between cum->intoffset and bitpos to integer registers.  */
9904 
9905 static void
rs6000_darwin64_record_arg_flush(CUMULATIVE_ARGS * cum,HOST_WIDE_INT bitpos,rtx rvec[],int * k)9906 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
9907 				  HOST_WIDE_INT bitpos, rtx rvec[], int *k)
9908 {
9909   enum machine_mode mode;
9910   unsigned int regno;
9911   unsigned int startbit, endbit;
9912   int this_regno, intregs, intoffset;
9913   rtx reg;
9914 
9915   if (cum->intoffset == -1)
9916     return;
9917 
9918   intoffset = cum->intoffset;
9919   cum->intoffset = -1;
9920 
9921   /* If this is the trailing part of a word, try to only load that
9922      much into the register.  Otherwise load the whole register.  Note
9923      that in the latter case we may pick up unwanted bits.  It's not a
9924      problem at the moment but may wish to revisit.  */
9925 
9926   if (intoffset % BITS_PER_WORD != 0)
9927     {
9928       mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9929 			  MODE_INT, 0);
9930       if (mode == BLKmode)
9931 	{
9932 	  /* We couldn't find an appropriate mode, which happens,
9933 	     e.g., in packed structs when there are 3 bytes to load.
9934 	     Back intoffset back to the beginning of the word in this
9935 	     case.  */
9936 	 intoffset = intoffset & -BITS_PER_WORD;
9937 	 mode = word_mode;
9938 	}
9939     }
9940   else
9941     mode = word_mode;
9942 
9943   startbit = intoffset & -BITS_PER_WORD;
9944   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9945   intregs = (endbit - startbit) / BITS_PER_WORD;
9946   this_regno = cum->words + intoffset / BITS_PER_WORD;
9947 
9948   if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
9949     cum->use_stack = 1;
9950 
9951   intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
9952   if (intregs <= 0)
9953     return;
9954 
9955   intoffset /= BITS_PER_UNIT;
9956   do
9957     {
9958       regno = GP_ARG_MIN_REG + this_regno;
9959       reg = gen_rtx_REG (mode, regno);
9960       rvec[(*k)++] =
9961 	gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
9962 
9963       this_regno += 1;
9964       intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
9965       mode = word_mode;
9966       intregs -= 1;
9967     }
9968   while (intregs > 0);
9969 }
9970 
9971 /* Recursive workhorse for the following.  */
9972 
9973 static void
rs6000_darwin64_record_arg_recurse(CUMULATIVE_ARGS * cum,const_tree type,HOST_WIDE_INT startbitpos,rtx rvec[],int * k)9974 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
9975 				    HOST_WIDE_INT startbitpos, rtx rvec[],
9976 				    int *k)
9977 {
9978   tree f;
9979 
9980   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9981     if (TREE_CODE (f) == FIELD_DECL)
9982       {
9983 	HOST_WIDE_INT bitpos = startbitpos;
9984 	tree ftype = TREE_TYPE (f);
9985 	enum machine_mode mode;
9986 	if (ftype == error_mark_node)
9987 	  continue;
9988 	mode = TYPE_MODE (ftype);
9989 
9990 	if (DECL_SIZE (f) != 0
9991 	    && host_integerp (bit_position (f), 1))
9992 	  bitpos += int_bit_position (f);
9993 
9994 	/* ??? FIXME: else assume zero offset.  */
9995 
9996 	if (TREE_CODE (ftype) == RECORD_TYPE)
9997 	  rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
9998 	else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
9999 	  {
10000 	    unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
10001 #if 0
10002 	    switch (mode)
10003 	      {
10004 	      case SCmode: mode = SFmode; break;
10005 	      case DCmode: mode = DFmode; break;
10006 	      case TCmode: mode = TFmode; break;
10007 	      default: break;
10008 	      }
10009 #endif
10010 	    rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
10011 	    if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
10012 	      {
10013 		gcc_assert (cum->fregno == FP_ARG_MAX_REG
10014 			    && (mode == TFmode || mode == TDmode));
10015 		/* Long double or _Decimal128 split over regs and memory.  */
10016 		mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
10017 		cum->use_stack=1;
10018 	      }
10019 	    rvec[(*k)++]
10020 	      = gen_rtx_EXPR_LIST (VOIDmode,
10021 				   gen_rtx_REG (mode, cum->fregno++),
10022 				   GEN_INT (bitpos / BITS_PER_UNIT));
10023 	    if (mode == TFmode || mode == TDmode)
10024 	      cum->fregno++;
10025 	  }
10026 	else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
10027 	  {
10028 	    rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
10029 	    rvec[(*k)++]
10030 	      = gen_rtx_EXPR_LIST (VOIDmode,
10031 				   gen_rtx_REG (mode, cum->vregno++),
10032 				   GEN_INT (bitpos / BITS_PER_UNIT));
10033 	  }
10034 	else if (cum->intoffset == -1)
10035 	  cum->intoffset = bitpos;
10036       }
10037 }
10038 
10039 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
10040    the register(s) to be used for each field and subfield of a struct
10041    being passed by value, along with the offset of where the
10042    register's value may be found in the block.  FP fields go in FP
10043    register, vector fields go in vector registers, and everything
10044    else goes in int registers, packed as in memory.
10045 
10046    This code is also used for function return values.  RETVAL indicates
10047    whether this is the case.
10048 
10049    Much of this is taken from the SPARC V9 port, which has a similar
10050    calling convention.  */
10051 
10052 static rtx
rs6000_darwin64_record_arg(CUMULATIVE_ARGS * orig_cum,const_tree type,bool named,bool retval)10053 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
10054 			    bool named, bool retval)
10055 {
10056   rtx rvec[FIRST_PSEUDO_REGISTER];
10057   int k = 1, kbase = 1;
10058   HOST_WIDE_INT typesize = int_size_in_bytes (type);
10059   /* This is a copy; modifications are not visible to our caller.  */
10060   CUMULATIVE_ARGS copy_cum = *orig_cum;
10061   CUMULATIVE_ARGS *cum = &copy_cum;
10062 
10063   /* Pad to 16 byte boundary if needed.  */
10064   if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
10065       && (cum->words % 2) != 0)
10066     cum->words++;
10067 
10068   cum->intoffset = 0;
10069   cum->use_stack = 0;
10070   cum->named = named;
10071 
10072   /* Put entries into rvec[] for individual FP and vector fields, and
10073      for the chunks of memory that go in int regs.  Note we start at
10074      element 1; 0 is reserved for an indication of using memory, and
10075      may or may not be filled in below. */
10076   rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
10077   rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
10078 
10079   /* If any part of the struct went on the stack put all of it there.
10080      This hack is because the generic code for
10081      FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
10082      parts of the struct are not at the beginning.  */
10083   if (cum->use_stack)
10084     {
10085       if (retval)
10086 	return NULL_RTX;    /* doesn't go in registers at all */
10087       kbase = 0;
10088       rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10089     }
10090   if (k > 1 || cum->use_stack)
10091     return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
10092   else
10093     return NULL_RTX;
10094 }
10095 
10096 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
10097 
10098 static rtx
rs6000_mixed_function_arg(enum machine_mode mode,const_tree type,int align_words)10099 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
10100 			   int align_words)
10101 {
10102   int n_units;
10103   int i, k;
10104   rtx rvec[GP_ARG_NUM_REG + 1];
10105 
10106   if (align_words >= GP_ARG_NUM_REG)
10107     return NULL_RTX;
10108 
10109   n_units = rs6000_arg_size (mode, type);
10110 
10111   /* Optimize the simple case where the arg fits in one gpr, except in
10112      the case of BLKmode due to assign_parms assuming that registers are
10113      BITS_PER_WORD wide.  */
10114   if (n_units == 0
10115       || (n_units == 1 && mode != BLKmode))
10116     return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10117 
10118   k = 0;
10119   if (align_words + n_units > GP_ARG_NUM_REG)
10120     /* Not all of the arg fits in gprs.  Say that it goes in memory too,
10121        using a magic NULL_RTX component.
10122        This is not strictly correct.  Only some of the arg belongs in
10123        memory, not all of it.  However, the normal scheme using
10124        function_arg_partial_nregs can result in unusual subregs, eg.
10125        (subreg:SI (reg:DF) 4), which are not handled well.  The code to
10126        store the whole arg to memory is often more efficient than code
10127        to store pieces, and we know that space is available in the right
10128        place for the whole arg.  */
10129     rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10130 
10131   i = 0;
10132   do
10133     {
10134       rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
10135       rtx off = GEN_INT (i++ * 4);
10136       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10137     }
10138   while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
10139 
10140   return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
10141 }
10142 
10143 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
10144    but must also be copied into the parameter save area starting at
10145    offset ALIGN_WORDS.  Fill in RVEC with the elements corresponding
10146    to the GPRs and/or memory.  Return the number of elements used.  */
10147 
10148 static int
rs6000_psave_function_arg(enum machine_mode mode,const_tree type,int align_words,rtx * rvec)10149 rs6000_psave_function_arg (enum machine_mode mode, const_tree type,
10150 			   int align_words, rtx *rvec)
10151 {
10152   int k = 0;
10153 
10154   if (align_words < GP_ARG_NUM_REG)
10155     {
10156       int n_words = rs6000_arg_size (mode, type);
10157 
10158       if (align_words + n_words > GP_ARG_NUM_REG
10159 	  || mode == BLKmode
10160 	  || (TARGET_32BIT && TARGET_POWERPC64))
10161 	{
10162 	  /* If this is partially on the stack, then we only
10163 	     include the portion actually in registers here.  */
10164 	  enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
10165 	  int i = 0;
10166 
10167 	  if (align_words + n_words > GP_ARG_NUM_REG)
10168 	    {
10169 	      /* Not all of the arg fits in gprs.  Say that it goes in memory
10170 		 too, using a magic NULL_RTX component.  Also see comment in
10171 		 rs6000_mixed_function_arg for why the normal
10172 		 function_arg_partial_nregs scheme doesn't work in this case. */
10173 	      rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10174 	    }
10175 
10176 	  do
10177 	    {
10178 	      rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
10179 	      rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
10180 	      rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10181 	    }
10182 	  while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
10183 	}
10184       else
10185 	{
10186 	  /* The whole arg fits in gprs.  */
10187 	  rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10188 	  rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
10189 	}
10190     }
10191   else
10192     {
10193       /* It's entirely in memory.  */
10194       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10195     }
10196 
10197   return k;
10198 }
10199 
10200 /* RVEC is a vector of K components of an argument of mode MODE.
10201    Construct the final function_arg return value from it.  */
10202 
10203 static rtx
rs6000_finish_function_arg(enum machine_mode mode,rtx * rvec,int k)10204 rs6000_finish_function_arg (enum machine_mode mode, rtx *rvec, int k)
10205 {
10206   gcc_assert (k >= 1);
10207 
10208   /* Avoid returning a PARALLEL in the trivial cases.  */
10209   if (k == 1)
10210     {
10211       if (XEXP (rvec[0], 0) == NULL_RTX)
10212 	return NULL_RTX;
10213 
10214       if (GET_MODE (XEXP (rvec[0], 0)) == mode)
10215 	return XEXP (rvec[0], 0);
10216     }
10217 
10218   return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
10219 }
10220 
10221 /* Determine where to put an argument to a function.
10222    Value is zero to push the argument on the stack,
10223    or a hard register in which to store the argument.
10224 
10225    MODE is the argument's machine mode.
10226    TYPE is the data type of the argument (as a tree).
10227     This is null for libcalls where that information may
10228     not be available.
10229    CUM is a variable of type CUMULATIVE_ARGS which gives info about
10230     the preceding args and about the function being called.  It is
10231     not modified in this routine.
10232    NAMED is nonzero if this argument is a named parameter
10233     (otherwise it is an extra parameter matching an ellipsis).
10234 
10235    On RS/6000 the first eight words of non-FP are normally in registers
10236    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
10237    Under V.4, the first 8 FP args are in registers.
10238 
10239    If this is floating-point and no prototype is specified, we use
10240    both an FP and integer register (or possibly FP reg and stack).  Library
10241    functions (when CALL_LIBCALL is set) always have the proper types for args,
10242    so we can pass the FP value just in one register.  emit_library_function
10243    doesn't support PARALLEL anyway.
10244 
10245    Note that for args passed by reference, function_arg will be called
10246    with MODE and TYPE set to that of the pointer to the arg, not the arg
10247    itself.  */
10248 
10249 static rtx
rs6000_function_arg(cumulative_args_t cum_v,enum machine_mode mode,const_tree type,bool named)10250 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
10251 		     const_tree type, bool named)
10252 {
10253   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10254   enum rs6000_abi abi = DEFAULT_ABI;
10255   enum machine_mode elt_mode;
10256   int n_elts;
10257 
10258   /* Return a marker to indicate whether CR1 needs to set or clear the
10259      bit that V.4 uses to say fp args were passed in registers.
10260      Assume that we don't need the marker for software floating point,
10261      or compiler generated library calls.  */
10262   if (mode == VOIDmode)
10263     {
10264       if (abi == ABI_V4
10265 	  && (cum->call_cookie & CALL_LIBCALL) == 0
10266 	  && (cum->stdarg
10267 	      || (cum->nargs_prototype < 0
10268 		  && (cum->prototype || TARGET_NO_PROTOTYPE))))
10269 	{
10270 	  /* For the SPE, we need to crxor CR6 always.  */
10271 	  if (TARGET_SPE_ABI)
10272 	    return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
10273 	  else if (TARGET_HARD_FLOAT && TARGET_FPRS)
10274 	    return GEN_INT (cum->call_cookie
10275 			    | ((cum->fregno == FP_ARG_MIN_REG)
10276 			       ? CALL_V4_SET_FP_ARGS
10277 			       : CALL_V4_CLEAR_FP_ARGS));
10278 	}
10279 
10280       return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
10281     }
10282 
10283   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10284 
10285   if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10286     {
10287       rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
10288       if (rslt != NULL_RTX)
10289 	return rslt;
10290       /* Else fall through to usual handling.  */
10291     }
10292 
10293   if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10294     {
10295       rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10296       rtx r, off;
10297       int i, k = 0;
10298 
10299       /* Do we also need to pass this argument in the parameter
10300 	 save area?  */
10301       if (TARGET_64BIT && ! cum->prototype)
10302 	{
10303 	  int align_words = (cum->words + 1) & ~1;
10304 	  k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10305 	}
10306 
10307       /* Describe where this argument goes in the vector registers.  */
10308       for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
10309 	{
10310 	  r = gen_rtx_REG (elt_mode, cum->vregno + i);
10311 	  off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10312 	  rvec[k++] =  gen_rtx_EXPR_LIST (VOIDmode, r, off);
10313 	}
10314 
10315       return rs6000_finish_function_arg (mode, rvec, k);
10316     }
10317   else if (TARGET_ALTIVEC_ABI
10318 	   && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
10319 	       || (type && TREE_CODE (type) == VECTOR_TYPE
10320 		   && int_size_in_bytes (type) == 16)))
10321     {
10322       if (named || abi == ABI_V4)
10323 	return NULL_RTX;
10324       else
10325 	{
10326 	  /* Vector parameters to varargs functions under AIX or Darwin
10327 	     get passed in memory and possibly also in GPRs.  */
10328 	  int align, align_words, n_words;
10329 	  enum machine_mode part_mode;
10330 
10331 	  /* Vector parameters must be 16-byte aligned.  In 32-bit
10332 	     mode this means we need to take into account the offset
10333 	     to the parameter save area.  In 64-bit mode, they just
10334 	     have to start on an even word, since the parameter save
10335 	     area is 16-byte aligned.  */
10336 	  if (TARGET_32BIT)
10337 	    align = -(rs6000_parm_offset () + cum->words) & 3;
10338 	  else
10339 	    align = cum->words & 1;
10340 	  align_words = cum->words + align;
10341 
10342 	  /* Out of registers?  Memory, then.  */
10343 	  if (align_words >= GP_ARG_NUM_REG)
10344 	    return NULL_RTX;
10345 
10346 	  if (TARGET_32BIT && TARGET_POWERPC64)
10347 	    return rs6000_mixed_function_arg (mode, type, align_words);
10348 
10349 	  /* The vector value goes in GPRs.  Only the part of the
10350 	     value in GPRs is reported here.  */
10351 	  part_mode = mode;
10352 	  n_words = rs6000_arg_size (mode, type);
10353 	  if (align_words + n_words > GP_ARG_NUM_REG)
10354 	    /* Fortunately, there are only two possibilities, the value
10355 	       is either wholly in GPRs or half in GPRs and half not.  */
10356 	    part_mode = DImode;
10357 
10358 	  return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
10359 	}
10360     }
10361   else if (TARGET_SPE_ABI && TARGET_SPE
10362 	   && (SPE_VECTOR_MODE (mode)
10363 	       || (TARGET_E500_DOUBLE && (mode == DFmode
10364 					  || mode == DCmode
10365 					  || mode == TFmode
10366 					  || mode == TCmode))))
10367     return rs6000_spe_function_arg (cum, mode, type);
10368 
10369   else if (abi == ABI_V4)
10370     {
10371       if (TARGET_HARD_FLOAT && TARGET_FPRS
10372 	  && ((TARGET_SINGLE_FLOAT && mode == SFmode)
10373 	      || (TARGET_DOUBLE_FLOAT && mode == DFmode)
10374 	      || (mode == TFmode && !TARGET_IEEEQUAD)
10375 	      || mode == SDmode || mode == DDmode || mode == TDmode))
10376 	{
10377 	  /* _Decimal128 must use an even/odd register pair.  This assumes
10378 	     that the register number is odd when fregno is odd.  */
10379 	  if (mode == TDmode && (cum->fregno % 2) == 1)
10380 	    cum->fregno++;
10381 
10382 	  if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
10383 	      <= FP_ARG_V4_MAX_REG)
10384 	    return gen_rtx_REG (mode, cum->fregno);
10385 	  else
10386 	    return NULL_RTX;
10387 	}
10388       else
10389 	{
10390 	  int n_words = rs6000_arg_size (mode, type);
10391 	  int gregno = cum->sysv_gregno;
10392 
10393 	  /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
10394 	     (r7,r8) or (r9,r10).  As does any other 2 word item such
10395 	     as complex int due to a historical mistake.  */
10396 	  if (n_words == 2)
10397 	    gregno += (1 - gregno) & 1;
10398 
10399 	  /* Multi-reg args are not split between registers and stack.  */
10400 	  if (gregno + n_words - 1 > GP_ARG_MAX_REG)
10401 	    return NULL_RTX;
10402 
10403 	  if (TARGET_32BIT && TARGET_POWERPC64)
10404 	    return rs6000_mixed_function_arg (mode, type,
10405 					      gregno - GP_ARG_MIN_REG);
10406 	  return gen_rtx_REG (mode, gregno);
10407 	}
10408     }
10409   else
10410     {
10411       int align_words = rs6000_parm_start (mode, type, cum->words);
10412 
10413       /* _Decimal128 must be passed in an even/odd float register pair.
10414 	 This assumes that the register number is odd when fregno is odd.  */
10415       if (elt_mode == TDmode && (cum->fregno % 2) == 1)
10416 	cum->fregno++;
10417 
10418       if (USE_FP_FOR_ARG_P (cum, elt_mode))
10419 	{
10420 	  rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10421 	  rtx r, off;
10422 	  int i, k = 0;
10423 	  unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10424 	  int fpr_words;
10425 
10426 	  /* Do we also need to pass this argument in the parameter
10427 	     save area?  */
10428 	  if (type && (cum->nargs_prototype <= 0
10429 		       || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10430 			   && TARGET_XL_COMPAT
10431 			   && align_words >= GP_ARG_NUM_REG)))
10432 	    k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10433 
10434 	  /* Describe where this argument goes in the fprs.  */
10435 	  for (i = 0; i < n_elts
10436 		      && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
10437 	    {
10438 	      /* Check if the argument is split over registers and memory.
10439 		 This can only ever happen for long double or _Decimal128;
10440 		 complex types are handled via split_complex_arg.  */
10441 	      enum machine_mode fmode = elt_mode;
10442 	      if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
10443 		{
10444 		  gcc_assert (fmode == TFmode || fmode == TDmode);
10445 		  fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
10446 		}
10447 
10448 	      r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
10449 	      off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10450 	      rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10451 	    }
10452 
10453 	  /* If there were not enough FPRs to hold the argument, the rest
10454 	     usually goes into memory.  However, if the current position
10455 	     is still within the register parameter area, a portion may
10456 	     actually have to go into GPRs.
10457 
10458 	     Note that it may happen that the portion of the argument
10459 	     passed in the first "half" of the first GPR was already
10460 	     passed in the last FPR as well.
10461 
10462 	     For unnamed arguments, we already set up GPRs to cover the
10463 	     whole argument in rs6000_psave_function_arg, so there is
10464 	     nothing further to do at this point.
10465 
10466 	     GCC 4.8/4.9 Note: This was implemented incorrectly in earlier
10467 	     GCC releases.  To avoid any ABI change on the release branch,
10468 	     we retain that original implementation here, but warn if we
10469 	     encounter a case where the ABI will change in the future.  */
10470 	  fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
10471 	  if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
10472 	      && cum->nargs_prototype > 0)
10473             {
10474 	      static bool warned;
10475 	      if (!warned && warn_psabi)
10476 		{
10477 		  warned = true;
10478 		  inform (input_location,
10479 			  "the ABI of passing homogeneous float aggregates"
10480 			  " will change in a future GCC release");
10481 		}
10482 	    }
10483 
10484 	  return rs6000_finish_function_arg (mode, rvec, k);
10485 	}
10486       else if (align_words < GP_ARG_NUM_REG)
10487 	{
10488 	  if (TARGET_32BIT && TARGET_POWERPC64)
10489 	    return rs6000_mixed_function_arg (mode, type, align_words);
10490 
10491 	  return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10492 	}
10493       else
10494 	return NULL_RTX;
10495     }
10496 }
10497 
10498 /* For an arg passed partly in registers and partly in memory, this is
10499    the number of bytes passed in registers.  For args passed entirely in
10500    registers or entirely in memory, zero.  When an arg is described by a
10501    PARALLEL, perhaps using more than one register type, this function
10502    returns the number of bytes used by the first element of the PARALLEL.  */
10503 
10504 static int
rs6000_arg_partial_bytes(cumulative_args_t cum_v,enum machine_mode mode,tree type,bool named)10505 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
10506 			  tree type, bool named)
10507 {
10508   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10509   bool passed_in_gprs = true;
10510   int ret = 0;
10511   int align_words;
10512   enum machine_mode elt_mode;
10513   int n_elts;
10514 
10515   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10516 
10517   if (DEFAULT_ABI == ABI_V4)
10518     return 0;
10519 
10520   if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10521     {
10522       /* If we are passing this arg in the fixed parameter save area
10523          (gprs or memory) as well as VRs, we do not use the partial
10524 	 bytes mechanism; instead, rs6000_function_arg will return a
10525 	 PARALLEL including a memory element as necessary.  */
10526       if (TARGET_64BIT && ! cum->prototype)
10527 	return 0;
10528 
10529       /* Otherwise, we pass in VRs only.  Check for partial copies.  */
10530       passed_in_gprs = false;
10531       if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
10532 	ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
10533     }
10534 
10535   /* In this complicated case we just disable the partial_nregs code.  */
10536   if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10537     return 0;
10538 
10539   align_words = rs6000_parm_start (mode, type, cum->words);
10540 
10541   if (USE_FP_FOR_ARG_P (cum, elt_mode))
10542     {
10543       unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10544 
10545       /* If we are passing this arg in the fixed parameter save area
10546          (gprs or memory) as well as FPRs, we do not use the partial
10547 	 bytes mechanism; instead, rs6000_function_arg will return a
10548 	 PARALLEL including a memory element as necessary.  */
10549       if (type
10550 	  && (cum->nargs_prototype <= 0
10551 	      || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10552 		  && TARGET_XL_COMPAT
10553 		  && align_words >= GP_ARG_NUM_REG)))
10554 	return 0;
10555 
10556       /* Otherwise, we pass in FPRs only.  Check for partial copies.  */
10557       passed_in_gprs = false;
10558       if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
10559 	ret = ((FP_ARG_MAX_REG + 1 - cum->fregno)
10560 	       * MIN (8, GET_MODE_SIZE (elt_mode)));
10561     }
10562 
10563   if (passed_in_gprs
10564       && align_words < GP_ARG_NUM_REG
10565       && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
10566     ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
10567 
10568   if (ret != 0 && TARGET_DEBUG_ARG)
10569     fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
10570 
10571   return ret;
10572 }
10573 
10574 /* A C expression that indicates when an argument must be passed by
10575    reference.  If nonzero for an argument, a copy of that argument is
10576    made in memory and a pointer to the argument is passed instead of
10577    the argument itself.  The pointer is passed in whatever way is
10578    appropriate for passing a pointer to that type.
10579 
10580    Under V.4, aggregates and long double are passed by reference.
10581 
10582    As an extension to all 32-bit ABIs, AltiVec vectors are passed by
10583    reference unless the AltiVec vector extension ABI is in force.
10584 
10585    As an extension to all ABIs, variable sized types are passed by
10586    reference.  */
10587 
10588 static bool
rs6000_pass_by_reference(cumulative_args_t cum ATTRIBUTE_UNUSED,enum machine_mode mode,const_tree type,bool named ATTRIBUTE_UNUSED)10589 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
10590 			  enum machine_mode mode, const_tree type,
10591 			  bool named ATTRIBUTE_UNUSED)
10592 {
10593   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
10594     {
10595       if (TARGET_DEBUG_ARG)
10596 	fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
10597       return 1;
10598     }
10599 
10600   if (!type)
10601     return 0;
10602 
10603   if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
10604     {
10605       if (TARGET_DEBUG_ARG)
10606 	fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
10607       return 1;
10608     }
10609 
10610   if (int_size_in_bytes (type) < 0)
10611     {
10612       if (TARGET_DEBUG_ARG)
10613 	fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
10614       return 1;
10615     }
10616 
10617   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
10618      modes only exist for GCC vector types if -maltivec.  */
10619   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
10620     {
10621       if (TARGET_DEBUG_ARG)
10622 	fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
10623       return 1;
10624     }
10625 
10626   /* Pass synthetic vectors in memory.  */
10627   if (TREE_CODE (type) == VECTOR_TYPE
10628       && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
10629     {
10630       static bool warned_for_pass_big_vectors = false;
10631       if (TARGET_DEBUG_ARG)
10632 	fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
10633       if (!warned_for_pass_big_vectors)
10634 	{
10635 	  warning (0, "GCC vector passed by reference: "
10636 		   "non-standard ABI extension with no compatibility guarantee");
10637 	  warned_for_pass_big_vectors = true;
10638 	}
10639       return 1;
10640     }
10641 
10642   return 0;
10643 }
10644 
10645 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
10646    already processes.  Return true if the parameter must be passed
10647    (fully or partially) on the stack.  */
10648 
10649 static bool
rs6000_parm_needs_stack(cumulative_args_t args_so_far,tree type)10650 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
10651 {
10652   enum machine_mode mode;
10653   int unsignedp;
10654   rtx entry_parm;
10655 
10656   /* Catch errors.  */
10657   if (type == NULL || type == error_mark_node)
10658     return true;
10659 
10660   /* Handle types with no storage requirement.  */
10661   if (TYPE_MODE (type) == VOIDmode)
10662     return false;
10663 
10664   /* Handle complex types.  */
10665   if (TREE_CODE (type) == COMPLEX_TYPE)
10666     return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
10667 	    || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
10668 
10669   /* Handle transparent aggregates.  */
10670   if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
10671       && TYPE_TRANSPARENT_AGGR (type))
10672     type = TREE_TYPE (first_field (type));
10673 
10674   /* See if this arg was passed by invisible reference.  */
10675   if (pass_by_reference (get_cumulative_args (args_so_far),
10676 			 TYPE_MODE (type), type, true))
10677     type = build_pointer_type (type);
10678 
10679   /* Find mode as it is passed by the ABI.  */
10680   unsignedp = TYPE_UNSIGNED (type);
10681   mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
10682 
10683   /* If we must pass in stack, we need a stack.  */
10684   if (rs6000_must_pass_in_stack (mode, type))
10685     return true;
10686 
10687   /* If there is no incoming register, we need a stack.  */
10688   entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
10689   if (entry_parm == NULL)
10690     return true;
10691 
10692   /* Likewise if we need to pass both in registers and on the stack.  */
10693   if (GET_CODE (entry_parm) == PARALLEL
10694       && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
10695     return true;
10696 
10697   /* Also true if we're partially in registers and partially not.  */
10698   if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
10699     return true;
10700 
10701   /* Update info on where next arg arrives in registers.  */
10702   rs6000_function_arg_advance (args_so_far, mode, type, true);
10703   return false;
10704 }
10705 
10706 /* Return true if FUN has no prototype, has a variable argument
10707    list, or passes any parameter in memory.  */
10708 
10709 static bool
rs6000_function_parms_need_stack(tree fun,bool incoming)10710 rs6000_function_parms_need_stack (tree fun, bool incoming)
10711 {
10712   tree fntype, result;
10713   CUMULATIVE_ARGS args_so_far_v;
10714   cumulative_args_t args_so_far;
10715 
10716   if (!fun)
10717     /* Must be a libcall, all of which only use reg parms.  */
10718     return false;
10719 
10720   fntype = fun;
10721   if (!TYPE_P (fun))
10722     fntype = TREE_TYPE (fun);
10723 
10724   /* Varargs functions need the parameter save area.  */
10725   if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
10726     return true;
10727 
10728   INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
10729   args_so_far = pack_cumulative_args (&args_so_far_v);
10730 
10731   /* When incoming, we will have been passed the function decl.
10732      It is necessary to use the decl to handle K&R style functions,
10733      where TYPE_ARG_TYPES may not be available.  */
10734   if (incoming)
10735     {
10736       gcc_assert (DECL_P (fun));
10737       result = DECL_RESULT (fun);
10738     }
10739   else
10740     result = TREE_TYPE (fntype);
10741 
10742   if (result && aggregate_value_p (result, fntype))
10743     {
10744       if (!TYPE_P (result))
10745 	result = TREE_TYPE (result);
10746       result = build_pointer_type (result);
10747       rs6000_parm_needs_stack (args_so_far, result);
10748     }
10749 
10750   if (incoming)
10751     {
10752       tree parm;
10753 
10754       for (parm = DECL_ARGUMENTS (fun);
10755 	   parm && parm != void_list_node;
10756 	   parm = TREE_CHAIN (parm))
10757 	if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
10758 	  return true;
10759     }
10760   else
10761     {
10762       function_args_iterator args_iter;
10763       tree arg_type;
10764 
10765       FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
10766 	if (rs6000_parm_needs_stack (args_so_far, arg_type))
10767 	  return true;
10768     }
10769 
10770   return false;
10771 }
10772 
10773 /* Return the size of the REG_PARM_STACK_SPACE are for FUN.  This is
10774    usually a constant depending on the ABI.  However, in the ELFv2 ABI
10775    the register parameter area is optional when calling a function that
10776    has a prototype is scope, has no variable argument list, and passes
10777    all parameters in registers.  */
10778 
10779 int
rs6000_reg_parm_stack_space(tree fun,bool incoming)10780 rs6000_reg_parm_stack_space (tree fun, bool incoming)
10781 {
10782   int reg_parm_stack_space;
10783 
10784   switch (DEFAULT_ABI)
10785     {
10786     default:
10787       reg_parm_stack_space = 0;
10788       break;
10789 
10790     case ABI_AIX:
10791     case ABI_DARWIN:
10792       reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10793       break;
10794 
10795     case ABI_ELFv2:
10796       /* ??? Recomputing this every time is a bit expensive.  Is there
10797 	 a place to cache this information?  */
10798       if (rs6000_function_parms_need_stack (fun, incoming))
10799 	reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10800       else
10801 	reg_parm_stack_space = 0;
10802       break;
10803     }
10804 
10805   return reg_parm_stack_space;
10806 }
10807 
10808 static void
rs6000_move_block_from_reg(int regno,rtx x,int nregs)10809 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
10810 {
10811   int i;
10812   enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
10813 
10814   if (nregs == 0)
10815     return;
10816 
10817   for (i = 0; i < nregs; i++)
10818     {
10819       rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
10820       if (reload_completed)
10821 	{
10822 	  if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
10823 	    tem = NULL_RTX;
10824 	  else
10825 	    tem = simplify_gen_subreg (reg_mode, x, BLKmode,
10826 				       i * GET_MODE_SIZE (reg_mode));
10827 	}
10828       else
10829 	tem = replace_equiv_address (tem, XEXP (tem, 0));
10830 
10831       gcc_assert (tem);
10832 
10833       emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
10834     }
10835 }
10836 
10837 /* Perform any needed actions needed for a function that is receiving a
10838    variable number of arguments.
10839 
10840    CUM is as above.
10841 
10842    MODE and TYPE are the mode and type of the current parameter.
10843 
10844    PRETEND_SIZE is a variable that should be set to the amount of stack
10845    that must be pushed by the prolog to pretend that our caller pushed
10846    it.
10847 
10848    Normally, this macro will push all remaining incoming registers on the
10849    stack and set PRETEND_SIZE to the length of the registers pushed.  */
10850 
10851 static void
setup_incoming_varargs(cumulative_args_t cum,enum machine_mode mode,tree type,int * pretend_size ATTRIBUTE_UNUSED,int no_rtl)10852 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
10853 			tree type, int *pretend_size ATTRIBUTE_UNUSED,
10854 			int no_rtl)
10855 {
10856   CUMULATIVE_ARGS next_cum;
10857   int reg_size = TARGET_32BIT ? 4 : 8;
10858   rtx save_area = NULL_RTX, mem;
10859   int first_reg_offset;
10860   alias_set_type set;
10861 
10862   /* Skip the last named argument.  */
10863   next_cum = *get_cumulative_args (cum);
10864   rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
10865 
10866   if (DEFAULT_ABI == ABI_V4)
10867     {
10868       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
10869 
10870       if (! no_rtl)
10871 	{
10872 	  int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
10873 	  HOST_WIDE_INT offset = 0;
10874 
10875 	  /* Try to optimize the size of the varargs save area.
10876 	     The ABI requires that ap.reg_save_area is doubleword
10877 	     aligned, but we don't need to allocate space for all
10878 	     the bytes, only those to which we actually will save
10879 	     anything.  */
10880 	  if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
10881 	    gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
10882 	  if (TARGET_HARD_FLOAT && TARGET_FPRS
10883 	      && next_cum.fregno <= FP_ARG_V4_MAX_REG
10884 	      && cfun->va_list_fpr_size)
10885 	    {
10886 	      if (gpr_reg_num)
10887 		fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
10888 			   * UNITS_PER_FP_WORD;
10889 	      if (cfun->va_list_fpr_size
10890 		  < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10891 		fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
10892 	      else
10893 		fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10894 			    * UNITS_PER_FP_WORD;
10895 	    }
10896 	  if (gpr_reg_num)
10897 	    {
10898 	      offset = -((first_reg_offset * reg_size) & ~7);
10899 	      if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
10900 		{
10901 		  gpr_reg_num = cfun->va_list_gpr_size;
10902 		  if (reg_size == 4 && (first_reg_offset & 1))
10903 		    gpr_reg_num++;
10904 		}
10905 	      gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
10906 	    }
10907 	  else if (fpr_size)
10908 	    offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
10909 		       * UNITS_PER_FP_WORD
10910 		     - (int) (GP_ARG_NUM_REG * reg_size);
10911 
10912 	  if (gpr_size + fpr_size)
10913 	    {
10914 	      rtx reg_save_area
10915 		= assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
10916 	      gcc_assert (GET_CODE (reg_save_area) == MEM);
10917 	      reg_save_area = XEXP (reg_save_area, 0);
10918 	      if (GET_CODE (reg_save_area) == PLUS)
10919 		{
10920 		  gcc_assert (XEXP (reg_save_area, 0)
10921 			      == virtual_stack_vars_rtx);
10922 		  gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
10923 		  offset += INTVAL (XEXP (reg_save_area, 1));
10924 		}
10925 	      else
10926 		gcc_assert (reg_save_area == virtual_stack_vars_rtx);
10927 	    }
10928 
10929 	  cfun->machine->varargs_save_offset = offset;
10930 	  save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
10931 	}
10932     }
10933   else
10934     {
10935       first_reg_offset = next_cum.words;
10936       save_area = virtual_incoming_args_rtx;
10937 
10938       if (targetm.calls.must_pass_in_stack (mode, type))
10939 	first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
10940     }
10941 
10942   set = get_varargs_alias_set ();
10943   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
10944       && cfun->va_list_gpr_size)
10945     {
10946       int nregs = GP_ARG_NUM_REG - first_reg_offset;
10947       int n_gpr;
10948 
10949       if (va_list_gpr_counter_field)
10950 	{
10951 	  /* V4 va_list_gpr_size counts number of registers needed.  */
10952 	  n_gpr = cfun->va_list_gpr_size;
10953 	}
10954       else
10955 	{
10956 	  /* char * va_list instead counts number of bytes needed.  */
10957 	  n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
10958 	}
10959       if (nregs > n_gpr)
10960 	nregs = n_gpr;
10961 
10962       mem = gen_rtx_MEM (BLKmode,
10963 			 plus_constant (Pmode, save_area,
10964 					first_reg_offset * reg_size));
10965       MEM_NOTRAP_P (mem) = 1;
10966       set_mem_alias_set (mem, set);
10967       set_mem_align (mem, BITS_PER_WORD);
10968 
10969       rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
10970 				  nregs);
10971     }
10972 
10973   /* Save FP registers if needed.  */
10974   if (DEFAULT_ABI == ABI_V4
10975       && TARGET_HARD_FLOAT && TARGET_FPRS
10976       && ! no_rtl
10977       && next_cum.fregno <= FP_ARG_V4_MAX_REG
10978       && cfun->va_list_fpr_size)
10979     {
10980       int fregno = next_cum.fregno, nregs;
10981       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
10982       rtx lab = gen_label_rtx ();
10983       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
10984 					       * UNITS_PER_FP_WORD);
10985 
10986       emit_jump_insn
10987 	(gen_rtx_SET (VOIDmode,
10988 		      pc_rtx,
10989 		      gen_rtx_IF_THEN_ELSE (VOIDmode,
10990 					    gen_rtx_NE (VOIDmode, cr1,
10991 							const0_rtx),
10992 					    gen_rtx_LABEL_REF (VOIDmode, lab),
10993 					    pc_rtx)));
10994 
10995       for (nregs = 0;
10996 	   fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
10997 	   fregno++, off += UNITS_PER_FP_WORD, nregs++)
10998 	{
10999 	  mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
11000 			      ? DFmode : SFmode,
11001                              plus_constant (Pmode, save_area, off));
11002   	  MEM_NOTRAP_P (mem) = 1;
11003   	  set_mem_alias_set (mem, set);
11004 	  set_mem_align (mem, GET_MODE_ALIGNMENT (
11005 			 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
11006 			  ? DFmode : SFmode));
11007 	  emit_move_insn (mem, gen_rtx_REG (
11008                           (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
11009 			   ? DFmode : SFmode, fregno));
11010 	}
11011 
11012       emit_label (lab);
11013     }
11014 }
11015 
11016 /* Create the va_list data type.  */
11017 
11018 static tree
rs6000_build_builtin_va_list(void)11019 rs6000_build_builtin_va_list (void)
11020 {
11021   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
11022 
11023   /* For AIX, prefer 'char *' because that's what the system
11024      header files like.  */
11025   if (DEFAULT_ABI != ABI_V4)
11026     return build_pointer_type (char_type_node);
11027 
11028   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
11029   type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11030       			  get_identifier ("__va_list_tag"), record);
11031 
11032   f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
11033 		      unsigned_char_type_node);
11034   f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
11035 		      unsigned_char_type_node);
11036   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
11037      every user file.  */
11038   f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
11039       		      get_identifier ("reserved"), short_unsigned_type_node);
11040   f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
11041       		      get_identifier ("overflow_arg_area"),
11042 		      ptr_type_node);
11043   f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
11044       		      get_identifier ("reg_save_area"),
11045 		      ptr_type_node);
11046 
11047   va_list_gpr_counter_field = f_gpr;
11048   va_list_fpr_counter_field = f_fpr;
11049 
11050   DECL_FIELD_CONTEXT (f_gpr) = record;
11051   DECL_FIELD_CONTEXT (f_fpr) = record;
11052   DECL_FIELD_CONTEXT (f_res) = record;
11053   DECL_FIELD_CONTEXT (f_ovf) = record;
11054   DECL_FIELD_CONTEXT (f_sav) = record;
11055 
11056   TYPE_STUB_DECL (record) = type_decl;
11057   TYPE_NAME (record) = type_decl;
11058   TYPE_FIELDS (record) = f_gpr;
11059   DECL_CHAIN (f_gpr) = f_fpr;
11060   DECL_CHAIN (f_fpr) = f_res;
11061   DECL_CHAIN (f_res) = f_ovf;
11062   DECL_CHAIN (f_ovf) = f_sav;
11063 
11064   layout_type (record);
11065 
11066   /* The correct type is an array type of one element.  */
11067   return build_array_type (record, build_index_type (size_zero_node));
11068 }
11069 
11070 /* Implement va_start.  */
11071 
11072 static void
rs6000_va_start(tree valist,rtx nextarg)11073 rs6000_va_start (tree valist, rtx nextarg)
11074 {
11075   HOST_WIDE_INT words, n_gpr, n_fpr;
11076   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
11077   tree gpr, fpr, ovf, sav, t;
11078 
11079   /* Only SVR4 needs something special.  */
11080   if (DEFAULT_ABI != ABI_V4)
11081     {
11082       std_expand_builtin_va_start (valist, nextarg);
11083       return;
11084     }
11085 
11086   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
11087   f_fpr = DECL_CHAIN (f_gpr);
11088   f_res = DECL_CHAIN (f_fpr);
11089   f_ovf = DECL_CHAIN (f_res);
11090   f_sav = DECL_CHAIN (f_ovf);
11091 
11092   valist = build_simple_mem_ref (valist);
11093   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
11094   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
11095 		f_fpr, NULL_TREE);
11096   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
11097 		f_ovf, NULL_TREE);
11098   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
11099 		f_sav, NULL_TREE);
11100 
11101   /* Count number of gp and fp argument registers used.  */
11102   words = crtl->args.info.words;
11103   n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
11104 	       GP_ARG_NUM_REG);
11105   n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
11106 	       FP_ARG_NUM_REG);
11107 
11108   if (TARGET_DEBUG_ARG)
11109     fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
11110 	     HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
11111 	     words, n_gpr, n_fpr);
11112 
11113   if (cfun->va_list_gpr_size)
11114     {
11115       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
11116 		  build_int_cst (NULL_TREE, n_gpr));
11117       TREE_SIDE_EFFECTS (t) = 1;
11118       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11119     }
11120 
11121   if (cfun->va_list_fpr_size)
11122     {
11123       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
11124 		  build_int_cst (NULL_TREE, n_fpr));
11125       TREE_SIDE_EFFECTS (t) = 1;
11126       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11127 
11128 #ifdef HAVE_AS_GNU_ATTRIBUTE
11129       if (call_ABI_of_interest (cfun->decl))
11130 	rs6000_passes_float = true;
11131 #endif
11132     }
11133 
11134   /* Find the overflow area.  */
11135   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
11136   if (words != 0)
11137     t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
11138   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
11139   TREE_SIDE_EFFECTS (t) = 1;
11140   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11141 
11142   /* If there were no va_arg invocations, don't set up the register
11143      save area.  */
11144   if (!cfun->va_list_gpr_size
11145       && !cfun->va_list_fpr_size
11146       && n_gpr < GP_ARG_NUM_REG
11147       && n_fpr < FP_ARG_V4_MAX_REG)
11148     return;
11149 
11150   /* Find the register save area.  */
11151   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
11152   if (cfun->machine->varargs_save_offset)
11153     t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
11154   t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
11155   TREE_SIDE_EFFECTS (t) = 1;
11156   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11157 }
11158 
11159 /* Implement va_arg.  */
11160 
11161 static tree
rs6000_gimplify_va_arg(tree valist,tree type,gimple_seq * pre_p,gimple_seq * post_p)11162 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
11163 			gimple_seq *post_p)
11164 {
11165   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
11166   tree gpr, fpr, ovf, sav, reg, t, u;
11167   int size, rsize, n_reg, sav_ofs, sav_scale;
11168   tree lab_false, lab_over, addr;
11169   int align;
11170   tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
11171   int regalign = 0;
11172   gimple stmt;
11173 
11174   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
11175     {
11176       t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
11177       return build_va_arg_indirect_ref (t);
11178     }
11179 
11180   /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
11181      earlier version of gcc, with the property that it always applied alignment
11182      adjustments to the va-args (even for zero-sized types).  The cheapest way
11183      to deal with this is to replicate the effect of the part of
11184      std_gimplify_va_arg_expr that carries out the align adjust, for the case
11185      of relevance.
11186      We don't need to check for pass-by-reference because of the test above.
11187      We can return a simplifed answer, since we know there's no offset to add.  */
11188 
11189   if (((TARGET_MACHO
11190         && rs6000_darwin64_abi)
11191        || DEFAULT_ABI == ABI_ELFv2
11192        || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
11193       && integer_zerop (TYPE_SIZE (type)))
11194     {
11195       unsigned HOST_WIDE_INT align, boundary;
11196       tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
11197       align = PARM_BOUNDARY / BITS_PER_UNIT;
11198       boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
11199       if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
11200 	boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
11201       boundary /= BITS_PER_UNIT;
11202       if (boundary > align)
11203 	{
11204 	  tree t ;
11205 	  /* This updates arg ptr by the amount that would be necessary
11206 	     to align the zero-sized (but not zero-alignment) item.  */
11207 	  t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
11208 		      fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
11209 	  gimplify_and_add (t, pre_p);
11210 
11211 	  t = fold_convert (sizetype, valist_tmp);
11212 	  t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
11213 		  fold_convert (TREE_TYPE (valist),
11214 				fold_build2 (BIT_AND_EXPR, sizetype, t,
11215 					     size_int (-boundary))));
11216 	  t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
11217 	  gimplify_and_add (t, pre_p);
11218 	}
11219       /* Since it is zero-sized there's no increment for the item itself. */
11220       valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
11221       return build_va_arg_indirect_ref (valist_tmp);
11222     }
11223 
11224   if (DEFAULT_ABI != ABI_V4)
11225     {
11226       if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
11227 	{
11228 	  tree elem_type = TREE_TYPE (type);
11229 	  enum machine_mode elem_mode = TYPE_MODE (elem_type);
11230 	  int elem_size = GET_MODE_SIZE (elem_mode);
11231 
11232 	  if (elem_size < UNITS_PER_WORD)
11233 	    {
11234 	      tree real_part, imag_part;
11235 	      gimple_seq post = NULL;
11236 
11237 	      real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
11238 						  &post);
11239 	      /* Copy the value into a temporary, lest the formal temporary
11240 		 be reused out from under us.  */
11241 	      real_part = get_initialized_tmp_var (real_part, pre_p, &post);
11242 	      gimple_seq_add_seq (pre_p, post);
11243 
11244 	      imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
11245 						  post_p);
11246 
11247 	      return build2 (COMPLEX_EXPR, type, real_part, imag_part);
11248 	    }
11249 	}
11250 
11251       return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
11252     }
11253 
11254   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
11255   f_fpr = DECL_CHAIN (f_gpr);
11256   f_res = DECL_CHAIN (f_fpr);
11257   f_ovf = DECL_CHAIN (f_res);
11258   f_sav = DECL_CHAIN (f_ovf);
11259 
11260   valist = build_va_arg_indirect_ref (valist);
11261   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
11262   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
11263 		f_fpr, NULL_TREE);
11264   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
11265 		f_ovf, NULL_TREE);
11266   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
11267 		f_sav, NULL_TREE);
11268 
11269   size = int_size_in_bytes (type);
11270   rsize = (size + 3) / 4;
11271   align = 1;
11272 
11273   if (TARGET_HARD_FLOAT && TARGET_FPRS
11274       && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
11275           || (TARGET_DOUBLE_FLOAT
11276               && (TYPE_MODE (type) == DFmode
11277  	          || TYPE_MODE (type) == TFmode
11278 	          || TYPE_MODE (type) == SDmode
11279 	          || TYPE_MODE (type) == DDmode
11280 	          || TYPE_MODE (type) == TDmode))))
11281     {
11282       /* FP args go in FP registers, if present.  */
11283       reg = fpr;
11284       n_reg = (size + 7) / 8;
11285       sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
11286       sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
11287       if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
11288 	align = 8;
11289     }
11290   else
11291     {
11292       /* Otherwise into GP registers.  */
11293       reg = gpr;
11294       n_reg = rsize;
11295       sav_ofs = 0;
11296       sav_scale = 4;
11297       if (n_reg == 2)
11298 	align = 8;
11299     }
11300 
11301   /* Pull the value out of the saved registers....  */
11302 
11303   lab_over = NULL;
11304   addr = create_tmp_var (ptr_type_node, "addr");
11305 
11306   /*  AltiVec vectors never go in registers when -mabi=altivec.  */
11307   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11308     align = 16;
11309   else
11310     {
11311       lab_false = create_artificial_label (input_location);
11312       lab_over = create_artificial_label (input_location);
11313 
11314       /* Long long and SPE vectors are aligned in the registers.
11315 	 As are any other 2 gpr item such as complex int due to a
11316 	 historical mistake.  */
11317       u = reg;
11318       if (n_reg == 2 && reg == gpr)
11319 	{
11320 	  regalign = 1;
11321 	  u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11322 		     build_int_cst (TREE_TYPE (reg), n_reg - 1));
11323 	  u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
11324 		      unshare_expr (reg), u);
11325 	}
11326       /* _Decimal128 is passed in even/odd fpr pairs; the stored
11327 	 reg number is 0 for f1, so we want to make it odd.  */
11328       else if (reg == fpr && TYPE_MODE (type) == TDmode)
11329 	{
11330 	  t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11331 		      build_int_cst (TREE_TYPE (reg), 1));
11332 	  u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
11333 	}
11334 
11335       t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
11336       t = build2 (GE_EXPR, boolean_type_node, u, t);
11337       u = build1 (GOTO_EXPR, void_type_node, lab_false);
11338       t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
11339       gimplify_and_add (t, pre_p);
11340 
11341       t = sav;
11342       if (sav_ofs)
11343 	t = fold_build_pointer_plus_hwi (sav, sav_ofs);
11344 
11345       u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11346 		  build_int_cst (TREE_TYPE (reg), n_reg));
11347       u = fold_convert (sizetype, u);
11348       u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
11349       t = fold_build_pointer_plus (t, u);
11350 
11351       /* _Decimal32 varargs are located in the second word of the 64-bit
11352 	 FP register for 32-bit binaries.  */
11353       if (!TARGET_POWERPC64
11354 	  && TARGET_HARD_FLOAT && TARGET_FPRS
11355 	  && TYPE_MODE (type) == SDmode)
11356 	t = fold_build_pointer_plus_hwi (t, size);
11357 
11358       gimplify_assign (addr, t, pre_p);
11359 
11360       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
11361 
11362       stmt = gimple_build_label (lab_false);
11363       gimple_seq_add_stmt (pre_p, stmt);
11364 
11365       if ((n_reg == 2 && !regalign) || n_reg > 2)
11366 	{
11367 	  /* Ensure that we don't find any more args in regs.
11368 	     Alignment has taken care of for special cases.  */
11369 	  gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
11370 	}
11371     }
11372 
11373   /* ... otherwise out of the overflow area.  */
11374 
11375   /* Care for on-stack alignment if needed.  */
11376   t = ovf;
11377   if (align != 1)
11378     {
11379       t = fold_build_pointer_plus_hwi (t, align - 1);
11380       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
11381 		  build_int_cst (TREE_TYPE (t), -align));
11382     }
11383   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
11384 
11385   gimplify_assign (unshare_expr (addr), t, pre_p);
11386 
11387   t = fold_build_pointer_plus_hwi (t, size);
11388   gimplify_assign (unshare_expr (ovf), t, pre_p);
11389 
11390   if (lab_over)
11391     {
11392       stmt = gimple_build_label (lab_over);
11393       gimple_seq_add_stmt (pre_p, stmt);
11394     }
11395 
11396   if (STRICT_ALIGNMENT
11397       && (TYPE_ALIGN (type)
11398 	  > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
11399     {
11400       /* The value (of type complex double, for example) may not be
11401 	 aligned in memory in the saved registers, so copy via a
11402 	 temporary.  (This is the same code as used for SPARC.)  */
11403       tree tmp = create_tmp_var (type, "va_arg_tmp");
11404       tree dest_addr = build_fold_addr_expr (tmp);
11405 
11406       tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
11407 				   3, dest_addr, addr, size_int (rsize * 4));
11408 
11409       gimplify_and_add (copy, pre_p);
11410       addr = dest_addr;
11411     }
11412 
11413   addr = fold_convert (ptrtype, addr);
11414   return build_va_arg_indirect_ref (addr);
11415 }
11416 
11417 /* Builtins.  */
11418 
11419 static void
def_builtin(const char * name,tree type,enum rs6000_builtins code)11420 def_builtin (const char *name, tree type, enum rs6000_builtins code)
11421 {
11422   tree t;
11423   unsigned classify = rs6000_builtin_info[(int)code].attr;
11424   const char *attr_string = "";
11425 
11426   gcc_assert (name != NULL);
11427   gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
11428 
11429   if (rs6000_builtin_decls[(int)code])
11430     fatal_error ("internal error: builtin function %s already processed", name);
11431 
11432   rs6000_builtin_decls[(int)code] = t =
11433     add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
11434 
11435   /* Set any special attributes.  */
11436   if ((classify & RS6000_BTC_CONST) != 0)
11437     {
11438       /* const function, function only depends on the inputs.  */
11439       TREE_READONLY (t) = 1;
11440       TREE_NOTHROW (t) = 1;
11441       attr_string = ", pure";
11442     }
11443   else if ((classify & RS6000_BTC_PURE) != 0)
11444     {
11445       /* pure function, function can read global memory, but does not set any
11446 	 external state.  */
11447       DECL_PURE_P (t) = 1;
11448       TREE_NOTHROW (t) = 1;
11449       attr_string = ", const";
11450     }
11451   else if ((classify & RS6000_BTC_FP) != 0)
11452     {
11453       /* Function is a math function.  If rounding mode is on, then treat the
11454 	 function as not reading global memory, but it can have arbitrary side
11455 	 effects.  If it is off, then assume the function is a const function.
11456 	 This mimics the ATTR_MATHFN_FPROUNDING attribute in
11457 	 builtin-attribute.def that is used for the math functions. */
11458       TREE_NOTHROW (t) = 1;
11459       if (flag_rounding_math)
11460 	{
11461 	  DECL_PURE_P (t) = 1;
11462 	  DECL_IS_NOVOPS (t) = 1;
11463 	  attr_string = ", fp, pure";
11464 	}
11465       else
11466 	{
11467 	  TREE_READONLY (t) = 1;
11468 	  attr_string = ", fp, const";
11469 	}
11470     }
11471   else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
11472     gcc_unreachable ();
11473 
11474   if (TARGET_DEBUG_BUILTIN)
11475     fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
11476 	     (int)code, name, attr_string);
11477 }
11478 
11479 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
11480 
11481 #undef RS6000_BUILTIN_1
11482 #undef RS6000_BUILTIN_2
11483 #undef RS6000_BUILTIN_3
11484 #undef RS6000_BUILTIN_A
11485 #undef RS6000_BUILTIN_D
11486 #undef RS6000_BUILTIN_E
11487 #undef RS6000_BUILTIN_H
11488 #undef RS6000_BUILTIN_P
11489 #undef RS6000_BUILTIN_Q
11490 #undef RS6000_BUILTIN_S
11491 #undef RS6000_BUILTIN_X
11492 
11493 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11494 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11495 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
11496   { MASK, ICODE, NAME, ENUM },
11497 
11498 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11499 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11500 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11501 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11502 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11503 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11504 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11505 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11506 
11507 static const struct builtin_description bdesc_3arg[] =
11508 {
11509 #include "rs6000-builtin.def"
11510 };
11511 
11512 /* DST operations: void foo (void *, const int, const char).  */
11513 
11514 #undef RS6000_BUILTIN_1
11515 #undef RS6000_BUILTIN_2
11516 #undef RS6000_BUILTIN_3
11517 #undef RS6000_BUILTIN_A
11518 #undef RS6000_BUILTIN_D
11519 #undef RS6000_BUILTIN_E
11520 #undef RS6000_BUILTIN_H
11521 #undef RS6000_BUILTIN_P
11522 #undef RS6000_BUILTIN_Q
11523 #undef RS6000_BUILTIN_S
11524 #undef RS6000_BUILTIN_X
11525 
11526 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11527 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11528 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11529 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11530 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
11531   { MASK, ICODE, NAME, ENUM },
11532 
11533 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11534 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11535 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11536 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11537 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11538 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11539 
11540 static const struct builtin_description bdesc_dst[] =
11541 {
11542 #include "rs6000-builtin.def"
11543 };
11544 
11545 /* Simple binary operations: VECc = foo (VECa, VECb).  */
11546 
11547 #undef RS6000_BUILTIN_1
11548 #undef RS6000_BUILTIN_2
11549 #undef RS6000_BUILTIN_3
11550 #undef RS6000_BUILTIN_A
11551 #undef RS6000_BUILTIN_D
11552 #undef RS6000_BUILTIN_E
11553 #undef RS6000_BUILTIN_H
11554 #undef RS6000_BUILTIN_P
11555 #undef RS6000_BUILTIN_Q
11556 #undef RS6000_BUILTIN_S
11557 #undef RS6000_BUILTIN_X
11558 
11559 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11560 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
11561   { MASK, ICODE, NAME, ENUM },
11562 
11563 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11564 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11565 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11566 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11567 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11568 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11569 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11570 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11571 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11572 
11573 static const struct builtin_description bdesc_2arg[] =
11574 {
11575 #include "rs6000-builtin.def"
11576 };
11577 
11578 #undef RS6000_BUILTIN_1
11579 #undef RS6000_BUILTIN_2
11580 #undef RS6000_BUILTIN_3
11581 #undef RS6000_BUILTIN_A
11582 #undef RS6000_BUILTIN_D
11583 #undef RS6000_BUILTIN_E
11584 #undef RS6000_BUILTIN_H
11585 #undef RS6000_BUILTIN_P
11586 #undef RS6000_BUILTIN_Q
11587 #undef RS6000_BUILTIN_S
11588 #undef RS6000_BUILTIN_X
11589 
11590 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11591 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11592 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11593 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11594 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11595 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11596 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11597 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
11598   { MASK, ICODE, NAME, ENUM },
11599 
11600 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11601 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11602 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11603 
11604 /* AltiVec predicates.  */
11605 
11606 static const struct builtin_description bdesc_altivec_preds[] =
11607 {
11608 #include "rs6000-builtin.def"
11609 };
11610 
11611 /* SPE predicates.  */
11612 #undef RS6000_BUILTIN_1
11613 #undef RS6000_BUILTIN_2
11614 #undef RS6000_BUILTIN_3
11615 #undef RS6000_BUILTIN_A
11616 #undef RS6000_BUILTIN_D
11617 #undef RS6000_BUILTIN_E
11618 #undef RS6000_BUILTIN_H
11619 #undef RS6000_BUILTIN_P
11620 #undef RS6000_BUILTIN_Q
11621 #undef RS6000_BUILTIN_S
11622 #undef RS6000_BUILTIN_X
11623 
11624 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11625 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11626 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11627 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11628 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11629 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11630 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11631 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11632 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11633 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
11634   { MASK, ICODE, NAME, ENUM },
11635 
11636 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11637 
11638 static const struct builtin_description bdesc_spe_predicates[] =
11639 {
11640 #include "rs6000-builtin.def"
11641 };
11642 
11643 /* SPE evsel predicates.  */
11644 #undef RS6000_BUILTIN_1
11645 #undef RS6000_BUILTIN_2
11646 #undef RS6000_BUILTIN_3
11647 #undef RS6000_BUILTIN_A
11648 #undef RS6000_BUILTIN_D
11649 #undef RS6000_BUILTIN_E
11650 #undef RS6000_BUILTIN_H
11651 #undef RS6000_BUILTIN_P
11652 #undef RS6000_BUILTIN_Q
11653 #undef RS6000_BUILTIN_S
11654 #undef RS6000_BUILTIN_X
11655 
11656 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11657 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11658 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11659 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11660 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11661 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
11662   { MASK, ICODE, NAME, ENUM },
11663 
11664 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11665 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11666 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11667 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11668 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11669 
11670 static const struct builtin_description bdesc_spe_evsel[] =
11671 {
11672 #include "rs6000-builtin.def"
11673 };
11674 
11675 /* PAIRED predicates.  */
11676 #undef RS6000_BUILTIN_1
11677 #undef RS6000_BUILTIN_2
11678 #undef RS6000_BUILTIN_3
11679 #undef RS6000_BUILTIN_A
11680 #undef RS6000_BUILTIN_D
11681 #undef RS6000_BUILTIN_E
11682 #undef RS6000_BUILTIN_H
11683 #undef RS6000_BUILTIN_P
11684 #undef RS6000_BUILTIN_Q
11685 #undef RS6000_BUILTIN_S
11686 #undef RS6000_BUILTIN_X
11687 
11688 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11689 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11690 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11691 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11692 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11693 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11694 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11695 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11696 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
11697   { MASK, ICODE, NAME, ENUM },
11698 
11699 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11700 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11701 
11702 static const struct builtin_description bdesc_paired_preds[] =
11703 {
11704 #include "rs6000-builtin.def"
11705 };
11706 
11707 /* ABS* operations.  */
11708 
11709 #undef RS6000_BUILTIN_1
11710 #undef RS6000_BUILTIN_2
11711 #undef RS6000_BUILTIN_3
11712 #undef RS6000_BUILTIN_A
11713 #undef RS6000_BUILTIN_D
11714 #undef RS6000_BUILTIN_E
11715 #undef RS6000_BUILTIN_H
11716 #undef RS6000_BUILTIN_P
11717 #undef RS6000_BUILTIN_Q
11718 #undef RS6000_BUILTIN_S
11719 #undef RS6000_BUILTIN_X
11720 
11721 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11722 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11723 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11724 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
11725   { MASK, ICODE, NAME, ENUM },
11726 
11727 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11728 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11729 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11730 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11731 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11732 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11733 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11734 
11735 static const struct builtin_description bdesc_abs[] =
11736 {
11737 #include "rs6000-builtin.def"
11738 };
11739 
11740 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
11741    foo (VECa).  */
11742 
11743 #undef RS6000_BUILTIN_1
11744 #undef RS6000_BUILTIN_2
11745 #undef RS6000_BUILTIN_3
11746 #undef RS6000_BUILTIN_A
11747 #undef RS6000_BUILTIN_D
11748 #undef RS6000_BUILTIN_E
11749 #undef RS6000_BUILTIN_H
11750 #undef RS6000_BUILTIN_P
11751 #undef RS6000_BUILTIN_Q
11752 #undef RS6000_BUILTIN_S
11753 #undef RS6000_BUILTIN_X
11754 
11755 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
11756   { MASK, ICODE, NAME, ENUM },
11757 
11758 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11759 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11760 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11761 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11762 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11763 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11764 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11765 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11766 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11767 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11768 
11769 static const struct builtin_description bdesc_1arg[] =
11770 {
11771 #include "rs6000-builtin.def"
11772 };
11773 
11774 /* HTM builtins.  */
11775 #undef RS6000_BUILTIN_1
11776 #undef RS6000_BUILTIN_2
11777 #undef RS6000_BUILTIN_3
11778 #undef RS6000_BUILTIN_A
11779 #undef RS6000_BUILTIN_D
11780 #undef RS6000_BUILTIN_E
11781 #undef RS6000_BUILTIN_H
11782 #undef RS6000_BUILTIN_P
11783 #undef RS6000_BUILTIN_Q
11784 #undef RS6000_BUILTIN_S
11785 #undef RS6000_BUILTIN_X
11786 
11787 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11788 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11789 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11790 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11791 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11792 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11793 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
11794   { MASK, ICODE, NAME, ENUM },
11795 
11796 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11797 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11798 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11799 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11800 
11801 static const struct builtin_description bdesc_htm[] =
11802 {
11803 #include "rs6000-builtin.def"
11804 };
11805 
11806 #undef RS6000_BUILTIN_1
11807 #undef RS6000_BUILTIN_2
11808 #undef RS6000_BUILTIN_3
11809 #undef RS6000_BUILTIN_A
11810 #undef RS6000_BUILTIN_D
11811 #undef RS6000_BUILTIN_E
11812 #undef RS6000_BUILTIN_H
11813 #undef RS6000_BUILTIN_P
11814 #undef RS6000_BUILTIN_Q
11815 #undef RS6000_BUILTIN_S
11816 
11817 /* Return true if a builtin function is overloaded.  */
11818 bool
rs6000_overloaded_builtin_p(enum rs6000_builtins fncode)11819 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
11820 {
11821   return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
11822 }
11823 
11824 /* Expand an expression EXP that calls a builtin without arguments.  */
11825 static rtx
rs6000_expand_zeroop_builtin(enum insn_code icode,rtx target)11826 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
11827 {
11828   rtx pat;
11829   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11830 
11831   if (icode == CODE_FOR_nothing)
11832     /* Builtin not supported on this processor.  */
11833     return 0;
11834 
11835   if (target == 0
11836       || GET_MODE (target) != tmode
11837       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11838     target = gen_reg_rtx (tmode);
11839 
11840   pat = GEN_FCN (icode) (target);
11841   if (! pat)
11842     return 0;
11843   emit_insn (pat);
11844 
11845   return target;
11846 }
11847 
11848 
11849 static rtx
rs6000_expand_unop_builtin(enum insn_code icode,tree exp,rtx target)11850 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
11851 {
11852   rtx pat;
11853   tree arg0 = CALL_EXPR_ARG (exp, 0);
11854   rtx op0 = expand_normal (arg0);
11855   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11856   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11857 
11858   if (icode == CODE_FOR_nothing)
11859     /* Builtin not supported on this processor.  */
11860     return 0;
11861 
11862   /* If we got invalid arguments bail out before generating bad rtl.  */
11863   if (arg0 == error_mark_node)
11864     return const0_rtx;
11865 
11866   if (icode == CODE_FOR_altivec_vspltisb
11867       || icode == CODE_FOR_altivec_vspltish
11868       || icode == CODE_FOR_altivec_vspltisw
11869       || icode == CODE_FOR_spe_evsplatfi
11870       || icode == CODE_FOR_spe_evsplati)
11871     {
11872       /* Only allow 5-bit *signed* literals.  */
11873       if (GET_CODE (op0) != CONST_INT
11874 	  || INTVAL (op0) > 15
11875 	  || INTVAL (op0) < -16)
11876 	{
11877 	  error ("argument 1 must be a 5-bit signed literal");
11878 	  return const0_rtx;
11879 	}
11880     }
11881 
11882   if (target == 0
11883       || GET_MODE (target) != tmode
11884       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11885     target = gen_reg_rtx (tmode);
11886 
11887   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11888     op0 = copy_to_mode_reg (mode0, op0);
11889 
11890   pat = GEN_FCN (icode) (target, op0);
11891   if (! pat)
11892     return 0;
11893   emit_insn (pat);
11894 
11895   return target;
11896 }
11897 
11898 static rtx
altivec_expand_abs_builtin(enum insn_code icode,tree exp,rtx target)11899 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
11900 {
11901   rtx pat, scratch1, scratch2;
11902   tree arg0 = CALL_EXPR_ARG (exp, 0);
11903   rtx op0 = expand_normal (arg0);
11904   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11905   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11906 
11907   /* If we have invalid arguments, bail out before generating bad rtl.  */
11908   if (arg0 == error_mark_node)
11909     return const0_rtx;
11910 
11911   if (target == 0
11912       || GET_MODE (target) != tmode
11913       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11914     target = gen_reg_rtx (tmode);
11915 
11916   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11917     op0 = copy_to_mode_reg (mode0, op0);
11918 
11919   scratch1 = gen_reg_rtx (mode0);
11920   scratch2 = gen_reg_rtx (mode0);
11921 
11922   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
11923   if (! pat)
11924     return 0;
11925   emit_insn (pat);
11926 
11927   return target;
11928 }
11929 
11930 static rtx
rs6000_expand_binop_builtin(enum insn_code icode,tree exp,rtx target)11931 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
11932 {
11933   rtx pat;
11934   tree arg0 = CALL_EXPR_ARG (exp, 0);
11935   tree arg1 = CALL_EXPR_ARG (exp, 1);
11936   rtx op0 = expand_normal (arg0);
11937   rtx op1 = expand_normal (arg1);
11938   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11939   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11940   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11941 
11942   if (icode == CODE_FOR_nothing)
11943     /* Builtin not supported on this processor.  */
11944     return 0;
11945 
11946   /* If we got invalid arguments bail out before generating bad rtl.  */
11947   if (arg0 == error_mark_node || arg1 == error_mark_node)
11948     return const0_rtx;
11949 
11950   if (icode == CODE_FOR_altivec_vcfux
11951       || icode == CODE_FOR_altivec_vcfsx
11952       || icode == CODE_FOR_altivec_vctsxs
11953       || icode == CODE_FOR_altivec_vctuxs
11954       || icode == CODE_FOR_altivec_vspltb
11955       || icode == CODE_FOR_altivec_vsplth
11956       || icode == CODE_FOR_altivec_vspltw
11957       || icode == CODE_FOR_spe_evaddiw
11958       || icode == CODE_FOR_spe_evldd
11959       || icode == CODE_FOR_spe_evldh
11960       || icode == CODE_FOR_spe_evldw
11961       || icode == CODE_FOR_spe_evlhhesplat
11962       || icode == CODE_FOR_spe_evlhhossplat
11963       || icode == CODE_FOR_spe_evlhhousplat
11964       || icode == CODE_FOR_spe_evlwhe
11965       || icode == CODE_FOR_spe_evlwhos
11966       || icode == CODE_FOR_spe_evlwhou
11967       || icode == CODE_FOR_spe_evlwhsplat
11968       || icode == CODE_FOR_spe_evlwwsplat
11969       || icode == CODE_FOR_spe_evrlwi
11970       || icode == CODE_FOR_spe_evslwi
11971       || icode == CODE_FOR_spe_evsrwis
11972       || icode == CODE_FOR_spe_evsubifw
11973       || icode == CODE_FOR_spe_evsrwiu)
11974     {
11975       /* Only allow 5-bit unsigned literals.  */
11976       STRIP_NOPS (arg1);
11977       if (TREE_CODE (arg1) != INTEGER_CST
11978 	  || TREE_INT_CST_LOW (arg1) & ~0x1f)
11979 	{
11980 	  error ("argument 2 must be a 5-bit unsigned literal");
11981 	  return const0_rtx;
11982 	}
11983     }
11984 
11985   if (target == 0
11986       || GET_MODE (target) != tmode
11987       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11988     target = gen_reg_rtx (tmode);
11989 
11990   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11991     op0 = copy_to_mode_reg (mode0, op0);
11992   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11993     op1 = copy_to_mode_reg (mode1, op1);
11994 
11995   pat = GEN_FCN (icode) (target, op0, op1);
11996   if (! pat)
11997     return 0;
11998   emit_insn (pat);
11999 
12000   return target;
12001 }
12002 
12003 static rtx
altivec_expand_predicate_builtin(enum insn_code icode,tree exp,rtx target)12004 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12005 {
12006   rtx pat, scratch;
12007   tree cr6_form = CALL_EXPR_ARG (exp, 0);
12008   tree arg0 = CALL_EXPR_ARG (exp, 1);
12009   tree arg1 = CALL_EXPR_ARG (exp, 2);
12010   rtx op0 = expand_normal (arg0);
12011   rtx op1 = expand_normal (arg1);
12012   enum machine_mode tmode = SImode;
12013   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12014   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12015   int cr6_form_int;
12016 
12017   if (TREE_CODE (cr6_form) != INTEGER_CST)
12018     {
12019       error ("argument 1 of __builtin_altivec_predicate must be a constant");
12020       return const0_rtx;
12021     }
12022   else
12023     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
12024 
12025   gcc_assert (mode0 == mode1);
12026 
12027   /* If we have invalid arguments, bail out before generating bad rtl.  */
12028   if (arg0 == error_mark_node || arg1 == error_mark_node)
12029     return const0_rtx;
12030 
12031   if (target == 0
12032       || GET_MODE (target) != tmode
12033       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12034     target = gen_reg_rtx (tmode);
12035 
12036   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12037     op0 = copy_to_mode_reg (mode0, op0);
12038   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12039     op1 = copy_to_mode_reg (mode1, op1);
12040 
12041   scratch = gen_reg_rtx (mode0);
12042 
12043   pat = GEN_FCN (icode) (scratch, op0, op1);
12044   if (! pat)
12045     return 0;
12046   emit_insn (pat);
12047 
12048   /* The vec_any* and vec_all* predicates use the same opcodes for two
12049      different operations, but the bits in CR6 will be different
12050      depending on what information we want.  So we have to play tricks
12051      with CR6 to get the right bits out.
12052 
12053      If you think this is disgusting, look at the specs for the
12054      AltiVec predicates.  */
12055 
12056   switch (cr6_form_int)
12057     {
12058     case 0:
12059       emit_insn (gen_cr6_test_for_zero (target));
12060       break;
12061     case 1:
12062       emit_insn (gen_cr6_test_for_zero_reverse (target));
12063       break;
12064     case 2:
12065       emit_insn (gen_cr6_test_for_lt (target));
12066       break;
12067     case 3:
12068       emit_insn (gen_cr6_test_for_lt_reverse (target));
12069       break;
12070     default:
12071       error ("argument 1 of __builtin_altivec_predicate is out of range");
12072       break;
12073     }
12074 
12075   return target;
12076 }
12077 
12078 static rtx
paired_expand_lv_builtin(enum insn_code icode,tree exp,rtx target)12079 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
12080 {
12081   rtx pat, addr;
12082   tree arg0 = CALL_EXPR_ARG (exp, 0);
12083   tree arg1 = CALL_EXPR_ARG (exp, 1);
12084   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12085   enum machine_mode mode0 = Pmode;
12086   enum machine_mode mode1 = Pmode;
12087   rtx op0 = expand_normal (arg0);
12088   rtx op1 = expand_normal (arg1);
12089 
12090   if (icode == CODE_FOR_nothing)
12091     /* Builtin not supported on this processor.  */
12092     return 0;
12093 
12094   /* If we got invalid arguments bail out before generating bad rtl.  */
12095   if (arg0 == error_mark_node || arg1 == error_mark_node)
12096     return const0_rtx;
12097 
12098   if (target == 0
12099       || GET_MODE (target) != tmode
12100       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12101     target = gen_reg_rtx (tmode);
12102 
12103   op1 = copy_to_mode_reg (mode1, op1);
12104 
12105   if (op0 == const0_rtx)
12106     {
12107       addr = gen_rtx_MEM (tmode, op1);
12108     }
12109   else
12110     {
12111       op0 = copy_to_mode_reg (mode0, op0);
12112       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
12113     }
12114 
12115   pat = GEN_FCN (icode) (target, addr);
12116 
12117   if (! pat)
12118     return 0;
12119   emit_insn (pat);
12120 
12121   return target;
12122 }
12123 
12124 /* Return a constant vector for use as a little-endian permute control vector
12125    to reverse the order of elements of the given vector mode.  */
12126 static rtx
swap_selector_for_mode(enum machine_mode mode)12127 swap_selector_for_mode (enum machine_mode mode)
12128 {
12129   /* These are little endian vectors, so their elements are reversed
12130      from what you would normally expect for a permute control vector.  */
12131   unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
12132   unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
12133   unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
12134   unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12135   unsigned int *swaparray, i;
12136   rtx perm[16];
12137 
12138   switch (mode)
12139     {
12140     case V2DFmode:
12141     case V2DImode:
12142       swaparray = swap2;
12143       break;
12144     case V4SFmode:
12145     case V4SImode:
12146       swaparray = swap4;
12147       break;
12148     case V8HImode:
12149       swaparray = swap8;
12150       break;
12151     case V16QImode:
12152       swaparray = swap16;
12153       break;
12154     default:
12155       gcc_unreachable ();
12156     }
12157 
12158   for (i = 0; i < 16; ++i)
12159     perm[i] = GEN_INT (swaparray[i]);
12160 
12161   return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
12162 }
12163 
12164 /* Generate code for an "lvx", "lvxl", or "lve*x" built-in for a little endian target
12165    with -maltivec=be specified.  Issue the load followed by an element-reversing
12166    permute.  */
12167 void
altivec_expand_lvx_be(rtx op0,rtx op1,enum machine_mode mode,unsigned unspec)12168 altivec_expand_lvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
12169 {
12170   rtx tmp = gen_reg_rtx (mode);
12171   rtx load = gen_rtx_SET (VOIDmode, tmp, op1);
12172   rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
12173   rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
12174   rtx sel = swap_selector_for_mode (mode);
12175   rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
12176 
12177   gcc_assert (REG_P (op0));
12178   emit_insn (par);
12179   emit_insn (gen_rtx_SET (VOIDmode, op0, vperm));
12180 }
12181 
12182 /* Generate code for a "stvx" or "stvxl" built-in for a little endian target
12183    with -maltivec=be specified.  Issue the store preceded by an element-reversing
12184    permute.  */
12185 void
altivec_expand_stvx_be(rtx op0,rtx op1,enum machine_mode mode,unsigned unspec)12186 altivec_expand_stvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
12187 {
12188   rtx tmp = gen_reg_rtx (mode);
12189   rtx store = gen_rtx_SET (VOIDmode, op0, tmp);
12190   rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
12191   rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
12192   rtx sel = swap_selector_for_mode (mode);
12193   rtx vperm;
12194 
12195   gcc_assert (REG_P (op1));
12196   vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
12197   emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
12198   emit_insn (par);
12199 }
12200 
12201 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
12202    specified.  Issue the store preceded by an element-reversing permute.  */
12203 void
altivec_expand_stvex_be(rtx op0,rtx op1,enum machine_mode mode,unsigned unspec)12204 altivec_expand_stvex_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
12205 {
12206   enum machine_mode inner_mode = GET_MODE_INNER (mode);
12207   rtx tmp = gen_reg_rtx (mode);
12208   rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
12209   rtx sel = swap_selector_for_mode (mode);
12210   rtx vperm;
12211 
12212   gcc_assert (REG_P (op1));
12213   vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
12214   emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
12215   emit_insn (gen_rtx_SET (VOIDmode, op0, stvx));
12216 }
12217 
12218 static rtx
altivec_expand_lv_builtin(enum insn_code icode,tree exp,rtx target,bool blk)12219 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
12220 {
12221   rtx pat, addr;
12222   tree arg0 = CALL_EXPR_ARG (exp, 0);
12223   tree arg1 = CALL_EXPR_ARG (exp, 1);
12224   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12225   enum machine_mode mode0 = Pmode;
12226   enum machine_mode mode1 = Pmode;
12227   rtx op0 = expand_normal (arg0);
12228   rtx op1 = expand_normal (arg1);
12229 
12230   if (icode == CODE_FOR_nothing)
12231     /* Builtin not supported on this processor.  */
12232     return 0;
12233 
12234   /* If we got invalid arguments bail out before generating bad rtl.  */
12235   if (arg0 == error_mark_node || arg1 == error_mark_node)
12236     return const0_rtx;
12237 
12238   if (target == 0
12239       || GET_MODE (target) != tmode
12240       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12241     target = gen_reg_rtx (tmode);
12242 
12243   op1 = copy_to_mode_reg (mode1, op1);
12244 
12245   if (op0 == const0_rtx)
12246     {
12247       addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
12248     }
12249   else
12250     {
12251       op0 = copy_to_mode_reg (mode0, op0);
12252       addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
12253     }
12254 
12255   pat = GEN_FCN (icode) (target, addr);
12256 
12257   if (! pat)
12258     return 0;
12259   emit_insn (pat);
12260 
12261   return target;
12262 }
12263 
12264 static rtx
spe_expand_stv_builtin(enum insn_code icode,tree exp)12265 spe_expand_stv_builtin (enum insn_code icode, tree exp)
12266 {
12267   tree arg0 = CALL_EXPR_ARG (exp, 0);
12268   tree arg1 = CALL_EXPR_ARG (exp, 1);
12269   tree arg2 = CALL_EXPR_ARG (exp, 2);
12270   rtx op0 = expand_normal (arg0);
12271   rtx op1 = expand_normal (arg1);
12272   rtx op2 = expand_normal (arg2);
12273   rtx pat;
12274   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
12275   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
12276   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
12277 
12278   /* Invalid arguments.  Bail before doing anything stoopid!  */
12279   if (arg0 == error_mark_node
12280       || arg1 == error_mark_node
12281       || arg2 == error_mark_node)
12282     return const0_rtx;
12283 
12284   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
12285     op0 = copy_to_mode_reg (mode2, op0);
12286   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
12287     op1 = copy_to_mode_reg (mode0, op1);
12288   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12289     op2 = copy_to_mode_reg (mode1, op2);
12290 
12291   pat = GEN_FCN (icode) (op1, op2, op0);
12292   if (pat)
12293     emit_insn (pat);
12294   return NULL_RTX;
12295 }
12296 
12297 static rtx
paired_expand_stv_builtin(enum insn_code icode,tree exp)12298 paired_expand_stv_builtin (enum insn_code icode, tree exp)
12299 {
12300   tree arg0 = CALL_EXPR_ARG (exp, 0);
12301   tree arg1 = CALL_EXPR_ARG (exp, 1);
12302   tree arg2 = CALL_EXPR_ARG (exp, 2);
12303   rtx op0 = expand_normal (arg0);
12304   rtx op1 = expand_normal (arg1);
12305   rtx op2 = expand_normal (arg2);
12306   rtx pat, addr;
12307   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12308   enum machine_mode mode1 = Pmode;
12309   enum machine_mode mode2 = Pmode;
12310 
12311   /* Invalid arguments.  Bail before doing anything stoopid!  */
12312   if (arg0 == error_mark_node
12313       || arg1 == error_mark_node
12314       || arg2 == error_mark_node)
12315     return const0_rtx;
12316 
12317   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
12318     op0 = copy_to_mode_reg (tmode, op0);
12319 
12320   op2 = copy_to_mode_reg (mode2, op2);
12321 
12322   if (op1 == const0_rtx)
12323     {
12324       addr = gen_rtx_MEM (tmode, op2);
12325     }
12326   else
12327     {
12328       op1 = copy_to_mode_reg (mode1, op1);
12329       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12330     }
12331 
12332   pat = GEN_FCN (icode) (addr, op0);
12333   if (pat)
12334     emit_insn (pat);
12335   return NULL_RTX;
12336 }
12337 
12338 static rtx
altivec_expand_stv_builtin(enum insn_code icode,tree exp)12339 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
12340 {
12341   tree arg0 = CALL_EXPR_ARG (exp, 0);
12342   tree arg1 = CALL_EXPR_ARG (exp, 1);
12343   tree arg2 = CALL_EXPR_ARG (exp, 2);
12344   rtx op0 = expand_normal (arg0);
12345   rtx op1 = expand_normal (arg1);
12346   rtx op2 = expand_normal (arg2);
12347   rtx pat, addr;
12348   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12349   enum machine_mode smode = insn_data[icode].operand[1].mode;
12350   enum machine_mode mode1 = Pmode;
12351   enum machine_mode mode2 = Pmode;
12352 
12353   /* Invalid arguments.  Bail before doing anything stoopid!  */
12354   if (arg0 == error_mark_node
12355       || arg1 == error_mark_node
12356       || arg2 == error_mark_node)
12357     return const0_rtx;
12358 
12359   if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
12360     op0 = copy_to_mode_reg (smode, op0);
12361 
12362   op2 = copy_to_mode_reg (mode2, op2);
12363 
12364   if (op1 == const0_rtx)
12365     {
12366       addr = gen_rtx_MEM (tmode, op2);
12367     }
12368   else
12369     {
12370       op1 = copy_to_mode_reg (mode1, op1);
12371       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12372     }
12373 
12374   pat = GEN_FCN (icode) (addr, op0);
12375   if (pat)
12376     emit_insn (pat);
12377   return NULL_RTX;
12378 }
12379 
12380 /* Return the appropriate SPR number associated with the given builtin.  */
12381 static inline HOST_WIDE_INT
htm_spr_num(enum rs6000_builtins code)12382 htm_spr_num (enum rs6000_builtins code)
12383 {
12384   if (code == HTM_BUILTIN_GET_TFHAR
12385       || code == HTM_BUILTIN_SET_TFHAR)
12386     return TFHAR_SPR;
12387   else if (code == HTM_BUILTIN_GET_TFIAR
12388 	   || code == HTM_BUILTIN_SET_TFIAR)
12389     return TFIAR_SPR;
12390   else if (code == HTM_BUILTIN_GET_TEXASR
12391 	   || code == HTM_BUILTIN_SET_TEXASR)
12392     return TEXASR_SPR;
12393   gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
12394 	      || code == HTM_BUILTIN_SET_TEXASRU);
12395   return TEXASRU_SPR;
12396 }
12397 
12398 /* Return the appropriate SPR regno associated with the given builtin.  */
12399 static inline HOST_WIDE_INT
htm_spr_regno(enum rs6000_builtins code)12400 htm_spr_regno (enum rs6000_builtins code)
12401 {
12402   if (code == HTM_BUILTIN_GET_TFHAR
12403       || code == HTM_BUILTIN_SET_TFHAR)
12404     return TFHAR_REGNO;
12405   else if (code == HTM_BUILTIN_GET_TFIAR
12406 	   || code == HTM_BUILTIN_SET_TFIAR)
12407     return TFIAR_REGNO;
12408   gcc_assert (code == HTM_BUILTIN_GET_TEXASR
12409 	      || code == HTM_BUILTIN_SET_TEXASR
12410 	      || code == HTM_BUILTIN_GET_TEXASRU
12411 	      || code == HTM_BUILTIN_SET_TEXASRU);
12412   return TEXASR_REGNO;
12413 }
12414 
12415 /* Return the correct ICODE value depending on whether we are
12416    setting or reading the HTM SPRs.  */
12417 static inline enum insn_code
rs6000_htm_spr_icode(bool nonvoid)12418 rs6000_htm_spr_icode (bool nonvoid)
12419 {
12420   if (nonvoid)
12421     return (TARGET_POWERPC64) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
12422   else
12423     return (TARGET_POWERPC64) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
12424 }
12425 
12426 /* Expand the HTM builtin in EXP and store the result in TARGET.
12427    Store true in *EXPANDEDP if we found a builtin to expand.  */
12428 static rtx
htm_expand_builtin(tree exp,rtx target,bool * expandedp)12429 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
12430 {
12431   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12432   bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
12433   enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12434   const struct builtin_description *d;
12435   size_t i;
12436 
12437   *expandedp = true;
12438 
12439   if (!TARGET_POWERPC64
12440       && (fcode == HTM_BUILTIN_TABORTDC
12441 	  || fcode == HTM_BUILTIN_TABORTDCI))
12442     {
12443       size_t uns_fcode = (size_t)fcode;
12444       const char *name = rs6000_builtin_info[uns_fcode].name;
12445       error ("builtin %s is only valid in 64-bit mode", name);
12446       return const0_rtx;
12447     }
12448 
12449   /* Expand the HTM builtins.  */
12450   d = bdesc_htm;
12451   for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
12452     if (d->code == fcode)
12453       {
12454 	rtx op[MAX_HTM_OPERANDS], pat;
12455 	int nopnds = 0;
12456 	tree arg;
12457 	call_expr_arg_iterator iter;
12458 	unsigned attr = rs6000_builtin_info[fcode].attr;
12459 	enum insn_code icode = d->icode;
12460 	const struct insn_operand_data *insn_op;
12461 	bool uses_spr = (attr & RS6000_BTC_SPR);
12462 	rtx cr = NULL_RTX;
12463 
12464 	if (uses_spr)
12465 	  icode = rs6000_htm_spr_icode (nonvoid);
12466         insn_op = &insn_data[icode].operand[0];
12467 
12468 	if (nonvoid)
12469 	  {
12470 	    machine_mode tmode = (uses_spr) ? insn_op->mode : SImode;
12471 	    if (!target
12472 		|| GET_MODE (target) != tmode
12473 		|| (uses_spr && !(*insn_op->predicate) (target, tmode)))
12474 	      target = gen_reg_rtx (tmode);
12475 	    if (uses_spr)
12476 	      op[nopnds++] = target;
12477 	  }
12478 
12479 	FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
12480 	{
12481 	  if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
12482 	    return const0_rtx;
12483 
12484 	  insn_op = &insn_data[icode].operand[nopnds];
12485 
12486 	  op[nopnds] = expand_normal (arg);
12487 
12488 	  if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
12489 	    {
12490 	      if (!strcmp (insn_op->constraint, "n"))
12491 		{
12492 		  int arg_num = (nonvoid) ? nopnds : nopnds + 1;
12493 		  if (!CONST_INT_P (op[nopnds]))
12494 		    error ("argument %d must be an unsigned literal", arg_num);
12495 		  else
12496 		    error ("argument %d is an unsigned literal that is "
12497 			   "out of range", arg_num);
12498 		  return const0_rtx;
12499 		}
12500 	      op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
12501 	    }
12502 
12503 	  nopnds++;
12504 	}
12505 
12506 	/* Handle the builtins for extended mnemonics.  These accept
12507 	   no arguments, but map to builtins that take arguments.  */
12508 	switch (fcode)
12509 	  {
12510 	  case HTM_BUILTIN_TENDALL:  /* Alias for: tend. 1  */
12511 	  case HTM_BUILTIN_TRESUME:  /* Alias for: tsr. 1  */
12512 	    op[nopnds++] = GEN_INT (1);
12513 #ifdef ENABLE_CHECKING
12514 	    attr |= RS6000_BTC_UNARY;
12515 #endif
12516 	    break;
12517 	  case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0  */
12518 	    op[nopnds++] = GEN_INT (0);
12519 #ifdef ENABLE_CHECKING
12520 	    attr |= RS6000_BTC_UNARY;
12521 #endif
12522 	    break;
12523 	  default:
12524 	    break;
12525 	  }
12526 
12527 	/* If this builtin accesses SPRs, then pass in the appropriate
12528 	   SPR number and SPR regno as the last two operands.  */
12529 	if (uses_spr)
12530 	  {
12531 	    machine_mode mode = (TARGET_POWERPC64) ? DImode : SImode;
12532 	    op[nopnds++] = gen_rtx_CONST_INT (mode, htm_spr_num (fcode));
12533 	    op[nopnds++] = gen_rtx_REG (mode, htm_spr_regno (fcode));
12534 	  }
12535 	/* If this builtin accesses a CR, then pass in a scratch
12536 	   CR as the last operand.  */
12537 	else if (attr & RS6000_BTC_CR)
12538 	  { cr = gen_reg_rtx (CCmode);
12539 	    op[nopnds++] = cr;
12540 	  }
12541 
12542 #ifdef ENABLE_CHECKING
12543 	int expected_nopnds = 0;
12544 	if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
12545 	  expected_nopnds = 1;
12546 	else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
12547 	  expected_nopnds = 2;
12548 	else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
12549 	  expected_nopnds = 3;
12550 	if (!(attr & RS6000_BTC_VOID))
12551 	  expected_nopnds += 1;
12552 	if (uses_spr)
12553 	  expected_nopnds += 2;
12554 
12555 	gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
12556 #endif
12557 
12558 	switch (nopnds)
12559 	  {
12560 	  case 0:
12561 	    pat = GEN_FCN (icode) (NULL_RTX);
12562 	    break;
12563 	  case 1:
12564 	    pat = GEN_FCN (icode) (op[0]);
12565 	    break;
12566 	  case 2:
12567 	    pat = GEN_FCN (icode) (op[0], op[1]);
12568 	    break;
12569 	  case 3:
12570 	    pat = GEN_FCN (icode) (op[0], op[1], op[2]);
12571 	    break;
12572 	  case 4:
12573 	    pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
12574 	    break;
12575 	  default:
12576 	    gcc_unreachable ();
12577 	  }
12578 	if (!pat)
12579 	  return NULL_RTX;
12580 	emit_insn (pat);
12581 
12582 	if (attr & RS6000_BTC_CR)
12583 	  {
12584 	    if (fcode == HTM_BUILTIN_TBEGIN)
12585 	      {
12586 		/* Emit code to set TARGET to true or false depending on
12587 		   whether the tbegin. instruction successfully or failed
12588 		   to start a transaction.  We do this by placing the 1's
12589 		   complement of CR's EQ bit into TARGET.  */
12590 		rtx scratch = gen_reg_rtx (SImode);
12591 		emit_insn (gen_rtx_SET (VOIDmode, scratch,
12592 					gen_rtx_EQ (SImode, cr,
12593 						     const0_rtx)));
12594 		emit_insn (gen_rtx_SET (VOIDmode, target,
12595 					gen_rtx_XOR (SImode, scratch,
12596 						     GEN_INT (1))));
12597 	      }
12598 	    else
12599 	      {
12600 		/* Emit code to copy the 4-bit condition register field
12601 		   CR into the least significant end of register TARGET.  */
12602 		rtx scratch1 = gen_reg_rtx (SImode);
12603 		rtx scratch2 = gen_reg_rtx (SImode);
12604 		rtx subreg = simplify_gen_subreg (CCmode, scratch1, SImode, 0);
12605 		emit_insn (gen_movcc (subreg, cr));
12606 		emit_insn (gen_lshrsi3 (scratch2, scratch1, GEN_INT (28)));
12607 		emit_insn (gen_andsi3 (target, scratch2, GEN_INT (0xf)));
12608 	      }
12609 	  }
12610 
12611 	if (nonvoid)
12612 	  return target;
12613 	return const0_rtx;
12614       }
12615 
12616   *expandedp = false;
12617   return NULL_RTX;
12618 }
12619 
12620 static rtx
rs6000_expand_ternop_builtin(enum insn_code icode,tree exp,rtx target)12621 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
12622 {
12623   rtx pat;
12624   tree arg0 = CALL_EXPR_ARG (exp, 0);
12625   tree arg1 = CALL_EXPR_ARG (exp, 1);
12626   tree arg2 = CALL_EXPR_ARG (exp, 2);
12627   rtx op0 = expand_normal (arg0);
12628   rtx op1 = expand_normal (arg1);
12629   rtx op2 = expand_normal (arg2);
12630   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12631   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12632   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12633   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
12634 
12635   if (icode == CODE_FOR_nothing)
12636     /* Builtin not supported on this processor.  */
12637     return 0;
12638 
12639   /* If we got invalid arguments bail out before generating bad rtl.  */
12640   if (arg0 == error_mark_node
12641       || arg1 == error_mark_node
12642       || arg2 == error_mark_node)
12643     return const0_rtx;
12644 
12645   /* Check and prepare argument depending on the instruction code.
12646 
12647      Note that a switch statement instead of the sequence of tests
12648      would be incorrect as many of the CODE_FOR values could be
12649      CODE_FOR_nothing and that would yield multiple alternatives
12650      with identical values.  We'd never reach here at runtime in
12651      this case.  */
12652   if (icode == CODE_FOR_altivec_vsldoi_v4sf
12653       || icode == CODE_FOR_altivec_vsldoi_v4si
12654       || icode == CODE_FOR_altivec_vsldoi_v8hi
12655       || icode == CODE_FOR_altivec_vsldoi_v16qi)
12656     {
12657       /* Only allow 4-bit unsigned literals.  */
12658       STRIP_NOPS (arg2);
12659       if (TREE_CODE (arg2) != INTEGER_CST
12660 	  || TREE_INT_CST_LOW (arg2) & ~0xf)
12661 	{
12662 	  error ("argument 3 must be a 4-bit unsigned literal");
12663 	  return const0_rtx;
12664 	}
12665     }
12666   else if (icode == CODE_FOR_vsx_xxpermdi_v2df
12667            || icode == CODE_FOR_vsx_xxpermdi_v2di
12668            || icode == CODE_FOR_vsx_xxsldwi_v16qi
12669            || icode == CODE_FOR_vsx_xxsldwi_v8hi
12670            || icode == CODE_FOR_vsx_xxsldwi_v4si
12671            || icode == CODE_FOR_vsx_xxsldwi_v4sf
12672            || icode == CODE_FOR_vsx_xxsldwi_v2di
12673            || icode == CODE_FOR_vsx_xxsldwi_v2df)
12674     {
12675       /* Only allow 2-bit unsigned literals.  */
12676       STRIP_NOPS (arg2);
12677       if (TREE_CODE (arg2) != INTEGER_CST
12678 	  || TREE_INT_CST_LOW (arg2) & ~0x3)
12679 	{
12680 	  error ("argument 3 must be a 2-bit unsigned literal");
12681 	  return const0_rtx;
12682 	}
12683     }
12684   else if (icode == CODE_FOR_vsx_set_v2df
12685            || icode == CODE_FOR_vsx_set_v2di
12686 	   || icode == CODE_FOR_bcdadd
12687 	   || icode == CODE_FOR_bcdadd_lt
12688 	   || icode == CODE_FOR_bcdadd_eq
12689 	   || icode == CODE_FOR_bcdadd_gt
12690 	   || icode == CODE_FOR_bcdsub
12691 	   || icode == CODE_FOR_bcdsub_lt
12692 	   || icode == CODE_FOR_bcdsub_eq
12693 	   || icode == CODE_FOR_bcdsub_gt)
12694     {
12695       /* Only allow 1-bit unsigned literals.  */
12696       STRIP_NOPS (arg2);
12697       if (TREE_CODE (arg2) != INTEGER_CST
12698 	  || TREE_INT_CST_LOW (arg2) & ~0x1)
12699 	{
12700 	  error ("argument 3 must be a 1-bit unsigned literal");
12701 	  return const0_rtx;
12702 	}
12703     }
12704   else if (icode == CODE_FOR_dfp_ddedpd_dd
12705            || icode == CODE_FOR_dfp_ddedpd_td)
12706     {
12707       /* Only allow 2-bit unsigned literals where the value is 0 or 2.  */
12708       STRIP_NOPS (arg0);
12709       if (TREE_CODE (arg0) != INTEGER_CST
12710 	  || TREE_INT_CST_LOW (arg2) & ~0x3)
12711 	{
12712 	  error ("argument 1 must be 0 or 2");
12713 	  return const0_rtx;
12714 	}
12715     }
12716   else if (icode == CODE_FOR_dfp_denbcd_dd
12717 	   || icode == CODE_FOR_dfp_denbcd_td)
12718     {
12719       /* Only allow 1-bit unsigned literals.  */
12720       STRIP_NOPS (arg0);
12721       if (TREE_CODE (arg0) != INTEGER_CST
12722 	  || TREE_INT_CST_LOW (arg0) & ~0x1)
12723 	{
12724 	  error ("argument 1 must be a 1-bit unsigned literal");
12725 	  return const0_rtx;
12726 	}
12727     }
12728   else if (icode == CODE_FOR_dfp_dscli_dd
12729            || icode == CODE_FOR_dfp_dscli_td
12730 	   || icode == CODE_FOR_dfp_dscri_dd
12731 	   || icode == CODE_FOR_dfp_dscri_td)
12732     {
12733       /* Only allow 6-bit unsigned literals.  */
12734       STRIP_NOPS (arg1);
12735       if (TREE_CODE (arg1) != INTEGER_CST
12736 	  || TREE_INT_CST_LOW (arg1) & ~0x3f)
12737 	{
12738 	  error ("argument 2 must be a 6-bit unsigned literal");
12739 	  return const0_rtx;
12740 	}
12741     }
12742   else if (icode == CODE_FOR_crypto_vshasigmaw
12743 	   || icode == CODE_FOR_crypto_vshasigmad)
12744     {
12745       /* Check whether the 2nd and 3rd arguments are integer constants and in
12746 	 range and prepare arguments.  */
12747       STRIP_NOPS (arg1);
12748       if (TREE_CODE (arg1) != INTEGER_CST
12749 	  || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
12750 	{
12751 	  error ("argument 2 must be 0 or 1");
12752 	  return const0_rtx;
12753 	}
12754 
12755       STRIP_NOPS (arg2);
12756       if (TREE_CODE (arg2) != INTEGER_CST
12757 	  || !IN_RANGE (TREE_INT_CST_LOW (arg2), 0, 15))
12758 	{
12759 	  error ("argument 3 must be in the range 0..15");
12760 	  return const0_rtx;
12761 	}
12762     }
12763 
12764   if (target == 0
12765       || GET_MODE (target) != tmode
12766       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12767     target = gen_reg_rtx (tmode);
12768 
12769   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12770     op0 = copy_to_mode_reg (mode0, op0);
12771   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12772     op1 = copy_to_mode_reg (mode1, op1);
12773   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12774     op2 = copy_to_mode_reg (mode2, op2);
12775 
12776   if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
12777     pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
12778   else
12779     pat = GEN_FCN (icode) (target, op0, op1, op2);
12780   if (! pat)
12781     return 0;
12782   emit_insn (pat);
12783 
12784   return target;
12785 }
12786 
12787 /* Expand the lvx builtins.  */
12788 static rtx
altivec_expand_ld_builtin(tree exp,rtx target,bool * expandedp)12789 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
12790 {
12791   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12792   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12793   tree arg0;
12794   enum machine_mode tmode, mode0;
12795   rtx pat, op0;
12796   enum insn_code icode;
12797 
12798   switch (fcode)
12799     {
12800     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
12801       icode = CODE_FOR_vector_altivec_load_v16qi;
12802       break;
12803     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
12804       icode = CODE_FOR_vector_altivec_load_v8hi;
12805       break;
12806     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
12807       icode = CODE_FOR_vector_altivec_load_v4si;
12808       break;
12809     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
12810       icode = CODE_FOR_vector_altivec_load_v4sf;
12811       break;
12812     case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
12813       icode = CODE_FOR_vector_altivec_load_v2df;
12814       break;
12815     case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
12816       icode = CODE_FOR_vector_altivec_load_v2di;
12817     case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
12818       icode = CODE_FOR_vector_altivec_load_v1ti;
12819       break;
12820     default:
12821       *expandedp = false;
12822       return NULL_RTX;
12823     }
12824 
12825   *expandedp = true;
12826 
12827   arg0 = CALL_EXPR_ARG (exp, 0);
12828   op0 = expand_normal (arg0);
12829   tmode = insn_data[icode].operand[0].mode;
12830   mode0 = insn_data[icode].operand[1].mode;
12831 
12832   if (target == 0
12833       || GET_MODE (target) != tmode
12834       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12835     target = gen_reg_rtx (tmode);
12836 
12837   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12838     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12839 
12840   pat = GEN_FCN (icode) (target, op0);
12841   if (! pat)
12842     return 0;
12843   emit_insn (pat);
12844   return target;
12845 }
12846 
12847 /* Expand the stvx builtins.  */
12848 static rtx
altivec_expand_st_builtin(tree exp,rtx target ATTRIBUTE_UNUSED,bool * expandedp)12849 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12850 			   bool *expandedp)
12851 {
12852   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12853   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12854   tree arg0, arg1;
12855   enum machine_mode mode0, mode1;
12856   rtx pat, op0, op1;
12857   enum insn_code icode;
12858 
12859   switch (fcode)
12860     {
12861     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
12862       icode = CODE_FOR_vector_altivec_store_v16qi;
12863       break;
12864     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
12865       icode = CODE_FOR_vector_altivec_store_v8hi;
12866       break;
12867     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
12868       icode = CODE_FOR_vector_altivec_store_v4si;
12869       break;
12870     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
12871       icode = CODE_FOR_vector_altivec_store_v4sf;
12872       break;
12873     case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
12874       icode = CODE_FOR_vector_altivec_store_v2df;
12875       break;
12876     case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
12877       icode = CODE_FOR_vector_altivec_store_v2di;
12878     case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
12879       icode = CODE_FOR_vector_altivec_store_v1ti;
12880       break;
12881     default:
12882       *expandedp = false;
12883       return NULL_RTX;
12884     }
12885 
12886   arg0 = CALL_EXPR_ARG (exp, 0);
12887   arg1 = CALL_EXPR_ARG (exp, 1);
12888   op0 = expand_normal (arg0);
12889   op1 = expand_normal (arg1);
12890   mode0 = insn_data[icode].operand[0].mode;
12891   mode1 = insn_data[icode].operand[1].mode;
12892 
12893   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12894     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12895   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12896     op1 = copy_to_mode_reg (mode1, op1);
12897 
12898   pat = GEN_FCN (icode) (op0, op1);
12899   if (pat)
12900     emit_insn (pat);
12901 
12902   *expandedp = true;
12903   return NULL_RTX;
12904 }
12905 
12906 /* Expand the dst builtins.  */
12907 static rtx
altivec_expand_dst_builtin(tree exp,rtx target ATTRIBUTE_UNUSED,bool * expandedp)12908 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12909 			    bool *expandedp)
12910 {
12911   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12912   enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12913   tree arg0, arg1, arg2;
12914   enum machine_mode mode0, mode1;
12915   rtx pat, op0, op1, op2;
12916   const struct builtin_description *d;
12917   size_t i;
12918 
12919   *expandedp = false;
12920 
12921   /* Handle DST variants.  */
12922   d = bdesc_dst;
12923   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12924     if (d->code == fcode)
12925       {
12926 	arg0 = CALL_EXPR_ARG (exp, 0);
12927 	arg1 = CALL_EXPR_ARG (exp, 1);
12928 	arg2 = CALL_EXPR_ARG (exp, 2);
12929 	op0 = expand_normal (arg0);
12930 	op1 = expand_normal (arg1);
12931 	op2 = expand_normal (arg2);
12932 	mode0 = insn_data[d->icode].operand[0].mode;
12933 	mode1 = insn_data[d->icode].operand[1].mode;
12934 
12935 	/* Invalid arguments, bail out before generating bad rtl.  */
12936 	if (arg0 == error_mark_node
12937 	    || arg1 == error_mark_node
12938 	    || arg2 == error_mark_node)
12939 	  return const0_rtx;
12940 
12941 	*expandedp = true;
12942 	STRIP_NOPS (arg2);
12943 	if (TREE_CODE (arg2) != INTEGER_CST
12944 	    || TREE_INT_CST_LOW (arg2) & ~0x3)
12945 	  {
12946 	    error ("argument to %qs must be a 2-bit unsigned literal", d->name);
12947 	    return const0_rtx;
12948 	  }
12949 
12950 	if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
12951 	  op0 = copy_to_mode_reg (Pmode, op0);
12952 	if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
12953 	  op1 = copy_to_mode_reg (mode1, op1);
12954 
12955 	pat = GEN_FCN (d->icode) (op0, op1, op2);
12956 	if (pat != 0)
12957 	  emit_insn (pat);
12958 
12959 	return NULL_RTX;
12960       }
12961 
12962   return NULL_RTX;
12963 }
12964 
12965 /* Expand vec_init builtin.  */
12966 static rtx
altivec_expand_vec_init_builtin(tree type,tree exp,rtx target)12967 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
12968 {
12969   enum machine_mode tmode = TYPE_MODE (type);
12970   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
12971   int i, n_elt = GET_MODE_NUNITS (tmode);
12972 
12973   gcc_assert (VECTOR_MODE_P (tmode));
12974   gcc_assert (n_elt == call_expr_nargs (exp));
12975 
12976   if (!target || !register_operand (target, tmode))
12977     target = gen_reg_rtx (tmode);
12978 
12979   /* If we have a vector compromised of a single element, such as V1TImode, do
12980      the initialization directly.  */
12981   if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
12982     {
12983       rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
12984       emit_move_insn (target, gen_lowpart (tmode, x));
12985     }
12986   else
12987     {
12988       rtvec v = rtvec_alloc (n_elt);
12989 
12990       for (i = 0; i < n_elt; ++i)
12991 	{
12992 	  rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
12993 	  RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
12994 	}
12995 
12996       rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
12997     }
12998 
12999   return target;
13000 }
13001 
13002 /* Return the integer constant in ARG.  Constrain it to be in the range
13003    of the subparts of VEC_TYPE; issue an error if not.  */
13004 
13005 static int
get_element_number(tree vec_type,tree arg)13006 get_element_number (tree vec_type, tree arg)
13007 {
13008   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
13009 
13010   if (!host_integerp (arg, 1)
13011       || (elt = tree_low_cst (arg, 1), elt > max))
13012     {
13013       error ("selector must be an integer constant in the range 0..%wi", max);
13014       return 0;
13015     }
13016 
13017   return elt;
13018 }
13019 
13020 /* Expand vec_set builtin.  */
13021 static rtx
altivec_expand_vec_set_builtin(tree exp)13022 altivec_expand_vec_set_builtin (tree exp)
13023 {
13024   enum machine_mode tmode, mode1;
13025   tree arg0, arg1, arg2;
13026   int elt;
13027   rtx op0, op1;
13028 
13029   arg0 = CALL_EXPR_ARG (exp, 0);
13030   arg1 = CALL_EXPR_ARG (exp, 1);
13031   arg2 = CALL_EXPR_ARG (exp, 2);
13032 
13033   tmode = TYPE_MODE (TREE_TYPE (arg0));
13034   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
13035   gcc_assert (VECTOR_MODE_P (tmode));
13036 
13037   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
13038   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
13039   elt = get_element_number (TREE_TYPE (arg0), arg2);
13040 
13041   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
13042     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
13043 
13044   op0 = force_reg (tmode, op0);
13045   op1 = force_reg (mode1, op1);
13046 
13047   rs6000_expand_vector_set (op0, op1, elt);
13048 
13049   return op0;
13050 }
13051 
13052 /* Expand vec_ext builtin.  */
13053 static rtx
altivec_expand_vec_ext_builtin(tree exp,rtx target)13054 altivec_expand_vec_ext_builtin (tree exp, rtx target)
13055 {
13056   enum machine_mode tmode, mode0;
13057   tree arg0, arg1;
13058   int elt;
13059   rtx op0;
13060 
13061   arg0 = CALL_EXPR_ARG (exp, 0);
13062   arg1 = CALL_EXPR_ARG (exp, 1);
13063 
13064   op0 = expand_normal (arg0);
13065   elt = get_element_number (TREE_TYPE (arg0), arg1);
13066 
13067   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
13068   mode0 = TYPE_MODE (TREE_TYPE (arg0));
13069   gcc_assert (VECTOR_MODE_P (mode0));
13070 
13071   op0 = force_reg (mode0, op0);
13072 
13073   if (optimize || !target || !register_operand (target, tmode))
13074     target = gen_reg_rtx (tmode);
13075 
13076   rs6000_expand_vector_extract (target, op0, elt);
13077 
13078   return target;
13079 }
13080 
13081 /* Expand the builtin in EXP and store the result in TARGET.  Store
13082    true in *EXPANDEDP if we found a builtin to expand.  */
13083 static rtx
altivec_expand_builtin(tree exp,rtx target,bool * expandedp)13084 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
13085 {
13086   const struct builtin_description *d;
13087   size_t i;
13088   enum insn_code icode;
13089   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13090   tree arg0;
13091   rtx op0, pat;
13092   enum machine_mode tmode, mode0;
13093   enum rs6000_builtins fcode
13094     = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13095 
13096   if (rs6000_overloaded_builtin_p (fcode))
13097     {
13098       *expandedp = true;
13099       error ("unresolved overload for Altivec builtin %qF", fndecl);
13100 
13101       /* Given it is invalid, just generate a normal call.  */
13102       return expand_call (exp, target, false);
13103     }
13104 
13105   target = altivec_expand_ld_builtin (exp, target, expandedp);
13106   if (*expandedp)
13107     return target;
13108 
13109   target = altivec_expand_st_builtin (exp, target, expandedp);
13110   if (*expandedp)
13111     return target;
13112 
13113   target = altivec_expand_dst_builtin (exp, target, expandedp);
13114   if (*expandedp)
13115     return target;
13116 
13117   *expandedp = true;
13118 
13119   switch (fcode)
13120     {
13121     case ALTIVEC_BUILTIN_STVX_V2DF:
13122       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df, exp);
13123     case ALTIVEC_BUILTIN_STVX_V2DI:
13124       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di, exp);
13125     case ALTIVEC_BUILTIN_STVX_V4SF:
13126       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf, exp);
13127     case ALTIVEC_BUILTIN_STVX:
13128     case ALTIVEC_BUILTIN_STVX_V4SI:
13129       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
13130     case ALTIVEC_BUILTIN_STVX_V8HI:
13131       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi, exp);
13132     case ALTIVEC_BUILTIN_STVX_V16QI:
13133       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi, exp);
13134     case ALTIVEC_BUILTIN_STVEBX:
13135       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
13136     case ALTIVEC_BUILTIN_STVEHX:
13137       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
13138     case ALTIVEC_BUILTIN_STVEWX:
13139       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
13140     case ALTIVEC_BUILTIN_STVXL_V2DF:
13141       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
13142     case ALTIVEC_BUILTIN_STVXL_V2DI:
13143       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
13144     case ALTIVEC_BUILTIN_STVXL_V4SF:
13145       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
13146     case ALTIVEC_BUILTIN_STVXL:
13147     case ALTIVEC_BUILTIN_STVXL_V4SI:
13148       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
13149     case ALTIVEC_BUILTIN_STVXL_V8HI:
13150       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
13151     case ALTIVEC_BUILTIN_STVXL_V16QI:
13152       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
13153 
13154     case ALTIVEC_BUILTIN_STVLX:
13155       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
13156     case ALTIVEC_BUILTIN_STVLXL:
13157       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
13158     case ALTIVEC_BUILTIN_STVRX:
13159       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
13160     case ALTIVEC_BUILTIN_STVRXL:
13161       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
13162 
13163     case VSX_BUILTIN_STXVD2X_V1TI:
13164       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
13165     case VSX_BUILTIN_STXVD2X_V2DF:
13166       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
13167     case VSX_BUILTIN_STXVD2X_V2DI:
13168       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
13169     case VSX_BUILTIN_STXVW4X_V4SF:
13170       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
13171     case VSX_BUILTIN_STXVW4X_V4SI:
13172       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
13173     case VSX_BUILTIN_STXVW4X_V8HI:
13174       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
13175     case VSX_BUILTIN_STXVW4X_V16QI:
13176       return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
13177 
13178     case ALTIVEC_BUILTIN_MFVSCR:
13179       icode = CODE_FOR_altivec_mfvscr;
13180       tmode = insn_data[icode].operand[0].mode;
13181 
13182       if (target == 0
13183 	  || GET_MODE (target) != tmode
13184 	  || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13185 	target = gen_reg_rtx (tmode);
13186 
13187       pat = GEN_FCN (icode) (target);
13188       if (! pat)
13189 	return 0;
13190       emit_insn (pat);
13191       return target;
13192 
13193     case ALTIVEC_BUILTIN_MTVSCR:
13194       icode = CODE_FOR_altivec_mtvscr;
13195       arg0 = CALL_EXPR_ARG (exp, 0);
13196       op0 = expand_normal (arg0);
13197       mode0 = insn_data[icode].operand[0].mode;
13198 
13199       /* If we got invalid arguments bail out before generating bad rtl.  */
13200       if (arg0 == error_mark_node)
13201 	return const0_rtx;
13202 
13203       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13204 	op0 = copy_to_mode_reg (mode0, op0);
13205 
13206       pat = GEN_FCN (icode) (op0);
13207       if (pat)
13208 	emit_insn (pat);
13209       return NULL_RTX;
13210 
13211     case ALTIVEC_BUILTIN_DSSALL:
13212       emit_insn (gen_altivec_dssall ());
13213       return NULL_RTX;
13214 
13215     case ALTIVEC_BUILTIN_DSS:
13216       icode = CODE_FOR_altivec_dss;
13217       arg0 = CALL_EXPR_ARG (exp, 0);
13218       STRIP_NOPS (arg0);
13219       op0 = expand_normal (arg0);
13220       mode0 = insn_data[icode].operand[0].mode;
13221 
13222       /* If we got invalid arguments bail out before generating bad rtl.  */
13223       if (arg0 == error_mark_node)
13224 	return const0_rtx;
13225 
13226       if (TREE_CODE (arg0) != INTEGER_CST
13227 	  || TREE_INT_CST_LOW (arg0) & ~0x3)
13228 	{
13229 	  error ("argument to dss must be a 2-bit unsigned literal");
13230 	  return const0_rtx;
13231 	}
13232 
13233       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13234 	op0 = copy_to_mode_reg (mode0, op0);
13235 
13236       emit_insn (gen_altivec_dss (op0));
13237       return NULL_RTX;
13238 
13239     case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
13240     case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
13241     case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
13242     case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
13243     case VSX_BUILTIN_VEC_INIT_V2DF:
13244     case VSX_BUILTIN_VEC_INIT_V2DI:
13245     case VSX_BUILTIN_VEC_INIT_V1TI:
13246       return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
13247 
13248     case ALTIVEC_BUILTIN_VEC_SET_V4SI:
13249     case ALTIVEC_BUILTIN_VEC_SET_V8HI:
13250     case ALTIVEC_BUILTIN_VEC_SET_V16QI:
13251     case ALTIVEC_BUILTIN_VEC_SET_V4SF:
13252     case VSX_BUILTIN_VEC_SET_V2DF:
13253     case VSX_BUILTIN_VEC_SET_V2DI:
13254     case VSX_BUILTIN_VEC_SET_V1TI:
13255       return altivec_expand_vec_set_builtin (exp);
13256 
13257     case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
13258     case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
13259     case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
13260     case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
13261     case VSX_BUILTIN_VEC_EXT_V2DF:
13262     case VSX_BUILTIN_VEC_EXT_V2DI:
13263     case VSX_BUILTIN_VEC_EXT_V1TI:
13264       return altivec_expand_vec_ext_builtin (exp, target);
13265 
13266     default:
13267       break;
13268       /* Fall through.  */
13269     }
13270 
13271   /* Expand abs* operations.  */
13272   d = bdesc_abs;
13273   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
13274     if (d->code == fcode)
13275       return altivec_expand_abs_builtin (d->icode, exp, target);
13276 
13277   /* Expand the AltiVec predicates.  */
13278   d = bdesc_altivec_preds;
13279   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
13280     if (d->code == fcode)
13281       return altivec_expand_predicate_builtin (d->icode, exp, target);
13282 
13283   /* LV* are funky.  We initialized them differently.  */
13284   switch (fcode)
13285     {
13286     case ALTIVEC_BUILTIN_LVSL:
13287       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
13288 					exp, target, false);
13289     case ALTIVEC_BUILTIN_LVSR:
13290       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
13291 					exp, target, false);
13292     case ALTIVEC_BUILTIN_LVEBX:
13293       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
13294 					exp, target, false);
13295     case ALTIVEC_BUILTIN_LVEHX:
13296       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
13297 					exp, target, false);
13298     case ALTIVEC_BUILTIN_LVEWX:
13299       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
13300 					exp, target, false);
13301     case ALTIVEC_BUILTIN_LVXL_V2DF:
13302       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
13303 					exp, target, false);
13304     case ALTIVEC_BUILTIN_LVXL_V2DI:
13305       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
13306 					exp, target, false);
13307     case ALTIVEC_BUILTIN_LVXL_V4SF:
13308       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
13309 					exp, target, false);
13310     case ALTIVEC_BUILTIN_LVXL:
13311     case ALTIVEC_BUILTIN_LVXL_V4SI:
13312       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
13313 					exp, target, false);
13314     case ALTIVEC_BUILTIN_LVXL_V8HI:
13315       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
13316 					exp, target, false);
13317     case ALTIVEC_BUILTIN_LVXL_V16QI:
13318       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
13319 					exp, target, false);
13320     case ALTIVEC_BUILTIN_LVX_V2DF:
13321       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df,
13322 					exp, target, false);
13323     case ALTIVEC_BUILTIN_LVX_V2DI:
13324       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di,
13325 					exp, target, false);
13326     case ALTIVEC_BUILTIN_LVX_V4SF:
13327       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf,
13328 					exp, target, false);
13329     case ALTIVEC_BUILTIN_LVX:
13330     case ALTIVEC_BUILTIN_LVX_V4SI:
13331       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
13332 					exp, target, false);
13333     case ALTIVEC_BUILTIN_LVX_V8HI:
13334       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi,
13335 					exp, target, false);
13336     case ALTIVEC_BUILTIN_LVX_V16QI:
13337       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi,
13338 					exp, target, false);
13339     case ALTIVEC_BUILTIN_LVLX:
13340       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
13341 					exp, target, true);
13342     case ALTIVEC_BUILTIN_LVLXL:
13343       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
13344 					exp, target, true);
13345     case ALTIVEC_BUILTIN_LVRX:
13346       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
13347 					exp, target, true);
13348     case ALTIVEC_BUILTIN_LVRXL:
13349       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
13350 					exp, target, true);
13351     case VSX_BUILTIN_LXVD2X_V1TI:
13352       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
13353 					exp, target, false);
13354     case VSX_BUILTIN_LXVD2X_V2DF:
13355       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
13356 					exp, target, false);
13357     case VSX_BUILTIN_LXVD2X_V2DI:
13358       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
13359 					exp, target, false);
13360     case VSX_BUILTIN_LXVW4X_V4SF:
13361       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
13362 					exp, target, false);
13363     case VSX_BUILTIN_LXVW4X_V4SI:
13364       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
13365 					exp, target, false);
13366     case VSX_BUILTIN_LXVW4X_V8HI:
13367       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
13368 					exp, target, false);
13369     case VSX_BUILTIN_LXVW4X_V16QI:
13370       return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
13371 					exp, target, false);
13372       break;
13373     default:
13374       break;
13375       /* Fall through.  */
13376     }
13377 
13378   *expandedp = false;
13379   return NULL_RTX;
13380 }
13381 
13382 /* Expand the builtin in EXP and store the result in TARGET.  Store
13383    true in *EXPANDEDP if we found a builtin to expand.  */
13384 static rtx
paired_expand_builtin(tree exp,rtx target,bool * expandedp)13385 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
13386 {
13387   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13388   enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13389   const struct builtin_description *d;
13390   size_t i;
13391 
13392   *expandedp = true;
13393 
13394   switch (fcode)
13395     {
13396     case PAIRED_BUILTIN_STX:
13397       return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
13398     case PAIRED_BUILTIN_LX:
13399       return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
13400     default:
13401       break;
13402       /* Fall through.  */
13403     }
13404 
13405   /* Expand the paired predicates.  */
13406   d = bdesc_paired_preds;
13407   for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
13408     if (d->code == fcode)
13409       return paired_expand_predicate_builtin (d->icode, exp, target);
13410 
13411   *expandedp = false;
13412   return NULL_RTX;
13413 }
13414 
13415 /* Binops that need to be initialized manually, but can be expanded
13416    automagically by rs6000_expand_binop_builtin.  */
13417 static const struct builtin_description bdesc_2arg_spe[] =
13418 {
13419   { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
13420   { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
13421   { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
13422   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
13423   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
13424   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
13425   { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
13426   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
13427   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
13428   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
13429   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
13430   { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
13431   { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
13432   { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
13433   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
13434   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
13435   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
13436   { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
13437   { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
13438   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
13439   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
13440   { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
13441 };
13442 
13443 /* Expand the builtin in EXP and store the result in TARGET.  Store
13444    true in *EXPANDEDP if we found a builtin to expand.
13445 
13446    This expands the SPE builtins that are not simple unary and binary
13447    operations.  */
13448 static rtx
spe_expand_builtin(tree exp,rtx target,bool * expandedp)13449 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
13450 {
13451   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13452   tree arg1, arg0;
13453   enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13454   enum insn_code icode;
13455   enum machine_mode tmode, mode0;
13456   rtx pat, op0;
13457   const struct builtin_description *d;
13458   size_t i;
13459 
13460   *expandedp = true;
13461 
13462   /* Syntax check for a 5-bit unsigned immediate.  */
13463   switch (fcode)
13464     {
13465     case SPE_BUILTIN_EVSTDD:
13466     case SPE_BUILTIN_EVSTDH:
13467     case SPE_BUILTIN_EVSTDW:
13468     case SPE_BUILTIN_EVSTWHE:
13469     case SPE_BUILTIN_EVSTWHO:
13470     case SPE_BUILTIN_EVSTWWE:
13471     case SPE_BUILTIN_EVSTWWO:
13472       arg1 = CALL_EXPR_ARG (exp, 2);
13473       if (TREE_CODE (arg1) != INTEGER_CST
13474 	  || TREE_INT_CST_LOW (arg1) & ~0x1f)
13475 	{
13476 	  error ("argument 2 must be a 5-bit unsigned literal");
13477 	  return const0_rtx;
13478 	}
13479       break;
13480     default:
13481       break;
13482     }
13483 
13484   /* The evsplat*i instructions are not quite generic.  */
13485   switch (fcode)
13486     {
13487     case SPE_BUILTIN_EVSPLATFI:
13488       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
13489 					 exp, target);
13490     case SPE_BUILTIN_EVSPLATI:
13491       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
13492 					 exp, target);
13493     default:
13494       break;
13495     }
13496 
13497   d = bdesc_2arg_spe;
13498   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
13499     if (d->code == fcode)
13500       return rs6000_expand_binop_builtin (d->icode, exp, target);
13501 
13502   d = bdesc_spe_predicates;
13503   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
13504     if (d->code == fcode)
13505       return spe_expand_predicate_builtin (d->icode, exp, target);
13506 
13507   d = bdesc_spe_evsel;
13508   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
13509     if (d->code == fcode)
13510       return spe_expand_evsel_builtin (d->icode, exp, target);
13511 
13512   switch (fcode)
13513     {
13514     case SPE_BUILTIN_EVSTDDX:
13515       return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
13516     case SPE_BUILTIN_EVSTDHX:
13517       return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
13518     case SPE_BUILTIN_EVSTDWX:
13519       return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
13520     case SPE_BUILTIN_EVSTWHEX:
13521       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
13522     case SPE_BUILTIN_EVSTWHOX:
13523       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
13524     case SPE_BUILTIN_EVSTWWEX:
13525       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
13526     case SPE_BUILTIN_EVSTWWOX:
13527       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
13528     case SPE_BUILTIN_EVSTDD:
13529       return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
13530     case SPE_BUILTIN_EVSTDH:
13531       return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
13532     case SPE_BUILTIN_EVSTDW:
13533       return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
13534     case SPE_BUILTIN_EVSTWHE:
13535       return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
13536     case SPE_BUILTIN_EVSTWHO:
13537       return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
13538     case SPE_BUILTIN_EVSTWWE:
13539       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
13540     case SPE_BUILTIN_EVSTWWO:
13541       return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
13542     case SPE_BUILTIN_MFSPEFSCR:
13543       icode = CODE_FOR_spe_mfspefscr;
13544       tmode = insn_data[icode].operand[0].mode;
13545 
13546       if (target == 0
13547 	  || GET_MODE (target) != tmode
13548 	  || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13549 	target = gen_reg_rtx (tmode);
13550 
13551       pat = GEN_FCN (icode) (target);
13552       if (! pat)
13553 	return 0;
13554       emit_insn (pat);
13555       return target;
13556     case SPE_BUILTIN_MTSPEFSCR:
13557       icode = CODE_FOR_spe_mtspefscr;
13558       arg0 = CALL_EXPR_ARG (exp, 0);
13559       op0 = expand_normal (arg0);
13560       mode0 = insn_data[icode].operand[0].mode;
13561 
13562       if (arg0 == error_mark_node)
13563 	return const0_rtx;
13564 
13565       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13566 	op0 = copy_to_mode_reg (mode0, op0);
13567 
13568       pat = GEN_FCN (icode) (op0);
13569       if (pat)
13570 	emit_insn (pat);
13571       return NULL_RTX;
13572     default:
13573       break;
13574     }
13575 
13576   *expandedp = false;
13577   return NULL_RTX;
13578 }
13579 
13580 static rtx
paired_expand_predicate_builtin(enum insn_code icode,tree exp,rtx target)13581 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13582 {
13583   rtx pat, scratch, tmp;
13584   tree form = CALL_EXPR_ARG (exp, 0);
13585   tree arg0 = CALL_EXPR_ARG (exp, 1);
13586   tree arg1 = CALL_EXPR_ARG (exp, 2);
13587   rtx op0 = expand_normal (arg0);
13588   rtx op1 = expand_normal (arg1);
13589   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13590   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13591   int form_int;
13592   enum rtx_code code;
13593 
13594   if (TREE_CODE (form) != INTEGER_CST)
13595     {
13596       error ("argument 1 of __builtin_paired_predicate must be a constant");
13597       return const0_rtx;
13598     }
13599   else
13600     form_int = TREE_INT_CST_LOW (form);
13601 
13602   gcc_assert (mode0 == mode1);
13603 
13604   if (arg0 == error_mark_node || arg1 == error_mark_node)
13605     return const0_rtx;
13606 
13607   if (target == 0
13608       || GET_MODE (target) != SImode
13609       || !(*insn_data[icode].operand[0].predicate) (target, SImode))
13610     target = gen_reg_rtx (SImode);
13611   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
13612     op0 = copy_to_mode_reg (mode0, op0);
13613   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
13614     op1 = copy_to_mode_reg (mode1, op1);
13615 
13616   scratch = gen_reg_rtx (CCFPmode);
13617 
13618   pat = GEN_FCN (icode) (scratch, op0, op1);
13619   if (!pat)
13620     return const0_rtx;
13621 
13622   emit_insn (pat);
13623 
13624   switch (form_int)
13625     {
13626       /* LT bit.  */
13627     case 0:
13628       code = LT;
13629       break;
13630       /* GT bit.  */
13631     case 1:
13632       code = GT;
13633       break;
13634       /* EQ bit.  */
13635     case 2:
13636       code = EQ;
13637       break;
13638       /* UN bit.  */
13639     case 3:
13640       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13641       return target;
13642     default:
13643       error ("argument 1 of __builtin_paired_predicate is out of range");
13644       return const0_rtx;
13645     }
13646 
13647   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13648   emit_move_insn (target, tmp);
13649   return target;
13650 }
13651 
13652 static rtx
spe_expand_predicate_builtin(enum insn_code icode,tree exp,rtx target)13653 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13654 {
13655   rtx pat, scratch, tmp;
13656   tree form = CALL_EXPR_ARG (exp, 0);
13657   tree arg0 = CALL_EXPR_ARG (exp, 1);
13658   tree arg1 = CALL_EXPR_ARG (exp, 2);
13659   rtx op0 = expand_normal (arg0);
13660   rtx op1 = expand_normal (arg1);
13661   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13662   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13663   int form_int;
13664   enum rtx_code code;
13665 
13666   if (TREE_CODE (form) != INTEGER_CST)
13667     {
13668       error ("argument 1 of __builtin_spe_predicate must be a constant");
13669       return const0_rtx;
13670     }
13671   else
13672     form_int = TREE_INT_CST_LOW (form);
13673 
13674   gcc_assert (mode0 == mode1);
13675 
13676   if (arg0 == error_mark_node || arg1 == error_mark_node)
13677     return const0_rtx;
13678 
13679   if (target == 0
13680       || GET_MODE (target) != SImode
13681       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
13682     target = gen_reg_rtx (SImode);
13683 
13684   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13685     op0 = copy_to_mode_reg (mode0, op0);
13686   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13687     op1 = copy_to_mode_reg (mode1, op1);
13688 
13689   scratch = gen_reg_rtx (CCmode);
13690 
13691   pat = GEN_FCN (icode) (scratch, op0, op1);
13692   if (! pat)
13693     return const0_rtx;
13694   emit_insn (pat);
13695 
13696   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
13697      _lower_.  We use one compare, but look in different bits of the
13698      CR for each variant.
13699 
13700      There are 2 elements in each SPE simd type (upper/lower).  The CR
13701      bits are set as follows:
13702 
13703      BIT0  | BIT 1  | BIT 2   | BIT 3
13704      U     |   L    | (U | L) | (U & L)
13705 
13706      So, for an "all" relationship, BIT 3 would be set.
13707      For an "any" relationship, BIT 2 would be set.  Etc.
13708 
13709      Following traditional nomenclature, these bits map to:
13710 
13711      BIT0  | BIT 1  | BIT 2   | BIT 3
13712      LT    | GT     | EQ      | OV
13713 
13714      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
13715   */
13716 
13717   switch (form_int)
13718     {
13719       /* All variant.  OV bit.  */
13720     case 0:
13721       /* We need to get to the OV bit, which is the ORDERED bit.  We
13722 	 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
13723 	 that's ugly and will make validate_condition_mode die.
13724 	 So let's just use another pattern.  */
13725       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13726       return target;
13727       /* Any variant.  EQ bit.  */
13728     case 1:
13729       code = EQ;
13730       break;
13731       /* Upper variant.  LT bit.  */
13732     case 2:
13733       code = LT;
13734       break;
13735       /* Lower variant.  GT bit.  */
13736     case 3:
13737       code = GT;
13738       break;
13739     default:
13740       error ("argument 1 of __builtin_spe_predicate is out of range");
13741       return const0_rtx;
13742     }
13743 
13744   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13745   emit_move_insn (target, tmp);
13746 
13747   return target;
13748 }
13749 
13750 /* The evsel builtins look like this:
13751 
13752      e = __builtin_spe_evsel_OP (a, b, c, d);
13753 
13754    and work like this:
13755 
13756      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
13757      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
13758 */
13759 
13760 static rtx
spe_expand_evsel_builtin(enum insn_code icode,tree exp,rtx target)13761 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
13762 {
13763   rtx pat, scratch;
13764   tree arg0 = CALL_EXPR_ARG (exp, 0);
13765   tree arg1 = CALL_EXPR_ARG (exp, 1);
13766   tree arg2 = CALL_EXPR_ARG (exp, 2);
13767   tree arg3 = CALL_EXPR_ARG (exp, 3);
13768   rtx op0 = expand_normal (arg0);
13769   rtx op1 = expand_normal (arg1);
13770   rtx op2 = expand_normal (arg2);
13771   rtx op3 = expand_normal (arg3);
13772   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13773   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13774 
13775   gcc_assert (mode0 == mode1);
13776 
13777   if (arg0 == error_mark_node || arg1 == error_mark_node
13778       || arg2 == error_mark_node || arg3 == error_mark_node)
13779     return const0_rtx;
13780 
13781   if (target == 0
13782       || GET_MODE (target) != mode0
13783       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
13784     target = gen_reg_rtx (mode0);
13785 
13786   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13787     op0 = copy_to_mode_reg (mode0, op0);
13788   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13789     op1 = copy_to_mode_reg (mode0, op1);
13790   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
13791     op2 = copy_to_mode_reg (mode0, op2);
13792   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
13793     op3 = copy_to_mode_reg (mode0, op3);
13794 
13795   /* Generate the compare.  */
13796   scratch = gen_reg_rtx (CCmode);
13797   pat = GEN_FCN (icode) (scratch, op0, op1);
13798   if (! pat)
13799     return const0_rtx;
13800   emit_insn (pat);
13801 
13802   if (mode0 == V2SImode)
13803     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
13804   else
13805     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
13806 
13807   return target;
13808 }
13809 
13810 /* Raise an error message for a builtin function that is called without the
13811    appropriate target options being set.  */
13812 
13813 static void
rs6000_invalid_builtin(enum rs6000_builtins fncode)13814 rs6000_invalid_builtin (enum rs6000_builtins fncode)
13815 {
13816   size_t uns_fncode = (size_t)fncode;
13817   const char *name = rs6000_builtin_info[uns_fncode].name;
13818   HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
13819 
13820   gcc_assert (name != NULL);
13821   if ((fnmask & RS6000_BTM_CELL) != 0)
13822     error ("Builtin function %s is only valid for the cell processor", name);
13823   else if ((fnmask & RS6000_BTM_VSX) != 0)
13824     error ("Builtin function %s requires the -mvsx option", name);
13825   else if ((fnmask & RS6000_BTM_HTM) != 0)
13826     error ("Builtin function %s requires the -mhtm option", name);
13827   else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
13828     error ("Builtin function %s requires the -maltivec option", name);
13829   else if ((fnmask & RS6000_BTM_PAIRED) != 0)
13830     error ("Builtin function %s requires the -mpaired option", name);
13831   else if ((fnmask & RS6000_BTM_SPE) != 0)
13832     error ("Builtin function %s requires the -mspe option", name);
13833   else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
13834 	   == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
13835     error ("Builtin function %s requires the -mhard-dfp and"
13836 	   " -mpower8-vector options", name);
13837   else if ((fnmask & RS6000_BTM_DFP) != 0)
13838     error ("Builtin function %s requires the -mhard-dfp option", name);
13839   else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
13840     error ("Builtin function %s requires the -mpower8-vector option", name);
13841   else if ((fnmask & (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
13842 	   == (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
13843     error ("Builtin function %s requires the -mhard-float and"
13844 	   " -mlong-double-128 options", name);
13845   else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
13846     error ("Builtin function %s requires the -mhard-float option", name);
13847   else
13848     error ("Builtin function %s is not supported with the current options",
13849 	   name);
13850 }
13851 
13852 /* Expand an expression EXP that calls a built-in function,
13853    with result going to TARGET if that's convenient
13854    (and in mode MODE if that's convenient).
13855    SUBTARGET may be used as the target for computing one of EXP's operands.
13856    IGNORE is nonzero if the value is to be ignored.  */
13857 
13858 static rtx
rs6000_expand_builtin(tree exp,rtx target,rtx subtarget ATTRIBUTE_UNUSED,enum machine_mode mode ATTRIBUTE_UNUSED,int ignore ATTRIBUTE_UNUSED)13859 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13860 		       enum machine_mode mode ATTRIBUTE_UNUSED,
13861 		       int ignore ATTRIBUTE_UNUSED)
13862 {
13863   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13864   enum rs6000_builtins fcode
13865     = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
13866   size_t uns_fcode = (size_t)fcode;
13867   const struct builtin_description *d;
13868   size_t i;
13869   rtx ret;
13870   bool success;
13871   HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
13872   bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
13873 
13874   if (TARGET_DEBUG_BUILTIN)
13875     {
13876       enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
13877       const char *name1 = rs6000_builtin_info[uns_fcode].name;
13878       const char *name2 = ((icode != CODE_FOR_nothing)
13879 			   ? get_insn_name ((int)icode)
13880 			   : "nothing");
13881       const char *name3;
13882 
13883       switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
13884 	{
13885 	default:		   name3 = "unknown";	break;
13886 	case RS6000_BTC_SPECIAL:   name3 = "special";	break;
13887 	case RS6000_BTC_UNARY:	   name3 = "unary";	break;
13888 	case RS6000_BTC_BINARY:	   name3 = "binary";	break;
13889 	case RS6000_BTC_TERNARY:   name3 = "ternary";	break;
13890 	case RS6000_BTC_PREDICATE: name3 = "predicate";	break;
13891 	case RS6000_BTC_ABS:	   name3 = "abs";	break;
13892 	case RS6000_BTC_EVSEL:	   name3 = "evsel";	break;
13893 	case RS6000_BTC_DST:	   name3 = "dst";	break;
13894 	}
13895 
13896 
13897       fprintf (stderr,
13898 	       "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
13899 	       (name1) ? name1 : "---", fcode,
13900 	       (name2) ? name2 : "---", (int)icode,
13901 	       name3,
13902 	       func_valid_p ? "" : ", not valid");
13903     }
13904 
13905   if (!func_valid_p)
13906     {
13907       rs6000_invalid_builtin (fcode);
13908 
13909       /* Given it is invalid, just generate a normal call.  */
13910       return expand_call (exp, target, ignore);
13911     }
13912 
13913   switch (fcode)
13914     {
13915     case RS6000_BUILTIN_RECIP:
13916       return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
13917 
13918     case RS6000_BUILTIN_RECIPF:
13919       return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
13920 
13921     case RS6000_BUILTIN_RSQRTF:
13922       return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
13923 
13924     case RS6000_BUILTIN_RSQRT:
13925       return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
13926 
13927     case POWER7_BUILTIN_BPERMD:
13928       return rs6000_expand_binop_builtin (((TARGET_64BIT)
13929 					   ? CODE_FOR_bpermd_di
13930 					   : CODE_FOR_bpermd_si), exp, target);
13931 
13932     case RS6000_BUILTIN_GET_TB:
13933       return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
13934 					   target);
13935 
13936     case RS6000_BUILTIN_MFTB:
13937       return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
13938 					    ? CODE_FOR_rs6000_mftb_di
13939 					    : CODE_FOR_rs6000_mftb_si),
13940 					   target);
13941 
13942     case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
13943     case ALTIVEC_BUILTIN_MASK_FOR_STORE:
13944       {
13945 	int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
13946 		     : (int) CODE_FOR_altivec_lvsl_direct);
13947 	enum machine_mode tmode = insn_data[icode].operand[0].mode;
13948 	enum machine_mode mode = insn_data[icode].operand[1].mode;
13949 	tree arg;
13950 	rtx op, addr, pat;
13951 
13952 	gcc_assert (TARGET_ALTIVEC);
13953 
13954 	arg = CALL_EXPR_ARG (exp, 0);
13955 	gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
13956 	op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
13957 	addr = memory_address (mode, op);
13958 	if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
13959 	  op = addr;
13960 	else
13961 	  {
13962 	    /* For the load case need to negate the address.  */
13963 	    op = gen_reg_rtx (GET_MODE (addr));
13964 	    emit_insn (gen_rtx_SET (VOIDmode, op,
13965 				    gen_rtx_NEG (GET_MODE (addr), addr)));
13966 	  }
13967 	op = gen_rtx_MEM (mode, op);
13968 
13969 	if (target == 0
13970 	    || GET_MODE (target) != tmode
13971 	    || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13972 	  target = gen_reg_rtx (tmode);
13973 
13974 	pat = GEN_FCN (icode) (target, op);
13975 	if (!pat)
13976 	  return 0;
13977 	emit_insn (pat);
13978 
13979 	return target;
13980       }
13981 
13982     case ALTIVEC_BUILTIN_VCFUX:
13983     case ALTIVEC_BUILTIN_VCFSX:
13984     case ALTIVEC_BUILTIN_VCTUXS:
13985     case ALTIVEC_BUILTIN_VCTSXS:
13986   /* FIXME: There's got to be a nicer way to handle this case than
13987      constructing a new CALL_EXPR.  */
13988       if (call_expr_nargs (exp) == 1)
13989 	{
13990 	  exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
13991 				 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
13992 	}
13993       break;
13994 
13995     default:
13996       break;
13997     }
13998 
13999   if (TARGET_ALTIVEC)
14000     {
14001       ret = altivec_expand_builtin (exp, target, &success);
14002 
14003       if (success)
14004 	return ret;
14005     }
14006   if (TARGET_SPE)
14007     {
14008       ret = spe_expand_builtin (exp, target, &success);
14009 
14010       if (success)
14011 	return ret;
14012     }
14013   if (TARGET_PAIRED_FLOAT)
14014     {
14015       ret = paired_expand_builtin (exp, target, &success);
14016 
14017       if (success)
14018 	return ret;
14019     }
14020   if (TARGET_HTM)
14021     {
14022       ret = htm_expand_builtin (exp, target, &success);
14023 
14024       if (success)
14025 	return ret;
14026     }
14027 
14028   unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
14029   gcc_assert (attr == RS6000_BTC_UNARY
14030 	      || attr == RS6000_BTC_BINARY
14031 	      || attr == RS6000_BTC_TERNARY);
14032 
14033   /* Handle simple unary operations.  */
14034   d = bdesc_1arg;
14035   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
14036     if (d->code == fcode)
14037       return rs6000_expand_unop_builtin (d->icode, exp, target);
14038 
14039   /* Handle simple binary operations.  */
14040   d = bdesc_2arg;
14041   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14042     if (d->code == fcode)
14043       return rs6000_expand_binop_builtin (d->icode, exp, target);
14044 
14045   /* Handle simple ternary operations.  */
14046   d = bdesc_3arg;
14047   for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
14048     if (d->code == fcode)
14049       return rs6000_expand_ternop_builtin (d->icode, exp, target);
14050 
14051   gcc_unreachable ();
14052 }
14053 
14054 static void
rs6000_init_builtins(void)14055 rs6000_init_builtins (void)
14056 {
14057   tree tdecl;
14058   tree ftype;
14059   enum machine_mode mode;
14060 
14061   if (TARGET_DEBUG_BUILTIN)
14062     fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
14063 	     (TARGET_PAIRED_FLOAT) ? ", paired"	 : "",
14064 	     (TARGET_SPE)	   ? ", spe"	 : "",
14065 	     (TARGET_ALTIVEC)	   ? ", altivec" : "",
14066 	     (TARGET_VSX)	   ? ", vsx"	 : "");
14067 
14068   V2SI_type_node = build_vector_type (intSI_type_node, 2);
14069   V2SF_type_node = build_vector_type (float_type_node, 2);
14070   V2DI_type_node = build_vector_type (intDI_type_node, 2);
14071   V2DF_type_node = build_vector_type (double_type_node, 2);
14072   V4HI_type_node = build_vector_type (intHI_type_node, 4);
14073   V4SI_type_node = build_vector_type (intSI_type_node, 4);
14074   V4SF_type_node = build_vector_type (float_type_node, 4);
14075   V8HI_type_node = build_vector_type (intHI_type_node, 8);
14076   V16QI_type_node = build_vector_type (intQI_type_node, 16);
14077 
14078   unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
14079   unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
14080   unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
14081   unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
14082 
14083   opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
14084   opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
14085   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
14086   opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
14087 
14088   /* We use V1TI mode as a special container to hold __int128_t items that
14089      must live in VSX registers.  */
14090   if (intTI_type_node)
14091     {
14092       V1TI_type_node = build_vector_type (intTI_type_node, 1);
14093       unsigned_V1TI_type_node = build_vector_type (unsigned_intTI_type_node, 1);
14094     }
14095 
14096   /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
14097      types, especially in C++ land.  Similarly, 'vector pixel' is distinct from
14098      'vector unsigned short'.  */
14099 
14100   bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
14101   bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
14102   bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
14103   bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
14104   pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
14105 
14106   long_integer_type_internal_node = long_integer_type_node;
14107   long_unsigned_type_internal_node = long_unsigned_type_node;
14108   long_long_integer_type_internal_node = long_long_integer_type_node;
14109   long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
14110   intQI_type_internal_node = intQI_type_node;
14111   uintQI_type_internal_node = unsigned_intQI_type_node;
14112   intHI_type_internal_node = intHI_type_node;
14113   uintHI_type_internal_node = unsigned_intHI_type_node;
14114   intSI_type_internal_node = intSI_type_node;
14115   uintSI_type_internal_node = unsigned_intSI_type_node;
14116   intDI_type_internal_node = intDI_type_node;
14117   uintDI_type_internal_node = unsigned_intDI_type_node;
14118   intTI_type_internal_node = intTI_type_node;
14119   uintTI_type_internal_node = unsigned_intTI_type_node;
14120   float_type_internal_node = float_type_node;
14121   double_type_internal_node = double_type_node;
14122   long_double_type_internal_node = long_double_type_node;
14123   dfloat64_type_internal_node = dfloat64_type_node;
14124   dfloat128_type_internal_node = dfloat128_type_node;
14125   void_type_internal_node = void_type_node;
14126 
14127   /* Initialize the modes for builtin_function_type, mapping a machine mode to
14128      tree type node.  */
14129   builtin_mode_to_type[QImode][0] = integer_type_node;
14130   builtin_mode_to_type[HImode][0] = integer_type_node;
14131   builtin_mode_to_type[SImode][0] = intSI_type_node;
14132   builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
14133   builtin_mode_to_type[DImode][0] = intDI_type_node;
14134   builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
14135   builtin_mode_to_type[TImode][0] = intTI_type_node;
14136   builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
14137   builtin_mode_to_type[SFmode][0] = float_type_node;
14138   builtin_mode_to_type[DFmode][0] = double_type_node;
14139   builtin_mode_to_type[TFmode][0] = long_double_type_node;
14140   builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
14141   builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
14142   builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
14143   builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
14144   builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
14145   builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
14146   builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
14147   builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
14148   builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
14149   builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
14150   builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
14151   builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
14152   builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
14153   builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
14154   builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
14155   builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
14156   builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
14157 
14158   tdecl = add_builtin_type ("__bool char", bool_char_type_node);
14159   TYPE_NAME (bool_char_type_node) = tdecl;
14160 
14161   tdecl = add_builtin_type ("__bool short", bool_short_type_node);
14162   TYPE_NAME (bool_short_type_node) = tdecl;
14163 
14164   tdecl = add_builtin_type ("__bool int", bool_int_type_node);
14165   TYPE_NAME (bool_int_type_node) = tdecl;
14166 
14167   tdecl = add_builtin_type ("__pixel", pixel_type_node);
14168   TYPE_NAME (pixel_type_node) = tdecl;
14169 
14170   bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
14171   bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
14172   bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
14173   bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
14174   pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
14175 
14176   tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
14177   TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
14178 
14179   tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
14180   TYPE_NAME (V16QI_type_node) = tdecl;
14181 
14182   tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
14183   TYPE_NAME ( bool_V16QI_type_node) = tdecl;
14184 
14185   tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
14186   TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
14187 
14188   tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
14189   TYPE_NAME (V8HI_type_node) = tdecl;
14190 
14191   tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
14192   TYPE_NAME (bool_V8HI_type_node) = tdecl;
14193 
14194   tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
14195   TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
14196 
14197   tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
14198   TYPE_NAME (V4SI_type_node) = tdecl;
14199 
14200   tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
14201   TYPE_NAME (bool_V4SI_type_node) = tdecl;
14202 
14203   tdecl = add_builtin_type ("__vector float", V4SF_type_node);
14204   TYPE_NAME (V4SF_type_node) = tdecl;
14205 
14206   tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
14207   TYPE_NAME (pixel_V8HI_type_node) = tdecl;
14208 
14209   tdecl = add_builtin_type ("__vector double", V2DF_type_node);
14210   TYPE_NAME (V2DF_type_node) = tdecl;
14211 
14212   if (TARGET_POWERPC64)
14213     {
14214       tdecl = add_builtin_type ("__vector long", V2DI_type_node);
14215       TYPE_NAME (V2DI_type_node) = tdecl;
14216 
14217       tdecl = add_builtin_type ("__vector unsigned long",
14218 				unsigned_V2DI_type_node);
14219       TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
14220 
14221       tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
14222       TYPE_NAME (bool_V2DI_type_node) = tdecl;
14223     }
14224   else
14225     {
14226       tdecl = add_builtin_type ("__vector long long", V2DI_type_node);
14227       TYPE_NAME (V2DI_type_node) = tdecl;
14228 
14229       tdecl = add_builtin_type ("__vector unsigned long long",
14230 				unsigned_V2DI_type_node);
14231       TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
14232 
14233       tdecl = add_builtin_type ("__vector __bool long long",
14234 				bool_V2DI_type_node);
14235       TYPE_NAME (bool_V2DI_type_node) = tdecl;
14236     }
14237 
14238   if (V1TI_type_node)
14239     {
14240       tdecl = add_builtin_type ("__vector __int128", V1TI_type_node);
14241       TYPE_NAME (V1TI_type_node) = tdecl;
14242 
14243       tdecl = add_builtin_type ("__vector unsigned __int128",
14244 				unsigned_V1TI_type_node);
14245       TYPE_NAME (unsigned_V1TI_type_node) = tdecl;
14246     }
14247 
14248   /* Paired and SPE builtins are only available if you build a compiler with
14249      the appropriate options, so only create those builtins with the
14250      appropriate compiler option.  Create Altivec and VSX builtins on machines
14251      with at least the general purpose extensions (970 and newer) to allow the
14252      use of the target attribute.  */
14253   if (TARGET_PAIRED_FLOAT)
14254     paired_init_builtins ();
14255   if (TARGET_SPE)
14256     spe_init_builtins ();
14257   if (TARGET_EXTRA_BUILTINS)
14258     altivec_init_builtins ();
14259   if (TARGET_HTM)
14260     htm_init_builtins ();
14261 
14262   if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
14263     rs6000_common_init_builtins ();
14264 
14265   ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
14266 				 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
14267   def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
14268 
14269   ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
14270 				 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
14271   def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
14272 
14273   ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
14274 				 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
14275   def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
14276 
14277   ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
14278 				 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
14279   def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
14280 
14281   mode = (TARGET_64BIT) ? DImode : SImode;
14282   ftype = builtin_function_type (mode, mode, mode, VOIDmode,
14283 				 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
14284   def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
14285 
14286   ftype = build_function_type_list (unsigned_intDI_type_node,
14287 				    NULL_TREE);
14288   def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
14289 
14290   if (TARGET_64BIT)
14291     ftype = build_function_type_list (unsigned_intDI_type_node,
14292 				      NULL_TREE);
14293   else
14294     ftype = build_function_type_list (unsigned_intSI_type_node,
14295 				      NULL_TREE);
14296   def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
14297 
14298 #if TARGET_XCOFF
14299   /* AIX libm provides clog as __clog.  */
14300   if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
14301     set_user_assembler_name (tdecl, "__clog");
14302 #endif
14303 
14304 #ifdef SUBTARGET_INIT_BUILTINS
14305   SUBTARGET_INIT_BUILTINS;
14306 #endif
14307 }
14308 
14309 /* Returns the rs6000 builtin decl for CODE.  */
14310 
14311 static tree
rs6000_builtin_decl(unsigned code,bool initialize_p ATTRIBUTE_UNUSED)14312 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
14313 {
14314   HOST_WIDE_INT fnmask;
14315 
14316   if (code >= RS6000_BUILTIN_COUNT)
14317     return error_mark_node;
14318 
14319   fnmask = rs6000_builtin_info[code].mask;
14320   if ((fnmask & rs6000_builtin_mask) != fnmask)
14321     {
14322       rs6000_invalid_builtin ((enum rs6000_builtins)code);
14323       return error_mark_node;
14324     }
14325 
14326   return rs6000_builtin_decls[code];
14327 }
14328 
14329 static void
spe_init_builtins(void)14330 spe_init_builtins (void)
14331 {
14332   tree puint_type_node = build_pointer_type (unsigned_type_node);
14333   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
14334   const struct builtin_description *d;
14335   size_t i;
14336 
14337   tree v2si_ftype_4_v2si
14338     = build_function_type_list (opaque_V2SI_type_node,
14339                                 opaque_V2SI_type_node,
14340                                 opaque_V2SI_type_node,
14341                                 opaque_V2SI_type_node,
14342                                 opaque_V2SI_type_node,
14343                                 NULL_TREE);
14344 
14345   tree v2sf_ftype_4_v2sf
14346     = build_function_type_list (opaque_V2SF_type_node,
14347                                 opaque_V2SF_type_node,
14348                                 opaque_V2SF_type_node,
14349                                 opaque_V2SF_type_node,
14350                                 opaque_V2SF_type_node,
14351                                 NULL_TREE);
14352 
14353   tree int_ftype_int_v2si_v2si
14354     = build_function_type_list (integer_type_node,
14355                                 integer_type_node,
14356                                 opaque_V2SI_type_node,
14357                                 opaque_V2SI_type_node,
14358                                 NULL_TREE);
14359 
14360   tree int_ftype_int_v2sf_v2sf
14361     = build_function_type_list (integer_type_node,
14362                                 integer_type_node,
14363                                 opaque_V2SF_type_node,
14364                                 opaque_V2SF_type_node,
14365                                 NULL_TREE);
14366 
14367   tree void_ftype_v2si_puint_int
14368     = build_function_type_list (void_type_node,
14369                                 opaque_V2SI_type_node,
14370                                 puint_type_node,
14371                                 integer_type_node,
14372                                 NULL_TREE);
14373 
14374   tree void_ftype_v2si_puint_char
14375     = build_function_type_list (void_type_node,
14376                                 opaque_V2SI_type_node,
14377                                 puint_type_node,
14378                                 char_type_node,
14379                                 NULL_TREE);
14380 
14381   tree void_ftype_v2si_pv2si_int
14382     = build_function_type_list (void_type_node,
14383                                 opaque_V2SI_type_node,
14384                                 opaque_p_V2SI_type_node,
14385                                 integer_type_node,
14386                                 NULL_TREE);
14387 
14388   tree void_ftype_v2si_pv2si_char
14389     = build_function_type_list (void_type_node,
14390                                 opaque_V2SI_type_node,
14391                                 opaque_p_V2SI_type_node,
14392                                 char_type_node,
14393                                 NULL_TREE);
14394 
14395   tree void_ftype_int
14396     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14397 
14398   tree int_ftype_void
14399     = build_function_type_list (integer_type_node, NULL_TREE);
14400 
14401   tree v2si_ftype_pv2si_int
14402     = build_function_type_list (opaque_V2SI_type_node,
14403                                 opaque_p_V2SI_type_node,
14404                                 integer_type_node,
14405                                 NULL_TREE);
14406 
14407   tree v2si_ftype_puint_int
14408     = build_function_type_list (opaque_V2SI_type_node,
14409                                 puint_type_node,
14410                                 integer_type_node,
14411                                 NULL_TREE);
14412 
14413   tree v2si_ftype_pushort_int
14414     = build_function_type_list (opaque_V2SI_type_node,
14415                                 pushort_type_node,
14416                                 integer_type_node,
14417                                 NULL_TREE);
14418 
14419   tree v2si_ftype_signed_char
14420     = build_function_type_list (opaque_V2SI_type_node,
14421                                 signed_char_type_node,
14422                                 NULL_TREE);
14423 
14424   add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
14425 
14426   /* Initialize irregular SPE builtins.  */
14427 
14428   def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
14429   def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
14430   def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
14431   def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
14432   def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
14433   def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
14434   def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
14435   def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
14436   def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
14437   def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
14438   def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
14439   def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
14440   def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
14441   def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
14442   def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
14443   def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
14444   def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
14445   def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
14446 
14447   /* Loads.  */
14448   def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
14449   def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
14450   def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
14451   def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
14452   def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
14453   def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
14454   def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
14455   def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
14456   def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
14457   def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
14458   def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
14459   def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
14460   def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
14461   def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
14462   def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
14463   def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
14464   def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
14465   def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
14466   def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
14467   def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
14468   def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
14469   def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
14470 
14471   /* Predicates.  */
14472   d = bdesc_spe_predicates;
14473   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
14474     {
14475       tree type;
14476 
14477       switch (insn_data[d->icode].operand[1].mode)
14478 	{
14479 	case V2SImode:
14480 	  type = int_ftype_int_v2si_v2si;
14481 	  break;
14482 	case V2SFmode:
14483 	  type = int_ftype_int_v2sf_v2sf;
14484 	  break;
14485 	default:
14486 	  gcc_unreachable ();
14487 	}
14488 
14489       def_builtin (d->name, type, d->code);
14490     }
14491 
14492   /* Evsel predicates.  */
14493   d = bdesc_spe_evsel;
14494   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
14495     {
14496       tree type;
14497 
14498       switch (insn_data[d->icode].operand[1].mode)
14499 	{
14500 	case V2SImode:
14501 	  type = v2si_ftype_4_v2si;
14502 	  break;
14503 	case V2SFmode:
14504 	  type = v2sf_ftype_4_v2sf;
14505 	  break;
14506 	default:
14507 	  gcc_unreachable ();
14508 	}
14509 
14510       def_builtin (d->name, type, d->code);
14511     }
14512 }
14513 
14514 static void
paired_init_builtins(void)14515 paired_init_builtins (void)
14516 {
14517   const struct builtin_description *d;
14518   size_t i;
14519 
14520    tree int_ftype_int_v2sf_v2sf
14521     = build_function_type_list (integer_type_node,
14522                                 integer_type_node,
14523                                 V2SF_type_node,
14524                                 V2SF_type_node,
14525                                 NULL_TREE);
14526   tree pcfloat_type_node =
14527     build_pointer_type (build_qualified_type
14528 			(float_type_node, TYPE_QUAL_CONST));
14529 
14530   tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
14531 							   long_integer_type_node,
14532 							   pcfloat_type_node,
14533 							   NULL_TREE);
14534   tree void_ftype_v2sf_long_pcfloat =
14535     build_function_type_list (void_type_node,
14536 			      V2SF_type_node,
14537 			      long_integer_type_node,
14538 			      pcfloat_type_node,
14539 			      NULL_TREE);
14540 
14541 
14542   def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
14543 	       PAIRED_BUILTIN_LX);
14544 
14545 
14546   def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
14547 	       PAIRED_BUILTIN_STX);
14548 
14549   /* Predicates.  */
14550   d = bdesc_paired_preds;
14551   for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
14552     {
14553       tree type;
14554 
14555       if (TARGET_DEBUG_BUILTIN)
14556 	fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
14557 		 (int)i, get_insn_name (d->icode), (int)d->icode,
14558 		 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
14559 
14560       switch (insn_data[d->icode].operand[1].mode)
14561 	{
14562 	case V2SFmode:
14563 	  type = int_ftype_int_v2sf_v2sf;
14564 	  break;
14565 	default:
14566 	  gcc_unreachable ();
14567 	}
14568 
14569       def_builtin (d->name, type, d->code);
14570     }
14571 }
14572 
14573 static void
altivec_init_builtins(void)14574 altivec_init_builtins (void)
14575 {
14576   const struct builtin_description *d;
14577   size_t i;
14578   tree ftype;
14579   tree decl;
14580 
14581   tree pvoid_type_node = build_pointer_type (void_type_node);
14582 
14583   tree pcvoid_type_node
14584     = build_pointer_type (build_qualified_type (void_type_node,
14585 						TYPE_QUAL_CONST));
14586 
14587   tree int_ftype_opaque
14588     = build_function_type_list (integer_type_node,
14589 				opaque_V4SI_type_node, NULL_TREE);
14590   tree opaque_ftype_opaque
14591     = build_function_type_list (integer_type_node, NULL_TREE);
14592   tree opaque_ftype_opaque_int
14593     = build_function_type_list (opaque_V4SI_type_node,
14594 				opaque_V4SI_type_node, integer_type_node, NULL_TREE);
14595   tree opaque_ftype_opaque_opaque_int
14596     = build_function_type_list (opaque_V4SI_type_node,
14597 				opaque_V4SI_type_node, opaque_V4SI_type_node,
14598 				integer_type_node, NULL_TREE);
14599   tree int_ftype_int_opaque_opaque
14600     = build_function_type_list (integer_type_node,
14601                                 integer_type_node, opaque_V4SI_type_node,
14602                                 opaque_V4SI_type_node, NULL_TREE);
14603   tree int_ftype_int_v4si_v4si
14604     = build_function_type_list (integer_type_node,
14605 				integer_type_node, V4SI_type_node,
14606 				V4SI_type_node, NULL_TREE);
14607   tree int_ftype_int_v2di_v2di
14608     = build_function_type_list (integer_type_node,
14609 				integer_type_node, V2DI_type_node,
14610 				V2DI_type_node, NULL_TREE);
14611   tree void_ftype_v4si
14612     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
14613   tree v8hi_ftype_void
14614     = build_function_type_list (V8HI_type_node, NULL_TREE);
14615   tree void_ftype_void
14616     = build_function_type_list (void_type_node, NULL_TREE);
14617   tree void_ftype_int
14618     = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14619 
14620   tree opaque_ftype_long_pcvoid
14621     = build_function_type_list (opaque_V4SI_type_node,
14622 				long_integer_type_node, pcvoid_type_node,
14623 				NULL_TREE);
14624   tree v16qi_ftype_long_pcvoid
14625     = build_function_type_list (V16QI_type_node,
14626 				long_integer_type_node, pcvoid_type_node,
14627 				NULL_TREE);
14628   tree v8hi_ftype_long_pcvoid
14629     = build_function_type_list (V8HI_type_node,
14630 				long_integer_type_node, pcvoid_type_node,
14631 				NULL_TREE);
14632   tree v4si_ftype_long_pcvoid
14633     = build_function_type_list (V4SI_type_node,
14634 				long_integer_type_node, pcvoid_type_node,
14635 				NULL_TREE);
14636   tree v4sf_ftype_long_pcvoid
14637     = build_function_type_list (V4SF_type_node,
14638 				long_integer_type_node, pcvoid_type_node,
14639 				NULL_TREE);
14640   tree v2df_ftype_long_pcvoid
14641     = build_function_type_list (V2DF_type_node,
14642 				long_integer_type_node, pcvoid_type_node,
14643 				NULL_TREE);
14644   tree v2di_ftype_long_pcvoid
14645     = build_function_type_list (V2DI_type_node,
14646 				long_integer_type_node, pcvoid_type_node,
14647 				NULL_TREE);
14648 
14649   tree void_ftype_opaque_long_pvoid
14650     = build_function_type_list (void_type_node,
14651 				opaque_V4SI_type_node, long_integer_type_node,
14652 				pvoid_type_node, NULL_TREE);
14653   tree void_ftype_v4si_long_pvoid
14654     = build_function_type_list (void_type_node,
14655 				V4SI_type_node, long_integer_type_node,
14656 				pvoid_type_node, NULL_TREE);
14657   tree void_ftype_v16qi_long_pvoid
14658     = build_function_type_list (void_type_node,
14659 				V16QI_type_node, long_integer_type_node,
14660 				pvoid_type_node, NULL_TREE);
14661   tree void_ftype_v8hi_long_pvoid
14662     = build_function_type_list (void_type_node,
14663 				V8HI_type_node, long_integer_type_node,
14664 				pvoid_type_node, NULL_TREE);
14665   tree void_ftype_v4sf_long_pvoid
14666     = build_function_type_list (void_type_node,
14667 				V4SF_type_node, long_integer_type_node,
14668 				pvoid_type_node, NULL_TREE);
14669   tree void_ftype_v2df_long_pvoid
14670     = build_function_type_list (void_type_node,
14671 				V2DF_type_node, long_integer_type_node,
14672 				pvoid_type_node, NULL_TREE);
14673   tree void_ftype_v2di_long_pvoid
14674     = build_function_type_list (void_type_node,
14675 				V2DI_type_node, long_integer_type_node,
14676 				pvoid_type_node, NULL_TREE);
14677   tree int_ftype_int_v8hi_v8hi
14678     = build_function_type_list (integer_type_node,
14679 				integer_type_node, V8HI_type_node,
14680 				V8HI_type_node, NULL_TREE);
14681   tree int_ftype_int_v16qi_v16qi
14682     = build_function_type_list (integer_type_node,
14683 				integer_type_node, V16QI_type_node,
14684 				V16QI_type_node, NULL_TREE);
14685   tree int_ftype_int_v4sf_v4sf
14686     = build_function_type_list (integer_type_node,
14687 				integer_type_node, V4SF_type_node,
14688 				V4SF_type_node, NULL_TREE);
14689   tree int_ftype_int_v2df_v2df
14690     = build_function_type_list (integer_type_node,
14691 				integer_type_node, V2DF_type_node,
14692 				V2DF_type_node, NULL_TREE);
14693   tree v2di_ftype_v2di
14694     = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
14695   tree v4si_ftype_v4si
14696     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
14697   tree v8hi_ftype_v8hi
14698     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
14699   tree v16qi_ftype_v16qi
14700     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
14701   tree v4sf_ftype_v4sf
14702     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
14703   tree v2df_ftype_v2df
14704     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
14705   tree void_ftype_pcvoid_int_int
14706     = build_function_type_list (void_type_node,
14707 				pcvoid_type_node, integer_type_node,
14708 				integer_type_node, NULL_TREE);
14709 
14710   def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
14711   def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
14712   def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
14713   def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
14714   def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
14715   def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
14716   def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
14717   def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
14718   def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
14719   def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
14720   def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
14721 	       ALTIVEC_BUILTIN_LVXL_V2DF);
14722   def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
14723 	       ALTIVEC_BUILTIN_LVXL_V2DI);
14724   def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
14725 	       ALTIVEC_BUILTIN_LVXL_V4SF);
14726   def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
14727 	       ALTIVEC_BUILTIN_LVXL_V4SI);
14728   def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
14729 	       ALTIVEC_BUILTIN_LVXL_V8HI);
14730   def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
14731 	       ALTIVEC_BUILTIN_LVXL_V16QI);
14732   def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
14733   def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
14734 	       ALTIVEC_BUILTIN_LVX_V2DF);
14735   def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
14736 	       ALTIVEC_BUILTIN_LVX_V2DI);
14737   def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
14738 	       ALTIVEC_BUILTIN_LVX_V4SF);
14739   def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
14740 	       ALTIVEC_BUILTIN_LVX_V4SI);
14741   def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
14742 	       ALTIVEC_BUILTIN_LVX_V8HI);
14743   def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
14744 	       ALTIVEC_BUILTIN_LVX_V16QI);
14745   def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
14746   def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
14747 	       ALTIVEC_BUILTIN_STVX_V2DF);
14748   def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
14749 	       ALTIVEC_BUILTIN_STVX_V2DI);
14750   def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
14751 	       ALTIVEC_BUILTIN_STVX_V4SF);
14752   def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
14753 	       ALTIVEC_BUILTIN_STVX_V4SI);
14754   def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
14755 	       ALTIVEC_BUILTIN_STVX_V8HI);
14756   def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
14757 	       ALTIVEC_BUILTIN_STVX_V16QI);
14758   def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
14759   def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
14760   def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
14761 	       ALTIVEC_BUILTIN_STVXL_V2DF);
14762   def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
14763 	       ALTIVEC_BUILTIN_STVXL_V2DI);
14764   def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
14765 	       ALTIVEC_BUILTIN_STVXL_V4SF);
14766   def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
14767 	       ALTIVEC_BUILTIN_STVXL_V4SI);
14768   def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
14769 	       ALTIVEC_BUILTIN_STVXL_V8HI);
14770   def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
14771 	       ALTIVEC_BUILTIN_STVXL_V16QI);
14772   def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
14773   def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
14774   def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
14775   def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
14776   def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
14777   def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
14778   def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
14779   def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
14780   def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
14781   def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
14782   def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
14783   def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
14784   def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
14785   def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
14786   def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
14787   def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
14788 
14789   def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
14790 	       VSX_BUILTIN_LXVD2X_V2DF);
14791   def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
14792 	       VSX_BUILTIN_LXVD2X_V2DI);
14793   def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
14794 	       VSX_BUILTIN_LXVW4X_V4SF);
14795   def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
14796 	       VSX_BUILTIN_LXVW4X_V4SI);
14797   def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
14798 	       VSX_BUILTIN_LXVW4X_V8HI);
14799   def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
14800 	       VSX_BUILTIN_LXVW4X_V16QI);
14801   def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
14802 	       VSX_BUILTIN_STXVD2X_V2DF);
14803   def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
14804 	       VSX_BUILTIN_STXVD2X_V2DI);
14805   def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
14806 	       VSX_BUILTIN_STXVW4X_V4SF);
14807   def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
14808 	       VSX_BUILTIN_STXVW4X_V4SI);
14809   def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
14810 	       VSX_BUILTIN_STXVW4X_V8HI);
14811   def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
14812 	       VSX_BUILTIN_STXVW4X_V16QI);
14813   def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
14814 	       VSX_BUILTIN_VEC_LD);
14815   def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
14816 	       VSX_BUILTIN_VEC_ST);
14817 
14818   def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
14819   def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
14820   def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
14821 
14822   def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
14823   def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
14824   def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
14825   def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
14826   def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
14827   def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
14828   def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
14829   def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
14830   def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
14831   def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
14832   def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
14833   def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
14834 
14835   /* Cell builtins.  */
14836   def_builtin ("__builtin_altivec_lvlx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
14837   def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
14838   def_builtin ("__builtin_altivec_lvrx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
14839   def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
14840 
14841   def_builtin ("__builtin_vec_lvlx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
14842   def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
14843   def_builtin ("__builtin_vec_lvrx",  v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
14844   def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
14845 
14846   def_builtin ("__builtin_altivec_stvlx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
14847   def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
14848   def_builtin ("__builtin_altivec_stvrx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
14849   def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
14850 
14851   def_builtin ("__builtin_vec_stvlx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
14852   def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
14853   def_builtin ("__builtin_vec_stvrx",  void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
14854   def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
14855 
14856   /* Add the DST variants.  */
14857   d = bdesc_dst;
14858   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
14859     def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
14860 
14861   /* Initialize the predicates.  */
14862   d = bdesc_altivec_preds;
14863   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
14864     {
14865       enum machine_mode mode1;
14866       tree type;
14867 
14868       if (rs6000_overloaded_builtin_p (d->code))
14869 	mode1 = VOIDmode;
14870       else
14871 	mode1 = insn_data[d->icode].operand[1].mode;
14872 
14873       switch (mode1)
14874 	{
14875 	case VOIDmode:
14876 	  type = int_ftype_int_opaque_opaque;
14877 	  break;
14878 	case V2DImode:
14879 	  type = int_ftype_int_v2di_v2di;
14880 	  break;
14881 	case V4SImode:
14882 	  type = int_ftype_int_v4si_v4si;
14883 	  break;
14884 	case V8HImode:
14885 	  type = int_ftype_int_v8hi_v8hi;
14886 	  break;
14887 	case V16QImode:
14888 	  type = int_ftype_int_v16qi_v16qi;
14889 	  break;
14890 	case V4SFmode:
14891 	  type = int_ftype_int_v4sf_v4sf;
14892 	  break;
14893 	case V2DFmode:
14894 	  type = int_ftype_int_v2df_v2df;
14895 	  break;
14896 	default:
14897 	  gcc_unreachable ();
14898 	}
14899 
14900       def_builtin (d->name, type, d->code);
14901     }
14902 
14903   /* Initialize the abs* operators.  */
14904   d = bdesc_abs;
14905   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
14906     {
14907       enum machine_mode mode0;
14908       tree type;
14909 
14910       mode0 = insn_data[d->icode].operand[0].mode;
14911 
14912       switch (mode0)
14913 	{
14914 	case V2DImode:
14915 	  type = v2di_ftype_v2di;
14916 	  break;
14917 	case V4SImode:
14918 	  type = v4si_ftype_v4si;
14919 	  break;
14920 	case V8HImode:
14921 	  type = v8hi_ftype_v8hi;
14922 	  break;
14923 	case V16QImode:
14924 	  type = v16qi_ftype_v16qi;
14925 	  break;
14926 	case V4SFmode:
14927 	  type = v4sf_ftype_v4sf;
14928 	  break;
14929 	case V2DFmode:
14930 	  type = v2df_ftype_v2df;
14931 	  break;
14932 	default:
14933 	  gcc_unreachable ();
14934 	}
14935 
14936       def_builtin (d->name, type, d->code);
14937     }
14938 
14939   /* Initialize target builtin that implements
14940      targetm.vectorize.builtin_mask_for_load.  */
14941 
14942   decl = add_builtin_function ("__builtin_altivec_mask_for_load",
14943 			       v16qi_ftype_long_pcvoid,
14944 			       ALTIVEC_BUILTIN_MASK_FOR_LOAD,
14945 			       BUILT_IN_MD, NULL, NULL_TREE);
14946   TREE_READONLY (decl) = 1;
14947   /* Record the decl. Will be used by rs6000_builtin_mask_for_load.  */
14948   altivec_builtin_mask_for_load = decl;
14949 
14950   /* Access to the vec_init patterns.  */
14951   ftype = build_function_type_list (V4SI_type_node, integer_type_node,
14952 				    integer_type_node, integer_type_node,
14953 				    integer_type_node, NULL_TREE);
14954   def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
14955 
14956   ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
14957 				    short_integer_type_node,
14958 				    short_integer_type_node,
14959 				    short_integer_type_node,
14960 				    short_integer_type_node,
14961 				    short_integer_type_node,
14962 				    short_integer_type_node,
14963 				    short_integer_type_node, NULL_TREE);
14964   def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
14965 
14966   ftype = build_function_type_list (V16QI_type_node, char_type_node,
14967 				    char_type_node, char_type_node,
14968 				    char_type_node, char_type_node,
14969 				    char_type_node, char_type_node,
14970 				    char_type_node, char_type_node,
14971 				    char_type_node, char_type_node,
14972 				    char_type_node, char_type_node,
14973 				    char_type_node, char_type_node,
14974 				    char_type_node, NULL_TREE);
14975   def_builtin ("__builtin_vec_init_v16qi", ftype,
14976 	       ALTIVEC_BUILTIN_VEC_INIT_V16QI);
14977 
14978   ftype = build_function_type_list (V4SF_type_node, float_type_node,
14979 				    float_type_node, float_type_node,
14980 				    float_type_node, NULL_TREE);
14981   def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
14982 
14983   /* VSX builtins.  */
14984   ftype = build_function_type_list (V2DF_type_node, double_type_node,
14985 				    double_type_node, NULL_TREE);
14986   def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
14987 
14988   ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
14989 				    intDI_type_node, NULL_TREE);
14990   def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
14991 
14992   /* Access to the vec_set patterns.  */
14993   ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
14994 				    intSI_type_node,
14995 				    integer_type_node, NULL_TREE);
14996   def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
14997 
14998   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
14999 				    intHI_type_node,
15000 				    integer_type_node, NULL_TREE);
15001   def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
15002 
15003   ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
15004 				    intQI_type_node,
15005 				    integer_type_node, NULL_TREE);
15006   def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
15007 
15008   ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
15009 				    float_type_node,
15010 				    integer_type_node, NULL_TREE);
15011   def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
15012 
15013   ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
15014 				    double_type_node,
15015 				    integer_type_node, NULL_TREE);
15016   def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
15017 
15018   ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
15019 				    intDI_type_node,
15020 				    integer_type_node, NULL_TREE);
15021   def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
15022 
15023   /* Access to the vec_extract patterns.  */
15024   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
15025 				    integer_type_node, NULL_TREE);
15026   def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
15027 
15028   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
15029 				    integer_type_node, NULL_TREE);
15030   def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
15031 
15032   ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
15033 				    integer_type_node, NULL_TREE);
15034   def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
15035 
15036   ftype = build_function_type_list (float_type_node, V4SF_type_node,
15037 				    integer_type_node, NULL_TREE);
15038   def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
15039 
15040   ftype = build_function_type_list (double_type_node, V2DF_type_node,
15041 				    integer_type_node, NULL_TREE);
15042   def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
15043 
15044   ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
15045 				    integer_type_node, NULL_TREE);
15046   def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
15047 
15048 
15049   if (V1TI_type_node)
15050     {
15051       tree v1ti_ftype_long_pcvoid
15052 	= build_function_type_list (V1TI_type_node,
15053 				    long_integer_type_node, pcvoid_type_node,
15054 				    NULL_TREE);
15055       tree void_ftype_v1ti_long_pvoid
15056 	= build_function_type_list (void_type_node,
15057 				    V1TI_type_node, long_integer_type_node,
15058 				    pvoid_type_node, NULL_TREE);
15059       def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
15060 		   VSX_BUILTIN_LXVD2X_V1TI);
15061       def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
15062 		   VSX_BUILTIN_STXVD2X_V1TI);
15063       ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
15064 					NULL_TREE, NULL_TREE);
15065       def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
15066       ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
15067 					intTI_type_node,
15068 					integer_type_node, NULL_TREE);
15069       def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
15070       ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
15071 					integer_type_node, NULL_TREE);
15072       def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
15073     }
15074 
15075 }
15076 
15077 static void
htm_init_builtins(void)15078 htm_init_builtins (void)
15079 {
15080   HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
15081   const struct builtin_description *d;
15082   size_t i;
15083 
15084   d = bdesc_htm;
15085   for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
15086     {
15087       tree op[MAX_HTM_OPERANDS], type;
15088       HOST_WIDE_INT mask = d->mask;
15089       unsigned attr = rs6000_builtin_info[d->code].attr;
15090       bool void_func = (attr & RS6000_BTC_VOID);
15091       int attr_args = (attr & RS6000_BTC_TYPE_MASK);
15092       int nopnds = 0;
15093       tree gpr_type_node;
15094       tree rettype;
15095       tree argtype;
15096 
15097       if (TARGET_32BIT && TARGET_POWERPC64)
15098 	gpr_type_node = long_long_unsigned_type_node;
15099       else
15100 	gpr_type_node = long_unsigned_type_node;
15101 
15102       if (attr & RS6000_BTC_SPR)
15103 	{
15104 	  rettype = gpr_type_node;
15105 	  argtype = gpr_type_node;
15106 	}
15107       else if (d->code == HTM_BUILTIN_TABORTDC
15108 	       || d->code == HTM_BUILTIN_TABORTDCI)
15109 	{
15110 	  rettype = unsigned_type_node;
15111 	  argtype = gpr_type_node;
15112 	}
15113       else
15114 	{
15115 	  rettype = unsigned_type_node;
15116 	  argtype = unsigned_type_node;
15117 	}
15118 
15119       if ((mask & builtin_mask) != mask)
15120 	{
15121 	  if (TARGET_DEBUG_BUILTIN)
15122 	    fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
15123 	  continue;
15124 	}
15125 
15126       if (d->name == 0)
15127 	{
15128 	  if (TARGET_DEBUG_BUILTIN)
15129 	    fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
15130 		     (long unsigned) i);
15131 	  continue;
15132 	}
15133 
15134       op[nopnds++] = (void_func) ? void_type_node : rettype;
15135 
15136       if (attr_args == RS6000_BTC_UNARY)
15137 	op[nopnds++] = argtype;
15138       else if (attr_args == RS6000_BTC_BINARY)
15139 	{
15140 	  op[nopnds++] = argtype;
15141 	  op[nopnds++] = argtype;
15142 	}
15143       else if (attr_args == RS6000_BTC_TERNARY)
15144 	{
15145 	  op[nopnds++] = argtype;
15146 	  op[nopnds++] = argtype;
15147 	  op[nopnds++] = argtype;
15148 	}
15149 
15150       switch (nopnds)
15151 	{
15152 	case 1:
15153 	  type = build_function_type_list (op[0], NULL_TREE);
15154 	  break;
15155 	case 2:
15156 	  type = build_function_type_list (op[0], op[1], NULL_TREE);
15157 	  break;
15158 	case 3:
15159 	  type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
15160 	  break;
15161 	case 4:
15162 	  type = build_function_type_list (op[0], op[1], op[2], op[3],
15163 					   NULL_TREE);
15164 	  break;
15165 	default:
15166 	  gcc_unreachable ();
15167 	}
15168 
15169       def_builtin (d->name, type, d->code);
15170     }
15171 }
15172 
15173 /* Hash function for builtin functions with up to 3 arguments and a return
15174    type.  */
15175 static unsigned
builtin_hash_function(const void * hash_entry)15176 builtin_hash_function (const void *hash_entry)
15177 {
15178   unsigned ret = 0;
15179   int i;
15180   const struct builtin_hash_struct *bh =
15181     (const struct builtin_hash_struct *) hash_entry;
15182 
15183   for (i = 0; i < 4; i++)
15184     {
15185       ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
15186       ret = (ret * 2) + bh->uns_p[i];
15187     }
15188 
15189   return ret;
15190 }
15191 
15192 /* Compare builtin hash entries H1 and H2 for equivalence.  */
15193 static int
builtin_hash_eq(const void * h1,const void * h2)15194 builtin_hash_eq (const void *h1, const void *h2)
15195 {
15196   const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
15197   const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
15198 
15199   return ((p1->mode[0] == p2->mode[0])
15200 	  && (p1->mode[1] == p2->mode[1])
15201 	  && (p1->mode[2] == p2->mode[2])
15202 	  && (p1->mode[3] == p2->mode[3])
15203 	  && (p1->uns_p[0] == p2->uns_p[0])
15204 	  && (p1->uns_p[1] == p2->uns_p[1])
15205 	  && (p1->uns_p[2] == p2->uns_p[2])
15206 	  && (p1->uns_p[3] == p2->uns_p[3]));
15207 }
15208 
15209 /* Map types for builtin functions with an explicit return type and up to 3
15210    arguments.  Functions with fewer than 3 arguments use VOIDmode as the type
15211    of the argument.  */
15212 static tree
builtin_function_type(enum machine_mode mode_ret,enum machine_mode mode_arg0,enum machine_mode mode_arg1,enum machine_mode mode_arg2,enum rs6000_builtins builtin,const char * name)15213 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
15214 		       enum machine_mode mode_arg1, enum machine_mode mode_arg2,
15215 		       enum rs6000_builtins builtin, const char *name)
15216 {
15217   struct builtin_hash_struct h;
15218   struct builtin_hash_struct *h2;
15219   void **found;
15220   int num_args = 3;
15221   int i;
15222   tree ret_type = NULL_TREE;
15223   tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
15224 
15225   /* Create builtin_hash_table.  */
15226   if (builtin_hash_table == NULL)
15227     builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
15228 					  builtin_hash_eq, NULL);
15229 
15230   h.type = NULL_TREE;
15231   h.mode[0] = mode_ret;
15232   h.mode[1] = mode_arg0;
15233   h.mode[2] = mode_arg1;
15234   h.mode[3] = mode_arg2;
15235   h.uns_p[0] = 0;
15236   h.uns_p[1] = 0;
15237   h.uns_p[2] = 0;
15238   h.uns_p[3] = 0;
15239 
15240   /* If the builtin is a type that produces unsigned results or takes unsigned
15241      arguments, and it is returned as a decl for the vectorizer (such as
15242      widening multiplies, permute), make sure the arguments and return value
15243      are type correct.  */
15244   switch (builtin)
15245     {
15246       /* unsigned 1 argument functions.  */
15247     case CRYPTO_BUILTIN_VSBOX:
15248     case P8V_BUILTIN_VGBBD:
15249     case MISC_BUILTIN_CDTBCD:
15250     case MISC_BUILTIN_CBCDTD:
15251       h.uns_p[0] = 1;
15252       h.uns_p[1] = 1;
15253       break;
15254 
15255       /* unsigned 2 argument functions.  */
15256     case ALTIVEC_BUILTIN_VMULEUB_UNS:
15257     case ALTIVEC_BUILTIN_VMULEUH_UNS:
15258     case ALTIVEC_BUILTIN_VMULOUB_UNS:
15259     case ALTIVEC_BUILTIN_VMULOUH_UNS:
15260     case CRYPTO_BUILTIN_VCIPHER:
15261     case CRYPTO_BUILTIN_VCIPHERLAST:
15262     case CRYPTO_BUILTIN_VNCIPHER:
15263     case CRYPTO_BUILTIN_VNCIPHERLAST:
15264     case CRYPTO_BUILTIN_VPMSUMB:
15265     case CRYPTO_BUILTIN_VPMSUMH:
15266     case CRYPTO_BUILTIN_VPMSUMW:
15267     case CRYPTO_BUILTIN_VPMSUMD:
15268     case CRYPTO_BUILTIN_VPMSUM:
15269     case MISC_BUILTIN_ADDG6S:
15270     case MISC_BUILTIN_DIVWEU:
15271     case MISC_BUILTIN_DIVWEUO:
15272     case MISC_BUILTIN_DIVDEU:
15273     case MISC_BUILTIN_DIVDEUO:
15274       h.uns_p[0] = 1;
15275       h.uns_p[1] = 1;
15276       h.uns_p[2] = 1;
15277       break;
15278 
15279       /* unsigned 3 argument functions.  */
15280     case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
15281     case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
15282     case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
15283     case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
15284     case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
15285     case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
15286     case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
15287     case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
15288     case VSX_BUILTIN_VPERM_16QI_UNS:
15289     case VSX_BUILTIN_VPERM_8HI_UNS:
15290     case VSX_BUILTIN_VPERM_4SI_UNS:
15291     case VSX_BUILTIN_VPERM_2DI_UNS:
15292     case VSX_BUILTIN_XXSEL_16QI_UNS:
15293     case VSX_BUILTIN_XXSEL_8HI_UNS:
15294     case VSX_BUILTIN_XXSEL_4SI_UNS:
15295     case VSX_BUILTIN_XXSEL_2DI_UNS:
15296     case CRYPTO_BUILTIN_VPERMXOR:
15297     case CRYPTO_BUILTIN_VPERMXOR_V2DI:
15298     case CRYPTO_BUILTIN_VPERMXOR_V4SI:
15299     case CRYPTO_BUILTIN_VPERMXOR_V8HI:
15300     case CRYPTO_BUILTIN_VPERMXOR_V16QI:
15301     case CRYPTO_BUILTIN_VSHASIGMAW:
15302     case CRYPTO_BUILTIN_VSHASIGMAD:
15303     case CRYPTO_BUILTIN_VSHASIGMA:
15304       h.uns_p[0] = 1;
15305       h.uns_p[1] = 1;
15306       h.uns_p[2] = 1;
15307       h.uns_p[3] = 1;
15308       break;
15309 
15310       /* signed permute functions with unsigned char mask.  */
15311     case ALTIVEC_BUILTIN_VPERM_16QI:
15312     case ALTIVEC_BUILTIN_VPERM_8HI:
15313     case ALTIVEC_BUILTIN_VPERM_4SI:
15314     case ALTIVEC_BUILTIN_VPERM_4SF:
15315     case ALTIVEC_BUILTIN_VPERM_2DI:
15316     case ALTIVEC_BUILTIN_VPERM_2DF:
15317     case VSX_BUILTIN_VPERM_16QI:
15318     case VSX_BUILTIN_VPERM_8HI:
15319     case VSX_BUILTIN_VPERM_4SI:
15320     case VSX_BUILTIN_VPERM_4SF:
15321     case VSX_BUILTIN_VPERM_2DI:
15322     case VSX_BUILTIN_VPERM_2DF:
15323       h.uns_p[3] = 1;
15324       break;
15325 
15326       /* unsigned args, signed return.  */
15327     case VSX_BUILTIN_XVCVUXDDP_UNS:
15328     case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
15329       h.uns_p[1] = 1;
15330       break;
15331 
15332       /* signed args, unsigned return.  */
15333     case VSX_BUILTIN_XVCVDPUXDS_UNS:
15334     case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
15335     case MISC_BUILTIN_UNPACK_TD:
15336     case MISC_BUILTIN_UNPACK_V1TI:
15337       h.uns_p[0] = 1;
15338       break;
15339 
15340       /* unsigned arguments for 128-bit pack instructions.  */
15341     case MISC_BUILTIN_PACK_TD:
15342     case MISC_BUILTIN_PACK_V1TI:
15343       h.uns_p[1] = 1;
15344       h.uns_p[2] = 1;
15345       break;
15346 
15347     default:
15348       break;
15349     }
15350 
15351   /* Figure out how many args are present.  */
15352   while (num_args > 0 && h.mode[num_args] == VOIDmode)
15353     num_args--;
15354 
15355   if (num_args == 0)
15356     fatal_error ("internal error: builtin function %s had no type", name);
15357 
15358   ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
15359   if (!ret_type && h.uns_p[0])
15360     ret_type = builtin_mode_to_type[h.mode[0]][0];
15361 
15362   if (!ret_type)
15363     fatal_error ("internal error: builtin function %s had an unexpected "
15364 		 "return type %s", name, GET_MODE_NAME (h.mode[0]));
15365 
15366   for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
15367     arg_type[i] = NULL_TREE;
15368 
15369   for (i = 0; i < num_args; i++)
15370     {
15371       int m = (int) h.mode[i+1];
15372       int uns_p = h.uns_p[i+1];
15373 
15374       arg_type[i] = builtin_mode_to_type[m][uns_p];
15375       if (!arg_type[i] && uns_p)
15376 	arg_type[i] = builtin_mode_to_type[m][0];
15377 
15378       if (!arg_type[i])
15379 	fatal_error ("internal error: builtin function %s, argument %d "
15380 		     "had unexpected argument type %s", name, i,
15381 		     GET_MODE_NAME (m));
15382     }
15383 
15384   found = htab_find_slot (builtin_hash_table, &h, INSERT);
15385   if (*found == NULL)
15386     {
15387       h2 = ggc_alloc_builtin_hash_struct ();
15388       *h2 = h;
15389       *found = (void *)h2;
15390 
15391       h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
15392 					   arg_type[2], NULL_TREE);
15393     }
15394 
15395   return ((struct builtin_hash_struct *)(*found))->type;
15396 }
15397 
15398 static void
rs6000_common_init_builtins(void)15399 rs6000_common_init_builtins (void)
15400 {
15401   const struct builtin_description *d;
15402   size_t i;
15403 
15404   tree opaque_ftype_opaque = NULL_TREE;
15405   tree opaque_ftype_opaque_opaque = NULL_TREE;
15406   tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
15407   tree v2si_ftype_qi = NULL_TREE;
15408   tree v2si_ftype_v2si_qi = NULL_TREE;
15409   tree v2si_ftype_int_qi = NULL_TREE;
15410   HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
15411 
15412   if (!TARGET_PAIRED_FLOAT)
15413     {
15414       builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
15415       builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
15416     }
15417 
15418   /* Paired and SPE builtins are only available if you build a compiler with
15419      the appropriate options, so only create those builtins with the
15420      appropriate compiler option.  Create Altivec and VSX builtins on machines
15421      with at least the general purpose extensions (970 and newer) to allow the
15422      use of the target attribute..  */
15423 
15424   if (TARGET_EXTRA_BUILTINS)
15425     builtin_mask |= RS6000_BTM_COMMON;
15426 
15427   /* Add the ternary operators.  */
15428   d = bdesc_3arg;
15429   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
15430     {
15431       tree type;
15432       HOST_WIDE_INT mask = d->mask;
15433 
15434       if ((mask & builtin_mask) != mask)
15435 	{
15436 	  if (TARGET_DEBUG_BUILTIN)
15437 	    fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
15438 	  continue;
15439 	}
15440 
15441       if (rs6000_overloaded_builtin_p (d->code))
15442 	{
15443 	  if (! (type = opaque_ftype_opaque_opaque_opaque))
15444 	    type = opaque_ftype_opaque_opaque_opaque
15445 	      = build_function_type_list (opaque_V4SI_type_node,
15446 					  opaque_V4SI_type_node,
15447 					  opaque_V4SI_type_node,
15448 					  opaque_V4SI_type_node,
15449 					  NULL_TREE);
15450 	}
15451       else
15452 	{
15453 	  enum insn_code icode = d->icode;
15454 	  if (d->name == 0)
15455 	    {
15456 	      if (TARGET_DEBUG_BUILTIN)
15457 		fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
15458 			 (long unsigned)i);
15459 
15460 	      continue;
15461 	    }
15462 
15463           if (icode == CODE_FOR_nothing)
15464 	    {
15465 	      if (TARGET_DEBUG_BUILTIN)
15466 		fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
15467 			 d->name);
15468 
15469 	      continue;
15470 	    }
15471 
15472 	  type = builtin_function_type (insn_data[icode].operand[0].mode,
15473 					insn_data[icode].operand[1].mode,
15474 					insn_data[icode].operand[2].mode,
15475 					insn_data[icode].operand[3].mode,
15476 					d->code, d->name);
15477 	}
15478 
15479       def_builtin (d->name, type, d->code);
15480     }
15481 
15482   /* Add the binary operators.  */
15483   d = bdesc_2arg;
15484   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15485     {
15486       enum machine_mode mode0, mode1, mode2;
15487       tree type;
15488       HOST_WIDE_INT mask = d->mask;
15489 
15490       if ((mask & builtin_mask) != mask)
15491 	{
15492 	  if (TARGET_DEBUG_BUILTIN)
15493 	    fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
15494 	  continue;
15495 	}
15496 
15497       if (rs6000_overloaded_builtin_p (d->code))
15498 	{
15499 	  if (! (type = opaque_ftype_opaque_opaque))
15500 	    type = opaque_ftype_opaque_opaque
15501 	      = build_function_type_list (opaque_V4SI_type_node,
15502 					  opaque_V4SI_type_node,
15503 					  opaque_V4SI_type_node,
15504 					  NULL_TREE);
15505 	}
15506       else
15507 	{
15508 	  enum insn_code icode = d->icode;
15509 	  if (d->name == 0)
15510 	    {
15511 	      if (TARGET_DEBUG_BUILTIN)
15512 		fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
15513 			 (long unsigned)i);
15514 
15515 	      continue;
15516 	    }
15517 
15518           if (icode == CODE_FOR_nothing)
15519 	    {
15520 	      if (TARGET_DEBUG_BUILTIN)
15521 		fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
15522 			 d->name);
15523 
15524 	      continue;
15525 	    }
15526 
15527           mode0 = insn_data[icode].operand[0].mode;
15528           mode1 = insn_data[icode].operand[1].mode;
15529           mode2 = insn_data[icode].operand[2].mode;
15530 
15531 	  if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
15532 	    {
15533 	      if (! (type = v2si_ftype_v2si_qi))
15534 		type = v2si_ftype_v2si_qi
15535 		  = build_function_type_list (opaque_V2SI_type_node,
15536 					      opaque_V2SI_type_node,
15537 					      char_type_node,
15538 					      NULL_TREE);
15539 	    }
15540 
15541 	  else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
15542 		   && mode2 == QImode)
15543 	    {
15544 	      if (! (type = v2si_ftype_int_qi))
15545 		type = v2si_ftype_int_qi
15546 		  = build_function_type_list (opaque_V2SI_type_node,
15547 					      integer_type_node,
15548 					      char_type_node,
15549 					      NULL_TREE);
15550 	    }
15551 
15552 	  else
15553 	    type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
15554 					  d->code, d->name);
15555 	}
15556 
15557       def_builtin (d->name, type, d->code);
15558     }
15559 
15560   /* Add the simple unary operators.  */
15561   d = bdesc_1arg;
15562   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15563     {
15564       enum machine_mode mode0, mode1;
15565       tree type;
15566       HOST_WIDE_INT mask = d->mask;
15567 
15568       if ((mask & builtin_mask) != mask)
15569 	{
15570 	  if (TARGET_DEBUG_BUILTIN)
15571 	    fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
15572 	  continue;
15573 	}
15574 
15575       if (rs6000_overloaded_builtin_p (d->code))
15576 	{
15577 	  if (! (type = opaque_ftype_opaque))
15578 	    type = opaque_ftype_opaque
15579 	      = build_function_type_list (opaque_V4SI_type_node,
15580 					  opaque_V4SI_type_node,
15581 					  NULL_TREE);
15582 	}
15583       else
15584         {
15585 	  enum insn_code icode = d->icode;
15586 	  if (d->name == 0)
15587 	    {
15588 	      if (TARGET_DEBUG_BUILTIN)
15589 		fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
15590 			 (long unsigned)i);
15591 
15592 	      continue;
15593 	    }
15594 
15595           if (icode == CODE_FOR_nothing)
15596 	    {
15597 	      if (TARGET_DEBUG_BUILTIN)
15598 		fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
15599 			 d->name);
15600 
15601 	      continue;
15602 	    }
15603 
15604           mode0 = insn_data[icode].operand[0].mode;
15605           mode1 = insn_data[icode].operand[1].mode;
15606 
15607 	  if (mode0 == V2SImode && mode1 == QImode)
15608 	    {
15609 	      if (! (type = v2si_ftype_qi))
15610 		type = v2si_ftype_qi
15611 		  = build_function_type_list (opaque_V2SI_type_node,
15612 					      char_type_node,
15613 					      NULL_TREE);
15614 	    }
15615 
15616 	  else
15617 	    type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
15618 					  d->code, d->name);
15619 	}
15620 
15621       def_builtin (d->name, type, d->code);
15622     }
15623 }
15624 
15625 static void
rs6000_init_libfuncs(void)15626 rs6000_init_libfuncs (void)
15627 {
15628   if (!TARGET_IEEEQUAD)
15629       /* AIX/Darwin/64-bit Linux quad floating point routines.  */
15630     if (!TARGET_XL_COMPAT)
15631       {
15632 	set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
15633 	set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
15634 	set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
15635 	set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
15636 
15637 	if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
15638 	  {
15639 	    set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
15640 	    set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
15641 	    set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
15642 	    set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
15643 	    set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
15644 	    set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
15645 	    set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
15646 
15647 	    set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
15648 	    set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
15649 	    set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
15650 	    set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
15651 	    set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
15652 	    set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
15653 	    set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
15654 	    set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
15655 	  }
15656 
15657 	if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
15658 	  set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
15659       }
15660     else
15661       {
15662 	set_optab_libfunc (add_optab, TFmode, "_xlqadd");
15663 	set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
15664 	set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
15665 	set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
15666       }
15667   else
15668     {
15669       /* 32-bit SVR4 quad floating point routines.  */
15670 
15671       set_optab_libfunc (add_optab, TFmode, "_q_add");
15672       set_optab_libfunc (sub_optab, TFmode, "_q_sub");
15673       set_optab_libfunc (neg_optab, TFmode, "_q_neg");
15674       set_optab_libfunc (smul_optab, TFmode, "_q_mul");
15675       set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
15676       if (TARGET_PPC_GPOPT)
15677 	set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
15678 
15679       set_optab_libfunc (eq_optab, TFmode, "_q_feq");
15680       set_optab_libfunc (ne_optab, TFmode, "_q_fne");
15681       set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
15682       set_optab_libfunc (ge_optab, TFmode, "_q_fge");
15683       set_optab_libfunc (lt_optab, TFmode, "_q_flt");
15684       set_optab_libfunc (le_optab, TFmode, "_q_fle");
15685 
15686       set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
15687       set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
15688       set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
15689       set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
15690       set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
15691       set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
15692       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
15693       set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
15694     }
15695 }
15696 
15697 
15698 /* Expand a block clear operation, and return 1 if successful.  Return 0
15699    if we should let the compiler generate normal code.
15700 
15701    operands[0] is the destination
15702    operands[1] is the length
15703    operands[3] is the alignment */
15704 
15705 int
expand_block_clear(rtx operands[])15706 expand_block_clear (rtx operands[])
15707 {
15708   rtx orig_dest = operands[0];
15709   rtx bytes_rtx	= operands[1];
15710   rtx align_rtx = operands[3];
15711   bool constp	= (GET_CODE (bytes_rtx) == CONST_INT);
15712   HOST_WIDE_INT align;
15713   HOST_WIDE_INT bytes;
15714   int offset;
15715   int clear_bytes;
15716   int clear_step;
15717 
15718   /* If this is not a fixed size move, just call memcpy */
15719   if (! constp)
15720     return 0;
15721 
15722   /* This must be a fixed size alignment  */
15723   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15724   align = INTVAL (align_rtx) * BITS_PER_UNIT;
15725 
15726   /* Anything to clear? */
15727   bytes = INTVAL (bytes_rtx);
15728   if (bytes <= 0)
15729     return 1;
15730 
15731   /* Use the builtin memset after a point, to avoid huge code bloat.
15732      When optimize_size, avoid any significant code bloat; calling
15733      memset is about 4 instructions, so allow for one instruction to
15734      load zero and three to do clearing.  */
15735   if (TARGET_ALTIVEC && align >= 128)
15736     clear_step = 16;
15737   else if (TARGET_POWERPC64 && align >= 32)
15738     clear_step = 8;
15739   else if (TARGET_SPE && align >= 64)
15740     clear_step = 8;
15741   else
15742     clear_step = 4;
15743 
15744   if (optimize_size && bytes > 3 * clear_step)
15745     return 0;
15746   if (! optimize_size && bytes > 8 * clear_step)
15747     return 0;
15748 
15749   for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
15750     {
15751       enum machine_mode mode = BLKmode;
15752       rtx dest;
15753 
15754       if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
15755 	{
15756 	  clear_bytes = 16;
15757 	  mode = V4SImode;
15758 	}
15759       else if (bytes >= 8 && TARGET_SPE && align >= 64)
15760         {
15761           clear_bytes = 8;
15762           mode = V2SImode;
15763         }
15764       else if (bytes >= 8 && TARGET_POWERPC64
15765 	       /* 64-bit loads and stores require word-aligned
15766 		  displacements.  */
15767 	       && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
15768 	{
15769 	  clear_bytes = 8;
15770 	  mode = DImode;
15771 	}
15772       else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15773 	{			/* move 4 bytes */
15774 	  clear_bytes = 4;
15775 	  mode = SImode;
15776 	}
15777       else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15778 	{			/* move 2 bytes */
15779 	  clear_bytes = 2;
15780 	  mode = HImode;
15781 	}
15782       else /* move 1 byte at a time */
15783 	{
15784 	  clear_bytes = 1;
15785 	  mode = QImode;
15786 	}
15787 
15788       dest = adjust_address (orig_dest, mode, offset);
15789 
15790       emit_move_insn (dest, CONST0_RTX (mode));
15791     }
15792 
15793   return 1;
15794 }
15795 
15796 
15797 /* Expand a block move operation, and return 1 if successful.  Return 0
15798    if we should let the compiler generate normal code.
15799 
15800    operands[0] is the destination
15801    operands[1] is the source
15802    operands[2] is the length
15803    operands[3] is the alignment */
15804 
15805 #define MAX_MOVE_REG 4
15806 
15807 int
expand_block_move(rtx operands[])15808 expand_block_move (rtx operands[])
15809 {
15810   rtx orig_dest = operands[0];
15811   rtx orig_src	= operands[1];
15812   rtx bytes_rtx	= operands[2];
15813   rtx align_rtx = operands[3];
15814   int constp	= (GET_CODE (bytes_rtx) == CONST_INT);
15815   int align;
15816   int bytes;
15817   int offset;
15818   int move_bytes;
15819   rtx stores[MAX_MOVE_REG];
15820   int num_reg = 0;
15821 
15822   /* If this is not a fixed size move, just call memcpy */
15823   if (! constp)
15824     return 0;
15825 
15826   /* This must be a fixed size alignment */
15827   gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15828   align = INTVAL (align_rtx) * BITS_PER_UNIT;
15829 
15830   /* Anything to move? */
15831   bytes = INTVAL (bytes_rtx);
15832   if (bytes <= 0)
15833     return 1;
15834 
15835   if (bytes > rs6000_block_move_inline_limit)
15836     return 0;
15837 
15838   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
15839     {
15840       union {
15841 	rtx (*movmemsi) (rtx, rtx, rtx, rtx);
15842 	rtx (*mov) (rtx, rtx);
15843       } gen_func;
15844       enum machine_mode mode = BLKmode;
15845       rtx src, dest;
15846 
15847       /* Altivec first, since it will be faster than a string move
15848 	 when it applies, and usually not significantly larger.  */
15849       if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
15850 	{
15851 	  move_bytes = 16;
15852 	  mode = V4SImode;
15853 	  gen_func.mov = gen_movv4si;
15854 	}
15855       else if (TARGET_SPE && bytes >= 8 && align >= 64)
15856         {
15857           move_bytes = 8;
15858           mode = V2SImode;
15859           gen_func.mov = gen_movv2si;
15860         }
15861       else if (TARGET_STRING
15862 	  && bytes > 24		/* move up to 32 bytes at a time */
15863 	  && ! fixed_regs[5]
15864 	  && ! fixed_regs[6]
15865 	  && ! fixed_regs[7]
15866 	  && ! fixed_regs[8]
15867 	  && ! fixed_regs[9]
15868 	  && ! fixed_regs[10]
15869 	  && ! fixed_regs[11]
15870 	  && ! fixed_regs[12])
15871 	{
15872 	  move_bytes = (bytes > 32) ? 32 : bytes;
15873 	  gen_func.movmemsi = gen_movmemsi_8reg;
15874 	}
15875       else if (TARGET_STRING
15876 	       && bytes > 16	/* move up to 24 bytes at a time */
15877 	       && ! fixed_regs[5]
15878 	       && ! fixed_regs[6]
15879 	       && ! fixed_regs[7]
15880 	       && ! fixed_regs[8]
15881 	       && ! fixed_regs[9]
15882 	       && ! fixed_regs[10])
15883 	{
15884 	  move_bytes = (bytes > 24) ? 24 : bytes;
15885 	  gen_func.movmemsi = gen_movmemsi_6reg;
15886 	}
15887       else if (TARGET_STRING
15888 	       && bytes > 8	/* move up to 16 bytes at a time */
15889 	       && ! fixed_regs[5]
15890 	       && ! fixed_regs[6]
15891 	       && ! fixed_regs[7]
15892 	       && ! fixed_regs[8])
15893 	{
15894 	  move_bytes = (bytes > 16) ? 16 : bytes;
15895 	  gen_func.movmemsi = gen_movmemsi_4reg;
15896 	}
15897       else if (bytes >= 8 && TARGET_POWERPC64
15898 	       /* 64-bit loads and stores require word-aligned
15899 		  displacements.  */
15900 	       && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
15901 	{
15902 	  move_bytes = 8;
15903 	  mode = DImode;
15904 	  gen_func.mov = gen_movdi;
15905 	}
15906       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
15907 	{			/* move up to 8 bytes at a time */
15908 	  move_bytes = (bytes > 8) ? 8 : bytes;
15909 	  gen_func.movmemsi = gen_movmemsi_2reg;
15910 	}
15911       else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15912 	{			/* move 4 bytes */
15913 	  move_bytes = 4;
15914 	  mode = SImode;
15915 	  gen_func.mov = gen_movsi;
15916 	}
15917       else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15918 	{			/* move 2 bytes */
15919 	  move_bytes = 2;
15920 	  mode = HImode;
15921 	  gen_func.mov = gen_movhi;
15922 	}
15923       else if (TARGET_STRING && bytes > 1)
15924 	{			/* move up to 4 bytes at a time */
15925 	  move_bytes = (bytes > 4) ? 4 : bytes;
15926 	  gen_func.movmemsi = gen_movmemsi_1reg;
15927 	}
15928       else /* move 1 byte at a time */
15929 	{
15930 	  move_bytes = 1;
15931 	  mode = QImode;
15932 	  gen_func.mov = gen_movqi;
15933 	}
15934 
15935       src = adjust_address (orig_src, mode, offset);
15936       dest = adjust_address (orig_dest, mode, offset);
15937 
15938       if (mode != BLKmode)
15939 	{
15940 	  rtx tmp_reg = gen_reg_rtx (mode);
15941 
15942 	  emit_insn ((*gen_func.mov) (tmp_reg, src));
15943 	  stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
15944 	}
15945 
15946       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
15947 	{
15948 	  int i;
15949 	  for (i = 0; i < num_reg; i++)
15950 	    emit_insn (stores[i]);
15951 	  num_reg = 0;
15952 	}
15953 
15954       if (mode == BLKmode)
15955 	{
15956 	  /* Move the address into scratch registers.  The movmemsi
15957 	     patterns require zero offset.  */
15958 	  if (!REG_P (XEXP (src, 0)))
15959 	    {
15960 	      rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
15961 	      src = replace_equiv_address (src, src_reg);
15962 	    }
15963 	  set_mem_size (src, move_bytes);
15964 
15965 	  if (!REG_P (XEXP (dest, 0)))
15966 	    {
15967 	      rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
15968 	      dest = replace_equiv_address (dest, dest_reg);
15969 	    }
15970 	  set_mem_size (dest, move_bytes);
15971 
15972 	  emit_insn ((*gen_func.movmemsi) (dest, src,
15973 					   GEN_INT (move_bytes & 31),
15974 					   align_rtx));
15975 	}
15976     }
15977 
15978   return 1;
15979 }
15980 
15981 
15982 /* Return a string to perform a load_multiple operation.
15983    operands[0] is the vector.
15984    operands[1] is the source address.
15985    operands[2] is the first destination register.  */
15986 
15987 const char *
rs6000_output_load_multiple(rtx operands[3])15988 rs6000_output_load_multiple (rtx operands[3])
15989 {
15990   /* We have to handle the case where the pseudo used to contain the address
15991      is assigned to one of the output registers.  */
15992   int i, j;
15993   int words = XVECLEN (operands[0], 0);
15994   rtx xop[10];
15995 
15996   if (XVECLEN (operands[0], 0) == 1)
15997     return "lwz %2,0(%1)";
15998 
15999   for (i = 0; i < words; i++)
16000     if (refers_to_regno_p (REGNO (operands[2]) + i,
16001 			   REGNO (operands[2]) + i + 1, operands[1], 0))
16002       {
16003 	if (i == words-1)
16004 	  {
16005 	    xop[0] = GEN_INT (4 * (words-1));
16006 	    xop[1] = operands[1];
16007 	    xop[2] = operands[2];
16008 	    output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
16009 	    return "";
16010 	  }
16011 	else if (i == 0)
16012 	  {
16013 	    xop[0] = GEN_INT (4 * (words-1));
16014 	    xop[1] = operands[1];
16015 	    xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
16016 	    output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
16017 	    return "";
16018 	  }
16019 	else
16020 	  {
16021 	    for (j = 0; j < words; j++)
16022 	      if (j != i)
16023 		{
16024 		  xop[0] = GEN_INT (j * 4);
16025 		  xop[1] = operands[1];
16026 		  xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
16027 		  output_asm_insn ("lwz %2,%0(%1)", xop);
16028 		}
16029 	    xop[0] = GEN_INT (i * 4);
16030 	    xop[1] = operands[1];
16031 	    output_asm_insn ("lwz %1,%0(%1)", xop);
16032 	    return "";
16033 	  }
16034       }
16035 
16036   return "lswi %2,%1,%N0";
16037 }
16038 
16039 
16040 /* A validation routine: say whether CODE, a condition code, and MODE
16041    match.  The other alternatives either don't make sense or should
16042    never be generated.  */
16043 
16044 void
validate_condition_mode(enum rtx_code code,enum machine_mode mode)16045 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
16046 {
16047   gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
16048 	       || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
16049 	      && GET_MODE_CLASS (mode) == MODE_CC);
16050 
16051   /* These don't make sense.  */
16052   gcc_assert ((code != GT && code != LT && code != GE && code != LE)
16053 	      || mode != CCUNSmode);
16054 
16055   gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
16056 	      || mode == CCUNSmode);
16057 
16058   gcc_assert (mode == CCFPmode
16059 	      || (code != ORDERED && code != UNORDERED
16060 		  && code != UNEQ && code != LTGT
16061 		  && code != UNGT && code != UNLT
16062 		  && code != UNGE && code != UNLE));
16063 
16064   /* These should never be generated except for
16065      flag_finite_math_only.  */
16066   gcc_assert (mode != CCFPmode
16067 	      || flag_finite_math_only
16068 	      || (code != LE && code != GE
16069 		  && code != UNEQ && code != LTGT
16070 		  && code != UNGT && code != UNLT));
16071 
16072   /* These are invalid; the information is not there.  */
16073   gcc_assert (mode != CCEQmode || code == EQ || code == NE);
16074 }
16075 
16076 
16077 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
16078    mask required to convert the result of a rotate insn into a shift
16079    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
16080 
16081 int
includes_lshift_p(rtx shiftop,rtx andop)16082 includes_lshift_p (rtx shiftop, rtx andop)
16083 {
16084   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
16085 
16086   shift_mask <<= INTVAL (shiftop);
16087 
16088   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
16089 }
16090 
16091 /* Similar, but for right shift.  */
16092 
16093 int
includes_rshift_p(rtx shiftop,rtx andop)16094 includes_rshift_p (rtx shiftop, rtx andop)
16095 {
16096   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
16097 
16098   shift_mask >>= INTVAL (shiftop);
16099 
16100   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
16101 }
16102 
16103 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
16104    to perform a left shift.  It must have exactly SHIFTOP least
16105    significant 0's, then one or more 1's, then zero or more 0's.  */
16106 
16107 int
includes_rldic_lshift_p(rtx shiftop,rtx andop)16108 includes_rldic_lshift_p (rtx shiftop, rtx andop)
16109 {
16110   if (GET_CODE (andop) == CONST_INT)
16111     {
16112       HOST_WIDE_INT c, lsb, shift_mask;
16113 
16114       c = INTVAL (andop);
16115       if (c == 0 || c == ~0)
16116 	return 0;
16117 
16118       shift_mask = ~0;
16119       shift_mask <<= INTVAL (shiftop);
16120 
16121       /* Find the least significant one bit.  */
16122       lsb = c & -c;
16123 
16124       /* It must coincide with the LSB of the shift mask.  */
16125       if (-lsb != shift_mask)
16126 	return 0;
16127 
16128       /* Invert to look for the next transition (if any).  */
16129       c = ~c;
16130 
16131       /* Remove the low group of ones (originally low group of zeros).  */
16132       c &= -lsb;
16133 
16134       /* Again find the lsb, and check we have all 1's above.  */
16135       lsb = c & -c;
16136       return c == -lsb;
16137     }
16138   else if (GET_CODE (andop) == CONST_DOUBLE
16139 	   && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
16140     {
16141       HOST_WIDE_INT low, high, lsb;
16142       HOST_WIDE_INT shift_mask_low, shift_mask_high;
16143 
16144       low = CONST_DOUBLE_LOW (andop);
16145       if (HOST_BITS_PER_WIDE_INT < 64)
16146 	high = CONST_DOUBLE_HIGH (andop);
16147 
16148       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
16149 	  || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
16150 	return 0;
16151 
16152       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
16153 	{
16154 	  shift_mask_high = ~0;
16155 	  if (INTVAL (shiftop) > 32)
16156 	    shift_mask_high <<= INTVAL (shiftop) - 32;
16157 
16158 	  lsb = high & -high;
16159 
16160 	  if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
16161 	    return 0;
16162 
16163 	  high = ~high;
16164 	  high &= -lsb;
16165 
16166 	  lsb = high & -high;
16167 	  return high == -lsb;
16168 	}
16169 
16170       shift_mask_low = ~0;
16171       shift_mask_low <<= INTVAL (shiftop);
16172 
16173       lsb = low & -low;
16174 
16175       if (-lsb != shift_mask_low)
16176 	return 0;
16177 
16178       if (HOST_BITS_PER_WIDE_INT < 64)
16179 	high = ~high;
16180       low = ~low;
16181       low &= -lsb;
16182 
16183       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
16184 	{
16185 	  lsb = high & -high;
16186 	  return high == -lsb;
16187 	}
16188 
16189       lsb = low & -low;
16190       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
16191     }
16192   else
16193     return 0;
16194 }
16195 
16196 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
16197    to perform a left shift.  It must have SHIFTOP or more least
16198    significant 0's, with the remainder of the word 1's.  */
16199 
16200 int
includes_rldicr_lshift_p(rtx shiftop,rtx andop)16201 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
16202 {
16203   if (GET_CODE (andop) == CONST_INT)
16204     {
16205       HOST_WIDE_INT c, lsb, shift_mask;
16206 
16207       shift_mask = ~0;
16208       shift_mask <<= INTVAL (shiftop);
16209       c = INTVAL (andop);
16210 
16211       /* Find the least significant one bit.  */
16212       lsb = c & -c;
16213 
16214       /* It must be covered by the shift mask.
16215 	 This test also rejects c == 0.  */
16216       if ((lsb & shift_mask) == 0)
16217 	return 0;
16218 
16219       /* Check we have all 1's above the transition, and reject all 1's.  */
16220       return c == -lsb && lsb != 1;
16221     }
16222   else if (GET_CODE (andop) == CONST_DOUBLE
16223 	   && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
16224     {
16225       HOST_WIDE_INT low, lsb, shift_mask_low;
16226 
16227       low = CONST_DOUBLE_LOW (andop);
16228 
16229       if (HOST_BITS_PER_WIDE_INT < 64)
16230 	{
16231 	  HOST_WIDE_INT high, shift_mask_high;
16232 
16233 	  high = CONST_DOUBLE_HIGH (andop);
16234 
16235 	  if (low == 0)
16236 	    {
16237 	      shift_mask_high = ~0;
16238 	      if (INTVAL (shiftop) > 32)
16239 		shift_mask_high <<= INTVAL (shiftop) - 32;
16240 
16241 	      lsb = high & -high;
16242 
16243 	      if ((lsb & shift_mask_high) == 0)
16244 		return 0;
16245 
16246 	      return high == -lsb;
16247 	    }
16248 	  if (high != ~0)
16249 	    return 0;
16250 	}
16251 
16252       shift_mask_low = ~0;
16253       shift_mask_low <<= INTVAL (shiftop);
16254 
16255       lsb = low & -low;
16256 
16257       if ((lsb & shift_mask_low) == 0)
16258 	return 0;
16259 
16260       return low == -lsb && lsb != 1;
16261     }
16262   else
16263     return 0;
16264 }
16265 
16266 /* Return 1 if operands will generate a valid arguments to rlwimi
16267 instruction for insert with right shift in 64-bit mode.  The mask may
16268 not start on the first bit or stop on the last bit because wrap-around
16269 effects of instruction do not correspond to semantics of RTL insn.  */
16270 
16271 int
insvdi_rshift_rlwimi_p(rtx sizeop,rtx startop,rtx shiftop)16272 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
16273 {
16274   if (INTVAL (startop) > 32
16275       && INTVAL (startop) < 64
16276       && INTVAL (sizeop) > 1
16277       && INTVAL (sizeop) + INTVAL (startop) < 64
16278       && INTVAL (shiftop) > 0
16279       && INTVAL (sizeop) + INTVAL (shiftop) < 32
16280       && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
16281     return 1;
16282 
16283   return 0;
16284 }
16285 
16286 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
16287    for lfq and stfq insns iff the registers are hard registers.   */
16288 
16289 int
registers_ok_for_quad_peep(rtx reg1,rtx reg2)16290 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
16291 {
16292   /* We might have been passed a SUBREG.  */
16293   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
16294     return 0;
16295 
16296   /* We might have been passed non floating point registers.  */
16297   if (!FP_REGNO_P (REGNO (reg1))
16298       || !FP_REGNO_P (REGNO (reg2)))
16299     return 0;
16300 
16301   return (REGNO (reg1) == REGNO (reg2) - 1);
16302 }
16303 
16304 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
16305    addr1 and addr2 must be in consecutive memory locations
16306    (addr2 == addr1 + 8).  */
16307 
16308 int
mems_ok_for_quad_peep(rtx mem1,rtx mem2)16309 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
16310 {
16311   rtx addr1, addr2;
16312   unsigned int reg1, reg2;
16313   int offset1, offset2;
16314 
16315   /* The mems cannot be volatile.  */
16316   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
16317     return 0;
16318 
16319   addr1 = XEXP (mem1, 0);
16320   addr2 = XEXP (mem2, 0);
16321 
16322   /* Extract an offset (if used) from the first addr.  */
16323   if (GET_CODE (addr1) == PLUS)
16324     {
16325       /* If not a REG, return zero.  */
16326       if (GET_CODE (XEXP (addr1, 0)) != REG)
16327 	return 0;
16328       else
16329 	{
16330 	  reg1 = REGNO (XEXP (addr1, 0));
16331 	  /* The offset must be constant!  */
16332 	  if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
16333 	    return 0;
16334 	  offset1 = INTVAL (XEXP (addr1, 1));
16335 	}
16336     }
16337   else if (GET_CODE (addr1) != REG)
16338     return 0;
16339   else
16340     {
16341       reg1 = REGNO (addr1);
16342       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
16343       offset1 = 0;
16344     }
16345 
16346   /* And now for the second addr.  */
16347   if (GET_CODE (addr2) == PLUS)
16348     {
16349       /* If not a REG, return zero.  */
16350       if (GET_CODE (XEXP (addr2, 0)) != REG)
16351 	return 0;
16352       else
16353 	{
16354 	  reg2 = REGNO (XEXP (addr2, 0));
16355 	  /* The offset must be constant. */
16356 	  if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
16357 	    return 0;
16358 	  offset2 = INTVAL (XEXP (addr2, 1));
16359 	}
16360     }
16361   else if (GET_CODE (addr2) != REG)
16362     return 0;
16363   else
16364     {
16365       reg2 = REGNO (addr2);
16366       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
16367       offset2 = 0;
16368     }
16369 
16370   /* Both of these must have the same base register.  */
16371   if (reg1 != reg2)
16372     return 0;
16373 
16374   /* The offset for the second addr must be 8 more than the first addr.  */
16375   if (offset2 != offset1 + 8)
16376     return 0;
16377 
16378   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
16379      instructions.  */
16380   return 1;
16381 }
16382 
16383 
16384 rtx
rs6000_secondary_memory_needed_rtx(enum machine_mode mode)16385 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
16386 {
16387   static bool eliminated = false;
16388   rtx ret;
16389 
16390   if (mode != SDmode || TARGET_NO_SDMODE_STACK)
16391     ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
16392   else
16393     {
16394       rtx mem = cfun->machine->sdmode_stack_slot;
16395       gcc_assert (mem != NULL_RTX);
16396 
16397       if (!eliminated)
16398 	{
16399 	  mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
16400 	  cfun->machine->sdmode_stack_slot = mem;
16401 	  eliminated = true;
16402 	}
16403       ret = mem;
16404     }
16405 
16406   if (TARGET_DEBUG_ADDR)
16407     {
16408       fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
16409 	       GET_MODE_NAME (mode));
16410       if (!ret)
16411 	fprintf (stderr, "\tNULL_RTX\n");
16412       else
16413 	debug_rtx (ret);
16414     }
16415 
16416   return ret;
16417 }
16418 
16419 /* Return the mode to be used for memory when a secondary memory
16420    location is needed.  For SDmode values we need to use DDmode, in
16421    all other cases we can use the same mode.  */
16422 enum machine_mode
rs6000_secondary_memory_needed_mode(enum machine_mode mode)16423 rs6000_secondary_memory_needed_mode (enum machine_mode mode)
16424 {
16425   if (mode == SDmode)
16426     return DDmode;
16427   return mode;
16428 }
16429 
16430 static tree
rs6000_check_sdmode(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)16431 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16432 {
16433   /* Don't walk into types.  */
16434   if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
16435     {
16436       *walk_subtrees = 0;
16437       return NULL_TREE;
16438     }
16439 
16440   switch (TREE_CODE (*tp))
16441     {
16442     case VAR_DECL:
16443     case PARM_DECL:
16444     case FIELD_DECL:
16445     case RESULT_DECL:
16446     case SSA_NAME:
16447     case REAL_CST:
16448     case MEM_REF:
16449     case VIEW_CONVERT_EXPR:
16450       if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
16451 	return *tp;
16452       break;
16453     default:
16454       break;
16455     }
16456 
16457   return NULL_TREE;
16458 }
16459 
16460 /* Classify a register type.  Because the FMRGOW/FMRGEW instructions only work
16461    on traditional floating point registers, and the VMRGOW/VMRGEW instructions
16462    only work on the traditional altivec registers, note if an altivec register
16463    was chosen.  */
16464 
16465 static enum rs6000_reg_type
register_to_reg_type(rtx reg,bool * is_altivec)16466 register_to_reg_type (rtx reg, bool *is_altivec)
16467 {
16468   HOST_WIDE_INT regno;
16469   enum reg_class rclass;
16470 
16471   if (GET_CODE (reg) == SUBREG)
16472     reg = SUBREG_REG (reg);
16473 
16474   if (!REG_P (reg))
16475     return NO_REG_TYPE;
16476 
16477   regno = REGNO (reg);
16478   if (regno >= FIRST_PSEUDO_REGISTER)
16479     {
16480       if (!lra_in_progress && !reload_in_progress && !reload_completed)
16481 	return PSEUDO_REG_TYPE;
16482 
16483       regno = true_regnum (reg);
16484       if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16485 	return PSEUDO_REG_TYPE;
16486     }
16487 
16488   gcc_assert (regno >= 0);
16489 
16490   if (is_altivec && ALTIVEC_REGNO_P (regno))
16491     *is_altivec = true;
16492 
16493   rclass = rs6000_regno_regclass[regno];
16494   return reg_class_to_reg_type[(int)rclass];
16495 }
16496 
16497 /* Helper function for rs6000_secondary_reload to return true if a move to a
16498    different register classe is really a simple move.  */
16499 
16500 static bool
rs6000_secondary_reload_simple_move(enum rs6000_reg_type to_type,enum rs6000_reg_type from_type,enum machine_mode mode)16501 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
16502 				     enum rs6000_reg_type from_type,
16503 				     enum machine_mode mode)
16504 {
16505   int size;
16506 
16507   /* Add support for various direct moves available.  In this function, we only
16508      look at cases where we don't need any extra registers, and one or more
16509      simple move insns are issued.  At present, 32-bit integers are not allowed
16510      in FPR/VSX registers.  Single precision binary floating is not a simple
16511      move because we need to convert to the single precision memory layout.
16512      The 4-byte SDmode can be moved.  */
16513   size = GET_MODE_SIZE (mode);
16514   if (TARGET_DIRECT_MOVE
16515       && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
16516       && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16517 	  || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
16518     return true;
16519 
16520   else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
16521 	   && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
16522 	       || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16523     return true;
16524 
16525   else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
16526 	   && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
16527 	       || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16528     return true;
16529 
16530   return false;
16531 }
16532 
16533 /* Power8 helper function for rs6000_secondary_reload, handle all of the
16534    special direct moves that involve allocating an extra register, return the
16535    insn code of the helper function if there is such a function or
16536    CODE_FOR_nothing if not.  */
16537 
16538 static bool
rs6000_secondary_reload_direct_move(enum rs6000_reg_type to_type,enum rs6000_reg_type from_type,enum machine_mode mode,secondary_reload_info * sri,bool altivec_p)16539 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
16540 				     enum rs6000_reg_type from_type,
16541 				     enum machine_mode mode,
16542 				     secondary_reload_info *sri,
16543 				     bool altivec_p)
16544 {
16545   bool ret = false;
16546   enum insn_code icode = CODE_FOR_nothing;
16547   int cost = 0;
16548   int size = GET_MODE_SIZE (mode);
16549 
16550   if (TARGET_POWERPC64)
16551     {
16552       if (size == 16)
16553 	{
16554 	  /* Handle moving 128-bit values from GPRs to VSX point registers on
16555 	     power8 when running in 64-bit mode using XXPERMDI to glue the two
16556 	     64-bit values back together.  */
16557 	  if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16558 	    {
16559 	      cost = 3;			/* 2 mtvsrd's, 1 xxpermdi.  */
16560 	      icode = reg_addr[mode].reload_vsx_gpr;
16561 	    }
16562 
16563 	  /* Handle moving 128-bit values from VSX point registers to GPRs on
16564 	     power8 when running in 64-bit mode using XXPERMDI to get access to the
16565 	     bottom 64-bit value.  */
16566 	  else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16567 	    {
16568 	      cost = 3;			/* 2 mfvsrd's, 1 xxpermdi.  */
16569 	      icode = reg_addr[mode].reload_gpr_vsx;
16570 	    }
16571 	}
16572 
16573       else if (mode == SFmode)
16574 	{
16575 	  if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16576 	    {
16577 	      cost = 3;			/* xscvdpspn, mfvsrd, and.  */
16578 	      icode = reg_addr[mode].reload_gpr_vsx;
16579 	    }
16580 
16581 	  else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16582 	    {
16583 	      cost = 2;			/* mtvsrz, xscvspdpn.  */
16584 	      icode = reg_addr[mode].reload_vsx_gpr;
16585 	    }
16586 	}
16587     }
16588 
16589   if (TARGET_POWERPC64 && size == 16)
16590     {
16591       /* Handle moving 128-bit values from GPRs to VSX point registers on
16592 	 power8 when running in 64-bit mode using XXPERMDI to glue the two
16593 	 64-bit values back together.  */
16594       if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16595 	{
16596 	  cost = 3;			/* 2 mtvsrd's, 1 xxpermdi.  */
16597 	  icode = reg_addr[mode].reload_vsx_gpr;
16598 	}
16599 
16600       /* Handle moving 128-bit values from VSX point registers to GPRs on
16601 	 power8 when running in 64-bit mode using XXPERMDI to get access to the
16602 	 bottom 64-bit value.  */
16603       else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16604 	{
16605 	  cost = 3;			/* 2 mfvsrd's, 1 xxpermdi.  */
16606 	  icode = reg_addr[mode].reload_gpr_vsx;
16607 	}
16608     }
16609 
16610   else if (!TARGET_POWERPC64 && size == 8)
16611     {
16612       /* Handle moving 64-bit values from GPRs to floating point registers on
16613 	 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
16614 	 values back together.  Altivec register classes must be handled
16615 	 specially since a different instruction is used, and the secondary
16616 	 reload support requires a single instruction class in the scratch
16617 	 register constraint.  However, right now TFmode is not allowed in
16618 	 Altivec registers, so the pattern will never match.  */
16619       if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
16620 	{
16621 	  cost = 3;			/* 2 mtvsrwz's, 1 fmrgow.  */
16622 	  icode = reg_addr[mode].reload_fpr_gpr;
16623 	}
16624     }
16625 
16626   if (icode != CODE_FOR_nothing)
16627     {
16628       ret = true;
16629       if (sri)
16630 	{
16631 	  sri->icode = icode;
16632 	  sri->extra_cost = cost;
16633 	}
16634     }
16635 
16636   return ret;
16637 }
16638 
16639 /* Return whether a move between two register classes can be done either
16640    directly (simple move) or via a pattern that uses a single extra temporary
16641    (using power8's direct move in this case.  */
16642 
16643 static bool
rs6000_secondary_reload_move(enum rs6000_reg_type to_type,enum rs6000_reg_type from_type,enum machine_mode mode,secondary_reload_info * sri,bool altivec_p)16644 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
16645 			      enum rs6000_reg_type from_type,
16646 			      enum machine_mode mode,
16647 			      secondary_reload_info *sri,
16648 			      bool altivec_p)
16649 {
16650   /* Fall back to load/store reloads if either type is not a register.  */
16651   if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
16652     return false;
16653 
16654   /* If we haven't allocated registers yet, assume the move can be done for the
16655      standard register types.  */
16656   if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
16657       || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
16658       || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
16659     return true;
16660 
16661   /* Moves to the same set of registers is a simple move for non-specialized
16662      registers.  */
16663   if (to_type == from_type && IS_STD_REG_TYPE (to_type))
16664     return true;
16665 
16666   /* Check whether a simple move can be done directly.  */
16667   if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
16668     {
16669       if (sri)
16670 	{
16671 	  sri->icode = CODE_FOR_nothing;
16672 	  sri->extra_cost = 0;
16673 	}
16674       return true;
16675     }
16676 
16677   /* Now check if we can do it in a few steps.  */
16678   return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
16679 					      altivec_p);
16680 }
16681 
16682 /* Inform reload about cases where moving X with a mode MODE to a register in
16683    RCLASS requires an extra scratch or immediate register.  Return the class
16684    needed for the immediate register.
16685 
16686    For VSX and Altivec, we may need a register to convert sp+offset into
16687    reg+sp.
16688 
16689    For misaligned 64-bit gpr loads and stores we need a register to
16690    convert an offset address to indirect.  */
16691 
16692 static reg_class_t
rs6000_secondary_reload(bool in_p,rtx x,reg_class_t rclass_i,enum machine_mode mode,secondary_reload_info * sri)16693 rs6000_secondary_reload (bool in_p,
16694 			 rtx x,
16695 			 reg_class_t rclass_i,
16696 			 enum machine_mode mode,
16697 			 secondary_reload_info *sri)
16698 {
16699   enum reg_class rclass = (enum reg_class) rclass_i;
16700   reg_class_t ret = ALL_REGS;
16701   enum insn_code icode;
16702   bool default_p = false;
16703 
16704   sri->icode = CODE_FOR_nothing;
16705   icode = ((in_p)
16706 	   ? reg_addr[mode].reload_load
16707 	   : reg_addr[mode].reload_store);
16708 
16709   if (REG_P (x) || register_operand (x, mode))
16710     {
16711       enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
16712       bool altivec_p = (rclass == ALTIVEC_REGS);
16713       enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
16714 
16715       if (!in_p)
16716 	{
16717 	  enum rs6000_reg_type exchange = to_type;
16718 	  to_type = from_type;
16719 	  from_type = exchange;
16720 	}
16721 
16722       /* Can we do a direct move of some sort?  */
16723       if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
16724 					altivec_p))
16725 	{
16726 	  icode = (enum insn_code)sri->icode;
16727 	  default_p = false;
16728 	  ret = NO_REGS;
16729 	}
16730     }
16731 
16732   /* Handle vector moves with reload helper functions.  */
16733   if (ret == ALL_REGS && icode != CODE_FOR_nothing)
16734     {
16735       ret = NO_REGS;
16736       sri->icode = CODE_FOR_nothing;
16737       sri->extra_cost = 0;
16738 
16739       if (GET_CODE (x) == MEM)
16740 	{
16741 	  rtx addr = XEXP (x, 0);
16742 
16743 	  /* Loads to and stores from gprs can do reg+offset, and wouldn't need
16744 	     an extra register in that case, but it would need an extra
16745 	     register if the addressing is reg+reg or (reg+reg)&(-16).  Special
16746 	     case load/store quad.  */
16747 	  if (rclass == GENERAL_REGS || rclass == BASE_REGS)
16748 	    {
16749 	      if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
16750 		  && GET_MODE_SIZE (mode) == 16
16751 		  && quad_memory_operand (x, mode))
16752 		{
16753 		  sri->icode = icode;
16754 		  sri->extra_cost = 2;
16755 		}
16756 
16757 	      else if (!legitimate_indirect_address_p (addr, false)
16758 		       && !rs6000_legitimate_offset_address_p (PTImode, addr,
16759 							       false, true))
16760 		{
16761 		  sri->icode = icode;
16762 		  /* account for splitting the loads, and converting the
16763 		     address from reg+reg to reg.  */
16764 		  sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
16765 				     + ((GET_CODE (addr) == AND) ? 1 : 0));
16766 		}
16767 	    }
16768          /* Allow scalar loads to/from the traditional floating point
16769             registers, even if VSX memory is set.  */
16770          else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
16771                   && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
16772                   && (legitimate_indirect_address_p (addr, false)
16773                       || legitimate_indirect_address_p (addr, false)
16774                       || rs6000_legitimate_offset_address_p (mode, addr,
16775                                                              false, true)))
16776 
16777            ;
16778          /* Loads to and stores from vector registers can only do reg+reg
16779             addressing.  Altivec registers can also do (reg+reg)&(-16).  Allow
16780             scalar modes loading up the traditional floating point registers
16781             to use offset addresses.  */
16782 	  else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
16783 		   || rclass == FLOAT_REGS || rclass == NO_REGS)
16784 	    {
16785 	      if (!VECTOR_MEM_ALTIVEC_P (mode)
16786 		  && GET_CODE (addr) == AND
16787 		  && GET_CODE (XEXP (addr, 1)) == CONST_INT
16788 		  && INTVAL (XEXP (addr, 1)) == -16
16789 		  && (legitimate_indirect_address_p (XEXP (addr, 0), false)
16790 		      || legitimate_indexed_address_p (XEXP (addr, 0), false)))
16791 		{
16792 		  sri->icode = icode;
16793 		  sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
16794 				     ? 2 : 1);
16795 		}
16796 	      else if (!legitimate_indirect_address_p (addr, false)
16797 		       && (rclass == NO_REGS
16798 			   || !legitimate_indexed_address_p (addr, false)))
16799 		{
16800 		  sri->icode = icode;
16801 		  sri->extra_cost = 1;
16802 		}
16803 	      else
16804 		icode = CODE_FOR_nothing;
16805 	    }
16806 	  /* Any other loads, including to pseudo registers which haven't been
16807 	     assigned to a register yet, default to require a scratch
16808 	     register.  */
16809 	  else
16810 	    {
16811 	      sri->icode = icode;
16812 	      sri->extra_cost = 2;
16813 	    }
16814 	}
16815       else if (REG_P (x))
16816 	{
16817 	  int regno = true_regnum (x);
16818 
16819 	  icode = CODE_FOR_nothing;
16820 	  if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16821 	    default_p = true;
16822 	  else
16823 	    {
16824 	      enum reg_class xclass = REGNO_REG_CLASS (regno);
16825 	      enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
16826 	      enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
16827 
16828 	      /* If memory is needed, use default_secondary_reload to create the
16829 		 stack slot.  */
16830 	      if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
16831 		default_p = true;
16832 	      else
16833 		ret = NO_REGS;
16834 	    }
16835 	}
16836       else
16837 	default_p = true;
16838     }
16839   else if (TARGET_POWERPC64
16840 	   && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16841 	   && MEM_P (x)
16842 	   && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
16843     {
16844       rtx addr = XEXP (x, 0);
16845       rtx off = address_offset (addr);
16846 
16847       if (off != NULL_RTX)
16848 	{
16849 	  unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16850 	  unsigned HOST_WIDE_INT offset = INTVAL (off);
16851 
16852 	  /* We need a secondary reload when our legitimate_address_p
16853 	     says the address is good (as otherwise the entire address
16854 	     will be reloaded), and the offset is not a multiple of
16855 	     four or we have an address wrap.  Address wrap will only
16856 	     occur for LO_SUMs since legitimate_offset_address_p
16857 	     rejects addresses for 16-byte mems that will wrap.  */
16858 	  if (GET_CODE (addr) == LO_SUM
16859 	      ? (1 /* legitimate_address_p allows any offset for lo_sum */
16860 		 && ((offset & 3) != 0
16861 		     || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
16862 	      : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
16863 		 && (offset & 3) != 0))
16864 	    {
16865 	      if (in_p)
16866 		sri->icode = CODE_FOR_reload_di_load;
16867 	      else
16868 		sri->icode = CODE_FOR_reload_di_store;
16869 	      sri->extra_cost = 2;
16870 	      ret = NO_REGS;
16871 	    }
16872 	  else
16873 	    default_p = true;
16874 	}
16875       else
16876 	default_p = true;
16877     }
16878   else if (!TARGET_POWERPC64
16879 	   && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16880 	   && MEM_P (x)
16881 	   && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
16882     {
16883       rtx addr = XEXP (x, 0);
16884       rtx off = address_offset (addr);
16885 
16886       if (off != NULL_RTX)
16887 	{
16888 	  unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16889 	  unsigned HOST_WIDE_INT offset = INTVAL (off);
16890 
16891 	  /* We need a secondary reload when our legitimate_address_p
16892 	     says the address is good (as otherwise the entire address
16893 	     will be reloaded), and we have a wrap.
16894 
16895 	     legitimate_lo_sum_address_p allows LO_SUM addresses to
16896 	     have any offset so test for wrap in the low 16 bits.
16897 
16898 	     legitimate_offset_address_p checks for the range
16899 	     [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
16900 	     for mode size of 16.  We wrap at [0x7ffc,0x7fff] and
16901 	     [0x7ff4,0x7fff] respectively, so test for the
16902 	     intersection of these ranges, [0x7ffc,0x7fff] and
16903 	     [0x7ff4,0x7ff7] respectively.
16904 
16905 	     Note that the address we see here may have been
16906 	     manipulated by legitimize_reload_address.  */
16907 	  if (GET_CODE (addr) == LO_SUM
16908 	      ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
16909 	      : offset - (0x8000 - extra) < UNITS_PER_WORD)
16910 	    {
16911 	      if (in_p)
16912 		sri->icode = CODE_FOR_reload_si_load;
16913 	      else
16914 		sri->icode = CODE_FOR_reload_si_store;
16915 	      sri->extra_cost = 2;
16916 	      ret = NO_REGS;
16917 	    }
16918 	  else
16919 	    default_p = true;
16920 	}
16921       else
16922 	default_p = true;
16923     }
16924   else
16925     default_p = true;
16926 
16927   if (default_p)
16928     ret = default_secondary_reload (in_p, x, rclass, mode, sri);
16929 
16930   gcc_assert (ret != ALL_REGS);
16931 
16932   if (TARGET_DEBUG_ADDR)
16933     {
16934       fprintf (stderr,
16935 	       "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
16936 	       "mode = %s",
16937 	       reg_class_names[ret],
16938 	       in_p ? "true" : "false",
16939 	       reg_class_names[rclass],
16940 	       GET_MODE_NAME (mode));
16941 
16942       if (default_p)
16943 	fprintf (stderr, ", default secondary reload");
16944 
16945       if (sri->icode != CODE_FOR_nothing)
16946 	fprintf (stderr, ", reload func = %s, extra cost = %d\n",
16947 		 insn_data[sri->icode].name, sri->extra_cost);
16948       else
16949 	fprintf (stderr, "\n");
16950 
16951       debug_rtx (x);
16952     }
16953 
16954   return ret;
16955 }
16956 
16957 /* Better tracing for rs6000_secondary_reload_inner.  */
16958 
16959 static void
rs6000_secondary_reload_trace(int line,rtx reg,rtx mem,rtx scratch,bool store_p)16960 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
16961 			       bool store_p)
16962 {
16963   rtx set, clobber;
16964 
16965   gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
16966 
16967   fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
16968 	   store_p ? "store" : "load");
16969 
16970   if (store_p)
16971     set = gen_rtx_SET (VOIDmode, mem, reg);
16972   else
16973     set = gen_rtx_SET (VOIDmode, reg, mem);
16974 
16975   clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
16976   debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
16977 }
16978 
16979 static void
rs6000_secondary_reload_fail(int line,rtx reg,rtx mem,rtx scratch,bool store_p)16980 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
16981 			      bool store_p)
16982 {
16983   rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
16984   gcc_unreachable ();
16985 }
16986 
16987 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
16988    to SP+reg addressing.  */
16989 
16990 void
rs6000_secondary_reload_inner(rtx reg,rtx mem,rtx scratch,bool store_p)16991 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
16992 {
16993   int regno = true_regnum (reg);
16994   enum machine_mode mode = GET_MODE (reg);
16995   enum reg_class rclass;
16996   rtx addr;
16997   rtx and_op2 = NULL_RTX;
16998   rtx addr_op1;
16999   rtx addr_op2;
17000   rtx scratch_or_premodify = scratch;
17001   rtx and_rtx;
17002   rtx cc_clobber;
17003 
17004   if (TARGET_DEBUG_ADDR)
17005     rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
17006 
17007   if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
17008     rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17009 
17010   if (GET_CODE (mem) != MEM)
17011     rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17012 
17013   rclass = REGNO_REG_CLASS (regno);
17014   addr = find_replacement (&XEXP (mem, 0));
17015 
17016   switch (rclass)
17017     {
17018       /* GPRs can handle reg + small constant, all other addresses need to use
17019 	 the scratch register.  */
17020     case GENERAL_REGS:
17021     case BASE_REGS:
17022       if (GET_CODE (addr) == AND)
17023 	{
17024 	  and_op2 = XEXP (addr, 1);
17025 	  addr = find_replacement (&XEXP (addr, 0));
17026 	}
17027 
17028       if (GET_CODE (addr) == PRE_MODIFY)
17029 	{
17030 	  scratch_or_premodify = find_replacement (&XEXP (addr, 0));
17031 	  if (!REG_P (scratch_or_premodify))
17032 	    rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17033 
17034 	  addr = find_replacement (&XEXP (addr, 1));
17035 	  if (GET_CODE (addr) != PLUS)
17036 	    rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17037 	}
17038 
17039       if (GET_CODE (addr) == PLUS
17040 	  && (and_op2 != NULL_RTX
17041 	      || !rs6000_legitimate_offset_address_p (PTImode, addr,
17042 						      false, true)))
17043 	{
17044 	  /* find_replacement already recurses into both operands of
17045 	     PLUS so we don't need to call it here.  */
17046 	  addr_op1 = XEXP (addr, 0);
17047 	  addr_op2 = XEXP (addr, 1);
17048 	  if (!legitimate_indirect_address_p (addr_op1, false))
17049 	    rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17050 
17051 	  if (!REG_P (addr_op2)
17052 	      && (GET_CODE (addr_op2) != CONST_INT
17053 		  || !satisfies_constraint_I (addr_op2)))
17054 	    {
17055 	      if (TARGET_DEBUG_ADDR)
17056 		{
17057 		  fprintf (stderr,
17058 			   "\nMove plus addr to register %s, mode = %s: ",
17059 			   rs6000_reg_names[REGNO (scratch)],
17060 			   GET_MODE_NAME (mode));
17061 		  debug_rtx (addr_op2);
17062 		}
17063 	      rs6000_emit_move (scratch, addr_op2, Pmode);
17064 	      addr_op2 = scratch;
17065 	    }
17066 
17067 	  emit_insn (gen_rtx_SET (VOIDmode,
17068 				  scratch_or_premodify,
17069 				  gen_rtx_PLUS (Pmode,
17070 						addr_op1,
17071 						addr_op2)));
17072 
17073 	  addr = scratch_or_premodify;
17074 	  scratch_or_premodify = scratch;
17075 	}
17076       else if (!legitimate_indirect_address_p (addr, false)
17077 	       && !rs6000_legitimate_offset_address_p (PTImode, addr,
17078 						       false, true))
17079 	{
17080 	  if (TARGET_DEBUG_ADDR)
17081 	    {
17082 	      fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
17083 		       rs6000_reg_names[REGNO (scratch_or_premodify)],
17084 		       GET_MODE_NAME (mode));
17085 	      debug_rtx (addr);
17086 	    }
17087 	  rs6000_emit_move (scratch_or_premodify, addr, Pmode);
17088 	  addr = scratch_or_premodify;
17089 	  scratch_or_premodify = scratch;
17090 	}
17091       break;
17092 
17093       /* Float registers can do offset+reg addressing for scalar types.  */
17094     case FLOAT_REGS:
17095       if (legitimate_indirect_address_p (addr, false)	/* reg */
17096 	  || legitimate_indexed_address_p (addr, false)	/* reg+reg */
17097 	  || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
17098 	      && and_op2 == NULL_RTX
17099 	      && scratch_or_premodify == scratch
17100 	      && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
17101 	break;
17102 
17103       /* If this isn't a legacy floating point load/store, fall through to the
17104 	 VSX defaults.  */
17105 
17106       /* VSX/Altivec registers can only handle reg+reg addressing.  Move other
17107 	 addresses into a scratch register.  */
17108     case VSX_REGS:
17109     case ALTIVEC_REGS:
17110 
17111       /* With float regs, we need to handle the AND ourselves, since we can't
17112 	 use the Altivec instruction with an implicit AND -16.  Allow scalar
17113 	 loads to float registers to use reg+offset even if VSX.  */
17114       if (GET_CODE (addr) == AND
17115 	  && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
17116 	      || GET_CODE (XEXP (addr, 1)) != CONST_INT
17117 	      || INTVAL (XEXP (addr, 1)) != -16
17118 	      || !VECTOR_MEM_ALTIVEC_P (mode)))
17119 	{
17120 	  and_op2 = XEXP (addr, 1);
17121 	  addr = find_replacement (&XEXP (addr, 0));
17122 	}
17123 
17124       /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
17125 	 as the address later.  */
17126       if (GET_CODE (addr) == PRE_MODIFY
17127 	  && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
17128 	       && (rclass != FLOAT_REGS
17129 		   || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
17130 	      || and_op2 != NULL_RTX
17131 	      || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
17132 	{
17133 	  scratch_or_premodify = find_replacement (&XEXP (addr, 0));
17134 	  if (!legitimate_indirect_address_p (scratch_or_premodify, false))
17135 	    rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17136 
17137 	  addr = find_replacement (&XEXP (addr, 1));
17138 	  if (GET_CODE (addr) != PLUS)
17139 	    rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17140 	}
17141 
17142       if (legitimate_indirect_address_p (addr, false)	/* reg */
17143 	  || legitimate_indexed_address_p (addr, false)	/* reg+reg */
17144 	  || (GET_CODE (addr) == AND			/* Altivec memory */
17145 	      && rclass == ALTIVEC_REGS
17146 	      && GET_CODE (XEXP (addr, 1)) == CONST_INT
17147 	      && INTVAL (XEXP (addr, 1)) == -16
17148 	      && (legitimate_indirect_address_p (XEXP (addr, 0), false)
17149 		  || legitimate_indexed_address_p (XEXP (addr, 0), false))))
17150 	;
17151 
17152       else if (GET_CODE (addr) == PLUS)
17153 	{
17154 	  addr_op1 = XEXP (addr, 0);
17155 	  addr_op2 = XEXP (addr, 1);
17156 	  if (!REG_P (addr_op1))
17157 	    rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17158 
17159 	  if (TARGET_DEBUG_ADDR)
17160 	    {
17161 	      fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
17162 		       rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
17163 	      debug_rtx (addr_op2);
17164 	    }
17165 	  rs6000_emit_move (scratch, addr_op2, Pmode);
17166 	  emit_insn (gen_rtx_SET (VOIDmode,
17167 				  scratch_or_premodify,
17168 				  gen_rtx_PLUS (Pmode,
17169 						addr_op1,
17170 						scratch)));
17171 	  addr = scratch_or_premodify;
17172 	  scratch_or_premodify = scratch;
17173 	}
17174 
17175       else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
17176 	       || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
17177 	       || REG_P (addr))
17178 	{
17179 	  if (TARGET_DEBUG_ADDR)
17180 	    {
17181 	      fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
17182 		       rs6000_reg_names[REGNO (scratch_or_premodify)],
17183 		       GET_MODE_NAME (mode));
17184 	      debug_rtx (addr);
17185 	    }
17186 
17187 	  rs6000_emit_move (scratch_or_premodify, addr, Pmode);
17188 	  addr = scratch_or_premodify;
17189 	  scratch_or_premodify = scratch;
17190 	}
17191 
17192       else
17193 	rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17194 
17195       break;
17196 
17197     default:
17198       rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17199     }
17200 
17201   /* If the original address involved a pre-modify that we couldn't use the VSX
17202      memory instruction with update, and we haven't taken care of already,
17203      store the address in the pre-modify register and use that as the
17204      address.  */
17205   if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
17206     {
17207       emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
17208       addr = scratch_or_premodify;
17209     }
17210 
17211   /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
17212      memory instruction, recreate the AND now, including the clobber which is
17213      generated by the general ANDSI3/ANDDI3 patterns for the
17214      andi. instruction.  */
17215   if (and_op2 != NULL_RTX)
17216     {
17217       if (! legitimate_indirect_address_p (addr, false))
17218 	{
17219 	  emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
17220 	  addr = scratch;
17221 	}
17222 
17223       if (TARGET_DEBUG_ADDR)
17224 	{
17225 	  fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
17226 		   rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
17227 	  debug_rtx (and_op2);
17228 	}
17229 
17230       and_rtx = gen_rtx_SET (VOIDmode,
17231 			     scratch,
17232 			     gen_rtx_AND (Pmode,
17233 					  addr,
17234 					  and_op2));
17235 
17236       cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
17237       emit_insn (gen_rtx_PARALLEL (VOIDmode,
17238 				   gen_rtvec (2, and_rtx, cc_clobber)));
17239       addr = scratch;
17240     }
17241 
17242   /* Adjust the address if it changed.  */
17243   if (addr != XEXP (mem, 0))
17244     {
17245       mem = replace_equiv_address_nv (mem, addr);
17246       if (TARGET_DEBUG_ADDR)
17247 	fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
17248     }
17249 
17250   /* Now create the move.  */
17251   if (store_p)
17252     emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
17253   else
17254     emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
17255 
17256   return;
17257 }
17258 
17259 /* Convert reloads involving 64-bit gprs and misaligned offset
17260    addressing, or multiple 32-bit gprs and offsets that are too large,
17261    to use indirect addressing.  */
17262 
17263 void
rs6000_secondary_reload_gpr(rtx reg,rtx mem,rtx scratch,bool store_p)17264 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
17265 {
17266   int regno = true_regnum (reg);
17267   enum reg_class rclass;
17268   rtx addr;
17269   rtx scratch_or_premodify = scratch;
17270 
17271   if (TARGET_DEBUG_ADDR)
17272     {
17273       fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
17274 	       store_p ? "store" : "load");
17275       fprintf (stderr, "reg:\n");
17276       debug_rtx (reg);
17277       fprintf (stderr, "mem:\n");
17278       debug_rtx (mem);
17279       fprintf (stderr, "scratch:\n");
17280       debug_rtx (scratch);
17281     }
17282 
17283   gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
17284   gcc_assert (GET_CODE (mem) == MEM);
17285   rclass = REGNO_REG_CLASS (regno);
17286   gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
17287   addr = XEXP (mem, 0);
17288 
17289   if (GET_CODE (addr) == PRE_MODIFY)
17290     {
17291       scratch_or_premodify = XEXP (addr, 0);
17292       gcc_assert (REG_P (scratch_or_premodify));
17293       addr = XEXP (addr, 1);
17294     }
17295   gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
17296 
17297   rs6000_emit_move (scratch_or_premodify, addr, Pmode);
17298 
17299   mem = replace_equiv_address_nv (mem, scratch_or_premodify);
17300 
17301   /* Now create the move.  */
17302   if (store_p)
17303     emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
17304   else
17305     emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
17306 
17307   return;
17308 }
17309 
17310 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
17311    this function has any SDmode references.  If we are on a power7 or later, we
17312    don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
17313    can load/store the value.  */
17314 
17315 static void
rs6000_alloc_sdmode_stack_slot(void)17316 rs6000_alloc_sdmode_stack_slot (void)
17317 {
17318   tree t;
17319   basic_block bb;
17320   gimple_stmt_iterator gsi;
17321 
17322   gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
17323   /* We use a different approach for dealing with the secondary
17324      memory in LRA.  */
17325   if (ira_use_lra_p)
17326     return;
17327 
17328   if (TARGET_NO_SDMODE_STACK)
17329     return;
17330 
17331   FOR_EACH_BB (bb)
17332     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
17333       {
17334 	tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
17335 	if (ret)
17336 	  {
17337 	    rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
17338 	    cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
17339 								  SDmode, 0);
17340 	    return;
17341 	  }
17342       }
17343 
17344   /* Check for any SDmode parameters of the function.  */
17345   for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
17346     {
17347       if (TREE_TYPE (t) == error_mark_node)
17348 	continue;
17349 
17350       if (TYPE_MODE (TREE_TYPE (t)) == SDmode
17351 	  || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
17352 	{
17353 	  rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
17354 	  cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
17355 								SDmode, 0);
17356 	  return;
17357 	}
17358     }
17359 }
17360 
17361 static void
rs6000_instantiate_decls(void)17362 rs6000_instantiate_decls (void)
17363 {
17364   if (cfun->machine->sdmode_stack_slot != NULL_RTX)
17365     instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
17366 }
17367 
17368 /* Given an rtx X being reloaded into a reg required to be
17369    in class CLASS, return the class of reg to actually use.
17370    In general this is just CLASS; but on some machines
17371    in some cases it is preferable to use a more restrictive class.
17372 
17373    On the RS/6000, we have to return NO_REGS when we want to reload a
17374    floating-point CONST_DOUBLE to force it to be copied to memory.
17375 
17376    We also don't want to reload integer values into floating-point
17377    registers if we can at all help it.  In fact, this can
17378    cause reload to die, if it tries to generate a reload of CTR
17379    into a FP register and discovers it doesn't have the memory location
17380    required.
17381 
17382    ??? Would it be a good idea to have reload do the converse, that is
17383    try to reload floating modes into FP registers if possible?
17384  */
17385 
17386 static enum reg_class
rs6000_preferred_reload_class(rtx x,enum reg_class rclass)17387 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
17388 {
17389   enum machine_mode mode = GET_MODE (x);
17390 
17391   if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
17392     return rclass;
17393 
17394   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
17395       && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
17396       && easy_vector_constant (x, mode))
17397     return ALTIVEC_REGS;
17398 
17399   if ((CONSTANT_P (x) || GET_CODE (x) == PLUS))
17400     {
17401       if (reg_class_subset_p (GENERAL_REGS, rclass))
17402 	return GENERAL_REGS;
17403       if (reg_class_subset_p (BASE_REGS, rclass))
17404 	return BASE_REGS;
17405       return NO_REGS;
17406     }
17407 
17408   if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
17409     return GENERAL_REGS;
17410 
17411   /* For VSX, prefer the traditional registers for 64-bit values because we can
17412      use the non-VSX loads.  Prefer the Altivec registers if Altivec is
17413      handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
17414      prefer Altivec loads..  */
17415   if (rclass == VSX_REGS)
17416     {
17417       if (MEM_P (x) && reg_addr[mode].scalar_in_vmx_p)
17418 	{
17419 	  rtx addr = XEXP (x, 0);
17420 	  if (rs6000_legitimate_offset_address_p (mode, addr, false, true)
17421 	      || legitimate_lo_sum_address_p (mode, addr, false))
17422 	    return FLOAT_REGS;
17423 	}
17424       else if (GET_MODE_SIZE (mode) <= 8 && !reg_addr[mode].scalar_in_vmx_p)
17425 	return FLOAT_REGS;
17426 
17427       if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
17428 	  || mode == V1TImode)
17429 	return ALTIVEC_REGS;
17430 
17431       return rclass;
17432     }
17433 
17434   return rclass;
17435 }
17436 
17437 /* Debug version of rs6000_preferred_reload_class.  */
17438 static enum reg_class
rs6000_debug_preferred_reload_class(rtx x,enum reg_class rclass)17439 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
17440 {
17441   enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
17442 
17443   fprintf (stderr,
17444 	   "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
17445 	   "mode = %s, x:\n",
17446 	   reg_class_names[ret], reg_class_names[rclass],
17447 	   GET_MODE_NAME (GET_MODE (x)));
17448   debug_rtx (x);
17449 
17450   return ret;
17451 }
17452 
17453 /* If we are copying between FP or AltiVec registers and anything else, we need
17454    a memory location.  The exception is when we are targeting ppc64 and the
17455    move to/from fpr to gpr instructions are available.  Also, under VSX, you
17456    can copy vector registers from the FP register set to the Altivec register
17457    set and vice versa.  */
17458 
17459 static bool
rs6000_secondary_memory_needed(enum reg_class from_class,enum reg_class to_class,enum machine_mode mode)17460 rs6000_secondary_memory_needed (enum reg_class from_class,
17461 				enum reg_class to_class,
17462 				enum machine_mode mode)
17463 {
17464   enum rs6000_reg_type from_type, to_type;
17465   bool altivec_p = ((from_class == ALTIVEC_REGS)
17466 		    || (to_class == ALTIVEC_REGS));
17467 
17468   /* If a simple/direct move is available, we don't need secondary memory  */
17469   from_type = reg_class_to_reg_type[(int)from_class];
17470   to_type = reg_class_to_reg_type[(int)to_class];
17471 
17472   if (rs6000_secondary_reload_move (to_type, from_type, mode,
17473 				    (secondary_reload_info *)0, altivec_p))
17474     return false;
17475 
17476   /* If we have a floating point or vector register class, we need to use
17477      memory to transfer the data.  */
17478   if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
17479     return true;
17480 
17481   return false;
17482 }
17483 
17484 /* Debug version of rs6000_secondary_memory_needed.  */
17485 static bool
rs6000_debug_secondary_memory_needed(enum reg_class from_class,enum reg_class to_class,enum machine_mode mode)17486 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
17487 				      enum reg_class to_class,
17488 				      enum machine_mode mode)
17489 {
17490   bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
17491 
17492   fprintf (stderr,
17493 	   "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
17494 	   "to_class = %s, mode = %s\n",
17495 	   ret ? "true" : "false",
17496 	   reg_class_names[from_class],
17497 	   reg_class_names[to_class],
17498 	   GET_MODE_NAME (mode));
17499 
17500   return ret;
17501 }
17502 
17503 /* Return the register class of a scratch register needed to copy IN into
17504    or out of a register in RCLASS in MODE.  If it can be done directly,
17505    NO_REGS is returned.  */
17506 
17507 static enum reg_class
rs6000_secondary_reload_class(enum reg_class rclass,enum machine_mode mode,rtx in)17508 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
17509 			       rtx in)
17510 {
17511   int regno;
17512 
17513   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
17514 #if TARGET_MACHO
17515 		     && MACHOPIC_INDIRECT
17516 #endif
17517 		     ))
17518     {
17519       /* We cannot copy a symbolic operand directly into anything
17520 	 other than BASE_REGS for TARGET_ELF.  So indicate that a
17521 	 register from BASE_REGS is needed as an intermediate
17522 	 register.
17523 
17524 	 On Darwin, pic addresses require a load from memory, which
17525 	 needs a base register.  */
17526       if (rclass != BASE_REGS
17527 	  && (GET_CODE (in) == SYMBOL_REF
17528 	      || GET_CODE (in) == HIGH
17529 	      || GET_CODE (in) == LABEL_REF
17530 	      || GET_CODE (in) == CONST))
17531 	return BASE_REGS;
17532     }
17533 
17534   if (GET_CODE (in) == REG)
17535     {
17536       regno = REGNO (in);
17537       if (regno >= FIRST_PSEUDO_REGISTER)
17538 	{
17539 	  regno = true_regnum (in);
17540 	  if (regno >= FIRST_PSEUDO_REGISTER)
17541 	    regno = -1;
17542 	}
17543     }
17544   else if (GET_CODE (in) == SUBREG)
17545     {
17546       regno = true_regnum (in);
17547       if (regno >= FIRST_PSEUDO_REGISTER)
17548 	regno = -1;
17549     }
17550   else
17551     regno = -1;
17552 
17553   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
17554      into anything.  */
17555   if (rclass == GENERAL_REGS || rclass == BASE_REGS
17556       || (regno >= 0 && INT_REGNO_P (regno)))
17557     return NO_REGS;
17558 
17559   /* Constants, memory, and FP registers can go into FP registers.  */
17560   if ((regno == -1 || FP_REGNO_P (regno))
17561       && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
17562     return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
17563 
17564   /* Memory, and FP/altivec registers can go into fp/altivec registers under
17565      VSX.  However, for scalar variables, use the traditional floating point
17566      registers so that we can use offset+register addressing.  */
17567   if (TARGET_VSX
17568       && (regno == -1 || VSX_REGNO_P (regno))
17569       && VSX_REG_CLASS_P (rclass))
17570     {
17571       if (GET_MODE_SIZE (mode) < 16)
17572 	return FLOAT_REGS;
17573 
17574       return NO_REGS;
17575     }
17576 
17577   /* Memory, and AltiVec registers can go into AltiVec registers.  */
17578   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
17579       && rclass == ALTIVEC_REGS)
17580     return NO_REGS;
17581 
17582   /* We can copy among the CR registers.  */
17583   if ((rclass == CR_REGS || rclass == CR0_REGS)
17584       && regno >= 0 && CR_REGNO_P (regno))
17585     return NO_REGS;
17586 
17587   /* Otherwise, we need GENERAL_REGS.  */
17588   return GENERAL_REGS;
17589 }
17590 
17591 /* Debug version of rs6000_secondary_reload_class.  */
17592 static enum reg_class
rs6000_debug_secondary_reload_class(enum reg_class rclass,enum machine_mode mode,rtx in)17593 rs6000_debug_secondary_reload_class (enum reg_class rclass,
17594 				     enum machine_mode mode, rtx in)
17595 {
17596   enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
17597   fprintf (stderr,
17598 	   "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
17599 	   "mode = %s, input rtx:\n",
17600 	   reg_class_names[ret], reg_class_names[rclass],
17601 	   GET_MODE_NAME (mode));
17602   debug_rtx (in);
17603 
17604   return ret;
17605 }
17606 
17607 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid.  */
17608 
17609 static bool
rs6000_cannot_change_mode_class(enum machine_mode from,enum machine_mode to,enum reg_class rclass)17610 rs6000_cannot_change_mode_class (enum machine_mode from,
17611 				 enum machine_mode to,
17612 				 enum reg_class rclass)
17613 {
17614   unsigned from_size = GET_MODE_SIZE (from);
17615   unsigned to_size = GET_MODE_SIZE (to);
17616 
17617   if (from_size != to_size)
17618     {
17619       enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
17620 
17621       if (reg_classes_intersect_p (xclass, rclass))
17622 	{
17623 	  unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
17624 	  unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
17625 
17626 	  /* Don't allow 64-bit types to overlap with 128-bit types that take a
17627 	     single register under VSX because the scalar part of the register
17628 	     is in the upper 64-bits, and not the lower 64-bits.  Types like
17629 	     TFmode/TDmode that take 2 scalar register can overlap.  128-bit
17630 	     IEEE floating point can't overlap, and neither can small
17631 	     values.  */
17632 
17633 	  if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
17634 	    return true;
17635 
17636 	  /* TDmode in floating-mode registers must always go into a register
17637 	     pair with the most significant word in the even-numbered register
17638 	     to match ISA requirements.  In little-endian mode, this does not
17639 	     match subreg numbering, so we cannot allow subregs.  */
17640 	  if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
17641 	    return true;
17642 
17643 	  if (from_size < 8 || to_size < 8)
17644 	    return true;
17645 
17646 	  if (from_size == 8 && (8 * to_nregs) != to_size)
17647 	    return true;
17648 
17649 	  if (to_size == 8 && (8 * from_nregs) != from_size)
17650 	    return true;
17651 
17652 	  return false;
17653 	}
17654       else
17655 	return false;
17656     }
17657 
17658   if (TARGET_E500_DOUBLE
17659       && ((((to) == DFmode) + ((from) == DFmode)) == 1
17660 	  || (((to) == TFmode) + ((from) == TFmode)) == 1
17661 	  || (((to) == DDmode) + ((from) == DDmode)) == 1
17662 	  || (((to) == TDmode) + ((from) == TDmode)) == 1
17663 	  || (((to) == DImode) + ((from) == DImode)) == 1))
17664     return true;
17665 
17666   /* Since the VSX register set includes traditional floating point registers
17667      and altivec registers, just check for the size being different instead of
17668      trying to check whether the modes are vector modes.  Otherwise it won't
17669      allow say DF and DI to change classes.  For types like TFmode and TDmode
17670      that take 2 64-bit registers, rather than a single 128-bit register, don't
17671      allow subregs of those types to other 128 bit types.  */
17672   if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
17673     {
17674       unsigned num_regs = (from_size + 15) / 16;
17675       if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
17676 	  || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
17677 	return true;
17678 
17679       return (from_size != 8 && from_size != 16);
17680     }
17681 
17682   if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
17683       && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
17684     return true;
17685 
17686   if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
17687       && reg_classes_intersect_p (GENERAL_REGS, rclass))
17688     return true;
17689 
17690   return false;
17691 }
17692 
17693 /* Debug version of rs6000_cannot_change_mode_class.  */
17694 static bool
rs6000_debug_cannot_change_mode_class(enum machine_mode from,enum machine_mode to,enum reg_class rclass)17695 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
17696 				       enum machine_mode to,
17697 				       enum reg_class rclass)
17698 {
17699   bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
17700 
17701   fprintf (stderr,
17702 	   "rs6000_cannot_change_mode_class, return %s, from = %s, "
17703 	   "to = %s, rclass = %s\n",
17704 	   ret ? "true" : "false",
17705 	   GET_MODE_NAME (from), GET_MODE_NAME (to),
17706 	   reg_class_names[rclass]);
17707 
17708   return ret;
17709 }
17710 
17711 /* Return a string to do a move operation of 128 bits of data.  */
17712 
17713 const char *
rs6000_output_move_128bit(rtx operands[])17714 rs6000_output_move_128bit (rtx operands[])
17715 {
17716   rtx dest = operands[0];
17717   rtx src = operands[1];
17718   enum machine_mode mode = GET_MODE (dest);
17719   int dest_regno;
17720   int src_regno;
17721   bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
17722   bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
17723 
17724   if (REG_P (dest))
17725     {
17726       dest_regno = REGNO (dest);
17727       dest_gpr_p = INT_REGNO_P (dest_regno);
17728       dest_fp_p = FP_REGNO_P (dest_regno);
17729       dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
17730       dest_vsx_p = dest_fp_p | dest_vmx_p;
17731     }
17732   else
17733     {
17734       dest_regno = -1;
17735       dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
17736     }
17737 
17738   if (REG_P (src))
17739     {
17740       src_regno = REGNO (src);
17741       src_gpr_p = INT_REGNO_P (src_regno);
17742       src_fp_p = FP_REGNO_P (src_regno);
17743       src_vmx_p = ALTIVEC_REGNO_P (src_regno);
17744       src_vsx_p = src_fp_p | src_vmx_p;
17745     }
17746   else
17747     {
17748       src_regno = -1;
17749       src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
17750     }
17751 
17752   /* Register moves.  */
17753   if (dest_regno >= 0 && src_regno >= 0)
17754     {
17755       if (dest_gpr_p)
17756 	{
17757 	  if (src_gpr_p)
17758 	    return "#";
17759 
17760 	  else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
17761 	    return "#";
17762 	}
17763 
17764       else if (TARGET_VSX && dest_vsx_p)
17765 	{
17766 	  if (src_vsx_p)
17767 	    return "xxlor %x0,%x1,%x1";
17768 
17769 	  else if (TARGET_DIRECT_MOVE && src_gpr_p)
17770 	    return "#";
17771 	}
17772 
17773       else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
17774 	return "vor %0,%1,%1";
17775 
17776       else if (dest_fp_p && src_fp_p)
17777 	return "#";
17778     }
17779 
17780   /* Loads.  */
17781   else if (dest_regno >= 0 && MEM_P (src))
17782     {
17783       if (dest_gpr_p)
17784 	{
17785 	  if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17786 	    return "lq %0,%1";
17787 	  else
17788 	    return "#";
17789 	}
17790 
17791       else if (TARGET_ALTIVEC && dest_vmx_p
17792 	       && altivec_indexed_or_indirect_operand (src, mode))
17793 	return "lvx %0,%y1";
17794 
17795       else if (TARGET_VSX && dest_vsx_p)
17796 	{
17797 	  if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17798 	    return "lxvw4x %x0,%y1";
17799 	  else
17800 	    return "lxvd2x %x0,%y1";
17801 	}
17802 
17803       else if (TARGET_ALTIVEC && dest_vmx_p)
17804 	return "lvx %0,%y1";
17805 
17806       else if (dest_fp_p)
17807 	return "#";
17808     }
17809 
17810   /* Stores.  */
17811   else if (src_regno >= 0 && MEM_P (dest))
17812     {
17813       if (src_gpr_p)
17814 	{
17815  	  if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17816 	    return "stq %1,%0";
17817 	  else
17818 	    return "#";
17819 	}
17820 
17821       else if (TARGET_ALTIVEC && src_vmx_p
17822 	       && altivec_indexed_or_indirect_operand (src, mode))
17823 	return "stvx %1,%y0";
17824 
17825       else if (TARGET_VSX && src_vsx_p)
17826 	{
17827 	  if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17828 	    return "stxvw4x %x1,%y0";
17829 	  else
17830 	    return "stxvd2x %x1,%y0";
17831 	}
17832 
17833       else if (TARGET_ALTIVEC && src_vmx_p)
17834 	return "stvx %1,%y0";
17835 
17836       else if (src_fp_p)
17837 	return "#";
17838     }
17839 
17840   /* Constants.  */
17841   else if (dest_regno >= 0
17842 	   && (GET_CODE (src) == CONST_INT
17843 	       || GET_CODE (src) == CONST_DOUBLE
17844 	       || GET_CODE (src) == CONST_VECTOR))
17845     {
17846       if (dest_gpr_p)
17847 	return "#";
17848 
17849       else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
17850 	return "xxlxor %x0,%x0,%x0";
17851 
17852       else if (TARGET_ALTIVEC && dest_vmx_p)
17853 	return output_vec_const_move (operands);
17854     }
17855 
17856   if (TARGET_DEBUG_ADDR)
17857     {
17858       fprintf (stderr, "\n===== Bad 128 bit move:\n");
17859       debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
17860     }
17861 
17862   gcc_unreachable ();
17863 }
17864 
17865 /* Validate a 128-bit move.  */
17866 bool
rs6000_move_128bit_ok_p(rtx operands[])17867 rs6000_move_128bit_ok_p (rtx operands[])
17868 {
17869   enum machine_mode mode = GET_MODE (operands[0]);
17870   return (gpc_reg_operand (operands[0], mode)
17871 	  || gpc_reg_operand (operands[1], mode));
17872 }
17873 
17874 /* Return true if a 128-bit move needs to be split.  */
17875 bool
rs6000_split_128bit_ok_p(rtx operands[])17876 rs6000_split_128bit_ok_p (rtx operands[])
17877 {
17878   if (!reload_completed)
17879     return false;
17880 
17881   if (!gpr_or_gpr_p (operands[0], operands[1]))
17882     return false;
17883 
17884   if (quad_load_store_p (operands[0], operands[1]))
17885     return false;
17886 
17887   return true;
17888 }
17889 
17890 
17891 /* Given a comparison operation, return the bit number in CCR to test.  We
17892    know this is a valid comparison.
17893 
17894    SCC_P is 1 if this is for an scc.  That means that %D will have been
17895    used instead of %C, so the bits will be in different places.
17896 
17897    Return -1 if OP isn't a valid comparison for some reason.  */
17898 
17899 int
ccr_bit(rtx op,int scc_p)17900 ccr_bit (rtx op, int scc_p)
17901 {
17902   enum rtx_code code = GET_CODE (op);
17903   enum machine_mode cc_mode;
17904   int cc_regnum;
17905   int base_bit;
17906   rtx reg;
17907 
17908   if (!COMPARISON_P (op))
17909     return -1;
17910 
17911   reg = XEXP (op, 0);
17912 
17913   gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
17914 
17915   cc_mode = GET_MODE (reg);
17916   cc_regnum = REGNO (reg);
17917   base_bit = 4 * (cc_regnum - CR0_REGNO);
17918 
17919   validate_condition_mode (code, cc_mode);
17920 
17921   /* When generating a sCOND operation, only positive conditions are
17922      allowed.  */
17923   gcc_assert (!scc_p
17924 	      || code == EQ || code == GT || code == LT || code == UNORDERED
17925 	      || code == GTU || code == LTU);
17926 
17927   switch (code)
17928     {
17929     case NE:
17930       return scc_p ? base_bit + 3 : base_bit + 2;
17931     case EQ:
17932       return base_bit + 2;
17933     case GT:  case GTU:  case UNLE:
17934       return base_bit + 1;
17935     case LT:  case LTU:  case UNGE:
17936       return base_bit;
17937     case ORDERED:  case UNORDERED:
17938       return base_bit + 3;
17939 
17940     case GE:  case GEU:
17941       /* If scc, we will have done a cror to put the bit in the
17942 	 unordered position.  So test that bit.  For integer, this is ! LT
17943 	 unless this is an scc insn.  */
17944       return scc_p ? base_bit + 3 : base_bit;
17945 
17946     case LE:  case LEU:
17947       return scc_p ? base_bit + 3 : base_bit + 1;
17948 
17949     default:
17950       gcc_unreachable ();
17951     }
17952 }
17953 
17954 /* Return the GOT register.  */
17955 
17956 rtx
rs6000_got_register(rtx value ATTRIBUTE_UNUSED)17957 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
17958 {
17959   /* The second flow pass currently (June 1999) can't update
17960      regs_ever_live without disturbing other parts of the compiler, so
17961      update it here to make the prolog/epilogue code happy.  */
17962   if (!can_create_pseudo_p ()
17963       && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17964     df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
17965 
17966   crtl->uses_pic_offset_table = 1;
17967 
17968   return pic_offset_table_rtx;
17969 }
17970 
17971 static rs6000_stack_t stack_info;
17972 
17973 /* Function to init struct machine_function.
17974    This will be called, via a pointer variable,
17975    from push_function_context.  */
17976 
17977 static struct machine_function *
rs6000_init_machine_status(void)17978 rs6000_init_machine_status (void)
17979 {
17980   stack_info.reload_completed = 0;
17981   return ggc_alloc_cleared_machine_function ();
17982 }
17983 
17984 /* These macros test for integers and extract the low-order bits.  */
17985 #define INT_P(X)  \
17986 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)	\
17987  && GET_MODE (X) == VOIDmode)
17988 
17989 #define INT_LOWPART(X) \
17990   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
17991 
17992 int
extract_MB(rtx op)17993 extract_MB (rtx op)
17994 {
17995   int i;
17996   unsigned long val = INT_LOWPART (op);
17997 
17998   /* If the high bit is zero, the value is the first 1 bit we find
17999      from the left.  */
18000   if ((val & 0x80000000) == 0)
18001     {
18002       gcc_assert (val & 0xffffffff);
18003 
18004       i = 1;
18005       while (((val <<= 1) & 0x80000000) == 0)
18006 	++i;
18007       return i;
18008     }
18009 
18010   /* If the high bit is set and the low bit is not, or the mask is all
18011      1's, the value is zero.  */
18012   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
18013     return 0;
18014 
18015   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
18016      from the right.  */
18017   i = 31;
18018   while (((val >>= 1) & 1) != 0)
18019     --i;
18020 
18021   return i;
18022 }
18023 
18024 int
extract_ME(rtx op)18025 extract_ME (rtx op)
18026 {
18027   int i;
18028   unsigned long val = INT_LOWPART (op);
18029 
18030   /* If the low bit is zero, the value is the first 1 bit we find from
18031      the right.  */
18032   if ((val & 1) == 0)
18033     {
18034       gcc_assert (val & 0xffffffff);
18035 
18036       i = 30;
18037       while (((val >>= 1) & 1) == 0)
18038 	--i;
18039 
18040       return i;
18041     }
18042 
18043   /* If the low bit is set and the high bit is not, or the mask is all
18044      1's, the value is 31.  */
18045   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
18046     return 31;
18047 
18048   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
18049      from the left.  */
18050   i = 0;
18051   while (((val <<= 1) & 0x80000000) != 0)
18052     ++i;
18053 
18054   return i;
18055 }
18056 
18057 /* Locate some local-dynamic symbol still in use by this function
18058    so that we can print its name in some tls_ld pattern.  */
18059 
18060 static const char *
rs6000_get_some_local_dynamic_name(void)18061 rs6000_get_some_local_dynamic_name (void)
18062 {
18063   rtx insn;
18064 
18065   if (cfun->machine->some_ld_name)
18066     return cfun->machine->some_ld_name;
18067 
18068   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
18069     if (INSN_P (insn)
18070 	&& for_each_rtx (&PATTERN (insn),
18071 			 rs6000_get_some_local_dynamic_name_1, 0))
18072       return cfun->machine->some_ld_name;
18073 
18074   gcc_unreachable ();
18075 }
18076 
18077 /* Helper function for rs6000_get_some_local_dynamic_name.  */
18078 
18079 static int
rs6000_get_some_local_dynamic_name_1(rtx * px,void * data ATTRIBUTE_UNUSED)18080 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
18081 {
18082   rtx x = *px;
18083 
18084   if (GET_CODE (x) == SYMBOL_REF)
18085     {
18086       const char *str = XSTR (x, 0);
18087       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
18088 	{
18089 	  cfun->machine->some_ld_name = str;
18090 	  return 1;
18091 	}
18092     }
18093 
18094   return 0;
18095 }
18096 
18097 /* Write out a function code label.  */
18098 
18099 void
rs6000_output_function_entry(FILE * file,const char * fname)18100 rs6000_output_function_entry (FILE *file, const char *fname)
18101 {
18102   if (fname[0] != '.')
18103     {
18104       switch (DEFAULT_ABI)
18105 	{
18106 	default:
18107 	  gcc_unreachable ();
18108 
18109 	case ABI_AIX:
18110 	  if (DOT_SYMBOLS)
18111 	    putc ('.', file);
18112 	  else
18113 	    ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
18114 	  break;
18115 
18116 	case ABI_ELFv2:
18117 	case ABI_V4:
18118 	case ABI_DARWIN:
18119 	  break;
18120 	}
18121     }
18122 
18123   RS6000_OUTPUT_BASENAME (file, fname);
18124 }
18125 
18126 /* Print an operand.  Recognize special options, documented below.  */
18127 
18128 #if TARGET_ELF
18129 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
18130 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
18131 #else
18132 #define SMALL_DATA_RELOC "sda21"
18133 #define SMALL_DATA_REG 0
18134 #endif
18135 
18136 void
print_operand(FILE * file,rtx x,int code)18137 print_operand (FILE *file, rtx x, int code)
18138 {
18139   int i;
18140   unsigned HOST_WIDE_INT uval;
18141 
18142   switch (code)
18143     {
18144       /* %a is output_address.  */
18145 
18146     case 'b':
18147       /* If constant, low-order 16 bits of constant, unsigned.
18148 	 Otherwise, write normally.  */
18149       if (INT_P (x))
18150 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
18151       else
18152 	print_operand (file, x, 0);
18153       return;
18154 
18155     case 'B':
18156       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
18157 	 for 64-bit mask direction.  */
18158       putc (((INT_LOWPART (x) & 1) == 0 ? 'r' : 'l'), file);
18159       return;
18160 
18161       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
18162 	 output_operand.  */
18163 
18164     case 'D':
18165       /* Like 'J' but get to the GT bit only.  */
18166       gcc_assert (REG_P (x));
18167 
18168       /* Bit 1 is GT bit.  */
18169       i = 4 * (REGNO (x) - CR0_REGNO) + 1;
18170 
18171       /* Add one for shift count in rlinm for scc.  */
18172       fprintf (file, "%d", i + 1);
18173       return;
18174 
18175     case 'E':
18176       /* X is a CR register.  Print the number of the EQ bit of the CR */
18177       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18178 	output_operand_lossage ("invalid %%E value");
18179       else
18180 	fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
18181       return;
18182 
18183     case 'f':
18184       /* X is a CR register.  Print the shift count needed to move it
18185 	 to the high-order four bits.  */
18186       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18187 	output_operand_lossage ("invalid %%f value");
18188       else
18189 	fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
18190       return;
18191 
18192     case 'F':
18193       /* Similar, but print the count for the rotate in the opposite
18194 	 direction.  */
18195       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18196 	output_operand_lossage ("invalid %%F value");
18197       else
18198 	fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
18199       return;
18200 
18201     case 'G':
18202       /* X is a constant integer.  If it is negative, print "m",
18203 	 otherwise print "z".  This is to make an aze or ame insn.  */
18204       if (GET_CODE (x) != CONST_INT)
18205 	output_operand_lossage ("invalid %%G value");
18206       else if (INTVAL (x) >= 0)
18207 	putc ('z', file);
18208       else
18209 	putc ('m', file);
18210       return;
18211 
18212     case 'h':
18213       /* If constant, output low-order five bits.  Otherwise, write
18214 	 normally.  */
18215       if (INT_P (x))
18216 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
18217       else
18218 	print_operand (file, x, 0);
18219       return;
18220 
18221     case 'H':
18222       /* If constant, output low-order six bits.  Otherwise, write
18223 	 normally.  */
18224       if (INT_P (x))
18225 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
18226       else
18227 	print_operand (file, x, 0);
18228       return;
18229 
18230     case 'I':
18231       /* Print `i' if this is a constant, else nothing.  */
18232       if (INT_P (x))
18233 	putc ('i', file);
18234       return;
18235 
18236     case 'j':
18237       /* Write the bit number in CCR for jump.  */
18238       i = ccr_bit (x, 0);
18239       if (i == -1)
18240 	output_operand_lossage ("invalid %%j code");
18241       else
18242 	fprintf (file, "%d", i);
18243       return;
18244 
18245     case 'J':
18246       /* Similar, but add one for shift count in rlinm for scc and pass
18247 	 scc flag to `ccr_bit'.  */
18248       i = ccr_bit (x, 1);
18249       if (i == -1)
18250 	output_operand_lossage ("invalid %%J code");
18251       else
18252 	/* If we want bit 31, write a shift count of zero, not 32.  */
18253 	fprintf (file, "%d", i == 31 ? 0 : i + 1);
18254       return;
18255 
18256     case 'k':
18257       /* X must be a constant.  Write the 1's complement of the
18258 	 constant.  */
18259       if (! INT_P (x))
18260 	output_operand_lossage ("invalid %%k value");
18261       else
18262 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
18263       return;
18264 
18265     case 'K':
18266       /* X must be a symbolic constant on ELF.  Write an
18267 	 expression suitable for an 'addi' that adds in the low 16
18268 	 bits of the MEM.  */
18269       if (GET_CODE (x) == CONST)
18270 	{
18271 	  if (GET_CODE (XEXP (x, 0)) != PLUS
18272 	      || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
18273 		  && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
18274 	      || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
18275 	    output_operand_lossage ("invalid %%K value");
18276 	}
18277       print_operand_address (file, x);
18278       fputs ("@l", file);
18279       return;
18280 
18281       /* %l is output_asm_label.  */
18282 
18283     case 'L':
18284       /* Write second word of DImode or DFmode reference.  Works on register
18285 	 or non-indexed memory only.  */
18286       if (REG_P (x))
18287 	fputs (reg_names[REGNO (x) + 1], file);
18288       else if (MEM_P (x))
18289 	{
18290 	  /* Handle possible auto-increment.  Since it is pre-increment and
18291 	     we have already done it, we can just use an offset of word.  */
18292 	  if (GET_CODE (XEXP (x, 0)) == PRE_INC
18293 	      || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18294 	    output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
18295 					   UNITS_PER_WORD));
18296 	  else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18297 	    output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
18298 					   UNITS_PER_WORD));
18299 	  else
18300 	    output_address (XEXP (adjust_address_nv (x, SImode,
18301 						     UNITS_PER_WORD),
18302 				  0));
18303 
18304 	  if (small_data_operand (x, GET_MODE (x)))
18305 	    fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18306 		     reg_names[SMALL_DATA_REG]);
18307 	}
18308       return;
18309 
18310     case 'm':
18311       /* MB value for a mask operand.  */
18312       if (! mask_operand (x, SImode))
18313 	output_operand_lossage ("invalid %%m value");
18314 
18315       fprintf (file, "%d", extract_MB (x));
18316       return;
18317 
18318     case 'M':
18319       /* ME value for a mask operand.  */
18320       if (! mask_operand (x, SImode))
18321 	output_operand_lossage ("invalid %%M value");
18322 
18323       fprintf (file, "%d", extract_ME (x));
18324       return;
18325 
18326       /* %n outputs the negative of its operand.  */
18327 
18328     case 'N':
18329       /* Write the number of elements in the vector times 4.  */
18330       if (GET_CODE (x) != PARALLEL)
18331 	output_operand_lossage ("invalid %%N value");
18332       else
18333 	fprintf (file, "%d", XVECLEN (x, 0) * 4);
18334       return;
18335 
18336     case 'O':
18337       /* Similar, but subtract 1 first.  */
18338       if (GET_CODE (x) != PARALLEL)
18339 	output_operand_lossage ("invalid %%O value");
18340       else
18341 	fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
18342       return;
18343 
18344     case 'p':
18345       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
18346       if (! INT_P (x)
18347 	  || INT_LOWPART (x) < 0
18348 	  || (i = exact_log2 (INT_LOWPART (x))) < 0)
18349 	output_operand_lossage ("invalid %%p value");
18350       else
18351 	fprintf (file, "%d", i);
18352       return;
18353 
18354     case 'P':
18355       /* The operand must be an indirect memory reference.  The result
18356 	 is the register name.  */
18357       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
18358 	  || REGNO (XEXP (x, 0)) >= 32)
18359 	output_operand_lossage ("invalid %%P value");
18360       else
18361 	fputs (reg_names[REGNO (XEXP (x, 0))], file);
18362       return;
18363 
18364     case 'q':
18365       /* This outputs the logical code corresponding to a boolean
18366 	 expression.  The expression may have one or both operands
18367 	 negated (if one, only the first one).  For condition register
18368 	 logical operations, it will also treat the negated
18369 	 CR codes as NOTs, but not handle NOTs of them.  */
18370       {
18371 	const char *const *t = 0;
18372 	const char *s;
18373 	enum rtx_code code = GET_CODE (x);
18374 	static const char * const tbl[3][3] = {
18375 	  { "and", "andc", "nor" },
18376 	  { "or", "orc", "nand" },
18377 	  { "xor", "eqv", "xor" } };
18378 
18379 	if (code == AND)
18380 	  t = tbl[0];
18381 	else if (code == IOR)
18382 	  t = tbl[1];
18383 	else if (code == XOR)
18384 	  t = tbl[2];
18385 	else
18386 	  output_operand_lossage ("invalid %%q value");
18387 
18388 	if (GET_CODE (XEXP (x, 0)) != NOT)
18389 	  s = t[0];
18390 	else
18391 	  {
18392 	    if (GET_CODE (XEXP (x, 1)) == NOT)
18393 	      s = t[2];
18394 	    else
18395 	      s = t[1];
18396 	  }
18397 
18398 	fputs (s, file);
18399       }
18400       return;
18401 
18402     case 'Q':
18403       if (! TARGET_MFCRF)
18404 	return;
18405       fputc (',', file);
18406       /* FALLTHRU */
18407 
18408     case 'R':
18409       /* X is a CR register.  Print the mask for `mtcrf'.  */
18410       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18411 	output_operand_lossage ("invalid %%R value");
18412       else
18413 	fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
18414       return;
18415 
18416     case 's':
18417       /* Low 5 bits of 32 - value */
18418       if (! INT_P (x))
18419 	output_operand_lossage ("invalid %%s value");
18420       else
18421 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
18422       return;
18423 
18424     case 'S':
18425       /* PowerPC64 mask position.  All 0's is excluded.
18426 	 CONST_INT 32-bit mask is considered sign-extended so any
18427 	 transition must occur within the CONST_INT, not on the boundary.  */
18428       if (! mask64_operand (x, DImode))
18429 	output_operand_lossage ("invalid %%S value");
18430 
18431       uval = INT_LOWPART (x);
18432 
18433       if (uval & 1)	/* Clear Left */
18434 	{
18435 #if HOST_BITS_PER_WIDE_INT > 64
18436 	  uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
18437 #endif
18438 	  i = 64;
18439 	}
18440       else		/* Clear Right */
18441 	{
18442 	  uval = ~uval;
18443 #if HOST_BITS_PER_WIDE_INT > 64
18444 	  uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
18445 #endif
18446 	  i = 63;
18447 	}
18448       while (uval != 0)
18449 	--i, uval >>= 1;
18450       gcc_assert (i >= 0);
18451       fprintf (file, "%d", i);
18452       return;
18453 
18454     case 't':
18455       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
18456       gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
18457 
18458       /* Bit 3 is OV bit.  */
18459       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
18460 
18461       /* If we want bit 31, write a shift count of zero, not 32.  */
18462       fprintf (file, "%d", i == 31 ? 0 : i + 1);
18463       return;
18464 
18465     case 'T':
18466       /* Print the symbolic name of a branch target register.  */
18467       if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
18468 				  && REGNO (x) != CTR_REGNO))
18469 	output_operand_lossage ("invalid %%T value");
18470       else if (REGNO (x) == LR_REGNO)
18471 	fputs ("lr", file);
18472       else
18473 	fputs ("ctr", file);
18474       return;
18475 
18476     case 'u':
18477       /* High-order 16 bits of constant for use in unsigned operand.  */
18478       if (! INT_P (x))
18479 	output_operand_lossage ("invalid %%u value");
18480       else
18481 	fprintf (file, HOST_WIDE_INT_PRINT_HEX,
18482 		 (INT_LOWPART (x) >> 16) & 0xffff);
18483       return;
18484 
18485     case 'v':
18486       /* High-order 16 bits of constant for use in signed operand.  */
18487       if (! INT_P (x))
18488 	output_operand_lossage ("invalid %%v value");
18489       else
18490 	fprintf (file, HOST_WIDE_INT_PRINT_HEX,
18491 		 (INT_LOWPART (x) >> 16) & 0xffff);
18492       return;
18493 
18494     case 'U':
18495       /* Print `u' if this has an auto-increment or auto-decrement.  */
18496       if (MEM_P (x)
18497 	  && (GET_CODE (XEXP (x, 0)) == PRE_INC
18498 	      || GET_CODE (XEXP (x, 0)) == PRE_DEC
18499 	      || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
18500 	putc ('u', file);
18501       return;
18502 
18503     case 'V':
18504       /* Print the trap code for this operand.  */
18505       switch (GET_CODE (x))
18506 	{
18507 	case EQ:
18508 	  fputs ("eq", file);   /* 4 */
18509 	  break;
18510 	case NE:
18511 	  fputs ("ne", file);   /* 24 */
18512 	  break;
18513 	case LT:
18514 	  fputs ("lt", file);   /* 16 */
18515 	  break;
18516 	case LE:
18517 	  fputs ("le", file);   /* 20 */
18518 	  break;
18519 	case GT:
18520 	  fputs ("gt", file);   /* 8 */
18521 	  break;
18522 	case GE:
18523 	  fputs ("ge", file);   /* 12 */
18524 	  break;
18525 	case LTU:
18526 	  fputs ("llt", file);  /* 2 */
18527 	  break;
18528 	case LEU:
18529 	  fputs ("lle", file);  /* 6 */
18530 	  break;
18531 	case GTU:
18532 	  fputs ("lgt", file);  /* 1 */
18533 	  break;
18534 	case GEU:
18535 	  fputs ("lge", file);  /* 5 */
18536 	  break;
18537 	default:
18538 	  gcc_unreachable ();
18539 	}
18540       break;
18541 
18542     case 'w':
18543       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
18544 	 normally.  */
18545       if (INT_P (x))
18546 	fprintf (file, HOST_WIDE_INT_PRINT_DEC,
18547 		 ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
18548       else
18549 	print_operand (file, x, 0);
18550       return;
18551 
18552     case 'W':
18553       /* MB value for a PowerPC64 rldic operand.  */
18554       i = clz_hwi (GET_CODE (x) == CONST_INT
18555 		   ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
18556 
18557 #if HOST_BITS_PER_WIDE_INT == 32
18558       if (GET_CODE (x) == CONST_INT && i > 0)
18559 	i += 32;  /* zero-extend high-part was all 0's */
18560       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
18561 	i = clz_hwi (CONST_DOUBLE_LOW (x)) + 32;
18562 #endif
18563 
18564       fprintf (file, "%d", i);
18565       return;
18566 
18567     case 'x':
18568       /* X is a FPR or Altivec register used in a VSX context.  */
18569       if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
18570 	output_operand_lossage ("invalid %%x value");
18571       else
18572 	{
18573 	  int reg = REGNO (x);
18574 	  int vsx_reg = (FP_REGNO_P (reg)
18575 			 ? reg - 32
18576 			 : reg - FIRST_ALTIVEC_REGNO + 32);
18577 
18578 #ifdef TARGET_REGNAMES
18579 	  if (TARGET_REGNAMES)
18580 	    fprintf (file, "%%vs%d", vsx_reg);
18581 	  else
18582 #endif
18583 	    fprintf (file, "%d", vsx_reg);
18584 	}
18585       return;
18586 
18587     case 'X':
18588       if (MEM_P (x)
18589 	  && (legitimate_indexed_address_p (XEXP (x, 0), 0)
18590 	      || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
18591 		  && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
18592 	putc ('x', file);
18593       return;
18594 
18595     case 'Y':
18596       /* Like 'L', for third word of TImode/PTImode  */
18597       if (REG_P (x))
18598 	fputs (reg_names[REGNO (x) + 2], file);
18599       else if (MEM_P (x))
18600 	{
18601 	  if (GET_CODE (XEXP (x, 0)) == PRE_INC
18602 	      || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18603 	    output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18604 	  else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18605 	    output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18606 	  else
18607 	    output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
18608 	  if (small_data_operand (x, GET_MODE (x)))
18609 	    fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18610 		     reg_names[SMALL_DATA_REG]);
18611 	}
18612       return;
18613 
18614     case 'z':
18615       /* X is a SYMBOL_REF.  Write out the name preceded by a
18616 	 period and without any trailing data in brackets.  Used for function
18617 	 names.  If we are configured for System V (or the embedded ABI) on
18618 	 the PowerPC, do not emit the period, since those systems do not use
18619 	 TOCs and the like.  */
18620       gcc_assert (GET_CODE (x) == SYMBOL_REF);
18621 
18622       /* Mark the decl as referenced so that cgraph will output the
18623 	 function.  */
18624       if (SYMBOL_REF_DECL (x))
18625 	mark_decl_referenced (SYMBOL_REF_DECL (x));
18626 
18627       /* For macho, check to see if we need a stub.  */
18628       if (TARGET_MACHO)
18629 	{
18630 	  const char *name = XSTR (x, 0);
18631 #if TARGET_MACHO
18632 	  if (darwin_emit_branch_islands
18633 	      && MACHOPIC_INDIRECT
18634 	      && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
18635 	    name = machopic_indirection_name (x, /*stub_p=*/true);
18636 #endif
18637 	  assemble_name (file, name);
18638 	}
18639       else if (!DOT_SYMBOLS)
18640 	assemble_name (file, XSTR (x, 0));
18641       else
18642 	rs6000_output_function_entry (file, XSTR (x, 0));
18643       return;
18644 
18645     case 'Z':
18646       /* Like 'L', for last word of TImode/PTImode.  */
18647       if (REG_P (x))
18648 	fputs (reg_names[REGNO (x) + 3], file);
18649       else if (MEM_P (x))
18650 	{
18651 	  if (GET_CODE (XEXP (x, 0)) == PRE_INC
18652 	      || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18653 	    output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18654 	  else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18655 	    output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18656 	  else
18657 	    output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
18658 	  if (small_data_operand (x, GET_MODE (x)))
18659 	    fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18660 		     reg_names[SMALL_DATA_REG]);
18661 	}
18662       return;
18663 
18664       /* Print AltiVec or SPE memory operand.  */
18665     case 'y':
18666       {
18667 	rtx tmp;
18668 
18669 	gcc_assert (MEM_P (x));
18670 
18671 	tmp = XEXP (x, 0);
18672 
18673 	/* Ugly hack because %y is overloaded.  */
18674 	if ((TARGET_SPE || TARGET_E500_DOUBLE)
18675 	    && (GET_MODE_SIZE (GET_MODE (x)) == 8
18676 		|| GET_MODE (x) == TFmode
18677 		|| GET_MODE (x) == TImode
18678 		|| GET_MODE (x) == PTImode))
18679 	  {
18680 	    /* Handle [reg].  */
18681 	    if (REG_P (tmp))
18682 	      {
18683 		fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
18684 		break;
18685 	      }
18686 	    /* Handle [reg+UIMM].  */
18687 	    else if (GET_CODE (tmp) == PLUS &&
18688 		     GET_CODE (XEXP (tmp, 1)) == CONST_INT)
18689 	      {
18690 		int x;
18691 
18692 		gcc_assert (REG_P (XEXP (tmp, 0)));
18693 
18694 		x = INTVAL (XEXP (tmp, 1));
18695 		fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
18696 		break;
18697 	      }
18698 
18699 	    /* Fall through.  Must be [reg+reg].  */
18700 	  }
18701 	if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
18702 	    && GET_CODE (tmp) == AND
18703 	    && GET_CODE (XEXP (tmp, 1)) == CONST_INT
18704 	    && INTVAL (XEXP (tmp, 1)) == -16)
18705 	  tmp = XEXP (tmp, 0);
18706 	else if (VECTOR_MEM_VSX_P (GET_MODE (x))
18707 		 && GET_CODE (tmp) == PRE_MODIFY)
18708 	  tmp = XEXP (tmp, 1);
18709 	if (REG_P (tmp))
18710 	  fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
18711 	else
18712 	  {
18713 	    if (!GET_CODE (tmp) == PLUS
18714 		|| !REG_P (XEXP (tmp, 0))
18715 		|| !REG_P (XEXP (tmp, 1)))
18716 	      {
18717 		output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
18718 		break;
18719 	      }
18720 
18721 	    if (REGNO (XEXP (tmp, 0)) == 0)
18722 	      fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
18723 		       reg_names[ REGNO (XEXP (tmp, 0)) ]);
18724 	    else
18725 	      fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
18726 		       reg_names[ REGNO (XEXP (tmp, 1)) ]);
18727 	  }
18728 	break;
18729       }
18730 
18731     case 0:
18732       if (REG_P (x))
18733 	fprintf (file, "%s", reg_names[REGNO (x)]);
18734       else if (MEM_P (x))
18735 	{
18736 	  /* We need to handle PRE_INC and PRE_DEC here, since we need to
18737 	     know the width from the mode.  */
18738 	  if (GET_CODE (XEXP (x, 0)) == PRE_INC)
18739 	    fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
18740 		     reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18741 	  else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
18742 	    fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
18743 		     reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18744 	  else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18745 	    output_address (XEXP (XEXP (x, 0), 1));
18746 	  else
18747 	    output_address (XEXP (x, 0));
18748 	}
18749       else
18750 	{
18751 	  if (toc_relative_expr_p (x, false))
18752 	    /* This hack along with a corresponding hack in
18753 	       rs6000_output_addr_const_extra arranges to output addends
18754 	       where the assembler expects to find them.  eg.
18755 	       (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
18756 	       without this hack would be output as "x@toc+4".  We
18757 	       want "x+4@toc".  */
18758 	    output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18759 	  else
18760 	    output_addr_const (file, x);
18761 	}
18762       return;
18763 
18764     case '&':
18765       assemble_name (file, rs6000_get_some_local_dynamic_name ());
18766       return;
18767 
18768     default:
18769       output_operand_lossage ("invalid %%xn code");
18770     }
18771 }
18772 
18773 /* Print the address of an operand.  */
18774 
18775 void
print_operand_address(FILE * file,rtx x)18776 print_operand_address (FILE *file, rtx x)
18777 {
18778   if (REG_P (x))
18779     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
18780   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
18781 	   || GET_CODE (x) == LABEL_REF)
18782     {
18783       output_addr_const (file, x);
18784       if (small_data_operand (x, GET_MODE (x)))
18785 	fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18786 		 reg_names[SMALL_DATA_REG]);
18787       else
18788 	gcc_assert (!TARGET_TOC);
18789     }
18790   else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18791 	   && REG_P (XEXP (x, 1)))
18792     {
18793       if (REGNO (XEXP (x, 0)) == 0)
18794 	fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
18795 		 reg_names[ REGNO (XEXP (x, 0)) ]);
18796       else
18797 	fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
18798 		 reg_names[ REGNO (XEXP (x, 1)) ]);
18799     }
18800   else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18801 	   && GET_CODE (XEXP (x, 1)) == CONST_INT)
18802     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
18803 	     INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
18804 #if TARGET_MACHO
18805   else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18806 	   && CONSTANT_P (XEXP (x, 1)))
18807     {
18808       fprintf (file, "lo16(");
18809       output_addr_const (file, XEXP (x, 1));
18810       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18811     }
18812 #endif
18813 #if TARGET_ELF
18814   else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18815 	   && CONSTANT_P (XEXP (x, 1)))
18816     {
18817       output_addr_const (file, XEXP (x, 1));
18818       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18819     }
18820 #endif
18821   else if (toc_relative_expr_p (x, false))
18822     {
18823       /* This hack along with a corresponding hack in
18824 	 rs6000_output_addr_const_extra arranges to output addends
18825 	 where the assembler expects to find them.  eg.
18826 	 (lo_sum (reg 9)
18827 	 .       (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
18828 	 without this hack would be output as "x@toc+8@l(9)".  We
18829 	 want "x+8@toc@l(9)".  */
18830       output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18831       if (GET_CODE (x) == LO_SUM)
18832 	fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
18833       else
18834 	fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
18835     }
18836   else
18837     gcc_unreachable ();
18838 }
18839 
18840 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA.  */
18841 
18842 static bool
rs6000_output_addr_const_extra(FILE * file,rtx x)18843 rs6000_output_addr_const_extra (FILE *file, rtx x)
18844 {
18845   if (GET_CODE (x) == UNSPEC)
18846     switch (XINT (x, 1))
18847       {
18848       case UNSPEC_TOCREL:
18849 	gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
18850 			     && REG_P (XVECEXP (x, 0, 1))
18851 			     && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
18852 	output_addr_const (file, XVECEXP (x, 0, 0));
18853 	if (x == tocrel_base && tocrel_offset != const0_rtx)
18854 	  {
18855 	    if (INTVAL (tocrel_offset) >= 0)
18856 	      fprintf (file, "+");
18857 	    output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
18858 	  }
18859 	if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
18860 	  {
18861 	    putc ('-', file);
18862 	    assemble_name (file, toc_label_name);
18863 	  }
18864 	else if (TARGET_ELF)
18865 	  fputs ("@toc", file);
18866 	return true;
18867 
18868 #if TARGET_MACHO
18869       case UNSPEC_MACHOPIC_OFFSET:
18870 	output_addr_const (file, XVECEXP (x, 0, 0));
18871 	putc ('-', file);
18872 	machopic_output_function_base_name (file);
18873 	return true;
18874 #endif
18875       }
18876   return false;
18877 }
18878 
18879 /* Target hook for assembling integer objects.  The PowerPC version has
18880    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
18881    is defined.  It also needs to handle DI-mode objects on 64-bit
18882    targets.  */
18883 
18884 static bool
rs6000_assemble_integer(rtx x,unsigned int size,int aligned_p)18885 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
18886 {
18887 #ifdef RELOCATABLE_NEEDS_FIXUP
18888   /* Special handling for SI values.  */
18889   if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
18890     {
18891       static int recurse = 0;
18892 
18893       /* For -mrelocatable, we mark all addresses that need to be fixed up in
18894 	 the .fixup section.  Since the TOC section is already relocated, we
18895 	 don't need to mark it here.  We used to skip the text section, but it
18896 	 should never be valid for relocated addresses to be placed in the text
18897 	 section.  */
18898       if (TARGET_RELOCATABLE
18899 	  && in_section != toc_section
18900 	  && !recurse
18901 	  && GET_CODE (x) != CONST_INT
18902 	  && GET_CODE (x) != CONST_DOUBLE
18903 	  && CONSTANT_P (x))
18904 	{
18905 	  char buf[256];
18906 
18907 	  recurse = 1;
18908 	  ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
18909 	  fixuplabelno++;
18910 	  ASM_OUTPUT_LABEL (asm_out_file, buf);
18911 	  fprintf (asm_out_file, "\t.long\t(");
18912 	  output_addr_const (asm_out_file, x);
18913 	  fprintf (asm_out_file, ")@fixup\n");
18914 	  fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
18915 	  ASM_OUTPUT_ALIGN (asm_out_file, 2);
18916 	  fprintf (asm_out_file, "\t.long\t");
18917 	  assemble_name (asm_out_file, buf);
18918 	  fprintf (asm_out_file, "\n\t.previous\n");
18919 	  recurse = 0;
18920 	  return true;
18921 	}
18922       /* Remove initial .'s to turn a -mcall-aixdesc function
18923 	 address into the address of the descriptor, not the function
18924 	 itself.  */
18925       else if (GET_CODE (x) == SYMBOL_REF
18926 	       && XSTR (x, 0)[0] == '.'
18927 	       && DEFAULT_ABI == ABI_AIX)
18928 	{
18929 	  const char *name = XSTR (x, 0);
18930 	  while (*name == '.')
18931 	    name++;
18932 
18933 	  fprintf (asm_out_file, "\t.long\t%s\n", name);
18934 	  return true;
18935 	}
18936     }
18937 #endif /* RELOCATABLE_NEEDS_FIXUP */
18938   return default_assemble_integer (x, size, aligned_p);
18939 }
18940 
18941 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
18942 /* Emit an assembler directive to set symbol visibility for DECL to
18943    VISIBILITY_TYPE.  */
18944 
18945 static void
rs6000_assemble_visibility(tree decl,int vis)18946 rs6000_assemble_visibility (tree decl, int vis)
18947 {
18948   if (TARGET_XCOFF)
18949     return;
18950 
18951   /* Functions need to have their entry point symbol visibility set as
18952      well as their descriptor symbol visibility.  */
18953   if (DEFAULT_ABI == ABI_AIX
18954       && DOT_SYMBOLS
18955       && TREE_CODE (decl) == FUNCTION_DECL)
18956     {
18957       static const char * const visibility_types[] = {
18958 	NULL, "internal", "hidden", "protected"
18959       };
18960 
18961       const char *name, *type;
18962 
18963       name = ((* targetm.strip_name_encoding)
18964 	      (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
18965       type = visibility_types[vis];
18966 
18967       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
18968       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
18969     }
18970   else
18971     default_assemble_visibility (decl, vis);
18972 }
18973 #endif
18974 
18975 enum rtx_code
rs6000_reverse_condition(enum machine_mode mode,enum rtx_code code)18976 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
18977 {
18978   /* Reversal of FP compares takes care -- an ordered compare
18979      becomes an unordered compare and vice versa.  */
18980   if (mode == CCFPmode
18981       && (!flag_finite_math_only
18982 	  || code == UNLT || code == UNLE || code == UNGT || code == UNGE
18983 	  || code == UNEQ || code == LTGT))
18984     return reverse_condition_maybe_unordered (code);
18985   else
18986     return reverse_condition (code);
18987 }
18988 
18989 /* Generate a compare for CODE.  Return a brand-new rtx that
18990    represents the result of the compare.  */
18991 
18992 static rtx
rs6000_generate_compare(rtx cmp,enum machine_mode mode)18993 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
18994 {
18995   enum machine_mode comp_mode;
18996   rtx compare_result;
18997   enum rtx_code code = GET_CODE (cmp);
18998   rtx op0 = XEXP (cmp, 0);
18999   rtx op1 = XEXP (cmp, 1);
19000 
19001   if (FLOAT_MODE_P (mode))
19002     comp_mode = CCFPmode;
19003   else if (code == GTU || code == LTU
19004 	   || code == GEU || code == LEU)
19005     comp_mode = CCUNSmode;
19006   else if ((code == EQ || code == NE)
19007 	   && unsigned_reg_p (op0)
19008 	   && (unsigned_reg_p (op1)
19009 	       || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
19010     /* These are unsigned values, perhaps there will be a later
19011        ordering compare that can be shared with this one.  */
19012     comp_mode = CCUNSmode;
19013   else
19014     comp_mode = CCmode;
19015 
19016   /* If we have an unsigned compare, make sure we don't have a signed value as
19017      an immediate.  */
19018   if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
19019       && INTVAL (op1) < 0)
19020     {
19021       op0 = copy_rtx_if_shared (op0);
19022       op1 = force_reg (GET_MODE (op0), op1);
19023       cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
19024     }
19025 
19026   /* First, the compare.  */
19027   compare_result = gen_reg_rtx (comp_mode);
19028 
19029   /* E500 FP compare instructions on the GPRs.  Yuck!  */
19030   if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
19031       && FLOAT_MODE_P (mode))
19032     {
19033       rtx cmp, or_result, compare_result2;
19034       enum machine_mode op_mode = GET_MODE (op0);
19035 
19036       if (op_mode == VOIDmode)
19037 	op_mode = GET_MODE (op1);
19038 
19039       /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
19040 	 This explains the following mess.  */
19041 
19042       switch (code)
19043 	{
19044 	case EQ: case UNEQ: case NE: case LTGT:
19045 	  switch (op_mode)
19046 	    {
19047 	    case SFmode:
19048 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19049 		? gen_tstsfeq_gpr (compare_result, op0, op1)
19050 		: gen_cmpsfeq_gpr (compare_result, op0, op1);
19051 	      break;
19052 
19053 	    case DFmode:
19054 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19055 		? gen_tstdfeq_gpr (compare_result, op0, op1)
19056 		: gen_cmpdfeq_gpr (compare_result, op0, op1);
19057 	      break;
19058 
19059 	    case TFmode:
19060 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19061 		? gen_tsttfeq_gpr (compare_result, op0, op1)
19062 		: gen_cmptfeq_gpr (compare_result, op0, op1);
19063 	      break;
19064 
19065 	    default:
19066 	      gcc_unreachable ();
19067 	    }
19068 	  break;
19069 
19070 	case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
19071 	  switch (op_mode)
19072 	    {
19073 	    case SFmode:
19074 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19075 		? gen_tstsfgt_gpr (compare_result, op0, op1)
19076 		: gen_cmpsfgt_gpr (compare_result, op0, op1);
19077 	      break;
19078 
19079 	    case DFmode:
19080 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19081 		? gen_tstdfgt_gpr (compare_result, op0, op1)
19082 		: gen_cmpdfgt_gpr (compare_result, op0, op1);
19083 	      break;
19084 
19085 	    case TFmode:
19086 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19087 		? gen_tsttfgt_gpr (compare_result, op0, op1)
19088 		: gen_cmptfgt_gpr (compare_result, op0, op1);
19089 	      break;
19090 
19091 	    default:
19092 	      gcc_unreachable ();
19093 	    }
19094 	  break;
19095 
19096 	case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
19097 	  switch (op_mode)
19098 	    {
19099 	    case SFmode:
19100 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19101 		? gen_tstsflt_gpr (compare_result, op0, op1)
19102 		: gen_cmpsflt_gpr (compare_result, op0, op1);
19103 	      break;
19104 
19105 	    case DFmode:
19106 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19107 		? gen_tstdflt_gpr (compare_result, op0, op1)
19108 		: gen_cmpdflt_gpr (compare_result, op0, op1);
19109 	      break;
19110 
19111 	    case TFmode:
19112 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19113 		? gen_tsttflt_gpr (compare_result, op0, op1)
19114 		: gen_cmptflt_gpr (compare_result, op0, op1);
19115 	      break;
19116 
19117 	    default:
19118 	      gcc_unreachable ();
19119 	    }
19120 	  break;
19121         default:
19122           gcc_unreachable ();
19123 	}
19124 
19125       /* Synthesize LE and GE from LT/GT || EQ.  */
19126       if (code == LE || code == GE || code == LEU || code == GEU)
19127 	{
19128 	  emit_insn (cmp);
19129 
19130 	  switch (code)
19131 	    {
19132 	    case LE: code = LT; break;
19133 	    case GE: code = GT; break;
19134 	    case LEU: code = LT; break;
19135 	    case GEU: code = GT; break;
19136 	    default: gcc_unreachable ();
19137 	    }
19138 
19139 	  compare_result2 = gen_reg_rtx (CCFPmode);
19140 
19141 	  /* Do the EQ.  */
19142 	  switch (op_mode)
19143 	    {
19144 	    case SFmode:
19145 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19146 		? gen_tstsfeq_gpr (compare_result2, op0, op1)
19147 		: gen_cmpsfeq_gpr (compare_result2, op0, op1);
19148 	      break;
19149 
19150 	    case DFmode:
19151 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19152 		? gen_tstdfeq_gpr (compare_result2, op0, op1)
19153 		: gen_cmpdfeq_gpr (compare_result2, op0, op1);
19154 	      break;
19155 
19156 	    case TFmode:
19157 	      cmp = (flag_finite_math_only && !flag_trapping_math)
19158 		? gen_tsttfeq_gpr (compare_result2, op0, op1)
19159 		: gen_cmptfeq_gpr (compare_result2, op0, op1);
19160 	      break;
19161 
19162 	    default:
19163 	      gcc_unreachable ();
19164 	    }
19165 	  emit_insn (cmp);
19166 
19167 	  /* OR them together.  */
19168 	  or_result = gen_reg_rtx (CCFPmode);
19169 	  cmp = gen_e500_cr_ior_compare (or_result, compare_result,
19170 					   compare_result2);
19171 	  compare_result = or_result;
19172 	  code = EQ;
19173 	}
19174       else
19175 	{
19176 	  if (code == NE || code == LTGT)
19177 	    code = NE;
19178 	  else
19179 	    code = EQ;
19180 	}
19181 
19182       emit_insn (cmp);
19183     }
19184   else
19185     {
19186       /* Generate XLC-compatible TFmode compare as PARALLEL with extra
19187 	 CLOBBERs to match cmptf_internal2 pattern.  */
19188       if (comp_mode == CCFPmode && TARGET_XL_COMPAT
19189 	  && GET_MODE (op0) == TFmode
19190 	  && !TARGET_IEEEQUAD
19191 	  && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
19192 	emit_insn (gen_rtx_PARALLEL (VOIDmode,
19193 	  gen_rtvec (10,
19194 		     gen_rtx_SET (VOIDmode,
19195 				  compare_result,
19196 				  gen_rtx_COMPARE (comp_mode, op0, op1)),
19197 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19198 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19199 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19200 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19201 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19202 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19203 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19204 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19205 		     gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
19206       else if (GET_CODE (op1) == UNSPEC
19207 	       && XINT (op1, 1) == UNSPEC_SP_TEST)
19208 	{
19209 	  rtx op1b = XVECEXP (op1, 0, 0);
19210 	  comp_mode = CCEQmode;
19211 	  compare_result = gen_reg_rtx (CCEQmode);
19212 	  if (TARGET_64BIT)
19213 	    emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
19214 	  else
19215 	    emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
19216 	}
19217       else
19218 	emit_insn (gen_rtx_SET (VOIDmode, compare_result,
19219 				gen_rtx_COMPARE (comp_mode, op0, op1)));
19220     }
19221 
19222   /* Some kinds of FP comparisons need an OR operation;
19223      under flag_finite_math_only we don't bother.  */
19224   if (FLOAT_MODE_P (mode)
19225       && !flag_finite_math_only
19226       && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
19227       && (code == LE || code == GE
19228 	  || code == UNEQ || code == LTGT
19229 	  || code == UNGT || code == UNLT))
19230     {
19231       enum rtx_code or1, or2;
19232       rtx or1_rtx, or2_rtx, compare2_rtx;
19233       rtx or_result = gen_reg_rtx (CCEQmode);
19234 
19235       switch (code)
19236 	{
19237 	case LE: or1 = LT;  or2 = EQ;  break;
19238 	case GE: or1 = GT;  or2 = EQ;  break;
19239 	case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
19240 	case LTGT: or1 = LT;  or2 = GT;  break;
19241 	case UNGT: or1 = UNORDERED;  or2 = GT;  break;
19242 	case UNLT: or1 = UNORDERED;  or2 = LT;  break;
19243 	default:  gcc_unreachable ();
19244 	}
19245       validate_condition_mode (or1, comp_mode);
19246       validate_condition_mode (or2, comp_mode);
19247       or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
19248       or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
19249       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
19250 				      gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
19251 				      const_true_rtx);
19252       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
19253 
19254       compare_result = or_result;
19255       code = EQ;
19256     }
19257 
19258   validate_condition_mode (code, GET_MODE (compare_result));
19259 
19260   return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
19261 }
19262 
19263 
19264 /* Emit the RTL for an sISEL pattern.  */
19265 
19266 void
rs6000_emit_sISEL(enum machine_mode mode ATTRIBUTE_UNUSED,rtx operands[])19267 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
19268 {
19269   rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
19270 }
19271 
19272 void
rs6000_emit_sCOND(enum machine_mode mode,rtx operands[])19273 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
19274 {
19275   rtx condition_rtx;
19276   enum machine_mode op_mode;
19277   enum rtx_code cond_code;
19278   rtx result = operands[0];
19279 
19280   if (TARGET_ISEL && (mode == SImode || mode == DImode))
19281     {
19282       rs6000_emit_sISEL (mode, operands);
19283       return;
19284     }
19285 
19286   condition_rtx = rs6000_generate_compare (operands[1], mode);
19287   cond_code = GET_CODE (condition_rtx);
19288 
19289   if (FLOAT_MODE_P (mode)
19290       && !TARGET_FPRS && TARGET_HARD_FLOAT)
19291     {
19292       rtx t;
19293 
19294       PUT_MODE (condition_rtx, SImode);
19295       t = XEXP (condition_rtx, 0);
19296 
19297       gcc_assert (cond_code == NE || cond_code == EQ);
19298 
19299       if (cond_code == NE)
19300 	emit_insn (gen_e500_flip_gt_bit (t, t));
19301 
19302       emit_insn (gen_move_from_CR_gt_bit (result, t));
19303       return;
19304     }
19305 
19306   if (cond_code == NE
19307       || cond_code == GE || cond_code == LE
19308       || cond_code == GEU || cond_code == LEU
19309       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
19310     {
19311       rtx not_result = gen_reg_rtx (CCEQmode);
19312       rtx not_op, rev_cond_rtx;
19313       enum machine_mode cc_mode;
19314 
19315       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
19316 
19317       rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
19318 				     SImode, XEXP (condition_rtx, 0), const0_rtx);
19319       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
19320       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
19321       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
19322     }
19323 
19324   op_mode = GET_MODE (XEXP (operands[1], 0));
19325   if (op_mode == VOIDmode)
19326     op_mode = GET_MODE (XEXP (operands[1], 1));
19327 
19328   if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
19329     {
19330       PUT_MODE (condition_rtx, DImode);
19331       convert_move (result, condition_rtx, 0);
19332     }
19333   else
19334     {
19335       PUT_MODE (condition_rtx, SImode);
19336       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
19337     }
19338 }
19339 
19340 /* Emit a branch of kind CODE to location LOC.  */
19341 
19342 void
rs6000_emit_cbranch(enum machine_mode mode,rtx operands[])19343 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
19344 {
19345   rtx condition_rtx, loc_ref;
19346 
19347   condition_rtx = rs6000_generate_compare (operands[0], mode);
19348   loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
19349   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
19350 			       gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
19351 						     loc_ref, pc_rtx)));
19352 }
19353 
19354 /* Return the string to output a conditional branch to LABEL, which is
19355    the operand template of the label, or NULL if the branch is really a
19356    conditional return.
19357 
19358    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
19359    condition code register and its mode specifies what kind of
19360    comparison we made.
19361 
19362    REVERSED is nonzero if we should reverse the sense of the comparison.
19363 
19364    INSN is the insn.  */
19365 
19366 char *
output_cbranch(rtx op,const char * label,int reversed,rtx insn)19367 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
19368 {
19369   static char string[64];
19370   enum rtx_code code = GET_CODE (op);
19371   rtx cc_reg = XEXP (op, 0);
19372   enum machine_mode mode = GET_MODE (cc_reg);
19373   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
19374   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
19375   int really_reversed = reversed ^ need_longbranch;
19376   char *s = string;
19377   const char *ccode;
19378   const char *pred;
19379   rtx note;
19380 
19381   validate_condition_mode (code, mode);
19382 
19383   /* Work out which way this really branches.  We could use
19384      reverse_condition_maybe_unordered here always but this
19385      makes the resulting assembler clearer.  */
19386   if (really_reversed)
19387     {
19388       /* Reversal of FP compares takes care -- an ordered compare
19389 	 becomes an unordered compare and vice versa.  */
19390       if (mode == CCFPmode)
19391 	code = reverse_condition_maybe_unordered (code);
19392       else
19393 	code = reverse_condition (code);
19394     }
19395 
19396   if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
19397     {
19398       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
19399 	 to the GT bit.  */
19400       switch (code)
19401 	{
19402 	case EQ:
19403 	  /* Opposite of GT.  */
19404 	  code = GT;
19405 	  break;
19406 
19407 	case NE:
19408 	  code = UNLE;
19409 	  break;
19410 
19411 	default:
19412 	  gcc_unreachable ();
19413 	}
19414     }
19415 
19416   switch (code)
19417     {
19418       /* Not all of these are actually distinct opcodes, but
19419 	 we distinguish them for clarity of the resulting assembler.  */
19420     case NE: case LTGT:
19421       ccode = "ne"; break;
19422     case EQ: case UNEQ:
19423       ccode = "eq"; break;
19424     case GE: case GEU:
19425       ccode = "ge"; break;
19426     case GT: case GTU: case UNGT:
19427       ccode = "gt"; break;
19428     case LE: case LEU:
19429       ccode = "le"; break;
19430     case LT: case LTU: case UNLT:
19431       ccode = "lt"; break;
19432     case UNORDERED: ccode = "un"; break;
19433     case ORDERED: ccode = "nu"; break;
19434     case UNGE: ccode = "nl"; break;
19435     case UNLE: ccode = "ng"; break;
19436     default:
19437       gcc_unreachable ();
19438     }
19439 
19440   /* Maybe we have a guess as to how likely the branch is.  */
19441   pred = "";
19442   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
19443   if (note != NULL_RTX)
19444     {
19445       /* PROB is the difference from 50%.  */
19446       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
19447 
19448       /* Only hint for highly probable/improbable branches on newer
19449 	 cpus as static prediction overrides processor dynamic
19450 	 prediction.  For older cpus we may as well always hint, but
19451 	 assume not taken for branches that are very close to 50% as a
19452 	 mispredicted taken branch is more expensive than a
19453 	 mispredicted not-taken branch.  */
19454       if (rs6000_always_hint
19455 	  || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
19456 	      && br_prob_note_reliable_p (note)))
19457 	{
19458 	  if (abs (prob) > REG_BR_PROB_BASE / 20
19459 	      && ((prob > 0) ^ need_longbranch))
19460 	    pred = "+";
19461 	  else
19462 	    pred = "-";
19463 	}
19464     }
19465 
19466   if (label == NULL)
19467     s += sprintf (s, "b%slr%s ", ccode, pred);
19468   else
19469     s += sprintf (s, "b%s%s ", ccode, pred);
19470 
19471   /* We need to escape any '%' characters in the reg_names string.
19472      Assume they'd only be the first character....  */
19473   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
19474     *s++ = '%';
19475   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
19476 
19477   if (label != NULL)
19478     {
19479       /* If the branch distance was too far, we may have to use an
19480 	 unconditional branch to go the distance.  */
19481       if (need_longbranch)
19482 	s += sprintf (s, ",$+8\n\tb %s", label);
19483       else
19484 	s += sprintf (s, ",%s", label);
19485     }
19486 
19487   return string;
19488 }
19489 
19490 /* Return the string to flip the GT bit on a CR.  */
19491 char *
output_e500_flip_gt_bit(rtx dst,rtx src)19492 output_e500_flip_gt_bit (rtx dst, rtx src)
19493 {
19494   static char string[64];
19495   int a, b;
19496 
19497   gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
19498 	      && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
19499 
19500   /* GT bit.  */
19501   a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
19502   b = 4 * (REGNO (src) - CR0_REGNO) + 1;
19503 
19504   sprintf (string, "crnot %d,%d", a, b);
19505   return string;
19506 }
19507 
19508 /* Return insn for VSX or Altivec comparisons.  */
19509 
19510 static rtx
rs6000_emit_vector_compare_inner(enum rtx_code code,rtx op0,rtx op1)19511 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
19512 {
19513   rtx mask;
19514   enum machine_mode mode = GET_MODE (op0);
19515 
19516   switch (code)
19517     {
19518     default:
19519       break;
19520 
19521     case GE:
19522       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
19523 	return NULL_RTX;
19524 
19525     case EQ:
19526     case GT:
19527     case GTU:
19528     case ORDERED:
19529     case UNORDERED:
19530     case UNEQ:
19531     case LTGT:
19532       mask = gen_reg_rtx (mode);
19533       emit_insn (gen_rtx_SET (VOIDmode,
19534 			      mask,
19535 			      gen_rtx_fmt_ee (code, mode, op0, op1)));
19536       return mask;
19537     }
19538 
19539   return NULL_RTX;
19540 }
19541 
19542 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
19543    DMODE is expected destination mode. This is a recursive function.  */
19544 
19545 static rtx
rs6000_emit_vector_compare(enum rtx_code rcode,rtx op0,rtx op1,enum machine_mode dmode)19546 rs6000_emit_vector_compare (enum rtx_code rcode,
19547 			    rtx op0, rtx op1,
19548 			    enum machine_mode dmode)
19549 {
19550   rtx mask;
19551   bool swap_operands = false;
19552   bool try_again = false;
19553 
19554   gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
19555   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
19556 
19557   /* See if the comparison works as is.  */
19558   mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19559   if (mask)
19560     return mask;
19561 
19562   switch (rcode)
19563     {
19564     case LT:
19565       rcode = GT;
19566       swap_operands = true;
19567       try_again = true;
19568       break;
19569     case LTU:
19570       rcode = GTU;
19571       swap_operands = true;
19572       try_again = true;
19573       break;
19574     case NE:
19575     case UNLE:
19576     case UNLT:
19577     case UNGE:
19578     case UNGT:
19579       /* Invert condition and try again.
19580 	 e.g., A != B becomes ~(A==B).  */
19581       {
19582 	enum rtx_code rev_code;
19583 	enum insn_code nor_code;
19584 	rtx mask2;
19585 
19586 	rev_code = reverse_condition_maybe_unordered (rcode);
19587 	if (rev_code == UNKNOWN)
19588 	  return NULL_RTX;
19589 
19590 	nor_code = optab_handler (one_cmpl_optab, dmode);
19591 	if (nor_code == CODE_FOR_nothing)
19592 	  return NULL_RTX;
19593 
19594 	mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
19595 	if (!mask2)
19596 	  return NULL_RTX;
19597 
19598 	mask = gen_reg_rtx (dmode);
19599 	emit_insn (GEN_FCN (nor_code) (mask, mask2));
19600 	return mask;
19601       }
19602       break;
19603     case GE:
19604     case GEU:
19605     case LE:
19606     case LEU:
19607       /* Try GT/GTU/LT/LTU OR EQ */
19608       {
19609 	rtx c_rtx, eq_rtx;
19610 	enum insn_code ior_code;
19611 	enum rtx_code new_code;
19612 
19613 	switch (rcode)
19614 	  {
19615 	  case  GE:
19616 	    new_code = GT;
19617 	    break;
19618 
19619 	  case GEU:
19620 	    new_code = GTU;
19621 	    break;
19622 
19623 	  case LE:
19624 	    new_code = LT;
19625 	    break;
19626 
19627 	  case LEU:
19628 	    new_code = LTU;
19629 	    break;
19630 
19631 	  default:
19632 	    gcc_unreachable ();
19633 	  }
19634 
19635 	ior_code = optab_handler (ior_optab, dmode);
19636 	if (ior_code == CODE_FOR_nothing)
19637 	  return NULL_RTX;
19638 
19639 	c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
19640 	if (!c_rtx)
19641 	  return NULL_RTX;
19642 
19643 	eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
19644 	if (!eq_rtx)
19645 	  return NULL_RTX;
19646 
19647 	mask = gen_reg_rtx (dmode);
19648 	emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
19649 	return mask;
19650       }
19651       break;
19652     default:
19653       return NULL_RTX;
19654     }
19655 
19656   if (try_again)
19657     {
19658       if (swap_operands)
19659 	{
19660 	  rtx tmp;
19661 	  tmp = op0;
19662 	  op0 = op1;
19663 	  op1 = tmp;
19664 	}
19665 
19666       mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19667       if (mask)
19668 	return mask;
19669     }
19670 
19671   /* You only get two chances.  */
19672   return NULL_RTX;
19673 }
19674 
19675 /* Emit vector conditional expression.  DEST is destination. OP_TRUE and
19676    OP_FALSE are two VEC_COND_EXPR operands.  CC_OP0 and CC_OP1 are the two
19677    operands for the relation operation COND.  */
19678 
19679 int
rs6000_emit_vector_cond_expr(rtx dest,rtx op_true,rtx op_false,rtx cond,rtx cc_op0,rtx cc_op1)19680 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
19681 			      rtx cond, rtx cc_op0, rtx cc_op1)
19682 {
19683   enum machine_mode dest_mode = GET_MODE (dest);
19684   enum machine_mode mask_mode = GET_MODE (cc_op0);
19685   enum rtx_code rcode = GET_CODE (cond);
19686   enum machine_mode cc_mode = CCmode;
19687   rtx mask;
19688   rtx cond2;
19689   rtx tmp;
19690   bool invert_move = false;
19691 
19692   if (VECTOR_UNIT_NONE_P (dest_mode))
19693     return 0;
19694 
19695   gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
19696 	      && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
19697 
19698   switch (rcode)
19699     {
19700       /* Swap operands if we can, and fall back to doing the operation as
19701 	 specified, and doing a NOR to invert the test.  */
19702     case NE:
19703     case UNLE:
19704     case UNLT:
19705     case UNGE:
19706     case UNGT:
19707       /* Invert condition and try again.
19708 	 e.g., A  = (B != C) ? D : E becomes A = (B == C) ? E : D.  */
19709       invert_move = true;
19710       rcode = reverse_condition_maybe_unordered (rcode);
19711       if (rcode == UNKNOWN)
19712 	return 0;
19713       break;
19714 
19715       /* Mark unsigned tests with CCUNSmode.  */
19716     case GTU:
19717     case GEU:
19718     case LTU:
19719     case LEU:
19720       cc_mode = CCUNSmode;
19721       break;
19722 
19723     default:
19724       break;
19725     }
19726 
19727   /* Get the vector mask for the given relational operations.  */
19728   mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
19729 
19730   if (!mask)
19731     return 0;
19732 
19733   if (invert_move)
19734     {
19735       tmp = op_true;
19736       op_true = op_false;
19737       op_false = tmp;
19738     }
19739 
19740   cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
19741 			  CONST0_RTX (dest_mode));
19742   emit_insn (gen_rtx_SET (VOIDmode,
19743 			  dest,
19744 			  gen_rtx_IF_THEN_ELSE (dest_mode,
19745 						cond2,
19746 						op_true,
19747 						op_false)));
19748   return 1;
19749 }
19750 
19751 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
19752    operands of the last comparison is nonzero/true, FALSE_COND if it
19753    is zero/false.  Return 0 if the hardware has no such operation.  */
19754 
19755 int
rs6000_emit_cmove(rtx dest,rtx op,rtx true_cond,rtx false_cond)19756 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19757 {
19758   enum rtx_code code = GET_CODE (op);
19759   rtx op0 = XEXP (op, 0);
19760   rtx op1 = XEXP (op, 1);
19761   REAL_VALUE_TYPE c1;
19762   enum machine_mode compare_mode = GET_MODE (op0);
19763   enum machine_mode result_mode = GET_MODE (dest);
19764   rtx temp;
19765   bool is_against_zero;
19766 
19767   /* These modes should always match.  */
19768   if (GET_MODE (op1) != compare_mode
19769       /* In the isel case however, we can use a compare immediate, so
19770 	 op1 may be a small constant.  */
19771       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
19772     return 0;
19773   if (GET_MODE (true_cond) != result_mode)
19774     return 0;
19775   if (GET_MODE (false_cond) != result_mode)
19776     return 0;
19777 
19778   /* Don't allow using floating point comparisons for integer results for
19779      now.  */
19780   if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
19781     return 0;
19782 
19783   /* First, work out if the hardware can do this at all, or
19784      if it's too slow....  */
19785   if (!FLOAT_MODE_P (compare_mode))
19786     {
19787       if (TARGET_ISEL)
19788 	return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
19789       return 0;
19790     }
19791   else if (TARGET_HARD_FLOAT && !TARGET_FPRS
19792 	   && SCALAR_FLOAT_MODE_P (compare_mode))
19793     return 0;
19794 
19795   is_against_zero = op1 == CONST0_RTX (compare_mode);
19796 
19797   /* A floating-point subtract might overflow, underflow, or produce
19798      an inexact result, thus changing the floating-point flags, so it
19799      can't be generated if we care about that.  It's safe if one side
19800      of the construct is zero, since then no subtract will be
19801      generated.  */
19802   if (SCALAR_FLOAT_MODE_P (compare_mode)
19803       && flag_trapping_math && ! is_against_zero)
19804     return 0;
19805 
19806   /* Eliminate half of the comparisons by switching operands, this
19807      makes the remaining code simpler.  */
19808   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
19809       || code == LTGT || code == LT || code == UNLE)
19810     {
19811       code = reverse_condition_maybe_unordered (code);
19812       temp = true_cond;
19813       true_cond = false_cond;
19814       false_cond = temp;
19815     }
19816 
19817   /* UNEQ and LTGT take four instructions for a comparison with zero,
19818      it'll probably be faster to use a branch here too.  */
19819   if (code == UNEQ && HONOR_NANS (compare_mode))
19820     return 0;
19821 
19822   if (GET_CODE (op1) == CONST_DOUBLE)
19823     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
19824 
19825   /* We're going to try to implement comparisons by performing
19826      a subtract, then comparing against zero.  Unfortunately,
19827      Inf - Inf is NaN which is not zero, and so if we don't
19828      know that the operand is finite and the comparison
19829      would treat EQ different to UNORDERED, we can't do it.  */
19830   if (HONOR_INFINITIES (compare_mode)
19831       && code != GT && code != UNGE
19832       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
19833       /* Constructs of the form (a OP b ? a : b) are safe.  */
19834       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
19835 	  || (! rtx_equal_p (op0, true_cond)
19836 	      && ! rtx_equal_p (op1, true_cond))))
19837     return 0;
19838 
19839   /* At this point we know we can use fsel.  */
19840 
19841   /* Reduce the comparison to a comparison against zero.  */
19842   if (! is_against_zero)
19843     {
19844       temp = gen_reg_rtx (compare_mode);
19845       emit_insn (gen_rtx_SET (VOIDmode, temp,
19846 			      gen_rtx_MINUS (compare_mode, op0, op1)));
19847       op0 = temp;
19848       op1 = CONST0_RTX (compare_mode);
19849     }
19850 
19851   /* If we don't care about NaNs we can reduce some of the comparisons
19852      down to faster ones.  */
19853   if (! HONOR_NANS (compare_mode))
19854     switch (code)
19855       {
19856       case GT:
19857 	code = LE;
19858 	temp = true_cond;
19859 	true_cond = false_cond;
19860 	false_cond = temp;
19861 	break;
19862       case UNGE:
19863 	code = GE;
19864 	break;
19865       case UNEQ:
19866 	code = EQ;
19867 	break;
19868       default:
19869 	break;
19870       }
19871 
19872   /* Now, reduce everything down to a GE.  */
19873   switch (code)
19874     {
19875     case GE:
19876       break;
19877 
19878     case LE:
19879       temp = gen_reg_rtx (compare_mode);
19880       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19881       op0 = temp;
19882       break;
19883 
19884     case ORDERED:
19885       temp = gen_reg_rtx (compare_mode);
19886       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
19887       op0 = temp;
19888       break;
19889 
19890     case EQ:
19891       temp = gen_reg_rtx (compare_mode);
19892       emit_insn (gen_rtx_SET (VOIDmode, temp,
19893 			      gen_rtx_NEG (compare_mode,
19894 					   gen_rtx_ABS (compare_mode, op0))));
19895       op0 = temp;
19896       break;
19897 
19898     case UNGE:
19899       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
19900       temp = gen_reg_rtx (result_mode);
19901       emit_insn (gen_rtx_SET (VOIDmode, temp,
19902 			      gen_rtx_IF_THEN_ELSE (result_mode,
19903 						    gen_rtx_GE (VOIDmode,
19904 								op0, op1),
19905 						    true_cond, false_cond)));
19906       false_cond = true_cond;
19907       true_cond = temp;
19908 
19909       temp = gen_reg_rtx (compare_mode);
19910       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19911       op0 = temp;
19912       break;
19913 
19914     case GT:
19915       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
19916       temp = gen_reg_rtx (result_mode);
19917       emit_insn (gen_rtx_SET (VOIDmode, temp,
19918 			      gen_rtx_IF_THEN_ELSE (result_mode,
19919 						    gen_rtx_GE (VOIDmode,
19920 								op0, op1),
19921 						    true_cond, false_cond)));
19922       true_cond = false_cond;
19923       false_cond = temp;
19924 
19925       temp = gen_reg_rtx (compare_mode);
19926       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19927       op0 = temp;
19928       break;
19929 
19930     default:
19931       gcc_unreachable ();
19932     }
19933 
19934   emit_insn (gen_rtx_SET (VOIDmode, dest,
19935 			  gen_rtx_IF_THEN_ELSE (result_mode,
19936 						gen_rtx_GE (VOIDmode,
19937 							    op0, op1),
19938 						true_cond, false_cond)));
19939   return 1;
19940 }
19941 
19942 /* Same as above, but for ints (isel).  */
19943 
19944 static int
rs6000_emit_int_cmove(rtx dest,rtx op,rtx true_cond,rtx false_cond)19945 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19946 {
19947   rtx condition_rtx, cr;
19948   enum machine_mode mode = GET_MODE (dest);
19949   enum rtx_code cond_code;
19950   rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
19951   bool signedp;
19952 
19953   if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
19954     return 0;
19955 
19956   /* We still have to do the compare, because isel doesn't do a
19957      compare, it just looks at the CRx bits set by a previous compare
19958      instruction.  */
19959   condition_rtx = rs6000_generate_compare (op, mode);
19960   cond_code = GET_CODE (condition_rtx);
19961   cr = XEXP (condition_rtx, 0);
19962   signedp = GET_MODE (cr) == CCmode;
19963 
19964   isel_func = (mode == SImode
19965 	       ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
19966 	       : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
19967 
19968   switch (cond_code)
19969     {
19970     case LT: case GT: case LTU: case GTU: case EQ:
19971       /* isel handles these directly.  */
19972       break;
19973 
19974     default:
19975       /* We need to swap the sense of the comparison.  */
19976       {
19977 	rtx t = true_cond;
19978 	true_cond = false_cond;
19979 	false_cond = t;
19980 	PUT_CODE (condition_rtx, reverse_condition (cond_code));
19981       }
19982       break;
19983     }
19984 
19985   false_cond = force_reg (mode, false_cond);
19986   if (true_cond != const0_rtx)
19987     true_cond = force_reg (mode, true_cond);
19988 
19989   emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
19990 
19991   return 1;
19992 }
19993 
19994 const char *
output_isel(rtx * operands)19995 output_isel (rtx *operands)
19996 {
19997   enum rtx_code code;
19998 
19999   code = GET_CODE (operands[1]);
20000 
20001   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
20002     {
20003       gcc_assert (GET_CODE (operands[2]) == REG
20004 		  && GET_CODE (operands[3]) == REG);
20005       PUT_CODE (operands[1], reverse_condition (code));
20006       return "isel %0,%3,%2,%j1";
20007     }
20008 
20009   return "isel %0,%2,%3,%j1";
20010 }
20011 
20012 void
rs6000_emit_minmax(rtx dest,enum rtx_code code,rtx op0,rtx op1)20013 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
20014 {
20015   enum machine_mode mode = GET_MODE (op0);
20016   enum rtx_code c;
20017   rtx target;
20018 
20019   /* VSX/altivec have direct min/max insns.  */
20020   if ((code == SMAX || code == SMIN)
20021       && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
20022 	  || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
20023     {
20024       emit_insn (gen_rtx_SET (VOIDmode,
20025 			      dest,
20026 			      gen_rtx_fmt_ee (code, mode, op0, op1)));
20027       return;
20028     }
20029 
20030   if (code == SMAX || code == SMIN)
20031     c = GE;
20032   else
20033     c = GEU;
20034 
20035   if (code == SMAX || code == UMAX)
20036     target = emit_conditional_move (dest, c, op0, op1, mode,
20037 				    op0, op1, mode, 0);
20038   else
20039     target = emit_conditional_move (dest, c, op0, op1, mode,
20040 				    op1, op0, mode, 0);
20041   gcc_assert (target);
20042   if (target != dest)
20043     emit_move_insn (dest, target);
20044 }
20045 
20046 /* A subroutine of the atomic operation splitters.  Jump to LABEL if
20047    COND is true.  Mark the jump as unlikely to be taken.  */
20048 
20049 static void
emit_unlikely_jump(rtx cond,rtx label)20050 emit_unlikely_jump (rtx cond, rtx label)
20051 {
20052   rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
20053   rtx x;
20054 
20055   x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
20056   x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
20057   add_reg_note (x, REG_BR_PROB, very_unlikely);
20058 }
20059 
20060 /* A subroutine of the atomic operation splitters.  Emit a load-locked
20061    instruction in MODE.  For QI/HImode, possibly use a pattern than includes
20062    the zero_extend operation.  */
20063 
20064 static void
emit_load_locked(enum machine_mode mode,rtx reg,rtx mem)20065 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
20066 {
20067   rtx (*fn) (rtx, rtx) = NULL;
20068 
20069   switch (mode)
20070     {
20071     case QImode:
20072       fn = gen_load_lockedqi;
20073       break;
20074     case HImode:
20075       fn = gen_load_lockedhi;
20076       break;
20077     case SImode:
20078       if (GET_MODE (mem) == QImode)
20079 	fn = gen_load_lockedqi_si;
20080       else if (GET_MODE (mem) == HImode)
20081 	fn = gen_load_lockedhi_si;
20082       else
20083 	fn = gen_load_lockedsi;
20084       break;
20085     case DImode:
20086       fn = gen_load_lockeddi;
20087       break;
20088     case TImode:
20089       fn = gen_load_lockedti;
20090       break;
20091     default:
20092       gcc_unreachable ();
20093     }
20094   emit_insn (fn (reg, mem));
20095 }
20096 
20097 /* A subroutine of the atomic operation splitters.  Emit a store-conditional
20098    instruction in MODE.  */
20099 
20100 static void
emit_store_conditional(enum machine_mode mode,rtx res,rtx mem,rtx val)20101 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
20102 {
20103   rtx (*fn) (rtx, rtx, rtx) = NULL;
20104 
20105   switch (mode)
20106     {
20107     case QImode:
20108       fn = gen_store_conditionalqi;
20109       break;
20110     case HImode:
20111       fn = gen_store_conditionalhi;
20112       break;
20113     case SImode:
20114       fn = gen_store_conditionalsi;
20115       break;
20116     case DImode:
20117       fn = gen_store_conditionaldi;
20118       break;
20119     case TImode:
20120       fn = gen_store_conditionalti;
20121       break;
20122     default:
20123       gcc_unreachable ();
20124     }
20125 
20126   /* Emit sync before stwcx. to address PPC405 Erratum.  */
20127   if (PPC405_ERRATUM77)
20128     emit_insn (gen_hwsync ());
20129 
20130   emit_insn (fn (res, mem, val));
20131 }
20132 
20133 /* Expand barriers before and after a load_locked/store_cond sequence.  */
20134 
20135 static rtx
rs6000_pre_atomic_barrier(rtx mem,enum memmodel model)20136 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
20137 {
20138   rtx addr = XEXP (mem, 0);
20139   int strict_p = (reload_in_progress || reload_completed);
20140 
20141   if (!legitimate_indirect_address_p (addr, strict_p)
20142       && !legitimate_indexed_address_p (addr, strict_p))
20143     {
20144       addr = force_reg (Pmode, addr);
20145       mem = replace_equiv_address_nv (mem, addr);
20146     }
20147 
20148   switch (model)
20149     {
20150     case MEMMODEL_RELAXED:
20151     case MEMMODEL_CONSUME:
20152     case MEMMODEL_ACQUIRE:
20153       break;
20154     case MEMMODEL_RELEASE:
20155     case MEMMODEL_ACQ_REL:
20156       emit_insn (gen_lwsync ());
20157       break;
20158     case MEMMODEL_SEQ_CST:
20159       emit_insn (gen_hwsync ());
20160       break;
20161     default:
20162       gcc_unreachable ();
20163     }
20164   return mem;
20165 }
20166 
20167 static void
rs6000_post_atomic_barrier(enum memmodel model)20168 rs6000_post_atomic_barrier (enum memmodel model)
20169 {
20170   switch (model)
20171     {
20172     case MEMMODEL_RELAXED:
20173     case MEMMODEL_CONSUME:
20174     case MEMMODEL_RELEASE:
20175       break;
20176     case MEMMODEL_ACQUIRE:
20177     case MEMMODEL_ACQ_REL:
20178     case MEMMODEL_SEQ_CST:
20179       emit_insn (gen_isync ());
20180       break;
20181     default:
20182       gcc_unreachable ();
20183     }
20184 }
20185 
20186 /* A subroutine of the various atomic expanders.  For sub-word operations,
20187    we must adjust things to operate on SImode.  Given the original MEM,
20188    return a new aligned memory.  Also build and return the quantities by
20189    which to shift and mask.  */
20190 
20191 static rtx
rs6000_adjust_atomic_subword(rtx orig_mem,rtx * pshift,rtx * pmask)20192 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
20193 {
20194   rtx addr, align, shift, mask, mem;
20195   HOST_WIDE_INT shift_mask;
20196   enum machine_mode mode = GET_MODE (orig_mem);
20197 
20198   /* For smaller modes, we have to implement this via SImode.  */
20199   shift_mask = (mode == QImode ? 0x18 : 0x10);
20200 
20201   addr = XEXP (orig_mem, 0);
20202   addr = force_reg (GET_MODE (addr), addr);
20203 
20204   /* Aligned memory containing subword.  Generate a new memory.  We
20205      do not want any of the existing MEM_ATTR data, as we're now
20206      accessing memory outside the original object.  */
20207   align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
20208 			       NULL_RTX, 1, OPTAB_LIB_WIDEN);
20209   mem = gen_rtx_MEM (SImode, align);
20210   MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
20211   if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
20212     set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
20213 
20214   /* Shift amount for subword relative to aligned word.  */
20215   shift = gen_reg_rtx (SImode);
20216   addr = gen_lowpart (SImode, addr);
20217   emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
20218   if (WORDS_BIG_ENDIAN)
20219     shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
20220 			         shift, 1, OPTAB_LIB_WIDEN);
20221   *pshift = shift;
20222 
20223   /* Mask for insertion.  */
20224   mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
20225 			      shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
20226   *pmask = mask;
20227 
20228   return mem;
20229 }
20230 
20231 /* A subroutine of the various atomic expanders.  For sub-word operands,
20232    combine OLDVAL and NEWVAL via MASK.  Returns a new pseduo.  */
20233 
20234 static rtx
rs6000_mask_atomic_subword(rtx oldval,rtx newval,rtx mask)20235 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
20236 {
20237   rtx x;
20238 
20239   x = gen_reg_rtx (SImode);
20240   emit_insn (gen_rtx_SET (VOIDmode, x,
20241 			  gen_rtx_AND (SImode,
20242 				       gen_rtx_NOT (SImode, mask),
20243 				       oldval)));
20244 
20245   x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
20246 
20247   return x;
20248 }
20249 
20250 /* A subroutine of the various atomic expanders.  For sub-word operands,
20251    extract WIDE to NARROW via SHIFT.  */
20252 
20253 static void
rs6000_finish_atomic_subword(rtx narrow,rtx wide,rtx shift)20254 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
20255 {
20256   wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
20257 			      wide, 1, OPTAB_LIB_WIDEN);
20258   emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
20259 }
20260 
20261 /* Expand an atomic compare and swap operation.  */
20262 
20263 void
rs6000_expand_atomic_compare_and_swap(rtx operands[])20264 rs6000_expand_atomic_compare_and_swap (rtx operands[])
20265 {
20266   rtx boolval, retval, mem, oldval, newval, cond;
20267   rtx label1, label2, x, mask, shift;
20268   enum machine_mode mode, orig_mode;
20269   enum memmodel mod_s, mod_f;
20270   bool is_weak;
20271 
20272   boolval = operands[0];
20273   retval = operands[1];
20274   mem = operands[2];
20275   oldval = operands[3];
20276   newval = operands[4];
20277   is_weak = (INTVAL (operands[5]) != 0);
20278   mod_s = (enum memmodel) INTVAL (operands[6]);
20279   mod_f = (enum memmodel) INTVAL (operands[7]);
20280   orig_mode = mode = GET_MODE (mem);
20281 
20282   mask = shift = NULL_RTX;
20283   if (mode == QImode || mode == HImode)
20284     {
20285       /* Before power8, we didn't have access to lbarx/lharx, so generate a
20286 	 lwarx and shift/mask operations.  With power8, we need to do the
20287 	 comparison in SImode, but the store is still done in QI/HImode.  */
20288       oldval = convert_modes (SImode, mode, oldval, 1);
20289 
20290       if (!TARGET_SYNC_HI_QI)
20291 	{
20292 	  mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20293 
20294 	  /* Shift and mask OLDVAL into position with the word.  */
20295 	  oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
20296 					NULL_RTX, 1, OPTAB_LIB_WIDEN);
20297 
20298 	  /* Shift and mask NEWVAL into position within the word.  */
20299 	  newval = convert_modes (SImode, mode, newval, 1);
20300 	  newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
20301 					NULL_RTX, 1, OPTAB_LIB_WIDEN);
20302 	}
20303 
20304       /* Prepare to adjust the return value.  */
20305       retval = gen_reg_rtx (SImode);
20306       mode = SImode;
20307     }
20308   else if (reg_overlap_mentioned_p (retval, oldval))
20309     oldval = copy_to_reg (oldval);
20310 
20311   mem = rs6000_pre_atomic_barrier (mem, mod_s);
20312 
20313   label1 = NULL_RTX;
20314   if (!is_weak)
20315     {
20316       label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20317       emit_label (XEXP (label1, 0));
20318     }
20319   label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20320 
20321   emit_load_locked (mode, retval, mem);
20322 
20323   x = retval;
20324   if (mask)
20325     {
20326       x = expand_simple_binop (SImode, AND, retval, mask,
20327 			       NULL_RTX, 1, OPTAB_LIB_WIDEN);
20328     }
20329 
20330   cond = gen_reg_rtx (CCmode);
20331   /* If we have TImode, synthesize a comparison.  */
20332   if (mode != TImode)
20333     x = gen_rtx_COMPARE (CCmode, x, oldval);
20334   else
20335     {
20336       rtx xor1_result = gen_reg_rtx (DImode);
20337       rtx xor2_result = gen_reg_rtx (DImode);
20338       rtx or_result = gen_reg_rtx (DImode);
20339       rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
20340       rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
20341       rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
20342       rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
20343 
20344       emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
20345       emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
20346       emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
20347       x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
20348     }
20349 
20350   emit_insn (gen_rtx_SET (VOIDmode, cond, x));
20351 
20352   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20353   emit_unlikely_jump (x, label2);
20354 
20355   x = newval;
20356   if (mask)
20357     x = rs6000_mask_atomic_subword (retval, newval, mask);
20358 
20359   emit_store_conditional (orig_mode, cond, mem, x);
20360 
20361   if (!is_weak)
20362     {
20363       x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20364       emit_unlikely_jump (x, label1);
20365     }
20366 
20367   if (mod_f != MEMMODEL_RELAXED)
20368     emit_label (XEXP (label2, 0));
20369 
20370   rs6000_post_atomic_barrier (mod_s);
20371 
20372   if (mod_f == MEMMODEL_RELAXED)
20373     emit_label (XEXP (label2, 0));
20374 
20375   if (shift)
20376     rs6000_finish_atomic_subword (operands[1], retval, shift);
20377   else if (mode != GET_MODE (operands[1]))
20378     convert_move (operands[1], retval, 1);
20379 
20380   /* In all cases, CR0 contains EQ on success, and NE on failure.  */
20381   x = gen_rtx_EQ (SImode, cond, const0_rtx);
20382   emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
20383 }
20384 
20385 /* Expand an atomic exchange operation.  */
20386 
20387 void
rs6000_expand_atomic_exchange(rtx operands[])20388 rs6000_expand_atomic_exchange (rtx operands[])
20389 {
20390   rtx retval, mem, val, cond;
20391   enum machine_mode mode;
20392   enum memmodel model;
20393   rtx label, x, mask, shift;
20394 
20395   retval = operands[0];
20396   mem = operands[1];
20397   val = operands[2];
20398   model = (enum memmodel) INTVAL (operands[3]);
20399   mode = GET_MODE (mem);
20400 
20401   mask = shift = NULL_RTX;
20402   if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
20403     {
20404       mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20405 
20406       /* Shift and mask VAL into position with the word.  */
20407       val = convert_modes (SImode, mode, val, 1);
20408       val = expand_simple_binop (SImode, ASHIFT, val, shift,
20409 				 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20410 
20411       /* Prepare to adjust the return value.  */
20412       retval = gen_reg_rtx (SImode);
20413       mode = SImode;
20414     }
20415 
20416   mem = rs6000_pre_atomic_barrier (mem, model);
20417 
20418   label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20419   emit_label (XEXP (label, 0));
20420 
20421   emit_load_locked (mode, retval, mem);
20422 
20423   x = val;
20424   if (mask)
20425     x = rs6000_mask_atomic_subword (retval, val, mask);
20426 
20427   cond = gen_reg_rtx (CCmode);
20428   emit_store_conditional (mode, cond, mem, x);
20429 
20430   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20431   emit_unlikely_jump (x, label);
20432 
20433   rs6000_post_atomic_barrier (model);
20434 
20435   if (shift)
20436     rs6000_finish_atomic_subword (operands[0], retval, shift);
20437 }
20438 
20439 /* Expand an atomic fetch-and-operate pattern.  CODE is the binary operation
20440    to perform.  MEM is the memory on which to operate.  VAL is the second
20441    operand of the binary operator.  BEFORE and AFTER are optional locations to
20442    return the value of MEM either before of after the operation.  MODEL_RTX
20443    is a CONST_INT containing the memory model to use.  */
20444 
20445 void
rs6000_expand_atomic_op(enum rtx_code code,rtx mem,rtx val,rtx orig_before,rtx orig_after,rtx model_rtx)20446 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
20447 			 rtx orig_before, rtx orig_after, rtx model_rtx)
20448 {
20449   enum memmodel model = (enum memmodel) INTVAL (model_rtx);
20450   enum machine_mode mode = GET_MODE (mem);
20451   enum machine_mode store_mode = mode;
20452   rtx label, x, cond, mask, shift;
20453   rtx before = orig_before, after = orig_after;
20454 
20455   mask = shift = NULL_RTX;
20456   /* On power8, we want to use SImode for the operation.  On previous systems,
20457      use the operation in a subword and shift/mask to get the proper byte or
20458      halfword.  */
20459   if (mode == QImode || mode == HImode)
20460     {
20461       if (TARGET_SYNC_HI_QI)
20462 	{
20463 	  val = convert_modes (SImode, mode, val, 1);
20464 
20465 	  /* Prepare to adjust the return value.  */
20466 	  before = gen_reg_rtx (SImode);
20467 	  if (after)
20468 	    after = gen_reg_rtx (SImode);
20469 	  mode = SImode;
20470 	}
20471       else
20472 	{
20473 	  mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20474 
20475 	  /* Shift and mask VAL into position with the word.  */
20476 	  val = convert_modes (SImode, mode, val, 1);
20477 	  val = expand_simple_binop (SImode, ASHIFT, val, shift,
20478 				     NULL_RTX, 1, OPTAB_LIB_WIDEN);
20479 
20480 	  switch (code)
20481 	    {
20482 	    case IOR:
20483 	    case XOR:
20484 	      /* We've already zero-extended VAL.  That is sufficient to
20485 		 make certain that it does not affect other bits.  */
20486 	      mask = NULL;
20487 	      break;
20488 
20489 	    case AND:
20490 	      /* If we make certain that all of the other bits in VAL are
20491 		 set, that will be sufficient to not affect other bits.  */
20492 	      x = gen_rtx_NOT (SImode, mask);
20493 	      x = gen_rtx_IOR (SImode, x, val);
20494 	      emit_insn (gen_rtx_SET (VOIDmode, val, x));
20495 	      mask = NULL;
20496 	      break;
20497 
20498 	    case NOT:
20499 	    case PLUS:
20500 	    case MINUS:
20501 	      /* These will all affect bits outside the field and need
20502 		 adjustment via MASK within the loop.  */
20503 	      break;
20504 
20505 	    default:
20506 	      gcc_unreachable ();
20507 	    }
20508 
20509 	  /* Prepare to adjust the return value.  */
20510 	  before = gen_reg_rtx (SImode);
20511 	  if (after)
20512 	    after = gen_reg_rtx (SImode);
20513 	  store_mode = mode = SImode;
20514 	}
20515     }
20516 
20517   mem = rs6000_pre_atomic_barrier (mem, model);
20518 
20519   label = gen_label_rtx ();
20520   emit_label (label);
20521   label = gen_rtx_LABEL_REF (VOIDmode, label);
20522 
20523   if (before == NULL_RTX)
20524     before = gen_reg_rtx (mode);
20525 
20526   emit_load_locked (mode, before, mem);
20527 
20528   if (code == NOT)
20529     {
20530       x = expand_simple_binop (mode, AND, before, val,
20531 			       NULL_RTX, 1, OPTAB_LIB_WIDEN);
20532       after = expand_simple_unop (mode, NOT, x, after, 1);
20533     }
20534   else
20535     {
20536       after = expand_simple_binop (mode, code, before, val,
20537 				   after, 1, OPTAB_LIB_WIDEN);
20538     }
20539 
20540   x = after;
20541   if (mask)
20542     {
20543       x = expand_simple_binop (SImode, AND, after, mask,
20544 			       NULL_RTX, 1, OPTAB_LIB_WIDEN);
20545       x = rs6000_mask_atomic_subword (before, x, mask);
20546     }
20547   else if (store_mode != mode)
20548     x = convert_modes (store_mode, mode, x, 1);
20549 
20550   cond = gen_reg_rtx (CCmode);
20551   emit_store_conditional (store_mode, cond, mem, x);
20552 
20553   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20554   emit_unlikely_jump (x, label);
20555 
20556   rs6000_post_atomic_barrier (model);
20557 
20558   if (shift)
20559     {
20560       /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
20561 	 then do the calcuations in a SImode register.  */
20562       if (orig_before)
20563 	rs6000_finish_atomic_subword (orig_before, before, shift);
20564       if (orig_after)
20565 	rs6000_finish_atomic_subword (orig_after, after, shift);
20566     }
20567   else if (store_mode != mode)
20568     {
20569       /* QImode/HImode on machines with lbarx/lharx where we do the native
20570 	 operation and then do the calcuations in a SImode register.  */
20571       if (orig_before)
20572 	convert_move (orig_before, before, 1);
20573       if (orig_after)
20574 	convert_move (orig_after, after, 1);
20575     }
20576   else if (orig_after && after != orig_after)
20577     emit_move_insn (orig_after, after);
20578 }
20579 
20580 /* Emit instructions to move SRC to DST.  Called by splitters for
20581    multi-register moves.  It will emit at most one instruction for
20582    each register that is accessed; that is, it won't emit li/lis pairs
20583    (or equivalent for 64-bit code).  One of SRC or DST must be a hard
20584    register.  */
20585 
20586 void
rs6000_split_multireg_move(rtx dst,rtx src)20587 rs6000_split_multireg_move (rtx dst, rtx src)
20588 {
20589   /* The register number of the first register being moved.  */
20590   int reg;
20591   /* The mode that is to be moved.  */
20592   enum machine_mode mode;
20593   /* The mode that the move is being done in, and its size.  */
20594   enum machine_mode reg_mode;
20595   int reg_mode_size;
20596   /* The number of registers that will be moved.  */
20597   int nregs;
20598 
20599   reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
20600   mode = GET_MODE (dst);
20601   nregs = hard_regno_nregs[reg][mode];
20602   if (FP_REGNO_P (reg))
20603     reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
20604 	((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
20605   else if (ALTIVEC_REGNO_P (reg))
20606     reg_mode = V16QImode;
20607   else if (TARGET_E500_DOUBLE && mode == TFmode)
20608     reg_mode = DFmode;
20609   else
20610     reg_mode = word_mode;
20611   reg_mode_size = GET_MODE_SIZE (reg_mode);
20612 
20613   gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
20614 
20615   /* TDmode residing in FP registers is special, since the ISA requires that
20616      the lower-numbered word of a register pair is always the most significant
20617      word, even in little-endian mode.  This does not match the usual subreg
20618      semantics, so we cannnot use simplify_gen_subreg in those cases.  Access
20619      the appropriate constituent registers "by hand" in little-endian mode.
20620 
20621      Note we do not need to check for destructive overlap here since TDmode
20622      can only reside in even/odd register pairs.  */
20623   if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
20624     {
20625       rtx p_src, p_dst;
20626       int i;
20627 
20628       for (i = 0; i < nregs; i++)
20629 	{
20630 	  if (REG_P (src) && FP_REGNO_P (REGNO (src)))
20631 	    p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
20632 	  else
20633 	    p_src = simplify_gen_subreg (reg_mode, src, mode,
20634 					 i * reg_mode_size);
20635 
20636 	  if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
20637 	    p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
20638 	  else
20639 	    p_dst = simplify_gen_subreg (reg_mode, dst, mode,
20640 					 i * reg_mode_size);
20641 
20642 	  emit_insn (gen_rtx_SET (VOIDmode, p_dst, p_src));
20643 	}
20644 
20645       return;
20646     }
20647 
20648   if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
20649     {
20650       /* Move register range backwards, if we might have destructive
20651 	 overlap.  */
20652       int i;
20653       for (i = nregs - 1; i >= 0; i--)
20654 	emit_insn (gen_rtx_SET (VOIDmode,
20655 				simplify_gen_subreg (reg_mode, dst, mode,
20656 						     i * reg_mode_size),
20657 				simplify_gen_subreg (reg_mode, src, mode,
20658 						     i * reg_mode_size)));
20659     }
20660   else
20661     {
20662       int i;
20663       int j = -1;
20664       bool used_update = false;
20665       rtx restore_basereg = NULL_RTX;
20666 
20667       if (MEM_P (src) && INT_REGNO_P (reg))
20668 	{
20669 	  rtx breg;
20670 
20671 	  if (GET_CODE (XEXP (src, 0)) == PRE_INC
20672 	      || GET_CODE (XEXP (src, 0)) == PRE_DEC)
20673 	    {
20674 	      rtx delta_rtx;
20675 	      breg = XEXP (XEXP (src, 0), 0);
20676 	      delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
20677 			   ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
20678 			   : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
20679 	      emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20680 	      src = replace_equiv_address (src, breg);
20681 	    }
20682 	  else if (! rs6000_offsettable_memref_p (src, reg_mode))
20683 	    {
20684 	      if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
20685 		{
20686 		  rtx basereg = XEXP (XEXP (src, 0), 0);
20687 		  if (TARGET_UPDATE)
20688 		    {
20689 		      rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
20690 		      emit_insn (gen_rtx_SET (VOIDmode, ndst,
20691 				 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
20692 		      used_update = true;
20693 		    }
20694 		  else
20695 		    emit_insn (gen_rtx_SET (VOIDmode, basereg,
20696 			       XEXP (XEXP (src, 0), 1)));
20697 		  src = replace_equiv_address (src, basereg);
20698 		}
20699 	      else
20700 		{
20701 		  rtx basereg = gen_rtx_REG (Pmode, reg);
20702 		  emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
20703 		  src = replace_equiv_address (src, basereg);
20704 		}
20705 	    }
20706 
20707 	  breg = XEXP (src, 0);
20708 	  if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
20709 	    breg = XEXP (breg, 0);
20710 
20711 	  /* If the base register we are using to address memory is
20712 	     also a destination reg, then change that register last.  */
20713 	  if (REG_P (breg)
20714 	      && REGNO (breg) >= REGNO (dst)
20715 	      && REGNO (breg) < REGNO (dst) + nregs)
20716 	    j = REGNO (breg) - REGNO (dst);
20717 	}
20718       else if (MEM_P (dst) && INT_REGNO_P (reg))
20719 	{
20720 	  rtx breg;
20721 
20722 	  if (GET_CODE (XEXP (dst, 0)) == PRE_INC
20723 	      || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
20724 	    {
20725 	      rtx delta_rtx;
20726 	      breg = XEXP (XEXP (dst, 0), 0);
20727 	      delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
20728 			   ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
20729 			   : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
20730 
20731 	      /* We have to update the breg before doing the store.
20732 		 Use store with update, if available.  */
20733 
20734 	      if (TARGET_UPDATE)
20735 		{
20736 		  rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20737 		  emit_insn (TARGET_32BIT
20738 			     ? (TARGET_POWERPC64
20739 				? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
20740 				: gen_movsi_update (breg, breg, delta_rtx, nsrc))
20741 			     : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
20742 		  used_update = true;
20743 		}
20744 	      else
20745 		emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20746 	      dst = replace_equiv_address (dst, breg);
20747 	    }
20748 	  else if (!rs6000_offsettable_memref_p (dst, reg_mode)
20749 		   && GET_CODE (XEXP (dst, 0)) != LO_SUM)
20750 	    {
20751 	      if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
20752 		{
20753 		  rtx basereg = XEXP (XEXP (dst, 0), 0);
20754 		  if (TARGET_UPDATE)
20755 		    {
20756 		      rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20757 		      emit_insn (gen_rtx_SET (VOIDmode,
20758 				 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
20759 		      used_update = true;
20760 		    }
20761 		  else
20762 		    emit_insn (gen_rtx_SET (VOIDmode, basereg,
20763 			       XEXP (XEXP (dst, 0), 1)));
20764 		  dst = replace_equiv_address (dst, basereg);
20765 		}
20766 	      else
20767 		{
20768 		  rtx basereg = XEXP (XEXP (dst, 0), 0);
20769 		  rtx offsetreg = XEXP (XEXP (dst, 0), 1);
20770 		  gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
20771 			      && REG_P (basereg)
20772 			      && REG_P (offsetreg)
20773 			      && REGNO (basereg) != REGNO (offsetreg));
20774 		  if (REGNO (basereg) == 0)
20775 		    {
20776 		      rtx tmp = offsetreg;
20777 		      offsetreg = basereg;
20778 		      basereg = tmp;
20779 		    }
20780 		  emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
20781 		  restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
20782 		  dst = replace_equiv_address (dst, basereg);
20783 		}
20784 	    }
20785 	  else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
20786 	    gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
20787 	}
20788 
20789       for (i = 0; i < nregs; i++)
20790 	{
20791 	  /* Calculate index to next subword.  */
20792 	  ++j;
20793 	  if (j == nregs)
20794 	    j = 0;
20795 
20796 	  /* If compiler already emitted move of first word by
20797 	     store with update, no need to do anything.  */
20798 	  if (j == 0 && used_update)
20799 	    continue;
20800 
20801 	  emit_insn (gen_rtx_SET (VOIDmode,
20802 				  simplify_gen_subreg (reg_mode, dst, mode,
20803 						       j * reg_mode_size),
20804 				  simplify_gen_subreg (reg_mode, src, mode,
20805 						       j * reg_mode_size)));
20806 	}
20807       if (restore_basereg != NULL_RTX)
20808 	emit_insn (restore_basereg);
20809     }
20810 }
20811 
20812 
20813 /* This page contains routines that are used to determine what the
20814    function prologue and epilogue code will do and write them out.  */
20815 
20816 static inline bool
save_reg_p(int r)20817 save_reg_p (int r)
20818 {
20819   return !call_used_regs[r] && df_regs_ever_live_p (r);
20820 }
20821 
20822 /* Return the first fixed-point register that is required to be
20823    saved. 32 if none.  */
20824 
20825 int
first_reg_to_save(void)20826 first_reg_to_save (void)
20827 {
20828   int first_reg;
20829 
20830   /* Find lowest numbered live register.  */
20831   for (first_reg = 13; first_reg <= 31; first_reg++)
20832     if (save_reg_p (first_reg))
20833       break;
20834 
20835   if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
20836       && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
20837 	  || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
20838 	  || (TARGET_TOC && TARGET_MINIMAL_TOC))
20839       && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
20840     first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
20841 
20842 #if TARGET_MACHO
20843   if (flag_pic
20844       && crtl->uses_pic_offset_table
20845       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
20846     return RS6000_PIC_OFFSET_TABLE_REGNUM;
20847 #endif
20848 
20849   return first_reg;
20850 }
20851 
20852 /* Similar, for FP regs.  */
20853 
20854 int
first_fp_reg_to_save(void)20855 first_fp_reg_to_save (void)
20856 {
20857   int first_reg;
20858 
20859   /* Find lowest numbered live register.  */
20860   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
20861     if (save_reg_p (first_reg))
20862       break;
20863 
20864   return first_reg;
20865 }
20866 
20867 /* Similar, for AltiVec regs.  */
20868 
20869 static int
first_altivec_reg_to_save(void)20870 first_altivec_reg_to_save (void)
20871 {
20872   int i;
20873 
20874   /* Stack frame remains as is unless we are in AltiVec ABI.  */
20875   if (! TARGET_ALTIVEC_ABI)
20876     return LAST_ALTIVEC_REGNO + 1;
20877 
20878   /* On Darwin, the unwind routines are compiled without
20879      TARGET_ALTIVEC, and use save_world to save/restore the
20880      altivec registers when necessary.  */
20881   if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20882       && ! TARGET_ALTIVEC)
20883     return FIRST_ALTIVEC_REGNO + 20;
20884 
20885   /* Find lowest numbered live register.  */
20886   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
20887     if (save_reg_p (i))
20888       break;
20889 
20890   return i;
20891 }
20892 
20893 /* Return a 32-bit mask of the AltiVec registers we need to set in
20894    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
20895    the 32-bit word is 0.  */
20896 
20897 static unsigned int
compute_vrsave_mask(void)20898 compute_vrsave_mask (void)
20899 {
20900   unsigned int i, mask = 0;
20901 
20902   /* On Darwin, the unwind routines are compiled without
20903      TARGET_ALTIVEC, and use save_world to save/restore the
20904      call-saved altivec registers when necessary.  */
20905   if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20906       && ! TARGET_ALTIVEC)
20907     mask |= 0xFFF;
20908 
20909   /* First, find out if we use _any_ altivec registers.  */
20910   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
20911     if (df_regs_ever_live_p (i))
20912       mask |= ALTIVEC_REG_BIT (i);
20913 
20914   if (mask == 0)
20915     return mask;
20916 
20917   /* Next, remove the argument registers from the set.  These must
20918      be in the VRSAVE mask set by the caller, so we don't need to add
20919      them in again.  More importantly, the mask we compute here is
20920      used to generate CLOBBERs in the set_vrsave insn, and we do not
20921      wish the argument registers to die.  */
20922   for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
20923     mask &= ~ALTIVEC_REG_BIT (i);
20924 
20925   /* Similarly, remove the return value from the set.  */
20926   {
20927     bool yes = false;
20928     diddle_return_value (is_altivec_return_reg, &yes);
20929     if (yes)
20930       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
20931   }
20932 
20933   return mask;
20934 }
20935 
20936 /* For a very restricted set of circumstances, we can cut down the
20937    size of prologues/epilogues by calling our own save/restore-the-world
20938    routines.  */
20939 
20940 static void
compute_save_world_info(rs6000_stack_t * info_ptr)20941 compute_save_world_info (rs6000_stack_t *info_ptr)
20942 {
20943   info_ptr->world_save_p = 1;
20944   info_ptr->world_save_p
20945     = (WORLD_SAVE_P (info_ptr)
20946        && DEFAULT_ABI == ABI_DARWIN
20947        && !cfun->has_nonlocal_label
20948        && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
20949        && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
20950        && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
20951        && info_ptr->cr_save_p);
20952 
20953   /* This will not work in conjunction with sibcalls.  Make sure there
20954      are none.  (This check is expensive, but seldom executed.) */
20955   if (WORLD_SAVE_P (info_ptr))
20956     {
20957       rtx insn;
20958       for ( insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
20959 	if ( GET_CODE (insn) == CALL_INSN
20960 	     && SIBLING_CALL_P (insn))
20961 	  {
20962 	    info_ptr->world_save_p = 0;
20963 	    break;
20964 	  }
20965     }
20966 
20967   if (WORLD_SAVE_P (info_ptr))
20968     {
20969       /* Even if we're not touching VRsave, make sure there's room on the
20970 	 stack for it, if it looks like we're calling SAVE_WORLD, which
20971 	 will attempt to save it. */
20972       info_ptr->vrsave_size  = 4;
20973 
20974       /* If we are going to save the world, we need to save the link register too.  */
20975       info_ptr->lr_save_p = 1;
20976 
20977       /* "Save" the VRsave register too if we're saving the world.  */
20978       if (info_ptr->vrsave_mask == 0)
20979 	info_ptr->vrsave_mask = compute_vrsave_mask ();
20980 
20981       /* Because the Darwin register save/restore routines only handle
20982 	 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
20983 	 check.  */
20984       gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
20985 		  && (info_ptr->first_altivec_reg_save
20986 		      >= FIRST_SAVED_ALTIVEC_REGNO));
20987     }
20988   return;
20989 }
20990 
20991 
20992 static void
is_altivec_return_reg(rtx reg,void * xyes)20993 is_altivec_return_reg (rtx reg, void *xyes)
20994 {
20995   bool *yes = (bool *) xyes;
20996   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
20997     *yes = true;
20998 }
20999 
21000 
21001 /* Look for user-defined global regs in the range FIRST to LAST-1.
21002    We should not restore these, and so cannot use lmw or out-of-line
21003    restore functions if there are any.  We also can't save them
21004    (well, emit frame notes for them), because frame unwinding during
21005    exception handling will restore saved registers.  */
21006 
21007 static bool
global_regs_p(unsigned first,unsigned last)21008 global_regs_p (unsigned first, unsigned last)
21009 {
21010   while (first < last)
21011     if (global_regs[first++])
21012       return true;
21013   return false;
21014 }
21015 
21016 /* Determine the strategy for savings/restoring registers.  */
21017 
21018 enum {
21019   SAVRES_MULTIPLE = 0x1,
21020   SAVE_INLINE_FPRS = 0x2,
21021   SAVE_INLINE_GPRS = 0x4,
21022   REST_INLINE_FPRS = 0x8,
21023   REST_INLINE_GPRS = 0x10,
21024   SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
21025   SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
21026   REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
21027   SAVE_INLINE_VRS = 0x100,
21028   REST_INLINE_VRS = 0x200
21029 };
21030 
21031 static int
rs6000_savres_strategy(rs6000_stack_t * info,bool using_static_chain_p)21032 rs6000_savres_strategy (rs6000_stack_t *info,
21033 			bool using_static_chain_p)
21034 {
21035   int strategy = 0;
21036   bool lr_save_p;
21037 
21038   if (TARGET_MULTIPLE
21039       && !TARGET_POWERPC64
21040       && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
21041       && info->first_gp_reg_save < 31
21042       && !global_regs_p (info->first_gp_reg_save, 32))
21043     strategy |= SAVRES_MULTIPLE;
21044 
21045   if (crtl->calls_eh_return
21046       || cfun->machine->ra_need_lr)
21047     strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
21048 		 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
21049 		 | SAVE_INLINE_VRS | REST_INLINE_VRS);
21050 
21051   if (info->first_fp_reg_save == 64
21052       /* The out-of-line FP routines use double-precision stores;
21053 	 we can't use those routines if we don't have such stores.  */
21054       || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
21055       || global_regs_p (info->first_fp_reg_save, 64))
21056     strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
21057 
21058   if (info->first_gp_reg_save == 32
21059       || (!(strategy & SAVRES_MULTIPLE)
21060 	  && global_regs_p (info->first_gp_reg_save, 32)))
21061     strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
21062 
21063   if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
21064       || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
21065     strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
21066 
21067   /* Define cutoff for using out-of-line functions to save registers.  */
21068   if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
21069     {
21070       if (!optimize_size)
21071 	{
21072 	  strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
21073 	  strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
21074 	  strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
21075 	}
21076       else
21077 	{
21078 	  /* Prefer out-of-line restore if it will exit.  */
21079 	  if (info->first_fp_reg_save > 61)
21080 	    strategy |= SAVE_INLINE_FPRS;
21081 	  if (info->first_gp_reg_save > 29)
21082 	    {
21083 	      if (info->first_fp_reg_save == 64)
21084 		strategy |= SAVE_INLINE_GPRS;
21085 	      else
21086 		strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
21087 	    }
21088 	  if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
21089 	    strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
21090 	}
21091     }
21092   else if (DEFAULT_ABI == ABI_DARWIN)
21093     {
21094       if (info->first_fp_reg_save > 60)
21095 	strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
21096       if (info->first_gp_reg_save > 29)
21097 	strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
21098       strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
21099     }
21100   else
21101     {
21102       gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
21103       if (info->first_fp_reg_save > 61)
21104 	strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
21105       strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
21106       strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
21107     }
21108 
21109   /* Don't bother to try to save things out-of-line if r11 is occupied
21110      by the static chain.  It would require too much fiddling and the
21111      static chain is rarely used anyway.  FPRs are saved w.r.t the stack
21112      pointer on Darwin, and AIX uses r1 or r12.  */
21113   if (using_static_chain_p
21114       && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
21115     strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
21116 		 | SAVE_INLINE_GPRS
21117 		 | SAVE_INLINE_VRS | REST_INLINE_VRS);
21118 
21119   /* We can only use the out-of-line routines to restore if we've
21120      saved all the registers from first_fp_reg_save in the prologue.
21121      Otherwise, we risk loading garbage.  */
21122   if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
21123     {
21124       int i;
21125 
21126       for (i = info->first_fp_reg_save; i < 64; i++)
21127 	if (!save_reg_p (i))
21128 	  {
21129 	    strategy |= REST_INLINE_FPRS;
21130 	    break;
21131 	  }
21132     }
21133 
21134   /* If we are going to use store multiple, then don't even bother
21135      with the out-of-line routines, since the store-multiple
21136      instruction will always be smaller.  */
21137   if ((strategy & SAVRES_MULTIPLE))
21138     strategy |= SAVE_INLINE_GPRS;
21139 
21140   /* info->lr_save_p isn't yet set if the only reason lr needs to be
21141      saved is an out-of-line save or restore.  Set up the value for
21142      the next test (excluding out-of-line gpr restore).  */
21143   lr_save_p = (info->lr_save_p
21144 	       || !(strategy & SAVE_INLINE_GPRS)
21145 	       || !(strategy & SAVE_INLINE_FPRS)
21146 	       || !(strategy & SAVE_INLINE_VRS)
21147 	       || !(strategy & REST_INLINE_FPRS)
21148 	       || !(strategy & REST_INLINE_VRS));
21149 
21150   /* The situation is more complicated with load multiple.  We'd
21151      prefer to use the out-of-line routines for restores, since the
21152      "exit" out-of-line routines can handle the restore of LR and the
21153      frame teardown.  However if doesn't make sense to use the
21154      out-of-line routine if that is the only reason we'd need to save
21155      LR, and we can't use the "exit" out-of-line gpr restore if we
21156      have saved some fprs; In those cases it is advantageous to use
21157      load multiple when available.  */
21158   if ((strategy & SAVRES_MULTIPLE)
21159       && (!lr_save_p
21160 	  || info->first_fp_reg_save != 64))
21161     strategy |= REST_INLINE_GPRS;
21162 
21163   /* Saving CR interferes with the exit routines used on the SPE, so
21164      just punt here.  */
21165   if (TARGET_SPE_ABI
21166       && info->spe_64bit_regs_used
21167       && info->cr_save_p)
21168     strategy |= REST_INLINE_GPRS;
21169 
21170   /* We can only use load multiple or the out-of-line routines to
21171      restore if we've used store multiple or out-of-line routines
21172      in the prologue, i.e. if we've saved all the registers from
21173      first_gp_reg_save.  Otherwise, we risk loading garbage.  */
21174   if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
21175       == SAVE_INLINE_GPRS)
21176     {
21177       int i;
21178 
21179       for (i = info->first_gp_reg_save; i < 32; i++)
21180 	if (!save_reg_p (i))
21181 	  {
21182 	    strategy |= REST_INLINE_GPRS;
21183 	    break;
21184 	  }
21185     }
21186 
21187   if (TARGET_ELF && TARGET_64BIT)
21188     {
21189       if (!(strategy & SAVE_INLINE_FPRS))
21190 	strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
21191       else if (!(strategy & SAVE_INLINE_GPRS)
21192 	       && info->first_fp_reg_save == 64)
21193 	strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
21194     }
21195   else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
21196     strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
21197 
21198   if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
21199     strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
21200 
21201   return strategy;
21202 }
21203 
21204 /* Calculate the stack information for the current function.  This is
21205    complicated by having two separate calling sequences, the AIX calling
21206    sequence and the V.4 calling sequence.
21207 
21208    AIX (and Darwin/Mac OS X) stack frames look like:
21209 							  32-bit  64-bit
21210 	SP---->	+---------------------------------------+
21211 		| back chain to caller			| 0	  0
21212 		+---------------------------------------+
21213 		| saved CR				| 4       8 (8-11)
21214 		+---------------------------------------+
21215 		| saved LR				| 8       16
21216 		+---------------------------------------+
21217 		| reserved for compilers		| 12      24
21218 		+---------------------------------------+
21219 		| reserved for binders			| 16      32
21220 		+---------------------------------------+
21221 		| saved TOC pointer			| 20      40
21222 		+---------------------------------------+
21223 		| Parameter save area (P)		| 24      48
21224 		+---------------------------------------+
21225 		| Alloca space (A)			| 24+P    etc.
21226 		+---------------------------------------+
21227 		| Local variable space (L)		| 24+P+A
21228 		+---------------------------------------+
21229 		| Float/int conversion temporary (X)	| 24+P+A+L
21230 		+---------------------------------------+
21231 		| Save area for AltiVec registers (W)	| 24+P+A+L+X
21232 		+---------------------------------------+
21233 		| AltiVec alignment padding (Y)		| 24+P+A+L+X+W
21234 		+---------------------------------------+
21235 		| Save area for VRSAVE register (Z)	| 24+P+A+L+X+W+Y
21236 		+---------------------------------------+
21237 		| Save area for GP registers (G)	| 24+P+A+X+L+X+W+Y+Z
21238 		+---------------------------------------+
21239 		| Save area for FP registers (F)	| 24+P+A+X+L+X+W+Y+Z+G
21240 		+---------------------------------------+
21241 	old SP->| back chain to caller's caller		|
21242 		+---------------------------------------+
21243 
21244    The required alignment for AIX configurations is two words (i.e., 8
21245    or 16 bytes).
21246 
21247    The ELFv2 ABI is a variant of the AIX ABI.  Stack frames look like:
21248 
21249 	SP---->	+---------------------------------------+
21250 		| Back chain to caller			|  0
21251 		+---------------------------------------+
21252 		| Save area for CR			|  8
21253 		+---------------------------------------+
21254 		| Saved LR				|  16
21255 		+---------------------------------------+
21256 		| Saved TOC pointer			|  24
21257 		+---------------------------------------+
21258 		| Parameter save area (P)		|  32
21259 		+---------------------------------------+
21260 		| Alloca space (A)			|  32+P
21261 		+---------------------------------------+
21262 		| Local variable space (L)		|  32+P+A
21263 		+---------------------------------------+
21264 		| Save area for AltiVec registers (W)	|  32+P+A+L
21265 		+---------------------------------------+
21266 		| AltiVec alignment padding (Y)		|  32+P+A+L+W
21267 		+---------------------------------------+
21268 		| Save area for GP registers (G)	|  32+P+A+L+W+Y
21269 		+---------------------------------------+
21270 		| Save area for FP registers (F)	|  32+P+A+L+W+Y+G
21271 		+---------------------------------------+
21272 	old SP->| back chain to caller's caller		|  32+P+A+L+W+Y+G+F
21273 		+---------------------------------------+
21274 
21275 
21276    V.4 stack frames look like:
21277 
21278 	SP---->	+---------------------------------------+
21279 		| back chain to caller			| 0
21280 		+---------------------------------------+
21281 		| caller's saved LR			| 4
21282 		+---------------------------------------+
21283 		| Parameter save area (P)		| 8
21284 		+---------------------------------------+
21285 		| Alloca space (A)			| 8+P
21286 		+---------------------------------------+
21287 		| Varargs save area (V)			| 8+P+A
21288 		+---------------------------------------+
21289 		| Local variable space (L)		| 8+P+A+V
21290 		+---------------------------------------+
21291 		| Float/int conversion temporary (X)	| 8+P+A+V+L
21292 		+---------------------------------------+
21293 		| Save area for AltiVec registers (W)	| 8+P+A+V+L+X
21294 		+---------------------------------------+
21295 		| AltiVec alignment padding (Y)		| 8+P+A+V+L+X+W
21296 		+---------------------------------------+
21297 		| Save area for VRSAVE register (Z)	| 8+P+A+V+L+X+W+Y
21298 		+---------------------------------------+
21299 		| SPE: area for 64-bit GP registers	|
21300 		+---------------------------------------+
21301 		| SPE alignment padding			|
21302 		+---------------------------------------+
21303 		| saved CR (C)				| 8+P+A+V+L+X+W+Y+Z
21304 		+---------------------------------------+
21305 		| Save area for GP registers (G)	| 8+P+A+V+L+X+W+Y+Z+C
21306 		+---------------------------------------+
21307 		| Save area for FP registers (F)	| 8+P+A+V+L+X+W+Y+Z+C+G
21308 		+---------------------------------------+
21309 	old SP->| back chain to caller's caller		|
21310 		+---------------------------------------+
21311 
21312    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
21313    given.  (But note below and in sysv4.h that we require only 8 and
21314    may round up the size of our stack frame anyways.  The historical
21315    reason is early versions of powerpc-linux which didn't properly
21316    align the stack at program startup.  A happy side-effect is that
21317    -mno-eabi libraries can be used with -meabi programs.)
21318 
21319    The EABI configuration defaults to the V.4 layout.  However,
21320    the stack alignment requirements may differ.  If -mno-eabi is not
21321    given, the required stack alignment is 8 bytes; if -mno-eabi is
21322    given, the required alignment is 16 bytes.  (But see V.4 comment
21323    above.)  */
21324 
21325 #ifndef ABI_STACK_BOUNDARY
21326 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
21327 #endif
21328 
21329 static rs6000_stack_t *
rs6000_stack_info(void)21330 rs6000_stack_info (void)
21331 {
21332   /* We should never be called for thunks, we are not set up for that.  */
21333   gcc_assert (!cfun->is_thunk);
21334 
21335   rs6000_stack_t *info_ptr = &stack_info;
21336   int reg_size = TARGET_32BIT ? 4 : 8;
21337   int ehrd_size;
21338   int ehcr_size;
21339   int save_align;
21340   int first_gp;
21341   HOST_WIDE_INT non_fixed_size;
21342   bool using_static_chain_p;
21343 
21344   if (reload_completed && info_ptr->reload_completed)
21345     return info_ptr;
21346 
21347   memset (info_ptr, 0, sizeof (*info_ptr));
21348   info_ptr->reload_completed = reload_completed;
21349 
21350   if (TARGET_SPE)
21351     {
21352       /* Cache value so we don't rescan instruction chain over and over.  */
21353       if (cfun->machine->insn_chain_scanned_p == 0)
21354 	cfun->machine->insn_chain_scanned_p
21355 	  = spe_func_has_64bit_regs_p () + 1;
21356       info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
21357     }
21358 
21359   /* Select which calling sequence.  */
21360   info_ptr->abi = DEFAULT_ABI;
21361 
21362   /* Calculate which registers need to be saved & save area size.  */
21363   info_ptr->first_gp_reg_save = first_reg_to_save ();
21364   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
21365      even if it currently looks like we won't.  Reload may need it to
21366      get at a constant; if so, it will have already created a constant
21367      pool entry for it.  */
21368   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
21369        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
21370        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
21371       && crtl->uses_const_pool
21372       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
21373     first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
21374   else
21375     first_gp = info_ptr->first_gp_reg_save;
21376 
21377   info_ptr->gp_size = reg_size * (32 - first_gp);
21378 
21379   /* For the SPE, we have an additional upper 32-bits on each GPR.
21380      Ideally we should save the entire 64-bits only when the upper
21381      half is used in SIMD instructions.  Since we only record
21382      registers live (not the size they are used in), this proves
21383      difficult because we'd have to traverse the instruction chain at
21384      the right time, taking reload into account.  This is a real pain,
21385      so we opt to save the GPRs in 64-bits always if but one register
21386      gets used in 64-bits.  Otherwise, all the registers in the frame
21387      get saved in 32-bits.
21388 
21389      So... since when we save all GPRs (except the SP) in 64-bits, the
21390      traditional GP save area will be empty.  */
21391   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21392     info_ptr->gp_size = 0;
21393 
21394   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
21395   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
21396 
21397   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
21398   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
21399 				 - info_ptr->first_altivec_reg_save);
21400 
21401   /* Does this function call anything?  */
21402   info_ptr->calls_p = (! crtl->is_leaf
21403 		       || cfun->machine->ra_needs_full_frame);
21404 
21405   /* Determine if we need to save the condition code registers.  */
21406   if (df_regs_ever_live_p (CR2_REGNO)
21407       || df_regs_ever_live_p (CR3_REGNO)
21408       || df_regs_ever_live_p (CR4_REGNO))
21409     {
21410       info_ptr->cr_save_p = 1;
21411       if (DEFAULT_ABI == ABI_V4)
21412 	info_ptr->cr_size = reg_size;
21413     }
21414 
21415   /* If the current function calls __builtin_eh_return, then we need
21416      to allocate stack space for registers that will hold data for
21417      the exception handler.  */
21418   if (crtl->calls_eh_return)
21419     {
21420       unsigned int i;
21421       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
21422 	continue;
21423 
21424       /* SPE saves EH registers in 64-bits.  */
21425       ehrd_size = i * (TARGET_SPE_ABI
21426 		       && info_ptr->spe_64bit_regs_used != 0
21427 		       ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
21428     }
21429   else
21430     ehrd_size = 0;
21431 
21432   /* In the ELFv2 ABI, we also need to allocate space for separate
21433      CR field save areas if the function calls __builtin_eh_return.  */
21434   if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
21435     {
21436       /* This hard-codes that we have three call-saved CR fields.  */
21437       ehcr_size = 3 * reg_size;
21438       /* We do *not* use the regular CR save mechanism.  */
21439       info_ptr->cr_save_p = 0;
21440     }
21441   else
21442     ehcr_size = 0;
21443 
21444   /* Determine various sizes.  */
21445   info_ptr->reg_size     = reg_size;
21446   info_ptr->fixed_size   = RS6000_SAVE_AREA;
21447   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
21448   info_ptr->parm_size    = RS6000_ALIGN (crtl->outgoing_args_size,
21449 					 TARGET_ALTIVEC ? 16 : 8);
21450   if (FRAME_GROWS_DOWNWARD)
21451     info_ptr->vars_size
21452       += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
21453 		       + info_ptr->parm_size,
21454 		       ABI_STACK_BOUNDARY / BITS_PER_UNIT)
21455 	 - (info_ptr->fixed_size + info_ptr->vars_size
21456 	    + info_ptr->parm_size);
21457 
21458   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21459     info_ptr->spe_gp_size = 8 * (32 - first_gp);
21460   else
21461     info_ptr->spe_gp_size = 0;
21462 
21463   if (TARGET_ALTIVEC_ABI)
21464     info_ptr->vrsave_mask = compute_vrsave_mask ();
21465   else
21466     info_ptr->vrsave_mask = 0;
21467 
21468   if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
21469     info_ptr->vrsave_size  = 4;
21470   else
21471     info_ptr->vrsave_size  = 0;
21472 
21473   compute_save_world_info (info_ptr);
21474 
21475   /* Calculate the offsets.  */
21476   switch (DEFAULT_ABI)
21477     {
21478     case ABI_NONE:
21479     default:
21480       gcc_unreachable ();
21481 
21482     case ABI_AIX:
21483     case ABI_ELFv2:
21484     case ABI_DARWIN:
21485       info_ptr->fp_save_offset   = - info_ptr->fp_size;
21486       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
21487 
21488       if (TARGET_ALTIVEC_ABI)
21489 	{
21490 	  info_ptr->vrsave_save_offset
21491 	    = info_ptr->gp_save_offset - info_ptr->vrsave_size;
21492 
21493 	  /* Align stack so vector save area is on a quadword boundary.
21494 	     The padding goes above the vectors.  */
21495 	  if (info_ptr->altivec_size != 0)
21496 	    info_ptr->altivec_padding_size
21497 	      = info_ptr->vrsave_save_offset & 0xF;
21498 	  else
21499 	    info_ptr->altivec_padding_size = 0;
21500 
21501 	  info_ptr->altivec_save_offset
21502 	    = info_ptr->vrsave_save_offset
21503 	    - info_ptr->altivec_padding_size
21504 	    - info_ptr->altivec_size;
21505 	  gcc_assert (info_ptr->altivec_size == 0
21506 		      || info_ptr->altivec_save_offset % 16 == 0);
21507 
21508 	  /* Adjust for AltiVec case.  */
21509 	  info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
21510 	}
21511       else
21512 	info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
21513 
21514       info_ptr->ehcr_offset      = info_ptr->ehrd_offset - ehcr_size;
21515       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
21516       info_ptr->lr_save_offset   = 2*reg_size;
21517       break;
21518 
21519     case ABI_V4:
21520       info_ptr->fp_save_offset   = - info_ptr->fp_size;
21521       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
21522       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
21523 
21524       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21525 	{
21526 	  /* Align stack so SPE GPR save area is aligned on a
21527 	     double-word boundary.  */
21528 	  if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
21529 	    info_ptr->spe_padding_size
21530 	      = 8 - (-info_ptr->cr_save_offset % 8);
21531 	  else
21532 	    info_ptr->spe_padding_size = 0;
21533 
21534 	  info_ptr->spe_gp_save_offset
21535 	    = info_ptr->cr_save_offset
21536 	    - info_ptr->spe_padding_size
21537 	    - info_ptr->spe_gp_size;
21538 
21539 	  /* Adjust for SPE case.  */
21540 	  info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
21541 	}
21542       else if (TARGET_ALTIVEC_ABI)
21543 	{
21544 	  info_ptr->vrsave_save_offset
21545 	    = info_ptr->cr_save_offset - info_ptr->vrsave_size;
21546 
21547 	  /* Align stack so vector save area is on a quadword boundary.  */
21548 	  if (info_ptr->altivec_size != 0)
21549 	    info_ptr->altivec_padding_size
21550 	      = 16 - (-info_ptr->vrsave_save_offset % 16);
21551 	  else
21552 	    info_ptr->altivec_padding_size = 0;
21553 
21554 	  info_ptr->altivec_save_offset
21555 	    = info_ptr->vrsave_save_offset
21556 	    - info_ptr->altivec_padding_size
21557 	    - info_ptr->altivec_size;
21558 
21559 	  /* Adjust for AltiVec case.  */
21560 	  info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
21561 	}
21562       else
21563 	info_ptr->ehrd_offset    = info_ptr->cr_save_offset;
21564       info_ptr->ehrd_offset      -= ehrd_size;
21565       info_ptr->lr_save_offset   = reg_size;
21566       break;
21567     }
21568 
21569   save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
21570   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
21571 					 + info_ptr->gp_size
21572 					 + info_ptr->altivec_size
21573 					 + info_ptr->altivec_padding_size
21574 					 + info_ptr->spe_gp_size
21575 					 + info_ptr->spe_padding_size
21576 					 + ehrd_size
21577 					 + ehcr_size
21578 					 + info_ptr->cr_size
21579 					 + info_ptr->vrsave_size,
21580 					 save_align);
21581 
21582   non_fixed_size	 = (info_ptr->vars_size
21583 			    + info_ptr->parm_size
21584 			    + info_ptr->save_size);
21585 
21586   info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
21587 				       ABI_STACK_BOUNDARY / BITS_PER_UNIT);
21588 
21589   /* Determine if we need to save the link register.  */
21590   if (info_ptr->calls_p
21591       || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21592 	  && crtl->profile
21593 	  && !TARGET_PROFILE_KERNEL)
21594       || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
21595 #ifdef TARGET_RELOCATABLE
21596       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
21597 #endif
21598       || rs6000_ra_ever_killed ())
21599     info_ptr->lr_save_p = 1;
21600 
21601   using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21602 			  && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21603 			  && call_used_regs[STATIC_CHAIN_REGNUM]);
21604   info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
21605 						      using_static_chain_p);
21606 
21607   if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
21608       || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
21609       || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
21610       || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
21611       || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
21612       || !(info_ptr->savres_strategy & REST_INLINE_VRS))
21613     info_ptr->lr_save_p = 1;
21614 
21615   if (info_ptr->lr_save_p)
21616     df_set_regs_ever_live (LR_REGNO, true);
21617 
21618   /* Determine if we need to allocate any stack frame:
21619 
21620      For AIX we need to push the stack if a frame pointer is needed
21621      (because the stack might be dynamically adjusted), if we are
21622      debugging, if we make calls, or if the sum of fp_save, gp_save,
21623      and local variables are more than the space needed to save all
21624      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
21625      + 18*8 = 288 (GPR13 reserved).
21626 
21627      For V.4 we don't have the stack cushion that AIX uses, but assume
21628      that the debugger can handle stackless frames.  */
21629 
21630   if (info_ptr->calls_p)
21631     info_ptr->push_p = 1;
21632 
21633   else if (DEFAULT_ABI == ABI_V4)
21634     info_ptr->push_p = non_fixed_size != 0;
21635 
21636   else if (frame_pointer_needed)
21637     info_ptr->push_p = 1;
21638 
21639   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
21640     info_ptr->push_p = 1;
21641 
21642   else
21643     info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
21644 
21645   /* Zero offsets if we're not saving those registers.  */
21646   if (info_ptr->fp_size == 0)
21647     info_ptr->fp_save_offset = 0;
21648 
21649   if (info_ptr->gp_size == 0)
21650     info_ptr->gp_save_offset = 0;
21651 
21652   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
21653     info_ptr->altivec_save_offset = 0;
21654 
21655   /* Zero VRSAVE offset if not saved and restored.  */
21656   if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
21657     info_ptr->vrsave_save_offset = 0;
21658 
21659   if (! TARGET_SPE_ABI
21660       || info_ptr->spe_64bit_regs_used == 0
21661       || info_ptr->spe_gp_size == 0)
21662     info_ptr->spe_gp_save_offset = 0;
21663 
21664   if (! info_ptr->lr_save_p)
21665     info_ptr->lr_save_offset = 0;
21666 
21667   if (! info_ptr->cr_save_p)
21668     info_ptr->cr_save_offset = 0;
21669 
21670   return info_ptr;
21671 }
21672 
21673 /* Return true if the current function uses any GPRs in 64-bit SIMD
21674    mode.  */
21675 
21676 static bool
spe_func_has_64bit_regs_p(void)21677 spe_func_has_64bit_regs_p (void)
21678 {
21679   rtx insns, insn;
21680 
21681   /* Functions that save and restore all the call-saved registers will
21682      need to save/restore the registers in 64-bits.  */
21683   if (crtl->calls_eh_return
21684       || cfun->calls_setjmp
21685       || crtl->has_nonlocal_goto)
21686     return true;
21687 
21688   insns = get_insns ();
21689 
21690   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
21691     {
21692       if (INSN_P (insn))
21693 	{
21694 	  rtx i;
21695 
21696 	  /* FIXME: This should be implemented with attributes...
21697 
21698 	         (set_attr "spe64" "true")....then,
21699 	         if (get_spe64(insn)) return true;
21700 
21701 	     It's the only reliable way to do the stuff below.  */
21702 
21703 	  i = PATTERN (insn);
21704 	  if (GET_CODE (i) == SET)
21705 	    {
21706 	      enum machine_mode mode = GET_MODE (SET_SRC (i));
21707 
21708 	      if (SPE_VECTOR_MODE (mode))
21709 		return true;
21710 	      if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
21711 		return true;
21712 	    }
21713 	}
21714     }
21715 
21716   return false;
21717 }
21718 
21719 static void
debug_stack_info(rs6000_stack_t * info)21720 debug_stack_info (rs6000_stack_t *info)
21721 {
21722   const char *abi_string;
21723 
21724   if (! info)
21725     info = rs6000_stack_info ();
21726 
21727   fprintf (stderr, "\nStack information for function %s:\n",
21728 	   ((current_function_decl && DECL_NAME (current_function_decl))
21729 	    ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
21730 	    : "<unknown>"));
21731 
21732   switch (info->abi)
21733     {
21734     default:		 abi_string = "Unknown";	break;
21735     case ABI_NONE:	 abi_string = "NONE";		break;
21736     case ABI_AIX:	 abi_string = "AIX";		break;
21737     case ABI_ELFv2:	 abi_string = "ELFv2";		break;
21738     case ABI_DARWIN:	 abi_string = "Darwin";		break;
21739     case ABI_V4:	 abi_string = "V.4";		break;
21740     }
21741 
21742   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
21743 
21744   if (TARGET_ALTIVEC_ABI)
21745     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
21746 
21747   if (TARGET_SPE_ABI)
21748     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
21749 
21750   if (info->first_gp_reg_save != 32)
21751     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
21752 
21753   if (info->first_fp_reg_save != 64)
21754     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
21755 
21756   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
21757     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
21758 	     info->first_altivec_reg_save);
21759 
21760   if (info->lr_save_p)
21761     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
21762 
21763   if (info->cr_save_p)
21764     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
21765 
21766   if (info->vrsave_mask)
21767     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
21768 
21769   if (info->push_p)
21770     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
21771 
21772   if (info->calls_p)
21773     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
21774 
21775   if (info->gp_save_offset)
21776     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
21777 
21778   if (info->fp_save_offset)
21779     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
21780 
21781   if (info->altivec_save_offset)
21782     fprintf (stderr, "\taltivec_save_offset = %5d\n",
21783 	     info->altivec_save_offset);
21784 
21785   if (info->spe_gp_save_offset)
21786     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
21787 	     info->spe_gp_save_offset);
21788 
21789   if (info->vrsave_save_offset)
21790     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
21791 	     info->vrsave_save_offset);
21792 
21793   if (info->lr_save_offset)
21794     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
21795 
21796   if (info->cr_save_offset)
21797     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
21798 
21799   if (info->varargs_save_offset)
21800     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
21801 
21802   if (info->total_size)
21803     fprintf (stderr, "\ttotal_size          = " HOST_WIDE_INT_PRINT_DEC"\n",
21804 	     info->total_size);
21805 
21806   if (info->vars_size)
21807     fprintf (stderr, "\tvars_size           = " HOST_WIDE_INT_PRINT_DEC"\n",
21808 	     info->vars_size);
21809 
21810   if (info->parm_size)
21811     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
21812 
21813   if (info->fixed_size)
21814     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
21815 
21816   if (info->gp_size)
21817     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
21818 
21819   if (info->spe_gp_size)
21820     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
21821 
21822   if (info->fp_size)
21823     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
21824 
21825   if (info->altivec_size)
21826     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
21827 
21828   if (info->vrsave_size)
21829     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
21830 
21831   if (info->altivec_padding_size)
21832     fprintf (stderr, "\taltivec_padding_size= %5d\n",
21833 	     info->altivec_padding_size);
21834 
21835   if (info->spe_padding_size)
21836     fprintf (stderr, "\tspe_padding_size    = %5d\n",
21837 	     info->spe_padding_size);
21838 
21839   if (info->cr_size)
21840     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
21841 
21842   if (info->save_size)
21843     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
21844 
21845   if (info->reg_size != 4)
21846     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
21847 
21848     fprintf (stderr, "\tsave-strategy       =  %04x\n", info->savres_strategy);
21849 
21850   fprintf (stderr, "\n");
21851 }
21852 
21853 rtx
rs6000_return_addr(int count,rtx frame)21854 rs6000_return_addr (int count, rtx frame)
21855 {
21856   /* Currently we don't optimize very well between prolog and body
21857      code and for PIC code the code can be actually quite bad, so
21858      don't try to be too clever here.  */
21859   if (count != 0
21860       || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
21861     {
21862       cfun->machine->ra_needs_full_frame = 1;
21863 
21864       return
21865 	gen_rtx_MEM
21866 	  (Pmode,
21867 	   memory_address
21868 	   (Pmode,
21869 	    plus_constant (Pmode,
21870 			   copy_to_reg
21871 			   (gen_rtx_MEM (Pmode,
21872 					 memory_address (Pmode, frame))),
21873 			   RETURN_ADDRESS_OFFSET)));
21874     }
21875 
21876   cfun->machine->ra_need_lr = 1;
21877   return get_hard_reg_initial_val (Pmode, LR_REGNO);
21878 }
21879 
21880 /* Say whether a function is a candidate for sibcall handling or not.  */
21881 
21882 static bool
rs6000_function_ok_for_sibcall(tree decl,tree exp)21883 rs6000_function_ok_for_sibcall (tree decl, tree exp)
21884 {
21885   tree fntype;
21886 
21887   if (decl)
21888     fntype = TREE_TYPE (decl);
21889   else
21890     fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
21891 
21892   /* We can't do it if the called function has more vector parameters
21893      than the current function; there's nowhere to put the VRsave code.  */
21894   if (TARGET_ALTIVEC_ABI
21895       && TARGET_ALTIVEC_VRSAVE
21896       && !(decl && decl == current_function_decl))
21897     {
21898       function_args_iterator args_iter;
21899       tree type;
21900       int nvreg = 0;
21901 
21902       /* Functions with vector parameters are required to have a
21903 	 prototype, so the argument type info must be available
21904 	 here.  */
21905       FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
21906 	if (TREE_CODE (type) == VECTOR_TYPE
21907 	    && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21908 	  nvreg++;
21909 
21910       FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
21911 	if (TREE_CODE (type) == VECTOR_TYPE
21912 	    && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21913 	  nvreg--;
21914 
21915       if (nvreg > 0)
21916 	return false;
21917     }
21918 
21919   /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
21920      functions, because the callee may have a different TOC pointer to
21921      the caller and there's no way to ensure we restore the TOC when
21922      we return.  With the secure-plt SYSV ABI we can't make non-local
21923      calls when -fpic/PIC because the plt call stubs use r30.  */
21924   if (DEFAULT_ABI == ABI_DARWIN
21925       || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21926 	  && decl
21927 	  && !DECL_EXTERNAL (decl)
21928 	  && (*targetm.binds_local_p) (decl))
21929       || (DEFAULT_ABI == ABI_V4
21930 	  && (!TARGET_SECURE_PLT
21931 	      || !flag_pic
21932 	      || (decl
21933 		  && (*targetm.binds_local_p) (decl)))))
21934     {
21935       tree attr_list = TYPE_ATTRIBUTES (fntype);
21936 
21937       if (!lookup_attribute ("longcall", attr_list)
21938 	  || lookup_attribute ("shortcall", attr_list))
21939 	return true;
21940     }
21941 
21942   return false;
21943 }
21944 
21945 /* NULL if INSN insn is valid within a low-overhead loop.
21946    Otherwise return why doloop cannot be applied.
21947    PowerPC uses the COUNT register for branch on table instructions.  */
21948 
21949 static const char *
rs6000_invalid_within_doloop(const_rtx insn)21950 rs6000_invalid_within_doloop (const_rtx insn)
21951 {
21952   if (CALL_P (insn))
21953     return "Function call in the loop.";
21954 
21955   if (JUMP_P (insn)
21956       && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
21957 	  || GET_CODE (PATTERN (insn)) == ADDR_VEC))
21958     return "Computed branch in the loop.";
21959 
21960   return NULL;
21961 }
21962 
21963 static int
rs6000_ra_ever_killed(void)21964 rs6000_ra_ever_killed (void)
21965 {
21966   rtx top;
21967   rtx reg;
21968   rtx insn;
21969 
21970   if (cfun->is_thunk)
21971     return 0;
21972 
21973   if (cfun->machine->lr_save_state)
21974     return cfun->machine->lr_save_state - 1;
21975 
21976   /* regs_ever_live has LR marked as used if any sibcalls are present,
21977      but this should not force saving and restoring in the
21978      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
21979      clobbers LR, so that is inappropriate.  */
21980 
21981   /* Also, the prologue can generate a store into LR that
21982      doesn't really count, like this:
21983 
21984         move LR->R0
21985         bcl to set PIC register
21986         move LR->R31
21987         move R0->LR
21988 
21989      When we're called from the epilogue, we need to avoid counting
21990      this as a store.  */
21991 
21992   push_topmost_sequence ();
21993   top = get_insns ();
21994   pop_topmost_sequence ();
21995   reg = gen_rtx_REG (Pmode, LR_REGNO);
21996 
21997   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
21998     {
21999       if (INSN_P (insn))
22000 	{
22001 	  if (CALL_P (insn))
22002 	    {
22003 	      if (!SIBLING_CALL_P (insn))
22004 		return 1;
22005 	    }
22006 	  else if (find_regno_note (insn, REG_INC, LR_REGNO))
22007 	    return 1;
22008 	  else if (set_of (reg, insn) != NULL_RTX
22009 		   && !prologue_epilogue_contains (insn))
22010 	    return 1;
22011     	}
22012     }
22013   return 0;
22014 }
22015 
22016 /* Emit instructions needed to load the TOC register.
22017    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
22018    a constant pool; or for SVR4 -fpic.  */
22019 
22020 void
rs6000_emit_load_toc_table(int fromprolog)22021 rs6000_emit_load_toc_table (int fromprolog)
22022 {
22023   rtx dest;
22024   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
22025 
22026   if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
22027     {
22028       char buf[30];
22029       rtx lab, tmp1, tmp2, got;
22030 
22031       lab = gen_label_rtx ();
22032       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
22033       lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
22034       if (flag_pic == 2)
22035 	got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
22036       else
22037 	got = rs6000_got_sym ();
22038       tmp1 = tmp2 = dest;
22039       if (!fromprolog)
22040 	{
22041 	  tmp1 = gen_reg_rtx (Pmode);
22042 	  tmp2 = gen_reg_rtx (Pmode);
22043 	}
22044       emit_insn (gen_load_toc_v4_PIC_1 (lab));
22045       emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
22046       emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
22047       emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
22048     }
22049   else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
22050     {
22051       emit_insn (gen_load_toc_v4_pic_si ());
22052       emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
22053     }
22054   else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
22055     {
22056       char buf[30];
22057       rtx temp0 = (fromprolog
22058 		   ? gen_rtx_REG (Pmode, 0)
22059 		   : gen_reg_rtx (Pmode));
22060 
22061       if (fromprolog)
22062 	{
22063 	  rtx symF, symL;
22064 
22065 	  ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
22066 	  symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
22067 
22068 	  ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
22069 	  symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
22070 
22071 	  emit_insn (gen_load_toc_v4_PIC_1 (symF));
22072 	  emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
22073 	  emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
22074 	}
22075       else
22076 	{
22077 	  rtx tocsym, lab;
22078 
22079 	  tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
22080 	  lab = gen_label_rtx ();
22081 	  emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
22082 	  emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
22083 	  if (TARGET_LINK_STACK)
22084 	    emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
22085 	  emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
22086 	}
22087       emit_insn (gen_addsi3 (dest, temp0, dest));
22088     }
22089   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
22090     {
22091       /* This is for AIX code running in non-PIC ELF32.  */
22092       char buf[30];
22093       rtx realsym;
22094       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
22095       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
22096 
22097       emit_insn (gen_elf_high (dest, realsym));
22098       emit_insn (gen_elf_low (dest, dest, realsym));
22099     }
22100   else
22101     {
22102       gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
22103 
22104       if (TARGET_32BIT)
22105 	emit_insn (gen_load_toc_aix_si (dest));
22106       else
22107 	emit_insn (gen_load_toc_aix_di (dest));
22108     }
22109 }
22110 
22111 /* Emit instructions to restore the link register after determining where
22112    its value has been stored.  */
22113 
22114 void
rs6000_emit_eh_reg_restore(rtx source,rtx scratch)22115 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
22116 {
22117   rs6000_stack_t *info = rs6000_stack_info ();
22118   rtx operands[2];
22119 
22120   operands[0] = source;
22121   operands[1] = scratch;
22122 
22123   if (info->lr_save_p)
22124     {
22125       rtx frame_rtx = stack_pointer_rtx;
22126       HOST_WIDE_INT sp_offset = 0;
22127       rtx tmp;
22128 
22129       if (frame_pointer_needed
22130 	  || cfun->calls_alloca
22131 	  || info->total_size > 32767)
22132 	{
22133 	  tmp = gen_frame_mem (Pmode, frame_rtx);
22134 	  emit_move_insn (operands[1], tmp);
22135 	  frame_rtx = operands[1];
22136 	}
22137       else if (info->push_p)
22138 	sp_offset = info->total_size;
22139 
22140       tmp = plus_constant (Pmode, frame_rtx,
22141 			   info->lr_save_offset + sp_offset);
22142       tmp = gen_frame_mem (Pmode, tmp);
22143       emit_move_insn (tmp, operands[0]);
22144     }
22145   else
22146     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
22147 
22148   /* Freeze lr_save_p.  We've just emitted rtl that depends on the
22149      state of lr_save_p so any change from here on would be a bug.  In
22150      particular, stop rs6000_ra_ever_killed from considering the SET
22151      of lr we may have added just above.  */
22152   cfun->machine->lr_save_state = info->lr_save_p + 1;
22153 }
22154 
22155 static GTY(()) alias_set_type set = -1;
22156 
22157 alias_set_type
get_TOC_alias_set(void)22158 get_TOC_alias_set (void)
22159 {
22160   if (set == -1)
22161     set = new_alias_set ();
22162   return set;
22163 }
22164 
22165 /* This returns nonzero if the current function uses the TOC.  This is
22166    determined by the presence of (use (unspec ... UNSPEC_TOC)), which
22167    is generated by the ABI_V4 load_toc_* patterns.  */
22168 #if TARGET_ELF
22169 static int
uses_TOC(void)22170 uses_TOC (void)
22171 {
22172   rtx insn;
22173 
22174   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
22175     if (INSN_P (insn))
22176       {
22177 	rtx pat = PATTERN (insn);
22178 	int i;
22179 
22180 	if (GET_CODE (pat) == PARALLEL)
22181 	  for (i = 0; i < XVECLEN (pat, 0); i++)
22182 	    {
22183 	      rtx sub = XVECEXP (pat, 0, i);
22184 	      if (GET_CODE (sub) == USE)
22185 		{
22186 		  sub = XEXP (sub, 0);
22187 		  if (GET_CODE (sub) == UNSPEC
22188 		      && XINT (sub, 1) == UNSPEC_TOC)
22189 		    return 1;
22190 		}
22191 	    }
22192       }
22193   return 0;
22194 }
22195 #endif
22196 
22197 rtx
create_TOC_reference(rtx symbol,rtx largetoc_reg)22198 create_TOC_reference (rtx symbol, rtx largetoc_reg)
22199 {
22200   rtx tocrel, tocreg, hi;
22201 
22202   if (TARGET_DEBUG_ADDR)
22203     {
22204       if (GET_CODE (symbol) == SYMBOL_REF)
22205 	fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
22206 		 XSTR (symbol, 0));
22207       else
22208 	{
22209 	  fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
22210 		   GET_RTX_NAME (GET_CODE (symbol)));
22211 	  debug_rtx (symbol);
22212 	}
22213     }
22214 
22215   if (!can_create_pseudo_p ())
22216     df_set_regs_ever_live (TOC_REGISTER, true);
22217 
22218   tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
22219   tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
22220   if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
22221     return tocrel;
22222 
22223   hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
22224   if (largetoc_reg != NULL)
22225     {
22226       emit_move_insn (largetoc_reg, hi);
22227       hi = largetoc_reg;
22228     }
22229   return gen_rtx_LO_SUM (Pmode, hi, tocrel);
22230 }
22231 
22232 /* Issue assembly directives that create a reference to the given DWARF
22233    FRAME_TABLE_LABEL from the current function section.  */
22234 void
rs6000_aix_asm_output_dwarf_table_ref(char * frame_table_label)22235 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
22236 {
22237   fprintf (asm_out_file, "\t.ref %s\n",
22238 	   (* targetm.strip_name_encoding) (frame_table_label));
22239 }
22240 
22241 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
22242    and the change to the stack pointer.  */
22243 
22244 static void
rs6000_emit_stack_tie(rtx fp,bool hard_frame_needed)22245 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
22246 {
22247   rtvec p;
22248   int i;
22249   rtx regs[3];
22250 
22251   i = 0;
22252   regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22253   if (hard_frame_needed)
22254     regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
22255   if (!(REGNO (fp) == STACK_POINTER_REGNUM
22256 	|| (hard_frame_needed
22257 	    && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
22258     regs[i++] = fp;
22259 
22260   p = rtvec_alloc (i);
22261   while (--i >= 0)
22262     {
22263       rtx mem = gen_frame_mem (BLKmode, regs[i]);
22264       RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
22265     }
22266 
22267   emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
22268 }
22269 
22270 /* Emit the correct code for allocating stack space, as insns.
22271    If COPY_REG, make sure a copy of the old frame is left there.
22272    The generated code may use hard register 0 as a temporary.  */
22273 
22274 static void
rs6000_emit_allocate_stack(HOST_WIDE_INT size,rtx copy_reg,int copy_off)22275 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
22276 {
22277   rtx insn;
22278   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22279   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
22280   rtx todec = gen_int_mode (-size, Pmode);
22281   rtx par, set, mem;
22282 
22283   if (INTVAL (todec) != -size)
22284     {
22285       warning (0, "stack frame too large");
22286       emit_insn (gen_trap ());
22287       return;
22288     }
22289 
22290   if (crtl->limit_stack)
22291     {
22292       if (REG_P (stack_limit_rtx)
22293 	  && REGNO (stack_limit_rtx) > 1
22294 	  && REGNO (stack_limit_rtx) <= 31)
22295 	{
22296 	  emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
22297 	  emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
22298 				    const0_rtx));
22299 	}
22300       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
22301 	       && TARGET_32BIT
22302 	       && DEFAULT_ABI == ABI_V4)
22303 	{
22304 	  rtx toload = gen_rtx_CONST (VOIDmode,
22305 				      gen_rtx_PLUS (Pmode,
22306 						    stack_limit_rtx,
22307 						    GEN_INT (size)));
22308 
22309 	  emit_insn (gen_elf_high (tmp_reg, toload));
22310 	  emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
22311 	  emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
22312 				    const0_rtx));
22313 	}
22314       else
22315 	warning (0, "stack limit expression is not supported");
22316     }
22317 
22318   if (copy_reg)
22319     {
22320       if (copy_off != 0)
22321 	emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
22322       else
22323 	emit_move_insn (copy_reg, stack_reg);
22324     }
22325 
22326   if (size > 32767)
22327     {
22328       /* Need a note here so that try_split doesn't get confused.  */
22329       if (get_last_insn () == NULL_RTX)
22330 	emit_note (NOTE_INSN_DELETED);
22331       insn = emit_move_insn (tmp_reg, todec);
22332       try_split (PATTERN (insn), insn, 0);
22333       todec = tmp_reg;
22334     }
22335 
22336   insn = emit_insn (TARGET_32BIT
22337 		    ? gen_movsi_update_stack (stack_reg, stack_reg,
22338 					todec, stack_reg)
22339 		    : gen_movdi_di_update_stack (stack_reg, stack_reg,
22340 					   todec, stack_reg));
22341   /* Since we didn't use gen_frame_mem to generate the MEM, grab
22342      it now and set the alias set/attributes. The above gen_*_update
22343      calls will generate a PARALLEL with the MEM set being the first
22344      operation. */
22345   par = PATTERN (insn);
22346   gcc_assert (GET_CODE (par) == PARALLEL);
22347   set = XVECEXP (par, 0, 0);
22348   gcc_assert (GET_CODE (set) == SET);
22349   mem = SET_DEST (set);
22350   gcc_assert (MEM_P (mem));
22351   MEM_NOTRAP_P (mem) = 1;
22352   set_mem_alias_set (mem, get_frame_alias_set ());
22353 
22354   RTX_FRAME_RELATED_P (insn) = 1;
22355   add_reg_note (insn, REG_FRAME_RELATED_EXPR,
22356 		gen_rtx_SET (VOIDmode, stack_reg,
22357 			     gen_rtx_PLUS (Pmode, stack_reg,
22358 					   GEN_INT (-size))));
22359 }
22360 
22361 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
22362 
22363 #if PROBE_INTERVAL > 32768
22364 #error Cannot use indexed addressing mode for stack probing
22365 #endif
22366 
22367 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
22368    inclusive.  These are offsets from the current stack pointer.  */
22369 
22370 static void
rs6000_emit_probe_stack_range(HOST_WIDE_INT first,HOST_WIDE_INT size)22371 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
22372 {
22373   /* See if we have a constant small number of probes to generate.  If so,
22374      that's the easy case.  */
22375   if (first + size <= 32768)
22376     {
22377       HOST_WIDE_INT i;
22378 
22379       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
22380 	 it exceeds SIZE.  If only one probe is needed, this will not
22381 	 generate any code.  Then probe at FIRST + SIZE.  */
22382       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
22383 	emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
22384 					 -(first + i)));
22385 
22386       emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
22387 				       -(first + size)));
22388     }
22389 
22390   /* Otherwise, do the same as above, but in a loop.  Note that we must be
22391      extra careful with variables wrapping around because we might be at
22392      the very top (or the very bottom) of the address space and we have
22393      to be able to handle this case properly; in particular, we use an
22394      equality test for the loop condition.  */
22395   else
22396     {
22397       HOST_WIDE_INT rounded_size;
22398       rtx r12 = gen_rtx_REG (Pmode, 12);
22399       rtx r0 = gen_rtx_REG (Pmode, 0);
22400 
22401       /* Sanity check for the addressing mode we're going to use.  */
22402       gcc_assert (first <= 32768);
22403 
22404       /* Step 1: round SIZE to the previous multiple of the interval.  */
22405 
22406       rounded_size = size & -PROBE_INTERVAL;
22407 
22408 
22409       /* Step 2: compute initial and final value of the loop counter.  */
22410 
22411       /* TEST_ADDR = SP + FIRST.  */
22412       emit_insn (gen_rtx_SET (VOIDmode, r12,
22413 			      plus_constant (Pmode, stack_pointer_rtx,
22414 					     -first)));
22415 
22416       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
22417       if (rounded_size > 32768)
22418 	{
22419 	  emit_move_insn (r0, GEN_INT (-rounded_size));
22420 	  emit_insn (gen_rtx_SET (VOIDmode, r0,
22421 				  gen_rtx_PLUS (Pmode, r12, r0)));
22422 	}
22423       else
22424 	emit_insn (gen_rtx_SET (VOIDmode, r0,
22425 			        plus_constant (Pmode, r12, -rounded_size)));
22426 
22427 
22428       /* Step 3: the loop
22429 
22430 	 while (TEST_ADDR != LAST_ADDR)
22431 	   {
22432 	     TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
22433 	     probe at TEST_ADDR
22434 	   }
22435 
22436 	 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
22437 	 until it is equal to ROUNDED_SIZE.  */
22438 
22439       if (TARGET_64BIT)
22440 	emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
22441       else
22442 	emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
22443 
22444 
22445       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
22446 	 that SIZE is equal to ROUNDED_SIZE.  */
22447 
22448       if (size != rounded_size)
22449 	emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
22450     }
22451 }
22452 
22453 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
22454    absolute addresses.  */
22455 
22456 const char *
output_probe_stack_range(rtx reg1,rtx reg2)22457 output_probe_stack_range (rtx reg1, rtx reg2)
22458 {
22459   static int labelno = 0;
22460   char loop_lab[32], end_lab[32];
22461   rtx xops[2];
22462 
22463   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
22464   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
22465 
22466   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
22467 
22468   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
22469   xops[0] = reg1;
22470   xops[1] = reg2;
22471   if (TARGET_64BIT)
22472     output_asm_insn ("cmpd 0,%0,%1", xops);
22473   else
22474     output_asm_insn ("cmpw 0,%0,%1", xops);
22475 
22476   fputs ("\tbeq 0,", asm_out_file);
22477   assemble_name_raw (asm_out_file, end_lab);
22478   fputc ('\n', asm_out_file);
22479 
22480   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
22481   xops[1] = GEN_INT (-PROBE_INTERVAL);
22482   output_asm_insn ("addi %0,%0,%1", xops);
22483 
22484   /* Probe at TEST_ADDR and branch.  */
22485   xops[1] = gen_rtx_REG (Pmode, 0);
22486   output_asm_insn ("stw %1,0(%0)", xops);
22487   fprintf (asm_out_file, "\tb ");
22488   assemble_name_raw (asm_out_file, loop_lab);
22489   fputc ('\n', asm_out_file);
22490 
22491   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
22492 
22493   return "";
22494 }
22495 
22496 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
22497    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
22498    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
22499    deduce these equivalences by itself so it wasn't necessary to hold
22500    its hand so much.  Don't be tempted to always supply d2_f_d_e with
22501    the actual cfa register, ie. r31 when we are using a hard frame
22502    pointer.  That fails when saving regs off r1, and sched moves the
22503    r31 setup past the reg saves.  */
22504 
22505 static rtx
rs6000_frame_related(rtx insn,rtx reg,HOST_WIDE_INT val,rtx reg2,rtx rreg)22506 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
22507 		      rtx reg2, rtx rreg)
22508 {
22509   rtx real, temp;
22510 
22511   if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
22512     {
22513       /* No need for any replacement.  Just set RTX_FRAME_RELATED_P.  */
22514       int i;
22515 
22516       gcc_checking_assert (val == 0);
22517       real = PATTERN (insn);
22518       if (GET_CODE (real) == PARALLEL)
22519 	for (i = 0; i < XVECLEN (real, 0); i++)
22520 	  if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22521 	    {
22522 	      rtx set = XVECEXP (real, 0, i);
22523 
22524 	      RTX_FRAME_RELATED_P (set) = 1;
22525 	    }
22526       RTX_FRAME_RELATED_P (insn) = 1;
22527       return insn;
22528     }
22529 
22530   /* copy_rtx will not make unique copies of registers, so we need to
22531      ensure we don't have unwanted sharing here.  */
22532   if (reg == reg2)
22533     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22534 
22535   if (reg == rreg)
22536     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22537 
22538   real = copy_rtx (PATTERN (insn));
22539 
22540   if (reg2 != NULL_RTX)
22541     real = replace_rtx (real, reg2, rreg);
22542 
22543   if (REGNO (reg) == STACK_POINTER_REGNUM)
22544     gcc_checking_assert (val == 0);
22545   else
22546     real = replace_rtx (real, reg,
22547 			gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
22548 							  STACK_POINTER_REGNUM),
22549 				      GEN_INT (val)));
22550 
22551   /* We expect that 'real' is either a SET or a PARALLEL containing
22552      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
22553      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
22554 
22555   if (GET_CODE (real) == SET)
22556     {
22557       rtx set = real;
22558 
22559       temp = simplify_rtx (SET_SRC (set));
22560       if (temp)
22561 	SET_SRC (set) = temp;
22562       temp = simplify_rtx (SET_DEST (set));
22563       if (temp)
22564 	SET_DEST (set) = temp;
22565       if (GET_CODE (SET_DEST (set)) == MEM)
22566 	{
22567 	  temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22568 	  if (temp)
22569 	    XEXP (SET_DEST (set), 0) = temp;
22570 	}
22571     }
22572   else
22573     {
22574       int i;
22575 
22576       gcc_assert (GET_CODE (real) == PARALLEL);
22577       for (i = 0; i < XVECLEN (real, 0); i++)
22578 	if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22579 	  {
22580 	    rtx set = XVECEXP (real, 0, i);
22581 
22582 	    temp = simplify_rtx (SET_SRC (set));
22583 	    if (temp)
22584 	      SET_SRC (set) = temp;
22585 	    temp = simplify_rtx (SET_DEST (set));
22586 	    if (temp)
22587 	      SET_DEST (set) = temp;
22588 	    if (GET_CODE (SET_DEST (set)) == MEM)
22589 	      {
22590 		temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22591 		if (temp)
22592 		  XEXP (SET_DEST (set), 0) = temp;
22593 	      }
22594 	    RTX_FRAME_RELATED_P (set) = 1;
22595 	  }
22596     }
22597 
22598   RTX_FRAME_RELATED_P (insn) = 1;
22599   add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
22600 
22601   return insn;
22602 }
22603 
22604 /* Returns an insn that has a vrsave set operation with the
22605    appropriate CLOBBERs.  */
22606 
22607 static rtx
generate_set_vrsave(rtx reg,rs6000_stack_t * info,int epiloguep)22608 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
22609 {
22610   int nclobs, i;
22611   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
22612   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
22613 
22614   clobs[0]
22615     = gen_rtx_SET (VOIDmode,
22616 		   vrsave,
22617 		   gen_rtx_UNSPEC_VOLATILE (SImode,
22618 					    gen_rtvec (2, reg, vrsave),
22619 					    UNSPECV_SET_VRSAVE));
22620 
22621   nclobs = 1;
22622 
22623   /* We need to clobber the registers in the mask so the scheduler
22624      does not move sets to VRSAVE before sets of AltiVec registers.
22625 
22626      However, if the function receives nonlocal gotos, reload will set
22627      all call saved registers live.  We will end up with:
22628 
22629      	(set (reg 999) (mem))
22630 	(parallel [ (set (reg vrsave) (unspec blah))
22631 		    (clobber (reg 999))])
22632 
22633      The clobber will cause the store into reg 999 to be dead, and
22634      flow will attempt to delete an epilogue insn.  In this case, we
22635      need an unspec use/set of the register.  */
22636 
22637   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
22638     if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22639       {
22640 	if (!epiloguep || call_used_regs [i])
22641 	  clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
22642 					     gen_rtx_REG (V4SImode, i));
22643 	else
22644 	  {
22645 	    rtx reg = gen_rtx_REG (V4SImode, i);
22646 
22647 	    clobs[nclobs++]
22648 	      = gen_rtx_SET (VOIDmode,
22649 			     reg,
22650 			     gen_rtx_UNSPEC (V4SImode,
22651 					     gen_rtvec (1, reg), 27));
22652 	  }
22653       }
22654 
22655   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
22656 
22657   for (i = 0; i < nclobs; ++i)
22658     XVECEXP (insn, 0, i) = clobs[i];
22659 
22660   return insn;
22661 }
22662 
22663 static rtx
gen_frame_set(rtx reg,rtx frame_reg,int offset,bool store)22664 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
22665 {
22666   rtx addr, mem;
22667 
22668   addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
22669   mem = gen_frame_mem (GET_MODE (reg), addr);
22670   return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
22671 }
22672 
22673 static rtx
gen_frame_load(rtx reg,rtx frame_reg,int offset)22674 gen_frame_load (rtx reg, rtx frame_reg, int offset)
22675 {
22676   return gen_frame_set (reg, frame_reg, offset, false);
22677 }
22678 
22679 static rtx
gen_frame_store(rtx reg,rtx frame_reg,int offset)22680 gen_frame_store (rtx reg, rtx frame_reg, int offset)
22681 {
22682   return gen_frame_set (reg, frame_reg, offset, true);
22683 }
22684 
22685 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
22686    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
22687 
22688 static rtx
emit_frame_save(rtx frame_reg,enum machine_mode mode,unsigned int regno,int offset,HOST_WIDE_INT frame_reg_to_sp)22689 emit_frame_save (rtx frame_reg, enum machine_mode mode,
22690 		 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
22691 {
22692   rtx reg, insn;
22693 
22694   /* Some cases that need register indexed addressing.  */
22695   gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
22696 			 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
22697 			 || (TARGET_E500_DOUBLE && mode == DFmode)
22698 			 || (TARGET_SPE_ABI
22699 			     && SPE_VECTOR_MODE (mode)
22700 			     && !SPE_CONST_OFFSET_OK (offset))));
22701 
22702   reg = gen_rtx_REG (mode, regno);
22703   insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
22704   return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
22705 			       NULL_RTX, NULL_RTX);
22706 }
22707 
22708 /* Emit an offset memory reference suitable for a frame store, while
22709    converting to a valid addressing mode.  */
22710 
22711 static rtx
gen_frame_mem_offset(enum machine_mode mode,rtx reg,int offset)22712 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
22713 {
22714   rtx int_rtx, offset_rtx;
22715 
22716   int_rtx = GEN_INT (offset);
22717 
22718   if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
22719       || (TARGET_E500_DOUBLE && mode == DFmode))
22720     {
22721       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
22722       emit_move_insn (offset_rtx, int_rtx);
22723     }
22724   else
22725     offset_rtx = int_rtx;
22726 
22727   return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
22728 }
22729 
22730 #ifndef TARGET_FIX_AND_CONTINUE
22731 #define TARGET_FIX_AND_CONTINUE 0
22732 #endif
22733 
22734 /* It's really GPR 13 or 14, FPR 14 and VR 20.  We need the smallest.  */
22735 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
22736 #define LAST_SAVRES_REGISTER 31
22737 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
22738 
22739 enum {
22740   SAVRES_LR = 0x1,
22741   SAVRES_SAVE = 0x2,
22742   SAVRES_REG = 0x0c,
22743   SAVRES_GPR = 0,
22744   SAVRES_FPR = 4,
22745   SAVRES_VR  = 8
22746 };
22747 
22748 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
22749 
22750 /* Temporary holding space for an out-of-line register save/restore
22751    routine name.  */
22752 static char savres_routine_name[30];
22753 
22754 /* Return the name for an out-of-line register save/restore routine.
22755    We are saving/restoring GPRs if GPR is true.  */
22756 
22757 static char *
rs6000_savres_routine_name(rs6000_stack_t * info,int regno,int sel)22758 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
22759 {
22760   const char *prefix = "";
22761   const char *suffix = "";
22762 
22763   /* Different targets are supposed to define
22764      {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
22765      routine name could be defined with:
22766 
22767      sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
22768 
22769      This is a nice idea in practice, but in reality, things are
22770      complicated in several ways:
22771 
22772      - ELF targets have save/restore routines for GPRs.
22773 
22774      - SPE targets use different prefixes for 32/64-bit registers, and
22775        neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
22776 
22777      - PPC64 ELF targets have routines for save/restore of GPRs that
22778        differ in what they do with the link register, so having a set
22779        prefix doesn't work.  (We only use one of the save routines at
22780        the moment, though.)
22781 
22782      - PPC32 elf targets have "exit" versions of the restore routines
22783        that restore the link register and can save some extra space.
22784        These require an extra suffix.  (There are also "tail" versions
22785        of the restore routines and "GOT" versions of the save routines,
22786        but we don't generate those at present.  Same problems apply,
22787        though.)
22788 
22789      We deal with all this by synthesizing our own prefix/suffix and
22790      using that for the simple sprintf call shown above.  */
22791   if (TARGET_SPE)
22792     {
22793       /* No floating point saves on the SPE.  */
22794       gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
22795 
22796       if ((sel & SAVRES_SAVE))
22797 	prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
22798       else
22799 	prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
22800 
22801       if ((sel & SAVRES_LR))
22802 	suffix = "_x";
22803     }
22804   else if (DEFAULT_ABI == ABI_V4)
22805     {
22806       if (TARGET_64BIT)
22807 	goto aix_names;
22808 
22809       if ((sel & SAVRES_REG) == SAVRES_GPR)
22810 	prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
22811       else if ((sel & SAVRES_REG) == SAVRES_FPR)
22812 	prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
22813       else if ((sel & SAVRES_REG) == SAVRES_VR)
22814 	prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22815       else
22816 	abort ();
22817 
22818       if ((sel & SAVRES_LR))
22819 	suffix = "_x";
22820     }
22821   else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22822     {
22823 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
22824       /* No out-of-line save/restore routines for GPRs on AIX.  */
22825       gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
22826 #endif
22827 
22828     aix_names:
22829       if ((sel & SAVRES_REG) == SAVRES_GPR)
22830 	prefix = ((sel & SAVRES_SAVE)
22831 		  ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
22832 		  : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
22833       else if ((sel & SAVRES_REG) == SAVRES_FPR)
22834 	{
22835 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
22836 	  if ((sel & SAVRES_LR))
22837 	    prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
22838 	  else
22839 #endif
22840 	    {
22841 	      prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
22842 	      suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
22843 	    }
22844 	}
22845       else if ((sel & SAVRES_REG) == SAVRES_VR)
22846 	prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22847       else
22848 	abort ();
22849     }
22850 
22851    if (DEFAULT_ABI == ABI_DARWIN)
22852     {
22853       /* The Darwin approach is (slightly) different, in order to be
22854 	 compatible with code generated by the system toolchain.  There is a
22855 	 single symbol for the start of save sequence, and the code here
22856 	 embeds an offset into that code on the basis of the first register
22857 	 to be saved.  */
22858       prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
22859       if ((sel & SAVRES_REG) == SAVRES_GPR)
22860 	sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
22861 		 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
22862 		 (regno - 13) * 4, prefix, regno);
22863       else if ((sel & SAVRES_REG) == SAVRES_FPR)
22864 	sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
22865 		 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
22866       else if ((sel & SAVRES_REG) == SAVRES_VR)
22867 	sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
22868 		 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
22869       else
22870 	abort ();
22871     }
22872   else
22873     sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
22874 
22875   return savres_routine_name;
22876 }
22877 
22878 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
22879    We are saving/restoring GPRs if GPR is true.  */
22880 
22881 static rtx
rs6000_savres_routine_sym(rs6000_stack_t * info,int sel)22882 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
22883 {
22884   int regno = ((sel & SAVRES_REG) == SAVRES_GPR
22885 	       ? info->first_gp_reg_save
22886 	       : (sel & SAVRES_REG) == SAVRES_FPR
22887 	       ? info->first_fp_reg_save - 32
22888 	       : (sel & SAVRES_REG) == SAVRES_VR
22889 	       ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
22890 	       : -1);
22891   rtx sym;
22892   int select = sel;
22893 
22894   /* On the SPE, we never have any FPRs, but we do have 32/64-bit
22895      versions of the gpr routines.  */
22896   if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
22897       && info->spe_64bit_regs_used)
22898     select ^= SAVRES_FPR ^ SAVRES_GPR;
22899 
22900   /* Don't generate bogus routine names.  */
22901   gcc_assert (FIRST_SAVRES_REGISTER <= regno
22902 	      && regno <= LAST_SAVRES_REGISTER
22903 	      && select >= 0 && select <= 12);
22904 
22905   sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
22906 
22907   if (sym == NULL)
22908     {
22909       char *name;
22910 
22911       name = rs6000_savres_routine_name (info, regno, sel);
22912 
22913       sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
22914 	= gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
22915       SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
22916     }
22917 
22918   return sym;
22919 }
22920 
22921 /* Emit a sequence of insns, including a stack tie if needed, for
22922    resetting the stack pointer.  If UPDT_REGNO is not 1, then don't
22923    reset the stack pointer, but move the base of the frame into
22924    reg UPDT_REGNO for use by out-of-line register restore routines.  */
22925 
22926 static rtx
rs6000_emit_stack_reset(rs6000_stack_t * info,rtx frame_reg_rtx,HOST_WIDE_INT frame_off,unsigned updt_regno)22927 rs6000_emit_stack_reset (rs6000_stack_t *info,
22928 			 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
22929 			 unsigned updt_regno)
22930 {
22931   rtx updt_reg_rtx;
22932 
22933   /* This blockage is needed so that sched doesn't decide to move
22934      the sp change before the register restores.  */
22935   if (DEFAULT_ABI == ABI_V4
22936       || (TARGET_SPE_ABI
22937 	  && info->spe_64bit_regs_used != 0
22938 	  && info->first_gp_reg_save != 32))
22939     rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
22940 
22941   /* If we are restoring registers out-of-line, we will be using the
22942      "exit" variants of the restore routines, which will reset the
22943      stack for us.  But we do need to point updt_reg into the
22944      right place for those routines.  */
22945   updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
22946 
22947   if (frame_off != 0)
22948     return emit_insn (gen_add3_insn (updt_reg_rtx,
22949 				     frame_reg_rtx, GEN_INT (frame_off)));
22950   else if (REGNO (frame_reg_rtx) != updt_regno)
22951     return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
22952 
22953   return NULL_RTX;
22954 }
22955 
22956 /* Return the register number used as a pointer by out-of-line
22957    save/restore functions.  */
22958 
22959 static inline unsigned
ptr_regno_for_savres(int sel)22960 ptr_regno_for_savres (int sel)
22961 {
22962   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22963     return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
22964   return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
22965 }
22966 
22967 /* Construct a parallel rtx describing the effect of a call to an
22968    out-of-line register save/restore routine, and emit the insn
22969    or jump_insn as appropriate.  */
22970 
22971 static rtx
rs6000_emit_savres_rtx(rs6000_stack_t * info,rtx frame_reg_rtx,int save_area_offset,int lr_offset,enum machine_mode reg_mode,int sel)22972 rs6000_emit_savres_rtx (rs6000_stack_t *info,
22973 			rtx frame_reg_rtx, int save_area_offset, int lr_offset,
22974 			enum machine_mode reg_mode, int sel)
22975 {
22976   int i;
22977   int offset, start_reg, end_reg, n_regs, use_reg;
22978   int reg_size = GET_MODE_SIZE (reg_mode);
22979   rtx sym;
22980   rtvec p;
22981   rtx par, insn;
22982 
22983   offset = 0;
22984   start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22985 	       ? info->first_gp_reg_save
22986 	       : (sel & SAVRES_REG) == SAVRES_FPR
22987 	       ? info->first_fp_reg_save
22988 	       : (sel & SAVRES_REG) == SAVRES_VR
22989 	       ? info->first_altivec_reg_save
22990 	       : -1);
22991   end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22992 	     ? 32
22993 	     : (sel & SAVRES_REG) == SAVRES_FPR
22994 	     ? 64
22995 	     : (sel & SAVRES_REG) == SAVRES_VR
22996 	     ? LAST_ALTIVEC_REGNO + 1
22997 	     : -1);
22998   n_regs = end_reg - start_reg;
22999   p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
23000 		   + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
23001 		   + n_regs);
23002 
23003   if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
23004     RTVEC_ELT (p, offset++) = ret_rtx;
23005 
23006   RTVEC_ELT (p, offset++)
23007     = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
23008 
23009   sym = rs6000_savres_routine_sym (info, sel);
23010   RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
23011 
23012   use_reg = ptr_regno_for_savres (sel);
23013   if ((sel & SAVRES_REG) == SAVRES_VR)
23014     {
23015       /* Vector regs are saved/restored using [reg+reg] addressing.  */
23016       RTVEC_ELT (p, offset++)
23017 	= gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
23018       RTVEC_ELT (p, offset++)
23019 	= gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
23020     }
23021   else
23022     RTVEC_ELT (p, offset++)
23023       = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
23024 
23025   for (i = 0; i < end_reg - start_reg; i++)
23026     RTVEC_ELT (p, i + offset)
23027       = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
23028 		       frame_reg_rtx, save_area_offset + reg_size * i,
23029 		       (sel & SAVRES_SAVE) != 0);
23030 
23031   if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
23032     RTVEC_ELT (p, i + offset)
23033       = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
23034 
23035   par = gen_rtx_PARALLEL (VOIDmode, p);
23036 
23037   if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
23038     {
23039       insn = emit_jump_insn (par);
23040       JUMP_LABEL (insn) = ret_rtx;
23041     }
23042   else
23043     insn = emit_insn (par);
23044   return insn;
23045 }
23046 
23047 /* Emit code to store CR fields that need to be saved into REG.  */
23048 
23049 static void
rs6000_emit_move_from_cr(rtx reg)23050 rs6000_emit_move_from_cr (rtx reg)
23051 {
23052   /* Only the ELFv2 ABI allows storing only selected fields.  */
23053   if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
23054     {
23055       int i, cr_reg[8], count = 0;
23056 
23057       /* Collect CR fields that must be saved.  */
23058       for (i = 0; i < 8; i++)
23059 	if (save_reg_p (CR0_REGNO + i))
23060 	  cr_reg[count++] = i;
23061 
23062       /* If it's just a single one, use mfcrf.  */
23063       if (count == 1)
23064 	{
23065 	  rtvec p = rtvec_alloc (1);
23066 	  rtvec r = rtvec_alloc (2);
23067 	  RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
23068 	  RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
23069 	  RTVEC_ELT (p, 0)
23070 	    = gen_rtx_SET (VOIDmode, reg,
23071 			   gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
23072 
23073 	  emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23074 	  return;
23075 	}
23076 
23077       /* ??? It might be better to handle count == 2 / 3 cases here
23078 	 as well, using logical operations to combine the values.  */
23079     }
23080 
23081   emit_insn (gen_movesi_from_cr (reg));
23082 }
23083 
23084 /* Determine whether the gp REG is really used.  */
23085 
23086 static bool
rs6000_reg_live_or_pic_offset_p(int reg)23087 rs6000_reg_live_or_pic_offset_p (int reg)
23088 {
23089   /* If the function calls eh_return, claim used all the registers that would
23090      be checked for liveness otherwise.  This is required for the PIC offset
23091      register with -mminimal-toc on AIX, as it is advertised as "fixed" for
23092      register allocation purposes in this case.  */
23093 
23094   return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
23095            && (!call_used_regs[reg]
23096                || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
23097 		   && !TARGET_SINGLE_PIC_BASE
23098                    && TARGET_TOC && TARGET_MINIMAL_TOC)))
23099           || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
23100 	      && !TARGET_SINGLE_PIC_BASE
23101               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
23102                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
23103 }
23104 
23105 /* Emit function prologue as insns.  */
23106 
23107 void
rs6000_emit_prologue(void)23108 rs6000_emit_prologue (void)
23109 {
23110   rs6000_stack_t *info = rs6000_stack_info ();
23111   enum machine_mode reg_mode = Pmode;
23112   int reg_size = TARGET_32BIT ? 4 : 8;
23113   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
23114   rtx frame_reg_rtx = sp_reg_rtx;
23115   unsigned int cr_save_regno;
23116   rtx cr_save_rtx = NULL_RTX;
23117   rtx insn;
23118   int strategy;
23119   int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
23120 			      && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
23121 			      && call_used_regs[STATIC_CHAIN_REGNUM]);
23122   /* Offset to top of frame for frame_reg and sp respectively.  */
23123   HOST_WIDE_INT frame_off = 0;
23124   HOST_WIDE_INT sp_off = 0;
23125 
23126 #ifdef ENABLE_CHECKING
23127   /* Track and check usage of r0, r11, r12.  */
23128   int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
23129 #define START_USE(R) do \
23130   {						\
23131     gcc_assert ((reg_inuse & (1 << (R))) == 0);	\
23132     reg_inuse |= 1 << (R);			\
23133   } while (0)
23134 #define END_USE(R) do \
23135   {						\
23136     gcc_assert ((reg_inuse & (1 << (R))) != 0);	\
23137     reg_inuse &= ~(1 << (R));			\
23138   } while (0)
23139 #define NOT_INUSE(R) do \
23140   {						\
23141     gcc_assert ((reg_inuse & (1 << (R))) == 0);	\
23142   } while (0)
23143 #else
23144 #define START_USE(R) do {} while (0)
23145 #define END_USE(R) do {} while (0)
23146 #define NOT_INUSE(R) do {} while (0)
23147 #endif
23148 
23149   if (DEFAULT_ABI == ABI_ELFv2)
23150     {
23151       cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
23152 
23153       /* With -mminimal-toc we may generate an extra use of r2 below.  */
23154       if (!TARGET_SINGLE_PIC_BASE
23155 	  && TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
23156 	cfun->machine->r2_setup_needed = true;
23157     }
23158 
23159 
23160   if (flag_stack_usage_info)
23161     current_function_static_stack_size = info->total_size;
23162 
23163   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
23164     rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
23165 
23166   if (TARGET_FIX_AND_CONTINUE)
23167     {
23168       /* gdb on darwin arranges to forward a function from the old
23169 	 address by modifying the first 5 instructions of the function
23170 	 to branch to the overriding function.  This is necessary to
23171 	 permit function pointers that point to the old function to
23172 	 actually forward to the new function.  */
23173       emit_insn (gen_nop ());
23174       emit_insn (gen_nop ());
23175       emit_insn (gen_nop ());
23176       emit_insn (gen_nop ());
23177       emit_insn (gen_nop ());
23178     }
23179 
23180   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
23181     {
23182       reg_mode = V2SImode;
23183       reg_size = 8;
23184     }
23185 
23186   /* Handle world saves specially here.  */
23187   if (WORLD_SAVE_P (info))
23188     {
23189       int i, j, sz;
23190       rtx treg;
23191       rtvec p;
23192       rtx reg0;
23193 
23194       /* save_world expects lr in r0. */
23195       reg0 = gen_rtx_REG (Pmode, 0);
23196       if (info->lr_save_p)
23197 	{
23198 	  insn = emit_move_insn (reg0,
23199 				 gen_rtx_REG (Pmode, LR_REGNO));
23200 	  RTX_FRAME_RELATED_P (insn) = 1;
23201 	}
23202 
23203       /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
23204 	 assumptions about the offsets of various bits of the stack
23205 	 frame.  */
23206       gcc_assert (info->gp_save_offset == -220
23207 		  && info->fp_save_offset == -144
23208 		  && info->lr_save_offset == 8
23209 		  && info->cr_save_offset == 4
23210 		  && info->push_p
23211 		  && info->lr_save_p
23212 		  && (!crtl->calls_eh_return
23213 		      || info->ehrd_offset == -432)
23214 		  && info->vrsave_save_offset == -224
23215 		  && info->altivec_save_offset == -416);
23216 
23217       treg = gen_rtx_REG (SImode, 11);
23218       emit_move_insn (treg, GEN_INT (-info->total_size));
23219 
23220       /* SAVE_WORLD takes the caller's LR in R0 and the frame size
23221 	 in R11.  It also clobbers R12, so beware!  */
23222 
23223       /* Preserve CR2 for save_world prologues */
23224       sz = 5;
23225       sz += 32 - info->first_gp_reg_save;
23226       sz += 64 - info->first_fp_reg_save;
23227       sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
23228       p = rtvec_alloc (sz);
23229       j = 0;
23230       RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
23231 					    gen_rtx_REG (SImode,
23232 							 LR_REGNO));
23233       RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
23234 					gen_rtx_SYMBOL_REF (Pmode,
23235 							    "*save_world"));
23236       /* We do floats first so that the instruction pattern matches
23237 	 properly.  */
23238       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
23239 	RTVEC_ELT (p, j++)
23240 	  = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
23241 					  ? DFmode : SFmode,
23242 					  info->first_fp_reg_save + i),
23243 			     frame_reg_rtx,
23244 			     info->fp_save_offset + frame_off + 8 * i);
23245       for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
23246 	RTVEC_ELT (p, j++)
23247 	  = gen_frame_store (gen_rtx_REG (V4SImode,
23248 					  info->first_altivec_reg_save + i),
23249 			     frame_reg_rtx,
23250 			     info->altivec_save_offset + frame_off + 16 * i);
23251       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23252 	RTVEC_ELT (p, j++)
23253 	  = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23254 			     frame_reg_rtx,
23255 			     info->gp_save_offset + frame_off + reg_size * i);
23256 
23257       /* CR register traditionally saved as CR2.  */
23258       RTVEC_ELT (p, j++)
23259 	= gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
23260 			   frame_reg_rtx, info->cr_save_offset + frame_off);
23261       /* Explain about use of R0.  */
23262       if (info->lr_save_p)
23263 	RTVEC_ELT (p, j++)
23264 	  = gen_frame_store (reg0,
23265 			     frame_reg_rtx, info->lr_save_offset + frame_off);
23266       /* Explain what happens to the stack pointer.  */
23267       {
23268 	rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
23269 	RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
23270       }
23271 
23272       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23273       rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23274 			    treg, GEN_INT (-info->total_size));
23275       sp_off = frame_off = info->total_size;
23276     }
23277 
23278   strategy = info->savres_strategy;
23279 
23280   /* For V.4, update stack before we do any saving and set back pointer.  */
23281   if (! WORLD_SAVE_P (info)
23282       && info->push_p
23283       && (DEFAULT_ABI == ABI_V4
23284 	  || crtl->calls_eh_return))
23285     {
23286       bool need_r11 = (TARGET_SPE
23287 		       ? (!(strategy & SAVE_INLINE_GPRS)
23288 			  && info->spe_64bit_regs_used == 0)
23289 		       : (!(strategy & SAVE_INLINE_FPRS)
23290 			  || !(strategy & SAVE_INLINE_GPRS)
23291 			  || !(strategy & SAVE_INLINE_VRS)));
23292       int ptr_regno = -1;
23293       rtx ptr_reg = NULL_RTX;
23294       int ptr_off = 0;
23295 
23296       if (info->total_size < 32767)
23297 	frame_off = info->total_size;
23298       else if (need_r11)
23299 	ptr_regno = 11;
23300       else if (info->cr_save_p
23301 	       || info->lr_save_p
23302 	       || info->first_fp_reg_save < 64
23303 	       || info->first_gp_reg_save < 32
23304 	       || info->altivec_size != 0
23305 	       || info->vrsave_mask != 0
23306 	       || crtl->calls_eh_return)
23307 	ptr_regno = 12;
23308       else
23309 	{
23310 	  /* The prologue won't be saving any regs so there is no need
23311 	     to set up a frame register to access any frame save area.
23312 	     We also won't be using frame_off anywhere below, but set
23313 	     the correct value anyway to protect against future
23314 	     changes to this function.  */
23315 	  frame_off = info->total_size;
23316 	}
23317       if (ptr_regno != -1)
23318 	{
23319 	  /* Set up the frame offset to that needed by the first
23320 	     out-of-line save function.  */
23321 	  START_USE (ptr_regno);
23322 	  ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23323 	  frame_reg_rtx = ptr_reg;
23324 	  if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
23325 	    gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
23326 	  else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
23327 	    ptr_off = info->gp_save_offset + info->gp_size;
23328 	  else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
23329 	    ptr_off = info->altivec_save_offset + info->altivec_size;
23330 	  frame_off = -ptr_off;
23331 	}
23332       rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
23333       sp_off = info->total_size;
23334       if (frame_reg_rtx != sp_reg_rtx)
23335 	rs6000_emit_stack_tie (frame_reg_rtx, false);
23336     }
23337 
23338   /* If we use the link register, get it into r0.  */
23339   if (!WORLD_SAVE_P (info) && info->lr_save_p)
23340     {
23341       rtx addr, reg, mem;
23342 
23343       reg = gen_rtx_REG (Pmode, 0);
23344       START_USE (0);
23345       insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
23346       RTX_FRAME_RELATED_P (insn) = 1;
23347 
23348       if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
23349 			| SAVE_NOINLINE_FPRS_SAVES_LR)))
23350 	{
23351 	  addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
23352 			       GEN_INT (info->lr_save_offset + frame_off));
23353 	  mem = gen_rtx_MEM (Pmode, addr);
23354 	  /* This should not be of rs6000_sr_alias_set, because of
23355 	     __builtin_return_address.  */
23356 
23357 	  insn = emit_move_insn (mem, reg);
23358 	  rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23359 				NULL_RTX, NULL_RTX);
23360 	  END_USE (0);
23361 	}
23362     }
23363 
23364   /* If we need to save CR, put it into r12 or r11.  Choose r12 except when
23365      r12 will be needed by out-of-line gpr restore.  */
23366   cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
23367 		   && !(strategy & (SAVE_INLINE_GPRS
23368 				    | SAVE_NOINLINE_GPRS_SAVES_LR))
23369 		   ? 11 : 12);
23370   if (!WORLD_SAVE_P (info)
23371       && info->cr_save_p
23372       && REGNO (frame_reg_rtx) != cr_save_regno
23373       && !(using_static_chain_p && cr_save_regno == 11))
23374     {
23375       cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
23376       START_USE (cr_save_regno);
23377       rs6000_emit_move_from_cr (cr_save_rtx);
23378     }
23379 
23380   /* Do any required saving of fpr's.  If only one or two to save, do
23381      it ourselves.  Otherwise, call function.  */
23382   if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
23383     {
23384       int i;
23385       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
23386 	if (save_reg_p (info->first_fp_reg_save + i))
23387 	  emit_frame_save (frame_reg_rtx,
23388 			   (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
23389 			    ? DFmode : SFmode),
23390 			   info->first_fp_reg_save + i,
23391 			   info->fp_save_offset + frame_off + 8 * i,
23392 			   sp_off - frame_off);
23393     }
23394   else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
23395     {
23396       bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
23397       int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
23398       unsigned ptr_regno = ptr_regno_for_savres (sel);
23399       rtx ptr_reg = frame_reg_rtx;
23400 
23401       if (REGNO (frame_reg_rtx) == ptr_regno)
23402 	gcc_checking_assert (frame_off == 0);
23403       else
23404 	{
23405 	  ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23406 	  NOT_INUSE (ptr_regno);
23407 	  emit_insn (gen_add3_insn (ptr_reg,
23408 				    frame_reg_rtx, GEN_INT (frame_off)));
23409 	}
23410       insn = rs6000_emit_savres_rtx (info, ptr_reg,
23411 				     info->fp_save_offset,
23412 				     info->lr_save_offset,
23413 				     DFmode, sel);
23414       rs6000_frame_related (insn, ptr_reg, sp_off,
23415 			    NULL_RTX, NULL_RTX);
23416       if (lr)
23417 	END_USE (0);
23418     }
23419 
23420   /* Save GPRs.  This is done as a PARALLEL if we are using
23421      the store-multiple instructions.  */
23422   if (!WORLD_SAVE_P (info)
23423       && TARGET_SPE_ABI
23424       && info->spe_64bit_regs_used != 0
23425       && info->first_gp_reg_save != 32)
23426     {
23427       int i;
23428       rtx spe_save_area_ptr;
23429       HOST_WIDE_INT save_off;
23430       int ool_adjust = 0;
23431 
23432       /* Determine whether we can address all of the registers that need
23433 	 to be saved with an offset from frame_reg_rtx that fits in
23434 	 the small const field for SPE memory instructions.  */
23435       int spe_regs_addressable
23436 	= (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
23437 				+ reg_size * (32 - info->first_gp_reg_save - 1))
23438 	   && (strategy & SAVE_INLINE_GPRS));
23439 
23440       if (spe_regs_addressable)
23441 	{
23442 	  spe_save_area_ptr = frame_reg_rtx;
23443 	  save_off = frame_off;
23444 	}
23445       else
23446 	{
23447 	  /* Make r11 point to the start of the SPE save area.  We need
23448 	     to be careful here if r11 is holding the static chain.  If
23449 	     it is, then temporarily save it in r0.  */
23450 	  HOST_WIDE_INT offset;
23451 
23452 	  if (!(strategy & SAVE_INLINE_GPRS))
23453 	    ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
23454 	  offset = info->spe_gp_save_offset + frame_off - ool_adjust;
23455 	  spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
23456 	  save_off = frame_off - offset;
23457 
23458 	  if (using_static_chain_p)
23459 	    {
23460 	      rtx r0 = gen_rtx_REG (Pmode, 0);
23461 
23462 	      START_USE (0);
23463 	      gcc_assert (info->first_gp_reg_save > 11);
23464 
23465 	      emit_move_insn (r0, spe_save_area_ptr);
23466 	    }
23467 	  else if (REGNO (frame_reg_rtx) != 11)
23468 	    START_USE (11);
23469 
23470 	  emit_insn (gen_addsi3 (spe_save_area_ptr,
23471 				 frame_reg_rtx, GEN_INT (offset)));
23472 	  if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
23473 	    frame_off = -info->spe_gp_save_offset + ool_adjust;
23474 	}
23475 
23476       if ((strategy & SAVE_INLINE_GPRS))
23477 	{
23478 	  for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23479 	    if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23480 	      emit_frame_save (spe_save_area_ptr, reg_mode,
23481 			       info->first_gp_reg_save + i,
23482 			       (info->spe_gp_save_offset + save_off
23483 				+ reg_size * i),
23484 			       sp_off - save_off);
23485 	}
23486       else
23487 	{
23488 	  insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
23489 					 info->spe_gp_save_offset + save_off,
23490 					 0, reg_mode,
23491 					 SAVRES_SAVE | SAVRES_GPR);
23492 
23493 	  rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
23494 				NULL_RTX, NULL_RTX);
23495 	}
23496 
23497       /* Move the static chain pointer back.  */
23498       if (!spe_regs_addressable)
23499 	{
23500 	  if (using_static_chain_p)
23501 	    {
23502 	      emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
23503 	      END_USE (0);
23504 	    }
23505 	  else if (REGNO (frame_reg_rtx) != 11)
23506 	    END_USE (11);
23507 	}
23508     }
23509   else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
23510     {
23511       bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
23512       int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
23513       unsigned ptr_regno = ptr_regno_for_savres (sel);
23514       rtx ptr_reg = frame_reg_rtx;
23515       bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
23516       int end_save = info->gp_save_offset + info->gp_size;
23517       int ptr_off;
23518 
23519       if (!ptr_set_up)
23520 	ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23521 
23522       /* Need to adjust r11 (r12) if we saved any FPRs.  */
23523       if (end_save + frame_off != 0)
23524 	{
23525 	  rtx offset = GEN_INT (end_save + frame_off);
23526 
23527 	  if (ptr_set_up)
23528 	    frame_off = -end_save;
23529 	  else
23530 	    NOT_INUSE (ptr_regno);
23531 	  emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23532 	}
23533       else if (!ptr_set_up)
23534 	{
23535 	  NOT_INUSE (ptr_regno);
23536 	  emit_move_insn (ptr_reg, frame_reg_rtx);
23537 	}
23538       ptr_off = -end_save;
23539       insn = rs6000_emit_savres_rtx (info, ptr_reg,
23540 				     info->gp_save_offset + ptr_off,
23541 				     info->lr_save_offset + ptr_off,
23542 				     reg_mode, sel);
23543       rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
23544 			    NULL_RTX, NULL_RTX);
23545       if (lr)
23546 	END_USE (0);
23547     }
23548   else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
23549     {
23550       rtvec p;
23551       int i;
23552       p = rtvec_alloc (32 - info->first_gp_reg_save);
23553       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23554 	RTVEC_ELT (p, i)
23555 	  = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23556 			     frame_reg_rtx,
23557 			     info->gp_save_offset + frame_off + reg_size * i);
23558       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23559       rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23560 			    NULL_RTX, NULL_RTX);
23561     }
23562   else if (!WORLD_SAVE_P (info))
23563     {
23564       int i;
23565       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23566 	if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23567 	  emit_frame_save (frame_reg_rtx, reg_mode,
23568 			   info->first_gp_reg_save + i,
23569 			   info->gp_save_offset + frame_off + reg_size * i,
23570 			   sp_off - frame_off);
23571     }
23572 
23573   if (crtl->calls_eh_return)
23574     {
23575       unsigned int i;
23576       rtvec p;
23577 
23578       for (i = 0; ; ++i)
23579 	{
23580 	  unsigned int regno = EH_RETURN_DATA_REGNO (i);
23581 	  if (regno == INVALID_REGNUM)
23582 	    break;
23583 	}
23584 
23585       p = rtvec_alloc (i);
23586 
23587       for (i = 0; ; ++i)
23588 	{
23589 	  unsigned int regno = EH_RETURN_DATA_REGNO (i);
23590 	  if (regno == INVALID_REGNUM)
23591 	    break;
23592 
23593 	  insn
23594 	    = gen_frame_store (gen_rtx_REG (reg_mode, regno),
23595 			       sp_reg_rtx,
23596 			       info->ehrd_offset + sp_off + reg_size * (int) i);
23597 	  RTVEC_ELT (p, i) = insn;
23598 	  RTX_FRAME_RELATED_P (insn) = 1;
23599 	}
23600 
23601       insn = emit_insn (gen_blockage ());
23602       RTX_FRAME_RELATED_P (insn) = 1;
23603       add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
23604     }
23605 
23606   /* In AIX ABI we need to make sure r2 is really saved.  */
23607   if (TARGET_AIX && crtl->calls_eh_return)
23608     {
23609       rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
23610       rtx save_insn, join_insn, note;
23611       long toc_restore_insn;
23612 
23613       tmp_reg = gen_rtx_REG (Pmode, 11);
23614       tmp_reg_si = gen_rtx_REG (SImode, 11);
23615       if (using_static_chain_p)
23616 	{
23617 	  START_USE (0);
23618 	  emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
23619 	}
23620       else
23621 	START_USE (11);
23622       emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
23623       /* Peek at instruction to which this function returns.  If it's
23624 	 restoring r2, then we know we've already saved r2.  We can't
23625 	 unconditionally save r2 because the value we have will already
23626 	 be updated if we arrived at this function via a plt call or
23627 	 toc adjusting stub.  */
23628       emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
23629       toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
23630 			  + RS6000_TOC_SAVE_SLOT);
23631       hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
23632       emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
23633       compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
23634       validate_condition_mode (EQ, CCUNSmode);
23635       lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
23636       emit_insn (gen_rtx_SET (VOIDmode, compare_result,
23637 			      gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
23638       toc_save_done = gen_label_rtx ();
23639       jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
23640 				   gen_rtx_EQ (VOIDmode, compare_result,
23641 					       const0_rtx),
23642 				   gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
23643 				   pc_rtx);
23644       jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
23645       JUMP_LABEL (jump) = toc_save_done;
23646       LABEL_NUSES (toc_save_done) += 1;
23647 
23648       save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
23649 				   TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
23650 				   sp_off - frame_off);
23651 
23652       emit_label (toc_save_done);
23653 
23654       /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
23655 	 have a CFG that has different saves along different paths.
23656 	 Move the note to a dummy blockage insn, which describes that
23657 	 R2 is unconditionally saved after the label.  */
23658       /* ??? An alternate representation might be a special insn pattern
23659 	 containing both the branch and the store.  That might let the
23660 	 code that minimizes the number of DW_CFA_advance opcodes better
23661 	 freedom in placing the annotations.  */
23662       note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
23663       if (note)
23664 	remove_note (save_insn, note);
23665       else
23666 	note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
23667 			       copy_rtx (PATTERN (save_insn)), NULL_RTX);
23668       RTX_FRAME_RELATED_P (save_insn) = 0;
23669 
23670       join_insn = emit_insn (gen_blockage ());
23671       REG_NOTES (join_insn) = note;
23672       RTX_FRAME_RELATED_P (join_insn) = 1;
23673 
23674       if (using_static_chain_p)
23675 	{
23676 	  emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
23677 	  END_USE (0);
23678 	}
23679       else
23680 	END_USE (11);
23681     }
23682 
23683   /* Save CR if we use any that must be preserved.  */
23684   if (!WORLD_SAVE_P (info) && info->cr_save_p)
23685     {
23686       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
23687 			       GEN_INT (info->cr_save_offset + frame_off));
23688       rtx mem = gen_frame_mem (SImode, addr);
23689 
23690       /* If we didn't copy cr before, do so now using r0.  */
23691       if (cr_save_rtx == NULL_RTX)
23692 	{
23693 	  START_USE (0);
23694 	  cr_save_rtx = gen_rtx_REG (SImode, 0);
23695 	  rs6000_emit_move_from_cr (cr_save_rtx);
23696 	}
23697 
23698       /* Saving CR requires a two-instruction sequence: one instruction
23699 	 to move the CR to a general-purpose register, and a second
23700 	 instruction that stores the GPR to memory.
23701 
23702 	 We do not emit any DWARF CFI records for the first of these,
23703 	 because we cannot properly represent the fact that CR is saved in
23704 	 a register.  One reason is that we cannot express that multiple
23705 	 CR fields are saved; another reason is that on 64-bit, the size
23706 	 of the CR register in DWARF (4 bytes) differs from the size of
23707 	 a general-purpose register.
23708 
23709 	 This means if any intervening instruction were to clobber one of
23710 	 the call-saved CR fields, we'd have incorrect CFI.  To prevent
23711 	 this from happening, we mark the store to memory as a use of
23712 	 those CR fields, which prevents any such instruction from being
23713 	 scheduled in between the two instructions.  */
23714       rtx crsave_v[9];
23715       int n_crsave = 0;
23716       int i;
23717 
23718       crsave_v[n_crsave++] = gen_rtx_SET (VOIDmode, mem, cr_save_rtx);
23719       for (i = 0; i < 8; i++)
23720 	if (save_reg_p (CR0_REGNO + i))
23721 	  crsave_v[n_crsave++]
23722 	    = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23723 
23724       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
23725 					  gen_rtvec_v (n_crsave, crsave_v)));
23726       END_USE (REGNO (cr_save_rtx));
23727 
23728       /* Now, there's no way that dwarf2out_frame_debug_expr is going to
23729 	 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
23730 	 so we need to construct a frame expression manually.  */
23731       RTX_FRAME_RELATED_P (insn) = 1;
23732 
23733       /* Update address to be stack-pointer relative, like
23734 	 rs6000_frame_related would do.  */
23735       addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
23736 			   GEN_INT (info->cr_save_offset + sp_off));
23737       mem = gen_frame_mem (SImode, addr);
23738 
23739       if (DEFAULT_ABI == ABI_ELFv2)
23740 	{
23741 	  /* In the ELFv2 ABI we generate separate CFI records for each
23742 	     CR field that was actually saved.  They all point to the
23743 	     same 32-bit stack slot.  */
23744 	  rtx crframe[8];
23745 	  int n_crframe = 0;
23746 
23747 	  for (i = 0; i < 8; i++)
23748 	    if (save_reg_p (CR0_REGNO + i))
23749 	      {
23750 		crframe[n_crframe]
23751 		  = gen_rtx_SET (VOIDmode, mem,
23752 				 gen_rtx_REG (SImode, CR0_REGNO + i));
23753 
23754 		RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
23755 		n_crframe++;
23756 	     }
23757 
23758 	  add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23759 			gen_rtx_PARALLEL (VOIDmode,
23760 					  gen_rtvec_v (n_crframe, crframe)));
23761 	}
23762       else
23763 	{
23764 	  /* In other ABIs, by convention, we use a single CR regnum to
23765 	     represent the fact that all call-saved CR fields are saved.
23766 	     We use CR2_REGNO to be compatible with gcc-2.95 on Linux.  */
23767 	  rtx set = gen_rtx_SET (VOIDmode, mem,
23768 				 gen_rtx_REG (SImode, CR2_REGNO));
23769 	  add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
23770 	}
23771     }
23772 
23773   /* In the ELFv2 ABI we need to save all call-saved CR fields into
23774      *separate* slots if the routine calls __builtin_eh_return, so
23775      that they can be independently restored by the unwinder.  */
23776   if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
23777     {
23778       int i, cr_off = info->ehcr_offset;
23779       rtx crsave;
23780 
23781       /* ??? We might get better performance by using multiple mfocrf
23782 	 instructions.  */
23783       crsave = gen_rtx_REG (SImode, 0);
23784       emit_insn (gen_movesi_from_cr (crsave));
23785 
23786       for (i = 0; i < 8; i++)
23787 	if (!call_used_regs[CR0_REGNO + i])
23788 	  {
23789 	    rtvec p = rtvec_alloc (2);
23790 	    RTVEC_ELT (p, 0)
23791 	      = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
23792 	    RTVEC_ELT (p, 1)
23793 	      = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23794 
23795 	    insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23796 
23797 	    RTX_FRAME_RELATED_P (insn) = 1;
23798 	    add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23799 			  gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
23800 					   sp_reg_rtx, cr_off + sp_off));
23801 
23802 	    cr_off += reg_size;
23803 	  }
23804     }
23805 
23806   /* Update stack and set back pointer unless this is V.4,
23807      for which it was done previously.  */
23808   if (!WORLD_SAVE_P (info) && info->push_p
23809       && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
23810     {
23811       rtx ptr_reg = NULL;
23812       int ptr_off = 0;
23813 
23814       /* If saving altivec regs we need to be able to address all save
23815 	 locations using a 16-bit offset.  */
23816       if ((strategy & SAVE_INLINE_VRS) == 0
23817 	  || (info->altivec_size != 0
23818 	      && (info->altivec_save_offset + info->altivec_size - 16
23819 		  + info->total_size - frame_off) > 32767)
23820 	  || (info->vrsave_size != 0
23821 	      && (info->vrsave_save_offset
23822 		  + info->total_size - frame_off) > 32767))
23823 	{
23824 	  int sel = SAVRES_SAVE | SAVRES_VR;
23825 	  unsigned ptr_regno = ptr_regno_for_savres (sel);
23826 
23827 	  if (using_static_chain_p
23828 	      && ptr_regno == STATIC_CHAIN_REGNUM)
23829 	    ptr_regno = 12;
23830 	  if (REGNO (frame_reg_rtx) != ptr_regno)
23831 	    START_USE (ptr_regno);
23832 	  ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23833 	  frame_reg_rtx = ptr_reg;
23834 	  ptr_off = info->altivec_save_offset + info->altivec_size;
23835 	  frame_off = -ptr_off;
23836 	}
23837       else if (REGNO (frame_reg_rtx) == 1)
23838 	frame_off = info->total_size;
23839       rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
23840       sp_off = info->total_size;
23841       if (frame_reg_rtx != sp_reg_rtx)
23842 	rs6000_emit_stack_tie (frame_reg_rtx, false);
23843     }
23844 
23845   /* Set frame pointer, if needed.  */
23846   if (frame_pointer_needed)
23847     {
23848       insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
23849 			     sp_reg_rtx);
23850       RTX_FRAME_RELATED_P (insn) = 1;
23851     }
23852 
23853   /* Save AltiVec registers if needed.  Save here because the red zone does
23854      not always include AltiVec registers.  */
23855   if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23856       && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
23857     {
23858       int end_save = info->altivec_save_offset + info->altivec_size;
23859       int ptr_off;
23860       /* Oddly, the vector save/restore functions point r0 at the end
23861 	 of the save area, then use r11 or r12 to load offsets for
23862 	 [reg+reg] addressing.  */
23863       rtx ptr_reg = gen_rtx_REG (Pmode, 0);
23864       int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
23865       rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
23866 
23867       gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
23868       NOT_INUSE (0);
23869       if (end_save + frame_off != 0)
23870 	{
23871 	  rtx offset = GEN_INT (end_save + frame_off);
23872 
23873 	  emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23874 	}
23875       else
23876 	emit_move_insn (ptr_reg, frame_reg_rtx);
23877 
23878       ptr_off = -end_save;
23879       insn = rs6000_emit_savres_rtx (info, scratch_reg,
23880 				     info->altivec_save_offset + ptr_off,
23881 				     0, V4SImode, SAVRES_SAVE | SAVRES_VR);
23882       rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
23883 			    NULL_RTX, NULL_RTX);
23884       if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
23885 	{
23886 	  /* The oddity mentioned above clobbered our frame reg.  */
23887 	  emit_move_insn (frame_reg_rtx, ptr_reg);
23888 	  frame_off = ptr_off;
23889 	}
23890     }
23891   else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23892 	   && info->altivec_size != 0)
23893     {
23894       int i;
23895 
23896       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
23897 	if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
23898 	  {
23899 	    rtx areg, savereg, mem;
23900 	    int offset;
23901 
23902 	    offset = (info->altivec_save_offset + frame_off
23903 		      + 16 * (i - info->first_altivec_reg_save));
23904 
23905 	    savereg = gen_rtx_REG (V4SImode, i);
23906 
23907 	    NOT_INUSE (0);
23908 	    areg = gen_rtx_REG (Pmode, 0);
23909 	    emit_move_insn (areg, GEN_INT (offset));
23910 
23911 	    /* AltiVec addressing mode is [reg+reg].  */
23912 	    mem = gen_frame_mem (V4SImode,
23913 				 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
23914 
23915 	    /* Rather than emitting a generic move, force use of the stvx
23916 	       instruction, which we always want.  In particular we don't
23917 	       want xxpermdi/stxvd2x for little endian.  */
23918 	    insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
23919 
23920 	    rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23921 				  areg, GEN_INT (offset));
23922 	  }
23923     }
23924 
23925   /* VRSAVE is a bit vector representing which AltiVec registers
23926      are used.  The OS uses this to determine which vector
23927      registers to save on a context switch.  We need to save
23928      VRSAVE on the stack frame, add whatever AltiVec registers we
23929      used in this function, and do the corresponding magic in the
23930      epilogue.  */
23931 
23932   if (!WORLD_SAVE_P (info)
23933       && TARGET_ALTIVEC
23934       && TARGET_ALTIVEC_VRSAVE
23935       && info->vrsave_mask != 0)
23936     {
23937       rtx reg, vrsave;
23938       int offset;
23939       int save_regno;
23940 
23941       /* Get VRSAVE onto a GPR.  Note that ABI_V4 and ABI_DARWIN might
23942 	 be using r12 as frame_reg_rtx and r11 as the static chain
23943 	 pointer for nested functions.  */
23944       save_regno = 12;
23945       if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
23946 	  && !using_static_chain_p)
23947 	save_regno = 11;
23948       else if (REGNO (frame_reg_rtx) == 12)
23949 	{
23950 	  save_regno = 11;
23951 	  if (using_static_chain_p)
23952 	    save_regno = 0;
23953 	}
23954 
23955       NOT_INUSE (save_regno);
23956       reg = gen_rtx_REG (SImode, save_regno);
23957       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
23958       if (TARGET_MACHO)
23959 	emit_insn (gen_get_vrsave_internal (reg));
23960       else
23961 	emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
23962 
23963       /* Save VRSAVE.  */
23964       offset = info->vrsave_save_offset + frame_off;
23965       insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
23966 
23967       /* Include the registers in the mask.  */
23968       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
23969 
23970       insn = emit_insn (generate_set_vrsave (reg, info, 0));
23971     }
23972 
23973   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
23974   if (!TARGET_SINGLE_PIC_BASE
23975       && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
23976 	  || (DEFAULT_ABI == ABI_V4
23977 	      && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
23978 	      && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
23979     {
23980       /* If emit_load_toc_table will use the link register, we need to save
23981 	 it.  We use R12 for this purpose because emit_load_toc_table
23982 	 can use register 0.  This allows us to use a plain 'blr' to return
23983 	 from the procedure more often.  */
23984       int save_LR_around_toc_setup = (TARGET_ELF
23985 				      && DEFAULT_ABI == ABI_V4
23986 				      && flag_pic
23987 				      && ! info->lr_save_p
23988 				      && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
23989       if (save_LR_around_toc_setup)
23990 	{
23991 	  rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23992 	  rtx tmp = gen_rtx_REG (Pmode, 12);
23993 
23994 	  insn = emit_move_insn (tmp, lr);
23995 	  RTX_FRAME_RELATED_P (insn) = 1;
23996 
23997 	  rs6000_emit_load_toc_table (TRUE);
23998 
23999 	  insn = emit_move_insn (lr, tmp);
24000 	  add_reg_note (insn, REG_CFA_RESTORE, lr);
24001 	  RTX_FRAME_RELATED_P (insn) = 1;
24002 	}
24003       else
24004 	rs6000_emit_load_toc_table (TRUE);
24005     }
24006 
24007 #if TARGET_MACHO
24008   if (!TARGET_SINGLE_PIC_BASE
24009       && DEFAULT_ABI == ABI_DARWIN
24010       && flag_pic && crtl->uses_pic_offset_table)
24011     {
24012       rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
24013       rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
24014 
24015       /* Save and restore LR locally around this call (in R0).  */
24016       if (!info->lr_save_p)
24017 	emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
24018 
24019       emit_insn (gen_load_macho_picbase (src));
24020 
24021       emit_move_insn (gen_rtx_REG (Pmode,
24022 				   RS6000_PIC_OFFSET_TABLE_REGNUM),
24023 		      lr);
24024 
24025       if (!info->lr_save_p)
24026 	emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
24027     }
24028 #endif
24029 
24030   /* If we need to, save the TOC register after doing the stack setup.
24031      Do not emit eh frame info for this save.  The unwinder wants info,
24032      conceptually attached to instructions in this function, about
24033      register values in the caller of this function.  This R2 may have
24034      already been changed from the value in the caller.
24035      We don't attempt to write accurate DWARF EH frame info for R2
24036      because code emitted by gcc for a (non-pointer) function call
24037      doesn't save and restore R2.  Instead, R2 is managed out-of-line
24038      by a linker generated plt call stub when the function resides in
24039      a shared library.  This behaviour is costly to describe in DWARF,
24040      both in terms of the size of DWARF info and the time taken in the
24041      unwinder to interpret it.  R2 changes, apart from the
24042      calls_eh_return case earlier in this function, are handled by
24043      linux-unwind.h frob_update_context.  */
24044   if (rs6000_save_toc_in_prologue_p ())
24045     {
24046       rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
24047       emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
24048     }
24049 }
24050 
24051 /* Output .extern statements for the save/restore routines we use.  */
24052 
24053 static void
rs6000_output_savres_externs(FILE * file)24054 rs6000_output_savres_externs (FILE *file)
24055 {
24056   rs6000_stack_t *info = rs6000_stack_info ();
24057 
24058   if (TARGET_DEBUG_STACK)
24059     debug_stack_info (info);
24060 
24061   /* Write .extern for any function we will call to save and restore
24062      fp values.  */
24063   if (info->first_fp_reg_save < 64
24064       && !TARGET_MACHO
24065       && !TARGET_ELF)
24066     {
24067       char *name;
24068       int regno = info->first_fp_reg_save - 32;
24069 
24070       if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
24071 	{
24072 	  bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
24073 	  int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
24074 	  name = rs6000_savres_routine_name (info, regno, sel);
24075 	  fprintf (file, "\t.extern %s\n", name);
24076 	}
24077       if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
24078 	{
24079 	  bool lr = (info->savres_strategy
24080 		     & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24081 	  int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24082 	  name = rs6000_savres_routine_name (info, regno, sel);
24083 	  fprintf (file, "\t.extern %s\n", name);
24084 	}
24085     }
24086 }
24087 
24088 /* Write function prologue.  */
24089 
24090 static void
rs6000_output_function_prologue(FILE * file,HOST_WIDE_INT size ATTRIBUTE_UNUSED)24091 rs6000_output_function_prologue (FILE *file,
24092 				 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
24093 {
24094   if (!cfun->is_thunk)
24095     rs6000_output_savres_externs (file);
24096 
24097   /* ELFv2 ABI r2 setup code and local entry point.  This must follow
24098      immediately after the global entry point label.  */
24099   if (DEFAULT_ABI == ABI_ELFv2 && cfun->machine->r2_setup_needed)
24100     {
24101       const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
24102 
24103       fprintf (file, "0:\taddis 2,12,.TOC.-0b@ha\n");
24104       fprintf (file, "\taddi 2,2,.TOC.-0b@l\n");
24105 
24106       fputs ("\t.localentry\t", file);
24107       assemble_name (file, name);
24108       fputs (",.-", file);
24109       assemble_name (file, name);
24110       fputs ("\n", file);
24111     }
24112 
24113   /* Output -mprofile-kernel code.  This needs to be done here instead of
24114      in output_function_profile since it must go after the ELFv2 ABI
24115      local entry point.  */
24116   if (TARGET_PROFILE_KERNEL)
24117     {
24118       gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
24119       gcc_assert (!TARGET_32BIT);
24120 
24121       asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
24122       asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
24123 
24124       /* In the ELFv2 ABI we have no compiler stack word.  It must be
24125 	 the resposibility of _mcount to preserve the static chain
24126 	 register if required.  */
24127       if (DEFAULT_ABI != ABI_ELFv2
24128 	  && cfun->static_chain_decl != NULL)
24129 	{
24130 	  asm_fprintf (file, "\tstd %s,24(%s)\n",
24131 		       reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24132 	  fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24133 	  asm_fprintf (file, "\tld %s,24(%s)\n",
24134 		       reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24135 	}
24136       else
24137 	fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24138     }
24139 
24140   rs6000_pic_labelno++;
24141 }
24142 
24143 /* Non-zero if vmx regs are restored before the frame pop, zero if
24144    we restore after the pop when possible.  */
24145 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
24146 
24147 /* Restoring cr is a two step process: loading a reg from the frame
24148    save, then moving the reg to cr.  For ABI_V4 we must let the
24149    unwinder know that the stack location is no longer valid at or
24150    before the stack deallocation, but we can't emit a cfa_restore for
24151    cr at the stack deallocation like we do for other registers.
24152    The trouble is that it is possible for the move to cr to be
24153    scheduled after the stack deallocation.  So say exactly where cr
24154    is located on each of the two insns.  */
24155 
24156 static rtx
load_cr_save(int regno,rtx frame_reg_rtx,int offset,bool exit_func)24157 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
24158 {
24159   rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
24160   rtx reg = gen_rtx_REG (SImode, regno);
24161   rtx insn = emit_move_insn (reg, mem);
24162 
24163   if (!exit_func && DEFAULT_ABI == ABI_V4)
24164     {
24165       rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
24166       rtx set = gen_rtx_SET (VOIDmode, reg, cr);
24167 
24168       add_reg_note (insn, REG_CFA_REGISTER, set);
24169       RTX_FRAME_RELATED_P (insn) = 1;
24170     }
24171   return reg;
24172 }
24173 
24174 /* Reload CR from REG.  */
24175 
24176 static void
restore_saved_cr(rtx reg,int using_mfcr_multiple,bool exit_func)24177 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
24178 {
24179   int count = 0;
24180   int i;
24181 
24182   if (using_mfcr_multiple)
24183     {
24184       for (i = 0; i < 8; i++)
24185 	if (save_reg_p (CR0_REGNO + i))
24186 	  count++;
24187       gcc_assert (count);
24188     }
24189 
24190   if (using_mfcr_multiple && count > 1)
24191     {
24192       rtx insn;
24193       rtvec p;
24194       int ndx;
24195 
24196       p = rtvec_alloc (count);
24197 
24198       ndx = 0;
24199       for (i = 0; i < 8; i++)
24200 	if (save_reg_p (CR0_REGNO + i))
24201 	  {
24202 	    rtvec r = rtvec_alloc (2);
24203 	    RTVEC_ELT (r, 0) = reg;
24204 	    RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
24205 	    RTVEC_ELT (p, ndx) =
24206 	      gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
24207 			   gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
24208 	    ndx++;
24209 	  }
24210       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
24211       gcc_assert (ndx == count);
24212 
24213       /* For the ELFv2 ABI we generate a CFA_RESTORE for each
24214 	 CR field separately.  */
24215       if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
24216 	{
24217 	  for (i = 0; i < 8; i++)
24218 	    if (save_reg_p (CR0_REGNO + i))
24219 	      add_reg_note (insn, REG_CFA_RESTORE,
24220 			    gen_rtx_REG (SImode, CR0_REGNO + i));
24221 
24222 	  RTX_FRAME_RELATED_P (insn) = 1;
24223 	}
24224     }
24225   else
24226     for (i = 0; i < 8; i++)
24227       if (save_reg_p (CR0_REGNO + i))
24228 	{
24229 	  rtx insn = emit_insn (gen_movsi_to_cr_one
24230 				 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
24231 
24232 	  /* For the ELFv2 ABI we generate a CFA_RESTORE for each
24233 	     CR field separately, attached to the insn that in fact
24234 	     restores this particular CR field.  */
24235 	  if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
24236 	    {
24237 	      add_reg_note (insn, REG_CFA_RESTORE,
24238 			    gen_rtx_REG (SImode, CR0_REGNO + i));
24239 
24240 	      RTX_FRAME_RELATED_P (insn) = 1;
24241 	    }
24242 	}
24243 
24244   /* For other ABIs, we just generate a single CFA_RESTORE for CR2.  */
24245   if (!exit_func && DEFAULT_ABI != ABI_ELFv2
24246       && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
24247     {
24248       rtx insn = get_last_insn ();
24249       rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
24250 
24251       add_reg_note (insn, REG_CFA_RESTORE, cr);
24252       RTX_FRAME_RELATED_P (insn) = 1;
24253     }
24254 }
24255 
24256 /* Like cr, the move to lr instruction can be scheduled after the
24257    stack deallocation, but unlike cr, its stack frame save is still
24258    valid.  So we only need to emit the cfa_restore on the correct
24259    instruction.  */
24260 
24261 static void
load_lr_save(int regno,rtx frame_reg_rtx,int offset)24262 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
24263 {
24264   rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
24265   rtx reg = gen_rtx_REG (Pmode, regno);
24266 
24267   emit_move_insn (reg, mem);
24268 }
24269 
24270 static void
restore_saved_lr(int regno,bool exit_func)24271 restore_saved_lr (int regno, bool exit_func)
24272 {
24273   rtx reg = gen_rtx_REG (Pmode, regno);
24274   rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
24275   rtx insn = emit_move_insn (lr, reg);
24276 
24277   if (!exit_func && flag_shrink_wrap)
24278     {
24279       add_reg_note (insn, REG_CFA_RESTORE, lr);
24280       RTX_FRAME_RELATED_P (insn) = 1;
24281     }
24282 }
24283 
24284 static rtx
add_crlr_cfa_restore(const rs6000_stack_t * info,rtx cfa_restores)24285 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
24286 {
24287   if (DEFAULT_ABI == ABI_ELFv2)
24288     {
24289       int i;
24290       for (i = 0; i < 8; i++)
24291 	if (save_reg_p (CR0_REGNO + i))
24292 	  {
24293 	    rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
24294 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
24295 					   cfa_restores);
24296 	  }
24297     }
24298   else if (info->cr_save_p)
24299     cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24300 				   gen_rtx_REG (SImode, CR2_REGNO),
24301 				   cfa_restores);
24302 
24303   if (info->lr_save_p)
24304     cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24305 				   gen_rtx_REG (Pmode, LR_REGNO),
24306 				   cfa_restores);
24307   return cfa_restores;
24308 }
24309 
24310 /* Return true if OFFSET from stack pointer can be clobbered by signals.
24311    V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
24312    below stack pointer not cloberred by signals.  */
24313 
24314 static inline bool
offset_below_red_zone_p(HOST_WIDE_INT offset)24315 offset_below_red_zone_p (HOST_WIDE_INT offset)
24316 {
24317   return offset < (DEFAULT_ABI == ABI_V4
24318 		   ? 0
24319 		   : TARGET_32BIT ? -220 : -288);
24320 }
24321 
24322 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn.  */
24323 
24324 static void
emit_cfa_restores(rtx cfa_restores)24325 emit_cfa_restores (rtx cfa_restores)
24326 {
24327   rtx insn = get_last_insn ();
24328   rtx *loc = &REG_NOTES (insn);
24329 
24330   while (*loc)
24331     loc = &XEXP (*loc, 1);
24332   *loc = cfa_restores;
24333   RTX_FRAME_RELATED_P (insn) = 1;
24334 }
24335 
24336 /* Emit function epilogue as insns.  */
24337 
24338 void
rs6000_emit_epilogue(int sibcall)24339 rs6000_emit_epilogue (int sibcall)
24340 {
24341   rs6000_stack_t *info;
24342   int restoring_GPRs_inline;
24343   int restoring_FPRs_inline;
24344   int using_load_multiple;
24345   int using_mtcr_multiple;
24346   int use_backchain_to_restore_sp;
24347   int restore_lr;
24348   int strategy;
24349   HOST_WIDE_INT frame_off = 0;
24350   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
24351   rtx frame_reg_rtx = sp_reg_rtx;
24352   rtx cfa_restores = NULL_RTX;
24353   rtx insn;
24354   rtx cr_save_reg = NULL_RTX;
24355   enum machine_mode reg_mode = Pmode;
24356   int reg_size = TARGET_32BIT ? 4 : 8;
24357   int i;
24358   bool exit_func;
24359   unsigned ptr_regno;
24360 
24361   info = rs6000_stack_info ();
24362 
24363   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
24364     {
24365       reg_mode = V2SImode;
24366       reg_size = 8;
24367     }
24368 
24369   strategy = info->savres_strategy;
24370   using_load_multiple = strategy & SAVRES_MULTIPLE;
24371   restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
24372   restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
24373   using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
24374 			 || rs6000_cpu == PROCESSOR_PPC603
24375 			 || rs6000_cpu == PROCESSOR_PPC750
24376 			 || optimize_size);
24377   /* Restore via the backchain when we have a large frame, since this
24378      is more efficient than an addis, addi pair.  The second condition
24379      here will not trigger at the moment;  We don't actually need a
24380      frame pointer for alloca, but the generic parts of the compiler
24381      give us one anyway.  */
24382   use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
24383 				 || (cfun->calls_alloca
24384 				     && !frame_pointer_needed));
24385   restore_lr = (info->lr_save_p
24386 		&& (restoring_FPRs_inline
24387 		    || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
24388 		&& (restoring_GPRs_inline
24389 		    || info->first_fp_reg_save < 64));
24390 
24391   if (WORLD_SAVE_P (info))
24392     {
24393       int i, j;
24394       char rname[30];
24395       const char *alloc_rname;
24396       rtvec p;
24397 
24398       /* eh_rest_world_r10 will return to the location saved in the LR
24399 	 stack slot (which is not likely to be our caller.)
24400 	 Input: R10 -- stack adjustment.  Clobbers R0, R11, R12, R7, R8.
24401 	 rest_world is similar, except any R10 parameter is ignored.
24402 	 The exception-handling stuff that was here in 2.95 is no
24403 	 longer necessary.  */
24404 
24405       p = rtvec_alloc (9
24406 		       + 1
24407 		       + 32 - info->first_gp_reg_save
24408 		       + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
24409 		       + 63 + 1 - info->first_fp_reg_save);
24410 
24411       strcpy (rname, ((crtl->calls_eh_return) ?
24412 		      "*eh_rest_world_r10" : "*rest_world"));
24413       alloc_rname = ggc_strdup (rname);
24414 
24415       j = 0;
24416       RTVEC_ELT (p, j++) = ret_rtx;
24417       RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
24418 					gen_rtx_REG (Pmode,
24419 						     LR_REGNO));
24420       RTVEC_ELT (p, j++)
24421 	= gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
24422       /* The instruction pattern requires a clobber here;
24423 	 it is shared with the restVEC helper. */
24424       RTVEC_ELT (p, j++)
24425 	= gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
24426 
24427       {
24428 	/* CR register traditionally saved as CR2.  */
24429 	rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
24430 	RTVEC_ELT (p, j++)
24431 	  = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
24432 	if (flag_shrink_wrap)
24433 	  {
24434 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24435 					   gen_rtx_REG (Pmode, LR_REGNO),
24436 					   cfa_restores);
24437 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24438 	  }
24439       }
24440 
24441       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24442 	{
24443 	  rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24444 	  RTVEC_ELT (p, j++)
24445 	    = gen_frame_load (reg,
24446 			      frame_reg_rtx, info->gp_save_offset + reg_size * i);
24447 	  if (flag_shrink_wrap)
24448 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24449 	}
24450       for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
24451 	{
24452 	  rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
24453 	  RTVEC_ELT (p, j++)
24454 	    = gen_frame_load (reg,
24455 			      frame_reg_rtx, info->altivec_save_offset + 16 * i);
24456 	  if (flag_shrink_wrap)
24457 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24458 	}
24459       for (i = 0; info->first_fp_reg_save + i <= 63; i++)
24460 	{
24461 	  rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
24462 				  ? DFmode : SFmode),
24463 				 info->first_fp_reg_save + i);
24464 	  RTVEC_ELT (p, j++)
24465 	    = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
24466 	  if (flag_shrink_wrap)
24467 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24468 	}
24469       RTVEC_ELT (p, j++)
24470 	= gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
24471       RTVEC_ELT (p, j++)
24472 	= gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
24473       RTVEC_ELT (p, j++)
24474 	= gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
24475       RTVEC_ELT (p, j++)
24476 	= gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
24477       RTVEC_ELT (p, j++)
24478 	= gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
24479       insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
24480 
24481       if (flag_shrink_wrap)
24482 	{
24483 	  REG_NOTES (insn) = cfa_restores;
24484 	  add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24485 	  RTX_FRAME_RELATED_P (insn) = 1;
24486 	}
24487       return;
24488     }
24489 
24490   /* frame_reg_rtx + frame_off points to the top of this stack frame.  */
24491   if (info->push_p)
24492     frame_off = info->total_size;
24493 
24494   /* Restore AltiVec registers if we must do so before adjusting the
24495      stack.  */
24496   if (TARGET_ALTIVEC_ABI
24497       && info->altivec_size != 0
24498       && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24499 	  || (DEFAULT_ABI != ABI_V4
24500 	      && offset_below_red_zone_p (info->altivec_save_offset))))
24501     {
24502       int i;
24503       int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24504 
24505       gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
24506       if (use_backchain_to_restore_sp)
24507 	{
24508 	  int frame_regno = 11;
24509 
24510 	  if ((strategy & REST_INLINE_VRS) == 0)
24511 	    {
24512 	      /* Of r11 and r12, select the one not clobbered by an
24513 		 out-of-line restore function for the frame register.  */
24514 	      frame_regno = 11 + 12 - scratch_regno;
24515 	    }
24516 	  frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
24517 	  emit_move_insn (frame_reg_rtx,
24518 			  gen_rtx_MEM (Pmode, sp_reg_rtx));
24519 	  frame_off = 0;
24520 	}
24521       else if (frame_pointer_needed)
24522 	frame_reg_rtx = hard_frame_pointer_rtx;
24523 
24524       if ((strategy & REST_INLINE_VRS) == 0)
24525 	{
24526 	  int end_save = info->altivec_save_offset + info->altivec_size;
24527 	  int ptr_off;
24528 	  rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24529 	  rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24530 
24531 	  if (end_save + frame_off != 0)
24532 	    {
24533 	      rtx offset = GEN_INT (end_save + frame_off);
24534 
24535 	      emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24536 	    }
24537 	  else
24538 	    emit_move_insn (ptr_reg, frame_reg_rtx);
24539 
24540 	  ptr_off = -end_save;
24541 	  insn = rs6000_emit_savres_rtx (info, scratch_reg,
24542 					 info->altivec_save_offset + ptr_off,
24543 					 0, V4SImode, SAVRES_VR);
24544 	}
24545       else
24546 	{
24547 	  for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24548 	    if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24549 	      {
24550 		rtx addr, areg, mem, reg;
24551 
24552 		areg = gen_rtx_REG (Pmode, 0);
24553 		emit_move_insn
24554 		  (areg, GEN_INT (info->altivec_save_offset
24555 				  + frame_off
24556 				  + 16 * (i - info->first_altivec_reg_save)));
24557 
24558 		/* AltiVec addressing mode is [reg+reg].  */
24559 		addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24560 		mem = gen_frame_mem (V4SImode, addr);
24561 
24562 		reg = gen_rtx_REG (V4SImode, i);
24563 		/* Rather than emitting a generic move, force use of the
24564 		   lvx instruction, which we always want.  In particular
24565 		   we don't want lxvd2x/xxpermdi for little endian.  */
24566 		(void) emit_insn (gen_altivec_lvx_v4si_internal (reg, mem));
24567 	      }
24568 	}
24569 
24570       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24571 	if (((strategy & REST_INLINE_VRS) == 0
24572 	     || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24573 	    && (flag_shrink_wrap
24574 		|| (offset_below_red_zone_p
24575 		    (info->altivec_save_offset
24576 		     + 16 * (i - info->first_altivec_reg_save)))))
24577 	  {
24578 	    rtx reg = gen_rtx_REG (V4SImode, i);
24579 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24580 	  }
24581     }
24582 
24583   /* Restore VRSAVE if we must do so before adjusting the stack.  */
24584   if (TARGET_ALTIVEC
24585       && TARGET_ALTIVEC_VRSAVE
24586       && info->vrsave_mask != 0
24587       && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24588 	  || (DEFAULT_ABI != ABI_V4
24589 	      && offset_below_red_zone_p (info->vrsave_save_offset))))
24590     {
24591       rtx reg;
24592 
24593       if (frame_reg_rtx == sp_reg_rtx)
24594 	{
24595 	  if (use_backchain_to_restore_sp)
24596 	    {
24597 	      frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24598 	      emit_move_insn (frame_reg_rtx,
24599 			      gen_rtx_MEM (Pmode, sp_reg_rtx));
24600 	      frame_off = 0;
24601 	    }
24602 	  else if (frame_pointer_needed)
24603 	    frame_reg_rtx = hard_frame_pointer_rtx;
24604 	}
24605 
24606       reg = gen_rtx_REG (SImode, 12);
24607       emit_insn (gen_frame_load (reg, frame_reg_rtx,
24608 				 info->vrsave_save_offset + frame_off));
24609 
24610       emit_insn (generate_set_vrsave (reg, info, 1));
24611     }
24612 
24613   insn = NULL_RTX;
24614   /* If we have a large stack frame, restore the old stack pointer
24615      using the backchain.  */
24616   if (use_backchain_to_restore_sp)
24617     {
24618       if (frame_reg_rtx == sp_reg_rtx)
24619 	{
24620 	  /* Under V.4, don't reset the stack pointer until after we're done
24621 	     loading the saved registers.  */
24622 	  if (DEFAULT_ABI == ABI_V4)
24623 	    frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24624 
24625 	  insn = emit_move_insn (frame_reg_rtx,
24626 				 gen_rtx_MEM (Pmode, sp_reg_rtx));
24627 	  frame_off = 0;
24628 	}
24629       else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24630 	       && DEFAULT_ABI == ABI_V4)
24631 	/* frame_reg_rtx has been set up by the altivec restore.  */
24632 	;
24633       else
24634 	{
24635 	  insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
24636 	  frame_reg_rtx = sp_reg_rtx;
24637 	}
24638     }
24639   /* If we have a frame pointer, we can restore the old stack pointer
24640      from it.  */
24641   else if (frame_pointer_needed)
24642     {
24643       frame_reg_rtx = sp_reg_rtx;
24644       if (DEFAULT_ABI == ABI_V4)
24645 	frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24646       /* Prevent reordering memory accesses against stack pointer restore.  */
24647       else if (cfun->calls_alloca
24648 	       || offset_below_red_zone_p (-info->total_size))
24649 	rs6000_emit_stack_tie (frame_reg_rtx, true);
24650 
24651       insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
24652 				       GEN_INT (info->total_size)));
24653       frame_off = 0;
24654     }
24655   else if (info->push_p
24656 	   && DEFAULT_ABI != ABI_V4
24657 	   && !crtl->calls_eh_return)
24658     {
24659       /* Prevent reordering memory accesses against stack pointer restore.  */
24660       if (cfun->calls_alloca
24661 	  || offset_below_red_zone_p (-info->total_size))
24662 	rs6000_emit_stack_tie (frame_reg_rtx, false);
24663       insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
24664 				       GEN_INT (info->total_size)));
24665       frame_off = 0;
24666     }
24667   if (insn && frame_reg_rtx == sp_reg_rtx)
24668     {
24669       if (cfa_restores)
24670 	{
24671 	  REG_NOTES (insn) = cfa_restores;
24672 	  cfa_restores = NULL_RTX;
24673 	}
24674       add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24675       RTX_FRAME_RELATED_P (insn) = 1;
24676     }
24677 
24678   /* Restore AltiVec registers if we have not done so already.  */
24679   if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24680       && TARGET_ALTIVEC_ABI
24681       && info->altivec_size != 0
24682       && (DEFAULT_ABI == ABI_V4
24683 	  || !offset_below_red_zone_p (info->altivec_save_offset)))
24684     {
24685       int i;
24686 
24687       if ((strategy & REST_INLINE_VRS) == 0)
24688 	{
24689 	  int end_save = info->altivec_save_offset + info->altivec_size;
24690 	  int ptr_off;
24691 	  rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24692 	  int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24693 	  rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24694 
24695 	  if (end_save + frame_off != 0)
24696 	    {
24697 	      rtx offset = GEN_INT (end_save + frame_off);
24698 
24699 	      emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24700 	    }
24701 	  else
24702 	    emit_move_insn (ptr_reg, frame_reg_rtx);
24703 
24704 	  ptr_off = -end_save;
24705 	  insn = rs6000_emit_savres_rtx (info, scratch_reg,
24706 					 info->altivec_save_offset + ptr_off,
24707 					 0, V4SImode, SAVRES_VR);
24708 	  if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
24709 	    {
24710 	      /* Frame reg was clobbered by out-of-line save.  Restore it
24711 		 from ptr_reg, and if we are calling out-of-line gpr or
24712 		 fpr restore set up the correct pointer and offset.  */
24713 	      unsigned newptr_regno = 1;
24714 	      if (!restoring_GPRs_inline)
24715 		{
24716 		  bool lr = info->gp_save_offset + info->gp_size == 0;
24717 		  int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24718 		  newptr_regno = ptr_regno_for_savres (sel);
24719 		  end_save = info->gp_save_offset + info->gp_size;
24720 		}
24721 	      else if (!restoring_FPRs_inline)
24722 		{
24723 		  bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
24724 		  int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24725 		  newptr_regno = ptr_regno_for_savres (sel);
24726 		  end_save = info->gp_save_offset + info->gp_size;
24727 		}
24728 
24729 	      if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
24730 		frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
24731 
24732 	      if (end_save + ptr_off != 0)
24733 		{
24734 		  rtx offset = GEN_INT (end_save + ptr_off);
24735 
24736 		  frame_off = -end_save;
24737 		  emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
24738 		}
24739 	      else
24740 		{
24741 		  frame_off = ptr_off;
24742 		  emit_move_insn (frame_reg_rtx, ptr_reg);
24743 		}
24744 	    }
24745 	}
24746       else
24747 	{
24748 	  for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24749 	    if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24750 	      {
24751 		rtx addr, areg, mem, reg;
24752 
24753 		areg = gen_rtx_REG (Pmode, 0);
24754 		emit_move_insn
24755 		  (areg, GEN_INT (info->altivec_save_offset
24756 				  + frame_off
24757 				  + 16 * (i - info->first_altivec_reg_save)));
24758 
24759 		/* AltiVec addressing mode is [reg+reg].  */
24760 		addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24761 		mem = gen_frame_mem (V4SImode, addr);
24762 
24763 		reg = gen_rtx_REG (V4SImode, i);
24764 		/* Rather than emitting a generic move, force use of the
24765 		   lvx instruction, which we always want.  In particular
24766 		   we don't want lxvd2x/xxpermdi for little endian.  */
24767 		(void) emit_insn (gen_altivec_lvx_v4si_internal (reg, mem));
24768 	      }
24769 	}
24770 
24771       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24772 	if (((strategy & REST_INLINE_VRS) == 0
24773 	     || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24774 	    && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
24775 	  {
24776 	    rtx reg = gen_rtx_REG (V4SImode, i);
24777 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24778 	  }
24779     }
24780 
24781   /* Restore VRSAVE if we have not done so already.  */
24782   if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24783       && TARGET_ALTIVEC
24784       && TARGET_ALTIVEC_VRSAVE
24785       && info->vrsave_mask != 0
24786       && (DEFAULT_ABI == ABI_V4
24787 	  || !offset_below_red_zone_p (info->vrsave_save_offset)))
24788     {
24789       rtx reg;
24790 
24791       reg = gen_rtx_REG (SImode, 12);
24792       emit_insn (gen_frame_load (reg, frame_reg_rtx,
24793 				 info->vrsave_save_offset + frame_off));
24794 
24795       emit_insn (generate_set_vrsave (reg, info, 1));
24796     }
24797 
24798   /* If we exit by an out-of-line restore function on ABI_V4 then that
24799      function will deallocate the stack, so we don't need to worry
24800      about the unwinder restoring cr from an invalid stack frame
24801      location.  */
24802   exit_func = (!restoring_FPRs_inline
24803 	       || (!restoring_GPRs_inline
24804 		   && info->first_fp_reg_save == 64));
24805 
24806   /* In the ELFv2 ABI we need to restore all call-saved CR fields from
24807      *separate* slots if the routine calls __builtin_eh_return, so
24808      that they can be independently restored by the unwinder.  */
24809   if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
24810     {
24811       int i, cr_off = info->ehcr_offset;
24812 
24813       for (i = 0; i < 8; i++)
24814 	if (!call_used_regs[CR0_REGNO + i])
24815 	  {
24816 	    rtx reg = gen_rtx_REG (SImode, 0);
24817 	    emit_insn (gen_frame_load (reg, frame_reg_rtx,
24818 				       cr_off + frame_off));
24819 
24820 	    insn = emit_insn (gen_movsi_to_cr_one
24821 				(gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
24822 
24823 	    if (!exit_func && flag_shrink_wrap)
24824 	      {
24825 		add_reg_note (insn, REG_CFA_RESTORE,
24826 			      gen_rtx_REG (SImode, CR0_REGNO + i));
24827 
24828 		RTX_FRAME_RELATED_P (insn) = 1;
24829 	      }
24830 
24831 	    cr_off += reg_size;
24832 	  }
24833     }
24834 
24835   /* Get the old lr if we saved it.  If we are restoring registers
24836      out-of-line, then the out-of-line routines can do this for us.  */
24837   if (restore_lr && restoring_GPRs_inline)
24838     load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24839 
24840   /* Get the old cr if we saved it.  */
24841   if (info->cr_save_p)
24842     {
24843       unsigned cr_save_regno = 12;
24844 
24845       if (!restoring_GPRs_inline)
24846 	{
24847 	  /* Ensure we don't use the register used by the out-of-line
24848 	     gpr register restore below.  */
24849 	  bool lr = info->gp_save_offset + info->gp_size == 0;
24850 	  int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24851 	  int gpr_ptr_regno = ptr_regno_for_savres (sel);
24852 
24853 	  if (gpr_ptr_regno == 12)
24854 	    cr_save_regno = 11;
24855 	  gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
24856 	}
24857       else if (REGNO (frame_reg_rtx) == 12)
24858 	cr_save_regno = 11;
24859 
24860       cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
24861 				  info->cr_save_offset + frame_off,
24862 				  exit_func);
24863     }
24864 
24865   /* Set LR here to try to overlap restores below.  */
24866   if (restore_lr && restoring_GPRs_inline)
24867     restore_saved_lr (0, exit_func);
24868 
24869   /* Load exception handler data registers, if needed.  */
24870   if (crtl->calls_eh_return)
24871     {
24872       unsigned int i, regno;
24873 
24874       if (TARGET_AIX)
24875 	{
24876 	  rtx reg = gen_rtx_REG (reg_mode, 2);
24877 	  emit_insn (gen_frame_load (reg, frame_reg_rtx,
24878 				     frame_off + RS6000_TOC_SAVE_SLOT));
24879 	}
24880 
24881       for (i = 0; ; ++i)
24882 	{
24883 	  rtx mem;
24884 
24885 	  regno = EH_RETURN_DATA_REGNO (i);
24886 	  if (regno == INVALID_REGNUM)
24887 	    break;
24888 
24889 	  /* Note: possible use of r0 here to address SPE regs.  */
24890 	  mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
24891 				      info->ehrd_offset + frame_off
24892 				      + reg_size * (int) i);
24893 
24894 	  emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
24895 	}
24896     }
24897 
24898   /* Restore GPRs.  This is done as a PARALLEL if we are using
24899      the load-multiple instructions.  */
24900   if (TARGET_SPE_ABI
24901       && info->spe_64bit_regs_used
24902       && info->first_gp_reg_save != 32)
24903     {
24904       /* Determine whether we can address all of the registers that need
24905 	 to be saved with an offset from frame_reg_rtx that fits in
24906 	 the small const field for SPE memory instructions.  */
24907       int spe_regs_addressable
24908 	= (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
24909 				+ reg_size * (32 - info->first_gp_reg_save - 1))
24910 	   && restoring_GPRs_inline);
24911 
24912       if (!spe_regs_addressable)
24913 	{
24914 	  int ool_adjust = 0;
24915 	  rtx old_frame_reg_rtx = frame_reg_rtx;
24916 	  /* Make r11 point to the start of the SPE save area.  We worried about
24917 	     not clobbering it when we were saving registers in the prologue.
24918 	     There's no need to worry here because the static chain is passed
24919 	     anew to every function.  */
24920 
24921 	  if (!restoring_GPRs_inline)
24922 	    ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
24923 	  frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24924 	  emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
24925 				 GEN_INT (info->spe_gp_save_offset
24926 					  + frame_off
24927 					  - ool_adjust)));
24928 	  /* Keep the invariant that frame_reg_rtx + frame_off points
24929 	     at the top of the stack frame.  */
24930 	  frame_off = -info->spe_gp_save_offset + ool_adjust;
24931 	}
24932 
24933       if (restoring_GPRs_inline)
24934 	{
24935 	  HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
24936 
24937 	  for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24938 	    if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24939 	      {
24940 		rtx offset, addr, mem, reg;
24941 
24942 		/* We're doing all this to ensure that the immediate offset
24943 		   fits into the immediate field of 'evldd'.  */
24944 		gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
24945 
24946 		offset = GEN_INT (spe_offset + reg_size * i);
24947 		addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
24948 		mem = gen_rtx_MEM (V2SImode, addr);
24949 		reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24950 
24951 		emit_move_insn (reg, mem);
24952 	      }
24953 	}
24954       else
24955 	rs6000_emit_savres_rtx (info, frame_reg_rtx,
24956 				info->spe_gp_save_offset + frame_off,
24957 				info->lr_save_offset + frame_off,
24958 				reg_mode,
24959 				SAVRES_GPR | SAVRES_LR);
24960     }
24961   else if (!restoring_GPRs_inline)
24962     {
24963       /* We are jumping to an out-of-line function.  */
24964       rtx ptr_reg;
24965       int end_save = info->gp_save_offset + info->gp_size;
24966       bool can_use_exit = end_save == 0;
24967       int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
24968       int ptr_off;
24969 
24970       /* Emit stack reset code if we need it.  */
24971       ptr_regno = ptr_regno_for_savres (sel);
24972       ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
24973       if (can_use_exit)
24974 	rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24975       else if (end_save + frame_off != 0)
24976 	emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
24977 				  GEN_INT (end_save + frame_off)));
24978       else if (REGNO (frame_reg_rtx) != ptr_regno)
24979 	emit_move_insn (ptr_reg, frame_reg_rtx);
24980       if (REGNO (frame_reg_rtx) == ptr_regno)
24981 	frame_off = -end_save;
24982 
24983       if (can_use_exit && info->cr_save_p)
24984 	restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
24985 
24986       ptr_off = -end_save;
24987       rs6000_emit_savres_rtx (info, ptr_reg,
24988 			      info->gp_save_offset + ptr_off,
24989 			      info->lr_save_offset + ptr_off,
24990 			      reg_mode, sel);
24991     }
24992   else if (using_load_multiple)
24993     {
24994       rtvec p;
24995       p = rtvec_alloc (32 - info->first_gp_reg_save);
24996       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24997 	RTVEC_ELT (p, i)
24998 	  = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24999 			    frame_reg_rtx,
25000 			    info->gp_save_offset + frame_off + reg_size * i);
25001       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
25002     }
25003   else
25004     {
25005       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
25006 	if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
25007 	  emit_insn (gen_frame_load
25008 		     (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
25009 		      frame_reg_rtx,
25010 		      info->gp_save_offset + frame_off + reg_size * i));
25011     }
25012 
25013   if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
25014     {
25015       /* If the frame pointer was used then we can't delay emitting
25016 	 a REG_CFA_DEF_CFA note.  This must happen on the insn that
25017 	 restores the frame pointer, r31.  We may have already emitted
25018 	 a REG_CFA_DEF_CFA note, but that's OK;  A duplicate is
25019 	 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
25020 	 be harmless if emitted.  */
25021       if (frame_pointer_needed)
25022 	{
25023 	  insn = get_last_insn ();
25024 	  add_reg_note (insn, REG_CFA_DEF_CFA,
25025 			plus_constant (Pmode, frame_reg_rtx, frame_off));
25026 	  RTX_FRAME_RELATED_P (insn) = 1;
25027 	}
25028 
25029       /* Set up cfa_restores.  We always need these when
25030 	 shrink-wrapping.  If not shrink-wrapping then we only need
25031 	 the cfa_restore when the stack location is no longer valid.
25032 	 The cfa_restores must be emitted on or before the insn that
25033 	 invalidates the stack, and of course must not be emitted
25034 	 before the insn that actually does the restore.  The latter
25035 	 is why it is a bad idea to emit the cfa_restores as a group
25036 	 on the last instruction here that actually does a restore:
25037 	 That insn may be reordered with respect to others doing
25038 	 restores.  */
25039       if (flag_shrink_wrap
25040 	  && !restoring_GPRs_inline
25041 	  && info->first_fp_reg_save == 64)
25042 	cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
25043 
25044       for (i = info->first_gp_reg_save; i < 32; i++)
25045 	if (!restoring_GPRs_inline
25046 	    || using_load_multiple
25047 	    || rs6000_reg_live_or_pic_offset_p (i))
25048 	  {
25049 	    rtx reg = gen_rtx_REG (reg_mode, i);
25050 
25051 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
25052 	  }
25053     }
25054 
25055   if (!restoring_GPRs_inline
25056       && info->first_fp_reg_save == 64)
25057     {
25058       /* We are jumping to an out-of-line function.  */
25059       if (cfa_restores)
25060 	emit_cfa_restores (cfa_restores);
25061       return;
25062     }
25063 
25064   if (restore_lr && !restoring_GPRs_inline)
25065     {
25066       load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
25067       restore_saved_lr (0, exit_func);
25068     }
25069 
25070   /* Restore fpr's if we need to do it without calling a function.  */
25071   if (restoring_FPRs_inline)
25072     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
25073       if (save_reg_p (info->first_fp_reg_save + i))
25074 	{
25075 	  rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
25076 				  ? DFmode : SFmode),
25077 				 info->first_fp_reg_save + i);
25078 	  emit_insn (gen_frame_load (reg, frame_reg_rtx,
25079 				     info->fp_save_offset + frame_off + 8 * i));
25080 	  if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
25081 	    cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
25082 	}
25083 
25084   /* If we saved cr, restore it here.  Just those that were used.  */
25085   if (info->cr_save_p)
25086     restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
25087 
25088   /* If this is V.4, unwind the stack pointer after all of the loads
25089      have been done, or set up r11 if we are restoring fp out of line.  */
25090   ptr_regno = 1;
25091   if (!restoring_FPRs_inline)
25092     {
25093       bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
25094       int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
25095       ptr_regno = ptr_regno_for_savres (sel);
25096     }
25097 
25098   insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
25099   if (REGNO (frame_reg_rtx) == ptr_regno)
25100     frame_off = 0;
25101 
25102   if (insn && restoring_FPRs_inline)
25103     {
25104       if (cfa_restores)
25105 	{
25106 	  REG_NOTES (insn) = cfa_restores;
25107 	  cfa_restores = NULL_RTX;
25108 	}
25109       add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
25110       RTX_FRAME_RELATED_P (insn) = 1;
25111     }
25112 
25113   if (crtl->calls_eh_return)
25114     {
25115       rtx sa = EH_RETURN_STACKADJ_RTX;
25116       emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
25117     }
25118 
25119   if (!sibcall)
25120     {
25121       rtvec p;
25122       bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
25123       if (! restoring_FPRs_inline)
25124 	{
25125 	  p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
25126 	  RTVEC_ELT (p, 0) = ret_rtx;
25127 	}
25128       else
25129 	{
25130 	  if (cfa_restores)
25131 	    {
25132 	      /* We can't hang the cfa_restores off a simple return,
25133 		 since the shrink-wrap code sometimes uses an existing
25134 		 return.  This means there might be a path from
25135 		 pre-prologue code to this return, and dwarf2cfi code
25136 		 wants the eh_frame unwinder state to be the same on
25137 		 all paths to any point.  So we need to emit the
25138 		 cfa_restores before the return.  For -m64 we really
25139 		 don't need epilogue cfa_restores at all, except for
25140 		 this irritating dwarf2cfi with shrink-wrap
25141 		 requirement;  The stack red-zone means eh_frame info
25142 		 from the prologue telling the unwinder to restore
25143 		 from the stack is perfectly good right to the end of
25144 		 the function.  */
25145 	      emit_insn (gen_blockage ());
25146 	      emit_cfa_restores (cfa_restores);
25147 	      cfa_restores = NULL_RTX;
25148 	    }
25149 	  p = rtvec_alloc (2);
25150 	  RTVEC_ELT (p, 0) = simple_return_rtx;
25151 	}
25152 
25153       RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
25154 			  ? gen_rtx_USE (VOIDmode,
25155 					 gen_rtx_REG (Pmode, LR_REGNO))
25156 			  : gen_rtx_CLOBBER (VOIDmode,
25157 					     gen_rtx_REG (Pmode, LR_REGNO)));
25158 
25159       /* If we have to restore more than two FP registers, branch to the
25160 	 restore function.  It will return to our caller.  */
25161       if (! restoring_FPRs_inline)
25162 	{
25163 	  int i;
25164 	  int reg;
25165 	  rtx sym;
25166 
25167 	  if (flag_shrink_wrap)
25168 	    cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
25169 
25170 	  sym = rs6000_savres_routine_sym (info,
25171 					   SAVRES_FPR | (lr ? SAVRES_LR : 0));
25172 	  RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
25173 	  reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
25174 	  RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
25175 
25176 	  for (i = 0; i < 64 - info->first_fp_reg_save; i++)
25177 	    {
25178 	      rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
25179 
25180 	      RTVEC_ELT (p, i + 4)
25181 		= gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
25182 	      if (flag_shrink_wrap)
25183 		cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
25184 					       cfa_restores);
25185 	    }
25186 	}
25187 
25188       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
25189     }
25190 
25191   if (cfa_restores)
25192     {
25193       if (sibcall)
25194 	/* Ensure the cfa_restores are hung off an insn that won't
25195 	   be reordered above other restores.  */
25196 	emit_insn (gen_blockage ());
25197 
25198       emit_cfa_restores (cfa_restores);
25199     }
25200 }
25201 
25202 /* Write function epilogue.  */
25203 
25204 static void
rs6000_output_function_epilogue(FILE * file,HOST_WIDE_INT size ATTRIBUTE_UNUSED)25205 rs6000_output_function_epilogue (FILE *file,
25206 				 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
25207 {
25208 #if TARGET_MACHO
25209   macho_branch_islands ();
25210   /* Mach-O doesn't support labels at the end of objects, so if
25211      it looks like we might want one, insert a NOP.  */
25212   {
25213     rtx insn = get_last_insn ();
25214     rtx deleted_debug_label = NULL_RTX;
25215     while (insn
25216 	   && NOTE_P (insn)
25217 	   && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
25218       {
25219 	/* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
25220 	   notes only, instead set their CODE_LABEL_NUMBER to -1,
25221 	   otherwise there would be code generation differences
25222 	   in between -g and -g0.  */
25223 	if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
25224 	  deleted_debug_label = insn;
25225 	insn = PREV_INSN (insn);
25226       }
25227     if (insn
25228 	&& (LABEL_P (insn)
25229 	    || (NOTE_P (insn)
25230 		&& NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
25231       fputs ("\tnop\n", file);
25232     else if (deleted_debug_label)
25233       for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
25234 	if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
25235 	  CODE_LABEL_NUMBER (insn) = -1;
25236   }
25237 #endif
25238 
25239   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
25240      on its format.
25241 
25242      We don't output a traceback table if -finhibit-size-directive was
25243      used.  The documentation for -finhibit-size-directive reads
25244      ``don't output a @code{.size} assembler directive, or anything
25245      else that would cause trouble if the function is split in the
25246      middle, and the two halves are placed at locations far apart in
25247      memory.''  The traceback table has this property, since it
25248      includes the offset from the start of the function to the
25249      traceback table itself.
25250 
25251      System V.4 Powerpc's (and the embedded ABI derived from it) use a
25252      different traceback table.  */
25253   if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25254       && ! flag_inhibit_size_directive
25255       && rs6000_traceback != traceback_none && !cfun->is_thunk)
25256     {
25257       const char *fname = NULL;
25258       const char *language_string = lang_hooks.name;
25259       int fixed_parms = 0, float_parms = 0, parm_info = 0;
25260       int i;
25261       int optional_tbtab;
25262       rs6000_stack_t *info = rs6000_stack_info ();
25263 
25264       if (rs6000_traceback == traceback_full)
25265 	optional_tbtab = 1;
25266       else if (rs6000_traceback == traceback_part)
25267 	optional_tbtab = 0;
25268       else
25269 	optional_tbtab = !optimize_size && !TARGET_ELF;
25270 
25271       if (optional_tbtab)
25272 	{
25273 	  fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
25274 	  while (*fname == '.')	/* V.4 encodes . in the name */
25275 	    fname++;
25276 
25277 	  /* Need label immediately before tbtab, so we can compute
25278 	     its offset from the function start.  */
25279 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
25280 	  ASM_OUTPUT_LABEL (file, fname);
25281 	}
25282 
25283       /* The .tbtab pseudo-op can only be used for the first eight
25284 	 expressions, since it can't handle the possibly variable
25285 	 length fields that follow.  However, if you omit the optional
25286 	 fields, the assembler outputs zeros for all optional fields
25287 	 anyways, giving each variable length field is minimum length
25288 	 (as defined in sys/debug.h).  Thus we can not use the .tbtab
25289 	 pseudo-op at all.  */
25290 
25291       /* An all-zero word flags the start of the tbtab, for debuggers
25292 	 that have to find it by searching forward from the entry
25293 	 point or from the current pc.  */
25294       fputs ("\t.long 0\n", file);
25295 
25296       /* Tbtab format type.  Use format type 0.  */
25297       fputs ("\t.byte 0,", file);
25298 
25299       /* Language type.  Unfortunately, there does not seem to be any
25300 	 official way to discover the language being compiled, so we
25301 	 use language_string.
25302 	 C is 0.  Fortran is 1.  Pascal is 2.  Ada is 3.  C++ is 9.
25303 	 Java is 13.  Objective-C is 14.  Objective-C++ isn't assigned
25304 	 a number, so for now use 9.  LTO and Go aren't assigned numbers
25305 	 either, so for now use 0.  */
25306       if (! strcmp (language_string, "GNU C")
25307 	  || ! strcmp (language_string, "GNU GIMPLE")
25308 	  || ! strcmp (language_string, "GNU Go"))
25309 	i = 0;
25310       else if (! strcmp (language_string, "GNU F77")
25311 	       || ! strcmp (language_string, "GNU Fortran"))
25312 	i = 1;
25313       else if (! strcmp (language_string, "GNU Pascal"))
25314 	i = 2;
25315       else if (! strcmp (language_string, "GNU Ada"))
25316 	i = 3;
25317       else if (! strcmp (language_string, "GNU C++")
25318 	       || ! strcmp (language_string, "GNU Objective-C++"))
25319 	i = 9;
25320       else if (! strcmp (language_string, "GNU Java"))
25321 	i = 13;
25322       else if (! strcmp (language_string, "GNU Objective-C"))
25323 	i = 14;
25324       else
25325 	gcc_unreachable ();
25326       fprintf (file, "%d,", i);
25327 
25328       /* 8 single bit fields: global linkage (not set for C extern linkage,
25329 	 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
25330 	 from start of procedure stored in tbtab, internal function, function
25331 	 has controlled storage, function has no toc, function uses fp,
25332 	 function logs/aborts fp operations.  */
25333       /* Assume that fp operations are used if any fp reg must be saved.  */
25334       fprintf (file, "%d,",
25335 	       (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
25336 
25337       /* 6 bitfields: function is interrupt handler, name present in
25338 	 proc table, function calls alloca, on condition directives
25339 	 (controls stack walks, 3 bits), saves condition reg, saves
25340 	 link reg.  */
25341       /* The `function calls alloca' bit seems to be set whenever reg 31 is
25342 	 set up as a frame pointer, even when there is no alloca call.  */
25343       fprintf (file, "%d,",
25344 	       ((optional_tbtab << 6)
25345 		| ((optional_tbtab & frame_pointer_needed) << 5)
25346 		| (info->cr_save_p << 1)
25347 		| (info->lr_save_p)));
25348 
25349       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
25350 	 (6 bits).  */
25351       fprintf (file, "%d,",
25352 	       (info->push_p << 7) | (64 - info->first_fp_reg_save));
25353 
25354       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
25355       fprintf (file, "%d,", (32 - first_reg_to_save ()));
25356 
25357       if (optional_tbtab)
25358 	{
25359 	  /* Compute the parameter info from the function decl argument
25360 	     list.  */
25361 	  tree decl;
25362 	  int next_parm_info_bit = 31;
25363 
25364 	  for (decl = DECL_ARGUMENTS (current_function_decl);
25365 	       decl; decl = DECL_CHAIN (decl))
25366 	    {
25367 	      rtx parameter = DECL_INCOMING_RTL (decl);
25368 	      enum machine_mode mode = GET_MODE (parameter);
25369 
25370 	      if (GET_CODE (parameter) == REG)
25371 		{
25372 		  if (SCALAR_FLOAT_MODE_P (mode))
25373 		    {
25374 		      int bits;
25375 
25376 		      float_parms++;
25377 
25378 		      switch (mode)
25379 			{
25380 			case SFmode:
25381 			case SDmode:
25382 			  bits = 0x2;
25383 			  break;
25384 
25385 			case DFmode:
25386 			case DDmode:
25387 			case TFmode:
25388 			case TDmode:
25389 			  bits = 0x3;
25390 			  break;
25391 
25392 			default:
25393 			  gcc_unreachable ();
25394 			}
25395 
25396 		      /* If only one bit will fit, don't or in this entry.  */
25397 		      if (next_parm_info_bit > 0)
25398 			parm_info |= (bits << (next_parm_info_bit - 1));
25399 		      next_parm_info_bit -= 2;
25400 		    }
25401 		  else
25402 		    {
25403 		      fixed_parms += ((GET_MODE_SIZE (mode)
25404 				       + (UNITS_PER_WORD - 1))
25405 				      / UNITS_PER_WORD);
25406 		      next_parm_info_bit -= 1;
25407 		    }
25408 		}
25409 	    }
25410 	}
25411 
25412       /* Number of fixed point parameters.  */
25413       /* This is actually the number of words of fixed point parameters; thus
25414 	 an 8 byte struct counts as 2; and thus the maximum value is 8.  */
25415       fprintf (file, "%d,", fixed_parms);
25416 
25417       /* 2 bitfields: number of floating point parameters (7 bits), parameters
25418 	 all on stack.  */
25419       /* This is actually the number of fp registers that hold parameters;
25420 	 and thus the maximum value is 13.  */
25421       /* Set parameters on stack bit if parameters are not in their original
25422 	 registers, regardless of whether they are on the stack?  Xlc
25423 	 seems to set the bit when not optimizing.  */
25424       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
25425 
25426       if (! optional_tbtab)
25427 	return;
25428 
25429       /* Optional fields follow.  Some are variable length.  */
25430 
25431       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
25432 	 11 double float.  */
25433       /* There is an entry for each parameter in a register, in the order that
25434 	 they occur in the parameter list.  Any intervening arguments on the
25435 	 stack are ignored.  If the list overflows a long (max possible length
25436 	 34 bits) then completely leave off all elements that don't fit.  */
25437       /* Only emit this long if there was at least one parameter.  */
25438       if (fixed_parms || float_parms)
25439 	fprintf (file, "\t.long %d\n", parm_info);
25440 
25441       /* Offset from start of code to tb table.  */
25442       fputs ("\t.long ", file);
25443       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
25444       RS6000_OUTPUT_BASENAME (file, fname);
25445       putc ('-', file);
25446       rs6000_output_function_entry (file, fname);
25447       putc ('\n', file);
25448 
25449       /* Interrupt handler mask.  */
25450       /* Omit this long, since we never set the interrupt handler bit
25451 	 above.  */
25452 
25453       /* Number of CTL (controlled storage) anchors.  */
25454       /* Omit this long, since the has_ctl bit is never set above.  */
25455 
25456       /* Displacement into stack of each CTL anchor.  */
25457       /* Omit this list of longs, because there are no CTL anchors.  */
25458 
25459       /* Length of function name.  */
25460       if (*fname == '*')
25461 	++fname;
25462       fprintf (file, "\t.short %d\n", (int) strlen (fname));
25463 
25464       /* Function name.  */
25465       assemble_string (fname, strlen (fname));
25466 
25467       /* Register for alloca automatic storage; this is always reg 31.
25468 	 Only emit this if the alloca bit was set above.  */
25469       if (frame_pointer_needed)
25470 	fputs ("\t.byte 31\n", file);
25471 
25472       fputs ("\t.align 2\n", file);
25473     }
25474 }
25475 
25476 /* A C compound statement that outputs the assembler code for a thunk
25477    function, used to implement C++ virtual function calls with
25478    multiple inheritance.  The thunk acts as a wrapper around a virtual
25479    function, adjusting the implicit object parameter before handing
25480    control off to the real function.
25481 
25482    First, emit code to add the integer DELTA to the location that
25483    contains the incoming first argument.  Assume that this argument
25484    contains a pointer, and is the one used to pass the `this' pointer
25485    in C++.  This is the incoming argument *before* the function
25486    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
25487    values of all other incoming arguments.
25488 
25489    After the addition, emit code to jump to FUNCTION, which is a
25490    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
25491    not touch the return address.  Hence returning from FUNCTION will
25492    return to whoever called the current `thunk'.
25493 
25494    The effect must be as if FUNCTION had been called directly with the
25495    adjusted first argument.  This macro is responsible for emitting
25496    all of the code for a thunk function; output_function_prologue()
25497    and output_function_epilogue() are not invoked.
25498 
25499    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
25500    been extracted from it.)  It might possibly be useful on some
25501    targets, but probably not.
25502 
25503    If you do not define this macro, the target-independent code in the
25504    C++ frontend will generate a less efficient heavyweight thunk that
25505    calls FUNCTION instead of jumping to it.  The generic approach does
25506    not support varargs.  */
25507 
25508 static void
rs6000_output_mi_thunk(FILE * file,tree thunk_fndecl ATTRIBUTE_UNUSED,HOST_WIDE_INT delta,HOST_WIDE_INT vcall_offset,tree function)25509 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
25510 			HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
25511 			tree function)
25512 {
25513   rtx this_rtx, insn, funexp;
25514 
25515   reload_completed = 1;
25516   epilogue_completed = 1;
25517 
25518   /* Mark the end of the (empty) prologue.  */
25519   emit_note (NOTE_INSN_PROLOGUE_END);
25520 
25521   /* Find the "this" pointer.  If the function returns a structure,
25522      the structure return pointer is in r3.  */
25523   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
25524     this_rtx = gen_rtx_REG (Pmode, 4);
25525   else
25526     this_rtx = gen_rtx_REG (Pmode, 3);
25527 
25528   /* Apply the constant offset, if required.  */
25529   if (delta)
25530     emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
25531 
25532   /* Apply the offset from the vtable, if required.  */
25533   if (vcall_offset)
25534     {
25535       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
25536       rtx tmp = gen_rtx_REG (Pmode, 12);
25537 
25538       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
25539       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
25540 	{
25541 	  emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
25542 	  emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
25543 	}
25544       else
25545 	{
25546 	  rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
25547 
25548 	  emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
25549 	}
25550       emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
25551     }
25552 
25553   /* Generate a tail call to the target function.  */
25554   if (!TREE_USED (function))
25555     {
25556       assemble_external (function);
25557       TREE_USED (function) = 1;
25558     }
25559   funexp = XEXP (DECL_RTL (function), 0);
25560   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
25561 
25562 #if TARGET_MACHO
25563   if (MACHOPIC_INDIRECT)
25564     funexp = machopic_indirect_call_target (funexp);
25565 #endif
25566 
25567   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
25568      generate sibcall RTL explicitly.  */
25569   insn = emit_call_insn (
25570 	   gen_rtx_PARALLEL (VOIDmode,
25571 	     gen_rtvec (4,
25572 			gen_rtx_CALL (VOIDmode,
25573 				      funexp, const0_rtx),
25574 			gen_rtx_USE (VOIDmode, const0_rtx),
25575 			gen_rtx_USE (VOIDmode,
25576 				     gen_rtx_REG (SImode,
25577 						  LR_REGNO)),
25578 			simple_return_rtx)));
25579   SIBLING_CALL_P (insn) = 1;
25580   emit_barrier ();
25581 
25582   /* Ensure we have a global entry point for the thunk.   ??? We could
25583      avoid that if the target routine doesn't need a global entry point,
25584      but we do not know whether this is the case at this point.  */
25585   if (DEFAULT_ABI == ABI_ELFv2)
25586     cfun->machine->r2_setup_needed = true;
25587 
25588   /* Run just enough of rest_of_compilation to get the insns emitted.
25589      There's not really enough bulk here to make other passes such as
25590      instruction scheduling worth while.  Note that use_thunk calls
25591      assemble_start_function and assemble_end_function.  */
25592   insn = get_insns ();
25593   shorten_branches (insn);
25594   final_start_function (insn, file, 1);
25595   final (insn, file, 1);
25596   final_end_function ();
25597 
25598   reload_completed = 0;
25599   epilogue_completed = 0;
25600 }
25601 
25602 /* A quick summary of the various types of 'constant-pool tables'
25603    under PowerPC:
25604 
25605    Target	Flags		Name		One table per
25606    AIX		(none)		AIX TOC		object file
25607    AIX		-mfull-toc	AIX TOC		object file
25608    AIX		-mminimal-toc	AIX minimal TOC	translation unit
25609    SVR4/EABI	(none)		SVR4 SDATA	object file
25610    SVR4/EABI	-fpic		SVR4 pic	object file
25611    SVR4/EABI	-fPIC		SVR4 PIC	translation unit
25612    SVR4/EABI	-mrelocatable	EABI TOC	function
25613    SVR4/EABI	-maix		AIX TOC		object file
25614    SVR4/EABI	-maix -mminimal-toc
25615 				AIX minimal TOC	translation unit
25616 
25617    Name			Reg.	Set by	entries	      contains:
25618 					made by	 addrs?	fp?	sum?
25619 
25620    AIX TOC		2	crt0	as	 Y	option	option
25621    AIX minimal TOC	30	prolog	gcc	 Y	Y	option
25622    SVR4 SDATA		13	crt0	gcc	 N	Y	N
25623    SVR4 pic		30	prolog	ld	 Y	not yet	N
25624    SVR4 PIC		30	prolog	gcc	 Y	option	option
25625    EABI TOC		30	prolog	gcc	 Y	option	option
25626 
25627 */
25628 
25629 /* Hash functions for the hash table.  */
25630 
25631 static unsigned
rs6000_hash_constant(rtx k)25632 rs6000_hash_constant (rtx k)
25633 {
25634   enum rtx_code code = GET_CODE (k);
25635   enum machine_mode mode = GET_MODE (k);
25636   unsigned result = (code << 3) ^ mode;
25637   const char *format;
25638   int flen, fidx;
25639 
25640   format = GET_RTX_FORMAT (code);
25641   flen = strlen (format);
25642   fidx = 0;
25643 
25644   switch (code)
25645     {
25646     case LABEL_REF:
25647       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
25648 
25649     case CONST_DOUBLE:
25650       if (mode != VOIDmode)
25651 	return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
25652       flen = 2;
25653       break;
25654 
25655     case CODE_LABEL:
25656       fidx = 3;
25657       break;
25658 
25659     default:
25660       break;
25661     }
25662 
25663   for (; fidx < flen; fidx++)
25664     switch (format[fidx])
25665       {
25666       case 's':
25667 	{
25668 	  unsigned i, len;
25669 	  const char *str = XSTR (k, fidx);
25670 	  len = strlen (str);
25671 	  result = result * 613 + len;
25672 	  for (i = 0; i < len; i++)
25673 	    result = result * 613 + (unsigned) str[i];
25674 	  break;
25675 	}
25676       case 'u':
25677       case 'e':
25678 	result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
25679 	break;
25680       case 'i':
25681       case 'n':
25682 	result = result * 613 + (unsigned) XINT (k, fidx);
25683 	break;
25684       case 'w':
25685 	if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
25686 	  result = result * 613 + (unsigned) XWINT (k, fidx);
25687 	else
25688 	  {
25689 	    size_t i;
25690 	    for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
25691 	      result = result * 613 + (unsigned) (XWINT (k, fidx)
25692 						  >> CHAR_BIT * i);
25693 	  }
25694 	break;
25695       case '0':
25696 	break;
25697       default:
25698 	gcc_unreachable ();
25699       }
25700 
25701   return result;
25702 }
25703 
25704 static unsigned
toc_hash_function(const void * hash_entry)25705 toc_hash_function (const void *hash_entry)
25706 {
25707   const struct toc_hash_struct *thc =
25708     (const struct toc_hash_struct *) hash_entry;
25709   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
25710 }
25711 
25712 /* Compare H1 and H2 for equivalence.  */
25713 
25714 static int
toc_hash_eq(const void * h1,const void * h2)25715 toc_hash_eq (const void *h1, const void *h2)
25716 {
25717   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
25718   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
25719 
25720   if (((const struct toc_hash_struct *) h1)->key_mode
25721       != ((const struct toc_hash_struct *) h2)->key_mode)
25722     return 0;
25723 
25724   return rtx_equal_p (r1, r2);
25725 }
25726 
25727 /* These are the names given by the C++ front-end to vtables, and
25728    vtable-like objects.  Ideally, this logic should not be here;
25729    instead, there should be some programmatic way of inquiring as
25730    to whether or not an object is a vtable.  */
25731 
25732 #define VTABLE_NAME_P(NAME)				\
25733   (strncmp ("_vt.", name, strlen ("_vt.")) == 0		\
25734   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0	\
25735   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0	\
25736   || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0	\
25737   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
25738 
25739 #ifdef NO_DOLLAR_IN_LABEL
25740 /* Return a GGC-allocated character string translating dollar signs in
25741    input NAME to underscores.  Used by XCOFF ASM_OUTPUT_LABELREF.  */
25742 
25743 const char *
rs6000_xcoff_strip_dollar(const char * name)25744 rs6000_xcoff_strip_dollar (const char *name)
25745 {
25746   char *strip, *p;
25747   const char *q;
25748   size_t len;
25749 
25750   q = (const char *) strchr (name, '$');
25751 
25752   if (q == 0 || q == name)
25753     return name;
25754 
25755   len = strlen (name);
25756   strip = XALLOCAVEC (char, len + 1);
25757   strcpy (strip, name);
25758   p = strip + (q - name);
25759   while (p)
25760     {
25761       *p = '_';
25762       p = strchr (p + 1, '$');
25763     }
25764 
25765   return ggc_alloc_string (strip, len);
25766 }
25767 #endif
25768 
25769 void
rs6000_output_symbol_ref(FILE * file,rtx x)25770 rs6000_output_symbol_ref (FILE *file, rtx x)
25771 {
25772   /* Currently C++ toc references to vtables can be emitted before it
25773      is decided whether the vtable is public or private.  If this is
25774      the case, then the linker will eventually complain that there is
25775      a reference to an unknown section.  Thus, for vtables only,
25776      we emit the TOC reference to reference the symbol and not the
25777      section.  */
25778   const char *name = XSTR (x, 0);
25779 
25780   if (VTABLE_NAME_P (name))
25781     {
25782       RS6000_OUTPUT_BASENAME (file, name);
25783     }
25784   else
25785     assemble_name (file, name);
25786 }
25787 
25788 /* Output a TOC entry.  We derive the entry name from what is being
25789    written.  */
25790 
25791 void
output_toc(FILE * file,rtx x,int labelno,enum machine_mode mode)25792 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
25793 {
25794   char buf[256];
25795   const char *name = buf;
25796   rtx base = x;
25797   HOST_WIDE_INT offset = 0;
25798 
25799   gcc_assert (!TARGET_NO_TOC);
25800 
25801   /* When the linker won't eliminate them, don't output duplicate
25802      TOC entries (this happens on AIX if there is any kind of TOC,
25803      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
25804      CODE_LABELs.  */
25805   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
25806     {
25807       struct toc_hash_struct *h;
25808       void * * found;
25809 
25810       /* Create toc_hash_table.  This can't be done at TARGET_OPTION_OVERRIDE
25811 	 time because GGC is not initialized at that point.  */
25812       if (toc_hash_table == NULL)
25813 	toc_hash_table = htab_create_ggc (1021, toc_hash_function,
25814 					  toc_hash_eq, NULL);
25815 
25816       h = ggc_alloc_toc_hash_struct ();
25817       h->key = x;
25818       h->key_mode = mode;
25819       h->labelno = labelno;
25820 
25821       found = htab_find_slot (toc_hash_table, h, INSERT);
25822       if (*found == NULL)
25823 	*found = h;
25824       else  /* This is indeed a duplicate.
25825 	       Set this label equal to that label.  */
25826 	{
25827 	  fputs ("\t.set ", file);
25828 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25829 	  fprintf (file, "%d,", labelno);
25830 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25831 	  fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
25832 					      found)->labelno));
25833 
25834 #ifdef HAVE_AS_TLS
25835 	  if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
25836 	      && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
25837 		  || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
25838 	    {
25839 	      fputs ("\t.set ", file);
25840 	      ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25841 	      fprintf (file, "%d,", labelno);
25842 	      ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25843 	      fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
25844 			       			      found)->labelno));
25845 	    }
25846 #endif
25847 	  return;
25848 	}
25849     }
25850 
25851   /* If we're going to put a double constant in the TOC, make sure it's
25852      aligned properly when strict alignment is on.  */
25853   if (GET_CODE (x) == CONST_DOUBLE
25854       && STRICT_ALIGNMENT
25855       && GET_MODE_BITSIZE (mode) >= 64
25856       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
25857     ASM_OUTPUT_ALIGN (file, 3);
25858   }
25859 
25860   (*targetm.asm_out.internal_label) (file, "LC", labelno);
25861 
25862   /* Handle FP constants specially.  Note that if we have a minimal
25863      TOC, things we put here aren't actually in the TOC, so we can allow
25864      FP constants.  */
25865   if (GET_CODE (x) == CONST_DOUBLE &&
25866       (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
25867     {
25868       REAL_VALUE_TYPE rv;
25869       long k[4];
25870 
25871       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25872       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25873 	REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
25874       else
25875 	REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
25876 
25877       if (TARGET_64BIT)
25878 	{
25879 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
25880 	    fputs (DOUBLE_INT_ASM_OP, file);
25881 	  else
25882 	    fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25883 		     k[0] & 0xffffffff, k[1] & 0xffffffff,
25884 		     k[2] & 0xffffffff, k[3] & 0xffffffff);
25885 	  fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
25886 		   k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25887 		   k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
25888 		   k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
25889 		   k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
25890 	  return;
25891 	}
25892       else
25893 	{
25894 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
25895 	    fputs ("\t.long ", file);
25896 	  else
25897 	    fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25898 		     k[0] & 0xffffffff, k[1] & 0xffffffff,
25899 		     k[2] & 0xffffffff, k[3] & 0xffffffff);
25900 	  fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
25901 		   k[0] & 0xffffffff, k[1] & 0xffffffff,
25902 		   k[2] & 0xffffffff, k[3] & 0xffffffff);
25903 	  return;
25904 	}
25905     }
25906   else if (GET_CODE (x) == CONST_DOUBLE &&
25907 	   (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
25908     {
25909       REAL_VALUE_TYPE rv;
25910       long k[2];
25911 
25912       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25913 
25914       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25915 	REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
25916       else
25917 	REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
25918 
25919       if (TARGET_64BIT)
25920 	{
25921 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
25922 	    fputs (DOUBLE_INT_ASM_OP, file);
25923 	  else
25924 	    fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25925 		     k[0] & 0xffffffff, k[1] & 0xffffffff);
25926 	  fprintf (file, "0x%lx%08lx\n",
25927 		   k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25928 		   k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
25929 	  return;
25930 	}
25931       else
25932 	{
25933 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
25934 	    fputs ("\t.long ", file);
25935 	  else
25936 	    fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25937 		     k[0] & 0xffffffff, k[1] & 0xffffffff);
25938 	  fprintf (file, "0x%lx,0x%lx\n",
25939 		   k[0] & 0xffffffff, k[1] & 0xffffffff);
25940 	  return;
25941 	}
25942     }
25943   else if (GET_CODE (x) == CONST_DOUBLE &&
25944 	   (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
25945     {
25946       REAL_VALUE_TYPE rv;
25947       long l;
25948 
25949       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25950       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25951 	REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
25952       else
25953 	REAL_VALUE_TO_TARGET_SINGLE (rv, l);
25954 
25955       if (TARGET_64BIT)
25956 	{
25957 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
25958 	    fputs (DOUBLE_INT_ASM_OP, file);
25959 	  else
25960 	    fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25961 	  if (WORDS_BIG_ENDIAN)
25962 	    fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
25963 	  else
25964 	    fprintf (file, "0x%lx\n", l & 0xffffffff);
25965 	  return;
25966 	}
25967       else
25968 	{
25969 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
25970 	    fputs ("\t.long ", file);
25971 	  else
25972 	    fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25973 	  fprintf (file, "0x%lx\n", l & 0xffffffff);
25974 	  return;
25975 	}
25976     }
25977   else if (GET_MODE (x) == VOIDmode
25978 	   && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
25979     {
25980       unsigned HOST_WIDE_INT low;
25981       HOST_WIDE_INT high;
25982 
25983       if (GET_CODE (x) == CONST_DOUBLE)
25984 	{
25985 	  low = CONST_DOUBLE_LOW (x);
25986 	  high = CONST_DOUBLE_HIGH (x);
25987 	}
25988       else
25989 #if HOST_BITS_PER_WIDE_INT == 32
25990 	{
25991 	  low = INTVAL (x);
25992 	  high = (low & 0x80000000) ? ~0 : 0;
25993 	}
25994 #else
25995 	{
25996 	  low = INTVAL (x) & 0xffffffff;
25997 	  high = (HOST_WIDE_INT) INTVAL (x) >> 32;
25998 	}
25999 #endif
26000 
26001       /* TOC entries are always Pmode-sized, so when big-endian
26002 	 smaller integer constants in the TOC need to be padded.
26003 	 (This is still a win over putting the constants in
26004 	 a separate constant pool, because then we'd have
26005 	 to have both a TOC entry _and_ the actual constant.)
26006 
26007 	 For a 32-bit target, CONST_INT values are loaded and shifted
26008 	 entirely within `low' and can be stored in one TOC entry.  */
26009 
26010       /* It would be easy to make this work, but it doesn't now.  */
26011       gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
26012 
26013       if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
26014 	{
26015 #if HOST_BITS_PER_WIDE_INT == 32
26016 	  lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
26017 			 POINTER_SIZE, &low, &high, 0);
26018 #else
26019 	  low |= high << 32;
26020 	  low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
26021 	  high = (HOST_WIDE_INT) low >> 32;
26022 	  low &= 0xffffffff;
26023 #endif
26024 	}
26025 
26026       if (TARGET_64BIT)
26027 	{
26028 	  if (TARGET_ELF || TARGET_MINIMAL_TOC)
26029 	    fputs (DOUBLE_INT_ASM_OP, file);
26030 	  else
26031 	    fprintf (file, "\t.tc ID_%lx_%lx[TC],",
26032 		     (long) high & 0xffffffff, (long) low & 0xffffffff);
26033 	  fprintf (file, "0x%lx%08lx\n",
26034 		   (long) high & 0xffffffff, (long) low & 0xffffffff);
26035 	  return;
26036 	}
26037       else
26038 	{
26039 	  if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
26040 	    {
26041 	      if (TARGET_ELF || TARGET_MINIMAL_TOC)
26042 		fputs ("\t.long ", file);
26043 	      else
26044 		fprintf (file, "\t.tc ID_%lx_%lx[TC],",
26045 			 (long) high & 0xffffffff, (long) low & 0xffffffff);
26046 	      fprintf (file, "0x%lx,0x%lx\n",
26047 		       (long) high & 0xffffffff, (long) low & 0xffffffff);
26048 	    }
26049 	  else
26050 	    {
26051 	      if (TARGET_ELF || TARGET_MINIMAL_TOC)
26052 		fputs ("\t.long ", file);
26053 	      else
26054 		fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
26055 	      fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
26056 	    }
26057 	  return;
26058 	}
26059     }
26060 
26061   if (GET_CODE (x) == CONST)
26062     {
26063       gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
26064 		  && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
26065 
26066       base = XEXP (XEXP (x, 0), 0);
26067       offset = INTVAL (XEXP (XEXP (x, 0), 1));
26068     }
26069 
26070   switch (GET_CODE (base))
26071     {
26072     case SYMBOL_REF:
26073       name = XSTR (base, 0);
26074       break;
26075 
26076     case LABEL_REF:
26077       ASM_GENERATE_INTERNAL_LABEL (buf, "L",
26078 				   CODE_LABEL_NUMBER (XEXP (base, 0)));
26079       break;
26080 
26081     case CODE_LABEL:
26082       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
26083       break;
26084 
26085     default:
26086       gcc_unreachable ();
26087     }
26088 
26089   if (TARGET_ELF || TARGET_MINIMAL_TOC)
26090     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
26091   else
26092     {
26093       fputs ("\t.tc ", file);
26094       RS6000_OUTPUT_BASENAME (file, name);
26095 
26096       if (offset < 0)
26097 	fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
26098       else if (offset)
26099 	fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
26100 
26101       /* Mark large TOC symbols on AIX with [TE] so they are mapped
26102 	 after other TOC symbols, reducing overflow of small TOC access
26103 	 to [TC] symbols.  */
26104       fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
26105 	     ? "[TE]," : "[TC],", file);
26106     }
26107 
26108   /* Currently C++ toc references to vtables can be emitted before it
26109      is decided whether the vtable is public or private.  If this is
26110      the case, then the linker will eventually complain that there is
26111      a TOC reference to an unknown section.  Thus, for vtables only,
26112      we emit the TOC reference to reference the symbol and not the
26113      section.  */
26114   if (VTABLE_NAME_P (name))
26115     {
26116       RS6000_OUTPUT_BASENAME (file, name);
26117       if (offset < 0)
26118 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
26119       else if (offset > 0)
26120 	fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
26121     }
26122   else
26123     output_addr_const (file, x);
26124 
26125 #if HAVE_AS_TLS
26126   if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
26127       && SYMBOL_REF_TLS_MODEL (base) != 0)
26128     {
26129       if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
26130 	fputs ("@le", file);
26131       else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
26132 	fputs ("@ie", file);
26133       /* Use global-dynamic for local-dynamic.  */
26134       else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
26135 	       || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
26136 	{
26137 	  putc ('\n', file);
26138 	  (*targetm.asm_out.internal_label) (file, "LCM", labelno);
26139 	  fputs ("\t.tc .", file);
26140 	  RS6000_OUTPUT_BASENAME (file, name);
26141 	  fputs ("[TC],", file);
26142 	  output_addr_const (file, x);
26143 	  fputs ("@m", file);
26144 	}
26145     }
26146 #endif
26147 
26148   putc ('\n', file);
26149 }
26150 
26151 /* Output an assembler pseudo-op to write an ASCII string of N characters
26152    starting at P to FILE.
26153 
26154    On the RS/6000, we have to do this using the .byte operation and
26155    write out special characters outside the quoted string.
26156    Also, the assembler is broken; very long strings are truncated,
26157    so we must artificially break them up early.  */
26158 
26159 void
output_ascii(FILE * file,const char * p,int n)26160 output_ascii (FILE *file, const char *p, int n)
26161 {
26162   char c;
26163   int i, count_string;
26164   const char *for_string = "\t.byte \"";
26165   const char *for_decimal = "\t.byte ";
26166   const char *to_close = NULL;
26167 
26168   count_string = 0;
26169   for (i = 0; i < n; i++)
26170     {
26171       c = *p++;
26172       if (c >= ' ' && c < 0177)
26173 	{
26174 	  if (for_string)
26175 	    fputs (for_string, file);
26176 	  putc (c, file);
26177 
26178 	  /* Write two quotes to get one.  */
26179 	  if (c == '"')
26180 	    {
26181 	      putc (c, file);
26182 	      ++count_string;
26183 	    }
26184 
26185 	  for_string = NULL;
26186 	  for_decimal = "\"\n\t.byte ";
26187 	  to_close = "\"\n";
26188 	  ++count_string;
26189 
26190 	  if (count_string >= 512)
26191 	    {
26192 	      fputs (to_close, file);
26193 
26194 	      for_string = "\t.byte \"";
26195 	      for_decimal = "\t.byte ";
26196 	      to_close = NULL;
26197 	      count_string = 0;
26198 	    }
26199 	}
26200       else
26201 	{
26202 	  if (for_decimal)
26203 	    fputs (for_decimal, file);
26204 	  fprintf (file, "%d", c);
26205 
26206 	  for_string = "\n\t.byte \"";
26207 	  for_decimal = ", ";
26208 	  to_close = "\n";
26209 	  count_string = 0;
26210 	}
26211     }
26212 
26213   /* Now close the string if we have written one.  Then end the line.  */
26214   if (to_close)
26215     fputs (to_close, file);
26216 }
26217 
26218 /* Generate a unique section name for FILENAME for a section type
26219    represented by SECTION_DESC.  Output goes into BUF.
26220 
26221    SECTION_DESC can be any string, as long as it is different for each
26222    possible section type.
26223 
26224    We name the section in the same manner as xlc.  The name begins with an
26225    underscore followed by the filename (after stripping any leading directory
26226    names) with the last period replaced by the string SECTION_DESC.  If
26227    FILENAME does not contain a period, SECTION_DESC is appended to the end of
26228    the name.  */
26229 
26230 void
rs6000_gen_section_name(char ** buf,const char * filename,const char * section_desc)26231 rs6000_gen_section_name (char **buf, const char *filename,
26232 			 const char *section_desc)
26233 {
26234   const char *q, *after_last_slash, *last_period = 0;
26235   char *p;
26236   int len;
26237 
26238   after_last_slash = filename;
26239   for (q = filename; *q; q++)
26240     {
26241       if (*q == '/')
26242 	after_last_slash = q + 1;
26243       else if (*q == '.')
26244 	last_period = q;
26245     }
26246 
26247   len = strlen (after_last_slash) + strlen (section_desc) + 2;
26248   *buf = (char *) xmalloc (len);
26249 
26250   p = *buf;
26251   *p++ = '_';
26252 
26253   for (q = after_last_slash; *q; q++)
26254     {
26255       if (q == last_period)
26256 	{
26257 	  strcpy (p, section_desc);
26258 	  p += strlen (section_desc);
26259 	  break;
26260 	}
26261 
26262       else if (ISALNUM (*q))
26263 	*p++ = *q;
26264     }
26265 
26266   if (last_period == 0)
26267     strcpy (p, section_desc);
26268   else
26269     *p = '\0';
26270 }
26271 
26272 /* Emit profile function.  */
26273 
26274 void
output_profile_hook(int labelno ATTRIBUTE_UNUSED)26275 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
26276 {
26277   /* Non-standard profiling for kernels, which just saves LR then calls
26278      _mcount without worrying about arg saves.  The idea is to change
26279      the function prologue as little as possible as it isn't easy to
26280      account for arg save/restore code added just for _mcount.  */
26281   if (TARGET_PROFILE_KERNEL)
26282     return;
26283 
26284   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
26285     {
26286 #ifndef NO_PROFILE_COUNTERS
26287 # define NO_PROFILE_COUNTERS 0
26288 #endif
26289       if (NO_PROFILE_COUNTERS)
26290 	emit_library_call (init_one_libfunc (RS6000_MCOUNT),
26291 			   LCT_NORMAL, VOIDmode, 0);
26292       else
26293 	{
26294 	  char buf[30];
26295 	  const char *label_name;
26296 	  rtx fun;
26297 
26298 	  ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
26299 	  label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
26300 	  fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
26301 
26302 	  emit_library_call (init_one_libfunc (RS6000_MCOUNT),
26303 			     LCT_NORMAL, VOIDmode, 1, fun, Pmode);
26304 	}
26305     }
26306   else if (DEFAULT_ABI == ABI_DARWIN)
26307     {
26308       const char *mcount_name = RS6000_MCOUNT;
26309       int caller_addr_regno = LR_REGNO;
26310 
26311       /* Be conservative and always set this, at least for now.  */
26312       crtl->uses_pic_offset_table = 1;
26313 
26314 #if TARGET_MACHO
26315       /* For PIC code, set up a stub and collect the caller's address
26316 	 from r0, which is where the prologue puts it.  */
26317       if (MACHOPIC_INDIRECT
26318 	  && crtl->uses_pic_offset_table)
26319 	caller_addr_regno = 0;
26320 #endif
26321       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
26322 			 LCT_NORMAL, VOIDmode, 1,
26323 			 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
26324     }
26325 }
26326 
26327 /* Write function profiler code.  */
26328 
26329 void
output_function_profiler(FILE * file,int labelno)26330 output_function_profiler (FILE *file, int labelno)
26331 {
26332   char buf[100];
26333 
26334   switch (DEFAULT_ABI)
26335     {
26336     default:
26337       gcc_unreachable ();
26338 
26339     case ABI_V4:
26340       if (!TARGET_32BIT)
26341 	{
26342 	  warning (0, "no profiling of 64-bit code for this ABI");
26343 	  return;
26344 	}
26345       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
26346       fprintf (file, "\tmflr %s\n", reg_names[0]);
26347       if (NO_PROFILE_COUNTERS)
26348 	{
26349 	  asm_fprintf (file, "\tstw %s,4(%s)\n",
26350 		       reg_names[0], reg_names[1]);
26351 	}
26352       else if (TARGET_SECURE_PLT && flag_pic)
26353 	{
26354 	  if (TARGET_LINK_STACK)
26355 	    {
26356 	      char name[32];
26357 	      get_ppc476_thunk_name (name);
26358 	      asm_fprintf (file, "\tbl %s\n", name);
26359 	    }
26360 	  else
26361 	    asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
26362 	  asm_fprintf (file, "\tstw %s,4(%s)\n",
26363 		       reg_names[0], reg_names[1]);
26364 	  asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
26365 	  asm_fprintf (file, "\taddis %s,%s,",
26366 		       reg_names[12], reg_names[12]);
26367 	  assemble_name (file, buf);
26368 	  asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
26369 	  assemble_name (file, buf);
26370 	  asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
26371 	}
26372       else if (flag_pic == 1)
26373 	{
26374 	  fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
26375 	  asm_fprintf (file, "\tstw %s,4(%s)\n",
26376 		       reg_names[0], reg_names[1]);
26377 	  asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
26378 	  asm_fprintf (file, "\tlwz %s,", reg_names[0]);
26379 	  assemble_name (file, buf);
26380 	  asm_fprintf (file, "@got(%s)\n", reg_names[12]);
26381 	}
26382       else if (flag_pic > 1)
26383 	{
26384 	  asm_fprintf (file, "\tstw %s,4(%s)\n",
26385 		       reg_names[0], reg_names[1]);
26386 	  /* Now, we need to get the address of the label.  */
26387 	  if (TARGET_LINK_STACK)
26388 	    {
26389 	      char name[32];
26390 	      get_ppc476_thunk_name (name);
26391 	      asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
26392 	      assemble_name (file, buf);
26393 	      fputs ("-.\n1:", file);
26394 	      asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
26395 	      asm_fprintf (file, "\taddi %s,%s,4\n",
26396 			   reg_names[11], reg_names[11]);
26397 	    }
26398 	  else
26399 	    {
26400 	      fputs ("\tbcl 20,31,1f\n\t.long ", file);
26401 	      assemble_name (file, buf);
26402 	      fputs ("-.\n1:", file);
26403 	      asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
26404 	    }
26405 	  asm_fprintf (file, "\tlwz %s,0(%s)\n",
26406 		       reg_names[0], reg_names[11]);
26407 	  asm_fprintf (file, "\tadd %s,%s,%s\n",
26408 		       reg_names[0], reg_names[0], reg_names[11]);
26409 	}
26410       else
26411 	{
26412 	  asm_fprintf (file, "\tlis %s,", reg_names[12]);
26413 	  assemble_name (file, buf);
26414 	  fputs ("@ha\n", file);
26415 	  asm_fprintf (file, "\tstw %s,4(%s)\n",
26416 		       reg_names[0], reg_names[1]);
26417 	  asm_fprintf (file, "\tla %s,", reg_names[0]);
26418 	  assemble_name (file, buf);
26419 	  asm_fprintf (file, "@l(%s)\n", reg_names[12]);
26420 	}
26421 
26422       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
26423       fprintf (file, "\tbl %s%s\n",
26424 	       RS6000_MCOUNT, flag_pic ? "@plt" : "");
26425       break;
26426 
26427     case ABI_AIX:
26428     case ABI_ELFv2:
26429     case ABI_DARWIN:
26430       /* Don't do anything, done in output_profile_hook ().  */
26431       break;
26432     }
26433 }
26434 
26435 
26436 
26437 /* The following variable value is the last issued insn.  */
26438 
26439 static rtx last_scheduled_insn;
26440 
26441 /* The following variable helps to balance issuing of load and
26442    store instructions */
26443 
26444 static int load_store_pendulum;
26445 
26446 /* Power4 load update and store update instructions are cracked into a
26447    load or store and an integer insn which are executed in the same cycle.
26448    Branches have their own dispatch slot which does not count against the
26449    GCC issue rate, but it changes the program flow so there are no other
26450    instructions to issue in this cycle.  */
26451 
26452 static int
rs6000_variable_issue_1(rtx insn,int more)26453 rs6000_variable_issue_1 (rtx insn, int more)
26454 {
26455   last_scheduled_insn = insn;
26456   if (GET_CODE (PATTERN (insn)) == USE
26457       || GET_CODE (PATTERN (insn)) == CLOBBER)
26458     {
26459       cached_can_issue_more = more;
26460       return cached_can_issue_more;
26461     }
26462 
26463   if (insn_terminates_group_p (insn, current_group))
26464     {
26465       cached_can_issue_more = 0;
26466       return cached_can_issue_more;
26467     }
26468 
26469   /* If no reservation, but reach here */
26470   if (recog_memoized (insn) < 0)
26471     return more;
26472 
26473   if (rs6000_sched_groups)
26474     {
26475       if (is_microcoded_insn (insn))
26476         cached_can_issue_more = 0;
26477       else if (is_cracked_insn (insn))
26478         cached_can_issue_more = more > 2 ? more - 2 : 0;
26479       else
26480         cached_can_issue_more = more - 1;
26481 
26482       return cached_can_issue_more;
26483     }
26484 
26485   if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
26486     return 0;
26487 
26488   cached_can_issue_more = more - 1;
26489   return cached_can_issue_more;
26490 }
26491 
26492 static int
rs6000_variable_issue(FILE * stream,int verbose,rtx insn,int more)26493 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
26494 {
26495   int r = rs6000_variable_issue_1 (insn, more);
26496   if (verbose)
26497     fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
26498   return r;
26499 }
26500 
26501 /* Adjust the cost of a scheduling dependency.  Return the new cost of
26502    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
26503 
26504 static int
rs6000_adjust_cost(rtx insn,rtx link,rtx dep_insn,int cost)26505 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
26506 {
26507   enum attr_type attr_type;
26508 
26509   if (! recog_memoized (insn))
26510     return 0;
26511 
26512   switch (REG_NOTE_KIND (link))
26513     {
26514     case REG_DEP_TRUE:
26515       {
26516         /* Data dependency; DEP_INSN writes a register that INSN reads
26517 	   some cycles later.  */
26518 
26519 	/* Separate a load from a narrower, dependent store.  */
26520 	if (rs6000_sched_groups
26521 	    && GET_CODE (PATTERN (insn)) == SET
26522 	    && GET_CODE (PATTERN (dep_insn)) == SET
26523 	    && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
26524 	    && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
26525 	    && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
26526 		> GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
26527 	  return cost + 14;
26528 
26529         attr_type = get_attr_type (insn);
26530 
26531         switch (attr_type)
26532           {
26533           case TYPE_JMPREG:
26534             /* Tell the first scheduling pass about the latency between
26535                a mtctr and bctr (and mtlr and br/blr).  The first
26536                scheduling pass will not know about this latency since
26537                the mtctr instruction, which has the latency associated
26538                to it, will be generated by reload.  */
26539             return 4;
26540           case TYPE_BRANCH:
26541             /* Leave some extra cycles between a compare and its
26542                dependent branch, to inhibit expensive mispredicts.  */
26543             if ((rs6000_cpu_attr == CPU_PPC603
26544                  || rs6000_cpu_attr == CPU_PPC604
26545                  || rs6000_cpu_attr == CPU_PPC604E
26546                  || rs6000_cpu_attr == CPU_PPC620
26547                  || rs6000_cpu_attr == CPU_PPC630
26548                  || rs6000_cpu_attr == CPU_PPC750
26549                  || rs6000_cpu_attr == CPU_PPC7400
26550                  || rs6000_cpu_attr == CPU_PPC7450
26551                  || rs6000_cpu_attr == CPU_PPCE5500
26552                  || rs6000_cpu_attr == CPU_PPCE6500
26553                  || rs6000_cpu_attr == CPU_POWER4
26554                  || rs6000_cpu_attr == CPU_POWER5
26555 		 || rs6000_cpu_attr == CPU_POWER7
26556 		 || rs6000_cpu_attr == CPU_POWER8
26557                  || rs6000_cpu_attr == CPU_CELL)
26558                 && recog_memoized (dep_insn)
26559                 && (INSN_CODE (dep_insn) >= 0))
26560 
26561               switch (get_attr_type (dep_insn))
26562                 {
26563                 case TYPE_CMP:
26564                 case TYPE_COMPARE:
26565                 case TYPE_DELAYED_COMPARE:
26566                 case TYPE_IMUL_COMPARE:
26567                 case TYPE_LMUL_COMPARE:
26568                 case TYPE_FPCOMPARE:
26569                 case TYPE_CR_LOGICAL:
26570                 case TYPE_DELAYED_CR:
26571 		  return cost + 2;
26572 		default:
26573 		  break;
26574 		}
26575             break;
26576 
26577           case TYPE_STORE:
26578           case TYPE_STORE_U:
26579           case TYPE_STORE_UX:
26580           case TYPE_FPSTORE:
26581           case TYPE_FPSTORE_U:
26582           case TYPE_FPSTORE_UX:
26583             if ((rs6000_cpu == PROCESSOR_POWER6)
26584                 && recog_memoized (dep_insn)
26585                 && (INSN_CODE (dep_insn) >= 0))
26586               {
26587 
26588                 if (GET_CODE (PATTERN (insn)) != SET)
26589                   /* If this happens, we have to extend this to schedule
26590                      optimally.  Return default for now.  */
26591                   return cost;
26592 
26593                 /* Adjust the cost for the case where the value written
26594                    by a fixed point operation is used as the address
26595                    gen value on a store. */
26596                 switch (get_attr_type (dep_insn))
26597                   {
26598                   case TYPE_LOAD:
26599                   case TYPE_LOAD_U:
26600                   case TYPE_LOAD_UX:
26601                   case TYPE_CNTLZ:
26602                     {
26603                       if (! store_data_bypass_p (dep_insn, insn))
26604                         return 4;
26605                       break;
26606                     }
26607                   case TYPE_LOAD_EXT:
26608                   case TYPE_LOAD_EXT_U:
26609                   case TYPE_LOAD_EXT_UX:
26610                   case TYPE_VAR_SHIFT_ROTATE:
26611                   case TYPE_VAR_DELAYED_COMPARE:
26612                     {
26613                       if (! store_data_bypass_p (dep_insn, insn))
26614                         return 6;
26615                       break;
26616 		    }
26617                   case TYPE_INTEGER:
26618                   case TYPE_COMPARE:
26619                   case TYPE_FAST_COMPARE:
26620                   case TYPE_EXTS:
26621                   case TYPE_SHIFT:
26622                   case TYPE_INSERT_WORD:
26623                   case TYPE_INSERT_DWORD:
26624                   case TYPE_FPLOAD_U:
26625                   case TYPE_FPLOAD_UX:
26626                   case TYPE_STORE_U:
26627                   case TYPE_STORE_UX:
26628                   case TYPE_FPSTORE_U:
26629                   case TYPE_FPSTORE_UX:
26630                     {
26631                       if (! store_data_bypass_p (dep_insn, insn))
26632                         return 3;
26633                       break;
26634                     }
26635                   case TYPE_IMUL:
26636                   case TYPE_IMUL2:
26637                   case TYPE_IMUL3:
26638                   case TYPE_LMUL:
26639                   case TYPE_IMUL_COMPARE:
26640                   case TYPE_LMUL_COMPARE:
26641                     {
26642                       if (! store_data_bypass_p (dep_insn, insn))
26643                         return 17;
26644                       break;
26645                     }
26646                   case TYPE_IDIV:
26647                     {
26648                       if (! store_data_bypass_p (dep_insn, insn))
26649                         return 45;
26650                       break;
26651                     }
26652                   case TYPE_LDIV:
26653                     {
26654                       if (! store_data_bypass_p (dep_insn, insn))
26655                         return 57;
26656                       break;
26657                     }
26658                   default:
26659                     break;
26660                   }
26661               }
26662 	    break;
26663 
26664           case TYPE_LOAD:
26665           case TYPE_LOAD_U:
26666           case TYPE_LOAD_UX:
26667           case TYPE_LOAD_EXT:
26668           case TYPE_LOAD_EXT_U:
26669           case TYPE_LOAD_EXT_UX:
26670             if ((rs6000_cpu == PROCESSOR_POWER6)
26671                 && recog_memoized (dep_insn)
26672                 && (INSN_CODE (dep_insn) >= 0))
26673               {
26674 
26675                 /* Adjust the cost for the case where the value written
26676                    by a fixed point instruction is used within the address
26677                    gen portion of a subsequent load(u)(x) */
26678                 switch (get_attr_type (dep_insn))
26679                   {
26680                   case TYPE_LOAD:
26681                   case TYPE_LOAD_U:
26682                   case TYPE_LOAD_UX:
26683                   case TYPE_CNTLZ:
26684                     {
26685                       if (set_to_load_agen (dep_insn, insn))
26686                         return 4;
26687                       break;
26688                     }
26689                   case TYPE_LOAD_EXT:
26690                   case TYPE_LOAD_EXT_U:
26691                   case TYPE_LOAD_EXT_UX:
26692                   case TYPE_VAR_SHIFT_ROTATE:
26693                   case TYPE_VAR_DELAYED_COMPARE:
26694                     {
26695                       if (set_to_load_agen (dep_insn, insn))
26696                         return 6;
26697                       break;
26698                     }
26699                   case TYPE_INTEGER:
26700                   case TYPE_COMPARE:
26701                   case TYPE_FAST_COMPARE:
26702                   case TYPE_EXTS:
26703                   case TYPE_SHIFT:
26704                   case TYPE_INSERT_WORD:
26705                   case TYPE_INSERT_DWORD:
26706                   case TYPE_FPLOAD_U:
26707                   case TYPE_FPLOAD_UX:
26708                   case TYPE_STORE_U:
26709                   case TYPE_STORE_UX:
26710                   case TYPE_FPSTORE_U:
26711                   case TYPE_FPSTORE_UX:
26712                     {
26713                       if (set_to_load_agen (dep_insn, insn))
26714                         return 3;
26715                       break;
26716                     }
26717                   case TYPE_IMUL:
26718                   case TYPE_IMUL2:
26719                   case TYPE_IMUL3:
26720                   case TYPE_LMUL:
26721                   case TYPE_IMUL_COMPARE:
26722                   case TYPE_LMUL_COMPARE:
26723                     {
26724                       if (set_to_load_agen (dep_insn, insn))
26725                         return 17;
26726                       break;
26727                     }
26728                   case TYPE_IDIV:
26729                     {
26730                       if (set_to_load_agen (dep_insn, insn))
26731                         return 45;
26732                       break;
26733                     }
26734                   case TYPE_LDIV:
26735                     {
26736                       if (set_to_load_agen (dep_insn, insn))
26737                         return 57;
26738                       break;
26739                     }
26740                   default:
26741                     break;
26742                   }
26743               }
26744             break;
26745 
26746           case TYPE_FPLOAD:
26747             if ((rs6000_cpu == PROCESSOR_POWER6)
26748                 && recog_memoized (dep_insn)
26749                 && (INSN_CODE (dep_insn) >= 0)
26750                 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
26751               return 2;
26752 
26753           default:
26754             break;
26755           }
26756 
26757 	/* Fall out to return default cost.  */
26758       }
26759       break;
26760 
26761     case REG_DEP_OUTPUT:
26762       /* Output dependency; DEP_INSN writes a register that INSN writes some
26763 	 cycles later.  */
26764       if ((rs6000_cpu == PROCESSOR_POWER6)
26765           && recog_memoized (dep_insn)
26766           && (INSN_CODE (dep_insn) >= 0))
26767         {
26768           attr_type = get_attr_type (insn);
26769 
26770           switch (attr_type)
26771             {
26772             case TYPE_FP:
26773               if (get_attr_type (dep_insn) == TYPE_FP)
26774                 return 1;
26775               break;
26776             case TYPE_FPLOAD:
26777               if (get_attr_type (dep_insn) == TYPE_MFFGPR)
26778                 return 2;
26779               break;
26780             default:
26781               break;
26782             }
26783         }
26784     case REG_DEP_ANTI:
26785       /* Anti dependency; DEP_INSN reads a register that INSN writes some
26786 	 cycles later.  */
26787       return 0;
26788 
26789     default:
26790       gcc_unreachable ();
26791     }
26792 
26793   return cost;
26794 }
26795 
26796 /* Debug version of rs6000_adjust_cost.  */
26797 
26798 static int
rs6000_debug_adjust_cost(rtx insn,rtx link,rtx dep_insn,int cost)26799 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
26800 {
26801   int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
26802 
26803   if (ret != cost)
26804     {
26805       const char *dep;
26806 
26807       switch (REG_NOTE_KIND (link))
26808 	{
26809 	default:	     dep = "unknown depencency"; break;
26810 	case REG_DEP_TRUE:   dep = "data dependency";	 break;
26811 	case REG_DEP_OUTPUT: dep = "output dependency";  break;
26812 	case REG_DEP_ANTI:   dep = "anti depencency";	 break;
26813 	}
26814 
26815       fprintf (stderr,
26816 	       "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
26817 	       "%s, insn:\n", ret, cost, dep);
26818 
26819       debug_rtx (insn);
26820     }
26821 
26822   return ret;
26823 }
26824 
26825 /* The function returns a true if INSN is microcoded.
26826    Return false otherwise.  */
26827 
26828 static bool
is_microcoded_insn(rtx insn)26829 is_microcoded_insn (rtx insn)
26830 {
26831   if (!insn || !NONDEBUG_INSN_P (insn)
26832       || GET_CODE (PATTERN (insn)) == USE
26833       || GET_CODE (PATTERN (insn)) == CLOBBER)
26834     return false;
26835 
26836   if (rs6000_cpu_attr == CPU_CELL)
26837     return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
26838 
26839   if (rs6000_sched_groups
26840       && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26841     {
26842       enum attr_type type = get_attr_type (insn);
26843       if (type == TYPE_LOAD_EXT_U
26844 	  || type == TYPE_LOAD_EXT_UX
26845 	  || type == TYPE_LOAD_UX
26846 	  || type == TYPE_STORE_UX
26847 	  || type == TYPE_MFCR)
26848 	return true;
26849     }
26850 
26851   return false;
26852 }
26853 
26854 /* The function returns true if INSN is cracked into 2 instructions
26855    by the processor (and therefore occupies 2 issue slots).  */
26856 
26857 static bool
is_cracked_insn(rtx insn)26858 is_cracked_insn (rtx insn)
26859 {
26860   if (!insn || !NONDEBUG_INSN_P (insn)
26861       || GET_CODE (PATTERN (insn)) == USE
26862       || GET_CODE (PATTERN (insn)) == CLOBBER)
26863     return false;
26864 
26865   if (rs6000_sched_groups
26866       && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26867     {
26868       enum attr_type type = get_attr_type (insn);
26869       if (type == TYPE_LOAD_U || type == TYPE_STORE_U
26870 	  || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
26871 	  || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
26872 	  || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
26873 	  || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
26874 	  || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
26875 	  || type == TYPE_IDIV || type == TYPE_LDIV
26876 	  || type == TYPE_INSERT_WORD)
26877 	return true;
26878     }
26879 
26880   return false;
26881 }
26882 
26883 /* The function returns true if INSN can be issued only from
26884    the branch slot.  */
26885 
26886 static bool
is_branch_slot_insn(rtx insn)26887 is_branch_slot_insn (rtx insn)
26888 {
26889   if (!insn || !NONDEBUG_INSN_P (insn)
26890       || GET_CODE (PATTERN (insn)) == USE
26891       || GET_CODE (PATTERN (insn)) == CLOBBER)
26892     return false;
26893 
26894   if (rs6000_sched_groups)
26895     {
26896       enum attr_type type = get_attr_type (insn);
26897       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
26898 	return true;
26899       return false;
26900     }
26901 
26902   return false;
26903 }
26904 
26905 /* The function returns true if out_inst sets a value that is
26906    used in the address generation computation of in_insn */
26907 static bool
set_to_load_agen(rtx out_insn,rtx in_insn)26908 set_to_load_agen (rtx out_insn, rtx in_insn)
26909 {
26910   rtx out_set, in_set;
26911 
26912   /* For performance reasons, only handle the simple case where
26913      both loads are a single_set. */
26914   out_set = single_set (out_insn);
26915   if (out_set)
26916     {
26917       in_set = single_set (in_insn);
26918       if (in_set)
26919         return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
26920     }
26921 
26922   return false;
26923 }
26924 
26925 /* Try to determine base/offset/size parts of the given MEM.
26926    Return true if successful, false if all the values couldn't
26927    be determined.
26928 
26929    This function only looks for REG or REG+CONST address forms.
26930    REG+REG address form will return false. */
26931 
26932 static bool
get_memref_parts(rtx mem,rtx * base,HOST_WIDE_INT * offset,HOST_WIDE_INT * size)26933 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
26934 		  HOST_WIDE_INT *size)
26935 {
26936   rtx addr_rtx;
26937   if MEM_SIZE_KNOWN_P (mem)
26938     *size = MEM_SIZE (mem);
26939   else
26940     return false;
26941 
26942   if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
26943     addr_rtx = XEXP (XEXP (mem, 0), 1);
26944   else
26945     addr_rtx = (XEXP (mem, 0));
26946 
26947   if (GET_CODE (addr_rtx) == REG)
26948     {
26949       *base = addr_rtx;
26950       *offset = 0;
26951     }
26952   else if (GET_CODE (addr_rtx) == PLUS
26953 	   && CONST_INT_P (XEXP (addr_rtx, 1)))
26954     {
26955       *base = XEXP (addr_rtx, 0);
26956       *offset = INTVAL (XEXP (addr_rtx, 1));
26957     }
26958   else
26959     return false;
26960 
26961   return true;
26962 }
26963 
26964 /* The function returns true if the target storage location of
26965    mem1 is adjacent to the target storage location of mem2 */
26966 /* Return 1 if memory locations are adjacent.  */
26967 
26968 static bool
adjacent_mem_locations(rtx mem1,rtx mem2)26969 adjacent_mem_locations (rtx mem1, rtx mem2)
26970 {
26971   rtx reg1, reg2;
26972   HOST_WIDE_INT off1, size1, off2, size2;
26973 
26974   if (get_memref_parts (mem1, &reg1, &off1, &size1)
26975       && get_memref_parts (mem2, &reg2, &off2, &size2))
26976     return ((REGNO (reg1) == REGNO (reg2))
26977 	    && ((off1 + size1 == off2)
26978 		|| (off2 + size2 == off1)));
26979 
26980   return false;
26981 }
26982 
26983 /* This function returns true if it can be determined that the two MEM
26984    locations overlap by at least 1 byte based on base reg/offset/size. */
26985 
26986 static bool
mem_locations_overlap(rtx mem1,rtx mem2)26987 mem_locations_overlap (rtx mem1, rtx mem2)
26988 {
26989   rtx reg1, reg2;
26990   HOST_WIDE_INT off1, size1, off2, size2;
26991 
26992   if (get_memref_parts (mem1, &reg1, &off1, &size1)
26993       && get_memref_parts (mem2, &reg2, &off2, &size2))
26994     return ((REGNO (reg1) == REGNO (reg2))
26995 	    && (((off1 <= off2) && (off1 + size1 > off2))
26996 		|| ((off2 <= off1) && (off2 + size2 > off1))));
26997 
26998   return false;
26999 }
27000 
27001 /* A C statement (sans semicolon) to update the integer scheduling
27002    priority INSN_PRIORITY (INSN). Increase the priority to execute the
27003    INSN earlier, reduce the priority to execute INSN later.  Do not
27004    define this macro if you do not need to adjust the scheduling
27005    priorities of insns.  */
27006 
27007 static int
rs6000_adjust_priority(rtx insn ATTRIBUTE_UNUSED,int priority)27008 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
27009 {
27010   rtx load_mem, str_mem;
27011   /* On machines (like the 750) which have asymmetric integer units,
27012      where one integer unit can do multiply and divides and the other
27013      can't, reduce the priority of multiply/divide so it is scheduled
27014      before other integer operations.  */
27015 
27016 #if 0
27017   if (! INSN_P (insn))
27018     return priority;
27019 
27020   if (GET_CODE (PATTERN (insn)) == USE)
27021     return priority;
27022 
27023   switch (rs6000_cpu_attr) {
27024   case CPU_PPC750:
27025     switch (get_attr_type (insn))
27026       {
27027       default:
27028 	break;
27029 
27030       case TYPE_IMUL:
27031       case TYPE_IDIV:
27032 	fprintf (stderr, "priority was %#x (%d) before adjustment\n",
27033 		 priority, priority);
27034 	if (priority >= 0 && priority < 0x01000000)
27035 	  priority >>= 3;
27036 	break;
27037       }
27038   }
27039 #endif
27040 
27041   if (insn_must_be_first_in_group (insn)
27042       && reload_completed
27043       && current_sched_info->sched_max_insns_priority
27044       && rs6000_sched_restricted_insns_priority)
27045     {
27046 
27047       /* Prioritize insns that can be dispatched only in the first
27048 	 dispatch slot.  */
27049       if (rs6000_sched_restricted_insns_priority == 1)
27050 	/* Attach highest priority to insn. This means that in
27051 	   haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
27052 	   precede 'priority' (critical path) considerations.  */
27053 	return current_sched_info->sched_max_insns_priority;
27054       else if (rs6000_sched_restricted_insns_priority == 2)
27055 	/* Increase priority of insn by a minimal amount. This means that in
27056 	   haifa-sched.c:ready_sort(), only 'priority' (critical path)
27057 	   considerations precede dispatch-slot restriction considerations.  */
27058 	return (priority + 1);
27059     }
27060 
27061   if (rs6000_cpu == PROCESSOR_POWER6
27062       && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
27063           || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
27064     /* Attach highest priority to insn if the scheduler has just issued two
27065        stores and this instruction is a load, or two loads and this instruction
27066        is a store. Power6 wants loads and stores scheduled alternately
27067        when possible */
27068     return current_sched_info->sched_max_insns_priority;
27069 
27070   return priority;
27071 }
27072 
27073 /* Return true if the instruction is nonpipelined on the Cell. */
27074 static bool
is_nonpipeline_insn(rtx insn)27075 is_nonpipeline_insn (rtx insn)
27076 {
27077   enum attr_type type;
27078   if (!insn || !NONDEBUG_INSN_P (insn)
27079       || GET_CODE (PATTERN (insn)) == USE
27080       || GET_CODE (PATTERN (insn)) == CLOBBER)
27081     return false;
27082 
27083   type = get_attr_type (insn);
27084   if (type == TYPE_IMUL
27085       || type == TYPE_IMUL2
27086       || type == TYPE_IMUL3
27087       || type == TYPE_LMUL
27088       || type == TYPE_IDIV
27089       || type == TYPE_LDIV
27090       || type == TYPE_SDIV
27091       || type == TYPE_DDIV
27092       || type == TYPE_SSQRT
27093       || type == TYPE_DSQRT
27094       || type == TYPE_MFCR
27095       || type == TYPE_MFCRF
27096       || type == TYPE_MFJMPR)
27097     {
27098       return true;
27099     }
27100   return false;
27101 }
27102 
27103 
27104 /* Return how many instructions the machine can issue per cycle.  */
27105 
27106 static int
rs6000_issue_rate(void)27107 rs6000_issue_rate (void)
27108 {
27109   /* Unless scheduling for register pressure, use issue rate of 1 for
27110      first scheduling pass to decrease degradation.  */
27111   if (!reload_completed && !flag_sched_pressure)
27112     return 1;
27113 
27114   switch (rs6000_cpu_attr) {
27115   case CPU_RS64A:
27116   case CPU_PPC601: /* ? */
27117   case CPU_PPC7450:
27118     return 3;
27119   case CPU_PPC440:
27120   case CPU_PPC603:
27121   case CPU_PPC750:
27122   case CPU_PPC7400:
27123   case CPU_PPC8540:
27124   case CPU_PPC8548:
27125   case CPU_CELL:
27126   case CPU_PPCE300C2:
27127   case CPU_PPCE300C3:
27128   case CPU_PPCE500MC:
27129   case CPU_PPCE500MC64:
27130   case CPU_PPCE5500:
27131   case CPU_PPCE6500:
27132   case CPU_TITAN:
27133     return 2;
27134   case CPU_PPC476:
27135   case CPU_PPC604:
27136   case CPU_PPC604E:
27137   case CPU_PPC620:
27138   case CPU_PPC630:
27139     return 4;
27140   case CPU_POWER4:
27141   case CPU_POWER5:
27142   case CPU_POWER6:
27143   case CPU_POWER7:
27144     return 5;
27145   case CPU_POWER8:
27146     return 7;
27147   default:
27148     return 1;
27149   }
27150 }
27151 
27152 /* Return how many instructions to look ahead for better insn
27153    scheduling.  */
27154 
27155 static int
rs6000_use_sched_lookahead(void)27156 rs6000_use_sched_lookahead (void)
27157 {
27158   switch (rs6000_cpu_attr)
27159     {
27160     case CPU_PPC8540:
27161     case CPU_PPC8548:
27162       return 4;
27163 
27164     case CPU_CELL:
27165       return (reload_completed ? 8 : 0);
27166 
27167     default:
27168       return 0;
27169     }
27170 }
27171 
27172 /* We are choosing insn from the ready queue.  Return nonzero if INSN can be chosen.  */
27173 static int
rs6000_use_sched_lookahead_guard(rtx insn)27174 rs6000_use_sched_lookahead_guard (rtx insn)
27175 {
27176   if (rs6000_cpu_attr != CPU_CELL)
27177     return 1;
27178 
27179    if (insn == NULL_RTX || !INSN_P (insn))
27180      abort ();
27181 
27182   if (!reload_completed
27183       || is_nonpipeline_insn (insn)
27184       || is_microcoded_insn (insn))
27185     return 0;
27186 
27187   return 1;
27188 }
27189 
27190 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
27191    and return true.  */
27192 
27193 static bool
find_mem_ref(rtx pat,rtx * mem_ref)27194 find_mem_ref (rtx pat, rtx *mem_ref)
27195 {
27196   const char * fmt;
27197   int i, j;
27198 
27199   /* stack_tie does not produce any real memory traffic.  */
27200   if (tie_operand (pat, VOIDmode))
27201     return false;
27202 
27203   if (GET_CODE (pat) == MEM)
27204     {
27205       *mem_ref = pat;
27206       return true;
27207     }
27208 
27209   /* Recursively process the pattern.  */
27210   fmt = GET_RTX_FORMAT (GET_CODE (pat));
27211 
27212   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
27213     {
27214       if (fmt[i] == 'e')
27215 	{
27216 	  if (find_mem_ref (XEXP (pat, i), mem_ref))
27217 	    return true;
27218 	}
27219       else if (fmt[i] == 'E')
27220 	for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
27221 	  {
27222 	    if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
27223 	      return true;
27224 	  }
27225     }
27226 
27227   return false;
27228 }
27229 
27230 /* Determine if PAT is a PATTERN of a load insn.  */
27231 
27232 static bool
is_load_insn1(rtx pat,rtx * load_mem)27233 is_load_insn1 (rtx pat, rtx *load_mem)
27234 {
27235   if (!pat || pat == NULL_RTX)
27236     return false;
27237 
27238   if (GET_CODE (pat) == SET)
27239     return find_mem_ref (SET_SRC (pat), load_mem);
27240 
27241   if (GET_CODE (pat) == PARALLEL)
27242     {
27243       int i;
27244 
27245       for (i = 0; i < XVECLEN (pat, 0); i++)
27246 	if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
27247 	  return true;
27248     }
27249 
27250   return false;
27251 }
27252 
27253 /* Determine if INSN loads from memory.  */
27254 
27255 static bool
is_load_insn(rtx insn,rtx * load_mem)27256 is_load_insn (rtx insn, rtx *load_mem)
27257 {
27258   if (!insn || !INSN_P (insn))
27259     return false;
27260 
27261   if (GET_CODE (insn) == CALL_INSN)
27262     return false;
27263 
27264   return is_load_insn1 (PATTERN (insn), load_mem);
27265 }
27266 
27267 /* Determine if PAT is a PATTERN of a store insn.  */
27268 
27269 static bool
is_store_insn1(rtx pat,rtx * str_mem)27270 is_store_insn1 (rtx pat, rtx *str_mem)
27271 {
27272   if (!pat || pat == NULL_RTX)
27273     return false;
27274 
27275   if (GET_CODE (pat) == SET)
27276     return find_mem_ref (SET_DEST (pat), str_mem);
27277 
27278   if (GET_CODE (pat) == PARALLEL)
27279     {
27280       int i;
27281 
27282       for (i = 0; i < XVECLEN (pat, 0); i++)
27283 	if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
27284 	  return true;
27285     }
27286 
27287   return false;
27288 }
27289 
27290 /* Determine if INSN stores to memory.  */
27291 
27292 static bool
is_store_insn(rtx insn,rtx * str_mem)27293 is_store_insn (rtx insn, rtx *str_mem)
27294 {
27295   if (!insn || !INSN_P (insn))
27296     return false;
27297 
27298   return is_store_insn1 (PATTERN (insn), str_mem);
27299 }
27300 
27301 /* Returns whether the dependence between INSN and NEXT is considered
27302    costly by the given target.  */
27303 
27304 static bool
rs6000_is_costly_dependence(dep_t dep,int cost,int distance)27305 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
27306 {
27307   rtx insn;
27308   rtx next;
27309   rtx load_mem, str_mem;
27310 
27311   /* If the flag is not enabled - no dependence is considered costly;
27312      allow all dependent insns in the same group.
27313      This is the most aggressive option.  */
27314   if (rs6000_sched_costly_dep == no_dep_costly)
27315     return false;
27316 
27317   /* If the flag is set to 1 - a dependence is always considered costly;
27318      do not allow dependent instructions in the same group.
27319      This is the most conservative option.  */
27320   if (rs6000_sched_costly_dep == all_deps_costly)
27321     return true;
27322 
27323   insn = DEP_PRO (dep);
27324   next = DEP_CON (dep);
27325 
27326   if (rs6000_sched_costly_dep == store_to_load_dep_costly
27327       && is_load_insn (next, &load_mem)
27328       && is_store_insn (insn, &str_mem))
27329     /* Prevent load after store in the same group.  */
27330     return true;
27331 
27332   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
27333       && is_load_insn (next, &load_mem)
27334       && is_store_insn (insn, &str_mem)
27335       && DEP_TYPE (dep) == REG_DEP_TRUE
27336       && mem_locations_overlap(str_mem, load_mem))
27337      /* Prevent load after store in the same group if it is a true
27338 	dependence.  */
27339      return true;
27340 
27341   /* The flag is set to X; dependences with latency >= X are considered costly,
27342      and will not be scheduled in the same group.  */
27343   if (rs6000_sched_costly_dep <= max_dep_latency
27344       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
27345     return true;
27346 
27347   return false;
27348 }
27349 
27350 /* Return the next insn after INSN that is found before TAIL is reached,
27351    skipping any "non-active" insns - insns that will not actually occupy
27352    an issue slot.  Return NULL_RTX if such an insn is not found.  */
27353 
27354 static rtx
get_next_active_insn(rtx insn,rtx tail)27355 get_next_active_insn (rtx insn, rtx tail)
27356 {
27357   if (insn == NULL_RTX || insn == tail)
27358     return NULL_RTX;
27359 
27360   while (1)
27361     {
27362       insn = NEXT_INSN (insn);
27363       if (insn == NULL_RTX || insn == tail)
27364 	return NULL_RTX;
27365 
27366       if (CALL_P (insn)
27367 	  || JUMP_P (insn)
27368 	  || (NONJUMP_INSN_P (insn)
27369 	      && GET_CODE (PATTERN (insn)) != USE
27370 	      && GET_CODE (PATTERN (insn)) != CLOBBER
27371 	      && INSN_CODE (insn) != CODE_FOR_stack_tie))
27372 	break;
27373     }
27374   return insn;
27375 }
27376 
27377 /* We are about to begin issuing insns for this clock cycle. */
27378 
27379 static int
rs6000_sched_reorder(FILE * dump ATTRIBUTE_UNUSED,int sched_verbose,rtx * ready ATTRIBUTE_UNUSED,int * pn_ready ATTRIBUTE_UNUSED,int clock_var ATTRIBUTE_UNUSED)27380 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
27381                         rtx *ready ATTRIBUTE_UNUSED,
27382                         int *pn_ready ATTRIBUTE_UNUSED,
27383 		        int clock_var ATTRIBUTE_UNUSED)
27384 {
27385   int n_ready = *pn_ready;
27386 
27387   if (sched_verbose)
27388     fprintf (dump, "// rs6000_sched_reorder :\n");
27389 
27390   /* Reorder the ready list, if the second to last ready insn
27391      is a nonepipeline insn.  */
27392   if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
27393   {
27394     if (is_nonpipeline_insn (ready[n_ready - 1])
27395         && (recog_memoized (ready[n_ready - 2]) > 0))
27396       /* Simply swap first two insns.  */
27397       {
27398 	rtx tmp = ready[n_ready - 1];
27399 	ready[n_ready - 1] = ready[n_ready - 2];
27400 	ready[n_ready - 2] = tmp;
27401       }
27402   }
27403 
27404   if (rs6000_cpu == PROCESSOR_POWER6)
27405     load_store_pendulum = 0;
27406 
27407   return rs6000_issue_rate ();
27408 }
27409 
27410 /* Like rs6000_sched_reorder, but called after issuing each insn.  */
27411 
27412 static int
rs6000_sched_reorder2(FILE * dump,int sched_verbose,rtx * ready,int * pn_ready,int clock_var ATTRIBUTE_UNUSED)27413 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
27414 		         int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
27415 {
27416   if (sched_verbose)
27417     fprintf (dump, "// rs6000_sched_reorder2 :\n");
27418 
27419   /* For Power6, we need to handle some special cases to try and keep the
27420      store queue from overflowing and triggering expensive flushes.
27421 
27422      This code monitors how load and store instructions are being issued
27423      and skews the ready list one way or the other to increase the likelihood
27424      that a desired instruction is issued at the proper time.
27425 
27426      A couple of things are done.  First, we maintain a "load_store_pendulum"
27427      to track the current state of load/store issue.
27428 
27429        - If the pendulum is at zero, then no loads or stores have been
27430          issued in the current cycle so we do nothing.
27431 
27432        - If the pendulum is 1, then a single load has been issued in this
27433          cycle and we attempt to locate another load in the ready list to
27434          issue with it.
27435 
27436        - If the pendulum is -2, then two stores have already been
27437          issued in this cycle, so we increase the priority of the first load
27438          in the ready list to increase it's likelihood of being chosen first
27439          in the next cycle.
27440 
27441        - If the pendulum is -1, then a single store has been issued in this
27442          cycle and we attempt to locate another store in the ready list to
27443          issue with it, preferring a store to an adjacent memory location to
27444          facilitate store pairing in the store queue.
27445 
27446        - If the pendulum is 2, then two loads have already been
27447          issued in this cycle, so we increase the priority of the first store
27448          in the ready list to increase it's likelihood of being chosen first
27449          in the next cycle.
27450 
27451        - If the pendulum < -2 or > 2, then do nothing.
27452 
27453        Note: This code covers the most common scenarios.  There exist non
27454              load/store instructions which make use of the LSU and which
27455              would need to be accounted for to strictly model the behavior
27456              of the machine.  Those instructions are currently unaccounted
27457              for to help minimize compile time overhead of this code.
27458    */
27459   if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
27460     {
27461       int pos;
27462       int i;
27463       rtx tmp, load_mem, str_mem;
27464 
27465       if (is_store_insn (last_scheduled_insn, &str_mem))
27466         /* Issuing a store, swing the load_store_pendulum to the left */
27467         load_store_pendulum--;
27468       else if (is_load_insn (last_scheduled_insn, &load_mem))
27469         /* Issuing a load, swing the load_store_pendulum to the right */
27470         load_store_pendulum++;
27471       else
27472         return cached_can_issue_more;
27473 
27474       /* If the pendulum is balanced, or there is only one instruction on
27475          the ready list, then all is well, so return. */
27476       if ((load_store_pendulum == 0) || (*pn_ready <= 1))
27477         return cached_can_issue_more;
27478 
27479       if (load_store_pendulum == 1)
27480         {
27481           /* A load has been issued in this cycle.  Scan the ready list
27482              for another load to issue with it */
27483           pos = *pn_ready-1;
27484 
27485           while (pos >= 0)
27486             {
27487               if (is_load_insn (ready[pos], &load_mem))
27488                 {
27489                   /* Found a load.  Move it to the head of the ready list,
27490                      and adjust it's priority so that it is more likely to
27491                      stay there */
27492                   tmp = ready[pos];
27493                   for (i=pos; i<*pn_ready-1; i++)
27494                     ready[i] = ready[i + 1];
27495                   ready[*pn_ready-1] = tmp;
27496 
27497                   if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27498                     INSN_PRIORITY (tmp)++;
27499                   break;
27500                 }
27501               pos--;
27502             }
27503         }
27504       else if (load_store_pendulum == -2)
27505         {
27506           /* Two stores have been issued in this cycle.  Increase the
27507              priority of the first load in the ready list to favor it for
27508              issuing in the next cycle. */
27509           pos = *pn_ready-1;
27510 
27511           while (pos >= 0)
27512             {
27513               if (is_load_insn (ready[pos], &load_mem)
27514                   && !sel_sched_p ()
27515 		  && INSN_PRIORITY_KNOWN (ready[pos]))
27516                 {
27517                   INSN_PRIORITY (ready[pos])++;
27518 
27519                   /* Adjust the pendulum to account for the fact that a load
27520                      was found and increased in priority.  This is to prevent
27521                      increasing the priority of multiple loads */
27522                   load_store_pendulum--;
27523 
27524                   break;
27525                 }
27526               pos--;
27527             }
27528         }
27529       else if (load_store_pendulum == -1)
27530         {
27531           /* A store has been issued in this cycle.  Scan the ready list for
27532              another store to issue with it, preferring a store to an adjacent
27533              memory location */
27534           int first_store_pos = -1;
27535 
27536           pos = *pn_ready-1;
27537 
27538           while (pos >= 0)
27539             {
27540               if (is_store_insn (ready[pos], &str_mem))
27541                 {
27542 		  rtx str_mem2;
27543                   /* Maintain the index of the first store found on the
27544                      list */
27545                   if (first_store_pos == -1)
27546                     first_store_pos = pos;
27547 
27548                   if (is_store_insn (last_scheduled_insn, &str_mem2)
27549                       && adjacent_mem_locations (str_mem, str_mem2))
27550                     {
27551                       /* Found an adjacent store.  Move it to the head of the
27552                          ready list, and adjust it's priority so that it is
27553                          more likely to stay there */
27554                       tmp = ready[pos];
27555                       for (i=pos; i<*pn_ready-1; i++)
27556                         ready[i] = ready[i + 1];
27557                       ready[*pn_ready-1] = tmp;
27558 
27559                       if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27560                         INSN_PRIORITY (tmp)++;
27561 
27562                       first_store_pos = -1;
27563 
27564                       break;
27565                     };
27566                 }
27567               pos--;
27568             }
27569 
27570           if (first_store_pos >= 0)
27571             {
27572               /* An adjacent store wasn't found, but a non-adjacent store was,
27573                  so move the non-adjacent store to the front of the ready
27574                  list, and adjust its priority so that it is more likely to
27575                  stay there. */
27576               tmp = ready[first_store_pos];
27577               for (i=first_store_pos; i<*pn_ready-1; i++)
27578                 ready[i] = ready[i + 1];
27579               ready[*pn_ready-1] = tmp;
27580               if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27581                 INSN_PRIORITY (tmp)++;
27582             }
27583         }
27584       else if (load_store_pendulum == 2)
27585        {
27586            /* Two loads have been issued in this cycle.  Increase the priority
27587               of the first store in the ready list to favor it for issuing in
27588               the next cycle. */
27589           pos = *pn_ready-1;
27590 
27591           while (pos >= 0)
27592             {
27593               if (is_store_insn (ready[pos], &str_mem)
27594                   && !sel_sched_p ()
27595 		  && INSN_PRIORITY_KNOWN (ready[pos]))
27596                 {
27597                   INSN_PRIORITY (ready[pos])++;
27598 
27599                   /* Adjust the pendulum to account for the fact that a store
27600                      was found and increased in priority.  This is to prevent
27601                      increasing the priority of multiple stores */
27602                   load_store_pendulum++;
27603 
27604                   break;
27605                 }
27606               pos--;
27607             }
27608         }
27609     }
27610 
27611   return cached_can_issue_more;
27612 }
27613 
27614 /* Return whether the presence of INSN causes a dispatch group termination
27615    of group WHICH_GROUP.
27616 
27617    If WHICH_GROUP == current_group, this function will return true if INSN
27618    causes the termination of the current group (i.e, the dispatch group to
27619    which INSN belongs). This means that INSN will be the last insn in the
27620    group it belongs to.
27621 
27622    If WHICH_GROUP == previous_group, this function will return true if INSN
27623    causes the termination of the previous group (i.e, the dispatch group that
27624    precedes the group to which INSN belongs).  This means that INSN will be
27625    the first insn in the group it belongs to).  */
27626 
27627 static bool
insn_terminates_group_p(rtx insn,enum group_termination which_group)27628 insn_terminates_group_p (rtx insn, enum group_termination which_group)
27629 {
27630   bool first, last;
27631 
27632   if (! insn)
27633     return false;
27634 
27635   first = insn_must_be_first_in_group (insn);
27636   last = insn_must_be_last_in_group (insn);
27637 
27638   if (first && last)
27639     return true;
27640 
27641   if (which_group == current_group)
27642     return last;
27643   else if (which_group == previous_group)
27644     return first;
27645 
27646   return false;
27647 }
27648 
27649 
27650 static bool
insn_must_be_first_in_group(rtx insn)27651 insn_must_be_first_in_group (rtx insn)
27652 {
27653   enum attr_type type;
27654 
27655   if (!insn
27656       || GET_CODE (insn) == NOTE
27657       || DEBUG_INSN_P (insn)
27658       || GET_CODE (PATTERN (insn)) == USE
27659       || GET_CODE (PATTERN (insn)) == CLOBBER)
27660     return false;
27661 
27662   switch (rs6000_cpu)
27663     {
27664     case PROCESSOR_POWER5:
27665       if (is_cracked_insn (insn))
27666         return true;
27667     case PROCESSOR_POWER4:
27668       if (is_microcoded_insn (insn))
27669         return true;
27670 
27671       if (!rs6000_sched_groups)
27672         return false;
27673 
27674       type = get_attr_type (insn);
27675 
27676       switch (type)
27677         {
27678         case TYPE_MFCR:
27679         case TYPE_MFCRF:
27680         case TYPE_MTCR:
27681         case TYPE_DELAYED_CR:
27682         case TYPE_CR_LOGICAL:
27683         case TYPE_MTJMPR:
27684         case TYPE_MFJMPR:
27685         case TYPE_IDIV:
27686         case TYPE_LDIV:
27687         case TYPE_LOAD_L:
27688         case TYPE_STORE_C:
27689         case TYPE_ISYNC:
27690         case TYPE_SYNC:
27691           return true;
27692         default:
27693           break;
27694         }
27695       break;
27696     case PROCESSOR_POWER6:
27697       type = get_attr_type (insn);
27698 
27699       switch (type)
27700         {
27701         case TYPE_INSERT_DWORD:
27702         case TYPE_EXTS:
27703         case TYPE_CNTLZ:
27704         case TYPE_SHIFT:
27705         case TYPE_VAR_SHIFT_ROTATE:
27706         case TYPE_TRAP:
27707         case TYPE_IMUL:
27708         case TYPE_IMUL2:
27709         case TYPE_IMUL3:
27710         case TYPE_LMUL:
27711         case TYPE_IDIV:
27712         case TYPE_INSERT_WORD:
27713         case TYPE_DELAYED_COMPARE:
27714         case TYPE_IMUL_COMPARE:
27715         case TYPE_LMUL_COMPARE:
27716         case TYPE_FPCOMPARE:
27717         case TYPE_MFCR:
27718         case TYPE_MTCR:
27719         case TYPE_MFJMPR:
27720         case TYPE_MTJMPR:
27721         case TYPE_ISYNC:
27722         case TYPE_SYNC:
27723         case TYPE_LOAD_L:
27724         case TYPE_STORE_C:
27725         case TYPE_LOAD_U:
27726         case TYPE_LOAD_UX:
27727         case TYPE_LOAD_EXT_UX:
27728         case TYPE_STORE_U:
27729         case TYPE_STORE_UX:
27730         case TYPE_FPLOAD_U:
27731         case TYPE_FPLOAD_UX:
27732         case TYPE_FPSTORE_U:
27733         case TYPE_FPSTORE_UX:
27734           return true;
27735         default:
27736           break;
27737         }
27738       break;
27739     case PROCESSOR_POWER7:
27740       type = get_attr_type (insn);
27741 
27742       switch (type)
27743         {
27744         case TYPE_CR_LOGICAL:
27745         case TYPE_MFCR:
27746         case TYPE_MFCRF:
27747         case TYPE_MTCR:
27748         case TYPE_IDIV:
27749         case TYPE_LDIV:
27750         case TYPE_COMPARE:
27751         case TYPE_DELAYED_COMPARE:
27752         case TYPE_VAR_DELAYED_COMPARE:
27753         case TYPE_ISYNC:
27754         case TYPE_LOAD_L:
27755         case TYPE_STORE_C:
27756         case TYPE_LOAD_U:
27757         case TYPE_LOAD_UX:
27758         case TYPE_LOAD_EXT:
27759         case TYPE_LOAD_EXT_U:
27760         case TYPE_LOAD_EXT_UX:
27761         case TYPE_STORE_U:
27762         case TYPE_STORE_UX:
27763         case TYPE_FPLOAD_U:
27764         case TYPE_FPLOAD_UX:
27765         case TYPE_FPSTORE_U:
27766         case TYPE_FPSTORE_UX:
27767         case TYPE_MFJMPR:
27768         case TYPE_MTJMPR:
27769           return true;
27770         default:
27771           break;
27772         }
27773       break;
27774     case PROCESSOR_POWER8:
27775       type = get_attr_type (insn);
27776 
27777       switch (type)
27778         {
27779         case TYPE_CR_LOGICAL:
27780         case TYPE_DELAYED_CR:
27781         case TYPE_MFCR:
27782         case TYPE_MFCRF:
27783         case TYPE_MTCR:
27784         case TYPE_COMPARE:
27785         case TYPE_DELAYED_COMPARE:
27786         case TYPE_VAR_DELAYED_COMPARE:
27787         case TYPE_IMUL_COMPARE:
27788         case TYPE_LMUL_COMPARE:
27789         case TYPE_SYNC:
27790         case TYPE_ISYNC:
27791         case TYPE_LOAD_L:
27792         case TYPE_STORE_C:
27793         case TYPE_LOAD_U:
27794         case TYPE_LOAD_UX:
27795         case TYPE_LOAD_EXT:
27796         case TYPE_LOAD_EXT_U:
27797         case TYPE_LOAD_EXT_UX:
27798         case TYPE_STORE_UX:
27799         case TYPE_VECSTORE:
27800         case TYPE_MFJMPR:
27801         case TYPE_MTJMPR:
27802           return true;
27803         default:
27804           break;
27805         }
27806       break;
27807     default:
27808       break;
27809     }
27810 
27811   return false;
27812 }
27813 
27814 static bool
insn_must_be_last_in_group(rtx insn)27815 insn_must_be_last_in_group (rtx insn)
27816 {
27817   enum attr_type type;
27818 
27819   if (!insn
27820       || GET_CODE (insn) == NOTE
27821       || DEBUG_INSN_P (insn)
27822       || GET_CODE (PATTERN (insn)) == USE
27823       || GET_CODE (PATTERN (insn)) == CLOBBER)
27824     return false;
27825 
27826   switch (rs6000_cpu) {
27827   case PROCESSOR_POWER4:
27828   case PROCESSOR_POWER5:
27829     if (is_microcoded_insn (insn))
27830       return true;
27831 
27832     if (is_branch_slot_insn (insn))
27833       return true;
27834 
27835     break;
27836   case PROCESSOR_POWER6:
27837     type = get_attr_type (insn);
27838 
27839     switch (type)
27840       {
27841       case TYPE_EXTS:
27842       case TYPE_CNTLZ:
27843       case TYPE_SHIFT:
27844       case TYPE_VAR_SHIFT_ROTATE:
27845       case TYPE_TRAP:
27846       case TYPE_IMUL:
27847       case TYPE_IMUL2:
27848       case TYPE_IMUL3:
27849       case TYPE_LMUL:
27850       case TYPE_IDIV:
27851       case TYPE_DELAYED_COMPARE:
27852       case TYPE_IMUL_COMPARE:
27853       case TYPE_LMUL_COMPARE:
27854       case TYPE_FPCOMPARE:
27855       case TYPE_MFCR:
27856       case TYPE_MTCR:
27857       case TYPE_MFJMPR:
27858       case TYPE_MTJMPR:
27859       case TYPE_ISYNC:
27860       case TYPE_SYNC:
27861       case TYPE_LOAD_L:
27862       case TYPE_STORE_C:
27863         return true;
27864       default:
27865         break;
27866     }
27867     break;
27868   case PROCESSOR_POWER7:
27869     type = get_attr_type (insn);
27870 
27871     switch (type)
27872       {
27873       case TYPE_ISYNC:
27874       case TYPE_SYNC:
27875       case TYPE_LOAD_L:
27876       case TYPE_STORE_C:
27877       case TYPE_LOAD_EXT_U:
27878       case TYPE_LOAD_EXT_UX:
27879       case TYPE_STORE_UX:
27880         return true;
27881       default:
27882         break;
27883     }
27884     break;
27885   case PROCESSOR_POWER8:
27886     type = get_attr_type (insn);
27887 
27888     switch (type)
27889       {
27890       case TYPE_MFCR:
27891       case TYPE_MTCR:
27892       case TYPE_ISYNC:
27893       case TYPE_SYNC:
27894       case TYPE_LOAD_L:
27895       case TYPE_STORE_C:
27896       case TYPE_LOAD_EXT_U:
27897       case TYPE_LOAD_EXT_UX:
27898       case TYPE_STORE_UX:
27899         return true;
27900       default:
27901         break;
27902     }
27903     break;
27904   default:
27905     break;
27906   }
27907 
27908   return false;
27909 }
27910 
27911 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
27912    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
27913 
27914 static bool
is_costly_group(rtx * group_insns,rtx next_insn)27915 is_costly_group (rtx *group_insns, rtx next_insn)
27916 {
27917   int i;
27918   int issue_rate = rs6000_issue_rate ();
27919 
27920   for (i = 0; i < issue_rate; i++)
27921     {
27922       sd_iterator_def sd_it;
27923       dep_t dep;
27924       rtx insn = group_insns[i];
27925 
27926       if (!insn)
27927 	continue;
27928 
27929       FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
27930 	{
27931 	  rtx next = DEP_CON (dep);
27932 
27933 	  if (next == next_insn
27934 	      && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
27935 	    return true;
27936 	}
27937     }
27938 
27939   return false;
27940 }
27941 
27942 /* Utility of the function redefine_groups.
27943    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
27944    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
27945    to keep it "far" (in a separate group) from GROUP_INSNS, following
27946    one of the following schemes, depending on the value of the flag
27947    -minsert_sched_nops = X:
27948    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
27949        in order to force NEXT_INSN into a separate group.
27950    (2) X < sched_finish_regroup_exact: insert exactly X nops.
27951    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
27952    insertion (has a group just ended, how many vacant issue slots remain in the
27953    last group, and how many dispatch groups were encountered so far).  */
27954 
27955 static int
force_new_group(int sched_verbose,FILE * dump,rtx * group_insns,rtx next_insn,bool * group_end,int can_issue_more,int * group_count)27956 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
27957 		 rtx next_insn, bool *group_end, int can_issue_more,
27958 		 int *group_count)
27959 {
27960   rtx nop;
27961   bool force;
27962   int issue_rate = rs6000_issue_rate ();
27963   bool end = *group_end;
27964   int i;
27965 
27966   if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
27967     return can_issue_more;
27968 
27969   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
27970     return can_issue_more;
27971 
27972   force = is_costly_group (group_insns, next_insn);
27973   if (!force)
27974     return can_issue_more;
27975 
27976   if (sched_verbose > 6)
27977     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
27978 	     *group_count ,can_issue_more);
27979 
27980   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
27981     {
27982       if (*group_end)
27983 	can_issue_more = 0;
27984 
27985       /* Since only a branch can be issued in the last issue_slot, it is
27986 	 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
27987 	 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
27988 	 in this case the last nop will start a new group and the branch
27989 	 will be forced to the new group.  */
27990       if (can_issue_more && !is_branch_slot_insn (next_insn))
27991 	can_issue_more--;
27992 
27993       /* Do we have a special group ending nop? */
27994       if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
27995 	  || rs6000_cpu_attr == CPU_POWER8)
27996 	{
27997 	  nop = gen_group_ending_nop ();
27998 	  emit_insn_before (nop, next_insn);
27999 	  can_issue_more = 0;
28000 	}
28001       else
28002 	while (can_issue_more > 0)
28003 	  {
28004 	    nop = gen_nop ();
28005 	    emit_insn_before (nop, next_insn);
28006 	    can_issue_more--;
28007 	  }
28008 
28009       *group_end = true;
28010       return 0;
28011     }
28012 
28013   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
28014     {
28015       int n_nops = rs6000_sched_insert_nops;
28016 
28017       /* Nops can't be issued from the branch slot, so the effective
28018 	 issue_rate for nops is 'issue_rate - 1'.  */
28019       if (can_issue_more == 0)
28020 	can_issue_more = issue_rate;
28021       can_issue_more--;
28022       if (can_issue_more == 0)
28023 	{
28024 	  can_issue_more = issue_rate - 1;
28025 	  (*group_count)++;
28026 	  end = true;
28027 	  for (i = 0; i < issue_rate; i++)
28028 	    {
28029 	      group_insns[i] = 0;
28030 	    }
28031 	}
28032 
28033       while (n_nops > 0)
28034 	{
28035 	  nop = gen_nop ();
28036 	  emit_insn_before (nop, next_insn);
28037 	  if (can_issue_more == issue_rate - 1) /* new group begins */
28038 	    end = false;
28039 	  can_issue_more--;
28040 	  if (can_issue_more == 0)
28041 	    {
28042 	      can_issue_more = issue_rate - 1;
28043 	      (*group_count)++;
28044 	      end = true;
28045 	      for (i = 0; i < issue_rate; i++)
28046 		{
28047 		  group_insns[i] = 0;
28048 		}
28049 	    }
28050 	  n_nops--;
28051 	}
28052 
28053       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
28054       can_issue_more++;
28055 
28056       /* Is next_insn going to start a new group?  */
28057       *group_end
28058 	= (end
28059 	   || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
28060 	   || (can_issue_more <= 2 && is_cracked_insn (next_insn))
28061 	   || (can_issue_more < issue_rate &&
28062 	       insn_terminates_group_p (next_insn, previous_group)));
28063       if (*group_end && end)
28064 	(*group_count)--;
28065 
28066       if (sched_verbose > 6)
28067 	fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
28068 		 *group_count, can_issue_more);
28069       return can_issue_more;
28070     }
28071 
28072   return can_issue_more;
28073 }
28074 
28075 /* This function tries to synch the dispatch groups that the compiler "sees"
28076    with the dispatch groups that the processor dispatcher is expected to
28077    form in practice.  It tries to achieve this synchronization by forcing the
28078    estimated processor grouping on the compiler (as opposed to the function
28079    'pad_goups' which tries to force the scheduler's grouping on the processor).
28080 
28081    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
28082    examines the (estimated) dispatch groups that will be formed by the processor
28083    dispatcher.  It marks these group boundaries to reflect the estimated
28084    processor grouping, overriding the grouping that the scheduler had marked.
28085    Depending on the value of the flag '-minsert-sched-nops' this function can
28086    force certain insns into separate groups or force a certain distance between
28087    them by inserting nops, for example, if there exists a "costly dependence"
28088    between the insns.
28089 
28090    The function estimates the group boundaries that the processor will form as
28091    follows:  It keeps track of how many vacant issue slots are available after
28092    each insn.  A subsequent insn will start a new group if one of the following
28093    4 cases applies:
28094    - no more vacant issue slots remain in the current dispatch group.
28095    - only the last issue slot, which is the branch slot, is vacant, but the next
28096      insn is not a branch.
28097    - only the last 2 or less issue slots, including the branch slot, are vacant,
28098      which means that a cracked insn (which occupies two issue slots) can't be
28099      issued in this group.
28100    - less than 'issue_rate' slots are vacant, and the next insn always needs to
28101      start a new group.  */
28102 
28103 static int
redefine_groups(FILE * dump,int sched_verbose,rtx prev_head_insn,rtx tail)28104 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
28105 {
28106   rtx insn, next_insn;
28107   int issue_rate;
28108   int can_issue_more;
28109   int slot, i;
28110   bool group_end;
28111   int group_count = 0;
28112   rtx *group_insns;
28113 
28114   /* Initialize.  */
28115   issue_rate = rs6000_issue_rate ();
28116   group_insns = XALLOCAVEC (rtx, issue_rate);
28117   for (i = 0; i < issue_rate; i++)
28118     {
28119       group_insns[i] = 0;
28120     }
28121   can_issue_more = issue_rate;
28122   slot = 0;
28123   insn = get_next_active_insn (prev_head_insn, tail);
28124   group_end = false;
28125 
28126   while (insn != NULL_RTX)
28127     {
28128       slot = (issue_rate - can_issue_more);
28129       group_insns[slot] = insn;
28130       can_issue_more =
28131 	rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
28132       if (insn_terminates_group_p (insn, current_group))
28133 	can_issue_more = 0;
28134 
28135       next_insn = get_next_active_insn (insn, tail);
28136       if (next_insn == NULL_RTX)
28137 	return group_count + 1;
28138 
28139       /* Is next_insn going to start a new group?  */
28140       group_end
28141 	= (can_issue_more == 0
28142 	   || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
28143 	   || (can_issue_more <= 2 && is_cracked_insn (next_insn))
28144 	   || (can_issue_more < issue_rate &&
28145 	       insn_terminates_group_p (next_insn, previous_group)));
28146 
28147       can_issue_more = force_new_group (sched_verbose, dump, group_insns,
28148 					next_insn, &group_end, can_issue_more,
28149 					&group_count);
28150 
28151       if (group_end)
28152 	{
28153 	  group_count++;
28154 	  can_issue_more = 0;
28155 	  for (i = 0; i < issue_rate; i++)
28156 	    {
28157 	      group_insns[i] = 0;
28158 	    }
28159 	}
28160 
28161       if (GET_MODE (next_insn) == TImode && can_issue_more)
28162 	PUT_MODE (next_insn, VOIDmode);
28163       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
28164 	PUT_MODE (next_insn, TImode);
28165 
28166       insn = next_insn;
28167       if (can_issue_more == 0)
28168 	can_issue_more = issue_rate;
28169     } /* while */
28170 
28171   return group_count;
28172 }
28173 
28174 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
28175    dispatch group boundaries that the scheduler had marked.  Pad with nops
28176    any dispatch groups which have vacant issue slots, in order to force the
28177    scheduler's grouping on the processor dispatcher.  The function
28178    returns the number of dispatch groups found.  */
28179 
28180 static int
pad_groups(FILE * dump,int sched_verbose,rtx prev_head_insn,rtx tail)28181 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
28182 {
28183   rtx insn, next_insn;
28184   rtx nop;
28185   int issue_rate;
28186   int can_issue_more;
28187   int group_end;
28188   int group_count = 0;
28189 
28190   /* Initialize issue_rate.  */
28191   issue_rate = rs6000_issue_rate ();
28192   can_issue_more = issue_rate;
28193 
28194   insn = get_next_active_insn (prev_head_insn, tail);
28195   next_insn = get_next_active_insn (insn, tail);
28196 
28197   while (insn != NULL_RTX)
28198     {
28199       can_issue_more =
28200       	rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
28201 
28202       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
28203 
28204       if (next_insn == NULL_RTX)
28205 	break;
28206 
28207       if (group_end)
28208 	{
28209 	  /* If the scheduler had marked group termination at this location
28210 	     (between insn and next_insn), and neither insn nor next_insn will
28211 	     force group termination, pad the group with nops to force group
28212 	     termination.  */
28213 	  if (can_issue_more
28214 	      && (rs6000_sched_insert_nops == sched_finish_pad_groups)
28215 	      && !insn_terminates_group_p (insn, current_group)
28216 	      && !insn_terminates_group_p (next_insn, previous_group))
28217 	    {
28218 	      if (!is_branch_slot_insn (next_insn))
28219 		can_issue_more--;
28220 
28221 	      while (can_issue_more)
28222 		{
28223 		  nop = gen_nop ();
28224 		  emit_insn_before (nop, next_insn);
28225 		  can_issue_more--;
28226 		}
28227 	    }
28228 
28229 	  can_issue_more = issue_rate;
28230 	  group_count++;
28231 	}
28232 
28233       insn = next_insn;
28234       next_insn = get_next_active_insn (insn, tail);
28235     }
28236 
28237   return group_count;
28238 }
28239 
28240 /* We're beginning a new block.  Initialize data structures as necessary.  */
28241 
28242 static void
rs6000_sched_init(FILE * dump ATTRIBUTE_UNUSED,int sched_verbose ATTRIBUTE_UNUSED,int max_ready ATTRIBUTE_UNUSED)28243 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
28244 		     int sched_verbose ATTRIBUTE_UNUSED,
28245 		     int max_ready ATTRIBUTE_UNUSED)
28246 {
28247   last_scheduled_insn = NULL_RTX;
28248   load_store_pendulum = 0;
28249 }
28250 
28251 /* The following function is called at the end of scheduling BB.
28252    After reload, it inserts nops at insn group bundling.  */
28253 
28254 static void
rs6000_sched_finish(FILE * dump,int sched_verbose)28255 rs6000_sched_finish (FILE *dump, int sched_verbose)
28256 {
28257   int n_groups;
28258 
28259   if (sched_verbose)
28260     fprintf (dump, "=== Finishing schedule.\n");
28261 
28262   if (reload_completed && rs6000_sched_groups)
28263     {
28264       /* Do not run sched_finish hook when selective scheduling enabled.  */
28265       if (sel_sched_p ())
28266 	return;
28267 
28268       if (rs6000_sched_insert_nops == sched_finish_none)
28269 	return;
28270 
28271       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
28272 	n_groups = pad_groups (dump, sched_verbose,
28273 			       current_sched_info->prev_head,
28274 			       current_sched_info->next_tail);
28275       else
28276 	n_groups = redefine_groups (dump, sched_verbose,
28277 				    current_sched_info->prev_head,
28278 				    current_sched_info->next_tail);
28279 
28280       if (sched_verbose >= 6)
28281 	{
28282     	  fprintf (dump, "ngroups = %d\n", n_groups);
28283 	  print_rtl (dump, current_sched_info->prev_head);
28284 	  fprintf (dump, "Done finish_sched\n");
28285 	}
28286     }
28287 }
28288 
28289 struct _rs6000_sched_context
28290 {
28291   short cached_can_issue_more;
28292   rtx last_scheduled_insn;
28293   int load_store_pendulum;
28294 };
28295 
28296 typedef struct _rs6000_sched_context rs6000_sched_context_def;
28297 typedef rs6000_sched_context_def *rs6000_sched_context_t;
28298 
28299 /* Allocate store for new scheduling context.  */
28300 static void *
rs6000_alloc_sched_context(void)28301 rs6000_alloc_sched_context (void)
28302 {
28303   return xmalloc (sizeof (rs6000_sched_context_def));
28304 }
28305 
28306 /* If CLEAN_P is true then initializes _SC with clean data,
28307    and from the global context otherwise.  */
28308 static void
rs6000_init_sched_context(void * _sc,bool clean_p)28309 rs6000_init_sched_context (void *_sc, bool clean_p)
28310 {
28311   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
28312 
28313   if (clean_p)
28314     {
28315       sc->cached_can_issue_more = 0;
28316       sc->last_scheduled_insn = NULL_RTX;
28317       sc->load_store_pendulum = 0;
28318     }
28319   else
28320     {
28321       sc->cached_can_issue_more = cached_can_issue_more;
28322       sc->last_scheduled_insn = last_scheduled_insn;
28323       sc->load_store_pendulum = load_store_pendulum;
28324     }
28325 }
28326 
28327 /* Sets the global scheduling context to the one pointed to by _SC.  */
28328 static void
rs6000_set_sched_context(void * _sc)28329 rs6000_set_sched_context (void *_sc)
28330 {
28331   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
28332 
28333   gcc_assert (sc != NULL);
28334 
28335   cached_can_issue_more = sc->cached_can_issue_more;
28336   last_scheduled_insn = sc->last_scheduled_insn;
28337   load_store_pendulum = sc->load_store_pendulum;
28338 }
28339 
28340 /* Free _SC.  */
28341 static void
rs6000_free_sched_context(void * _sc)28342 rs6000_free_sched_context (void *_sc)
28343 {
28344   gcc_assert (_sc != NULL);
28345 
28346   free (_sc);
28347 }
28348 
28349 
28350 /* Length in units of the trampoline for entering a nested function.  */
28351 
28352 int
rs6000_trampoline_size(void)28353 rs6000_trampoline_size (void)
28354 {
28355   int ret = 0;
28356 
28357   switch (DEFAULT_ABI)
28358     {
28359     default:
28360       gcc_unreachable ();
28361 
28362     case ABI_AIX:
28363       ret = (TARGET_32BIT) ? 12 : 24;
28364       break;
28365 
28366     case ABI_ELFv2:
28367       gcc_assert (!TARGET_32BIT);
28368       ret = 32;
28369       break;
28370 
28371     case ABI_DARWIN:
28372     case ABI_V4:
28373       ret = (TARGET_32BIT) ? 40 : 48;
28374       break;
28375     }
28376 
28377   return ret;
28378 }
28379 
28380 /* Emit RTL insns to initialize the variable parts of a trampoline.
28381    FNADDR is an RTX for the address of the function's pure code.
28382    CXT is an RTX for the static chain value for the function.  */
28383 
28384 static void
rs6000_trampoline_init(rtx m_tramp,tree fndecl,rtx cxt)28385 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
28386 {
28387   int regsize = (TARGET_32BIT) ? 4 : 8;
28388   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
28389   rtx ctx_reg = force_reg (Pmode, cxt);
28390   rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
28391 
28392   switch (DEFAULT_ABI)
28393     {
28394     default:
28395       gcc_unreachable ();
28396 
28397     /* Under AIX, just build the 3 word function descriptor */
28398     case ABI_AIX:
28399       {
28400 	rtx fnmem, fn_reg, toc_reg;
28401 
28402 	if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28403 	  error ("You cannot take the address of a nested function if you use "
28404 		 "the -mno-pointers-to-nested-functions option.");
28405 
28406 	fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
28407 	fn_reg = gen_reg_rtx (Pmode);
28408 	toc_reg = gen_reg_rtx (Pmode);
28409 
28410   /* Macro to shorten the code expansions below.  */
28411 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
28412 
28413 	m_tramp = replace_equiv_address (m_tramp, addr);
28414 
28415 	emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
28416 	emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
28417 	emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
28418 	emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
28419 	emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
28420 
28421 # undef MEM_PLUS
28422       }
28423       break;
28424 
28425     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
28426     case ABI_ELFv2:
28427     case ABI_DARWIN:
28428     case ABI_V4:
28429       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
28430 			 LCT_NORMAL, VOIDmode, 4,
28431 			 addr, Pmode,
28432 			 GEN_INT (rs6000_trampoline_size ()), SImode,
28433 			 fnaddr, Pmode,
28434 			 ctx_reg, Pmode);
28435       break;
28436     }
28437 }
28438 
28439 
28440 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
28441    identifier as an argument, so the front end shouldn't look it up.  */
28442 
28443 static bool
rs6000_attribute_takes_identifier_p(const_tree attr_id)28444 rs6000_attribute_takes_identifier_p (const_tree attr_id)
28445 {
28446   return is_attribute_p ("altivec", attr_id);
28447 }
28448 
28449 /* Handle the "altivec" attribute.  The attribute may have
28450    arguments as follows:
28451 
28452 	__attribute__((altivec(vector__)))
28453 	__attribute__((altivec(pixel__)))	(always followed by 'unsigned short')
28454 	__attribute__((altivec(bool__)))	(always followed by 'unsigned')
28455 
28456   and may appear more than once (e.g., 'vector bool char') in a
28457   given declaration.  */
28458 
28459 static tree
rs6000_handle_altivec_attribute(tree * node,tree name ATTRIBUTE_UNUSED,tree args,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)28460 rs6000_handle_altivec_attribute (tree *node,
28461 				 tree name ATTRIBUTE_UNUSED,
28462 				 tree args,
28463 				 int flags ATTRIBUTE_UNUSED,
28464 				 bool *no_add_attrs)
28465 {
28466   tree type = *node, result = NULL_TREE;
28467   enum machine_mode mode;
28468   int unsigned_p;
28469   char altivec_type
28470     = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
28471 	&& TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
28472        ? *IDENTIFIER_POINTER (TREE_VALUE (args))
28473        : '?');
28474 
28475   while (POINTER_TYPE_P (type)
28476 	 || TREE_CODE (type) == FUNCTION_TYPE
28477 	 || TREE_CODE (type) == METHOD_TYPE
28478 	 || TREE_CODE (type) == ARRAY_TYPE)
28479     type = TREE_TYPE (type);
28480 
28481   mode = TYPE_MODE (type);
28482 
28483   /* Check for invalid AltiVec type qualifiers.  */
28484   if (type == long_double_type_node)
28485     error ("use of %<long double%> in AltiVec types is invalid");
28486   else if (type == boolean_type_node)
28487     error ("use of boolean types in AltiVec types is invalid");
28488   else if (TREE_CODE (type) == COMPLEX_TYPE)
28489     error ("use of %<complex%> in AltiVec types is invalid");
28490   else if (DECIMAL_FLOAT_MODE_P (mode))
28491     error ("use of decimal floating point types in AltiVec types is invalid");
28492   else if (!TARGET_VSX)
28493     {
28494       if (type == long_unsigned_type_node || type == long_integer_type_node)
28495 	{
28496 	  if (TARGET_64BIT)
28497 	    error ("use of %<long%> in AltiVec types is invalid for "
28498 		   "64-bit code without -mvsx");
28499 	  else if (rs6000_warn_altivec_long)
28500 	    warning (0, "use of %<long%> in AltiVec types is deprecated; "
28501 		     "use %<int%>");
28502 	}
28503       else if (type == long_long_unsigned_type_node
28504 	       || type == long_long_integer_type_node)
28505 	error ("use of %<long long%> in AltiVec types is invalid without "
28506 	       "-mvsx");
28507       else if (type == double_type_node)
28508 	error ("use of %<double%> in AltiVec types is invalid without -mvsx");
28509     }
28510 
28511   switch (altivec_type)
28512     {
28513     case 'v':
28514       unsigned_p = TYPE_UNSIGNED (type);
28515       switch (mode)
28516 	{
28517 	case TImode:
28518 	  result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
28519 	  break;
28520 	case DImode:
28521 	  result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
28522 	  break;
28523 	case SImode:
28524 	  result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
28525 	  break;
28526 	case HImode:
28527 	  result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
28528 	  break;
28529 	case QImode:
28530 	  result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
28531 	  break;
28532 	case SFmode: result = V4SF_type_node; break;
28533 	case DFmode: result = V2DF_type_node; break;
28534 	  /* If the user says 'vector int bool', we may be handed the 'bool'
28535 	     attribute _before_ the 'vector' attribute, and so select the
28536 	     proper type in the 'b' case below.  */
28537 	case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
28538 	case V2DImode: case V2DFmode:
28539 	  result = type;
28540 	default: break;
28541 	}
28542       break;
28543     case 'b':
28544       switch (mode)
28545 	{
28546 	case DImode: case V2DImode: result = bool_V2DI_type_node; break;
28547 	case SImode: case V4SImode: result = bool_V4SI_type_node; break;
28548 	case HImode: case V8HImode: result = bool_V8HI_type_node; break;
28549 	case QImode: case V16QImode: result = bool_V16QI_type_node;
28550 	default: break;
28551 	}
28552       break;
28553     case 'p':
28554       switch (mode)
28555 	{
28556 	case V8HImode: result = pixel_V8HI_type_node;
28557 	default: break;
28558 	}
28559     default: break;
28560     }
28561 
28562   /* Propagate qualifiers attached to the element type
28563      onto the vector type.  */
28564   if (result && result != type && TYPE_QUALS (type))
28565     result = build_qualified_type (result, TYPE_QUALS (type));
28566 
28567   *no_add_attrs = true;  /* No need to hang on to the attribute.  */
28568 
28569   if (result)
28570     *node = lang_hooks.types.reconstruct_complex_type (*node, result);
28571 
28572   return NULL_TREE;
28573 }
28574 
28575 /* AltiVec defines four built-in scalar types that serve as vector
28576    elements; we must teach the compiler how to mangle them.  */
28577 
28578 static const char *
rs6000_mangle_type(const_tree type)28579 rs6000_mangle_type (const_tree type)
28580 {
28581   type = TYPE_MAIN_VARIANT (type);
28582 
28583   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
28584       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
28585     return NULL;
28586 
28587   if (type == bool_char_type_node) return "U6__boolc";
28588   if (type == bool_short_type_node) return "U6__bools";
28589   if (type == pixel_type_node) return "u7__pixel";
28590   if (type == bool_int_type_node) return "U6__booli";
28591   if (type == bool_long_type_node) return "U6__booll";
28592 
28593   /* Mangle IBM extended float long double as `g' (__float128) on
28594      powerpc*-linux where long-double-64 previously was the default.  */
28595   if (TYPE_MAIN_VARIANT (type) == long_double_type_node
28596       && TARGET_ELF
28597       && TARGET_LONG_DOUBLE_128
28598       && !TARGET_IEEEQUAD)
28599     return "g";
28600 
28601   /* For all other types, use normal C++ mangling.  */
28602   return NULL;
28603 }
28604 
28605 /* Handle a "longcall" or "shortcall" attribute; arguments as in
28606    struct attribute_spec.handler.  */
28607 
28608 static tree
rs6000_handle_longcall_attribute(tree * node,tree name,tree args ATTRIBUTE_UNUSED,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)28609 rs6000_handle_longcall_attribute (tree *node, tree name,
28610 				  tree args ATTRIBUTE_UNUSED,
28611 				  int flags ATTRIBUTE_UNUSED,
28612 				  bool *no_add_attrs)
28613 {
28614   if (TREE_CODE (*node) != FUNCTION_TYPE
28615       && TREE_CODE (*node) != FIELD_DECL
28616       && TREE_CODE (*node) != TYPE_DECL)
28617     {
28618       warning (OPT_Wattributes, "%qE attribute only applies to functions",
28619 	       name);
28620       *no_add_attrs = true;
28621     }
28622 
28623   return NULL_TREE;
28624 }
28625 
28626 /* Set longcall attributes on all functions declared when
28627    rs6000_default_long_calls is true.  */
28628 static void
rs6000_set_default_type_attributes(tree type)28629 rs6000_set_default_type_attributes (tree type)
28630 {
28631   if (rs6000_default_long_calls
28632       && (TREE_CODE (type) == FUNCTION_TYPE
28633 	  || TREE_CODE (type) == METHOD_TYPE))
28634     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
28635 					NULL_TREE,
28636 					TYPE_ATTRIBUTES (type));
28637 
28638 #if TARGET_MACHO
28639   darwin_set_default_type_attributes (type);
28640 #endif
28641 }
28642 
28643 /* Return a reference suitable for calling a function with the
28644    longcall attribute.  */
28645 
28646 rtx
rs6000_longcall_ref(rtx call_ref)28647 rs6000_longcall_ref (rtx call_ref)
28648 {
28649   const char *call_name;
28650   tree node;
28651 
28652   if (GET_CODE (call_ref) != SYMBOL_REF)
28653     return call_ref;
28654 
28655   /* System V adds '.' to the internal name, so skip them.  */
28656   call_name = XSTR (call_ref, 0);
28657   if (*call_name == '.')
28658     {
28659       while (*call_name == '.')
28660 	call_name++;
28661 
28662       node = get_identifier (call_name);
28663       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
28664     }
28665 
28666   return force_reg (Pmode, call_ref);
28667 }
28668 
28669 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
28670 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
28671 #endif
28672 
28673 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
28674    struct attribute_spec.handler.  */
28675 static tree
rs6000_handle_struct_attribute(tree * node,tree name,tree args ATTRIBUTE_UNUSED,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)28676 rs6000_handle_struct_attribute (tree *node, tree name,
28677 				tree args ATTRIBUTE_UNUSED,
28678 				int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28679 {
28680   tree *type = NULL;
28681   if (DECL_P (*node))
28682     {
28683       if (TREE_CODE (*node) == TYPE_DECL)
28684         type = &TREE_TYPE (*node);
28685     }
28686   else
28687     type = node;
28688 
28689   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
28690                  || TREE_CODE (*type) == UNION_TYPE)))
28691     {
28692       warning (OPT_Wattributes, "%qE attribute ignored", name);
28693       *no_add_attrs = true;
28694     }
28695 
28696   else if ((is_attribute_p ("ms_struct", name)
28697             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
28698            || ((is_attribute_p ("gcc_struct", name)
28699                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
28700     {
28701       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
28702                name);
28703       *no_add_attrs = true;
28704     }
28705 
28706   return NULL_TREE;
28707 }
28708 
28709 static bool
rs6000_ms_bitfield_layout_p(const_tree record_type)28710 rs6000_ms_bitfield_layout_p (const_tree record_type)
28711 {
28712   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
28713           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
28714     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
28715 }
28716 
28717 #ifdef USING_ELFOS_H
28718 
28719 /* A get_unnamed_section callback, used for switching to toc_section.  */
28720 
28721 static void
rs6000_elf_output_toc_section_asm_op(const void * data ATTRIBUTE_UNUSED)28722 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
28723 {
28724   if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28725       && TARGET_MINIMAL_TOC
28726       && !TARGET_RELOCATABLE)
28727     {
28728       if (!toc_initialized)
28729 	{
28730 	  toc_initialized = 1;
28731 	  fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28732 	  (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
28733 	  fprintf (asm_out_file, "\t.tc ");
28734 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
28735 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28736 	  fprintf (asm_out_file, "\n");
28737 
28738 	  fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28739 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28740 	  fprintf (asm_out_file, " = .+32768\n");
28741 	}
28742       else
28743 	fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28744     }
28745   else if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28746 	   && !TARGET_RELOCATABLE)
28747     fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28748   else
28749     {
28750       fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28751       if (!toc_initialized)
28752 	{
28753 	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28754 	  fprintf (asm_out_file, " = .+32768\n");
28755 	  toc_initialized = 1;
28756 	}
28757     }
28758 }
28759 
28760 /* Implement TARGET_ASM_INIT_SECTIONS.  */
28761 
28762 static void
rs6000_elf_asm_init_sections(void)28763 rs6000_elf_asm_init_sections (void)
28764 {
28765   toc_section
28766     = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
28767 
28768   sdata2_section
28769     = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
28770 			   SDATA2_SECTION_ASM_OP);
28771 }
28772 
28773 /* Implement TARGET_SELECT_RTX_SECTION.  */
28774 
28775 static section *
rs6000_elf_select_rtx_section(enum machine_mode mode,rtx x,unsigned HOST_WIDE_INT align)28776 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
28777 			       unsigned HOST_WIDE_INT align)
28778 {
28779   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
28780     return toc_section;
28781   else
28782     return default_elf_select_rtx_section (mode, x, align);
28783 }
28784 
28785 /* For a SYMBOL_REF, set generic flags and then perform some
28786    target-specific processing.
28787 
28788    When the AIX ABI is requested on a non-AIX system, replace the
28789    function name with the real name (with a leading .) rather than the
28790    function descriptor name.  This saves a lot of overriding code to
28791    read the prefixes.  */
28792 
28793 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
28794 static void
rs6000_elf_encode_section_info(tree decl,rtx rtl,int first)28795 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
28796 {
28797   default_encode_section_info (decl, rtl, first);
28798 
28799   if (first
28800       && TREE_CODE (decl) == FUNCTION_DECL
28801       && !TARGET_AIX
28802       && DEFAULT_ABI == ABI_AIX)
28803     {
28804       rtx sym_ref = XEXP (rtl, 0);
28805       size_t len = strlen (XSTR (sym_ref, 0));
28806       char *str = XALLOCAVEC (char, len + 2);
28807       str[0] = '.';
28808       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
28809       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
28810     }
28811 }
28812 
28813 static inline bool
compare_section_name(const char * section,const char * templ)28814 compare_section_name (const char *section, const char *templ)
28815 {
28816   int len;
28817 
28818   len = strlen (templ);
28819   return (strncmp (section, templ, len) == 0
28820 	  && (section[len] == 0 || section[len] == '.'));
28821 }
28822 
28823 bool
rs6000_elf_in_small_data_p(const_tree decl)28824 rs6000_elf_in_small_data_p (const_tree decl)
28825 {
28826   if (rs6000_sdata == SDATA_NONE)
28827     return false;
28828 
28829   /* We want to merge strings, so we never consider them small data.  */
28830   if (TREE_CODE (decl) == STRING_CST)
28831     return false;
28832 
28833   /* Functions are never in the small data area.  */
28834   if (TREE_CODE (decl) == FUNCTION_DECL)
28835     return false;
28836 
28837   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
28838     {
28839       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
28840       if (compare_section_name (section, ".sdata")
28841 	  || compare_section_name (section, ".sdata2")
28842 	  || compare_section_name (section, ".gnu.linkonce.s")
28843 	  || compare_section_name (section, ".sbss")
28844 	  || compare_section_name (section, ".sbss2")
28845 	  || compare_section_name (section, ".gnu.linkonce.sb")
28846 	  || strcmp (section, ".PPC.EMB.sdata0") == 0
28847 	  || strcmp (section, ".PPC.EMB.sbss0") == 0)
28848 	return true;
28849     }
28850   else
28851     {
28852       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
28853 
28854       if (size > 0
28855 	  && size <= g_switch_value
28856 	  /* If it's not public, and we're not going to reference it there,
28857 	     there's no need to put it in the small data section.  */
28858 	  && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
28859 	return true;
28860     }
28861 
28862   return false;
28863 }
28864 
28865 #endif /* USING_ELFOS_H */
28866 
28867 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  */
28868 
28869 static bool
rs6000_use_blocks_for_constant_p(enum machine_mode mode,const_rtx x)28870 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
28871 {
28872   return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
28873 }
28874 
28875 /* Do not place thread-local symbols refs in the object blocks.  */
28876 
28877 static bool
rs6000_use_blocks_for_decl_p(const_tree decl)28878 rs6000_use_blocks_for_decl_p (const_tree decl)
28879 {
28880   return !DECL_THREAD_LOCAL_P (decl);
28881 }
28882 
28883 /* Return a REG that occurs in ADDR with coefficient 1.
28884    ADDR can be effectively incremented by incrementing REG.
28885 
28886    r0 is special and we must not select it as an address
28887    register by this routine since our caller will try to
28888    increment the returned register via an "la" instruction.  */
28889 
28890 rtx
find_addr_reg(rtx addr)28891 find_addr_reg (rtx addr)
28892 {
28893   while (GET_CODE (addr) == PLUS)
28894     {
28895       if (GET_CODE (XEXP (addr, 0)) == REG
28896 	  && REGNO (XEXP (addr, 0)) != 0)
28897 	addr = XEXP (addr, 0);
28898       else if (GET_CODE (XEXP (addr, 1)) == REG
28899 	       && REGNO (XEXP (addr, 1)) != 0)
28900 	addr = XEXP (addr, 1);
28901       else if (CONSTANT_P (XEXP (addr, 0)))
28902 	addr = XEXP (addr, 1);
28903       else if (CONSTANT_P (XEXP (addr, 1)))
28904 	addr = XEXP (addr, 0);
28905       else
28906 	gcc_unreachable ();
28907     }
28908   gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
28909   return addr;
28910 }
28911 
28912 void
rs6000_fatal_bad_address(rtx op)28913 rs6000_fatal_bad_address (rtx op)
28914 {
28915   fatal_insn ("bad address", op);
28916 }
28917 
28918 #if TARGET_MACHO
28919 
28920 typedef struct branch_island_d {
28921   tree function_name;
28922   tree label_name;
28923   int line_number;
28924 } branch_island;
28925 
28926 
28927 static vec<branch_island, va_gc> *branch_islands;
28928 
28929 /* Remember to generate a branch island for far calls to the given
28930    function.  */
28931 
28932 static void
add_compiler_branch_island(tree label_name,tree function_name,int line_number)28933 add_compiler_branch_island (tree label_name, tree function_name,
28934 			    int line_number)
28935 {
28936   branch_island bi = {function_name, label_name, line_number};
28937   vec_safe_push (branch_islands, bi);
28938 }
28939 
28940 /* Generate far-jump branch islands for everything recorded in
28941    branch_islands.  Invoked immediately after the last instruction of
28942    the epilogue has been emitted; the branch islands must be appended
28943    to, and contiguous with, the function body.  Mach-O stubs are
28944    generated in machopic_output_stub().  */
28945 
28946 static void
macho_branch_islands(void)28947 macho_branch_islands (void)
28948 {
28949   char tmp_buf[512];
28950 
28951   while (!vec_safe_is_empty (branch_islands))
28952     {
28953       branch_island *bi = &branch_islands->last ();
28954       const char *label = IDENTIFIER_POINTER (bi->label_name);
28955       const char *name = IDENTIFIER_POINTER (bi->function_name);
28956       char name_buf[512];
28957       /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF().  */
28958       if (name[0] == '*' || name[0] == '&')
28959 	strcpy (name_buf, name+1);
28960       else
28961 	{
28962 	  name_buf[0] = '_';
28963 	  strcpy (name_buf+1, name);
28964 	}
28965       strcpy (tmp_buf, "\n");
28966       strcat (tmp_buf, label);
28967 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28968       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28969 	dbxout_stabd (N_SLINE, bi->line_number);
28970 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28971       if (flag_pic)
28972 	{
28973 	  if (TARGET_LINK_STACK)
28974 	    {
28975 	      char name[32];
28976 	      get_ppc476_thunk_name (name);
28977 	      strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
28978 	      strcat (tmp_buf, name);
28979 	      strcat (tmp_buf, "\n");
28980 	      strcat (tmp_buf, label);
28981 	      strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28982 	    }
28983 	  else
28984 	    {
28985 	      strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
28986 	      strcat (tmp_buf, label);
28987 	      strcat (tmp_buf, "_pic\n");
28988 	      strcat (tmp_buf, label);
28989 	      strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28990 	    }
28991 
28992 	  strcat (tmp_buf, "\taddis r11,r11,ha16(");
28993 	  strcat (tmp_buf, name_buf);
28994 	  strcat (tmp_buf, " - ");
28995 	  strcat (tmp_buf, label);
28996 	  strcat (tmp_buf, "_pic)\n");
28997 
28998 	  strcat (tmp_buf, "\tmtlr r0\n");
28999 
29000 	  strcat (tmp_buf, "\taddi r12,r11,lo16(");
29001 	  strcat (tmp_buf, name_buf);
29002 	  strcat (tmp_buf, " - ");
29003 	  strcat (tmp_buf, label);
29004 	  strcat (tmp_buf, "_pic)\n");
29005 
29006 	  strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
29007 	}
29008       else
29009 	{
29010 	  strcat (tmp_buf, ":\nlis r12,hi16(");
29011 	  strcat (tmp_buf, name_buf);
29012 	  strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
29013 	  strcat (tmp_buf, name_buf);
29014 	  strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
29015 	}
29016       output_asm_insn (tmp_buf, 0);
29017 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
29018       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
29019 	dbxout_stabd (N_SLINE, bi->line_number);
29020 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
29021       branch_islands->pop ();
29022     }
29023 }
29024 
29025 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
29026    already there or not.  */
29027 
29028 static int
no_previous_def(tree function_name)29029 no_previous_def (tree function_name)
29030 {
29031   branch_island *bi;
29032   unsigned ix;
29033 
29034   FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
29035     if (function_name == bi->function_name)
29036       return 0;
29037   return 1;
29038 }
29039 
29040 /* GET_PREV_LABEL gets the label name from the previous definition of
29041    the function.  */
29042 
29043 static tree
get_prev_label(tree function_name)29044 get_prev_label (tree function_name)
29045 {
29046   branch_island *bi;
29047   unsigned ix;
29048 
29049   FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
29050     if (function_name == bi->function_name)
29051       return bi->label_name;
29052   return NULL_TREE;
29053 }
29054 
29055 /* INSN is either a function call or a millicode call.  It may have an
29056    unconditional jump in its delay slot.
29057 
29058    CALL_DEST is the routine we are calling.  */
29059 
29060 char *
output_call(rtx insn,rtx * operands,int dest_operand_number,int cookie_operand_number)29061 output_call (rtx insn, rtx *operands, int dest_operand_number,
29062 	     int cookie_operand_number)
29063 {
29064   static char buf[256];
29065   if (darwin_emit_branch_islands
29066       && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
29067       && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
29068     {
29069       tree labelname;
29070       tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
29071 
29072       if (no_previous_def (funname))
29073 	{
29074 	  rtx label_rtx = gen_label_rtx ();
29075 	  char *label_buf, temp_buf[256];
29076 	  ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
29077 				       CODE_LABEL_NUMBER (label_rtx));
29078 	  label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
29079 	  labelname = get_identifier (label_buf);
29080 	  add_compiler_branch_island (labelname, funname, insn_line (insn));
29081 	}
29082       else
29083 	labelname = get_prev_label (funname);
29084 
29085       /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
29086 	 instruction will reach 'foo', otherwise link as 'bl L42'".
29087 	 "L42" should be a 'branch island', that will do a far jump to
29088 	 'foo'.  Branch islands are generated in
29089 	 macho_branch_islands().  */
29090       sprintf (buf, "jbsr %%z%d,%.246s",
29091 	       dest_operand_number, IDENTIFIER_POINTER (labelname));
29092     }
29093   else
29094     sprintf (buf, "bl %%z%d", dest_operand_number);
29095   return buf;
29096 }
29097 
29098 /* Generate PIC and indirect symbol stubs.  */
29099 
29100 void
machopic_output_stub(FILE * file,const char * symb,const char * stub)29101 machopic_output_stub (FILE *file, const char *symb, const char *stub)
29102 {
29103   unsigned int length;
29104   char *symbol_name, *lazy_ptr_name;
29105   char *local_label_0;
29106   static int label = 0;
29107 
29108   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
29109   symb = (*targetm.strip_name_encoding) (symb);
29110 
29111 
29112   length = strlen (symb);
29113   symbol_name = XALLOCAVEC (char, length + 32);
29114   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
29115 
29116   lazy_ptr_name = XALLOCAVEC (char, length + 32);
29117   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
29118 
29119   if (flag_pic == 2)
29120     switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
29121   else
29122     switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
29123 
29124   if (flag_pic == 2)
29125     {
29126       fprintf (file, "\t.align 5\n");
29127 
29128       fprintf (file, "%s:\n", stub);
29129       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29130 
29131       label++;
29132       local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
29133       sprintf (local_label_0, "\"L%011d$spb\"", label);
29134 
29135       fprintf (file, "\tmflr r0\n");
29136       if (TARGET_LINK_STACK)
29137 	{
29138 	  char name[32];
29139 	  get_ppc476_thunk_name (name);
29140 	  fprintf (file, "\tbl %s\n", name);
29141 	  fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
29142 	}
29143       else
29144 	{
29145 	  fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
29146 	  fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
29147 	}
29148       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
29149 	       lazy_ptr_name, local_label_0);
29150       fprintf (file, "\tmtlr r0\n");
29151       fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
29152 	       (TARGET_64BIT ? "ldu" : "lwzu"),
29153 	       lazy_ptr_name, local_label_0);
29154       fprintf (file, "\tmtctr r12\n");
29155       fprintf (file, "\tbctr\n");
29156     }
29157   else
29158     {
29159       fprintf (file, "\t.align 4\n");
29160 
29161       fprintf (file, "%s:\n", stub);
29162       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29163 
29164       fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
29165       fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
29166 	       (TARGET_64BIT ? "ldu" : "lwzu"),
29167 	       lazy_ptr_name);
29168       fprintf (file, "\tmtctr r12\n");
29169       fprintf (file, "\tbctr\n");
29170     }
29171 
29172   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
29173   fprintf (file, "%s:\n", lazy_ptr_name);
29174   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29175   fprintf (file, "%sdyld_stub_binding_helper\n",
29176 	   (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
29177 }
29178 
29179 /* Legitimize PIC addresses.  If the address is already
29180    position-independent, we return ORIG.  Newly generated
29181    position-independent addresses go into a reg.  This is REG if non
29182    zero, otherwise we allocate register(s) as necessary.  */
29183 
29184 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
29185 
29186 rtx
rs6000_machopic_legitimize_pic_address(rtx orig,enum machine_mode mode,rtx reg)29187 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
29188 					rtx reg)
29189 {
29190   rtx base, offset;
29191 
29192   if (reg == NULL && ! reload_in_progress && ! reload_completed)
29193     reg = gen_reg_rtx (Pmode);
29194 
29195   if (GET_CODE (orig) == CONST)
29196     {
29197       rtx reg_temp;
29198 
29199       if (GET_CODE (XEXP (orig, 0)) == PLUS
29200 	  && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
29201 	return orig;
29202 
29203       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
29204 
29205       /* Use a different reg for the intermediate value, as
29206 	 it will be marked UNCHANGING.  */
29207       reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
29208       base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
29209 						     Pmode, reg_temp);
29210       offset =
29211 	rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
29212 						Pmode, reg);
29213 
29214       if (GET_CODE (offset) == CONST_INT)
29215 	{
29216 	  if (SMALL_INT (offset))
29217 	    return plus_constant (Pmode, base, INTVAL (offset));
29218 	  else if (! reload_in_progress && ! reload_completed)
29219 	    offset = force_reg (Pmode, offset);
29220 	  else
29221 	    {
29222  	      rtx mem = force_const_mem (Pmode, orig);
29223 	      return machopic_legitimize_pic_address (mem, Pmode, reg);
29224 	    }
29225 	}
29226       return gen_rtx_PLUS (Pmode, base, offset);
29227     }
29228 
29229   /* Fall back on generic machopic code.  */
29230   return machopic_legitimize_pic_address (orig, mode, reg);
29231 }
29232 
29233 /* Output a .machine directive for the Darwin assembler, and call
29234    the generic start_file routine.  */
29235 
29236 static void
rs6000_darwin_file_start(void)29237 rs6000_darwin_file_start (void)
29238 {
29239   static const struct
29240   {
29241     const char *arg;
29242     const char *name;
29243     HOST_WIDE_INT if_set;
29244   } mapping[] = {
29245     { "ppc64", "ppc64", MASK_64BIT },
29246     { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
29247     { "power4", "ppc970", 0 },
29248     { "G5", "ppc970", 0 },
29249     { "7450", "ppc7450", 0 },
29250     { "7400", "ppc7400", MASK_ALTIVEC },
29251     { "G4", "ppc7400", 0 },
29252     { "750", "ppc750", 0 },
29253     { "740", "ppc750", 0 },
29254     { "G3", "ppc750", 0 },
29255     { "604e", "ppc604e", 0 },
29256     { "604", "ppc604", 0 },
29257     { "603e", "ppc603", 0 },
29258     { "603", "ppc603", 0 },
29259     { "601", "ppc601", 0 },
29260     { NULL, "ppc", 0 } };
29261   const char *cpu_id = "";
29262   size_t i;
29263 
29264   rs6000_file_start ();
29265   darwin_file_start ();
29266 
29267   /* Determine the argument to -mcpu=.  Default to G3 if not specified.  */
29268 
29269   if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
29270     cpu_id = rs6000_default_cpu;
29271 
29272   if (global_options_set.x_rs6000_cpu_index)
29273     cpu_id = processor_target_table[rs6000_cpu_index].name;
29274 
29275   /* Look through the mapping array.  Pick the first name that either
29276      matches the argument, has a bit set in IF_SET that is also set
29277      in the target flags, or has a NULL name.  */
29278 
29279   i = 0;
29280   while (mapping[i].arg != NULL
29281 	 && strcmp (mapping[i].arg, cpu_id) != 0
29282 	 && (mapping[i].if_set & rs6000_isa_flags) == 0)
29283     i++;
29284 
29285   fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
29286 }
29287 
29288 #endif /* TARGET_MACHO */
29289 
29290 #if TARGET_ELF
29291 static int
rs6000_elf_reloc_rw_mask(void)29292 rs6000_elf_reloc_rw_mask (void)
29293 {
29294   if (flag_pic)
29295     return 3;
29296   else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29297     return 2;
29298   else
29299     return 0;
29300 }
29301 
29302 /* Record an element in the table of global constructors.  SYMBOL is
29303    a SYMBOL_REF of the function to be called; PRIORITY is a number
29304    between 0 and MAX_INIT_PRIORITY.
29305 
29306    This differs from default_named_section_asm_out_constructor in
29307    that we have special handling for -mrelocatable.  */
29308 
29309 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
29310 static void
rs6000_elf_asm_out_constructor(rtx symbol,int priority)29311 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
29312 {
29313   const char *section = ".ctors";
29314   char buf[16];
29315 
29316   if (priority != DEFAULT_INIT_PRIORITY)
29317     {
29318       sprintf (buf, ".ctors.%.5u",
29319 	       /* Invert the numbering so the linker puts us in the proper
29320 		  order; constructors are run from right to left, and the
29321 		  linker sorts in increasing order.  */
29322 	       MAX_INIT_PRIORITY - priority);
29323       section = buf;
29324     }
29325 
29326   switch_to_section (get_section (section, SECTION_WRITE, NULL));
29327   assemble_align (POINTER_SIZE);
29328 
29329   if (TARGET_RELOCATABLE)
29330     {
29331       fputs ("\t.long (", asm_out_file);
29332       output_addr_const (asm_out_file, symbol);
29333       fputs (")@fixup\n", asm_out_file);
29334     }
29335   else
29336     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
29337 }
29338 
29339 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
29340 static void
rs6000_elf_asm_out_destructor(rtx symbol,int priority)29341 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
29342 {
29343   const char *section = ".dtors";
29344   char buf[16];
29345 
29346   if (priority != DEFAULT_INIT_PRIORITY)
29347     {
29348       sprintf (buf, ".dtors.%.5u",
29349 	       /* Invert the numbering so the linker puts us in the proper
29350 		  order; constructors are run from right to left, and the
29351 		  linker sorts in increasing order.  */
29352 	       MAX_INIT_PRIORITY - priority);
29353       section = buf;
29354     }
29355 
29356   switch_to_section (get_section (section, SECTION_WRITE, NULL));
29357   assemble_align (POINTER_SIZE);
29358 
29359   if (TARGET_RELOCATABLE)
29360     {
29361       fputs ("\t.long (", asm_out_file);
29362       output_addr_const (asm_out_file, symbol);
29363       fputs (")@fixup\n", asm_out_file);
29364     }
29365   else
29366     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
29367 }
29368 
29369 void
rs6000_elf_declare_function_name(FILE * file,const char * name,tree decl)29370 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
29371 {
29372   if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
29373     {
29374       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
29375       ASM_OUTPUT_LABEL (file, name);
29376       fputs (DOUBLE_INT_ASM_OP, file);
29377       rs6000_output_function_entry (file, name);
29378       fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
29379       if (DOT_SYMBOLS)
29380 	{
29381 	  fputs ("\t.size\t", file);
29382 	  assemble_name (file, name);
29383 	  fputs (",24\n\t.type\t.", file);
29384 	  assemble_name (file, name);
29385 	  fputs (",@function\n", file);
29386 	  if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
29387 	    {
29388 	      fputs ("\t.globl\t.", file);
29389 	      assemble_name (file, name);
29390 	      putc ('\n', file);
29391 	    }
29392 	}
29393       else
29394 	ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
29395       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
29396       rs6000_output_function_entry (file, name);
29397       fputs (":\n", file);
29398       return;
29399     }
29400 
29401   if (TARGET_RELOCATABLE
29402       && !TARGET_SECURE_PLT
29403       && (get_pool_size () != 0 || crtl->profile)
29404       && uses_TOC ())
29405     {
29406       char buf[256];
29407 
29408       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
29409 
29410       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
29411       fprintf (file, "\t.long ");
29412       assemble_name (file, buf);
29413       putc ('-', file);
29414       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
29415       assemble_name (file, buf);
29416       putc ('\n', file);
29417     }
29418 
29419   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
29420   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
29421 
29422   if (DEFAULT_ABI == ABI_AIX)
29423     {
29424       const char *desc_name, *orig_name;
29425 
29426       orig_name = (*targetm.strip_name_encoding) (name);
29427       desc_name = orig_name;
29428       while (*desc_name == '.')
29429 	desc_name++;
29430 
29431       if (TREE_PUBLIC (decl))
29432 	fprintf (file, "\t.globl %s\n", desc_name);
29433 
29434       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
29435       fprintf (file, "%s:\n", desc_name);
29436       fprintf (file, "\t.long %s\n", orig_name);
29437       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
29438       fputs ("\t.long 0\n", file);
29439       fprintf (file, "\t.previous\n");
29440     }
29441   ASM_OUTPUT_LABEL (file, name);
29442 }
29443 
29444 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
29445 static void
rs6000_elf_file_end(void)29446 rs6000_elf_file_end (void)
29447 {
29448 #ifdef HAVE_AS_GNU_ATTRIBUTE
29449   if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
29450     {
29451       if (rs6000_passes_float)
29452 	fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
29453 		 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
29454 		  : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
29455 		  : 2));
29456       if (rs6000_passes_vector)
29457 	fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
29458 		 (TARGET_ALTIVEC_ABI ? 2
29459 		  : TARGET_SPE_ABI ? 3
29460 		  : 1));
29461       if (rs6000_returns_struct)
29462 	fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
29463 		 aix_struct_return ? 2 : 1);
29464     }
29465 #endif
29466 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
29467   if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
29468     file_end_indicate_exec_stack ();
29469 #endif
29470 }
29471 #endif
29472 
29473 #if TARGET_XCOFF
29474 static void
rs6000_xcoff_asm_output_anchor(rtx symbol)29475 rs6000_xcoff_asm_output_anchor (rtx symbol)
29476 {
29477   char buffer[100];
29478 
29479   sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
29480 	   SYMBOL_REF_BLOCK_OFFSET (symbol));
29481   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
29482 }
29483 
29484 static void
rs6000_xcoff_asm_globalize_label(FILE * stream,const char * name)29485 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
29486 {
29487   fputs (GLOBAL_ASM_OP, stream);
29488   RS6000_OUTPUT_BASENAME (stream, name);
29489   putc ('\n', stream);
29490 }
29491 
29492 /* A get_unnamed_decl callback, used for read-only sections.  PTR
29493    points to the section string variable.  */
29494 
29495 static void
rs6000_xcoff_output_readonly_section_asm_op(const void * directive)29496 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
29497 {
29498   fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
29499 	   *(const char *const *) directive,
29500 	   XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29501 }
29502 
29503 /* Likewise for read-write sections.  */
29504 
29505 static void
rs6000_xcoff_output_readwrite_section_asm_op(const void * directive)29506 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
29507 {
29508   fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
29509 	   *(const char *const *) directive,
29510 	   XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29511 }
29512 
29513 static void
rs6000_xcoff_output_tls_section_asm_op(const void * directive)29514 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
29515 {
29516   fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
29517 	   *(const char *const *) directive,
29518 	   XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29519 }
29520 
29521 /* A get_unnamed_section callback, used for switching to toc_section.  */
29522 
29523 static void
rs6000_xcoff_output_toc_section_asm_op(const void * data ATTRIBUTE_UNUSED)29524 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
29525 {
29526   if (TARGET_MINIMAL_TOC)
29527     {
29528       /* toc_section is always selected at least once from
29529 	 rs6000_xcoff_file_start, so this is guaranteed to
29530 	 always be defined once and only once in each file.  */
29531       if (!toc_initialized)
29532 	{
29533 	  fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
29534 	  fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
29535 	  toc_initialized = 1;
29536 	}
29537       fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
29538 	       (TARGET_32BIT ? "" : ",3"));
29539     }
29540   else
29541     fputs ("\t.toc\n", asm_out_file);
29542 }
29543 
29544 /* Implement TARGET_ASM_INIT_SECTIONS.  */
29545 
29546 static void
rs6000_xcoff_asm_init_sections(void)29547 rs6000_xcoff_asm_init_sections (void)
29548 {
29549   read_only_data_section
29550     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29551 			   &xcoff_read_only_section_name);
29552 
29553   private_data_section
29554     = get_unnamed_section (SECTION_WRITE,
29555 			   rs6000_xcoff_output_readwrite_section_asm_op,
29556 			   &xcoff_private_data_section_name);
29557 
29558   tls_data_section
29559     = get_unnamed_section (SECTION_TLS,
29560 			   rs6000_xcoff_output_tls_section_asm_op,
29561 			   &xcoff_tls_data_section_name);
29562 
29563   tls_private_data_section
29564     = get_unnamed_section (SECTION_TLS,
29565 			   rs6000_xcoff_output_tls_section_asm_op,
29566 			   &xcoff_private_data_section_name);
29567 
29568   read_only_private_data_section
29569     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29570 			   &xcoff_private_data_section_name);
29571 
29572   toc_section
29573     = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
29574 
29575   readonly_data_section = read_only_data_section;
29576   exception_section = data_section;
29577 }
29578 
29579 static int
rs6000_xcoff_reloc_rw_mask(void)29580 rs6000_xcoff_reloc_rw_mask (void)
29581 {
29582   return 3;
29583 }
29584 
29585 static void
rs6000_xcoff_asm_named_section(const char * name,unsigned int flags,tree decl ATTRIBUTE_UNUSED)29586 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
29587 				tree decl ATTRIBUTE_UNUSED)
29588 {
29589   int smclass;
29590   static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
29591 
29592   if (flags & SECTION_CODE)
29593     smclass = 0;
29594   else if (flags & SECTION_TLS)
29595     smclass = 3;
29596   else if (flags & SECTION_WRITE)
29597     smclass = 2;
29598   else
29599     smclass = 1;
29600 
29601   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
29602 	   (flags & SECTION_CODE) ? "." : "",
29603 	   name, suffix[smclass], flags & SECTION_ENTSIZE);
29604 }
29605 
29606 #define IN_NAMED_SECTION(DECL) \
29607   ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
29608    && DECL_SECTION_NAME (DECL) != NULL_TREE)
29609 
29610 static section *
rs6000_xcoff_select_section(tree decl,int reloc,unsigned HOST_WIDE_INT align)29611 rs6000_xcoff_select_section (tree decl, int reloc,
29612 			     unsigned HOST_WIDE_INT align)
29613 {
29614   /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
29615      named section.  */
29616   if (align > BIGGEST_ALIGNMENT)
29617     {
29618       resolve_unique_section (decl, reloc, true);
29619       if (IN_NAMED_SECTION (decl))
29620 	return get_named_section (decl, NULL, reloc);
29621     }
29622 
29623   if (decl_readonly_section (decl, reloc))
29624     {
29625       if (TREE_PUBLIC (decl))
29626 	return read_only_data_section;
29627       else
29628 	return read_only_private_data_section;
29629     }
29630   else
29631     {
29632 #if HAVE_AS_TLS
29633       if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29634 	{
29635 	  if (TREE_PUBLIC (decl))
29636 	    return tls_data_section;
29637 	  else if (bss_initializer_p (decl))
29638 	    {
29639 	      /* Convert to COMMON to emit in BSS.  */
29640 	      DECL_COMMON (decl) = 1;
29641 	      return tls_comm_section;
29642 	    }
29643 	  else
29644 	    return tls_private_data_section;
29645 	}
29646       else
29647 #endif
29648 	if (TREE_PUBLIC (decl))
29649 	return data_section;
29650       else
29651 	return private_data_section;
29652     }
29653 }
29654 
29655 static void
rs6000_xcoff_unique_section(tree decl,int reloc ATTRIBUTE_UNUSED)29656 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
29657 {
29658   const char *name;
29659 
29660   /* Use select_section for private data and uninitialized data with
29661      alignment <= BIGGEST_ALIGNMENT.  */
29662   if (!TREE_PUBLIC (decl)
29663       || DECL_COMMON (decl)
29664       || (DECL_INITIAL (decl) == NULL_TREE
29665 	  && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
29666       || DECL_INITIAL (decl) == error_mark_node
29667       || (flag_zero_initialized_in_bss
29668 	  && initializer_zerop (DECL_INITIAL (decl))))
29669     return;
29670 
29671   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
29672   name = (*targetm.strip_name_encoding) (name);
29673   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
29674 }
29675 
29676 /* Select section for constant in constant pool.
29677 
29678    On RS/6000, all constants are in the private read-only data area.
29679    However, if this is being placed in the TOC it must be output as a
29680    toc entry.  */
29681 
29682 static section *
rs6000_xcoff_select_rtx_section(enum machine_mode mode,rtx x,unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)29683 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
29684 				 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
29685 {
29686   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
29687     return toc_section;
29688   else
29689     return read_only_private_data_section;
29690 }
29691 
29692 /* Remove any trailing [DS] or the like from the symbol name.  */
29693 
29694 static const char *
rs6000_xcoff_strip_name_encoding(const char * name)29695 rs6000_xcoff_strip_name_encoding (const char *name)
29696 {
29697   size_t len;
29698   if (*name == '*')
29699     name++;
29700   len = strlen (name);
29701   if (name[len - 1] == ']')
29702     return ggc_alloc_string (name, len - 4);
29703   else
29704     return name;
29705 }
29706 
29707 /* Section attributes.  AIX is always PIC.  */
29708 
29709 static unsigned int
rs6000_xcoff_section_type_flags(tree decl,const char * name,int reloc)29710 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
29711 {
29712   unsigned int align;
29713   unsigned int flags = default_section_type_flags (decl, name, reloc);
29714 
29715   /* Align to at least UNIT size.  */
29716   if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
29717     align = MIN_UNITS_PER_WORD;
29718   else
29719     /* Increase alignment of large objects if not already stricter.  */
29720     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
29721 		 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
29722 		 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
29723 
29724   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
29725 }
29726 
29727 /* Output at beginning of assembler file.
29728 
29729    Initialize the section names for the RS/6000 at this point.
29730 
29731    Specify filename, including full path, to assembler.
29732 
29733    We want to go into the TOC section so at least one .toc will be emitted.
29734    Also, in order to output proper .bs/.es pairs, we need at least one static
29735    [RW] section emitted.
29736 
29737    Finally, declare mcount when profiling to make the assembler happy.  */
29738 
29739 static void
rs6000_xcoff_file_start(void)29740 rs6000_xcoff_file_start (void)
29741 {
29742   rs6000_gen_section_name (&xcoff_bss_section_name,
29743 			   main_input_filename, ".bss_");
29744   rs6000_gen_section_name (&xcoff_private_data_section_name,
29745 			   main_input_filename, ".rw_");
29746   rs6000_gen_section_name (&xcoff_read_only_section_name,
29747 			   main_input_filename, ".ro_");
29748   rs6000_gen_section_name (&xcoff_tls_data_section_name,
29749 			   main_input_filename, ".tls_");
29750   rs6000_gen_section_name (&xcoff_tbss_section_name,
29751 			   main_input_filename, ".tbss_[UL]");
29752 
29753   fputs ("\t.file\t", asm_out_file);
29754   output_quoted_string (asm_out_file, main_input_filename);
29755   fputc ('\n', asm_out_file);
29756   if (write_symbols != NO_DEBUG)
29757     switch_to_section (private_data_section);
29758   switch_to_section (text_section);
29759   if (profile_flag)
29760     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
29761   rs6000_file_start ();
29762 }
29763 
29764 /* Output at end of assembler file.
29765    On the RS/6000, referencing data should automatically pull in text.  */
29766 
29767 static void
rs6000_xcoff_file_end(void)29768 rs6000_xcoff_file_end (void)
29769 {
29770   switch_to_section (text_section);
29771   fputs ("_section_.text:\n", asm_out_file);
29772   switch_to_section (data_section);
29773   fputs (TARGET_32BIT
29774 	 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
29775 	 asm_out_file);
29776 }
29777 
29778 #ifdef HAVE_AS_TLS
29779 static void
rs6000_xcoff_encode_section_info(tree decl,rtx rtl,int first)29780 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
29781 {
29782   rtx symbol;
29783   int flags;
29784 
29785   default_encode_section_info (decl, rtl, first);
29786 
29787   /* Careful not to prod global register variables.  */
29788   if (!MEM_P (rtl))
29789     return;
29790   symbol = XEXP (rtl, 0);
29791   if (GET_CODE (symbol) != SYMBOL_REF)
29792     return;
29793 
29794   flags = SYMBOL_REF_FLAGS (symbol);
29795 
29796   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29797     flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
29798 
29799   SYMBOL_REF_FLAGS (symbol) = flags;
29800 }
29801 #endif /* HAVE_AS_TLS */
29802 #endif /* TARGET_XCOFF */
29803 
29804 /* Compute a (partial) cost for rtx X.  Return true if the complete
29805    cost has been computed, and false if subexpressions should be
29806    scanned.  In either case, *TOTAL contains the cost result.  */
29807 
29808 static bool
rs6000_rtx_costs(rtx x,int code,int outer_code,int opno ATTRIBUTE_UNUSED,int * total,bool speed)29809 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
29810 		  int *total, bool speed)
29811 {
29812   enum machine_mode mode = GET_MODE (x);
29813 
29814   switch (code)
29815     {
29816       /* On the RS/6000, if it is valid in the insn, it is free.  */
29817     case CONST_INT:
29818       if (((outer_code == SET
29819 	    || outer_code == PLUS
29820 	    || outer_code == MINUS)
29821 	   && (satisfies_constraint_I (x)
29822 	       || satisfies_constraint_L (x)))
29823 	  || (outer_code == AND
29824 	      && (satisfies_constraint_K (x)
29825 		  || (mode == SImode
29826 		      ? satisfies_constraint_L (x)
29827 		      : satisfies_constraint_J (x))
29828 		  || mask_operand (x, mode)
29829 		  || (mode == DImode
29830 		      && mask64_operand (x, DImode))))
29831 	  || ((outer_code == IOR || outer_code == XOR)
29832 	      && (satisfies_constraint_K (x)
29833 		  || (mode == SImode
29834 		      ? satisfies_constraint_L (x)
29835 		      : satisfies_constraint_J (x))))
29836 	  || outer_code == ASHIFT
29837 	  || outer_code == ASHIFTRT
29838 	  || outer_code == LSHIFTRT
29839 	  || outer_code == ROTATE
29840 	  || outer_code == ROTATERT
29841 	  || outer_code == ZERO_EXTRACT
29842 	  || (outer_code == MULT
29843 	      && satisfies_constraint_I (x))
29844 	  || ((outer_code == DIV || outer_code == UDIV
29845 	       || outer_code == MOD || outer_code == UMOD)
29846 	      && exact_log2 (INTVAL (x)) >= 0)
29847 	  || (outer_code == COMPARE
29848 	      && (satisfies_constraint_I (x)
29849 		  || satisfies_constraint_K (x)))
29850 	  || ((outer_code == EQ || outer_code == NE)
29851 	      && (satisfies_constraint_I (x)
29852 		  || satisfies_constraint_K (x)
29853 		  || (mode == SImode
29854 		      ? satisfies_constraint_L (x)
29855 		      : satisfies_constraint_J (x))))
29856 	  || (outer_code == GTU
29857 	      && satisfies_constraint_I (x))
29858 	  || (outer_code == LTU
29859 	      && satisfies_constraint_P (x)))
29860 	{
29861 	  *total = 0;
29862 	  return true;
29863 	}
29864       else if ((outer_code == PLUS
29865 		&& reg_or_add_cint_operand (x, VOIDmode))
29866 	       || (outer_code == MINUS
29867 		   && reg_or_sub_cint_operand (x, VOIDmode))
29868 	       || ((outer_code == SET
29869 		    || outer_code == IOR
29870 		    || outer_code == XOR)
29871 		   && (INTVAL (x)
29872 		       & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
29873 	{
29874 	  *total = COSTS_N_INSNS (1);
29875 	  return true;
29876 	}
29877       /* FALLTHRU */
29878 
29879     case CONST_DOUBLE:
29880       if (mode == DImode && code == CONST_DOUBLE)
29881 	{
29882 	  if ((outer_code == IOR || outer_code == XOR)
29883 	      && CONST_DOUBLE_HIGH (x) == 0
29884 	      && (CONST_DOUBLE_LOW (x)
29885 		  & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0)
29886 	    {
29887 	      *total = 0;
29888 	      return true;
29889 	    }
29890 	  else if ((outer_code == AND && and64_2_operand (x, DImode))
29891 		   || ((outer_code == SET
29892 			|| outer_code == IOR
29893 			|| outer_code == XOR)
29894 		       && CONST_DOUBLE_HIGH (x) == 0))
29895 	    {
29896 	      *total = COSTS_N_INSNS (1);
29897 	      return true;
29898 	    }
29899 	}
29900       /* FALLTHRU */
29901 
29902     case CONST:
29903     case HIGH:
29904     case SYMBOL_REF:
29905     case MEM:
29906       /* When optimizing for size, MEM should be slightly more expensive
29907 	 than generating address, e.g., (plus (reg) (const)).
29908 	 L1 cache latency is about two instructions.  */
29909       *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
29910       return true;
29911 
29912     case LABEL_REF:
29913       *total = 0;
29914       return true;
29915 
29916     case PLUS:
29917     case MINUS:
29918       if (FLOAT_MODE_P (mode))
29919 	*total = rs6000_cost->fp;
29920       else
29921 	*total = COSTS_N_INSNS (1);
29922       return false;
29923 
29924     case MULT:
29925       if (GET_CODE (XEXP (x, 1)) == CONST_INT
29926 	  && satisfies_constraint_I (XEXP (x, 1)))
29927 	{
29928 	  if (INTVAL (XEXP (x, 1)) >= -256
29929 	      && INTVAL (XEXP (x, 1)) <= 255)
29930 	    *total = rs6000_cost->mulsi_const9;
29931 	  else
29932 	    *total = rs6000_cost->mulsi_const;
29933 	}
29934       else if (mode == SFmode)
29935 	*total = rs6000_cost->fp;
29936       else if (FLOAT_MODE_P (mode))
29937 	*total = rs6000_cost->dmul;
29938       else if (mode == DImode)
29939 	*total = rs6000_cost->muldi;
29940       else
29941 	*total = rs6000_cost->mulsi;
29942       return false;
29943 
29944     case FMA:
29945       if (mode == SFmode)
29946 	*total = rs6000_cost->fp;
29947       else
29948 	*total = rs6000_cost->dmul;
29949       break;
29950 
29951     case DIV:
29952     case MOD:
29953       if (FLOAT_MODE_P (mode))
29954 	{
29955 	  *total = mode == DFmode ? rs6000_cost->ddiv
29956 				  : rs6000_cost->sdiv;
29957 	  return false;
29958 	}
29959       /* FALLTHRU */
29960 
29961     case UDIV:
29962     case UMOD:
29963       if (GET_CODE (XEXP (x, 1)) == CONST_INT
29964 	  && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
29965 	{
29966 	  if (code == DIV || code == MOD)
29967 	    /* Shift, addze */
29968 	    *total = COSTS_N_INSNS (2);
29969 	  else
29970 	    /* Shift */
29971 	    *total = COSTS_N_INSNS (1);
29972 	}
29973       else
29974 	{
29975 	  if (GET_MODE (XEXP (x, 1)) == DImode)
29976 	    *total = rs6000_cost->divdi;
29977 	  else
29978 	    *total = rs6000_cost->divsi;
29979 	}
29980       /* Add in shift and subtract for MOD. */
29981       if (code == MOD || code == UMOD)
29982 	*total += COSTS_N_INSNS (2);
29983       return false;
29984 
29985     case CTZ:
29986     case FFS:
29987       *total = COSTS_N_INSNS (4);
29988       return false;
29989 
29990     case POPCOUNT:
29991       *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
29992       return false;
29993 
29994     case PARITY:
29995       *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
29996       return false;
29997 
29998     case NOT:
29999       if (outer_code == AND || outer_code == IOR || outer_code == XOR)
30000 	{
30001 	  *total = 0;
30002 	  return false;
30003 	}
30004       /* FALLTHRU */
30005 
30006     case AND:
30007     case CLZ:
30008     case IOR:
30009     case XOR:
30010     case ZERO_EXTRACT:
30011       *total = COSTS_N_INSNS (1);
30012       return false;
30013 
30014     case ASHIFT:
30015     case ASHIFTRT:
30016     case LSHIFTRT:
30017     case ROTATE:
30018     case ROTATERT:
30019       /* Handle mul_highpart.  */
30020       if (outer_code == TRUNCATE
30021 	  && GET_CODE (XEXP (x, 0)) == MULT)
30022 	{
30023 	  if (mode == DImode)
30024 	    *total = rs6000_cost->muldi;
30025 	  else
30026 	    *total = rs6000_cost->mulsi;
30027 	  return true;
30028 	}
30029       else if (outer_code == AND)
30030 	*total = 0;
30031       else
30032 	*total = COSTS_N_INSNS (1);
30033       return false;
30034 
30035     case SIGN_EXTEND:
30036     case ZERO_EXTEND:
30037       if (GET_CODE (XEXP (x, 0)) == MEM)
30038 	*total = 0;
30039       else
30040 	*total = COSTS_N_INSNS (1);
30041       return false;
30042 
30043     case COMPARE:
30044     case NEG:
30045     case ABS:
30046       if (!FLOAT_MODE_P (mode))
30047 	{
30048 	  *total = COSTS_N_INSNS (1);
30049 	  return false;
30050 	}
30051       /* FALLTHRU */
30052 
30053     case FLOAT:
30054     case UNSIGNED_FLOAT:
30055     case FIX:
30056     case UNSIGNED_FIX:
30057     case FLOAT_TRUNCATE:
30058       *total = rs6000_cost->fp;
30059       return false;
30060 
30061     case FLOAT_EXTEND:
30062       if (mode == DFmode)
30063 	*total = 0;
30064       else
30065 	*total = rs6000_cost->fp;
30066       return false;
30067 
30068     case UNSPEC:
30069       switch (XINT (x, 1))
30070 	{
30071 	case UNSPEC_FRSP:
30072 	  *total = rs6000_cost->fp;
30073 	  return true;
30074 
30075 	default:
30076 	  break;
30077 	}
30078       break;
30079 
30080     case CALL:
30081     case IF_THEN_ELSE:
30082       if (!speed)
30083 	{
30084 	  *total = COSTS_N_INSNS (1);
30085 	  return true;
30086 	}
30087       else if (FLOAT_MODE_P (mode)
30088 	       && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
30089 	{
30090 	  *total = rs6000_cost->fp;
30091 	  return false;
30092 	}
30093       break;
30094 
30095     case EQ:
30096     case GTU:
30097     case LTU:
30098       /* Carry bit requires mode == Pmode.
30099 	 NEG or PLUS already counted so only add one.  */
30100       if (mode == Pmode
30101 	  && (outer_code == NEG || outer_code == PLUS))
30102 	{
30103 	  *total = COSTS_N_INSNS (1);
30104 	  return true;
30105 	}
30106       if (outer_code == SET)
30107 	{
30108 	  if (XEXP (x, 1) == const0_rtx)
30109 	    {
30110 	      if (TARGET_ISEL && !TARGET_MFCRF)
30111 		*total = COSTS_N_INSNS (8);
30112 	      else
30113 		*total = COSTS_N_INSNS (2);
30114 	      return true;
30115 	    }
30116 	  else if (mode == Pmode)
30117 	    {
30118 	      *total = COSTS_N_INSNS (3);
30119 	      return false;
30120 	    }
30121 	}
30122       /* FALLTHRU */
30123 
30124     case GT:
30125     case LT:
30126     case UNORDERED:
30127       if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
30128 	{
30129 	  if (TARGET_ISEL && !TARGET_MFCRF)
30130 	    *total = COSTS_N_INSNS (8);
30131 	  else
30132 	    *total = COSTS_N_INSNS (2);
30133 	  return true;
30134 	}
30135       /* CC COMPARE.  */
30136       if (outer_code == COMPARE)
30137 	{
30138 	  *total = 0;
30139 	  return true;
30140 	}
30141       break;
30142 
30143     default:
30144       break;
30145     }
30146 
30147   return false;
30148 }
30149 
30150 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost.  */
30151 
30152 static bool
rs6000_debug_rtx_costs(rtx x,int code,int outer_code,int opno,int * total,bool speed)30153 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
30154 			bool speed)
30155 {
30156   bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
30157 
30158   fprintf (stderr,
30159 	   "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
30160 	   "opno = %d, total = %d, speed = %s, x:\n",
30161 	   ret ? "complete" : "scan inner",
30162 	   GET_RTX_NAME (code),
30163 	   GET_RTX_NAME (outer_code),
30164 	   opno,
30165 	   *total,
30166 	   speed ? "true" : "false");
30167 
30168   debug_rtx (x);
30169 
30170   return ret;
30171 }
30172 
30173 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost.  */
30174 
30175 static int
rs6000_debug_address_cost(rtx x,enum machine_mode mode,addr_space_t as,bool speed)30176 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
30177 			   addr_space_t as, bool speed)
30178 {
30179   int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
30180 
30181   fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
30182 	   ret, speed ? "true" : "false");
30183   debug_rtx (x);
30184 
30185   return ret;
30186 }
30187 
30188 
30189 /* A C expression returning the cost of moving data from a register of class
30190    CLASS1 to one of CLASS2.  */
30191 
30192 static int
rs6000_register_move_cost(enum machine_mode mode,reg_class_t from,reg_class_t to)30193 rs6000_register_move_cost (enum machine_mode mode,
30194 			   reg_class_t from, reg_class_t to)
30195 {
30196   int ret;
30197 
30198   if (TARGET_DEBUG_COST)
30199     dbg_cost_ctrl++;
30200 
30201   /*  Moves from/to GENERAL_REGS.  */
30202   if (reg_classes_intersect_p (to, GENERAL_REGS)
30203       || reg_classes_intersect_p (from, GENERAL_REGS))
30204     {
30205       reg_class_t rclass = from;
30206 
30207       if (! reg_classes_intersect_p (to, GENERAL_REGS))
30208 	rclass = to;
30209 
30210       if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
30211 	ret = (rs6000_memory_move_cost (mode, rclass, false)
30212 	       + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
30213 
30214       /* It's more expensive to move CR_REGS than CR0_REGS because of the
30215 	 shift.  */
30216       else if (rclass == CR_REGS)
30217 	ret = 4;
30218 
30219       /* For those processors that have slow LR/CTR moves, make them more
30220          expensive than memory in order to bias spills to memory .*/
30221       else if ((rs6000_cpu == PROCESSOR_POWER6
30222 		|| rs6000_cpu == PROCESSOR_POWER7
30223 		|| rs6000_cpu == PROCESSOR_POWER8)
30224 	       && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
30225         ret = 6 * hard_regno_nregs[0][mode];
30226 
30227       else
30228 	/* A move will cost one instruction per GPR moved.  */
30229 	ret = 2 * hard_regno_nregs[0][mode];
30230     }
30231 
30232   /* If we have VSX, we can easily move between FPR or Altivec registers.  */
30233   else if (VECTOR_MEM_VSX_P (mode)
30234 	   && reg_classes_intersect_p (to, VSX_REGS)
30235 	   && reg_classes_intersect_p (from, VSX_REGS))
30236     ret = 2 * hard_regno_nregs[32][mode];
30237 
30238   /* Moving between two similar registers is just one instruction.  */
30239   else if (reg_classes_intersect_p (to, from))
30240     ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
30241 
30242   /* Everything else has to go through GENERAL_REGS.  */
30243   else
30244     ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
30245 	   + rs6000_register_move_cost (mode, from, GENERAL_REGS));
30246 
30247   if (TARGET_DEBUG_COST)
30248     {
30249       if (dbg_cost_ctrl == 1)
30250 	fprintf (stderr,
30251 		 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
30252 		 ret, GET_MODE_NAME (mode), reg_class_names[from],
30253 		 reg_class_names[to]);
30254       dbg_cost_ctrl--;
30255     }
30256 
30257   return ret;
30258 }
30259 
30260 /* A C expressions returning the cost of moving data of MODE from a register to
30261    or from memory.  */
30262 
30263 static int
rs6000_memory_move_cost(enum machine_mode mode,reg_class_t rclass,bool in ATTRIBUTE_UNUSED)30264 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
30265 			 bool in ATTRIBUTE_UNUSED)
30266 {
30267   int ret;
30268 
30269   if (TARGET_DEBUG_COST)
30270     dbg_cost_ctrl++;
30271 
30272   if (reg_classes_intersect_p (rclass, GENERAL_REGS))
30273     ret = 4 * hard_regno_nregs[0][mode];
30274   else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
30275 	    || reg_classes_intersect_p (rclass, VSX_REGS)))
30276     ret = 4 * hard_regno_nregs[32][mode];
30277   else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
30278     ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
30279   else
30280     ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
30281 
30282   if (TARGET_DEBUG_COST)
30283     {
30284       if (dbg_cost_ctrl == 1)
30285 	fprintf (stderr,
30286 		 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
30287 		 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
30288       dbg_cost_ctrl--;
30289     }
30290 
30291   return ret;
30292 }
30293 
30294 /* Returns a code for a target-specific builtin that implements
30295    reciprocal of the function, or NULL_TREE if not available.  */
30296 
30297 static tree
rs6000_builtin_reciprocal(unsigned int fn,bool md_fn,bool sqrt ATTRIBUTE_UNUSED)30298 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
30299 			   bool sqrt ATTRIBUTE_UNUSED)
30300 {
30301   if (optimize_insn_for_size_p ())
30302     return NULL_TREE;
30303 
30304   if (md_fn)
30305     switch (fn)
30306       {
30307       case VSX_BUILTIN_XVSQRTDP:
30308 	if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
30309 	  return NULL_TREE;
30310 
30311 	return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
30312 
30313       case VSX_BUILTIN_XVSQRTSP:
30314 	if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
30315 	  return NULL_TREE;
30316 
30317 	return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
30318 
30319       default:
30320 	return NULL_TREE;
30321       }
30322 
30323   else
30324     switch (fn)
30325       {
30326       case BUILT_IN_SQRT:
30327 	if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
30328 	  return NULL_TREE;
30329 
30330 	return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
30331 
30332       case BUILT_IN_SQRTF:
30333 	if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
30334 	  return NULL_TREE;
30335 
30336 	return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
30337 
30338       default:
30339 	return NULL_TREE;
30340       }
30341 }
30342 
30343 /* Load up a constant.  If the mode is a vector mode, splat the value across
30344    all of the vector elements.  */
30345 
30346 static rtx
rs6000_load_constant_and_splat(enum machine_mode mode,REAL_VALUE_TYPE dconst)30347 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
30348 {
30349   rtx reg;
30350 
30351   if (mode == SFmode || mode == DFmode)
30352     {
30353       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
30354       reg = force_reg (mode, d);
30355     }
30356   else if (mode == V4SFmode)
30357     {
30358       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
30359       rtvec v = gen_rtvec (4, d, d, d, d);
30360       reg = gen_reg_rtx (mode);
30361       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
30362     }
30363   else if (mode == V2DFmode)
30364     {
30365       rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
30366       rtvec v = gen_rtvec (2, d, d);
30367       reg = gen_reg_rtx (mode);
30368       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
30369     }
30370   else
30371     gcc_unreachable ();
30372 
30373   return reg;
30374 }
30375 
30376 /* Generate an FMA instruction.  */
30377 
30378 static void
rs6000_emit_madd(rtx target,rtx m1,rtx m2,rtx a)30379 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
30380 {
30381   enum machine_mode mode = GET_MODE (target);
30382   rtx dst;
30383 
30384   dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
30385   gcc_assert (dst != NULL);
30386 
30387   if (dst != target)
30388     emit_move_insn (target, dst);
30389 }
30390 
30391 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a).  */
30392 
30393 static void
rs6000_emit_msub(rtx target,rtx m1,rtx m2,rtx a)30394 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
30395 {
30396   enum machine_mode mode = GET_MODE (target);
30397   rtx dst;
30398 
30399   /* Altivec does not support fms directly;
30400      generate in terms of fma in that case.  */
30401   if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
30402     dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
30403   else
30404     {
30405       a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
30406       dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
30407     }
30408   gcc_assert (dst != NULL);
30409 
30410   if (dst != target)
30411     emit_move_insn (target, dst);
30412 }
30413 
30414 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a).  */
30415 
30416 static void
rs6000_emit_nmsub(rtx dst,rtx m1,rtx m2,rtx a)30417 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
30418 {
30419   enum machine_mode mode = GET_MODE (dst);
30420   rtx r;
30421 
30422   /* This is a tad more complicated, since the fnma_optab is for
30423      a different expression: fma(-m1, m2, a), which is the same
30424      thing except in the case of signed zeros.
30425 
30426      Fortunately we know that if FMA is supported that FNMSUB is
30427      also supported in the ISA.  Just expand it directly.  */
30428 
30429   gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
30430 
30431   r = gen_rtx_NEG (mode, a);
30432   r = gen_rtx_FMA (mode, m1, m2, r);
30433   r = gen_rtx_NEG (mode, r);
30434   emit_insn (gen_rtx_SET (VOIDmode, dst, r));
30435 }
30436 
30437 /* Newton-Raphson approximation of floating point divide DST = N/D.  If NOTE_P,
30438    add a reg_note saying that this was a division.  Support both scalar and
30439    vector divide.  Assumes no trapping math and finite arguments.  */
30440 
30441 void
rs6000_emit_swdiv(rtx dst,rtx n,rtx d,bool note_p)30442 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
30443 {
30444   enum machine_mode mode = GET_MODE (dst);
30445   rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
30446   int i;
30447 
30448   /* Low precision estimates guarantee 5 bits of accuracy.  High
30449      precision estimates guarantee 14 bits of accuracy.  SFmode
30450      requires 23 bits of accuracy.  DFmode requires 52 bits of
30451      accuracy.  Each pass at least doubles the accuracy, leading
30452      to the following.  */
30453   int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
30454   if (mode == DFmode || mode == V2DFmode)
30455     passes++;
30456 
30457   enum insn_code code = optab_handler (smul_optab, mode);
30458   insn_gen_fn gen_mul = GEN_FCN (code);
30459 
30460   gcc_assert (code != CODE_FOR_nothing);
30461 
30462   one = rs6000_load_constant_and_splat (mode, dconst1);
30463 
30464   /* x0 = 1./d estimate */
30465   x0 = gen_reg_rtx (mode);
30466   emit_insn (gen_rtx_SET (VOIDmode, x0,
30467 			  gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
30468 					  UNSPEC_FRES)));
30469 
30470   /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i).  */
30471   if (passes > 1) {
30472 
30473     /* e0 = 1. - d * x0  */
30474     e0 = gen_reg_rtx (mode);
30475     rs6000_emit_nmsub (e0, d, x0, one);
30476 
30477     /* x1 = x0 + e0 * x0  */
30478     x1 = gen_reg_rtx (mode);
30479     rs6000_emit_madd (x1, e0, x0, x0);
30480 
30481     for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
30482 	 ++i, xprev = xnext, eprev = enext) {
30483 
30484       /* enext = eprev * eprev  */
30485       enext = gen_reg_rtx (mode);
30486       emit_insn (gen_mul (enext, eprev, eprev));
30487 
30488       /* xnext = xprev + enext * xprev  */
30489       xnext = gen_reg_rtx (mode);
30490       rs6000_emit_madd (xnext, enext, xprev, xprev);
30491     }
30492 
30493   } else
30494     xprev = x0;
30495 
30496   /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i).  */
30497 
30498   /* u = n * xprev  */
30499   u = gen_reg_rtx (mode);
30500   emit_insn (gen_mul (u, n, xprev));
30501 
30502   /* v = n - (d * u)  */
30503   v = gen_reg_rtx (mode);
30504   rs6000_emit_nmsub (v, d, u, n);
30505 
30506   /* dst = (v * xprev) + u  */
30507   rs6000_emit_madd (dst, v, xprev, u);
30508 
30509   if (note_p)
30510     add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
30511 }
30512 
30513 /* Newton-Raphson approximation of single/double-precision floating point
30514    rsqrt.  Assumes no trapping math and finite arguments.  */
30515 
30516 void
rs6000_emit_swrsqrt(rtx dst,rtx src)30517 rs6000_emit_swrsqrt (rtx dst, rtx src)
30518 {
30519   enum machine_mode mode = GET_MODE (src);
30520   rtx x0 = gen_reg_rtx (mode);
30521   rtx y = gen_reg_rtx (mode);
30522 
30523   /* Low precision estimates guarantee 5 bits of accuracy.  High
30524      precision estimates guarantee 14 bits of accuracy.  SFmode
30525      requires 23 bits of accuracy.  DFmode requires 52 bits of
30526      accuracy.  Each pass at least doubles the accuracy, leading
30527      to the following.  */
30528   int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
30529   if (mode == DFmode || mode == V2DFmode)
30530     passes++;
30531 
30532   REAL_VALUE_TYPE dconst3_2;
30533   int i;
30534   rtx halfthree;
30535   enum insn_code code = optab_handler (smul_optab, mode);
30536   insn_gen_fn gen_mul = GEN_FCN (code);
30537 
30538   gcc_assert (code != CODE_FOR_nothing);
30539 
30540   /* Load up the constant 1.5 either as a scalar, or as a vector.  */
30541   real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
30542   SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
30543 
30544   halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
30545 
30546   /* x0 = rsqrt estimate */
30547   emit_insn (gen_rtx_SET (VOIDmode, x0,
30548 			  gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
30549 					  UNSPEC_RSQRT)));
30550 
30551   /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
30552   rs6000_emit_msub (y, src, halfthree, src);
30553 
30554   for (i = 0; i < passes; i++)
30555     {
30556       rtx x1 = gen_reg_rtx (mode);
30557       rtx u = gen_reg_rtx (mode);
30558       rtx v = gen_reg_rtx (mode);
30559 
30560       /* x1 = x0 * (1.5 - y * (x0 * x0)) */
30561       emit_insn (gen_mul (u, x0, x0));
30562       rs6000_emit_nmsub (v, y, u, halfthree);
30563       emit_insn (gen_mul (x1, x0, v));
30564       x0 = x1;
30565     }
30566 
30567   emit_move_insn (dst, x0);
30568   return;
30569 }
30570 
30571 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
30572    (Power7) targets.  DST is the target, and SRC is the argument operand.  */
30573 
30574 void
rs6000_emit_popcount(rtx dst,rtx src)30575 rs6000_emit_popcount (rtx dst, rtx src)
30576 {
30577   enum machine_mode mode = GET_MODE (dst);
30578   rtx tmp1, tmp2;
30579 
30580   /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can.  */
30581   if (TARGET_POPCNTD)
30582     {
30583       if (mode == SImode)
30584 	emit_insn (gen_popcntdsi2 (dst, src));
30585       else
30586 	emit_insn (gen_popcntddi2 (dst, src));
30587       return;
30588     }
30589 
30590   tmp1 = gen_reg_rtx (mode);
30591 
30592   if (mode == SImode)
30593     {
30594       emit_insn (gen_popcntbsi2 (tmp1, src));
30595       tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
30596 			   NULL_RTX, 0);
30597       tmp2 = force_reg (SImode, tmp2);
30598       emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
30599     }
30600   else
30601     {
30602       emit_insn (gen_popcntbdi2 (tmp1, src));
30603       tmp2 = expand_mult (DImode, tmp1,
30604 			  GEN_INT ((HOST_WIDE_INT)
30605 				   0x01010101 << 32 | 0x01010101),
30606 			  NULL_RTX, 0);
30607       tmp2 = force_reg (DImode, tmp2);
30608       emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
30609     }
30610 }
30611 
30612 
30613 /* Emit parity intrinsic on TARGET_POPCNTB targets.  DST is the
30614    target, and SRC is the argument operand.  */
30615 
30616 void
rs6000_emit_parity(rtx dst,rtx src)30617 rs6000_emit_parity (rtx dst, rtx src)
30618 {
30619   enum machine_mode mode = GET_MODE (dst);
30620   rtx tmp;
30621 
30622   tmp = gen_reg_rtx (mode);
30623 
30624   /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can.  */
30625   if (TARGET_CMPB)
30626     {
30627       if (mode == SImode)
30628 	{
30629 	  emit_insn (gen_popcntbsi2 (tmp, src));
30630 	  emit_insn (gen_paritysi2_cmpb (dst, tmp));
30631 	}
30632       else
30633 	{
30634 	  emit_insn (gen_popcntbdi2 (tmp, src));
30635 	  emit_insn (gen_paritydi2_cmpb (dst, tmp));
30636 	}
30637       return;
30638     }
30639 
30640   if (mode == SImode)
30641     {
30642       /* Is mult+shift >= shift+xor+shift+xor?  */
30643       if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
30644 	{
30645 	  rtx tmp1, tmp2, tmp3, tmp4;
30646 
30647 	  tmp1 = gen_reg_rtx (SImode);
30648 	  emit_insn (gen_popcntbsi2 (tmp1, src));
30649 
30650 	  tmp2 = gen_reg_rtx (SImode);
30651 	  emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
30652 	  tmp3 = gen_reg_rtx (SImode);
30653 	  emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
30654 
30655 	  tmp4 = gen_reg_rtx (SImode);
30656 	  emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
30657 	  emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
30658 	}
30659       else
30660 	rs6000_emit_popcount (tmp, src);
30661       emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
30662     }
30663   else
30664     {
30665       /* Is mult+shift >= shift+xor+shift+xor+shift+xor?  */
30666       if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
30667 	{
30668 	  rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
30669 
30670 	  tmp1 = gen_reg_rtx (DImode);
30671 	  emit_insn (gen_popcntbdi2 (tmp1, src));
30672 
30673 	  tmp2 = gen_reg_rtx (DImode);
30674 	  emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
30675 	  tmp3 = gen_reg_rtx (DImode);
30676 	  emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
30677 
30678 	  tmp4 = gen_reg_rtx (DImode);
30679 	  emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
30680 	  tmp5 = gen_reg_rtx (DImode);
30681 	  emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
30682 
30683 	  tmp6 = gen_reg_rtx (DImode);
30684 	  emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
30685 	  emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
30686 	}
30687       else
30688         rs6000_emit_popcount (tmp, src);
30689       emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
30690     }
30691 }
30692 
30693 /* Expand an Altivec constant permutation for little endian mode.
30694    There are two issues: First, the two input operands must be
30695    swapped so that together they form a double-wide array in LE
30696    order.  Second, the vperm instruction has surprising behavior
30697    in LE mode:  it interprets the elements of the source vectors
30698    in BE mode ("left to right") and interprets the elements of
30699    the destination vector in LE mode ("right to left").  To
30700    correct for this, we must subtract each element of the permute
30701    control vector from 31.
30702 
30703    For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
30704    with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
30705    We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
30706    serve as the permute control vector.  Then, in BE mode,
30707 
30708      vperm 9,10,11,12
30709 
30710    places the desired result in vr9.  However, in LE mode the
30711    vector contents will be
30712 
30713      vr10 = 00000003 00000002 00000001 00000000
30714      vr11 = 00000007 00000006 00000005 00000004
30715 
30716    The result of the vperm using the same permute control vector is
30717 
30718      vr9  = 05000000 07000000 01000000 03000000
30719 
30720    That is, the leftmost 4 bytes of vr10 are interpreted as the
30721    source for the rightmost 4 bytes of vr9, and so on.
30722 
30723    If we change the permute control vector to
30724 
30725      vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
30726 
30727    and issue
30728 
30729      vperm 9,11,10,12
30730 
30731    we get the desired
30732 
30733    vr9  = 00000006 00000004 00000002 00000000.  */
30734 
30735 void
altivec_expand_vec_perm_const_le(rtx operands[4])30736 altivec_expand_vec_perm_const_le (rtx operands[4])
30737 {
30738   unsigned int i;
30739   rtx perm[16];
30740   rtx constv, unspec;
30741   rtx target = operands[0];
30742   rtx op0 = operands[1];
30743   rtx op1 = operands[2];
30744   rtx sel = operands[3];
30745 
30746   /* Unpack and adjust the constant selector.  */
30747   for (i = 0; i < 16; ++i)
30748     {
30749       rtx e = XVECEXP (sel, 0, i);
30750       unsigned int elt = 31 - (INTVAL (e) & 31);
30751       perm[i] = GEN_INT (elt);
30752     }
30753 
30754   /* Expand to a permute, swapping the inputs and using the
30755      adjusted selector.  */
30756   if (!REG_P (op0))
30757     op0 = force_reg (V16QImode, op0);
30758   if (!REG_P (op1))
30759     op1 = force_reg (V16QImode, op1);
30760 
30761   constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
30762   constv = force_reg (V16QImode, constv);
30763   unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
30764 			   UNSPEC_VPERM);
30765   if (!REG_P (target))
30766     {
30767       rtx tmp = gen_reg_rtx (V16QImode);
30768       emit_move_insn (tmp, unspec);
30769       unspec = tmp;
30770     }
30771 
30772   emit_move_insn (target, unspec);
30773 }
30774 
30775 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
30776    permute control vector.  But here it's not a constant, so we must
30777    generate a vector NAND or NOR to do the adjustment.  */
30778 
30779 void
altivec_expand_vec_perm_le(rtx operands[4])30780 altivec_expand_vec_perm_le (rtx operands[4])
30781 {
30782   rtx notx, iorx, unspec;
30783   rtx target = operands[0];
30784   rtx op0 = operands[1];
30785   rtx op1 = operands[2];
30786   rtx sel = operands[3];
30787   rtx tmp = target;
30788   rtx norreg = gen_reg_rtx (V16QImode);
30789   enum machine_mode mode = GET_MODE (target);
30790 
30791   /* Get everything in regs so the pattern matches.  */
30792   if (!REG_P (op0))
30793     op0 = force_reg (mode, op0);
30794   if (!REG_P (op1))
30795     op1 = force_reg (mode, op1);
30796   if (!REG_P (sel))
30797     sel = force_reg (V16QImode, sel);
30798   if (!REG_P (target))
30799     tmp = gen_reg_rtx (mode);
30800 
30801   /* Invert the selector with a VNAND if available, else a VNOR.
30802      The VNAND is preferred for future fusion opportunities.  */
30803   notx = gen_rtx_NOT (V16QImode, sel);
30804   iorx = (TARGET_P8_VECTOR
30805 	  ? gen_rtx_IOR (V16QImode, notx, notx)
30806 	  : gen_rtx_AND (V16QImode, notx, notx));
30807   emit_insn (gen_rtx_SET (VOIDmode, norreg, iorx));
30808 
30809   /* Permute with operands reversed and adjusted selector.  */
30810   unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
30811 			   UNSPEC_VPERM);
30812 
30813   /* Copy into target, possibly by way of a register.  */
30814   if (!REG_P (target))
30815     {
30816       emit_move_insn (tmp, unspec);
30817       unspec = tmp;
30818     }
30819 
30820   emit_move_insn (target, unspec);
30821 }
30822 
30823 /* Expand an Altivec constant permutation.  Return true if we match
30824    an efficient implementation; false to fall back to VPERM.  */
30825 
30826 bool
altivec_expand_vec_perm_const(rtx operands[4])30827 altivec_expand_vec_perm_const (rtx operands[4])
30828 {
30829   struct altivec_perm_insn {
30830     HOST_WIDE_INT mask;
30831     enum insn_code impl;
30832     unsigned char perm[16];
30833   };
30834   static const struct altivec_perm_insn patterns[] = {
30835     { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
30836       {  1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
30837     { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
30838       {  2,  3,  6,  7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
30839     { OPTION_MASK_ALTIVEC,
30840       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
30841        : CODE_FOR_altivec_vmrglb_direct),
30842       {  0, 16,  1, 17,  2, 18,  3, 19,  4, 20,  5, 21,  6, 22,  7, 23 } },
30843     { OPTION_MASK_ALTIVEC,
30844       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
30845        : CODE_FOR_altivec_vmrglh_direct),
30846       {  0,  1, 16, 17,  2,  3, 18, 19,  4,  5, 20, 21,  6,  7, 22, 23 } },
30847     { OPTION_MASK_ALTIVEC,
30848       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
30849        : CODE_FOR_altivec_vmrglw_direct),
30850       {  0,  1,  2,  3, 16, 17, 18, 19,  4,  5,  6,  7, 20, 21, 22, 23 } },
30851     { OPTION_MASK_ALTIVEC,
30852       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
30853        : CODE_FOR_altivec_vmrghb_direct),
30854       {  8, 24,  9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
30855     { OPTION_MASK_ALTIVEC,
30856       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
30857        : CODE_FOR_altivec_vmrghh_direct),
30858       {  8,  9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
30859     { OPTION_MASK_ALTIVEC,
30860       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
30861        : CODE_FOR_altivec_vmrghw_direct),
30862       {  8,  9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
30863     { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
30864       {  0,  1,  2,  3, 16, 17, 18, 19,  8,  9, 10, 11, 24, 25, 26, 27 } },
30865     { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
30866       {  4,  5,  6,  7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
30867   };
30868 
30869   unsigned int i, j, elt, which;
30870   unsigned char perm[16];
30871   rtx target, op0, op1, sel, x;
30872   bool one_vec;
30873 
30874   target = operands[0];
30875   op0 = operands[1];
30876   op1 = operands[2];
30877   sel = operands[3];
30878 
30879   /* Unpack the constant selector.  */
30880   for (i = which = 0; i < 16; ++i)
30881     {
30882       rtx e = XVECEXP (sel, 0, i);
30883       elt = INTVAL (e) & 31;
30884       which |= (elt < 16 ? 1 : 2);
30885       perm[i] = elt;
30886     }
30887 
30888   /* Simplify the constant selector based on operands.  */
30889   switch (which)
30890     {
30891     default:
30892       gcc_unreachable ();
30893 
30894     case 3:
30895       one_vec = false;
30896       if (!rtx_equal_p (op0, op1))
30897 	break;
30898       /* FALLTHRU */
30899 
30900     case 2:
30901       for (i = 0; i < 16; ++i)
30902 	perm[i] &= 15;
30903       op0 = op1;
30904       one_vec = true;
30905       break;
30906 
30907     case 1:
30908       op1 = op0;
30909       one_vec = true;
30910       break;
30911     }
30912 
30913   /* Look for splat patterns.  */
30914   if (one_vec)
30915     {
30916       elt = perm[0];
30917 
30918       for (i = 0; i < 16; ++i)
30919 	if (perm[i] != elt)
30920 	  break;
30921       if (i == 16)
30922 	{
30923           if (!BYTES_BIG_ENDIAN)
30924             elt = 15 - elt;
30925 	  emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
30926 	  return true;
30927 	}
30928 
30929       if (elt % 2 == 0)
30930 	{
30931 	  for (i = 0; i < 16; i += 2)
30932 	    if (perm[i] != elt || perm[i + 1] != elt + 1)
30933 	      break;
30934 	  if (i == 16)
30935 	    {
30936 	      int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
30937 	      x = gen_reg_rtx (V8HImode);
30938 	      emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
30939 						    GEN_INT (field)));
30940 	      emit_move_insn (target, gen_lowpart (V16QImode, x));
30941 	      return true;
30942 	    }
30943 	}
30944 
30945       if (elt % 4 == 0)
30946 	{
30947 	  for (i = 0; i < 16; i += 4)
30948 	    if (perm[i] != elt
30949 		|| perm[i + 1] != elt + 1
30950 		|| perm[i + 2] != elt + 2
30951 		|| perm[i + 3] != elt + 3)
30952 	      break;
30953 	  if (i == 16)
30954 	    {
30955 	      int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
30956 	      x = gen_reg_rtx (V4SImode);
30957 	      emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
30958 						    GEN_INT (field)));
30959 	      emit_move_insn (target, gen_lowpart (V16QImode, x));
30960 	      return true;
30961 	    }
30962 	}
30963     }
30964 
30965   /* Look for merge and pack patterns.  */
30966   for (j = 0; j < ARRAY_SIZE (patterns); ++j)
30967     {
30968       bool swapped;
30969 
30970       if ((patterns[j].mask & rs6000_isa_flags) == 0)
30971 	continue;
30972 
30973       elt = patterns[j].perm[0];
30974       if (perm[0] == elt)
30975 	swapped = false;
30976       else if (perm[0] == elt + 16)
30977 	swapped = true;
30978       else
30979 	continue;
30980       for (i = 1; i < 16; ++i)
30981 	{
30982 	  elt = patterns[j].perm[i];
30983 	  if (swapped)
30984 	    elt = (elt >= 16 ? elt - 16 : elt + 16);
30985 	  else if (one_vec && elt >= 16)
30986 	    elt -= 16;
30987 	  if (perm[i] != elt)
30988 	    break;
30989 	}
30990       if (i == 16)
30991 	{
30992 	  enum insn_code icode = patterns[j].impl;
30993 	  enum machine_mode omode = insn_data[icode].operand[0].mode;
30994 	  enum machine_mode imode = insn_data[icode].operand[1].mode;
30995 
30996 	  /* For little-endian, don't use vpkuwum and vpkuhum if the
30997 	     underlying vector type is not V4SI and V8HI, respectively.
30998 	     For example, using vpkuwum with a V8HI picks up the even
30999 	     halfwords (BE numbering) when the even halfwords (LE
31000 	     numbering) are what we need.  */
31001 	  if (!BYTES_BIG_ENDIAN
31002 	      && icode == CODE_FOR_altivec_vpkuwum_direct
31003 	      && ((GET_CODE (op0) == REG
31004 		   && GET_MODE (op0) != V4SImode)
31005 		  || (GET_CODE (op0) == SUBREG
31006 		      && GET_MODE (XEXP (op0, 0)) != V4SImode)))
31007 	    continue;
31008 	  if (!BYTES_BIG_ENDIAN
31009 	      && icode == CODE_FOR_altivec_vpkuhum_direct
31010 	      && ((GET_CODE (op0) == REG
31011 		   && GET_MODE (op0) != V8HImode)
31012 		  || (GET_CODE (op0) == SUBREG
31013 		      && GET_MODE (XEXP (op0, 0)) != V8HImode)))
31014 	    continue;
31015 
31016           /* For little-endian, the two input operands must be swapped
31017              (or swapped back) to ensure proper right-to-left numbering
31018              from 0 to 2N-1.  */
31019 	  if (swapped ^ !BYTES_BIG_ENDIAN)
31020 	    x = op0, op0 = op1, op1 = x;
31021 	  if (imode != V16QImode)
31022 	    {
31023 	      op0 = gen_lowpart (imode, op0);
31024 	      op1 = gen_lowpart (imode, op1);
31025 	    }
31026 	  if (omode == V16QImode)
31027 	    x = target;
31028 	  else
31029 	    x = gen_reg_rtx (omode);
31030 	  emit_insn (GEN_FCN (icode) (x, op0, op1));
31031 	  if (omode != V16QImode)
31032 	    emit_move_insn (target, gen_lowpart (V16QImode, x));
31033 	  return true;
31034 	}
31035     }
31036 
31037   if (!BYTES_BIG_ENDIAN)
31038     {
31039       altivec_expand_vec_perm_const_le (operands);
31040       return true;
31041     }
31042 
31043   return false;
31044 }
31045 
31046 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
31047    Return true if we match an efficient implementation.  */
31048 
31049 static bool
rs6000_expand_vec_perm_const_1(rtx target,rtx op0,rtx op1,unsigned char perm0,unsigned char perm1)31050 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
31051 				unsigned char perm0, unsigned char perm1)
31052 {
31053   rtx x;
31054 
31055   /* If both selectors come from the same operand, fold to single op.  */
31056   if ((perm0 & 2) == (perm1 & 2))
31057     {
31058       if (perm0 & 2)
31059 	op0 = op1;
31060       else
31061 	op1 = op0;
31062     }
31063   /* If both operands are equal, fold to simpler permutation.  */
31064   if (rtx_equal_p (op0, op1))
31065     {
31066       perm0 = perm0 & 1;
31067       perm1 = (perm1 & 1) + 2;
31068     }
31069   /* If the first selector comes from the second operand, swap.  */
31070   else if (perm0 & 2)
31071     {
31072       if (perm1 & 2)
31073 	return false;
31074       perm0 -= 2;
31075       perm1 += 2;
31076       x = op0, op0 = op1, op1 = x;
31077     }
31078   /* If the second selector does not come from the second operand, fail.  */
31079   else if ((perm1 & 2) == 0)
31080     return false;
31081 
31082   /* Success! */
31083   if (target != NULL)
31084     {
31085       enum machine_mode vmode, dmode;
31086       rtvec v;
31087 
31088       vmode = GET_MODE (target);
31089       gcc_assert (GET_MODE_NUNITS (vmode) == 2);
31090       dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
31091       x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
31092       v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
31093       x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
31094       emit_insn (gen_rtx_SET (VOIDmode, target, x));
31095     }
31096   return true;
31097 }
31098 
31099 bool
rs6000_expand_vec_perm_const(rtx operands[4])31100 rs6000_expand_vec_perm_const (rtx operands[4])
31101 {
31102   rtx target, op0, op1, sel;
31103   unsigned char perm0, perm1;
31104 
31105   target = operands[0];
31106   op0 = operands[1];
31107   op1 = operands[2];
31108   sel = operands[3];
31109 
31110   /* Unpack the constant selector.  */
31111   perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
31112   perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
31113 
31114   return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
31115 }
31116 
31117 /* Test whether a constant permutation is supported.  */
31118 
31119 static bool
rs6000_vectorize_vec_perm_const_ok(enum machine_mode vmode,const unsigned char * sel)31120 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
31121 				    const unsigned char *sel)
31122 {
31123   /* AltiVec (and thus VSX) can handle arbitrary permutations.  */
31124   if (TARGET_ALTIVEC)
31125     return true;
31126 
31127   /* Check for ps_merge* or evmerge* insns.  */
31128   if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
31129       || (TARGET_SPE && vmode == V2SImode))
31130     {
31131       rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
31132       rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
31133       return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
31134     }
31135 
31136   return false;
31137 }
31138 
31139 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave.  */
31140 
31141 static void
rs6000_do_expand_vec_perm(rtx target,rtx op0,rtx op1,enum machine_mode vmode,unsigned nelt,rtx perm[])31142 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
31143 			   enum machine_mode vmode, unsigned nelt, rtx perm[])
31144 {
31145   enum machine_mode imode;
31146   rtx x;
31147 
31148   imode = vmode;
31149   if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
31150     {
31151       imode = GET_MODE_INNER (vmode);
31152       imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
31153       imode = mode_for_vector (imode, nelt);
31154     }
31155 
31156   x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
31157   x = expand_vec_perm (vmode, op0, op1, x, target);
31158   if (x != target)
31159     emit_move_insn (target, x);
31160 }
31161 
31162 /* Expand an extract even operation.  */
31163 
31164 void
rs6000_expand_extract_even(rtx target,rtx op0,rtx op1)31165 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
31166 {
31167   enum machine_mode vmode = GET_MODE (target);
31168   unsigned i, nelt = GET_MODE_NUNITS (vmode);
31169   rtx perm[16];
31170 
31171   for (i = 0; i < nelt; i++)
31172     perm[i] = GEN_INT (i * 2);
31173 
31174   rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
31175 }
31176 
31177 /* Expand a vector interleave operation.  */
31178 
31179 void
rs6000_expand_interleave(rtx target,rtx op0,rtx op1,bool highp)31180 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
31181 {
31182   enum machine_mode vmode = GET_MODE (target);
31183   unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
31184   rtx perm[16];
31185 
31186   high = (highp ? 0 : nelt / 2);
31187   for (i = 0; i < nelt / 2; i++)
31188     {
31189       perm[i * 2] = GEN_INT (i + high);
31190       perm[i * 2 + 1] = GEN_INT (i + nelt + high);
31191     }
31192 
31193   rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
31194 }
31195 
31196 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT.  */
31197 void
rs6000_scale_v2df(rtx tgt,rtx src,int scale)31198 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
31199 {
31200   HOST_WIDE_INT hwi_scale (scale);
31201   REAL_VALUE_TYPE r_pow;
31202   rtvec v = rtvec_alloc (2);
31203   rtx elt;
31204   rtx scale_vec = gen_reg_rtx (V2DFmode);
31205   (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
31206   elt = CONST_DOUBLE_FROM_REAL_VALUE (r_pow, DFmode);
31207   RTVEC_ELT (v, 0) = elt;
31208   RTVEC_ELT (v, 1) = elt;
31209   rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
31210   emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
31211 }
31212 
31213 /* Return an RTX representing where to find the function value of a
31214    function returning MODE.  */
31215 static rtx
rs6000_complex_function_value(enum machine_mode mode)31216 rs6000_complex_function_value (enum machine_mode mode)
31217 {
31218   unsigned int regno;
31219   rtx r1, r2;
31220   enum machine_mode inner = GET_MODE_INNER (mode);
31221   unsigned int inner_bytes = GET_MODE_SIZE (inner);
31222 
31223   if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
31224     regno = FP_ARG_RETURN;
31225   else
31226     {
31227       regno = GP_ARG_RETURN;
31228 
31229       /* 32-bit is OK since it'll go in r3/r4.  */
31230       if (TARGET_32BIT && inner_bytes >= 4)
31231 	return gen_rtx_REG (mode, regno);
31232     }
31233 
31234   if (inner_bytes >= 8)
31235     return gen_rtx_REG (mode, regno);
31236 
31237   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
31238 			  const0_rtx);
31239   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
31240 			  GEN_INT (inner_bytes));
31241   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
31242 }
31243 
31244 /* Target hook for TARGET_FUNCTION_VALUE.
31245 
31246    On the SPE, both FPs and vectors are returned in r3.
31247 
31248    On RS/6000 an integer value is in r3 and a floating-point value is in
31249    fp1, unless -msoft-float.  */
31250 
31251 static rtx
rs6000_function_value(const_tree valtype,const_tree fn_decl_or_type ATTRIBUTE_UNUSED,bool outgoing ATTRIBUTE_UNUSED)31252 rs6000_function_value (const_tree valtype,
31253 		       const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
31254 		       bool outgoing ATTRIBUTE_UNUSED)
31255 {
31256   enum machine_mode mode;
31257   unsigned int regno;
31258   enum machine_mode elt_mode;
31259   int n_elts;
31260 
31261   /* Special handling for structs in darwin64.  */
31262   if (TARGET_MACHO
31263       && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
31264     {
31265       CUMULATIVE_ARGS valcum;
31266       rtx valret;
31267 
31268       valcum.words = 0;
31269       valcum.fregno = FP_ARG_MIN_REG;
31270       valcum.vregno = ALTIVEC_ARG_MIN_REG;
31271       /* Do a trial code generation as if this were going to be passed as
31272 	 an argument; if any part goes in memory, we return NULL.  */
31273       valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
31274       if (valret)
31275 	return valret;
31276       /* Otherwise fall through to standard ABI rules.  */
31277     }
31278 
31279   /* The ELFv2 ABI returns homogeneous VFP aggregates in registers.  */
31280   if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (valtype), valtype,
31281 					     &elt_mode, &n_elts))
31282     {
31283       int first_reg, n_regs, i;
31284       rtx par;
31285 
31286       if (SCALAR_FLOAT_MODE_P (elt_mode))
31287 	{
31288 	  /* _Decimal128 must use even/odd register pairs.  */
31289 	  first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
31290 	  n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
31291 	}
31292       else
31293 	{
31294 	  first_reg = ALTIVEC_ARG_RETURN;
31295 	  n_regs = 1;
31296 	}
31297 
31298       par = gen_rtx_PARALLEL (TYPE_MODE (valtype), rtvec_alloc (n_elts));
31299       for (i = 0; i < n_elts; i++)
31300 	{
31301 	  rtx r = gen_rtx_REG (elt_mode, first_reg + i * n_regs);
31302 	  rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
31303 	  XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
31304 	}
31305 
31306       return par;
31307     }
31308 
31309   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
31310     {
31311       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
31312       return gen_rtx_PARALLEL (DImode,
31313 	gen_rtvec (2,
31314 		   gen_rtx_EXPR_LIST (VOIDmode,
31315 				      gen_rtx_REG (SImode, GP_ARG_RETURN),
31316 				      const0_rtx),
31317 		   gen_rtx_EXPR_LIST (VOIDmode,
31318 				      gen_rtx_REG (SImode,
31319 						   GP_ARG_RETURN + 1),
31320 				      GEN_INT (4))));
31321     }
31322   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
31323     {
31324       return gen_rtx_PARALLEL (DCmode,
31325 	gen_rtvec (4,
31326 		   gen_rtx_EXPR_LIST (VOIDmode,
31327 				      gen_rtx_REG (SImode, GP_ARG_RETURN),
31328 				      const0_rtx),
31329 		   gen_rtx_EXPR_LIST (VOIDmode,
31330 				      gen_rtx_REG (SImode,
31331 						   GP_ARG_RETURN + 1),
31332 				      GEN_INT (4)),
31333 		   gen_rtx_EXPR_LIST (VOIDmode,
31334 				      gen_rtx_REG (SImode,
31335 						   GP_ARG_RETURN + 2),
31336 				      GEN_INT (8)),
31337 		   gen_rtx_EXPR_LIST (VOIDmode,
31338 				      gen_rtx_REG (SImode,
31339 						   GP_ARG_RETURN + 3),
31340 				      GEN_INT (12))));
31341     }
31342 
31343   mode = TYPE_MODE (valtype);
31344   if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
31345       || POINTER_TYPE_P (valtype))
31346     mode = TARGET_32BIT ? SImode : DImode;
31347 
31348   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
31349     /* _Decimal128 must use an even/odd register pair.  */
31350     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
31351   else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
31352 	   && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
31353     regno = FP_ARG_RETURN;
31354   else if (TREE_CODE (valtype) == COMPLEX_TYPE
31355 	   && targetm.calls.split_complex_arg)
31356     return rs6000_complex_function_value (mode);
31357   /* VSX is a superset of Altivec and adds V2DImode/V2DFmode.  Since the same
31358      return register is used in both cases, and we won't see V2DImode/V2DFmode
31359      for pure altivec, combine the two cases.  */
31360   else if (TREE_CODE (valtype) == VECTOR_TYPE
31361 	   && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
31362 	   && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
31363     regno = ALTIVEC_ARG_RETURN;
31364   else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
31365 	   && (mode == DFmode || mode == DCmode
31366 	       || mode == TFmode || mode == TCmode))
31367     return spe_build_register_parallel (mode, GP_ARG_RETURN);
31368   else
31369     regno = GP_ARG_RETURN;
31370 
31371   return gen_rtx_REG (mode, regno);
31372 }
31373 
31374 /* Define how to find the value returned by a library function
31375    assuming the value has mode MODE.  */
31376 rtx
rs6000_libcall_value(enum machine_mode mode)31377 rs6000_libcall_value (enum machine_mode mode)
31378 {
31379   unsigned int regno;
31380 
31381   if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
31382     {
31383       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
31384       return gen_rtx_PARALLEL (DImode,
31385 	gen_rtvec (2,
31386 		   gen_rtx_EXPR_LIST (VOIDmode,
31387 				      gen_rtx_REG (SImode, GP_ARG_RETURN),
31388 				      const0_rtx),
31389 		   gen_rtx_EXPR_LIST (VOIDmode,
31390 				      gen_rtx_REG (SImode,
31391 						   GP_ARG_RETURN + 1),
31392 				      GEN_INT (4))));
31393     }
31394 
31395   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
31396     /* _Decimal128 must use an even/odd register pair.  */
31397     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
31398   else if (SCALAR_FLOAT_MODE_P (mode)
31399 	   && TARGET_HARD_FLOAT && TARGET_FPRS
31400            && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
31401     regno = FP_ARG_RETURN;
31402   /* VSX is a superset of Altivec and adds V2DImode/V2DFmode.  Since the same
31403      return register is used in both cases, and we won't see V2DImode/V2DFmode
31404      for pure altivec, combine the two cases.  */
31405   else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
31406 	   && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
31407     regno = ALTIVEC_ARG_RETURN;
31408   else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
31409     return rs6000_complex_function_value (mode);
31410   else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
31411 	   && (mode == DFmode || mode == DCmode
31412 	       || mode == TFmode || mode == TCmode))
31413     return spe_build_register_parallel (mode, GP_ARG_RETURN);
31414   else
31415     regno = GP_ARG_RETURN;
31416 
31417   return gen_rtx_REG (mode, regno);
31418 }
31419 
31420 
31421 /* Return true if we use LRA instead of reload pass.  */
31422 static bool
rs6000_lra_p(void)31423 rs6000_lra_p (void)
31424 {
31425   return rs6000_lra_flag;
31426 }
31427 
31428 /* Given FROM and TO register numbers, say whether this elimination is allowed.
31429    Frame pointer elimination is automatically handled.
31430 
31431    For the RS/6000, if frame pointer elimination is being done, we would like
31432    to convert ap into fp, not sp.
31433 
31434    We need r30 if -mminimal-toc was specified, and there are constant pool
31435    references.  */
31436 
31437 static bool
rs6000_can_eliminate(const int from,const int to)31438 rs6000_can_eliminate (const int from, const int to)
31439 {
31440   return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
31441           ? ! frame_pointer_needed
31442           : from == RS6000_PIC_OFFSET_TABLE_REGNUM
31443             ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
31444             : true);
31445 }
31446 
31447 /* Define the offset between two registers, FROM to be eliminated and its
31448    replacement TO, at the start of a routine.  */
31449 HOST_WIDE_INT
rs6000_initial_elimination_offset(int from,int to)31450 rs6000_initial_elimination_offset (int from, int to)
31451 {
31452   rs6000_stack_t *info = rs6000_stack_info ();
31453   HOST_WIDE_INT offset;
31454 
31455   if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31456     offset = info->push_p ? 0 : -info->total_size;
31457   else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31458     {
31459       offset = info->push_p ? 0 : -info->total_size;
31460       if (FRAME_GROWS_DOWNWARD)
31461 	offset += info->fixed_size + info->vars_size + info->parm_size;
31462     }
31463   else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
31464     offset = FRAME_GROWS_DOWNWARD
31465 	     ? info->fixed_size + info->vars_size + info->parm_size
31466 	     : 0;
31467   else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
31468     offset = info->total_size;
31469   else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31470     offset = info->push_p ? info->total_size : 0;
31471   else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
31472     offset = 0;
31473   else
31474     gcc_unreachable ();
31475 
31476   return offset;
31477 }
31478 
31479 static rtx
rs6000_dwarf_register_span(rtx reg)31480 rs6000_dwarf_register_span (rtx reg)
31481 {
31482   rtx parts[8];
31483   int i, words;
31484   unsigned regno = REGNO (reg);
31485   enum machine_mode mode = GET_MODE (reg);
31486 
31487   if (TARGET_SPE
31488       && regno < 32
31489       && (SPE_VECTOR_MODE (GET_MODE (reg))
31490 	  || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
31491 	      && mode != SFmode && mode != SDmode && mode != SCmode)))
31492     ;
31493   else
31494     return NULL_RTX;
31495 
31496   regno = REGNO (reg);
31497 
31498   /* The duality of the SPE register size wreaks all kinds of havoc.
31499      This is a way of distinguishing r0 in 32-bits from r0 in
31500      64-bits.  */
31501   words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
31502   gcc_assert (words <= 4);
31503   for (i = 0; i < words; i++, regno++)
31504     {
31505       if (BYTES_BIG_ENDIAN)
31506 	{
31507 	  parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
31508 	  parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
31509 	}
31510       else
31511 	{
31512 	  parts[2 * i] = gen_rtx_REG (SImode, regno);
31513 	  parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
31514 	}
31515     }
31516 
31517   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
31518 }
31519 
31520 /* Fill in sizes for SPE register high parts in table used by unwinder.  */
31521 
31522 static void
rs6000_init_dwarf_reg_sizes_extra(tree address)31523 rs6000_init_dwarf_reg_sizes_extra (tree address)
31524 {
31525   if (TARGET_SPE)
31526     {
31527       int i;
31528       enum machine_mode mode = TYPE_MODE (char_type_node);
31529       rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
31530       rtx mem = gen_rtx_MEM (BLKmode, addr);
31531       rtx value = gen_int_mode (4, mode);
31532 
31533       for (i = 1201; i < 1232; i++)
31534 	{
31535 	  int column = DWARF_REG_TO_UNWIND_COLUMN (i);
31536 	  HOST_WIDE_INT offset
31537 	    = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
31538 
31539 	  emit_move_insn (adjust_address (mem, mode, offset), value);
31540 	}
31541     }
31542 }
31543 
31544 /* Map internal gcc register numbers to DWARF2 register numbers.  */
31545 
31546 unsigned int
rs6000_dbx_register_number(unsigned int regno)31547 rs6000_dbx_register_number (unsigned int regno)
31548 {
31549   if (regno <= 63 || write_symbols != DWARF2_DEBUG)
31550     return regno;
31551   if (regno == LR_REGNO)
31552     return 108;
31553   if (regno == CTR_REGNO)
31554     return 109;
31555   if (CR_REGNO_P (regno))
31556     return regno - CR0_REGNO + 86;
31557   if (regno == CA_REGNO)
31558     return 101;  /* XER */
31559   if (ALTIVEC_REGNO_P (regno))
31560     return regno - FIRST_ALTIVEC_REGNO + 1124;
31561   if (regno == VRSAVE_REGNO)
31562     return 356;
31563   if (regno == VSCR_REGNO)
31564     return 67;
31565   if (regno == SPE_ACC_REGNO)
31566     return 99;
31567   if (regno == SPEFSCR_REGNO)
31568     return 612;
31569   /* SPE high reg number.  We get these values of regno from
31570      rs6000_dwarf_register_span.  */
31571   gcc_assert (regno >= 1200 && regno < 1232);
31572   return regno;
31573 }
31574 
31575 /* target hook eh_return_filter_mode */
31576 static enum machine_mode
rs6000_eh_return_filter_mode(void)31577 rs6000_eh_return_filter_mode (void)
31578 {
31579   return TARGET_32BIT ? SImode : word_mode;
31580 }
31581 
31582 /* Target hook for scalar_mode_supported_p.  */
31583 static bool
rs6000_scalar_mode_supported_p(enum machine_mode mode)31584 rs6000_scalar_mode_supported_p (enum machine_mode mode)
31585 {
31586   if (DECIMAL_FLOAT_MODE_P (mode))
31587     return default_decimal_float_supported_p ();
31588   else
31589     return default_scalar_mode_supported_p (mode);
31590 }
31591 
31592 /* Target hook for vector_mode_supported_p.  */
31593 static bool
rs6000_vector_mode_supported_p(enum machine_mode mode)31594 rs6000_vector_mode_supported_p (enum machine_mode mode)
31595 {
31596 
31597   if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
31598     return true;
31599 
31600   if (TARGET_SPE && SPE_VECTOR_MODE (mode))
31601     return true;
31602 
31603   else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
31604     return true;
31605 
31606   else
31607     return false;
31608 }
31609 
31610 /* Target hook for invalid_arg_for_unprototyped_fn. */
31611 static const char *
invalid_arg_for_unprototyped_fn(const_tree typelist,const_tree funcdecl,const_tree val)31612 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
31613 {
31614   return (!rs6000_darwin64_abi
31615 	  && typelist == 0
31616           && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
31617           && (funcdecl == NULL_TREE
31618               || (TREE_CODE (funcdecl) == FUNCTION_DECL
31619                   && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
31620 	  ? N_("AltiVec argument passed to unprototyped function")
31621 	  : NULL;
31622 }
31623 
31624 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
31625    setup by using __stack_chk_fail_local hidden function instead of
31626    calling __stack_chk_fail directly.  Otherwise it is better to call
31627    __stack_chk_fail directly.  */
31628 
31629 static tree ATTRIBUTE_UNUSED
rs6000_stack_protect_fail(void)31630 rs6000_stack_protect_fail (void)
31631 {
31632   return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
31633 	 ? default_hidden_stack_protect_fail ()
31634 	 : default_external_stack_protect_fail ();
31635 }
31636 
31637 void
rs6000_final_prescan_insn(rtx insn,rtx * operand ATTRIBUTE_UNUSED,int num_operands ATTRIBUTE_UNUSED)31638 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
31639 			   int num_operands ATTRIBUTE_UNUSED)
31640 {
31641   if (rs6000_warn_cell_microcode)
31642     {
31643       const char *temp;
31644       int insn_code_number = recog_memoized (insn);
31645       location_t location = INSN_LOCATION (insn);
31646 
31647       /* Punt on insns we cannot recognize.  */
31648       if (insn_code_number < 0)
31649 	return;
31650 
31651       temp = get_insn_template (insn_code_number, insn);
31652 
31653       if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
31654 	warning_at (location, OPT_mwarn_cell_microcode,
31655 		    "emitting microcode insn %s\t[%s] #%d",
31656 		    temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31657       else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
31658 	warning_at (location, OPT_mwarn_cell_microcode,
31659 		    "emitting conditional microcode insn %s\t[%s] #%d",
31660 		    temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31661     }
31662 }
31663 
31664 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook.  */
31665 
31666 #if TARGET_ELF
31667 static unsigned HOST_WIDE_INT
rs6000_asan_shadow_offset(void)31668 rs6000_asan_shadow_offset (void)
31669 {
31670   return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
31671 }
31672 #endif
31673 
31674 /* Mask options that we want to support inside of attribute((target)) and
31675    #pragma GCC target operations.  Note, we do not include things like
31676    64/32-bit, endianess, hard/soft floating point, etc. that would have
31677    different calling sequences.  */
31678 
31679 struct rs6000_opt_mask {
31680   const char *name;		/* option name */
31681   HOST_WIDE_INT mask;		/* mask to set */
31682   bool invert;			/* invert sense of mask */
31683   bool valid_target;		/* option is a target option */
31684 };
31685 
31686 static struct rs6000_opt_mask const rs6000_opt_masks[] =
31687 {
31688   { "altivec",			OPTION_MASK_ALTIVEC,		false, true  },
31689   { "cmpb",			OPTION_MASK_CMPB,		false, true  },
31690   { "crypto",			OPTION_MASK_CRYPTO,		false, true  },
31691   { "direct-move",		OPTION_MASK_DIRECT_MOVE,	false, true  },
31692   { "dlmzb",			OPTION_MASK_DLMZB,		false, true  },
31693   { "fprnd",			OPTION_MASK_FPRND,		false, true  },
31694   { "hard-dfp",			OPTION_MASK_DFP,		false, true  },
31695   { "htm",			OPTION_MASK_HTM,		false, true  },
31696   { "isel",			OPTION_MASK_ISEL,		false, true  },
31697   { "mfcrf",			OPTION_MASK_MFCRF,		false, true  },
31698   { "mfpgpr",			OPTION_MASK_MFPGPR,		false, true  },
31699   { "mulhw",			OPTION_MASK_MULHW,		false, true  },
31700   { "multiple",			OPTION_MASK_MULTIPLE,		false, true  },
31701   { "popcntb",			OPTION_MASK_POPCNTB,		false, true  },
31702   { "popcntd",			OPTION_MASK_POPCNTD,		false, true  },
31703   { "power8-fusion",		OPTION_MASK_P8_FUSION,		false, true  },
31704   { "power8-fusion-sign",	OPTION_MASK_P8_FUSION_SIGN,	false, true  },
31705   { "power8-vector",		OPTION_MASK_P8_VECTOR,		false, true  },
31706   { "powerpc-gfxopt",		OPTION_MASK_PPC_GFXOPT,		false, true  },
31707   { "powerpc-gpopt",		OPTION_MASK_PPC_GPOPT,		false, true  },
31708   { "quad-memory",		OPTION_MASK_QUAD_MEMORY,	false, true  },
31709   { "quad-memory-atomic",	OPTION_MASK_QUAD_MEMORY_ATOMIC,	false, true  },
31710   { "recip-precision",		OPTION_MASK_RECIP_PRECISION,	false, true  },
31711   { "save-toc-indirect",	OPTION_MASK_SAVE_TOC_INDIRECT,	false, true  },
31712   { "string",			OPTION_MASK_STRING,		false, true  },
31713   { "update",			OPTION_MASK_NO_UPDATE,		true , true  },
31714   { "upper-regs-df",		OPTION_MASK_UPPER_REGS_DF,	false, true  },
31715   { "upper-regs-sf",		OPTION_MASK_UPPER_REGS_SF,	false, true  },
31716   { "vsx",			OPTION_MASK_VSX,		false, true  },
31717   { "vsx-timode",		OPTION_MASK_VSX_TIMODE,		false, true  },
31718 #ifdef OPTION_MASK_64BIT
31719 #if TARGET_AIX_OS
31720   { "aix64",			OPTION_MASK_64BIT,		false, false },
31721   { "aix32",			OPTION_MASK_64BIT,		true,  false },
31722 #else
31723   { "64",			OPTION_MASK_64BIT,		false, false },
31724   { "32",			OPTION_MASK_64BIT,		true,  false },
31725 #endif
31726 #endif
31727 #ifdef OPTION_MASK_EABI
31728   { "eabi",			OPTION_MASK_EABI,		false, false },
31729 #endif
31730 #ifdef OPTION_MASK_LITTLE_ENDIAN
31731   { "little",			OPTION_MASK_LITTLE_ENDIAN,	false, false },
31732   { "big",			OPTION_MASK_LITTLE_ENDIAN,	true,  false },
31733 #endif
31734 #ifdef OPTION_MASK_RELOCATABLE
31735   { "relocatable",		OPTION_MASK_RELOCATABLE,	false, false },
31736 #endif
31737 #ifdef OPTION_MASK_STRICT_ALIGN
31738   { "strict-align",		OPTION_MASK_STRICT_ALIGN,	false, false },
31739 #endif
31740   { "soft-float",		OPTION_MASK_SOFT_FLOAT,		false, false },
31741   { "string",			OPTION_MASK_STRING,		false, false },
31742 };
31743 
31744 /* Builtin mask mapping for printing the flags.  */
31745 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
31746 {
31747   { "altivec",		 RS6000_BTM_ALTIVEC,	false, false },
31748   { "vsx",		 RS6000_BTM_VSX,	false, false },
31749   { "spe",		 RS6000_BTM_SPE,	false, false },
31750   { "paired",		 RS6000_BTM_PAIRED,	false, false },
31751   { "fre",		 RS6000_BTM_FRE,	false, false },
31752   { "fres",		 RS6000_BTM_FRES,	false, false },
31753   { "frsqrte",		 RS6000_BTM_FRSQRTE,	false, false },
31754   { "frsqrtes",		 RS6000_BTM_FRSQRTES,	false, false },
31755   { "popcntd",		 RS6000_BTM_POPCNTD,	false, false },
31756   { "cell",		 RS6000_BTM_CELL,	false, false },
31757   { "power8-vector",	 RS6000_BTM_P8_VECTOR,	false, false },
31758   { "crypto",		 RS6000_BTM_CRYPTO,	false, false },
31759   { "htm",		 RS6000_BTM_HTM,	false, false },
31760   { "hard-dfp",		 RS6000_BTM_DFP,	false, false },
31761   { "hard-float",	 RS6000_BTM_HARD_FLOAT,	false, false },
31762   { "long-double-128",	 RS6000_BTM_LDBL128,	false, false },
31763 };
31764 
31765 /* Option variables that we want to support inside attribute((target)) and
31766    #pragma GCC target operations.  */
31767 
31768 struct rs6000_opt_var {
31769   const char *name;		/* option name */
31770   size_t global_offset;		/* offset of the option in global_options.  */
31771   size_t target_offset;		/* offset of the option in target optiosn.  */
31772 };
31773 
31774 static struct rs6000_opt_var const rs6000_opt_vars[] =
31775 {
31776   { "friz",
31777     offsetof (struct gcc_options, x_TARGET_FRIZ),
31778     offsetof (struct cl_target_option, x_TARGET_FRIZ), },
31779   { "avoid-indexed-addresses",
31780     offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
31781     offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
31782   { "paired",
31783     offsetof (struct gcc_options, x_rs6000_paired_float),
31784     offsetof (struct cl_target_option, x_rs6000_paired_float), },
31785   { "longcall",
31786     offsetof (struct gcc_options, x_rs6000_default_long_calls),
31787     offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
31788   { "optimize-swaps",
31789     offsetof (struct gcc_options, x_rs6000_optimize_swaps),
31790     offsetof (struct cl_target_option, x_rs6000_optimize_swaps), },
31791   { "allow-movmisalign",
31792     offsetof (struct gcc_options, x_TARGET_ALLOW_MOVMISALIGN),
31793     offsetof (struct cl_target_option, x_TARGET_ALLOW_MOVMISALIGN), },
31794   { "allow-df-permute",
31795     offsetof (struct gcc_options, x_TARGET_ALLOW_DF_PERMUTE),
31796     offsetof (struct cl_target_option, x_TARGET_ALLOW_DF_PERMUTE), },
31797   { "sched-groups",
31798     offsetof (struct gcc_options, x_TARGET_SCHED_GROUPS),
31799     offsetof (struct cl_target_option, x_TARGET_SCHED_GROUPS), },
31800   { "always-hint",
31801     offsetof (struct gcc_options, x_TARGET_ALWAYS_HINT),
31802     offsetof (struct cl_target_option, x_TARGET_ALWAYS_HINT), },
31803   { "align-branch-targets",
31804     offsetof (struct gcc_options, x_TARGET_ALIGN_BRANCH_TARGETS),
31805     offsetof (struct cl_target_option, x_TARGET_ALIGN_BRANCH_TARGETS), },
31806   { "vectorize-builtins",
31807     offsetof (struct gcc_options, x_TARGET_VECTORIZE_BUILTINS),
31808     offsetof (struct cl_target_option, x_TARGET_VECTORIZE_BUILTINS), },
31809   { "tls-markers",
31810     offsetof (struct gcc_options, x_tls_markers),
31811     offsetof (struct cl_target_option, x_tls_markers), },
31812   { "sched-prolog",
31813     offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
31814     offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
31815   { "sched-epilog",
31816     offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
31817     offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
31818   { "gen-cell-microcode",
31819     offsetof (struct gcc_options, x_rs6000_gen_cell_microcode),
31820     offsetof (struct cl_target_option, x_rs6000_gen_cell_microcode), },
31821   { "warn-cell-microcode",
31822     offsetof (struct gcc_options, x_rs6000_warn_cell_microcode),
31823     offsetof (struct cl_target_option, x_rs6000_warn_cell_microcode), },
31824 };
31825 
31826 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
31827    parsing.  Return true if there were no errors.  */
31828 
31829 static bool
rs6000_inner_target_options(tree args,bool attr_p)31830 rs6000_inner_target_options (tree args, bool attr_p)
31831 {
31832   bool ret = true;
31833 
31834   if (args == NULL_TREE)
31835     ;
31836 
31837   else if (TREE_CODE (args) == STRING_CST)
31838     {
31839       char *p = ASTRDUP (TREE_STRING_POINTER (args));
31840       char *q;
31841 
31842       while ((q = strtok (p, ",")) != NULL)
31843 	{
31844 	  bool error_p = false;
31845 	  bool not_valid_p = false;
31846 	  const char *cpu_opt = NULL;
31847 
31848 	  p = NULL;
31849 	  if (strncmp (q, "cpu=", 4) == 0)
31850 	    {
31851 	      int cpu_index = rs6000_cpu_name_lookup (q+4);
31852 	      if (cpu_index >= 0)
31853 		rs6000_cpu_index = cpu_index;
31854 	      else
31855 		{
31856 		  error_p = true;
31857 		  cpu_opt = q+4;
31858 		}
31859 	    }
31860 	  else if (strncmp (q, "tune=", 5) == 0)
31861 	    {
31862 	      int tune_index = rs6000_cpu_name_lookup (q+5);
31863 	      if (tune_index >= 0)
31864 		rs6000_tune_index = tune_index;
31865 	      else
31866 		{
31867 		  error_p = true;
31868 		  cpu_opt = q+5;
31869 		}
31870 	    }
31871 	  else
31872 	    {
31873 	      size_t i;
31874 	      bool invert = false;
31875 	      char *r = q;
31876 
31877 	      error_p = true;
31878 	      if (strncmp (r, "no-", 3) == 0)
31879 		{
31880 		  invert = true;
31881 		  r += 3;
31882 		}
31883 
31884 	      for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
31885 		if (strcmp (r, rs6000_opt_masks[i].name) == 0)
31886 		  {
31887 		    HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
31888 
31889 		    if (!rs6000_opt_masks[i].valid_target)
31890 		      not_valid_p = true;
31891 		    else
31892 		      {
31893 			error_p = false;
31894 			rs6000_isa_flags_explicit |= mask;
31895 
31896 			/* VSX needs altivec, so -mvsx automagically sets
31897 			   altivec and disables -mavoid-indexed-addresses.  */
31898 			if (!invert)
31899 			  {
31900 			    if (mask == OPTION_MASK_VSX)
31901 			      {
31902 				mask |= OPTION_MASK_ALTIVEC;
31903 				TARGET_AVOID_XFORM = 0;
31904 			      }
31905 			  }
31906 
31907 			if (rs6000_opt_masks[i].invert)
31908 			  invert = !invert;
31909 
31910 			if (invert)
31911 			  rs6000_isa_flags &= ~mask;
31912 			else
31913 			  rs6000_isa_flags |= mask;
31914 		      }
31915 		    break;
31916 		  }
31917 
31918 	      if (error_p && !not_valid_p)
31919 		{
31920 		  for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
31921 		    if (strcmp (r, rs6000_opt_vars[i].name) == 0)
31922 		      {
31923 			size_t j = rs6000_opt_vars[i].global_offset;
31924 			*((int *) ((char *)&global_options + j)) = !invert;
31925 			error_p = false;
31926 			not_valid_p = false;
31927 			break;
31928 		      }
31929 		}
31930 	    }
31931 
31932 	  if (error_p)
31933 	    {
31934 	      const char *eprefix, *esuffix;
31935 
31936 	      ret = false;
31937 	      if (attr_p)
31938 		{
31939 		  eprefix = "__attribute__((__target__(";
31940 		  esuffix = ")))";
31941 		}
31942 	      else
31943 		{
31944 		  eprefix = "#pragma GCC target ";
31945 		  esuffix = "";
31946 		}
31947 
31948 	      if (cpu_opt)
31949 		error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
31950 		       q, esuffix);
31951 	      else if (not_valid_p)
31952 		error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
31953 	      else
31954 		error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
31955 	    }
31956 	}
31957     }
31958 
31959   else if (TREE_CODE (args) == TREE_LIST)
31960     {
31961       do
31962 	{
31963 	  tree value = TREE_VALUE (args);
31964 	  if (value)
31965 	    {
31966 	      bool ret2 = rs6000_inner_target_options (value, attr_p);
31967 	      if (!ret2)
31968 		ret = false;
31969 	    }
31970 	  args = TREE_CHAIN (args);
31971 	}
31972       while (args != NULL_TREE);
31973     }
31974 
31975   else
31976     gcc_unreachable ();
31977 
31978   return ret;
31979 }
31980 
31981 /* Print out the target options as a list for -mdebug=target.  */
31982 
31983 static void
rs6000_debug_target_options(tree args,const char * prefix)31984 rs6000_debug_target_options (tree args, const char *prefix)
31985 {
31986   if (args == NULL_TREE)
31987     fprintf (stderr, "%s<NULL>", prefix);
31988 
31989   else if (TREE_CODE (args) == STRING_CST)
31990     {
31991       char *p = ASTRDUP (TREE_STRING_POINTER (args));
31992       char *q;
31993 
31994       while ((q = strtok (p, ",")) != NULL)
31995 	{
31996 	  p = NULL;
31997 	  fprintf (stderr, "%s\"%s\"", prefix, q);
31998 	  prefix = ", ";
31999 	}
32000     }
32001 
32002   else if (TREE_CODE (args) == TREE_LIST)
32003     {
32004       do
32005 	{
32006 	  tree value = TREE_VALUE (args);
32007 	  if (value)
32008 	    {
32009 	      rs6000_debug_target_options (value, prefix);
32010 	      prefix = ", ";
32011 	    }
32012 	  args = TREE_CHAIN (args);
32013 	}
32014       while (args != NULL_TREE);
32015     }
32016 
32017   else
32018     gcc_unreachable ();
32019 
32020   return;
32021 }
32022 
32023 
32024 /* Hook to validate attribute((target("..."))).  */
32025 
32026 static bool
rs6000_valid_attribute_p(tree fndecl,tree ARG_UNUSED (name),tree args,int flags)32027 rs6000_valid_attribute_p (tree fndecl,
32028 			  tree ARG_UNUSED (name),
32029 			  tree args,
32030 			  int flags)
32031 {
32032   struct cl_target_option cur_target;
32033   bool ret;
32034   tree old_optimize = build_optimization_node ();
32035   tree new_target, new_optimize;
32036   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
32037 
32038   gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
32039 
32040   if (TARGET_DEBUG_TARGET)
32041     {
32042       tree tname = DECL_NAME (fndecl);
32043       fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
32044       if (tname)
32045 	fprintf (stderr, "function: %.*s\n",
32046 		 (int) IDENTIFIER_LENGTH (tname),
32047 		 IDENTIFIER_POINTER (tname));
32048       else
32049 	fprintf (stderr, "function: unknown\n");
32050 
32051       fprintf (stderr, "args:");
32052       rs6000_debug_target_options (args, " ");
32053       fprintf (stderr, "\n");
32054 
32055       if (flags)
32056 	fprintf (stderr, "flags: 0x%x\n", flags);
32057 
32058       fprintf (stderr, "--------------------\n");
32059     }
32060 
32061   old_optimize = build_optimization_node ();
32062   func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
32063 
32064   /* If the function changed the optimization levels as well as setting target
32065      options, start with the optimizations specified.  */
32066   if (func_optimize && func_optimize != old_optimize)
32067     cl_optimization_restore (&global_options,
32068 			     TREE_OPTIMIZATION (func_optimize));
32069 
32070   /* The target attributes may also change some optimization flags, so update
32071      the optimization options if necessary.  */
32072   cl_target_option_save (&cur_target, &global_options);
32073   rs6000_cpu_index = rs6000_tune_index = -1;
32074   ret = rs6000_inner_target_options (args, true);
32075 
32076   /* Set up any additional state.  */
32077   if (ret)
32078     {
32079       ret = rs6000_option_override_internal (false);
32080       new_target = build_target_option_node ();
32081     }
32082   else
32083     new_target = NULL;
32084 
32085   new_optimize = build_optimization_node ();
32086 
32087   if (!new_target)
32088     ret = false;
32089 
32090   else if (fndecl)
32091     {
32092       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
32093 
32094       if (old_optimize != new_optimize)
32095 	DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
32096     }
32097 
32098   cl_target_option_restore (&global_options, &cur_target);
32099 
32100   if (old_optimize != new_optimize)
32101     cl_optimization_restore (&global_options,
32102 			     TREE_OPTIMIZATION (old_optimize));
32103 
32104   return ret;
32105 }
32106 
32107 
32108 /* Hook to validate the current #pragma GCC target and set the state, and
32109    update the macros based on what was changed.  If ARGS is NULL, then
32110    POP_TARGET is used to reset the options.  */
32111 
32112 bool
rs6000_pragma_target_parse(tree args,tree pop_target)32113 rs6000_pragma_target_parse (tree args, tree pop_target)
32114 {
32115   tree prev_tree = build_target_option_node ();
32116   tree cur_tree;
32117   struct cl_target_option *prev_opt, *cur_opt;
32118   HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
32119   HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
32120 
32121   if (TARGET_DEBUG_TARGET)
32122     {
32123       fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
32124       fprintf (stderr, "args:");
32125       rs6000_debug_target_options (args, " ");
32126       fprintf (stderr, "\n");
32127 
32128       if (pop_target)
32129 	{
32130 	  fprintf (stderr, "pop_target:\n");
32131 	  debug_tree (pop_target);
32132 	}
32133       else
32134 	fprintf (stderr, "pop_target: <NULL>\n");
32135 
32136       fprintf (stderr, "--------------------\n");
32137     }
32138 
32139   if (! args)
32140     {
32141       cur_tree = ((pop_target)
32142 		  ? pop_target
32143 		  : target_option_default_node);
32144       cl_target_option_restore (&global_options,
32145 				TREE_TARGET_OPTION (cur_tree));
32146     }
32147   else
32148     {
32149       rs6000_cpu_index = rs6000_tune_index = -1;
32150       if (!rs6000_inner_target_options (args, false)
32151 	  || !rs6000_option_override_internal (false)
32152 	  || (cur_tree = build_target_option_node ()) == NULL_TREE)
32153 	{
32154 	  if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
32155 	    fprintf (stderr, "invalid pragma\n");
32156 
32157 	  return false;
32158 	}
32159     }
32160 
32161   target_option_current_node = cur_tree;
32162 
32163   /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
32164      change the macros that are defined.  */
32165   if (rs6000_target_modify_macros_ptr)
32166     {
32167       prev_opt    = TREE_TARGET_OPTION (prev_tree);
32168       prev_bumask = prev_opt->x_rs6000_builtin_mask;
32169       prev_flags  = prev_opt->x_rs6000_isa_flags;
32170 
32171       cur_opt     = TREE_TARGET_OPTION (cur_tree);
32172       cur_flags   = cur_opt->x_rs6000_isa_flags;
32173       cur_bumask  = cur_opt->x_rs6000_builtin_mask;
32174 
32175       diff_bumask = (prev_bumask ^ cur_bumask);
32176       diff_flags  = (prev_flags ^ cur_flags);
32177 
32178       if ((diff_flags != 0) || (diff_bumask != 0))
32179 	{
32180 	  /* Delete old macros.  */
32181 	  rs6000_target_modify_macros_ptr (false,
32182 					   prev_flags & diff_flags,
32183 					   prev_bumask & diff_bumask);
32184 
32185 	  /* Define new macros.  */
32186 	  rs6000_target_modify_macros_ptr (true,
32187 					   cur_flags & diff_flags,
32188 					   cur_bumask & diff_bumask);
32189 	}
32190     }
32191 
32192   return true;
32193 }
32194 
32195 
32196 /* Remember the last target of rs6000_set_current_function.  */
32197 static GTY(()) tree rs6000_previous_fndecl;
32198 
32199 /* Establish appropriate back-end context for processing the function
32200    FNDECL.  The argument might be NULL to indicate processing at top
32201    level, outside of any function scope.  */
32202 static void
rs6000_set_current_function(tree fndecl)32203 rs6000_set_current_function (tree fndecl)
32204 {
32205   tree old_tree = (rs6000_previous_fndecl
32206 		   ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
32207 		   : NULL_TREE);
32208 
32209   tree new_tree = (fndecl
32210 		   ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
32211 		   : NULL_TREE);
32212 
32213   if (TARGET_DEBUG_TARGET)
32214     {
32215       bool print_final = false;
32216       fprintf (stderr, "\n==================== rs6000_set_current_function");
32217 
32218       if (fndecl)
32219 	fprintf (stderr, ", fndecl %s (%p)",
32220 		 (DECL_NAME (fndecl)
32221 		  ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
32222 		  : "<unknown>"), (void *)fndecl);
32223 
32224       if (rs6000_previous_fndecl)
32225 	fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
32226 
32227       fprintf (stderr, "\n");
32228       if (new_tree)
32229 	{
32230 	  fprintf (stderr, "\nnew fndecl target specific options:\n");
32231 	  debug_tree (new_tree);
32232 	  print_final = true;
32233 	}
32234 
32235       if (old_tree)
32236 	{
32237 	  fprintf (stderr, "\nold fndecl target specific options:\n");
32238 	  debug_tree (old_tree);
32239 	  print_final = true;
32240 	}
32241 
32242       if (print_final)
32243 	fprintf (stderr, "--------------------\n");
32244     }
32245 
32246   /* Only change the context if the function changes.  This hook is called
32247      several times in the course of compiling a function, and we don't want to
32248      slow things down too much or call target_reinit when it isn't safe.  */
32249   if (fndecl && fndecl != rs6000_previous_fndecl)
32250     {
32251       rs6000_previous_fndecl = fndecl;
32252       if (old_tree == new_tree)
32253 	;
32254 
32255       else if (new_tree)
32256 	{
32257 	  cl_target_option_restore (&global_options,
32258 				    TREE_TARGET_OPTION (new_tree));
32259 	  target_reinit ();
32260 	}
32261 
32262       else if (old_tree)
32263 	{
32264 	  struct cl_target_option *def
32265 	    = TREE_TARGET_OPTION (target_option_current_node);
32266 
32267 	  cl_target_option_restore (&global_options, def);
32268 	  target_reinit ();
32269 	}
32270     }
32271 }
32272 
32273 
32274 /* Save the current options */
32275 
32276 static void
rs6000_function_specific_save(struct cl_target_option * ptr)32277 rs6000_function_specific_save (struct cl_target_option *ptr)
32278 {
32279   ptr->x_rs6000_isa_flags = rs6000_isa_flags;
32280   ptr->x_rs6000_isa_flags_explicit = rs6000_isa_flags_explicit;
32281 }
32282 
32283 /* Restore the current options */
32284 
32285 static void
rs6000_function_specific_restore(struct cl_target_option * ptr)32286 rs6000_function_specific_restore (struct cl_target_option *ptr)
32287 {
32288   rs6000_isa_flags = ptr->x_rs6000_isa_flags;
32289   rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
32290   (void) rs6000_option_override_internal (false);
32291 }
32292 
32293 /* Print the current options */
32294 
32295 static void
rs6000_function_specific_print(FILE * file,int indent,struct cl_target_option * ptr)32296 rs6000_function_specific_print (FILE *file, int indent,
32297 				struct cl_target_option *ptr)
32298 {
32299   rs6000_print_isa_options (file, indent, "Isa options set",
32300 			    ptr->x_rs6000_isa_flags);
32301 
32302   rs6000_print_isa_options (file, indent, "Isa options explicit",
32303 			    ptr->x_rs6000_isa_flags_explicit);
32304 }
32305 
32306 /* Helper function to print the current isa or misc options on a line.  */
32307 
32308 static void
rs6000_print_options_internal(FILE * file,int indent,const char * string,HOST_WIDE_INT flags,const char * prefix,const struct rs6000_opt_mask * opts,size_t num_elements)32309 rs6000_print_options_internal (FILE *file,
32310 			       int indent,
32311 			       const char *string,
32312 			       HOST_WIDE_INT flags,
32313 			       const char *prefix,
32314 			       const struct rs6000_opt_mask *opts,
32315 			       size_t num_elements)
32316 {
32317   size_t i;
32318   size_t start_column = 0;
32319   size_t cur_column;
32320   size_t max_column = 76;
32321   const char *comma = "";
32322 
32323   if (indent)
32324     start_column += fprintf (file, "%*s", indent, "");
32325 
32326   if (!flags)
32327     {
32328       fprintf (stderr, DEBUG_FMT_S, string, "<none>");
32329       return;
32330     }
32331 
32332   start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
32333 
32334   /* Print the various mask options.  */
32335   cur_column = start_column;
32336   for (i = 0; i < num_elements; i++)
32337     {
32338       if ((flags & opts[i].mask) != 0)
32339 	{
32340 	  const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
32341 	  size_t len = (strlen (comma)
32342 			+ strlen (prefix)
32343 			+ strlen (no_str)
32344 			+ strlen (rs6000_opt_masks[i].name));
32345 
32346 	  cur_column += len;
32347 	  if (cur_column > max_column)
32348 	    {
32349 	      fprintf (stderr, ", \\\n%*s", (int)start_column, "");
32350 	      cur_column = start_column + len;
32351 	      comma = "";
32352 	    }
32353 
32354 	  fprintf (file, "%s%s%s%s", comma, prefix, no_str,
32355 		   rs6000_opt_masks[i].name);
32356 	  flags &= ~ opts[i].mask;
32357 	  comma = ", ";
32358 	}
32359     }
32360 
32361   fputs ("\n", file);
32362 }
32363 
32364 /* Helper function to print the current isa options on a line.  */
32365 
32366 static void
rs6000_print_isa_options(FILE * file,int indent,const char * string,HOST_WIDE_INT flags)32367 rs6000_print_isa_options (FILE *file, int indent, const char *string,
32368 			  HOST_WIDE_INT flags)
32369 {
32370   rs6000_print_options_internal (file, indent, string, flags, "-m",
32371 				 &rs6000_opt_masks[0],
32372 				 ARRAY_SIZE (rs6000_opt_masks));
32373 }
32374 
32375 static void
rs6000_print_builtin_options(FILE * file,int indent,const char * string,HOST_WIDE_INT flags)32376 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
32377 			      HOST_WIDE_INT flags)
32378 {
32379   rs6000_print_options_internal (file, indent, string, flags, "",
32380 				 &rs6000_builtin_mask_names[0],
32381 				 ARRAY_SIZE (rs6000_builtin_mask_names));
32382 }
32383 
32384 
32385 /* Hook to determine if one function can safely inline another.  */
32386 
32387 static bool
rs6000_can_inline_p(tree caller,tree callee)32388 rs6000_can_inline_p (tree caller, tree callee)
32389 {
32390   bool ret = false;
32391   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
32392   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
32393 
32394   /* If callee has no option attributes, then it is ok to inline.  */
32395   if (!callee_tree)
32396     ret = true;
32397 
32398   /* If caller has no option attributes, but callee does then it is not ok to
32399      inline.  */
32400   else if (!caller_tree)
32401     ret = false;
32402 
32403   else
32404     {
32405       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
32406       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
32407 
32408       /* Callee's options should a subset of the caller's, i.e. a vsx function
32409 	 can inline an altivec function but a non-vsx function can't inline a
32410 	 vsx function.  */
32411       if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
32412 	  == callee_opts->x_rs6000_isa_flags)
32413 	ret = true;
32414     }
32415 
32416   if (TARGET_DEBUG_TARGET)
32417     fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
32418 	     (DECL_NAME (caller)
32419 	      ? IDENTIFIER_POINTER (DECL_NAME (caller))
32420 	      : "<unknown>"),
32421 	     (DECL_NAME (callee)
32422 	      ? IDENTIFIER_POINTER (DECL_NAME (callee))
32423 	      : "<unknown>"),
32424 	     (ret ? "can" : "cannot"));
32425 
32426   return ret;
32427 }
32428 
32429 /* Allocate a stack temp and fixup the address so it meets the particular
32430    memory requirements (either offetable or REG+REG addressing).  */
32431 
32432 rtx
rs6000_allocate_stack_temp(enum machine_mode mode,bool offsettable_p,bool reg_reg_p)32433 rs6000_allocate_stack_temp (enum machine_mode mode,
32434 			    bool offsettable_p,
32435 			    bool reg_reg_p)
32436 {
32437   rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
32438   rtx addr = XEXP (stack, 0);
32439   int strict_p = (reload_in_progress || reload_completed);
32440 
32441   if (!legitimate_indirect_address_p (addr, strict_p))
32442     {
32443       if (offsettable_p
32444 	  && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
32445 	stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
32446 
32447       else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
32448 	stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
32449     }
32450 
32451   return stack;
32452 }
32453 
32454 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
32455    to such a form to deal with memory reference instructions like STFIWX that
32456    only take reg+reg addressing.  */
32457 
32458 rtx
rs6000_address_for_fpconvert(rtx x)32459 rs6000_address_for_fpconvert (rtx x)
32460 {
32461   int strict_p = (reload_in_progress || reload_completed);
32462   rtx addr;
32463 
32464   gcc_assert (MEM_P (x));
32465   addr = XEXP (x, 0);
32466   if (! legitimate_indirect_address_p (addr, strict_p)
32467       && ! legitimate_indexed_address_p (addr, strict_p))
32468     {
32469       if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
32470 	{
32471 	  rtx reg = XEXP (addr, 0);
32472 	  HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
32473 	  rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
32474 	  gcc_assert (REG_P (reg));
32475 	  emit_insn (gen_add3_insn (reg, reg, size_rtx));
32476 	  addr = reg;
32477 	}
32478       else if (GET_CODE (addr) == PRE_MODIFY)
32479 	{
32480 	  rtx reg = XEXP (addr, 0);
32481 	  rtx expr = XEXP (addr, 1);
32482 	  gcc_assert (REG_P (reg));
32483 	  gcc_assert (GET_CODE (expr) == PLUS);
32484 	  emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
32485 	  addr = reg;
32486 	}
32487 
32488       x = replace_equiv_address (x, copy_addr_to_reg (addr));
32489     }
32490 
32491   return x;
32492 }
32493 
32494 /* Given a memory reference, if it is not in the form for altivec memory
32495    reference instructions (i.e. reg or reg+reg addressing with AND of -16),
32496    convert to the altivec format.  */
32497 
32498 rtx
rs6000_address_for_altivec(rtx x)32499 rs6000_address_for_altivec (rtx x)
32500 {
32501   gcc_assert (MEM_P (x));
32502   if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
32503     {
32504       rtx addr = XEXP (x, 0);
32505       int strict_p = (reload_in_progress || reload_completed);
32506 
32507       if (!legitimate_indexed_address_p (addr, strict_p)
32508 	  && !legitimate_indirect_address_p (addr, strict_p))
32509 	addr = copy_to_mode_reg (Pmode, addr);
32510 
32511       addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
32512       x = change_address (x, GET_MODE (x), addr);
32513     }
32514 
32515   return x;
32516 }
32517 
32518 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
32519 
32520    On the RS/6000, all integer constants are acceptable, most won't be valid
32521    for particular insns, though.  Only easy FP constants are acceptable.  */
32522 
32523 static bool
rs6000_legitimate_constant_p(enum machine_mode mode,rtx x)32524 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
32525 {
32526   if (TARGET_ELF && rs6000_tls_referenced_p (x))
32527     return false;
32528 
32529   return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
32530 	  || GET_MODE (x) == VOIDmode
32531 	  || (TARGET_POWERPC64 && mode == DImode)
32532 	  || easy_fp_constant (x, mode)
32533 	  || easy_vector_constant (x, mode));
32534 }
32535 
32536 
32537 
32538 /* Expand code to perform a call under the AIX or ELFv2 ABI.  */
32539 
32540 void
rs6000_call_aix(rtx value,rtx func_desc,rtx flag,rtx cookie)32541 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32542 {
32543   rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
32544   rtx toc_load = NULL_RTX;
32545   rtx toc_restore = NULL_RTX;
32546   rtx func_addr;
32547   rtx abi_reg = NULL_RTX;
32548   rtx call[4];
32549   int n_call;
32550   rtx insn;
32551 
32552   /* Handle longcall attributes.  */
32553   if (INTVAL (cookie) & CALL_LONG)
32554     func_desc = rs6000_longcall_ref (func_desc);
32555 
32556   /* Handle indirect calls.  */
32557   if (GET_CODE (func_desc) != SYMBOL_REF
32558       || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
32559     {
32560       /* Save the TOC into its reserved slot before the call,
32561 	 and prepare to restore it after the call.  */
32562       rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32563       rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
32564       rtx stack_toc_mem = gen_frame_mem (Pmode,
32565 					 gen_rtx_PLUS (Pmode, stack_ptr,
32566 						       stack_toc_offset));
32567       toc_restore = gen_rtx_SET (VOIDmode, toc_reg, stack_toc_mem);
32568 
32569       /* Can we optimize saving the TOC in the prologue or
32570 	 do we need to do it at every call?  */
32571       if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
32572 	cfun->machine->save_toc_in_prologue = true;
32573       else
32574 	{
32575 	  MEM_VOLATILE_P (stack_toc_mem) = 1;
32576 	  emit_move_insn (stack_toc_mem, toc_reg);
32577 	}
32578 
32579       if (DEFAULT_ABI == ABI_ELFv2)
32580 	{
32581 	  /* A function pointer in the ELFv2 ABI is just a plain address, but
32582 	     the ABI requires it to be loaded into r12 before the call.  */
32583 	  func_addr = gen_rtx_REG (Pmode, 12);
32584 	  emit_move_insn (func_addr, func_desc);
32585 	  abi_reg = func_addr;
32586 	}
32587       else
32588 	{
32589 	  /* A function pointer under AIX is a pointer to a data area whose
32590 	     first word contains the actual address of the function, whose
32591 	     second word contains a pointer to its TOC, and whose third word
32592 	     contains a value to place in the static chain register (r11).
32593 	     Note that if we load the static chain, our "trampoline" need
32594 	     not have any executable code.  */
32595 
32596 	  /* Load up address of the actual function.  */
32597 	  func_desc = force_reg (Pmode, func_desc);
32598 	  func_addr = gen_reg_rtx (Pmode);
32599 	  emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
32600 
32601 	  /* Prepare to load the TOC of the called function.  Note that the
32602 	     TOC load must happen immediately before the actual call so
32603 	     that unwinding the TOC registers works correctly.  See the
32604 	     comment in frob_update_context.  */
32605 	  rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
32606 	  rtx func_toc_mem = gen_rtx_MEM (Pmode,
32607 					  gen_rtx_PLUS (Pmode, func_desc,
32608 							func_toc_offset));
32609 	  toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
32610 
32611 	  /* If we have a static chain, load it up.  */
32612 	  if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
32613 	    {
32614 	      rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
32615 	      rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
32616 	      rtx func_sc_mem = gen_rtx_MEM (Pmode,
32617 					     gen_rtx_PLUS (Pmode, func_desc,
32618 							   func_sc_offset));
32619 	      emit_move_insn (sc_reg, func_sc_mem);
32620 	      abi_reg = sc_reg;
32621 	    }
32622 	}
32623     }
32624   else
32625     {
32626       /* Direct calls use the TOC: for local calls, the callee will
32627 	 assume the TOC register is set; for non-local calls, the
32628 	 PLT stub needs the TOC register.  */
32629       abi_reg = toc_reg;
32630       func_addr = func_desc;
32631     }
32632 
32633   /* Create the call.  */
32634   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
32635   if (value != NULL_RTX)
32636     call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32637   n_call = 1;
32638 
32639   if (toc_load)
32640     call[n_call++] = toc_load;
32641   if (toc_restore)
32642     call[n_call++] = toc_restore;
32643 
32644   call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
32645 
32646   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
32647   insn = emit_call_insn (insn);
32648 
32649   /* Mention all registers defined by the ABI to hold information
32650      as uses in CALL_INSN_FUNCTION_USAGE.  */
32651   if (abi_reg)
32652     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
32653 }
32654 
32655 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI.  */
32656 
32657 void
rs6000_sibcall_aix(rtx value,rtx func_desc,rtx flag,rtx cookie)32658 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32659 {
32660   rtx call[2];
32661   rtx insn;
32662 
32663   gcc_assert (INTVAL (cookie) == 0);
32664 
32665   /* Create the call.  */
32666   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
32667   if (value != NULL_RTX)
32668     call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32669 
32670   call[1] = simple_return_rtx;
32671 
32672   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
32673   insn = emit_call_insn (insn);
32674 
32675   /* Note use of the TOC register.  */
32676   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
32677   /* We need to also mark a use of the link register since the function we
32678      sibling-call to will use it to return to our caller.  */
32679   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, LR_REGNO));
32680 }
32681 
32682 /* Return whether we need to always update the saved TOC pointer when we update
32683    the stack pointer.  */
32684 
32685 static bool
rs6000_save_toc_in_prologue_p(void)32686 rs6000_save_toc_in_prologue_p (void)
32687 {
32688   return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
32689 }
32690 
32691 #ifdef HAVE_GAS_HIDDEN
32692 # define USE_HIDDEN_LINKONCE 1
32693 #else
32694 # define USE_HIDDEN_LINKONCE 0
32695 #endif
32696 
32697 /* Fills in the label name that should be used for a 476 link stack thunk.  */
32698 
32699 void
get_ppc476_thunk_name(char name[32])32700 get_ppc476_thunk_name (char name[32])
32701 {
32702   gcc_assert (TARGET_LINK_STACK);
32703 
32704   if (USE_HIDDEN_LINKONCE)
32705     sprintf (name, "__ppc476.get_thunk");
32706   else
32707     ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
32708 }
32709 
32710 /* This function emits the simple thunk routine that is used to preserve
32711    the link stack on the 476 cpu.  */
32712 
32713 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
32714 static void
rs6000_code_end(void)32715 rs6000_code_end (void)
32716 {
32717   char name[32];
32718   tree decl;
32719 
32720   if (!TARGET_LINK_STACK)
32721     return;
32722 
32723   get_ppc476_thunk_name (name);
32724 
32725   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
32726 		     build_function_type_list (void_type_node, NULL_TREE));
32727   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
32728 				   NULL_TREE, void_type_node);
32729   TREE_PUBLIC (decl) = 1;
32730   TREE_STATIC (decl) = 1;
32731 
32732 #if RS6000_WEAK
32733   if (USE_HIDDEN_LINKONCE)
32734     {
32735       DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
32736       targetm.asm_out.unique_section (decl, 0);
32737       switch_to_section (get_named_section (decl, NULL, 0));
32738       DECL_WEAK (decl) = 1;
32739       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
32740       targetm.asm_out.globalize_label (asm_out_file, name);
32741       targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
32742       ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
32743     }
32744   else
32745 #endif
32746     {
32747       switch_to_section (text_section);
32748       ASM_OUTPUT_LABEL (asm_out_file, name);
32749     }
32750 
32751   DECL_INITIAL (decl) = make_node (BLOCK);
32752   current_function_decl = decl;
32753   init_function_start (decl);
32754   first_function_block_is_cold = false;
32755   /* Make sure unwind info is emitted for the thunk if needed.  */
32756   final_start_function (emit_barrier (), asm_out_file, 1);
32757 
32758   fputs ("\tblr\n", asm_out_file);
32759 
32760   final_end_function ();
32761   init_insn_lengths ();
32762   free_after_compilation (cfun);
32763   set_cfun (NULL);
32764   current_function_decl = NULL;
32765 }
32766 
32767 /* Add r30 to hard reg set if the prologue sets it up and it is not
32768    pic_offset_table_rtx.  */
32769 
32770 static void
rs6000_set_up_by_prologue(struct hard_reg_set_container * set)32771 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
32772 {
32773   if (!TARGET_SINGLE_PIC_BASE
32774       && TARGET_TOC
32775       && TARGET_MINIMAL_TOC
32776       && get_pool_size () != 0)
32777     add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
32778 }
32779 
32780 
32781 /* Helper function for rs6000_split_logical to emit a logical instruction after
32782    spliting the operation to single GPR registers.
32783 
32784    DEST is the destination register.
32785    OP1 and OP2 are the input source registers.
32786    CODE is the base operation (AND, IOR, XOR, NOT).
32787    MODE is the machine mode.
32788    If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32789    If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32790    If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32791    CLOBBER_REG is either NULL or a scratch register of type CC to allow
32792    formation of the AND instructions.  */
32793 
32794 static void
rs6000_split_logical_inner(rtx dest,rtx op1,rtx op2,enum rtx_code code,enum machine_mode mode,bool complement_final_p,bool complement_op1_p,bool complement_op2_p,rtx clobber_reg)32795 rs6000_split_logical_inner (rtx dest,
32796 			    rtx op1,
32797 			    rtx op2,
32798 			    enum rtx_code code,
32799 			    enum machine_mode mode,
32800 			    bool complement_final_p,
32801 			    bool complement_op1_p,
32802 			    bool complement_op2_p,
32803 			    rtx clobber_reg)
32804 {
32805   rtx bool_rtx;
32806   rtx set_rtx;
32807 
32808   /* Optimize AND of 0/0xffffffff and IOR/XOR of 0.  */
32809   if (op2 && GET_CODE (op2) == CONST_INT
32810       && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
32811       && !complement_final_p && !complement_op1_p && !complement_op2_p)
32812     {
32813       HOST_WIDE_INT mask = GET_MODE_MASK (mode);
32814       HOST_WIDE_INT value = INTVAL (op2) & mask;
32815 
32816       /* Optimize AND of 0 to just set 0.  Optimize AND of -1 to be a move.  */
32817       if (code == AND)
32818 	{
32819 	  if (value == 0)
32820 	    {
32821 	      emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
32822 	      return;
32823 	    }
32824 
32825 	  else if (value == mask)
32826 	    {
32827 	      if (!rtx_equal_p (dest, op1))
32828 		emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32829 	      return;
32830 	    }
32831 	}
32832 
32833       /* Optimize IOR/XOR of 0 to be a simple move.  Split large operations
32834 	 into separate ORI/ORIS or XORI/XORIS instrucitons.  */
32835       else if (code == IOR || code == XOR)
32836 	{
32837 	  if (value == 0)
32838 	    {
32839 	      if (!rtx_equal_p (dest, op1))
32840 		emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32841 	      return;
32842 	    }
32843 	}
32844     }
32845 
32846   if (complement_op1_p)
32847     op1 = gen_rtx_NOT (mode, op1);
32848 
32849   if (complement_op2_p)
32850     op2 = gen_rtx_NOT (mode, op2);
32851 
32852   /* For canonical RTL, if only one arm is inverted it is the first.  */
32853   if (!complement_op1_p && complement_op2_p)
32854     {
32855       rtx temp = op1;
32856       op1 = op2;
32857       op2 = temp;
32858     }
32859 
32860   bool_rtx = ((code == NOT)
32861 	      ? gen_rtx_NOT (mode, op1)
32862 	      : gen_rtx_fmt_ee (code, mode, op1, op2));
32863 
32864   if (complement_final_p)
32865     bool_rtx = gen_rtx_NOT (mode, bool_rtx);
32866 
32867   set_rtx = gen_rtx_SET (VOIDmode, dest, bool_rtx);
32868 
32869   /* Is this AND with an explicit clobber?  */
32870   if (clobber_reg)
32871     {
32872       rtx clobber = gen_rtx_CLOBBER (VOIDmode, clobber_reg);
32873       set_rtx = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_rtx, clobber));
32874     }
32875 
32876   emit_insn (set_rtx);
32877   return;
32878 }
32879 
32880 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system.  These
32881    operations are split immediately during RTL generation to allow for more
32882    optimizations of the AND/IOR/XOR.
32883 
32884    OPERANDS is an array containing the destination and two input operands.
32885    CODE is the base operation (AND, IOR, XOR, NOT).
32886    MODE is the machine mode.
32887    If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32888    If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32889    If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32890    CLOBBER_REG is either NULL or a scratch register of type CC to allow
32891    formation of the AND instructions.  */
32892 
32893 static void
rs6000_split_logical_di(rtx operands[3],enum rtx_code code,bool complement_final_p,bool complement_op1_p,bool complement_op2_p,rtx clobber_reg)32894 rs6000_split_logical_di (rtx operands[3],
32895 			 enum rtx_code code,
32896 			 bool complement_final_p,
32897 			 bool complement_op1_p,
32898 			 bool complement_op2_p,
32899 			 rtx clobber_reg)
32900 {
32901   const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
32902   const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
32903   const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
32904   enum hi_lo { hi = 0, lo = 1 };
32905   rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
32906   size_t i;
32907 
32908   op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
32909   op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
32910   op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
32911   op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
32912 
32913   if (code == NOT)
32914     op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
32915   else
32916     {
32917       if (GET_CODE (operands[2]) != CONST_INT)
32918 	{
32919 	  op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
32920 	  op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
32921 	}
32922       else
32923 	{
32924 	  HOST_WIDE_INT value = INTVAL (operands[2]);
32925 	  HOST_WIDE_INT value_hi_lo[2];
32926 
32927 	  gcc_assert (!complement_final_p);
32928 	  gcc_assert (!complement_op1_p);
32929 	  gcc_assert (!complement_op2_p);
32930 
32931 	  value_hi_lo[hi] = value >> 32;
32932 	  value_hi_lo[lo] = value & lower_32bits;
32933 
32934 	  for (i = 0; i < 2; i++)
32935 	    {
32936 	      HOST_WIDE_INT sub_value = value_hi_lo[i];
32937 
32938 	      if (sub_value & sign_bit)
32939 		sub_value |= upper_32bits;
32940 
32941 	      op2_hi_lo[i] = GEN_INT (sub_value);
32942 
32943 	      /* If this is an AND instruction, check to see if we need to load
32944 		 the value in a register.  */
32945 	      if (code == AND && sub_value != -1 && sub_value != 0
32946 		  && !and_operand (op2_hi_lo[i], SImode))
32947 		op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
32948 	    }
32949 	}
32950     }
32951 
32952   for (i = 0; i < 2; i++)
32953     {
32954       /* Split large IOR/XOR operations.  */
32955       if ((code == IOR || code == XOR)
32956 	  && GET_CODE (op2_hi_lo[i]) == CONST_INT
32957 	  && !complement_final_p
32958 	  && !complement_op1_p
32959 	  && !complement_op2_p
32960 	  && clobber_reg == NULL_RTX
32961 	  && !logical_const_operand (op2_hi_lo[i], SImode))
32962 	{
32963 	  HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
32964 	  HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
32965 	  HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
32966 	  rtx tmp = gen_reg_rtx (SImode);
32967 
32968 	  /* Make sure the constant is sign extended.  */
32969 	  if ((hi_16bits & sign_bit) != 0)
32970 	    hi_16bits |= upper_32bits;
32971 
32972 	  rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
32973 				      code, SImode, false, false, false,
32974 				      NULL_RTX);
32975 
32976 	  rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
32977 				      code, SImode, false, false, false,
32978 				      NULL_RTX);
32979 	}
32980       else
32981 	rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
32982 				    code, SImode, complement_final_p,
32983 				    complement_op1_p, complement_op2_p,
32984 				    clobber_reg);
32985     }
32986 
32987   return;
32988 }
32989 
32990 /* Split the insns that make up boolean operations operating on multiple GPR
32991    registers.  The boolean MD patterns ensure that the inputs either are
32992    exactly the same as the output registers, or there is no overlap.
32993 
32994    OPERANDS is an array containing the destination and two input operands.
32995    CODE is the base operation (AND, IOR, XOR, NOT).
32996    MODE is the machine mode.
32997    If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32998    If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32999    If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
33000    CLOBBER_REG is either NULL or a scratch register of type CC to allow
33001    formation of the AND instructions.  */
33002 
33003 void
rs6000_split_logical(rtx operands[3],enum rtx_code code,bool complement_final_p,bool complement_op1_p,bool complement_op2_p,rtx clobber_reg)33004 rs6000_split_logical (rtx operands[3],
33005 		      enum rtx_code code,
33006 		      bool complement_final_p,
33007 		      bool complement_op1_p,
33008 		      bool complement_op2_p,
33009 		      rtx clobber_reg)
33010 {
33011   enum machine_mode mode = GET_MODE (operands[0]);
33012   enum machine_mode sub_mode;
33013   rtx op0, op1, op2;
33014   int sub_size, regno0, regno1, nregs, i;
33015 
33016   /* If this is DImode, use the specialized version that can run before
33017      register allocation.  */
33018   if (mode == DImode && !TARGET_POWERPC64)
33019     {
33020       rs6000_split_logical_di (operands, code, complement_final_p,
33021 			       complement_op1_p, complement_op2_p,
33022 			       clobber_reg);
33023       return;
33024     }
33025 
33026   op0 = operands[0];
33027   op1 = operands[1];
33028   op2 = (code == NOT) ? NULL_RTX : operands[2];
33029   sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
33030   sub_size = GET_MODE_SIZE (sub_mode);
33031   regno0 = REGNO (op0);
33032   regno1 = REGNO (op1);
33033 
33034   gcc_assert (reload_completed);
33035   gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
33036   gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
33037 
33038   nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
33039   gcc_assert (nregs > 1);
33040 
33041   if (op2 && REG_P (op2))
33042     gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
33043 
33044   for (i = 0; i < nregs; i++)
33045     {
33046       int offset = i * sub_size;
33047       rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
33048       rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
33049       rtx sub_op2 = ((code == NOT)
33050 		     ? NULL_RTX
33051 		     : simplify_subreg (sub_mode, op2, mode, offset));
33052 
33053       rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
33054 				  complement_final_p, complement_op1_p,
33055 				  complement_op2_p, clobber_reg);
33056     }
33057 
33058   return;
33059 }
33060 
33061 
33062 /* Return true if the peephole2 can combine a load involving a combination of
33063    an addis instruction and a load with an offset that can be fused together on
33064    a power8.  */
33065 
33066 bool
fusion_gpr_load_p(rtx addis_reg,rtx addis_value,rtx target,rtx mem)33067 fusion_gpr_load_p (rtx addis_reg,	/* register set via addis.  */
33068 		   rtx addis_value,	/* addis value.  */
33069 		   rtx target,		/* target register that is loaded.  */
33070 		   rtx mem)		/* bottom part of the memory addr. */
33071 {
33072   rtx addr;
33073   rtx base_reg;
33074 
33075   /* Validate arguments.  */
33076   if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
33077     return false;
33078 
33079   if (!base_reg_operand (target, GET_MODE (target)))
33080     return false;
33081 
33082   if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
33083     return false;
33084 
33085   /* Allow sign/zero extension.  */
33086   if (GET_CODE (mem) == ZERO_EXTEND
33087       || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
33088     mem = XEXP (mem, 0);
33089 
33090   if (!MEM_P (mem))
33091     return false;
33092 
33093   if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
33094     return false;
33095 
33096   addr = XEXP (mem, 0);			/* either PLUS or LO_SUM.  */
33097   if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
33098     return false;
33099 
33100   /* Validate that the register used to load the high value is either the
33101      register being loaded, or we can safely replace its use.
33102 
33103      This function is only called from the peephole2 pass and we assume that
33104      there are 2 instructions in the peephole (addis and load), so we want to
33105      check if the target register was not used in the memory address and the
33106      register to hold the addis result is dead after the peephole.  */
33107   if (REGNO (addis_reg) != REGNO (target))
33108     {
33109       if (reg_mentioned_p (target, mem))
33110 	return false;
33111 
33112       if (!peep2_reg_dead_p (2, addis_reg))
33113 	return false;
33114 
33115       /* If the target register being loaded is the stack pointer, we must
33116          avoid loading any other value into it, even temporarily.  */
33117       if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
33118 	return false;
33119     }
33120 
33121   base_reg = XEXP (addr, 0);
33122   return REGNO (addis_reg) == REGNO (base_reg);
33123 }
33124 
33125 /* During the peephole2 pass, adjust and expand the insns for a load fusion
33126    sequence.  We adjust the addis register to use the target register.  If the
33127    load sign extends, we adjust the code to do the zero extending load, and an
33128    explicit sign extension later since the fusion only covers zero extending
33129    loads.
33130 
33131    The operands are:
33132 	operands[0]	register set with addis (to be replaced with target)
33133 	operands[1]	value set via addis
33134 	operands[2]	target register being loaded
33135 	operands[3]	D-form memory reference using operands[0].  */
33136 
33137 void
expand_fusion_gpr_load(rtx * operands)33138 expand_fusion_gpr_load (rtx *operands)
33139 {
33140   rtx addis_value = operands[1];
33141   rtx target = operands[2];
33142   rtx orig_mem = operands[3];
33143   rtx  new_addr, new_mem, orig_addr, offset;
33144   enum rtx_code plus_or_lo_sum;
33145   enum machine_mode target_mode = GET_MODE (target);
33146   enum machine_mode extend_mode = target_mode;
33147   enum machine_mode ptr_mode = Pmode;
33148   enum rtx_code extend = UNKNOWN;
33149 
33150   if (GET_CODE (orig_mem) == ZERO_EXTEND
33151       || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
33152     {
33153       extend = GET_CODE (orig_mem);
33154       orig_mem = XEXP (orig_mem, 0);
33155       target_mode = GET_MODE (orig_mem);
33156     }
33157 
33158   gcc_assert (MEM_P (orig_mem));
33159 
33160   orig_addr = XEXP (orig_mem, 0);
33161   plus_or_lo_sum = GET_CODE (orig_addr);
33162   gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
33163 
33164   offset = XEXP (orig_addr, 1);
33165   new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
33166   new_mem = replace_equiv_address_nv (orig_mem, new_addr);
33167 
33168   if (extend != UNKNOWN)
33169     new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
33170 
33171   new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
33172 			    UNSPEC_FUSION_GPR);
33173   emit_insn (gen_rtx_SET (VOIDmode, target, new_mem));
33174 
33175   if (extend == SIGN_EXTEND)
33176     {
33177       int sub_off = ((BYTES_BIG_ENDIAN)
33178 		     ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
33179 		     : 0);
33180       rtx sign_reg
33181 	= simplify_subreg (target_mode, target, extend_mode, sub_off);
33182 
33183       emit_insn (gen_rtx_SET (VOIDmode, target,
33184 			      gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
33185     }
33186 
33187   return;
33188 }
33189 
33190 /* Return a string to fuse an addis instruction with a gpr load to the same
33191    register that we loaded up the addis instruction.  The address that is used
33192    is the logical address that was formed during peephole2:
33193 	(lo_sum (high) (low-part))
33194 
33195    The code is complicated, so we call output_asm_insn directly, and just
33196    return "".  */
33197 
33198 const char *
emit_fusion_gpr_load(rtx target,rtx mem)33199 emit_fusion_gpr_load (rtx target, rtx mem)
33200 {
33201   rtx addis_value;
33202   rtx fuse_ops[10];
33203   rtx addr;
33204   rtx load_offset;
33205   const char *addis_str = NULL;
33206   const char *load_str = NULL;
33207   const char *mode_name = NULL;
33208   char insn_template[80];
33209   enum machine_mode mode;
33210   const char *comment_str = ASM_COMMENT_START;
33211 
33212   if (GET_CODE (mem) == ZERO_EXTEND)
33213     mem = XEXP (mem, 0);
33214 
33215   gcc_assert (REG_P (target) && MEM_P (mem));
33216 
33217   if (*comment_str == ' ')
33218     comment_str++;
33219 
33220   addr = XEXP (mem, 0);
33221   if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
33222     gcc_unreachable ();
33223 
33224   addis_value = XEXP (addr, 0);
33225   load_offset = XEXP (addr, 1);
33226 
33227   /* Now emit the load instruction to the same register.  */
33228   mode = GET_MODE (mem);
33229   switch (mode)
33230     {
33231     case QImode:
33232       mode_name = "char";
33233       load_str = "lbz";
33234       break;
33235 
33236     case HImode:
33237       mode_name = "short";
33238       load_str = "lhz";
33239       break;
33240 
33241     case SImode:
33242       mode_name = "int";
33243       load_str = "lwz";
33244       break;
33245 
33246     case DImode:
33247       gcc_assert (TARGET_POWERPC64);
33248       mode_name = "long";
33249       load_str = "ld";
33250       break;
33251 
33252     default:
33253       gcc_unreachable ();
33254     }
33255 
33256   /* Emit the addis instruction.  */
33257   fuse_ops[0] = target;
33258   if (satisfies_constraint_L (addis_value))
33259     {
33260       fuse_ops[1] = addis_value;
33261       addis_str = "lis %0,%v1";
33262     }
33263 
33264   else if (GET_CODE (addis_value) == PLUS)
33265     {
33266       rtx op0 = XEXP (addis_value, 0);
33267       rtx op1 = XEXP (addis_value, 1);
33268 
33269       if (REG_P (op0) && CONST_INT_P (op1)
33270 	  && satisfies_constraint_L (op1))
33271 	{
33272 	  fuse_ops[1] = op0;
33273 	  fuse_ops[2] = op1;
33274 	  addis_str = "addis %0,%1,%v2";
33275 	}
33276     }
33277 
33278   else if (GET_CODE (addis_value) == HIGH)
33279     {
33280       rtx value = XEXP (addis_value, 0);
33281       if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
33282 	{
33283 	  fuse_ops[1] = XVECEXP (value, 0, 0);		/* symbol ref.  */
33284 	  fuse_ops[2] = XVECEXP (value, 0, 1);		/* TOC register.  */
33285 	  if (TARGET_ELF)
33286 	    addis_str = "addis %0,%2,%1@toc@ha";
33287 
33288 	  else if (TARGET_XCOFF)
33289 	    addis_str = "addis %0,%1@u(%2)";
33290 
33291 	  else
33292 	    gcc_unreachable ();
33293 	}
33294 
33295       else if (GET_CODE (value) == PLUS)
33296 	{
33297 	  rtx op0 = XEXP (value, 0);
33298 	  rtx op1 = XEXP (value, 1);
33299 
33300 	  if (GET_CODE (op0) == UNSPEC
33301 	      && XINT (op0, 1) == UNSPEC_TOCREL
33302 	      && CONST_INT_P (op1))
33303 	    {
33304 	      fuse_ops[1] = XVECEXP (op0, 0, 0);	/* symbol ref.  */
33305 	      fuse_ops[2] = XVECEXP (op0, 0, 1);	/* TOC register.  */
33306 	      fuse_ops[3] = op1;
33307 	      if (TARGET_ELF)
33308 		addis_str = "addis %0,%2,%1+%3@toc@ha";
33309 
33310 	      else if (TARGET_XCOFF)
33311 		addis_str = "addis %0,%1+%3@u(%2)";
33312 
33313 	      else
33314 		gcc_unreachable ();
33315 	    }
33316 	}
33317 
33318       else if (satisfies_constraint_L (value))
33319 	{
33320 	  fuse_ops[1] = value;
33321 	  addis_str = "lis %0,%v1";
33322 	}
33323 
33324       else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
33325 	{
33326 	  fuse_ops[1] = value;
33327 	  addis_str = "lis %0,%1@ha";
33328 	}
33329     }
33330 
33331   if (!addis_str)
33332     fatal_insn ("Could not generate addis value for fusion", addis_value);
33333 
33334   sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s", addis_str,
33335 	   comment_str, mode_name);
33336   output_asm_insn (insn_template, fuse_ops);
33337 
33338   /* Emit the D-form load instruction.  */
33339   if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
33340     {
33341       sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
33342       fuse_ops[1] = load_offset;
33343       output_asm_insn (insn_template, fuse_ops);
33344     }
33345 
33346   else if (GET_CODE (load_offset) == UNSPEC
33347 	   && XINT (load_offset, 1) == UNSPEC_TOCREL)
33348     {
33349       if (TARGET_ELF)
33350 	sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
33351 
33352       else if (TARGET_XCOFF)
33353 	sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
33354 
33355       else
33356 	gcc_unreachable ();
33357 
33358       fuse_ops[1] = XVECEXP (load_offset, 0, 0);
33359       output_asm_insn (insn_template, fuse_ops);
33360     }
33361 
33362   else if (GET_CODE (load_offset) == PLUS
33363 	   && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
33364 	   && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
33365 	   && CONST_INT_P (XEXP (load_offset, 1)))
33366     {
33367       rtx tocrel_unspec = XEXP (load_offset, 0);
33368       if (TARGET_ELF)
33369 	sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
33370 
33371       else if (TARGET_XCOFF)
33372 	sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
33373 
33374       else
33375 	gcc_unreachable ();
33376 
33377       fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
33378       fuse_ops[2] = XEXP (load_offset, 1);
33379       output_asm_insn (insn_template, fuse_ops);
33380     }
33381 
33382   else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
33383     {
33384       sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
33385 
33386       fuse_ops[1] = load_offset;
33387       output_asm_insn (insn_template, fuse_ops);
33388     }
33389 
33390   else
33391     fatal_insn ("Unable to generate load offset for fusion", load_offset);
33392 
33393   return "";
33394 }
33395 
33396 /* Analyze vector computations and remove unnecessary doubleword
33397    swaps (xxswapdi instructions).  This pass is performed only
33398    for little-endian VSX code generation.
33399 
33400    For this specific case, loads and stores of 4x32 and 2x64 vectors
33401    are inefficient.  These are implemented using the lvx2dx and
33402    stvx2dx instructions, which invert the order of doublewords in
33403    a vector register.  Thus the code generation inserts an xxswapdi
33404    after each such load, and prior to each such store.  (For spill
33405    code after register assignment, an additional xxswapdi is inserted
33406    following each store in order to return a hard register to its
33407    unpermuted value.)
33408 
33409    The extra xxswapdi instructions reduce performance.  This can be
33410    particularly bad for vectorized code.  The purpose of this pass
33411    is to reduce the number of xxswapdi instructions required for
33412    correctness.
33413 
33414    The primary insight is that much code that operates on vectors
33415    does not care about the relative order of elements in a register,
33416    so long as the correct memory order is preserved.  If we have
33417    a computation where all input values are provided by lvxd2x/xxswapdi
33418    sequences, all outputs are stored using xxswapdi/stvxd2x sequences,
33419    and all intermediate computations are pure SIMD (independent of
33420    element order), then all the xxswapdi's associated with the loads
33421    and stores may be removed.
33422 
33423    This pass uses some of the infrastructure and logical ideas from
33424    the "web" pass in web.c.  We create maximal webs of computations
33425    fitting the description above using union-find.  Each such web is
33426    then optimized by removing its unnecessary xxswapdi instructions.
33427 
33428    The pass is placed prior to global optimization so that we can
33429    perform the optimization in the safest and simplest way possible;
33430    that is, by replacing each xxswapdi insn with a register copy insn.
33431    Subsequent forward propagation will remove copies where possible.
33432 
33433    There are some operations sensitive to element order for which we
33434    can still allow the operation, provided we modify those operations.
33435    These include CONST_VECTORs, for which we must swap the first and
33436    second halves of the constant vector; and SUBREGs, for which we
33437    must adjust the byte offset to account for the swapped doublewords.
33438    A remaining opportunity would be non-immediate-form splats, for
33439    which we should adjust the selected lane of the input.  We should
33440    also make code generation adjustments for sum-across operations,
33441    since this is a common vectorizer reduction.
33442 
33443    Because we run prior to the first split, we can see loads and stores
33444    here that match *vsx_le_perm_{load,store}_<mode>.  These are vanilla
33445    vector loads and stores that have not yet been split into a permuting
33446    load/store and a swap.  (One way this can happen is with a builtin
33447    call to vec_vsx_{ld,st}.)  We can handle these as well, but rather
33448    than deleting a swap, we convert the load/store into a permuting
33449    load/store (which effectively removes the swap).  */
33450 
33451 /* Notes on Permutes
33452 
33453    We do not currently handle computations that contain permutes.  There
33454    is a general transformation that can be performed correctly, but it
33455    may introduce more expensive code than it replaces.  To handle these
33456    would require a cost model to determine when to perform the optimization.
33457    This commentary records how this could be done if desired.
33458 
33459    The most general permute is something like this (example for V16QI):
33460 
33461    (vec_select:V16QI (vec_concat:V32QI (op1:V16QI) (op2:V16QI))
33462                      (parallel [(const_int a0) (const_int a1)
33463                                  ...
33464                                 (const_int a14) (const_int a15)]))
33465 
33466    where a0,...,a15 are in [0,31] and select elements from op1 and op2
33467    to produce in the result.
33468 
33469    Regardless of mode, we can convert the PARALLEL to a mask of 16
33470    byte-element selectors.  Let's call this M, with M[i] representing
33471    the ith byte-element selector value.  Then if we swap doublewords
33472    throughout the computation, we can get correct behavior by replacing
33473    M with M' as follows:
33474 
33475             { M[i+8]+8 : i < 8, M[i+8] in [0,7] U [16,23]
33476     M'[i] = { M[i+8]-8 : i < 8, M[i+8] in [8,15] U [24,31]
33477             { M[i-8]+8 : i >= 8, M[i-8] in [0,7] U [16,23]
33478             { M[i-8]-8 : i >= 8, M[i-8] in [8,15] U [24,31]
33479 
33480    This seems promising at first, since we are just replacing one mask
33481    with another.  But certain masks are preferable to others.  If M
33482    is a mask that matches a vmrghh pattern, for example, M' certainly
33483    will not.  Instead of a single vmrghh, we would generate a load of
33484    M' and a vperm.  So we would need to know how many xxswapd's we can
33485    remove as a result of this transformation to determine if it's
33486    profitable; and preferably the logic would need to be aware of all
33487    the special preferable masks.
33488 
33489    Another form of permute is an UNSPEC_VPERM, in which the mask is
33490    already in a register.  In some cases, this mask may be a constant
33491    that we can discover with ud-chains, in which case the above
33492    transformation is ok.  However, the common usage here is for the
33493    mask to be produced by an UNSPEC_LVSL, in which case the mask
33494    cannot be known at compile time.  In such a case we would have to
33495    generate several instructions to compute M' as above at run time,
33496    and a cost model is needed again.  */
33497 
33498 /* This is based on the union-find logic in web.c.  web_entry_base is
33499    defined in df.h.  */
33500 class swap_web_entry : public web_entry_base
33501 {
33502  public:
33503   /* Pointer to the insn.  */
33504   rtx insn;
33505   /* Set if insn contains a mention of a vector register.  All other
33506      fields are undefined if this field is unset.  */
33507   unsigned int is_relevant : 1;
33508   /* Set if insn is a load.  */
33509   unsigned int is_load : 1;
33510   /* Set if insn is a store.  */
33511   unsigned int is_store : 1;
33512   /* Set if insn is a doubleword swap.  This can either be a register swap
33513      or a permuting load or store (test is_load and is_store for this).  */
33514   unsigned int is_swap : 1;
33515   /* Set if the insn has a live-in use of a parameter register.  */
33516   unsigned int is_live_in : 1;
33517   /* Set if the insn has a live-out def of a return register.  */
33518   unsigned int is_live_out : 1;
33519   /* Set if the insn contains a subreg reference of a vector register.  */
33520   unsigned int contains_subreg : 1;
33521   /* Set if the insn contains a 128-bit integer operand.  */
33522   unsigned int is_128_int : 1;
33523   /* Set if this is a call-insn.  */
33524   unsigned int is_call : 1;
33525   /* Set if this insn does not perform a vector operation for which
33526      element order matters, or if we know how to fix it up if it does.
33527      Undefined if is_swap is set.  */
33528   unsigned int is_swappable : 1;
33529   /* A nonzero value indicates what kind of special handling for this
33530      insn is required if doublewords are swapped.  Undefined if
33531      is_swappable is not set.  */
33532   unsigned int special_handling : 3;
33533   /* Set if the web represented by this entry cannot be optimized.  */
33534   unsigned int web_not_optimizable : 1;
33535   /* Set if this insn should be deleted.  */
33536   unsigned int will_delete : 1;
33537 };
33538 
33539 enum special_handling_values {
33540   SH_NONE = 0,
33541   SH_CONST_VECTOR,
33542   SH_SUBREG,
33543   SH_NOSWAP_LD,
33544   SH_NOSWAP_ST,
33545   SH_EXTRACT,
33546   SH_SPLAT
33547 };
33548 
33549 /* Union INSN with all insns containing definitions that reach USE.
33550    Detect whether USE is live-in to the current function.  */
33551 static void
union_defs(swap_web_entry * insn_entry,rtx insn,df_ref use)33552 union_defs (swap_web_entry *insn_entry, rtx insn, df_ref use)
33553 {
33554   struct df_link *link = DF_REF_CHAIN (use);
33555 
33556   if (!link)
33557     insn_entry[INSN_UID (insn)].is_live_in = 1;
33558 
33559   while (link)
33560     {
33561       if (DF_REF_IS_ARTIFICIAL (link->ref))
33562 	insn_entry[INSN_UID (insn)].is_live_in = 1;
33563 
33564       if (DF_REF_INSN_INFO (link->ref))
33565 	{
33566 	  rtx def_insn = DF_REF_INSN (link->ref);
33567 	  (void)unionfind_union (insn_entry + INSN_UID (insn),
33568 				 insn_entry + INSN_UID (def_insn));
33569 	}
33570 
33571       link = link->next;
33572     }
33573 }
33574 
33575 /* Union INSN with all insns containing uses reached from DEF.
33576    Detect whether DEF is live-out from the current function.  */
33577 static void
union_uses(swap_web_entry * insn_entry,rtx insn,df_ref def)33578 union_uses (swap_web_entry *insn_entry, rtx insn, df_ref def)
33579 {
33580   struct df_link *link = DF_REF_CHAIN (def);
33581 
33582   if (!link)
33583     insn_entry[INSN_UID (insn)].is_live_out = 1;
33584 
33585   while (link)
33586     {
33587       /* This could be an eh use or some other artificial use;
33588 	 we treat these all the same (killing the optimization).  */
33589       if (DF_REF_IS_ARTIFICIAL (link->ref))
33590 	insn_entry[INSN_UID (insn)].is_live_out = 1;
33591 
33592       if (DF_REF_INSN_INFO (link->ref))
33593 	{
33594 	  rtx use_insn = DF_REF_INSN (link->ref);
33595 	  (void)unionfind_union (insn_entry + INSN_UID (insn),
33596 				 insn_entry + INSN_UID (use_insn));
33597 	}
33598 
33599       link = link->next;
33600     }
33601 }
33602 
33603 /* Return 1 iff INSN is a load insn, including permuting loads that
33604    represent an lvxd2x instruction; else return 0.  */
33605 static unsigned int
insn_is_load_p(rtx insn)33606 insn_is_load_p (rtx insn)
33607 {
33608   rtx body = PATTERN (insn);
33609 
33610   if (GET_CODE (body) == SET)
33611     {
33612       if (GET_CODE (SET_SRC (body)) == MEM)
33613 	return 1;
33614 
33615       if (GET_CODE (SET_SRC (body)) == VEC_SELECT
33616 	  && GET_CODE (XEXP (SET_SRC (body), 0)) == MEM)
33617 	return 1;
33618 
33619       return 0;
33620     }
33621 
33622   if (GET_CODE (body) != PARALLEL)
33623     return 0;
33624 
33625   rtx set = XVECEXP (body, 0, 0);
33626 
33627   if (GET_CODE (set) == SET && GET_CODE (SET_SRC (set)) == MEM)
33628     return 1;
33629 
33630   return 0;
33631 }
33632 
33633 /* Return 1 iff INSN is a store insn, including permuting stores that
33634    represent an stvxd2x instruction; else return 0.  */
33635 static unsigned int
insn_is_store_p(rtx insn)33636 insn_is_store_p (rtx insn)
33637 {
33638   rtx body = PATTERN (insn);
33639   if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == MEM)
33640     return 1;
33641   if (GET_CODE (body) != PARALLEL)
33642     return 0;
33643   rtx set = XVECEXP (body, 0, 0);
33644   if (GET_CODE (set) == SET && GET_CODE (SET_DEST (set)) == MEM)
33645     return 1;
33646   return 0;
33647 }
33648 
33649 /* Return 1 iff INSN swaps doublewords.  This may be a reg-reg swap,
33650    a permuting load, or a permuting store.  */
33651 static unsigned int
insn_is_swap_p(rtx insn)33652 insn_is_swap_p (rtx insn)
33653 {
33654   rtx body = PATTERN (insn);
33655   if (GET_CODE (body) != SET)
33656     return 0;
33657   rtx rhs = SET_SRC (body);
33658   if (GET_CODE (rhs) != VEC_SELECT)
33659     return 0;
33660   rtx parallel = XEXP (rhs, 1);
33661   if (GET_CODE (parallel) != PARALLEL)
33662     return 0;
33663   unsigned int len = XVECLEN (parallel, 0);
33664   if (len != 2 && len != 4 && len != 8 && len != 16)
33665     return 0;
33666   for (unsigned int i = 0; i < len / 2; ++i)
33667     {
33668       rtx op = XVECEXP (parallel, 0, i);
33669       if (GET_CODE (op) != CONST_INT || INTVAL (op) != len / 2 + i)
33670 	return 0;
33671     }
33672   for (unsigned int i = len / 2; i < len; ++i)
33673     {
33674       rtx op = XVECEXP (parallel, 0, i);
33675       if (GET_CODE (op) != CONST_INT || INTVAL (op) != i - len / 2)
33676 	return 0;
33677     }
33678   return 1;
33679 }
33680 
33681 /* Return 1 iff OP is an operand that will not be affected by having
33682    vector doublewords swapped in memory.  */
33683 static unsigned int
rtx_is_swappable_p(rtx op,unsigned int * special)33684 rtx_is_swappable_p (rtx op, unsigned int *special)
33685 {
33686   enum rtx_code code = GET_CODE (op);
33687   int i, j;
33688   rtx parallel;
33689 
33690   switch (code)
33691     {
33692     case LABEL_REF:
33693     case SYMBOL_REF:
33694     case CLOBBER:
33695     case REG:
33696       return 1;
33697 
33698     case VEC_CONCAT:
33699     case ASM_INPUT:
33700     case ASM_OPERANDS:
33701       return 0;
33702 
33703     case CONST_VECTOR:
33704       {
33705 	*special = SH_CONST_VECTOR;
33706 	return 1;
33707       }
33708 
33709     case VEC_DUPLICATE:
33710       /* Opportunity: If XEXP (op, 0) has the same mode as the result,
33711 	 and XEXP (op, 1) is a PARALLEL with a single QImode const int,
33712 	 it represents a vector splat for which we can do special
33713 	 handling.  */
33714       if (GET_CODE (XEXP (op, 0)) == CONST_INT)
33715 	return 1;
33716       else if (GET_CODE (XEXP (op, 0)) == REG
33717 	       && GET_MODE_INNER (GET_MODE (op)) == GET_MODE (XEXP (op, 0)))
33718 	/* This catches V2DF and V2DI splat, at a minimum.  */
33719 	return 1;
33720       else if (GET_CODE (XEXP (op, 0)) == VEC_SELECT)
33721 	/* If the duplicated item is from a select, defer to the select
33722 	   processing to see if we can change the lane for the splat.  */
33723 	return rtx_is_swappable_p (XEXP (op, 0), special);
33724       else
33725 	return 0;
33726 
33727     case VEC_SELECT:
33728       /* A vec_extract operation is ok if we change the lane.  */
33729       if (GET_CODE (XEXP (op, 0)) == REG
33730 	  && GET_MODE_INNER (GET_MODE (XEXP (op, 0))) == GET_MODE (op)
33731 	  && GET_CODE ((parallel = XEXP (op, 1))) == PARALLEL
33732 	  && XVECLEN (parallel, 0) == 1
33733 	  && GET_CODE (XVECEXP (parallel, 0, 0)) == CONST_INT)
33734 	{
33735 	  *special = SH_EXTRACT;
33736 	  return 1;
33737 	}
33738       else
33739 	return 0;
33740 
33741     case UNSPEC:
33742       {
33743 	/* Various operations are unsafe for this optimization, at least
33744 	   without significant additional work.  Permutes are obviously
33745 	   problematic, as both the permute control vector and the ordering
33746 	   of the target values are invalidated by doubleword swapping.
33747 	   Vector pack and unpack modify the number of vector lanes.
33748 	   Merge-high/low will not operate correctly on swapped operands.
33749 	   Vector shifts across element boundaries are clearly uncool,
33750 	   as are vector select and concatenate operations.  Vector
33751 	   sum-across instructions define one operand with a specific
33752 	   order-dependent element, so additional fixup code would be
33753 	   needed to make those work.  Vector set and non-immediate-form
33754 	   vector splat are element-order sensitive.  A few of these
33755 	   cases might be workable with special handling if required.
33756 	   Adding cost modeling would be appropriate in some cases.  */
33757 	int val = XINT (op, 1);
33758 	switch (val)
33759 	  {
33760 	  default:
33761 	    break;
33762 	  case UNSPEC_VMRGH_DIRECT:
33763 	  case UNSPEC_VMRGL_DIRECT:
33764 	  case UNSPEC_VPACK_SIGN_SIGN_SAT:
33765 	  case UNSPEC_VPACK_SIGN_UNS_SAT:
33766 	  case UNSPEC_VPACK_UNS_UNS_MOD:
33767 	  case UNSPEC_VPACK_UNS_UNS_MOD_DIRECT:
33768 	  case UNSPEC_VPACK_UNS_UNS_SAT:
33769 	  case UNSPEC_VPERM:
33770 	  case UNSPEC_VPERM_UNS:
33771 	  case UNSPEC_VPERMHI:
33772 	  case UNSPEC_VPERMSI:
33773 	  case UNSPEC_VPKPX:
33774 	  case UNSPEC_VSLDOI:
33775 	  case UNSPEC_VSLO:
33776 	  case UNSPEC_VSRO:
33777 	  case UNSPEC_VSUM2SWS:
33778 	  case UNSPEC_VSUM4S:
33779 	  case UNSPEC_VSUM4UBS:
33780 	  case UNSPEC_VSUMSWS:
33781 	  case UNSPEC_VSUMSWS_DIRECT:
33782 	  case UNSPEC_VSX_CONCAT:
33783 	  case UNSPEC_VSX_SET:
33784 	  case UNSPEC_VSX_SLDWI:
33785 	  case UNSPEC_VUNPACK_HI_SIGN:
33786 	  case UNSPEC_VUNPACK_HI_SIGN_DIRECT:
33787 	  case UNSPEC_VUNPACK_LO_SIGN:
33788 	  case UNSPEC_VUNPACK_LO_SIGN_DIRECT:
33789 	  case UNSPEC_VUPKHPX:
33790 	  case UNSPEC_VUPKHS_V4SF:
33791 	  case UNSPEC_VUPKHU_V4SF:
33792 	  case UNSPEC_VUPKLPX:
33793 	  case UNSPEC_VUPKLS_V4SF:
33794 	  case UNSPEC_VUPKLU_V4SF:
33795 	  case UNSPEC_VSX_CVDPSPN:
33796 	  case UNSPEC_VSX_CVSPDP:
33797 	  case UNSPEC_VSX_CVSPDPN:
33798 	    return 0;
33799 	  case UNSPEC_VSPLT_DIRECT:
33800 	    *special = SH_SPLAT;
33801 	    return 1;
33802 	  }
33803       }
33804 
33805     default:
33806       break;
33807     }
33808 
33809   const char *fmt = GET_RTX_FORMAT (code);
33810   int ok = 1;
33811 
33812   for (i = 0; i < GET_RTX_LENGTH (code); ++i)
33813     if (fmt[i] == 'e' || fmt[i] == 'u')
33814       {
33815 	unsigned int special_op = SH_NONE;
33816 	ok &= rtx_is_swappable_p (XEXP (op, i), &special_op);
33817 	if (special_op == SH_NONE)
33818 	  continue;
33819 	/* Ensure we never have two kinds of special handling
33820 	   for the same insn.  */
33821 	if (*special != SH_NONE && *special != special_op)
33822 	  return 0;
33823 	*special = special_op;
33824       }
33825     else if (fmt[i] == 'E')
33826       for (j = 0; j < XVECLEN (op, i); ++j)
33827 	{
33828 	  unsigned int special_op = SH_NONE;
33829 	  ok &= rtx_is_swappable_p (XVECEXP (op, i, j), &special_op);
33830 	if (special_op == SH_NONE)
33831 	  continue;
33832 	  /* Ensure we never have two kinds of special handling
33833 	     for the same insn.  */
33834 	  if (*special != SH_NONE && *special != special_op)
33835 	    return 0;
33836 	  *special = special_op;
33837 	}
33838 
33839   return ok;
33840 }
33841 
33842 /* Return 1 iff INSN is an operand that will not be affected by
33843    having vector doublewords swapped in memory (in which case
33844    *SPECIAL is unchanged), or that can be modified to be correct
33845    if vector doublewords are swapped in memory (in which case
33846    *SPECIAL is changed to a value indicating how).  */
33847 static unsigned int
insn_is_swappable_p(swap_web_entry * insn_entry,rtx insn,unsigned int * special)33848 insn_is_swappable_p (swap_web_entry *insn_entry, rtx insn,
33849 		     unsigned int *special)
33850 {
33851   /* Calls are always bad.  */
33852   if (GET_CODE (insn) == CALL_INSN)
33853     return 0;
33854 
33855   /* Loads and stores seen here are not permuting, but we can still
33856      fix them up by converting them to permuting ones.  Exceptions:
33857      UNSPEC_LVE, UNSPEC_LVX, and UNSPEC_STVX, which have a PARALLEL
33858      body instead of a SET; and UNSPEC_STVE, which has an UNSPEC
33859      for the SET source.  */
33860   rtx body = PATTERN (insn);
33861   int i = INSN_UID (insn);
33862 
33863   if (insn_entry[i].is_load)
33864     {
33865       if (GET_CODE (body) == SET)
33866 	{
33867 	  *special = SH_NOSWAP_LD;
33868 	  return 1;
33869 	}
33870       else
33871 	return 0;
33872     }
33873 
33874   if (insn_entry[i].is_store)
33875     {
33876       if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) != UNSPEC)
33877 	{
33878 	  *special = SH_NOSWAP_ST;
33879 	  return 1;
33880 	}
33881       else
33882 	return 0;
33883     }
33884 
33885   /* A convert to single precision can be left as is provided that
33886      all of its uses are in xxspltw instructions that splat BE element
33887      zero.  */
33888   if (GET_CODE (body) == SET
33889       && GET_CODE (SET_SRC (body)) == UNSPEC
33890       && XINT (SET_SRC (body), 1) == UNSPEC_VSX_CVDPSPN)
33891     {
33892       df_ref *def_rec;
33893 
33894       for (def_rec = DF_INSN_UID_DEFS (i); *def_rec; def_rec++)
33895 	{
33896 	  df_ref def = *def_rec;
33897 	  struct df_link *link = DF_REF_CHAIN (def);
33898 	  if (!link)
33899 	    return 0;
33900 
33901 	  for (; link; link = link->next) {
33902 	    rtx use_insn = DF_REF_INSN (link->ref);
33903 	    rtx use_body = PATTERN (use_insn);
33904 	    if (GET_CODE (use_body) != SET
33905 		|| GET_CODE (SET_SRC (use_body)) != UNSPEC
33906 		|| XINT (SET_SRC (use_body), 1) != UNSPEC_VSX_XXSPLTW
33907 		|| XEXP (XEXP (SET_SRC (use_body), 0), 1) != const0_rtx)
33908 	      return 0;
33909 	  }
33910 	}
33911 
33912       return 1;
33913     }
33914 
33915   /* Otherwise check the operands for vector lane violations.  */
33916   return rtx_is_swappable_p (body, special);
33917 }
33918 
33919 enum chain_purpose { FOR_LOADS, FOR_STORES };
33920 
33921 /* Return true if the UD or DU chain headed by LINK is non-empty,
33922    and every entry on the chain references an insn that is a
33923    register swap.  Furthermore, if PURPOSE is FOR_LOADS, each such
33924    register swap must have only permuting loads as reaching defs.
33925    If PURPOSE is FOR_STORES, each such register swap must have only
33926    register swaps or permuting stores as reached uses.  */
33927 static bool
chain_contains_only_swaps(swap_web_entry * insn_entry,struct df_link * link,enum chain_purpose purpose)33928 chain_contains_only_swaps (swap_web_entry *insn_entry, struct df_link *link,
33929 			   enum chain_purpose purpose)
33930 {
33931   if (!link)
33932     return false;
33933 
33934   for (; link; link = link->next)
33935     {
33936       if (!VECTOR_MODE_P (GET_MODE (DF_REF_REG (link->ref))))
33937 	continue;
33938 
33939       if (DF_REF_IS_ARTIFICIAL (link->ref))
33940 	return false;
33941 
33942       rtx reached_insn = DF_REF_INSN (link->ref);
33943       unsigned uid = INSN_UID (reached_insn);
33944 
33945       if (!insn_entry[uid].is_swap || insn_entry[uid].is_load
33946 	  || insn_entry[uid].is_store)
33947 	return false;
33948 
33949       if (purpose == FOR_LOADS)
33950 	{
33951 	  df_ref *use_rec;
33952 	  for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
33953 	    {
33954 	      df_ref use = *use_rec;
33955 	      struct df_link *swap_link = DF_REF_CHAIN (use);
33956 
33957 	      while (swap_link)
33958 		{
33959 		  if (DF_REF_IS_ARTIFICIAL (link->ref))
33960 		    return false;
33961 
33962 		  rtx swap_def_insn = DF_REF_INSN (swap_link->ref);
33963 		  unsigned uid2 = INSN_UID (swap_def_insn);
33964 
33965 		  /* Only permuting loads are allowed.  */
33966 		  if (!insn_entry[uid2].is_swap || !insn_entry[uid2].is_load)
33967 		    return false;
33968 
33969 		  swap_link = swap_link->next;
33970 		}
33971 	    }
33972 	}
33973       else if (purpose == FOR_STORES)
33974 	{
33975 	  df_ref *def_rec;
33976 	  for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
33977 	    {
33978 	      df_ref def = *def_rec;
33979 	      struct df_link *swap_link = DF_REF_CHAIN (def);
33980 
33981 	      while (swap_link)
33982 		{
33983 		  if (DF_REF_IS_ARTIFICIAL (link->ref))
33984 		    return false;
33985 
33986 		  rtx swap_use_insn = DF_REF_INSN (swap_link->ref);
33987 		  unsigned uid2 = INSN_UID (swap_use_insn);
33988 
33989 		  /* Permuting stores or register swaps are allowed.  */
33990 		  if (!insn_entry[uid2].is_swap || insn_entry[uid2].is_load)
33991 		    return false;
33992 
33993 		  swap_link = swap_link->next;
33994 		}
33995 	    }
33996 	}
33997     }
33998 
33999   return true;
34000 }
34001 
34002 /* Mark the xxswapdi instructions associated with permuting loads and
34003    stores for removal.  Note that we only flag them for deletion here,
34004    as there is a possibility of a swap being reached from multiple
34005    loads, etc.  */
34006 static void
mark_swaps_for_removal(swap_web_entry * insn_entry,unsigned int i)34007 mark_swaps_for_removal (swap_web_entry *insn_entry, unsigned int i)
34008 {
34009   rtx insn = insn_entry[i].insn;
34010   unsigned uid = INSN_UID (insn);
34011 
34012   if (insn_entry[i].is_load)
34013     {
34014       df_ref *def_rec;
34015       for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
34016 	{
34017 	  df_ref def = *def_rec;
34018 	  struct df_link *link = DF_REF_CHAIN (def);
34019 
34020 	  /* We know by now that these are swaps, so we can delete
34021 	     them confidently.  */
34022 	  while (link)
34023 	    {
34024 	      rtx use_insn = DF_REF_INSN (link->ref);
34025 	      insn_entry[INSN_UID (use_insn)].will_delete = 1;
34026 	      link = link->next;
34027 	    }
34028 	}
34029     }
34030   else if (insn_entry[i].is_store)
34031     {
34032       df_ref *use_rec;
34033       for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
34034 	{
34035 	  df_ref use = *use_rec;
34036 	  /* Ignore uses for addressability.  */
34037 	  machine_mode mode = GET_MODE (DF_REF_REG (use));
34038 	  if (!VECTOR_MODE_P (mode))
34039 	    continue;
34040 
34041 	  struct df_link *link = DF_REF_CHAIN (use);
34042 
34043 	  /* We know by now that these are swaps, so we can delete
34044 	     them confidently.  */
34045 	  while (link)
34046 	    {
34047 	      rtx def_insn = DF_REF_INSN (link->ref);
34048 	      insn_entry[INSN_UID (def_insn)].will_delete = 1;
34049 	      link = link->next;
34050 	    }
34051 	}
34052     }
34053 }
34054 
34055 /* OP is either a CONST_VECTOR or an expression containing one.
34056    Swap the first half of the vector with the second in the first
34057    case.  Recurse to find it in the second.  */
34058 static void
swap_const_vector_halves(rtx op)34059 swap_const_vector_halves (rtx op)
34060 {
34061   int i;
34062   enum rtx_code code = GET_CODE (op);
34063   if (GET_CODE (op) == CONST_VECTOR)
34064     {
34065       int half_units = GET_MODE_NUNITS (GET_MODE (op)) / 2;
34066       for (i = 0; i < half_units; ++i)
34067 	{
34068 	  rtx temp = CONST_VECTOR_ELT (op, i);
34069 	  CONST_VECTOR_ELT (op, i) = CONST_VECTOR_ELT (op, i + half_units);
34070 	  CONST_VECTOR_ELT (op, i + half_units) = temp;
34071 	}
34072     }
34073   else
34074     {
34075       int j;
34076       const char *fmt = GET_RTX_FORMAT (code);
34077       for (i = 0; i < GET_RTX_LENGTH (code); ++i)
34078 	if (fmt[i] == 'e' || fmt[i] == 'u')
34079 	  swap_const_vector_halves (XEXP (op, i));
34080 	else if (fmt[i] == 'E')
34081 	  for (j = 0; j < XVECLEN (op, i); ++j)
34082 	    swap_const_vector_halves (XVECEXP (op, i, j));
34083     }
34084 }
34085 
34086 /* Find all subregs of a vector expression that perform a narrowing,
34087    and adjust the subreg index to account for doubleword swapping.  */
34088 static void
adjust_subreg_index(rtx op)34089 adjust_subreg_index (rtx op)
34090 {
34091   enum rtx_code code = GET_CODE (op);
34092   if (code == SUBREG
34093       && (GET_MODE_SIZE (GET_MODE (op))
34094 	  < GET_MODE_SIZE (GET_MODE (XEXP (op, 0)))))
34095     {
34096       unsigned int index = SUBREG_BYTE (op);
34097       if (index < 8)
34098 	index += 8;
34099       else
34100 	index -= 8;
34101       SUBREG_BYTE (op) = index;
34102     }
34103 
34104   const char *fmt = GET_RTX_FORMAT (code);
34105   int i,j;
34106   for (i = 0; i < GET_RTX_LENGTH (code); ++i)
34107     if (fmt[i] == 'e' || fmt[i] == 'u')
34108       adjust_subreg_index (XEXP (op, i));
34109     else if (fmt[i] == 'E')
34110       for (j = 0; j < XVECLEN (op, i); ++j)
34111 	adjust_subreg_index (XVECEXP (op, i, j));
34112 }
34113 
34114 /* Convert the non-permuting load INSN to a permuting one.  */
34115 static void
permute_load(rtx insn)34116 permute_load (rtx insn)
34117 {
34118   rtx body = PATTERN (insn);
34119   rtx mem_op = SET_SRC (body);
34120   rtx tgt_reg = SET_DEST (body);
34121   machine_mode mode = GET_MODE (tgt_reg);
34122   int n_elts = GET_MODE_NUNITS (mode);
34123   int half_elts = n_elts / 2;
34124   rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
34125   int i, j;
34126   for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
34127     XVECEXP (par, 0, i) = GEN_INT (j);
34128   for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
34129     XVECEXP (par, 0, i) = GEN_INT (j);
34130   rtx sel = gen_rtx_VEC_SELECT (mode, mem_op, par);
34131   SET_SRC (body) = sel;
34132   INSN_CODE (insn) = -1; /* Force re-recognition.  */
34133   df_insn_rescan (insn);
34134 
34135   if (dump_file)
34136     fprintf (dump_file, "Replacing load %d with permuted load\n",
34137 	     INSN_UID (insn));
34138 }
34139 
34140 /* Convert the non-permuting store INSN to a permuting one.  */
34141 static void
permute_store(rtx insn)34142 permute_store (rtx insn)
34143 {
34144   rtx body = PATTERN (insn);
34145   rtx src_reg = SET_SRC (body);
34146   machine_mode mode = GET_MODE (src_reg);
34147   int n_elts = GET_MODE_NUNITS (mode);
34148   int half_elts = n_elts / 2;
34149   rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
34150   int i, j;
34151   for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
34152     XVECEXP (par, 0, i) = GEN_INT (j);
34153   for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
34154     XVECEXP (par, 0, i) = GEN_INT (j);
34155   rtx sel = gen_rtx_VEC_SELECT (mode, src_reg, par);
34156   SET_SRC (body) = sel;
34157   INSN_CODE (insn) = -1; /* Force re-recognition.  */
34158   df_insn_rescan (insn);
34159 
34160   if (dump_file)
34161     fprintf (dump_file, "Replacing store %d with permuted store\n",
34162 	     INSN_UID (insn));
34163 }
34164 
34165 /* Given OP that contains a vector extract operation, adjust the index
34166    of the extracted lane to account for the doubleword swap.  */
34167 static void
adjust_extract(rtx insn)34168 adjust_extract (rtx insn)
34169 {
34170   rtx pattern = PATTERN (insn);
34171   if (GET_CODE (pattern) == PARALLEL)
34172     pattern = XVECEXP (pattern, 0, 0);
34173   rtx src = SET_SRC (pattern);
34174   /* The vec_select may be wrapped in a vec_duplicate for a splat, so
34175      account for that.  */
34176   rtx sel = GET_CODE (src) == VEC_DUPLICATE ? XEXP (src, 0) : src;
34177   rtx par = XEXP (sel, 1);
34178   int half_elts = GET_MODE_NUNITS (GET_MODE (XEXP (sel, 0))) >> 1;
34179   int lane = INTVAL (XVECEXP (par, 0, 0));
34180   lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
34181   XVECEXP (par, 0, 0) = GEN_INT (lane);
34182   INSN_CODE (insn) = -1; /* Force re-recognition.  */
34183   df_insn_rescan (insn);
34184 
34185   if (dump_file)
34186     fprintf (dump_file, "Changing lane for extract %d\n", INSN_UID (insn));
34187 }
34188 
34189 /* Given OP that contains a vector direct-splat operation, adjust the index
34190    of the source lane to account for the doubleword swap.  */
34191 static void
adjust_splat(rtx insn)34192 adjust_splat (rtx insn)
34193 {
34194   rtx body = PATTERN (insn);
34195   rtx unspec = XEXP (body, 1);
34196   int half_elts = GET_MODE_NUNITS (GET_MODE (unspec)) >> 1;
34197   int lane = INTVAL (XVECEXP (unspec, 0, 1));
34198   lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
34199   XVECEXP (unspec, 0, 1) = GEN_INT (lane);
34200   INSN_CODE (insn) = -1; /* Force re-recognition.  */
34201   df_insn_rescan (insn);
34202 
34203   if (dump_file)
34204     fprintf (dump_file, "Changing lane for splat %d\n", INSN_UID (insn));
34205 }
34206 
34207 /* The insn described by INSN_ENTRY[I] can be swapped, but only
34208    with special handling.  Take care of that here.  */
34209 static void
handle_special_swappables(swap_web_entry * insn_entry,unsigned i)34210 handle_special_swappables (swap_web_entry *insn_entry, unsigned i)
34211 {
34212   rtx insn = insn_entry[i].insn;
34213   rtx body = PATTERN (insn);
34214 
34215   switch (insn_entry[i].special_handling)
34216     {
34217     default:
34218       gcc_unreachable ();
34219     case SH_CONST_VECTOR:
34220       {
34221 	/* A CONST_VECTOR will only show up somewhere in the RHS of a SET.  */
34222 	gcc_assert (GET_CODE (body) == SET);
34223 	rtx rhs = SET_SRC (body);
34224 	swap_const_vector_halves (rhs);
34225 	if (dump_file)
34226 	  fprintf (dump_file, "Swapping constant halves in insn %d\n", i);
34227 	break;
34228       }
34229     case SH_SUBREG:
34230       /* A subreg of the same size is already safe.  For subregs that
34231 	 select a smaller portion of a reg, adjust the index for
34232 	 swapped doublewords.  */
34233       adjust_subreg_index (body);
34234       if (dump_file)
34235 	fprintf (dump_file, "Adjusting subreg in insn %d\n", i);
34236       break;
34237     case SH_NOSWAP_LD:
34238       /* Convert a non-permuting load to a permuting one.  */
34239       permute_load (insn);
34240       break;
34241     case SH_NOSWAP_ST:
34242       /* Convert a non-permuting store to a permuting one.  */
34243       permute_store (insn);
34244       break;
34245     case SH_EXTRACT:
34246       /* Change the lane on an extract operation.  */
34247       adjust_extract (insn);
34248       break;
34249     case SH_SPLAT:
34250       /* Change the lane on a direct-splat operation.  */
34251       adjust_splat (insn);
34252       break;
34253     }
34254 }
34255 
34256 /* Find the insn from the Ith table entry, which is known to be a
34257    register swap Y = SWAP(X).  Replace it with a copy Y = X.  */
34258 static void
replace_swap_with_copy(swap_web_entry * insn_entry,unsigned i)34259 replace_swap_with_copy (swap_web_entry *insn_entry, unsigned i)
34260 {
34261   rtx insn = insn_entry[i].insn;
34262   rtx body = PATTERN (insn);
34263   rtx src_reg = XEXP (SET_SRC (body), 0);
34264   rtx copy = gen_rtx_SET (VOIDmode, SET_DEST (body), src_reg);
34265   rtx new_insn = emit_insn_before (copy, insn);
34266   set_block_for_insn (new_insn, BLOCK_FOR_INSN (insn));
34267   df_insn_rescan (new_insn);
34268 
34269   if (dump_file)
34270     {
34271       unsigned int new_uid = INSN_UID (new_insn);
34272       fprintf (dump_file, "Replacing swap %d with copy %d\n", i, new_uid);
34273     }
34274 
34275   df_insn_delete (BLOCK_FOR_INSN (insn), INSN_UID (insn));
34276   remove_insn (insn);
34277   INSN_DELETED_P (insn) = 1;
34278 }
34279 
34280 /* Dump the swap table to DUMP_FILE.  */
34281 static void
dump_swap_insn_table(swap_web_entry * insn_entry)34282 dump_swap_insn_table (swap_web_entry *insn_entry)
34283 {
34284   int e = get_max_uid ();
34285   fprintf (dump_file, "\nRelevant insns with their flag settings\n\n");
34286 
34287   for (int i = 0; i < e; ++i)
34288     if (insn_entry[i].is_relevant)
34289       {
34290 	swap_web_entry *pred_entry = (swap_web_entry *)insn_entry[i].pred ();
34291 	fprintf (dump_file, "%6d %6d  ", i,
34292 		 pred_entry && pred_entry->insn
34293 		 ? INSN_UID (pred_entry->insn) : 0);
34294 	if (insn_entry[i].is_load)
34295 	  fputs ("load ", dump_file);
34296 	if (insn_entry[i].is_store)
34297 	  fputs ("store ", dump_file);
34298 	if (insn_entry[i].is_swap)
34299 	  fputs ("swap ", dump_file);
34300 	if (insn_entry[i].is_live_in)
34301 	  fputs ("live-in ", dump_file);
34302 	if (insn_entry[i].is_live_out)
34303 	  fputs ("live-out ", dump_file);
34304 	if (insn_entry[i].contains_subreg)
34305 	  fputs ("subreg ", dump_file);
34306 	if (insn_entry[i].is_128_int)
34307 	  fputs ("int128 ", dump_file);
34308 	if (insn_entry[i].is_call)
34309 	  fputs ("call ", dump_file);
34310 	if (insn_entry[i].is_swappable)
34311 	  {
34312 	    fputs ("swappable ", dump_file);
34313 	    if (insn_entry[i].special_handling == SH_CONST_VECTOR)
34314 	      fputs ("special:constvec ", dump_file);
34315 	    else if (insn_entry[i].special_handling == SH_SUBREG)
34316 	      fputs ("special:subreg ", dump_file);
34317 	    else if (insn_entry[i].special_handling == SH_NOSWAP_LD)
34318 	      fputs ("special:load ", dump_file);
34319 	    else if (insn_entry[i].special_handling == SH_NOSWAP_ST)
34320 	      fputs ("special:store ", dump_file);
34321 	    else if (insn_entry[i].special_handling == SH_EXTRACT)
34322 	      fputs ("special:extract ", dump_file);
34323 	    else if (insn_entry[i].special_handling == SH_SPLAT)
34324 	      fputs ("special:splat ", dump_file);
34325 	  }
34326 	if (insn_entry[i].web_not_optimizable)
34327 	  fputs ("unoptimizable ", dump_file);
34328 	if (insn_entry[i].will_delete)
34329 	  fputs ("delete ", dump_file);
34330 	fputs ("\n", dump_file);
34331       }
34332   fputs ("\n", dump_file);
34333 }
34334 
34335 /* Main entry point for this pass.  */
34336 unsigned int
rs6000_analyze_swaps(function * fun)34337 rs6000_analyze_swaps (function *fun)
34338 {
34339   swap_web_entry *insn_entry;
34340   basic_block bb;
34341   rtx insn;
34342 
34343   /* Dataflow analysis for use-def chains.  */
34344   df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
34345   df_chain_add_problem (DF_DU_CHAIN | DF_UD_CHAIN);
34346   df_analyze ();
34347   df_set_flags (DF_DEFER_INSN_RESCAN);
34348 
34349   /* Allocate structure to represent webs of insns.  */
34350   insn_entry = XCNEWVEC (swap_web_entry, get_max_uid ());
34351 
34352   /* Walk the insns to gather basic data.  */
34353   FOR_ALL_BB_FN (bb, fun)
34354     FOR_BB_INSNS (bb, insn)
34355     {
34356       unsigned int uid = INSN_UID (insn);
34357       if (NONDEBUG_INSN_P (insn))
34358 	{
34359 	  insn_entry[uid].insn = insn;
34360 
34361 	  if (GET_CODE (insn) == CALL_INSN)
34362 	    insn_entry[uid].is_call = 1;
34363 
34364 	  /* Walk the uses and defs to see if we mention vector regs.
34365 	     Record any constraints on optimization of such mentions.  */
34366 	  df_ref *use_rec;
34367 	  for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
34368 	    {
34369 	      df_ref mention = *use_rec;
34370 	      /* We use DF_REF_REAL_REG here to get inside any subregs.  */
34371 	      machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
34372 
34373 	      /* If a use gets its value from a call insn, it will be
34374 		 a hard register and will look like (reg:V4SI 3 3).
34375 		 The df analysis creates two mentions for GPR3 and GPR4,
34376 		 both DImode.  We must recognize this and treat it as a
34377 		 vector mention to ensure the call is unioned with this
34378 		 use.  */
34379 	      if (mode == DImode && DF_REF_INSN_INFO (mention))
34380 		{
34381 		  rtx feeder = DF_REF_INSN (mention);
34382 		  /* FIXME:  It is pretty hard to get from the df mention
34383 		     to the mode of the use in the insn.  We arbitrarily
34384 		     pick a vector mode here, even though the use might
34385 		     be a real DImode.  We can be too conservative
34386 		     (create a web larger than necessary) because of
34387 		     this, so consider eventually fixing this.  */
34388 		  if (GET_CODE (feeder) == CALL_INSN)
34389 		    mode = V4SImode;
34390 		}
34391 
34392 	      if (VECTOR_MODE_P (mode) || mode == TImode)
34393 		{
34394 		  insn_entry[uid].is_relevant = 1;
34395 		  if (mode == TImode || mode == V1TImode)
34396 		    insn_entry[uid].is_128_int = 1;
34397 		  if (DF_REF_INSN_INFO (mention))
34398 		    insn_entry[uid].contains_subreg
34399 		      = !rtx_equal_p (DF_REF_REG (mention),
34400 				      DF_REF_REAL_REG (mention));
34401 		  union_defs (insn_entry, insn, mention);
34402 		}
34403 	    }
34404 	  df_ref *def_rec;
34405 	  for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
34406 	    {
34407 	      df_ref mention = *def_rec;
34408 	      /* We use DF_REF_REAL_REG here to get inside any subregs.  */
34409 	      machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
34410 
34411 	      /* If we're loading up a hard vector register for a call,
34412 		 it looks like (set (reg:V4SI 9 9) (...)).  The df
34413 		 analysis creates two mentions for GPR9 and GPR10, both
34414 		 DImode.  So relying on the mode from the mentions
34415 		 isn't sufficient to ensure we union the call into the
34416 		 web with the parameter setup code.  */
34417 	      if (mode == DImode && GET_CODE (insn) == SET
34418 		  && VECTOR_MODE_P (GET_MODE (SET_DEST (insn))))
34419 		mode = GET_MODE (SET_DEST (insn));
34420 
34421 	      if (VECTOR_MODE_P (mode) || mode == TImode)
34422 		{
34423 		  insn_entry[uid].is_relevant = 1;
34424 		  if (mode == TImode || mode == V1TImode)
34425 		    insn_entry[uid].is_128_int = 1;
34426 		  if (DF_REF_INSN_INFO (mention))
34427 		    insn_entry[uid].contains_subreg
34428 		      = !rtx_equal_p (DF_REF_REG (mention),
34429 				      DF_REF_REAL_REG (mention));
34430 		  /* REG_FUNCTION_VALUE_P is not valid for subregs. */
34431 		  else if (REG_FUNCTION_VALUE_P (DF_REF_REG (mention)))
34432 		    insn_entry[uid].is_live_out = 1;
34433 		  union_uses (insn_entry, insn, mention);
34434 		}
34435 	    }
34436 
34437 	  if (insn_entry[uid].is_relevant)
34438 	    {
34439 	      /* Determine if this is a load or store.  */
34440 	      insn_entry[uid].is_load = insn_is_load_p (insn);
34441 	      insn_entry[uid].is_store = insn_is_store_p (insn);
34442 
34443 	      /* Determine if this is a doubleword swap.  If not,
34444 		 determine whether it can legally be swapped.  */
34445 	      if (insn_is_swap_p (insn))
34446 		insn_entry[uid].is_swap = 1;
34447 	      else
34448 		{
34449 		  unsigned int special = SH_NONE;
34450 		  insn_entry[uid].is_swappable
34451 		    = insn_is_swappable_p (insn_entry, insn, &special);
34452 		  if (special != SH_NONE && insn_entry[uid].contains_subreg)
34453 		    insn_entry[uid].is_swappable = 0;
34454 		  else if (special != SH_NONE)
34455 		    insn_entry[uid].special_handling = special;
34456 		  else if (insn_entry[uid].contains_subreg)
34457 		    insn_entry[uid].special_handling = SH_SUBREG;
34458 		}
34459 	    }
34460 	}
34461     }
34462 
34463   if (dump_file)
34464     {
34465       fprintf (dump_file, "\nSwap insn entry table when first built\n");
34466       dump_swap_insn_table (insn_entry);
34467     }
34468 
34469   /* Record unoptimizable webs.  */
34470   unsigned e = get_max_uid (), i;
34471   for (i = 0; i < e; ++i)
34472     {
34473       if (!insn_entry[i].is_relevant)
34474 	continue;
34475 
34476       swap_web_entry *root
34477 	= (swap_web_entry*)(&insn_entry[i])->unionfind_root ();
34478       unsigned uid = INSN_UID (insn_entry[i].insn);
34479 
34480       if (insn_entry[i].is_live_in || insn_entry[i].is_live_out
34481 	  || (insn_entry[i].contains_subreg
34482 	      && insn_entry[i].special_handling != SH_SUBREG)
34483 	  || insn_entry[i].is_128_int || insn_entry[i].is_call
34484 	  || !(insn_entry[i].is_swappable || insn_entry[i].is_swap))
34485 	root->web_not_optimizable = 1;
34486 
34487       /* If we have loads or stores that aren't permuting then the
34488 	 optimization isn't appropriate.  */
34489       else if ((insn_entry[i].is_load || insn_entry[i].is_store)
34490 	  && !insn_entry[i].is_swap && !insn_entry[i].is_swappable)
34491 	root->web_not_optimizable = 1;
34492 
34493       /* If we have permuting loads or stores that are not accompanied
34494 	 by a register swap, the optimization isn't appropriate.  */
34495       else if (insn_entry[i].is_load && insn_entry[i].is_swap)
34496 	{
34497 	  df_ref *def_rec;
34498 
34499 	  for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
34500 	    {
34501 	      df_ref def = *def_rec;
34502 	      struct df_link *link = DF_REF_CHAIN (def);
34503 
34504 	      if (!chain_contains_only_swaps (insn_entry, link, FOR_LOADS))
34505 		{
34506 		  root->web_not_optimizable = 1;
34507 		  break;
34508 		}
34509 	    }
34510 	}
34511       else if (insn_entry[i].is_store && insn_entry[i].is_swap)
34512 	{
34513 	  df_ref *use_rec;
34514 
34515 	  for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
34516 	    {
34517 	      df_ref use = *use_rec;
34518 	      struct df_link *link = DF_REF_CHAIN (use);
34519 
34520 	      if (!chain_contains_only_swaps (insn_entry, link, FOR_STORES))
34521 		{
34522 		  root->web_not_optimizable = 1;
34523 		  break;
34524 		}
34525 	    }
34526 	}
34527     }
34528 
34529   if (dump_file)
34530     {
34531       fprintf (dump_file, "\nSwap insn entry table after web analysis\n");
34532       dump_swap_insn_table (insn_entry);
34533     }
34534 
34535   /* For each load and store in an optimizable web (which implies
34536      the loads and stores are permuting), find the associated
34537      register swaps and mark them for removal.  Due to various
34538      optimizations we may mark the same swap more than once.  Also
34539      perform special handling for swappable insns that require it.  */
34540   for (i = 0; i < e; ++i)
34541     if ((insn_entry[i].is_load || insn_entry[i].is_store)
34542 	&& insn_entry[i].is_swap)
34543       {
34544 	swap_web_entry* root_entry
34545 	  = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
34546 	if (!root_entry->web_not_optimizable)
34547 	  mark_swaps_for_removal (insn_entry, i);
34548       }
34549     else if (insn_entry[i].is_swappable && insn_entry[i].special_handling)
34550       {
34551 	swap_web_entry* root_entry
34552 	  = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
34553 	if (!root_entry->web_not_optimizable)
34554 	  handle_special_swappables (insn_entry, i);
34555       }
34556 
34557   /* Now delete the swaps marked for removal.  */
34558   for (i = 0; i < e; ++i)
34559     if (insn_entry[i].will_delete)
34560       replace_swap_with_copy (insn_entry, i);
34561 
34562   /* Clean up.  */
34563   free (insn_entry);
34564   return 0;
34565 }
34566 
34567 
34568 struct gcc_target targetm = TARGET_INITIALIZER;
34569 
34570 #include "gt-rs6000.h"
34571