2017-12-01 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / config / powerpcspe / powerpcspe.c
blobb5fc656a8f1d9bd279b6faade023e784c70bdb29
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2017 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "memmodel.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "cfgloop.h"
31 #include "df.h"
32 #include "tm_p.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "expmed.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "ira.h"
39 #include "recog.h"
40 #include "cgraph.h"
41 #include "diagnostic-core.h"
42 #include "insn-attr.h"
43 #include "flags.h"
44 #include "alias.h"
45 #include "fold-const.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "print-tree.h"
49 #include "varasm.h"
50 #include "explow.h"
51 #include "expr.h"
52 #include "output.h"
53 #include "dbxout.h"
54 #include "common/common-target.h"
55 #include "langhooks.h"
56 #include "reload.h"
57 #include "sched-int.h"
58 #include "gimplify.h"
59 #include "gimple-fold.h"
60 #include "gimple-iterator.h"
61 #include "gimple-ssa.h"
62 #include "gimple-walk.h"
63 #include "intl.h"
64 #include "params.h"
65 #include "tm-constrs.h"
66 #include "tree-vectorizer.h"
67 #include "target-globals.h"
68 #include "builtins.h"
69 #include "context.h"
70 #include "tree-pass.h"
71 #include "except.h"
72 #if TARGET_XCOFF
73 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
74 #endif
75 #if TARGET_MACHO
76 #include "gstab.h" /* for N_SLINE */
77 #endif
78 #include "case-cfn-macros.h"
79 #include "ppc-auxv.h"
81 /* This file should be included last. */
82 #include "target-def.h"
84 #ifndef TARGET_NO_PROTOTYPE
85 #define TARGET_NO_PROTOTYPE 0
86 #endif
88 #define min(A,B) ((A) < (B) ? (A) : (B))
89 #define max(A,B) ((A) > (B) ? (A) : (B))
91 static pad_direction rs6000_function_arg_padding (machine_mode, const_tree);
93 /* Structure used to define the rs6000 stack */
94 typedef struct rs6000_stack {
95 int reload_completed; /* stack info won't change from here on */
96 int first_gp_reg_save; /* first callee saved GP register used */
97 int first_fp_reg_save; /* first callee saved FP register used */
98 int first_altivec_reg_save; /* first callee saved AltiVec register used */
99 int lr_save_p; /* true if the link reg needs to be saved */
100 int cr_save_p; /* true if the CR reg needs to be saved */
101 unsigned int vrsave_mask; /* mask of vec registers to save */
102 int push_p; /* true if we need to allocate stack space */
103 int calls_p; /* true if the function makes any calls */
104 int world_save_p; /* true if we're saving *everything*:
105 r13-r31, cr, f14-f31, vrsave, v20-v31 */
106 enum rs6000_abi abi; /* which ABI to use */
107 int gp_save_offset; /* offset to save GP regs from initial SP */
108 int fp_save_offset; /* offset to save FP regs from initial SP */
109 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
110 int lr_save_offset; /* offset to save LR from initial SP */
111 int cr_save_offset; /* offset to save CR from initial SP */
112 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
113 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
114 int varargs_save_offset; /* offset to save the varargs registers */
115 int ehrd_offset; /* offset to EH return data */
116 int ehcr_offset; /* offset to EH CR field data */
117 int reg_size; /* register size (4 or 8) */
118 HOST_WIDE_INT vars_size; /* variable save area size */
119 int parm_size; /* outgoing parameter size */
120 int save_size; /* save area size */
121 int fixed_size; /* fixed size of stack frame */
122 int gp_size; /* size of saved GP registers */
123 int fp_size; /* size of saved FP registers */
124 int altivec_size; /* size of saved AltiVec registers */
125 int cr_size; /* size to hold CR if not in fixed area */
126 int vrsave_size; /* size to hold VRSAVE */
127 int altivec_padding_size; /* size of altivec alignment padding */
128 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
129 int spe_padding_size;
130 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
131 int spe_64bit_regs_used;
132 int savres_strategy;
133 } rs6000_stack_t;
135 /* A C structure for machine-specific, per-function data.
136 This is added to the cfun structure. */
137 typedef struct GTY(()) machine_function
139 /* Whether the instruction chain has been scanned already. */
140 int spe_insn_chain_scanned_p;
141 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
142 int ra_needs_full_frame;
143 /* Flags if __builtin_return_address (0) was used. */
144 int ra_need_lr;
145 /* Cache lr_save_p after expansion of builtin_eh_return. */
146 int lr_save_state;
147 /* Whether we need to save the TOC to the reserved stack location in the
148 function prologue. */
149 bool save_toc_in_prologue;
150 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
151 varargs save area. */
152 HOST_WIDE_INT varargs_save_offset;
153 /* Temporary stack slot to use for SDmode copies. This slot is
154 64-bits wide and is allocated early enough so that the offset
155 does not overflow the 16-bit load/store offset field. */
156 rtx sdmode_stack_slot;
157 /* Alternative internal arg pointer for -fsplit-stack. */
158 rtx split_stack_arg_pointer;
159 bool split_stack_argp_used;
160 /* Flag if r2 setup is needed with ELFv2 ABI. */
161 bool r2_setup_needed;
162 /* The number of components we use for separate shrink-wrapping. */
163 int n_components;
164 /* The components already handled by separate shrink-wrapping, which should
165 not be considered by the prologue and epilogue. */
166 bool gpr_is_wrapped_separately[32];
167 bool fpr_is_wrapped_separately[32];
168 bool lr_is_wrapped_separately;
169 } machine_function;
171 /* Support targetm.vectorize.builtin_mask_for_load. */
172 static GTY(()) tree altivec_builtin_mask_for_load;
174 /* Set to nonzero once AIX common-mode calls have been defined. */
175 static GTY(()) int common_mode_defined;
177 /* Label number of label created for -mrelocatable, to call to so we can
178 get the address of the GOT section */
179 static int rs6000_pic_labelno;
181 #ifdef USING_ELFOS_H
182 /* Counter for labels which are to be placed in .fixup. */
183 int fixuplabelno = 0;
184 #endif
186 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
187 int dot_symbols;
189 /* Specify the machine mode that pointers have. After generation of rtl, the
190 compiler makes no further distinction between pointers and any other objects
191 of this machine mode. */
192 scalar_int_mode rs6000_pmode;
194 /* Width in bits of a pointer. */
195 unsigned rs6000_pointer_size;
197 #ifdef HAVE_AS_GNU_ATTRIBUTE
198 # ifndef HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE
199 # define HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE 0
200 # endif
201 /* Flag whether floating point values have been passed/returned.
202 Note that this doesn't say whether fprs are used, since the
203 Tag_GNU_Power_ABI_FP .gnu.attributes value this flag controls
204 should be set for soft-float values passed in gprs and ieee128
205 values passed in vsx registers. */
206 static bool rs6000_passes_float;
207 static bool rs6000_passes_long_double;
208 /* Flag whether vector values have been passed/returned. */
209 static bool rs6000_passes_vector;
210 /* Flag whether small (<= 8 byte) structures have been returned. */
211 static bool rs6000_returns_struct;
212 #endif
214 /* Value is TRUE if register/mode pair is acceptable. */
215 static bool rs6000_hard_regno_mode_ok_p
216 [NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
218 /* Maximum number of registers needed for a given register class and mode. */
219 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
221 /* How many registers are needed for a given register and mode. */
222 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
224 /* Map register number to register class. */
225 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
227 static int dbg_cost_ctrl;
229 /* Built in types. */
230 tree rs6000_builtin_types[RS6000_BTI_MAX];
231 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
233 /* Flag to say the TOC is initialized */
234 int toc_initialized, need_toc_init;
235 char toc_label_name[10];
237 /* Cached value of rs6000_variable_issue. This is cached in
238 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
239 static short cached_can_issue_more;
241 static GTY(()) section *read_only_data_section;
242 static GTY(()) section *private_data_section;
243 static GTY(()) section *tls_data_section;
244 static GTY(()) section *tls_private_data_section;
245 static GTY(()) section *read_only_private_data_section;
246 static GTY(()) section *sdata2_section;
247 static GTY(()) section *toc_section;
249 struct builtin_description
251 const HOST_WIDE_INT mask;
252 const enum insn_code icode;
253 const char *const name;
254 const enum rs6000_builtins code;
257 /* Describe the vector unit used for modes. */
258 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
259 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
261 /* Register classes for various constraints that are based on the target
262 switches. */
263 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
265 /* Describe the alignment of a vector. */
266 int rs6000_vector_align[NUM_MACHINE_MODES];
268 /* Map selected modes to types for builtins. */
269 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
271 /* What modes to automatically generate reciprocal divide estimate (fre) and
272 reciprocal sqrt (frsqrte) for. */
273 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
275 /* Masks to determine which reciprocal esitmate instructions to generate
276 automatically. */
277 enum rs6000_recip_mask {
278 RECIP_SF_DIV = 0x001, /* Use divide estimate */
279 RECIP_DF_DIV = 0x002,
280 RECIP_V4SF_DIV = 0x004,
281 RECIP_V2DF_DIV = 0x008,
283 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
284 RECIP_DF_RSQRT = 0x020,
285 RECIP_V4SF_RSQRT = 0x040,
286 RECIP_V2DF_RSQRT = 0x080,
288 /* Various combination of flags for -mrecip=xxx. */
289 RECIP_NONE = 0,
290 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
291 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
292 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
294 RECIP_HIGH_PRECISION = RECIP_ALL,
296 /* On low precision machines like the power5, don't enable double precision
297 reciprocal square root estimate, since it isn't accurate enough. */
298 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
301 /* -mrecip options. */
302 static struct
304 const char *string; /* option name */
305 unsigned int mask; /* mask bits to set */
306 } recip_options[] = {
307 { "all", RECIP_ALL },
308 { "none", RECIP_NONE },
309 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
310 | RECIP_V2DF_DIV) },
311 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
312 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
313 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
314 | RECIP_V2DF_RSQRT) },
315 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
316 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
319 /* Used by __builtin_cpu_is(), mapping from PLATFORM names to values. */
320 static const struct
322 const char *cpu;
323 unsigned int cpuid;
324 } cpu_is_info[] = {
325 { "power9", PPC_PLATFORM_POWER9 },
326 { "power8", PPC_PLATFORM_POWER8 },
327 { "power7", PPC_PLATFORM_POWER7 },
328 { "power6x", PPC_PLATFORM_POWER6X },
329 { "power6", PPC_PLATFORM_POWER6 },
330 { "power5+", PPC_PLATFORM_POWER5_PLUS },
331 { "power5", PPC_PLATFORM_POWER5 },
332 { "ppc970", PPC_PLATFORM_PPC970 },
333 { "power4", PPC_PLATFORM_POWER4 },
334 { "ppca2", PPC_PLATFORM_PPCA2 },
335 { "ppc476", PPC_PLATFORM_PPC476 },
336 { "ppc464", PPC_PLATFORM_PPC464 },
337 { "ppc440", PPC_PLATFORM_PPC440 },
338 { "ppc405", PPC_PLATFORM_PPC405 },
339 { "ppc-cell-be", PPC_PLATFORM_CELL_BE }
342 /* Used by __builtin_cpu_supports(), mapping from HWCAP names to masks. */
343 static const struct
345 const char *hwcap;
346 int mask;
347 unsigned int id;
348 } cpu_supports_info[] = {
349 /* AT_HWCAP masks. */
350 { "4xxmac", PPC_FEATURE_HAS_4xxMAC, 0 },
351 { "altivec", PPC_FEATURE_HAS_ALTIVEC, 0 },
352 { "arch_2_05", PPC_FEATURE_ARCH_2_05, 0 },
353 { "arch_2_06", PPC_FEATURE_ARCH_2_06, 0 },
354 { "archpmu", PPC_FEATURE_PERFMON_COMPAT, 0 },
355 { "booke", PPC_FEATURE_BOOKE, 0 },
356 { "cellbe", PPC_FEATURE_CELL_BE, 0 },
357 { "dfp", PPC_FEATURE_HAS_DFP, 0 },
358 { "efpdouble", PPC_FEATURE_HAS_EFP_DOUBLE, 0 },
359 { "efpsingle", PPC_FEATURE_HAS_EFP_SINGLE, 0 },
360 { "fpu", PPC_FEATURE_HAS_FPU, 0 },
361 { "ic_snoop", PPC_FEATURE_ICACHE_SNOOP, 0 },
362 { "mmu", PPC_FEATURE_HAS_MMU, 0 },
363 { "notb", PPC_FEATURE_NO_TB, 0 },
364 { "pa6t", PPC_FEATURE_PA6T, 0 },
365 { "power4", PPC_FEATURE_POWER4, 0 },
366 { "power5", PPC_FEATURE_POWER5, 0 },
367 { "power5+", PPC_FEATURE_POWER5_PLUS, 0 },
368 { "power6x", PPC_FEATURE_POWER6_EXT, 0 },
369 { "ppc32", PPC_FEATURE_32, 0 },
370 { "ppc601", PPC_FEATURE_601_INSTR, 0 },
371 { "ppc64", PPC_FEATURE_64, 0 },
372 { "ppcle", PPC_FEATURE_PPC_LE, 0 },
373 { "smt", PPC_FEATURE_SMT, 0 },
374 { "spe", PPC_FEATURE_HAS_SPE, 0 },
375 { "true_le", PPC_FEATURE_TRUE_LE, 0 },
376 { "ucache", PPC_FEATURE_UNIFIED_CACHE, 0 },
377 { "vsx", PPC_FEATURE_HAS_VSX, 0 },
379 /* AT_HWCAP2 masks. */
380 { "arch_2_07", PPC_FEATURE2_ARCH_2_07, 1 },
381 { "dscr", PPC_FEATURE2_HAS_DSCR, 1 },
382 { "ebb", PPC_FEATURE2_HAS_EBB, 1 },
383 { "htm", PPC_FEATURE2_HAS_HTM, 1 },
384 { "htm-nosc", PPC_FEATURE2_HTM_NOSC, 1 },
385 { "isel", PPC_FEATURE2_HAS_ISEL, 1 },
386 { "tar", PPC_FEATURE2_HAS_TAR, 1 },
387 { "vcrypto", PPC_FEATURE2_HAS_VEC_CRYPTO, 1 },
388 { "arch_3_00", PPC_FEATURE2_ARCH_3_00, 1 },
389 { "ieee128", PPC_FEATURE2_HAS_IEEE128, 1 }
392 /* Newer LIBCs explicitly export this symbol to declare that they provide
393 the AT_PLATFORM and AT_HWCAP/AT_HWCAP2 values in the TCB. We emit a
394 reference to this symbol whenever we expand a CPU builtin, so that
395 we never link against an old LIBC. */
396 const char *tcb_verification_symbol = "__parse_hwcap_and_convert_at_platform";
398 /* True if we have expanded a CPU builtin. */
399 bool cpu_builtin_p;
401 /* Pointer to function (in powerpcspe-c.c) that can define or undefine target
402 macros that have changed. Languages that don't support the preprocessor
403 don't link in powerpcspe-c.c, so we can't call it directly. */
404 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
406 /* Simplfy register classes into simpler classifications. We assume
407 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
408 check for standard register classes (gpr/floating/altivec/vsx) and
409 floating/vector classes (float/altivec/vsx). */
411 enum rs6000_reg_type {
412 NO_REG_TYPE,
413 PSEUDO_REG_TYPE,
414 GPR_REG_TYPE,
415 VSX_REG_TYPE,
416 ALTIVEC_REG_TYPE,
417 FPR_REG_TYPE,
418 SPR_REG_TYPE,
419 CR_REG_TYPE,
420 SPE_ACC_TYPE,
421 SPEFSCR_REG_TYPE
424 /* Map register class to register type. */
425 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
427 /* First/last register type for the 'normal' register types (i.e. general
428 purpose, floating point, altivec, and VSX registers). */
429 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
431 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
434 /* Register classes we care about in secondary reload or go if legitimate
435 address. We only need to worry about GPR, FPR, and Altivec registers here,
436 along an ANY field that is the OR of the 3 register classes. */
438 enum rs6000_reload_reg_type {
439 RELOAD_REG_GPR, /* General purpose registers. */
440 RELOAD_REG_FPR, /* Traditional floating point regs. */
441 RELOAD_REG_VMX, /* Altivec (VMX) registers. */
442 RELOAD_REG_ANY, /* OR of GPR, FPR, Altivec masks. */
443 N_RELOAD_REG
446 /* For setting up register classes, loop through the 3 register classes mapping
447 into real registers, and skip the ANY class, which is just an OR of the
448 bits. */
449 #define FIRST_RELOAD_REG_CLASS RELOAD_REG_GPR
450 #define LAST_RELOAD_REG_CLASS RELOAD_REG_VMX
452 /* Map reload register type to a register in the register class. */
453 struct reload_reg_map_type {
454 const char *name; /* Register class name. */
455 int reg; /* Register in the register class. */
458 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
459 { "Gpr", FIRST_GPR_REGNO }, /* RELOAD_REG_GPR. */
460 { "Fpr", FIRST_FPR_REGNO }, /* RELOAD_REG_FPR. */
461 { "VMX", FIRST_ALTIVEC_REGNO }, /* RELOAD_REG_VMX. */
462 { "Any", -1 }, /* RELOAD_REG_ANY. */
465 /* Mask bits for each register class, indexed per mode. Historically the
466 compiler has been more restrictive which types can do PRE_MODIFY instead of
467 PRE_INC and PRE_DEC, so keep track of sepaate bits for these two. */
468 typedef unsigned char addr_mask_type;
470 #define RELOAD_REG_VALID 0x01 /* Mode valid in register.. */
471 #define RELOAD_REG_MULTIPLE 0x02 /* Mode takes multiple registers. */
472 #define RELOAD_REG_INDEXED 0x04 /* Reg+reg addressing. */
473 #define RELOAD_REG_OFFSET 0x08 /* Reg+offset addressing. */
474 #define RELOAD_REG_PRE_INCDEC 0x10 /* PRE_INC/PRE_DEC valid. */
475 #define RELOAD_REG_PRE_MODIFY 0x20 /* PRE_MODIFY valid. */
476 #define RELOAD_REG_AND_M16 0x40 /* AND -16 addressing. */
477 #define RELOAD_REG_QUAD_OFFSET 0x80 /* quad offset is limited. */
479 /* Register type masks based on the type, of valid addressing modes. */
480 struct rs6000_reg_addr {
481 enum insn_code reload_load; /* INSN to reload for loading. */
482 enum insn_code reload_store; /* INSN to reload for storing. */
483 enum insn_code reload_fpr_gpr; /* INSN to move from FPR to GPR. */
484 enum insn_code reload_gpr_vsx; /* INSN to move from GPR to VSX. */
485 enum insn_code reload_vsx_gpr; /* INSN to move from VSX to GPR. */
486 enum insn_code fusion_gpr_ld; /* INSN for fusing gpr ADDIS/loads. */
487 /* INSNs for fusing addi with loads
488 or stores for each reg. class. */
489 enum insn_code fusion_addi_ld[(int)N_RELOAD_REG];
490 enum insn_code fusion_addi_st[(int)N_RELOAD_REG];
491 /* INSNs for fusing addis with loads
492 or stores for each reg. class. */
493 enum insn_code fusion_addis_ld[(int)N_RELOAD_REG];
494 enum insn_code fusion_addis_st[(int)N_RELOAD_REG];
495 addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks. */
496 bool scalar_in_vmx_p; /* Scalar value can go in VMX. */
497 bool fused_toc; /* Mode supports TOC fusion. */
500 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
502 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC. */
503 static inline bool
504 mode_supports_pre_incdec_p (machine_mode mode)
506 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
507 != 0);
510 /* Helper function to say whether a mode supports PRE_MODIFY. */
511 static inline bool
512 mode_supports_pre_modify_p (machine_mode mode)
514 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
515 != 0);
518 /* Given that there exists at least one variable that is set (produced)
519 by OUT_INSN and read (consumed) by IN_INSN, return true iff
520 IN_INSN represents one or more memory store operations and none of
521 the variables set by OUT_INSN is used by IN_INSN as the address of a
522 store operation. If either IN_INSN or OUT_INSN does not represent
523 a "single" RTL SET expression (as loosely defined by the
524 implementation of the single_set function) or a PARALLEL with only
525 SETs, CLOBBERs, and USEs inside, this function returns false.
527 This rs6000-specific version of store_data_bypass_p checks for
528 certain conditions that result in assertion failures (and internal
529 compiler errors) in the generic store_data_bypass_p function and
530 returns false rather than calling store_data_bypass_p if one of the
531 problematic conditions is detected. */
534 rs6000_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
536 rtx out_set, in_set;
537 rtx out_pat, in_pat;
538 rtx out_exp, in_exp;
539 int i, j;
541 in_set = single_set (in_insn);
542 if (in_set)
544 if (MEM_P (SET_DEST (in_set)))
546 out_set = single_set (out_insn);
547 if (!out_set)
549 out_pat = PATTERN (out_insn);
550 if (GET_CODE (out_pat) == PARALLEL)
552 for (i = 0; i < XVECLEN (out_pat, 0); i++)
554 out_exp = XVECEXP (out_pat, 0, i);
555 if ((GET_CODE (out_exp) == CLOBBER)
556 || (GET_CODE (out_exp) == USE))
557 continue;
558 else if (GET_CODE (out_exp) != SET)
559 return false;
565 else
567 in_pat = PATTERN (in_insn);
568 if (GET_CODE (in_pat) != PARALLEL)
569 return false;
571 for (i = 0; i < XVECLEN (in_pat, 0); i++)
573 in_exp = XVECEXP (in_pat, 0, i);
574 if ((GET_CODE (in_exp) == CLOBBER) || (GET_CODE (in_exp) == USE))
575 continue;
576 else if (GET_CODE (in_exp) != SET)
577 return false;
579 if (MEM_P (SET_DEST (in_exp)))
581 out_set = single_set (out_insn);
582 if (!out_set)
584 out_pat = PATTERN (out_insn);
585 if (GET_CODE (out_pat) != PARALLEL)
586 return false;
587 for (j = 0; j < XVECLEN (out_pat, 0); j++)
589 out_exp = XVECEXP (out_pat, 0, j);
590 if ((GET_CODE (out_exp) == CLOBBER)
591 || (GET_CODE (out_exp) == USE))
592 continue;
593 else if (GET_CODE (out_exp) != SET)
594 return false;
600 return store_data_bypass_p (out_insn, in_insn);
603 /* Return true if we have D-form addressing in altivec registers. */
604 static inline bool
605 mode_supports_vmx_dform (machine_mode mode)
607 return ((reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_OFFSET) != 0);
610 /* Return true if we have D-form addressing in VSX registers. This addressing
611 is more limited than normal d-form addressing in that the offset must be
612 aligned on a 16-byte boundary. */
613 static inline bool
614 mode_supports_vsx_dform_quad (machine_mode mode)
616 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_QUAD_OFFSET)
617 != 0);
621 /* Target cpu costs. */
623 struct processor_costs {
624 const int mulsi; /* cost of SImode multiplication. */
625 const int mulsi_const; /* cost of SImode multiplication by constant. */
626 const int mulsi_const9; /* cost of SImode mult by short constant. */
627 const int muldi; /* cost of DImode multiplication. */
628 const int divsi; /* cost of SImode division. */
629 const int divdi; /* cost of DImode division. */
630 const int fp; /* cost of simple SFmode and DFmode insns. */
631 const int dmul; /* cost of DFmode multiplication (and fmadd). */
632 const int sdiv; /* cost of SFmode division (fdivs). */
633 const int ddiv; /* cost of DFmode division (fdiv). */
634 const int cache_line_size; /* cache line size in bytes. */
635 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
636 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
637 const int simultaneous_prefetches; /* number of parallel prefetch
638 operations. */
639 const int sfdf_convert; /* cost of SF->DF conversion. */
642 const struct processor_costs *rs6000_cost;
644 /* Processor costs (relative to an add) */
646 /* Instruction size costs on 32bit processors. */
647 static const
648 struct processor_costs size32_cost = {
649 COSTS_N_INSNS (1), /* mulsi */
650 COSTS_N_INSNS (1), /* mulsi_const */
651 COSTS_N_INSNS (1), /* mulsi_const9 */
652 COSTS_N_INSNS (1), /* muldi */
653 COSTS_N_INSNS (1), /* divsi */
654 COSTS_N_INSNS (1), /* divdi */
655 COSTS_N_INSNS (1), /* fp */
656 COSTS_N_INSNS (1), /* dmul */
657 COSTS_N_INSNS (1), /* sdiv */
658 COSTS_N_INSNS (1), /* ddiv */
659 32, /* cache line size */
660 0, /* l1 cache */
661 0, /* l2 cache */
662 0, /* streams */
663 0, /* SF->DF convert */
666 /* Instruction size costs on 64bit processors. */
667 static const
668 struct processor_costs size64_cost = {
669 COSTS_N_INSNS (1), /* mulsi */
670 COSTS_N_INSNS (1), /* mulsi_const */
671 COSTS_N_INSNS (1), /* mulsi_const9 */
672 COSTS_N_INSNS (1), /* muldi */
673 COSTS_N_INSNS (1), /* divsi */
674 COSTS_N_INSNS (1), /* divdi */
675 COSTS_N_INSNS (1), /* fp */
676 COSTS_N_INSNS (1), /* dmul */
677 COSTS_N_INSNS (1), /* sdiv */
678 COSTS_N_INSNS (1), /* ddiv */
679 128, /* cache line size */
680 0, /* l1 cache */
681 0, /* l2 cache */
682 0, /* streams */
683 0, /* SF->DF convert */
686 /* Instruction costs on RS64A processors. */
687 static const
688 struct processor_costs rs64a_cost = {
689 COSTS_N_INSNS (20), /* mulsi */
690 COSTS_N_INSNS (12), /* mulsi_const */
691 COSTS_N_INSNS (8), /* mulsi_const9 */
692 COSTS_N_INSNS (34), /* muldi */
693 COSTS_N_INSNS (65), /* divsi */
694 COSTS_N_INSNS (67), /* divdi */
695 COSTS_N_INSNS (4), /* fp */
696 COSTS_N_INSNS (4), /* dmul */
697 COSTS_N_INSNS (31), /* sdiv */
698 COSTS_N_INSNS (31), /* ddiv */
699 128, /* cache line size */
700 128, /* l1 cache */
701 2048, /* l2 cache */
702 1, /* streams */
703 0, /* SF->DF convert */
706 /* Instruction costs on MPCCORE processors. */
707 static const
708 struct processor_costs mpccore_cost = {
709 COSTS_N_INSNS (2), /* mulsi */
710 COSTS_N_INSNS (2), /* mulsi_const */
711 COSTS_N_INSNS (2), /* mulsi_const9 */
712 COSTS_N_INSNS (2), /* muldi */
713 COSTS_N_INSNS (6), /* divsi */
714 COSTS_N_INSNS (6), /* divdi */
715 COSTS_N_INSNS (4), /* fp */
716 COSTS_N_INSNS (5), /* dmul */
717 COSTS_N_INSNS (10), /* sdiv */
718 COSTS_N_INSNS (17), /* ddiv */
719 32, /* cache line size */
720 4, /* l1 cache */
721 16, /* l2 cache */
722 1, /* streams */
723 0, /* SF->DF convert */
726 /* Instruction costs on PPC403 processors. */
727 static const
728 struct processor_costs ppc403_cost = {
729 COSTS_N_INSNS (4), /* mulsi */
730 COSTS_N_INSNS (4), /* mulsi_const */
731 COSTS_N_INSNS (4), /* mulsi_const9 */
732 COSTS_N_INSNS (4), /* muldi */
733 COSTS_N_INSNS (33), /* divsi */
734 COSTS_N_INSNS (33), /* divdi */
735 COSTS_N_INSNS (11), /* fp */
736 COSTS_N_INSNS (11), /* dmul */
737 COSTS_N_INSNS (11), /* sdiv */
738 COSTS_N_INSNS (11), /* ddiv */
739 32, /* cache line size */
740 4, /* l1 cache */
741 16, /* l2 cache */
742 1, /* streams */
743 0, /* SF->DF convert */
746 /* Instruction costs on PPC405 processors. */
747 static const
748 struct processor_costs ppc405_cost = {
749 COSTS_N_INSNS (5), /* mulsi */
750 COSTS_N_INSNS (4), /* mulsi_const */
751 COSTS_N_INSNS (3), /* mulsi_const9 */
752 COSTS_N_INSNS (5), /* muldi */
753 COSTS_N_INSNS (35), /* divsi */
754 COSTS_N_INSNS (35), /* divdi */
755 COSTS_N_INSNS (11), /* fp */
756 COSTS_N_INSNS (11), /* dmul */
757 COSTS_N_INSNS (11), /* sdiv */
758 COSTS_N_INSNS (11), /* ddiv */
759 32, /* cache line size */
760 16, /* l1 cache */
761 128, /* l2 cache */
762 1, /* streams */
763 0, /* SF->DF convert */
766 /* Instruction costs on PPC440 processors. */
767 static const
768 struct processor_costs ppc440_cost = {
769 COSTS_N_INSNS (3), /* mulsi */
770 COSTS_N_INSNS (2), /* mulsi_const */
771 COSTS_N_INSNS (2), /* mulsi_const9 */
772 COSTS_N_INSNS (3), /* muldi */
773 COSTS_N_INSNS (34), /* divsi */
774 COSTS_N_INSNS (34), /* divdi */
775 COSTS_N_INSNS (5), /* fp */
776 COSTS_N_INSNS (5), /* dmul */
777 COSTS_N_INSNS (19), /* sdiv */
778 COSTS_N_INSNS (33), /* ddiv */
779 32, /* cache line size */
780 32, /* l1 cache */
781 256, /* l2 cache */
782 1, /* streams */
783 0, /* SF->DF convert */
786 /* Instruction costs on PPC476 processors. */
787 static const
788 struct processor_costs ppc476_cost = {
789 COSTS_N_INSNS (4), /* mulsi */
790 COSTS_N_INSNS (4), /* mulsi_const */
791 COSTS_N_INSNS (4), /* mulsi_const9 */
792 COSTS_N_INSNS (4), /* muldi */
793 COSTS_N_INSNS (11), /* divsi */
794 COSTS_N_INSNS (11), /* divdi */
795 COSTS_N_INSNS (6), /* fp */
796 COSTS_N_INSNS (6), /* dmul */
797 COSTS_N_INSNS (19), /* sdiv */
798 COSTS_N_INSNS (33), /* ddiv */
799 32, /* l1 cache line size */
800 32, /* l1 cache */
801 512, /* l2 cache */
802 1, /* streams */
803 0, /* SF->DF convert */
806 /* Instruction costs on PPC601 processors. */
807 static const
808 struct processor_costs ppc601_cost = {
809 COSTS_N_INSNS (5), /* mulsi */
810 COSTS_N_INSNS (5), /* mulsi_const */
811 COSTS_N_INSNS (5), /* mulsi_const9 */
812 COSTS_N_INSNS (5), /* muldi */
813 COSTS_N_INSNS (36), /* divsi */
814 COSTS_N_INSNS (36), /* divdi */
815 COSTS_N_INSNS (4), /* fp */
816 COSTS_N_INSNS (5), /* dmul */
817 COSTS_N_INSNS (17), /* sdiv */
818 COSTS_N_INSNS (31), /* ddiv */
819 32, /* cache line size */
820 32, /* l1 cache */
821 256, /* l2 cache */
822 1, /* streams */
823 0, /* SF->DF convert */
826 /* Instruction costs on PPC603 processors. */
827 static const
828 struct processor_costs ppc603_cost = {
829 COSTS_N_INSNS (5), /* mulsi */
830 COSTS_N_INSNS (3), /* mulsi_const */
831 COSTS_N_INSNS (2), /* mulsi_const9 */
832 COSTS_N_INSNS (5), /* muldi */
833 COSTS_N_INSNS (37), /* divsi */
834 COSTS_N_INSNS (37), /* divdi */
835 COSTS_N_INSNS (3), /* fp */
836 COSTS_N_INSNS (4), /* dmul */
837 COSTS_N_INSNS (18), /* sdiv */
838 COSTS_N_INSNS (33), /* ddiv */
839 32, /* cache line size */
840 8, /* l1 cache */
841 64, /* l2 cache */
842 1, /* streams */
843 0, /* SF->DF convert */
846 /* Instruction costs on PPC604 processors. */
847 static const
848 struct processor_costs ppc604_cost = {
849 COSTS_N_INSNS (4), /* mulsi */
850 COSTS_N_INSNS (4), /* mulsi_const */
851 COSTS_N_INSNS (4), /* mulsi_const9 */
852 COSTS_N_INSNS (4), /* muldi */
853 COSTS_N_INSNS (20), /* divsi */
854 COSTS_N_INSNS (20), /* divdi */
855 COSTS_N_INSNS (3), /* fp */
856 COSTS_N_INSNS (3), /* dmul */
857 COSTS_N_INSNS (18), /* sdiv */
858 COSTS_N_INSNS (32), /* ddiv */
859 32, /* cache line size */
860 16, /* l1 cache */
861 512, /* l2 cache */
862 1, /* streams */
863 0, /* SF->DF convert */
866 /* Instruction costs on PPC604e processors. */
867 static const
868 struct processor_costs ppc604e_cost = {
869 COSTS_N_INSNS (2), /* mulsi */
870 COSTS_N_INSNS (2), /* mulsi_const */
871 COSTS_N_INSNS (2), /* mulsi_const9 */
872 COSTS_N_INSNS (2), /* muldi */
873 COSTS_N_INSNS (20), /* divsi */
874 COSTS_N_INSNS (20), /* divdi */
875 COSTS_N_INSNS (3), /* fp */
876 COSTS_N_INSNS (3), /* dmul */
877 COSTS_N_INSNS (18), /* sdiv */
878 COSTS_N_INSNS (32), /* ddiv */
879 32, /* cache line size */
880 32, /* l1 cache */
881 1024, /* l2 cache */
882 1, /* streams */
883 0, /* SF->DF convert */
886 /* Instruction costs on PPC620 processors. */
887 static const
888 struct processor_costs ppc620_cost = {
889 COSTS_N_INSNS (5), /* mulsi */
890 COSTS_N_INSNS (4), /* mulsi_const */
891 COSTS_N_INSNS (3), /* mulsi_const9 */
892 COSTS_N_INSNS (7), /* muldi */
893 COSTS_N_INSNS (21), /* divsi */
894 COSTS_N_INSNS (37), /* divdi */
895 COSTS_N_INSNS (3), /* fp */
896 COSTS_N_INSNS (3), /* dmul */
897 COSTS_N_INSNS (18), /* sdiv */
898 COSTS_N_INSNS (32), /* ddiv */
899 128, /* cache line size */
900 32, /* l1 cache */
901 1024, /* l2 cache */
902 1, /* streams */
903 0, /* SF->DF convert */
906 /* Instruction costs on PPC630 processors. */
907 static const
908 struct processor_costs ppc630_cost = {
909 COSTS_N_INSNS (5), /* mulsi */
910 COSTS_N_INSNS (4), /* mulsi_const */
911 COSTS_N_INSNS (3), /* mulsi_const9 */
912 COSTS_N_INSNS (7), /* muldi */
913 COSTS_N_INSNS (21), /* divsi */
914 COSTS_N_INSNS (37), /* divdi */
915 COSTS_N_INSNS (3), /* fp */
916 COSTS_N_INSNS (3), /* dmul */
917 COSTS_N_INSNS (17), /* sdiv */
918 COSTS_N_INSNS (21), /* ddiv */
919 128, /* cache line size */
920 64, /* l1 cache */
921 1024, /* l2 cache */
922 1, /* streams */
923 0, /* SF->DF convert */
926 /* Instruction costs on Cell processor. */
927 /* COSTS_N_INSNS (1) ~ one add. */
928 static const
929 struct processor_costs ppccell_cost = {
930 COSTS_N_INSNS (9/2)+2, /* mulsi */
931 COSTS_N_INSNS (6/2), /* mulsi_const */
932 COSTS_N_INSNS (6/2), /* mulsi_const9 */
933 COSTS_N_INSNS (15/2)+2, /* muldi */
934 COSTS_N_INSNS (38/2), /* divsi */
935 COSTS_N_INSNS (70/2), /* divdi */
936 COSTS_N_INSNS (10/2), /* fp */
937 COSTS_N_INSNS (10/2), /* dmul */
938 COSTS_N_INSNS (74/2), /* sdiv */
939 COSTS_N_INSNS (74/2), /* ddiv */
940 128, /* cache line size */
941 32, /* l1 cache */
942 512, /* l2 cache */
943 6, /* streams */
944 0, /* SF->DF convert */
947 /* Instruction costs on PPC750 and PPC7400 processors. */
948 static const
949 struct processor_costs ppc750_cost = {
950 COSTS_N_INSNS (5), /* mulsi */
951 COSTS_N_INSNS (3), /* mulsi_const */
952 COSTS_N_INSNS (2), /* mulsi_const9 */
953 COSTS_N_INSNS (5), /* muldi */
954 COSTS_N_INSNS (17), /* divsi */
955 COSTS_N_INSNS (17), /* divdi */
956 COSTS_N_INSNS (3), /* fp */
957 COSTS_N_INSNS (3), /* dmul */
958 COSTS_N_INSNS (17), /* sdiv */
959 COSTS_N_INSNS (31), /* ddiv */
960 32, /* cache line size */
961 32, /* l1 cache */
962 512, /* l2 cache */
963 1, /* streams */
964 0, /* SF->DF convert */
967 /* Instruction costs on PPC7450 processors. */
968 static const
969 struct processor_costs ppc7450_cost = {
970 COSTS_N_INSNS (4), /* mulsi */
971 COSTS_N_INSNS (3), /* mulsi_const */
972 COSTS_N_INSNS (3), /* mulsi_const9 */
973 COSTS_N_INSNS (4), /* muldi */
974 COSTS_N_INSNS (23), /* divsi */
975 COSTS_N_INSNS (23), /* divdi */
976 COSTS_N_INSNS (5), /* fp */
977 COSTS_N_INSNS (5), /* dmul */
978 COSTS_N_INSNS (21), /* sdiv */
979 COSTS_N_INSNS (35), /* ddiv */
980 32, /* cache line size */
981 32, /* l1 cache */
982 1024, /* l2 cache */
983 1, /* streams */
984 0, /* SF->DF convert */
987 /* Instruction costs on PPC8540 processors. */
988 static const
989 struct processor_costs ppc8540_cost = {
990 COSTS_N_INSNS (4), /* mulsi */
991 COSTS_N_INSNS (4), /* mulsi_const */
992 COSTS_N_INSNS (4), /* mulsi_const9 */
993 COSTS_N_INSNS (4), /* muldi */
994 COSTS_N_INSNS (19), /* divsi */
995 COSTS_N_INSNS (19), /* divdi */
996 COSTS_N_INSNS (4), /* fp */
997 COSTS_N_INSNS (4), /* dmul */
998 COSTS_N_INSNS (29), /* sdiv */
999 COSTS_N_INSNS (29), /* ddiv */
1000 32, /* cache line size */
1001 32, /* l1 cache */
1002 256, /* l2 cache */
1003 1, /* prefetch streams /*/
1004 0, /* SF->DF convert */
1007 /* Instruction costs on E300C2 and E300C3 cores. */
1008 static const
1009 struct processor_costs ppce300c2c3_cost = {
1010 COSTS_N_INSNS (4), /* mulsi */
1011 COSTS_N_INSNS (4), /* mulsi_const */
1012 COSTS_N_INSNS (4), /* mulsi_const9 */
1013 COSTS_N_INSNS (4), /* muldi */
1014 COSTS_N_INSNS (19), /* divsi */
1015 COSTS_N_INSNS (19), /* divdi */
1016 COSTS_N_INSNS (3), /* fp */
1017 COSTS_N_INSNS (4), /* dmul */
1018 COSTS_N_INSNS (18), /* sdiv */
1019 COSTS_N_INSNS (33), /* ddiv */
1021 16, /* l1 cache */
1022 16, /* l2 cache */
1023 1, /* prefetch streams /*/
1024 0, /* SF->DF convert */
1027 /* Instruction costs on PPCE500MC processors. */
1028 static const
1029 struct processor_costs ppce500mc_cost = {
1030 COSTS_N_INSNS (4), /* mulsi */
1031 COSTS_N_INSNS (4), /* mulsi_const */
1032 COSTS_N_INSNS (4), /* mulsi_const9 */
1033 COSTS_N_INSNS (4), /* muldi */
1034 COSTS_N_INSNS (14), /* divsi */
1035 COSTS_N_INSNS (14), /* divdi */
1036 COSTS_N_INSNS (8), /* fp */
1037 COSTS_N_INSNS (10), /* dmul */
1038 COSTS_N_INSNS (36), /* sdiv */
1039 COSTS_N_INSNS (66), /* ddiv */
1040 64, /* cache line size */
1041 32, /* l1 cache */
1042 128, /* l2 cache */
1043 1, /* prefetch streams /*/
1044 0, /* SF->DF convert */
1047 /* Instruction costs on PPCE500MC64 processors. */
1048 static const
1049 struct processor_costs ppce500mc64_cost = {
1050 COSTS_N_INSNS (4), /* mulsi */
1051 COSTS_N_INSNS (4), /* mulsi_const */
1052 COSTS_N_INSNS (4), /* mulsi_const9 */
1053 COSTS_N_INSNS (4), /* muldi */
1054 COSTS_N_INSNS (14), /* divsi */
1055 COSTS_N_INSNS (14), /* divdi */
1056 COSTS_N_INSNS (4), /* fp */
1057 COSTS_N_INSNS (10), /* dmul */
1058 COSTS_N_INSNS (36), /* sdiv */
1059 COSTS_N_INSNS (66), /* ddiv */
1060 64, /* cache line size */
1061 32, /* l1 cache */
1062 128, /* l2 cache */
1063 1, /* prefetch streams /*/
1064 0, /* SF->DF convert */
1067 /* Instruction costs on PPCE5500 processors. */
1068 static const
1069 struct processor_costs ppce5500_cost = {
1070 COSTS_N_INSNS (5), /* mulsi */
1071 COSTS_N_INSNS (5), /* mulsi_const */
1072 COSTS_N_INSNS (4), /* mulsi_const9 */
1073 COSTS_N_INSNS (5), /* muldi */
1074 COSTS_N_INSNS (14), /* divsi */
1075 COSTS_N_INSNS (14), /* divdi */
1076 COSTS_N_INSNS (7), /* fp */
1077 COSTS_N_INSNS (10), /* dmul */
1078 COSTS_N_INSNS (36), /* sdiv */
1079 COSTS_N_INSNS (66), /* ddiv */
1080 64, /* cache line size */
1081 32, /* l1 cache */
1082 128, /* l2 cache */
1083 1, /* prefetch streams /*/
1084 0, /* SF->DF convert */
1087 /* Instruction costs on PPCE6500 processors. */
1088 static const
1089 struct processor_costs ppce6500_cost = {
1090 COSTS_N_INSNS (5), /* mulsi */
1091 COSTS_N_INSNS (5), /* mulsi_const */
1092 COSTS_N_INSNS (4), /* mulsi_const9 */
1093 COSTS_N_INSNS (5), /* muldi */
1094 COSTS_N_INSNS (14), /* divsi */
1095 COSTS_N_INSNS (14), /* divdi */
1096 COSTS_N_INSNS (7), /* fp */
1097 COSTS_N_INSNS (10), /* dmul */
1098 COSTS_N_INSNS (36), /* sdiv */
1099 COSTS_N_INSNS (66), /* ddiv */
1100 64, /* cache line size */
1101 32, /* l1 cache */
1102 128, /* l2 cache */
1103 1, /* prefetch streams /*/
1104 0, /* SF->DF convert */
1107 /* Instruction costs on AppliedMicro Titan processors. */
1108 static const
1109 struct processor_costs titan_cost = {
1110 COSTS_N_INSNS (5), /* mulsi */
1111 COSTS_N_INSNS (5), /* mulsi_const */
1112 COSTS_N_INSNS (5), /* mulsi_const9 */
1113 COSTS_N_INSNS (5), /* muldi */
1114 COSTS_N_INSNS (18), /* divsi */
1115 COSTS_N_INSNS (18), /* divdi */
1116 COSTS_N_INSNS (10), /* fp */
1117 COSTS_N_INSNS (10), /* dmul */
1118 COSTS_N_INSNS (46), /* sdiv */
1119 COSTS_N_INSNS (72), /* ddiv */
1120 32, /* cache line size */
1121 32, /* l1 cache */
1122 512, /* l2 cache */
1123 1, /* prefetch streams /*/
1124 0, /* SF->DF convert */
1127 /* Instruction costs on POWER4 and POWER5 processors. */
1128 static const
1129 struct processor_costs power4_cost = {
1130 COSTS_N_INSNS (3), /* mulsi */
1131 COSTS_N_INSNS (2), /* mulsi_const */
1132 COSTS_N_INSNS (2), /* mulsi_const9 */
1133 COSTS_N_INSNS (4), /* muldi */
1134 COSTS_N_INSNS (18), /* divsi */
1135 COSTS_N_INSNS (34), /* divdi */
1136 COSTS_N_INSNS (3), /* fp */
1137 COSTS_N_INSNS (3), /* dmul */
1138 COSTS_N_INSNS (17), /* sdiv */
1139 COSTS_N_INSNS (17), /* ddiv */
1140 128, /* cache line size */
1141 32, /* l1 cache */
1142 1024, /* l2 cache */
1143 8, /* prefetch streams /*/
1144 0, /* SF->DF convert */
1147 /* Instruction costs on POWER6 processors. */
1148 static const
1149 struct processor_costs power6_cost = {
1150 COSTS_N_INSNS (8), /* mulsi */
1151 COSTS_N_INSNS (8), /* mulsi_const */
1152 COSTS_N_INSNS (8), /* mulsi_const9 */
1153 COSTS_N_INSNS (8), /* muldi */
1154 COSTS_N_INSNS (22), /* divsi */
1155 COSTS_N_INSNS (28), /* divdi */
1156 COSTS_N_INSNS (3), /* fp */
1157 COSTS_N_INSNS (3), /* dmul */
1158 COSTS_N_INSNS (13), /* sdiv */
1159 COSTS_N_INSNS (16), /* ddiv */
1160 128, /* cache line size */
1161 64, /* l1 cache */
1162 2048, /* l2 cache */
1163 16, /* prefetch streams */
1164 0, /* SF->DF convert */
1167 /* Instruction costs on POWER7 processors. */
1168 static const
1169 struct processor_costs power7_cost = {
1170 COSTS_N_INSNS (2), /* mulsi */
1171 COSTS_N_INSNS (2), /* mulsi_const */
1172 COSTS_N_INSNS (2), /* mulsi_const9 */
1173 COSTS_N_INSNS (2), /* muldi */
1174 COSTS_N_INSNS (18), /* divsi */
1175 COSTS_N_INSNS (34), /* divdi */
1176 COSTS_N_INSNS (3), /* fp */
1177 COSTS_N_INSNS (3), /* dmul */
1178 COSTS_N_INSNS (13), /* sdiv */
1179 COSTS_N_INSNS (16), /* ddiv */
1180 128, /* cache line size */
1181 32, /* l1 cache */
1182 256, /* l2 cache */
1183 12, /* prefetch streams */
1184 COSTS_N_INSNS (3), /* SF->DF convert */
1187 /* Instruction costs on POWER8 processors. */
1188 static const
1189 struct processor_costs power8_cost = {
1190 COSTS_N_INSNS (3), /* mulsi */
1191 COSTS_N_INSNS (3), /* mulsi_const */
1192 COSTS_N_INSNS (3), /* mulsi_const9 */
1193 COSTS_N_INSNS (3), /* muldi */
1194 COSTS_N_INSNS (19), /* divsi */
1195 COSTS_N_INSNS (35), /* divdi */
1196 COSTS_N_INSNS (3), /* fp */
1197 COSTS_N_INSNS (3), /* dmul */
1198 COSTS_N_INSNS (14), /* sdiv */
1199 COSTS_N_INSNS (17), /* ddiv */
1200 128, /* cache line size */
1201 32, /* l1 cache */
1202 256, /* l2 cache */
1203 12, /* prefetch streams */
1204 COSTS_N_INSNS (3), /* SF->DF convert */
1207 /* Instruction costs on POWER9 processors. */
1208 static const
1209 struct processor_costs power9_cost = {
1210 COSTS_N_INSNS (3), /* mulsi */
1211 COSTS_N_INSNS (3), /* mulsi_const */
1212 COSTS_N_INSNS (3), /* mulsi_const9 */
1213 COSTS_N_INSNS (3), /* muldi */
1214 COSTS_N_INSNS (8), /* divsi */
1215 COSTS_N_INSNS (12), /* divdi */
1216 COSTS_N_INSNS (3), /* fp */
1217 COSTS_N_INSNS (3), /* dmul */
1218 COSTS_N_INSNS (13), /* sdiv */
1219 COSTS_N_INSNS (18), /* ddiv */
1220 128, /* cache line size */
1221 32, /* l1 cache */
1222 512, /* l2 cache */
1223 8, /* prefetch streams */
1224 COSTS_N_INSNS (3), /* SF->DF convert */
1227 /* Instruction costs on POWER A2 processors. */
1228 static const
1229 struct processor_costs ppca2_cost = {
1230 COSTS_N_INSNS (16), /* mulsi */
1231 COSTS_N_INSNS (16), /* mulsi_const */
1232 COSTS_N_INSNS (16), /* mulsi_const9 */
1233 COSTS_N_INSNS (16), /* muldi */
1234 COSTS_N_INSNS (22), /* divsi */
1235 COSTS_N_INSNS (28), /* divdi */
1236 COSTS_N_INSNS (3), /* fp */
1237 COSTS_N_INSNS (3), /* dmul */
1238 COSTS_N_INSNS (59), /* sdiv */
1239 COSTS_N_INSNS (72), /* ddiv */
1241 16, /* l1 cache */
1242 2048, /* l2 cache */
1243 16, /* prefetch streams */
1244 0, /* SF->DF convert */
1248 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
1249 #undef RS6000_BUILTIN_0
1250 #undef RS6000_BUILTIN_1
1251 #undef RS6000_BUILTIN_2
1252 #undef RS6000_BUILTIN_3
1253 #undef RS6000_BUILTIN_A
1254 #undef RS6000_BUILTIN_D
1255 #undef RS6000_BUILTIN_E
1256 #undef RS6000_BUILTIN_H
1257 #undef RS6000_BUILTIN_P
1258 #undef RS6000_BUILTIN_Q
1259 #undef RS6000_BUILTIN_S
1260 #undef RS6000_BUILTIN_X
1262 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE) \
1263 { NAME, ICODE, MASK, ATTR },
1265 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
1266 { NAME, ICODE, MASK, ATTR },
1268 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
1269 { NAME, ICODE, MASK, ATTR },
1271 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
1272 { NAME, ICODE, MASK, ATTR },
1274 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
1275 { NAME, ICODE, MASK, ATTR },
1277 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
1278 { NAME, ICODE, MASK, ATTR },
1280 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
1281 { NAME, ICODE, MASK, ATTR },
1283 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
1284 { NAME, ICODE, MASK, ATTR },
1286 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
1287 { NAME, ICODE, MASK, ATTR },
1289 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
1290 { NAME, ICODE, MASK, ATTR },
1292 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
1293 { NAME, ICODE, MASK, ATTR },
1295 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
1296 { NAME, ICODE, MASK, ATTR },
1298 struct rs6000_builtin_info_type {
1299 const char *name;
1300 const enum insn_code icode;
1301 const HOST_WIDE_INT mask;
1302 const unsigned attr;
1305 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1307 #include "powerpcspe-builtin.def"
1310 #undef RS6000_BUILTIN_0
1311 #undef RS6000_BUILTIN_1
1312 #undef RS6000_BUILTIN_2
1313 #undef RS6000_BUILTIN_3
1314 #undef RS6000_BUILTIN_A
1315 #undef RS6000_BUILTIN_D
1316 #undef RS6000_BUILTIN_E
1317 #undef RS6000_BUILTIN_H
1318 #undef RS6000_BUILTIN_P
1319 #undef RS6000_BUILTIN_Q
1320 #undef RS6000_BUILTIN_S
1321 #undef RS6000_BUILTIN_X
1323 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
1324 static tree (*rs6000_veclib_handler) (combined_fn, tree, tree);
1327 static bool rs6000_debug_legitimate_address_p (machine_mode, rtx, bool);
1328 static bool spe_func_has_64bit_regs_p (void);
1329 static struct machine_function * rs6000_init_machine_status (void);
1330 static int rs6000_ra_ever_killed (void);
1331 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1332 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1333 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1334 static tree rs6000_builtin_vectorized_libmass (combined_fn, tree, tree);
1335 static void rs6000_emit_set_long_const (rtx, HOST_WIDE_INT);
1336 static int rs6000_memory_move_cost (machine_mode, reg_class_t, bool);
1337 static bool rs6000_debug_rtx_costs (rtx, machine_mode, int, int, int *, bool);
1338 static int rs6000_debug_address_cost (rtx, machine_mode, addr_space_t,
1339 bool);
1340 static int rs6000_debug_adjust_cost (rtx_insn *, int, rtx_insn *, int,
1341 unsigned int);
1342 static bool is_microcoded_insn (rtx_insn *);
1343 static bool is_nonpipeline_insn (rtx_insn *);
1344 static bool is_cracked_insn (rtx_insn *);
1345 static bool is_load_insn (rtx, rtx *);
1346 static bool is_store_insn (rtx, rtx *);
1347 static bool set_to_load_agen (rtx_insn *,rtx_insn *);
1348 static bool insn_terminates_group_p (rtx_insn *, enum group_termination);
1349 static bool insn_must_be_first_in_group (rtx_insn *);
1350 static bool insn_must_be_last_in_group (rtx_insn *);
1351 static void altivec_init_builtins (void);
1352 static tree builtin_function_type (machine_mode, machine_mode,
1353 machine_mode, machine_mode,
1354 enum rs6000_builtins, const char *name);
1355 static void rs6000_common_init_builtins (void);
1356 static void paired_init_builtins (void);
1357 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1358 static void spe_init_builtins (void);
1359 static void htm_init_builtins (void);
1360 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1361 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1362 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1363 static rs6000_stack_t *rs6000_stack_info (void);
1364 static void is_altivec_return_reg (rtx, void *);
1365 int easy_vector_constant (rtx, machine_mode);
1366 static rtx rs6000_debug_legitimize_address (rtx, rtx, machine_mode);
1367 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1368 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1369 bool, bool);
1370 #if TARGET_MACHO
1371 static void macho_branch_islands (void);
1372 #endif
1373 static rtx rs6000_legitimize_reload_address (rtx, machine_mode, int, int,
1374 int, int *);
1375 static rtx rs6000_debug_legitimize_reload_address (rtx, machine_mode, int,
1376 int, int, int *);
1377 static bool rs6000_mode_dependent_address (const_rtx);
1378 static bool rs6000_debug_mode_dependent_address (const_rtx);
1379 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1380 machine_mode, rtx);
1381 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1382 machine_mode,
1383 rtx);
1384 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1385 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1386 enum reg_class);
1387 static bool rs6000_debug_secondary_memory_needed (machine_mode,
1388 reg_class_t,
1389 reg_class_t);
1390 static bool rs6000_debug_can_change_mode_class (machine_mode,
1391 machine_mode,
1392 reg_class_t);
1393 static bool rs6000_save_toc_in_prologue_p (void);
1394 static rtx rs6000_internal_arg_pointer (void);
1396 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, machine_mode, int, int,
1397 int, int *)
1398 = rs6000_legitimize_reload_address;
1400 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1401 = rs6000_mode_dependent_address;
1403 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1404 machine_mode, rtx)
1405 = rs6000_secondary_reload_class;
1407 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1408 = rs6000_preferred_reload_class;
1410 const int INSN_NOT_AVAILABLE = -1;
1412 static void rs6000_print_isa_options (FILE *, int, const char *,
1413 HOST_WIDE_INT);
1414 static void rs6000_print_builtin_options (FILE *, int, const char *,
1415 HOST_WIDE_INT);
1416 static HOST_WIDE_INT rs6000_disable_incompatible_switches (void);
1418 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1419 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1420 enum rs6000_reg_type,
1421 machine_mode,
1422 secondary_reload_info *,
1423 bool);
1424 rtl_opt_pass *make_pass_analyze_swaps (gcc::context*);
1425 static bool rs6000_keep_leaf_when_profiled () __attribute__ ((unused));
1426 static tree rs6000_fold_builtin (tree, int, tree *, bool);
1428 /* Hash table stuff for keeping track of TOC entries. */
1430 struct GTY((for_user)) toc_hash_struct
1432 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1433 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1434 rtx key;
1435 machine_mode key_mode;
1436 int labelno;
1439 struct toc_hasher : ggc_ptr_hash<toc_hash_struct>
1441 static hashval_t hash (toc_hash_struct *);
1442 static bool equal (toc_hash_struct *, toc_hash_struct *);
1445 static GTY (()) hash_table<toc_hasher> *toc_hash_table;
1447 /* Hash table to keep track of the argument types for builtin functions. */
1449 struct GTY((for_user)) builtin_hash_struct
1451 tree type;
1452 machine_mode mode[4]; /* return value + 3 arguments. */
1453 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1456 struct builtin_hasher : ggc_ptr_hash<builtin_hash_struct>
1458 static hashval_t hash (builtin_hash_struct *);
1459 static bool equal (builtin_hash_struct *, builtin_hash_struct *);
1462 static GTY (()) hash_table<builtin_hasher> *builtin_hash_table;
1465 /* Default register names. */
1466 char rs6000_reg_names[][8] =
1468 "0", "1", "2", "3", "4", "5", "6", "7",
1469 "8", "9", "10", "11", "12", "13", "14", "15",
1470 "16", "17", "18", "19", "20", "21", "22", "23",
1471 "24", "25", "26", "27", "28", "29", "30", "31",
1472 "0", "1", "2", "3", "4", "5", "6", "7",
1473 "8", "9", "10", "11", "12", "13", "14", "15",
1474 "16", "17", "18", "19", "20", "21", "22", "23",
1475 "24", "25", "26", "27", "28", "29", "30", "31",
1476 "mq", "lr", "ctr","ap",
1477 "0", "1", "2", "3", "4", "5", "6", "7",
1478 "ca",
1479 /* AltiVec registers. */
1480 "0", "1", "2", "3", "4", "5", "6", "7",
1481 "8", "9", "10", "11", "12", "13", "14", "15",
1482 "16", "17", "18", "19", "20", "21", "22", "23",
1483 "24", "25", "26", "27", "28", "29", "30", "31",
1484 "vrsave", "vscr",
1485 /* SPE registers. */
1486 "spe_acc", "spefscr",
1487 /* Soft frame pointer. */
1488 "sfp",
1489 /* HTM SPR registers. */
1490 "tfhar", "tfiar", "texasr",
1491 /* SPE High registers. */
1492 "0", "1", "2", "3", "4", "5", "6", "7",
1493 "8", "9", "10", "11", "12", "13", "14", "15",
1494 "16", "17", "18", "19", "20", "21", "22", "23",
1495 "24", "25", "26", "27", "28", "29", "30", "31"
1498 #ifdef TARGET_REGNAMES
1499 static const char alt_reg_names[][8] =
1501 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1502 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1503 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1504 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1505 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1506 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1507 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1508 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1509 "mq", "lr", "ctr", "ap",
1510 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1511 "ca",
1512 /* AltiVec registers. */
1513 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1514 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1515 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1516 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1517 "vrsave", "vscr",
1518 /* SPE registers. */
1519 "spe_acc", "spefscr",
1520 /* Soft frame pointer. */
1521 "sfp",
1522 /* HTM SPR registers. */
1523 "tfhar", "tfiar", "texasr",
1524 /* SPE High registers. */
1525 "%rh0", "%rh1", "%rh2", "%rh3", "%rh4", "%rh5", "%rh6", "%rh7",
1526 "%rh8", "%rh9", "%rh10", "%r11", "%rh12", "%rh13", "%rh14", "%rh15",
1527 "%rh16", "%rh17", "%rh18", "%rh19", "%rh20", "%rh21", "%rh22", "%rh23",
1528 "%rh24", "%rh25", "%rh26", "%rh27", "%rh28", "%rh29", "%rh30", "%rh31"
1530 #endif
1532 /* Table of valid machine attributes. */
1534 static const struct attribute_spec rs6000_attribute_table[] =
1536 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1537 affects_type_identity } */
1538 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1539 false },
1540 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1541 false },
1542 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1543 false },
1544 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1545 false },
1546 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1547 false },
1548 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1549 SUBTARGET_ATTRIBUTE_TABLE,
1550 #endif
1551 { NULL, 0, 0, false, false, false, NULL, false }
1554 #ifndef TARGET_PROFILE_KERNEL
1555 #define TARGET_PROFILE_KERNEL 0
1556 #endif
1558 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1559 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1561 /* Initialize the GCC target structure. */
1562 #undef TARGET_ATTRIBUTE_TABLE
1563 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1564 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1565 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1566 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1567 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1569 #undef TARGET_ASM_ALIGNED_DI_OP
1570 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1572 /* Default unaligned ops are only provided for ELF. Find the ops needed
1573 for non-ELF systems. */
1574 #ifndef OBJECT_FORMAT_ELF
1575 #if TARGET_XCOFF
1576 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1577 64-bit targets. */
1578 #undef TARGET_ASM_UNALIGNED_HI_OP
1579 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1580 #undef TARGET_ASM_UNALIGNED_SI_OP
1581 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1582 #undef TARGET_ASM_UNALIGNED_DI_OP
1583 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1584 #else
1585 /* For Darwin. */
1586 #undef TARGET_ASM_UNALIGNED_HI_OP
1587 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1588 #undef TARGET_ASM_UNALIGNED_SI_OP
1589 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1590 #undef TARGET_ASM_UNALIGNED_DI_OP
1591 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1592 #undef TARGET_ASM_ALIGNED_DI_OP
1593 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1594 #endif
1595 #endif
1597 /* This hook deals with fixups for relocatable code and DI-mode objects
1598 in 64-bit code. */
1599 #undef TARGET_ASM_INTEGER
1600 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1602 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1603 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1604 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1605 #endif
1607 #undef TARGET_SET_UP_BY_PROLOGUE
1608 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1610 #undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
1611 #define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS rs6000_get_separate_components
1612 #undef TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB
1613 #define TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB rs6000_components_for_bb
1614 #undef TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS
1615 #define TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS rs6000_disqualify_components
1616 #undef TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS
1617 #define TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS rs6000_emit_prologue_components
1618 #undef TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS
1619 #define TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS rs6000_emit_epilogue_components
1620 #undef TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS
1621 #define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS rs6000_set_handled_components
1623 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1624 #define TARGET_EXTRA_LIVE_ON_ENTRY rs6000_live_on_entry
1626 #undef TARGET_INTERNAL_ARG_POINTER
1627 #define TARGET_INTERNAL_ARG_POINTER rs6000_internal_arg_pointer
1629 #undef TARGET_HAVE_TLS
1630 #define TARGET_HAVE_TLS HAVE_AS_TLS
1632 #undef TARGET_CANNOT_FORCE_CONST_MEM
1633 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1635 #undef TARGET_DELEGITIMIZE_ADDRESS
1636 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1638 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1639 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1641 #undef TARGET_LEGITIMATE_COMBINED_INSN
1642 #define TARGET_LEGITIMATE_COMBINED_INSN rs6000_legitimate_combined_insn
1644 #undef TARGET_ASM_FUNCTION_PROLOGUE
1645 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1646 #undef TARGET_ASM_FUNCTION_EPILOGUE
1647 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1649 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1650 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1652 #undef TARGET_LEGITIMIZE_ADDRESS
1653 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1655 #undef TARGET_SCHED_VARIABLE_ISSUE
1656 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1658 #undef TARGET_SCHED_ISSUE_RATE
1659 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1660 #undef TARGET_SCHED_ADJUST_COST
1661 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1662 #undef TARGET_SCHED_ADJUST_PRIORITY
1663 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1664 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1665 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1666 #undef TARGET_SCHED_INIT
1667 #define TARGET_SCHED_INIT rs6000_sched_init
1668 #undef TARGET_SCHED_FINISH
1669 #define TARGET_SCHED_FINISH rs6000_sched_finish
1670 #undef TARGET_SCHED_REORDER
1671 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1672 #undef TARGET_SCHED_REORDER2
1673 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1675 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1676 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1678 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1679 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1681 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1682 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1683 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1684 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1685 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1686 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1687 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1688 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1690 #undef TARGET_SCHED_CAN_SPECULATE_INSN
1691 #define TARGET_SCHED_CAN_SPECULATE_INSN rs6000_sched_can_speculate_insn
1693 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1694 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1695 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1696 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1697 rs6000_builtin_support_vector_misalignment
1698 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1699 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1700 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1701 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1702 rs6000_builtin_vectorization_cost
1703 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1704 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1705 rs6000_preferred_simd_mode
1706 #undef TARGET_VECTORIZE_INIT_COST
1707 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1708 #undef TARGET_VECTORIZE_ADD_STMT_COST
1709 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1710 #undef TARGET_VECTORIZE_FINISH_COST
1711 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1712 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1713 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1715 #undef TARGET_INIT_BUILTINS
1716 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1717 #undef TARGET_BUILTIN_DECL
1718 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1720 #undef TARGET_FOLD_BUILTIN
1721 #define TARGET_FOLD_BUILTIN rs6000_fold_builtin
1722 #undef TARGET_GIMPLE_FOLD_BUILTIN
1723 #define TARGET_GIMPLE_FOLD_BUILTIN rs6000_gimple_fold_builtin
1725 #undef TARGET_EXPAND_BUILTIN
1726 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1728 #undef TARGET_MANGLE_TYPE
1729 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1731 #undef TARGET_INIT_LIBFUNCS
1732 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1734 #if TARGET_MACHO
1735 #undef TARGET_BINDS_LOCAL_P
1736 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1737 #endif
1739 #undef TARGET_MS_BITFIELD_LAYOUT_P
1740 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1742 #undef TARGET_ASM_OUTPUT_MI_THUNK
1743 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1745 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1746 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1748 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1749 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1751 #undef TARGET_REGISTER_MOVE_COST
1752 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1753 #undef TARGET_MEMORY_MOVE_COST
1754 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1755 #undef TARGET_CANNOT_COPY_INSN_P
1756 #define TARGET_CANNOT_COPY_INSN_P rs6000_cannot_copy_insn_p
1757 #undef TARGET_RTX_COSTS
1758 #define TARGET_RTX_COSTS rs6000_rtx_costs
1759 #undef TARGET_ADDRESS_COST
1760 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1762 #undef TARGET_DWARF_REGISTER_SPAN
1763 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1765 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1766 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1768 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1769 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1771 #undef TARGET_PROMOTE_FUNCTION_MODE
1772 #define TARGET_PROMOTE_FUNCTION_MODE rs6000_promote_function_mode
1774 #undef TARGET_RETURN_IN_MEMORY
1775 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1777 #undef TARGET_RETURN_IN_MSB
1778 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1780 #undef TARGET_SETUP_INCOMING_VARARGS
1781 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1783 /* Always strict argument naming on rs6000. */
1784 #undef TARGET_STRICT_ARGUMENT_NAMING
1785 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1786 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1787 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1788 #undef TARGET_SPLIT_COMPLEX_ARG
1789 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1790 #undef TARGET_MUST_PASS_IN_STACK
1791 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1792 #undef TARGET_PASS_BY_REFERENCE
1793 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1794 #undef TARGET_ARG_PARTIAL_BYTES
1795 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1796 #undef TARGET_FUNCTION_ARG_ADVANCE
1797 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1798 #undef TARGET_FUNCTION_ARG
1799 #define TARGET_FUNCTION_ARG rs6000_function_arg
1800 #undef TARGET_FUNCTION_ARG_PADDING
1801 #define TARGET_FUNCTION_ARG_PADDING rs6000_function_arg_padding
1802 #undef TARGET_FUNCTION_ARG_BOUNDARY
1803 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1805 #undef TARGET_BUILD_BUILTIN_VA_LIST
1806 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1808 #undef TARGET_EXPAND_BUILTIN_VA_START
1809 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1811 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1812 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1814 #undef TARGET_EH_RETURN_FILTER_MODE
1815 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1817 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1818 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1820 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1821 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1823 #undef TARGET_FLOATN_MODE
1824 #define TARGET_FLOATN_MODE rs6000_floatn_mode
1826 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1827 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1829 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1830 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1832 #undef TARGET_MD_ASM_ADJUST
1833 #define TARGET_MD_ASM_ADJUST rs6000_md_asm_adjust
1835 #undef TARGET_OPTION_OVERRIDE
1836 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1838 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1839 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1840 rs6000_builtin_vectorized_function
1842 #undef TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION
1843 #define TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION \
1844 rs6000_builtin_md_vectorized_function
1846 #undef TARGET_STACK_PROTECT_GUARD
1847 #define TARGET_STACK_PROTECT_GUARD rs6000_init_stack_protect_guard
1849 #if !TARGET_MACHO
1850 #undef TARGET_STACK_PROTECT_FAIL
1851 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1852 #endif
1854 #ifdef HAVE_AS_TLS
1855 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1856 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1857 #endif
1859 /* Use a 32-bit anchor range. This leads to sequences like:
1861 addis tmp,anchor,high
1862 add dest,tmp,low
1864 where tmp itself acts as an anchor, and can be shared between
1865 accesses to the same 64k page. */
1866 #undef TARGET_MIN_ANCHOR_OFFSET
1867 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1868 #undef TARGET_MAX_ANCHOR_OFFSET
1869 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1870 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1871 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1872 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1873 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1875 #undef TARGET_BUILTIN_RECIPROCAL
1876 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1878 #undef TARGET_EXPAND_TO_RTL_HOOK
1879 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1881 #undef TARGET_INSTANTIATE_DECLS
1882 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1884 #undef TARGET_SECONDARY_RELOAD
1885 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1886 #undef TARGET_SECONDARY_MEMORY_NEEDED
1887 #define TARGET_SECONDARY_MEMORY_NEEDED rs6000_secondary_memory_needed
1888 #undef TARGET_SECONDARY_MEMORY_NEEDED_MODE
1889 #define TARGET_SECONDARY_MEMORY_NEEDED_MODE rs6000_secondary_memory_needed_mode
1891 #undef TARGET_LEGITIMATE_ADDRESS_P
1892 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1894 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1895 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1897 #undef TARGET_LRA_P
1898 #define TARGET_LRA_P rs6000_lra_p
1900 #undef TARGET_COMPUTE_PRESSURE_CLASSES
1901 #define TARGET_COMPUTE_PRESSURE_CLASSES rs6000_compute_pressure_classes
1903 #undef TARGET_CAN_ELIMINATE
1904 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1906 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1907 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1909 #undef TARGET_SCHED_REASSOCIATION_WIDTH
1910 #define TARGET_SCHED_REASSOCIATION_WIDTH rs6000_reassociation_width
1912 #undef TARGET_TRAMPOLINE_INIT
1913 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1915 #undef TARGET_FUNCTION_VALUE
1916 #define TARGET_FUNCTION_VALUE rs6000_function_value
1918 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1919 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1921 #undef TARGET_OPTION_SAVE
1922 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1924 #undef TARGET_OPTION_RESTORE
1925 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1927 #undef TARGET_OPTION_PRINT
1928 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1930 #undef TARGET_CAN_INLINE_P
1931 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1933 #undef TARGET_SET_CURRENT_FUNCTION
1934 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1936 #undef TARGET_LEGITIMATE_CONSTANT_P
1937 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1939 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1940 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1942 #undef TARGET_CAN_USE_DOLOOP_P
1943 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1945 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
1946 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV rs6000_atomic_assign_expand_fenv
1948 #undef TARGET_LIBGCC_CMP_RETURN_MODE
1949 #define TARGET_LIBGCC_CMP_RETURN_MODE rs6000_abi_word_mode
1950 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
1951 #define TARGET_LIBGCC_SHIFT_COUNT_MODE rs6000_abi_word_mode
1952 #undef TARGET_UNWIND_WORD_MODE
1953 #define TARGET_UNWIND_WORD_MODE rs6000_abi_word_mode
1955 #undef TARGET_OFFLOAD_OPTIONS
1956 #define TARGET_OFFLOAD_OPTIONS rs6000_offload_options
1958 #undef TARGET_C_MODE_FOR_SUFFIX
1959 #define TARGET_C_MODE_FOR_SUFFIX rs6000_c_mode_for_suffix
1961 #undef TARGET_INVALID_BINARY_OP
1962 #define TARGET_INVALID_BINARY_OP rs6000_invalid_binary_op
1964 #undef TARGET_OPTAB_SUPPORTED_P
1965 #define TARGET_OPTAB_SUPPORTED_P rs6000_optab_supported_p
1967 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
1968 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
1970 #undef TARGET_HARD_REGNO_NREGS
1971 #define TARGET_HARD_REGNO_NREGS rs6000_hard_regno_nregs_hook
1972 #undef TARGET_HARD_REGNO_MODE_OK
1973 #define TARGET_HARD_REGNO_MODE_OK rs6000_hard_regno_mode_ok
1975 #undef TARGET_MODES_TIEABLE_P
1976 #define TARGET_MODES_TIEABLE_P rs6000_modes_tieable_p
1978 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
1979 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
1980 rs6000_hard_regno_call_part_clobbered
1982 #undef TARGET_SLOW_UNALIGNED_ACCESS
1983 #define TARGET_SLOW_UNALIGNED_ACCESS rs6000_slow_unaligned_access
1985 #undef TARGET_CAN_CHANGE_MODE_CLASS
1986 #define TARGET_CAN_CHANGE_MODE_CLASS rs6000_can_change_mode_class
1988 #undef TARGET_CONSTANT_ALIGNMENT
1989 #define TARGET_CONSTANT_ALIGNMENT rs6000_constant_alignment
1991 #undef TARGET_STARTING_FRAME_OFFSET
1992 #define TARGET_STARTING_FRAME_OFFSET rs6000_starting_frame_offset
1995 /* Processor table. */
1996 struct rs6000_ptt
1998 const char *const name; /* Canonical processor name. */
1999 const enum processor_type processor; /* Processor type enum value. */
2000 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
2003 static struct rs6000_ptt const processor_target_table[] =
2005 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
2006 #include "powerpcspe-cpus.def"
2007 #undef RS6000_CPU
2010 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
2011 name is invalid. */
2013 static int
2014 rs6000_cpu_name_lookup (const char *name)
2016 size_t i;
2018 if (name != NULL)
2020 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2021 if (! strcmp (name, processor_target_table[i].name))
2022 return (int)i;
2025 return -1;
2029 /* Return number of consecutive hard regs needed starting at reg REGNO
2030 to hold something of mode MODE.
2031 This is ordinarily the length in words of a value of mode MODE
2032 but can be less for certain modes in special long registers.
2034 For the SPE, GPRs are 64 bits but only 32 bits are visible in
2035 scalar instructions. The upper 32 bits are only available to the
2036 SIMD instructions.
2038 POWER and PowerPC GPRs hold 32 bits worth;
2039 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
2041 static int
2042 rs6000_hard_regno_nregs_internal (int regno, machine_mode mode)
2044 unsigned HOST_WIDE_INT reg_size;
2046 /* 128-bit floating point usually takes 2 registers, unless it is IEEE
2047 128-bit floating point that can go in vector registers, which has VSX
2048 memory addressing. */
2049 if (FP_REGNO_P (regno))
2050 reg_size = (VECTOR_MEM_VSX_P (mode) || FLOAT128_VECTOR_P (mode)
2051 ? UNITS_PER_VSX_WORD
2052 : UNITS_PER_FP_WORD);
2054 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
2055 reg_size = UNITS_PER_SPE_WORD;
2057 else if (ALTIVEC_REGNO_P (regno))
2058 reg_size = UNITS_PER_ALTIVEC_WORD;
2060 /* The value returned for SCmode in the E500 double case is 2 for
2061 ABI compatibility; storing an SCmode value in a single register
2062 would require function_arg and rs6000_spe_function_arg to handle
2063 SCmode so as to pass the value correctly in a pair of
2064 registers. */
2065 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
2066 && !DECIMAL_FLOAT_MODE_P (mode) && SPE_SIMD_REGNO_P (regno))
2067 reg_size = UNITS_PER_FP_WORD;
2069 else
2070 reg_size = UNITS_PER_WORD;
2072 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
2075 /* Value is 1 if hard register REGNO can hold a value of machine-mode
2076 MODE. */
2077 static int
2078 rs6000_hard_regno_mode_ok_uncached (int regno, machine_mode mode)
2080 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
2082 if (COMPLEX_MODE_P (mode))
2083 mode = GET_MODE_INNER (mode);
2085 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
2086 register combinations, and use PTImode where we need to deal with quad
2087 word memory operations. Don't allow quad words in the argument or frame
2088 pointer registers, just registers 0..31. */
2089 if (mode == PTImode)
2090 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2091 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2092 && ((regno & 1) == 0));
2094 /* VSX registers that overlap the FPR registers are larger than for non-VSX
2095 implementations. Don't allow an item to be split between a FP register
2096 and an Altivec register. Allow TImode in all VSX registers if the user
2097 asked for it. */
2098 if (TARGET_VSX && VSX_REGNO_P (regno)
2099 && (VECTOR_MEM_VSX_P (mode)
2100 || FLOAT128_VECTOR_P (mode)
2101 || reg_addr[mode].scalar_in_vmx_p
2102 || (TARGET_VSX_TIMODE && mode == TImode)
2103 || (TARGET_VADDUQM && mode == V1TImode)))
2105 if (FP_REGNO_P (regno))
2106 return FP_REGNO_P (last_regno);
2108 if (ALTIVEC_REGNO_P (regno))
2110 if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
2111 return 0;
2113 return ALTIVEC_REGNO_P (last_regno);
2117 /* The GPRs can hold any mode, but values bigger than one register
2118 cannot go past R31. */
2119 if (INT_REGNO_P (regno))
2120 return INT_REGNO_P (last_regno);
2122 /* The float registers (except for VSX vector modes) can only hold floating
2123 modes and DImode. */
2124 if (FP_REGNO_P (regno))
2126 if (FLOAT128_VECTOR_P (mode))
2127 return false;
2129 if (SCALAR_FLOAT_MODE_P (mode)
2130 && (mode != TDmode || (regno % 2) == 0)
2131 && FP_REGNO_P (last_regno))
2132 return 1;
2134 if (GET_MODE_CLASS (mode) == MODE_INT)
2136 if(GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
2137 return 1;
2139 if (TARGET_VSX_SMALL_INTEGER)
2141 if (mode == SImode)
2142 return 1;
2144 if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
2145 return 1;
2149 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
2150 && PAIRED_VECTOR_MODE (mode))
2151 return 1;
2153 return 0;
2156 /* The CR register can only hold CC modes. */
2157 if (CR_REGNO_P (regno))
2158 return GET_MODE_CLASS (mode) == MODE_CC;
2160 if (CA_REGNO_P (regno))
2161 return mode == Pmode || mode == SImode;
2163 /* AltiVec only in AldyVec registers. */
2164 if (ALTIVEC_REGNO_P (regno))
2165 return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
2166 || mode == V1TImode);
2168 /* ...but GPRs can hold SIMD data on the SPE in one register. */
2169 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
2170 return 1;
2172 /* We cannot put non-VSX TImode or PTImode anywhere except general register
2173 and it must be able to fit within the register set. */
2175 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
2178 /* Implement TARGET_HARD_REGNO_NREGS. */
2180 static unsigned int
2181 rs6000_hard_regno_nregs_hook (unsigned int regno, machine_mode mode)
2183 return rs6000_hard_regno_nregs[mode][regno];
2186 /* Implement TARGET_HARD_REGNO_MODE_OK. */
2188 static bool
2189 rs6000_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2191 return rs6000_hard_regno_mode_ok_p[mode][regno];
2194 /* Implement TARGET_MODES_TIEABLE_P.
2196 PTImode cannot tie with other modes because PTImode is restricted to even
2197 GPR registers, and TImode can go in any GPR as well as VSX registers (PR
2198 57744).
2200 Altivec/VSX vector tests were moved ahead of scalar float mode, so that IEEE
2201 128-bit floating point on VSX systems ties with other vectors. */
2203 static bool
2204 rs6000_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2206 if (mode1 == PTImode)
2207 return mode2 == PTImode;
2208 if (mode2 == PTImode)
2209 return false;
2211 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode1))
2212 return ALTIVEC_OR_VSX_VECTOR_MODE (mode2);
2213 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode2))
2214 return false;
2216 if (SCALAR_FLOAT_MODE_P (mode1))
2217 return SCALAR_FLOAT_MODE_P (mode2);
2218 if (SCALAR_FLOAT_MODE_P (mode2))
2219 return false;
2221 if (GET_MODE_CLASS (mode1) == MODE_CC)
2222 return GET_MODE_CLASS (mode2) == MODE_CC;
2223 if (GET_MODE_CLASS (mode2) == MODE_CC)
2224 return false;
2226 if (SPE_VECTOR_MODE (mode1))
2227 return SPE_VECTOR_MODE (mode2);
2228 if (SPE_VECTOR_MODE (mode2))
2229 return false;
2231 return true;
2234 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED. */
2236 static bool
2237 rs6000_hard_regno_call_part_clobbered (unsigned int regno, machine_mode mode)
2239 if (TARGET_32BIT
2240 && TARGET_POWERPC64
2241 && GET_MODE_SIZE (mode) > 4
2242 && INT_REGNO_P (regno))
2243 return true;
2245 if (TARGET_VSX
2246 && FP_REGNO_P (regno)
2247 && GET_MODE_SIZE (mode) > 8
2248 && !FLOAT128_2REG_P (mode))
2249 return true;
2251 return false;
2254 /* Print interesting facts about registers. */
2255 static void
2256 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
2258 int r, m;
2260 for (r = first_regno; r <= last_regno; ++r)
2262 const char *comma = "";
2263 int len;
2265 if (first_regno == last_regno)
2266 fprintf (stderr, "%s:\t", reg_name);
2267 else
2268 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
2270 len = 8;
2271 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2272 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
2274 if (len > 70)
2276 fprintf (stderr, ",\n\t");
2277 len = 8;
2278 comma = "";
2281 if (rs6000_hard_regno_nregs[m][r] > 1)
2282 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
2283 rs6000_hard_regno_nregs[m][r]);
2284 else
2285 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
2287 comma = ", ";
2290 if (call_used_regs[r])
2292 if (len > 70)
2294 fprintf (stderr, ",\n\t");
2295 len = 8;
2296 comma = "";
2299 len += fprintf (stderr, "%s%s", comma, "call-used");
2300 comma = ", ";
2303 if (fixed_regs[r])
2305 if (len > 70)
2307 fprintf (stderr, ",\n\t");
2308 len = 8;
2309 comma = "";
2312 len += fprintf (stderr, "%s%s", comma, "fixed");
2313 comma = ", ";
2316 if (len > 70)
2318 fprintf (stderr, ",\n\t");
2319 comma = "";
2322 len += fprintf (stderr, "%sreg-class = %s", comma,
2323 reg_class_names[(int)rs6000_regno_regclass[r]]);
2324 comma = ", ";
2326 if (len > 70)
2328 fprintf (stderr, ",\n\t");
2329 comma = "";
2332 fprintf (stderr, "%sregno = %d\n", comma, r);
2336 static const char *
2337 rs6000_debug_vector_unit (enum rs6000_vector v)
2339 const char *ret;
2341 switch (v)
2343 case VECTOR_NONE: ret = "none"; break;
2344 case VECTOR_ALTIVEC: ret = "altivec"; break;
2345 case VECTOR_VSX: ret = "vsx"; break;
2346 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
2347 case VECTOR_PAIRED: ret = "paired"; break;
2348 case VECTOR_SPE: ret = "spe"; break;
2349 case VECTOR_OTHER: ret = "other"; break;
2350 default: ret = "unknown"; break;
2353 return ret;
2356 /* Inner function printing just the address mask for a particular reload
2357 register class. */
2358 DEBUG_FUNCTION char *
2359 rs6000_debug_addr_mask (addr_mask_type mask, bool keep_spaces)
2361 static char ret[8];
2362 char *p = ret;
2364 if ((mask & RELOAD_REG_VALID) != 0)
2365 *p++ = 'v';
2366 else if (keep_spaces)
2367 *p++ = ' ';
2369 if ((mask & RELOAD_REG_MULTIPLE) != 0)
2370 *p++ = 'm';
2371 else if (keep_spaces)
2372 *p++ = ' ';
2374 if ((mask & RELOAD_REG_INDEXED) != 0)
2375 *p++ = 'i';
2376 else if (keep_spaces)
2377 *p++ = ' ';
2379 if ((mask & RELOAD_REG_QUAD_OFFSET) != 0)
2380 *p++ = 'O';
2381 else if ((mask & RELOAD_REG_OFFSET) != 0)
2382 *p++ = 'o';
2383 else if (keep_spaces)
2384 *p++ = ' ';
2386 if ((mask & RELOAD_REG_PRE_INCDEC) != 0)
2387 *p++ = '+';
2388 else if (keep_spaces)
2389 *p++ = ' ';
2391 if ((mask & RELOAD_REG_PRE_MODIFY) != 0)
2392 *p++ = '+';
2393 else if (keep_spaces)
2394 *p++ = ' ';
2396 if ((mask & RELOAD_REG_AND_M16) != 0)
2397 *p++ = '&';
2398 else if (keep_spaces)
2399 *p++ = ' ';
2401 *p = '\0';
2403 return ret;
2406 /* Print the address masks in a human readble fashion. */
2407 DEBUG_FUNCTION void
2408 rs6000_debug_print_mode (ssize_t m)
2410 ssize_t rc;
2411 int spaces = 0;
2412 bool fuse_extra_p;
2414 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
2415 for (rc = 0; rc < N_RELOAD_REG; rc++)
2416 fprintf (stderr, " %s: %s", reload_reg_map[rc].name,
2417 rs6000_debug_addr_mask (reg_addr[m].addr_mask[rc], true));
2419 if ((reg_addr[m].reload_store != CODE_FOR_nothing)
2420 || (reg_addr[m].reload_load != CODE_FOR_nothing))
2421 fprintf (stderr, " Reload=%c%c",
2422 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
2423 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
2424 else
2425 spaces += sizeof (" Reload=sl") - 1;
2427 if (reg_addr[m].scalar_in_vmx_p)
2429 fprintf (stderr, "%*s Upper=y", spaces, "");
2430 spaces = 0;
2432 else
2433 spaces += sizeof (" Upper=y") - 1;
2435 fuse_extra_p = ((reg_addr[m].fusion_gpr_ld != CODE_FOR_nothing)
2436 || reg_addr[m].fused_toc);
2437 if (!fuse_extra_p)
2439 for (rc = 0; rc < N_RELOAD_REG; rc++)
2441 if (rc != RELOAD_REG_ANY)
2443 if (reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing
2444 || reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing
2445 || reg_addr[m].fusion_addi_st[rc] != CODE_FOR_nothing
2446 || reg_addr[m].fusion_addis_ld[rc] != CODE_FOR_nothing
2447 || reg_addr[m].fusion_addis_st[rc] != CODE_FOR_nothing)
2449 fuse_extra_p = true;
2450 break;
2456 if (fuse_extra_p)
2458 fprintf (stderr, "%*s Fuse:", spaces, "");
2459 spaces = 0;
2461 for (rc = 0; rc < N_RELOAD_REG; rc++)
2463 if (rc != RELOAD_REG_ANY)
2465 char load, store;
2467 if (reg_addr[m].fusion_addis_ld[rc] != CODE_FOR_nothing)
2468 load = 'l';
2469 else if (reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing)
2470 load = 'L';
2471 else
2472 load = '-';
2474 if (reg_addr[m].fusion_addis_st[rc] != CODE_FOR_nothing)
2475 store = 's';
2476 else if (reg_addr[m].fusion_addi_st[rc] != CODE_FOR_nothing)
2477 store = 'S';
2478 else
2479 store = '-';
2481 if (load == '-' && store == '-')
2482 spaces += 5;
2483 else
2485 fprintf (stderr, "%*s%c=%c%c", (spaces + 1), "",
2486 reload_reg_map[rc].name[0], load, store);
2487 spaces = 0;
2492 if (reg_addr[m].fusion_gpr_ld != CODE_FOR_nothing)
2494 fprintf (stderr, "%*sP8gpr", (spaces + 1), "");
2495 spaces = 0;
2497 else
2498 spaces += sizeof (" P8gpr") - 1;
2500 if (reg_addr[m].fused_toc)
2502 fprintf (stderr, "%*sToc", (spaces + 1), "");
2503 spaces = 0;
2505 else
2506 spaces += sizeof (" Toc") - 1;
2508 else
2509 spaces += sizeof (" Fuse: G=ls F=ls v=ls P8gpr Toc") - 1;
2511 if (rs6000_vector_unit[m] != VECTOR_NONE
2512 || rs6000_vector_mem[m] != VECTOR_NONE)
2514 fprintf (stderr, "%*s vector: arith=%-10s mem=%s",
2515 spaces, "",
2516 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
2517 rs6000_debug_vector_unit (rs6000_vector_mem[m]));
2520 fputs ("\n", stderr);
2523 #define DEBUG_FMT_ID "%-32s= "
2524 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
2525 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
2526 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
2528 /* Print various interesting information with -mdebug=reg. */
2529 static void
2530 rs6000_debug_reg_global (void)
2532 static const char *const tf[2] = { "false", "true" };
2533 const char *nl = (const char *)0;
2534 int m;
2535 size_t m1, m2, v;
2536 char costly_num[20];
2537 char nop_num[20];
2538 char flags_buffer[40];
2539 const char *costly_str;
2540 const char *nop_str;
2541 const char *trace_str;
2542 const char *abi_str;
2543 const char *cmodel_str;
2544 struct cl_target_option cl_opts;
2546 /* Modes we want tieable information on. */
2547 static const machine_mode print_tieable_modes[] = {
2548 QImode,
2549 HImode,
2550 SImode,
2551 DImode,
2552 TImode,
2553 PTImode,
2554 SFmode,
2555 DFmode,
2556 TFmode,
2557 IFmode,
2558 KFmode,
2559 SDmode,
2560 DDmode,
2561 TDmode,
2562 V8QImode,
2563 V4HImode,
2564 V2SImode,
2565 V16QImode,
2566 V8HImode,
2567 V4SImode,
2568 V2DImode,
2569 V1TImode,
2570 V32QImode,
2571 V16HImode,
2572 V8SImode,
2573 V4DImode,
2574 V2TImode,
2575 V2SFmode,
2576 V4SFmode,
2577 V2DFmode,
2578 V8SFmode,
2579 V4DFmode,
2580 CCmode,
2581 CCUNSmode,
2582 CCEQmode,
2585 /* Virtual regs we are interested in. */
2586 const static struct {
2587 int regno; /* register number. */
2588 const char *name; /* register name. */
2589 } virtual_regs[] = {
2590 { STACK_POINTER_REGNUM, "stack pointer:" },
2591 { TOC_REGNUM, "toc: " },
2592 { STATIC_CHAIN_REGNUM, "static chain: " },
2593 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
2594 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
2595 { ARG_POINTER_REGNUM, "arg pointer: " },
2596 { FRAME_POINTER_REGNUM, "frame pointer:" },
2597 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
2598 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
2599 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
2600 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
2601 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
2602 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
2603 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
2604 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
2605 { LAST_VIRTUAL_REGISTER, "last virtual: " },
2608 fputs ("\nHard register information:\n", stderr);
2609 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2610 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2611 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2612 LAST_ALTIVEC_REGNO,
2613 "vs");
2614 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2615 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2616 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2617 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2618 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2619 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2620 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
2621 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
2623 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2624 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2625 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2627 fprintf (stderr,
2628 "\n"
2629 "d reg_class = %s\n"
2630 "f reg_class = %s\n"
2631 "v reg_class = %s\n"
2632 "wa reg_class = %s\n"
2633 "wb reg_class = %s\n"
2634 "wd reg_class = %s\n"
2635 "we reg_class = %s\n"
2636 "wf reg_class = %s\n"
2637 "wg reg_class = %s\n"
2638 "wh reg_class = %s\n"
2639 "wi reg_class = %s\n"
2640 "wj reg_class = %s\n"
2641 "wk reg_class = %s\n"
2642 "wl reg_class = %s\n"
2643 "wm reg_class = %s\n"
2644 "wo reg_class = %s\n"
2645 "wp reg_class = %s\n"
2646 "wq reg_class = %s\n"
2647 "wr reg_class = %s\n"
2648 "ws reg_class = %s\n"
2649 "wt reg_class = %s\n"
2650 "wu reg_class = %s\n"
2651 "wv reg_class = %s\n"
2652 "ww reg_class = %s\n"
2653 "wx reg_class = %s\n"
2654 "wy reg_class = %s\n"
2655 "wz reg_class = %s\n"
2656 "wA reg_class = %s\n"
2657 "wH reg_class = %s\n"
2658 "wI reg_class = %s\n"
2659 "wJ reg_class = %s\n"
2660 "wK reg_class = %s\n"
2661 "\n",
2662 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2663 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2664 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2665 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2666 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wb]],
2667 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2668 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_we]],
2669 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2670 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2671 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wh]],
2672 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wi]],
2673 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wj]],
2674 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wk]],
2675 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2676 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2677 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wo]],
2678 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wp]],
2679 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wq]],
2680 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2681 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2682 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2683 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2684 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2685 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2686 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2687 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2688 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]],
2689 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wA]],
2690 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wH]],
2691 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wI]],
2692 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wJ]],
2693 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wK]]);
2695 nl = "\n";
2696 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2697 rs6000_debug_print_mode (m);
2699 fputs ("\n", stderr);
2701 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2703 machine_mode mode1 = print_tieable_modes[m1];
2704 bool first_time = true;
2706 nl = (const char *)0;
2707 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2709 machine_mode mode2 = print_tieable_modes[m2];
2710 if (mode1 != mode2 && rs6000_modes_tieable_p (mode1, mode2))
2712 if (first_time)
2714 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2715 nl = "\n";
2716 first_time = false;
2719 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2723 if (!first_time)
2724 fputs ("\n", stderr);
2727 if (nl)
2728 fputs (nl, stderr);
2730 if (rs6000_recip_control)
2732 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2734 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2735 if (rs6000_recip_bits[m])
2737 fprintf (stderr,
2738 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2739 GET_MODE_NAME (m),
2740 (RS6000_RECIP_AUTO_RE_P (m)
2741 ? "auto"
2742 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2743 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2744 ? "auto"
2745 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2748 fputs ("\n", stderr);
2751 if (rs6000_cpu_index >= 0)
2753 const char *name = processor_target_table[rs6000_cpu_index].name;
2754 HOST_WIDE_INT flags
2755 = processor_target_table[rs6000_cpu_index].target_enable;
2757 sprintf (flags_buffer, "-mcpu=%s flags", name);
2758 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2760 else
2761 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2763 if (rs6000_tune_index >= 0)
2765 const char *name = processor_target_table[rs6000_tune_index].name;
2766 HOST_WIDE_INT flags
2767 = processor_target_table[rs6000_tune_index].target_enable;
2769 sprintf (flags_buffer, "-mtune=%s flags", name);
2770 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2772 else
2773 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2775 cl_target_option_save (&cl_opts, &global_options);
2776 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2777 rs6000_isa_flags);
2779 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2780 rs6000_isa_flags_explicit);
2782 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2783 rs6000_builtin_mask);
2785 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2787 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2788 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2790 switch (rs6000_sched_costly_dep)
2792 case max_dep_latency:
2793 costly_str = "max_dep_latency";
2794 break;
2796 case no_dep_costly:
2797 costly_str = "no_dep_costly";
2798 break;
2800 case all_deps_costly:
2801 costly_str = "all_deps_costly";
2802 break;
2804 case true_store_to_load_dep_costly:
2805 costly_str = "true_store_to_load_dep_costly";
2806 break;
2808 case store_to_load_dep_costly:
2809 costly_str = "store_to_load_dep_costly";
2810 break;
2812 default:
2813 costly_str = costly_num;
2814 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2815 break;
2818 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2820 switch (rs6000_sched_insert_nops)
2822 case sched_finish_regroup_exact:
2823 nop_str = "sched_finish_regroup_exact";
2824 break;
2826 case sched_finish_pad_groups:
2827 nop_str = "sched_finish_pad_groups";
2828 break;
2830 case sched_finish_none:
2831 nop_str = "sched_finish_none";
2832 break;
2834 default:
2835 nop_str = nop_num;
2836 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2837 break;
2840 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2842 switch (rs6000_sdata)
2844 default:
2845 case SDATA_NONE:
2846 break;
2848 case SDATA_DATA:
2849 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2850 break;
2852 case SDATA_SYSV:
2853 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2854 break;
2856 case SDATA_EABI:
2857 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2858 break;
2862 switch (rs6000_traceback)
2864 case traceback_default: trace_str = "default"; break;
2865 case traceback_none: trace_str = "none"; break;
2866 case traceback_part: trace_str = "part"; break;
2867 case traceback_full: trace_str = "full"; break;
2868 default: trace_str = "unknown"; break;
2871 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2873 switch (rs6000_current_cmodel)
2875 case CMODEL_SMALL: cmodel_str = "small"; break;
2876 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2877 case CMODEL_LARGE: cmodel_str = "large"; break;
2878 default: cmodel_str = "unknown"; break;
2881 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2883 switch (rs6000_current_abi)
2885 case ABI_NONE: abi_str = "none"; break;
2886 case ABI_AIX: abi_str = "aix"; break;
2887 case ABI_ELFv2: abi_str = "ELFv2"; break;
2888 case ABI_V4: abi_str = "V4"; break;
2889 case ABI_DARWIN: abi_str = "darwin"; break;
2890 default: abi_str = "unknown"; break;
2893 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2895 if (rs6000_altivec_abi)
2896 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2898 if (rs6000_spe_abi)
2899 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2901 if (rs6000_darwin64_abi)
2902 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2904 if (rs6000_float_gprs)
2905 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2907 fprintf (stderr, DEBUG_FMT_S, "fprs",
2908 (TARGET_FPRS ? "true" : "false"));
2910 fprintf (stderr, DEBUG_FMT_S, "single_float",
2911 (TARGET_SINGLE_FLOAT ? "true" : "false"));
2913 fprintf (stderr, DEBUG_FMT_S, "double_float",
2914 (TARGET_DOUBLE_FLOAT ? "true" : "false"));
2916 fprintf (stderr, DEBUG_FMT_S, "soft_float",
2917 (TARGET_SOFT_FLOAT ? "true" : "false"));
2919 fprintf (stderr, DEBUG_FMT_S, "e500_single",
2920 (TARGET_E500_SINGLE ? "true" : "false"));
2922 fprintf (stderr, DEBUG_FMT_S, "e500_double",
2923 (TARGET_E500_DOUBLE ? "true" : "false"));
2925 if (TARGET_LINK_STACK)
2926 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2928 fprintf (stderr, DEBUG_FMT_S, "lra", TARGET_LRA ? "true" : "false");
2930 if (TARGET_P8_FUSION)
2932 char options[80];
2934 strcpy (options, (TARGET_P9_FUSION) ? "power9" : "power8");
2935 if (TARGET_TOC_FUSION)
2936 strcat (options, ", toc");
2938 if (TARGET_P8_FUSION_SIGN)
2939 strcat (options, ", sign");
2941 fprintf (stderr, DEBUG_FMT_S, "fusion", options);
2944 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2945 TARGET_SECURE_PLT ? "secure" : "bss");
2946 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2947 aix_struct_return ? "aix" : "sysv");
2948 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2949 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2950 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2951 tf[!!rs6000_align_branch_targets]);
2952 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2953 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2954 rs6000_long_double_type_size);
2955 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2956 (int)rs6000_sched_restricted_insns_priority);
2957 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2958 (int)END_BUILTINS);
2959 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2960 (int)RS6000_BUILTIN_COUNT);
2962 fprintf (stderr, DEBUG_FMT_D, "Enable float128 on VSX",
2963 (int)TARGET_FLOAT128_ENABLE_TYPE);
2965 if (TARGET_VSX)
2966 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2967 (int)VECTOR_ELEMENT_SCALAR_64BIT);
2969 if (TARGET_DIRECT_MOVE_128)
2970 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit mfvsrld element",
2971 (int)VECTOR_ELEMENT_MFVSRLD_64BIT);
2975 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2976 legitimate address support to figure out the appropriate addressing to
2977 use. */
2979 static void
2980 rs6000_setup_reg_addr_masks (void)
2982 ssize_t rc, reg, m, nregs;
2983 addr_mask_type any_addr_mask, addr_mask;
2985 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2987 machine_mode m2 = (machine_mode) m;
2988 bool complex_p = false;
2989 bool small_int_p = (m2 == QImode || m2 == HImode || m2 == SImode);
2990 size_t msize;
2992 if (COMPLEX_MODE_P (m2))
2994 complex_p = true;
2995 m2 = GET_MODE_INNER (m2);
2998 msize = GET_MODE_SIZE (m2);
3000 /* SDmode is special in that we want to access it only via REG+REG
3001 addressing on power7 and above, since we want to use the LFIWZX and
3002 STFIWZX instructions to load it. */
3003 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
3005 any_addr_mask = 0;
3006 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
3008 addr_mask = 0;
3009 reg = reload_reg_map[rc].reg;
3011 /* Can mode values go in the GPR/FPR/Altivec registers? */
3012 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
3014 bool small_int_vsx_p = (small_int_p
3015 && (rc == RELOAD_REG_FPR
3016 || rc == RELOAD_REG_VMX));
3018 nregs = rs6000_hard_regno_nregs[m][reg];
3019 addr_mask |= RELOAD_REG_VALID;
3021 /* Indicate if the mode takes more than 1 physical register. If
3022 it takes a single register, indicate it can do REG+REG
3023 addressing. Small integers in VSX registers can only do
3024 REG+REG addressing. */
3025 if (small_int_vsx_p)
3026 addr_mask |= RELOAD_REG_INDEXED;
3027 else if (nregs > 1 || m == BLKmode || complex_p)
3028 addr_mask |= RELOAD_REG_MULTIPLE;
3029 else
3030 addr_mask |= RELOAD_REG_INDEXED;
3032 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
3033 addressing. Restrict addressing on SPE for 64-bit types
3034 because of the SUBREG hackery used to address 64-bit floats in
3035 '32-bit' GPRs. If we allow scalars into Altivec registers,
3036 don't allow PRE_INC, PRE_DEC, or PRE_MODIFY. */
3038 if (TARGET_UPDATE
3039 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
3040 && msize <= 8
3041 && !VECTOR_MODE_P (m2)
3042 && !FLOAT128_VECTOR_P (m2)
3043 && !complex_p
3044 && !small_int_vsx_p
3045 && (m2 != DFmode || !TARGET_UPPER_REGS_DF)
3046 && (m2 != SFmode || !TARGET_UPPER_REGS_SF)
3047 && !(TARGET_E500_DOUBLE && msize == 8))
3049 addr_mask |= RELOAD_REG_PRE_INCDEC;
3051 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
3052 we don't allow PRE_MODIFY for some multi-register
3053 operations. */
3054 switch (m)
3056 default:
3057 addr_mask |= RELOAD_REG_PRE_MODIFY;
3058 break;
3060 case E_DImode:
3061 if (TARGET_POWERPC64)
3062 addr_mask |= RELOAD_REG_PRE_MODIFY;
3063 break;
3065 case E_DFmode:
3066 case E_DDmode:
3067 if (TARGET_DF_INSN)
3068 addr_mask |= RELOAD_REG_PRE_MODIFY;
3069 break;
3074 /* GPR and FPR registers can do REG+OFFSET addressing, except
3075 possibly for SDmode. ISA 3.0 (i.e. power9) adds D-form addressing
3076 for 64-bit scalars and 32-bit SFmode to altivec registers. */
3077 if ((addr_mask != 0) && !indexed_only_p
3078 && msize <= 8
3079 && (rc == RELOAD_REG_GPR
3080 || ((msize == 8 || m2 == SFmode)
3081 && (rc == RELOAD_REG_FPR
3082 || (rc == RELOAD_REG_VMX
3083 && TARGET_P9_DFORM_SCALAR)))))
3084 addr_mask |= RELOAD_REG_OFFSET;
3086 /* VSX registers can do REG+OFFSET addresssing if ISA 3.0
3087 instructions are enabled. The offset for 128-bit VSX registers is
3088 only 12-bits. While GPRs can handle the full offset range, VSX
3089 registers can only handle the restricted range. */
3090 else if ((addr_mask != 0) && !indexed_only_p
3091 && msize == 16 && TARGET_P9_DFORM_VECTOR
3092 && (ALTIVEC_OR_VSX_VECTOR_MODE (m2)
3093 || (m2 == TImode && TARGET_VSX_TIMODE)))
3095 addr_mask |= RELOAD_REG_OFFSET;
3096 if (rc == RELOAD_REG_FPR || rc == RELOAD_REG_VMX)
3097 addr_mask |= RELOAD_REG_QUAD_OFFSET;
3100 /* VMX registers can do (REG & -16) and ((REG+REG) & -16)
3101 addressing on 128-bit types. */
3102 if (rc == RELOAD_REG_VMX && msize == 16
3103 && (addr_mask & RELOAD_REG_VALID) != 0)
3104 addr_mask |= RELOAD_REG_AND_M16;
3106 reg_addr[m].addr_mask[rc] = addr_mask;
3107 any_addr_mask |= addr_mask;
3110 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
3115 /* Initialize the various global tables that are based on register size. */
3116 static void
3117 rs6000_init_hard_regno_mode_ok (bool global_init_p)
3119 ssize_t r, m, c;
3120 int align64;
3121 int align32;
3123 /* Precalculate REGNO_REG_CLASS. */
3124 rs6000_regno_regclass[0] = GENERAL_REGS;
3125 for (r = 1; r < 32; ++r)
3126 rs6000_regno_regclass[r] = BASE_REGS;
3128 for (r = 32; r < 64; ++r)
3129 rs6000_regno_regclass[r] = FLOAT_REGS;
3131 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
3132 rs6000_regno_regclass[r] = NO_REGS;
3134 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
3135 rs6000_regno_regclass[r] = ALTIVEC_REGS;
3137 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
3138 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
3139 rs6000_regno_regclass[r] = CR_REGS;
3141 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
3142 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
3143 rs6000_regno_regclass[CA_REGNO] = NO_REGS;
3144 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
3145 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
3146 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
3147 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
3148 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
3149 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
3150 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
3151 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
3152 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
3154 /* Precalculate register class to simpler reload register class. We don't
3155 need all of the register classes that are combinations of different
3156 classes, just the simple ones that have constraint letters. */
3157 for (c = 0; c < N_REG_CLASSES; c++)
3158 reg_class_to_reg_type[c] = NO_REG_TYPE;
3160 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
3161 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
3162 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
3163 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
3164 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
3165 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
3166 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
3167 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
3168 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
3169 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
3170 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
3171 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
3173 if (TARGET_VSX)
3175 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
3176 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
3178 else
3180 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
3181 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
3184 /* Precalculate the valid memory formats as well as the vector information,
3185 this must be set up before the rs6000_hard_regno_nregs_internal calls
3186 below. */
3187 gcc_assert ((int)VECTOR_NONE == 0);
3188 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
3189 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
3191 gcc_assert ((int)CODE_FOR_nothing == 0);
3192 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
3194 gcc_assert ((int)NO_REGS == 0);
3195 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
3197 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
3198 believes it can use native alignment or still uses 128-bit alignment. */
3199 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
3201 align64 = 64;
3202 align32 = 32;
3204 else
3206 align64 = 128;
3207 align32 = 128;
3210 /* KF mode (IEEE 128-bit in VSX registers). We do not have arithmetic, so
3211 only set the memory modes. Include TFmode if -mabi=ieeelongdouble. */
3212 if (TARGET_FLOAT128_TYPE)
3214 rs6000_vector_mem[KFmode] = VECTOR_VSX;
3215 rs6000_vector_align[KFmode] = 128;
3217 if (FLOAT128_IEEE_P (TFmode))
3219 rs6000_vector_mem[TFmode] = VECTOR_VSX;
3220 rs6000_vector_align[TFmode] = 128;
3224 /* V2DF mode, VSX only. */
3225 if (TARGET_VSX)
3227 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
3228 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
3229 rs6000_vector_align[V2DFmode] = align64;
3232 /* V4SF mode, either VSX or Altivec. */
3233 if (TARGET_VSX)
3235 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
3236 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
3237 rs6000_vector_align[V4SFmode] = align32;
3239 else if (TARGET_ALTIVEC)
3241 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
3242 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
3243 rs6000_vector_align[V4SFmode] = align32;
3246 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
3247 and stores. */
3248 if (TARGET_ALTIVEC)
3250 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
3251 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
3252 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
3253 rs6000_vector_align[V4SImode] = align32;
3254 rs6000_vector_align[V8HImode] = align32;
3255 rs6000_vector_align[V16QImode] = align32;
3257 if (TARGET_VSX)
3259 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
3260 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
3261 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
3263 else
3265 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
3266 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
3267 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
3271 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
3272 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
3273 if (TARGET_VSX)
3275 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
3276 rs6000_vector_unit[V2DImode]
3277 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3278 rs6000_vector_align[V2DImode] = align64;
3280 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
3281 rs6000_vector_unit[V1TImode]
3282 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3283 rs6000_vector_align[V1TImode] = 128;
3286 /* DFmode, see if we want to use the VSX unit. Memory is handled
3287 differently, so don't set rs6000_vector_mem. */
3288 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
3290 rs6000_vector_unit[DFmode] = VECTOR_VSX;
3291 rs6000_vector_align[DFmode] = 64;
3294 /* SFmode, see if we want to use the VSX unit. */
3295 if (TARGET_P8_VECTOR && TARGET_VSX_SCALAR_FLOAT)
3297 rs6000_vector_unit[SFmode] = VECTOR_VSX;
3298 rs6000_vector_align[SFmode] = 32;
3301 /* Allow TImode in VSX register and set the VSX memory macros. */
3302 if (TARGET_VSX && TARGET_VSX_TIMODE)
3304 rs6000_vector_mem[TImode] = VECTOR_VSX;
3305 rs6000_vector_align[TImode] = align64;
3308 /* TODO add SPE and paired floating point vector support. */
3310 /* Register class constraints for the constraints that depend on compile
3311 switches. When the VSX code was added, different constraints were added
3312 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
3313 of the VSX registers are used. The register classes for scalar floating
3314 point types is set, based on whether we allow that type into the upper
3315 (Altivec) registers. GCC has register classes to target the Altivec
3316 registers for load/store operations, to select using a VSX memory
3317 operation instead of the traditional floating point operation. The
3318 constraints are:
3320 d - Register class to use with traditional DFmode instructions.
3321 f - Register class to use with traditional SFmode instructions.
3322 v - Altivec register.
3323 wa - Any VSX register.
3324 wc - Reserved to represent individual CR bits (used in LLVM).
3325 wd - Preferred register class for V2DFmode.
3326 wf - Preferred register class for V4SFmode.
3327 wg - Float register for power6x move insns.
3328 wh - FP register for direct move instructions.
3329 wi - FP or VSX register to hold 64-bit integers for VSX insns.
3330 wj - FP or VSX register to hold 64-bit integers for direct moves.
3331 wk - FP or VSX register to hold 64-bit doubles for direct moves.
3332 wl - Float register if we can do 32-bit signed int loads.
3333 wm - VSX register for ISA 2.07 direct move operations.
3334 wn - always NO_REGS.
3335 wr - GPR if 64-bit mode is permitted.
3336 ws - Register class to do ISA 2.06 DF operations.
3337 wt - VSX register for TImode in VSX registers.
3338 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
3339 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
3340 ww - Register class to do SF conversions in with VSX operations.
3341 wx - Float register if we can do 32-bit int stores.
3342 wy - Register class to do ISA 2.07 SF operations.
3343 wz - Float register if we can do 32-bit unsigned int loads.
3344 wH - Altivec register if SImode is allowed in VSX registers.
3345 wI - VSX register if SImode is allowed in VSX registers.
3346 wJ - VSX register if QImode/HImode are allowed in VSX registers.
3347 wK - Altivec register if QImode/HImode are allowed in VSX registers. */
3349 if (TARGET_HARD_FLOAT && TARGET_FPRS)
3350 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS; /* SFmode */
3352 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
3353 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS; /* DFmode */
3355 if (TARGET_VSX)
3357 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
3358 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS; /* V2DFmode */
3359 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS; /* V4SFmode */
3361 if (TARGET_VSX_TIMODE)
3362 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS; /* TImode */
3364 if (TARGET_UPPER_REGS_DF) /* DFmode */
3366 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
3367 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
3369 else
3370 rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
3372 if (TARGET_UPPER_REGS_DI) /* DImode */
3373 rs6000_constraints[RS6000_CONSTRAINT_wi] = VSX_REGS;
3374 else
3375 rs6000_constraints[RS6000_CONSTRAINT_wi] = FLOAT_REGS;
3378 /* Add conditional constraints based on various options, to allow us to
3379 collapse multiple insn patterns. */
3380 if (TARGET_ALTIVEC)
3381 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
3383 if (TARGET_MFPGPR) /* DFmode */
3384 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
3386 if (TARGET_LFIWAX)
3387 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS; /* DImode */
3389 if (TARGET_DIRECT_MOVE)
3391 rs6000_constraints[RS6000_CONSTRAINT_wh] = FLOAT_REGS;
3392 rs6000_constraints[RS6000_CONSTRAINT_wj] /* DImode */
3393 = rs6000_constraints[RS6000_CONSTRAINT_wi];
3394 rs6000_constraints[RS6000_CONSTRAINT_wk] /* DFmode */
3395 = rs6000_constraints[RS6000_CONSTRAINT_ws];
3396 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
3399 if (TARGET_POWERPC64)
3401 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
3402 rs6000_constraints[RS6000_CONSTRAINT_wA] = BASE_REGS;
3405 if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF) /* SFmode */
3407 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
3408 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
3409 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
3411 else if (TARGET_P8_VECTOR)
3413 rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
3414 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
3416 else if (TARGET_VSX)
3417 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
3419 if (TARGET_STFIWX)
3420 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS; /* DImode */
3422 if (TARGET_LFIWZX)
3423 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS; /* DImode */
3425 if (TARGET_FLOAT128_TYPE)
3427 rs6000_constraints[RS6000_CONSTRAINT_wq] = VSX_REGS; /* KFmode */
3428 if (FLOAT128_IEEE_P (TFmode))
3429 rs6000_constraints[RS6000_CONSTRAINT_wp] = VSX_REGS; /* TFmode */
3432 /* Support for new D-form instructions. */
3433 if (TARGET_P9_DFORM_SCALAR)
3434 rs6000_constraints[RS6000_CONSTRAINT_wb] = ALTIVEC_REGS;
3436 /* Support for ISA 3.0 (power9) vectors. */
3437 if (TARGET_P9_VECTOR)
3438 rs6000_constraints[RS6000_CONSTRAINT_wo] = VSX_REGS;
3440 /* Support for new direct moves (ISA 3.0 + 64bit). */
3441 if (TARGET_DIRECT_MOVE_128)
3442 rs6000_constraints[RS6000_CONSTRAINT_we] = VSX_REGS;
3444 /* Support small integers in VSX registers. */
3445 if (TARGET_VSX_SMALL_INTEGER)
3447 rs6000_constraints[RS6000_CONSTRAINT_wH] = ALTIVEC_REGS;
3448 rs6000_constraints[RS6000_CONSTRAINT_wI] = FLOAT_REGS;
3449 if (TARGET_P9_VECTOR)
3451 rs6000_constraints[RS6000_CONSTRAINT_wJ] = FLOAT_REGS;
3452 rs6000_constraints[RS6000_CONSTRAINT_wK] = ALTIVEC_REGS;
3456 /* Set up the reload helper and direct move functions. */
3457 if (TARGET_VSX || TARGET_ALTIVEC)
3459 if (TARGET_64BIT)
3461 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
3462 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
3463 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
3464 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
3465 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
3466 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
3467 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
3468 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
3469 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
3470 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
3471 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
3472 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
3473 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
3474 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
3475 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
3476 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
3477 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
3478 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
3479 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
3480 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
3482 if (FLOAT128_VECTOR_P (KFmode))
3484 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_di_store;
3485 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_di_load;
3488 if (FLOAT128_VECTOR_P (TFmode))
3490 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_di_store;
3491 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_di_load;
3494 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3495 available. */
3496 if (TARGET_NO_SDMODE_STACK)
3498 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
3499 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
3502 if (TARGET_VSX_TIMODE)
3504 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
3505 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
3508 if (TARGET_DIRECT_MOVE && !TARGET_DIRECT_MOVE_128)
3510 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
3511 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
3512 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
3513 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
3514 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
3515 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
3516 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
3517 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
3518 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
3520 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
3521 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
3522 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
3523 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
3524 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
3525 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
3526 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
3527 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
3528 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
3530 if (FLOAT128_VECTOR_P (KFmode))
3532 reg_addr[KFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxkf;
3533 reg_addr[KFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprkf;
3536 if (FLOAT128_VECTOR_P (TFmode))
3538 reg_addr[TFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxtf;
3539 reg_addr[TFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprtf;
3543 else
3545 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
3546 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
3547 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
3548 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
3549 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
3550 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
3551 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
3552 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
3553 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
3554 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
3555 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
3556 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
3557 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
3558 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
3559 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
3560 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
3561 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
3562 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
3563 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
3564 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
3566 if (FLOAT128_VECTOR_P (KFmode))
3568 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_si_store;
3569 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_si_load;
3572 if (FLOAT128_IEEE_P (TFmode))
3574 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_si_store;
3575 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_si_load;
3578 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3579 available. */
3580 if (TARGET_NO_SDMODE_STACK)
3582 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
3583 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
3586 if (TARGET_VSX_TIMODE)
3588 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
3589 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
3592 if (TARGET_DIRECT_MOVE)
3594 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
3595 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
3596 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
3600 if (TARGET_UPPER_REGS_DF)
3601 reg_addr[DFmode].scalar_in_vmx_p = true;
3603 if (TARGET_UPPER_REGS_DI)
3604 reg_addr[DImode].scalar_in_vmx_p = true;
3606 if (TARGET_UPPER_REGS_SF)
3607 reg_addr[SFmode].scalar_in_vmx_p = true;
3609 if (TARGET_VSX_SMALL_INTEGER)
3611 reg_addr[SImode].scalar_in_vmx_p = true;
3612 if (TARGET_P9_VECTOR)
3614 reg_addr[HImode].scalar_in_vmx_p = true;
3615 reg_addr[QImode].scalar_in_vmx_p = true;
3620 /* Setup the fusion operations. */
3621 if (TARGET_P8_FUSION)
3623 reg_addr[QImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_qi;
3624 reg_addr[HImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_hi;
3625 reg_addr[SImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_si;
3626 if (TARGET_64BIT)
3627 reg_addr[DImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_di;
3630 if (TARGET_P9_FUSION)
3632 struct fuse_insns {
3633 enum machine_mode mode; /* mode of the fused type. */
3634 enum machine_mode pmode; /* pointer mode. */
3635 enum rs6000_reload_reg_type rtype; /* register type. */
3636 enum insn_code load; /* load insn. */
3637 enum insn_code store; /* store insn. */
3640 static const struct fuse_insns addis_insns[] = {
3641 { E_SFmode, E_DImode, RELOAD_REG_FPR,
3642 CODE_FOR_fusion_vsx_di_sf_load,
3643 CODE_FOR_fusion_vsx_di_sf_store },
3645 { E_SFmode, E_SImode, RELOAD_REG_FPR,
3646 CODE_FOR_fusion_vsx_si_sf_load,
3647 CODE_FOR_fusion_vsx_si_sf_store },
3649 { E_DFmode, E_DImode, RELOAD_REG_FPR,
3650 CODE_FOR_fusion_vsx_di_df_load,
3651 CODE_FOR_fusion_vsx_di_df_store },
3653 { E_DFmode, E_SImode, RELOAD_REG_FPR,
3654 CODE_FOR_fusion_vsx_si_df_load,
3655 CODE_FOR_fusion_vsx_si_df_store },
3657 { E_DImode, E_DImode, RELOAD_REG_FPR,
3658 CODE_FOR_fusion_vsx_di_di_load,
3659 CODE_FOR_fusion_vsx_di_di_store },
3661 { E_DImode, E_SImode, RELOAD_REG_FPR,
3662 CODE_FOR_fusion_vsx_si_di_load,
3663 CODE_FOR_fusion_vsx_si_di_store },
3665 { E_QImode, E_DImode, RELOAD_REG_GPR,
3666 CODE_FOR_fusion_gpr_di_qi_load,
3667 CODE_FOR_fusion_gpr_di_qi_store },
3669 { E_QImode, E_SImode, RELOAD_REG_GPR,
3670 CODE_FOR_fusion_gpr_si_qi_load,
3671 CODE_FOR_fusion_gpr_si_qi_store },
3673 { E_HImode, E_DImode, RELOAD_REG_GPR,
3674 CODE_FOR_fusion_gpr_di_hi_load,
3675 CODE_FOR_fusion_gpr_di_hi_store },
3677 { E_HImode, E_SImode, RELOAD_REG_GPR,
3678 CODE_FOR_fusion_gpr_si_hi_load,
3679 CODE_FOR_fusion_gpr_si_hi_store },
3681 { E_SImode, E_DImode, RELOAD_REG_GPR,
3682 CODE_FOR_fusion_gpr_di_si_load,
3683 CODE_FOR_fusion_gpr_di_si_store },
3685 { E_SImode, E_SImode, RELOAD_REG_GPR,
3686 CODE_FOR_fusion_gpr_si_si_load,
3687 CODE_FOR_fusion_gpr_si_si_store },
3689 { E_SFmode, E_DImode, RELOAD_REG_GPR,
3690 CODE_FOR_fusion_gpr_di_sf_load,
3691 CODE_FOR_fusion_gpr_di_sf_store },
3693 { E_SFmode, E_SImode, RELOAD_REG_GPR,
3694 CODE_FOR_fusion_gpr_si_sf_load,
3695 CODE_FOR_fusion_gpr_si_sf_store },
3697 { E_DImode, E_DImode, RELOAD_REG_GPR,
3698 CODE_FOR_fusion_gpr_di_di_load,
3699 CODE_FOR_fusion_gpr_di_di_store },
3701 { E_DFmode, E_DImode, RELOAD_REG_GPR,
3702 CODE_FOR_fusion_gpr_di_df_load,
3703 CODE_FOR_fusion_gpr_di_df_store },
3706 machine_mode cur_pmode = Pmode;
3707 size_t i;
3709 for (i = 0; i < ARRAY_SIZE (addis_insns); i++)
3711 machine_mode xmode = addis_insns[i].mode;
3712 enum rs6000_reload_reg_type rtype = addis_insns[i].rtype;
3714 if (addis_insns[i].pmode != cur_pmode)
3715 continue;
3717 if (rtype == RELOAD_REG_FPR
3718 && (!TARGET_HARD_FLOAT || !TARGET_FPRS))
3719 continue;
3721 reg_addr[xmode].fusion_addis_ld[rtype] = addis_insns[i].load;
3722 reg_addr[xmode].fusion_addis_st[rtype] = addis_insns[i].store;
3724 if (rtype == RELOAD_REG_FPR && TARGET_P9_DFORM_SCALAR)
3726 reg_addr[xmode].fusion_addis_ld[RELOAD_REG_VMX]
3727 = addis_insns[i].load;
3728 reg_addr[xmode].fusion_addis_st[RELOAD_REG_VMX]
3729 = addis_insns[i].store;
3734 /* Note which types we support fusing TOC setup plus memory insn. We only do
3735 fused TOCs for medium/large code models. */
3736 if (TARGET_P8_FUSION && TARGET_TOC_FUSION && TARGET_POWERPC64
3737 && (TARGET_CMODEL != CMODEL_SMALL))
3739 reg_addr[QImode].fused_toc = true;
3740 reg_addr[HImode].fused_toc = true;
3741 reg_addr[SImode].fused_toc = true;
3742 reg_addr[DImode].fused_toc = true;
3743 if (TARGET_HARD_FLOAT && TARGET_FPRS)
3745 if (TARGET_SINGLE_FLOAT)
3746 reg_addr[SFmode].fused_toc = true;
3747 if (TARGET_DOUBLE_FLOAT)
3748 reg_addr[DFmode].fused_toc = true;
3752 /* Precalculate HARD_REGNO_NREGS. */
3753 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
3754 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3755 rs6000_hard_regno_nregs[m][r]
3756 = rs6000_hard_regno_nregs_internal (r, (machine_mode)m);
3758 /* Precalculate TARGET_HARD_REGNO_MODE_OK. */
3759 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
3760 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3761 if (rs6000_hard_regno_mode_ok_uncached (r, (machine_mode)m))
3762 rs6000_hard_regno_mode_ok_p[m][r] = true;
3764 /* Precalculate CLASS_MAX_NREGS sizes. */
3765 for (c = 0; c < LIM_REG_CLASSES; ++c)
3767 int reg_size;
3769 if (TARGET_VSX && VSX_REG_CLASS_P (c))
3770 reg_size = UNITS_PER_VSX_WORD;
3772 else if (c == ALTIVEC_REGS)
3773 reg_size = UNITS_PER_ALTIVEC_WORD;
3775 else if (c == FLOAT_REGS)
3776 reg_size = UNITS_PER_FP_WORD;
3778 else
3779 reg_size = UNITS_PER_WORD;
3781 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3783 machine_mode m2 = (machine_mode)m;
3784 int reg_size2 = reg_size;
3786 /* TDmode & IBM 128-bit floating point always takes 2 registers, even
3787 in VSX. */
3788 if (TARGET_VSX && VSX_REG_CLASS_P (c) && FLOAT128_2REG_P (m))
3789 reg_size2 = UNITS_PER_FP_WORD;
3791 rs6000_class_max_nregs[m][c]
3792 = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
3796 if (TARGET_E500_DOUBLE)
3797 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
3799 /* Calculate which modes to automatically generate code to use a the
3800 reciprocal divide and square root instructions. In the future, possibly
3801 automatically generate the instructions even if the user did not specify
3802 -mrecip. The older machines double precision reciprocal sqrt estimate is
3803 not accurate enough. */
3804 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
3805 if (TARGET_FRES)
3806 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3807 if (TARGET_FRE)
3808 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3809 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3810 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3811 if (VECTOR_UNIT_VSX_P (V2DFmode))
3812 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3814 if (TARGET_FRSQRTES)
3815 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3816 if (TARGET_FRSQRTE)
3817 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3818 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3819 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3820 if (VECTOR_UNIT_VSX_P (V2DFmode))
3821 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3823 if (rs6000_recip_control)
3825 if (!flag_finite_math_only)
3826 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
3827 if (flag_trapping_math)
3828 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
3829 if (!flag_reciprocal_math)
3830 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
3831 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
3833 if (RS6000_RECIP_HAVE_RE_P (SFmode)
3834 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
3835 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3837 if (RS6000_RECIP_HAVE_RE_P (DFmode)
3838 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
3839 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3841 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
3842 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
3843 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3845 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
3846 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
3847 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3849 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
3850 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
3851 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3853 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
3854 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
3855 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3857 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
3858 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
3859 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3861 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
3862 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
3863 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3867 /* Update the addr mask bits in reg_addr to help secondary reload and go if
3868 legitimate address support to figure out the appropriate addressing to
3869 use. */
3870 rs6000_setup_reg_addr_masks ();
3872 if (global_init_p || TARGET_DEBUG_TARGET)
3874 if (TARGET_DEBUG_REG)
3875 rs6000_debug_reg_global ();
3877 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
3878 fprintf (stderr,
3879 "SImode variable mult cost = %d\n"
3880 "SImode constant mult cost = %d\n"
3881 "SImode short constant mult cost = %d\n"
3882 "DImode multipliciation cost = %d\n"
3883 "SImode division cost = %d\n"
3884 "DImode division cost = %d\n"
3885 "Simple fp operation cost = %d\n"
3886 "DFmode multiplication cost = %d\n"
3887 "SFmode division cost = %d\n"
3888 "DFmode division cost = %d\n"
3889 "cache line size = %d\n"
3890 "l1 cache size = %d\n"
3891 "l2 cache size = %d\n"
3892 "simultaneous prefetches = %d\n"
3893 "\n",
3894 rs6000_cost->mulsi,
3895 rs6000_cost->mulsi_const,
3896 rs6000_cost->mulsi_const9,
3897 rs6000_cost->muldi,
3898 rs6000_cost->divsi,
3899 rs6000_cost->divdi,
3900 rs6000_cost->fp,
3901 rs6000_cost->dmul,
3902 rs6000_cost->sdiv,
3903 rs6000_cost->ddiv,
3904 rs6000_cost->cache_line_size,
3905 rs6000_cost->l1_cache_size,
3906 rs6000_cost->l2_cache_size,
3907 rs6000_cost->simultaneous_prefetches);
3911 #if TARGET_MACHO
3912 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
3914 static void
3915 darwin_rs6000_override_options (void)
3917 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
3918 off. */
3919 rs6000_altivec_abi = 1;
3920 TARGET_ALTIVEC_VRSAVE = 1;
3921 rs6000_current_abi = ABI_DARWIN;
3923 if (DEFAULT_ABI == ABI_DARWIN
3924 && TARGET_64BIT)
3925 darwin_one_byte_bool = 1;
3927 if (TARGET_64BIT && ! TARGET_POWERPC64)
3929 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
3930 warning (0, "-m64 requires PowerPC64 architecture, enabling");
3932 if (flag_mkernel)
3934 rs6000_default_long_calls = 1;
3935 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
3938 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
3939 Altivec. */
3940 if (!flag_mkernel && !flag_apple_kext
3941 && TARGET_64BIT
3942 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3943 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3945 /* Unless the user (not the configurer) has explicitly overridden
3946 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3947 G4 unless targeting the kernel. */
3948 if (!flag_mkernel
3949 && !flag_apple_kext
3950 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3951 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3952 && ! global_options_set.x_rs6000_cpu_index)
3954 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3957 #endif
3959 /* If not otherwise specified by a target, make 'long double' equivalent to
3960 'double'. */
3962 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3963 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3964 #endif
3966 /* Return the builtin mask of the various options used that could affect which
3967 builtins were used. In the past we used target_flags, but we've run out of
3968 bits, and some options like SPE and PAIRED are no longer in
3969 target_flags. */
3971 HOST_WIDE_INT
3972 rs6000_builtin_mask_calculate (void)
3974 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3975 | ((TARGET_CMPB) ? RS6000_BTM_CMPB : 0)
3976 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3977 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
3978 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
3979 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3980 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3981 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3982 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3983 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3984 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3985 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3986 | ((TARGET_P9_VECTOR) ? RS6000_BTM_P9_VECTOR : 0)
3987 | ((TARGET_P9_MISC) ? RS6000_BTM_P9_MISC : 0)
3988 | ((TARGET_MODULO) ? RS6000_BTM_MODULO : 0)
3989 | ((TARGET_64BIT) ? RS6000_BTM_64BIT : 0)
3990 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3991 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0)
3992 | ((TARGET_DFP) ? RS6000_BTM_DFP : 0)
3993 | ((TARGET_HARD_FLOAT) ? RS6000_BTM_HARD_FLOAT : 0)
3994 | ((TARGET_LONG_DOUBLE_128) ? RS6000_BTM_LDBL128 : 0)
3995 | ((TARGET_FLOAT128_TYPE) ? RS6000_BTM_FLOAT128 : 0));
3998 /* Implement TARGET_MD_ASM_ADJUST. All asm statements are considered
3999 to clobber the XER[CA] bit because clobbering that bit without telling
4000 the compiler worked just fine with versions of GCC before GCC 5, and
4001 breaking a lot of older code in ways that are hard to track down is
4002 not such a great idea. */
4004 static rtx_insn *
4005 rs6000_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
4006 vec<const char *> &/*constraints*/,
4007 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
4009 clobbers.safe_push (gen_rtx_REG (SImode, CA_REGNO));
4010 SET_HARD_REG_BIT (clobbered_regs, CA_REGNO);
4011 return NULL;
4014 /* Override command line options.
4016 Combine build-specific configuration information with options
4017 specified on the command line to set various state variables which
4018 influence code generation, optimization, and expansion of built-in
4019 functions. Assure that command-line configuration preferences are
4020 compatible with each other and with the build configuration; issue
4021 warnings while adjusting configuration or error messages while
4022 rejecting configuration.
4024 Upon entry to this function:
4026 This function is called once at the beginning of
4027 compilation, and then again at the start and end of compiling
4028 each section of code that has a different configuration, as
4029 indicated, for example, by adding the
4031 __attribute__((__target__("cpu=power9")))
4033 qualifier to a function definition or, for example, by bracketing
4034 code between
4036 #pragma GCC target("altivec")
4040 #pragma GCC reset_options
4042 directives. Parameter global_init_p is true for the initial
4043 invocation, which initializes global variables, and false for all
4044 subsequent invocations.
4047 Various global state information is assumed to be valid. This
4048 includes OPTION_TARGET_CPU_DEFAULT, representing the name of the
4049 default CPU specified at build configure time, TARGET_DEFAULT,
4050 representing the default set of option flags for the default
4051 target, and global_options_set.x_rs6000_isa_flags, representing
4052 which options were requested on the command line.
4054 Upon return from this function:
4056 rs6000_isa_flags_explicit has a non-zero bit for each flag that
4057 was set by name on the command line. Additionally, if certain
4058 attributes are automatically enabled or disabled by this function
4059 in order to assure compatibility between options and
4060 configuration, the flags associated with those attributes are
4061 also set. By setting these "explicit bits", we avoid the risk
4062 that other code might accidentally overwrite these particular
4063 attributes with "default values".
4065 The various bits of rs6000_isa_flags are set to indicate the
4066 target options that have been selected for the most current
4067 compilation efforts. This has the effect of also turning on the
4068 associated TARGET_XXX values since these are macros which are
4069 generally defined to test the corresponding bit of the
4070 rs6000_isa_flags variable.
4072 The variable rs6000_builtin_mask is set to represent the target
4073 options for the most current compilation efforts, consistent with
4074 the current contents of rs6000_isa_flags. This variable controls
4075 expansion of built-in functions.
4077 Various other global variables and fields of global structures
4078 (over 50 in all) are initialized to reflect the desired options
4079 for the most current compilation efforts. */
4081 static bool
4082 rs6000_option_override_internal (bool global_init_p)
4084 bool ret = true;
4085 bool have_cpu = false;
4087 /* The default cpu requested at configure time, if any. */
4088 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
4090 HOST_WIDE_INT set_masks;
4091 HOST_WIDE_INT ignore_masks;
4092 int cpu_index;
4093 int tune_index;
4094 struct cl_target_option *main_target_opt
4095 = ((global_init_p || target_option_default_node == NULL)
4096 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
4098 /* Print defaults. */
4099 if ((TARGET_DEBUG_REG || TARGET_DEBUG_TARGET) && global_init_p)
4100 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
4102 /* Remember the explicit arguments. */
4103 if (global_init_p)
4104 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
4106 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
4107 library functions, so warn about it. The flag may be useful for
4108 performance studies from time to time though, so don't disable it
4109 entirely. */
4110 if (global_options_set.x_rs6000_alignment_flags
4111 && rs6000_alignment_flags == MASK_ALIGN_POWER
4112 && DEFAULT_ABI == ABI_DARWIN
4113 && TARGET_64BIT)
4114 warning (0, "-malign-power is not supported for 64-bit Darwin;"
4115 " it is incompatible with the installed C and C++ libraries");
4117 /* Numerous experiment shows that IRA based loop pressure
4118 calculation works better for RTL loop invariant motion on targets
4119 with enough (>= 32) registers. It is an expensive optimization.
4120 So it is on only for peak performance. */
4121 if (optimize >= 3 && global_init_p
4122 && !global_options_set.x_flag_ira_loop_pressure)
4123 flag_ira_loop_pressure = 1;
4125 /* -fsanitize=address needs to turn on -fasynchronous-unwind-tables in order
4126 for tracebacks to be complete but not if any -fasynchronous-unwind-tables
4127 options were already specified. */
4128 if (flag_sanitize & SANITIZE_USER_ADDRESS
4129 && !global_options_set.x_flag_asynchronous_unwind_tables)
4130 flag_asynchronous_unwind_tables = 1;
4132 /* Set the pointer size. */
4133 if (TARGET_64BIT)
4135 rs6000_pmode = DImode;
4136 rs6000_pointer_size = 64;
4138 else
4140 rs6000_pmode = SImode;
4141 rs6000_pointer_size = 32;
4144 /* Some OSs don't support saving the high part of 64-bit registers on context
4145 switch. Other OSs don't support saving Altivec registers. On those OSs,
4146 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
4147 if the user wants either, the user must explicitly specify them and we
4148 won't interfere with the user's specification. */
4150 set_masks = POWERPC_MASKS;
4151 #ifdef OS_MISSING_POWERPC64
4152 if (OS_MISSING_POWERPC64)
4153 set_masks &= ~OPTION_MASK_POWERPC64;
4154 #endif
4155 #ifdef OS_MISSING_ALTIVEC
4156 if (OS_MISSING_ALTIVEC)
4157 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX
4158 | OTHER_VSX_VECTOR_MASKS);
4159 #endif
4161 /* Don't override by the processor default if given explicitly. */
4162 set_masks &= ~rs6000_isa_flags_explicit;
4164 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
4165 the cpu in a target attribute or pragma, but did not specify a tuning
4166 option, use the cpu for the tuning option rather than the option specified
4167 with -mtune on the command line. Process a '--with-cpu' configuration
4168 request as an implicit --cpu. */
4169 if (rs6000_cpu_index >= 0)
4171 cpu_index = rs6000_cpu_index;
4172 have_cpu = true;
4174 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
4176 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
4177 have_cpu = true;
4179 else if (implicit_cpu)
4181 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
4182 have_cpu = true;
4184 else
4186 /* PowerPC 64-bit LE requires at least ISA 2.07. */
4187 const char *default_cpu = ((!TARGET_POWERPC64)
4188 ? "powerpc"
4189 : ((BYTES_BIG_ENDIAN)
4190 ? "powerpc64"
4191 : "powerpc64le"));
4193 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
4194 have_cpu = false;
4197 gcc_assert (cpu_index >= 0);
4199 if (have_cpu)
4201 #ifndef HAVE_AS_POWER9
4202 if (processor_target_table[rs6000_cpu_index].processor
4203 == PROCESSOR_POWER9)
4205 have_cpu = false;
4206 warning (0, "will not generate power9 instructions because "
4207 "assembler lacks power9 support");
4209 #endif
4210 #ifndef HAVE_AS_POWER8
4211 if (processor_target_table[rs6000_cpu_index].processor
4212 == PROCESSOR_POWER8)
4214 have_cpu = false;
4215 warning (0, "will not generate power8 instructions because "
4216 "assembler lacks power8 support");
4218 #endif
4219 #ifndef HAVE_AS_POPCNTD
4220 if (processor_target_table[rs6000_cpu_index].processor
4221 == PROCESSOR_POWER7)
4223 have_cpu = false;
4224 warning (0, "will not generate power7 instructions because "
4225 "assembler lacks power7 support");
4227 #endif
4228 #ifndef HAVE_AS_DFP
4229 if (processor_target_table[rs6000_cpu_index].processor
4230 == PROCESSOR_POWER6)
4232 have_cpu = false;
4233 warning (0, "will not generate power6 instructions because "
4234 "assembler lacks power6 support");
4236 #endif
4237 #ifndef HAVE_AS_POPCNTB
4238 if (processor_target_table[rs6000_cpu_index].processor
4239 == PROCESSOR_POWER5)
4241 have_cpu = false;
4242 warning (0, "will not generate power5 instructions because "
4243 "assembler lacks power5 support");
4245 #endif
4247 if (!have_cpu)
4249 /* PowerPC 64-bit LE requires at least ISA 2.07. */
4250 const char *default_cpu = (!TARGET_POWERPC64
4251 ? "powerpc"
4252 : (BYTES_BIG_ENDIAN
4253 ? "powerpc64"
4254 : "powerpc64le"));
4256 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
4260 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
4261 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
4262 with those from the cpu, except for options that were explicitly set. If
4263 we don't have a cpu, do not override the target bits set in
4264 TARGET_DEFAULT. */
4265 if (have_cpu)
4267 rs6000_isa_flags &= ~set_masks;
4268 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
4269 & set_masks);
4271 else
4273 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
4274 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
4275 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
4276 to using rs6000_isa_flags, we need to do the initialization here.
4278 If there is a TARGET_DEFAULT, use that. Otherwise fall back to using
4279 -mcpu=powerpc, -mcpu=powerpc64, or -mcpu=powerpc64le defaults. */
4280 HOST_WIDE_INT flags = ((TARGET_DEFAULT) ? TARGET_DEFAULT
4281 : processor_target_table[cpu_index].target_enable);
4282 rs6000_isa_flags |= (flags & ~rs6000_isa_flags_explicit);
4285 if (rs6000_tune_index >= 0)
4286 tune_index = rs6000_tune_index;
4287 else if (have_cpu)
4288 rs6000_tune_index = tune_index = cpu_index;
4289 else
4291 size_t i;
4292 enum processor_type tune_proc
4293 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
4295 tune_index = -1;
4296 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
4297 if (processor_target_table[i].processor == tune_proc)
4299 rs6000_tune_index = tune_index = i;
4300 break;
4304 gcc_assert (tune_index >= 0);
4305 rs6000_cpu = processor_target_table[tune_index].processor;
4307 /* Pick defaults for SPE related control flags. Do this early to make sure
4308 that the TARGET_ macros are representative ASAP. */
4310 int spe_capable_cpu =
4311 (rs6000_cpu == PROCESSOR_PPC8540
4312 || rs6000_cpu == PROCESSOR_PPC8548);
4314 if (!global_options_set.x_rs6000_spe_abi)
4315 rs6000_spe_abi = spe_capable_cpu;
4317 if (!global_options_set.x_rs6000_spe)
4318 rs6000_spe = spe_capable_cpu;
4320 if (!global_options_set.x_rs6000_float_gprs)
4321 rs6000_float_gprs =
4322 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
4323 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
4324 : 0);
4327 if (global_options_set.x_rs6000_spe_abi
4328 && rs6000_spe_abi
4329 && !TARGET_SPE_ABI)
4330 error ("not configured for SPE ABI");
4332 if (global_options_set.x_rs6000_spe
4333 && rs6000_spe
4334 && !TARGET_SPE)
4335 error ("not configured for SPE instruction set");
4337 if (main_target_opt != NULL
4338 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
4339 || (main_target_opt->x_rs6000_spe != rs6000_spe)
4340 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
4341 error ("target attribute or pragma changes SPE ABI");
4343 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
4344 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
4345 || rs6000_cpu == PROCESSOR_PPCE5500)
4347 if (TARGET_ALTIVEC)
4348 error ("AltiVec not supported in this target");
4349 if (TARGET_SPE)
4350 error ("SPE not supported in this target");
4352 if (rs6000_cpu == PROCESSOR_PPCE6500)
4354 if (TARGET_SPE)
4355 error ("SPE not supported in this target");
4358 /* Disable Cell microcode if we are optimizing for the Cell
4359 and not optimizing for size. */
4360 if (rs6000_gen_cell_microcode == -1)
4361 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
4362 && !optimize_size);
4364 /* If we are optimizing big endian systems for space and it's OK to
4365 use instructions that would be microcoded on the Cell, use the
4366 load/store multiple and string instructions. */
4367 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
4368 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
4369 | OPTION_MASK_STRING);
4371 /* Don't allow -mmultiple or -mstring on little endian systems
4372 unless the cpu is a 750, because the hardware doesn't support the
4373 instructions used in little endian mode, and causes an alignment
4374 trap. The 750 does not cause an alignment trap (except when the
4375 target is unaligned). */
4377 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
4379 if (TARGET_MULTIPLE)
4381 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
4382 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
4383 warning (0, "-mmultiple is not supported on little endian systems");
4386 if (TARGET_STRING)
4388 rs6000_isa_flags &= ~OPTION_MASK_STRING;
4389 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
4390 warning (0, "-mstring is not supported on little endian systems");
4394 /* If little-endian, default to -mstrict-align on older processors.
4395 Testing for htm matches power8 and later. */
4396 if (!BYTES_BIG_ENDIAN
4397 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
4398 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
4400 /* -maltivec={le,be} implies -maltivec. */
4401 if (rs6000_altivec_element_order != 0)
4402 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
4404 /* Disallow -maltivec=le in big endian mode for now. This is not
4405 known to be useful for anyone. */
4406 if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
4408 warning (0, N_("-maltivec=le not allowed for big-endian targets"));
4409 rs6000_altivec_element_order = 0;
4412 /* Add some warnings for VSX. */
4413 if (TARGET_VSX)
4415 const char *msg = NULL;
4416 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
4417 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
4419 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4420 msg = N_("-mvsx requires hardware floating point");
4421 else
4423 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
4424 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4427 else if (TARGET_PAIRED_FLOAT)
4428 msg = N_("-mvsx and -mpaired are incompatible");
4429 else if (TARGET_AVOID_XFORM > 0)
4430 msg = N_("-mvsx needs indexed addressing");
4431 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
4432 & OPTION_MASK_ALTIVEC))
4434 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4435 msg = N_("-mvsx and -mno-altivec are incompatible");
4436 else
4437 msg = N_("-mno-altivec disables vsx");
4440 if (msg)
4442 warning (0, msg);
4443 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
4444 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4448 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
4449 the -mcpu setting to enable options that conflict. */
4450 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
4451 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
4452 | OPTION_MASK_ALTIVEC
4453 | OPTION_MASK_VSX)) != 0)
4454 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
4455 | OPTION_MASK_DIRECT_MOVE)
4456 & ~rs6000_isa_flags_explicit);
4458 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4459 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
4461 /* Handle explicit -mno-{altivec,vsx,power8-vector,power9-vector} and turn
4462 off all of the options that depend on those flags. */
4463 ignore_masks = rs6000_disable_incompatible_switches ();
4465 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
4466 unless the user explicitly used the -mno-<option> to disable the code. */
4467 if (TARGET_P9_VECTOR || TARGET_MODULO || TARGET_P9_DFORM_SCALAR
4468 || TARGET_P9_DFORM_VECTOR || TARGET_P9_DFORM_BOTH > 0)
4469 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
4470 else if (TARGET_P9_MINMAX)
4472 if (have_cpu)
4474 if (cpu_index == PROCESSOR_POWER9)
4476 /* legacy behavior: allow -mcpu-power9 with certain
4477 capabilities explicitly disabled. */
4478 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
4479 /* However, reject this automatic fix if certain
4480 capabilities required for TARGET_P9_MINMAX support
4481 have been explicitly disabled. */
4482 if (((OPTION_MASK_VSX | OPTION_MASK_UPPER_REGS_SF
4483 | OPTION_MASK_UPPER_REGS_DF) & rs6000_isa_flags)
4484 != (OPTION_MASK_VSX | OPTION_MASK_UPPER_REGS_SF
4485 | OPTION_MASK_UPPER_REGS_DF))
4486 error ("-mpower9-minmax incompatible with explicitly disabled options");
4488 else
4489 error ("Power9 target option is incompatible with -mcpu=<xxx> for "
4490 "<xxx> less than power9");
4492 else if ((ISA_3_0_MASKS_SERVER & rs6000_isa_flags_explicit)
4493 != (ISA_3_0_MASKS_SERVER & rs6000_isa_flags
4494 & rs6000_isa_flags_explicit))
4495 /* Enforce that none of the ISA_3_0_MASKS_SERVER flags
4496 were explicitly cleared. */
4497 error ("-mpower9-minmax incompatible with explicitly disabled options");
4498 else
4499 rs6000_isa_flags |= ISA_3_0_MASKS_SERVER;
4501 else if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
4502 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~ignore_masks);
4503 else if (TARGET_VSX)
4504 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~ignore_masks);
4505 else if (TARGET_POPCNTD)
4506 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~ignore_masks);
4507 else if (TARGET_DFP)
4508 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~ignore_masks);
4509 else if (TARGET_CMPB)
4510 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~ignore_masks);
4511 else if (TARGET_FPRND)
4512 rs6000_isa_flags |= (ISA_2_4_MASKS & ~ignore_masks);
4513 else if (TARGET_POPCNTB)
4514 rs6000_isa_flags |= (ISA_2_2_MASKS & ~ignore_masks);
4515 else if (TARGET_ALTIVEC)
4516 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~ignore_masks);
4518 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
4520 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
4521 error ("-mcrypto requires -maltivec");
4522 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
4525 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
4527 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
4528 error ("-mdirect-move requires -mvsx");
4529 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
4532 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
4534 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4535 error ("-mpower8-vector requires -maltivec");
4536 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4539 if (TARGET_P8_VECTOR && !TARGET_VSX)
4541 if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4542 && (rs6000_isa_flags_explicit & OPTION_MASK_VSX))
4543 error ("-mpower8-vector requires -mvsx");
4544 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR) == 0)
4546 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4547 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4548 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4550 else
4552 /* OPTION_MASK_P8_VECTOR is explicit, and OPTION_MASK_VSX is
4553 not explicit. */
4554 rs6000_isa_flags |= OPTION_MASK_VSX;
4555 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4559 if (TARGET_VSX_TIMODE && !TARGET_VSX)
4561 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
4562 error ("-mvsx-timode requires -mvsx");
4563 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
4566 if (TARGET_DFP && !TARGET_HARD_FLOAT)
4568 if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
4569 error ("-mhard-dfp requires -mhard-float");
4570 rs6000_isa_flags &= ~OPTION_MASK_DFP;
4573 /* Allow an explicit -mupper-regs to set -mupper-regs-df, -mupper-regs-di,
4574 and -mupper-regs-sf, depending on the cpu, unless the user explicitly also
4575 set the individual option. */
4576 if (TARGET_UPPER_REGS > 0)
4578 if (TARGET_VSX
4579 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF))
4581 rs6000_isa_flags |= OPTION_MASK_UPPER_REGS_DF;
4582 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DF;
4584 if (TARGET_VSX
4585 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DI))
4587 rs6000_isa_flags |= OPTION_MASK_UPPER_REGS_DI;
4588 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DI;
4590 if (TARGET_P8_VECTOR
4591 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF))
4593 rs6000_isa_flags |= OPTION_MASK_UPPER_REGS_SF;
4594 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_SF;
4597 else if (TARGET_UPPER_REGS == 0)
4599 if (TARGET_VSX
4600 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF))
4602 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DF;
4603 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DF;
4605 if (TARGET_VSX
4606 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DI))
4608 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DI;
4609 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DI;
4611 if (TARGET_P8_VECTOR
4612 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF))
4614 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_SF;
4615 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_SF;
4619 if (TARGET_UPPER_REGS_DF && !TARGET_VSX)
4621 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF)
4622 error ("-mupper-regs-df requires -mvsx");
4623 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DF;
4626 if (TARGET_UPPER_REGS_DI && !TARGET_VSX)
4628 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DI)
4629 error ("-mupper-regs-di requires -mvsx");
4630 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DI;
4633 if (TARGET_UPPER_REGS_SF && !TARGET_P8_VECTOR)
4635 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF)
4636 error ("-mupper-regs-sf requires -mpower8-vector");
4637 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_SF;
4640 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
4641 silently turn off quad memory mode. */
4642 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
4644 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4645 warning (0, N_("-mquad-memory requires 64-bit mode"));
4647 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
4648 warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
4650 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
4651 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
4654 /* Non-atomic quad memory load/store are disabled for little endian, since
4655 the words are reversed, but atomic operations can still be done by
4656 swapping the words. */
4657 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
4659 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4660 warning (0, N_("-mquad-memory is not available in little endian mode"));
4662 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
4665 /* Assume if the user asked for normal quad memory instructions, they want
4666 the atomic versions as well, unless they explicity told us not to use quad
4667 word atomic instructions. */
4668 if (TARGET_QUAD_MEMORY
4669 && !TARGET_QUAD_MEMORY_ATOMIC
4670 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
4671 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
4673 /* Enable power8 fusion if we are tuning for power8, even if we aren't
4674 generating power8 instructions. */
4675 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
4676 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
4677 & OPTION_MASK_P8_FUSION);
4679 /* Setting additional fusion flags turns on base fusion. */
4680 if (!TARGET_P8_FUSION && (TARGET_P8_FUSION_SIGN || TARGET_TOC_FUSION))
4682 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4684 if (TARGET_P8_FUSION_SIGN)
4685 error ("-mpower8-fusion-sign requires -mpower8-fusion");
4687 if (TARGET_TOC_FUSION)
4688 error ("-mtoc-fusion requires -mpower8-fusion");
4690 rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
4692 else
4693 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4696 /* Power9 fusion is a superset over power8 fusion. */
4697 if (TARGET_P9_FUSION && !TARGET_P8_FUSION)
4699 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4701 /* We prefer to not mention undocumented options in
4702 error messages. However, if users have managed to select
4703 power9-fusion without selecting power8-fusion, they
4704 already know about undocumented flags. */
4705 error ("-mpower9-fusion requires -mpower8-fusion");
4706 rs6000_isa_flags &= ~OPTION_MASK_P9_FUSION;
4708 else
4709 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4712 /* Enable power9 fusion if we are tuning for power9, even if we aren't
4713 generating power9 instructions. */
4714 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_FUSION))
4715 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
4716 & OPTION_MASK_P9_FUSION);
4718 /* Power8 does not fuse sign extended loads with the addis. If we are
4719 optimizing at high levels for speed, convert a sign extended load into a
4720 zero extending load, and an explicit sign extension. */
4721 if (TARGET_P8_FUSION
4722 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
4723 && optimize_function_for_speed_p (cfun)
4724 && optimize >= 3)
4725 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
4727 /* TOC fusion requires 64-bit and medium/large code model. */
4728 if (TARGET_TOC_FUSION && !TARGET_POWERPC64)
4730 rs6000_isa_flags &= ~OPTION_MASK_TOC_FUSION;
4731 if ((rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION) != 0)
4732 warning (0, N_("-mtoc-fusion requires 64-bit"));
4735 if (TARGET_TOC_FUSION && (TARGET_CMODEL == CMODEL_SMALL))
4737 rs6000_isa_flags &= ~OPTION_MASK_TOC_FUSION;
4738 if ((rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION) != 0)
4739 warning (0, N_("-mtoc-fusion requires medium/large code model"));
4742 /* Turn on -mtoc-fusion by default if p8-fusion and 64-bit medium/large code
4743 model. */
4744 if (TARGET_P8_FUSION && !TARGET_TOC_FUSION && TARGET_POWERPC64
4745 && (TARGET_CMODEL != CMODEL_SMALL)
4746 && !(rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION))
4747 rs6000_isa_flags |= OPTION_MASK_TOC_FUSION;
4749 /* ISA 3.0 vector instructions include ISA 2.07. */
4750 if (TARGET_P9_VECTOR && !TARGET_P8_VECTOR)
4752 /* We prefer to not mention undocumented options in
4753 error messages. However, if users have managed to select
4754 power9-vector without selecting power8-vector, they
4755 already know about undocumented flags. */
4756 if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) &&
4757 (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR))
4758 error ("-mpower9-vector requires -mpower8-vector");
4759 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) == 0)
4761 rs6000_isa_flags &= ~OPTION_MASK_P9_VECTOR;
4762 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4763 rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
4765 else
4767 /* OPTION_MASK_P9_VECTOR is explicit and
4768 OPTION_MASK_P8_VECTOR is not explicit. */
4769 rs6000_isa_flags |= OPTION_MASK_P8_VECTOR;
4770 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4774 /* -mpower9-dform turns on both -mpower9-dform-scalar and
4775 -mpower9-dform-vector. */
4776 if (TARGET_P9_DFORM_BOTH > 0)
4778 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_VECTOR))
4779 rs6000_isa_flags |= OPTION_MASK_P9_DFORM_VECTOR;
4781 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_SCALAR))
4782 rs6000_isa_flags |= OPTION_MASK_P9_DFORM_SCALAR;
4784 else if (TARGET_P9_DFORM_BOTH == 0)
4786 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_VECTOR))
4787 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_VECTOR;
4789 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_SCALAR))
4790 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_SCALAR;
4793 /* ISA 3.0 D-form instructions require p9-vector and upper-regs. */
4794 if ((TARGET_P9_DFORM_SCALAR || TARGET_P9_DFORM_VECTOR) && !TARGET_P9_VECTOR)
4796 /* We prefer to not mention undocumented options in
4797 error messages. However, if users have managed to select
4798 power9-dform without selecting power9-vector, they
4799 already know about undocumented flags. */
4800 if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR)
4801 && (rs6000_isa_flags_explicit & (OPTION_MASK_P9_DFORM_SCALAR
4802 | OPTION_MASK_P9_DFORM_VECTOR)))
4803 error ("-mpower9-dform requires -mpower9-vector");
4804 else if (rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR)
4806 rs6000_isa_flags &=
4807 ~(OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4808 rs6000_isa_flags_explicit |=
4809 (OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4811 else
4813 /* We know that OPTION_MASK_P9_VECTOR is not explicit and
4814 OPTION_MASK_P9_DFORM_SCALAR or OPTION_MASK_P9_DORM_VECTOR
4815 may be explicit. */
4816 rs6000_isa_flags |= OPTION_MASK_P9_VECTOR;
4817 rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
4821 if ((TARGET_P9_DFORM_SCALAR || TARGET_P9_DFORM_VECTOR)
4822 && !TARGET_DIRECT_MOVE)
4824 /* We prefer to not mention undocumented options in
4825 error messages. However, if users have managed to select
4826 power9-dform without selecting direct-move, they
4827 already know about undocumented flags. */
4828 if ((rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
4829 && ((rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_VECTOR) ||
4830 (rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_SCALAR) ||
4831 (TARGET_P9_DFORM_BOTH == 1)))
4832 error ("-mpower9-dform, -mpower9-dform-vector, -mpower9-dform-scalar"
4833 " require -mdirect-move");
4834 else if ((rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE) == 0)
4836 rs6000_isa_flags |= OPTION_MASK_DIRECT_MOVE;
4837 rs6000_isa_flags_explicit |= OPTION_MASK_DIRECT_MOVE;
4839 else
4841 rs6000_isa_flags &=
4842 ~(OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4843 rs6000_isa_flags_explicit |=
4844 (OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4848 if (TARGET_P9_DFORM_SCALAR && !TARGET_UPPER_REGS_DF)
4850 /* We prefer to not mention undocumented options in
4851 error messages. However, if users have managed to select
4852 power9-dform without selecting upper-regs-df, they
4853 already know about undocumented flags. */
4854 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF)
4855 error ("-mpower9-dform requires -mupper-regs-df");
4856 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_SCALAR;
4859 if (TARGET_P9_DFORM_SCALAR && !TARGET_UPPER_REGS_SF)
4861 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF)
4862 error ("-mpower9-dform requires -mupper-regs-sf");
4863 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_SCALAR;
4866 /* Enable LRA by default. */
4867 if ((rs6000_isa_flags_explicit & OPTION_MASK_LRA) == 0)
4868 rs6000_isa_flags |= OPTION_MASK_LRA;
4870 /* There have been bugs with -mvsx-timode that don't show up with -mlra,
4871 but do show up with -mno-lra. Given -mlra will become the default once
4872 PR 69847 is fixed, turn off the options with problems by default if
4873 -mno-lra was used, and warn if the user explicitly asked for the option.
4875 Enable -mpower9-dform-vector by default if LRA and other power9 options.
4876 Enable -mvsx-timode by default if LRA and VSX. */
4877 if (!TARGET_LRA)
4879 if (TARGET_VSX_TIMODE)
4881 if ((rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE) != 0)
4882 warning (0, "-mvsx-timode might need -mlra");
4884 else
4885 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
4889 else
4891 if (TARGET_VSX && !TARGET_VSX_TIMODE
4892 && (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE) == 0)
4893 rs6000_isa_flags |= OPTION_MASK_VSX_TIMODE;
4896 /* Set -mallow-movmisalign to explicitly on if we have full ISA 2.07
4897 support. If we only have ISA 2.06 support, and the user did not specify
4898 the switch, leave it set to -1 so the movmisalign patterns are enabled,
4899 but we don't enable the full vectorization support */
4900 if (TARGET_ALLOW_MOVMISALIGN == -1 && TARGET_P8_VECTOR && TARGET_DIRECT_MOVE)
4901 TARGET_ALLOW_MOVMISALIGN = 1;
4903 else if (TARGET_ALLOW_MOVMISALIGN && !TARGET_VSX)
4905 if (TARGET_ALLOW_MOVMISALIGN > 0
4906 && global_options_set.x_TARGET_ALLOW_MOVMISALIGN)
4907 error ("-mallow-movmisalign requires -mvsx");
4909 TARGET_ALLOW_MOVMISALIGN = 0;
4912 /* Determine when unaligned vector accesses are permitted, and when
4913 they are preferred over masked Altivec loads. Note that if
4914 TARGET_ALLOW_MOVMISALIGN has been disabled by the user, then
4915 TARGET_EFFICIENT_UNALIGNED_VSX must be as well. The converse is
4916 not true. */
4917 if (TARGET_EFFICIENT_UNALIGNED_VSX)
4919 if (!TARGET_VSX)
4921 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4922 error ("-mefficient-unaligned-vsx requires -mvsx");
4924 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4927 else if (!TARGET_ALLOW_MOVMISALIGN)
4929 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4930 error ("-mefficient-unaligned-vsx requires -mallow-movmisalign");
4932 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4936 /* Check whether we should allow small integers into VSX registers. We
4937 require direct move to prevent the register allocator from having to move
4938 variables through memory to do moves. SImode can be used on ISA 2.07,
4939 while HImode and QImode require ISA 3.0. */
4940 if (TARGET_VSX_SMALL_INTEGER
4941 && (!TARGET_DIRECT_MOVE || !TARGET_P8_VECTOR || !TARGET_UPPER_REGS_DI))
4943 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_SMALL_INTEGER)
4944 error ("-mvsx-small-integer requires -mpower8-vector, "
4945 "-mupper-regs-di, and -mdirect-move");
4947 rs6000_isa_flags &= ~OPTION_MASK_VSX_SMALL_INTEGER;
4950 /* Set long double size before the IEEE 128-bit tests. */
4951 if (!global_options_set.x_rs6000_long_double_type_size)
4953 if (main_target_opt != NULL
4954 && (main_target_opt->x_rs6000_long_double_type_size
4955 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
4956 error ("target attribute or pragma changes long double size");
4957 else
4958 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
4961 /* Set -mabi=ieeelongdouble on some old targets. Note, AIX and Darwin
4962 explicitly redefine TARGET_IEEEQUAD to 0, so those systems will not
4963 pick up this default. */
4964 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
4965 if (!global_options_set.x_rs6000_ieeequad)
4966 rs6000_ieeequad = 1;
4967 #endif
4969 /* Enable the default support for IEEE 128-bit floating point on Linux VSX
4970 sytems, but don't enable the __float128 keyword. */
4971 if (TARGET_VSX && TARGET_LONG_DOUBLE_128
4972 && (TARGET_FLOAT128_ENABLE_TYPE || TARGET_IEEEQUAD)
4973 && ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_TYPE) == 0))
4974 rs6000_isa_flags |= OPTION_MASK_FLOAT128_TYPE;
4976 /* IEEE 128-bit floating point requires VSX support. */
4977 if (!TARGET_VSX)
4979 if (TARGET_FLOAT128_KEYWORD)
4981 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
4982 error ("-mfloat128 requires VSX support");
4984 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4985 | OPTION_MASK_FLOAT128_KEYWORD
4986 | OPTION_MASK_FLOAT128_HW);
4989 else if (TARGET_FLOAT128_TYPE)
4991 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_TYPE) != 0)
4992 error ("-mfloat128-type requires VSX support");
4994 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4995 | OPTION_MASK_FLOAT128_KEYWORD
4996 | OPTION_MASK_FLOAT128_HW);
5000 /* -mfloat128 and -mfloat128-hardware internally require the underlying IEEE
5001 128-bit floating point support to be enabled. */
5002 if (!TARGET_FLOAT128_TYPE)
5004 if (TARGET_FLOAT128_KEYWORD)
5006 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
5008 error ("-mfloat128 requires -mfloat128-type");
5009 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
5010 | OPTION_MASK_FLOAT128_KEYWORD
5011 | OPTION_MASK_FLOAT128_HW);
5013 else
5014 rs6000_isa_flags |= OPTION_MASK_FLOAT128_TYPE;
5017 if (TARGET_FLOAT128_HW)
5019 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
5021 error ("-mfloat128-hardware requires -mfloat128-type");
5022 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
5024 else
5025 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
5026 | OPTION_MASK_FLOAT128_KEYWORD
5027 | OPTION_MASK_FLOAT128_HW);
5031 /* If we have -mfloat128-type and full ISA 3.0 support, enable
5032 -mfloat128-hardware by default. However, don't enable the __float128
5033 keyword. If the user explicitly turned on -mfloat128-hardware, enable the
5034 -mfloat128 option as well if it was not already set. */
5035 if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_HW
5036 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) == ISA_3_0_MASKS_IEEE
5037 && !(rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW))
5038 rs6000_isa_flags |= OPTION_MASK_FLOAT128_HW;
5040 if (TARGET_FLOAT128_HW
5041 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) != ISA_3_0_MASKS_IEEE)
5043 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
5044 error ("-mfloat128-hardware requires full ISA 3.0 support");
5046 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
5049 if (TARGET_FLOAT128_HW && !TARGET_64BIT)
5051 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
5052 error ("-mfloat128-hardware requires -m64");
5054 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
5057 if (TARGET_FLOAT128_HW && !TARGET_FLOAT128_KEYWORD
5058 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0
5059 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) == 0)
5060 rs6000_isa_flags |= OPTION_MASK_FLOAT128_KEYWORD;
5062 /* Print the options after updating the defaults. */
5063 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
5064 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
5066 /* E500mc does "better" if we inline more aggressively. Respect the
5067 user's opinion, though. */
5068 if (rs6000_block_move_inline_limit == 0
5069 && (rs6000_cpu == PROCESSOR_PPCE500MC
5070 || rs6000_cpu == PROCESSOR_PPCE500MC64
5071 || rs6000_cpu == PROCESSOR_PPCE5500
5072 || rs6000_cpu == PROCESSOR_PPCE6500))
5073 rs6000_block_move_inline_limit = 128;
5075 /* store_one_arg depends on expand_block_move to handle at least the
5076 size of reg_parm_stack_space. */
5077 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
5078 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
5080 if (global_init_p)
5082 /* If the appropriate debug option is enabled, replace the target hooks
5083 with debug versions that call the real version and then prints
5084 debugging information. */
5085 if (TARGET_DEBUG_COST)
5087 targetm.rtx_costs = rs6000_debug_rtx_costs;
5088 targetm.address_cost = rs6000_debug_address_cost;
5089 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
5092 if (TARGET_DEBUG_ADDR)
5094 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
5095 targetm.legitimize_address = rs6000_debug_legitimize_address;
5096 rs6000_secondary_reload_class_ptr
5097 = rs6000_debug_secondary_reload_class;
5098 targetm.secondary_memory_needed
5099 = rs6000_debug_secondary_memory_needed;
5100 targetm.can_change_mode_class
5101 = rs6000_debug_can_change_mode_class;
5102 rs6000_preferred_reload_class_ptr
5103 = rs6000_debug_preferred_reload_class;
5104 rs6000_legitimize_reload_address_ptr
5105 = rs6000_debug_legitimize_reload_address;
5106 rs6000_mode_dependent_address_ptr
5107 = rs6000_debug_mode_dependent_address;
5110 if (rs6000_veclibabi_name)
5112 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
5113 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
5114 else
5116 error ("unknown vectorization library ABI type (%s) for "
5117 "-mveclibabi= switch", rs6000_veclibabi_name);
5118 ret = false;
5123 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
5124 target attribute or pragma which automatically enables both options,
5125 unless the altivec ABI was set. This is set by default for 64-bit, but
5126 not for 32-bit. */
5127 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
5128 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC
5129 | OPTION_MASK_FLOAT128_TYPE
5130 | OPTION_MASK_FLOAT128_KEYWORD)
5131 & ~rs6000_isa_flags_explicit);
5133 /* Enable Altivec ABI for AIX -maltivec. */
5134 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
5136 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
5137 error ("target attribute or pragma changes AltiVec ABI");
5138 else
5139 rs6000_altivec_abi = 1;
5142 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
5143 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
5144 be explicitly overridden in either case. */
5145 if (TARGET_ELF)
5147 if (!global_options_set.x_rs6000_altivec_abi
5148 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
5150 if (main_target_opt != NULL &&
5151 !main_target_opt->x_rs6000_altivec_abi)
5152 error ("target attribute or pragma changes AltiVec ABI");
5153 else
5154 rs6000_altivec_abi = 1;
5158 /* Set the Darwin64 ABI as default for 64-bit Darwin.
5159 So far, the only darwin64 targets are also MACH-O. */
5160 if (TARGET_MACHO
5161 && DEFAULT_ABI == ABI_DARWIN
5162 && TARGET_64BIT)
5164 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
5165 error ("target attribute or pragma changes darwin64 ABI");
5166 else
5168 rs6000_darwin64_abi = 1;
5169 /* Default to natural alignment, for better performance. */
5170 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
5174 /* Place FP constants in the constant pool instead of TOC
5175 if section anchors enabled. */
5176 if (flag_section_anchors
5177 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
5178 TARGET_NO_FP_IN_TOC = 1;
5180 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
5181 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
5183 #ifdef SUBTARGET_OVERRIDE_OPTIONS
5184 SUBTARGET_OVERRIDE_OPTIONS;
5185 #endif
5186 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
5187 SUBSUBTARGET_OVERRIDE_OPTIONS;
5188 #endif
5189 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
5190 SUB3TARGET_OVERRIDE_OPTIONS;
5191 #endif
5193 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
5194 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
5196 /* For the E500 family of cores, reset the single/double FP flags to let us
5197 check that they remain constant across attributes or pragmas. Also,
5198 clear a possible request for string instructions, not supported and which
5199 we might have silently queried above for -Os.
5201 For other families, clear ISEL in case it was set implicitly.
5204 switch (rs6000_cpu)
5206 case PROCESSOR_PPC8540:
5207 case PROCESSOR_PPC8548:
5208 case PROCESSOR_PPCE500MC:
5209 case PROCESSOR_PPCE500MC64:
5210 case PROCESSOR_PPCE5500:
5211 case PROCESSOR_PPCE6500:
5213 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
5214 rs6000_double_float = TARGET_E500_DOUBLE;
5216 rs6000_isa_flags &= ~OPTION_MASK_STRING;
5218 break;
5220 default:
5222 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
5223 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
5225 break;
5228 if (main_target_opt)
5230 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
5231 error ("target attribute or pragma changes single precision floating "
5232 "point");
5233 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
5234 error ("target attribute or pragma changes double precision floating "
5235 "point");
5238 /* Detect invalid option combinations with E500. */
5239 CHECK_E500_OPTIONS;
5241 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
5242 && rs6000_cpu != PROCESSOR_POWER5
5243 && rs6000_cpu != PROCESSOR_POWER6
5244 && rs6000_cpu != PROCESSOR_POWER7
5245 && rs6000_cpu != PROCESSOR_POWER8
5246 && rs6000_cpu != PROCESSOR_POWER9
5247 && rs6000_cpu != PROCESSOR_PPCA2
5248 && rs6000_cpu != PROCESSOR_CELL
5249 && rs6000_cpu != PROCESSOR_PPC476);
5250 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
5251 || rs6000_cpu == PROCESSOR_POWER5
5252 || rs6000_cpu == PROCESSOR_POWER7
5253 || rs6000_cpu == PROCESSOR_POWER8);
5254 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
5255 || rs6000_cpu == PROCESSOR_POWER5
5256 || rs6000_cpu == PROCESSOR_POWER6
5257 || rs6000_cpu == PROCESSOR_POWER7
5258 || rs6000_cpu == PROCESSOR_POWER8
5259 || rs6000_cpu == PROCESSOR_POWER9
5260 || rs6000_cpu == PROCESSOR_PPCE500MC
5261 || rs6000_cpu == PROCESSOR_PPCE500MC64
5262 || rs6000_cpu == PROCESSOR_PPCE5500
5263 || rs6000_cpu == PROCESSOR_PPCE6500);
5265 /* Allow debug switches to override the above settings. These are set to -1
5266 in powerpcspe.opt to indicate the user hasn't directly set the switch. */
5267 if (TARGET_ALWAYS_HINT >= 0)
5268 rs6000_always_hint = TARGET_ALWAYS_HINT;
5270 if (TARGET_SCHED_GROUPS >= 0)
5271 rs6000_sched_groups = TARGET_SCHED_GROUPS;
5273 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
5274 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
5276 rs6000_sched_restricted_insns_priority
5277 = (rs6000_sched_groups ? 1 : 0);
5279 /* Handle -msched-costly-dep option. */
5280 rs6000_sched_costly_dep
5281 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
5283 if (rs6000_sched_costly_dep_str)
5285 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
5286 rs6000_sched_costly_dep = no_dep_costly;
5287 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
5288 rs6000_sched_costly_dep = all_deps_costly;
5289 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
5290 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
5291 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
5292 rs6000_sched_costly_dep = store_to_load_dep_costly;
5293 else
5294 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
5295 atoi (rs6000_sched_costly_dep_str));
5298 /* Handle -minsert-sched-nops option. */
5299 rs6000_sched_insert_nops
5300 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
5302 if (rs6000_sched_insert_nops_str)
5304 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
5305 rs6000_sched_insert_nops = sched_finish_none;
5306 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
5307 rs6000_sched_insert_nops = sched_finish_pad_groups;
5308 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
5309 rs6000_sched_insert_nops = sched_finish_regroup_exact;
5310 else
5311 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
5312 atoi (rs6000_sched_insert_nops_str));
5315 /* Handle stack protector */
5316 if (!global_options_set.x_rs6000_stack_protector_guard)
5317 #ifdef TARGET_THREAD_SSP_OFFSET
5318 rs6000_stack_protector_guard = SSP_TLS;
5319 #else
5320 rs6000_stack_protector_guard = SSP_GLOBAL;
5321 #endif
5323 #ifdef TARGET_THREAD_SSP_OFFSET
5324 rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
5325 rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
5326 #endif
5328 if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
5330 char *endp;
5331 const char *str = rs6000_stack_protector_guard_offset_str;
5333 errno = 0;
5334 long offset = strtol (str, &endp, 0);
5335 if (!*str || *endp || errno)
5336 error ("%qs is not a valid number "
5337 "in -mstack-protector-guard-offset=", str);
5339 if (!IN_RANGE (offset, -0x8000, 0x7fff)
5340 || (TARGET_64BIT && (offset & 3)))
5341 error ("%qs is not a valid offset "
5342 "in -mstack-protector-guard-offset=", str);
5344 rs6000_stack_protector_guard_offset = offset;
5347 if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
5349 const char *str = rs6000_stack_protector_guard_reg_str;
5350 int reg = decode_reg_name (str);
5352 if (!IN_RANGE (reg, 1, 31))
5353 error ("%qs is not a valid base register "
5354 "in -mstack-protector-guard-reg=", str);
5356 rs6000_stack_protector_guard_reg = reg;
5359 if (rs6000_stack_protector_guard == SSP_TLS
5360 && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
5361 error ("-mstack-protector-guard=tls needs a valid base register");
5363 if (global_init_p)
5365 #ifdef TARGET_REGNAMES
5366 /* If the user desires alternate register names, copy in the
5367 alternate names now. */
5368 if (TARGET_REGNAMES)
5369 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
5370 #endif
5372 /* Set aix_struct_return last, after the ABI is determined.
5373 If -maix-struct-return or -msvr4-struct-return was explicitly
5374 used, don't override with the ABI default. */
5375 if (!global_options_set.x_aix_struct_return)
5376 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
5378 #if 0
5379 /* IBM XL compiler defaults to unsigned bitfields. */
5380 if (TARGET_XL_COMPAT)
5381 flag_signed_bitfields = 0;
5382 #endif
5384 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
5385 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
5387 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
5389 /* We can only guarantee the availability of DI pseudo-ops when
5390 assembling for 64-bit targets. */
5391 if (!TARGET_64BIT)
5393 targetm.asm_out.aligned_op.di = NULL;
5394 targetm.asm_out.unaligned_op.di = NULL;
5398 /* Set branch target alignment, if not optimizing for size. */
5399 if (!optimize_size)
5401 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
5402 aligned 8byte to avoid misprediction by the branch predictor. */
5403 if (rs6000_cpu == PROCESSOR_TITAN
5404 || rs6000_cpu == PROCESSOR_CELL)
5406 if (align_functions <= 0)
5407 align_functions = 8;
5408 if (align_jumps <= 0)
5409 align_jumps = 8;
5410 if (align_loops <= 0)
5411 align_loops = 8;
5413 if (rs6000_align_branch_targets)
5415 if (align_functions <= 0)
5416 align_functions = 16;
5417 if (align_jumps <= 0)
5418 align_jumps = 16;
5419 if (align_loops <= 0)
5421 can_override_loop_align = 1;
5422 align_loops = 16;
5425 if (align_jumps_max_skip <= 0)
5426 align_jumps_max_skip = 15;
5427 if (align_loops_max_skip <= 0)
5428 align_loops_max_skip = 15;
5431 /* Arrange to save and restore machine status around nested functions. */
5432 init_machine_status = rs6000_init_machine_status;
5434 /* We should always be splitting complex arguments, but we can't break
5435 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
5436 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
5437 targetm.calls.split_complex_arg = NULL;
5439 /* The AIX and ELFv1 ABIs define standard function descriptors. */
5440 if (DEFAULT_ABI == ABI_AIX)
5441 targetm.calls.custom_function_descriptors = 0;
5444 /* Initialize rs6000_cost with the appropriate target costs. */
5445 if (optimize_size)
5446 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
5447 else
5448 switch (rs6000_cpu)
5450 case PROCESSOR_RS64A:
5451 rs6000_cost = &rs64a_cost;
5452 break;
5454 case PROCESSOR_MPCCORE:
5455 rs6000_cost = &mpccore_cost;
5456 break;
5458 case PROCESSOR_PPC403:
5459 rs6000_cost = &ppc403_cost;
5460 break;
5462 case PROCESSOR_PPC405:
5463 rs6000_cost = &ppc405_cost;
5464 break;
5466 case PROCESSOR_PPC440:
5467 rs6000_cost = &ppc440_cost;
5468 break;
5470 case PROCESSOR_PPC476:
5471 rs6000_cost = &ppc476_cost;
5472 break;
5474 case PROCESSOR_PPC601:
5475 rs6000_cost = &ppc601_cost;
5476 break;
5478 case PROCESSOR_PPC603:
5479 rs6000_cost = &ppc603_cost;
5480 break;
5482 case PROCESSOR_PPC604:
5483 rs6000_cost = &ppc604_cost;
5484 break;
5486 case PROCESSOR_PPC604e:
5487 rs6000_cost = &ppc604e_cost;
5488 break;
5490 case PROCESSOR_PPC620:
5491 rs6000_cost = &ppc620_cost;
5492 break;
5494 case PROCESSOR_PPC630:
5495 rs6000_cost = &ppc630_cost;
5496 break;
5498 case PROCESSOR_CELL:
5499 rs6000_cost = &ppccell_cost;
5500 break;
5502 case PROCESSOR_PPC750:
5503 case PROCESSOR_PPC7400:
5504 rs6000_cost = &ppc750_cost;
5505 break;
5507 case PROCESSOR_PPC7450:
5508 rs6000_cost = &ppc7450_cost;
5509 break;
5511 case PROCESSOR_PPC8540:
5512 case PROCESSOR_PPC8548:
5513 rs6000_cost = &ppc8540_cost;
5514 break;
5516 case PROCESSOR_PPCE300C2:
5517 case PROCESSOR_PPCE300C3:
5518 rs6000_cost = &ppce300c2c3_cost;
5519 break;
5521 case PROCESSOR_PPCE500MC:
5522 rs6000_cost = &ppce500mc_cost;
5523 break;
5525 case PROCESSOR_PPCE500MC64:
5526 rs6000_cost = &ppce500mc64_cost;
5527 break;
5529 case PROCESSOR_PPCE5500:
5530 rs6000_cost = &ppce5500_cost;
5531 break;
5533 case PROCESSOR_PPCE6500:
5534 rs6000_cost = &ppce6500_cost;
5535 break;
5537 case PROCESSOR_TITAN:
5538 rs6000_cost = &titan_cost;
5539 break;
5541 case PROCESSOR_POWER4:
5542 case PROCESSOR_POWER5:
5543 rs6000_cost = &power4_cost;
5544 break;
5546 case PROCESSOR_POWER6:
5547 rs6000_cost = &power6_cost;
5548 break;
5550 case PROCESSOR_POWER7:
5551 rs6000_cost = &power7_cost;
5552 break;
5554 case PROCESSOR_POWER8:
5555 rs6000_cost = &power8_cost;
5556 break;
5558 case PROCESSOR_POWER9:
5559 rs6000_cost = &power9_cost;
5560 break;
5562 case PROCESSOR_PPCA2:
5563 rs6000_cost = &ppca2_cost;
5564 break;
5566 default:
5567 gcc_unreachable ();
5570 if (global_init_p)
5572 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
5573 rs6000_cost->simultaneous_prefetches,
5574 global_options.x_param_values,
5575 global_options_set.x_param_values);
5576 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
5577 global_options.x_param_values,
5578 global_options_set.x_param_values);
5579 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
5580 rs6000_cost->cache_line_size,
5581 global_options.x_param_values,
5582 global_options_set.x_param_values);
5583 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
5584 global_options.x_param_values,
5585 global_options_set.x_param_values);
5587 /* Increase loop peeling limits based on performance analysis. */
5588 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
5589 global_options.x_param_values,
5590 global_options_set.x_param_values);
5591 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
5592 global_options.x_param_values,
5593 global_options_set.x_param_values);
5595 /* Use the 'model' -fsched-pressure algorithm by default. */
5596 maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM,
5597 SCHED_PRESSURE_MODEL,
5598 global_options.x_param_values,
5599 global_options_set.x_param_values);
5601 /* If using typedef char *va_list, signal that
5602 __builtin_va_start (&ap, 0) can be optimized to
5603 ap = __builtin_next_arg (0). */
5604 if (DEFAULT_ABI != ABI_V4)
5605 targetm.expand_builtin_va_start = NULL;
5608 /* Set up single/double float flags.
5609 If TARGET_HARD_FLOAT is set, but neither single or double is set,
5610 then set both flags. */
5611 if (TARGET_HARD_FLOAT && TARGET_FPRS
5612 && rs6000_single_float == 0 && rs6000_double_float == 0)
5613 rs6000_single_float = rs6000_double_float = 1;
5615 /* If not explicitly specified via option, decide whether to generate indexed
5616 load/store instructions. A value of -1 indicates that the
5617 initial value of this variable has not been overwritten. During
5618 compilation, TARGET_AVOID_XFORM is either 0 or 1. */
5619 if (TARGET_AVOID_XFORM == -1)
5620 /* Avoid indexed addressing when targeting Power6 in order to avoid the
5621 DERAT mispredict penalty. However the LVE and STVE altivec instructions
5622 need indexed accesses and the type used is the scalar type of the element
5623 being loaded or stored. */
5624 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
5625 && !TARGET_ALTIVEC);
5627 /* Set the -mrecip options. */
5628 if (rs6000_recip_name)
5630 char *p = ASTRDUP (rs6000_recip_name);
5631 char *q;
5632 unsigned int mask, i;
5633 bool invert;
5635 while ((q = strtok (p, ",")) != NULL)
5637 p = NULL;
5638 if (*q == '!')
5640 invert = true;
5641 q++;
5643 else
5644 invert = false;
5646 if (!strcmp (q, "default"))
5647 mask = ((TARGET_RECIP_PRECISION)
5648 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
5649 else
5651 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
5652 if (!strcmp (q, recip_options[i].string))
5654 mask = recip_options[i].mask;
5655 break;
5658 if (i == ARRAY_SIZE (recip_options))
5660 error ("unknown option for -mrecip=%s", q);
5661 invert = false;
5662 mask = 0;
5663 ret = false;
5667 if (invert)
5668 rs6000_recip_control &= ~mask;
5669 else
5670 rs6000_recip_control |= mask;
5674 /* Set the builtin mask of the various options used that could affect which
5675 builtins were used. In the past we used target_flags, but we've run out
5676 of bits, and some options like SPE and PAIRED are no longer in
5677 target_flags. */
5678 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
5679 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
5680 rs6000_print_builtin_options (stderr, 0, "builtin mask",
5681 rs6000_builtin_mask);
5683 /* Initialize all of the registers. */
5684 rs6000_init_hard_regno_mode_ok (global_init_p);
5686 /* Save the initial options in case the user does function specific options */
5687 if (global_init_p)
5688 target_option_default_node = target_option_current_node
5689 = build_target_option_node (&global_options);
5691 /* If not explicitly specified via option, decide whether to generate the
5692 extra blr's required to preserve the link stack on some cpus (eg, 476). */
5693 if (TARGET_LINK_STACK == -1)
5694 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
5696 return ret;
5699 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
5700 define the target cpu type. */
5702 static void
5703 rs6000_option_override (void)
5705 (void) rs6000_option_override_internal (true);
5709 /* Implement targetm.vectorize.builtin_mask_for_load. */
5710 static tree
5711 rs6000_builtin_mask_for_load (void)
5713 /* Don't use lvsl/vperm for P8 and similarly efficient machines. */
5714 if ((TARGET_ALTIVEC && !TARGET_VSX)
5715 || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
5716 return altivec_builtin_mask_for_load;
5717 else
5718 return 0;
5721 /* Implement LOOP_ALIGN. */
5723 rs6000_loop_align (rtx label)
5725 basic_block bb;
5726 int ninsns;
5728 /* Don't override loop alignment if -falign-loops was specified. */
5729 if (!can_override_loop_align)
5730 return align_loops_log;
5732 bb = BLOCK_FOR_INSN (label);
5733 ninsns = num_loop_insns(bb->loop_father);
5735 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
5736 if (ninsns > 4 && ninsns <= 8
5737 && (rs6000_cpu == PROCESSOR_POWER4
5738 || rs6000_cpu == PROCESSOR_POWER5
5739 || rs6000_cpu == PROCESSOR_POWER6
5740 || rs6000_cpu == PROCESSOR_POWER7
5741 || rs6000_cpu == PROCESSOR_POWER8
5742 || rs6000_cpu == PROCESSOR_POWER9))
5743 return 5;
5744 else
5745 return align_loops_log;
5748 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
5749 static int
5750 rs6000_loop_align_max_skip (rtx_insn *label)
5752 return (1 << rs6000_loop_align (label)) - 1;
5755 /* Return true iff, data reference of TYPE can reach vector alignment (16)
5756 after applying N number of iterations. This routine does not determine
5757 how may iterations are required to reach desired alignment. */
5759 static bool
5760 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
5762 if (is_packed)
5763 return false;
5765 if (TARGET_32BIT)
5767 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
5768 return true;
5770 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
5771 return true;
5773 return false;
5775 else
5777 if (TARGET_MACHO)
5778 return false;
5780 /* Assuming that all other types are naturally aligned. CHECKME! */
5781 return true;
5785 /* Return true if the vector misalignment factor is supported by the
5786 target. */
5787 static bool
5788 rs6000_builtin_support_vector_misalignment (machine_mode mode,
5789 const_tree type,
5790 int misalignment,
5791 bool is_packed)
5793 if (TARGET_VSX)
5795 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5796 return true;
5798 /* Return if movmisalign pattern is not supported for this mode. */
5799 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
5800 return false;
5802 if (misalignment == -1)
5804 /* Misalignment factor is unknown at compile time but we know
5805 it's word aligned. */
5806 if (rs6000_vector_alignment_reachable (type, is_packed))
5808 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
5810 if (element_size == 64 || element_size == 32)
5811 return true;
5814 return false;
5817 /* VSX supports word-aligned vector. */
5818 if (misalignment % 4 == 0)
5819 return true;
5821 return false;
5824 /* Implement targetm.vectorize.builtin_vectorization_cost. */
5825 static int
5826 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
5827 tree vectype, int misalign)
5829 unsigned elements;
5830 tree elem_type;
5832 switch (type_of_cost)
5834 case scalar_stmt:
5835 case scalar_load:
5836 case scalar_store:
5837 case vector_stmt:
5838 case vector_load:
5839 case vector_store:
5840 case vec_to_scalar:
5841 case scalar_to_vec:
5842 case cond_branch_not_taken:
5843 return 1;
5845 case vec_perm:
5846 if (TARGET_VSX)
5847 return 3;
5848 else
5849 return 1;
5851 case vec_promote_demote:
5852 if (TARGET_VSX)
5853 return 4;
5854 else
5855 return 1;
5857 case cond_branch_taken:
5858 return 3;
5860 case unaligned_load:
5861 case vector_gather_load:
5862 if (TARGET_P9_VECTOR)
5863 return 3;
5865 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5866 return 1;
5868 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5870 elements = TYPE_VECTOR_SUBPARTS (vectype);
5871 if (elements == 2)
5872 /* Double word aligned. */
5873 return 2;
5875 if (elements == 4)
5877 switch (misalign)
5879 case 8:
5880 /* Double word aligned. */
5881 return 2;
5883 case -1:
5884 /* Unknown misalignment. */
5885 case 4:
5886 case 12:
5887 /* Word aligned. */
5888 return 22;
5890 default:
5891 gcc_unreachable ();
5896 if (TARGET_ALTIVEC)
5897 /* Misaligned loads are not supported. */
5898 gcc_unreachable ();
5900 return 2;
5902 case unaligned_store:
5903 case vector_scatter_store:
5904 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5905 return 1;
5907 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5909 elements = TYPE_VECTOR_SUBPARTS (vectype);
5910 if (elements == 2)
5911 /* Double word aligned. */
5912 return 2;
5914 if (elements == 4)
5916 switch (misalign)
5918 case 8:
5919 /* Double word aligned. */
5920 return 2;
5922 case -1:
5923 /* Unknown misalignment. */
5924 case 4:
5925 case 12:
5926 /* Word aligned. */
5927 return 23;
5929 default:
5930 gcc_unreachable ();
5935 if (TARGET_ALTIVEC)
5936 /* Misaligned stores are not supported. */
5937 gcc_unreachable ();
5939 return 2;
5941 case vec_construct:
5942 /* This is a rough approximation assuming non-constant elements
5943 constructed into a vector via element insertion. FIXME:
5944 vec_construct is not granular enough for uniformly good
5945 decisions. If the initialization is a splat, this is
5946 cheaper than we estimate. Improve this someday. */
5947 elem_type = TREE_TYPE (vectype);
5948 /* 32-bit vectors loaded into registers are stored as double
5949 precision, so we need 2 permutes, 2 converts, and 1 merge
5950 to construct a vector of short floats from them. */
5951 if (SCALAR_FLOAT_TYPE_P (elem_type)
5952 && TYPE_PRECISION (elem_type) == 32)
5953 return 5;
5954 /* On POWER9, integer vector types are built up in GPRs and then
5955 use a direct move (2 cycles). For POWER8 this is even worse,
5956 as we need two direct moves and a merge, and the direct moves
5957 are five cycles. */
5958 else if (INTEGRAL_TYPE_P (elem_type))
5960 if (TARGET_P9_VECTOR)
5961 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 2;
5962 else
5963 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 11;
5965 else
5966 /* V2DFmode doesn't need a direct move. */
5967 return 2;
5969 default:
5970 gcc_unreachable ();
5974 /* Implement targetm.vectorize.preferred_simd_mode. */
5976 static machine_mode
5977 rs6000_preferred_simd_mode (scalar_mode mode)
5979 if (TARGET_VSX)
5980 switch (mode)
5982 case E_DFmode:
5983 return V2DFmode;
5984 default:;
5986 if (TARGET_ALTIVEC || TARGET_VSX)
5987 switch (mode)
5989 case E_SFmode:
5990 return V4SFmode;
5991 case E_TImode:
5992 return V1TImode;
5993 case E_DImode:
5994 return V2DImode;
5995 case E_SImode:
5996 return V4SImode;
5997 case E_HImode:
5998 return V8HImode;
5999 case E_QImode:
6000 return V16QImode;
6001 default:;
6003 if (TARGET_SPE)
6004 switch (mode)
6006 case E_SFmode:
6007 return V2SFmode;
6008 case E_SImode:
6009 return V2SImode;
6010 default:;
6012 if (TARGET_PAIRED_FLOAT
6013 && mode == SFmode)
6014 return V2SFmode;
6015 return word_mode;
6018 typedef struct _rs6000_cost_data
6020 struct loop *loop_info;
6021 unsigned cost[3];
6022 } rs6000_cost_data;
6024 /* Test for likely overcommitment of vector hardware resources. If a
6025 loop iteration is relatively large, and too large a percentage of
6026 instructions in the loop are vectorized, the cost model may not
6027 adequately reflect delays from unavailable vector resources.
6028 Penalize the loop body cost for this case. */
6030 static void
6031 rs6000_density_test (rs6000_cost_data *data)
6033 const int DENSITY_PCT_THRESHOLD = 85;
6034 const int DENSITY_SIZE_THRESHOLD = 70;
6035 const int DENSITY_PENALTY = 10;
6036 struct loop *loop = data->loop_info;
6037 basic_block *bbs = get_loop_body (loop);
6038 int nbbs = loop->num_nodes;
6039 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
6040 int i, density_pct;
6042 for (i = 0; i < nbbs; i++)
6044 basic_block bb = bbs[i];
6045 gimple_stmt_iterator gsi;
6047 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6049 gimple *stmt = gsi_stmt (gsi);
6050 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6052 if (!STMT_VINFO_RELEVANT_P (stmt_info)
6053 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
6054 not_vec_cost++;
6058 free (bbs);
6059 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
6061 if (density_pct > DENSITY_PCT_THRESHOLD
6062 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
6064 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
6065 if (dump_enabled_p ())
6066 dump_printf_loc (MSG_NOTE, vect_location,
6067 "density %d%%, cost %d exceeds threshold, penalizing "
6068 "loop body cost by %d%%", density_pct,
6069 vec_cost + not_vec_cost, DENSITY_PENALTY);
6073 /* Implement targetm.vectorize.init_cost. */
6075 /* For each vectorized loop, this var holds TRUE iff a non-memory vector
6076 instruction is needed by the vectorization. */
6077 static bool rs6000_vect_nonmem;
6079 static void *
6080 rs6000_init_cost (struct loop *loop_info)
6082 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
6083 data->loop_info = loop_info;
6084 data->cost[vect_prologue] = 0;
6085 data->cost[vect_body] = 0;
6086 data->cost[vect_epilogue] = 0;
6087 rs6000_vect_nonmem = false;
6088 return data;
6091 /* Implement targetm.vectorize.add_stmt_cost. */
6093 static unsigned
6094 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
6095 struct _stmt_vec_info *stmt_info, int misalign,
6096 enum vect_cost_model_location where)
6098 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
6099 unsigned retval = 0;
6101 if (flag_vect_cost_model)
6103 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
6104 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
6105 misalign);
6106 /* Statements in an inner loop relative to the loop being
6107 vectorized are weighted more heavily. The value here is
6108 arbitrary and could potentially be improved with analysis. */
6109 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
6110 count *= 50; /* FIXME. */
6112 retval = (unsigned) (count * stmt_cost);
6113 cost_data->cost[where] += retval;
6115 /* Check whether we're doing something other than just a copy loop.
6116 Not all such loops may be profitably vectorized; see
6117 rs6000_finish_cost. */
6118 if ((kind == vec_to_scalar || kind == vec_perm
6119 || kind == vec_promote_demote || kind == vec_construct
6120 || kind == scalar_to_vec)
6121 || (where == vect_body && kind == vector_stmt))
6122 rs6000_vect_nonmem = true;
6125 return retval;
6128 /* Implement targetm.vectorize.finish_cost. */
6130 static void
6131 rs6000_finish_cost (void *data, unsigned *prologue_cost,
6132 unsigned *body_cost, unsigned *epilogue_cost)
6134 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
6136 if (cost_data->loop_info)
6137 rs6000_density_test (cost_data);
6139 /* Don't vectorize minimum-vectorization-factor, simple copy loops
6140 that require versioning for any reason. The vectorization is at
6141 best a wash inside the loop, and the versioning checks make
6142 profitability highly unlikely and potentially quite harmful. */
6143 if (cost_data->loop_info)
6145 loop_vec_info vec_info = loop_vec_info_for_loop (cost_data->loop_info);
6146 if (!rs6000_vect_nonmem
6147 && LOOP_VINFO_VECT_FACTOR (vec_info) == 2
6148 && LOOP_REQUIRES_VERSIONING (vec_info))
6149 cost_data->cost[vect_body] += 10000;
6152 *prologue_cost = cost_data->cost[vect_prologue];
6153 *body_cost = cost_data->cost[vect_body];
6154 *epilogue_cost = cost_data->cost[vect_epilogue];
6157 /* Implement targetm.vectorize.destroy_cost_data. */
6159 static void
6160 rs6000_destroy_cost_data (void *data)
6162 free (data);
6165 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
6166 library with vectorized intrinsics. */
6168 static tree
6169 rs6000_builtin_vectorized_libmass (combined_fn fn, tree type_out,
6170 tree type_in)
6172 char name[32];
6173 const char *suffix = NULL;
6174 tree fntype, new_fndecl, bdecl = NULL_TREE;
6175 int n_args = 1;
6176 const char *bname;
6177 machine_mode el_mode, in_mode;
6178 int n, in_n;
6180 /* Libmass is suitable for unsafe math only as it does not correctly support
6181 parts of IEEE with the required precision such as denormals. Only support
6182 it if we have VSX to use the simd d2 or f4 functions.
6183 XXX: Add variable length support. */
6184 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
6185 return NULL_TREE;
6187 el_mode = TYPE_MODE (TREE_TYPE (type_out));
6188 n = TYPE_VECTOR_SUBPARTS (type_out);
6189 in_mode = TYPE_MODE (TREE_TYPE (type_in));
6190 in_n = TYPE_VECTOR_SUBPARTS (type_in);
6191 if (el_mode != in_mode
6192 || n != in_n)
6193 return NULL_TREE;
6195 switch (fn)
6197 CASE_CFN_ATAN2:
6198 CASE_CFN_HYPOT:
6199 CASE_CFN_POW:
6200 n_args = 2;
6201 gcc_fallthrough ();
6203 CASE_CFN_ACOS:
6204 CASE_CFN_ACOSH:
6205 CASE_CFN_ASIN:
6206 CASE_CFN_ASINH:
6207 CASE_CFN_ATAN:
6208 CASE_CFN_ATANH:
6209 CASE_CFN_CBRT:
6210 CASE_CFN_COS:
6211 CASE_CFN_COSH:
6212 CASE_CFN_ERF:
6213 CASE_CFN_ERFC:
6214 CASE_CFN_EXP2:
6215 CASE_CFN_EXP:
6216 CASE_CFN_EXPM1:
6217 CASE_CFN_LGAMMA:
6218 CASE_CFN_LOG10:
6219 CASE_CFN_LOG1P:
6220 CASE_CFN_LOG2:
6221 CASE_CFN_LOG:
6222 CASE_CFN_SIN:
6223 CASE_CFN_SINH:
6224 CASE_CFN_SQRT:
6225 CASE_CFN_TAN:
6226 CASE_CFN_TANH:
6227 if (el_mode == DFmode && n == 2)
6229 bdecl = mathfn_built_in (double_type_node, fn);
6230 suffix = "d2"; /* pow -> powd2 */
6232 else if (el_mode == SFmode && n == 4)
6234 bdecl = mathfn_built_in (float_type_node, fn);
6235 suffix = "4"; /* powf -> powf4 */
6237 else
6238 return NULL_TREE;
6239 if (!bdecl)
6240 return NULL_TREE;
6241 break;
6243 default:
6244 return NULL_TREE;
6247 gcc_assert (suffix != NULL);
6248 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
6249 if (!bname)
6250 return NULL_TREE;
6252 strcpy (name, bname + sizeof ("__builtin_") - 1);
6253 strcat (name, suffix);
6255 if (n_args == 1)
6256 fntype = build_function_type_list (type_out, type_in, NULL);
6257 else if (n_args == 2)
6258 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
6259 else
6260 gcc_unreachable ();
6262 /* Build a function declaration for the vectorized function. */
6263 new_fndecl = build_decl (BUILTINS_LOCATION,
6264 FUNCTION_DECL, get_identifier (name), fntype);
6265 TREE_PUBLIC (new_fndecl) = 1;
6266 DECL_EXTERNAL (new_fndecl) = 1;
6267 DECL_IS_NOVOPS (new_fndecl) = 1;
6268 TREE_READONLY (new_fndecl) = 1;
6270 return new_fndecl;
6273 /* Returns a function decl for a vectorized version of the builtin function
6274 with builtin function code FN and the result vector type TYPE, or NULL_TREE
6275 if it is not available. */
6277 static tree
6278 rs6000_builtin_vectorized_function (unsigned int fn, tree type_out,
6279 tree type_in)
6281 machine_mode in_mode, out_mode;
6282 int in_n, out_n;
6284 if (TARGET_DEBUG_BUILTIN)
6285 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
6286 combined_fn_name (combined_fn (fn)),
6287 GET_MODE_NAME (TYPE_MODE (type_out)),
6288 GET_MODE_NAME (TYPE_MODE (type_in)));
6290 if (TREE_CODE (type_out) != VECTOR_TYPE
6291 || TREE_CODE (type_in) != VECTOR_TYPE
6292 || !TARGET_VECTORIZE_BUILTINS)
6293 return NULL_TREE;
6295 out_mode = TYPE_MODE (TREE_TYPE (type_out));
6296 out_n = TYPE_VECTOR_SUBPARTS (type_out);
6297 in_mode = TYPE_MODE (TREE_TYPE (type_in));
6298 in_n = TYPE_VECTOR_SUBPARTS (type_in);
6300 switch (fn)
6302 CASE_CFN_COPYSIGN:
6303 if (VECTOR_UNIT_VSX_P (V2DFmode)
6304 && out_mode == DFmode && out_n == 2
6305 && in_mode == DFmode && in_n == 2)
6306 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
6307 if (VECTOR_UNIT_VSX_P (V4SFmode)
6308 && out_mode == SFmode && out_n == 4
6309 && in_mode == SFmode && in_n == 4)
6310 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
6311 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
6312 && out_mode == SFmode && out_n == 4
6313 && in_mode == SFmode && in_n == 4)
6314 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
6315 break;
6316 CASE_CFN_CEIL:
6317 if (VECTOR_UNIT_VSX_P (V2DFmode)
6318 && out_mode == DFmode && out_n == 2
6319 && in_mode == DFmode && in_n == 2)
6320 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
6321 if (VECTOR_UNIT_VSX_P (V4SFmode)
6322 && out_mode == SFmode && out_n == 4
6323 && in_mode == SFmode && in_n == 4)
6324 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
6325 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
6326 && out_mode == SFmode && out_n == 4
6327 && in_mode == SFmode && in_n == 4)
6328 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
6329 break;
6330 CASE_CFN_FLOOR:
6331 if (VECTOR_UNIT_VSX_P (V2DFmode)
6332 && out_mode == DFmode && out_n == 2
6333 && in_mode == DFmode && in_n == 2)
6334 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
6335 if (VECTOR_UNIT_VSX_P (V4SFmode)
6336 && out_mode == SFmode && out_n == 4
6337 && in_mode == SFmode && in_n == 4)
6338 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
6339 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
6340 && out_mode == SFmode && out_n == 4
6341 && in_mode == SFmode && in_n == 4)
6342 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
6343 break;
6344 CASE_CFN_FMA:
6345 if (VECTOR_UNIT_VSX_P (V2DFmode)
6346 && out_mode == DFmode && out_n == 2
6347 && in_mode == DFmode && in_n == 2)
6348 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
6349 if (VECTOR_UNIT_VSX_P (V4SFmode)
6350 && out_mode == SFmode && out_n == 4
6351 && in_mode == SFmode && in_n == 4)
6352 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
6353 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
6354 && out_mode == SFmode && out_n == 4
6355 && in_mode == SFmode && in_n == 4)
6356 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
6357 break;
6358 CASE_CFN_TRUNC:
6359 if (VECTOR_UNIT_VSX_P (V2DFmode)
6360 && out_mode == DFmode && out_n == 2
6361 && in_mode == DFmode && in_n == 2)
6362 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
6363 if (VECTOR_UNIT_VSX_P (V4SFmode)
6364 && out_mode == SFmode && out_n == 4
6365 && in_mode == SFmode && in_n == 4)
6366 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
6367 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
6368 && out_mode == SFmode && out_n == 4
6369 && in_mode == SFmode && in_n == 4)
6370 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
6371 break;
6372 CASE_CFN_NEARBYINT:
6373 if (VECTOR_UNIT_VSX_P (V2DFmode)
6374 && flag_unsafe_math_optimizations
6375 && out_mode == DFmode && out_n == 2
6376 && in_mode == DFmode && in_n == 2)
6377 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
6378 if (VECTOR_UNIT_VSX_P (V4SFmode)
6379 && flag_unsafe_math_optimizations
6380 && out_mode == SFmode && out_n == 4
6381 && in_mode == SFmode && in_n == 4)
6382 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
6383 break;
6384 CASE_CFN_RINT:
6385 if (VECTOR_UNIT_VSX_P (V2DFmode)
6386 && !flag_trapping_math
6387 && out_mode == DFmode && out_n == 2
6388 && in_mode == DFmode && in_n == 2)
6389 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
6390 if (VECTOR_UNIT_VSX_P (V4SFmode)
6391 && !flag_trapping_math
6392 && out_mode == SFmode && out_n == 4
6393 && in_mode == SFmode && in_n == 4)
6394 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
6395 break;
6396 default:
6397 break;
6400 /* Generate calls to libmass if appropriate. */
6401 if (rs6000_veclib_handler)
6402 return rs6000_veclib_handler (combined_fn (fn), type_out, type_in);
6404 return NULL_TREE;
6407 /* Implement TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION. */
6409 static tree
6410 rs6000_builtin_md_vectorized_function (tree fndecl, tree type_out,
6411 tree type_in)
6413 machine_mode in_mode, out_mode;
6414 int in_n, out_n;
6416 if (TARGET_DEBUG_BUILTIN)
6417 fprintf (stderr, "rs6000_builtin_md_vectorized_function (%s, %s, %s)\n",
6418 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
6419 GET_MODE_NAME (TYPE_MODE (type_out)),
6420 GET_MODE_NAME (TYPE_MODE (type_in)));
6422 if (TREE_CODE (type_out) != VECTOR_TYPE
6423 || TREE_CODE (type_in) != VECTOR_TYPE
6424 || !TARGET_VECTORIZE_BUILTINS)
6425 return NULL_TREE;
6427 out_mode = TYPE_MODE (TREE_TYPE (type_out));
6428 out_n = TYPE_VECTOR_SUBPARTS (type_out);
6429 in_mode = TYPE_MODE (TREE_TYPE (type_in));
6430 in_n = TYPE_VECTOR_SUBPARTS (type_in);
6432 enum rs6000_builtins fn
6433 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
6434 switch (fn)
6436 case RS6000_BUILTIN_RSQRTF:
6437 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
6438 && out_mode == SFmode && out_n == 4
6439 && in_mode == SFmode && in_n == 4)
6440 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
6441 break;
6442 case RS6000_BUILTIN_RSQRT:
6443 if (VECTOR_UNIT_VSX_P (V2DFmode)
6444 && out_mode == DFmode && out_n == 2
6445 && in_mode == DFmode && in_n == 2)
6446 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
6447 break;
6448 case RS6000_BUILTIN_RECIPF:
6449 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
6450 && out_mode == SFmode && out_n == 4
6451 && in_mode == SFmode && in_n == 4)
6452 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
6453 break;
6454 case RS6000_BUILTIN_RECIP:
6455 if (VECTOR_UNIT_VSX_P (V2DFmode)
6456 && out_mode == DFmode && out_n == 2
6457 && in_mode == DFmode && in_n == 2)
6458 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
6459 break;
6460 default:
6461 break;
6463 return NULL_TREE;
6466 /* Default CPU string for rs6000*_file_start functions. */
6467 static const char *rs6000_default_cpu;
6469 /* Do anything needed at the start of the asm file. */
6471 static void
6472 rs6000_file_start (void)
6474 char buffer[80];
6475 const char *start = buffer;
6476 FILE *file = asm_out_file;
6478 rs6000_default_cpu = TARGET_CPU_DEFAULT;
6480 default_file_start ();
6482 if (flag_verbose_asm)
6484 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
6486 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
6488 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
6489 start = "";
6492 if (global_options_set.x_rs6000_cpu_index)
6494 fprintf (file, "%s -mcpu=%s", start,
6495 processor_target_table[rs6000_cpu_index].name);
6496 start = "";
6499 if (global_options_set.x_rs6000_tune_index)
6501 fprintf (file, "%s -mtune=%s", start,
6502 processor_target_table[rs6000_tune_index].name);
6503 start = "";
6506 if (PPC405_ERRATUM77)
6508 fprintf (file, "%s PPC405CR_ERRATUM77", start);
6509 start = "";
6512 #ifdef USING_ELFOS_H
6513 switch (rs6000_sdata)
6515 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
6516 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
6517 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
6518 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
6521 if (rs6000_sdata && g_switch_value)
6523 fprintf (file, "%s -G %d", start,
6524 g_switch_value);
6525 start = "";
6527 #endif
6529 if (*start == '\0')
6530 putc ('\n', file);
6533 #ifdef USING_ELFOS_H
6534 if (!(rs6000_default_cpu && rs6000_default_cpu[0])
6535 && !global_options_set.x_rs6000_cpu_index)
6537 fputs ("\t.machine ", asm_out_file);
6538 if ((rs6000_isa_flags & OPTION_MASK_MODULO) != 0)
6539 fputs ("power9\n", asm_out_file);
6540 else if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
6541 fputs ("power8\n", asm_out_file);
6542 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTD) != 0)
6543 fputs ("power7\n", asm_out_file);
6544 else if ((rs6000_isa_flags & OPTION_MASK_CMPB) != 0)
6545 fputs ("power6\n", asm_out_file);
6546 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTB) != 0)
6547 fputs ("power5\n", asm_out_file);
6548 else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
6549 fputs ("power4\n", asm_out_file);
6550 else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
6551 fputs ("ppc64\n", asm_out_file);
6552 else
6553 fputs ("ppc\n", asm_out_file);
6555 #endif
6557 if (DEFAULT_ABI == ABI_ELFv2)
6558 fprintf (file, "\t.abiversion 2\n");
6562 /* Return nonzero if this function is known to have a null epilogue. */
6565 direct_return (void)
6567 if (reload_completed)
6569 rs6000_stack_t *info = rs6000_stack_info ();
6571 if (info->first_gp_reg_save == 32
6572 && info->first_fp_reg_save == 64
6573 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
6574 && ! info->lr_save_p
6575 && ! info->cr_save_p
6576 && info->vrsave_size == 0
6577 && ! info->push_p)
6578 return 1;
6581 return 0;
6584 /* Return the number of instructions it takes to form a constant in an
6585 integer register. */
6588 num_insns_constant_wide (HOST_WIDE_INT value)
6590 /* signed constant loadable with addi */
6591 if (((unsigned HOST_WIDE_INT) value + 0x8000) < 0x10000)
6592 return 1;
6594 /* constant loadable with addis */
6595 else if ((value & 0xffff) == 0
6596 && (value >> 31 == -1 || value >> 31 == 0))
6597 return 1;
6599 else if (TARGET_POWERPC64)
6601 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
6602 HOST_WIDE_INT high = value >> 31;
6604 if (high == 0 || high == -1)
6605 return 2;
6607 high >>= 1;
6609 if (low == 0)
6610 return num_insns_constant_wide (high) + 1;
6611 else if (high == 0)
6612 return num_insns_constant_wide (low) + 1;
6613 else
6614 return (num_insns_constant_wide (high)
6615 + num_insns_constant_wide (low) + 1);
6618 else
6619 return 2;
6623 num_insns_constant (rtx op, machine_mode mode)
6625 HOST_WIDE_INT low, high;
6627 switch (GET_CODE (op))
6629 case CONST_INT:
6630 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
6631 && rs6000_is_valid_and_mask (op, mode))
6632 return 2;
6633 else
6634 return num_insns_constant_wide (INTVAL (op));
6636 case CONST_WIDE_INT:
6638 int i;
6639 int ins = CONST_WIDE_INT_NUNITS (op) - 1;
6640 for (i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
6641 ins += num_insns_constant_wide (CONST_WIDE_INT_ELT (op, i));
6642 return ins;
6645 case CONST_DOUBLE:
6646 if (mode == SFmode || mode == SDmode)
6648 long l;
6650 if (DECIMAL_FLOAT_MODE_P (mode))
6651 REAL_VALUE_TO_TARGET_DECIMAL32
6652 (*CONST_DOUBLE_REAL_VALUE (op), l);
6653 else
6654 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), l);
6655 return num_insns_constant_wide ((HOST_WIDE_INT) l);
6658 long l[2];
6659 if (DECIMAL_FLOAT_MODE_P (mode))
6660 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (op), l);
6661 else
6662 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (op), l);
6663 high = l[WORDS_BIG_ENDIAN == 0];
6664 low = l[WORDS_BIG_ENDIAN != 0];
6666 if (TARGET_32BIT)
6667 return (num_insns_constant_wide (low)
6668 + num_insns_constant_wide (high));
6669 else
6671 if ((high == 0 && low >= 0)
6672 || (high == -1 && low < 0))
6673 return num_insns_constant_wide (low);
6675 else if (rs6000_is_valid_and_mask (op, mode))
6676 return 2;
6678 else if (low == 0)
6679 return num_insns_constant_wide (high) + 1;
6681 else
6682 return (num_insns_constant_wide (high)
6683 + num_insns_constant_wide (low) + 1);
6686 default:
6687 gcc_unreachable ();
6691 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
6692 If the mode of OP is MODE_VECTOR_INT, this simply returns the
6693 corresponding element of the vector, but for V4SFmode and V2SFmode,
6694 the corresponding "float" is interpreted as an SImode integer. */
6696 HOST_WIDE_INT
6697 const_vector_elt_as_int (rtx op, unsigned int elt)
6699 rtx tmp;
6701 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
6702 gcc_assert (GET_MODE (op) != V2DImode
6703 && GET_MODE (op) != V2DFmode);
6705 tmp = CONST_VECTOR_ELT (op, elt);
6706 if (GET_MODE (op) == V4SFmode
6707 || GET_MODE (op) == V2SFmode)
6708 tmp = gen_lowpart (SImode, tmp);
6709 return INTVAL (tmp);
6712 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
6713 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
6714 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
6715 all items are set to the same value and contain COPIES replicas of the
6716 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
6717 operand and the others are set to the value of the operand's msb. */
6719 static bool
6720 vspltis_constant (rtx op, unsigned step, unsigned copies)
6722 machine_mode mode = GET_MODE (op);
6723 machine_mode inner = GET_MODE_INNER (mode);
6725 unsigned i;
6726 unsigned nunits;
6727 unsigned bitsize;
6728 unsigned mask;
6730 HOST_WIDE_INT val;
6731 HOST_WIDE_INT splat_val;
6732 HOST_WIDE_INT msb_val;
6734 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
6735 return false;
6737 nunits = GET_MODE_NUNITS (mode);
6738 bitsize = GET_MODE_BITSIZE (inner);
6739 mask = GET_MODE_MASK (inner);
6741 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6742 splat_val = val;
6743 msb_val = val >= 0 ? 0 : -1;
6745 /* Construct the value to be splatted, if possible. If not, return 0. */
6746 for (i = 2; i <= copies; i *= 2)
6748 HOST_WIDE_INT small_val;
6749 bitsize /= 2;
6750 small_val = splat_val >> bitsize;
6751 mask >>= bitsize;
6752 if (splat_val != ((HOST_WIDE_INT)
6753 ((unsigned HOST_WIDE_INT) small_val << bitsize)
6754 | (small_val & mask)))
6755 return false;
6756 splat_val = small_val;
6759 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
6760 if (EASY_VECTOR_15 (splat_val))
6763 /* Also check if we can splat, and then add the result to itself. Do so if
6764 the value is positive, of if the splat instruction is using OP's mode;
6765 for splat_val < 0, the splat and the add should use the same mode. */
6766 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
6767 && (splat_val >= 0 || (step == 1 && copies == 1)))
6770 /* Also check if are loading up the most significant bit which can be done by
6771 loading up -1 and shifting the value left by -1. */
6772 else if (EASY_VECTOR_MSB (splat_val, inner))
6775 else
6776 return false;
6778 /* Check if VAL is present in every STEP-th element, and the
6779 other elements are filled with its most significant bit. */
6780 for (i = 1; i < nunits; ++i)
6782 HOST_WIDE_INT desired_val;
6783 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
6784 if ((i & (step - 1)) == 0)
6785 desired_val = val;
6786 else
6787 desired_val = msb_val;
6789 if (desired_val != const_vector_elt_as_int (op, elt))
6790 return false;
6793 return true;
6796 /* Like vsplitis_constant, but allow the value to be shifted left with a VSLDOI
6797 instruction, filling in the bottom elements with 0 or -1.
6799 Return 0 if the constant cannot be generated with VSLDOI. Return positive
6800 for the number of zeroes to shift in, or negative for the number of 0xff
6801 bytes to shift in.
6803 OP is a CONST_VECTOR. */
6806 vspltis_shifted (rtx op)
6808 machine_mode mode = GET_MODE (op);
6809 machine_mode inner = GET_MODE_INNER (mode);
6811 unsigned i, j;
6812 unsigned nunits;
6813 unsigned mask;
6815 HOST_WIDE_INT val;
6817 if (mode != V16QImode && mode != V8HImode && mode != V4SImode)
6818 return false;
6820 /* We need to create pseudo registers to do the shift, so don't recognize
6821 shift vector constants after reload. */
6822 if (!can_create_pseudo_p ())
6823 return false;
6825 nunits = GET_MODE_NUNITS (mode);
6826 mask = GET_MODE_MASK (inner);
6828 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? 0 : nunits - 1);
6830 /* Check if the value can really be the operand of a vspltis[bhw]. */
6831 if (EASY_VECTOR_15 (val))
6834 /* Also check if we are loading up the most significant bit which can be done
6835 by loading up -1 and shifting the value left by -1. */
6836 else if (EASY_VECTOR_MSB (val, inner))
6839 else
6840 return 0;
6842 /* Check if VAL is present in every STEP-th element until we find elements
6843 that are 0 or all 1 bits. */
6844 for (i = 1; i < nunits; ++i)
6846 unsigned elt = BYTES_BIG_ENDIAN ? i : nunits - 1 - i;
6847 HOST_WIDE_INT elt_val = const_vector_elt_as_int (op, elt);
6849 /* If the value isn't the splat value, check for the remaining elements
6850 being 0/-1. */
6851 if (val != elt_val)
6853 if (elt_val == 0)
6855 for (j = i+1; j < nunits; ++j)
6857 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6858 if (const_vector_elt_as_int (op, elt2) != 0)
6859 return 0;
6862 return (nunits - i) * GET_MODE_SIZE (inner);
6865 else if ((elt_val & mask) == mask)
6867 for (j = i+1; j < nunits; ++j)
6869 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6870 if ((const_vector_elt_as_int (op, elt2) & mask) != mask)
6871 return 0;
6874 return -((nunits - i) * GET_MODE_SIZE (inner));
6877 else
6878 return 0;
6882 /* If all elements are equal, we don't need to do VLSDOI. */
6883 return 0;
6887 /* Return true if OP is of the given MODE and can be synthesized
6888 with a vspltisb, vspltish or vspltisw. */
6890 bool
6891 easy_altivec_constant (rtx op, machine_mode mode)
6893 unsigned step, copies;
6895 if (mode == VOIDmode)
6896 mode = GET_MODE (op);
6897 else if (mode != GET_MODE (op))
6898 return false;
6900 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
6901 constants. */
6902 if (mode == V2DFmode)
6903 return zero_constant (op, mode);
6905 else if (mode == V2DImode)
6907 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
6908 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
6909 return false;
6911 if (zero_constant (op, mode))
6912 return true;
6914 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
6915 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
6916 return true;
6918 return false;
6921 /* V1TImode is a special container for TImode. Ignore for now. */
6922 else if (mode == V1TImode)
6923 return false;
6925 /* Start with a vspltisw. */
6926 step = GET_MODE_NUNITS (mode) / 4;
6927 copies = 1;
6929 if (vspltis_constant (op, step, copies))
6930 return true;
6932 /* Then try with a vspltish. */
6933 if (step == 1)
6934 copies <<= 1;
6935 else
6936 step >>= 1;
6938 if (vspltis_constant (op, step, copies))
6939 return true;
6941 /* And finally a vspltisb. */
6942 if (step == 1)
6943 copies <<= 1;
6944 else
6945 step >>= 1;
6947 if (vspltis_constant (op, step, copies))
6948 return true;
6950 if (vspltis_shifted (op) != 0)
6951 return true;
6953 return false;
6956 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
6957 result is OP. Abort if it is not possible. */
6960 gen_easy_altivec_constant (rtx op)
6962 machine_mode mode = GET_MODE (op);
6963 int nunits = GET_MODE_NUNITS (mode);
6964 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6965 unsigned step = nunits / 4;
6966 unsigned copies = 1;
6968 /* Start with a vspltisw. */
6969 if (vspltis_constant (op, step, copies))
6970 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
6972 /* Then try with a vspltish. */
6973 if (step == 1)
6974 copies <<= 1;
6975 else
6976 step >>= 1;
6978 if (vspltis_constant (op, step, copies))
6979 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
6981 /* And finally a vspltisb. */
6982 if (step == 1)
6983 copies <<= 1;
6984 else
6985 step >>= 1;
6987 if (vspltis_constant (op, step, copies))
6988 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
6990 gcc_unreachable ();
6993 /* Return true if OP is of the given MODE and can be synthesized with ISA 3.0
6994 instructions (xxspltib, vupkhsb/vextsb2w/vextb2d).
6996 Return the number of instructions needed (1 or 2) into the address pointed
6997 via NUM_INSNS_PTR.
6999 Return the constant that is being split via CONSTANT_PTR. */
7001 bool
7002 xxspltib_constant_p (rtx op,
7003 machine_mode mode,
7004 int *num_insns_ptr,
7005 int *constant_ptr)
7007 size_t nunits = GET_MODE_NUNITS (mode);
7008 size_t i;
7009 HOST_WIDE_INT value;
7010 rtx element;
7012 /* Set the returned values to out of bound values. */
7013 *num_insns_ptr = -1;
7014 *constant_ptr = 256;
7016 if (!TARGET_P9_VECTOR)
7017 return false;
7019 if (mode == VOIDmode)
7020 mode = GET_MODE (op);
7022 else if (mode != GET_MODE (op) && GET_MODE (op) != VOIDmode)
7023 return false;
7025 /* Handle (vec_duplicate <constant>). */
7026 if (GET_CODE (op) == VEC_DUPLICATE)
7028 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
7029 && mode != V2DImode)
7030 return false;
7032 element = XEXP (op, 0);
7033 if (!CONST_INT_P (element))
7034 return false;
7036 value = INTVAL (element);
7037 if (!IN_RANGE (value, -128, 127))
7038 return false;
7041 /* Handle (const_vector [...]). */
7042 else if (GET_CODE (op) == CONST_VECTOR)
7044 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
7045 && mode != V2DImode)
7046 return false;
7048 element = CONST_VECTOR_ELT (op, 0);
7049 if (!CONST_INT_P (element))
7050 return false;
7052 value = INTVAL (element);
7053 if (!IN_RANGE (value, -128, 127))
7054 return false;
7056 for (i = 1; i < nunits; i++)
7058 element = CONST_VECTOR_ELT (op, i);
7059 if (!CONST_INT_P (element))
7060 return false;
7062 if (value != INTVAL (element))
7063 return false;
7067 /* Handle integer constants being loaded into the upper part of the VSX
7068 register as a scalar. If the value isn't 0/-1, only allow it if the mode
7069 can go in Altivec registers. Prefer VSPLTISW/VUPKHSW over XXSPLITIB. */
7070 else if (CONST_INT_P (op))
7072 if (!SCALAR_INT_MODE_P (mode))
7073 return false;
7075 value = INTVAL (op);
7076 if (!IN_RANGE (value, -128, 127))
7077 return false;
7079 if (!IN_RANGE (value, -1, 0))
7081 if (!(reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID))
7082 return false;
7084 if (EASY_VECTOR_15 (value))
7085 return false;
7089 else
7090 return false;
7092 /* See if we could generate vspltisw/vspltish directly instead of xxspltib +
7093 sign extend. Special case 0/-1 to allow getting any VSX register instead
7094 of an Altivec register. */
7095 if ((mode == V4SImode || mode == V8HImode) && !IN_RANGE (value, -1, 0)
7096 && EASY_VECTOR_15 (value))
7097 return false;
7099 /* Return # of instructions and the constant byte for XXSPLTIB. */
7100 if (mode == V16QImode)
7101 *num_insns_ptr = 1;
7103 else if (IN_RANGE (value, -1, 0))
7104 *num_insns_ptr = 1;
7106 else
7107 *num_insns_ptr = 2;
7109 *constant_ptr = (int) value;
7110 return true;
7113 const char *
7114 output_vec_const_move (rtx *operands)
7116 int cst, cst2, shift;
7117 machine_mode mode;
7118 rtx dest, vec;
7120 dest = operands[0];
7121 vec = operands[1];
7122 mode = GET_MODE (dest);
7124 if (TARGET_VSX)
7126 bool dest_vmx_p = ALTIVEC_REGNO_P (REGNO (dest));
7127 int xxspltib_value = 256;
7128 int num_insns = -1;
7130 if (zero_constant (vec, mode))
7132 if (TARGET_P9_VECTOR)
7133 return "xxspltib %x0,0";
7135 else if (dest_vmx_p)
7136 return "vspltisw %0,0";
7138 else
7139 return "xxlxor %x0,%x0,%x0";
7142 if (all_ones_constant (vec, mode))
7144 if (TARGET_P9_VECTOR)
7145 return "xxspltib %x0,255";
7147 else if (dest_vmx_p)
7148 return "vspltisw %0,-1";
7150 else if (TARGET_P8_VECTOR)
7151 return "xxlorc %x0,%x0,%x0";
7153 else
7154 gcc_unreachable ();
7157 if (TARGET_P9_VECTOR
7158 && xxspltib_constant_p (vec, mode, &num_insns, &xxspltib_value))
7160 if (num_insns == 1)
7162 operands[2] = GEN_INT (xxspltib_value & 0xff);
7163 return "xxspltib %x0,%2";
7166 return "#";
7170 if (TARGET_ALTIVEC)
7172 rtx splat_vec;
7174 gcc_assert (ALTIVEC_REGNO_P (REGNO (dest)));
7175 if (zero_constant (vec, mode))
7176 return "vspltisw %0,0";
7178 if (all_ones_constant (vec, mode))
7179 return "vspltisw %0,-1";
7181 /* Do we need to construct a value using VSLDOI? */
7182 shift = vspltis_shifted (vec);
7183 if (shift != 0)
7184 return "#";
7186 splat_vec = gen_easy_altivec_constant (vec);
7187 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
7188 operands[1] = XEXP (splat_vec, 0);
7189 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
7190 return "#";
7192 switch (GET_MODE (splat_vec))
7194 case E_V4SImode:
7195 return "vspltisw %0,%1";
7197 case E_V8HImode:
7198 return "vspltish %0,%1";
7200 case E_V16QImode:
7201 return "vspltisb %0,%1";
7203 default:
7204 gcc_unreachable ();
7208 gcc_assert (TARGET_SPE);
7210 /* Vector constant 0 is handled as a splitter of V2SI, and in the
7211 pattern of V1DI, V4HI, and V2SF.
7213 FIXME: We should probably return # and add post reload
7214 splitters for these, but this way is so easy ;-). */
7215 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
7216 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
7217 operands[1] = CONST_VECTOR_ELT (vec, 0);
7218 operands[2] = CONST_VECTOR_ELT (vec, 1);
7219 if (cst == cst2)
7220 return "li %0,%1\n\tevmergelo %0,%0,%0";
7221 else if (WORDS_BIG_ENDIAN)
7222 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
7223 else
7224 return "li %0,%2\n\tevmergelo %0,%0,%0\n\tli %0,%1";
7227 /* Initialize TARGET of vector PAIRED to VALS. */
7229 void
7230 paired_expand_vector_init (rtx target, rtx vals)
7232 machine_mode mode = GET_MODE (target);
7233 int n_elts = GET_MODE_NUNITS (mode);
7234 int n_var = 0;
7235 rtx x, new_rtx, tmp, constant_op, op1, op2;
7236 int i;
7238 for (i = 0; i < n_elts; ++i)
7240 x = XVECEXP (vals, 0, i);
7241 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
7242 ++n_var;
7244 if (n_var == 0)
7246 /* Load from constant pool. */
7247 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
7248 return;
7251 if (n_var == 2)
7253 /* The vector is initialized only with non-constants. */
7254 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
7255 XVECEXP (vals, 0, 1));
7257 emit_move_insn (target, new_rtx);
7258 return;
7261 /* One field is non-constant and the other one is a constant. Load the
7262 constant from the constant pool and use ps_merge instruction to
7263 construct the whole vector. */
7264 op1 = XVECEXP (vals, 0, 0);
7265 op2 = XVECEXP (vals, 0, 1);
7267 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
7269 tmp = gen_reg_rtx (GET_MODE (constant_op));
7270 emit_move_insn (tmp, constant_op);
7272 if (CONSTANT_P (op1))
7273 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
7274 else
7275 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
7277 emit_move_insn (target, new_rtx);
7280 void
7281 paired_expand_vector_move (rtx operands[])
7283 rtx op0 = operands[0], op1 = operands[1];
7285 emit_move_insn (op0, op1);
7288 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
7289 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
7290 operands for the relation operation COND. This is a recursive
7291 function. */
7293 static void
7294 paired_emit_vector_compare (enum rtx_code rcode,
7295 rtx dest, rtx op0, rtx op1,
7296 rtx cc_op0, rtx cc_op1)
7298 rtx tmp = gen_reg_rtx (V2SFmode);
7299 rtx tmp1, max, min;
7301 gcc_assert (TARGET_PAIRED_FLOAT);
7302 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
7304 switch (rcode)
7306 case LT:
7307 case LTU:
7308 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
7309 return;
7310 case GE:
7311 case GEU:
7312 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
7313 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
7314 return;
7315 case LE:
7316 case LEU:
7317 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
7318 return;
7319 case GT:
7320 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
7321 return;
7322 case EQ:
7323 tmp1 = gen_reg_rtx (V2SFmode);
7324 max = gen_reg_rtx (V2SFmode);
7325 min = gen_reg_rtx (V2SFmode);
7326 gen_reg_rtx (V2SFmode);
7328 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
7329 emit_insn (gen_selv2sf4
7330 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
7331 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
7332 emit_insn (gen_selv2sf4
7333 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
7334 emit_insn (gen_subv2sf3 (tmp1, min, max));
7335 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
7336 return;
7337 case NE:
7338 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
7339 return;
7340 case UNLE:
7341 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
7342 return;
7343 case UNLT:
7344 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
7345 return;
7346 case UNGE:
7347 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
7348 return;
7349 case UNGT:
7350 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
7351 return;
7352 default:
7353 gcc_unreachable ();
7356 return;
7359 /* Emit vector conditional expression.
7360 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
7361 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
7364 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
7365 rtx cond, rtx cc_op0, rtx cc_op1)
7367 enum rtx_code rcode = GET_CODE (cond);
7369 if (!TARGET_PAIRED_FLOAT)
7370 return 0;
7372 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
7374 return 1;
7377 /* Initialize vector TARGET to VALS. */
7379 void
7380 rs6000_expand_vector_init (rtx target, rtx vals)
7382 machine_mode mode = GET_MODE (target);
7383 machine_mode inner_mode = GET_MODE_INNER (mode);
7384 int n_elts = GET_MODE_NUNITS (mode);
7385 int n_var = 0, one_var = -1;
7386 bool all_same = true, all_const_zero = true;
7387 rtx x, mem;
7388 int i;
7390 for (i = 0; i < n_elts; ++i)
7392 x = XVECEXP (vals, 0, i);
7393 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
7394 ++n_var, one_var = i;
7395 else if (x != CONST0_RTX (inner_mode))
7396 all_const_zero = false;
7398 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
7399 all_same = false;
7402 if (n_var == 0)
7404 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
7405 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
7406 if ((int_vector_p || TARGET_VSX) && all_const_zero)
7408 /* Zero register. */
7409 emit_move_insn (target, CONST0_RTX (mode));
7410 return;
7412 else if (int_vector_p && easy_vector_constant (const_vec, mode))
7414 /* Splat immediate. */
7415 emit_insn (gen_rtx_SET (target, const_vec));
7416 return;
7418 else
7420 /* Load from constant pool. */
7421 emit_move_insn (target, const_vec);
7422 return;
7426 /* Double word values on VSX can use xxpermdi or lxvdsx. */
7427 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
7429 rtx op[2];
7430 size_t i;
7431 size_t num_elements = all_same ? 1 : 2;
7432 for (i = 0; i < num_elements; i++)
7434 op[i] = XVECEXP (vals, 0, i);
7435 /* Just in case there is a SUBREG with a smaller mode, do a
7436 conversion. */
7437 if (GET_MODE (op[i]) != inner_mode)
7439 rtx tmp = gen_reg_rtx (inner_mode);
7440 convert_move (tmp, op[i], 0);
7441 op[i] = tmp;
7443 /* Allow load with splat double word. */
7444 else if (MEM_P (op[i]))
7446 if (!all_same)
7447 op[i] = force_reg (inner_mode, op[i]);
7449 else if (!REG_P (op[i]))
7450 op[i] = force_reg (inner_mode, op[i]);
7453 if (all_same)
7455 if (mode == V2DFmode)
7456 emit_insn (gen_vsx_splat_v2df (target, op[0]));
7457 else
7458 emit_insn (gen_vsx_splat_v2di (target, op[0]));
7460 else
7462 if (mode == V2DFmode)
7463 emit_insn (gen_vsx_concat_v2df (target, op[0], op[1]));
7464 else
7465 emit_insn (gen_vsx_concat_v2di (target, op[0], op[1]));
7467 return;
7470 /* Special case initializing vector int if we are on 64-bit systems with
7471 direct move or we have the ISA 3.0 instructions. */
7472 if (mode == V4SImode && VECTOR_MEM_VSX_P (V4SImode)
7473 && TARGET_DIRECT_MOVE_64BIT)
7475 if (all_same)
7477 rtx element0 = XVECEXP (vals, 0, 0);
7478 if (MEM_P (element0))
7479 element0 = rs6000_address_for_fpconvert (element0);
7480 else
7481 element0 = force_reg (SImode, element0);
7483 if (TARGET_P9_VECTOR)
7484 emit_insn (gen_vsx_splat_v4si (target, element0));
7485 else
7487 rtx tmp = gen_reg_rtx (DImode);
7488 emit_insn (gen_zero_extendsidi2 (tmp, element0));
7489 emit_insn (gen_vsx_splat_v4si_di (target, tmp));
7491 return;
7493 else
7495 rtx elements[4];
7496 size_t i;
7498 for (i = 0; i < 4; i++)
7500 elements[i] = XVECEXP (vals, 0, i);
7501 if (!CONST_INT_P (elements[i]) && !REG_P (elements[i]))
7502 elements[i] = copy_to_mode_reg (SImode, elements[i]);
7505 emit_insn (gen_vsx_init_v4si (target, elements[0], elements[1],
7506 elements[2], elements[3]));
7507 return;
7511 /* With single precision floating point on VSX, know that internally single
7512 precision is actually represented as a double, and either make 2 V2DF
7513 vectors, and convert these vectors to single precision, or do one
7514 conversion, and splat the result to the other elements. */
7515 if (mode == V4SFmode && VECTOR_MEM_VSX_P (V4SFmode))
7517 if (all_same)
7519 rtx element0 = XVECEXP (vals, 0, 0);
7521 if (TARGET_P9_VECTOR)
7523 if (MEM_P (element0))
7524 element0 = rs6000_address_for_fpconvert (element0);
7526 emit_insn (gen_vsx_splat_v4sf (target, element0));
7529 else
7531 rtx freg = gen_reg_rtx (V4SFmode);
7532 rtx sreg = force_reg (SFmode, element0);
7533 rtx cvt = (TARGET_XSCVDPSPN
7534 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
7535 : gen_vsx_xscvdpsp_scalar (freg, sreg));
7537 emit_insn (cvt);
7538 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg,
7539 const0_rtx));
7542 else
7544 rtx dbl_even = gen_reg_rtx (V2DFmode);
7545 rtx dbl_odd = gen_reg_rtx (V2DFmode);
7546 rtx flt_even = gen_reg_rtx (V4SFmode);
7547 rtx flt_odd = gen_reg_rtx (V4SFmode);
7548 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
7549 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
7550 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
7551 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
7553 /* Use VMRGEW if we can instead of doing a permute. */
7554 if (TARGET_P8_VECTOR)
7556 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op2));
7557 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op1, op3));
7558 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
7559 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
7560 if (BYTES_BIG_ENDIAN)
7561 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_even, flt_odd));
7562 else
7563 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_odd, flt_even));
7565 else
7567 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
7568 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
7569 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
7570 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
7571 rs6000_expand_extract_even (target, flt_even, flt_odd);
7574 return;
7577 /* Special case initializing vector short/char that are splats if we are on
7578 64-bit systems with direct move. */
7579 if (all_same && TARGET_DIRECT_MOVE_64BIT
7580 && (mode == V16QImode || mode == V8HImode))
7582 rtx op0 = XVECEXP (vals, 0, 0);
7583 rtx di_tmp = gen_reg_rtx (DImode);
7585 if (!REG_P (op0))
7586 op0 = force_reg (GET_MODE_INNER (mode), op0);
7588 if (mode == V16QImode)
7590 emit_insn (gen_zero_extendqidi2 (di_tmp, op0));
7591 emit_insn (gen_vsx_vspltb_di (target, di_tmp));
7592 return;
7595 if (mode == V8HImode)
7597 emit_insn (gen_zero_extendhidi2 (di_tmp, op0));
7598 emit_insn (gen_vsx_vsplth_di (target, di_tmp));
7599 return;
7603 /* Store value to stack temp. Load vector element. Splat. However, splat
7604 of 64-bit items is not supported on Altivec. */
7605 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
7607 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
7608 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
7609 XVECEXP (vals, 0, 0));
7610 x = gen_rtx_UNSPEC (VOIDmode,
7611 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
7612 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7613 gen_rtvec (2,
7614 gen_rtx_SET (target, mem),
7615 x)));
7616 x = gen_rtx_VEC_SELECT (inner_mode, target,
7617 gen_rtx_PARALLEL (VOIDmode,
7618 gen_rtvec (1, const0_rtx)));
7619 emit_insn (gen_rtx_SET (target, gen_rtx_VEC_DUPLICATE (mode, x)));
7620 return;
7623 /* One field is non-constant. Load constant then overwrite
7624 varying field. */
7625 if (n_var == 1)
7627 rtx copy = copy_rtx (vals);
7629 /* Load constant part of vector, substitute neighboring value for
7630 varying element. */
7631 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
7632 rs6000_expand_vector_init (target, copy);
7634 /* Insert variable. */
7635 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
7636 return;
7639 /* Construct the vector in memory one field at a time
7640 and load the whole vector. */
7641 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
7642 for (i = 0; i < n_elts; i++)
7643 emit_move_insn (adjust_address_nv (mem, inner_mode,
7644 i * GET_MODE_SIZE (inner_mode)),
7645 XVECEXP (vals, 0, i));
7646 emit_move_insn (target, mem);
7649 /* Set field ELT of TARGET to VAL. */
7651 void
7652 rs6000_expand_vector_set (rtx target, rtx val, int elt)
7654 machine_mode mode = GET_MODE (target);
7655 machine_mode inner_mode = GET_MODE_INNER (mode);
7656 rtx reg = gen_reg_rtx (mode);
7657 rtx mask, mem, x;
7658 int width = GET_MODE_SIZE (inner_mode);
7659 int i;
7661 val = force_reg (GET_MODE (val), val);
7663 if (VECTOR_MEM_VSX_P (mode))
7665 rtx insn = NULL_RTX;
7666 rtx elt_rtx = GEN_INT (elt);
7668 if (mode == V2DFmode)
7669 insn = gen_vsx_set_v2df (target, target, val, elt_rtx);
7671 else if (mode == V2DImode)
7672 insn = gen_vsx_set_v2di (target, target, val, elt_rtx);
7674 else if (TARGET_P9_VECTOR && TARGET_VSX_SMALL_INTEGER
7675 && TARGET_UPPER_REGS_DI && TARGET_POWERPC64)
7677 if (mode == V4SImode)
7678 insn = gen_vsx_set_v4si_p9 (target, target, val, elt_rtx);
7679 else if (mode == V8HImode)
7680 insn = gen_vsx_set_v8hi_p9 (target, target, val, elt_rtx);
7681 else if (mode == V16QImode)
7682 insn = gen_vsx_set_v16qi_p9 (target, target, val, elt_rtx);
7685 if (insn)
7687 emit_insn (insn);
7688 return;
7692 /* Simplify setting single element vectors like V1TImode. */
7693 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
7695 emit_move_insn (target, gen_lowpart (mode, val));
7696 return;
7699 /* Load single variable value. */
7700 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
7701 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
7702 x = gen_rtx_UNSPEC (VOIDmode,
7703 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
7704 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7705 gen_rtvec (2,
7706 gen_rtx_SET (reg, mem),
7707 x)));
7709 /* Linear sequence. */
7710 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
7711 for (i = 0; i < 16; ++i)
7712 XVECEXP (mask, 0, i) = GEN_INT (i);
7714 /* Set permute mask to insert element into target. */
7715 for (i = 0; i < width; ++i)
7716 XVECEXP (mask, 0, elt*width + i)
7717 = GEN_INT (i + 0x10);
7718 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
7720 if (BYTES_BIG_ENDIAN)
7721 x = gen_rtx_UNSPEC (mode,
7722 gen_rtvec (3, target, reg,
7723 force_reg (V16QImode, x)),
7724 UNSPEC_VPERM);
7725 else
7727 if (TARGET_P9_VECTOR)
7728 x = gen_rtx_UNSPEC (mode,
7729 gen_rtvec (3, target, reg,
7730 force_reg (V16QImode, x)),
7731 UNSPEC_VPERMR);
7732 else
7734 /* Invert selector. We prefer to generate VNAND on P8 so
7735 that future fusion opportunities can kick in, but must
7736 generate VNOR elsewhere. */
7737 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
7738 rtx iorx = (TARGET_P8_VECTOR
7739 ? gen_rtx_IOR (V16QImode, notx, notx)
7740 : gen_rtx_AND (V16QImode, notx, notx));
7741 rtx tmp = gen_reg_rtx (V16QImode);
7742 emit_insn (gen_rtx_SET (tmp, iorx));
7744 /* Permute with operands reversed and adjusted selector. */
7745 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
7746 UNSPEC_VPERM);
7750 emit_insn (gen_rtx_SET (target, x));
7753 /* Extract field ELT from VEC into TARGET. */
7755 void
7756 rs6000_expand_vector_extract (rtx target, rtx vec, rtx elt)
7758 machine_mode mode = GET_MODE (vec);
7759 machine_mode inner_mode = GET_MODE_INNER (mode);
7760 rtx mem;
7762 if (VECTOR_MEM_VSX_P (mode) && CONST_INT_P (elt))
7764 switch (mode)
7766 default:
7767 break;
7768 case E_V1TImode:
7769 gcc_assert (INTVAL (elt) == 0 && inner_mode == TImode);
7770 emit_move_insn (target, gen_lowpart (TImode, vec));
7771 break;
7772 case E_V2DFmode:
7773 emit_insn (gen_vsx_extract_v2df (target, vec, elt));
7774 return;
7775 case E_V2DImode:
7776 emit_insn (gen_vsx_extract_v2di (target, vec, elt));
7777 return;
7778 case E_V4SFmode:
7779 emit_insn (gen_vsx_extract_v4sf (target, vec, elt));
7780 return;
7781 case E_V16QImode:
7782 if (TARGET_DIRECT_MOVE_64BIT)
7784 emit_insn (gen_vsx_extract_v16qi (target, vec, elt));
7785 return;
7787 else
7788 break;
7789 case E_V8HImode:
7790 if (TARGET_DIRECT_MOVE_64BIT)
7792 emit_insn (gen_vsx_extract_v8hi (target, vec, elt));
7793 return;
7795 else
7796 break;
7797 case E_V4SImode:
7798 if (TARGET_DIRECT_MOVE_64BIT)
7800 emit_insn (gen_vsx_extract_v4si (target, vec, elt));
7801 return;
7803 break;
7806 else if (VECTOR_MEM_VSX_P (mode) && !CONST_INT_P (elt)
7807 && TARGET_DIRECT_MOVE_64BIT)
7809 if (GET_MODE (elt) != DImode)
7811 rtx tmp = gen_reg_rtx (DImode);
7812 convert_move (tmp, elt, 0);
7813 elt = tmp;
7815 else if (!REG_P (elt))
7816 elt = force_reg (DImode, elt);
7818 switch (mode)
7820 case E_V2DFmode:
7821 emit_insn (gen_vsx_extract_v2df_var (target, vec, elt));
7822 return;
7824 case E_V2DImode:
7825 emit_insn (gen_vsx_extract_v2di_var (target, vec, elt));
7826 return;
7828 case E_V4SFmode:
7829 emit_insn (gen_vsx_extract_v4sf_var (target, vec, elt));
7830 return;
7832 case E_V4SImode:
7833 emit_insn (gen_vsx_extract_v4si_var (target, vec, elt));
7834 return;
7836 case E_V8HImode:
7837 emit_insn (gen_vsx_extract_v8hi_var (target, vec, elt));
7838 return;
7840 case E_V16QImode:
7841 emit_insn (gen_vsx_extract_v16qi_var (target, vec, elt));
7842 return;
7844 default:
7845 gcc_unreachable ();
7849 gcc_assert (CONST_INT_P (elt));
7851 /* Allocate mode-sized buffer. */
7852 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
7854 emit_move_insn (mem, vec);
7856 /* Add offset to field within buffer matching vector element. */
7857 mem = adjust_address_nv (mem, inner_mode,
7858 INTVAL (elt) * GET_MODE_SIZE (inner_mode));
7860 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
7863 /* Helper function to return the register number of a RTX. */
7864 static inline int
7865 regno_or_subregno (rtx op)
7867 if (REG_P (op))
7868 return REGNO (op);
7869 else if (SUBREG_P (op))
7870 return subreg_regno (op);
7871 else
7872 gcc_unreachable ();
7875 /* Adjust a memory address (MEM) of a vector type to point to a scalar field
7876 within the vector (ELEMENT) with a mode (SCALAR_MODE). Use a base register
7877 temporary (BASE_TMP) to fixup the address. Return the new memory address
7878 that is valid for reads or writes to a given register (SCALAR_REG). */
7881 rs6000_adjust_vec_address (rtx scalar_reg,
7882 rtx mem,
7883 rtx element,
7884 rtx base_tmp,
7885 machine_mode scalar_mode)
7887 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
7888 rtx addr = XEXP (mem, 0);
7889 rtx element_offset;
7890 rtx new_addr;
7891 bool valid_addr_p;
7893 /* Vector addresses should not have PRE_INC, PRE_DEC, or PRE_MODIFY. */
7894 gcc_assert (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC);
7896 /* Calculate what we need to add to the address to get the element
7897 address. */
7898 if (CONST_INT_P (element))
7899 element_offset = GEN_INT (INTVAL (element) * scalar_size);
7900 else
7902 int byte_shift = exact_log2 (scalar_size);
7903 gcc_assert (byte_shift >= 0);
7905 if (byte_shift == 0)
7906 element_offset = element;
7908 else
7910 if (TARGET_POWERPC64)
7911 emit_insn (gen_ashldi3 (base_tmp, element, GEN_INT (byte_shift)));
7912 else
7913 emit_insn (gen_ashlsi3 (base_tmp, element, GEN_INT (byte_shift)));
7915 element_offset = base_tmp;
7919 /* Create the new address pointing to the element within the vector. If we
7920 are adding 0, we don't have to change the address. */
7921 if (element_offset == const0_rtx)
7922 new_addr = addr;
7924 /* A simple indirect address can be converted into a reg + offset
7925 address. */
7926 else if (REG_P (addr) || SUBREG_P (addr))
7927 new_addr = gen_rtx_PLUS (Pmode, addr, element_offset);
7929 /* Optimize D-FORM addresses with constant offset with a constant element, to
7930 include the element offset in the address directly. */
7931 else if (GET_CODE (addr) == PLUS)
7933 rtx op0 = XEXP (addr, 0);
7934 rtx op1 = XEXP (addr, 1);
7935 rtx insn;
7937 gcc_assert (REG_P (op0) || SUBREG_P (op0));
7938 if (CONST_INT_P (op1) && CONST_INT_P (element_offset))
7940 HOST_WIDE_INT offset = INTVAL (op1) + INTVAL (element_offset);
7941 rtx offset_rtx = GEN_INT (offset);
7943 if (IN_RANGE (offset, -32768, 32767)
7944 && (scalar_size < 8 || (offset & 0x3) == 0))
7945 new_addr = gen_rtx_PLUS (Pmode, op0, offset_rtx);
7946 else
7948 emit_move_insn (base_tmp, offset_rtx);
7949 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7952 else
7954 bool op1_reg_p = (REG_P (op1) || SUBREG_P (op1));
7955 bool ele_reg_p = (REG_P (element_offset) || SUBREG_P (element_offset));
7957 /* Note, ADDI requires the register being added to be a base
7958 register. If the register was R0, load it up into the temporary
7959 and do the add. */
7960 if (op1_reg_p
7961 && (ele_reg_p || reg_or_subregno (op1) != FIRST_GPR_REGNO))
7963 insn = gen_add3_insn (base_tmp, op1, element_offset);
7964 gcc_assert (insn != NULL_RTX);
7965 emit_insn (insn);
7968 else if (ele_reg_p
7969 && reg_or_subregno (element_offset) != FIRST_GPR_REGNO)
7971 insn = gen_add3_insn (base_tmp, element_offset, op1);
7972 gcc_assert (insn != NULL_RTX);
7973 emit_insn (insn);
7976 else
7978 emit_move_insn (base_tmp, op1);
7979 emit_insn (gen_add2_insn (base_tmp, element_offset));
7982 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7986 else
7988 emit_move_insn (base_tmp, addr);
7989 new_addr = gen_rtx_PLUS (Pmode, base_tmp, element_offset);
7992 /* If we have a PLUS, we need to see whether the particular register class
7993 allows for D-FORM or X-FORM addressing. */
7994 if (GET_CODE (new_addr) == PLUS)
7996 rtx op1 = XEXP (new_addr, 1);
7997 addr_mask_type addr_mask;
7998 int scalar_regno = regno_or_subregno (scalar_reg);
8000 gcc_assert (scalar_regno < FIRST_PSEUDO_REGISTER);
8001 if (INT_REGNO_P (scalar_regno))
8002 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_GPR];
8004 else if (FP_REGNO_P (scalar_regno))
8005 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_FPR];
8007 else if (ALTIVEC_REGNO_P (scalar_regno))
8008 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_VMX];
8010 else
8011 gcc_unreachable ();
8013 if (REG_P (op1) || SUBREG_P (op1))
8014 valid_addr_p = (addr_mask & RELOAD_REG_INDEXED) != 0;
8015 else
8016 valid_addr_p = (addr_mask & RELOAD_REG_OFFSET) != 0;
8019 else if (REG_P (new_addr) || SUBREG_P (new_addr))
8020 valid_addr_p = true;
8022 else
8023 valid_addr_p = false;
8025 if (!valid_addr_p)
8027 emit_move_insn (base_tmp, new_addr);
8028 new_addr = base_tmp;
8031 return change_address (mem, scalar_mode, new_addr);
8034 /* Split a variable vec_extract operation into the component instructions. */
8036 void
8037 rs6000_split_vec_extract_var (rtx dest, rtx src, rtx element, rtx tmp_gpr,
8038 rtx tmp_altivec)
8040 machine_mode mode = GET_MODE (src);
8041 machine_mode scalar_mode = GET_MODE (dest);
8042 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
8043 int byte_shift = exact_log2 (scalar_size);
8045 gcc_assert (byte_shift >= 0);
8047 /* If we are given a memory address, optimize to load just the element. We
8048 don't have to adjust the vector element number on little endian
8049 systems. */
8050 if (MEM_P (src))
8052 gcc_assert (REG_P (tmp_gpr));
8053 emit_move_insn (dest, rs6000_adjust_vec_address (dest, src, element,
8054 tmp_gpr, scalar_mode));
8055 return;
8058 else if (REG_P (src) || SUBREG_P (src))
8060 int bit_shift = byte_shift + 3;
8061 rtx element2;
8062 int dest_regno = regno_or_subregno (dest);
8063 int src_regno = regno_or_subregno (src);
8064 int element_regno = regno_or_subregno (element);
8066 gcc_assert (REG_P (tmp_gpr));
8068 /* See if we want to generate VEXTU{B,H,W}{L,R}X if the destination is in
8069 a general purpose register. */
8070 if (TARGET_P9_VECTOR
8071 && (mode == V16QImode || mode == V8HImode || mode == V4SImode)
8072 && INT_REGNO_P (dest_regno)
8073 && ALTIVEC_REGNO_P (src_regno)
8074 && INT_REGNO_P (element_regno))
8076 rtx dest_si = gen_rtx_REG (SImode, dest_regno);
8077 rtx element_si = gen_rtx_REG (SImode, element_regno);
8079 if (mode == V16QImode)
8080 emit_insn (VECTOR_ELT_ORDER_BIG
8081 ? gen_vextublx (dest_si, element_si, src)
8082 : gen_vextubrx (dest_si, element_si, src));
8084 else if (mode == V8HImode)
8086 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
8087 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const1_rtx));
8088 emit_insn (VECTOR_ELT_ORDER_BIG
8089 ? gen_vextuhlx (dest_si, tmp_gpr_si, src)
8090 : gen_vextuhrx (dest_si, tmp_gpr_si, src));
8094 else
8096 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
8097 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const2_rtx));
8098 emit_insn (VECTOR_ELT_ORDER_BIG
8099 ? gen_vextuwlx (dest_si, tmp_gpr_si, src)
8100 : gen_vextuwrx (dest_si, tmp_gpr_si, src));
8103 return;
8107 gcc_assert (REG_P (tmp_altivec));
8109 /* For little endian, adjust element ordering. For V2DI/V2DF, we can use
8110 an XOR, otherwise we need to subtract. The shift amount is so VSLO
8111 will shift the element into the upper position (adding 3 to convert a
8112 byte shift into a bit shift). */
8113 if (scalar_size == 8)
8115 if (!VECTOR_ELT_ORDER_BIG)
8117 emit_insn (gen_xordi3 (tmp_gpr, element, const1_rtx));
8118 element2 = tmp_gpr;
8120 else
8121 element2 = element;
8123 /* Generate RLDIC directly to shift left 6 bits and retrieve 1
8124 bit. */
8125 emit_insn (gen_rtx_SET (tmp_gpr,
8126 gen_rtx_AND (DImode,
8127 gen_rtx_ASHIFT (DImode,
8128 element2,
8129 GEN_INT (6)),
8130 GEN_INT (64))));
8132 else
8134 if (!VECTOR_ELT_ORDER_BIG)
8136 rtx num_ele_m1 = GEN_INT (GET_MODE_NUNITS (mode) - 1);
8138 emit_insn (gen_anddi3 (tmp_gpr, element, num_ele_m1));
8139 emit_insn (gen_subdi3 (tmp_gpr, num_ele_m1, tmp_gpr));
8140 element2 = tmp_gpr;
8142 else
8143 element2 = element;
8145 emit_insn (gen_ashldi3 (tmp_gpr, element2, GEN_INT (bit_shift)));
8148 /* Get the value into the lower byte of the Altivec register where VSLO
8149 expects it. */
8150 if (TARGET_P9_VECTOR)
8151 emit_insn (gen_vsx_splat_v2di (tmp_altivec, tmp_gpr));
8152 else if (can_create_pseudo_p ())
8153 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_gpr, tmp_gpr));
8154 else
8156 rtx tmp_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
8157 emit_move_insn (tmp_di, tmp_gpr);
8158 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_di, tmp_di));
8161 /* Do the VSLO to get the value into the final location. */
8162 switch (mode)
8164 case E_V2DFmode:
8165 emit_insn (gen_vsx_vslo_v2df (dest, src, tmp_altivec));
8166 return;
8168 case E_V2DImode:
8169 emit_insn (gen_vsx_vslo_v2di (dest, src, tmp_altivec));
8170 return;
8172 case E_V4SFmode:
8174 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
8175 rtx tmp_altivec_v4sf = gen_rtx_REG (V4SFmode, REGNO (tmp_altivec));
8176 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
8177 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
8178 tmp_altivec));
8180 emit_insn (gen_vsx_xscvspdp_scalar2 (dest, tmp_altivec_v4sf));
8181 return;
8184 case E_V4SImode:
8185 case E_V8HImode:
8186 case E_V16QImode:
8188 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
8189 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
8190 rtx tmp_gpr_di = gen_rtx_REG (DImode, REGNO (dest));
8191 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
8192 tmp_altivec));
8193 emit_move_insn (tmp_gpr_di, tmp_altivec_di);
8194 emit_insn (gen_ashrdi3 (tmp_gpr_di, tmp_gpr_di,
8195 GEN_INT (64 - (8 * scalar_size))));
8196 return;
8199 default:
8200 gcc_unreachable ();
8203 return;
8205 else
8206 gcc_unreachable ();
8209 /* Helper function for rs6000_split_v4si_init to build up a DImode value from
8210 two SImode values. */
8212 static void
8213 rs6000_split_v4si_init_di_reg (rtx dest, rtx si1, rtx si2, rtx tmp)
8215 const unsigned HOST_WIDE_INT mask_32bit = HOST_WIDE_INT_C (0xffffffff);
8217 if (CONST_INT_P (si1) && CONST_INT_P (si2))
8219 unsigned HOST_WIDE_INT const1 = (UINTVAL (si1) & mask_32bit) << 32;
8220 unsigned HOST_WIDE_INT const2 = UINTVAL (si2) & mask_32bit;
8222 emit_move_insn (dest, GEN_INT (const1 | const2));
8223 return;
8226 /* Put si1 into upper 32-bits of dest. */
8227 if (CONST_INT_P (si1))
8228 emit_move_insn (dest, GEN_INT ((UINTVAL (si1) & mask_32bit) << 32));
8229 else
8231 /* Generate RLDIC. */
8232 rtx si1_di = gen_rtx_REG (DImode, regno_or_subregno (si1));
8233 rtx shift_rtx = gen_rtx_ASHIFT (DImode, si1_di, GEN_INT (32));
8234 rtx mask_rtx = GEN_INT (mask_32bit << 32);
8235 rtx and_rtx = gen_rtx_AND (DImode, shift_rtx, mask_rtx);
8236 gcc_assert (!reg_overlap_mentioned_p (dest, si1));
8237 emit_insn (gen_rtx_SET (dest, and_rtx));
8240 /* Put si2 into the temporary. */
8241 gcc_assert (!reg_overlap_mentioned_p (dest, tmp));
8242 if (CONST_INT_P (si2))
8243 emit_move_insn (tmp, GEN_INT (UINTVAL (si2) & mask_32bit));
8244 else
8245 emit_insn (gen_zero_extendsidi2 (tmp, si2));
8247 /* Combine the two parts. */
8248 emit_insn (gen_iordi3 (dest, dest, tmp));
8249 return;
8252 /* Split a V4SI initialization. */
8254 void
8255 rs6000_split_v4si_init (rtx operands[])
8257 rtx dest = operands[0];
8259 /* Destination is a GPR, build up the two DImode parts in place. */
8260 if (REG_P (dest) || SUBREG_P (dest))
8262 int d_regno = regno_or_subregno (dest);
8263 rtx scalar1 = operands[1];
8264 rtx scalar2 = operands[2];
8265 rtx scalar3 = operands[3];
8266 rtx scalar4 = operands[4];
8267 rtx tmp1 = operands[5];
8268 rtx tmp2 = operands[6];
8270 /* Even though we only need one temporary (plus the destination, which
8271 has an early clobber constraint, try to use two temporaries, one for
8272 each double word created. That way the 2nd insn scheduling pass can
8273 rearrange things so the two parts are done in parallel. */
8274 if (BYTES_BIG_ENDIAN)
8276 rtx di_lo = gen_rtx_REG (DImode, d_regno);
8277 rtx di_hi = gen_rtx_REG (DImode, d_regno + 1);
8278 rs6000_split_v4si_init_di_reg (di_lo, scalar1, scalar2, tmp1);
8279 rs6000_split_v4si_init_di_reg (di_hi, scalar3, scalar4, tmp2);
8281 else
8283 rtx di_lo = gen_rtx_REG (DImode, d_regno + 1);
8284 rtx di_hi = gen_rtx_REG (DImode, d_regno);
8285 gcc_assert (!VECTOR_ELT_ORDER_BIG);
8286 rs6000_split_v4si_init_di_reg (di_lo, scalar4, scalar3, tmp1);
8287 rs6000_split_v4si_init_di_reg (di_hi, scalar2, scalar1, tmp2);
8289 return;
8292 else
8293 gcc_unreachable ();
8296 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
8298 bool
8299 invalid_e500_subreg (rtx op, machine_mode mode)
8301 if (TARGET_E500_DOUBLE)
8303 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
8304 subreg:TI and reg:TF. Decimal float modes are like integer
8305 modes (only low part of each register used) for this
8306 purpose. */
8307 if (GET_CODE (op) == SUBREG
8308 && (mode == SImode || mode == DImode || mode == TImode
8309 || mode == DDmode || mode == TDmode || mode == PTImode)
8310 && REG_P (SUBREG_REG (op))
8311 && (GET_MODE (SUBREG_REG (op)) == DFmode
8312 || GET_MODE (SUBREG_REG (op)) == TFmode
8313 || GET_MODE (SUBREG_REG (op)) == IFmode
8314 || GET_MODE (SUBREG_REG (op)) == KFmode))
8315 return true;
8317 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
8318 reg:TI. */
8319 if (GET_CODE (op) == SUBREG
8320 && (mode == DFmode || mode == TFmode || mode == IFmode
8321 || mode == KFmode)
8322 && REG_P (SUBREG_REG (op))
8323 && (GET_MODE (SUBREG_REG (op)) == DImode
8324 || GET_MODE (SUBREG_REG (op)) == TImode
8325 || GET_MODE (SUBREG_REG (op)) == PTImode
8326 || GET_MODE (SUBREG_REG (op)) == DDmode
8327 || GET_MODE (SUBREG_REG (op)) == TDmode))
8328 return true;
8331 if (TARGET_SPE
8332 && GET_CODE (op) == SUBREG
8333 && mode == SImode
8334 && REG_P (SUBREG_REG (op))
8335 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
8336 return true;
8338 return false;
8341 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
8342 selects whether the alignment is abi mandated, optional, or
8343 both abi and optional alignment. */
8345 unsigned int
8346 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
8348 if (how != align_opt)
8350 if (TREE_CODE (type) == VECTOR_TYPE)
8352 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
8353 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
8355 if (align < 64)
8356 align = 64;
8358 else if (align < 128)
8359 align = 128;
8361 else if (TARGET_E500_DOUBLE
8362 && TREE_CODE (type) == REAL_TYPE
8363 && TYPE_MODE (type) == DFmode)
8365 if (align < 64)
8366 align = 64;
8370 if (how != align_abi)
8372 if (TREE_CODE (type) == ARRAY_TYPE
8373 && TYPE_MODE (TREE_TYPE (type)) == QImode)
8375 if (align < BITS_PER_WORD)
8376 align = BITS_PER_WORD;
8380 return align;
8383 /* Implement TARGET_SLOW_UNALIGNED_ACCESS. Altivec vector memory
8384 instructions simply ignore the low bits; SPE vector memory
8385 instructions trap on unaligned accesses; VSX memory instructions are
8386 aligned to 4 or 8 bytes. */
8388 static bool
8389 rs6000_slow_unaligned_access (machine_mode mode, unsigned int align)
8391 return (STRICT_ALIGNMENT
8392 || (!TARGET_EFFICIENT_UNALIGNED_VSX
8393 && ((SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode) && align < 32)
8394 || ((VECTOR_MODE_P (mode) || FLOAT128_VECTOR_P (mode))
8395 && (int) align < VECTOR_ALIGN (mode)))));
8398 /* Previous GCC releases forced all vector types to have 16-byte alignment. */
8400 bool
8401 rs6000_special_adjust_field_align_p (tree type, unsigned int computed)
8403 if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
8405 if (computed != 128)
8407 static bool warned;
8408 if (!warned && warn_psabi)
8410 warned = true;
8411 inform (input_location,
8412 "the layout of aggregates containing vectors with"
8413 " %d-byte alignment has changed in GCC 5",
8414 computed / BITS_PER_UNIT);
8417 /* In current GCC there is no special case. */
8418 return false;
8421 return false;
8424 /* AIX increases natural record alignment to doubleword if the first
8425 field is an FP double while the FP fields remain word aligned. */
8427 unsigned int
8428 rs6000_special_round_type_align (tree type, unsigned int computed,
8429 unsigned int specified)
8431 unsigned int align = MAX (computed, specified);
8432 tree field = TYPE_FIELDS (type);
8434 /* Skip all non field decls */
8435 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
8436 field = DECL_CHAIN (field);
8438 if (field != NULL && field != type)
8440 type = TREE_TYPE (field);
8441 while (TREE_CODE (type) == ARRAY_TYPE)
8442 type = TREE_TYPE (type);
8444 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
8445 align = MAX (align, 64);
8448 return align;
8451 /* Darwin increases record alignment to the natural alignment of
8452 the first field. */
8454 unsigned int
8455 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
8456 unsigned int specified)
8458 unsigned int align = MAX (computed, specified);
8460 if (TYPE_PACKED (type))
8461 return align;
8463 /* Find the first field, looking down into aggregates. */
8464 do {
8465 tree field = TYPE_FIELDS (type);
8466 /* Skip all non field decls */
8467 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
8468 field = DECL_CHAIN (field);
8469 if (! field)
8470 break;
8471 /* A packed field does not contribute any extra alignment. */
8472 if (DECL_PACKED (field))
8473 return align;
8474 type = TREE_TYPE (field);
8475 while (TREE_CODE (type) == ARRAY_TYPE)
8476 type = TREE_TYPE (type);
8477 } while (AGGREGATE_TYPE_P (type));
8479 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
8480 align = MAX (align, TYPE_ALIGN (type));
8482 return align;
8485 /* Return 1 for an operand in small memory on V.4/eabi. */
8488 small_data_operand (rtx op ATTRIBUTE_UNUSED,
8489 machine_mode mode ATTRIBUTE_UNUSED)
8491 #if TARGET_ELF
8492 rtx sym_ref;
8494 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
8495 return 0;
8497 if (DEFAULT_ABI != ABI_V4)
8498 return 0;
8500 /* Vector and float memory instructions have a limited offset on the
8501 SPE, so using a vector or float variable directly as an operand is
8502 not useful. */
8503 if (TARGET_SPE
8504 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
8505 return 0;
8507 if (GET_CODE (op) == SYMBOL_REF)
8508 sym_ref = op;
8510 else if (GET_CODE (op) != CONST
8511 || GET_CODE (XEXP (op, 0)) != PLUS
8512 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
8513 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
8514 return 0;
8516 else
8518 rtx sum = XEXP (op, 0);
8519 HOST_WIDE_INT summand;
8521 /* We have to be careful here, because it is the referenced address
8522 that must be 32k from _SDA_BASE_, not just the symbol. */
8523 summand = INTVAL (XEXP (sum, 1));
8524 if (summand < 0 || summand > g_switch_value)
8525 return 0;
8527 sym_ref = XEXP (sum, 0);
8530 return SYMBOL_REF_SMALL_P (sym_ref);
8531 #else
8532 return 0;
8533 #endif
8536 /* Return true if either operand is a general purpose register. */
8538 bool
8539 gpr_or_gpr_p (rtx op0, rtx op1)
8541 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
8542 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
8545 /* Return true if this is a move direct operation between GPR registers and
8546 floating point/VSX registers. */
8548 bool
8549 direct_move_p (rtx op0, rtx op1)
8551 int regno0, regno1;
8553 if (!REG_P (op0) || !REG_P (op1))
8554 return false;
8556 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
8557 return false;
8559 regno0 = REGNO (op0);
8560 regno1 = REGNO (op1);
8561 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
8562 return false;
8564 if (INT_REGNO_P (regno0))
8565 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
8567 else if (INT_REGNO_P (regno1))
8569 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
8570 return true;
8572 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
8573 return true;
8576 return false;
8579 /* Return true if the OFFSET is valid for the quad address instructions that
8580 use d-form (register + offset) addressing. */
8582 static inline bool
8583 quad_address_offset_p (HOST_WIDE_INT offset)
8585 return (IN_RANGE (offset, -32768, 32767) && ((offset) & 0xf) == 0);
8588 /* Return true if the ADDR is an acceptable address for a quad memory
8589 operation of mode MODE (either LQ/STQ for general purpose registers, or
8590 LXV/STXV for vector registers under ISA 3.0. GPR_P is true if this address
8591 is intended for LQ/STQ. If it is false, the address is intended for the ISA
8592 3.0 LXV/STXV instruction. */
8594 bool
8595 quad_address_p (rtx addr, machine_mode mode, bool strict)
8597 rtx op0, op1;
8599 if (GET_MODE_SIZE (mode) != 16)
8600 return false;
8602 if (legitimate_indirect_address_p (addr, strict))
8603 return true;
8605 if (VECTOR_MODE_P (mode) && !mode_supports_vsx_dform_quad (mode))
8606 return false;
8608 if (GET_CODE (addr) != PLUS)
8609 return false;
8611 op0 = XEXP (addr, 0);
8612 if (!REG_P (op0) || !INT_REG_OK_FOR_BASE_P (op0, strict))
8613 return false;
8615 op1 = XEXP (addr, 1);
8616 if (!CONST_INT_P (op1))
8617 return false;
8619 return quad_address_offset_p (INTVAL (op1));
8622 /* Return true if this is a load or store quad operation. This function does
8623 not handle the atomic quad memory instructions. */
8625 bool
8626 quad_load_store_p (rtx op0, rtx op1)
8628 bool ret;
8630 if (!TARGET_QUAD_MEMORY)
8631 ret = false;
8633 else if (REG_P (op0) && MEM_P (op1))
8634 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
8635 && quad_memory_operand (op1, GET_MODE (op1))
8636 && !reg_overlap_mentioned_p (op0, op1));
8638 else if (MEM_P (op0) && REG_P (op1))
8639 ret = (quad_memory_operand (op0, GET_MODE (op0))
8640 && quad_int_reg_operand (op1, GET_MODE (op1)));
8642 else
8643 ret = false;
8645 if (TARGET_DEBUG_ADDR)
8647 fprintf (stderr, "\n========== quad_load_store, return %s\n",
8648 ret ? "true" : "false");
8649 debug_rtx (gen_rtx_SET (op0, op1));
8652 return ret;
8655 /* Given an address, return a constant offset term if one exists. */
8657 static rtx
8658 address_offset (rtx op)
8660 if (GET_CODE (op) == PRE_INC
8661 || GET_CODE (op) == PRE_DEC)
8662 op = XEXP (op, 0);
8663 else if (GET_CODE (op) == PRE_MODIFY
8664 || GET_CODE (op) == LO_SUM)
8665 op = XEXP (op, 1);
8667 if (GET_CODE (op) == CONST)
8668 op = XEXP (op, 0);
8670 if (GET_CODE (op) == PLUS)
8671 op = XEXP (op, 1);
8673 if (CONST_INT_P (op))
8674 return op;
8676 return NULL_RTX;
8679 /* Return true if the MEM operand is a memory operand suitable for use
8680 with a (full width, possibly multiple) gpr load/store. On
8681 powerpc64 this means the offset must be divisible by 4.
8682 Implements 'Y' constraint.
8684 Accept direct, indexed, offset, lo_sum and tocref. Since this is
8685 a constraint function we know the operand has satisfied a suitable
8686 memory predicate. Also accept some odd rtl generated by reload
8687 (see rs6000_legitimize_reload_address for various forms). It is
8688 important that reload rtl be accepted by appropriate constraints
8689 but not by the operand predicate.
8691 Offsetting a lo_sum should not be allowed, except where we know by
8692 alignment that a 32k boundary is not crossed, but see the ???
8693 comment in rs6000_legitimize_reload_address. Note that by
8694 "offsetting" here we mean a further offset to access parts of the
8695 MEM. It's fine to have a lo_sum where the inner address is offset
8696 from a sym, since the same sym+offset will appear in the high part
8697 of the address calculation. */
8699 bool
8700 mem_operand_gpr (rtx op, machine_mode mode)
8702 unsigned HOST_WIDE_INT offset;
8703 int extra;
8704 rtx addr = XEXP (op, 0);
8706 op = address_offset (addr);
8707 if (op == NULL_RTX)
8708 return true;
8710 offset = INTVAL (op);
8711 if (TARGET_POWERPC64 && (offset & 3) != 0)
8712 return false;
8714 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
8715 if (extra < 0)
8716 extra = 0;
8718 if (GET_CODE (addr) == LO_SUM)
8719 /* For lo_sum addresses, we must allow any offset except one that
8720 causes a wrap, so test only the low 16 bits. */
8721 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
8723 return offset + 0x8000 < 0x10000u - extra;
8726 /* As above, but for DS-FORM VSX insns. Unlike mem_operand_gpr,
8727 enforce an offset divisible by 4 even for 32-bit. */
8729 bool
8730 mem_operand_ds_form (rtx op, machine_mode mode)
8732 unsigned HOST_WIDE_INT offset;
8733 int extra;
8734 rtx addr = XEXP (op, 0);
8736 if (!offsettable_address_p (false, mode, addr))
8737 return false;
8739 op = address_offset (addr);
8740 if (op == NULL_RTX)
8741 return true;
8743 offset = INTVAL (op);
8744 if ((offset & 3) != 0)
8745 return false;
8747 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
8748 if (extra < 0)
8749 extra = 0;
8751 if (GET_CODE (addr) == LO_SUM)
8752 /* For lo_sum addresses, we must allow any offset except one that
8753 causes a wrap, so test only the low 16 bits. */
8754 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
8756 return offset + 0x8000 < 0x10000u - extra;
8759 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
8761 static bool
8762 reg_offset_addressing_ok_p (machine_mode mode)
8764 switch (mode)
8766 case E_V16QImode:
8767 case E_V8HImode:
8768 case E_V4SFmode:
8769 case E_V4SImode:
8770 case E_V2DFmode:
8771 case E_V2DImode:
8772 case E_V1TImode:
8773 case E_TImode:
8774 case E_TFmode:
8775 case E_KFmode:
8776 /* AltiVec/VSX vector modes. Only reg+reg addressing was valid until the
8777 ISA 3.0 vector d-form addressing mode was added. While TImode is not
8778 a vector mode, if we want to use the VSX registers to move it around,
8779 we need to restrict ourselves to reg+reg addressing. Similarly for
8780 IEEE 128-bit floating point that is passed in a single vector
8781 register. */
8782 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
8783 return mode_supports_vsx_dform_quad (mode);
8784 break;
8786 case E_V4HImode:
8787 case E_V2SImode:
8788 case E_V1DImode:
8789 case E_V2SFmode:
8790 /* Paired vector modes. Only reg+reg addressing is valid. */
8791 if (TARGET_PAIRED_FLOAT)
8792 return false;
8793 break;
8795 case E_SDmode:
8796 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
8797 addressing for the LFIWZX and STFIWX instructions. */
8798 if (TARGET_NO_SDMODE_STACK)
8799 return false;
8800 break;
8802 default:
8803 break;
8806 return true;
8809 static bool
8810 virtual_stack_registers_memory_p (rtx op)
8812 int regnum;
8814 if (GET_CODE (op) == REG)
8815 regnum = REGNO (op);
8817 else if (GET_CODE (op) == PLUS
8818 && GET_CODE (XEXP (op, 0)) == REG
8819 && GET_CODE (XEXP (op, 1)) == CONST_INT)
8820 regnum = REGNO (XEXP (op, 0));
8822 else
8823 return false;
8825 return (regnum >= FIRST_VIRTUAL_REGISTER
8826 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
8829 /* Return true if a MODE sized memory accesses to OP plus OFFSET
8830 is known to not straddle a 32k boundary. This function is used
8831 to determine whether -mcmodel=medium code can use TOC pointer
8832 relative addressing for OP. This means the alignment of the TOC
8833 pointer must also be taken into account, and unfortunately that is
8834 only 8 bytes. */
8836 #ifndef POWERPC64_TOC_POINTER_ALIGNMENT
8837 #define POWERPC64_TOC_POINTER_ALIGNMENT 8
8838 #endif
8840 static bool
8841 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
8842 machine_mode mode)
8844 tree decl;
8845 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
8847 if (GET_CODE (op) != SYMBOL_REF)
8848 return false;
8850 /* ISA 3.0 vector d-form addressing is restricted, don't allow
8851 SYMBOL_REF. */
8852 if (mode_supports_vsx_dform_quad (mode))
8853 return false;
8855 dsize = GET_MODE_SIZE (mode);
8856 decl = SYMBOL_REF_DECL (op);
8857 if (!decl)
8859 if (dsize == 0)
8860 return false;
8862 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
8863 replacing memory addresses with an anchor plus offset. We
8864 could find the decl by rummaging around in the block->objects
8865 VEC for the given offset but that seems like too much work. */
8866 dalign = BITS_PER_UNIT;
8867 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
8868 && SYMBOL_REF_ANCHOR_P (op)
8869 && SYMBOL_REF_BLOCK (op) != NULL)
8871 struct object_block *block = SYMBOL_REF_BLOCK (op);
8873 dalign = block->alignment;
8874 offset += SYMBOL_REF_BLOCK_OFFSET (op);
8876 else if (CONSTANT_POOL_ADDRESS_P (op))
8878 /* It would be nice to have get_pool_align().. */
8879 machine_mode cmode = get_pool_mode (op);
8881 dalign = GET_MODE_ALIGNMENT (cmode);
8884 else if (DECL_P (decl))
8886 dalign = DECL_ALIGN (decl);
8888 if (dsize == 0)
8890 /* Allow BLKmode when the entire object is known to not
8891 cross a 32k boundary. */
8892 if (!DECL_SIZE_UNIT (decl))
8893 return false;
8895 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
8896 return false;
8898 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8899 if (dsize > 32768)
8900 return false;
8902 dalign /= BITS_PER_UNIT;
8903 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
8904 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
8905 return dalign >= dsize;
8908 else
8909 gcc_unreachable ();
8911 /* Find how many bits of the alignment we know for this access. */
8912 dalign /= BITS_PER_UNIT;
8913 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
8914 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
8915 mask = dalign - 1;
8916 lsb = offset & -offset;
8917 mask &= lsb - 1;
8918 dalign = mask + 1;
8920 return dalign >= dsize;
8923 static bool
8924 constant_pool_expr_p (rtx op)
8926 rtx base, offset;
8928 split_const (op, &base, &offset);
8929 return (GET_CODE (base) == SYMBOL_REF
8930 && CONSTANT_POOL_ADDRESS_P (base)
8931 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
8934 static const_rtx tocrel_base, tocrel_offset;
8936 /* Return true if OP is a toc pointer relative address (the output
8937 of create_TOC_reference). If STRICT, do not match non-split
8938 -mcmodel=large/medium toc pointer relative addresses. */
8940 bool
8941 toc_relative_expr_p (const_rtx op, bool strict)
8943 if (!TARGET_TOC)
8944 return false;
8946 if (TARGET_CMODEL != CMODEL_SMALL)
8948 /* When strict ensure we have everything tidy. */
8949 if (strict
8950 && !(GET_CODE (op) == LO_SUM
8951 && REG_P (XEXP (op, 0))
8952 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict)))
8953 return false;
8955 /* When not strict, allow non-split TOC addresses and also allow
8956 (lo_sum (high ..)) TOC addresses created during reload. */
8957 if (GET_CODE (op) == LO_SUM)
8958 op = XEXP (op, 1);
8961 tocrel_base = op;
8962 tocrel_offset = const0_rtx;
8963 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
8965 tocrel_base = XEXP (op, 0);
8966 tocrel_offset = XEXP (op, 1);
8969 return (GET_CODE (tocrel_base) == UNSPEC
8970 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
8973 /* Return true if X is a constant pool address, and also for cmodel=medium
8974 if X is a toc-relative address known to be offsettable within MODE. */
8976 bool
8977 legitimate_constant_pool_address_p (const_rtx x, machine_mode mode,
8978 bool strict)
8980 return (toc_relative_expr_p (x, strict)
8981 && (TARGET_CMODEL != CMODEL_MEDIUM
8982 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
8983 || mode == QImode
8984 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
8985 INTVAL (tocrel_offset), mode)));
8988 static bool
8989 legitimate_small_data_p (machine_mode mode, rtx x)
8991 return (DEFAULT_ABI == ABI_V4
8992 && !flag_pic && !TARGET_TOC
8993 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
8994 && small_data_operand (x, mode));
8997 /* SPE offset addressing is limited to 5-bits worth of double words. */
8998 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
9000 bool
9001 rs6000_legitimate_offset_address_p (machine_mode mode, rtx x,
9002 bool strict, bool worst_case)
9004 unsigned HOST_WIDE_INT offset;
9005 unsigned int extra;
9007 if (GET_CODE (x) != PLUS)
9008 return false;
9009 if (!REG_P (XEXP (x, 0)))
9010 return false;
9011 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
9012 return false;
9013 if (mode_supports_vsx_dform_quad (mode))
9014 return quad_address_p (x, mode, strict);
9015 if (!reg_offset_addressing_ok_p (mode))
9016 return virtual_stack_registers_memory_p (x);
9017 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
9018 return true;
9019 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
9020 return false;
9022 offset = INTVAL (XEXP (x, 1));
9023 extra = 0;
9024 switch (mode)
9026 case E_V4HImode:
9027 case E_V2SImode:
9028 case E_V1DImode:
9029 case E_V2SFmode:
9030 /* SPE vector modes. */
9031 return SPE_CONST_OFFSET_OK (offset);
9033 case E_DFmode:
9034 case E_DDmode:
9035 case E_DImode:
9036 /* On e500v2, we may have:
9038 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
9040 Which gets addressed with evldd instructions. */
9041 if (TARGET_E500_DOUBLE)
9042 return SPE_CONST_OFFSET_OK (offset);
9044 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
9045 addressing. */
9046 if (VECTOR_MEM_VSX_P (mode))
9047 return false;
9049 if (!worst_case)
9050 break;
9051 if (!TARGET_POWERPC64)
9052 extra = 4;
9053 else if (offset & 3)
9054 return false;
9055 break;
9057 case E_TFmode:
9058 case E_IFmode:
9059 case E_KFmode:
9060 case E_TDmode:
9061 case E_TImode:
9062 case E_PTImode:
9063 if (TARGET_E500_DOUBLE)
9064 return (SPE_CONST_OFFSET_OK (offset)
9065 && SPE_CONST_OFFSET_OK (offset + 8));
9067 extra = 8;
9068 if (!worst_case)
9069 break;
9070 if (!TARGET_POWERPC64)
9071 extra = 12;
9072 else if (offset & 3)
9073 return false;
9074 break;
9076 default:
9077 break;
9080 offset += 0x8000;
9081 return offset < 0x10000 - extra;
9084 bool
9085 legitimate_indexed_address_p (rtx x, int strict)
9087 rtx op0, op1;
9089 if (GET_CODE (x) != PLUS)
9090 return false;
9092 op0 = XEXP (x, 0);
9093 op1 = XEXP (x, 1);
9095 /* Recognize the rtl generated by reload which we know will later be
9096 replaced with proper base and index regs. */
9097 if (!strict
9098 && reload_in_progress
9099 && (REG_P (op0) || GET_CODE (op0) == PLUS)
9100 && REG_P (op1))
9101 return true;
9103 return (REG_P (op0) && REG_P (op1)
9104 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
9105 && INT_REG_OK_FOR_INDEX_P (op1, strict))
9106 || (INT_REG_OK_FOR_BASE_P (op1, strict)
9107 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
9110 bool
9111 avoiding_indexed_address_p (machine_mode mode)
9113 /* Avoid indexed addressing for modes that have non-indexed
9114 load/store instruction forms. */
9115 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
9118 bool
9119 legitimate_indirect_address_p (rtx x, int strict)
9121 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
9124 bool
9125 macho_lo_sum_memory_operand (rtx x, machine_mode mode)
9127 if (!TARGET_MACHO || !flag_pic
9128 || mode != SImode || GET_CODE (x) != MEM)
9129 return false;
9130 x = XEXP (x, 0);
9132 if (GET_CODE (x) != LO_SUM)
9133 return false;
9134 if (GET_CODE (XEXP (x, 0)) != REG)
9135 return false;
9136 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
9137 return false;
9138 x = XEXP (x, 1);
9140 return CONSTANT_P (x);
9143 static bool
9144 legitimate_lo_sum_address_p (machine_mode mode, rtx x, int strict)
9146 if (GET_CODE (x) != LO_SUM)
9147 return false;
9148 if (GET_CODE (XEXP (x, 0)) != REG)
9149 return false;
9150 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
9151 return false;
9152 /* quad word addresses are restricted, and we can't use LO_SUM. */
9153 if (mode_supports_vsx_dform_quad (mode))
9154 return false;
9155 /* Restrict addressing for DI because of our SUBREG hackery. */
9156 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
9157 return false;
9158 x = XEXP (x, 1);
9160 if (TARGET_ELF || TARGET_MACHO)
9162 bool large_toc_ok;
9164 if (DEFAULT_ABI == ABI_V4 && flag_pic)
9165 return false;
9166 /* LRA doesn't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
9167 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
9168 recognizes some LO_SUM addresses as valid although this
9169 function says opposite. In most cases, LRA through different
9170 transformations can generate correct code for address reloads.
9171 It can not manage only some LO_SUM cases. So we need to add
9172 code analogous to one in rs6000_legitimize_reload_address for
9173 LOW_SUM here saying that some addresses are still valid. */
9174 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
9175 && small_toc_ref (x, VOIDmode));
9176 if (TARGET_TOC && ! large_toc_ok)
9177 return false;
9178 if (GET_MODE_NUNITS (mode) != 1)
9179 return false;
9180 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9181 && !(/* ??? Assume floating point reg based on mode? */
9182 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
9183 && (mode == DFmode || mode == DDmode)))
9184 return false;
9186 return CONSTANT_P (x) || large_toc_ok;
9189 return false;
9193 /* Try machine-dependent ways of modifying an illegitimate address
9194 to be legitimate. If we find one, return the new, valid address.
9195 This is used from only one place: `memory_address' in explow.c.
9197 OLDX is the address as it was before break_out_memory_refs was
9198 called. In some cases it is useful to look at this to decide what
9199 needs to be done.
9201 It is always safe for this function to do nothing. It exists to
9202 recognize opportunities to optimize the output.
9204 On RS/6000, first check for the sum of a register with a constant
9205 integer that is out of range. If so, generate code to add the
9206 constant with the low-order 16 bits masked to the register and force
9207 this result into another register (this can be done with `cau').
9208 Then generate an address of REG+(CONST&0xffff), allowing for the
9209 possibility of bit 16 being a one.
9211 Then check for the sum of a register and something not constant, try to
9212 load the other things into a register and return the sum. */
9214 static rtx
9215 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
9216 machine_mode mode)
9218 unsigned int extra;
9220 if (!reg_offset_addressing_ok_p (mode)
9221 || mode_supports_vsx_dform_quad (mode))
9223 if (virtual_stack_registers_memory_p (x))
9224 return x;
9226 /* In theory we should not be seeing addresses of the form reg+0,
9227 but just in case it is generated, optimize it away. */
9228 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
9229 return force_reg (Pmode, XEXP (x, 0));
9231 /* For TImode with load/store quad, restrict addresses to just a single
9232 pointer, so it works with both GPRs and VSX registers. */
9233 /* Make sure both operands are registers. */
9234 else if (GET_CODE (x) == PLUS
9235 && (mode != TImode || !TARGET_VSX_TIMODE))
9236 return gen_rtx_PLUS (Pmode,
9237 force_reg (Pmode, XEXP (x, 0)),
9238 force_reg (Pmode, XEXP (x, 1)));
9239 else
9240 return force_reg (Pmode, x);
9242 if (GET_CODE (x) == SYMBOL_REF)
9244 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
9245 if (model != 0)
9246 return rs6000_legitimize_tls_address (x, model);
9249 extra = 0;
9250 switch (mode)
9252 case E_TFmode:
9253 case E_TDmode:
9254 case E_TImode:
9255 case E_PTImode:
9256 case E_IFmode:
9257 case E_KFmode:
9258 /* As in legitimate_offset_address_p we do not assume
9259 worst-case. The mode here is just a hint as to the registers
9260 used. A TImode is usually in gprs, but may actually be in
9261 fprs. Leave worst-case scenario for reload to handle via
9262 insn constraints. PTImode is only GPRs. */
9263 extra = 8;
9264 break;
9265 default:
9266 break;
9269 if (GET_CODE (x) == PLUS
9270 && GET_CODE (XEXP (x, 0)) == REG
9271 && GET_CODE (XEXP (x, 1)) == CONST_INT
9272 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
9273 >= 0x10000 - extra)
9274 && !(SPE_VECTOR_MODE (mode)
9275 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
9277 HOST_WIDE_INT high_int, low_int;
9278 rtx sum;
9279 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
9280 if (low_int >= 0x8000 - extra)
9281 low_int = 0;
9282 high_int = INTVAL (XEXP (x, 1)) - low_int;
9283 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
9284 GEN_INT (high_int)), 0);
9285 return plus_constant (Pmode, sum, low_int);
9287 else if (GET_CODE (x) == PLUS
9288 && GET_CODE (XEXP (x, 0)) == REG
9289 && GET_CODE (XEXP (x, 1)) != CONST_INT
9290 && GET_MODE_NUNITS (mode) == 1
9291 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
9292 || (/* ??? Assume floating point reg based on mode? */
9293 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
9294 && (mode == DFmode || mode == DDmode)))
9295 && !avoiding_indexed_address_p (mode))
9297 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
9298 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
9300 else if (SPE_VECTOR_MODE (mode)
9301 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
9303 if (mode == DImode)
9304 return x;
9305 /* We accept [reg + reg] and [reg + OFFSET]. */
9307 if (GET_CODE (x) == PLUS)
9309 rtx op1 = XEXP (x, 0);
9310 rtx op2 = XEXP (x, 1);
9311 rtx y;
9313 op1 = force_reg (Pmode, op1);
9315 if (GET_CODE (op2) != REG
9316 && (GET_CODE (op2) != CONST_INT
9317 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
9318 || (GET_MODE_SIZE (mode) > 8
9319 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
9320 op2 = force_reg (Pmode, op2);
9322 /* We can't always do [reg + reg] for these, because [reg +
9323 reg + offset] is not a legitimate addressing mode. */
9324 y = gen_rtx_PLUS (Pmode, op1, op2);
9326 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
9327 return force_reg (Pmode, y);
9328 else
9329 return y;
9332 return force_reg (Pmode, x);
9334 else if ((TARGET_ELF
9335 #if TARGET_MACHO
9336 || !MACHO_DYNAMIC_NO_PIC_P
9337 #endif
9339 && TARGET_32BIT
9340 && TARGET_NO_TOC
9341 && ! flag_pic
9342 && GET_CODE (x) != CONST_INT
9343 && GET_CODE (x) != CONST_WIDE_INT
9344 && GET_CODE (x) != CONST_DOUBLE
9345 && CONSTANT_P (x)
9346 && GET_MODE_NUNITS (mode) == 1
9347 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
9348 || (/* ??? Assume floating point reg based on mode? */
9349 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
9350 && (mode == DFmode || mode == DDmode))))
9352 rtx reg = gen_reg_rtx (Pmode);
9353 if (TARGET_ELF)
9354 emit_insn (gen_elf_high (reg, x));
9355 else
9356 emit_insn (gen_macho_high (reg, x));
9357 return gen_rtx_LO_SUM (Pmode, reg, x);
9359 else if (TARGET_TOC
9360 && GET_CODE (x) == SYMBOL_REF
9361 && constant_pool_expr_p (x)
9362 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
9363 return create_TOC_reference (x, NULL_RTX);
9364 else
9365 return x;
9368 /* Debug version of rs6000_legitimize_address. */
9369 static rtx
9370 rs6000_debug_legitimize_address (rtx x, rtx oldx, machine_mode mode)
9372 rtx ret;
9373 rtx_insn *insns;
9375 start_sequence ();
9376 ret = rs6000_legitimize_address (x, oldx, mode);
9377 insns = get_insns ();
9378 end_sequence ();
9380 if (ret != x)
9382 fprintf (stderr,
9383 "\nrs6000_legitimize_address: mode %s, old code %s, "
9384 "new code %s, modified\n",
9385 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
9386 GET_RTX_NAME (GET_CODE (ret)));
9388 fprintf (stderr, "Original address:\n");
9389 debug_rtx (x);
9391 fprintf (stderr, "oldx:\n");
9392 debug_rtx (oldx);
9394 fprintf (stderr, "New address:\n");
9395 debug_rtx (ret);
9397 if (insns)
9399 fprintf (stderr, "Insns added:\n");
9400 debug_rtx_list (insns, 20);
9403 else
9405 fprintf (stderr,
9406 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
9407 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
9409 debug_rtx (x);
9412 if (insns)
9413 emit_insn (insns);
9415 return ret;
9418 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
9419 We need to emit DTP-relative relocations. */
9421 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
9422 static void
9423 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
9425 switch (size)
9427 case 4:
9428 fputs ("\t.long\t", file);
9429 break;
9430 case 8:
9431 fputs (DOUBLE_INT_ASM_OP, file);
9432 break;
9433 default:
9434 gcc_unreachable ();
9436 output_addr_const (file, x);
9437 if (TARGET_ELF)
9438 fputs ("@dtprel+0x8000", file);
9439 else if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF)
9441 switch (SYMBOL_REF_TLS_MODEL (x))
9443 case 0:
9444 break;
9445 case TLS_MODEL_LOCAL_EXEC:
9446 fputs ("@le", file);
9447 break;
9448 case TLS_MODEL_INITIAL_EXEC:
9449 fputs ("@ie", file);
9450 break;
9451 case TLS_MODEL_GLOBAL_DYNAMIC:
9452 case TLS_MODEL_LOCAL_DYNAMIC:
9453 fputs ("@m", file);
9454 break;
9455 default:
9456 gcc_unreachable ();
9461 /* Return true if X is a symbol that refers to real (rather than emulated)
9462 TLS. */
9464 static bool
9465 rs6000_real_tls_symbol_ref_p (rtx x)
9467 return (GET_CODE (x) == SYMBOL_REF
9468 && SYMBOL_REF_TLS_MODEL (x) >= TLS_MODEL_REAL);
9471 /* In the name of slightly smaller debug output, and to cater to
9472 general assembler lossage, recognize various UNSPEC sequences
9473 and turn them back into a direct symbol reference. */
9475 static rtx
9476 rs6000_delegitimize_address (rtx orig_x)
9478 rtx x, y, offset;
9480 orig_x = delegitimize_mem_from_attrs (orig_x);
9481 x = orig_x;
9482 if (MEM_P (x))
9483 x = XEXP (x, 0);
9485 y = x;
9486 if (TARGET_CMODEL != CMODEL_SMALL
9487 && GET_CODE (y) == LO_SUM)
9488 y = XEXP (y, 1);
9490 offset = NULL_RTX;
9491 if (GET_CODE (y) == PLUS
9492 && GET_MODE (y) == Pmode
9493 && CONST_INT_P (XEXP (y, 1)))
9495 offset = XEXP (y, 1);
9496 y = XEXP (y, 0);
9499 if (GET_CODE (y) == UNSPEC
9500 && XINT (y, 1) == UNSPEC_TOCREL)
9502 y = XVECEXP (y, 0, 0);
9504 #ifdef HAVE_AS_TLS
9505 /* Do not associate thread-local symbols with the original
9506 constant pool symbol. */
9507 if (TARGET_XCOFF
9508 && GET_CODE (y) == SYMBOL_REF
9509 && CONSTANT_POOL_ADDRESS_P (y)
9510 && rs6000_real_tls_symbol_ref_p (get_pool_constant (y)))
9511 return orig_x;
9512 #endif
9514 if (offset != NULL_RTX)
9515 y = gen_rtx_PLUS (Pmode, y, offset);
9516 if (!MEM_P (orig_x))
9517 return y;
9518 else
9519 return replace_equiv_address_nv (orig_x, y);
9522 if (TARGET_MACHO
9523 && GET_CODE (orig_x) == LO_SUM
9524 && GET_CODE (XEXP (orig_x, 1)) == CONST)
9526 y = XEXP (XEXP (orig_x, 1), 0);
9527 if (GET_CODE (y) == UNSPEC
9528 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
9529 return XVECEXP (y, 0, 0);
9532 return orig_x;
9535 /* Return true if X shouldn't be emitted into the debug info.
9536 The linker doesn't like .toc section references from
9537 .debug_* sections, so reject .toc section symbols. */
9539 static bool
9540 rs6000_const_not_ok_for_debug_p (rtx x)
9542 if (GET_CODE (x) == UNSPEC)
9543 return true;
9544 if (GET_CODE (x) == SYMBOL_REF
9545 && CONSTANT_POOL_ADDRESS_P (x))
9547 rtx c = get_pool_constant (x);
9548 machine_mode cmode = get_pool_mode (x);
9549 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
9550 return true;
9553 return false;
9557 /* Implement the TARGET_LEGITIMATE_COMBINED_INSN hook. */
9559 static bool
9560 rs6000_legitimate_combined_insn (rtx_insn *insn)
9562 int icode = INSN_CODE (insn);
9564 /* Reject creating doloop insns. Combine should not be allowed
9565 to create these for a number of reasons:
9566 1) In a nested loop, if combine creates one of these in an
9567 outer loop and the register allocator happens to allocate ctr
9568 to the outer loop insn, then the inner loop can't use ctr.
9569 Inner loops ought to be more highly optimized.
9570 2) Combine often wants to create one of these from what was
9571 originally a three insn sequence, first combining the three
9572 insns to two, then to ctrsi/ctrdi. When ctrsi/ctrdi is not
9573 allocated ctr, the splitter takes use back to the three insn
9574 sequence. It's better to stop combine at the two insn
9575 sequence.
9576 3) Faced with not being able to allocate ctr for ctrsi/crtdi
9577 insns, the register allocator sometimes uses floating point
9578 or vector registers for the pseudo. Since ctrsi/ctrdi is a
9579 jump insn and output reloads are not implemented for jumps,
9580 the ctrsi/ctrdi splitters need to handle all possible cases.
9581 That's a pain, and it gets to be seriously difficult when a
9582 splitter that runs after reload needs memory to transfer from
9583 a gpr to fpr. See PR70098 and PR71763 which are not fixed
9584 for the difficult case. It's better to not create problems
9585 in the first place. */
9586 if (icode != CODE_FOR_nothing
9587 && (icode == CODE_FOR_ctrsi_internal1
9588 || icode == CODE_FOR_ctrdi_internal1
9589 || icode == CODE_FOR_ctrsi_internal2
9590 || icode == CODE_FOR_ctrdi_internal2
9591 || icode == CODE_FOR_ctrsi_internal3
9592 || icode == CODE_FOR_ctrdi_internal3
9593 || icode == CODE_FOR_ctrsi_internal4
9594 || icode == CODE_FOR_ctrdi_internal4))
9595 return false;
9597 return true;
9600 /* Construct the SYMBOL_REF for the tls_get_addr function. */
9602 static GTY(()) rtx rs6000_tls_symbol;
9603 static rtx
9604 rs6000_tls_get_addr (void)
9606 if (!rs6000_tls_symbol)
9607 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
9609 return rs6000_tls_symbol;
9612 /* Construct the SYMBOL_REF for TLS GOT references. */
9614 static GTY(()) rtx rs6000_got_symbol;
9615 static rtx
9616 rs6000_got_sym (void)
9618 if (!rs6000_got_symbol)
9620 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
9621 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
9622 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
9625 return rs6000_got_symbol;
9628 /* AIX Thread-Local Address support. */
9630 static rtx
9631 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
9633 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
9634 const char *name;
9635 char *tlsname;
9637 name = XSTR (addr, 0);
9638 /* Append TLS CSECT qualifier, unless the symbol already is qualified
9639 or the symbol will be in TLS private data section. */
9640 if (name[strlen (name) - 1] != ']'
9641 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
9642 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
9644 tlsname = XALLOCAVEC (char, strlen (name) + 4);
9645 strcpy (tlsname, name);
9646 strcat (tlsname,
9647 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
9648 tlsaddr = copy_rtx (addr);
9649 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
9651 else
9652 tlsaddr = addr;
9654 /* Place addr into TOC constant pool. */
9655 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
9657 /* Output the TOC entry and create the MEM referencing the value. */
9658 if (constant_pool_expr_p (XEXP (sym, 0))
9659 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
9661 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
9662 mem = gen_const_mem (Pmode, tocref);
9663 set_mem_alias_set (mem, get_TOC_alias_set ());
9665 else
9666 return sym;
9668 /* Use global-dynamic for local-dynamic. */
9669 if (model == TLS_MODEL_GLOBAL_DYNAMIC
9670 || model == TLS_MODEL_LOCAL_DYNAMIC)
9672 /* Create new TOC reference for @m symbol. */
9673 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
9674 tlsname = XALLOCAVEC (char, strlen (name) + 1);
9675 strcpy (tlsname, "*LCM");
9676 strcat (tlsname, name + 3);
9677 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
9678 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
9679 tocref = create_TOC_reference (modaddr, NULL_RTX);
9680 rtx modmem = gen_const_mem (Pmode, tocref);
9681 set_mem_alias_set (modmem, get_TOC_alias_set ());
9683 rtx modreg = gen_reg_rtx (Pmode);
9684 emit_insn (gen_rtx_SET (modreg, modmem));
9686 tmpreg = gen_reg_rtx (Pmode);
9687 emit_insn (gen_rtx_SET (tmpreg, mem));
9689 dest = gen_reg_rtx (Pmode);
9690 if (TARGET_32BIT)
9691 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
9692 else
9693 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
9694 return dest;
9696 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
9697 else if (TARGET_32BIT)
9699 tlsreg = gen_reg_rtx (SImode);
9700 emit_insn (gen_tls_get_tpointer (tlsreg));
9702 else
9703 tlsreg = gen_rtx_REG (DImode, 13);
9705 /* Load the TOC value into temporary register. */
9706 tmpreg = gen_reg_rtx (Pmode);
9707 emit_insn (gen_rtx_SET (tmpreg, mem));
9708 set_unique_reg_note (get_last_insn (), REG_EQUAL,
9709 gen_rtx_MINUS (Pmode, addr, tlsreg));
9711 /* Add TOC symbol value to TLS pointer. */
9712 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
9714 return dest;
9717 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
9718 this (thread-local) address. */
9720 static rtx
9721 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
9723 rtx dest, insn;
9725 if (TARGET_XCOFF)
9726 return rs6000_legitimize_tls_address_aix (addr, model);
9728 dest = gen_reg_rtx (Pmode);
9729 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
9731 rtx tlsreg;
9733 if (TARGET_64BIT)
9735 tlsreg = gen_rtx_REG (Pmode, 13);
9736 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
9738 else
9740 tlsreg = gen_rtx_REG (Pmode, 2);
9741 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
9743 emit_insn (insn);
9745 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
9747 rtx tlsreg, tmp;
9749 tmp = gen_reg_rtx (Pmode);
9750 if (TARGET_64BIT)
9752 tlsreg = gen_rtx_REG (Pmode, 13);
9753 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
9755 else
9757 tlsreg = gen_rtx_REG (Pmode, 2);
9758 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
9760 emit_insn (insn);
9761 if (TARGET_64BIT)
9762 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
9763 else
9764 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
9765 emit_insn (insn);
9767 else
9769 rtx r3, got, tga, tmp1, tmp2, call_insn;
9771 /* We currently use relocations like @got@tlsgd for tls, which
9772 means the linker will handle allocation of tls entries, placing
9773 them in the .got section. So use a pointer to the .got section,
9774 not one to secondary TOC sections used by 64-bit -mminimal-toc,
9775 or to secondary GOT sections used by 32-bit -fPIC. */
9776 if (TARGET_64BIT)
9777 got = gen_rtx_REG (Pmode, 2);
9778 else
9780 if (flag_pic == 1)
9781 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
9782 else
9784 rtx gsym = rs6000_got_sym ();
9785 got = gen_reg_rtx (Pmode);
9786 if (flag_pic == 0)
9787 rs6000_emit_move (got, gsym, Pmode);
9788 else
9790 rtx mem, lab;
9792 tmp1 = gen_reg_rtx (Pmode);
9793 tmp2 = gen_reg_rtx (Pmode);
9794 mem = gen_const_mem (Pmode, tmp1);
9795 lab = gen_label_rtx ();
9796 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
9797 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
9798 if (TARGET_LINK_STACK)
9799 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
9800 emit_move_insn (tmp2, mem);
9801 rtx_insn *last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
9802 set_unique_reg_note (last, REG_EQUAL, gsym);
9807 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
9809 tga = rs6000_tls_get_addr ();
9810 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
9811 const0_rtx, Pmode);
9813 r3 = gen_rtx_REG (Pmode, 3);
9814 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9816 if (TARGET_64BIT)
9817 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
9818 else
9819 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
9821 else if (DEFAULT_ABI == ABI_V4)
9822 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
9823 else
9824 gcc_unreachable ();
9825 call_insn = last_call_insn ();
9826 PATTERN (call_insn) = insn;
9827 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
9828 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
9829 pic_offset_table_rtx);
9831 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
9833 tga = rs6000_tls_get_addr ();
9834 tmp1 = gen_reg_rtx (Pmode);
9835 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
9836 const0_rtx, Pmode);
9838 r3 = gen_rtx_REG (Pmode, 3);
9839 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9841 if (TARGET_64BIT)
9842 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
9843 else
9844 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
9846 else if (DEFAULT_ABI == ABI_V4)
9847 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
9848 else
9849 gcc_unreachable ();
9850 call_insn = last_call_insn ();
9851 PATTERN (call_insn) = insn;
9852 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
9853 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
9854 pic_offset_table_rtx);
9856 if (rs6000_tls_size == 16)
9858 if (TARGET_64BIT)
9859 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
9860 else
9861 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
9863 else if (rs6000_tls_size == 32)
9865 tmp2 = gen_reg_rtx (Pmode);
9866 if (TARGET_64BIT)
9867 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
9868 else
9869 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
9870 emit_insn (insn);
9871 if (TARGET_64BIT)
9872 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
9873 else
9874 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
9876 else
9878 tmp2 = gen_reg_rtx (Pmode);
9879 if (TARGET_64BIT)
9880 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
9881 else
9882 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
9883 emit_insn (insn);
9884 insn = gen_rtx_SET (dest, gen_rtx_PLUS (Pmode, tmp2, tmp1));
9886 emit_insn (insn);
9888 else
9890 /* IE, or 64-bit offset LE. */
9891 tmp2 = gen_reg_rtx (Pmode);
9892 if (TARGET_64BIT)
9893 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
9894 else
9895 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
9896 emit_insn (insn);
9897 if (TARGET_64BIT)
9898 insn = gen_tls_tls_64 (dest, tmp2, addr);
9899 else
9900 insn = gen_tls_tls_32 (dest, tmp2, addr);
9901 emit_insn (insn);
9905 return dest;
9908 /* Only create the global variable for the stack protect guard if we are using
9909 the global flavor of that guard. */
9910 static tree
9911 rs6000_init_stack_protect_guard (void)
9913 if (rs6000_stack_protector_guard == SSP_GLOBAL)
9914 return default_stack_protect_guard ();
9916 return NULL_TREE;
9919 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
9921 static bool
9922 rs6000_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
9924 if (GET_CODE (x) == HIGH
9925 && GET_CODE (XEXP (x, 0)) == UNSPEC)
9926 return true;
9928 /* A TLS symbol in the TOC cannot contain a sum. */
9929 if (GET_CODE (x) == CONST
9930 && GET_CODE (XEXP (x, 0)) == PLUS
9931 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
9932 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
9933 return true;
9935 /* Do not place an ELF TLS symbol in the constant pool. */
9936 return TARGET_ELF && tls_referenced_p (x);
9939 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
9940 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
9941 can be addressed relative to the toc pointer. */
9943 static bool
9944 use_toc_relative_ref (rtx sym, machine_mode mode)
9946 return ((constant_pool_expr_p (sym)
9947 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
9948 get_pool_mode (sym)))
9949 || (TARGET_CMODEL == CMODEL_MEDIUM
9950 && SYMBOL_REF_LOCAL_P (sym)
9951 && GET_MODE_SIZE (mode) <= POWERPC64_TOC_POINTER_ALIGNMENT));
9954 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
9955 replace the input X, or the original X if no replacement is called for.
9956 The output parameter *WIN is 1 if the calling macro should goto WIN,
9957 0 if it should not.
9959 For RS/6000, we wish to handle large displacements off a base
9960 register by splitting the addend across an addiu/addis and the mem insn.
9961 This cuts number of extra insns needed from 3 to 1.
9963 On Darwin, we use this to generate code for floating point constants.
9964 A movsf_low is generated so we wind up with 2 instructions rather than 3.
9965 The Darwin code is inside #if TARGET_MACHO because only then are the
9966 machopic_* functions defined. */
9967 static rtx
9968 rs6000_legitimize_reload_address (rtx x, machine_mode mode,
9969 int opnum, int type,
9970 int ind_levels ATTRIBUTE_UNUSED, int *win)
9972 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
9973 bool quad_offset_p = mode_supports_vsx_dform_quad (mode);
9975 /* Nasty hack for vsx_splat_v2df/v2di load from mem, which takes a
9976 DFmode/DImode MEM. Ditto for ISA 3.0 vsx_splat_v4sf/v4si. */
9977 if (reg_offset_p
9978 && opnum == 1
9979 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
9980 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)
9981 || (mode == SFmode && recog_data.operand_mode[0] == V4SFmode
9982 && TARGET_P9_VECTOR)
9983 || (mode == SImode && recog_data.operand_mode[0] == V4SImode
9984 && TARGET_P9_VECTOR)))
9985 reg_offset_p = false;
9987 /* We must recognize output that we have already generated ourselves. */
9988 if (GET_CODE (x) == PLUS
9989 && GET_CODE (XEXP (x, 0)) == PLUS
9990 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
9991 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
9992 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9994 if (TARGET_DEBUG_ADDR)
9996 fprintf (stderr, "\nlegitimize_reload_address push_reload #1:\n");
9997 debug_rtx (x);
9999 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10000 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
10001 opnum, (enum reload_type) type);
10002 *win = 1;
10003 return x;
10006 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
10007 if (GET_CODE (x) == LO_SUM
10008 && GET_CODE (XEXP (x, 0)) == HIGH)
10010 if (TARGET_DEBUG_ADDR)
10012 fprintf (stderr, "\nlegitimize_reload_address push_reload #2:\n");
10013 debug_rtx (x);
10015 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10016 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10017 opnum, (enum reload_type) type);
10018 *win = 1;
10019 return x;
10022 #if TARGET_MACHO
10023 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
10024 && GET_CODE (x) == LO_SUM
10025 && GET_CODE (XEXP (x, 0)) == PLUS
10026 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
10027 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
10028 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
10029 && machopic_operand_p (XEXP (x, 1)))
10031 /* Result of previous invocation of this function on Darwin
10032 floating point constant. */
10033 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10034 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10035 opnum, (enum reload_type) type);
10036 *win = 1;
10037 return x;
10039 #endif
10041 if (TARGET_CMODEL != CMODEL_SMALL
10042 && reg_offset_p
10043 && !quad_offset_p
10044 && small_toc_ref (x, VOIDmode))
10046 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
10047 x = gen_rtx_LO_SUM (Pmode, hi, x);
10048 if (TARGET_DEBUG_ADDR)
10050 fprintf (stderr, "\nlegitimize_reload_address push_reload #3:\n");
10051 debug_rtx (x);
10053 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10054 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10055 opnum, (enum reload_type) type);
10056 *win = 1;
10057 return x;
10060 if (GET_CODE (x) == PLUS
10061 && REG_P (XEXP (x, 0))
10062 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
10063 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
10064 && CONST_INT_P (XEXP (x, 1))
10065 && reg_offset_p
10066 && !SPE_VECTOR_MODE (mode)
10067 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
10068 && (quad_offset_p || !VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
10070 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
10071 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
10072 HOST_WIDE_INT high
10073 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
10075 /* Check for 32-bit overflow or quad addresses with one of the
10076 four least significant bits set. */
10077 if (high + low != val
10078 || (quad_offset_p && (low & 0xf)))
10080 *win = 0;
10081 return x;
10084 /* Reload the high part into a base reg; leave the low part
10085 in the mem directly. */
10087 x = gen_rtx_PLUS (GET_MODE (x),
10088 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
10089 GEN_INT (high)),
10090 GEN_INT (low));
10092 if (TARGET_DEBUG_ADDR)
10094 fprintf (stderr, "\nlegitimize_reload_address push_reload #4:\n");
10095 debug_rtx (x);
10097 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10098 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
10099 opnum, (enum reload_type) type);
10100 *win = 1;
10101 return x;
10104 if (GET_CODE (x) == SYMBOL_REF
10105 && reg_offset_p
10106 && !quad_offset_p
10107 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
10108 && !SPE_VECTOR_MODE (mode)
10109 #if TARGET_MACHO
10110 && DEFAULT_ABI == ABI_DARWIN
10111 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
10112 && machopic_symbol_defined_p (x)
10113 #else
10114 && DEFAULT_ABI == ABI_V4
10115 && !flag_pic
10116 #endif
10117 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
10118 The same goes for DImode without 64-bit gprs and DFmode and DDmode
10119 without fprs.
10120 ??? Assume floating point reg based on mode? This assumption is
10121 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
10122 where reload ends up doing a DFmode load of a constant from
10123 mem using two gprs. Unfortunately, at this point reload
10124 hasn't yet selected regs so poking around in reload data
10125 won't help and even if we could figure out the regs reliably,
10126 we'd still want to allow this transformation when the mem is
10127 naturally aligned. Since we say the address is good here, we
10128 can't disable offsets from LO_SUMs in mem_operand_gpr.
10129 FIXME: Allow offset from lo_sum for other modes too, when
10130 mem is sufficiently aligned.
10132 Also disallow this if the type can go in VMX/Altivec registers, since
10133 those registers do not have d-form (reg+offset) address modes. */
10134 && !reg_addr[mode].scalar_in_vmx_p
10135 && mode != TFmode
10136 && mode != TDmode
10137 && mode != IFmode
10138 && mode != KFmode
10139 && (mode != TImode || !TARGET_VSX_TIMODE)
10140 && mode != PTImode
10141 && (mode != DImode || TARGET_POWERPC64)
10142 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
10143 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
10145 #if TARGET_MACHO
10146 if (flag_pic)
10148 rtx offset = machopic_gen_offset (x);
10149 x = gen_rtx_LO_SUM (GET_MODE (x),
10150 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
10151 gen_rtx_HIGH (Pmode, offset)), offset);
10153 else
10154 #endif
10155 x = gen_rtx_LO_SUM (GET_MODE (x),
10156 gen_rtx_HIGH (Pmode, x), x);
10158 if (TARGET_DEBUG_ADDR)
10160 fprintf (stderr, "\nlegitimize_reload_address push_reload #5:\n");
10161 debug_rtx (x);
10163 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10164 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10165 opnum, (enum reload_type) type);
10166 *win = 1;
10167 return x;
10170 /* Reload an offset address wrapped by an AND that represents the
10171 masking of the lower bits. Strip the outer AND and let reload
10172 convert the offset address into an indirect address. For VSX,
10173 force reload to create the address with an AND in a separate
10174 register, because we can't guarantee an altivec register will
10175 be used. */
10176 if (VECTOR_MEM_ALTIVEC_P (mode)
10177 && GET_CODE (x) == AND
10178 && GET_CODE (XEXP (x, 0)) == PLUS
10179 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
10180 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
10181 && GET_CODE (XEXP (x, 1)) == CONST_INT
10182 && INTVAL (XEXP (x, 1)) == -16)
10184 x = XEXP (x, 0);
10185 *win = 1;
10186 return x;
10189 if (TARGET_TOC
10190 && reg_offset_p
10191 && !quad_offset_p
10192 && GET_CODE (x) == SYMBOL_REF
10193 && use_toc_relative_ref (x, mode))
10195 x = create_TOC_reference (x, NULL_RTX);
10196 if (TARGET_CMODEL != CMODEL_SMALL)
10198 if (TARGET_DEBUG_ADDR)
10200 fprintf (stderr, "\nlegitimize_reload_address push_reload #6:\n");
10201 debug_rtx (x);
10203 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10204 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10205 opnum, (enum reload_type) type);
10207 *win = 1;
10208 return x;
10210 *win = 0;
10211 return x;
10214 /* Debug version of rs6000_legitimize_reload_address. */
10215 static rtx
10216 rs6000_debug_legitimize_reload_address (rtx x, machine_mode mode,
10217 int opnum, int type,
10218 int ind_levels, int *win)
10220 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
10221 ind_levels, win);
10222 fprintf (stderr,
10223 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
10224 "type = %d, ind_levels = %d, win = %d, original addr:\n",
10225 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
10226 debug_rtx (x);
10228 if (x == ret)
10229 fprintf (stderr, "Same address returned\n");
10230 else if (!ret)
10231 fprintf (stderr, "NULL returned\n");
10232 else
10234 fprintf (stderr, "New address:\n");
10235 debug_rtx (ret);
10238 return ret;
10241 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
10242 that is a valid memory address for an instruction.
10243 The MODE argument is the machine mode for the MEM expression
10244 that wants to use this address.
10246 On the RS/6000, there are four valid address: a SYMBOL_REF that
10247 refers to a constant pool entry of an address (or the sum of it
10248 plus a constant), a short (16-bit signed) constant plus a register,
10249 the sum of two registers, or a register indirect, possibly with an
10250 auto-increment. For DFmode, DDmode and DImode with a constant plus
10251 register, we must ensure that both words are addressable or PowerPC64
10252 with offset word aligned.
10254 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
10255 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
10256 because adjacent memory cells are accessed by adding word-sized offsets
10257 during assembly output. */
10258 static bool
10259 rs6000_legitimate_address_p (machine_mode mode, rtx x, bool reg_ok_strict)
10261 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
10262 bool quad_offset_p = mode_supports_vsx_dform_quad (mode);
10264 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
10265 if (VECTOR_MEM_ALTIVEC_P (mode)
10266 && GET_CODE (x) == AND
10267 && GET_CODE (XEXP (x, 1)) == CONST_INT
10268 && INTVAL (XEXP (x, 1)) == -16)
10269 x = XEXP (x, 0);
10271 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
10272 return 0;
10273 if (legitimate_indirect_address_p (x, reg_ok_strict))
10274 return 1;
10275 if (TARGET_UPDATE
10276 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
10277 && mode_supports_pre_incdec_p (mode)
10278 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
10279 return 1;
10280 /* Handle restricted vector d-form offsets in ISA 3.0. */
10281 if (quad_offset_p)
10283 if (quad_address_p (x, mode, reg_ok_strict))
10284 return 1;
10286 else if (virtual_stack_registers_memory_p (x))
10287 return 1;
10289 else if (reg_offset_p)
10291 if (legitimate_small_data_p (mode, x))
10292 return 1;
10293 if (legitimate_constant_pool_address_p (x, mode,
10294 reg_ok_strict || lra_in_progress))
10295 return 1;
10296 if (reg_addr[mode].fused_toc && GET_CODE (x) == UNSPEC
10297 && XINT (x, 1) == UNSPEC_FUSION_ADDIS)
10298 return 1;
10301 /* For TImode, if we have TImode in VSX registers, only allow register
10302 indirect addresses. This will allow the values to go in either GPRs
10303 or VSX registers without reloading. The vector types would tend to
10304 go into VSX registers, so we allow REG+REG, while TImode seems
10305 somewhat split, in that some uses are GPR based, and some VSX based. */
10306 /* FIXME: We could loosen this by changing the following to
10307 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
10308 but currently we cannot allow REG+REG addressing for TImode. See
10309 PR72827 for complete details on how this ends up hoodwinking DSE. */
10310 if (mode == TImode && TARGET_VSX_TIMODE)
10311 return 0;
10312 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
10313 if (! reg_ok_strict
10314 && reg_offset_p
10315 && GET_CODE (x) == PLUS
10316 && GET_CODE (XEXP (x, 0)) == REG
10317 && (XEXP (x, 0) == virtual_stack_vars_rtx
10318 || XEXP (x, 0) == arg_pointer_rtx)
10319 && GET_CODE (XEXP (x, 1)) == CONST_INT)
10320 return 1;
10321 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
10322 return 1;
10323 if (!FLOAT128_2REG_P (mode)
10324 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
10325 || TARGET_POWERPC64
10326 || (mode != DFmode && mode != DDmode)
10327 || (TARGET_E500_DOUBLE && mode != DDmode))
10328 && (TARGET_POWERPC64 || mode != DImode)
10329 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
10330 && mode != PTImode
10331 && !avoiding_indexed_address_p (mode)
10332 && legitimate_indexed_address_p (x, reg_ok_strict))
10333 return 1;
10334 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
10335 && mode_supports_pre_modify_p (mode)
10336 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
10337 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
10338 reg_ok_strict, false)
10339 || (!avoiding_indexed_address_p (mode)
10340 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
10341 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
10342 return 1;
10343 if (reg_offset_p && !quad_offset_p
10344 && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
10345 return 1;
10346 return 0;
10349 /* Debug version of rs6000_legitimate_address_p. */
10350 static bool
10351 rs6000_debug_legitimate_address_p (machine_mode mode, rtx x,
10352 bool reg_ok_strict)
10354 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
10355 fprintf (stderr,
10356 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
10357 "strict = %d, reload = %s, code = %s\n",
10358 ret ? "true" : "false",
10359 GET_MODE_NAME (mode),
10360 reg_ok_strict,
10361 (reload_completed
10362 ? "after"
10363 : (reload_in_progress ? "progress" : "before")),
10364 GET_RTX_NAME (GET_CODE (x)));
10365 debug_rtx (x);
10367 return ret;
10370 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
10372 static bool
10373 rs6000_mode_dependent_address_p (const_rtx addr,
10374 addr_space_t as ATTRIBUTE_UNUSED)
10376 return rs6000_mode_dependent_address_ptr (addr);
10379 /* Go to LABEL if ADDR (a legitimate address expression)
10380 has an effect that depends on the machine mode it is used for.
10382 On the RS/6000 this is true of all integral offsets (since AltiVec
10383 and VSX modes don't allow them) or is a pre-increment or decrement.
10385 ??? Except that due to conceptual problems in offsettable_address_p
10386 we can't really report the problems of integral offsets. So leave
10387 this assuming that the adjustable offset must be valid for the
10388 sub-words of a TFmode operand, which is what we had before. */
10390 static bool
10391 rs6000_mode_dependent_address (const_rtx addr)
10393 switch (GET_CODE (addr))
10395 case PLUS:
10396 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
10397 is considered a legitimate address before reload, so there
10398 are no offset restrictions in that case. Note that this
10399 condition is safe in strict mode because any address involving
10400 virtual_stack_vars_rtx or arg_pointer_rtx would already have
10401 been rejected as illegitimate. */
10402 if (XEXP (addr, 0) != virtual_stack_vars_rtx
10403 && XEXP (addr, 0) != arg_pointer_rtx
10404 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
10406 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
10407 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
10409 break;
10411 case LO_SUM:
10412 /* Anything in the constant pool is sufficiently aligned that
10413 all bytes have the same high part address. */
10414 return !legitimate_constant_pool_address_p (addr, QImode, false);
10416 /* Auto-increment cases are now treated generically in recog.c. */
10417 case PRE_MODIFY:
10418 return TARGET_UPDATE;
10420 /* AND is only allowed in Altivec loads. */
10421 case AND:
10422 return true;
10424 default:
10425 break;
10428 return false;
10431 /* Debug version of rs6000_mode_dependent_address. */
10432 static bool
10433 rs6000_debug_mode_dependent_address (const_rtx addr)
10435 bool ret = rs6000_mode_dependent_address (addr);
10437 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
10438 ret ? "true" : "false");
10439 debug_rtx (addr);
10441 return ret;
10444 /* Implement FIND_BASE_TERM. */
10447 rs6000_find_base_term (rtx op)
10449 rtx base;
10451 base = op;
10452 if (GET_CODE (base) == CONST)
10453 base = XEXP (base, 0);
10454 if (GET_CODE (base) == PLUS)
10455 base = XEXP (base, 0);
10456 if (GET_CODE (base) == UNSPEC)
10457 switch (XINT (base, 1))
10459 case UNSPEC_TOCREL:
10460 case UNSPEC_MACHOPIC_OFFSET:
10461 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
10462 for aliasing purposes. */
10463 return XVECEXP (base, 0, 0);
10466 return op;
10469 /* More elaborate version of recog's offsettable_memref_p predicate
10470 that works around the ??? note of rs6000_mode_dependent_address.
10471 In particular it accepts
10473 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
10475 in 32-bit mode, that the recog predicate rejects. */
10477 static bool
10478 rs6000_offsettable_memref_p (rtx op, machine_mode reg_mode)
10480 bool worst_case;
10482 if (!MEM_P (op))
10483 return false;
10485 /* First mimic offsettable_memref_p. */
10486 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
10487 return true;
10489 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
10490 the latter predicate knows nothing about the mode of the memory
10491 reference and, therefore, assumes that it is the largest supported
10492 mode (TFmode). As a consequence, legitimate offsettable memory
10493 references are rejected. rs6000_legitimate_offset_address_p contains
10494 the correct logic for the PLUS case of rs6000_mode_dependent_address,
10495 at least with a little bit of help here given that we know the
10496 actual registers used. */
10497 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
10498 || GET_MODE_SIZE (reg_mode) == 4);
10499 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
10500 true, worst_case);
10503 /* Determine the reassociation width to be used in reassociate_bb.
10504 This takes into account how many parallel operations we
10505 can actually do of a given type, and also the latency.
10507 int add/sub 6/cycle
10508 mul 2/cycle
10509 vect add/sub/mul 2/cycle
10510 fp add/sub/mul 2/cycle
10511 dfp 1/cycle
10514 static int
10515 rs6000_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
10516 machine_mode mode)
10518 switch (rs6000_cpu)
10520 case PROCESSOR_POWER8:
10521 case PROCESSOR_POWER9:
10522 if (DECIMAL_FLOAT_MODE_P (mode))
10523 return 1;
10524 if (VECTOR_MODE_P (mode))
10525 return 4;
10526 if (INTEGRAL_MODE_P (mode))
10527 return opc == MULT_EXPR ? 4 : 6;
10528 if (FLOAT_MODE_P (mode))
10529 return 4;
10530 break;
10531 default:
10532 break;
10534 return 1;
10537 /* Change register usage conditional on target flags. */
10538 static void
10539 rs6000_conditional_register_usage (void)
10541 int i;
10543 if (TARGET_DEBUG_TARGET)
10544 fprintf (stderr, "rs6000_conditional_register_usage called\n");
10546 /* Set MQ register fixed (already call_used) so that it will not be
10547 allocated. */
10548 fixed_regs[64] = 1;
10550 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
10551 if (TARGET_64BIT)
10552 fixed_regs[13] = call_used_regs[13]
10553 = call_really_used_regs[13] = 1;
10555 /* Conditionally disable FPRs. */
10556 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
10557 for (i = 32; i < 64; i++)
10558 fixed_regs[i] = call_used_regs[i]
10559 = call_really_used_regs[i] = 1;
10561 /* The TOC register is not killed across calls in a way that is
10562 visible to the compiler. */
10563 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10564 call_really_used_regs[2] = 0;
10566 if (DEFAULT_ABI == ABI_V4 && flag_pic == 2)
10567 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10569 if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10570 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10571 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10572 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10574 if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
10575 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10576 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10577 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10579 if (TARGET_TOC && TARGET_MINIMAL_TOC)
10580 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10581 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10583 if (TARGET_SPE)
10585 global_regs[SPEFSCR_REGNO] = 1;
10586 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
10587 registers in prologues and epilogues. We no longer use r14
10588 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
10589 pool for link-compatibility with older versions of GCC. Once
10590 "old" code has died out, we can return r14 to the allocation
10591 pool. */
10592 fixed_regs[14]
10593 = call_used_regs[14]
10594 = call_really_used_regs[14] = 1;
10597 if (!TARGET_ALTIVEC && !TARGET_VSX)
10599 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10600 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
10601 call_really_used_regs[VRSAVE_REGNO] = 1;
10604 if (TARGET_ALTIVEC || TARGET_VSX)
10605 global_regs[VSCR_REGNO] = 1;
10607 if (TARGET_ALTIVEC_ABI)
10609 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
10610 call_used_regs[i] = call_really_used_regs[i] = 1;
10612 /* AIX reserves VR20:31 in non-extended ABI mode. */
10613 if (TARGET_XCOFF)
10614 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
10615 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
10620 /* Output insns to set DEST equal to the constant SOURCE as a series of
10621 lis, ori and shl instructions and return TRUE. */
10623 bool
10624 rs6000_emit_set_const (rtx dest, rtx source)
10626 machine_mode mode = GET_MODE (dest);
10627 rtx temp, set;
10628 rtx_insn *insn;
10629 HOST_WIDE_INT c;
10631 gcc_checking_assert (CONST_INT_P (source));
10632 c = INTVAL (source);
10633 switch (mode)
10635 case E_QImode:
10636 case E_HImode:
10637 emit_insn (gen_rtx_SET (dest, source));
10638 return true;
10640 case E_SImode:
10641 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
10643 emit_insn (gen_rtx_SET (copy_rtx (temp),
10644 GEN_INT (c & ~(HOST_WIDE_INT) 0xffff)));
10645 emit_insn (gen_rtx_SET (dest,
10646 gen_rtx_IOR (SImode, copy_rtx (temp),
10647 GEN_INT (c & 0xffff))));
10648 break;
10650 case E_DImode:
10651 if (!TARGET_POWERPC64)
10653 rtx hi, lo;
10655 hi = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN == 0,
10656 DImode);
10657 lo = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
10658 DImode);
10659 emit_move_insn (hi, GEN_INT (c >> 32));
10660 c = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
10661 emit_move_insn (lo, GEN_INT (c));
10663 else
10664 rs6000_emit_set_long_const (dest, c);
10665 break;
10667 default:
10668 gcc_unreachable ();
10671 insn = get_last_insn ();
10672 set = single_set (insn);
10673 if (! CONSTANT_P (SET_SRC (set)))
10674 set_unique_reg_note (insn, REG_EQUAL, GEN_INT (c));
10676 return true;
10679 /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode.
10680 Output insns to set DEST equal to the constant C as a series of
10681 lis, ori and shl instructions. */
10683 static void
10684 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c)
10686 rtx temp;
10687 HOST_WIDE_INT ud1, ud2, ud3, ud4;
10689 ud1 = c & 0xffff;
10690 c = c >> 16;
10691 ud2 = c & 0xffff;
10692 c = c >> 16;
10693 ud3 = c & 0xffff;
10694 c = c >> 16;
10695 ud4 = c & 0xffff;
10697 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
10698 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
10699 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
10701 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
10702 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
10704 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10706 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10707 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
10708 if (ud1 != 0)
10709 emit_move_insn (dest,
10710 gen_rtx_IOR (DImode, copy_rtx (temp),
10711 GEN_INT (ud1)));
10713 else if (ud3 == 0 && ud4 == 0)
10715 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10717 gcc_assert (ud2 & 0x8000);
10718 emit_move_insn (copy_rtx (temp),
10719 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
10720 if (ud1 != 0)
10721 emit_move_insn (copy_rtx (temp),
10722 gen_rtx_IOR (DImode, copy_rtx (temp),
10723 GEN_INT (ud1)));
10724 emit_move_insn (dest,
10725 gen_rtx_ZERO_EXTEND (DImode,
10726 gen_lowpart (SImode,
10727 copy_rtx (temp))));
10729 else if ((ud4 == 0xffff && (ud3 & 0x8000))
10730 || (ud4 == 0 && ! (ud3 & 0x8000)))
10732 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10734 emit_move_insn (copy_rtx (temp),
10735 GEN_INT (((ud3 << 16) ^ 0x80000000) - 0x80000000));
10736 if (ud2 != 0)
10737 emit_move_insn (copy_rtx (temp),
10738 gen_rtx_IOR (DImode, copy_rtx (temp),
10739 GEN_INT (ud2)));
10740 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10741 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
10742 GEN_INT (16)));
10743 if (ud1 != 0)
10744 emit_move_insn (dest,
10745 gen_rtx_IOR (DImode, copy_rtx (temp),
10746 GEN_INT (ud1)));
10748 else
10750 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10752 emit_move_insn (copy_rtx (temp),
10753 GEN_INT (((ud4 << 16) ^ 0x80000000) - 0x80000000));
10754 if (ud3 != 0)
10755 emit_move_insn (copy_rtx (temp),
10756 gen_rtx_IOR (DImode, copy_rtx (temp),
10757 GEN_INT (ud3)));
10759 emit_move_insn (ud2 != 0 || ud1 != 0 ? copy_rtx (temp) : dest,
10760 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
10761 GEN_INT (32)));
10762 if (ud2 != 0)
10763 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10764 gen_rtx_IOR (DImode, copy_rtx (temp),
10765 GEN_INT (ud2 << 16)));
10766 if (ud1 != 0)
10767 emit_move_insn (dest,
10768 gen_rtx_IOR (DImode, copy_rtx (temp),
10769 GEN_INT (ud1)));
10773 /* Helper for the following. Get rid of [r+r] memory refs
10774 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
10776 static void
10777 rs6000_eliminate_indexed_memrefs (rtx operands[2])
10779 if (reload_in_progress)
10780 return;
10782 if (GET_CODE (operands[0]) == MEM
10783 && GET_CODE (XEXP (operands[0], 0)) != REG
10784 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
10785 GET_MODE (operands[0]), false))
10786 operands[0]
10787 = replace_equiv_address (operands[0],
10788 copy_addr_to_reg (XEXP (operands[0], 0)));
10790 if (GET_CODE (operands[1]) == MEM
10791 && GET_CODE (XEXP (operands[1], 0)) != REG
10792 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
10793 GET_MODE (operands[1]), false))
10794 operands[1]
10795 = replace_equiv_address (operands[1],
10796 copy_addr_to_reg (XEXP (operands[1], 0)));
10799 /* Generate a vector of constants to permute MODE for a little-endian
10800 storage operation by swapping the two halves of a vector. */
10801 static rtvec
10802 rs6000_const_vec (machine_mode mode)
10804 int i, subparts;
10805 rtvec v;
10807 switch (mode)
10809 case E_V1TImode:
10810 subparts = 1;
10811 break;
10812 case E_V2DFmode:
10813 case E_V2DImode:
10814 subparts = 2;
10815 break;
10816 case E_V4SFmode:
10817 case E_V4SImode:
10818 subparts = 4;
10819 break;
10820 case E_V8HImode:
10821 subparts = 8;
10822 break;
10823 case E_V16QImode:
10824 subparts = 16;
10825 break;
10826 default:
10827 gcc_unreachable();
10830 v = rtvec_alloc (subparts);
10832 for (i = 0; i < subparts / 2; ++i)
10833 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
10834 for (i = subparts / 2; i < subparts; ++i)
10835 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
10837 return v;
10840 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
10841 for a VSX load or store operation. */
10843 rs6000_gen_le_vsx_permute (rtx source, machine_mode mode)
10845 /* Use ROTATE instead of VEC_SELECT on IEEE 128-bit floating point, and
10846 128-bit integers if they are allowed in VSX registers. */
10847 if (FLOAT128_VECTOR_P (mode) || mode == TImode || mode == V1TImode)
10848 return gen_rtx_ROTATE (mode, source, GEN_INT (64));
10849 else
10851 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
10852 return gen_rtx_VEC_SELECT (mode, source, par);
10856 /* Emit a little-endian load from vector memory location SOURCE to VSX
10857 register DEST in mode MODE. The load is done with two permuting
10858 insn's that represent an lxvd2x and xxpermdi. */
10859 void
10860 rs6000_emit_le_vsx_load (rtx dest, rtx source, machine_mode mode)
10862 rtx tmp, permute_mem, permute_reg;
10864 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
10865 V1TImode). */
10866 if (mode == TImode || mode == V1TImode)
10868 mode = V2DImode;
10869 dest = gen_lowpart (V2DImode, dest);
10870 source = adjust_address (source, V2DImode, 0);
10873 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
10874 permute_mem = rs6000_gen_le_vsx_permute (source, mode);
10875 permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
10876 emit_insn (gen_rtx_SET (tmp, permute_mem));
10877 emit_insn (gen_rtx_SET (dest, permute_reg));
10880 /* Emit a little-endian store to vector memory location DEST from VSX
10881 register SOURCE in mode MODE. The store is done with two permuting
10882 insn's that represent an xxpermdi and an stxvd2x. */
10883 void
10884 rs6000_emit_le_vsx_store (rtx dest, rtx source, machine_mode mode)
10886 rtx tmp, permute_src, permute_tmp;
10888 /* This should never be called during or after reload, because it does
10889 not re-permute the source register. It is intended only for use
10890 during expand. */
10891 gcc_assert (!reload_in_progress && !lra_in_progress && !reload_completed);
10893 /* Use V2DImode to do swaps of types with 128-bit scalar parts (TImode,
10894 V1TImode). */
10895 if (mode == TImode || mode == V1TImode)
10897 mode = V2DImode;
10898 dest = adjust_address (dest, V2DImode, 0);
10899 source = gen_lowpart (V2DImode, source);
10902 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
10903 permute_src = rs6000_gen_le_vsx_permute (source, mode);
10904 permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
10905 emit_insn (gen_rtx_SET (tmp, permute_src));
10906 emit_insn (gen_rtx_SET (dest, permute_tmp));
10909 /* Emit a sequence representing a little-endian VSX load or store,
10910 moving data from SOURCE to DEST in mode MODE. This is done
10911 separately from rs6000_emit_move to ensure it is called only
10912 during expand. LE VSX loads and stores introduced later are
10913 handled with a split. The expand-time RTL generation allows
10914 us to optimize away redundant pairs of register-permutes. */
10915 void
10916 rs6000_emit_le_vsx_move (rtx dest, rtx source, machine_mode mode)
10918 gcc_assert (!BYTES_BIG_ENDIAN
10919 && VECTOR_MEM_VSX_P (mode)
10920 && !TARGET_P9_VECTOR
10921 && !gpr_or_gpr_p (dest, source)
10922 && (MEM_P (source) ^ MEM_P (dest)));
10924 if (MEM_P (source))
10926 gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
10927 rs6000_emit_le_vsx_load (dest, source, mode);
10929 else
10931 if (!REG_P (source))
10932 source = force_reg (mode, source);
10933 rs6000_emit_le_vsx_store (dest, source, mode);
10937 /* Return whether a SFmode or SImode move can be done without converting one
10938 mode to another. This arrises when we have:
10940 (SUBREG:SF (REG:SI ...))
10941 (SUBREG:SI (REG:SF ...))
10943 and one of the values is in a floating point/vector register, where SFmode
10944 scalars are stored in DFmode format. */
10946 bool
10947 valid_sf_si_move (rtx dest, rtx src, machine_mode mode)
10949 if (TARGET_ALLOW_SF_SUBREG)
10950 return true;
10952 if (mode != SFmode && GET_MODE_CLASS (mode) != MODE_INT)
10953 return true;
10955 if (!SUBREG_P (src) || !sf_subreg_operand (src, mode))
10956 return true;
10958 /*. Allow (set (SUBREG:SI (REG:SF)) (SUBREG:SI (REG:SF))). */
10959 if (SUBREG_P (dest))
10961 rtx dest_subreg = SUBREG_REG (dest);
10962 rtx src_subreg = SUBREG_REG (src);
10963 return GET_MODE (dest_subreg) == GET_MODE (src_subreg);
10966 return false;
10970 /* Helper function to change moves with:
10972 (SUBREG:SF (REG:SI)) and
10973 (SUBREG:SI (REG:SF))
10975 into separate UNSPEC insns. In the PowerPC architecture, scalar SFmode
10976 values are stored as DFmode values in the VSX registers. We need to convert
10977 the bits before we can use a direct move or operate on the bits in the
10978 vector register as an integer type.
10980 Skip things like (set (SUBREG:SI (...) (SUBREG:SI (...)). */
10982 static bool
10983 rs6000_emit_move_si_sf_subreg (rtx dest, rtx source, machine_mode mode)
10985 if (TARGET_DIRECT_MOVE_64BIT && !reload_in_progress && !reload_completed
10986 && !lra_in_progress
10987 && (!SUBREG_P (dest) || !sf_subreg_operand (dest, mode))
10988 && SUBREG_P (source) && sf_subreg_operand (source, mode))
10990 rtx inner_source = SUBREG_REG (source);
10991 machine_mode inner_mode = GET_MODE (inner_source);
10993 if (mode == SImode && inner_mode == SFmode)
10995 emit_insn (gen_movsi_from_sf (dest, inner_source));
10996 return true;
10999 if (mode == SFmode && inner_mode == SImode)
11001 emit_insn (gen_movsf_from_si (dest, inner_source));
11002 return true;
11006 return false;
11009 /* Emit a move from SOURCE to DEST in mode MODE. */
11010 void
11011 rs6000_emit_move (rtx dest, rtx source, machine_mode mode)
11013 rtx operands[2];
11014 operands[0] = dest;
11015 operands[1] = source;
11017 if (TARGET_DEBUG_ADDR)
11019 fprintf (stderr,
11020 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
11021 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
11022 GET_MODE_NAME (mode),
11023 reload_in_progress,
11024 reload_completed,
11025 can_create_pseudo_p ());
11026 debug_rtx (dest);
11027 fprintf (stderr, "source:\n");
11028 debug_rtx (source);
11031 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
11032 if (CONST_WIDE_INT_P (operands[1])
11033 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11035 /* This should be fixed with the introduction of CONST_WIDE_INT. */
11036 gcc_unreachable ();
11039 /* See if we need to special case SImode/SFmode SUBREG moves. */
11040 if ((mode == SImode || mode == SFmode) && SUBREG_P (source)
11041 && rs6000_emit_move_si_sf_subreg (dest, source, mode))
11042 return;
11044 /* Check if GCC is setting up a block move that will end up using FP
11045 registers as temporaries. We must make sure this is acceptable. */
11046 if (GET_CODE (operands[0]) == MEM
11047 && GET_CODE (operands[1]) == MEM
11048 && mode == DImode
11049 && (rs6000_slow_unaligned_access (DImode, MEM_ALIGN (operands[0]))
11050 || rs6000_slow_unaligned_access (DImode, MEM_ALIGN (operands[1])))
11051 && ! (rs6000_slow_unaligned_access (SImode,
11052 (MEM_ALIGN (operands[0]) > 32
11053 ? 32 : MEM_ALIGN (operands[0])))
11054 || rs6000_slow_unaligned_access (SImode,
11055 (MEM_ALIGN (operands[1]) > 32
11056 ? 32 : MEM_ALIGN (operands[1]))))
11057 && ! MEM_VOLATILE_P (operands [0])
11058 && ! MEM_VOLATILE_P (operands [1]))
11060 emit_move_insn (adjust_address (operands[0], SImode, 0),
11061 adjust_address (operands[1], SImode, 0));
11062 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
11063 adjust_address (copy_rtx (operands[1]), SImode, 4));
11064 return;
11067 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
11068 && !gpc_reg_operand (operands[1], mode))
11069 operands[1] = force_reg (mode, operands[1]);
11071 /* Recognize the case where operand[1] is a reference to thread-local
11072 data and load its address to a register. */
11073 if (tls_referenced_p (operands[1]))
11075 enum tls_model model;
11076 rtx tmp = operands[1];
11077 rtx addend = NULL;
11079 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
11081 addend = XEXP (XEXP (tmp, 0), 1);
11082 tmp = XEXP (XEXP (tmp, 0), 0);
11085 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
11086 model = SYMBOL_REF_TLS_MODEL (tmp);
11087 gcc_assert (model != 0);
11089 tmp = rs6000_legitimize_tls_address (tmp, model);
11090 if (addend)
11092 tmp = gen_rtx_PLUS (mode, tmp, addend);
11093 tmp = force_operand (tmp, operands[0]);
11095 operands[1] = tmp;
11098 /* Handle the case where reload calls us with an invalid address. */
11099 if (reload_in_progress && mode == Pmode
11100 && (! general_operand (operands[1], mode)
11101 || ! nonimmediate_operand (operands[0], mode)))
11102 goto emit_set;
11104 /* 128-bit constant floating-point values on Darwin should really be loaded
11105 as two parts. However, this premature splitting is a problem when DFmode
11106 values can go into Altivec registers. */
11107 if (FLOAT128_IBM_P (mode) && !reg_addr[DFmode].scalar_in_vmx_p
11108 && GET_CODE (operands[1]) == CONST_DOUBLE)
11110 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
11111 simplify_gen_subreg (DFmode, operands[1], mode, 0),
11112 DFmode);
11113 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
11114 GET_MODE_SIZE (DFmode)),
11115 simplify_gen_subreg (DFmode, operands[1], mode,
11116 GET_MODE_SIZE (DFmode)),
11117 DFmode);
11118 return;
11121 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
11122 cfun->machine->sdmode_stack_slot =
11123 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
11126 /* Transform (p0:DD, (SUBREG:DD p1:SD)) to ((SUBREG:SD p0:DD),
11127 p1:SD) if p1 is not of floating point class and p0 is spilled as
11128 we can have no analogous movsd_store for this. */
11129 if (lra_in_progress && mode == DDmode
11130 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
11131 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
11132 && GET_CODE (operands[1]) == SUBREG && REG_P (SUBREG_REG (operands[1]))
11133 && GET_MODE (SUBREG_REG (operands[1])) == SDmode)
11135 enum reg_class cl;
11136 int regno = REGNO (SUBREG_REG (operands[1]));
11138 if (regno >= FIRST_PSEUDO_REGISTER)
11140 cl = reg_preferred_class (regno);
11141 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][1];
11143 if (regno >= 0 && ! FP_REGNO_P (regno))
11145 mode = SDmode;
11146 operands[0] = gen_lowpart_SUBREG (SDmode, operands[0]);
11147 operands[1] = SUBREG_REG (operands[1]);
11150 if (lra_in_progress
11151 && mode == SDmode
11152 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
11153 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
11154 && (REG_P (operands[1])
11155 || (GET_CODE (operands[1]) == SUBREG
11156 && REG_P (SUBREG_REG (operands[1])))))
11158 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
11159 ? SUBREG_REG (operands[1]) : operands[1]);
11160 enum reg_class cl;
11162 if (regno >= FIRST_PSEUDO_REGISTER)
11164 cl = reg_preferred_class (regno);
11165 gcc_assert (cl != NO_REGS);
11166 regno = ira_class_hard_regs[cl][0];
11168 if (FP_REGNO_P (regno))
11170 if (GET_MODE (operands[0]) != DDmode)
11171 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
11172 emit_insn (gen_movsd_store (operands[0], operands[1]));
11174 else if (INT_REGNO_P (regno))
11175 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
11176 else
11177 gcc_unreachable();
11178 return;
11180 /* Transform ((SUBREG:DD p0:SD), p1:DD) to (p0:SD, (SUBREG:SD
11181 p:DD)) if p0 is not of floating point class and p1 is spilled as
11182 we can have no analogous movsd_load for this. */
11183 if (lra_in_progress && mode == DDmode
11184 && GET_CODE (operands[0]) == SUBREG && REG_P (SUBREG_REG (operands[0]))
11185 && GET_MODE (SUBREG_REG (operands[0])) == SDmode
11186 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
11187 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
11189 enum reg_class cl;
11190 int regno = REGNO (SUBREG_REG (operands[0]));
11192 if (regno >= FIRST_PSEUDO_REGISTER)
11194 cl = reg_preferred_class (regno);
11195 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][0];
11197 if (regno >= 0 && ! FP_REGNO_P (regno))
11199 mode = SDmode;
11200 operands[0] = SUBREG_REG (operands[0]);
11201 operands[1] = gen_lowpart_SUBREG (SDmode, operands[1]);
11204 if (lra_in_progress
11205 && mode == SDmode
11206 && (REG_P (operands[0])
11207 || (GET_CODE (operands[0]) == SUBREG
11208 && REG_P (SUBREG_REG (operands[0]))))
11209 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
11210 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
11212 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
11213 ? SUBREG_REG (operands[0]) : operands[0]);
11214 enum reg_class cl;
11216 if (regno >= FIRST_PSEUDO_REGISTER)
11218 cl = reg_preferred_class (regno);
11219 gcc_assert (cl != NO_REGS);
11220 regno = ira_class_hard_regs[cl][0];
11222 if (FP_REGNO_P (regno))
11224 if (GET_MODE (operands[1]) != DDmode)
11225 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
11226 emit_insn (gen_movsd_load (operands[0], operands[1]));
11228 else if (INT_REGNO_P (regno))
11229 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
11230 else
11231 gcc_unreachable();
11232 return;
11235 if (reload_in_progress
11236 && mode == SDmode
11237 && cfun->machine->sdmode_stack_slot != NULL_RTX
11238 && MEM_P (operands[0])
11239 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
11240 && REG_P (operands[1]))
11242 if (FP_REGNO_P (REGNO (operands[1])))
11244 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
11245 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11246 emit_insn (gen_movsd_store (mem, operands[1]));
11248 else if (INT_REGNO_P (REGNO (operands[1])))
11250 rtx mem = operands[0];
11251 if (BYTES_BIG_ENDIAN)
11252 mem = adjust_address_nv (mem, mode, 4);
11253 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11254 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
11256 else
11257 gcc_unreachable();
11258 return;
11260 if (reload_in_progress
11261 && mode == SDmode
11262 && REG_P (operands[0])
11263 && MEM_P (operands[1])
11264 && cfun->machine->sdmode_stack_slot != NULL_RTX
11265 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
11267 if (FP_REGNO_P (REGNO (operands[0])))
11269 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
11270 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11271 emit_insn (gen_movsd_load (operands[0], mem));
11273 else if (INT_REGNO_P (REGNO (operands[0])))
11275 rtx mem = operands[1];
11276 if (BYTES_BIG_ENDIAN)
11277 mem = adjust_address_nv (mem, mode, 4);
11278 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11279 emit_insn (gen_movsd_hardfloat (operands[0], mem));
11281 else
11282 gcc_unreachable();
11283 return;
11286 /* FIXME: In the long term, this switch statement should go away
11287 and be replaced by a sequence of tests based on things like
11288 mode == Pmode. */
11289 switch (mode)
11291 case E_HImode:
11292 case E_QImode:
11293 if (CONSTANT_P (operands[1])
11294 && GET_CODE (operands[1]) != CONST_INT)
11295 operands[1] = force_const_mem (mode, operands[1]);
11296 break;
11298 case E_TFmode:
11299 case E_TDmode:
11300 case E_IFmode:
11301 case E_KFmode:
11302 if (FLOAT128_2REG_P (mode))
11303 rs6000_eliminate_indexed_memrefs (operands);
11304 /* fall through */
11306 case E_DFmode:
11307 case E_DDmode:
11308 case E_SFmode:
11309 case E_SDmode:
11310 if (CONSTANT_P (operands[1])
11311 && ! easy_fp_constant (operands[1], mode))
11312 operands[1] = force_const_mem (mode, operands[1]);
11313 break;
11315 case E_V16QImode:
11316 case E_V8HImode:
11317 case E_V4SFmode:
11318 case E_V4SImode:
11319 case E_V4HImode:
11320 case E_V2SFmode:
11321 case E_V2SImode:
11322 case E_V1DImode:
11323 case E_V2DFmode:
11324 case E_V2DImode:
11325 case E_V1TImode:
11326 if (CONSTANT_P (operands[1])
11327 && !easy_vector_constant (operands[1], mode))
11328 operands[1] = force_const_mem (mode, operands[1]);
11329 break;
11331 case E_SImode:
11332 case E_DImode:
11333 /* Use default pattern for address of ELF small data */
11334 if (TARGET_ELF
11335 && mode == Pmode
11336 && DEFAULT_ABI == ABI_V4
11337 && (GET_CODE (operands[1]) == SYMBOL_REF
11338 || GET_CODE (operands[1]) == CONST)
11339 && small_data_operand (operands[1], mode))
11341 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11342 return;
11345 if (DEFAULT_ABI == ABI_V4
11346 && mode == Pmode && mode == SImode
11347 && flag_pic == 1 && got_operand (operands[1], mode))
11349 emit_insn (gen_movsi_got (operands[0], operands[1]));
11350 return;
11353 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
11354 && TARGET_NO_TOC
11355 && ! flag_pic
11356 && mode == Pmode
11357 && CONSTANT_P (operands[1])
11358 && GET_CODE (operands[1]) != HIGH
11359 && GET_CODE (operands[1]) != CONST_INT)
11361 rtx target = (!can_create_pseudo_p ()
11362 ? operands[0]
11363 : gen_reg_rtx (mode));
11365 /* If this is a function address on -mcall-aixdesc,
11366 convert it to the address of the descriptor. */
11367 if (DEFAULT_ABI == ABI_AIX
11368 && GET_CODE (operands[1]) == SYMBOL_REF
11369 && XSTR (operands[1], 0)[0] == '.')
11371 const char *name = XSTR (operands[1], 0);
11372 rtx new_ref;
11373 while (*name == '.')
11374 name++;
11375 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
11376 CONSTANT_POOL_ADDRESS_P (new_ref)
11377 = CONSTANT_POOL_ADDRESS_P (operands[1]);
11378 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
11379 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
11380 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
11381 operands[1] = new_ref;
11384 if (DEFAULT_ABI == ABI_DARWIN)
11386 #if TARGET_MACHO
11387 if (MACHO_DYNAMIC_NO_PIC_P)
11389 /* Take care of any required data indirection. */
11390 operands[1] = rs6000_machopic_legitimize_pic_address (
11391 operands[1], mode, operands[0]);
11392 if (operands[0] != operands[1])
11393 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11394 return;
11396 #endif
11397 emit_insn (gen_macho_high (target, operands[1]));
11398 emit_insn (gen_macho_low (operands[0], target, operands[1]));
11399 return;
11402 emit_insn (gen_elf_high (target, operands[1]));
11403 emit_insn (gen_elf_low (operands[0], target, operands[1]));
11404 return;
11407 /* If this is a SYMBOL_REF that refers to a constant pool entry,
11408 and we have put it in the TOC, we just need to make a TOC-relative
11409 reference to it. */
11410 if (TARGET_TOC
11411 && GET_CODE (operands[1]) == SYMBOL_REF
11412 && use_toc_relative_ref (operands[1], mode))
11413 operands[1] = create_TOC_reference (operands[1], operands[0]);
11414 else if (mode == Pmode
11415 && CONSTANT_P (operands[1])
11416 && GET_CODE (operands[1]) != HIGH
11417 && ((GET_CODE (operands[1]) != CONST_INT
11418 && ! easy_fp_constant (operands[1], mode))
11419 || (GET_CODE (operands[1]) == CONST_INT
11420 && (num_insns_constant (operands[1], mode)
11421 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
11422 || (GET_CODE (operands[0]) == REG
11423 && FP_REGNO_P (REGNO (operands[0]))))
11424 && !toc_relative_expr_p (operands[1], false)
11425 && (TARGET_CMODEL == CMODEL_SMALL
11426 || can_create_pseudo_p ()
11427 || (REG_P (operands[0])
11428 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
11431 #if TARGET_MACHO
11432 /* Darwin uses a special PIC legitimizer. */
11433 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
11435 operands[1] =
11436 rs6000_machopic_legitimize_pic_address (operands[1], mode,
11437 operands[0]);
11438 if (operands[0] != operands[1])
11439 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11440 return;
11442 #endif
11444 /* If we are to limit the number of things we put in the TOC and
11445 this is a symbol plus a constant we can add in one insn,
11446 just put the symbol in the TOC and add the constant. Don't do
11447 this if reload is in progress. */
11448 if (GET_CODE (operands[1]) == CONST
11449 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
11450 && GET_CODE (XEXP (operands[1], 0)) == PLUS
11451 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
11452 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
11453 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
11454 && ! side_effects_p (operands[0]))
11456 rtx sym =
11457 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
11458 rtx other = XEXP (XEXP (operands[1], 0), 1);
11460 sym = force_reg (mode, sym);
11461 emit_insn (gen_add3_insn (operands[0], sym, other));
11462 return;
11465 operands[1] = force_const_mem (mode, operands[1]);
11467 if (TARGET_TOC
11468 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
11469 && use_toc_relative_ref (XEXP (operands[1], 0), mode))
11471 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
11472 operands[0]);
11473 operands[1] = gen_const_mem (mode, tocref);
11474 set_mem_alias_set (operands[1], get_TOC_alias_set ());
11477 break;
11479 case E_TImode:
11480 if (!VECTOR_MEM_VSX_P (TImode))
11481 rs6000_eliminate_indexed_memrefs (operands);
11482 break;
11484 case E_PTImode:
11485 rs6000_eliminate_indexed_memrefs (operands);
11486 break;
11488 default:
11489 fatal_insn ("bad move", gen_rtx_SET (dest, source));
11492 /* Above, we may have called force_const_mem which may have returned
11493 an invalid address. If we can, fix this up; otherwise, reload will
11494 have to deal with it. */
11495 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
11496 operands[1] = validize_mem (operands[1]);
11498 emit_set:
11499 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11502 /* Return true if a structure, union or array containing FIELD should be
11503 accessed using `BLKMODE'.
11505 For the SPE, simd types are V2SI, and gcc can be tempted to put the
11506 entire thing in a DI and use subregs to access the internals.
11507 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
11508 back-end. Because a single GPR can hold a V2SI, but not a DI, the
11509 best thing to do is set structs to BLKmode and avoid Severe Tire
11510 Damage.
11512 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
11513 fit into 1, whereas DI still needs two. */
11515 static bool
11516 rs6000_member_type_forces_blk (const_tree field, machine_mode mode)
11518 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
11519 || (TARGET_E500_DOUBLE && mode == DFmode));
11522 /* Nonzero if we can use a floating-point register to pass this arg. */
11523 #define USE_FP_FOR_ARG_P(CUM,MODE) \
11524 (SCALAR_FLOAT_MODE_NOT_VECTOR_P (MODE) \
11525 && (CUM)->fregno <= FP_ARG_MAX_REG \
11526 && TARGET_HARD_FLOAT && TARGET_FPRS)
11528 /* Nonzero if we can use an AltiVec register to pass this arg. */
11529 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
11530 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
11531 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
11532 && TARGET_ALTIVEC_ABI \
11533 && (NAMED))
11535 /* Walk down the type tree of TYPE counting consecutive base elements.
11536 If *MODEP is VOIDmode, then set it to the first valid floating point
11537 or vector type. If a non-floating point or vector type is found, or
11538 if a floating point or vector type that doesn't match a non-VOIDmode
11539 *MODEP is found, then return -1, otherwise return the count in the
11540 sub-tree. */
11542 static int
11543 rs6000_aggregate_candidate (const_tree type, machine_mode *modep)
11545 machine_mode mode;
11546 HOST_WIDE_INT size;
11548 switch (TREE_CODE (type))
11550 case REAL_TYPE:
11551 mode = TYPE_MODE (type);
11552 if (!SCALAR_FLOAT_MODE_P (mode))
11553 return -1;
11555 if (*modep == VOIDmode)
11556 *modep = mode;
11558 if (*modep == mode)
11559 return 1;
11561 break;
11563 case COMPLEX_TYPE:
11564 mode = TYPE_MODE (TREE_TYPE (type));
11565 if (!SCALAR_FLOAT_MODE_P (mode))
11566 return -1;
11568 if (*modep == VOIDmode)
11569 *modep = mode;
11571 if (*modep == mode)
11572 return 2;
11574 break;
11576 case VECTOR_TYPE:
11577 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
11578 return -1;
11580 /* Use V4SImode as representative of all 128-bit vector types. */
11581 size = int_size_in_bytes (type);
11582 switch (size)
11584 case 16:
11585 mode = V4SImode;
11586 break;
11587 default:
11588 return -1;
11591 if (*modep == VOIDmode)
11592 *modep = mode;
11594 /* Vector modes are considered to be opaque: two vectors are
11595 equivalent for the purposes of being homogeneous aggregates
11596 if they are the same size. */
11597 if (*modep == mode)
11598 return 1;
11600 break;
11602 case ARRAY_TYPE:
11604 int count;
11605 tree index = TYPE_DOMAIN (type);
11607 /* Can't handle incomplete types nor sizes that are not
11608 fixed. */
11609 if (!COMPLETE_TYPE_P (type)
11610 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
11611 return -1;
11613 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
11614 if (count == -1
11615 || !index
11616 || !TYPE_MAX_VALUE (index)
11617 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
11618 || !TYPE_MIN_VALUE (index)
11619 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
11620 || count < 0)
11621 return -1;
11623 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
11624 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
11626 /* There must be no padding. */
11627 if (wi::to_wide (TYPE_SIZE (type))
11628 != count * GET_MODE_BITSIZE (*modep))
11629 return -1;
11631 return count;
11634 case RECORD_TYPE:
11636 int count = 0;
11637 int sub_count;
11638 tree field;
11640 /* Can't handle incomplete types nor sizes that are not
11641 fixed. */
11642 if (!COMPLETE_TYPE_P (type)
11643 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
11644 return -1;
11646 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
11648 if (TREE_CODE (field) != FIELD_DECL)
11649 continue;
11651 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
11652 if (sub_count < 0)
11653 return -1;
11654 count += sub_count;
11657 /* There must be no padding. */
11658 if (wi::to_wide (TYPE_SIZE (type))
11659 != count * GET_MODE_BITSIZE (*modep))
11660 return -1;
11662 return count;
11665 case UNION_TYPE:
11666 case QUAL_UNION_TYPE:
11668 /* These aren't very interesting except in a degenerate case. */
11669 int count = 0;
11670 int sub_count;
11671 tree field;
11673 /* Can't handle incomplete types nor sizes that are not
11674 fixed. */
11675 if (!COMPLETE_TYPE_P (type)
11676 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
11677 return -1;
11679 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
11681 if (TREE_CODE (field) != FIELD_DECL)
11682 continue;
11684 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
11685 if (sub_count < 0)
11686 return -1;
11687 count = count > sub_count ? count : sub_count;
11690 /* There must be no padding. */
11691 if (wi::to_wide (TYPE_SIZE (type))
11692 != count * GET_MODE_BITSIZE (*modep))
11693 return -1;
11695 return count;
11698 default:
11699 break;
11702 return -1;
11705 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
11706 float or vector aggregate that shall be passed in FP/vector registers
11707 according to the ELFv2 ABI, return the homogeneous element mode in
11708 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
11710 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
11712 static bool
11713 rs6000_discover_homogeneous_aggregate (machine_mode mode, const_tree type,
11714 machine_mode *elt_mode,
11715 int *n_elts)
11717 /* Note that we do not accept complex types at the top level as
11718 homogeneous aggregates; these types are handled via the
11719 targetm.calls.split_complex_arg mechanism. Complex types
11720 can be elements of homogeneous aggregates, however. */
11721 if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
11723 machine_mode field_mode = VOIDmode;
11724 int field_count = rs6000_aggregate_candidate (type, &field_mode);
11726 if (field_count > 0)
11728 int n_regs = (SCALAR_FLOAT_MODE_P (field_mode) ?
11729 (GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
11731 /* The ELFv2 ABI allows homogeneous aggregates to occupy
11732 up to AGGR_ARG_NUM_REG registers. */
11733 if (field_count * n_regs <= AGGR_ARG_NUM_REG)
11735 if (elt_mode)
11736 *elt_mode = field_mode;
11737 if (n_elts)
11738 *n_elts = field_count;
11739 return true;
11744 if (elt_mode)
11745 *elt_mode = mode;
11746 if (n_elts)
11747 *n_elts = 1;
11748 return false;
11751 /* Return a nonzero value to say to return the function value in
11752 memory, just as large structures are always returned. TYPE will be
11753 the data type of the value, and FNTYPE will be the type of the
11754 function doing the returning, or @code{NULL} for libcalls.
11756 The AIX ABI for the RS/6000 specifies that all structures are
11757 returned in memory. The Darwin ABI does the same.
11759 For the Darwin 64 Bit ABI, a function result can be returned in
11760 registers or in memory, depending on the size of the return data
11761 type. If it is returned in registers, the value occupies the same
11762 registers as it would if it were the first and only function
11763 argument. Otherwise, the function places its result in memory at
11764 the location pointed to by GPR3.
11766 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
11767 but a draft put them in memory, and GCC used to implement the draft
11768 instead of the final standard. Therefore, aix_struct_return
11769 controls this instead of DEFAULT_ABI; V.4 targets needing backward
11770 compatibility can change DRAFT_V4_STRUCT_RET to override the
11771 default, and -m switches get the final word. See
11772 rs6000_option_override_internal for more details.
11774 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
11775 long double support is enabled. These values are returned in memory.
11777 int_size_in_bytes returns -1 for variable size objects, which go in
11778 memory always. The cast to unsigned makes -1 > 8. */
11780 static bool
11781 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
11783 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
11784 if (TARGET_MACHO
11785 && rs6000_darwin64_abi
11786 && TREE_CODE (type) == RECORD_TYPE
11787 && int_size_in_bytes (type) > 0)
11789 CUMULATIVE_ARGS valcum;
11790 rtx valret;
11792 valcum.words = 0;
11793 valcum.fregno = FP_ARG_MIN_REG;
11794 valcum.vregno = ALTIVEC_ARG_MIN_REG;
11795 /* Do a trial code generation as if this were going to be passed
11796 as an argument; if any part goes in memory, we return NULL. */
11797 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
11798 if (valret)
11799 return false;
11800 /* Otherwise fall through to more conventional ABI rules. */
11803 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
11804 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
11805 NULL, NULL))
11806 return false;
11808 /* The ELFv2 ABI returns aggregates up to 16B in registers */
11809 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
11810 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
11811 return false;
11813 if (AGGREGATE_TYPE_P (type)
11814 && (aix_struct_return
11815 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
11816 return true;
11818 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
11819 modes only exist for GCC vector types if -maltivec. */
11820 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
11821 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11822 return false;
11824 /* Return synthetic vectors in memory. */
11825 if (TREE_CODE (type) == VECTOR_TYPE
11826 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
11828 static bool warned_for_return_big_vectors = false;
11829 if (!warned_for_return_big_vectors)
11831 warning (OPT_Wpsabi, "GCC vector returned by reference: "
11832 "non-standard ABI extension with no compatibility guarantee");
11833 warned_for_return_big_vectors = true;
11835 return true;
11838 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
11839 && FLOAT128_IEEE_P (TYPE_MODE (type)))
11840 return true;
11842 return false;
11845 /* Specify whether values returned in registers should be at the most
11846 significant end of a register. We want aggregates returned by
11847 value to match the way aggregates are passed to functions. */
11849 static bool
11850 rs6000_return_in_msb (const_tree valtype)
11852 return (DEFAULT_ABI == ABI_ELFv2
11853 && BYTES_BIG_ENDIAN
11854 && AGGREGATE_TYPE_P (valtype)
11855 && rs6000_function_arg_padding (TYPE_MODE (valtype),
11856 valtype) == PAD_UPWARD);
11859 #ifdef HAVE_AS_GNU_ATTRIBUTE
11860 /* Return TRUE if a call to function FNDECL may be one that
11861 potentially affects the function calling ABI of the object file. */
11863 static bool
11864 call_ABI_of_interest (tree fndecl)
11866 if (rs6000_gnu_attr && symtab->state == EXPANSION)
11868 struct cgraph_node *c_node;
11870 /* Libcalls are always interesting. */
11871 if (fndecl == NULL_TREE)
11872 return true;
11874 /* Any call to an external function is interesting. */
11875 if (DECL_EXTERNAL (fndecl))
11876 return true;
11878 /* Interesting functions that we are emitting in this object file. */
11879 c_node = cgraph_node::get (fndecl);
11880 c_node = c_node->ultimate_alias_target ();
11881 return !c_node->only_called_directly_p ();
11883 return false;
11885 #endif
11887 /* Initialize a variable CUM of type CUMULATIVE_ARGS
11888 for a call to a function whose data type is FNTYPE.
11889 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
11891 For incoming args we set the number of arguments in the prototype large
11892 so we never return a PARALLEL. */
11894 void
11895 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
11896 rtx libname ATTRIBUTE_UNUSED, int incoming,
11897 int libcall, int n_named_args,
11898 tree fndecl ATTRIBUTE_UNUSED,
11899 machine_mode return_mode ATTRIBUTE_UNUSED)
11901 static CUMULATIVE_ARGS zero_cumulative;
11903 *cum = zero_cumulative;
11904 cum->words = 0;
11905 cum->fregno = FP_ARG_MIN_REG;
11906 cum->vregno = ALTIVEC_ARG_MIN_REG;
11907 cum->prototype = (fntype && prototype_p (fntype));
11908 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
11909 ? CALL_LIBCALL : CALL_NORMAL);
11910 cum->sysv_gregno = GP_ARG_MIN_REG;
11911 cum->stdarg = stdarg_p (fntype);
11912 cum->libcall = libcall;
11914 cum->nargs_prototype = 0;
11915 if (incoming || cum->prototype)
11916 cum->nargs_prototype = n_named_args;
11918 /* Check for a longcall attribute. */
11919 if ((!fntype && rs6000_default_long_calls)
11920 || (fntype
11921 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
11922 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
11923 cum->call_cookie |= CALL_LONG;
11925 if (TARGET_DEBUG_ARG)
11927 fprintf (stderr, "\ninit_cumulative_args:");
11928 if (fntype)
11930 tree ret_type = TREE_TYPE (fntype);
11931 fprintf (stderr, " ret code = %s,",
11932 get_tree_code_name (TREE_CODE (ret_type)));
11935 if (cum->call_cookie & CALL_LONG)
11936 fprintf (stderr, " longcall,");
11938 fprintf (stderr, " proto = %d, nargs = %d\n",
11939 cum->prototype, cum->nargs_prototype);
11942 #ifdef HAVE_AS_GNU_ATTRIBUTE
11943 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4))
11945 cum->escapes = call_ABI_of_interest (fndecl);
11946 if (cum->escapes)
11948 tree return_type;
11950 if (fntype)
11952 return_type = TREE_TYPE (fntype);
11953 return_mode = TYPE_MODE (return_type);
11955 else
11956 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
11958 if (return_type != NULL)
11960 if (TREE_CODE (return_type) == RECORD_TYPE
11961 && TYPE_TRANSPARENT_AGGR (return_type))
11963 return_type = TREE_TYPE (first_field (return_type));
11964 return_mode = TYPE_MODE (return_type);
11966 if (AGGREGATE_TYPE_P (return_type)
11967 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
11968 <= 8))
11969 rs6000_returns_struct = true;
11971 if (SCALAR_FLOAT_MODE_P (return_mode))
11973 rs6000_passes_float = true;
11974 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
11975 && (FLOAT128_IBM_P (return_mode)
11976 || FLOAT128_IEEE_P (return_mode)
11977 || (return_type != NULL
11978 && (TYPE_MAIN_VARIANT (return_type)
11979 == long_double_type_node))))
11980 rs6000_passes_long_double = true;
11982 if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
11983 || SPE_VECTOR_MODE (return_mode))
11984 rs6000_passes_vector = true;
11987 #endif
11989 if (fntype
11990 && !TARGET_ALTIVEC
11991 && TARGET_ALTIVEC_ABI
11992 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
11994 error ("cannot return value in vector register because"
11995 " altivec instructions are disabled, use -maltivec"
11996 " to enable them");
12000 /* The mode the ABI uses for a word. This is not the same as word_mode
12001 for -m32 -mpowerpc64. This is used to implement various target hooks. */
12003 static scalar_int_mode
12004 rs6000_abi_word_mode (void)
12006 return TARGET_32BIT ? SImode : DImode;
12009 /* Implement the TARGET_OFFLOAD_OPTIONS hook. */
12010 static char *
12011 rs6000_offload_options (void)
12013 if (TARGET_64BIT)
12014 return xstrdup ("-foffload-abi=lp64");
12015 else
12016 return xstrdup ("-foffload-abi=ilp32");
12019 /* On rs6000, function arguments are promoted, as are function return
12020 values. */
12022 static machine_mode
12023 rs6000_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
12024 machine_mode mode,
12025 int *punsignedp ATTRIBUTE_UNUSED,
12026 const_tree, int)
12028 PROMOTE_MODE (mode, *punsignedp, type);
12030 return mode;
12033 /* Return true if TYPE must be passed on the stack and not in registers. */
12035 static bool
12036 rs6000_must_pass_in_stack (machine_mode mode, const_tree type)
12038 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
12039 return must_pass_in_stack_var_size (mode, type);
12040 else
12041 return must_pass_in_stack_var_size_or_pad (mode, type);
12044 static inline bool
12045 is_complex_IBM_long_double (machine_mode mode)
12047 return mode == ICmode || (!TARGET_IEEEQUAD && mode == TCmode);
12050 /* Whether ABI_V4 passes MODE args to a function in floating point
12051 registers. */
12053 static bool
12054 abi_v4_pass_in_fpr (machine_mode mode)
12056 if (!TARGET_FPRS || !TARGET_HARD_FLOAT)
12057 return false;
12058 if (TARGET_SINGLE_FLOAT && mode == SFmode)
12059 return true;
12060 if (TARGET_DOUBLE_FLOAT && mode == DFmode)
12061 return true;
12062 /* ABI_V4 passes complex IBM long double in 8 gprs.
12063 Stupid, but we can't change the ABI now. */
12064 if (is_complex_IBM_long_double (mode))
12065 return false;
12066 if (FLOAT128_2REG_P (mode))
12067 return true;
12068 if (DECIMAL_FLOAT_MODE_P (mode))
12069 return true;
12070 return false;
12073 /* Implement TARGET_FUNCTION_ARG_PADDING
12075 For the AIX ABI structs are always stored left shifted in their
12076 argument slot. */
12078 static pad_direction
12079 rs6000_function_arg_padding (machine_mode mode, const_tree type)
12081 #ifndef AGGREGATE_PADDING_FIXED
12082 #define AGGREGATE_PADDING_FIXED 0
12083 #endif
12084 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
12085 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
12086 #endif
12088 if (!AGGREGATE_PADDING_FIXED)
12090 /* GCC used to pass structures of the same size as integer types as
12091 if they were in fact integers, ignoring TARGET_FUNCTION_ARG_PADDING.
12092 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
12093 passed padded downward, except that -mstrict-align further
12094 muddied the water in that multi-component structures of 2 and 4
12095 bytes in size were passed padded upward.
12097 The following arranges for best compatibility with previous
12098 versions of gcc, but removes the -mstrict-align dependency. */
12099 if (BYTES_BIG_ENDIAN)
12101 HOST_WIDE_INT size = 0;
12103 if (mode == BLKmode)
12105 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12106 size = int_size_in_bytes (type);
12108 else
12109 size = GET_MODE_SIZE (mode);
12111 if (size == 1 || size == 2 || size == 4)
12112 return PAD_DOWNWARD;
12114 return PAD_UPWARD;
12117 if (AGGREGATES_PAD_UPWARD_ALWAYS)
12119 if (type != 0 && AGGREGATE_TYPE_P (type))
12120 return PAD_UPWARD;
12123 /* Fall back to the default. */
12124 return default_function_arg_padding (mode, type);
12127 /* If defined, a C expression that gives the alignment boundary, in bits,
12128 of an argument with the specified mode and type. If it is not defined,
12129 PARM_BOUNDARY is used for all arguments.
12131 V.4 wants long longs and doubles to be double word aligned. Just
12132 testing the mode size is a boneheaded way to do this as it means
12133 that other types such as complex int are also double word aligned.
12134 However, we're stuck with this because changing the ABI might break
12135 existing library interfaces.
12137 Doubleword align SPE vectors.
12138 Quadword align Altivec/VSX vectors.
12139 Quadword align large synthetic vector types. */
12141 static unsigned int
12142 rs6000_function_arg_boundary (machine_mode mode, const_tree type)
12144 machine_mode elt_mode;
12145 int n_elts;
12147 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
12149 if (DEFAULT_ABI == ABI_V4
12150 && (GET_MODE_SIZE (mode) == 8
12151 || (TARGET_HARD_FLOAT
12152 && TARGET_FPRS
12153 && !is_complex_IBM_long_double (mode)
12154 && FLOAT128_2REG_P (mode))))
12155 return 64;
12156 else if (FLOAT128_VECTOR_P (mode))
12157 return 128;
12158 else if (SPE_VECTOR_MODE (mode)
12159 || (type && TREE_CODE (type) == VECTOR_TYPE
12160 && int_size_in_bytes (type) >= 8
12161 && int_size_in_bytes (type) < 16))
12162 return 64;
12163 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
12164 || (type && TREE_CODE (type) == VECTOR_TYPE
12165 && int_size_in_bytes (type) >= 16))
12166 return 128;
12168 /* Aggregate types that need > 8 byte alignment are quadword-aligned
12169 in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
12170 -mcompat-align-parm is used. */
12171 if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
12172 || DEFAULT_ABI == ABI_ELFv2)
12173 && type && TYPE_ALIGN (type) > 64)
12175 /* "Aggregate" means any AGGREGATE_TYPE except for single-element
12176 or homogeneous float/vector aggregates here. We already handled
12177 vector aggregates above, but still need to check for float here. */
12178 bool aggregate_p = (AGGREGATE_TYPE_P (type)
12179 && !SCALAR_FLOAT_MODE_P (elt_mode));
12181 /* We used to check for BLKmode instead of the above aggregate type
12182 check. Warn when this results in any difference to the ABI. */
12183 if (aggregate_p != (mode == BLKmode))
12185 static bool warned;
12186 if (!warned && warn_psabi)
12188 warned = true;
12189 inform (input_location,
12190 "the ABI of passing aggregates with %d-byte alignment"
12191 " has changed in GCC 5",
12192 (int) TYPE_ALIGN (type) / BITS_PER_UNIT);
12196 if (aggregate_p)
12197 return 128;
12200 /* Similar for the Darwin64 ABI. Note that for historical reasons we
12201 implement the "aggregate type" check as a BLKmode check here; this
12202 means certain aggregate types are in fact not aligned. */
12203 if (TARGET_MACHO && rs6000_darwin64_abi
12204 && mode == BLKmode
12205 && type && TYPE_ALIGN (type) > 64)
12206 return 128;
12208 return PARM_BOUNDARY;
12211 /* The offset in words to the start of the parameter save area. */
12213 static unsigned int
12214 rs6000_parm_offset (void)
12216 return (DEFAULT_ABI == ABI_V4 ? 2
12217 : DEFAULT_ABI == ABI_ELFv2 ? 4
12218 : 6);
12221 /* For a function parm of MODE and TYPE, return the starting word in
12222 the parameter area. NWORDS of the parameter area are already used. */
12224 static unsigned int
12225 rs6000_parm_start (machine_mode mode, const_tree type,
12226 unsigned int nwords)
12228 unsigned int align;
12230 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
12231 return nwords + (-(rs6000_parm_offset () + nwords) & align);
12234 /* Compute the size (in words) of a function argument. */
12236 static unsigned long
12237 rs6000_arg_size (machine_mode mode, const_tree type)
12239 unsigned long size;
12241 if (mode != BLKmode)
12242 size = GET_MODE_SIZE (mode);
12243 else
12244 size = int_size_in_bytes (type);
12246 if (TARGET_32BIT)
12247 return (size + 3) >> 2;
12248 else
12249 return (size + 7) >> 3;
12252 /* Use this to flush pending int fields. */
12254 static void
12255 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
12256 HOST_WIDE_INT bitpos, int final)
12258 unsigned int startbit, endbit;
12259 int intregs, intoffset;
12261 /* Handle the situations where a float is taking up the first half
12262 of the GPR, and the other half is empty (typically due to
12263 alignment restrictions). We can detect this by a 8-byte-aligned
12264 int field, or by seeing that this is the final flush for this
12265 argument. Count the word and continue on. */
12266 if (cum->floats_in_gpr == 1
12267 && (cum->intoffset % 64 == 0
12268 || (cum->intoffset == -1 && final)))
12270 cum->words++;
12271 cum->floats_in_gpr = 0;
12274 if (cum->intoffset == -1)
12275 return;
12277 intoffset = cum->intoffset;
12278 cum->intoffset = -1;
12279 cum->floats_in_gpr = 0;
12281 if (intoffset % BITS_PER_WORD != 0)
12283 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
12284 if (!int_mode_for_size (bits, 0).exists ())
12286 /* We couldn't find an appropriate mode, which happens,
12287 e.g., in packed structs when there are 3 bytes to load.
12288 Back intoffset back to the beginning of the word in this
12289 case. */
12290 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
12294 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
12295 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
12296 intregs = (endbit - startbit) / BITS_PER_WORD;
12297 cum->words += intregs;
12298 /* words should be unsigned. */
12299 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
12301 int pad = (endbit/BITS_PER_WORD) - cum->words;
12302 cum->words += pad;
12306 /* The darwin64 ABI calls for us to recurse down through structs,
12307 looking for elements passed in registers. Unfortunately, we have
12308 to track int register count here also because of misalignments
12309 in powerpc alignment mode. */
12311 static void
12312 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
12313 const_tree type,
12314 HOST_WIDE_INT startbitpos)
12316 tree f;
12318 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
12319 if (TREE_CODE (f) == FIELD_DECL)
12321 HOST_WIDE_INT bitpos = startbitpos;
12322 tree ftype = TREE_TYPE (f);
12323 machine_mode mode;
12324 if (ftype == error_mark_node)
12325 continue;
12326 mode = TYPE_MODE (ftype);
12328 if (DECL_SIZE (f) != 0
12329 && tree_fits_uhwi_p (bit_position (f)))
12330 bitpos += int_bit_position (f);
12332 /* ??? FIXME: else assume zero offset. */
12334 if (TREE_CODE (ftype) == RECORD_TYPE)
12335 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
12336 else if (USE_FP_FOR_ARG_P (cum, mode))
12338 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
12339 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
12340 cum->fregno += n_fpregs;
12341 /* Single-precision floats present a special problem for
12342 us, because they are smaller than an 8-byte GPR, and so
12343 the structure-packing rules combined with the standard
12344 varargs behavior mean that we want to pack float/float
12345 and float/int combinations into a single register's
12346 space. This is complicated by the arg advance flushing,
12347 which works on arbitrarily large groups of int-type
12348 fields. */
12349 if (mode == SFmode)
12351 if (cum->floats_in_gpr == 1)
12353 /* Two floats in a word; count the word and reset
12354 the float count. */
12355 cum->words++;
12356 cum->floats_in_gpr = 0;
12358 else if (bitpos % 64 == 0)
12360 /* A float at the beginning of an 8-byte word;
12361 count it and put off adjusting cum->words until
12362 we see if a arg advance flush is going to do it
12363 for us. */
12364 cum->floats_in_gpr++;
12366 else
12368 /* The float is at the end of a word, preceded
12369 by integer fields, so the arg advance flush
12370 just above has already set cum->words and
12371 everything is taken care of. */
12374 else
12375 cum->words += n_fpregs;
12377 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
12379 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
12380 cum->vregno++;
12381 cum->words += 2;
12383 else if (cum->intoffset == -1)
12384 cum->intoffset = bitpos;
12388 /* Check for an item that needs to be considered specially under the darwin 64
12389 bit ABI. These are record types where the mode is BLK or the structure is
12390 8 bytes in size. */
12391 static int
12392 rs6000_darwin64_struct_check_p (machine_mode mode, const_tree type)
12394 return rs6000_darwin64_abi
12395 && ((mode == BLKmode
12396 && TREE_CODE (type) == RECORD_TYPE
12397 && int_size_in_bytes (type) > 0)
12398 || (type && TREE_CODE (type) == RECORD_TYPE
12399 && int_size_in_bytes (type) == 8)) ? 1 : 0;
12402 /* Update the data in CUM to advance over an argument
12403 of mode MODE and data type TYPE.
12404 (TYPE is null for libcalls where that information may not be available.)
12406 Note that for args passed by reference, function_arg will be called
12407 with MODE and TYPE set to that of the pointer to the arg, not the arg
12408 itself. */
12410 static void
12411 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, machine_mode mode,
12412 const_tree type, bool named, int depth)
12414 machine_mode elt_mode;
12415 int n_elts;
12417 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
12419 /* Only tick off an argument if we're not recursing. */
12420 if (depth == 0)
12421 cum->nargs_prototype--;
12423 #ifdef HAVE_AS_GNU_ATTRIBUTE
12424 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4)
12425 && cum->escapes)
12427 if (SCALAR_FLOAT_MODE_P (mode))
12429 rs6000_passes_float = true;
12430 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
12431 && (FLOAT128_IBM_P (mode)
12432 || FLOAT128_IEEE_P (mode)
12433 || (type != NULL
12434 && TYPE_MAIN_VARIANT (type) == long_double_type_node)))
12435 rs6000_passes_long_double = true;
12437 if ((named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
12438 || (SPE_VECTOR_MODE (mode)
12439 && !cum->stdarg
12440 && cum->sysv_gregno <= GP_ARG_MAX_REG))
12441 rs6000_passes_vector = true;
12443 #endif
12445 if (TARGET_ALTIVEC_ABI
12446 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
12447 || (type && TREE_CODE (type) == VECTOR_TYPE
12448 && int_size_in_bytes (type) == 16)))
12450 bool stack = false;
12452 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
12454 cum->vregno += n_elts;
12456 if (!TARGET_ALTIVEC)
12457 error ("cannot pass argument in vector register because"
12458 " altivec instructions are disabled, use -maltivec"
12459 " to enable them");
12461 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
12462 even if it is going to be passed in a vector register.
12463 Darwin does the same for variable-argument functions. */
12464 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
12465 && TARGET_64BIT)
12466 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
12467 stack = true;
12469 else
12470 stack = true;
12472 if (stack)
12474 int align;
12476 /* Vector parameters must be 16-byte aligned. In 32-bit
12477 mode this means we need to take into account the offset
12478 to the parameter save area. In 64-bit mode, they just
12479 have to start on an even word, since the parameter save
12480 area is 16-byte aligned. */
12481 if (TARGET_32BIT)
12482 align = -(rs6000_parm_offset () + cum->words) & 3;
12483 else
12484 align = cum->words & 1;
12485 cum->words += align + rs6000_arg_size (mode, type);
12487 if (TARGET_DEBUG_ARG)
12489 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
12490 cum->words, align);
12491 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
12492 cum->nargs_prototype, cum->prototype,
12493 GET_MODE_NAME (mode));
12497 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
12498 && !cum->stdarg
12499 && cum->sysv_gregno <= GP_ARG_MAX_REG)
12500 cum->sysv_gregno++;
12502 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
12504 int size = int_size_in_bytes (type);
12505 /* Variable sized types have size == -1 and are
12506 treated as if consisting entirely of ints.
12507 Pad to 16 byte boundary if needed. */
12508 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
12509 && (cum->words % 2) != 0)
12510 cum->words++;
12511 /* For varargs, we can just go up by the size of the struct. */
12512 if (!named)
12513 cum->words += (size + 7) / 8;
12514 else
12516 /* It is tempting to say int register count just goes up by
12517 sizeof(type)/8, but this is wrong in a case such as
12518 { int; double; int; } [powerpc alignment]. We have to
12519 grovel through the fields for these too. */
12520 cum->intoffset = 0;
12521 cum->floats_in_gpr = 0;
12522 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
12523 rs6000_darwin64_record_arg_advance_flush (cum,
12524 size * BITS_PER_UNIT, 1);
12526 if (TARGET_DEBUG_ARG)
12528 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
12529 cum->words, TYPE_ALIGN (type), size);
12530 fprintf (stderr,
12531 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
12532 cum->nargs_prototype, cum->prototype,
12533 GET_MODE_NAME (mode));
12536 else if (DEFAULT_ABI == ABI_V4)
12538 if (abi_v4_pass_in_fpr (mode))
12540 /* _Decimal128 must use an even/odd register pair. This assumes
12541 that the register number is odd when fregno is odd. */
12542 if (mode == TDmode && (cum->fregno % 2) == 1)
12543 cum->fregno++;
12545 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
12546 <= FP_ARG_V4_MAX_REG)
12547 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
12548 else
12550 cum->fregno = FP_ARG_V4_MAX_REG + 1;
12551 if (mode == DFmode || FLOAT128_IBM_P (mode)
12552 || mode == DDmode || mode == TDmode)
12553 cum->words += cum->words & 1;
12554 cum->words += rs6000_arg_size (mode, type);
12557 else
12559 int n_words = rs6000_arg_size (mode, type);
12560 int gregno = cum->sysv_gregno;
12562 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
12563 (r7,r8) or (r9,r10). As does any other 2 word item such
12564 as complex int due to a historical mistake. */
12565 if (n_words == 2)
12566 gregno += (1 - gregno) & 1;
12568 /* Multi-reg args are not split between registers and stack. */
12569 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
12571 /* Long long and SPE vectors are aligned on the stack.
12572 So are other 2 word items such as complex int due to
12573 a historical mistake. */
12574 if (n_words == 2)
12575 cum->words += cum->words & 1;
12576 cum->words += n_words;
12579 /* Note: continuing to accumulate gregno past when we've started
12580 spilling to the stack indicates the fact that we've started
12581 spilling to the stack to expand_builtin_saveregs. */
12582 cum->sysv_gregno = gregno + n_words;
12585 if (TARGET_DEBUG_ARG)
12587 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
12588 cum->words, cum->fregno);
12589 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
12590 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
12591 fprintf (stderr, "mode = %4s, named = %d\n",
12592 GET_MODE_NAME (mode), named);
12595 else
12597 int n_words = rs6000_arg_size (mode, type);
12598 int start_words = cum->words;
12599 int align_words = rs6000_parm_start (mode, type, start_words);
12601 cum->words = align_words + n_words;
12603 if (SCALAR_FLOAT_MODE_P (elt_mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
12605 /* _Decimal128 must be passed in an even/odd float register pair.
12606 This assumes that the register number is odd when fregno is
12607 odd. */
12608 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
12609 cum->fregno++;
12610 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
12613 if (TARGET_DEBUG_ARG)
12615 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
12616 cum->words, cum->fregno);
12617 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
12618 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
12619 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
12620 named, align_words - start_words, depth);
12625 static void
12626 rs6000_function_arg_advance (cumulative_args_t cum, machine_mode mode,
12627 const_tree type, bool named)
12629 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
12633 static rtx
12634 spe_build_register_parallel (machine_mode mode, int gregno)
12636 rtx r1, r3, r5, r7;
12638 switch (mode)
12640 case E_DFmode:
12641 r1 = gen_rtx_REG (DImode, gregno);
12642 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
12643 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
12645 case E_DCmode:
12646 case E_TFmode:
12647 r1 = gen_rtx_REG (DImode, gregno);
12648 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
12649 r3 = gen_rtx_REG (DImode, gregno + 2);
12650 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
12651 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
12653 case E_TCmode:
12654 r1 = gen_rtx_REG (DImode, gregno);
12655 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
12656 r3 = gen_rtx_REG (DImode, gregno + 2);
12657 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
12658 r5 = gen_rtx_REG (DImode, gregno + 4);
12659 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
12660 r7 = gen_rtx_REG (DImode, gregno + 6);
12661 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
12662 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
12664 default:
12665 gcc_unreachable ();
12669 /* Determine where to put a SIMD argument on the SPE. */
12670 static rtx
12671 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, machine_mode mode,
12672 const_tree type)
12674 int gregno = cum->sysv_gregno;
12676 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
12677 are passed and returned in a pair of GPRs for ABI compatibility. */
12678 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
12679 || mode == DCmode || mode == TCmode))
12681 int n_words = rs6000_arg_size (mode, type);
12683 /* Doubles go in an odd/even register pair (r5/r6, etc). */
12684 if (mode == DFmode)
12685 gregno += (1 - gregno) & 1;
12687 /* Multi-reg args are not split between registers and stack. */
12688 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
12689 return NULL_RTX;
12691 return spe_build_register_parallel (mode, gregno);
12693 if (cum->stdarg)
12695 int n_words = rs6000_arg_size (mode, type);
12697 /* SPE vectors are put in odd registers. */
12698 if (n_words == 2 && (gregno & 1) == 0)
12699 gregno += 1;
12701 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
12703 rtx r1, r2;
12704 machine_mode m = SImode;
12706 r1 = gen_rtx_REG (m, gregno);
12707 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
12708 r2 = gen_rtx_REG (m, gregno + 1);
12709 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
12710 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
12712 else
12713 return NULL_RTX;
12715 else
12717 if (gregno <= GP_ARG_MAX_REG)
12718 return gen_rtx_REG (mode, gregno);
12719 else
12720 return NULL_RTX;
12724 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
12725 structure between cum->intoffset and bitpos to integer registers. */
12727 static void
12728 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
12729 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
12731 machine_mode mode;
12732 unsigned int regno;
12733 unsigned int startbit, endbit;
12734 int this_regno, intregs, intoffset;
12735 rtx reg;
12737 if (cum->intoffset == -1)
12738 return;
12740 intoffset = cum->intoffset;
12741 cum->intoffset = -1;
12743 /* If this is the trailing part of a word, try to only load that
12744 much into the register. Otherwise load the whole register. Note
12745 that in the latter case we may pick up unwanted bits. It's not a
12746 problem at the moment but may wish to revisit. */
12748 if (intoffset % BITS_PER_WORD != 0)
12750 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
12751 if (!int_mode_for_size (bits, 0).exists (&mode))
12753 /* We couldn't find an appropriate mode, which happens,
12754 e.g., in packed structs when there are 3 bytes to load.
12755 Back intoffset back to the beginning of the word in this
12756 case. */
12757 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
12758 mode = word_mode;
12761 else
12762 mode = word_mode;
12764 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
12765 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
12766 intregs = (endbit - startbit) / BITS_PER_WORD;
12767 this_regno = cum->words + intoffset / BITS_PER_WORD;
12769 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
12770 cum->use_stack = 1;
12772 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
12773 if (intregs <= 0)
12774 return;
12776 intoffset /= BITS_PER_UNIT;
12779 regno = GP_ARG_MIN_REG + this_regno;
12780 reg = gen_rtx_REG (mode, regno);
12781 rvec[(*k)++] =
12782 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
12784 this_regno += 1;
12785 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
12786 mode = word_mode;
12787 intregs -= 1;
12789 while (intregs > 0);
12792 /* Recursive workhorse for the following. */
12794 static void
12795 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
12796 HOST_WIDE_INT startbitpos, rtx rvec[],
12797 int *k)
12799 tree f;
12801 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
12802 if (TREE_CODE (f) == FIELD_DECL)
12804 HOST_WIDE_INT bitpos = startbitpos;
12805 tree ftype = TREE_TYPE (f);
12806 machine_mode mode;
12807 if (ftype == error_mark_node)
12808 continue;
12809 mode = TYPE_MODE (ftype);
12811 if (DECL_SIZE (f) != 0
12812 && tree_fits_uhwi_p (bit_position (f)))
12813 bitpos += int_bit_position (f);
12815 /* ??? FIXME: else assume zero offset. */
12817 if (TREE_CODE (ftype) == RECORD_TYPE)
12818 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
12819 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
12821 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
12822 #if 0
12823 switch (mode)
12825 case E_SCmode: mode = SFmode; break;
12826 case E_DCmode: mode = DFmode; break;
12827 case E_TCmode: mode = TFmode; break;
12828 default: break;
12830 #endif
12831 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
12832 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
12834 gcc_assert (cum->fregno == FP_ARG_MAX_REG
12835 && (mode == TFmode || mode == TDmode));
12836 /* Long double or _Decimal128 split over regs and memory. */
12837 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
12838 cum->use_stack=1;
12840 rvec[(*k)++]
12841 = gen_rtx_EXPR_LIST (VOIDmode,
12842 gen_rtx_REG (mode, cum->fregno++),
12843 GEN_INT (bitpos / BITS_PER_UNIT));
12844 if (FLOAT128_2REG_P (mode))
12845 cum->fregno++;
12847 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
12849 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
12850 rvec[(*k)++]
12851 = gen_rtx_EXPR_LIST (VOIDmode,
12852 gen_rtx_REG (mode, cum->vregno++),
12853 GEN_INT (bitpos / BITS_PER_UNIT));
12855 else if (cum->intoffset == -1)
12856 cum->intoffset = bitpos;
12860 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
12861 the register(s) to be used for each field and subfield of a struct
12862 being passed by value, along with the offset of where the
12863 register's value may be found in the block. FP fields go in FP
12864 register, vector fields go in vector registers, and everything
12865 else goes in int registers, packed as in memory.
12867 This code is also used for function return values. RETVAL indicates
12868 whether this is the case.
12870 Much of this is taken from the SPARC V9 port, which has a similar
12871 calling convention. */
12873 static rtx
12874 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
12875 bool named, bool retval)
12877 rtx rvec[FIRST_PSEUDO_REGISTER];
12878 int k = 1, kbase = 1;
12879 HOST_WIDE_INT typesize = int_size_in_bytes (type);
12880 /* This is a copy; modifications are not visible to our caller. */
12881 CUMULATIVE_ARGS copy_cum = *orig_cum;
12882 CUMULATIVE_ARGS *cum = &copy_cum;
12884 /* Pad to 16 byte boundary if needed. */
12885 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
12886 && (cum->words % 2) != 0)
12887 cum->words++;
12889 cum->intoffset = 0;
12890 cum->use_stack = 0;
12891 cum->named = named;
12893 /* Put entries into rvec[] for individual FP and vector fields, and
12894 for the chunks of memory that go in int regs. Note we start at
12895 element 1; 0 is reserved for an indication of using memory, and
12896 may or may not be filled in below. */
12897 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
12898 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
12900 /* If any part of the struct went on the stack put all of it there.
12901 This hack is because the generic code for
12902 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
12903 parts of the struct are not at the beginning. */
12904 if (cum->use_stack)
12906 if (retval)
12907 return NULL_RTX; /* doesn't go in registers at all */
12908 kbase = 0;
12909 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12911 if (k > 1 || cum->use_stack)
12912 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
12913 else
12914 return NULL_RTX;
12917 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
12919 static rtx
12920 rs6000_mixed_function_arg (machine_mode mode, const_tree type,
12921 int align_words)
12923 int n_units;
12924 int i, k;
12925 rtx rvec[GP_ARG_NUM_REG + 1];
12927 if (align_words >= GP_ARG_NUM_REG)
12928 return NULL_RTX;
12930 n_units = rs6000_arg_size (mode, type);
12932 /* Optimize the simple case where the arg fits in one gpr, except in
12933 the case of BLKmode due to assign_parms assuming that registers are
12934 BITS_PER_WORD wide. */
12935 if (n_units == 0
12936 || (n_units == 1 && mode != BLKmode))
12937 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
12939 k = 0;
12940 if (align_words + n_units > GP_ARG_NUM_REG)
12941 /* Not all of the arg fits in gprs. Say that it goes in memory too,
12942 using a magic NULL_RTX component.
12943 This is not strictly correct. Only some of the arg belongs in
12944 memory, not all of it. However, the normal scheme using
12945 function_arg_partial_nregs can result in unusual subregs, eg.
12946 (subreg:SI (reg:DF) 4), which are not handled well. The code to
12947 store the whole arg to memory is often more efficient than code
12948 to store pieces, and we know that space is available in the right
12949 place for the whole arg. */
12950 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12952 i = 0;
12955 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
12956 rtx off = GEN_INT (i++ * 4);
12957 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12959 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
12961 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
12964 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
12965 but must also be copied into the parameter save area starting at
12966 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
12967 to the GPRs and/or memory. Return the number of elements used. */
12969 static int
12970 rs6000_psave_function_arg (machine_mode mode, const_tree type,
12971 int align_words, rtx *rvec)
12973 int k = 0;
12975 if (align_words < GP_ARG_NUM_REG)
12977 int n_words = rs6000_arg_size (mode, type);
12979 if (align_words + n_words > GP_ARG_NUM_REG
12980 || mode == BLKmode
12981 || (TARGET_32BIT && TARGET_POWERPC64))
12983 /* If this is partially on the stack, then we only
12984 include the portion actually in registers here. */
12985 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
12986 int i = 0;
12988 if (align_words + n_words > GP_ARG_NUM_REG)
12990 /* Not all of the arg fits in gprs. Say that it goes in memory
12991 too, using a magic NULL_RTX component. Also see comment in
12992 rs6000_mixed_function_arg for why the normal
12993 function_arg_partial_nregs scheme doesn't work in this case. */
12994 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12999 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
13000 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
13001 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13003 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
13005 else
13007 /* The whole arg fits in gprs. */
13008 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
13009 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
13012 else
13014 /* It's entirely in memory. */
13015 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
13018 return k;
13021 /* RVEC is a vector of K components of an argument of mode MODE.
13022 Construct the final function_arg return value from it. */
13024 static rtx
13025 rs6000_finish_function_arg (machine_mode mode, rtx *rvec, int k)
13027 gcc_assert (k >= 1);
13029 /* Avoid returning a PARALLEL in the trivial cases. */
13030 if (k == 1)
13032 if (XEXP (rvec[0], 0) == NULL_RTX)
13033 return NULL_RTX;
13035 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
13036 return XEXP (rvec[0], 0);
13039 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
13042 /* Determine where to put an argument to a function.
13043 Value is zero to push the argument on the stack,
13044 or a hard register in which to store the argument.
13046 MODE is the argument's machine mode.
13047 TYPE is the data type of the argument (as a tree).
13048 This is null for libcalls where that information may
13049 not be available.
13050 CUM is a variable of type CUMULATIVE_ARGS which gives info about
13051 the preceding args and about the function being called. It is
13052 not modified in this routine.
13053 NAMED is nonzero if this argument is a named parameter
13054 (otherwise it is an extra parameter matching an ellipsis).
13056 On RS/6000 the first eight words of non-FP are normally in registers
13057 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
13058 Under V.4, the first 8 FP args are in registers.
13060 If this is floating-point and no prototype is specified, we use
13061 both an FP and integer register (or possibly FP reg and stack). Library
13062 functions (when CALL_LIBCALL is set) always have the proper types for args,
13063 so we can pass the FP value just in one register. emit_library_function
13064 doesn't support PARALLEL anyway.
13066 Note that for args passed by reference, function_arg will be called
13067 with MODE and TYPE set to that of the pointer to the arg, not the arg
13068 itself. */
13070 static rtx
13071 rs6000_function_arg (cumulative_args_t cum_v, machine_mode mode,
13072 const_tree type, bool named)
13074 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
13075 enum rs6000_abi abi = DEFAULT_ABI;
13076 machine_mode elt_mode;
13077 int n_elts;
13079 /* Return a marker to indicate whether CR1 needs to set or clear the
13080 bit that V.4 uses to say fp args were passed in registers.
13081 Assume that we don't need the marker for software floating point,
13082 or compiler generated library calls. */
13083 if (mode == VOIDmode)
13085 if (abi == ABI_V4
13086 && (cum->call_cookie & CALL_LIBCALL) == 0
13087 && (cum->stdarg
13088 || (cum->nargs_prototype < 0
13089 && (cum->prototype || TARGET_NO_PROTOTYPE))))
13091 /* For the SPE, we need to crxor CR6 always. */
13092 if (TARGET_SPE_ABI)
13093 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
13094 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
13095 return GEN_INT (cum->call_cookie
13096 | ((cum->fregno == FP_ARG_MIN_REG)
13097 ? CALL_V4_SET_FP_ARGS
13098 : CALL_V4_CLEAR_FP_ARGS));
13101 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
13104 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
13106 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
13108 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
13109 if (rslt != NULL_RTX)
13110 return rslt;
13111 /* Else fall through to usual handling. */
13114 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
13116 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
13117 rtx r, off;
13118 int i, k = 0;
13120 /* Do we also need to pass this argument in the parameter save area?
13121 Library support functions for IEEE 128-bit are assumed to not need the
13122 value passed both in GPRs and in vector registers. */
13123 if (TARGET_64BIT && !cum->prototype
13124 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
13126 int align_words = ROUND_UP (cum->words, 2);
13127 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
13130 /* Describe where this argument goes in the vector registers. */
13131 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
13133 r = gen_rtx_REG (elt_mode, cum->vregno + i);
13134 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
13135 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13138 return rs6000_finish_function_arg (mode, rvec, k);
13140 else if (TARGET_ALTIVEC_ABI
13141 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
13142 || (type && TREE_CODE (type) == VECTOR_TYPE
13143 && int_size_in_bytes (type) == 16)))
13145 if (named || abi == ABI_V4)
13146 return NULL_RTX;
13147 else
13149 /* Vector parameters to varargs functions under AIX or Darwin
13150 get passed in memory and possibly also in GPRs. */
13151 int align, align_words, n_words;
13152 machine_mode part_mode;
13154 /* Vector parameters must be 16-byte aligned. In 32-bit
13155 mode this means we need to take into account the offset
13156 to the parameter save area. In 64-bit mode, they just
13157 have to start on an even word, since the parameter save
13158 area is 16-byte aligned. */
13159 if (TARGET_32BIT)
13160 align = -(rs6000_parm_offset () + cum->words) & 3;
13161 else
13162 align = cum->words & 1;
13163 align_words = cum->words + align;
13165 /* Out of registers? Memory, then. */
13166 if (align_words >= GP_ARG_NUM_REG)
13167 return NULL_RTX;
13169 if (TARGET_32BIT && TARGET_POWERPC64)
13170 return rs6000_mixed_function_arg (mode, type, align_words);
13172 /* The vector value goes in GPRs. Only the part of the
13173 value in GPRs is reported here. */
13174 part_mode = mode;
13175 n_words = rs6000_arg_size (mode, type);
13176 if (align_words + n_words > GP_ARG_NUM_REG)
13177 /* Fortunately, there are only two possibilities, the value
13178 is either wholly in GPRs or half in GPRs and half not. */
13179 part_mode = DImode;
13181 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
13184 else if (TARGET_SPE_ABI && TARGET_SPE
13185 && (SPE_VECTOR_MODE (mode)
13186 || (TARGET_E500_DOUBLE && (mode == DFmode
13187 || mode == DCmode
13188 || mode == TFmode
13189 || mode == TCmode))))
13190 return rs6000_spe_function_arg (cum, mode, type);
13192 else if (abi == ABI_V4)
13194 if (abi_v4_pass_in_fpr (mode))
13196 /* _Decimal128 must use an even/odd register pair. This assumes
13197 that the register number is odd when fregno is odd. */
13198 if (mode == TDmode && (cum->fregno % 2) == 1)
13199 cum->fregno++;
13201 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
13202 <= FP_ARG_V4_MAX_REG)
13203 return gen_rtx_REG (mode, cum->fregno);
13204 else
13205 return NULL_RTX;
13207 else
13209 int n_words = rs6000_arg_size (mode, type);
13210 int gregno = cum->sysv_gregno;
13212 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
13213 (r7,r8) or (r9,r10). As does any other 2 word item such
13214 as complex int due to a historical mistake. */
13215 if (n_words == 2)
13216 gregno += (1 - gregno) & 1;
13218 /* Multi-reg args are not split between registers and stack. */
13219 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
13220 return NULL_RTX;
13222 if (TARGET_32BIT && TARGET_POWERPC64)
13223 return rs6000_mixed_function_arg (mode, type,
13224 gregno - GP_ARG_MIN_REG);
13225 return gen_rtx_REG (mode, gregno);
13228 else
13230 int align_words = rs6000_parm_start (mode, type, cum->words);
13232 /* _Decimal128 must be passed in an even/odd float register pair.
13233 This assumes that the register number is odd when fregno is odd. */
13234 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
13235 cum->fregno++;
13237 if (USE_FP_FOR_ARG_P (cum, elt_mode))
13239 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
13240 rtx r, off;
13241 int i, k = 0;
13242 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
13243 int fpr_words;
13245 /* Do we also need to pass this argument in the parameter
13246 save area? */
13247 if (type && (cum->nargs_prototype <= 0
13248 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
13249 && TARGET_XL_COMPAT
13250 && align_words >= GP_ARG_NUM_REG)))
13251 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
13253 /* Describe where this argument goes in the fprs. */
13254 for (i = 0; i < n_elts
13255 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
13257 /* Check if the argument is split over registers and memory.
13258 This can only ever happen for long double or _Decimal128;
13259 complex types are handled via split_complex_arg. */
13260 machine_mode fmode = elt_mode;
13261 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
13263 gcc_assert (FLOAT128_2REG_P (fmode));
13264 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
13267 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
13268 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
13269 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13272 /* If there were not enough FPRs to hold the argument, the rest
13273 usually goes into memory. However, if the current position
13274 is still within the register parameter area, a portion may
13275 actually have to go into GPRs.
13277 Note that it may happen that the portion of the argument
13278 passed in the first "half" of the first GPR was already
13279 passed in the last FPR as well.
13281 For unnamed arguments, we already set up GPRs to cover the
13282 whole argument in rs6000_psave_function_arg, so there is
13283 nothing further to do at this point. */
13284 fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
13285 if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
13286 && cum->nargs_prototype > 0)
13288 static bool warned;
13290 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
13291 int n_words = rs6000_arg_size (mode, type);
13293 align_words += fpr_words;
13294 n_words -= fpr_words;
13298 r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
13299 off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
13300 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13302 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
13304 if (!warned && warn_psabi)
13306 warned = true;
13307 inform (input_location,
13308 "the ABI of passing homogeneous float aggregates"
13309 " has changed in GCC 5");
13313 return rs6000_finish_function_arg (mode, rvec, k);
13315 else if (align_words < GP_ARG_NUM_REG)
13317 if (TARGET_32BIT && TARGET_POWERPC64)
13318 return rs6000_mixed_function_arg (mode, type, align_words);
13320 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
13322 else
13323 return NULL_RTX;
13327 /* For an arg passed partly in registers and partly in memory, this is
13328 the number of bytes passed in registers. For args passed entirely in
13329 registers or entirely in memory, zero. When an arg is described by a
13330 PARALLEL, perhaps using more than one register type, this function
13331 returns the number of bytes used by the first element of the PARALLEL. */
13333 static int
13334 rs6000_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
13335 tree type, bool named)
13337 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
13338 bool passed_in_gprs = true;
13339 int ret = 0;
13340 int align_words;
13341 machine_mode elt_mode;
13342 int n_elts;
13344 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
13346 if (DEFAULT_ABI == ABI_V4)
13347 return 0;
13349 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
13351 /* If we are passing this arg in the fixed parameter save area (gprs or
13352 memory) as well as VRs, we do not use the partial bytes mechanism;
13353 instead, rs6000_function_arg will return a PARALLEL including a memory
13354 element as necessary. Library support functions for IEEE 128-bit are
13355 assumed to not need the value passed both in GPRs and in vector
13356 registers. */
13357 if (TARGET_64BIT && !cum->prototype
13358 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
13359 return 0;
13361 /* Otherwise, we pass in VRs only. Check for partial copies. */
13362 passed_in_gprs = false;
13363 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
13364 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
13367 /* In this complicated case we just disable the partial_nregs code. */
13368 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
13369 return 0;
13371 align_words = rs6000_parm_start (mode, type, cum->words);
13373 if (USE_FP_FOR_ARG_P (cum, elt_mode))
13375 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
13377 /* If we are passing this arg in the fixed parameter save area
13378 (gprs or memory) as well as FPRs, we do not use the partial
13379 bytes mechanism; instead, rs6000_function_arg will return a
13380 PARALLEL including a memory element as necessary. */
13381 if (type
13382 && (cum->nargs_prototype <= 0
13383 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
13384 && TARGET_XL_COMPAT
13385 && align_words >= GP_ARG_NUM_REG)))
13386 return 0;
13388 /* Otherwise, we pass in FPRs only. Check for partial copies. */
13389 passed_in_gprs = false;
13390 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
13392 /* Compute number of bytes / words passed in FPRs. If there
13393 is still space available in the register parameter area
13394 *after* that amount, a part of the argument will be passed
13395 in GPRs. In that case, the total amount passed in any
13396 registers is equal to the amount that would have been passed
13397 in GPRs if everything were passed there, so we fall back to
13398 the GPR code below to compute the appropriate value. */
13399 int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
13400 * MIN (8, GET_MODE_SIZE (elt_mode)));
13401 int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
13403 if (align_words + fpr_words < GP_ARG_NUM_REG)
13404 passed_in_gprs = true;
13405 else
13406 ret = fpr;
13410 if (passed_in_gprs
13411 && align_words < GP_ARG_NUM_REG
13412 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
13413 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
13415 if (ret != 0 && TARGET_DEBUG_ARG)
13416 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
13418 return ret;
13421 /* A C expression that indicates when an argument must be passed by
13422 reference. If nonzero for an argument, a copy of that argument is
13423 made in memory and a pointer to the argument is passed instead of
13424 the argument itself. The pointer is passed in whatever way is
13425 appropriate for passing a pointer to that type.
13427 Under V.4, aggregates and long double are passed by reference.
13429 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
13430 reference unless the AltiVec vector extension ABI is in force.
13432 As an extension to all ABIs, variable sized types are passed by
13433 reference. */
13435 static bool
13436 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
13437 machine_mode mode, const_tree type,
13438 bool named ATTRIBUTE_UNUSED)
13440 if (!type)
13441 return 0;
13443 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
13444 && FLOAT128_IEEE_P (TYPE_MODE (type)))
13446 if (TARGET_DEBUG_ARG)
13447 fprintf (stderr, "function_arg_pass_by_reference: V4 IEEE 128-bit\n");
13448 return 1;
13451 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
13453 if (TARGET_DEBUG_ARG)
13454 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
13455 return 1;
13458 if (int_size_in_bytes (type) < 0)
13460 if (TARGET_DEBUG_ARG)
13461 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
13462 return 1;
13465 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
13466 modes only exist for GCC vector types if -maltivec. */
13467 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
13469 if (TARGET_DEBUG_ARG)
13470 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
13471 return 1;
13474 /* Pass synthetic vectors in memory. */
13475 if (TREE_CODE (type) == VECTOR_TYPE
13476 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
13478 static bool warned_for_pass_big_vectors = false;
13479 if (TARGET_DEBUG_ARG)
13480 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
13481 if (!warned_for_pass_big_vectors)
13483 warning (OPT_Wpsabi, "GCC vector passed by reference: "
13484 "non-standard ABI extension with no compatibility guarantee");
13485 warned_for_pass_big_vectors = true;
13487 return 1;
13490 return 0;
13493 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
13494 already processes. Return true if the parameter must be passed
13495 (fully or partially) on the stack. */
13497 static bool
13498 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
13500 machine_mode mode;
13501 int unsignedp;
13502 rtx entry_parm;
13504 /* Catch errors. */
13505 if (type == NULL || type == error_mark_node)
13506 return true;
13508 /* Handle types with no storage requirement. */
13509 if (TYPE_MODE (type) == VOIDmode)
13510 return false;
13512 /* Handle complex types. */
13513 if (TREE_CODE (type) == COMPLEX_TYPE)
13514 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
13515 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
13517 /* Handle transparent aggregates. */
13518 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
13519 && TYPE_TRANSPARENT_AGGR (type))
13520 type = TREE_TYPE (first_field (type));
13522 /* See if this arg was passed by invisible reference. */
13523 if (pass_by_reference (get_cumulative_args (args_so_far),
13524 TYPE_MODE (type), type, true))
13525 type = build_pointer_type (type);
13527 /* Find mode as it is passed by the ABI. */
13528 unsignedp = TYPE_UNSIGNED (type);
13529 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
13531 /* If we must pass in stack, we need a stack. */
13532 if (rs6000_must_pass_in_stack (mode, type))
13533 return true;
13535 /* If there is no incoming register, we need a stack. */
13536 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
13537 if (entry_parm == NULL)
13538 return true;
13540 /* Likewise if we need to pass both in registers and on the stack. */
13541 if (GET_CODE (entry_parm) == PARALLEL
13542 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
13543 return true;
13545 /* Also true if we're partially in registers and partially not. */
13546 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
13547 return true;
13549 /* Update info on where next arg arrives in registers. */
13550 rs6000_function_arg_advance (args_so_far, mode, type, true);
13551 return false;
13554 /* Return true if FUN has no prototype, has a variable argument
13555 list, or passes any parameter in memory. */
13557 static bool
13558 rs6000_function_parms_need_stack (tree fun, bool incoming)
13560 tree fntype, result;
13561 CUMULATIVE_ARGS args_so_far_v;
13562 cumulative_args_t args_so_far;
13564 if (!fun)
13565 /* Must be a libcall, all of which only use reg parms. */
13566 return false;
13568 fntype = fun;
13569 if (!TYPE_P (fun))
13570 fntype = TREE_TYPE (fun);
13572 /* Varargs functions need the parameter save area. */
13573 if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
13574 return true;
13576 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
13577 args_so_far = pack_cumulative_args (&args_so_far_v);
13579 /* When incoming, we will have been passed the function decl.
13580 It is necessary to use the decl to handle K&R style functions,
13581 where TYPE_ARG_TYPES may not be available. */
13582 if (incoming)
13584 gcc_assert (DECL_P (fun));
13585 result = DECL_RESULT (fun);
13587 else
13588 result = TREE_TYPE (fntype);
13590 if (result && aggregate_value_p (result, fntype))
13592 if (!TYPE_P (result))
13593 result = TREE_TYPE (result);
13594 result = build_pointer_type (result);
13595 rs6000_parm_needs_stack (args_so_far, result);
13598 if (incoming)
13600 tree parm;
13602 for (parm = DECL_ARGUMENTS (fun);
13603 parm && parm != void_list_node;
13604 parm = TREE_CHAIN (parm))
13605 if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
13606 return true;
13608 else
13610 function_args_iterator args_iter;
13611 tree arg_type;
13613 FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
13614 if (rs6000_parm_needs_stack (args_so_far, arg_type))
13615 return true;
13618 return false;
13621 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
13622 usually a constant depending on the ABI. However, in the ELFv2 ABI
13623 the register parameter area is optional when calling a function that
13624 has a prototype is scope, has no variable argument list, and passes
13625 all parameters in registers. */
13628 rs6000_reg_parm_stack_space (tree fun, bool incoming)
13630 int reg_parm_stack_space;
13632 switch (DEFAULT_ABI)
13634 default:
13635 reg_parm_stack_space = 0;
13636 break;
13638 case ABI_AIX:
13639 case ABI_DARWIN:
13640 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
13641 break;
13643 case ABI_ELFv2:
13644 /* ??? Recomputing this every time is a bit expensive. Is there
13645 a place to cache this information? */
13646 if (rs6000_function_parms_need_stack (fun, incoming))
13647 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
13648 else
13649 reg_parm_stack_space = 0;
13650 break;
13653 return reg_parm_stack_space;
13656 static void
13657 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
13659 int i;
13660 machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
13662 if (nregs == 0)
13663 return;
13665 for (i = 0; i < nregs; i++)
13667 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
13668 if (reload_completed)
13670 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
13671 tem = NULL_RTX;
13672 else
13673 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
13674 i * GET_MODE_SIZE (reg_mode));
13676 else
13677 tem = replace_equiv_address (tem, XEXP (tem, 0));
13679 gcc_assert (tem);
13681 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
13685 /* Perform any needed actions needed for a function that is receiving a
13686 variable number of arguments.
13688 CUM is as above.
13690 MODE and TYPE are the mode and type of the current parameter.
13692 PRETEND_SIZE is a variable that should be set to the amount of stack
13693 that must be pushed by the prolog to pretend that our caller pushed
13696 Normally, this macro will push all remaining incoming registers on the
13697 stack and set PRETEND_SIZE to the length of the registers pushed. */
13699 static void
13700 setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
13701 tree type, int *pretend_size ATTRIBUTE_UNUSED,
13702 int no_rtl)
13704 CUMULATIVE_ARGS next_cum;
13705 int reg_size = TARGET_32BIT ? 4 : 8;
13706 rtx save_area = NULL_RTX, mem;
13707 int first_reg_offset;
13708 alias_set_type set;
13710 /* Skip the last named argument. */
13711 next_cum = *get_cumulative_args (cum);
13712 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
13714 if (DEFAULT_ABI == ABI_V4)
13716 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
13718 if (! no_rtl)
13720 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
13721 HOST_WIDE_INT offset = 0;
13723 /* Try to optimize the size of the varargs save area.
13724 The ABI requires that ap.reg_save_area is doubleword
13725 aligned, but we don't need to allocate space for all
13726 the bytes, only those to which we actually will save
13727 anything. */
13728 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
13729 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
13730 if (TARGET_HARD_FLOAT && TARGET_FPRS
13731 && next_cum.fregno <= FP_ARG_V4_MAX_REG
13732 && cfun->va_list_fpr_size)
13734 if (gpr_reg_num)
13735 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
13736 * UNITS_PER_FP_WORD;
13737 if (cfun->va_list_fpr_size
13738 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
13739 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
13740 else
13741 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
13742 * UNITS_PER_FP_WORD;
13744 if (gpr_reg_num)
13746 offset = -((first_reg_offset * reg_size) & ~7);
13747 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
13749 gpr_reg_num = cfun->va_list_gpr_size;
13750 if (reg_size == 4 && (first_reg_offset & 1))
13751 gpr_reg_num++;
13753 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
13755 else if (fpr_size)
13756 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
13757 * UNITS_PER_FP_WORD
13758 - (int) (GP_ARG_NUM_REG * reg_size);
13760 if (gpr_size + fpr_size)
13762 rtx reg_save_area
13763 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
13764 gcc_assert (GET_CODE (reg_save_area) == MEM);
13765 reg_save_area = XEXP (reg_save_area, 0);
13766 if (GET_CODE (reg_save_area) == PLUS)
13768 gcc_assert (XEXP (reg_save_area, 0)
13769 == virtual_stack_vars_rtx);
13770 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
13771 offset += INTVAL (XEXP (reg_save_area, 1));
13773 else
13774 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
13777 cfun->machine->varargs_save_offset = offset;
13778 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
13781 else
13783 first_reg_offset = next_cum.words;
13784 save_area = crtl->args.internal_arg_pointer;
13786 if (targetm.calls.must_pass_in_stack (mode, type))
13787 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
13790 set = get_varargs_alias_set ();
13791 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
13792 && cfun->va_list_gpr_size)
13794 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
13796 if (va_list_gpr_counter_field)
13797 /* V4 va_list_gpr_size counts number of registers needed. */
13798 n_gpr = cfun->va_list_gpr_size;
13799 else
13800 /* char * va_list instead counts number of bytes needed. */
13801 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
13803 if (nregs > n_gpr)
13804 nregs = n_gpr;
13806 mem = gen_rtx_MEM (BLKmode,
13807 plus_constant (Pmode, save_area,
13808 first_reg_offset * reg_size));
13809 MEM_NOTRAP_P (mem) = 1;
13810 set_mem_alias_set (mem, set);
13811 set_mem_align (mem, BITS_PER_WORD);
13813 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
13814 nregs);
13817 /* Save FP registers if needed. */
13818 if (DEFAULT_ABI == ABI_V4
13819 && TARGET_HARD_FLOAT && TARGET_FPRS
13820 && ! no_rtl
13821 && next_cum.fregno <= FP_ARG_V4_MAX_REG
13822 && cfun->va_list_fpr_size)
13824 int fregno = next_cum.fregno, nregs;
13825 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
13826 rtx lab = gen_label_rtx ();
13827 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
13828 * UNITS_PER_FP_WORD);
13830 emit_jump_insn
13831 (gen_rtx_SET (pc_rtx,
13832 gen_rtx_IF_THEN_ELSE (VOIDmode,
13833 gen_rtx_NE (VOIDmode, cr1,
13834 const0_rtx),
13835 gen_rtx_LABEL_REF (VOIDmode, lab),
13836 pc_rtx)));
13838 for (nregs = 0;
13839 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
13840 fregno++, off += UNITS_PER_FP_WORD, nregs++)
13842 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13843 ? DFmode : SFmode,
13844 plus_constant (Pmode, save_area, off));
13845 MEM_NOTRAP_P (mem) = 1;
13846 set_mem_alias_set (mem, set);
13847 set_mem_align (mem, GET_MODE_ALIGNMENT (
13848 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13849 ? DFmode : SFmode));
13850 emit_move_insn (mem, gen_rtx_REG (
13851 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13852 ? DFmode : SFmode, fregno));
13855 emit_label (lab);
13859 /* Create the va_list data type. */
13861 static tree
13862 rs6000_build_builtin_va_list (void)
13864 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
13866 /* For AIX, prefer 'char *' because that's what the system
13867 header files like. */
13868 if (DEFAULT_ABI != ABI_V4)
13869 return build_pointer_type (char_type_node);
13871 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
13872 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
13873 get_identifier ("__va_list_tag"), record);
13875 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
13876 unsigned_char_type_node);
13877 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
13878 unsigned_char_type_node);
13879 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
13880 every user file. */
13881 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13882 get_identifier ("reserved"), short_unsigned_type_node);
13883 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13884 get_identifier ("overflow_arg_area"),
13885 ptr_type_node);
13886 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13887 get_identifier ("reg_save_area"),
13888 ptr_type_node);
13890 va_list_gpr_counter_field = f_gpr;
13891 va_list_fpr_counter_field = f_fpr;
13893 DECL_FIELD_CONTEXT (f_gpr) = record;
13894 DECL_FIELD_CONTEXT (f_fpr) = record;
13895 DECL_FIELD_CONTEXT (f_res) = record;
13896 DECL_FIELD_CONTEXT (f_ovf) = record;
13897 DECL_FIELD_CONTEXT (f_sav) = record;
13899 TYPE_STUB_DECL (record) = type_decl;
13900 TYPE_NAME (record) = type_decl;
13901 TYPE_FIELDS (record) = f_gpr;
13902 DECL_CHAIN (f_gpr) = f_fpr;
13903 DECL_CHAIN (f_fpr) = f_res;
13904 DECL_CHAIN (f_res) = f_ovf;
13905 DECL_CHAIN (f_ovf) = f_sav;
13907 layout_type (record);
13909 /* The correct type is an array type of one element. */
13910 return build_array_type (record, build_index_type (size_zero_node));
13913 /* Implement va_start. */
13915 static void
13916 rs6000_va_start (tree valist, rtx nextarg)
13918 HOST_WIDE_INT words, n_gpr, n_fpr;
13919 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
13920 tree gpr, fpr, ovf, sav, t;
13922 /* Only SVR4 needs something special. */
13923 if (DEFAULT_ABI != ABI_V4)
13925 std_expand_builtin_va_start (valist, nextarg);
13926 return;
13929 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
13930 f_fpr = DECL_CHAIN (f_gpr);
13931 f_res = DECL_CHAIN (f_fpr);
13932 f_ovf = DECL_CHAIN (f_res);
13933 f_sav = DECL_CHAIN (f_ovf);
13935 valist = build_simple_mem_ref (valist);
13936 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
13937 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
13938 f_fpr, NULL_TREE);
13939 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
13940 f_ovf, NULL_TREE);
13941 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
13942 f_sav, NULL_TREE);
13944 /* Count number of gp and fp argument registers used. */
13945 words = crtl->args.info.words;
13946 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
13947 GP_ARG_NUM_REG);
13948 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
13949 FP_ARG_NUM_REG);
13951 if (TARGET_DEBUG_ARG)
13952 fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
13953 HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
13954 words, n_gpr, n_fpr);
13956 if (cfun->va_list_gpr_size)
13958 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
13959 build_int_cst (NULL_TREE, n_gpr));
13960 TREE_SIDE_EFFECTS (t) = 1;
13961 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13964 if (cfun->va_list_fpr_size)
13966 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
13967 build_int_cst (NULL_TREE, n_fpr));
13968 TREE_SIDE_EFFECTS (t) = 1;
13969 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13971 #ifdef HAVE_AS_GNU_ATTRIBUTE
13972 if (call_ABI_of_interest (cfun->decl))
13973 rs6000_passes_float = true;
13974 #endif
13977 /* Find the overflow area. */
13978 t = make_tree (TREE_TYPE (ovf), crtl->args.internal_arg_pointer);
13979 if (words != 0)
13980 t = fold_build_pointer_plus_hwi (t, words * MIN_UNITS_PER_WORD);
13981 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
13982 TREE_SIDE_EFFECTS (t) = 1;
13983 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13985 /* If there were no va_arg invocations, don't set up the register
13986 save area. */
13987 if (!cfun->va_list_gpr_size
13988 && !cfun->va_list_fpr_size
13989 && n_gpr < GP_ARG_NUM_REG
13990 && n_fpr < FP_ARG_V4_MAX_REG)
13991 return;
13993 /* Find the register save area. */
13994 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
13995 if (cfun->machine->varargs_save_offset)
13996 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
13997 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
13998 TREE_SIDE_EFFECTS (t) = 1;
13999 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
14002 /* Implement va_arg. */
14004 static tree
14005 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
14006 gimple_seq *post_p)
14008 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
14009 tree gpr, fpr, ovf, sav, reg, t, u;
14010 int size, rsize, n_reg, sav_ofs, sav_scale;
14011 tree lab_false, lab_over, addr;
14012 int align;
14013 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
14014 int regalign = 0;
14015 gimple *stmt;
14017 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
14019 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
14020 return build_va_arg_indirect_ref (t);
14023 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
14024 earlier version of gcc, with the property that it always applied alignment
14025 adjustments to the va-args (even for zero-sized types). The cheapest way
14026 to deal with this is to replicate the effect of the part of
14027 std_gimplify_va_arg_expr that carries out the align adjust, for the case
14028 of relevance.
14029 We don't need to check for pass-by-reference because of the test above.
14030 We can return a simplifed answer, since we know there's no offset to add. */
14032 if (((TARGET_MACHO
14033 && rs6000_darwin64_abi)
14034 || DEFAULT_ABI == ABI_ELFv2
14035 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
14036 && integer_zerop (TYPE_SIZE (type)))
14038 unsigned HOST_WIDE_INT align, boundary;
14039 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
14040 align = PARM_BOUNDARY / BITS_PER_UNIT;
14041 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
14042 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
14043 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
14044 boundary /= BITS_PER_UNIT;
14045 if (boundary > align)
14047 tree t ;
14048 /* This updates arg ptr by the amount that would be necessary
14049 to align the zero-sized (but not zero-alignment) item. */
14050 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
14051 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
14052 gimplify_and_add (t, pre_p);
14054 t = fold_convert (sizetype, valist_tmp);
14055 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
14056 fold_convert (TREE_TYPE (valist),
14057 fold_build2 (BIT_AND_EXPR, sizetype, t,
14058 size_int (-boundary))));
14059 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
14060 gimplify_and_add (t, pre_p);
14062 /* Since it is zero-sized there's no increment for the item itself. */
14063 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
14064 return build_va_arg_indirect_ref (valist_tmp);
14067 if (DEFAULT_ABI != ABI_V4)
14069 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
14071 tree elem_type = TREE_TYPE (type);
14072 machine_mode elem_mode = TYPE_MODE (elem_type);
14073 int elem_size = GET_MODE_SIZE (elem_mode);
14075 if (elem_size < UNITS_PER_WORD)
14077 tree real_part, imag_part;
14078 gimple_seq post = NULL;
14080 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
14081 &post);
14082 /* Copy the value into a temporary, lest the formal temporary
14083 be reused out from under us. */
14084 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
14085 gimple_seq_add_seq (pre_p, post);
14087 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
14088 post_p);
14090 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
14094 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
14097 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
14098 f_fpr = DECL_CHAIN (f_gpr);
14099 f_res = DECL_CHAIN (f_fpr);
14100 f_ovf = DECL_CHAIN (f_res);
14101 f_sav = DECL_CHAIN (f_ovf);
14103 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
14104 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
14105 f_fpr, NULL_TREE);
14106 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
14107 f_ovf, NULL_TREE);
14108 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
14109 f_sav, NULL_TREE);
14111 size = int_size_in_bytes (type);
14112 rsize = (size + 3) / 4;
14113 int pad = 4 * rsize - size;
14114 align = 1;
14116 machine_mode mode = TYPE_MODE (type);
14117 if (abi_v4_pass_in_fpr (mode))
14119 /* FP args go in FP registers, if present. */
14120 reg = fpr;
14121 n_reg = (size + 7) / 8;
14122 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
14123 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
14124 if (mode != SFmode && mode != SDmode)
14125 align = 8;
14127 else
14129 /* Otherwise into GP registers. */
14130 reg = gpr;
14131 n_reg = rsize;
14132 sav_ofs = 0;
14133 sav_scale = 4;
14134 if (n_reg == 2)
14135 align = 8;
14138 /* Pull the value out of the saved registers.... */
14140 lab_over = NULL;
14141 addr = create_tmp_var (ptr_type_node, "addr");
14143 /* AltiVec vectors never go in registers when -mabi=altivec. */
14144 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
14145 align = 16;
14146 else
14148 lab_false = create_artificial_label (input_location);
14149 lab_over = create_artificial_label (input_location);
14151 /* Long long and SPE vectors are aligned in the registers.
14152 As are any other 2 gpr item such as complex int due to a
14153 historical mistake. */
14154 u = reg;
14155 if (n_reg == 2 && reg == gpr)
14157 regalign = 1;
14158 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
14159 build_int_cst (TREE_TYPE (reg), n_reg - 1));
14160 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
14161 unshare_expr (reg), u);
14163 /* _Decimal128 is passed in even/odd fpr pairs; the stored
14164 reg number is 0 for f1, so we want to make it odd. */
14165 else if (reg == fpr && mode == TDmode)
14167 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
14168 build_int_cst (TREE_TYPE (reg), 1));
14169 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
14172 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
14173 t = build2 (GE_EXPR, boolean_type_node, u, t);
14174 u = build1 (GOTO_EXPR, void_type_node, lab_false);
14175 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
14176 gimplify_and_add (t, pre_p);
14178 t = sav;
14179 if (sav_ofs)
14180 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
14182 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
14183 build_int_cst (TREE_TYPE (reg), n_reg));
14184 u = fold_convert (sizetype, u);
14185 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
14186 t = fold_build_pointer_plus (t, u);
14188 /* _Decimal32 varargs are located in the second word of the 64-bit
14189 FP register for 32-bit binaries. */
14190 if (TARGET_32BIT
14191 && TARGET_HARD_FLOAT && TARGET_FPRS
14192 && mode == SDmode)
14193 t = fold_build_pointer_plus_hwi (t, size);
14195 /* Args are passed right-aligned. */
14196 if (BYTES_BIG_ENDIAN)
14197 t = fold_build_pointer_plus_hwi (t, pad);
14199 gimplify_assign (addr, t, pre_p);
14201 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
14203 stmt = gimple_build_label (lab_false);
14204 gimple_seq_add_stmt (pre_p, stmt);
14206 if ((n_reg == 2 && !regalign) || n_reg > 2)
14208 /* Ensure that we don't find any more args in regs.
14209 Alignment has taken care of for special cases. */
14210 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
14214 /* ... otherwise out of the overflow area. */
14216 /* Care for on-stack alignment if needed. */
14217 t = ovf;
14218 if (align != 1)
14220 t = fold_build_pointer_plus_hwi (t, align - 1);
14221 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
14222 build_int_cst (TREE_TYPE (t), -align));
14225 /* Args are passed right-aligned. */
14226 if (BYTES_BIG_ENDIAN)
14227 t = fold_build_pointer_plus_hwi (t, pad);
14229 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
14231 gimplify_assign (unshare_expr (addr), t, pre_p);
14233 t = fold_build_pointer_plus_hwi (t, size);
14234 gimplify_assign (unshare_expr (ovf), t, pre_p);
14236 if (lab_over)
14238 stmt = gimple_build_label (lab_over);
14239 gimple_seq_add_stmt (pre_p, stmt);
14242 if (STRICT_ALIGNMENT
14243 && (TYPE_ALIGN (type)
14244 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
14246 /* The value (of type complex double, for example) may not be
14247 aligned in memory in the saved registers, so copy via a
14248 temporary. (This is the same code as used for SPARC.) */
14249 tree tmp = create_tmp_var (type, "va_arg_tmp");
14250 tree dest_addr = build_fold_addr_expr (tmp);
14252 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
14253 3, dest_addr, addr, size_int (rsize * 4));
14255 gimplify_and_add (copy, pre_p);
14256 addr = dest_addr;
14259 addr = fold_convert (ptrtype, addr);
14260 return build_va_arg_indirect_ref (addr);
14263 /* Builtins. */
14265 static void
14266 def_builtin (const char *name, tree type, enum rs6000_builtins code)
14268 tree t;
14269 unsigned classify = rs6000_builtin_info[(int)code].attr;
14270 const char *attr_string = "";
14272 gcc_assert (name != NULL);
14273 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
14275 if (rs6000_builtin_decls[(int)code])
14276 fatal_error (input_location,
14277 "internal error: builtin function %s already processed", name);
14279 rs6000_builtin_decls[(int)code] = t =
14280 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
14282 /* Set any special attributes. */
14283 if ((classify & RS6000_BTC_CONST) != 0)
14285 /* const function, function only depends on the inputs. */
14286 TREE_READONLY (t) = 1;
14287 TREE_NOTHROW (t) = 1;
14288 attr_string = ", const";
14290 else if ((classify & RS6000_BTC_PURE) != 0)
14292 /* pure function, function can read global memory, but does not set any
14293 external state. */
14294 DECL_PURE_P (t) = 1;
14295 TREE_NOTHROW (t) = 1;
14296 attr_string = ", pure";
14298 else if ((classify & RS6000_BTC_FP) != 0)
14300 /* Function is a math function. If rounding mode is on, then treat the
14301 function as not reading global memory, but it can have arbitrary side
14302 effects. If it is off, then assume the function is a const function.
14303 This mimics the ATTR_MATHFN_FPROUNDING attribute in
14304 builtin-attribute.def that is used for the math functions. */
14305 TREE_NOTHROW (t) = 1;
14306 if (flag_rounding_math)
14308 DECL_PURE_P (t) = 1;
14309 DECL_IS_NOVOPS (t) = 1;
14310 attr_string = ", fp, pure";
14312 else
14314 TREE_READONLY (t) = 1;
14315 attr_string = ", fp, const";
14318 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
14319 gcc_unreachable ();
14321 if (TARGET_DEBUG_BUILTIN)
14322 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
14323 (int)code, name, attr_string);
14326 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
14328 #undef RS6000_BUILTIN_0
14329 #undef RS6000_BUILTIN_1
14330 #undef RS6000_BUILTIN_2
14331 #undef RS6000_BUILTIN_3
14332 #undef RS6000_BUILTIN_A
14333 #undef RS6000_BUILTIN_D
14334 #undef RS6000_BUILTIN_E
14335 #undef RS6000_BUILTIN_H
14336 #undef RS6000_BUILTIN_P
14337 #undef RS6000_BUILTIN_Q
14338 #undef RS6000_BUILTIN_S
14339 #undef RS6000_BUILTIN_X
14341 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14342 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14343 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14344 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
14345 { MASK, ICODE, NAME, ENUM },
14347 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14348 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14349 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14350 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14351 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14352 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14353 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14354 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14356 static const struct builtin_description bdesc_3arg[] =
14358 #include "powerpcspe-builtin.def"
14361 /* DST operations: void foo (void *, const int, const char). */
14363 #undef RS6000_BUILTIN_0
14364 #undef RS6000_BUILTIN_1
14365 #undef RS6000_BUILTIN_2
14366 #undef RS6000_BUILTIN_3
14367 #undef RS6000_BUILTIN_A
14368 #undef RS6000_BUILTIN_D
14369 #undef RS6000_BUILTIN_E
14370 #undef RS6000_BUILTIN_H
14371 #undef RS6000_BUILTIN_P
14372 #undef RS6000_BUILTIN_Q
14373 #undef RS6000_BUILTIN_S
14374 #undef RS6000_BUILTIN_X
14376 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14377 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14378 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14379 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14380 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14381 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
14382 { MASK, ICODE, NAME, ENUM },
14384 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14385 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14386 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14387 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14388 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14389 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14391 static const struct builtin_description bdesc_dst[] =
14393 #include "powerpcspe-builtin.def"
14396 /* Simple binary operations: VECc = foo (VECa, VECb). */
14398 #undef RS6000_BUILTIN_0
14399 #undef RS6000_BUILTIN_1
14400 #undef RS6000_BUILTIN_2
14401 #undef RS6000_BUILTIN_3
14402 #undef RS6000_BUILTIN_A
14403 #undef RS6000_BUILTIN_D
14404 #undef RS6000_BUILTIN_E
14405 #undef RS6000_BUILTIN_H
14406 #undef RS6000_BUILTIN_P
14407 #undef RS6000_BUILTIN_Q
14408 #undef RS6000_BUILTIN_S
14409 #undef RS6000_BUILTIN_X
14411 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14412 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14413 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
14414 { MASK, ICODE, NAME, ENUM },
14416 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14417 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14418 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14419 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14420 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14421 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14422 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14423 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14424 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14426 static const struct builtin_description bdesc_2arg[] =
14428 #include "powerpcspe-builtin.def"
14431 #undef RS6000_BUILTIN_0
14432 #undef RS6000_BUILTIN_1
14433 #undef RS6000_BUILTIN_2
14434 #undef RS6000_BUILTIN_3
14435 #undef RS6000_BUILTIN_A
14436 #undef RS6000_BUILTIN_D
14437 #undef RS6000_BUILTIN_E
14438 #undef RS6000_BUILTIN_H
14439 #undef RS6000_BUILTIN_P
14440 #undef RS6000_BUILTIN_Q
14441 #undef RS6000_BUILTIN_S
14442 #undef RS6000_BUILTIN_X
14444 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14445 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14446 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14447 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14448 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14449 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14450 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14451 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14452 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
14453 { MASK, ICODE, NAME, ENUM },
14455 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14456 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14457 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14459 /* AltiVec predicates. */
14461 static const struct builtin_description bdesc_altivec_preds[] =
14463 #include "powerpcspe-builtin.def"
14466 /* SPE predicates. */
14467 #undef RS6000_BUILTIN_0
14468 #undef RS6000_BUILTIN_1
14469 #undef RS6000_BUILTIN_2
14470 #undef RS6000_BUILTIN_3
14471 #undef RS6000_BUILTIN_A
14472 #undef RS6000_BUILTIN_D
14473 #undef RS6000_BUILTIN_E
14474 #undef RS6000_BUILTIN_H
14475 #undef RS6000_BUILTIN_P
14476 #undef RS6000_BUILTIN_Q
14477 #undef RS6000_BUILTIN_S
14478 #undef RS6000_BUILTIN_X
14480 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14481 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14482 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14483 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14484 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14485 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14486 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14487 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14488 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14489 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14490 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
14491 { MASK, ICODE, NAME, ENUM },
14493 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14495 static const struct builtin_description bdesc_spe_predicates[] =
14497 #include "powerpcspe-builtin.def"
14500 /* SPE evsel predicates. */
14501 #undef RS6000_BUILTIN_0
14502 #undef RS6000_BUILTIN_1
14503 #undef RS6000_BUILTIN_2
14504 #undef RS6000_BUILTIN_3
14505 #undef RS6000_BUILTIN_A
14506 #undef RS6000_BUILTIN_D
14507 #undef RS6000_BUILTIN_E
14508 #undef RS6000_BUILTIN_H
14509 #undef RS6000_BUILTIN_P
14510 #undef RS6000_BUILTIN_Q
14511 #undef RS6000_BUILTIN_S
14512 #undef RS6000_BUILTIN_X
14514 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14515 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14516 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14517 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14518 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14519 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14520 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
14521 { MASK, ICODE, NAME, ENUM },
14523 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14524 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14525 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14526 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14527 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14529 static const struct builtin_description bdesc_spe_evsel[] =
14531 #include "powerpcspe-builtin.def"
14534 /* PAIRED predicates. */
14535 #undef RS6000_BUILTIN_0
14536 #undef RS6000_BUILTIN_1
14537 #undef RS6000_BUILTIN_2
14538 #undef RS6000_BUILTIN_3
14539 #undef RS6000_BUILTIN_A
14540 #undef RS6000_BUILTIN_D
14541 #undef RS6000_BUILTIN_E
14542 #undef RS6000_BUILTIN_H
14543 #undef RS6000_BUILTIN_P
14544 #undef RS6000_BUILTIN_Q
14545 #undef RS6000_BUILTIN_S
14546 #undef RS6000_BUILTIN_X
14548 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14549 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14550 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14551 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14552 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14553 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14554 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14555 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14556 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14557 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
14558 { MASK, ICODE, NAME, ENUM },
14560 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14561 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14563 static const struct builtin_description bdesc_paired_preds[] =
14565 #include "powerpcspe-builtin.def"
14568 /* ABS* operations. */
14570 #undef RS6000_BUILTIN_0
14571 #undef RS6000_BUILTIN_1
14572 #undef RS6000_BUILTIN_2
14573 #undef RS6000_BUILTIN_3
14574 #undef RS6000_BUILTIN_A
14575 #undef RS6000_BUILTIN_D
14576 #undef RS6000_BUILTIN_E
14577 #undef RS6000_BUILTIN_H
14578 #undef RS6000_BUILTIN_P
14579 #undef RS6000_BUILTIN_Q
14580 #undef RS6000_BUILTIN_S
14581 #undef RS6000_BUILTIN_X
14583 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14584 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14585 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14586 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14587 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
14588 { MASK, ICODE, NAME, ENUM },
14590 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14591 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14592 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14593 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14594 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14595 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14596 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14598 static const struct builtin_description bdesc_abs[] =
14600 #include "powerpcspe-builtin.def"
14603 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
14604 foo (VECa). */
14606 #undef RS6000_BUILTIN_0
14607 #undef RS6000_BUILTIN_1
14608 #undef RS6000_BUILTIN_2
14609 #undef RS6000_BUILTIN_3
14610 #undef RS6000_BUILTIN_A
14611 #undef RS6000_BUILTIN_D
14612 #undef RS6000_BUILTIN_E
14613 #undef RS6000_BUILTIN_H
14614 #undef RS6000_BUILTIN_P
14615 #undef RS6000_BUILTIN_Q
14616 #undef RS6000_BUILTIN_S
14617 #undef RS6000_BUILTIN_X
14619 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14620 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
14621 { MASK, ICODE, NAME, ENUM },
14623 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14624 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14625 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14626 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14627 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14628 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14629 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14630 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14631 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14632 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14634 static const struct builtin_description bdesc_1arg[] =
14636 #include "powerpcspe-builtin.def"
14639 /* Simple no-argument operations: result = __builtin_darn_32 () */
14641 #undef RS6000_BUILTIN_0
14642 #undef RS6000_BUILTIN_1
14643 #undef RS6000_BUILTIN_2
14644 #undef RS6000_BUILTIN_3
14645 #undef RS6000_BUILTIN_A
14646 #undef RS6000_BUILTIN_D
14647 #undef RS6000_BUILTIN_E
14648 #undef RS6000_BUILTIN_H
14649 #undef RS6000_BUILTIN_P
14650 #undef RS6000_BUILTIN_Q
14651 #undef RS6000_BUILTIN_S
14652 #undef RS6000_BUILTIN_X
14654 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE) \
14655 { MASK, ICODE, NAME, ENUM },
14657 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14658 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14659 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14660 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14661 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14662 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14663 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14664 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14665 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14666 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14667 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14669 static const struct builtin_description bdesc_0arg[] =
14671 #include "powerpcspe-builtin.def"
14674 /* HTM builtins. */
14675 #undef RS6000_BUILTIN_0
14676 #undef RS6000_BUILTIN_1
14677 #undef RS6000_BUILTIN_2
14678 #undef RS6000_BUILTIN_3
14679 #undef RS6000_BUILTIN_A
14680 #undef RS6000_BUILTIN_D
14681 #undef RS6000_BUILTIN_E
14682 #undef RS6000_BUILTIN_H
14683 #undef RS6000_BUILTIN_P
14684 #undef RS6000_BUILTIN_Q
14685 #undef RS6000_BUILTIN_S
14686 #undef RS6000_BUILTIN_X
14688 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14689 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14690 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14691 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14692 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14693 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14694 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14695 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
14696 { MASK, ICODE, NAME, ENUM },
14698 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14699 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14700 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14701 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14703 static const struct builtin_description bdesc_htm[] =
14705 #include "powerpcspe-builtin.def"
14708 #undef RS6000_BUILTIN_0
14709 #undef RS6000_BUILTIN_1
14710 #undef RS6000_BUILTIN_2
14711 #undef RS6000_BUILTIN_3
14712 #undef RS6000_BUILTIN_A
14713 #undef RS6000_BUILTIN_D
14714 #undef RS6000_BUILTIN_E
14715 #undef RS6000_BUILTIN_H
14716 #undef RS6000_BUILTIN_P
14717 #undef RS6000_BUILTIN_Q
14718 #undef RS6000_BUILTIN_S
14720 /* Return true if a builtin function is overloaded. */
14721 bool
14722 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
14724 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
14727 const char *
14728 rs6000_overloaded_builtin_name (enum rs6000_builtins fncode)
14730 return rs6000_builtin_info[(int)fncode].name;
14733 /* Expand an expression EXP that calls a builtin without arguments. */
14734 static rtx
14735 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
14737 rtx pat;
14738 machine_mode tmode = insn_data[icode].operand[0].mode;
14740 if (icode == CODE_FOR_nothing)
14741 /* Builtin not supported on this processor. */
14742 return 0;
14744 if (target == 0
14745 || GET_MODE (target) != tmode
14746 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14747 target = gen_reg_rtx (tmode);
14749 pat = GEN_FCN (icode) (target);
14750 if (! pat)
14751 return 0;
14752 emit_insn (pat);
14754 return target;
14758 static rtx
14759 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
14761 rtx pat;
14762 tree arg0 = CALL_EXPR_ARG (exp, 0);
14763 tree arg1 = CALL_EXPR_ARG (exp, 1);
14764 rtx op0 = expand_normal (arg0);
14765 rtx op1 = expand_normal (arg1);
14766 machine_mode mode0 = insn_data[icode].operand[0].mode;
14767 machine_mode mode1 = insn_data[icode].operand[1].mode;
14769 if (icode == CODE_FOR_nothing)
14770 /* Builtin not supported on this processor. */
14771 return 0;
14773 /* If we got invalid arguments bail out before generating bad rtl. */
14774 if (arg0 == error_mark_node || arg1 == error_mark_node)
14775 return const0_rtx;
14777 if (GET_CODE (op0) != CONST_INT
14778 || INTVAL (op0) > 255
14779 || INTVAL (op0) < 0)
14781 error ("argument 1 must be an 8-bit field value");
14782 return const0_rtx;
14785 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
14786 op0 = copy_to_mode_reg (mode0, op0);
14788 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
14789 op1 = copy_to_mode_reg (mode1, op1);
14791 pat = GEN_FCN (icode) (op0, op1);
14792 if (! pat)
14793 return const0_rtx;
14794 emit_insn (pat);
14796 return NULL_RTX;
14799 static rtx
14800 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
14802 rtx pat;
14803 tree arg0 = CALL_EXPR_ARG (exp, 0);
14804 rtx op0 = expand_normal (arg0);
14805 machine_mode tmode = insn_data[icode].operand[0].mode;
14806 machine_mode mode0 = insn_data[icode].operand[1].mode;
14808 if (icode == CODE_FOR_nothing)
14809 /* Builtin not supported on this processor. */
14810 return 0;
14812 /* If we got invalid arguments bail out before generating bad rtl. */
14813 if (arg0 == error_mark_node)
14814 return const0_rtx;
14816 if (icode == CODE_FOR_altivec_vspltisb
14817 || icode == CODE_FOR_altivec_vspltish
14818 || icode == CODE_FOR_altivec_vspltisw
14819 || icode == CODE_FOR_spe_evsplatfi
14820 || icode == CODE_FOR_spe_evsplati)
14822 /* Only allow 5-bit *signed* literals. */
14823 if (GET_CODE (op0) != CONST_INT
14824 || INTVAL (op0) > 15
14825 || INTVAL (op0) < -16)
14827 error ("argument 1 must be a 5-bit signed literal");
14828 return CONST0_RTX (tmode);
14832 if (target == 0
14833 || GET_MODE (target) != tmode
14834 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14835 target = gen_reg_rtx (tmode);
14837 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14838 op0 = copy_to_mode_reg (mode0, op0);
14840 pat = GEN_FCN (icode) (target, op0);
14841 if (! pat)
14842 return 0;
14843 emit_insn (pat);
14845 return target;
14848 static rtx
14849 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
14851 rtx pat, scratch1, scratch2;
14852 tree arg0 = CALL_EXPR_ARG (exp, 0);
14853 rtx op0 = expand_normal (arg0);
14854 machine_mode tmode = insn_data[icode].operand[0].mode;
14855 machine_mode mode0 = insn_data[icode].operand[1].mode;
14857 /* If we have invalid arguments, bail out before generating bad rtl. */
14858 if (arg0 == error_mark_node)
14859 return const0_rtx;
14861 if (target == 0
14862 || GET_MODE (target) != tmode
14863 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14864 target = gen_reg_rtx (tmode);
14866 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14867 op0 = copy_to_mode_reg (mode0, op0);
14869 scratch1 = gen_reg_rtx (mode0);
14870 scratch2 = gen_reg_rtx (mode0);
14872 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
14873 if (! pat)
14874 return 0;
14875 emit_insn (pat);
14877 return target;
14880 static rtx
14881 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
14883 rtx pat;
14884 tree arg0 = CALL_EXPR_ARG (exp, 0);
14885 tree arg1 = CALL_EXPR_ARG (exp, 1);
14886 rtx op0 = expand_normal (arg0);
14887 rtx op1 = expand_normal (arg1);
14888 machine_mode tmode = insn_data[icode].operand[0].mode;
14889 machine_mode mode0 = insn_data[icode].operand[1].mode;
14890 machine_mode mode1 = insn_data[icode].operand[2].mode;
14892 if (icode == CODE_FOR_nothing)
14893 /* Builtin not supported on this processor. */
14894 return 0;
14896 /* If we got invalid arguments bail out before generating bad rtl. */
14897 if (arg0 == error_mark_node || arg1 == error_mark_node)
14898 return const0_rtx;
14900 if (icode == CODE_FOR_altivec_vcfux
14901 || icode == CODE_FOR_altivec_vcfsx
14902 || icode == CODE_FOR_altivec_vctsxs
14903 || icode == CODE_FOR_altivec_vctuxs
14904 || icode == CODE_FOR_altivec_vspltb
14905 || icode == CODE_FOR_altivec_vsplth
14906 || icode == CODE_FOR_altivec_vspltw
14907 || icode == CODE_FOR_spe_evaddiw
14908 || icode == CODE_FOR_spe_evldd
14909 || icode == CODE_FOR_spe_evldh
14910 || icode == CODE_FOR_spe_evldw
14911 || icode == CODE_FOR_spe_evlhhesplat
14912 || icode == CODE_FOR_spe_evlhhossplat
14913 || icode == CODE_FOR_spe_evlhhousplat
14914 || icode == CODE_FOR_spe_evlwhe
14915 || icode == CODE_FOR_spe_evlwhos
14916 || icode == CODE_FOR_spe_evlwhou
14917 || icode == CODE_FOR_spe_evlwhsplat
14918 || icode == CODE_FOR_spe_evlwwsplat
14919 || icode == CODE_FOR_spe_evrlwi
14920 || icode == CODE_FOR_spe_evslwi
14921 || icode == CODE_FOR_spe_evsrwis
14922 || icode == CODE_FOR_spe_evsubifw
14923 || icode == CODE_FOR_spe_evsrwiu)
14925 /* Only allow 5-bit unsigned literals. */
14926 STRIP_NOPS (arg1);
14927 if (TREE_CODE (arg1) != INTEGER_CST
14928 || TREE_INT_CST_LOW (arg1) & ~0x1f)
14930 error ("argument 2 must be a 5-bit unsigned literal");
14931 return CONST0_RTX (tmode);
14934 else if (icode == CODE_FOR_dfptstsfi_eq_dd
14935 || icode == CODE_FOR_dfptstsfi_lt_dd
14936 || icode == CODE_FOR_dfptstsfi_gt_dd
14937 || icode == CODE_FOR_dfptstsfi_unordered_dd
14938 || icode == CODE_FOR_dfptstsfi_eq_td
14939 || icode == CODE_FOR_dfptstsfi_lt_td
14940 || icode == CODE_FOR_dfptstsfi_gt_td
14941 || icode == CODE_FOR_dfptstsfi_unordered_td)
14943 /* Only allow 6-bit unsigned literals. */
14944 STRIP_NOPS (arg0);
14945 if (TREE_CODE (arg0) != INTEGER_CST
14946 || !IN_RANGE (TREE_INT_CST_LOW (arg0), 0, 63))
14948 error ("argument 1 must be a 6-bit unsigned literal");
14949 return CONST0_RTX (tmode);
14952 else if (icode == CODE_FOR_xststdcdp
14953 || icode == CODE_FOR_xststdcsp
14954 || icode == CODE_FOR_xvtstdcdp
14955 || icode == CODE_FOR_xvtstdcsp)
14957 /* Only allow 7-bit unsigned literals. */
14958 STRIP_NOPS (arg1);
14959 if (TREE_CODE (arg1) != INTEGER_CST
14960 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 127))
14962 error ("argument 2 must be a 7-bit unsigned literal");
14963 return CONST0_RTX (tmode);
14967 if (target == 0
14968 || GET_MODE (target) != tmode
14969 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14970 target = gen_reg_rtx (tmode);
14972 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14973 op0 = copy_to_mode_reg (mode0, op0);
14974 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14975 op1 = copy_to_mode_reg (mode1, op1);
14977 pat = GEN_FCN (icode) (target, op0, op1);
14978 if (! pat)
14979 return 0;
14980 emit_insn (pat);
14982 return target;
14985 static rtx
14986 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
14988 rtx pat, scratch;
14989 tree cr6_form = CALL_EXPR_ARG (exp, 0);
14990 tree arg0 = CALL_EXPR_ARG (exp, 1);
14991 tree arg1 = CALL_EXPR_ARG (exp, 2);
14992 rtx op0 = expand_normal (arg0);
14993 rtx op1 = expand_normal (arg1);
14994 machine_mode tmode = SImode;
14995 machine_mode mode0 = insn_data[icode].operand[1].mode;
14996 machine_mode mode1 = insn_data[icode].operand[2].mode;
14997 int cr6_form_int;
14999 if (TREE_CODE (cr6_form) != INTEGER_CST)
15001 error ("argument 1 of __builtin_altivec_predicate must be a constant");
15002 return const0_rtx;
15004 else
15005 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
15007 gcc_assert (mode0 == mode1);
15009 /* If we have invalid arguments, bail out before generating bad rtl. */
15010 if (arg0 == error_mark_node || arg1 == error_mark_node)
15011 return const0_rtx;
15013 if (target == 0
15014 || GET_MODE (target) != tmode
15015 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15016 target = gen_reg_rtx (tmode);
15018 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15019 op0 = copy_to_mode_reg (mode0, op0);
15020 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15021 op1 = copy_to_mode_reg (mode1, op1);
15023 /* Note that for many of the relevant operations (e.g. cmpne or
15024 cmpeq) with float or double operands, it makes more sense for the
15025 mode of the allocated scratch register to select a vector of
15026 integer. But the choice to copy the mode of operand 0 was made
15027 long ago and there are no plans to change it. */
15028 scratch = gen_reg_rtx (mode0);
15030 pat = GEN_FCN (icode) (scratch, op0, op1);
15031 if (! pat)
15032 return 0;
15033 emit_insn (pat);
15035 /* The vec_any* and vec_all* predicates use the same opcodes for two
15036 different operations, but the bits in CR6 will be different
15037 depending on what information we want. So we have to play tricks
15038 with CR6 to get the right bits out.
15040 If you think this is disgusting, look at the specs for the
15041 AltiVec predicates. */
15043 switch (cr6_form_int)
15045 case 0:
15046 emit_insn (gen_cr6_test_for_zero (target));
15047 break;
15048 case 1:
15049 emit_insn (gen_cr6_test_for_zero_reverse (target));
15050 break;
15051 case 2:
15052 emit_insn (gen_cr6_test_for_lt (target));
15053 break;
15054 case 3:
15055 emit_insn (gen_cr6_test_for_lt_reverse (target));
15056 break;
15057 default:
15058 error ("argument 1 of __builtin_altivec_predicate is out of range");
15059 break;
15062 return target;
15065 static rtx
15066 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
15068 rtx pat, addr;
15069 tree arg0 = CALL_EXPR_ARG (exp, 0);
15070 tree arg1 = CALL_EXPR_ARG (exp, 1);
15071 machine_mode tmode = insn_data[icode].operand[0].mode;
15072 machine_mode mode0 = Pmode;
15073 machine_mode mode1 = Pmode;
15074 rtx op0 = expand_normal (arg0);
15075 rtx op1 = expand_normal (arg1);
15077 if (icode == CODE_FOR_nothing)
15078 /* Builtin not supported on this processor. */
15079 return 0;
15081 /* If we got invalid arguments bail out before generating bad rtl. */
15082 if (arg0 == error_mark_node || arg1 == error_mark_node)
15083 return const0_rtx;
15085 if (target == 0
15086 || GET_MODE (target) != tmode
15087 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15088 target = gen_reg_rtx (tmode);
15090 op1 = copy_to_mode_reg (mode1, op1);
15092 if (op0 == const0_rtx)
15094 addr = gen_rtx_MEM (tmode, op1);
15096 else
15098 op0 = copy_to_mode_reg (mode0, op0);
15099 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
15102 pat = GEN_FCN (icode) (target, addr);
15104 if (! pat)
15105 return 0;
15106 emit_insn (pat);
15108 return target;
15111 /* Return a constant vector for use as a little-endian permute control vector
15112 to reverse the order of elements of the given vector mode. */
15113 static rtx
15114 swap_selector_for_mode (machine_mode mode)
15116 /* These are little endian vectors, so their elements are reversed
15117 from what you would normally expect for a permute control vector. */
15118 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
15119 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
15120 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
15121 unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
15122 unsigned int *swaparray, i;
15123 rtx perm[16];
15125 switch (mode)
15127 case E_V2DFmode:
15128 case E_V2DImode:
15129 swaparray = swap2;
15130 break;
15131 case E_V4SFmode:
15132 case E_V4SImode:
15133 swaparray = swap4;
15134 break;
15135 case E_V8HImode:
15136 swaparray = swap8;
15137 break;
15138 case E_V16QImode:
15139 swaparray = swap16;
15140 break;
15141 default:
15142 gcc_unreachable ();
15145 for (i = 0; i < 16; ++i)
15146 perm[i] = GEN_INT (swaparray[i]);
15148 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
15151 /* Generate code for an "lvxl", or "lve*x" built-in for a little endian target
15152 with -maltivec=be specified. Issue the load followed by an element-
15153 reversing permute. */
15154 void
15155 altivec_expand_lvx_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
15157 rtx tmp = gen_reg_rtx (mode);
15158 rtx load = gen_rtx_SET (tmp, op1);
15159 rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
15160 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
15161 rtx sel = swap_selector_for_mode (mode);
15162 rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
15164 gcc_assert (REG_P (op0));
15165 emit_insn (par);
15166 emit_insn (gen_rtx_SET (op0, vperm));
15169 /* Generate code for a "stvxl" built-in for a little endian target with
15170 -maltivec=be specified. Issue the store preceded by an element-reversing
15171 permute. */
15172 void
15173 altivec_expand_stvx_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
15175 rtx tmp = gen_reg_rtx (mode);
15176 rtx store = gen_rtx_SET (op0, tmp);
15177 rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
15178 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
15179 rtx sel = swap_selector_for_mode (mode);
15180 rtx vperm;
15182 gcc_assert (REG_P (op1));
15183 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
15184 emit_insn (gen_rtx_SET (tmp, vperm));
15185 emit_insn (par);
15188 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
15189 specified. Issue the store preceded by an element-reversing permute. */
15190 void
15191 altivec_expand_stvex_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
15193 machine_mode inner_mode = GET_MODE_INNER (mode);
15194 rtx tmp = gen_reg_rtx (mode);
15195 rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
15196 rtx sel = swap_selector_for_mode (mode);
15197 rtx vperm;
15199 gcc_assert (REG_P (op1));
15200 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
15201 emit_insn (gen_rtx_SET (tmp, vperm));
15202 emit_insn (gen_rtx_SET (op0, stvx));
15205 static rtx
15206 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
15208 rtx pat, addr;
15209 tree arg0 = CALL_EXPR_ARG (exp, 0);
15210 tree arg1 = CALL_EXPR_ARG (exp, 1);
15211 machine_mode tmode = insn_data[icode].operand[0].mode;
15212 machine_mode mode0 = Pmode;
15213 machine_mode mode1 = Pmode;
15214 rtx op0 = expand_normal (arg0);
15215 rtx op1 = expand_normal (arg1);
15217 if (icode == CODE_FOR_nothing)
15218 /* Builtin not supported on this processor. */
15219 return 0;
15221 /* If we got invalid arguments bail out before generating bad rtl. */
15222 if (arg0 == error_mark_node || arg1 == error_mark_node)
15223 return const0_rtx;
15225 if (target == 0
15226 || GET_MODE (target) != tmode
15227 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15228 target = gen_reg_rtx (tmode);
15230 op1 = copy_to_mode_reg (mode1, op1);
15232 /* For LVX, express the RTL accurately by ANDing the address with -16.
15233 LVXL and LVE*X expand to use UNSPECs to hide their special behavior,
15234 so the raw address is fine. */
15235 if (icode == CODE_FOR_altivec_lvx_v2df_2op
15236 || icode == CODE_FOR_altivec_lvx_v2di_2op
15237 || icode == CODE_FOR_altivec_lvx_v4sf_2op
15238 || icode == CODE_FOR_altivec_lvx_v4si_2op
15239 || icode == CODE_FOR_altivec_lvx_v8hi_2op
15240 || icode == CODE_FOR_altivec_lvx_v16qi_2op)
15242 rtx rawaddr;
15243 if (op0 == const0_rtx)
15244 rawaddr = op1;
15245 else
15247 op0 = copy_to_mode_reg (mode0, op0);
15248 rawaddr = gen_rtx_PLUS (Pmode, op1, op0);
15250 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
15251 addr = gen_rtx_MEM (blk ? BLKmode : tmode, addr);
15253 /* For -maltivec=be, emit the load and follow it up with a
15254 permute to swap the elements. */
15255 if (!BYTES_BIG_ENDIAN && VECTOR_ELT_ORDER_BIG)
15257 rtx temp = gen_reg_rtx (tmode);
15258 emit_insn (gen_rtx_SET (temp, addr));
15260 rtx sel = swap_selector_for_mode (tmode);
15261 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, temp, temp, sel),
15262 UNSPEC_VPERM);
15263 emit_insn (gen_rtx_SET (target, vperm));
15265 else
15266 emit_insn (gen_rtx_SET (target, addr));
15268 else
15270 if (op0 == const0_rtx)
15271 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
15272 else
15274 op0 = copy_to_mode_reg (mode0, op0);
15275 addr = gen_rtx_MEM (blk ? BLKmode : tmode,
15276 gen_rtx_PLUS (Pmode, op1, op0));
15279 pat = GEN_FCN (icode) (target, addr);
15280 if (! pat)
15281 return 0;
15282 emit_insn (pat);
15285 return target;
15288 static rtx
15289 spe_expand_stv_builtin (enum insn_code icode, tree exp)
15291 tree arg0 = CALL_EXPR_ARG (exp, 0);
15292 tree arg1 = CALL_EXPR_ARG (exp, 1);
15293 tree arg2 = CALL_EXPR_ARG (exp, 2);
15294 rtx op0 = expand_normal (arg0);
15295 rtx op1 = expand_normal (arg1);
15296 rtx op2 = expand_normal (arg2);
15297 rtx pat;
15298 machine_mode mode0 = insn_data[icode].operand[0].mode;
15299 machine_mode mode1 = insn_data[icode].operand[1].mode;
15300 machine_mode mode2 = insn_data[icode].operand[2].mode;
15302 /* Invalid arguments. Bail before doing anything stoopid! */
15303 if (arg0 == error_mark_node
15304 || arg1 == error_mark_node
15305 || arg2 == error_mark_node)
15306 return const0_rtx;
15308 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
15309 op0 = copy_to_mode_reg (mode2, op0);
15310 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
15311 op1 = copy_to_mode_reg (mode0, op1);
15312 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
15313 op2 = copy_to_mode_reg (mode1, op2);
15315 pat = GEN_FCN (icode) (op1, op2, op0);
15316 if (pat)
15317 emit_insn (pat);
15318 return NULL_RTX;
15321 static rtx
15322 paired_expand_stv_builtin (enum insn_code icode, tree exp)
15324 tree arg0 = CALL_EXPR_ARG (exp, 0);
15325 tree arg1 = CALL_EXPR_ARG (exp, 1);
15326 tree arg2 = CALL_EXPR_ARG (exp, 2);
15327 rtx op0 = expand_normal (arg0);
15328 rtx op1 = expand_normal (arg1);
15329 rtx op2 = expand_normal (arg2);
15330 rtx pat, addr;
15331 machine_mode tmode = insn_data[icode].operand[0].mode;
15332 machine_mode mode1 = Pmode;
15333 machine_mode mode2 = Pmode;
15335 /* Invalid arguments. Bail before doing anything stoopid! */
15336 if (arg0 == error_mark_node
15337 || arg1 == error_mark_node
15338 || arg2 == error_mark_node)
15339 return const0_rtx;
15341 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
15342 op0 = copy_to_mode_reg (tmode, op0);
15344 op2 = copy_to_mode_reg (mode2, op2);
15346 if (op1 == const0_rtx)
15348 addr = gen_rtx_MEM (tmode, op2);
15350 else
15352 op1 = copy_to_mode_reg (mode1, op1);
15353 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
15356 pat = GEN_FCN (icode) (addr, op0);
15357 if (pat)
15358 emit_insn (pat);
15359 return NULL_RTX;
15362 static rtx
15363 altivec_expand_stxvl_builtin (enum insn_code icode, tree exp)
15365 rtx pat;
15366 tree arg0 = CALL_EXPR_ARG (exp, 0);
15367 tree arg1 = CALL_EXPR_ARG (exp, 1);
15368 tree arg2 = CALL_EXPR_ARG (exp, 2);
15369 rtx op0 = expand_normal (arg0);
15370 rtx op1 = expand_normal (arg1);
15371 rtx op2 = expand_normal (arg2);
15372 machine_mode mode0 = insn_data[icode].operand[0].mode;
15373 machine_mode mode1 = insn_data[icode].operand[1].mode;
15374 machine_mode mode2 = insn_data[icode].operand[2].mode;
15376 if (icode == CODE_FOR_nothing)
15377 /* Builtin not supported on this processor. */
15378 return NULL_RTX;
15380 /* If we got invalid arguments bail out before generating bad rtl. */
15381 if (arg0 == error_mark_node
15382 || arg1 == error_mark_node
15383 || arg2 == error_mark_node)
15384 return NULL_RTX;
15386 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15387 op0 = copy_to_mode_reg (mode0, op0);
15388 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15389 op1 = copy_to_mode_reg (mode1, op1);
15390 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15391 op2 = copy_to_mode_reg (mode2, op2);
15393 pat = GEN_FCN (icode) (op0, op1, op2);
15394 if (pat)
15395 emit_insn (pat);
15397 return NULL_RTX;
15400 static rtx
15401 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
15403 tree arg0 = CALL_EXPR_ARG (exp, 0);
15404 tree arg1 = CALL_EXPR_ARG (exp, 1);
15405 tree arg2 = CALL_EXPR_ARG (exp, 2);
15406 rtx op0 = expand_normal (arg0);
15407 rtx op1 = expand_normal (arg1);
15408 rtx op2 = expand_normal (arg2);
15409 rtx pat, addr, rawaddr;
15410 machine_mode tmode = insn_data[icode].operand[0].mode;
15411 machine_mode smode = insn_data[icode].operand[1].mode;
15412 machine_mode mode1 = Pmode;
15413 machine_mode mode2 = Pmode;
15415 /* Invalid arguments. Bail before doing anything stoopid! */
15416 if (arg0 == error_mark_node
15417 || arg1 == error_mark_node
15418 || arg2 == error_mark_node)
15419 return const0_rtx;
15421 op2 = copy_to_mode_reg (mode2, op2);
15423 /* For STVX, express the RTL accurately by ANDing the address with -16.
15424 STVXL and STVE*X expand to use UNSPECs to hide their special behavior,
15425 so the raw address is fine. */
15426 if (icode == CODE_FOR_altivec_stvx_v2df_2op
15427 || icode == CODE_FOR_altivec_stvx_v2di_2op
15428 || icode == CODE_FOR_altivec_stvx_v4sf_2op
15429 || icode == CODE_FOR_altivec_stvx_v4si_2op
15430 || icode == CODE_FOR_altivec_stvx_v8hi_2op
15431 || icode == CODE_FOR_altivec_stvx_v16qi_2op)
15433 if (op1 == const0_rtx)
15434 rawaddr = op2;
15435 else
15437 op1 = copy_to_mode_reg (mode1, op1);
15438 rawaddr = gen_rtx_PLUS (Pmode, op2, op1);
15441 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
15442 addr = gen_rtx_MEM (tmode, addr);
15444 op0 = copy_to_mode_reg (tmode, op0);
15446 /* For -maltivec=be, emit a permute to swap the elements, followed
15447 by the store. */
15448 if (!BYTES_BIG_ENDIAN && VECTOR_ELT_ORDER_BIG)
15450 rtx temp = gen_reg_rtx (tmode);
15451 rtx sel = swap_selector_for_mode (tmode);
15452 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, op0, op0, sel),
15453 UNSPEC_VPERM);
15454 emit_insn (gen_rtx_SET (temp, vperm));
15455 emit_insn (gen_rtx_SET (addr, temp));
15457 else
15458 emit_insn (gen_rtx_SET (addr, op0));
15460 else
15462 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
15463 op0 = copy_to_mode_reg (smode, op0);
15465 if (op1 == const0_rtx)
15466 addr = gen_rtx_MEM (tmode, op2);
15467 else
15469 op1 = copy_to_mode_reg (mode1, op1);
15470 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op2, op1));
15473 pat = GEN_FCN (icode) (addr, op0);
15474 if (pat)
15475 emit_insn (pat);
15478 return NULL_RTX;
15481 /* Return the appropriate SPR number associated with the given builtin. */
15482 static inline HOST_WIDE_INT
15483 htm_spr_num (enum rs6000_builtins code)
15485 if (code == HTM_BUILTIN_GET_TFHAR
15486 || code == HTM_BUILTIN_SET_TFHAR)
15487 return TFHAR_SPR;
15488 else if (code == HTM_BUILTIN_GET_TFIAR
15489 || code == HTM_BUILTIN_SET_TFIAR)
15490 return TFIAR_SPR;
15491 else if (code == HTM_BUILTIN_GET_TEXASR
15492 || code == HTM_BUILTIN_SET_TEXASR)
15493 return TEXASR_SPR;
15494 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
15495 || code == HTM_BUILTIN_SET_TEXASRU);
15496 return TEXASRU_SPR;
15499 /* Return the appropriate SPR regno associated with the given builtin. */
15500 static inline HOST_WIDE_INT
15501 htm_spr_regno (enum rs6000_builtins code)
15503 if (code == HTM_BUILTIN_GET_TFHAR
15504 || code == HTM_BUILTIN_SET_TFHAR)
15505 return TFHAR_REGNO;
15506 else if (code == HTM_BUILTIN_GET_TFIAR
15507 || code == HTM_BUILTIN_SET_TFIAR)
15508 return TFIAR_REGNO;
15509 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
15510 || code == HTM_BUILTIN_SET_TEXASR
15511 || code == HTM_BUILTIN_GET_TEXASRU
15512 || code == HTM_BUILTIN_SET_TEXASRU);
15513 return TEXASR_REGNO;
15516 /* Return the correct ICODE value depending on whether we are
15517 setting or reading the HTM SPRs. */
15518 static inline enum insn_code
15519 rs6000_htm_spr_icode (bool nonvoid)
15521 if (nonvoid)
15522 return (TARGET_POWERPC64) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
15523 else
15524 return (TARGET_POWERPC64) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
15527 /* Expand the HTM builtin in EXP and store the result in TARGET.
15528 Store true in *EXPANDEDP if we found a builtin to expand. */
15529 static rtx
15530 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
15532 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15533 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
15534 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
15535 const struct builtin_description *d;
15536 size_t i;
15538 *expandedp = true;
15540 if (!TARGET_POWERPC64
15541 && (fcode == HTM_BUILTIN_TABORTDC
15542 || fcode == HTM_BUILTIN_TABORTDCI))
15544 size_t uns_fcode = (size_t)fcode;
15545 const char *name = rs6000_builtin_info[uns_fcode].name;
15546 error ("builtin %s is only valid in 64-bit mode", name);
15547 return const0_rtx;
15550 /* Expand the HTM builtins. */
15551 d = bdesc_htm;
15552 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
15553 if (d->code == fcode)
15555 rtx op[MAX_HTM_OPERANDS], pat;
15556 int nopnds = 0;
15557 tree arg;
15558 call_expr_arg_iterator iter;
15559 unsigned attr = rs6000_builtin_info[fcode].attr;
15560 enum insn_code icode = d->icode;
15561 const struct insn_operand_data *insn_op;
15562 bool uses_spr = (attr & RS6000_BTC_SPR);
15563 rtx cr = NULL_RTX;
15565 if (uses_spr)
15566 icode = rs6000_htm_spr_icode (nonvoid);
15567 insn_op = &insn_data[icode].operand[0];
15569 if (nonvoid)
15571 machine_mode tmode = (uses_spr) ? insn_op->mode : E_SImode;
15572 if (!target
15573 || GET_MODE (target) != tmode
15574 || (uses_spr && !(*insn_op->predicate) (target, tmode)))
15575 target = gen_reg_rtx (tmode);
15576 if (uses_spr)
15577 op[nopnds++] = target;
15580 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
15582 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
15583 return const0_rtx;
15585 insn_op = &insn_data[icode].operand[nopnds];
15587 op[nopnds] = expand_normal (arg);
15589 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
15591 if (!strcmp (insn_op->constraint, "n"))
15593 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
15594 if (!CONST_INT_P (op[nopnds]))
15595 error ("argument %d must be an unsigned literal", arg_num);
15596 else
15597 error ("argument %d is an unsigned literal that is "
15598 "out of range", arg_num);
15599 return const0_rtx;
15601 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
15604 nopnds++;
15607 /* Handle the builtins for extended mnemonics. These accept
15608 no arguments, but map to builtins that take arguments. */
15609 switch (fcode)
15611 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
15612 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
15613 op[nopnds++] = GEN_INT (1);
15614 if (flag_checking)
15615 attr |= RS6000_BTC_UNARY;
15616 break;
15617 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
15618 op[nopnds++] = GEN_INT (0);
15619 if (flag_checking)
15620 attr |= RS6000_BTC_UNARY;
15621 break;
15622 default:
15623 break;
15626 /* If this builtin accesses SPRs, then pass in the appropriate
15627 SPR number and SPR regno as the last two operands. */
15628 if (uses_spr)
15630 machine_mode mode = (TARGET_POWERPC64) ? DImode : SImode;
15631 op[nopnds++] = gen_rtx_CONST_INT (mode, htm_spr_num (fcode));
15632 op[nopnds++] = gen_rtx_REG (mode, htm_spr_regno (fcode));
15634 /* If this builtin accesses a CR, then pass in a scratch
15635 CR as the last operand. */
15636 else if (attr & RS6000_BTC_CR)
15637 { cr = gen_reg_rtx (CCmode);
15638 op[nopnds++] = cr;
15641 if (flag_checking)
15643 int expected_nopnds = 0;
15644 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
15645 expected_nopnds = 1;
15646 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
15647 expected_nopnds = 2;
15648 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
15649 expected_nopnds = 3;
15650 if (!(attr & RS6000_BTC_VOID))
15651 expected_nopnds += 1;
15652 if (uses_spr)
15653 expected_nopnds += 2;
15655 gcc_assert (nopnds == expected_nopnds
15656 && nopnds <= MAX_HTM_OPERANDS);
15659 switch (nopnds)
15661 case 1:
15662 pat = GEN_FCN (icode) (op[0]);
15663 break;
15664 case 2:
15665 pat = GEN_FCN (icode) (op[0], op[1]);
15666 break;
15667 case 3:
15668 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
15669 break;
15670 case 4:
15671 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
15672 break;
15673 default:
15674 gcc_unreachable ();
15676 if (!pat)
15677 return NULL_RTX;
15678 emit_insn (pat);
15680 if (attr & RS6000_BTC_CR)
15682 if (fcode == HTM_BUILTIN_TBEGIN)
15684 /* Emit code to set TARGET to true or false depending on
15685 whether the tbegin. instruction successfully or failed
15686 to start a transaction. We do this by placing the 1's
15687 complement of CR's EQ bit into TARGET. */
15688 rtx scratch = gen_reg_rtx (SImode);
15689 emit_insn (gen_rtx_SET (scratch,
15690 gen_rtx_EQ (SImode, cr,
15691 const0_rtx)));
15692 emit_insn (gen_rtx_SET (target,
15693 gen_rtx_XOR (SImode, scratch,
15694 GEN_INT (1))));
15696 else
15698 /* Emit code to copy the 4-bit condition register field
15699 CR into the least significant end of register TARGET. */
15700 rtx scratch1 = gen_reg_rtx (SImode);
15701 rtx scratch2 = gen_reg_rtx (SImode);
15702 rtx subreg = simplify_gen_subreg (CCmode, scratch1, SImode, 0);
15703 emit_insn (gen_movcc (subreg, cr));
15704 emit_insn (gen_lshrsi3 (scratch2, scratch1, GEN_INT (28)));
15705 emit_insn (gen_andsi3 (target, scratch2, GEN_INT (0xf)));
15709 if (nonvoid)
15710 return target;
15711 return const0_rtx;
15714 *expandedp = false;
15715 return NULL_RTX;
15718 /* Expand the CPU builtin in FCODE and store the result in TARGET. */
15720 static rtx
15721 cpu_expand_builtin (enum rs6000_builtins fcode, tree exp ATTRIBUTE_UNUSED,
15722 rtx target)
15724 /* __builtin_cpu_init () is a nop, so expand to nothing. */
15725 if (fcode == RS6000_BUILTIN_CPU_INIT)
15726 return const0_rtx;
15728 if (target == 0 || GET_MODE (target) != SImode)
15729 target = gen_reg_rtx (SImode);
15731 #ifdef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
15732 tree arg = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
15733 if (TREE_CODE (arg) != STRING_CST)
15735 error ("builtin %s only accepts a string argument",
15736 rs6000_builtin_info[(size_t) fcode].name);
15737 return const0_rtx;
15740 if (fcode == RS6000_BUILTIN_CPU_IS)
15742 const char *cpu = TREE_STRING_POINTER (arg);
15743 rtx cpuid = NULL_RTX;
15744 for (size_t i = 0; i < ARRAY_SIZE (cpu_is_info); i++)
15745 if (strcmp (cpu, cpu_is_info[i].cpu) == 0)
15747 /* The CPUID value in the TCB is offset by _DL_FIRST_PLATFORM. */
15748 cpuid = GEN_INT (cpu_is_info[i].cpuid + _DL_FIRST_PLATFORM);
15749 break;
15751 if (cpuid == NULL_RTX)
15753 /* Invalid CPU argument. */
15754 error ("cpu %s is an invalid argument to builtin %s",
15755 cpu, rs6000_builtin_info[(size_t) fcode].name);
15756 return const0_rtx;
15759 rtx platform = gen_reg_rtx (SImode);
15760 rtx tcbmem = gen_const_mem (SImode,
15761 gen_rtx_PLUS (Pmode,
15762 gen_rtx_REG (Pmode, TLS_REGNUM),
15763 GEN_INT (TCB_PLATFORM_OFFSET)));
15764 emit_move_insn (platform, tcbmem);
15765 emit_insn (gen_eqsi3 (target, platform, cpuid));
15767 else if (fcode == RS6000_BUILTIN_CPU_SUPPORTS)
15769 const char *hwcap = TREE_STRING_POINTER (arg);
15770 rtx mask = NULL_RTX;
15771 int hwcap_offset;
15772 for (size_t i = 0; i < ARRAY_SIZE (cpu_supports_info); i++)
15773 if (strcmp (hwcap, cpu_supports_info[i].hwcap) == 0)
15775 mask = GEN_INT (cpu_supports_info[i].mask);
15776 hwcap_offset = TCB_HWCAP_OFFSET (cpu_supports_info[i].id);
15777 break;
15779 if (mask == NULL_RTX)
15781 /* Invalid HWCAP argument. */
15782 error ("hwcap %s is an invalid argument to builtin %s",
15783 hwcap, rs6000_builtin_info[(size_t) fcode].name);
15784 return const0_rtx;
15787 rtx tcb_hwcap = gen_reg_rtx (SImode);
15788 rtx tcbmem = gen_const_mem (SImode,
15789 gen_rtx_PLUS (Pmode,
15790 gen_rtx_REG (Pmode, TLS_REGNUM),
15791 GEN_INT (hwcap_offset)));
15792 emit_move_insn (tcb_hwcap, tcbmem);
15793 rtx scratch1 = gen_reg_rtx (SImode);
15794 emit_insn (gen_rtx_SET (scratch1, gen_rtx_AND (SImode, tcb_hwcap, mask)));
15795 rtx scratch2 = gen_reg_rtx (SImode);
15796 emit_insn (gen_eqsi3 (scratch2, scratch1, const0_rtx));
15797 emit_insn (gen_rtx_SET (target, gen_rtx_XOR (SImode, scratch2, const1_rtx)));
15800 /* Record that we have expanded a CPU builtin, so that we can later
15801 emit a reference to the special symbol exported by LIBC to ensure we
15802 do not link against an old LIBC that doesn't support this feature. */
15803 cpu_builtin_p = true;
15805 #else
15806 /* For old LIBCs, always return FALSE. */
15807 emit_move_insn (target, GEN_INT (0));
15808 #endif /* TARGET_LIBC_PROVIDES_HWCAP_IN_TCB */
15810 return target;
15813 static rtx
15814 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
15816 rtx pat;
15817 tree arg0 = CALL_EXPR_ARG (exp, 0);
15818 tree arg1 = CALL_EXPR_ARG (exp, 1);
15819 tree arg2 = CALL_EXPR_ARG (exp, 2);
15820 rtx op0 = expand_normal (arg0);
15821 rtx op1 = expand_normal (arg1);
15822 rtx op2 = expand_normal (arg2);
15823 machine_mode tmode = insn_data[icode].operand[0].mode;
15824 machine_mode mode0 = insn_data[icode].operand[1].mode;
15825 machine_mode mode1 = insn_data[icode].operand[2].mode;
15826 machine_mode mode2 = insn_data[icode].operand[3].mode;
15828 if (icode == CODE_FOR_nothing)
15829 /* Builtin not supported on this processor. */
15830 return 0;
15832 /* If we got invalid arguments bail out before generating bad rtl. */
15833 if (arg0 == error_mark_node
15834 || arg1 == error_mark_node
15835 || arg2 == error_mark_node)
15836 return const0_rtx;
15838 /* Check and prepare argument depending on the instruction code.
15840 Note that a switch statement instead of the sequence of tests
15841 would be incorrect as many of the CODE_FOR values could be
15842 CODE_FOR_nothing and that would yield multiple alternatives
15843 with identical values. We'd never reach here at runtime in
15844 this case. */
15845 if (icode == CODE_FOR_altivec_vsldoi_v4sf
15846 || icode == CODE_FOR_altivec_vsldoi_v2df
15847 || icode == CODE_FOR_altivec_vsldoi_v4si
15848 || icode == CODE_FOR_altivec_vsldoi_v8hi
15849 || icode == CODE_FOR_altivec_vsldoi_v16qi)
15851 /* Only allow 4-bit unsigned literals. */
15852 STRIP_NOPS (arg2);
15853 if (TREE_CODE (arg2) != INTEGER_CST
15854 || TREE_INT_CST_LOW (arg2) & ~0xf)
15856 error ("argument 3 must be a 4-bit unsigned literal");
15857 return CONST0_RTX (tmode);
15860 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
15861 || icode == CODE_FOR_vsx_xxpermdi_v2di
15862 || icode == CODE_FOR_vsx_xxpermdi_v2df_be
15863 || icode == CODE_FOR_vsx_xxpermdi_v2di_be
15864 || icode == CODE_FOR_vsx_xxpermdi_v1ti
15865 || icode == CODE_FOR_vsx_xxpermdi_v4sf
15866 || icode == CODE_FOR_vsx_xxpermdi_v4si
15867 || icode == CODE_FOR_vsx_xxpermdi_v8hi
15868 || icode == CODE_FOR_vsx_xxpermdi_v16qi
15869 || icode == CODE_FOR_vsx_xxsldwi_v16qi
15870 || icode == CODE_FOR_vsx_xxsldwi_v8hi
15871 || icode == CODE_FOR_vsx_xxsldwi_v4si
15872 || icode == CODE_FOR_vsx_xxsldwi_v4sf
15873 || icode == CODE_FOR_vsx_xxsldwi_v2di
15874 || icode == CODE_FOR_vsx_xxsldwi_v2df)
15876 /* Only allow 2-bit unsigned literals. */
15877 STRIP_NOPS (arg2);
15878 if (TREE_CODE (arg2) != INTEGER_CST
15879 || TREE_INT_CST_LOW (arg2) & ~0x3)
15881 error ("argument 3 must be a 2-bit unsigned literal");
15882 return CONST0_RTX (tmode);
15885 else if (icode == CODE_FOR_vsx_set_v2df
15886 || icode == CODE_FOR_vsx_set_v2di
15887 || icode == CODE_FOR_bcdadd
15888 || icode == CODE_FOR_bcdadd_lt
15889 || icode == CODE_FOR_bcdadd_eq
15890 || icode == CODE_FOR_bcdadd_gt
15891 || icode == CODE_FOR_bcdsub
15892 || icode == CODE_FOR_bcdsub_lt
15893 || icode == CODE_FOR_bcdsub_eq
15894 || icode == CODE_FOR_bcdsub_gt)
15896 /* Only allow 1-bit unsigned literals. */
15897 STRIP_NOPS (arg2);
15898 if (TREE_CODE (arg2) != INTEGER_CST
15899 || TREE_INT_CST_LOW (arg2) & ~0x1)
15901 error ("argument 3 must be a 1-bit unsigned literal");
15902 return CONST0_RTX (tmode);
15905 else if (icode == CODE_FOR_dfp_ddedpd_dd
15906 || icode == CODE_FOR_dfp_ddedpd_td)
15908 /* Only allow 2-bit unsigned literals where the value is 0 or 2. */
15909 STRIP_NOPS (arg0);
15910 if (TREE_CODE (arg0) != INTEGER_CST
15911 || TREE_INT_CST_LOW (arg2) & ~0x3)
15913 error ("argument 1 must be 0 or 2");
15914 return CONST0_RTX (tmode);
15917 else if (icode == CODE_FOR_dfp_denbcd_dd
15918 || icode == CODE_FOR_dfp_denbcd_td)
15920 /* Only allow 1-bit unsigned literals. */
15921 STRIP_NOPS (arg0);
15922 if (TREE_CODE (arg0) != INTEGER_CST
15923 || TREE_INT_CST_LOW (arg0) & ~0x1)
15925 error ("argument 1 must be a 1-bit unsigned literal");
15926 return CONST0_RTX (tmode);
15929 else if (icode == CODE_FOR_dfp_dscli_dd
15930 || icode == CODE_FOR_dfp_dscli_td
15931 || icode == CODE_FOR_dfp_dscri_dd
15932 || icode == CODE_FOR_dfp_dscri_td)
15934 /* Only allow 6-bit unsigned literals. */
15935 STRIP_NOPS (arg1);
15936 if (TREE_CODE (arg1) != INTEGER_CST
15937 || TREE_INT_CST_LOW (arg1) & ~0x3f)
15939 error ("argument 2 must be a 6-bit unsigned literal");
15940 return CONST0_RTX (tmode);
15943 else if (icode == CODE_FOR_crypto_vshasigmaw
15944 || icode == CODE_FOR_crypto_vshasigmad)
15946 /* Check whether the 2nd and 3rd arguments are integer constants and in
15947 range and prepare arguments. */
15948 STRIP_NOPS (arg1);
15949 if (TREE_CODE (arg1) != INTEGER_CST || wi::geu_p (wi::to_wide (arg1), 2))
15951 error ("argument 2 must be 0 or 1");
15952 return CONST0_RTX (tmode);
15955 STRIP_NOPS (arg2);
15956 if (TREE_CODE (arg2) != INTEGER_CST
15957 || wi::geu_p (wi::to_wide (arg2), 16))
15959 error ("argument 3 must be in the range 0..15");
15960 return CONST0_RTX (tmode);
15964 if (target == 0
15965 || GET_MODE (target) != tmode
15966 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15967 target = gen_reg_rtx (tmode);
15969 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15970 op0 = copy_to_mode_reg (mode0, op0);
15971 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15972 op1 = copy_to_mode_reg (mode1, op1);
15973 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15974 op2 = copy_to_mode_reg (mode2, op2);
15976 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
15977 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
15978 else
15979 pat = GEN_FCN (icode) (target, op0, op1, op2);
15980 if (! pat)
15981 return 0;
15982 emit_insn (pat);
15984 return target;
15987 /* Expand the lvx builtins. */
15988 static rtx
15989 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
15991 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15992 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15993 tree arg0;
15994 machine_mode tmode, mode0;
15995 rtx pat, op0;
15996 enum insn_code icode;
15998 switch (fcode)
16000 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
16001 icode = CODE_FOR_vector_altivec_load_v16qi;
16002 break;
16003 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
16004 icode = CODE_FOR_vector_altivec_load_v8hi;
16005 break;
16006 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
16007 icode = CODE_FOR_vector_altivec_load_v4si;
16008 break;
16009 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
16010 icode = CODE_FOR_vector_altivec_load_v4sf;
16011 break;
16012 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
16013 icode = CODE_FOR_vector_altivec_load_v2df;
16014 break;
16015 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
16016 icode = CODE_FOR_vector_altivec_load_v2di;
16017 break;
16018 case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
16019 icode = CODE_FOR_vector_altivec_load_v1ti;
16020 break;
16021 default:
16022 *expandedp = false;
16023 return NULL_RTX;
16026 *expandedp = true;
16028 arg0 = CALL_EXPR_ARG (exp, 0);
16029 op0 = expand_normal (arg0);
16030 tmode = insn_data[icode].operand[0].mode;
16031 mode0 = insn_data[icode].operand[1].mode;
16033 if (target == 0
16034 || GET_MODE (target) != tmode
16035 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16036 target = gen_reg_rtx (tmode);
16038 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16039 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16041 pat = GEN_FCN (icode) (target, op0);
16042 if (! pat)
16043 return 0;
16044 emit_insn (pat);
16045 return target;
16048 /* Expand the stvx builtins. */
16049 static rtx
16050 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
16051 bool *expandedp)
16053 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16054 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
16055 tree arg0, arg1;
16056 machine_mode mode0, mode1;
16057 rtx pat, op0, op1;
16058 enum insn_code icode;
16060 switch (fcode)
16062 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
16063 icode = CODE_FOR_vector_altivec_store_v16qi;
16064 break;
16065 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
16066 icode = CODE_FOR_vector_altivec_store_v8hi;
16067 break;
16068 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
16069 icode = CODE_FOR_vector_altivec_store_v4si;
16070 break;
16071 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
16072 icode = CODE_FOR_vector_altivec_store_v4sf;
16073 break;
16074 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
16075 icode = CODE_FOR_vector_altivec_store_v2df;
16076 break;
16077 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
16078 icode = CODE_FOR_vector_altivec_store_v2di;
16079 break;
16080 case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
16081 icode = CODE_FOR_vector_altivec_store_v1ti;
16082 break;
16083 default:
16084 *expandedp = false;
16085 return NULL_RTX;
16088 arg0 = CALL_EXPR_ARG (exp, 0);
16089 arg1 = CALL_EXPR_ARG (exp, 1);
16090 op0 = expand_normal (arg0);
16091 op1 = expand_normal (arg1);
16092 mode0 = insn_data[icode].operand[0].mode;
16093 mode1 = insn_data[icode].operand[1].mode;
16095 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16096 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16097 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
16098 op1 = copy_to_mode_reg (mode1, op1);
16100 pat = GEN_FCN (icode) (op0, op1);
16101 if (pat)
16102 emit_insn (pat);
16104 *expandedp = true;
16105 return NULL_RTX;
16108 /* Expand the dst builtins. */
16109 static rtx
16110 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
16111 bool *expandedp)
16113 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16114 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16115 tree arg0, arg1, arg2;
16116 machine_mode mode0, mode1;
16117 rtx pat, op0, op1, op2;
16118 const struct builtin_description *d;
16119 size_t i;
16121 *expandedp = false;
16123 /* Handle DST variants. */
16124 d = bdesc_dst;
16125 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
16126 if (d->code == fcode)
16128 arg0 = CALL_EXPR_ARG (exp, 0);
16129 arg1 = CALL_EXPR_ARG (exp, 1);
16130 arg2 = CALL_EXPR_ARG (exp, 2);
16131 op0 = expand_normal (arg0);
16132 op1 = expand_normal (arg1);
16133 op2 = expand_normal (arg2);
16134 mode0 = insn_data[d->icode].operand[0].mode;
16135 mode1 = insn_data[d->icode].operand[1].mode;
16137 /* Invalid arguments, bail out before generating bad rtl. */
16138 if (arg0 == error_mark_node
16139 || arg1 == error_mark_node
16140 || arg2 == error_mark_node)
16141 return const0_rtx;
16143 *expandedp = true;
16144 STRIP_NOPS (arg2);
16145 if (TREE_CODE (arg2) != INTEGER_CST
16146 || TREE_INT_CST_LOW (arg2) & ~0x3)
16148 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
16149 return const0_rtx;
16152 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
16153 op0 = copy_to_mode_reg (Pmode, op0);
16154 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
16155 op1 = copy_to_mode_reg (mode1, op1);
16157 pat = GEN_FCN (d->icode) (op0, op1, op2);
16158 if (pat != 0)
16159 emit_insn (pat);
16161 return NULL_RTX;
16164 return NULL_RTX;
16167 /* Expand vec_init builtin. */
16168 static rtx
16169 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
16171 machine_mode tmode = TYPE_MODE (type);
16172 machine_mode inner_mode = GET_MODE_INNER (tmode);
16173 int i, n_elt = GET_MODE_NUNITS (tmode);
16175 gcc_assert (VECTOR_MODE_P (tmode));
16176 gcc_assert (n_elt == call_expr_nargs (exp));
16178 if (!target || !register_operand (target, tmode))
16179 target = gen_reg_rtx (tmode);
16181 /* If we have a vector compromised of a single element, such as V1TImode, do
16182 the initialization directly. */
16183 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
16185 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
16186 emit_move_insn (target, gen_lowpart (tmode, x));
16188 else
16190 rtvec v = rtvec_alloc (n_elt);
16192 for (i = 0; i < n_elt; ++i)
16194 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
16195 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
16198 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
16201 return target;
16204 /* Return the integer constant in ARG. Constrain it to be in the range
16205 of the subparts of VEC_TYPE; issue an error if not. */
16207 static int
16208 get_element_number (tree vec_type, tree arg)
16210 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
16212 if (!tree_fits_uhwi_p (arg)
16213 || (elt = tree_to_uhwi (arg), elt > max))
16215 error ("selector must be an integer constant in the range 0..%wi", max);
16216 return 0;
16219 return elt;
16222 /* Expand vec_set builtin. */
16223 static rtx
16224 altivec_expand_vec_set_builtin (tree exp)
16226 machine_mode tmode, mode1;
16227 tree arg0, arg1, arg2;
16228 int elt;
16229 rtx op0, op1;
16231 arg0 = CALL_EXPR_ARG (exp, 0);
16232 arg1 = CALL_EXPR_ARG (exp, 1);
16233 arg2 = CALL_EXPR_ARG (exp, 2);
16235 tmode = TYPE_MODE (TREE_TYPE (arg0));
16236 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
16237 gcc_assert (VECTOR_MODE_P (tmode));
16239 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
16240 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
16241 elt = get_element_number (TREE_TYPE (arg0), arg2);
16243 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
16244 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
16246 op0 = force_reg (tmode, op0);
16247 op1 = force_reg (mode1, op1);
16249 rs6000_expand_vector_set (op0, op1, elt);
16251 return op0;
16254 /* Expand vec_ext builtin. */
16255 static rtx
16256 altivec_expand_vec_ext_builtin (tree exp, rtx target)
16258 machine_mode tmode, mode0;
16259 tree arg0, arg1;
16260 rtx op0;
16261 rtx op1;
16263 arg0 = CALL_EXPR_ARG (exp, 0);
16264 arg1 = CALL_EXPR_ARG (exp, 1);
16266 op0 = expand_normal (arg0);
16267 op1 = expand_normal (arg1);
16269 /* Call get_element_number to validate arg1 if it is a constant. */
16270 if (TREE_CODE (arg1) == INTEGER_CST)
16271 (void) get_element_number (TREE_TYPE (arg0), arg1);
16273 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
16274 mode0 = TYPE_MODE (TREE_TYPE (arg0));
16275 gcc_assert (VECTOR_MODE_P (mode0));
16277 op0 = force_reg (mode0, op0);
16279 if (optimize || !target || !register_operand (target, tmode))
16280 target = gen_reg_rtx (tmode);
16282 rs6000_expand_vector_extract (target, op0, op1);
16284 return target;
16287 /* Expand the builtin in EXP and store the result in TARGET. Store
16288 true in *EXPANDEDP if we found a builtin to expand. */
16289 static rtx
16290 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
16292 const struct builtin_description *d;
16293 size_t i;
16294 enum insn_code icode;
16295 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16296 tree arg0, arg1, arg2;
16297 rtx op0, pat;
16298 machine_mode tmode, mode0;
16299 enum rs6000_builtins fcode
16300 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16302 if (rs6000_overloaded_builtin_p (fcode))
16304 *expandedp = true;
16305 error ("unresolved overload for Altivec builtin %qF", fndecl);
16307 /* Given it is invalid, just generate a normal call. */
16308 return expand_call (exp, target, false);
16311 target = altivec_expand_ld_builtin (exp, target, expandedp);
16312 if (*expandedp)
16313 return target;
16315 target = altivec_expand_st_builtin (exp, target, expandedp);
16316 if (*expandedp)
16317 return target;
16319 target = altivec_expand_dst_builtin (exp, target, expandedp);
16320 if (*expandedp)
16321 return target;
16323 *expandedp = true;
16325 switch (fcode)
16327 case ALTIVEC_BUILTIN_STVX_V2DF:
16328 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df_2op, exp);
16329 case ALTIVEC_BUILTIN_STVX_V2DI:
16330 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di_2op, exp);
16331 case ALTIVEC_BUILTIN_STVX_V4SF:
16332 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf_2op, exp);
16333 case ALTIVEC_BUILTIN_STVX:
16334 case ALTIVEC_BUILTIN_STVX_V4SI:
16335 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si_2op, exp);
16336 case ALTIVEC_BUILTIN_STVX_V8HI:
16337 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi_2op, exp);
16338 case ALTIVEC_BUILTIN_STVX_V16QI:
16339 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi_2op, exp);
16340 case ALTIVEC_BUILTIN_STVEBX:
16341 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
16342 case ALTIVEC_BUILTIN_STVEHX:
16343 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
16344 case ALTIVEC_BUILTIN_STVEWX:
16345 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
16346 case ALTIVEC_BUILTIN_STVXL_V2DF:
16347 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
16348 case ALTIVEC_BUILTIN_STVXL_V2DI:
16349 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
16350 case ALTIVEC_BUILTIN_STVXL_V4SF:
16351 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
16352 case ALTIVEC_BUILTIN_STVXL:
16353 case ALTIVEC_BUILTIN_STVXL_V4SI:
16354 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
16355 case ALTIVEC_BUILTIN_STVXL_V8HI:
16356 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
16357 case ALTIVEC_BUILTIN_STVXL_V16QI:
16358 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
16360 case ALTIVEC_BUILTIN_STVLX:
16361 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
16362 case ALTIVEC_BUILTIN_STVLXL:
16363 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
16364 case ALTIVEC_BUILTIN_STVRX:
16365 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
16366 case ALTIVEC_BUILTIN_STVRXL:
16367 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
16369 case P9V_BUILTIN_STXVL:
16370 return altivec_expand_stxvl_builtin (CODE_FOR_stxvl, exp);
16372 case VSX_BUILTIN_STXVD2X_V1TI:
16373 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
16374 case VSX_BUILTIN_STXVD2X_V2DF:
16375 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
16376 case VSX_BUILTIN_STXVD2X_V2DI:
16377 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
16378 case VSX_BUILTIN_STXVW4X_V4SF:
16379 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
16380 case VSX_BUILTIN_STXVW4X_V4SI:
16381 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
16382 case VSX_BUILTIN_STXVW4X_V8HI:
16383 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
16384 case VSX_BUILTIN_STXVW4X_V16QI:
16385 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
16387 /* For the following on big endian, it's ok to use any appropriate
16388 unaligned-supporting store, so use a generic expander. For
16389 little-endian, the exact element-reversing instruction must
16390 be used. */
16391 case VSX_BUILTIN_ST_ELEMREV_V2DF:
16393 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2df
16394 : CODE_FOR_vsx_st_elemrev_v2df);
16395 return altivec_expand_stv_builtin (code, exp);
16397 case VSX_BUILTIN_ST_ELEMREV_V2DI:
16399 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2di
16400 : CODE_FOR_vsx_st_elemrev_v2di);
16401 return altivec_expand_stv_builtin (code, exp);
16403 case VSX_BUILTIN_ST_ELEMREV_V4SF:
16405 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4sf
16406 : CODE_FOR_vsx_st_elemrev_v4sf);
16407 return altivec_expand_stv_builtin (code, exp);
16409 case VSX_BUILTIN_ST_ELEMREV_V4SI:
16411 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4si
16412 : CODE_FOR_vsx_st_elemrev_v4si);
16413 return altivec_expand_stv_builtin (code, exp);
16415 case VSX_BUILTIN_ST_ELEMREV_V8HI:
16417 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v8hi
16418 : CODE_FOR_vsx_st_elemrev_v8hi);
16419 return altivec_expand_stv_builtin (code, exp);
16421 case VSX_BUILTIN_ST_ELEMREV_V16QI:
16423 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v16qi
16424 : CODE_FOR_vsx_st_elemrev_v16qi);
16425 return altivec_expand_stv_builtin (code, exp);
16428 case ALTIVEC_BUILTIN_MFVSCR:
16429 icode = CODE_FOR_altivec_mfvscr;
16430 tmode = insn_data[icode].operand[0].mode;
16432 if (target == 0
16433 || GET_MODE (target) != tmode
16434 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16435 target = gen_reg_rtx (tmode);
16437 pat = GEN_FCN (icode) (target);
16438 if (! pat)
16439 return 0;
16440 emit_insn (pat);
16441 return target;
16443 case ALTIVEC_BUILTIN_MTVSCR:
16444 icode = CODE_FOR_altivec_mtvscr;
16445 arg0 = CALL_EXPR_ARG (exp, 0);
16446 op0 = expand_normal (arg0);
16447 mode0 = insn_data[icode].operand[0].mode;
16449 /* If we got invalid arguments bail out before generating bad rtl. */
16450 if (arg0 == error_mark_node)
16451 return const0_rtx;
16453 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16454 op0 = copy_to_mode_reg (mode0, op0);
16456 pat = GEN_FCN (icode) (op0);
16457 if (pat)
16458 emit_insn (pat);
16459 return NULL_RTX;
16461 case ALTIVEC_BUILTIN_DSSALL:
16462 emit_insn (gen_altivec_dssall ());
16463 return NULL_RTX;
16465 case ALTIVEC_BUILTIN_DSS:
16466 icode = CODE_FOR_altivec_dss;
16467 arg0 = CALL_EXPR_ARG (exp, 0);
16468 STRIP_NOPS (arg0);
16469 op0 = expand_normal (arg0);
16470 mode0 = insn_data[icode].operand[0].mode;
16472 /* If we got invalid arguments bail out before generating bad rtl. */
16473 if (arg0 == error_mark_node)
16474 return const0_rtx;
16476 if (TREE_CODE (arg0) != INTEGER_CST
16477 || TREE_INT_CST_LOW (arg0) & ~0x3)
16479 error ("argument to dss must be a 2-bit unsigned literal");
16480 return const0_rtx;
16483 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16484 op0 = copy_to_mode_reg (mode0, op0);
16486 emit_insn (gen_altivec_dss (op0));
16487 return NULL_RTX;
16489 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
16490 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
16491 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
16492 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
16493 case VSX_BUILTIN_VEC_INIT_V2DF:
16494 case VSX_BUILTIN_VEC_INIT_V2DI:
16495 case VSX_BUILTIN_VEC_INIT_V1TI:
16496 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
16498 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
16499 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
16500 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
16501 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
16502 case VSX_BUILTIN_VEC_SET_V2DF:
16503 case VSX_BUILTIN_VEC_SET_V2DI:
16504 case VSX_BUILTIN_VEC_SET_V1TI:
16505 return altivec_expand_vec_set_builtin (exp);
16507 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
16508 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
16509 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
16510 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
16511 case VSX_BUILTIN_VEC_EXT_V2DF:
16512 case VSX_BUILTIN_VEC_EXT_V2DI:
16513 case VSX_BUILTIN_VEC_EXT_V1TI:
16514 return altivec_expand_vec_ext_builtin (exp, target);
16516 case P9V_BUILTIN_VEXTRACT4B:
16517 case P9V_BUILTIN_VEC_VEXTRACT4B:
16518 arg1 = CALL_EXPR_ARG (exp, 1);
16519 STRIP_NOPS (arg1);
16521 /* Generate a normal call if it is invalid. */
16522 if (arg1 == error_mark_node)
16523 return expand_call (exp, target, false);
16525 if (TREE_CODE (arg1) != INTEGER_CST || TREE_INT_CST_LOW (arg1) > 12)
16527 error ("second argument to vec_vextract4b must be 0..12");
16528 return expand_call (exp, target, false);
16530 break;
16532 case P9V_BUILTIN_VINSERT4B:
16533 case P9V_BUILTIN_VINSERT4B_DI:
16534 case P9V_BUILTIN_VEC_VINSERT4B:
16535 arg2 = CALL_EXPR_ARG (exp, 2);
16536 STRIP_NOPS (arg2);
16538 /* Generate a normal call if it is invalid. */
16539 if (arg2 == error_mark_node)
16540 return expand_call (exp, target, false);
16542 if (TREE_CODE (arg2) != INTEGER_CST || TREE_INT_CST_LOW (arg2) > 12)
16544 error ("third argument to vec_vinsert4b must be 0..12");
16545 return expand_call (exp, target, false);
16547 break;
16549 default:
16550 break;
16551 /* Fall through. */
16554 /* Expand abs* operations. */
16555 d = bdesc_abs;
16556 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
16557 if (d->code == fcode)
16558 return altivec_expand_abs_builtin (d->icode, exp, target);
16560 /* Expand the AltiVec predicates. */
16561 d = bdesc_altivec_preds;
16562 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
16563 if (d->code == fcode)
16564 return altivec_expand_predicate_builtin (d->icode, exp, target);
16566 /* LV* are funky. We initialized them differently. */
16567 switch (fcode)
16569 case ALTIVEC_BUILTIN_LVSL:
16570 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
16571 exp, target, false);
16572 case ALTIVEC_BUILTIN_LVSR:
16573 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
16574 exp, target, false);
16575 case ALTIVEC_BUILTIN_LVEBX:
16576 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
16577 exp, target, false);
16578 case ALTIVEC_BUILTIN_LVEHX:
16579 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
16580 exp, target, false);
16581 case ALTIVEC_BUILTIN_LVEWX:
16582 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
16583 exp, target, false);
16584 case ALTIVEC_BUILTIN_LVXL_V2DF:
16585 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
16586 exp, target, false);
16587 case ALTIVEC_BUILTIN_LVXL_V2DI:
16588 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
16589 exp, target, false);
16590 case ALTIVEC_BUILTIN_LVXL_V4SF:
16591 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
16592 exp, target, false);
16593 case ALTIVEC_BUILTIN_LVXL:
16594 case ALTIVEC_BUILTIN_LVXL_V4SI:
16595 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
16596 exp, target, false);
16597 case ALTIVEC_BUILTIN_LVXL_V8HI:
16598 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
16599 exp, target, false);
16600 case ALTIVEC_BUILTIN_LVXL_V16QI:
16601 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
16602 exp, target, false);
16603 case ALTIVEC_BUILTIN_LVX_V2DF:
16604 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df_2op,
16605 exp, target, false);
16606 case ALTIVEC_BUILTIN_LVX_V2DI:
16607 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di_2op,
16608 exp, target, false);
16609 case ALTIVEC_BUILTIN_LVX_V4SF:
16610 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf_2op,
16611 exp, target, false);
16612 case ALTIVEC_BUILTIN_LVX:
16613 case ALTIVEC_BUILTIN_LVX_V4SI:
16614 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si_2op,
16615 exp, target, false);
16616 case ALTIVEC_BUILTIN_LVX_V8HI:
16617 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi_2op,
16618 exp, target, false);
16619 case ALTIVEC_BUILTIN_LVX_V16QI:
16620 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi_2op,
16621 exp, target, false);
16622 case ALTIVEC_BUILTIN_LVLX:
16623 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
16624 exp, target, true);
16625 case ALTIVEC_BUILTIN_LVLXL:
16626 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
16627 exp, target, true);
16628 case ALTIVEC_BUILTIN_LVRX:
16629 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
16630 exp, target, true);
16631 case ALTIVEC_BUILTIN_LVRXL:
16632 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
16633 exp, target, true);
16634 case VSX_BUILTIN_LXVD2X_V1TI:
16635 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
16636 exp, target, false);
16637 case VSX_BUILTIN_LXVD2X_V2DF:
16638 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
16639 exp, target, false);
16640 case VSX_BUILTIN_LXVD2X_V2DI:
16641 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
16642 exp, target, false);
16643 case VSX_BUILTIN_LXVW4X_V4SF:
16644 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
16645 exp, target, false);
16646 case VSX_BUILTIN_LXVW4X_V4SI:
16647 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
16648 exp, target, false);
16649 case VSX_BUILTIN_LXVW4X_V8HI:
16650 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
16651 exp, target, false);
16652 case VSX_BUILTIN_LXVW4X_V16QI:
16653 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
16654 exp, target, false);
16655 /* For the following on big endian, it's ok to use any appropriate
16656 unaligned-supporting load, so use a generic expander. For
16657 little-endian, the exact element-reversing instruction must
16658 be used. */
16659 case VSX_BUILTIN_LD_ELEMREV_V2DF:
16661 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2df
16662 : CODE_FOR_vsx_ld_elemrev_v2df);
16663 return altivec_expand_lv_builtin (code, exp, target, false);
16665 case VSX_BUILTIN_LD_ELEMREV_V2DI:
16667 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2di
16668 : CODE_FOR_vsx_ld_elemrev_v2di);
16669 return altivec_expand_lv_builtin (code, exp, target, false);
16671 case VSX_BUILTIN_LD_ELEMREV_V4SF:
16673 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4sf
16674 : CODE_FOR_vsx_ld_elemrev_v4sf);
16675 return altivec_expand_lv_builtin (code, exp, target, false);
16677 case VSX_BUILTIN_LD_ELEMREV_V4SI:
16679 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4si
16680 : CODE_FOR_vsx_ld_elemrev_v4si);
16681 return altivec_expand_lv_builtin (code, exp, target, false);
16683 case VSX_BUILTIN_LD_ELEMREV_V8HI:
16685 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v8hi
16686 : CODE_FOR_vsx_ld_elemrev_v8hi);
16687 return altivec_expand_lv_builtin (code, exp, target, false);
16689 case VSX_BUILTIN_LD_ELEMREV_V16QI:
16691 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v16qi
16692 : CODE_FOR_vsx_ld_elemrev_v16qi);
16693 return altivec_expand_lv_builtin (code, exp, target, false);
16695 break;
16696 default:
16697 break;
16698 /* Fall through. */
16701 *expandedp = false;
16702 return NULL_RTX;
16705 /* Expand the builtin in EXP and store the result in TARGET. Store
16706 true in *EXPANDEDP if we found a builtin to expand. */
16707 static rtx
16708 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
16710 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16711 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16712 const struct builtin_description *d;
16713 size_t i;
16715 *expandedp = true;
16717 switch (fcode)
16719 case PAIRED_BUILTIN_STX:
16720 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
16721 case PAIRED_BUILTIN_LX:
16722 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
16723 default:
16724 break;
16725 /* Fall through. */
16728 /* Expand the paired predicates. */
16729 d = bdesc_paired_preds;
16730 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
16731 if (d->code == fcode)
16732 return paired_expand_predicate_builtin (d->icode, exp, target);
16734 *expandedp = false;
16735 return NULL_RTX;
16738 /* Binops that need to be initialized manually, but can be expanded
16739 automagically by rs6000_expand_binop_builtin. */
16740 static const struct builtin_description bdesc_2arg_spe[] =
16742 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
16743 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
16744 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
16745 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
16746 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
16747 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
16748 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
16749 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
16750 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
16751 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
16752 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
16753 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
16754 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
16755 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
16756 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
16757 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
16758 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
16759 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
16760 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
16761 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
16762 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
16763 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
16766 /* Expand the builtin in EXP and store the result in TARGET. Store
16767 true in *EXPANDEDP if we found a builtin to expand.
16769 This expands the SPE builtins that are not simple unary and binary
16770 operations. */
16771 static rtx
16772 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
16774 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16775 tree arg1, arg0;
16776 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16777 enum insn_code icode;
16778 machine_mode tmode, mode0;
16779 rtx pat, op0;
16780 const struct builtin_description *d;
16781 size_t i;
16783 *expandedp = true;
16785 /* Syntax check for a 5-bit unsigned immediate. */
16786 switch (fcode)
16788 case SPE_BUILTIN_EVSTDD:
16789 case SPE_BUILTIN_EVSTDH:
16790 case SPE_BUILTIN_EVSTDW:
16791 case SPE_BUILTIN_EVSTWHE:
16792 case SPE_BUILTIN_EVSTWHO:
16793 case SPE_BUILTIN_EVSTWWE:
16794 case SPE_BUILTIN_EVSTWWO:
16795 arg1 = CALL_EXPR_ARG (exp, 2);
16796 if (TREE_CODE (arg1) != INTEGER_CST
16797 || TREE_INT_CST_LOW (arg1) & ~0x1f)
16799 error ("argument 2 must be a 5-bit unsigned literal");
16800 return const0_rtx;
16802 break;
16803 default:
16804 break;
16807 /* The evsplat*i instructions are not quite generic. */
16808 switch (fcode)
16810 case SPE_BUILTIN_EVSPLATFI:
16811 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
16812 exp, target);
16813 case SPE_BUILTIN_EVSPLATI:
16814 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
16815 exp, target);
16816 default:
16817 break;
16820 d = bdesc_2arg_spe;
16821 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
16822 if (d->code == fcode)
16823 return rs6000_expand_binop_builtin (d->icode, exp, target);
16825 d = bdesc_spe_predicates;
16826 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
16827 if (d->code == fcode)
16828 return spe_expand_predicate_builtin (d->icode, exp, target);
16830 d = bdesc_spe_evsel;
16831 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
16832 if (d->code == fcode)
16833 return spe_expand_evsel_builtin (d->icode, exp, target);
16835 switch (fcode)
16837 case SPE_BUILTIN_EVSTDDX:
16838 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
16839 case SPE_BUILTIN_EVSTDHX:
16840 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
16841 case SPE_BUILTIN_EVSTDWX:
16842 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
16843 case SPE_BUILTIN_EVSTWHEX:
16844 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
16845 case SPE_BUILTIN_EVSTWHOX:
16846 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
16847 case SPE_BUILTIN_EVSTWWEX:
16848 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
16849 case SPE_BUILTIN_EVSTWWOX:
16850 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
16851 case SPE_BUILTIN_EVSTDD:
16852 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
16853 case SPE_BUILTIN_EVSTDH:
16854 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
16855 case SPE_BUILTIN_EVSTDW:
16856 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
16857 case SPE_BUILTIN_EVSTWHE:
16858 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
16859 case SPE_BUILTIN_EVSTWHO:
16860 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
16861 case SPE_BUILTIN_EVSTWWE:
16862 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
16863 case SPE_BUILTIN_EVSTWWO:
16864 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
16865 case SPE_BUILTIN_MFSPEFSCR:
16866 icode = CODE_FOR_spe_mfspefscr;
16867 tmode = insn_data[icode].operand[0].mode;
16869 if (target == 0
16870 || GET_MODE (target) != tmode
16871 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16872 target = gen_reg_rtx (tmode);
16874 pat = GEN_FCN (icode) (target);
16875 if (! pat)
16876 return 0;
16877 emit_insn (pat);
16878 return target;
16879 case SPE_BUILTIN_MTSPEFSCR:
16880 icode = CODE_FOR_spe_mtspefscr;
16881 arg0 = CALL_EXPR_ARG (exp, 0);
16882 op0 = expand_normal (arg0);
16883 mode0 = insn_data[icode].operand[0].mode;
16885 if (arg0 == error_mark_node)
16886 return const0_rtx;
16888 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16889 op0 = copy_to_mode_reg (mode0, op0);
16891 pat = GEN_FCN (icode) (op0);
16892 if (pat)
16893 emit_insn (pat);
16894 return NULL_RTX;
16895 default:
16896 break;
16899 *expandedp = false;
16900 return NULL_RTX;
16903 static rtx
16904 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
16906 rtx pat, scratch, tmp;
16907 tree form = CALL_EXPR_ARG (exp, 0);
16908 tree arg0 = CALL_EXPR_ARG (exp, 1);
16909 tree arg1 = CALL_EXPR_ARG (exp, 2);
16910 rtx op0 = expand_normal (arg0);
16911 rtx op1 = expand_normal (arg1);
16912 machine_mode mode0 = insn_data[icode].operand[1].mode;
16913 machine_mode mode1 = insn_data[icode].operand[2].mode;
16914 int form_int;
16915 enum rtx_code code;
16917 if (TREE_CODE (form) != INTEGER_CST)
16919 error ("argument 1 of __builtin_paired_predicate must be a constant");
16920 return const0_rtx;
16922 else
16923 form_int = TREE_INT_CST_LOW (form);
16925 gcc_assert (mode0 == mode1);
16927 if (arg0 == error_mark_node || arg1 == error_mark_node)
16928 return const0_rtx;
16930 if (target == 0
16931 || GET_MODE (target) != SImode
16932 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
16933 target = gen_reg_rtx (SImode);
16934 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
16935 op0 = copy_to_mode_reg (mode0, op0);
16936 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
16937 op1 = copy_to_mode_reg (mode1, op1);
16939 scratch = gen_reg_rtx (CCFPmode);
16941 pat = GEN_FCN (icode) (scratch, op0, op1);
16942 if (!pat)
16943 return const0_rtx;
16945 emit_insn (pat);
16947 switch (form_int)
16949 /* LT bit. */
16950 case 0:
16951 code = LT;
16952 break;
16953 /* GT bit. */
16954 case 1:
16955 code = GT;
16956 break;
16957 /* EQ bit. */
16958 case 2:
16959 code = EQ;
16960 break;
16961 /* UN bit. */
16962 case 3:
16963 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
16964 return target;
16965 default:
16966 error ("argument 1 of __builtin_paired_predicate is out of range");
16967 return const0_rtx;
16970 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
16971 emit_move_insn (target, tmp);
16972 return target;
16975 static rtx
16976 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
16978 rtx pat, scratch, tmp;
16979 tree form = CALL_EXPR_ARG (exp, 0);
16980 tree arg0 = CALL_EXPR_ARG (exp, 1);
16981 tree arg1 = CALL_EXPR_ARG (exp, 2);
16982 rtx op0 = expand_normal (arg0);
16983 rtx op1 = expand_normal (arg1);
16984 machine_mode mode0 = insn_data[icode].operand[1].mode;
16985 machine_mode mode1 = insn_data[icode].operand[2].mode;
16986 int form_int;
16987 enum rtx_code code;
16989 if (TREE_CODE (form) != INTEGER_CST)
16991 error ("argument 1 of __builtin_spe_predicate must be a constant");
16992 return const0_rtx;
16994 else
16995 form_int = TREE_INT_CST_LOW (form);
16997 gcc_assert (mode0 == mode1);
16999 if (arg0 == error_mark_node || arg1 == error_mark_node)
17000 return const0_rtx;
17002 if (target == 0
17003 || GET_MODE (target) != SImode
17004 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
17005 target = gen_reg_rtx (SImode);
17007 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17008 op0 = copy_to_mode_reg (mode0, op0);
17009 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
17010 op1 = copy_to_mode_reg (mode1, op1);
17012 scratch = gen_reg_rtx (CCmode);
17014 pat = GEN_FCN (icode) (scratch, op0, op1);
17015 if (! pat)
17016 return const0_rtx;
17017 emit_insn (pat);
17019 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
17020 _lower_. We use one compare, but look in different bits of the
17021 CR for each variant.
17023 There are 2 elements in each SPE simd type (upper/lower). The CR
17024 bits are set as follows:
17026 BIT0 | BIT 1 | BIT 2 | BIT 3
17027 U | L | (U | L) | (U & L)
17029 So, for an "all" relationship, BIT 3 would be set.
17030 For an "any" relationship, BIT 2 would be set. Etc.
17032 Following traditional nomenclature, these bits map to:
17034 BIT0 | BIT 1 | BIT 2 | BIT 3
17035 LT | GT | EQ | OV
17037 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
17040 switch (form_int)
17042 /* All variant. OV bit. */
17043 case 0:
17044 /* We need to get to the OV bit, which is the ORDERED bit. We
17045 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
17046 that's ugly and will make validate_condition_mode die.
17047 So let's just use another pattern. */
17048 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
17049 return target;
17050 /* Any variant. EQ bit. */
17051 case 1:
17052 code = EQ;
17053 break;
17054 /* Upper variant. LT bit. */
17055 case 2:
17056 code = LT;
17057 break;
17058 /* Lower variant. GT bit. */
17059 case 3:
17060 code = GT;
17061 break;
17062 default:
17063 error ("argument 1 of __builtin_spe_predicate is out of range");
17064 return const0_rtx;
17067 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
17068 emit_move_insn (target, tmp);
17070 return target;
17073 /* The evsel builtins look like this:
17075 e = __builtin_spe_evsel_OP (a, b, c, d);
17077 and work like this:
17079 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
17080 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
17083 static rtx
17084 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
17086 rtx pat, scratch;
17087 tree arg0 = CALL_EXPR_ARG (exp, 0);
17088 tree arg1 = CALL_EXPR_ARG (exp, 1);
17089 tree arg2 = CALL_EXPR_ARG (exp, 2);
17090 tree arg3 = CALL_EXPR_ARG (exp, 3);
17091 rtx op0 = expand_normal (arg0);
17092 rtx op1 = expand_normal (arg1);
17093 rtx op2 = expand_normal (arg2);
17094 rtx op3 = expand_normal (arg3);
17095 machine_mode mode0 = insn_data[icode].operand[1].mode;
17096 machine_mode mode1 = insn_data[icode].operand[2].mode;
17098 gcc_assert (mode0 == mode1);
17100 if (arg0 == error_mark_node || arg1 == error_mark_node
17101 || arg2 == error_mark_node || arg3 == error_mark_node)
17102 return const0_rtx;
17104 if (target == 0
17105 || GET_MODE (target) != mode0
17106 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
17107 target = gen_reg_rtx (mode0);
17109 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17110 op0 = copy_to_mode_reg (mode0, op0);
17111 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
17112 op1 = copy_to_mode_reg (mode0, op1);
17113 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
17114 op2 = copy_to_mode_reg (mode0, op2);
17115 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
17116 op3 = copy_to_mode_reg (mode0, op3);
17118 /* Generate the compare. */
17119 scratch = gen_reg_rtx (CCmode);
17120 pat = GEN_FCN (icode) (scratch, op0, op1);
17121 if (! pat)
17122 return const0_rtx;
17123 emit_insn (pat);
17125 if (mode0 == V2SImode)
17126 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
17127 else
17128 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
17130 return target;
17133 /* Raise an error message for a builtin function that is called without the
17134 appropriate target options being set. */
17136 static void
17137 rs6000_invalid_builtin (enum rs6000_builtins fncode)
17139 size_t uns_fncode = (size_t)fncode;
17140 const char *name = rs6000_builtin_info[uns_fncode].name;
17141 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
17143 gcc_assert (name != NULL);
17144 if ((fnmask & RS6000_BTM_CELL) != 0)
17145 error ("Builtin function %s is only valid for the cell processor", name);
17146 else if ((fnmask & RS6000_BTM_VSX) != 0)
17147 error ("Builtin function %s requires the -mvsx option", name);
17148 else if ((fnmask & RS6000_BTM_HTM) != 0)
17149 error ("Builtin function %s requires the -mhtm option", name);
17150 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
17151 error ("Builtin function %s requires the -maltivec option", name);
17152 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
17153 error ("Builtin function %s requires the -mpaired option", name);
17154 else if ((fnmask & RS6000_BTM_SPE) != 0)
17155 error ("Builtin function %s requires the -mspe option", name);
17156 else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
17157 == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
17158 error ("Builtin function %s requires the -mhard-dfp and"
17159 " -mpower8-vector options", name);
17160 else if ((fnmask & RS6000_BTM_DFP) != 0)
17161 error ("Builtin function %s requires the -mhard-dfp option", name);
17162 else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
17163 error ("Builtin function %s requires the -mpower8-vector option", name);
17164 else if ((fnmask & (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
17165 == (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
17166 error ("Builtin function %s requires the -mcpu=power9 and"
17167 " -m64 options", name);
17168 else if ((fnmask & RS6000_BTM_P9_VECTOR) != 0)
17169 error ("Builtin function %s requires the -mcpu=power9 option", name);
17170 else if ((fnmask & (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
17171 == (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
17172 error ("Builtin function %s requires the -mcpu=power9 and"
17173 " -m64 options", name);
17174 else if ((fnmask & RS6000_BTM_P9_MISC) == RS6000_BTM_P9_MISC)
17175 error ("Builtin function %s requires the -mcpu=power9 option", name);
17176 else if ((fnmask & (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
17177 == (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
17178 error ("Builtin function %s requires the -mhard-float and"
17179 " -mlong-double-128 options", name);
17180 else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
17181 error ("Builtin function %s requires the -mhard-float option", name);
17182 else if ((fnmask & RS6000_BTM_FLOAT128) != 0)
17183 error ("Builtin function %s requires the -mfloat128 option", name);
17184 else
17185 error ("Builtin function %s is not supported with the current options",
17186 name);
17189 /* Target hook for early folding of built-ins, shamelessly stolen
17190 from ia64.c. */
17192 static tree
17193 rs6000_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED,
17194 tree *args, bool ignore ATTRIBUTE_UNUSED)
17196 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
17198 enum rs6000_builtins fn_code
17199 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
17200 switch (fn_code)
17202 case RS6000_BUILTIN_NANQ:
17203 case RS6000_BUILTIN_NANSQ:
17205 tree type = TREE_TYPE (TREE_TYPE (fndecl));
17206 const char *str = c_getstr (*args);
17207 int quiet = fn_code == RS6000_BUILTIN_NANQ;
17208 REAL_VALUE_TYPE real;
17210 if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
17211 return build_real (type, real);
17212 return NULL_TREE;
17214 case RS6000_BUILTIN_INFQ:
17215 case RS6000_BUILTIN_HUGE_VALQ:
17217 tree type = TREE_TYPE (TREE_TYPE (fndecl));
17218 REAL_VALUE_TYPE inf;
17219 real_inf (&inf);
17220 return build_real (type, inf);
17222 default:
17223 break;
17226 #ifdef SUBTARGET_FOLD_BUILTIN
17227 return SUBTARGET_FOLD_BUILTIN (fndecl, n_args, args, ignore);
17228 #else
17229 return NULL_TREE;
17230 #endif
17233 /* Fold a machine-dependent built-in in GIMPLE. (For folding into
17234 a constant, use rs6000_fold_builtin.) */
17236 bool
17237 rs6000_gimple_fold_builtin (gimple_stmt_iterator *gsi)
17239 gimple *stmt = gsi_stmt (*gsi);
17240 tree fndecl = gimple_call_fndecl (stmt);
17241 gcc_checking_assert (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD);
17242 enum rs6000_builtins fn_code
17243 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
17244 tree arg0, arg1, lhs;
17246 switch (fn_code)
17248 /* Flavors of vec_add. We deliberately don't expand
17249 P8V_BUILTIN_VADDUQM as it gets lowered from V1TImode to
17250 TImode, resulting in much poorer code generation. */
17251 case ALTIVEC_BUILTIN_VADDUBM:
17252 case ALTIVEC_BUILTIN_VADDUHM:
17253 case ALTIVEC_BUILTIN_VADDUWM:
17254 case P8V_BUILTIN_VADDUDM:
17255 case ALTIVEC_BUILTIN_VADDFP:
17256 case VSX_BUILTIN_XVADDDP:
17258 arg0 = gimple_call_arg (stmt, 0);
17259 arg1 = gimple_call_arg (stmt, 1);
17260 lhs = gimple_call_lhs (stmt);
17261 gimple *g = gimple_build_assign (lhs, PLUS_EXPR, arg0, arg1);
17262 gimple_set_location (g, gimple_location (stmt));
17263 gsi_replace (gsi, g, true);
17264 return true;
17266 /* Flavors of vec_sub. We deliberately don't expand
17267 P8V_BUILTIN_VSUBUQM. */
17268 case ALTIVEC_BUILTIN_VSUBUBM:
17269 case ALTIVEC_BUILTIN_VSUBUHM:
17270 case ALTIVEC_BUILTIN_VSUBUWM:
17271 case P8V_BUILTIN_VSUBUDM:
17272 case ALTIVEC_BUILTIN_VSUBFP:
17273 case VSX_BUILTIN_XVSUBDP:
17275 arg0 = gimple_call_arg (stmt, 0);
17276 arg1 = gimple_call_arg (stmt, 1);
17277 lhs = gimple_call_lhs (stmt);
17278 gimple *g = gimple_build_assign (lhs, MINUS_EXPR, arg0, arg1);
17279 gimple_set_location (g, gimple_location (stmt));
17280 gsi_replace (gsi, g, true);
17281 return true;
17283 case VSX_BUILTIN_XVMULSP:
17284 case VSX_BUILTIN_XVMULDP:
17286 arg0 = gimple_call_arg (stmt, 0);
17287 arg1 = gimple_call_arg (stmt, 1);
17288 lhs = gimple_call_lhs (stmt);
17289 gimple *g = gimple_build_assign (lhs, MULT_EXPR, arg0, arg1);
17290 gimple_set_location (g, gimple_location (stmt));
17291 gsi_replace (gsi, g, true);
17292 return true;
17294 /* Even element flavors of vec_mul (signed). */
17295 case ALTIVEC_BUILTIN_VMULESB:
17296 case ALTIVEC_BUILTIN_VMULESH:
17297 /* Even element flavors of vec_mul (unsigned). */
17298 case ALTIVEC_BUILTIN_VMULEUB:
17299 case ALTIVEC_BUILTIN_VMULEUH:
17301 arg0 = gimple_call_arg (stmt, 0);
17302 arg1 = gimple_call_arg (stmt, 1);
17303 lhs = gimple_call_lhs (stmt);
17304 gimple *g = gimple_build_assign (lhs, VEC_WIDEN_MULT_EVEN_EXPR, arg0, arg1);
17305 gimple_set_location (g, gimple_location (stmt));
17306 gsi_replace (gsi, g, true);
17307 return true;
17309 /* Odd element flavors of vec_mul (signed). */
17310 case ALTIVEC_BUILTIN_VMULOSB:
17311 case ALTIVEC_BUILTIN_VMULOSH:
17312 /* Odd element flavors of vec_mul (unsigned). */
17313 case ALTIVEC_BUILTIN_VMULOUB:
17314 case ALTIVEC_BUILTIN_VMULOUH:
17316 arg0 = gimple_call_arg (stmt, 0);
17317 arg1 = gimple_call_arg (stmt, 1);
17318 lhs = gimple_call_lhs (stmt);
17319 gimple *g = gimple_build_assign (lhs, VEC_WIDEN_MULT_ODD_EXPR, arg0, arg1);
17320 gimple_set_location (g, gimple_location (stmt));
17321 gsi_replace (gsi, g, true);
17322 return true;
17324 /* Flavors of vec_div (Integer). */
17325 case VSX_BUILTIN_DIV_V2DI:
17326 case VSX_BUILTIN_UDIV_V2DI:
17328 arg0 = gimple_call_arg (stmt, 0);
17329 arg1 = gimple_call_arg (stmt, 1);
17330 lhs = gimple_call_lhs (stmt);
17331 gimple *g = gimple_build_assign (lhs, TRUNC_DIV_EXPR, arg0, arg1);
17332 gimple_set_location (g, gimple_location (stmt));
17333 gsi_replace (gsi, g, true);
17334 return true;
17336 /* Flavors of vec_div (Float). */
17337 case VSX_BUILTIN_XVDIVSP:
17338 case VSX_BUILTIN_XVDIVDP:
17340 arg0 = gimple_call_arg (stmt, 0);
17341 arg1 = gimple_call_arg (stmt, 1);
17342 lhs = gimple_call_lhs (stmt);
17343 gimple *g = gimple_build_assign (lhs, RDIV_EXPR, arg0, arg1);
17344 gimple_set_location (g, gimple_location (stmt));
17345 gsi_replace (gsi, g, true);
17346 return true;
17348 /* Flavors of vec_and. */
17349 case ALTIVEC_BUILTIN_VAND:
17351 arg0 = gimple_call_arg (stmt, 0);
17352 arg1 = gimple_call_arg (stmt, 1);
17353 lhs = gimple_call_lhs (stmt);
17354 gimple *g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, arg1);
17355 gimple_set_location (g, gimple_location (stmt));
17356 gsi_replace (gsi, g, true);
17357 return true;
17359 /* Flavors of vec_andc. */
17360 case ALTIVEC_BUILTIN_VANDC:
17362 arg0 = gimple_call_arg (stmt, 0);
17363 arg1 = gimple_call_arg (stmt, 1);
17364 lhs = gimple_call_lhs (stmt);
17365 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17366 gimple *g = gimple_build_assign(temp, BIT_NOT_EXPR, arg1);
17367 gimple_set_location (g, gimple_location (stmt));
17368 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17369 g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, temp);
17370 gimple_set_location (g, gimple_location (stmt));
17371 gsi_replace (gsi, g, true);
17372 return true;
17374 /* Flavors of vec_nand. */
17375 case P8V_BUILTIN_VEC_NAND:
17376 case P8V_BUILTIN_NAND_V16QI:
17377 case P8V_BUILTIN_NAND_V8HI:
17378 case P8V_BUILTIN_NAND_V4SI:
17379 case P8V_BUILTIN_NAND_V4SF:
17380 case P8V_BUILTIN_NAND_V2DF:
17381 case P8V_BUILTIN_NAND_V2DI:
17383 arg0 = gimple_call_arg (stmt, 0);
17384 arg1 = gimple_call_arg (stmt, 1);
17385 lhs = gimple_call_lhs (stmt);
17386 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17387 gimple *g = gimple_build_assign(temp, BIT_AND_EXPR, arg0, arg1);
17388 gimple_set_location (g, gimple_location (stmt));
17389 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17390 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
17391 gimple_set_location (g, gimple_location (stmt));
17392 gsi_replace (gsi, g, true);
17393 return true;
17395 /* Flavors of vec_or. */
17396 case ALTIVEC_BUILTIN_VOR:
17398 arg0 = gimple_call_arg (stmt, 0);
17399 arg1 = gimple_call_arg (stmt, 1);
17400 lhs = gimple_call_lhs (stmt);
17401 gimple *g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, arg1);
17402 gimple_set_location (g, gimple_location (stmt));
17403 gsi_replace (gsi, g, true);
17404 return true;
17406 /* flavors of vec_orc. */
17407 case P8V_BUILTIN_ORC_V16QI:
17408 case P8V_BUILTIN_ORC_V8HI:
17409 case P8V_BUILTIN_ORC_V4SI:
17410 case P8V_BUILTIN_ORC_V4SF:
17411 case P8V_BUILTIN_ORC_V2DF:
17412 case P8V_BUILTIN_ORC_V2DI:
17414 arg0 = gimple_call_arg (stmt, 0);
17415 arg1 = gimple_call_arg (stmt, 1);
17416 lhs = gimple_call_lhs (stmt);
17417 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17418 gimple *g = gimple_build_assign(temp, BIT_NOT_EXPR, arg1);
17419 gimple_set_location (g, gimple_location (stmt));
17420 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17421 g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, temp);
17422 gimple_set_location (g, gimple_location (stmt));
17423 gsi_replace (gsi, g, true);
17424 return true;
17426 /* Flavors of vec_xor. */
17427 case ALTIVEC_BUILTIN_VXOR:
17429 arg0 = gimple_call_arg (stmt, 0);
17430 arg1 = gimple_call_arg (stmt, 1);
17431 lhs = gimple_call_lhs (stmt);
17432 gimple *g = gimple_build_assign (lhs, BIT_XOR_EXPR, arg0, arg1);
17433 gimple_set_location (g, gimple_location (stmt));
17434 gsi_replace (gsi, g, true);
17435 return true;
17437 /* Flavors of vec_nor. */
17438 case ALTIVEC_BUILTIN_VNOR:
17440 arg0 = gimple_call_arg (stmt, 0);
17441 arg1 = gimple_call_arg (stmt, 1);
17442 lhs = gimple_call_lhs (stmt);
17443 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17444 gimple *g = gimple_build_assign (temp, BIT_IOR_EXPR, arg0, arg1);
17445 gimple_set_location (g, gimple_location (stmt));
17446 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17447 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
17448 gimple_set_location (g, gimple_location (stmt));
17449 gsi_replace (gsi, g, true);
17450 return true;
17452 default:
17453 break;
17456 return false;
17459 /* Expand an expression EXP that calls a built-in function,
17460 with result going to TARGET if that's convenient
17461 (and in mode MODE if that's convenient).
17462 SUBTARGET may be used as the target for computing one of EXP's operands.
17463 IGNORE is nonzero if the value is to be ignored. */
17465 static rtx
17466 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17467 machine_mode mode ATTRIBUTE_UNUSED,
17468 int ignore ATTRIBUTE_UNUSED)
17470 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17471 enum rs6000_builtins fcode
17472 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
17473 size_t uns_fcode = (size_t)fcode;
17474 const struct builtin_description *d;
17475 size_t i;
17476 rtx ret;
17477 bool success;
17478 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
17479 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
17481 if (TARGET_DEBUG_BUILTIN)
17483 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
17484 const char *name1 = rs6000_builtin_info[uns_fcode].name;
17485 const char *name2 = ((icode != CODE_FOR_nothing)
17486 ? get_insn_name ((int)icode)
17487 : "nothing");
17488 const char *name3;
17490 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
17492 default: name3 = "unknown"; break;
17493 case RS6000_BTC_SPECIAL: name3 = "special"; break;
17494 case RS6000_BTC_UNARY: name3 = "unary"; break;
17495 case RS6000_BTC_BINARY: name3 = "binary"; break;
17496 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
17497 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
17498 case RS6000_BTC_ABS: name3 = "abs"; break;
17499 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
17500 case RS6000_BTC_DST: name3 = "dst"; break;
17504 fprintf (stderr,
17505 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
17506 (name1) ? name1 : "---", fcode,
17507 (name2) ? name2 : "---", (int)icode,
17508 name3,
17509 func_valid_p ? "" : ", not valid");
17512 if (!func_valid_p)
17514 rs6000_invalid_builtin (fcode);
17516 /* Given it is invalid, just generate a normal call. */
17517 return expand_call (exp, target, ignore);
17520 switch (fcode)
17522 case RS6000_BUILTIN_RECIP:
17523 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
17525 case RS6000_BUILTIN_RECIPF:
17526 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
17528 case RS6000_BUILTIN_RSQRTF:
17529 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
17531 case RS6000_BUILTIN_RSQRT:
17532 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
17534 case POWER7_BUILTIN_BPERMD:
17535 return rs6000_expand_binop_builtin (((TARGET_64BIT)
17536 ? CODE_FOR_bpermd_di
17537 : CODE_FOR_bpermd_si), exp, target);
17539 case RS6000_BUILTIN_GET_TB:
17540 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
17541 target);
17543 case RS6000_BUILTIN_MFTB:
17544 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
17545 ? CODE_FOR_rs6000_mftb_di
17546 : CODE_FOR_rs6000_mftb_si),
17547 target);
17549 case RS6000_BUILTIN_MFFS:
17550 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
17552 case RS6000_BUILTIN_MTFSF:
17553 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
17555 case RS6000_BUILTIN_CPU_INIT:
17556 case RS6000_BUILTIN_CPU_IS:
17557 case RS6000_BUILTIN_CPU_SUPPORTS:
17558 return cpu_expand_builtin (fcode, exp, target);
17560 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
17561 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
17563 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
17564 : (int) CODE_FOR_altivec_lvsl_direct);
17565 machine_mode tmode = insn_data[icode].operand[0].mode;
17566 machine_mode mode = insn_data[icode].operand[1].mode;
17567 tree arg;
17568 rtx op, addr, pat;
17570 gcc_assert (TARGET_ALTIVEC);
17572 arg = CALL_EXPR_ARG (exp, 0);
17573 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
17574 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
17575 addr = memory_address (mode, op);
17576 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
17577 op = addr;
17578 else
17580 /* For the load case need to negate the address. */
17581 op = gen_reg_rtx (GET_MODE (addr));
17582 emit_insn (gen_rtx_SET (op, gen_rtx_NEG (GET_MODE (addr), addr)));
17584 op = gen_rtx_MEM (mode, op);
17586 if (target == 0
17587 || GET_MODE (target) != tmode
17588 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17589 target = gen_reg_rtx (tmode);
17591 pat = GEN_FCN (icode) (target, op);
17592 if (!pat)
17593 return 0;
17594 emit_insn (pat);
17596 return target;
17599 case ALTIVEC_BUILTIN_VCFUX:
17600 case ALTIVEC_BUILTIN_VCFSX:
17601 case ALTIVEC_BUILTIN_VCTUXS:
17602 case ALTIVEC_BUILTIN_VCTSXS:
17603 /* FIXME: There's got to be a nicer way to handle this case than
17604 constructing a new CALL_EXPR. */
17605 if (call_expr_nargs (exp) == 1)
17607 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
17608 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
17610 break;
17612 default:
17613 break;
17616 if (TARGET_ALTIVEC)
17618 ret = altivec_expand_builtin (exp, target, &success);
17620 if (success)
17621 return ret;
17623 if (TARGET_SPE)
17625 ret = spe_expand_builtin (exp, target, &success);
17627 if (success)
17628 return ret;
17630 if (TARGET_PAIRED_FLOAT)
17632 ret = paired_expand_builtin (exp, target, &success);
17634 if (success)
17635 return ret;
17637 if (TARGET_HTM)
17639 ret = htm_expand_builtin (exp, target, &success);
17641 if (success)
17642 return ret;
17645 unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
17646 /* RS6000_BTC_SPECIAL represents no-operand operators. */
17647 gcc_assert (attr == RS6000_BTC_UNARY
17648 || attr == RS6000_BTC_BINARY
17649 || attr == RS6000_BTC_TERNARY
17650 || attr == RS6000_BTC_SPECIAL);
17652 /* Handle simple unary operations. */
17653 d = bdesc_1arg;
17654 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17655 if (d->code == fcode)
17656 return rs6000_expand_unop_builtin (d->icode, exp, target);
17658 /* Handle simple binary operations. */
17659 d = bdesc_2arg;
17660 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17661 if (d->code == fcode)
17662 return rs6000_expand_binop_builtin (d->icode, exp, target);
17664 /* Handle simple ternary operations. */
17665 d = bdesc_3arg;
17666 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
17667 if (d->code == fcode)
17668 return rs6000_expand_ternop_builtin (d->icode, exp, target);
17670 /* Handle simple no-argument operations. */
17671 d = bdesc_0arg;
17672 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
17673 if (d->code == fcode)
17674 return rs6000_expand_zeroop_builtin (d->icode, target);
17676 gcc_unreachable ();
17679 /* Create a builtin vector type with a name. Taking care not to give
17680 the canonical type a name. */
17682 static tree
17683 rs6000_vector_type (const char *name, tree elt_type, unsigned num_elts)
17685 tree result = build_vector_type (elt_type, num_elts);
17687 /* Copy so we don't give the canonical type a name. */
17688 result = build_variant_type_copy (result);
17690 add_builtin_type (name, result);
17692 return result;
17695 static void
17696 rs6000_init_builtins (void)
17698 tree tdecl;
17699 tree ftype;
17700 machine_mode mode;
17702 if (TARGET_DEBUG_BUILTIN)
17703 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
17704 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
17705 (TARGET_SPE) ? ", spe" : "",
17706 (TARGET_ALTIVEC) ? ", altivec" : "",
17707 (TARGET_VSX) ? ", vsx" : "");
17709 V2SI_type_node = build_vector_type (intSI_type_node, 2);
17710 V2SF_type_node = build_vector_type (float_type_node, 2);
17711 V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64 ? "__vector long"
17712 : "__vector long long",
17713 intDI_type_node, 2);
17714 V2DF_type_node = rs6000_vector_type ("__vector double", double_type_node, 2);
17715 V4HI_type_node = build_vector_type (intHI_type_node, 4);
17716 V4SI_type_node = rs6000_vector_type ("__vector signed int",
17717 intSI_type_node, 4);
17718 V4SF_type_node = rs6000_vector_type ("__vector float", float_type_node, 4);
17719 V8HI_type_node = rs6000_vector_type ("__vector signed short",
17720 intHI_type_node, 8);
17721 V16QI_type_node = rs6000_vector_type ("__vector signed char",
17722 intQI_type_node, 16);
17724 unsigned_V16QI_type_node = rs6000_vector_type ("__vector unsigned char",
17725 unsigned_intQI_type_node, 16);
17726 unsigned_V8HI_type_node = rs6000_vector_type ("__vector unsigned short",
17727 unsigned_intHI_type_node, 8);
17728 unsigned_V4SI_type_node = rs6000_vector_type ("__vector unsigned int",
17729 unsigned_intSI_type_node, 4);
17730 unsigned_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
17731 ? "__vector unsigned long"
17732 : "__vector unsigned long long",
17733 unsigned_intDI_type_node, 2);
17735 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
17736 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
17737 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
17738 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
17740 const_str_type_node
17741 = build_pointer_type (build_qualified_type (char_type_node,
17742 TYPE_QUAL_CONST));
17744 /* We use V1TI mode as a special container to hold __int128_t items that
17745 must live in VSX registers. */
17746 if (intTI_type_node)
17748 V1TI_type_node = rs6000_vector_type ("__vector __int128",
17749 intTI_type_node, 1);
17750 unsigned_V1TI_type_node
17751 = rs6000_vector_type ("__vector unsigned __int128",
17752 unsigned_intTI_type_node, 1);
17755 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
17756 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
17757 'vector unsigned short'. */
17759 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
17760 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
17761 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
17762 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
17763 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
17765 long_integer_type_internal_node = long_integer_type_node;
17766 long_unsigned_type_internal_node = long_unsigned_type_node;
17767 long_long_integer_type_internal_node = long_long_integer_type_node;
17768 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
17769 intQI_type_internal_node = intQI_type_node;
17770 uintQI_type_internal_node = unsigned_intQI_type_node;
17771 intHI_type_internal_node = intHI_type_node;
17772 uintHI_type_internal_node = unsigned_intHI_type_node;
17773 intSI_type_internal_node = intSI_type_node;
17774 uintSI_type_internal_node = unsigned_intSI_type_node;
17775 intDI_type_internal_node = intDI_type_node;
17776 uintDI_type_internal_node = unsigned_intDI_type_node;
17777 intTI_type_internal_node = intTI_type_node;
17778 uintTI_type_internal_node = unsigned_intTI_type_node;
17779 float_type_internal_node = float_type_node;
17780 double_type_internal_node = double_type_node;
17781 long_double_type_internal_node = long_double_type_node;
17782 dfloat64_type_internal_node = dfloat64_type_node;
17783 dfloat128_type_internal_node = dfloat128_type_node;
17784 void_type_internal_node = void_type_node;
17786 /* 128-bit floating point support. KFmode is IEEE 128-bit floating point.
17787 IFmode is the IBM extended 128-bit format that is a pair of doubles.
17788 TFmode will be either IEEE 128-bit floating point or the IBM double-double
17789 format that uses a pair of doubles, depending on the switches and
17790 defaults.
17792 We do not enable the actual __float128 keyword unless the user explicitly
17793 asks for it, because the library support is not yet complete.
17795 If we don't support for either 128-bit IBM double double or IEEE 128-bit
17796 floating point, we need make sure the type is non-zero or else self-test
17797 fails during bootstrap.
17799 We don't register a built-in type for __ibm128 if the type is the same as
17800 long double. Instead we add a #define for __ibm128 in
17801 rs6000_cpu_cpp_builtins to long double. */
17802 if (TARGET_LONG_DOUBLE_128 && FLOAT128_IEEE_P (TFmode))
17804 ibm128_float_type_node = make_node (REAL_TYPE);
17805 TYPE_PRECISION (ibm128_float_type_node) = 128;
17806 SET_TYPE_MODE (ibm128_float_type_node, IFmode);
17807 layout_type (ibm128_float_type_node);
17809 lang_hooks.types.register_builtin_type (ibm128_float_type_node,
17810 "__ibm128");
17812 else
17813 ibm128_float_type_node = long_double_type_node;
17815 if (TARGET_FLOAT128_KEYWORD)
17817 ieee128_float_type_node = float128_type_node;
17818 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
17819 "__float128");
17822 else if (TARGET_FLOAT128_TYPE)
17824 ieee128_float_type_node = make_node (REAL_TYPE);
17825 TYPE_PRECISION (ibm128_float_type_node) = 128;
17826 SET_TYPE_MODE (ieee128_float_type_node, KFmode);
17827 layout_type (ieee128_float_type_node);
17829 /* If we are not exporting the __float128/_Float128 keywords, we need a
17830 keyword to get the types created. Use __ieee128 as the dummy
17831 keyword. */
17832 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
17833 "__ieee128");
17836 else
17837 ieee128_float_type_node = long_double_type_node;
17839 /* Initialize the modes for builtin_function_type, mapping a machine mode to
17840 tree type node. */
17841 builtin_mode_to_type[QImode][0] = integer_type_node;
17842 builtin_mode_to_type[HImode][0] = integer_type_node;
17843 builtin_mode_to_type[SImode][0] = intSI_type_node;
17844 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
17845 builtin_mode_to_type[DImode][0] = intDI_type_node;
17846 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
17847 builtin_mode_to_type[TImode][0] = intTI_type_node;
17848 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
17849 builtin_mode_to_type[SFmode][0] = float_type_node;
17850 builtin_mode_to_type[DFmode][0] = double_type_node;
17851 builtin_mode_to_type[IFmode][0] = ibm128_float_type_node;
17852 builtin_mode_to_type[KFmode][0] = ieee128_float_type_node;
17853 builtin_mode_to_type[TFmode][0] = long_double_type_node;
17854 builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
17855 builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
17856 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
17857 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
17858 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
17859 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
17860 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
17861 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
17862 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
17863 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
17864 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
17865 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
17866 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
17867 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
17868 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
17869 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
17870 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
17872 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
17873 TYPE_NAME (bool_char_type_node) = tdecl;
17875 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
17876 TYPE_NAME (bool_short_type_node) = tdecl;
17878 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
17879 TYPE_NAME (bool_int_type_node) = tdecl;
17881 tdecl = add_builtin_type ("__pixel", pixel_type_node);
17882 TYPE_NAME (pixel_type_node) = tdecl;
17884 bool_V16QI_type_node = rs6000_vector_type ("__vector __bool char",
17885 bool_char_type_node, 16);
17886 bool_V8HI_type_node = rs6000_vector_type ("__vector __bool short",
17887 bool_short_type_node, 8);
17888 bool_V4SI_type_node = rs6000_vector_type ("__vector __bool int",
17889 bool_int_type_node, 4);
17890 bool_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
17891 ? "__vector __bool long"
17892 : "__vector __bool long long",
17893 bool_long_type_node, 2);
17894 pixel_V8HI_type_node = rs6000_vector_type ("__vector __pixel",
17895 pixel_type_node, 8);
17897 /* Paired and SPE builtins are only available if you build a compiler with
17898 the appropriate options, so only create those builtins with the
17899 appropriate compiler option. Create Altivec and VSX builtins on machines
17900 with at least the general purpose extensions (970 and newer) to allow the
17901 use of the target attribute. */
17902 if (TARGET_PAIRED_FLOAT)
17903 paired_init_builtins ();
17904 if (TARGET_SPE)
17905 spe_init_builtins ();
17906 if (TARGET_EXTRA_BUILTINS)
17907 altivec_init_builtins ();
17908 if (TARGET_HTM)
17909 htm_init_builtins ();
17911 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
17912 rs6000_common_init_builtins ();
17914 ftype = build_function_type_list (ieee128_float_type_node,
17915 const_str_type_node, NULL_TREE);
17916 def_builtin ("__builtin_nanq", ftype, RS6000_BUILTIN_NANQ);
17917 def_builtin ("__builtin_nansq", ftype, RS6000_BUILTIN_NANSQ);
17919 ftype = build_function_type_list (ieee128_float_type_node, NULL_TREE);
17920 def_builtin ("__builtin_infq", ftype, RS6000_BUILTIN_INFQ);
17921 def_builtin ("__builtin_huge_valq", ftype, RS6000_BUILTIN_HUGE_VALQ);
17923 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
17924 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
17925 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
17927 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
17928 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
17929 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
17931 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
17932 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
17933 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
17935 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
17936 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
17937 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
17939 mode = (TARGET_64BIT) ? DImode : SImode;
17940 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
17941 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
17942 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
17944 ftype = build_function_type_list (unsigned_intDI_type_node,
17945 NULL_TREE);
17946 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
17948 if (TARGET_64BIT)
17949 ftype = build_function_type_list (unsigned_intDI_type_node,
17950 NULL_TREE);
17951 else
17952 ftype = build_function_type_list (unsigned_intSI_type_node,
17953 NULL_TREE);
17954 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
17956 ftype = build_function_type_list (double_type_node, NULL_TREE);
17957 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
17959 ftype = build_function_type_list (void_type_node,
17960 intSI_type_node, double_type_node,
17961 NULL_TREE);
17962 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
17964 ftype = build_function_type_list (void_type_node, NULL_TREE);
17965 def_builtin ("__builtin_cpu_init", ftype, RS6000_BUILTIN_CPU_INIT);
17967 ftype = build_function_type_list (bool_int_type_node, const_ptr_type_node,
17968 NULL_TREE);
17969 def_builtin ("__builtin_cpu_is", ftype, RS6000_BUILTIN_CPU_IS);
17970 def_builtin ("__builtin_cpu_supports", ftype, RS6000_BUILTIN_CPU_SUPPORTS);
17972 /* AIX libm provides clog as __clog. */
17973 if (TARGET_XCOFF &&
17974 (tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
17975 set_user_assembler_name (tdecl, "__clog");
17977 #ifdef SUBTARGET_INIT_BUILTINS
17978 SUBTARGET_INIT_BUILTINS;
17979 #endif
17982 /* Returns the rs6000 builtin decl for CODE. */
17984 static tree
17985 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
17987 HOST_WIDE_INT fnmask;
17989 if (code >= RS6000_BUILTIN_COUNT)
17990 return error_mark_node;
17992 fnmask = rs6000_builtin_info[code].mask;
17993 if ((fnmask & rs6000_builtin_mask) != fnmask)
17995 rs6000_invalid_builtin ((enum rs6000_builtins)code);
17996 return error_mark_node;
17999 return rs6000_builtin_decls[code];
18002 static void
18003 spe_init_builtins (void)
18005 tree puint_type_node = build_pointer_type (unsigned_type_node);
18006 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
18007 const struct builtin_description *d;
18008 size_t i;
18009 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18011 tree v2si_ftype_4_v2si
18012 = build_function_type_list (opaque_V2SI_type_node,
18013 opaque_V2SI_type_node,
18014 opaque_V2SI_type_node,
18015 opaque_V2SI_type_node,
18016 opaque_V2SI_type_node,
18017 NULL_TREE);
18019 tree v2sf_ftype_4_v2sf
18020 = build_function_type_list (opaque_V2SF_type_node,
18021 opaque_V2SF_type_node,
18022 opaque_V2SF_type_node,
18023 opaque_V2SF_type_node,
18024 opaque_V2SF_type_node,
18025 NULL_TREE);
18027 tree int_ftype_int_v2si_v2si
18028 = build_function_type_list (integer_type_node,
18029 integer_type_node,
18030 opaque_V2SI_type_node,
18031 opaque_V2SI_type_node,
18032 NULL_TREE);
18034 tree int_ftype_int_v2sf_v2sf
18035 = build_function_type_list (integer_type_node,
18036 integer_type_node,
18037 opaque_V2SF_type_node,
18038 opaque_V2SF_type_node,
18039 NULL_TREE);
18041 tree void_ftype_v2si_puint_int
18042 = build_function_type_list (void_type_node,
18043 opaque_V2SI_type_node,
18044 puint_type_node,
18045 integer_type_node,
18046 NULL_TREE);
18048 tree void_ftype_v2si_puint_char
18049 = build_function_type_list (void_type_node,
18050 opaque_V2SI_type_node,
18051 puint_type_node,
18052 char_type_node,
18053 NULL_TREE);
18055 tree void_ftype_v2si_pv2si_int
18056 = build_function_type_list (void_type_node,
18057 opaque_V2SI_type_node,
18058 opaque_p_V2SI_type_node,
18059 integer_type_node,
18060 NULL_TREE);
18062 tree void_ftype_v2si_pv2si_char
18063 = build_function_type_list (void_type_node,
18064 opaque_V2SI_type_node,
18065 opaque_p_V2SI_type_node,
18066 char_type_node,
18067 NULL_TREE);
18069 tree void_ftype_int
18070 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
18072 tree int_ftype_void
18073 = build_function_type_list (integer_type_node, NULL_TREE);
18075 tree v2si_ftype_pv2si_int
18076 = build_function_type_list (opaque_V2SI_type_node,
18077 opaque_p_V2SI_type_node,
18078 integer_type_node,
18079 NULL_TREE);
18081 tree v2si_ftype_puint_int
18082 = build_function_type_list (opaque_V2SI_type_node,
18083 puint_type_node,
18084 integer_type_node,
18085 NULL_TREE);
18087 tree v2si_ftype_pushort_int
18088 = build_function_type_list (opaque_V2SI_type_node,
18089 pushort_type_node,
18090 integer_type_node,
18091 NULL_TREE);
18093 tree v2si_ftype_signed_char
18094 = build_function_type_list (opaque_V2SI_type_node,
18095 signed_char_type_node,
18096 NULL_TREE);
18098 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
18100 /* Initialize irregular SPE builtins. */
18102 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
18103 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
18104 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
18105 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
18106 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
18107 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
18108 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
18109 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
18110 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
18111 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
18112 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
18113 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
18114 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
18115 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
18116 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
18117 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
18118 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
18119 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
18121 /* Loads. */
18122 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
18123 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
18124 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
18125 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
18126 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
18127 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
18128 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
18129 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
18130 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
18131 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
18132 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
18133 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
18134 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
18135 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
18136 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
18137 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
18138 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
18139 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
18140 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
18141 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
18142 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
18143 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
18145 /* Predicates. */
18146 d = bdesc_spe_predicates;
18147 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
18149 tree type;
18150 HOST_WIDE_INT mask = d->mask;
18152 if ((mask & builtin_mask) != mask)
18154 if (TARGET_DEBUG_BUILTIN)
18155 fprintf (stderr, "spe_init_builtins, skip predicate %s\n",
18156 d->name);
18157 continue;
18160 /* Cannot define builtin if the instruction is disabled. */
18161 gcc_assert (d->icode != CODE_FOR_nothing);
18162 switch (insn_data[d->icode].operand[1].mode)
18164 case E_V2SImode:
18165 type = int_ftype_int_v2si_v2si;
18166 break;
18167 case E_V2SFmode:
18168 type = int_ftype_int_v2sf_v2sf;
18169 break;
18170 default:
18171 gcc_unreachable ();
18174 def_builtin (d->name, type, d->code);
18177 /* Evsel predicates. */
18178 d = bdesc_spe_evsel;
18179 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
18181 tree type;
18182 HOST_WIDE_INT mask = d->mask;
18184 if ((mask & builtin_mask) != mask)
18186 if (TARGET_DEBUG_BUILTIN)
18187 fprintf (stderr, "spe_init_builtins, skip evsel %s\n",
18188 d->name);
18189 continue;
18192 /* Cannot define builtin if the instruction is disabled. */
18193 gcc_assert (d->icode != CODE_FOR_nothing);
18194 switch (insn_data[d->icode].operand[1].mode)
18196 case E_V2SImode:
18197 type = v2si_ftype_4_v2si;
18198 break;
18199 case E_V2SFmode:
18200 type = v2sf_ftype_4_v2sf;
18201 break;
18202 default:
18203 gcc_unreachable ();
18206 def_builtin (d->name, type, d->code);
18210 static void
18211 paired_init_builtins (void)
18213 const struct builtin_description *d;
18214 size_t i;
18215 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18217 tree int_ftype_int_v2sf_v2sf
18218 = build_function_type_list (integer_type_node,
18219 integer_type_node,
18220 V2SF_type_node,
18221 V2SF_type_node,
18222 NULL_TREE);
18223 tree pcfloat_type_node =
18224 build_pointer_type (build_qualified_type
18225 (float_type_node, TYPE_QUAL_CONST));
18227 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
18228 long_integer_type_node,
18229 pcfloat_type_node,
18230 NULL_TREE);
18231 tree void_ftype_v2sf_long_pcfloat =
18232 build_function_type_list (void_type_node,
18233 V2SF_type_node,
18234 long_integer_type_node,
18235 pcfloat_type_node,
18236 NULL_TREE);
18239 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
18240 PAIRED_BUILTIN_LX);
18243 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
18244 PAIRED_BUILTIN_STX);
18246 /* Predicates. */
18247 d = bdesc_paired_preds;
18248 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
18250 tree type;
18251 HOST_WIDE_INT mask = d->mask;
18253 if ((mask & builtin_mask) != mask)
18255 if (TARGET_DEBUG_BUILTIN)
18256 fprintf (stderr, "paired_init_builtins, skip predicate %s\n",
18257 d->name);
18258 continue;
18261 /* Cannot define builtin if the instruction is disabled. */
18262 gcc_assert (d->icode != CODE_FOR_nothing);
18264 if (TARGET_DEBUG_BUILTIN)
18265 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
18266 (int)i, get_insn_name (d->icode), (int)d->icode,
18267 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
18269 switch (insn_data[d->icode].operand[1].mode)
18271 case E_V2SFmode:
18272 type = int_ftype_int_v2sf_v2sf;
18273 break;
18274 default:
18275 gcc_unreachable ();
18278 def_builtin (d->name, type, d->code);
18282 static void
18283 altivec_init_builtins (void)
18285 const struct builtin_description *d;
18286 size_t i;
18287 tree ftype;
18288 tree decl;
18289 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18291 tree pvoid_type_node = build_pointer_type (void_type_node);
18293 tree pcvoid_type_node
18294 = build_pointer_type (build_qualified_type (void_type_node,
18295 TYPE_QUAL_CONST));
18297 tree int_ftype_opaque
18298 = build_function_type_list (integer_type_node,
18299 opaque_V4SI_type_node, NULL_TREE);
18300 tree opaque_ftype_opaque
18301 = build_function_type_list (integer_type_node, NULL_TREE);
18302 tree opaque_ftype_opaque_int
18303 = build_function_type_list (opaque_V4SI_type_node,
18304 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
18305 tree opaque_ftype_opaque_opaque_int
18306 = build_function_type_list (opaque_V4SI_type_node,
18307 opaque_V4SI_type_node, opaque_V4SI_type_node,
18308 integer_type_node, NULL_TREE);
18309 tree opaque_ftype_opaque_opaque_opaque
18310 = build_function_type_list (opaque_V4SI_type_node,
18311 opaque_V4SI_type_node, opaque_V4SI_type_node,
18312 opaque_V4SI_type_node, NULL_TREE);
18313 tree opaque_ftype_opaque_opaque
18314 = build_function_type_list (opaque_V4SI_type_node,
18315 opaque_V4SI_type_node, opaque_V4SI_type_node,
18316 NULL_TREE);
18317 tree int_ftype_int_opaque_opaque
18318 = build_function_type_list (integer_type_node,
18319 integer_type_node, opaque_V4SI_type_node,
18320 opaque_V4SI_type_node, NULL_TREE);
18321 tree int_ftype_int_v4si_v4si
18322 = build_function_type_list (integer_type_node,
18323 integer_type_node, V4SI_type_node,
18324 V4SI_type_node, NULL_TREE);
18325 tree int_ftype_int_v2di_v2di
18326 = build_function_type_list (integer_type_node,
18327 integer_type_node, V2DI_type_node,
18328 V2DI_type_node, NULL_TREE);
18329 tree void_ftype_v4si
18330 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
18331 tree v8hi_ftype_void
18332 = build_function_type_list (V8HI_type_node, NULL_TREE);
18333 tree void_ftype_void
18334 = build_function_type_list (void_type_node, NULL_TREE);
18335 tree void_ftype_int
18336 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
18338 tree opaque_ftype_long_pcvoid
18339 = build_function_type_list (opaque_V4SI_type_node,
18340 long_integer_type_node, pcvoid_type_node,
18341 NULL_TREE);
18342 tree v16qi_ftype_long_pcvoid
18343 = build_function_type_list (V16QI_type_node,
18344 long_integer_type_node, pcvoid_type_node,
18345 NULL_TREE);
18346 tree v8hi_ftype_long_pcvoid
18347 = build_function_type_list (V8HI_type_node,
18348 long_integer_type_node, pcvoid_type_node,
18349 NULL_TREE);
18350 tree v4si_ftype_long_pcvoid
18351 = build_function_type_list (V4SI_type_node,
18352 long_integer_type_node, pcvoid_type_node,
18353 NULL_TREE);
18354 tree v4sf_ftype_long_pcvoid
18355 = build_function_type_list (V4SF_type_node,
18356 long_integer_type_node, pcvoid_type_node,
18357 NULL_TREE);
18358 tree v2df_ftype_long_pcvoid
18359 = build_function_type_list (V2DF_type_node,
18360 long_integer_type_node, pcvoid_type_node,
18361 NULL_TREE);
18362 tree v2di_ftype_long_pcvoid
18363 = build_function_type_list (V2DI_type_node,
18364 long_integer_type_node, pcvoid_type_node,
18365 NULL_TREE);
18367 tree void_ftype_opaque_long_pvoid
18368 = build_function_type_list (void_type_node,
18369 opaque_V4SI_type_node, long_integer_type_node,
18370 pvoid_type_node, NULL_TREE);
18371 tree void_ftype_v4si_long_pvoid
18372 = build_function_type_list (void_type_node,
18373 V4SI_type_node, long_integer_type_node,
18374 pvoid_type_node, NULL_TREE);
18375 tree void_ftype_v16qi_long_pvoid
18376 = build_function_type_list (void_type_node,
18377 V16QI_type_node, long_integer_type_node,
18378 pvoid_type_node, NULL_TREE);
18380 tree void_ftype_v16qi_pvoid_long
18381 = build_function_type_list (void_type_node,
18382 V16QI_type_node, pvoid_type_node,
18383 long_integer_type_node, NULL_TREE);
18385 tree void_ftype_v8hi_long_pvoid
18386 = build_function_type_list (void_type_node,
18387 V8HI_type_node, long_integer_type_node,
18388 pvoid_type_node, NULL_TREE);
18389 tree void_ftype_v4sf_long_pvoid
18390 = build_function_type_list (void_type_node,
18391 V4SF_type_node, long_integer_type_node,
18392 pvoid_type_node, NULL_TREE);
18393 tree void_ftype_v2df_long_pvoid
18394 = build_function_type_list (void_type_node,
18395 V2DF_type_node, long_integer_type_node,
18396 pvoid_type_node, NULL_TREE);
18397 tree void_ftype_v2di_long_pvoid
18398 = build_function_type_list (void_type_node,
18399 V2DI_type_node, long_integer_type_node,
18400 pvoid_type_node, NULL_TREE);
18401 tree int_ftype_int_v8hi_v8hi
18402 = build_function_type_list (integer_type_node,
18403 integer_type_node, V8HI_type_node,
18404 V8HI_type_node, NULL_TREE);
18405 tree int_ftype_int_v16qi_v16qi
18406 = build_function_type_list (integer_type_node,
18407 integer_type_node, V16QI_type_node,
18408 V16QI_type_node, NULL_TREE);
18409 tree int_ftype_int_v4sf_v4sf
18410 = build_function_type_list (integer_type_node,
18411 integer_type_node, V4SF_type_node,
18412 V4SF_type_node, NULL_TREE);
18413 tree int_ftype_int_v2df_v2df
18414 = build_function_type_list (integer_type_node,
18415 integer_type_node, V2DF_type_node,
18416 V2DF_type_node, NULL_TREE);
18417 tree v2di_ftype_v2di
18418 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
18419 tree v4si_ftype_v4si
18420 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
18421 tree v8hi_ftype_v8hi
18422 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
18423 tree v16qi_ftype_v16qi
18424 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
18425 tree v4sf_ftype_v4sf
18426 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
18427 tree v2df_ftype_v2df
18428 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
18429 tree void_ftype_pcvoid_int_int
18430 = build_function_type_list (void_type_node,
18431 pcvoid_type_node, integer_type_node,
18432 integer_type_node, NULL_TREE);
18434 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
18435 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
18436 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
18437 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
18438 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
18439 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
18440 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
18441 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
18442 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
18443 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
18444 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
18445 ALTIVEC_BUILTIN_LVXL_V2DF);
18446 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
18447 ALTIVEC_BUILTIN_LVXL_V2DI);
18448 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
18449 ALTIVEC_BUILTIN_LVXL_V4SF);
18450 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
18451 ALTIVEC_BUILTIN_LVXL_V4SI);
18452 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
18453 ALTIVEC_BUILTIN_LVXL_V8HI);
18454 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
18455 ALTIVEC_BUILTIN_LVXL_V16QI);
18456 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
18457 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
18458 ALTIVEC_BUILTIN_LVX_V2DF);
18459 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
18460 ALTIVEC_BUILTIN_LVX_V2DI);
18461 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
18462 ALTIVEC_BUILTIN_LVX_V4SF);
18463 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
18464 ALTIVEC_BUILTIN_LVX_V4SI);
18465 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
18466 ALTIVEC_BUILTIN_LVX_V8HI);
18467 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
18468 ALTIVEC_BUILTIN_LVX_V16QI);
18469 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
18470 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
18471 ALTIVEC_BUILTIN_STVX_V2DF);
18472 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
18473 ALTIVEC_BUILTIN_STVX_V2DI);
18474 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
18475 ALTIVEC_BUILTIN_STVX_V4SF);
18476 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
18477 ALTIVEC_BUILTIN_STVX_V4SI);
18478 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
18479 ALTIVEC_BUILTIN_STVX_V8HI);
18480 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
18481 ALTIVEC_BUILTIN_STVX_V16QI);
18482 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
18483 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
18484 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
18485 ALTIVEC_BUILTIN_STVXL_V2DF);
18486 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
18487 ALTIVEC_BUILTIN_STVXL_V2DI);
18488 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
18489 ALTIVEC_BUILTIN_STVXL_V4SF);
18490 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
18491 ALTIVEC_BUILTIN_STVXL_V4SI);
18492 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
18493 ALTIVEC_BUILTIN_STVXL_V8HI);
18494 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
18495 ALTIVEC_BUILTIN_STVXL_V16QI);
18496 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
18497 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
18498 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
18499 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
18500 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
18501 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
18502 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
18503 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
18504 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
18505 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
18506 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
18507 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
18508 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
18509 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
18510 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
18511 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
18513 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
18514 VSX_BUILTIN_LXVD2X_V2DF);
18515 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
18516 VSX_BUILTIN_LXVD2X_V2DI);
18517 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
18518 VSX_BUILTIN_LXVW4X_V4SF);
18519 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
18520 VSX_BUILTIN_LXVW4X_V4SI);
18521 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
18522 VSX_BUILTIN_LXVW4X_V8HI);
18523 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
18524 VSX_BUILTIN_LXVW4X_V16QI);
18525 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
18526 VSX_BUILTIN_STXVD2X_V2DF);
18527 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
18528 VSX_BUILTIN_STXVD2X_V2DI);
18529 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
18530 VSX_BUILTIN_STXVW4X_V4SF);
18531 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
18532 VSX_BUILTIN_STXVW4X_V4SI);
18533 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
18534 VSX_BUILTIN_STXVW4X_V8HI);
18535 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
18536 VSX_BUILTIN_STXVW4X_V16QI);
18538 def_builtin ("__builtin_vsx_ld_elemrev_v2df", v2df_ftype_long_pcvoid,
18539 VSX_BUILTIN_LD_ELEMREV_V2DF);
18540 def_builtin ("__builtin_vsx_ld_elemrev_v2di", v2di_ftype_long_pcvoid,
18541 VSX_BUILTIN_LD_ELEMREV_V2DI);
18542 def_builtin ("__builtin_vsx_ld_elemrev_v4sf", v4sf_ftype_long_pcvoid,
18543 VSX_BUILTIN_LD_ELEMREV_V4SF);
18544 def_builtin ("__builtin_vsx_ld_elemrev_v4si", v4si_ftype_long_pcvoid,
18545 VSX_BUILTIN_LD_ELEMREV_V4SI);
18546 def_builtin ("__builtin_vsx_st_elemrev_v2df", void_ftype_v2df_long_pvoid,
18547 VSX_BUILTIN_ST_ELEMREV_V2DF);
18548 def_builtin ("__builtin_vsx_st_elemrev_v2di", void_ftype_v2di_long_pvoid,
18549 VSX_BUILTIN_ST_ELEMREV_V2DI);
18550 def_builtin ("__builtin_vsx_st_elemrev_v4sf", void_ftype_v4sf_long_pvoid,
18551 VSX_BUILTIN_ST_ELEMREV_V4SF);
18552 def_builtin ("__builtin_vsx_st_elemrev_v4si", void_ftype_v4si_long_pvoid,
18553 VSX_BUILTIN_ST_ELEMREV_V4SI);
18555 if (TARGET_P9_VECTOR)
18557 def_builtin ("__builtin_vsx_ld_elemrev_v8hi", v8hi_ftype_long_pcvoid,
18558 VSX_BUILTIN_LD_ELEMREV_V8HI);
18559 def_builtin ("__builtin_vsx_ld_elemrev_v16qi", v16qi_ftype_long_pcvoid,
18560 VSX_BUILTIN_LD_ELEMREV_V16QI);
18561 def_builtin ("__builtin_vsx_st_elemrev_v8hi",
18562 void_ftype_v8hi_long_pvoid, VSX_BUILTIN_ST_ELEMREV_V8HI);
18563 def_builtin ("__builtin_vsx_st_elemrev_v16qi",
18564 void_ftype_v16qi_long_pvoid, VSX_BUILTIN_ST_ELEMREV_V16QI);
18566 else
18568 rs6000_builtin_decls[(int) VSX_BUILTIN_LD_ELEMREV_V8HI]
18569 = rs6000_builtin_decls[(int) VSX_BUILTIN_LXVW4X_V8HI];
18570 rs6000_builtin_decls[(int) VSX_BUILTIN_LD_ELEMREV_V16QI]
18571 = rs6000_builtin_decls[(int) VSX_BUILTIN_LXVW4X_V16QI];
18572 rs6000_builtin_decls[(int) VSX_BUILTIN_ST_ELEMREV_V8HI]
18573 = rs6000_builtin_decls[(int) VSX_BUILTIN_STXVW4X_V8HI];
18574 rs6000_builtin_decls[(int) VSX_BUILTIN_ST_ELEMREV_V16QI]
18575 = rs6000_builtin_decls[(int) VSX_BUILTIN_STXVW4X_V16QI];
18578 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
18579 VSX_BUILTIN_VEC_LD);
18580 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
18581 VSX_BUILTIN_VEC_ST);
18582 def_builtin ("__builtin_vec_xl", opaque_ftype_long_pcvoid,
18583 VSX_BUILTIN_VEC_XL);
18584 def_builtin ("__builtin_vec_xst", void_ftype_opaque_long_pvoid,
18585 VSX_BUILTIN_VEC_XST);
18587 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
18588 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
18589 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
18591 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
18592 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
18593 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
18594 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
18595 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
18596 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
18597 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
18598 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
18599 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
18600 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
18601 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
18602 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
18604 def_builtin ("__builtin_vec_adde", opaque_ftype_opaque_opaque_opaque,
18605 ALTIVEC_BUILTIN_VEC_ADDE);
18606 def_builtin ("__builtin_vec_addec", opaque_ftype_opaque_opaque_opaque,
18607 ALTIVEC_BUILTIN_VEC_ADDEC);
18608 def_builtin ("__builtin_vec_cmpne", opaque_ftype_opaque_opaque,
18609 ALTIVEC_BUILTIN_VEC_CMPNE);
18610 def_builtin ("__builtin_vec_mul", opaque_ftype_opaque_opaque,
18611 ALTIVEC_BUILTIN_VEC_MUL);
18613 /* Cell builtins. */
18614 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
18615 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
18616 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
18617 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
18619 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
18620 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
18621 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
18622 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
18624 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
18625 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
18626 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
18627 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
18629 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
18630 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
18631 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
18632 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
18634 if (TARGET_P9_VECTOR)
18635 def_builtin ("__builtin_altivec_stxvl", void_ftype_v16qi_pvoid_long,
18636 P9V_BUILTIN_STXVL);
18638 /* Add the DST variants. */
18639 d = bdesc_dst;
18640 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
18642 HOST_WIDE_INT mask = d->mask;
18644 /* It is expected that these dst built-in functions may have
18645 d->icode equal to CODE_FOR_nothing. */
18646 if ((mask & builtin_mask) != mask)
18648 if (TARGET_DEBUG_BUILTIN)
18649 fprintf (stderr, "altivec_init_builtins, skip dst %s\n",
18650 d->name);
18651 continue;
18653 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
18656 /* Initialize the predicates. */
18657 d = bdesc_altivec_preds;
18658 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
18660 machine_mode mode1;
18661 tree type;
18662 HOST_WIDE_INT mask = d->mask;
18664 if ((mask & builtin_mask) != mask)
18666 if (TARGET_DEBUG_BUILTIN)
18667 fprintf (stderr, "altivec_init_builtins, skip predicate %s\n",
18668 d->name);
18669 continue;
18672 if (rs6000_overloaded_builtin_p (d->code))
18673 mode1 = VOIDmode;
18674 else
18676 /* Cannot define builtin if the instruction is disabled. */
18677 gcc_assert (d->icode != CODE_FOR_nothing);
18678 mode1 = insn_data[d->icode].operand[1].mode;
18681 switch (mode1)
18683 case E_VOIDmode:
18684 type = int_ftype_int_opaque_opaque;
18685 break;
18686 case E_V2DImode:
18687 type = int_ftype_int_v2di_v2di;
18688 break;
18689 case E_V4SImode:
18690 type = int_ftype_int_v4si_v4si;
18691 break;
18692 case E_V8HImode:
18693 type = int_ftype_int_v8hi_v8hi;
18694 break;
18695 case E_V16QImode:
18696 type = int_ftype_int_v16qi_v16qi;
18697 break;
18698 case E_V4SFmode:
18699 type = int_ftype_int_v4sf_v4sf;
18700 break;
18701 case E_V2DFmode:
18702 type = int_ftype_int_v2df_v2df;
18703 break;
18704 default:
18705 gcc_unreachable ();
18708 def_builtin (d->name, type, d->code);
18711 /* Initialize the abs* operators. */
18712 d = bdesc_abs;
18713 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
18715 machine_mode mode0;
18716 tree type;
18717 HOST_WIDE_INT mask = d->mask;
18719 if ((mask & builtin_mask) != mask)
18721 if (TARGET_DEBUG_BUILTIN)
18722 fprintf (stderr, "altivec_init_builtins, skip abs %s\n",
18723 d->name);
18724 continue;
18727 /* Cannot define builtin if the instruction is disabled. */
18728 gcc_assert (d->icode != CODE_FOR_nothing);
18729 mode0 = insn_data[d->icode].operand[0].mode;
18731 switch (mode0)
18733 case E_V2DImode:
18734 type = v2di_ftype_v2di;
18735 break;
18736 case E_V4SImode:
18737 type = v4si_ftype_v4si;
18738 break;
18739 case E_V8HImode:
18740 type = v8hi_ftype_v8hi;
18741 break;
18742 case E_V16QImode:
18743 type = v16qi_ftype_v16qi;
18744 break;
18745 case E_V4SFmode:
18746 type = v4sf_ftype_v4sf;
18747 break;
18748 case E_V2DFmode:
18749 type = v2df_ftype_v2df;
18750 break;
18751 default:
18752 gcc_unreachable ();
18755 def_builtin (d->name, type, d->code);
18758 /* Initialize target builtin that implements
18759 targetm.vectorize.builtin_mask_for_load. */
18761 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
18762 v16qi_ftype_long_pcvoid,
18763 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
18764 BUILT_IN_MD, NULL, NULL_TREE);
18765 TREE_READONLY (decl) = 1;
18766 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
18767 altivec_builtin_mask_for_load = decl;
18769 /* Access to the vec_init patterns. */
18770 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
18771 integer_type_node, integer_type_node,
18772 integer_type_node, NULL_TREE);
18773 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
18775 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
18776 short_integer_type_node,
18777 short_integer_type_node,
18778 short_integer_type_node,
18779 short_integer_type_node,
18780 short_integer_type_node,
18781 short_integer_type_node,
18782 short_integer_type_node, NULL_TREE);
18783 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
18785 ftype = build_function_type_list (V16QI_type_node, char_type_node,
18786 char_type_node, char_type_node,
18787 char_type_node, char_type_node,
18788 char_type_node, char_type_node,
18789 char_type_node, char_type_node,
18790 char_type_node, char_type_node,
18791 char_type_node, char_type_node,
18792 char_type_node, char_type_node,
18793 char_type_node, NULL_TREE);
18794 def_builtin ("__builtin_vec_init_v16qi", ftype,
18795 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
18797 ftype = build_function_type_list (V4SF_type_node, float_type_node,
18798 float_type_node, float_type_node,
18799 float_type_node, NULL_TREE);
18800 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
18802 /* VSX builtins. */
18803 ftype = build_function_type_list (V2DF_type_node, double_type_node,
18804 double_type_node, NULL_TREE);
18805 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
18807 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
18808 intDI_type_node, NULL_TREE);
18809 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
18811 /* Access to the vec_set patterns. */
18812 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
18813 intSI_type_node,
18814 integer_type_node, NULL_TREE);
18815 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
18817 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
18818 intHI_type_node,
18819 integer_type_node, NULL_TREE);
18820 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
18822 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
18823 intQI_type_node,
18824 integer_type_node, NULL_TREE);
18825 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
18827 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
18828 float_type_node,
18829 integer_type_node, NULL_TREE);
18830 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
18832 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
18833 double_type_node,
18834 integer_type_node, NULL_TREE);
18835 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
18837 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
18838 intDI_type_node,
18839 integer_type_node, NULL_TREE);
18840 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
18842 /* Access to the vec_extract patterns. */
18843 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
18844 integer_type_node, NULL_TREE);
18845 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
18847 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
18848 integer_type_node, NULL_TREE);
18849 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
18851 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
18852 integer_type_node, NULL_TREE);
18853 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
18855 ftype = build_function_type_list (float_type_node, V4SF_type_node,
18856 integer_type_node, NULL_TREE);
18857 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
18859 ftype = build_function_type_list (double_type_node, V2DF_type_node,
18860 integer_type_node, NULL_TREE);
18861 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
18863 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
18864 integer_type_node, NULL_TREE);
18865 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
18868 if (V1TI_type_node)
18870 tree v1ti_ftype_long_pcvoid
18871 = build_function_type_list (V1TI_type_node,
18872 long_integer_type_node, pcvoid_type_node,
18873 NULL_TREE);
18874 tree void_ftype_v1ti_long_pvoid
18875 = build_function_type_list (void_type_node,
18876 V1TI_type_node, long_integer_type_node,
18877 pvoid_type_node, NULL_TREE);
18878 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
18879 VSX_BUILTIN_LXVD2X_V1TI);
18880 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
18881 VSX_BUILTIN_STXVD2X_V1TI);
18882 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
18883 NULL_TREE, NULL_TREE);
18884 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
18885 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
18886 intTI_type_node,
18887 integer_type_node, NULL_TREE);
18888 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
18889 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
18890 integer_type_node, NULL_TREE);
18891 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
18896 static void
18897 htm_init_builtins (void)
18899 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18900 const struct builtin_description *d;
18901 size_t i;
18903 d = bdesc_htm;
18904 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
18906 tree op[MAX_HTM_OPERANDS], type;
18907 HOST_WIDE_INT mask = d->mask;
18908 unsigned attr = rs6000_builtin_info[d->code].attr;
18909 bool void_func = (attr & RS6000_BTC_VOID);
18910 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
18911 int nopnds = 0;
18912 tree gpr_type_node;
18913 tree rettype;
18914 tree argtype;
18916 /* It is expected that these htm built-in functions may have
18917 d->icode equal to CODE_FOR_nothing. */
18919 if (TARGET_32BIT && TARGET_POWERPC64)
18920 gpr_type_node = long_long_unsigned_type_node;
18921 else
18922 gpr_type_node = long_unsigned_type_node;
18924 if (attr & RS6000_BTC_SPR)
18926 rettype = gpr_type_node;
18927 argtype = gpr_type_node;
18929 else if (d->code == HTM_BUILTIN_TABORTDC
18930 || d->code == HTM_BUILTIN_TABORTDCI)
18932 rettype = unsigned_type_node;
18933 argtype = gpr_type_node;
18935 else
18937 rettype = unsigned_type_node;
18938 argtype = unsigned_type_node;
18941 if ((mask & builtin_mask) != mask)
18943 if (TARGET_DEBUG_BUILTIN)
18944 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
18945 continue;
18948 if (d->name == 0)
18950 if (TARGET_DEBUG_BUILTIN)
18951 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
18952 (long unsigned) i);
18953 continue;
18956 op[nopnds++] = (void_func) ? void_type_node : rettype;
18958 if (attr_args == RS6000_BTC_UNARY)
18959 op[nopnds++] = argtype;
18960 else if (attr_args == RS6000_BTC_BINARY)
18962 op[nopnds++] = argtype;
18963 op[nopnds++] = argtype;
18965 else if (attr_args == RS6000_BTC_TERNARY)
18967 op[nopnds++] = argtype;
18968 op[nopnds++] = argtype;
18969 op[nopnds++] = argtype;
18972 switch (nopnds)
18974 case 1:
18975 type = build_function_type_list (op[0], NULL_TREE);
18976 break;
18977 case 2:
18978 type = build_function_type_list (op[0], op[1], NULL_TREE);
18979 break;
18980 case 3:
18981 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
18982 break;
18983 case 4:
18984 type = build_function_type_list (op[0], op[1], op[2], op[3],
18985 NULL_TREE);
18986 break;
18987 default:
18988 gcc_unreachable ();
18991 def_builtin (d->name, type, d->code);
18995 /* Hash function for builtin functions with up to 3 arguments and a return
18996 type. */
18997 hashval_t
18998 builtin_hasher::hash (builtin_hash_struct *bh)
19000 unsigned ret = 0;
19001 int i;
19003 for (i = 0; i < 4; i++)
19005 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
19006 ret = (ret * 2) + bh->uns_p[i];
19009 return ret;
19012 /* Compare builtin hash entries H1 and H2 for equivalence. */
19013 bool
19014 builtin_hasher::equal (builtin_hash_struct *p1, builtin_hash_struct *p2)
19016 return ((p1->mode[0] == p2->mode[0])
19017 && (p1->mode[1] == p2->mode[1])
19018 && (p1->mode[2] == p2->mode[2])
19019 && (p1->mode[3] == p2->mode[3])
19020 && (p1->uns_p[0] == p2->uns_p[0])
19021 && (p1->uns_p[1] == p2->uns_p[1])
19022 && (p1->uns_p[2] == p2->uns_p[2])
19023 && (p1->uns_p[3] == p2->uns_p[3]));
19026 /* Map types for builtin functions with an explicit return type and up to 3
19027 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
19028 of the argument. */
19029 static tree
19030 builtin_function_type (machine_mode mode_ret, machine_mode mode_arg0,
19031 machine_mode mode_arg1, machine_mode mode_arg2,
19032 enum rs6000_builtins builtin, const char *name)
19034 struct builtin_hash_struct h;
19035 struct builtin_hash_struct *h2;
19036 int num_args = 3;
19037 int i;
19038 tree ret_type = NULL_TREE;
19039 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
19041 /* Create builtin_hash_table. */
19042 if (builtin_hash_table == NULL)
19043 builtin_hash_table = hash_table<builtin_hasher>::create_ggc (1500);
19045 h.type = NULL_TREE;
19046 h.mode[0] = mode_ret;
19047 h.mode[1] = mode_arg0;
19048 h.mode[2] = mode_arg1;
19049 h.mode[3] = mode_arg2;
19050 h.uns_p[0] = 0;
19051 h.uns_p[1] = 0;
19052 h.uns_p[2] = 0;
19053 h.uns_p[3] = 0;
19055 /* If the builtin is a type that produces unsigned results or takes unsigned
19056 arguments, and it is returned as a decl for the vectorizer (such as
19057 widening multiplies, permute), make sure the arguments and return value
19058 are type correct. */
19059 switch (builtin)
19061 /* unsigned 1 argument functions. */
19062 case CRYPTO_BUILTIN_VSBOX:
19063 case P8V_BUILTIN_VGBBD:
19064 case MISC_BUILTIN_CDTBCD:
19065 case MISC_BUILTIN_CBCDTD:
19066 h.uns_p[0] = 1;
19067 h.uns_p[1] = 1;
19068 break;
19070 /* unsigned 2 argument functions. */
19071 case ALTIVEC_BUILTIN_VMULEUB:
19072 case ALTIVEC_BUILTIN_VMULEUH:
19073 case ALTIVEC_BUILTIN_VMULOUB:
19074 case ALTIVEC_BUILTIN_VMULOUH:
19075 case CRYPTO_BUILTIN_VCIPHER:
19076 case CRYPTO_BUILTIN_VCIPHERLAST:
19077 case CRYPTO_BUILTIN_VNCIPHER:
19078 case CRYPTO_BUILTIN_VNCIPHERLAST:
19079 case CRYPTO_BUILTIN_VPMSUMB:
19080 case CRYPTO_BUILTIN_VPMSUMH:
19081 case CRYPTO_BUILTIN_VPMSUMW:
19082 case CRYPTO_BUILTIN_VPMSUMD:
19083 case CRYPTO_BUILTIN_VPMSUM:
19084 case MISC_BUILTIN_ADDG6S:
19085 case MISC_BUILTIN_DIVWEU:
19086 case MISC_BUILTIN_DIVWEUO:
19087 case MISC_BUILTIN_DIVDEU:
19088 case MISC_BUILTIN_DIVDEUO:
19089 case VSX_BUILTIN_UDIV_V2DI:
19090 h.uns_p[0] = 1;
19091 h.uns_p[1] = 1;
19092 h.uns_p[2] = 1;
19093 break;
19095 /* unsigned 3 argument functions. */
19096 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
19097 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
19098 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
19099 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
19100 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
19101 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
19102 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
19103 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
19104 case VSX_BUILTIN_VPERM_16QI_UNS:
19105 case VSX_BUILTIN_VPERM_8HI_UNS:
19106 case VSX_BUILTIN_VPERM_4SI_UNS:
19107 case VSX_BUILTIN_VPERM_2DI_UNS:
19108 case VSX_BUILTIN_XXSEL_16QI_UNS:
19109 case VSX_BUILTIN_XXSEL_8HI_UNS:
19110 case VSX_BUILTIN_XXSEL_4SI_UNS:
19111 case VSX_BUILTIN_XXSEL_2DI_UNS:
19112 case CRYPTO_BUILTIN_VPERMXOR:
19113 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
19114 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
19115 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
19116 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
19117 case CRYPTO_BUILTIN_VSHASIGMAW:
19118 case CRYPTO_BUILTIN_VSHASIGMAD:
19119 case CRYPTO_BUILTIN_VSHASIGMA:
19120 h.uns_p[0] = 1;
19121 h.uns_p[1] = 1;
19122 h.uns_p[2] = 1;
19123 h.uns_p[3] = 1;
19124 break;
19126 /* signed permute functions with unsigned char mask. */
19127 case ALTIVEC_BUILTIN_VPERM_16QI:
19128 case ALTIVEC_BUILTIN_VPERM_8HI:
19129 case ALTIVEC_BUILTIN_VPERM_4SI:
19130 case ALTIVEC_BUILTIN_VPERM_4SF:
19131 case ALTIVEC_BUILTIN_VPERM_2DI:
19132 case ALTIVEC_BUILTIN_VPERM_2DF:
19133 case VSX_BUILTIN_VPERM_16QI:
19134 case VSX_BUILTIN_VPERM_8HI:
19135 case VSX_BUILTIN_VPERM_4SI:
19136 case VSX_BUILTIN_VPERM_4SF:
19137 case VSX_BUILTIN_VPERM_2DI:
19138 case VSX_BUILTIN_VPERM_2DF:
19139 h.uns_p[3] = 1;
19140 break;
19142 /* unsigned args, signed return. */
19143 case VSX_BUILTIN_XVCVUXDSP:
19144 case VSX_BUILTIN_XVCVUXDDP_UNS:
19145 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
19146 h.uns_p[1] = 1;
19147 break;
19149 /* signed args, unsigned return. */
19150 case VSX_BUILTIN_XVCVDPUXDS_UNS:
19151 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
19152 case MISC_BUILTIN_UNPACK_TD:
19153 case MISC_BUILTIN_UNPACK_V1TI:
19154 h.uns_p[0] = 1;
19155 break;
19157 /* unsigned arguments for 128-bit pack instructions. */
19158 case MISC_BUILTIN_PACK_TD:
19159 case MISC_BUILTIN_PACK_V1TI:
19160 h.uns_p[1] = 1;
19161 h.uns_p[2] = 1;
19162 break;
19164 default:
19165 break;
19168 /* Figure out how many args are present. */
19169 while (num_args > 0 && h.mode[num_args] == VOIDmode)
19170 num_args--;
19172 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
19173 if (!ret_type && h.uns_p[0])
19174 ret_type = builtin_mode_to_type[h.mode[0]][0];
19176 if (!ret_type)
19177 fatal_error (input_location,
19178 "internal error: builtin function %s had an unexpected "
19179 "return type %s", name, GET_MODE_NAME (h.mode[0]));
19181 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
19182 arg_type[i] = NULL_TREE;
19184 for (i = 0; i < num_args; i++)
19186 int m = (int) h.mode[i+1];
19187 int uns_p = h.uns_p[i+1];
19189 arg_type[i] = builtin_mode_to_type[m][uns_p];
19190 if (!arg_type[i] && uns_p)
19191 arg_type[i] = builtin_mode_to_type[m][0];
19193 if (!arg_type[i])
19194 fatal_error (input_location,
19195 "internal error: builtin function %s, argument %d "
19196 "had unexpected argument type %s", name, i,
19197 GET_MODE_NAME (m));
19200 builtin_hash_struct **found = builtin_hash_table->find_slot (&h, INSERT);
19201 if (*found == NULL)
19203 h2 = ggc_alloc<builtin_hash_struct> ();
19204 *h2 = h;
19205 *found = h2;
19207 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
19208 arg_type[2], NULL_TREE);
19211 return (*found)->type;
19214 static void
19215 rs6000_common_init_builtins (void)
19217 const struct builtin_description *d;
19218 size_t i;
19220 tree opaque_ftype_opaque = NULL_TREE;
19221 tree opaque_ftype_opaque_opaque = NULL_TREE;
19222 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
19223 tree v2si_ftype = NULL_TREE;
19224 tree v2si_ftype_qi = NULL_TREE;
19225 tree v2si_ftype_v2si_qi = NULL_TREE;
19226 tree v2si_ftype_int_qi = NULL_TREE;
19227 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
19229 if (!TARGET_PAIRED_FLOAT)
19231 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
19232 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
19235 /* Paired and SPE builtins are only available if you build a compiler with
19236 the appropriate options, so only create those builtins with the
19237 appropriate compiler option. Create Altivec and VSX builtins on machines
19238 with at least the general purpose extensions (970 and newer) to allow the
19239 use of the target attribute.. */
19241 if (TARGET_EXTRA_BUILTINS)
19242 builtin_mask |= RS6000_BTM_COMMON;
19244 /* Add the ternary operators. */
19245 d = bdesc_3arg;
19246 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
19248 tree type;
19249 HOST_WIDE_INT mask = d->mask;
19251 if ((mask & builtin_mask) != mask)
19253 if (TARGET_DEBUG_BUILTIN)
19254 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
19255 continue;
19258 if (rs6000_overloaded_builtin_p (d->code))
19260 if (! (type = opaque_ftype_opaque_opaque_opaque))
19261 type = opaque_ftype_opaque_opaque_opaque
19262 = build_function_type_list (opaque_V4SI_type_node,
19263 opaque_V4SI_type_node,
19264 opaque_V4SI_type_node,
19265 opaque_V4SI_type_node,
19266 NULL_TREE);
19268 else
19270 enum insn_code icode = d->icode;
19271 if (d->name == 0)
19273 if (TARGET_DEBUG_BUILTIN)
19274 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
19275 (long unsigned)i);
19277 continue;
19280 if (icode == CODE_FOR_nothing)
19282 if (TARGET_DEBUG_BUILTIN)
19283 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
19284 d->name);
19286 continue;
19289 type = builtin_function_type (insn_data[icode].operand[0].mode,
19290 insn_data[icode].operand[1].mode,
19291 insn_data[icode].operand[2].mode,
19292 insn_data[icode].operand[3].mode,
19293 d->code, d->name);
19296 def_builtin (d->name, type, d->code);
19299 /* Add the binary operators. */
19300 d = bdesc_2arg;
19301 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19303 machine_mode mode0, mode1, mode2;
19304 tree type;
19305 HOST_WIDE_INT mask = d->mask;
19307 if ((mask & builtin_mask) != mask)
19309 if (TARGET_DEBUG_BUILTIN)
19310 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
19311 continue;
19314 if (rs6000_overloaded_builtin_p (d->code))
19316 if (! (type = opaque_ftype_opaque_opaque))
19317 type = opaque_ftype_opaque_opaque
19318 = build_function_type_list (opaque_V4SI_type_node,
19319 opaque_V4SI_type_node,
19320 opaque_V4SI_type_node,
19321 NULL_TREE);
19323 else
19325 enum insn_code icode = d->icode;
19326 if (d->name == 0)
19328 if (TARGET_DEBUG_BUILTIN)
19329 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
19330 (long unsigned)i);
19332 continue;
19335 if (icode == CODE_FOR_nothing)
19337 if (TARGET_DEBUG_BUILTIN)
19338 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
19339 d->name);
19341 continue;
19344 mode0 = insn_data[icode].operand[0].mode;
19345 mode1 = insn_data[icode].operand[1].mode;
19346 mode2 = insn_data[icode].operand[2].mode;
19348 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
19350 if (! (type = v2si_ftype_v2si_qi))
19351 type = v2si_ftype_v2si_qi
19352 = build_function_type_list (opaque_V2SI_type_node,
19353 opaque_V2SI_type_node,
19354 char_type_node,
19355 NULL_TREE);
19358 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
19359 && mode2 == QImode)
19361 if (! (type = v2si_ftype_int_qi))
19362 type = v2si_ftype_int_qi
19363 = build_function_type_list (opaque_V2SI_type_node,
19364 integer_type_node,
19365 char_type_node,
19366 NULL_TREE);
19369 else
19370 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
19371 d->code, d->name);
19374 def_builtin (d->name, type, d->code);
19377 /* Add the simple unary operators. */
19378 d = bdesc_1arg;
19379 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19381 machine_mode mode0, mode1;
19382 tree type;
19383 HOST_WIDE_INT mask = d->mask;
19385 if ((mask & builtin_mask) != mask)
19387 if (TARGET_DEBUG_BUILTIN)
19388 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
19389 continue;
19392 if (rs6000_overloaded_builtin_p (d->code))
19394 if (! (type = opaque_ftype_opaque))
19395 type = opaque_ftype_opaque
19396 = build_function_type_list (opaque_V4SI_type_node,
19397 opaque_V4SI_type_node,
19398 NULL_TREE);
19400 else
19402 enum insn_code icode = d->icode;
19403 if (d->name == 0)
19405 if (TARGET_DEBUG_BUILTIN)
19406 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
19407 (long unsigned)i);
19409 continue;
19412 if (icode == CODE_FOR_nothing)
19414 if (TARGET_DEBUG_BUILTIN)
19415 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
19416 d->name);
19418 continue;
19421 mode0 = insn_data[icode].operand[0].mode;
19422 mode1 = insn_data[icode].operand[1].mode;
19424 if (mode0 == V2SImode && mode1 == QImode)
19426 if (! (type = v2si_ftype_qi))
19427 type = v2si_ftype_qi
19428 = build_function_type_list (opaque_V2SI_type_node,
19429 char_type_node,
19430 NULL_TREE);
19433 else
19434 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
19435 d->code, d->name);
19438 def_builtin (d->name, type, d->code);
19441 /* Add the simple no-argument operators. */
19442 d = bdesc_0arg;
19443 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
19445 machine_mode mode0;
19446 tree type;
19447 HOST_WIDE_INT mask = d->mask;
19449 if ((mask & builtin_mask) != mask)
19451 if (TARGET_DEBUG_BUILTIN)
19452 fprintf (stderr, "rs6000_builtin, skip no-argument %s\n", d->name);
19453 continue;
19455 if (rs6000_overloaded_builtin_p (d->code))
19457 if (!opaque_ftype_opaque)
19458 opaque_ftype_opaque
19459 = build_function_type_list (opaque_V4SI_type_node, NULL_TREE);
19460 type = opaque_ftype_opaque;
19462 else
19464 enum insn_code icode = d->icode;
19465 if (d->name == 0)
19467 if (TARGET_DEBUG_BUILTIN)
19468 fprintf (stderr, "rs6000_builtin, bdesc_0arg[%lu] no name\n",
19469 (long unsigned) i);
19470 continue;
19472 if (icode == CODE_FOR_nothing)
19474 if (TARGET_DEBUG_BUILTIN)
19475 fprintf (stderr,
19476 "rs6000_builtin, skip no-argument %s (no code)\n",
19477 d->name);
19478 continue;
19480 mode0 = insn_data[icode].operand[0].mode;
19481 if (mode0 == V2SImode)
19483 /* code for SPE */
19484 if (! (type = v2si_ftype))
19486 v2si_ftype
19487 = build_function_type_list (opaque_V2SI_type_node,
19488 NULL_TREE);
19489 type = v2si_ftype;
19492 else
19493 type = builtin_function_type (mode0, VOIDmode, VOIDmode, VOIDmode,
19494 d->code, d->name);
19496 def_builtin (d->name, type, d->code);
19500 /* Set up AIX/Darwin/64-bit Linux quad floating point routines. */
19501 static void
19502 init_float128_ibm (machine_mode mode)
19504 if (!TARGET_XL_COMPAT)
19506 set_optab_libfunc (add_optab, mode, "__gcc_qadd");
19507 set_optab_libfunc (sub_optab, mode, "__gcc_qsub");
19508 set_optab_libfunc (smul_optab, mode, "__gcc_qmul");
19509 set_optab_libfunc (sdiv_optab, mode, "__gcc_qdiv");
19511 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
19513 set_optab_libfunc (neg_optab, mode, "__gcc_qneg");
19514 set_optab_libfunc (eq_optab, mode, "__gcc_qeq");
19515 set_optab_libfunc (ne_optab, mode, "__gcc_qne");
19516 set_optab_libfunc (gt_optab, mode, "__gcc_qgt");
19517 set_optab_libfunc (ge_optab, mode, "__gcc_qge");
19518 set_optab_libfunc (lt_optab, mode, "__gcc_qlt");
19519 set_optab_libfunc (le_optab, mode, "__gcc_qle");
19521 set_conv_libfunc (sext_optab, mode, SFmode, "__gcc_stoq");
19522 set_conv_libfunc (sext_optab, mode, DFmode, "__gcc_dtoq");
19523 set_conv_libfunc (trunc_optab, SFmode, mode, "__gcc_qtos");
19524 set_conv_libfunc (trunc_optab, DFmode, mode, "__gcc_qtod");
19525 set_conv_libfunc (sfix_optab, SImode, mode, "__gcc_qtoi");
19526 set_conv_libfunc (ufix_optab, SImode, mode, "__gcc_qtou");
19527 set_conv_libfunc (sfloat_optab, mode, SImode, "__gcc_itoq");
19528 set_conv_libfunc (ufloat_optab, mode, SImode, "__gcc_utoq");
19531 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
19532 set_optab_libfunc (unord_optab, mode, "__gcc_qunord");
19534 else
19536 set_optab_libfunc (add_optab, mode, "_xlqadd");
19537 set_optab_libfunc (sub_optab, mode, "_xlqsub");
19538 set_optab_libfunc (smul_optab, mode, "_xlqmul");
19539 set_optab_libfunc (sdiv_optab, mode, "_xlqdiv");
19542 /* Add various conversions for IFmode to use the traditional TFmode
19543 names. */
19544 if (mode == IFmode)
19546 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdtf2");
19547 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddtf2");
19548 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctftd2");
19549 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunctfsd2");
19550 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunctfdd2");
19551 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtdtf2");
19553 if (TARGET_POWERPC64)
19555 set_conv_libfunc (sfix_optab, TImode, mode, "__fixtfti");
19556 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunstfti");
19557 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattitf");
19558 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntitf");
19563 /* Set up IEEE 128-bit floating point routines. Use different names if the
19564 arguments can be passed in a vector register. The historical PowerPC
19565 implementation of IEEE 128-bit floating point used _q_<op> for the names, so
19566 continue to use that if we aren't using vector registers to pass IEEE
19567 128-bit floating point. */
19569 static void
19570 init_float128_ieee (machine_mode mode)
19572 if (FLOAT128_VECTOR_P (mode))
19574 set_optab_libfunc (add_optab, mode, "__addkf3");
19575 set_optab_libfunc (sub_optab, mode, "__subkf3");
19576 set_optab_libfunc (neg_optab, mode, "__negkf2");
19577 set_optab_libfunc (smul_optab, mode, "__mulkf3");
19578 set_optab_libfunc (sdiv_optab, mode, "__divkf3");
19579 set_optab_libfunc (sqrt_optab, mode, "__sqrtkf2");
19580 set_optab_libfunc (abs_optab, mode, "__abstkf2");
19582 set_optab_libfunc (eq_optab, mode, "__eqkf2");
19583 set_optab_libfunc (ne_optab, mode, "__nekf2");
19584 set_optab_libfunc (gt_optab, mode, "__gtkf2");
19585 set_optab_libfunc (ge_optab, mode, "__gekf2");
19586 set_optab_libfunc (lt_optab, mode, "__ltkf2");
19587 set_optab_libfunc (le_optab, mode, "__lekf2");
19588 set_optab_libfunc (unord_optab, mode, "__unordkf2");
19590 set_conv_libfunc (sext_optab, mode, SFmode, "__extendsfkf2");
19591 set_conv_libfunc (sext_optab, mode, DFmode, "__extenddfkf2");
19592 set_conv_libfunc (trunc_optab, SFmode, mode, "__trunckfsf2");
19593 set_conv_libfunc (trunc_optab, DFmode, mode, "__trunckfdf2");
19595 set_conv_libfunc (sext_optab, mode, IFmode, "__extendtfkf2");
19596 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
19597 set_conv_libfunc (sext_optab, mode, TFmode, "__extendtfkf2");
19599 set_conv_libfunc (trunc_optab, IFmode, mode, "__trunckftf2");
19600 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
19601 set_conv_libfunc (trunc_optab, TFmode, mode, "__trunckftf2");
19603 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdkf2");
19604 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddkf2");
19605 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunckftd2");
19606 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunckfsd2");
19607 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunckfdd2");
19608 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtdkf2");
19610 set_conv_libfunc (sfix_optab, SImode, mode, "__fixkfsi");
19611 set_conv_libfunc (ufix_optab, SImode, mode, "__fixunskfsi");
19612 set_conv_libfunc (sfix_optab, DImode, mode, "__fixkfdi");
19613 set_conv_libfunc (ufix_optab, DImode, mode, "__fixunskfdi");
19615 set_conv_libfunc (sfloat_optab, mode, SImode, "__floatsikf");
19616 set_conv_libfunc (ufloat_optab, mode, SImode, "__floatunsikf");
19617 set_conv_libfunc (sfloat_optab, mode, DImode, "__floatdikf");
19618 set_conv_libfunc (ufloat_optab, mode, DImode, "__floatundikf");
19620 if (TARGET_POWERPC64)
19622 set_conv_libfunc (sfix_optab, TImode, mode, "__fixkfti");
19623 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunskfti");
19624 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattikf");
19625 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntikf");
19629 else
19631 set_optab_libfunc (add_optab, mode, "_q_add");
19632 set_optab_libfunc (sub_optab, mode, "_q_sub");
19633 set_optab_libfunc (neg_optab, mode, "_q_neg");
19634 set_optab_libfunc (smul_optab, mode, "_q_mul");
19635 set_optab_libfunc (sdiv_optab, mode, "_q_div");
19636 if (TARGET_PPC_GPOPT)
19637 set_optab_libfunc (sqrt_optab, mode, "_q_sqrt");
19639 set_optab_libfunc (eq_optab, mode, "_q_feq");
19640 set_optab_libfunc (ne_optab, mode, "_q_fne");
19641 set_optab_libfunc (gt_optab, mode, "_q_fgt");
19642 set_optab_libfunc (ge_optab, mode, "_q_fge");
19643 set_optab_libfunc (lt_optab, mode, "_q_flt");
19644 set_optab_libfunc (le_optab, mode, "_q_fle");
19646 set_conv_libfunc (sext_optab, mode, SFmode, "_q_stoq");
19647 set_conv_libfunc (sext_optab, mode, DFmode, "_q_dtoq");
19648 set_conv_libfunc (trunc_optab, SFmode, mode, "_q_qtos");
19649 set_conv_libfunc (trunc_optab, DFmode, mode, "_q_qtod");
19650 set_conv_libfunc (sfix_optab, SImode, mode, "_q_qtoi");
19651 set_conv_libfunc (ufix_optab, SImode, mode, "_q_qtou");
19652 set_conv_libfunc (sfloat_optab, mode, SImode, "_q_itoq");
19653 set_conv_libfunc (ufloat_optab, mode, SImode, "_q_utoq");
19657 static void
19658 rs6000_init_libfuncs (void)
19660 /* __float128 support. */
19661 if (TARGET_FLOAT128_TYPE)
19663 init_float128_ibm (IFmode);
19664 init_float128_ieee (KFmode);
19667 /* AIX/Darwin/64-bit Linux quad floating point routines. */
19668 if (TARGET_LONG_DOUBLE_128)
19670 if (!TARGET_IEEEQUAD)
19671 init_float128_ibm (TFmode);
19673 /* IEEE 128-bit including 32-bit SVR4 quad floating point routines. */
19674 else
19675 init_float128_ieee (TFmode);
19680 /* Expand a block clear operation, and return 1 if successful. Return 0
19681 if we should let the compiler generate normal code.
19683 operands[0] is the destination
19684 operands[1] is the length
19685 operands[3] is the alignment */
19688 expand_block_clear (rtx operands[])
19690 rtx orig_dest = operands[0];
19691 rtx bytes_rtx = operands[1];
19692 rtx align_rtx = operands[3];
19693 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
19694 HOST_WIDE_INT align;
19695 HOST_WIDE_INT bytes;
19696 int offset;
19697 int clear_bytes;
19698 int clear_step;
19700 /* If this is not a fixed size move, just call memcpy */
19701 if (! constp)
19702 return 0;
19704 /* This must be a fixed size alignment */
19705 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
19706 align = INTVAL (align_rtx) * BITS_PER_UNIT;
19708 /* Anything to clear? */
19709 bytes = INTVAL (bytes_rtx);
19710 if (bytes <= 0)
19711 return 1;
19713 /* Use the builtin memset after a point, to avoid huge code bloat.
19714 When optimize_size, avoid any significant code bloat; calling
19715 memset is about 4 instructions, so allow for one instruction to
19716 load zero and three to do clearing. */
19717 if (TARGET_ALTIVEC && align >= 128)
19718 clear_step = 16;
19719 else if (TARGET_POWERPC64 && (align >= 64 || !STRICT_ALIGNMENT))
19720 clear_step = 8;
19721 else if (TARGET_SPE && align >= 64)
19722 clear_step = 8;
19723 else
19724 clear_step = 4;
19726 if (optimize_size && bytes > 3 * clear_step)
19727 return 0;
19728 if (! optimize_size && bytes > 8 * clear_step)
19729 return 0;
19731 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
19733 machine_mode mode = BLKmode;
19734 rtx dest;
19736 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
19738 clear_bytes = 16;
19739 mode = V4SImode;
19741 else if (bytes >= 8 && TARGET_SPE && align >= 64)
19743 clear_bytes = 8;
19744 mode = V2SImode;
19746 else if (bytes >= 8 && TARGET_POWERPC64
19747 && (align >= 64 || !STRICT_ALIGNMENT))
19749 clear_bytes = 8;
19750 mode = DImode;
19751 if (offset == 0 && align < 64)
19753 rtx addr;
19755 /* If the address form is reg+offset with offset not a
19756 multiple of four, reload into reg indirect form here
19757 rather than waiting for reload. This way we get one
19758 reload, not one per store. */
19759 addr = XEXP (orig_dest, 0);
19760 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
19761 && GET_CODE (XEXP (addr, 1)) == CONST_INT
19762 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
19764 addr = copy_addr_to_reg (addr);
19765 orig_dest = replace_equiv_address (orig_dest, addr);
19769 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
19770 { /* move 4 bytes */
19771 clear_bytes = 4;
19772 mode = SImode;
19774 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
19775 { /* move 2 bytes */
19776 clear_bytes = 2;
19777 mode = HImode;
19779 else /* move 1 byte at a time */
19781 clear_bytes = 1;
19782 mode = QImode;
19785 dest = adjust_address (orig_dest, mode, offset);
19787 emit_move_insn (dest, CONST0_RTX (mode));
19790 return 1;
19793 /* Emit a potentially record-form instruction, setting DST from SRC.
19794 If DOT is 0, that is all; otherwise, set CCREG to the result of the
19795 signed comparison of DST with zero. If DOT is 1, the generated RTL
19796 doesn't care about the DST result; if DOT is 2, it does. If CCREG
19797 is CR0 do a single dot insn (as a PARALLEL); otherwise, do a SET and
19798 a separate COMPARE. */
19800 static void
19801 rs6000_emit_dot_insn (rtx dst, rtx src, int dot, rtx ccreg)
19803 if (dot == 0)
19805 emit_move_insn (dst, src);
19806 return;
19809 if (cc_reg_not_cr0_operand (ccreg, CCmode))
19811 emit_move_insn (dst, src);
19812 emit_move_insn (ccreg, gen_rtx_COMPARE (CCmode, dst, const0_rtx));
19813 return;
19816 rtx ccset = gen_rtx_SET (ccreg, gen_rtx_COMPARE (CCmode, src, const0_rtx));
19817 if (dot == 1)
19819 rtx clobber = gen_rtx_CLOBBER (VOIDmode, dst);
19820 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, clobber)));
19822 else
19824 rtx set = gen_rtx_SET (dst, src);
19825 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, set)));
19829 /* Figure out the correct instructions to generate to load data for
19830 block compare. MODE is used for the read from memory, and
19831 data is zero extended if REG is wider than MODE. If LE code
19832 is being generated, bswap loads are used.
19834 REG is the destination register to move the data into.
19835 MEM is the memory block being read.
19836 MODE is the mode of memory to use for the read. */
19837 static void
19838 do_load_for_compare (rtx reg, rtx mem, machine_mode mode)
19840 switch (GET_MODE (reg))
19842 case E_DImode:
19843 switch (mode)
19845 case E_QImode:
19846 emit_insn (gen_zero_extendqidi2 (reg, mem));
19847 break;
19848 case E_HImode:
19850 rtx src = mem;
19851 if (!BYTES_BIG_ENDIAN)
19853 src = gen_reg_rtx (HImode);
19854 emit_insn (gen_bswaphi2 (src, mem));
19856 emit_insn (gen_zero_extendhidi2 (reg, src));
19857 break;
19859 case E_SImode:
19861 rtx src = mem;
19862 if (!BYTES_BIG_ENDIAN)
19864 src = gen_reg_rtx (SImode);
19865 emit_insn (gen_bswapsi2 (src, mem));
19867 emit_insn (gen_zero_extendsidi2 (reg, src));
19869 break;
19870 case E_DImode:
19871 if (!BYTES_BIG_ENDIAN)
19872 emit_insn (gen_bswapdi2 (reg, mem));
19873 else
19874 emit_insn (gen_movdi (reg, mem));
19875 break;
19876 default:
19877 gcc_unreachable ();
19879 break;
19881 case E_SImode:
19882 switch (mode)
19884 case E_QImode:
19885 emit_insn (gen_zero_extendqisi2 (reg, mem));
19886 break;
19887 case E_HImode:
19889 rtx src = mem;
19890 if (!BYTES_BIG_ENDIAN)
19892 src = gen_reg_rtx (HImode);
19893 emit_insn (gen_bswaphi2 (src, mem));
19895 emit_insn (gen_zero_extendhisi2 (reg, src));
19896 break;
19898 case E_SImode:
19899 if (!BYTES_BIG_ENDIAN)
19900 emit_insn (gen_bswapsi2 (reg, mem));
19901 else
19902 emit_insn (gen_movsi (reg, mem));
19903 break;
19904 case E_DImode:
19905 /* DImode is larger than the destination reg so is not expected. */
19906 gcc_unreachable ();
19907 break;
19908 default:
19909 gcc_unreachable ();
19911 break;
19912 default:
19913 gcc_unreachable ();
19914 break;
19918 /* Select the mode to be used for reading the next chunk of bytes
19919 in the compare.
19921 OFFSET is the current read offset from the beginning of the block.
19922 BYTES is the number of bytes remaining to be read.
19923 ALIGN is the minimum alignment of the memory blocks being compared in bytes.
19924 WORD_MODE_OK indicates using WORD_MODE is allowed, else SImode is
19925 the largest allowable mode. */
19926 static machine_mode
19927 select_block_compare_mode (unsigned HOST_WIDE_INT offset,
19928 unsigned HOST_WIDE_INT bytes,
19929 unsigned HOST_WIDE_INT align, bool word_mode_ok)
19931 /* First see if we can do a whole load unit
19932 as that will be more efficient than a larger load + shift. */
19934 /* If big, use biggest chunk.
19935 If exactly chunk size, use that size.
19936 If remainder can be done in one piece with shifting, do that.
19937 Do largest chunk possible without violating alignment rules. */
19939 /* The most we can read without potential page crossing. */
19940 unsigned HOST_WIDE_INT maxread = ROUND_UP (bytes, align);
19942 if (word_mode_ok && bytes >= UNITS_PER_WORD)
19943 return word_mode;
19944 else if (bytes == GET_MODE_SIZE (SImode))
19945 return SImode;
19946 else if (bytes == GET_MODE_SIZE (HImode))
19947 return HImode;
19948 else if (bytes == GET_MODE_SIZE (QImode))
19949 return QImode;
19950 else if (bytes < GET_MODE_SIZE (SImode)
19951 && offset >= GET_MODE_SIZE (SImode) - bytes)
19952 /* This matches the case were we have SImode and 3 bytes
19953 and offset >= 1 and permits us to move back one and overlap
19954 with the previous read, thus avoiding having to shift
19955 unwanted bytes off of the input. */
19956 return SImode;
19957 else if (word_mode_ok && bytes < UNITS_PER_WORD
19958 && offset >= UNITS_PER_WORD-bytes)
19959 /* Similarly, if we can use DImode it will get matched here and
19960 can do an overlapping read that ends at the end of the block. */
19961 return word_mode;
19962 else if (word_mode_ok && maxread >= UNITS_PER_WORD)
19963 /* It is safe to do all remaining in one load of largest size,
19964 possibly with a shift to get rid of unwanted bytes. */
19965 return word_mode;
19966 else if (maxread >= GET_MODE_SIZE (SImode))
19967 /* It is safe to do all remaining in one SImode load,
19968 possibly with a shift to get rid of unwanted bytes. */
19969 return SImode;
19970 else if (bytes > GET_MODE_SIZE (SImode))
19971 return SImode;
19972 else if (bytes > GET_MODE_SIZE (HImode))
19973 return HImode;
19975 /* final fallback is do one byte */
19976 return QImode;
19979 /* Compute the alignment of pointer+OFFSET where the original alignment
19980 of pointer was BASE_ALIGN. */
19981 static unsigned HOST_WIDE_INT
19982 compute_current_alignment (unsigned HOST_WIDE_INT base_align,
19983 unsigned HOST_WIDE_INT offset)
19985 if (offset == 0)
19986 return base_align;
19987 return min (base_align, offset & -offset);
19990 /* Expand a block compare operation, and return true if successful.
19991 Return false if we should let the compiler generate normal code,
19992 probably a memcmp call.
19994 OPERANDS[0] is the target (result).
19995 OPERANDS[1] is the first source.
19996 OPERANDS[2] is the second source.
19997 OPERANDS[3] is the length.
19998 OPERANDS[4] is the alignment. */
19999 bool
20000 expand_block_compare (rtx operands[])
20002 rtx target = operands[0];
20003 rtx orig_src1 = operands[1];
20004 rtx orig_src2 = operands[2];
20005 rtx bytes_rtx = operands[3];
20006 rtx align_rtx = operands[4];
20007 HOST_WIDE_INT cmp_bytes = 0;
20008 rtx src1 = orig_src1;
20009 rtx src2 = orig_src2;
20011 /* This case is complicated to handle because the subtract
20012 with carry instructions do not generate the 64-bit
20013 carry and so we must emit code to calculate it ourselves.
20014 We choose not to implement this yet. */
20015 if (TARGET_32BIT && TARGET_POWERPC64)
20016 return false;
20018 /* If this is not a fixed size compare, just call memcmp. */
20019 if (!CONST_INT_P (bytes_rtx))
20020 return false;
20022 /* This must be a fixed size alignment. */
20023 if (!CONST_INT_P (align_rtx))
20024 return false;
20026 unsigned int base_align = UINTVAL (align_rtx) / BITS_PER_UNIT;
20028 /* rs6000_slow_unaligned_access -- don't do unaligned stuff. */
20029 if (rs6000_slow_unaligned_access (word_mode, MEM_ALIGN (orig_src1))
20030 || rs6000_slow_unaligned_access (word_mode, MEM_ALIGN (orig_src2)))
20031 return false;
20033 gcc_assert (GET_MODE (target) == SImode);
20035 /* Anything to move? */
20036 unsigned HOST_WIDE_INT bytes = UINTVAL (bytes_rtx);
20037 if (bytes == 0)
20038 return true;
20040 /* The code generated for p7 and older is not faster than glibc
20041 memcmp if alignment is small and length is not short, so bail
20042 out to avoid those conditions. */
20043 if (!TARGET_EFFICIENT_OVERLAPPING_UNALIGNED
20044 && ((base_align == 1 && bytes > 16)
20045 || (base_align == 2 && bytes > 32)))
20046 return false;
20048 rtx tmp_reg_src1 = gen_reg_rtx (word_mode);
20049 rtx tmp_reg_src2 = gen_reg_rtx (word_mode);
20050 /* P7/P8 code uses cond for subfc. but P9 uses
20051 it for cmpld which needs CCUNSmode. */
20052 rtx cond;
20053 if (TARGET_P9_MISC)
20054 cond = gen_reg_rtx (CCUNSmode);
20055 else
20056 cond = gen_reg_rtx (CCmode);
20058 /* If we have an LE target without ldbrx and word_mode is DImode,
20059 then we must avoid using word_mode. */
20060 int word_mode_ok = !(!BYTES_BIG_ENDIAN && !TARGET_LDBRX
20061 && word_mode == DImode);
20063 /* Strategy phase. How many ops will this take and should we expand it? */
20065 unsigned HOST_WIDE_INT offset = 0;
20066 machine_mode load_mode =
20067 select_block_compare_mode (offset, bytes, base_align, word_mode_ok);
20068 unsigned int load_mode_size = GET_MODE_SIZE (load_mode);
20070 /* We don't want to generate too much code. */
20071 unsigned HOST_WIDE_INT max_bytes =
20072 load_mode_size * (unsigned HOST_WIDE_INT) rs6000_block_compare_inline_limit;
20073 if (!IN_RANGE (bytes, 1, max_bytes))
20074 return false;
20076 bool generate_6432_conversion = false;
20077 rtx convert_label = NULL;
20078 rtx final_label = NULL;
20080 /* Example of generated code for 18 bytes aligned 1 byte.
20081 Compiled with -fno-reorder-blocks for clarity.
20082 ldbrx 10,31,8
20083 ldbrx 9,7,8
20084 subfc. 9,9,10
20085 bne 0,.L6487
20086 addi 9,12,8
20087 addi 5,11,8
20088 ldbrx 10,0,9
20089 ldbrx 9,0,5
20090 subfc. 9,9,10
20091 bne 0,.L6487
20092 addi 9,12,16
20093 lhbrx 10,0,9
20094 addi 9,11,16
20095 lhbrx 9,0,9
20096 subf 9,9,10
20097 b .L6488
20098 .p2align 4,,15
20099 .L6487: #convert_label
20100 popcntd 9,9
20101 subfe 10,10,10
20102 or 9,9,10
20103 .L6488: #final_label
20104 extsw 10,9
20106 We start off with DImode for two blocks that jump to the DI->SI conversion
20107 if the difference is found there, then a final block of HImode that skips
20108 the DI->SI conversion. */
20110 while (bytes > 0)
20112 unsigned int align = compute_current_alignment (base_align, offset);
20113 if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20114 load_mode = select_block_compare_mode (offset, bytes, align,
20115 word_mode_ok);
20116 else
20117 load_mode = select_block_compare_mode (0, bytes, align, word_mode_ok);
20118 load_mode_size = GET_MODE_SIZE (load_mode);
20119 if (bytes >= load_mode_size)
20120 cmp_bytes = load_mode_size;
20121 else if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20123 /* Move this load back so it doesn't go past the end.
20124 P8/P9 can do this efficiently. */
20125 unsigned int extra_bytes = load_mode_size - bytes;
20126 cmp_bytes = bytes;
20127 if (extra_bytes < offset)
20129 offset -= extra_bytes;
20130 cmp_bytes = load_mode_size;
20131 bytes = cmp_bytes;
20134 else
20135 /* P7 and earlier can't do the overlapping load trick fast,
20136 so this forces a non-overlapping load and a shift to get
20137 rid of the extra bytes. */
20138 cmp_bytes = bytes;
20140 src1 = adjust_address (orig_src1, load_mode, offset);
20141 src2 = adjust_address (orig_src2, load_mode, offset);
20143 if (!REG_P (XEXP (src1, 0)))
20145 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20146 src1 = replace_equiv_address (src1, src1_reg);
20148 set_mem_size (src1, cmp_bytes);
20150 if (!REG_P (XEXP (src2, 0)))
20152 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20153 src2 = replace_equiv_address (src2, src2_reg);
20155 set_mem_size (src2, cmp_bytes);
20157 do_load_for_compare (tmp_reg_src1, src1, load_mode);
20158 do_load_for_compare (tmp_reg_src2, src2, load_mode);
20160 if (cmp_bytes < load_mode_size)
20162 /* Shift unneeded bytes off. */
20163 rtx sh = GEN_INT (BITS_PER_UNIT * (load_mode_size - cmp_bytes));
20164 if (word_mode == DImode)
20166 emit_insn (gen_lshrdi3 (tmp_reg_src1, tmp_reg_src1, sh));
20167 emit_insn (gen_lshrdi3 (tmp_reg_src2, tmp_reg_src2, sh));
20169 else
20171 emit_insn (gen_lshrsi3 (tmp_reg_src1, tmp_reg_src1, sh));
20172 emit_insn (gen_lshrsi3 (tmp_reg_src2, tmp_reg_src2, sh));
20176 int remain = bytes - cmp_bytes;
20177 if (GET_MODE_SIZE (GET_MODE (target)) > GET_MODE_SIZE (load_mode))
20179 /* Target is larger than load size so we don't need to
20180 reduce result size. */
20182 /* We previously did a block that need 64->32 conversion but
20183 the current block does not, so a label is needed to jump
20184 to the end. */
20185 if (generate_6432_conversion && !final_label)
20186 final_label = gen_label_rtx ();
20188 if (remain > 0)
20190 /* This is not the last block, branch to the end if the result
20191 of this subtract is not zero. */
20192 if (!final_label)
20193 final_label = gen_label_rtx ();
20194 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20195 rtx tmp = gen_rtx_MINUS (word_mode, tmp_reg_src1, tmp_reg_src2);
20196 rtx cr = gen_reg_rtx (CCmode);
20197 rs6000_emit_dot_insn (tmp_reg_src2, tmp, 2, cr);
20198 emit_insn (gen_movsi (target,
20199 gen_lowpart (SImode, tmp_reg_src2)));
20200 rtx ne_rtx = gen_rtx_NE (VOIDmode, cr, const0_rtx);
20201 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
20202 fin_ref, pc_rtx);
20203 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20204 JUMP_LABEL (j) = final_label;
20205 LABEL_NUSES (final_label) += 1;
20207 else
20209 if (word_mode == DImode)
20211 emit_insn (gen_subdi3 (tmp_reg_src2, tmp_reg_src1,
20212 tmp_reg_src2));
20213 emit_insn (gen_movsi (target,
20214 gen_lowpart (SImode, tmp_reg_src2)));
20216 else
20217 emit_insn (gen_subsi3 (target, tmp_reg_src1, tmp_reg_src2));
20219 if (final_label)
20221 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20222 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, fin_ref));
20223 JUMP_LABEL(j) = final_label;
20224 LABEL_NUSES (final_label) += 1;
20225 emit_barrier ();
20229 else
20231 /* Do we need a 64->32 conversion block? We need the 64->32
20232 conversion even if target size == load_mode size because
20233 the subtract generates one extra bit. */
20234 generate_6432_conversion = true;
20236 if (remain > 0)
20238 if (!convert_label)
20239 convert_label = gen_label_rtx ();
20241 /* Compare to zero and branch to convert_label if not zero. */
20242 rtx cvt_ref = gen_rtx_LABEL_REF (VOIDmode, convert_label);
20243 if (TARGET_P9_MISC)
20245 /* Generate a compare, and convert with a setb later. */
20246 rtx cmp = gen_rtx_COMPARE (CCUNSmode, tmp_reg_src1,
20247 tmp_reg_src2);
20248 emit_insn (gen_rtx_SET (cond, cmp));
20250 else
20251 /* Generate a subfc. and use the longer
20252 sequence for conversion. */
20253 if (TARGET_64BIT)
20254 emit_insn (gen_subfdi3_carry_dot2 (tmp_reg_src2, tmp_reg_src2,
20255 tmp_reg_src1, cond));
20256 else
20257 emit_insn (gen_subfsi3_carry_dot2 (tmp_reg_src2, tmp_reg_src2,
20258 tmp_reg_src1, cond));
20259 rtx ne_rtx = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20260 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
20261 cvt_ref, pc_rtx);
20262 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20263 JUMP_LABEL(j) = convert_label;
20264 LABEL_NUSES (convert_label) += 1;
20266 else
20268 /* Just do the subtract/compare. Since this is the last block
20269 the convert code will be generated immediately following. */
20270 if (TARGET_P9_MISC)
20272 rtx cmp = gen_rtx_COMPARE (CCUNSmode, tmp_reg_src1,
20273 tmp_reg_src2);
20274 emit_insn (gen_rtx_SET (cond, cmp));
20276 else
20277 if (TARGET_64BIT)
20278 emit_insn (gen_subfdi3_carry (tmp_reg_src2, tmp_reg_src2,
20279 tmp_reg_src1));
20280 else
20281 emit_insn (gen_subfsi3_carry (tmp_reg_src2, tmp_reg_src2,
20282 tmp_reg_src1));
20286 offset += cmp_bytes;
20287 bytes -= cmp_bytes;
20290 if (generate_6432_conversion)
20292 if (convert_label)
20293 emit_label (convert_label);
20295 /* We need to produce DI result from sub, then convert to target SI
20296 while maintaining <0 / ==0 / >0 properties. This sequence works:
20297 subfc L,A,B
20298 subfe H,H,H
20299 popcntd L,L
20300 rldimi L,H,6,0
20302 This is an alternate one Segher cooked up if somebody
20303 wants to expand this for something that doesn't have popcntd:
20304 subfc L,a,b
20305 subfe H,x,x
20306 addic t,L,-1
20307 subfe v,t,L
20308 or z,v,H
20310 And finally, p9 can just do this:
20311 cmpld A,B
20312 setb r */
20314 if (TARGET_P9_MISC)
20316 emit_insn (gen_setb_unsigned (target, cond));
20318 else
20320 if (TARGET_64BIT)
20322 rtx tmp_reg_ca = gen_reg_rtx (DImode);
20323 emit_insn (gen_subfdi3_carry_in_xx (tmp_reg_ca));
20324 emit_insn (gen_popcntddi2 (tmp_reg_src2, tmp_reg_src2));
20325 emit_insn (gen_iordi3 (tmp_reg_src2, tmp_reg_src2, tmp_reg_ca));
20326 emit_insn (gen_movsi (target, gen_lowpart (SImode, tmp_reg_src2)));
20328 else
20330 rtx tmp_reg_ca = gen_reg_rtx (SImode);
20331 emit_insn (gen_subfsi3_carry_in_xx (tmp_reg_ca));
20332 emit_insn (gen_popcntdsi2 (tmp_reg_src2, tmp_reg_src2));
20333 emit_insn (gen_iorsi3 (target, tmp_reg_src2, tmp_reg_ca));
20338 if (final_label)
20339 emit_label (final_label);
20341 gcc_assert (bytes == 0);
20342 return true;
20345 /* Generate alignment check and branch code to set up for
20346 strncmp when we don't have DI alignment.
20347 STRNCMP_LABEL is the label to branch if there is a page crossing.
20348 SRC is the string pointer to be examined.
20349 BYTES is the max number of bytes to compare. */
20350 static void
20351 expand_strncmp_align_check (rtx strncmp_label, rtx src, HOST_WIDE_INT bytes)
20353 rtx lab_ref = gen_rtx_LABEL_REF (VOIDmode, strncmp_label);
20354 rtx src_check = copy_addr_to_reg (XEXP (src, 0));
20355 if (GET_MODE (src_check) == SImode)
20356 emit_insn (gen_andsi3 (src_check, src_check, GEN_INT (0xfff)));
20357 else
20358 emit_insn (gen_anddi3 (src_check, src_check, GEN_INT (0xfff)));
20359 rtx cond = gen_reg_rtx (CCmode);
20360 emit_move_insn (cond, gen_rtx_COMPARE (CCmode, src_check,
20361 GEN_INT (4096 - bytes)));
20363 rtx cmp_rtx = gen_rtx_LT (VOIDmode, cond, const0_rtx);
20365 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, cmp_rtx,
20366 pc_rtx, lab_ref);
20367 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20368 JUMP_LABEL (j) = strncmp_label;
20369 LABEL_NUSES (strncmp_label) += 1;
20372 /* Expand a string compare operation with length, and return
20373 true if successful. Return false if we should let the
20374 compiler generate normal code, probably a strncmp call.
20376 OPERANDS[0] is the target (result).
20377 OPERANDS[1] is the first source.
20378 OPERANDS[2] is the second source.
20379 If NO_LENGTH is zero, then:
20380 OPERANDS[3] is the length.
20381 OPERANDS[4] is the alignment in bytes.
20382 If NO_LENGTH is nonzero, then:
20383 OPERANDS[3] is the alignment in bytes. */
20384 bool
20385 expand_strn_compare (rtx operands[], int no_length)
20387 rtx target = operands[0];
20388 rtx orig_src1 = operands[1];
20389 rtx orig_src2 = operands[2];
20390 rtx bytes_rtx, align_rtx;
20391 if (no_length)
20393 bytes_rtx = NULL;
20394 align_rtx = operands[3];
20396 else
20398 bytes_rtx = operands[3];
20399 align_rtx = operands[4];
20401 unsigned HOST_WIDE_INT cmp_bytes = 0;
20402 rtx src1 = orig_src1;
20403 rtx src2 = orig_src2;
20405 /* If we have a length, it must be constant. This simplifies things
20406 a bit as we don't have to generate code to check if we've exceeded
20407 the length. Later this could be expanded to handle this case. */
20408 if (!no_length && !CONST_INT_P (bytes_rtx))
20409 return false;
20411 /* This must be a fixed size alignment. */
20412 if (!CONST_INT_P (align_rtx))
20413 return false;
20415 unsigned int base_align = UINTVAL (align_rtx);
20416 int align1 = MEM_ALIGN (orig_src1) / BITS_PER_UNIT;
20417 int align2 = MEM_ALIGN (orig_src2) / BITS_PER_UNIT;
20419 /* rs6000_slow_unaligned_access -- don't do unaligned stuff. */
20420 if (rs6000_slow_unaligned_access (word_mode, align1)
20421 || rs6000_slow_unaligned_access (word_mode, align2))
20422 return false;
20424 gcc_assert (GET_MODE (target) == SImode);
20426 /* If we have an LE target without ldbrx and word_mode is DImode,
20427 then we must avoid using word_mode. */
20428 int word_mode_ok = !(!BYTES_BIG_ENDIAN && !TARGET_LDBRX
20429 && word_mode == DImode);
20431 unsigned int word_mode_size = GET_MODE_SIZE (word_mode);
20433 unsigned HOST_WIDE_INT offset = 0;
20434 unsigned HOST_WIDE_INT bytes; /* N from the strncmp args if available. */
20435 unsigned HOST_WIDE_INT compare_length; /* How much to compare inline. */
20436 if (no_length)
20437 /* Use this as a standin to determine the mode to use. */
20438 bytes = rs6000_string_compare_inline_limit * word_mode_size;
20439 else
20440 bytes = UINTVAL (bytes_rtx);
20442 machine_mode load_mode =
20443 select_block_compare_mode (offset, bytes, base_align, word_mode_ok);
20444 unsigned int load_mode_size = GET_MODE_SIZE (load_mode);
20445 compare_length = rs6000_string_compare_inline_limit * load_mode_size;
20447 /* If we have equality at the end of the last compare and we have not
20448 found the end of the string, we need to call strcmp/strncmp to
20449 compare the remainder. */
20450 bool equality_compare_rest = false;
20452 if (no_length)
20454 bytes = compare_length;
20455 equality_compare_rest = true;
20457 else
20459 if (bytes <= compare_length)
20460 compare_length = bytes;
20461 else
20462 equality_compare_rest = true;
20465 rtx result_reg = gen_reg_rtx (word_mode);
20466 rtx final_move_label = gen_label_rtx ();
20467 rtx final_label = gen_label_rtx ();
20468 rtx begin_compare_label = NULL;
20470 if (base_align < 8)
20472 /* Generate code that checks distance to 4k boundary for this case. */
20473 begin_compare_label = gen_label_rtx ();
20474 rtx strncmp_label = gen_label_rtx ();
20475 rtx jmp;
20477 /* Strncmp for power8 in glibc does this:
20478 rldicl r8,r3,0,52
20479 cmpldi cr7,r8,4096-16
20480 bgt cr7,L(pagecross) */
20482 /* Make sure that the length we use for the alignment test and
20483 the subsequent code generation are in agreement so we do not
20484 go past the length we tested for a 4k boundary crossing. */
20485 unsigned HOST_WIDE_INT align_test = compare_length;
20486 if (align_test < 8)
20488 align_test = HOST_WIDE_INT_1U << ceil_log2 (align_test);
20489 base_align = align_test;
20491 else
20493 align_test = ROUND_UP (align_test, 8);
20494 base_align = 8;
20497 if (align1 < 8)
20498 expand_strncmp_align_check (strncmp_label, src1, align_test);
20499 if (align2 < 8)
20500 expand_strncmp_align_check (strncmp_label, src2, align_test);
20502 /* Now generate the following sequence:
20503 - branch to begin_compare
20504 - strncmp_label
20505 - call to strncmp
20506 - branch to final_label
20507 - begin_compare_label */
20509 rtx cmp_ref = gen_rtx_LABEL_REF (VOIDmode, begin_compare_label);
20510 jmp = emit_jump_insn (gen_rtx_SET (pc_rtx, cmp_ref));
20511 JUMP_LABEL (jmp) = begin_compare_label;
20512 LABEL_NUSES (begin_compare_label) += 1;
20513 emit_barrier ();
20515 emit_label (strncmp_label);
20517 if (!REG_P (XEXP (src1, 0)))
20519 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20520 src1 = replace_equiv_address (src1, src1_reg);
20523 if (!REG_P (XEXP (src2, 0)))
20525 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20526 src2 = replace_equiv_address (src2, src2_reg);
20529 if (no_length)
20531 tree fun = builtin_decl_explicit (BUILT_IN_STRCMP);
20532 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20533 target, LCT_NORMAL, GET_MODE (target),
20534 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20535 force_reg (Pmode, XEXP (src2, 0)), Pmode);
20537 else
20539 /* -m32 -mpowerpc64 results in word_mode being DImode even
20540 though otherwise it is 32-bit. The length arg to strncmp
20541 is a size_t which will be the same size as pointers. */
20542 rtx len_rtx;
20543 if (TARGET_64BIT)
20544 len_rtx = gen_reg_rtx (DImode);
20545 else
20546 len_rtx = gen_reg_rtx (SImode);
20548 emit_move_insn (len_rtx, bytes_rtx);
20550 tree fun = builtin_decl_explicit (BUILT_IN_STRNCMP);
20551 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20552 target, LCT_NORMAL, GET_MODE (target),
20553 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20554 force_reg (Pmode, XEXP (src2, 0)), Pmode,
20555 len_rtx, GET_MODE (len_rtx));
20558 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20559 jmp = emit_jump_insn (gen_rtx_SET (pc_rtx, fin_ref));
20560 JUMP_LABEL (jmp) = final_label;
20561 LABEL_NUSES (final_label) += 1;
20562 emit_barrier ();
20563 emit_label (begin_compare_label);
20566 rtx cleanup_label = NULL;
20567 rtx tmp_reg_src1 = gen_reg_rtx (word_mode);
20568 rtx tmp_reg_src2 = gen_reg_rtx (word_mode);
20570 /* Generate sequence of ld/ldbrx, cmpb to compare out
20571 to the length specified. */
20572 unsigned HOST_WIDE_INT bytes_to_compare = compare_length;
20573 while (bytes_to_compare > 0)
20575 /* Compare sequence:
20576 check each 8B with: ld/ld cmpd bne
20577 If equal, use rldicr/cmpb to check for zero byte.
20578 cleanup code at end:
20579 cmpb get byte that differs
20580 cmpb look for zero byte
20581 orc combine
20582 cntlzd get bit of first zero/diff byte
20583 subfic convert for rldcl use
20584 rldcl rldcl extract diff/zero byte
20585 subf subtract for final result
20587 The last compare can branch around the cleanup code if the
20588 result is zero because the strings are exactly equal. */
20589 unsigned int align = compute_current_alignment (base_align, offset);
20590 if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20591 load_mode = select_block_compare_mode (offset, bytes_to_compare, align,
20592 word_mode_ok);
20593 else
20594 load_mode = select_block_compare_mode (0, bytes_to_compare, align,
20595 word_mode_ok);
20596 load_mode_size = GET_MODE_SIZE (load_mode);
20597 if (bytes_to_compare >= load_mode_size)
20598 cmp_bytes = load_mode_size;
20599 else if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20601 /* Move this load back so it doesn't go past the end.
20602 P8/P9 can do this efficiently. */
20603 unsigned int extra_bytes = load_mode_size - bytes_to_compare;
20604 cmp_bytes = bytes_to_compare;
20605 if (extra_bytes < offset)
20607 offset -= extra_bytes;
20608 cmp_bytes = load_mode_size;
20609 bytes_to_compare = cmp_bytes;
20612 else
20613 /* P7 and earlier can't do the overlapping load trick fast,
20614 so this forces a non-overlapping load and a shift to get
20615 rid of the extra bytes. */
20616 cmp_bytes = bytes_to_compare;
20618 src1 = adjust_address (orig_src1, load_mode, offset);
20619 src2 = adjust_address (orig_src2, load_mode, offset);
20621 if (!REG_P (XEXP (src1, 0)))
20623 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20624 src1 = replace_equiv_address (src1, src1_reg);
20626 set_mem_size (src1, cmp_bytes);
20628 if (!REG_P (XEXP (src2, 0)))
20630 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20631 src2 = replace_equiv_address (src2, src2_reg);
20633 set_mem_size (src2, cmp_bytes);
20635 do_load_for_compare (tmp_reg_src1, src1, load_mode);
20636 do_load_for_compare (tmp_reg_src2, src2, load_mode);
20638 /* We must always left-align the data we read, and
20639 clear any bytes to the right that are beyond the string.
20640 Otherwise the cmpb sequence won't produce the correct
20641 results. The beginning of the compare will be done
20642 with word_mode so will not have any extra shifts or
20643 clear rights. */
20645 if (load_mode_size < word_mode_size)
20647 /* Rotate left first. */
20648 rtx sh = GEN_INT (BITS_PER_UNIT * (word_mode_size - load_mode_size));
20649 if (word_mode == DImode)
20651 emit_insn (gen_rotldi3 (tmp_reg_src1, tmp_reg_src1, sh));
20652 emit_insn (gen_rotldi3 (tmp_reg_src2, tmp_reg_src2, sh));
20654 else
20656 emit_insn (gen_rotlsi3 (tmp_reg_src1, tmp_reg_src1, sh));
20657 emit_insn (gen_rotlsi3 (tmp_reg_src2, tmp_reg_src2, sh));
20661 if (cmp_bytes < word_mode_size)
20663 /* Now clear right. This plus the rotate can be
20664 turned into a rldicr instruction. */
20665 HOST_WIDE_INT mb = BITS_PER_UNIT * (word_mode_size - cmp_bytes);
20666 rtx mask = GEN_INT (HOST_WIDE_INT_M1U << mb);
20667 if (word_mode == DImode)
20669 emit_insn (gen_anddi3_mask (tmp_reg_src1, tmp_reg_src1, mask));
20670 emit_insn (gen_anddi3_mask (tmp_reg_src2, tmp_reg_src2, mask));
20672 else
20674 emit_insn (gen_andsi3_mask (tmp_reg_src1, tmp_reg_src1, mask));
20675 emit_insn (gen_andsi3_mask (tmp_reg_src2, tmp_reg_src2, mask));
20679 /* Cases to handle. A and B are chunks of the two strings.
20680 1: Not end of comparison:
20681 A != B: branch to cleanup code to compute result.
20682 A == B: check for 0 byte, next block if not found.
20683 2: End of the inline comparison:
20684 A != B: branch to cleanup code to compute result.
20685 A == B: check for 0 byte, call strcmp/strncmp
20686 3: compared requested N bytes:
20687 A == B: branch to result 0.
20688 A != B: cleanup code to compute result. */
20690 unsigned HOST_WIDE_INT remain = bytes_to_compare - cmp_bytes;
20692 rtx dst_label;
20693 if (remain > 0 || equality_compare_rest)
20695 /* Branch to cleanup code, otherwise fall through to do
20696 more compares. */
20697 if (!cleanup_label)
20698 cleanup_label = gen_label_rtx ();
20699 dst_label = cleanup_label;
20701 else
20702 /* Branch to end and produce result of 0. */
20703 dst_label = final_move_label;
20705 rtx lab_ref = gen_rtx_LABEL_REF (VOIDmode, dst_label);
20706 rtx cond = gen_reg_rtx (CCmode);
20708 /* Always produce the 0 result, it is needed if
20709 cmpb finds a 0 byte in this chunk. */
20710 rtx tmp = gen_rtx_MINUS (word_mode, tmp_reg_src1, tmp_reg_src2);
20711 rs6000_emit_dot_insn (result_reg, tmp, 1, cond);
20713 rtx cmp_rtx;
20714 if (remain == 0 && !equality_compare_rest)
20715 cmp_rtx = gen_rtx_EQ (VOIDmode, cond, const0_rtx);
20716 else
20717 cmp_rtx = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20719 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, cmp_rtx,
20720 lab_ref, pc_rtx);
20721 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20722 JUMP_LABEL (j) = dst_label;
20723 LABEL_NUSES (dst_label) += 1;
20725 if (remain > 0 || equality_compare_rest)
20727 /* Generate a cmpb to test for a 0 byte and branch
20728 to final result if found. */
20729 rtx cmpb_zero = gen_reg_rtx (word_mode);
20730 rtx lab_ref_fin = gen_rtx_LABEL_REF (VOIDmode, final_move_label);
20731 rtx condz = gen_reg_rtx (CCmode);
20732 rtx zero_reg = gen_reg_rtx (word_mode);
20733 if (word_mode == SImode)
20735 emit_insn (gen_movsi (zero_reg, GEN_INT (0)));
20736 emit_insn (gen_cmpbsi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20737 if (cmp_bytes < word_mode_size)
20739 /* Don't want to look at zero bytes past end. */
20740 HOST_WIDE_INT mb =
20741 BITS_PER_UNIT * (word_mode_size - cmp_bytes);
20742 rtx mask = GEN_INT (HOST_WIDE_INT_M1U << mb);
20743 emit_insn (gen_andsi3_mask (cmpb_zero, cmpb_zero, mask));
20746 else
20748 emit_insn (gen_movdi (zero_reg, GEN_INT (0)));
20749 emit_insn (gen_cmpbdi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20750 if (cmp_bytes < word_mode_size)
20752 /* Don't want to look at zero bytes past end. */
20753 HOST_WIDE_INT mb =
20754 BITS_PER_UNIT * (word_mode_size - cmp_bytes);
20755 rtx mask = GEN_INT (HOST_WIDE_INT_M1U << mb);
20756 emit_insn (gen_anddi3_mask (cmpb_zero, cmpb_zero, mask));
20760 emit_move_insn (condz, gen_rtx_COMPARE (CCmode, cmpb_zero, zero_reg));
20761 rtx cmpnz_rtx = gen_rtx_NE (VOIDmode, condz, const0_rtx);
20762 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, cmpnz_rtx,
20763 lab_ref_fin, pc_rtx);
20764 rtx j2 = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20765 JUMP_LABEL (j2) = final_move_label;
20766 LABEL_NUSES (final_move_label) += 1;
20770 offset += cmp_bytes;
20771 bytes_to_compare -= cmp_bytes;
20774 if (equality_compare_rest)
20776 /* Update pointers past what has been compared already. */
20777 src1 = adjust_address (orig_src1, load_mode, offset);
20778 src2 = adjust_address (orig_src2, load_mode, offset);
20780 if (!REG_P (XEXP (src1, 0)))
20782 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20783 src1 = replace_equiv_address (src1, src1_reg);
20785 set_mem_size (src1, cmp_bytes);
20787 if (!REG_P (XEXP (src2, 0)))
20789 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20790 src2 = replace_equiv_address (src2, src2_reg);
20792 set_mem_size (src2, cmp_bytes);
20794 /* Construct call to strcmp/strncmp to compare the rest of the string. */
20795 if (no_length)
20797 tree fun = builtin_decl_explicit (BUILT_IN_STRCMP);
20798 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20799 target, LCT_NORMAL, GET_MODE (target),
20800 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20801 force_reg (Pmode, XEXP (src2, 0)), Pmode);
20803 else
20805 rtx len_rtx;
20806 if (TARGET_64BIT)
20807 len_rtx = gen_reg_rtx (DImode);
20808 else
20809 len_rtx = gen_reg_rtx (SImode);
20811 emit_move_insn (len_rtx, GEN_INT (bytes - compare_length));
20812 tree fun = builtin_decl_explicit (BUILT_IN_STRNCMP);
20813 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20814 target, LCT_NORMAL, GET_MODE (target),
20815 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20816 force_reg (Pmode, XEXP (src2, 0)), Pmode,
20817 len_rtx, GET_MODE (len_rtx));
20820 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20821 rtx jmp = emit_jump_insn (gen_rtx_SET (pc_rtx, fin_ref));
20822 JUMP_LABEL (jmp) = final_label;
20823 LABEL_NUSES (final_label) += 1;
20824 emit_barrier ();
20827 if (cleanup_label)
20828 emit_label (cleanup_label);
20830 /* Generate the final sequence that identifies the differing
20831 byte and generates the final result, taking into account
20832 zero bytes:
20834 cmpb cmpb_result1, src1, src2
20835 cmpb cmpb_result2, src1, zero
20836 orc cmpb_result1, cmp_result1, cmpb_result2
20837 cntlzd get bit of first zero/diff byte
20838 addi convert for rldcl use
20839 rldcl rldcl extract diff/zero byte
20840 subf subtract for final result
20843 rtx cmpb_diff = gen_reg_rtx (word_mode);
20844 rtx cmpb_zero = gen_reg_rtx (word_mode);
20845 rtx rot_amt = gen_reg_rtx (word_mode);
20846 rtx zero_reg = gen_reg_rtx (word_mode);
20848 rtx rot1_1 = gen_reg_rtx (word_mode);
20849 rtx rot1_2 = gen_reg_rtx (word_mode);
20850 rtx rot2_1 = gen_reg_rtx (word_mode);
20851 rtx rot2_2 = gen_reg_rtx (word_mode);
20853 if (word_mode == SImode)
20855 emit_insn (gen_cmpbsi3 (cmpb_diff, tmp_reg_src1, tmp_reg_src2));
20856 emit_insn (gen_movsi (zero_reg, GEN_INT (0)));
20857 emit_insn (gen_cmpbsi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20858 emit_insn (gen_one_cmplsi2 (cmpb_diff,cmpb_diff));
20859 emit_insn (gen_iorsi3 (cmpb_diff, cmpb_diff, cmpb_zero));
20860 emit_insn (gen_clzsi2 (rot_amt, cmpb_diff));
20861 emit_insn (gen_addsi3 (rot_amt, rot_amt, GEN_INT (8)));
20862 emit_insn (gen_rotlsi3 (rot1_1, tmp_reg_src1,
20863 gen_lowpart (SImode, rot_amt)));
20864 emit_insn (gen_andsi3_mask (rot1_2, rot1_1, GEN_INT (0xff)));
20865 emit_insn (gen_rotlsi3 (rot2_1, tmp_reg_src2,
20866 gen_lowpart (SImode, rot_amt)));
20867 emit_insn (gen_andsi3_mask (rot2_2, rot2_1, GEN_INT (0xff)));
20868 emit_insn (gen_subsi3 (result_reg, rot1_2, rot2_2));
20870 else
20872 emit_insn (gen_cmpbdi3 (cmpb_diff, tmp_reg_src1, tmp_reg_src2));
20873 emit_insn (gen_movdi (zero_reg, GEN_INT (0)));
20874 emit_insn (gen_cmpbdi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20875 emit_insn (gen_one_cmpldi2 (cmpb_diff,cmpb_diff));
20876 emit_insn (gen_iordi3 (cmpb_diff, cmpb_diff, cmpb_zero));
20877 emit_insn (gen_clzdi2 (rot_amt, cmpb_diff));
20878 emit_insn (gen_adddi3 (rot_amt, rot_amt, GEN_INT (8)));
20879 emit_insn (gen_rotldi3 (rot1_1, tmp_reg_src1,
20880 gen_lowpart (SImode, rot_amt)));
20881 emit_insn (gen_anddi3_mask (rot1_2, rot1_1, GEN_INT (0xff)));
20882 emit_insn (gen_rotldi3 (rot2_1, tmp_reg_src2,
20883 gen_lowpart (SImode, rot_amt)));
20884 emit_insn (gen_anddi3_mask (rot2_2, rot2_1, GEN_INT (0xff)));
20885 emit_insn (gen_subdi3 (result_reg, rot1_2, rot2_2));
20888 emit_label (final_move_label);
20889 emit_insn (gen_movsi (target,
20890 gen_lowpart (SImode, result_reg)));
20891 emit_label (final_label);
20892 return true;
20895 /* Expand a block move operation, and return 1 if successful. Return 0
20896 if we should let the compiler generate normal code.
20898 operands[0] is the destination
20899 operands[1] is the source
20900 operands[2] is the length
20901 operands[3] is the alignment */
20903 #define MAX_MOVE_REG 4
20906 expand_block_move (rtx operands[])
20908 rtx orig_dest = operands[0];
20909 rtx orig_src = operands[1];
20910 rtx bytes_rtx = operands[2];
20911 rtx align_rtx = operands[3];
20912 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
20913 int align;
20914 int bytes;
20915 int offset;
20916 int move_bytes;
20917 rtx stores[MAX_MOVE_REG];
20918 int num_reg = 0;
20920 /* If this is not a fixed size move, just call memcpy */
20921 if (! constp)
20922 return 0;
20924 /* This must be a fixed size alignment */
20925 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
20926 align = INTVAL (align_rtx) * BITS_PER_UNIT;
20928 /* Anything to move? */
20929 bytes = INTVAL (bytes_rtx);
20930 if (bytes <= 0)
20931 return 1;
20933 if (bytes > rs6000_block_move_inline_limit)
20934 return 0;
20936 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
20938 union {
20939 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
20940 rtx (*mov) (rtx, rtx);
20941 } gen_func;
20942 machine_mode mode = BLKmode;
20943 rtx src, dest;
20945 /* Altivec first, since it will be faster than a string move
20946 when it applies, and usually not significantly larger. */
20947 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
20949 move_bytes = 16;
20950 mode = V4SImode;
20951 gen_func.mov = gen_movv4si;
20953 else if (TARGET_SPE && bytes >= 8 && align >= 64)
20955 move_bytes = 8;
20956 mode = V2SImode;
20957 gen_func.mov = gen_movv2si;
20959 else if (TARGET_STRING
20960 && bytes > 24 /* move up to 32 bytes at a time */
20961 && ! fixed_regs[5]
20962 && ! fixed_regs[6]
20963 && ! fixed_regs[7]
20964 && ! fixed_regs[8]
20965 && ! fixed_regs[9]
20966 && ! fixed_regs[10]
20967 && ! fixed_regs[11]
20968 && ! fixed_regs[12])
20970 move_bytes = (bytes > 32) ? 32 : bytes;
20971 gen_func.movmemsi = gen_movmemsi_8reg;
20973 else if (TARGET_STRING
20974 && bytes > 16 /* move up to 24 bytes at a time */
20975 && ! fixed_regs[5]
20976 && ! fixed_regs[6]
20977 && ! fixed_regs[7]
20978 && ! fixed_regs[8]
20979 && ! fixed_regs[9]
20980 && ! fixed_regs[10])
20982 move_bytes = (bytes > 24) ? 24 : bytes;
20983 gen_func.movmemsi = gen_movmemsi_6reg;
20985 else if (TARGET_STRING
20986 && bytes > 8 /* move up to 16 bytes at a time */
20987 && ! fixed_regs[5]
20988 && ! fixed_regs[6]
20989 && ! fixed_regs[7]
20990 && ! fixed_regs[8])
20992 move_bytes = (bytes > 16) ? 16 : bytes;
20993 gen_func.movmemsi = gen_movmemsi_4reg;
20995 else if (bytes >= 8 && TARGET_POWERPC64
20996 && (align >= 64 || !STRICT_ALIGNMENT))
20998 move_bytes = 8;
20999 mode = DImode;
21000 gen_func.mov = gen_movdi;
21001 if (offset == 0 && align < 64)
21003 rtx addr;
21005 /* If the address form is reg+offset with offset not a
21006 multiple of four, reload into reg indirect form here
21007 rather than waiting for reload. This way we get one
21008 reload, not one per load and/or store. */
21009 addr = XEXP (orig_dest, 0);
21010 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
21011 && GET_CODE (XEXP (addr, 1)) == CONST_INT
21012 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
21014 addr = copy_addr_to_reg (addr);
21015 orig_dest = replace_equiv_address (orig_dest, addr);
21017 addr = XEXP (orig_src, 0);
21018 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
21019 && GET_CODE (XEXP (addr, 1)) == CONST_INT
21020 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
21022 addr = copy_addr_to_reg (addr);
21023 orig_src = replace_equiv_address (orig_src, addr);
21027 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
21028 { /* move up to 8 bytes at a time */
21029 move_bytes = (bytes > 8) ? 8 : bytes;
21030 gen_func.movmemsi = gen_movmemsi_2reg;
21032 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
21033 { /* move 4 bytes */
21034 move_bytes = 4;
21035 mode = SImode;
21036 gen_func.mov = gen_movsi;
21038 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
21039 { /* move 2 bytes */
21040 move_bytes = 2;
21041 mode = HImode;
21042 gen_func.mov = gen_movhi;
21044 else if (TARGET_STRING && bytes > 1)
21045 { /* move up to 4 bytes at a time */
21046 move_bytes = (bytes > 4) ? 4 : bytes;
21047 gen_func.movmemsi = gen_movmemsi_1reg;
21049 else /* move 1 byte at a time */
21051 move_bytes = 1;
21052 mode = QImode;
21053 gen_func.mov = gen_movqi;
21056 src = adjust_address (orig_src, mode, offset);
21057 dest = adjust_address (orig_dest, mode, offset);
21059 if (mode != BLKmode)
21061 rtx tmp_reg = gen_reg_rtx (mode);
21063 emit_insn ((*gen_func.mov) (tmp_reg, src));
21064 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
21067 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
21069 int i;
21070 for (i = 0; i < num_reg; i++)
21071 emit_insn (stores[i]);
21072 num_reg = 0;
21075 if (mode == BLKmode)
21077 /* Move the address into scratch registers. The movmemsi
21078 patterns require zero offset. */
21079 if (!REG_P (XEXP (src, 0)))
21081 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
21082 src = replace_equiv_address (src, src_reg);
21084 set_mem_size (src, move_bytes);
21086 if (!REG_P (XEXP (dest, 0)))
21088 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
21089 dest = replace_equiv_address (dest, dest_reg);
21091 set_mem_size (dest, move_bytes);
21093 emit_insn ((*gen_func.movmemsi) (dest, src,
21094 GEN_INT (move_bytes & 31),
21095 align_rtx));
21099 return 1;
21103 /* Return a string to perform a load_multiple operation.
21104 operands[0] is the vector.
21105 operands[1] is the source address.
21106 operands[2] is the first destination register. */
21108 const char *
21109 rs6000_output_load_multiple (rtx operands[3])
21111 /* We have to handle the case where the pseudo used to contain the address
21112 is assigned to one of the output registers. */
21113 int i, j;
21114 int words = XVECLEN (operands[0], 0);
21115 rtx xop[10];
21117 if (XVECLEN (operands[0], 0) == 1)
21118 return "lwz %2,0(%1)";
21120 for (i = 0; i < words; i++)
21121 if (refers_to_regno_p (REGNO (operands[2]) + i, operands[1]))
21123 if (i == words-1)
21125 xop[0] = GEN_INT (4 * (words-1));
21126 xop[1] = operands[1];
21127 xop[2] = operands[2];
21128 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
21129 return "";
21131 else if (i == 0)
21133 xop[0] = GEN_INT (4 * (words-1));
21134 xop[1] = operands[1];
21135 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
21136 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
21137 return "";
21139 else
21141 for (j = 0; j < words; j++)
21142 if (j != i)
21144 xop[0] = GEN_INT (j * 4);
21145 xop[1] = operands[1];
21146 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
21147 output_asm_insn ("lwz %2,%0(%1)", xop);
21149 xop[0] = GEN_INT (i * 4);
21150 xop[1] = operands[1];
21151 output_asm_insn ("lwz %1,%0(%1)", xop);
21152 return "";
21156 return "lswi %2,%1,%N0";
21160 /* A validation routine: say whether CODE, a condition code, and MODE
21161 match. The other alternatives either don't make sense or should
21162 never be generated. */
21164 void
21165 validate_condition_mode (enum rtx_code code, machine_mode mode)
21167 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
21168 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
21169 && GET_MODE_CLASS (mode) == MODE_CC);
21171 /* These don't make sense. */
21172 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
21173 || mode != CCUNSmode);
21175 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
21176 || mode == CCUNSmode);
21178 gcc_assert (mode == CCFPmode
21179 || (code != ORDERED && code != UNORDERED
21180 && code != UNEQ && code != LTGT
21181 && code != UNGT && code != UNLT
21182 && code != UNGE && code != UNLE));
21184 /* These should never be generated except for
21185 flag_finite_math_only. */
21186 gcc_assert (mode != CCFPmode
21187 || flag_finite_math_only
21188 || (code != LE && code != GE
21189 && code != UNEQ && code != LTGT
21190 && code != UNGT && code != UNLT));
21192 /* These are invalid; the information is not there. */
21193 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
21197 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm,
21198 rldicl, rldicr, or rldic instruction in mode MODE. If so, if E is
21199 not zero, store there the bit offset (counted from the right) where
21200 the single stretch of 1 bits begins; and similarly for B, the bit
21201 offset where it ends. */
21203 bool
21204 rs6000_is_valid_mask (rtx mask, int *b, int *e, machine_mode mode)
21206 unsigned HOST_WIDE_INT val = INTVAL (mask);
21207 unsigned HOST_WIDE_INT bit;
21208 int nb, ne;
21209 int n = GET_MODE_PRECISION (mode);
21211 if (mode != DImode && mode != SImode)
21212 return false;
21214 if (INTVAL (mask) >= 0)
21216 bit = val & -val;
21217 ne = exact_log2 (bit);
21218 nb = exact_log2 (val + bit);
21220 else if (val + 1 == 0)
21222 nb = n;
21223 ne = 0;
21225 else if (val & 1)
21227 val = ~val;
21228 bit = val & -val;
21229 nb = exact_log2 (bit);
21230 ne = exact_log2 (val + bit);
21232 else
21234 bit = val & -val;
21235 ne = exact_log2 (bit);
21236 if (val + bit == 0)
21237 nb = n;
21238 else
21239 nb = 0;
21242 nb--;
21244 if (nb < 0 || ne < 0 || nb >= n || ne >= n)
21245 return false;
21247 if (b)
21248 *b = nb;
21249 if (e)
21250 *e = ne;
21252 return true;
21255 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm, rldicl,
21256 or rldicr instruction, to implement an AND with it in mode MODE. */
21258 bool
21259 rs6000_is_valid_and_mask (rtx mask, machine_mode mode)
21261 int nb, ne;
21263 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
21264 return false;
21266 /* For DImode, we need a rldicl, rldicr, or a rlwinm with mask that
21267 does not wrap. */
21268 if (mode == DImode)
21269 return (ne == 0 || nb == 63 || (nb < 32 && ne <= nb));
21271 /* For SImode, rlwinm can do everything. */
21272 if (mode == SImode)
21273 return (nb < 32 && ne < 32);
21275 return false;
21278 /* Return the instruction template for an AND with mask in mode MODE, with
21279 operands OPERANDS. If DOT is true, make it a record-form instruction. */
21281 const char *
21282 rs6000_insn_for_and_mask (machine_mode mode, rtx *operands, bool dot)
21284 int nb, ne;
21286 if (!rs6000_is_valid_mask (operands[2], &nb, &ne, mode))
21287 gcc_unreachable ();
21289 if (mode == DImode && ne == 0)
21291 operands[3] = GEN_INT (63 - nb);
21292 if (dot)
21293 return "rldicl. %0,%1,0,%3";
21294 return "rldicl %0,%1,0,%3";
21297 if (mode == DImode && nb == 63)
21299 operands[3] = GEN_INT (63 - ne);
21300 if (dot)
21301 return "rldicr. %0,%1,0,%3";
21302 return "rldicr %0,%1,0,%3";
21305 if (nb < 32 && ne < 32)
21307 operands[3] = GEN_INT (31 - nb);
21308 operands[4] = GEN_INT (31 - ne);
21309 if (dot)
21310 return "rlwinm. %0,%1,0,%3,%4";
21311 return "rlwinm %0,%1,0,%3,%4";
21314 gcc_unreachable ();
21317 /* Return whether MASK (a CONST_INT) is a valid mask for any rlw[i]nm,
21318 rld[i]cl, rld[i]cr, or rld[i]c instruction, to implement an AND with
21319 shift SHIFT (a ROTATE, ASHIFT, or LSHIFTRT) in mode MODE. */
21321 bool
21322 rs6000_is_valid_shift_mask (rtx mask, rtx shift, machine_mode mode)
21324 int nb, ne;
21326 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
21327 return false;
21329 int n = GET_MODE_PRECISION (mode);
21330 int sh = -1;
21332 if (CONST_INT_P (XEXP (shift, 1)))
21334 sh = INTVAL (XEXP (shift, 1));
21335 if (sh < 0 || sh >= n)
21336 return false;
21339 rtx_code code = GET_CODE (shift);
21341 /* Convert any shift by 0 to a rotate, to simplify below code. */
21342 if (sh == 0)
21343 code = ROTATE;
21345 /* Convert rotate to simple shift if we can, to make analysis simpler. */
21346 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
21347 code = ASHIFT;
21348 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
21350 code = LSHIFTRT;
21351 sh = n - sh;
21354 /* DImode rotates need rld*. */
21355 if (mode == DImode && code == ROTATE)
21356 return (nb == 63 || ne == 0 || ne == sh);
21358 /* SImode rotates need rlw*. */
21359 if (mode == SImode && code == ROTATE)
21360 return (nb < 32 && ne < 32 && sh < 32);
21362 /* Wrap-around masks are only okay for rotates. */
21363 if (ne > nb)
21364 return false;
21366 /* Variable shifts are only okay for rotates. */
21367 if (sh < 0)
21368 return false;
21370 /* Don't allow ASHIFT if the mask is wrong for that. */
21371 if (code == ASHIFT && ne < sh)
21372 return false;
21374 /* If we can do it with an rlw*, we can do it. Don't allow LSHIFTRT
21375 if the mask is wrong for that. */
21376 if (nb < 32 && ne < 32 && sh < 32
21377 && !(code == LSHIFTRT && nb >= 32 - sh))
21378 return true;
21380 /* If we can do it with an rld*, we can do it. Don't allow LSHIFTRT
21381 if the mask is wrong for that. */
21382 if (code == LSHIFTRT)
21383 sh = 64 - sh;
21384 if (nb == 63 || ne == 0 || ne == sh)
21385 return !(code == LSHIFTRT && nb >= sh);
21387 return false;
21390 /* Return the instruction template for a shift with mask in mode MODE, with
21391 operands OPERANDS. If DOT is true, make it a record-form instruction. */
21393 const char *
21394 rs6000_insn_for_shift_mask (machine_mode mode, rtx *operands, bool dot)
21396 int nb, ne;
21398 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
21399 gcc_unreachable ();
21401 if (mode == DImode && ne == 0)
21403 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
21404 operands[2] = GEN_INT (64 - INTVAL (operands[2]));
21405 operands[3] = GEN_INT (63 - nb);
21406 if (dot)
21407 return "rld%I2cl. %0,%1,%2,%3";
21408 return "rld%I2cl %0,%1,%2,%3";
21411 if (mode == DImode && nb == 63)
21413 operands[3] = GEN_INT (63 - ne);
21414 if (dot)
21415 return "rld%I2cr. %0,%1,%2,%3";
21416 return "rld%I2cr %0,%1,%2,%3";
21419 if (mode == DImode
21420 && GET_CODE (operands[4]) != LSHIFTRT
21421 && CONST_INT_P (operands[2])
21422 && ne == INTVAL (operands[2]))
21424 operands[3] = GEN_INT (63 - nb);
21425 if (dot)
21426 return "rld%I2c. %0,%1,%2,%3";
21427 return "rld%I2c %0,%1,%2,%3";
21430 if (nb < 32 && ne < 32)
21432 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
21433 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
21434 operands[3] = GEN_INT (31 - nb);
21435 operands[4] = GEN_INT (31 - ne);
21436 /* This insn can also be a 64-bit rotate with mask that really makes
21437 it just a shift right (with mask); the %h below are to adjust for
21438 that situation (shift count is >= 32 in that case). */
21439 if (dot)
21440 return "rlw%I2nm. %0,%1,%h2,%3,%4";
21441 return "rlw%I2nm %0,%1,%h2,%3,%4";
21444 gcc_unreachable ();
21447 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwimi or
21448 rldimi instruction, to implement an insert with shift SHIFT (a ROTATE,
21449 ASHIFT, or LSHIFTRT) in mode MODE. */
21451 bool
21452 rs6000_is_valid_insert_mask (rtx mask, rtx shift, machine_mode mode)
21454 int nb, ne;
21456 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
21457 return false;
21459 int n = GET_MODE_PRECISION (mode);
21461 int sh = INTVAL (XEXP (shift, 1));
21462 if (sh < 0 || sh >= n)
21463 return false;
21465 rtx_code code = GET_CODE (shift);
21467 /* Convert any shift by 0 to a rotate, to simplify below code. */
21468 if (sh == 0)
21469 code = ROTATE;
21471 /* Convert rotate to simple shift if we can, to make analysis simpler. */
21472 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
21473 code = ASHIFT;
21474 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
21476 code = LSHIFTRT;
21477 sh = n - sh;
21480 /* DImode rotates need rldimi. */
21481 if (mode == DImode && code == ROTATE)
21482 return (ne == sh);
21484 /* SImode rotates need rlwimi. */
21485 if (mode == SImode && code == ROTATE)
21486 return (nb < 32 && ne < 32 && sh < 32);
21488 /* Wrap-around masks are only okay for rotates. */
21489 if (ne > nb)
21490 return false;
21492 /* Don't allow ASHIFT if the mask is wrong for that. */
21493 if (code == ASHIFT && ne < sh)
21494 return false;
21496 /* If we can do it with an rlwimi, we can do it. Don't allow LSHIFTRT
21497 if the mask is wrong for that. */
21498 if (nb < 32 && ne < 32 && sh < 32
21499 && !(code == LSHIFTRT && nb >= 32 - sh))
21500 return true;
21502 /* If we can do it with an rldimi, we can do it. Don't allow LSHIFTRT
21503 if the mask is wrong for that. */
21504 if (code == LSHIFTRT)
21505 sh = 64 - sh;
21506 if (ne == sh)
21507 return !(code == LSHIFTRT && nb >= sh);
21509 return false;
21512 /* Return the instruction template for an insert with mask in mode MODE, with
21513 operands OPERANDS. If DOT is true, make it a record-form instruction. */
21515 const char *
21516 rs6000_insn_for_insert_mask (machine_mode mode, rtx *operands, bool dot)
21518 int nb, ne;
21520 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
21521 gcc_unreachable ();
21523 /* Prefer rldimi because rlwimi is cracked. */
21524 if (TARGET_POWERPC64
21525 && (!dot || mode == DImode)
21526 && GET_CODE (operands[4]) != LSHIFTRT
21527 && ne == INTVAL (operands[2]))
21529 operands[3] = GEN_INT (63 - nb);
21530 if (dot)
21531 return "rldimi. %0,%1,%2,%3";
21532 return "rldimi %0,%1,%2,%3";
21535 if (nb < 32 && ne < 32)
21537 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
21538 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
21539 operands[3] = GEN_INT (31 - nb);
21540 operands[4] = GEN_INT (31 - ne);
21541 if (dot)
21542 return "rlwimi. %0,%1,%2,%3,%4";
21543 return "rlwimi %0,%1,%2,%3,%4";
21546 gcc_unreachable ();
21549 /* Return whether an AND with C (a CONST_INT) in mode MODE can be done
21550 using two machine instructions. */
21552 bool
21553 rs6000_is_valid_2insn_and (rtx c, machine_mode mode)
21555 /* There are two kinds of AND we can handle with two insns:
21556 1) those we can do with two rl* insn;
21557 2) ori[s];xori[s].
21559 We do not handle that last case yet. */
21561 /* If there is just one stretch of ones, we can do it. */
21562 if (rs6000_is_valid_mask (c, NULL, NULL, mode))
21563 return true;
21565 /* Otherwise, fill in the lowest "hole"; if we can do the result with
21566 one insn, we can do the whole thing with two. */
21567 unsigned HOST_WIDE_INT val = INTVAL (c);
21568 unsigned HOST_WIDE_INT bit1 = val & -val;
21569 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
21570 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
21571 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
21572 return rs6000_is_valid_and_mask (GEN_INT (val + bit3 - bit2), mode);
21575 /* Emit the two insns to do an AND in mode MODE, with operands OPERANDS.
21576 If EXPAND is true, split rotate-and-mask instructions we generate to
21577 their constituent parts as well (this is used during expand); if DOT
21578 is 1, make the last insn a record-form instruction clobbering the
21579 destination GPR and setting the CC reg (from operands[3]); if 2, set
21580 that GPR as well as the CC reg. */
21582 void
21583 rs6000_emit_2insn_and (machine_mode mode, rtx *operands, bool expand, int dot)
21585 gcc_assert (!(expand && dot));
21587 unsigned HOST_WIDE_INT val = INTVAL (operands[2]);
21589 /* If it is one stretch of ones, it is DImode; shift left, mask, then
21590 shift right. This generates better code than doing the masks without
21591 shifts, or shifting first right and then left. */
21592 int nb, ne;
21593 if (rs6000_is_valid_mask (operands[2], &nb, &ne, mode) && nb >= ne)
21595 gcc_assert (mode == DImode);
21597 int shift = 63 - nb;
21598 if (expand)
21600 rtx tmp1 = gen_reg_rtx (DImode);
21601 rtx tmp2 = gen_reg_rtx (DImode);
21602 emit_insn (gen_ashldi3 (tmp1, operands[1], GEN_INT (shift)));
21603 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (val << shift)));
21604 emit_insn (gen_lshrdi3 (operands[0], tmp2, GEN_INT (shift)));
21606 else
21608 rtx tmp = gen_rtx_ASHIFT (mode, operands[1], GEN_INT (shift));
21609 tmp = gen_rtx_AND (mode, tmp, GEN_INT (val << shift));
21610 emit_move_insn (operands[0], tmp);
21611 tmp = gen_rtx_LSHIFTRT (mode, operands[0], GEN_INT (shift));
21612 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21614 return;
21617 /* Otherwise, make a mask2 that cuts out the lowest "hole", and a mask1
21618 that does the rest. */
21619 unsigned HOST_WIDE_INT bit1 = val & -val;
21620 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
21621 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
21622 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
21624 unsigned HOST_WIDE_INT mask1 = -bit3 + bit2 - 1;
21625 unsigned HOST_WIDE_INT mask2 = val + bit3 - bit2;
21627 gcc_assert (rs6000_is_valid_and_mask (GEN_INT (mask2), mode));
21629 /* Two "no-rotate"-and-mask instructions, for SImode. */
21630 if (rs6000_is_valid_and_mask (GEN_INT (mask1), mode))
21632 gcc_assert (mode == SImode);
21634 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
21635 rtx tmp = gen_rtx_AND (mode, operands[1], GEN_INT (mask1));
21636 emit_move_insn (reg, tmp);
21637 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
21638 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21639 return;
21642 gcc_assert (mode == DImode);
21644 /* Two "no-rotate"-and-mask instructions, for DImode: both are rlwinm
21645 insns; we have to do the first in SImode, because it wraps. */
21646 if (mask2 <= 0xffffffff
21647 && rs6000_is_valid_and_mask (GEN_INT (mask1), SImode))
21649 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
21650 rtx tmp = gen_rtx_AND (SImode, gen_lowpart (SImode, operands[1]),
21651 GEN_INT (mask1));
21652 rtx reg_low = gen_lowpart (SImode, reg);
21653 emit_move_insn (reg_low, tmp);
21654 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
21655 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21656 return;
21659 /* Two rld* insns: rotate, clear the hole in the middle (which now is
21660 at the top end), rotate back and clear the other hole. */
21661 int right = exact_log2 (bit3);
21662 int left = 64 - right;
21664 /* Rotate the mask too. */
21665 mask1 = (mask1 >> right) | ((bit2 - 1) << left);
21667 if (expand)
21669 rtx tmp1 = gen_reg_rtx (DImode);
21670 rtx tmp2 = gen_reg_rtx (DImode);
21671 rtx tmp3 = gen_reg_rtx (DImode);
21672 emit_insn (gen_rotldi3 (tmp1, operands[1], GEN_INT (left)));
21673 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (mask1)));
21674 emit_insn (gen_rotldi3 (tmp3, tmp2, GEN_INT (right)));
21675 emit_insn (gen_anddi3 (operands[0], tmp3, GEN_INT (mask2)));
21677 else
21679 rtx tmp = gen_rtx_ROTATE (mode, operands[1], GEN_INT (left));
21680 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask1));
21681 emit_move_insn (operands[0], tmp);
21682 tmp = gen_rtx_ROTATE (mode, operands[0], GEN_INT (right));
21683 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask2));
21684 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21688 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
21689 for lfq and stfq insns iff the registers are hard registers. */
21692 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
21694 /* We might have been passed a SUBREG. */
21695 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
21696 return 0;
21698 /* We might have been passed non floating point registers. */
21699 if (!FP_REGNO_P (REGNO (reg1))
21700 || !FP_REGNO_P (REGNO (reg2)))
21701 return 0;
21703 return (REGNO (reg1) == REGNO (reg2) - 1);
21706 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
21707 addr1 and addr2 must be in consecutive memory locations
21708 (addr2 == addr1 + 8). */
21711 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
21713 rtx addr1, addr2;
21714 unsigned int reg1, reg2;
21715 int offset1, offset2;
21717 /* The mems cannot be volatile. */
21718 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
21719 return 0;
21721 addr1 = XEXP (mem1, 0);
21722 addr2 = XEXP (mem2, 0);
21724 /* Extract an offset (if used) from the first addr. */
21725 if (GET_CODE (addr1) == PLUS)
21727 /* If not a REG, return zero. */
21728 if (GET_CODE (XEXP (addr1, 0)) != REG)
21729 return 0;
21730 else
21732 reg1 = REGNO (XEXP (addr1, 0));
21733 /* The offset must be constant! */
21734 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
21735 return 0;
21736 offset1 = INTVAL (XEXP (addr1, 1));
21739 else if (GET_CODE (addr1) != REG)
21740 return 0;
21741 else
21743 reg1 = REGNO (addr1);
21744 /* This was a simple (mem (reg)) expression. Offset is 0. */
21745 offset1 = 0;
21748 /* And now for the second addr. */
21749 if (GET_CODE (addr2) == PLUS)
21751 /* If not a REG, return zero. */
21752 if (GET_CODE (XEXP (addr2, 0)) != REG)
21753 return 0;
21754 else
21756 reg2 = REGNO (XEXP (addr2, 0));
21757 /* The offset must be constant. */
21758 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
21759 return 0;
21760 offset2 = INTVAL (XEXP (addr2, 1));
21763 else if (GET_CODE (addr2) != REG)
21764 return 0;
21765 else
21767 reg2 = REGNO (addr2);
21768 /* This was a simple (mem (reg)) expression. Offset is 0. */
21769 offset2 = 0;
21772 /* Both of these must have the same base register. */
21773 if (reg1 != reg2)
21774 return 0;
21776 /* The offset for the second addr must be 8 more than the first addr. */
21777 if (offset2 != offset1 + 8)
21778 return 0;
21780 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
21781 instructions. */
21782 return 1;
21787 rs6000_secondary_memory_needed_rtx (machine_mode mode)
21789 static bool eliminated = false;
21790 rtx ret;
21792 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
21793 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
21794 else
21796 rtx mem = cfun->machine->sdmode_stack_slot;
21797 gcc_assert (mem != NULL_RTX);
21799 if (!eliminated)
21801 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
21802 cfun->machine->sdmode_stack_slot = mem;
21803 eliminated = true;
21805 ret = mem;
21808 if (TARGET_DEBUG_ADDR)
21810 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
21811 GET_MODE_NAME (mode));
21812 if (!ret)
21813 fprintf (stderr, "\tNULL_RTX\n");
21814 else
21815 debug_rtx (ret);
21818 return ret;
21821 /* Implement TARGET_SECONDARY_MEMORY_NEEDED_MODE. For SDmode values we
21822 need to use DDmode, in all other cases we can use the same mode. */
21823 static machine_mode
21824 rs6000_secondary_memory_needed_mode (machine_mode mode)
21826 if (lra_in_progress && mode == SDmode)
21827 return DDmode;
21828 return mode;
21831 static tree
21832 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21834 /* Don't walk into types. */
21835 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
21837 *walk_subtrees = 0;
21838 return NULL_TREE;
21841 switch (TREE_CODE (*tp))
21843 case VAR_DECL:
21844 case PARM_DECL:
21845 case FIELD_DECL:
21846 case RESULT_DECL:
21847 case SSA_NAME:
21848 case REAL_CST:
21849 case MEM_REF:
21850 case VIEW_CONVERT_EXPR:
21851 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
21852 return *tp;
21853 break;
21854 default:
21855 break;
21858 return NULL_TREE;
21861 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
21862 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
21863 only work on the traditional altivec registers, note if an altivec register
21864 was chosen. */
21866 static enum rs6000_reg_type
21867 register_to_reg_type (rtx reg, bool *is_altivec)
21869 HOST_WIDE_INT regno;
21870 enum reg_class rclass;
21872 if (GET_CODE (reg) == SUBREG)
21873 reg = SUBREG_REG (reg);
21875 if (!REG_P (reg))
21876 return NO_REG_TYPE;
21878 regno = REGNO (reg);
21879 if (regno >= FIRST_PSEUDO_REGISTER)
21881 if (!lra_in_progress && !reload_in_progress && !reload_completed)
21882 return PSEUDO_REG_TYPE;
21884 regno = true_regnum (reg);
21885 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
21886 return PSEUDO_REG_TYPE;
21889 gcc_assert (regno >= 0);
21891 if (is_altivec && ALTIVEC_REGNO_P (regno))
21892 *is_altivec = true;
21894 rclass = rs6000_regno_regclass[regno];
21895 return reg_class_to_reg_type[(int)rclass];
21898 /* Helper function to return the cost of adding a TOC entry address. */
21900 static inline int
21901 rs6000_secondary_reload_toc_costs (addr_mask_type addr_mask)
21903 int ret;
21905 if (TARGET_CMODEL != CMODEL_SMALL)
21906 ret = ((addr_mask & RELOAD_REG_OFFSET) == 0) ? 1 : 2;
21908 else
21909 ret = (TARGET_MINIMAL_TOC) ? 6 : 3;
21911 return ret;
21914 /* Helper function for rs6000_secondary_reload to determine whether the memory
21915 address (ADDR) with a given register class (RCLASS) and machine mode (MODE)
21916 needs reloading. Return negative if the memory is not handled by the memory
21917 helper functions and to try a different reload method, 0 if no additional
21918 instructions are need, and positive to give the extra cost for the
21919 memory. */
21921 static int
21922 rs6000_secondary_reload_memory (rtx addr,
21923 enum reg_class rclass,
21924 machine_mode mode)
21926 int extra_cost = 0;
21927 rtx reg, and_arg, plus_arg0, plus_arg1;
21928 addr_mask_type addr_mask;
21929 const char *type = NULL;
21930 const char *fail_msg = NULL;
21932 if (GPR_REG_CLASS_P (rclass))
21933 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
21935 else if (rclass == FLOAT_REGS)
21936 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
21938 else if (rclass == ALTIVEC_REGS)
21939 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
21941 /* For the combined VSX_REGS, turn off Altivec AND -16. */
21942 else if (rclass == VSX_REGS)
21943 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_VMX]
21944 & ~RELOAD_REG_AND_M16);
21946 /* If the register allocator hasn't made up its mind yet on the register
21947 class to use, settle on defaults to use. */
21948 else if (rclass == NO_REGS)
21950 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_ANY]
21951 & ~RELOAD_REG_AND_M16);
21953 if ((addr_mask & RELOAD_REG_MULTIPLE) != 0)
21954 addr_mask &= ~(RELOAD_REG_INDEXED
21955 | RELOAD_REG_PRE_INCDEC
21956 | RELOAD_REG_PRE_MODIFY);
21959 else
21960 addr_mask = 0;
21962 /* If the register isn't valid in this register class, just return now. */
21963 if ((addr_mask & RELOAD_REG_VALID) == 0)
21965 if (TARGET_DEBUG_ADDR)
21967 fprintf (stderr,
21968 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
21969 "not valid in class\n",
21970 GET_MODE_NAME (mode), reg_class_names[rclass]);
21971 debug_rtx (addr);
21974 return -1;
21977 switch (GET_CODE (addr))
21979 /* Does the register class supports auto update forms for this mode? We
21980 don't need a scratch register, since the powerpc only supports
21981 PRE_INC, PRE_DEC, and PRE_MODIFY. */
21982 case PRE_INC:
21983 case PRE_DEC:
21984 reg = XEXP (addr, 0);
21985 if (!base_reg_operand (addr, GET_MODE (reg)))
21987 fail_msg = "no base register #1";
21988 extra_cost = -1;
21991 else if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
21993 extra_cost = 1;
21994 type = "update";
21996 break;
21998 case PRE_MODIFY:
21999 reg = XEXP (addr, 0);
22000 plus_arg1 = XEXP (addr, 1);
22001 if (!base_reg_operand (reg, GET_MODE (reg))
22002 || GET_CODE (plus_arg1) != PLUS
22003 || !rtx_equal_p (reg, XEXP (plus_arg1, 0)))
22005 fail_msg = "bad PRE_MODIFY";
22006 extra_cost = -1;
22009 else if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
22011 extra_cost = 1;
22012 type = "update";
22014 break;
22016 /* Do we need to simulate AND -16 to clear the bottom address bits used
22017 in VMX load/stores? Only allow the AND for vector sizes. */
22018 case AND:
22019 and_arg = XEXP (addr, 0);
22020 if (GET_MODE_SIZE (mode) != 16
22021 || GET_CODE (XEXP (addr, 1)) != CONST_INT
22022 || INTVAL (XEXP (addr, 1)) != -16)
22024 fail_msg = "bad Altivec AND #1";
22025 extra_cost = -1;
22028 if (rclass != ALTIVEC_REGS)
22030 if (legitimate_indirect_address_p (and_arg, false))
22031 extra_cost = 1;
22033 else if (legitimate_indexed_address_p (and_arg, false))
22034 extra_cost = 2;
22036 else
22038 fail_msg = "bad Altivec AND #2";
22039 extra_cost = -1;
22042 type = "and";
22044 break;
22046 /* If this is an indirect address, make sure it is a base register. */
22047 case REG:
22048 case SUBREG:
22049 if (!legitimate_indirect_address_p (addr, false))
22051 extra_cost = 1;
22052 type = "move";
22054 break;
22056 /* If this is an indexed address, make sure the register class can handle
22057 indexed addresses for this mode. */
22058 case PLUS:
22059 plus_arg0 = XEXP (addr, 0);
22060 plus_arg1 = XEXP (addr, 1);
22062 /* (plus (plus (reg) (constant)) (constant)) is generated during
22063 push_reload processing, so handle it now. */
22064 if (GET_CODE (plus_arg0) == PLUS && CONST_INT_P (plus_arg1))
22066 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22068 extra_cost = 1;
22069 type = "offset";
22073 /* (plus (plus (reg) (constant)) (reg)) is also generated during
22074 push_reload processing, so handle it now. */
22075 else if (GET_CODE (plus_arg0) == PLUS && REG_P (plus_arg1))
22077 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
22079 extra_cost = 1;
22080 type = "indexed #2";
22084 else if (!base_reg_operand (plus_arg0, GET_MODE (plus_arg0)))
22086 fail_msg = "no base register #2";
22087 extra_cost = -1;
22090 else if (int_reg_operand (plus_arg1, GET_MODE (plus_arg1)))
22092 if ((addr_mask & RELOAD_REG_INDEXED) == 0
22093 || !legitimate_indexed_address_p (addr, false))
22095 extra_cost = 1;
22096 type = "indexed";
22100 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0
22101 && CONST_INT_P (plus_arg1))
22103 if (!quad_address_offset_p (INTVAL (plus_arg1)))
22105 extra_cost = 1;
22106 type = "vector d-form offset";
22110 /* Make sure the register class can handle offset addresses. */
22111 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
22113 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22115 extra_cost = 1;
22116 type = "offset #2";
22120 else
22122 fail_msg = "bad PLUS";
22123 extra_cost = -1;
22126 break;
22128 case LO_SUM:
22129 /* Quad offsets are restricted and can't handle normal addresses. */
22130 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
22132 extra_cost = -1;
22133 type = "vector d-form lo_sum";
22136 else if (!legitimate_lo_sum_address_p (mode, addr, false))
22138 fail_msg = "bad LO_SUM";
22139 extra_cost = -1;
22142 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22144 extra_cost = 1;
22145 type = "lo_sum";
22147 break;
22149 /* Static addresses need to create a TOC entry. */
22150 case CONST:
22151 case SYMBOL_REF:
22152 case LABEL_REF:
22153 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
22155 extra_cost = -1;
22156 type = "vector d-form lo_sum #2";
22159 else
22161 type = "address";
22162 extra_cost = rs6000_secondary_reload_toc_costs (addr_mask);
22164 break;
22166 /* TOC references look like offsetable memory. */
22167 case UNSPEC:
22168 if (TARGET_CMODEL == CMODEL_SMALL || XINT (addr, 1) != UNSPEC_TOCREL)
22170 fail_msg = "bad UNSPEC";
22171 extra_cost = -1;
22174 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
22176 extra_cost = -1;
22177 type = "vector d-form lo_sum #3";
22180 else if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22182 extra_cost = 1;
22183 type = "toc reference";
22185 break;
22187 default:
22189 fail_msg = "bad address";
22190 extra_cost = -1;
22194 if (TARGET_DEBUG_ADDR /* && extra_cost != 0 */)
22196 if (extra_cost < 0)
22197 fprintf (stderr,
22198 "rs6000_secondary_reload_memory error: mode = %s, "
22199 "class = %s, addr_mask = '%s', %s\n",
22200 GET_MODE_NAME (mode),
22201 reg_class_names[rclass],
22202 rs6000_debug_addr_mask (addr_mask, false),
22203 (fail_msg != NULL) ? fail_msg : "<bad address>");
22205 else
22206 fprintf (stderr,
22207 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
22208 "addr_mask = '%s', extra cost = %d, %s\n",
22209 GET_MODE_NAME (mode),
22210 reg_class_names[rclass],
22211 rs6000_debug_addr_mask (addr_mask, false),
22212 extra_cost,
22213 (type) ? type : "<none>");
22215 debug_rtx (addr);
22218 return extra_cost;
22221 /* Helper function for rs6000_secondary_reload to return true if a move to a
22222 different register classe is really a simple move. */
22224 static bool
22225 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
22226 enum rs6000_reg_type from_type,
22227 machine_mode mode)
22229 int size = GET_MODE_SIZE (mode);
22231 /* Add support for various direct moves available. In this function, we only
22232 look at cases where we don't need any extra registers, and one or more
22233 simple move insns are issued. Originally small integers are not allowed
22234 in FPR/VSX registers. Single precision binary floating is not a simple
22235 move because we need to convert to the single precision memory layout.
22236 The 4-byte SDmode can be moved. TDmode values are disallowed since they
22237 need special direct move handling, which we do not support yet. */
22238 if (TARGET_DIRECT_MOVE
22239 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
22240 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
22242 if (TARGET_POWERPC64)
22244 /* ISA 2.07: MTVSRD or MVFVSRD. */
22245 if (size == 8)
22246 return true;
22248 /* ISA 3.0: MTVSRDD or MFVSRD + MFVSRLD. */
22249 if (size == 16 && TARGET_P9_VECTOR && mode != TDmode)
22250 return true;
22253 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
22254 if (TARGET_VSX_SMALL_INTEGER)
22256 if (mode == SImode)
22257 return true;
22259 if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
22260 return true;
22263 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
22264 if (mode == SDmode)
22265 return true;
22268 /* Power6+: MFTGPR or MFFGPR. */
22269 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
22270 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
22271 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
22272 return true;
22274 /* Move to/from SPR. */
22275 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
22276 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
22277 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
22278 return true;
22280 return false;
22283 /* Direct move helper function for rs6000_secondary_reload, handle all of the
22284 special direct moves that involve allocating an extra register, return the
22285 insn code of the helper function if there is such a function or
22286 CODE_FOR_nothing if not. */
22288 static bool
22289 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
22290 enum rs6000_reg_type from_type,
22291 machine_mode mode,
22292 secondary_reload_info *sri,
22293 bool altivec_p)
22295 bool ret = false;
22296 enum insn_code icode = CODE_FOR_nothing;
22297 int cost = 0;
22298 int size = GET_MODE_SIZE (mode);
22300 if (TARGET_POWERPC64 && size == 16)
22302 /* Handle moving 128-bit values from GPRs to VSX point registers on
22303 ISA 2.07 (power8, power9) when running in 64-bit mode using
22304 XXPERMDI to glue the two 64-bit values back together. */
22305 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
22307 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
22308 icode = reg_addr[mode].reload_vsx_gpr;
22311 /* Handle moving 128-bit values from VSX point registers to GPRs on
22312 ISA 2.07 when running in 64-bit mode using XXPERMDI to get access to the
22313 bottom 64-bit value. */
22314 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
22316 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
22317 icode = reg_addr[mode].reload_gpr_vsx;
22321 else if (TARGET_POWERPC64 && mode == SFmode)
22323 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
22325 cost = 3; /* xscvdpspn, mfvsrd, and. */
22326 icode = reg_addr[mode].reload_gpr_vsx;
22329 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
22331 cost = 2; /* mtvsrz, xscvspdpn. */
22332 icode = reg_addr[mode].reload_vsx_gpr;
22336 else if (!TARGET_POWERPC64 && size == 8)
22338 /* Handle moving 64-bit values from GPRs to floating point registers on
22339 ISA 2.07 when running in 32-bit mode using FMRGOW to glue the two
22340 32-bit values back together. Altivec register classes must be handled
22341 specially since a different instruction is used, and the secondary
22342 reload support requires a single instruction class in the scratch
22343 register constraint. However, right now TFmode is not allowed in
22344 Altivec registers, so the pattern will never match. */
22345 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
22347 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
22348 icode = reg_addr[mode].reload_fpr_gpr;
22352 if (icode != CODE_FOR_nothing)
22354 ret = true;
22355 if (sri)
22357 sri->icode = icode;
22358 sri->extra_cost = cost;
22362 return ret;
22365 /* Return whether a move between two register classes can be done either
22366 directly (simple move) or via a pattern that uses a single extra temporary
22367 (using ISA 2.07's direct move in this case. */
22369 static bool
22370 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
22371 enum rs6000_reg_type from_type,
22372 machine_mode mode,
22373 secondary_reload_info *sri,
22374 bool altivec_p)
22376 /* Fall back to load/store reloads if either type is not a register. */
22377 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
22378 return false;
22380 /* If we haven't allocated registers yet, assume the move can be done for the
22381 standard register types. */
22382 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
22383 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
22384 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
22385 return true;
22387 /* Moves to the same set of registers is a simple move for non-specialized
22388 registers. */
22389 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
22390 return true;
22392 /* Check whether a simple move can be done directly. */
22393 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
22395 if (sri)
22397 sri->icode = CODE_FOR_nothing;
22398 sri->extra_cost = 0;
22400 return true;
22403 /* Now check if we can do it in a few steps. */
22404 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
22405 altivec_p);
22408 /* Inform reload about cases where moving X with a mode MODE to a register in
22409 RCLASS requires an extra scratch or immediate register. Return the class
22410 needed for the immediate register.
22412 For VSX and Altivec, we may need a register to convert sp+offset into
22413 reg+sp.
22415 For misaligned 64-bit gpr loads and stores we need a register to
22416 convert an offset address to indirect. */
22418 static reg_class_t
22419 rs6000_secondary_reload (bool in_p,
22420 rtx x,
22421 reg_class_t rclass_i,
22422 machine_mode mode,
22423 secondary_reload_info *sri)
22425 enum reg_class rclass = (enum reg_class) rclass_i;
22426 reg_class_t ret = ALL_REGS;
22427 enum insn_code icode;
22428 bool default_p = false;
22429 bool done_p = false;
22431 /* Allow subreg of memory before/during reload. */
22432 bool memory_p = (MEM_P (x)
22433 || (!reload_completed && GET_CODE (x) == SUBREG
22434 && MEM_P (SUBREG_REG (x))));
22436 sri->icode = CODE_FOR_nothing;
22437 sri->t_icode = CODE_FOR_nothing;
22438 sri->extra_cost = 0;
22439 icode = ((in_p)
22440 ? reg_addr[mode].reload_load
22441 : reg_addr[mode].reload_store);
22443 if (REG_P (x) || register_operand (x, mode))
22445 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
22446 bool altivec_p = (rclass == ALTIVEC_REGS);
22447 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
22449 if (!in_p)
22450 std::swap (to_type, from_type);
22452 /* Can we do a direct move of some sort? */
22453 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
22454 altivec_p))
22456 icode = (enum insn_code)sri->icode;
22457 default_p = false;
22458 done_p = true;
22459 ret = NO_REGS;
22463 /* Make sure 0.0 is not reloaded or forced into memory. */
22464 if (x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
22466 ret = NO_REGS;
22467 default_p = false;
22468 done_p = true;
22471 /* If this is a scalar floating point value and we want to load it into the
22472 traditional Altivec registers, do it via a move via a traditional floating
22473 point register, unless we have D-form addressing. Also make sure that
22474 non-zero constants use a FPR. */
22475 if (!done_p && reg_addr[mode].scalar_in_vmx_p
22476 && !mode_supports_vmx_dform (mode)
22477 && (rclass == VSX_REGS || rclass == ALTIVEC_REGS)
22478 && (memory_p || (GET_CODE (x) == CONST_DOUBLE)))
22480 ret = FLOAT_REGS;
22481 default_p = false;
22482 done_p = true;
22485 /* Handle reload of load/stores if we have reload helper functions. */
22486 if (!done_p && icode != CODE_FOR_nothing && memory_p)
22488 int extra_cost = rs6000_secondary_reload_memory (XEXP (x, 0), rclass,
22489 mode);
22491 if (extra_cost >= 0)
22493 done_p = true;
22494 ret = NO_REGS;
22495 if (extra_cost > 0)
22497 sri->extra_cost = extra_cost;
22498 sri->icode = icode;
22503 /* Handle unaligned loads and stores of integer registers. */
22504 if (!done_p && TARGET_POWERPC64
22505 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
22506 && memory_p
22507 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
22509 rtx addr = XEXP (x, 0);
22510 rtx off = address_offset (addr);
22512 if (off != NULL_RTX)
22514 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
22515 unsigned HOST_WIDE_INT offset = INTVAL (off);
22517 /* We need a secondary reload when our legitimate_address_p
22518 says the address is good (as otherwise the entire address
22519 will be reloaded), and the offset is not a multiple of
22520 four or we have an address wrap. Address wrap will only
22521 occur for LO_SUMs since legitimate_offset_address_p
22522 rejects addresses for 16-byte mems that will wrap. */
22523 if (GET_CODE (addr) == LO_SUM
22524 ? (1 /* legitimate_address_p allows any offset for lo_sum */
22525 && ((offset & 3) != 0
22526 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
22527 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
22528 && (offset & 3) != 0))
22530 /* -m32 -mpowerpc64 needs to use a 32-bit scratch register. */
22531 if (in_p)
22532 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_load
22533 : CODE_FOR_reload_di_load);
22534 else
22535 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_store
22536 : CODE_FOR_reload_di_store);
22537 sri->extra_cost = 2;
22538 ret = NO_REGS;
22539 done_p = true;
22541 else
22542 default_p = true;
22544 else
22545 default_p = true;
22548 if (!done_p && !TARGET_POWERPC64
22549 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
22550 && memory_p
22551 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
22553 rtx addr = XEXP (x, 0);
22554 rtx off = address_offset (addr);
22556 if (off != NULL_RTX)
22558 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
22559 unsigned HOST_WIDE_INT offset = INTVAL (off);
22561 /* We need a secondary reload when our legitimate_address_p
22562 says the address is good (as otherwise the entire address
22563 will be reloaded), and we have a wrap.
22565 legitimate_lo_sum_address_p allows LO_SUM addresses to
22566 have any offset so test for wrap in the low 16 bits.
22568 legitimate_offset_address_p checks for the range
22569 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
22570 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
22571 [0x7ff4,0x7fff] respectively, so test for the
22572 intersection of these ranges, [0x7ffc,0x7fff] and
22573 [0x7ff4,0x7ff7] respectively.
22575 Note that the address we see here may have been
22576 manipulated by legitimize_reload_address. */
22577 if (GET_CODE (addr) == LO_SUM
22578 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
22579 : offset - (0x8000 - extra) < UNITS_PER_WORD)
22581 if (in_p)
22582 sri->icode = CODE_FOR_reload_si_load;
22583 else
22584 sri->icode = CODE_FOR_reload_si_store;
22585 sri->extra_cost = 2;
22586 ret = NO_REGS;
22587 done_p = true;
22589 else
22590 default_p = true;
22592 else
22593 default_p = true;
22596 if (!done_p)
22597 default_p = true;
22599 if (default_p)
22600 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
22602 gcc_assert (ret != ALL_REGS);
22604 if (TARGET_DEBUG_ADDR)
22606 fprintf (stderr,
22607 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
22608 "mode = %s",
22609 reg_class_names[ret],
22610 in_p ? "true" : "false",
22611 reg_class_names[rclass],
22612 GET_MODE_NAME (mode));
22614 if (reload_completed)
22615 fputs (", after reload", stderr);
22617 if (!done_p)
22618 fputs (", done_p not set", stderr);
22620 if (default_p)
22621 fputs (", default secondary reload", stderr);
22623 if (sri->icode != CODE_FOR_nothing)
22624 fprintf (stderr, ", reload func = %s, extra cost = %d",
22625 insn_data[sri->icode].name, sri->extra_cost);
22627 else if (sri->extra_cost > 0)
22628 fprintf (stderr, ", extra cost = %d", sri->extra_cost);
22630 fputs ("\n", stderr);
22631 debug_rtx (x);
22634 return ret;
22637 /* Better tracing for rs6000_secondary_reload_inner. */
22639 static void
22640 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
22641 bool store_p)
22643 rtx set, clobber;
22645 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
22647 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
22648 store_p ? "store" : "load");
22650 if (store_p)
22651 set = gen_rtx_SET (mem, reg);
22652 else
22653 set = gen_rtx_SET (reg, mem);
22655 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
22656 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
22659 static void rs6000_secondary_reload_fail (int, rtx, rtx, rtx, bool)
22660 ATTRIBUTE_NORETURN;
22662 static void
22663 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
22664 bool store_p)
22666 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
22667 gcc_unreachable ();
22670 /* Fixup reload addresses for values in GPR, FPR, and VMX registers that have
22671 reload helper functions. These were identified in
22672 rs6000_secondary_reload_memory, and if reload decided to use the secondary
22673 reload, it calls the insns:
22674 reload_<RELOAD:mode>_<P:mptrsize>_store
22675 reload_<RELOAD:mode>_<P:mptrsize>_load
22677 which in turn calls this function, to do whatever is necessary to create
22678 valid addresses. */
22680 void
22681 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
22683 int regno = true_regnum (reg);
22684 machine_mode mode = GET_MODE (reg);
22685 addr_mask_type addr_mask;
22686 rtx addr;
22687 rtx new_addr;
22688 rtx op_reg, op0, op1;
22689 rtx and_op;
22690 rtx cc_clobber;
22691 rtvec rv;
22693 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER || !MEM_P (mem)
22694 || !base_reg_operand (scratch, GET_MODE (scratch)))
22695 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22697 if (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO))
22698 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
22700 else if (IN_RANGE (regno, FIRST_FPR_REGNO, LAST_FPR_REGNO))
22701 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
22703 else if (IN_RANGE (regno, FIRST_ALTIVEC_REGNO, LAST_ALTIVEC_REGNO))
22704 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
22706 else
22707 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22709 /* Make sure the mode is valid in this register class. */
22710 if ((addr_mask & RELOAD_REG_VALID) == 0)
22711 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22713 if (TARGET_DEBUG_ADDR)
22714 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
22716 new_addr = addr = XEXP (mem, 0);
22717 switch (GET_CODE (addr))
22719 /* Does the register class support auto update forms for this mode? If
22720 not, do the update now. We don't need a scratch register, since the
22721 powerpc only supports PRE_INC, PRE_DEC, and PRE_MODIFY. */
22722 case PRE_INC:
22723 case PRE_DEC:
22724 op_reg = XEXP (addr, 0);
22725 if (!base_reg_operand (op_reg, Pmode))
22726 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22728 if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
22730 emit_insn (gen_add2_insn (op_reg, GEN_INT (GET_MODE_SIZE (mode))));
22731 new_addr = op_reg;
22733 break;
22735 case PRE_MODIFY:
22736 op0 = XEXP (addr, 0);
22737 op1 = XEXP (addr, 1);
22738 if (!base_reg_operand (op0, Pmode)
22739 || GET_CODE (op1) != PLUS
22740 || !rtx_equal_p (op0, XEXP (op1, 0)))
22741 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22743 if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
22745 emit_insn (gen_rtx_SET (op0, op1));
22746 new_addr = reg;
22748 break;
22750 /* Do we need to simulate AND -16 to clear the bottom address bits used
22751 in VMX load/stores? */
22752 case AND:
22753 op0 = XEXP (addr, 0);
22754 op1 = XEXP (addr, 1);
22755 if ((addr_mask & RELOAD_REG_AND_M16) == 0)
22757 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
22758 op_reg = op0;
22760 else if (GET_CODE (op1) == PLUS)
22762 emit_insn (gen_rtx_SET (scratch, op1));
22763 op_reg = scratch;
22766 else
22767 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22769 and_op = gen_rtx_AND (GET_MODE (scratch), op_reg, op1);
22770 cc_clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (CCmode));
22771 rv = gen_rtvec (2, gen_rtx_SET (scratch, and_op), cc_clobber);
22772 emit_insn (gen_rtx_PARALLEL (VOIDmode, rv));
22773 new_addr = scratch;
22775 break;
22777 /* If this is an indirect address, make sure it is a base register. */
22778 case REG:
22779 case SUBREG:
22780 if (!base_reg_operand (addr, GET_MODE (addr)))
22782 emit_insn (gen_rtx_SET (scratch, addr));
22783 new_addr = scratch;
22785 break;
22787 /* If this is an indexed address, make sure the register class can handle
22788 indexed addresses for this mode. */
22789 case PLUS:
22790 op0 = XEXP (addr, 0);
22791 op1 = XEXP (addr, 1);
22792 if (!base_reg_operand (op0, Pmode))
22793 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22795 else if (int_reg_operand (op1, Pmode))
22797 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
22799 emit_insn (gen_rtx_SET (scratch, addr));
22800 new_addr = scratch;
22804 else if (mode_supports_vsx_dform_quad (mode) && CONST_INT_P (op1))
22806 if (((addr_mask & RELOAD_REG_QUAD_OFFSET) == 0)
22807 || !quad_address_p (addr, mode, false))
22809 emit_insn (gen_rtx_SET (scratch, addr));
22810 new_addr = scratch;
22814 /* Make sure the register class can handle offset addresses. */
22815 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
22817 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22819 emit_insn (gen_rtx_SET (scratch, addr));
22820 new_addr = scratch;
22824 else
22825 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22827 break;
22829 case LO_SUM:
22830 op0 = XEXP (addr, 0);
22831 op1 = XEXP (addr, 1);
22832 if (!base_reg_operand (op0, Pmode))
22833 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22835 else if (int_reg_operand (op1, Pmode))
22837 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
22839 emit_insn (gen_rtx_SET (scratch, addr));
22840 new_addr = scratch;
22844 /* Quad offsets are restricted and can't handle normal addresses. */
22845 else if (mode_supports_vsx_dform_quad (mode))
22847 emit_insn (gen_rtx_SET (scratch, addr));
22848 new_addr = scratch;
22851 /* Make sure the register class can handle offset addresses. */
22852 else if (legitimate_lo_sum_address_p (mode, addr, false))
22854 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22856 emit_insn (gen_rtx_SET (scratch, addr));
22857 new_addr = scratch;
22861 else
22862 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22864 break;
22866 case SYMBOL_REF:
22867 case CONST:
22868 case LABEL_REF:
22869 rs6000_emit_move (scratch, addr, Pmode);
22870 new_addr = scratch;
22871 break;
22873 default:
22874 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22877 /* Adjust the address if it changed. */
22878 if (addr != new_addr)
22880 mem = replace_equiv_address_nv (mem, new_addr);
22881 if (TARGET_DEBUG_ADDR)
22882 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
22885 /* Now create the move. */
22886 if (store_p)
22887 emit_insn (gen_rtx_SET (mem, reg));
22888 else
22889 emit_insn (gen_rtx_SET (reg, mem));
22891 return;
22894 /* Convert reloads involving 64-bit gprs and misaligned offset
22895 addressing, or multiple 32-bit gprs and offsets that are too large,
22896 to use indirect addressing. */
22898 void
22899 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
22901 int regno = true_regnum (reg);
22902 enum reg_class rclass;
22903 rtx addr;
22904 rtx scratch_or_premodify = scratch;
22906 if (TARGET_DEBUG_ADDR)
22908 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
22909 store_p ? "store" : "load");
22910 fprintf (stderr, "reg:\n");
22911 debug_rtx (reg);
22912 fprintf (stderr, "mem:\n");
22913 debug_rtx (mem);
22914 fprintf (stderr, "scratch:\n");
22915 debug_rtx (scratch);
22918 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
22919 gcc_assert (GET_CODE (mem) == MEM);
22920 rclass = REGNO_REG_CLASS (regno);
22921 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
22922 addr = XEXP (mem, 0);
22924 if (GET_CODE (addr) == PRE_MODIFY)
22926 gcc_assert (REG_P (XEXP (addr, 0))
22927 && GET_CODE (XEXP (addr, 1)) == PLUS
22928 && XEXP (XEXP (addr, 1), 0) == XEXP (addr, 0));
22929 scratch_or_premodify = XEXP (addr, 0);
22930 if (!HARD_REGISTER_P (scratch_or_premodify))
22931 /* If we have a pseudo here then reload will have arranged
22932 to have it replaced, but only in the original insn.
22933 Use the replacement here too. */
22934 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
22936 /* RTL emitted by rs6000_secondary_reload_gpr uses RTL
22937 expressions from the original insn, without unsharing them.
22938 Any RTL that points into the original insn will of course
22939 have register replacements applied. That is why we don't
22940 need to look for replacements under the PLUS. */
22941 addr = XEXP (addr, 1);
22943 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
22945 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
22947 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
22949 /* Now create the move. */
22950 if (store_p)
22951 emit_insn (gen_rtx_SET (mem, reg));
22952 else
22953 emit_insn (gen_rtx_SET (reg, mem));
22955 return;
22958 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
22959 this function has any SDmode references. If we are on a power7 or later, we
22960 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
22961 can load/store the value. */
22963 static void
22964 rs6000_alloc_sdmode_stack_slot (void)
22966 tree t;
22967 basic_block bb;
22968 gimple_stmt_iterator gsi;
22970 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
22971 /* We use a different approach for dealing with the secondary
22972 memory in LRA. */
22973 if (ira_use_lra_p)
22974 return;
22976 if (TARGET_NO_SDMODE_STACK)
22977 return;
22979 FOR_EACH_BB_FN (bb, cfun)
22980 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
22982 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
22983 if (ret)
22985 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
22986 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
22987 SDmode, 0);
22988 return;
22992 /* Check for any SDmode parameters of the function. */
22993 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
22995 if (TREE_TYPE (t) == error_mark_node)
22996 continue;
22998 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
22999 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
23001 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
23002 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
23003 SDmode, 0);
23004 return;
23009 static void
23010 rs6000_instantiate_decls (void)
23012 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
23013 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
23016 /* Given an rtx X being reloaded into a reg required to be
23017 in class CLASS, return the class of reg to actually use.
23018 In general this is just CLASS; but on some machines
23019 in some cases it is preferable to use a more restrictive class.
23021 On the RS/6000, we have to return NO_REGS when we want to reload a
23022 floating-point CONST_DOUBLE to force it to be copied to memory.
23024 We also don't want to reload integer values into floating-point
23025 registers if we can at all help it. In fact, this can
23026 cause reload to die, if it tries to generate a reload of CTR
23027 into a FP register and discovers it doesn't have the memory location
23028 required.
23030 ??? Would it be a good idea to have reload do the converse, that is
23031 try to reload floating modes into FP registers if possible?
23034 static enum reg_class
23035 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
23037 machine_mode mode = GET_MODE (x);
23038 bool is_constant = CONSTANT_P (x);
23040 /* If a mode can't go in FPR/ALTIVEC/VSX registers, don't return a preferred
23041 reload class for it. */
23042 if ((rclass == ALTIVEC_REGS || rclass == VSX_REGS)
23043 && (reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID) == 0)
23044 return NO_REGS;
23046 if ((rclass == FLOAT_REGS || rclass == VSX_REGS)
23047 && (reg_addr[mode].addr_mask[RELOAD_REG_FPR] & RELOAD_REG_VALID) == 0)
23048 return NO_REGS;
23050 /* For VSX, see if we should prefer FLOAT_REGS or ALTIVEC_REGS. Do not allow
23051 the reloading of address expressions using PLUS into floating point
23052 registers. */
23053 if (TARGET_VSX && VSX_REG_CLASS_P (rclass) && GET_CODE (x) != PLUS)
23055 if (is_constant)
23057 /* Zero is always allowed in all VSX registers. */
23058 if (x == CONST0_RTX (mode))
23059 return rclass;
23061 /* If this is a vector constant that can be formed with a few Altivec
23062 instructions, we want altivec registers. */
23063 if (GET_CODE (x) == CONST_VECTOR && easy_vector_constant (x, mode))
23064 return ALTIVEC_REGS;
23066 /* If this is an integer constant that can easily be loaded into
23067 vector registers, allow it. */
23068 if (CONST_INT_P (x))
23070 HOST_WIDE_INT value = INTVAL (x);
23072 /* ISA 2.07 can generate -1 in all registers with XXLORC. ISA
23073 2.06 can generate it in the Altivec registers with
23074 VSPLTI<x>. */
23075 if (value == -1)
23077 if (TARGET_P8_VECTOR)
23078 return rclass;
23079 else if (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
23080 return ALTIVEC_REGS;
23081 else
23082 return NO_REGS;
23085 /* ISA 3.0 can load -128..127 using the XXSPLTIB instruction and
23086 a sign extend in the Altivec registers. */
23087 if (IN_RANGE (value, -128, 127) && TARGET_P9_VECTOR
23088 && TARGET_VSX_SMALL_INTEGER
23089 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS))
23090 return ALTIVEC_REGS;
23093 /* Force constant to memory. */
23094 return NO_REGS;
23097 /* D-form addressing can easily reload the value. */
23098 if (mode_supports_vmx_dform (mode)
23099 || mode_supports_vsx_dform_quad (mode))
23100 return rclass;
23102 /* If this is a scalar floating point value and we don't have D-form
23103 addressing, prefer the traditional floating point registers so that we
23104 can use D-form (register+offset) addressing. */
23105 if (rclass == VSX_REGS
23106 && (mode == SFmode || GET_MODE_SIZE (mode) == 8))
23107 return FLOAT_REGS;
23109 /* Prefer the Altivec registers if Altivec is handling the vector
23110 operations (i.e. V16QI, V8HI, and V4SI), or if we prefer Altivec
23111 loads. */
23112 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
23113 || mode == V1TImode)
23114 return ALTIVEC_REGS;
23116 return rclass;
23119 if (is_constant || GET_CODE (x) == PLUS)
23121 if (reg_class_subset_p (GENERAL_REGS, rclass))
23122 return GENERAL_REGS;
23123 if (reg_class_subset_p (BASE_REGS, rclass))
23124 return BASE_REGS;
23125 return NO_REGS;
23128 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
23129 return GENERAL_REGS;
23131 return rclass;
23134 /* Debug version of rs6000_preferred_reload_class. */
23135 static enum reg_class
23136 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
23138 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
23140 fprintf (stderr,
23141 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
23142 "mode = %s, x:\n",
23143 reg_class_names[ret], reg_class_names[rclass],
23144 GET_MODE_NAME (GET_MODE (x)));
23145 debug_rtx (x);
23147 return ret;
23150 /* If we are copying between FP or AltiVec registers and anything else, we need
23151 a memory location. The exception is when we are targeting ppc64 and the
23152 move to/from fpr to gpr instructions are available. Also, under VSX, you
23153 can copy vector registers from the FP register set to the Altivec register
23154 set and vice versa. */
23156 static bool
23157 rs6000_secondary_memory_needed (machine_mode mode,
23158 reg_class_t from_class,
23159 reg_class_t to_class)
23161 enum rs6000_reg_type from_type, to_type;
23162 bool altivec_p = ((from_class == ALTIVEC_REGS)
23163 || (to_class == ALTIVEC_REGS));
23165 /* If a simple/direct move is available, we don't need secondary memory */
23166 from_type = reg_class_to_reg_type[(int)from_class];
23167 to_type = reg_class_to_reg_type[(int)to_class];
23169 if (rs6000_secondary_reload_move (to_type, from_type, mode,
23170 (secondary_reload_info *)0, altivec_p))
23171 return false;
23173 /* If we have a floating point or vector register class, we need to use
23174 memory to transfer the data. */
23175 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
23176 return true;
23178 return false;
23181 /* Debug version of rs6000_secondary_memory_needed. */
23182 static bool
23183 rs6000_debug_secondary_memory_needed (machine_mode mode,
23184 reg_class_t from_class,
23185 reg_class_t to_class)
23187 bool ret = rs6000_secondary_memory_needed (mode, from_class, to_class);
23189 fprintf (stderr,
23190 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
23191 "to_class = %s, mode = %s\n",
23192 ret ? "true" : "false",
23193 reg_class_names[from_class],
23194 reg_class_names[to_class],
23195 GET_MODE_NAME (mode));
23197 return ret;
23200 /* Return the register class of a scratch register needed to copy IN into
23201 or out of a register in RCLASS in MODE. If it can be done directly,
23202 NO_REGS is returned. */
23204 static enum reg_class
23205 rs6000_secondary_reload_class (enum reg_class rclass, machine_mode mode,
23206 rtx in)
23208 int regno;
23210 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
23211 #if TARGET_MACHO
23212 && MACHOPIC_INDIRECT
23213 #endif
23216 /* We cannot copy a symbolic operand directly into anything
23217 other than BASE_REGS for TARGET_ELF. So indicate that a
23218 register from BASE_REGS is needed as an intermediate
23219 register.
23221 On Darwin, pic addresses require a load from memory, which
23222 needs a base register. */
23223 if (rclass != BASE_REGS
23224 && (GET_CODE (in) == SYMBOL_REF
23225 || GET_CODE (in) == HIGH
23226 || GET_CODE (in) == LABEL_REF
23227 || GET_CODE (in) == CONST))
23228 return BASE_REGS;
23231 if (GET_CODE (in) == REG)
23233 regno = REGNO (in);
23234 if (regno >= FIRST_PSEUDO_REGISTER)
23236 regno = true_regnum (in);
23237 if (regno >= FIRST_PSEUDO_REGISTER)
23238 regno = -1;
23241 else if (GET_CODE (in) == SUBREG)
23243 regno = true_regnum (in);
23244 if (regno >= FIRST_PSEUDO_REGISTER)
23245 regno = -1;
23247 else
23248 regno = -1;
23250 /* If we have VSX register moves, prefer moving scalar values between
23251 Altivec registers and GPR by going via an FPR (and then via memory)
23252 instead of reloading the secondary memory address for Altivec moves. */
23253 if (TARGET_VSX
23254 && GET_MODE_SIZE (mode) < 16
23255 && !mode_supports_vmx_dform (mode)
23256 && (((rclass == GENERAL_REGS || rclass == BASE_REGS)
23257 && (regno >= 0 && ALTIVEC_REGNO_P (regno)))
23258 || ((rclass == VSX_REGS || rclass == ALTIVEC_REGS)
23259 && (regno >= 0 && INT_REGNO_P (regno)))))
23260 return FLOAT_REGS;
23262 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
23263 into anything. */
23264 if (rclass == GENERAL_REGS || rclass == BASE_REGS
23265 || (regno >= 0 && INT_REGNO_P (regno)))
23266 return NO_REGS;
23268 /* Constants, memory, and VSX registers can go into VSX registers (both the
23269 traditional floating point and the altivec registers). */
23270 if (rclass == VSX_REGS
23271 && (regno == -1 || VSX_REGNO_P (regno)))
23272 return NO_REGS;
23274 /* Constants, memory, and FP registers can go into FP registers. */
23275 if ((regno == -1 || FP_REGNO_P (regno))
23276 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
23277 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
23279 /* Memory, and AltiVec registers can go into AltiVec registers. */
23280 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
23281 && rclass == ALTIVEC_REGS)
23282 return NO_REGS;
23284 /* We can copy among the CR registers. */
23285 if ((rclass == CR_REGS || rclass == CR0_REGS)
23286 && regno >= 0 && CR_REGNO_P (regno))
23287 return NO_REGS;
23289 /* Otherwise, we need GENERAL_REGS. */
23290 return GENERAL_REGS;
23293 /* Debug version of rs6000_secondary_reload_class. */
23294 static enum reg_class
23295 rs6000_debug_secondary_reload_class (enum reg_class rclass,
23296 machine_mode mode, rtx in)
23298 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
23299 fprintf (stderr,
23300 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
23301 "mode = %s, input rtx:\n",
23302 reg_class_names[ret], reg_class_names[rclass],
23303 GET_MODE_NAME (mode));
23304 debug_rtx (in);
23306 return ret;
23309 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
23311 static bool
23312 rs6000_can_change_mode_class (machine_mode from,
23313 machine_mode to,
23314 reg_class_t rclass)
23316 unsigned from_size = GET_MODE_SIZE (from);
23317 unsigned to_size = GET_MODE_SIZE (to);
23319 if (from_size != to_size)
23321 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
23323 if (reg_classes_intersect_p (xclass, rclass))
23325 unsigned to_nregs = hard_regno_nregs (FIRST_FPR_REGNO, to);
23326 unsigned from_nregs = hard_regno_nregs (FIRST_FPR_REGNO, from);
23327 bool to_float128_vector_p = FLOAT128_VECTOR_P (to);
23328 bool from_float128_vector_p = FLOAT128_VECTOR_P (from);
23330 /* Don't allow 64-bit types to overlap with 128-bit types that take a
23331 single register under VSX because the scalar part of the register
23332 is in the upper 64-bits, and not the lower 64-bits. Types like
23333 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
23334 IEEE floating point can't overlap, and neither can small
23335 values. */
23337 if (to_float128_vector_p && from_float128_vector_p)
23338 return true;
23340 else if (to_float128_vector_p || from_float128_vector_p)
23341 return false;
23343 /* TDmode in floating-mode registers must always go into a register
23344 pair with the most significant word in the even-numbered register
23345 to match ISA requirements. In little-endian mode, this does not
23346 match subreg numbering, so we cannot allow subregs. */
23347 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
23348 return false;
23350 if (from_size < 8 || to_size < 8)
23351 return false;
23353 if (from_size == 8 && (8 * to_nregs) != to_size)
23354 return false;
23356 if (to_size == 8 && (8 * from_nregs) != from_size)
23357 return false;
23359 return true;
23361 else
23362 return true;
23365 if (TARGET_E500_DOUBLE
23366 && ((((to) == DFmode) + ((from) == DFmode)) == 1
23367 || (((to) == TFmode) + ((from) == TFmode)) == 1
23368 || (((to) == IFmode) + ((from) == IFmode)) == 1
23369 || (((to) == KFmode) + ((from) == KFmode)) == 1
23370 || (((to) == DDmode) + ((from) == DDmode)) == 1
23371 || (((to) == TDmode) + ((from) == TDmode)) == 1
23372 || (((to) == DImode) + ((from) == DImode)) == 1))
23373 return false;
23375 /* Since the VSX register set includes traditional floating point registers
23376 and altivec registers, just check for the size being different instead of
23377 trying to check whether the modes are vector modes. Otherwise it won't
23378 allow say DF and DI to change classes. For types like TFmode and TDmode
23379 that take 2 64-bit registers, rather than a single 128-bit register, don't
23380 allow subregs of those types to other 128 bit types. */
23381 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
23383 unsigned num_regs = (from_size + 15) / 16;
23384 if (hard_regno_nregs (FIRST_FPR_REGNO, to) > num_regs
23385 || hard_regno_nregs (FIRST_FPR_REGNO, from) > num_regs)
23386 return false;
23388 return (from_size == 8 || from_size == 16);
23391 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
23392 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
23393 return false;
23395 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
23396 && reg_classes_intersect_p (GENERAL_REGS, rclass))
23397 return false;
23399 return true;
23402 /* Debug version of rs6000_can_change_mode_class. */
23403 static bool
23404 rs6000_debug_can_change_mode_class (machine_mode from,
23405 machine_mode to,
23406 reg_class_t rclass)
23408 bool ret = rs6000_can_change_mode_class (from, to, rclass);
23410 fprintf (stderr,
23411 "rs6000_can_change_mode_class, return %s, from = %s, "
23412 "to = %s, rclass = %s\n",
23413 ret ? "true" : "false",
23414 GET_MODE_NAME (from), GET_MODE_NAME (to),
23415 reg_class_names[rclass]);
23417 return ret;
23420 /* Return a string to do a move operation of 128 bits of data. */
23422 const char *
23423 rs6000_output_move_128bit (rtx operands[])
23425 rtx dest = operands[0];
23426 rtx src = operands[1];
23427 machine_mode mode = GET_MODE (dest);
23428 int dest_regno;
23429 int src_regno;
23430 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
23431 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
23433 if (REG_P (dest))
23435 dest_regno = REGNO (dest);
23436 dest_gpr_p = INT_REGNO_P (dest_regno);
23437 dest_fp_p = FP_REGNO_P (dest_regno);
23438 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
23439 dest_vsx_p = dest_fp_p | dest_vmx_p;
23441 else
23443 dest_regno = -1;
23444 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
23447 if (REG_P (src))
23449 src_regno = REGNO (src);
23450 src_gpr_p = INT_REGNO_P (src_regno);
23451 src_fp_p = FP_REGNO_P (src_regno);
23452 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
23453 src_vsx_p = src_fp_p | src_vmx_p;
23455 else
23457 src_regno = -1;
23458 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
23461 /* Register moves. */
23462 if (dest_regno >= 0 && src_regno >= 0)
23464 if (dest_gpr_p)
23466 if (src_gpr_p)
23467 return "#";
23469 if (TARGET_DIRECT_MOVE_128 && src_vsx_p)
23470 return (WORDS_BIG_ENDIAN
23471 ? "mfvsrd %0,%x1\n\tmfvsrld %L0,%x1"
23472 : "mfvsrd %L0,%x1\n\tmfvsrld %0,%x1");
23474 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
23475 return "#";
23478 else if (TARGET_VSX && dest_vsx_p)
23480 if (src_vsx_p)
23481 return "xxlor %x0,%x1,%x1";
23483 else if (TARGET_DIRECT_MOVE_128 && src_gpr_p)
23484 return (WORDS_BIG_ENDIAN
23485 ? "mtvsrdd %x0,%1,%L1"
23486 : "mtvsrdd %x0,%L1,%1");
23488 else if (TARGET_DIRECT_MOVE && src_gpr_p)
23489 return "#";
23492 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
23493 return "vor %0,%1,%1";
23495 else if (dest_fp_p && src_fp_p)
23496 return "#";
23499 /* Loads. */
23500 else if (dest_regno >= 0 && MEM_P (src))
23502 if (dest_gpr_p)
23504 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
23505 return "lq %0,%1";
23506 else
23507 return "#";
23510 else if (TARGET_ALTIVEC && dest_vmx_p
23511 && altivec_indexed_or_indirect_operand (src, mode))
23512 return "lvx %0,%y1";
23514 else if (TARGET_VSX && dest_vsx_p)
23516 if (mode_supports_vsx_dform_quad (mode)
23517 && quad_address_p (XEXP (src, 0), mode, true))
23518 return "lxv %x0,%1";
23520 else if (TARGET_P9_VECTOR)
23521 return "lxvx %x0,%y1";
23523 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
23524 return "lxvw4x %x0,%y1";
23526 else
23527 return "lxvd2x %x0,%y1";
23530 else if (TARGET_ALTIVEC && dest_vmx_p)
23531 return "lvx %0,%y1";
23533 else if (dest_fp_p)
23534 return "#";
23537 /* Stores. */
23538 else if (src_regno >= 0 && MEM_P (dest))
23540 if (src_gpr_p)
23542 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
23543 return "stq %1,%0";
23544 else
23545 return "#";
23548 else if (TARGET_ALTIVEC && src_vmx_p
23549 && altivec_indexed_or_indirect_operand (src, mode))
23550 return "stvx %1,%y0";
23552 else if (TARGET_VSX && src_vsx_p)
23554 if (mode_supports_vsx_dform_quad (mode)
23555 && quad_address_p (XEXP (dest, 0), mode, true))
23556 return "stxv %x1,%0";
23558 else if (TARGET_P9_VECTOR)
23559 return "stxvx %x1,%y0";
23561 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
23562 return "stxvw4x %x1,%y0";
23564 else
23565 return "stxvd2x %x1,%y0";
23568 else if (TARGET_ALTIVEC && src_vmx_p)
23569 return "stvx %1,%y0";
23571 else if (src_fp_p)
23572 return "#";
23575 /* Constants. */
23576 else if (dest_regno >= 0
23577 && (GET_CODE (src) == CONST_INT
23578 || GET_CODE (src) == CONST_WIDE_INT
23579 || GET_CODE (src) == CONST_DOUBLE
23580 || GET_CODE (src) == CONST_VECTOR))
23582 if (dest_gpr_p)
23583 return "#";
23585 else if ((dest_vmx_p && TARGET_ALTIVEC)
23586 || (dest_vsx_p && TARGET_VSX))
23587 return output_vec_const_move (operands);
23590 fatal_insn ("Bad 128-bit move", gen_rtx_SET (dest, src));
23593 /* Validate a 128-bit move. */
23594 bool
23595 rs6000_move_128bit_ok_p (rtx operands[])
23597 machine_mode mode = GET_MODE (operands[0]);
23598 return (gpc_reg_operand (operands[0], mode)
23599 || gpc_reg_operand (operands[1], mode));
23602 /* Return true if a 128-bit move needs to be split. */
23603 bool
23604 rs6000_split_128bit_ok_p (rtx operands[])
23606 if (!reload_completed)
23607 return false;
23609 if (!gpr_or_gpr_p (operands[0], operands[1]))
23610 return false;
23612 if (quad_load_store_p (operands[0], operands[1]))
23613 return false;
23615 return true;
23619 /* Given a comparison operation, return the bit number in CCR to test. We
23620 know this is a valid comparison.
23622 SCC_P is 1 if this is for an scc. That means that %D will have been
23623 used instead of %C, so the bits will be in different places.
23625 Return -1 if OP isn't a valid comparison for some reason. */
23628 ccr_bit (rtx op, int scc_p)
23630 enum rtx_code code = GET_CODE (op);
23631 machine_mode cc_mode;
23632 int cc_regnum;
23633 int base_bit;
23634 rtx reg;
23636 if (!COMPARISON_P (op))
23637 return -1;
23639 reg = XEXP (op, 0);
23641 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
23643 cc_mode = GET_MODE (reg);
23644 cc_regnum = REGNO (reg);
23645 base_bit = 4 * (cc_regnum - CR0_REGNO);
23647 validate_condition_mode (code, cc_mode);
23649 /* When generating a sCOND operation, only positive conditions are
23650 allowed. */
23651 gcc_assert (!scc_p
23652 || code == EQ || code == GT || code == LT || code == UNORDERED
23653 || code == GTU || code == LTU);
23655 switch (code)
23657 case NE:
23658 return scc_p ? base_bit + 3 : base_bit + 2;
23659 case EQ:
23660 return base_bit + 2;
23661 case GT: case GTU: case UNLE:
23662 return base_bit + 1;
23663 case LT: case LTU: case UNGE:
23664 return base_bit;
23665 case ORDERED: case UNORDERED:
23666 return base_bit + 3;
23668 case GE: case GEU:
23669 /* If scc, we will have done a cror to put the bit in the
23670 unordered position. So test that bit. For integer, this is ! LT
23671 unless this is an scc insn. */
23672 return scc_p ? base_bit + 3 : base_bit;
23674 case LE: case LEU:
23675 return scc_p ? base_bit + 3 : base_bit + 1;
23677 default:
23678 gcc_unreachable ();
23682 /* Return the GOT register. */
23685 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
23687 /* The second flow pass currently (June 1999) can't update
23688 regs_ever_live without disturbing other parts of the compiler, so
23689 update it here to make the prolog/epilogue code happy. */
23690 if (!can_create_pseudo_p ()
23691 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
23692 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
23694 crtl->uses_pic_offset_table = 1;
23696 return pic_offset_table_rtx;
23699 static rs6000_stack_t stack_info;
23701 /* Function to init struct machine_function.
23702 This will be called, via a pointer variable,
23703 from push_function_context. */
23705 static struct machine_function *
23706 rs6000_init_machine_status (void)
23708 stack_info.reload_completed = 0;
23709 return ggc_cleared_alloc<machine_function> ();
23712 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
23714 /* Write out a function code label. */
23716 void
23717 rs6000_output_function_entry (FILE *file, const char *fname)
23719 if (fname[0] != '.')
23721 switch (DEFAULT_ABI)
23723 default:
23724 gcc_unreachable ();
23726 case ABI_AIX:
23727 if (DOT_SYMBOLS)
23728 putc ('.', file);
23729 else
23730 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
23731 break;
23733 case ABI_ELFv2:
23734 case ABI_V4:
23735 case ABI_DARWIN:
23736 break;
23740 RS6000_OUTPUT_BASENAME (file, fname);
23743 /* Print an operand. Recognize special options, documented below. */
23745 #if TARGET_ELF
23746 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
23747 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
23748 #else
23749 #define SMALL_DATA_RELOC "sda21"
23750 #define SMALL_DATA_REG 0
23751 #endif
23753 void
23754 print_operand (FILE *file, rtx x, int code)
23756 int i;
23757 unsigned HOST_WIDE_INT uval;
23759 switch (code)
23761 /* %a is output_address. */
23763 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
23764 output_operand. */
23766 case 'D':
23767 /* Like 'J' but get to the GT bit only. */
23768 gcc_assert (REG_P (x));
23770 /* Bit 1 is GT bit. */
23771 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
23773 /* Add one for shift count in rlinm for scc. */
23774 fprintf (file, "%d", i + 1);
23775 return;
23777 case 'e':
23778 /* If the low 16 bits are 0, but some other bit is set, write 's'. */
23779 if (! INT_P (x))
23781 output_operand_lossage ("invalid %%e value");
23782 return;
23785 uval = INTVAL (x);
23786 if ((uval & 0xffff) == 0 && uval != 0)
23787 putc ('s', file);
23788 return;
23790 case 'E':
23791 /* X is a CR register. Print the number of the EQ bit of the CR */
23792 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23793 output_operand_lossage ("invalid %%E value");
23794 else
23795 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
23796 return;
23798 case 'f':
23799 /* X is a CR register. Print the shift count needed to move it
23800 to the high-order four bits. */
23801 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23802 output_operand_lossage ("invalid %%f value");
23803 else
23804 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
23805 return;
23807 case 'F':
23808 /* Similar, but print the count for the rotate in the opposite
23809 direction. */
23810 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23811 output_operand_lossage ("invalid %%F value");
23812 else
23813 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
23814 return;
23816 case 'G':
23817 /* X is a constant integer. If it is negative, print "m",
23818 otherwise print "z". This is to make an aze or ame insn. */
23819 if (GET_CODE (x) != CONST_INT)
23820 output_operand_lossage ("invalid %%G value");
23821 else if (INTVAL (x) >= 0)
23822 putc ('z', file);
23823 else
23824 putc ('m', file);
23825 return;
23827 case 'h':
23828 /* If constant, output low-order five bits. Otherwise, write
23829 normally. */
23830 if (INT_P (x))
23831 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
23832 else
23833 print_operand (file, x, 0);
23834 return;
23836 case 'H':
23837 /* If constant, output low-order six bits. Otherwise, write
23838 normally. */
23839 if (INT_P (x))
23840 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
23841 else
23842 print_operand (file, x, 0);
23843 return;
23845 case 'I':
23846 /* Print `i' if this is a constant, else nothing. */
23847 if (INT_P (x))
23848 putc ('i', file);
23849 return;
23851 case 'j':
23852 /* Write the bit number in CCR for jump. */
23853 i = ccr_bit (x, 0);
23854 if (i == -1)
23855 output_operand_lossage ("invalid %%j code");
23856 else
23857 fprintf (file, "%d", i);
23858 return;
23860 case 'J':
23861 /* Similar, but add one for shift count in rlinm for scc and pass
23862 scc flag to `ccr_bit'. */
23863 i = ccr_bit (x, 1);
23864 if (i == -1)
23865 output_operand_lossage ("invalid %%J code");
23866 else
23867 /* If we want bit 31, write a shift count of zero, not 32. */
23868 fprintf (file, "%d", i == 31 ? 0 : i + 1);
23869 return;
23871 case 'k':
23872 /* X must be a constant. Write the 1's complement of the
23873 constant. */
23874 if (! INT_P (x))
23875 output_operand_lossage ("invalid %%k value");
23876 else
23877 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
23878 return;
23880 case 'K':
23881 /* X must be a symbolic constant on ELF. Write an
23882 expression suitable for an 'addi' that adds in the low 16
23883 bits of the MEM. */
23884 if (GET_CODE (x) == CONST)
23886 if (GET_CODE (XEXP (x, 0)) != PLUS
23887 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
23888 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
23889 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
23890 output_operand_lossage ("invalid %%K value");
23892 print_operand_address (file, x);
23893 fputs ("@l", file);
23894 return;
23896 /* %l is output_asm_label. */
23898 case 'L':
23899 /* Write second word of DImode or DFmode reference. Works on register
23900 or non-indexed memory only. */
23901 if (REG_P (x))
23902 fputs (reg_names[REGNO (x) + 1], file);
23903 else if (MEM_P (x))
23905 machine_mode mode = GET_MODE (x);
23906 /* Handle possible auto-increment. Since it is pre-increment and
23907 we have already done it, we can just use an offset of word. */
23908 if (GET_CODE (XEXP (x, 0)) == PRE_INC
23909 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
23910 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
23911 UNITS_PER_WORD));
23912 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
23913 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
23914 UNITS_PER_WORD));
23915 else
23916 output_address (mode, XEXP (adjust_address_nv (x, SImode,
23917 UNITS_PER_WORD),
23918 0));
23920 if (small_data_operand (x, GET_MODE (x)))
23921 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
23922 reg_names[SMALL_DATA_REG]);
23924 return;
23926 case 'N':
23927 /* Write the number of elements in the vector times 4. */
23928 if (GET_CODE (x) != PARALLEL)
23929 output_operand_lossage ("invalid %%N value");
23930 else
23931 fprintf (file, "%d", XVECLEN (x, 0) * 4);
23932 return;
23934 case 'O':
23935 /* Similar, but subtract 1 first. */
23936 if (GET_CODE (x) != PARALLEL)
23937 output_operand_lossage ("invalid %%O value");
23938 else
23939 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
23940 return;
23942 case 'p':
23943 /* X is a CONST_INT that is a power of two. Output the logarithm. */
23944 if (! INT_P (x)
23945 || INTVAL (x) < 0
23946 || (i = exact_log2 (INTVAL (x))) < 0)
23947 output_operand_lossage ("invalid %%p value");
23948 else
23949 fprintf (file, "%d", i);
23950 return;
23952 case 'P':
23953 /* The operand must be an indirect memory reference. The result
23954 is the register name. */
23955 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
23956 || REGNO (XEXP (x, 0)) >= 32)
23957 output_operand_lossage ("invalid %%P value");
23958 else
23959 fputs (reg_names[REGNO (XEXP (x, 0))], file);
23960 return;
23962 case 'q':
23963 /* This outputs the logical code corresponding to a boolean
23964 expression. The expression may have one or both operands
23965 negated (if one, only the first one). For condition register
23966 logical operations, it will also treat the negated
23967 CR codes as NOTs, but not handle NOTs of them. */
23969 const char *const *t = 0;
23970 const char *s;
23971 enum rtx_code code = GET_CODE (x);
23972 static const char * const tbl[3][3] = {
23973 { "and", "andc", "nor" },
23974 { "or", "orc", "nand" },
23975 { "xor", "eqv", "xor" } };
23977 if (code == AND)
23978 t = tbl[0];
23979 else if (code == IOR)
23980 t = tbl[1];
23981 else if (code == XOR)
23982 t = tbl[2];
23983 else
23984 output_operand_lossage ("invalid %%q value");
23986 if (GET_CODE (XEXP (x, 0)) != NOT)
23987 s = t[0];
23988 else
23990 if (GET_CODE (XEXP (x, 1)) == NOT)
23991 s = t[2];
23992 else
23993 s = t[1];
23996 fputs (s, file);
23998 return;
24000 case 'Q':
24001 if (! TARGET_MFCRF)
24002 return;
24003 fputc (',', file);
24004 /* FALLTHRU */
24006 case 'R':
24007 /* X is a CR register. Print the mask for `mtcrf'. */
24008 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
24009 output_operand_lossage ("invalid %%R value");
24010 else
24011 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
24012 return;
24014 case 's':
24015 /* Low 5 bits of 32 - value */
24016 if (! INT_P (x))
24017 output_operand_lossage ("invalid %%s value");
24018 else
24019 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
24020 return;
24022 case 't':
24023 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
24024 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
24026 /* Bit 3 is OV bit. */
24027 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
24029 /* If we want bit 31, write a shift count of zero, not 32. */
24030 fprintf (file, "%d", i == 31 ? 0 : i + 1);
24031 return;
24033 case 'T':
24034 /* Print the symbolic name of a branch target register. */
24035 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
24036 && REGNO (x) != CTR_REGNO))
24037 output_operand_lossage ("invalid %%T value");
24038 else if (REGNO (x) == LR_REGNO)
24039 fputs ("lr", file);
24040 else
24041 fputs ("ctr", file);
24042 return;
24044 case 'u':
24045 /* High-order or low-order 16 bits of constant, whichever is non-zero,
24046 for use in unsigned operand. */
24047 if (! INT_P (x))
24049 output_operand_lossage ("invalid %%u value");
24050 return;
24053 uval = INTVAL (x);
24054 if ((uval & 0xffff) == 0)
24055 uval >>= 16;
24057 fprintf (file, HOST_WIDE_INT_PRINT_HEX, uval & 0xffff);
24058 return;
24060 case 'v':
24061 /* High-order 16 bits of constant for use in signed operand. */
24062 if (! INT_P (x))
24063 output_operand_lossage ("invalid %%v value");
24064 else
24065 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
24066 (INTVAL (x) >> 16) & 0xffff);
24067 return;
24069 case 'U':
24070 /* Print `u' if this has an auto-increment or auto-decrement. */
24071 if (MEM_P (x)
24072 && (GET_CODE (XEXP (x, 0)) == PRE_INC
24073 || GET_CODE (XEXP (x, 0)) == PRE_DEC
24074 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
24075 putc ('u', file);
24076 return;
24078 case 'V':
24079 /* Print the trap code for this operand. */
24080 switch (GET_CODE (x))
24082 case EQ:
24083 fputs ("eq", file); /* 4 */
24084 break;
24085 case NE:
24086 fputs ("ne", file); /* 24 */
24087 break;
24088 case LT:
24089 fputs ("lt", file); /* 16 */
24090 break;
24091 case LE:
24092 fputs ("le", file); /* 20 */
24093 break;
24094 case GT:
24095 fputs ("gt", file); /* 8 */
24096 break;
24097 case GE:
24098 fputs ("ge", file); /* 12 */
24099 break;
24100 case LTU:
24101 fputs ("llt", file); /* 2 */
24102 break;
24103 case LEU:
24104 fputs ("lle", file); /* 6 */
24105 break;
24106 case GTU:
24107 fputs ("lgt", file); /* 1 */
24108 break;
24109 case GEU:
24110 fputs ("lge", file); /* 5 */
24111 break;
24112 default:
24113 gcc_unreachable ();
24115 break;
24117 case 'w':
24118 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
24119 normally. */
24120 if (INT_P (x))
24121 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
24122 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
24123 else
24124 print_operand (file, x, 0);
24125 return;
24127 case 'x':
24128 /* X is a FPR or Altivec register used in a VSX context. */
24129 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
24130 output_operand_lossage ("invalid %%x value");
24131 else
24133 int reg = REGNO (x);
24134 int vsx_reg = (FP_REGNO_P (reg)
24135 ? reg - 32
24136 : reg - FIRST_ALTIVEC_REGNO + 32);
24138 #ifdef TARGET_REGNAMES
24139 if (TARGET_REGNAMES)
24140 fprintf (file, "%%vs%d", vsx_reg);
24141 else
24142 #endif
24143 fprintf (file, "%d", vsx_reg);
24145 return;
24147 case 'X':
24148 if (MEM_P (x)
24149 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
24150 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
24151 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
24152 putc ('x', file);
24153 return;
24155 case 'Y':
24156 /* Like 'L', for third word of TImode/PTImode */
24157 if (REG_P (x))
24158 fputs (reg_names[REGNO (x) + 2], file);
24159 else if (MEM_P (x))
24161 machine_mode mode = GET_MODE (x);
24162 if (GET_CODE (XEXP (x, 0)) == PRE_INC
24163 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
24164 output_address (mode, plus_constant (Pmode,
24165 XEXP (XEXP (x, 0), 0), 8));
24166 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
24167 output_address (mode, plus_constant (Pmode,
24168 XEXP (XEXP (x, 0), 0), 8));
24169 else
24170 output_address (mode, XEXP (adjust_address_nv (x, SImode, 8), 0));
24171 if (small_data_operand (x, GET_MODE (x)))
24172 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
24173 reg_names[SMALL_DATA_REG]);
24175 return;
24177 case 'z':
24178 /* X is a SYMBOL_REF. Write out the name preceded by a
24179 period and without any trailing data in brackets. Used for function
24180 names. If we are configured for System V (or the embedded ABI) on
24181 the PowerPC, do not emit the period, since those systems do not use
24182 TOCs and the like. */
24183 gcc_assert (GET_CODE (x) == SYMBOL_REF);
24185 /* For macho, check to see if we need a stub. */
24186 if (TARGET_MACHO)
24188 const char *name = XSTR (x, 0);
24189 #if TARGET_MACHO
24190 if (darwin_emit_branch_islands
24191 && MACHOPIC_INDIRECT
24192 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
24193 name = machopic_indirection_name (x, /*stub_p=*/true);
24194 #endif
24195 assemble_name (file, name);
24197 else if (!DOT_SYMBOLS)
24198 assemble_name (file, XSTR (x, 0));
24199 else
24200 rs6000_output_function_entry (file, XSTR (x, 0));
24201 return;
24203 case 'Z':
24204 /* Like 'L', for last word of TImode/PTImode. */
24205 if (REG_P (x))
24206 fputs (reg_names[REGNO (x) + 3], file);
24207 else if (MEM_P (x))
24209 machine_mode mode = GET_MODE (x);
24210 if (GET_CODE (XEXP (x, 0)) == PRE_INC
24211 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
24212 output_address (mode, plus_constant (Pmode,
24213 XEXP (XEXP (x, 0), 0), 12));
24214 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
24215 output_address (mode, plus_constant (Pmode,
24216 XEXP (XEXP (x, 0), 0), 12));
24217 else
24218 output_address (mode, XEXP (adjust_address_nv (x, SImode, 12), 0));
24219 if (small_data_operand (x, GET_MODE (x)))
24220 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
24221 reg_names[SMALL_DATA_REG]);
24223 return;
24225 /* Print AltiVec or SPE memory operand. */
24226 case 'y':
24228 rtx tmp;
24230 gcc_assert (MEM_P (x));
24232 tmp = XEXP (x, 0);
24234 /* Ugly hack because %y is overloaded. */
24235 if ((TARGET_SPE || TARGET_E500_DOUBLE)
24236 && (GET_MODE_SIZE (GET_MODE (x)) == 8
24237 || FLOAT128_2REG_P (GET_MODE (x))
24238 || GET_MODE (x) == TImode
24239 || GET_MODE (x) == PTImode))
24241 /* Handle [reg]. */
24242 if (REG_P (tmp))
24244 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
24245 break;
24247 /* Handle [reg+UIMM]. */
24248 else if (GET_CODE (tmp) == PLUS &&
24249 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
24251 int x;
24253 gcc_assert (REG_P (XEXP (tmp, 0)));
24255 x = INTVAL (XEXP (tmp, 1));
24256 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
24257 break;
24260 /* Fall through. Must be [reg+reg]. */
24262 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
24263 && GET_CODE (tmp) == AND
24264 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
24265 && INTVAL (XEXP (tmp, 1)) == -16)
24266 tmp = XEXP (tmp, 0);
24267 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
24268 && GET_CODE (tmp) == PRE_MODIFY)
24269 tmp = XEXP (tmp, 1);
24270 if (REG_P (tmp))
24271 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
24272 else
24274 if (GET_CODE (tmp) != PLUS
24275 || !REG_P (XEXP (tmp, 0))
24276 || !REG_P (XEXP (tmp, 1)))
24278 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
24279 break;
24282 if (REGNO (XEXP (tmp, 0)) == 0)
24283 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
24284 reg_names[ REGNO (XEXP (tmp, 0)) ]);
24285 else
24286 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
24287 reg_names[ REGNO (XEXP (tmp, 1)) ]);
24289 break;
24292 case 0:
24293 if (REG_P (x))
24294 fprintf (file, "%s", reg_names[REGNO (x)]);
24295 else if (MEM_P (x))
24297 /* We need to handle PRE_INC and PRE_DEC here, since we need to
24298 know the width from the mode. */
24299 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
24300 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
24301 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
24302 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
24303 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
24304 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
24305 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
24306 output_address (GET_MODE (x), XEXP (XEXP (x, 0), 1));
24307 else
24308 output_address (GET_MODE (x), XEXP (x, 0));
24310 else
24312 if (toc_relative_expr_p (x, false))
24313 /* This hack along with a corresponding hack in
24314 rs6000_output_addr_const_extra arranges to output addends
24315 where the assembler expects to find them. eg.
24316 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
24317 without this hack would be output as "x@toc+4". We
24318 want "x+4@toc". */
24319 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
24320 else
24321 output_addr_const (file, x);
24323 return;
24325 case '&':
24326 if (const char *name = get_some_local_dynamic_name ())
24327 assemble_name (file, name);
24328 else
24329 output_operand_lossage ("'%%&' used without any "
24330 "local dynamic TLS references");
24331 return;
24333 default:
24334 output_operand_lossage ("invalid %%xn code");
24338 /* Print the address of an operand. */
24340 void
24341 print_operand_address (FILE *file, rtx x)
24343 if (REG_P (x))
24344 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
24345 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
24346 || GET_CODE (x) == LABEL_REF)
24348 output_addr_const (file, x);
24349 if (small_data_operand (x, GET_MODE (x)))
24350 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
24351 reg_names[SMALL_DATA_REG]);
24352 else
24353 gcc_assert (!TARGET_TOC);
24355 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
24356 && REG_P (XEXP (x, 1)))
24358 if (REGNO (XEXP (x, 0)) == 0)
24359 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
24360 reg_names[ REGNO (XEXP (x, 0)) ]);
24361 else
24362 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
24363 reg_names[ REGNO (XEXP (x, 1)) ]);
24365 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
24366 && GET_CODE (XEXP (x, 1)) == CONST_INT)
24367 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
24368 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
24369 #if TARGET_MACHO
24370 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
24371 && CONSTANT_P (XEXP (x, 1)))
24373 fprintf (file, "lo16(");
24374 output_addr_const (file, XEXP (x, 1));
24375 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
24377 #endif
24378 #if TARGET_ELF
24379 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
24380 && CONSTANT_P (XEXP (x, 1)))
24382 output_addr_const (file, XEXP (x, 1));
24383 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
24385 #endif
24386 else if (toc_relative_expr_p (x, false))
24388 /* This hack along with a corresponding hack in
24389 rs6000_output_addr_const_extra arranges to output addends
24390 where the assembler expects to find them. eg.
24391 (lo_sum (reg 9)
24392 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
24393 without this hack would be output as "x@toc+8@l(9)". We
24394 want "x+8@toc@l(9)". */
24395 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
24396 if (GET_CODE (x) == LO_SUM)
24397 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
24398 else
24399 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
24401 else
24402 gcc_unreachable ();
24405 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
24407 static bool
24408 rs6000_output_addr_const_extra (FILE *file, rtx x)
24410 if (GET_CODE (x) == UNSPEC)
24411 switch (XINT (x, 1))
24413 case UNSPEC_TOCREL:
24414 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
24415 && REG_P (XVECEXP (x, 0, 1))
24416 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
24417 output_addr_const (file, XVECEXP (x, 0, 0));
24418 if (x == tocrel_base && tocrel_offset != const0_rtx)
24420 if (INTVAL (tocrel_offset) >= 0)
24421 fprintf (file, "+");
24422 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
24424 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
24426 putc ('-', file);
24427 assemble_name (file, toc_label_name);
24428 need_toc_init = 1;
24430 else if (TARGET_ELF)
24431 fputs ("@toc", file);
24432 return true;
24434 #if TARGET_MACHO
24435 case UNSPEC_MACHOPIC_OFFSET:
24436 output_addr_const (file, XVECEXP (x, 0, 0));
24437 putc ('-', file);
24438 machopic_output_function_base_name (file);
24439 return true;
24440 #endif
24442 return false;
24445 /* Target hook for assembling integer objects. The PowerPC version has
24446 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
24447 is defined. It also needs to handle DI-mode objects on 64-bit
24448 targets. */
24450 static bool
24451 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
24453 #ifdef RELOCATABLE_NEEDS_FIXUP
24454 /* Special handling for SI values. */
24455 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
24457 static int recurse = 0;
24459 /* For -mrelocatable, we mark all addresses that need to be fixed up in
24460 the .fixup section. Since the TOC section is already relocated, we
24461 don't need to mark it here. We used to skip the text section, but it
24462 should never be valid for relocated addresses to be placed in the text
24463 section. */
24464 if (DEFAULT_ABI == ABI_V4
24465 && (TARGET_RELOCATABLE || flag_pic > 1)
24466 && in_section != toc_section
24467 && !recurse
24468 && !CONST_SCALAR_INT_P (x)
24469 && CONSTANT_P (x))
24471 char buf[256];
24473 recurse = 1;
24474 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
24475 fixuplabelno++;
24476 ASM_OUTPUT_LABEL (asm_out_file, buf);
24477 fprintf (asm_out_file, "\t.long\t(");
24478 output_addr_const (asm_out_file, x);
24479 fprintf (asm_out_file, ")@fixup\n");
24480 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
24481 ASM_OUTPUT_ALIGN (asm_out_file, 2);
24482 fprintf (asm_out_file, "\t.long\t");
24483 assemble_name (asm_out_file, buf);
24484 fprintf (asm_out_file, "\n\t.previous\n");
24485 recurse = 0;
24486 return true;
24488 /* Remove initial .'s to turn a -mcall-aixdesc function
24489 address into the address of the descriptor, not the function
24490 itself. */
24491 else if (GET_CODE (x) == SYMBOL_REF
24492 && XSTR (x, 0)[0] == '.'
24493 && DEFAULT_ABI == ABI_AIX)
24495 const char *name = XSTR (x, 0);
24496 while (*name == '.')
24497 name++;
24499 fprintf (asm_out_file, "\t.long\t%s\n", name);
24500 return true;
24503 #endif /* RELOCATABLE_NEEDS_FIXUP */
24504 return default_assemble_integer (x, size, aligned_p);
24507 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
24508 /* Emit an assembler directive to set symbol visibility for DECL to
24509 VISIBILITY_TYPE. */
24511 static void
24512 rs6000_assemble_visibility (tree decl, int vis)
24514 if (TARGET_XCOFF)
24515 return;
24517 /* Functions need to have their entry point symbol visibility set as
24518 well as their descriptor symbol visibility. */
24519 if (DEFAULT_ABI == ABI_AIX
24520 && DOT_SYMBOLS
24521 && TREE_CODE (decl) == FUNCTION_DECL)
24523 static const char * const visibility_types[] = {
24524 NULL, "protected", "hidden", "internal"
24527 const char *name, *type;
24529 name = ((* targetm.strip_name_encoding)
24530 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
24531 type = visibility_types[vis];
24533 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
24534 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
24536 else
24537 default_assemble_visibility (decl, vis);
24539 #endif
24541 enum rtx_code
24542 rs6000_reverse_condition (machine_mode mode, enum rtx_code code)
24544 /* Reversal of FP compares takes care -- an ordered compare
24545 becomes an unordered compare and vice versa. */
24546 if (mode == CCFPmode
24547 && (!flag_finite_math_only
24548 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
24549 || code == UNEQ || code == LTGT))
24550 return reverse_condition_maybe_unordered (code);
24551 else
24552 return reverse_condition (code);
24555 /* Generate a compare for CODE. Return a brand-new rtx that
24556 represents the result of the compare. */
24558 static rtx
24559 rs6000_generate_compare (rtx cmp, machine_mode mode)
24561 machine_mode comp_mode;
24562 rtx compare_result;
24563 enum rtx_code code = GET_CODE (cmp);
24564 rtx op0 = XEXP (cmp, 0);
24565 rtx op1 = XEXP (cmp, 1);
24567 if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
24568 comp_mode = CCmode;
24569 else if (FLOAT_MODE_P (mode))
24570 comp_mode = CCFPmode;
24571 else if (code == GTU || code == LTU
24572 || code == GEU || code == LEU)
24573 comp_mode = CCUNSmode;
24574 else if ((code == EQ || code == NE)
24575 && unsigned_reg_p (op0)
24576 && (unsigned_reg_p (op1)
24577 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
24578 /* These are unsigned values, perhaps there will be a later
24579 ordering compare that can be shared with this one. */
24580 comp_mode = CCUNSmode;
24581 else
24582 comp_mode = CCmode;
24584 /* If we have an unsigned compare, make sure we don't have a signed value as
24585 an immediate. */
24586 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
24587 && INTVAL (op1) < 0)
24589 op0 = copy_rtx_if_shared (op0);
24590 op1 = force_reg (GET_MODE (op0), op1);
24591 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
24594 /* First, the compare. */
24595 compare_result = gen_reg_rtx (comp_mode);
24597 /* E500 FP compare instructions on the GPRs. Yuck! */
24598 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
24599 && FLOAT_MODE_P (mode))
24601 rtx cmp, or_result, compare_result2;
24602 machine_mode op_mode = GET_MODE (op0);
24603 bool reverse_p;
24605 if (op_mode == VOIDmode)
24606 op_mode = GET_MODE (op1);
24608 /* First reverse the condition codes that aren't directly supported. */
24609 switch (code)
24611 case NE:
24612 case UNLT:
24613 case UNLE:
24614 case UNGT:
24615 case UNGE:
24616 code = reverse_condition_maybe_unordered (code);
24617 reverse_p = true;
24618 break;
24620 case EQ:
24621 case LT:
24622 case LE:
24623 case GT:
24624 case GE:
24625 reverse_p = false;
24626 break;
24628 default:
24629 gcc_unreachable ();
24632 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
24633 This explains the following mess. */
24635 switch (code)
24637 case EQ:
24638 switch (op_mode)
24640 case E_SFmode:
24641 cmp = (flag_finite_math_only && !flag_trapping_math)
24642 ? gen_tstsfeq_gpr (compare_result, op0, op1)
24643 : gen_cmpsfeq_gpr (compare_result, op0, op1);
24644 break;
24646 case E_DFmode:
24647 cmp = (flag_finite_math_only && !flag_trapping_math)
24648 ? gen_tstdfeq_gpr (compare_result, op0, op1)
24649 : gen_cmpdfeq_gpr (compare_result, op0, op1);
24650 break;
24652 case E_TFmode:
24653 case E_IFmode:
24654 case E_KFmode:
24655 cmp = (flag_finite_math_only && !flag_trapping_math)
24656 ? gen_tsttfeq_gpr (compare_result, op0, op1)
24657 : gen_cmptfeq_gpr (compare_result, op0, op1);
24658 break;
24660 default:
24661 gcc_unreachable ();
24663 break;
24665 case GT:
24666 case GE:
24667 switch (op_mode)
24669 case E_SFmode:
24670 cmp = (flag_finite_math_only && !flag_trapping_math)
24671 ? gen_tstsfgt_gpr (compare_result, op0, op1)
24672 : gen_cmpsfgt_gpr (compare_result, op0, op1);
24673 break;
24675 case E_DFmode:
24676 cmp = (flag_finite_math_only && !flag_trapping_math)
24677 ? gen_tstdfgt_gpr (compare_result, op0, op1)
24678 : gen_cmpdfgt_gpr (compare_result, op0, op1);
24679 break;
24681 case E_TFmode:
24682 case E_IFmode:
24683 case E_KFmode:
24684 cmp = (flag_finite_math_only && !flag_trapping_math)
24685 ? gen_tsttfgt_gpr (compare_result, op0, op1)
24686 : gen_cmptfgt_gpr (compare_result, op0, op1);
24687 break;
24689 default:
24690 gcc_unreachable ();
24692 break;
24694 case LT:
24695 case LE:
24696 switch (op_mode)
24698 case E_SFmode:
24699 cmp = (flag_finite_math_only && !flag_trapping_math)
24700 ? gen_tstsflt_gpr (compare_result, op0, op1)
24701 : gen_cmpsflt_gpr (compare_result, op0, op1);
24702 break;
24704 case E_DFmode:
24705 cmp = (flag_finite_math_only && !flag_trapping_math)
24706 ? gen_tstdflt_gpr (compare_result, op0, op1)
24707 : gen_cmpdflt_gpr (compare_result, op0, op1);
24708 break;
24710 case E_TFmode:
24711 case E_IFmode:
24712 case E_KFmode:
24713 cmp = (flag_finite_math_only && !flag_trapping_math)
24714 ? gen_tsttflt_gpr (compare_result, op0, op1)
24715 : gen_cmptflt_gpr (compare_result, op0, op1);
24716 break;
24718 default:
24719 gcc_unreachable ();
24721 break;
24723 default:
24724 gcc_unreachable ();
24727 /* Synthesize LE and GE from LT/GT || EQ. */
24728 if (code == LE || code == GE)
24730 emit_insn (cmp);
24732 compare_result2 = gen_reg_rtx (CCFPmode);
24734 /* Do the EQ. */
24735 switch (op_mode)
24737 case E_SFmode:
24738 cmp = (flag_finite_math_only && !flag_trapping_math)
24739 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
24740 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
24741 break;
24743 case E_DFmode:
24744 cmp = (flag_finite_math_only && !flag_trapping_math)
24745 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
24746 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
24747 break;
24749 case E_TFmode:
24750 case E_IFmode:
24751 case E_KFmode:
24752 cmp = (flag_finite_math_only && !flag_trapping_math)
24753 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
24754 : gen_cmptfeq_gpr (compare_result2, op0, op1);
24755 break;
24757 default:
24758 gcc_unreachable ();
24761 emit_insn (cmp);
24763 /* OR them together. */
24764 or_result = gen_reg_rtx (CCFPmode);
24765 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
24766 compare_result2);
24767 compare_result = or_result;
24770 code = reverse_p ? NE : EQ;
24772 emit_insn (cmp);
24775 /* IEEE 128-bit support in VSX registers when we do not have hardware
24776 support. */
24777 else if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
24779 rtx libfunc = NULL_RTX;
24780 bool check_nan = false;
24781 rtx dest;
24783 switch (code)
24785 case EQ:
24786 case NE:
24787 libfunc = optab_libfunc (eq_optab, mode);
24788 break;
24790 case GT:
24791 case GE:
24792 libfunc = optab_libfunc (ge_optab, mode);
24793 break;
24795 case LT:
24796 case LE:
24797 libfunc = optab_libfunc (le_optab, mode);
24798 break;
24800 case UNORDERED:
24801 case ORDERED:
24802 libfunc = optab_libfunc (unord_optab, mode);
24803 code = (code == UNORDERED) ? NE : EQ;
24804 break;
24806 case UNGE:
24807 case UNGT:
24808 check_nan = true;
24809 libfunc = optab_libfunc (ge_optab, mode);
24810 code = (code == UNGE) ? GE : GT;
24811 break;
24813 case UNLE:
24814 case UNLT:
24815 check_nan = true;
24816 libfunc = optab_libfunc (le_optab, mode);
24817 code = (code == UNLE) ? LE : LT;
24818 break;
24820 case UNEQ:
24821 case LTGT:
24822 check_nan = true;
24823 libfunc = optab_libfunc (eq_optab, mode);
24824 code = (code = UNEQ) ? EQ : NE;
24825 break;
24827 default:
24828 gcc_unreachable ();
24831 gcc_assert (libfunc);
24833 if (!check_nan)
24834 dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
24835 SImode, op0, mode, op1, mode);
24837 /* The library signals an exception for signalling NaNs, so we need to
24838 handle isgreater, etc. by first checking isordered. */
24839 else
24841 rtx ne_rtx, normal_dest, unord_dest;
24842 rtx unord_func = optab_libfunc (unord_optab, mode);
24843 rtx join_label = gen_label_rtx ();
24844 rtx join_ref = gen_rtx_LABEL_REF (VOIDmode, join_label);
24845 rtx unord_cmp = gen_reg_rtx (comp_mode);
24848 /* Test for either value being a NaN. */
24849 gcc_assert (unord_func);
24850 unord_dest = emit_library_call_value (unord_func, NULL_RTX, LCT_CONST,
24851 SImode, op0, mode, op1, mode);
24853 /* Set value (0) if either value is a NaN, and jump to the join
24854 label. */
24855 dest = gen_reg_rtx (SImode);
24856 emit_move_insn (dest, const1_rtx);
24857 emit_insn (gen_rtx_SET (unord_cmp,
24858 gen_rtx_COMPARE (comp_mode, unord_dest,
24859 const0_rtx)));
24861 ne_rtx = gen_rtx_NE (comp_mode, unord_cmp, const0_rtx);
24862 emit_jump_insn (gen_rtx_SET (pc_rtx,
24863 gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
24864 join_ref,
24865 pc_rtx)));
24867 /* Do the normal comparison, knowing that the values are not
24868 NaNs. */
24869 normal_dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
24870 SImode, op0, mode, op1, mode);
24872 emit_insn (gen_cstoresi4 (dest,
24873 gen_rtx_fmt_ee (code, SImode, normal_dest,
24874 const0_rtx),
24875 normal_dest, const0_rtx));
24877 /* Join NaN and non-Nan paths. Compare dest against 0. */
24878 emit_label (join_label);
24879 code = NE;
24882 emit_insn (gen_rtx_SET (compare_result,
24883 gen_rtx_COMPARE (comp_mode, dest, const0_rtx)));
24886 else
24888 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
24889 CLOBBERs to match cmptf_internal2 pattern. */
24890 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
24891 && FLOAT128_IBM_P (GET_MODE (op0))
24892 && TARGET_HARD_FLOAT && TARGET_FPRS)
24893 emit_insn (gen_rtx_PARALLEL (VOIDmode,
24894 gen_rtvec (10,
24895 gen_rtx_SET (compare_result,
24896 gen_rtx_COMPARE (comp_mode, op0, op1)),
24897 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24898 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24899 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24900 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24901 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24902 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24903 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24904 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24905 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
24906 else if (GET_CODE (op1) == UNSPEC
24907 && XINT (op1, 1) == UNSPEC_SP_TEST)
24909 rtx op1b = XVECEXP (op1, 0, 0);
24910 comp_mode = CCEQmode;
24911 compare_result = gen_reg_rtx (CCEQmode);
24912 if (TARGET_64BIT)
24913 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
24914 else
24915 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
24917 else
24918 emit_insn (gen_rtx_SET (compare_result,
24919 gen_rtx_COMPARE (comp_mode, op0, op1)));
24922 /* Some kinds of FP comparisons need an OR operation;
24923 under flag_finite_math_only we don't bother. */
24924 if (FLOAT_MODE_P (mode)
24925 && (!FLOAT128_IEEE_P (mode) || TARGET_FLOAT128_HW)
24926 && !flag_finite_math_only
24927 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
24928 && (code == LE || code == GE
24929 || code == UNEQ || code == LTGT
24930 || code == UNGT || code == UNLT))
24932 enum rtx_code or1, or2;
24933 rtx or1_rtx, or2_rtx, compare2_rtx;
24934 rtx or_result = gen_reg_rtx (CCEQmode);
24936 switch (code)
24938 case LE: or1 = LT; or2 = EQ; break;
24939 case GE: or1 = GT; or2 = EQ; break;
24940 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
24941 case LTGT: or1 = LT; or2 = GT; break;
24942 case UNGT: or1 = UNORDERED; or2 = GT; break;
24943 case UNLT: or1 = UNORDERED; or2 = LT; break;
24944 default: gcc_unreachable ();
24946 validate_condition_mode (or1, comp_mode);
24947 validate_condition_mode (or2, comp_mode);
24948 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
24949 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
24950 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
24951 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
24952 const_true_rtx);
24953 emit_insn (gen_rtx_SET (or_result, compare2_rtx));
24955 compare_result = or_result;
24956 code = EQ;
24959 validate_condition_mode (code, GET_MODE (compare_result));
24961 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
24965 /* Return the diagnostic message string if the binary operation OP is
24966 not permitted on TYPE1 and TYPE2, NULL otherwise. */
24968 static const char*
24969 rs6000_invalid_binary_op (int op ATTRIBUTE_UNUSED,
24970 const_tree type1,
24971 const_tree type2)
24973 machine_mode mode1 = TYPE_MODE (type1);
24974 machine_mode mode2 = TYPE_MODE (type2);
24976 /* For complex modes, use the inner type. */
24977 if (COMPLEX_MODE_P (mode1))
24978 mode1 = GET_MODE_INNER (mode1);
24980 if (COMPLEX_MODE_P (mode2))
24981 mode2 = GET_MODE_INNER (mode2);
24983 /* Don't allow IEEE 754R 128-bit binary floating point and IBM extended
24984 double to intermix unless -mfloat128-convert. */
24985 if (mode1 == mode2)
24986 return NULL;
24988 if (!TARGET_FLOAT128_CVT)
24990 if ((mode1 == KFmode && mode2 == IFmode)
24991 || (mode1 == IFmode && mode2 == KFmode))
24992 return N_("__float128 and __ibm128 cannot be used in the same "
24993 "expression");
24995 if (TARGET_IEEEQUAD
24996 && ((mode1 == IFmode && mode2 == TFmode)
24997 || (mode1 == TFmode && mode2 == IFmode)))
24998 return N_("__ibm128 and long double cannot be used in the same "
24999 "expression");
25001 if (!TARGET_IEEEQUAD
25002 && ((mode1 == KFmode && mode2 == TFmode)
25003 || (mode1 == TFmode && mode2 == KFmode)))
25004 return N_("__float128 and long double cannot be used in the same "
25005 "expression");
25008 return NULL;
25012 /* Expand floating point conversion to/from __float128 and __ibm128. */
25014 void
25015 rs6000_expand_float128_convert (rtx dest, rtx src, bool unsigned_p)
25017 machine_mode dest_mode = GET_MODE (dest);
25018 machine_mode src_mode = GET_MODE (src);
25019 convert_optab cvt = unknown_optab;
25020 bool do_move = false;
25021 rtx libfunc = NULL_RTX;
25022 rtx dest2;
25023 typedef rtx (*rtx_2func_t) (rtx, rtx);
25024 rtx_2func_t hw_convert = (rtx_2func_t)0;
25025 size_t kf_or_tf;
25027 struct hw_conv_t {
25028 rtx_2func_t from_df;
25029 rtx_2func_t from_sf;
25030 rtx_2func_t from_si_sign;
25031 rtx_2func_t from_si_uns;
25032 rtx_2func_t from_di_sign;
25033 rtx_2func_t from_di_uns;
25034 rtx_2func_t to_df;
25035 rtx_2func_t to_sf;
25036 rtx_2func_t to_si_sign;
25037 rtx_2func_t to_si_uns;
25038 rtx_2func_t to_di_sign;
25039 rtx_2func_t to_di_uns;
25040 } hw_conversions[2] = {
25041 /* convertions to/from KFmode */
25043 gen_extenddfkf2_hw, /* KFmode <- DFmode. */
25044 gen_extendsfkf2_hw, /* KFmode <- SFmode. */
25045 gen_float_kfsi2_hw, /* KFmode <- SImode (signed). */
25046 gen_floatuns_kfsi2_hw, /* KFmode <- SImode (unsigned). */
25047 gen_float_kfdi2_hw, /* KFmode <- DImode (signed). */
25048 gen_floatuns_kfdi2_hw, /* KFmode <- DImode (unsigned). */
25049 gen_trunckfdf2_hw, /* DFmode <- KFmode. */
25050 gen_trunckfsf2_hw, /* SFmode <- KFmode. */
25051 gen_fix_kfsi2_hw, /* SImode <- KFmode (signed). */
25052 gen_fixuns_kfsi2_hw, /* SImode <- KFmode (unsigned). */
25053 gen_fix_kfdi2_hw, /* DImode <- KFmode (signed). */
25054 gen_fixuns_kfdi2_hw, /* DImode <- KFmode (unsigned). */
25057 /* convertions to/from TFmode */
25059 gen_extenddftf2_hw, /* TFmode <- DFmode. */
25060 gen_extendsftf2_hw, /* TFmode <- SFmode. */
25061 gen_float_tfsi2_hw, /* TFmode <- SImode (signed). */
25062 gen_floatuns_tfsi2_hw, /* TFmode <- SImode (unsigned). */
25063 gen_float_tfdi2_hw, /* TFmode <- DImode (signed). */
25064 gen_floatuns_tfdi2_hw, /* TFmode <- DImode (unsigned). */
25065 gen_trunctfdf2_hw, /* DFmode <- TFmode. */
25066 gen_trunctfsf2_hw, /* SFmode <- TFmode. */
25067 gen_fix_tfsi2_hw, /* SImode <- TFmode (signed). */
25068 gen_fixuns_tfsi2_hw, /* SImode <- TFmode (unsigned). */
25069 gen_fix_tfdi2_hw, /* DImode <- TFmode (signed). */
25070 gen_fixuns_tfdi2_hw, /* DImode <- TFmode (unsigned). */
25074 if (dest_mode == src_mode)
25075 gcc_unreachable ();
25077 /* Eliminate memory operations. */
25078 if (MEM_P (src))
25079 src = force_reg (src_mode, src);
25081 if (MEM_P (dest))
25083 rtx tmp = gen_reg_rtx (dest_mode);
25084 rs6000_expand_float128_convert (tmp, src, unsigned_p);
25085 rs6000_emit_move (dest, tmp, dest_mode);
25086 return;
25089 /* Convert to IEEE 128-bit floating point. */
25090 if (FLOAT128_IEEE_P (dest_mode))
25092 if (dest_mode == KFmode)
25093 kf_or_tf = 0;
25094 else if (dest_mode == TFmode)
25095 kf_or_tf = 1;
25096 else
25097 gcc_unreachable ();
25099 switch (src_mode)
25101 case E_DFmode:
25102 cvt = sext_optab;
25103 hw_convert = hw_conversions[kf_or_tf].from_df;
25104 break;
25106 case E_SFmode:
25107 cvt = sext_optab;
25108 hw_convert = hw_conversions[kf_or_tf].from_sf;
25109 break;
25111 case E_KFmode:
25112 case E_IFmode:
25113 case E_TFmode:
25114 if (FLOAT128_IBM_P (src_mode))
25115 cvt = sext_optab;
25116 else
25117 do_move = true;
25118 break;
25120 case E_SImode:
25121 if (unsigned_p)
25123 cvt = ufloat_optab;
25124 hw_convert = hw_conversions[kf_or_tf].from_si_uns;
25126 else
25128 cvt = sfloat_optab;
25129 hw_convert = hw_conversions[kf_or_tf].from_si_sign;
25131 break;
25133 case E_DImode:
25134 if (unsigned_p)
25136 cvt = ufloat_optab;
25137 hw_convert = hw_conversions[kf_or_tf].from_di_uns;
25139 else
25141 cvt = sfloat_optab;
25142 hw_convert = hw_conversions[kf_or_tf].from_di_sign;
25144 break;
25146 default:
25147 gcc_unreachable ();
25151 /* Convert from IEEE 128-bit floating point. */
25152 else if (FLOAT128_IEEE_P (src_mode))
25154 if (src_mode == KFmode)
25155 kf_or_tf = 0;
25156 else if (src_mode == TFmode)
25157 kf_or_tf = 1;
25158 else
25159 gcc_unreachable ();
25161 switch (dest_mode)
25163 case E_DFmode:
25164 cvt = trunc_optab;
25165 hw_convert = hw_conversions[kf_or_tf].to_df;
25166 break;
25168 case E_SFmode:
25169 cvt = trunc_optab;
25170 hw_convert = hw_conversions[kf_or_tf].to_sf;
25171 break;
25173 case E_KFmode:
25174 case E_IFmode:
25175 case E_TFmode:
25176 if (FLOAT128_IBM_P (dest_mode))
25177 cvt = trunc_optab;
25178 else
25179 do_move = true;
25180 break;
25182 case E_SImode:
25183 if (unsigned_p)
25185 cvt = ufix_optab;
25186 hw_convert = hw_conversions[kf_or_tf].to_si_uns;
25188 else
25190 cvt = sfix_optab;
25191 hw_convert = hw_conversions[kf_or_tf].to_si_sign;
25193 break;
25195 case E_DImode:
25196 if (unsigned_p)
25198 cvt = ufix_optab;
25199 hw_convert = hw_conversions[kf_or_tf].to_di_uns;
25201 else
25203 cvt = sfix_optab;
25204 hw_convert = hw_conversions[kf_or_tf].to_di_sign;
25206 break;
25208 default:
25209 gcc_unreachable ();
25213 /* Both IBM format. */
25214 else if (FLOAT128_IBM_P (dest_mode) && FLOAT128_IBM_P (src_mode))
25215 do_move = true;
25217 else
25218 gcc_unreachable ();
25220 /* Handle conversion between TFmode/KFmode. */
25221 if (do_move)
25222 emit_move_insn (dest, gen_lowpart (dest_mode, src));
25224 /* Handle conversion if we have hardware support. */
25225 else if (TARGET_FLOAT128_HW && hw_convert)
25226 emit_insn ((hw_convert) (dest, src));
25228 /* Call an external function to do the conversion. */
25229 else if (cvt != unknown_optab)
25231 libfunc = convert_optab_libfunc (cvt, dest_mode, src_mode);
25232 gcc_assert (libfunc != NULL_RTX);
25234 dest2 = emit_library_call_value (libfunc, dest, LCT_CONST, dest_mode,
25235 src, src_mode);
25237 gcc_assert (dest2 != NULL_RTX);
25238 if (!rtx_equal_p (dest, dest2))
25239 emit_move_insn (dest, dest2);
25242 else
25243 gcc_unreachable ();
25245 return;
25249 /* Emit the RTL for an sISEL pattern. */
25251 void
25252 rs6000_emit_sISEL (machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
25254 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
25257 /* Emit RTL that sets a register to zero if OP1 and OP2 are equal. SCRATCH
25258 can be used as that dest register. Return the dest register. */
25261 rs6000_emit_eqne (machine_mode mode, rtx op1, rtx op2, rtx scratch)
25263 if (op2 == const0_rtx)
25264 return op1;
25266 if (GET_CODE (scratch) == SCRATCH)
25267 scratch = gen_reg_rtx (mode);
25269 if (logical_operand (op2, mode))
25270 emit_insn (gen_rtx_SET (scratch, gen_rtx_XOR (mode, op1, op2)));
25271 else
25272 emit_insn (gen_rtx_SET (scratch,
25273 gen_rtx_PLUS (mode, op1, negate_rtx (mode, op2))));
25275 return scratch;
25278 void
25279 rs6000_emit_sCOND (machine_mode mode, rtx operands[])
25281 rtx condition_rtx;
25282 machine_mode op_mode;
25283 enum rtx_code cond_code;
25284 rtx result = operands[0];
25286 condition_rtx = rs6000_generate_compare (operands[1], mode);
25287 cond_code = GET_CODE (condition_rtx);
25289 if (FLOAT_MODE_P (mode)
25290 && !TARGET_FPRS && TARGET_HARD_FLOAT)
25292 rtx t;
25294 PUT_MODE (condition_rtx, SImode);
25295 t = XEXP (condition_rtx, 0);
25297 gcc_assert (cond_code == NE || cond_code == EQ);
25299 if (cond_code == NE)
25300 emit_insn (gen_e500_flip_gt_bit (t, t));
25302 emit_insn (gen_move_from_CR_gt_bit (result, t));
25303 return;
25306 if (cond_code == NE
25307 || cond_code == GE || cond_code == LE
25308 || cond_code == GEU || cond_code == LEU
25309 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
25311 rtx not_result = gen_reg_rtx (CCEQmode);
25312 rtx not_op, rev_cond_rtx;
25313 machine_mode cc_mode;
25315 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
25317 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
25318 SImode, XEXP (condition_rtx, 0), const0_rtx);
25319 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
25320 emit_insn (gen_rtx_SET (not_result, not_op));
25321 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
25324 op_mode = GET_MODE (XEXP (operands[1], 0));
25325 if (op_mode == VOIDmode)
25326 op_mode = GET_MODE (XEXP (operands[1], 1));
25328 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
25330 PUT_MODE (condition_rtx, DImode);
25331 convert_move (result, condition_rtx, 0);
25333 else
25335 PUT_MODE (condition_rtx, SImode);
25336 emit_insn (gen_rtx_SET (result, condition_rtx));
25340 /* Emit a branch of kind CODE to location LOC. */
25342 void
25343 rs6000_emit_cbranch (machine_mode mode, rtx operands[])
25345 rtx condition_rtx, loc_ref;
25347 condition_rtx = rs6000_generate_compare (operands[0], mode);
25348 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
25349 emit_jump_insn (gen_rtx_SET (pc_rtx,
25350 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
25351 loc_ref, pc_rtx)));
25354 /* Return the string to output a conditional branch to LABEL, which is
25355 the operand template of the label, or NULL if the branch is really a
25356 conditional return.
25358 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
25359 condition code register and its mode specifies what kind of
25360 comparison we made.
25362 REVERSED is nonzero if we should reverse the sense of the comparison.
25364 INSN is the insn. */
25366 char *
25367 output_cbranch (rtx op, const char *label, int reversed, rtx_insn *insn)
25369 static char string[64];
25370 enum rtx_code code = GET_CODE (op);
25371 rtx cc_reg = XEXP (op, 0);
25372 machine_mode mode = GET_MODE (cc_reg);
25373 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
25374 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
25375 int really_reversed = reversed ^ need_longbranch;
25376 char *s = string;
25377 const char *ccode;
25378 const char *pred;
25379 rtx note;
25381 validate_condition_mode (code, mode);
25383 /* Work out which way this really branches. We could use
25384 reverse_condition_maybe_unordered here always but this
25385 makes the resulting assembler clearer. */
25386 if (really_reversed)
25388 /* Reversal of FP compares takes care -- an ordered compare
25389 becomes an unordered compare and vice versa. */
25390 if (mode == CCFPmode)
25391 code = reverse_condition_maybe_unordered (code);
25392 else
25393 code = reverse_condition (code);
25396 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
25398 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
25399 to the GT bit. */
25400 switch (code)
25402 case EQ:
25403 /* Opposite of GT. */
25404 code = GT;
25405 break;
25407 case NE:
25408 code = UNLE;
25409 break;
25411 default:
25412 gcc_unreachable ();
25416 switch (code)
25418 /* Not all of these are actually distinct opcodes, but
25419 we distinguish them for clarity of the resulting assembler. */
25420 case NE: case LTGT:
25421 ccode = "ne"; break;
25422 case EQ: case UNEQ:
25423 ccode = "eq"; break;
25424 case GE: case GEU:
25425 ccode = "ge"; break;
25426 case GT: case GTU: case UNGT:
25427 ccode = "gt"; break;
25428 case LE: case LEU:
25429 ccode = "le"; break;
25430 case LT: case LTU: case UNLT:
25431 ccode = "lt"; break;
25432 case UNORDERED: ccode = "un"; break;
25433 case ORDERED: ccode = "nu"; break;
25434 case UNGE: ccode = "nl"; break;
25435 case UNLE: ccode = "ng"; break;
25436 default:
25437 gcc_unreachable ();
25440 /* Maybe we have a guess as to how likely the branch is. */
25441 pred = "";
25442 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
25443 if (note != NULL_RTX)
25445 /* PROB is the difference from 50%. */
25446 int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
25447 .to_reg_br_prob_base () - REG_BR_PROB_BASE / 2;
25449 /* Only hint for highly probable/improbable branches on newer cpus when
25450 we have real profile data, as static prediction overrides processor
25451 dynamic prediction. For older cpus we may as well always hint, but
25452 assume not taken for branches that are very close to 50% as a
25453 mispredicted taken branch is more expensive than a
25454 mispredicted not-taken branch. */
25455 if (rs6000_always_hint
25456 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
25457 && (profile_status_for_fn (cfun) != PROFILE_GUESSED)
25458 && br_prob_note_reliable_p (note)))
25460 if (abs (prob) > REG_BR_PROB_BASE / 20
25461 && ((prob > 0) ^ need_longbranch))
25462 pred = "+";
25463 else
25464 pred = "-";
25468 if (label == NULL)
25469 s += sprintf (s, "b%slr%s ", ccode, pred);
25470 else
25471 s += sprintf (s, "b%s%s ", ccode, pred);
25473 /* We need to escape any '%' characters in the reg_names string.
25474 Assume they'd only be the first character.... */
25475 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
25476 *s++ = '%';
25477 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
25479 if (label != NULL)
25481 /* If the branch distance was too far, we may have to use an
25482 unconditional branch to go the distance. */
25483 if (need_longbranch)
25484 s += sprintf (s, ",$+8\n\tb %s", label);
25485 else
25486 s += sprintf (s, ",%s", label);
25489 return string;
25492 /* Return the string to flip the GT bit on a CR. */
25493 char *
25494 output_e500_flip_gt_bit (rtx dst, rtx src)
25496 static char string[64];
25497 int a, b;
25499 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
25500 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
25502 /* GT bit. */
25503 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
25504 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
25506 sprintf (string, "crnot %d,%d", a, b);
25507 return string;
25510 /* Return insn for VSX or Altivec comparisons. */
25512 static rtx
25513 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
25515 rtx mask;
25516 machine_mode mode = GET_MODE (op0);
25518 switch (code)
25520 default:
25521 break;
25523 case GE:
25524 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
25525 return NULL_RTX;
25526 /* FALLTHRU */
25528 case EQ:
25529 case GT:
25530 case GTU:
25531 case ORDERED:
25532 case UNORDERED:
25533 case UNEQ:
25534 case LTGT:
25535 mask = gen_reg_rtx (mode);
25536 emit_insn (gen_rtx_SET (mask, gen_rtx_fmt_ee (code, mode, op0, op1)));
25537 return mask;
25540 return NULL_RTX;
25543 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
25544 DMODE is expected destination mode. This is a recursive function. */
25546 static rtx
25547 rs6000_emit_vector_compare (enum rtx_code rcode,
25548 rtx op0, rtx op1,
25549 machine_mode dmode)
25551 rtx mask;
25552 bool swap_operands = false;
25553 bool try_again = false;
25555 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
25556 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
25558 /* See if the comparison works as is. */
25559 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
25560 if (mask)
25561 return mask;
25563 switch (rcode)
25565 case LT:
25566 rcode = GT;
25567 swap_operands = true;
25568 try_again = true;
25569 break;
25570 case LTU:
25571 rcode = GTU;
25572 swap_operands = true;
25573 try_again = true;
25574 break;
25575 case NE:
25576 case UNLE:
25577 case UNLT:
25578 case UNGE:
25579 case UNGT:
25580 /* Invert condition and try again.
25581 e.g., A != B becomes ~(A==B). */
25583 enum rtx_code rev_code;
25584 enum insn_code nor_code;
25585 rtx mask2;
25587 rev_code = reverse_condition_maybe_unordered (rcode);
25588 if (rev_code == UNKNOWN)
25589 return NULL_RTX;
25591 nor_code = optab_handler (one_cmpl_optab, dmode);
25592 if (nor_code == CODE_FOR_nothing)
25593 return NULL_RTX;
25595 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
25596 if (!mask2)
25597 return NULL_RTX;
25599 mask = gen_reg_rtx (dmode);
25600 emit_insn (GEN_FCN (nor_code) (mask, mask2));
25601 return mask;
25603 break;
25604 case GE:
25605 case GEU:
25606 case LE:
25607 case LEU:
25608 /* Try GT/GTU/LT/LTU OR EQ */
25610 rtx c_rtx, eq_rtx;
25611 enum insn_code ior_code;
25612 enum rtx_code new_code;
25614 switch (rcode)
25616 case GE:
25617 new_code = GT;
25618 break;
25620 case GEU:
25621 new_code = GTU;
25622 break;
25624 case LE:
25625 new_code = LT;
25626 break;
25628 case LEU:
25629 new_code = LTU;
25630 break;
25632 default:
25633 gcc_unreachable ();
25636 ior_code = optab_handler (ior_optab, dmode);
25637 if (ior_code == CODE_FOR_nothing)
25638 return NULL_RTX;
25640 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
25641 if (!c_rtx)
25642 return NULL_RTX;
25644 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
25645 if (!eq_rtx)
25646 return NULL_RTX;
25648 mask = gen_reg_rtx (dmode);
25649 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
25650 return mask;
25652 break;
25653 default:
25654 return NULL_RTX;
25657 if (try_again)
25659 if (swap_operands)
25660 std::swap (op0, op1);
25662 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
25663 if (mask)
25664 return mask;
25667 /* You only get two chances. */
25668 return NULL_RTX;
25671 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
25672 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
25673 operands for the relation operation COND. */
25676 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
25677 rtx cond, rtx cc_op0, rtx cc_op1)
25679 machine_mode dest_mode = GET_MODE (dest);
25680 machine_mode mask_mode = GET_MODE (cc_op0);
25681 enum rtx_code rcode = GET_CODE (cond);
25682 machine_mode cc_mode = CCmode;
25683 rtx mask;
25684 rtx cond2;
25685 bool invert_move = false;
25687 if (VECTOR_UNIT_NONE_P (dest_mode))
25688 return 0;
25690 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
25691 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
25693 switch (rcode)
25695 /* Swap operands if we can, and fall back to doing the operation as
25696 specified, and doing a NOR to invert the test. */
25697 case NE:
25698 case UNLE:
25699 case UNLT:
25700 case UNGE:
25701 case UNGT:
25702 /* Invert condition and try again.
25703 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
25704 invert_move = true;
25705 rcode = reverse_condition_maybe_unordered (rcode);
25706 if (rcode == UNKNOWN)
25707 return 0;
25708 break;
25710 case GE:
25711 case LE:
25712 if (GET_MODE_CLASS (mask_mode) == MODE_VECTOR_INT)
25714 /* Invert condition to avoid compound test. */
25715 invert_move = true;
25716 rcode = reverse_condition (rcode);
25718 break;
25720 case GTU:
25721 case GEU:
25722 case LTU:
25723 case LEU:
25724 /* Mark unsigned tests with CCUNSmode. */
25725 cc_mode = CCUNSmode;
25727 /* Invert condition to avoid compound test if necessary. */
25728 if (rcode == GEU || rcode == LEU)
25730 invert_move = true;
25731 rcode = reverse_condition (rcode);
25733 break;
25735 default:
25736 break;
25739 /* Get the vector mask for the given relational operations. */
25740 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
25742 if (!mask)
25743 return 0;
25745 if (invert_move)
25746 std::swap (op_true, op_false);
25748 /* Optimize vec1 == vec2, to know the mask generates -1/0. */
25749 if (GET_MODE_CLASS (dest_mode) == MODE_VECTOR_INT
25750 && (GET_CODE (op_true) == CONST_VECTOR
25751 || GET_CODE (op_false) == CONST_VECTOR))
25753 rtx constant_0 = CONST0_RTX (dest_mode);
25754 rtx constant_m1 = CONSTM1_RTX (dest_mode);
25756 if (op_true == constant_m1 && op_false == constant_0)
25758 emit_move_insn (dest, mask);
25759 return 1;
25762 else if (op_true == constant_0 && op_false == constant_m1)
25764 emit_insn (gen_rtx_SET (dest, gen_rtx_NOT (dest_mode, mask)));
25765 return 1;
25768 /* If we can't use the vector comparison directly, perhaps we can use
25769 the mask for the true or false fields, instead of loading up a
25770 constant. */
25771 if (op_true == constant_m1)
25772 op_true = mask;
25774 if (op_false == constant_0)
25775 op_false = mask;
25778 if (!REG_P (op_true) && !SUBREG_P (op_true))
25779 op_true = force_reg (dest_mode, op_true);
25781 if (!REG_P (op_false) && !SUBREG_P (op_false))
25782 op_false = force_reg (dest_mode, op_false);
25784 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
25785 CONST0_RTX (dest_mode));
25786 emit_insn (gen_rtx_SET (dest,
25787 gen_rtx_IF_THEN_ELSE (dest_mode,
25788 cond2,
25789 op_true,
25790 op_false)));
25791 return 1;
25794 /* ISA 3.0 (power9) minmax subcase to emit a XSMAXCDP or XSMINCDP instruction
25795 for SF/DF scalars. Move TRUE_COND to DEST if OP of the operands of the last
25796 comparison is nonzero/true, FALSE_COND if it is zero/false. Return 0 if the
25797 hardware has no such operation. */
25799 static int
25800 rs6000_emit_p9_fp_minmax (rtx dest, rtx op, rtx true_cond, rtx false_cond)
25802 enum rtx_code code = GET_CODE (op);
25803 rtx op0 = XEXP (op, 0);
25804 rtx op1 = XEXP (op, 1);
25805 machine_mode compare_mode = GET_MODE (op0);
25806 machine_mode result_mode = GET_MODE (dest);
25807 bool max_p = false;
25809 if (result_mode != compare_mode)
25810 return 0;
25812 if (code == GE || code == GT)
25813 max_p = true;
25814 else if (code == LE || code == LT)
25815 max_p = false;
25816 else
25817 return 0;
25819 if (rtx_equal_p (op0, true_cond) && rtx_equal_p (op1, false_cond))
25822 else if (rtx_equal_p (op1, true_cond) && rtx_equal_p (op0, false_cond))
25823 max_p = !max_p;
25825 else
25826 return 0;
25828 rs6000_emit_minmax (dest, max_p ? SMAX : SMIN, op0, op1);
25829 return 1;
25832 /* ISA 3.0 (power9) conditional move subcase to emit XSCMP{EQ,GE,GT,NE}DP and
25833 XXSEL instructions for SF/DF scalars. Move TRUE_COND to DEST if OP of the
25834 operands of the last comparison is nonzero/true, FALSE_COND if it is
25835 zero/false. Return 0 if the hardware has no such operation. */
25837 static int
25838 rs6000_emit_p9_fp_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
25840 enum rtx_code code = GET_CODE (op);
25841 rtx op0 = XEXP (op, 0);
25842 rtx op1 = XEXP (op, 1);
25843 machine_mode result_mode = GET_MODE (dest);
25844 rtx compare_rtx;
25845 rtx cmove_rtx;
25846 rtx clobber_rtx;
25848 if (!can_create_pseudo_p ())
25849 return 0;
25851 switch (code)
25853 case EQ:
25854 case GE:
25855 case GT:
25856 break;
25858 case NE:
25859 case LT:
25860 case LE:
25861 code = swap_condition (code);
25862 std::swap (op0, op1);
25863 break;
25865 default:
25866 return 0;
25869 /* Generate: [(parallel [(set (dest)
25870 (if_then_else (op (cmp1) (cmp2))
25871 (true)
25872 (false)))
25873 (clobber (scratch))])]. */
25875 compare_rtx = gen_rtx_fmt_ee (code, CCFPmode, op0, op1);
25876 cmove_rtx = gen_rtx_SET (dest,
25877 gen_rtx_IF_THEN_ELSE (result_mode,
25878 compare_rtx,
25879 true_cond,
25880 false_cond));
25882 clobber_rtx = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (V2DImode));
25883 emit_insn (gen_rtx_PARALLEL (VOIDmode,
25884 gen_rtvec (2, cmove_rtx, clobber_rtx)));
25886 return 1;
25889 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
25890 operands of the last comparison is nonzero/true, FALSE_COND if it
25891 is zero/false. Return 0 if the hardware has no such operation. */
25894 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
25896 enum rtx_code code = GET_CODE (op);
25897 rtx op0 = XEXP (op, 0);
25898 rtx op1 = XEXP (op, 1);
25899 machine_mode compare_mode = GET_MODE (op0);
25900 machine_mode result_mode = GET_MODE (dest);
25901 rtx temp;
25902 bool is_against_zero;
25904 /* These modes should always match. */
25905 if (GET_MODE (op1) != compare_mode
25906 /* In the isel case however, we can use a compare immediate, so
25907 op1 may be a small constant. */
25908 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
25909 return 0;
25910 if (GET_MODE (true_cond) != result_mode)
25911 return 0;
25912 if (GET_MODE (false_cond) != result_mode)
25913 return 0;
25915 /* See if we can use the ISA 3.0 (power9) min/max/compare functions. */
25916 if (TARGET_P9_MINMAX
25917 && (compare_mode == SFmode || compare_mode == DFmode)
25918 && (result_mode == SFmode || result_mode == DFmode))
25920 if (rs6000_emit_p9_fp_minmax (dest, op, true_cond, false_cond))
25921 return 1;
25923 if (rs6000_emit_p9_fp_cmove (dest, op, true_cond, false_cond))
25924 return 1;
25927 /* Don't allow using floating point comparisons for integer results for
25928 now. */
25929 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
25930 return 0;
25932 /* First, work out if the hardware can do this at all, or
25933 if it's too slow.... */
25934 if (!FLOAT_MODE_P (compare_mode))
25936 if (TARGET_ISEL)
25937 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
25938 return 0;
25940 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
25941 && SCALAR_FLOAT_MODE_P (compare_mode))
25942 return 0;
25944 is_against_zero = op1 == CONST0_RTX (compare_mode);
25946 /* A floating-point subtract might overflow, underflow, or produce
25947 an inexact result, thus changing the floating-point flags, so it
25948 can't be generated if we care about that. It's safe if one side
25949 of the construct is zero, since then no subtract will be
25950 generated. */
25951 if (SCALAR_FLOAT_MODE_P (compare_mode)
25952 && flag_trapping_math && ! is_against_zero)
25953 return 0;
25955 /* Eliminate half of the comparisons by switching operands, this
25956 makes the remaining code simpler. */
25957 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
25958 || code == LTGT || code == LT || code == UNLE)
25960 code = reverse_condition_maybe_unordered (code);
25961 temp = true_cond;
25962 true_cond = false_cond;
25963 false_cond = temp;
25966 /* UNEQ and LTGT take four instructions for a comparison with zero,
25967 it'll probably be faster to use a branch here too. */
25968 if (code == UNEQ && HONOR_NANS (compare_mode))
25969 return 0;
25971 /* We're going to try to implement comparisons by performing
25972 a subtract, then comparing against zero. Unfortunately,
25973 Inf - Inf is NaN which is not zero, and so if we don't
25974 know that the operand is finite and the comparison
25975 would treat EQ different to UNORDERED, we can't do it. */
25976 if (HONOR_INFINITIES (compare_mode)
25977 && code != GT && code != UNGE
25978 && (GET_CODE (op1) != CONST_DOUBLE
25979 || real_isinf (CONST_DOUBLE_REAL_VALUE (op1)))
25980 /* Constructs of the form (a OP b ? a : b) are safe. */
25981 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
25982 || (! rtx_equal_p (op0, true_cond)
25983 && ! rtx_equal_p (op1, true_cond))))
25984 return 0;
25986 /* At this point we know we can use fsel. */
25988 /* Reduce the comparison to a comparison against zero. */
25989 if (! is_against_zero)
25991 temp = gen_reg_rtx (compare_mode);
25992 emit_insn (gen_rtx_SET (temp, gen_rtx_MINUS (compare_mode, op0, op1)));
25993 op0 = temp;
25994 op1 = CONST0_RTX (compare_mode);
25997 /* If we don't care about NaNs we can reduce some of the comparisons
25998 down to faster ones. */
25999 if (! HONOR_NANS (compare_mode))
26000 switch (code)
26002 case GT:
26003 code = LE;
26004 temp = true_cond;
26005 true_cond = false_cond;
26006 false_cond = temp;
26007 break;
26008 case UNGE:
26009 code = GE;
26010 break;
26011 case UNEQ:
26012 code = EQ;
26013 break;
26014 default:
26015 break;
26018 /* Now, reduce everything down to a GE. */
26019 switch (code)
26021 case GE:
26022 break;
26024 case LE:
26025 temp = gen_reg_rtx (compare_mode);
26026 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
26027 op0 = temp;
26028 break;
26030 case ORDERED:
26031 temp = gen_reg_rtx (compare_mode);
26032 emit_insn (gen_rtx_SET (temp, gen_rtx_ABS (compare_mode, op0)));
26033 op0 = temp;
26034 break;
26036 case EQ:
26037 temp = gen_reg_rtx (compare_mode);
26038 emit_insn (gen_rtx_SET (temp,
26039 gen_rtx_NEG (compare_mode,
26040 gen_rtx_ABS (compare_mode, op0))));
26041 op0 = temp;
26042 break;
26044 case UNGE:
26045 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
26046 temp = gen_reg_rtx (result_mode);
26047 emit_insn (gen_rtx_SET (temp,
26048 gen_rtx_IF_THEN_ELSE (result_mode,
26049 gen_rtx_GE (VOIDmode,
26050 op0, op1),
26051 true_cond, false_cond)));
26052 false_cond = true_cond;
26053 true_cond = temp;
26055 temp = gen_reg_rtx (compare_mode);
26056 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
26057 op0 = temp;
26058 break;
26060 case GT:
26061 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
26062 temp = gen_reg_rtx (result_mode);
26063 emit_insn (gen_rtx_SET (temp,
26064 gen_rtx_IF_THEN_ELSE (result_mode,
26065 gen_rtx_GE (VOIDmode,
26066 op0, op1),
26067 true_cond, false_cond)));
26068 true_cond = false_cond;
26069 false_cond = temp;
26071 temp = gen_reg_rtx (compare_mode);
26072 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
26073 op0 = temp;
26074 break;
26076 default:
26077 gcc_unreachable ();
26080 emit_insn (gen_rtx_SET (dest,
26081 gen_rtx_IF_THEN_ELSE (result_mode,
26082 gen_rtx_GE (VOIDmode,
26083 op0, op1),
26084 true_cond, false_cond)));
26085 return 1;
26088 /* Same as above, but for ints (isel). */
26090 static int
26091 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
26093 rtx condition_rtx, cr;
26094 machine_mode mode = GET_MODE (dest);
26095 enum rtx_code cond_code;
26096 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
26097 bool signedp;
26099 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
26100 return 0;
26102 /* We still have to do the compare, because isel doesn't do a
26103 compare, it just looks at the CRx bits set by a previous compare
26104 instruction. */
26105 condition_rtx = rs6000_generate_compare (op, mode);
26106 cond_code = GET_CODE (condition_rtx);
26107 cr = XEXP (condition_rtx, 0);
26108 signedp = GET_MODE (cr) == CCmode;
26110 isel_func = (mode == SImode
26111 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
26112 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
26114 switch (cond_code)
26116 case LT: case GT: case LTU: case GTU: case EQ:
26117 /* isel handles these directly. */
26118 break;
26120 default:
26121 /* We need to swap the sense of the comparison. */
26123 std::swap (false_cond, true_cond);
26124 PUT_CODE (condition_rtx, reverse_condition (cond_code));
26126 break;
26129 false_cond = force_reg (mode, false_cond);
26130 if (true_cond != const0_rtx)
26131 true_cond = force_reg (mode, true_cond);
26133 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
26135 return 1;
26138 const char *
26139 output_isel (rtx *operands)
26141 enum rtx_code code;
26143 code = GET_CODE (operands[1]);
26145 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
26147 gcc_assert (GET_CODE (operands[2]) == REG
26148 && GET_CODE (operands[3]) == REG);
26149 PUT_CODE (operands[1], reverse_condition (code));
26150 return "isel %0,%3,%2,%j1";
26153 return "isel %0,%2,%3,%j1";
26156 void
26157 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
26159 machine_mode mode = GET_MODE (op0);
26160 enum rtx_code c;
26161 rtx target;
26163 /* VSX/altivec have direct min/max insns. */
26164 if ((code == SMAX || code == SMIN)
26165 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
26166 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
26168 emit_insn (gen_rtx_SET (dest, gen_rtx_fmt_ee (code, mode, op0, op1)));
26169 return;
26172 if (code == SMAX || code == SMIN)
26173 c = GE;
26174 else
26175 c = GEU;
26177 if (code == SMAX || code == UMAX)
26178 target = emit_conditional_move (dest, c, op0, op1, mode,
26179 op0, op1, mode, 0);
26180 else
26181 target = emit_conditional_move (dest, c, op0, op1, mode,
26182 op1, op0, mode, 0);
26183 gcc_assert (target);
26184 if (target != dest)
26185 emit_move_insn (dest, target);
26188 /* Split a signbit operation on 64-bit machines with direct move. Also allow
26189 for the value to come from memory or if it is already loaded into a GPR. */
26191 void
26192 rs6000_split_signbit (rtx dest, rtx src)
26194 machine_mode d_mode = GET_MODE (dest);
26195 machine_mode s_mode = GET_MODE (src);
26196 rtx dest_di = (d_mode == DImode) ? dest : gen_lowpart (DImode, dest);
26197 rtx shift_reg = dest_di;
26199 gcc_assert (FLOAT128_IEEE_P (s_mode) && TARGET_POWERPC64);
26201 if (MEM_P (src))
26203 rtx mem = (WORDS_BIG_ENDIAN
26204 ? adjust_address (src, DImode, 0)
26205 : adjust_address (src, DImode, 8));
26206 emit_insn (gen_rtx_SET (dest_di, mem));
26209 else
26211 unsigned int r = reg_or_subregno (src);
26213 if (INT_REGNO_P (r))
26214 shift_reg = gen_rtx_REG (DImode, r + (BYTES_BIG_ENDIAN == 0));
26216 else
26218 /* Generate the special mfvsrd instruction to get it in a GPR. */
26219 gcc_assert (VSX_REGNO_P (r));
26220 if (s_mode == KFmode)
26221 emit_insn (gen_signbitkf2_dm2 (dest_di, src));
26222 else
26223 emit_insn (gen_signbittf2_dm2 (dest_di, src));
26227 emit_insn (gen_lshrdi3 (dest_di, shift_reg, GEN_INT (63)));
26228 return;
26231 /* A subroutine of the atomic operation splitters. Jump to LABEL if
26232 COND is true. Mark the jump as unlikely to be taken. */
26234 static void
26235 emit_unlikely_jump (rtx cond, rtx label)
26237 rtx x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
26238 rtx_insn *insn = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
26239 add_reg_br_prob_note (insn, profile_probability::very_unlikely ());
26242 /* A subroutine of the atomic operation splitters. Emit a load-locked
26243 instruction in MODE. For QI/HImode, possibly use a pattern than includes
26244 the zero_extend operation. */
26246 static void
26247 emit_load_locked (machine_mode mode, rtx reg, rtx mem)
26249 rtx (*fn) (rtx, rtx) = NULL;
26251 switch (mode)
26253 case E_QImode:
26254 fn = gen_load_lockedqi;
26255 break;
26256 case E_HImode:
26257 fn = gen_load_lockedhi;
26258 break;
26259 case E_SImode:
26260 if (GET_MODE (mem) == QImode)
26261 fn = gen_load_lockedqi_si;
26262 else if (GET_MODE (mem) == HImode)
26263 fn = gen_load_lockedhi_si;
26264 else
26265 fn = gen_load_lockedsi;
26266 break;
26267 case E_DImode:
26268 fn = gen_load_lockeddi;
26269 break;
26270 case E_TImode:
26271 fn = gen_load_lockedti;
26272 break;
26273 default:
26274 gcc_unreachable ();
26276 emit_insn (fn (reg, mem));
26279 /* A subroutine of the atomic operation splitters. Emit a store-conditional
26280 instruction in MODE. */
26282 static void
26283 emit_store_conditional (machine_mode mode, rtx res, rtx mem, rtx val)
26285 rtx (*fn) (rtx, rtx, rtx) = NULL;
26287 switch (mode)
26289 case E_QImode:
26290 fn = gen_store_conditionalqi;
26291 break;
26292 case E_HImode:
26293 fn = gen_store_conditionalhi;
26294 break;
26295 case E_SImode:
26296 fn = gen_store_conditionalsi;
26297 break;
26298 case E_DImode:
26299 fn = gen_store_conditionaldi;
26300 break;
26301 case E_TImode:
26302 fn = gen_store_conditionalti;
26303 break;
26304 default:
26305 gcc_unreachable ();
26308 /* Emit sync before stwcx. to address PPC405 Erratum. */
26309 if (PPC405_ERRATUM77)
26310 emit_insn (gen_hwsync ());
26312 emit_insn (fn (res, mem, val));
26315 /* Expand barriers before and after a load_locked/store_cond sequence. */
26317 static rtx
26318 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
26320 rtx addr = XEXP (mem, 0);
26321 int strict_p = (reload_in_progress || reload_completed);
26323 if (!legitimate_indirect_address_p (addr, strict_p)
26324 && !legitimate_indexed_address_p (addr, strict_p))
26326 addr = force_reg (Pmode, addr);
26327 mem = replace_equiv_address_nv (mem, addr);
26330 switch (model)
26332 case MEMMODEL_RELAXED:
26333 case MEMMODEL_CONSUME:
26334 case MEMMODEL_ACQUIRE:
26335 break;
26336 case MEMMODEL_RELEASE:
26337 case MEMMODEL_ACQ_REL:
26338 emit_insn (gen_lwsync ());
26339 break;
26340 case MEMMODEL_SEQ_CST:
26341 emit_insn (gen_hwsync ());
26342 break;
26343 default:
26344 gcc_unreachable ();
26346 return mem;
26349 static void
26350 rs6000_post_atomic_barrier (enum memmodel model)
26352 switch (model)
26354 case MEMMODEL_RELAXED:
26355 case MEMMODEL_CONSUME:
26356 case MEMMODEL_RELEASE:
26357 break;
26358 case MEMMODEL_ACQUIRE:
26359 case MEMMODEL_ACQ_REL:
26360 case MEMMODEL_SEQ_CST:
26361 emit_insn (gen_isync ());
26362 break;
26363 default:
26364 gcc_unreachable ();
26368 /* A subroutine of the various atomic expanders. For sub-word operations,
26369 we must adjust things to operate on SImode. Given the original MEM,
26370 return a new aligned memory. Also build and return the quantities by
26371 which to shift and mask. */
26373 static rtx
26374 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
26376 rtx addr, align, shift, mask, mem;
26377 HOST_WIDE_INT shift_mask;
26378 machine_mode mode = GET_MODE (orig_mem);
26380 /* For smaller modes, we have to implement this via SImode. */
26381 shift_mask = (mode == QImode ? 0x18 : 0x10);
26383 addr = XEXP (orig_mem, 0);
26384 addr = force_reg (GET_MODE (addr), addr);
26386 /* Aligned memory containing subword. Generate a new memory. We
26387 do not want any of the existing MEM_ATTR data, as we're now
26388 accessing memory outside the original object. */
26389 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
26390 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26391 mem = gen_rtx_MEM (SImode, align);
26392 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
26393 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
26394 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
26396 /* Shift amount for subword relative to aligned word. */
26397 shift = gen_reg_rtx (SImode);
26398 addr = gen_lowpart (SImode, addr);
26399 rtx tmp = gen_reg_rtx (SImode);
26400 emit_insn (gen_ashlsi3 (tmp, addr, GEN_INT (3)));
26401 emit_insn (gen_andsi3 (shift, tmp, GEN_INT (shift_mask)));
26402 if (BYTES_BIG_ENDIAN)
26403 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
26404 shift, 1, OPTAB_LIB_WIDEN);
26405 *pshift = shift;
26407 /* Mask for insertion. */
26408 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
26409 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
26410 *pmask = mask;
26412 return mem;
26415 /* A subroutine of the various atomic expanders. For sub-word operands,
26416 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
26418 static rtx
26419 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
26421 rtx x;
26423 x = gen_reg_rtx (SImode);
26424 emit_insn (gen_rtx_SET (x, gen_rtx_AND (SImode,
26425 gen_rtx_NOT (SImode, mask),
26426 oldval)));
26428 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
26430 return x;
26433 /* A subroutine of the various atomic expanders. For sub-word operands,
26434 extract WIDE to NARROW via SHIFT. */
26436 static void
26437 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
26439 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
26440 wide, 1, OPTAB_LIB_WIDEN);
26441 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
26444 /* Expand an atomic compare and swap operation. */
26446 void
26447 rs6000_expand_atomic_compare_and_swap (rtx operands[])
26449 rtx boolval, retval, mem, oldval, newval, cond;
26450 rtx label1, label2, x, mask, shift;
26451 machine_mode mode, orig_mode;
26452 enum memmodel mod_s, mod_f;
26453 bool is_weak;
26455 boolval = operands[0];
26456 retval = operands[1];
26457 mem = operands[2];
26458 oldval = operands[3];
26459 newval = operands[4];
26460 is_weak = (INTVAL (operands[5]) != 0);
26461 mod_s = memmodel_base (INTVAL (operands[6]));
26462 mod_f = memmodel_base (INTVAL (operands[7]));
26463 orig_mode = mode = GET_MODE (mem);
26465 mask = shift = NULL_RTX;
26466 if (mode == QImode || mode == HImode)
26468 /* Before power8, we didn't have access to lbarx/lharx, so generate a
26469 lwarx and shift/mask operations. With power8, we need to do the
26470 comparison in SImode, but the store is still done in QI/HImode. */
26471 oldval = convert_modes (SImode, mode, oldval, 1);
26473 if (!TARGET_SYNC_HI_QI)
26475 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
26477 /* Shift and mask OLDVAL into position with the word. */
26478 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
26479 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26481 /* Shift and mask NEWVAL into position within the word. */
26482 newval = convert_modes (SImode, mode, newval, 1);
26483 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
26484 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26487 /* Prepare to adjust the return value. */
26488 retval = gen_reg_rtx (SImode);
26489 mode = SImode;
26491 else if (reg_overlap_mentioned_p (retval, oldval))
26492 oldval = copy_to_reg (oldval);
26494 if (mode != TImode && !reg_or_short_operand (oldval, mode))
26495 oldval = copy_to_mode_reg (mode, oldval);
26497 if (reg_overlap_mentioned_p (retval, newval))
26498 newval = copy_to_reg (newval);
26500 mem = rs6000_pre_atomic_barrier (mem, mod_s);
26502 label1 = NULL_RTX;
26503 if (!is_weak)
26505 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
26506 emit_label (XEXP (label1, 0));
26508 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
26510 emit_load_locked (mode, retval, mem);
26512 x = retval;
26513 if (mask)
26514 x = expand_simple_binop (SImode, AND, retval, mask,
26515 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26517 cond = gen_reg_rtx (CCmode);
26518 /* If we have TImode, synthesize a comparison. */
26519 if (mode != TImode)
26520 x = gen_rtx_COMPARE (CCmode, x, oldval);
26521 else
26523 rtx xor1_result = gen_reg_rtx (DImode);
26524 rtx xor2_result = gen_reg_rtx (DImode);
26525 rtx or_result = gen_reg_rtx (DImode);
26526 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
26527 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
26528 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
26529 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
26531 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
26532 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
26533 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
26534 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
26537 emit_insn (gen_rtx_SET (cond, x));
26539 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26540 emit_unlikely_jump (x, label2);
26542 x = newval;
26543 if (mask)
26544 x = rs6000_mask_atomic_subword (retval, newval, mask);
26546 emit_store_conditional (orig_mode, cond, mem, x);
26548 if (!is_weak)
26550 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26551 emit_unlikely_jump (x, label1);
26554 if (!is_mm_relaxed (mod_f))
26555 emit_label (XEXP (label2, 0));
26557 rs6000_post_atomic_barrier (mod_s);
26559 if (is_mm_relaxed (mod_f))
26560 emit_label (XEXP (label2, 0));
26562 if (shift)
26563 rs6000_finish_atomic_subword (operands[1], retval, shift);
26564 else if (mode != GET_MODE (operands[1]))
26565 convert_move (operands[1], retval, 1);
26567 /* In all cases, CR0 contains EQ on success, and NE on failure. */
26568 x = gen_rtx_EQ (SImode, cond, const0_rtx);
26569 emit_insn (gen_rtx_SET (boolval, x));
26572 /* Expand an atomic exchange operation. */
26574 void
26575 rs6000_expand_atomic_exchange (rtx operands[])
26577 rtx retval, mem, val, cond;
26578 machine_mode mode;
26579 enum memmodel model;
26580 rtx label, x, mask, shift;
26582 retval = operands[0];
26583 mem = operands[1];
26584 val = operands[2];
26585 model = memmodel_base (INTVAL (operands[3]));
26586 mode = GET_MODE (mem);
26588 mask = shift = NULL_RTX;
26589 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
26591 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
26593 /* Shift and mask VAL into position with the word. */
26594 val = convert_modes (SImode, mode, val, 1);
26595 val = expand_simple_binop (SImode, ASHIFT, val, shift,
26596 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26598 /* Prepare to adjust the return value. */
26599 retval = gen_reg_rtx (SImode);
26600 mode = SImode;
26603 mem = rs6000_pre_atomic_barrier (mem, model);
26605 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
26606 emit_label (XEXP (label, 0));
26608 emit_load_locked (mode, retval, mem);
26610 x = val;
26611 if (mask)
26612 x = rs6000_mask_atomic_subword (retval, val, mask);
26614 cond = gen_reg_rtx (CCmode);
26615 emit_store_conditional (mode, cond, mem, x);
26617 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26618 emit_unlikely_jump (x, label);
26620 rs6000_post_atomic_barrier (model);
26622 if (shift)
26623 rs6000_finish_atomic_subword (operands[0], retval, shift);
26626 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
26627 to perform. MEM is the memory on which to operate. VAL is the second
26628 operand of the binary operator. BEFORE and AFTER are optional locations to
26629 return the value of MEM either before of after the operation. MODEL_RTX
26630 is a CONST_INT containing the memory model to use. */
26632 void
26633 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
26634 rtx orig_before, rtx orig_after, rtx model_rtx)
26636 enum memmodel model = memmodel_base (INTVAL (model_rtx));
26637 machine_mode mode = GET_MODE (mem);
26638 machine_mode store_mode = mode;
26639 rtx label, x, cond, mask, shift;
26640 rtx before = orig_before, after = orig_after;
26642 mask = shift = NULL_RTX;
26643 /* On power8, we want to use SImode for the operation. On previous systems,
26644 use the operation in a subword and shift/mask to get the proper byte or
26645 halfword. */
26646 if (mode == QImode || mode == HImode)
26648 if (TARGET_SYNC_HI_QI)
26650 val = convert_modes (SImode, mode, val, 1);
26652 /* Prepare to adjust the return value. */
26653 before = gen_reg_rtx (SImode);
26654 if (after)
26655 after = gen_reg_rtx (SImode);
26656 mode = SImode;
26658 else
26660 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
26662 /* Shift and mask VAL into position with the word. */
26663 val = convert_modes (SImode, mode, val, 1);
26664 val = expand_simple_binop (SImode, ASHIFT, val, shift,
26665 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26667 switch (code)
26669 case IOR:
26670 case XOR:
26671 /* We've already zero-extended VAL. That is sufficient to
26672 make certain that it does not affect other bits. */
26673 mask = NULL;
26674 break;
26676 case AND:
26677 /* If we make certain that all of the other bits in VAL are
26678 set, that will be sufficient to not affect other bits. */
26679 x = gen_rtx_NOT (SImode, mask);
26680 x = gen_rtx_IOR (SImode, x, val);
26681 emit_insn (gen_rtx_SET (val, x));
26682 mask = NULL;
26683 break;
26685 case NOT:
26686 case PLUS:
26687 case MINUS:
26688 /* These will all affect bits outside the field and need
26689 adjustment via MASK within the loop. */
26690 break;
26692 default:
26693 gcc_unreachable ();
26696 /* Prepare to adjust the return value. */
26697 before = gen_reg_rtx (SImode);
26698 if (after)
26699 after = gen_reg_rtx (SImode);
26700 store_mode = mode = SImode;
26704 mem = rs6000_pre_atomic_barrier (mem, model);
26706 label = gen_label_rtx ();
26707 emit_label (label);
26708 label = gen_rtx_LABEL_REF (VOIDmode, label);
26710 if (before == NULL_RTX)
26711 before = gen_reg_rtx (mode);
26713 emit_load_locked (mode, before, mem);
26715 if (code == NOT)
26717 x = expand_simple_binop (mode, AND, before, val,
26718 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26719 after = expand_simple_unop (mode, NOT, x, after, 1);
26721 else
26723 after = expand_simple_binop (mode, code, before, val,
26724 after, 1, OPTAB_LIB_WIDEN);
26727 x = after;
26728 if (mask)
26730 x = expand_simple_binop (SImode, AND, after, mask,
26731 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26732 x = rs6000_mask_atomic_subword (before, x, mask);
26734 else if (store_mode != mode)
26735 x = convert_modes (store_mode, mode, x, 1);
26737 cond = gen_reg_rtx (CCmode);
26738 emit_store_conditional (store_mode, cond, mem, x);
26740 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26741 emit_unlikely_jump (x, label);
26743 rs6000_post_atomic_barrier (model);
26745 if (shift)
26747 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
26748 then do the calcuations in a SImode register. */
26749 if (orig_before)
26750 rs6000_finish_atomic_subword (orig_before, before, shift);
26751 if (orig_after)
26752 rs6000_finish_atomic_subword (orig_after, after, shift);
26754 else if (store_mode != mode)
26756 /* QImode/HImode on machines with lbarx/lharx where we do the native
26757 operation and then do the calcuations in a SImode register. */
26758 if (orig_before)
26759 convert_move (orig_before, before, 1);
26760 if (orig_after)
26761 convert_move (orig_after, after, 1);
26763 else if (orig_after && after != orig_after)
26764 emit_move_insn (orig_after, after);
26767 /* Emit instructions to move SRC to DST. Called by splitters for
26768 multi-register moves. It will emit at most one instruction for
26769 each register that is accessed; that is, it won't emit li/lis pairs
26770 (or equivalent for 64-bit code). One of SRC or DST must be a hard
26771 register. */
26773 void
26774 rs6000_split_multireg_move (rtx dst, rtx src)
26776 /* The register number of the first register being moved. */
26777 int reg;
26778 /* The mode that is to be moved. */
26779 machine_mode mode;
26780 /* The mode that the move is being done in, and its size. */
26781 machine_mode reg_mode;
26782 int reg_mode_size;
26783 /* The number of registers that will be moved. */
26784 int nregs;
26786 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
26787 mode = GET_MODE (dst);
26788 nregs = hard_regno_nregs (reg, mode);
26789 if (FP_REGNO_P (reg))
26790 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
26791 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
26792 else if (ALTIVEC_REGNO_P (reg))
26793 reg_mode = V16QImode;
26794 else if (TARGET_E500_DOUBLE && FLOAT128_2REG_P (mode))
26795 reg_mode = DFmode;
26796 else
26797 reg_mode = word_mode;
26798 reg_mode_size = GET_MODE_SIZE (reg_mode);
26800 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
26802 /* TDmode residing in FP registers is special, since the ISA requires that
26803 the lower-numbered word of a register pair is always the most significant
26804 word, even in little-endian mode. This does not match the usual subreg
26805 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
26806 the appropriate constituent registers "by hand" in little-endian mode.
26808 Note we do not need to check for destructive overlap here since TDmode
26809 can only reside in even/odd register pairs. */
26810 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
26812 rtx p_src, p_dst;
26813 int i;
26815 for (i = 0; i < nregs; i++)
26817 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
26818 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
26819 else
26820 p_src = simplify_gen_subreg (reg_mode, src, mode,
26821 i * reg_mode_size);
26823 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
26824 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
26825 else
26826 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
26827 i * reg_mode_size);
26829 emit_insn (gen_rtx_SET (p_dst, p_src));
26832 return;
26835 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
26837 /* Move register range backwards, if we might have destructive
26838 overlap. */
26839 int i;
26840 for (i = nregs - 1; i >= 0; i--)
26841 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
26842 i * reg_mode_size),
26843 simplify_gen_subreg (reg_mode, src, mode,
26844 i * reg_mode_size)));
26846 else
26848 int i;
26849 int j = -1;
26850 bool used_update = false;
26851 rtx restore_basereg = NULL_RTX;
26853 if (MEM_P (src) && INT_REGNO_P (reg))
26855 rtx breg;
26857 if (GET_CODE (XEXP (src, 0)) == PRE_INC
26858 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
26860 rtx delta_rtx;
26861 breg = XEXP (XEXP (src, 0), 0);
26862 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
26863 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
26864 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
26865 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
26866 src = replace_equiv_address (src, breg);
26868 else if (! rs6000_offsettable_memref_p (src, reg_mode))
26870 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
26872 rtx basereg = XEXP (XEXP (src, 0), 0);
26873 if (TARGET_UPDATE)
26875 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
26876 emit_insn (gen_rtx_SET (ndst,
26877 gen_rtx_MEM (reg_mode,
26878 XEXP (src, 0))));
26879 used_update = true;
26881 else
26882 emit_insn (gen_rtx_SET (basereg,
26883 XEXP (XEXP (src, 0), 1)));
26884 src = replace_equiv_address (src, basereg);
26886 else
26888 rtx basereg = gen_rtx_REG (Pmode, reg);
26889 emit_insn (gen_rtx_SET (basereg, XEXP (src, 0)));
26890 src = replace_equiv_address (src, basereg);
26894 breg = XEXP (src, 0);
26895 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
26896 breg = XEXP (breg, 0);
26898 /* If the base register we are using to address memory is
26899 also a destination reg, then change that register last. */
26900 if (REG_P (breg)
26901 && REGNO (breg) >= REGNO (dst)
26902 && REGNO (breg) < REGNO (dst) + nregs)
26903 j = REGNO (breg) - REGNO (dst);
26905 else if (MEM_P (dst) && INT_REGNO_P (reg))
26907 rtx breg;
26909 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
26910 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
26912 rtx delta_rtx;
26913 breg = XEXP (XEXP (dst, 0), 0);
26914 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
26915 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
26916 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
26918 /* We have to update the breg before doing the store.
26919 Use store with update, if available. */
26921 if (TARGET_UPDATE)
26923 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
26924 emit_insn (TARGET_32BIT
26925 ? (TARGET_POWERPC64
26926 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
26927 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
26928 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
26929 used_update = true;
26931 else
26932 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
26933 dst = replace_equiv_address (dst, breg);
26935 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
26936 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
26938 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
26940 rtx basereg = XEXP (XEXP (dst, 0), 0);
26941 if (TARGET_UPDATE)
26943 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
26944 emit_insn (gen_rtx_SET (gen_rtx_MEM (reg_mode,
26945 XEXP (dst, 0)),
26946 nsrc));
26947 used_update = true;
26949 else
26950 emit_insn (gen_rtx_SET (basereg,
26951 XEXP (XEXP (dst, 0), 1)));
26952 dst = replace_equiv_address (dst, basereg);
26954 else
26956 rtx basereg = XEXP (XEXP (dst, 0), 0);
26957 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
26958 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
26959 && REG_P (basereg)
26960 && REG_P (offsetreg)
26961 && REGNO (basereg) != REGNO (offsetreg));
26962 if (REGNO (basereg) == 0)
26964 rtx tmp = offsetreg;
26965 offsetreg = basereg;
26966 basereg = tmp;
26968 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
26969 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
26970 dst = replace_equiv_address (dst, basereg);
26973 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
26974 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
26977 for (i = 0; i < nregs; i++)
26979 /* Calculate index to next subword. */
26980 ++j;
26981 if (j == nregs)
26982 j = 0;
26984 /* If compiler already emitted move of first word by
26985 store with update, no need to do anything. */
26986 if (j == 0 && used_update)
26987 continue;
26989 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
26990 j * reg_mode_size),
26991 simplify_gen_subreg (reg_mode, src, mode,
26992 j * reg_mode_size)));
26994 if (restore_basereg != NULL_RTX)
26995 emit_insn (restore_basereg);
27000 /* This page contains routines that are used to determine what the
27001 function prologue and epilogue code will do and write them out. */
27003 static inline bool
27004 save_reg_p (int r)
27006 return !call_used_regs[r] && df_regs_ever_live_p (r);
27009 /* Determine whether the gp REG is really used. */
27011 static bool
27012 rs6000_reg_live_or_pic_offset_p (int reg)
27014 /* We need to mark the PIC offset register live for the same conditions
27015 as it is set up, or otherwise it won't be saved before we clobber it. */
27017 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
27019 if (TARGET_TOC && TARGET_MINIMAL_TOC
27020 && (crtl->calls_eh_return
27021 || df_regs_ever_live_p (reg)
27022 || !constant_pool_empty_p ()))
27023 return true;
27025 if ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
27026 && flag_pic)
27027 return true;
27030 /* If the function calls eh_return, claim used all the registers that would
27031 be checked for liveness otherwise. */
27033 return ((crtl->calls_eh_return || df_regs_ever_live_p (reg))
27034 && !call_used_regs[reg]);
27037 /* Return the first fixed-point register that is required to be
27038 saved. 32 if none. */
27041 first_reg_to_save (void)
27043 int first_reg;
27045 /* Find lowest numbered live register. */
27046 for (first_reg = 13; first_reg <= 31; first_reg++)
27047 if (save_reg_p (first_reg))
27048 break;
27050 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
27051 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
27052 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
27053 || (TARGET_TOC && TARGET_MINIMAL_TOC))
27054 && rs6000_reg_live_or_pic_offset_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
27055 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
27057 #if TARGET_MACHO
27058 if (flag_pic
27059 && crtl->uses_pic_offset_table
27060 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
27061 return RS6000_PIC_OFFSET_TABLE_REGNUM;
27062 #endif
27064 return first_reg;
27067 /* Similar, for FP regs. */
27070 first_fp_reg_to_save (void)
27072 int first_reg;
27074 /* Find lowest numbered live register. */
27075 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
27076 if (save_reg_p (first_reg))
27077 break;
27079 return first_reg;
27082 /* Similar, for AltiVec regs. */
27084 static int
27085 first_altivec_reg_to_save (void)
27087 int i;
27089 /* Stack frame remains as is unless we are in AltiVec ABI. */
27090 if (! TARGET_ALTIVEC_ABI)
27091 return LAST_ALTIVEC_REGNO + 1;
27093 /* On Darwin, the unwind routines are compiled without
27094 TARGET_ALTIVEC, and use save_world to save/restore the
27095 altivec registers when necessary. */
27096 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
27097 && ! TARGET_ALTIVEC)
27098 return FIRST_ALTIVEC_REGNO + 20;
27100 /* Find lowest numbered live register. */
27101 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
27102 if (save_reg_p (i))
27103 break;
27105 return i;
27108 /* Return a 32-bit mask of the AltiVec registers we need to set in
27109 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
27110 the 32-bit word is 0. */
27112 static unsigned int
27113 compute_vrsave_mask (void)
27115 unsigned int i, mask = 0;
27117 /* On Darwin, the unwind routines are compiled without
27118 TARGET_ALTIVEC, and use save_world to save/restore the
27119 call-saved altivec registers when necessary. */
27120 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
27121 && ! TARGET_ALTIVEC)
27122 mask |= 0xFFF;
27124 /* First, find out if we use _any_ altivec registers. */
27125 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
27126 if (df_regs_ever_live_p (i))
27127 mask |= ALTIVEC_REG_BIT (i);
27129 if (mask == 0)
27130 return mask;
27132 /* Next, remove the argument registers from the set. These must
27133 be in the VRSAVE mask set by the caller, so we don't need to add
27134 them in again. More importantly, the mask we compute here is
27135 used to generate CLOBBERs in the set_vrsave insn, and we do not
27136 wish the argument registers to die. */
27137 for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
27138 mask &= ~ALTIVEC_REG_BIT (i);
27140 /* Similarly, remove the return value from the set. */
27142 bool yes = false;
27143 diddle_return_value (is_altivec_return_reg, &yes);
27144 if (yes)
27145 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
27148 return mask;
27151 /* For a very restricted set of circumstances, we can cut down the
27152 size of prologues/epilogues by calling our own save/restore-the-world
27153 routines. */
27155 static void
27156 compute_save_world_info (rs6000_stack_t *info)
27158 info->world_save_p = 1;
27159 info->world_save_p
27160 = (WORLD_SAVE_P (info)
27161 && DEFAULT_ABI == ABI_DARWIN
27162 && !cfun->has_nonlocal_label
27163 && info->first_fp_reg_save == FIRST_SAVED_FP_REGNO
27164 && info->first_gp_reg_save == FIRST_SAVED_GP_REGNO
27165 && info->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
27166 && info->cr_save_p);
27168 /* This will not work in conjunction with sibcalls. Make sure there
27169 are none. (This check is expensive, but seldom executed.) */
27170 if (WORLD_SAVE_P (info))
27172 rtx_insn *insn;
27173 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
27174 if (CALL_P (insn) && SIBLING_CALL_P (insn))
27176 info->world_save_p = 0;
27177 break;
27181 if (WORLD_SAVE_P (info))
27183 /* Even if we're not touching VRsave, make sure there's room on the
27184 stack for it, if it looks like we're calling SAVE_WORLD, which
27185 will attempt to save it. */
27186 info->vrsave_size = 4;
27188 /* If we are going to save the world, we need to save the link register too. */
27189 info->lr_save_p = 1;
27191 /* "Save" the VRsave register too if we're saving the world. */
27192 if (info->vrsave_mask == 0)
27193 info->vrsave_mask = compute_vrsave_mask ();
27195 /* Because the Darwin register save/restore routines only handle
27196 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
27197 check. */
27198 gcc_assert (info->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
27199 && (info->first_altivec_reg_save
27200 >= FIRST_SAVED_ALTIVEC_REGNO));
27203 return;
27207 static void
27208 is_altivec_return_reg (rtx reg, void *xyes)
27210 bool *yes = (bool *) xyes;
27211 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
27212 *yes = true;
27216 /* Return whether REG is a global user reg or has been specifed by
27217 -ffixed-REG. We should not restore these, and so cannot use
27218 lmw or out-of-line restore functions if there are any. We also
27219 can't save them (well, emit frame notes for them), because frame
27220 unwinding during exception handling will restore saved registers. */
27222 static bool
27223 fixed_reg_p (int reg)
27225 /* Ignore fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] when the
27226 backend sets it, overriding anything the user might have given. */
27227 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
27228 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
27229 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
27230 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
27231 return false;
27233 return fixed_regs[reg];
27236 /* Determine the strategy for savings/restoring registers. */
27238 enum {
27239 SAVE_MULTIPLE = 0x1,
27240 SAVE_INLINE_GPRS = 0x2,
27241 SAVE_INLINE_FPRS = 0x4,
27242 SAVE_NOINLINE_GPRS_SAVES_LR = 0x8,
27243 SAVE_NOINLINE_FPRS_SAVES_LR = 0x10,
27244 SAVE_INLINE_VRS = 0x20,
27245 REST_MULTIPLE = 0x100,
27246 REST_INLINE_GPRS = 0x200,
27247 REST_INLINE_FPRS = 0x400,
27248 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x800,
27249 REST_INLINE_VRS = 0x1000
27252 static int
27253 rs6000_savres_strategy (rs6000_stack_t *info,
27254 bool using_static_chain_p)
27256 int strategy = 0;
27258 /* Select between in-line and out-of-line save and restore of regs.
27259 First, all the obvious cases where we don't use out-of-line. */
27260 if (crtl->calls_eh_return
27261 || cfun->machine->ra_need_lr)
27262 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
27263 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
27264 | SAVE_INLINE_VRS | REST_INLINE_VRS);
27266 if (info->first_gp_reg_save == 32)
27267 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27269 if (info->first_fp_reg_save == 64
27270 /* The out-of-line FP routines use double-precision stores;
27271 we can't use those routines if we don't have such stores. */
27272 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT))
27273 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27275 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
27276 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27278 /* Define cutoff for using out-of-line functions to save registers. */
27279 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
27281 if (!optimize_size)
27283 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27284 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27285 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27287 else
27289 /* Prefer out-of-line restore if it will exit. */
27290 if (info->first_fp_reg_save > 61)
27291 strategy |= SAVE_INLINE_FPRS;
27292 if (info->first_gp_reg_save > 29)
27294 if (info->first_fp_reg_save == 64)
27295 strategy |= SAVE_INLINE_GPRS;
27296 else
27297 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27299 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
27300 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27303 else if (DEFAULT_ABI == ABI_DARWIN)
27305 if (info->first_fp_reg_save > 60)
27306 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27307 if (info->first_gp_reg_save > 29)
27308 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27309 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27311 else
27313 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
27314 if ((flag_shrink_wrap_separate && optimize_function_for_speed_p (cfun))
27315 || info->first_fp_reg_save > 61)
27316 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27317 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27318 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27321 /* Don't bother to try to save things out-of-line if r11 is occupied
27322 by the static chain. It would require too much fiddling and the
27323 static chain is rarely used anyway. FPRs are saved w.r.t the stack
27324 pointer on Darwin, and AIX uses r1 or r12. */
27325 if (using_static_chain_p
27326 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
27327 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
27328 | SAVE_INLINE_GPRS
27329 | SAVE_INLINE_VRS);
27331 /* Saving CR interferes with the exit routines used on the SPE, so
27332 just punt here. */
27333 if (TARGET_SPE_ABI
27334 && info->spe_64bit_regs_used
27335 && info->cr_save_p)
27336 strategy |= REST_INLINE_GPRS;
27338 /* We can only use the out-of-line routines to restore fprs if we've
27339 saved all the registers from first_fp_reg_save in the prologue.
27340 Otherwise, we risk loading garbage. Of course, if we have saved
27341 out-of-line then we know we haven't skipped any fprs. */
27342 if ((strategy & SAVE_INLINE_FPRS)
27343 && !(strategy & REST_INLINE_FPRS))
27345 int i;
27347 for (i = info->first_fp_reg_save; i < 64; i++)
27348 if (fixed_regs[i] || !save_reg_p (i))
27350 strategy |= REST_INLINE_FPRS;
27351 break;
27355 /* Similarly, for altivec regs. */
27356 if ((strategy & SAVE_INLINE_VRS)
27357 && !(strategy & REST_INLINE_VRS))
27359 int i;
27361 for (i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
27362 if (fixed_regs[i] || !save_reg_p (i))
27364 strategy |= REST_INLINE_VRS;
27365 break;
27369 /* info->lr_save_p isn't yet set if the only reason lr needs to be
27370 saved is an out-of-line save or restore. Set up the value for
27371 the next test (excluding out-of-line gprs). */
27372 bool lr_save_p = (info->lr_save_p
27373 || !(strategy & SAVE_INLINE_FPRS)
27374 || !(strategy & SAVE_INLINE_VRS)
27375 || !(strategy & REST_INLINE_FPRS)
27376 || !(strategy & REST_INLINE_VRS));
27378 if (TARGET_MULTIPLE
27379 && !TARGET_POWERPC64
27380 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
27381 && info->first_gp_reg_save < 31
27382 && !(flag_shrink_wrap
27383 && flag_shrink_wrap_separate
27384 && optimize_function_for_speed_p (cfun)))
27386 /* Prefer store multiple for saves over out-of-line routines,
27387 since the store-multiple instruction will always be smaller. */
27388 strategy |= SAVE_INLINE_GPRS | SAVE_MULTIPLE;
27390 /* The situation is more complicated with load multiple. We'd
27391 prefer to use the out-of-line routines for restores, since the
27392 "exit" out-of-line routines can handle the restore of LR and the
27393 frame teardown. However if doesn't make sense to use the
27394 out-of-line routine if that is the only reason we'd need to save
27395 LR, and we can't use the "exit" out-of-line gpr restore if we
27396 have saved some fprs; In those cases it is advantageous to use
27397 load multiple when available. */
27398 if (info->first_fp_reg_save != 64 || !lr_save_p)
27399 strategy |= REST_INLINE_GPRS | REST_MULTIPLE;
27402 /* Using the "exit" out-of-line routine does not improve code size
27403 if using it would require lr to be saved and if only saving one
27404 or two gprs. */
27405 else if (!lr_save_p && info->first_gp_reg_save > 29)
27406 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27408 /* We can only use load multiple or the out-of-line routines to
27409 restore gprs if we've saved all the registers from
27410 first_gp_reg_save. Otherwise, we risk loading garbage.
27411 Of course, if we have saved out-of-line or used stmw then we know
27412 we haven't skipped any gprs. */
27413 if ((strategy & (SAVE_INLINE_GPRS | SAVE_MULTIPLE)) == SAVE_INLINE_GPRS
27414 && (strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
27416 int i;
27418 for (i = info->first_gp_reg_save; i < 32; i++)
27419 if (fixed_reg_p (i) || !save_reg_p (i))
27421 strategy |= REST_INLINE_GPRS;
27422 strategy &= ~REST_MULTIPLE;
27423 break;
27427 if (TARGET_ELF && TARGET_64BIT)
27429 if (!(strategy & SAVE_INLINE_FPRS))
27430 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
27431 else if (!(strategy & SAVE_INLINE_GPRS)
27432 && info->first_fp_reg_save == 64)
27433 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
27435 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
27436 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
27438 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
27439 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
27441 return strategy;
27444 /* Calculate the stack information for the current function. This is
27445 complicated by having two separate calling sequences, the AIX calling
27446 sequence and the V.4 calling sequence.
27448 AIX (and Darwin/Mac OS X) stack frames look like:
27449 32-bit 64-bit
27450 SP----> +---------------------------------------+
27451 | back chain to caller | 0 0
27452 +---------------------------------------+
27453 | saved CR | 4 8 (8-11)
27454 +---------------------------------------+
27455 | saved LR | 8 16
27456 +---------------------------------------+
27457 | reserved for compilers | 12 24
27458 +---------------------------------------+
27459 | reserved for binders | 16 32
27460 +---------------------------------------+
27461 | saved TOC pointer | 20 40
27462 +---------------------------------------+
27463 | Parameter save area (+padding*) (P) | 24 48
27464 +---------------------------------------+
27465 | Alloca space (A) | 24+P etc.
27466 +---------------------------------------+
27467 | Local variable space (L) | 24+P+A
27468 +---------------------------------------+
27469 | Float/int conversion temporary (X) | 24+P+A+L
27470 +---------------------------------------+
27471 | Save area for AltiVec registers (W) | 24+P+A+L+X
27472 +---------------------------------------+
27473 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
27474 +---------------------------------------+
27475 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
27476 +---------------------------------------+
27477 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
27478 +---------------------------------------+
27479 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
27480 +---------------------------------------+
27481 old SP->| back chain to caller's caller |
27482 +---------------------------------------+
27484 * If the alloca area is present, the parameter save area is
27485 padded so that the former starts 16-byte aligned.
27487 The required alignment for AIX configurations is two words (i.e., 8
27488 or 16 bytes).
27490 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
27492 SP----> +---------------------------------------+
27493 | Back chain to caller | 0
27494 +---------------------------------------+
27495 | Save area for CR | 8
27496 +---------------------------------------+
27497 | Saved LR | 16
27498 +---------------------------------------+
27499 | Saved TOC pointer | 24
27500 +---------------------------------------+
27501 | Parameter save area (+padding*) (P) | 32
27502 +---------------------------------------+
27503 | Alloca space (A) | 32+P
27504 +---------------------------------------+
27505 | Local variable space (L) | 32+P+A
27506 +---------------------------------------+
27507 | Save area for AltiVec registers (W) | 32+P+A+L
27508 +---------------------------------------+
27509 | AltiVec alignment padding (Y) | 32+P+A+L+W
27510 +---------------------------------------+
27511 | Save area for GP registers (G) | 32+P+A+L+W+Y
27512 +---------------------------------------+
27513 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
27514 +---------------------------------------+
27515 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
27516 +---------------------------------------+
27518 * If the alloca area is present, the parameter save area is
27519 padded so that the former starts 16-byte aligned.
27521 V.4 stack frames look like:
27523 SP----> +---------------------------------------+
27524 | back chain to caller | 0
27525 +---------------------------------------+
27526 | caller's saved LR | 4
27527 +---------------------------------------+
27528 | Parameter save area (+padding*) (P) | 8
27529 +---------------------------------------+
27530 | Alloca space (A) | 8+P
27531 +---------------------------------------+
27532 | Varargs save area (V) | 8+P+A
27533 +---------------------------------------+
27534 | Local variable space (L) | 8+P+A+V
27535 +---------------------------------------+
27536 | Float/int conversion temporary (X) | 8+P+A+V+L
27537 +---------------------------------------+
27538 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
27539 +---------------------------------------+
27540 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
27541 +---------------------------------------+
27542 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
27543 +---------------------------------------+
27544 | SPE: area for 64-bit GP registers |
27545 +---------------------------------------+
27546 | SPE alignment padding |
27547 +---------------------------------------+
27548 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
27549 +---------------------------------------+
27550 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
27551 +---------------------------------------+
27552 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
27553 +---------------------------------------+
27554 old SP->| back chain to caller's caller |
27555 +---------------------------------------+
27557 * If the alloca area is present and the required alignment is
27558 16 bytes, the parameter save area is padded so that the
27559 alloca area starts 16-byte aligned.
27561 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
27562 given. (But note below and in sysv4.h that we require only 8 and
27563 may round up the size of our stack frame anyways. The historical
27564 reason is early versions of powerpc-linux which didn't properly
27565 align the stack at program startup. A happy side-effect is that
27566 -mno-eabi libraries can be used with -meabi programs.)
27568 The EABI configuration defaults to the V.4 layout. However,
27569 the stack alignment requirements may differ. If -mno-eabi is not
27570 given, the required stack alignment is 8 bytes; if -mno-eabi is
27571 given, the required alignment is 16 bytes. (But see V.4 comment
27572 above.) */
27574 #ifndef ABI_STACK_BOUNDARY
27575 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
27576 #endif
27578 static rs6000_stack_t *
27579 rs6000_stack_info (void)
27581 /* We should never be called for thunks, we are not set up for that. */
27582 gcc_assert (!cfun->is_thunk);
27584 rs6000_stack_t *info = &stack_info;
27585 int reg_size = TARGET_32BIT ? 4 : 8;
27586 int ehrd_size;
27587 int ehcr_size;
27588 int save_align;
27589 int first_gp;
27590 HOST_WIDE_INT non_fixed_size;
27591 bool using_static_chain_p;
27593 if (reload_completed && info->reload_completed)
27594 return info;
27596 memset (info, 0, sizeof (*info));
27597 info->reload_completed = reload_completed;
27599 if (TARGET_SPE)
27601 /* Cache value so we don't rescan instruction chain over and over. */
27602 if (cfun->machine->spe_insn_chain_scanned_p == 0)
27603 cfun->machine->spe_insn_chain_scanned_p
27604 = spe_func_has_64bit_regs_p () + 1;
27605 info->spe_64bit_regs_used = cfun->machine->spe_insn_chain_scanned_p - 1;
27608 /* Select which calling sequence. */
27609 info->abi = DEFAULT_ABI;
27611 /* Calculate which registers need to be saved & save area size. */
27612 info->first_gp_reg_save = first_reg_to_save ();
27613 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
27614 even if it currently looks like we won't. Reload may need it to
27615 get at a constant; if so, it will have already created a constant
27616 pool entry for it. */
27617 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
27618 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
27619 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
27620 && crtl->uses_const_pool
27621 && info->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
27622 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
27623 else
27624 first_gp = info->first_gp_reg_save;
27626 info->gp_size = reg_size * (32 - first_gp);
27628 /* For the SPE, we have an additional upper 32-bits on each GPR.
27629 Ideally we should save the entire 64-bits only when the upper
27630 half is used in SIMD instructions. Since we only record
27631 registers live (not the size they are used in), this proves
27632 difficult because we'd have to traverse the instruction chain at
27633 the right time, taking reload into account. This is a real pain,
27634 so we opt to save the GPRs in 64-bits always if but one register
27635 gets used in 64-bits. Otherwise, all the registers in the frame
27636 get saved in 32-bits.
27638 So... since when we save all GPRs (except the SP) in 64-bits, the
27639 traditional GP save area will be empty. */
27640 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
27641 info->gp_size = 0;
27643 info->first_fp_reg_save = first_fp_reg_to_save ();
27644 info->fp_size = 8 * (64 - info->first_fp_reg_save);
27646 info->first_altivec_reg_save = first_altivec_reg_to_save ();
27647 info->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
27648 - info->first_altivec_reg_save);
27650 /* Does this function call anything? */
27651 info->calls_p = (!crtl->is_leaf || cfun->machine->ra_needs_full_frame);
27653 /* Determine if we need to save the condition code registers. */
27654 if (save_reg_p (CR2_REGNO)
27655 || save_reg_p (CR3_REGNO)
27656 || save_reg_p (CR4_REGNO))
27658 info->cr_save_p = 1;
27659 if (DEFAULT_ABI == ABI_V4)
27660 info->cr_size = reg_size;
27663 /* If the current function calls __builtin_eh_return, then we need
27664 to allocate stack space for registers that will hold data for
27665 the exception handler. */
27666 if (crtl->calls_eh_return)
27668 unsigned int i;
27669 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
27670 continue;
27672 /* SPE saves EH registers in 64-bits. */
27673 ehrd_size = i * (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0
27674 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
27676 else
27677 ehrd_size = 0;
27679 /* In the ELFv2 ABI, we also need to allocate space for separate
27680 CR field save areas if the function calls __builtin_eh_return. */
27681 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
27683 /* This hard-codes that we have three call-saved CR fields. */
27684 ehcr_size = 3 * reg_size;
27685 /* We do *not* use the regular CR save mechanism. */
27686 info->cr_save_p = 0;
27688 else
27689 ehcr_size = 0;
27691 /* Determine various sizes. */
27692 info->reg_size = reg_size;
27693 info->fixed_size = RS6000_SAVE_AREA;
27694 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
27695 if (cfun->calls_alloca)
27696 info->parm_size =
27697 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
27698 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
27699 else
27700 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
27701 TARGET_ALTIVEC ? 16 : 8);
27702 if (FRAME_GROWS_DOWNWARD)
27703 info->vars_size
27704 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
27705 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
27706 - (info->fixed_size + info->vars_size + info->parm_size);
27708 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
27709 info->spe_gp_size = 8 * (32 - first_gp);
27711 if (TARGET_ALTIVEC_ABI)
27712 info->vrsave_mask = compute_vrsave_mask ();
27714 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
27715 info->vrsave_size = 4;
27717 compute_save_world_info (info);
27719 /* Calculate the offsets. */
27720 switch (DEFAULT_ABI)
27722 case ABI_NONE:
27723 default:
27724 gcc_unreachable ();
27726 case ABI_AIX:
27727 case ABI_ELFv2:
27728 case ABI_DARWIN:
27729 info->fp_save_offset = -info->fp_size;
27730 info->gp_save_offset = info->fp_save_offset - info->gp_size;
27732 if (TARGET_ALTIVEC_ABI)
27734 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
27736 /* Align stack so vector save area is on a quadword boundary.
27737 The padding goes above the vectors. */
27738 if (info->altivec_size != 0)
27739 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
27741 info->altivec_save_offset = info->vrsave_save_offset
27742 - info->altivec_padding_size
27743 - info->altivec_size;
27744 gcc_assert (info->altivec_size == 0
27745 || info->altivec_save_offset % 16 == 0);
27747 /* Adjust for AltiVec case. */
27748 info->ehrd_offset = info->altivec_save_offset - ehrd_size;
27750 else
27751 info->ehrd_offset = info->gp_save_offset - ehrd_size;
27753 info->ehcr_offset = info->ehrd_offset - ehcr_size;
27754 info->cr_save_offset = reg_size; /* first word when 64-bit. */
27755 info->lr_save_offset = 2*reg_size;
27756 break;
27758 case ABI_V4:
27759 info->fp_save_offset = -info->fp_size;
27760 info->gp_save_offset = info->fp_save_offset - info->gp_size;
27761 info->cr_save_offset = info->gp_save_offset - info->cr_size;
27763 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
27765 /* Align stack so SPE GPR save area is aligned on a
27766 double-word boundary. */
27767 if (info->spe_gp_size != 0 && info->cr_save_offset != 0)
27768 info->spe_padding_size = 8 - (-info->cr_save_offset % 8);
27769 else
27770 info->spe_padding_size = 0;
27772 info->spe_gp_save_offset = info->cr_save_offset
27773 - info->spe_padding_size
27774 - info->spe_gp_size;
27776 /* Adjust for SPE case. */
27777 info->ehrd_offset = info->spe_gp_save_offset;
27779 else if (TARGET_ALTIVEC_ABI)
27781 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
27783 /* Align stack so vector save area is on a quadword boundary. */
27784 if (info->altivec_size != 0)
27785 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
27787 info->altivec_save_offset = info->vrsave_save_offset
27788 - info->altivec_padding_size
27789 - info->altivec_size;
27791 /* Adjust for AltiVec case. */
27792 info->ehrd_offset = info->altivec_save_offset;
27794 else
27795 info->ehrd_offset = info->cr_save_offset;
27797 info->ehrd_offset -= ehrd_size;
27798 info->lr_save_offset = reg_size;
27801 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
27802 info->save_size = RS6000_ALIGN (info->fp_size
27803 + info->gp_size
27804 + info->altivec_size
27805 + info->altivec_padding_size
27806 + info->spe_gp_size
27807 + info->spe_padding_size
27808 + ehrd_size
27809 + ehcr_size
27810 + info->cr_size
27811 + info->vrsave_size,
27812 save_align);
27814 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
27816 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
27817 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
27819 /* Determine if we need to save the link register. */
27820 if (info->calls_p
27821 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
27822 && crtl->profile
27823 && !TARGET_PROFILE_KERNEL)
27824 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
27825 #ifdef TARGET_RELOCATABLE
27826 || (DEFAULT_ABI == ABI_V4
27827 && (TARGET_RELOCATABLE || flag_pic > 1)
27828 && !constant_pool_empty_p ())
27829 #endif
27830 || rs6000_ra_ever_killed ())
27831 info->lr_save_p = 1;
27833 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
27834 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
27835 && call_used_regs[STATIC_CHAIN_REGNUM]);
27836 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
27838 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
27839 || !(info->savres_strategy & SAVE_INLINE_FPRS)
27840 || !(info->savres_strategy & SAVE_INLINE_VRS)
27841 || !(info->savres_strategy & REST_INLINE_GPRS)
27842 || !(info->savres_strategy & REST_INLINE_FPRS)
27843 || !(info->savres_strategy & REST_INLINE_VRS))
27844 info->lr_save_p = 1;
27846 if (info->lr_save_p)
27847 df_set_regs_ever_live (LR_REGNO, true);
27849 /* Determine if we need to allocate any stack frame:
27851 For AIX we need to push the stack if a frame pointer is needed
27852 (because the stack might be dynamically adjusted), if we are
27853 debugging, if we make calls, or if the sum of fp_save, gp_save,
27854 and local variables are more than the space needed to save all
27855 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
27856 + 18*8 = 288 (GPR13 reserved).
27858 For V.4 we don't have the stack cushion that AIX uses, but assume
27859 that the debugger can handle stackless frames. */
27861 if (info->calls_p)
27862 info->push_p = 1;
27864 else if (DEFAULT_ABI == ABI_V4)
27865 info->push_p = non_fixed_size != 0;
27867 else if (frame_pointer_needed)
27868 info->push_p = 1;
27870 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
27871 info->push_p = 1;
27873 else
27874 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
27876 return info;
27879 /* Return true if the current function uses any GPRs in 64-bit SIMD
27880 mode. */
27882 static bool
27883 spe_func_has_64bit_regs_p (void)
27885 rtx_insn *insns, *insn;
27887 /* Functions that save and restore all the call-saved registers will
27888 need to save/restore the registers in 64-bits. */
27889 if (crtl->calls_eh_return
27890 || cfun->calls_setjmp
27891 || crtl->has_nonlocal_goto)
27892 return true;
27894 insns = get_insns ();
27896 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
27898 if (INSN_P (insn))
27900 rtx i;
27902 /* FIXME: This should be implemented with attributes...
27904 (set_attr "spe64" "true")....then,
27905 if (get_spe64(insn)) return true;
27907 It's the only reliable way to do the stuff below. */
27909 i = PATTERN (insn);
27910 if (GET_CODE (i) == SET)
27912 machine_mode mode = GET_MODE (SET_SRC (i));
27914 if (SPE_VECTOR_MODE (mode))
27915 return true;
27916 if (TARGET_E500_DOUBLE
27917 && (mode == DFmode || FLOAT128_2REG_P (mode)))
27918 return true;
27923 return false;
27926 static void
27927 debug_stack_info (rs6000_stack_t *info)
27929 const char *abi_string;
27931 if (! info)
27932 info = rs6000_stack_info ();
27934 fprintf (stderr, "\nStack information for function %s:\n",
27935 ((current_function_decl && DECL_NAME (current_function_decl))
27936 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
27937 : "<unknown>"));
27939 switch (info->abi)
27941 default: abi_string = "Unknown"; break;
27942 case ABI_NONE: abi_string = "NONE"; break;
27943 case ABI_AIX: abi_string = "AIX"; break;
27944 case ABI_ELFv2: abi_string = "ELFv2"; break;
27945 case ABI_DARWIN: abi_string = "Darwin"; break;
27946 case ABI_V4: abi_string = "V.4"; break;
27949 fprintf (stderr, "\tABI = %5s\n", abi_string);
27951 if (TARGET_ALTIVEC_ABI)
27952 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
27954 if (TARGET_SPE_ABI)
27955 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
27957 if (info->first_gp_reg_save != 32)
27958 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
27960 if (info->first_fp_reg_save != 64)
27961 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
27963 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
27964 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
27965 info->first_altivec_reg_save);
27967 if (info->lr_save_p)
27968 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
27970 if (info->cr_save_p)
27971 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
27973 if (info->vrsave_mask)
27974 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
27976 if (info->push_p)
27977 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
27979 if (info->calls_p)
27980 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
27982 if (info->gp_size)
27983 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
27985 if (info->fp_size)
27986 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
27988 if (info->altivec_size)
27989 fprintf (stderr, "\taltivec_save_offset = %5d\n",
27990 info->altivec_save_offset);
27992 if (info->spe_gp_size)
27993 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
27994 info->spe_gp_save_offset);
27996 if (info->vrsave_size)
27997 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
27998 info->vrsave_save_offset);
28000 if (info->lr_save_p)
28001 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
28003 if (info->cr_save_p)
28004 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
28006 if (info->varargs_save_offset)
28007 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
28009 if (info->total_size)
28010 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
28011 info->total_size);
28013 if (info->vars_size)
28014 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
28015 info->vars_size);
28017 if (info->parm_size)
28018 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
28020 if (info->fixed_size)
28021 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
28023 if (info->gp_size)
28024 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
28026 if (info->spe_gp_size)
28027 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
28029 if (info->fp_size)
28030 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
28032 if (info->altivec_size)
28033 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
28035 if (info->vrsave_size)
28036 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
28038 if (info->altivec_padding_size)
28039 fprintf (stderr, "\taltivec_padding_size= %5d\n",
28040 info->altivec_padding_size);
28042 if (info->spe_padding_size)
28043 fprintf (stderr, "\tspe_padding_size = %5d\n",
28044 info->spe_padding_size);
28046 if (info->cr_size)
28047 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
28049 if (info->save_size)
28050 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
28052 if (info->reg_size != 4)
28053 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
28055 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
28057 fprintf (stderr, "\n");
28061 rs6000_return_addr (int count, rtx frame)
28063 /* Currently we don't optimize very well between prolog and body
28064 code and for PIC code the code can be actually quite bad, so
28065 don't try to be too clever here. */
28066 if (count != 0
28067 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
28069 cfun->machine->ra_needs_full_frame = 1;
28071 return
28072 gen_rtx_MEM
28073 (Pmode,
28074 memory_address
28075 (Pmode,
28076 plus_constant (Pmode,
28077 copy_to_reg
28078 (gen_rtx_MEM (Pmode,
28079 memory_address (Pmode, frame))),
28080 RETURN_ADDRESS_OFFSET)));
28083 cfun->machine->ra_need_lr = 1;
28084 return get_hard_reg_initial_val (Pmode, LR_REGNO);
28087 /* Say whether a function is a candidate for sibcall handling or not. */
28089 static bool
28090 rs6000_function_ok_for_sibcall (tree decl, tree exp)
28092 tree fntype;
28094 if (decl)
28095 fntype = TREE_TYPE (decl);
28096 else
28097 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
28099 /* We can't do it if the called function has more vector parameters
28100 than the current function; there's nowhere to put the VRsave code. */
28101 if (TARGET_ALTIVEC_ABI
28102 && TARGET_ALTIVEC_VRSAVE
28103 && !(decl && decl == current_function_decl))
28105 function_args_iterator args_iter;
28106 tree type;
28107 int nvreg = 0;
28109 /* Functions with vector parameters are required to have a
28110 prototype, so the argument type info must be available
28111 here. */
28112 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
28113 if (TREE_CODE (type) == VECTOR_TYPE
28114 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
28115 nvreg++;
28117 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
28118 if (TREE_CODE (type) == VECTOR_TYPE
28119 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
28120 nvreg--;
28122 if (nvreg > 0)
28123 return false;
28126 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
28127 functions, because the callee may have a different TOC pointer to
28128 the caller and there's no way to ensure we restore the TOC when
28129 we return. With the secure-plt SYSV ABI we can't make non-local
28130 calls when -fpic/PIC because the plt call stubs use r30. */
28131 if (DEFAULT_ABI == ABI_DARWIN
28132 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28133 && decl
28134 && !DECL_EXTERNAL (decl)
28135 && !DECL_WEAK (decl)
28136 && (*targetm.binds_local_p) (decl))
28137 || (DEFAULT_ABI == ABI_V4
28138 && (!TARGET_SECURE_PLT
28139 || !flag_pic
28140 || (decl
28141 && (*targetm.binds_local_p) (decl)))))
28143 tree attr_list = TYPE_ATTRIBUTES (fntype);
28145 if (!lookup_attribute ("longcall", attr_list)
28146 || lookup_attribute ("shortcall", attr_list))
28147 return true;
28150 return false;
28153 static int
28154 rs6000_ra_ever_killed (void)
28156 rtx_insn *top;
28157 rtx reg;
28158 rtx_insn *insn;
28160 if (cfun->is_thunk)
28161 return 0;
28163 if (cfun->machine->lr_save_state)
28164 return cfun->machine->lr_save_state - 1;
28166 /* regs_ever_live has LR marked as used if any sibcalls are present,
28167 but this should not force saving and restoring in the
28168 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
28169 clobbers LR, so that is inappropriate. */
28171 /* Also, the prologue can generate a store into LR that
28172 doesn't really count, like this:
28174 move LR->R0
28175 bcl to set PIC register
28176 move LR->R31
28177 move R0->LR
28179 When we're called from the epilogue, we need to avoid counting
28180 this as a store. */
28182 push_topmost_sequence ();
28183 top = get_insns ();
28184 pop_topmost_sequence ();
28185 reg = gen_rtx_REG (Pmode, LR_REGNO);
28187 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
28189 if (INSN_P (insn))
28191 if (CALL_P (insn))
28193 if (!SIBLING_CALL_P (insn))
28194 return 1;
28196 else if (find_regno_note (insn, REG_INC, LR_REGNO))
28197 return 1;
28198 else if (set_of (reg, insn) != NULL_RTX
28199 && !prologue_epilogue_contains (insn))
28200 return 1;
28203 return 0;
28206 /* Emit instructions needed to load the TOC register.
28207 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
28208 a constant pool; or for SVR4 -fpic. */
28210 void
28211 rs6000_emit_load_toc_table (int fromprolog)
28213 rtx dest;
28214 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
28216 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
28218 char buf[30];
28219 rtx lab, tmp1, tmp2, got;
28221 lab = gen_label_rtx ();
28222 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
28223 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
28224 if (flag_pic == 2)
28226 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
28227 need_toc_init = 1;
28229 else
28230 got = rs6000_got_sym ();
28231 tmp1 = tmp2 = dest;
28232 if (!fromprolog)
28234 tmp1 = gen_reg_rtx (Pmode);
28235 tmp2 = gen_reg_rtx (Pmode);
28237 emit_insn (gen_load_toc_v4_PIC_1 (lab));
28238 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
28239 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
28240 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
28242 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
28244 emit_insn (gen_load_toc_v4_pic_si ());
28245 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
28247 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
28249 char buf[30];
28250 rtx temp0 = (fromprolog
28251 ? gen_rtx_REG (Pmode, 0)
28252 : gen_reg_rtx (Pmode));
28254 if (fromprolog)
28256 rtx symF, symL;
28258 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
28259 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
28261 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
28262 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
28264 emit_insn (gen_load_toc_v4_PIC_1 (symF));
28265 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
28266 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
28268 else
28270 rtx tocsym, lab;
28272 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
28273 need_toc_init = 1;
28274 lab = gen_label_rtx ();
28275 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
28276 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
28277 if (TARGET_LINK_STACK)
28278 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
28279 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
28281 emit_insn (gen_addsi3 (dest, temp0, dest));
28283 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
28285 /* This is for AIX code running in non-PIC ELF32. */
28286 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
28288 need_toc_init = 1;
28289 emit_insn (gen_elf_high (dest, realsym));
28290 emit_insn (gen_elf_low (dest, dest, realsym));
28292 else
28294 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
28296 if (TARGET_32BIT)
28297 emit_insn (gen_load_toc_aix_si (dest));
28298 else
28299 emit_insn (gen_load_toc_aix_di (dest));
28303 /* Emit instructions to restore the link register after determining where
28304 its value has been stored. */
28306 void
28307 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
28309 rs6000_stack_t *info = rs6000_stack_info ();
28310 rtx operands[2];
28312 operands[0] = source;
28313 operands[1] = scratch;
28315 if (info->lr_save_p)
28317 rtx frame_rtx = stack_pointer_rtx;
28318 HOST_WIDE_INT sp_offset = 0;
28319 rtx tmp;
28321 if (frame_pointer_needed
28322 || cfun->calls_alloca
28323 || info->total_size > 32767)
28325 tmp = gen_frame_mem (Pmode, frame_rtx);
28326 emit_move_insn (operands[1], tmp);
28327 frame_rtx = operands[1];
28329 else if (info->push_p)
28330 sp_offset = info->total_size;
28332 tmp = plus_constant (Pmode, frame_rtx,
28333 info->lr_save_offset + sp_offset);
28334 tmp = gen_frame_mem (Pmode, tmp);
28335 emit_move_insn (tmp, operands[0]);
28337 else
28338 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
28340 /* Freeze lr_save_p. We've just emitted rtl that depends on the
28341 state of lr_save_p so any change from here on would be a bug. In
28342 particular, stop rs6000_ra_ever_killed from considering the SET
28343 of lr we may have added just above. */
28344 cfun->machine->lr_save_state = info->lr_save_p + 1;
28347 static GTY(()) alias_set_type set = -1;
28349 alias_set_type
28350 get_TOC_alias_set (void)
28352 if (set == -1)
28353 set = new_alias_set ();
28354 return set;
28357 /* This returns nonzero if the current function uses the TOC. This is
28358 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
28359 is generated by the ABI_V4 load_toc_* patterns. */
28360 #if TARGET_ELF
28361 static int
28362 uses_TOC (void)
28364 rtx_insn *insn;
28366 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
28367 if (INSN_P (insn))
28369 rtx pat = PATTERN (insn);
28370 int i;
28372 if (GET_CODE (pat) == PARALLEL)
28373 for (i = 0; i < XVECLEN (pat, 0); i++)
28375 rtx sub = XVECEXP (pat, 0, i);
28376 if (GET_CODE (sub) == USE)
28378 sub = XEXP (sub, 0);
28379 if (GET_CODE (sub) == UNSPEC
28380 && XINT (sub, 1) == UNSPEC_TOC)
28381 return 1;
28385 return 0;
28387 #endif
28390 create_TOC_reference (rtx symbol, rtx largetoc_reg)
28392 rtx tocrel, tocreg, hi;
28394 if (TARGET_DEBUG_ADDR)
28396 if (GET_CODE (symbol) == SYMBOL_REF)
28397 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
28398 XSTR (symbol, 0));
28399 else
28401 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
28402 GET_RTX_NAME (GET_CODE (symbol)));
28403 debug_rtx (symbol);
28407 if (!can_create_pseudo_p ())
28408 df_set_regs_ever_live (TOC_REGISTER, true);
28410 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
28411 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
28412 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
28413 return tocrel;
28415 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
28416 if (largetoc_reg != NULL)
28418 emit_move_insn (largetoc_reg, hi);
28419 hi = largetoc_reg;
28421 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
28424 /* Issue assembly directives that create a reference to the given DWARF
28425 FRAME_TABLE_LABEL from the current function section. */
28426 void
28427 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
28429 fprintf (asm_out_file, "\t.ref %s\n",
28430 (* targetm.strip_name_encoding) (frame_table_label));
28433 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
28434 and the change to the stack pointer. */
28436 static void
28437 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
28439 rtvec p;
28440 int i;
28441 rtx regs[3];
28443 i = 0;
28444 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28445 if (hard_frame_needed)
28446 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
28447 if (!(REGNO (fp) == STACK_POINTER_REGNUM
28448 || (hard_frame_needed
28449 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
28450 regs[i++] = fp;
28452 p = rtvec_alloc (i);
28453 while (--i >= 0)
28455 rtx mem = gen_frame_mem (BLKmode, regs[i]);
28456 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
28459 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
28462 /* Emit the correct code for allocating stack space, as insns.
28463 If COPY_REG, make sure a copy of the old frame is left there.
28464 The generated code may use hard register 0 as a temporary. */
28466 static rtx_insn *
28467 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
28469 rtx_insn *insn;
28470 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28471 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
28472 rtx todec = gen_int_mode (-size, Pmode);
28473 rtx par, set, mem;
28475 if (INTVAL (todec) != -size)
28477 warning (0, "stack frame too large");
28478 emit_insn (gen_trap ());
28479 return 0;
28482 if (crtl->limit_stack)
28484 if (REG_P (stack_limit_rtx)
28485 && REGNO (stack_limit_rtx) > 1
28486 && REGNO (stack_limit_rtx) <= 31)
28488 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
28489 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
28490 const0_rtx));
28492 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
28493 && TARGET_32BIT
28494 && DEFAULT_ABI == ABI_V4
28495 && !flag_pic)
28497 rtx toload = gen_rtx_CONST (VOIDmode,
28498 gen_rtx_PLUS (Pmode,
28499 stack_limit_rtx,
28500 GEN_INT (size)));
28502 emit_insn (gen_elf_high (tmp_reg, toload));
28503 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
28504 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
28505 const0_rtx));
28507 else
28508 warning (0, "stack limit expression is not supported");
28511 if (copy_reg)
28513 if (copy_off != 0)
28514 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
28515 else
28516 emit_move_insn (copy_reg, stack_reg);
28519 if (size > 32767)
28521 /* Need a note here so that try_split doesn't get confused. */
28522 if (get_last_insn () == NULL_RTX)
28523 emit_note (NOTE_INSN_DELETED);
28524 insn = emit_move_insn (tmp_reg, todec);
28525 try_split (PATTERN (insn), insn, 0);
28526 todec = tmp_reg;
28529 insn = emit_insn (TARGET_32BIT
28530 ? gen_movsi_update_stack (stack_reg, stack_reg,
28531 todec, stack_reg)
28532 : gen_movdi_di_update_stack (stack_reg, stack_reg,
28533 todec, stack_reg));
28534 /* Since we didn't use gen_frame_mem to generate the MEM, grab
28535 it now and set the alias set/attributes. The above gen_*_update
28536 calls will generate a PARALLEL with the MEM set being the first
28537 operation. */
28538 par = PATTERN (insn);
28539 gcc_assert (GET_CODE (par) == PARALLEL);
28540 set = XVECEXP (par, 0, 0);
28541 gcc_assert (GET_CODE (set) == SET);
28542 mem = SET_DEST (set);
28543 gcc_assert (MEM_P (mem));
28544 MEM_NOTRAP_P (mem) = 1;
28545 set_mem_alias_set (mem, get_frame_alias_set ());
28547 RTX_FRAME_RELATED_P (insn) = 1;
28548 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
28549 gen_rtx_SET (stack_reg, gen_rtx_PLUS (Pmode, stack_reg,
28550 GEN_INT (-size))));
28551 return insn;
28554 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
28556 #if PROBE_INTERVAL > 32768
28557 #error Cannot use indexed addressing mode for stack probing
28558 #endif
28560 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
28561 inclusive. These are offsets from the current stack pointer. */
28563 static void
28564 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
28566 /* See if we have a constant small number of probes to generate. If so,
28567 that's the easy case. */
28568 if (first + size <= 32768)
28570 HOST_WIDE_INT i;
28572 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
28573 it exceeds SIZE. If only one probe is needed, this will not
28574 generate any code. Then probe at FIRST + SIZE. */
28575 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
28576 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
28577 -(first + i)));
28579 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
28580 -(first + size)));
28583 /* Otherwise, do the same as above, but in a loop. Note that we must be
28584 extra careful with variables wrapping around because we might be at
28585 the very top (or the very bottom) of the address space and we have
28586 to be able to handle this case properly; in particular, we use an
28587 equality test for the loop condition. */
28588 else
28590 HOST_WIDE_INT rounded_size;
28591 rtx r12 = gen_rtx_REG (Pmode, 12);
28592 rtx r0 = gen_rtx_REG (Pmode, 0);
28594 /* Sanity check for the addressing mode we're going to use. */
28595 gcc_assert (first <= 32768);
28597 /* Step 1: round SIZE to the previous multiple of the interval. */
28599 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
28602 /* Step 2: compute initial and final value of the loop counter. */
28604 /* TEST_ADDR = SP + FIRST. */
28605 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
28606 -first)));
28608 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
28609 if (rounded_size > 32768)
28611 emit_move_insn (r0, GEN_INT (-rounded_size));
28612 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
28614 else
28615 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
28616 -rounded_size)));
28619 /* Step 3: the loop
28623 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
28624 probe at TEST_ADDR
28626 while (TEST_ADDR != LAST_ADDR)
28628 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
28629 until it is equal to ROUNDED_SIZE. */
28631 if (TARGET_64BIT)
28632 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
28633 else
28634 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
28637 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
28638 that SIZE is equal to ROUNDED_SIZE. */
28640 if (size != rounded_size)
28641 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
28645 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
28646 absolute addresses. */
28648 const char *
28649 output_probe_stack_range (rtx reg1, rtx reg2)
28651 static int labelno = 0;
28652 char loop_lab[32];
28653 rtx xops[2];
28655 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
28657 /* Loop. */
28658 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
28660 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
28661 xops[0] = reg1;
28662 xops[1] = GEN_INT (-PROBE_INTERVAL);
28663 output_asm_insn ("addi %0,%0,%1", xops);
28665 /* Probe at TEST_ADDR. */
28666 xops[1] = gen_rtx_REG (Pmode, 0);
28667 output_asm_insn ("stw %1,0(%0)", xops);
28669 /* Test if TEST_ADDR == LAST_ADDR. */
28670 xops[1] = reg2;
28671 if (TARGET_64BIT)
28672 output_asm_insn ("cmpd 0,%0,%1", xops);
28673 else
28674 output_asm_insn ("cmpw 0,%0,%1", xops);
28676 /* Branch. */
28677 fputs ("\tbne 0,", asm_out_file);
28678 assemble_name_raw (asm_out_file, loop_lab);
28679 fputc ('\n', asm_out_file);
28681 return "";
28684 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
28685 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
28686 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
28687 deduce these equivalences by itself so it wasn't necessary to hold
28688 its hand so much. Don't be tempted to always supply d2_f_d_e with
28689 the actual cfa register, ie. r31 when we are using a hard frame
28690 pointer. That fails when saving regs off r1, and sched moves the
28691 r31 setup past the reg saves. */
28693 static rtx_insn *
28694 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
28695 rtx reg2, rtx repl2)
28697 rtx repl;
28699 if (REGNO (reg) == STACK_POINTER_REGNUM)
28701 gcc_checking_assert (val == 0);
28702 repl = NULL_RTX;
28704 else
28705 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
28706 GEN_INT (val));
28708 rtx pat = PATTERN (insn);
28709 if (!repl && !reg2)
28711 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
28712 if (GET_CODE (pat) == PARALLEL)
28713 for (int i = 0; i < XVECLEN (pat, 0); i++)
28714 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
28716 rtx set = XVECEXP (pat, 0, i);
28718 /* If this PARALLEL has been emitted for out-of-line
28719 register save functions, or store multiple, then omit
28720 eh_frame info for any user-defined global regs. If
28721 eh_frame info is supplied, frame unwinding will
28722 restore a user reg. */
28723 if (!REG_P (SET_SRC (set))
28724 || !fixed_reg_p (REGNO (SET_SRC (set))))
28725 RTX_FRAME_RELATED_P (set) = 1;
28727 RTX_FRAME_RELATED_P (insn) = 1;
28728 return insn;
28731 /* We expect that 'pat' is either a SET or a PARALLEL containing
28732 SETs (and possibly other stuff). In a PARALLEL, all the SETs
28733 are important so they all have to be marked RTX_FRAME_RELATED_P.
28734 Call simplify_replace_rtx on the SETs rather than the whole insn
28735 so as to leave the other stuff alone (for example USE of r12). */
28737 set_used_flags (pat);
28738 if (GET_CODE (pat) == SET)
28740 if (repl)
28741 pat = simplify_replace_rtx (pat, reg, repl);
28742 if (reg2)
28743 pat = simplify_replace_rtx (pat, reg2, repl2);
28745 else if (GET_CODE (pat) == PARALLEL)
28747 pat = shallow_copy_rtx (pat);
28748 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
28750 for (int i = 0; i < XVECLEN (pat, 0); i++)
28751 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
28753 rtx set = XVECEXP (pat, 0, i);
28755 if (repl)
28756 set = simplify_replace_rtx (set, reg, repl);
28757 if (reg2)
28758 set = simplify_replace_rtx (set, reg2, repl2);
28759 XVECEXP (pat, 0, i) = set;
28761 /* Omit eh_frame info for any user-defined global regs. */
28762 if (!REG_P (SET_SRC (set))
28763 || !fixed_reg_p (REGNO (SET_SRC (set))))
28764 RTX_FRAME_RELATED_P (set) = 1;
28767 else
28768 gcc_unreachable ();
28770 RTX_FRAME_RELATED_P (insn) = 1;
28771 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
28773 return insn;
28776 /* Returns an insn that has a vrsave set operation with the
28777 appropriate CLOBBERs. */
28779 static rtx
28780 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
28782 int nclobs, i;
28783 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
28784 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
28786 clobs[0]
28787 = gen_rtx_SET (vrsave,
28788 gen_rtx_UNSPEC_VOLATILE (SImode,
28789 gen_rtvec (2, reg, vrsave),
28790 UNSPECV_SET_VRSAVE));
28792 nclobs = 1;
28794 /* We need to clobber the registers in the mask so the scheduler
28795 does not move sets to VRSAVE before sets of AltiVec registers.
28797 However, if the function receives nonlocal gotos, reload will set
28798 all call saved registers live. We will end up with:
28800 (set (reg 999) (mem))
28801 (parallel [ (set (reg vrsave) (unspec blah))
28802 (clobber (reg 999))])
28804 The clobber will cause the store into reg 999 to be dead, and
28805 flow will attempt to delete an epilogue insn. In this case, we
28806 need an unspec use/set of the register. */
28808 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
28809 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
28811 if (!epiloguep || call_used_regs [i])
28812 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
28813 gen_rtx_REG (V4SImode, i));
28814 else
28816 rtx reg = gen_rtx_REG (V4SImode, i);
28818 clobs[nclobs++]
28819 = gen_rtx_SET (reg,
28820 gen_rtx_UNSPEC (V4SImode,
28821 gen_rtvec (1, reg), 27));
28825 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
28827 for (i = 0; i < nclobs; ++i)
28828 XVECEXP (insn, 0, i) = clobs[i];
28830 return insn;
28833 static rtx
28834 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
28836 rtx addr, mem;
28838 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
28839 mem = gen_frame_mem (GET_MODE (reg), addr);
28840 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
28843 static rtx
28844 gen_frame_load (rtx reg, rtx frame_reg, int offset)
28846 return gen_frame_set (reg, frame_reg, offset, false);
28849 static rtx
28850 gen_frame_store (rtx reg, rtx frame_reg, int offset)
28852 return gen_frame_set (reg, frame_reg, offset, true);
28855 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
28856 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
28858 static rtx_insn *
28859 emit_frame_save (rtx frame_reg, machine_mode mode,
28860 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
28862 rtx reg;
28864 /* Some cases that need register indexed addressing. */
28865 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
28866 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
28867 || (TARGET_E500_DOUBLE && mode == DFmode)
28868 || (TARGET_SPE_ABI
28869 && SPE_VECTOR_MODE (mode)
28870 && !SPE_CONST_OFFSET_OK (offset))));
28872 reg = gen_rtx_REG (mode, regno);
28873 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
28874 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
28875 NULL_RTX, NULL_RTX);
28878 /* Emit an offset memory reference suitable for a frame store, while
28879 converting to a valid addressing mode. */
28881 static rtx
28882 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
28884 rtx int_rtx, offset_rtx;
28886 int_rtx = GEN_INT (offset);
28888 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
28889 || (TARGET_E500_DOUBLE && mode == DFmode))
28891 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
28892 emit_move_insn (offset_rtx, int_rtx);
28894 else
28895 offset_rtx = int_rtx;
28897 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
28900 #ifndef TARGET_FIX_AND_CONTINUE
28901 #define TARGET_FIX_AND_CONTINUE 0
28902 #endif
28904 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
28905 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
28906 #define LAST_SAVRES_REGISTER 31
28907 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
28909 enum {
28910 SAVRES_LR = 0x1,
28911 SAVRES_SAVE = 0x2,
28912 SAVRES_REG = 0x0c,
28913 SAVRES_GPR = 0,
28914 SAVRES_FPR = 4,
28915 SAVRES_VR = 8
28918 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
28920 /* Temporary holding space for an out-of-line register save/restore
28921 routine name. */
28922 static char savres_routine_name[30];
28924 /* Return the name for an out-of-line register save/restore routine.
28925 We are saving/restoring GPRs if GPR is true. */
28927 static char *
28928 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
28930 const char *prefix = "";
28931 const char *suffix = "";
28933 /* Different targets are supposed to define
28934 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
28935 routine name could be defined with:
28937 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
28939 This is a nice idea in practice, but in reality, things are
28940 complicated in several ways:
28942 - ELF targets have save/restore routines for GPRs.
28944 - SPE targets use different prefixes for 32/64-bit registers, and
28945 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
28947 - PPC64 ELF targets have routines for save/restore of GPRs that
28948 differ in what they do with the link register, so having a set
28949 prefix doesn't work. (We only use one of the save routines at
28950 the moment, though.)
28952 - PPC32 elf targets have "exit" versions of the restore routines
28953 that restore the link register and can save some extra space.
28954 These require an extra suffix. (There are also "tail" versions
28955 of the restore routines and "GOT" versions of the save routines,
28956 but we don't generate those at present. Same problems apply,
28957 though.)
28959 We deal with all this by synthesizing our own prefix/suffix and
28960 using that for the simple sprintf call shown above. */
28961 if (TARGET_SPE)
28963 /* No floating point saves on the SPE. */
28964 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
28966 if ((sel & SAVRES_SAVE))
28967 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
28968 else
28969 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
28971 if ((sel & SAVRES_LR))
28972 suffix = "_x";
28974 else if (DEFAULT_ABI == ABI_V4)
28976 if (TARGET_64BIT)
28977 goto aix_names;
28979 if ((sel & SAVRES_REG) == SAVRES_GPR)
28980 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
28981 else if ((sel & SAVRES_REG) == SAVRES_FPR)
28982 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
28983 else if ((sel & SAVRES_REG) == SAVRES_VR)
28984 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
28985 else
28986 abort ();
28988 if ((sel & SAVRES_LR))
28989 suffix = "_x";
28991 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28993 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
28994 /* No out-of-line save/restore routines for GPRs on AIX. */
28995 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
28996 #endif
28998 aix_names:
28999 if ((sel & SAVRES_REG) == SAVRES_GPR)
29000 prefix = ((sel & SAVRES_SAVE)
29001 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
29002 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
29003 else if ((sel & SAVRES_REG) == SAVRES_FPR)
29005 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
29006 if ((sel & SAVRES_LR))
29007 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
29008 else
29009 #endif
29011 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
29012 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
29015 else if ((sel & SAVRES_REG) == SAVRES_VR)
29016 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
29017 else
29018 abort ();
29021 if (DEFAULT_ABI == ABI_DARWIN)
29023 /* The Darwin approach is (slightly) different, in order to be
29024 compatible with code generated by the system toolchain. There is a
29025 single symbol for the start of save sequence, and the code here
29026 embeds an offset into that code on the basis of the first register
29027 to be saved. */
29028 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
29029 if ((sel & SAVRES_REG) == SAVRES_GPR)
29030 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
29031 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
29032 (regno - 13) * 4, prefix, regno);
29033 else if ((sel & SAVRES_REG) == SAVRES_FPR)
29034 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
29035 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
29036 else if ((sel & SAVRES_REG) == SAVRES_VR)
29037 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
29038 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
29039 else
29040 abort ();
29042 else
29043 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
29045 return savres_routine_name;
29048 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
29049 We are saving/restoring GPRs if GPR is true. */
29051 static rtx
29052 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
29054 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
29055 ? info->first_gp_reg_save
29056 : (sel & SAVRES_REG) == SAVRES_FPR
29057 ? info->first_fp_reg_save - 32
29058 : (sel & SAVRES_REG) == SAVRES_VR
29059 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
29060 : -1);
29061 rtx sym;
29062 int select = sel;
29064 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
29065 versions of the gpr routines. */
29066 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
29067 && info->spe_64bit_regs_used)
29068 select ^= SAVRES_FPR ^ SAVRES_GPR;
29070 /* Don't generate bogus routine names. */
29071 gcc_assert (FIRST_SAVRES_REGISTER <= regno
29072 && regno <= LAST_SAVRES_REGISTER
29073 && select >= 0 && select <= 12);
29075 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
29077 if (sym == NULL)
29079 char *name;
29081 name = rs6000_savres_routine_name (info, regno, sel);
29083 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
29084 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
29085 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
29088 return sym;
29091 /* Emit a sequence of insns, including a stack tie if needed, for
29092 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
29093 reset the stack pointer, but move the base of the frame into
29094 reg UPDT_REGNO for use by out-of-line register restore routines. */
29096 static rtx
29097 rs6000_emit_stack_reset (rs6000_stack_t *info,
29098 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
29099 unsigned updt_regno)
29101 /* If there is nothing to do, don't do anything. */
29102 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
29103 return NULL_RTX;
29105 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
29107 /* This blockage is needed so that sched doesn't decide to move
29108 the sp change before the register restores. */
29109 if (DEFAULT_ABI == ABI_V4
29110 || (TARGET_SPE_ABI
29111 && info->spe_64bit_regs_used != 0
29112 && info->first_gp_reg_save != 32))
29113 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
29114 GEN_INT (frame_off)));
29116 /* If we are restoring registers out-of-line, we will be using the
29117 "exit" variants of the restore routines, which will reset the
29118 stack for us. But we do need to point updt_reg into the
29119 right place for those routines. */
29120 if (frame_off != 0)
29121 return emit_insn (gen_add3_insn (updt_reg_rtx,
29122 frame_reg_rtx, GEN_INT (frame_off)));
29123 else
29124 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
29126 return NULL_RTX;
29129 /* Return the register number used as a pointer by out-of-line
29130 save/restore functions. */
29132 static inline unsigned
29133 ptr_regno_for_savres (int sel)
29135 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29136 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
29137 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
29140 /* Construct a parallel rtx describing the effect of a call to an
29141 out-of-line register save/restore routine, and emit the insn
29142 or jump_insn as appropriate. */
29144 static rtx_insn *
29145 rs6000_emit_savres_rtx (rs6000_stack_t *info,
29146 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
29147 machine_mode reg_mode, int sel)
29149 int i;
29150 int offset, start_reg, end_reg, n_regs, use_reg;
29151 int reg_size = GET_MODE_SIZE (reg_mode);
29152 rtx sym;
29153 rtvec p;
29154 rtx par;
29155 rtx_insn *insn;
29157 offset = 0;
29158 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
29159 ? info->first_gp_reg_save
29160 : (sel & SAVRES_REG) == SAVRES_FPR
29161 ? info->first_fp_reg_save
29162 : (sel & SAVRES_REG) == SAVRES_VR
29163 ? info->first_altivec_reg_save
29164 : -1);
29165 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
29166 ? 32
29167 : (sel & SAVRES_REG) == SAVRES_FPR
29168 ? 64
29169 : (sel & SAVRES_REG) == SAVRES_VR
29170 ? LAST_ALTIVEC_REGNO + 1
29171 : -1);
29172 n_regs = end_reg - start_reg;
29173 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
29174 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
29175 + n_regs);
29177 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
29178 RTVEC_ELT (p, offset++) = ret_rtx;
29180 RTVEC_ELT (p, offset++)
29181 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
29183 sym = rs6000_savres_routine_sym (info, sel);
29184 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
29186 use_reg = ptr_regno_for_savres (sel);
29187 if ((sel & SAVRES_REG) == SAVRES_VR)
29189 /* Vector regs are saved/restored using [reg+reg] addressing. */
29190 RTVEC_ELT (p, offset++)
29191 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
29192 RTVEC_ELT (p, offset++)
29193 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
29195 else
29196 RTVEC_ELT (p, offset++)
29197 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
29199 for (i = 0; i < end_reg - start_reg; i++)
29200 RTVEC_ELT (p, i + offset)
29201 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
29202 frame_reg_rtx, save_area_offset + reg_size * i,
29203 (sel & SAVRES_SAVE) != 0);
29205 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
29206 RTVEC_ELT (p, i + offset)
29207 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
29209 par = gen_rtx_PARALLEL (VOIDmode, p);
29211 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
29213 insn = emit_jump_insn (par);
29214 JUMP_LABEL (insn) = ret_rtx;
29216 else
29217 insn = emit_insn (par);
29218 return insn;
29221 /* Emit code to store CR fields that need to be saved into REG. */
29223 static void
29224 rs6000_emit_move_from_cr (rtx reg)
29226 /* Only the ELFv2 ABI allows storing only selected fields. */
29227 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
29229 int i, cr_reg[8], count = 0;
29231 /* Collect CR fields that must be saved. */
29232 for (i = 0; i < 8; i++)
29233 if (save_reg_p (CR0_REGNO + i))
29234 cr_reg[count++] = i;
29236 /* If it's just a single one, use mfcrf. */
29237 if (count == 1)
29239 rtvec p = rtvec_alloc (1);
29240 rtvec r = rtvec_alloc (2);
29241 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
29242 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
29243 RTVEC_ELT (p, 0)
29244 = gen_rtx_SET (reg,
29245 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
29247 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
29248 return;
29251 /* ??? It might be better to handle count == 2 / 3 cases here
29252 as well, using logical operations to combine the values. */
29255 emit_insn (gen_movesi_from_cr (reg));
29258 /* Return whether the split-stack arg pointer (r12) is used. */
29260 static bool
29261 split_stack_arg_pointer_used_p (void)
29263 /* If the pseudo holding the arg pointer is no longer a pseudo,
29264 then the arg pointer is used. */
29265 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
29266 && (!REG_P (cfun->machine->split_stack_arg_pointer)
29267 || (REGNO (cfun->machine->split_stack_arg_pointer)
29268 < FIRST_PSEUDO_REGISTER)))
29269 return true;
29271 /* Unfortunately we also need to do some code scanning, since
29272 r12 may have been substituted for the pseudo. */
29273 rtx_insn *insn;
29274 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
29275 FOR_BB_INSNS (bb, insn)
29276 if (NONDEBUG_INSN_P (insn))
29278 /* A call destroys r12. */
29279 if (CALL_P (insn))
29280 return false;
29282 df_ref use;
29283 FOR_EACH_INSN_USE (use, insn)
29285 rtx x = DF_REF_REG (use);
29286 if (REG_P (x) && REGNO (x) == 12)
29287 return true;
29289 df_ref def;
29290 FOR_EACH_INSN_DEF (def, insn)
29292 rtx x = DF_REF_REG (def);
29293 if (REG_P (x) && REGNO (x) == 12)
29294 return false;
29297 return bitmap_bit_p (DF_LR_OUT (bb), 12);
29300 /* Return whether we need to emit an ELFv2 global entry point prologue. */
29302 static bool
29303 rs6000_global_entry_point_needed_p (void)
29305 /* Only needed for the ELFv2 ABI. */
29306 if (DEFAULT_ABI != ABI_ELFv2)
29307 return false;
29309 /* With -msingle-pic-base, we assume the whole program shares the same
29310 TOC, so no global entry point prologues are needed anywhere. */
29311 if (TARGET_SINGLE_PIC_BASE)
29312 return false;
29314 /* Ensure we have a global entry point for thunks. ??? We could
29315 avoid that if the target routine doesn't need a global entry point,
29316 but we do not know whether this is the case at this point. */
29317 if (cfun->is_thunk)
29318 return true;
29320 /* For regular functions, rs6000_emit_prologue sets this flag if the
29321 routine ever uses the TOC pointer. */
29322 return cfun->machine->r2_setup_needed;
29325 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
29326 static sbitmap
29327 rs6000_get_separate_components (void)
29329 rs6000_stack_t *info = rs6000_stack_info ();
29331 if (WORLD_SAVE_P (info))
29332 return NULL;
29334 if (TARGET_SPE_ABI)
29335 return NULL;
29337 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
29338 && !(info->savres_strategy & REST_MULTIPLE));
29340 /* Component 0 is the save/restore of LR (done via GPR0).
29341 Components 13..31 are the save/restore of GPR13..GPR31.
29342 Components 46..63 are the save/restore of FPR14..FPR31. */
29344 cfun->machine->n_components = 64;
29346 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
29347 bitmap_clear (components);
29349 int reg_size = TARGET_32BIT ? 4 : 8;
29350 int fp_reg_size = 8;
29352 /* The GPRs we need saved to the frame. */
29353 if ((info->savres_strategy & SAVE_INLINE_GPRS)
29354 && (info->savres_strategy & REST_INLINE_GPRS))
29356 int offset = info->gp_save_offset;
29357 if (info->push_p)
29358 offset += info->total_size;
29360 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
29362 if (IN_RANGE (offset, -0x8000, 0x7fff)
29363 && rs6000_reg_live_or_pic_offset_p (regno))
29364 bitmap_set_bit (components, regno);
29366 offset += reg_size;
29370 /* Don't mess with the hard frame pointer. */
29371 if (frame_pointer_needed)
29372 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
29374 /* Don't mess with the fixed TOC register. */
29375 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
29376 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
29377 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
29378 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
29380 /* The FPRs we need saved to the frame. */
29381 if ((info->savres_strategy & SAVE_INLINE_FPRS)
29382 && (info->savres_strategy & REST_INLINE_FPRS))
29384 int offset = info->fp_save_offset;
29385 if (info->push_p)
29386 offset += info->total_size;
29388 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
29390 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
29391 bitmap_set_bit (components, regno);
29393 offset += fp_reg_size;
29397 /* Optimize LR save and restore if we can. This is component 0. Any
29398 out-of-line register save/restore routines need LR. */
29399 if (info->lr_save_p
29400 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
29401 && (info->savres_strategy & SAVE_INLINE_GPRS)
29402 && (info->savres_strategy & REST_INLINE_GPRS)
29403 && (info->savres_strategy & SAVE_INLINE_FPRS)
29404 && (info->savres_strategy & REST_INLINE_FPRS)
29405 && (info->savres_strategy & SAVE_INLINE_VRS)
29406 && (info->savres_strategy & REST_INLINE_VRS))
29408 int offset = info->lr_save_offset;
29409 if (info->push_p)
29410 offset += info->total_size;
29411 if (IN_RANGE (offset, -0x8000, 0x7fff))
29412 bitmap_set_bit (components, 0);
29415 return components;
29418 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
29419 static sbitmap
29420 rs6000_components_for_bb (basic_block bb)
29422 rs6000_stack_t *info = rs6000_stack_info ();
29424 bitmap in = DF_LIVE_IN (bb);
29425 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
29426 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
29428 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
29429 bitmap_clear (components);
29431 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
29433 /* GPRs. */
29434 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
29435 if (bitmap_bit_p (in, regno)
29436 || bitmap_bit_p (gen, regno)
29437 || bitmap_bit_p (kill, regno))
29438 bitmap_set_bit (components, regno);
29440 /* FPRs. */
29441 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
29442 if (bitmap_bit_p (in, regno)
29443 || bitmap_bit_p (gen, regno)
29444 || bitmap_bit_p (kill, regno))
29445 bitmap_set_bit (components, regno);
29447 /* The link register. */
29448 if (bitmap_bit_p (in, LR_REGNO)
29449 || bitmap_bit_p (gen, LR_REGNO)
29450 || bitmap_bit_p (kill, LR_REGNO))
29451 bitmap_set_bit (components, 0);
29453 return components;
29456 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
29457 static void
29458 rs6000_disqualify_components (sbitmap components, edge e,
29459 sbitmap edge_components, bool /*is_prologue*/)
29461 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
29462 live where we want to place that code. */
29463 if (bitmap_bit_p (edge_components, 0)
29464 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
29466 if (dump_file)
29467 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
29468 "on entry to bb %d\n", e->dest->index);
29469 bitmap_clear_bit (components, 0);
29473 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
29474 static void
29475 rs6000_emit_prologue_components (sbitmap components)
29477 rs6000_stack_t *info = rs6000_stack_info ();
29478 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
29479 ? HARD_FRAME_POINTER_REGNUM
29480 : STACK_POINTER_REGNUM);
29482 machine_mode reg_mode = Pmode;
29483 int reg_size = TARGET_32BIT ? 4 : 8;
29484 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
29485 ? DFmode : SFmode;
29486 int fp_reg_size = 8;
29488 /* Prologue for LR. */
29489 if (bitmap_bit_p (components, 0))
29491 rtx reg = gen_rtx_REG (reg_mode, 0);
29492 rtx_insn *insn = emit_move_insn (reg, gen_rtx_REG (reg_mode, LR_REGNO));
29493 RTX_FRAME_RELATED_P (insn) = 1;
29494 add_reg_note (insn, REG_CFA_REGISTER, NULL);
29496 int offset = info->lr_save_offset;
29497 if (info->push_p)
29498 offset += info->total_size;
29500 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
29501 RTX_FRAME_RELATED_P (insn) = 1;
29502 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
29503 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
29504 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
29507 /* Prologue for the GPRs. */
29508 int offset = info->gp_save_offset;
29509 if (info->push_p)
29510 offset += info->total_size;
29512 for (int i = info->first_gp_reg_save; i < 32; i++)
29514 if (bitmap_bit_p (components, i))
29516 rtx reg = gen_rtx_REG (reg_mode, i);
29517 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
29518 RTX_FRAME_RELATED_P (insn) = 1;
29519 rtx set = copy_rtx (single_set (insn));
29520 add_reg_note (insn, REG_CFA_OFFSET, set);
29523 offset += reg_size;
29526 /* Prologue for the FPRs. */
29527 offset = info->fp_save_offset;
29528 if (info->push_p)
29529 offset += info->total_size;
29531 for (int i = info->first_fp_reg_save; i < 64; i++)
29533 if (bitmap_bit_p (components, i))
29535 rtx reg = gen_rtx_REG (fp_reg_mode, i);
29536 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
29537 RTX_FRAME_RELATED_P (insn) = 1;
29538 rtx set = copy_rtx (single_set (insn));
29539 add_reg_note (insn, REG_CFA_OFFSET, set);
29542 offset += fp_reg_size;
29546 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
29547 static void
29548 rs6000_emit_epilogue_components (sbitmap components)
29550 rs6000_stack_t *info = rs6000_stack_info ();
29551 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
29552 ? HARD_FRAME_POINTER_REGNUM
29553 : STACK_POINTER_REGNUM);
29555 machine_mode reg_mode = Pmode;
29556 int reg_size = TARGET_32BIT ? 4 : 8;
29558 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
29559 ? DFmode : SFmode;
29560 int fp_reg_size = 8;
29562 /* Epilogue for the FPRs. */
29563 int offset = info->fp_save_offset;
29564 if (info->push_p)
29565 offset += info->total_size;
29567 for (int i = info->first_fp_reg_save; i < 64; i++)
29569 if (bitmap_bit_p (components, i))
29571 rtx reg = gen_rtx_REG (fp_reg_mode, i);
29572 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
29573 RTX_FRAME_RELATED_P (insn) = 1;
29574 add_reg_note (insn, REG_CFA_RESTORE, reg);
29577 offset += fp_reg_size;
29580 /* Epilogue for the GPRs. */
29581 offset = info->gp_save_offset;
29582 if (info->push_p)
29583 offset += info->total_size;
29585 for (int i = info->first_gp_reg_save; i < 32; i++)
29587 if (bitmap_bit_p (components, i))
29589 rtx reg = gen_rtx_REG (reg_mode, i);
29590 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
29591 RTX_FRAME_RELATED_P (insn) = 1;
29592 add_reg_note (insn, REG_CFA_RESTORE, reg);
29595 offset += reg_size;
29598 /* Epilogue for LR. */
29599 if (bitmap_bit_p (components, 0))
29601 int offset = info->lr_save_offset;
29602 if (info->push_p)
29603 offset += info->total_size;
29605 rtx reg = gen_rtx_REG (reg_mode, 0);
29606 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
29608 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
29609 insn = emit_move_insn (lr, reg);
29610 RTX_FRAME_RELATED_P (insn) = 1;
29611 add_reg_note (insn, REG_CFA_RESTORE, lr);
29615 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
29616 static void
29617 rs6000_set_handled_components (sbitmap components)
29619 rs6000_stack_t *info = rs6000_stack_info ();
29621 for (int i = info->first_gp_reg_save; i < 32; i++)
29622 if (bitmap_bit_p (components, i))
29623 cfun->machine->gpr_is_wrapped_separately[i] = true;
29625 for (int i = info->first_fp_reg_save; i < 64; i++)
29626 if (bitmap_bit_p (components, i))
29627 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
29629 if (bitmap_bit_p (components, 0))
29630 cfun->machine->lr_is_wrapped_separately = true;
29633 /* Emit function prologue as insns. */
29635 void
29636 rs6000_emit_prologue (void)
29638 rs6000_stack_t *info = rs6000_stack_info ();
29639 machine_mode reg_mode = Pmode;
29640 int reg_size = TARGET_32BIT ? 4 : 8;
29641 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
29642 ? DFmode : SFmode;
29643 int fp_reg_size = 8;
29644 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29645 rtx frame_reg_rtx = sp_reg_rtx;
29646 unsigned int cr_save_regno;
29647 rtx cr_save_rtx = NULL_RTX;
29648 rtx_insn *insn;
29649 int strategy;
29650 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
29651 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
29652 && call_used_regs[STATIC_CHAIN_REGNUM]);
29653 int using_split_stack = (flag_split_stack
29654 && (lookup_attribute ("no_split_stack",
29655 DECL_ATTRIBUTES (cfun->decl))
29656 == NULL));
29658 /* Offset to top of frame for frame_reg and sp respectively. */
29659 HOST_WIDE_INT frame_off = 0;
29660 HOST_WIDE_INT sp_off = 0;
29661 /* sp_adjust is the stack adjusting instruction, tracked so that the
29662 insn setting up the split-stack arg pointer can be emitted just
29663 prior to it, when r12 is not used here for other purposes. */
29664 rtx_insn *sp_adjust = 0;
29666 #if CHECKING_P
29667 /* Track and check usage of r0, r11, r12. */
29668 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
29669 #define START_USE(R) do \
29671 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
29672 reg_inuse |= 1 << (R); \
29673 } while (0)
29674 #define END_USE(R) do \
29676 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
29677 reg_inuse &= ~(1 << (R)); \
29678 } while (0)
29679 #define NOT_INUSE(R) do \
29681 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
29682 } while (0)
29683 #else
29684 #define START_USE(R) do {} while (0)
29685 #define END_USE(R) do {} while (0)
29686 #define NOT_INUSE(R) do {} while (0)
29687 #endif
29689 if (DEFAULT_ABI == ABI_ELFv2
29690 && !TARGET_SINGLE_PIC_BASE)
29692 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
29694 /* With -mminimal-toc we may generate an extra use of r2 below. */
29695 if (TARGET_TOC && TARGET_MINIMAL_TOC
29696 && !constant_pool_empty_p ())
29697 cfun->machine->r2_setup_needed = true;
29701 if (flag_stack_usage_info)
29702 current_function_static_stack_size = info->total_size;
29704 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
29705 || flag_stack_clash_protection)
29707 HOST_WIDE_INT size = info->total_size;
29709 if (crtl->is_leaf && !cfun->calls_alloca)
29711 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
29712 rs6000_emit_probe_stack_range (get_stack_check_protect (),
29713 size - get_stack_check_protect ());
29715 else if (size > 0)
29716 rs6000_emit_probe_stack_range (get_stack_check_protect (), size);
29719 if (TARGET_FIX_AND_CONTINUE)
29721 /* gdb on darwin arranges to forward a function from the old
29722 address by modifying the first 5 instructions of the function
29723 to branch to the overriding function. This is necessary to
29724 permit function pointers that point to the old function to
29725 actually forward to the new function. */
29726 emit_insn (gen_nop ());
29727 emit_insn (gen_nop ());
29728 emit_insn (gen_nop ());
29729 emit_insn (gen_nop ());
29730 emit_insn (gen_nop ());
29733 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
29735 reg_mode = V2SImode;
29736 reg_size = 8;
29739 /* Handle world saves specially here. */
29740 if (WORLD_SAVE_P (info))
29742 int i, j, sz;
29743 rtx treg;
29744 rtvec p;
29745 rtx reg0;
29747 /* save_world expects lr in r0. */
29748 reg0 = gen_rtx_REG (Pmode, 0);
29749 if (info->lr_save_p)
29751 insn = emit_move_insn (reg0,
29752 gen_rtx_REG (Pmode, LR_REGNO));
29753 RTX_FRAME_RELATED_P (insn) = 1;
29756 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
29757 assumptions about the offsets of various bits of the stack
29758 frame. */
29759 gcc_assert (info->gp_save_offset == -220
29760 && info->fp_save_offset == -144
29761 && info->lr_save_offset == 8
29762 && info->cr_save_offset == 4
29763 && info->push_p
29764 && info->lr_save_p
29765 && (!crtl->calls_eh_return
29766 || info->ehrd_offset == -432)
29767 && info->vrsave_save_offset == -224
29768 && info->altivec_save_offset == -416);
29770 treg = gen_rtx_REG (SImode, 11);
29771 emit_move_insn (treg, GEN_INT (-info->total_size));
29773 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
29774 in R11. It also clobbers R12, so beware! */
29776 /* Preserve CR2 for save_world prologues */
29777 sz = 5;
29778 sz += 32 - info->first_gp_reg_save;
29779 sz += 64 - info->first_fp_reg_save;
29780 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
29781 p = rtvec_alloc (sz);
29782 j = 0;
29783 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
29784 gen_rtx_REG (SImode,
29785 LR_REGNO));
29786 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
29787 gen_rtx_SYMBOL_REF (Pmode,
29788 "*save_world"));
29789 /* We do floats first so that the instruction pattern matches
29790 properly. */
29791 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
29792 RTVEC_ELT (p, j++)
29793 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
29794 ? DFmode : SFmode,
29795 info->first_fp_reg_save + i),
29796 frame_reg_rtx,
29797 info->fp_save_offset + frame_off + 8 * i);
29798 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
29799 RTVEC_ELT (p, j++)
29800 = gen_frame_store (gen_rtx_REG (V4SImode,
29801 info->first_altivec_reg_save + i),
29802 frame_reg_rtx,
29803 info->altivec_save_offset + frame_off + 16 * i);
29804 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
29805 RTVEC_ELT (p, j++)
29806 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
29807 frame_reg_rtx,
29808 info->gp_save_offset + frame_off + reg_size * i);
29810 /* CR register traditionally saved as CR2. */
29811 RTVEC_ELT (p, j++)
29812 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
29813 frame_reg_rtx, info->cr_save_offset + frame_off);
29814 /* Explain about use of R0. */
29815 if (info->lr_save_p)
29816 RTVEC_ELT (p, j++)
29817 = gen_frame_store (reg0,
29818 frame_reg_rtx, info->lr_save_offset + frame_off);
29819 /* Explain what happens to the stack pointer. */
29821 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
29822 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
29825 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
29826 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
29827 treg, GEN_INT (-info->total_size));
29828 sp_off = frame_off = info->total_size;
29831 strategy = info->savres_strategy;
29833 /* For V.4, update stack before we do any saving and set back pointer. */
29834 if (! WORLD_SAVE_P (info)
29835 && info->push_p
29836 && (DEFAULT_ABI == ABI_V4
29837 || crtl->calls_eh_return))
29839 bool need_r11 = (TARGET_SPE
29840 ? (!(strategy & SAVE_INLINE_GPRS)
29841 && info->spe_64bit_regs_used == 0)
29842 : (!(strategy & SAVE_INLINE_FPRS)
29843 || !(strategy & SAVE_INLINE_GPRS)
29844 || !(strategy & SAVE_INLINE_VRS)));
29845 int ptr_regno = -1;
29846 rtx ptr_reg = NULL_RTX;
29847 int ptr_off = 0;
29849 if (info->total_size < 32767)
29850 frame_off = info->total_size;
29851 else if (need_r11)
29852 ptr_regno = 11;
29853 else if (info->cr_save_p
29854 || info->lr_save_p
29855 || info->first_fp_reg_save < 64
29856 || info->first_gp_reg_save < 32
29857 || info->altivec_size != 0
29858 || info->vrsave_size != 0
29859 || crtl->calls_eh_return)
29860 ptr_regno = 12;
29861 else
29863 /* The prologue won't be saving any regs so there is no need
29864 to set up a frame register to access any frame save area.
29865 We also won't be using frame_off anywhere below, but set
29866 the correct value anyway to protect against future
29867 changes to this function. */
29868 frame_off = info->total_size;
29870 if (ptr_regno != -1)
29872 /* Set up the frame offset to that needed by the first
29873 out-of-line save function. */
29874 START_USE (ptr_regno);
29875 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
29876 frame_reg_rtx = ptr_reg;
29877 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
29878 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
29879 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
29880 ptr_off = info->gp_save_offset + info->gp_size;
29881 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
29882 ptr_off = info->altivec_save_offset + info->altivec_size;
29883 frame_off = -ptr_off;
29885 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
29886 ptr_reg, ptr_off);
29887 if (REGNO (frame_reg_rtx) == 12)
29888 sp_adjust = 0;
29889 sp_off = info->total_size;
29890 if (frame_reg_rtx != sp_reg_rtx)
29891 rs6000_emit_stack_tie (frame_reg_rtx, false);
29894 /* If we use the link register, get it into r0. */
29895 if (!WORLD_SAVE_P (info) && info->lr_save_p
29896 && !cfun->machine->lr_is_wrapped_separately)
29898 rtx addr, reg, mem;
29900 reg = gen_rtx_REG (Pmode, 0);
29901 START_USE (0);
29902 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
29903 RTX_FRAME_RELATED_P (insn) = 1;
29905 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
29906 | SAVE_NOINLINE_FPRS_SAVES_LR)))
29908 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
29909 GEN_INT (info->lr_save_offset + frame_off));
29910 mem = gen_rtx_MEM (Pmode, addr);
29911 /* This should not be of rs6000_sr_alias_set, because of
29912 __builtin_return_address. */
29914 insn = emit_move_insn (mem, reg);
29915 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
29916 NULL_RTX, NULL_RTX);
29917 END_USE (0);
29921 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
29922 r12 will be needed by out-of-line gpr restore. */
29923 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29924 && !(strategy & (SAVE_INLINE_GPRS
29925 | SAVE_NOINLINE_GPRS_SAVES_LR))
29926 ? 11 : 12);
29927 if (!WORLD_SAVE_P (info)
29928 && info->cr_save_p
29929 && REGNO (frame_reg_rtx) != cr_save_regno
29930 && !(using_static_chain_p && cr_save_regno == 11)
29931 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
29933 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
29934 START_USE (cr_save_regno);
29935 rs6000_emit_move_from_cr (cr_save_rtx);
29938 /* Do any required saving of fpr's. If only one or two to save, do
29939 it ourselves. Otherwise, call function. */
29940 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
29942 int offset = info->fp_save_offset + frame_off;
29943 for (int i = info->first_fp_reg_save; i < 64; i++)
29945 if (save_reg_p (i)
29946 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
29947 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
29948 sp_off - frame_off);
29950 offset += fp_reg_size;
29953 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
29955 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
29956 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
29957 unsigned ptr_regno = ptr_regno_for_savres (sel);
29958 rtx ptr_reg = frame_reg_rtx;
29960 if (REGNO (frame_reg_rtx) == ptr_regno)
29961 gcc_checking_assert (frame_off == 0);
29962 else
29964 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
29965 NOT_INUSE (ptr_regno);
29966 emit_insn (gen_add3_insn (ptr_reg,
29967 frame_reg_rtx, GEN_INT (frame_off)));
29969 insn = rs6000_emit_savres_rtx (info, ptr_reg,
29970 info->fp_save_offset,
29971 info->lr_save_offset,
29972 DFmode, sel);
29973 rs6000_frame_related (insn, ptr_reg, sp_off,
29974 NULL_RTX, NULL_RTX);
29975 if (lr)
29976 END_USE (0);
29979 /* Save GPRs. This is done as a PARALLEL if we are using
29980 the store-multiple instructions. */
29981 if (!WORLD_SAVE_P (info)
29982 && TARGET_SPE_ABI
29983 && info->spe_64bit_regs_used != 0
29984 && info->first_gp_reg_save != 32)
29986 int i;
29987 rtx spe_save_area_ptr;
29988 HOST_WIDE_INT save_off;
29989 int ool_adjust = 0;
29991 /* Determine whether we can address all of the registers that need
29992 to be saved with an offset from frame_reg_rtx that fits in
29993 the small const field for SPE memory instructions. */
29994 int spe_regs_addressable
29995 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
29996 + reg_size * (32 - info->first_gp_reg_save - 1))
29997 && (strategy & SAVE_INLINE_GPRS));
29999 if (spe_regs_addressable)
30001 spe_save_area_ptr = frame_reg_rtx;
30002 save_off = frame_off;
30004 else
30006 /* Make r11 point to the start of the SPE save area. We need
30007 to be careful here if r11 is holding the static chain. If
30008 it is, then temporarily save it in r0. */
30009 HOST_WIDE_INT offset;
30011 if (!(strategy & SAVE_INLINE_GPRS))
30012 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
30013 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
30014 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
30015 save_off = frame_off - offset;
30017 if (using_static_chain_p)
30019 rtx r0 = gen_rtx_REG (Pmode, 0);
30021 START_USE (0);
30022 gcc_assert (info->first_gp_reg_save > 11);
30024 emit_move_insn (r0, spe_save_area_ptr);
30026 else if (REGNO (frame_reg_rtx) != 11)
30027 START_USE (11);
30029 emit_insn (gen_addsi3 (spe_save_area_ptr,
30030 frame_reg_rtx, GEN_INT (offset)));
30031 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
30032 frame_off = -info->spe_gp_save_offset + ool_adjust;
30035 if ((strategy & SAVE_INLINE_GPRS))
30037 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
30038 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
30039 emit_frame_save (spe_save_area_ptr, reg_mode,
30040 info->first_gp_reg_save + i,
30041 (info->spe_gp_save_offset + save_off
30042 + reg_size * i),
30043 sp_off - save_off);
30045 else
30047 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
30048 info->spe_gp_save_offset + save_off,
30049 0, reg_mode,
30050 SAVRES_SAVE | SAVRES_GPR);
30052 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
30053 NULL_RTX, NULL_RTX);
30056 /* Move the static chain pointer back. */
30057 if (!spe_regs_addressable)
30059 if (using_static_chain_p)
30061 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
30062 END_USE (0);
30064 else if (REGNO (frame_reg_rtx) != 11)
30065 END_USE (11);
30068 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
30070 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
30071 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
30072 unsigned ptr_regno = ptr_regno_for_savres (sel);
30073 rtx ptr_reg = frame_reg_rtx;
30074 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
30075 int end_save = info->gp_save_offset + info->gp_size;
30076 int ptr_off;
30078 if (ptr_regno == 12)
30079 sp_adjust = 0;
30080 if (!ptr_set_up)
30081 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
30083 /* Need to adjust r11 (r12) if we saved any FPRs. */
30084 if (end_save + frame_off != 0)
30086 rtx offset = GEN_INT (end_save + frame_off);
30088 if (ptr_set_up)
30089 frame_off = -end_save;
30090 else
30091 NOT_INUSE (ptr_regno);
30092 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
30094 else if (!ptr_set_up)
30096 NOT_INUSE (ptr_regno);
30097 emit_move_insn (ptr_reg, frame_reg_rtx);
30099 ptr_off = -end_save;
30100 insn = rs6000_emit_savres_rtx (info, ptr_reg,
30101 info->gp_save_offset + ptr_off,
30102 info->lr_save_offset + ptr_off,
30103 reg_mode, sel);
30104 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
30105 NULL_RTX, NULL_RTX);
30106 if (lr)
30107 END_USE (0);
30109 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
30111 rtvec p;
30112 int i;
30113 p = rtvec_alloc (32 - info->first_gp_reg_save);
30114 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
30115 RTVEC_ELT (p, i)
30116 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
30117 frame_reg_rtx,
30118 info->gp_save_offset + frame_off + reg_size * i);
30119 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
30120 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
30121 NULL_RTX, NULL_RTX);
30123 else if (!WORLD_SAVE_P (info))
30125 int offset = info->gp_save_offset + frame_off;
30126 for (int i = info->first_gp_reg_save; i < 32; i++)
30128 if (rs6000_reg_live_or_pic_offset_p (i)
30129 && !cfun->machine->gpr_is_wrapped_separately[i])
30130 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
30131 sp_off - frame_off);
30133 offset += reg_size;
30137 if (crtl->calls_eh_return)
30139 unsigned int i;
30140 rtvec p;
30142 for (i = 0; ; ++i)
30144 unsigned int regno = EH_RETURN_DATA_REGNO (i);
30145 if (regno == INVALID_REGNUM)
30146 break;
30149 p = rtvec_alloc (i);
30151 for (i = 0; ; ++i)
30153 unsigned int regno = EH_RETURN_DATA_REGNO (i);
30154 if (regno == INVALID_REGNUM)
30155 break;
30157 rtx set
30158 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
30159 sp_reg_rtx,
30160 info->ehrd_offset + sp_off + reg_size * (int) i);
30161 RTVEC_ELT (p, i) = set;
30162 RTX_FRAME_RELATED_P (set) = 1;
30165 insn = emit_insn (gen_blockage ());
30166 RTX_FRAME_RELATED_P (insn) = 1;
30167 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
30170 /* In AIX ABI we need to make sure r2 is really saved. */
30171 if (TARGET_AIX && crtl->calls_eh_return)
30173 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
30174 rtx join_insn, note;
30175 rtx_insn *save_insn;
30176 long toc_restore_insn;
30178 tmp_reg = gen_rtx_REG (Pmode, 11);
30179 tmp_reg_si = gen_rtx_REG (SImode, 11);
30180 if (using_static_chain_p)
30182 START_USE (0);
30183 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
30185 else
30186 START_USE (11);
30187 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
30188 /* Peek at instruction to which this function returns. If it's
30189 restoring r2, then we know we've already saved r2. We can't
30190 unconditionally save r2 because the value we have will already
30191 be updated if we arrived at this function via a plt call or
30192 toc adjusting stub. */
30193 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
30194 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
30195 + RS6000_TOC_SAVE_SLOT);
30196 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
30197 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
30198 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
30199 validate_condition_mode (EQ, CCUNSmode);
30200 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
30201 emit_insn (gen_rtx_SET (compare_result,
30202 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
30203 toc_save_done = gen_label_rtx ();
30204 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
30205 gen_rtx_EQ (VOIDmode, compare_result,
30206 const0_rtx),
30207 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
30208 pc_rtx);
30209 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
30210 JUMP_LABEL (jump) = toc_save_done;
30211 LABEL_NUSES (toc_save_done) += 1;
30213 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
30214 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
30215 sp_off - frame_off);
30217 emit_label (toc_save_done);
30219 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
30220 have a CFG that has different saves along different paths.
30221 Move the note to a dummy blockage insn, which describes that
30222 R2 is unconditionally saved after the label. */
30223 /* ??? An alternate representation might be a special insn pattern
30224 containing both the branch and the store. That might let the
30225 code that minimizes the number of DW_CFA_advance opcodes better
30226 freedom in placing the annotations. */
30227 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
30228 if (note)
30229 remove_note (save_insn, note);
30230 else
30231 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
30232 copy_rtx (PATTERN (save_insn)), NULL_RTX);
30233 RTX_FRAME_RELATED_P (save_insn) = 0;
30235 join_insn = emit_insn (gen_blockage ());
30236 REG_NOTES (join_insn) = note;
30237 RTX_FRAME_RELATED_P (join_insn) = 1;
30239 if (using_static_chain_p)
30241 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
30242 END_USE (0);
30244 else
30245 END_USE (11);
30248 /* Save CR if we use any that must be preserved. */
30249 if (!WORLD_SAVE_P (info) && info->cr_save_p)
30251 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
30252 GEN_INT (info->cr_save_offset + frame_off));
30253 rtx mem = gen_frame_mem (SImode, addr);
30255 /* If we didn't copy cr before, do so now using r0. */
30256 if (cr_save_rtx == NULL_RTX)
30258 START_USE (0);
30259 cr_save_rtx = gen_rtx_REG (SImode, 0);
30260 rs6000_emit_move_from_cr (cr_save_rtx);
30263 /* Saving CR requires a two-instruction sequence: one instruction
30264 to move the CR to a general-purpose register, and a second
30265 instruction that stores the GPR to memory.
30267 We do not emit any DWARF CFI records for the first of these,
30268 because we cannot properly represent the fact that CR is saved in
30269 a register. One reason is that we cannot express that multiple
30270 CR fields are saved; another reason is that on 64-bit, the size
30271 of the CR register in DWARF (4 bytes) differs from the size of
30272 a general-purpose register.
30274 This means if any intervening instruction were to clobber one of
30275 the call-saved CR fields, we'd have incorrect CFI. To prevent
30276 this from happening, we mark the store to memory as a use of
30277 those CR fields, which prevents any such instruction from being
30278 scheduled in between the two instructions. */
30279 rtx crsave_v[9];
30280 int n_crsave = 0;
30281 int i;
30283 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
30284 for (i = 0; i < 8; i++)
30285 if (save_reg_p (CR0_REGNO + i))
30286 crsave_v[n_crsave++]
30287 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
30289 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
30290 gen_rtvec_v (n_crsave, crsave_v)));
30291 END_USE (REGNO (cr_save_rtx));
30293 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
30294 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
30295 so we need to construct a frame expression manually. */
30296 RTX_FRAME_RELATED_P (insn) = 1;
30298 /* Update address to be stack-pointer relative, like
30299 rs6000_frame_related would do. */
30300 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
30301 GEN_INT (info->cr_save_offset + sp_off));
30302 mem = gen_frame_mem (SImode, addr);
30304 if (DEFAULT_ABI == ABI_ELFv2)
30306 /* In the ELFv2 ABI we generate separate CFI records for each
30307 CR field that was actually saved. They all point to the
30308 same 32-bit stack slot. */
30309 rtx crframe[8];
30310 int n_crframe = 0;
30312 for (i = 0; i < 8; i++)
30313 if (save_reg_p (CR0_REGNO + i))
30315 crframe[n_crframe]
30316 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
30318 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
30319 n_crframe++;
30322 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
30323 gen_rtx_PARALLEL (VOIDmode,
30324 gen_rtvec_v (n_crframe, crframe)));
30326 else
30328 /* In other ABIs, by convention, we use a single CR regnum to
30329 represent the fact that all call-saved CR fields are saved.
30330 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
30331 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
30332 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
30336 /* In the ELFv2 ABI we need to save all call-saved CR fields into
30337 *separate* slots if the routine calls __builtin_eh_return, so
30338 that they can be independently restored by the unwinder. */
30339 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
30341 int i, cr_off = info->ehcr_offset;
30342 rtx crsave;
30344 /* ??? We might get better performance by using multiple mfocrf
30345 instructions. */
30346 crsave = gen_rtx_REG (SImode, 0);
30347 emit_insn (gen_movesi_from_cr (crsave));
30349 for (i = 0; i < 8; i++)
30350 if (!call_used_regs[CR0_REGNO + i])
30352 rtvec p = rtvec_alloc (2);
30353 RTVEC_ELT (p, 0)
30354 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
30355 RTVEC_ELT (p, 1)
30356 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
30358 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
30360 RTX_FRAME_RELATED_P (insn) = 1;
30361 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
30362 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
30363 sp_reg_rtx, cr_off + sp_off));
30365 cr_off += reg_size;
30369 /* Update stack and set back pointer unless this is V.4,
30370 for which it was done previously. */
30371 if (!WORLD_SAVE_P (info) && info->push_p
30372 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
30374 rtx ptr_reg = NULL;
30375 int ptr_off = 0;
30377 /* If saving altivec regs we need to be able to address all save
30378 locations using a 16-bit offset. */
30379 if ((strategy & SAVE_INLINE_VRS) == 0
30380 || (info->altivec_size != 0
30381 && (info->altivec_save_offset + info->altivec_size - 16
30382 + info->total_size - frame_off) > 32767)
30383 || (info->vrsave_size != 0
30384 && (info->vrsave_save_offset
30385 + info->total_size - frame_off) > 32767))
30387 int sel = SAVRES_SAVE | SAVRES_VR;
30388 unsigned ptr_regno = ptr_regno_for_savres (sel);
30390 if (using_static_chain_p
30391 && ptr_regno == STATIC_CHAIN_REGNUM)
30392 ptr_regno = 12;
30393 if (REGNO (frame_reg_rtx) != ptr_regno)
30394 START_USE (ptr_regno);
30395 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
30396 frame_reg_rtx = ptr_reg;
30397 ptr_off = info->altivec_save_offset + info->altivec_size;
30398 frame_off = -ptr_off;
30400 else if (REGNO (frame_reg_rtx) == 1)
30401 frame_off = info->total_size;
30402 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
30403 ptr_reg, ptr_off);
30404 if (REGNO (frame_reg_rtx) == 12)
30405 sp_adjust = 0;
30406 sp_off = info->total_size;
30407 if (frame_reg_rtx != sp_reg_rtx)
30408 rs6000_emit_stack_tie (frame_reg_rtx, false);
30411 /* Set frame pointer, if needed. */
30412 if (frame_pointer_needed)
30414 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
30415 sp_reg_rtx);
30416 RTX_FRAME_RELATED_P (insn) = 1;
30419 /* Save AltiVec registers if needed. Save here because the red zone does
30420 not always include AltiVec registers. */
30421 if (!WORLD_SAVE_P (info)
30422 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
30424 int end_save = info->altivec_save_offset + info->altivec_size;
30425 int ptr_off;
30426 /* Oddly, the vector save/restore functions point r0 at the end
30427 of the save area, then use r11 or r12 to load offsets for
30428 [reg+reg] addressing. */
30429 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
30430 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
30431 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
30433 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
30434 NOT_INUSE (0);
30435 if (scratch_regno == 12)
30436 sp_adjust = 0;
30437 if (end_save + frame_off != 0)
30439 rtx offset = GEN_INT (end_save + frame_off);
30441 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
30443 else
30444 emit_move_insn (ptr_reg, frame_reg_rtx);
30446 ptr_off = -end_save;
30447 insn = rs6000_emit_savres_rtx (info, scratch_reg,
30448 info->altivec_save_offset + ptr_off,
30449 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
30450 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
30451 NULL_RTX, NULL_RTX);
30452 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
30454 /* The oddity mentioned above clobbered our frame reg. */
30455 emit_move_insn (frame_reg_rtx, ptr_reg);
30456 frame_off = ptr_off;
30459 else if (!WORLD_SAVE_P (info)
30460 && info->altivec_size != 0)
30462 int i;
30464 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
30465 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
30467 rtx areg, savereg, mem;
30468 HOST_WIDE_INT offset;
30470 offset = (info->altivec_save_offset + frame_off
30471 + 16 * (i - info->first_altivec_reg_save));
30473 savereg = gen_rtx_REG (V4SImode, i);
30475 if (TARGET_P9_DFORM_VECTOR && quad_address_offset_p (offset))
30477 mem = gen_frame_mem (V4SImode,
30478 gen_rtx_PLUS (Pmode, frame_reg_rtx,
30479 GEN_INT (offset)));
30480 insn = emit_insn (gen_rtx_SET (mem, savereg));
30481 areg = NULL_RTX;
30483 else
30485 NOT_INUSE (0);
30486 areg = gen_rtx_REG (Pmode, 0);
30487 emit_move_insn (areg, GEN_INT (offset));
30489 /* AltiVec addressing mode is [reg+reg]. */
30490 mem = gen_frame_mem (V4SImode,
30491 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
30493 /* Rather than emitting a generic move, force use of the stvx
30494 instruction, which we always want on ISA 2.07 (power8) systems.
30495 In particular we don't want xxpermdi/stxvd2x for little
30496 endian. */
30497 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
30500 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
30501 areg, GEN_INT (offset));
30505 /* VRSAVE is a bit vector representing which AltiVec registers
30506 are used. The OS uses this to determine which vector
30507 registers to save on a context switch. We need to save
30508 VRSAVE on the stack frame, add whatever AltiVec registers we
30509 used in this function, and do the corresponding magic in the
30510 epilogue. */
30512 if (!WORLD_SAVE_P (info)
30513 && info->vrsave_size != 0)
30515 rtx reg, vrsave;
30516 int offset;
30517 int save_regno;
30519 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
30520 be using r12 as frame_reg_rtx and r11 as the static chain
30521 pointer for nested functions. */
30522 save_regno = 12;
30523 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
30524 && !using_static_chain_p)
30525 save_regno = 11;
30526 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
30528 save_regno = 11;
30529 if (using_static_chain_p)
30530 save_regno = 0;
30533 NOT_INUSE (save_regno);
30534 reg = gen_rtx_REG (SImode, save_regno);
30535 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
30536 if (TARGET_MACHO)
30537 emit_insn (gen_get_vrsave_internal (reg));
30538 else
30539 emit_insn (gen_rtx_SET (reg, vrsave));
30541 /* Save VRSAVE. */
30542 offset = info->vrsave_save_offset + frame_off;
30543 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
30545 /* Include the registers in the mask. */
30546 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
30548 insn = emit_insn (generate_set_vrsave (reg, info, 0));
30551 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
30552 if (!TARGET_SINGLE_PIC_BASE
30553 && ((TARGET_TOC && TARGET_MINIMAL_TOC
30554 && !constant_pool_empty_p ())
30555 || (DEFAULT_ABI == ABI_V4
30556 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
30557 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
30559 /* If emit_load_toc_table will use the link register, we need to save
30560 it. We use R12 for this purpose because emit_load_toc_table
30561 can use register 0. This allows us to use a plain 'blr' to return
30562 from the procedure more often. */
30563 int save_LR_around_toc_setup = (TARGET_ELF
30564 && DEFAULT_ABI == ABI_V4
30565 && flag_pic
30566 && ! info->lr_save_p
30567 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
30568 if (save_LR_around_toc_setup)
30570 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
30571 rtx tmp = gen_rtx_REG (Pmode, 12);
30573 sp_adjust = 0;
30574 insn = emit_move_insn (tmp, lr);
30575 RTX_FRAME_RELATED_P (insn) = 1;
30577 rs6000_emit_load_toc_table (TRUE);
30579 insn = emit_move_insn (lr, tmp);
30580 add_reg_note (insn, REG_CFA_RESTORE, lr);
30581 RTX_FRAME_RELATED_P (insn) = 1;
30583 else
30584 rs6000_emit_load_toc_table (TRUE);
30587 #if TARGET_MACHO
30588 if (!TARGET_SINGLE_PIC_BASE
30589 && DEFAULT_ABI == ABI_DARWIN
30590 && flag_pic && crtl->uses_pic_offset_table)
30592 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
30593 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
30595 /* Save and restore LR locally around this call (in R0). */
30596 if (!info->lr_save_p)
30597 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
30599 emit_insn (gen_load_macho_picbase (src));
30601 emit_move_insn (gen_rtx_REG (Pmode,
30602 RS6000_PIC_OFFSET_TABLE_REGNUM),
30603 lr);
30605 if (!info->lr_save_p)
30606 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
30608 #endif
30610 /* If we need to, save the TOC register after doing the stack setup.
30611 Do not emit eh frame info for this save. The unwinder wants info,
30612 conceptually attached to instructions in this function, about
30613 register values in the caller of this function. This R2 may have
30614 already been changed from the value in the caller.
30615 We don't attempt to write accurate DWARF EH frame info for R2
30616 because code emitted by gcc for a (non-pointer) function call
30617 doesn't save and restore R2. Instead, R2 is managed out-of-line
30618 by a linker generated plt call stub when the function resides in
30619 a shared library. This behavior is costly to describe in DWARF,
30620 both in terms of the size of DWARF info and the time taken in the
30621 unwinder to interpret it. R2 changes, apart from the
30622 calls_eh_return case earlier in this function, are handled by
30623 linux-unwind.h frob_update_context. */
30624 if (rs6000_save_toc_in_prologue_p ())
30626 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
30627 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
30630 if (using_split_stack && split_stack_arg_pointer_used_p ())
30632 /* Set up the arg pointer (r12) for -fsplit-stack code. If
30633 __morestack was called, it left the arg pointer to the old
30634 stack in r29. Otherwise, the arg pointer is the top of the
30635 current frame. */
30636 cfun->machine->split_stack_argp_used = true;
30637 if (sp_adjust)
30639 rtx r12 = gen_rtx_REG (Pmode, 12);
30640 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
30641 emit_insn_before (set_r12, sp_adjust);
30643 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
30645 rtx r12 = gen_rtx_REG (Pmode, 12);
30646 if (frame_off == 0)
30647 emit_move_insn (r12, frame_reg_rtx);
30648 else
30649 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
30651 if (info->push_p)
30653 rtx r12 = gen_rtx_REG (Pmode, 12);
30654 rtx r29 = gen_rtx_REG (Pmode, 29);
30655 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
30656 rtx not_more = gen_label_rtx ();
30657 rtx jump;
30659 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
30660 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
30661 gen_rtx_LABEL_REF (VOIDmode, not_more),
30662 pc_rtx);
30663 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
30664 JUMP_LABEL (jump) = not_more;
30665 LABEL_NUSES (not_more) += 1;
30666 emit_move_insn (r12, r29);
30667 emit_label (not_more);
30672 /* Output .extern statements for the save/restore routines we use. */
30674 static void
30675 rs6000_output_savres_externs (FILE *file)
30677 rs6000_stack_t *info = rs6000_stack_info ();
30679 if (TARGET_DEBUG_STACK)
30680 debug_stack_info (info);
30682 /* Write .extern for any function we will call to save and restore
30683 fp values. */
30684 if (info->first_fp_reg_save < 64
30685 && !TARGET_MACHO
30686 && !TARGET_ELF)
30688 char *name;
30689 int regno = info->first_fp_reg_save - 32;
30691 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
30693 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
30694 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
30695 name = rs6000_savres_routine_name (info, regno, sel);
30696 fprintf (file, "\t.extern %s\n", name);
30698 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
30700 bool lr = (info->savres_strategy
30701 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
30702 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
30703 name = rs6000_savres_routine_name (info, regno, sel);
30704 fprintf (file, "\t.extern %s\n", name);
30709 /* Write function prologue. */
30711 static void
30712 rs6000_output_function_prologue (FILE *file)
30714 if (!cfun->is_thunk)
30715 rs6000_output_savres_externs (file);
30717 /* ELFv2 ABI r2 setup code and local entry point. This must follow
30718 immediately after the global entry point label. */
30719 if (rs6000_global_entry_point_needed_p ())
30721 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
30723 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
30725 if (TARGET_CMODEL != CMODEL_LARGE)
30727 /* In the small and medium code models, we assume the TOC is less
30728 2 GB away from the text section, so it can be computed via the
30729 following two-instruction sequence. */
30730 char buf[256];
30732 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
30733 fprintf (file, "0:\taddis 2,12,.TOC.-");
30734 assemble_name (file, buf);
30735 fprintf (file, "@ha\n");
30736 fprintf (file, "\taddi 2,2,.TOC.-");
30737 assemble_name (file, buf);
30738 fprintf (file, "@l\n");
30740 else
30742 /* In the large code model, we allow arbitrary offsets between the
30743 TOC and the text section, so we have to load the offset from
30744 memory. The data field is emitted directly before the global
30745 entry point in rs6000_elf_declare_function_name. */
30746 char buf[256];
30748 #ifdef HAVE_AS_ENTRY_MARKERS
30749 /* If supported by the linker, emit a marker relocation. If the
30750 total code size of the final executable or shared library
30751 happens to fit into 2 GB after all, the linker will replace
30752 this code sequence with the sequence for the small or medium
30753 code model. */
30754 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
30755 #endif
30756 fprintf (file, "\tld 2,");
30757 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
30758 assemble_name (file, buf);
30759 fprintf (file, "-");
30760 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
30761 assemble_name (file, buf);
30762 fprintf (file, "(12)\n");
30763 fprintf (file, "\tadd 2,2,12\n");
30766 fputs ("\t.localentry\t", file);
30767 assemble_name (file, name);
30768 fputs (",.-", file);
30769 assemble_name (file, name);
30770 fputs ("\n", file);
30773 /* Output -mprofile-kernel code. This needs to be done here instead of
30774 in output_function_profile since it must go after the ELFv2 ABI
30775 local entry point. */
30776 if (TARGET_PROFILE_KERNEL && crtl->profile)
30778 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
30779 gcc_assert (!TARGET_32BIT);
30781 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
30783 /* In the ELFv2 ABI we have no compiler stack word. It must be
30784 the resposibility of _mcount to preserve the static chain
30785 register if required. */
30786 if (DEFAULT_ABI != ABI_ELFv2
30787 && cfun->static_chain_decl != NULL)
30789 asm_fprintf (file, "\tstd %s,24(%s)\n",
30790 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
30791 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
30792 asm_fprintf (file, "\tld %s,24(%s)\n",
30793 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
30795 else
30796 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
30799 rs6000_pic_labelno++;
30802 /* -mprofile-kernel code calls mcount before the function prolog,
30803 so a profiled leaf function should stay a leaf function. */
30804 static bool
30805 rs6000_keep_leaf_when_profiled ()
30807 return TARGET_PROFILE_KERNEL;
30810 /* Non-zero if vmx regs are restored before the frame pop, zero if
30811 we restore after the pop when possible. */
30812 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
30814 /* Restoring cr is a two step process: loading a reg from the frame
30815 save, then moving the reg to cr. For ABI_V4 we must let the
30816 unwinder know that the stack location is no longer valid at or
30817 before the stack deallocation, but we can't emit a cfa_restore for
30818 cr at the stack deallocation like we do for other registers.
30819 The trouble is that it is possible for the move to cr to be
30820 scheduled after the stack deallocation. So say exactly where cr
30821 is located on each of the two insns. */
30823 static rtx
30824 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
30826 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
30827 rtx reg = gen_rtx_REG (SImode, regno);
30828 rtx_insn *insn = emit_move_insn (reg, mem);
30830 if (!exit_func && DEFAULT_ABI == ABI_V4)
30832 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
30833 rtx set = gen_rtx_SET (reg, cr);
30835 add_reg_note (insn, REG_CFA_REGISTER, set);
30836 RTX_FRAME_RELATED_P (insn) = 1;
30838 return reg;
30841 /* Reload CR from REG. */
30843 static void
30844 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
30846 int count = 0;
30847 int i;
30849 if (using_mfcr_multiple)
30851 for (i = 0; i < 8; i++)
30852 if (save_reg_p (CR0_REGNO + i))
30853 count++;
30854 gcc_assert (count);
30857 if (using_mfcr_multiple && count > 1)
30859 rtx_insn *insn;
30860 rtvec p;
30861 int ndx;
30863 p = rtvec_alloc (count);
30865 ndx = 0;
30866 for (i = 0; i < 8; i++)
30867 if (save_reg_p (CR0_REGNO + i))
30869 rtvec r = rtvec_alloc (2);
30870 RTVEC_ELT (r, 0) = reg;
30871 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
30872 RTVEC_ELT (p, ndx) =
30873 gen_rtx_SET (gen_rtx_REG (CCmode, CR0_REGNO + i),
30874 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
30875 ndx++;
30877 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
30878 gcc_assert (ndx == count);
30880 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
30881 CR field separately. */
30882 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
30884 for (i = 0; i < 8; i++)
30885 if (save_reg_p (CR0_REGNO + i))
30886 add_reg_note (insn, REG_CFA_RESTORE,
30887 gen_rtx_REG (SImode, CR0_REGNO + i));
30889 RTX_FRAME_RELATED_P (insn) = 1;
30892 else
30893 for (i = 0; i < 8; i++)
30894 if (save_reg_p (CR0_REGNO + i))
30896 rtx insn = emit_insn (gen_movsi_to_cr_one
30897 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
30899 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
30900 CR field separately, attached to the insn that in fact
30901 restores this particular CR field. */
30902 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
30904 add_reg_note (insn, REG_CFA_RESTORE,
30905 gen_rtx_REG (SImode, CR0_REGNO + i));
30907 RTX_FRAME_RELATED_P (insn) = 1;
30911 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
30912 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
30913 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
30915 rtx_insn *insn = get_last_insn ();
30916 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
30918 add_reg_note (insn, REG_CFA_RESTORE, cr);
30919 RTX_FRAME_RELATED_P (insn) = 1;
30923 /* Like cr, the move to lr instruction can be scheduled after the
30924 stack deallocation, but unlike cr, its stack frame save is still
30925 valid. So we only need to emit the cfa_restore on the correct
30926 instruction. */
30928 static void
30929 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
30931 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
30932 rtx reg = gen_rtx_REG (Pmode, regno);
30934 emit_move_insn (reg, mem);
30937 static void
30938 restore_saved_lr (int regno, bool exit_func)
30940 rtx reg = gen_rtx_REG (Pmode, regno);
30941 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
30942 rtx_insn *insn = emit_move_insn (lr, reg);
30944 if (!exit_func && flag_shrink_wrap)
30946 add_reg_note (insn, REG_CFA_RESTORE, lr);
30947 RTX_FRAME_RELATED_P (insn) = 1;
30951 static rtx
30952 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
30954 if (DEFAULT_ABI == ABI_ELFv2)
30956 int i;
30957 for (i = 0; i < 8; i++)
30958 if (save_reg_p (CR0_REGNO + i))
30960 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
30961 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
30962 cfa_restores);
30965 else if (info->cr_save_p)
30966 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
30967 gen_rtx_REG (SImode, CR2_REGNO),
30968 cfa_restores);
30970 if (info->lr_save_p)
30971 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
30972 gen_rtx_REG (Pmode, LR_REGNO),
30973 cfa_restores);
30974 return cfa_restores;
30977 /* Return true if OFFSET from stack pointer can be clobbered by signals.
30978 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
30979 below stack pointer not cloberred by signals. */
30981 static inline bool
30982 offset_below_red_zone_p (HOST_WIDE_INT offset)
30984 return offset < (DEFAULT_ABI == ABI_V4
30986 : TARGET_32BIT ? -220 : -288);
30989 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
30991 static void
30992 emit_cfa_restores (rtx cfa_restores)
30994 rtx_insn *insn = get_last_insn ();
30995 rtx *loc = &REG_NOTES (insn);
30997 while (*loc)
30998 loc = &XEXP (*loc, 1);
30999 *loc = cfa_restores;
31000 RTX_FRAME_RELATED_P (insn) = 1;
31003 /* Emit function epilogue as insns. */
31005 void
31006 rs6000_emit_epilogue (int sibcall)
31008 rs6000_stack_t *info;
31009 int restoring_GPRs_inline;
31010 int restoring_FPRs_inline;
31011 int using_load_multiple;
31012 int using_mtcr_multiple;
31013 int use_backchain_to_restore_sp;
31014 int restore_lr;
31015 int strategy;
31016 HOST_WIDE_INT frame_off = 0;
31017 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
31018 rtx frame_reg_rtx = sp_reg_rtx;
31019 rtx cfa_restores = NULL_RTX;
31020 rtx insn;
31021 rtx cr_save_reg = NULL_RTX;
31022 machine_mode reg_mode = Pmode;
31023 int reg_size = TARGET_32BIT ? 4 : 8;
31024 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
31025 ? DFmode : SFmode;
31026 int fp_reg_size = 8;
31027 int i;
31028 bool exit_func;
31029 unsigned ptr_regno;
31031 info = rs6000_stack_info ();
31033 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
31035 reg_mode = V2SImode;
31036 reg_size = 8;
31039 strategy = info->savres_strategy;
31040 using_load_multiple = strategy & REST_MULTIPLE;
31041 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
31042 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
31043 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
31044 || rs6000_cpu == PROCESSOR_PPC603
31045 || rs6000_cpu == PROCESSOR_PPC750
31046 || optimize_size);
31047 /* Restore via the backchain when we have a large frame, since this
31048 is more efficient than an addis, addi pair. The second condition
31049 here will not trigger at the moment; We don't actually need a
31050 frame pointer for alloca, but the generic parts of the compiler
31051 give us one anyway. */
31052 use_backchain_to_restore_sp = (info->total_size + (info->lr_save_p
31053 ? info->lr_save_offset
31054 : 0) > 32767
31055 || (cfun->calls_alloca
31056 && !frame_pointer_needed));
31057 restore_lr = (info->lr_save_p
31058 && (restoring_FPRs_inline
31059 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
31060 && (restoring_GPRs_inline
31061 || info->first_fp_reg_save < 64)
31062 && !cfun->machine->lr_is_wrapped_separately);
31065 if (WORLD_SAVE_P (info))
31067 int i, j;
31068 char rname[30];
31069 const char *alloc_rname;
31070 rtvec p;
31072 /* eh_rest_world_r10 will return to the location saved in the LR
31073 stack slot (which is not likely to be our caller.)
31074 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
31075 rest_world is similar, except any R10 parameter is ignored.
31076 The exception-handling stuff that was here in 2.95 is no
31077 longer necessary. */
31079 p = rtvec_alloc (9
31080 + 32 - info->first_gp_reg_save
31081 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
31082 + 63 + 1 - info->first_fp_reg_save);
31084 strcpy (rname, ((crtl->calls_eh_return) ?
31085 "*eh_rest_world_r10" : "*rest_world"));
31086 alloc_rname = ggc_strdup (rname);
31088 j = 0;
31089 RTVEC_ELT (p, j++) = ret_rtx;
31090 RTVEC_ELT (p, j++)
31091 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
31092 /* The instruction pattern requires a clobber here;
31093 it is shared with the restVEC helper. */
31094 RTVEC_ELT (p, j++)
31095 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
31098 /* CR register traditionally saved as CR2. */
31099 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
31100 RTVEC_ELT (p, j++)
31101 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
31102 if (flag_shrink_wrap)
31104 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
31105 gen_rtx_REG (Pmode, LR_REGNO),
31106 cfa_restores);
31107 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31111 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
31113 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
31114 RTVEC_ELT (p, j++)
31115 = gen_frame_load (reg,
31116 frame_reg_rtx, info->gp_save_offset + reg_size * i);
31117 if (flag_shrink_wrap)
31118 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31120 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
31122 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
31123 RTVEC_ELT (p, j++)
31124 = gen_frame_load (reg,
31125 frame_reg_rtx, info->altivec_save_offset + 16 * i);
31126 if (flag_shrink_wrap)
31127 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31129 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
31131 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
31132 ? DFmode : SFmode),
31133 info->first_fp_reg_save + i);
31134 RTVEC_ELT (p, j++)
31135 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
31136 if (flag_shrink_wrap)
31137 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31139 RTVEC_ELT (p, j++)
31140 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
31141 RTVEC_ELT (p, j++)
31142 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
31143 RTVEC_ELT (p, j++)
31144 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
31145 RTVEC_ELT (p, j++)
31146 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
31147 RTVEC_ELT (p, j++)
31148 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
31149 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
31151 if (flag_shrink_wrap)
31153 REG_NOTES (insn) = cfa_restores;
31154 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
31155 RTX_FRAME_RELATED_P (insn) = 1;
31157 return;
31160 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
31161 if (info->push_p)
31162 frame_off = info->total_size;
31164 /* Restore AltiVec registers if we must do so before adjusting the
31165 stack. */
31166 if (info->altivec_size != 0
31167 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31168 || (DEFAULT_ABI != ABI_V4
31169 && offset_below_red_zone_p (info->altivec_save_offset))))
31171 int i;
31172 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
31174 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
31175 if (use_backchain_to_restore_sp)
31177 int frame_regno = 11;
31179 if ((strategy & REST_INLINE_VRS) == 0)
31181 /* Of r11 and r12, select the one not clobbered by an
31182 out-of-line restore function for the frame register. */
31183 frame_regno = 11 + 12 - scratch_regno;
31185 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
31186 emit_move_insn (frame_reg_rtx,
31187 gen_rtx_MEM (Pmode, sp_reg_rtx));
31188 frame_off = 0;
31190 else if (frame_pointer_needed)
31191 frame_reg_rtx = hard_frame_pointer_rtx;
31193 if ((strategy & REST_INLINE_VRS) == 0)
31195 int end_save = info->altivec_save_offset + info->altivec_size;
31196 int ptr_off;
31197 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
31198 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
31200 if (end_save + frame_off != 0)
31202 rtx offset = GEN_INT (end_save + frame_off);
31204 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
31206 else
31207 emit_move_insn (ptr_reg, frame_reg_rtx);
31209 ptr_off = -end_save;
31210 insn = rs6000_emit_savres_rtx (info, scratch_reg,
31211 info->altivec_save_offset + ptr_off,
31212 0, V4SImode, SAVRES_VR);
31214 else
31216 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31217 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
31219 rtx addr, areg, mem, insn;
31220 rtx reg = gen_rtx_REG (V4SImode, i);
31221 HOST_WIDE_INT offset
31222 = (info->altivec_save_offset + frame_off
31223 + 16 * (i - info->first_altivec_reg_save));
31225 if (TARGET_P9_DFORM_VECTOR && quad_address_offset_p (offset))
31227 mem = gen_frame_mem (V4SImode,
31228 gen_rtx_PLUS (Pmode, frame_reg_rtx,
31229 GEN_INT (offset)));
31230 insn = gen_rtx_SET (reg, mem);
31232 else
31234 areg = gen_rtx_REG (Pmode, 0);
31235 emit_move_insn (areg, GEN_INT (offset));
31237 /* AltiVec addressing mode is [reg+reg]. */
31238 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
31239 mem = gen_frame_mem (V4SImode, addr);
31241 /* Rather than emitting a generic move, force use of the
31242 lvx instruction, which we always want. In particular we
31243 don't want lxvd2x/xxpermdi for little endian. */
31244 insn = gen_altivec_lvx_v4si_internal (reg, mem);
31247 (void) emit_insn (insn);
31251 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31252 if (((strategy & REST_INLINE_VRS) == 0
31253 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
31254 && (flag_shrink_wrap
31255 || (offset_below_red_zone_p
31256 (info->altivec_save_offset
31257 + 16 * (i - info->first_altivec_reg_save)))))
31259 rtx reg = gen_rtx_REG (V4SImode, i);
31260 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31264 /* Restore VRSAVE if we must do so before adjusting the stack. */
31265 if (info->vrsave_size != 0
31266 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31267 || (DEFAULT_ABI != ABI_V4
31268 && offset_below_red_zone_p (info->vrsave_save_offset))))
31270 rtx reg;
31272 if (frame_reg_rtx == sp_reg_rtx)
31274 if (use_backchain_to_restore_sp)
31276 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31277 emit_move_insn (frame_reg_rtx,
31278 gen_rtx_MEM (Pmode, sp_reg_rtx));
31279 frame_off = 0;
31281 else if (frame_pointer_needed)
31282 frame_reg_rtx = hard_frame_pointer_rtx;
31285 reg = gen_rtx_REG (SImode, 12);
31286 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31287 info->vrsave_save_offset + frame_off));
31289 emit_insn (generate_set_vrsave (reg, info, 1));
31292 insn = NULL_RTX;
31293 /* If we have a large stack frame, restore the old stack pointer
31294 using the backchain. */
31295 if (use_backchain_to_restore_sp)
31297 if (frame_reg_rtx == sp_reg_rtx)
31299 /* Under V.4, don't reset the stack pointer until after we're done
31300 loading the saved registers. */
31301 if (DEFAULT_ABI == ABI_V4)
31302 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31304 insn = emit_move_insn (frame_reg_rtx,
31305 gen_rtx_MEM (Pmode, sp_reg_rtx));
31306 frame_off = 0;
31308 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31309 && DEFAULT_ABI == ABI_V4)
31310 /* frame_reg_rtx has been set up by the altivec restore. */
31312 else
31314 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
31315 frame_reg_rtx = sp_reg_rtx;
31318 /* If we have a frame pointer, we can restore the old stack pointer
31319 from it. */
31320 else if (frame_pointer_needed)
31322 frame_reg_rtx = sp_reg_rtx;
31323 if (DEFAULT_ABI == ABI_V4)
31324 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31325 /* Prevent reordering memory accesses against stack pointer restore. */
31326 else if (cfun->calls_alloca
31327 || offset_below_red_zone_p (-info->total_size))
31328 rs6000_emit_stack_tie (frame_reg_rtx, true);
31330 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
31331 GEN_INT (info->total_size)));
31332 frame_off = 0;
31334 else if (info->push_p
31335 && DEFAULT_ABI != ABI_V4
31336 && !crtl->calls_eh_return)
31338 /* Prevent reordering memory accesses against stack pointer restore. */
31339 if (cfun->calls_alloca
31340 || offset_below_red_zone_p (-info->total_size))
31341 rs6000_emit_stack_tie (frame_reg_rtx, false);
31342 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
31343 GEN_INT (info->total_size)));
31344 frame_off = 0;
31346 if (insn && frame_reg_rtx == sp_reg_rtx)
31348 if (cfa_restores)
31350 REG_NOTES (insn) = cfa_restores;
31351 cfa_restores = NULL_RTX;
31353 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
31354 RTX_FRAME_RELATED_P (insn) = 1;
31357 /* Restore AltiVec registers if we have not done so already. */
31358 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31359 && info->altivec_size != 0
31360 && (DEFAULT_ABI == ABI_V4
31361 || !offset_below_red_zone_p (info->altivec_save_offset)))
31363 int i;
31365 if ((strategy & REST_INLINE_VRS) == 0)
31367 int end_save = info->altivec_save_offset + info->altivec_size;
31368 int ptr_off;
31369 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
31370 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
31371 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
31373 if (end_save + frame_off != 0)
31375 rtx offset = GEN_INT (end_save + frame_off);
31377 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
31379 else
31380 emit_move_insn (ptr_reg, frame_reg_rtx);
31382 ptr_off = -end_save;
31383 insn = rs6000_emit_savres_rtx (info, scratch_reg,
31384 info->altivec_save_offset + ptr_off,
31385 0, V4SImode, SAVRES_VR);
31386 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
31388 /* Frame reg was clobbered by out-of-line save. Restore it
31389 from ptr_reg, and if we are calling out-of-line gpr or
31390 fpr restore set up the correct pointer and offset. */
31391 unsigned newptr_regno = 1;
31392 if (!restoring_GPRs_inline)
31394 bool lr = info->gp_save_offset + info->gp_size == 0;
31395 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
31396 newptr_regno = ptr_regno_for_savres (sel);
31397 end_save = info->gp_save_offset + info->gp_size;
31399 else if (!restoring_FPRs_inline)
31401 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
31402 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
31403 newptr_regno = ptr_regno_for_savres (sel);
31404 end_save = info->fp_save_offset + info->fp_size;
31407 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
31408 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
31410 if (end_save + ptr_off != 0)
31412 rtx offset = GEN_INT (end_save + ptr_off);
31414 frame_off = -end_save;
31415 if (TARGET_32BIT)
31416 emit_insn (gen_addsi3_carry (frame_reg_rtx,
31417 ptr_reg, offset));
31418 else
31419 emit_insn (gen_adddi3_carry (frame_reg_rtx,
31420 ptr_reg, offset));
31422 else
31424 frame_off = ptr_off;
31425 emit_move_insn (frame_reg_rtx, ptr_reg);
31429 else
31431 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31432 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
31434 rtx addr, areg, mem, insn;
31435 rtx reg = gen_rtx_REG (V4SImode, i);
31436 HOST_WIDE_INT offset
31437 = (info->altivec_save_offset + frame_off
31438 + 16 * (i - info->first_altivec_reg_save));
31440 if (TARGET_P9_DFORM_VECTOR && quad_address_offset_p (offset))
31442 mem = gen_frame_mem (V4SImode,
31443 gen_rtx_PLUS (Pmode, frame_reg_rtx,
31444 GEN_INT (offset)));
31445 insn = gen_rtx_SET (reg, mem);
31447 else
31449 areg = gen_rtx_REG (Pmode, 0);
31450 emit_move_insn (areg, GEN_INT (offset));
31452 /* AltiVec addressing mode is [reg+reg]. */
31453 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
31454 mem = gen_frame_mem (V4SImode, addr);
31456 /* Rather than emitting a generic move, force use of the
31457 lvx instruction, which we always want. In particular we
31458 don't want lxvd2x/xxpermdi for little endian. */
31459 insn = gen_altivec_lvx_v4si_internal (reg, mem);
31462 (void) emit_insn (insn);
31466 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31467 if (((strategy & REST_INLINE_VRS) == 0
31468 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
31469 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
31471 rtx reg = gen_rtx_REG (V4SImode, i);
31472 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31476 /* Restore VRSAVE if we have not done so already. */
31477 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31478 && info->vrsave_size != 0
31479 && (DEFAULT_ABI == ABI_V4
31480 || !offset_below_red_zone_p (info->vrsave_save_offset)))
31482 rtx reg;
31484 reg = gen_rtx_REG (SImode, 12);
31485 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31486 info->vrsave_save_offset + frame_off));
31488 emit_insn (generate_set_vrsave (reg, info, 1));
31491 /* If we exit by an out-of-line restore function on ABI_V4 then that
31492 function will deallocate the stack, so we don't need to worry
31493 about the unwinder restoring cr from an invalid stack frame
31494 location. */
31495 exit_func = (!restoring_FPRs_inline
31496 || (!restoring_GPRs_inline
31497 && info->first_fp_reg_save == 64));
31499 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
31500 *separate* slots if the routine calls __builtin_eh_return, so
31501 that they can be independently restored by the unwinder. */
31502 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
31504 int i, cr_off = info->ehcr_offset;
31506 for (i = 0; i < 8; i++)
31507 if (!call_used_regs[CR0_REGNO + i])
31509 rtx reg = gen_rtx_REG (SImode, 0);
31510 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31511 cr_off + frame_off));
31513 insn = emit_insn (gen_movsi_to_cr_one
31514 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
31516 if (!exit_func && flag_shrink_wrap)
31518 add_reg_note (insn, REG_CFA_RESTORE,
31519 gen_rtx_REG (SImode, CR0_REGNO + i));
31521 RTX_FRAME_RELATED_P (insn) = 1;
31524 cr_off += reg_size;
31528 /* Get the old lr if we saved it. If we are restoring registers
31529 out-of-line, then the out-of-line routines can do this for us. */
31530 if (restore_lr && restoring_GPRs_inline)
31531 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
31533 /* Get the old cr if we saved it. */
31534 if (info->cr_save_p)
31536 unsigned cr_save_regno = 12;
31538 if (!restoring_GPRs_inline)
31540 /* Ensure we don't use the register used by the out-of-line
31541 gpr register restore below. */
31542 bool lr = info->gp_save_offset + info->gp_size == 0;
31543 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
31544 int gpr_ptr_regno = ptr_regno_for_savres (sel);
31546 if (gpr_ptr_regno == 12)
31547 cr_save_regno = 11;
31548 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
31550 else if (REGNO (frame_reg_rtx) == 12)
31551 cr_save_regno = 11;
31553 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
31554 info->cr_save_offset + frame_off,
31555 exit_func);
31558 /* Set LR here to try to overlap restores below. */
31559 if (restore_lr && restoring_GPRs_inline)
31560 restore_saved_lr (0, exit_func);
31562 /* Load exception handler data registers, if needed. */
31563 if (crtl->calls_eh_return)
31565 unsigned int i, regno;
31567 if (TARGET_AIX)
31569 rtx reg = gen_rtx_REG (reg_mode, 2);
31570 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31571 frame_off + RS6000_TOC_SAVE_SLOT));
31574 for (i = 0; ; ++i)
31576 rtx mem;
31578 regno = EH_RETURN_DATA_REGNO (i);
31579 if (regno == INVALID_REGNUM)
31580 break;
31582 /* Note: possible use of r0 here to address SPE regs. */
31583 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
31584 info->ehrd_offset + frame_off
31585 + reg_size * (int) i);
31587 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
31591 /* Restore GPRs. This is done as a PARALLEL if we are using
31592 the load-multiple instructions. */
31593 if (TARGET_SPE_ABI
31594 && info->spe_64bit_regs_used
31595 && info->first_gp_reg_save != 32)
31597 /* Determine whether we can address all of the registers that need
31598 to be saved with an offset from frame_reg_rtx that fits in
31599 the small const field for SPE memory instructions. */
31600 int spe_regs_addressable
31601 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
31602 + reg_size * (32 - info->first_gp_reg_save - 1))
31603 && restoring_GPRs_inline);
31605 if (!spe_regs_addressable)
31607 int ool_adjust = 0;
31608 rtx old_frame_reg_rtx = frame_reg_rtx;
31609 /* Make r11 point to the start of the SPE save area. We worried about
31610 not clobbering it when we were saving registers in the prologue.
31611 There's no need to worry here because the static chain is passed
31612 anew to every function. */
31614 if (!restoring_GPRs_inline)
31615 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
31616 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31617 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
31618 GEN_INT (info->spe_gp_save_offset
31619 + frame_off
31620 - ool_adjust)));
31621 /* Keep the invariant that frame_reg_rtx + frame_off points
31622 at the top of the stack frame. */
31623 frame_off = -info->spe_gp_save_offset + ool_adjust;
31626 if (restoring_GPRs_inline)
31628 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
31630 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
31631 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
31633 rtx offset, addr, mem, reg;
31635 /* We're doing all this to ensure that the immediate offset
31636 fits into the immediate field of 'evldd'. */
31637 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
31639 offset = GEN_INT (spe_offset + reg_size * i);
31640 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
31641 mem = gen_rtx_MEM (V2SImode, addr);
31642 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
31644 emit_move_insn (reg, mem);
31647 else
31648 rs6000_emit_savres_rtx (info, frame_reg_rtx,
31649 info->spe_gp_save_offset + frame_off,
31650 info->lr_save_offset + frame_off,
31651 reg_mode,
31652 SAVRES_GPR | SAVRES_LR);
31654 else if (!restoring_GPRs_inline)
31656 /* We are jumping to an out-of-line function. */
31657 rtx ptr_reg;
31658 int end_save = info->gp_save_offset + info->gp_size;
31659 bool can_use_exit = end_save == 0;
31660 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
31661 int ptr_off;
31663 /* Emit stack reset code if we need it. */
31664 ptr_regno = ptr_regno_for_savres (sel);
31665 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
31666 if (can_use_exit)
31667 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
31668 else if (end_save + frame_off != 0)
31669 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
31670 GEN_INT (end_save + frame_off)));
31671 else if (REGNO (frame_reg_rtx) != ptr_regno)
31672 emit_move_insn (ptr_reg, frame_reg_rtx);
31673 if (REGNO (frame_reg_rtx) == ptr_regno)
31674 frame_off = -end_save;
31676 if (can_use_exit && info->cr_save_p)
31677 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
31679 ptr_off = -end_save;
31680 rs6000_emit_savres_rtx (info, ptr_reg,
31681 info->gp_save_offset + ptr_off,
31682 info->lr_save_offset + ptr_off,
31683 reg_mode, sel);
31685 else if (using_load_multiple)
31687 rtvec p;
31688 p = rtvec_alloc (32 - info->first_gp_reg_save);
31689 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
31690 RTVEC_ELT (p, i)
31691 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
31692 frame_reg_rtx,
31693 info->gp_save_offset + frame_off + reg_size * i);
31694 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
31696 else
31698 int offset = info->gp_save_offset + frame_off;
31699 for (i = info->first_gp_reg_save; i < 32; i++)
31701 if (rs6000_reg_live_or_pic_offset_p (i)
31702 && !cfun->machine->gpr_is_wrapped_separately[i])
31704 rtx reg = gen_rtx_REG (reg_mode, i);
31705 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
31708 offset += reg_size;
31712 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
31714 /* If the frame pointer was used then we can't delay emitting
31715 a REG_CFA_DEF_CFA note. This must happen on the insn that
31716 restores the frame pointer, r31. We may have already emitted
31717 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
31718 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
31719 be harmless if emitted. */
31720 if (frame_pointer_needed)
31722 insn = get_last_insn ();
31723 add_reg_note (insn, REG_CFA_DEF_CFA,
31724 plus_constant (Pmode, frame_reg_rtx, frame_off));
31725 RTX_FRAME_RELATED_P (insn) = 1;
31728 /* Set up cfa_restores. We always need these when
31729 shrink-wrapping. If not shrink-wrapping then we only need
31730 the cfa_restore when the stack location is no longer valid.
31731 The cfa_restores must be emitted on or before the insn that
31732 invalidates the stack, and of course must not be emitted
31733 before the insn that actually does the restore. The latter
31734 is why it is a bad idea to emit the cfa_restores as a group
31735 on the last instruction here that actually does a restore:
31736 That insn may be reordered with respect to others doing
31737 restores. */
31738 if (flag_shrink_wrap
31739 && !restoring_GPRs_inline
31740 && info->first_fp_reg_save == 64)
31741 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
31743 for (i = info->first_gp_reg_save; i < 32; i++)
31744 if (!restoring_GPRs_inline
31745 || using_load_multiple
31746 || rs6000_reg_live_or_pic_offset_p (i))
31748 if (cfun->machine->gpr_is_wrapped_separately[i])
31749 continue;
31751 rtx reg = gen_rtx_REG (reg_mode, i);
31752 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31756 if (!restoring_GPRs_inline
31757 && info->first_fp_reg_save == 64)
31759 /* We are jumping to an out-of-line function. */
31760 if (cfa_restores)
31761 emit_cfa_restores (cfa_restores);
31762 return;
31765 if (restore_lr && !restoring_GPRs_inline)
31767 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
31768 restore_saved_lr (0, exit_func);
31771 /* Restore fpr's if we need to do it without calling a function. */
31772 if (restoring_FPRs_inline)
31774 int offset = info->fp_save_offset + frame_off;
31775 for (i = info->first_fp_reg_save; i < 64; i++)
31777 if (save_reg_p (i)
31778 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
31780 rtx reg = gen_rtx_REG (fp_reg_mode, i);
31781 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
31782 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
31783 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
31784 cfa_restores);
31787 offset += fp_reg_size;
31791 /* If we saved cr, restore it here. Just those that were used. */
31792 if (info->cr_save_p)
31793 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
31795 /* If this is V.4, unwind the stack pointer after all of the loads
31796 have been done, or set up r11 if we are restoring fp out of line. */
31797 ptr_regno = 1;
31798 if (!restoring_FPRs_inline)
31800 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
31801 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
31802 ptr_regno = ptr_regno_for_savres (sel);
31805 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
31806 if (REGNO (frame_reg_rtx) == ptr_regno)
31807 frame_off = 0;
31809 if (insn && restoring_FPRs_inline)
31811 if (cfa_restores)
31813 REG_NOTES (insn) = cfa_restores;
31814 cfa_restores = NULL_RTX;
31816 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
31817 RTX_FRAME_RELATED_P (insn) = 1;
31820 if (crtl->calls_eh_return)
31822 rtx sa = EH_RETURN_STACKADJ_RTX;
31823 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
31826 if (!sibcall && restoring_FPRs_inline)
31828 if (cfa_restores)
31830 /* We can't hang the cfa_restores off a simple return,
31831 since the shrink-wrap code sometimes uses an existing
31832 return. This means there might be a path from
31833 pre-prologue code to this return, and dwarf2cfi code
31834 wants the eh_frame unwinder state to be the same on
31835 all paths to any point. So we need to emit the
31836 cfa_restores before the return. For -m64 we really
31837 don't need epilogue cfa_restores at all, except for
31838 this irritating dwarf2cfi with shrink-wrap
31839 requirement; The stack red-zone means eh_frame info
31840 from the prologue telling the unwinder to restore
31841 from the stack is perfectly good right to the end of
31842 the function. */
31843 emit_insn (gen_blockage ());
31844 emit_cfa_restores (cfa_restores);
31845 cfa_restores = NULL_RTX;
31848 emit_jump_insn (targetm.gen_simple_return ());
31851 if (!sibcall && !restoring_FPRs_inline)
31853 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
31854 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
31855 int elt = 0;
31856 RTVEC_ELT (p, elt++) = ret_rtx;
31857 if (lr)
31858 RTVEC_ELT (p, elt++)
31859 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
31861 /* We have to restore more than two FP registers, so branch to the
31862 restore function. It will return to our caller. */
31863 int i;
31864 int reg;
31865 rtx sym;
31867 if (flag_shrink_wrap)
31868 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
31870 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
31871 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
31872 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
31873 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
31875 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
31877 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
31879 RTVEC_ELT (p, elt++)
31880 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
31881 if (flag_shrink_wrap)
31882 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31885 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
31888 if (cfa_restores)
31890 if (sibcall)
31891 /* Ensure the cfa_restores are hung off an insn that won't
31892 be reordered above other restores. */
31893 emit_insn (gen_blockage ());
31895 emit_cfa_restores (cfa_restores);
31899 /* Write function epilogue. */
31901 static void
31902 rs6000_output_function_epilogue (FILE *file)
31904 #if TARGET_MACHO
31905 macho_branch_islands ();
31908 rtx_insn *insn = get_last_insn ();
31909 rtx_insn *deleted_debug_label = NULL;
31911 /* Mach-O doesn't support labels at the end of objects, so if
31912 it looks like we might want one, take special action.
31914 First, collect any sequence of deleted debug labels. */
31915 while (insn
31916 && NOTE_P (insn)
31917 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
31919 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
31920 notes only, instead set their CODE_LABEL_NUMBER to -1,
31921 otherwise there would be code generation differences
31922 in between -g and -g0. */
31923 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
31924 deleted_debug_label = insn;
31925 insn = PREV_INSN (insn);
31928 /* Second, if we have:
31929 label:
31930 barrier
31931 then this needs to be detected, so skip past the barrier. */
31933 if (insn && BARRIER_P (insn))
31934 insn = PREV_INSN (insn);
31936 /* Up to now we've only seen notes or barriers. */
31937 if (insn)
31939 if (LABEL_P (insn)
31940 || (NOTE_P (insn)
31941 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
31942 /* Trailing label: <barrier>. */
31943 fputs ("\tnop\n", file);
31944 else
31946 /* Lastly, see if we have a completely empty function body. */
31947 while (insn && ! INSN_P (insn))
31948 insn = PREV_INSN (insn);
31949 /* If we don't find any insns, we've got an empty function body;
31950 I.e. completely empty - without a return or branch. This is
31951 taken as the case where a function body has been removed
31952 because it contains an inline __builtin_unreachable(). GCC
31953 states that reaching __builtin_unreachable() means UB so we're
31954 not obliged to do anything special; however, we want
31955 non-zero-sized function bodies. To meet this, and help the
31956 user out, let's trap the case. */
31957 if (insn == NULL)
31958 fputs ("\ttrap\n", file);
31961 else if (deleted_debug_label)
31962 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
31963 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
31964 CODE_LABEL_NUMBER (insn) = -1;
31966 #endif
31968 /* Output a traceback table here. See /usr/include/sys/debug.h for info
31969 on its format.
31971 We don't output a traceback table if -finhibit-size-directive was
31972 used. The documentation for -finhibit-size-directive reads
31973 ``don't output a @code{.size} assembler directive, or anything
31974 else that would cause trouble if the function is split in the
31975 middle, and the two halves are placed at locations far apart in
31976 memory.'' The traceback table has this property, since it
31977 includes the offset from the start of the function to the
31978 traceback table itself.
31980 System V.4 Powerpc's (and the embedded ABI derived from it) use a
31981 different traceback table. */
31982 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
31983 && ! flag_inhibit_size_directive
31984 && rs6000_traceback != traceback_none && !cfun->is_thunk)
31986 const char *fname = NULL;
31987 const char *language_string = lang_hooks.name;
31988 int fixed_parms = 0, float_parms = 0, parm_info = 0;
31989 int i;
31990 int optional_tbtab;
31991 rs6000_stack_t *info = rs6000_stack_info ();
31993 if (rs6000_traceback == traceback_full)
31994 optional_tbtab = 1;
31995 else if (rs6000_traceback == traceback_part)
31996 optional_tbtab = 0;
31997 else
31998 optional_tbtab = !optimize_size && !TARGET_ELF;
32000 if (optional_tbtab)
32002 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
32003 while (*fname == '.') /* V.4 encodes . in the name */
32004 fname++;
32006 /* Need label immediately before tbtab, so we can compute
32007 its offset from the function start. */
32008 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
32009 ASM_OUTPUT_LABEL (file, fname);
32012 /* The .tbtab pseudo-op can only be used for the first eight
32013 expressions, since it can't handle the possibly variable
32014 length fields that follow. However, if you omit the optional
32015 fields, the assembler outputs zeros for all optional fields
32016 anyways, giving each variable length field is minimum length
32017 (as defined in sys/debug.h). Thus we can not use the .tbtab
32018 pseudo-op at all. */
32020 /* An all-zero word flags the start of the tbtab, for debuggers
32021 that have to find it by searching forward from the entry
32022 point or from the current pc. */
32023 fputs ("\t.long 0\n", file);
32025 /* Tbtab format type. Use format type 0. */
32026 fputs ("\t.byte 0,", file);
32028 /* Language type. Unfortunately, there does not seem to be any
32029 official way to discover the language being compiled, so we
32030 use language_string.
32031 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
32032 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
32033 a number, so for now use 9. LTO, Go and JIT aren't assigned numbers
32034 either, so for now use 0. */
32035 if (lang_GNU_C ()
32036 || ! strcmp (language_string, "GNU GIMPLE")
32037 || ! strcmp (language_string, "GNU Go")
32038 || ! strcmp (language_string, "libgccjit"))
32039 i = 0;
32040 else if (! strcmp (language_string, "GNU F77")
32041 || lang_GNU_Fortran ())
32042 i = 1;
32043 else if (! strcmp (language_string, "GNU Pascal"))
32044 i = 2;
32045 else if (! strcmp (language_string, "GNU Ada"))
32046 i = 3;
32047 else if (lang_GNU_CXX ()
32048 || ! strcmp (language_string, "GNU Objective-C++"))
32049 i = 9;
32050 else if (! strcmp (language_string, "GNU Java"))
32051 i = 13;
32052 else if (! strcmp (language_string, "GNU Objective-C"))
32053 i = 14;
32054 else
32055 gcc_unreachable ();
32056 fprintf (file, "%d,", i);
32058 /* 8 single bit fields: global linkage (not set for C extern linkage,
32059 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
32060 from start of procedure stored in tbtab, internal function, function
32061 has controlled storage, function has no toc, function uses fp,
32062 function logs/aborts fp operations. */
32063 /* Assume that fp operations are used if any fp reg must be saved. */
32064 fprintf (file, "%d,",
32065 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
32067 /* 6 bitfields: function is interrupt handler, name present in
32068 proc table, function calls alloca, on condition directives
32069 (controls stack walks, 3 bits), saves condition reg, saves
32070 link reg. */
32071 /* The `function calls alloca' bit seems to be set whenever reg 31 is
32072 set up as a frame pointer, even when there is no alloca call. */
32073 fprintf (file, "%d,",
32074 ((optional_tbtab << 6)
32075 | ((optional_tbtab & frame_pointer_needed) << 5)
32076 | (info->cr_save_p << 1)
32077 | (info->lr_save_p)));
32079 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
32080 (6 bits). */
32081 fprintf (file, "%d,",
32082 (info->push_p << 7) | (64 - info->first_fp_reg_save));
32084 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
32085 fprintf (file, "%d,", (32 - first_reg_to_save ()));
32087 if (optional_tbtab)
32089 /* Compute the parameter info from the function decl argument
32090 list. */
32091 tree decl;
32092 int next_parm_info_bit = 31;
32094 for (decl = DECL_ARGUMENTS (current_function_decl);
32095 decl; decl = DECL_CHAIN (decl))
32097 rtx parameter = DECL_INCOMING_RTL (decl);
32098 machine_mode mode = GET_MODE (parameter);
32100 if (GET_CODE (parameter) == REG)
32102 if (SCALAR_FLOAT_MODE_P (mode))
32104 int bits;
32106 float_parms++;
32108 switch (mode)
32110 case E_SFmode:
32111 case E_SDmode:
32112 bits = 0x2;
32113 break;
32115 case E_DFmode:
32116 case E_DDmode:
32117 case E_TFmode:
32118 case E_TDmode:
32119 case E_IFmode:
32120 case E_KFmode:
32121 bits = 0x3;
32122 break;
32124 default:
32125 gcc_unreachable ();
32128 /* If only one bit will fit, don't or in this entry. */
32129 if (next_parm_info_bit > 0)
32130 parm_info |= (bits << (next_parm_info_bit - 1));
32131 next_parm_info_bit -= 2;
32133 else
32135 fixed_parms += ((GET_MODE_SIZE (mode)
32136 + (UNITS_PER_WORD - 1))
32137 / UNITS_PER_WORD);
32138 next_parm_info_bit -= 1;
32144 /* Number of fixed point parameters. */
32145 /* This is actually the number of words of fixed point parameters; thus
32146 an 8 byte struct counts as 2; and thus the maximum value is 8. */
32147 fprintf (file, "%d,", fixed_parms);
32149 /* 2 bitfields: number of floating point parameters (7 bits), parameters
32150 all on stack. */
32151 /* This is actually the number of fp registers that hold parameters;
32152 and thus the maximum value is 13. */
32153 /* Set parameters on stack bit if parameters are not in their original
32154 registers, regardless of whether they are on the stack? Xlc
32155 seems to set the bit when not optimizing. */
32156 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
32158 if (optional_tbtab)
32160 /* Optional fields follow. Some are variable length. */
32162 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
32163 float, 11 double float. */
32164 /* There is an entry for each parameter in a register, in the order
32165 that they occur in the parameter list. Any intervening arguments
32166 on the stack are ignored. If the list overflows a long (max
32167 possible length 34 bits) then completely leave off all elements
32168 that don't fit. */
32169 /* Only emit this long if there was at least one parameter. */
32170 if (fixed_parms || float_parms)
32171 fprintf (file, "\t.long %d\n", parm_info);
32173 /* Offset from start of code to tb table. */
32174 fputs ("\t.long ", file);
32175 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
32176 RS6000_OUTPUT_BASENAME (file, fname);
32177 putc ('-', file);
32178 rs6000_output_function_entry (file, fname);
32179 putc ('\n', file);
32181 /* Interrupt handler mask. */
32182 /* Omit this long, since we never set the interrupt handler bit
32183 above. */
32185 /* Number of CTL (controlled storage) anchors. */
32186 /* Omit this long, since the has_ctl bit is never set above. */
32188 /* Displacement into stack of each CTL anchor. */
32189 /* Omit this list of longs, because there are no CTL anchors. */
32191 /* Length of function name. */
32192 if (*fname == '*')
32193 ++fname;
32194 fprintf (file, "\t.short %d\n", (int) strlen (fname));
32196 /* Function name. */
32197 assemble_string (fname, strlen (fname));
32199 /* Register for alloca automatic storage; this is always reg 31.
32200 Only emit this if the alloca bit was set above. */
32201 if (frame_pointer_needed)
32202 fputs ("\t.byte 31\n", file);
32204 fputs ("\t.align 2\n", file);
32208 /* Arrange to define .LCTOC1 label, if not already done. */
32209 if (need_toc_init)
32211 need_toc_init = 0;
32212 if (!toc_initialized)
32214 switch_to_section (toc_section);
32215 switch_to_section (current_function_section ());
32220 /* -fsplit-stack support. */
32222 /* A SYMBOL_REF for __morestack. */
32223 static GTY(()) rtx morestack_ref;
32225 static rtx
32226 gen_add3_const (rtx rt, rtx ra, long c)
32228 if (TARGET_64BIT)
32229 return gen_adddi3 (rt, ra, GEN_INT (c));
32230 else
32231 return gen_addsi3 (rt, ra, GEN_INT (c));
32234 /* Emit -fsplit-stack prologue, which goes before the regular function
32235 prologue (at local entry point in the case of ELFv2). */
32237 void
32238 rs6000_expand_split_stack_prologue (void)
32240 rs6000_stack_t *info = rs6000_stack_info ();
32241 unsigned HOST_WIDE_INT allocate;
32242 long alloc_hi, alloc_lo;
32243 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
32244 rtx_insn *insn;
32246 gcc_assert (flag_split_stack && reload_completed);
32248 if (!info->push_p)
32249 return;
32251 if (global_regs[29])
32253 error ("-fsplit-stack uses register r29");
32254 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
32255 "conflicts with %qD", global_regs_decl[29]);
32258 allocate = info->total_size;
32259 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
32261 sorry ("Stack frame larger than 2G is not supported for -fsplit-stack");
32262 return;
32264 if (morestack_ref == NULL_RTX)
32266 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
32267 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
32268 | SYMBOL_FLAG_FUNCTION);
32271 r0 = gen_rtx_REG (Pmode, 0);
32272 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32273 r12 = gen_rtx_REG (Pmode, 12);
32274 emit_insn (gen_load_split_stack_limit (r0));
32275 /* Always emit two insns here to calculate the requested stack,
32276 so that the linker can edit them when adjusting size for calling
32277 non-split-stack code. */
32278 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
32279 alloc_lo = -allocate - alloc_hi;
32280 if (alloc_hi != 0)
32282 emit_insn (gen_add3_const (r12, r1, alloc_hi));
32283 if (alloc_lo != 0)
32284 emit_insn (gen_add3_const (r12, r12, alloc_lo));
32285 else
32286 emit_insn (gen_nop ());
32288 else
32290 emit_insn (gen_add3_const (r12, r1, alloc_lo));
32291 emit_insn (gen_nop ());
32294 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
32295 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
32296 ok_label = gen_label_rtx ();
32297 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
32298 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
32299 gen_rtx_LABEL_REF (VOIDmode, ok_label),
32300 pc_rtx);
32301 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
32302 JUMP_LABEL (insn) = ok_label;
32303 /* Mark the jump as very likely to be taken. */
32304 add_reg_br_prob_note (insn, profile_probability::very_likely ());
32306 lr = gen_rtx_REG (Pmode, LR_REGNO);
32307 insn = emit_move_insn (r0, lr);
32308 RTX_FRAME_RELATED_P (insn) = 1;
32309 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
32310 RTX_FRAME_RELATED_P (insn) = 1;
32312 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
32313 const0_rtx, const0_rtx));
32314 call_fusage = NULL_RTX;
32315 use_reg (&call_fusage, r12);
32316 /* Say the call uses r0, even though it doesn't, to stop regrename
32317 from twiddling with the insns saving lr, trashing args for cfun.
32318 The insns restoring lr are similarly protected by making
32319 split_stack_return use r0. */
32320 use_reg (&call_fusage, r0);
32321 add_function_usage_to (insn, call_fusage);
32322 /* Indicate that this function can't jump to non-local gotos. */
32323 make_reg_eh_region_note_nothrow_nononlocal (insn);
32324 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
32325 insn = emit_move_insn (lr, r0);
32326 add_reg_note (insn, REG_CFA_RESTORE, lr);
32327 RTX_FRAME_RELATED_P (insn) = 1;
32328 emit_insn (gen_split_stack_return ());
32330 emit_label (ok_label);
32331 LABEL_NUSES (ok_label) = 1;
32334 /* Return the internal arg pointer used for function incoming
32335 arguments. When -fsplit-stack, the arg pointer is r12 so we need
32336 to copy it to a pseudo in order for it to be preserved over calls
32337 and suchlike. We'd really like to use a pseudo here for the
32338 internal arg pointer but data-flow analysis is not prepared to
32339 accept pseudos as live at the beginning of a function. */
32341 static rtx
32342 rs6000_internal_arg_pointer (void)
32344 if (flag_split_stack
32345 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
32346 == NULL))
32349 if (cfun->machine->split_stack_arg_pointer == NULL_RTX)
32351 rtx pat;
32353 cfun->machine->split_stack_arg_pointer = gen_reg_rtx (Pmode);
32354 REG_POINTER (cfun->machine->split_stack_arg_pointer) = 1;
32356 /* Put the pseudo initialization right after the note at the
32357 beginning of the function. */
32358 pat = gen_rtx_SET (cfun->machine->split_stack_arg_pointer,
32359 gen_rtx_REG (Pmode, 12));
32360 push_topmost_sequence ();
32361 emit_insn_after (pat, get_insns ());
32362 pop_topmost_sequence ();
32364 return plus_constant (Pmode, cfun->machine->split_stack_arg_pointer,
32365 FIRST_PARM_OFFSET (current_function_decl));
32367 return virtual_incoming_args_rtx;
32370 /* We may have to tell the dataflow pass that the split stack prologue
32371 is initializing a register. */
32373 static void
32374 rs6000_live_on_entry (bitmap regs)
32376 if (flag_split_stack)
32377 bitmap_set_bit (regs, 12);
32380 /* Emit -fsplit-stack dynamic stack allocation space check. */
32382 void
32383 rs6000_split_stack_space_check (rtx size, rtx label)
32385 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32386 rtx limit = gen_reg_rtx (Pmode);
32387 rtx requested = gen_reg_rtx (Pmode);
32388 rtx cmp = gen_reg_rtx (CCUNSmode);
32389 rtx jump;
32391 emit_insn (gen_load_split_stack_limit (limit));
32392 if (CONST_INT_P (size))
32393 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
32394 else
32396 size = force_reg (Pmode, size);
32397 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
32399 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
32400 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
32401 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
32402 gen_rtx_LABEL_REF (VOIDmode, label),
32403 pc_rtx);
32404 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
32405 JUMP_LABEL (jump) = label;
32408 /* A C compound statement that outputs the assembler code for a thunk
32409 function, used to implement C++ virtual function calls with
32410 multiple inheritance. The thunk acts as a wrapper around a virtual
32411 function, adjusting the implicit object parameter before handing
32412 control off to the real function.
32414 First, emit code to add the integer DELTA to the location that
32415 contains the incoming first argument. Assume that this argument
32416 contains a pointer, and is the one used to pass the `this' pointer
32417 in C++. This is the incoming argument *before* the function
32418 prologue, e.g. `%o0' on a sparc. The addition must preserve the
32419 values of all other incoming arguments.
32421 After the addition, emit code to jump to FUNCTION, which is a
32422 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
32423 not touch the return address. Hence returning from FUNCTION will
32424 return to whoever called the current `thunk'.
32426 The effect must be as if FUNCTION had been called directly with the
32427 adjusted first argument. This macro is responsible for emitting
32428 all of the code for a thunk function; output_function_prologue()
32429 and output_function_epilogue() are not invoked.
32431 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
32432 been extracted from it.) It might possibly be useful on some
32433 targets, but probably not.
32435 If you do not define this macro, the target-independent code in the
32436 C++ frontend will generate a less efficient heavyweight thunk that
32437 calls FUNCTION instead of jumping to it. The generic approach does
32438 not support varargs. */
32440 static void
32441 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
32442 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
32443 tree function)
32445 rtx this_rtx, funexp;
32446 rtx_insn *insn;
32448 reload_completed = 1;
32449 epilogue_completed = 1;
32451 /* Mark the end of the (empty) prologue. */
32452 emit_note (NOTE_INSN_PROLOGUE_END);
32454 /* Find the "this" pointer. If the function returns a structure,
32455 the structure return pointer is in r3. */
32456 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
32457 this_rtx = gen_rtx_REG (Pmode, 4);
32458 else
32459 this_rtx = gen_rtx_REG (Pmode, 3);
32461 /* Apply the constant offset, if required. */
32462 if (delta)
32463 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
32465 /* Apply the offset from the vtable, if required. */
32466 if (vcall_offset)
32468 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
32469 rtx tmp = gen_rtx_REG (Pmode, 12);
32471 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
32472 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
32474 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
32475 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
32477 else
32479 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
32481 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
32483 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
32486 /* Generate a tail call to the target function. */
32487 if (!TREE_USED (function))
32489 assemble_external (function);
32490 TREE_USED (function) = 1;
32492 funexp = XEXP (DECL_RTL (function), 0);
32493 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
32495 #if TARGET_MACHO
32496 if (MACHOPIC_INDIRECT)
32497 funexp = machopic_indirect_call_target (funexp);
32498 #endif
32500 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
32501 generate sibcall RTL explicitly. */
32502 insn = emit_call_insn (
32503 gen_rtx_PARALLEL (VOIDmode,
32504 gen_rtvec (3,
32505 gen_rtx_CALL (VOIDmode,
32506 funexp, const0_rtx),
32507 gen_rtx_USE (VOIDmode, const0_rtx),
32508 simple_return_rtx)));
32509 SIBLING_CALL_P (insn) = 1;
32510 emit_barrier ();
32512 /* Run just enough of rest_of_compilation to get the insns emitted.
32513 There's not really enough bulk here to make other passes such as
32514 instruction scheduling worth while. Note that use_thunk calls
32515 assemble_start_function and assemble_end_function. */
32516 insn = get_insns ();
32517 shorten_branches (insn);
32518 final_start_function (insn, file, 1);
32519 final (insn, file, 1);
32520 final_end_function ();
32522 reload_completed = 0;
32523 epilogue_completed = 0;
32526 /* A quick summary of the various types of 'constant-pool tables'
32527 under PowerPC:
32529 Target Flags Name One table per
32530 AIX (none) AIX TOC object file
32531 AIX -mfull-toc AIX TOC object file
32532 AIX -mminimal-toc AIX minimal TOC translation unit
32533 SVR4/EABI (none) SVR4 SDATA object file
32534 SVR4/EABI -fpic SVR4 pic object file
32535 SVR4/EABI -fPIC SVR4 PIC translation unit
32536 SVR4/EABI -mrelocatable EABI TOC function
32537 SVR4/EABI -maix AIX TOC object file
32538 SVR4/EABI -maix -mminimal-toc
32539 AIX minimal TOC translation unit
32541 Name Reg. Set by entries contains:
32542 made by addrs? fp? sum?
32544 AIX TOC 2 crt0 as Y option option
32545 AIX minimal TOC 30 prolog gcc Y Y option
32546 SVR4 SDATA 13 crt0 gcc N Y N
32547 SVR4 pic 30 prolog ld Y not yet N
32548 SVR4 PIC 30 prolog gcc Y option option
32549 EABI TOC 30 prolog gcc Y option option
32553 /* Hash functions for the hash table. */
32555 static unsigned
32556 rs6000_hash_constant (rtx k)
32558 enum rtx_code code = GET_CODE (k);
32559 machine_mode mode = GET_MODE (k);
32560 unsigned result = (code << 3) ^ mode;
32561 const char *format;
32562 int flen, fidx;
32564 format = GET_RTX_FORMAT (code);
32565 flen = strlen (format);
32566 fidx = 0;
32568 switch (code)
32570 case LABEL_REF:
32571 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
32573 case CONST_WIDE_INT:
32575 int i;
32576 flen = CONST_WIDE_INT_NUNITS (k);
32577 for (i = 0; i < flen; i++)
32578 result = result * 613 + CONST_WIDE_INT_ELT (k, i);
32579 return result;
32582 case CONST_DOUBLE:
32583 if (mode != VOIDmode)
32584 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
32585 flen = 2;
32586 break;
32588 case CODE_LABEL:
32589 fidx = 3;
32590 break;
32592 default:
32593 break;
32596 for (; fidx < flen; fidx++)
32597 switch (format[fidx])
32599 case 's':
32601 unsigned i, len;
32602 const char *str = XSTR (k, fidx);
32603 len = strlen (str);
32604 result = result * 613 + len;
32605 for (i = 0; i < len; i++)
32606 result = result * 613 + (unsigned) str[i];
32607 break;
32609 case 'u':
32610 case 'e':
32611 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
32612 break;
32613 case 'i':
32614 case 'n':
32615 result = result * 613 + (unsigned) XINT (k, fidx);
32616 break;
32617 case 'w':
32618 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
32619 result = result * 613 + (unsigned) XWINT (k, fidx);
32620 else
32622 size_t i;
32623 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
32624 result = result * 613 + (unsigned) (XWINT (k, fidx)
32625 >> CHAR_BIT * i);
32627 break;
32628 case '0':
32629 break;
32630 default:
32631 gcc_unreachable ();
32634 return result;
32637 hashval_t
32638 toc_hasher::hash (toc_hash_struct *thc)
32640 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
32643 /* Compare H1 and H2 for equivalence. */
32645 bool
32646 toc_hasher::equal (toc_hash_struct *h1, toc_hash_struct *h2)
32648 rtx r1 = h1->key;
32649 rtx r2 = h2->key;
32651 if (h1->key_mode != h2->key_mode)
32652 return 0;
32654 return rtx_equal_p (r1, r2);
32657 /* These are the names given by the C++ front-end to vtables, and
32658 vtable-like objects. Ideally, this logic should not be here;
32659 instead, there should be some programmatic way of inquiring as
32660 to whether or not an object is a vtable. */
32662 #define VTABLE_NAME_P(NAME) \
32663 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
32664 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
32665 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
32666 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
32667 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
32669 #ifdef NO_DOLLAR_IN_LABEL
32670 /* Return a GGC-allocated character string translating dollar signs in
32671 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
32673 const char *
32674 rs6000_xcoff_strip_dollar (const char *name)
32676 char *strip, *p;
32677 const char *q;
32678 size_t len;
32680 q = (const char *) strchr (name, '$');
32682 if (q == 0 || q == name)
32683 return name;
32685 len = strlen (name);
32686 strip = XALLOCAVEC (char, len + 1);
32687 strcpy (strip, name);
32688 p = strip + (q - name);
32689 while (p)
32691 *p = '_';
32692 p = strchr (p + 1, '$');
32695 return ggc_alloc_string (strip, len);
32697 #endif
32699 void
32700 rs6000_output_symbol_ref (FILE *file, rtx x)
32702 const char *name = XSTR (x, 0);
32704 /* Currently C++ toc references to vtables can be emitted before it
32705 is decided whether the vtable is public or private. If this is
32706 the case, then the linker will eventually complain that there is
32707 a reference to an unknown section. Thus, for vtables only,
32708 we emit the TOC reference to reference the identifier and not the
32709 symbol. */
32710 if (VTABLE_NAME_P (name))
32712 RS6000_OUTPUT_BASENAME (file, name);
32714 else
32715 assemble_name (file, name);
32718 /* Output a TOC entry. We derive the entry name from what is being
32719 written. */
32721 void
32722 output_toc (FILE *file, rtx x, int labelno, machine_mode mode)
32724 char buf[256];
32725 const char *name = buf;
32726 rtx base = x;
32727 HOST_WIDE_INT offset = 0;
32729 gcc_assert (!TARGET_NO_TOC);
32731 /* When the linker won't eliminate them, don't output duplicate
32732 TOC entries (this happens on AIX if there is any kind of TOC,
32733 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
32734 CODE_LABELs. */
32735 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
32737 struct toc_hash_struct *h;
32739 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
32740 time because GGC is not initialized at that point. */
32741 if (toc_hash_table == NULL)
32742 toc_hash_table = hash_table<toc_hasher>::create_ggc (1021);
32744 h = ggc_alloc<toc_hash_struct> ();
32745 h->key = x;
32746 h->key_mode = mode;
32747 h->labelno = labelno;
32749 toc_hash_struct **found = toc_hash_table->find_slot (h, INSERT);
32750 if (*found == NULL)
32751 *found = h;
32752 else /* This is indeed a duplicate.
32753 Set this label equal to that label. */
32755 fputs ("\t.set ", file);
32756 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
32757 fprintf (file, "%d,", labelno);
32758 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
32759 fprintf (file, "%d\n", ((*found)->labelno));
32761 #ifdef HAVE_AS_TLS
32762 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
32763 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
32764 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
32766 fputs ("\t.set ", file);
32767 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
32768 fprintf (file, "%d,", labelno);
32769 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
32770 fprintf (file, "%d\n", ((*found)->labelno));
32772 #endif
32773 return;
32777 /* If we're going to put a double constant in the TOC, make sure it's
32778 aligned properly when strict alignment is on. */
32779 if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
32780 && STRICT_ALIGNMENT
32781 && GET_MODE_BITSIZE (mode) >= 64
32782 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
32783 ASM_OUTPUT_ALIGN (file, 3);
32786 (*targetm.asm_out.internal_label) (file, "LC", labelno);
32788 /* Handle FP constants specially. Note that if we have a minimal
32789 TOC, things we put here aren't actually in the TOC, so we can allow
32790 FP constants. */
32791 if (GET_CODE (x) == CONST_DOUBLE &&
32792 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode
32793 || GET_MODE (x) == IFmode || GET_MODE (x) == KFmode))
32795 long k[4];
32797 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
32798 REAL_VALUE_TO_TARGET_DECIMAL128 (*CONST_DOUBLE_REAL_VALUE (x), k);
32799 else
32800 REAL_VALUE_TO_TARGET_LONG_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
32802 if (TARGET_64BIT)
32804 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32805 fputs (DOUBLE_INT_ASM_OP, file);
32806 else
32807 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
32808 k[0] & 0xffffffff, k[1] & 0xffffffff,
32809 k[2] & 0xffffffff, k[3] & 0xffffffff);
32810 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
32811 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
32812 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
32813 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
32814 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
32815 return;
32817 else
32819 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32820 fputs ("\t.long ", file);
32821 else
32822 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
32823 k[0] & 0xffffffff, k[1] & 0xffffffff,
32824 k[2] & 0xffffffff, k[3] & 0xffffffff);
32825 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
32826 k[0] & 0xffffffff, k[1] & 0xffffffff,
32827 k[2] & 0xffffffff, k[3] & 0xffffffff);
32828 return;
32831 else if (GET_CODE (x) == CONST_DOUBLE &&
32832 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
32834 long k[2];
32836 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
32837 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (x), k);
32838 else
32839 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
32841 if (TARGET_64BIT)
32843 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32844 fputs (DOUBLE_INT_ASM_OP, file);
32845 else
32846 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
32847 k[0] & 0xffffffff, k[1] & 0xffffffff);
32848 fprintf (file, "0x%lx%08lx\n",
32849 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
32850 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
32851 return;
32853 else
32855 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32856 fputs ("\t.long ", file);
32857 else
32858 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
32859 k[0] & 0xffffffff, k[1] & 0xffffffff);
32860 fprintf (file, "0x%lx,0x%lx\n",
32861 k[0] & 0xffffffff, k[1] & 0xffffffff);
32862 return;
32865 else if (GET_CODE (x) == CONST_DOUBLE &&
32866 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
32868 long l;
32870 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
32871 REAL_VALUE_TO_TARGET_DECIMAL32 (*CONST_DOUBLE_REAL_VALUE (x), l);
32872 else
32873 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
32875 if (TARGET_64BIT)
32877 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32878 fputs (DOUBLE_INT_ASM_OP, file);
32879 else
32880 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
32881 if (WORDS_BIG_ENDIAN)
32882 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
32883 else
32884 fprintf (file, "0x%lx\n", l & 0xffffffff);
32885 return;
32887 else
32889 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32890 fputs ("\t.long ", file);
32891 else
32892 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
32893 fprintf (file, "0x%lx\n", l & 0xffffffff);
32894 return;
32897 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
32899 unsigned HOST_WIDE_INT low;
32900 HOST_WIDE_INT high;
32902 low = INTVAL (x) & 0xffffffff;
32903 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
32905 /* TOC entries are always Pmode-sized, so when big-endian
32906 smaller integer constants in the TOC need to be padded.
32907 (This is still a win over putting the constants in
32908 a separate constant pool, because then we'd have
32909 to have both a TOC entry _and_ the actual constant.)
32911 For a 32-bit target, CONST_INT values are loaded and shifted
32912 entirely within `low' and can be stored in one TOC entry. */
32914 /* It would be easy to make this work, but it doesn't now. */
32915 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
32917 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
32919 low |= high << 32;
32920 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
32921 high = (HOST_WIDE_INT) low >> 32;
32922 low &= 0xffffffff;
32925 if (TARGET_64BIT)
32927 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32928 fputs (DOUBLE_INT_ASM_OP, file);
32929 else
32930 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
32931 (long) high & 0xffffffff, (long) low & 0xffffffff);
32932 fprintf (file, "0x%lx%08lx\n",
32933 (long) high & 0xffffffff, (long) low & 0xffffffff);
32934 return;
32936 else
32938 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
32940 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32941 fputs ("\t.long ", file);
32942 else
32943 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
32944 (long) high & 0xffffffff, (long) low & 0xffffffff);
32945 fprintf (file, "0x%lx,0x%lx\n",
32946 (long) high & 0xffffffff, (long) low & 0xffffffff);
32948 else
32950 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32951 fputs ("\t.long ", file);
32952 else
32953 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
32954 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
32956 return;
32960 if (GET_CODE (x) == CONST)
32962 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
32963 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
32965 base = XEXP (XEXP (x, 0), 0);
32966 offset = INTVAL (XEXP (XEXP (x, 0), 1));
32969 switch (GET_CODE (base))
32971 case SYMBOL_REF:
32972 name = XSTR (base, 0);
32973 break;
32975 case LABEL_REF:
32976 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
32977 CODE_LABEL_NUMBER (XEXP (base, 0)));
32978 break;
32980 case CODE_LABEL:
32981 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
32982 break;
32984 default:
32985 gcc_unreachable ();
32988 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32989 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
32990 else
32992 fputs ("\t.tc ", file);
32993 RS6000_OUTPUT_BASENAME (file, name);
32995 if (offset < 0)
32996 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
32997 else if (offset)
32998 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
33000 /* Mark large TOC symbols on AIX with [TE] so they are mapped
33001 after other TOC symbols, reducing overflow of small TOC access
33002 to [TC] symbols. */
33003 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
33004 ? "[TE]," : "[TC],", file);
33007 /* Currently C++ toc references to vtables can be emitted before it
33008 is decided whether the vtable is public or private. If this is
33009 the case, then the linker will eventually complain that there is
33010 a TOC reference to an unknown section. Thus, for vtables only,
33011 we emit the TOC reference to reference the symbol and not the
33012 section. */
33013 if (VTABLE_NAME_P (name))
33015 RS6000_OUTPUT_BASENAME (file, name);
33016 if (offset < 0)
33017 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
33018 else if (offset > 0)
33019 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
33021 else
33022 output_addr_const (file, x);
33024 #if HAVE_AS_TLS
33025 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF)
33027 switch (SYMBOL_REF_TLS_MODEL (base))
33029 case 0:
33030 break;
33031 case TLS_MODEL_LOCAL_EXEC:
33032 fputs ("@le", file);
33033 break;
33034 case TLS_MODEL_INITIAL_EXEC:
33035 fputs ("@ie", file);
33036 break;
33037 /* Use global-dynamic for local-dynamic. */
33038 case TLS_MODEL_GLOBAL_DYNAMIC:
33039 case TLS_MODEL_LOCAL_DYNAMIC:
33040 putc ('\n', file);
33041 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
33042 fputs ("\t.tc .", file);
33043 RS6000_OUTPUT_BASENAME (file, name);
33044 fputs ("[TC],", file);
33045 output_addr_const (file, x);
33046 fputs ("@m", file);
33047 break;
33048 default:
33049 gcc_unreachable ();
33052 #endif
33054 putc ('\n', file);
33057 /* Output an assembler pseudo-op to write an ASCII string of N characters
33058 starting at P to FILE.
33060 On the RS/6000, we have to do this using the .byte operation and
33061 write out special characters outside the quoted string.
33062 Also, the assembler is broken; very long strings are truncated,
33063 so we must artificially break them up early. */
33065 void
33066 output_ascii (FILE *file, const char *p, int n)
33068 char c;
33069 int i, count_string;
33070 const char *for_string = "\t.byte \"";
33071 const char *for_decimal = "\t.byte ";
33072 const char *to_close = NULL;
33074 count_string = 0;
33075 for (i = 0; i < n; i++)
33077 c = *p++;
33078 if (c >= ' ' && c < 0177)
33080 if (for_string)
33081 fputs (for_string, file);
33082 putc (c, file);
33084 /* Write two quotes to get one. */
33085 if (c == '"')
33087 putc (c, file);
33088 ++count_string;
33091 for_string = NULL;
33092 for_decimal = "\"\n\t.byte ";
33093 to_close = "\"\n";
33094 ++count_string;
33096 if (count_string >= 512)
33098 fputs (to_close, file);
33100 for_string = "\t.byte \"";
33101 for_decimal = "\t.byte ";
33102 to_close = NULL;
33103 count_string = 0;
33106 else
33108 if (for_decimal)
33109 fputs (for_decimal, file);
33110 fprintf (file, "%d", c);
33112 for_string = "\n\t.byte \"";
33113 for_decimal = ", ";
33114 to_close = "\n";
33115 count_string = 0;
33119 /* Now close the string if we have written one. Then end the line. */
33120 if (to_close)
33121 fputs (to_close, file);
33124 /* Generate a unique section name for FILENAME for a section type
33125 represented by SECTION_DESC. Output goes into BUF.
33127 SECTION_DESC can be any string, as long as it is different for each
33128 possible section type.
33130 We name the section in the same manner as xlc. The name begins with an
33131 underscore followed by the filename (after stripping any leading directory
33132 names) with the last period replaced by the string SECTION_DESC. If
33133 FILENAME does not contain a period, SECTION_DESC is appended to the end of
33134 the name. */
33136 void
33137 rs6000_gen_section_name (char **buf, const char *filename,
33138 const char *section_desc)
33140 const char *q, *after_last_slash, *last_period = 0;
33141 char *p;
33142 int len;
33144 after_last_slash = filename;
33145 for (q = filename; *q; q++)
33147 if (*q == '/')
33148 after_last_slash = q + 1;
33149 else if (*q == '.')
33150 last_period = q;
33153 len = strlen (after_last_slash) + strlen (section_desc) + 2;
33154 *buf = (char *) xmalloc (len);
33156 p = *buf;
33157 *p++ = '_';
33159 for (q = after_last_slash; *q; q++)
33161 if (q == last_period)
33163 strcpy (p, section_desc);
33164 p += strlen (section_desc);
33165 break;
33168 else if (ISALNUM (*q))
33169 *p++ = *q;
33172 if (last_period == 0)
33173 strcpy (p, section_desc);
33174 else
33175 *p = '\0';
33178 /* Emit profile function. */
33180 void
33181 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
33183 /* Non-standard profiling for kernels, which just saves LR then calls
33184 _mcount without worrying about arg saves. The idea is to change
33185 the function prologue as little as possible as it isn't easy to
33186 account for arg save/restore code added just for _mcount. */
33187 if (TARGET_PROFILE_KERNEL)
33188 return;
33190 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
33192 #ifndef NO_PROFILE_COUNTERS
33193 # define NO_PROFILE_COUNTERS 0
33194 #endif
33195 if (NO_PROFILE_COUNTERS)
33196 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
33197 LCT_NORMAL, VOIDmode);
33198 else
33200 char buf[30];
33201 const char *label_name;
33202 rtx fun;
33204 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
33205 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
33206 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
33208 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
33209 LCT_NORMAL, VOIDmode, fun, Pmode);
33212 else if (DEFAULT_ABI == ABI_DARWIN)
33214 const char *mcount_name = RS6000_MCOUNT;
33215 int caller_addr_regno = LR_REGNO;
33217 /* Be conservative and always set this, at least for now. */
33218 crtl->uses_pic_offset_table = 1;
33220 #if TARGET_MACHO
33221 /* For PIC code, set up a stub and collect the caller's address
33222 from r0, which is where the prologue puts it. */
33223 if (MACHOPIC_INDIRECT
33224 && crtl->uses_pic_offset_table)
33225 caller_addr_regno = 0;
33226 #endif
33227 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
33228 LCT_NORMAL, VOIDmode,
33229 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
33233 /* Write function profiler code. */
33235 void
33236 output_function_profiler (FILE *file, int labelno)
33238 char buf[100];
33240 switch (DEFAULT_ABI)
33242 default:
33243 gcc_unreachable ();
33245 case ABI_V4:
33246 if (!TARGET_32BIT)
33248 warning (0, "no profiling of 64-bit code for this ABI");
33249 return;
33251 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
33252 fprintf (file, "\tmflr %s\n", reg_names[0]);
33253 if (NO_PROFILE_COUNTERS)
33255 asm_fprintf (file, "\tstw %s,4(%s)\n",
33256 reg_names[0], reg_names[1]);
33258 else if (TARGET_SECURE_PLT && flag_pic)
33260 if (TARGET_LINK_STACK)
33262 char name[32];
33263 get_ppc476_thunk_name (name);
33264 asm_fprintf (file, "\tbl %s\n", name);
33266 else
33267 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
33268 asm_fprintf (file, "\tstw %s,4(%s)\n",
33269 reg_names[0], reg_names[1]);
33270 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
33271 asm_fprintf (file, "\taddis %s,%s,",
33272 reg_names[12], reg_names[12]);
33273 assemble_name (file, buf);
33274 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
33275 assemble_name (file, buf);
33276 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
33278 else if (flag_pic == 1)
33280 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
33281 asm_fprintf (file, "\tstw %s,4(%s)\n",
33282 reg_names[0], reg_names[1]);
33283 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
33284 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
33285 assemble_name (file, buf);
33286 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
33288 else if (flag_pic > 1)
33290 asm_fprintf (file, "\tstw %s,4(%s)\n",
33291 reg_names[0], reg_names[1]);
33292 /* Now, we need to get the address of the label. */
33293 if (TARGET_LINK_STACK)
33295 char name[32];
33296 get_ppc476_thunk_name (name);
33297 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
33298 assemble_name (file, buf);
33299 fputs ("-.\n1:", file);
33300 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
33301 asm_fprintf (file, "\taddi %s,%s,4\n",
33302 reg_names[11], reg_names[11]);
33304 else
33306 fputs ("\tbcl 20,31,1f\n\t.long ", file);
33307 assemble_name (file, buf);
33308 fputs ("-.\n1:", file);
33309 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
33311 asm_fprintf (file, "\tlwz %s,0(%s)\n",
33312 reg_names[0], reg_names[11]);
33313 asm_fprintf (file, "\tadd %s,%s,%s\n",
33314 reg_names[0], reg_names[0], reg_names[11]);
33316 else
33318 asm_fprintf (file, "\tlis %s,", reg_names[12]);
33319 assemble_name (file, buf);
33320 fputs ("@ha\n", file);
33321 asm_fprintf (file, "\tstw %s,4(%s)\n",
33322 reg_names[0], reg_names[1]);
33323 asm_fprintf (file, "\tla %s,", reg_names[0]);
33324 assemble_name (file, buf);
33325 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
33328 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
33329 fprintf (file, "\tbl %s%s\n",
33330 RS6000_MCOUNT, flag_pic ? "@plt" : "");
33331 break;
33333 case ABI_AIX:
33334 case ABI_ELFv2:
33335 case ABI_DARWIN:
33336 /* Don't do anything, done in output_profile_hook (). */
33337 break;
33343 /* The following variable value is the last issued insn. */
33345 static rtx_insn *last_scheduled_insn;
33347 /* The following variable helps to balance issuing of load and
33348 store instructions */
33350 static int load_store_pendulum;
33352 /* The following variable helps pair divide insns during scheduling. */
33353 static int divide_cnt;
33354 /* The following variable helps pair and alternate vector and vector load
33355 insns during scheduling. */
33356 static int vec_pairing;
33359 /* Power4 load update and store update instructions are cracked into a
33360 load or store and an integer insn which are executed in the same cycle.
33361 Branches have their own dispatch slot which does not count against the
33362 GCC issue rate, but it changes the program flow so there are no other
33363 instructions to issue in this cycle. */
33365 static int
33366 rs6000_variable_issue_1 (rtx_insn *insn, int more)
33368 last_scheduled_insn = insn;
33369 if (GET_CODE (PATTERN (insn)) == USE
33370 || GET_CODE (PATTERN (insn)) == CLOBBER)
33372 cached_can_issue_more = more;
33373 return cached_can_issue_more;
33376 if (insn_terminates_group_p (insn, current_group))
33378 cached_can_issue_more = 0;
33379 return cached_can_issue_more;
33382 /* If no reservation, but reach here */
33383 if (recog_memoized (insn) < 0)
33384 return more;
33386 if (rs6000_sched_groups)
33388 if (is_microcoded_insn (insn))
33389 cached_can_issue_more = 0;
33390 else if (is_cracked_insn (insn))
33391 cached_can_issue_more = more > 2 ? more - 2 : 0;
33392 else
33393 cached_can_issue_more = more - 1;
33395 return cached_can_issue_more;
33398 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
33399 return 0;
33401 cached_can_issue_more = more - 1;
33402 return cached_can_issue_more;
33405 static int
33406 rs6000_variable_issue (FILE *stream, int verbose, rtx_insn *insn, int more)
33408 int r = rs6000_variable_issue_1 (insn, more);
33409 if (verbose)
33410 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
33411 return r;
33414 /* Adjust the cost of a scheduling dependency. Return the new cost of
33415 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
33417 static int
33418 rs6000_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
33419 unsigned int)
33421 enum attr_type attr_type;
33423 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
33424 return cost;
33426 switch (dep_type)
33428 case REG_DEP_TRUE:
33430 /* Data dependency; DEP_INSN writes a register that INSN reads
33431 some cycles later. */
33433 /* Separate a load from a narrower, dependent store. */
33434 if ((rs6000_sched_groups || rs6000_cpu_attr == CPU_POWER9)
33435 && GET_CODE (PATTERN (insn)) == SET
33436 && GET_CODE (PATTERN (dep_insn)) == SET
33437 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
33438 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
33439 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
33440 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
33441 return cost + 14;
33443 attr_type = get_attr_type (insn);
33445 switch (attr_type)
33447 case TYPE_JMPREG:
33448 /* Tell the first scheduling pass about the latency between
33449 a mtctr and bctr (and mtlr and br/blr). The first
33450 scheduling pass will not know about this latency since
33451 the mtctr instruction, which has the latency associated
33452 to it, will be generated by reload. */
33453 return 4;
33454 case TYPE_BRANCH:
33455 /* Leave some extra cycles between a compare and its
33456 dependent branch, to inhibit expensive mispredicts. */
33457 if ((rs6000_cpu_attr == CPU_PPC603
33458 || rs6000_cpu_attr == CPU_PPC604
33459 || rs6000_cpu_attr == CPU_PPC604E
33460 || rs6000_cpu_attr == CPU_PPC620
33461 || rs6000_cpu_attr == CPU_PPC630
33462 || rs6000_cpu_attr == CPU_PPC750
33463 || rs6000_cpu_attr == CPU_PPC7400
33464 || rs6000_cpu_attr == CPU_PPC7450
33465 || rs6000_cpu_attr == CPU_PPCE5500
33466 || rs6000_cpu_attr == CPU_PPCE6500
33467 || rs6000_cpu_attr == CPU_POWER4
33468 || rs6000_cpu_attr == CPU_POWER5
33469 || rs6000_cpu_attr == CPU_POWER7
33470 || rs6000_cpu_attr == CPU_POWER8
33471 || rs6000_cpu_attr == CPU_POWER9
33472 || rs6000_cpu_attr == CPU_CELL)
33473 && recog_memoized (dep_insn)
33474 && (INSN_CODE (dep_insn) >= 0))
33476 switch (get_attr_type (dep_insn))
33478 case TYPE_CMP:
33479 case TYPE_FPCOMPARE:
33480 case TYPE_CR_LOGICAL:
33481 case TYPE_DELAYED_CR:
33482 return cost + 2;
33483 case TYPE_EXTS:
33484 case TYPE_MUL:
33485 if (get_attr_dot (dep_insn) == DOT_YES)
33486 return cost + 2;
33487 else
33488 break;
33489 case TYPE_SHIFT:
33490 if (get_attr_dot (dep_insn) == DOT_YES
33491 && get_attr_var_shift (dep_insn) == VAR_SHIFT_NO)
33492 return cost + 2;
33493 else
33494 break;
33495 default:
33496 break;
33498 break;
33500 case TYPE_STORE:
33501 case TYPE_FPSTORE:
33502 if ((rs6000_cpu == PROCESSOR_POWER6)
33503 && recog_memoized (dep_insn)
33504 && (INSN_CODE (dep_insn) >= 0))
33507 if (GET_CODE (PATTERN (insn)) != SET)
33508 /* If this happens, we have to extend this to schedule
33509 optimally. Return default for now. */
33510 return cost;
33512 /* Adjust the cost for the case where the value written
33513 by a fixed point operation is used as the address
33514 gen value on a store. */
33515 switch (get_attr_type (dep_insn))
33517 case TYPE_LOAD:
33518 case TYPE_CNTLZ:
33520 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33521 return get_attr_sign_extend (dep_insn)
33522 == SIGN_EXTEND_YES ? 6 : 4;
33523 break;
33525 case TYPE_SHIFT:
33527 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33528 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
33529 6 : 3;
33530 break;
33532 case TYPE_INTEGER:
33533 case TYPE_ADD:
33534 case TYPE_LOGICAL:
33535 case TYPE_EXTS:
33536 case TYPE_INSERT:
33538 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33539 return 3;
33540 break;
33542 case TYPE_STORE:
33543 case TYPE_FPLOAD:
33544 case TYPE_FPSTORE:
33546 if (get_attr_update (dep_insn) == UPDATE_YES
33547 && ! rs6000_store_data_bypass_p (dep_insn, insn))
33548 return 3;
33549 break;
33551 case TYPE_MUL:
33553 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33554 return 17;
33555 break;
33557 case TYPE_DIV:
33559 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33560 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
33561 break;
33563 default:
33564 break;
33567 break;
33569 case TYPE_LOAD:
33570 if ((rs6000_cpu == PROCESSOR_POWER6)
33571 && recog_memoized (dep_insn)
33572 && (INSN_CODE (dep_insn) >= 0))
33575 /* Adjust the cost for the case where the value written
33576 by a fixed point instruction is used within the address
33577 gen portion of a subsequent load(u)(x) */
33578 switch (get_attr_type (dep_insn))
33580 case TYPE_LOAD:
33581 case TYPE_CNTLZ:
33583 if (set_to_load_agen (dep_insn, insn))
33584 return get_attr_sign_extend (dep_insn)
33585 == SIGN_EXTEND_YES ? 6 : 4;
33586 break;
33588 case TYPE_SHIFT:
33590 if (set_to_load_agen (dep_insn, insn))
33591 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
33592 6 : 3;
33593 break;
33595 case TYPE_INTEGER:
33596 case TYPE_ADD:
33597 case TYPE_LOGICAL:
33598 case TYPE_EXTS:
33599 case TYPE_INSERT:
33601 if (set_to_load_agen (dep_insn, insn))
33602 return 3;
33603 break;
33605 case TYPE_STORE:
33606 case TYPE_FPLOAD:
33607 case TYPE_FPSTORE:
33609 if (get_attr_update (dep_insn) == UPDATE_YES
33610 && set_to_load_agen (dep_insn, insn))
33611 return 3;
33612 break;
33614 case TYPE_MUL:
33616 if (set_to_load_agen (dep_insn, insn))
33617 return 17;
33618 break;
33620 case TYPE_DIV:
33622 if (set_to_load_agen (dep_insn, insn))
33623 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
33624 break;
33626 default:
33627 break;
33630 break;
33632 case TYPE_FPLOAD:
33633 if ((rs6000_cpu == PROCESSOR_POWER6)
33634 && get_attr_update (insn) == UPDATE_NO
33635 && recog_memoized (dep_insn)
33636 && (INSN_CODE (dep_insn) >= 0)
33637 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
33638 return 2;
33640 default:
33641 break;
33644 /* Fall out to return default cost. */
33646 break;
33648 case REG_DEP_OUTPUT:
33649 /* Output dependency; DEP_INSN writes a register that INSN writes some
33650 cycles later. */
33651 if ((rs6000_cpu == PROCESSOR_POWER6)
33652 && recog_memoized (dep_insn)
33653 && (INSN_CODE (dep_insn) >= 0))
33655 attr_type = get_attr_type (insn);
33657 switch (attr_type)
33659 case TYPE_FP:
33660 case TYPE_FPSIMPLE:
33661 if (get_attr_type (dep_insn) == TYPE_FP
33662 || get_attr_type (dep_insn) == TYPE_FPSIMPLE)
33663 return 1;
33664 break;
33665 case TYPE_FPLOAD:
33666 if (get_attr_update (insn) == UPDATE_NO
33667 && get_attr_type (dep_insn) == TYPE_MFFGPR)
33668 return 2;
33669 break;
33670 default:
33671 break;
33674 /* Fall through, no cost for output dependency. */
33675 /* FALLTHRU */
33677 case REG_DEP_ANTI:
33678 /* Anti dependency; DEP_INSN reads a register that INSN writes some
33679 cycles later. */
33680 return 0;
33682 default:
33683 gcc_unreachable ();
33686 return cost;
33689 /* Debug version of rs6000_adjust_cost. */
33691 static int
33692 rs6000_debug_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn,
33693 int cost, unsigned int dw)
33695 int ret = rs6000_adjust_cost (insn, dep_type, dep_insn, cost, dw);
33697 if (ret != cost)
33699 const char *dep;
33701 switch (dep_type)
33703 default: dep = "unknown depencency"; break;
33704 case REG_DEP_TRUE: dep = "data dependency"; break;
33705 case REG_DEP_OUTPUT: dep = "output dependency"; break;
33706 case REG_DEP_ANTI: dep = "anti depencency"; break;
33709 fprintf (stderr,
33710 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
33711 "%s, insn:\n", ret, cost, dep);
33713 debug_rtx (insn);
33716 return ret;
33719 /* The function returns a true if INSN is microcoded.
33720 Return false otherwise. */
33722 static bool
33723 is_microcoded_insn (rtx_insn *insn)
33725 if (!insn || !NONDEBUG_INSN_P (insn)
33726 || GET_CODE (PATTERN (insn)) == USE
33727 || GET_CODE (PATTERN (insn)) == CLOBBER)
33728 return false;
33730 if (rs6000_cpu_attr == CPU_CELL)
33731 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
33733 if (rs6000_sched_groups
33734 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
33736 enum attr_type type = get_attr_type (insn);
33737 if ((type == TYPE_LOAD
33738 && get_attr_update (insn) == UPDATE_YES
33739 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
33740 || ((type == TYPE_LOAD || type == TYPE_STORE)
33741 && get_attr_update (insn) == UPDATE_YES
33742 && get_attr_indexed (insn) == INDEXED_YES)
33743 || type == TYPE_MFCR)
33744 return true;
33747 return false;
33750 /* The function returns true if INSN is cracked into 2 instructions
33751 by the processor (and therefore occupies 2 issue slots). */
33753 static bool
33754 is_cracked_insn (rtx_insn *insn)
33756 if (!insn || !NONDEBUG_INSN_P (insn)
33757 || GET_CODE (PATTERN (insn)) == USE
33758 || GET_CODE (PATTERN (insn)) == CLOBBER)
33759 return false;
33761 if (rs6000_sched_groups
33762 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
33764 enum attr_type type = get_attr_type (insn);
33765 if ((type == TYPE_LOAD
33766 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
33767 && get_attr_update (insn) == UPDATE_NO)
33768 || (type == TYPE_LOAD
33769 && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
33770 && get_attr_update (insn) == UPDATE_YES
33771 && get_attr_indexed (insn) == INDEXED_NO)
33772 || (type == TYPE_STORE
33773 && get_attr_update (insn) == UPDATE_YES
33774 && get_attr_indexed (insn) == INDEXED_NO)
33775 || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
33776 && get_attr_update (insn) == UPDATE_YES)
33777 || type == TYPE_DELAYED_CR
33778 || (type == TYPE_EXTS
33779 && get_attr_dot (insn) == DOT_YES)
33780 || (type == TYPE_SHIFT
33781 && get_attr_dot (insn) == DOT_YES
33782 && get_attr_var_shift (insn) == VAR_SHIFT_NO)
33783 || (type == TYPE_MUL
33784 && get_attr_dot (insn) == DOT_YES)
33785 || type == TYPE_DIV
33786 || (type == TYPE_INSERT
33787 && get_attr_size (insn) == SIZE_32))
33788 return true;
33791 return false;
33794 /* The function returns true if INSN can be issued only from
33795 the branch slot. */
33797 static bool
33798 is_branch_slot_insn (rtx_insn *insn)
33800 if (!insn || !NONDEBUG_INSN_P (insn)
33801 || GET_CODE (PATTERN (insn)) == USE
33802 || GET_CODE (PATTERN (insn)) == CLOBBER)
33803 return false;
33805 if (rs6000_sched_groups)
33807 enum attr_type type = get_attr_type (insn);
33808 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
33809 return true;
33810 return false;
33813 return false;
33816 /* The function returns true if out_inst sets a value that is
33817 used in the address generation computation of in_insn */
33818 static bool
33819 set_to_load_agen (rtx_insn *out_insn, rtx_insn *in_insn)
33821 rtx out_set, in_set;
33823 /* For performance reasons, only handle the simple case where
33824 both loads are a single_set. */
33825 out_set = single_set (out_insn);
33826 if (out_set)
33828 in_set = single_set (in_insn);
33829 if (in_set)
33830 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
33833 return false;
33836 /* Try to determine base/offset/size parts of the given MEM.
33837 Return true if successful, false if all the values couldn't
33838 be determined.
33840 This function only looks for REG or REG+CONST address forms.
33841 REG+REG address form will return false. */
33843 static bool
33844 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
33845 HOST_WIDE_INT *size)
33847 rtx addr_rtx;
33848 if MEM_SIZE_KNOWN_P (mem)
33849 *size = MEM_SIZE (mem);
33850 else
33851 return false;
33853 addr_rtx = (XEXP (mem, 0));
33854 if (GET_CODE (addr_rtx) == PRE_MODIFY)
33855 addr_rtx = XEXP (addr_rtx, 1);
33857 *offset = 0;
33858 while (GET_CODE (addr_rtx) == PLUS
33859 && CONST_INT_P (XEXP (addr_rtx, 1)))
33861 *offset += INTVAL (XEXP (addr_rtx, 1));
33862 addr_rtx = XEXP (addr_rtx, 0);
33864 if (!REG_P (addr_rtx))
33865 return false;
33867 *base = addr_rtx;
33868 return true;
33871 /* The function returns true if the target storage location of
33872 mem1 is adjacent to the target storage location of mem2 */
33873 /* Return 1 if memory locations are adjacent. */
33875 static bool
33876 adjacent_mem_locations (rtx mem1, rtx mem2)
33878 rtx reg1, reg2;
33879 HOST_WIDE_INT off1, size1, off2, size2;
33881 if (get_memref_parts (mem1, &reg1, &off1, &size1)
33882 && get_memref_parts (mem2, &reg2, &off2, &size2))
33883 return ((REGNO (reg1) == REGNO (reg2))
33884 && ((off1 + size1 == off2)
33885 || (off2 + size2 == off1)));
33887 return false;
33890 /* This function returns true if it can be determined that the two MEM
33891 locations overlap by at least 1 byte based on base reg/offset/size. */
33893 static bool
33894 mem_locations_overlap (rtx mem1, rtx mem2)
33896 rtx reg1, reg2;
33897 HOST_WIDE_INT off1, size1, off2, size2;
33899 if (get_memref_parts (mem1, &reg1, &off1, &size1)
33900 && get_memref_parts (mem2, &reg2, &off2, &size2))
33901 return ((REGNO (reg1) == REGNO (reg2))
33902 && (((off1 <= off2) && (off1 + size1 > off2))
33903 || ((off2 <= off1) && (off2 + size2 > off1))));
33905 return false;
33908 /* A C statement (sans semicolon) to update the integer scheduling
33909 priority INSN_PRIORITY (INSN). Increase the priority to execute the
33910 INSN earlier, reduce the priority to execute INSN later. Do not
33911 define this macro if you do not need to adjust the scheduling
33912 priorities of insns. */
33914 static int
33915 rs6000_adjust_priority (rtx_insn *insn ATTRIBUTE_UNUSED, int priority)
33917 rtx load_mem, str_mem;
33918 /* On machines (like the 750) which have asymmetric integer units,
33919 where one integer unit can do multiply and divides and the other
33920 can't, reduce the priority of multiply/divide so it is scheduled
33921 before other integer operations. */
33923 #if 0
33924 if (! INSN_P (insn))
33925 return priority;
33927 if (GET_CODE (PATTERN (insn)) == USE)
33928 return priority;
33930 switch (rs6000_cpu_attr) {
33931 case CPU_PPC750:
33932 switch (get_attr_type (insn))
33934 default:
33935 break;
33937 case TYPE_MUL:
33938 case TYPE_DIV:
33939 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
33940 priority, priority);
33941 if (priority >= 0 && priority < 0x01000000)
33942 priority >>= 3;
33943 break;
33946 #endif
33948 if (insn_must_be_first_in_group (insn)
33949 && reload_completed
33950 && current_sched_info->sched_max_insns_priority
33951 && rs6000_sched_restricted_insns_priority)
33954 /* Prioritize insns that can be dispatched only in the first
33955 dispatch slot. */
33956 if (rs6000_sched_restricted_insns_priority == 1)
33957 /* Attach highest priority to insn. This means that in
33958 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
33959 precede 'priority' (critical path) considerations. */
33960 return current_sched_info->sched_max_insns_priority;
33961 else if (rs6000_sched_restricted_insns_priority == 2)
33962 /* Increase priority of insn by a minimal amount. This means that in
33963 haifa-sched.c:ready_sort(), only 'priority' (critical path)
33964 considerations precede dispatch-slot restriction considerations. */
33965 return (priority + 1);
33968 if (rs6000_cpu == PROCESSOR_POWER6
33969 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
33970 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
33971 /* Attach highest priority to insn if the scheduler has just issued two
33972 stores and this instruction is a load, or two loads and this instruction
33973 is a store. Power6 wants loads and stores scheduled alternately
33974 when possible */
33975 return current_sched_info->sched_max_insns_priority;
33977 return priority;
33980 /* Return true if the instruction is nonpipelined on the Cell. */
33981 static bool
33982 is_nonpipeline_insn (rtx_insn *insn)
33984 enum attr_type type;
33985 if (!insn || !NONDEBUG_INSN_P (insn)
33986 || GET_CODE (PATTERN (insn)) == USE
33987 || GET_CODE (PATTERN (insn)) == CLOBBER)
33988 return false;
33990 type = get_attr_type (insn);
33991 if (type == TYPE_MUL
33992 || type == TYPE_DIV
33993 || type == TYPE_SDIV
33994 || type == TYPE_DDIV
33995 || type == TYPE_SSQRT
33996 || type == TYPE_DSQRT
33997 || type == TYPE_MFCR
33998 || type == TYPE_MFCRF
33999 || type == TYPE_MFJMPR)
34001 return true;
34003 return false;
34007 /* Return how many instructions the machine can issue per cycle. */
34009 static int
34010 rs6000_issue_rate (void)
34012 /* Unless scheduling for register pressure, use issue rate of 1 for
34013 first scheduling pass to decrease degradation. */
34014 if (!reload_completed && !flag_sched_pressure)
34015 return 1;
34017 switch (rs6000_cpu_attr) {
34018 case CPU_RS64A:
34019 case CPU_PPC601: /* ? */
34020 case CPU_PPC7450:
34021 return 3;
34022 case CPU_PPC440:
34023 case CPU_PPC603:
34024 case CPU_PPC750:
34025 case CPU_PPC7400:
34026 case CPU_PPC8540:
34027 case CPU_PPC8548:
34028 case CPU_CELL:
34029 case CPU_PPCE300C2:
34030 case CPU_PPCE300C3:
34031 case CPU_PPCE500MC:
34032 case CPU_PPCE500MC64:
34033 case CPU_PPCE5500:
34034 case CPU_PPCE6500:
34035 case CPU_TITAN:
34036 return 2;
34037 case CPU_PPC476:
34038 case CPU_PPC604:
34039 case CPU_PPC604E:
34040 case CPU_PPC620:
34041 case CPU_PPC630:
34042 return 4;
34043 case CPU_POWER4:
34044 case CPU_POWER5:
34045 case CPU_POWER6:
34046 case CPU_POWER7:
34047 return 5;
34048 case CPU_POWER8:
34049 return 7;
34050 case CPU_POWER9:
34051 return 6;
34052 default:
34053 return 1;
34057 /* Return how many instructions to look ahead for better insn
34058 scheduling. */
34060 static int
34061 rs6000_use_sched_lookahead (void)
34063 switch (rs6000_cpu_attr)
34065 case CPU_PPC8540:
34066 case CPU_PPC8548:
34067 return 4;
34069 case CPU_CELL:
34070 return (reload_completed ? 8 : 0);
34072 default:
34073 return 0;
34077 /* We are choosing insn from the ready queue. Return zero if INSN can be
34078 chosen. */
34079 static int
34080 rs6000_use_sched_lookahead_guard (rtx_insn *insn, int ready_index)
34082 if (ready_index == 0)
34083 return 0;
34085 if (rs6000_cpu_attr != CPU_CELL)
34086 return 0;
34088 gcc_assert (insn != NULL_RTX && INSN_P (insn));
34090 if (!reload_completed
34091 || is_nonpipeline_insn (insn)
34092 || is_microcoded_insn (insn))
34093 return 1;
34095 return 0;
34098 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
34099 and return true. */
34101 static bool
34102 find_mem_ref (rtx pat, rtx *mem_ref)
34104 const char * fmt;
34105 int i, j;
34107 /* stack_tie does not produce any real memory traffic. */
34108 if (tie_operand (pat, VOIDmode))
34109 return false;
34111 if (GET_CODE (pat) == MEM)
34113 *mem_ref = pat;
34114 return true;
34117 /* Recursively process the pattern. */
34118 fmt = GET_RTX_FORMAT (GET_CODE (pat));
34120 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
34122 if (fmt[i] == 'e')
34124 if (find_mem_ref (XEXP (pat, i), mem_ref))
34125 return true;
34127 else if (fmt[i] == 'E')
34128 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
34130 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
34131 return true;
34135 return false;
34138 /* Determine if PAT is a PATTERN of a load insn. */
34140 static bool
34141 is_load_insn1 (rtx pat, rtx *load_mem)
34143 if (!pat || pat == NULL_RTX)
34144 return false;
34146 if (GET_CODE (pat) == SET)
34147 return find_mem_ref (SET_SRC (pat), load_mem);
34149 if (GET_CODE (pat) == PARALLEL)
34151 int i;
34153 for (i = 0; i < XVECLEN (pat, 0); i++)
34154 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
34155 return true;
34158 return false;
34161 /* Determine if INSN loads from memory. */
34163 static bool
34164 is_load_insn (rtx insn, rtx *load_mem)
34166 if (!insn || !INSN_P (insn))
34167 return false;
34169 if (CALL_P (insn))
34170 return false;
34172 return is_load_insn1 (PATTERN (insn), load_mem);
34175 /* Determine if PAT is a PATTERN of a store insn. */
34177 static bool
34178 is_store_insn1 (rtx pat, rtx *str_mem)
34180 if (!pat || pat == NULL_RTX)
34181 return false;
34183 if (GET_CODE (pat) == SET)
34184 return find_mem_ref (SET_DEST (pat), str_mem);
34186 if (GET_CODE (pat) == PARALLEL)
34188 int i;
34190 for (i = 0; i < XVECLEN (pat, 0); i++)
34191 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
34192 return true;
34195 return false;
34198 /* Determine if INSN stores to memory. */
34200 static bool
34201 is_store_insn (rtx insn, rtx *str_mem)
34203 if (!insn || !INSN_P (insn))
34204 return false;
34206 return is_store_insn1 (PATTERN (insn), str_mem);
34209 /* Return whether TYPE is a Power9 pairable vector instruction type. */
34211 static bool
34212 is_power9_pairable_vec_type (enum attr_type type)
34214 switch (type)
34216 case TYPE_VECSIMPLE:
34217 case TYPE_VECCOMPLEX:
34218 case TYPE_VECDIV:
34219 case TYPE_VECCMP:
34220 case TYPE_VECPERM:
34221 case TYPE_VECFLOAT:
34222 case TYPE_VECFDIV:
34223 case TYPE_VECDOUBLE:
34224 return true;
34225 default:
34226 break;
34228 return false;
34231 /* Returns whether the dependence between INSN and NEXT is considered
34232 costly by the given target. */
34234 static bool
34235 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
34237 rtx insn;
34238 rtx next;
34239 rtx load_mem, str_mem;
34241 /* If the flag is not enabled - no dependence is considered costly;
34242 allow all dependent insns in the same group.
34243 This is the most aggressive option. */
34244 if (rs6000_sched_costly_dep == no_dep_costly)
34245 return false;
34247 /* If the flag is set to 1 - a dependence is always considered costly;
34248 do not allow dependent instructions in the same group.
34249 This is the most conservative option. */
34250 if (rs6000_sched_costly_dep == all_deps_costly)
34251 return true;
34253 insn = DEP_PRO (dep);
34254 next = DEP_CON (dep);
34256 if (rs6000_sched_costly_dep == store_to_load_dep_costly
34257 && is_load_insn (next, &load_mem)
34258 && is_store_insn (insn, &str_mem))
34259 /* Prevent load after store in the same group. */
34260 return true;
34262 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
34263 && is_load_insn (next, &load_mem)
34264 && is_store_insn (insn, &str_mem)
34265 && DEP_TYPE (dep) == REG_DEP_TRUE
34266 && mem_locations_overlap(str_mem, load_mem))
34267 /* Prevent load after store in the same group if it is a true
34268 dependence. */
34269 return true;
34271 /* The flag is set to X; dependences with latency >= X are considered costly,
34272 and will not be scheduled in the same group. */
34273 if (rs6000_sched_costly_dep <= max_dep_latency
34274 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
34275 return true;
34277 return false;
34280 /* Return the next insn after INSN that is found before TAIL is reached,
34281 skipping any "non-active" insns - insns that will not actually occupy
34282 an issue slot. Return NULL_RTX if such an insn is not found. */
34284 static rtx_insn *
34285 get_next_active_insn (rtx_insn *insn, rtx_insn *tail)
34287 if (insn == NULL_RTX || insn == tail)
34288 return NULL;
34290 while (1)
34292 insn = NEXT_INSN (insn);
34293 if (insn == NULL_RTX || insn == tail)
34294 return NULL;
34296 if (CALL_P (insn)
34297 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
34298 || (NONJUMP_INSN_P (insn)
34299 && GET_CODE (PATTERN (insn)) != USE
34300 && GET_CODE (PATTERN (insn)) != CLOBBER
34301 && INSN_CODE (insn) != CODE_FOR_stack_tie))
34302 break;
34304 return insn;
34307 /* Do Power9 specific sched_reorder2 reordering of ready list. */
34309 static int
34310 power9_sched_reorder2 (rtx_insn **ready, int lastpos)
34312 int pos;
34313 int i;
34314 rtx_insn *tmp;
34315 enum attr_type type, type2;
34317 type = get_attr_type (last_scheduled_insn);
34319 /* Try to issue fixed point divides back-to-back in pairs so they will be
34320 routed to separate execution units and execute in parallel. */
34321 if (type == TYPE_DIV && divide_cnt == 0)
34323 /* First divide has been scheduled. */
34324 divide_cnt = 1;
34326 /* Scan the ready list looking for another divide, if found move it
34327 to the end of the list so it is chosen next. */
34328 pos = lastpos;
34329 while (pos >= 0)
34331 if (recog_memoized (ready[pos]) >= 0
34332 && get_attr_type (ready[pos]) == TYPE_DIV)
34334 tmp = ready[pos];
34335 for (i = pos; i < lastpos; i++)
34336 ready[i] = ready[i + 1];
34337 ready[lastpos] = tmp;
34338 break;
34340 pos--;
34343 else
34345 /* Last insn was the 2nd divide or not a divide, reset the counter. */
34346 divide_cnt = 0;
34348 /* The best dispatch throughput for vector and vector load insns can be
34349 achieved by interleaving a vector and vector load such that they'll
34350 dispatch to the same superslice. If this pairing cannot be achieved
34351 then it is best to pair vector insns together and vector load insns
34352 together.
34354 To aid in this pairing, vec_pairing maintains the current state with
34355 the following values:
34357 0 : Initial state, no vecload/vector pairing has been started.
34359 1 : A vecload or vector insn has been issued and a candidate for
34360 pairing has been found and moved to the end of the ready
34361 list. */
34362 if (type == TYPE_VECLOAD)
34364 /* Issued a vecload. */
34365 if (vec_pairing == 0)
34367 int vecload_pos = -1;
34368 /* We issued a single vecload, look for a vector insn to pair it
34369 with. If one isn't found, try to pair another vecload. */
34370 pos = lastpos;
34371 while (pos >= 0)
34373 if (recog_memoized (ready[pos]) >= 0)
34375 type2 = get_attr_type (ready[pos]);
34376 if (is_power9_pairable_vec_type (type2))
34378 /* Found a vector insn to pair with, move it to the
34379 end of the ready list so it is scheduled next. */
34380 tmp = ready[pos];
34381 for (i = pos; i < lastpos; i++)
34382 ready[i] = ready[i + 1];
34383 ready[lastpos] = tmp;
34384 vec_pairing = 1;
34385 return cached_can_issue_more;
34387 else if (type2 == TYPE_VECLOAD && vecload_pos == -1)
34388 /* Remember position of first vecload seen. */
34389 vecload_pos = pos;
34391 pos--;
34393 if (vecload_pos >= 0)
34395 /* Didn't find a vector to pair with but did find a vecload,
34396 move it to the end of the ready list. */
34397 tmp = ready[vecload_pos];
34398 for (i = vecload_pos; i < lastpos; i++)
34399 ready[i] = ready[i + 1];
34400 ready[lastpos] = tmp;
34401 vec_pairing = 1;
34402 return cached_can_issue_more;
34406 else if (is_power9_pairable_vec_type (type))
34408 /* Issued a vector operation. */
34409 if (vec_pairing == 0)
34411 int vec_pos = -1;
34412 /* We issued a single vector insn, look for a vecload to pair it
34413 with. If one isn't found, try to pair another vector. */
34414 pos = lastpos;
34415 while (pos >= 0)
34417 if (recog_memoized (ready[pos]) >= 0)
34419 type2 = get_attr_type (ready[pos]);
34420 if (type2 == TYPE_VECLOAD)
34422 /* Found a vecload insn to pair with, move it to the
34423 end of the ready list so it is scheduled next. */
34424 tmp = ready[pos];
34425 for (i = pos; i < lastpos; i++)
34426 ready[i] = ready[i + 1];
34427 ready[lastpos] = tmp;
34428 vec_pairing = 1;
34429 return cached_can_issue_more;
34431 else if (is_power9_pairable_vec_type (type2)
34432 && vec_pos == -1)
34433 /* Remember position of first vector insn seen. */
34434 vec_pos = pos;
34436 pos--;
34438 if (vec_pos >= 0)
34440 /* Didn't find a vecload to pair with but did find a vector
34441 insn, move it to the end of the ready list. */
34442 tmp = ready[vec_pos];
34443 for (i = vec_pos; i < lastpos; i++)
34444 ready[i] = ready[i + 1];
34445 ready[lastpos] = tmp;
34446 vec_pairing = 1;
34447 return cached_can_issue_more;
34452 /* We've either finished a vec/vecload pair, couldn't find an insn to
34453 continue the current pair, or the last insn had nothing to do with
34454 with pairing. In any case, reset the state. */
34455 vec_pairing = 0;
34458 return cached_can_issue_more;
34461 /* We are about to begin issuing insns for this clock cycle. */
34463 static int
34464 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
34465 rtx_insn **ready ATTRIBUTE_UNUSED,
34466 int *pn_ready ATTRIBUTE_UNUSED,
34467 int clock_var ATTRIBUTE_UNUSED)
34469 int n_ready = *pn_ready;
34471 if (sched_verbose)
34472 fprintf (dump, "// rs6000_sched_reorder :\n");
34474 /* Reorder the ready list, if the second to last ready insn
34475 is a nonepipeline insn. */
34476 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
34478 if (is_nonpipeline_insn (ready[n_ready - 1])
34479 && (recog_memoized (ready[n_ready - 2]) > 0))
34480 /* Simply swap first two insns. */
34481 std::swap (ready[n_ready - 1], ready[n_ready - 2]);
34484 if (rs6000_cpu == PROCESSOR_POWER6)
34485 load_store_pendulum = 0;
34487 return rs6000_issue_rate ();
34490 /* Like rs6000_sched_reorder, but called after issuing each insn. */
34492 static int
34493 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx_insn **ready,
34494 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
34496 if (sched_verbose)
34497 fprintf (dump, "// rs6000_sched_reorder2 :\n");
34499 /* For Power6, we need to handle some special cases to try and keep the
34500 store queue from overflowing and triggering expensive flushes.
34502 This code monitors how load and store instructions are being issued
34503 and skews the ready list one way or the other to increase the likelihood
34504 that a desired instruction is issued at the proper time.
34506 A couple of things are done. First, we maintain a "load_store_pendulum"
34507 to track the current state of load/store issue.
34509 - If the pendulum is at zero, then no loads or stores have been
34510 issued in the current cycle so we do nothing.
34512 - If the pendulum is 1, then a single load has been issued in this
34513 cycle and we attempt to locate another load in the ready list to
34514 issue with it.
34516 - If the pendulum is -2, then two stores have already been
34517 issued in this cycle, so we increase the priority of the first load
34518 in the ready list to increase it's likelihood of being chosen first
34519 in the next cycle.
34521 - If the pendulum is -1, then a single store has been issued in this
34522 cycle and we attempt to locate another store in the ready list to
34523 issue with it, preferring a store to an adjacent memory location to
34524 facilitate store pairing in the store queue.
34526 - If the pendulum is 2, then two loads have already been
34527 issued in this cycle, so we increase the priority of the first store
34528 in the ready list to increase it's likelihood of being chosen first
34529 in the next cycle.
34531 - If the pendulum < -2 or > 2, then do nothing.
34533 Note: This code covers the most common scenarios. There exist non
34534 load/store instructions which make use of the LSU and which
34535 would need to be accounted for to strictly model the behavior
34536 of the machine. Those instructions are currently unaccounted
34537 for to help minimize compile time overhead of this code.
34539 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
34541 int pos;
34542 int i;
34543 rtx_insn *tmp;
34544 rtx load_mem, str_mem;
34546 if (is_store_insn (last_scheduled_insn, &str_mem))
34547 /* Issuing a store, swing the load_store_pendulum to the left */
34548 load_store_pendulum--;
34549 else if (is_load_insn (last_scheduled_insn, &load_mem))
34550 /* Issuing a load, swing the load_store_pendulum to the right */
34551 load_store_pendulum++;
34552 else
34553 return cached_can_issue_more;
34555 /* If the pendulum is balanced, or there is only one instruction on
34556 the ready list, then all is well, so return. */
34557 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
34558 return cached_can_issue_more;
34560 if (load_store_pendulum == 1)
34562 /* A load has been issued in this cycle. Scan the ready list
34563 for another load to issue with it */
34564 pos = *pn_ready-1;
34566 while (pos >= 0)
34568 if (is_load_insn (ready[pos], &load_mem))
34570 /* Found a load. Move it to the head of the ready list,
34571 and adjust it's priority so that it is more likely to
34572 stay there */
34573 tmp = ready[pos];
34574 for (i=pos; i<*pn_ready-1; i++)
34575 ready[i] = ready[i + 1];
34576 ready[*pn_ready-1] = tmp;
34578 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
34579 INSN_PRIORITY (tmp)++;
34580 break;
34582 pos--;
34585 else if (load_store_pendulum == -2)
34587 /* Two stores have been issued in this cycle. Increase the
34588 priority of the first load in the ready list to favor it for
34589 issuing in the next cycle. */
34590 pos = *pn_ready-1;
34592 while (pos >= 0)
34594 if (is_load_insn (ready[pos], &load_mem)
34595 && !sel_sched_p ()
34596 && INSN_PRIORITY_KNOWN (ready[pos]))
34598 INSN_PRIORITY (ready[pos])++;
34600 /* Adjust the pendulum to account for the fact that a load
34601 was found and increased in priority. This is to prevent
34602 increasing the priority of multiple loads */
34603 load_store_pendulum--;
34605 break;
34607 pos--;
34610 else if (load_store_pendulum == -1)
34612 /* A store has been issued in this cycle. Scan the ready list for
34613 another store to issue with it, preferring a store to an adjacent
34614 memory location */
34615 int first_store_pos = -1;
34617 pos = *pn_ready-1;
34619 while (pos >= 0)
34621 if (is_store_insn (ready[pos], &str_mem))
34623 rtx str_mem2;
34624 /* Maintain the index of the first store found on the
34625 list */
34626 if (first_store_pos == -1)
34627 first_store_pos = pos;
34629 if (is_store_insn (last_scheduled_insn, &str_mem2)
34630 && adjacent_mem_locations (str_mem, str_mem2))
34632 /* Found an adjacent store. Move it to the head of the
34633 ready list, and adjust it's priority so that it is
34634 more likely to stay there */
34635 tmp = ready[pos];
34636 for (i=pos; i<*pn_ready-1; i++)
34637 ready[i] = ready[i + 1];
34638 ready[*pn_ready-1] = tmp;
34640 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
34641 INSN_PRIORITY (tmp)++;
34643 first_store_pos = -1;
34645 break;
34648 pos--;
34651 if (first_store_pos >= 0)
34653 /* An adjacent store wasn't found, but a non-adjacent store was,
34654 so move the non-adjacent store to the front of the ready
34655 list, and adjust its priority so that it is more likely to
34656 stay there. */
34657 tmp = ready[first_store_pos];
34658 for (i=first_store_pos; i<*pn_ready-1; i++)
34659 ready[i] = ready[i + 1];
34660 ready[*pn_ready-1] = tmp;
34661 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
34662 INSN_PRIORITY (tmp)++;
34665 else if (load_store_pendulum == 2)
34667 /* Two loads have been issued in this cycle. Increase the priority
34668 of the first store in the ready list to favor it for issuing in
34669 the next cycle. */
34670 pos = *pn_ready-1;
34672 while (pos >= 0)
34674 if (is_store_insn (ready[pos], &str_mem)
34675 && !sel_sched_p ()
34676 && INSN_PRIORITY_KNOWN (ready[pos]))
34678 INSN_PRIORITY (ready[pos])++;
34680 /* Adjust the pendulum to account for the fact that a store
34681 was found and increased in priority. This is to prevent
34682 increasing the priority of multiple stores */
34683 load_store_pendulum++;
34685 break;
34687 pos--;
34692 /* Do Power9 dependent reordering if necessary. */
34693 if (rs6000_cpu == PROCESSOR_POWER9 && last_scheduled_insn
34694 && recog_memoized (last_scheduled_insn) >= 0)
34695 return power9_sched_reorder2 (ready, *pn_ready - 1);
34697 return cached_can_issue_more;
34700 /* Return whether the presence of INSN causes a dispatch group termination
34701 of group WHICH_GROUP.
34703 If WHICH_GROUP == current_group, this function will return true if INSN
34704 causes the termination of the current group (i.e, the dispatch group to
34705 which INSN belongs). This means that INSN will be the last insn in the
34706 group it belongs to.
34708 If WHICH_GROUP == previous_group, this function will return true if INSN
34709 causes the termination of the previous group (i.e, the dispatch group that
34710 precedes the group to which INSN belongs). This means that INSN will be
34711 the first insn in the group it belongs to). */
34713 static bool
34714 insn_terminates_group_p (rtx_insn *insn, enum group_termination which_group)
34716 bool first, last;
34718 if (! insn)
34719 return false;
34721 first = insn_must_be_first_in_group (insn);
34722 last = insn_must_be_last_in_group (insn);
34724 if (first && last)
34725 return true;
34727 if (which_group == current_group)
34728 return last;
34729 else if (which_group == previous_group)
34730 return first;
34732 return false;
34736 static bool
34737 insn_must_be_first_in_group (rtx_insn *insn)
34739 enum attr_type type;
34741 if (!insn
34742 || NOTE_P (insn)
34743 || DEBUG_INSN_P (insn)
34744 || GET_CODE (PATTERN (insn)) == USE
34745 || GET_CODE (PATTERN (insn)) == CLOBBER)
34746 return false;
34748 switch (rs6000_cpu)
34750 case PROCESSOR_POWER5:
34751 if (is_cracked_insn (insn))
34752 return true;
34753 /* FALLTHRU */
34754 case PROCESSOR_POWER4:
34755 if (is_microcoded_insn (insn))
34756 return true;
34758 if (!rs6000_sched_groups)
34759 return false;
34761 type = get_attr_type (insn);
34763 switch (type)
34765 case TYPE_MFCR:
34766 case TYPE_MFCRF:
34767 case TYPE_MTCR:
34768 case TYPE_DELAYED_CR:
34769 case TYPE_CR_LOGICAL:
34770 case TYPE_MTJMPR:
34771 case TYPE_MFJMPR:
34772 case TYPE_DIV:
34773 case TYPE_LOAD_L:
34774 case TYPE_STORE_C:
34775 case TYPE_ISYNC:
34776 case TYPE_SYNC:
34777 return true;
34778 default:
34779 break;
34781 break;
34782 case PROCESSOR_POWER6:
34783 type = get_attr_type (insn);
34785 switch (type)
34787 case TYPE_EXTS:
34788 case TYPE_CNTLZ:
34789 case TYPE_TRAP:
34790 case TYPE_MUL:
34791 case TYPE_INSERT:
34792 case TYPE_FPCOMPARE:
34793 case TYPE_MFCR:
34794 case TYPE_MTCR:
34795 case TYPE_MFJMPR:
34796 case TYPE_MTJMPR:
34797 case TYPE_ISYNC:
34798 case TYPE_SYNC:
34799 case TYPE_LOAD_L:
34800 case TYPE_STORE_C:
34801 return true;
34802 case TYPE_SHIFT:
34803 if (get_attr_dot (insn) == DOT_NO
34804 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
34805 return true;
34806 else
34807 break;
34808 case TYPE_DIV:
34809 if (get_attr_size (insn) == SIZE_32)
34810 return true;
34811 else
34812 break;
34813 case TYPE_LOAD:
34814 case TYPE_STORE:
34815 case TYPE_FPLOAD:
34816 case TYPE_FPSTORE:
34817 if (get_attr_update (insn) == UPDATE_YES)
34818 return true;
34819 else
34820 break;
34821 default:
34822 break;
34824 break;
34825 case PROCESSOR_POWER7:
34826 type = get_attr_type (insn);
34828 switch (type)
34830 case TYPE_CR_LOGICAL:
34831 case TYPE_MFCR:
34832 case TYPE_MFCRF:
34833 case TYPE_MTCR:
34834 case TYPE_DIV:
34835 case TYPE_ISYNC:
34836 case TYPE_LOAD_L:
34837 case TYPE_STORE_C:
34838 case TYPE_MFJMPR:
34839 case TYPE_MTJMPR:
34840 return true;
34841 case TYPE_MUL:
34842 case TYPE_SHIFT:
34843 case TYPE_EXTS:
34844 if (get_attr_dot (insn) == DOT_YES)
34845 return true;
34846 else
34847 break;
34848 case TYPE_LOAD:
34849 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34850 || get_attr_update (insn) == UPDATE_YES)
34851 return true;
34852 else
34853 break;
34854 case TYPE_STORE:
34855 case TYPE_FPLOAD:
34856 case TYPE_FPSTORE:
34857 if (get_attr_update (insn) == UPDATE_YES)
34858 return true;
34859 else
34860 break;
34861 default:
34862 break;
34864 break;
34865 case PROCESSOR_POWER8:
34866 type = get_attr_type (insn);
34868 switch (type)
34870 case TYPE_CR_LOGICAL:
34871 case TYPE_DELAYED_CR:
34872 case TYPE_MFCR:
34873 case TYPE_MFCRF:
34874 case TYPE_MTCR:
34875 case TYPE_SYNC:
34876 case TYPE_ISYNC:
34877 case TYPE_LOAD_L:
34878 case TYPE_STORE_C:
34879 case TYPE_VECSTORE:
34880 case TYPE_MFJMPR:
34881 case TYPE_MTJMPR:
34882 return true;
34883 case TYPE_SHIFT:
34884 case TYPE_EXTS:
34885 case TYPE_MUL:
34886 if (get_attr_dot (insn) == DOT_YES)
34887 return true;
34888 else
34889 break;
34890 case TYPE_LOAD:
34891 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34892 || get_attr_update (insn) == UPDATE_YES)
34893 return true;
34894 else
34895 break;
34896 case TYPE_STORE:
34897 if (get_attr_update (insn) == UPDATE_YES
34898 && get_attr_indexed (insn) == INDEXED_YES)
34899 return true;
34900 else
34901 break;
34902 default:
34903 break;
34905 break;
34906 default:
34907 break;
34910 return false;
34913 static bool
34914 insn_must_be_last_in_group (rtx_insn *insn)
34916 enum attr_type type;
34918 if (!insn
34919 || NOTE_P (insn)
34920 || DEBUG_INSN_P (insn)
34921 || GET_CODE (PATTERN (insn)) == USE
34922 || GET_CODE (PATTERN (insn)) == CLOBBER)
34923 return false;
34925 switch (rs6000_cpu) {
34926 case PROCESSOR_POWER4:
34927 case PROCESSOR_POWER5:
34928 if (is_microcoded_insn (insn))
34929 return true;
34931 if (is_branch_slot_insn (insn))
34932 return true;
34934 break;
34935 case PROCESSOR_POWER6:
34936 type = get_attr_type (insn);
34938 switch (type)
34940 case TYPE_EXTS:
34941 case TYPE_CNTLZ:
34942 case TYPE_TRAP:
34943 case TYPE_MUL:
34944 case TYPE_FPCOMPARE:
34945 case TYPE_MFCR:
34946 case TYPE_MTCR:
34947 case TYPE_MFJMPR:
34948 case TYPE_MTJMPR:
34949 case TYPE_ISYNC:
34950 case TYPE_SYNC:
34951 case TYPE_LOAD_L:
34952 case TYPE_STORE_C:
34953 return true;
34954 case TYPE_SHIFT:
34955 if (get_attr_dot (insn) == DOT_NO
34956 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
34957 return true;
34958 else
34959 break;
34960 case TYPE_DIV:
34961 if (get_attr_size (insn) == SIZE_32)
34962 return true;
34963 else
34964 break;
34965 default:
34966 break;
34968 break;
34969 case PROCESSOR_POWER7:
34970 type = get_attr_type (insn);
34972 switch (type)
34974 case TYPE_ISYNC:
34975 case TYPE_SYNC:
34976 case TYPE_LOAD_L:
34977 case TYPE_STORE_C:
34978 return true;
34979 case TYPE_LOAD:
34980 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34981 && get_attr_update (insn) == UPDATE_YES)
34982 return true;
34983 else
34984 break;
34985 case TYPE_STORE:
34986 if (get_attr_update (insn) == UPDATE_YES
34987 && get_attr_indexed (insn) == INDEXED_YES)
34988 return true;
34989 else
34990 break;
34991 default:
34992 break;
34994 break;
34995 case PROCESSOR_POWER8:
34996 type = get_attr_type (insn);
34998 switch (type)
35000 case TYPE_MFCR:
35001 case TYPE_MTCR:
35002 case TYPE_ISYNC:
35003 case TYPE_SYNC:
35004 case TYPE_LOAD_L:
35005 case TYPE_STORE_C:
35006 return true;
35007 case TYPE_LOAD:
35008 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
35009 && get_attr_update (insn) == UPDATE_YES)
35010 return true;
35011 else
35012 break;
35013 case TYPE_STORE:
35014 if (get_attr_update (insn) == UPDATE_YES
35015 && get_attr_indexed (insn) == INDEXED_YES)
35016 return true;
35017 else
35018 break;
35019 default:
35020 break;
35022 break;
35023 default:
35024 break;
35027 return false;
35030 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
35031 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
35033 static bool
35034 is_costly_group (rtx *group_insns, rtx next_insn)
35036 int i;
35037 int issue_rate = rs6000_issue_rate ();
35039 for (i = 0; i < issue_rate; i++)
35041 sd_iterator_def sd_it;
35042 dep_t dep;
35043 rtx insn = group_insns[i];
35045 if (!insn)
35046 continue;
35048 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
35050 rtx next = DEP_CON (dep);
35052 if (next == next_insn
35053 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
35054 return true;
35058 return false;
35061 /* Utility of the function redefine_groups.
35062 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
35063 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
35064 to keep it "far" (in a separate group) from GROUP_INSNS, following
35065 one of the following schemes, depending on the value of the flag
35066 -minsert_sched_nops = X:
35067 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
35068 in order to force NEXT_INSN into a separate group.
35069 (2) X < sched_finish_regroup_exact: insert exactly X nops.
35070 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
35071 insertion (has a group just ended, how many vacant issue slots remain in the
35072 last group, and how many dispatch groups were encountered so far). */
35074 static int
35075 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
35076 rtx_insn *next_insn, bool *group_end, int can_issue_more,
35077 int *group_count)
35079 rtx nop;
35080 bool force;
35081 int issue_rate = rs6000_issue_rate ();
35082 bool end = *group_end;
35083 int i;
35085 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
35086 return can_issue_more;
35088 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
35089 return can_issue_more;
35091 force = is_costly_group (group_insns, next_insn);
35092 if (!force)
35093 return can_issue_more;
35095 if (sched_verbose > 6)
35096 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
35097 *group_count ,can_issue_more);
35099 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
35101 if (*group_end)
35102 can_issue_more = 0;
35104 /* Since only a branch can be issued in the last issue_slot, it is
35105 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
35106 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
35107 in this case the last nop will start a new group and the branch
35108 will be forced to the new group. */
35109 if (can_issue_more && !is_branch_slot_insn (next_insn))
35110 can_issue_more--;
35112 /* Do we have a special group ending nop? */
35113 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
35114 || rs6000_cpu_attr == CPU_POWER8)
35116 nop = gen_group_ending_nop ();
35117 emit_insn_before (nop, next_insn);
35118 can_issue_more = 0;
35120 else
35121 while (can_issue_more > 0)
35123 nop = gen_nop ();
35124 emit_insn_before (nop, next_insn);
35125 can_issue_more--;
35128 *group_end = true;
35129 return 0;
35132 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
35134 int n_nops = rs6000_sched_insert_nops;
35136 /* Nops can't be issued from the branch slot, so the effective
35137 issue_rate for nops is 'issue_rate - 1'. */
35138 if (can_issue_more == 0)
35139 can_issue_more = issue_rate;
35140 can_issue_more--;
35141 if (can_issue_more == 0)
35143 can_issue_more = issue_rate - 1;
35144 (*group_count)++;
35145 end = true;
35146 for (i = 0; i < issue_rate; i++)
35148 group_insns[i] = 0;
35152 while (n_nops > 0)
35154 nop = gen_nop ();
35155 emit_insn_before (nop, next_insn);
35156 if (can_issue_more == issue_rate - 1) /* new group begins */
35157 end = false;
35158 can_issue_more--;
35159 if (can_issue_more == 0)
35161 can_issue_more = issue_rate - 1;
35162 (*group_count)++;
35163 end = true;
35164 for (i = 0; i < issue_rate; i++)
35166 group_insns[i] = 0;
35169 n_nops--;
35172 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
35173 can_issue_more++;
35175 /* Is next_insn going to start a new group? */
35176 *group_end
35177 = (end
35178 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
35179 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
35180 || (can_issue_more < issue_rate &&
35181 insn_terminates_group_p (next_insn, previous_group)));
35182 if (*group_end && end)
35183 (*group_count)--;
35185 if (sched_verbose > 6)
35186 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
35187 *group_count, can_issue_more);
35188 return can_issue_more;
35191 return can_issue_more;
35194 /* This function tries to synch the dispatch groups that the compiler "sees"
35195 with the dispatch groups that the processor dispatcher is expected to
35196 form in practice. It tries to achieve this synchronization by forcing the
35197 estimated processor grouping on the compiler (as opposed to the function
35198 'pad_goups' which tries to force the scheduler's grouping on the processor).
35200 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
35201 examines the (estimated) dispatch groups that will be formed by the processor
35202 dispatcher. It marks these group boundaries to reflect the estimated
35203 processor grouping, overriding the grouping that the scheduler had marked.
35204 Depending on the value of the flag '-minsert-sched-nops' this function can
35205 force certain insns into separate groups or force a certain distance between
35206 them by inserting nops, for example, if there exists a "costly dependence"
35207 between the insns.
35209 The function estimates the group boundaries that the processor will form as
35210 follows: It keeps track of how many vacant issue slots are available after
35211 each insn. A subsequent insn will start a new group if one of the following
35212 4 cases applies:
35213 - no more vacant issue slots remain in the current dispatch group.
35214 - only the last issue slot, which is the branch slot, is vacant, but the next
35215 insn is not a branch.
35216 - only the last 2 or less issue slots, including the branch slot, are vacant,
35217 which means that a cracked insn (which occupies two issue slots) can't be
35218 issued in this group.
35219 - less than 'issue_rate' slots are vacant, and the next insn always needs to
35220 start a new group. */
35222 static int
35223 redefine_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
35224 rtx_insn *tail)
35226 rtx_insn *insn, *next_insn;
35227 int issue_rate;
35228 int can_issue_more;
35229 int slot, i;
35230 bool group_end;
35231 int group_count = 0;
35232 rtx *group_insns;
35234 /* Initialize. */
35235 issue_rate = rs6000_issue_rate ();
35236 group_insns = XALLOCAVEC (rtx, issue_rate);
35237 for (i = 0; i < issue_rate; i++)
35239 group_insns[i] = 0;
35241 can_issue_more = issue_rate;
35242 slot = 0;
35243 insn = get_next_active_insn (prev_head_insn, tail);
35244 group_end = false;
35246 while (insn != NULL_RTX)
35248 slot = (issue_rate - can_issue_more);
35249 group_insns[slot] = insn;
35250 can_issue_more =
35251 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
35252 if (insn_terminates_group_p (insn, current_group))
35253 can_issue_more = 0;
35255 next_insn = get_next_active_insn (insn, tail);
35256 if (next_insn == NULL_RTX)
35257 return group_count + 1;
35259 /* Is next_insn going to start a new group? */
35260 group_end
35261 = (can_issue_more == 0
35262 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
35263 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
35264 || (can_issue_more < issue_rate &&
35265 insn_terminates_group_p (next_insn, previous_group)));
35267 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
35268 next_insn, &group_end, can_issue_more,
35269 &group_count);
35271 if (group_end)
35273 group_count++;
35274 can_issue_more = 0;
35275 for (i = 0; i < issue_rate; i++)
35277 group_insns[i] = 0;
35281 if (GET_MODE (next_insn) == TImode && can_issue_more)
35282 PUT_MODE (next_insn, VOIDmode);
35283 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
35284 PUT_MODE (next_insn, TImode);
35286 insn = next_insn;
35287 if (can_issue_more == 0)
35288 can_issue_more = issue_rate;
35289 } /* while */
35291 return group_count;
35294 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
35295 dispatch group boundaries that the scheduler had marked. Pad with nops
35296 any dispatch groups which have vacant issue slots, in order to force the
35297 scheduler's grouping on the processor dispatcher. The function
35298 returns the number of dispatch groups found. */
35300 static int
35301 pad_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
35302 rtx_insn *tail)
35304 rtx_insn *insn, *next_insn;
35305 rtx nop;
35306 int issue_rate;
35307 int can_issue_more;
35308 int group_end;
35309 int group_count = 0;
35311 /* Initialize issue_rate. */
35312 issue_rate = rs6000_issue_rate ();
35313 can_issue_more = issue_rate;
35315 insn = get_next_active_insn (prev_head_insn, tail);
35316 next_insn = get_next_active_insn (insn, tail);
35318 while (insn != NULL_RTX)
35320 can_issue_more =
35321 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
35323 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
35325 if (next_insn == NULL_RTX)
35326 break;
35328 if (group_end)
35330 /* If the scheduler had marked group termination at this location
35331 (between insn and next_insn), and neither insn nor next_insn will
35332 force group termination, pad the group with nops to force group
35333 termination. */
35334 if (can_issue_more
35335 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
35336 && !insn_terminates_group_p (insn, current_group)
35337 && !insn_terminates_group_p (next_insn, previous_group))
35339 if (!is_branch_slot_insn (next_insn))
35340 can_issue_more--;
35342 while (can_issue_more)
35344 nop = gen_nop ();
35345 emit_insn_before (nop, next_insn);
35346 can_issue_more--;
35350 can_issue_more = issue_rate;
35351 group_count++;
35354 insn = next_insn;
35355 next_insn = get_next_active_insn (insn, tail);
35358 return group_count;
35361 /* We're beginning a new block. Initialize data structures as necessary. */
35363 static void
35364 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
35365 int sched_verbose ATTRIBUTE_UNUSED,
35366 int max_ready ATTRIBUTE_UNUSED)
35368 last_scheduled_insn = NULL;
35369 load_store_pendulum = 0;
35370 divide_cnt = 0;
35371 vec_pairing = 0;
35374 /* The following function is called at the end of scheduling BB.
35375 After reload, it inserts nops at insn group bundling. */
35377 static void
35378 rs6000_sched_finish (FILE *dump, int sched_verbose)
35380 int n_groups;
35382 if (sched_verbose)
35383 fprintf (dump, "=== Finishing schedule.\n");
35385 if (reload_completed && rs6000_sched_groups)
35387 /* Do not run sched_finish hook when selective scheduling enabled. */
35388 if (sel_sched_p ())
35389 return;
35391 if (rs6000_sched_insert_nops == sched_finish_none)
35392 return;
35394 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
35395 n_groups = pad_groups (dump, sched_verbose,
35396 current_sched_info->prev_head,
35397 current_sched_info->next_tail);
35398 else
35399 n_groups = redefine_groups (dump, sched_verbose,
35400 current_sched_info->prev_head,
35401 current_sched_info->next_tail);
35403 if (sched_verbose >= 6)
35405 fprintf (dump, "ngroups = %d\n", n_groups);
35406 print_rtl (dump, current_sched_info->prev_head);
35407 fprintf (dump, "Done finish_sched\n");
35412 struct rs6000_sched_context
35414 short cached_can_issue_more;
35415 rtx_insn *last_scheduled_insn;
35416 int load_store_pendulum;
35417 int divide_cnt;
35418 int vec_pairing;
35421 typedef struct rs6000_sched_context rs6000_sched_context_def;
35422 typedef rs6000_sched_context_def *rs6000_sched_context_t;
35424 /* Allocate store for new scheduling context. */
35425 static void *
35426 rs6000_alloc_sched_context (void)
35428 return xmalloc (sizeof (rs6000_sched_context_def));
35431 /* If CLEAN_P is true then initializes _SC with clean data,
35432 and from the global context otherwise. */
35433 static void
35434 rs6000_init_sched_context (void *_sc, bool clean_p)
35436 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
35438 if (clean_p)
35440 sc->cached_can_issue_more = 0;
35441 sc->last_scheduled_insn = NULL;
35442 sc->load_store_pendulum = 0;
35443 sc->divide_cnt = 0;
35444 sc->vec_pairing = 0;
35446 else
35448 sc->cached_can_issue_more = cached_can_issue_more;
35449 sc->last_scheduled_insn = last_scheduled_insn;
35450 sc->load_store_pendulum = load_store_pendulum;
35451 sc->divide_cnt = divide_cnt;
35452 sc->vec_pairing = vec_pairing;
35456 /* Sets the global scheduling context to the one pointed to by _SC. */
35457 static void
35458 rs6000_set_sched_context (void *_sc)
35460 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
35462 gcc_assert (sc != NULL);
35464 cached_can_issue_more = sc->cached_can_issue_more;
35465 last_scheduled_insn = sc->last_scheduled_insn;
35466 load_store_pendulum = sc->load_store_pendulum;
35467 divide_cnt = sc->divide_cnt;
35468 vec_pairing = sc->vec_pairing;
35471 /* Free _SC. */
35472 static void
35473 rs6000_free_sched_context (void *_sc)
35475 gcc_assert (_sc != NULL);
35477 free (_sc);
35480 static bool
35481 rs6000_sched_can_speculate_insn (rtx_insn *insn)
35483 switch (get_attr_type (insn))
35485 case TYPE_DIV:
35486 case TYPE_SDIV:
35487 case TYPE_DDIV:
35488 case TYPE_VECDIV:
35489 case TYPE_SSQRT:
35490 case TYPE_DSQRT:
35491 return false;
35493 default:
35494 return true;
35498 /* Length in units of the trampoline for entering a nested function. */
35501 rs6000_trampoline_size (void)
35503 int ret = 0;
35505 switch (DEFAULT_ABI)
35507 default:
35508 gcc_unreachable ();
35510 case ABI_AIX:
35511 ret = (TARGET_32BIT) ? 12 : 24;
35512 break;
35514 case ABI_ELFv2:
35515 gcc_assert (!TARGET_32BIT);
35516 ret = 32;
35517 break;
35519 case ABI_DARWIN:
35520 case ABI_V4:
35521 ret = (TARGET_32BIT) ? 40 : 48;
35522 break;
35525 return ret;
35528 /* Emit RTL insns to initialize the variable parts of a trampoline.
35529 FNADDR is an RTX for the address of the function's pure code.
35530 CXT is an RTX for the static chain value for the function. */
35532 static void
35533 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
35535 int regsize = (TARGET_32BIT) ? 4 : 8;
35536 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
35537 rtx ctx_reg = force_reg (Pmode, cxt);
35538 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
35540 switch (DEFAULT_ABI)
35542 default:
35543 gcc_unreachable ();
35545 /* Under AIX, just build the 3 word function descriptor */
35546 case ABI_AIX:
35548 rtx fnmem, fn_reg, toc_reg;
35550 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
35551 error ("You cannot take the address of a nested function if you use "
35552 "the -mno-pointers-to-nested-functions option.");
35554 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
35555 fn_reg = gen_reg_rtx (Pmode);
35556 toc_reg = gen_reg_rtx (Pmode);
35558 /* Macro to shorten the code expansions below. */
35559 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
35561 m_tramp = replace_equiv_address (m_tramp, addr);
35563 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
35564 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
35565 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
35566 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
35567 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
35569 # undef MEM_PLUS
35571 break;
35573 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
35574 case ABI_ELFv2:
35575 case ABI_DARWIN:
35576 case ABI_V4:
35577 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
35578 LCT_NORMAL, VOIDmode,
35579 addr, Pmode,
35580 GEN_INT (rs6000_trampoline_size ()), SImode,
35581 fnaddr, Pmode,
35582 ctx_reg, Pmode);
35583 break;
35588 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
35589 identifier as an argument, so the front end shouldn't look it up. */
35591 static bool
35592 rs6000_attribute_takes_identifier_p (const_tree attr_id)
35594 return is_attribute_p ("altivec", attr_id);
35597 /* Handle the "altivec" attribute. The attribute may have
35598 arguments as follows:
35600 __attribute__((altivec(vector__)))
35601 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
35602 __attribute__((altivec(bool__))) (always followed by 'unsigned')
35604 and may appear more than once (e.g., 'vector bool char') in a
35605 given declaration. */
35607 static tree
35608 rs6000_handle_altivec_attribute (tree *node,
35609 tree name ATTRIBUTE_UNUSED,
35610 tree args,
35611 int flags ATTRIBUTE_UNUSED,
35612 bool *no_add_attrs)
35614 tree type = *node, result = NULL_TREE;
35615 machine_mode mode;
35616 int unsigned_p;
35617 char altivec_type
35618 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
35619 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
35620 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
35621 : '?');
35623 while (POINTER_TYPE_P (type)
35624 || TREE_CODE (type) == FUNCTION_TYPE
35625 || TREE_CODE (type) == METHOD_TYPE
35626 || TREE_CODE (type) == ARRAY_TYPE)
35627 type = TREE_TYPE (type);
35629 mode = TYPE_MODE (type);
35631 /* Check for invalid AltiVec type qualifiers. */
35632 if (type == long_double_type_node)
35633 error ("use of %<long double%> in AltiVec types is invalid");
35634 else if (type == boolean_type_node)
35635 error ("use of boolean types in AltiVec types is invalid");
35636 else if (TREE_CODE (type) == COMPLEX_TYPE)
35637 error ("use of %<complex%> in AltiVec types is invalid");
35638 else if (DECIMAL_FLOAT_MODE_P (mode))
35639 error ("use of decimal floating point types in AltiVec types is invalid");
35640 else if (!TARGET_VSX)
35642 if (type == long_unsigned_type_node || type == long_integer_type_node)
35644 if (TARGET_64BIT)
35645 error ("use of %<long%> in AltiVec types is invalid for "
35646 "64-bit code without -mvsx");
35647 else if (rs6000_warn_altivec_long)
35648 warning (0, "use of %<long%> in AltiVec types is deprecated; "
35649 "use %<int%>");
35651 else if (type == long_long_unsigned_type_node
35652 || type == long_long_integer_type_node)
35653 error ("use of %<long long%> in AltiVec types is invalid without "
35654 "-mvsx");
35655 else if (type == double_type_node)
35656 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
35659 switch (altivec_type)
35661 case 'v':
35662 unsigned_p = TYPE_UNSIGNED (type);
35663 switch (mode)
35665 case E_TImode:
35666 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
35667 break;
35668 case E_DImode:
35669 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
35670 break;
35671 case E_SImode:
35672 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
35673 break;
35674 case E_HImode:
35675 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
35676 break;
35677 case E_QImode:
35678 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
35679 break;
35680 case E_SFmode: result = V4SF_type_node; break;
35681 case E_DFmode: result = V2DF_type_node; break;
35682 /* If the user says 'vector int bool', we may be handed the 'bool'
35683 attribute _before_ the 'vector' attribute, and so select the
35684 proper type in the 'b' case below. */
35685 case E_V4SImode: case E_V8HImode: case E_V16QImode: case E_V4SFmode:
35686 case E_V2DImode: case E_V2DFmode:
35687 result = type;
35688 default: break;
35690 break;
35691 case 'b':
35692 switch (mode)
35694 case E_DImode: case E_V2DImode: result = bool_V2DI_type_node; break;
35695 case E_SImode: case E_V4SImode: result = bool_V4SI_type_node; break;
35696 case E_HImode: case E_V8HImode: result = bool_V8HI_type_node; break;
35697 case E_QImode: case E_V16QImode: result = bool_V16QI_type_node;
35698 default: break;
35700 break;
35701 case 'p':
35702 switch (mode)
35704 case E_V8HImode: result = pixel_V8HI_type_node;
35705 default: break;
35707 default: break;
35710 /* Propagate qualifiers attached to the element type
35711 onto the vector type. */
35712 if (result && result != type && TYPE_QUALS (type))
35713 result = build_qualified_type (result, TYPE_QUALS (type));
35715 *no_add_attrs = true; /* No need to hang on to the attribute. */
35717 if (result)
35718 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
35720 return NULL_TREE;
35723 /* AltiVec defines four built-in scalar types that serve as vector
35724 elements; we must teach the compiler how to mangle them. */
35726 static const char *
35727 rs6000_mangle_type (const_tree type)
35729 type = TYPE_MAIN_VARIANT (type);
35731 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
35732 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
35733 return NULL;
35735 if (type == bool_char_type_node) return "U6__boolc";
35736 if (type == bool_short_type_node) return "U6__bools";
35737 if (type == pixel_type_node) return "u7__pixel";
35738 if (type == bool_int_type_node) return "U6__booli";
35739 if (type == bool_long_type_node) return "U6__booll";
35741 /* Use a unique name for __float128 rather than trying to use "e" or "g". Use
35742 "g" for IBM extended double, no matter whether it is long double (using
35743 -mabi=ibmlongdouble) or the distinct __ibm128 type. */
35744 if (TARGET_FLOAT128_TYPE)
35746 if (type == ieee128_float_type_node)
35747 return "U10__float128";
35749 if (type == ibm128_float_type_node)
35750 return "g";
35752 if (type == long_double_type_node && TARGET_LONG_DOUBLE_128)
35753 return (TARGET_IEEEQUAD) ? "U10__float128" : "g";
35756 /* Mangle IBM extended float long double as `g' (__float128) on
35757 powerpc*-linux where long-double-64 previously was the default. */
35758 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
35759 && TARGET_ELF
35760 && TARGET_LONG_DOUBLE_128
35761 && !TARGET_IEEEQUAD)
35762 return "g";
35764 /* For all other types, use normal C++ mangling. */
35765 return NULL;
35768 /* Handle a "longcall" or "shortcall" attribute; arguments as in
35769 struct attribute_spec.handler. */
35771 static tree
35772 rs6000_handle_longcall_attribute (tree *node, tree name,
35773 tree args ATTRIBUTE_UNUSED,
35774 int flags ATTRIBUTE_UNUSED,
35775 bool *no_add_attrs)
35777 if (TREE_CODE (*node) != FUNCTION_TYPE
35778 && TREE_CODE (*node) != FIELD_DECL
35779 && TREE_CODE (*node) != TYPE_DECL)
35781 warning (OPT_Wattributes, "%qE attribute only applies to functions",
35782 name);
35783 *no_add_attrs = true;
35786 return NULL_TREE;
35789 /* Set longcall attributes on all functions declared when
35790 rs6000_default_long_calls is true. */
35791 static void
35792 rs6000_set_default_type_attributes (tree type)
35794 if (rs6000_default_long_calls
35795 && (TREE_CODE (type) == FUNCTION_TYPE
35796 || TREE_CODE (type) == METHOD_TYPE))
35797 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
35798 NULL_TREE,
35799 TYPE_ATTRIBUTES (type));
35801 #if TARGET_MACHO
35802 darwin_set_default_type_attributes (type);
35803 #endif
35806 /* Return a reference suitable for calling a function with the
35807 longcall attribute. */
35810 rs6000_longcall_ref (rtx call_ref)
35812 const char *call_name;
35813 tree node;
35815 if (GET_CODE (call_ref) != SYMBOL_REF)
35816 return call_ref;
35818 /* System V adds '.' to the internal name, so skip them. */
35819 call_name = XSTR (call_ref, 0);
35820 if (*call_name == '.')
35822 while (*call_name == '.')
35823 call_name++;
35825 node = get_identifier (call_name);
35826 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
35829 return force_reg (Pmode, call_ref);
35832 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
35833 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
35834 #endif
35836 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
35837 struct attribute_spec.handler. */
35838 static tree
35839 rs6000_handle_struct_attribute (tree *node, tree name,
35840 tree args ATTRIBUTE_UNUSED,
35841 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
35843 tree *type = NULL;
35844 if (DECL_P (*node))
35846 if (TREE_CODE (*node) == TYPE_DECL)
35847 type = &TREE_TYPE (*node);
35849 else
35850 type = node;
35852 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
35853 || TREE_CODE (*type) == UNION_TYPE)))
35855 warning (OPT_Wattributes, "%qE attribute ignored", name);
35856 *no_add_attrs = true;
35859 else if ((is_attribute_p ("ms_struct", name)
35860 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
35861 || ((is_attribute_p ("gcc_struct", name)
35862 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
35864 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
35865 name);
35866 *no_add_attrs = true;
35869 return NULL_TREE;
35872 static bool
35873 rs6000_ms_bitfield_layout_p (const_tree record_type)
35875 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
35876 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
35877 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
35880 #ifdef USING_ELFOS_H
35882 /* A get_unnamed_section callback, used for switching to toc_section. */
35884 static void
35885 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
35887 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
35888 && TARGET_MINIMAL_TOC)
35890 if (!toc_initialized)
35892 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
35893 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35894 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
35895 fprintf (asm_out_file, "\t.tc ");
35896 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
35897 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
35898 fprintf (asm_out_file, "\n");
35900 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
35901 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35902 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
35903 fprintf (asm_out_file, " = .+32768\n");
35904 toc_initialized = 1;
35906 else
35907 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
35909 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
35911 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
35912 if (!toc_initialized)
35914 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35915 toc_initialized = 1;
35918 else
35920 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
35921 if (!toc_initialized)
35923 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35924 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
35925 fprintf (asm_out_file, " = .+32768\n");
35926 toc_initialized = 1;
35931 /* Implement TARGET_ASM_INIT_SECTIONS. */
35933 static void
35934 rs6000_elf_asm_init_sections (void)
35936 toc_section
35937 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
35939 sdata2_section
35940 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
35941 SDATA2_SECTION_ASM_OP);
35944 /* Implement TARGET_SELECT_RTX_SECTION. */
35946 static section *
35947 rs6000_elf_select_rtx_section (machine_mode mode, rtx x,
35948 unsigned HOST_WIDE_INT align)
35950 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
35951 return toc_section;
35952 else
35953 return default_elf_select_rtx_section (mode, x, align);
35956 /* For a SYMBOL_REF, set generic flags and then perform some
35957 target-specific processing.
35959 When the AIX ABI is requested on a non-AIX system, replace the
35960 function name with the real name (with a leading .) rather than the
35961 function descriptor name. This saves a lot of overriding code to
35962 read the prefixes. */
35964 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
35965 static void
35966 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
35968 default_encode_section_info (decl, rtl, first);
35970 if (first
35971 && TREE_CODE (decl) == FUNCTION_DECL
35972 && !TARGET_AIX
35973 && DEFAULT_ABI == ABI_AIX)
35975 rtx sym_ref = XEXP (rtl, 0);
35976 size_t len = strlen (XSTR (sym_ref, 0));
35977 char *str = XALLOCAVEC (char, len + 2);
35978 str[0] = '.';
35979 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
35980 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
35984 static inline bool
35985 compare_section_name (const char *section, const char *templ)
35987 int len;
35989 len = strlen (templ);
35990 return (strncmp (section, templ, len) == 0
35991 && (section[len] == 0 || section[len] == '.'));
35994 bool
35995 rs6000_elf_in_small_data_p (const_tree decl)
35997 if (rs6000_sdata == SDATA_NONE)
35998 return false;
36000 /* We want to merge strings, so we never consider them small data. */
36001 if (TREE_CODE (decl) == STRING_CST)
36002 return false;
36004 /* Functions are never in the small data area. */
36005 if (TREE_CODE (decl) == FUNCTION_DECL)
36006 return false;
36008 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
36010 const char *section = DECL_SECTION_NAME (decl);
36011 if (compare_section_name (section, ".sdata")
36012 || compare_section_name (section, ".sdata2")
36013 || compare_section_name (section, ".gnu.linkonce.s")
36014 || compare_section_name (section, ".sbss")
36015 || compare_section_name (section, ".sbss2")
36016 || compare_section_name (section, ".gnu.linkonce.sb")
36017 || strcmp (section, ".PPC.EMB.sdata0") == 0
36018 || strcmp (section, ".PPC.EMB.sbss0") == 0)
36019 return true;
36021 else
36023 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
36025 if (size > 0
36026 && size <= g_switch_value
36027 /* If it's not public, and we're not going to reference it there,
36028 there's no need to put it in the small data section. */
36029 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
36030 return true;
36033 return false;
36036 #endif /* USING_ELFOS_H */
36038 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
36040 static bool
36041 rs6000_use_blocks_for_constant_p (machine_mode mode, const_rtx x)
36043 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
36046 /* Do not place thread-local symbols refs in the object blocks. */
36048 static bool
36049 rs6000_use_blocks_for_decl_p (const_tree decl)
36051 return !DECL_THREAD_LOCAL_P (decl);
36054 /* Return a REG that occurs in ADDR with coefficient 1.
36055 ADDR can be effectively incremented by incrementing REG.
36057 r0 is special and we must not select it as an address
36058 register by this routine since our caller will try to
36059 increment the returned register via an "la" instruction. */
36062 find_addr_reg (rtx addr)
36064 while (GET_CODE (addr) == PLUS)
36066 if (GET_CODE (XEXP (addr, 0)) == REG
36067 && REGNO (XEXP (addr, 0)) != 0)
36068 addr = XEXP (addr, 0);
36069 else if (GET_CODE (XEXP (addr, 1)) == REG
36070 && REGNO (XEXP (addr, 1)) != 0)
36071 addr = XEXP (addr, 1);
36072 else if (CONSTANT_P (XEXP (addr, 0)))
36073 addr = XEXP (addr, 1);
36074 else if (CONSTANT_P (XEXP (addr, 1)))
36075 addr = XEXP (addr, 0);
36076 else
36077 gcc_unreachable ();
36079 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
36080 return addr;
36083 void
36084 rs6000_fatal_bad_address (rtx op)
36086 fatal_insn ("bad address", op);
36089 #if TARGET_MACHO
36091 typedef struct branch_island_d {
36092 tree function_name;
36093 tree label_name;
36094 int line_number;
36095 } branch_island;
36098 static vec<branch_island, va_gc> *branch_islands;
36100 /* Remember to generate a branch island for far calls to the given
36101 function. */
36103 static void
36104 add_compiler_branch_island (tree label_name, tree function_name,
36105 int line_number)
36107 branch_island bi = {function_name, label_name, line_number};
36108 vec_safe_push (branch_islands, bi);
36111 /* Generate far-jump branch islands for everything recorded in
36112 branch_islands. Invoked immediately after the last instruction of
36113 the epilogue has been emitted; the branch islands must be appended
36114 to, and contiguous with, the function body. Mach-O stubs are
36115 generated in machopic_output_stub(). */
36117 static void
36118 macho_branch_islands (void)
36120 char tmp_buf[512];
36122 while (!vec_safe_is_empty (branch_islands))
36124 branch_island *bi = &branch_islands->last ();
36125 const char *label = IDENTIFIER_POINTER (bi->label_name);
36126 const char *name = IDENTIFIER_POINTER (bi->function_name);
36127 char name_buf[512];
36128 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
36129 if (name[0] == '*' || name[0] == '&')
36130 strcpy (name_buf, name+1);
36131 else
36133 name_buf[0] = '_';
36134 strcpy (name_buf+1, name);
36136 strcpy (tmp_buf, "\n");
36137 strcat (tmp_buf, label);
36138 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
36139 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
36140 dbxout_stabd (N_SLINE, bi->line_number);
36141 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
36142 if (flag_pic)
36144 if (TARGET_LINK_STACK)
36146 char name[32];
36147 get_ppc476_thunk_name (name);
36148 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
36149 strcat (tmp_buf, name);
36150 strcat (tmp_buf, "\n");
36151 strcat (tmp_buf, label);
36152 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
36154 else
36156 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
36157 strcat (tmp_buf, label);
36158 strcat (tmp_buf, "_pic\n");
36159 strcat (tmp_buf, label);
36160 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
36163 strcat (tmp_buf, "\taddis r11,r11,ha16(");
36164 strcat (tmp_buf, name_buf);
36165 strcat (tmp_buf, " - ");
36166 strcat (tmp_buf, label);
36167 strcat (tmp_buf, "_pic)\n");
36169 strcat (tmp_buf, "\tmtlr r0\n");
36171 strcat (tmp_buf, "\taddi r12,r11,lo16(");
36172 strcat (tmp_buf, name_buf);
36173 strcat (tmp_buf, " - ");
36174 strcat (tmp_buf, label);
36175 strcat (tmp_buf, "_pic)\n");
36177 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
36179 else
36181 strcat (tmp_buf, ":\nlis r12,hi16(");
36182 strcat (tmp_buf, name_buf);
36183 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
36184 strcat (tmp_buf, name_buf);
36185 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
36187 output_asm_insn (tmp_buf, 0);
36188 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
36189 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
36190 dbxout_stabd (N_SLINE, bi->line_number);
36191 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
36192 branch_islands->pop ();
36196 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
36197 already there or not. */
36199 static int
36200 no_previous_def (tree function_name)
36202 branch_island *bi;
36203 unsigned ix;
36205 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
36206 if (function_name == bi->function_name)
36207 return 0;
36208 return 1;
36211 /* GET_PREV_LABEL gets the label name from the previous definition of
36212 the function. */
36214 static tree
36215 get_prev_label (tree function_name)
36217 branch_island *bi;
36218 unsigned ix;
36220 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
36221 if (function_name == bi->function_name)
36222 return bi->label_name;
36223 return NULL_TREE;
36226 /* INSN is either a function call or a millicode call. It may have an
36227 unconditional jump in its delay slot.
36229 CALL_DEST is the routine we are calling. */
36231 char *
36232 output_call (rtx_insn *insn, rtx *operands, int dest_operand_number,
36233 int cookie_operand_number)
36235 static char buf[256];
36236 if (darwin_emit_branch_islands
36237 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
36238 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
36240 tree labelname;
36241 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
36243 if (no_previous_def (funname))
36245 rtx label_rtx = gen_label_rtx ();
36246 char *label_buf, temp_buf[256];
36247 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
36248 CODE_LABEL_NUMBER (label_rtx));
36249 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
36250 labelname = get_identifier (label_buf);
36251 add_compiler_branch_island (labelname, funname, insn_line (insn));
36253 else
36254 labelname = get_prev_label (funname);
36256 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
36257 instruction will reach 'foo', otherwise link as 'bl L42'".
36258 "L42" should be a 'branch island', that will do a far jump to
36259 'foo'. Branch islands are generated in
36260 macho_branch_islands(). */
36261 sprintf (buf, "jbsr %%z%d,%.246s",
36262 dest_operand_number, IDENTIFIER_POINTER (labelname));
36264 else
36265 sprintf (buf, "bl %%z%d", dest_operand_number);
36266 return buf;
36269 /* Generate PIC and indirect symbol stubs. */
36271 void
36272 machopic_output_stub (FILE *file, const char *symb, const char *stub)
36274 unsigned int length;
36275 char *symbol_name, *lazy_ptr_name;
36276 char *local_label_0;
36277 static int label = 0;
36279 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
36280 symb = (*targetm.strip_name_encoding) (symb);
36283 length = strlen (symb);
36284 symbol_name = XALLOCAVEC (char, length + 32);
36285 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
36287 lazy_ptr_name = XALLOCAVEC (char, length + 32);
36288 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
36290 if (flag_pic == 2)
36291 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
36292 else
36293 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
36295 if (flag_pic == 2)
36297 fprintf (file, "\t.align 5\n");
36299 fprintf (file, "%s:\n", stub);
36300 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
36302 label++;
36303 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
36304 sprintf (local_label_0, "\"L%011d$spb\"", label);
36306 fprintf (file, "\tmflr r0\n");
36307 if (TARGET_LINK_STACK)
36309 char name[32];
36310 get_ppc476_thunk_name (name);
36311 fprintf (file, "\tbl %s\n", name);
36312 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
36314 else
36316 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
36317 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
36319 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
36320 lazy_ptr_name, local_label_0);
36321 fprintf (file, "\tmtlr r0\n");
36322 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
36323 (TARGET_64BIT ? "ldu" : "lwzu"),
36324 lazy_ptr_name, local_label_0);
36325 fprintf (file, "\tmtctr r12\n");
36326 fprintf (file, "\tbctr\n");
36328 else
36330 fprintf (file, "\t.align 4\n");
36332 fprintf (file, "%s:\n", stub);
36333 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
36335 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
36336 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
36337 (TARGET_64BIT ? "ldu" : "lwzu"),
36338 lazy_ptr_name);
36339 fprintf (file, "\tmtctr r12\n");
36340 fprintf (file, "\tbctr\n");
36343 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
36344 fprintf (file, "%s:\n", lazy_ptr_name);
36345 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
36346 fprintf (file, "%sdyld_stub_binding_helper\n",
36347 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
36350 /* Legitimize PIC addresses. If the address is already
36351 position-independent, we return ORIG. Newly generated
36352 position-independent addresses go into a reg. This is REG if non
36353 zero, otherwise we allocate register(s) as necessary. */
36355 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
36358 rs6000_machopic_legitimize_pic_address (rtx orig, machine_mode mode,
36359 rtx reg)
36361 rtx base, offset;
36363 if (reg == NULL && ! reload_in_progress && ! reload_completed)
36364 reg = gen_reg_rtx (Pmode);
36366 if (GET_CODE (orig) == CONST)
36368 rtx reg_temp;
36370 if (GET_CODE (XEXP (orig, 0)) == PLUS
36371 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
36372 return orig;
36374 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
36376 /* Use a different reg for the intermediate value, as
36377 it will be marked UNCHANGING. */
36378 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
36379 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
36380 Pmode, reg_temp);
36381 offset =
36382 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
36383 Pmode, reg);
36385 if (GET_CODE (offset) == CONST_INT)
36387 if (SMALL_INT (offset))
36388 return plus_constant (Pmode, base, INTVAL (offset));
36389 else if (! reload_in_progress && ! reload_completed)
36390 offset = force_reg (Pmode, offset);
36391 else
36393 rtx mem = force_const_mem (Pmode, orig);
36394 return machopic_legitimize_pic_address (mem, Pmode, reg);
36397 return gen_rtx_PLUS (Pmode, base, offset);
36400 /* Fall back on generic machopic code. */
36401 return machopic_legitimize_pic_address (orig, mode, reg);
36404 /* Output a .machine directive for the Darwin assembler, and call
36405 the generic start_file routine. */
36407 static void
36408 rs6000_darwin_file_start (void)
36410 static const struct
36412 const char *arg;
36413 const char *name;
36414 HOST_WIDE_INT if_set;
36415 } mapping[] = {
36416 { "ppc64", "ppc64", MASK_64BIT },
36417 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
36418 { "power4", "ppc970", 0 },
36419 { "G5", "ppc970", 0 },
36420 { "7450", "ppc7450", 0 },
36421 { "7400", "ppc7400", MASK_ALTIVEC },
36422 { "G4", "ppc7400", 0 },
36423 { "750", "ppc750", 0 },
36424 { "740", "ppc750", 0 },
36425 { "G3", "ppc750", 0 },
36426 { "604e", "ppc604e", 0 },
36427 { "604", "ppc604", 0 },
36428 { "603e", "ppc603", 0 },
36429 { "603", "ppc603", 0 },
36430 { "601", "ppc601", 0 },
36431 { NULL, "ppc", 0 } };
36432 const char *cpu_id = "";
36433 size_t i;
36435 rs6000_file_start ();
36436 darwin_file_start ();
36438 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
36440 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
36441 cpu_id = rs6000_default_cpu;
36443 if (global_options_set.x_rs6000_cpu_index)
36444 cpu_id = processor_target_table[rs6000_cpu_index].name;
36446 /* Look through the mapping array. Pick the first name that either
36447 matches the argument, has a bit set in IF_SET that is also set
36448 in the target flags, or has a NULL name. */
36450 i = 0;
36451 while (mapping[i].arg != NULL
36452 && strcmp (mapping[i].arg, cpu_id) != 0
36453 && (mapping[i].if_set & rs6000_isa_flags) == 0)
36454 i++;
36456 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
36459 #endif /* TARGET_MACHO */
36461 #if TARGET_ELF
36462 static int
36463 rs6000_elf_reloc_rw_mask (void)
36465 if (flag_pic)
36466 return 3;
36467 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
36468 return 2;
36469 else
36470 return 0;
36473 /* Record an element in the table of global constructors. SYMBOL is
36474 a SYMBOL_REF of the function to be called; PRIORITY is a number
36475 between 0 and MAX_INIT_PRIORITY.
36477 This differs from default_named_section_asm_out_constructor in
36478 that we have special handling for -mrelocatable. */
36480 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
36481 static void
36482 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
36484 const char *section = ".ctors";
36485 char buf[18];
36487 if (priority != DEFAULT_INIT_PRIORITY)
36489 sprintf (buf, ".ctors.%.5u",
36490 /* Invert the numbering so the linker puts us in the proper
36491 order; constructors are run from right to left, and the
36492 linker sorts in increasing order. */
36493 MAX_INIT_PRIORITY - priority);
36494 section = buf;
36497 switch_to_section (get_section (section, SECTION_WRITE, NULL));
36498 assemble_align (POINTER_SIZE);
36500 if (DEFAULT_ABI == ABI_V4
36501 && (TARGET_RELOCATABLE || flag_pic > 1))
36503 fputs ("\t.long (", asm_out_file);
36504 output_addr_const (asm_out_file, symbol);
36505 fputs (")@fixup\n", asm_out_file);
36507 else
36508 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
36511 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
36512 static void
36513 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
36515 const char *section = ".dtors";
36516 char buf[18];
36518 if (priority != DEFAULT_INIT_PRIORITY)
36520 sprintf (buf, ".dtors.%.5u",
36521 /* Invert the numbering so the linker puts us in the proper
36522 order; constructors are run from right to left, and the
36523 linker sorts in increasing order. */
36524 MAX_INIT_PRIORITY - priority);
36525 section = buf;
36528 switch_to_section (get_section (section, SECTION_WRITE, NULL));
36529 assemble_align (POINTER_SIZE);
36531 if (DEFAULT_ABI == ABI_V4
36532 && (TARGET_RELOCATABLE || flag_pic > 1))
36534 fputs ("\t.long (", asm_out_file);
36535 output_addr_const (asm_out_file, symbol);
36536 fputs (")@fixup\n", asm_out_file);
36538 else
36539 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
36542 void
36543 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
36545 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
36547 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
36548 ASM_OUTPUT_LABEL (file, name);
36549 fputs (DOUBLE_INT_ASM_OP, file);
36550 rs6000_output_function_entry (file, name);
36551 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
36552 if (DOT_SYMBOLS)
36554 fputs ("\t.size\t", file);
36555 assemble_name (file, name);
36556 fputs (",24\n\t.type\t.", file);
36557 assemble_name (file, name);
36558 fputs (",@function\n", file);
36559 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
36561 fputs ("\t.globl\t.", file);
36562 assemble_name (file, name);
36563 putc ('\n', file);
36566 else
36567 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
36568 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
36569 rs6000_output_function_entry (file, name);
36570 fputs (":\n", file);
36571 return;
36574 if (DEFAULT_ABI == ABI_V4
36575 && (TARGET_RELOCATABLE || flag_pic > 1)
36576 && !TARGET_SECURE_PLT
36577 && (!constant_pool_empty_p () || crtl->profile)
36578 && uses_TOC ())
36580 char buf[256];
36582 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
36584 fprintf (file, "\t.long ");
36585 assemble_name (file, toc_label_name);
36586 need_toc_init = 1;
36587 putc ('-', file);
36588 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
36589 assemble_name (file, buf);
36590 putc ('\n', file);
36593 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
36594 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
36596 if (TARGET_CMODEL == CMODEL_LARGE && rs6000_global_entry_point_needed_p ())
36598 char buf[256];
36600 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
36602 fprintf (file, "\t.quad .TOC.-");
36603 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
36604 assemble_name (file, buf);
36605 putc ('\n', file);
36608 if (DEFAULT_ABI == ABI_AIX)
36610 const char *desc_name, *orig_name;
36612 orig_name = (*targetm.strip_name_encoding) (name);
36613 desc_name = orig_name;
36614 while (*desc_name == '.')
36615 desc_name++;
36617 if (TREE_PUBLIC (decl))
36618 fprintf (file, "\t.globl %s\n", desc_name);
36620 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
36621 fprintf (file, "%s:\n", desc_name);
36622 fprintf (file, "\t.long %s\n", orig_name);
36623 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
36624 fputs ("\t.long 0\n", file);
36625 fprintf (file, "\t.previous\n");
36627 ASM_OUTPUT_LABEL (file, name);
36630 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
36631 static void
36632 rs6000_elf_file_end (void)
36634 #ifdef HAVE_AS_GNU_ATTRIBUTE
36635 /* ??? The value emitted depends on options active at file end.
36636 Assume anyone using #pragma or attributes that might change
36637 options knows what they are doing. */
36638 if ((TARGET_64BIT || DEFAULT_ABI == ABI_V4)
36639 && rs6000_passes_float)
36641 int fp;
36643 if (TARGET_DF_FPR | TARGET_DF_SPE)
36644 fp = 1;
36645 else if (TARGET_SF_FPR | TARGET_SF_SPE)
36646 fp = 3;
36647 else
36648 fp = 2;
36649 if (rs6000_passes_long_double)
36651 if (!TARGET_LONG_DOUBLE_128)
36652 fp |= 2 * 4;
36653 else if (TARGET_IEEEQUAD)
36654 fp |= 3 * 4;
36655 else
36656 fp |= 1 * 4;
36658 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", fp);
36660 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
36662 if (rs6000_passes_vector)
36663 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
36664 (TARGET_ALTIVEC_ABI ? 2
36665 : TARGET_SPE_ABI ? 3
36666 : 1));
36667 if (rs6000_returns_struct)
36668 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
36669 aix_struct_return ? 2 : 1);
36671 #endif
36672 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
36673 if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
36674 file_end_indicate_exec_stack ();
36675 #endif
36677 if (flag_split_stack)
36678 file_end_indicate_split_stack ();
36680 if (cpu_builtin_p)
36682 /* We have expanded a CPU builtin, so we need to emit a reference to
36683 the special symbol that LIBC uses to declare it supports the
36684 AT_PLATFORM and AT_HWCAP/AT_HWCAP2 in the TCB feature. */
36685 switch_to_section (data_section);
36686 fprintf (asm_out_file, "\t.align %u\n", TARGET_32BIT ? 2 : 3);
36687 fprintf (asm_out_file, "\t%s %s\n",
36688 TARGET_32BIT ? ".long" : ".quad", tcb_verification_symbol);
36691 #endif
36693 #if TARGET_XCOFF
36695 #ifndef HAVE_XCOFF_DWARF_EXTRAS
36696 #define HAVE_XCOFF_DWARF_EXTRAS 0
36697 #endif
36699 static enum unwind_info_type
36700 rs6000_xcoff_debug_unwind_info (void)
36702 return UI_NONE;
36705 static void
36706 rs6000_xcoff_asm_output_anchor (rtx symbol)
36708 char buffer[100];
36710 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
36711 SYMBOL_REF_BLOCK_OFFSET (symbol));
36712 fprintf (asm_out_file, "%s", SET_ASM_OP);
36713 RS6000_OUTPUT_BASENAME (asm_out_file, XSTR (symbol, 0));
36714 fprintf (asm_out_file, ",");
36715 RS6000_OUTPUT_BASENAME (asm_out_file, buffer);
36716 fprintf (asm_out_file, "\n");
36719 static void
36720 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
36722 fputs (GLOBAL_ASM_OP, stream);
36723 RS6000_OUTPUT_BASENAME (stream, name);
36724 putc ('\n', stream);
36727 /* A get_unnamed_decl callback, used for read-only sections. PTR
36728 points to the section string variable. */
36730 static void
36731 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
36733 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
36734 *(const char *const *) directive,
36735 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
36738 /* Likewise for read-write sections. */
36740 static void
36741 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
36743 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
36744 *(const char *const *) directive,
36745 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
36748 static void
36749 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
36751 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
36752 *(const char *const *) directive,
36753 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
36756 /* A get_unnamed_section callback, used for switching to toc_section. */
36758 static void
36759 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
36761 if (TARGET_MINIMAL_TOC)
36763 /* toc_section is always selected at least once from
36764 rs6000_xcoff_file_start, so this is guaranteed to
36765 always be defined once and only once in each file. */
36766 if (!toc_initialized)
36768 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
36769 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
36770 toc_initialized = 1;
36772 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
36773 (TARGET_32BIT ? "" : ",3"));
36775 else
36776 fputs ("\t.toc\n", asm_out_file);
36779 /* Implement TARGET_ASM_INIT_SECTIONS. */
36781 static void
36782 rs6000_xcoff_asm_init_sections (void)
36784 read_only_data_section
36785 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
36786 &xcoff_read_only_section_name);
36788 private_data_section
36789 = get_unnamed_section (SECTION_WRITE,
36790 rs6000_xcoff_output_readwrite_section_asm_op,
36791 &xcoff_private_data_section_name);
36793 tls_data_section
36794 = get_unnamed_section (SECTION_TLS,
36795 rs6000_xcoff_output_tls_section_asm_op,
36796 &xcoff_tls_data_section_name);
36798 tls_private_data_section
36799 = get_unnamed_section (SECTION_TLS,
36800 rs6000_xcoff_output_tls_section_asm_op,
36801 &xcoff_private_data_section_name);
36803 read_only_private_data_section
36804 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
36805 &xcoff_private_data_section_name);
36807 toc_section
36808 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
36810 readonly_data_section = read_only_data_section;
36813 static int
36814 rs6000_xcoff_reloc_rw_mask (void)
36816 return 3;
36819 static void
36820 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
36821 tree decl ATTRIBUTE_UNUSED)
36823 int smclass;
36824 static const char * const suffix[5] = { "PR", "RO", "RW", "TL", "XO" };
36826 if (flags & SECTION_EXCLUDE)
36827 smclass = 4;
36828 else if (flags & SECTION_DEBUG)
36830 fprintf (asm_out_file, "\t.dwsect %s\n", name);
36831 return;
36833 else if (flags & SECTION_CODE)
36834 smclass = 0;
36835 else if (flags & SECTION_TLS)
36836 smclass = 3;
36837 else if (flags & SECTION_WRITE)
36838 smclass = 2;
36839 else
36840 smclass = 1;
36842 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
36843 (flags & SECTION_CODE) ? "." : "",
36844 name, suffix[smclass], flags & SECTION_ENTSIZE);
36847 #define IN_NAMED_SECTION(DECL) \
36848 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
36849 && DECL_SECTION_NAME (DECL) != NULL)
36851 static section *
36852 rs6000_xcoff_select_section (tree decl, int reloc,
36853 unsigned HOST_WIDE_INT align)
36855 /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
36856 named section. */
36857 if (align > BIGGEST_ALIGNMENT)
36859 resolve_unique_section (decl, reloc, true);
36860 if (IN_NAMED_SECTION (decl))
36861 return get_named_section (decl, NULL, reloc);
36864 if (decl_readonly_section (decl, reloc))
36866 if (TREE_PUBLIC (decl))
36867 return read_only_data_section;
36868 else
36869 return read_only_private_data_section;
36871 else
36873 #if HAVE_AS_TLS
36874 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
36876 if (TREE_PUBLIC (decl))
36877 return tls_data_section;
36878 else if (bss_initializer_p (decl))
36880 /* Convert to COMMON to emit in BSS. */
36881 DECL_COMMON (decl) = 1;
36882 return tls_comm_section;
36884 else
36885 return tls_private_data_section;
36887 else
36888 #endif
36889 if (TREE_PUBLIC (decl))
36890 return data_section;
36891 else
36892 return private_data_section;
36896 static void
36897 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
36899 const char *name;
36901 /* Use select_section for private data and uninitialized data with
36902 alignment <= BIGGEST_ALIGNMENT. */
36903 if (!TREE_PUBLIC (decl)
36904 || DECL_COMMON (decl)
36905 || (DECL_INITIAL (decl) == NULL_TREE
36906 && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
36907 || DECL_INITIAL (decl) == error_mark_node
36908 || (flag_zero_initialized_in_bss
36909 && initializer_zerop (DECL_INITIAL (decl))))
36910 return;
36912 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
36913 name = (*targetm.strip_name_encoding) (name);
36914 set_decl_section_name (decl, name);
36917 /* Select section for constant in constant pool.
36919 On RS/6000, all constants are in the private read-only data area.
36920 However, if this is being placed in the TOC it must be output as a
36921 toc entry. */
36923 static section *
36924 rs6000_xcoff_select_rtx_section (machine_mode mode, rtx x,
36925 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
36927 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
36928 return toc_section;
36929 else
36930 return read_only_private_data_section;
36933 /* Remove any trailing [DS] or the like from the symbol name. */
36935 static const char *
36936 rs6000_xcoff_strip_name_encoding (const char *name)
36938 size_t len;
36939 if (*name == '*')
36940 name++;
36941 len = strlen (name);
36942 if (name[len - 1] == ']')
36943 return ggc_alloc_string (name, len - 4);
36944 else
36945 return name;
36948 /* Section attributes. AIX is always PIC. */
36950 static unsigned int
36951 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
36953 unsigned int align;
36954 unsigned int flags = default_section_type_flags (decl, name, reloc);
36956 /* Align to at least UNIT size. */
36957 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
36958 align = MIN_UNITS_PER_WORD;
36959 else
36960 /* Increase alignment of large objects if not already stricter. */
36961 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
36962 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
36963 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
36965 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
36968 /* Output at beginning of assembler file.
36970 Initialize the section names for the RS/6000 at this point.
36972 Specify filename, including full path, to assembler.
36974 We want to go into the TOC section so at least one .toc will be emitted.
36975 Also, in order to output proper .bs/.es pairs, we need at least one static
36976 [RW] section emitted.
36978 Finally, declare mcount when profiling to make the assembler happy. */
36980 static void
36981 rs6000_xcoff_file_start (void)
36983 rs6000_gen_section_name (&xcoff_bss_section_name,
36984 main_input_filename, ".bss_");
36985 rs6000_gen_section_name (&xcoff_private_data_section_name,
36986 main_input_filename, ".rw_");
36987 rs6000_gen_section_name (&xcoff_read_only_section_name,
36988 main_input_filename, ".ro_");
36989 rs6000_gen_section_name (&xcoff_tls_data_section_name,
36990 main_input_filename, ".tls_");
36991 rs6000_gen_section_name (&xcoff_tbss_section_name,
36992 main_input_filename, ".tbss_[UL]");
36994 fputs ("\t.file\t", asm_out_file);
36995 output_quoted_string (asm_out_file, main_input_filename);
36996 fputc ('\n', asm_out_file);
36997 if (write_symbols != NO_DEBUG)
36998 switch_to_section (private_data_section);
36999 switch_to_section (toc_section);
37000 switch_to_section (text_section);
37001 if (profile_flag)
37002 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
37003 rs6000_file_start ();
37006 /* Output at end of assembler file.
37007 On the RS/6000, referencing data should automatically pull in text. */
37009 static void
37010 rs6000_xcoff_file_end (void)
37012 switch_to_section (text_section);
37013 fputs ("_section_.text:\n", asm_out_file);
37014 switch_to_section (data_section);
37015 fputs (TARGET_32BIT
37016 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
37017 asm_out_file);
37020 struct declare_alias_data
37022 FILE *file;
37023 bool function_descriptor;
37026 /* Declare alias N. A helper function for for_node_and_aliases. */
37028 static bool
37029 rs6000_declare_alias (struct symtab_node *n, void *d)
37031 struct declare_alias_data *data = (struct declare_alias_data *)d;
37032 /* Main symbol is output specially, because varasm machinery does part of
37033 the job for us - we do not need to declare .globl/lglobs and such. */
37034 if (!n->alias || n->weakref)
37035 return false;
37037 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (n->decl)))
37038 return false;
37040 /* Prevent assemble_alias from trying to use .set pseudo operation
37041 that does not behave as expected by the middle-end. */
37042 TREE_ASM_WRITTEN (n->decl) = true;
37044 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl));
37045 char *buffer = (char *) alloca (strlen (name) + 2);
37046 char *p;
37047 int dollar_inside = 0;
37049 strcpy (buffer, name);
37050 p = strchr (buffer, '$');
37051 while (p) {
37052 *p = '_';
37053 dollar_inside++;
37054 p = strchr (p + 1, '$');
37056 if (TREE_PUBLIC (n->decl))
37058 if (!RS6000_WEAK || !DECL_WEAK (n->decl))
37060 if (dollar_inside) {
37061 if (data->function_descriptor)
37062 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
37063 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
37065 if (data->function_descriptor)
37067 fputs ("\t.globl .", data->file);
37068 RS6000_OUTPUT_BASENAME (data->file, buffer);
37069 putc ('\n', data->file);
37071 fputs ("\t.globl ", data->file);
37072 RS6000_OUTPUT_BASENAME (data->file, buffer);
37073 putc ('\n', data->file);
37075 #ifdef ASM_WEAKEN_DECL
37076 else if (DECL_WEAK (n->decl) && !data->function_descriptor)
37077 ASM_WEAKEN_DECL (data->file, n->decl, name, NULL);
37078 #endif
37080 else
37082 if (dollar_inside)
37084 if (data->function_descriptor)
37085 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
37086 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
37088 if (data->function_descriptor)
37090 fputs ("\t.lglobl .", data->file);
37091 RS6000_OUTPUT_BASENAME (data->file, buffer);
37092 putc ('\n', data->file);
37094 fputs ("\t.lglobl ", data->file);
37095 RS6000_OUTPUT_BASENAME (data->file, buffer);
37096 putc ('\n', data->file);
37098 if (data->function_descriptor)
37099 fputs (".", data->file);
37100 RS6000_OUTPUT_BASENAME (data->file, buffer);
37101 fputs (":\n", data->file);
37102 return false;
37106 #ifdef HAVE_GAS_HIDDEN
37107 /* Helper function to calculate visibility of a DECL
37108 and return the value as a const string. */
37110 static const char *
37111 rs6000_xcoff_visibility (tree decl)
37113 static const char * const visibility_types[] = {
37114 "", ",protected", ",hidden", ",internal"
37117 enum symbol_visibility vis = DECL_VISIBILITY (decl);
37119 if (TREE_CODE (decl) == FUNCTION_DECL
37120 && cgraph_node::get (decl)
37121 && cgraph_node::get (decl)->instrumentation_clone
37122 && cgraph_node::get (decl)->instrumented_version)
37123 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
37125 return visibility_types[vis];
37127 #endif
37130 /* This macro produces the initial definition of a function name.
37131 On the RS/6000, we need to place an extra '.' in the function name and
37132 output the function descriptor.
37133 Dollar signs are converted to underscores.
37135 The csect for the function will have already been created when
37136 text_section was selected. We do have to go back to that csect, however.
37138 The third and fourth parameters to the .function pseudo-op (16 and 044)
37139 are placeholders which no longer have any use.
37141 Because AIX assembler's .set command has unexpected semantics, we output
37142 all aliases as alternative labels in front of the definition. */
37144 void
37145 rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
37147 char *buffer = (char *) alloca (strlen (name) + 1);
37148 char *p;
37149 int dollar_inside = 0;
37150 struct declare_alias_data data = {file, false};
37152 strcpy (buffer, name);
37153 p = strchr (buffer, '$');
37154 while (p) {
37155 *p = '_';
37156 dollar_inside++;
37157 p = strchr (p + 1, '$');
37159 if (TREE_PUBLIC (decl))
37161 if (!RS6000_WEAK || !DECL_WEAK (decl))
37163 if (dollar_inside) {
37164 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
37165 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
37167 fputs ("\t.globl .", file);
37168 RS6000_OUTPUT_BASENAME (file, buffer);
37169 #ifdef HAVE_GAS_HIDDEN
37170 fputs (rs6000_xcoff_visibility (decl), file);
37171 #endif
37172 putc ('\n', file);
37175 else
37177 if (dollar_inside) {
37178 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
37179 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
37181 fputs ("\t.lglobl .", file);
37182 RS6000_OUTPUT_BASENAME (file, buffer);
37183 putc ('\n', file);
37185 fputs ("\t.csect ", file);
37186 RS6000_OUTPUT_BASENAME (file, buffer);
37187 fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
37188 RS6000_OUTPUT_BASENAME (file, buffer);
37189 fputs (":\n", file);
37190 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
37191 &data, true);
37192 fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
37193 RS6000_OUTPUT_BASENAME (file, buffer);
37194 fputs (", TOC[tc0], 0\n", file);
37195 in_section = NULL;
37196 switch_to_section (function_section (decl));
37197 putc ('.', file);
37198 RS6000_OUTPUT_BASENAME (file, buffer);
37199 fputs (":\n", file);
37200 data.function_descriptor = true;
37201 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
37202 &data, true);
37203 if (!DECL_IGNORED_P (decl))
37205 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
37206 xcoffout_declare_function (file, decl, buffer);
37207 else if (write_symbols == DWARF2_DEBUG)
37209 name = (*targetm.strip_name_encoding) (name);
37210 fprintf (file, "\t.function .%s,.%s,2,0\n", name, name);
37213 return;
37217 /* Output assembly language to globalize a symbol from a DECL,
37218 possibly with visibility. */
37220 void
37221 rs6000_xcoff_asm_globalize_decl_name (FILE *stream, tree decl)
37223 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
37224 fputs (GLOBAL_ASM_OP, stream);
37225 RS6000_OUTPUT_BASENAME (stream, name);
37226 #ifdef HAVE_GAS_HIDDEN
37227 fputs (rs6000_xcoff_visibility (decl), stream);
37228 #endif
37229 putc ('\n', stream);
37232 /* Output assembly language to define a symbol as COMMON from a DECL,
37233 possibly with visibility. */
37235 void
37236 rs6000_xcoff_asm_output_aligned_decl_common (FILE *stream,
37237 tree decl ATTRIBUTE_UNUSED,
37238 const char *name,
37239 unsigned HOST_WIDE_INT size,
37240 unsigned HOST_WIDE_INT align)
37242 unsigned HOST_WIDE_INT align2 = 2;
37244 if (align > 32)
37245 align2 = floor_log2 (align / BITS_PER_UNIT);
37246 else if (size > 4)
37247 align2 = 3;
37249 fputs (COMMON_ASM_OP, stream);
37250 RS6000_OUTPUT_BASENAME (stream, name);
37252 fprintf (stream,
37253 "," HOST_WIDE_INT_PRINT_UNSIGNED "," HOST_WIDE_INT_PRINT_UNSIGNED,
37254 size, align2);
37256 #ifdef HAVE_GAS_HIDDEN
37257 fputs (rs6000_xcoff_visibility (decl), stream);
37258 #endif
37259 putc ('\n', stream);
37262 /* This macro produces the initial definition of a object (variable) name.
37263 Because AIX assembler's .set command has unexpected semantics, we output
37264 all aliases as alternative labels in front of the definition. */
37266 void
37267 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
37269 struct declare_alias_data data = {file, false};
37270 RS6000_OUTPUT_BASENAME (file, name);
37271 fputs (":\n", file);
37272 symtab_node::get_create (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
37273 &data, true);
37276 /* Overide the default 'SYMBOL-.' syntax with AIX compatible 'SYMBOL-$'. */
37278 void
37279 rs6000_asm_output_dwarf_pcrel (FILE *file, int size, const char *label)
37281 fputs (integer_asm_op (size, FALSE), file);
37282 assemble_name (file, label);
37283 fputs ("-$", file);
37286 /* Output a symbol offset relative to the dbase for the current object.
37287 We use __gcc_unwind_dbase as an arbitrary base for dbase and assume
37288 signed offsets.
37290 __gcc_unwind_dbase is embedded in all executables/libraries through
37291 libgcc/config/rs6000/crtdbase.S. */
37293 void
37294 rs6000_asm_output_dwarf_datarel (FILE *file, int size, const char *label)
37296 fputs (integer_asm_op (size, FALSE), file);
37297 assemble_name (file, label);
37298 fputs("-__gcc_unwind_dbase", file);
37301 #ifdef HAVE_AS_TLS
37302 static void
37303 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
37305 rtx symbol;
37306 int flags;
37307 const char *symname;
37309 default_encode_section_info (decl, rtl, first);
37311 /* Careful not to prod global register variables. */
37312 if (!MEM_P (rtl))
37313 return;
37314 symbol = XEXP (rtl, 0);
37315 if (GET_CODE (symbol) != SYMBOL_REF)
37316 return;
37318 flags = SYMBOL_REF_FLAGS (symbol);
37320 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
37321 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
37323 SYMBOL_REF_FLAGS (symbol) = flags;
37325 /* Append mapping class to extern decls. */
37326 symname = XSTR (symbol, 0);
37327 if (decl /* sync condition with assemble_external () */
37328 && DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
37329 && ((TREE_CODE (decl) == VAR_DECL && !DECL_THREAD_LOCAL_P (decl))
37330 || TREE_CODE (decl) == FUNCTION_DECL)
37331 && symname[strlen (symname) - 1] != ']')
37333 char *newname = (char *) alloca (strlen (symname) + 5);
37334 strcpy (newname, symname);
37335 strcat (newname, (TREE_CODE (decl) == FUNCTION_DECL
37336 ? "[DS]" : "[UA]"));
37337 XSTR (symbol, 0) = ggc_strdup (newname);
37340 #endif /* HAVE_AS_TLS */
37341 #endif /* TARGET_XCOFF */
37343 void
37344 rs6000_asm_weaken_decl (FILE *stream, tree decl,
37345 const char *name, const char *val)
37347 fputs ("\t.weak\t", stream);
37348 RS6000_OUTPUT_BASENAME (stream, name);
37349 if (decl && TREE_CODE (decl) == FUNCTION_DECL
37350 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
37352 if (TARGET_XCOFF)
37353 fputs ("[DS]", stream);
37354 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
37355 if (TARGET_XCOFF)
37356 fputs (rs6000_xcoff_visibility (decl), stream);
37357 #endif
37358 fputs ("\n\t.weak\t.", stream);
37359 RS6000_OUTPUT_BASENAME (stream, name);
37361 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
37362 if (TARGET_XCOFF)
37363 fputs (rs6000_xcoff_visibility (decl), stream);
37364 #endif
37365 fputc ('\n', stream);
37366 if (val)
37368 #ifdef ASM_OUTPUT_DEF
37369 ASM_OUTPUT_DEF (stream, name, val);
37370 #endif
37371 if (decl && TREE_CODE (decl) == FUNCTION_DECL
37372 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
37374 fputs ("\t.set\t.", stream);
37375 RS6000_OUTPUT_BASENAME (stream, name);
37376 fputs (",.", stream);
37377 RS6000_OUTPUT_BASENAME (stream, val);
37378 fputc ('\n', stream);
37384 /* Return true if INSN should not be copied. */
37386 static bool
37387 rs6000_cannot_copy_insn_p (rtx_insn *insn)
37389 return recog_memoized (insn) >= 0
37390 && get_attr_cannot_copy (insn);
37393 /* Compute a (partial) cost for rtx X. Return true if the complete
37394 cost has been computed, and false if subexpressions should be
37395 scanned. In either case, *TOTAL contains the cost result. */
37397 static bool
37398 rs6000_rtx_costs (rtx x, machine_mode mode, int outer_code,
37399 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
37401 int code = GET_CODE (x);
37403 switch (code)
37405 /* On the RS/6000, if it is valid in the insn, it is free. */
37406 case CONST_INT:
37407 if (((outer_code == SET
37408 || outer_code == PLUS
37409 || outer_code == MINUS)
37410 && (satisfies_constraint_I (x)
37411 || satisfies_constraint_L (x)))
37412 || (outer_code == AND
37413 && (satisfies_constraint_K (x)
37414 || (mode == SImode
37415 ? satisfies_constraint_L (x)
37416 : satisfies_constraint_J (x))))
37417 || ((outer_code == IOR || outer_code == XOR)
37418 && (satisfies_constraint_K (x)
37419 || (mode == SImode
37420 ? satisfies_constraint_L (x)
37421 : satisfies_constraint_J (x))))
37422 || outer_code == ASHIFT
37423 || outer_code == ASHIFTRT
37424 || outer_code == LSHIFTRT
37425 || outer_code == ROTATE
37426 || outer_code == ROTATERT
37427 || outer_code == ZERO_EXTRACT
37428 || (outer_code == MULT
37429 && satisfies_constraint_I (x))
37430 || ((outer_code == DIV || outer_code == UDIV
37431 || outer_code == MOD || outer_code == UMOD)
37432 && exact_log2 (INTVAL (x)) >= 0)
37433 || (outer_code == COMPARE
37434 && (satisfies_constraint_I (x)
37435 || satisfies_constraint_K (x)))
37436 || ((outer_code == EQ || outer_code == NE)
37437 && (satisfies_constraint_I (x)
37438 || satisfies_constraint_K (x)
37439 || (mode == SImode
37440 ? satisfies_constraint_L (x)
37441 : satisfies_constraint_J (x))))
37442 || (outer_code == GTU
37443 && satisfies_constraint_I (x))
37444 || (outer_code == LTU
37445 && satisfies_constraint_P (x)))
37447 *total = 0;
37448 return true;
37450 else if ((outer_code == PLUS
37451 && reg_or_add_cint_operand (x, VOIDmode))
37452 || (outer_code == MINUS
37453 && reg_or_sub_cint_operand (x, VOIDmode))
37454 || ((outer_code == SET
37455 || outer_code == IOR
37456 || outer_code == XOR)
37457 && (INTVAL (x)
37458 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
37460 *total = COSTS_N_INSNS (1);
37461 return true;
37463 /* FALLTHRU */
37465 case CONST_DOUBLE:
37466 case CONST_WIDE_INT:
37467 case CONST:
37468 case HIGH:
37469 case SYMBOL_REF:
37470 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
37471 return true;
37473 case MEM:
37474 /* When optimizing for size, MEM should be slightly more expensive
37475 than generating address, e.g., (plus (reg) (const)).
37476 L1 cache latency is about two instructions. */
37477 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
37478 if (rs6000_slow_unaligned_access (mode, MEM_ALIGN (x)))
37479 *total += COSTS_N_INSNS (100);
37480 return true;
37482 case LABEL_REF:
37483 *total = 0;
37484 return true;
37486 case PLUS:
37487 case MINUS:
37488 if (FLOAT_MODE_P (mode))
37489 *total = rs6000_cost->fp;
37490 else
37491 *total = COSTS_N_INSNS (1);
37492 return false;
37494 case MULT:
37495 if (GET_CODE (XEXP (x, 1)) == CONST_INT
37496 && satisfies_constraint_I (XEXP (x, 1)))
37498 if (INTVAL (XEXP (x, 1)) >= -256
37499 && INTVAL (XEXP (x, 1)) <= 255)
37500 *total = rs6000_cost->mulsi_const9;
37501 else
37502 *total = rs6000_cost->mulsi_const;
37504 else if (mode == SFmode)
37505 *total = rs6000_cost->fp;
37506 else if (FLOAT_MODE_P (mode))
37507 *total = rs6000_cost->dmul;
37508 else if (mode == DImode)
37509 *total = rs6000_cost->muldi;
37510 else
37511 *total = rs6000_cost->mulsi;
37512 return false;
37514 case FMA:
37515 if (mode == SFmode)
37516 *total = rs6000_cost->fp;
37517 else
37518 *total = rs6000_cost->dmul;
37519 break;
37521 case DIV:
37522 case MOD:
37523 if (FLOAT_MODE_P (mode))
37525 *total = mode == DFmode ? rs6000_cost->ddiv
37526 : rs6000_cost->sdiv;
37527 return false;
37529 /* FALLTHRU */
37531 case UDIV:
37532 case UMOD:
37533 if (GET_CODE (XEXP (x, 1)) == CONST_INT
37534 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
37536 if (code == DIV || code == MOD)
37537 /* Shift, addze */
37538 *total = COSTS_N_INSNS (2);
37539 else
37540 /* Shift */
37541 *total = COSTS_N_INSNS (1);
37543 else
37545 if (GET_MODE (XEXP (x, 1)) == DImode)
37546 *total = rs6000_cost->divdi;
37547 else
37548 *total = rs6000_cost->divsi;
37550 /* Add in shift and subtract for MOD unless we have a mod instruction. */
37551 if (!TARGET_MODULO && (code == MOD || code == UMOD))
37552 *total += COSTS_N_INSNS (2);
37553 return false;
37555 case CTZ:
37556 *total = COSTS_N_INSNS (TARGET_CTZ ? 1 : 4);
37557 return false;
37559 case FFS:
37560 *total = COSTS_N_INSNS (4);
37561 return false;
37563 case POPCOUNT:
37564 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
37565 return false;
37567 case PARITY:
37568 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
37569 return false;
37571 case NOT:
37572 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
37573 *total = 0;
37574 else
37575 *total = COSTS_N_INSNS (1);
37576 return false;
37578 case AND:
37579 if (CONST_INT_P (XEXP (x, 1)))
37581 rtx left = XEXP (x, 0);
37582 rtx_code left_code = GET_CODE (left);
37584 /* rotate-and-mask: 1 insn. */
37585 if ((left_code == ROTATE
37586 || left_code == ASHIFT
37587 || left_code == LSHIFTRT)
37588 && rs6000_is_valid_shift_mask (XEXP (x, 1), left, mode))
37590 *total = rtx_cost (XEXP (left, 0), mode, left_code, 0, speed);
37591 if (!CONST_INT_P (XEXP (left, 1)))
37592 *total += rtx_cost (XEXP (left, 1), SImode, left_code, 1, speed);
37593 *total += COSTS_N_INSNS (1);
37594 return true;
37597 /* rotate-and-mask (no rotate), andi., andis.: 1 insn. */
37598 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
37599 if (rs6000_is_valid_and_mask (XEXP (x, 1), mode)
37600 || (val & 0xffff) == val
37601 || (val & 0xffff0000) == val
37602 || ((val & 0xffff) == 0 && mode == SImode))
37604 *total = rtx_cost (left, mode, AND, 0, speed);
37605 *total += COSTS_N_INSNS (1);
37606 return true;
37609 /* 2 insns. */
37610 if (rs6000_is_valid_2insn_and (XEXP (x, 1), mode))
37612 *total = rtx_cost (left, mode, AND, 0, speed);
37613 *total += COSTS_N_INSNS (2);
37614 return true;
37618 *total = COSTS_N_INSNS (1);
37619 return false;
37621 case IOR:
37622 /* FIXME */
37623 *total = COSTS_N_INSNS (1);
37624 return true;
37626 case CLZ:
37627 case XOR:
37628 case ZERO_EXTRACT:
37629 *total = COSTS_N_INSNS (1);
37630 return false;
37632 case ASHIFT:
37633 /* The EXTSWSLI instruction is a combined instruction. Don't count both
37634 the sign extend and shift separately within the insn. */
37635 if (TARGET_EXTSWSLI && mode == DImode
37636 && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
37637 && GET_MODE (XEXP (XEXP (x, 0), 0)) == SImode)
37639 *total = 0;
37640 return false;
37642 /* fall through */
37644 case ASHIFTRT:
37645 case LSHIFTRT:
37646 case ROTATE:
37647 case ROTATERT:
37648 /* Handle mul_highpart. */
37649 if (outer_code == TRUNCATE
37650 && GET_CODE (XEXP (x, 0)) == MULT)
37652 if (mode == DImode)
37653 *total = rs6000_cost->muldi;
37654 else
37655 *total = rs6000_cost->mulsi;
37656 return true;
37658 else if (outer_code == AND)
37659 *total = 0;
37660 else
37661 *total = COSTS_N_INSNS (1);
37662 return false;
37664 case SIGN_EXTEND:
37665 case ZERO_EXTEND:
37666 if (GET_CODE (XEXP (x, 0)) == MEM)
37667 *total = 0;
37668 else
37669 *total = COSTS_N_INSNS (1);
37670 return false;
37672 case COMPARE:
37673 case NEG:
37674 case ABS:
37675 if (!FLOAT_MODE_P (mode))
37677 *total = COSTS_N_INSNS (1);
37678 return false;
37680 /* FALLTHRU */
37682 case FLOAT:
37683 case UNSIGNED_FLOAT:
37684 case FIX:
37685 case UNSIGNED_FIX:
37686 case FLOAT_TRUNCATE:
37687 *total = rs6000_cost->fp;
37688 return false;
37690 case FLOAT_EXTEND:
37691 if (mode == DFmode)
37692 *total = rs6000_cost->sfdf_convert;
37693 else
37694 *total = rs6000_cost->fp;
37695 return false;
37697 case UNSPEC:
37698 switch (XINT (x, 1))
37700 case UNSPEC_FRSP:
37701 *total = rs6000_cost->fp;
37702 return true;
37704 default:
37705 break;
37707 break;
37709 case CALL:
37710 case IF_THEN_ELSE:
37711 if (!speed)
37713 *total = COSTS_N_INSNS (1);
37714 return true;
37716 else if (FLOAT_MODE_P (mode)
37717 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
37719 *total = rs6000_cost->fp;
37720 return false;
37722 break;
37724 case NE:
37725 case EQ:
37726 case GTU:
37727 case LTU:
37728 /* Carry bit requires mode == Pmode.
37729 NEG or PLUS already counted so only add one. */
37730 if (mode == Pmode
37731 && (outer_code == NEG || outer_code == PLUS))
37733 *total = COSTS_N_INSNS (1);
37734 return true;
37736 if (outer_code == SET)
37738 if (XEXP (x, 1) == const0_rtx)
37740 if (TARGET_ISEL && !TARGET_MFCRF)
37741 *total = COSTS_N_INSNS (8);
37742 else
37743 *total = COSTS_N_INSNS (2);
37744 return true;
37746 else
37748 *total = COSTS_N_INSNS (3);
37749 return false;
37752 /* FALLTHRU */
37754 case GT:
37755 case LT:
37756 case UNORDERED:
37757 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
37759 if (TARGET_ISEL && !TARGET_MFCRF)
37760 *total = COSTS_N_INSNS (8);
37761 else
37762 *total = COSTS_N_INSNS (2);
37763 return true;
37765 /* CC COMPARE. */
37766 if (outer_code == COMPARE)
37768 *total = 0;
37769 return true;
37771 break;
37773 default:
37774 break;
37777 return false;
37780 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
37782 static bool
37783 rs6000_debug_rtx_costs (rtx x, machine_mode mode, int outer_code,
37784 int opno, int *total, bool speed)
37786 bool ret = rs6000_rtx_costs (x, mode, outer_code, opno, total, speed);
37788 fprintf (stderr,
37789 "\nrs6000_rtx_costs, return = %s, mode = %s, outer_code = %s, "
37790 "opno = %d, total = %d, speed = %s, x:\n",
37791 ret ? "complete" : "scan inner",
37792 GET_MODE_NAME (mode),
37793 GET_RTX_NAME (outer_code),
37794 opno,
37795 *total,
37796 speed ? "true" : "false");
37798 debug_rtx (x);
37800 return ret;
37803 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
37805 static int
37806 rs6000_debug_address_cost (rtx x, machine_mode mode,
37807 addr_space_t as, bool speed)
37809 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
37811 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
37812 ret, speed ? "true" : "false");
37813 debug_rtx (x);
37815 return ret;
37819 /* A C expression returning the cost of moving data from a register of class
37820 CLASS1 to one of CLASS2. */
37822 static int
37823 rs6000_register_move_cost (machine_mode mode,
37824 reg_class_t from, reg_class_t to)
37826 int ret;
37828 if (TARGET_DEBUG_COST)
37829 dbg_cost_ctrl++;
37831 /* Moves from/to GENERAL_REGS. */
37832 if (reg_classes_intersect_p (to, GENERAL_REGS)
37833 || reg_classes_intersect_p (from, GENERAL_REGS))
37835 reg_class_t rclass = from;
37837 if (! reg_classes_intersect_p (to, GENERAL_REGS))
37838 rclass = to;
37840 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
37841 ret = (rs6000_memory_move_cost (mode, rclass, false)
37842 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
37844 /* It's more expensive to move CR_REGS than CR0_REGS because of the
37845 shift. */
37846 else if (rclass == CR_REGS)
37847 ret = 4;
37849 /* For those processors that have slow LR/CTR moves, make them more
37850 expensive than memory in order to bias spills to memory .*/
37851 else if ((rs6000_cpu == PROCESSOR_POWER6
37852 || rs6000_cpu == PROCESSOR_POWER7
37853 || rs6000_cpu == PROCESSOR_POWER8
37854 || rs6000_cpu == PROCESSOR_POWER9)
37855 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
37856 ret = 6 * hard_regno_nregs (0, mode);
37858 else
37859 /* A move will cost one instruction per GPR moved. */
37860 ret = 2 * hard_regno_nregs (0, mode);
37863 /* If we have VSX, we can easily move between FPR or Altivec registers. */
37864 else if (VECTOR_MEM_VSX_P (mode)
37865 && reg_classes_intersect_p (to, VSX_REGS)
37866 && reg_classes_intersect_p (from, VSX_REGS))
37867 ret = 2 * hard_regno_nregs (FIRST_FPR_REGNO, mode);
37869 /* Moving between two similar registers is just one instruction. */
37870 else if (reg_classes_intersect_p (to, from))
37871 ret = (FLOAT128_2REG_P (mode)) ? 4 : 2;
37873 /* Everything else has to go through GENERAL_REGS. */
37874 else
37875 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
37876 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
37878 if (TARGET_DEBUG_COST)
37880 if (dbg_cost_ctrl == 1)
37881 fprintf (stderr,
37882 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
37883 ret, GET_MODE_NAME (mode), reg_class_names[from],
37884 reg_class_names[to]);
37885 dbg_cost_ctrl--;
37888 return ret;
37891 /* A C expressions returning the cost of moving data of MODE from a register to
37892 or from memory. */
37894 static int
37895 rs6000_memory_move_cost (machine_mode mode, reg_class_t rclass,
37896 bool in ATTRIBUTE_UNUSED)
37898 int ret;
37900 if (TARGET_DEBUG_COST)
37901 dbg_cost_ctrl++;
37903 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
37904 ret = 4 * hard_regno_nregs (0, mode);
37905 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
37906 || reg_classes_intersect_p (rclass, VSX_REGS)))
37907 ret = 4 * hard_regno_nregs (32, mode);
37908 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
37909 ret = 4 * hard_regno_nregs (FIRST_ALTIVEC_REGNO, mode);
37910 else
37911 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
37913 if (TARGET_DEBUG_COST)
37915 if (dbg_cost_ctrl == 1)
37916 fprintf (stderr,
37917 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
37918 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
37919 dbg_cost_ctrl--;
37922 return ret;
37925 /* Returns a code for a target-specific builtin that implements
37926 reciprocal of the function, or NULL_TREE if not available. */
37928 static tree
37929 rs6000_builtin_reciprocal (tree fndecl)
37931 switch (DECL_FUNCTION_CODE (fndecl))
37933 case VSX_BUILTIN_XVSQRTDP:
37934 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
37935 return NULL_TREE;
37937 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
37939 case VSX_BUILTIN_XVSQRTSP:
37940 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
37941 return NULL_TREE;
37943 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
37945 default:
37946 return NULL_TREE;
37950 /* Load up a constant. If the mode is a vector mode, splat the value across
37951 all of the vector elements. */
37953 static rtx
37954 rs6000_load_constant_and_splat (machine_mode mode, REAL_VALUE_TYPE dconst)
37956 rtx reg;
37958 if (mode == SFmode || mode == DFmode)
37960 rtx d = const_double_from_real_value (dconst, mode);
37961 reg = force_reg (mode, d);
37963 else if (mode == V4SFmode)
37965 rtx d = const_double_from_real_value (dconst, SFmode);
37966 rtvec v = gen_rtvec (4, d, d, d, d);
37967 reg = gen_reg_rtx (mode);
37968 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
37970 else if (mode == V2DFmode)
37972 rtx d = const_double_from_real_value (dconst, DFmode);
37973 rtvec v = gen_rtvec (2, d, d);
37974 reg = gen_reg_rtx (mode);
37975 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
37977 else
37978 gcc_unreachable ();
37980 return reg;
37983 /* Generate an FMA instruction. */
37985 static void
37986 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
37988 machine_mode mode = GET_MODE (target);
37989 rtx dst;
37991 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
37992 gcc_assert (dst != NULL);
37994 if (dst != target)
37995 emit_move_insn (target, dst);
37998 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
38000 static void
38001 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
38003 machine_mode mode = GET_MODE (dst);
38004 rtx r;
38006 /* This is a tad more complicated, since the fnma_optab is for
38007 a different expression: fma(-m1, m2, a), which is the same
38008 thing except in the case of signed zeros.
38010 Fortunately we know that if FMA is supported that FNMSUB is
38011 also supported in the ISA. Just expand it directly. */
38013 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
38015 r = gen_rtx_NEG (mode, a);
38016 r = gen_rtx_FMA (mode, m1, m2, r);
38017 r = gen_rtx_NEG (mode, r);
38018 emit_insn (gen_rtx_SET (dst, r));
38021 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
38022 add a reg_note saying that this was a division. Support both scalar and
38023 vector divide. Assumes no trapping math and finite arguments. */
38025 void
38026 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
38028 machine_mode mode = GET_MODE (dst);
38029 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
38030 int i;
38032 /* Low precision estimates guarantee 5 bits of accuracy. High
38033 precision estimates guarantee 14 bits of accuracy. SFmode
38034 requires 23 bits of accuracy. DFmode requires 52 bits of
38035 accuracy. Each pass at least doubles the accuracy, leading
38036 to the following. */
38037 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
38038 if (mode == DFmode || mode == V2DFmode)
38039 passes++;
38041 enum insn_code code = optab_handler (smul_optab, mode);
38042 insn_gen_fn gen_mul = GEN_FCN (code);
38044 gcc_assert (code != CODE_FOR_nothing);
38046 one = rs6000_load_constant_and_splat (mode, dconst1);
38048 /* x0 = 1./d estimate */
38049 x0 = gen_reg_rtx (mode);
38050 emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
38051 UNSPEC_FRES)));
38053 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
38054 if (passes > 1) {
38056 /* e0 = 1. - d * x0 */
38057 e0 = gen_reg_rtx (mode);
38058 rs6000_emit_nmsub (e0, d, x0, one);
38060 /* x1 = x0 + e0 * x0 */
38061 x1 = gen_reg_rtx (mode);
38062 rs6000_emit_madd (x1, e0, x0, x0);
38064 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
38065 ++i, xprev = xnext, eprev = enext) {
38067 /* enext = eprev * eprev */
38068 enext = gen_reg_rtx (mode);
38069 emit_insn (gen_mul (enext, eprev, eprev));
38071 /* xnext = xprev + enext * xprev */
38072 xnext = gen_reg_rtx (mode);
38073 rs6000_emit_madd (xnext, enext, xprev, xprev);
38076 } else
38077 xprev = x0;
38079 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
38081 /* u = n * xprev */
38082 u = gen_reg_rtx (mode);
38083 emit_insn (gen_mul (u, n, xprev));
38085 /* v = n - (d * u) */
38086 v = gen_reg_rtx (mode);
38087 rs6000_emit_nmsub (v, d, u, n);
38089 /* dst = (v * xprev) + u */
38090 rs6000_emit_madd (dst, v, xprev, u);
38092 if (note_p)
38093 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
38096 /* Goldschmidt's Algorithm for single/double-precision floating point
38097 sqrt and rsqrt. Assumes no trapping math and finite arguments. */
38099 void
38100 rs6000_emit_swsqrt (rtx dst, rtx src, bool recip)
38102 machine_mode mode = GET_MODE (src);
38103 rtx e = gen_reg_rtx (mode);
38104 rtx g = gen_reg_rtx (mode);
38105 rtx h = gen_reg_rtx (mode);
38107 /* Low precision estimates guarantee 5 bits of accuracy. High
38108 precision estimates guarantee 14 bits of accuracy. SFmode
38109 requires 23 bits of accuracy. DFmode requires 52 bits of
38110 accuracy. Each pass at least doubles the accuracy, leading
38111 to the following. */
38112 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
38113 if (mode == DFmode || mode == V2DFmode)
38114 passes++;
38116 int i;
38117 rtx mhalf;
38118 enum insn_code code = optab_handler (smul_optab, mode);
38119 insn_gen_fn gen_mul = GEN_FCN (code);
38121 gcc_assert (code != CODE_FOR_nothing);
38123 mhalf = rs6000_load_constant_and_splat (mode, dconsthalf);
38125 /* e = rsqrt estimate */
38126 emit_insn (gen_rtx_SET (e, gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
38127 UNSPEC_RSQRT)));
38129 /* If (src == 0.0) filter infinity to prevent NaN for sqrt(0.0). */
38130 if (!recip)
38132 rtx zero = force_reg (mode, CONST0_RTX (mode));
38134 if (mode == SFmode)
38136 rtx target = emit_conditional_move (e, GT, src, zero, mode,
38137 e, zero, mode, 0);
38138 if (target != e)
38139 emit_move_insn (e, target);
38141 else
38143 rtx cond = gen_rtx_GT (VOIDmode, e, zero);
38144 rs6000_emit_vector_cond_expr (e, e, zero, cond, src, zero);
38148 /* g = sqrt estimate. */
38149 emit_insn (gen_mul (g, e, src));
38150 /* h = 1/(2*sqrt) estimate. */
38151 emit_insn (gen_mul (h, e, mhalf));
38153 if (recip)
38155 if (passes == 1)
38157 rtx t = gen_reg_rtx (mode);
38158 rs6000_emit_nmsub (t, g, h, mhalf);
38159 /* Apply correction directly to 1/rsqrt estimate. */
38160 rs6000_emit_madd (dst, e, t, e);
38162 else
38164 for (i = 0; i < passes; i++)
38166 rtx t1 = gen_reg_rtx (mode);
38167 rtx g1 = gen_reg_rtx (mode);
38168 rtx h1 = gen_reg_rtx (mode);
38170 rs6000_emit_nmsub (t1, g, h, mhalf);
38171 rs6000_emit_madd (g1, g, t1, g);
38172 rs6000_emit_madd (h1, h, t1, h);
38174 g = g1;
38175 h = h1;
38177 /* Multiply by 2 for 1/rsqrt. */
38178 emit_insn (gen_add3_insn (dst, h, h));
38181 else
38183 rtx t = gen_reg_rtx (mode);
38184 rs6000_emit_nmsub (t, g, h, mhalf);
38185 rs6000_emit_madd (dst, g, t, g);
38188 return;
38191 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
38192 (Power7) targets. DST is the target, and SRC is the argument operand. */
38194 void
38195 rs6000_emit_popcount (rtx dst, rtx src)
38197 machine_mode mode = GET_MODE (dst);
38198 rtx tmp1, tmp2;
38200 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
38201 if (TARGET_POPCNTD)
38203 if (mode == SImode)
38204 emit_insn (gen_popcntdsi2 (dst, src));
38205 else
38206 emit_insn (gen_popcntddi2 (dst, src));
38207 return;
38210 tmp1 = gen_reg_rtx (mode);
38212 if (mode == SImode)
38214 emit_insn (gen_popcntbsi2 (tmp1, src));
38215 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
38216 NULL_RTX, 0);
38217 tmp2 = force_reg (SImode, tmp2);
38218 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
38220 else
38222 emit_insn (gen_popcntbdi2 (tmp1, src));
38223 tmp2 = expand_mult (DImode, tmp1,
38224 GEN_INT ((HOST_WIDE_INT)
38225 0x01010101 << 32 | 0x01010101),
38226 NULL_RTX, 0);
38227 tmp2 = force_reg (DImode, tmp2);
38228 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
38233 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
38234 target, and SRC is the argument operand. */
38236 void
38237 rs6000_emit_parity (rtx dst, rtx src)
38239 machine_mode mode = GET_MODE (dst);
38240 rtx tmp;
38242 tmp = gen_reg_rtx (mode);
38244 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
38245 if (TARGET_CMPB)
38247 if (mode == SImode)
38249 emit_insn (gen_popcntbsi2 (tmp, src));
38250 emit_insn (gen_paritysi2_cmpb (dst, tmp));
38252 else
38254 emit_insn (gen_popcntbdi2 (tmp, src));
38255 emit_insn (gen_paritydi2_cmpb (dst, tmp));
38257 return;
38260 if (mode == SImode)
38262 /* Is mult+shift >= shift+xor+shift+xor? */
38263 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
38265 rtx tmp1, tmp2, tmp3, tmp4;
38267 tmp1 = gen_reg_rtx (SImode);
38268 emit_insn (gen_popcntbsi2 (tmp1, src));
38270 tmp2 = gen_reg_rtx (SImode);
38271 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
38272 tmp3 = gen_reg_rtx (SImode);
38273 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
38275 tmp4 = gen_reg_rtx (SImode);
38276 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
38277 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
38279 else
38280 rs6000_emit_popcount (tmp, src);
38281 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
38283 else
38285 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
38286 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
38288 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
38290 tmp1 = gen_reg_rtx (DImode);
38291 emit_insn (gen_popcntbdi2 (tmp1, src));
38293 tmp2 = gen_reg_rtx (DImode);
38294 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
38295 tmp3 = gen_reg_rtx (DImode);
38296 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
38298 tmp4 = gen_reg_rtx (DImode);
38299 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
38300 tmp5 = gen_reg_rtx (DImode);
38301 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
38303 tmp6 = gen_reg_rtx (DImode);
38304 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
38305 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
38307 else
38308 rs6000_emit_popcount (tmp, src);
38309 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
38313 /* Expand an Altivec constant permutation for little endian mode.
38314 There are two issues: First, the two input operands must be
38315 swapped so that together they form a double-wide array in LE
38316 order. Second, the vperm instruction has surprising behavior
38317 in LE mode: it interprets the elements of the source vectors
38318 in BE mode ("left to right") and interprets the elements of
38319 the destination vector in LE mode ("right to left"). To
38320 correct for this, we must subtract each element of the permute
38321 control vector from 31.
38323 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
38324 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
38325 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
38326 serve as the permute control vector. Then, in BE mode,
38328 vperm 9,10,11,12
38330 places the desired result in vr9. However, in LE mode the
38331 vector contents will be
38333 vr10 = 00000003 00000002 00000001 00000000
38334 vr11 = 00000007 00000006 00000005 00000004
38336 The result of the vperm using the same permute control vector is
38338 vr9 = 05000000 07000000 01000000 03000000
38340 That is, the leftmost 4 bytes of vr10 are interpreted as the
38341 source for the rightmost 4 bytes of vr9, and so on.
38343 If we change the permute control vector to
38345 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
38347 and issue
38349 vperm 9,11,10,12
38351 we get the desired
38353 vr9 = 00000006 00000004 00000002 00000000. */
38355 void
38356 altivec_expand_vec_perm_const_le (rtx operands[4])
38358 unsigned int i;
38359 rtx perm[16];
38360 rtx constv, unspec;
38361 rtx target = operands[0];
38362 rtx op0 = operands[1];
38363 rtx op1 = operands[2];
38364 rtx sel = operands[3];
38366 /* Unpack and adjust the constant selector. */
38367 for (i = 0; i < 16; ++i)
38369 rtx e = XVECEXP (sel, 0, i);
38370 unsigned int elt = 31 - (INTVAL (e) & 31);
38371 perm[i] = GEN_INT (elt);
38374 /* Expand to a permute, swapping the inputs and using the
38375 adjusted selector. */
38376 if (!REG_P (op0))
38377 op0 = force_reg (V16QImode, op0);
38378 if (!REG_P (op1))
38379 op1 = force_reg (V16QImode, op1);
38381 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
38382 constv = force_reg (V16QImode, constv);
38383 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
38384 UNSPEC_VPERM);
38385 if (!REG_P (target))
38387 rtx tmp = gen_reg_rtx (V16QImode);
38388 emit_move_insn (tmp, unspec);
38389 unspec = tmp;
38392 emit_move_insn (target, unspec);
38395 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
38396 permute control vector. But here it's not a constant, so we must
38397 generate a vector NAND or NOR to do the adjustment. */
38399 void
38400 altivec_expand_vec_perm_le (rtx operands[4])
38402 rtx notx, iorx, unspec;
38403 rtx target = operands[0];
38404 rtx op0 = operands[1];
38405 rtx op1 = operands[2];
38406 rtx sel = operands[3];
38407 rtx tmp = target;
38408 rtx norreg = gen_reg_rtx (V16QImode);
38409 machine_mode mode = GET_MODE (target);
38411 /* Get everything in regs so the pattern matches. */
38412 if (!REG_P (op0))
38413 op0 = force_reg (mode, op0);
38414 if (!REG_P (op1))
38415 op1 = force_reg (mode, op1);
38416 if (!REG_P (sel))
38417 sel = force_reg (V16QImode, sel);
38418 if (!REG_P (target))
38419 tmp = gen_reg_rtx (mode);
38421 if (TARGET_P9_VECTOR)
38423 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op0, op1, sel),
38424 UNSPEC_VPERMR);
38426 else
38428 /* Invert the selector with a VNAND if available, else a VNOR.
38429 The VNAND is preferred for future fusion opportunities. */
38430 notx = gen_rtx_NOT (V16QImode, sel);
38431 iorx = (TARGET_P8_VECTOR
38432 ? gen_rtx_IOR (V16QImode, notx, notx)
38433 : gen_rtx_AND (V16QImode, notx, notx));
38434 emit_insn (gen_rtx_SET (norreg, iorx));
38436 /* Permute with operands reversed and adjusted selector. */
38437 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
38438 UNSPEC_VPERM);
38441 /* Copy into target, possibly by way of a register. */
38442 if (!REG_P (target))
38444 emit_move_insn (tmp, unspec);
38445 unspec = tmp;
38448 emit_move_insn (target, unspec);
38451 /* Expand an Altivec constant permutation. Return true if we match
38452 an efficient implementation; false to fall back to VPERM. */
38454 bool
38455 altivec_expand_vec_perm_const (rtx operands[4])
38457 struct altivec_perm_insn {
38458 HOST_WIDE_INT mask;
38459 enum insn_code impl;
38460 unsigned char perm[16];
38462 static const struct altivec_perm_insn patterns[] = {
38463 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
38464 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
38465 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
38466 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
38467 { OPTION_MASK_ALTIVEC,
38468 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
38469 : CODE_FOR_altivec_vmrglb_direct),
38470 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
38471 { OPTION_MASK_ALTIVEC,
38472 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
38473 : CODE_FOR_altivec_vmrglh_direct),
38474 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
38475 { OPTION_MASK_ALTIVEC,
38476 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
38477 : CODE_FOR_altivec_vmrglw_direct),
38478 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
38479 { OPTION_MASK_ALTIVEC,
38480 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
38481 : CODE_FOR_altivec_vmrghb_direct),
38482 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
38483 { OPTION_MASK_ALTIVEC,
38484 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
38485 : CODE_FOR_altivec_vmrghh_direct),
38486 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
38487 { OPTION_MASK_ALTIVEC,
38488 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
38489 : CODE_FOR_altivec_vmrghw_direct),
38490 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
38491 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
38492 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
38493 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
38494 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
38497 unsigned int i, j, elt, which;
38498 unsigned char perm[16];
38499 rtx target, op0, op1, sel, x;
38500 bool one_vec;
38502 target = operands[0];
38503 op0 = operands[1];
38504 op1 = operands[2];
38505 sel = operands[3];
38507 /* Unpack the constant selector. */
38508 for (i = which = 0; i < 16; ++i)
38510 rtx e = XVECEXP (sel, 0, i);
38511 elt = INTVAL (e) & 31;
38512 which |= (elt < 16 ? 1 : 2);
38513 perm[i] = elt;
38516 /* Simplify the constant selector based on operands. */
38517 switch (which)
38519 default:
38520 gcc_unreachable ();
38522 case 3:
38523 one_vec = false;
38524 if (!rtx_equal_p (op0, op1))
38525 break;
38526 /* FALLTHRU */
38528 case 2:
38529 for (i = 0; i < 16; ++i)
38530 perm[i] &= 15;
38531 op0 = op1;
38532 one_vec = true;
38533 break;
38535 case 1:
38536 op1 = op0;
38537 one_vec = true;
38538 break;
38541 /* Look for splat patterns. */
38542 if (one_vec)
38544 elt = perm[0];
38546 for (i = 0; i < 16; ++i)
38547 if (perm[i] != elt)
38548 break;
38549 if (i == 16)
38551 if (!BYTES_BIG_ENDIAN)
38552 elt = 15 - elt;
38553 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
38554 return true;
38557 if (elt % 2 == 0)
38559 for (i = 0; i < 16; i += 2)
38560 if (perm[i] != elt || perm[i + 1] != elt + 1)
38561 break;
38562 if (i == 16)
38564 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
38565 x = gen_reg_rtx (V8HImode);
38566 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
38567 GEN_INT (field)));
38568 emit_move_insn (target, gen_lowpart (V16QImode, x));
38569 return true;
38573 if (elt % 4 == 0)
38575 for (i = 0; i < 16; i += 4)
38576 if (perm[i] != elt
38577 || perm[i + 1] != elt + 1
38578 || perm[i + 2] != elt + 2
38579 || perm[i + 3] != elt + 3)
38580 break;
38581 if (i == 16)
38583 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
38584 x = gen_reg_rtx (V4SImode);
38585 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
38586 GEN_INT (field)));
38587 emit_move_insn (target, gen_lowpart (V16QImode, x));
38588 return true;
38593 /* Look for merge and pack patterns. */
38594 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
38596 bool swapped;
38598 if ((patterns[j].mask & rs6000_isa_flags) == 0)
38599 continue;
38601 elt = patterns[j].perm[0];
38602 if (perm[0] == elt)
38603 swapped = false;
38604 else if (perm[0] == elt + 16)
38605 swapped = true;
38606 else
38607 continue;
38608 for (i = 1; i < 16; ++i)
38610 elt = patterns[j].perm[i];
38611 if (swapped)
38612 elt = (elt >= 16 ? elt - 16 : elt + 16);
38613 else if (one_vec && elt >= 16)
38614 elt -= 16;
38615 if (perm[i] != elt)
38616 break;
38618 if (i == 16)
38620 enum insn_code icode = patterns[j].impl;
38621 machine_mode omode = insn_data[icode].operand[0].mode;
38622 machine_mode imode = insn_data[icode].operand[1].mode;
38624 /* For little-endian, don't use vpkuwum and vpkuhum if the
38625 underlying vector type is not V4SI and V8HI, respectively.
38626 For example, using vpkuwum with a V8HI picks up the even
38627 halfwords (BE numbering) when the even halfwords (LE
38628 numbering) are what we need. */
38629 if (!BYTES_BIG_ENDIAN
38630 && icode == CODE_FOR_altivec_vpkuwum_direct
38631 && ((GET_CODE (op0) == REG
38632 && GET_MODE (op0) != V4SImode)
38633 || (GET_CODE (op0) == SUBREG
38634 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
38635 continue;
38636 if (!BYTES_BIG_ENDIAN
38637 && icode == CODE_FOR_altivec_vpkuhum_direct
38638 && ((GET_CODE (op0) == REG
38639 && GET_MODE (op0) != V8HImode)
38640 || (GET_CODE (op0) == SUBREG
38641 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
38642 continue;
38644 /* For little-endian, the two input operands must be swapped
38645 (or swapped back) to ensure proper right-to-left numbering
38646 from 0 to 2N-1. */
38647 if (swapped ^ !BYTES_BIG_ENDIAN)
38648 std::swap (op0, op1);
38649 if (imode != V16QImode)
38651 op0 = gen_lowpart (imode, op0);
38652 op1 = gen_lowpart (imode, op1);
38654 if (omode == V16QImode)
38655 x = target;
38656 else
38657 x = gen_reg_rtx (omode);
38658 emit_insn (GEN_FCN (icode) (x, op0, op1));
38659 if (omode != V16QImode)
38660 emit_move_insn (target, gen_lowpart (V16QImode, x));
38661 return true;
38665 if (!BYTES_BIG_ENDIAN)
38667 altivec_expand_vec_perm_const_le (operands);
38668 return true;
38671 return false;
38674 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
38675 Return true if we match an efficient implementation. */
38677 static bool
38678 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
38679 unsigned char perm0, unsigned char perm1)
38681 rtx x;
38683 /* If both selectors come from the same operand, fold to single op. */
38684 if ((perm0 & 2) == (perm1 & 2))
38686 if (perm0 & 2)
38687 op0 = op1;
38688 else
38689 op1 = op0;
38691 /* If both operands are equal, fold to simpler permutation. */
38692 if (rtx_equal_p (op0, op1))
38694 perm0 = perm0 & 1;
38695 perm1 = (perm1 & 1) + 2;
38697 /* If the first selector comes from the second operand, swap. */
38698 else if (perm0 & 2)
38700 if (perm1 & 2)
38701 return false;
38702 perm0 -= 2;
38703 perm1 += 2;
38704 std::swap (op0, op1);
38706 /* If the second selector does not come from the second operand, fail. */
38707 else if ((perm1 & 2) == 0)
38708 return false;
38710 /* Success! */
38711 if (target != NULL)
38713 machine_mode vmode, dmode;
38714 rtvec v;
38716 vmode = GET_MODE (target);
38717 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
38718 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4).require ();
38719 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
38720 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
38721 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
38722 emit_insn (gen_rtx_SET (target, x));
38724 return true;
38727 bool
38728 rs6000_expand_vec_perm_const (rtx operands[4])
38730 rtx target, op0, op1, sel;
38731 unsigned char perm0, perm1;
38733 target = operands[0];
38734 op0 = operands[1];
38735 op1 = operands[2];
38736 sel = operands[3];
38738 /* Unpack the constant selector. */
38739 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
38740 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
38742 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
38745 /* Test whether a constant permutation is supported. */
38747 static bool
38748 rs6000_vectorize_vec_perm_const_ok (machine_mode vmode, vec_perm_indices sel)
38750 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
38751 if (TARGET_ALTIVEC)
38752 return true;
38754 /* Check for ps_merge* or evmerge* insns. */
38755 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
38756 || (TARGET_SPE && vmode == V2SImode))
38758 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
38759 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
38760 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
38763 return false;
38766 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
38768 static void
38769 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
38770 machine_mode vmode, unsigned nelt, rtx perm[])
38772 machine_mode imode;
38773 rtx x;
38775 imode = vmode;
38776 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
38777 imode = mode_for_int_vector (vmode).require ();
38779 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
38780 x = expand_vec_perm (vmode, op0, op1, x, target);
38781 if (x != target)
38782 emit_move_insn (target, x);
38785 /* Expand an extract even operation. */
38787 void
38788 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
38790 machine_mode vmode = GET_MODE (target);
38791 unsigned i, nelt = GET_MODE_NUNITS (vmode);
38792 rtx perm[16];
38794 for (i = 0; i < nelt; i++)
38795 perm[i] = GEN_INT (i * 2);
38797 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
38800 /* Expand a vector interleave operation. */
38802 void
38803 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
38805 machine_mode vmode = GET_MODE (target);
38806 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
38807 rtx perm[16];
38809 high = (highp ? 0 : nelt / 2);
38810 for (i = 0; i < nelt / 2; i++)
38812 perm[i * 2] = GEN_INT (i + high);
38813 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
38816 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
38819 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT. */
38820 void
38821 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
38823 HOST_WIDE_INT hwi_scale (scale);
38824 REAL_VALUE_TYPE r_pow;
38825 rtvec v = rtvec_alloc (2);
38826 rtx elt;
38827 rtx scale_vec = gen_reg_rtx (V2DFmode);
38828 (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
38829 elt = const_double_from_real_value (r_pow, DFmode);
38830 RTVEC_ELT (v, 0) = elt;
38831 RTVEC_ELT (v, 1) = elt;
38832 rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
38833 emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
38836 /* Return an RTX representing where to find the function value of a
38837 function returning MODE. */
38838 static rtx
38839 rs6000_complex_function_value (machine_mode mode)
38841 unsigned int regno;
38842 rtx r1, r2;
38843 machine_mode inner = GET_MODE_INNER (mode);
38844 unsigned int inner_bytes = GET_MODE_UNIT_SIZE (mode);
38846 if (TARGET_FLOAT128_TYPE
38847 && (mode == KCmode
38848 || (mode == TCmode && TARGET_IEEEQUAD)))
38849 regno = ALTIVEC_ARG_RETURN;
38851 else if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
38852 regno = FP_ARG_RETURN;
38854 else
38856 regno = GP_ARG_RETURN;
38858 /* 32-bit is OK since it'll go in r3/r4. */
38859 if (TARGET_32BIT && inner_bytes >= 4)
38860 return gen_rtx_REG (mode, regno);
38863 if (inner_bytes >= 8)
38864 return gen_rtx_REG (mode, regno);
38866 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
38867 const0_rtx);
38868 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
38869 GEN_INT (inner_bytes));
38870 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
38873 /* Return an rtx describing a return value of MODE as a PARALLEL
38874 in N_ELTS registers, each of mode ELT_MODE, starting at REGNO,
38875 stride REG_STRIDE. */
38877 static rtx
38878 rs6000_parallel_return (machine_mode mode,
38879 int n_elts, machine_mode elt_mode,
38880 unsigned int regno, unsigned int reg_stride)
38882 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
38884 int i;
38885 for (i = 0; i < n_elts; i++)
38887 rtx r = gen_rtx_REG (elt_mode, regno);
38888 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
38889 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
38890 regno += reg_stride;
38893 return par;
38896 /* Target hook for TARGET_FUNCTION_VALUE.
38898 On the SPE, both FPs and vectors are returned in r3.
38900 On RS/6000 an integer value is in r3 and a floating-point value is in
38901 fp1, unless -msoft-float. */
38903 static rtx
38904 rs6000_function_value (const_tree valtype,
38905 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
38906 bool outgoing ATTRIBUTE_UNUSED)
38908 machine_mode mode;
38909 unsigned int regno;
38910 machine_mode elt_mode;
38911 int n_elts;
38913 /* Special handling for structs in darwin64. */
38914 if (TARGET_MACHO
38915 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
38917 CUMULATIVE_ARGS valcum;
38918 rtx valret;
38920 valcum.words = 0;
38921 valcum.fregno = FP_ARG_MIN_REG;
38922 valcum.vregno = ALTIVEC_ARG_MIN_REG;
38923 /* Do a trial code generation as if this were going to be passed as
38924 an argument; if any part goes in memory, we return NULL. */
38925 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
38926 if (valret)
38927 return valret;
38928 /* Otherwise fall through to standard ABI rules. */
38931 mode = TYPE_MODE (valtype);
38933 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
38934 if (rs6000_discover_homogeneous_aggregate (mode, valtype, &elt_mode, &n_elts))
38936 int first_reg, n_regs;
38938 if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (elt_mode))
38940 /* _Decimal128 must use even/odd register pairs. */
38941 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
38942 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
38944 else
38946 first_reg = ALTIVEC_ARG_RETURN;
38947 n_regs = 1;
38950 return rs6000_parallel_return (mode, n_elts, elt_mode, first_reg, n_regs);
38953 /* Some return value types need be split in -mpowerpc64, 32bit ABI. */
38954 if (TARGET_32BIT && TARGET_POWERPC64)
38955 switch (mode)
38957 default:
38958 break;
38959 case E_DImode:
38960 case E_SCmode:
38961 case E_DCmode:
38962 case E_TCmode:
38963 int count = GET_MODE_SIZE (mode) / 4;
38964 return rs6000_parallel_return (mode, count, SImode, GP_ARG_RETURN, 1);
38967 if ((INTEGRAL_TYPE_P (valtype)
38968 && GET_MODE_BITSIZE (mode) < (TARGET_32BIT ? 32 : 64))
38969 || POINTER_TYPE_P (valtype))
38970 mode = TARGET_32BIT ? SImode : DImode;
38972 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
38973 /* _Decimal128 must use an even/odd register pair. */
38974 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
38975 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
38976 && !FLOAT128_VECTOR_P (mode)
38977 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
38978 regno = FP_ARG_RETURN;
38979 else if (TREE_CODE (valtype) == COMPLEX_TYPE
38980 && targetm.calls.split_complex_arg)
38981 return rs6000_complex_function_value (mode);
38982 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
38983 return register is used in both cases, and we won't see V2DImode/V2DFmode
38984 for pure altivec, combine the two cases. */
38985 else if ((TREE_CODE (valtype) == VECTOR_TYPE || FLOAT128_VECTOR_P (mode))
38986 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
38987 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
38988 regno = ALTIVEC_ARG_RETURN;
38989 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
38990 && (mode == DFmode || mode == DCmode
38991 || FLOAT128_IBM_P (mode) || mode == TCmode))
38992 return spe_build_register_parallel (mode, GP_ARG_RETURN);
38993 else
38994 regno = GP_ARG_RETURN;
38996 return gen_rtx_REG (mode, regno);
38999 /* Define how to find the value returned by a library function
39000 assuming the value has mode MODE. */
39002 rs6000_libcall_value (machine_mode mode)
39004 unsigned int regno;
39006 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
39007 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
39008 return rs6000_parallel_return (mode, 2, SImode, GP_ARG_RETURN, 1);
39010 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
39011 /* _Decimal128 must use an even/odd register pair. */
39012 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
39013 else if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode)
39014 && TARGET_HARD_FLOAT && TARGET_FPRS
39015 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
39016 regno = FP_ARG_RETURN;
39017 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
39018 return register is used in both cases, and we won't see V2DImode/V2DFmode
39019 for pure altivec, combine the two cases. */
39020 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
39021 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
39022 regno = ALTIVEC_ARG_RETURN;
39023 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
39024 return rs6000_complex_function_value (mode);
39025 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
39026 && (mode == DFmode || mode == DCmode
39027 || FLOAT128_IBM_P (mode) || mode == TCmode))
39028 return spe_build_register_parallel (mode, GP_ARG_RETURN);
39029 else
39030 regno = GP_ARG_RETURN;
39032 return gen_rtx_REG (mode, regno);
39036 /* Return true if we use LRA instead of reload pass. */
39037 static bool
39038 rs6000_lra_p (void)
39040 return TARGET_LRA;
39043 /* Compute register pressure classes. We implement the target hook to avoid
39044 IRA picking something like NON_SPECIAL_REGS as a pressure class, which can
39045 lead to incorrect estimates of number of available registers and therefor
39046 increased register pressure/spill. */
39047 static int
39048 rs6000_compute_pressure_classes (enum reg_class *pressure_classes)
39050 int n;
39052 n = 0;
39053 pressure_classes[n++] = GENERAL_REGS;
39054 if (TARGET_VSX)
39055 pressure_classes[n++] = VSX_REGS;
39056 else
39058 if (TARGET_ALTIVEC)
39059 pressure_classes[n++] = ALTIVEC_REGS;
39060 if (TARGET_HARD_FLOAT && TARGET_FPRS)
39061 pressure_classes[n++] = FLOAT_REGS;
39063 pressure_classes[n++] = CR_REGS;
39064 pressure_classes[n++] = SPECIAL_REGS;
39066 return n;
39069 /* Given FROM and TO register numbers, say whether this elimination is allowed.
39070 Frame pointer elimination is automatically handled.
39072 For the RS/6000, if frame pointer elimination is being done, we would like
39073 to convert ap into fp, not sp.
39075 We need r30 if -mminimal-toc was specified, and there are constant pool
39076 references. */
39078 static bool
39079 rs6000_can_eliminate (const int from, const int to)
39081 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
39082 ? ! frame_pointer_needed
39083 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
39084 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC
39085 || constant_pool_empty_p ()
39086 : true);
39089 /* Define the offset between two registers, FROM to be eliminated and its
39090 replacement TO, at the start of a routine. */
39091 HOST_WIDE_INT
39092 rs6000_initial_elimination_offset (int from, int to)
39094 rs6000_stack_t *info = rs6000_stack_info ();
39095 HOST_WIDE_INT offset;
39097 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
39098 offset = info->push_p ? 0 : -info->total_size;
39099 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
39101 offset = info->push_p ? 0 : -info->total_size;
39102 if (FRAME_GROWS_DOWNWARD)
39103 offset += info->fixed_size + info->vars_size + info->parm_size;
39105 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
39106 offset = FRAME_GROWS_DOWNWARD
39107 ? info->fixed_size + info->vars_size + info->parm_size
39108 : 0;
39109 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
39110 offset = info->total_size;
39111 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
39112 offset = info->push_p ? info->total_size : 0;
39113 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
39114 offset = 0;
39115 else
39116 gcc_unreachable ();
39118 return offset;
39121 static rtx
39122 rs6000_dwarf_register_span (rtx reg)
39124 rtx parts[8];
39125 int i, words;
39126 unsigned regno = REGNO (reg);
39127 machine_mode mode = GET_MODE (reg);
39129 if (TARGET_SPE
39130 && regno < 32
39131 && (SPE_VECTOR_MODE (GET_MODE (reg))
39132 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
39133 && mode != SFmode && mode != SDmode && mode != SCmode)))
39135 else
39136 return NULL_RTX;
39138 regno = REGNO (reg);
39140 /* The duality of the SPE register size wreaks all kinds of havoc.
39141 This is a way of distinguishing r0 in 32-bits from r0 in
39142 64-bits. */
39143 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
39144 gcc_assert (words <= 4);
39145 for (i = 0; i < words; i++, regno++)
39147 if (BYTES_BIG_ENDIAN)
39149 parts[2 * i] = gen_rtx_REG (SImode, regno + FIRST_SPE_HIGH_REGNO);
39150 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
39152 else
39154 parts[2 * i] = gen_rtx_REG (SImode, regno);
39155 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + FIRST_SPE_HIGH_REGNO);
39159 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
39162 /* Fill in sizes for SPE register high parts in table used by unwinder. */
39164 static void
39165 rs6000_init_dwarf_reg_sizes_extra (tree address)
39167 if (TARGET_SPE)
39169 int i;
39170 machine_mode mode = TYPE_MODE (char_type_node);
39171 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
39172 rtx mem = gen_rtx_MEM (BLKmode, addr);
39173 rtx value = gen_int_mode (4, mode);
39175 for (i = FIRST_SPE_HIGH_REGNO; i < LAST_SPE_HIGH_REGNO+1; i++)
39177 int column = DWARF_REG_TO_UNWIND_COLUMN
39178 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
39179 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
39181 emit_move_insn (adjust_address (mem, mode, offset), value);
39185 if (TARGET_MACHO && ! TARGET_ALTIVEC)
39187 int i;
39188 machine_mode mode = TYPE_MODE (char_type_node);
39189 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
39190 rtx mem = gen_rtx_MEM (BLKmode, addr);
39191 rtx value = gen_int_mode (16, mode);
39193 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
39194 The unwinder still needs to know the size of Altivec registers. */
39196 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
39198 int column = DWARF_REG_TO_UNWIND_COLUMN
39199 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
39200 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
39202 emit_move_insn (adjust_address (mem, mode, offset), value);
39207 /* Map internal gcc register numbers to debug format register numbers.
39208 FORMAT specifies the type of debug register number to use:
39209 0 -- debug information, except for frame-related sections
39210 1 -- DWARF .debug_frame section
39211 2 -- DWARF .eh_frame section */
39213 unsigned int
39214 rs6000_dbx_register_number (unsigned int regno, unsigned int format)
39216 /* We never use the GCC internal number for SPE high registers.
39217 Those are mapped to the 1200..1231 range for all debug formats. */
39218 if (SPE_HIGH_REGNO_P (regno))
39219 return regno - FIRST_SPE_HIGH_REGNO + 1200;
39221 /* Except for the above, we use the internal number for non-DWARF
39222 debug information, and also for .eh_frame. */
39223 if ((format == 0 && write_symbols != DWARF2_DEBUG) || format == 2)
39224 return regno;
39226 /* On some platforms, we use the standard DWARF register
39227 numbering for .debug_info and .debug_frame. */
39228 #ifdef RS6000_USE_DWARF_NUMBERING
39229 if (regno <= 63)
39230 return regno;
39231 if (regno == LR_REGNO)
39232 return 108;
39233 if (regno == CTR_REGNO)
39234 return 109;
39235 /* Special handling for CR for .debug_frame: rs6000_emit_prologue has
39236 translated any combination of CR2, CR3, CR4 saves to a save of CR2.
39237 The actual code emitted saves the whole of CR, so we map CR2_REGNO
39238 to the DWARF reg for CR. */
39239 if (format == 1 && regno == CR2_REGNO)
39240 return 64;
39241 if (CR_REGNO_P (regno))
39242 return regno - CR0_REGNO + 86;
39243 if (regno == CA_REGNO)
39244 return 101; /* XER */
39245 if (ALTIVEC_REGNO_P (regno))
39246 return regno - FIRST_ALTIVEC_REGNO + 1124;
39247 if (regno == VRSAVE_REGNO)
39248 return 356;
39249 if (regno == VSCR_REGNO)
39250 return 67;
39251 if (regno == SPE_ACC_REGNO)
39252 return 99;
39253 if (regno == SPEFSCR_REGNO)
39254 return 612;
39255 #endif
39256 return regno;
39259 /* target hook eh_return_filter_mode */
39260 static scalar_int_mode
39261 rs6000_eh_return_filter_mode (void)
39263 return TARGET_32BIT ? SImode : word_mode;
39266 /* Target hook for scalar_mode_supported_p. */
39267 static bool
39268 rs6000_scalar_mode_supported_p (scalar_mode mode)
39270 /* -m32 does not support TImode. This is the default, from
39271 default_scalar_mode_supported_p. For -m32 -mpowerpc64 we want the
39272 same ABI as for -m32. But default_scalar_mode_supported_p allows
39273 integer modes of precision 2 * BITS_PER_WORD, which matches TImode
39274 for -mpowerpc64. */
39275 if (TARGET_32BIT && mode == TImode)
39276 return false;
39278 if (DECIMAL_FLOAT_MODE_P (mode))
39279 return default_decimal_float_supported_p ();
39280 else if (TARGET_FLOAT128_TYPE && (mode == KFmode || mode == IFmode))
39281 return true;
39282 else
39283 return default_scalar_mode_supported_p (mode);
39286 /* Target hook for vector_mode_supported_p. */
39287 static bool
39288 rs6000_vector_mode_supported_p (machine_mode mode)
39291 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
39292 return true;
39294 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
39295 return true;
39297 /* There is no vector form for IEEE 128-bit. If we return true for IEEE
39298 128-bit, the compiler might try to widen IEEE 128-bit to IBM
39299 double-double. */
39300 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode) && !FLOAT128_IEEE_P (mode))
39301 return true;
39303 else
39304 return false;
39307 /* Target hook for floatn_mode. */
39308 static opt_scalar_float_mode
39309 rs6000_floatn_mode (int n, bool extended)
39311 if (extended)
39313 switch (n)
39315 case 32:
39316 return DFmode;
39318 case 64:
39319 if (TARGET_FLOAT128_KEYWORD)
39320 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
39321 else
39322 return opt_scalar_float_mode ();
39324 case 128:
39325 return opt_scalar_float_mode ();
39327 default:
39328 /* Those are the only valid _FloatNx types. */
39329 gcc_unreachable ();
39332 else
39334 switch (n)
39336 case 32:
39337 return SFmode;
39339 case 64:
39340 return DFmode;
39342 case 128:
39343 if (TARGET_FLOAT128_KEYWORD)
39344 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
39345 else
39346 return opt_scalar_float_mode ();
39348 default:
39349 return opt_scalar_float_mode ();
39355 /* Target hook for c_mode_for_suffix. */
39356 static machine_mode
39357 rs6000_c_mode_for_suffix (char suffix)
39359 if (TARGET_FLOAT128_TYPE)
39361 if (suffix == 'q' || suffix == 'Q')
39362 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
39364 /* At the moment, we are not defining a suffix for IBM extended double.
39365 If/when the default for -mabi=ieeelongdouble is changed, and we want
39366 to support __ibm128 constants in legacy library code, we may need to
39367 re-evalaute this decision. Currently, c-lex.c only supports 'w' and
39368 'q' as machine dependent suffixes. The x86_64 port uses 'w' for
39369 __float80 constants. */
39372 return VOIDmode;
39375 /* Target hook for invalid_arg_for_unprototyped_fn. */
39376 static const char *
39377 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
39379 return (!rs6000_darwin64_abi
39380 && typelist == 0
39381 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
39382 && (funcdecl == NULL_TREE
39383 || (TREE_CODE (funcdecl) == FUNCTION_DECL
39384 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
39385 ? N_("AltiVec argument passed to unprototyped function")
39386 : NULL;
39389 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
39390 setup by using __stack_chk_fail_local hidden function instead of
39391 calling __stack_chk_fail directly. Otherwise it is better to call
39392 __stack_chk_fail directly. */
39394 static tree ATTRIBUTE_UNUSED
39395 rs6000_stack_protect_fail (void)
39397 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
39398 ? default_hidden_stack_protect_fail ()
39399 : default_external_stack_protect_fail ();
39402 void
39403 rs6000_final_prescan_insn (rtx_insn *insn, rtx *operand ATTRIBUTE_UNUSED,
39404 int num_operands ATTRIBUTE_UNUSED)
39406 if (rs6000_warn_cell_microcode)
39408 const char *temp;
39409 int insn_code_number = recog_memoized (insn);
39410 location_t location = INSN_LOCATION (insn);
39412 /* Punt on insns we cannot recognize. */
39413 if (insn_code_number < 0)
39414 return;
39416 /* get_insn_template can modify recog_data, so save and restore it. */
39417 struct recog_data_d recog_data_save = recog_data;
39418 for (int i = 0; i < recog_data.n_operands; i++)
39419 recog_data.operand[i] = copy_rtx (recog_data.operand[i]);
39420 temp = get_insn_template (insn_code_number, insn);
39421 recog_data = recog_data_save;
39423 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
39424 warning_at (location, OPT_mwarn_cell_microcode,
39425 "emitting microcode insn %s\t[%s] #%d",
39426 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
39427 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
39428 warning_at (location, OPT_mwarn_cell_microcode,
39429 "emitting conditional microcode insn %s\t[%s] #%d",
39430 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
39434 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
39436 #if TARGET_ELF
39437 static unsigned HOST_WIDE_INT
39438 rs6000_asan_shadow_offset (void)
39440 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
39442 #endif
39444 /* Mask options that we want to support inside of attribute((target)) and
39445 #pragma GCC target operations. Note, we do not include things like
39446 64/32-bit, endianness, hard/soft floating point, etc. that would have
39447 different calling sequences. */
39449 struct rs6000_opt_mask {
39450 const char *name; /* option name */
39451 HOST_WIDE_INT mask; /* mask to set */
39452 bool invert; /* invert sense of mask */
39453 bool valid_target; /* option is a target option */
39456 static struct rs6000_opt_mask const rs6000_opt_masks[] =
39458 { "altivec", OPTION_MASK_ALTIVEC, false, true },
39459 { "cmpb", OPTION_MASK_CMPB, false, true },
39460 { "crypto", OPTION_MASK_CRYPTO, false, true },
39461 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
39462 { "dlmzb", OPTION_MASK_DLMZB, false, true },
39463 { "efficient-unaligned-vsx", OPTION_MASK_EFFICIENT_UNALIGNED_VSX,
39464 false, true },
39465 { "float128", OPTION_MASK_FLOAT128_KEYWORD, false, false },
39466 { "float128-type", OPTION_MASK_FLOAT128_TYPE, false, false },
39467 { "float128-hardware", OPTION_MASK_FLOAT128_HW, false, false },
39468 { "fprnd", OPTION_MASK_FPRND, false, true },
39469 { "hard-dfp", OPTION_MASK_DFP, false, true },
39470 { "htm", OPTION_MASK_HTM, false, true },
39471 { "isel", OPTION_MASK_ISEL, false, true },
39472 { "mfcrf", OPTION_MASK_MFCRF, false, true },
39473 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
39474 { "modulo", OPTION_MASK_MODULO, false, true },
39475 { "mulhw", OPTION_MASK_MULHW, false, true },
39476 { "multiple", OPTION_MASK_MULTIPLE, false, true },
39477 { "popcntb", OPTION_MASK_POPCNTB, false, true },
39478 { "popcntd", OPTION_MASK_POPCNTD, false, true },
39479 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
39480 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
39481 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
39482 { "power9-dform-scalar", OPTION_MASK_P9_DFORM_SCALAR, false, true },
39483 { "power9-dform-vector", OPTION_MASK_P9_DFORM_VECTOR, false, true },
39484 { "power9-fusion", OPTION_MASK_P9_FUSION, false, true },
39485 { "power9-minmax", OPTION_MASK_P9_MINMAX, false, true },
39486 { "power9-misc", OPTION_MASK_P9_MISC, false, true },
39487 { "power9-vector", OPTION_MASK_P9_VECTOR, false, true },
39488 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
39489 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
39490 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
39491 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
39492 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
39493 { "save-toc-indirect", OPTION_MASK_SAVE_TOC_INDIRECT, false, true },
39494 { "string", OPTION_MASK_STRING, false, true },
39495 { "toc-fusion", OPTION_MASK_TOC_FUSION, false, true },
39496 { "update", OPTION_MASK_NO_UPDATE, true , true },
39497 { "upper-regs-di", OPTION_MASK_UPPER_REGS_DI, false, true },
39498 { "upper-regs-df", OPTION_MASK_UPPER_REGS_DF, false, true },
39499 { "upper-regs-sf", OPTION_MASK_UPPER_REGS_SF, false, true },
39500 { "vsx", OPTION_MASK_VSX, false, true },
39501 { "vsx-small-integer", OPTION_MASK_VSX_SMALL_INTEGER, false, true },
39502 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
39503 #ifdef OPTION_MASK_64BIT
39504 #if TARGET_AIX_OS
39505 { "aix64", OPTION_MASK_64BIT, false, false },
39506 { "aix32", OPTION_MASK_64BIT, true, false },
39507 #else
39508 { "64", OPTION_MASK_64BIT, false, false },
39509 { "32", OPTION_MASK_64BIT, true, false },
39510 #endif
39511 #endif
39512 #ifdef OPTION_MASK_EABI
39513 { "eabi", OPTION_MASK_EABI, false, false },
39514 #endif
39515 #ifdef OPTION_MASK_LITTLE_ENDIAN
39516 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
39517 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
39518 #endif
39519 #ifdef OPTION_MASK_RELOCATABLE
39520 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
39521 #endif
39522 #ifdef OPTION_MASK_STRICT_ALIGN
39523 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
39524 #endif
39525 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
39526 { "string", OPTION_MASK_STRING, false, false },
39529 /* Builtin mask mapping for printing the flags. */
39530 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
39532 { "altivec", RS6000_BTM_ALTIVEC, false, false },
39533 { "vsx", RS6000_BTM_VSX, false, false },
39534 { "spe", RS6000_BTM_SPE, false, false },
39535 { "paired", RS6000_BTM_PAIRED, false, false },
39536 { "fre", RS6000_BTM_FRE, false, false },
39537 { "fres", RS6000_BTM_FRES, false, false },
39538 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
39539 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
39540 { "popcntd", RS6000_BTM_POPCNTD, false, false },
39541 { "cell", RS6000_BTM_CELL, false, false },
39542 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
39543 { "power9-vector", RS6000_BTM_P9_VECTOR, false, false },
39544 { "power9-misc", RS6000_BTM_P9_MISC, false, false },
39545 { "crypto", RS6000_BTM_CRYPTO, false, false },
39546 { "htm", RS6000_BTM_HTM, false, false },
39547 { "hard-dfp", RS6000_BTM_DFP, false, false },
39548 { "hard-float", RS6000_BTM_HARD_FLOAT, false, false },
39549 { "long-double-128", RS6000_BTM_LDBL128, false, false },
39550 { "float128", RS6000_BTM_FLOAT128, false, false },
39553 /* Option variables that we want to support inside attribute((target)) and
39554 #pragma GCC target operations. */
39556 struct rs6000_opt_var {
39557 const char *name; /* option name */
39558 size_t global_offset; /* offset of the option in global_options. */
39559 size_t target_offset; /* offset of the option in target options. */
39562 static struct rs6000_opt_var const rs6000_opt_vars[] =
39564 { "friz",
39565 offsetof (struct gcc_options, x_TARGET_FRIZ),
39566 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
39567 { "avoid-indexed-addresses",
39568 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
39569 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
39570 { "paired",
39571 offsetof (struct gcc_options, x_rs6000_paired_float),
39572 offsetof (struct cl_target_option, x_rs6000_paired_float), },
39573 { "longcall",
39574 offsetof (struct gcc_options, x_rs6000_default_long_calls),
39575 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
39576 { "optimize-swaps",
39577 offsetof (struct gcc_options, x_rs6000_optimize_swaps),
39578 offsetof (struct cl_target_option, x_rs6000_optimize_swaps), },
39579 { "allow-movmisalign",
39580 offsetof (struct gcc_options, x_TARGET_ALLOW_MOVMISALIGN),
39581 offsetof (struct cl_target_option, x_TARGET_ALLOW_MOVMISALIGN), },
39582 { "allow-df-permute",
39583 offsetof (struct gcc_options, x_TARGET_ALLOW_DF_PERMUTE),
39584 offsetof (struct cl_target_option, x_TARGET_ALLOW_DF_PERMUTE), },
39585 { "sched-groups",
39586 offsetof (struct gcc_options, x_TARGET_SCHED_GROUPS),
39587 offsetof (struct cl_target_option, x_TARGET_SCHED_GROUPS), },
39588 { "always-hint",
39589 offsetof (struct gcc_options, x_TARGET_ALWAYS_HINT),
39590 offsetof (struct cl_target_option, x_TARGET_ALWAYS_HINT), },
39591 { "align-branch-targets",
39592 offsetof (struct gcc_options, x_TARGET_ALIGN_BRANCH_TARGETS),
39593 offsetof (struct cl_target_option, x_TARGET_ALIGN_BRANCH_TARGETS), },
39594 { "vectorize-builtins",
39595 offsetof (struct gcc_options, x_TARGET_VECTORIZE_BUILTINS),
39596 offsetof (struct cl_target_option, x_TARGET_VECTORIZE_BUILTINS), },
39597 { "tls-markers",
39598 offsetof (struct gcc_options, x_tls_markers),
39599 offsetof (struct cl_target_option, x_tls_markers), },
39600 { "sched-prolog",
39601 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
39602 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
39603 { "sched-epilog",
39604 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
39605 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
39606 { "gen-cell-microcode",
39607 offsetof (struct gcc_options, x_rs6000_gen_cell_microcode),
39608 offsetof (struct cl_target_option, x_rs6000_gen_cell_microcode), },
39609 { "warn-cell-microcode",
39610 offsetof (struct gcc_options, x_rs6000_warn_cell_microcode),
39611 offsetof (struct cl_target_option, x_rs6000_warn_cell_microcode), },
39614 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
39615 parsing. Return true if there were no errors. */
39617 static bool
39618 rs6000_inner_target_options (tree args, bool attr_p)
39620 bool ret = true;
39622 if (args == NULL_TREE)
39625 else if (TREE_CODE (args) == STRING_CST)
39627 char *p = ASTRDUP (TREE_STRING_POINTER (args));
39628 char *q;
39630 while ((q = strtok (p, ",")) != NULL)
39632 bool error_p = false;
39633 bool not_valid_p = false;
39634 const char *cpu_opt = NULL;
39636 p = NULL;
39637 if (strncmp (q, "cpu=", 4) == 0)
39639 int cpu_index = rs6000_cpu_name_lookup (q+4);
39640 if (cpu_index >= 0)
39641 rs6000_cpu_index = cpu_index;
39642 else
39644 error_p = true;
39645 cpu_opt = q+4;
39648 else if (strncmp (q, "tune=", 5) == 0)
39650 int tune_index = rs6000_cpu_name_lookup (q+5);
39651 if (tune_index >= 0)
39652 rs6000_tune_index = tune_index;
39653 else
39655 error_p = true;
39656 cpu_opt = q+5;
39659 else
39661 size_t i;
39662 bool invert = false;
39663 char *r = q;
39665 error_p = true;
39666 if (strncmp (r, "no-", 3) == 0)
39668 invert = true;
39669 r += 3;
39672 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
39673 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
39675 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
39677 if (!rs6000_opt_masks[i].valid_target)
39678 not_valid_p = true;
39679 else
39681 error_p = false;
39682 rs6000_isa_flags_explicit |= mask;
39684 /* VSX needs altivec, so -mvsx automagically sets
39685 altivec and disables -mavoid-indexed-addresses. */
39686 if (!invert)
39688 if (mask == OPTION_MASK_VSX)
39690 mask |= OPTION_MASK_ALTIVEC;
39691 TARGET_AVOID_XFORM = 0;
39695 if (rs6000_opt_masks[i].invert)
39696 invert = !invert;
39698 if (invert)
39699 rs6000_isa_flags &= ~mask;
39700 else
39701 rs6000_isa_flags |= mask;
39703 break;
39706 if (error_p && !not_valid_p)
39708 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
39709 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
39711 size_t j = rs6000_opt_vars[i].global_offset;
39712 *((int *) ((char *)&global_options + j)) = !invert;
39713 error_p = false;
39714 not_valid_p = false;
39715 break;
39720 if (error_p)
39722 const char *eprefix, *esuffix;
39724 ret = false;
39725 if (attr_p)
39727 eprefix = "__attribute__((__target__(";
39728 esuffix = ")))";
39730 else
39732 eprefix = "#pragma GCC target ";
39733 esuffix = "";
39736 if (cpu_opt)
39737 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
39738 q, esuffix);
39739 else if (not_valid_p)
39740 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
39741 else
39742 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
39747 else if (TREE_CODE (args) == TREE_LIST)
39751 tree value = TREE_VALUE (args);
39752 if (value)
39754 bool ret2 = rs6000_inner_target_options (value, attr_p);
39755 if (!ret2)
39756 ret = false;
39758 args = TREE_CHAIN (args);
39760 while (args != NULL_TREE);
39763 else
39765 error ("attribute %<target%> argument not a string");
39766 return false;
39769 return ret;
39772 /* Print out the target options as a list for -mdebug=target. */
39774 static void
39775 rs6000_debug_target_options (tree args, const char *prefix)
39777 if (args == NULL_TREE)
39778 fprintf (stderr, "%s<NULL>", prefix);
39780 else if (TREE_CODE (args) == STRING_CST)
39782 char *p = ASTRDUP (TREE_STRING_POINTER (args));
39783 char *q;
39785 while ((q = strtok (p, ",")) != NULL)
39787 p = NULL;
39788 fprintf (stderr, "%s\"%s\"", prefix, q);
39789 prefix = ", ";
39793 else if (TREE_CODE (args) == TREE_LIST)
39797 tree value = TREE_VALUE (args);
39798 if (value)
39800 rs6000_debug_target_options (value, prefix);
39801 prefix = ", ";
39803 args = TREE_CHAIN (args);
39805 while (args != NULL_TREE);
39808 else
39809 gcc_unreachable ();
39811 return;
39815 /* Hook to validate attribute((target("..."))). */
39817 static bool
39818 rs6000_valid_attribute_p (tree fndecl,
39819 tree ARG_UNUSED (name),
39820 tree args,
39821 int flags)
39823 struct cl_target_option cur_target;
39824 bool ret;
39825 tree old_optimize = build_optimization_node (&global_options);
39826 tree new_target, new_optimize;
39827 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
39829 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
39831 if (TARGET_DEBUG_TARGET)
39833 tree tname = DECL_NAME (fndecl);
39834 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
39835 if (tname)
39836 fprintf (stderr, "function: %.*s\n",
39837 (int) IDENTIFIER_LENGTH (tname),
39838 IDENTIFIER_POINTER (tname));
39839 else
39840 fprintf (stderr, "function: unknown\n");
39842 fprintf (stderr, "args:");
39843 rs6000_debug_target_options (args, " ");
39844 fprintf (stderr, "\n");
39846 if (flags)
39847 fprintf (stderr, "flags: 0x%x\n", flags);
39849 fprintf (stderr, "--------------------\n");
39852 old_optimize = build_optimization_node (&global_options);
39853 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
39855 /* If the function changed the optimization levels as well as setting target
39856 options, start with the optimizations specified. */
39857 if (func_optimize && func_optimize != old_optimize)
39858 cl_optimization_restore (&global_options,
39859 TREE_OPTIMIZATION (func_optimize));
39861 /* The target attributes may also change some optimization flags, so update
39862 the optimization options if necessary. */
39863 cl_target_option_save (&cur_target, &global_options);
39864 rs6000_cpu_index = rs6000_tune_index = -1;
39865 ret = rs6000_inner_target_options (args, true);
39867 /* Set up any additional state. */
39868 if (ret)
39870 ret = rs6000_option_override_internal (false);
39871 new_target = build_target_option_node (&global_options);
39873 else
39874 new_target = NULL;
39876 new_optimize = build_optimization_node (&global_options);
39878 if (!new_target)
39879 ret = false;
39881 else if (fndecl)
39883 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
39885 if (old_optimize != new_optimize)
39886 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
39889 cl_target_option_restore (&global_options, &cur_target);
39891 if (old_optimize != new_optimize)
39892 cl_optimization_restore (&global_options,
39893 TREE_OPTIMIZATION (old_optimize));
39895 return ret;
39899 /* Hook to validate the current #pragma GCC target and set the state, and
39900 update the macros based on what was changed. If ARGS is NULL, then
39901 POP_TARGET is used to reset the options. */
39903 bool
39904 rs6000_pragma_target_parse (tree args, tree pop_target)
39906 tree prev_tree = build_target_option_node (&global_options);
39907 tree cur_tree;
39908 struct cl_target_option *prev_opt, *cur_opt;
39909 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
39910 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
39912 if (TARGET_DEBUG_TARGET)
39914 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
39915 fprintf (stderr, "args:");
39916 rs6000_debug_target_options (args, " ");
39917 fprintf (stderr, "\n");
39919 if (pop_target)
39921 fprintf (stderr, "pop_target:\n");
39922 debug_tree (pop_target);
39924 else
39925 fprintf (stderr, "pop_target: <NULL>\n");
39927 fprintf (stderr, "--------------------\n");
39930 if (! args)
39932 cur_tree = ((pop_target)
39933 ? pop_target
39934 : target_option_default_node);
39935 cl_target_option_restore (&global_options,
39936 TREE_TARGET_OPTION (cur_tree));
39938 else
39940 rs6000_cpu_index = rs6000_tune_index = -1;
39941 if (!rs6000_inner_target_options (args, false)
39942 || !rs6000_option_override_internal (false)
39943 || (cur_tree = build_target_option_node (&global_options))
39944 == NULL_TREE)
39946 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
39947 fprintf (stderr, "invalid pragma\n");
39949 return false;
39953 target_option_current_node = cur_tree;
39955 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
39956 change the macros that are defined. */
39957 if (rs6000_target_modify_macros_ptr)
39959 prev_opt = TREE_TARGET_OPTION (prev_tree);
39960 prev_bumask = prev_opt->x_rs6000_builtin_mask;
39961 prev_flags = prev_opt->x_rs6000_isa_flags;
39963 cur_opt = TREE_TARGET_OPTION (cur_tree);
39964 cur_flags = cur_opt->x_rs6000_isa_flags;
39965 cur_bumask = cur_opt->x_rs6000_builtin_mask;
39967 diff_bumask = (prev_bumask ^ cur_bumask);
39968 diff_flags = (prev_flags ^ cur_flags);
39970 if ((diff_flags != 0) || (diff_bumask != 0))
39972 /* Delete old macros. */
39973 rs6000_target_modify_macros_ptr (false,
39974 prev_flags & diff_flags,
39975 prev_bumask & diff_bumask);
39977 /* Define new macros. */
39978 rs6000_target_modify_macros_ptr (true,
39979 cur_flags & diff_flags,
39980 cur_bumask & diff_bumask);
39984 return true;
39988 /* Remember the last target of rs6000_set_current_function. */
39989 static GTY(()) tree rs6000_previous_fndecl;
39991 /* Establish appropriate back-end context for processing the function
39992 FNDECL. The argument might be NULL to indicate processing at top
39993 level, outside of any function scope. */
39994 static void
39995 rs6000_set_current_function (tree fndecl)
39997 tree old_tree = (rs6000_previous_fndecl
39998 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
39999 : NULL_TREE);
40001 tree new_tree = (fndecl
40002 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
40003 : NULL_TREE);
40005 if (TARGET_DEBUG_TARGET)
40007 bool print_final = false;
40008 fprintf (stderr, "\n==================== rs6000_set_current_function");
40010 if (fndecl)
40011 fprintf (stderr, ", fndecl %s (%p)",
40012 (DECL_NAME (fndecl)
40013 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
40014 : "<unknown>"), (void *)fndecl);
40016 if (rs6000_previous_fndecl)
40017 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
40019 fprintf (stderr, "\n");
40020 if (new_tree)
40022 fprintf (stderr, "\nnew fndecl target specific options:\n");
40023 debug_tree (new_tree);
40024 print_final = true;
40027 if (old_tree)
40029 fprintf (stderr, "\nold fndecl target specific options:\n");
40030 debug_tree (old_tree);
40031 print_final = true;
40034 if (print_final)
40035 fprintf (stderr, "--------------------\n");
40038 /* Only change the context if the function changes. This hook is called
40039 several times in the course of compiling a function, and we don't want to
40040 slow things down too much or call target_reinit when it isn't safe. */
40041 if (fndecl && fndecl != rs6000_previous_fndecl)
40043 rs6000_previous_fndecl = fndecl;
40044 if (old_tree == new_tree)
40047 else if (new_tree && new_tree != target_option_default_node)
40049 cl_target_option_restore (&global_options,
40050 TREE_TARGET_OPTION (new_tree));
40051 if (TREE_TARGET_GLOBALS (new_tree))
40052 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
40053 else
40054 TREE_TARGET_GLOBALS (new_tree)
40055 = save_target_globals_default_opts ();
40058 else if (old_tree && old_tree != target_option_default_node)
40060 new_tree = target_option_current_node;
40061 cl_target_option_restore (&global_options,
40062 TREE_TARGET_OPTION (new_tree));
40063 if (TREE_TARGET_GLOBALS (new_tree))
40064 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
40065 else if (new_tree == target_option_default_node)
40066 restore_target_globals (&default_target_globals);
40067 else
40068 TREE_TARGET_GLOBALS (new_tree)
40069 = save_target_globals_default_opts ();
40075 /* Save the current options */
40077 static void
40078 rs6000_function_specific_save (struct cl_target_option *ptr,
40079 struct gcc_options *opts)
40081 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
40082 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
40085 /* Restore the current options */
40087 static void
40088 rs6000_function_specific_restore (struct gcc_options *opts,
40089 struct cl_target_option *ptr)
40092 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
40093 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
40094 (void) rs6000_option_override_internal (false);
40097 /* Print the current options */
40099 static void
40100 rs6000_function_specific_print (FILE *file, int indent,
40101 struct cl_target_option *ptr)
40103 rs6000_print_isa_options (file, indent, "Isa options set",
40104 ptr->x_rs6000_isa_flags);
40106 rs6000_print_isa_options (file, indent, "Isa options explicit",
40107 ptr->x_rs6000_isa_flags_explicit);
40110 /* Helper function to print the current isa or misc options on a line. */
40112 static void
40113 rs6000_print_options_internal (FILE *file,
40114 int indent,
40115 const char *string,
40116 HOST_WIDE_INT flags,
40117 const char *prefix,
40118 const struct rs6000_opt_mask *opts,
40119 size_t num_elements)
40121 size_t i;
40122 size_t start_column = 0;
40123 size_t cur_column;
40124 size_t max_column = 120;
40125 size_t prefix_len = strlen (prefix);
40126 size_t comma_len = 0;
40127 const char *comma = "";
40129 if (indent)
40130 start_column += fprintf (file, "%*s", indent, "");
40132 if (!flags)
40134 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
40135 return;
40138 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
40140 /* Print the various mask options. */
40141 cur_column = start_column;
40142 for (i = 0; i < num_elements; i++)
40144 bool invert = opts[i].invert;
40145 const char *name = opts[i].name;
40146 const char *no_str = "";
40147 HOST_WIDE_INT mask = opts[i].mask;
40148 size_t len = comma_len + prefix_len + strlen (name);
40150 if (!invert)
40152 if ((flags & mask) == 0)
40154 no_str = "no-";
40155 len += sizeof ("no-") - 1;
40158 flags &= ~mask;
40161 else
40163 if ((flags & mask) != 0)
40165 no_str = "no-";
40166 len += sizeof ("no-") - 1;
40169 flags |= mask;
40172 cur_column += len;
40173 if (cur_column > max_column)
40175 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
40176 cur_column = start_column + len;
40177 comma = "";
40180 fprintf (file, "%s%s%s%s", comma, prefix, no_str, name);
40181 comma = ", ";
40182 comma_len = sizeof (", ") - 1;
40185 fputs ("\n", file);
40188 /* Helper function to print the current isa options on a line. */
40190 static void
40191 rs6000_print_isa_options (FILE *file, int indent, const char *string,
40192 HOST_WIDE_INT flags)
40194 rs6000_print_options_internal (file, indent, string, flags, "-m",
40195 &rs6000_opt_masks[0],
40196 ARRAY_SIZE (rs6000_opt_masks));
40199 static void
40200 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
40201 HOST_WIDE_INT flags)
40203 rs6000_print_options_internal (file, indent, string, flags, "",
40204 &rs6000_builtin_mask_names[0],
40205 ARRAY_SIZE (rs6000_builtin_mask_names));
40208 /* If the user used -mno-vsx, we need turn off all of the implicit ISA 2.06,
40209 2.07, and 3.0 options that relate to the vector unit (-mdirect-move,
40210 -mvsx-timode, -mupper-regs-df).
40212 If the user used -mno-power8-vector, we need to turn off all of the implicit
40213 ISA 2.07 and 3.0 options that relate to the vector unit.
40215 If the user used -mno-power9-vector, we need to turn off all of the implicit
40216 ISA 3.0 options that relate to the vector unit.
40218 This function does not handle explicit options such as the user specifying
40219 -mdirect-move. These are handled in rs6000_option_override_internal, and
40220 the appropriate error is given if needed.
40222 We return a mask of all of the implicit options that should not be enabled
40223 by default. */
40225 static HOST_WIDE_INT
40226 rs6000_disable_incompatible_switches (void)
40228 HOST_WIDE_INT ignore_masks = rs6000_isa_flags_explicit;
40229 size_t i, j;
40231 static const struct {
40232 const HOST_WIDE_INT no_flag; /* flag explicitly turned off. */
40233 const HOST_WIDE_INT dep_flags; /* flags that depend on this option. */
40234 const char *const name; /* name of the switch. */
40235 } flags[] = {
40236 { OPTION_MASK_P9_VECTOR, OTHER_P9_VECTOR_MASKS, "power9-vector" },
40237 { OPTION_MASK_P8_VECTOR, OTHER_P8_VECTOR_MASKS, "power8-vector" },
40238 { OPTION_MASK_VSX, OTHER_VSX_VECTOR_MASKS, "vsx" },
40241 for (i = 0; i < ARRAY_SIZE (flags); i++)
40243 HOST_WIDE_INT no_flag = flags[i].no_flag;
40245 if ((rs6000_isa_flags & no_flag) == 0
40246 && (rs6000_isa_flags_explicit & no_flag) != 0)
40248 HOST_WIDE_INT dep_flags = flags[i].dep_flags;
40249 HOST_WIDE_INT set_flags = (rs6000_isa_flags_explicit
40250 & rs6000_isa_flags
40251 & dep_flags);
40253 if (set_flags)
40255 for (j = 0; j < ARRAY_SIZE (rs6000_opt_masks); j++)
40256 if ((set_flags & rs6000_opt_masks[j].mask) != 0)
40258 set_flags &= ~rs6000_opt_masks[j].mask;
40259 error ("-mno-%s turns off -m%s",
40260 flags[i].name,
40261 rs6000_opt_masks[j].name);
40264 gcc_assert (!set_flags);
40267 rs6000_isa_flags &= ~dep_flags;
40268 ignore_masks |= no_flag | dep_flags;
40272 if (!TARGET_P9_VECTOR
40273 && (rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) != 0
40274 && TARGET_P9_DFORM_BOTH > 0)
40276 error ("-mno-power9-vector turns off -mpower9-dform");
40277 TARGET_P9_DFORM_BOTH = 0;
40280 return ignore_masks;
40284 /* Hook to determine if one function can safely inline another. */
40286 static bool
40287 rs6000_can_inline_p (tree caller, tree callee)
40289 bool ret = false;
40290 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
40291 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
40293 /* If callee has no option attributes, then it is ok to inline. */
40294 if (!callee_tree)
40295 ret = true;
40297 /* If caller has no option attributes, but callee does then it is not ok to
40298 inline. */
40299 else if (!caller_tree)
40300 ret = false;
40302 else
40304 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
40305 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
40307 /* Callee's options should a subset of the caller's, i.e. a vsx function
40308 can inline an altivec function but a non-vsx function can't inline a
40309 vsx function. */
40310 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
40311 == callee_opts->x_rs6000_isa_flags)
40312 ret = true;
40315 if (TARGET_DEBUG_TARGET)
40316 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
40317 (DECL_NAME (caller)
40318 ? IDENTIFIER_POINTER (DECL_NAME (caller))
40319 : "<unknown>"),
40320 (DECL_NAME (callee)
40321 ? IDENTIFIER_POINTER (DECL_NAME (callee))
40322 : "<unknown>"),
40323 (ret ? "can" : "cannot"));
40325 return ret;
40328 /* Allocate a stack temp and fixup the address so it meets the particular
40329 memory requirements (either offetable or REG+REG addressing). */
40332 rs6000_allocate_stack_temp (machine_mode mode,
40333 bool offsettable_p,
40334 bool reg_reg_p)
40336 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
40337 rtx addr = XEXP (stack, 0);
40338 int strict_p = (reload_in_progress || reload_completed);
40340 if (!legitimate_indirect_address_p (addr, strict_p))
40342 if (offsettable_p
40343 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
40344 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
40346 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
40347 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
40350 return stack;
40353 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
40354 to such a form to deal with memory reference instructions like STFIWX that
40355 only take reg+reg addressing. */
40358 rs6000_address_for_fpconvert (rtx x)
40360 int strict_p = (reload_in_progress || reload_completed);
40361 rtx addr;
40363 gcc_assert (MEM_P (x));
40364 addr = XEXP (x, 0);
40365 if (! legitimate_indirect_address_p (addr, strict_p)
40366 && ! legitimate_indexed_address_p (addr, strict_p))
40368 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
40370 rtx reg = XEXP (addr, 0);
40371 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
40372 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
40373 gcc_assert (REG_P (reg));
40374 emit_insn (gen_add3_insn (reg, reg, size_rtx));
40375 addr = reg;
40377 else if (GET_CODE (addr) == PRE_MODIFY)
40379 rtx reg = XEXP (addr, 0);
40380 rtx expr = XEXP (addr, 1);
40381 gcc_assert (REG_P (reg));
40382 gcc_assert (GET_CODE (expr) == PLUS);
40383 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
40384 addr = reg;
40387 x = replace_equiv_address (x, copy_addr_to_reg (addr));
40390 return x;
40393 /* Given a memory reference, if it is not in the form for altivec memory
40394 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
40395 convert to the altivec format. */
40398 rs6000_address_for_altivec (rtx x)
40400 gcc_assert (MEM_P (x));
40401 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
40403 rtx addr = XEXP (x, 0);
40404 int strict_p = (reload_in_progress || reload_completed);
40406 if (!legitimate_indexed_address_p (addr, strict_p)
40407 && !legitimate_indirect_address_p (addr, strict_p))
40408 addr = copy_to_mode_reg (Pmode, addr);
40410 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
40411 x = change_address (x, GET_MODE (x), addr);
40414 return x;
40417 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
40419 On the RS/6000, all integer constants are acceptable, most won't be valid
40420 for particular insns, though. Only easy FP constants are acceptable. */
40422 static bool
40423 rs6000_legitimate_constant_p (machine_mode mode, rtx x)
40425 if (TARGET_ELF && tls_referenced_p (x))
40426 return false;
40428 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
40429 || GET_MODE (x) == VOIDmode
40430 || (TARGET_POWERPC64 && mode == DImode)
40431 || easy_fp_constant (x, mode)
40432 || easy_vector_constant (x, mode));
40436 /* Return TRUE iff the sequence ending in LAST sets the static chain. */
40438 static bool
40439 chain_already_loaded (rtx_insn *last)
40441 for (; last != NULL; last = PREV_INSN (last))
40443 if (NONJUMP_INSN_P (last))
40445 rtx patt = PATTERN (last);
40447 if (GET_CODE (patt) == SET)
40449 rtx lhs = XEXP (patt, 0);
40451 if (REG_P (lhs) && REGNO (lhs) == STATIC_CHAIN_REGNUM)
40452 return true;
40456 return false;
40459 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
40461 void
40462 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
40464 const bool direct_call_p
40465 = GET_CODE (func_desc) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (func_desc);
40466 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
40467 rtx toc_load = NULL_RTX;
40468 rtx toc_restore = NULL_RTX;
40469 rtx func_addr;
40470 rtx abi_reg = NULL_RTX;
40471 rtx call[4];
40472 int n_call;
40473 rtx insn;
40475 /* Handle longcall attributes. */
40476 if (INTVAL (cookie) & CALL_LONG)
40477 func_desc = rs6000_longcall_ref (func_desc);
40479 /* Handle indirect calls. */
40480 if (GET_CODE (func_desc) != SYMBOL_REF
40481 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
40483 /* Save the TOC into its reserved slot before the call,
40484 and prepare to restore it after the call. */
40485 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
40486 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
40487 rtx stack_toc_mem = gen_frame_mem (Pmode,
40488 gen_rtx_PLUS (Pmode, stack_ptr,
40489 stack_toc_offset));
40490 rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode,
40491 gen_rtvec (1, stack_toc_offset),
40492 UNSPEC_TOCSLOT);
40493 toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec);
40495 /* Can we optimize saving the TOC in the prologue or
40496 do we need to do it at every call? */
40497 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
40498 cfun->machine->save_toc_in_prologue = true;
40499 else
40501 MEM_VOLATILE_P (stack_toc_mem) = 1;
40502 emit_move_insn (stack_toc_mem, toc_reg);
40505 if (DEFAULT_ABI == ABI_ELFv2)
40507 /* A function pointer in the ELFv2 ABI is just a plain address, but
40508 the ABI requires it to be loaded into r12 before the call. */
40509 func_addr = gen_rtx_REG (Pmode, 12);
40510 emit_move_insn (func_addr, func_desc);
40511 abi_reg = func_addr;
40513 else
40515 /* A function pointer under AIX is a pointer to a data area whose
40516 first word contains the actual address of the function, whose
40517 second word contains a pointer to its TOC, and whose third word
40518 contains a value to place in the static chain register (r11).
40519 Note that if we load the static chain, our "trampoline" need
40520 not have any executable code. */
40522 /* Load up address of the actual function. */
40523 func_desc = force_reg (Pmode, func_desc);
40524 func_addr = gen_reg_rtx (Pmode);
40525 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
40527 /* Prepare to load the TOC of the called function. Note that the
40528 TOC load must happen immediately before the actual call so
40529 that unwinding the TOC registers works correctly. See the
40530 comment in frob_update_context. */
40531 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
40532 rtx func_toc_mem = gen_rtx_MEM (Pmode,
40533 gen_rtx_PLUS (Pmode, func_desc,
40534 func_toc_offset));
40535 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
40537 /* If we have a static chain, load it up. But, if the call was
40538 originally direct, the 3rd word has not been written since no
40539 trampoline has been built, so we ought not to load it, lest we
40540 override a static chain value. */
40541 if (!direct_call_p
40542 && TARGET_POINTERS_TO_NESTED_FUNCTIONS
40543 && !chain_already_loaded (get_current_sequence ()->next->last))
40545 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
40546 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
40547 rtx func_sc_mem = gen_rtx_MEM (Pmode,
40548 gen_rtx_PLUS (Pmode, func_desc,
40549 func_sc_offset));
40550 emit_move_insn (sc_reg, func_sc_mem);
40551 abi_reg = sc_reg;
40555 else
40557 /* Direct calls use the TOC: for local calls, the callee will
40558 assume the TOC register is set; for non-local calls, the
40559 PLT stub needs the TOC register. */
40560 abi_reg = toc_reg;
40561 func_addr = func_desc;
40564 /* Create the call. */
40565 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
40566 if (value != NULL_RTX)
40567 call[0] = gen_rtx_SET (value, call[0]);
40568 n_call = 1;
40570 if (toc_load)
40571 call[n_call++] = toc_load;
40572 if (toc_restore)
40573 call[n_call++] = toc_restore;
40575 call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
40577 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
40578 insn = emit_call_insn (insn);
40580 /* Mention all registers defined by the ABI to hold information
40581 as uses in CALL_INSN_FUNCTION_USAGE. */
40582 if (abi_reg)
40583 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
40586 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
40588 void
40589 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
40591 rtx call[2];
40592 rtx insn;
40594 gcc_assert (INTVAL (cookie) == 0);
40596 /* Create the call. */
40597 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
40598 if (value != NULL_RTX)
40599 call[0] = gen_rtx_SET (value, call[0]);
40601 call[1] = simple_return_rtx;
40603 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
40604 insn = emit_call_insn (insn);
40606 /* Note use of the TOC register. */
40607 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
40610 /* Return whether we need to always update the saved TOC pointer when we update
40611 the stack pointer. */
40613 static bool
40614 rs6000_save_toc_in_prologue_p (void)
40616 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
40619 #ifdef HAVE_GAS_HIDDEN
40620 # define USE_HIDDEN_LINKONCE 1
40621 #else
40622 # define USE_HIDDEN_LINKONCE 0
40623 #endif
40625 /* Fills in the label name that should be used for a 476 link stack thunk. */
40627 void
40628 get_ppc476_thunk_name (char name[32])
40630 gcc_assert (TARGET_LINK_STACK);
40632 if (USE_HIDDEN_LINKONCE)
40633 sprintf (name, "__ppc476.get_thunk");
40634 else
40635 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
40638 /* This function emits the simple thunk routine that is used to preserve
40639 the link stack on the 476 cpu. */
40641 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
40642 static void
40643 rs6000_code_end (void)
40645 char name[32];
40646 tree decl;
40648 if (!TARGET_LINK_STACK)
40649 return;
40651 get_ppc476_thunk_name (name);
40653 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
40654 build_function_type_list (void_type_node, NULL_TREE));
40655 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
40656 NULL_TREE, void_type_node);
40657 TREE_PUBLIC (decl) = 1;
40658 TREE_STATIC (decl) = 1;
40660 #if RS6000_WEAK
40661 if (USE_HIDDEN_LINKONCE && !TARGET_XCOFF)
40663 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
40664 targetm.asm_out.unique_section (decl, 0);
40665 switch_to_section (get_named_section (decl, NULL, 0));
40666 DECL_WEAK (decl) = 1;
40667 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
40668 targetm.asm_out.globalize_label (asm_out_file, name);
40669 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
40670 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
40672 else
40673 #endif
40675 switch_to_section (text_section);
40676 ASM_OUTPUT_LABEL (asm_out_file, name);
40679 DECL_INITIAL (decl) = make_node (BLOCK);
40680 current_function_decl = decl;
40681 allocate_struct_function (decl, false);
40682 init_function_start (decl);
40683 first_function_block_is_cold = false;
40684 /* Make sure unwind info is emitted for the thunk if needed. */
40685 final_start_function (emit_barrier (), asm_out_file, 1);
40687 fputs ("\tblr\n", asm_out_file);
40689 final_end_function ();
40690 init_insn_lengths ();
40691 free_after_compilation (cfun);
40692 set_cfun (NULL);
40693 current_function_decl = NULL;
40696 /* Add r30 to hard reg set if the prologue sets it up and it is not
40697 pic_offset_table_rtx. */
40699 static void
40700 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
40702 if (!TARGET_SINGLE_PIC_BASE
40703 && TARGET_TOC
40704 && TARGET_MINIMAL_TOC
40705 && !constant_pool_empty_p ())
40706 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
40707 if (cfun->machine->split_stack_argp_used)
40708 add_to_hard_reg_set (&set->set, Pmode, 12);
40712 /* Helper function for rs6000_split_logical to emit a logical instruction after
40713 spliting the operation to single GPR registers.
40715 DEST is the destination register.
40716 OP1 and OP2 are the input source registers.
40717 CODE is the base operation (AND, IOR, XOR, NOT).
40718 MODE is the machine mode.
40719 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
40720 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
40721 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
40723 static void
40724 rs6000_split_logical_inner (rtx dest,
40725 rtx op1,
40726 rtx op2,
40727 enum rtx_code code,
40728 machine_mode mode,
40729 bool complement_final_p,
40730 bool complement_op1_p,
40731 bool complement_op2_p)
40733 rtx bool_rtx;
40735 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
40736 if (op2 && GET_CODE (op2) == CONST_INT
40737 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
40738 && !complement_final_p && !complement_op1_p && !complement_op2_p)
40740 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
40741 HOST_WIDE_INT value = INTVAL (op2) & mask;
40743 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
40744 if (code == AND)
40746 if (value == 0)
40748 emit_insn (gen_rtx_SET (dest, const0_rtx));
40749 return;
40752 else if (value == mask)
40754 if (!rtx_equal_p (dest, op1))
40755 emit_insn (gen_rtx_SET (dest, op1));
40756 return;
40760 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
40761 into separate ORI/ORIS or XORI/XORIS instrucitons. */
40762 else if (code == IOR || code == XOR)
40764 if (value == 0)
40766 if (!rtx_equal_p (dest, op1))
40767 emit_insn (gen_rtx_SET (dest, op1));
40768 return;
40773 if (code == AND && mode == SImode
40774 && !complement_final_p && !complement_op1_p && !complement_op2_p)
40776 emit_insn (gen_andsi3 (dest, op1, op2));
40777 return;
40780 if (complement_op1_p)
40781 op1 = gen_rtx_NOT (mode, op1);
40783 if (complement_op2_p)
40784 op2 = gen_rtx_NOT (mode, op2);
40786 /* For canonical RTL, if only one arm is inverted it is the first. */
40787 if (!complement_op1_p && complement_op2_p)
40788 std::swap (op1, op2);
40790 bool_rtx = ((code == NOT)
40791 ? gen_rtx_NOT (mode, op1)
40792 : gen_rtx_fmt_ee (code, mode, op1, op2));
40794 if (complement_final_p)
40795 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
40797 emit_insn (gen_rtx_SET (dest, bool_rtx));
40800 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
40801 operations are split immediately during RTL generation to allow for more
40802 optimizations of the AND/IOR/XOR.
40804 OPERANDS is an array containing the destination and two input operands.
40805 CODE is the base operation (AND, IOR, XOR, NOT).
40806 MODE is the machine mode.
40807 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
40808 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
40809 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
40810 CLOBBER_REG is either NULL or a scratch register of type CC to allow
40811 formation of the AND instructions. */
40813 static void
40814 rs6000_split_logical_di (rtx operands[3],
40815 enum rtx_code code,
40816 bool complement_final_p,
40817 bool complement_op1_p,
40818 bool complement_op2_p)
40820 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
40821 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
40822 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
40823 enum hi_lo { hi = 0, lo = 1 };
40824 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
40825 size_t i;
40827 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
40828 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
40829 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
40830 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
40832 if (code == NOT)
40833 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
40834 else
40836 if (GET_CODE (operands[2]) != CONST_INT)
40838 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
40839 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
40841 else
40843 HOST_WIDE_INT value = INTVAL (operands[2]);
40844 HOST_WIDE_INT value_hi_lo[2];
40846 gcc_assert (!complement_final_p);
40847 gcc_assert (!complement_op1_p);
40848 gcc_assert (!complement_op2_p);
40850 value_hi_lo[hi] = value >> 32;
40851 value_hi_lo[lo] = value & lower_32bits;
40853 for (i = 0; i < 2; i++)
40855 HOST_WIDE_INT sub_value = value_hi_lo[i];
40857 if (sub_value & sign_bit)
40858 sub_value |= upper_32bits;
40860 op2_hi_lo[i] = GEN_INT (sub_value);
40862 /* If this is an AND instruction, check to see if we need to load
40863 the value in a register. */
40864 if (code == AND && sub_value != -1 && sub_value != 0
40865 && !and_operand (op2_hi_lo[i], SImode))
40866 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
40871 for (i = 0; i < 2; i++)
40873 /* Split large IOR/XOR operations. */
40874 if ((code == IOR || code == XOR)
40875 && GET_CODE (op2_hi_lo[i]) == CONST_INT
40876 && !complement_final_p
40877 && !complement_op1_p
40878 && !complement_op2_p
40879 && !logical_const_operand (op2_hi_lo[i], SImode))
40881 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
40882 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
40883 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
40884 rtx tmp = gen_reg_rtx (SImode);
40886 /* Make sure the constant is sign extended. */
40887 if ((hi_16bits & sign_bit) != 0)
40888 hi_16bits |= upper_32bits;
40890 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
40891 code, SImode, false, false, false);
40893 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
40894 code, SImode, false, false, false);
40896 else
40897 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
40898 code, SImode, complement_final_p,
40899 complement_op1_p, complement_op2_p);
40902 return;
40905 /* Split the insns that make up boolean operations operating on multiple GPR
40906 registers. The boolean MD patterns ensure that the inputs either are
40907 exactly the same as the output registers, or there is no overlap.
40909 OPERANDS is an array containing the destination and two input operands.
40910 CODE is the base operation (AND, IOR, XOR, NOT).
40911 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
40912 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
40913 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
40915 void
40916 rs6000_split_logical (rtx operands[3],
40917 enum rtx_code code,
40918 bool complement_final_p,
40919 bool complement_op1_p,
40920 bool complement_op2_p)
40922 machine_mode mode = GET_MODE (operands[0]);
40923 machine_mode sub_mode;
40924 rtx op0, op1, op2;
40925 int sub_size, regno0, regno1, nregs, i;
40927 /* If this is DImode, use the specialized version that can run before
40928 register allocation. */
40929 if (mode == DImode && !TARGET_POWERPC64)
40931 rs6000_split_logical_di (operands, code, complement_final_p,
40932 complement_op1_p, complement_op2_p);
40933 return;
40936 op0 = operands[0];
40937 op1 = operands[1];
40938 op2 = (code == NOT) ? NULL_RTX : operands[2];
40939 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
40940 sub_size = GET_MODE_SIZE (sub_mode);
40941 regno0 = REGNO (op0);
40942 regno1 = REGNO (op1);
40944 gcc_assert (reload_completed);
40945 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
40946 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
40948 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
40949 gcc_assert (nregs > 1);
40951 if (op2 && REG_P (op2))
40952 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
40954 for (i = 0; i < nregs; i++)
40956 int offset = i * sub_size;
40957 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
40958 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
40959 rtx sub_op2 = ((code == NOT)
40960 ? NULL_RTX
40961 : simplify_subreg (sub_mode, op2, mode, offset));
40963 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
40964 complement_final_p, complement_op1_p,
40965 complement_op2_p);
40968 return;
40972 /* Return true if the peephole2 can combine a load involving a combination of
40973 an addis instruction and a load with an offset that can be fused together on
40974 a power8. */
40976 bool
40977 fusion_gpr_load_p (rtx addis_reg, /* register set via addis. */
40978 rtx addis_value, /* addis value. */
40979 rtx target, /* target register that is loaded. */
40980 rtx mem) /* bottom part of the memory addr. */
40982 rtx addr;
40983 rtx base_reg;
40985 /* Validate arguments. */
40986 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
40987 return false;
40989 if (!base_reg_operand (target, GET_MODE (target)))
40990 return false;
40992 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
40993 return false;
40995 /* Allow sign/zero extension. */
40996 if (GET_CODE (mem) == ZERO_EXTEND
40997 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
40998 mem = XEXP (mem, 0);
41000 if (!MEM_P (mem))
41001 return false;
41003 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
41004 return false;
41006 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
41007 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
41008 return false;
41010 /* Validate that the register used to load the high value is either the
41011 register being loaded, or we can safely replace its use.
41013 This function is only called from the peephole2 pass and we assume that
41014 there are 2 instructions in the peephole (addis and load), so we want to
41015 check if the target register was not used in the memory address and the
41016 register to hold the addis result is dead after the peephole. */
41017 if (REGNO (addis_reg) != REGNO (target))
41019 if (reg_mentioned_p (target, mem))
41020 return false;
41022 if (!peep2_reg_dead_p (2, addis_reg))
41023 return false;
41025 /* If the target register being loaded is the stack pointer, we must
41026 avoid loading any other value into it, even temporarily. */
41027 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
41028 return false;
41031 base_reg = XEXP (addr, 0);
41032 return REGNO (addis_reg) == REGNO (base_reg);
41035 /* During the peephole2 pass, adjust and expand the insns for a load fusion
41036 sequence. We adjust the addis register to use the target register. If the
41037 load sign extends, we adjust the code to do the zero extending load, and an
41038 explicit sign extension later since the fusion only covers zero extending
41039 loads.
41041 The operands are:
41042 operands[0] register set with addis (to be replaced with target)
41043 operands[1] value set via addis
41044 operands[2] target register being loaded
41045 operands[3] D-form memory reference using operands[0]. */
41047 void
41048 expand_fusion_gpr_load (rtx *operands)
41050 rtx addis_value = operands[1];
41051 rtx target = operands[2];
41052 rtx orig_mem = operands[3];
41053 rtx new_addr, new_mem, orig_addr, offset;
41054 enum rtx_code plus_or_lo_sum;
41055 machine_mode target_mode = GET_MODE (target);
41056 machine_mode extend_mode = target_mode;
41057 machine_mode ptr_mode = Pmode;
41058 enum rtx_code extend = UNKNOWN;
41060 if (GET_CODE (orig_mem) == ZERO_EXTEND
41061 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
41063 extend = GET_CODE (orig_mem);
41064 orig_mem = XEXP (orig_mem, 0);
41065 target_mode = GET_MODE (orig_mem);
41068 gcc_assert (MEM_P (orig_mem));
41070 orig_addr = XEXP (orig_mem, 0);
41071 plus_or_lo_sum = GET_CODE (orig_addr);
41072 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
41074 offset = XEXP (orig_addr, 1);
41075 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
41076 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
41078 if (extend != UNKNOWN)
41079 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
41081 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
41082 UNSPEC_FUSION_GPR);
41083 emit_insn (gen_rtx_SET (target, new_mem));
41085 if (extend == SIGN_EXTEND)
41087 int sub_off = ((BYTES_BIG_ENDIAN)
41088 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
41089 : 0);
41090 rtx sign_reg
41091 = simplify_subreg (target_mode, target, extend_mode, sub_off);
41093 emit_insn (gen_rtx_SET (target,
41094 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
41097 return;
41100 /* Emit the addis instruction that will be part of a fused instruction
41101 sequence. */
41103 void
41104 emit_fusion_addis (rtx target, rtx addis_value, const char *comment,
41105 const char *mode_name)
41107 rtx fuse_ops[10];
41108 char insn_template[80];
41109 const char *addis_str = NULL;
41110 const char *comment_str = ASM_COMMENT_START;
41112 if (*comment_str == ' ')
41113 comment_str++;
41115 /* Emit the addis instruction. */
41116 fuse_ops[0] = target;
41117 if (satisfies_constraint_L (addis_value))
41119 fuse_ops[1] = addis_value;
41120 addis_str = "lis %0,%v1";
41123 else if (GET_CODE (addis_value) == PLUS)
41125 rtx op0 = XEXP (addis_value, 0);
41126 rtx op1 = XEXP (addis_value, 1);
41128 if (REG_P (op0) && CONST_INT_P (op1)
41129 && satisfies_constraint_L (op1))
41131 fuse_ops[1] = op0;
41132 fuse_ops[2] = op1;
41133 addis_str = "addis %0,%1,%v2";
41137 else if (GET_CODE (addis_value) == HIGH)
41139 rtx value = XEXP (addis_value, 0);
41140 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
41142 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
41143 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
41144 if (TARGET_ELF)
41145 addis_str = "addis %0,%2,%1@toc@ha";
41147 else if (TARGET_XCOFF)
41148 addis_str = "addis %0,%1@u(%2)";
41150 else
41151 gcc_unreachable ();
41154 else if (GET_CODE (value) == PLUS)
41156 rtx op0 = XEXP (value, 0);
41157 rtx op1 = XEXP (value, 1);
41159 if (GET_CODE (op0) == UNSPEC
41160 && XINT (op0, 1) == UNSPEC_TOCREL
41161 && CONST_INT_P (op1))
41163 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
41164 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
41165 fuse_ops[3] = op1;
41166 if (TARGET_ELF)
41167 addis_str = "addis %0,%2,%1+%3@toc@ha";
41169 else if (TARGET_XCOFF)
41170 addis_str = "addis %0,%1+%3@u(%2)";
41172 else
41173 gcc_unreachable ();
41177 else if (satisfies_constraint_L (value))
41179 fuse_ops[1] = value;
41180 addis_str = "lis %0,%v1";
41183 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
41185 fuse_ops[1] = value;
41186 addis_str = "lis %0,%1@ha";
41190 if (!addis_str)
41191 fatal_insn ("Could not generate addis value for fusion", addis_value);
41193 sprintf (insn_template, "%s\t\t%s %s, type %s", addis_str, comment_str,
41194 comment, mode_name);
41195 output_asm_insn (insn_template, fuse_ops);
41198 /* Emit a D-form load or store instruction that is the second instruction
41199 of a fusion sequence. */
41201 void
41202 emit_fusion_load_store (rtx load_store_reg, rtx addis_reg, rtx offset,
41203 const char *insn_str)
41205 rtx fuse_ops[10];
41206 char insn_template[80];
41208 fuse_ops[0] = load_store_reg;
41209 fuse_ops[1] = addis_reg;
41211 if (CONST_INT_P (offset) && satisfies_constraint_I (offset))
41213 sprintf (insn_template, "%s %%0,%%2(%%1)", insn_str);
41214 fuse_ops[2] = offset;
41215 output_asm_insn (insn_template, fuse_ops);
41218 else if (GET_CODE (offset) == UNSPEC
41219 && XINT (offset, 1) == UNSPEC_TOCREL)
41221 if (TARGET_ELF)
41222 sprintf (insn_template, "%s %%0,%%2@toc@l(%%1)", insn_str);
41224 else if (TARGET_XCOFF)
41225 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
41227 else
41228 gcc_unreachable ();
41230 fuse_ops[2] = XVECEXP (offset, 0, 0);
41231 output_asm_insn (insn_template, fuse_ops);
41234 else if (GET_CODE (offset) == PLUS
41235 && GET_CODE (XEXP (offset, 0)) == UNSPEC
41236 && XINT (XEXP (offset, 0), 1) == UNSPEC_TOCREL
41237 && CONST_INT_P (XEXP (offset, 1)))
41239 rtx tocrel_unspec = XEXP (offset, 0);
41240 if (TARGET_ELF)
41241 sprintf (insn_template, "%s %%0,%%2+%%3@toc@l(%%1)", insn_str);
41243 else if (TARGET_XCOFF)
41244 sprintf (insn_template, "%s %%0,%%2+%%3@l(%%1)", insn_str);
41246 else
41247 gcc_unreachable ();
41249 fuse_ops[2] = XVECEXP (tocrel_unspec, 0, 0);
41250 fuse_ops[3] = XEXP (offset, 1);
41251 output_asm_insn (insn_template, fuse_ops);
41254 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (offset))
41256 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
41258 fuse_ops[2] = offset;
41259 output_asm_insn (insn_template, fuse_ops);
41262 else
41263 fatal_insn ("Unable to generate load/store offset for fusion", offset);
41265 return;
41268 /* Wrap a TOC address that can be fused to indicate that special fusion
41269 processing is needed. */
41272 fusion_wrap_memory_address (rtx old_mem)
41274 rtx old_addr = XEXP (old_mem, 0);
41275 rtvec v = gen_rtvec (1, old_addr);
41276 rtx new_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_FUSION_ADDIS);
41277 return replace_equiv_address_nv (old_mem, new_addr, false);
41280 /* Given an address, convert it into the addis and load offset parts. Addresses
41281 created during the peephole2 process look like:
41282 (lo_sum (high (unspec [(sym)] UNSPEC_TOCREL))
41283 (unspec [(...)] UNSPEC_TOCREL))
41285 Addresses created via toc fusion look like:
41286 (unspec [(unspec [(...)] UNSPEC_TOCREL)] UNSPEC_FUSION_ADDIS)) */
41288 static void
41289 fusion_split_address (rtx addr, rtx *p_hi, rtx *p_lo)
41291 rtx hi, lo;
41293 if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_FUSION_ADDIS)
41295 lo = XVECEXP (addr, 0, 0);
41296 hi = gen_rtx_HIGH (Pmode, lo);
41298 else if (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
41300 hi = XEXP (addr, 0);
41301 lo = XEXP (addr, 1);
41303 else
41304 gcc_unreachable ();
41306 *p_hi = hi;
41307 *p_lo = lo;
41310 /* Return a string to fuse an addis instruction with a gpr load to the same
41311 register that we loaded up the addis instruction. The address that is used
41312 is the logical address that was formed during peephole2:
41313 (lo_sum (high) (low-part))
41315 Or the address is the TOC address that is wrapped before register allocation:
41316 (unspec [(addr) (toc-reg)] UNSPEC_FUSION_ADDIS)
41318 The code is complicated, so we call output_asm_insn directly, and just
41319 return "". */
41321 const char *
41322 emit_fusion_gpr_load (rtx target, rtx mem)
41324 rtx addis_value;
41325 rtx addr;
41326 rtx load_offset;
41327 const char *load_str = NULL;
41328 const char *mode_name = NULL;
41329 machine_mode mode;
41331 if (GET_CODE (mem) == ZERO_EXTEND)
41332 mem = XEXP (mem, 0);
41334 gcc_assert (REG_P (target) && MEM_P (mem));
41336 addr = XEXP (mem, 0);
41337 fusion_split_address (addr, &addis_value, &load_offset);
41339 /* Now emit the load instruction to the same register. */
41340 mode = GET_MODE (mem);
41341 switch (mode)
41343 case E_QImode:
41344 mode_name = "char";
41345 load_str = "lbz";
41346 break;
41348 case E_HImode:
41349 mode_name = "short";
41350 load_str = "lhz";
41351 break;
41353 case E_SImode:
41354 case E_SFmode:
41355 mode_name = (mode == SFmode) ? "float" : "int";
41356 load_str = "lwz";
41357 break;
41359 case E_DImode:
41360 case E_DFmode:
41361 gcc_assert (TARGET_POWERPC64);
41362 mode_name = (mode == DFmode) ? "double" : "long";
41363 load_str = "ld";
41364 break;
41366 default:
41367 fatal_insn ("Bad GPR fusion", gen_rtx_SET (target, mem));
41370 /* Emit the addis instruction. */
41371 emit_fusion_addis (target, addis_value, "gpr load fusion", mode_name);
41373 /* Emit the D-form load instruction. */
41374 emit_fusion_load_store (target, target, load_offset, load_str);
41376 return "";
41380 /* Return true if the peephole2 can combine a load/store involving a
41381 combination of an addis instruction and the memory operation. This was
41382 added to the ISA 3.0 (power9) hardware. */
41384 bool
41385 fusion_p9_p (rtx addis_reg, /* register set via addis. */
41386 rtx addis_value, /* addis value. */
41387 rtx dest, /* destination (memory or register). */
41388 rtx src) /* source (register or memory). */
41390 rtx addr, mem, offset;
41391 machine_mode mode = GET_MODE (src);
41393 /* Validate arguments. */
41394 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
41395 return false;
41397 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
41398 return false;
41400 /* Ignore extend operations that are part of the load. */
41401 if (GET_CODE (src) == FLOAT_EXTEND || GET_CODE (src) == ZERO_EXTEND)
41402 src = XEXP (src, 0);
41404 /* Test for memory<-register or register<-memory. */
41405 if (fpr_reg_operand (src, mode) || int_reg_operand (src, mode))
41407 if (!MEM_P (dest))
41408 return false;
41410 mem = dest;
41413 else if (MEM_P (src))
41415 if (!fpr_reg_operand (dest, mode) && !int_reg_operand (dest, mode))
41416 return false;
41418 mem = src;
41421 else
41422 return false;
41424 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
41425 if (GET_CODE (addr) == PLUS)
41427 if (!rtx_equal_p (addis_reg, XEXP (addr, 0)))
41428 return false;
41430 return satisfies_constraint_I (XEXP (addr, 1));
41433 else if (GET_CODE (addr) == LO_SUM)
41435 if (!rtx_equal_p (addis_reg, XEXP (addr, 0)))
41436 return false;
41438 offset = XEXP (addr, 1);
41439 if (TARGET_XCOFF || (TARGET_ELF && TARGET_POWERPC64))
41440 return small_toc_ref (offset, GET_MODE (offset));
41442 else if (TARGET_ELF && !TARGET_POWERPC64)
41443 return CONSTANT_P (offset);
41446 return false;
41449 /* During the peephole2 pass, adjust and expand the insns for an extended fusion
41450 load sequence.
41452 The operands are:
41453 operands[0] register set with addis
41454 operands[1] value set via addis
41455 operands[2] target register being loaded
41456 operands[3] D-form memory reference using operands[0].
41458 This is similar to the fusion introduced with power8, except it scales to
41459 both loads/stores and does not require the result register to be the same as
41460 the base register. At the moment, we only do this if register set with addis
41461 is dead. */
41463 void
41464 expand_fusion_p9_load (rtx *operands)
41466 rtx tmp_reg = operands[0];
41467 rtx addis_value = operands[1];
41468 rtx target = operands[2];
41469 rtx orig_mem = operands[3];
41470 rtx new_addr, new_mem, orig_addr, offset, set, clobber, insn;
41471 enum rtx_code plus_or_lo_sum;
41472 machine_mode target_mode = GET_MODE (target);
41473 machine_mode extend_mode = target_mode;
41474 machine_mode ptr_mode = Pmode;
41475 enum rtx_code extend = UNKNOWN;
41477 if (GET_CODE (orig_mem) == FLOAT_EXTEND || GET_CODE (orig_mem) == ZERO_EXTEND)
41479 extend = GET_CODE (orig_mem);
41480 orig_mem = XEXP (orig_mem, 0);
41481 target_mode = GET_MODE (orig_mem);
41484 gcc_assert (MEM_P (orig_mem));
41486 orig_addr = XEXP (orig_mem, 0);
41487 plus_or_lo_sum = GET_CODE (orig_addr);
41488 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
41490 offset = XEXP (orig_addr, 1);
41491 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
41492 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
41494 if (extend != UNKNOWN)
41495 new_mem = gen_rtx_fmt_e (extend, extend_mode, new_mem);
41497 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
41498 UNSPEC_FUSION_P9);
41500 set = gen_rtx_SET (target, new_mem);
41501 clobber = gen_rtx_CLOBBER (VOIDmode, tmp_reg);
41502 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber));
41503 emit_insn (insn);
41505 return;
41508 /* During the peephole2 pass, adjust and expand the insns for an extended fusion
41509 store sequence.
41511 The operands are:
41512 operands[0] register set with addis
41513 operands[1] value set via addis
41514 operands[2] target D-form memory being stored to
41515 operands[3] register being stored
41517 This is similar to the fusion introduced with power8, except it scales to
41518 both loads/stores and does not require the result register to be the same as
41519 the base register. At the moment, we only do this if register set with addis
41520 is dead. */
41522 void
41523 expand_fusion_p9_store (rtx *operands)
41525 rtx tmp_reg = operands[0];
41526 rtx addis_value = operands[1];
41527 rtx orig_mem = operands[2];
41528 rtx src = operands[3];
41529 rtx new_addr, new_mem, orig_addr, offset, set, clobber, insn, new_src;
41530 enum rtx_code plus_or_lo_sum;
41531 machine_mode target_mode = GET_MODE (orig_mem);
41532 machine_mode ptr_mode = Pmode;
41534 gcc_assert (MEM_P (orig_mem));
41536 orig_addr = XEXP (orig_mem, 0);
41537 plus_or_lo_sum = GET_CODE (orig_addr);
41538 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
41540 offset = XEXP (orig_addr, 1);
41541 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
41542 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
41544 new_src = gen_rtx_UNSPEC (target_mode, gen_rtvec (1, src),
41545 UNSPEC_FUSION_P9);
41547 set = gen_rtx_SET (new_mem, new_src);
41548 clobber = gen_rtx_CLOBBER (VOIDmode, tmp_reg);
41549 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber));
41550 emit_insn (insn);
41552 return;
41555 /* Return a string to fuse an addis instruction with a load using extended
41556 fusion. The address that is used is the logical address that was formed
41557 during peephole2: (lo_sum (high) (low-part))
41559 The code is complicated, so we call output_asm_insn directly, and just
41560 return "". */
41562 const char *
41563 emit_fusion_p9_load (rtx reg, rtx mem, rtx tmp_reg)
41565 machine_mode mode = GET_MODE (reg);
41566 rtx hi;
41567 rtx lo;
41568 rtx addr;
41569 const char *load_string;
41570 int r;
41572 if (GET_CODE (mem) == FLOAT_EXTEND || GET_CODE (mem) == ZERO_EXTEND)
41574 mem = XEXP (mem, 0);
41575 mode = GET_MODE (mem);
41578 if (GET_CODE (reg) == SUBREG)
41580 gcc_assert (SUBREG_BYTE (reg) == 0);
41581 reg = SUBREG_REG (reg);
41584 if (!REG_P (reg))
41585 fatal_insn ("emit_fusion_p9_load, bad reg #1", reg);
41587 r = REGNO (reg);
41588 if (FP_REGNO_P (r))
41590 if (mode == SFmode)
41591 load_string = "lfs";
41592 else if (mode == DFmode || mode == DImode)
41593 load_string = "lfd";
41594 else
41595 gcc_unreachable ();
41597 else if (ALTIVEC_REGNO_P (r) && TARGET_P9_DFORM_SCALAR)
41599 if (mode == SFmode)
41600 load_string = "lxssp";
41601 else if (mode == DFmode || mode == DImode)
41602 load_string = "lxsd";
41603 else
41604 gcc_unreachable ();
41606 else if (INT_REGNO_P (r))
41608 switch (mode)
41610 case E_QImode:
41611 load_string = "lbz";
41612 break;
41613 case E_HImode:
41614 load_string = "lhz";
41615 break;
41616 case E_SImode:
41617 case E_SFmode:
41618 load_string = "lwz";
41619 break;
41620 case E_DImode:
41621 case E_DFmode:
41622 if (!TARGET_POWERPC64)
41623 gcc_unreachable ();
41624 load_string = "ld";
41625 break;
41626 default:
41627 gcc_unreachable ();
41630 else
41631 fatal_insn ("emit_fusion_p9_load, bad reg #2", reg);
41633 if (!MEM_P (mem))
41634 fatal_insn ("emit_fusion_p9_load not MEM", mem);
41636 addr = XEXP (mem, 0);
41637 fusion_split_address (addr, &hi, &lo);
41639 /* Emit the addis instruction. */
41640 emit_fusion_addis (tmp_reg, hi, "power9 load fusion", GET_MODE_NAME (mode));
41642 /* Emit the D-form load instruction. */
41643 emit_fusion_load_store (reg, tmp_reg, lo, load_string);
41645 return "";
41648 /* Return a string to fuse an addis instruction with a store using extended
41649 fusion. The address that is used is the logical address that was formed
41650 during peephole2: (lo_sum (high) (low-part))
41652 The code is complicated, so we call output_asm_insn directly, and just
41653 return "". */
41655 const char *
41656 emit_fusion_p9_store (rtx mem, rtx reg, rtx tmp_reg)
41658 machine_mode mode = GET_MODE (reg);
41659 rtx hi;
41660 rtx lo;
41661 rtx addr;
41662 const char *store_string;
41663 int r;
41665 if (GET_CODE (reg) == SUBREG)
41667 gcc_assert (SUBREG_BYTE (reg) == 0);
41668 reg = SUBREG_REG (reg);
41671 if (!REG_P (reg))
41672 fatal_insn ("emit_fusion_p9_store, bad reg #1", reg);
41674 r = REGNO (reg);
41675 if (FP_REGNO_P (r))
41677 if (mode == SFmode)
41678 store_string = "stfs";
41679 else if (mode == DFmode)
41680 store_string = "stfd";
41681 else
41682 gcc_unreachable ();
41684 else if (ALTIVEC_REGNO_P (r) && TARGET_P9_DFORM_SCALAR)
41686 if (mode == SFmode)
41687 store_string = "stxssp";
41688 else if (mode == DFmode || mode == DImode)
41689 store_string = "stxsd";
41690 else
41691 gcc_unreachable ();
41693 else if (INT_REGNO_P (r))
41695 switch (mode)
41697 case E_QImode:
41698 store_string = "stb";
41699 break;
41700 case E_HImode:
41701 store_string = "sth";
41702 break;
41703 case E_SImode:
41704 case E_SFmode:
41705 store_string = "stw";
41706 break;
41707 case E_DImode:
41708 case E_DFmode:
41709 if (!TARGET_POWERPC64)
41710 gcc_unreachable ();
41711 store_string = "std";
41712 break;
41713 default:
41714 gcc_unreachable ();
41717 else
41718 fatal_insn ("emit_fusion_p9_store, bad reg #2", reg);
41720 if (!MEM_P (mem))
41721 fatal_insn ("emit_fusion_p9_store not MEM", mem);
41723 addr = XEXP (mem, 0);
41724 fusion_split_address (addr, &hi, &lo);
41726 /* Emit the addis instruction. */
41727 emit_fusion_addis (tmp_reg, hi, "power9 store fusion", GET_MODE_NAME (mode));
41729 /* Emit the D-form load instruction. */
41730 emit_fusion_load_store (reg, tmp_reg, lo, store_string);
41732 return "";
41736 /* Analyze vector computations and remove unnecessary doubleword
41737 swaps (xxswapdi instructions). This pass is performed only
41738 for little-endian VSX code generation.
41740 For this specific case, loads and stores of 4x32 and 2x64 vectors
41741 are inefficient. These are implemented using the lvx2dx and
41742 stvx2dx instructions, which invert the order of doublewords in
41743 a vector register. Thus the code generation inserts an xxswapdi
41744 after each such load, and prior to each such store. (For spill
41745 code after register assignment, an additional xxswapdi is inserted
41746 following each store in order to return a hard register to its
41747 unpermuted value.)
41749 The extra xxswapdi instructions reduce performance. This can be
41750 particularly bad for vectorized code. The purpose of this pass
41751 is to reduce the number of xxswapdi instructions required for
41752 correctness.
41754 The primary insight is that much code that operates on vectors
41755 does not care about the relative order of elements in a register,
41756 so long as the correct memory order is preserved. If we have
41757 a computation where all input values are provided by lvxd2x/xxswapdi
41758 sequences, all outputs are stored using xxswapdi/stvxd2x sequences,
41759 and all intermediate computations are pure SIMD (independent of
41760 element order), then all the xxswapdi's associated with the loads
41761 and stores may be removed.
41763 This pass uses some of the infrastructure and logical ideas from
41764 the "web" pass in web.c. We create maximal webs of computations
41765 fitting the description above using union-find. Each such web is
41766 then optimized by removing its unnecessary xxswapdi instructions.
41768 The pass is placed prior to global optimization so that we can
41769 perform the optimization in the safest and simplest way possible;
41770 that is, by replacing each xxswapdi insn with a register copy insn.
41771 Subsequent forward propagation will remove copies where possible.
41773 There are some operations sensitive to element order for which we
41774 can still allow the operation, provided we modify those operations.
41775 These include CONST_VECTORs, for which we must swap the first and
41776 second halves of the constant vector; and SUBREGs, for which we
41777 must adjust the byte offset to account for the swapped doublewords.
41778 A remaining opportunity would be non-immediate-form splats, for
41779 which we should adjust the selected lane of the input. We should
41780 also make code generation adjustments for sum-across operations,
41781 since this is a common vectorizer reduction.
41783 Because we run prior to the first split, we can see loads and stores
41784 here that match *vsx_le_perm_{load,store}_<mode>. These are vanilla
41785 vector loads and stores that have not yet been split into a permuting
41786 load/store and a swap. (One way this can happen is with a builtin
41787 call to vec_vsx_{ld,st}.) We can handle these as well, but rather
41788 than deleting a swap, we convert the load/store into a permuting
41789 load/store (which effectively removes the swap). */
41791 /* Notes on Permutes
41793 We do not currently handle computations that contain permutes. There
41794 is a general transformation that can be performed correctly, but it
41795 may introduce more expensive code than it replaces. To handle these
41796 would require a cost model to determine when to perform the optimization.
41797 This commentary records how this could be done if desired.
41799 The most general permute is something like this (example for V16QI):
41801 (vec_select:V16QI (vec_concat:V32QI (op1:V16QI) (op2:V16QI))
41802 (parallel [(const_int a0) (const_int a1)
41804 (const_int a14) (const_int a15)]))
41806 where a0,...,a15 are in [0,31] and select elements from op1 and op2
41807 to produce in the result.
41809 Regardless of mode, we can convert the PARALLEL to a mask of 16
41810 byte-element selectors. Let's call this M, with M[i] representing
41811 the ith byte-element selector value. Then if we swap doublewords
41812 throughout the computation, we can get correct behavior by replacing
41813 M with M' as follows:
41815 M'[i] = { (M[i]+8)%16 : M[i] in [0,15]
41816 { ((M[i]+8)%16)+16 : M[i] in [16,31]
41818 This seems promising at first, since we are just replacing one mask
41819 with another. But certain masks are preferable to others. If M
41820 is a mask that matches a vmrghh pattern, for example, M' certainly
41821 will not. Instead of a single vmrghh, we would generate a load of
41822 M' and a vperm. So we would need to know how many xxswapd's we can
41823 remove as a result of this transformation to determine if it's
41824 profitable; and preferably the logic would need to be aware of all
41825 the special preferable masks.
41827 Another form of permute is an UNSPEC_VPERM, in which the mask is
41828 already in a register. In some cases, this mask may be a constant
41829 that we can discover with ud-chains, in which case the above
41830 transformation is ok. However, the common usage here is for the
41831 mask to be produced by an UNSPEC_LVSL, in which case the mask
41832 cannot be known at compile time. In such a case we would have to
41833 generate several instructions to compute M' as above at run time,
41834 and a cost model is needed again.
41836 However, when the mask M for an UNSPEC_VPERM is loaded from the
41837 constant pool, we can replace M with M' as above at no cost
41838 beyond adding a constant pool entry. */
41840 /* This is based on the union-find logic in web.c. web_entry_base is
41841 defined in df.h. */
41842 class swap_web_entry : public web_entry_base
41844 public:
41845 /* Pointer to the insn. */
41846 rtx_insn *insn;
41847 /* Set if insn contains a mention of a vector register. All other
41848 fields are undefined if this field is unset. */
41849 unsigned int is_relevant : 1;
41850 /* Set if insn is a load. */
41851 unsigned int is_load : 1;
41852 /* Set if insn is a store. */
41853 unsigned int is_store : 1;
41854 /* Set if insn is a doubleword swap. This can either be a register swap
41855 or a permuting load or store (test is_load and is_store for this). */
41856 unsigned int is_swap : 1;
41857 /* Set if the insn has a live-in use of a parameter register. */
41858 unsigned int is_live_in : 1;
41859 /* Set if the insn has a live-out def of a return register. */
41860 unsigned int is_live_out : 1;
41861 /* Set if the insn contains a subreg reference of a vector register. */
41862 unsigned int contains_subreg : 1;
41863 /* Set if the insn contains a 128-bit integer operand. */
41864 unsigned int is_128_int : 1;
41865 /* Set if this is a call-insn. */
41866 unsigned int is_call : 1;
41867 /* Set if this insn does not perform a vector operation for which
41868 element order matters, or if we know how to fix it up if it does.
41869 Undefined if is_swap is set. */
41870 unsigned int is_swappable : 1;
41871 /* A nonzero value indicates what kind of special handling for this
41872 insn is required if doublewords are swapped. Undefined if
41873 is_swappable is not set. */
41874 unsigned int special_handling : 4;
41875 /* Set if the web represented by this entry cannot be optimized. */
41876 unsigned int web_not_optimizable : 1;
41877 /* Set if this insn should be deleted. */
41878 unsigned int will_delete : 1;
41881 enum special_handling_values {
41882 SH_NONE = 0,
41883 SH_CONST_VECTOR,
41884 SH_SUBREG,
41885 SH_NOSWAP_LD,
41886 SH_NOSWAP_ST,
41887 SH_EXTRACT,
41888 SH_SPLAT,
41889 SH_XXPERMDI,
41890 SH_CONCAT,
41891 SH_VPERM
41894 /* Union INSN with all insns containing definitions that reach USE.
41895 Detect whether USE is live-in to the current function. */
41896 static void
41897 union_defs (swap_web_entry *insn_entry, rtx insn, df_ref use)
41899 struct df_link *link = DF_REF_CHAIN (use);
41901 if (!link)
41902 insn_entry[INSN_UID (insn)].is_live_in = 1;
41904 while (link)
41906 if (DF_REF_IS_ARTIFICIAL (link->ref))
41907 insn_entry[INSN_UID (insn)].is_live_in = 1;
41909 if (DF_REF_INSN_INFO (link->ref))
41911 rtx def_insn = DF_REF_INSN (link->ref);
41912 (void)unionfind_union (insn_entry + INSN_UID (insn),
41913 insn_entry + INSN_UID (def_insn));
41916 link = link->next;
41920 /* Union INSN with all insns containing uses reached from DEF.
41921 Detect whether DEF is live-out from the current function. */
41922 static void
41923 union_uses (swap_web_entry *insn_entry, rtx insn, df_ref def)
41925 struct df_link *link = DF_REF_CHAIN (def);
41927 if (!link)
41928 insn_entry[INSN_UID (insn)].is_live_out = 1;
41930 while (link)
41932 /* This could be an eh use or some other artificial use;
41933 we treat these all the same (killing the optimization). */
41934 if (DF_REF_IS_ARTIFICIAL (link->ref))
41935 insn_entry[INSN_UID (insn)].is_live_out = 1;
41937 if (DF_REF_INSN_INFO (link->ref))
41939 rtx use_insn = DF_REF_INSN (link->ref);
41940 (void)unionfind_union (insn_entry + INSN_UID (insn),
41941 insn_entry + INSN_UID (use_insn));
41944 link = link->next;
41948 /* Return 1 iff INSN is a load insn, including permuting loads that
41949 represent an lvxd2x instruction; else return 0. */
41950 static unsigned int
41951 insn_is_load_p (rtx insn)
41953 rtx body = PATTERN (insn);
41955 if (GET_CODE (body) == SET)
41957 if (GET_CODE (SET_SRC (body)) == MEM)
41958 return 1;
41960 if (GET_CODE (SET_SRC (body)) == VEC_SELECT
41961 && GET_CODE (XEXP (SET_SRC (body), 0)) == MEM)
41962 return 1;
41964 return 0;
41967 if (GET_CODE (body) != PARALLEL)
41968 return 0;
41970 rtx set = XVECEXP (body, 0, 0);
41972 if (GET_CODE (set) == SET && GET_CODE (SET_SRC (set)) == MEM)
41973 return 1;
41975 return 0;
41978 /* Return 1 iff INSN is a store insn, including permuting stores that
41979 represent an stvxd2x instruction; else return 0. */
41980 static unsigned int
41981 insn_is_store_p (rtx insn)
41983 rtx body = PATTERN (insn);
41984 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == MEM)
41985 return 1;
41986 if (GET_CODE (body) != PARALLEL)
41987 return 0;
41988 rtx set = XVECEXP (body, 0, 0);
41989 if (GET_CODE (set) == SET && GET_CODE (SET_DEST (set)) == MEM)
41990 return 1;
41991 return 0;
41994 /* Return 1 iff INSN swaps doublewords. This may be a reg-reg swap,
41995 a permuting load, or a permuting store. */
41996 static unsigned int
41997 insn_is_swap_p (rtx insn)
41999 rtx body = PATTERN (insn);
42000 if (GET_CODE (body) != SET)
42001 return 0;
42002 rtx rhs = SET_SRC (body);
42003 if (GET_CODE (rhs) != VEC_SELECT)
42004 return 0;
42005 rtx parallel = XEXP (rhs, 1);
42006 if (GET_CODE (parallel) != PARALLEL)
42007 return 0;
42008 unsigned int len = XVECLEN (parallel, 0);
42009 if (len != 2 && len != 4 && len != 8 && len != 16)
42010 return 0;
42011 for (unsigned int i = 0; i < len / 2; ++i)
42013 rtx op = XVECEXP (parallel, 0, i);
42014 if (GET_CODE (op) != CONST_INT || INTVAL (op) != len / 2 + i)
42015 return 0;
42017 for (unsigned int i = len / 2; i < len; ++i)
42019 rtx op = XVECEXP (parallel, 0, i);
42020 if (GET_CODE (op) != CONST_INT || INTVAL (op) != i - len / 2)
42021 return 0;
42023 return 1;
42026 /* Return TRUE if insn is a swap fed by a load from the constant pool. */
42027 static bool
42028 const_load_sequence_p (swap_web_entry *insn_entry, rtx insn)
42030 unsigned uid = INSN_UID (insn);
42031 if (!insn_entry[uid].is_swap || insn_entry[uid].is_load)
42032 return false;
42034 /* Find the unique use in the swap and locate its def. If the def
42035 isn't unique, punt. */
42036 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42037 df_ref use;
42038 FOR_EACH_INSN_INFO_USE (use, insn_info)
42040 struct df_link *def_link = DF_REF_CHAIN (use);
42041 if (!def_link || def_link->next)
42042 return false;
42044 rtx def_insn = DF_REF_INSN (def_link->ref);
42045 unsigned uid2 = INSN_UID (def_insn);
42046 if (!insn_entry[uid2].is_load || !insn_entry[uid2].is_swap)
42047 return false;
42049 rtx body = PATTERN (def_insn);
42050 if (GET_CODE (body) != SET
42051 || GET_CODE (SET_SRC (body)) != VEC_SELECT
42052 || GET_CODE (XEXP (SET_SRC (body), 0)) != MEM)
42053 return false;
42055 rtx mem = XEXP (SET_SRC (body), 0);
42056 rtx base_reg = XEXP (mem, 0);
42058 df_ref base_use;
42059 insn_info = DF_INSN_INFO_GET (def_insn);
42060 FOR_EACH_INSN_INFO_USE (base_use, insn_info)
42062 if (!rtx_equal_p (DF_REF_REG (base_use), base_reg))
42063 continue;
42065 struct df_link *base_def_link = DF_REF_CHAIN (base_use);
42066 if (!base_def_link || base_def_link->next)
42067 return false;
42069 rtx tocrel_insn = DF_REF_INSN (base_def_link->ref);
42070 rtx tocrel_body = PATTERN (tocrel_insn);
42071 rtx base, offset;
42072 if (GET_CODE (tocrel_body) != SET)
42073 return false;
42074 /* There is an extra level of indirection for small/large
42075 code models. */
42076 rtx tocrel_expr = SET_SRC (tocrel_body);
42077 if (GET_CODE (tocrel_expr) == MEM)
42078 tocrel_expr = XEXP (tocrel_expr, 0);
42079 if (!toc_relative_expr_p (tocrel_expr, false))
42080 return false;
42081 split_const (XVECEXP (tocrel_base, 0, 0), &base, &offset);
42082 if (GET_CODE (base) != SYMBOL_REF || !CONSTANT_POOL_ADDRESS_P (base))
42083 return false;
42086 return true;
42089 /* Return TRUE iff OP matches a V2DF reduction pattern. See the
42090 definition of vsx_reduc_<VEC_reduc_name>_v2df in vsx.md. */
42091 static bool
42092 v2df_reduction_p (rtx op)
42094 if (GET_MODE (op) != V2DFmode)
42095 return false;
42097 enum rtx_code code = GET_CODE (op);
42098 if (code != PLUS && code != SMIN && code != SMAX)
42099 return false;
42101 rtx concat = XEXP (op, 0);
42102 if (GET_CODE (concat) != VEC_CONCAT)
42103 return false;
42105 rtx select0 = XEXP (concat, 0);
42106 rtx select1 = XEXP (concat, 1);
42107 if (GET_CODE (select0) != VEC_SELECT || GET_CODE (select1) != VEC_SELECT)
42108 return false;
42110 rtx reg0 = XEXP (select0, 0);
42111 rtx reg1 = XEXP (select1, 0);
42112 if (!rtx_equal_p (reg0, reg1) || !REG_P (reg0))
42113 return false;
42115 rtx parallel0 = XEXP (select0, 1);
42116 rtx parallel1 = XEXP (select1, 1);
42117 if (GET_CODE (parallel0) != PARALLEL || GET_CODE (parallel1) != PARALLEL)
42118 return false;
42120 if (!rtx_equal_p (XVECEXP (parallel0, 0, 0), const1_rtx)
42121 || !rtx_equal_p (XVECEXP (parallel1, 0, 0), const0_rtx))
42122 return false;
42124 return true;
42127 /* Return 1 iff OP is an operand that will not be affected by having
42128 vector doublewords swapped in memory. */
42129 static unsigned int
42130 rtx_is_swappable_p (rtx op, unsigned int *special)
42132 enum rtx_code code = GET_CODE (op);
42133 int i, j;
42134 rtx parallel;
42136 switch (code)
42138 case LABEL_REF:
42139 case SYMBOL_REF:
42140 case CLOBBER:
42141 case REG:
42142 return 1;
42144 case VEC_CONCAT:
42145 case ASM_INPUT:
42146 case ASM_OPERANDS:
42147 return 0;
42149 case CONST_VECTOR:
42151 *special = SH_CONST_VECTOR;
42152 return 1;
42155 case VEC_DUPLICATE:
42156 /* Opportunity: If XEXP (op, 0) has the same mode as the result,
42157 and XEXP (op, 1) is a PARALLEL with a single QImode const int,
42158 it represents a vector splat for which we can do special
42159 handling. */
42160 if (GET_CODE (XEXP (op, 0)) == CONST_INT)
42161 return 1;
42162 else if (REG_P (XEXP (op, 0))
42163 && GET_MODE_INNER (GET_MODE (op)) == GET_MODE (XEXP (op, 0)))
42164 /* This catches V2DF and V2DI splat, at a minimum. */
42165 return 1;
42166 else if (GET_CODE (XEXP (op, 0)) == TRUNCATE
42167 && REG_P (XEXP (XEXP (op, 0), 0))
42168 && GET_MODE_INNER (GET_MODE (op)) == GET_MODE (XEXP (op, 0)))
42169 /* This catches splat of a truncated value. */
42170 return 1;
42171 else if (GET_CODE (XEXP (op, 0)) == VEC_SELECT)
42172 /* If the duplicated item is from a select, defer to the select
42173 processing to see if we can change the lane for the splat. */
42174 return rtx_is_swappable_p (XEXP (op, 0), special);
42175 else
42176 return 0;
42178 case VEC_SELECT:
42179 /* A vec_extract operation is ok if we change the lane. */
42180 if (GET_CODE (XEXP (op, 0)) == REG
42181 && GET_MODE_INNER (GET_MODE (XEXP (op, 0))) == GET_MODE (op)
42182 && GET_CODE ((parallel = XEXP (op, 1))) == PARALLEL
42183 && XVECLEN (parallel, 0) == 1
42184 && GET_CODE (XVECEXP (parallel, 0, 0)) == CONST_INT)
42186 *special = SH_EXTRACT;
42187 return 1;
42189 /* An XXPERMDI is ok if we adjust the lanes. Note that if the
42190 XXPERMDI is a swap operation, it will be identified by
42191 insn_is_swap_p and therefore we won't get here. */
42192 else if (GET_CODE (XEXP (op, 0)) == VEC_CONCAT
42193 && (GET_MODE (XEXP (op, 0)) == V4DFmode
42194 || GET_MODE (XEXP (op, 0)) == V4DImode)
42195 && GET_CODE ((parallel = XEXP (op, 1))) == PARALLEL
42196 && XVECLEN (parallel, 0) == 2
42197 && GET_CODE (XVECEXP (parallel, 0, 0)) == CONST_INT
42198 && GET_CODE (XVECEXP (parallel, 0, 1)) == CONST_INT)
42200 *special = SH_XXPERMDI;
42201 return 1;
42203 else if (v2df_reduction_p (op))
42204 return 1;
42205 else
42206 return 0;
42208 case UNSPEC:
42210 /* Various operations are unsafe for this optimization, at least
42211 without significant additional work. Permutes are obviously
42212 problematic, as both the permute control vector and the ordering
42213 of the target values are invalidated by doubleword swapping.
42214 Vector pack and unpack modify the number of vector lanes.
42215 Merge-high/low will not operate correctly on swapped operands.
42216 Vector shifts across element boundaries are clearly uncool,
42217 as are vector select and concatenate operations. Vector
42218 sum-across instructions define one operand with a specific
42219 order-dependent element, so additional fixup code would be
42220 needed to make those work. Vector set and non-immediate-form
42221 vector splat are element-order sensitive. A few of these
42222 cases might be workable with special handling if required.
42223 Adding cost modeling would be appropriate in some cases. */
42224 int val = XINT (op, 1);
42225 switch (val)
42227 default:
42228 break;
42229 case UNSPEC_VMRGH_DIRECT:
42230 case UNSPEC_VMRGL_DIRECT:
42231 case UNSPEC_VPACK_SIGN_SIGN_SAT:
42232 case UNSPEC_VPACK_SIGN_UNS_SAT:
42233 case UNSPEC_VPACK_UNS_UNS_MOD:
42234 case UNSPEC_VPACK_UNS_UNS_MOD_DIRECT:
42235 case UNSPEC_VPACK_UNS_UNS_SAT:
42236 case UNSPEC_VPERM:
42237 case UNSPEC_VPERM_UNS:
42238 case UNSPEC_VPERMHI:
42239 case UNSPEC_VPERMSI:
42240 case UNSPEC_VPKPX:
42241 case UNSPEC_VSLDOI:
42242 case UNSPEC_VSLO:
42243 case UNSPEC_VSRO:
42244 case UNSPEC_VSUM2SWS:
42245 case UNSPEC_VSUM4S:
42246 case UNSPEC_VSUM4UBS:
42247 case UNSPEC_VSUMSWS:
42248 case UNSPEC_VSUMSWS_DIRECT:
42249 case UNSPEC_VSX_CONCAT:
42250 case UNSPEC_VSX_SET:
42251 case UNSPEC_VSX_SLDWI:
42252 case UNSPEC_VUNPACK_HI_SIGN:
42253 case UNSPEC_VUNPACK_HI_SIGN_DIRECT:
42254 case UNSPEC_VUNPACK_LO_SIGN:
42255 case UNSPEC_VUNPACK_LO_SIGN_DIRECT:
42256 case UNSPEC_VUPKHPX:
42257 case UNSPEC_VUPKHS_V4SF:
42258 case UNSPEC_VUPKHU_V4SF:
42259 case UNSPEC_VUPKLPX:
42260 case UNSPEC_VUPKLS_V4SF:
42261 case UNSPEC_VUPKLU_V4SF:
42262 case UNSPEC_VSX_CVDPSPN:
42263 case UNSPEC_VSX_CVSPDP:
42264 case UNSPEC_VSX_CVSPDPN:
42265 case UNSPEC_VSX_EXTRACT:
42266 case UNSPEC_VSX_VSLO:
42267 case UNSPEC_VSX_VEC_INIT:
42268 return 0;
42269 case UNSPEC_VSPLT_DIRECT:
42270 case UNSPEC_VSX_XXSPLTD:
42271 *special = SH_SPLAT;
42272 return 1;
42273 case UNSPEC_REDUC_PLUS:
42274 case UNSPEC_REDUC:
42275 return 1;
42279 default:
42280 break;
42283 const char *fmt = GET_RTX_FORMAT (code);
42284 int ok = 1;
42286 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
42287 if (fmt[i] == 'e' || fmt[i] == 'u')
42289 unsigned int special_op = SH_NONE;
42290 ok &= rtx_is_swappable_p (XEXP (op, i), &special_op);
42291 if (special_op == SH_NONE)
42292 continue;
42293 /* Ensure we never have two kinds of special handling
42294 for the same insn. */
42295 if (*special != SH_NONE && *special != special_op)
42296 return 0;
42297 *special = special_op;
42299 else if (fmt[i] == 'E')
42300 for (j = 0; j < XVECLEN (op, i); ++j)
42302 unsigned int special_op = SH_NONE;
42303 ok &= rtx_is_swappable_p (XVECEXP (op, i, j), &special_op);
42304 if (special_op == SH_NONE)
42305 continue;
42306 /* Ensure we never have two kinds of special handling
42307 for the same insn. */
42308 if (*special != SH_NONE && *special != special_op)
42309 return 0;
42310 *special = special_op;
42313 return ok;
42316 /* Return 1 iff INSN is an operand that will not be affected by
42317 having vector doublewords swapped in memory (in which case
42318 *SPECIAL is unchanged), or that can be modified to be correct
42319 if vector doublewords are swapped in memory (in which case
42320 *SPECIAL is changed to a value indicating how). */
42321 static unsigned int
42322 insn_is_swappable_p (swap_web_entry *insn_entry, rtx insn,
42323 unsigned int *special)
42325 /* Calls are always bad. */
42326 if (GET_CODE (insn) == CALL_INSN)
42327 return 0;
42329 /* Loads and stores seen here are not permuting, but we can still
42330 fix them up by converting them to permuting ones. Exceptions:
42331 UNSPEC_LVE, UNSPEC_LVX, and UNSPEC_STVX, which have a PARALLEL
42332 body instead of a SET; and UNSPEC_STVE, which has an UNSPEC
42333 for the SET source. Also we must now make an exception for lvx
42334 and stvx when they are not in the UNSPEC_LVX/STVX form (with the
42335 explicit "& -16") since this leads to unrecognizable insns. */
42336 rtx body = PATTERN (insn);
42337 int i = INSN_UID (insn);
42339 if (insn_entry[i].is_load)
42341 if (GET_CODE (body) == SET)
42343 rtx rhs = SET_SRC (body);
42344 /* Even without a swap, the RHS might be a vec_select for, say,
42345 a byte-reversing load. */
42346 if (GET_CODE (rhs) != MEM)
42347 return 0;
42348 if (GET_CODE (XEXP (rhs, 0)) == AND)
42349 return 0;
42351 *special = SH_NOSWAP_LD;
42352 return 1;
42354 else
42355 return 0;
42358 if (insn_entry[i].is_store)
42360 if (GET_CODE (body) == SET
42361 && GET_CODE (SET_SRC (body)) != UNSPEC)
42363 rtx lhs = SET_DEST (body);
42364 /* Even without a swap, the LHS might be a vec_select for, say,
42365 a byte-reversing store. */
42366 if (GET_CODE (lhs) != MEM)
42367 return 0;
42368 if (GET_CODE (XEXP (lhs, 0)) == AND)
42369 return 0;
42371 *special = SH_NOSWAP_ST;
42372 return 1;
42374 else
42375 return 0;
42378 /* A convert to single precision can be left as is provided that
42379 all of its uses are in xxspltw instructions that splat BE element
42380 zero. */
42381 if (GET_CODE (body) == SET
42382 && GET_CODE (SET_SRC (body)) == UNSPEC
42383 && XINT (SET_SRC (body), 1) == UNSPEC_VSX_CVDPSPN)
42385 df_ref def;
42386 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42388 FOR_EACH_INSN_INFO_DEF (def, insn_info)
42390 struct df_link *link = DF_REF_CHAIN (def);
42391 if (!link)
42392 return 0;
42394 for (; link; link = link->next) {
42395 rtx use_insn = DF_REF_INSN (link->ref);
42396 rtx use_body = PATTERN (use_insn);
42397 if (GET_CODE (use_body) != SET
42398 || GET_CODE (SET_SRC (use_body)) != UNSPEC
42399 || XINT (SET_SRC (use_body), 1) != UNSPEC_VSX_XXSPLTW
42400 || XVECEXP (SET_SRC (use_body), 0, 1) != const0_rtx)
42401 return 0;
42405 return 1;
42408 /* A concatenation of two doublewords is ok if we reverse the
42409 order of the inputs. */
42410 if (GET_CODE (body) == SET
42411 && GET_CODE (SET_SRC (body)) == VEC_CONCAT
42412 && (GET_MODE (SET_SRC (body)) == V2DFmode
42413 || GET_MODE (SET_SRC (body)) == V2DImode))
42415 *special = SH_CONCAT;
42416 return 1;
42419 /* V2DF reductions are always swappable. */
42420 if (GET_CODE (body) == PARALLEL)
42422 rtx expr = XVECEXP (body, 0, 0);
42423 if (GET_CODE (expr) == SET
42424 && v2df_reduction_p (SET_SRC (expr)))
42425 return 1;
42428 /* An UNSPEC_VPERM is ok if the mask operand is loaded from the
42429 constant pool. */
42430 if (GET_CODE (body) == SET
42431 && GET_CODE (SET_SRC (body)) == UNSPEC
42432 && XINT (SET_SRC (body), 1) == UNSPEC_VPERM
42433 && XVECLEN (SET_SRC (body), 0) == 3
42434 && GET_CODE (XVECEXP (SET_SRC (body), 0, 2)) == REG)
42436 rtx mask_reg = XVECEXP (SET_SRC (body), 0, 2);
42437 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42438 df_ref use;
42439 FOR_EACH_INSN_INFO_USE (use, insn_info)
42440 if (rtx_equal_p (DF_REF_REG (use), mask_reg))
42442 struct df_link *def_link = DF_REF_CHAIN (use);
42443 /* Punt if multiple definitions for this reg. */
42444 if (def_link && !def_link->next &&
42445 const_load_sequence_p (insn_entry,
42446 DF_REF_INSN (def_link->ref)))
42448 *special = SH_VPERM;
42449 return 1;
42454 /* Otherwise check the operands for vector lane violations. */
42455 return rtx_is_swappable_p (body, special);
42458 enum chain_purpose { FOR_LOADS, FOR_STORES };
42460 /* Return true if the UD or DU chain headed by LINK is non-empty,
42461 and every entry on the chain references an insn that is a
42462 register swap. Furthermore, if PURPOSE is FOR_LOADS, each such
42463 register swap must have only permuting loads as reaching defs.
42464 If PURPOSE is FOR_STORES, each such register swap must have only
42465 register swaps or permuting stores as reached uses. */
42466 static bool
42467 chain_contains_only_swaps (swap_web_entry *insn_entry, struct df_link *link,
42468 enum chain_purpose purpose)
42470 if (!link)
42471 return false;
42473 for (; link; link = link->next)
42475 if (!ALTIVEC_OR_VSX_VECTOR_MODE (GET_MODE (DF_REF_REG (link->ref))))
42476 continue;
42478 if (DF_REF_IS_ARTIFICIAL (link->ref))
42479 return false;
42481 rtx reached_insn = DF_REF_INSN (link->ref);
42482 unsigned uid = INSN_UID (reached_insn);
42483 struct df_insn_info *insn_info = DF_INSN_INFO_GET (reached_insn);
42485 if (!insn_entry[uid].is_swap || insn_entry[uid].is_load
42486 || insn_entry[uid].is_store)
42487 return false;
42489 if (purpose == FOR_LOADS)
42491 df_ref use;
42492 FOR_EACH_INSN_INFO_USE (use, insn_info)
42494 struct df_link *swap_link = DF_REF_CHAIN (use);
42496 while (swap_link)
42498 if (DF_REF_IS_ARTIFICIAL (link->ref))
42499 return false;
42501 rtx swap_def_insn = DF_REF_INSN (swap_link->ref);
42502 unsigned uid2 = INSN_UID (swap_def_insn);
42504 /* Only permuting loads are allowed. */
42505 if (!insn_entry[uid2].is_swap || !insn_entry[uid2].is_load)
42506 return false;
42508 swap_link = swap_link->next;
42512 else if (purpose == FOR_STORES)
42514 df_ref def;
42515 FOR_EACH_INSN_INFO_DEF (def, insn_info)
42517 struct df_link *swap_link = DF_REF_CHAIN (def);
42519 while (swap_link)
42521 if (DF_REF_IS_ARTIFICIAL (link->ref))
42522 return false;
42524 rtx swap_use_insn = DF_REF_INSN (swap_link->ref);
42525 unsigned uid2 = INSN_UID (swap_use_insn);
42527 /* Permuting stores or register swaps are allowed. */
42528 if (!insn_entry[uid2].is_swap || insn_entry[uid2].is_load)
42529 return false;
42531 swap_link = swap_link->next;
42537 return true;
42540 /* Mark the xxswapdi instructions associated with permuting loads and
42541 stores for removal. Note that we only flag them for deletion here,
42542 as there is a possibility of a swap being reached from multiple
42543 loads, etc. */
42544 static void
42545 mark_swaps_for_removal (swap_web_entry *insn_entry, unsigned int i)
42547 rtx insn = insn_entry[i].insn;
42548 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42550 if (insn_entry[i].is_load)
42552 df_ref def;
42553 FOR_EACH_INSN_INFO_DEF (def, insn_info)
42555 struct df_link *link = DF_REF_CHAIN (def);
42557 /* We know by now that these are swaps, so we can delete
42558 them confidently. */
42559 while (link)
42561 rtx use_insn = DF_REF_INSN (link->ref);
42562 insn_entry[INSN_UID (use_insn)].will_delete = 1;
42563 link = link->next;
42567 else if (insn_entry[i].is_store)
42569 df_ref use;
42570 FOR_EACH_INSN_INFO_USE (use, insn_info)
42572 /* Ignore uses for addressability. */
42573 machine_mode mode = GET_MODE (DF_REF_REG (use));
42574 if (!ALTIVEC_OR_VSX_VECTOR_MODE (mode))
42575 continue;
42577 struct df_link *link = DF_REF_CHAIN (use);
42579 /* We know by now that these are swaps, so we can delete
42580 them confidently. */
42581 while (link)
42583 rtx def_insn = DF_REF_INSN (link->ref);
42584 insn_entry[INSN_UID (def_insn)].will_delete = 1;
42585 link = link->next;
42591 /* OP is either a CONST_VECTOR or an expression containing one.
42592 Swap the first half of the vector with the second in the first
42593 case. Recurse to find it in the second. */
42594 static void
42595 swap_const_vector_halves (rtx op)
42597 int i;
42598 enum rtx_code code = GET_CODE (op);
42599 if (GET_CODE (op) == CONST_VECTOR)
42601 int half_units = GET_MODE_NUNITS (GET_MODE (op)) / 2;
42602 for (i = 0; i < half_units; ++i)
42604 rtx temp = CONST_VECTOR_ELT (op, i);
42605 CONST_VECTOR_ELT (op, i) = CONST_VECTOR_ELT (op, i + half_units);
42606 CONST_VECTOR_ELT (op, i + half_units) = temp;
42609 else
42611 int j;
42612 const char *fmt = GET_RTX_FORMAT (code);
42613 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
42614 if (fmt[i] == 'e' || fmt[i] == 'u')
42615 swap_const_vector_halves (XEXP (op, i));
42616 else if (fmt[i] == 'E')
42617 for (j = 0; j < XVECLEN (op, i); ++j)
42618 swap_const_vector_halves (XVECEXP (op, i, j));
42622 /* Find all subregs of a vector expression that perform a narrowing,
42623 and adjust the subreg index to account for doubleword swapping. */
42624 static void
42625 adjust_subreg_index (rtx op)
42627 enum rtx_code code = GET_CODE (op);
42628 if (code == SUBREG
42629 && (GET_MODE_SIZE (GET_MODE (op))
42630 < GET_MODE_SIZE (GET_MODE (XEXP (op, 0)))))
42632 unsigned int index = SUBREG_BYTE (op);
42633 if (index < 8)
42634 index += 8;
42635 else
42636 index -= 8;
42637 SUBREG_BYTE (op) = index;
42640 const char *fmt = GET_RTX_FORMAT (code);
42641 int i,j;
42642 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
42643 if (fmt[i] == 'e' || fmt[i] == 'u')
42644 adjust_subreg_index (XEXP (op, i));
42645 else if (fmt[i] == 'E')
42646 for (j = 0; j < XVECLEN (op, i); ++j)
42647 adjust_subreg_index (XVECEXP (op, i, j));
42650 /* Convert the non-permuting load INSN to a permuting one. */
42651 static void
42652 permute_load (rtx_insn *insn)
42654 rtx body = PATTERN (insn);
42655 rtx mem_op = SET_SRC (body);
42656 rtx tgt_reg = SET_DEST (body);
42657 machine_mode mode = GET_MODE (tgt_reg);
42658 int n_elts = GET_MODE_NUNITS (mode);
42659 int half_elts = n_elts / 2;
42660 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
42661 int i, j;
42662 for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
42663 XVECEXP (par, 0, i) = GEN_INT (j);
42664 for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
42665 XVECEXP (par, 0, i) = GEN_INT (j);
42666 rtx sel = gen_rtx_VEC_SELECT (mode, mem_op, par);
42667 SET_SRC (body) = sel;
42668 INSN_CODE (insn) = -1; /* Force re-recognition. */
42669 df_insn_rescan (insn);
42671 if (dump_file)
42672 fprintf (dump_file, "Replacing load %d with permuted load\n",
42673 INSN_UID (insn));
42676 /* Convert the non-permuting store INSN to a permuting one. */
42677 static void
42678 permute_store (rtx_insn *insn)
42680 rtx body = PATTERN (insn);
42681 rtx src_reg = SET_SRC (body);
42682 machine_mode mode = GET_MODE (src_reg);
42683 int n_elts = GET_MODE_NUNITS (mode);
42684 int half_elts = n_elts / 2;
42685 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
42686 int i, j;
42687 for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
42688 XVECEXP (par, 0, i) = GEN_INT (j);
42689 for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
42690 XVECEXP (par, 0, i) = GEN_INT (j);
42691 rtx sel = gen_rtx_VEC_SELECT (mode, src_reg, par);
42692 SET_SRC (body) = sel;
42693 INSN_CODE (insn) = -1; /* Force re-recognition. */
42694 df_insn_rescan (insn);
42696 if (dump_file)
42697 fprintf (dump_file, "Replacing store %d with permuted store\n",
42698 INSN_UID (insn));
42701 /* Given OP that contains a vector extract operation, adjust the index
42702 of the extracted lane to account for the doubleword swap. */
42703 static void
42704 adjust_extract (rtx_insn *insn)
42706 rtx pattern = PATTERN (insn);
42707 if (GET_CODE (pattern) == PARALLEL)
42708 pattern = XVECEXP (pattern, 0, 0);
42709 rtx src = SET_SRC (pattern);
42710 /* The vec_select may be wrapped in a vec_duplicate for a splat, so
42711 account for that. */
42712 rtx sel = GET_CODE (src) == VEC_DUPLICATE ? XEXP (src, 0) : src;
42713 rtx par = XEXP (sel, 1);
42714 int half_elts = GET_MODE_NUNITS (GET_MODE (XEXP (sel, 0))) >> 1;
42715 int lane = INTVAL (XVECEXP (par, 0, 0));
42716 lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
42717 XVECEXP (par, 0, 0) = GEN_INT (lane);
42718 INSN_CODE (insn) = -1; /* Force re-recognition. */
42719 df_insn_rescan (insn);
42721 if (dump_file)
42722 fprintf (dump_file, "Changing lane for extract %d\n", INSN_UID (insn));
42725 /* Given OP that contains a vector direct-splat operation, adjust the index
42726 of the source lane to account for the doubleword swap. */
42727 static void
42728 adjust_splat (rtx_insn *insn)
42730 rtx body = PATTERN (insn);
42731 rtx unspec = XEXP (body, 1);
42732 int half_elts = GET_MODE_NUNITS (GET_MODE (unspec)) >> 1;
42733 int lane = INTVAL (XVECEXP (unspec, 0, 1));
42734 lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
42735 XVECEXP (unspec, 0, 1) = GEN_INT (lane);
42736 INSN_CODE (insn) = -1; /* Force re-recognition. */
42737 df_insn_rescan (insn);
42739 if (dump_file)
42740 fprintf (dump_file, "Changing lane for splat %d\n", INSN_UID (insn));
42743 /* Given OP that contains an XXPERMDI operation (that is not a doubleword
42744 swap), reverse the order of the source operands and adjust the indices
42745 of the source lanes to account for doubleword reversal. */
42746 static void
42747 adjust_xxpermdi (rtx_insn *insn)
42749 rtx set = PATTERN (insn);
42750 rtx select = XEXP (set, 1);
42751 rtx concat = XEXP (select, 0);
42752 rtx src0 = XEXP (concat, 0);
42753 XEXP (concat, 0) = XEXP (concat, 1);
42754 XEXP (concat, 1) = src0;
42755 rtx parallel = XEXP (select, 1);
42756 int lane0 = INTVAL (XVECEXP (parallel, 0, 0));
42757 int lane1 = INTVAL (XVECEXP (parallel, 0, 1));
42758 int new_lane0 = 3 - lane1;
42759 int new_lane1 = 3 - lane0;
42760 XVECEXP (parallel, 0, 0) = GEN_INT (new_lane0);
42761 XVECEXP (parallel, 0, 1) = GEN_INT (new_lane1);
42762 INSN_CODE (insn) = -1; /* Force re-recognition. */
42763 df_insn_rescan (insn);
42765 if (dump_file)
42766 fprintf (dump_file, "Changing lanes for xxpermdi %d\n", INSN_UID (insn));
42769 /* Given OP that contains a VEC_CONCAT operation of two doublewords,
42770 reverse the order of those inputs. */
42771 static void
42772 adjust_concat (rtx_insn *insn)
42774 rtx set = PATTERN (insn);
42775 rtx concat = XEXP (set, 1);
42776 rtx src0 = XEXP (concat, 0);
42777 XEXP (concat, 0) = XEXP (concat, 1);
42778 XEXP (concat, 1) = src0;
42779 INSN_CODE (insn) = -1; /* Force re-recognition. */
42780 df_insn_rescan (insn);
42782 if (dump_file)
42783 fprintf (dump_file, "Reversing inputs for concat %d\n", INSN_UID (insn));
42786 /* Given an UNSPEC_VPERM insn, modify the mask loaded from the
42787 constant pool to reflect swapped doublewords. */
42788 static void
42789 adjust_vperm (rtx_insn *insn)
42791 /* We previously determined that the UNSPEC_VPERM was fed by a
42792 swap of a swapping load of a TOC-relative constant pool symbol.
42793 Find the MEM in the swapping load and replace it with a MEM for
42794 the adjusted mask constant. */
42795 rtx set = PATTERN (insn);
42796 rtx mask_reg = XVECEXP (SET_SRC (set), 0, 2);
42798 /* Find the swap. */
42799 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42800 df_ref use;
42801 rtx_insn *swap_insn = 0;
42802 FOR_EACH_INSN_INFO_USE (use, insn_info)
42803 if (rtx_equal_p (DF_REF_REG (use), mask_reg))
42805 struct df_link *def_link = DF_REF_CHAIN (use);
42806 gcc_assert (def_link && !def_link->next);
42807 swap_insn = DF_REF_INSN (def_link->ref);
42808 break;
42810 gcc_assert (swap_insn);
42812 /* Find the load. */
42813 insn_info = DF_INSN_INFO_GET (swap_insn);
42814 rtx_insn *load_insn = 0;
42815 FOR_EACH_INSN_INFO_USE (use, insn_info)
42817 struct df_link *def_link = DF_REF_CHAIN (use);
42818 gcc_assert (def_link && !def_link->next);
42819 load_insn = DF_REF_INSN (def_link->ref);
42820 break;
42822 gcc_assert (load_insn);
42824 /* Find the TOC-relative symbol access. */
42825 insn_info = DF_INSN_INFO_GET (load_insn);
42826 rtx_insn *tocrel_insn = 0;
42827 FOR_EACH_INSN_INFO_USE (use, insn_info)
42829 struct df_link *def_link = DF_REF_CHAIN (use);
42830 gcc_assert (def_link && !def_link->next);
42831 tocrel_insn = DF_REF_INSN (def_link->ref);
42832 break;
42834 gcc_assert (tocrel_insn);
42836 /* Find the embedded CONST_VECTOR. We have to call toc_relative_expr_p
42837 to set tocrel_base; otherwise it would be unnecessary as we've
42838 already established it will return true. */
42839 rtx base, offset;
42840 rtx tocrel_expr = SET_SRC (PATTERN (tocrel_insn));
42841 /* There is an extra level of indirection for small/large code models. */
42842 if (GET_CODE (tocrel_expr) == MEM)
42843 tocrel_expr = XEXP (tocrel_expr, 0);
42844 if (!toc_relative_expr_p (tocrel_expr, false))
42845 gcc_unreachable ();
42846 split_const (XVECEXP (tocrel_base, 0, 0), &base, &offset);
42847 rtx const_vector = get_pool_constant (base);
42848 /* With the extra indirection, get_pool_constant will produce the
42849 real constant from the reg_equal expression, so get the real
42850 constant. */
42851 if (GET_CODE (const_vector) == SYMBOL_REF)
42852 const_vector = get_pool_constant (const_vector);
42853 gcc_assert (GET_CODE (const_vector) == CONST_VECTOR);
42855 /* Create an adjusted mask from the initial mask. */
42856 unsigned int new_mask[16], i, val;
42857 for (i = 0; i < 16; ++i) {
42858 val = INTVAL (XVECEXP (const_vector, 0, i));
42859 if (val < 16)
42860 new_mask[i] = (val + 8) % 16;
42861 else
42862 new_mask[i] = ((val + 8) % 16) + 16;
42865 /* Create a new CONST_VECTOR and a MEM that references it. */
42866 rtx vals = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
42867 for (i = 0; i < 16; ++i)
42868 XVECEXP (vals, 0, i) = GEN_INT (new_mask[i]);
42869 rtx new_const_vector = gen_rtx_CONST_VECTOR (V16QImode, XVEC (vals, 0));
42870 rtx new_mem = force_const_mem (V16QImode, new_const_vector);
42871 /* This gives us a MEM whose base operand is a SYMBOL_REF, which we
42872 can't recognize. Force the SYMBOL_REF into a register. */
42873 if (!REG_P (XEXP (new_mem, 0))) {
42874 rtx base_reg = force_reg (Pmode, XEXP (new_mem, 0));
42875 XEXP (new_mem, 0) = base_reg;
42876 /* Move the newly created insn ahead of the load insn. */
42877 rtx_insn *force_insn = get_last_insn ();
42878 remove_insn (force_insn);
42879 rtx_insn *before_load_insn = PREV_INSN (load_insn);
42880 add_insn_after (force_insn, before_load_insn, BLOCK_FOR_INSN (load_insn));
42881 df_insn_rescan (before_load_insn);
42882 df_insn_rescan (force_insn);
42885 /* Replace the MEM in the load instruction and rescan it. */
42886 XEXP (SET_SRC (PATTERN (load_insn)), 0) = new_mem;
42887 INSN_CODE (load_insn) = -1; /* Force re-recognition. */
42888 df_insn_rescan (load_insn);
42890 if (dump_file)
42891 fprintf (dump_file, "Adjusting mask for vperm %d\n", INSN_UID (insn));
42894 /* The insn described by INSN_ENTRY[I] can be swapped, but only
42895 with special handling. Take care of that here. */
42896 static void
42897 handle_special_swappables (swap_web_entry *insn_entry, unsigned i)
42899 rtx_insn *insn = insn_entry[i].insn;
42900 rtx body = PATTERN (insn);
42902 switch (insn_entry[i].special_handling)
42904 default:
42905 gcc_unreachable ();
42906 case SH_CONST_VECTOR:
42908 /* A CONST_VECTOR will only show up somewhere in the RHS of a SET. */
42909 gcc_assert (GET_CODE (body) == SET);
42910 rtx rhs = SET_SRC (body);
42911 swap_const_vector_halves (rhs);
42912 if (dump_file)
42913 fprintf (dump_file, "Swapping constant halves in insn %d\n", i);
42914 break;
42916 case SH_SUBREG:
42917 /* A subreg of the same size is already safe. For subregs that
42918 select a smaller portion of a reg, adjust the index for
42919 swapped doublewords. */
42920 adjust_subreg_index (body);
42921 if (dump_file)
42922 fprintf (dump_file, "Adjusting subreg in insn %d\n", i);
42923 break;
42924 case SH_NOSWAP_LD:
42925 /* Convert a non-permuting load to a permuting one. */
42926 permute_load (insn);
42927 break;
42928 case SH_NOSWAP_ST:
42929 /* Convert a non-permuting store to a permuting one. */
42930 permute_store (insn);
42931 break;
42932 case SH_EXTRACT:
42933 /* Change the lane on an extract operation. */
42934 adjust_extract (insn);
42935 break;
42936 case SH_SPLAT:
42937 /* Change the lane on a direct-splat operation. */
42938 adjust_splat (insn);
42939 break;
42940 case SH_XXPERMDI:
42941 /* Change the lanes on an XXPERMDI operation. */
42942 adjust_xxpermdi (insn);
42943 break;
42944 case SH_CONCAT:
42945 /* Reverse the order of a concatenation operation. */
42946 adjust_concat (insn);
42947 break;
42948 case SH_VPERM:
42949 /* Change the mask loaded from the constant pool for a VPERM. */
42950 adjust_vperm (insn);
42951 break;
42955 /* Find the insn from the Ith table entry, which is known to be a
42956 register swap Y = SWAP(X). Replace it with a copy Y = X. */
42957 static void
42958 replace_swap_with_copy (swap_web_entry *insn_entry, unsigned i)
42960 rtx_insn *insn = insn_entry[i].insn;
42961 rtx body = PATTERN (insn);
42962 rtx src_reg = XEXP (SET_SRC (body), 0);
42963 rtx copy = gen_rtx_SET (SET_DEST (body), src_reg);
42964 rtx_insn *new_insn = emit_insn_before (copy, insn);
42965 set_block_for_insn (new_insn, BLOCK_FOR_INSN (insn));
42966 df_insn_rescan (new_insn);
42968 if (dump_file)
42970 unsigned int new_uid = INSN_UID (new_insn);
42971 fprintf (dump_file, "Replacing swap %d with copy %d\n", i, new_uid);
42974 df_insn_delete (insn);
42975 remove_insn (insn);
42976 insn->set_deleted ();
42979 /* Dump the swap table to DUMP_FILE. */
42980 static void
42981 dump_swap_insn_table (swap_web_entry *insn_entry)
42983 int e = get_max_uid ();
42984 fprintf (dump_file, "\nRelevant insns with their flag settings\n\n");
42986 for (int i = 0; i < e; ++i)
42987 if (insn_entry[i].is_relevant)
42989 swap_web_entry *pred_entry = (swap_web_entry *)insn_entry[i].pred ();
42990 fprintf (dump_file, "%6d %6d ", i,
42991 pred_entry && pred_entry->insn
42992 ? INSN_UID (pred_entry->insn) : 0);
42993 if (insn_entry[i].is_load)
42994 fputs ("load ", dump_file);
42995 if (insn_entry[i].is_store)
42996 fputs ("store ", dump_file);
42997 if (insn_entry[i].is_swap)
42998 fputs ("swap ", dump_file);
42999 if (insn_entry[i].is_live_in)
43000 fputs ("live-in ", dump_file);
43001 if (insn_entry[i].is_live_out)
43002 fputs ("live-out ", dump_file);
43003 if (insn_entry[i].contains_subreg)
43004 fputs ("subreg ", dump_file);
43005 if (insn_entry[i].is_128_int)
43006 fputs ("int128 ", dump_file);
43007 if (insn_entry[i].is_call)
43008 fputs ("call ", dump_file);
43009 if (insn_entry[i].is_swappable)
43011 fputs ("swappable ", dump_file);
43012 if (insn_entry[i].special_handling == SH_CONST_VECTOR)
43013 fputs ("special:constvec ", dump_file);
43014 else if (insn_entry[i].special_handling == SH_SUBREG)
43015 fputs ("special:subreg ", dump_file);
43016 else if (insn_entry[i].special_handling == SH_NOSWAP_LD)
43017 fputs ("special:load ", dump_file);
43018 else if (insn_entry[i].special_handling == SH_NOSWAP_ST)
43019 fputs ("special:store ", dump_file);
43020 else if (insn_entry[i].special_handling == SH_EXTRACT)
43021 fputs ("special:extract ", dump_file);
43022 else if (insn_entry[i].special_handling == SH_SPLAT)
43023 fputs ("special:splat ", dump_file);
43024 else if (insn_entry[i].special_handling == SH_XXPERMDI)
43025 fputs ("special:xxpermdi ", dump_file);
43026 else if (insn_entry[i].special_handling == SH_CONCAT)
43027 fputs ("special:concat ", dump_file);
43028 else if (insn_entry[i].special_handling == SH_VPERM)
43029 fputs ("special:vperm ", dump_file);
43031 if (insn_entry[i].web_not_optimizable)
43032 fputs ("unoptimizable ", dump_file);
43033 if (insn_entry[i].will_delete)
43034 fputs ("delete ", dump_file);
43035 fputs ("\n", dump_file);
43037 fputs ("\n", dump_file);
43040 /* Return RTX with its address canonicalized to (reg) or (+ reg reg).
43041 Here RTX is an (& addr (const_int -16)). Always return a new copy
43042 to avoid problems with combine. */
43043 static rtx
43044 alignment_with_canonical_addr (rtx align)
43046 rtx canon;
43047 rtx addr = XEXP (align, 0);
43049 if (REG_P (addr))
43050 canon = addr;
43052 else if (GET_CODE (addr) == PLUS)
43054 rtx addrop0 = XEXP (addr, 0);
43055 rtx addrop1 = XEXP (addr, 1);
43057 if (!REG_P (addrop0))
43058 addrop0 = force_reg (GET_MODE (addrop0), addrop0);
43060 if (!REG_P (addrop1))
43061 addrop1 = force_reg (GET_MODE (addrop1), addrop1);
43063 canon = gen_rtx_PLUS (GET_MODE (addr), addrop0, addrop1);
43066 else
43067 canon = force_reg (GET_MODE (addr), addr);
43069 return gen_rtx_AND (GET_MODE (align), canon, GEN_INT (-16));
43072 /* Check whether an rtx is an alignment mask, and if so, return
43073 a fully-expanded rtx for the masking operation. */
43074 static rtx
43075 alignment_mask (rtx_insn *insn)
43077 rtx body = PATTERN (insn);
43079 if (GET_CODE (body) != SET
43080 || GET_CODE (SET_SRC (body)) != AND
43081 || !REG_P (XEXP (SET_SRC (body), 0)))
43082 return 0;
43084 rtx mask = XEXP (SET_SRC (body), 1);
43086 if (GET_CODE (mask) == CONST_INT)
43088 if (INTVAL (mask) == -16)
43089 return alignment_with_canonical_addr (SET_SRC (body));
43090 else
43091 return 0;
43094 if (!REG_P (mask))
43095 return 0;
43097 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43098 df_ref use;
43099 rtx real_mask = 0;
43101 FOR_EACH_INSN_INFO_USE (use, insn_info)
43103 if (!rtx_equal_p (DF_REF_REG (use), mask))
43104 continue;
43106 struct df_link *def_link = DF_REF_CHAIN (use);
43107 if (!def_link || def_link->next)
43108 return 0;
43110 rtx_insn *const_insn = DF_REF_INSN (def_link->ref);
43111 rtx const_body = PATTERN (const_insn);
43112 if (GET_CODE (const_body) != SET)
43113 return 0;
43115 real_mask = SET_SRC (const_body);
43117 if (GET_CODE (real_mask) != CONST_INT
43118 || INTVAL (real_mask) != -16)
43119 return 0;
43122 if (real_mask == 0)
43123 return 0;
43125 return alignment_with_canonical_addr (SET_SRC (body));
43128 /* Given INSN that's a load or store based at BASE_REG, look for a
43129 feeding computation that aligns its address on a 16-byte boundary. */
43130 static rtx
43131 find_alignment_op (rtx_insn *insn, rtx base_reg)
43133 df_ref base_use;
43134 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43135 rtx and_operation = 0;
43137 FOR_EACH_INSN_INFO_USE (base_use, insn_info)
43139 if (!rtx_equal_p (DF_REF_REG (base_use), base_reg))
43140 continue;
43142 struct df_link *base_def_link = DF_REF_CHAIN (base_use);
43143 if (!base_def_link || base_def_link->next)
43144 break;
43146 /* With stack-protector code enabled, and possibly in other
43147 circumstances, there may not be an associated insn for
43148 the def. */
43149 if (DF_REF_IS_ARTIFICIAL (base_def_link->ref))
43150 break;
43152 rtx_insn *and_insn = DF_REF_INSN (base_def_link->ref);
43153 and_operation = alignment_mask (and_insn);
43154 if (and_operation != 0)
43155 break;
43158 return and_operation;
43161 struct del_info { bool replace; rtx_insn *replace_insn; };
43163 /* If INSN is the load for an lvx pattern, put it in canonical form. */
43164 static void
43165 recombine_lvx_pattern (rtx_insn *insn, del_info *to_delete)
43167 rtx body = PATTERN (insn);
43168 gcc_assert (GET_CODE (body) == SET
43169 && GET_CODE (SET_SRC (body)) == VEC_SELECT
43170 && GET_CODE (XEXP (SET_SRC (body), 0)) == MEM);
43172 rtx mem = XEXP (SET_SRC (body), 0);
43173 rtx base_reg = XEXP (mem, 0);
43175 rtx and_operation = find_alignment_op (insn, base_reg);
43177 if (and_operation != 0)
43179 df_ref def;
43180 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43181 FOR_EACH_INSN_INFO_DEF (def, insn_info)
43183 struct df_link *link = DF_REF_CHAIN (def);
43184 if (!link || link->next)
43185 break;
43187 rtx_insn *swap_insn = DF_REF_INSN (link->ref);
43188 if (!insn_is_swap_p (swap_insn)
43189 || insn_is_load_p (swap_insn)
43190 || insn_is_store_p (swap_insn))
43191 break;
43193 /* Expected lvx pattern found. Change the swap to
43194 a copy, and propagate the AND operation into the
43195 load. */
43196 to_delete[INSN_UID (swap_insn)].replace = true;
43197 to_delete[INSN_UID (swap_insn)].replace_insn = swap_insn;
43199 XEXP (mem, 0) = and_operation;
43200 SET_SRC (body) = mem;
43201 INSN_CODE (insn) = -1; /* Force re-recognition. */
43202 df_insn_rescan (insn);
43204 if (dump_file)
43205 fprintf (dump_file, "lvx opportunity found at %d\n",
43206 INSN_UID (insn));
43211 /* If INSN is the store for an stvx pattern, put it in canonical form. */
43212 static void
43213 recombine_stvx_pattern (rtx_insn *insn, del_info *to_delete)
43215 rtx body = PATTERN (insn);
43216 gcc_assert (GET_CODE (body) == SET
43217 && GET_CODE (SET_DEST (body)) == MEM
43218 && GET_CODE (SET_SRC (body)) == VEC_SELECT);
43219 rtx mem = SET_DEST (body);
43220 rtx base_reg = XEXP (mem, 0);
43222 rtx and_operation = find_alignment_op (insn, base_reg);
43224 if (and_operation != 0)
43226 rtx src_reg = XEXP (SET_SRC (body), 0);
43227 df_ref src_use;
43228 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43229 FOR_EACH_INSN_INFO_USE (src_use, insn_info)
43231 if (!rtx_equal_p (DF_REF_REG (src_use), src_reg))
43232 continue;
43234 struct df_link *link = DF_REF_CHAIN (src_use);
43235 if (!link || link->next)
43236 break;
43238 rtx_insn *swap_insn = DF_REF_INSN (link->ref);
43239 if (!insn_is_swap_p (swap_insn)
43240 || insn_is_load_p (swap_insn)
43241 || insn_is_store_p (swap_insn))
43242 break;
43244 /* Expected stvx pattern found. Change the swap to
43245 a copy, and propagate the AND operation into the
43246 store. */
43247 to_delete[INSN_UID (swap_insn)].replace = true;
43248 to_delete[INSN_UID (swap_insn)].replace_insn = swap_insn;
43250 XEXP (mem, 0) = and_operation;
43251 SET_SRC (body) = src_reg;
43252 INSN_CODE (insn) = -1; /* Force re-recognition. */
43253 df_insn_rescan (insn);
43255 if (dump_file)
43256 fprintf (dump_file, "stvx opportunity found at %d\n",
43257 INSN_UID (insn));
43262 /* Look for patterns created from builtin lvx and stvx calls, and
43263 canonicalize them to be properly recognized as such. */
43264 static void
43265 recombine_lvx_stvx_patterns (function *fun)
43267 int i;
43268 basic_block bb;
43269 rtx_insn *insn;
43271 int num_insns = get_max_uid ();
43272 del_info *to_delete = XCNEWVEC (del_info, num_insns);
43274 FOR_ALL_BB_FN (bb, fun)
43275 FOR_BB_INSNS (bb, insn)
43277 if (!NONDEBUG_INSN_P (insn))
43278 continue;
43280 if (insn_is_load_p (insn) && insn_is_swap_p (insn))
43281 recombine_lvx_pattern (insn, to_delete);
43282 else if (insn_is_store_p (insn) && insn_is_swap_p (insn))
43283 recombine_stvx_pattern (insn, to_delete);
43286 /* Turning swaps into copies is delayed until now, to avoid problems
43287 with deleting instructions during the insn walk. */
43288 for (i = 0; i < num_insns; i++)
43289 if (to_delete[i].replace)
43291 rtx swap_body = PATTERN (to_delete[i].replace_insn);
43292 rtx src_reg = XEXP (SET_SRC (swap_body), 0);
43293 rtx copy = gen_rtx_SET (SET_DEST (swap_body), src_reg);
43294 rtx_insn *new_insn = emit_insn_before (copy,
43295 to_delete[i].replace_insn);
43296 set_block_for_insn (new_insn,
43297 BLOCK_FOR_INSN (to_delete[i].replace_insn));
43298 df_insn_rescan (new_insn);
43299 df_insn_delete (to_delete[i].replace_insn);
43300 remove_insn (to_delete[i].replace_insn);
43301 to_delete[i].replace_insn->set_deleted ();
43304 free (to_delete);
43307 /* Main entry point for this pass. */
43308 unsigned int
43309 rs6000_analyze_swaps (function *fun)
43311 swap_web_entry *insn_entry;
43312 basic_block bb;
43313 rtx_insn *insn, *curr_insn = 0;
43315 /* Dataflow analysis for use-def chains. */
43316 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
43317 df_chain_add_problem (DF_DU_CHAIN | DF_UD_CHAIN);
43318 df_analyze ();
43319 df_set_flags (DF_DEFER_INSN_RESCAN);
43321 /* Pre-pass to recombine lvx and stvx patterns so we don't lose info. */
43322 recombine_lvx_stvx_patterns (fun);
43324 /* Allocate structure to represent webs of insns. */
43325 insn_entry = XCNEWVEC (swap_web_entry, get_max_uid ());
43327 /* Walk the insns to gather basic data. */
43328 FOR_ALL_BB_FN (bb, fun)
43329 FOR_BB_INSNS_SAFE (bb, insn, curr_insn)
43331 unsigned int uid = INSN_UID (insn);
43332 if (NONDEBUG_INSN_P (insn))
43334 insn_entry[uid].insn = insn;
43336 if (GET_CODE (insn) == CALL_INSN)
43337 insn_entry[uid].is_call = 1;
43339 /* Walk the uses and defs to see if we mention vector regs.
43340 Record any constraints on optimization of such mentions. */
43341 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43342 df_ref mention;
43343 FOR_EACH_INSN_INFO_USE (mention, insn_info)
43345 /* We use DF_REF_REAL_REG here to get inside any subregs. */
43346 machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
43348 /* If a use gets its value from a call insn, it will be
43349 a hard register and will look like (reg:V4SI 3 3).
43350 The df analysis creates two mentions for GPR3 and GPR4,
43351 both DImode. We must recognize this and treat it as a
43352 vector mention to ensure the call is unioned with this
43353 use. */
43354 if (mode == DImode && DF_REF_INSN_INFO (mention))
43356 rtx feeder = DF_REF_INSN (mention);
43357 /* FIXME: It is pretty hard to get from the df mention
43358 to the mode of the use in the insn. We arbitrarily
43359 pick a vector mode here, even though the use might
43360 be a real DImode. We can be too conservative
43361 (create a web larger than necessary) because of
43362 this, so consider eventually fixing this. */
43363 if (GET_CODE (feeder) == CALL_INSN)
43364 mode = V4SImode;
43367 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode) || mode == TImode)
43369 insn_entry[uid].is_relevant = 1;
43370 if (mode == TImode || mode == V1TImode
43371 || FLOAT128_VECTOR_P (mode))
43372 insn_entry[uid].is_128_int = 1;
43373 if (DF_REF_INSN_INFO (mention))
43374 insn_entry[uid].contains_subreg
43375 = !rtx_equal_p (DF_REF_REG (mention),
43376 DF_REF_REAL_REG (mention));
43377 union_defs (insn_entry, insn, mention);
43380 FOR_EACH_INSN_INFO_DEF (mention, insn_info)
43382 /* We use DF_REF_REAL_REG here to get inside any subregs. */
43383 machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
43385 /* If we're loading up a hard vector register for a call,
43386 it looks like (set (reg:V4SI 9 9) (...)). The df
43387 analysis creates two mentions for GPR9 and GPR10, both
43388 DImode. So relying on the mode from the mentions
43389 isn't sufficient to ensure we union the call into the
43390 web with the parameter setup code. */
43391 if (mode == DImode && GET_CODE (insn) == SET
43392 && ALTIVEC_OR_VSX_VECTOR_MODE (GET_MODE (SET_DEST (insn))))
43393 mode = GET_MODE (SET_DEST (insn));
43395 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode) || mode == TImode)
43397 insn_entry[uid].is_relevant = 1;
43398 if (mode == TImode || mode == V1TImode
43399 || FLOAT128_VECTOR_P (mode))
43400 insn_entry[uid].is_128_int = 1;
43401 if (DF_REF_INSN_INFO (mention))
43402 insn_entry[uid].contains_subreg
43403 = !rtx_equal_p (DF_REF_REG (mention),
43404 DF_REF_REAL_REG (mention));
43405 /* REG_FUNCTION_VALUE_P is not valid for subregs. */
43406 else if (REG_FUNCTION_VALUE_P (DF_REF_REG (mention)))
43407 insn_entry[uid].is_live_out = 1;
43408 union_uses (insn_entry, insn, mention);
43412 if (insn_entry[uid].is_relevant)
43414 /* Determine if this is a load or store. */
43415 insn_entry[uid].is_load = insn_is_load_p (insn);
43416 insn_entry[uid].is_store = insn_is_store_p (insn);
43418 /* Determine if this is a doubleword swap. If not,
43419 determine whether it can legally be swapped. */
43420 if (insn_is_swap_p (insn))
43421 insn_entry[uid].is_swap = 1;
43422 else
43424 unsigned int special = SH_NONE;
43425 insn_entry[uid].is_swappable
43426 = insn_is_swappable_p (insn_entry, insn, &special);
43427 if (special != SH_NONE && insn_entry[uid].contains_subreg)
43428 insn_entry[uid].is_swappable = 0;
43429 else if (special != SH_NONE)
43430 insn_entry[uid].special_handling = special;
43431 else if (insn_entry[uid].contains_subreg)
43432 insn_entry[uid].special_handling = SH_SUBREG;
43438 if (dump_file)
43440 fprintf (dump_file, "\nSwap insn entry table when first built\n");
43441 dump_swap_insn_table (insn_entry);
43444 /* Record unoptimizable webs. */
43445 unsigned e = get_max_uid (), i;
43446 for (i = 0; i < e; ++i)
43448 if (!insn_entry[i].is_relevant)
43449 continue;
43451 swap_web_entry *root
43452 = (swap_web_entry*)(&insn_entry[i])->unionfind_root ();
43454 if (insn_entry[i].is_live_in || insn_entry[i].is_live_out
43455 || (insn_entry[i].contains_subreg
43456 && insn_entry[i].special_handling != SH_SUBREG)
43457 || insn_entry[i].is_128_int || insn_entry[i].is_call
43458 || !(insn_entry[i].is_swappable || insn_entry[i].is_swap))
43459 root->web_not_optimizable = 1;
43461 /* If we have loads or stores that aren't permuting then the
43462 optimization isn't appropriate. */
43463 else if ((insn_entry[i].is_load || insn_entry[i].is_store)
43464 && !insn_entry[i].is_swap && !insn_entry[i].is_swappable)
43465 root->web_not_optimizable = 1;
43467 /* If we have permuting loads or stores that are not accompanied
43468 by a register swap, the optimization isn't appropriate. */
43469 else if (insn_entry[i].is_load && insn_entry[i].is_swap)
43471 rtx insn = insn_entry[i].insn;
43472 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43473 df_ref def;
43475 FOR_EACH_INSN_INFO_DEF (def, insn_info)
43477 struct df_link *link = DF_REF_CHAIN (def);
43479 if (!chain_contains_only_swaps (insn_entry, link, FOR_LOADS))
43481 root->web_not_optimizable = 1;
43482 break;
43486 else if (insn_entry[i].is_store && insn_entry[i].is_swap)
43488 rtx insn = insn_entry[i].insn;
43489 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43490 df_ref use;
43492 FOR_EACH_INSN_INFO_USE (use, insn_info)
43494 struct df_link *link = DF_REF_CHAIN (use);
43496 if (!chain_contains_only_swaps (insn_entry, link, FOR_STORES))
43498 root->web_not_optimizable = 1;
43499 break;
43505 if (dump_file)
43507 fprintf (dump_file, "\nSwap insn entry table after web analysis\n");
43508 dump_swap_insn_table (insn_entry);
43511 /* For each load and store in an optimizable web (which implies
43512 the loads and stores are permuting), find the associated
43513 register swaps and mark them for removal. Due to various
43514 optimizations we may mark the same swap more than once. Also
43515 perform special handling for swappable insns that require it. */
43516 for (i = 0; i < e; ++i)
43517 if ((insn_entry[i].is_load || insn_entry[i].is_store)
43518 && insn_entry[i].is_swap)
43520 swap_web_entry* root_entry
43521 = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
43522 if (!root_entry->web_not_optimizable)
43523 mark_swaps_for_removal (insn_entry, i);
43525 else if (insn_entry[i].is_swappable && insn_entry[i].special_handling)
43527 swap_web_entry* root_entry
43528 = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
43529 if (!root_entry->web_not_optimizable)
43530 handle_special_swappables (insn_entry, i);
43533 /* Now delete the swaps marked for removal. */
43534 for (i = 0; i < e; ++i)
43535 if (insn_entry[i].will_delete)
43536 replace_swap_with_copy (insn_entry, i);
43538 /* Clean up. */
43539 free (insn_entry);
43540 return 0;
43543 const pass_data pass_data_analyze_swaps =
43545 RTL_PASS, /* type */
43546 "swaps", /* name */
43547 OPTGROUP_NONE, /* optinfo_flags */
43548 TV_NONE, /* tv_id */
43549 0, /* properties_required */
43550 0, /* properties_provided */
43551 0, /* properties_destroyed */
43552 0, /* todo_flags_start */
43553 TODO_df_finish, /* todo_flags_finish */
43556 class pass_analyze_swaps : public rtl_opt_pass
43558 public:
43559 pass_analyze_swaps(gcc::context *ctxt)
43560 : rtl_opt_pass(pass_data_analyze_swaps, ctxt)
43563 /* opt_pass methods: */
43564 virtual bool gate (function *)
43566 return (optimize > 0 && !BYTES_BIG_ENDIAN && TARGET_VSX
43567 && !TARGET_P9_VECTOR && rs6000_optimize_swaps);
43570 virtual unsigned int execute (function *fun)
43572 return rs6000_analyze_swaps (fun);
43575 opt_pass *clone ()
43577 return new pass_analyze_swaps (m_ctxt);
43580 }; // class pass_analyze_swaps
43582 rtl_opt_pass *
43583 make_pass_analyze_swaps (gcc::context *ctxt)
43585 return new pass_analyze_swaps (ctxt);
43588 #ifdef RS6000_GLIBC_ATOMIC_FENV
43589 /* Function declarations for rs6000_atomic_assign_expand_fenv. */
43590 static tree atomic_hold_decl, atomic_clear_decl, atomic_update_decl;
43591 #endif
43593 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
43595 static void
43596 rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
43598 if (!TARGET_HARD_FLOAT || !TARGET_FPRS)
43600 #ifdef RS6000_GLIBC_ATOMIC_FENV
43601 if (atomic_hold_decl == NULL_TREE)
43603 atomic_hold_decl
43604 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
43605 get_identifier ("__atomic_feholdexcept"),
43606 build_function_type_list (void_type_node,
43607 double_ptr_type_node,
43608 NULL_TREE));
43609 TREE_PUBLIC (atomic_hold_decl) = 1;
43610 DECL_EXTERNAL (atomic_hold_decl) = 1;
43613 if (atomic_clear_decl == NULL_TREE)
43615 atomic_clear_decl
43616 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
43617 get_identifier ("__atomic_feclearexcept"),
43618 build_function_type_list (void_type_node,
43619 NULL_TREE));
43620 TREE_PUBLIC (atomic_clear_decl) = 1;
43621 DECL_EXTERNAL (atomic_clear_decl) = 1;
43624 tree const_double = build_qualified_type (double_type_node,
43625 TYPE_QUAL_CONST);
43626 tree const_double_ptr = build_pointer_type (const_double);
43627 if (atomic_update_decl == NULL_TREE)
43629 atomic_update_decl
43630 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
43631 get_identifier ("__atomic_feupdateenv"),
43632 build_function_type_list (void_type_node,
43633 const_double_ptr,
43634 NULL_TREE));
43635 TREE_PUBLIC (atomic_update_decl) = 1;
43636 DECL_EXTERNAL (atomic_update_decl) = 1;
43639 tree fenv_var = create_tmp_var_raw (double_type_node);
43640 TREE_ADDRESSABLE (fenv_var) = 1;
43641 tree fenv_addr = build1 (ADDR_EXPR, double_ptr_type_node, fenv_var);
43643 *hold = build_call_expr (atomic_hold_decl, 1, fenv_addr);
43644 *clear = build_call_expr (atomic_clear_decl, 0);
43645 *update = build_call_expr (atomic_update_decl, 1,
43646 fold_convert (const_double_ptr, fenv_addr));
43647 #endif
43648 return;
43651 tree mffs = rs6000_builtin_decls[RS6000_BUILTIN_MFFS];
43652 tree mtfsf = rs6000_builtin_decls[RS6000_BUILTIN_MTFSF];
43653 tree call_mffs = build_call_expr (mffs, 0);
43655 /* Generates the equivalent of feholdexcept (&fenv_var)
43657 *fenv_var = __builtin_mffs ();
43658 double fenv_hold;
43659 *(uint64_t*)&fenv_hold = *(uint64_t*)fenv_var & 0xffffffff00000007LL;
43660 __builtin_mtfsf (0xff, fenv_hold); */
43662 /* Mask to clear everything except for the rounding modes and non-IEEE
43663 arithmetic flag. */
43664 const unsigned HOST_WIDE_INT hold_exception_mask =
43665 HOST_WIDE_INT_C (0xffffffff00000007);
43667 tree fenv_var = create_tmp_var_raw (double_type_node);
43669 tree hold_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_var, call_mffs);
43671 tree fenv_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_var);
43672 tree fenv_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
43673 build_int_cst (uint64_type_node,
43674 hold_exception_mask));
43676 tree fenv_hold_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
43677 fenv_llu_and);
43679 tree hold_mtfsf = build_call_expr (mtfsf, 2,
43680 build_int_cst (unsigned_type_node, 0xff),
43681 fenv_hold_mtfsf);
43683 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_mffs, hold_mtfsf);
43685 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT):
43687 double fenv_clear = __builtin_mffs ();
43688 *(uint64_t)&fenv_clear &= 0xffffffff00000000LL;
43689 __builtin_mtfsf (0xff, fenv_clear); */
43691 /* Mask to clear everything except for the rounding modes and non-IEEE
43692 arithmetic flag. */
43693 const unsigned HOST_WIDE_INT clear_exception_mask =
43694 HOST_WIDE_INT_C (0xffffffff00000000);
43696 tree fenv_clear = create_tmp_var_raw (double_type_node);
43698 tree clear_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_clear, call_mffs);
43700 tree fenv_clean_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_clear);
43701 tree fenv_clear_llu_and = build2 (BIT_AND_EXPR, uint64_type_node,
43702 fenv_clean_llu,
43703 build_int_cst (uint64_type_node,
43704 clear_exception_mask));
43706 tree fenv_clear_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
43707 fenv_clear_llu_and);
43709 tree clear_mtfsf = build_call_expr (mtfsf, 2,
43710 build_int_cst (unsigned_type_node, 0xff),
43711 fenv_clear_mtfsf);
43713 *clear = build2 (COMPOUND_EXPR, void_type_node, clear_mffs, clear_mtfsf);
43715 /* Generates the equivalent of feupdateenv (&fenv_var)
43717 double old_fenv = __builtin_mffs ();
43718 double fenv_update;
43719 *(uint64_t*)&fenv_update = (*(uint64_t*)&old & 0xffffffff1fffff00LL) |
43720 (*(uint64_t*)fenv_var 0x1ff80fff);
43721 __builtin_mtfsf (0xff, fenv_update); */
43723 const unsigned HOST_WIDE_INT update_exception_mask =
43724 HOST_WIDE_INT_C (0xffffffff1fffff00);
43725 const unsigned HOST_WIDE_INT new_exception_mask =
43726 HOST_WIDE_INT_C (0x1ff80fff);
43728 tree old_fenv = create_tmp_var_raw (double_type_node);
43729 tree update_mffs = build2 (MODIFY_EXPR, void_type_node, old_fenv, call_mffs);
43731 tree old_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, old_fenv);
43732 tree old_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, old_llu,
43733 build_int_cst (uint64_type_node,
43734 update_exception_mask));
43736 tree new_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
43737 build_int_cst (uint64_type_node,
43738 new_exception_mask));
43740 tree new_llu_mask = build2 (BIT_IOR_EXPR, uint64_type_node,
43741 old_llu_and, new_llu_and);
43743 tree fenv_update_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
43744 new_llu_mask);
43746 tree update_mtfsf = build_call_expr (mtfsf, 2,
43747 build_int_cst (unsigned_type_node, 0xff),
43748 fenv_update_mtfsf);
43750 *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf);
43753 /* Implement the TARGET_OPTAB_SUPPORTED_P hook. */
43755 static bool
43756 rs6000_optab_supported_p (int op, machine_mode mode1, machine_mode,
43757 optimization_type opt_type)
43759 switch (op)
43761 case rsqrt_optab:
43762 return (opt_type == OPTIMIZE_FOR_SPEED
43763 && RS6000_RECIP_AUTO_RSQRTE_P (mode1));
43765 default:
43766 return true;
43770 /* Implement TARGET_CONSTANT_ALIGNMENT. */
43772 static HOST_WIDE_INT
43773 rs6000_constant_alignment (const_tree exp, HOST_WIDE_INT align)
43775 if (TREE_CODE (exp) == STRING_CST
43776 && (STRICT_ALIGNMENT || !optimize_size))
43777 return MAX (align, BITS_PER_WORD);
43778 return align;
43781 /* Implement TARGET_STARTING_FRAME_OFFSET. */
43783 static HOST_WIDE_INT
43784 rs6000_starting_frame_offset (void)
43786 if (FRAME_GROWS_DOWNWARD)
43787 return 0;
43788 return RS6000_STARTING_FRAME_OFFSET;
43791 struct gcc_target targetm = TARGET_INITIALIZER;
43793 #include "gt-powerpcspe.h"