Add mode_for_int_vector helper functions
[official-gcc.git] / gcc / config / powerpcspe / powerpcspe.c
blob82c36dee6783bc38810304c0ed47e6a671e2aa1f
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_secondary_memory_needed (enum reg_class, enum reg_class,
1388 machine_mode);
1389 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1390 enum reg_class,
1391 machine_mode);
1392 static bool rs6000_cannot_change_mode_class (machine_mode,
1393 machine_mode,
1394 enum reg_class);
1395 static bool rs6000_debug_cannot_change_mode_class (machine_mode,
1396 machine_mode,
1397 enum reg_class);
1398 static bool rs6000_save_toc_in_prologue_p (void);
1399 static rtx rs6000_internal_arg_pointer (void);
1401 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, machine_mode, int, int,
1402 int, int *)
1403 = rs6000_legitimize_reload_address;
1405 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1406 = rs6000_mode_dependent_address;
1408 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1409 machine_mode, rtx)
1410 = rs6000_secondary_reload_class;
1412 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1413 = rs6000_preferred_reload_class;
1415 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1416 machine_mode)
1417 = rs6000_secondary_memory_needed;
1419 bool (*rs6000_cannot_change_mode_class_ptr) (machine_mode,
1420 machine_mode,
1421 enum reg_class)
1422 = rs6000_cannot_change_mode_class;
1424 const int INSN_NOT_AVAILABLE = -1;
1426 static void rs6000_print_isa_options (FILE *, int, const char *,
1427 HOST_WIDE_INT);
1428 static void rs6000_print_builtin_options (FILE *, int, const char *,
1429 HOST_WIDE_INT);
1430 static HOST_WIDE_INT rs6000_disable_incompatible_switches (void);
1432 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1433 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1434 enum rs6000_reg_type,
1435 machine_mode,
1436 secondary_reload_info *,
1437 bool);
1438 rtl_opt_pass *make_pass_analyze_swaps (gcc::context*);
1439 static bool rs6000_keep_leaf_when_profiled () __attribute__ ((unused));
1440 static tree rs6000_fold_builtin (tree, int, tree *, bool);
1442 /* Hash table stuff for keeping track of TOC entries. */
1444 struct GTY((for_user)) toc_hash_struct
1446 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1447 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1448 rtx key;
1449 machine_mode key_mode;
1450 int labelno;
1453 struct toc_hasher : ggc_ptr_hash<toc_hash_struct>
1455 static hashval_t hash (toc_hash_struct *);
1456 static bool equal (toc_hash_struct *, toc_hash_struct *);
1459 static GTY (()) hash_table<toc_hasher> *toc_hash_table;
1461 /* Hash table to keep track of the argument types for builtin functions. */
1463 struct GTY((for_user)) builtin_hash_struct
1465 tree type;
1466 machine_mode mode[4]; /* return value + 3 arguments. */
1467 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1470 struct builtin_hasher : ggc_ptr_hash<builtin_hash_struct>
1472 static hashval_t hash (builtin_hash_struct *);
1473 static bool equal (builtin_hash_struct *, builtin_hash_struct *);
1476 static GTY (()) hash_table<builtin_hasher> *builtin_hash_table;
1479 /* Default register names. */
1480 char rs6000_reg_names[][8] =
1482 "0", "1", "2", "3", "4", "5", "6", "7",
1483 "8", "9", "10", "11", "12", "13", "14", "15",
1484 "16", "17", "18", "19", "20", "21", "22", "23",
1485 "24", "25", "26", "27", "28", "29", "30", "31",
1486 "0", "1", "2", "3", "4", "5", "6", "7",
1487 "8", "9", "10", "11", "12", "13", "14", "15",
1488 "16", "17", "18", "19", "20", "21", "22", "23",
1489 "24", "25", "26", "27", "28", "29", "30", "31",
1490 "mq", "lr", "ctr","ap",
1491 "0", "1", "2", "3", "4", "5", "6", "7",
1492 "ca",
1493 /* AltiVec registers. */
1494 "0", "1", "2", "3", "4", "5", "6", "7",
1495 "8", "9", "10", "11", "12", "13", "14", "15",
1496 "16", "17", "18", "19", "20", "21", "22", "23",
1497 "24", "25", "26", "27", "28", "29", "30", "31",
1498 "vrsave", "vscr",
1499 /* SPE registers. */
1500 "spe_acc", "spefscr",
1501 /* Soft frame pointer. */
1502 "sfp",
1503 /* HTM SPR registers. */
1504 "tfhar", "tfiar", "texasr",
1505 /* SPE High registers. */
1506 "0", "1", "2", "3", "4", "5", "6", "7",
1507 "8", "9", "10", "11", "12", "13", "14", "15",
1508 "16", "17", "18", "19", "20", "21", "22", "23",
1509 "24", "25", "26", "27", "28", "29", "30", "31"
1512 #ifdef TARGET_REGNAMES
1513 static const char alt_reg_names[][8] =
1515 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1516 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1517 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1518 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1519 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1520 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1521 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1522 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1523 "mq", "lr", "ctr", "ap",
1524 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1525 "ca",
1526 /* AltiVec registers. */
1527 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1528 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1529 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1530 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1531 "vrsave", "vscr",
1532 /* SPE registers. */
1533 "spe_acc", "spefscr",
1534 /* Soft frame pointer. */
1535 "sfp",
1536 /* HTM SPR registers. */
1537 "tfhar", "tfiar", "texasr",
1538 /* SPE High registers. */
1539 "%rh0", "%rh1", "%rh2", "%rh3", "%rh4", "%rh5", "%rh6", "%rh7",
1540 "%rh8", "%rh9", "%rh10", "%r11", "%rh12", "%rh13", "%rh14", "%rh15",
1541 "%rh16", "%rh17", "%rh18", "%rh19", "%rh20", "%rh21", "%rh22", "%rh23",
1542 "%rh24", "%rh25", "%rh26", "%rh27", "%rh28", "%rh29", "%rh30", "%rh31"
1544 #endif
1546 /* Table of valid machine attributes. */
1548 static const struct attribute_spec rs6000_attribute_table[] =
1550 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1551 affects_type_identity } */
1552 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1553 false },
1554 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1555 false },
1556 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1557 false },
1558 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1559 false },
1560 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1561 false },
1562 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1563 SUBTARGET_ATTRIBUTE_TABLE,
1564 #endif
1565 { NULL, 0, 0, false, false, false, NULL, false }
1568 #ifndef TARGET_PROFILE_KERNEL
1569 #define TARGET_PROFILE_KERNEL 0
1570 #endif
1572 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1573 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1575 /* Initialize the GCC target structure. */
1576 #undef TARGET_ATTRIBUTE_TABLE
1577 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1578 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1579 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1580 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1581 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1583 #undef TARGET_ASM_ALIGNED_DI_OP
1584 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1586 /* Default unaligned ops are only provided for ELF. Find the ops needed
1587 for non-ELF systems. */
1588 #ifndef OBJECT_FORMAT_ELF
1589 #if TARGET_XCOFF
1590 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1591 64-bit targets. */
1592 #undef TARGET_ASM_UNALIGNED_HI_OP
1593 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1594 #undef TARGET_ASM_UNALIGNED_SI_OP
1595 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1596 #undef TARGET_ASM_UNALIGNED_DI_OP
1597 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1598 #else
1599 /* For Darwin. */
1600 #undef TARGET_ASM_UNALIGNED_HI_OP
1601 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1602 #undef TARGET_ASM_UNALIGNED_SI_OP
1603 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1604 #undef TARGET_ASM_UNALIGNED_DI_OP
1605 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1606 #undef TARGET_ASM_ALIGNED_DI_OP
1607 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1608 #endif
1609 #endif
1611 /* This hook deals with fixups for relocatable code and DI-mode objects
1612 in 64-bit code. */
1613 #undef TARGET_ASM_INTEGER
1614 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1616 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1617 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1618 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1619 #endif
1621 #undef TARGET_SET_UP_BY_PROLOGUE
1622 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1624 #undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
1625 #define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS rs6000_get_separate_components
1626 #undef TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB
1627 #define TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB rs6000_components_for_bb
1628 #undef TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS
1629 #define TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS rs6000_disqualify_components
1630 #undef TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS
1631 #define TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS rs6000_emit_prologue_components
1632 #undef TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS
1633 #define TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS rs6000_emit_epilogue_components
1634 #undef TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS
1635 #define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS rs6000_set_handled_components
1637 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1638 #define TARGET_EXTRA_LIVE_ON_ENTRY rs6000_live_on_entry
1640 #undef TARGET_INTERNAL_ARG_POINTER
1641 #define TARGET_INTERNAL_ARG_POINTER rs6000_internal_arg_pointer
1643 #undef TARGET_HAVE_TLS
1644 #define TARGET_HAVE_TLS HAVE_AS_TLS
1646 #undef TARGET_CANNOT_FORCE_CONST_MEM
1647 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1649 #undef TARGET_DELEGITIMIZE_ADDRESS
1650 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1652 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1653 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1655 #undef TARGET_LEGITIMATE_COMBINED_INSN
1656 #define TARGET_LEGITIMATE_COMBINED_INSN rs6000_legitimate_combined_insn
1658 #undef TARGET_ASM_FUNCTION_PROLOGUE
1659 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1660 #undef TARGET_ASM_FUNCTION_EPILOGUE
1661 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1663 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1664 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1666 #undef TARGET_LEGITIMIZE_ADDRESS
1667 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1669 #undef TARGET_SCHED_VARIABLE_ISSUE
1670 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1672 #undef TARGET_SCHED_ISSUE_RATE
1673 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1674 #undef TARGET_SCHED_ADJUST_COST
1675 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1676 #undef TARGET_SCHED_ADJUST_PRIORITY
1677 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1678 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1679 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1680 #undef TARGET_SCHED_INIT
1681 #define TARGET_SCHED_INIT rs6000_sched_init
1682 #undef TARGET_SCHED_FINISH
1683 #define TARGET_SCHED_FINISH rs6000_sched_finish
1684 #undef TARGET_SCHED_REORDER
1685 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1686 #undef TARGET_SCHED_REORDER2
1687 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1689 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1690 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1692 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1693 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1695 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1696 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1697 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1698 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1699 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1700 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1701 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1702 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1704 #undef TARGET_SCHED_CAN_SPECULATE_INSN
1705 #define TARGET_SCHED_CAN_SPECULATE_INSN rs6000_sched_can_speculate_insn
1707 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1708 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1709 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1710 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1711 rs6000_builtin_support_vector_misalignment
1712 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1713 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1714 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1715 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1716 rs6000_builtin_vectorization_cost
1717 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1718 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1719 rs6000_preferred_simd_mode
1720 #undef TARGET_VECTORIZE_INIT_COST
1721 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1722 #undef TARGET_VECTORIZE_ADD_STMT_COST
1723 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1724 #undef TARGET_VECTORIZE_FINISH_COST
1725 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1726 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1727 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1729 #undef TARGET_INIT_BUILTINS
1730 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1731 #undef TARGET_BUILTIN_DECL
1732 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1734 #undef TARGET_FOLD_BUILTIN
1735 #define TARGET_FOLD_BUILTIN rs6000_fold_builtin
1736 #undef TARGET_GIMPLE_FOLD_BUILTIN
1737 #define TARGET_GIMPLE_FOLD_BUILTIN rs6000_gimple_fold_builtin
1739 #undef TARGET_EXPAND_BUILTIN
1740 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1742 #undef TARGET_MANGLE_TYPE
1743 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1745 #undef TARGET_INIT_LIBFUNCS
1746 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1748 #if TARGET_MACHO
1749 #undef TARGET_BINDS_LOCAL_P
1750 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1751 #endif
1753 #undef TARGET_MS_BITFIELD_LAYOUT_P
1754 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1756 #undef TARGET_ASM_OUTPUT_MI_THUNK
1757 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1759 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1760 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1762 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1763 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1765 #undef TARGET_REGISTER_MOVE_COST
1766 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1767 #undef TARGET_MEMORY_MOVE_COST
1768 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1769 #undef TARGET_CANNOT_COPY_INSN_P
1770 #define TARGET_CANNOT_COPY_INSN_P rs6000_cannot_copy_insn_p
1771 #undef TARGET_RTX_COSTS
1772 #define TARGET_RTX_COSTS rs6000_rtx_costs
1773 #undef TARGET_ADDRESS_COST
1774 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1776 #undef TARGET_DWARF_REGISTER_SPAN
1777 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1779 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1780 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1782 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1783 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1785 #undef TARGET_PROMOTE_FUNCTION_MODE
1786 #define TARGET_PROMOTE_FUNCTION_MODE rs6000_promote_function_mode
1788 #undef TARGET_RETURN_IN_MEMORY
1789 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1791 #undef TARGET_RETURN_IN_MSB
1792 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1794 #undef TARGET_SETUP_INCOMING_VARARGS
1795 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1797 /* Always strict argument naming on rs6000. */
1798 #undef TARGET_STRICT_ARGUMENT_NAMING
1799 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1800 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1801 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1802 #undef TARGET_SPLIT_COMPLEX_ARG
1803 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1804 #undef TARGET_MUST_PASS_IN_STACK
1805 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1806 #undef TARGET_PASS_BY_REFERENCE
1807 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1808 #undef TARGET_ARG_PARTIAL_BYTES
1809 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1810 #undef TARGET_FUNCTION_ARG_ADVANCE
1811 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1812 #undef TARGET_FUNCTION_ARG
1813 #define TARGET_FUNCTION_ARG rs6000_function_arg
1814 #undef TARGET_FUNCTION_ARG_PADDING
1815 #define TARGET_FUNCTION_ARG_PADDING rs6000_function_arg_padding
1816 #undef TARGET_FUNCTION_ARG_BOUNDARY
1817 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1819 #undef TARGET_BUILD_BUILTIN_VA_LIST
1820 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1822 #undef TARGET_EXPAND_BUILTIN_VA_START
1823 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1825 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1826 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1828 #undef TARGET_EH_RETURN_FILTER_MODE
1829 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1831 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1832 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1834 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1835 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1837 #undef TARGET_FLOATN_MODE
1838 #define TARGET_FLOATN_MODE rs6000_floatn_mode
1840 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1841 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1843 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1844 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1846 #undef TARGET_MD_ASM_ADJUST
1847 #define TARGET_MD_ASM_ADJUST rs6000_md_asm_adjust
1849 #undef TARGET_OPTION_OVERRIDE
1850 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1852 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1853 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1854 rs6000_builtin_vectorized_function
1856 #undef TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION
1857 #define TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION \
1858 rs6000_builtin_md_vectorized_function
1860 #undef TARGET_STACK_PROTECT_GUARD
1861 #define TARGET_STACK_PROTECT_GUARD rs6000_init_stack_protect_guard
1863 #if !TARGET_MACHO
1864 #undef TARGET_STACK_PROTECT_FAIL
1865 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1866 #endif
1868 #ifdef HAVE_AS_TLS
1869 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1870 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1871 #endif
1873 /* Use a 32-bit anchor range. This leads to sequences like:
1875 addis tmp,anchor,high
1876 add dest,tmp,low
1878 where tmp itself acts as an anchor, and can be shared between
1879 accesses to the same 64k page. */
1880 #undef TARGET_MIN_ANCHOR_OFFSET
1881 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1882 #undef TARGET_MAX_ANCHOR_OFFSET
1883 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1884 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1885 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1886 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1887 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1889 #undef TARGET_BUILTIN_RECIPROCAL
1890 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1892 #undef TARGET_EXPAND_TO_RTL_HOOK
1893 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1895 #undef TARGET_INSTANTIATE_DECLS
1896 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1898 #undef TARGET_SECONDARY_RELOAD
1899 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1901 #undef TARGET_LEGITIMATE_ADDRESS_P
1902 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1904 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1905 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1907 #undef TARGET_LRA_P
1908 #define TARGET_LRA_P rs6000_lra_p
1910 #undef TARGET_COMPUTE_PRESSURE_CLASSES
1911 #define TARGET_COMPUTE_PRESSURE_CLASSES rs6000_compute_pressure_classes
1913 #undef TARGET_CAN_ELIMINATE
1914 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1916 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1917 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1919 #undef TARGET_SCHED_REASSOCIATION_WIDTH
1920 #define TARGET_SCHED_REASSOCIATION_WIDTH rs6000_reassociation_width
1922 #undef TARGET_TRAMPOLINE_INIT
1923 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1925 #undef TARGET_FUNCTION_VALUE
1926 #define TARGET_FUNCTION_VALUE rs6000_function_value
1928 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1929 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1931 #undef TARGET_OPTION_SAVE
1932 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1934 #undef TARGET_OPTION_RESTORE
1935 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1937 #undef TARGET_OPTION_PRINT
1938 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1940 #undef TARGET_CAN_INLINE_P
1941 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1943 #undef TARGET_SET_CURRENT_FUNCTION
1944 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1946 #undef TARGET_LEGITIMATE_CONSTANT_P
1947 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1949 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1950 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1952 #undef TARGET_CAN_USE_DOLOOP_P
1953 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1955 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
1956 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV rs6000_atomic_assign_expand_fenv
1958 #undef TARGET_LIBGCC_CMP_RETURN_MODE
1959 #define TARGET_LIBGCC_CMP_RETURN_MODE rs6000_abi_word_mode
1960 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
1961 #define TARGET_LIBGCC_SHIFT_COUNT_MODE rs6000_abi_word_mode
1962 #undef TARGET_UNWIND_WORD_MODE
1963 #define TARGET_UNWIND_WORD_MODE rs6000_abi_word_mode
1965 #undef TARGET_OFFLOAD_OPTIONS
1966 #define TARGET_OFFLOAD_OPTIONS rs6000_offload_options
1968 #undef TARGET_C_MODE_FOR_SUFFIX
1969 #define TARGET_C_MODE_FOR_SUFFIX rs6000_c_mode_for_suffix
1971 #undef TARGET_INVALID_BINARY_OP
1972 #define TARGET_INVALID_BINARY_OP rs6000_invalid_binary_op
1974 #undef TARGET_OPTAB_SUPPORTED_P
1975 #define TARGET_OPTAB_SUPPORTED_P rs6000_optab_supported_p
1977 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
1978 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
1980 #undef TARGET_HARD_REGNO_MODE_OK
1981 #define TARGET_HARD_REGNO_MODE_OK rs6000_hard_regno_mode_ok
1983 #undef TARGET_MODES_TIEABLE_P
1984 #define TARGET_MODES_TIEABLE_P rs6000_modes_tieable_p
1986 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
1987 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
1988 rs6000_hard_regno_call_part_clobbered
1991 /* Processor table. */
1992 struct rs6000_ptt
1994 const char *const name; /* Canonical processor name. */
1995 const enum processor_type processor; /* Processor type enum value. */
1996 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1999 static struct rs6000_ptt const processor_target_table[] =
2001 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
2002 #include "powerpcspe-cpus.def"
2003 #undef RS6000_CPU
2006 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
2007 name is invalid. */
2009 static int
2010 rs6000_cpu_name_lookup (const char *name)
2012 size_t i;
2014 if (name != NULL)
2016 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2017 if (! strcmp (name, processor_target_table[i].name))
2018 return (int)i;
2021 return -1;
2025 /* Return number of consecutive hard regs needed starting at reg REGNO
2026 to hold something of mode MODE.
2027 This is ordinarily the length in words of a value of mode MODE
2028 but can be less for certain modes in special long registers.
2030 For the SPE, GPRs are 64 bits but only 32 bits are visible in
2031 scalar instructions. The upper 32 bits are only available to the
2032 SIMD instructions.
2034 POWER and PowerPC GPRs hold 32 bits worth;
2035 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
2037 static int
2038 rs6000_hard_regno_nregs_internal (int regno, machine_mode mode)
2040 unsigned HOST_WIDE_INT reg_size;
2042 /* 128-bit floating point usually takes 2 registers, unless it is IEEE
2043 128-bit floating point that can go in vector registers, which has VSX
2044 memory addressing. */
2045 if (FP_REGNO_P (regno))
2046 reg_size = (VECTOR_MEM_VSX_P (mode) || FLOAT128_VECTOR_P (mode)
2047 ? UNITS_PER_VSX_WORD
2048 : UNITS_PER_FP_WORD);
2050 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
2051 reg_size = UNITS_PER_SPE_WORD;
2053 else if (ALTIVEC_REGNO_P (regno))
2054 reg_size = UNITS_PER_ALTIVEC_WORD;
2056 /* The value returned for SCmode in the E500 double case is 2 for
2057 ABI compatibility; storing an SCmode value in a single register
2058 would require function_arg and rs6000_spe_function_arg to handle
2059 SCmode so as to pass the value correctly in a pair of
2060 registers. */
2061 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
2062 && !DECIMAL_FLOAT_MODE_P (mode) && SPE_SIMD_REGNO_P (regno))
2063 reg_size = UNITS_PER_FP_WORD;
2065 else
2066 reg_size = UNITS_PER_WORD;
2068 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
2071 /* Value is 1 if hard register REGNO can hold a value of machine-mode
2072 MODE. */
2073 static int
2074 rs6000_hard_regno_mode_ok_uncached (int regno, machine_mode mode)
2076 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
2078 if (COMPLEX_MODE_P (mode))
2079 mode = GET_MODE_INNER (mode);
2081 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
2082 register combinations, and use PTImode where we need to deal with quad
2083 word memory operations. Don't allow quad words in the argument or frame
2084 pointer registers, just registers 0..31. */
2085 if (mode == PTImode)
2086 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2087 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2088 && ((regno & 1) == 0));
2090 /* VSX registers that overlap the FPR registers are larger than for non-VSX
2091 implementations. Don't allow an item to be split between a FP register
2092 and an Altivec register. Allow TImode in all VSX registers if the user
2093 asked for it. */
2094 if (TARGET_VSX && VSX_REGNO_P (regno)
2095 && (VECTOR_MEM_VSX_P (mode)
2096 || FLOAT128_VECTOR_P (mode)
2097 || reg_addr[mode].scalar_in_vmx_p
2098 || (TARGET_VSX_TIMODE && mode == TImode)
2099 || (TARGET_VADDUQM && mode == V1TImode)))
2101 if (FP_REGNO_P (regno))
2102 return FP_REGNO_P (last_regno);
2104 if (ALTIVEC_REGNO_P (regno))
2106 if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
2107 return 0;
2109 return ALTIVEC_REGNO_P (last_regno);
2113 /* The GPRs can hold any mode, but values bigger than one register
2114 cannot go past R31. */
2115 if (INT_REGNO_P (regno))
2116 return INT_REGNO_P (last_regno);
2118 /* The float registers (except for VSX vector modes) can only hold floating
2119 modes and DImode. */
2120 if (FP_REGNO_P (regno))
2122 if (FLOAT128_VECTOR_P (mode))
2123 return false;
2125 if (SCALAR_FLOAT_MODE_P (mode)
2126 && (mode != TDmode || (regno % 2) == 0)
2127 && FP_REGNO_P (last_regno))
2128 return 1;
2130 if (GET_MODE_CLASS (mode) == MODE_INT)
2132 if(GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
2133 return 1;
2135 if (TARGET_VSX_SMALL_INTEGER)
2137 if (mode == SImode)
2138 return 1;
2140 if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
2141 return 1;
2145 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
2146 && PAIRED_VECTOR_MODE (mode))
2147 return 1;
2149 return 0;
2152 /* The CR register can only hold CC modes. */
2153 if (CR_REGNO_P (regno))
2154 return GET_MODE_CLASS (mode) == MODE_CC;
2156 if (CA_REGNO_P (regno))
2157 return mode == Pmode || mode == SImode;
2159 /* AltiVec only in AldyVec registers. */
2160 if (ALTIVEC_REGNO_P (regno))
2161 return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
2162 || mode == V1TImode);
2164 /* ...but GPRs can hold SIMD data on the SPE in one register. */
2165 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
2166 return 1;
2168 /* We cannot put non-VSX TImode or PTImode anywhere except general register
2169 and it must be able to fit within the register set. */
2171 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
2174 /* Implement TARGET_HARD_REGNO_MODE_OK. */
2176 static bool
2177 rs6000_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2179 return rs6000_hard_regno_mode_ok_p[mode][regno];
2182 /* Implement TARGET_MODES_TIEABLE_P.
2184 PTImode cannot tie with other modes because PTImode is restricted to even
2185 GPR registers, and TImode can go in any GPR as well as VSX registers (PR
2186 57744).
2188 Altivec/VSX vector tests were moved ahead of scalar float mode, so that IEEE
2189 128-bit floating point on VSX systems ties with other vectors. */
2191 static bool
2192 rs6000_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2194 if (mode1 == PTImode)
2195 return mode2 == PTImode;
2196 if (mode2 == PTImode)
2197 return false;
2199 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode1))
2200 return ALTIVEC_OR_VSX_VECTOR_MODE (mode2);
2201 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode2))
2202 return false;
2204 if (SCALAR_FLOAT_MODE_P (mode1))
2205 return SCALAR_FLOAT_MODE_P (mode2);
2206 if (SCALAR_FLOAT_MODE_P (mode2))
2207 return false;
2209 if (GET_MODE_CLASS (mode1) == MODE_CC)
2210 return GET_MODE_CLASS (mode2) == MODE_CC;
2211 if (GET_MODE_CLASS (mode2) == MODE_CC)
2212 return false;
2214 if (SPE_VECTOR_MODE (mode1))
2215 return SPE_VECTOR_MODE (mode2);
2216 if (SPE_VECTOR_MODE (mode2))
2217 return false;
2219 return true;
2222 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED. */
2224 static bool
2225 rs6000_hard_regno_call_part_clobbered (unsigned int regno, machine_mode mode)
2227 if (TARGET_32BIT
2228 && TARGET_POWERPC64
2229 && GET_MODE_SIZE (mode) > 4
2230 && INT_REGNO_P (regno))
2231 return true;
2233 if (TARGET_VSX
2234 && FP_REGNO_P (regno)
2235 && GET_MODE_SIZE (mode) > 8
2236 && !FLOAT128_2REG_P (mode))
2237 return true;
2239 return false;
2242 /* Print interesting facts about registers. */
2243 static void
2244 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
2246 int r, m;
2248 for (r = first_regno; r <= last_regno; ++r)
2250 const char *comma = "";
2251 int len;
2253 if (first_regno == last_regno)
2254 fprintf (stderr, "%s:\t", reg_name);
2255 else
2256 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
2258 len = 8;
2259 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2260 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
2262 if (len > 70)
2264 fprintf (stderr, ",\n\t");
2265 len = 8;
2266 comma = "";
2269 if (rs6000_hard_regno_nregs[m][r] > 1)
2270 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
2271 rs6000_hard_regno_nregs[m][r]);
2272 else
2273 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
2275 comma = ", ";
2278 if (call_used_regs[r])
2280 if (len > 70)
2282 fprintf (stderr, ",\n\t");
2283 len = 8;
2284 comma = "";
2287 len += fprintf (stderr, "%s%s", comma, "call-used");
2288 comma = ", ";
2291 if (fixed_regs[r])
2293 if (len > 70)
2295 fprintf (stderr, ",\n\t");
2296 len = 8;
2297 comma = "";
2300 len += fprintf (stderr, "%s%s", comma, "fixed");
2301 comma = ", ";
2304 if (len > 70)
2306 fprintf (stderr, ",\n\t");
2307 comma = "";
2310 len += fprintf (stderr, "%sreg-class = %s", comma,
2311 reg_class_names[(int)rs6000_regno_regclass[r]]);
2312 comma = ", ";
2314 if (len > 70)
2316 fprintf (stderr, ",\n\t");
2317 comma = "";
2320 fprintf (stderr, "%sregno = %d\n", comma, r);
2324 static const char *
2325 rs6000_debug_vector_unit (enum rs6000_vector v)
2327 const char *ret;
2329 switch (v)
2331 case VECTOR_NONE: ret = "none"; break;
2332 case VECTOR_ALTIVEC: ret = "altivec"; break;
2333 case VECTOR_VSX: ret = "vsx"; break;
2334 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
2335 case VECTOR_PAIRED: ret = "paired"; break;
2336 case VECTOR_SPE: ret = "spe"; break;
2337 case VECTOR_OTHER: ret = "other"; break;
2338 default: ret = "unknown"; break;
2341 return ret;
2344 /* Inner function printing just the address mask for a particular reload
2345 register class. */
2346 DEBUG_FUNCTION char *
2347 rs6000_debug_addr_mask (addr_mask_type mask, bool keep_spaces)
2349 static char ret[8];
2350 char *p = ret;
2352 if ((mask & RELOAD_REG_VALID) != 0)
2353 *p++ = 'v';
2354 else if (keep_spaces)
2355 *p++ = ' ';
2357 if ((mask & RELOAD_REG_MULTIPLE) != 0)
2358 *p++ = 'm';
2359 else if (keep_spaces)
2360 *p++ = ' ';
2362 if ((mask & RELOAD_REG_INDEXED) != 0)
2363 *p++ = 'i';
2364 else if (keep_spaces)
2365 *p++ = ' ';
2367 if ((mask & RELOAD_REG_QUAD_OFFSET) != 0)
2368 *p++ = 'O';
2369 else if ((mask & RELOAD_REG_OFFSET) != 0)
2370 *p++ = 'o';
2371 else if (keep_spaces)
2372 *p++ = ' ';
2374 if ((mask & RELOAD_REG_PRE_INCDEC) != 0)
2375 *p++ = '+';
2376 else if (keep_spaces)
2377 *p++ = ' ';
2379 if ((mask & RELOAD_REG_PRE_MODIFY) != 0)
2380 *p++ = '+';
2381 else if (keep_spaces)
2382 *p++ = ' ';
2384 if ((mask & RELOAD_REG_AND_M16) != 0)
2385 *p++ = '&';
2386 else if (keep_spaces)
2387 *p++ = ' ';
2389 *p = '\0';
2391 return ret;
2394 /* Print the address masks in a human readble fashion. */
2395 DEBUG_FUNCTION void
2396 rs6000_debug_print_mode (ssize_t m)
2398 ssize_t rc;
2399 int spaces = 0;
2400 bool fuse_extra_p;
2402 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
2403 for (rc = 0; rc < N_RELOAD_REG; rc++)
2404 fprintf (stderr, " %s: %s", reload_reg_map[rc].name,
2405 rs6000_debug_addr_mask (reg_addr[m].addr_mask[rc], true));
2407 if ((reg_addr[m].reload_store != CODE_FOR_nothing)
2408 || (reg_addr[m].reload_load != CODE_FOR_nothing))
2409 fprintf (stderr, " Reload=%c%c",
2410 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
2411 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
2412 else
2413 spaces += sizeof (" Reload=sl") - 1;
2415 if (reg_addr[m].scalar_in_vmx_p)
2417 fprintf (stderr, "%*s Upper=y", spaces, "");
2418 spaces = 0;
2420 else
2421 spaces += sizeof (" Upper=y") - 1;
2423 fuse_extra_p = ((reg_addr[m].fusion_gpr_ld != CODE_FOR_nothing)
2424 || reg_addr[m].fused_toc);
2425 if (!fuse_extra_p)
2427 for (rc = 0; rc < N_RELOAD_REG; rc++)
2429 if (rc != RELOAD_REG_ANY)
2431 if (reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing
2432 || reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing
2433 || reg_addr[m].fusion_addi_st[rc] != CODE_FOR_nothing
2434 || reg_addr[m].fusion_addis_ld[rc] != CODE_FOR_nothing
2435 || reg_addr[m].fusion_addis_st[rc] != CODE_FOR_nothing)
2437 fuse_extra_p = true;
2438 break;
2444 if (fuse_extra_p)
2446 fprintf (stderr, "%*s Fuse:", spaces, "");
2447 spaces = 0;
2449 for (rc = 0; rc < N_RELOAD_REG; rc++)
2451 if (rc != RELOAD_REG_ANY)
2453 char load, store;
2455 if (reg_addr[m].fusion_addis_ld[rc] != CODE_FOR_nothing)
2456 load = 'l';
2457 else if (reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing)
2458 load = 'L';
2459 else
2460 load = '-';
2462 if (reg_addr[m].fusion_addis_st[rc] != CODE_FOR_nothing)
2463 store = 's';
2464 else if (reg_addr[m].fusion_addi_st[rc] != CODE_FOR_nothing)
2465 store = 'S';
2466 else
2467 store = '-';
2469 if (load == '-' && store == '-')
2470 spaces += 5;
2471 else
2473 fprintf (stderr, "%*s%c=%c%c", (spaces + 1), "",
2474 reload_reg_map[rc].name[0], load, store);
2475 spaces = 0;
2480 if (reg_addr[m].fusion_gpr_ld != CODE_FOR_nothing)
2482 fprintf (stderr, "%*sP8gpr", (spaces + 1), "");
2483 spaces = 0;
2485 else
2486 spaces += sizeof (" P8gpr") - 1;
2488 if (reg_addr[m].fused_toc)
2490 fprintf (stderr, "%*sToc", (spaces + 1), "");
2491 spaces = 0;
2493 else
2494 spaces += sizeof (" Toc") - 1;
2496 else
2497 spaces += sizeof (" Fuse: G=ls F=ls v=ls P8gpr Toc") - 1;
2499 if (rs6000_vector_unit[m] != VECTOR_NONE
2500 || rs6000_vector_mem[m] != VECTOR_NONE)
2502 fprintf (stderr, "%*s vector: arith=%-10s mem=%s",
2503 spaces, "",
2504 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
2505 rs6000_debug_vector_unit (rs6000_vector_mem[m]));
2508 fputs ("\n", stderr);
2511 #define DEBUG_FMT_ID "%-32s= "
2512 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
2513 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
2514 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
2516 /* Print various interesting information with -mdebug=reg. */
2517 static void
2518 rs6000_debug_reg_global (void)
2520 static const char *const tf[2] = { "false", "true" };
2521 const char *nl = (const char *)0;
2522 int m;
2523 size_t m1, m2, v;
2524 char costly_num[20];
2525 char nop_num[20];
2526 char flags_buffer[40];
2527 const char *costly_str;
2528 const char *nop_str;
2529 const char *trace_str;
2530 const char *abi_str;
2531 const char *cmodel_str;
2532 struct cl_target_option cl_opts;
2534 /* Modes we want tieable information on. */
2535 static const machine_mode print_tieable_modes[] = {
2536 QImode,
2537 HImode,
2538 SImode,
2539 DImode,
2540 TImode,
2541 PTImode,
2542 SFmode,
2543 DFmode,
2544 TFmode,
2545 IFmode,
2546 KFmode,
2547 SDmode,
2548 DDmode,
2549 TDmode,
2550 V8QImode,
2551 V4HImode,
2552 V2SImode,
2553 V16QImode,
2554 V8HImode,
2555 V4SImode,
2556 V2DImode,
2557 V1TImode,
2558 V32QImode,
2559 V16HImode,
2560 V8SImode,
2561 V4DImode,
2562 V2TImode,
2563 V2SFmode,
2564 V4SFmode,
2565 V2DFmode,
2566 V8SFmode,
2567 V4DFmode,
2568 CCmode,
2569 CCUNSmode,
2570 CCEQmode,
2573 /* Virtual regs we are interested in. */
2574 const static struct {
2575 int regno; /* register number. */
2576 const char *name; /* register name. */
2577 } virtual_regs[] = {
2578 { STACK_POINTER_REGNUM, "stack pointer:" },
2579 { TOC_REGNUM, "toc: " },
2580 { STATIC_CHAIN_REGNUM, "static chain: " },
2581 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
2582 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
2583 { ARG_POINTER_REGNUM, "arg pointer: " },
2584 { FRAME_POINTER_REGNUM, "frame pointer:" },
2585 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
2586 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
2587 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
2588 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
2589 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
2590 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
2591 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
2592 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
2593 { LAST_VIRTUAL_REGISTER, "last virtual: " },
2596 fputs ("\nHard register information:\n", stderr);
2597 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2598 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2599 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2600 LAST_ALTIVEC_REGNO,
2601 "vs");
2602 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2603 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2604 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2605 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2606 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2607 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2608 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
2609 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
2611 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2612 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2613 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2615 fprintf (stderr,
2616 "\n"
2617 "d reg_class = %s\n"
2618 "f reg_class = %s\n"
2619 "v reg_class = %s\n"
2620 "wa reg_class = %s\n"
2621 "wb reg_class = %s\n"
2622 "wd reg_class = %s\n"
2623 "we reg_class = %s\n"
2624 "wf reg_class = %s\n"
2625 "wg reg_class = %s\n"
2626 "wh reg_class = %s\n"
2627 "wi reg_class = %s\n"
2628 "wj reg_class = %s\n"
2629 "wk reg_class = %s\n"
2630 "wl reg_class = %s\n"
2631 "wm reg_class = %s\n"
2632 "wo reg_class = %s\n"
2633 "wp reg_class = %s\n"
2634 "wq reg_class = %s\n"
2635 "wr reg_class = %s\n"
2636 "ws reg_class = %s\n"
2637 "wt reg_class = %s\n"
2638 "wu reg_class = %s\n"
2639 "wv reg_class = %s\n"
2640 "ww reg_class = %s\n"
2641 "wx reg_class = %s\n"
2642 "wy reg_class = %s\n"
2643 "wz reg_class = %s\n"
2644 "wA reg_class = %s\n"
2645 "wH reg_class = %s\n"
2646 "wI reg_class = %s\n"
2647 "wJ reg_class = %s\n"
2648 "wK reg_class = %s\n"
2649 "\n",
2650 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2651 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2652 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2653 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2654 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wb]],
2655 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2656 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_we]],
2657 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2658 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2659 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wh]],
2660 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wi]],
2661 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wj]],
2662 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wk]],
2663 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2664 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2665 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wo]],
2666 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wp]],
2667 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wq]],
2668 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2669 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2670 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2671 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2672 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2673 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2674 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2675 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2676 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]],
2677 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wA]],
2678 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wH]],
2679 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wI]],
2680 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wJ]],
2681 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wK]]);
2683 nl = "\n";
2684 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2685 rs6000_debug_print_mode (m);
2687 fputs ("\n", stderr);
2689 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2691 machine_mode mode1 = print_tieable_modes[m1];
2692 bool first_time = true;
2694 nl = (const char *)0;
2695 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2697 machine_mode mode2 = print_tieable_modes[m2];
2698 if (mode1 != mode2 && rs6000_modes_tieable_p (mode1, mode2))
2700 if (first_time)
2702 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2703 nl = "\n";
2704 first_time = false;
2707 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2711 if (!first_time)
2712 fputs ("\n", stderr);
2715 if (nl)
2716 fputs (nl, stderr);
2718 if (rs6000_recip_control)
2720 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2722 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2723 if (rs6000_recip_bits[m])
2725 fprintf (stderr,
2726 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2727 GET_MODE_NAME (m),
2728 (RS6000_RECIP_AUTO_RE_P (m)
2729 ? "auto"
2730 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2731 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2732 ? "auto"
2733 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2736 fputs ("\n", stderr);
2739 if (rs6000_cpu_index >= 0)
2741 const char *name = processor_target_table[rs6000_cpu_index].name;
2742 HOST_WIDE_INT flags
2743 = processor_target_table[rs6000_cpu_index].target_enable;
2745 sprintf (flags_buffer, "-mcpu=%s flags", name);
2746 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2748 else
2749 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2751 if (rs6000_tune_index >= 0)
2753 const char *name = processor_target_table[rs6000_tune_index].name;
2754 HOST_WIDE_INT flags
2755 = processor_target_table[rs6000_tune_index].target_enable;
2757 sprintf (flags_buffer, "-mtune=%s flags", name);
2758 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2760 else
2761 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2763 cl_target_option_save (&cl_opts, &global_options);
2764 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2765 rs6000_isa_flags);
2767 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2768 rs6000_isa_flags_explicit);
2770 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2771 rs6000_builtin_mask);
2773 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2775 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2776 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2778 switch (rs6000_sched_costly_dep)
2780 case max_dep_latency:
2781 costly_str = "max_dep_latency";
2782 break;
2784 case no_dep_costly:
2785 costly_str = "no_dep_costly";
2786 break;
2788 case all_deps_costly:
2789 costly_str = "all_deps_costly";
2790 break;
2792 case true_store_to_load_dep_costly:
2793 costly_str = "true_store_to_load_dep_costly";
2794 break;
2796 case store_to_load_dep_costly:
2797 costly_str = "store_to_load_dep_costly";
2798 break;
2800 default:
2801 costly_str = costly_num;
2802 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2803 break;
2806 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2808 switch (rs6000_sched_insert_nops)
2810 case sched_finish_regroup_exact:
2811 nop_str = "sched_finish_regroup_exact";
2812 break;
2814 case sched_finish_pad_groups:
2815 nop_str = "sched_finish_pad_groups";
2816 break;
2818 case sched_finish_none:
2819 nop_str = "sched_finish_none";
2820 break;
2822 default:
2823 nop_str = nop_num;
2824 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2825 break;
2828 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2830 switch (rs6000_sdata)
2832 default:
2833 case SDATA_NONE:
2834 break;
2836 case SDATA_DATA:
2837 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2838 break;
2840 case SDATA_SYSV:
2841 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2842 break;
2844 case SDATA_EABI:
2845 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2846 break;
2850 switch (rs6000_traceback)
2852 case traceback_default: trace_str = "default"; break;
2853 case traceback_none: trace_str = "none"; break;
2854 case traceback_part: trace_str = "part"; break;
2855 case traceback_full: trace_str = "full"; break;
2856 default: trace_str = "unknown"; break;
2859 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2861 switch (rs6000_current_cmodel)
2863 case CMODEL_SMALL: cmodel_str = "small"; break;
2864 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2865 case CMODEL_LARGE: cmodel_str = "large"; break;
2866 default: cmodel_str = "unknown"; break;
2869 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2871 switch (rs6000_current_abi)
2873 case ABI_NONE: abi_str = "none"; break;
2874 case ABI_AIX: abi_str = "aix"; break;
2875 case ABI_ELFv2: abi_str = "ELFv2"; break;
2876 case ABI_V4: abi_str = "V4"; break;
2877 case ABI_DARWIN: abi_str = "darwin"; break;
2878 default: abi_str = "unknown"; break;
2881 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2883 if (rs6000_altivec_abi)
2884 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2886 if (rs6000_spe_abi)
2887 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2889 if (rs6000_darwin64_abi)
2890 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2892 if (rs6000_float_gprs)
2893 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2895 fprintf (stderr, DEBUG_FMT_S, "fprs",
2896 (TARGET_FPRS ? "true" : "false"));
2898 fprintf (stderr, DEBUG_FMT_S, "single_float",
2899 (TARGET_SINGLE_FLOAT ? "true" : "false"));
2901 fprintf (stderr, DEBUG_FMT_S, "double_float",
2902 (TARGET_DOUBLE_FLOAT ? "true" : "false"));
2904 fprintf (stderr, DEBUG_FMT_S, "soft_float",
2905 (TARGET_SOFT_FLOAT ? "true" : "false"));
2907 fprintf (stderr, DEBUG_FMT_S, "e500_single",
2908 (TARGET_E500_SINGLE ? "true" : "false"));
2910 fprintf (stderr, DEBUG_FMT_S, "e500_double",
2911 (TARGET_E500_DOUBLE ? "true" : "false"));
2913 if (TARGET_LINK_STACK)
2914 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2916 fprintf (stderr, DEBUG_FMT_S, "lra", TARGET_LRA ? "true" : "false");
2918 if (TARGET_P8_FUSION)
2920 char options[80];
2922 strcpy (options, (TARGET_P9_FUSION) ? "power9" : "power8");
2923 if (TARGET_TOC_FUSION)
2924 strcat (options, ", toc");
2926 if (TARGET_P8_FUSION_SIGN)
2927 strcat (options, ", sign");
2929 fprintf (stderr, DEBUG_FMT_S, "fusion", options);
2932 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2933 TARGET_SECURE_PLT ? "secure" : "bss");
2934 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2935 aix_struct_return ? "aix" : "sysv");
2936 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2937 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2938 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2939 tf[!!rs6000_align_branch_targets]);
2940 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2941 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2942 rs6000_long_double_type_size);
2943 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2944 (int)rs6000_sched_restricted_insns_priority);
2945 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2946 (int)END_BUILTINS);
2947 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2948 (int)RS6000_BUILTIN_COUNT);
2950 fprintf (stderr, DEBUG_FMT_D, "Enable float128 on VSX",
2951 (int)TARGET_FLOAT128_ENABLE_TYPE);
2953 if (TARGET_VSX)
2954 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2955 (int)VECTOR_ELEMENT_SCALAR_64BIT);
2957 if (TARGET_DIRECT_MOVE_128)
2958 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit mfvsrld element",
2959 (int)VECTOR_ELEMENT_MFVSRLD_64BIT);
2963 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2964 legitimate address support to figure out the appropriate addressing to
2965 use. */
2967 static void
2968 rs6000_setup_reg_addr_masks (void)
2970 ssize_t rc, reg, m, nregs;
2971 addr_mask_type any_addr_mask, addr_mask;
2973 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2975 machine_mode m2 = (machine_mode) m;
2976 bool complex_p = false;
2977 bool small_int_p = (m2 == QImode || m2 == HImode || m2 == SImode);
2978 size_t msize;
2980 if (COMPLEX_MODE_P (m2))
2982 complex_p = true;
2983 m2 = GET_MODE_INNER (m2);
2986 msize = GET_MODE_SIZE (m2);
2988 /* SDmode is special in that we want to access it only via REG+REG
2989 addressing on power7 and above, since we want to use the LFIWZX and
2990 STFIWZX instructions to load it. */
2991 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2993 any_addr_mask = 0;
2994 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2996 addr_mask = 0;
2997 reg = reload_reg_map[rc].reg;
2999 /* Can mode values go in the GPR/FPR/Altivec registers? */
3000 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
3002 bool small_int_vsx_p = (small_int_p
3003 && (rc == RELOAD_REG_FPR
3004 || rc == RELOAD_REG_VMX));
3006 nregs = rs6000_hard_regno_nregs[m][reg];
3007 addr_mask |= RELOAD_REG_VALID;
3009 /* Indicate if the mode takes more than 1 physical register. If
3010 it takes a single register, indicate it can do REG+REG
3011 addressing. Small integers in VSX registers can only do
3012 REG+REG addressing. */
3013 if (small_int_vsx_p)
3014 addr_mask |= RELOAD_REG_INDEXED;
3015 else if (nregs > 1 || m == BLKmode || complex_p)
3016 addr_mask |= RELOAD_REG_MULTIPLE;
3017 else
3018 addr_mask |= RELOAD_REG_INDEXED;
3020 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
3021 addressing. Restrict addressing on SPE for 64-bit types
3022 because of the SUBREG hackery used to address 64-bit floats in
3023 '32-bit' GPRs. If we allow scalars into Altivec registers,
3024 don't allow PRE_INC, PRE_DEC, or PRE_MODIFY. */
3026 if (TARGET_UPDATE
3027 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
3028 && msize <= 8
3029 && !VECTOR_MODE_P (m2)
3030 && !FLOAT128_VECTOR_P (m2)
3031 && !complex_p
3032 && !small_int_vsx_p
3033 && (m2 != DFmode || !TARGET_UPPER_REGS_DF)
3034 && (m2 != SFmode || !TARGET_UPPER_REGS_SF)
3035 && !(TARGET_E500_DOUBLE && msize == 8))
3037 addr_mask |= RELOAD_REG_PRE_INCDEC;
3039 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
3040 we don't allow PRE_MODIFY for some multi-register
3041 operations. */
3042 switch (m)
3044 default:
3045 addr_mask |= RELOAD_REG_PRE_MODIFY;
3046 break;
3048 case E_DImode:
3049 if (TARGET_POWERPC64)
3050 addr_mask |= RELOAD_REG_PRE_MODIFY;
3051 break;
3053 case E_DFmode:
3054 case E_DDmode:
3055 if (TARGET_DF_INSN)
3056 addr_mask |= RELOAD_REG_PRE_MODIFY;
3057 break;
3062 /* GPR and FPR registers can do REG+OFFSET addressing, except
3063 possibly for SDmode. ISA 3.0 (i.e. power9) adds D-form addressing
3064 for 64-bit scalars and 32-bit SFmode to altivec registers. */
3065 if ((addr_mask != 0) && !indexed_only_p
3066 && msize <= 8
3067 && (rc == RELOAD_REG_GPR
3068 || ((msize == 8 || m2 == SFmode)
3069 && (rc == RELOAD_REG_FPR
3070 || (rc == RELOAD_REG_VMX
3071 && TARGET_P9_DFORM_SCALAR)))))
3072 addr_mask |= RELOAD_REG_OFFSET;
3074 /* VSX registers can do REG+OFFSET addresssing if ISA 3.0
3075 instructions are enabled. The offset for 128-bit VSX registers is
3076 only 12-bits. While GPRs can handle the full offset range, VSX
3077 registers can only handle the restricted range. */
3078 else if ((addr_mask != 0) && !indexed_only_p
3079 && msize == 16 && TARGET_P9_DFORM_VECTOR
3080 && (ALTIVEC_OR_VSX_VECTOR_MODE (m2)
3081 || (m2 == TImode && TARGET_VSX_TIMODE)))
3083 addr_mask |= RELOAD_REG_OFFSET;
3084 if (rc == RELOAD_REG_FPR || rc == RELOAD_REG_VMX)
3085 addr_mask |= RELOAD_REG_QUAD_OFFSET;
3088 /* VMX registers can do (REG & -16) and ((REG+REG) & -16)
3089 addressing on 128-bit types. */
3090 if (rc == RELOAD_REG_VMX && msize == 16
3091 && (addr_mask & RELOAD_REG_VALID) != 0)
3092 addr_mask |= RELOAD_REG_AND_M16;
3094 reg_addr[m].addr_mask[rc] = addr_mask;
3095 any_addr_mask |= addr_mask;
3098 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
3103 /* Initialize the various global tables that are based on register size. */
3104 static void
3105 rs6000_init_hard_regno_mode_ok (bool global_init_p)
3107 ssize_t r, m, c;
3108 int align64;
3109 int align32;
3111 /* Precalculate REGNO_REG_CLASS. */
3112 rs6000_regno_regclass[0] = GENERAL_REGS;
3113 for (r = 1; r < 32; ++r)
3114 rs6000_regno_regclass[r] = BASE_REGS;
3116 for (r = 32; r < 64; ++r)
3117 rs6000_regno_regclass[r] = FLOAT_REGS;
3119 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
3120 rs6000_regno_regclass[r] = NO_REGS;
3122 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
3123 rs6000_regno_regclass[r] = ALTIVEC_REGS;
3125 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
3126 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
3127 rs6000_regno_regclass[r] = CR_REGS;
3129 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
3130 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
3131 rs6000_regno_regclass[CA_REGNO] = NO_REGS;
3132 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
3133 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
3134 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
3135 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
3136 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
3137 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
3138 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
3139 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
3140 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
3142 /* Precalculate register class to simpler reload register class. We don't
3143 need all of the register classes that are combinations of different
3144 classes, just the simple ones that have constraint letters. */
3145 for (c = 0; c < N_REG_CLASSES; c++)
3146 reg_class_to_reg_type[c] = NO_REG_TYPE;
3148 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
3149 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
3150 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
3151 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
3152 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
3153 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
3154 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
3155 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
3156 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
3157 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
3158 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
3159 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
3161 if (TARGET_VSX)
3163 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
3164 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
3166 else
3168 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
3169 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
3172 /* Precalculate the valid memory formats as well as the vector information,
3173 this must be set up before the rs6000_hard_regno_nregs_internal calls
3174 below. */
3175 gcc_assert ((int)VECTOR_NONE == 0);
3176 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
3177 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
3179 gcc_assert ((int)CODE_FOR_nothing == 0);
3180 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
3182 gcc_assert ((int)NO_REGS == 0);
3183 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
3185 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
3186 believes it can use native alignment or still uses 128-bit alignment. */
3187 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
3189 align64 = 64;
3190 align32 = 32;
3192 else
3194 align64 = 128;
3195 align32 = 128;
3198 /* KF mode (IEEE 128-bit in VSX registers). We do not have arithmetic, so
3199 only set the memory modes. Include TFmode if -mabi=ieeelongdouble. */
3200 if (TARGET_FLOAT128_TYPE)
3202 rs6000_vector_mem[KFmode] = VECTOR_VSX;
3203 rs6000_vector_align[KFmode] = 128;
3205 if (FLOAT128_IEEE_P (TFmode))
3207 rs6000_vector_mem[TFmode] = VECTOR_VSX;
3208 rs6000_vector_align[TFmode] = 128;
3212 /* V2DF mode, VSX only. */
3213 if (TARGET_VSX)
3215 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
3216 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
3217 rs6000_vector_align[V2DFmode] = align64;
3220 /* V4SF mode, either VSX or Altivec. */
3221 if (TARGET_VSX)
3223 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
3224 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
3225 rs6000_vector_align[V4SFmode] = align32;
3227 else if (TARGET_ALTIVEC)
3229 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
3230 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
3231 rs6000_vector_align[V4SFmode] = align32;
3234 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
3235 and stores. */
3236 if (TARGET_ALTIVEC)
3238 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
3239 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
3240 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
3241 rs6000_vector_align[V4SImode] = align32;
3242 rs6000_vector_align[V8HImode] = align32;
3243 rs6000_vector_align[V16QImode] = align32;
3245 if (TARGET_VSX)
3247 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
3248 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
3249 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
3251 else
3253 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
3254 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
3255 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
3259 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
3260 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
3261 if (TARGET_VSX)
3263 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
3264 rs6000_vector_unit[V2DImode]
3265 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3266 rs6000_vector_align[V2DImode] = align64;
3268 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
3269 rs6000_vector_unit[V1TImode]
3270 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3271 rs6000_vector_align[V1TImode] = 128;
3274 /* DFmode, see if we want to use the VSX unit. Memory is handled
3275 differently, so don't set rs6000_vector_mem. */
3276 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
3278 rs6000_vector_unit[DFmode] = VECTOR_VSX;
3279 rs6000_vector_align[DFmode] = 64;
3282 /* SFmode, see if we want to use the VSX unit. */
3283 if (TARGET_P8_VECTOR && TARGET_VSX_SCALAR_FLOAT)
3285 rs6000_vector_unit[SFmode] = VECTOR_VSX;
3286 rs6000_vector_align[SFmode] = 32;
3289 /* Allow TImode in VSX register and set the VSX memory macros. */
3290 if (TARGET_VSX && TARGET_VSX_TIMODE)
3292 rs6000_vector_mem[TImode] = VECTOR_VSX;
3293 rs6000_vector_align[TImode] = align64;
3296 /* TODO add SPE and paired floating point vector support. */
3298 /* Register class constraints for the constraints that depend on compile
3299 switches. When the VSX code was added, different constraints were added
3300 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
3301 of the VSX registers are used. The register classes for scalar floating
3302 point types is set, based on whether we allow that type into the upper
3303 (Altivec) registers. GCC has register classes to target the Altivec
3304 registers for load/store operations, to select using a VSX memory
3305 operation instead of the traditional floating point operation. The
3306 constraints are:
3308 d - Register class to use with traditional DFmode instructions.
3309 f - Register class to use with traditional SFmode instructions.
3310 v - Altivec register.
3311 wa - Any VSX register.
3312 wc - Reserved to represent individual CR bits (used in LLVM).
3313 wd - Preferred register class for V2DFmode.
3314 wf - Preferred register class for V4SFmode.
3315 wg - Float register for power6x move insns.
3316 wh - FP register for direct move instructions.
3317 wi - FP or VSX register to hold 64-bit integers for VSX insns.
3318 wj - FP or VSX register to hold 64-bit integers for direct moves.
3319 wk - FP or VSX register to hold 64-bit doubles for direct moves.
3320 wl - Float register if we can do 32-bit signed int loads.
3321 wm - VSX register for ISA 2.07 direct move operations.
3322 wn - always NO_REGS.
3323 wr - GPR if 64-bit mode is permitted.
3324 ws - Register class to do ISA 2.06 DF operations.
3325 wt - VSX register for TImode in VSX registers.
3326 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
3327 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
3328 ww - Register class to do SF conversions in with VSX operations.
3329 wx - Float register if we can do 32-bit int stores.
3330 wy - Register class to do ISA 2.07 SF operations.
3331 wz - Float register if we can do 32-bit unsigned int loads.
3332 wH - Altivec register if SImode is allowed in VSX registers.
3333 wI - VSX register if SImode is allowed in VSX registers.
3334 wJ - VSX register if QImode/HImode are allowed in VSX registers.
3335 wK - Altivec register if QImode/HImode are allowed in VSX registers. */
3337 if (TARGET_HARD_FLOAT && TARGET_FPRS)
3338 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS; /* SFmode */
3340 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
3341 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS; /* DFmode */
3343 if (TARGET_VSX)
3345 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
3346 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS; /* V2DFmode */
3347 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS; /* V4SFmode */
3349 if (TARGET_VSX_TIMODE)
3350 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS; /* TImode */
3352 if (TARGET_UPPER_REGS_DF) /* DFmode */
3354 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
3355 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
3357 else
3358 rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
3360 if (TARGET_UPPER_REGS_DI) /* DImode */
3361 rs6000_constraints[RS6000_CONSTRAINT_wi] = VSX_REGS;
3362 else
3363 rs6000_constraints[RS6000_CONSTRAINT_wi] = FLOAT_REGS;
3366 /* Add conditional constraints based on various options, to allow us to
3367 collapse multiple insn patterns. */
3368 if (TARGET_ALTIVEC)
3369 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
3371 if (TARGET_MFPGPR) /* DFmode */
3372 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
3374 if (TARGET_LFIWAX)
3375 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS; /* DImode */
3377 if (TARGET_DIRECT_MOVE)
3379 rs6000_constraints[RS6000_CONSTRAINT_wh] = FLOAT_REGS;
3380 rs6000_constraints[RS6000_CONSTRAINT_wj] /* DImode */
3381 = rs6000_constraints[RS6000_CONSTRAINT_wi];
3382 rs6000_constraints[RS6000_CONSTRAINT_wk] /* DFmode */
3383 = rs6000_constraints[RS6000_CONSTRAINT_ws];
3384 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
3387 if (TARGET_POWERPC64)
3389 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
3390 rs6000_constraints[RS6000_CONSTRAINT_wA] = BASE_REGS;
3393 if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF) /* SFmode */
3395 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
3396 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
3397 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
3399 else if (TARGET_P8_VECTOR)
3401 rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
3402 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
3404 else if (TARGET_VSX)
3405 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
3407 if (TARGET_STFIWX)
3408 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS; /* DImode */
3410 if (TARGET_LFIWZX)
3411 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS; /* DImode */
3413 if (TARGET_FLOAT128_TYPE)
3415 rs6000_constraints[RS6000_CONSTRAINT_wq] = VSX_REGS; /* KFmode */
3416 if (FLOAT128_IEEE_P (TFmode))
3417 rs6000_constraints[RS6000_CONSTRAINT_wp] = VSX_REGS; /* TFmode */
3420 /* Support for new D-form instructions. */
3421 if (TARGET_P9_DFORM_SCALAR)
3422 rs6000_constraints[RS6000_CONSTRAINT_wb] = ALTIVEC_REGS;
3424 /* Support for ISA 3.0 (power9) vectors. */
3425 if (TARGET_P9_VECTOR)
3426 rs6000_constraints[RS6000_CONSTRAINT_wo] = VSX_REGS;
3428 /* Support for new direct moves (ISA 3.0 + 64bit). */
3429 if (TARGET_DIRECT_MOVE_128)
3430 rs6000_constraints[RS6000_CONSTRAINT_we] = VSX_REGS;
3432 /* Support small integers in VSX registers. */
3433 if (TARGET_VSX_SMALL_INTEGER)
3435 rs6000_constraints[RS6000_CONSTRAINT_wH] = ALTIVEC_REGS;
3436 rs6000_constraints[RS6000_CONSTRAINT_wI] = FLOAT_REGS;
3437 if (TARGET_P9_VECTOR)
3439 rs6000_constraints[RS6000_CONSTRAINT_wJ] = FLOAT_REGS;
3440 rs6000_constraints[RS6000_CONSTRAINT_wK] = ALTIVEC_REGS;
3444 /* Set up the reload helper and direct move functions. */
3445 if (TARGET_VSX || TARGET_ALTIVEC)
3447 if (TARGET_64BIT)
3449 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
3450 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
3451 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
3452 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
3453 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
3454 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
3455 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
3456 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
3457 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
3458 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
3459 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
3460 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
3461 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
3462 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
3463 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
3464 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
3465 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
3466 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
3467 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
3468 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
3470 if (FLOAT128_VECTOR_P (KFmode))
3472 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_di_store;
3473 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_di_load;
3476 if (FLOAT128_VECTOR_P (TFmode))
3478 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_di_store;
3479 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_di_load;
3482 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3483 available. */
3484 if (TARGET_NO_SDMODE_STACK)
3486 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
3487 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
3490 if (TARGET_VSX_TIMODE)
3492 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
3493 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
3496 if (TARGET_DIRECT_MOVE && !TARGET_DIRECT_MOVE_128)
3498 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
3499 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
3500 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
3501 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
3502 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
3503 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
3504 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
3505 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
3506 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
3508 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
3509 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
3510 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
3511 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
3512 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
3513 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
3514 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
3515 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
3516 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
3518 if (FLOAT128_VECTOR_P (KFmode))
3520 reg_addr[KFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxkf;
3521 reg_addr[KFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprkf;
3524 if (FLOAT128_VECTOR_P (TFmode))
3526 reg_addr[TFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxtf;
3527 reg_addr[TFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprtf;
3531 else
3533 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
3534 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
3535 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
3536 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
3537 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
3538 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
3539 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
3540 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
3541 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
3542 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
3543 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
3544 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
3545 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
3546 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
3547 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
3548 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
3549 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
3550 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
3551 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
3552 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
3554 if (FLOAT128_VECTOR_P (KFmode))
3556 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_si_store;
3557 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_si_load;
3560 if (FLOAT128_IEEE_P (TFmode))
3562 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_si_store;
3563 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_si_load;
3566 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3567 available. */
3568 if (TARGET_NO_SDMODE_STACK)
3570 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
3571 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
3574 if (TARGET_VSX_TIMODE)
3576 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
3577 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
3580 if (TARGET_DIRECT_MOVE)
3582 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
3583 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
3584 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
3588 if (TARGET_UPPER_REGS_DF)
3589 reg_addr[DFmode].scalar_in_vmx_p = true;
3591 if (TARGET_UPPER_REGS_DI)
3592 reg_addr[DImode].scalar_in_vmx_p = true;
3594 if (TARGET_UPPER_REGS_SF)
3595 reg_addr[SFmode].scalar_in_vmx_p = true;
3597 if (TARGET_VSX_SMALL_INTEGER)
3599 reg_addr[SImode].scalar_in_vmx_p = true;
3600 if (TARGET_P9_VECTOR)
3602 reg_addr[HImode].scalar_in_vmx_p = true;
3603 reg_addr[QImode].scalar_in_vmx_p = true;
3608 /* Setup the fusion operations. */
3609 if (TARGET_P8_FUSION)
3611 reg_addr[QImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_qi;
3612 reg_addr[HImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_hi;
3613 reg_addr[SImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_si;
3614 if (TARGET_64BIT)
3615 reg_addr[DImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_di;
3618 if (TARGET_P9_FUSION)
3620 struct fuse_insns {
3621 enum machine_mode mode; /* mode of the fused type. */
3622 enum machine_mode pmode; /* pointer mode. */
3623 enum rs6000_reload_reg_type rtype; /* register type. */
3624 enum insn_code load; /* load insn. */
3625 enum insn_code store; /* store insn. */
3628 static const struct fuse_insns addis_insns[] = {
3629 { E_SFmode, E_DImode, RELOAD_REG_FPR,
3630 CODE_FOR_fusion_vsx_di_sf_load,
3631 CODE_FOR_fusion_vsx_di_sf_store },
3633 { E_SFmode, E_SImode, RELOAD_REG_FPR,
3634 CODE_FOR_fusion_vsx_si_sf_load,
3635 CODE_FOR_fusion_vsx_si_sf_store },
3637 { E_DFmode, E_DImode, RELOAD_REG_FPR,
3638 CODE_FOR_fusion_vsx_di_df_load,
3639 CODE_FOR_fusion_vsx_di_df_store },
3641 { E_DFmode, E_SImode, RELOAD_REG_FPR,
3642 CODE_FOR_fusion_vsx_si_df_load,
3643 CODE_FOR_fusion_vsx_si_df_store },
3645 { E_DImode, E_DImode, RELOAD_REG_FPR,
3646 CODE_FOR_fusion_vsx_di_di_load,
3647 CODE_FOR_fusion_vsx_di_di_store },
3649 { E_DImode, E_SImode, RELOAD_REG_FPR,
3650 CODE_FOR_fusion_vsx_si_di_load,
3651 CODE_FOR_fusion_vsx_si_di_store },
3653 { E_QImode, E_DImode, RELOAD_REG_GPR,
3654 CODE_FOR_fusion_gpr_di_qi_load,
3655 CODE_FOR_fusion_gpr_di_qi_store },
3657 { E_QImode, E_SImode, RELOAD_REG_GPR,
3658 CODE_FOR_fusion_gpr_si_qi_load,
3659 CODE_FOR_fusion_gpr_si_qi_store },
3661 { E_HImode, E_DImode, RELOAD_REG_GPR,
3662 CODE_FOR_fusion_gpr_di_hi_load,
3663 CODE_FOR_fusion_gpr_di_hi_store },
3665 { E_HImode, E_SImode, RELOAD_REG_GPR,
3666 CODE_FOR_fusion_gpr_si_hi_load,
3667 CODE_FOR_fusion_gpr_si_hi_store },
3669 { E_SImode, E_DImode, RELOAD_REG_GPR,
3670 CODE_FOR_fusion_gpr_di_si_load,
3671 CODE_FOR_fusion_gpr_di_si_store },
3673 { E_SImode, E_SImode, RELOAD_REG_GPR,
3674 CODE_FOR_fusion_gpr_si_si_load,
3675 CODE_FOR_fusion_gpr_si_si_store },
3677 { E_SFmode, E_DImode, RELOAD_REG_GPR,
3678 CODE_FOR_fusion_gpr_di_sf_load,
3679 CODE_FOR_fusion_gpr_di_sf_store },
3681 { E_SFmode, E_SImode, RELOAD_REG_GPR,
3682 CODE_FOR_fusion_gpr_si_sf_load,
3683 CODE_FOR_fusion_gpr_si_sf_store },
3685 { E_DImode, E_DImode, RELOAD_REG_GPR,
3686 CODE_FOR_fusion_gpr_di_di_load,
3687 CODE_FOR_fusion_gpr_di_di_store },
3689 { E_DFmode, E_DImode, RELOAD_REG_GPR,
3690 CODE_FOR_fusion_gpr_di_df_load,
3691 CODE_FOR_fusion_gpr_di_df_store },
3694 machine_mode cur_pmode = Pmode;
3695 size_t i;
3697 for (i = 0; i < ARRAY_SIZE (addis_insns); i++)
3699 machine_mode xmode = addis_insns[i].mode;
3700 enum rs6000_reload_reg_type rtype = addis_insns[i].rtype;
3702 if (addis_insns[i].pmode != cur_pmode)
3703 continue;
3705 if (rtype == RELOAD_REG_FPR
3706 && (!TARGET_HARD_FLOAT || !TARGET_FPRS))
3707 continue;
3709 reg_addr[xmode].fusion_addis_ld[rtype] = addis_insns[i].load;
3710 reg_addr[xmode].fusion_addis_st[rtype] = addis_insns[i].store;
3712 if (rtype == RELOAD_REG_FPR && TARGET_P9_DFORM_SCALAR)
3714 reg_addr[xmode].fusion_addis_ld[RELOAD_REG_VMX]
3715 = addis_insns[i].load;
3716 reg_addr[xmode].fusion_addis_st[RELOAD_REG_VMX]
3717 = addis_insns[i].store;
3722 /* Note which types we support fusing TOC setup plus memory insn. We only do
3723 fused TOCs for medium/large code models. */
3724 if (TARGET_P8_FUSION && TARGET_TOC_FUSION && TARGET_POWERPC64
3725 && (TARGET_CMODEL != CMODEL_SMALL))
3727 reg_addr[QImode].fused_toc = true;
3728 reg_addr[HImode].fused_toc = true;
3729 reg_addr[SImode].fused_toc = true;
3730 reg_addr[DImode].fused_toc = true;
3731 if (TARGET_HARD_FLOAT && TARGET_FPRS)
3733 if (TARGET_SINGLE_FLOAT)
3734 reg_addr[SFmode].fused_toc = true;
3735 if (TARGET_DOUBLE_FLOAT)
3736 reg_addr[DFmode].fused_toc = true;
3740 /* Precalculate HARD_REGNO_NREGS. */
3741 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
3742 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3743 rs6000_hard_regno_nregs[m][r]
3744 = rs6000_hard_regno_nregs_internal (r, (machine_mode)m);
3746 /* Precalculate TARGET_HARD_REGNO_MODE_OK. */
3747 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
3748 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3749 if (rs6000_hard_regno_mode_ok_uncached (r, (machine_mode)m))
3750 rs6000_hard_regno_mode_ok_p[m][r] = true;
3752 /* Precalculate CLASS_MAX_NREGS sizes. */
3753 for (c = 0; c < LIM_REG_CLASSES; ++c)
3755 int reg_size;
3757 if (TARGET_VSX && VSX_REG_CLASS_P (c))
3758 reg_size = UNITS_PER_VSX_WORD;
3760 else if (c == ALTIVEC_REGS)
3761 reg_size = UNITS_PER_ALTIVEC_WORD;
3763 else if (c == FLOAT_REGS)
3764 reg_size = UNITS_PER_FP_WORD;
3766 else
3767 reg_size = UNITS_PER_WORD;
3769 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3771 machine_mode m2 = (machine_mode)m;
3772 int reg_size2 = reg_size;
3774 /* TDmode & IBM 128-bit floating point always takes 2 registers, even
3775 in VSX. */
3776 if (TARGET_VSX && VSX_REG_CLASS_P (c) && FLOAT128_2REG_P (m))
3777 reg_size2 = UNITS_PER_FP_WORD;
3779 rs6000_class_max_nregs[m][c]
3780 = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
3784 if (TARGET_E500_DOUBLE)
3785 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
3787 /* Calculate which modes to automatically generate code to use a the
3788 reciprocal divide and square root instructions. In the future, possibly
3789 automatically generate the instructions even if the user did not specify
3790 -mrecip. The older machines double precision reciprocal sqrt estimate is
3791 not accurate enough. */
3792 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
3793 if (TARGET_FRES)
3794 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3795 if (TARGET_FRE)
3796 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3797 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3798 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3799 if (VECTOR_UNIT_VSX_P (V2DFmode))
3800 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3802 if (TARGET_FRSQRTES)
3803 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3804 if (TARGET_FRSQRTE)
3805 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3806 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3807 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3808 if (VECTOR_UNIT_VSX_P (V2DFmode))
3809 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3811 if (rs6000_recip_control)
3813 if (!flag_finite_math_only)
3814 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
3815 if (flag_trapping_math)
3816 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
3817 if (!flag_reciprocal_math)
3818 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
3819 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
3821 if (RS6000_RECIP_HAVE_RE_P (SFmode)
3822 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
3823 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3825 if (RS6000_RECIP_HAVE_RE_P (DFmode)
3826 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
3827 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3829 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
3830 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
3831 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3833 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
3834 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
3835 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3837 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
3838 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
3839 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3841 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
3842 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
3843 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3845 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
3846 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
3847 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3849 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
3850 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
3851 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3855 /* Update the addr mask bits in reg_addr to help secondary reload and go if
3856 legitimate address support to figure out the appropriate addressing to
3857 use. */
3858 rs6000_setup_reg_addr_masks ();
3860 if (global_init_p || TARGET_DEBUG_TARGET)
3862 if (TARGET_DEBUG_REG)
3863 rs6000_debug_reg_global ();
3865 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
3866 fprintf (stderr,
3867 "SImode variable mult cost = %d\n"
3868 "SImode constant mult cost = %d\n"
3869 "SImode short constant mult cost = %d\n"
3870 "DImode multipliciation cost = %d\n"
3871 "SImode division cost = %d\n"
3872 "DImode division cost = %d\n"
3873 "Simple fp operation cost = %d\n"
3874 "DFmode multiplication cost = %d\n"
3875 "SFmode division cost = %d\n"
3876 "DFmode division cost = %d\n"
3877 "cache line size = %d\n"
3878 "l1 cache size = %d\n"
3879 "l2 cache size = %d\n"
3880 "simultaneous prefetches = %d\n"
3881 "\n",
3882 rs6000_cost->mulsi,
3883 rs6000_cost->mulsi_const,
3884 rs6000_cost->mulsi_const9,
3885 rs6000_cost->muldi,
3886 rs6000_cost->divsi,
3887 rs6000_cost->divdi,
3888 rs6000_cost->fp,
3889 rs6000_cost->dmul,
3890 rs6000_cost->sdiv,
3891 rs6000_cost->ddiv,
3892 rs6000_cost->cache_line_size,
3893 rs6000_cost->l1_cache_size,
3894 rs6000_cost->l2_cache_size,
3895 rs6000_cost->simultaneous_prefetches);
3899 #if TARGET_MACHO
3900 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
3902 static void
3903 darwin_rs6000_override_options (void)
3905 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
3906 off. */
3907 rs6000_altivec_abi = 1;
3908 TARGET_ALTIVEC_VRSAVE = 1;
3909 rs6000_current_abi = ABI_DARWIN;
3911 if (DEFAULT_ABI == ABI_DARWIN
3912 && TARGET_64BIT)
3913 darwin_one_byte_bool = 1;
3915 if (TARGET_64BIT && ! TARGET_POWERPC64)
3917 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
3918 warning (0, "-m64 requires PowerPC64 architecture, enabling");
3920 if (flag_mkernel)
3922 rs6000_default_long_calls = 1;
3923 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
3926 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
3927 Altivec. */
3928 if (!flag_mkernel && !flag_apple_kext
3929 && TARGET_64BIT
3930 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3931 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3933 /* Unless the user (not the configurer) has explicitly overridden
3934 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3935 G4 unless targeting the kernel. */
3936 if (!flag_mkernel
3937 && !flag_apple_kext
3938 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3939 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3940 && ! global_options_set.x_rs6000_cpu_index)
3942 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3945 #endif
3947 /* If not otherwise specified by a target, make 'long double' equivalent to
3948 'double'. */
3950 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3951 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3952 #endif
3954 /* Return the builtin mask of the various options used that could affect which
3955 builtins were used. In the past we used target_flags, but we've run out of
3956 bits, and some options like SPE and PAIRED are no longer in
3957 target_flags. */
3959 HOST_WIDE_INT
3960 rs6000_builtin_mask_calculate (void)
3962 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3963 | ((TARGET_CMPB) ? RS6000_BTM_CMPB : 0)
3964 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3965 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
3966 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
3967 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3968 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3969 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3970 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3971 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3972 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3973 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3974 | ((TARGET_P9_VECTOR) ? RS6000_BTM_P9_VECTOR : 0)
3975 | ((TARGET_P9_MISC) ? RS6000_BTM_P9_MISC : 0)
3976 | ((TARGET_MODULO) ? RS6000_BTM_MODULO : 0)
3977 | ((TARGET_64BIT) ? RS6000_BTM_64BIT : 0)
3978 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3979 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0)
3980 | ((TARGET_DFP) ? RS6000_BTM_DFP : 0)
3981 | ((TARGET_HARD_FLOAT) ? RS6000_BTM_HARD_FLOAT : 0)
3982 | ((TARGET_LONG_DOUBLE_128) ? RS6000_BTM_LDBL128 : 0)
3983 | ((TARGET_FLOAT128_TYPE) ? RS6000_BTM_FLOAT128 : 0));
3986 /* Implement TARGET_MD_ASM_ADJUST. All asm statements are considered
3987 to clobber the XER[CA] bit because clobbering that bit without telling
3988 the compiler worked just fine with versions of GCC before GCC 5, and
3989 breaking a lot of older code in ways that are hard to track down is
3990 not such a great idea. */
3992 static rtx_insn *
3993 rs6000_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
3994 vec<const char *> &/*constraints*/,
3995 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
3997 clobbers.safe_push (gen_rtx_REG (SImode, CA_REGNO));
3998 SET_HARD_REG_BIT (clobbered_regs, CA_REGNO);
3999 return NULL;
4002 /* Override command line options.
4004 Combine build-specific configuration information with options
4005 specified on the command line to set various state variables which
4006 influence code generation, optimization, and expansion of built-in
4007 functions. Assure that command-line configuration preferences are
4008 compatible with each other and with the build configuration; issue
4009 warnings while adjusting configuration or error messages while
4010 rejecting configuration.
4012 Upon entry to this function:
4014 This function is called once at the beginning of
4015 compilation, and then again at the start and end of compiling
4016 each section of code that has a different configuration, as
4017 indicated, for example, by adding the
4019 __attribute__((__target__("cpu=power9")))
4021 qualifier to a function definition or, for example, by bracketing
4022 code between
4024 #pragma GCC target("altivec")
4028 #pragma GCC reset_options
4030 directives. Parameter global_init_p is true for the initial
4031 invocation, which initializes global variables, and false for all
4032 subsequent invocations.
4035 Various global state information is assumed to be valid. This
4036 includes OPTION_TARGET_CPU_DEFAULT, representing the name of the
4037 default CPU specified at build configure time, TARGET_DEFAULT,
4038 representing the default set of option flags for the default
4039 target, and global_options_set.x_rs6000_isa_flags, representing
4040 which options were requested on the command line.
4042 Upon return from this function:
4044 rs6000_isa_flags_explicit has a non-zero bit for each flag that
4045 was set by name on the command line. Additionally, if certain
4046 attributes are automatically enabled or disabled by this function
4047 in order to assure compatibility between options and
4048 configuration, the flags associated with those attributes are
4049 also set. By setting these "explicit bits", we avoid the risk
4050 that other code might accidentally overwrite these particular
4051 attributes with "default values".
4053 The various bits of rs6000_isa_flags are set to indicate the
4054 target options that have been selected for the most current
4055 compilation efforts. This has the effect of also turning on the
4056 associated TARGET_XXX values since these are macros which are
4057 generally defined to test the corresponding bit of the
4058 rs6000_isa_flags variable.
4060 The variable rs6000_builtin_mask is set to represent the target
4061 options for the most current compilation efforts, consistent with
4062 the current contents of rs6000_isa_flags. This variable controls
4063 expansion of built-in functions.
4065 Various other global variables and fields of global structures
4066 (over 50 in all) are initialized to reflect the desired options
4067 for the most current compilation efforts. */
4069 static bool
4070 rs6000_option_override_internal (bool global_init_p)
4072 bool ret = true;
4073 bool have_cpu = false;
4075 /* The default cpu requested at configure time, if any. */
4076 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
4078 HOST_WIDE_INT set_masks;
4079 HOST_WIDE_INT ignore_masks;
4080 int cpu_index;
4081 int tune_index;
4082 struct cl_target_option *main_target_opt
4083 = ((global_init_p || target_option_default_node == NULL)
4084 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
4086 /* Print defaults. */
4087 if ((TARGET_DEBUG_REG || TARGET_DEBUG_TARGET) && global_init_p)
4088 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
4090 /* Remember the explicit arguments. */
4091 if (global_init_p)
4092 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
4094 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
4095 library functions, so warn about it. The flag may be useful for
4096 performance studies from time to time though, so don't disable it
4097 entirely. */
4098 if (global_options_set.x_rs6000_alignment_flags
4099 && rs6000_alignment_flags == MASK_ALIGN_POWER
4100 && DEFAULT_ABI == ABI_DARWIN
4101 && TARGET_64BIT)
4102 warning (0, "-malign-power is not supported for 64-bit Darwin;"
4103 " it is incompatible with the installed C and C++ libraries");
4105 /* Numerous experiment shows that IRA based loop pressure
4106 calculation works better for RTL loop invariant motion on targets
4107 with enough (>= 32) registers. It is an expensive optimization.
4108 So it is on only for peak performance. */
4109 if (optimize >= 3 && global_init_p
4110 && !global_options_set.x_flag_ira_loop_pressure)
4111 flag_ira_loop_pressure = 1;
4113 /* -fsanitize=address needs to turn on -fasynchronous-unwind-tables in order
4114 for tracebacks to be complete but not if any -fasynchronous-unwind-tables
4115 options were already specified. */
4116 if (flag_sanitize & SANITIZE_USER_ADDRESS
4117 && !global_options_set.x_flag_asynchronous_unwind_tables)
4118 flag_asynchronous_unwind_tables = 1;
4120 /* Set the pointer size. */
4121 if (TARGET_64BIT)
4123 rs6000_pmode = DImode;
4124 rs6000_pointer_size = 64;
4126 else
4128 rs6000_pmode = SImode;
4129 rs6000_pointer_size = 32;
4132 /* Some OSs don't support saving the high part of 64-bit registers on context
4133 switch. Other OSs don't support saving Altivec registers. On those OSs,
4134 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
4135 if the user wants either, the user must explicitly specify them and we
4136 won't interfere with the user's specification. */
4138 set_masks = POWERPC_MASKS;
4139 #ifdef OS_MISSING_POWERPC64
4140 if (OS_MISSING_POWERPC64)
4141 set_masks &= ~OPTION_MASK_POWERPC64;
4142 #endif
4143 #ifdef OS_MISSING_ALTIVEC
4144 if (OS_MISSING_ALTIVEC)
4145 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX
4146 | OTHER_VSX_VECTOR_MASKS);
4147 #endif
4149 /* Don't override by the processor default if given explicitly. */
4150 set_masks &= ~rs6000_isa_flags_explicit;
4152 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
4153 the cpu in a target attribute or pragma, but did not specify a tuning
4154 option, use the cpu for the tuning option rather than the option specified
4155 with -mtune on the command line. Process a '--with-cpu' configuration
4156 request as an implicit --cpu. */
4157 if (rs6000_cpu_index >= 0)
4159 cpu_index = rs6000_cpu_index;
4160 have_cpu = true;
4162 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
4164 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
4165 have_cpu = true;
4167 else if (implicit_cpu)
4169 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
4170 have_cpu = true;
4172 else
4174 /* PowerPC 64-bit LE requires at least ISA 2.07. */
4175 const char *default_cpu = ((!TARGET_POWERPC64)
4176 ? "powerpc"
4177 : ((BYTES_BIG_ENDIAN)
4178 ? "powerpc64"
4179 : "powerpc64le"));
4181 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
4182 have_cpu = false;
4185 gcc_assert (cpu_index >= 0);
4187 if (have_cpu)
4189 #ifndef HAVE_AS_POWER9
4190 if (processor_target_table[rs6000_cpu_index].processor
4191 == PROCESSOR_POWER9)
4193 have_cpu = false;
4194 warning (0, "will not generate power9 instructions because "
4195 "assembler lacks power9 support");
4197 #endif
4198 #ifndef HAVE_AS_POWER8
4199 if (processor_target_table[rs6000_cpu_index].processor
4200 == PROCESSOR_POWER8)
4202 have_cpu = false;
4203 warning (0, "will not generate power8 instructions because "
4204 "assembler lacks power8 support");
4206 #endif
4207 #ifndef HAVE_AS_POPCNTD
4208 if (processor_target_table[rs6000_cpu_index].processor
4209 == PROCESSOR_POWER7)
4211 have_cpu = false;
4212 warning (0, "will not generate power7 instructions because "
4213 "assembler lacks power7 support");
4215 #endif
4216 #ifndef HAVE_AS_DFP
4217 if (processor_target_table[rs6000_cpu_index].processor
4218 == PROCESSOR_POWER6)
4220 have_cpu = false;
4221 warning (0, "will not generate power6 instructions because "
4222 "assembler lacks power6 support");
4224 #endif
4225 #ifndef HAVE_AS_POPCNTB
4226 if (processor_target_table[rs6000_cpu_index].processor
4227 == PROCESSOR_POWER5)
4229 have_cpu = false;
4230 warning (0, "will not generate power5 instructions because "
4231 "assembler lacks power5 support");
4233 #endif
4235 if (!have_cpu)
4237 /* PowerPC 64-bit LE requires at least ISA 2.07. */
4238 const char *default_cpu = (!TARGET_POWERPC64
4239 ? "powerpc"
4240 : (BYTES_BIG_ENDIAN
4241 ? "powerpc64"
4242 : "powerpc64le"));
4244 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
4248 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
4249 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
4250 with those from the cpu, except for options that were explicitly set. If
4251 we don't have a cpu, do not override the target bits set in
4252 TARGET_DEFAULT. */
4253 if (have_cpu)
4255 rs6000_isa_flags &= ~set_masks;
4256 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
4257 & set_masks);
4259 else
4261 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
4262 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
4263 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
4264 to using rs6000_isa_flags, we need to do the initialization here.
4266 If there is a TARGET_DEFAULT, use that. Otherwise fall back to using
4267 -mcpu=powerpc, -mcpu=powerpc64, or -mcpu=powerpc64le defaults. */
4268 HOST_WIDE_INT flags = ((TARGET_DEFAULT) ? TARGET_DEFAULT
4269 : processor_target_table[cpu_index].target_enable);
4270 rs6000_isa_flags |= (flags & ~rs6000_isa_flags_explicit);
4273 if (rs6000_tune_index >= 0)
4274 tune_index = rs6000_tune_index;
4275 else if (have_cpu)
4276 rs6000_tune_index = tune_index = cpu_index;
4277 else
4279 size_t i;
4280 enum processor_type tune_proc
4281 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
4283 tune_index = -1;
4284 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
4285 if (processor_target_table[i].processor == tune_proc)
4287 rs6000_tune_index = tune_index = i;
4288 break;
4292 gcc_assert (tune_index >= 0);
4293 rs6000_cpu = processor_target_table[tune_index].processor;
4295 /* Pick defaults for SPE related control flags. Do this early to make sure
4296 that the TARGET_ macros are representative ASAP. */
4298 int spe_capable_cpu =
4299 (rs6000_cpu == PROCESSOR_PPC8540
4300 || rs6000_cpu == PROCESSOR_PPC8548);
4302 if (!global_options_set.x_rs6000_spe_abi)
4303 rs6000_spe_abi = spe_capable_cpu;
4305 if (!global_options_set.x_rs6000_spe)
4306 rs6000_spe = spe_capable_cpu;
4308 if (!global_options_set.x_rs6000_float_gprs)
4309 rs6000_float_gprs =
4310 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
4311 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
4312 : 0);
4315 if (global_options_set.x_rs6000_spe_abi
4316 && rs6000_spe_abi
4317 && !TARGET_SPE_ABI)
4318 error ("not configured for SPE ABI");
4320 if (global_options_set.x_rs6000_spe
4321 && rs6000_spe
4322 && !TARGET_SPE)
4323 error ("not configured for SPE instruction set");
4325 if (main_target_opt != NULL
4326 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
4327 || (main_target_opt->x_rs6000_spe != rs6000_spe)
4328 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
4329 error ("target attribute or pragma changes SPE ABI");
4331 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
4332 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
4333 || rs6000_cpu == PROCESSOR_PPCE5500)
4335 if (TARGET_ALTIVEC)
4336 error ("AltiVec not supported in this target");
4337 if (TARGET_SPE)
4338 error ("SPE not supported in this target");
4340 if (rs6000_cpu == PROCESSOR_PPCE6500)
4342 if (TARGET_SPE)
4343 error ("SPE not supported in this target");
4346 /* Disable Cell microcode if we are optimizing for the Cell
4347 and not optimizing for size. */
4348 if (rs6000_gen_cell_microcode == -1)
4349 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
4350 && !optimize_size);
4352 /* If we are optimizing big endian systems for space and it's OK to
4353 use instructions that would be microcoded on the Cell, use the
4354 load/store multiple and string instructions. */
4355 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
4356 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
4357 | OPTION_MASK_STRING);
4359 /* Don't allow -mmultiple or -mstring on little endian systems
4360 unless the cpu is a 750, because the hardware doesn't support the
4361 instructions used in little endian mode, and causes an alignment
4362 trap. The 750 does not cause an alignment trap (except when the
4363 target is unaligned). */
4365 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
4367 if (TARGET_MULTIPLE)
4369 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
4370 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
4371 warning (0, "-mmultiple is not supported on little endian systems");
4374 if (TARGET_STRING)
4376 rs6000_isa_flags &= ~OPTION_MASK_STRING;
4377 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
4378 warning (0, "-mstring is not supported on little endian systems");
4382 /* If little-endian, default to -mstrict-align on older processors.
4383 Testing for htm matches power8 and later. */
4384 if (!BYTES_BIG_ENDIAN
4385 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
4386 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
4388 /* -maltivec={le,be} implies -maltivec. */
4389 if (rs6000_altivec_element_order != 0)
4390 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
4392 /* Disallow -maltivec=le in big endian mode for now. This is not
4393 known to be useful for anyone. */
4394 if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
4396 warning (0, N_("-maltivec=le not allowed for big-endian targets"));
4397 rs6000_altivec_element_order = 0;
4400 /* Add some warnings for VSX. */
4401 if (TARGET_VSX)
4403 const char *msg = NULL;
4404 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
4405 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
4407 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4408 msg = N_("-mvsx requires hardware floating point");
4409 else
4411 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
4412 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4415 else if (TARGET_PAIRED_FLOAT)
4416 msg = N_("-mvsx and -mpaired are incompatible");
4417 else if (TARGET_AVOID_XFORM > 0)
4418 msg = N_("-mvsx needs indexed addressing");
4419 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
4420 & OPTION_MASK_ALTIVEC))
4422 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4423 msg = N_("-mvsx and -mno-altivec are incompatible");
4424 else
4425 msg = N_("-mno-altivec disables vsx");
4428 if (msg)
4430 warning (0, msg);
4431 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
4432 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4436 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
4437 the -mcpu setting to enable options that conflict. */
4438 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
4439 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
4440 | OPTION_MASK_ALTIVEC
4441 | OPTION_MASK_VSX)) != 0)
4442 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
4443 | OPTION_MASK_DIRECT_MOVE)
4444 & ~rs6000_isa_flags_explicit);
4446 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4447 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
4449 /* Handle explicit -mno-{altivec,vsx,power8-vector,power9-vector} and turn
4450 off all of the options that depend on those flags. */
4451 ignore_masks = rs6000_disable_incompatible_switches ();
4453 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
4454 unless the user explicitly used the -mno-<option> to disable the code. */
4455 if (TARGET_P9_VECTOR || TARGET_MODULO || TARGET_P9_DFORM_SCALAR
4456 || TARGET_P9_DFORM_VECTOR || TARGET_P9_DFORM_BOTH > 0)
4457 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
4458 else if (TARGET_P9_MINMAX)
4460 if (have_cpu)
4462 if (cpu_index == PROCESSOR_POWER9)
4464 /* legacy behavior: allow -mcpu-power9 with certain
4465 capabilities explicitly disabled. */
4466 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
4467 /* However, reject this automatic fix if certain
4468 capabilities required for TARGET_P9_MINMAX support
4469 have been explicitly disabled. */
4470 if (((OPTION_MASK_VSX | OPTION_MASK_UPPER_REGS_SF
4471 | OPTION_MASK_UPPER_REGS_DF) & rs6000_isa_flags)
4472 != (OPTION_MASK_VSX | OPTION_MASK_UPPER_REGS_SF
4473 | OPTION_MASK_UPPER_REGS_DF))
4474 error ("-mpower9-minmax incompatible with explicitly disabled options");
4476 else
4477 error ("Power9 target option is incompatible with -mcpu=<xxx> for "
4478 "<xxx> less than power9");
4480 else if ((ISA_3_0_MASKS_SERVER & rs6000_isa_flags_explicit)
4481 != (ISA_3_0_MASKS_SERVER & rs6000_isa_flags
4482 & rs6000_isa_flags_explicit))
4483 /* Enforce that none of the ISA_3_0_MASKS_SERVER flags
4484 were explicitly cleared. */
4485 error ("-mpower9-minmax incompatible with explicitly disabled options");
4486 else
4487 rs6000_isa_flags |= ISA_3_0_MASKS_SERVER;
4489 else if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
4490 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~ignore_masks);
4491 else if (TARGET_VSX)
4492 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~ignore_masks);
4493 else if (TARGET_POPCNTD)
4494 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~ignore_masks);
4495 else if (TARGET_DFP)
4496 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~ignore_masks);
4497 else if (TARGET_CMPB)
4498 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~ignore_masks);
4499 else if (TARGET_FPRND)
4500 rs6000_isa_flags |= (ISA_2_4_MASKS & ~ignore_masks);
4501 else if (TARGET_POPCNTB)
4502 rs6000_isa_flags |= (ISA_2_2_MASKS & ~ignore_masks);
4503 else if (TARGET_ALTIVEC)
4504 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~ignore_masks);
4506 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
4508 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
4509 error ("-mcrypto requires -maltivec");
4510 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
4513 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
4515 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
4516 error ("-mdirect-move requires -mvsx");
4517 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
4520 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
4522 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4523 error ("-mpower8-vector requires -maltivec");
4524 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4527 if (TARGET_P8_VECTOR && !TARGET_VSX)
4529 if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4530 && (rs6000_isa_flags_explicit & OPTION_MASK_VSX))
4531 error ("-mpower8-vector requires -mvsx");
4532 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR) == 0)
4534 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4535 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4536 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4538 else
4540 /* OPTION_MASK_P8_VECTOR is explicit, and OPTION_MASK_VSX is
4541 not explicit. */
4542 rs6000_isa_flags |= OPTION_MASK_VSX;
4543 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4547 if (TARGET_VSX_TIMODE && !TARGET_VSX)
4549 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
4550 error ("-mvsx-timode requires -mvsx");
4551 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
4554 if (TARGET_DFP && !TARGET_HARD_FLOAT)
4556 if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
4557 error ("-mhard-dfp requires -mhard-float");
4558 rs6000_isa_flags &= ~OPTION_MASK_DFP;
4561 /* Allow an explicit -mupper-regs to set -mupper-regs-df, -mupper-regs-di,
4562 and -mupper-regs-sf, depending on the cpu, unless the user explicitly also
4563 set the individual option. */
4564 if (TARGET_UPPER_REGS > 0)
4566 if (TARGET_VSX
4567 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF))
4569 rs6000_isa_flags |= OPTION_MASK_UPPER_REGS_DF;
4570 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DF;
4572 if (TARGET_VSX
4573 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DI))
4575 rs6000_isa_flags |= OPTION_MASK_UPPER_REGS_DI;
4576 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DI;
4578 if (TARGET_P8_VECTOR
4579 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF))
4581 rs6000_isa_flags |= OPTION_MASK_UPPER_REGS_SF;
4582 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_SF;
4585 else if (TARGET_UPPER_REGS == 0)
4587 if (TARGET_VSX
4588 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF))
4590 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DF;
4591 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DF;
4593 if (TARGET_VSX
4594 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DI))
4596 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DI;
4597 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_DI;
4599 if (TARGET_P8_VECTOR
4600 && !(rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF))
4602 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_SF;
4603 rs6000_isa_flags_explicit |= OPTION_MASK_UPPER_REGS_SF;
4607 if (TARGET_UPPER_REGS_DF && !TARGET_VSX)
4609 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF)
4610 error ("-mupper-regs-df requires -mvsx");
4611 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DF;
4614 if (TARGET_UPPER_REGS_DI && !TARGET_VSX)
4616 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DI)
4617 error ("-mupper-regs-di requires -mvsx");
4618 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_DI;
4621 if (TARGET_UPPER_REGS_SF && !TARGET_P8_VECTOR)
4623 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF)
4624 error ("-mupper-regs-sf requires -mpower8-vector");
4625 rs6000_isa_flags &= ~OPTION_MASK_UPPER_REGS_SF;
4628 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
4629 silently turn off quad memory mode. */
4630 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
4632 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4633 warning (0, N_("-mquad-memory requires 64-bit mode"));
4635 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
4636 warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
4638 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
4639 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
4642 /* Non-atomic quad memory load/store are disabled for little endian, since
4643 the words are reversed, but atomic operations can still be done by
4644 swapping the words. */
4645 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
4647 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4648 warning (0, N_("-mquad-memory is not available in little endian mode"));
4650 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
4653 /* Assume if the user asked for normal quad memory instructions, they want
4654 the atomic versions as well, unless they explicity told us not to use quad
4655 word atomic instructions. */
4656 if (TARGET_QUAD_MEMORY
4657 && !TARGET_QUAD_MEMORY_ATOMIC
4658 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
4659 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
4661 /* Enable power8 fusion if we are tuning for power8, even if we aren't
4662 generating power8 instructions. */
4663 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
4664 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
4665 & OPTION_MASK_P8_FUSION);
4667 /* Setting additional fusion flags turns on base fusion. */
4668 if (!TARGET_P8_FUSION && (TARGET_P8_FUSION_SIGN || TARGET_TOC_FUSION))
4670 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4672 if (TARGET_P8_FUSION_SIGN)
4673 error ("-mpower8-fusion-sign requires -mpower8-fusion");
4675 if (TARGET_TOC_FUSION)
4676 error ("-mtoc-fusion requires -mpower8-fusion");
4678 rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
4680 else
4681 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4684 /* Power9 fusion is a superset over power8 fusion. */
4685 if (TARGET_P9_FUSION && !TARGET_P8_FUSION)
4687 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4689 /* We prefer to not mention undocumented options in
4690 error messages. However, if users have managed to select
4691 power9-fusion without selecting power8-fusion, they
4692 already know about undocumented flags. */
4693 error ("-mpower9-fusion requires -mpower8-fusion");
4694 rs6000_isa_flags &= ~OPTION_MASK_P9_FUSION;
4696 else
4697 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4700 /* Enable power9 fusion if we are tuning for power9, even if we aren't
4701 generating power9 instructions. */
4702 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_FUSION))
4703 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
4704 & OPTION_MASK_P9_FUSION);
4706 /* Power8 does not fuse sign extended loads with the addis. If we are
4707 optimizing at high levels for speed, convert a sign extended load into a
4708 zero extending load, and an explicit sign extension. */
4709 if (TARGET_P8_FUSION
4710 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
4711 && optimize_function_for_speed_p (cfun)
4712 && optimize >= 3)
4713 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
4715 /* TOC fusion requires 64-bit and medium/large code model. */
4716 if (TARGET_TOC_FUSION && !TARGET_POWERPC64)
4718 rs6000_isa_flags &= ~OPTION_MASK_TOC_FUSION;
4719 if ((rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION) != 0)
4720 warning (0, N_("-mtoc-fusion requires 64-bit"));
4723 if (TARGET_TOC_FUSION && (TARGET_CMODEL == CMODEL_SMALL))
4725 rs6000_isa_flags &= ~OPTION_MASK_TOC_FUSION;
4726 if ((rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION) != 0)
4727 warning (0, N_("-mtoc-fusion requires medium/large code model"));
4730 /* Turn on -mtoc-fusion by default if p8-fusion and 64-bit medium/large code
4731 model. */
4732 if (TARGET_P8_FUSION && !TARGET_TOC_FUSION && TARGET_POWERPC64
4733 && (TARGET_CMODEL != CMODEL_SMALL)
4734 && !(rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION))
4735 rs6000_isa_flags |= OPTION_MASK_TOC_FUSION;
4737 /* ISA 3.0 vector instructions include ISA 2.07. */
4738 if (TARGET_P9_VECTOR && !TARGET_P8_VECTOR)
4740 /* We prefer to not mention undocumented options in
4741 error messages. However, if users have managed to select
4742 power9-vector without selecting power8-vector, they
4743 already know about undocumented flags. */
4744 if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) &&
4745 (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR))
4746 error ("-mpower9-vector requires -mpower8-vector");
4747 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) == 0)
4749 rs6000_isa_flags &= ~OPTION_MASK_P9_VECTOR;
4750 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4751 rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
4753 else
4755 /* OPTION_MASK_P9_VECTOR is explicit and
4756 OPTION_MASK_P8_VECTOR is not explicit. */
4757 rs6000_isa_flags |= OPTION_MASK_P8_VECTOR;
4758 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4762 /* -mpower9-dform turns on both -mpower9-dform-scalar and
4763 -mpower9-dform-vector. */
4764 if (TARGET_P9_DFORM_BOTH > 0)
4766 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_VECTOR))
4767 rs6000_isa_flags |= OPTION_MASK_P9_DFORM_VECTOR;
4769 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_SCALAR))
4770 rs6000_isa_flags |= OPTION_MASK_P9_DFORM_SCALAR;
4772 else if (TARGET_P9_DFORM_BOTH == 0)
4774 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_VECTOR))
4775 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_VECTOR;
4777 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_SCALAR))
4778 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_SCALAR;
4781 /* ISA 3.0 D-form instructions require p9-vector and upper-regs. */
4782 if ((TARGET_P9_DFORM_SCALAR || TARGET_P9_DFORM_VECTOR) && !TARGET_P9_VECTOR)
4784 /* We prefer to not mention undocumented options in
4785 error messages. However, if users have managed to select
4786 power9-dform without selecting power9-vector, they
4787 already know about undocumented flags. */
4788 if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR)
4789 && (rs6000_isa_flags_explicit & (OPTION_MASK_P9_DFORM_SCALAR
4790 | OPTION_MASK_P9_DFORM_VECTOR)))
4791 error ("-mpower9-dform requires -mpower9-vector");
4792 else if (rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR)
4794 rs6000_isa_flags &=
4795 ~(OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4796 rs6000_isa_flags_explicit |=
4797 (OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4799 else
4801 /* We know that OPTION_MASK_P9_VECTOR is not explicit and
4802 OPTION_MASK_P9_DFORM_SCALAR or OPTION_MASK_P9_DORM_VECTOR
4803 may be explicit. */
4804 rs6000_isa_flags |= OPTION_MASK_P9_VECTOR;
4805 rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
4809 if ((TARGET_P9_DFORM_SCALAR || TARGET_P9_DFORM_VECTOR)
4810 && !TARGET_DIRECT_MOVE)
4812 /* We prefer to not mention undocumented options in
4813 error messages. However, if users have managed to select
4814 power9-dform without selecting direct-move, they
4815 already know about undocumented flags. */
4816 if ((rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
4817 && ((rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_VECTOR) ||
4818 (rs6000_isa_flags_explicit & OPTION_MASK_P9_DFORM_SCALAR) ||
4819 (TARGET_P9_DFORM_BOTH == 1)))
4820 error ("-mpower9-dform, -mpower9-dform-vector, -mpower9-dform-scalar"
4821 " require -mdirect-move");
4822 else if ((rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE) == 0)
4824 rs6000_isa_flags |= OPTION_MASK_DIRECT_MOVE;
4825 rs6000_isa_flags_explicit |= OPTION_MASK_DIRECT_MOVE;
4827 else
4829 rs6000_isa_flags &=
4830 ~(OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4831 rs6000_isa_flags_explicit |=
4832 (OPTION_MASK_P9_DFORM_SCALAR | OPTION_MASK_P9_DFORM_VECTOR);
4836 if (TARGET_P9_DFORM_SCALAR && !TARGET_UPPER_REGS_DF)
4838 /* We prefer to not mention undocumented options in
4839 error messages. However, if users have managed to select
4840 power9-dform without selecting upper-regs-df, they
4841 already know about undocumented flags. */
4842 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_DF)
4843 error ("-mpower9-dform requires -mupper-regs-df");
4844 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_SCALAR;
4847 if (TARGET_P9_DFORM_SCALAR && !TARGET_UPPER_REGS_SF)
4849 if (rs6000_isa_flags_explicit & OPTION_MASK_UPPER_REGS_SF)
4850 error ("-mpower9-dform requires -mupper-regs-sf");
4851 rs6000_isa_flags &= ~OPTION_MASK_P9_DFORM_SCALAR;
4854 /* Enable LRA by default. */
4855 if ((rs6000_isa_flags_explicit & OPTION_MASK_LRA) == 0)
4856 rs6000_isa_flags |= OPTION_MASK_LRA;
4858 /* There have been bugs with -mvsx-timode that don't show up with -mlra,
4859 but do show up with -mno-lra. Given -mlra will become the default once
4860 PR 69847 is fixed, turn off the options with problems by default if
4861 -mno-lra was used, and warn if the user explicitly asked for the option.
4863 Enable -mpower9-dform-vector by default if LRA and other power9 options.
4864 Enable -mvsx-timode by default if LRA and VSX. */
4865 if (!TARGET_LRA)
4867 if (TARGET_VSX_TIMODE)
4869 if ((rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE) != 0)
4870 warning (0, "-mvsx-timode might need -mlra");
4872 else
4873 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
4877 else
4879 if (TARGET_VSX && !TARGET_VSX_TIMODE
4880 && (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE) == 0)
4881 rs6000_isa_flags |= OPTION_MASK_VSX_TIMODE;
4884 /* Set -mallow-movmisalign to explicitly on if we have full ISA 2.07
4885 support. If we only have ISA 2.06 support, and the user did not specify
4886 the switch, leave it set to -1 so the movmisalign patterns are enabled,
4887 but we don't enable the full vectorization support */
4888 if (TARGET_ALLOW_MOVMISALIGN == -1 && TARGET_P8_VECTOR && TARGET_DIRECT_MOVE)
4889 TARGET_ALLOW_MOVMISALIGN = 1;
4891 else if (TARGET_ALLOW_MOVMISALIGN && !TARGET_VSX)
4893 if (TARGET_ALLOW_MOVMISALIGN > 0
4894 && global_options_set.x_TARGET_ALLOW_MOVMISALIGN)
4895 error ("-mallow-movmisalign requires -mvsx");
4897 TARGET_ALLOW_MOVMISALIGN = 0;
4900 /* Determine when unaligned vector accesses are permitted, and when
4901 they are preferred over masked Altivec loads. Note that if
4902 TARGET_ALLOW_MOVMISALIGN has been disabled by the user, then
4903 TARGET_EFFICIENT_UNALIGNED_VSX must be as well. The converse is
4904 not true. */
4905 if (TARGET_EFFICIENT_UNALIGNED_VSX)
4907 if (!TARGET_VSX)
4909 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4910 error ("-mefficient-unaligned-vsx requires -mvsx");
4912 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4915 else if (!TARGET_ALLOW_MOVMISALIGN)
4917 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4918 error ("-mefficient-unaligned-vsx requires -mallow-movmisalign");
4920 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4924 /* Check whether we should allow small integers into VSX registers. We
4925 require direct move to prevent the register allocator from having to move
4926 variables through memory to do moves. SImode can be used on ISA 2.07,
4927 while HImode and QImode require ISA 3.0. */
4928 if (TARGET_VSX_SMALL_INTEGER
4929 && (!TARGET_DIRECT_MOVE || !TARGET_P8_VECTOR || !TARGET_UPPER_REGS_DI))
4931 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_SMALL_INTEGER)
4932 error ("-mvsx-small-integer requires -mpower8-vector, "
4933 "-mupper-regs-di, and -mdirect-move");
4935 rs6000_isa_flags &= ~OPTION_MASK_VSX_SMALL_INTEGER;
4938 /* Set long double size before the IEEE 128-bit tests. */
4939 if (!global_options_set.x_rs6000_long_double_type_size)
4941 if (main_target_opt != NULL
4942 && (main_target_opt->x_rs6000_long_double_type_size
4943 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
4944 error ("target attribute or pragma changes long double size");
4945 else
4946 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
4949 /* Set -mabi=ieeelongdouble on some old targets. Note, AIX and Darwin
4950 explicitly redefine TARGET_IEEEQUAD to 0, so those systems will not
4951 pick up this default. */
4952 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
4953 if (!global_options_set.x_rs6000_ieeequad)
4954 rs6000_ieeequad = 1;
4955 #endif
4957 /* Enable the default support for IEEE 128-bit floating point on Linux VSX
4958 sytems, but don't enable the __float128 keyword. */
4959 if (TARGET_VSX && TARGET_LONG_DOUBLE_128
4960 && (TARGET_FLOAT128_ENABLE_TYPE || TARGET_IEEEQUAD)
4961 && ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_TYPE) == 0))
4962 rs6000_isa_flags |= OPTION_MASK_FLOAT128_TYPE;
4964 /* IEEE 128-bit floating point requires VSX support. */
4965 if (!TARGET_VSX)
4967 if (TARGET_FLOAT128_KEYWORD)
4969 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
4970 error ("-mfloat128 requires VSX support");
4972 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4973 | OPTION_MASK_FLOAT128_KEYWORD
4974 | OPTION_MASK_FLOAT128_HW);
4977 else if (TARGET_FLOAT128_TYPE)
4979 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_TYPE) != 0)
4980 error ("-mfloat128-type requires VSX support");
4982 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4983 | OPTION_MASK_FLOAT128_KEYWORD
4984 | OPTION_MASK_FLOAT128_HW);
4988 /* -mfloat128 and -mfloat128-hardware internally require the underlying IEEE
4989 128-bit floating point support to be enabled. */
4990 if (!TARGET_FLOAT128_TYPE)
4992 if (TARGET_FLOAT128_KEYWORD)
4994 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
4996 error ("-mfloat128 requires -mfloat128-type");
4997 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4998 | OPTION_MASK_FLOAT128_KEYWORD
4999 | OPTION_MASK_FLOAT128_HW);
5001 else
5002 rs6000_isa_flags |= OPTION_MASK_FLOAT128_TYPE;
5005 if (TARGET_FLOAT128_HW)
5007 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
5009 error ("-mfloat128-hardware requires -mfloat128-type");
5010 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
5012 else
5013 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
5014 | OPTION_MASK_FLOAT128_KEYWORD
5015 | OPTION_MASK_FLOAT128_HW);
5019 /* If we have -mfloat128-type and full ISA 3.0 support, enable
5020 -mfloat128-hardware by default. However, don't enable the __float128
5021 keyword. If the user explicitly turned on -mfloat128-hardware, enable the
5022 -mfloat128 option as well if it was not already set. */
5023 if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_HW
5024 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) == ISA_3_0_MASKS_IEEE
5025 && !(rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW))
5026 rs6000_isa_flags |= OPTION_MASK_FLOAT128_HW;
5028 if (TARGET_FLOAT128_HW
5029 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) != ISA_3_0_MASKS_IEEE)
5031 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
5032 error ("-mfloat128-hardware requires full ISA 3.0 support");
5034 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
5037 if (TARGET_FLOAT128_HW && !TARGET_64BIT)
5039 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
5040 error ("-mfloat128-hardware requires -m64");
5042 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
5045 if (TARGET_FLOAT128_HW && !TARGET_FLOAT128_KEYWORD
5046 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0
5047 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) == 0)
5048 rs6000_isa_flags |= OPTION_MASK_FLOAT128_KEYWORD;
5050 /* Print the options after updating the defaults. */
5051 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
5052 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
5054 /* E500mc does "better" if we inline more aggressively. Respect the
5055 user's opinion, though. */
5056 if (rs6000_block_move_inline_limit == 0
5057 && (rs6000_cpu == PROCESSOR_PPCE500MC
5058 || rs6000_cpu == PROCESSOR_PPCE500MC64
5059 || rs6000_cpu == PROCESSOR_PPCE5500
5060 || rs6000_cpu == PROCESSOR_PPCE6500))
5061 rs6000_block_move_inline_limit = 128;
5063 /* store_one_arg depends on expand_block_move to handle at least the
5064 size of reg_parm_stack_space. */
5065 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
5066 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
5068 if (global_init_p)
5070 /* If the appropriate debug option is enabled, replace the target hooks
5071 with debug versions that call the real version and then prints
5072 debugging information. */
5073 if (TARGET_DEBUG_COST)
5075 targetm.rtx_costs = rs6000_debug_rtx_costs;
5076 targetm.address_cost = rs6000_debug_address_cost;
5077 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
5080 if (TARGET_DEBUG_ADDR)
5082 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
5083 targetm.legitimize_address = rs6000_debug_legitimize_address;
5084 rs6000_secondary_reload_class_ptr
5085 = rs6000_debug_secondary_reload_class;
5086 rs6000_secondary_memory_needed_ptr
5087 = rs6000_debug_secondary_memory_needed;
5088 rs6000_cannot_change_mode_class_ptr
5089 = rs6000_debug_cannot_change_mode_class;
5090 rs6000_preferred_reload_class_ptr
5091 = rs6000_debug_preferred_reload_class;
5092 rs6000_legitimize_reload_address_ptr
5093 = rs6000_debug_legitimize_reload_address;
5094 rs6000_mode_dependent_address_ptr
5095 = rs6000_debug_mode_dependent_address;
5098 if (rs6000_veclibabi_name)
5100 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
5101 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
5102 else
5104 error ("unknown vectorization library ABI type (%s) for "
5105 "-mveclibabi= switch", rs6000_veclibabi_name);
5106 ret = false;
5111 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
5112 target attribute or pragma which automatically enables both options,
5113 unless the altivec ABI was set. This is set by default for 64-bit, but
5114 not for 32-bit. */
5115 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
5116 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC
5117 | OPTION_MASK_FLOAT128_TYPE
5118 | OPTION_MASK_FLOAT128_KEYWORD)
5119 & ~rs6000_isa_flags_explicit);
5121 /* Enable Altivec ABI for AIX -maltivec. */
5122 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
5124 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
5125 error ("target attribute or pragma changes AltiVec ABI");
5126 else
5127 rs6000_altivec_abi = 1;
5130 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
5131 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
5132 be explicitly overridden in either case. */
5133 if (TARGET_ELF)
5135 if (!global_options_set.x_rs6000_altivec_abi
5136 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
5138 if (main_target_opt != NULL &&
5139 !main_target_opt->x_rs6000_altivec_abi)
5140 error ("target attribute or pragma changes AltiVec ABI");
5141 else
5142 rs6000_altivec_abi = 1;
5146 /* Set the Darwin64 ABI as default for 64-bit Darwin.
5147 So far, the only darwin64 targets are also MACH-O. */
5148 if (TARGET_MACHO
5149 && DEFAULT_ABI == ABI_DARWIN
5150 && TARGET_64BIT)
5152 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
5153 error ("target attribute or pragma changes darwin64 ABI");
5154 else
5156 rs6000_darwin64_abi = 1;
5157 /* Default to natural alignment, for better performance. */
5158 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
5162 /* Place FP constants in the constant pool instead of TOC
5163 if section anchors enabled. */
5164 if (flag_section_anchors
5165 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
5166 TARGET_NO_FP_IN_TOC = 1;
5168 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
5169 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
5171 #ifdef SUBTARGET_OVERRIDE_OPTIONS
5172 SUBTARGET_OVERRIDE_OPTIONS;
5173 #endif
5174 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
5175 SUBSUBTARGET_OVERRIDE_OPTIONS;
5176 #endif
5177 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
5178 SUB3TARGET_OVERRIDE_OPTIONS;
5179 #endif
5181 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
5182 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
5184 /* For the E500 family of cores, reset the single/double FP flags to let us
5185 check that they remain constant across attributes or pragmas. Also,
5186 clear a possible request for string instructions, not supported and which
5187 we might have silently queried above for -Os.
5189 For other families, clear ISEL in case it was set implicitly.
5192 switch (rs6000_cpu)
5194 case PROCESSOR_PPC8540:
5195 case PROCESSOR_PPC8548:
5196 case PROCESSOR_PPCE500MC:
5197 case PROCESSOR_PPCE500MC64:
5198 case PROCESSOR_PPCE5500:
5199 case PROCESSOR_PPCE6500:
5201 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
5202 rs6000_double_float = TARGET_E500_DOUBLE;
5204 rs6000_isa_flags &= ~OPTION_MASK_STRING;
5206 break;
5208 default:
5210 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
5211 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
5213 break;
5216 if (main_target_opt)
5218 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
5219 error ("target attribute or pragma changes single precision floating "
5220 "point");
5221 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
5222 error ("target attribute or pragma changes double precision floating "
5223 "point");
5226 /* Detect invalid option combinations with E500. */
5227 CHECK_E500_OPTIONS;
5229 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
5230 && rs6000_cpu != PROCESSOR_POWER5
5231 && rs6000_cpu != PROCESSOR_POWER6
5232 && rs6000_cpu != PROCESSOR_POWER7
5233 && rs6000_cpu != PROCESSOR_POWER8
5234 && rs6000_cpu != PROCESSOR_POWER9
5235 && rs6000_cpu != PROCESSOR_PPCA2
5236 && rs6000_cpu != PROCESSOR_CELL
5237 && rs6000_cpu != PROCESSOR_PPC476);
5238 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
5239 || rs6000_cpu == PROCESSOR_POWER5
5240 || rs6000_cpu == PROCESSOR_POWER7
5241 || rs6000_cpu == PROCESSOR_POWER8);
5242 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
5243 || rs6000_cpu == PROCESSOR_POWER5
5244 || rs6000_cpu == PROCESSOR_POWER6
5245 || rs6000_cpu == PROCESSOR_POWER7
5246 || rs6000_cpu == PROCESSOR_POWER8
5247 || rs6000_cpu == PROCESSOR_POWER9
5248 || rs6000_cpu == PROCESSOR_PPCE500MC
5249 || rs6000_cpu == PROCESSOR_PPCE500MC64
5250 || rs6000_cpu == PROCESSOR_PPCE5500
5251 || rs6000_cpu == PROCESSOR_PPCE6500);
5253 /* Allow debug switches to override the above settings. These are set to -1
5254 in powerpcspe.opt to indicate the user hasn't directly set the switch. */
5255 if (TARGET_ALWAYS_HINT >= 0)
5256 rs6000_always_hint = TARGET_ALWAYS_HINT;
5258 if (TARGET_SCHED_GROUPS >= 0)
5259 rs6000_sched_groups = TARGET_SCHED_GROUPS;
5261 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
5262 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
5264 rs6000_sched_restricted_insns_priority
5265 = (rs6000_sched_groups ? 1 : 0);
5267 /* Handle -msched-costly-dep option. */
5268 rs6000_sched_costly_dep
5269 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
5271 if (rs6000_sched_costly_dep_str)
5273 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
5274 rs6000_sched_costly_dep = no_dep_costly;
5275 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
5276 rs6000_sched_costly_dep = all_deps_costly;
5277 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
5278 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
5279 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
5280 rs6000_sched_costly_dep = store_to_load_dep_costly;
5281 else
5282 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
5283 atoi (rs6000_sched_costly_dep_str));
5286 /* Handle -minsert-sched-nops option. */
5287 rs6000_sched_insert_nops
5288 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
5290 if (rs6000_sched_insert_nops_str)
5292 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
5293 rs6000_sched_insert_nops = sched_finish_none;
5294 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
5295 rs6000_sched_insert_nops = sched_finish_pad_groups;
5296 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
5297 rs6000_sched_insert_nops = sched_finish_regroup_exact;
5298 else
5299 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
5300 atoi (rs6000_sched_insert_nops_str));
5303 /* Handle stack protector */
5304 if (!global_options_set.x_rs6000_stack_protector_guard)
5305 #ifdef TARGET_THREAD_SSP_OFFSET
5306 rs6000_stack_protector_guard = SSP_TLS;
5307 #else
5308 rs6000_stack_protector_guard = SSP_GLOBAL;
5309 #endif
5311 #ifdef TARGET_THREAD_SSP_OFFSET
5312 rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
5313 rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
5314 #endif
5316 if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
5318 char *endp;
5319 const char *str = rs6000_stack_protector_guard_offset_str;
5321 errno = 0;
5322 long offset = strtol (str, &endp, 0);
5323 if (!*str || *endp || errno)
5324 error ("%qs is not a valid number "
5325 "in -mstack-protector-guard-offset=", str);
5327 if (!IN_RANGE (offset, -0x8000, 0x7fff)
5328 || (TARGET_64BIT && (offset & 3)))
5329 error ("%qs is not a valid offset "
5330 "in -mstack-protector-guard-offset=", str);
5332 rs6000_stack_protector_guard_offset = offset;
5335 if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
5337 const char *str = rs6000_stack_protector_guard_reg_str;
5338 int reg = decode_reg_name (str);
5340 if (!IN_RANGE (reg, 1, 31))
5341 error ("%qs is not a valid base register "
5342 "in -mstack-protector-guard-reg=", str);
5344 rs6000_stack_protector_guard_reg = reg;
5347 if (rs6000_stack_protector_guard == SSP_TLS
5348 && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
5349 error ("-mstack-protector-guard=tls needs a valid base register");
5351 if (global_init_p)
5353 #ifdef TARGET_REGNAMES
5354 /* If the user desires alternate register names, copy in the
5355 alternate names now. */
5356 if (TARGET_REGNAMES)
5357 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
5358 #endif
5360 /* Set aix_struct_return last, after the ABI is determined.
5361 If -maix-struct-return or -msvr4-struct-return was explicitly
5362 used, don't override with the ABI default. */
5363 if (!global_options_set.x_aix_struct_return)
5364 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
5366 #if 0
5367 /* IBM XL compiler defaults to unsigned bitfields. */
5368 if (TARGET_XL_COMPAT)
5369 flag_signed_bitfields = 0;
5370 #endif
5372 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
5373 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
5375 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
5377 /* We can only guarantee the availability of DI pseudo-ops when
5378 assembling for 64-bit targets. */
5379 if (!TARGET_64BIT)
5381 targetm.asm_out.aligned_op.di = NULL;
5382 targetm.asm_out.unaligned_op.di = NULL;
5386 /* Set branch target alignment, if not optimizing for size. */
5387 if (!optimize_size)
5389 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
5390 aligned 8byte to avoid misprediction by the branch predictor. */
5391 if (rs6000_cpu == PROCESSOR_TITAN
5392 || rs6000_cpu == PROCESSOR_CELL)
5394 if (align_functions <= 0)
5395 align_functions = 8;
5396 if (align_jumps <= 0)
5397 align_jumps = 8;
5398 if (align_loops <= 0)
5399 align_loops = 8;
5401 if (rs6000_align_branch_targets)
5403 if (align_functions <= 0)
5404 align_functions = 16;
5405 if (align_jumps <= 0)
5406 align_jumps = 16;
5407 if (align_loops <= 0)
5409 can_override_loop_align = 1;
5410 align_loops = 16;
5413 if (align_jumps_max_skip <= 0)
5414 align_jumps_max_skip = 15;
5415 if (align_loops_max_skip <= 0)
5416 align_loops_max_skip = 15;
5419 /* Arrange to save and restore machine status around nested functions. */
5420 init_machine_status = rs6000_init_machine_status;
5422 /* We should always be splitting complex arguments, but we can't break
5423 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
5424 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
5425 targetm.calls.split_complex_arg = NULL;
5427 /* The AIX and ELFv1 ABIs define standard function descriptors. */
5428 if (DEFAULT_ABI == ABI_AIX)
5429 targetm.calls.custom_function_descriptors = 0;
5432 /* Initialize rs6000_cost with the appropriate target costs. */
5433 if (optimize_size)
5434 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
5435 else
5436 switch (rs6000_cpu)
5438 case PROCESSOR_RS64A:
5439 rs6000_cost = &rs64a_cost;
5440 break;
5442 case PROCESSOR_MPCCORE:
5443 rs6000_cost = &mpccore_cost;
5444 break;
5446 case PROCESSOR_PPC403:
5447 rs6000_cost = &ppc403_cost;
5448 break;
5450 case PROCESSOR_PPC405:
5451 rs6000_cost = &ppc405_cost;
5452 break;
5454 case PROCESSOR_PPC440:
5455 rs6000_cost = &ppc440_cost;
5456 break;
5458 case PROCESSOR_PPC476:
5459 rs6000_cost = &ppc476_cost;
5460 break;
5462 case PROCESSOR_PPC601:
5463 rs6000_cost = &ppc601_cost;
5464 break;
5466 case PROCESSOR_PPC603:
5467 rs6000_cost = &ppc603_cost;
5468 break;
5470 case PROCESSOR_PPC604:
5471 rs6000_cost = &ppc604_cost;
5472 break;
5474 case PROCESSOR_PPC604e:
5475 rs6000_cost = &ppc604e_cost;
5476 break;
5478 case PROCESSOR_PPC620:
5479 rs6000_cost = &ppc620_cost;
5480 break;
5482 case PROCESSOR_PPC630:
5483 rs6000_cost = &ppc630_cost;
5484 break;
5486 case PROCESSOR_CELL:
5487 rs6000_cost = &ppccell_cost;
5488 break;
5490 case PROCESSOR_PPC750:
5491 case PROCESSOR_PPC7400:
5492 rs6000_cost = &ppc750_cost;
5493 break;
5495 case PROCESSOR_PPC7450:
5496 rs6000_cost = &ppc7450_cost;
5497 break;
5499 case PROCESSOR_PPC8540:
5500 case PROCESSOR_PPC8548:
5501 rs6000_cost = &ppc8540_cost;
5502 break;
5504 case PROCESSOR_PPCE300C2:
5505 case PROCESSOR_PPCE300C3:
5506 rs6000_cost = &ppce300c2c3_cost;
5507 break;
5509 case PROCESSOR_PPCE500MC:
5510 rs6000_cost = &ppce500mc_cost;
5511 break;
5513 case PROCESSOR_PPCE500MC64:
5514 rs6000_cost = &ppce500mc64_cost;
5515 break;
5517 case PROCESSOR_PPCE5500:
5518 rs6000_cost = &ppce5500_cost;
5519 break;
5521 case PROCESSOR_PPCE6500:
5522 rs6000_cost = &ppce6500_cost;
5523 break;
5525 case PROCESSOR_TITAN:
5526 rs6000_cost = &titan_cost;
5527 break;
5529 case PROCESSOR_POWER4:
5530 case PROCESSOR_POWER5:
5531 rs6000_cost = &power4_cost;
5532 break;
5534 case PROCESSOR_POWER6:
5535 rs6000_cost = &power6_cost;
5536 break;
5538 case PROCESSOR_POWER7:
5539 rs6000_cost = &power7_cost;
5540 break;
5542 case PROCESSOR_POWER8:
5543 rs6000_cost = &power8_cost;
5544 break;
5546 case PROCESSOR_POWER9:
5547 rs6000_cost = &power9_cost;
5548 break;
5550 case PROCESSOR_PPCA2:
5551 rs6000_cost = &ppca2_cost;
5552 break;
5554 default:
5555 gcc_unreachable ();
5558 if (global_init_p)
5560 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
5561 rs6000_cost->simultaneous_prefetches,
5562 global_options.x_param_values,
5563 global_options_set.x_param_values);
5564 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
5565 global_options.x_param_values,
5566 global_options_set.x_param_values);
5567 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
5568 rs6000_cost->cache_line_size,
5569 global_options.x_param_values,
5570 global_options_set.x_param_values);
5571 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
5572 global_options.x_param_values,
5573 global_options_set.x_param_values);
5575 /* Increase loop peeling limits based on performance analysis. */
5576 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
5577 global_options.x_param_values,
5578 global_options_set.x_param_values);
5579 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
5580 global_options.x_param_values,
5581 global_options_set.x_param_values);
5583 /* Use the 'model' -fsched-pressure algorithm by default. */
5584 maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM,
5585 SCHED_PRESSURE_MODEL,
5586 global_options.x_param_values,
5587 global_options_set.x_param_values);
5589 /* If using typedef char *va_list, signal that
5590 __builtin_va_start (&ap, 0) can be optimized to
5591 ap = __builtin_next_arg (0). */
5592 if (DEFAULT_ABI != ABI_V4)
5593 targetm.expand_builtin_va_start = NULL;
5596 /* Set up single/double float flags.
5597 If TARGET_HARD_FLOAT is set, but neither single or double is set,
5598 then set both flags. */
5599 if (TARGET_HARD_FLOAT && TARGET_FPRS
5600 && rs6000_single_float == 0 && rs6000_double_float == 0)
5601 rs6000_single_float = rs6000_double_float = 1;
5603 /* If not explicitly specified via option, decide whether to generate indexed
5604 load/store instructions. A value of -1 indicates that the
5605 initial value of this variable has not been overwritten. During
5606 compilation, TARGET_AVOID_XFORM is either 0 or 1. */
5607 if (TARGET_AVOID_XFORM == -1)
5608 /* Avoid indexed addressing when targeting Power6 in order to avoid the
5609 DERAT mispredict penalty. However the LVE and STVE altivec instructions
5610 need indexed accesses and the type used is the scalar type of the element
5611 being loaded or stored. */
5612 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
5613 && !TARGET_ALTIVEC);
5615 /* Set the -mrecip options. */
5616 if (rs6000_recip_name)
5618 char *p = ASTRDUP (rs6000_recip_name);
5619 char *q;
5620 unsigned int mask, i;
5621 bool invert;
5623 while ((q = strtok (p, ",")) != NULL)
5625 p = NULL;
5626 if (*q == '!')
5628 invert = true;
5629 q++;
5631 else
5632 invert = false;
5634 if (!strcmp (q, "default"))
5635 mask = ((TARGET_RECIP_PRECISION)
5636 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
5637 else
5639 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
5640 if (!strcmp (q, recip_options[i].string))
5642 mask = recip_options[i].mask;
5643 break;
5646 if (i == ARRAY_SIZE (recip_options))
5648 error ("unknown option for -mrecip=%s", q);
5649 invert = false;
5650 mask = 0;
5651 ret = false;
5655 if (invert)
5656 rs6000_recip_control &= ~mask;
5657 else
5658 rs6000_recip_control |= mask;
5662 /* Set the builtin mask of the various options used that could affect which
5663 builtins were used. In the past we used target_flags, but we've run out
5664 of bits, and some options like SPE and PAIRED are no longer in
5665 target_flags. */
5666 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
5667 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
5668 rs6000_print_builtin_options (stderr, 0, "builtin mask",
5669 rs6000_builtin_mask);
5671 /* Initialize all of the registers. */
5672 rs6000_init_hard_regno_mode_ok (global_init_p);
5674 /* Save the initial options in case the user does function specific options */
5675 if (global_init_p)
5676 target_option_default_node = target_option_current_node
5677 = build_target_option_node (&global_options);
5679 /* If not explicitly specified via option, decide whether to generate the
5680 extra blr's required to preserve the link stack on some cpus (eg, 476). */
5681 if (TARGET_LINK_STACK == -1)
5682 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
5684 return ret;
5687 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
5688 define the target cpu type. */
5690 static void
5691 rs6000_option_override (void)
5693 (void) rs6000_option_override_internal (true);
5697 /* Implement targetm.vectorize.builtin_mask_for_load. */
5698 static tree
5699 rs6000_builtin_mask_for_load (void)
5701 /* Don't use lvsl/vperm for P8 and similarly efficient machines. */
5702 if ((TARGET_ALTIVEC && !TARGET_VSX)
5703 || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
5704 return altivec_builtin_mask_for_load;
5705 else
5706 return 0;
5709 /* Implement LOOP_ALIGN. */
5711 rs6000_loop_align (rtx label)
5713 basic_block bb;
5714 int ninsns;
5716 /* Don't override loop alignment if -falign-loops was specified. */
5717 if (!can_override_loop_align)
5718 return align_loops_log;
5720 bb = BLOCK_FOR_INSN (label);
5721 ninsns = num_loop_insns(bb->loop_father);
5723 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
5724 if (ninsns > 4 && ninsns <= 8
5725 && (rs6000_cpu == PROCESSOR_POWER4
5726 || rs6000_cpu == PROCESSOR_POWER5
5727 || rs6000_cpu == PROCESSOR_POWER6
5728 || rs6000_cpu == PROCESSOR_POWER7
5729 || rs6000_cpu == PROCESSOR_POWER8
5730 || rs6000_cpu == PROCESSOR_POWER9))
5731 return 5;
5732 else
5733 return align_loops_log;
5736 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
5737 static int
5738 rs6000_loop_align_max_skip (rtx_insn *label)
5740 return (1 << rs6000_loop_align (label)) - 1;
5743 /* Return true iff, data reference of TYPE can reach vector alignment (16)
5744 after applying N number of iterations. This routine does not determine
5745 how may iterations are required to reach desired alignment. */
5747 static bool
5748 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
5750 if (is_packed)
5751 return false;
5753 if (TARGET_32BIT)
5755 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
5756 return true;
5758 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
5759 return true;
5761 return false;
5763 else
5765 if (TARGET_MACHO)
5766 return false;
5768 /* Assuming that all other types are naturally aligned. CHECKME! */
5769 return true;
5773 /* Return true if the vector misalignment factor is supported by the
5774 target. */
5775 static bool
5776 rs6000_builtin_support_vector_misalignment (machine_mode mode,
5777 const_tree type,
5778 int misalignment,
5779 bool is_packed)
5781 if (TARGET_VSX)
5783 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5784 return true;
5786 /* Return if movmisalign pattern is not supported for this mode. */
5787 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
5788 return false;
5790 if (misalignment == -1)
5792 /* Misalignment factor is unknown at compile time but we know
5793 it's word aligned. */
5794 if (rs6000_vector_alignment_reachable (type, is_packed))
5796 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
5798 if (element_size == 64 || element_size == 32)
5799 return true;
5802 return false;
5805 /* VSX supports word-aligned vector. */
5806 if (misalignment % 4 == 0)
5807 return true;
5809 return false;
5812 /* Implement targetm.vectorize.builtin_vectorization_cost. */
5813 static int
5814 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
5815 tree vectype, int misalign)
5817 unsigned elements;
5818 tree elem_type;
5820 switch (type_of_cost)
5822 case scalar_stmt:
5823 case scalar_load:
5824 case scalar_store:
5825 case vector_stmt:
5826 case vector_load:
5827 case vector_store:
5828 case vec_to_scalar:
5829 case scalar_to_vec:
5830 case cond_branch_not_taken:
5831 return 1;
5833 case vec_perm:
5834 if (TARGET_VSX)
5835 return 3;
5836 else
5837 return 1;
5839 case vec_promote_demote:
5840 if (TARGET_VSX)
5841 return 4;
5842 else
5843 return 1;
5845 case cond_branch_taken:
5846 return 3;
5848 case unaligned_load:
5849 if (TARGET_P9_VECTOR)
5850 return 3;
5852 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5853 return 1;
5855 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5857 elements = TYPE_VECTOR_SUBPARTS (vectype);
5858 if (elements == 2)
5859 /* Double word aligned. */
5860 return 2;
5862 if (elements == 4)
5864 switch (misalign)
5866 case 8:
5867 /* Double word aligned. */
5868 return 2;
5870 case -1:
5871 /* Unknown misalignment. */
5872 case 4:
5873 case 12:
5874 /* Word aligned. */
5875 return 22;
5877 default:
5878 gcc_unreachable ();
5883 if (TARGET_ALTIVEC)
5884 /* Misaligned loads are not supported. */
5885 gcc_unreachable ();
5887 return 2;
5889 case unaligned_store:
5890 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5891 return 1;
5893 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5895 elements = TYPE_VECTOR_SUBPARTS (vectype);
5896 if (elements == 2)
5897 /* Double word aligned. */
5898 return 2;
5900 if (elements == 4)
5902 switch (misalign)
5904 case 8:
5905 /* Double word aligned. */
5906 return 2;
5908 case -1:
5909 /* Unknown misalignment. */
5910 case 4:
5911 case 12:
5912 /* Word aligned. */
5913 return 23;
5915 default:
5916 gcc_unreachable ();
5921 if (TARGET_ALTIVEC)
5922 /* Misaligned stores are not supported. */
5923 gcc_unreachable ();
5925 return 2;
5927 case vec_construct:
5928 /* This is a rough approximation assuming non-constant elements
5929 constructed into a vector via element insertion. FIXME:
5930 vec_construct is not granular enough for uniformly good
5931 decisions. If the initialization is a splat, this is
5932 cheaper than we estimate. Improve this someday. */
5933 elem_type = TREE_TYPE (vectype);
5934 /* 32-bit vectors loaded into registers are stored as double
5935 precision, so we need 2 permutes, 2 converts, and 1 merge
5936 to construct a vector of short floats from them. */
5937 if (SCALAR_FLOAT_TYPE_P (elem_type)
5938 && TYPE_PRECISION (elem_type) == 32)
5939 return 5;
5940 /* On POWER9, integer vector types are built up in GPRs and then
5941 use a direct move (2 cycles). For POWER8 this is even worse,
5942 as we need two direct moves and a merge, and the direct moves
5943 are five cycles. */
5944 else if (INTEGRAL_TYPE_P (elem_type))
5946 if (TARGET_P9_VECTOR)
5947 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 2;
5948 else
5949 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 11;
5951 else
5952 /* V2DFmode doesn't need a direct move. */
5953 return 2;
5955 default:
5956 gcc_unreachable ();
5960 /* Implement targetm.vectorize.preferred_simd_mode. */
5962 static machine_mode
5963 rs6000_preferred_simd_mode (scalar_mode mode)
5965 if (TARGET_VSX)
5966 switch (mode)
5968 case E_DFmode:
5969 return V2DFmode;
5970 default:;
5972 if (TARGET_ALTIVEC || TARGET_VSX)
5973 switch (mode)
5975 case E_SFmode:
5976 return V4SFmode;
5977 case E_TImode:
5978 return V1TImode;
5979 case E_DImode:
5980 return V2DImode;
5981 case E_SImode:
5982 return V4SImode;
5983 case E_HImode:
5984 return V8HImode;
5985 case E_QImode:
5986 return V16QImode;
5987 default:;
5989 if (TARGET_SPE)
5990 switch (mode)
5992 case E_SFmode:
5993 return V2SFmode;
5994 case E_SImode:
5995 return V2SImode;
5996 default:;
5998 if (TARGET_PAIRED_FLOAT
5999 && mode == SFmode)
6000 return V2SFmode;
6001 return word_mode;
6004 typedef struct _rs6000_cost_data
6006 struct loop *loop_info;
6007 unsigned cost[3];
6008 } rs6000_cost_data;
6010 /* Test for likely overcommitment of vector hardware resources. If a
6011 loop iteration is relatively large, and too large a percentage of
6012 instructions in the loop are vectorized, the cost model may not
6013 adequately reflect delays from unavailable vector resources.
6014 Penalize the loop body cost for this case. */
6016 static void
6017 rs6000_density_test (rs6000_cost_data *data)
6019 const int DENSITY_PCT_THRESHOLD = 85;
6020 const int DENSITY_SIZE_THRESHOLD = 70;
6021 const int DENSITY_PENALTY = 10;
6022 struct loop *loop = data->loop_info;
6023 basic_block *bbs = get_loop_body (loop);
6024 int nbbs = loop->num_nodes;
6025 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
6026 int i, density_pct;
6028 for (i = 0; i < nbbs; i++)
6030 basic_block bb = bbs[i];
6031 gimple_stmt_iterator gsi;
6033 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6035 gimple *stmt = gsi_stmt (gsi);
6036 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6038 if (!STMT_VINFO_RELEVANT_P (stmt_info)
6039 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
6040 not_vec_cost++;
6044 free (bbs);
6045 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
6047 if (density_pct > DENSITY_PCT_THRESHOLD
6048 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
6050 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
6051 if (dump_enabled_p ())
6052 dump_printf_loc (MSG_NOTE, vect_location,
6053 "density %d%%, cost %d exceeds threshold, penalizing "
6054 "loop body cost by %d%%", density_pct,
6055 vec_cost + not_vec_cost, DENSITY_PENALTY);
6059 /* Implement targetm.vectorize.init_cost. */
6061 /* For each vectorized loop, this var holds TRUE iff a non-memory vector
6062 instruction is needed by the vectorization. */
6063 static bool rs6000_vect_nonmem;
6065 static void *
6066 rs6000_init_cost (struct loop *loop_info)
6068 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
6069 data->loop_info = loop_info;
6070 data->cost[vect_prologue] = 0;
6071 data->cost[vect_body] = 0;
6072 data->cost[vect_epilogue] = 0;
6073 rs6000_vect_nonmem = false;
6074 return data;
6077 /* Implement targetm.vectorize.add_stmt_cost. */
6079 static unsigned
6080 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
6081 struct _stmt_vec_info *stmt_info, int misalign,
6082 enum vect_cost_model_location where)
6084 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
6085 unsigned retval = 0;
6087 if (flag_vect_cost_model)
6089 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
6090 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
6091 misalign);
6092 /* Statements in an inner loop relative to the loop being
6093 vectorized are weighted more heavily. The value here is
6094 arbitrary and could potentially be improved with analysis. */
6095 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
6096 count *= 50; /* FIXME. */
6098 retval = (unsigned) (count * stmt_cost);
6099 cost_data->cost[where] += retval;
6101 /* Check whether we're doing something other than just a copy loop.
6102 Not all such loops may be profitably vectorized; see
6103 rs6000_finish_cost. */
6104 if ((kind == vec_to_scalar || kind == vec_perm
6105 || kind == vec_promote_demote || kind == vec_construct
6106 || kind == scalar_to_vec)
6107 || (where == vect_body && kind == vector_stmt))
6108 rs6000_vect_nonmem = true;
6111 return retval;
6114 /* Implement targetm.vectorize.finish_cost. */
6116 static void
6117 rs6000_finish_cost (void *data, unsigned *prologue_cost,
6118 unsigned *body_cost, unsigned *epilogue_cost)
6120 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
6122 if (cost_data->loop_info)
6123 rs6000_density_test (cost_data);
6125 /* Don't vectorize minimum-vectorization-factor, simple copy loops
6126 that require versioning for any reason. The vectorization is at
6127 best a wash inside the loop, and the versioning checks make
6128 profitability highly unlikely and potentially quite harmful. */
6129 if (cost_data->loop_info)
6131 loop_vec_info vec_info = loop_vec_info_for_loop (cost_data->loop_info);
6132 if (!rs6000_vect_nonmem
6133 && LOOP_VINFO_VECT_FACTOR (vec_info) == 2
6134 && LOOP_REQUIRES_VERSIONING (vec_info))
6135 cost_data->cost[vect_body] += 10000;
6138 *prologue_cost = cost_data->cost[vect_prologue];
6139 *body_cost = cost_data->cost[vect_body];
6140 *epilogue_cost = cost_data->cost[vect_epilogue];
6143 /* Implement targetm.vectorize.destroy_cost_data. */
6145 static void
6146 rs6000_destroy_cost_data (void *data)
6148 free (data);
6151 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
6152 library with vectorized intrinsics. */
6154 static tree
6155 rs6000_builtin_vectorized_libmass (combined_fn fn, tree type_out,
6156 tree type_in)
6158 char name[32];
6159 const char *suffix = NULL;
6160 tree fntype, new_fndecl, bdecl = NULL_TREE;
6161 int n_args = 1;
6162 const char *bname;
6163 machine_mode el_mode, in_mode;
6164 int n, in_n;
6166 /* Libmass is suitable for unsafe math only as it does not correctly support
6167 parts of IEEE with the required precision such as denormals. Only support
6168 it if we have VSX to use the simd d2 or f4 functions.
6169 XXX: Add variable length support. */
6170 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
6171 return NULL_TREE;
6173 el_mode = TYPE_MODE (TREE_TYPE (type_out));
6174 n = TYPE_VECTOR_SUBPARTS (type_out);
6175 in_mode = TYPE_MODE (TREE_TYPE (type_in));
6176 in_n = TYPE_VECTOR_SUBPARTS (type_in);
6177 if (el_mode != in_mode
6178 || n != in_n)
6179 return NULL_TREE;
6181 switch (fn)
6183 CASE_CFN_ATAN2:
6184 CASE_CFN_HYPOT:
6185 CASE_CFN_POW:
6186 n_args = 2;
6187 gcc_fallthrough ();
6189 CASE_CFN_ACOS:
6190 CASE_CFN_ACOSH:
6191 CASE_CFN_ASIN:
6192 CASE_CFN_ASINH:
6193 CASE_CFN_ATAN:
6194 CASE_CFN_ATANH:
6195 CASE_CFN_CBRT:
6196 CASE_CFN_COS:
6197 CASE_CFN_COSH:
6198 CASE_CFN_ERF:
6199 CASE_CFN_ERFC:
6200 CASE_CFN_EXP2:
6201 CASE_CFN_EXP:
6202 CASE_CFN_EXPM1:
6203 CASE_CFN_LGAMMA:
6204 CASE_CFN_LOG10:
6205 CASE_CFN_LOG1P:
6206 CASE_CFN_LOG2:
6207 CASE_CFN_LOG:
6208 CASE_CFN_SIN:
6209 CASE_CFN_SINH:
6210 CASE_CFN_SQRT:
6211 CASE_CFN_TAN:
6212 CASE_CFN_TANH:
6213 if (el_mode == DFmode && n == 2)
6215 bdecl = mathfn_built_in (double_type_node, fn);
6216 suffix = "d2"; /* pow -> powd2 */
6218 else if (el_mode == SFmode && n == 4)
6220 bdecl = mathfn_built_in (float_type_node, fn);
6221 suffix = "4"; /* powf -> powf4 */
6223 else
6224 return NULL_TREE;
6225 if (!bdecl)
6226 return NULL_TREE;
6227 break;
6229 default:
6230 return NULL_TREE;
6233 gcc_assert (suffix != NULL);
6234 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
6235 if (!bname)
6236 return NULL_TREE;
6238 strcpy (name, bname + sizeof ("__builtin_") - 1);
6239 strcat (name, suffix);
6241 if (n_args == 1)
6242 fntype = build_function_type_list (type_out, type_in, NULL);
6243 else if (n_args == 2)
6244 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
6245 else
6246 gcc_unreachable ();
6248 /* Build a function declaration for the vectorized function. */
6249 new_fndecl = build_decl (BUILTINS_LOCATION,
6250 FUNCTION_DECL, get_identifier (name), fntype);
6251 TREE_PUBLIC (new_fndecl) = 1;
6252 DECL_EXTERNAL (new_fndecl) = 1;
6253 DECL_IS_NOVOPS (new_fndecl) = 1;
6254 TREE_READONLY (new_fndecl) = 1;
6256 return new_fndecl;
6259 /* Returns a function decl for a vectorized version of the builtin function
6260 with builtin function code FN and the result vector type TYPE, or NULL_TREE
6261 if it is not available. */
6263 static tree
6264 rs6000_builtin_vectorized_function (unsigned int fn, tree type_out,
6265 tree type_in)
6267 machine_mode in_mode, out_mode;
6268 int in_n, out_n;
6270 if (TARGET_DEBUG_BUILTIN)
6271 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
6272 combined_fn_name (combined_fn (fn)),
6273 GET_MODE_NAME (TYPE_MODE (type_out)),
6274 GET_MODE_NAME (TYPE_MODE (type_in)));
6276 if (TREE_CODE (type_out) != VECTOR_TYPE
6277 || TREE_CODE (type_in) != VECTOR_TYPE
6278 || !TARGET_VECTORIZE_BUILTINS)
6279 return NULL_TREE;
6281 out_mode = TYPE_MODE (TREE_TYPE (type_out));
6282 out_n = TYPE_VECTOR_SUBPARTS (type_out);
6283 in_mode = TYPE_MODE (TREE_TYPE (type_in));
6284 in_n = TYPE_VECTOR_SUBPARTS (type_in);
6286 switch (fn)
6288 CASE_CFN_COPYSIGN:
6289 if (VECTOR_UNIT_VSX_P (V2DFmode)
6290 && out_mode == DFmode && out_n == 2
6291 && in_mode == DFmode && in_n == 2)
6292 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
6293 if (VECTOR_UNIT_VSX_P (V4SFmode)
6294 && out_mode == SFmode && out_n == 4
6295 && in_mode == SFmode && in_n == 4)
6296 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
6297 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
6298 && out_mode == SFmode && out_n == 4
6299 && in_mode == SFmode && in_n == 4)
6300 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
6301 break;
6302 CASE_CFN_CEIL:
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_XVRDPIP];
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_XVRSPIP];
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_VRFIP];
6315 break;
6316 CASE_CFN_FLOOR:
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_XVRDPIM];
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_XVRSPIM];
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_VRFIM];
6329 break;
6330 CASE_CFN_FMA:
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_XVMADDDP];
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_XVMADDSP];
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_VMADDFP];
6343 break;
6344 CASE_CFN_TRUNC:
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_XVRDPIZ];
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_XVRSPIZ];
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_VRFIZ];
6357 break;
6358 CASE_CFN_NEARBYINT:
6359 if (VECTOR_UNIT_VSX_P (V2DFmode)
6360 && flag_unsafe_math_optimizations
6361 && out_mode == DFmode && out_n == 2
6362 && in_mode == DFmode && in_n == 2)
6363 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
6364 if (VECTOR_UNIT_VSX_P (V4SFmode)
6365 && flag_unsafe_math_optimizations
6366 && out_mode == SFmode && out_n == 4
6367 && in_mode == SFmode && in_n == 4)
6368 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
6369 break;
6370 CASE_CFN_RINT:
6371 if (VECTOR_UNIT_VSX_P (V2DFmode)
6372 && !flag_trapping_math
6373 && out_mode == DFmode && out_n == 2
6374 && in_mode == DFmode && in_n == 2)
6375 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
6376 if (VECTOR_UNIT_VSX_P (V4SFmode)
6377 && !flag_trapping_math
6378 && out_mode == SFmode && out_n == 4
6379 && in_mode == SFmode && in_n == 4)
6380 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
6381 break;
6382 default:
6383 break;
6386 /* Generate calls to libmass if appropriate. */
6387 if (rs6000_veclib_handler)
6388 return rs6000_veclib_handler (combined_fn (fn), type_out, type_in);
6390 return NULL_TREE;
6393 /* Implement TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION. */
6395 static tree
6396 rs6000_builtin_md_vectorized_function (tree fndecl, tree type_out,
6397 tree type_in)
6399 machine_mode in_mode, out_mode;
6400 int in_n, out_n;
6402 if (TARGET_DEBUG_BUILTIN)
6403 fprintf (stderr, "rs6000_builtin_md_vectorized_function (%s, %s, %s)\n",
6404 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
6405 GET_MODE_NAME (TYPE_MODE (type_out)),
6406 GET_MODE_NAME (TYPE_MODE (type_in)));
6408 if (TREE_CODE (type_out) != VECTOR_TYPE
6409 || TREE_CODE (type_in) != VECTOR_TYPE
6410 || !TARGET_VECTORIZE_BUILTINS)
6411 return NULL_TREE;
6413 out_mode = TYPE_MODE (TREE_TYPE (type_out));
6414 out_n = TYPE_VECTOR_SUBPARTS (type_out);
6415 in_mode = TYPE_MODE (TREE_TYPE (type_in));
6416 in_n = TYPE_VECTOR_SUBPARTS (type_in);
6418 enum rs6000_builtins fn
6419 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
6420 switch (fn)
6422 case RS6000_BUILTIN_RSQRTF:
6423 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
6424 && out_mode == SFmode && out_n == 4
6425 && in_mode == SFmode && in_n == 4)
6426 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
6427 break;
6428 case RS6000_BUILTIN_RSQRT:
6429 if (VECTOR_UNIT_VSX_P (V2DFmode)
6430 && out_mode == DFmode && out_n == 2
6431 && in_mode == DFmode && in_n == 2)
6432 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
6433 break;
6434 case RS6000_BUILTIN_RECIPF:
6435 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
6436 && out_mode == SFmode && out_n == 4
6437 && in_mode == SFmode && in_n == 4)
6438 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
6439 break;
6440 case RS6000_BUILTIN_RECIP:
6441 if (VECTOR_UNIT_VSX_P (V2DFmode)
6442 && out_mode == DFmode && out_n == 2
6443 && in_mode == DFmode && in_n == 2)
6444 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
6445 break;
6446 default:
6447 break;
6449 return NULL_TREE;
6452 /* Default CPU string for rs6000*_file_start functions. */
6453 static const char *rs6000_default_cpu;
6455 /* Do anything needed at the start of the asm file. */
6457 static void
6458 rs6000_file_start (void)
6460 char buffer[80];
6461 const char *start = buffer;
6462 FILE *file = asm_out_file;
6464 rs6000_default_cpu = TARGET_CPU_DEFAULT;
6466 default_file_start ();
6468 if (flag_verbose_asm)
6470 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
6472 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
6474 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
6475 start = "";
6478 if (global_options_set.x_rs6000_cpu_index)
6480 fprintf (file, "%s -mcpu=%s", start,
6481 processor_target_table[rs6000_cpu_index].name);
6482 start = "";
6485 if (global_options_set.x_rs6000_tune_index)
6487 fprintf (file, "%s -mtune=%s", start,
6488 processor_target_table[rs6000_tune_index].name);
6489 start = "";
6492 if (PPC405_ERRATUM77)
6494 fprintf (file, "%s PPC405CR_ERRATUM77", start);
6495 start = "";
6498 #ifdef USING_ELFOS_H
6499 switch (rs6000_sdata)
6501 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
6502 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
6503 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
6504 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
6507 if (rs6000_sdata && g_switch_value)
6509 fprintf (file, "%s -G %d", start,
6510 g_switch_value);
6511 start = "";
6513 #endif
6515 if (*start == '\0')
6516 putc ('\n', file);
6519 #ifdef USING_ELFOS_H
6520 if (!(rs6000_default_cpu && rs6000_default_cpu[0])
6521 && !global_options_set.x_rs6000_cpu_index)
6523 fputs ("\t.machine ", asm_out_file);
6524 if ((rs6000_isa_flags & OPTION_MASK_MODULO) != 0)
6525 fputs ("power9\n", asm_out_file);
6526 else if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
6527 fputs ("power8\n", asm_out_file);
6528 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTD) != 0)
6529 fputs ("power7\n", asm_out_file);
6530 else if ((rs6000_isa_flags & OPTION_MASK_CMPB) != 0)
6531 fputs ("power6\n", asm_out_file);
6532 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTB) != 0)
6533 fputs ("power5\n", asm_out_file);
6534 else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
6535 fputs ("power4\n", asm_out_file);
6536 else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
6537 fputs ("ppc64\n", asm_out_file);
6538 else
6539 fputs ("ppc\n", asm_out_file);
6541 #endif
6543 if (DEFAULT_ABI == ABI_ELFv2)
6544 fprintf (file, "\t.abiversion 2\n");
6548 /* Return nonzero if this function is known to have a null epilogue. */
6551 direct_return (void)
6553 if (reload_completed)
6555 rs6000_stack_t *info = rs6000_stack_info ();
6557 if (info->first_gp_reg_save == 32
6558 && info->first_fp_reg_save == 64
6559 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
6560 && ! info->lr_save_p
6561 && ! info->cr_save_p
6562 && info->vrsave_size == 0
6563 && ! info->push_p)
6564 return 1;
6567 return 0;
6570 /* Return the number of instructions it takes to form a constant in an
6571 integer register. */
6574 num_insns_constant_wide (HOST_WIDE_INT value)
6576 /* signed constant loadable with addi */
6577 if (((unsigned HOST_WIDE_INT) value + 0x8000) < 0x10000)
6578 return 1;
6580 /* constant loadable with addis */
6581 else if ((value & 0xffff) == 0
6582 && (value >> 31 == -1 || value >> 31 == 0))
6583 return 1;
6585 else if (TARGET_POWERPC64)
6587 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
6588 HOST_WIDE_INT high = value >> 31;
6590 if (high == 0 || high == -1)
6591 return 2;
6593 high >>= 1;
6595 if (low == 0)
6596 return num_insns_constant_wide (high) + 1;
6597 else if (high == 0)
6598 return num_insns_constant_wide (low) + 1;
6599 else
6600 return (num_insns_constant_wide (high)
6601 + num_insns_constant_wide (low) + 1);
6604 else
6605 return 2;
6609 num_insns_constant (rtx op, machine_mode mode)
6611 HOST_WIDE_INT low, high;
6613 switch (GET_CODE (op))
6615 case CONST_INT:
6616 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
6617 && rs6000_is_valid_and_mask (op, mode))
6618 return 2;
6619 else
6620 return num_insns_constant_wide (INTVAL (op));
6622 case CONST_WIDE_INT:
6624 int i;
6625 int ins = CONST_WIDE_INT_NUNITS (op) - 1;
6626 for (i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
6627 ins += num_insns_constant_wide (CONST_WIDE_INT_ELT (op, i));
6628 return ins;
6631 case CONST_DOUBLE:
6632 if (mode == SFmode || mode == SDmode)
6634 long l;
6636 if (DECIMAL_FLOAT_MODE_P (mode))
6637 REAL_VALUE_TO_TARGET_DECIMAL32
6638 (*CONST_DOUBLE_REAL_VALUE (op), l);
6639 else
6640 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), l);
6641 return num_insns_constant_wide ((HOST_WIDE_INT) l);
6644 long l[2];
6645 if (DECIMAL_FLOAT_MODE_P (mode))
6646 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (op), l);
6647 else
6648 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (op), l);
6649 high = l[WORDS_BIG_ENDIAN == 0];
6650 low = l[WORDS_BIG_ENDIAN != 0];
6652 if (TARGET_32BIT)
6653 return (num_insns_constant_wide (low)
6654 + num_insns_constant_wide (high));
6655 else
6657 if ((high == 0 && low >= 0)
6658 || (high == -1 && low < 0))
6659 return num_insns_constant_wide (low);
6661 else if (rs6000_is_valid_and_mask (op, mode))
6662 return 2;
6664 else if (low == 0)
6665 return num_insns_constant_wide (high) + 1;
6667 else
6668 return (num_insns_constant_wide (high)
6669 + num_insns_constant_wide (low) + 1);
6672 default:
6673 gcc_unreachable ();
6677 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
6678 If the mode of OP is MODE_VECTOR_INT, this simply returns the
6679 corresponding element of the vector, but for V4SFmode and V2SFmode,
6680 the corresponding "float" is interpreted as an SImode integer. */
6682 HOST_WIDE_INT
6683 const_vector_elt_as_int (rtx op, unsigned int elt)
6685 rtx tmp;
6687 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
6688 gcc_assert (GET_MODE (op) != V2DImode
6689 && GET_MODE (op) != V2DFmode);
6691 tmp = CONST_VECTOR_ELT (op, elt);
6692 if (GET_MODE (op) == V4SFmode
6693 || GET_MODE (op) == V2SFmode)
6694 tmp = gen_lowpart (SImode, tmp);
6695 return INTVAL (tmp);
6698 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
6699 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
6700 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
6701 all items are set to the same value and contain COPIES replicas of the
6702 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
6703 operand and the others are set to the value of the operand's msb. */
6705 static bool
6706 vspltis_constant (rtx op, unsigned step, unsigned copies)
6708 machine_mode mode = GET_MODE (op);
6709 machine_mode inner = GET_MODE_INNER (mode);
6711 unsigned i;
6712 unsigned nunits;
6713 unsigned bitsize;
6714 unsigned mask;
6716 HOST_WIDE_INT val;
6717 HOST_WIDE_INT splat_val;
6718 HOST_WIDE_INT msb_val;
6720 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
6721 return false;
6723 nunits = GET_MODE_NUNITS (mode);
6724 bitsize = GET_MODE_BITSIZE (inner);
6725 mask = GET_MODE_MASK (inner);
6727 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6728 splat_val = val;
6729 msb_val = val >= 0 ? 0 : -1;
6731 /* Construct the value to be splatted, if possible. If not, return 0. */
6732 for (i = 2; i <= copies; i *= 2)
6734 HOST_WIDE_INT small_val;
6735 bitsize /= 2;
6736 small_val = splat_val >> bitsize;
6737 mask >>= bitsize;
6738 if (splat_val != ((HOST_WIDE_INT)
6739 ((unsigned HOST_WIDE_INT) small_val << bitsize)
6740 | (small_val & mask)))
6741 return false;
6742 splat_val = small_val;
6745 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
6746 if (EASY_VECTOR_15 (splat_val))
6749 /* Also check if we can splat, and then add the result to itself. Do so if
6750 the value is positive, of if the splat instruction is using OP's mode;
6751 for splat_val < 0, the splat and the add should use the same mode. */
6752 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
6753 && (splat_val >= 0 || (step == 1 && copies == 1)))
6756 /* Also check if are loading up the most significant bit which can be done by
6757 loading up -1 and shifting the value left by -1. */
6758 else if (EASY_VECTOR_MSB (splat_val, inner))
6761 else
6762 return false;
6764 /* Check if VAL is present in every STEP-th element, and the
6765 other elements are filled with its most significant bit. */
6766 for (i = 1; i < nunits; ++i)
6768 HOST_WIDE_INT desired_val;
6769 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
6770 if ((i & (step - 1)) == 0)
6771 desired_val = val;
6772 else
6773 desired_val = msb_val;
6775 if (desired_val != const_vector_elt_as_int (op, elt))
6776 return false;
6779 return true;
6782 /* Like vsplitis_constant, but allow the value to be shifted left with a VSLDOI
6783 instruction, filling in the bottom elements with 0 or -1.
6785 Return 0 if the constant cannot be generated with VSLDOI. Return positive
6786 for the number of zeroes to shift in, or negative for the number of 0xff
6787 bytes to shift in.
6789 OP is a CONST_VECTOR. */
6792 vspltis_shifted (rtx op)
6794 machine_mode mode = GET_MODE (op);
6795 machine_mode inner = GET_MODE_INNER (mode);
6797 unsigned i, j;
6798 unsigned nunits;
6799 unsigned mask;
6801 HOST_WIDE_INT val;
6803 if (mode != V16QImode && mode != V8HImode && mode != V4SImode)
6804 return false;
6806 /* We need to create pseudo registers to do the shift, so don't recognize
6807 shift vector constants after reload. */
6808 if (!can_create_pseudo_p ())
6809 return false;
6811 nunits = GET_MODE_NUNITS (mode);
6812 mask = GET_MODE_MASK (inner);
6814 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? 0 : nunits - 1);
6816 /* Check if the value can really be the operand of a vspltis[bhw]. */
6817 if (EASY_VECTOR_15 (val))
6820 /* Also check if we are loading up the most significant bit which can be done
6821 by loading up -1 and shifting the value left by -1. */
6822 else if (EASY_VECTOR_MSB (val, inner))
6825 else
6826 return 0;
6828 /* Check if VAL is present in every STEP-th element until we find elements
6829 that are 0 or all 1 bits. */
6830 for (i = 1; i < nunits; ++i)
6832 unsigned elt = BYTES_BIG_ENDIAN ? i : nunits - 1 - i;
6833 HOST_WIDE_INT elt_val = const_vector_elt_as_int (op, elt);
6835 /* If the value isn't the splat value, check for the remaining elements
6836 being 0/-1. */
6837 if (val != elt_val)
6839 if (elt_val == 0)
6841 for (j = i+1; j < nunits; ++j)
6843 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6844 if (const_vector_elt_as_int (op, elt2) != 0)
6845 return 0;
6848 return (nunits - i) * GET_MODE_SIZE (inner);
6851 else if ((elt_val & mask) == mask)
6853 for (j = i+1; j < nunits; ++j)
6855 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6856 if ((const_vector_elt_as_int (op, elt2) & mask) != mask)
6857 return 0;
6860 return -((nunits - i) * GET_MODE_SIZE (inner));
6863 else
6864 return 0;
6868 /* If all elements are equal, we don't need to do VLSDOI. */
6869 return 0;
6873 /* Return true if OP is of the given MODE and can be synthesized
6874 with a vspltisb, vspltish or vspltisw. */
6876 bool
6877 easy_altivec_constant (rtx op, machine_mode mode)
6879 unsigned step, copies;
6881 if (mode == VOIDmode)
6882 mode = GET_MODE (op);
6883 else if (mode != GET_MODE (op))
6884 return false;
6886 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
6887 constants. */
6888 if (mode == V2DFmode)
6889 return zero_constant (op, mode);
6891 else if (mode == V2DImode)
6893 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
6894 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
6895 return false;
6897 if (zero_constant (op, mode))
6898 return true;
6900 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
6901 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
6902 return true;
6904 return false;
6907 /* V1TImode is a special container for TImode. Ignore for now. */
6908 else if (mode == V1TImode)
6909 return false;
6911 /* Start with a vspltisw. */
6912 step = GET_MODE_NUNITS (mode) / 4;
6913 copies = 1;
6915 if (vspltis_constant (op, step, copies))
6916 return true;
6918 /* Then try with a vspltish. */
6919 if (step == 1)
6920 copies <<= 1;
6921 else
6922 step >>= 1;
6924 if (vspltis_constant (op, step, copies))
6925 return true;
6927 /* And finally a vspltisb. */
6928 if (step == 1)
6929 copies <<= 1;
6930 else
6931 step >>= 1;
6933 if (vspltis_constant (op, step, copies))
6934 return true;
6936 if (vspltis_shifted (op) != 0)
6937 return true;
6939 return false;
6942 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
6943 result is OP. Abort if it is not possible. */
6946 gen_easy_altivec_constant (rtx op)
6948 machine_mode mode = GET_MODE (op);
6949 int nunits = GET_MODE_NUNITS (mode);
6950 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6951 unsigned step = nunits / 4;
6952 unsigned copies = 1;
6954 /* Start with a vspltisw. */
6955 if (vspltis_constant (op, step, copies))
6956 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
6958 /* Then try with a vspltish. */
6959 if (step == 1)
6960 copies <<= 1;
6961 else
6962 step >>= 1;
6964 if (vspltis_constant (op, step, copies))
6965 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
6967 /* And finally a vspltisb. */
6968 if (step == 1)
6969 copies <<= 1;
6970 else
6971 step >>= 1;
6973 if (vspltis_constant (op, step, copies))
6974 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
6976 gcc_unreachable ();
6979 /* Return true if OP is of the given MODE and can be synthesized with ISA 3.0
6980 instructions (xxspltib, vupkhsb/vextsb2w/vextb2d).
6982 Return the number of instructions needed (1 or 2) into the address pointed
6983 via NUM_INSNS_PTR.
6985 Return the constant that is being split via CONSTANT_PTR. */
6987 bool
6988 xxspltib_constant_p (rtx op,
6989 machine_mode mode,
6990 int *num_insns_ptr,
6991 int *constant_ptr)
6993 size_t nunits = GET_MODE_NUNITS (mode);
6994 size_t i;
6995 HOST_WIDE_INT value;
6996 rtx element;
6998 /* Set the returned values to out of bound values. */
6999 *num_insns_ptr = -1;
7000 *constant_ptr = 256;
7002 if (!TARGET_P9_VECTOR)
7003 return false;
7005 if (mode == VOIDmode)
7006 mode = GET_MODE (op);
7008 else if (mode != GET_MODE (op) && GET_MODE (op) != VOIDmode)
7009 return false;
7011 /* Handle (vec_duplicate <constant>). */
7012 if (GET_CODE (op) == VEC_DUPLICATE)
7014 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
7015 && mode != V2DImode)
7016 return false;
7018 element = XEXP (op, 0);
7019 if (!CONST_INT_P (element))
7020 return false;
7022 value = INTVAL (element);
7023 if (!IN_RANGE (value, -128, 127))
7024 return false;
7027 /* Handle (const_vector [...]). */
7028 else if (GET_CODE (op) == CONST_VECTOR)
7030 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
7031 && mode != V2DImode)
7032 return false;
7034 element = CONST_VECTOR_ELT (op, 0);
7035 if (!CONST_INT_P (element))
7036 return false;
7038 value = INTVAL (element);
7039 if (!IN_RANGE (value, -128, 127))
7040 return false;
7042 for (i = 1; i < nunits; i++)
7044 element = CONST_VECTOR_ELT (op, i);
7045 if (!CONST_INT_P (element))
7046 return false;
7048 if (value != INTVAL (element))
7049 return false;
7053 /* Handle integer constants being loaded into the upper part of the VSX
7054 register as a scalar. If the value isn't 0/-1, only allow it if the mode
7055 can go in Altivec registers. Prefer VSPLTISW/VUPKHSW over XXSPLITIB. */
7056 else if (CONST_INT_P (op))
7058 if (!SCALAR_INT_MODE_P (mode))
7059 return false;
7061 value = INTVAL (op);
7062 if (!IN_RANGE (value, -128, 127))
7063 return false;
7065 if (!IN_RANGE (value, -1, 0))
7067 if (!(reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID))
7068 return false;
7070 if (EASY_VECTOR_15 (value))
7071 return false;
7075 else
7076 return false;
7078 /* See if we could generate vspltisw/vspltish directly instead of xxspltib +
7079 sign extend. Special case 0/-1 to allow getting any VSX register instead
7080 of an Altivec register. */
7081 if ((mode == V4SImode || mode == V8HImode) && !IN_RANGE (value, -1, 0)
7082 && EASY_VECTOR_15 (value))
7083 return false;
7085 /* Return # of instructions and the constant byte for XXSPLTIB. */
7086 if (mode == V16QImode)
7087 *num_insns_ptr = 1;
7089 else if (IN_RANGE (value, -1, 0))
7090 *num_insns_ptr = 1;
7092 else
7093 *num_insns_ptr = 2;
7095 *constant_ptr = (int) value;
7096 return true;
7099 const char *
7100 output_vec_const_move (rtx *operands)
7102 int cst, cst2, shift;
7103 machine_mode mode;
7104 rtx dest, vec;
7106 dest = operands[0];
7107 vec = operands[1];
7108 mode = GET_MODE (dest);
7110 if (TARGET_VSX)
7112 bool dest_vmx_p = ALTIVEC_REGNO_P (REGNO (dest));
7113 int xxspltib_value = 256;
7114 int num_insns = -1;
7116 if (zero_constant (vec, mode))
7118 if (TARGET_P9_VECTOR)
7119 return "xxspltib %x0,0";
7121 else if (dest_vmx_p)
7122 return "vspltisw %0,0";
7124 else
7125 return "xxlxor %x0,%x0,%x0";
7128 if (all_ones_constant (vec, mode))
7130 if (TARGET_P9_VECTOR)
7131 return "xxspltib %x0,255";
7133 else if (dest_vmx_p)
7134 return "vspltisw %0,-1";
7136 else if (TARGET_P8_VECTOR)
7137 return "xxlorc %x0,%x0,%x0";
7139 else
7140 gcc_unreachable ();
7143 if (TARGET_P9_VECTOR
7144 && xxspltib_constant_p (vec, mode, &num_insns, &xxspltib_value))
7146 if (num_insns == 1)
7148 operands[2] = GEN_INT (xxspltib_value & 0xff);
7149 return "xxspltib %x0,%2";
7152 return "#";
7156 if (TARGET_ALTIVEC)
7158 rtx splat_vec;
7160 gcc_assert (ALTIVEC_REGNO_P (REGNO (dest)));
7161 if (zero_constant (vec, mode))
7162 return "vspltisw %0,0";
7164 if (all_ones_constant (vec, mode))
7165 return "vspltisw %0,-1";
7167 /* Do we need to construct a value using VSLDOI? */
7168 shift = vspltis_shifted (vec);
7169 if (shift != 0)
7170 return "#";
7172 splat_vec = gen_easy_altivec_constant (vec);
7173 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
7174 operands[1] = XEXP (splat_vec, 0);
7175 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
7176 return "#";
7178 switch (GET_MODE (splat_vec))
7180 case E_V4SImode:
7181 return "vspltisw %0,%1";
7183 case E_V8HImode:
7184 return "vspltish %0,%1";
7186 case E_V16QImode:
7187 return "vspltisb %0,%1";
7189 default:
7190 gcc_unreachable ();
7194 gcc_assert (TARGET_SPE);
7196 /* Vector constant 0 is handled as a splitter of V2SI, and in the
7197 pattern of V1DI, V4HI, and V2SF.
7199 FIXME: We should probably return # and add post reload
7200 splitters for these, but this way is so easy ;-). */
7201 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
7202 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
7203 operands[1] = CONST_VECTOR_ELT (vec, 0);
7204 operands[2] = CONST_VECTOR_ELT (vec, 1);
7205 if (cst == cst2)
7206 return "li %0,%1\n\tevmergelo %0,%0,%0";
7207 else if (WORDS_BIG_ENDIAN)
7208 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
7209 else
7210 return "li %0,%2\n\tevmergelo %0,%0,%0\n\tli %0,%1";
7213 /* Initialize TARGET of vector PAIRED to VALS. */
7215 void
7216 paired_expand_vector_init (rtx target, rtx vals)
7218 machine_mode mode = GET_MODE (target);
7219 int n_elts = GET_MODE_NUNITS (mode);
7220 int n_var = 0;
7221 rtx x, new_rtx, tmp, constant_op, op1, op2;
7222 int i;
7224 for (i = 0; i < n_elts; ++i)
7226 x = XVECEXP (vals, 0, i);
7227 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
7228 ++n_var;
7230 if (n_var == 0)
7232 /* Load from constant pool. */
7233 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
7234 return;
7237 if (n_var == 2)
7239 /* The vector is initialized only with non-constants. */
7240 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
7241 XVECEXP (vals, 0, 1));
7243 emit_move_insn (target, new_rtx);
7244 return;
7247 /* One field is non-constant and the other one is a constant. Load the
7248 constant from the constant pool and use ps_merge instruction to
7249 construct the whole vector. */
7250 op1 = XVECEXP (vals, 0, 0);
7251 op2 = XVECEXP (vals, 0, 1);
7253 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
7255 tmp = gen_reg_rtx (GET_MODE (constant_op));
7256 emit_move_insn (tmp, constant_op);
7258 if (CONSTANT_P (op1))
7259 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
7260 else
7261 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
7263 emit_move_insn (target, new_rtx);
7266 void
7267 paired_expand_vector_move (rtx operands[])
7269 rtx op0 = operands[0], op1 = operands[1];
7271 emit_move_insn (op0, op1);
7274 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
7275 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
7276 operands for the relation operation COND. This is a recursive
7277 function. */
7279 static void
7280 paired_emit_vector_compare (enum rtx_code rcode,
7281 rtx dest, rtx op0, rtx op1,
7282 rtx cc_op0, rtx cc_op1)
7284 rtx tmp = gen_reg_rtx (V2SFmode);
7285 rtx tmp1, max, min;
7287 gcc_assert (TARGET_PAIRED_FLOAT);
7288 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
7290 switch (rcode)
7292 case LT:
7293 case LTU:
7294 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
7295 return;
7296 case GE:
7297 case GEU:
7298 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
7299 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
7300 return;
7301 case LE:
7302 case LEU:
7303 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
7304 return;
7305 case GT:
7306 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
7307 return;
7308 case EQ:
7309 tmp1 = gen_reg_rtx (V2SFmode);
7310 max = gen_reg_rtx (V2SFmode);
7311 min = gen_reg_rtx (V2SFmode);
7312 gen_reg_rtx (V2SFmode);
7314 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
7315 emit_insn (gen_selv2sf4
7316 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
7317 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
7318 emit_insn (gen_selv2sf4
7319 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
7320 emit_insn (gen_subv2sf3 (tmp1, min, max));
7321 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
7322 return;
7323 case NE:
7324 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
7325 return;
7326 case UNLE:
7327 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
7328 return;
7329 case UNLT:
7330 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
7331 return;
7332 case UNGE:
7333 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
7334 return;
7335 case UNGT:
7336 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
7337 return;
7338 default:
7339 gcc_unreachable ();
7342 return;
7345 /* Emit vector conditional expression.
7346 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
7347 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
7350 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
7351 rtx cond, rtx cc_op0, rtx cc_op1)
7353 enum rtx_code rcode = GET_CODE (cond);
7355 if (!TARGET_PAIRED_FLOAT)
7356 return 0;
7358 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
7360 return 1;
7363 /* Initialize vector TARGET to VALS. */
7365 void
7366 rs6000_expand_vector_init (rtx target, rtx vals)
7368 machine_mode mode = GET_MODE (target);
7369 machine_mode inner_mode = GET_MODE_INNER (mode);
7370 int n_elts = GET_MODE_NUNITS (mode);
7371 int n_var = 0, one_var = -1;
7372 bool all_same = true, all_const_zero = true;
7373 rtx x, mem;
7374 int i;
7376 for (i = 0; i < n_elts; ++i)
7378 x = XVECEXP (vals, 0, i);
7379 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
7380 ++n_var, one_var = i;
7381 else if (x != CONST0_RTX (inner_mode))
7382 all_const_zero = false;
7384 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
7385 all_same = false;
7388 if (n_var == 0)
7390 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
7391 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
7392 if ((int_vector_p || TARGET_VSX) && all_const_zero)
7394 /* Zero register. */
7395 emit_move_insn (target, CONST0_RTX (mode));
7396 return;
7398 else if (int_vector_p && easy_vector_constant (const_vec, mode))
7400 /* Splat immediate. */
7401 emit_insn (gen_rtx_SET (target, const_vec));
7402 return;
7404 else
7406 /* Load from constant pool. */
7407 emit_move_insn (target, const_vec);
7408 return;
7412 /* Double word values on VSX can use xxpermdi or lxvdsx. */
7413 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
7415 rtx op[2];
7416 size_t i;
7417 size_t num_elements = all_same ? 1 : 2;
7418 for (i = 0; i < num_elements; i++)
7420 op[i] = XVECEXP (vals, 0, i);
7421 /* Just in case there is a SUBREG with a smaller mode, do a
7422 conversion. */
7423 if (GET_MODE (op[i]) != inner_mode)
7425 rtx tmp = gen_reg_rtx (inner_mode);
7426 convert_move (tmp, op[i], 0);
7427 op[i] = tmp;
7429 /* Allow load with splat double word. */
7430 else if (MEM_P (op[i]))
7432 if (!all_same)
7433 op[i] = force_reg (inner_mode, op[i]);
7435 else if (!REG_P (op[i]))
7436 op[i] = force_reg (inner_mode, op[i]);
7439 if (all_same)
7441 if (mode == V2DFmode)
7442 emit_insn (gen_vsx_splat_v2df (target, op[0]));
7443 else
7444 emit_insn (gen_vsx_splat_v2di (target, op[0]));
7446 else
7448 if (mode == V2DFmode)
7449 emit_insn (gen_vsx_concat_v2df (target, op[0], op[1]));
7450 else
7451 emit_insn (gen_vsx_concat_v2di (target, op[0], op[1]));
7453 return;
7456 /* Special case initializing vector int if we are on 64-bit systems with
7457 direct move or we have the ISA 3.0 instructions. */
7458 if (mode == V4SImode && VECTOR_MEM_VSX_P (V4SImode)
7459 && TARGET_DIRECT_MOVE_64BIT)
7461 if (all_same)
7463 rtx element0 = XVECEXP (vals, 0, 0);
7464 if (MEM_P (element0))
7465 element0 = rs6000_address_for_fpconvert (element0);
7466 else
7467 element0 = force_reg (SImode, element0);
7469 if (TARGET_P9_VECTOR)
7470 emit_insn (gen_vsx_splat_v4si (target, element0));
7471 else
7473 rtx tmp = gen_reg_rtx (DImode);
7474 emit_insn (gen_zero_extendsidi2 (tmp, element0));
7475 emit_insn (gen_vsx_splat_v4si_di (target, tmp));
7477 return;
7479 else
7481 rtx elements[4];
7482 size_t i;
7484 for (i = 0; i < 4; i++)
7486 elements[i] = XVECEXP (vals, 0, i);
7487 if (!CONST_INT_P (elements[i]) && !REG_P (elements[i]))
7488 elements[i] = copy_to_mode_reg (SImode, elements[i]);
7491 emit_insn (gen_vsx_init_v4si (target, elements[0], elements[1],
7492 elements[2], elements[3]));
7493 return;
7497 /* With single precision floating point on VSX, know that internally single
7498 precision is actually represented as a double, and either make 2 V2DF
7499 vectors, and convert these vectors to single precision, or do one
7500 conversion, and splat the result to the other elements. */
7501 if (mode == V4SFmode && VECTOR_MEM_VSX_P (V4SFmode))
7503 if (all_same)
7505 rtx element0 = XVECEXP (vals, 0, 0);
7507 if (TARGET_P9_VECTOR)
7509 if (MEM_P (element0))
7510 element0 = rs6000_address_for_fpconvert (element0);
7512 emit_insn (gen_vsx_splat_v4sf (target, element0));
7515 else
7517 rtx freg = gen_reg_rtx (V4SFmode);
7518 rtx sreg = force_reg (SFmode, element0);
7519 rtx cvt = (TARGET_XSCVDPSPN
7520 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
7521 : gen_vsx_xscvdpsp_scalar (freg, sreg));
7523 emit_insn (cvt);
7524 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg,
7525 const0_rtx));
7528 else
7530 rtx dbl_even = gen_reg_rtx (V2DFmode);
7531 rtx dbl_odd = gen_reg_rtx (V2DFmode);
7532 rtx flt_even = gen_reg_rtx (V4SFmode);
7533 rtx flt_odd = gen_reg_rtx (V4SFmode);
7534 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
7535 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
7536 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
7537 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
7539 /* Use VMRGEW if we can instead of doing a permute. */
7540 if (TARGET_P8_VECTOR)
7542 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op2));
7543 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op1, op3));
7544 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
7545 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
7546 if (BYTES_BIG_ENDIAN)
7547 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_even, flt_odd));
7548 else
7549 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_odd, flt_even));
7551 else
7553 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
7554 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
7555 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
7556 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
7557 rs6000_expand_extract_even (target, flt_even, flt_odd);
7560 return;
7563 /* Special case initializing vector short/char that are splats if we are on
7564 64-bit systems with direct move. */
7565 if (all_same && TARGET_DIRECT_MOVE_64BIT
7566 && (mode == V16QImode || mode == V8HImode))
7568 rtx op0 = XVECEXP (vals, 0, 0);
7569 rtx di_tmp = gen_reg_rtx (DImode);
7571 if (!REG_P (op0))
7572 op0 = force_reg (GET_MODE_INNER (mode), op0);
7574 if (mode == V16QImode)
7576 emit_insn (gen_zero_extendqidi2 (di_tmp, op0));
7577 emit_insn (gen_vsx_vspltb_di (target, di_tmp));
7578 return;
7581 if (mode == V8HImode)
7583 emit_insn (gen_zero_extendhidi2 (di_tmp, op0));
7584 emit_insn (gen_vsx_vsplth_di (target, di_tmp));
7585 return;
7589 /* Store value to stack temp. Load vector element. Splat. However, splat
7590 of 64-bit items is not supported on Altivec. */
7591 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
7593 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
7594 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
7595 XVECEXP (vals, 0, 0));
7596 x = gen_rtx_UNSPEC (VOIDmode,
7597 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
7598 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7599 gen_rtvec (2,
7600 gen_rtx_SET (target, mem),
7601 x)));
7602 x = gen_rtx_VEC_SELECT (inner_mode, target,
7603 gen_rtx_PARALLEL (VOIDmode,
7604 gen_rtvec (1, const0_rtx)));
7605 emit_insn (gen_rtx_SET (target, gen_rtx_VEC_DUPLICATE (mode, x)));
7606 return;
7609 /* One field is non-constant. Load constant then overwrite
7610 varying field. */
7611 if (n_var == 1)
7613 rtx copy = copy_rtx (vals);
7615 /* Load constant part of vector, substitute neighboring value for
7616 varying element. */
7617 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
7618 rs6000_expand_vector_init (target, copy);
7620 /* Insert variable. */
7621 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
7622 return;
7625 /* Construct the vector in memory one field at a time
7626 and load the whole vector. */
7627 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
7628 for (i = 0; i < n_elts; i++)
7629 emit_move_insn (adjust_address_nv (mem, inner_mode,
7630 i * GET_MODE_SIZE (inner_mode)),
7631 XVECEXP (vals, 0, i));
7632 emit_move_insn (target, mem);
7635 /* Set field ELT of TARGET to VAL. */
7637 void
7638 rs6000_expand_vector_set (rtx target, rtx val, int elt)
7640 machine_mode mode = GET_MODE (target);
7641 machine_mode inner_mode = GET_MODE_INNER (mode);
7642 rtx reg = gen_reg_rtx (mode);
7643 rtx mask, mem, x;
7644 int width = GET_MODE_SIZE (inner_mode);
7645 int i;
7647 val = force_reg (GET_MODE (val), val);
7649 if (VECTOR_MEM_VSX_P (mode))
7651 rtx insn = NULL_RTX;
7652 rtx elt_rtx = GEN_INT (elt);
7654 if (mode == V2DFmode)
7655 insn = gen_vsx_set_v2df (target, target, val, elt_rtx);
7657 else if (mode == V2DImode)
7658 insn = gen_vsx_set_v2di (target, target, val, elt_rtx);
7660 else if (TARGET_P9_VECTOR && TARGET_VSX_SMALL_INTEGER
7661 && TARGET_UPPER_REGS_DI && TARGET_POWERPC64)
7663 if (mode == V4SImode)
7664 insn = gen_vsx_set_v4si_p9 (target, target, val, elt_rtx);
7665 else if (mode == V8HImode)
7666 insn = gen_vsx_set_v8hi_p9 (target, target, val, elt_rtx);
7667 else if (mode == V16QImode)
7668 insn = gen_vsx_set_v16qi_p9 (target, target, val, elt_rtx);
7671 if (insn)
7673 emit_insn (insn);
7674 return;
7678 /* Simplify setting single element vectors like V1TImode. */
7679 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
7681 emit_move_insn (target, gen_lowpart (mode, val));
7682 return;
7685 /* Load single variable value. */
7686 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
7687 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
7688 x = gen_rtx_UNSPEC (VOIDmode,
7689 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
7690 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7691 gen_rtvec (2,
7692 gen_rtx_SET (reg, mem),
7693 x)));
7695 /* Linear sequence. */
7696 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
7697 for (i = 0; i < 16; ++i)
7698 XVECEXP (mask, 0, i) = GEN_INT (i);
7700 /* Set permute mask to insert element into target. */
7701 for (i = 0; i < width; ++i)
7702 XVECEXP (mask, 0, elt*width + i)
7703 = GEN_INT (i + 0x10);
7704 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
7706 if (BYTES_BIG_ENDIAN)
7707 x = gen_rtx_UNSPEC (mode,
7708 gen_rtvec (3, target, reg,
7709 force_reg (V16QImode, x)),
7710 UNSPEC_VPERM);
7711 else
7713 if (TARGET_P9_VECTOR)
7714 x = gen_rtx_UNSPEC (mode,
7715 gen_rtvec (3, target, reg,
7716 force_reg (V16QImode, x)),
7717 UNSPEC_VPERMR);
7718 else
7720 /* Invert selector. We prefer to generate VNAND on P8 so
7721 that future fusion opportunities can kick in, but must
7722 generate VNOR elsewhere. */
7723 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
7724 rtx iorx = (TARGET_P8_VECTOR
7725 ? gen_rtx_IOR (V16QImode, notx, notx)
7726 : gen_rtx_AND (V16QImode, notx, notx));
7727 rtx tmp = gen_reg_rtx (V16QImode);
7728 emit_insn (gen_rtx_SET (tmp, iorx));
7730 /* Permute with operands reversed and adjusted selector. */
7731 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
7732 UNSPEC_VPERM);
7736 emit_insn (gen_rtx_SET (target, x));
7739 /* Extract field ELT from VEC into TARGET. */
7741 void
7742 rs6000_expand_vector_extract (rtx target, rtx vec, rtx elt)
7744 machine_mode mode = GET_MODE (vec);
7745 machine_mode inner_mode = GET_MODE_INNER (mode);
7746 rtx mem;
7748 if (VECTOR_MEM_VSX_P (mode) && CONST_INT_P (elt))
7750 switch (mode)
7752 default:
7753 break;
7754 case E_V1TImode:
7755 gcc_assert (INTVAL (elt) == 0 && inner_mode == TImode);
7756 emit_move_insn (target, gen_lowpart (TImode, vec));
7757 break;
7758 case E_V2DFmode:
7759 emit_insn (gen_vsx_extract_v2df (target, vec, elt));
7760 return;
7761 case E_V2DImode:
7762 emit_insn (gen_vsx_extract_v2di (target, vec, elt));
7763 return;
7764 case E_V4SFmode:
7765 emit_insn (gen_vsx_extract_v4sf (target, vec, elt));
7766 return;
7767 case E_V16QImode:
7768 if (TARGET_DIRECT_MOVE_64BIT)
7770 emit_insn (gen_vsx_extract_v16qi (target, vec, elt));
7771 return;
7773 else
7774 break;
7775 case E_V8HImode:
7776 if (TARGET_DIRECT_MOVE_64BIT)
7778 emit_insn (gen_vsx_extract_v8hi (target, vec, elt));
7779 return;
7781 else
7782 break;
7783 case E_V4SImode:
7784 if (TARGET_DIRECT_MOVE_64BIT)
7786 emit_insn (gen_vsx_extract_v4si (target, vec, elt));
7787 return;
7789 break;
7792 else if (VECTOR_MEM_VSX_P (mode) && !CONST_INT_P (elt)
7793 && TARGET_DIRECT_MOVE_64BIT)
7795 if (GET_MODE (elt) != DImode)
7797 rtx tmp = gen_reg_rtx (DImode);
7798 convert_move (tmp, elt, 0);
7799 elt = tmp;
7801 else if (!REG_P (elt))
7802 elt = force_reg (DImode, elt);
7804 switch (mode)
7806 case E_V2DFmode:
7807 emit_insn (gen_vsx_extract_v2df_var (target, vec, elt));
7808 return;
7810 case E_V2DImode:
7811 emit_insn (gen_vsx_extract_v2di_var (target, vec, elt));
7812 return;
7814 case E_V4SFmode:
7815 emit_insn (gen_vsx_extract_v4sf_var (target, vec, elt));
7816 return;
7818 case E_V4SImode:
7819 emit_insn (gen_vsx_extract_v4si_var (target, vec, elt));
7820 return;
7822 case E_V8HImode:
7823 emit_insn (gen_vsx_extract_v8hi_var (target, vec, elt));
7824 return;
7826 case E_V16QImode:
7827 emit_insn (gen_vsx_extract_v16qi_var (target, vec, elt));
7828 return;
7830 default:
7831 gcc_unreachable ();
7835 gcc_assert (CONST_INT_P (elt));
7837 /* Allocate mode-sized buffer. */
7838 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
7840 emit_move_insn (mem, vec);
7842 /* Add offset to field within buffer matching vector element. */
7843 mem = adjust_address_nv (mem, inner_mode,
7844 INTVAL (elt) * GET_MODE_SIZE (inner_mode));
7846 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
7849 /* Helper function to return the register number of a RTX. */
7850 static inline int
7851 regno_or_subregno (rtx op)
7853 if (REG_P (op))
7854 return REGNO (op);
7855 else if (SUBREG_P (op))
7856 return subreg_regno (op);
7857 else
7858 gcc_unreachable ();
7861 /* Adjust a memory address (MEM) of a vector type to point to a scalar field
7862 within the vector (ELEMENT) with a mode (SCALAR_MODE). Use a base register
7863 temporary (BASE_TMP) to fixup the address. Return the new memory address
7864 that is valid for reads or writes to a given register (SCALAR_REG). */
7867 rs6000_adjust_vec_address (rtx scalar_reg,
7868 rtx mem,
7869 rtx element,
7870 rtx base_tmp,
7871 machine_mode scalar_mode)
7873 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
7874 rtx addr = XEXP (mem, 0);
7875 rtx element_offset;
7876 rtx new_addr;
7877 bool valid_addr_p;
7879 /* Vector addresses should not have PRE_INC, PRE_DEC, or PRE_MODIFY. */
7880 gcc_assert (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC);
7882 /* Calculate what we need to add to the address to get the element
7883 address. */
7884 if (CONST_INT_P (element))
7885 element_offset = GEN_INT (INTVAL (element) * scalar_size);
7886 else
7888 int byte_shift = exact_log2 (scalar_size);
7889 gcc_assert (byte_shift >= 0);
7891 if (byte_shift == 0)
7892 element_offset = element;
7894 else
7896 if (TARGET_POWERPC64)
7897 emit_insn (gen_ashldi3 (base_tmp, element, GEN_INT (byte_shift)));
7898 else
7899 emit_insn (gen_ashlsi3 (base_tmp, element, GEN_INT (byte_shift)));
7901 element_offset = base_tmp;
7905 /* Create the new address pointing to the element within the vector. If we
7906 are adding 0, we don't have to change the address. */
7907 if (element_offset == const0_rtx)
7908 new_addr = addr;
7910 /* A simple indirect address can be converted into a reg + offset
7911 address. */
7912 else if (REG_P (addr) || SUBREG_P (addr))
7913 new_addr = gen_rtx_PLUS (Pmode, addr, element_offset);
7915 /* Optimize D-FORM addresses with constant offset with a constant element, to
7916 include the element offset in the address directly. */
7917 else if (GET_CODE (addr) == PLUS)
7919 rtx op0 = XEXP (addr, 0);
7920 rtx op1 = XEXP (addr, 1);
7921 rtx insn;
7923 gcc_assert (REG_P (op0) || SUBREG_P (op0));
7924 if (CONST_INT_P (op1) && CONST_INT_P (element_offset))
7926 HOST_WIDE_INT offset = INTVAL (op1) + INTVAL (element_offset);
7927 rtx offset_rtx = GEN_INT (offset);
7929 if (IN_RANGE (offset, -32768, 32767)
7930 && (scalar_size < 8 || (offset & 0x3) == 0))
7931 new_addr = gen_rtx_PLUS (Pmode, op0, offset_rtx);
7932 else
7934 emit_move_insn (base_tmp, offset_rtx);
7935 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7938 else
7940 bool op1_reg_p = (REG_P (op1) || SUBREG_P (op1));
7941 bool ele_reg_p = (REG_P (element_offset) || SUBREG_P (element_offset));
7943 /* Note, ADDI requires the register being added to be a base
7944 register. If the register was R0, load it up into the temporary
7945 and do the add. */
7946 if (op1_reg_p
7947 && (ele_reg_p || reg_or_subregno (op1) != FIRST_GPR_REGNO))
7949 insn = gen_add3_insn (base_tmp, op1, element_offset);
7950 gcc_assert (insn != NULL_RTX);
7951 emit_insn (insn);
7954 else if (ele_reg_p
7955 && reg_or_subregno (element_offset) != FIRST_GPR_REGNO)
7957 insn = gen_add3_insn (base_tmp, element_offset, op1);
7958 gcc_assert (insn != NULL_RTX);
7959 emit_insn (insn);
7962 else
7964 emit_move_insn (base_tmp, op1);
7965 emit_insn (gen_add2_insn (base_tmp, element_offset));
7968 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7972 else
7974 emit_move_insn (base_tmp, addr);
7975 new_addr = gen_rtx_PLUS (Pmode, base_tmp, element_offset);
7978 /* If we have a PLUS, we need to see whether the particular register class
7979 allows for D-FORM or X-FORM addressing. */
7980 if (GET_CODE (new_addr) == PLUS)
7982 rtx op1 = XEXP (new_addr, 1);
7983 addr_mask_type addr_mask;
7984 int scalar_regno = regno_or_subregno (scalar_reg);
7986 gcc_assert (scalar_regno < FIRST_PSEUDO_REGISTER);
7987 if (INT_REGNO_P (scalar_regno))
7988 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_GPR];
7990 else if (FP_REGNO_P (scalar_regno))
7991 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_FPR];
7993 else if (ALTIVEC_REGNO_P (scalar_regno))
7994 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_VMX];
7996 else
7997 gcc_unreachable ();
7999 if (REG_P (op1) || SUBREG_P (op1))
8000 valid_addr_p = (addr_mask & RELOAD_REG_INDEXED) != 0;
8001 else
8002 valid_addr_p = (addr_mask & RELOAD_REG_OFFSET) != 0;
8005 else if (REG_P (new_addr) || SUBREG_P (new_addr))
8006 valid_addr_p = true;
8008 else
8009 valid_addr_p = false;
8011 if (!valid_addr_p)
8013 emit_move_insn (base_tmp, new_addr);
8014 new_addr = base_tmp;
8017 return change_address (mem, scalar_mode, new_addr);
8020 /* Split a variable vec_extract operation into the component instructions. */
8022 void
8023 rs6000_split_vec_extract_var (rtx dest, rtx src, rtx element, rtx tmp_gpr,
8024 rtx tmp_altivec)
8026 machine_mode mode = GET_MODE (src);
8027 machine_mode scalar_mode = GET_MODE (dest);
8028 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
8029 int byte_shift = exact_log2 (scalar_size);
8031 gcc_assert (byte_shift >= 0);
8033 /* If we are given a memory address, optimize to load just the element. We
8034 don't have to adjust the vector element number on little endian
8035 systems. */
8036 if (MEM_P (src))
8038 gcc_assert (REG_P (tmp_gpr));
8039 emit_move_insn (dest, rs6000_adjust_vec_address (dest, src, element,
8040 tmp_gpr, scalar_mode));
8041 return;
8044 else if (REG_P (src) || SUBREG_P (src))
8046 int bit_shift = byte_shift + 3;
8047 rtx element2;
8048 int dest_regno = regno_or_subregno (dest);
8049 int src_regno = regno_or_subregno (src);
8050 int element_regno = regno_or_subregno (element);
8052 gcc_assert (REG_P (tmp_gpr));
8054 /* See if we want to generate VEXTU{B,H,W}{L,R}X if the destination is in
8055 a general purpose register. */
8056 if (TARGET_P9_VECTOR
8057 && (mode == V16QImode || mode == V8HImode || mode == V4SImode)
8058 && INT_REGNO_P (dest_regno)
8059 && ALTIVEC_REGNO_P (src_regno)
8060 && INT_REGNO_P (element_regno))
8062 rtx dest_si = gen_rtx_REG (SImode, dest_regno);
8063 rtx element_si = gen_rtx_REG (SImode, element_regno);
8065 if (mode == V16QImode)
8066 emit_insn (VECTOR_ELT_ORDER_BIG
8067 ? gen_vextublx (dest_si, element_si, src)
8068 : gen_vextubrx (dest_si, element_si, src));
8070 else if (mode == V8HImode)
8072 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
8073 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const1_rtx));
8074 emit_insn (VECTOR_ELT_ORDER_BIG
8075 ? gen_vextuhlx (dest_si, tmp_gpr_si, src)
8076 : gen_vextuhrx (dest_si, tmp_gpr_si, src));
8080 else
8082 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
8083 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const2_rtx));
8084 emit_insn (VECTOR_ELT_ORDER_BIG
8085 ? gen_vextuwlx (dest_si, tmp_gpr_si, src)
8086 : gen_vextuwrx (dest_si, tmp_gpr_si, src));
8089 return;
8093 gcc_assert (REG_P (tmp_altivec));
8095 /* For little endian, adjust element ordering. For V2DI/V2DF, we can use
8096 an XOR, otherwise we need to subtract. The shift amount is so VSLO
8097 will shift the element into the upper position (adding 3 to convert a
8098 byte shift into a bit shift). */
8099 if (scalar_size == 8)
8101 if (!VECTOR_ELT_ORDER_BIG)
8103 emit_insn (gen_xordi3 (tmp_gpr, element, const1_rtx));
8104 element2 = tmp_gpr;
8106 else
8107 element2 = element;
8109 /* Generate RLDIC directly to shift left 6 bits and retrieve 1
8110 bit. */
8111 emit_insn (gen_rtx_SET (tmp_gpr,
8112 gen_rtx_AND (DImode,
8113 gen_rtx_ASHIFT (DImode,
8114 element2,
8115 GEN_INT (6)),
8116 GEN_INT (64))));
8118 else
8120 if (!VECTOR_ELT_ORDER_BIG)
8122 rtx num_ele_m1 = GEN_INT (GET_MODE_NUNITS (mode) - 1);
8124 emit_insn (gen_anddi3 (tmp_gpr, element, num_ele_m1));
8125 emit_insn (gen_subdi3 (tmp_gpr, num_ele_m1, tmp_gpr));
8126 element2 = tmp_gpr;
8128 else
8129 element2 = element;
8131 emit_insn (gen_ashldi3 (tmp_gpr, element2, GEN_INT (bit_shift)));
8134 /* Get the value into the lower byte of the Altivec register where VSLO
8135 expects it. */
8136 if (TARGET_P9_VECTOR)
8137 emit_insn (gen_vsx_splat_v2di (tmp_altivec, tmp_gpr));
8138 else if (can_create_pseudo_p ())
8139 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_gpr, tmp_gpr));
8140 else
8142 rtx tmp_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
8143 emit_move_insn (tmp_di, tmp_gpr);
8144 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_di, tmp_di));
8147 /* Do the VSLO to get the value into the final location. */
8148 switch (mode)
8150 case E_V2DFmode:
8151 emit_insn (gen_vsx_vslo_v2df (dest, src, tmp_altivec));
8152 return;
8154 case E_V2DImode:
8155 emit_insn (gen_vsx_vslo_v2di (dest, src, tmp_altivec));
8156 return;
8158 case E_V4SFmode:
8160 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
8161 rtx tmp_altivec_v4sf = gen_rtx_REG (V4SFmode, REGNO (tmp_altivec));
8162 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
8163 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
8164 tmp_altivec));
8166 emit_insn (gen_vsx_xscvspdp_scalar2 (dest, tmp_altivec_v4sf));
8167 return;
8170 case E_V4SImode:
8171 case E_V8HImode:
8172 case E_V16QImode:
8174 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
8175 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
8176 rtx tmp_gpr_di = gen_rtx_REG (DImode, REGNO (dest));
8177 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
8178 tmp_altivec));
8179 emit_move_insn (tmp_gpr_di, tmp_altivec_di);
8180 emit_insn (gen_ashrdi3 (tmp_gpr_di, tmp_gpr_di,
8181 GEN_INT (64 - (8 * scalar_size))));
8182 return;
8185 default:
8186 gcc_unreachable ();
8189 return;
8191 else
8192 gcc_unreachable ();
8195 /* Helper function for rs6000_split_v4si_init to build up a DImode value from
8196 two SImode values. */
8198 static void
8199 rs6000_split_v4si_init_di_reg (rtx dest, rtx si1, rtx si2, rtx tmp)
8201 const unsigned HOST_WIDE_INT mask_32bit = HOST_WIDE_INT_C (0xffffffff);
8203 if (CONST_INT_P (si1) && CONST_INT_P (si2))
8205 unsigned HOST_WIDE_INT const1 = (UINTVAL (si1) & mask_32bit) << 32;
8206 unsigned HOST_WIDE_INT const2 = UINTVAL (si2) & mask_32bit;
8208 emit_move_insn (dest, GEN_INT (const1 | const2));
8209 return;
8212 /* Put si1 into upper 32-bits of dest. */
8213 if (CONST_INT_P (si1))
8214 emit_move_insn (dest, GEN_INT ((UINTVAL (si1) & mask_32bit) << 32));
8215 else
8217 /* Generate RLDIC. */
8218 rtx si1_di = gen_rtx_REG (DImode, regno_or_subregno (si1));
8219 rtx shift_rtx = gen_rtx_ASHIFT (DImode, si1_di, GEN_INT (32));
8220 rtx mask_rtx = GEN_INT (mask_32bit << 32);
8221 rtx and_rtx = gen_rtx_AND (DImode, shift_rtx, mask_rtx);
8222 gcc_assert (!reg_overlap_mentioned_p (dest, si1));
8223 emit_insn (gen_rtx_SET (dest, and_rtx));
8226 /* Put si2 into the temporary. */
8227 gcc_assert (!reg_overlap_mentioned_p (dest, tmp));
8228 if (CONST_INT_P (si2))
8229 emit_move_insn (tmp, GEN_INT (UINTVAL (si2) & mask_32bit));
8230 else
8231 emit_insn (gen_zero_extendsidi2 (tmp, si2));
8233 /* Combine the two parts. */
8234 emit_insn (gen_iordi3 (dest, dest, tmp));
8235 return;
8238 /* Split a V4SI initialization. */
8240 void
8241 rs6000_split_v4si_init (rtx operands[])
8243 rtx dest = operands[0];
8245 /* Destination is a GPR, build up the two DImode parts in place. */
8246 if (REG_P (dest) || SUBREG_P (dest))
8248 int d_regno = regno_or_subregno (dest);
8249 rtx scalar1 = operands[1];
8250 rtx scalar2 = operands[2];
8251 rtx scalar3 = operands[3];
8252 rtx scalar4 = operands[4];
8253 rtx tmp1 = operands[5];
8254 rtx tmp2 = operands[6];
8256 /* Even though we only need one temporary (plus the destination, which
8257 has an early clobber constraint, try to use two temporaries, one for
8258 each double word created. That way the 2nd insn scheduling pass can
8259 rearrange things so the two parts are done in parallel. */
8260 if (BYTES_BIG_ENDIAN)
8262 rtx di_lo = gen_rtx_REG (DImode, d_regno);
8263 rtx di_hi = gen_rtx_REG (DImode, d_regno + 1);
8264 rs6000_split_v4si_init_di_reg (di_lo, scalar1, scalar2, tmp1);
8265 rs6000_split_v4si_init_di_reg (di_hi, scalar3, scalar4, tmp2);
8267 else
8269 rtx di_lo = gen_rtx_REG (DImode, d_regno + 1);
8270 rtx di_hi = gen_rtx_REG (DImode, d_regno);
8271 gcc_assert (!VECTOR_ELT_ORDER_BIG);
8272 rs6000_split_v4si_init_di_reg (di_lo, scalar4, scalar3, tmp1);
8273 rs6000_split_v4si_init_di_reg (di_hi, scalar2, scalar1, tmp2);
8275 return;
8278 else
8279 gcc_unreachable ();
8282 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
8284 bool
8285 invalid_e500_subreg (rtx op, machine_mode mode)
8287 if (TARGET_E500_DOUBLE)
8289 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
8290 subreg:TI and reg:TF. Decimal float modes are like integer
8291 modes (only low part of each register used) for this
8292 purpose. */
8293 if (GET_CODE (op) == SUBREG
8294 && (mode == SImode || mode == DImode || mode == TImode
8295 || mode == DDmode || mode == TDmode || mode == PTImode)
8296 && REG_P (SUBREG_REG (op))
8297 && (GET_MODE (SUBREG_REG (op)) == DFmode
8298 || GET_MODE (SUBREG_REG (op)) == TFmode
8299 || GET_MODE (SUBREG_REG (op)) == IFmode
8300 || GET_MODE (SUBREG_REG (op)) == KFmode))
8301 return true;
8303 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
8304 reg:TI. */
8305 if (GET_CODE (op) == SUBREG
8306 && (mode == DFmode || mode == TFmode || mode == IFmode
8307 || mode == KFmode)
8308 && REG_P (SUBREG_REG (op))
8309 && (GET_MODE (SUBREG_REG (op)) == DImode
8310 || GET_MODE (SUBREG_REG (op)) == TImode
8311 || GET_MODE (SUBREG_REG (op)) == PTImode
8312 || GET_MODE (SUBREG_REG (op)) == DDmode
8313 || GET_MODE (SUBREG_REG (op)) == TDmode))
8314 return true;
8317 if (TARGET_SPE
8318 && GET_CODE (op) == SUBREG
8319 && mode == SImode
8320 && REG_P (SUBREG_REG (op))
8321 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
8322 return true;
8324 return false;
8327 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
8328 selects whether the alignment is abi mandated, optional, or
8329 both abi and optional alignment. */
8331 unsigned int
8332 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
8334 if (how != align_opt)
8336 if (TREE_CODE (type) == VECTOR_TYPE)
8338 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
8339 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
8341 if (align < 64)
8342 align = 64;
8344 else if (align < 128)
8345 align = 128;
8347 else if (TARGET_E500_DOUBLE
8348 && TREE_CODE (type) == REAL_TYPE
8349 && TYPE_MODE (type) == DFmode)
8351 if (align < 64)
8352 align = 64;
8356 if (how != align_abi)
8358 if (TREE_CODE (type) == ARRAY_TYPE
8359 && TYPE_MODE (TREE_TYPE (type)) == QImode)
8361 if (align < BITS_PER_WORD)
8362 align = BITS_PER_WORD;
8366 return align;
8369 /* Previous GCC releases forced all vector types to have 16-byte alignment. */
8371 bool
8372 rs6000_special_adjust_field_align_p (tree type, unsigned int computed)
8374 if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
8376 if (computed != 128)
8378 static bool warned;
8379 if (!warned && warn_psabi)
8381 warned = true;
8382 inform (input_location,
8383 "the layout of aggregates containing vectors with"
8384 " %d-byte alignment has changed in GCC 5",
8385 computed / BITS_PER_UNIT);
8388 /* In current GCC there is no special case. */
8389 return false;
8392 return false;
8395 /* AIX increases natural record alignment to doubleword if the first
8396 field is an FP double while the FP fields remain word aligned. */
8398 unsigned int
8399 rs6000_special_round_type_align (tree type, unsigned int computed,
8400 unsigned int specified)
8402 unsigned int align = MAX (computed, specified);
8403 tree field = TYPE_FIELDS (type);
8405 /* Skip all non field decls */
8406 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
8407 field = DECL_CHAIN (field);
8409 if (field != NULL && field != type)
8411 type = TREE_TYPE (field);
8412 while (TREE_CODE (type) == ARRAY_TYPE)
8413 type = TREE_TYPE (type);
8415 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
8416 align = MAX (align, 64);
8419 return align;
8422 /* Darwin increases record alignment to the natural alignment of
8423 the first field. */
8425 unsigned int
8426 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
8427 unsigned int specified)
8429 unsigned int align = MAX (computed, specified);
8431 if (TYPE_PACKED (type))
8432 return align;
8434 /* Find the first field, looking down into aggregates. */
8435 do {
8436 tree field = TYPE_FIELDS (type);
8437 /* Skip all non field decls */
8438 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
8439 field = DECL_CHAIN (field);
8440 if (! field)
8441 break;
8442 /* A packed field does not contribute any extra alignment. */
8443 if (DECL_PACKED (field))
8444 return align;
8445 type = TREE_TYPE (field);
8446 while (TREE_CODE (type) == ARRAY_TYPE)
8447 type = TREE_TYPE (type);
8448 } while (AGGREGATE_TYPE_P (type));
8450 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
8451 align = MAX (align, TYPE_ALIGN (type));
8453 return align;
8456 /* Return 1 for an operand in small memory on V.4/eabi. */
8459 small_data_operand (rtx op ATTRIBUTE_UNUSED,
8460 machine_mode mode ATTRIBUTE_UNUSED)
8462 #if TARGET_ELF
8463 rtx sym_ref;
8465 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
8466 return 0;
8468 if (DEFAULT_ABI != ABI_V4)
8469 return 0;
8471 /* Vector and float memory instructions have a limited offset on the
8472 SPE, so using a vector or float variable directly as an operand is
8473 not useful. */
8474 if (TARGET_SPE
8475 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
8476 return 0;
8478 if (GET_CODE (op) == SYMBOL_REF)
8479 sym_ref = op;
8481 else if (GET_CODE (op) != CONST
8482 || GET_CODE (XEXP (op, 0)) != PLUS
8483 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
8484 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
8485 return 0;
8487 else
8489 rtx sum = XEXP (op, 0);
8490 HOST_WIDE_INT summand;
8492 /* We have to be careful here, because it is the referenced address
8493 that must be 32k from _SDA_BASE_, not just the symbol. */
8494 summand = INTVAL (XEXP (sum, 1));
8495 if (summand < 0 || summand > g_switch_value)
8496 return 0;
8498 sym_ref = XEXP (sum, 0);
8501 return SYMBOL_REF_SMALL_P (sym_ref);
8502 #else
8503 return 0;
8504 #endif
8507 /* Return true if either operand is a general purpose register. */
8509 bool
8510 gpr_or_gpr_p (rtx op0, rtx op1)
8512 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
8513 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
8516 /* Return true if this is a move direct operation between GPR registers and
8517 floating point/VSX registers. */
8519 bool
8520 direct_move_p (rtx op0, rtx op1)
8522 int regno0, regno1;
8524 if (!REG_P (op0) || !REG_P (op1))
8525 return false;
8527 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
8528 return false;
8530 regno0 = REGNO (op0);
8531 regno1 = REGNO (op1);
8532 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
8533 return false;
8535 if (INT_REGNO_P (regno0))
8536 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
8538 else if (INT_REGNO_P (regno1))
8540 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
8541 return true;
8543 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
8544 return true;
8547 return false;
8550 /* Return true if the OFFSET is valid for the quad address instructions that
8551 use d-form (register + offset) addressing. */
8553 static inline bool
8554 quad_address_offset_p (HOST_WIDE_INT offset)
8556 return (IN_RANGE (offset, -32768, 32767) && ((offset) & 0xf) == 0);
8559 /* Return true if the ADDR is an acceptable address for a quad memory
8560 operation of mode MODE (either LQ/STQ for general purpose registers, or
8561 LXV/STXV for vector registers under ISA 3.0. GPR_P is true if this address
8562 is intended for LQ/STQ. If it is false, the address is intended for the ISA
8563 3.0 LXV/STXV instruction. */
8565 bool
8566 quad_address_p (rtx addr, machine_mode mode, bool strict)
8568 rtx op0, op1;
8570 if (GET_MODE_SIZE (mode) != 16)
8571 return false;
8573 if (legitimate_indirect_address_p (addr, strict))
8574 return true;
8576 if (VECTOR_MODE_P (mode) && !mode_supports_vsx_dform_quad (mode))
8577 return false;
8579 if (GET_CODE (addr) != PLUS)
8580 return false;
8582 op0 = XEXP (addr, 0);
8583 if (!REG_P (op0) || !INT_REG_OK_FOR_BASE_P (op0, strict))
8584 return false;
8586 op1 = XEXP (addr, 1);
8587 if (!CONST_INT_P (op1))
8588 return false;
8590 return quad_address_offset_p (INTVAL (op1));
8593 /* Return true if this is a load or store quad operation. This function does
8594 not handle the atomic quad memory instructions. */
8596 bool
8597 quad_load_store_p (rtx op0, rtx op1)
8599 bool ret;
8601 if (!TARGET_QUAD_MEMORY)
8602 ret = false;
8604 else if (REG_P (op0) && MEM_P (op1))
8605 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
8606 && quad_memory_operand (op1, GET_MODE (op1))
8607 && !reg_overlap_mentioned_p (op0, op1));
8609 else if (MEM_P (op0) && REG_P (op1))
8610 ret = (quad_memory_operand (op0, GET_MODE (op0))
8611 && quad_int_reg_operand (op1, GET_MODE (op1)));
8613 else
8614 ret = false;
8616 if (TARGET_DEBUG_ADDR)
8618 fprintf (stderr, "\n========== quad_load_store, return %s\n",
8619 ret ? "true" : "false");
8620 debug_rtx (gen_rtx_SET (op0, op1));
8623 return ret;
8626 /* Given an address, return a constant offset term if one exists. */
8628 static rtx
8629 address_offset (rtx op)
8631 if (GET_CODE (op) == PRE_INC
8632 || GET_CODE (op) == PRE_DEC)
8633 op = XEXP (op, 0);
8634 else if (GET_CODE (op) == PRE_MODIFY
8635 || GET_CODE (op) == LO_SUM)
8636 op = XEXP (op, 1);
8638 if (GET_CODE (op) == CONST)
8639 op = XEXP (op, 0);
8641 if (GET_CODE (op) == PLUS)
8642 op = XEXP (op, 1);
8644 if (CONST_INT_P (op))
8645 return op;
8647 return NULL_RTX;
8650 /* Return true if the MEM operand is a memory operand suitable for use
8651 with a (full width, possibly multiple) gpr load/store. On
8652 powerpc64 this means the offset must be divisible by 4.
8653 Implements 'Y' constraint.
8655 Accept direct, indexed, offset, lo_sum and tocref. Since this is
8656 a constraint function we know the operand has satisfied a suitable
8657 memory predicate. Also accept some odd rtl generated by reload
8658 (see rs6000_legitimize_reload_address for various forms). It is
8659 important that reload rtl be accepted by appropriate constraints
8660 but not by the operand predicate.
8662 Offsetting a lo_sum should not be allowed, except where we know by
8663 alignment that a 32k boundary is not crossed, but see the ???
8664 comment in rs6000_legitimize_reload_address. Note that by
8665 "offsetting" here we mean a further offset to access parts of the
8666 MEM. It's fine to have a lo_sum where the inner address is offset
8667 from a sym, since the same sym+offset will appear in the high part
8668 of the address calculation. */
8670 bool
8671 mem_operand_gpr (rtx op, machine_mode mode)
8673 unsigned HOST_WIDE_INT offset;
8674 int extra;
8675 rtx addr = XEXP (op, 0);
8677 op = address_offset (addr);
8678 if (op == NULL_RTX)
8679 return true;
8681 offset = INTVAL (op);
8682 if (TARGET_POWERPC64 && (offset & 3) != 0)
8683 return false;
8685 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
8686 if (extra < 0)
8687 extra = 0;
8689 if (GET_CODE (addr) == LO_SUM)
8690 /* For lo_sum addresses, we must allow any offset except one that
8691 causes a wrap, so test only the low 16 bits. */
8692 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
8694 return offset + 0x8000 < 0x10000u - extra;
8697 /* As above, but for DS-FORM VSX insns. Unlike mem_operand_gpr,
8698 enforce an offset divisible by 4 even for 32-bit. */
8700 bool
8701 mem_operand_ds_form (rtx op, machine_mode mode)
8703 unsigned HOST_WIDE_INT offset;
8704 int extra;
8705 rtx addr = XEXP (op, 0);
8707 if (!offsettable_address_p (false, mode, addr))
8708 return false;
8710 op = address_offset (addr);
8711 if (op == NULL_RTX)
8712 return true;
8714 offset = INTVAL (op);
8715 if ((offset & 3) != 0)
8716 return false;
8718 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
8719 if (extra < 0)
8720 extra = 0;
8722 if (GET_CODE (addr) == LO_SUM)
8723 /* For lo_sum addresses, we must allow any offset except one that
8724 causes a wrap, so test only the low 16 bits. */
8725 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
8727 return offset + 0x8000 < 0x10000u - extra;
8730 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
8732 static bool
8733 reg_offset_addressing_ok_p (machine_mode mode)
8735 switch (mode)
8737 case E_V16QImode:
8738 case E_V8HImode:
8739 case E_V4SFmode:
8740 case E_V4SImode:
8741 case E_V2DFmode:
8742 case E_V2DImode:
8743 case E_V1TImode:
8744 case E_TImode:
8745 case E_TFmode:
8746 case E_KFmode:
8747 /* AltiVec/VSX vector modes. Only reg+reg addressing was valid until the
8748 ISA 3.0 vector d-form addressing mode was added. While TImode is not
8749 a vector mode, if we want to use the VSX registers to move it around,
8750 we need to restrict ourselves to reg+reg addressing. Similarly for
8751 IEEE 128-bit floating point that is passed in a single vector
8752 register. */
8753 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
8754 return mode_supports_vsx_dform_quad (mode);
8755 break;
8757 case E_V4HImode:
8758 case E_V2SImode:
8759 case E_V1DImode:
8760 case E_V2SFmode:
8761 /* Paired vector modes. Only reg+reg addressing is valid. */
8762 if (TARGET_PAIRED_FLOAT)
8763 return false;
8764 break;
8766 case E_SDmode:
8767 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
8768 addressing for the LFIWZX and STFIWX instructions. */
8769 if (TARGET_NO_SDMODE_STACK)
8770 return false;
8771 break;
8773 default:
8774 break;
8777 return true;
8780 static bool
8781 virtual_stack_registers_memory_p (rtx op)
8783 int regnum;
8785 if (GET_CODE (op) == REG)
8786 regnum = REGNO (op);
8788 else if (GET_CODE (op) == PLUS
8789 && GET_CODE (XEXP (op, 0)) == REG
8790 && GET_CODE (XEXP (op, 1)) == CONST_INT)
8791 regnum = REGNO (XEXP (op, 0));
8793 else
8794 return false;
8796 return (regnum >= FIRST_VIRTUAL_REGISTER
8797 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
8800 /* Return true if a MODE sized memory accesses to OP plus OFFSET
8801 is known to not straddle a 32k boundary. This function is used
8802 to determine whether -mcmodel=medium code can use TOC pointer
8803 relative addressing for OP. This means the alignment of the TOC
8804 pointer must also be taken into account, and unfortunately that is
8805 only 8 bytes. */
8807 #ifndef POWERPC64_TOC_POINTER_ALIGNMENT
8808 #define POWERPC64_TOC_POINTER_ALIGNMENT 8
8809 #endif
8811 static bool
8812 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
8813 machine_mode mode)
8815 tree decl;
8816 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
8818 if (GET_CODE (op) != SYMBOL_REF)
8819 return false;
8821 /* ISA 3.0 vector d-form addressing is restricted, don't allow
8822 SYMBOL_REF. */
8823 if (mode_supports_vsx_dform_quad (mode))
8824 return false;
8826 dsize = GET_MODE_SIZE (mode);
8827 decl = SYMBOL_REF_DECL (op);
8828 if (!decl)
8830 if (dsize == 0)
8831 return false;
8833 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
8834 replacing memory addresses with an anchor plus offset. We
8835 could find the decl by rummaging around in the block->objects
8836 VEC for the given offset but that seems like too much work. */
8837 dalign = BITS_PER_UNIT;
8838 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
8839 && SYMBOL_REF_ANCHOR_P (op)
8840 && SYMBOL_REF_BLOCK (op) != NULL)
8842 struct object_block *block = SYMBOL_REF_BLOCK (op);
8844 dalign = block->alignment;
8845 offset += SYMBOL_REF_BLOCK_OFFSET (op);
8847 else if (CONSTANT_POOL_ADDRESS_P (op))
8849 /* It would be nice to have get_pool_align().. */
8850 machine_mode cmode = get_pool_mode (op);
8852 dalign = GET_MODE_ALIGNMENT (cmode);
8855 else if (DECL_P (decl))
8857 dalign = DECL_ALIGN (decl);
8859 if (dsize == 0)
8861 /* Allow BLKmode when the entire object is known to not
8862 cross a 32k boundary. */
8863 if (!DECL_SIZE_UNIT (decl))
8864 return false;
8866 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
8867 return false;
8869 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8870 if (dsize > 32768)
8871 return false;
8873 dalign /= BITS_PER_UNIT;
8874 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
8875 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
8876 return dalign >= dsize;
8879 else
8880 gcc_unreachable ();
8882 /* Find how many bits of the alignment we know for this access. */
8883 dalign /= BITS_PER_UNIT;
8884 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
8885 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
8886 mask = dalign - 1;
8887 lsb = offset & -offset;
8888 mask &= lsb - 1;
8889 dalign = mask + 1;
8891 return dalign >= dsize;
8894 static bool
8895 constant_pool_expr_p (rtx op)
8897 rtx base, offset;
8899 split_const (op, &base, &offset);
8900 return (GET_CODE (base) == SYMBOL_REF
8901 && CONSTANT_POOL_ADDRESS_P (base)
8902 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
8905 static const_rtx tocrel_base, tocrel_offset;
8907 /* Return true if OP is a toc pointer relative address (the output
8908 of create_TOC_reference). If STRICT, do not match non-split
8909 -mcmodel=large/medium toc pointer relative addresses. */
8911 bool
8912 toc_relative_expr_p (const_rtx op, bool strict)
8914 if (!TARGET_TOC)
8915 return false;
8917 if (TARGET_CMODEL != CMODEL_SMALL)
8919 /* When strict ensure we have everything tidy. */
8920 if (strict
8921 && !(GET_CODE (op) == LO_SUM
8922 && REG_P (XEXP (op, 0))
8923 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict)))
8924 return false;
8926 /* When not strict, allow non-split TOC addresses and also allow
8927 (lo_sum (high ..)) TOC addresses created during reload. */
8928 if (GET_CODE (op) == LO_SUM)
8929 op = XEXP (op, 1);
8932 tocrel_base = op;
8933 tocrel_offset = const0_rtx;
8934 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
8936 tocrel_base = XEXP (op, 0);
8937 tocrel_offset = XEXP (op, 1);
8940 return (GET_CODE (tocrel_base) == UNSPEC
8941 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
8944 /* Return true if X is a constant pool address, and also for cmodel=medium
8945 if X is a toc-relative address known to be offsettable within MODE. */
8947 bool
8948 legitimate_constant_pool_address_p (const_rtx x, machine_mode mode,
8949 bool strict)
8951 return (toc_relative_expr_p (x, strict)
8952 && (TARGET_CMODEL != CMODEL_MEDIUM
8953 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
8954 || mode == QImode
8955 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
8956 INTVAL (tocrel_offset), mode)));
8959 static bool
8960 legitimate_small_data_p (machine_mode mode, rtx x)
8962 return (DEFAULT_ABI == ABI_V4
8963 && !flag_pic && !TARGET_TOC
8964 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
8965 && small_data_operand (x, mode));
8968 /* SPE offset addressing is limited to 5-bits worth of double words. */
8969 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
8971 bool
8972 rs6000_legitimate_offset_address_p (machine_mode mode, rtx x,
8973 bool strict, bool worst_case)
8975 unsigned HOST_WIDE_INT offset;
8976 unsigned int extra;
8978 if (GET_CODE (x) != PLUS)
8979 return false;
8980 if (!REG_P (XEXP (x, 0)))
8981 return false;
8982 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
8983 return false;
8984 if (mode_supports_vsx_dform_quad (mode))
8985 return quad_address_p (x, mode, strict);
8986 if (!reg_offset_addressing_ok_p (mode))
8987 return virtual_stack_registers_memory_p (x);
8988 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
8989 return true;
8990 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
8991 return false;
8993 offset = INTVAL (XEXP (x, 1));
8994 extra = 0;
8995 switch (mode)
8997 case E_V4HImode:
8998 case E_V2SImode:
8999 case E_V1DImode:
9000 case E_V2SFmode:
9001 /* SPE vector modes. */
9002 return SPE_CONST_OFFSET_OK (offset);
9004 case E_DFmode:
9005 case E_DDmode:
9006 case E_DImode:
9007 /* On e500v2, we may have:
9009 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
9011 Which gets addressed with evldd instructions. */
9012 if (TARGET_E500_DOUBLE)
9013 return SPE_CONST_OFFSET_OK (offset);
9015 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
9016 addressing. */
9017 if (VECTOR_MEM_VSX_P (mode))
9018 return false;
9020 if (!worst_case)
9021 break;
9022 if (!TARGET_POWERPC64)
9023 extra = 4;
9024 else if (offset & 3)
9025 return false;
9026 break;
9028 case E_TFmode:
9029 case E_IFmode:
9030 case E_KFmode:
9031 case E_TDmode:
9032 case E_TImode:
9033 case E_PTImode:
9034 if (TARGET_E500_DOUBLE)
9035 return (SPE_CONST_OFFSET_OK (offset)
9036 && SPE_CONST_OFFSET_OK (offset + 8));
9038 extra = 8;
9039 if (!worst_case)
9040 break;
9041 if (!TARGET_POWERPC64)
9042 extra = 12;
9043 else if (offset & 3)
9044 return false;
9045 break;
9047 default:
9048 break;
9051 offset += 0x8000;
9052 return offset < 0x10000 - extra;
9055 bool
9056 legitimate_indexed_address_p (rtx x, int strict)
9058 rtx op0, op1;
9060 if (GET_CODE (x) != PLUS)
9061 return false;
9063 op0 = XEXP (x, 0);
9064 op1 = XEXP (x, 1);
9066 /* Recognize the rtl generated by reload which we know will later be
9067 replaced with proper base and index regs. */
9068 if (!strict
9069 && reload_in_progress
9070 && (REG_P (op0) || GET_CODE (op0) == PLUS)
9071 && REG_P (op1))
9072 return true;
9074 return (REG_P (op0) && REG_P (op1)
9075 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
9076 && INT_REG_OK_FOR_INDEX_P (op1, strict))
9077 || (INT_REG_OK_FOR_BASE_P (op1, strict)
9078 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
9081 bool
9082 avoiding_indexed_address_p (machine_mode mode)
9084 /* Avoid indexed addressing for modes that have non-indexed
9085 load/store instruction forms. */
9086 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
9089 bool
9090 legitimate_indirect_address_p (rtx x, int strict)
9092 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
9095 bool
9096 macho_lo_sum_memory_operand (rtx x, machine_mode mode)
9098 if (!TARGET_MACHO || !flag_pic
9099 || mode != SImode || GET_CODE (x) != MEM)
9100 return false;
9101 x = XEXP (x, 0);
9103 if (GET_CODE (x) != LO_SUM)
9104 return false;
9105 if (GET_CODE (XEXP (x, 0)) != REG)
9106 return false;
9107 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
9108 return false;
9109 x = XEXP (x, 1);
9111 return CONSTANT_P (x);
9114 static bool
9115 legitimate_lo_sum_address_p (machine_mode mode, rtx x, int strict)
9117 if (GET_CODE (x) != LO_SUM)
9118 return false;
9119 if (GET_CODE (XEXP (x, 0)) != REG)
9120 return false;
9121 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
9122 return false;
9123 /* quad word addresses are restricted, and we can't use LO_SUM. */
9124 if (mode_supports_vsx_dform_quad (mode))
9125 return false;
9126 /* Restrict addressing for DI because of our SUBREG hackery. */
9127 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
9128 return false;
9129 x = XEXP (x, 1);
9131 if (TARGET_ELF || TARGET_MACHO)
9133 bool large_toc_ok;
9135 if (DEFAULT_ABI == ABI_V4 && flag_pic)
9136 return false;
9137 /* LRA doesn't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
9138 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
9139 recognizes some LO_SUM addresses as valid although this
9140 function says opposite. In most cases, LRA through different
9141 transformations can generate correct code for address reloads.
9142 It can not manage only some LO_SUM cases. So we need to add
9143 code analogous to one in rs6000_legitimize_reload_address for
9144 LOW_SUM here saying that some addresses are still valid. */
9145 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
9146 && small_toc_ref (x, VOIDmode));
9147 if (TARGET_TOC && ! large_toc_ok)
9148 return false;
9149 if (GET_MODE_NUNITS (mode) != 1)
9150 return false;
9151 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9152 && !(/* ??? Assume floating point reg based on mode? */
9153 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
9154 && (mode == DFmode || mode == DDmode)))
9155 return false;
9157 return CONSTANT_P (x) || large_toc_ok;
9160 return false;
9164 /* Try machine-dependent ways of modifying an illegitimate address
9165 to be legitimate. If we find one, return the new, valid address.
9166 This is used from only one place: `memory_address' in explow.c.
9168 OLDX is the address as it was before break_out_memory_refs was
9169 called. In some cases it is useful to look at this to decide what
9170 needs to be done.
9172 It is always safe for this function to do nothing. It exists to
9173 recognize opportunities to optimize the output.
9175 On RS/6000, first check for the sum of a register with a constant
9176 integer that is out of range. If so, generate code to add the
9177 constant with the low-order 16 bits masked to the register and force
9178 this result into another register (this can be done with `cau').
9179 Then generate an address of REG+(CONST&0xffff), allowing for the
9180 possibility of bit 16 being a one.
9182 Then check for the sum of a register and something not constant, try to
9183 load the other things into a register and return the sum. */
9185 static rtx
9186 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
9187 machine_mode mode)
9189 unsigned int extra;
9191 if (!reg_offset_addressing_ok_p (mode)
9192 || mode_supports_vsx_dform_quad (mode))
9194 if (virtual_stack_registers_memory_p (x))
9195 return x;
9197 /* In theory we should not be seeing addresses of the form reg+0,
9198 but just in case it is generated, optimize it away. */
9199 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
9200 return force_reg (Pmode, XEXP (x, 0));
9202 /* For TImode with load/store quad, restrict addresses to just a single
9203 pointer, so it works with both GPRs and VSX registers. */
9204 /* Make sure both operands are registers. */
9205 else if (GET_CODE (x) == PLUS
9206 && (mode != TImode || !TARGET_VSX_TIMODE))
9207 return gen_rtx_PLUS (Pmode,
9208 force_reg (Pmode, XEXP (x, 0)),
9209 force_reg (Pmode, XEXP (x, 1)));
9210 else
9211 return force_reg (Pmode, x);
9213 if (GET_CODE (x) == SYMBOL_REF)
9215 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
9216 if (model != 0)
9217 return rs6000_legitimize_tls_address (x, model);
9220 extra = 0;
9221 switch (mode)
9223 case E_TFmode:
9224 case E_TDmode:
9225 case E_TImode:
9226 case E_PTImode:
9227 case E_IFmode:
9228 case E_KFmode:
9229 /* As in legitimate_offset_address_p we do not assume
9230 worst-case. The mode here is just a hint as to the registers
9231 used. A TImode is usually in gprs, but may actually be in
9232 fprs. Leave worst-case scenario for reload to handle via
9233 insn constraints. PTImode is only GPRs. */
9234 extra = 8;
9235 break;
9236 default:
9237 break;
9240 if (GET_CODE (x) == PLUS
9241 && GET_CODE (XEXP (x, 0)) == REG
9242 && GET_CODE (XEXP (x, 1)) == CONST_INT
9243 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
9244 >= 0x10000 - extra)
9245 && !(SPE_VECTOR_MODE (mode)
9246 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
9248 HOST_WIDE_INT high_int, low_int;
9249 rtx sum;
9250 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
9251 if (low_int >= 0x8000 - extra)
9252 low_int = 0;
9253 high_int = INTVAL (XEXP (x, 1)) - low_int;
9254 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
9255 GEN_INT (high_int)), 0);
9256 return plus_constant (Pmode, sum, low_int);
9258 else if (GET_CODE (x) == PLUS
9259 && GET_CODE (XEXP (x, 0)) == REG
9260 && GET_CODE (XEXP (x, 1)) != CONST_INT
9261 && GET_MODE_NUNITS (mode) == 1
9262 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
9263 || (/* ??? Assume floating point reg based on mode? */
9264 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
9265 && (mode == DFmode || mode == DDmode)))
9266 && !avoiding_indexed_address_p (mode))
9268 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
9269 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
9271 else if (SPE_VECTOR_MODE (mode)
9272 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
9274 if (mode == DImode)
9275 return x;
9276 /* We accept [reg + reg] and [reg + OFFSET]. */
9278 if (GET_CODE (x) == PLUS)
9280 rtx op1 = XEXP (x, 0);
9281 rtx op2 = XEXP (x, 1);
9282 rtx y;
9284 op1 = force_reg (Pmode, op1);
9286 if (GET_CODE (op2) != REG
9287 && (GET_CODE (op2) != CONST_INT
9288 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
9289 || (GET_MODE_SIZE (mode) > 8
9290 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
9291 op2 = force_reg (Pmode, op2);
9293 /* We can't always do [reg + reg] for these, because [reg +
9294 reg + offset] is not a legitimate addressing mode. */
9295 y = gen_rtx_PLUS (Pmode, op1, op2);
9297 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
9298 return force_reg (Pmode, y);
9299 else
9300 return y;
9303 return force_reg (Pmode, x);
9305 else if ((TARGET_ELF
9306 #if TARGET_MACHO
9307 || !MACHO_DYNAMIC_NO_PIC_P
9308 #endif
9310 && TARGET_32BIT
9311 && TARGET_NO_TOC
9312 && ! flag_pic
9313 && GET_CODE (x) != CONST_INT
9314 && GET_CODE (x) != CONST_WIDE_INT
9315 && GET_CODE (x) != CONST_DOUBLE
9316 && CONSTANT_P (x)
9317 && GET_MODE_NUNITS (mode) == 1
9318 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
9319 || (/* ??? Assume floating point reg based on mode? */
9320 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
9321 && (mode == DFmode || mode == DDmode))))
9323 rtx reg = gen_reg_rtx (Pmode);
9324 if (TARGET_ELF)
9325 emit_insn (gen_elf_high (reg, x));
9326 else
9327 emit_insn (gen_macho_high (reg, x));
9328 return gen_rtx_LO_SUM (Pmode, reg, x);
9330 else if (TARGET_TOC
9331 && GET_CODE (x) == SYMBOL_REF
9332 && constant_pool_expr_p (x)
9333 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
9334 return create_TOC_reference (x, NULL_RTX);
9335 else
9336 return x;
9339 /* Debug version of rs6000_legitimize_address. */
9340 static rtx
9341 rs6000_debug_legitimize_address (rtx x, rtx oldx, machine_mode mode)
9343 rtx ret;
9344 rtx_insn *insns;
9346 start_sequence ();
9347 ret = rs6000_legitimize_address (x, oldx, mode);
9348 insns = get_insns ();
9349 end_sequence ();
9351 if (ret != x)
9353 fprintf (stderr,
9354 "\nrs6000_legitimize_address: mode %s, old code %s, "
9355 "new code %s, modified\n",
9356 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
9357 GET_RTX_NAME (GET_CODE (ret)));
9359 fprintf (stderr, "Original address:\n");
9360 debug_rtx (x);
9362 fprintf (stderr, "oldx:\n");
9363 debug_rtx (oldx);
9365 fprintf (stderr, "New address:\n");
9366 debug_rtx (ret);
9368 if (insns)
9370 fprintf (stderr, "Insns added:\n");
9371 debug_rtx_list (insns, 20);
9374 else
9376 fprintf (stderr,
9377 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
9378 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
9380 debug_rtx (x);
9383 if (insns)
9384 emit_insn (insns);
9386 return ret;
9389 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
9390 We need to emit DTP-relative relocations. */
9392 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
9393 static void
9394 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
9396 switch (size)
9398 case 4:
9399 fputs ("\t.long\t", file);
9400 break;
9401 case 8:
9402 fputs (DOUBLE_INT_ASM_OP, file);
9403 break;
9404 default:
9405 gcc_unreachable ();
9407 output_addr_const (file, x);
9408 if (TARGET_ELF)
9409 fputs ("@dtprel+0x8000", file);
9410 else if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF)
9412 switch (SYMBOL_REF_TLS_MODEL (x))
9414 case 0:
9415 break;
9416 case TLS_MODEL_LOCAL_EXEC:
9417 fputs ("@le", file);
9418 break;
9419 case TLS_MODEL_INITIAL_EXEC:
9420 fputs ("@ie", file);
9421 break;
9422 case TLS_MODEL_GLOBAL_DYNAMIC:
9423 case TLS_MODEL_LOCAL_DYNAMIC:
9424 fputs ("@m", file);
9425 break;
9426 default:
9427 gcc_unreachable ();
9432 /* Return true if X is a symbol that refers to real (rather than emulated)
9433 TLS. */
9435 static bool
9436 rs6000_real_tls_symbol_ref_p (rtx x)
9438 return (GET_CODE (x) == SYMBOL_REF
9439 && SYMBOL_REF_TLS_MODEL (x) >= TLS_MODEL_REAL);
9442 /* In the name of slightly smaller debug output, and to cater to
9443 general assembler lossage, recognize various UNSPEC sequences
9444 and turn them back into a direct symbol reference. */
9446 static rtx
9447 rs6000_delegitimize_address (rtx orig_x)
9449 rtx x, y, offset;
9451 orig_x = delegitimize_mem_from_attrs (orig_x);
9452 x = orig_x;
9453 if (MEM_P (x))
9454 x = XEXP (x, 0);
9456 y = x;
9457 if (TARGET_CMODEL != CMODEL_SMALL
9458 && GET_CODE (y) == LO_SUM)
9459 y = XEXP (y, 1);
9461 offset = NULL_RTX;
9462 if (GET_CODE (y) == PLUS
9463 && GET_MODE (y) == Pmode
9464 && CONST_INT_P (XEXP (y, 1)))
9466 offset = XEXP (y, 1);
9467 y = XEXP (y, 0);
9470 if (GET_CODE (y) == UNSPEC
9471 && XINT (y, 1) == UNSPEC_TOCREL)
9473 y = XVECEXP (y, 0, 0);
9475 #ifdef HAVE_AS_TLS
9476 /* Do not associate thread-local symbols with the original
9477 constant pool symbol. */
9478 if (TARGET_XCOFF
9479 && GET_CODE (y) == SYMBOL_REF
9480 && CONSTANT_POOL_ADDRESS_P (y)
9481 && rs6000_real_tls_symbol_ref_p (get_pool_constant (y)))
9482 return orig_x;
9483 #endif
9485 if (offset != NULL_RTX)
9486 y = gen_rtx_PLUS (Pmode, y, offset);
9487 if (!MEM_P (orig_x))
9488 return y;
9489 else
9490 return replace_equiv_address_nv (orig_x, y);
9493 if (TARGET_MACHO
9494 && GET_CODE (orig_x) == LO_SUM
9495 && GET_CODE (XEXP (orig_x, 1)) == CONST)
9497 y = XEXP (XEXP (orig_x, 1), 0);
9498 if (GET_CODE (y) == UNSPEC
9499 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
9500 return XVECEXP (y, 0, 0);
9503 return orig_x;
9506 /* Return true if X shouldn't be emitted into the debug info.
9507 The linker doesn't like .toc section references from
9508 .debug_* sections, so reject .toc section symbols. */
9510 static bool
9511 rs6000_const_not_ok_for_debug_p (rtx x)
9513 if (GET_CODE (x) == SYMBOL_REF
9514 && CONSTANT_POOL_ADDRESS_P (x))
9516 rtx c = get_pool_constant (x);
9517 machine_mode cmode = get_pool_mode (x);
9518 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
9519 return true;
9522 return false;
9526 /* Implement the TARGET_LEGITIMATE_COMBINED_INSN hook. */
9528 static bool
9529 rs6000_legitimate_combined_insn (rtx_insn *insn)
9531 int icode = INSN_CODE (insn);
9533 /* Reject creating doloop insns. Combine should not be allowed
9534 to create these for a number of reasons:
9535 1) In a nested loop, if combine creates one of these in an
9536 outer loop and the register allocator happens to allocate ctr
9537 to the outer loop insn, then the inner loop can't use ctr.
9538 Inner loops ought to be more highly optimized.
9539 2) Combine often wants to create one of these from what was
9540 originally a three insn sequence, first combining the three
9541 insns to two, then to ctrsi/ctrdi. When ctrsi/ctrdi is not
9542 allocated ctr, the splitter takes use back to the three insn
9543 sequence. It's better to stop combine at the two insn
9544 sequence.
9545 3) Faced with not being able to allocate ctr for ctrsi/crtdi
9546 insns, the register allocator sometimes uses floating point
9547 or vector registers for the pseudo. Since ctrsi/ctrdi is a
9548 jump insn and output reloads are not implemented for jumps,
9549 the ctrsi/ctrdi splitters need to handle all possible cases.
9550 That's a pain, and it gets to be seriously difficult when a
9551 splitter that runs after reload needs memory to transfer from
9552 a gpr to fpr. See PR70098 and PR71763 which are not fixed
9553 for the difficult case. It's better to not create problems
9554 in the first place. */
9555 if (icode != CODE_FOR_nothing
9556 && (icode == CODE_FOR_ctrsi_internal1
9557 || icode == CODE_FOR_ctrdi_internal1
9558 || icode == CODE_FOR_ctrsi_internal2
9559 || icode == CODE_FOR_ctrdi_internal2
9560 || icode == CODE_FOR_ctrsi_internal3
9561 || icode == CODE_FOR_ctrdi_internal3
9562 || icode == CODE_FOR_ctrsi_internal4
9563 || icode == CODE_FOR_ctrdi_internal4))
9564 return false;
9566 return true;
9569 /* Construct the SYMBOL_REF for the tls_get_addr function. */
9571 static GTY(()) rtx rs6000_tls_symbol;
9572 static rtx
9573 rs6000_tls_get_addr (void)
9575 if (!rs6000_tls_symbol)
9576 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
9578 return rs6000_tls_symbol;
9581 /* Construct the SYMBOL_REF for TLS GOT references. */
9583 static GTY(()) rtx rs6000_got_symbol;
9584 static rtx
9585 rs6000_got_sym (void)
9587 if (!rs6000_got_symbol)
9589 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
9590 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
9591 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
9594 return rs6000_got_symbol;
9597 /* AIX Thread-Local Address support. */
9599 static rtx
9600 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
9602 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
9603 const char *name;
9604 char *tlsname;
9606 name = XSTR (addr, 0);
9607 /* Append TLS CSECT qualifier, unless the symbol already is qualified
9608 or the symbol will be in TLS private data section. */
9609 if (name[strlen (name) - 1] != ']'
9610 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
9611 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
9613 tlsname = XALLOCAVEC (char, strlen (name) + 4);
9614 strcpy (tlsname, name);
9615 strcat (tlsname,
9616 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
9617 tlsaddr = copy_rtx (addr);
9618 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
9620 else
9621 tlsaddr = addr;
9623 /* Place addr into TOC constant pool. */
9624 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
9626 /* Output the TOC entry and create the MEM referencing the value. */
9627 if (constant_pool_expr_p (XEXP (sym, 0))
9628 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
9630 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
9631 mem = gen_const_mem (Pmode, tocref);
9632 set_mem_alias_set (mem, get_TOC_alias_set ());
9634 else
9635 return sym;
9637 /* Use global-dynamic for local-dynamic. */
9638 if (model == TLS_MODEL_GLOBAL_DYNAMIC
9639 || model == TLS_MODEL_LOCAL_DYNAMIC)
9641 /* Create new TOC reference for @m symbol. */
9642 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
9643 tlsname = XALLOCAVEC (char, strlen (name) + 1);
9644 strcpy (tlsname, "*LCM");
9645 strcat (tlsname, name + 3);
9646 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
9647 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
9648 tocref = create_TOC_reference (modaddr, NULL_RTX);
9649 rtx modmem = gen_const_mem (Pmode, tocref);
9650 set_mem_alias_set (modmem, get_TOC_alias_set ());
9652 rtx modreg = gen_reg_rtx (Pmode);
9653 emit_insn (gen_rtx_SET (modreg, modmem));
9655 tmpreg = gen_reg_rtx (Pmode);
9656 emit_insn (gen_rtx_SET (tmpreg, mem));
9658 dest = gen_reg_rtx (Pmode);
9659 if (TARGET_32BIT)
9660 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
9661 else
9662 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
9663 return dest;
9665 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
9666 else if (TARGET_32BIT)
9668 tlsreg = gen_reg_rtx (SImode);
9669 emit_insn (gen_tls_get_tpointer (tlsreg));
9671 else
9672 tlsreg = gen_rtx_REG (DImode, 13);
9674 /* Load the TOC value into temporary register. */
9675 tmpreg = gen_reg_rtx (Pmode);
9676 emit_insn (gen_rtx_SET (tmpreg, mem));
9677 set_unique_reg_note (get_last_insn (), REG_EQUAL,
9678 gen_rtx_MINUS (Pmode, addr, tlsreg));
9680 /* Add TOC symbol value to TLS pointer. */
9681 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
9683 return dest;
9686 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
9687 this (thread-local) address. */
9689 static rtx
9690 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
9692 rtx dest, insn;
9694 if (TARGET_XCOFF)
9695 return rs6000_legitimize_tls_address_aix (addr, model);
9697 dest = gen_reg_rtx (Pmode);
9698 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
9700 rtx tlsreg;
9702 if (TARGET_64BIT)
9704 tlsreg = gen_rtx_REG (Pmode, 13);
9705 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
9707 else
9709 tlsreg = gen_rtx_REG (Pmode, 2);
9710 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
9712 emit_insn (insn);
9714 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
9716 rtx tlsreg, tmp;
9718 tmp = gen_reg_rtx (Pmode);
9719 if (TARGET_64BIT)
9721 tlsreg = gen_rtx_REG (Pmode, 13);
9722 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
9724 else
9726 tlsreg = gen_rtx_REG (Pmode, 2);
9727 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
9729 emit_insn (insn);
9730 if (TARGET_64BIT)
9731 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
9732 else
9733 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
9734 emit_insn (insn);
9736 else
9738 rtx r3, got, tga, tmp1, tmp2, call_insn;
9740 /* We currently use relocations like @got@tlsgd for tls, which
9741 means the linker will handle allocation of tls entries, placing
9742 them in the .got section. So use a pointer to the .got section,
9743 not one to secondary TOC sections used by 64-bit -mminimal-toc,
9744 or to secondary GOT sections used by 32-bit -fPIC. */
9745 if (TARGET_64BIT)
9746 got = gen_rtx_REG (Pmode, 2);
9747 else
9749 if (flag_pic == 1)
9750 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
9751 else
9753 rtx gsym = rs6000_got_sym ();
9754 got = gen_reg_rtx (Pmode);
9755 if (flag_pic == 0)
9756 rs6000_emit_move (got, gsym, Pmode);
9757 else
9759 rtx mem, lab;
9761 tmp1 = gen_reg_rtx (Pmode);
9762 tmp2 = gen_reg_rtx (Pmode);
9763 mem = gen_const_mem (Pmode, tmp1);
9764 lab = gen_label_rtx ();
9765 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
9766 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
9767 if (TARGET_LINK_STACK)
9768 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
9769 emit_move_insn (tmp2, mem);
9770 rtx_insn *last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
9771 set_unique_reg_note (last, REG_EQUAL, gsym);
9776 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
9778 tga = rs6000_tls_get_addr ();
9779 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
9780 const0_rtx, Pmode);
9782 r3 = gen_rtx_REG (Pmode, 3);
9783 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9785 if (TARGET_64BIT)
9786 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
9787 else
9788 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
9790 else if (DEFAULT_ABI == ABI_V4)
9791 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
9792 else
9793 gcc_unreachable ();
9794 call_insn = last_call_insn ();
9795 PATTERN (call_insn) = insn;
9796 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
9797 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
9798 pic_offset_table_rtx);
9800 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
9802 tga = rs6000_tls_get_addr ();
9803 tmp1 = gen_reg_rtx (Pmode);
9804 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
9805 const0_rtx, Pmode);
9807 r3 = gen_rtx_REG (Pmode, 3);
9808 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9810 if (TARGET_64BIT)
9811 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
9812 else
9813 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
9815 else if (DEFAULT_ABI == ABI_V4)
9816 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
9817 else
9818 gcc_unreachable ();
9819 call_insn = last_call_insn ();
9820 PATTERN (call_insn) = insn;
9821 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
9822 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
9823 pic_offset_table_rtx);
9825 if (rs6000_tls_size == 16)
9827 if (TARGET_64BIT)
9828 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
9829 else
9830 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
9832 else if (rs6000_tls_size == 32)
9834 tmp2 = gen_reg_rtx (Pmode);
9835 if (TARGET_64BIT)
9836 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
9837 else
9838 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
9839 emit_insn (insn);
9840 if (TARGET_64BIT)
9841 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
9842 else
9843 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
9845 else
9847 tmp2 = gen_reg_rtx (Pmode);
9848 if (TARGET_64BIT)
9849 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
9850 else
9851 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
9852 emit_insn (insn);
9853 insn = gen_rtx_SET (dest, gen_rtx_PLUS (Pmode, tmp2, tmp1));
9855 emit_insn (insn);
9857 else
9859 /* IE, or 64-bit offset LE. */
9860 tmp2 = gen_reg_rtx (Pmode);
9861 if (TARGET_64BIT)
9862 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
9863 else
9864 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
9865 emit_insn (insn);
9866 if (TARGET_64BIT)
9867 insn = gen_tls_tls_64 (dest, tmp2, addr);
9868 else
9869 insn = gen_tls_tls_32 (dest, tmp2, addr);
9870 emit_insn (insn);
9874 return dest;
9877 /* Only create the global variable for the stack protect guard if we are using
9878 the global flavor of that guard. */
9879 static tree
9880 rs6000_init_stack_protect_guard (void)
9882 if (rs6000_stack_protector_guard == SSP_GLOBAL)
9883 return default_stack_protect_guard ();
9885 return NULL_TREE;
9888 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
9890 static bool
9891 rs6000_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
9893 if (GET_CODE (x) == HIGH
9894 && GET_CODE (XEXP (x, 0)) == UNSPEC)
9895 return true;
9897 /* A TLS symbol in the TOC cannot contain a sum. */
9898 if (GET_CODE (x) == CONST
9899 && GET_CODE (XEXP (x, 0)) == PLUS
9900 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
9901 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
9902 return true;
9904 /* Do not place an ELF TLS symbol in the constant pool. */
9905 return TARGET_ELF && tls_referenced_p (x);
9908 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
9909 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
9910 can be addressed relative to the toc pointer. */
9912 static bool
9913 use_toc_relative_ref (rtx sym, machine_mode mode)
9915 return ((constant_pool_expr_p (sym)
9916 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
9917 get_pool_mode (sym)))
9918 || (TARGET_CMODEL == CMODEL_MEDIUM
9919 && SYMBOL_REF_LOCAL_P (sym)
9920 && GET_MODE_SIZE (mode) <= POWERPC64_TOC_POINTER_ALIGNMENT));
9923 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
9924 replace the input X, or the original X if no replacement is called for.
9925 The output parameter *WIN is 1 if the calling macro should goto WIN,
9926 0 if it should not.
9928 For RS/6000, we wish to handle large displacements off a base
9929 register by splitting the addend across an addiu/addis and the mem insn.
9930 This cuts number of extra insns needed from 3 to 1.
9932 On Darwin, we use this to generate code for floating point constants.
9933 A movsf_low is generated so we wind up with 2 instructions rather than 3.
9934 The Darwin code is inside #if TARGET_MACHO because only then are the
9935 machopic_* functions defined. */
9936 static rtx
9937 rs6000_legitimize_reload_address (rtx x, machine_mode mode,
9938 int opnum, int type,
9939 int ind_levels ATTRIBUTE_UNUSED, int *win)
9941 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
9942 bool quad_offset_p = mode_supports_vsx_dform_quad (mode);
9944 /* Nasty hack for vsx_splat_v2df/v2di load from mem, which takes a
9945 DFmode/DImode MEM. Ditto for ISA 3.0 vsx_splat_v4sf/v4si. */
9946 if (reg_offset_p
9947 && opnum == 1
9948 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
9949 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)
9950 || (mode == SFmode && recog_data.operand_mode[0] == V4SFmode
9951 && TARGET_P9_VECTOR)
9952 || (mode == SImode && recog_data.operand_mode[0] == V4SImode
9953 && TARGET_P9_VECTOR)))
9954 reg_offset_p = false;
9956 /* We must recognize output that we have already generated ourselves. */
9957 if (GET_CODE (x) == PLUS
9958 && GET_CODE (XEXP (x, 0)) == PLUS
9959 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
9960 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
9961 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9963 if (TARGET_DEBUG_ADDR)
9965 fprintf (stderr, "\nlegitimize_reload_address push_reload #1:\n");
9966 debug_rtx (x);
9968 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9969 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
9970 opnum, (enum reload_type) type);
9971 *win = 1;
9972 return x;
9975 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
9976 if (GET_CODE (x) == LO_SUM
9977 && GET_CODE (XEXP (x, 0)) == HIGH)
9979 if (TARGET_DEBUG_ADDR)
9981 fprintf (stderr, "\nlegitimize_reload_address push_reload #2:\n");
9982 debug_rtx (x);
9984 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9985 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
9986 opnum, (enum reload_type) type);
9987 *win = 1;
9988 return x;
9991 #if TARGET_MACHO
9992 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
9993 && GET_CODE (x) == LO_SUM
9994 && GET_CODE (XEXP (x, 0)) == PLUS
9995 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
9996 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
9997 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
9998 && machopic_operand_p (XEXP (x, 1)))
10000 /* Result of previous invocation of this function on Darwin
10001 floating point constant. */
10002 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10003 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10004 opnum, (enum reload_type) type);
10005 *win = 1;
10006 return x;
10008 #endif
10010 if (TARGET_CMODEL != CMODEL_SMALL
10011 && reg_offset_p
10012 && !quad_offset_p
10013 && small_toc_ref (x, VOIDmode))
10015 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
10016 x = gen_rtx_LO_SUM (Pmode, hi, x);
10017 if (TARGET_DEBUG_ADDR)
10019 fprintf (stderr, "\nlegitimize_reload_address push_reload #3:\n");
10020 debug_rtx (x);
10022 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10023 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10024 opnum, (enum reload_type) type);
10025 *win = 1;
10026 return x;
10029 if (GET_CODE (x) == PLUS
10030 && REG_P (XEXP (x, 0))
10031 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
10032 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
10033 && CONST_INT_P (XEXP (x, 1))
10034 && reg_offset_p
10035 && !SPE_VECTOR_MODE (mode)
10036 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
10037 && (quad_offset_p || !VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
10039 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
10040 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
10041 HOST_WIDE_INT high
10042 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
10044 /* Check for 32-bit overflow or quad addresses with one of the
10045 four least significant bits set. */
10046 if (high + low != val
10047 || (quad_offset_p && (low & 0xf)))
10049 *win = 0;
10050 return x;
10053 /* Reload the high part into a base reg; leave the low part
10054 in the mem directly. */
10056 x = gen_rtx_PLUS (GET_MODE (x),
10057 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
10058 GEN_INT (high)),
10059 GEN_INT (low));
10061 if (TARGET_DEBUG_ADDR)
10063 fprintf (stderr, "\nlegitimize_reload_address push_reload #4:\n");
10064 debug_rtx (x);
10066 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10067 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
10068 opnum, (enum reload_type) type);
10069 *win = 1;
10070 return x;
10073 if (GET_CODE (x) == SYMBOL_REF
10074 && reg_offset_p
10075 && !quad_offset_p
10076 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
10077 && !SPE_VECTOR_MODE (mode)
10078 #if TARGET_MACHO
10079 && DEFAULT_ABI == ABI_DARWIN
10080 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
10081 && machopic_symbol_defined_p (x)
10082 #else
10083 && DEFAULT_ABI == ABI_V4
10084 && !flag_pic
10085 #endif
10086 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
10087 The same goes for DImode without 64-bit gprs and DFmode and DDmode
10088 without fprs.
10089 ??? Assume floating point reg based on mode? This assumption is
10090 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
10091 where reload ends up doing a DFmode load of a constant from
10092 mem using two gprs. Unfortunately, at this point reload
10093 hasn't yet selected regs so poking around in reload data
10094 won't help and even if we could figure out the regs reliably,
10095 we'd still want to allow this transformation when the mem is
10096 naturally aligned. Since we say the address is good here, we
10097 can't disable offsets from LO_SUMs in mem_operand_gpr.
10098 FIXME: Allow offset from lo_sum for other modes too, when
10099 mem is sufficiently aligned.
10101 Also disallow this if the type can go in VMX/Altivec registers, since
10102 those registers do not have d-form (reg+offset) address modes. */
10103 && !reg_addr[mode].scalar_in_vmx_p
10104 && mode != TFmode
10105 && mode != TDmode
10106 && mode != IFmode
10107 && mode != KFmode
10108 && (mode != TImode || !TARGET_VSX_TIMODE)
10109 && mode != PTImode
10110 && (mode != DImode || TARGET_POWERPC64)
10111 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
10112 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
10114 #if TARGET_MACHO
10115 if (flag_pic)
10117 rtx offset = machopic_gen_offset (x);
10118 x = gen_rtx_LO_SUM (GET_MODE (x),
10119 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
10120 gen_rtx_HIGH (Pmode, offset)), offset);
10122 else
10123 #endif
10124 x = gen_rtx_LO_SUM (GET_MODE (x),
10125 gen_rtx_HIGH (Pmode, x), x);
10127 if (TARGET_DEBUG_ADDR)
10129 fprintf (stderr, "\nlegitimize_reload_address push_reload #5:\n");
10130 debug_rtx (x);
10132 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10133 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10134 opnum, (enum reload_type) type);
10135 *win = 1;
10136 return x;
10139 /* Reload an offset address wrapped by an AND that represents the
10140 masking of the lower bits. Strip the outer AND and let reload
10141 convert the offset address into an indirect address. For VSX,
10142 force reload to create the address with an AND in a separate
10143 register, because we can't guarantee an altivec register will
10144 be used. */
10145 if (VECTOR_MEM_ALTIVEC_P (mode)
10146 && GET_CODE (x) == AND
10147 && GET_CODE (XEXP (x, 0)) == PLUS
10148 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
10149 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
10150 && GET_CODE (XEXP (x, 1)) == CONST_INT
10151 && INTVAL (XEXP (x, 1)) == -16)
10153 x = XEXP (x, 0);
10154 *win = 1;
10155 return x;
10158 if (TARGET_TOC
10159 && reg_offset_p
10160 && !quad_offset_p
10161 && GET_CODE (x) == SYMBOL_REF
10162 && use_toc_relative_ref (x, mode))
10164 x = create_TOC_reference (x, NULL_RTX);
10165 if (TARGET_CMODEL != CMODEL_SMALL)
10167 if (TARGET_DEBUG_ADDR)
10169 fprintf (stderr, "\nlegitimize_reload_address push_reload #6:\n");
10170 debug_rtx (x);
10172 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10173 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
10174 opnum, (enum reload_type) type);
10176 *win = 1;
10177 return x;
10179 *win = 0;
10180 return x;
10183 /* Debug version of rs6000_legitimize_reload_address. */
10184 static rtx
10185 rs6000_debug_legitimize_reload_address (rtx x, machine_mode mode,
10186 int opnum, int type,
10187 int ind_levels, int *win)
10189 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
10190 ind_levels, win);
10191 fprintf (stderr,
10192 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
10193 "type = %d, ind_levels = %d, win = %d, original addr:\n",
10194 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
10195 debug_rtx (x);
10197 if (x == ret)
10198 fprintf (stderr, "Same address returned\n");
10199 else if (!ret)
10200 fprintf (stderr, "NULL returned\n");
10201 else
10203 fprintf (stderr, "New address:\n");
10204 debug_rtx (ret);
10207 return ret;
10210 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
10211 that is a valid memory address for an instruction.
10212 The MODE argument is the machine mode for the MEM expression
10213 that wants to use this address.
10215 On the RS/6000, there are four valid address: a SYMBOL_REF that
10216 refers to a constant pool entry of an address (or the sum of it
10217 plus a constant), a short (16-bit signed) constant plus a register,
10218 the sum of two registers, or a register indirect, possibly with an
10219 auto-increment. For DFmode, DDmode and DImode with a constant plus
10220 register, we must ensure that both words are addressable or PowerPC64
10221 with offset word aligned.
10223 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
10224 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
10225 because adjacent memory cells are accessed by adding word-sized offsets
10226 during assembly output. */
10227 static bool
10228 rs6000_legitimate_address_p (machine_mode mode, rtx x, bool reg_ok_strict)
10230 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
10231 bool quad_offset_p = mode_supports_vsx_dform_quad (mode);
10233 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
10234 if (VECTOR_MEM_ALTIVEC_P (mode)
10235 && GET_CODE (x) == AND
10236 && GET_CODE (XEXP (x, 1)) == CONST_INT
10237 && INTVAL (XEXP (x, 1)) == -16)
10238 x = XEXP (x, 0);
10240 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
10241 return 0;
10242 if (legitimate_indirect_address_p (x, reg_ok_strict))
10243 return 1;
10244 if (TARGET_UPDATE
10245 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
10246 && mode_supports_pre_incdec_p (mode)
10247 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
10248 return 1;
10249 /* Handle restricted vector d-form offsets in ISA 3.0. */
10250 if (quad_offset_p)
10252 if (quad_address_p (x, mode, reg_ok_strict))
10253 return 1;
10255 else if (virtual_stack_registers_memory_p (x))
10256 return 1;
10258 else if (reg_offset_p)
10260 if (legitimate_small_data_p (mode, x))
10261 return 1;
10262 if (legitimate_constant_pool_address_p (x, mode,
10263 reg_ok_strict || lra_in_progress))
10264 return 1;
10265 if (reg_addr[mode].fused_toc && GET_CODE (x) == UNSPEC
10266 && XINT (x, 1) == UNSPEC_FUSION_ADDIS)
10267 return 1;
10270 /* For TImode, if we have TImode in VSX registers, only allow register
10271 indirect addresses. This will allow the values to go in either GPRs
10272 or VSX registers without reloading. The vector types would tend to
10273 go into VSX registers, so we allow REG+REG, while TImode seems
10274 somewhat split, in that some uses are GPR based, and some VSX based. */
10275 /* FIXME: We could loosen this by changing the following to
10276 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
10277 but currently we cannot allow REG+REG addressing for TImode. See
10278 PR72827 for complete details on how this ends up hoodwinking DSE. */
10279 if (mode == TImode && TARGET_VSX_TIMODE)
10280 return 0;
10281 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
10282 if (! reg_ok_strict
10283 && reg_offset_p
10284 && GET_CODE (x) == PLUS
10285 && GET_CODE (XEXP (x, 0)) == REG
10286 && (XEXP (x, 0) == virtual_stack_vars_rtx
10287 || XEXP (x, 0) == arg_pointer_rtx)
10288 && GET_CODE (XEXP (x, 1)) == CONST_INT)
10289 return 1;
10290 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
10291 return 1;
10292 if (!FLOAT128_2REG_P (mode)
10293 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
10294 || TARGET_POWERPC64
10295 || (mode != DFmode && mode != DDmode)
10296 || (TARGET_E500_DOUBLE && mode != DDmode))
10297 && (TARGET_POWERPC64 || mode != DImode)
10298 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
10299 && mode != PTImode
10300 && !avoiding_indexed_address_p (mode)
10301 && legitimate_indexed_address_p (x, reg_ok_strict))
10302 return 1;
10303 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
10304 && mode_supports_pre_modify_p (mode)
10305 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
10306 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
10307 reg_ok_strict, false)
10308 || (!avoiding_indexed_address_p (mode)
10309 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
10310 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
10311 return 1;
10312 if (reg_offset_p && !quad_offset_p
10313 && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
10314 return 1;
10315 return 0;
10318 /* Debug version of rs6000_legitimate_address_p. */
10319 static bool
10320 rs6000_debug_legitimate_address_p (machine_mode mode, rtx x,
10321 bool reg_ok_strict)
10323 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
10324 fprintf (stderr,
10325 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
10326 "strict = %d, reload = %s, code = %s\n",
10327 ret ? "true" : "false",
10328 GET_MODE_NAME (mode),
10329 reg_ok_strict,
10330 (reload_completed
10331 ? "after"
10332 : (reload_in_progress ? "progress" : "before")),
10333 GET_RTX_NAME (GET_CODE (x)));
10334 debug_rtx (x);
10336 return ret;
10339 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
10341 static bool
10342 rs6000_mode_dependent_address_p (const_rtx addr,
10343 addr_space_t as ATTRIBUTE_UNUSED)
10345 return rs6000_mode_dependent_address_ptr (addr);
10348 /* Go to LABEL if ADDR (a legitimate address expression)
10349 has an effect that depends on the machine mode it is used for.
10351 On the RS/6000 this is true of all integral offsets (since AltiVec
10352 and VSX modes don't allow them) or is a pre-increment or decrement.
10354 ??? Except that due to conceptual problems in offsettable_address_p
10355 we can't really report the problems of integral offsets. So leave
10356 this assuming that the adjustable offset must be valid for the
10357 sub-words of a TFmode operand, which is what we had before. */
10359 static bool
10360 rs6000_mode_dependent_address (const_rtx addr)
10362 switch (GET_CODE (addr))
10364 case PLUS:
10365 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
10366 is considered a legitimate address before reload, so there
10367 are no offset restrictions in that case. Note that this
10368 condition is safe in strict mode because any address involving
10369 virtual_stack_vars_rtx or arg_pointer_rtx would already have
10370 been rejected as illegitimate. */
10371 if (XEXP (addr, 0) != virtual_stack_vars_rtx
10372 && XEXP (addr, 0) != arg_pointer_rtx
10373 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
10375 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
10376 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
10378 break;
10380 case LO_SUM:
10381 /* Anything in the constant pool is sufficiently aligned that
10382 all bytes have the same high part address. */
10383 return !legitimate_constant_pool_address_p (addr, QImode, false);
10385 /* Auto-increment cases are now treated generically in recog.c. */
10386 case PRE_MODIFY:
10387 return TARGET_UPDATE;
10389 /* AND is only allowed in Altivec loads. */
10390 case AND:
10391 return true;
10393 default:
10394 break;
10397 return false;
10400 /* Debug version of rs6000_mode_dependent_address. */
10401 static bool
10402 rs6000_debug_mode_dependent_address (const_rtx addr)
10404 bool ret = rs6000_mode_dependent_address (addr);
10406 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
10407 ret ? "true" : "false");
10408 debug_rtx (addr);
10410 return ret;
10413 /* Implement FIND_BASE_TERM. */
10416 rs6000_find_base_term (rtx op)
10418 rtx base;
10420 base = op;
10421 if (GET_CODE (base) == CONST)
10422 base = XEXP (base, 0);
10423 if (GET_CODE (base) == PLUS)
10424 base = XEXP (base, 0);
10425 if (GET_CODE (base) == UNSPEC)
10426 switch (XINT (base, 1))
10428 case UNSPEC_TOCREL:
10429 case UNSPEC_MACHOPIC_OFFSET:
10430 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
10431 for aliasing purposes. */
10432 return XVECEXP (base, 0, 0);
10435 return op;
10438 /* More elaborate version of recog's offsettable_memref_p predicate
10439 that works around the ??? note of rs6000_mode_dependent_address.
10440 In particular it accepts
10442 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
10444 in 32-bit mode, that the recog predicate rejects. */
10446 static bool
10447 rs6000_offsettable_memref_p (rtx op, machine_mode reg_mode)
10449 bool worst_case;
10451 if (!MEM_P (op))
10452 return false;
10454 /* First mimic offsettable_memref_p. */
10455 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
10456 return true;
10458 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
10459 the latter predicate knows nothing about the mode of the memory
10460 reference and, therefore, assumes that it is the largest supported
10461 mode (TFmode). As a consequence, legitimate offsettable memory
10462 references are rejected. rs6000_legitimate_offset_address_p contains
10463 the correct logic for the PLUS case of rs6000_mode_dependent_address,
10464 at least with a little bit of help here given that we know the
10465 actual registers used. */
10466 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
10467 || GET_MODE_SIZE (reg_mode) == 4);
10468 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
10469 true, worst_case);
10472 /* Determine the reassociation width to be used in reassociate_bb.
10473 This takes into account how many parallel operations we
10474 can actually do of a given type, and also the latency.
10476 int add/sub 6/cycle
10477 mul 2/cycle
10478 vect add/sub/mul 2/cycle
10479 fp add/sub/mul 2/cycle
10480 dfp 1/cycle
10483 static int
10484 rs6000_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
10485 machine_mode mode)
10487 switch (rs6000_cpu)
10489 case PROCESSOR_POWER8:
10490 case PROCESSOR_POWER9:
10491 if (DECIMAL_FLOAT_MODE_P (mode))
10492 return 1;
10493 if (VECTOR_MODE_P (mode))
10494 return 4;
10495 if (INTEGRAL_MODE_P (mode))
10496 return opc == MULT_EXPR ? 4 : 6;
10497 if (FLOAT_MODE_P (mode))
10498 return 4;
10499 break;
10500 default:
10501 break;
10503 return 1;
10506 /* Change register usage conditional on target flags. */
10507 static void
10508 rs6000_conditional_register_usage (void)
10510 int i;
10512 if (TARGET_DEBUG_TARGET)
10513 fprintf (stderr, "rs6000_conditional_register_usage called\n");
10515 /* Set MQ register fixed (already call_used) so that it will not be
10516 allocated. */
10517 fixed_regs[64] = 1;
10519 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
10520 if (TARGET_64BIT)
10521 fixed_regs[13] = call_used_regs[13]
10522 = call_really_used_regs[13] = 1;
10524 /* Conditionally disable FPRs. */
10525 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
10526 for (i = 32; i < 64; i++)
10527 fixed_regs[i] = call_used_regs[i]
10528 = call_really_used_regs[i] = 1;
10530 /* The TOC register is not killed across calls in a way that is
10531 visible to the compiler. */
10532 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10533 call_really_used_regs[2] = 0;
10535 if (DEFAULT_ABI == ABI_V4 && flag_pic == 2)
10536 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10538 if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10539 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10540 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10541 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10543 if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
10544 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10545 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10546 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10548 if (TARGET_TOC && TARGET_MINIMAL_TOC)
10549 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10550 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10552 if (TARGET_SPE)
10554 global_regs[SPEFSCR_REGNO] = 1;
10555 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
10556 registers in prologues and epilogues. We no longer use r14
10557 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
10558 pool for link-compatibility with older versions of GCC. Once
10559 "old" code has died out, we can return r14 to the allocation
10560 pool. */
10561 fixed_regs[14]
10562 = call_used_regs[14]
10563 = call_really_used_regs[14] = 1;
10566 if (!TARGET_ALTIVEC && !TARGET_VSX)
10568 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10569 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
10570 call_really_used_regs[VRSAVE_REGNO] = 1;
10573 if (TARGET_ALTIVEC || TARGET_VSX)
10574 global_regs[VSCR_REGNO] = 1;
10576 if (TARGET_ALTIVEC_ABI)
10578 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
10579 call_used_regs[i] = call_really_used_regs[i] = 1;
10581 /* AIX reserves VR20:31 in non-extended ABI mode. */
10582 if (TARGET_XCOFF)
10583 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
10584 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
10589 /* Output insns to set DEST equal to the constant SOURCE as a series of
10590 lis, ori and shl instructions and return TRUE. */
10592 bool
10593 rs6000_emit_set_const (rtx dest, rtx source)
10595 machine_mode mode = GET_MODE (dest);
10596 rtx temp, set;
10597 rtx_insn *insn;
10598 HOST_WIDE_INT c;
10600 gcc_checking_assert (CONST_INT_P (source));
10601 c = INTVAL (source);
10602 switch (mode)
10604 case E_QImode:
10605 case E_HImode:
10606 emit_insn (gen_rtx_SET (dest, source));
10607 return true;
10609 case E_SImode:
10610 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
10612 emit_insn (gen_rtx_SET (copy_rtx (temp),
10613 GEN_INT (c & ~(HOST_WIDE_INT) 0xffff)));
10614 emit_insn (gen_rtx_SET (dest,
10615 gen_rtx_IOR (SImode, copy_rtx (temp),
10616 GEN_INT (c & 0xffff))));
10617 break;
10619 case E_DImode:
10620 if (!TARGET_POWERPC64)
10622 rtx hi, lo;
10624 hi = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN == 0,
10625 DImode);
10626 lo = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
10627 DImode);
10628 emit_move_insn (hi, GEN_INT (c >> 32));
10629 c = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
10630 emit_move_insn (lo, GEN_INT (c));
10632 else
10633 rs6000_emit_set_long_const (dest, c);
10634 break;
10636 default:
10637 gcc_unreachable ();
10640 insn = get_last_insn ();
10641 set = single_set (insn);
10642 if (! CONSTANT_P (SET_SRC (set)))
10643 set_unique_reg_note (insn, REG_EQUAL, GEN_INT (c));
10645 return true;
10648 /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode.
10649 Output insns to set DEST equal to the constant C as a series of
10650 lis, ori and shl instructions. */
10652 static void
10653 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c)
10655 rtx temp;
10656 HOST_WIDE_INT ud1, ud2, ud3, ud4;
10658 ud1 = c & 0xffff;
10659 c = c >> 16;
10660 ud2 = c & 0xffff;
10661 c = c >> 16;
10662 ud3 = c & 0xffff;
10663 c = c >> 16;
10664 ud4 = c & 0xffff;
10666 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
10667 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
10668 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
10670 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
10671 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
10673 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10675 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10676 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
10677 if (ud1 != 0)
10678 emit_move_insn (dest,
10679 gen_rtx_IOR (DImode, copy_rtx (temp),
10680 GEN_INT (ud1)));
10682 else if (ud3 == 0 && ud4 == 0)
10684 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10686 gcc_assert (ud2 & 0x8000);
10687 emit_move_insn (copy_rtx (temp),
10688 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
10689 if (ud1 != 0)
10690 emit_move_insn (copy_rtx (temp),
10691 gen_rtx_IOR (DImode, copy_rtx (temp),
10692 GEN_INT (ud1)));
10693 emit_move_insn (dest,
10694 gen_rtx_ZERO_EXTEND (DImode,
10695 gen_lowpart (SImode,
10696 copy_rtx (temp))));
10698 else if ((ud4 == 0xffff && (ud3 & 0x8000))
10699 || (ud4 == 0 && ! (ud3 & 0x8000)))
10701 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10703 emit_move_insn (copy_rtx (temp),
10704 GEN_INT (((ud3 << 16) ^ 0x80000000) - 0x80000000));
10705 if (ud2 != 0)
10706 emit_move_insn (copy_rtx (temp),
10707 gen_rtx_IOR (DImode, copy_rtx (temp),
10708 GEN_INT (ud2)));
10709 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10710 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
10711 GEN_INT (16)));
10712 if (ud1 != 0)
10713 emit_move_insn (dest,
10714 gen_rtx_IOR (DImode, copy_rtx (temp),
10715 GEN_INT (ud1)));
10717 else
10719 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10721 emit_move_insn (copy_rtx (temp),
10722 GEN_INT (((ud4 << 16) ^ 0x80000000) - 0x80000000));
10723 if (ud3 != 0)
10724 emit_move_insn (copy_rtx (temp),
10725 gen_rtx_IOR (DImode, copy_rtx (temp),
10726 GEN_INT (ud3)));
10728 emit_move_insn (ud2 != 0 || ud1 != 0 ? copy_rtx (temp) : dest,
10729 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
10730 GEN_INT (32)));
10731 if (ud2 != 0)
10732 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10733 gen_rtx_IOR (DImode, copy_rtx (temp),
10734 GEN_INT (ud2 << 16)));
10735 if (ud1 != 0)
10736 emit_move_insn (dest,
10737 gen_rtx_IOR (DImode, copy_rtx (temp),
10738 GEN_INT (ud1)));
10742 /* Helper for the following. Get rid of [r+r] memory refs
10743 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
10745 static void
10746 rs6000_eliminate_indexed_memrefs (rtx operands[2])
10748 if (reload_in_progress)
10749 return;
10751 if (GET_CODE (operands[0]) == MEM
10752 && GET_CODE (XEXP (operands[0], 0)) != REG
10753 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
10754 GET_MODE (operands[0]), false))
10755 operands[0]
10756 = replace_equiv_address (operands[0],
10757 copy_addr_to_reg (XEXP (operands[0], 0)));
10759 if (GET_CODE (operands[1]) == MEM
10760 && GET_CODE (XEXP (operands[1], 0)) != REG
10761 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
10762 GET_MODE (operands[1]), false))
10763 operands[1]
10764 = replace_equiv_address (operands[1],
10765 copy_addr_to_reg (XEXP (operands[1], 0)));
10768 /* Generate a vector of constants to permute MODE for a little-endian
10769 storage operation by swapping the two halves of a vector. */
10770 static rtvec
10771 rs6000_const_vec (machine_mode mode)
10773 int i, subparts;
10774 rtvec v;
10776 switch (mode)
10778 case E_V1TImode:
10779 subparts = 1;
10780 break;
10781 case E_V2DFmode:
10782 case E_V2DImode:
10783 subparts = 2;
10784 break;
10785 case E_V4SFmode:
10786 case E_V4SImode:
10787 subparts = 4;
10788 break;
10789 case E_V8HImode:
10790 subparts = 8;
10791 break;
10792 case E_V16QImode:
10793 subparts = 16;
10794 break;
10795 default:
10796 gcc_unreachable();
10799 v = rtvec_alloc (subparts);
10801 for (i = 0; i < subparts / 2; ++i)
10802 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
10803 for (i = subparts / 2; i < subparts; ++i)
10804 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
10806 return v;
10809 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
10810 for a VSX load or store operation. */
10812 rs6000_gen_le_vsx_permute (rtx source, machine_mode mode)
10814 /* Use ROTATE instead of VEC_SELECT on IEEE 128-bit floating point, and
10815 128-bit integers if they are allowed in VSX registers. */
10816 if (FLOAT128_VECTOR_P (mode) || mode == TImode || mode == V1TImode)
10817 return gen_rtx_ROTATE (mode, source, GEN_INT (64));
10818 else
10820 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
10821 return gen_rtx_VEC_SELECT (mode, source, par);
10825 /* Emit a little-endian load from vector memory location SOURCE to VSX
10826 register DEST in mode MODE. The load is done with two permuting
10827 insn's that represent an lxvd2x and xxpermdi. */
10828 void
10829 rs6000_emit_le_vsx_load (rtx dest, rtx source, machine_mode mode)
10831 rtx tmp, permute_mem, permute_reg;
10833 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
10834 V1TImode). */
10835 if (mode == TImode || mode == V1TImode)
10837 mode = V2DImode;
10838 dest = gen_lowpart (V2DImode, dest);
10839 source = adjust_address (source, V2DImode, 0);
10842 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
10843 permute_mem = rs6000_gen_le_vsx_permute (source, mode);
10844 permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
10845 emit_insn (gen_rtx_SET (tmp, permute_mem));
10846 emit_insn (gen_rtx_SET (dest, permute_reg));
10849 /* Emit a little-endian store to vector memory location DEST from VSX
10850 register SOURCE in mode MODE. The store is done with two permuting
10851 insn's that represent an xxpermdi and an stxvd2x. */
10852 void
10853 rs6000_emit_le_vsx_store (rtx dest, rtx source, machine_mode mode)
10855 rtx tmp, permute_src, permute_tmp;
10857 /* This should never be called during or after reload, because it does
10858 not re-permute the source register. It is intended only for use
10859 during expand. */
10860 gcc_assert (!reload_in_progress && !lra_in_progress && !reload_completed);
10862 /* Use V2DImode to do swaps of types with 128-bit scalar parts (TImode,
10863 V1TImode). */
10864 if (mode == TImode || mode == V1TImode)
10866 mode = V2DImode;
10867 dest = adjust_address (dest, V2DImode, 0);
10868 source = gen_lowpart (V2DImode, source);
10871 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
10872 permute_src = rs6000_gen_le_vsx_permute (source, mode);
10873 permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
10874 emit_insn (gen_rtx_SET (tmp, permute_src));
10875 emit_insn (gen_rtx_SET (dest, permute_tmp));
10878 /* Emit a sequence representing a little-endian VSX load or store,
10879 moving data from SOURCE to DEST in mode MODE. This is done
10880 separately from rs6000_emit_move to ensure it is called only
10881 during expand. LE VSX loads and stores introduced later are
10882 handled with a split. The expand-time RTL generation allows
10883 us to optimize away redundant pairs of register-permutes. */
10884 void
10885 rs6000_emit_le_vsx_move (rtx dest, rtx source, machine_mode mode)
10887 gcc_assert (!BYTES_BIG_ENDIAN
10888 && VECTOR_MEM_VSX_P (mode)
10889 && !TARGET_P9_VECTOR
10890 && !gpr_or_gpr_p (dest, source)
10891 && (MEM_P (source) ^ MEM_P (dest)));
10893 if (MEM_P (source))
10895 gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
10896 rs6000_emit_le_vsx_load (dest, source, mode);
10898 else
10900 if (!REG_P (source))
10901 source = force_reg (mode, source);
10902 rs6000_emit_le_vsx_store (dest, source, mode);
10906 /* Return whether a SFmode or SImode move can be done without converting one
10907 mode to another. This arrises when we have:
10909 (SUBREG:SF (REG:SI ...))
10910 (SUBREG:SI (REG:SF ...))
10912 and one of the values is in a floating point/vector register, where SFmode
10913 scalars are stored in DFmode format. */
10915 bool
10916 valid_sf_si_move (rtx dest, rtx src, machine_mode mode)
10918 if (TARGET_ALLOW_SF_SUBREG)
10919 return true;
10921 if (mode != SFmode && GET_MODE_CLASS (mode) != MODE_INT)
10922 return true;
10924 if (!SUBREG_P (src) || !sf_subreg_operand (src, mode))
10925 return true;
10927 /*. Allow (set (SUBREG:SI (REG:SF)) (SUBREG:SI (REG:SF))). */
10928 if (SUBREG_P (dest))
10930 rtx dest_subreg = SUBREG_REG (dest);
10931 rtx src_subreg = SUBREG_REG (src);
10932 return GET_MODE (dest_subreg) == GET_MODE (src_subreg);
10935 return false;
10939 /* Helper function to change moves with:
10941 (SUBREG:SF (REG:SI)) and
10942 (SUBREG:SI (REG:SF))
10944 into separate UNSPEC insns. In the PowerPC architecture, scalar SFmode
10945 values are stored as DFmode values in the VSX registers. We need to convert
10946 the bits before we can use a direct move or operate on the bits in the
10947 vector register as an integer type.
10949 Skip things like (set (SUBREG:SI (...) (SUBREG:SI (...)). */
10951 static bool
10952 rs6000_emit_move_si_sf_subreg (rtx dest, rtx source, machine_mode mode)
10954 if (TARGET_DIRECT_MOVE_64BIT && !reload_in_progress && !reload_completed
10955 && !lra_in_progress
10956 && (!SUBREG_P (dest) || !sf_subreg_operand (dest, mode))
10957 && SUBREG_P (source) && sf_subreg_operand (source, mode))
10959 rtx inner_source = SUBREG_REG (source);
10960 machine_mode inner_mode = GET_MODE (inner_source);
10962 if (mode == SImode && inner_mode == SFmode)
10964 emit_insn (gen_movsi_from_sf (dest, inner_source));
10965 return true;
10968 if (mode == SFmode && inner_mode == SImode)
10970 emit_insn (gen_movsf_from_si (dest, inner_source));
10971 return true;
10975 return false;
10978 /* Emit a move from SOURCE to DEST in mode MODE. */
10979 void
10980 rs6000_emit_move (rtx dest, rtx source, machine_mode mode)
10982 rtx operands[2];
10983 operands[0] = dest;
10984 operands[1] = source;
10986 if (TARGET_DEBUG_ADDR)
10988 fprintf (stderr,
10989 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
10990 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
10991 GET_MODE_NAME (mode),
10992 reload_in_progress,
10993 reload_completed,
10994 can_create_pseudo_p ());
10995 debug_rtx (dest);
10996 fprintf (stderr, "source:\n");
10997 debug_rtx (source);
11000 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
11001 if (CONST_WIDE_INT_P (operands[1])
11002 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11004 /* This should be fixed with the introduction of CONST_WIDE_INT. */
11005 gcc_unreachable ();
11008 /* See if we need to special case SImode/SFmode SUBREG moves. */
11009 if ((mode == SImode || mode == SFmode) && SUBREG_P (source)
11010 && rs6000_emit_move_si_sf_subreg (dest, source, mode))
11011 return;
11013 /* Check if GCC is setting up a block move that will end up using FP
11014 registers as temporaries. We must make sure this is acceptable. */
11015 if (GET_CODE (operands[0]) == MEM
11016 && GET_CODE (operands[1]) == MEM
11017 && mode == DImode
11018 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
11019 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
11020 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
11021 ? 32 : MEM_ALIGN (operands[0])))
11022 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
11023 ? 32
11024 : MEM_ALIGN (operands[1]))))
11025 && ! MEM_VOLATILE_P (operands [0])
11026 && ! MEM_VOLATILE_P (operands [1]))
11028 emit_move_insn (adjust_address (operands[0], SImode, 0),
11029 adjust_address (operands[1], SImode, 0));
11030 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
11031 adjust_address (copy_rtx (operands[1]), SImode, 4));
11032 return;
11035 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
11036 && !gpc_reg_operand (operands[1], mode))
11037 operands[1] = force_reg (mode, operands[1]);
11039 /* Recognize the case where operand[1] is a reference to thread-local
11040 data and load its address to a register. */
11041 if (tls_referenced_p (operands[1]))
11043 enum tls_model model;
11044 rtx tmp = operands[1];
11045 rtx addend = NULL;
11047 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
11049 addend = XEXP (XEXP (tmp, 0), 1);
11050 tmp = XEXP (XEXP (tmp, 0), 0);
11053 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
11054 model = SYMBOL_REF_TLS_MODEL (tmp);
11055 gcc_assert (model != 0);
11057 tmp = rs6000_legitimize_tls_address (tmp, model);
11058 if (addend)
11060 tmp = gen_rtx_PLUS (mode, tmp, addend);
11061 tmp = force_operand (tmp, operands[0]);
11063 operands[1] = tmp;
11066 /* Handle the case where reload calls us with an invalid address. */
11067 if (reload_in_progress && mode == Pmode
11068 && (! general_operand (operands[1], mode)
11069 || ! nonimmediate_operand (operands[0], mode)))
11070 goto emit_set;
11072 /* 128-bit constant floating-point values on Darwin should really be loaded
11073 as two parts. However, this premature splitting is a problem when DFmode
11074 values can go into Altivec registers. */
11075 if (FLOAT128_IBM_P (mode) && !reg_addr[DFmode].scalar_in_vmx_p
11076 && GET_CODE (operands[1]) == CONST_DOUBLE)
11078 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
11079 simplify_gen_subreg (DFmode, operands[1], mode, 0),
11080 DFmode);
11081 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
11082 GET_MODE_SIZE (DFmode)),
11083 simplify_gen_subreg (DFmode, operands[1], mode,
11084 GET_MODE_SIZE (DFmode)),
11085 DFmode);
11086 return;
11089 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
11090 cfun->machine->sdmode_stack_slot =
11091 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
11094 /* Transform (p0:DD, (SUBREG:DD p1:SD)) to ((SUBREG:SD p0:DD),
11095 p1:SD) if p1 is not of floating point class and p0 is spilled as
11096 we can have no analogous movsd_store for this. */
11097 if (lra_in_progress && mode == DDmode
11098 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
11099 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
11100 && GET_CODE (operands[1]) == SUBREG && REG_P (SUBREG_REG (operands[1]))
11101 && GET_MODE (SUBREG_REG (operands[1])) == SDmode)
11103 enum reg_class cl;
11104 int regno = REGNO (SUBREG_REG (operands[1]));
11106 if (regno >= FIRST_PSEUDO_REGISTER)
11108 cl = reg_preferred_class (regno);
11109 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][1];
11111 if (regno >= 0 && ! FP_REGNO_P (regno))
11113 mode = SDmode;
11114 operands[0] = gen_lowpart_SUBREG (SDmode, operands[0]);
11115 operands[1] = SUBREG_REG (operands[1]);
11118 if (lra_in_progress
11119 && mode == SDmode
11120 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
11121 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
11122 && (REG_P (operands[1])
11123 || (GET_CODE (operands[1]) == SUBREG
11124 && REG_P (SUBREG_REG (operands[1])))))
11126 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
11127 ? SUBREG_REG (operands[1]) : operands[1]);
11128 enum reg_class cl;
11130 if (regno >= FIRST_PSEUDO_REGISTER)
11132 cl = reg_preferred_class (regno);
11133 gcc_assert (cl != NO_REGS);
11134 regno = ira_class_hard_regs[cl][0];
11136 if (FP_REGNO_P (regno))
11138 if (GET_MODE (operands[0]) != DDmode)
11139 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
11140 emit_insn (gen_movsd_store (operands[0], operands[1]));
11142 else if (INT_REGNO_P (regno))
11143 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
11144 else
11145 gcc_unreachable();
11146 return;
11148 /* Transform ((SUBREG:DD p0:SD), p1:DD) to (p0:SD, (SUBREG:SD
11149 p:DD)) if p0 is not of floating point class and p1 is spilled as
11150 we can have no analogous movsd_load for this. */
11151 if (lra_in_progress && mode == DDmode
11152 && GET_CODE (operands[0]) == SUBREG && REG_P (SUBREG_REG (operands[0]))
11153 && GET_MODE (SUBREG_REG (operands[0])) == SDmode
11154 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
11155 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
11157 enum reg_class cl;
11158 int regno = REGNO (SUBREG_REG (operands[0]));
11160 if (regno >= FIRST_PSEUDO_REGISTER)
11162 cl = reg_preferred_class (regno);
11163 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][0];
11165 if (regno >= 0 && ! FP_REGNO_P (regno))
11167 mode = SDmode;
11168 operands[0] = SUBREG_REG (operands[0]);
11169 operands[1] = gen_lowpart_SUBREG (SDmode, operands[1]);
11172 if (lra_in_progress
11173 && mode == SDmode
11174 && (REG_P (operands[0])
11175 || (GET_CODE (operands[0]) == SUBREG
11176 && REG_P (SUBREG_REG (operands[0]))))
11177 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
11178 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
11180 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
11181 ? SUBREG_REG (operands[0]) : operands[0]);
11182 enum reg_class cl;
11184 if (regno >= FIRST_PSEUDO_REGISTER)
11186 cl = reg_preferred_class (regno);
11187 gcc_assert (cl != NO_REGS);
11188 regno = ira_class_hard_regs[cl][0];
11190 if (FP_REGNO_P (regno))
11192 if (GET_MODE (operands[1]) != DDmode)
11193 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
11194 emit_insn (gen_movsd_load (operands[0], operands[1]));
11196 else if (INT_REGNO_P (regno))
11197 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
11198 else
11199 gcc_unreachable();
11200 return;
11203 if (reload_in_progress
11204 && mode == SDmode
11205 && cfun->machine->sdmode_stack_slot != NULL_RTX
11206 && MEM_P (operands[0])
11207 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
11208 && REG_P (operands[1]))
11210 if (FP_REGNO_P (REGNO (operands[1])))
11212 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
11213 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11214 emit_insn (gen_movsd_store (mem, operands[1]));
11216 else if (INT_REGNO_P (REGNO (operands[1])))
11218 rtx mem = operands[0];
11219 if (BYTES_BIG_ENDIAN)
11220 mem = adjust_address_nv (mem, mode, 4);
11221 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11222 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
11224 else
11225 gcc_unreachable();
11226 return;
11228 if (reload_in_progress
11229 && mode == SDmode
11230 && REG_P (operands[0])
11231 && MEM_P (operands[1])
11232 && cfun->machine->sdmode_stack_slot != NULL_RTX
11233 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
11235 if (FP_REGNO_P (REGNO (operands[0])))
11237 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
11238 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11239 emit_insn (gen_movsd_load (operands[0], mem));
11241 else if (INT_REGNO_P (REGNO (operands[0])))
11243 rtx mem = operands[1];
11244 if (BYTES_BIG_ENDIAN)
11245 mem = adjust_address_nv (mem, mode, 4);
11246 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
11247 emit_insn (gen_movsd_hardfloat (operands[0], mem));
11249 else
11250 gcc_unreachable();
11251 return;
11254 /* FIXME: In the long term, this switch statement should go away
11255 and be replaced by a sequence of tests based on things like
11256 mode == Pmode. */
11257 switch (mode)
11259 case E_HImode:
11260 case E_QImode:
11261 if (CONSTANT_P (operands[1])
11262 && GET_CODE (operands[1]) != CONST_INT)
11263 operands[1] = force_const_mem (mode, operands[1]);
11264 break;
11266 case E_TFmode:
11267 case E_TDmode:
11268 case E_IFmode:
11269 case E_KFmode:
11270 if (FLOAT128_2REG_P (mode))
11271 rs6000_eliminate_indexed_memrefs (operands);
11272 /* fall through */
11274 case E_DFmode:
11275 case E_DDmode:
11276 case E_SFmode:
11277 case E_SDmode:
11278 if (CONSTANT_P (operands[1])
11279 && ! easy_fp_constant (operands[1], mode))
11280 operands[1] = force_const_mem (mode, operands[1]);
11281 break;
11283 case E_V16QImode:
11284 case E_V8HImode:
11285 case E_V4SFmode:
11286 case E_V4SImode:
11287 case E_V4HImode:
11288 case E_V2SFmode:
11289 case E_V2SImode:
11290 case E_V1DImode:
11291 case E_V2DFmode:
11292 case E_V2DImode:
11293 case E_V1TImode:
11294 if (CONSTANT_P (operands[1])
11295 && !easy_vector_constant (operands[1], mode))
11296 operands[1] = force_const_mem (mode, operands[1]);
11297 break;
11299 case E_SImode:
11300 case E_DImode:
11301 /* Use default pattern for address of ELF small data */
11302 if (TARGET_ELF
11303 && mode == Pmode
11304 && DEFAULT_ABI == ABI_V4
11305 && (GET_CODE (operands[1]) == SYMBOL_REF
11306 || GET_CODE (operands[1]) == CONST)
11307 && small_data_operand (operands[1], mode))
11309 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11310 return;
11313 if (DEFAULT_ABI == ABI_V4
11314 && mode == Pmode && mode == SImode
11315 && flag_pic == 1 && got_operand (operands[1], mode))
11317 emit_insn (gen_movsi_got (operands[0], operands[1]));
11318 return;
11321 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
11322 && TARGET_NO_TOC
11323 && ! flag_pic
11324 && mode == Pmode
11325 && CONSTANT_P (operands[1])
11326 && GET_CODE (operands[1]) != HIGH
11327 && GET_CODE (operands[1]) != CONST_INT)
11329 rtx target = (!can_create_pseudo_p ()
11330 ? operands[0]
11331 : gen_reg_rtx (mode));
11333 /* If this is a function address on -mcall-aixdesc,
11334 convert it to the address of the descriptor. */
11335 if (DEFAULT_ABI == ABI_AIX
11336 && GET_CODE (operands[1]) == SYMBOL_REF
11337 && XSTR (operands[1], 0)[0] == '.')
11339 const char *name = XSTR (operands[1], 0);
11340 rtx new_ref;
11341 while (*name == '.')
11342 name++;
11343 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
11344 CONSTANT_POOL_ADDRESS_P (new_ref)
11345 = CONSTANT_POOL_ADDRESS_P (operands[1]);
11346 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
11347 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
11348 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
11349 operands[1] = new_ref;
11352 if (DEFAULT_ABI == ABI_DARWIN)
11354 #if TARGET_MACHO
11355 if (MACHO_DYNAMIC_NO_PIC_P)
11357 /* Take care of any required data indirection. */
11358 operands[1] = rs6000_machopic_legitimize_pic_address (
11359 operands[1], mode, operands[0]);
11360 if (operands[0] != operands[1])
11361 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11362 return;
11364 #endif
11365 emit_insn (gen_macho_high (target, operands[1]));
11366 emit_insn (gen_macho_low (operands[0], target, operands[1]));
11367 return;
11370 emit_insn (gen_elf_high (target, operands[1]));
11371 emit_insn (gen_elf_low (operands[0], target, operands[1]));
11372 return;
11375 /* If this is a SYMBOL_REF that refers to a constant pool entry,
11376 and we have put it in the TOC, we just need to make a TOC-relative
11377 reference to it. */
11378 if (TARGET_TOC
11379 && GET_CODE (operands[1]) == SYMBOL_REF
11380 && use_toc_relative_ref (operands[1], mode))
11381 operands[1] = create_TOC_reference (operands[1], operands[0]);
11382 else if (mode == Pmode
11383 && CONSTANT_P (operands[1])
11384 && GET_CODE (operands[1]) != HIGH
11385 && ((GET_CODE (operands[1]) != CONST_INT
11386 && ! easy_fp_constant (operands[1], mode))
11387 || (GET_CODE (operands[1]) == CONST_INT
11388 && (num_insns_constant (operands[1], mode)
11389 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
11390 || (GET_CODE (operands[0]) == REG
11391 && FP_REGNO_P (REGNO (operands[0]))))
11392 && !toc_relative_expr_p (operands[1], false)
11393 && (TARGET_CMODEL == CMODEL_SMALL
11394 || can_create_pseudo_p ()
11395 || (REG_P (operands[0])
11396 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
11399 #if TARGET_MACHO
11400 /* Darwin uses a special PIC legitimizer. */
11401 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
11403 operands[1] =
11404 rs6000_machopic_legitimize_pic_address (operands[1], mode,
11405 operands[0]);
11406 if (operands[0] != operands[1])
11407 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11408 return;
11410 #endif
11412 /* If we are to limit the number of things we put in the TOC and
11413 this is a symbol plus a constant we can add in one insn,
11414 just put the symbol in the TOC and add the constant. Don't do
11415 this if reload is in progress. */
11416 if (GET_CODE (operands[1]) == CONST
11417 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
11418 && GET_CODE (XEXP (operands[1], 0)) == PLUS
11419 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
11420 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
11421 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
11422 && ! side_effects_p (operands[0]))
11424 rtx sym =
11425 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
11426 rtx other = XEXP (XEXP (operands[1], 0), 1);
11428 sym = force_reg (mode, sym);
11429 emit_insn (gen_add3_insn (operands[0], sym, other));
11430 return;
11433 operands[1] = force_const_mem (mode, operands[1]);
11435 if (TARGET_TOC
11436 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
11437 && use_toc_relative_ref (XEXP (operands[1], 0), mode))
11439 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
11440 operands[0]);
11441 operands[1] = gen_const_mem (mode, tocref);
11442 set_mem_alias_set (operands[1], get_TOC_alias_set ());
11445 break;
11447 case E_TImode:
11448 if (!VECTOR_MEM_VSX_P (TImode))
11449 rs6000_eliminate_indexed_memrefs (operands);
11450 break;
11452 case E_PTImode:
11453 rs6000_eliminate_indexed_memrefs (operands);
11454 break;
11456 default:
11457 fatal_insn ("bad move", gen_rtx_SET (dest, source));
11460 /* Above, we may have called force_const_mem which may have returned
11461 an invalid address. If we can, fix this up; otherwise, reload will
11462 have to deal with it. */
11463 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
11464 operands[1] = validize_mem (operands[1]);
11466 emit_set:
11467 emit_insn (gen_rtx_SET (operands[0], operands[1]));
11470 /* Return true if a structure, union or array containing FIELD should be
11471 accessed using `BLKMODE'.
11473 For the SPE, simd types are V2SI, and gcc can be tempted to put the
11474 entire thing in a DI and use subregs to access the internals.
11475 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
11476 back-end. Because a single GPR can hold a V2SI, but not a DI, the
11477 best thing to do is set structs to BLKmode and avoid Severe Tire
11478 Damage.
11480 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
11481 fit into 1, whereas DI still needs two. */
11483 static bool
11484 rs6000_member_type_forces_blk (const_tree field, machine_mode mode)
11486 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
11487 || (TARGET_E500_DOUBLE && mode == DFmode));
11490 /* Nonzero if we can use a floating-point register to pass this arg. */
11491 #define USE_FP_FOR_ARG_P(CUM,MODE) \
11492 (SCALAR_FLOAT_MODE_NOT_VECTOR_P (MODE) \
11493 && (CUM)->fregno <= FP_ARG_MAX_REG \
11494 && TARGET_HARD_FLOAT && TARGET_FPRS)
11496 /* Nonzero if we can use an AltiVec register to pass this arg. */
11497 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
11498 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
11499 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
11500 && TARGET_ALTIVEC_ABI \
11501 && (NAMED))
11503 /* Walk down the type tree of TYPE counting consecutive base elements.
11504 If *MODEP is VOIDmode, then set it to the first valid floating point
11505 or vector type. If a non-floating point or vector type is found, or
11506 if a floating point or vector type that doesn't match a non-VOIDmode
11507 *MODEP is found, then return -1, otherwise return the count in the
11508 sub-tree. */
11510 static int
11511 rs6000_aggregate_candidate (const_tree type, machine_mode *modep)
11513 machine_mode mode;
11514 HOST_WIDE_INT size;
11516 switch (TREE_CODE (type))
11518 case REAL_TYPE:
11519 mode = TYPE_MODE (type);
11520 if (!SCALAR_FLOAT_MODE_P (mode))
11521 return -1;
11523 if (*modep == VOIDmode)
11524 *modep = mode;
11526 if (*modep == mode)
11527 return 1;
11529 break;
11531 case COMPLEX_TYPE:
11532 mode = TYPE_MODE (TREE_TYPE (type));
11533 if (!SCALAR_FLOAT_MODE_P (mode))
11534 return -1;
11536 if (*modep == VOIDmode)
11537 *modep = mode;
11539 if (*modep == mode)
11540 return 2;
11542 break;
11544 case VECTOR_TYPE:
11545 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
11546 return -1;
11548 /* Use V4SImode as representative of all 128-bit vector types. */
11549 size = int_size_in_bytes (type);
11550 switch (size)
11552 case 16:
11553 mode = V4SImode;
11554 break;
11555 default:
11556 return -1;
11559 if (*modep == VOIDmode)
11560 *modep = mode;
11562 /* Vector modes are considered to be opaque: two vectors are
11563 equivalent for the purposes of being homogeneous aggregates
11564 if they are the same size. */
11565 if (*modep == mode)
11566 return 1;
11568 break;
11570 case ARRAY_TYPE:
11572 int count;
11573 tree index = TYPE_DOMAIN (type);
11575 /* Can't handle incomplete types nor sizes that are not
11576 fixed. */
11577 if (!COMPLETE_TYPE_P (type)
11578 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
11579 return -1;
11581 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
11582 if (count == -1
11583 || !index
11584 || !TYPE_MAX_VALUE (index)
11585 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
11586 || !TYPE_MIN_VALUE (index)
11587 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
11588 || count < 0)
11589 return -1;
11591 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
11592 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
11594 /* There must be no padding. */
11595 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
11596 return -1;
11598 return count;
11601 case RECORD_TYPE:
11603 int count = 0;
11604 int sub_count;
11605 tree field;
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 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
11615 if (TREE_CODE (field) != FIELD_DECL)
11616 continue;
11618 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
11619 if (sub_count < 0)
11620 return -1;
11621 count += sub_count;
11624 /* There must be no padding. */
11625 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
11626 return -1;
11628 return count;
11631 case UNION_TYPE:
11632 case QUAL_UNION_TYPE:
11634 /* These aren't very interesting except in a degenerate case. */
11635 int count = 0;
11636 int sub_count;
11637 tree field;
11639 /* Can't handle incomplete types nor sizes that are not
11640 fixed. */
11641 if (!COMPLETE_TYPE_P (type)
11642 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
11643 return -1;
11645 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
11647 if (TREE_CODE (field) != FIELD_DECL)
11648 continue;
11650 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
11651 if (sub_count < 0)
11652 return -1;
11653 count = count > sub_count ? count : sub_count;
11656 /* There must be no padding. */
11657 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
11658 return -1;
11660 return count;
11663 default:
11664 break;
11667 return -1;
11670 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
11671 float or vector aggregate that shall be passed in FP/vector registers
11672 according to the ELFv2 ABI, return the homogeneous element mode in
11673 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
11675 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
11677 static bool
11678 rs6000_discover_homogeneous_aggregate (machine_mode mode, const_tree type,
11679 machine_mode *elt_mode,
11680 int *n_elts)
11682 /* Note that we do not accept complex types at the top level as
11683 homogeneous aggregates; these types are handled via the
11684 targetm.calls.split_complex_arg mechanism. Complex types
11685 can be elements of homogeneous aggregates, however. */
11686 if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
11688 machine_mode field_mode = VOIDmode;
11689 int field_count = rs6000_aggregate_candidate (type, &field_mode);
11691 if (field_count > 0)
11693 int n_regs = (SCALAR_FLOAT_MODE_P (field_mode) ?
11694 (GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
11696 /* The ELFv2 ABI allows homogeneous aggregates to occupy
11697 up to AGGR_ARG_NUM_REG registers. */
11698 if (field_count * n_regs <= AGGR_ARG_NUM_REG)
11700 if (elt_mode)
11701 *elt_mode = field_mode;
11702 if (n_elts)
11703 *n_elts = field_count;
11704 return true;
11709 if (elt_mode)
11710 *elt_mode = mode;
11711 if (n_elts)
11712 *n_elts = 1;
11713 return false;
11716 /* Return a nonzero value to say to return the function value in
11717 memory, just as large structures are always returned. TYPE will be
11718 the data type of the value, and FNTYPE will be the type of the
11719 function doing the returning, or @code{NULL} for libcalls.
11721 The AIX ABI for the RS/6000 specifies that all structures are
11722 returned in memory. The Darwin ABI does the same.
11724 For the Darwin 64 Bit ABI, a function result can be returned in
11725 registers or in memory, depending on the size of the return data
11726 type. If it is returned in registers, the value occupies the same
11727 registers as it would if it were the first and only function
11728 argument. Otherwise, the function places its result in memory at
11729 the location pointed to by GPR3.
11731 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
11732 but a draft put them in memory, and GCC used to implement the draft
11733 instead of the final standard. Therefore, aix_struct_return
11734 controls this instead of DEFAULT_ABI; V.4 targets needing backward
11735 compatibility can change DRAFT_V4_STRUCT_RET to override the
11736 default, and -m switches get the final word. See
11737 rs6000_option_override_internal for more details.
11739 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
11740 long double support is enabled. These values are returned in memory.
11742 int_size_in_bytes returns -1 for variable size objects, which go in
11743 memory always. The cast to unsigned makes -1 > 8. */
11745 static bool
11746 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
11748 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
11749 if (TARGET_MACHO
11750 && rs6000_darwin64_abi
11751 && TREE_CODE (type) == RECORD_TYPE
11752 && int_size_in_bytes (type) > 0)
11754 CUMULATIVE_ARGS valcum;
11755 rtx valret;
11757 valcum.words = 0;
11758 valcum.fregno = FP_ARG_MIN_REG;
11759 valcum.vregno = ALTIVEC_ARG_MIN_REG;
11760 /* Do a trial code generation as if this were going to be passed
11761 as an argument; if any part goes in memory, we return NULL. */
11762 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
11763 if (valret)
11764 return false;
11765 /* Otherwise fall through to more conventional ABI rules. */
11768 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
11769 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
11770 NULL, NULL))
11771 return false;
11773 /* The ELFv2 ABI returns aggregates up to 16B in registers */
11774 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
11775 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
11776 return false;
11778 if (AGGREGATE_TYPE_P (type)
11779 && (aix_struct_return
11780 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
11781 return true;
11783 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
11784 modes only exist for GCC vector types if -maltivec. */
11785 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
11786 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11787 return false;
11789 /* Return synthetic vectors in memory. */
11790 if (TREE_CODE (type) == VECTOR_TYPE
11791 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
11793 static bool warned_for_return_big_vectors = false;
11794 if (!warned_for_return_big_vectors)
11796 warning (OPT_Wpsabi, "GCC vector returned by reference: "
11797 "non-standard ABI extension with no compatibility guarantee");
11798 warned_for_return_big_vectors = true;
11800 return true;
11803 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
11804 && FLOAT128_IEEE_P (TYPE_MODE (type)))
11805 return true;
11807 return false;
11810 /* Specify whether values returned in registers should be at the most
11811 significant end of a register. We want aggregates returned by
11812 value to match the way aggregates are passed to functions. */
11814 static bool
11815 rs6000_return_in_msb (const_tree valtype)
11817 return (DEFAULT_ABI == ABI_ELFv2
11818 && BYTES_BIG_ENDIAN
11819 && AGGREGATE_TYPE_P (valtype)
11820 && rs6000_function_arg_padding (TYPE_MODE (valtype),
11821 valtype) == PAD_UPWARD);
11824 #ifdef HAVE_AS_GNU_ATTRIBUTE
11825 /* Return TRUE if a call to function FNDECL may be one that
11826 potentially affects the function calling ABI of the object file. */
11828 static bool
11829 call_ABI_of_interest (tree fndecl)
11831 if (rs6000_gnu_attr && symtab->state == EXPANSION)
11833 struct cgraph_node *c_node;
11835 /* Libcalls are always interesting. */
11836 if (fndecl == NULL_TREE)
11837 return true;
11839 /* Any call to an external function is interesting. */
11840 if (DECL_EXTERNAL (fndecl))
11841 return true;
11843 /* Interesting functions that we are emitting in this object file. */
11844 c_node = cgraph_node::get (fndecl);
11845 c_node = c_node->ultimate_alias_target ();
11846 return !c_node->only_called_directly_p ();
11848 return false;
11850 #endif
11852 /* Initialize a variable CUM of type CUMULATIVE_ARGS
11853 for a call to a function whose data type is FNTYPE.
11854 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
11856 For incoming args we set the number of arguments in the prototype large
11857 so we never return a PARALLEL. */
11859 void
11860 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
11861 rtx libname ATTRIBUTE_UNUSED, int incoming,
11862 int libcall, int n_named_args,
11863 tree fndecl ATTRIBUTE_UNUSED,
11864 machine_mode return_mode ATTRIBUTE_UNUSED)
11866 static CUMULATIVE_ARGS zero_cumulative;
11868 *cum = zero_cumulative;
11869 cum->words = 0;
11870 cum->fregno = FP_ARG_MIN_REG;
11871 cum->vregno = ALTIVEC_ARG_MIN_REG;
11872 cum->prototype = (fntype && prototype_p (fntype));
11873 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
11874 ? CALL_LIBCALL : CALL_NORMAL);
11875 cum->sysv_gregno = GP_ARG_MIN_REG;
11876 cum->stdarg = stdarg_p (fntype);
11877 cum->libcall = libcall;
11879 cum->nargs_prototype = 0;
11880 if (incoming || cum->prototype)
11881 cum->nargs_prototype = n_named_args;
11883 /* Check for a longcall attribute. */
11884 if ((!fntype && rs6000_default_long_calls)
11885 || (fntype
11886 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
11887 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
11888 cum->call_cookie |= CALL_LONG;
11890 if (TARGET_DEBUG_ARG)
11892 fprintf (stderr, "\ninit_cumulative_args:");
11893 if (fntype)
11895 tree ret_type = TREE_TYPE (fntype);
11896 fprintf (stderr, " ret code = %s,",
11897 get_tree_code_name (TREE_CODE (ret_type)));
11900 if (cum->call_cookie & CALL_LONG)
11901 fprintf (stderr, " longcall,");
11903 fprintf (stderr, " proto = %d, nargs = %d\n",
11904 cum->prototype, cum->nargs_prototype);
11907 #ifdef HAVE_AS_GNU_ATTRIBUTE
11908 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4))
11910 cum->escapes = call_ABI_of_interest (fndecl);
11911 if (cum->escapes)
11913 tree return_type;
11915 if (fntype)
11917 return_type = TREE_TYPE (fntype);
11918 return_mode = TYPE_MODE (return_type);
11920 else
11921 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
11923 if (return_type != NULL)
11925 if (TREE_CODE (return_type) == RECORD_TYPE
11926 && TYPE_TRANSPARENT_AGGR (return_type))
11928 return_type = TREE_TYPE (first_field (return_type));
11929 return_mode = TYPE_MODE (return_type);
11931 if (AGGREGATE_TYPE_P (return_type)
11932 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
11933 <= 8))
11934 rs6000_returns_struct = true;
11936 if (SCALAR_FLOAT_MODE_P (return_mode))
11938 rs6000_passes_float = true;
11939 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
11940 && (FLOAT128_IBM_P (return_mode)
11941 || FLOAT128_IEEE_P (return_mode)
11942 || (return_type != NULL
11943 && (TYPE_MAIN_VARIANT (return_type)
11944 == long_double_type_node))))
11945 rs6000_passes_long_double = true;
11947 if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
11948 || SPE_VECTOR_MODE (return_mode))
11949 rs6000_passes_vector = true;
11952 #endif
11954 if (fntype
11955 && !TARGET_ALTIVEC
11956 && TARGET_ALTIVEC_ABI
11957 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
11959 error ("cannot return value in vector register because"
11960 " altivec instructions are disabled, use -maltivec"
11961 " to enable them");
11965 /* The mode the ABI uses for a word. This is not the same as word_mode
11966 for -m32 -mpowerpc64. This is used to implement various target hooks. */
11968 static scalar_int_mode
11969 rs6000_abi_word_mode (void)
11971 return TARGET_32BIT ? SImode : DImode;
11974 /* Implement the TARGET_OFFLOAD_OPTIONS hook. */
11975 static char *
11976 rs6000_offload_options (void)
11978 if (TARGET_64BIT)
11979 return xstrdup ("-foffload-abi=lp64");
11980 else
11981 return xstrdup ("-foffload-abi=ilp32");
11984 /* On rs6000, function arguments are promoted, as are function return
11985 values. */
11987 static machine_mode
11988 rs6000_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
11989 machine_mode mode,
11990 int *punsignedp ATTRIBUTE_UNUSED,
11991 const_tree, int)
11993 PROMOTE_MODE (mode, *punsignedp, type);
11995 return mode;
11998 /* Return true if TYPE must be passed on the stack and not in registers. */
12000 static bool
12001 rs6000_must_pass_in_stack (machine_mode mode, const_tree type)
12003 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
12004 return must_pass_in_stack_var_size (mode, type);
12005 else
12006 return must_pass_in_stack_var_size_or_pad (mode, type);
12009 static inline bool
12010 is_complex_IBM_long_double (machine_mode mode)
12012 return mode == ICmode || (!TARGET_IEEEQUAD && mode == TCmode);
12015 /* Whether ABI_V4 passes MODE args to a function in floating point
12016 registers. */
12018 static bool
12019 abi_v4_pass_in_fpr (machine_mode mode)
12021 if (!TARGET_FPRS || !TARGET_HARD_FLOAT)
12022 return false;
12023 if (TARGET_SINGLE_FLOAT && mode == SFmode)
12024 return true;
12025 if (TARGET_DOUBLE_FLOAT && mode == DFmode)
12026 return true;
12027 /* ABI_V4 passes complex IBM long double in 8 gprs.
12028 Stupid, but we can't change the ABI now. */
12029 if (is_complex_IBM_long_double (mode))
12030 return false;
12031 if (FLOAT128_2REG_P (mode))
12032 return true;
12033 if (DECIMAL_FLOAT_MODE_P (mode))
12034 return true;
12035 return false;
12038 /* Implement TARGET_FUNCTION_ARG_PADDING
12040 For the AIX ABI structs are always stored left shifted in their
12041 argument slot. */
12043 static pad_direction
12044 rs6000_function_arg_padding (machine_mode mode, const_tree type)
12046 #ifndef AGGREGATE_PADDING_FIXED
12047 #define AGGREGATE_PADDING_FIXED 0
12048 #endif
12049 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
12050 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
12051 #endif
12053 if (!AGGREGATE_PADDING_FIXED)
12055 /* GCC used to pass structures of the same size as integer types as
12056 if they were in fact integers, ignoring TARGET_FUNCTION_ARG_PADDING.
12057 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
12058 passed padded downward, except that -mstrict-align further
12059 muddied the water in that multi-component structures of 2 and 4
12060 bytes in size were passed padded upward.
12062 The following arranges for best compatibility with previous
12063 versions of gcc, but removes the -mstrict-align dependency. */
12064 if (BYTES_BIG_ENDIAN)
12066 HOST_WIDE_INT size = 0;
12068 if (mode == BLKmode)
12070 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12071 size = int_size_in_bytes (type);
12073 else
12074 size = GET_MODE_SIZE (mode);
12076 if (size == 1 || size == 2 || size == 4)
12077 return PAD_DOWNWARD;
12079 return PAD_UPWARD;
12082 if (AGGREGATES_PAD_UPWARD_ALWAYS)
12084 if (type != 0 && AGGREGATE_TYPE_P (type))
12085 return PAD_UPWARD;
12088 /* Fall back to the default. */
12089 return default_function_arg_padding (mode, type);
12092 /* If defined, a C expression that gives the alignment boundary, in bits,
12093 of an argument with the specified mode and type. If it is not defined,
12094 PARM_BOUNDARY is used for all arguments.
12096 V.4 wants long longs and doubles to be double word aligned. Just
12097 testing the mode size is a boneheaded way to do this as it means
12098 that other types such as complex int are also double word aligned.
12099 However, we're stuck with this because changing the ABI might break
12100 existing library interfaces.
12102 Doubleword align SPE vectors.
12103 Quadword align Altivec/VSX vectors.
12104 Quadword align large synthetic vector types. */
12106 static unsigned int
12107 rs6000_function_arg_boundary (machine_mode mode, const_tree type)
12109 machine_mode elt_mode;
12110 int n_elts;
12112 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
12114 if (DEFAULT_ABI == ABI_V4
12115 && (GET_MODE_SIZE (mode) == 8
12116 || (TARGET_HARD_FLOAT
12117 && TARGET_FPRS
12118 && !is_complex_IBM_long_double (mode)
12119 && FLOAT128_2REG_P (mode))))
12120 return 64;
12121 else if (FLOAT128_VECTOR_P (mode))
12122 return 128;
12123 else if (SPE_VECTOR_MODE (mode)
12124 || (type && TREE_CODE (type) == VECTOR_TYPE
12125 && int_size_in_bytes (type) >= 8
12126 && int_size_in_bytes (type) < 16))
12127 return 64;
12128 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
12129 || (type && TREE_CODE (type) == VECTOR_TYPE
12130 && int_size_in_bytes (type) >= 16))
12131 return 128;
12133 /* Aggregate types that need > 8 byte alignment are quadword-aligned
12134 in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
12135 -mcompat-align-parm is used. */
12136 if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
12137 || DEFAULT_ABI == ABI_ELFv2)
12138 && type && TYPE_ALIGN (type) > 64)
12140 /* "Aggregate" means any AGGREGATE_TYPE except for single-element
12141 or homogeneous float/vector aggregates here. We already handled
12142 vector aggregates above, but still need to check for float here. */
12143 bool aggregate_p = (AGGREGATE_TYPE_P (type)
12144 && !SCALAR_FLOAT_MODE_P (elt_mode));
12146 /* We used to check for BLKmode instead of the above aggregate type
12147 check. Warn when this results in any difference to the ABI. */
12148 if (aggregate_p != (mode == BLKmode))
12150 static bool warned;
12151 if (!warned && warn_psabi)
12153 warned = true;
12154 inform (input_location,
12155 "the ABI of passing aggregates with %d-byte alignment"
12156 " has changed in GCC 5",
12157 (int) TYPE_ALIGN (type) / BITS_PER_UNIT);
12161 if (aggregate_p)
12162 return 128;
12165 /* Similar for the Darwin64 ABI. Note that for historical reasons we
12166 implement the "aggregate type" check as a BLKmode check here; this
12167 means certain aggregate types are in fact not aligned. */
12168 if (TARGET_MACHO && rs6000_darwin64_abi
12169 && mode == BLKmode
12170 && type && TYPE_ALIGN (type) > 64)
12171 return 128;
12173 return PARM_BOUNDARY;
12176 /* The offset in words to the start of the parameter save area. */
12178 static unsigned int
12179 rs6000_parm_offset (void)
12181 return (DEFAULT_ABI == ABI_V4 ? 2
12182 : DEFAULT_ABI == ABI_ELFv2 ? 4
12183 : 6);
12186 /* For a function parm of MODE and TYPE, return the starting word in
12187 the parameter area. NWORDS of the parameter area are already used. */
12189 static unsigned int
12190 rs6000_parm_start (machine_mode mode, const_tree type,
12191 unsigned int nwords)
12193 unsigned int align;
12195 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
12196 return nwords + (-(rs6000_parm_offset () + nwords) & align);
12199 /* Compute the size (in words) of a function argument. */
12201 static unsigned long
12202 rs6000_arg_size (machine_mode mode, const_tree type)
12204 unsigned long size;
12206 if (mode != BLKmode)
12207 size = GET_MODE_SIZE (mode);
12208 else
12209 size = int_size_in_bytes (type);
12211 if (TARGET_32BIT)
12212 return (size + 3) >> 2;
12213 else
12214 return (size + 7) >> 3;
12217 /* Use this to flush pending int fields. */
12219 static void
12220 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
12221 HOST_WIDE_INT bitpos, int final)
12223 unsigned int startbit, endbit;
12224 int intregs, intoffset;
12226 /* Handle the situations where a float is taking up the first half
12227 of the GPR, and the other half is empty (typically due to
12228 alignment restrictions). We can detect this by a 8-byte-aligned
12229 int field, or by seeing that this is the final flush for this
12230 argument. Count the word and continue on. */
12231 if (cum->floats_in_gpr == 1
12232 && (cum->intoffset % 64 == 0
12233 || (cum->intoffset == -1 && final)))
12235 cum->words++;
12236 cum->floats_in_gpr = 0;
12239 if (cum->intoffset == -1)
12240 return;
12242 intoffset = cum->intoffset;
12243 cum->intoffset = -1;
12244 cum->floats_in_gpr = 0;
12246 if (intoffset % BITS_PER_WORD != 0)
12248 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
12249 if (!int_mode_for_size (bits, 0).exists ())
12251 /* We couldn't find an appropriate mode, which happens,
12252 e.g., in packed structs when there are 3 bytes to load.
12253 Back intoffset back to the beginning of the word in this
12254 case. */
12255 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
12259 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
12260 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
12261 intregs = (endbit - startbit) / BITS_PER_WORD;
12262 cum->words += intregs;
12263 /* words should be unsigned. */
12264 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
12266 int pad = (endbit/BITS_PER_WORD) - cum->words;
12267 cum->words += pad;
12271 /* The darwin64 ABI calls for us to recurse down through structs,
12272 looking for elements passed in registers. Unfortunately, we have
12273 to track int register count here also because of misalignments
12274 in powerpc alignment mode. */
12276 static void
12277 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
12278 const_tree type,
12279 HOST_WIDE_INT startbitpos)
12281 tree f;
12283 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
12284 if (TREE_CODE (f) == FIELD_DECL)
12286 HOST_WIDE_INT bitpos = startbitpos;
12287 tree ftype = TREE_TYPE (f);
12288 machine_mode mode;
12289 if (ftype == error_mark_node)
12290 continue;
12291 mode = TYPE_MODE (ftype);
12293 if (DECL_SIZE (f) != 0
12294 && tree_fits_uhwi_p (bit_position (f)))
12295 bitpos += int_bit_position (f);
12297 /* ??? FIXME: else assume zero offset. */
12299 if (TREE_CODE (ftype) == RECORD_TYPE)
12300 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
12301 else if (USE_FP_FOR_ARG_P (cum, mode))
12303 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
12304 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
12305 cum->fregno += n_fpregs;
12306 /* Single-precision floats present a special problem for
12307 us, because they are smaller than an 8-byte GPR, and so
12308 the structure-packing rules combined with the standard
12309 varargs behavior mean that we want to pack float/float
12310 and float/int combinations into a single register's
12311 space. This is complicated by the arg advance flushing,
12312 which works on arbitrarily large groups of int-type
12313 fields. */
12314 if (mode == SFmode)
12316 if (cum->floats_in_gpr == 1)
12318 /* Two floats in a word; count the word and reset
12319 the float count. */
12320 cum->words++;
12321 cum->floats_in_gpr = 0;
12323 else if (bitpos % 64 == 0)
12325 /* A float at the beginning of an 8-byte word;
12326 count it and put off adjusting cum->words until
12327 we see if a arg advance flush is going to do it
12328 for us. */
12329 cum->floats_in_gpr++;
12331 else
12333 /* The float is at the end of a word, preceded
12334 by integer fields, so the arg advance flush
12335 just above has already set cum->words and
12336 everything is taken care of. */
12339 else
12340 cum->words += n_fpregs;
12342 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
12344 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
12345 cum->vregno++;
12346 cum->words += 2;
12348 else if (cum->intoffset == -1)
12349 cum->intoffset = bitpos;
12353 /* Check for an item that needs to be considered specially under the darwin 64
12354 bit ABI. These are record types where the mode is BLK or the structure is
12355 8 bytes in size. */
12356 static int
12357 rs6000_darwin64_struct_check_p (machine_mode mode, const_tree type)
12359 return rs6000_darwin64_abi
12360 && ((mode == BLKmode
12361 && TREE_CODE (type) == RECORD_TYPE
12362 && int_size_in_bytes (type) > 0)
12363 || (type && TREE_CODE (type) == RECORD_TYPE
12364 && int_size_in_bytes (type) == 8)) ? 1 : 0;
12367 /* Update the data in CUM to advance over an argument
12368 of mode MODE and data type TYPE.
12369 (TYPE is null for libcalls where that information may not be available.)
12371 Note that for args passed by reference, function_arg will be called
12372 with MODE and TYPE set to that of the pointer to the arg, not the arg
12373 itself. */
12375 static void
12376 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, machine_mode mode,
12377 const_tree type, bool named, int depth)
12379 machine_mode elt_mode;
12380 int n_elts;
12382 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
12384 /* Only tick off an argument if we're not recursing. */
12385 if (depth == 0)
12386 cum->nargs_prototype--;
12388 #ifdef HAVE_AS_GNU_ATTRIBUTE
12389 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4)
12390 && cum->escapes)
12392 if (SCALAR_FLOAT_MODE_P (mode))
12394 rs6000_passes_float = true;
12395 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
12396 && (FLOAT128_IBM_P (mode)
12397 || FLOAT128_IEEE_P (mode)
12398 || (type != NULL
12399 && TYPE_MAIN_VARIANT (type) == long_double_type_node)))
12400 rs6000_passes_long_double = true;
12402 if ((named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
12403 || (SPE_VECTOR_MODE (mode)
12404 && !cum->stdarg
12405 && cum->sysv_gregno <= GP_ARG_MAX_REG))
12406 rs6000_passes_vector = true;
12408 #endif
12410 if (TARGET_ALTIVEC_ABI
12411 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
12412 || (type && TREE_CODE (type) == VECTOR_TYPE
12413 && int_size_in_bytes (type) == 16)))
12415 bool stack = false;
12417 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
12419 cum->vregno += n_elts;
12421 if (!TARGET_ALTIVEC)
12422 error ("cannot pass argument in vector register because"
12423 " altivec instructions are disabled, use -maltivec"
12424 " to enable them");
12426 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
12427 even if it is going to be passed in a vector register.
12428 Darwin does the same for variable-argument functions. */
12429 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
12430 && TARGET_64BIT)
12431 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
12432 stack = true;
12434 else
12435 stack = true;
12437 if (stack)
12439 int align;
12441 /* Vector parameters must be 16-byte aligned. In 32-bit
12442 mode this means we need to take into account the offset
12443 to the parameter save area. In 64-bit mode, they just
12444 have to start on an even word, since the parameter save
12445 area is 16-byte aligned. */
12446 if (TARGET_32BIT)
12447 align = -(rs6000_parm_offset () + cum->words) & 3;
12448 else
12449 align = cum->words & 1;
12450 cum->words += align + rs6000_arg_size (mode, type);
12452 if (TARGET_DEBUG_ARG)
12454 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
12455 cum->words, align);
12456 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
12457 cum->nargs_prototype, cum->prototype,
12458 GET_MODE_NAME (mode));
12462 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
12463 && !cum->stdarg
12464 && cum->sysv_gregno <= GP_ARG_MAX_REG)
12465 cum->sysv_gregno++;
12467 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
12469 int size = int_size_in_bytes (type);
12470 /* Variable sized types have size == -1 and are
12471 treated as if consisting entirely of ints.
12472 Pad to 16 byte boundary if needed. */
12473 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
12474 && (cum->words % 2) != 0)
12475 cum->words++;
12476 /* For varargs, we can just go up by the size of the struct. */
12477 if (!named)
12478 cum->words += (size + 7) / 8;
12479 else
12481 /* It is tempting to say int register count just goes up by
12482 sizeof(type)/8, but this is wrong in a case such as
12483 { int; double; int; } [powerpc alignment]. We have to
12484 grovel through the fields for these too. */
12485 cum->intoffset = 0;
12486 cum->floats_in_gpr = 0;
12487 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
12488 rs6000_darwin64_record_arg_advance_flush (cum,
12489 size * BITS_PER_UNIT, 1);
12491 if (TARGET_DEBUG_ARG)
12493 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
12494 cum->words, TYPE_ALIGN (type), size);
12495 fprintf (stderr,
12496 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
12497 cum->nargs_prototype, cum->prototype,
12498 GET_MODE_NAME (mode));
12501 else if (DEFAULT_ABI == ABI_V4)
12503 if (abi_v4_pass_in_fpr (mode))
12505 /* _Decimal128 must use an even/odd register pair. This assumes
12506 that the register number is odd when fregno is odd. */
12507 if (mode == TDmode && (cum->fregno % 2) == 1)
12508 cum->fregno++;
12510 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
12511 <= FP_ARG_V4_MAX_REG)
12512 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
12513 else
12515 cum->fregno = FP_ARG_V4_MAX_REG + 1;
12516 if (mode == DFmode || FLOAT128_IBM_P (mode)
12517 || mode == DDmode || mode == TDmode)
12518 cum->words += cum->words & 1;
12519 cum->words += rs6000_arg_size (mode, type);
12522 else
12524 int n_words = rs6000_arg_size (mode, type);
12525 int gregno = cum->sysv_gregno;
12527 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
12528 (r7,r8) or (r9,r10). As does any other 2 word item such
12529 as complex int due to a historical mistake. */
12530 if (n_words == 2)
12531 gregno += (1 - gregno) & 1;
12533 /* Multi-reg args are not split between registers and stack. */
12534 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
12536 /* Long long and SPE vectors are aligned on the stack.
12537 So are other 2 word items such as complex int due to
12538 a historical mistake. */
12539 if (n_words == 2)
12540 cum->words += cum->words & 1;
12541 cum->words += n_words;
12544 /* Note: continuing to accumulate gregno past when we've started
12545 spilling to the stack indicates the fact that we've started
12546 spilling to the stack to expand_builtin_saveregs. */
12547 cum->sysv_gregno = gregno + n_words;
12550 if (TARGET_DEBUG_ARG)
12552 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
12553 cum->words, cum->fregno);
12554 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
12555 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
12556 fprintf (stderr, "mode = %4s, named = %d\n",
12557 GET_MODE_NAME (mode), named);
12560 else
12562 int n_words = rs6000_arg_size (mode, type);
12563 int start_words = cum->words;
12564 int align_words = rs6000_parm_start (mode, type, start_words);
12566 cum->words = align_words + n_words;
12568 if (SCALAR_FLOAT_MODE_P (elt_mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
12570 /* _Decimal128 must be passed in an even/odd float register pair.
12571 This assumes that the register number is odd when fregno is
12572 odd. */
12573 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
12574 cum->fregno++;
12575 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
12578 if (TARGET_DEBUG_ARG)
12580 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
12581 cum->words, cum->fregno);
12582 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
12583 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
12584 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
12585 named, align_words - start_words, depth);
12590 static void
12591 rs6000_function_arg_advance (cumulative_args_t cum, machine_mode mode,
12592 const_tree type, bool named)
12594 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
12598 static rtx
12599 spe_build_register_parallel (machine_mode mode, int gregno)
12601 rtx r1, r3, r5, r7;
12603 switch (mode)
12605 case E_DFmode:
12606 r1 = gen_rtx_REG (DImode, gregno);
12607 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
12608 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
12610 case E_DCmode:
12611 case E_TFmode:
12612 r1 = gen_rtx_REG (DImode, gregno);
12613 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
12614 r3 = gen_rtx_REG (DImode, gregno + 2);
12615 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
12616 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
12618 case E_TCmode:
12619 r1 = gen_rtx_REG (DImode, gregno);
12620 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
12621 r3 = gen_rtx_REG (DImode, gregno + 2);
12622 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
12623 r5 = gen_rtx_REG (DImode, gregno + 4);
12624 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
12625 r7 = gen_rtx_REG (DImode, gregno + 6);
12626 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
12627 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
12629 default:
12630 gcc_unreachable ();
12634 /* Determine where to put a SIMD argument on the SPE. */
12635 static rtx
12636 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, machine_mode mode,
12637 const_tree type)
12639 int gregno = cum->sysv_gregno;
12641 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
12642 are passed and returned in a pair of GPRs for ABI compatibility. */
12643 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
12644 || mode == DCmode || mode == TCmode))
12646 int n_words = rs6000_arg_size (mode, type);
12648 /* Doubles go in an odd/even register pair (r5/r6, etc). */
12649 if (mode == DFmode)
12650 gregno += (1 - gregno) & 1;
12652 /* Multi-reg args are not split between registers and stack. */
12653 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
12654 return NULL_RTX;
12656 return spe_build_register_parallel (mode, gregno);
12658 if (cum->stdarg)
12660 int n_words = rs6000_arg_size (mode, type);
12662 /* SPE vectors are put in odd registers. */
12663 if (n_words == 2 && (gregno & 1) == 0)
12664 gregno += 1;
12666 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
12668 rtx r1, r2;
12669 machine_mode m = SImode;
12671 r1 = gen_rtx_REG (m, gregno);
12672 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
12673 r2 = gen_rtx_REG (m, gregno + 1);
12674 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
12675 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
12677 else
12678 return NULL_RTX;
12680 else
12682 if (gregno <= GP_ARG_MAX_REG)
12683 return gen_rtx_REG (mode, gregno);
12684 else
12685 return NULL_RTX;
12689 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
12690 structure between cum->intoffset and bitpos to integer registers. */
12692 static void
12693 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
12694 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
12696 machine_mode mode;
12697 unsigned int regno;
12698 unsigned int startbit, endbit;
12699 int this_regno, intregs, intoffset;
12700 rtx reg;
12702 if (cum->intoffset == -1)
12703 return;
12705 intoffset = cum->intoffset;
12706 cum->intoffset = -1;
12708 /* If this is the trailing part of a word, try to only load that
12709 much into the register. Otherwise load the whole register. Note
12710 that in the latter case we may pick up unwanted bits. It's not a
12711 problem at the moment but may wish to revisit. */
12713 if (intoffset % BITS_PER_WORD != 0)
12715 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
12716 if (!int_mode_for_size (bits, 0).exists (&mode))
12718 /* We couldn't find an appropriate mode, which happens,
12719 e.g., in packed structs when there are 3 bytes to load.
12720 Back intoffset back to the beginning of the word in this
12721 case. */
12722 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
12723 mode = word_mode;
12726 else
12727 mode = word_mode;
12729 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
12730 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
12731 intregs = (endbit - startbit) / BITS_PER_WORD;
12732 this_regno = cum->words + intoffset / BITS_PER_WORD;
12734 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
12735 cum->use_stack = 1;
12737 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
12738 if (intregs <= 0)
12739 return;
12741 intoffset /= BITS_PER_UNIT;
12744 regno = GP_ARG_MIN_REG + this_regno;
12745 reg = gen_rtx_REG (mode, regno);
12746 rvec[(*k)++] =
12747 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
12749 this_regno += 1;
12750 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
12751 mode = word_mode;
12752 intregs -= 1;
12754 while (intregs > 0);
12757 /* Recursive workhorse for the following. */
12759 static void
12760 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
12761 HOST_WIDE_INT startbitpos, rtx rvec[],
12762 int *k)
12764 tree f;
12766 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
12767 if (TREE_CODE (f) == FIELD_DECL)
12769 HOST_WIDE_INT bitpos = startbitpos;
12770 tree ftype = TREE_TYPE (f);
12771 machine_mode mode;
12772 if (ftype == error_mark_node)
12773 continue;
12774 mode = TYPE_MODE (ftype);
12776 if (DECL_SIZE (f) != 0
12777 && tree_fits_uhwi_p (bit_position (f)))
12778 bitpos += int_bit_position (f);
12780 /* ??? FIXME: else assume zero offset. */
12782 if (TREE_CODE (ftype) == RECORD_TYPE)
12783 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
12784 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
12786 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
12787 #if 0
12788 switch (mode)
12790 case E_SCmode: mode = SFmode; break;
12791 case E_DCmode: mode = DFmode; break;
12792 case E_TCmode: mode = TFmode; break;
12793 default: break;
12795 #endif
12796 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
12797 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
12799 gcc_assert (cum->fregno == FP_ARG_MAX_REG
12800 && (mode == TFmode || mode == TDmode));
12801 /* Long double or _Decimal128 split over regs and memory. */
12802 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
12803 cum->use_stack=1;
12805 rvec[(*k)++]
12806 = gen_rtx_EXPR_LIST (VOIDmode,
12807 gen_rtx_REG (mode, cum->fregno++),
12808 GEN_INT (bitpos / BITS_PER_UNIT));
12809 if (FLOAT128_2REG_P (mode))
12810 cum->fregno++;
12812 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
12814 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
12815 rvec[(*k)++]
12816 = gen_rtx_EXPR_LIST (VOIDmode,
12817 gen_rtx_REG (mode, cum->vregno++),
12818 GEN_INT (bitpos / BITS_PER_UNIT));
12820 else if (cum->intoffset == -1)
12821 cum->intoffset = bitpos;
12825 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
12826 the register(s) to be used for each field and subfield of a struct
12827 being passed by value, along with the offset of where the
12828 register's value may be found in the block. FP fields go in FP
12829 register, vector fields go in vector registers, and everything
12830 else goes in int registers, packed as in memory.
12832 This code is also used for function return values. RETVAL indicates
12833 whether this is the case.
12835 Much of this is taken from the SPARC V9 port, which has a similar
12836 calling convention. */
12838 static rtx
12839 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
12840 bool named, bool retval)
12842 rtx rvec[FIRST_PSEUDO_REGISTER];
12843 int k = 1, kbase = 1;
12844 HOST_WIDE_INT typesize = int_size_in_bytes (type);
12845 /* This is a copy; modifications are not visible to our caller. */
12846 CUMULATIVE_ARGS copy_cum = *orig_cum;
12847 CUMULATIVE_ARGS *cum = &copy_cum;
12849 /* Pad to 16 byte boundary if needed. */
12850 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
12851 && (cum->words % 2) != 0)
12852 cum->words++;
12854 cum->intoffset = 0;
12855 cum->use_stack = 0;
12856 cum->named = named;
12858 /* Put entries into rvec[] for individual FP and vector fields, and
12859 for the chunks of memory that go in int regs. Note we start at
12860 element 1; 0 is reserved for an indication of using memory, and
12861 may or may not be filled in below. */
12862 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
12863 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
12865 /* If any part of the struct went on the stack put all of it there.
12866 This hack is because the generic code for
12867 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
12868 parts of the struct are not at the beginning. */
12869 if (cum->use_stack)
12871 if (retval)
12872 return NULL_RTX; /* doesn't go in registers at all */
12873 kbase = 0;
12874 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12876 if (k > 1 || cum->use_stack)
12877 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
12878 else
12879 return NULL_RTX;
12882 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
12884 static rtx
12885 rs6000_mixed_function_arg (machine_mode mode, const_tree type,
12886 int align_words)
12888 int n_units;
12889 int i, k;
12890 rtx rvec[GP_ARG_NUM_REG + 1];
12892 if (align_words >= GP_ARG_NUM_REG)
12893 return NULL_RTX;
12895 n_units = rs6000_arg_size (mode, type);
12897 /* Optimize the simple case where the arg fits in one gpr, except in
12898 the case of BLKmode due to assign_parms assuming that registers are
12899 BITS_PER_WORD wide. */
12900 if (n_units == 0
12901 || (n_units == 1 && mode != BLKmode))
12902 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
12904 k = 0;
12905 if (align_words + n_units > GP_ARG_NUM_REG)
12906 /* Not all of the arg fits in gprs. Say that it goes in memory too,
12907 using a magic NULL_RTX component.
12908 This is not strictly correct. Only some of the arg belongs in
12909 memory, not all of it. However, the normal scheme using
12910 function_arg_partial_nregs can result in unusual subregs, eg.
12911 (subreg:SI (reg:DF) 4), which are not handled well. The code to
12912 store the whole arg to memory is often more efficient than code
12913 to store pieces, and we know that space is available in the right
12914 place for the whole arg. */
12915 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12917 i = 0;
12920 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
12921 rtx off = GEN_INT (i++ * 4);
12922 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12924 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
12926 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
12929 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
12930 but must also be copied into the parameter save area starting at
12931 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
12932 to the GPRs and/or memory. Return the number of elements used. */
12934 static int
12935 rs6000_psave_function_arg (machine_mode mode, const_tree type,
12936 int align_words, rtx *rvec)
12938 int k = 0;
12940 if (align_words < GP_ARG_NUM_REG)
12942 int n_words = rs6000_arg_size (mode, type);
12944 if (align_words + n_words > GP_ARG_NUM_REG
12945 || mode == BLKmode
12946 || (TARGET_32BIT && TARGET_POWERPC64))
12948 /* If this is partially on the stack, then we only
12949 include the portion actually in registers here. */
12950 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
12951 int i = 0;
12953 if (align_words + n_words > GP_ARG_NUM_REG)
12955 /* Not all of the arg fits in gprs. Say that it goes in memory
12956 too, using a magic NULL_RTX component. Also see comment in
12957 rs6000_mixed_function_arg for why the normal
12958 function_arg_partial_nregs scheme doesn't work in this case. */
12959 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12964 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
12965 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
12966 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12968 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
12970 else
12972 /* The whole arg fits in gprs. */
12973 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
12974 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
12977 else
12979 /* It's entirely in memory. */
12980 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12983 return k;
12986 /* RVEC is a vector of K components of an argument of mode MODE.
12987 Construct the final function_arg return value from it. */
12989 static rtx
12990 rs6000_finish_function_arg (machine_mode mode, rtx *rvec, int k)
12992 gcc_assert (k >= 1);
12994 /* Avoid returning a PARALLEL in the trivial cases. */
12995 if (k == 1)
12997 if (XEXP (rvec[0], 0) == NULL_RTX)
12998 return NULL_RTX;
13000 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
13001 return XEXP (rvec[0], 0);
13004 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
13007 /* Determine where to put an argument to a function.
13008 Value is zero to push the argument on the stack,
13009 or a hard register in which to store the argument.
13011 MODE is the argument's machine mode.
13012 TYPE is the data type of the argument (as a tree).
13013 This is null for libcalls where that information may
13014 not be available.
13015 CUM is a variable of type CUMULATIVE_ARGS which gives info about
13016 the preceding args and about the function being called. It is
13017 not modified in this routine.
13018 NAMED is nonzero if this argument is a named parameter
13019 (otherwise it is an extra parameter matching an ellipsis).
13021 On RS/6000 the first eight words of non-FP are normally in registers
13022 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
13023 Under V.4, the first 8 FP args are in registers.
13025 If this is floating-point and no prototype is specified, we use
13026 both an FP and integer register (or possibly FP reg and stack). Library
13027 functions (when CALL_LIBCALL is set) always have the proper types for args,
13028 so we can pass the FP value just in one register. emit_library_function
13029 doesn't support PARALLEL anyway.
13031 Note that for args passed by reference, function_arg will be called
13032 with MODE and TYPE set to that of the pointer to the arg, not the arg
13033 itself. */
13035 static rtx
13036 rs6000_function_arg (cumulative_args_t cum_v, machine_mode mode,
13037 const_tree type, bool named)
13039 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
13040 enum rs6000_abi abi = DEFAULT_ABI;
13041 machine_mode elt_mode;
13042 int n_elts;
13044 /* Return a marker to indicate whether CR1 needs to set or clear the
13045 bit that V.4 uses to say fp args were passed in registers.
13046 Assume that we don't need the marker for software floating point,
13047 or compiler generated library calls. */
13048 if (mode == VOIDmode)
13050 if (abi == ABI_V4
13051 && (cum->call_cookie & CALL_LIBCALL) == 0
13052 && (cum->stdarg
13053 || (cum->nargs_prototype < 0
13054 && (cum->prototype || TARGET_NO_PROTOTYPE))))
13056 /* For the SPE, we need to crxor CR6 always. */
13057 if (TARGET_SPE_ABI)
13058 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
13059 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
13060 return GEN_INT (cum->call_cookie
13061 | ((cum->fregno == FP_ARG_MIN_REG)
13062 ? CALL_V4_SET_FP_ARGS
13063 : CALL_V4_CLEAR_FP_ARGS));
13066 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
13069 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
13071 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
13073 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
13074 if (rslt != NULL_RTX)
13075 return rslt;
13076 /* Else fall through to usual handling. */
13079 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
13081 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
13082 rtx r, off;
13083 int i, k = 0;
13085 /* Do we also need to pass this argument in the parameter save area?
13086 Library support functions for IEEE 128-bit are assumed to not need the
13087 value passed both in GPRs and in vector registers. */
13088 if (TARGET_64BIT && !cum->prototype
13089 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
13091 int align_words = ROUND_UP (cum->words, 2);
13092 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
13095 /* Describe where this argument goes in the vector registers. */
13096 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
13098 r = gen_rtx_REG (elt_mode, cum->vregno + i);
13099 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
13100 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13103 return rs6000_finish_function_arg (mode, rvec, k);
13105 else if (TARGET_ALTIVEC_ABI
13106 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
13107 || (type && TREE_CODE (type) == VECTOR_TYPE
13108 && int_size_in_bytes (type) == 16)))
13110 if (named || abi == ABI_V4)
13111 return NULL_RTX;
13112 else
13114 /* Vector parameters to varargs functions under AIX or Darwin
13115 get passed in memory and possibly also in GPRs. */
13116 int align, align_words, n_words;
13117 machine_mode part_mode;
13119 /* Vector parameters must be 16-byte aligned. In 32-bit
13120 mode this means we need to take into account the offset
13121 to the parameter save area. In 64-bit mode, they just
13122 have to start on an even word, since the parameter save
13123 area is 16-byte aligned. */
13124 if (TARGET_32BIT)
13125 align = -(rs6000_parm_offset () + cum->words) & 3;
13126 else
13127 align = cum->words & 1;
13128 align_words = cum->words + align;
13130 /* Out of registers? Memory, then. */
13131 if (align_words >= GP_ARG_NUM_REG)
13132 return NULL_RTX;
13134 if (TARGET_32BIT && TARGET_POWERPC64)
13135 return rs6000_mixed_function_arg (mode, type, align_words);
13137 /* The vector value goes in GPRs. Only the part of the
13138 value in GPRs is reported here. */
13139 part_mode = mode;
13140 n_words = rs6000_arg_size (mode, type);
13141 if (align_words + n_words > GP_ARG_NUM_REG)
13142 /* Fortunately, there are only two possibilities, the value
13143 is either wholly in GPRs or half in GPRs and half not. */
13144 part_mode = DImode;
13146 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
13149 else if (TARGET_SPE_ABI && TARGET_SPE
13150 && (SPE_VECTOR_MODE (mode)
13151 || (TARGET_E500_DOUBLE && (mode == DFmode
13152 || mode == DCmode
13153 || mode == TFmode
13154 || mode == TCmode))))
13155 return rs6000_spe_function_arg (cum, mode, type);
13157 else if (abi == ABI_V4)
13159 if (abi_v4_pass_in_fpr (mode))
13161 /* _Decimal128 must use an even/odd register pair. This assumes
13162 that the register number is odd when fregno is odd. */
13163 if (mode == TDmode && (cum->fregno % 2) == 1)
13164 cum->fregno++;
13166 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
13167 <= FP_ARG_V4_MAX_REG)
13168 return gen_rtx_REG (mode, cum->fregno);
13169 else
13170 return NULL_RTX;
13172 else
13174 int n_words = rs6000_arg_size (mode, type);
13175 int gregno = cum->sysv_gregno;
13177 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
13178 (r7,r8) or (r9,r10). As does any other 2 word item such
13179 as complex int due to a historical mistake. */
13180 if (n_words == 2)
13181 gregno += (1 - gregno) & 1;
13183 /* Multi-reg args are not split between registers and stack. */
13184 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
13185 return NULL_RTX;
13187 if (TARGET_32BIT && TARGET_POWERPC64)
13188 return rs6000_mixed_function_arg (mode, type,
13189 gregno - GP_ARG_MIN_REG);
13190 return gen_rtx_REG (mode, gregno);
13193 else
13195 int align_words = rs6000_parm_start (mode, type, cum->words);
13197 /* _Decimal128 must be passed in an even/odd float register pair.
13198 This assumes that the register number is odd when fregno is odd. */
13199 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
13200 cum->fregno++;
13202 if (USE_FP_FOR_ARG_P (cum, elt_mode))
13204 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
13205 rtx r, off;
13206 int i, k = 0;
13207 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
13208 int fpr_words;
13210 /* Do we also need to pass this argument in the parameter
13211 save area? */
13212 if (type && (cum->nargs_prototype <= 0
13213 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
13214 && TARGET_XL_COMPAT
13215 && align_words >= GP_ARG_NUM_REG)))
13216 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
13218 /* Describe where this argument goes in the fprs. */
13219 for (i = 0; i < n_elts
13220 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
13222 /* Check if the argument is split over registers and memory.
13223 This can only ever happen for long double or _Decimal128;
13224 complex types are handled via split_complex_arg. */
13225 machine_mode fmode = elt_mode;
13226 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
13228 gcc_assert (FLOAT128_2REG_P (fmode));
13229 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
13232 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
13233 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
13234 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13237 /* If there were not enough FPRs to hold the argument, the rest
13238 usually goes into memory. However, if the current position
13239 is still within the register parameter area, a portion may
13240 actually have to go into GPRs.
13242 Note that it may happen that the portion of the argument
13243 passed in the first "half" of the first GPR was already
13244 passed in the last FPR as well.
13246 For unnamed arguments, we already set up GPRs to cover the
13247 whole argument in rs6000_psave_function_arg, so there is
13248 nothing further to do at this point. */
13249 fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
13250 if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
13251 && cum->nargs_prototype > 0)
13253 static bool warned;
13255 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
13256 int n_words = rs6000_arg_size (mode, type);
13258 align_words += fpr_words;
13259 n_words -= fpr_words;
13263 r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
13264 off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
13265 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
13267 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
13269 if (!warned && warn_psabi)
13271 warned = true;
13272 inform (input_location,
13273 "the ABI of passing homogeneous float aggregates"
13274 " has changed in GCC 5");
13278 return rs6000_finish_function_arg (mode, rvec, k);
13280 else if (align_words < GP_ARG_NUM_REG)
13282 if (TARGET_32BIT && TARGET_POWERPC64)
13283 return rs6000_mixed_function_arg (mode, type, align_words);
13285 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
13287 else
13288 return NULL_RTX;
13292 /* For an arg passed partly in registers and partly in memory, this is
13293 the number of bytes passed in registers. For args passed entirely in
13294 registers or entirely in memory, zero. When an arg is described by a
13295 PARALLEL, perhaps using more than one register type, this function
13296 returns the number of bytes used by the first element of the PARALLEL. */
13298 static int
13299 rs6000_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
13300 tree type, bool named)
13302 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
13303 bool passed_in_gprs = true;
13304 int ret = 0;
13305 int align_words;
13306 machine_mode elt_mode;
13307 int n_elts;
13309 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
13311 if (DEFAULT_ABI == ABI_V4)
13312 return 0;
13314 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
13316 /* If we are passing this arg in the fixed parameter save area (gprs or
13317 memory) as well as VRs, we do not use the partial bytes mechanism;
13318 instead, rs6000_function_arg will return a PARALLEL including a memory
13319 element as necessary. Library support functions for IEEE 128-bit are
13320 assumed to not need the value passed both in GPRs and in vector
13321 registers. */
13322 if (TARGET_64BIT && !cum->prototype
13323 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
13324 return 0;
13326 /* Otherwise, we pass in VRs only. Check for partial copies. */
13327 passed_in_gprs = false;
13328 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
13329 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
13332 /* In this complicated case we just disable the partial_nregs code. */
13333 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
13334 return 0;
13336 align_words = rs6000_parm_start (mode, type, cum->words);
13338 if (USE_FP_FOR_ARG_P (cum, elt_mode))
13340 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
13342 /* If we are passing this arg in the fixed parameter save area
13343 (gprs or memory) as well as FPRs, we do not use the partial
13344 bytes mechanism; instead, rs6000_function_arg will return a
13345 PARALLEL including a memory element as necessary. */
13346 if (type
13347 && (cum->nargs_prototype <= 0
13348 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
13349 && TARGET_XL_COMPAT
13350 && align_words >= GP_ARG_NUM_REG)))
13351 return 0;
13353 /* Otherwise, we pass in FPRs only. Check for partial copies. */
13354 passed_in_gprs = false;
13355 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
13357 /* Compute number of bytes / words passed in FPRs. If there
13358 is still space available in the register parameter area
13359 *after* that amount, a part of the argument will be passed
13360 in GPRs. In that case, the total amount passed in any
13361 registers is equal to the amount that would have been passed
13362 in GPRs if everything were passed there, so we fall back to
13363 the GPR code below to compute the appropriate value. */
13364 int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
13365 * MIN (8, GET_MODE_SIZE (elt_mode)));
13366 int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
13368 if (align_words + fpr_words < GP_ARG_NUM_REG)
13369 passed_in_gprs = true;
13370 else
13371 ret = fpr;
13375 if (passed_in_gprs
13376 && align_words < GP_ARG_NUM_REG
13377 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
13378 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
13380 if (ret != 0 && TARGET_DEBUG_ARG)
13381 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
13383 return ret;
13386 /* A C expression that indicates when an argument must be passed by
13387 reference. If nonzero for an argument, a copy of that argument is
13388 made in memory and a pointer to the argument is passed instead of
13389 the argument itself. The pointer is passed in whatever way is
13390 appropriate for passing a pointer to that type.
13392 Under V.4, aggregates and long double are passed by reference.
13394 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
13395 reference unless the AltiVec vector extension ABI is in force.
13397 As an extension to all ABIs, variable sized types are passed by
13398 reference. */
13400 static bool
13401 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
13402 machine_mode mode, const_tree type,
13403 bool named ATTRIBUTE_UNUSED)
13405 if (!type)
13406 return 0;
13408 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
13409 && FLOAT128_IEEE_P (TYPE_MODE (type)))
13411 if (TARGET_DEBUG_ARG)
13412 fprintf (stderr, "function_arg_pass_by_reference: V4 IEEE 128-bit\n");
13413 return 1;
13416 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
13418 if (TARGET_DEBUG_ARG)
13419 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
13420 return 1;
13423 if (int_size_in_bytes (type) < 0)
13425 if (TARGET_DEBUG_ARG)
13426 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
13427 return 1;
13430 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
13431 modes only exist for GCC vector types if -maltivec. */
13432 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
13434 if (TARGET_DEBUG_ARG)
13435 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
13436 return 1;
13439 /* Pass synthetic vectors in memory. */
13440 if (TREE_CODE (type) == VECTOR_TYPE
13441 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
13443 static bool warned_for_pass_big_vectors = false;
13444 if (TARGET_DEBUG_ARG)
13445 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
13446 if (!warned_for_pass_big_vectors)
13448 warning (OPT_Wpsabi, "GCC vector passed by reference: "
13449 "non-standard ABI extension with no compatibility guarantee");
13450 warned_for_pass_big_vectors = true;
13452 return 1;
13455 return 0;
13458 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
13459 already processes. Return true if the parameter must be passed
13460 (fully or partially) on the stack. */
13462 static bool
13463 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
13465 machine_mode mode;
13466 int unsignedp;
13467 rtx entry_parm;
13469 /* Catch errors. */
13470 if (type == NULL || type == error_mark_node)
13471 return true;
13473 /* Handle types with no storage requirement. */
13474 if (TYPE_MODE (type) == VOIDmode)
13475 return false;
13477 /* Handle complex types. */
13478 if (TREE_CODE (type) == COMPLEX_TYPE)
13479 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
13480 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
13482 /* Handle transparent aggregates. */
13483 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
13484 && TYPE_TRANSPARENT_AGGR (type))
13485 type = TREE_TYPE (first_field (type));
13487 /* See if this arg was passed by invisible reference. */
13488 if (pass_by_reference (get_cumulative_args (args_so_far),
13489 TYPE_MODE (type), type, true))
13490 type = build_pointer_type (type);
13492 /* Find mode as it is passed by the ABI. */
13493 unsignedp = TYPE_UNSIGNED (type);
13494 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
13496 /* If we must pass in stack, we need a stack. */
13497 if (rs6000_must_pass_in_stack (mode, type))
13498 return true;
13500 /* If there is no incoming register, we need a stack. */
13501 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
13502 if (entry_parm == NULL)
13503 return true;
13505 /* Likewise if we need to pass both in registers and on the stack. */
13506 if (GET_CODE (entry_parm) == PARALLEL
13507 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
13508 return true;
13510 /* Also true if we're partially in registers and partially not. */
13511 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
13512 return true;
13514 /* Update info on where next arg arrives in registers. */
13515 rs6000_function_arg_advance (args_so_far, mode, type, true);
13516 return false;
13519 /* Return true if FUN has no prototype, has a variable argument
13520 list, or passes any parameter in memory. */
13522 static bool
13523 rs6000_function_parms_need_stack (tree fun, bool incoming)
13525 tree fntype, result;
13526 CUMULATIVE_ARGS args_so_far_v;
13527 cumulative_args_t args_so_far;
13529 if (!fun)
13530 /* Must be a libcall, all of which only use reg parms. */
13531 return false;
13533 fntype = fun;
13534 if (!TYPE_P (fun))
13535 fntype = TREE_TYPE (fun);
13537 /* Varargs functions need the parameter save area. */
13538 if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
13539 return true;
13541 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
13542 args_so_far = pack_cumulative_args (&args_so_far_v);
13544 /* When incoming, we will have been passed the function decl.
13545 It is necessary to use the decl to handle K&R style functions,
13546 where TYPE_ARG_TYPES may not be available. */
13547 if (incoming)
13549 gcc_assert (DECL_P (fun));
13550 result = DECL_RESULT (fun);
13552 else
13553 result = TREE_TYPE (fntype);
13555 if (result && aggregate_value_p (result, fntype))
13557 if (!TYPE_P (result))
13558 result = TREE_TYPE (result);
13559 result = build_pointer_type (result);
13560 rs6000_parm_needs_stack (args_so_far, result);
13563 if (incoming)
13565 tree parm;
13567 for (parm = DECL_ARGUMENTS (fun);
13568 parm && parm != void_list_node;
13569 parm = TREE_CHAIN (parm))
13570 if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
13571 return true;
13573 else
13575 function_args_iterator args_iter;
13576 tree arg_type;
13578 FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
13579 if (rs6000_parm_needs_stack (args_so_far, arg_type))
13580 return true;
13583 return false;
13586 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
13587 usually a constant depending on the ABI. However, in the ELFv2 ABI
13588 the register parameter area is optional when calling a function that
13589 has a prototype is scope, has no variable argument list, and passes
13590 all parameters in registers. */
13593 rs6000_reg_parm_stack_space (tree fun, bool incoming)
13595 int reg_parm_stack_space;
13597 switch (DEFAULT_ABI)
13599 default:
13600 reg_parm_stack_space = 0;
13601 break;
13603 case ABI_AIX:
13604 case ABI_DARWIN:
13605 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
13606 break;
13608 case ABI_ELFv2:
13609 /* ??? Recomputing this every time is a bit expensive. Is there
13610 a place to cache this information? */
13611 if (rs6000_function_parms_need_stack (fun, incoming))
13612 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
13613 else
13614 reg_parm_stack_space = 0;
13615 break;
13618 return reg_parm_stack_space;
13621 static void
13622 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
13624 int i;
13625 machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
13627 if (nregs == 0)
13628 return;
13630 for (i = 0; i < nregs; i++)
13632 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
13633 if (reload_completed)
13635 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
13636 tem = NULL_RTX;
13637 else
13638 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
13639 i * GET_MODE_SIZE (reg_mode));
13641 else
13642 tem = replace_equiv_address (tem, XEXP (tem, 0));
13644 gcc_assert (tem);
13646 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
13650 /* Perform any needed actions needed for a function that is receiving a
13651 variable number of arguments.
13653 CUM is as above.
13655 MODE and TYPE are the mode and type of the current parameter.
13657 PRETEND_SIZE is a variable that should be set to the amount of stack
13658 that must be pushed by the prolog to pretend that our caller pushed
13661 Normally, this macro will push all remaining incoming registers on the
13662 stack and set PRETEND_SIZE to the length of the registers pushed. */
13664 static void
13665 setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
13666 tree type, int *pretend_size ATTRIBUTE_UNUSED,
13667 int no_rtl)
13669 CUMULATIVE_ARGS next_cum;
13670 int reg_size = TARGET_32BIT ? 4 : 8;
13671 rtx save_area = NULL_RTX, mem;
13672 int first_reg_offset;
13673 alias_set_type set;
13675 /* Skip the last named argument. */
13676 next_cum = *get_cumulative_args (cum);
13677 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
13679 if (DEFAULT_ABI == ABI_V4)
13681 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
13683 if (! no_rtl)
13685 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
13686 HOST_WIDE_INT offset = 0;
13688 /* Try to optimize the size of the varargs save area.
13689 The ABI requires that ap.reg_save_area is doubleword
13690 aligned, but we don't need to allocate space for all
13691 the bytes, only those to which we actually will save
13692 anything. */
13693 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
13694 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
13695 if (TARGET_HARD_FLOAT && TARGET_FPRS
13696 && next_cum.fregno <= FP_ARG_V4_MAX_REG
13697 && cfun->va_list_fpr_size)
13699 if (gpr_reg_num)
13700 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
13701 * UNITS_PER_FP_WORD;
13702 if (cfun->va_list_fpr_size
13703 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
13704 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
13705 else
13706 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
13707 * UNITS_PER_FP_WORD;
13709 if (gpr_reg_num)
13711 offset = -((first_reg_offset * reg_size) & ~7);
13712 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
13714 gpr_reg_num = cfun->va_list_gpr_size;
13715 if (reg_size == 4 && (first_reg_offset & 1))
13716 gpr_reg_num++;
13718 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
13720 else if (fpr_size)
13721 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
13722 * UNITS_PER_FP_WORD
13723 - (int) (GP_ARG_NUM_REG * reg_size);
13725 if (gpr_size + fpr_size)
13727 rtx reg_save_area
13728 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
13729 gcc_assert (GET_CODE (reg_save_area) == MEM);
13730 reg_save_area = XEXP (reg_save_area, 0);
13731 if (GET_CODE (reg_save_area) == PLUS)
13733 gcc_assert (XEXP (reg_save_area, 0)
13734 == virtual_stack_vars_rtx);
13735 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
13736 offset += INTVAL (XEXP (reg_save_area, 1));
13738 else
13739 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
13742 cfun->machine->varargs_save_offset = offset;
13743 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
13746 else
13748 first_reg_offset = next_cum.words;
13749 save_area = crtl->args.internal_arg_pointer;
13751 if (targetm.calls.must_pass_in_stack (mode, type))
13752 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
13755 set = get_varargs_alias_set ();
13756 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
13757 && cfun->va_list_gpr_size)
13759 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
13761 if (va_list_gpr_counter_field)
13762 /* V4 va_list_gpr_size counts number of registers needed. */
13763 n_gpr = cfun->va_list_gpr_size;
13764 else
13765 /* char * va_list instead counts number of bytes needed. */
13766 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
13768 if (nregs > n_gpr)
13769 nregs = n_gpr;
13771 mem = gen_rtx_MEM (BLKmode,
13772 plus_constant (Pmode, save_area,
13773 first_reg_offset * reg_size));
13774 MEM_NOTRAP_P (mem) = 1;
13775 set_mem_alias_set (mem, set);
13776 set_mem_align (mem, BITS_PER_WORD);
13778 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
13779 nregs);
13782 /* Save FP registers if needed. */
13783 if (DEFAULT_ABI == ABI_V4
13784 && TARGET_HARD_FLOAT && TARGET_FPRS
13785 && ! no_rtl
13786 && next_cum.fregno <= FP_ARG_V4_MAX_REG
13787 && cfun->va_list_fpr_size)
13789 int fregno = next_cum.fregno, nregs;
13790 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
13791 rtx lab = gen_label_rtx ();
13792 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
13793 * UNITS_PER_FP_WORD);
13795 emit_jump_insn
13796 (gen_rtx_SET (pc_rtx,
13797 gen_rtx_IF_THEN_ELSE (VOIDmode,
13798 gen_rtx_NE (VOIDmode, cr1,
13799 const0_rtx),
13800 gen_rtx_LABEL_REF (VOIDmode, lab),
13801 pc_rtx)));
13803 for (nregs = 0;
13804 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
13805 fregno++, off += UNITS_PER_FP_WORD, nregs++)
13807 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13808 ? DFmode : SFmode,
13809 plus_constant (Pmode, save_area, off));
13810 MEM_NOTRAP_P (mem) = 1;
13811 set_mem_alias_set (mem, set);
13812 set_mem_align (mem, GET_MODE_ALIGNMENT (
13813 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13814 ? DFmode : SFmode));
13815 emit_move_insn (mem, gen_rtx_REG (
13816 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13817 ? DFmode : SFmode, fregno));
13820 emit_label (lab);
13824 /* Create the va_list data type. */
13826 static tree
13827 rs6000_build_builtin_va_list (void)
13829 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
13831 /* For AIX, prefer 'char *' because that's what the system
13832 header files like. */
13833 if (DEFAULT_ABI != ABI_V4)
13834 return build_pointer_type (char_type_node);
13836 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
13837 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
13838 get_identifier ("__va_list_tag"), record);
13840 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
13841 unsigned_char_type_node);
13842 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
13843 unsigned_char_type_node);
13844 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
13845 every user file. */
13846 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13847 get_identifier ("reserved"), short_unsigned_type_node);
13848 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13849 get_identifier ("overflow_arg_area"),
13850 ptr_type_node);
13851 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13852 get_identifier ("reg_save_area"),
13853 ptr_type_node);
13855 va_list_gpr_counter_field = f_gpr;
13856 va_list_fpr_counter_field = f_fpr;
13858 DECL_FIELD_CONTEXT (f_gpr) = record;
13859 DECL_FIELD_CONTEXT (f_fpr) = record;
13860 DECL_FIELD_CONTEXT (f_res) = record;
13861 DECL_FIELD_CONTEXT (f_ovf) = record;
13862 DECL_FIELD_CONTEXT (f_sav) = record;
13864 TYPE_STUB_DECL (record) = type_decl;
13865 TYPE_NAME (record) = type_decl;
13866 TYPE_FIELDS (record) = f_gpr;
13867 DECL_CHAIN (f_gpr) = f_fpr;
13868 DECL_CHAIN (f_fpr) = f_res;
13869 DECL_CHAIN (f_res) = f_ovf;
13870 DECL_CHAIN (f_ovf) = f_sav;
13872 layout_type (record);
13874 /* The correct type is an array type of one element. */
13875 return build_array_type (record, build_index_type (size_zero_node));
13878 /* Implement va_start. */
13880 static void
13881 rs6000_va_start (tree valist, rtx nextarg)
13883 HOST_WIDE_INT words, n_gpr, n_fpr;
13884 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
13885 tree gpr, fpr, ovf, sav, t;
13887 /* Only SVR4 needs something special. */
13888 if (DEFAULT_ABI != ABI_V4)
13890 std_expand_builtin_va_start (valist, nextarg);
13891 return;
13894 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
13895 f_fpr = DECL_CHAIN (f_gpr);
13896 f_res = DECL_CHAIN (f_fpr);
13897 f_ovf = DECL_CHAIN (f_res);
13898 f_sav = DECL_CHAIN (f_ovf);
13900 valist = build_simple_mem_ref (valist);
13901 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
13902 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
13903 f_fpr, NULL_TREE);
13904 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
13905 f_ovf, NULL_TREE);
13906 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
13907 f_sav, NULL_TREE);
13909 /* Count number of gp and fp argument registers used. */
13910 words = crtl->args.info.words;
13911 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
13912 GP_ARG_NUM_REG);
13913 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
13914 FP_ARG_NUM_REG);
13916 if (TARGET_DEBUG_ARG)
13917 fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
13918 HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
13919 words, n_gpr, n_fpr);
13921 if (cfun->va_list_gpr_size)
13923 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
13924 build_int_cst (NULL_TREE, n_gpr));
13925 TREE_SIDE_EFFECTS (t) = 1;
13926 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13929 if (cfun->va_list_fpr_size)
13931 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
13932 build_int_cst (NULL_TREE, n_fpr));
13933 TREE_SIDE_EFFECTS (t) = 1;
13934 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13936 #ifdef HAVE_AS_GNU_ATTRIBUTE
13937 if (call_ABI_of_interest (cfun->decl))
13938 rs6000_passes_float = true;
13939 #endif
13942 /* Find the overflow area. */
13943 t = make_tree (TREE_TYPE (ovf), crtl->args.internal_arg_pointer);
13944 if (words != 0)
13945 t = fold_build_pointer_plus_hwi (t, words * MIN_UNITS_PER_WORD);
13946 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
13947 TREE_SIDE_EFFECTS (t) = 1;
13948 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13950 /* If there were no va_arg invocations, don't set up the register
13951 save area. */
13952 if (!cfun->va_list_gpr_size
13953 && !cfun->va_list_fpr_size
13954 && n_gpr < GP_ARG_NUM_REG
13955 && n_fpr < FP_ARG_V4_MAX_REG)
13956 return;
13958 /* Find the register save area. */
13959 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
13960 if (cfun->machine->varargs_save_offset)
13961 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
13962 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
13963 TREE_SIDE_EFFECTS (t) = 1;
13964 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13967 /* Implement va_arg. */
13969 static tree
13970 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
13971 gimple_seq *post_p)
13973 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
13974 tree gpr, fpr, ovf, sav, reg, t, u;
13975 int size, rsize, n_reg, sav_ofs, sav_scale;
13976 tree lab_false, lab_over, addr;
13977 int align;
13978 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
13979 int regalign = 0;
13980 gimple *stmt;
13982 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
13984 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
13985 return build_va_arg_indirect_ref (t);
13988 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
13989 earlier version of gcc, with the property that it always applied alignment
13990 adjustments to the va-args (even for zero-sized types). The cheapest way
13991 to deal with this is to replicate the effect of the part of
13992 std_gimplify_va_arg_expr that carries out the align adjust, for the case
13993 of relevance.
13994 We don't need to check for pass-by-reference because of the test above.
13995 We can return a simplifed answer, since we know there's no offset to add. */
13997 if (((TARGET_MACHO
13998 && rs6000_darwin64_abi)
13999 || DEFAULT_ABI == ABI_ELFv2
14000 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
14001 && integer_zerop (TYPE_SIZE (type)))
14003 unsigned HOST_WIDE_INT align, boundary;
14004 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
14005 align = PARM_BOUNDARY / BITS_PER_UNIT;
14006 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
14007 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
14008 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
14009 boundary /= BITS_PER_UNIT;
14010 if (boundary > align)
14012 tree t ;
14013 /* This updates arg ptr by the amount that would be necessary
14014 to align the zero-sized (but not zero-alignment) item. */
14015 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
14016 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
14017 gimplify_and_add (t, pre_p);
14019 t = fold_convert (sizetype, valist_tmp);
14020 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
14021 fold_convert (TREE_TYPE (valist),
14022 fold_build2 (BIT_AND_EXPR, sizetype, t,
14023 size_int (-boundary))));
14024 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
14025 gimplify_and_add (t, pre_p);
14027 /* Since it is zero-sized there's no increment for the item itself. */
14028 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
14029 return build_va_arg_indirect_ref (valist_tmp);
14032 if (DEFAULT_ABI != ABI_V4)
14034 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
14036 tree elem_type = TREE_TYPE (type);
14037 machine_mode elem_mode = TYPE_MODE (elem_type);
14038 int elem_size = GET_MODE_SIZE (elem_mode);
14040 if (elem_size < UNITS_PER_WORD)
14042 tree real_part, imag_part;
14043 gimple_seq post = NULL;
14045 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
14046 &post);
14047 /* Copy the value into a temporary, lest the formal temporary
14048 be reused out from under us. */
14049 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
14050 gimple_seq_add_seq (pre_p, post);
14052 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
14053 post_p);
14055 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
14059 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
14062 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
14063 f_fpr = DECL_CHAIN (f_gpr);
14064 f_res = DECL_CHAIN (f_fpr);
14065 f_ovf = DECL_CHAIN (f_res);
14066 f_sav = DECL_CHAIN (f_ovf);
14068 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
14069 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
14070 f_fpr, NULL_TREE);
14071 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
14072 f_ovf, NULL_TREE);
14073 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
14074 f_sav, NULL_TREE);
14076 size = int_size_in_bytes (type);
14077 rsize = (size + 3) / 4;
14078 int pad = 4 * rsize - size;
14079 align = 1;
14081 machine_mode mode = TYPE_MODE (type);
14082 if (abi_v4_pass_in_fpr (mode))
14084 /* FP args go in FP registers, if present. */
14085 reg = fpr;
14086 n_reg = (size + 7) / 8;
14087 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
14088 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
14089 if (mode != SFmode && mode != SDmode)
14090 align = 8;
14092 else
14094 /* Otherwise into GP registers. */
14095 reg = gpr;
14096 n_reg = rsize;
14097 sav_ofs = 0;
14098 sav_scale = 4;
14099 if (n_reg == 2)
14100 align = 8;
14103 /* Pull the value out of the saved registers.... */
14105 lab_over = NULL;
14106 addr = create_tmp_var (ptr_type_node, "addr");
14108 /* AltiVec vectors never go in registers when -mabi=altivec. */
14109 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
14110 align = 16;
14111 else
14113 lab_false = create_artificial_label (input_location);
14114 lab_over = create_artificial_label (input_location);
14116 /* Long long and SPE vectors are aligned in the registers.
14117 As are any other 2 gpr item such as complex int due to a
14118 historical mistake. */
14119 u = reg;
14120 if (n_reg == 2 && reg == gpr)
14122 regalign = 1;
14123 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
14124 build_int_cst (TREE_TYPE (reg), n_reg - 1));
14125 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
14126 unshare_expr (reg), u);
14128 /* _Decimal128 is passed in even/odd fpr pairs; the stored
14129 reg number is 0 for f1, so we want to make it odd. */
14130 else if (reg == fpr && mode == TDmode)
14132 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
14133 build_int_cst (TREE_TYPE (reg), 1));
14134 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
14137 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
14138 t = build2 (GE_EXPR, boolean_type_node, u, t);
14139 u = build1 (GOTO_EXPR, void_type_node, lab_false);
14140 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
14141 gimplify_and_add (t, pre_p);
14143 t = sav;
14144 if (sav_ofs)
14145 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
14147 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
14148 build_int_cst (TREE_TYPE (reg), n_reg));
14149 u = fold_convert (sizetype, u);
14150 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
14151 t = fold_build_pointer_plus (t, u);
14153 /* _Decimal32 varargs are located in the second word of the 64-bit
14154 FP register for 32-bit binaries. */
14155 if (TARGET_32BIT
14156 && TARGET_HARD_FLOAT && TARGET_FPRS
14157 && mode == SDmode)
14158 t = fold_build_pointer_plus_hwi (t, size);
14160 /* Args are passed right-aligned. */
14161 if (BYTES_BIG_ENDIAN)
14162 t = fold_build_pointer_plus_hwi (t, pad);
14164 gimplify_assign (addr, t, pre_p);
14166 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
14168 stmt = gimple_build_label (lab_false);
14169 gimple_seq_add_stmt (pre_p, stmt);
14171 if ((n_reg == 2 && !regalign) || n_reg > 2)
14173 /* Ensure that we don't find any more args in regs.
14174 Alignment has taken care of for special cases. */
14175 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
14179 /* ... otherwise out of the overflow area. */
14181 /* Care for on-stack alignment if needed. */
14182 t = ovf;
14183 if (align != 1)
14185 t = fold_build_pointer_plus_hwi (t, align - 1);
14186 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
14187 build_int_cst (TREE_TYPE (t), -align));
14190 /* Args are passed right-aligned. */
14191 if (BYTES_BIG_ENDIAN)
14192 t = fold_build_pointer_plus_hwi (t, pad);
14194 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
14196 gimplify_assign (unshare_expr (addr), t, pre_p);
14198 t = fold_build_pointer_plus_hwi (t, size);
14199 gimplify_assign (unshare_expr (ovf), t, pre_p);
14201 if (lab_over)
14203 stmt = gimple_build_label (lab_over);
14204 gimple_seq_add_stmt (pre_p, stmt);
14207 if (STRICT_ALIGNMENT
14208 && (TYPE_ALIGN (type)
14209 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
14211 /* The value (of type complex double, for example) may not be
14212 aligned in memory in the saved registers, so copy via a
14213 temporary. (This is the same code as used for SPARC.) */
14214 tree tmp = create_tmp_var (type, "va_arg_tmp");
14215 tree dest_addr = build_fold_addr_expr (tmp);
14217 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
14218 3, dest_addr, addr, size_int (rsize * 4));
14220 gimplify_and_add (copy, pre_p);
14221 addr = dest_addr;
14224 addr = fold_convert (ptrtype, addr);
14225 return build_va_arg_indirect_ref (addr);
14228 /* Builtins. */
14230 static void
14231 def_builtin (const char *name, tree type, enum rs6000_builtins code)
14233 tree t;
14234 unsigned classify = rs6000_builtin_info[(int)code].attr;
14235 const char *attr_string = "";
14237 gcc_assert (name != NULL);
14238 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
14240 if (rs6000_builtin_decls[(int)code])
14241 fatal_error (input_location,
14242 "internal error: builtin function %s already processed", name);
14244 rs6000_builtin_decls[(int)code] = t =
14245 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
14247 /* Set any special attributes. */
14248 if ((classify & RS6000_BTC_CONST) != 0)
14250 /* const function, function only depends on the inputs. */
14251 TREE_READONLY (t) = 1;
14252 TREE_NOTHROW (t) = 1;
14253 attr_string = ", const";
14255 else if ((classify & RS6000_BTC_PURE) != 0)
14257 /* pure function, function can read global memory, but does not set any
14258 external state. */
14259 DECL_PURE_P (t) = 1;
14260 TREE_NOTHROW (t) = 1;
14261 attr_string = ", pure";
14263 else if ((classify & RS6000_BTC_FP) != 0)
14265 /* Function is a math function. If rounding mode is on, then treat the
14266 function as not reading global memory, but it can have arbitrary side
14267 effects. If it is off, then assume the function is a const function.
14268 This mimics the ATTR_MATHFN_FPROUNDING attribute in
14269 builtin-attribute.def that is used for the math functions. */
14270 TREE_NOTHROW (t) = 1;
14271 if (flag_rounding_math)
14273 DECL_PURE_P (t) = 1;
14274 DECL_IS_NOVOPS (t) = 1;
14275 attr_string = ", fp, pure";
14277 else
14279 TREE_READONLY (t) = 1;
14280 attr_string = ", fp, const";
14283 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
14284 gcc_unreachable ();
14286 if (TARGET_DEBUG_BUILTIN)
14287 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
14288 (int)code, name, attr_string);
14291 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
14293 #undef RS6000_BUILTIN_0
14294 #undef RS6000_BUILTIN_1
14295 #undef RS6000_BUILTIN_2
14296 #undef RS6000_BUILTIN_3
14297 #undef RS6000_BUILTIN_A
14298 #undef RS6000_BUILTIN_D
14299 #undef RS6000_BUILTIN_E
14300 #undef RS6000_BUILTIN_H
14301 #undef RS6000_BUILTIN_P
14302 #undef RS6000_BUILTIN_Q
14303 #undef RS6000_BUILTIN_S
14304 #undef RS6000_BUILTIN_X
14306 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14307 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14308 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14309 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
14310 { MASK, ICODE, NAME, ENUM },
14312 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14313 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14314 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14315 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14316 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14317 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14318 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14319 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14321 static const struct builtin_description bdesc_3arg[] =
14323 #include "powerpcspe-builtin.def"
14326 /* DST operations: void foo (void *, const int, const char). */
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 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14346 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
14347 { MASK, ICODE, NAME, ENUM },
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_dst[] =
14358 #include "powerpcspe-builtin.def"
14361 /* Simple binary operations: VECc = foo (VECa, VECb). */
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 { MASK, ICODE, NAME, ENUM },
14381 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14382 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14383 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
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_2arg[] =
14393 #include "powerpcspe-builtin.def"
14396 #undef RS6000_BUILTIN_0
14397 #undef RS6000_BUILTIN_1
14398 #undef RS6000_BUILTIN_2
14399 #undef RS6000_BUILTIN_3
14400 #undef RS6000_BUILTIN_A
14401 #undef RS6000_BUILTIN_D
14402 #undef RS6000_BUILTIN_E
14403 #undef RS6000_BUILTIN_H
14404 #undef RS6000_BUILTIN_P
14405 #undef RS6000_BUILTIN_Q
14406 #undef RS6000_BUILTIN_S
14407 #undef RS6000_BUILTIN_X
14409 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14410 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14411 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14412 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14413 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14414 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14415 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14416 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14417 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
14418 { MASK, ICODE, NAME, ENUM },
14420 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14421 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14422 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14424 /* AltiVec predicates. */
14426 static const struct builtin_description bdesc_altivec_preds[] =
14428 #include "powerpcspe-builtin.def"
14431 /* SPE predicates. */
14432 #undef RS6000_BUILTIN_0
14433 #undef RS6000_BUILTIN_1
14434 #undef RS6000_BUILTIN_2
14435 #undef RS6000_BUILTIN_3
14436 #undef RS6000_BUILTIN_A
14437 #undef RS6000_BUILTIN_D
14438 #undef RS6000_BUILTIN_E
14439 #undef RS6000_BUILTIN_H
14440 #undef RS6000_BUILTIN_P
14441 #undef RS6000_BUILTIN_Q
14442 #undef RS6000_BUILTIN_S
14443 #undef RS6000_BUILTIN_X
14445 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14446 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14447 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14448 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14449 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14450 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14451 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14452 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14453 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14454 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14455 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
14456 { MASK, ICODE, NAME, ENUM },
14458 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14460 static const struct builtin_description bdesc_spe_predicates[] =
14462 #include "powerpcspe-builtin.def"
14465 /* SPE evsel predicates. */
14466 #undef RS6000_BUILTIN_0
14467 #undef RS6000_BUILTIN_1
14468 #undef RS6000_BUILTIN_2
14469 #undef RS6000_BUILTIN_3
14470 #undef RS6000_BUILTIN_A
14471 #undef RS6000_BUILTIN_D
14472 #undef RS6000_BUILTIN_E
14473 #undef RS6000_BUILTIN_H
14474 #undef RS6000_BUILTIN_P
14475 #undef RS6000_BUILTIN_Q
14476 #undef RS6000_BUILTIN_S
14477 #undef RS6000_BUILTIN_X
14479 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14480 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14481 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14482 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14483 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14484 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14485 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
14486 { MASK, ICODE, NAME, ENUM },
14488 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14489 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14490 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14491 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14492 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14494 static const struct builtin_description bdesc_spe_evsel[] =
14496 #include "powerpcspe-builtin.def"
14499 /* PAIRED predicates. */
14500 #undef RS6000_BUILTIN_0
14501 #undef RS6000_BUILTIN_1
14502 #undef RS6000_BUILTIN_2
14503 #undef RS6000_BUILTIN_3
14504 #undef RS6000_BUILTIN_A
14505 #undef RS6000_BUILTIN_D
14506 #undef RS6000_BUILTIN_E
14507 #undef RS6000_BUILTIN_H
14508 #undef RS6000_BUILTIN_P
14509 #undef RS6000_BUILTIN_Q
14510 #undef RS6000_BUILTIN_S
14511 #undef RS6000_BUILTIN_X
14513 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14514 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14515 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14516 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14517 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14518 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14519 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14520 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14521 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14522 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
14523 { MASK, ICODE, NAME, ENUM },
14525 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14526 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14528 static const struct builtin_description bdesc_paired_preds[] =
14530 #include "powerpcspe-builtin.def"
14533 /* ABS* operations. */
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 { MASK, ICODE, NAME, ENUM },
14555 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14556 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14557 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14558 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14559 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
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_abs[] =
14565 #include "powerpcspe-builtin.def"
14568 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
14569 foo (VECa). */
14571 #undef RS6000_BUILTIN_0
14572 #undef RS6000_BUILTIN_1
14573 #undef RS6000_BUILTIN_2
14574 #undef RS6000_BUILTIN_3
14575 #undef RS6000_BUILTIN_A
14576 #undef RS6000_BUILTIN_D
14577 #undef RS6000_BUILTIN_E
14578 #undef RS6000_BUILTIN_H
14579 #undef RS6000_BUILTIN_P
14580 #undef RS6000_BUILTIN_Q
14581 #undef RS6000_BUILTIN_S
14582 #undef RS6000_BUILTIN_X
14584 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14585 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
14586 { MASK, ICODE, NAME, ENUM },
14588 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14589 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14590 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14591 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14592 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14593 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
14594 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14595 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14596 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14597 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14599 static const struct builtin_description bdesc_1arg[] =
14601 #include "powerpcspe-builtin.def"
14604 /* Simple no-argument operations: result = __builtin_darn_32 () */
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 { MASK, ICODE, NAME, ENUM },
14622 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
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_0arg[] =
14636 #include "powerpcspe-builtin.def"
14639 /* HTM builtins. */
14640 #undef RS6000_BUILTIN_0
14641 #undef RS6000_BUILTIN_1
14642 #undef RS6000_BUILTIN_2
14643 #undef RS6000_BUILTIN_3
14644 #undef RS6000_BUILTIN_A
14645 #undef RS6000_BUILTIN_D
14646 #undef RS6000_BUILTIN_E
14647 #undef RS6000_BUILTIN_H
14648 #undef RS6000_BUILTIN_P
14649 #undef RS6000_BUILTIN_Q
14650 #undef RS6000_BUILTIN_S
14651 #undef RS6000_BUILTIN_X
14653 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
14654 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
14655 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
14656 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
14657 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
14658 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
14659 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
14660 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
14661 { MASK, ICODE, NAME, ENUM },
14663 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
14664 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
14665 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
14666 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
14668 static const struct builtin_description bdesc_htm[] =
14670 #include "powerpcspe-builtin.def"
14673 #undef RS6000_BUILTIN_0
14674 #undef RS6000_BUILTIN_1
14675 #undef RS6000_BUILTIN_2
14676 #undef RS6000_BUILTIN_3
14677 #undef RS6000_BUILTIN_A
14678 #undef RS6000_BUILTIN_D
14679 #undef RS6000_BUILTIN_E
14680 #undef RS6000_BUILTIN_H
14681 #undef RS6000_BUILTIN_P
14682 #undef RS6000_BUILTIN_Q
14683 #undef RS6000_BUILTIN_S
14685 /* Return true if a builtin function is overloaded. */
14686 bool
14687 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
14689 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
14692 const char *
14693 rs6000_overloaded_builtin_name (enum rs6000_builtins fncode)
14695 return rs6000_builtin_info[(int)fncode].name;
14698 /* Expand an expression EXP that calls a builtin without arguments. */
14699 static rtx
14700 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
14702 rtx pat;
14703 machine_mode tmode = insn_data[icode].operand[0].mode;
14705 if (icode == CODE_FOR_nothing)
14706 /* Builtin not supported on this processor. */
14707 return 0;
14709 if (target == 0
14710 || GET_MODE (target) != tmode
14711 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14712 target = gen_reg_rtx (tmode);
14714 pat = GEN_FCN (icode) (target);
14715 if (! pat)
14716 return 0;
14717 emit_insn (pat);
14719 return target;
14723 static rtx
14724 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
14726 rtx pat;
14727 tree arg0 = CALL_EXPR_ARG (exp, 0);
14728 tree arg1 = CALL_EXPR_ARG (exp, 1);
14729 rtx op0 = expand_normal (arg0);
14730 rtx op1 = expand_normal (arg1);
14731 machine_mode mode0 = insn_data[icode].operand[0].mode;
14732 machine_mode mode1 = insn_data[icode].operand[1].mode;
14734 if (icode == CODE_FOR_nothing)
14735 /* Builtin not supported on this processor. */
14736 return 0;
14738 /* If we got invalid arguments bail out before generating bad rtl. */
14739 if (arg0 == error_mark_node || arg1 == error_mark_node)
14740 return const0_rtx;
14742 if (GET_CODE (op0) != CONST_INT
14743 || INTVAL (op0) > 255
14744 || INTVAL (op0) < 0)
14746 error ("argument 1 must be an 8-bit field value");
14747 return const0_rtx;
14750 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
14751 op0 = copy_to_mode_reg (mode0, op0);
14753 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
14754 op1 = copy_to_mode_reg (mode1, op1);
14756 pat = GEN_FCN (icode) (op0, op1);
14757 if (! pat)
14758 return const0_rtx;
14759 emit_insn (pat);
14761 return NULL_RTX;
14764 static rtx
14765 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
14767 rtx pat;
14768 tree arg0 = CALL_EXPR_ARG (exp, 0);
14769 rtx op0 = expand_normal (arg0);
14770 machine_mode tmode = insn_data[icode].operand[0].mode;
14771 machine_mode mode0 = insn_data[icode].operand[1].mode;
14773 if (icode == CODE_FOR_nothing)
14774 /* Builtin not supported on this processor. */
14775 return 0;
14777 /* If we got invalid arguments bail out before generating bad rtl. */
14778 if (arg0 == error_mark_node)
14779 return const0_rtx;
14781 if (icode == CODE_FOR_altivec_vspltisb
14782 || icode == CODE_FOR_altivec_vspltish
14783 || icode == CODE_FOR_altivec_vspltisw
14784 || icode == CODE_FOR_spe_evsplatfi
14785 || icode == CODE_FOR_spe_evsplati)
14787 /* Only allow 5-bit *signed* literals. */
14788 if (GET_CODE (op0) != CONST_INT
14789 || INTVAL (op0) > 15
14790 || INTVAL (op0) < -16)
14792 error ("argument 1 must be a 5-bit signed literal");
14793 return CONST0_RTX (tmode);
14797 if (target == 0
14798 || GET_MODE (target) != tmode
14799 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14800 target = gen_reg_rtx (tmode);
14802 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14803 op0 = copy_to_mode_reg (mode0, op0);
14805 pat = GEN_FCN (icode) (target, op0);
14806 if (! pat)
14807 return 0;
14808 emit_insn (pat);
14810 return target;
14813 static rtx
14814 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
14816 rtx pat, scratch1, scratch2;
14817 tree arg0 = CALL_EXPR_ARG (exp, 0);
14818 rtx op0 = expand_normal (arg0);
14819 machine_mode tmode = insn_data[icode].operand[0].mode;
14820 machine_mode mode0 = insn_data[icode].operand[1].mode;
14822 /* If we have invalid arguments, bail out before generating bad rtl. */
14823 if (arg0 == error_mark_node)
14824 return const0_rtx;
14826 if (target == 0
14827 || GET_MODE (target) != tmode
14828 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14829 target = gen_reg_rtx (tmode);
14831 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14832 op0 = copy_to_mode_reg (mode0, op0);
14834 scratch1 = gen_reg_rtx (mode0);
14835 scratch2 = gen_reg_rtx (mode0);
14837 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
14838 if (! pat)
14839 return 0;
14840 emit_insn (pat);
14842 return target;
14845 static rtx
14846 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
14848 rtx pat;
14849 tree arg0 = CALL_EXPR_ARG (exp, 0);
14850 tree arg1 = CALL_EXPR_ARG (exp, 1);
14851 rtx op0 = expand_normal (arg0);
14852 rtx op1 = expand_normal (arg1);
14853 machine_mode tmode = insn_data[icode].operand[0].mode;
14854 machine_mode mode0 = insn_data[icode].operand[1].mode;
14855 machine_mode mode1 = insn_data[icode].operand[2].mode;
14857 if (icode == CODE_FOR_nothing)
14858 /* Builtin not supported on this processor. */
14859 return 0;
14861 /* If we got invalid arguments bail out before generating bad rtl. */
14862 if (arg0 == error_mark_node || arg1 == error_mark_node)
14863 return const0_rtx;
14865 if (icode == CODE_FOR_altivec_vcfux
14866 || icode == CODE_FOR_altivec_vcfsx
14867 || icode == CODE_FOR_altivec_vctsxs
14868 || icode == CODE_FOR_altivec_vctuxs
14869 || icode == CODE_FOR_altivec_vspltb
14870 || icode == CODE_FOR_altivec_vsplth
14871 || icode == CODE_FOR_altivec_vspltw
14872 || icode == CODE_FOR_spe_evaddiw
14873 || icode == CODE_FOR_spe_evldd
14874 || icode == CODE_FOR_spe_evldh
14875 || icode == CODE_FOR_spe_evldw
14876 || icode == CODE_FOR_spe_evlhhesplat
14877 || icode == CODE_FOR_spe_evlhhossplat
14878 || icode == CODE_FOR_spe_evlhhousplat
14879 || icode == CODE_FOR_spe_evlwhe
14880 || icode == CODE_FOR_spe_evlwhos
14881 || icode == CODE_FOR_spe_evlwhou
14882 || icode == CODE_FOR_spe_evlwhsplat
14883 || icode == CODE_FOR_spe_evlwwsplat
14884 || icode == CODE_FOR_spe_evrlwi
14885 || icode == CODE_FOR_spe_evslwi
14886 || icode == CODE_FOR_spe_evsrwis
14887 || icode == CODE_FOR_spe_evsubifw
14888 || icode == CODE_FOR_spe_evsrwiu)
14890 /* Only allow 5-bit unsigned literals. */
14891 STRIP_NOPS (arg1);
14892 if (TREE_CODE (arg1) != INTEGER_CST
14893 || TREE_INT_CST_LOW (arg1) & ~0x1f)
14895 error ("argument 2 must be a 5-bit unsigned literal");
14896 return CONST0_RTX (tmode);
14899 else if (icode == CODE_FOR_dfptstsfi_eq_dd
14900 || icode == CODE_FOR_dfptstsfi_lt_dd
14901 || icode == CODE_FOR_dfptstsfi_gt_dd
14902 || icode == CODE_FOR_dfptstsfi_unordered_dd
14903 || icode == CODE_FOR_dfptstsfi_eq_td
14904 || icode == CODE_FOR_dfptstsfi_lt_td
14905 || icode == CODE_FOR_dfptstsfi_gt_td
14906 || icode == CODE_FOR_dfptstsfi_unordered_td)
14908 /* Only allow 6-bit unsigned literals. */
14909 STRIP_NOPS (arg0);
14910 if (TREE_CODE (arg0) != INTEGER_CST
14911 || !IN_RANGE (TREE_INT_CST_LOW (arg0), 0, 63))
14913 error ("argument 1 must be a 6-bit unsigned literal");
14914 return CONST0_RTX (tmode);
14917 else if (icode == CODE_FOR_xststdcdp
14918 || icode == CODE_FOR_xststdcsp
14919 || icode == CODE_FOR_xvtstdcdp
14920 || icode == CODE_FOR_xvtstdcsp)
14922 /* Only allow 7-bit unsigned literals. */
14923 STRIP_NOPS (arg1);
14924 if (TREE_CODE (arg1) != INTEGER_CST
14925 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 127))
14927 error ("argument 2 must be a 7-bit unsigned literal");
14928 return CONST0_RTX (tmode);
14932 if (target == 0
14933 || GET_MODE (target) != tmode
14934 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14935 target = gen_reg_rtx (tmode);
14937 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14938 op0 = copy_to_mode_reg (mode0, op0);
14939 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14940 op1 = copy_to_mode_reg (mode1, op1);
14942 pat = GEN_FCN (icode) (target, op0, op1);
14943 if (! pat)
14944 return 0;
14945 emit_insn (pat);
14947 return target;
14950 static rtx
14951 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
14953 rtx pat, scratch;
14954 tree cr6_form = CALL_EXPR_ARG (exp, 0);
14955 tree arg0 = CALL_EXPR_ARG (exp, 1);
14956 tree arg1 = CALL_EXPR_ARG (exp, 2);
14957 rtx op0 = expand_normal (arg0);
14958 rtx op1 = expand_normal (arg1);
14959 machine_mode tmode = SImode;
14960 machine_mode mode0 = insn_data[icode].operand[1].mode;
14961 machine_mode mode1 = insn_data[icode].operand[2].mode;
14962 int cr6_form_int;
14964 if (TREE_CODE (cr6_form) != INTEGER_CST)
14966 error ("argument 1 of __builtin_altivec_predicate must be a constant");
14967 return const0_rtx;
14969 else
14970 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
14972 gcc_assert (mode0 == mode1);
14974 /* If we have invalid arguments, bail out before generating bad rtl. */
14975 if (arg0 == error_mark_node || arg1 == error_mark_node)
14976 return const0_rtx;
14978 if (target == 0
14979 || GET_MODE (target) != tmode
14980 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14981 target = gen_reg_rtx (tmode);
14983 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14984 op0 = copy_to_mode_reg (mode0, op0);
14985 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14986 op1 = copy_to_mode_reg (mode1, op1);
14988 /* Note that for many of the relevant operations (e.g. cmpne or
14989 cmpeq) with float or double operands, it makes more sense for the
14990 mode of the allocated scratch register to select a vector of
14991 integer. But the choice to copy the mode of operand 0 was made
14992 long ago and there are no plans to change it. */
14993 scratch = gen_reg_rtx (mode0);
14995 pat = GEN_FCN (icode) (scratch, op0, op1);
14996 if (! pat)
14997 return 0;
14998 emit_insn (pat);
15000 /* The vec_any* and vec_all* predicates use the same opcodes for two
15001 different operations, but the bits in CR6 will be different
15002 depending on what information we want. So we have to play tricks
15003 with CR6 to get the right bits out.
15005 If you think this is disgusting, look at the specs for the
15006 AltiVec predicates. */
15008 switch (cr6_form_int)
15010 case 0:
15011 emit_insn (gen_cr6_test_for_zero (target));
15012 break;
15013 case 1:
15014 emit_insn (gen_cr6_test_for_zero_reverse (target));
15015 break;
15016 case 2:
15017 emit_insn (gen_cr6_test_for_lt (target));
15018 break;
15019 case 3:
15020 emit_insn (gen_cr6_test_for_lt_reverse (target));
15021 break;
15022 default:
15023 error ("argument 1 of __builtin_altivec_predicate is out of range");
15024 break;
15027 return target;
15030 static rtx
15031 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
15033 rtx pat, addr;
15034 tree arg0 = CALL_EXPR_ARG (exp, 0);
15035 tree arg1 = CALL_EXPR_ARG (exp, 1);
15036 machine_mode tmode = insn_data[icode].operand[0].mode;
15037 machine_mode mode0 = Pmode;
15038 machine_mode mode1 = Pmode;
15039 rtx op0 = expand_normal (arg0);
15040 rtx op1 = expand_normal (arg1);
15042 if (icode == CODE_FOR_nothing)
15043 /* Builtin not supported on this processor. */
15044 return 0;
15046 /* If we got invalid arguments bail out before generating bad rtl. */
15047 if (arg0 == error_mark_node || arg1 == error_mark_node)
15048 return const0_rtx;
15050 if (target == 0
15051 || GET_MODE (target) != tmode
15052 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15053 target = gen_reg_rtx (tmode);
15055 op1 = copy_to_mode_reg (mode1, op1);
15057 if (op0 == const0_rtx)
15059 addr = gen_rtx_MEM (tmode, op1);
15061 else
15063 op0 = copy_to_mode_reg (mode0, op0);
15064 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
15067 pat = GEN_FCN (icode) (target, addr);
15069 if (! pat)
15070 return 0;
15071 emit_insn (pat);
15073 return target;
15076 /* Return a constant vector for use as a little-endian permute control vector
15077 to reverse the order of elements of the given vector mode. */
15078 static rtx
15079 swap_selector_for_mode (machine_mode mode)
15081 /* These are little endian vectors, so their elements are reversed
15082 from what you would normally expect for a permute control vector. */
15083 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
15084 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
15085 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
15086 unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
15087 unsigned int *swaparray, i;
15088 rtx perm[16];
15090 switch (mode)
15092 case E_V2DFmode:
15093 case E_V2DImode:
15094 swaparray = swap2;
15095 break;
15096 case E_V4SFmode:
15097 case E_V4SImode:
15098 swaparray = swap4;
15099 break;
15100 case E_V8HImode:
15101 swaparray = swap8;
15102 break;
15103 case E_V16QImode:
15104 swaparray = swap16;
15105 break;
15106 default:
15107 gcc_unreachable ();
15110 for (i = 0; i < 16; ++i)
15111 perm[i] = GEN_INT (swaparray[i]);
15113 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
15116 /* Generate code for an "lvxl", or "lve*x" built-in for a little endian target
15117 with -maltivec=be specified. Issue the load followed by an element-
15118 reversing permute. */
15119 void
15120 altivec_expand_lvx_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
15122 rtx tmp = gen_reg_rtx (mode);
15123 rtx load = gen_rtx_SET (tmp, op1);
15124 rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
15125 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
15126 rtx sel = swap_selector_for_mode (mode);
15127 rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
15129 gcc_assert (REG_P (op0));
15130 emit_insn (par);
15131 emit_insn (gen_rtx_SET (op0, vperm));
15134 /* Generate code for a "stvxl" built-in for a little endian target with
15135 -maltivec=be specified. Issue the store preceded by an element-reversing
15136 permute. */
15137 void
15138 altivec_expand_stvx_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
15140 rtx tmp = gen_reg_rtx (mode);
15141 rtx store = gen_rtx_SET (op0, tmp);
15142 rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
15143 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
15144 rtx sel = swap_selector_for_mode (mode);
15145 rtx vperm;
15147 gcc_assert (REG_P (op1));
15148 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
15149 emit_insn (gen_rtx_SET (tmp, vperm));
15150 emit_insn (par);
15153 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
15154 specified. Issue the store preceded by an element-reversing permute. */
15155 void
15156 altivec_expand_stvex_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
15158 machine_mode inner_mode = GET_MODE_INNER (mode);
15159 rtx tmp = gen_reg_rtx (mode);
15160 rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
15161 rtx sel = swap_selector_for_mode (mode);
15162 rtx vperm;
15164 gcc_assert (REG_P (op1));
15165 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
15166 emit_insn (gen_rtx_SET (tmp, vperm));
15167 emit_insn (gen_rtx_SET (op0, stvx));
15170 static rtx
15171 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
15173 rtx pat, addr;
15174 tree arg0 = CALL_EXPR_ARG (exp, 0);
15175 tree arg1 = CALL_EXPR_ARG (exp, 1);
15176 machine_mode tmode = insn_data[icode].operand[0].mode;
15177 machine_mode mode0 = Pmode;
15178 machine_mode mode1 = Pmode;
15179 rtx op0 = expand_normal (arg0);
15180 rtx op1 = expand_normal (arg1);
15182 if (icode == CODE_FOR_nothing)
15183 /* Builtin not supported on this processor. */
15184 return 0;
15186 /* If we got invalid arguments bail out before generating bad rtl. */
15187 if (arg0 == error_mark_node || arg1 == error_mark_node)
15188 return const0_rtx;
15190 if (target == 0
15191 || GET_MODE (target) != tmode
15192 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15193 target = gen_reg_rtx (tmode);
15195 op1 = copy_to_mode_reg (mode1, op1);
15197 /* For LVX, express the RTL accurately by ANDing the address with -16.
15198 LVXL and LVE*X expand to use UNSPECs to hide their special behavior,
15199 so the raw address is fine. */
15200 if (icode == CODE_FOR_altivec_lvx_v2df_2op
15201 || icode == CODE_FOR_altivec_lvx_v2di_2op
15202 || icode == CODE_FOR_altivec_lvx_v4sf_2op
15203 || icode == CODE_FOR_altivec_lvx_v4si_2op
15204 || icode == CODE_FOR_altivec_lvx_v8hi_2op
15205 || icode == CODE_FOR_altivec_lvx_v16qi_2op)
15207 rtx rawaddr;
15208 if (op0 == const0_rtx)
15209 rawaddr = op1;
15210 else
15212 op0 = copy_to_mode_reg (mode0, op0);
15213 rawaddr = gen_rtx_PLUS (Pmode, op1, op0);
15215 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
15216 addr = gen_rtx_MEM (blk ? BLKmode : tmode, addr);
15218 /* For -maltivec=be, emit the load and follow it up with a
15219 permute to swap the elements. */
15220 if (!BYTES_BIG_ENDIAN && VECTOR_ELT_ORDER_BIG)
15222 rtx temp = gen_reg_rtx (tmode);
15223 emit_insn (gen_rtx_SET (temp, addr));
15225 rtx sel = swap_selector_for_mode (tmode);
15226 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, temp, temp, sel),
15227 UNSPEC_VPERM);
15228 emit_insn (gen_rtx_SET (target, vperm));
15230 else
15231 emit_insn (gen_rtx_SET (target, addr));
15233 else
15235 if (op0 == const0_rtx)
15236 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
15237 else
15239 op0 = copy_to_mode_reg (mode0, op0);
15240 addr = gen_rtx_MEM (blk ? BLKmode : tmode,
15241 gen_rtx_PLUS (Pmode, op1, op0));
15244 pat = GEN_FCN (icode) (target, addr);
15245 if (! pat)
15246 return 0;
15247 emit_insn (pat);
15250 return target;
15253 static rtx
15254 spe_expand_stv_builtin (enum insn_code icode, tree exp)
15256 tree arg0 = CALL_EXPR_ARG (exp, 0);
15257 tree arg1 = CALL_EXPR_ARG (exp, 1);
15258 tree arg2 = CALL_EXPR_ARG (exp, 2);
15259 rtx op0 = expand_normal (arg0);
15260 rtx op1 = expand_normal (arg1);
15261 rtx op2 = expand_normal (arg2);
15262 rtx pat;
15263 machine_mode mode0 = insn_data[icode].operand[0].mode;
15264 machine_mode mode1 = insn_data[icode].operand[1].mode;
15265 machine_mode mode2 = insn_data[icode].operand[2].mode;
15267 /* Invalid arguments. Bail before doing anything stoopid! */
15268 if (arg0 == error_mark_node
15269 || arg1 == error_mark_node
15270 || arg2 == error_mark_node)
15271 return const0_rtx;
15273 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
15274 op0 = copy_to_mode_reg (mode2, op0);
15275 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
15276 op1 = copy_to_mode_reg (mode0, op1);
15277 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
15278 op2 = copy_to_mode_reg (mode1, op2);
15280 pat = GEN_FCN (icode) (op1, op2, op0);
15281 if (pat)
15282 emit_insn (pat);
15283 return NULL_RTX;
15286 static rtx
15287 paired_expand_stv_builtin (enum insn_code icode, tree exp)
15289 tree arg0 = CALL_EXPR_ARG (exp, 0);
15290 tree arg1 = CALL_EXPR_ARG (exp, 1);
15291 tree arg2 = CALL_EXPR_ARG (exp, 2);
15292 rtx op0 = expand_normal (arg0);
15293 rtx op1 = expand_normal (arg1);
15294 rtx op2 = expand_normal (arg2);
15295 rtx pat, addr;
15296 machine_mode tmode = insn_data[icode].operand[0].mode;
15297 machine_mode mode1 = Pmode;
15298 machine_mode mode2 = Pmode;
15300 /* Invalid arguments. Bail before doing anything stoopid! */
15301 if (arg0 == error_mark_node
15302 || arg1 == error_mark_node
15303 || arg2 == error_mark_node)
15304 return const0_rtx;
15306 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
15307 op0 = copy_to_mode_reg (tmode, op0);
15309 op2 = copy_to_mode_reg (mode2, op2);
15311 if (op1 == const0_rtx)
15313 addr = gen_rtx_MEM (tmode, op2);
15315 else
15317 op1 = copy_to_mode_reg (mode1, op1);
15318 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
15321 pat = GEN_FCN (icode) (addr, op0);
15322 if (pat)
15323 emit_insn (pat);
15324 return NULL_RTX;
15327 static rtx
15328 altivec_expand_stxvl_builtin (enum insn_code icode, tree exp)
15330 rtx pat;
15331 tree arg0 = CALL_EXPR_ARG (exp, 0);
15332 tree arg1 = CALL_EXPR_ARG (exp, 1);
15333 tree arg2 = CALL_EXPR_ARG (exp, 2);
15334 rtx op0 = expand_normal (arg0);
15335 rtx op1 = expand_normal (arg1);
15336 rtx op2 = expand_normal (arg2);
15337 machine_mode mode0 = insn_data[icode].operand[0].mode;
15338 machine_mode mode1 = insn_data[icode].operand[1].mode;
15339 machine_mode mode2 = insn_data[icode].operand[2].mode;
15341 if (icode == CODE_FOR_nothing)
15342 /* Builtin not supported on this processor. */
15343 return NULL_RTX;
15345 /* If we got invalid arguments bail out before generating bad rtl. */
15346 if (arg0 == error_mark_node
15347 || arg1 == error_mark_node
15348 || arg2 == error_mark_node)
15349 return NULL_RTX;
15351 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15352 op0 = copy_to_mode_reg (mode0, op0);
15353 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15354 op1 = copy_to_mode_reg (mode1, op1);
15355 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15356 op2 = copy_to_mode_reg (mode2, op2);
15358 pat = GEN_FCN (icode) (op0, op1, op2);
15359 if (pat)
15360 emit_insn (pat);
15362 return NULL_RTX;
15365 static rtx
15366 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
15368 tree arg0 = CALL_EXPR_ARG (exp, 0);
15369 tree arg1 = CALL_EXPR_ARG (exp, 1);
15370 tree arg2 = CALL_EXPR_ARG (exp, 2);
15371 rtx op0 = expand_normal (arg0);
15372 rtx op1 = expand_normal (arg1);
15373 rtx op2 = expand_normal (arg2);
15374 rtx pat, addr, rawaddr;
15375 machine_mode tmode = insn_data[icode].operand[0].mode;
15376 machine_mode smode = insn_data[icode].operand[1].mode;
15377 machine_mode mode1 = Pmode;
15378 machine_mode mode2 = Pmode;
15380 /* Invalid arguments. Bail before doing anything stoopid! */
15381 if (arg0 == error_mark_node
15382 || arg1 == error_mark_node
15383 || arg2 == error_mark_node)
15384 return const0_rtx;
15386 op2 = copy_to_mode_reg (mode2, op2);
15388 /* For STVX, express the RTL accurately by ANDing the address with -16.
15389 STVXL and STVE*X expand to use UNSPECs to hide their special behavior,
15390 so the raw address is fine. */
15391 if (icode == CODE_FOR_altivec_stvx_v2df_2op
15392 || icode == CODE_FOR_altivec_stvx_v2di_2op
15393 || icode == CODE_FOR_altivec_stvx_v4sf_2op
15394 || icode == CODE_FOR_altivec_stvx_v4si_2op
15395 || icode == CODE_FOR_altivec_stvx_v8hi_2op
15396 || icode == CODE_FOR_altivec_stvx_v16qi_2op)
15398 if (op1 == const0_rtx)
15399 rawaddr = op2;
15400 else
15402 op1 = copy_to_mode_reg (mode1, op1);
15403 rawaddr = gen_rtx_PLUS (Pmode, op2, op1);
15406 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
15407 addr = gen_rtx_MEM (tmode, addr);
15409 op0 = copy_to_mode_reg (tmode, op0);
15411 /* For -maltivec=be, emit a permute to swap the elements, followed
15412 by the store. */
15413 if (!BYTES_BIG_ENDIAN && VECTOR_ELT_ORDER_BIG)
15415 rtx temp = gen_reg_rtx (tmode);
15416 rtx sel = swap_selector_for_mode (tmode);
15417 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, op0, op0, sel),
15418 UNSPEC_VPERM);
15419 emit_insn (gen_rtx_SET (temp, vperm));
15420 emit_insn (gen_rtx_SET (addr, temp));
15422 else
15423 emit_insn (gen_rtx_SET (addr, op0));
15425 else
15427 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
15428 op0 = copy_to_mode_reg (smode, op0);
15430 if (op1 == const0_rtx)
15431 addr = gen_rtx_MEM (tmode, op2);
15432 else
15434 op1 = copy_to_mode_reg (mode1, op1);
15435 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op2, op1));
15438 pat = GEN_FCN (icode) (addr, op0);
15439 if (pat)
15440 emit_insn (pat);
15443 return NULL_RTX;
15446 /* Return the appropriate SPR number associated with the given builtin. */
15447 static inline HOST_WIDE_INT
15448 htm_spr_num (enum rs6000_builtins code)
15450 if (code == HTM_BUILTIN_GET_TFHAR
15451 || code == HTM_BUILTIN_SET_TFHAR)
15452 return TFHAR_SPR;
15453 else if (code == HTM_BUILTIN_GET_TFIAR
15454 || code == HTM_BUILTIN_SET_TFIAR)
15455 return TFIAR_SPR;
15456 else if (code == HTM_BUILTIN_GET_TEXASR
15457 || code == HTM_BUILTIN_SET_TEXASR)
15458 return TEXASR_SPR;
15459 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
15460 || code == HTM_BUILTIN_SET_TEXASRU);
15461 return TEXASRU_SPR;
15464 /* Return the appropriate SPR regno associated with the given builtin. */
15465 static inline HOST_WIDE_INT
15466 htm_spr_regno (enum rs6000_builtins code)
15468 if (code == HTM_BUILTIN_GET_TFHAR
15469 || code == HTM_BUILTIN_SET_TFHAR)
15470 return TFHAR_REGNO;
15471 else if (code == HTM_BUILTIN_GET_TFIAR
15472 || code == HTM_BUILTIN_SET_TFIAR)
15473 return TFIAR_REGNO;
15474 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
15475 || code == HTM_BUILTIN_SET_TEXASR
15476 || code == HTM_BUILTIN_GET_TEXASRU
15477 || code == HTM_BUILTIN_SET_TEXASRU);
15478 return TEXASR_REGNO;
15481 /* Return the correct ICODE value depending on whether we are
15482 setting or reading the HTM SPRs. */
15483 static inline enum insn_code
15484 rs6000_htm_spr_icode (bool nonvoid)
15486 if (nonvoid)
15487 return (TARGET_POWERPC64) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
15488 else
15489 return (TARGET_POWERPC64) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
15492 /* Expand the HTM builtin in EXP and store the result in TARGET.
15493 Store true in *EXPANDEDP if we found a builtin to expand. */
15494 static rtx
15495 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
15497 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15498 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
15499 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
15500 const struct builtin_description *d;
15501 size_t i;
15503 *expandedp = true;
15505 if (!TARGET_POWERPC64
15506 && (fcode == HTM_BUILTIN_TABORTDC
15507 || fcode == HTM_BUILTIN_TABORTDCI))
15509 size_t uns_fcode = (size_t)fcode;
15510 const char *name = rs6000_builtin_info[uns_fcode].name;
15511 error ("builtin %s is only valid in 64-bit mode", name);
15512 return const0_rtx;
15515 /* Expand the HTM builtins. */
15516 d = bdesc_htm;
15517 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
15518 if (d->code == fcode)
15520 rtx op[MAX_HTM_OPERANDS], pat;
15521 int nopnds = 0;
15522 tree arg;
15523 call_expr_arg_iterator iter;
15524 unsigned attr = rs6000_builtin_info[fcode].attr;
15525 enum insn_code icode = d->icode;
15526 const struct insn_operand_data *insn_op;
15527 bool uses_spr = (attr & RS6000_BTC_SPR);
15528 rtx cr = NULL_RTX;
15530 if (uses_spr)
15531 icode = rs6000_htm_spr_icode (nonvoid);
15532 insn_op = &insn_data[icode].operand[0];
15534 if (nonvoid)
15536 machine_mode tmode = (uses_spr) ? insn_op->mode : E_SImode;
15537 if (!target
15538 || GET_MODE (target) != tmode
15539 || (uses_spr && !(*insn_op->predicate) (target, tmode)))
15540 target = gen_reg_rtx (tmode);
15541 if (uses_spr)
15542 op[nopnds++] = target;
15545 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
15547 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
15548 return const0_rtx;
15550 insn_op = &insn_data[icode].operand[nopnds];
15552 op[nopnds] = expand_normal (arg);
15554 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
15556 if (!strcmp (insn_op->constraint, "n"))
15558 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
15559 if (!CONST_INT_P (op[nopnds]))
15560 error ("argument %d must be an unsigned literal", arg_num);
15561 else
15562 error ("argument %d is an unsigned literal that is "
15563 "out of range", arg_num);
15564 return const0_rtx;
15566 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
15569 nopnds++;
15572 /* Handle the builtins for extended mnemonics. These accept
15573 no arguments, but map to builtins that take arguments. */
15574 switch (fcode)
15576 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
15577 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
15578 op[nopnds++] = GEN_INT (1);
15579 if (flag_checking)
15580 attr |= RS6000_BTC_UNARY;
15581 break;
15582 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
15583 op[nopnds++] = GEN_INT (0);
15584 if (flag_checking)
15585 attr |= RS6000_BTC_UNARY;
15586 break;
15587 default:
15588 break;
15591 /* If this builtin accesses SPRs, then pass in the appropriate
15592 SPR number and SPR regno as the last two operands. */
15593 if (uses_spr)
15595 machine_mode mode = (TARGET_POWERPC64) ? DImode : SImode;
15596 op[nopnds++] = gen_rtx_CONST_INT (mode, htm_spr_num (fcode));
15597 op[nopnds++] = gen_rtx_REG (mode, htm_spr_regno (fcode));
15599 /* If this builtin accesses a CR, then pass in a scratch
15600 CR as the last operand. */
15601 else if (attr & RS6000_BTC_CR)
15602 { cr = gen_reg_rtx (CCmode);
15603 op[nopnds++] = cr;
15606 if (flag_checking)
15608 int expected_nopnds = 0;
15609 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
15610 expected_nopnds = 1;
15611 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
15612 expected_nopnds = 2;
15613 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
15614 expected_nopnds = 3;
15615 if (!(attr & RS6000_BTC_VOID))
15616 expected_nopnds += 1;
15617 if (uses_spr)
15618 expected_nopnds += 2;
15620 gcc_assert (nopnds == expected_nopnds
15621 && nopnds <= MAX_HTM_OPERANDS);
15624 switch (nopnds)
15626 case 1:
15627 pat = GEN_FCN (icode) (op[0]);
15628 break;
15629 case 2:
15630 pat = GEN_FCN (icode) (op[0], op[1]);
15631 break;
15632 case 3:
15633 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
15634 break;
15635 case 4:
15636 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
15637 break;
15638 default:
15639 gcc_unreachable ();
15641 if (!pat)
15642 return NULL_RTX;
15643 emit_insn (pat);
15645 if (attr & RS6000_BTC_CR)
15647 if (fcode == HTM_BUILTIN_TBEGIN)
15649 /* Emit code to set TARGET to true or false depending on
15650 whether the tbegin. instruction successfully or failed
15651 to start a transaction. We do this by placing the 1's
15652 complement of CR's EQ bit into TARGET. */
15653 rtx scratch = gen_reg_rtx (SImode);
15654 emit_insn (gen_rtx_SET (scratch,
15655 gen_rtx_EQ (SImode, cr,
15656 const0_rtx)));
15657 emit_insn (gen_rtx_SET (target,
15658 gen_rtx_XOR (SImode, scratch,
15659 GEN_INT (1))));
15661 else
15663 /* Emit code to copy the 4-bit condition register field
15664 CR into the least significant end of register TARGET. */
15665 rtx scratch1 = gen_reg_rtx (SImode);
15666 rtx scratch2 = gen_reg_rtx (SImode);
15667 rtx subreg = simplify_gen_subreg (CCmode, scratch1, SImode, 0);
15668 emit_insn (gen_movcc (subreg, cr));
15669 emit_insn (gen_lshrsi3 (scratch2, scratch1, GEN_INT (28)));
15670 emit_insn (gen_andsi3 (target, scratch2, GEN_INT (0xf)));
15674 if (nonvoid)
15675 return target;
15676 return const0_rtx;
15679 *expandedp = false;
15680 return NULL_RTX;
15683 /* Expand the CPU builtin in FCODE and store the result in TARGET. */
15685 static rtx
15686 cpu_expand_builtin (enum rs6000_builtins fcode, tree exp ATTRIBUTE_UNUSED,
15687 rtx target)
15689 /* __builtin_cpu_init () is a nop, so expand to nothing. */
15690 if (fcode == RS6000_BUILTIN_CPU_INIT)
15691 return const0_rtx;
15693 if (target == 0 || GET_MODE (target) != SImode)
15694 target = gen_reg_rtx (SImode);
15696 #ifdef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
15697 tree arg = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
15698 if (TREE_CODE (arg) != STRING_CST)
15700 error ("builtin %s only accepts a string argument",
15701 rs6000_builtin_info[(size_t) fcode].name);
15702 return const0_rtx;
15705 if (fcode == RS6000_BUILTIN_CPU_IS)
15707 const char *cpu = TREE_STRING_POINTER (arg);
15708 rtx cpuid = NULL_RTX;
15709 for (size_t i = 0; i < ARRAY_SIZE (cpu_is_info); i++)
15710 if (strcmp (cpu, cpu_is_info[i].cpu) == 0)
15712 /* The CPUID value in the TCB is offset by _DL_FIRST_PLATFORM. */
15713 cpuid = GEN_INT (cpu_is_info[i].cpuid + _DL_FIRST_PLATFORM);
15714 break;
15716 if (cpuid == NULL_RTX)
15718 /* Invalid CPU argument. */
15719 error ("cpu %s is an invalid argument to builtin %s",
15720 cpu, rs6000_builtin_info[(size_t) fcode].name);
15721 return const0_rtx;
15724 rtx platform = gen_reg_rtx (SImode);
15725 rtx tcbmem = gen_const_mem (SImode,
15726 gen_rtx_PLUS (Pmode,
15727 gen_rtx_REG (Pmode, TLS_REGNUM),
15728 GEN_INT (TCB_PLATFORM_OFFSET)));
15729 emit_move_insn (platform, tcbmem);
15730 emit_insn (gen_eqsi3 (target, platform, cpuid));
15732 else if (fcode == RS6000_BUILTIN_CPU_SUPPORTS)
15734 const char *hwcap = TREE_STRING_POINTER (arg);
15735 rtx mask = NULL_RTX;
15736 int hwcap_offset;
15737 for (size_t i = 0; i < ARRAY_SIZE (cpu_supports_info); i++)
15738 if (strcmp (hwcap, cpu_supports_info[i].hwcap) == 0)
15740 mask = GEN_INT (cpu_supports_info[i].mask);
15741 hwcap_offset = TCB_HWCAP_OFFSET (cpu_supports_info[i].id);
15742 break;
15744 if (mask == NULL_RTX)
15746 /* Invalid HWCAP argument. */
15747 error ("hwcap %s is an invalid argument to builtin %s",
15748 hwcap, rs6000_builtin_info[(size_t) fcode].name);
15749 return const0_rtx;
15752 rtx tcb_hwcap = gen_reg_rtx (SImode);
15753 rtx tcbmem = gen_const_mem (SImode,
15754 gen_rtx_PLUS (Pmode,
15755 gen_rtx_REG (Pmode, TLS_REGNUM),
15756 GEN_INT (hwcap_offset)));
15757 emit_move_insn (tcb_hwcap, tcbmem);
15758 rtx scratch1 = gen_reg_rtx (SImode);
15759 emit_insn (gen_rtx_SET (scratch1, gen_rtx_AND (SImode, tcb_hwcap, mask)));
15760 rtx scratch2 = gen_reg_rtx (SImode);
15761 emit_insn (gen_eqsi3 (scratch2, scratch1, const0_rtx));
15762 emit_insn (gen_rtx_SET (target, gen_rtx_XOR (SImode, scratch2, const1_rtx)));
15765 /* Record that we have expanded a CPU builtin, so that we can later
15766 emit a reference to the special symbol exported by LIBC to ensure we
15767 do not link against an old LIBC that doesn't support this feature. */
15768 cpu_builtin_p = true;
15770 #else
15771 /* For old LIBCs, always return FALSE. */
15772 emit_move_insn (target, GEN_INT (0));
15773 #endif /* TARGET_LIBC_PROVIDES_HWCAP_IN_TCB */
15775 return target;
15778 static rtx
15779 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
15781 rtx pat;
15782 tree arg0 = CALL_EXPR_ARG (exp, 0);
15783 tree arg1 = CALL_EXPR_ARG (exp, 1);
15784 tree arg2 = CALL_EXPR_ARG (exp, 2);
15785 rtx op0 = expand_normal (arg0);
15786 rtx op1 = expand_normal (arg1);
15787 rtx op2 = expand_normal (arg2);
15788 machine_mode tmode = insn_data[icode].operand[0].mode;
15789 machine_mode mode0 = insn_data[icode].operand[1].mode;
15790 machine_mode mode1 = insn_data[icode].operand[2].mode;
15791 machine_mode mode2 = insn_data[icode].operand[3].mode;
15793 if (icode == CODE_FOR_nothing)
15794 /* Builtin not supported on this processor. */
15795 return 0;
15797 /* If we got invalid arguments bail out before generating bad rtl. */
15798 if (arg0 == error_mark_node
15799 || arg1 == error_mark_node
15800 || arg2 == error_mark_node)
15801 return const0_rtx;
15803 /* Check and prepare argument depending on the instruction code.
15805 Note that a switch statement instead of the sequence of tests
15806 would be incorrect as many of the CODE_FOR values could be
15807 CODE_FOR_nothing and that would yield multiple alternatives
15808 with identical values. We'd never reach here at runtime in
15809 this case. */
15810 if (icode == CODE_FOR_altivec_vsldoi_v4sf
15811 || icode == CODE_FOR_altivec_vsldoi_v2df
15812 || icode == CODE_FOR_altivec_vsldoi_v4si
15813 || icode == CODE_FOR_altivec_vsldoi_v8hi
15814 || icode == CODE_FOR_altivec_vsldoi_v16qi)
15816 /* Only allow 4-bit unsigned literals. */
15817 STRIP_NOPS (arg2);
15818 if (TREE_CODE (arg2) != INTEGER_CST
15819 || TREE_INT_CST_LOW (arg2) & ~0xf)
15821 error ("argument 3 must be a 4-bit unsigned literal");
15822 return CONST0_RTX (tmode);
15825 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
15826 || icode == CODE_FOR_vsx_xxpermdi_v2di
15827 || icode == CODE_FOR_vsx_xxpermdi_v2df_be
15828 || icode == CODE_FOR_vsx_xxpermdi_v2di_be
15829 || icode == CODE_FOR_vsx_xxpermdi_v1ti
15830 || icode == CODE_FOR_vsx_xxpermdi_v4sf
15831 || icode == CODE_FOR_vsx_xxpermdi_v4si
15832 || icode == CODE_FOR_vsx_xxpermdi_v8hi
15833 || icode == CODE_FOR_vsx_xxpermdi_v16qi
15834 || icode == CODE_FOR_vsx_xxsldwi_v16qi
15835 || icode == CODE_FOR_vsx_xxsldwi_v8hi
15836 || icode == CODE_FOR_vsx_xxsldwi_v4si
15837 || icode == CODE_FOR_vsx_xxsldwi_v4sf
15838 || icode == CODE_FOR_vsx_xxsldwi_v2di
15839 || icode == CODE_FOR_vsx_xxsldwi_v2df)
15841 /* Only allow 2-bit unsigned literals. */
15842 STRIP_NOPS (arg2);
15843 if (TREE_CODE (arg2) != INTEGER_CST
15844 || TREE_INT_CST_LOW (arg2) & ~0x3)
15846 error ("argument 3 must be a 2-bit unsigned literal");
15847 return CONST0_RTX (tmode);
15850 else if (icode == CODE_FOR_vsx_set_v2df
15851 || icode == CODE_FOR_vsx_set_v2di
15852 || icode == CODE_FOR_bcdadd
15853 || icode == CODE_FOR_bcdadd_lt
15854 || icode == CODE_FOR_bcdadd_eq
15855 || icode == CODE_FOR_bcdadd_gt
15856 || icode == CODE_FOR_bcdsub
15857 || icode == CODE_FOR_bcdsub_lt
15858 || icode == CODE_FOR_bcdsub_eq
15859 || icode == CODE_FOR_bcdsub_gt)
15861 /* Only allow 1-bit unsigned literals. */
15862 STRIP_NOPS (arg2);
15863 if (TREE_CODE (arg2) != INTEGER_CST
15864 || TREE_INT_CST_LOW (arg2) & ~0x1)
15866 error ("argument 3 must be a 1-bit unsigned literal");
15867 return CONST0_RTX (tmode);
15870 else if (icode == CODE_FOR_dfp_ddedpd_dd
15871 || icode == CODE_FOR_dfp_ddedpd_td)
15873 /* Only allow 2-bit unsigned literals where the value is 0 or 2. */
15874 STRIP_NOPS (arg0);
15875 if (TREE_CODE (arg0) != INTEGER_CST
15876 || TREE_INT_CST_LOW (arg2) & ~0x3)
15878 error ("argument 1 must be 0 or 2");
15879 return CONST0_RTX (tmode);
15882 else if (icode == CODE_FOR_dfp_denbcd_dd
15883 || icode == CODE_FOR_dfp_denbcd_td)
15885 /* Only allow 1-bit unsigned literals. */
15886 STRIP_NOPS (arg0);
15887 if (TREE_CODE (arg0) != INTEGER_CST
15888 || TREE_INT_CST_LOW (arg0) & ~0x1)
15890 error ("argument 1 must be a 1-bit unsigned literal");
15891 return CONST0_RTX (tmode);
15894 else if (icode == CODE_FOR_dfp_dscli_dd
15895 || icode == CODE_FOR_dfp_dscli_td
15896 || icode == CODE_FOR_dfp_dscri_dd
15897 || icode == CODE_FOR_dfp_dscri_td)
15899 /* Only allow 6-bit unsigned literals. */
15900 STRIP_NOPS (arg1);
15901 if (TREE_CODE (arg1) != INTEGER_CST
15902 || TREE_INT_CST_LOW (arg1) & ~0x3f)
15904 error ("argument 2 must be a 6-bit unsigned literal");
15905 return CONST0_RTX (tmode);
15908 else if (icode == CODE_FOR_crypto_vshasigmaw
15909 || icode == CODE_FOR_crypto_vshasigmad)
15911 /* Check whether the 2nd and 3rd arguments are integer constants and in
15912 range and prepare arguments. */
15913 STRIP_NOPS (arg1);
15914 if (TREE_CODE (arg1) != INTEGER_CST || wi::geu_p (arg1, 2))
15916 error ("argument 2 must be 0 or 1");
15917 return CONST0_RTX (tmode);
15920 STRIP_NOPS (arg2);
15921 if (TREE_CODE (arg2) != INTEGER_CST || wi::geu_p (arg2, 16))
15923 error ("argument 3 must be in the range 0..15");
15924 return CONST0_RTX (tmode);
15928 if (target == 0
15929 || GET_MODE (target) != tmode
15930 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15931 target = gen_reg_rtx (tmode);
15933 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15934 op0 = copy_to_mode_reg (mode0, op0);
15935 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15936 op1 = copy_to_mode_reg (mode1, op1);
15937 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15938 op2 = copy_to_mode_reg (mode2, op2);
15940 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
15941 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
15942 else
15943 pat = GEN_FCN (icode) (target, op0, op1, op2);
15944 if (! pat)
15945 return 0;
15946 emit_insn (pat);
15948 return target;
15951 /* Expand the lvx builtins. */
15952 static rtx
15953 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
15955 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15956 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15957 tree arg0;
15958 machine_mode tmode, mode0;
15959 rtx pat, op0;
15960 enum insn_code icode;
15962 switch (fcode)
15964 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
15965 icode = CODE_FOR_vector_altivec_load_v16qi;
15966 break;
15967 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
15968 icode = CODE_FOR_vector_altivec_load_v8hi;
15969 break;
15970 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
15971 icode = CODE_FOR_vector_altivec_load_v4si;
15972 break;
15973 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
15974 icode = CODE_FOR_vector_altivec_load_v4sf;
15975 break;
15976 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
15977 icode = CODE_FOR_vector_altivec_load_v2df;
15978 break;
15979 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
15980 icode = CODE_FOR_vector_altivec_load_v2di;
15981 break;
15982 case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
15983 icode = CODE_FOR_vector_altivec_load_v1ti;
15984 break;
15985 default:
15986 *expandedp = false;
15987 return NULL_RTX;
15990 *expandedp = true;
15992 arg0 = CALL_EXPR_ARG (exp, 0);
15993 op0 = expand_normal (arg0);
15994 tmode = insn_data[icode].operand[0].mode;
15995 mode0 = insn_data[icode].operand[1].mode;
15997 if (target == 0
15998 || GET_MODE (target) != tmode
15999 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16000 target = gen_reg_rtx (tmode);
16002 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16003 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16005 pat = GEN_FCN (icode) (target, op0);
16006 if (! pat)
16007 return 0;
16008 emit_insn (pat);
16009 return target;
16012 /* Expand the stvx builtins. */
16013 static rtx
16014 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
16015 bool *expandedp)
16017 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16018 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
16019 tree arg0, arg1;
16020 machine_mode mode0, mode1;
16021 rtx pat, op0, op1;
16022 enum insn_code icode;
16024 switch (fcode)
16026 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
16027 icode = CODE_FOR_vector_altivec_store_v16qi;
16028 break;
16029 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
16030 icode = CODE_FOR_vector_altivec_store_v8hi;
16031 break;
16032 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
16033 icode = CODE_FOR_vector_altivec_store_v4si;
16034 break;
16035 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
16036 icode = CODE_FOR_vector_altivec_store_v4sf;
16037 break;
16038 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
16039 icode = CODE_FOR_vector_altivec_store_v2df;
16040 break;
16041 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
16042 icode = CODE_FOR_vector_altivec_store_v2di;
16043 break;
16044 case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
16045 icode = CODE_FOR_vector_altivec_store_v1ti;
16046 break;
16047 default:
16048 *expandedp = false;
16049 return NULL_RTX;
16052 arg0 = CALL_EXPR_ARG (exp, 0);
16053 arg1 = CALL_EXPR_ARG (exp, 1);
16054 op0 = expand_normal (arg0);
16055 op1 = expand_normal (arg1);
16056 mode0 = insn_data[icode].operand[0].mode;
16057 mode1 = insn_data[icode].operand[1].mode;
16059 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16060 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16061 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
16062 op1 = copy_to_mode_reg (mode1, op1);
16064 pat = GEN_FCN (icode) (op0, op1);
16065 if (pat)
16066 emit_insn (pat);
16068 *expandedp = true;
16069 return NULL_RTX;
16072 /* Expand the dst builtins. */
16073 static rtx
16074 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
16075 bool *expandedp)
16077 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16078 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16079 tree arg0, arg1, arg2;
16080 machine_mode mode0, mode1;
16081 rtx pat, op0, op1, op2;
16082 const struct builtin_description *d;
16083 size_t i;
16085 *expandedp = false;
16087 /* Handle DST variants. */
16088 d = bdesc_dst;
16089 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
16090 if (d->code == fcode)
16092 arg0 = CALL_EXPR_ARG (exp, 0);
16093 arg1 = CALL_EXPR_ARG (exp, 1);
16094 arg2 = CALL_EXPR_ARG (exp, 2);
16095 op0 = expand_normal (arg0);
16096 op1 = expand_normal (arg1);
16097 op2 = expand_normal (arg2);
16098 mode0 = insn_data[d->icode].operand[0].mode;
16099 mode1 = insn_data[d->icode].operand[1].mode;
16101 /* Invalid arguments, bail out before generating bad rtl. */
16102 if (arg0 == error_mark_node
16103 || arg1 == error_mark_node
16104 || arg2 == error_mark_node)
16105 return const0_rtx;
16107 *expandedp = true;
16108 STRIP_NOPS (arg2);
16109 if (TREE_CODE (arg2) != INTEGER_CST
16110 || TREE_INT_CST_LOW (arg2) & ~0x3)
16112 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
16113 return const0_rtx;
16116 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
16117 op0 = copy_to_mode_reg (Pmode, op0);
16118 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
16119 op1 = copy_to_mode_reg (mode1, op1);
16121 pat = GEN_FCN (d->icode) (op0, op1, op2);
16122 if (pat != 0)
16123 emit_insn (pat);
16125 return NULL_RTX;
16128 return NULL_RTX;
16131 /* Expand vec_init builtin. */
16132 static rtx
16133 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
16135 machine_mode tmode = TYPE_MODE (type);
16136 machine_mode inner_mode = GET_MODE_INNER (tmode);
16137 int i, n_elt = GET_MODE_NUNITS (tmode);
16139 gcc_assert (VECTOR_MODE_P (tmode));
16140 gcc_assert (n_elt == call_expr_nargs (exp));
16142 if (!target || !register_operand (target, tmode))
16143 target = gen_reg_rtx (tmode);
16145 /* If we have a vector compromised of a single element, such as V1TImode, do
16146 the initialization directly. */
16147 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
16149 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
16150 emit_move_insn (target, gen_lowpart (tmode, x));
16152 else
16154 rtvec v = rtvec_alloc (n_elt);
16156 for (i = 0; i < n_elt; ++i)
16158 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
16159 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
16162 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
16165 return target;
16168 /* Return the integer constant in ARG. Constrain it to be in the range
16169 of the subparts of VEC_TYPE; issue an error if not. */
16171 static int
16172 get_element_number (tree vec_type, tree arg)
16174 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
16176 if (!tree_fits_uhwi_p (arg)
16177 || (elt = tree_to_uhwi (arg), elt > max))
16179 error ("selector must be an integer constant in the range 0..%wi", max);
16180 return 0;
16183 return elt;
16186 /* Expand vec_set builtin. */
16187 static rtx
16188 altivec_expand_vec_set_builtin (tree exp)
16190 machine_mode tmode, mode1;
16191 tree arg0, arg1, arg2;
16192 int elt;
16193 rtx op0, op1;
16195 arg0 = CALL_EXPR_ARG (exp, 0);
16196 arg1 = CALL_EXPR_ARG (exp, 1);
16197 arg2 = CALL_EXPR_ARG (exp, 2);
16199 tmode = TYPE_MODE (TREE_TYPE (arg0));
16200 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
16201 gcc_assert (VECTOR_MODE_P (tmode));
16203 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
16204 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
16205 elt = get_element_number (TREE_TYPE (arg0), arg2);
16207 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
16208 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
16210 op0 = force_reg (tmode, op0);
16211 op1 = force_reg (mode1, op1);
16213 rs6000_expand_vector_set (op0, op1, elt);
16215 return op0;
16218 /* Expand vec_ext builtin. */
16219 static rtx
16220 altivec_expand_vec_ext_builtin (tree exp, rtx target)
16222 machine_mode tmode, mode0;
16223 tree arg0, arg1;
16224 rtx op0;
16225 rtx op1;
16227 arg0 = CALL_EXPR_ARG (exp, 0);
16228 arg1 = CALL_EXPR_ARG (exp, 1);
16230 op0 = expand_normal (arg0);
16231 op1 = expand_normal (arg1);
16233 /* Call get_element_number to validate arg1 if it is a constant. */
16234 if (TREE_CODE (arg1) == INTEGER_CST)
16235 (void) get_element_number (TREE_TYPE (arg0), arg1);
16237 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
16238 mode0 = TYPE_MODE (TREE_TYPE (arg0));
16239 gcc_assert (VECTOR_MODE_P (mode0));
16241 op0 = force_reg (mode0, op0);
16243 if (optimize || !target || !register_operand (target, tmode))
16244 target = gen_reg_rtx (tmode);
16246 rs6000_expand_vector_extract (target, op0, op1);
16248 return target;
16251 /* Expand the builtin in EXP and store the result in TARGET. Store
16252 true in *EXPANDEDP if we found a builtin to expand. */
16253 static rtx
16254 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
16256 const struct builtin_description *d;
16257 size_t i;
16258 enum insn_code icode;
16259 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16260 tree arg0, arg1, arg2;
16261 rtx op0, pat;
16262 machine_mode tmode, mode0;
16263 enum rs6000_builtins fcode
16264 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16266 if (rs6000_overloaded_builtin_p (fcode))
16268 *expandedp = true;
16269 error ("unresolved overload for Altivec builtin %qF", fndecl);
16271 /* Given it is invalid, just generate a normal call. */
16272 return expand_call (exp, target, false);
16275 target = altivec_expand_ld_builtin (exp, target, expandedp);
16276 if (*expandedp)
16277 return target;
16279 target = altivec_expand_st_builtin (exp, target, expandedp);
16280 if (*expandedp)
16281 return target;
16283 target = altivec_expand_dst_builtin (exp, target, expandedp);
16284 if (*expandedp)
16285 return target;
16287 *expandedp = true;
16289 switch (fcode)
16291 case ALTIVEC_BUILTIN_STVX_V2DF:
16292 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df_2op, exp);
16293 case ALTIVEC_BUILTIN_STVX_V2DI:
16294 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di_2op, exp);
16295 case ALTIVEC_BUILTIN_STVX_V4SF:
16296 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf_2op, exp);
16297 case ALTIVEC_BUILTIN_STVX:
16298 case ALTIVEC_BUILTIN_STVX_V4SI:
16299 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si_2op, exp);
16300 case ALTIVEC_BUILTIN_STVX_V8HI:
16301 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi_2op, exp);
16302 case ALTIVEC_BUILTIN_STVX_V16QI:
16303 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi_2op, exp);
16304 case ALTIVEC_BUILTIN_STVEBX:
16305 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
16306 case ALTIVEC_BUILTIN_STVEHX:
16307 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
16308 case ALTIVEC_BUILTIN_STVEWX:
16309 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
16310 case ALTIVEC_BUILTIN_STVXL_V2DF:
16311 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
16312 case ALTIVEC_BUILTIN_STVXL_V2DI:
16313 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
16314 case ALTIVEC_BUILTIN_STVXL_V4SF:
16315 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
16316 case ALTIVEC_BUILTIN_STVXL:
16317 case ALTIVEC_BUILTIN_STVXL_V4SI:
16318 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
16319 case ALTIVEC_BUILTIN_STVXL_V8HI:
16320 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
16321 case ALTIVEC_BUILTIN_STVXL_V16QI:
16322 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
16324 case ALTIVEC_BUILTIN_STVLX:
16325 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
16326 case ALTIVEC_BUILTIN_STVLXL:
16327 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
16328 case ALTIVEC_BUILTIN_STVRX:
16329 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
16330 case ALTIVEC_BUILTIN_STVRXL:
16331 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
16333 case P9V_BUILTIN_STXVL:
16334 return altivec_expand_stxvl_builtin (CODE_FOR_stxvl, exp);
16336 case VSX_BUILTIN_STXVD2X_V1TI:
16337 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
16338 case VSX_BUILTIN_STXVD2X_V2DF:
16339 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
16340 case VSX_BUILTIN_STXVD2X_V2DI:
16341 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
16342 case VSX_BUILTIN_STXVW4X_V4SF:
16343 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
16344 case VSX_BUILTIN_STXVW4X_V4SI:
16345 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
16346 case VSX_BUILTIN_STXVW4X_V8HI:
16347 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
16348 case VSX_BUILTIN_STXVW4X_V16QI:
16349 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
16351 /* For the following on big endian, it's ok to use any appropriate
16352 unaligned-supporting store, so use a generic expander. For
16353 little-endian, the exact element-reversing instruction must
16354 be used. */
16355 case VSX_BUILTIN_ST_ELEMREV_V2DF:
16357 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2df
16358 : CODE_FOR_vsx_st_elemrev_v2df);
16359 return altivec_expand_stv_builtin (code, exp);
16361 case VSX_BUILTIN_ST_ELEMREV_V2DI:
16363 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2di
16364 : CODE_FOR_vsx_st_elemrev_v2di);
16365 return altivec_expand_stv_builtin (code, exp);
16367 case VSX_BUILTIN_ST_ELEMREV_V4SF:
16369 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4sf
16370 : CODE_FOR_vsx_st_elemrev_v4sf);
16371 return altivec_expand_stv_builtin (code, exp);
16373 case VSX_BUILTIN_ST_ELEMREV_V4SI:
16375 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4si
16376 : CODE_FOR_vsx_st_elemrev_v4si);
16377 return altivec_expand_stv_builtin (code, exp);
16379 case VSX_BUILTIN_ST_ELEMREV_V8HI:
16381 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v8hi
16382 : CODE_FOR_vsx_st_elemrev_v8hi);
16383 return altivec_expand_stv_builtin (code, exp);
16385 case VSX_BUILTIN_ST_ELEMREV_V16QI:
16387 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v16qi
16388 : CODE_FOR_vsx_st_elemrev_v16qi);
16389 return altivec_expand_stv_builtin (code, exp);
16392 case ALTIVEC_BUILTIN_MFVSCR:
16393 icode = CODE_FOR_altivec_mfvscr;
16394 tmode = insn_data[icode].operand[0].mode;
16396 if (target == 0
16397 || GET_MODE (target) != tmode
16398 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16399 target = gen_reg_rtx (tmode);
16401 pat = GEN_FCN (icode) (target);
16402 if (! pat)
16403 return 0;
16404 emit_insn (pat);
16405 return target;
16407 case ALTIVEC_BUILTIN_MTVSCR:
16408 icode = CODE_FOR_altivec_mtvscr;
16409 arg0 = CALL_EXPR_ARG (exp, 0);
16410 op0 = expand_normal (arg0);
16411 mode0 = insn_data[icode].operand[0].mode;
16413 /* If we got invalid arguments bail out before generating bad rtl. */
16414 if (arg0 == error_mark_node)
16415 return const0_rtx;
16417 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16418 op0 = copy_to_mode_reg (mode0, op0);
16420 pat = GEN_FCN (icode) (op0);
16421 if (pat)
16422 emit_insn (pat);
16423 return NULL_RTX;
16425 case ALTIVEC_BUILTIN_DSSALL:
16426 emit_insn (gen_altivec_dssall ());
16427 return NULL_RTX;
16429 case ALTIVEC_BUILTIN_DSS:
16430 icode = CODE_FOR_altivec_dss;
16431 arg0 = CALL_EXPR_ARG (exp, 0);
16432 STRIP_NOPS (arg0);
16433 op0 = expand_normal (arg0);
16434 mode0 = insn_data[icode].operand[0].mode;
16436 /* If we got invalid arguments bail out before generating bad rtl. */
16437 if (arg0 == error_mark_node)
16438 return const0_rtx;
16440 if (TREE_CODE (arg0) != INTEGER_CST
16441 || TREE_INT_CST_LOW (arg0) & ~0x3)
16443 error ("argument to dss must be a 2-bit unsigned literal");
16444 return const0_rtx;
16447 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16448 op0 = copy_to_mode_reg (mode0, op0);
16450 emit_insn (gen_altivec_dss (op0));
16451 return NULL_RTX;
16453 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
16454 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
16455 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
16456 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
16457 case VSX_BUILTIN_VEC_INIT_V2DF:
16458 case VSX_BUILTIN_VEC_INIT_V2DI:
16459 case VSX_BUILTIN_VEC_INIT_V1TI:
16460 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
16462 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
16463 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
16464 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
16465 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
16466 case VSX_BUILTIN_VEC_SET_V2DF:
16467 case VSX_BUILTIN_VEC_SET_V2DI:
16468 case VSX_BUILTIN_VEC_SET_V1TI:
16469 return altivec_expand_vec_set_builtin (exp);
16471 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
16472 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
16473 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
16474 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
16475 case VSX_BUILTIN_VEC_EXT_V2DF:
16476 case VSX_BUILTIN_VEC_EXT_V2DI:
16477 case VSX_BUILTIN_VEC_EXT_V1TI:
16478 return altivec_expand_vec_ext_builtin (exp, target);
16480 case P9V_BUILTIN_VEXTRACT4B:
16481 case P9V_BUILTIN_VEC_VEXTRACT4B:
16482 arg1 = CALL_EXPR_ARG (exp, 1);
16483 STRIP_NOPS (arg1);
16485 /* Generate a normal call if it is invalid. */
16486 if (arg1 == error_mark_node)
16487 return expand_call (exp, target, false);
16489 if (TREE_CODE (arg1) != INTEGER_CST || TREE_INT_CST_LOW (arg1) > 12)
16491 error ("second argument to vec_vextract4b must be 0..12");
16492 return expand_call (exp, target, false);
16494 break;
16496 case P9V_BUILTIN_VINSERT4B:
16497 case P9V_BUILTIN_VINSERT4B_DI:
16498 case P9V_BUILTIN_VEC_VINSERT4B:
16499 arg2 = CALL_EXPR_ARG (exp, 2);
16500 STRIP_NOPS (arg2);
16502 /* Generate a normal call if it is invalid. */
16503 if (arg2 == error_mark_node)
16504 return expand_call (exp, target, false);
16506 if (TREE_CODE (arg2) != INTEGER_CST || TREE_INT_CST_LOW (arg2) > 12)
16508 error ("third argument to vec_vinsert4b must be 0..12");
16509 return expand_call (exp, target, false);
16511 break;
16513 default:
16514 break;
16515 /* Fall through. */
16518 /* Expand abs* operations. */
16519 d = bdesc_abs;
16520 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
16521 if (d->code == fcode)
16522 return altivec_expand_abs_builtin (d->icode, exp, target);
16524 /* Expand the AltiVec predicates. */
16525 d = bdesc_altivec_preds;
16526 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
16527 if (d->code == fcode)
16528 return altivec_expand_predicate_builtin (d->icode, exp, target);
16530 /* LV* are funky. We initialized them differently. */
16531 switch (fcode)
16533 case ALTIVEC_BUILTIN_LVSL:
16534 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
16535 exp, target, false);
16536 case ALTIVEC_BUILTIN_LVSR:
16537 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
16538 exp, target, false);
16539 case ALTIVEC_BUILTIN_LVEBX:
16540 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
16541 exp, target, false);
16542 case ALTIVEC_BUILTIN_LVEHX:
16543 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
16544 exp, target, false);
16545 case ALTIVEC_BUILTIN_LVEWX:
16546 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
16547 exp, target, false);
16548 case ALTIVEC_BUILTIN_LVXL_V2DF:
16549 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
16550 exp, target, false);
16551 case ALTIVEC_BUILTIN_LVXL_V2DI:
16552 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
16553 exp, target, false);
16554 case ALTIVEC_BUILTIN_LVXL_V4SF:
16555 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
16556 exp, target, false);
16557 case ALTIVEC_BUILTIN_LVXL:
16558 case ALTIVEC_BUILTIN_LVXL_V4SI:
16559 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
16560 exp, target, false);
16561 case ALTIVEC_BUILTIN_LVXL_V8HI:
16562 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
16563 exp, target, false);
16564 case ALTIVEC_BUILTIN_LVXL_V16QI:
16565 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
16566 exp, target, false);
16567 case ALTIVEC_BUILTIN_LVX_V2DF:
16568 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df_2op,
16569 exp, target, false);
16570 case ALTIVEC_BUILTIN_LVX_V2DI:
16571 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di_2op,
16572 exp, target, false);
16573 case ALTIVEC_BUILTIN_LVX_V4SF:
16574 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf_2op,
16575 exp, target, false);
16576 case ALTIVEC_BUILTIN_LVX:
16577 case ALTIVEC_BUILTIN_LVX_V4SI:
16578 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si_2op,
16579 exp, target, false);
16580 case ALTIVEC_BUILTIN_LVX_V8HI:
16581 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi_2op,
16582 exp, target, false);
16583 case ALTIVEC_BUILTIN_LVX_V16QI:
16584 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi_2op,
16585 exp, target, false);
16586 case ALTIVEC_BUILTIN_LVLX:
16587 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
16588 exp, target, true);
16589 case ALTIVEC_BUILTIN_LVLXL:
16590 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
16591 exp, target, true);
16592 case ALTIVEC_BUILTIN_LVRX:
16593 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
16594 exp, target, true);
16595 case ALTIVEC_BUILTIN_LVRXL:
16596 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
16597 exp, target, true);
16598 case VSX_BUILTIN_LXVD2X_V1TI:
16599 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
16600 exp, target, false);
16601 case VSX_BUILTIN_LXVD2X_V2DF:
16602 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
16603 exp, target, false);
16604 case VSX_BUILTIN_LXVD2X_V2DI:
16605 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
16606 exp, target, false);
16607 case VSX_BUILTIN_LXVW4X_V4SF:
16608 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
16609 exp, target, false);
16610 case VSX_BUILTIN_LXVW4X_V4SI:
16611 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
16612 exp, target, false);
16613 case VSX_BUILTIN_LXVW4X_V8HI:
16614 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
16615 exp, target, false);
16616 case VSX_BUILTIN_LXVW4X_V16QI:
16617 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
16618 exp, target, false);
16619 /* For the following on big endian, it's ok to use any appropriate
16620 unaligned-supporting load, so use a generic expander. For
16621 little-endian, the exact element-reversing instruction must
16622 be used. */
16623 case VSX_BUILTIN_LD_ELEMREV_V2DF:
16625 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2df
16626 : CODE_FOR_vsx_ld_elemrev_v2df);
16627 return altivec_expand_lv_builtin (code, exp, target, false);
16629 case VSX_BUILTIN_LD_ELEMREV_V2DI:
16631 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2di
16632 : CODE_FOR_vsx_ld_elemrev_v2di);
16633 return altivec_expand_lv_builtin (code, exp, target, false);
16635 case VSX_BUILTIN_LD_ELEMREV_V4SF:
16637 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4sf
16638 : CODE_FOR_vsx_ld_elemrev_v4sf);
16639 return altivec_expand_lv_builtin (code, exp, target, false);
16641 case VSX_BUILTIN_LD_ELEMREV_V4SI:
16643 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4si
16644 : CODE_FOR_vsx_ld_elemrev_v4si);
16645 return altivec_expand_lv_builtin (code, exp, target, false);
16647 case VSX_BUILTIN_LD_ELEMREV_V8HI:
16649 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v8hi
16650 : CODE_FOR_vsx_ld_elemrev_v8hi);
16651 return altivec_expand_lv_builtin (code, exp, target, false);
16653 case VSX_BUILTIN_LD_ELEMREV_V16QI:
16655 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v16qi
16656 : CODE_FOR_vsx_ld_elemrev_v16qi);
16657 return altivec_expand_lv_builtin (code, exp, target, false);
16659 break;
16660 default:
16661 break;
16662 /* Fall through. */
16665 *expandedp = false;
16666 return NULL_RTX;
16669 /* Expand the builtin in EXP and store the result in TARGET. Store
16670 true in *EXPANDEDP if we found a builtin to expand. */
16671 static rtx
16672 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
16674 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16675 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16676 const struct builtin_description *d;
16677 size_t i;
16679 *expandedp = true;
16681 switch (fcode)
16683 case PAIRED_BUILTIN_STX:
16684 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
16685 case PAIRED_BUILTIN_LX:
16686 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
16687 default:
16688 break;
16689 /* Fall through. */
16692 /* Expand the paired predicates. */
16693 d = bdesc_paired_preds;
16694 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
16695 if (d->code == fcode)
16696 return paired_expand_predicate_builtin (d->icode, exp, target);
16698 *expandedp = false;
16699 return NULL_RTX;
16702 /* Binops that need to be initialized manually, but can be expanded
16703 automagically by rs6000_expand_binop_builtin. */
16704 static const struct builtin_description bdesc_2arg_spe[] =
16706 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
16707 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
16708 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
16709 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
16710 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
16711 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
16712 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
16713 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
16714 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
16715 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
16716 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
16717 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
16718 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
16719 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
16720 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
16721 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
16722 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
16723 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
16724 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
16725 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
16726 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
16727 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
16730 /* Expand the builtin in EXP and store the result in TARGET. Store
16731 true in *EXPANDEDP if we found a builtin to expand.
16733 This expands the SPE builtins that are not simple unary and binary
16734 operations. */
16735 static rtx
16736 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
16738 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16739 tree arg1, arg0;
16740 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16741 enum insn_code icode;
16742 machine_mode tmode, mode0;
16743 rtx pat, op0;
16744 const struct builtin_description *d;
16745 size_t i;
16747 *expandedp = true;
16749 /* Syntax check for a 5-bit unsigned immediate. */
16750 switch (fcode)
16752 case SPE_BUILTIN_EVSTDD:
16753 case SPE_BUILTIN_EVSTDH:
16754 case SPE_BUILTIN_EVSTDW:
16755 case SPE_BUILTIN_EVSTWHE:
16756 case SPE_BUILTIN_EVSTWHO:
16757 case SPE_BUILTIN_EVSTWWE:
16758 case SPE_BUILTIN_EVSTWWO:
16759 arg1 = CALL_EXPR_ARG (exp, 2);
16760 if (TREE_CODE (arg1) != INTEGER_CST
16761 || TREE_INT_CST_LOW (arg1) & ~0x1f)
16763 error ("argument 2 must be a 5-bit unsigned literal");
16764 return const0_rtx;
16766 break;
16767 default:
16768 break;
16771 /* The evsplat*i instructions are not quite generic. */
16772 switch (fcode)
16774 case SPE_BUILTIN_EVSPLATFI:
16775 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
16776 exp, target);
16777 case SPE_BUILTIN_EVSPLATI:
16778 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
16779 exp, target);
16780 default:
16781 break;
16784 d = bdesc_2arg_spe;
16785 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
16786 if (d->code == fcode)
16787 return rs6000_expand_binop_builtin (d->icode, exp, target);
16789 d = bdesc_spe_predicates;
16790 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
16791 if (d->code == fcode)
16792 return spe_expand_predicate_builtin (d->icode, exp, target);
16794 d = bdesc_spe_evsel;
16795 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
16796 if (d->code == fcode)
16797 return spe_expand_evsel_builtin (d->icode, exp, target);
16799 switch (fcode)
16801 case SPE_BUILTIN_EVSTDDX:
16802 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
16803 case SPE_BUILTIN_EVSTDHX:
16804 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
16805 case SPE_BUILTIN_EVSTDWX:
16806 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
16807 case SPE_BUILTIN_EVSTWHEX:
16808 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
16809 case SPE_BUILTIN_EVSTWHOX:
16810 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
16811 case SPE_BUILTIN_EVSTWWEX:
16812 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
16813 case SPE_BUILTIN_EVSTWWOX:
16814 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
16815 case SPE_BUILTIN_EVSTDD:
16816 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
16817 case SPE_BUILTIN_EVSTDH:
16818 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
16819 case SPE_BUILTIN_EVSTDW:
16820 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
16821 case SPE_BUILTIN_EVSTWHE:
16822 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
16823 case SPE_BUILTIN_EVSTWHO:
16824 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
16825 case SPE_BUILTIN_EVSTWWE:
16826 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
16827 case SPE_BUILTIN_EVSTWWO:
16828 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
16829 case SPE_BUILTIN_MFSPEFSCR:
16830 icode = CODE_FOR_spe_mfspefscr;
16831 tmode = insn_data[icode].operand[0].mode;
16833 if (target == 0
16834 || GET_MODE (target) != tmode
16835 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16836 target = gen_reg_rtx (tmode);
16838 pat = GEN_FCN (icode) (target);
16839 if (! pat)
16840 return 0;
16841 emit_insn (pat);
16842 return target;
16843 case SPE_BUILTIN_MTSPEFSCR:
16844 icode = CODE_FOR_spe_mtspefscr;
16845 arg0 = CALL_EXPR_ARG (exp, 0);
16846 op0 = expand_normal (arg0);
16847 mode0 = insn_data[icode].operand[0].mode;
16849 if (arg0 == error_mark_node)
16850 return const0_rtx;
16852 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16853 op0 = copy_to_mode_reg (mode0, op0);
16855 pat = GEN_FCN (icode) (op0);
16856 if (pat)
16857 emit_insn (pat);
16858 return NULL_RTX;
16859 default:
16860 break;
16863 *expandedp = false;
16864 return NULL_RTX;
16867 static rtx
16868 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
16870 rtx pat, scratch, tmp;
16871 tree form = CALL_EXPR_ARG (exp, 0);
16872 tree arg0 = CALL_EXPR_ARG (exp, 1);
16873 tree arg1 = CALL_EXPR_ARG (exp, 2);
16874 rtx op0 = expand_normal (arg0);
16875 rtx op1 = expand_normal (arg1);
16876 machine_mode mode0 = insn_data[icode].operand[1].mode;
16877 machine_mode mode1 = insn_data[icode].operand[2].mode;
16878 int form_int;
16879 enum rtx_code code;
16881 if (TREE_CODE (form) != INTEGER_CST)
16883 error ("argument 1 of __builtin_paired_predicate must be a constant");
16884 return const0_rtx;
16886 else
16887 form_int = TREE_INT_CST_LOW (form);
16889 gcc_assert (mode0 == mode1);
16891 if (arg0 == error_mark_node || arg1 == error_mark_node)
16892 return const0_rtx;
16894 if (target == 0
16895 || GET_MODE (target) != SImode
16896 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
16897 target = gen_reg_rtx (SImode);
16898 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
16899 op0 = copy_to_mode_reg (mode0, op0);
16900 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
16901 op1 = copy_to_mode_reg (mode1, op1);
16903 scratch = gen_reg_rtx (CCFPmode);
16905 pat = GEN_FCN (icode) (scratch, op0, op1);
16906 if (!pat)
16907 return const0_rtx;
16909 emit_insn (pat);
16911 switch (form_int)
16913 /* LT bit. */
16914 case 0:
16915 code = LT;
16916 break;
16917 /* GT bit. */
16918 case 1:
16919 code = GT;
16920 break;
16921 /* EQ bit. */
16922 case 2:
16923 code = EQ;
16924 break;
16925 /* UN bit. */
16926 case 3:
16927 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
16928 return target;
16929 default:
16930 error ("argument 1 of __builtin_paired_predicate is out of range");
16931 return const0_rtx;
16934 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
16935 emit_move_insn (target, tmp);
16936 return target;
16939 static rtx
16940 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
16942 rtx pat, scratch, tmp;
16943 tree form = CALL_EXPR_ARG (exp, 0);
16944 tree arg0 = CALL_EXPR_ARG (exp, 1);
16945 tree arg1 = CALL_EXPR_ARG (exp, 2);
16946 rtx op0 = expand_normal (arg0);
16947 rtx op1 = expand_normal (arg1);
16948 machine_mode mode0 = insn_data[icode].operand[1].mode;
16949 machine_mode mode1 = insn_data[icode].operand[2].mode;
16950 int form_int;
16951 enum rtx_code code;
16953 if (TREE_CODE (form) != INTEGER_CST)
16955 error ("argument 1 of __builtin_spe_predicate must be a constant");
16956 return const0_rtx;
16958 else
16959 form_int = TREE_INT_CST_LOW (form);
16961 gcc_assert (mode0 == mode1);
16963 if (arg0 == error_mark_node || arg1 == error_mark_node)
16964 return const0_rtx;
16966 if (target == 0
16967 || GET_MODE (target) != SImode
16968 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
16969 target = gen_reg_rtx (SImode);
16971 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16972 op0 = copy_to_mode_reg (mode0, op0);
16973 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16974 op1 = copy_to_mode_reg (mode1, op1);
16976 scratch = gen_reg_rtx (CCmode);
16978 pat = GEN_FCN (icode) (scratch, op0, op1);
16979 if (! pat)
16980 return const0_rtx;
16981 emit_insn (pat);
16983 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
16984 _lower_. We use one compare, but look in different bits of the
16985 CR for each variant.
16987 There are 2 elements in each SPE simd type (upper/lower). The CR
16988 bits are set as follows:
16990 BIT0 | BIT 1 | BIT 2 | BIT 3
16991 U | L | (U | L) | (U & L)
16993 So, for an "all" relationship, BIT 3 would be set.
16994 For an "any" relationship, BIT 2 would be set. Etc.
16996 Following traditional nomenclature, these bits map to:
16998 BIT0 | BIT 1 | BIT 2 | BIT 3
16999 LT | GT | EQ | OV
17001 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
17004 switch (form_int)
17006 /* All variant. OV bit. */
17007 case 0:
17008 /* We need to get to the OV bit, which is the ORDERED bit. We
17009 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
17010 that's ugly and will make validate_condition_mode die.
17011 So let's just use another pattern. */
17012 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
17013 return target;
17014 /* Any variant. EQ bit. */
17015 case 1:
17016 code = EQ;
17017 break;
17018 /* Upper variant. LT bit. */
17019 case 2:
17020 code = LT;
17021 break;
17022 /* Lower variant. GT bit. */
17023 case 3:
17024 code = GT;
17025 break;
17026 default:
17027 error ("argument 1 of __builtin_spe_predicate is out of range");
17028 return const0_rtx;
17031 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
17032 emit_move_insn (target, tmp);
17034 return target;
17037 /* The evsel builtins look like this:
17039 e = __builtin_spe_evsel_OP (a, b, c, d);
17041 and work like this:
17043 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
17044 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
17047 static rtx
17048 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
17050 rtx pat, scratch;
17051 tree arg0 = CALL_EXPR_ARG (exp, 0);
17052 tree arg1 = CALL_EXPR_ARG (exp, 1);
17053 tree arg2 = CALL_EXPR_ARG (exp, 2);
17054 tree arg3 = CALL_EXPR_ARG (exp, 3);
17055 rtx op0 = expand_normal (arg0);
17056 rtx op1 = expand_normal (arg1);
17057 rtx op2 = expand_normal (arg2);
17058 rtx op3 = expand_normal (arg3);
17059 machine_mode mode0 = insn_data[icode].operand[1].mode;
17060 machine_mode mode1 = insn_data[icode].operand[2].mode;
17062 gcc_assert (mode0 == mode1);
17064 if (arg0 == error_mark_node || arg1 == error_mark_node
17065 || arg2 == error_mark_node || arg3 == error_mark_node)
17066 return const0_rtx;
17068 if (target == 0
17069 || GET_MODE (target) != mode0
17070 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
17071 target = gen_reg_rtx (mode0);
17073 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17074 op0 = copy_to_mode_reg (mode0, op0);
17075 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
17076 op1 = copy_to_mode_reg (mode0, op1);
17077 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
17078 op2 = copy_to_mode_reg (mode0, op2);
17079 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
17080 op3 = copy_to_mode_reg (mode0, op3);
17082 /* Generate the compare. */
17083 scratch = gen_reg_rtx (CCmode);
17084 pat = GEN_FCN (icode) (scratch, op0, op1);
17085 if (! pat)
17086 return const0_rtx;
17087 emit_insn (pat);
17089 if (mode0 == V2SImode)
17090 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
17091 else
17092 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
17094 return target;
17097 /* Raise an error message for a builtin function that is called without the
17098 appropriate target options being set. */
17100 static void
17101 rs6000_invalid_builtin (enum rs6000_builtins fncode)
17103 size_t uns_fncode = (size_t)fncode;
17104 const char *name = rs6000_builtin_info[uns_fncode].name;
17105 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
17107 gcc_assert (name != NULL);
17108 if ((fnmask & RS6000_BTM_CELL) != 0)
17109 error ("Builtin function %s is only valid for the cell processor", name);
17110 else if ((fnmask & RS6000_BTM_VSX) != 0)
17111 error ("Builtin function %s requires the -mvsx option", name);
17112 else if ((fnmask & RS6000_BTM_HTM) != 0)
17113 error ("Builtin function %s requires the -mhtm option", name);
17114 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
17115 error ("Builtin function %s requires the -maltivec option", name);
17116 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
17117 error ("Builtin function %s requires the -mpaired option", name);
17118 else if ((fnmask & RS6000_BTM_SPE) != 0)
17119 error ("Builtin function %s requires the -mspe option", name);
17120 else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
17121 == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
17122 error ("Builtin function %s requires the -mhard-dfp and"
17123 " -mpower8-vector options", name);
17124 else if ((fnmask & RS6000_BTM_DFP) != 0)
17125 error ("Builtin function %s requires the -mhard-dfp option", name);
17126 else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
17127 error ("Builtin function %s requires the -mpower8-vector option", name);
17128 else if ((fnmask & (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
17129 == (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
17130 error ("Builtin function %s requires the -mcpu=power9 and"
17131 " -m64 options", name);
17132 else if ((fnmask & RS6000_BTM_P9_VECTOR) != 0)
17133 error ("Builtin function %s requires the -mcpu=power9 option", name);
17134 else if ((fnmask & (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
17135 == (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
17136 error ("Builtin function %s requires the -mcpu=power9 and"
17137 " -m64 options", name);
17138 else if ((fnmask & RS6000_BTM_P9_MISC) == RS6000_BTM_P9_MISC)
17139 error ("Builtin function %s requires the -mcpu=power9 option", name);
17140 else if ((fnmask & (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
17141 == (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
17142 error ("Builtin function %s requires the -mhard-float and"
17143 " -mlong-double-128 options", name);
17144 else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
17145 error ("Builtin function %s requires the -mhard-float option", name);
17146 else if ((fnmask & RS6000_BTM_FLOAT128) != 0)
17147 error ("Builtin function %s requires the -mfloat128 option", name);
17148 else
17149 error ("Builtin function %s is not supported with the current options",
17150 name);
17153 /* Target hook for early folding of built-ins, shamelessly stolen
17154 from ia64.c. */
17156 static tree
17157 rs6000_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED,
17158 tree *args, bool ignore ATTRIBUTE_UNUSED)
17160 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
17162 enum rs6000_builtins fn_code
17163 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
17164 switch (fn_code)
17166 case RS6000_BUILTIN_NANQ:
17167 case RS6000_BUILTIN_NANSQ:
17169 tree type = TREE_TYPE (TREE_TYPE (fndecl));
17170 const char *str = c_getstr (*args);
17171 int quiet = fn_code == RS6000_BUILTIN_NANQ;
17172 REAL_VALUE_TYPE real;
17174 if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
17175 return build_real (type, real);
17176 return NULL_TREE;
17178 case RS6000_BUILTIN_INFQ:
17179 case RS6000_BUILTIN_HUGE_VALQ:
17181 tree type = TREE_TYPE (TREE_TYPE (fndecl));
17182 REAL_VALUE_TYPE inf;
17183 real_inf (&inf);
17184 return build_real (type, inf);
17186 default:
17187 break;
17190 #ifdef SUBTARGET_FOLD_BUILTIN
17191 return SUBTARGET_FOLD_BUILTIN (fndecl, n_args, args, ignore);
17192 #else
17193 return NULL_TREE;
17194 #endif
17197 /* Fold a machine-dependent built-in in GIMPLE. (For folding into
17198 a constant, use rs6000_fold_builtin.) */
17200 bool
17201 rs6000_gimple_fold_builtin (gimple_stmt_iterator *gsi)
17203 gimple *stmt = gsi_stmt (*gsi);
17204 tree fndecl = gimple_call_fndecl (stmt);
17205 gcc_checking_assert (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD);
17206 enum rs6000_builtins fn_code
17207 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
17208 tree arg0, arg1, lhs;
17210 switch (fn_code)
17212 /* Flavors of vec_add. We deliberately don't expand
17213 P8V_BUILTIN_VADDUQM as it gets lowered from V1TImode to
17214 TImode, resulting in much poorer code generation. */
17215 case ALTIVEC_BUILTIN_VADDUBM:
17216 case ALTIVEC_BUILTIN_VADDUHM:
17217 case ALTIVEC_BUILTIN_VADDUWM:
17218 case P8V_BUILTIN_VADDUDM:
17219 case ALTIVEC_BUILTIN_VADDFP:
17220 case VSX_BUILTIN_XVADDDP:
17222 arg0 = gimple_call_arg (stmt, 0);
17223 arg1 = gimple_call_arg (stmt, 1);
17224 lhs = gimple_call_lhs (stmt);
17225 gimple *g = gimple_build_assign (lhs, PLUS_EXPR, arg0, arg1);
17226 gimple_set_location (g, gimple_location (stmt));
17227 gsi_replace (gsi, g, true);
17228 return true;
17230 /* Flavors of vec_sub. We deliberately don't expand
17231 P8V_BUILTIN_VSUBUQM. */
17232 case ALTIVEC_BUILTIN_VSUBUBM:
17233 case ALTIVEC_BUILTIN_VSUBUHM:
17234 case ALTIVEC_BUILTIN_VSUBUWM:
17235 case P8V_BUILTIN_VSUBUDM:
17236 case ALTIVEC_BUILTIN_VSUBFP:
17237 case VSX_BUILTIN_XVSUBDP:
17239 arg0 = gimple_call_arg (stmt, 0);
17240 arg1 = gimple_call_arg (stmt, 1);
17241 lhs = gimple_call_lhs (stmt);
17242 gimple *g = gimple_build_assign (lhs, MINUS_EXPR, arg0, arg1);
17243 gimple_set_location (g, gimple_location (stmt));
17244 gsi_replace (gsi, g, true);
17245 return true;
17247 case VSX_BUILTIN_XVMULSP:
17248 case VSX_BUILTIN_XVMULDP:
17250 arg0 = gimple_call_arg (stmt, 0);
17251 arg1 = gimple_call_arg (stmt, 1);
17252 lhs = gimple_call_lhs (stmt);
17253 gimple *g = gimple_build_assign (lhs, MULT_EXPR, arg0, arg1);
17254 gimple_set_location (g, gimple_location (stmt));
17255 gsi_replace (gsi, g, true);
17256 return true;
17258 /* Even element flavors of vec_mul (signed). */
17259 case ALTIVEC_BUILTIN_VMULESB:
17260 case ALTIVEC_BUILTIN_VMULESH:
17261 /* Even element flavors of vec_mul (unsigned). */
17262 case ALTIVEC_BUILTIN_VMULEUB:
17263 case ALTIVEC_BUILTIN_VMULEUH:
17265 arg0 = gimple_call_arg (stmt, 0);
17266 arg1 = gimple_call_arg (stmt, 1);
17267 lhs = gimple_call_lhs (stmt);
17268 gimple *g = gimple_build_assign (lhs, VEC_WIDEN_MULT_EVEN_EXPR, arg0, arg1);
17269 gimple_set_location (g, gimple_location (stmt));
17270 gsi_replace (gsi, g, true);
17271 return true;
17273 /* Odd element flavors of vec_mul (signed). */
17274 case ALTIVEC_BUILTIN_VMULOSB:
17275 case ALTIVEC_BUILTIN_VMULOSH:
17276 /* Odd element flavors of vec_mul (unsigned). */
17277 case ALTIVEC_BUILTIN_VMULOUB:
17278 case ALTIVEC_BUILTIN_VMULOUH:
17280 arg0 = gimple_call_arg (stmt, 0);
17281 arg1 = gimple_call_arg (stmt, 1);
17282 lhs = gimple_call_lhs (stmt);
17283 gimple *g = gimple_build_assign (lhs, VEC_WIDEN_MULT_ODD_EXPR, arg0, arg1);
17284 gimple_set_location (g, gimple_location (stmt));
17285 gsi_replace (gsi, g, true);
17286 return true;
17288 /* Flavors of vec_div (Integer). */
17289 case VSX_BUILTIN_DIV_V2DI:
17290 case VSX_BUILTIN_UDIV_V2DI:
17292 arg0 = gimple_call_arg (stmt, 0);
17293 arg1 = gimple_call_arg (stmt, 1);
17294 lhs = gimple_call_lhs (stmt);
17295 gimple *g = gimple_build_assign (lhs, TRUNC_DIV_EXPR, arg0, arg1);
17296 gimple_set_location (g, gimple_location (stmt));
17297 gsi_replace (gsi, g, true);
17298 return true;
17300 /* Flavors of vec_div (Float). */
17301 case VSX_BUILTIN_XVDIVSP:
17302 case VSX_BUILTIN_XVDIVDP:
17304 arg0 = gimple_call_arg (stmt, 0);
17305 arg1 = gimple_call_arg (stmt, 1);
17306 lhs = gimple_call_lhs (stmt);
17307 gimple *g = gimple_build_assign (lhs, RDIV_EXPR, arg0, arg1);
17308 gimple_set_location (g, gimple_location (stmt));
17309 gsi_replace (gsi, g, true);
17310 return true;
17312 /* Flavors of vec_and. */
17313 case ALTIVEC_BUILTIN_VAND:
17315 arg0 = gimple_call_arg (stmt, 0);
17316 arg1 = gimple_call_arg (stmt, 1);
17317 lhs = gimple_call_lhs (stmt);
17318 gimple *g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, arg1);
17319 gimple_set_location (g, gimple_location (stmt));
17320 gsi_replace (gsi, g, true);
17321 return true;
17323 /* Flavors of vec_andc. */
17324 case ALTIVEC_BUILTIN_VANDC:
17326 arg0 = gimple_call_arg (stmt, 0);
17327 arg1 = gimple_call_arg (stmt, 1);
17328 lhs = gimple_call_lhs (stmt);
17329 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17330 gimple *g = gimple_build_assign(temp, BIT_NOT_EXPR, arg1);
17331 gimple_set_location (g, gimple_location (stmt));
17332 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17333 g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, temp);
17334 gimple_set_location (g, gimple_location (stmt));
17335 gsi_replace (gsi, g, true);
17336 return true;
17338 /* Flavors of vec_nand. */
17339 case P8V_BUILTIN_VEC_NAND:
17340 case P8V_BUILTIN_NAND_V16QI:
17341 case P8V_BUILTIN_NAND_V8HI:
17342 case P8V_BUILTIN_NAND_V4SI:
17343 case P8V_BUILTIN_NAND_V4SF:
17344 case P8V_BUILTIN_NAND_V2DF:
17345 case P8V_BUILTIN_NAND_V2DI:
17347 arg0 = gimple_call_arg (stmt, 0);
17348 arg1 = gimple_call_arg (stmt, 1);
17349 lhs = gimple_call_lhs (stmt);
17350 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17351 gimple *g = gimple_build_assign(temp, BIT_AND_EXPR, arg0, arg1);
17352 gimple_set_location (g, gimple_location (stmt));
17353 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17354 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
17355 gimple_set_location (g, gimple_location (stmt));
17356 gsi_replace (gsi, g, true);
17357 return true;
17359 /* Flavors of vec_or. */
17360 case ALTIVEC_BUILTIN_VOR:
17362 arg0 = gimple_call_arg (stmt, 0);
17363 arg1 = gimple_call_arg (stmt, 1);
17364 lhs = gimple_call_lhs (stmt);
17365 gimple *g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, arg1);
17366 gimple_set_location (g, gimple_location (stmt));
17367 gsi_replace (gsi, g, true);
17368 return true;
17370 /* flavors of vec_orc. */
17371 case P8V_BUILTIN_ORC_V16QI:
17372 case P8V_BUILTIN_ORC_V8HI:
17373 case P8V_BUILTIN_ORC_V4SI:
17374 case P8V_BUILTIN_ORC_V4SF:
17375 case P8V_BUILTIN_ORC_V2DF:
17376 case P8V_BUILTIN_ORC_V2DI:
17378 arg0 = gimple_call_arg (stmt, 0);
17379 arg1 = gimple_call_arg (stmt, 1);
17380 lhs = gimple_call_lhs (stmt);
17381 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17382 gimple *g = gimple_build_assign(temp, BIT_NOT_EXPR, arg1);
17383 gimple_set_location (g, gimple_location (stmt));
17384 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17385 g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, temp);
17386 gimple_set_location (g, gimple_location (stmt));
17387 gsi_replace (gsi, g, true);
17388 return true;
17390 /* Flavors of vec_xor. */
17391 case ALTIVEC_BUILTIN_VXOR:
17393 arg0 = gimple_call_arg (stmt, 0);
17394 arg1 = gimple_call_arg (stmt, 1);
17395 lhs = gimple_call_lhs (stmt);
17396 gimple *g = gimple_build_assign (lhs, BIT_XOR_EXPR, arg0, arg1);
17397 gimple_set_location (g, gimple_location (stmt));
17398 gsi_replace (gsi, g, true);
17399 return true;
17401 /* Flavors of vec_nor. */
17402 case ALTIVEC_BUILTIN_VNOR:
17404 arg0 = gimple_call_arg (stmt, 0);
17405 arg1 = gimple_call_arg (stmt, 1);
17406 lhs = gimple_call_lhs (stmt);
17407 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
17408 gimple *g = gimple_build_assign (temp, BIT_IOR_EXPR, arg0, arg1);
17409 gimple_set_location (g, gimple_location (stmt));
17410 gsi_insert_before(gsi, g, GSI_SAME_STMT);
17411 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
17412 gimple_set_location (g, gimple_location (stmt));
17413 gsi_replace (gsi, g, true);
17414 return true;
17416 default:
17417 break;
17420 return false;
17423 /* Expand an expression EXP that calls a built-in function,
17424 with result going to TARGET if that's convenient
17425 (and in mode MODE if that's convenient).
17426 SUBTARGET may be used as the target for computing one of EXP's operands.
17427 IGNORE is nonzero if the value is to be ignored. */
17429 static rtx
17430 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17431 machine_mode mode ATTRIBUTE_UNUSED,
17432 int ignore ATTRIBUTE_UNUSED)
17434 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17435 enum rs6000_builtins fcode
17436 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
17437 size_t uns_fcode = (size_t)fcode;
17438 const struct builtin_description *d;
17439 size_t i;
17440 rtx ret;
17441 bool success;
17442 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
17443 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
17445 if (TARGET_DEBUG_BUILTIN)
17447 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
17448 const char *name1 = rs6000_builtin_info[uns_fcode].name;
17449 const char *name2 = ((icode != CODE_FOR_nothing)
17450 ? get_insn_name ((int)icode)
17451 : "nothing");
17452 const char *name3;
17454 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
17456 default: name3 = "unknown"; break;
17457 case RS6000_BTC_SPECIAL: name3 = "special"; break;
17458 case RS6000_BTC_UNARY: name3 = "unary"; break;
17459 case RS6000_BTC_BINARY: name3 = "binary"; break;
17460 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
17461 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
17462 case RS6000_BTC_ABS: name3 = "abs"; break;
17463 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
17464 case RS6000_BTC_DST: name3 = "dst"; break;
17468 fprintf (stderr,
17469 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
17470 (name1) ? name1 : "---", fcode,
17471 (name2) ? name2 : "---", (int)icode,
17472 name3,
17473 func_valid_p ? "" : ", not valid");
17476 if (!func_valid_p)
17478 rs6000_invalid_builtin (fcode);
17480 /* Given it is invalid, just generate a normal call. */
17481 return expand_call (exp, target, ignore);
17484 switch (fcode)
17486 case RS6000_BUILTIN_RECIP:
17487 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
17489 case RS6000_BUILTIN_RECIPF:
17490 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
17492 case RS6000_BUILTIN_RSQRTF:
17493 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
17495 case RS6000_BUILTIN_RSQRT:
17496 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
17498 case POWER7_BUILTIN_BPERMD:
17499 return rs6000_expand_binop_builtin (((TARGET_64BIT)
17500 ? CODE_FOR_bpermd_di
17501 : CODE_FOR_bpermd_si), exp, target);
17503 case RS6000_BUILTIN_GET_TB:
17504 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
17505 target);
17507 case RS6000_BUILTIN_MFTB:
17508 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
17509 ? CODE_FOR_rs6000_mftb_di
17510 : CODE_FOR_rs6000_mftb_si),
17511 target);
17513 case RS6000_BUILTIN_MFFS:
17514 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
17516 case RS6000_BUILTIN_MTFSF:
17517 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
17519 case RS6000_BUILTIN_CPU_INIT:
17520 case RS6000_BUILTIN_CPU_IS:
17521 case RS6000_BUILTIN_CPU_SUPPORTS:
17522 return cpu_expand_builtin (fcode, exp, target);
17524 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
17525 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
17527 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
17528 : (int) CODE_FOR_altivec_lvsl_direct);
17529 machine_mode tmode = insn_data[icode].operand[0].mode;
17530 machine_mode mode = insn_data[icode].operand[1].mode;
17531 tree arg;
17532 rtx op, addr, pat;
17534 gcc_assert (TARGET_ALTIVEC);
17536 arg = CALL_EXPR_ARG (exp, 0);
17537 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
17538 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
17539 addr = memory_address (mode, op);
17540 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
17541 op = addr;
17542 else
17544 /* For the load case need to negate the address. */
17545 op = gen_reg_rtx (GET_MODE (addr));
17546 emit_insn (gen_rtx_SET (op, gen_rtx_NEG (GET_MODE (addr), addr)));
17548 op = gen_rtx_MEM (mode, op);
17550 if (target == 0
17551 || GET_MODE (target) != tmode
17552 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17553 target = gen_reg_rtx (tmode);
17555 pat = GEN_FCN (icode) (target, op);
17556 if (!pat)
17557 return 0;
17558 emit_insn (pat);
17560 return target;
17563 case ALTIVEC_BUILTIN_VCFUX:
17564 case ALTIVEC_BUILTIN_VCFSX:
17565 case ALTIVEC_BUILTIN_VCTUXS:
17566 case ALTIVEC_BUILTIN_VCTSXS:
17567 /* FIXME: There's got to be a nicer way to handle this case than
17568 constructing a new CALL_EXPR. */
17569 if (call_expr_nargs (exp) == 1)
17571 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
17572 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
17574 break;
17576 default:
17577 break;
17580 if (TARGET_ALTIVEC)
17582 ret = altivec_expand_builtin (exp, target, &success);
17584 if (success)
17585 return ret;
17587 if (TARGET_SPE)
17589 ret = spe_expand_builtin (exp, target, &success);
17591 if (success)
17592 return ret;
17594 if (TARGET_PAIRED_FLOAT)
17596 ret = paired_expand_builtin (exp, target, &success);
17598 if (success)
17599 return ret;
17601 if (TARGET_HTM)
17603 ret = htm_expand_builtin (exp, target, &success);
17605 if (success)
17606 return ret;
17609 unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
17610 /* RS6000_BTC_SPECIAL represents no-operand operators. */
17611 gcc_assert (attr == RS6000_BTC_UNARY
17612 || attr == RS6000_BTC_BINARY
17613 || attr == RS6000_BTC_TERNARY
17614 || attr == RS6000_BTC_SPECIAL);
17616 /* Handle simple unary operations. */
17617 d = bdesc_1arg;
17618 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17619 if (d->code == fcode)
17620 return rs6000_expand_unop_builtin (d->icode, exp, target);
17622 /* Handle simple binary operations. */
17623 d = bdesc_2arg;
17624 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17625 if (d->code == fcode)
17626 return rs6000_expand_binop_builtin (d->icode, exp, target);
17628 /* Handle simple ternary operations. */
17629 d = bdesc_3arg;
17630 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
17631 if (d->code == fcode)
17632 return rs6000_expand_ternop_builtin (d->icode, exp, target);
17634 /* Handle simple no-argument operations. */
17635 d = bdesc_0arg;
17636 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
17637 if (d->code == fcode)
17638 return rs6000_expand_zeroop_builtin (d->icode, target);
17640 gcc_unreachable ();
17643 /* Create a builtin vector type with a name. Taking care not to give
17644 the canonical type a name. */
17646 static tree
17647 rs6000_vector_type (const char *name, tree elt_type, unsigned num_elts)
17649 tree result = build_vector_type (elt_type, num_elts);
17651 /* Copy so we don't give the canonical type a name. */
17652 result = build_variant_type_copy (result);
17654 add_builtin_type (name, result);
17656 return result;
17659 static void
17660 rs6000_init_builtins (void)
17662 tree tdecl;
17663 tree ftype;
17664 machine_mode mode;
17666 if (TARGET_DEBUG_BUILTIN)
17667 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
17668 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
17669 (TARGET_SPE) ? ", spe" : "",
17670 (TARGET_ALTIVEC) ? ", altivec" : "",
17671 (TARGET_VSX) ? ", vsx" : "");
17673 V2SI_type_node = build_vector_type (intSI_type_node, 2);
17674 V2SF_type_node = build_vector_type (float_type_node, 2);
17675 V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64 ? "__vector long"
17676 : "__vector long long",
17677 intDI_type_node, 2);
17678 V2DF_type_node = rs6000_vector_type ("__vector double", double_type_node, 2);
17679 V4HI_type_node = build_vector_type (intHI_type_node, 4);
17680 V4SI_type_node = rs6000_vector_type ("__vector signed int",
17681 intSI_type_node, 4);
17682 V4SF_type_node = rs6000_vector_type ("__vector float", float_type_node, 4);
17683 V8HI_type_node = rs6000_vector_type ("__vector signed short",
17684 intHI_type_node, 8);
17685 V16QI_type_node = rs6000_vector_type ("__vector signed char",
17686 intQI_type_node, 16);
17688 unsigned_V16QI_type_node = rs6000_vector_type ("__vector unsigned char",
17689 unsigned_intQI_type_node, 16);
17690 unsigned_V8HI_type_node = rs6000_vector_type ("__vector unsigned short",
17691 unsigned_intHI_type_node, 8);
17692 unsigned_V4SI_type_node = rs6000_vector_type ("__vector unsigned int",
17693 unsigned_intSI_type_node, 4);
17694 unsigned_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
17695 ? "__vector unsigned long"
17696 : "__vector unsigned long long",
17697 unsigned_intDI_type_node, 2);
17699 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
17700 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
17701 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
17702 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
17704 const_str_type_node
17705 = build_pointer_type (build_qualified_type (char_type_node,
17706 TYPE_QUAL_CONST));
17708 /* We use V1TI mode as a special container to hold __int128_t items that
17709 must live in VSX registers. */
17710 if (intTI_type_node)
17712 V1TI_type_node = rs6000_vector_type ("__vector __int128",
17713 intTI_type_node, 1);
17714 unsigned_V1TI_type_node
17715 = rs6000_vector_type ("__vector unsigned __int128",
17716 unsigned_intTI_type_node, 1);
17719 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
17720 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
17721 'vector unsigned short'. */
17723 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
17724 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
17725 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
17726 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
17727 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
17729 long_integer_type_internal_node = long_integer_type_node;
17730 long_unsigned_type_internal_node = long_unsigned_type_node;
17731 long_long_integer_type_internal_node = long_long_integer_type_node;
17732 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
17733 intQI_type_internal_node = intQI_type_node;
17734 uintQI_type_internal_node = unsigned_intQI_type_node;
17735 intHI_type_internal_node = intHI_type_node;
17736 uintHI_type_internal_node = unsigned_intHI_type_node;
17737 intSI_type_internal_node = intSI_type_node;
17738 uintSI_type_internal_node = unsigned_intSI_type_node;
17739 intDI_type_internal_node = intDI_type_node;
17740 uintDI_type_internal_node = unsigned_intDI_type_node;
17741 intTI_type_internal_node = intTI_type_node;
17742 uintTI_type_internal_node = unsigned_intTI_type_node;
17743 float_type_internal_node = float_type_node;
17744 double_type_internal_node = double_type_node;
17745 long_double_type_internal_node = long_double_type_node;
17746 dfloat64_type_internal_node = dfloat64_type_node;
17747 dfloat128_type_internal_node = dfloat128_type_node;
17748 void_type_internal_node = void_type_node;
17750 /* 128-bit floating point support. KFmode is IEEE 128-bit floating point.
17751 IFmode is the IBM extended 128-bit format that is a pair of doubles.
17752 TFmode will be either IEEE 128-bit floating point or the IBM double-double
17753 format that uses a pair of doubles, depending on the switches and
17754 defaults.
17756 We do not enable the actual __float128 keyword unless the user explicitly
17757 asks for it, because the library support is not yet complete.
17759 If we don't support for either 128-bit IBM double double or IEEE 128-bit
17760 floating point, we need make sure the type is non-zero or else self-test
17761 fails during bootstrap.
17763 We don't register a built-in type for __ibm128 if the type is the same as
17764 long double. Instead we add a #define for __ibm128 in
17765 rs6000_cpu_cpp_builtins to long double. */
17766 if (TARGET_LONG_DOUBLE_128 && FLOAT128_IEEE_P (TFmode))
17768 ibm128_float_type_node = make_node (REAL_TYPE);
17769 TYPE_PRECISION (ibm128_float_type_node) = 128;
17770 SET_TYPE_MODE (ibm128_float_type_node, IFmode);
17771 layout_type (ibm128_float_type_node);
17773 lang_hooks.types.register_builtin_type (ibm128_float_type_node,
17774 "__ibm128");
17776 else
17777 ibm128_float_type_node = long_double_type_node;
17779 if (TARGET_FLOAT128_KEYWORD)
17781 ieee128_float_type_node = float128_type_node;
17782 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
17783 "__float128");
17786 else if (TARGET_FLOAT128_TYPE)
17788 ieee128_float_type_node = make_node (REAL_TYPE);
17789 TYPE_PRECISION (ibm128_float_type_node) = 128;
17790 SET_TYPE_MODE (ieee128_float_type_node, KFmode);
17791 layout_type (ieee128_float_type_node);
17793 /* If we are not exporting the __float128/_Float128 keywords, we need a
17794 keyword to get the types created. Use __ieee128 as the dummy
17795 keyword. */
17796 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
17797 "__ieee128");
17800 else
17801 ieee128_float_type_node = long_double_type_node;
17803 /* Initialize the modes for builtin_function_type, mapping a machine mode to
17804 tree type node. */
17805 builtin_mode_to_type[QImode][0] = integer_type_node;
17806 builtin_mode_to_type[HImode][0] = integer_type_node;
17807 builtin_mode_to_type[SImode][0] = intSI_type_node;
17808 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
17809 builtin_mode_to_type[DImode][0] = intDI_type_node;
17810 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
17811 builtin_mode_to_type[TImode][0] = intTI_type_node;
17812 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
17813 builtin_mode_to_type[SFmode][0] = float_type_node;
17814 builtin_mode_to_type[DFmode][0] = double_type_node;
17815 builtin_mode_to_type[IFmode][0] = ibm128_float_type_node;
17816 builtin_mode_to_type[KFmode][0] = ieee128_float_type_node;
17817 builtin_mode_to_type[TFmode][0] = long_double_type_node;
17818 builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
17819 builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
17820 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
17821 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
17822 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
17823 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
17824 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
17825 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
17826 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
17827 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
17828 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
17829 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
17830 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
17831 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
17832 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
17833 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
17834 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
17836 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
17837 TYPE_NAME (bool_char_type_node) = tdecl;
17839 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
17840 TYPE_NAME (bool_short_type_node) = tdecl;
17842 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
17843 TYPE_NAME (bool_int_type_node) = tdecl;
17845 tdecl = add_builtin_type ("__pixel", pixel_type_node);
17846 TYPE_NAME (pixel_type_node) = tdecl;
17848 bool_V16QI_type_node = rs6000_vector_type ("__vector __bool char",
17849 bool_char_type_node, 16);
17850 bool_V8HI_type_node = rs6000_vector_type ("__vector __bool short",
17851 bool_short_type_node, 8);
17852 bool_V4SI_type_node = rs6000_vector_type ("__vector __bool int",
17853 bool_int_type_node, 4);
17854 bool_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
17855 ? "__vector __bool long"
17856 : "__vector __bool long long",
17857 bool_long_type_node, 2);
17858 pixel_V8HI_type_node = rs6000_vector_type ("__vector __pixel",
17859 pixel_type_node, 8);
17861 /* Paired and SPE builtins are only available if you build a compiler with
17862 the appropriate options, so only create those builtins with the
17863 appropriate compiler option. Create Altivec and VSX builtins on machines
17864 with at least the general purpose extensions (970 and newer) to allow the
17865 use of the target attribute. */
17866 if (TARGET_PAIRED_FLOAT)
17867 paired_init_builtins ();
17868 if (TARGET_SPE)
17869 spe_init_builtins ();
17870 if (TARGET_EXTRA_BUILTINS)
17871 altivec_init_builtins ();
17872 if (TARGET_HTM)
17873 htm_init_builtins ();
17875 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
17876 rs6000_common_init_builtins ();
17878 ftype = build_function_type_list (ieee128_float_type_node,
17879 const_str_type_node, NULL_TREE);
17880 def_builtin ("__builtin_nanq", ftype, RS6000_BUILTIN_NANQ);
17881 def_builtin ("__builtin_nansq", ftype, RS6000_BUILTIN_NANSQ);
17883 ftype = build_function_type_list (ieee128_float_type_node, NULL_TREE);
17884 def_builtin ("__builtin_infq", ftype, RS6000_BUILTIN_INFQ);
17885 def_builtin ("__builtin_huge_valq", ftype, RS6000_BUILTIN_HUGE_VALQ);
17887 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
17888 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
17889 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
17891 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
17892 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
17893 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
17895 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
17896 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
17897 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
17899 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
17900 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
17901 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
17903 mode = (TARGET_64BIT) ? DImode : SImode;
17904 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
17905 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
17906 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
17908 ftype = build_function_type_list (unsigned_intDI_type_node,
17909 NULL_TREE);
17910 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
17912 if (TARGET_64BIT)
17913 ftype = build_function_type_list (unsigned_intDI_type_node,
17914 NULL_TREE);
17915 else
17916 ftype = build_function_type_list (unsigned_intSI_type_node,
17917 NULL_TREE);
17918 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
17920 ftype = build_function_type_list (double_type_node, NULL_TREE);
17921 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
17923 ftype = build_function_type_list (void_type_node,
17924 intSI_type_node, double_type_node,
17925 NULL_TREE);
17926 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
17928 ftype = build_function_type_list (void_type_node, NULL_TREE);
17929 def_builtin ("__builtin_cpu_init", ftype, RS6000_BUILTIN_CPU_INIT);
17931 ftype = build_function_type_list (bool_int_type_node, const_ptr_type_node,
17932 NULL_TREE);
17933 def_builtin ("__builtin_cpu_is", ftype, RS6000_BUILTIN_CPU_IS);
17934 def_builtin ("__builtin_cpu_supports", ftype, RS6000_BUILTIN_CPU_SUPPORTS);
17936 /* AIX libm provides clog as __clog. */
17937 if (TARGET_XCOFF &&
17938 (tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
17939 set_user_assembler_name (tdecl, "__clog");
17941 #ifdef SUBTARGET_INIT_BUILTINS
17942 SUBTARGET_INIT_BUILTINS;
17943 #endif
17946 /* Returns the rs6000 builtin decl for CODE. */
17948 static tree
17949 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
17951 HOST_WIDE_INT fnmask;
17953 if (code >= RS6000_BUILTIN_COUNT)
17954 return error_mark_node;
17956 fnmask = rs6000_builtin_info[code].mask;
17957 if ((fnmask & rs6000_builtin_mask) != fnmask)
17959 rs6000_invalid_builtin ((enum rs6000_builtins)code);
17960 return error_mark_node;
17963 return rs6000_builtin_decls[code];
17966 static void
17967 spe_init_builtins (void)
17969 tree puint_type_node = build_pointer_type (unsigned_type_node);
17970 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
17971 const struct builtin_description *d;
17972 size_t i;
17973 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
17975 tree v2si_ftype_4_v2si
17976 = build_function_type_list (opaque_V2SI_type_node,
17977 opaque_V2SI_type_node,
17978 opaque_V2SI_type_node,
17979 opaque_V2SI_type_node,
17980 opaque_V2SI_type_node,
17981 NULL_TREE);
17983 tree v2sf_ftype_4_v2sf
17984 = build_function_type_list (opaque_V2SF_type_node,
17985 opaque_V2SF_type_node,
17986 opaque_V2SF_type_node,
17987 opaque_V2SF_type_node,
17988 opaque_V2SF_type_node,
17989 NULL_TREE);
17991 tree int_ftype_int_v2si_v2si
17992 = build_function_type_list (integer_type_node,
17993 integer_type_node,
17994 opaque_V2SI_type_node,
17995 opaque_V2SI_type_node,
17996 NULL_TREE);
17998 tree int_ftype_int_v2sf_v2sf
17999 = build_function_type_list (integer_type_node,
18000 integer_type_node,
18001 opaque_V2SF_type_node,
18002 opaque_V2SF_type_node,
18003 NULL_TREE);
18005 tree void_ftype_v2si_puint_int
18006 = build_function_type_list (void_type_node,
18007 opaque_V2SI_type_node,
18008 puint_type_node,
18009 integer_type_node,
18010 NULL_TREE);
18012 tree void_ftype_v2si_puint_char
18013 = build_function_type_list (void_type_node,
18014 opaque_V2SI_type_node,
18015 puint_type_node,
18016 char_type_node,
18017 NULL_TREE);
18019 tree void_ftype_v2si_pv2si_int
18020 = build_function_type_list (void_type_node,
18021 opaque_V2SI_type_node,
18022 opaque_p_V2SI_type_node,
18023 integer_type_node,
18024 NULL_TREE);
18026 tree void_ftype_v2si_pv2si_char
18027 = build_function_type_list (void_type_node,
18028 opaque_V2SI_type_node,
18029 opaque_p_V2SI_type_node,
18030 char_type_node,
18031 NULL_TREE);
18033 tree void_ftype_int
18034 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
18036 tree int_ftype_void
18037 = build_function_type_list (integer_type_node, NULL_TREE);
18039 tree v2si_ftype_pv2si_int
18040 = build_function_type_list (opaque_V2SI_type_node,
18041 opaque_p_V2SI_type_node,
18042 integer_type_node,
18043 NULL_TREE);
18045 tree v2si_ftype_puint_int
18046 = build_function_type_list (opaque_V2SI_type_node,
18047 puint_type_node,
18048 integer_type_node,
18049 NULL_TREE);
18051 tree v2si_ftype_pushort_int
18052 = build_function_type_list (opaque_V2SI_type_node,
18053 pushort_type_node,
18054 integer_type_node,
18055 NULL_TREE);
18057 tree v2si_ftype_signed_char
18058 = build_function_type_list (opaque_V2SI_type_node,
18059 signed_char_type_node,
18060 NULL_TREE);
18062 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
18064 /* Initialize irregular SPE builtins. */
18066 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
18067 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
18068 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
18069 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
18070 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
18071 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
18072 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
18073 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
18074 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
18075 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
18076 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
18077 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
18078 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
18079 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
18080 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
18081 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
18082 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
18083 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
18085 /* Loads. */
18086 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
18087 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
18088 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
18089 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
18090 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
18091 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
18092 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
18093 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
18094 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
18095 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
18096 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
18097 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
18098 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
18099 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
18100 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
18101 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
18102 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
18103 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
18104 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
18105 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
18106 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
18107 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
18109 /* Predicates. */
18110 d = bdesc_spe_predicates;
18111 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
18113 tree type;
18114 HOST_WIDE_INT mask = d->mask;
18116 if ((mask & builtin_mask) != mask)
18118 if (TARGET_DEBUG_BUILTIN)
18119 fprintf (stderr, "spe_init_builtins, skip predicate %s\n",
18120 d->name);
18121 continue;
18124 /* Cannot define builtin if the instruction is disabled. */
18125 gcc_assert (d->icode != CODE_FOR_nothing);
18126 switch (insn_data[d->icode].operand[1].mode)
18128 case E_V2SImode:
18129 type = int_ftype_int_v2si_v2si;
18130 break;
18131 case E_V2SFmode:
18132 type = int_ftype_int_v2sf_v2sf;
18133 break;
18134 default:
18135 gcc_unreachable ();
18138 def_builtin (d->name, type, d->code);
18141 /* Evsel predicates. */
18142 d = bdesc_spe_evsel;
18143 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
18145 tree type;
18146 HOST_WIDE_INT mask = d->mask;
18148 if ((mask & builtin_mask) != mask)
18150 if (TARGET_DEBUG_BUILTIN)
18151 fprintf (stderr, "spe_init_builtins, skip evsel %s\n",
18152 d->name);
18153 continue;
18156 /* Cannot define builtin if the instruction is disabled. */
18157 gcc_assert (d->icode != CODE_FOR_nothing);
18158 switch (insn_data[d->icode].operand[1].mode)
18160 case E_V2SImode:
18161 type = v2si_ftype_4_v2si;
18162 break;
18163 case E_V2SFmode:
18164 type = v2sf_ftype_4_v2sf;
18165 break;
18166 default:
18167 gcc_unreachable ();
18170 def_builtin (d->name, type, d->code);
18174 static void
18175 paired_init_builtins (void)
18177 const struct builtin_description *d;
18178 size_t i;
18179 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18181 tree int_ftype_int_v2sf_v2sf
18182 = build_function_type_list (integer_type_node,
18183 integer_type_node,
18184 V2SF_type_node,
18185 V2SF_type_node,
18186 NULL_TREE);
18187 tree pcfloat_type_node =
18188 build_pointer_type (build_qualified_type
18189 (float_type_node, TYPE_QUAL_CONST));
18191 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
18192 long_integer_type_node,
18193 pcfloat_type_node,
18194 NULL_TREE);
18195 tree void_ftype_v2sf_long_pcfloat =
18196 build_function_type_list (void_type_node,
18197 V2SF_type_node,
18198 long_integer_type_node,
18199 pcfloat_type_node,
18200 NULL_TREE);
18203 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
18204 PAIRED_BUILTIN_LX);
18207 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
18208 PAIRED_BUILTIN_STX);
18210 /* Predicates. */
18211 d = bdesc_paired_preds;
18212 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
18214 tree type;
18215 HOST_WIDE_INT mask = d->mask;
18217 if ((mask & builtin_mask) != mask)
18219 if (TARGET_DEBUG_BUILTIN)
18220 fprintf (stderr, "paired_init_builtins, skip predicate %s\n",
18221 d->name);
18222 continue;
18225 /* Cannot define builtin if the instruction is disabled. */
18226 gcc_assert (d->icode != CODE_FOR_nothing);
18228 if (TARGET_DEBUG_BUILTIN)
18229 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
18230 (int)i, get_insn_name (d->icode), (int)d->icode,
18231 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
18233 switch (insn_data[d->icode].operand[1].mode)
18235 case E_V2SFmode:
18236 type = int_ftype_int_v2sf_v2sf;
18237 break;
18238 default:
18239 gcc_unreachable ();
18242 def_builtin (d->name, type, d->code);
18246 static void
18247 altivec_init_builtins (void)
18249 const struct builtin_description *d;
18250 size_t i;
18251 tree ftype;
18252 tree decl;
18253 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18255 tree pvoid_type_node = build_pointer_type (void_type_node);
18257 tree pcvoid_type_node
18258 = build_pointer_type (build_qualified_type (void_type_node,
18259 TYPE_QUAL_CONST));
18261 tree int_ftype_opaque
18262 = build_function_type_list (integer_type_node,
18263 opaque_V4SI_type_node, NULL_TREE);
18264 tree opaque_ftype_opaque
18265 = build_function_type_list (integer_type_node, NULL_TREE);
18266 tree opaque_ftype_opaque_int
18267 = build_function_type_list (opaque_V4SI_type_node,
18268 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
18269 tree opaque_ftype_opaque_opaque_int
18270 = build_function_type_list (opaque_V4SI_type_node,
18271 opaque_V4SI_type_node, opaque_V4SI_type_node,
18272 integer_type_node, NULL_TREE);
18273 tree opaque_ftype_opaque_opaque_opaque
18274 = build_function_type_list (opaque_V4SI_type_node,
18275 opaque_V4SI_type_node, opaque_V4SI_type_node,
18276 opaque_V4SI_type_node, NULL_TREE);
18277 tree opaque_ftype_opaque_opaque
18278 = build_function_type_list (opaque_V4SI_type_node,
18279 opaque_V4SI_type_node, opaque_V4SI_type_node,
18280 NULL_TREE);
18281 tree int_ftype_int_opaque_opaque
18282 = build_function_type_list (integer_type_node,
18283 integer_type_node, opaque_V4SI_type_node,
18284 opaque_V4SI_type_node, NULL_TREE);
18285 tree int_ftype_int_v4si_v4si
18286 = build_function_type_list (integer_type_node,
18287 integer_type_node, V4SI_type_node,
18288 V4SI_type_node, NULL_TREE);
18289 tree int_ftype_int_v2di_v2di
18290 = build_function_type_list (integer_type_node,
18291 integer_type_node, V2DI_type_node,
18292 V2DI_type_node, NULL_TREE);
18293 tree void_ftype_v4si
18294 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
18295 tree v8hi_ftype_void
18296 = build_function_type_list (V8HI_type_node, NULL_TREE);
18297 tree void_ftype_void
18298 = build_function_type_list (void_type_node, NULL_TREE);
18299 tree void_ftype_int
18300 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
18302 tree opaque_ftype_long_pcvoid
18303 = build_function_type_list (opaque_V4SI_type_node,
18304 long_integer_type_node, pcvoid_type_node,
18305 NULL_TREE);
18306 tree v16qi_ftype_long_pcvoid
18307 = build_function_type_list (V16QI_type_node,
18308 long_integer_type_node, pcvoid_type_node,
18309 NULL_TREE);
18310 tree v8hi_ftype_long_pcvoid
18311 = build_function_type_list (V8HI_type_node,
18312 long_integer_type_node, pcvoid_type_node,
18313 NULL_TREE);
18314 tree v4si_ftype_long_pcvoid
18315 = build_function_type_list (V4SI_type_node,
18316 long_integer_type_node, pcvoid_type_node,
18317 NULL_TREE);
18318 tree v4sf_ftype_long_pcvoid
18319 = build_function_type_list (V4SF_type_node,
18320 long_integer_type_node, pcvoid_type_node,
18321 NULL_TREE);
18322 tree v2df_ftype_long_pcvoid
18323 = build_function_type_list (V2DF_type_node,
18324 long_integer_type_node, pcvoid_type_node,
18325 NULL_TREE);
18326 tree v2di_ftype_long_pcvoid
18327 = build_function_type_list (V2DI_type_node,
18328 long_integer_type_node, pcvoid_type_node,
18329 NULL_TREE);
18331 tree void_ftype_opaque_long_pvoid
18332 = build_function_type_list (void_type_node,
18333 opaque_V4SI_type_node, long_integer_type_node,
18334 pvoid_type_node, NULL_TREE);
18335 tree void_ftype_v4si_long_pvoid
18336 = build_function_type_list (void_type_node,
18337 V4SI_type_node, long_integer_type_node,
18338 pvoid_type_node, NULL_TREE);
18339 tree void_ftype_v16qi_long_pvoid
18340 = build_function_type_list (void_type_node,
18341 V16QI_type_node, long_integer_type_node,
18342 pvoid_type_node, NULL_TREE);
18344 tree void_ftype_v16qi_pvoid_long
18345 = build_function_type_list (void_type_node,
18346 V16QI_type_node, pvoid_type_node,
18347 long_integer_type_node, NULL_TREE);
18349 tree void_ftype_v8hi_long_pvoid
18350 = build_function_type_list (void_type_node,
18351 V8HI_type_node, long_integer_type_node,
18352 pvoid_type_node, NULL_TREE);
18353 tree void_ftype_v4sf_long_pvoid
18354 = build_function_type_list (void_type_node,
18355 V4SF_type_node, long_integer_type_node,
18356 pvoid_type_node, NULL_TREE);
18357 tree void_ftype_v2df_long_pvoid
18358 = build_function_type_list (void_type_node,
18359 V2DF_type_node, long_integer_type_node,
18360 pvoid_type_node, NULL_TREE);
18361 tree void_ftype_v2di_long_pvoid
18362 = build_function_type_list (void_type_node,
18363 V2DI_type_node, long_integer_type_node,
18364 pvoid_type_node, NULL_TREE);
18365 tree int_ftype_int_v8hi_v8hi
18366 = build_function_type_list (integer_type_node,
18367 integer_type_node, V8HI_type_node,
18368 V8HI_type_node, NULL_TREE);
18369 tree int_ftype_int_v16qi_v16qi
18370 = build_function_type_list (integer_type_node,
18371 integer_type_node, V16QI_type_node,
18372 V16QI_type_node, NULL_TREE);
18373 tree int_ftype_int_v4sf_v4sf
18374 = build_function_type_list (integer_type_node,
18375 integer_type_node, V4SF_type_node,
18376 V4SF_type_node, NULL_TREE);
18377 tree int_ftype_int_v2df_v2df
18378 = build_function_type_list (integer_type_node,
18379 integer_type_node, V2DF_type_node,
18380 V2DF_type_node, NULL_TREE);
18381 tree v2di_ftype_v2di
18382 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
18383 tree v4si_ftype_v4si
18384 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
18385 tree v8hi_ftype_v8hi
18386 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
18387 tree v16qi_ftype_v16qi
18388 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
18389 tree v4sf_ftype_v4sf
18390 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
18391 tree v2df_ftype_v2df
18392 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
18393 tree void_ftype_pcvoid_int_int
18394 = build_function_type_list (void_type_node,
18395 pcvoid_type_node, integer_type_node,
18396 integer_type_node, NULL_TREE);
18398 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
18399 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
18400 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
18401 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
18402 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
18403 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
18404 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
18405 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
18406 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
18407 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
18408 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
18409 ALTIVEC_BUILTIN_LVXL_V2DF);
18410 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
18411 ALTIVEC_BUILTIN_LVXL_V2DI);
18412 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
18413 ALTIVEC_BUILTIN_LVXL_V4SF);
18414 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
18415 ALTIVEC_BUILTIN_LVXL_V4SI);
18416 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
18417 ALTIVEC_BUILTIN_LVXL_V8HI);
18418 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
18419 ALTIVEC_BUILTIN_LVXL_V16QI);
18420 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
18421 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
18422 ALTIVEC_BUILTIN_LVX_V2DF);
18423 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
18424 ALTIVEC_BUILTIN_LVX_V2DI);
18425 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
18426 ALTIVEC_BUILTIN_LVX_V4SF);
18427 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
18428 ALTIVEC_BUILTIN_LVX_V4SI);
18429 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
18430 ALTIVEC_BUILTIN_LVX_V8HI);
18431 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
18432 ALTIVEC_BUILTIN_LVX_V16QI);
18433 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
18434 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
18435 ALTIVEC_BUILTIN_STVX_V2DF);
18436 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
18437 ALTIVEC_BUILTIN_STVX_V2DI);
18438 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
18439 ALTIVEC_BUILTIN_STVX_V4SF);
18440 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
18441 ALTIVEC_BUILTIN_STVX_V4SI);
18442 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
18443 ALTIVEC_BUILTIN_STVX_V8HI);
18444 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
18445 ALTIVEC_BUILTIN_STVX_V16QI);
18446 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
18447 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
18448 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
18449 ALTIVEC_BUILTIN_STVXL_V2DF);
18450 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
18451 ALTIVEC_BUILTIN_STVXL_V2DI);
18452 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
18453 ALTIVEC_BUILTIN_STVXL_V4SF);
18454 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
18455 ALTIVEC_BUILTIN_STVXL_V4SI);
18456 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
18457 ALTIVEC_BUILTIN_STVXL_V8HI);
18458 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
18459 ALTIVEC_BUILTIN_STVXL_V16QI);
18460 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
18461 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
18462 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
18463 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
18464 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
18465 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
18466 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
18467 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
18468 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
18469 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
18470 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
18471 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
18472 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
18473 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
18474 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
18475 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
18477 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
18478 VSX_BUILTIN_LXVD2X_V2DF);
18479 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
18480 VSX_BUILTIN_LXVD2X_V2DI);
18481 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
18482 VSX_BUILTIN_LXVW4X_V4SF);
18483 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
18484 VSX_BUILTIN_LXVW4X_V4SI);
18485 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
18486 VSX_BUILTIN_LXVW4X_V8HI);
18487 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
18488 VSX_BUILTIN_LXVW4X_V16QI);
18489 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
18490 VSX_BUILTIN_STXVD2X_V2DF);
18491 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
18492 VSX_BUILTIN_STXVD2X_V2DI);
18493 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
18494 VSX_BUILTIN_STXVW4X_V4SF);
18495 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
18496 VSX_BUILTIN_STXVW4X_V4SI);
18497 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
18498 VSX_BUILTIN_STXVW4X_V8HI);
18499 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
18500 VSX_BUILTIN_STXVW4X_V16QI);
18502 def_builtin ("__builtin_vsx_ld_elemrev_v2df", v2df_ftype_long_pcvoid,
18503 VSX_BUILTIN_LD_ELEMREV_V2DF);
18504 def_builtin ("__builtin_vsx_ld_elemrev_v2di", v2di_ftype_long_pcvoid,
18505 VSX_BUILTIN_LD_ELEMREV_V2DI);
18506 def_builtin ("__builtin_vsx_ld_elemrev_v4sf", v4sf_ftype_long_pcvoid,
18507 VSX_BUILTIN_LD_ELEMREV_V4SF);
18508 def_builtin ("__builtin_vsx_ld_elemrev_v4si", v4si_ftype_long_pcvoid,
18509 VSX_BUILTIN_LD_ELEMREV_V4SI);
18510 def_builtin ("__builtin_vsx_st_elemrev_v2df", void_ftype_v2df_long_pvoid,
18511 VSX_BUILTIN_ST_ELEMREV_V2DF);
18512 def_builtin ("__builtin_vsx_st_elemrev_v2di", void_ftype_v2di_long_pvoid,
18513 VSX_BUILTIN_ST_ELEMREV_V2DI);
18514 def_builtin ("__builtin_vsx_st_elemrev_v4sf", void_ftype_v4sf_long_pvoid,
18515 VSX_BUILTIN_ST_ELEMREV_V4SF);
18516 def_builtin ("__builtin_vsx_st_elemrev_v4si", void_ftype_v4si_long_pvoid,
18517 VSX_BUILTIN_ST_ELEMREV_V4SI);
18519 if (TARGET_P9_VECTOR)
18521 def_builtin ("__builtin_vsx_ld_elemrev_v8hi", v8hi_ftype_long_pcvoid,
18522 VSX_BUILTIN_LD_ELEMREV_V8HI);
18523 def_builtin ("__builtin_vsx_ld_elemrev_v16qi", v16qi_ftype_long_pcvoid,
18524 VSX_BUILTIN_LD_ELEMREV_V16QI);
18525 def_builtin ("__builtin_vsx_st_elemrev_v8hi",
18526 void_ftype_v8hi_long_pvoid, VSX_BUILTIN_ST_ELEMREV_V8HI);
18527 def_builtin ("__builtin_vsx_st_elemrev_v16qi",
18528 void_ftype_v16qi_long_pvoid, VSX_BUILTIN_ST_ELEMREV_V16QI);
18530 else
18532 rs6000_builtin_decls[(int) VSX_BUILTIN_LD_ELEMREV_V8HI]
18533 = rs6000_builtin_decls[(int) VSX_BUILTIN_LXVW4X_V8HI];
18534 rs6000_builtin_decls[(int) VSX_BUILTIN_LD_ELEMREV_V16QI]
18535 = rs6000_builtin_decls[(int) VSX_BUILTIN_LXVW4X_V16QI];
18536 rs6000_builtin_decls[(int) VSX_BUILTIN_ST_ELEMREV_V8HI]
18537 = rs6000_builtin_decls[(int) VSX_BUILTIN_STXVW4X_V8HI];
18538 rs6000_builtin_decls[(int) VSX_BUILTIN_ST_ELEMREV_V16QI]
18539 = rs6000_builtin_decls[(int) VSX_BUILTIN_STXVW4X_V16QI];
18542 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
18543 VSX_BUILTIN_VEC_LD);
18544 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
18545 VSX_BUILTIN_VEC_ST);
18546 def_builtin ("__builtin_vec_xl", opaque_ftype_long_pcvoid,
18547 VSX_BUILTIN_VEC_XL);
18548 def_builtin ("__builtin_vec_xst", void_ftype_opaque_long_pvoid,
18549 VSX_BUILTIN_VEC_XST);
18551 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
18552 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
18553 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
18555 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
18556 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
18557 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
18558 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
18559 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
18560 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
18561 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
18562 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
18563 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
18564 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
18565 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
18566 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
18568 def_builtin ("__builtin_vec_adde", opaque_ftype_opaque_opaque_opaque,
18569 ALTIVEC_BUILTIN_VEC_ADDE);
18570 def_builtin ("__builtin_vec_addec", opaque_ftype_opaque_opaque_opaque,
18571 ALTIVEC_BUILTIN_VEC_ADDEC);
18572 def_builtin ("__builtin_vec_cmpne", opaque_ftype_opaque_opaque,
18573 ALTIVEC_BUILTIN_VEC_CMPNE);
18574 def_builtin ("__builtin_vec_mul", opaque_ftype_opaque_opaque,
18575 ALTIVEC_BUILTIN_VEC_MUL);
18577 /* Cell builtins. */
18578 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
18579 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
18580 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
18581 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
18583 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
18584 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
18585 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
18586 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
18588 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
18589 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
18590 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
18591 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
18593 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
18594 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
18595 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
18596 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
18598 if (TARGET_P9_VECTOR)
18599 def_builtin ("__builtin_altivec_stxvl", void_ftype_v16qi_pvoid_long,
18600 P9V_BUILTIN_STXVL);
18602 /* Add the DST variants. */
18603 d = bdesc_dst;
18604 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
18606 HOST_WIDE_INT mask = d->mask;
18608 /* It is expected that these dst built-in functions may have
18609 d->icode equal to CODE_FOR_nothing. */
18610 if ((mask & builtin_mask) != mask)
18612 if (TARGET_DEBUG_BUILTIN)
18613 fprintf (stderr, "altivec_init_builtins, skip dst %s\n",
18614 d->name);
18615 continue;
18617 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
18620 /* Initialize the predicates. */
18621 d = bdesc_altivec_preds;
18622 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
18624 machine_mode mode1;
18625 tree type;
18626 HOST_WIDE_INT mask = d->mask;
18628 if ((mask & builtin_mask) != mask)
18630 if (TARGET_DEBUG_BUILTIN)
18631 fprintf (stderr, "altivec_init_builtins, skip predicate %s\n",
18632 d->name);
18633 continue;
18636 if (rs6000_overloaded_builtin_p (d->code))
18637 mode1 = VOIDmode;
18638 else
18640 /* Cannot define builtin if the instruction is disabled. */
18641 gcc_assert (d->icode != CODE_FOR_nothing);
18642 mode1 = insn_data[d->icode].operand[1].mode;
18645 switch (mode1)
18647 case E_VOIDmode:
18648 type = int_ftype_int_opaque_opaque;
18649 break;
18650 case E_V2DImode:
18651 type = int_ftype_int_v2di_v2di;
18652 break;
18653 case E_V4SImode:
18654 type = int_ftype_int_v4si_v4si;
18655 break;
18656 case E_V8HImode:
18657 type = int_ftype_int_v8hi_v8hi;
18658 break;
18659 case E_V16QImode:
18660 type = int_ftype_int_v16qi_v16qi;
18661 break;
18662 case E_V4SFmode:
18663 type = int_ftype_int_v4sf_v4sf;
18664 break;
18665 case E_V2DFmode:
18666 type = int_ftype_int_v2df_v2df;
18667 break;
18668 default:
18669 gcc_unreachable ();
18672 def_builtin (d->name, type, d->code);
18675 /* Initialize the abs* operators. */
18676 d = bdesc_abs;
18677 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
18679 machine_mode mode0;
18680 tree type;
18681 HOST_WIDE_INT mask = d->mask;
18683 if ((mask & builtin_mask) != mask)
18685 if (TARGET_DEBUG_BUILTIN)
18686 fprintf (stderr, "altivec_init_builtins, skip abs %s\n",
18687 d->name);
18688 continue;
18691 /* Cannot define builtin if the instruction is disabled. */
18692 gcc_assert (d->icode != CODE_FOR_nothing);
18693 mode0 = insn_data[d->icode].operand[0].mode;
18695 switch (mode0)
18697 case E_V2DImode:
18698 type = v2di_ftype_v2di;
18699 break;
18700 case E_V4SImode:
18701 type = v4si_ftype_v4si;
18702 break;
18703 case E_V8HImode:
18704 type = v8hi_ftype_v8hi;
18705 break;
18706 case E_V16QImode:
18707 type = v16qi_ftype_v16qi;
18708 break;
18709 case E_V4SFmode:
18710 type = v4sf_ftype_v4sf;
18711 break;
18712 case E_V2DFmode:
18713 type = v2df_ftype_v2df;
18714 break;
18715 default:
18716 gcc_unreachable ();
18719 def_builtin (d->name, type, d->code);
18722 /* Initialize target builtin that implements
18723 targetm.vectorize.builtin_mask_for_load. */
18725 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
18726 v16qi_ftype_long_pcvoid,
18727 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
18728 BUILT_IN_MD, NULL, NULL_TREE);
18729 TREE_READONLY (decl) = 1;
18730 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
18731 altivec_builtin_mask_for_load = decl;
18733 /* Access to the vec_init patterns. */
18734 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
18735 integer_type_node, integer_type_node,
18736 integer_type_node, NULL_TREE);
18737 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
18739 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
18740 short_integer_type_node,
18741 short_integer_type_node,
18742 short_integer_type_node,
18743 short_integer_type_node,
18744 short_integer_type_node,
18745 short_integer_type_node,
18746 short_integer_type_node, NULL_TREE);
18747 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
18749 ftype = build_function_type_list (V16QI_type_node, char_type_node,
18750 char_type_node, char_type_node,
18751 char_type_node, char_type_node,
18752 char_type_node, char_type_node,
18753 char_type_node, char_type_node,
18754 char_type_node, char_type_node,
18755 char_type_node, char_type_node,
18756 char_type_node, char_type_node,
18757 char_type_node, NULL_TREE);
18758 def_builtin ("__builtin_vec_init_v16qi", ftype,
18759 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
18761 ftype = build_function_type_list (V4SF_type_node, float_type_node,
18762 float_type_node, float_type_node,
18763 float_type_node, NULL_TREE);
18764 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
18766 /* VSX builtins. */
18767 ftype = build_function_type_list (V2DF_type_node, double_type_node,
18768 double_type_node, NULL_TREE);
18769 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
18771 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
18772 intDI_type_node, NULL_TREE);
18773 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
18775 /* Access to the vec_set patterns. */
18776 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
18777 intSI_type_node,
18778 integer_type_node, NULL_TREE);
18779 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
18781 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
18782 intHI_type_node,
18783 integer_type_node, NULL_TREE);
18784 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
18786 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
18787 intQI_type_node,
18788 integer_type_node, NULL_TREE);
18789 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
18791 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
18792 float_type_node,
18793 integer_type_node, NULL_TREE);
18794 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
18796 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
18797 double_type_node,
18798 integer_type_node, NULL_TREE);
18799 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
18801 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
18802 intDI_type_node,
18803 integer_type_node, NULL_TREE);
18804 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
18806 /* Access to the vec_extract patterns. */
18807 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
18808 integer_type_node, NULL_TREE);
18809 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
18811 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
18812 integer_type_node, NULL_TREE);
18813 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
18815 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
18816 integer_type_node, NULL_TREE);
18817 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
18819 ftype = build_function_type_list (float_type_node, V4SF_type_node,
18820 integer_type_node, NULL_TREE);
18821 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
18823 ftype = build_function_type_list (double_type_node, V2DF_type_node,
18824 integer_type_node, NULL_TREE);
18825 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
18827 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
18828 integer_type_node, NULL_TREE);
18829 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
18832 if (V1TI_type_node)
18834 tree v1ti_ftype_long_pcvoid
18835 = build_function_type_list (V1TI_type_node,
18836 long_integer_type_node, pcvoid_type_node,
18837 NULL_TREE);
18838 tree void_ftype_v1ti_long_pvoid
18839 = build_function_type_list (void_type_node,
18840 V1TI_type_node, long_integer_type_node,
18841 pvoid_type_node, NULL_TREE);
18842 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
18843 VSX_BUILTIN_LXVD2X_V1TI);
18844 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
18845 VSX_BUILTIN_STXVD2X_V1TI);
18846 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
18847 NULL_TREE, NULL_TREE);
18848 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
18849 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
18850 intTI_type_node,
18851 integer_type_node, NULL_TREE);
18852 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
18853 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
18854 integer_type_node, NULL_TREE);
18855 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
18860 static void
18861 htm_init_builtins (void)
18863 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18864 const struct builtin_description *d;
18865 size_t i;
18867 d = bdesc_htm;
18868 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
18870 tree op[MAX_HTM_OPERANDS], type;
18871 HOST_WIDE_INT mask = d->mask;
18872 unsigned attr = rs6000_builtin_info[d->code].attr;
18873 bool void_func = (attr & RS6000_BTC_VOID);
18874 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
18875 int nopnds = 0;
18876 tree gpr_type_node;
18877 tree rettype;
18878 tree argtype;
18880 /* It is expected that these htm built-in functions may have
18881 d->icode equal to CODE_FOR_nothing. */
18883 if (TARGET_32BIT && TARGET_POWERPC64)
18884 gpr_type_node = long_long_unsigned_type_node;
18885 else
18886 gpr_type_node = long_unsigned_type_node;
18888 if (attr & RS6000_BTC_SPR)
18890 rettype = gpr_type_node;
18891 argtype = gpr_type_node;
18893 else if (d->code == HTM_BUILTIN_TABORTDC
18894 || d->code == HTM_BUILTIN_TABORTDCI)
18896 rettype = unsigned_type_node;
18897 argtype = gpr_type_node;
18899 else
18901 rettype = unsigned_type_node;
18902 argtype = unsigned_type_node;
18905 if ((mask & builtin_mask) != mask)
18907 if (TARGET_DEBUG_BUILTIN)
18908 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
18909 continue;
18912 if (d->name == 0)
18914 if (TARGET_DEBUG_BUILTIN)
18915 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
18916 (long unsigned) i);
18917 continue;
18920 op[nopnds++] = (void_func) ? void_type_node : rettype;
18922 if (attr_args == RS6000_BTC_UNARY)
18923 op[nopnds++] = argtype;
18924 else if (attr_args == RS6000_BTC_BINARY)
18926 op[nopnds++] = argtype;
18927 op[nopnds++] = argtype;
18929 else if (attr_args == RS6000_BTC_TERNARY)
18931 op[nopnds++] = argtype;
18932 op[nopnds++] = argtype;
18933 op[nopnds++] = argtype;
18936 switch (nopnds)
18938 case 1:
18939 type = build_function_type_list (op[0], NULL_TREE);
18940 break;
18941 case 2:
18942 type = build_function_type_list (op[0], op[1], NULL_TREE);
18943 break;
18944 case 3:
18945 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
18946 break;
18947 case 4:
18948 type = build_function_type_list (op[0], op[1], op[2], op[3],
18949 NULL_TREE);
18950 break;
18951 default:
18952 gcc_unreachable ();
18955 def_builtin (d->name, type, d->code);
18959 /* Hash function for builtin functions with up to 3 arguments and a return
18960 type. */
18961 hashval_t
18962 builtin_hasher::hash (builtin_hash_struct *bh)
18964 unsigned ret = 0;
18965 int i;
18967 for (i = 0; i < 4; i++)
18969 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
18970 ret = (ret * 2) + bh->uns_p[i];
18973 return ret;
18976 /* Compare builtin hash entries H1 and H2 for equivalence. */
18977 bool
18978 builtin_hasher::equal (builtin_hash_struct *p1, builtin_hash_struct *p2)
18980 return ((p1->mode[0] == p2->mode[0])
18981 && (p1->mode[1] == p2->mode[1])
18982 && (p1->mode[2] == p2->mode[2])
18983 && (p1->mode[3] == p2->mode[3])
18984 && (p1->uns_p[0] == p2->uns_p[0])
18985 && (p1->uns_p[1] == p2->uns_p[1])
18986 && (p1->uns_p[2] == p2->uns_p[2])
18987 && (p1->uns_p[3] == p2->uns_p[3]));
18990 /* Map types for builtin functions with an explicit return type and up to 3
18991 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
18992 of the argument. */
18993 static tree
18994 builtin_function_type (machine_mode mode_ret, machine_mode mode_arg0,
18995 machine_mode mode_arg1, machine_mode mode_arg2,
18996 enum rs6000_builtins builtin, const char *name)
18998 struct builtin_hash_struct h;
18999 struct builtin_hash_struct *h2;
19000 int num_args = 3;
19001 int i;
19002 tree ret_type = NULL_TREE;
19003 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
19005 /* Create builtin_hash_table. */
19006 if (builtin_hash_table == NULL)
19007 builtin_hash_table = hash_table<builtin_hasher>::create_ggc (1500);
19009 h.type = NULL_TREE;
19010 h.mode[0] = mode_ret;
19011 h.mode[1] = mode_arg0;
19012 h.mode[2] = mode_arg1;
19013 h.mode[3] = mode_arg2;
19014 h.uns_p[0] = 0;
19015 h.uns_p[1] = 0;
19016 h.uns_p[2] = 0;
19017 h.uns_p[3] = 0;
19019 /* If the builtin is a type that produces unsigned results or takes unsigned
19020 arguments, and it is returned as a decl for the vectorizer (such as
19021 widening multiplies, permute), make sure the arguments and return value
19022 are type correct. */
19023 switch (builtin)
19025 /* unsigned 1 argument functions. */
19026 case CRYPTO_BUILTIN_VSBOX:
19027 case P8V_BUILTIN_VGBBD:
19028 case MISC_BUILTIN_CDTBCD:
19029 case MISC_BUILTIN_CBCDTD:
19030 h.uns_p[0] = 1;
19031 h.uns_p[1] = 1;
19032 break;
19034 /* unsigned 2 argument functions. */
19035 case ALTIVEC_BUILTIN_VMULEUB:
19036 case ALTIVEC_BUILTIN_VMULEUH:
19037 case ALTIVEC_BUILTIN_VMULOUB:
19038 case ALTIVEC_BUILTIN_VMULOUH:
19039 case CRYPTO_BUILTIN_VCIPHER:
19040 case CRYPTO_BUILTIN_VCIPHERLAST:
19041 case CRYPTO_BUILTIN_VNCIPHER:
19042 case CRYPTO_BUILTIN_VNCIPHERLAST:
19043 case CRYPTO_BUILTIN_VPMSUMB:
19044 case CRYPTO_BUILTIN_VPMSUMH:
19045 case CRYPTO_BUILTIN_VPMSUMW:
19046 case CRYPTO_BUILTIN_VPMSUMD:
19047 case CRYPTO_BUILTIN_VPMSUM:
19048 case MISC_BUILTIN_ADDG6S:
19049 case MISC_BUILTIN_DIVWEU:
19050 case MISC_BUILTIN_DIVWEUO:
19051 case MISC_BUILTIN_DIVDEU:
19052 case MISC_BUILTIN_DIVDEUO:
19053 case VSX_BUILTIN_UDIV_V2DI:
19054 h.uns_p[0] = 1;
19055 h.uns_p[1] = 1;
19056 h.uns_p[2] = 1;
19057 break;
19059 /* unsigned 3 argument functions. */
19060 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
19061 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
19062 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
19063 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
19064 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
19065 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
19066 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
19067 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
19068 case VSX_BUILTIN_VPERM_16QI_UNS:
19069 case VSX_BUILTIN_VPERM_8HI_UNS:
19070 case VSX_BUILTIN_VPERM_4SI_UNS:
19071 case VSX_BUILTIN_VPERM_2DI_UNS:
19072 case VSX_BUILTIN_XXSEL_16QI_UNS:
19073 case VSX_BUILTIN_XXSEL_8HI_UNS:
19074 case VSX_BUILTIN_XXSEL_4SI_UNS:
19075 case VSX_BUILTIN_XXSEL_2DI_UNS:
19076 case CRYPTO_BUILTIN_VPERMXOR:
19077 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
19078 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
19079 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
19080 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
19081 case CRYPTO_BUILTIN_VSHASIGMAW:
19082 case CRYPTO_BUILTIN_VSHASIGMAD:
19083 case CRYPTO_BUILTIN_VSHASIGMA:
19084 h.uns_p[0] = 1;
19085 h.uns_p[1] = 1;
19086 h.uns_p[2] = 1;
19087 h.uns_p[3] = 1;
19088 break;
19090 /* signed permute functions with unsigned char mask. */
19091 case ALTIVEC_BUILTIN_VPERM_16QI:
19092 case ALTIVEC_BUILTIN_VPERM_8HI:
19093 case ALTIVEC_BUILTIN_VPERM_4SI:
19094 case ALTIVEC_BUILTIN_VPERM_4SF:
19095 case ALTIVEC_BUILTIN_VPERM_2DI:
19096 case ALTIVEC_BUILTIN_VPERM_2DF:
19097 case VSX_BUILTIN_VPERM_16QI:
19098 case VSX_BUILTIN_VPERM_8HI:
19099 case VSX_BUILTIN_VPERM_4SI:
19100 case VSX_BUILTIN_VPERM_4SF:
19101 case VSX_BUILTIN_VPERM_2DI:
19102 case VSX_BUILTIN_VPERM_2DF:
19103 h.uns_p[3] = 1;
19104 break;
19106 /* unsigned args, signed return. */
19107 case VSX_BUILTIN_XVCVUXDSP:
19108 case VSX_BUILTIN_XVCVUXDDP_UNS:
19109 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
19110 h.uns_p[1] = 1;
19111 break;
19113 /* signed args, unsigned return. */
19114 case VSX_BUILTIN_XVCVDPUXDS_UNS:
19115 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
19116 case MISC_BUILTIN_UNPACK_TD:
19117 case MISC_BUILTIN_UNPACK_V1TI:
19118 h.uns_p[0] = 1;
19119 break;
19121 /* unsigned arguments for 128-bit pack instructions. */
19122 case MISC_BUILTIN_PACK_TD:
19123 case MISC_BUILTIN_PACK_V1TI:
19124 h.uns_p[1] = 1;
19125 h.uns_p[2] = 1;
19126 break;
19128 default:
19129 break;
19132 /* Figure out how many args are present. */
19133 while (num_args > 0 && h.mode[num_args] == VOIDmode)
19134 num_args--;
19136 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
19137 if (!ret_type && h.uns_p[0])
19138 ret_type = builtin_mode_to_type[h.mode[0]][0];
19140 if (!ret_type)
19141 fatal_error (input_location,
19142 "internal error: builtin function %s had an unexpected "
19143 "return type %s", name, GET_MODE_NAME (h.mode[0]));
19145 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
19146 arg_type[i] = NULL_TREE;
19148 for (i = 0; i < num_args; i++)
19150 int m = (int) h.mode[i+1];
19151 int uns_p = h.uns_p[i+1];
19153 arg_type[i] = builtin_mode_to_type[m][uns_p];
19154 if (!arg_type[i] && uns_p)
19155 arg_type[i] = builtin_mode_to_type[m][0];
19157 if (!arg_type[i])
19158 fatal_error (input_location,
19159 "internal error: builtin function %s, argument %d "
19160 "had unexpected argument type %s", name, i,
19161 GET_MODE_NAME (m));
19164 builtin_hash_struct **found = builtin_hash_table->find_slot (&h, INSERT);
19165 if (*found == NULL)
19167 h2 = ggc_alloc<builtin_hash_struct> ();
19168 *h2 = h;
19169 *found = h2;
19171 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
19172 arg_type[2], NULL_TREE);
19175 return (*found)->type;
19178 static void
19179 rs6000_common_init_builtins (void)
19181 const struct builtin_description *d;
19182 size_t i;
19184 tree opaque_ftype_opaque = NULL_TREE;
19185 tree opaque_ftype_opaque_opaque = NULL_TREE;
19186 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
19187 tree v2si_ftype = NULL_TREE;
19188 tree v2si_ftype_qi = NULL_TREE;
19189 tree v2si_ftype_v2si_qi = NULL_TREE;
19190 tree v2si_ftype_int_qi = NULL_TREE;
19191 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
19193 if (!TARGET_PAIRED_FLOAT)
19195 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
19196 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
19199 /* Paired and SPE builtins are only available if you build a compiler with
19200 the appropriate options, so only create those builtins with the
19201 appropriate compiler option. Create Altivec and VSX builtins on machines
19202 with at least the general purpose extensions (970 and newer) to allow the
19203 use of the target attribute.. */
19205 if (TARGET_EXTRA_BUILTINS)
19206 builtin_mask |= RS6000_BTM_COMMON;
19208 /* Add the ternary operators. */
19209 d = bdesc_3arg;
19210 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
19212 tree type;
19213 HOST_WIDE_INT mask = d->mask;
19215 if ((mask & builtin_mask) != mask)
19217 if (TARGET_DEBUG_BUILTIN)
19218 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
19219 continue;
19222 if (rs6000_overloaded_builtin_p (d->code))
19224 if (! (type = opaque_ftype_opaque_opaque_opaque))
19225 type = opaque_ftype_opaque_opaque_opaque
19226 = build_function_type_list (opaque_V4SI_type_node,
19227 opaque_V4SI_type_node,
19228 opaque_V4SI_type_node,
19229 opaque_V4SI_type_node,
19230 NULL_TREE);
19232 else
19234 enum insn_code icode = d->icode;
19235 if (d->name == 0)
19237 if (TARGET_DEBUG_BUILTIN)
19238 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
19239 (long unsigned)i);
19241 continue;
19244 if (icode == CODE_FOR_nothing)
19246 if (TARGET_DEBUG_BUILTIN)
19247 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
19248 d->name);
19250 continue;
19253 type = builtin_function_type (insn_data[icode].operand[0].mode,
19254 insn_data[icode].operand[1].mode,
19255 insn_data[icode].operand[2].mode,
19256 insn_data[icode].operand[3].mode,
19257 d->code, d->name);
19260 def_builtin (d->name, type, d->code);
19263 /* Add the binary operators. */
19264 d = bdesc_2arg;
19265 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19267 machine_mode mode0, mode1, mode2;
19268 tree type;
19269 HOST_WIDE_INT mask = d->mask;
19271 if ((mask & builtin_mask) != mask)
19273 if (TARGET_DEBUG_BUILTIN)
19274 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
19275 continue;
19278 if (rs6000_overloaded_builtin_p (d->code))
19280 if (! (type = opaque_ftype_opaque_opaque))
19281 type = opaque_ftype_opaque_opaque
19282 = build_function_type_list (opaque_V4SI_type_node,
19283 opaque_V4SI_type_node,
19284 opaque_V4SI_type_node,
19285 NULL_TREE);
19287 else
19289 enum insn_code icode = d->icode;
19290 if (d->name == 0)
19292 if (TARGET_DEBUG_BUILTIN)
19293 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
19294 (long unsigned)i);
19296 continue;
19299 if (icode == CODE_FOR_nothing)
19301 if (TARGET_DEBUG_BUILTIN)
19302 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
19303 d->name);
19305 continue;
19308 mode0 = insn_data[icode].operand[0].mode;
19309 mode1 = insn_data[icode].operand[1].mode;
19310 mode2 = insn_data[icode].operand[2].mode;
19312 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
19314 if (! (type = v2si_ftype_v2si_qi))
19315 type = v2si_ftype_v2si_qi
19316 = build_function_type_list (opaque_V2SI_type_node,
19317 opaque_V2SI_type_node,
19318 char_type_node,
19319 NULL_TREE);
19322 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
19323 && mode2 == QImode)
19325 if (! (type = v2si_ftype_int_qi))
19326 type = v2si_ftype_int_qi
19327 = build_function_type_list (opaque_V2SI_type_node,
19328 integer_type_node,
19329 char_type_node,
19330 NULL_TREE);
19333 else
19334 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
19335 d->code, d->name);
19338 def_builtin (d->name, type, d->code);
19341 /* Add the simple unary operators. */
19342 d = bdesc_1arg;
19343 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19345 machine_mode mode0, mode1;
19346 tree type;
19347 HOST_WIDE_INT mask = d->mask;
19349 if ((mask & builtin_mask) != mask)
19351 if (TARGET_DEBUG_BUILTIN)
19352 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
19353 continue;
19356 if (rs6000_overloaded_builtin_p (d->code))
19358 if (! (type = opaque_ftype_opaque))
19359 type = opaque_ftype_opaque
19360 = build_function_type_list (opaque_V4SI_type_node,
19361 opaque_V4SI_type_node,
19362 NULL_TREE);
19364 else
19366 enum insn_code icode = d->icode;
19367 if (d->name == 0)
19369 if (TARGET_DEBUG_BUILTIN)
19370 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
19371 (long unsigned)i);
19373 continue;
19376 if (icode == CODE_FOR_nothing)
19378 if (TARGET_DEBUG_BUILTIN)
19379 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
19380 d->name);
19382 continue;
19385 mode0 = insn_data[icode].operand[0].mode;
19386 mode1 = insn_data[icode].operand[1].mode;
19388 if (mode0 == V2SImode && mode1 == QImode)
19390 if (! (type = v2si_ftype_qi))
19391 type = v2si_ftype_qi
19392 = build_function_type_list (opaque_V2SI_type_node,
19393 char_type_node,
19394 NULL_TREE);
19397 else
19398 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
19399 d->code, d->name);
19402 def_builtin (d->name, type, d->code);
19405 /* Add the simple no-argument operators. */
19406 d = bdesc_0arg;
19407 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
19409 machine_mode mode0;
19410 tree type;
19411 HOST_WIDE_INT mask = d->mask;
19413 if ((mask & builtin_mask) != mask)
19415 if (TARGET_DEBUG_BUILTIN)
19416 fprintf (stderr, "rs6000_builtin, skip no-argument %s\n", d->name);
19417 continue;
19419 if (rs6000_overloaded_builtin_p (d->code))
19421 if (!opaque_ftype_opaque)
19422 opaque_ftype_opaque
19423 = build_function_type_list (opaque_V4SI_type_node, NULL_TREE);
19424 type = opaque_ftype_opaque;
19426 else
19428 enum insn_code icode = d->icode;
19429 if (d->name == 0)
19431 if (TARGET_DEBUG_BUILTIN)
19432 fprintf (stderr, "rs6000_builtin, bdesc_0arg[%lu] no name\n",
19433 (long unsigned) i);
19434 continue;
19436 if (icode == CODE_FOR_nothing)
19438 if (TARGET_DEBUG_BUILTIN)
19439 fprintf (stderr,
19440 "rs6000_builtin, skip no-argument %s (no code)\n",
19441 d->name);
19442 continue;
19444 mode0 = insn_data[icode].operand[0].mode;
19445 if (mode0 == V2SImode)
19447 /* code for SPE */
19448 if (! (type = v2si_ftype))
19450 v2si_ftype
19451 = build_function_type_list (opaque_V2SI_type_node,
19452 NULL_TREE);
19453 type = v2si_ftype;
19456 else
19457 type = builtin_function_type (mode0, VOIDmode, VOIDmode, VOIDmode,
19458 d->code, d->name);
19460 def_builtin (d->name, type, d->code);
19464 /* Set up AIX/Darwin/64-bit Linux quad floating point routines. */
19465 static void
19466 init_float128_ibm (machine_mode mode)
19468 if (!TARGET_XL_COMPAT)
19470 set_optab_libfunc (add_optab, mode, "__gcc_qadd");
19471 set_optab_libfunc (sub_optab, mode, "__gcc_qsub");
19472 set_optab_libfunc (smul_optab, mode, "__gcc_qmul");
19473 set_optab_libfunc (sdiv_optab, mode, "__gcc_qdiv");
19475 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
19477 set_optab_libfunc (neg_optab, mode, "__gcc_qneg");
19478 set_optab_libfunc (eq_optab, mode, "__gcc_qeq");
19479 set_optab_libfunc (ne_optab, mode, "__gcc_qne");
19480 set_optab_libfunc (gt_optab, mode, "__gcc_qgt");
19481 set_optab_libfunc (ge_optab, mode, "__gcc_qge");
19482 set_optab_libfunc (lt_optab, mode, "__gcc_qlt");
19483 set_optab_libfunc (le_optab, mode, "__gcc_qle");
19485 set_conv_libfunc (sext_optab, mode, SFmode, "__gcc_stoq");
19486 set_conv_libfunc (sext_optab, mode, DFmode, "__gcc_dtoq");
19487 set_conv_libfunc (trunc_optab, SFmode, mode, "__gcc_qtos");
19488 set_conv_libfunc (trunc_optab, DFmode, mode, "__gcc_qtod");
19489 set_conv_libfunc (sfix_optab, SImode, mode, "__gcc_qtoi");
19490 set_conv_libfunc (ufix_optab, SImode, mode, "__gcc_qtou");
19491 set_conv_libfunc (sfloat_optab, mode, SImode, "__gcc_itoq");
19492 set_conv_libfunc (ufloat_optab, mode, SImode, "__gcc_utoq");
19495 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
19496 set_optab_libfunc (unord_optab, mode, "__gcc_qunord");
19498 else
19500 set_optab_libfunc (add_optab, mode, "_xlqadd");
19501 set_optab_libfunc (sub_optab, mode, "_xlqsub");
19502 set_optab_libfunc (smul_optab, mode, "_xlqmul");
19503 set_optab_libfunc (sdiv_optab, mode, "_xlqdiv");
19506 /* Add various conversions for IFmode to use the traditional TFmode
19507 names. */
19508 if (mode == IFmode)
19510 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdtf2");
19511 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddtf2");
19512 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctftd2");
19513 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunctfsd2");
19514 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunctfdd2");
19515 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtdtf2");
19517 if (TARGET_POWERPC64)
19519 set_conv_libfunc (sfix_optab, TImode, mode, "__fixtfti");
19520 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunstfti");
19521 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattitf");
19522 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntitf");
19527 /* Set up IEEE 128-bit floating point routines. Use different names if the
19528 arguments can be passed in a vector register. The historical PowerPC
19529 implementation of IEEE 128-bit floating point used _q_<op> for the names, so
19530 continue to use that if we aren't using vector registers to pass IEEE
19531 128-bit floating point. */
19533 static void
19534 init_float128_ieee (machine_mode mode)
19536 if (FLOAT128_VECTOR_P (mode))
19538 set_optab_libfunc (add_optab, mode, "__addkf3");
19539 set_optab_libfunc (sub_optab, mode, "__subkf3");
19540 set_optab_libfunc (neg_optab, mode, "__negkf2");
19541 set_optab_libfunc (smul_optab, mode, "__mulkf3");
19542 set_optab_libfunc (sdiv_optab, mode, "__divkf3");
19543 set_optab_libfunc (sqrt_optab, mode, "__sqrtkf2");
19544 set_optab_libfunc (abs_optab, mode, "__abstkf2");
19546 set_optab_libfunc (eq_optab, mode, "__eqkf2");
19547 set_optab_libfunc (ne_optab, mode, "__nekf2");
19548 set_optab_libfunc (gt_optab, mode, "__gtkf2");
19549 set_optab_libfunc (ge_optab, mode, "__gekf2");
19550 set_optab_libfunc (lt_optab, mode, "__ltkf2");
19551 set_optab_libfunc (le_optab, mode, "__lekf2");
19552 set_optab_libfunc (unord_optab, mode, "__unordkf2");
19554 set_conv_libfunc (sext_optab, mode, SFmode, "__extendsfkf2");
19555 set_conv_libfunc (sext_optab, mode, DFmode, "__extenddfkf2");
19556 set_conv_libfunc (trunc_optab, SFmode, mode, "__trunckfsf2");
19557 set_conv_libfunc (trunc_optab, DFmode, mode, "__trunckfdf2");
19559 set_conv_libfunc (sext_optab, mode, IFmode, "__extendtfkf2");
19560 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
19561 set_conv_libfunc (sext_optab, mode, TFmode, "__extendtfkf2");
19563 set_conv_libfunc (trunc_optab, IFmode, mode, "__trunckftf2");
19564 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
19565 set_conv_libfunc (trunc_optab, TFmode, mode, "__trunckftf2");
19567 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdkf2");
19568 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddkf2");
19569 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunckftd2");
19570 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunckfsd2");
19571 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunckfdd2");
19572 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtdkf2");
19574 set_conv_libfunc (sfix_optab, SImode, mode, "__fixkfsi");
19575 set_conv_libfunc (ufix_optab, SImode, mode, "__fixunskfsi");
19576 set_conv_libfunc (sfix_optab, DImode, mode, "__fixkfdi");
19577 set_conv_libfunc (ufix_optab, DImode, mode, "__fixunskfdi");
19579 set_conv_libfunc (sfloat_optab, mode, SImode, "__floatsikf");
19580 set_conv_libfunc (ufloat_optab, mode, SImode, "__floatunsikf");
19581 set_conv_libfunc (sfloat_optab, mode, DImode, "__floatdikf");
19582 set_conv_libfunc (ufloat_optab, mode, DImode, "__floatundikf");
19584 if (TARGET_POWERPC64)
19586 set_conv_libfunc (sfix_optab, TImode, mode, "__fixkfti");
19587 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunskfti");
19588 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattikf");
19589 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntikf");
19593 else
19595 set_optab_libfunc (add_optab, mode, "_q_add");
19596 set_optab_libfunc (sub_optab, mode, "_q_sub");
19597 set_optab_libfunc (neg_optab, mode, "_q_neg");
19598 set_optab_libfunc (smul_optab, mode, "_q_mul");
19599 set_optab_libfunc (sdiv_optab, mode, "_q_div");
19600 if (TARGET_PPC_GPOPT)
19601 set_optab_libfunc (sqrt_optab, mode, "_q_sqrt");
19603 set_optab_libfunc (eq_optab, mode, "_q_feq");
19604 set_optab_libfunc (ne_optab, mode, "_q_fne");
19605 set_optab_libfunc (gt_optab, mode, "_q_fgt");
19606 set_optab_libfunc (ge_optab, mode, "_q_fge");
19607 set_optab_libfunc (lt_optab, mode, "_q_flt");
19608 set_optab_libfunc (le_optab, mode, "_q_fle");
19610 set_conv_libfunc (sext_optab, mode, SFmode, "_q_stoq");
19611 set_conv_libfunc (sext_optab, mode, DFmode, "_q_dtoq");
19612 set_conv_libfunc (trunc_optab, SFmode, mode, "_q_qtos");
19613 set_conv_libfunc (trunc_optab, DFmode, mode, "_q_qtod");
19614 set_conv_libfunc (sfix_optab, SImode, mode, "_q_qtoi");
19615 set_conv_libfunc (ufix_optab, SImode, mode, "_q_qtou");
19616 set_conv_libfunc (sfloat_optab, mode, SImode, "_q_itoq");
19617 set_conv_libfunc (ufloat_optab, mode, SImode, "_q_utoq");
19621 static void
19622 rs6000_init_libfuncs (void)
19624 /* __float128 support. */
19625 if (TARGET_FLOAT128_TYPE)
19627 init_float128_ibm (IFmode);
19628 init_float128_ieee (KFmode);
19631 /* AIX/Darwin/64-bit Linux quad floating point routines. */
19632 if (TARGET_LONG_DOUBLE_128)
19634 if (!TARGET_IEEEQUAD)
19635 init_float128_ibm (TFmode);
19637 /* IEEE 128-bit including 32-bit SVR4 quad floating point routines. */
19638 else
19639 init_float128_ieee (TFmode);
19644 /* Expand a block clear operation, and return 1 if successful. Return 0
19645 if we should let the compiler generate normal code.
19647 operands[0] is the destination
19648 operands[1] is the length
19649 operands[3] is the alignment */
19652 expand_block_clear (rtx operands[])
19654 rtx orig_dest = operands[0];
19655 rtx bytes_rtx = operands[1];
19656 rtx align_rtx = operands[3];
19657 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
19658 HOST_WIDE_INT align;
19659 HOST_WIDE_INT bytes;
19660 int offset;
19661 int clear_bytes;
19662 int clear_step;
19664 /* If this is not a fixed size move, just call memcpy */
19665 if (! constp)
19666 return 0;
19668 /* This must be a fixed size alignment */
19669 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
19670 align = INTVAL (align_rtx) * BITS_PER_UNIT;
19672 /* Anything to clear? */
19673 bytes = INTVAL (bytes_rtx);
19674 if (bytes <= 0)
19675 return 1;
19677 /* Use the builtin memset after a point, to avoid huge code bloat.
19678 When optimize_size, avoid any significant code bloat; calling
19679 memset is about 4 instructions, so allow for one instruction to
19680 load zero and three to do clearing. */
19681 if (TARGET_ALTIVEC && align >= 128)
19682 clear_step = 16;
19683 else if (TARGET_POWERPC64 && (align >= 64 || !STRICT_ALIGNMENT))
19684 clear_step = 8;
19685 else if (TARGET_SPE && align >= 64)
19686 clear_step = 8;
19687 else
19688 clear_step = 4;
19690 if (optimize_size && bytes > 3 * clear_step)
19691 return 0;
19692 if (! optimize_size && bytes > 8 * clear_step)
19693 return 0;
19695 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
19697 machine_mode mode = BLKmode;
19698 rtx dest;
19700 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
19702 clear_bytes = 16;
19703 mode = V4SImode;
19705 else if (bytes >= 8 && TARGET_SPE && align >= 64)
19707 clear_bytes = 8;
19708 mode = V2SImode;
19710 else if (bytes >= 8 && TARGET_POWERPC64
19711 && (align >= 64 || !STRICT_ALIGNMENT))
19713 clear_bytes = 8;
19714 mode = DImode;
19715 if (offset == 0 && align < 64)
19717 rtx addr;
19719 /* If the address form is reg+offset with offset not a
19720 multiple of four, reload into reg indirect form here
19721 rather than waiting for reload. This way we get one
19722 reload, not one per store. */
19723 addr = XEXP (orig_dest, 0);
19724 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
19725 && GET_CODE (XEXP (addr, 1)) == CONST_INT
19726 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
19728 addr = copy_addr_to_reg (addr);
19729 orig_dest = replace_equiv_address (orig_dest, addr);
19733 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
19734 { /* move 4 bytes */
19735 clear_bytes = 4;
19736 mode = SImode;
19738 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
19739 { /* move 2 bytes */
19740 clear_bytes = 2;
19741 mode = HImode;
19743 else /* move 1 byte at a time */
19745 clear_bytes = 1;
19746 mode = QImode;
19749 dest = adjust_address (orig_dest, mode, offset);
19751 emit_move_insn (dest, CONST0_RTX (mode));
19754 return 1;
19757 /* Emit a potentially record-form instruction, setting DST from SRC.
19758 If DOT is 0, that is all; otherwise, set CCREG to the result of the
19759 signed comparison of DST with zero. If DOT is 1, the generated RTL
19760 doesn't care about the DST result; if DOT is 2, it does. If CCREG
19761 is CR0 do a single dot insn (as a PARALLEL); otherwise, do a SET and
19762 a separate COMPARE. */
19764 static void
19765 rs6000_emit_dot_insn (rtx dst, rtx src, int dot, rtx ccreg)
19767 if (dot == 0)
19769 emit_move_insn (dst, src);
19770 return;
19773 if (cc_reg_not_cr0_operand (ccreg, CCmode))
19775 emit_move_insn (dst, src);
19776 emit_move_insn (ccreg, gen_rtx_COMPARE (CCmode, dst, const0_rtx));
19777 return;
19780 rtx ccset = gen_rtx_SET (ccreg, gen_rtx_COMPARE (CCmode, src, const0_rtx));
19781 if (dot == 1)
19783 rtx clobber = gen_rtx_CLOBBER (VOIDmode, dst);
19784 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, clobber)));
19786 else
19788 rtx set = gen_rtx_SET (dst, src);
19789 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, set)));
19793 /* Figure out the correct instructions to generate to load data for
19794 block compare. MODE is used for the read from memory, and
19795 data is zero extended if REG is wider than MODE. If LE code
19796 is being generated, bswap loads are used.
19798 REG is the destination register to move the data into.
19799 MEM is the memory block being read.
19800 MODE is the mode of memory to use for the read. */
19801 static void
19802 do_load_for_compare (rtx reg, rtx mem, machine_mode mode)
19804 switch (GET_MODE (reg))
19806 case E_DImode:
19807 switch (mode)
19809 case E_QImode:
19810 emit_insn (gen_zero_extendqidi2 (reg, mem));
19811 break;
19812 case E_HImode:
19814 rtx src = mem;
19815 if (!BYTES_BIG_ENDIAN)
19817 src = gen_reg_rtx (HImode);
19818 emit_insn (gen_bswaphi2 (src, mem));
19820 emit_insn (gen_zero_extendhidi2 (reg, src));
19821 break;
19823 case E_SImode:
19825 rtx src = mem;
19826 if (!BYTES_BIG_ENDIAN)
19828 src = gen_reg_rtx (SImode);
19829 emit_insn (gen_bswapsi2 (src, mem));
19831 emit_insn (gen_zero_extendsidi2 (reg, src));
19833 break;
19834 case E_DImode:
19835 if (!BYTES_BIG_ENDIAN)
19836 emit_insn (gen_bswapdi2 (reg, mem));
19837 else
19838 emit_insn (gen_movdi (reg, mem));
19839 break;
19840 default:
19841 gcc_unreachable ();
19843 break;
19845 case E_SImode:
19846 switch (mode)
19848 case E_QImode:
19849 emit_insn (gen_zero_extendqisi2 (reg, mem));
19850 break;
19851 case E_HImode:
19853 rtx src = mem;
19854 if (!BYTES_BIG_ENDIAN)
19856 src = gen_reg_rtx (HImode);
19857 emit_insn (gen_bswaphi2 (src, mem));
19859 emit_insn (gen_zero_extendhisi2 (reg, src));
19860 break;
19862 case E_SImode:
19863 if (!BYTES_BIG_ENDIAN)
19864 emit_insn (gen_bswapsi2 (reg, mem));
19865 else
19866 emit_insn (gen_movsi (reg, mem));
19867 break;
19868 case E_DImode:
19869 /* DImode is larger than the destination reg so is not expected. */
19870 gcc_unreachable ();
19871 break;
19872 default:
19873 gcc_unreachable ();
19875 break;
19876 default:
19877 gcc_unreachable ();
19878 break;
19882 /* Select the mode to be used for reading the next chunk of bytes
19883 in the compare.
19885 OFFSET is the current read offset from the beginning of the block.
19886 BYTES is the number of bytes remaining to be read.
19887 ALIGN is the minimum alignment of the memory blocks being compared in bytes.
19888 WORD_MODE_OK indicates using WORD_MODE is allowed, else SImode is
19889 the largest allowable mode. */
19890 static machine_mode
19891 select_block_compare_mode (unsigned HOST_WIDE_INT offset,
19892 unsigned HOST_WIDE_INT bytes,
19893 unsigned HOST_WIDE_INT align, bool word_mode_ok)
19895 /* First see if we can do a whole load unit
19896 as that will be more efficient than a larger load + shift. */
19898 /* If big, use biggest chunk.
19899 If exactly chunk size, use that size.
19900 If remainder can be done in one piece with shifting, do that.
19901 Do largest chunk possible without violating alignment rules. */
19903 /* The most we can read without potential page crossing. */
19904 unsigned HOST_WIDE_INT maxread = ROUND_UP (bytes, align);
19906 if (word_mode_ok && bytes >= UNITS_PER_WORD)
19907 return word_mode;
19908 else if (bytes == GET_MODE_SIZE (SImode))
19909 return SImode;
19910 else if (bytes == GET_MODE_SIZE (HImode))
19911 return HImode;
19912 else if (bytes == GET_MODE_SIZE (QImode))
19913 return QImode;
19914 else if (bytes < GET_MODE_SIZE (SImode)
19915 && offset >= GET_MODE_SIZE (SImode) - bytes)
19916 /* This matches the case were we have SImode and 3 bytes
19917 and offset >= 1 and permits us to move back one and overlap
19918 with the previous read, thus avoiding having to shift
19919 unwanted bytes off of the input. */
19920 return SImode;
19921 else if (word_mode_ok && bytes < UNITS_PER_WORD
19922 && offset >= UNITS_PER_WORD-bytes)
19923 /* Similarly, if we can use DImode it will get matched here and
19924 can do an overlapping read that ends at the end of the block. */
19925 return word_mode;
19926 else if (word_mode_ok && maxread >= UNITS_PER_WORD)
19927 /* It is safe to do all remaining in one load of largest size,
19928 possibly with a shift to get rid of unwanted bytes. */
19929 return word_mode;
19930 else if (maxread >= GET_MODE_SIZE (SImode))
19931 /* It is safe to do all remaining in one SImode load,
19932 possibly with a shift to get rid of unwanted bytes. */
19933 return SImode;
19934 else if (bytes > GET_MODE_SIZE (SImode))
19935 return SImode;
19936 else if (bytes > GET_MODE_SIZE (HImode))
19937 return HImode;
19939 /* final fallback is do one byte */
19940 return QImode;
19943 /* Compute the alignment of pointer+OFFSET where the original alignment
19944 of pointer was BASE_ALIGN. */
19945 static unsigned HOST_WIDE_INT
19946 compute_current_alignment (unsigned HOST_WIDE_INT base_align,
19947 unsigned HOST_WIDE_INT offset)
19949 if (offset == 0)
19950 return base_align;
19951 return min (base_align, offset & -offset);
19954 /* Expand a block compare operation, and return true if successful.
19955 Return false if we should let the compiler generate normal code,
19956 probably a memcmp call.
19958 OPERANDS[0] is the target (result).
19959 OPERANDS[1] is the first source.
19960 OPERANDS[2] is the second source.
19961 OPERANDS[3] is the length.
19962 OPERANDS[4] is the alignment. */
19963 bool
19964 expand_block_compare (rtx operands[])
19966 rtx target = operands[0];
19967 rtx orig_src1 = operands[1];
19968 rtx orig_src2 = operands[2];
19969 rtx bytes_rtx = operands[3];
19970 rtx align_rtx = operands[4];
19971 HOST_WIDE_INT cmp_bytes = 0;
19972 rtx src1 = orig_src1;
19973 rtx src2 = orig_src2;
19975 /* This case is complicated to handle because the subtract
19976 with carry instructions do not generate the 64-bit
19977 carry and so we must emit code to calculate it ourselves.
19978 We choose not to implement this yet. */
19979 if (TARGET_32BIT && TARGET_POWERPC64)
19980 return false;
19982 /* If this is not a fixed size compare, just call memcmp. */
19983 if (!CONST_INT_P (bytes_rtx))
19984 return false;
19986 /* This must be a fixed size alignment. */
19987 if (!CONST_INT_P (align_rtx))
19988 return false;
19990 unsigned int base_align = UINTVAL (align_rtx) / BITS_PER_UNIT;
19992 /* SLOW_UNALIGNED_ACCESS -- don't do unaligned stuff. */
19993 if (SLOW_UNALIGNED_ACCESS (word_mode, MEM_ALIGN (orig_src1))
19994 || SLOW_UNALIGNED_ACCESS (word_mode, MEM_ALIGN (orig_src2)))
19995 return false;
19997 gcc_assert (GET_MODE (target) == SImode);
19999 /* Anything to move? */
20000 unsigned HOST_WIDE_INT bytes = UINTVAL (bytes_rtx);
20001 if (bytes == 0)
20002 return true;
20004 /* The code generated for p7 and older is not faster than glibc
20005 memcmp if alignment is small and length is not short, so bail
20006 out to avoid those conditions. */
20007 if (!TARGET_EFFICIENT_OVERLAPPING_UNALIGNED
20008 && ((base_align == 1 && bytes > 16)
20009 || (base_align == 2 && bytes > 32)))
20010 return false;
20012 rtx tmp_reg_src1 = gen_reg_rtx (word_mode);
20013 rtx tmp_reg_src2 = gen_reg_rtx (word_mode);
20014 /* P7/P8 code uses cond for subfc. but P9 uses
20015 it for cmpld which needs CCUNSmode. */
20016 rtx cond;
20017 if (TARGET_P9_MISC)
20018 cond = gen_reg_rtx (CCUNSmode);
20019 else
20020 cond = gen_reg_rtx (CCmode);
20022 /* If we have an LE target without ldbrx and word_mode is DImode,
20023 then we must avoid using word_mode. */
20024 int word_mode_ok = !(!BYTES_BIG_ENDIAN && !TARGET_LDBRX
20025 && word_mode == DImode);
20027 /* Strategy phase. How many ops will this take and should we expand it? */
20029 unsigned HOST_WIDE_INT offset = 0;
20030 machine_mode load_mode =
20031 select_block_compare_mode (offset, bytes, base_align, word_mode_ok);
20032 unsigned int load_mode_size = GET_MODE_SIZE (load_mode);
20034 /* We don't want to generate too much code. */
20035 unsigned HOST_WIDE_INT max_bytes =
20036 load_mode_size * (unsigned HOST_WIDE_INT) rs6000_block_compare_inline_limit;
20037 if (!IN_RANGE (bytes, 1, max_bytes))
20038 return false;
20040 bool generate_6432_conversion = false;
20041 rtx convert_label = NULL;
20042 rtx final_label = NULL;
20044 /* Example of generated code for 18 bytes aligned 1 byte.
20045 Compiled with -fno-reorder-blocks for clarity.
20046 ldbrx 10,31,8
20047 ldbrx 9,7,8
20048 subfc. 9,9,10
20049 bne 0,.L6487
20050 addi 9,12,8
20051 addi 5,11,8
20052 ldbrx 10,0,9
20053 ldbrx 9,0,5
20054 subfc. 9,9,10
20055 bne 0,.L6487
20056 addi 9,12,16
20057 lhbrx 10,0,9
20058 addi 9,11,16
20059 lhbrx 9,0,9
20060 subf 9,9,10
20061 b .L6488
20062 .p2align 4,,15
20063 .L6487: #convert_label
20064 popcntd 9,9
20065 subfe 10,10,10
20066 or 9,9,10
20067 .L6488: #final_label
20068 extsw 10,9
20070 We start off with DImode for two blocks that jump to the DI->SI conversion
20071 if the difference is found there, then a final block of HImode that skips
20072 the DI->SI conversion. */
20074 while (bytes > 0)
20076 unsigned int align = compute_current_alignment (base_align, offset);
20077 if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20078 load_mode = select_block_compare_mode (offset, bytes, align,
20079 word_mode_ok);
20080 else
20081 load_mode = select_block_compare_mode (0, bytes, align, word_mode_ok);
20082 load_mode_size = GET_MODE_SIZE (load_mode);
20083 if (bytes >= load_mode_size)
20084 cmp_bytes = load_mode_size;
20085 else if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20087 /* Move this load back so it doesn't go past the end.
20088 P8/P9 can do this efficiently. */
20089 unsigned int extra_bytes = load_mode_size - bytes;
20090 cmp_bytes = bytes;
20091 if (extra_bytes < offset)
20093 offset -= extra_bytes;
20094 cmp_bytes = load_mode_size;
20095 bytes = cmp_bytes;
20098 else
20099 /* P7 and earlier can't do the overlapping load trick fast,
20100 so this forces a non-overlapping load and a shift to get
20101 rid of the extra bytes. */
20102 cmp_bytes = bytes;
20104 src1 = adjust_address (orig_src1, load_mode, offset);
20105 src2 = adjust_address (orig_src2, load_mode, offset);
20107 if (!REG_P (XEXP (src1, 0)))
20109 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20110 src1 = replace_equiv_address (src1, src1_reg);
20112 set_mem_size (src1, cmp_bytes);
20114 if (!REG_P (XEXP (src2, 0)))
20116 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20117 src2 = replace_equiv_address (src2, src2_reg);
20119 set_mem_size (src2, cmp_bytes);
20121 do_load_for_compare (tmp_reg_src1, src1, load_mode);
20122 do_load_for_compare (tmp_reg_src2, src2, load_mode);
20124 if (cmp_bytes < load_mode_size)
20126 /* Shift unneeded bytes off. */
20127 rtx sh = GEN_INT (BITS_PER_UNIT * (load_mode_size - cmp_bytes));
20128 if (word_mode == DImode)
20130 emit_insn (gen_lshrdi3 (tmp_reg_src1, tmp_reg_src1, sh));
20131 emit_insn (gen_lshrdi3 (tmp_reg_src2, tmp_reg_src2, sh));
20133 else
20135 emit_insn (gen_lshrsi3 (tmp_reg_src1, tmp_reg_src1, sh));
20136 emit_insn (gen_lshrsi3 (tmp_reg_src2, tmp_reg_src2, sh));
20140 int remain = bytes - cmp_bytes;
20141 if (GET_MODE_SIZE (GET_MODE (target)) > GET_MODE_SIZE (load_mode))
20143 /* Target is larger than load size so we don't need to
20144 reduce result size. */
20146 /* We previously did a block that need 64->32 conversion but
20147 the current block does not, so a label is needed to jump
20148 to the end. */
20149 if (generate_6432_conversion && !final_label)
20150 final_label = gen_label_rtx ();
20152 if (remain > 0)
20154 /* This is not the last block, branch to the end if the result
20155 of this subtract is not zero. */
20156 if (!final_label)
20157 final_label = gen_label_rtx ();
20158 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20159 rtx tmp = gen_rtx_MINUS (word_mode, tmp_reg_src1, tmp_reg_src2);
20160 rtx cr = gen_reg_rtx (CCmode);
20161 rs6000_emit_dot_insn (tmp_reg_src2, tmp, 2, cr);
20162 emit_insn (gen_movsi (target,
20163 gen_lowpart (SImode, tmp_reg_src2)));
20164 rtx ne_rtx = gen_rtx_NE (VOIDmode, cr, const0_rtx);
20165 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
20166 fin_ref, pc_rtx);
20167 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20168 JUMP_LABEL (j) = final_label;
20169 LABEL_NUSES (final_label) += 1;
20171 else
20173 if (word_mode == DImode)
20175 emit_insn (gen_subdi3 (tmp_reg_src2, tmp_reg_src1,
20176 tmp_reg_src2));
20177 emit_insn (gen_movsi (target,
20178 gen_lowpart (SImode, tmp_reg_src2)));
20180 else
20181 emit_insn (gen_subsi3 (target, tmp_reg_src1, tmp_reg_src2));
20183 if (final_label)
20185 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20186 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, fin_ref));
20187 JUMP_LABEL(j) = final_label;
20188 LABEL_NUSES (final_label) += 1;
20189 emit_barrier ();
20193 else
20195 /* Do we need a 64->32 conversion block? We need the 64->32
20196 conversion even if target size == load_mode size because
20197 the subtract generates one extra bit. */
20198 generate_6432_conversion = true;
20200 if (remain > 0)
20202 if (!convert_label)
20203 convert_label = gen_label_rtx ();
20205 /* Compare to zero and branch to convert_label if not zero. */
20206 rtx cvt_ref = gen_rtx_LABEL_REF (VOIDmode, convert_label);
20207 if (TARGET_P9_MISC)
20209 /* Generate a compare, and convert with a setb later. */
20210 rtx cmp = gen_rtx_COMPARE (CCUNSmode, tmp_reg_src1,
20211 tmp_reg_src2);
20212 emit_insn (gen_rtx_SET (cond, cmp));
20214 else
20215 /* Generate a subfc. and use the longer
20216 sequence for conversion. */
20217 if (TARGET_64BIT)
20218 emit_insn (gen_subfdi3_carry_dot2 (tmp_reg_src2, tmp_reg_src2,
20219 tmp_reg_src1, cond));
20220 else
20221 emit_insn (gen_subfsi3_carry_dot2 (tmp_reg_src2, tmp_reg_src2,
20222 tmp_reg_src1, cond));
20223 rtx ne_rtx = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20224 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
20225 cvt_ref, pc_rtx);
20226 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20227 JUMP_LABEL(j) = convert_label;
20228 LABEL_NUSES (convert_label) += 1;
20230 else
20232 /* Just do the subtract/compare. Since this is the last block
20233 the convert code will be generated immediately following. */
20234 if (TARGET_P9_MISC)
20236 rtx cmp = gen_rtx_COMPARE (CCUNSmode, tmp_reg_src1,
20237 tmp_reg_src2);
20238 emit_insn (gen_rtx_SET (cond, cmp));
20240 else
20241 if (TARGET_64BIT)
20242 emit_insn (gen_subfdi3_carry (tmp_reg_src2, tmp_reg_src2,
20243 tmp_reg_src1));
20244 else
20245 emit_insn (gen_subfsi3_carry (tmp_reg_src2, tmp_reg_src2,
20246 tmp_reg_src1));
20250 offset += cmp_bytes;
20251 bytes -= cmp_bytes;
20254 if (generate_6432_conversion)
20256 if (convert_label)
20257 emit_label (convert_label);
20259 /* We need to produce DI result from sub, then convert to target SI
20260 while maintaining <0 / ==0 / >0 properties. This sequence works:
20261 subfc L,A,B
20262 subfe H,H,H
20263 popcntd L,L
20264 rldimi L,H,6,0
20266 This is an alternate one Segher cooked up if somebody
20267 wants to expand this for something that doesn't have popcntd:
20268 subfc L,a,b
20269 subfe H,x,x
20270 addic t,L,-1
20271 subfe v,t,L
20272 or z,v,H
20274 And finally, p9 can just do this:
20275 cmpld A,B
20276 setb r */
20278 if (TARGET_P9_MISC)
20280 emit_insn (gen_setb_unsigned (target, cond));
20282 else
20284 if (TARGET_64BIT)
20286 rtx tmp_reg_ca = gen_reg_rtx (DImode);
20287 emit_insn (gen_subfdi3_carry_in_xx (tmp_reg_ca));
20288 emit_insn (gen_popcntddi2 (tmp_reg_src2, tmp_reg_src2));
20289 emit_insn (gen_iordi3 (tmp_reg_src2, tmp_reg_src2, tmp_reg_ca));
20290 emit_insn (gen_movsi (target, gen_lowpart (SImode, tmp_reg_src2)));
20292 else
20294 rtx tmp_reg_ca = gen_reg_rtx (SImode);
20295 emit_insn (gen_subfsi3_carry_in_xx (tmp_reg_ca));
20296 emit_insn (gen_popcntdsi2 (tmp_reg_src2, tmp_reg_src2));
20297 emit_insn (gen_iorsi3 (target, tmp_reg_src2, tmp_reg_ca));
20302 if (final_label)
20303 emit_label (final_label);
20305 gcc_assert (bytes == 0);
20306 return true;
20309 /* Generate alignment check and branch code to set up for
20310 strncmp when we don't have DI alignment.
20311 STRNCMP_LABEL is the label to branch if there is a page crossing.
20312 SRC is the string pointer to be examined.
20313 BYTES is the max number of bytes to compare. */
20314 static void
20315 expand_strncmp_align_check (rtx strncmp_label, rtx src, HOST_WIDE_INT bytes)
20317 rtx lab_ref = gen_rtx_LABEL_REF (VOIDmode, strncmp_label);
20318 rtx src_check = copy_addr_to_reg (XEXP (src, 0));
20319 if (GET_MODE (src_check) == SImode)
20320 emit_insn (gen_andsi3 (src_check, src_check, GEN_INT (0xfff)));
20321 else
20322 emit_insn (gen_anddi3 (src_check, src_check, GEN_INT (0xfff)));
20323 rtx cond = gen_reg_rtx (CCmode);
20324 emit_move_insn (cond, gen_rtx_COMPARE (CCmode, src_check,
20325 GEN_INT (4096 - bytes)));
20327 rtx cmp_rtx = gen_rtx_LT (VOIDmode, cond, const0_rtx);
20329 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, cmp_rtx,
20330 pc_rtx, lab_ref);
20331 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20332 JUMP_LABEL (j) = strncmp_label;
20333 LABEL_NUSES (strncmp_label) += 1;
20336 /* Expand a string compare operation with length, and return
20337 true if successful. Return false if we should let the
20338 compiler generate normal code, probably a strncmp call.
20340 OPERANDS[0] is the target (result).
20341 OPERANDS[1] is the first source.
20342 OPERANDS[2] is the second source.
20343 If NO_LENGTH is zero, then:
20344 OPERANDS[3] is the length.
20345 OPERANDS[4] is the alignment in bytes.
20346 If NO_LENGTH is nonzero, then:
20347 OPERANDS[3] is the alignment in bytes. */
20348 bool
20349 expand_strn_compare (rtx operands[], int no_length)
20351 rtx target = operands[0];
20352 rtx orig_src1 = operands[1];
20353 rtx orig_src2 = operands[2];
20354 rtx bytes_rtx, align_rtx;
20355 if (no_length)
20357 bytes_rtx = NULL;
20358 align_rtx = operands[3];
20360 else
20362 bytes_rtx = operands[3];
20363 align_rtx = operands[4];
20365 unsigned HOST_WIDE_INT cmp_bytes = 0;
20366 rtx src1 = orig_src1;
20367 rtx src2 = orig_src2;
20369 /* If we have a length, it must be constant. This simplifies things
20370 a bit as we don't have to generate code to check if we've exceeded
20371 the length. Later this could be expanded to handle this case. */
20372 if (!no_length && !CONST_INT_P (bytes_rtx))
20373 return false;
20375 /* This must be a fixed size alignment. */
20376 if (!CONST_INT_P (align_rtx))
20377 return false;
20379 unsigned int base_align = UINTVAL (align_rtx);
20380 int align1 = MEM_ALIGN (orig_src1) / BITS_PER_UNIT;
20381 int align2 = MEM_ALIGN (orig_src2) / BITS_PER_UNIT;
20383 /* SLOW_UNALIGNED_ACCESS -- don't do unaligned stuff. */
20384 if (SLOW_UNALIGNED_ACCESS (word_mode, align1)
20385 || SLOW_UNALIGNED_ACCESS (word_mode, align2))
20386 return false;
20388 gcc_assert (GET_MODE (target) == SImode);
20390 /* If we have an LE target without ldbrx and word_mode is DImode,
20391 then we must avoid using word_mode. */
20392 int word_mode_ok = !(!BYTES_BIG_ENDIAN && !TARGET_LDBRX
20393 && word_mode == DImode);
20395 unsigned int word_mode_size = GET_MODE_SIZE (word_mode);
20397 unsigned HOST_WIDE_INT offset = 0;
20398 unsigned HOST_WIDE_INT bytes; /* N from the strncmp args if available. */
20399 unsigned HOST_WIDE_INT compare_length; /* How much to compare inline. */
20400 if (no_length)
20401 /* Use this as a standin to determine the mode to use. */
20402 bytes = rs6000_string_compare_inline_limit * word_mode_size;
20403 else
20404 bytes = UINTVAL (bytes_rtx);
20406 machine_mode load_mode =
20407 select_block_compare_mode (offset, bytes, base_align, word_mode_ok);
20408 unsigned int load_mode_size = GET_MODE_SIZE (load_mode);
20409 compare_length = rs6000_string_compare_inline_limit * load_mode_size;
20411 /* If we have equality at the end of the last compare and we have not
20412 found the end of the string, we need to call strcmp/strncmp to
20413 compare the remainder. */
20414 bool equality_compare_rest = false;
20416 if (no_length)
20418 bytes = compare_length;
20419 equality_compare_rest = true;
20421 else
20423 if (bytes <= compare_length)
20424 compare_length = bytes;
20425 else
20426 equality_compare_rest = true;
20429 rtx result_reg = gen_reg_rtx (word_mode);
20430 rtx final_move_label = gen_label_rtx ();
20431 rtx final_label = gen_label_rtx ();
20432 rtx begin_compare_label = NULL;
20434 if (base_align < 8)
20436 /* Generate code that checks distance to 4k boundary for this case. */
20437 begin_compare_label = gen_label_rtx ();
20438 rtx strncmp_label = gen_label_rtx ();
20439 rtx jmp;
20441 /* Strncmp for power8 in glibc does this:
20442 rldicl r8,r3,0,52
20443 cmpldi cr7,r8,4096-16
20444 bgt cr7,L(pagecross) */
20446 /* Make sure that the length we use for the alignment test and
20447 the subsequent code generation are in agreement so we do not
20448 go past the length we tested for a 4k boundary crossing. */
20449 unsigned HOST_WIDE_INT align_test = compare_length;
20450 if (align_test < 8)
20452 align_test = HOST_WIDE_INT_1U << ceil_log2 (align_test);
20453 base_align = align_test;
20455 else
20457 align_test = ROUND_UP (align_test, 8);
20458 base_align = 8;
20461 if (align1 < 8)
20462 expand_strncmp_align_check (strncmp_label, src1, align_test);
20463 if (align2 < 8)
20464 expand_strncmp_align_check (strncmp_label, src2, align_test);
20466 /* Now generate the following sequence:
20467 - branch to begin_compare
20468 - strncmp_label
20469 - call to strncmp
20470 - branch to final_label
20471 - begin_compare_label */
20473 rtx cmp_ref = gen_rtx_LABEL_REF (VOIDmode, begin_compare_label);
20474 jmp = emit_jump_insn (gen_rtx_SET (pc_rtx, cmp_ref));
20475 JUMP_LABEL (jmp) = begin_compare_label;
20476 LABEL_NUSES (begin_compare_label) += 1;
20477 emit_barrier ();
20479 emit_label (strncmp_label);
20481 if (!REG_P (XEXP (src1, 0)))
20483 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20484 src1 = replace_equiv_address (src1, src1_reg);
20487 if (!REG_P (XEXP (src2, 0)))
20489 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20490 src2 = replace_equiv_address (src2, src2_reg);
20493 if (no_length)
20495 tree fun = builtin_decl_explicit (BUILT_IN_STRCMP);
20496 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20497 target, LCT_NORMAL, GET_MODE (target),
20498 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20499 force_reg (Pmode, XEXP (src2, 0)), Pmode);
20501 else
20503 /* -m32 -mpowerpc64 results in word_mode being DImode even
20504 though otherwise it is 32-bit. The length arg to strncmp
20505 is a size_t which will be the same size as pointers. */
20506 rtx len_rtx;
20507 if (TARGET_64BIT)
20508 len_rtx = gen_reg_rtx (DImode);
20509 else
20510 len_rtx = gen_reg_rtx (SImode);
20512 emit_move_insn (len_rtx, bytes_rtx);
20514 tree fun = builtin_decl_explicit (BUILT_IN_STRNCMP);
20515 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20516 target, LCT_NORMAL, GET_MODE (target),
20517 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20518 force_reg (Pmode, XEXP (src2, 0)), Pmode,
20519 len_rtx, GET_MODE (len_rtx));
20522 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20523 jmp = emit_jump_insn (gen_rtx_SET (pc_rtx, fin_ref));
20524 JUMP_LABEL (jmp) = final_label;
20525 LABEL_NUSES (final_label) += 1;
20526 emit_barrier ();
20527 emit_label (begin_compare_label);
20530 rtx cleanup_label = NULL;
20531 rtx tmp_reg_src1 = gen_reg_rtx (word_mode);
20532 rtx tmp_reg_src2 = gen_reg_rtx (word_mode);
20534 /* Generate sequence of ld/ldbrx, cmpb to compare out
20535 to the length specified. */
20536 unsigned HOST_WIDE_INT bytes_to_compare = compare_length;
20537 while (bytes_to_compare > 0)
20539 /* Compare sequence:
20540 check each 8B with: ld/ld cmpd bne
20541 If equal, use rldicr/cmpb to check for zero byte.
20542 cleanup code at end:
20543 cmpb get byte that differs
20544 cmpb look for zero byte
20545 orc combine
20546 cntlzd get bit of first zero/diff byte
20547 subfic convert for rldcl use
20548 rldcl rldcl extract diff/zero byte
20549 subf subtract for final result
20551 The last compare can branch around the cleanup code if the
20552 result is zero because the strings are exactly equal. */
20553 unsigned int align = compute_current_alignment (base_align, offset);
20554 if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20555 load_mode = select_block_compare_mode (offset, bytes_to_compare, align,
20556 word_mode_ok);
20557 else
20558 load_mode = select_block_compare_mode (0, bytes_to_compare, align,
20559 word_mode_ok);
20560 load_mode_size = GET_MODE_SIZE (load_mode);
20561 if (bytes_to_compare >= load_mode_size)
20562 cmp_bytes = load_mode_size;
20563 else if (TARGET_EFFICIENT_OVERLAPPING_UNALIGNED)
20565 /* Move this load back so it doesn't go past the end.
20566 P8/P9 can do this efficiently. */
20567 unsigned int extra_bytes = load_mode_size - bytes_to_compare;
20568 cmp_bytes = bytes_to_compare;
20569 if (extra_bytes < offset)
20571 offset -= extra_bytes;
20572 cmp_bytes = load_mode_size;
20573 bytes_to_compare = cmp_bytes;
20576 else
20577 /* P7 and earlier can't do the overlapping load trick fast,
20578 so this forces a non-overlapping load and a shift to get
20579 rid of the extra bytes. */
20580 cmp_bytes = bytes_to_compare;
20582 src1 = adjust_address (orig_src1, load_mode, offset);
20583 src2 = adjust_address (orig_src2, load_mode, offset);
20585 if (!REG_P (XEXP (src1, 0)))
20587 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20588 src1 = replace_equiv_address (src1, src1_reg);
20590 set_mem_size (src1, cmp_bytes);
20592 if (!REG_P (XEXP (src2, 0)))
20594 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20595 src2 = replace_equiv_address (src2, src2_reg);
20597 set_mem_size (src2, cmp_bytes);
20599 do_load_for_compare (tmp_reg_src1, src1, load_mode);
20600 do_load_for_compare (tmp_reg_src2, src2, load_mode);
20602 /* We must always left-align the data we read, and
20603 clear any bytes to the right that are beyond the string.
20604 Otherwise the cmpb sequence won't produce the correct
20605 results. The beginning of the compare will be done
20606 with word_mode so will not have any extra shifts or
20607 clear rights. */
20609 if (load_mode_size < word_mode_size)
20611 /* Rotate left first. */
20612 rtx sh = GEN_INT (BITS_PER_UNIT * (word_mode_size - load_mode_size));
20613 if (word_mode == DImode)
20615 emit_insn (gen_rotldi3 (tmp_reg_src1, tmp_reg_src1, sh));
20616 emit_insn (gen_rotldi3 (tmp_reg_src2, tmp_reg_src2, sh));
20618 else
20620 emit_insn (gen_rotlsi3 (tmp_reg_src1, tmp_reg_src1, sh));
20621 emit_insn (gen_rotlsi3 (tmp_reg_src2, tmp_reg_src2, sh));
20625 if (cmp_bytes < word_mode_size)
20627 /* Now clear right. This plus the rotate can be
20628 turned into a rldicr instruction. */
20629 HOST_WIDE_INT mb = BITS_PER_UNIT * (word_mode_size - cmp_bytes);
20630 rtx mask = GEN_INT (HOST_WIDE_INT_M1U << mb);
20631 if (word_mode == DImode)
20633 emit_insn (gen_anddi3_mask (tmp_reg_src1, tmp_reg_src1, mask));
20634 emit_insn (gen_anddi3_mask (tmp_reg_src2, tmp_reg_src2, mask));
20636 else
20638 emit_insn (gen_andsi3_mask (tmp_reg_src1, tmp_reg_src1, mask));
20639 emit_insn (gen_andsi3_mask (tmp_reg_src2, tmp_reg_src2, mask));
20643 /* Cases to handle. A and B are chunks of the two strings.
20644 1: Not end of comparison:
20645 A != B: branch to cleanup code to compute result.
20646 A == B: check for 0 byte, next block if not found.
20647 2: End of the inline comparison:
20648 A != B: branch to cleanup code to compute result.
20649 A == B: check for 0 byte, call strcmp/strncmp
20650 3: compared requested N bytes:
20651 A == B: branch to result 0.
20652 A != B: cleanup code to compute result. */
20654 unsigned HOST_WIDE_INT remain = bytes_to_compare - cmp_bytes;
20656 rtx dst_label;
20657 if (remain > 0 || equality_compare_rest)
20659 /* Branch to cleanup code, otherwise fall through to do
20660 more compares. */
20661 if (!cleanup_label)
20662 cleanup_label = gen_label_rtx ();
20663 dst_label = cleanup_label;
20665 else
20666 /* Branch to end and produce result of 0. */
20667 dst_label = final_move_label;
20669 rtx lab_ref = gen_rtx_LABEL_REF (VOIDmode, dst_label);
20670 rtx cond = gen_reg_rtx (CCmode);
20672 /* Always produce the 0 result, it is needed if
20673 cmpb finds a 0 byte in this chunk. */
20674 rtx tmp = gen_rtx_MINUS (word_mode, tmp_reg_src1, tmp_reg_src2);
20675 rs6000_emit_dot_insn (result_reg, tmp, 1, cond);
20677 rtx cmp_rtx;
20678 if (remain == 0 && !equality_compare_rest)
20679 cmp_rtx = gen_rtx_EQ (VOIDmode, cond, const0_rtx);
20680 else
20681 cmp_rtx = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20683 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, cmp_rtx,
20684 lab_ref, pc_rtx);
20685 rtx j = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20686 JUMP_LABEL (j) = dst_label;
20687 LABEL_NUSES (dst_label) += 1;
20689 if (remain > 0 || equality_compare_rest)
20691 /* Generate a cmpb to test for a 0 byte and branch
20692 to final result if found. */
20693 rtx cmpb_zero = gen_reg_rtx (word_mode);
20694 rtx lab_ref_fin = gen_rtx_LABEL_REF (VOIDmode, final_move_label);
20695 rtx condz = gen_reg_rtx (CCmode);
20696 rtx zero_reg = gen_reg_rtx (word_mode);
20697 if (word_mode == SImode)
20699 emit_insn (gen_movsi (zero_reg, GEN_INT (0)));
20700 emit_insn (gen_cmpbsi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20701 if (cmp_bytes < word_mode_size)
20703 /* Don't want to look at zero bytes past end. */
20704 HOST_WIDE_INT mb =
20705 BITS_PER_UNIT * (word_mode_size - cmp_bytes);
20706 rtx mask = GEN_INT (HOST_WIDE_INT_M1U << mb);
20707 emit_insn (gen_andsi3_mask (cmpb_zero, cmpb_zero, mask));
20710 else
20712 emit_insn (gen_movdi (zero_reg, GEN_INT (0)));
20713 emit_insn (gen_cmpbdi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20714 if (cmp_bytes < word_mode_size)
20716 /* Don't want to look at zero bytes past end. */
20717 HOST_WIDE_INT mb =
20718 BITS_PER_UNIT * (word_mode_size - cmp_bytes);
20719 rtx mask = GEN_INT (HOST_WIDE_INT_M1U << mb);
20720 emit_insn (gen_anddi3_mask (cmpb_zero, cmpb_zero, mask));
20724 emit_move_insn (condz, gen_rtx_COMPARE (CCmode, cmpb_zero, zero_reg));
20725 rtx cmpnz_rtx = gen_rtx_NE (VOIDmode, condz, const0_rtx);
20726 rtx ifelse = gen_rtx_IF_THEN_ELSE (VOIDmode, cmpnz_rtx,
20727 lab_ref_fin, pc_rtx);
20728 rtx j2 = emit_jump_insn (gen_rtx_SET (pc_rtx, ifelse));
20729 JUMP_LABEL (j2) = final_move_label;
20730 LABEL_NUSES (final_move_label) += 1;
20734 offset += cmp_bytes;
20735 bytes_to_compare -= cmp_bytes;
20738 if (equality_compare_rest)
20740 /* Update pointers past what has been compared already. */
20741 src1 = adjust_address (orig_src1, load_mode, offset);
20742 src2 = adjust_address (orig_src2, load_mode, offset);
20744 if (!REG_P (XEXP (src1, 0)))
20746 rtx src1_reg = copy_addr_to_reg (XEXP (src1, 0));
20747 src1 = replace_equiv_address (src1, src1_reg);
20749 set_mem_size (src1, cmp_bytes);
20751 if (!REG_P (XEXP (src2, 0)))
20753 rtx src2_reg = copy_addr_to_reg (XEXP (src2, 0));
20754 src2 = replace_equiv_address (src2, src2_reg);
20756 set_mem_size (src2, cmp_bytes);
20758 /* Construct call to strcmp/strncmp to compare the rest of the string. */
20759 if (no_length)
20761 tree fun = builtin_decl_explicit (BUILT_IN_STRCMP);
20762 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20763 target, LCT_NORMAL, GET_MODE (target),
20764 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20765 force_reg (Pmode, XEXP (src2, 0)), Pmode);
20767 else
20769 rtx len_rtx;
20770 if (TARGET_64BIT)
20771 len_rtx = gen_reg_rtx (DImode);
20772 else
20773 len_rtx = gen_reg_rtx (SImode);
20775 emit_move_insn (len_rtx, GEN_INT (bytes - compare_length));
20776 tree fun = builtin_decl_explicit (BUILT_IN_STRNCMP);
20777 emit_library_call_value (XEXP (DECL_RTL (fun), 0),
20778 target, LCT_NORMAL, GET_MODE (target),
20779 force_reg (Pmode, XEXP (src1, 0)), Pmode,
20780 force_reg (Pmode, XEXP (src2, 0)), Pmode,
20781 len_rtx, GET_MODE (len_rtx));
20784 rtx fin_ref = gen_rtx_LABEL_REF (VOIDmode, final_label);
20785 rtx jmp = emit_jump_insn (gen_rtx_SET (pc_rtx, fin_ref));
20786 JUMP_LABEL (jmp) = final_label;
20787 LABEL_NUSES (final_label) += 1;
20788 emit_barrier ();
20791 if (cleanup_label)
20792 emit_label (cleanup_label);
20794 /* Generate the final sequence that identifies the differing
20795 byte and generates the final result, taking into account
20796 zero bytes:
20798 cmpb cmpb_result1, src1, src2
20799 cmpb cmpb_result2, src1, zero
20800 orc cmpb_result1, cmp_result1, cmpb_result2
20801 cntlzd get bit of first zero/diff byte
20802 addi convert for rldcl use
20803 rldcl rldcl extract diff/zero byte
20804 subf subtract for final result
20807 rtx cmpb_diff = gen_reg_rtx (word_mode);
20808 rtx cmpb_zero = gen_reg_rtx (word_mode);
20809 rtx rot_amt = gen_reg_rtx (word_mode);
20810 rtx zero_reg = gen_reg_rtx (word_mode);
20812 rtx rot1_1 = gen_reg_rtx (word_mode);
20813 rtx rot1_2 = gen_reg_rtx (word_mode);
20814 rtx rot2_1 = gen_reg_rtx (word_mode);
20815 rtx rot2_2 = gen_reg_rtx (word_mode);
20817 if (word_mode == SImode)
20819 emit_insn (gen_cmpbsi3 (cmpb_diff, tmp_reg_src1, tmp_reg_src2));
20820 emit_insn (gen_movsi (zero_reg, GEN_INT (0)));
20821 emit_insn (gen_cmpbsi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20822 emit_insn (gen_one_cmplsi2 (cmpb_diff,cmpb_diff));
20823 emit_insn (gen_iorsi3 (cmpb_diff, cmpb_diff, cmpb_zero));
20824 emit_insn (gen_clzsi2 (rot_amt, cmpb_diff));
20825 emit_insn (gen_addsi3 (rot_amt, rot_amt, GEN_INT (8)));
20826 emit_insn (gen_rotlsi3 (rot1_1, tmp_reg_src1,
20827 gen_lowpart (SImode, rot_amt)));
20828 emit_insn (gen_andsi3_mask (rot1_2, rot1_1, GEN_INT (0xff)));
20829 emit_insn (gen_rotlsi3 (rot2_1, tmp_reg_src2,
20830 gen_lowpart (SImode, rot_amt)));
20831 emit_insn (gen_andsi3_mask (rot2_2, rot2_1, GEN_INT (0xff)));
20832 emit_insn (gen_subsi3 (result_reg, rot1_2, rot2_2));
20834 else
20836 emit_insn (gen_cmpbdi3 (cmpb_diff, tmp_reg_src1, tmp_reg_src2));
20837 emit_insn (gen_movdi (zero_reg, GEN_INT (0)));
20838 emit_insn (gen_cmpbdi3 (cmpb_zero, tmp_reg_src1, zero_reg));
20839 emit_insn (gen_one_cmpldi2 (cmpb_diff,cmpb_diff));
20840 emit_insn (gen_iordi3 (cmpb_diff, cmpb_diff, cmpb_zero));
20841 emit_insn (gen_clzdi2 (rot_amt, cmpb_diff));
20842 emit_insn (gen_adddi3 (rot_amt, rot_amt, GEN_INT (8)));
20843 emit_insn (gen_rotldi3 (rot1_1, tmp_reg_src1,
20844 gen_lowpart (SImode, rot_amt)));
20845 emit_insn (gen_anddi3_mask (rot1_2, rot1_1, GEN_INT (0xff)));
20846 emit_insn (gen_rotldi3 (rot2_1, tmp_reg_src2,
20847 gen_lowpart (SImode, rot_amt)));
20848 emit_insn (gen_anddi3_mask (rot2_2, rot2_1, GEN_INT (0xff)));
20849 emit_insn (gen_subdi3 (result_reg, rot1_2, rot2_2));
20852 emit_label (final_move_label);
20853 emit_insn (gen_movsi (target,
20854 gen_lowpart (SImode, result_reg)));
20855 emit_label (final_label);
20856 return true;
20859 /* Expand a block move operation, and return 1 if successful. Return 0
20860 if we should let the compiler generate normal code.
20862 operands[0] is the destination
20863 operands[1] is the source
20864 operands[2] is the length
20865 operands[3] is the alignment */
20867 #define MAX_MOVE_REG 4
20870 expand_block_move (rtx operands[])
20872 rtx orig_dest = operands[0];
20873 rtx orig_src = operands[1];
20874 rtx bytes_rtx = operands[2];
20875 rtx align_rtx = operands[3];
20876 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
20877 int align;
20878 int bytes;
20879 int offset;
20880 int move_bytes;
20881 rtx stores[MAX_MOVE_REG];
20882 int num_reg = 0;
20884 /* If this is not a fixed size move, just call memcpy */
20885 if (! constp)
20886 return 0;
20888 /* This must be a fixed size alignment */
20889 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
20890 align = INTVAL (align_rtx) * BITS_PER_UNIT;
20892 /* Anything to move? */
20893 bytes = INTVAL (bytes_rtx);
20894 if (bytes <= 0)
20895 return 1;
20897 if (bytes > rs6000_block_move_inline_limit)
20898 return 0;
20900 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
20902 union {
20903 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
20904 rtx (*mov) (rtx, rtx);
20905 } gen_func;
20906 machine_mode mode = BLKmode;
20907 rtx src, dest;
20909 /* Altivec first, since it will be faster than a string move
20910 when it applies, and usually not significantly larger. */
20911 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
20913 move_bytes = 16;
20914 mode = V4SImode;
20915 gen_func.mov = gen_movv4si;
20917 else if (TARGET_SPE && bytes >= 8 && align >= 64)
20919 move_bytes = 8;
20920 mode = V2SImode;
20921 gen_func.mov = gen_movv2si;
20923 else if (TARGET_STRING
20924 && bytes > 24 /* move up to 32 bytes at a time */
20925 && ! fixed_regs[5]
20926 && ! fixed_regs[6]
20927 && ! fixed_regs[7]
20928 && ! fixed_regs[8]
20929 && ! fixed_regs[9]
20930 && ! fixed_regs[10]
20931 && ! fixed_regs[11]
20932 && ! fixed_regs[12])
20934 move_bytes = (bytes > 32) ? 32 : bytes;
20935 gen_func.movmemsi = gen_movmemsi_8reg;
20937 else if (TARGET_STRING
20938 && bytes > 16 /* move up to 24 bytes at a time */
20939 && ! fixed_regs[5]
20940 && ! fixed_regs[6]
20941 && ! fixed_regs[7]
20942 && ! fixed_regs[8]
20943 && ! fixed_regs[9]
20944 && ! fixed_regs[10])
20946 move_bytes = (bytes > 24) ? 24 : bytes;
20947 gen_func.movmemsi = gen_movmemsi_6reg;
20949 else if (TARGET_STRING
20950 && bytes > 8 /* move up to 16 bytes at a time */
20951 && ! fixed_regs[5]
20952 && ! fixed_regs[6]
20953 && ! fixed_regs[7]
20954 && ! fixed_regs[8])
20956 move_bytes = (bytes > 16) ? 16 : bytes;
20957 gen_func.movmemsi = gen_movmemsi_4reg;
20959 else if (bytes >= 8 && TARGET_POWERPC64
20960 && (align >= 64 || !STRICT_ALIGNMENT))
20962 move_bytes = 8;
20963 mode = DImode;
20964 gen_func.mov = gen_movdi;
20965 if (offset == 0 && align < 64)
20967 rtx addr;
20969 /* If the address form is reg+offset with offset not a
20970 multiple of four, reload into reg indirect form here
20971 rather than waiting for reload. This way we get one
20972 reload, not one per load and/or store. */
20973 addr = XEXP (orig_dest, 0);
20974 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
20975 && GET_CODE (XEXP (addr, 1)) == CONST_INT
20976 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
20978 addr = copy_addr_to_reg (addr);
20979 orig_dest = replace_equiv_address (orig_dest, addr);
20981 addr = XEXP (orig_src, 0);
20982 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
20983 && GET_CODE (XEXP (addr, 1)) == CONST_INT
20984 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
20986 addr = copy_addr_to_reg (addr);
20987 orig_src = replace_equiv_address (orig_src, addr);
20991 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
20992 { /* move up to 8 bytes at a time */
20993 move_bytes = (bytes > 8) ? 8 : bytes;
20994 gen_func.movmemsi = gen_movmemsi_2reg;
20996 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
20997 { /* move 4 bytes */
20998 move_bytes = 4;
20999 mode = SImode;
21000 gen_func.mov = gen_movsi;
21002 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
21003 { /* move 2 bytes */
21004 move_bytes = 2;
21005 mode = HImode;
21006 gen_func.mov = gen_movhi;
21008 else if (TARGET_STRING && bytes > 1)
21009 { /* move up to 4 bytes at a time */
21010 move_bytes = (bytes > 4) ? 4 : bytes;
21011 gen_func.movmemsi = gen_movmemsi_1reg;
21013 else /* move 1 byte at a time */
21015 move_bytes = 1;
21016 mode = QImode;
21017 gen_func.mov = gen_movqi;
21020 src = adjust_address (orig_src, mode, offset);
21021 dest = adjust_address (orig_dest, mode, offset);
21023 if (mode != BLKmode)
21025 rtx tmp_reg = gen_reg_rtx (mode);
21027 emit_insn ((*gen_func.mov) (tmp_reg, src));
21028 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
21031 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
21033 int i;
21034 for (i = 0; i < num_reg; i++)
21035 emit_insn (stores[i]);
21036 num_reg = 0;
21039 if (mode == BLKmode)
21041 /* Move the address into scratch registers. The movmemsi
21042 patterns require zero offset. */
21043 if (!REG_P (XEXP (src, 0)))
21045 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
21046 src = replace_equiv_address (src, src_reg);
21048 set_mem_size (src, move_bytes);
21050 if (!REG_P (XEXP (dest, 0)))
21052 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
21053 dest = replace_equiv_address (dest, dest_reg);
21055 set_mem_size (dest, move_bytes);
21057 emit_insn ((*gen_func.movmemsi) (dest, src,
21058 GEN_INT (move_bytes & 31),
21059 align_rtx));
21063 return 1;
21067 /* Return a string to perform a load_multiple operation.
21068 operands[0] is the vector.
21069 operands[1] is the source address.
21070 operands[2] is the first destination register. */
21072 const char *
21073 rs6000_output_load_multiple (rtx operands[3])
21075 /* We have to handle the case where the pseudo used to contain the address
21076 is assigned to one of the output registers. */
21077 int i, j;
21078 int words = XVECLEN (operands[0], 0);
21079 rtx xop[10];
21081 if (XVECLEN (operands[0], 0) == 1)
21082 return "lwz %2,0(%1)";
21084 for (i = 0; i < words; i++)
21085 if (refers_to_regno_p (REGNO (operands[2]) + i, operands[1]))
21087 if (i == words-1)
21089 xop[0] = GEN_INT (4 * (words-1));
21090 xop[1] = operands[1];
21091 xop[2] = operands[2];
21092 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
21093 return "";
21095 else if (i == 0)
21097 xop[0] = GEN_INT (4 * (words-1));
21098 xop[1] = operands[1];
21099 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
21100 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
21101 return "";
21103 else
21105 for (j = 0; j < words; j++)
21106 if (j != i)
21108 xop[0] = GEN_INT (j * 4);
21109 xop[1] = operands[1];
21110 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
21111 output_asm_insn ("lwz %2,%0(%1)", xop);
21113 xop[0] = GEN_INT (i * 4);
21114 xop[1] = operands[1];
21115 output_asm_insn ("lwz %1,%0(%1)", xop);
21116 return "";
21120 return "lswi %2,%1,%N0";
21124 /* A validation routine: say whether CODE, a condition code, and MODE
21125 match. The other alternatives either don't make sense or should
21126 never be generated. */
21128 void
21129 validate_condition_mode (enum rtx_code code, machine_mode mode)
21131 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
21132 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
21133 && GET_MODE_CLASS (mode) == MODE_CC);
21135 /* These don't make sense. */
21136 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
21137 || mode != CCUNSmode);
21139 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
21140 || mode == CCUNSmode);
21142 gcc_assert (mode == CCFPmode
21143 || (code != ORDERED && code != UNORDERED
21144 && code != UNEQ && code != LTGT
21145 && code != UNGT && code != UNLT
21146 && code != UNGE && code != UNLE));
21148 /* These should never be generated except for
21149 flag_finite_math_only. */
21150 gcc_assert (mode != CCFPmode
21151 || flag_finite_math_only
21152 || (code != LE && code != GE
21153 && code != UNEQ && code != LTGT
21154 && code != UNGT && code != UNLT));
21156 /* These are invalid; the information is not there. */
21157 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
21161 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm,
21162 rldicl, rldicr, or rldic instruction in mode MODE. If so, if E is
21163 not zero, store there the bit offset (counted from the right) where
21164 the single stretch of 1 bits begins; and similarly for B, the bit
21165 offset where it ends. */
21167 bool
21168 rs6000_is_valid_mask (rtx mask, int *b, int *e, machine_mode mode)
21170 unsigned HOST_WIDE_INT val = INTVAL (mask);
21171 unsigned HOST_WIDE_INT bit;
21172 int nb, ne;
21173 int n = GET_MODE_PRECISION (mode);
21175 if (mode != DImode && mode != SImode)
21176 return false;
21178 if (INTVAL (mask) >= 0)
21180 bit = val & -val;
21181 ne = exact_log2 (bit);
21182 nb = exact_log2 (val + bit);
21184 else if (val + 1 == 0)
21186 nb = n;
21187 ne = 0;
21189 else if (val & 1)
21191 val = ~val;
21192 bit = val & -val;
21193 nb = exact_log2 (bit);
21194 ne = exact_log2 (val + bit);
21196 else
21198 bit = val & -val;
21199 ne = exact_log2 (bit);
21200 if (val + bit == 0)
21201 nb = n;
21202 else
21203 nb = 0;
21206 nb--;
21208 if (nb < 0 || ne < 0 || nb >= n || ne >= n)
21209 return false;
21211 if (b)
21212 *b = nb;
21213 if (e)
21214 *e = ne;
21216 return true;
21219 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm, rldicl,
21220 or rldicr instruction, to implement an AND with it in mode MODE. */
21222 bool
21223 rs6000_is_valid_and_mask (rtx mask, machine_mode mode)
21225 int nb, ne;
21227 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
21228 return false;
21230 /* For DImode, we need a rldicl, rldicr, or a rlwinm with mask that
21231 does not wrap. */
21232 if (mode == DImode)
21233 return (ne == 0 || nb == 63 || (nb < 32 && ne <= nb));
21235 /* For SImode, rlwinm can do everything. */
21236 if (mode == SImode)
21237 return (nb < 32 && ne < 32);
21239 return false;
21242 /* Return the instruction template for an AND with mask in mode MODE, with
21243 operands OPERANDS. If DOT is true, make it a record-form instruction. */
21245 const char *
21246 rs6000_insn_for_and_mask (machine_mode mode, rtx *operands, bool dot)
21248 int nb, ne;
21250 if (!rs6000_is_valid_mask (operands[2], &nb, &ne, mode))
21251 gcc_unreachable ();
21253 if (mode == DImode && ne == 0)
21255 operands[3] = GEN_INT (63 - nb);
21256 if (dot)
21257 return "rldicl. %0,%1,0,%3";
21258 return "rldicl %0,%1,0,%3";
21261 if (mode == DImode && nb == 63)
21263 operands[3] = GEN_INT (63 - ne);
21264 if (dot)
21265 return "rldicr. %0,%1,0,%3";
21266 return "rldicr %0,%1,0,%3";
21269 if (nb < 32 && ne < 32)
21271 operands[3] = GEN_INT (31 - nb);
21272 operands[4] = GEN_INT (31 - ne);
21273 if (dot)
21274 return "rlwinm. %0,%1,0,%3,%4";
21275 return "rlwinm %0,%1,0,%3,%4";
21278 gcc_unreachable ();
21281 /* Return whether MASK (a CONST_INT) is a valid mask for any rlw[i]nm,
21282 rld[i]cl, rld[i]cr, or rld[i]c instruction, to implement an AND with
21283 shift SHIFT (a ROTATE, ASHIFT, or LSHIFTRT) in mode MODE. */
21285 bool
21286 rs6000_is_valid_shift_mask (rtx mask, rtx shift, machine_mode mode)
21288 int nb, ne;
21290 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
21291 return false;
21293 int n = GET_MODE_PRECISION (mode);
21294 int sh = -1;
21296 if (CONST_INT_P (XEXP (shift, 1)))
21298 sh = INTVAL (XEXP (shift, 1));
21299 if (sh < 0 || sh >= n)
21300 return false;
21303 rtx_code code = GET_CODE (shift);
21305 /* Convert any shift by 0 to a rotate, to simplify below code. */
21306 if (sh == 0)
21307 code = ROTATE;
21309 /* Convert rotate to simple shift if we can, to make analysis simpler. */
21310 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
21311 code = ASHIFT;
21312 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
21314 code = LSHIFTRT;
21315 sh = n - sh;
21318 /* DImode rotates need rld*. */
21319 if (mode == DImode && code == ROTATE)
21320 return (nb == 63 || ne == 0 || ne == sh);
21322 /* SImode rotates need rlw*. */
21323 if (mode == SImode && code == ROTATE)
21324 return (nb < 32 && ne < 32 && sh < 32);
21326 /* Wrap-around masks are only okay for rotates. */
21327 if (ne > nb)
21328 return false;
21330 /* Variable shifts are only okay for rotates. */
21331 if (sh < 0)
21332 return false;
21334 /* Don't allow ASHIFT if the mask is wrong for that. */
21335 if (code == ASHIFT && ne < sh)
21336 return false;
21338 /* If we can do it with an rlw*, we can do it. Don't allow LSHIFTRT
21339 if the mask is wrong for that. */
21340 if (nb < 32 && ne < 32 && sh < 32
21341 && !(code == LSHIFTRT && nb >= 32 - sh))
21342 return true;
21344 /* If we can do it with an rld*, we can do it. Don't allow LSHIFTRT
21345 if the mask is wrong for that. */
21346 if (code == LSHIFTRT)
21347 sh = 64 - sh;
21348 if (nb == 63 || ne == 0 || ne == sh)
21349 return !(code == LSHIFTRT && nb >= sh);
21351 return false;
21354 /* Return the instruction template for a shift with mask in mode MODE, with
21355 operands OPERANDS. If DOT is true, make it a record-form instruction. */
21357 const char *
21358 rs6000_insn_for_shift_mask (machine_mode mode, rtx *operands, bool dot)
21360 int nb, ne;
21362 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
21363 gcc_unreachable ();
21365 if (mode == DImode && ne == 0)
21367 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
21368 operands[2] = GEN_INT (64 - INTVAL (operands[2]));
21369 operands[3] = GEN_INT (63 - nb);
21370 if (dot)
21371 return "rld%I2cl. %0,%1,%2,%3";
21372 return "rld%I2cl %0,%1,%2,%3";
21375 if (mode == DImode && nb == 63)
21377 operands[3] = GEN_INT (63 - ne);
21378 if (dot)
21379 return "rld%I2cr. %0,%1,%2,%3";
21380 return "rld%I2cr %0,%1,%2,%3";
21383 if (mode == DImode
21384 && GET_CODE (operands[4]) != LSHIFTRT
21385 && CONST_INT_P (operands[2])
21386 && ne == INTVAL (operands[2]))
21388 operands[3] = GEN_INT (63 - nb);
21389 if (dot)
21390 return "rld%I2c. %0,%1,%2,%3";
21391 return "rld%I2c %0,%1,%2,%3";
21394 if (nb < 32 && ne < 32)
21396 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
21397 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
21398 operands[3] = GEN_INT (31 - nb);
21399 operands[4] = GEN_INT (31 - ne);
21400 /* This insn can also be a 64-bit rotate with mask that really makes
21401 it just a shift right (with mask); the %h below are to adjust for
21402 that situation (shift count is >= 32 in that case). */
21403 if (dot)
21404 return "rlw%I2nm. %0,%1,%h2,%3,%4";
21405 return "rlw%I2nm %0,%1,%h2,%3,%4";
21408 gcc_unreachable ();
21411 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwimi or
21412 rldimi instruction, to implement an insert with shift SHIFT (a ROTATE,
21413 ASHIFT, or LSHIFTRT) in mode MODE. */
21415 bool
21416 rs6000_is_valid_insert_mask (rtx mask, rtx shift, machine_mode mode)
21418 int nb, ne;
21420 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
21421 return false;
21423 int n = GET_MODE_PRECISION (mode);
21425 int sh = INTVAL (XEXP (shift, 1));
21426 if (sh < 0 || sh >= n)
21427 return false;
21429 rtx_code code = GET_CODE (shift);
21431 /* Convert any shift by 0 to a rotate, to simplify below code. */
21432 if (sh == 0)
21433 code = ROTATE;
21435 /* Convert rotate to simple shift if we can, to make analysis simpler. */
21436 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
21437 code = ASHIFT;
21438 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
21440 code = LSHIFTRT;
21441 sh = n - sh;
21444 /* DImode rotates need rldimi. */
21445 if (mode == DImode && code == ROTATE)
21446 return (ne == sh);
21448 /* SImode rotates need rlwimi. */
21449 if (mode == SImode && code == ROTATE)
21450 return (nb < 32 && ne < 32 && sh < 32);
21452 /* Wrap-around masks are only okay for rotates. */
21453 if (ne > nb)
21454 return false;
21456 /* Don't allow ASHIFT if the mask is wrong for that. */
21457 if (code == ASHIFT && ne < sh)
21458 return false;
21460 /* If we can do it with an rlwimi, we can do it. Don't allow LSHIFTRT
21461 if the mask is wrong for that. */
21462 if (nb < 32 && ne < 32 && sh < 32
21463 && !(code == LSHIFTRT && nb >= 32 - sh))
21464 return true;
21466 /* If we can do it with an rldimi, we can do it. Don't allow LSHIFTRT
21467 if the mask is wrong for that. */
21468 if (code == LSHIFTRT)
21469 sh = 64 - sh;
21470 if (ne == sh)
21471 return !(code == LSHIFTRT && nb >= sh);
21473 return false;
21476 /* Return the instruction template for an insert with mask in mode MODE, with
21477 operands OPERANDS. If DOT is true, make it a record-form instruction. */
21479 const char *
21480 rs6000_insn_for_insert_mask (machine_mode mode, rtx *operands, bool dot)
21482 int nb, ne;
21484 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
21485 gcc_unreachable ();
21487 /* Prefer rldimi because rlwimi is cracked. */
21488 if (TARGET_POWERPC64
21489 && (!dot || mode == DImode)
21490 && GET_CODE (operands[4]) != LSHIFTRT
21491 && ne == INTVAL (operands[2]))
21493 operands[3] = GEN_INT (63 - nb);
21494 if (dot)
21495 return "rldimi. %0,%1,%2,%3";
21496 return "rldimi %0,%1,%2,%3";
21499 if (nb < 32 && ne < 32)
21501 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
21502 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
21503 operands[3] = GEN_INT (31 - nb);
21504 operands[4] = GEN_INT (31 - ne);
21505 if (dot)
21506 return "rlwimi. %0,%1,%2,%3,%4";
21507 return "rlwimi %0,%1,%2,%3,%4";
21510 gcc_unreachable ();
21513 /* Return whether an AND with C (a CONST_INT) in mode MODE can be done
21514 using two machine instructions. */
21516 bool
21517 rs6000_is_valid_2insn_and (rtx c, machine_mode mode)
21519 /* There are two kinds of AND we can handle with two insns:
21520 1) those we can do with two rl* insn;
21521 2) ori[s];xori[s].
21523 We do not handle that last case yet. */
21525 /* If there is just one stretch of ones, we can do it. */
21526 if (rs6000_is_valid_mask (c, NULL, NULL, mode))
21527 return true;
21529 /* Otherwise, fill in the lowest "hole"; if we can do the result with
21530 one insn, we can do the whole thing with two. */
21531 unsigned HOST_WIDE_INT val = INTVAL (c);
21532 unsigned HOST_WIDE_INT bit1 = val & -val;
21533 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
21534 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
21535 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
21536 return rs6000_is_valid_and_mask (GEN_INT (val + bit3 - bit2), mode);
21539 /* Emit the two insns to do an AND in mode MODE, with operands OPERANDS.
21540 If EXPAND is true, split rotate-and-mask instructions we generate to
21541 their constituent parts as well (this is used during expand); if DOT
21542 is 1, make the last insn a record-form instruction clobbering the
21543 destination GPR and setting the CC reg (from operands[3]); if 2, set
21544 that GPR as well as the CC reg. */
21546 void
21547 rs6000_emit_2insn_and (machine_mode mode, rtx *operands, bool expand, int dot)
21549 gcc_assert (!(expand && dot));
21551 unsigned HOST_WIDE_INT val = INTVAL (operands[2]);
21553 /* If it is one stretch of ones, it is DImode; shift left, mask, then
21554 shift right. This generates better code than doing the masks without
21555 shifts, or shifting first right and then left. */
21556 int nb, ne;
21557 if (rs6000_is_valid_mask (operands[2], &nb, &ne, mode) && nb >= ne)
21559 gcc_assert (mode == DImode);
21561 int shift = 63 - nb;
21562 if (expand)
21564 rtx tmp1 = gen_reg_rtx (DImode);
21565 rtx tmp2 = gen_reg_rtx (DImode);
21566 emit_insn (gen_ashldi3 (tmp1, operands[1], GEN_INT (shift)));
21567 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (val << shift)));
21568 emit_insn (gen_lshrdi3 (operands[0], tmp2, GEN_INT (shift)));
21570 else
21572 rtx tmp = gen_rtx_ASHIFT (mode, operands[1], GEN_INT (shift));
21573 tmp = gen_rtx_AND (mode, tmp, GEN_INT (val << shift));
21574 emit_move_insn (operands[0], tmp);
21575 tmp = gen_rtx_LSHIFTRT (mode, operands[0], GEN_INT (shift));
21576 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21578 return;
21581 /* Otherwise, make a mask2 that cuts out the lowest "hole", and a mask1
21582 that does the rest. */
21583 unsigned HOST_WIDE_INT bit1 = val & -val;
21584 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
21585 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
21586 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
21588 unsigned HOST_WIDE_INT mask1 = -bit3 + bit2 - 1;
21589 unsigned HOST_WIDE_INT mask2 = val + bit3 - bit2;
21591 gcc_assert (rs6000_is_valid_and_mask (GEN_INT (mask2), mode));
21593 /* Two "no-rotate"-and-mask instructions, for SImode. */
21594 if (rs6000_is_valid_and_mask (GEN_INT (mask1), mode))
21596 gcc_assert (mode == SImode);
21598 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
21599 rtx tmp = gen_rtx_AND (mode, operands[1], GEN_INT (mask1));
21600 emit_move_insn (reg, tmp);
21601 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
21602 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21603 return;
21606 gcc_assert (mode == DImode);
21608 /* Two "no-rotate"-and-mask instructions, for DImode: both are rlwinm
21609 insns; we have to do the first in SImode, because it wraps. */
21610 if (mask2 <= 0xffffffff
21611 && rs6000_is_valid_and_mask (GEN_INT (mask1), SImode))
21613 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
21614 rtx tmp = gen_rtx_AND (SImode, gen_lowpart (SImode, operands[1]),
21615 GEN_INT (mask1));
21616 rtx reg_low = gen_lowpart (SImode, reg);
21617 emit_move_insn (reg_low, tmp);
21618 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
21619 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21620 return;
21623 /* Two rld* insns: rotate, clear the hole in the middle (which now is
21624 at the top end), rotate back and clear the other hole. */
21625 int right = exact_log2 (bit3);
21626 int left = 64 - right;
21628 /* Rotate the mask too. */
21629 mask1 = (mask1 >> right) | ((bit2 - 1) << left);
21631 if (expand)
21633 rtx tmp1 = gen_reg_rtx (DImode);
21634 rtx tmp2 = gen_reg_rtx (DImode);
21635 rtx tmp3 = gen_reg_rtx (DImode);
21636 emit_insn (gen_rotldi3 (tmp1, operands[1], GEN_INT (left)));
21637 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (mask1)));
21638 emit_insn (gen_rotldi3 (tmp3, tmp2, GEN_INT (right)));
21639 emit_insn (gen_anddi3 (operands[0], tmp3, GEN_INT (mask2)));
21641 else
21643 rtx tmp = gen_rtx_ROTATE (mode, operands[1], GEN_INT (left));
21644 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask1));
21645 emit_move_insn (operands[0], tmp);
21646 tmp = gen_rtx_ROTATE (mode, operands[0], GEN_INT (right));
21647 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask2));
21648 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
21652 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
21653 for lfq and stfq insns iff the registers are hard registers. */
21656 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
21658 /* We might have been passed a SUBREG. */
21659 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
21660 return 0;
21662 /* We might have been passed non floating point registers. */
21663 if (!FP_REGNO_P (REGNO (reg1))
21664 || !FP_REGNO_P (REGNO (reg2)))
21665 return 0;
21667 return (REGNO (reg1) == REGNO (reg2) - 1);
21670 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
21671 addr1 and addr2 must be in consecutive memory locations
21672 (addr2 == addr1 + 8). */
21675 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
21677 rtx addr1, addr2;
21678 unsigned int reg1, reg2;
21679 int offset1, offset2;
21681 /* The mems cannot be volatile. */
21682 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
21683 return 0;
21685 addr1 = XEXP (mem1, 0);
21686 addr2 = XEXP (mem2, 0);
21688 /* Extract an offset (if used) from the first addr. */
21689 if (GET_CODE (addr1) == PLUS)
21691 /* If not a REG, return zero. */
21692 if (GET_CODE (XEXP (addr1, 0)) != REG)
21693 return 0;
21694 else
21696 reg1 = REGNO (XEXP (addr1, 0));
21697 /* The offset must be constant! */
21698 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
21699 return 0;
21700 offset1 = INTVAL (XEXP (addr1, 1));
21703 else if (GET_CODE (addr1) != REG)
21704 return 0;
21705 else
21707 reg1 = REGNO (addr1);
21708 /* This was a simple (mem (reg)) expression. Offset is 0. */
21709 offset1 = 0;
21712 /* And now for the second addr. */
21713 if (GET_CODE (addr2) == PLUS)
21715 /* If not a REG, return zero. */
21716 if (GET_CODE (XEXP (addr2, 0)) != REG)
21717 return 0;
21718 else
21720 reg2 = REGNO (XEXP (addr2, 0));
21721 /* The offset must be constant. */
21722 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
21723 return 0;
21724 offset2 = INTVAL (XEXP (addr2, 1));
21727 else if (GET_CODE (addr2) != REG)
21728 return 0;
21729 else
21731 reg2 = REGNO (addr2);
21732 /* This was a simple (mem (reg)) expression. Offset is 0. */
21733 offset2 = 0;
21736 /* Both of these must have the same base register. */
21737 if (reg1 != reg2)
21738 return 0;
21740 /* The offset for the second addr must be 8 more than the first addr. */
21741 if (offset2 != offset1 + 8)
21742 return 0;
21744 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
21745 instructions. */
21746 return 1;
21751 rs6000_secondary_memory_needed_rtx (machine_mode mode)
21753 static bool eliminated = false;
21754 rtx ret;
21756 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
21757 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
21758 else
21760 rtx mem = cfun->machine->sdmode_stack_slot;
21761 gcc_assert (mem != NULL_RTX);
21763 if (!eliminated)
21765 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
21766 cfun->machine->sdmode_stack_slot = mem;
21767 eliminated = true;
21769 ret = mem;
21772 if (TARGET_DEBUG_ADDR)
21774 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
21775 GET_MODE_NAME (mode));
21776 if (!ret)
21777 fprintf (stderr, "\tNULL_RTX\n");
21778 else
21779 debug_rtx (ret);
21782 return ret;
21785 /* Return the mode to be used for memory when a secondary memory
21786 location is needed. For SDmode values we need to use DDmode, in
21787 all other cases we can use the same mode. */
21788 machine_mode
21789 rs6000_secondary_memory_needed_mode (machine_mode mode)
21791 if (lra_in_progress && mode == SDmode)
21792 return DDmode;
21793 return mode;
21796 static tree
21797 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21799 /* Don't walk into types. */
21800 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
21802 *walk_subtrees = 0;
21803 return NULL_TREE;
21806 switch (TREE_CODE (*tp))
21808 case VAR_DECL:
21809 case PARM_DECL:
21810 case FIELD_DECL:
21811 case RESULT_DECL:
21812 case SSA_NAME:
21813 case REAL_CST:
21814 case MEM_REF:
21815 case VIEW_CONVERT_EXPR:
21816 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
21817 return *tp;
21818 break;
21819 default:
21820 break;
21823 return NULL_TREE;
21826 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
21827 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
21828 only work on the traditional altivec registers, note if an altivec register
21829 was chosen. */
21831 static enum rs6000_reg_type
21832 register_to_reg_type (rtx reg, bool *is_altivec)
21834 HOST_WIDE_INT regno;
21835 enum reg_class rclass;
21837 if (GET_CODE (reg) == SUBREG)
21838 reg = SUBREG_REG (reg);
21840 if (!REG_P (reg))
21841 return NO_REG_TYPE;
21843 regno = REGNO (reg);
21844 if (regno >= FIRST_PSEUDO_REGISTER)
21846 if (!lra_in_progress && !reload_in_progress && !reload_completed)
21847 return PSEUDO_REG_TYPE;
21849 regno = true_regnum (reg);
21850 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
21851 return PSEUDO_REG_TYPE;
21854 gcc_assert (regno >= 0);
21856 if (is_altivec && ALTIVEC_REGNO_P (regno))
21857 *is_altivec = true;
21859 rclass = rs6000_regno_regclass[regno];
21860 return reg_class_to_reg_type[(int)rclass];
21863 /* Helper function to return the cost of adding a TOC entry address. */
21865 static inline int
21866 rs6000_secondary_reload_toc_costs (addr_mask_type addr_mask)
21868 int ret;
21870 if (TARGET_CMODEL != CMODEL_SMALL)
21871 ret = ((addr_mask & RELOAD_REG_OFFSET) == 0) ? 1 : 2;
21873 else
21874 ret = (TARGET_MINIMAL_TOC) ? 6 : 3;
21876 return ret;
21879 /* Helper function for rs6000_secondary_reload to determine whether the memory
21880 address (ADDR) with a given register class (RCLASS) and machine mode (MODE)
21881 needs reloading. Return negative if the memory is not handled by the memory
21882 helper functions and to try a different reload method, 0 if no additional
21883 instructions are need, and positive to give the extra cost for the
21884 memory. */
21886 static int
21887 rs6000_secondary_reload_memory (rtx addr,
21888 enum reg_class rclass,
21889 machine_mode mode)
21891 int extra_cost = 0;
21892 rtx reg, and_arg, plus_arg0, plus_arg1;
21893 addr_mask_type addr_mask;
21894 const char *type = NULL;
21895 const char *fail_msg = NULL;
21897 if (GPR_REG_CLASS_P (rclass))
21898 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
21900 else if (rclass == FLOAT_REGS)
21901 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
21903 else if (rclass == ALTIVEC_REGS)
21904 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
21906 /* For the combined VSX_REGS, turn off Altivec AND -16. */
21907 else if (rclass == VSX_REGS)
21908 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_VMX]
21909 & ~RELOAD_REG_AND_M16);
21911 /* If the register allocator hasn't made up its mind yet on the register
21912 class to use, settle on defaults to use. */
21913 else if (rclass == NO_REGS)
21915 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_ANY]
21916 & ~RELOAD_REG_AND_M16);
21918 if ((addr_mask & RELOAD_REG_MULTIPLE) != 0)
21919 addr_mask &= ~(RELOAD_REG_INDEXED
21920 | RELOAD_REG_PRE_INCDEC
21921 | RELOAD_REG_PRE_MODIFY);
21924 else
21925 addr_mask = 0;
21927 /* If the register isn't valid in this register class, just return now. */
21928 if ((addr_mask & RELOAD_REG_VALID) == 0)
21930 if (TARGET_DEBUG_ADDR)
21932 fprintf (stderr,
21933 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
21934 "not valid in class\n",
21935 GET_MODE_NAME (mode), reg_class_names[rclass]);
21936 debug_rtx (addr);
21939 return -1;
21942 switch (GET_CODE (addr))
21944 /* Does the register class supports auto update forms for this mode? We
21945 don't need a scratch register, since the powerpc only supports
21946 PRE_INC, PRE_DEC, and PRE_MODIFY. */
21947 case PRE_INC:
21948 case PRE_DEC:
21949 reg = XEXP (addr, 0);
21950 if (!base_reg_operand (addr, GET_MODE (reg)))
21952 fail_msg = "no base register #1";
21953 extra_cost = -1;
21956 else if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
21958 extra_cost = 1;
21959 type = "update";
21961 break;
21963 case PRE_MODIFY:
21964 reg = XEXP (addr, 0);
21965 plus_arg1 = XEXP (addr, 1);
21966 if (!base_reg_operand (reg, GET_MODE (reg))
21967 || GET_CODE (plus_arg1) != PLUS
21968 || !rtx_equal_p (reg, XEXP (plus_arg1, 0)))
21970 fail_msg = "bad PRE_MODIFY";
21971 extra_cost = -1;
21974 else if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
21976 extra_cost = 1;
21977 type = "update";
21979 break;
21981 /* Do we need to simulate AND -16 to clear the bottom address bits used
21982 in VMX load/stores? Only allow the AND for vector sizes. */
21983 case AND:
21984 and_arg = XEXP (addr, 0);
21985 if (GET_MODE_SIZE (mode) != 16
21986 || GET_CODE (XEXP (addr, 1)) != CONST_INT
21987 || INTVAL (XEXP (addr, 1)) != -16)
21989 fail_msg = "bad Altivec AND #1";
21990 extra_cost = -1;
21993 if (rclass != ALTIVEC_REGS)
21995 if (legitimate_indirect_address_p (and_arg, false))
21996 extra_cost = 1;
21998 else if (legitimate_indexed_address_p (and_arg, false))
21999 extra_cost = 2;
22001 else
22003 fail_msg = "bad Altivec AND #2";
22004 extra_cost = -1;
22007 type = "and";
22009 break;
22011 /* If this is an indirect address, make sure it is a base register. */
22012 case REG:
22013 case SUBREG:
22014 if (!legitimate_indirect_address_p (addr, false))
22016 extra_cost = 1;
22017 type = "move";
22019 break;
22021 /* If this is an indexed address, make sure the register class can handle
22022 indexed addresses for this mode. */
22023 case PLUS:
22024 plus_arg0 = XEXP (addr, 0);
22025 plus_arg1 = XEXP (addr, 1);
22027 /* (plus (plus (reg) (constant)) (constant)) is generated during
22028 push_reload processing, so handle it now. */
22029 if (GET_CODE (plus_arg0) == PLUS && CONST_INT_P (plus_arg1))
22031 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22033 extra_cost = 1;
22034 type = "offset";
22038 /* (plus (plus (reg) (constant)) (reg)) is also generated during
22039 push_reload processing, so handle it now. */
22040 else if (GET_CODE (plus_arg0) == PLUS && REG_P (plus_arg1))
22042 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
22044 extra_cost = 1;
22045 type = "indexed #2";
22049 else if (!base_reg_operand (plus_arg0, GET_MODE (plus_arg0)))
22051 fail_msg = "no base register #2";
22052 extra_cost = -1;
22055 else if (int_reg_operand (plus_arg1, GET_MODE (plus_arg1)))
22057 if ((addr_mask & RELOAD_REG_INDEXED) == 0
22058 || !legitimate_indexed_address_p (addr, false))
22060 extra_cost = 1;
22061 type = "indexed";
22065 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0
22066 && CONST_INT_P (plus_arg1))
22068 if (!quad_address_offset_p (INTVAL (plus_arg1)))
22070 extra_cost = 1;
22071 type = "vector d-form offset";
22075 /* Make sure the register class can handle offset addresses. */
22076 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
22078 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22080 extra_cost = 1;
22081 type = "offset #2";
22085 else
22087 fail_msg = "bad PLUS";
22088 extra_cost = -1;
22091 break;
22093 case LO_SUM:
22094 /* Quad offsets are restricted and can't handle normal addresses. */
22095 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
22097 extra_cost = -1;
22098 type = "vector d-form lo_sum";
22101 else if (!legitimate_lo_sum_address_p (mode, addr, false))
22103 fail_msg = "bad LO_SUM";
22104 extra_cost = -1;
22107 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22109 extra_cost = 1;
22110 type = "lo_sum";
22112 break;
22114 /* Static addresses need to create a TOC entry. */
22115 case CONST:
22116 case SYMBOL_REF:
22117 case LABEL_REF:
22118 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
22120 extra_cost = -1;
22121 type = "vector d-form lo_sum #2";
22124 else
22126 type = "address";
22127 extra_cost = rs6000_secondary_reload_toc_costs (addr_mask);
22129 break;
22131 /* TOC references look like offsetable memory. */
22132 case UNSPEC:
22133 if (TARGET_CMODEL == CMODEL_SMALL || XINT (addr, 1) != UNSPEC_TOCREL)
22135 fail_msg = "bad UNSPEC";
22136 extra_cost = -1;
22139 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
22141 extra_cost = -1;
22142 type = "vector d-form lo_sum #3";
22145 else if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22147 extra_cost = 1;
22148 type = "toc reference";
22150 break;
22152 default:
22154 fail_msg = "bad address";
22155 extra_cost = -1;
22159 if (TARGET_DEBUG_ADDR /* && extra_cost != 0 */)
22161 if (extra_cost < 0)
22162 fprintf (stderr,
22163 "rs6000_secondary_reload_memory error: mode = %s, "
22164 "class = %s, addr_mask = '%s', %s\n",
22165 GET_MODE_NAME (mode),
22166 reg_class_names[rclass],
22167 rs6000_debug_addr_mask (addr_mask, false),
22168 (fail_msg != NULL) ? fail_msg : "<bad address>");
22170 else
22171 fprintf (stderr,
22172 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
22173 "addr_mask = '%s', extra cost = %d, %s\n",
22174 GET_MODE_NAME (mode),
22175 reg_class_names[rclass],
22176 rs6000_debug_addr_mask (addr_mask, false),
22177 extra_cost,
22178 (type) ? type : "<none>");
22180 debug_rtx (addr);
22183 return extra_cost;
22186 /* Helper function for rs6000_secondary_reload to return true if a move to a
22187 different register classe is really a simple move. */
22189 static bool
22190 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
22191 enum rs6000_reg_type from_type,
22192 machine_mode mode)
22194 int size = GET_MODE_SIZE (mode);
22196 /* Add support for various direct moves available. In this function, we only
22197 look at cases where we don't need any extra registers, and one or more
22198 simple move insns are issued. Originally small integers are not allowed
22199 in FPR/VSX registers. Single precision binary floating is not a simple
22200 move because we need to convert to the single precision memory layout.
22201 The 4-byte SDmode can be moved. TDmode values are disallowed since they
22202 need special direct move handling, which we do not support yet. */
22203 if (TARGET_DIRECT_MOVE
22204 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
22205 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
22207 if (TARGET_POWERPC64)
22209 /* ISA 2.07: MTVSRD or MVFVSRD. */
22210 if (size == 8)
22211 return true;
22213 /* ISA 3.0: MTVSRDD or MFVSRD + MFVSRLD. */
22214 if (size == 16 && TARGET_P9_VECTOR && mode != TDmode)
22215 return true;
22218 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
22219 if (TARGET_VSX_SMALL_INTEGER)
22221 if (mode == SImode)
22222 return true;
22224 if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
22225 return true;
22228 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
22229 if (mode == SDmode)
22230 return true;
22233 /* Power6+: MFTGPR or MFFGPR. */
22234 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
22235 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
22236 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
22237 return true;
22239 /* Move to/from SPR. */
22240 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
22241 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
22242 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
22243 return true;
22245 return false;
22248 /* Direct move helper function for rs6000_secondary_reload, handle all of the
22249 special direct moves that involve allocating an extra register, return the
22250 insn code of the helper function if there is such a function or
22251 CODE_FOR_nothing if not. */
22253 static bool
22254 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
22255 enum rs6000_reg_type from_type,
22256 machine_mode mode,
22257 secondary_reload_info *sri,
22258 bool altivec_p)
22260 bool ret = false;
22261 enum insn_code icode = CODE_FOR_nothing;
22262 int cost = 0;
22263 int size = GET_MODE_SIZE (mode);
22265 if (TARGET_POWERPC64 && size == 16)
22267 /* Handle moving 128-bit values from GPRs to VSX point registers on
22268 ISA 2.07 (power8, power9) when running in 64-bit mode using
22269 XXPERMDI to glue the two 64-bit values back together. */
22270 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
22272 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
22273 icode = reg_addr[mode].reload_vsx_gpr;
22276 /* Handle moving 128-bit values from VSX point registers to GPRs on
22277 ISA 2.07 when running in 64-bit mode using XXPERMDI to get access to the
22278 bottom 64-bit value. */
22279 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
22281 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
22282 icode = reg_addr[mode].reload_gpr_vsx;
22286 else if (TARGET_POWERPC64 && mode == SFmode)
22288 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
22290 cost = 3; /* xscvdpspn, mfvsrd, and. */
22291 icode = reg_addr[mode].reload_gpr_vsx;
22294 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
22296 cost = 2; /* mtvsrz, xscvspdpn. */
22297 icode = reg_addr[mode].reload_vsx_gpr;
22301 else if (!TARGET_POWERPC64 && size == 8)
22303 /* Handle moving 64-bit values from GPRs to floating point registers on
22304 ISA 2.07 when running in 32-bit mode using FMRGOW to glue the two
22305 32-bit values back together. Altivec register classes must be handled
22306 specially since a different instruction is used, and the secondary
22307 reload support requires a single instruction class in the scratch
22308 register constraint. However, right now TFmode is not allowed in
22309 Altivec registers, so the pattern will never match. */
22310 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
22312 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
22313 icode = reg_addr[mode].reload_fpr_gpr;
22317 if (icode != CODE_FOR_nothing)
22319 ret = true;
22320 if (sri)
22322 sri->icode = icode;
22323 sri->extra_cost = cost;
22327 return ret;
22330 /* Return whether a move between two register classes can be done either
22331 directly (simple move) or via a pattern that uses a single extra temporary
22332 (using ISA 2.07's direct move in this case. */
22334 static bool
22335 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
22336 enum rs6000_reg_type from_type,
22337 machine_mode mode,
22338 secondary_reload_info *sri,
22339 bool altivec_p)
22341 /* Fall back to load/store reloads if either type is not a register. */
22342 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
22343 return false;
22345 /* If we haven't allocated registers yet, assume the move can be done for the
22346 standard register types. */
22347 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
22348 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
22349 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
22350 return true;
22352 /* Moves to the same set of registers is a simple move for non-specialized
22353 registers. */
22354 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
22355 return true;
22357 /* Check whether a simple move can be done directly. */
22358 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
22360 if (sri)
22362 sri->icode = CODE_FOR_nothing;
22363 sri->extra_cost = 0;
22365 return true;
22368 /* Now check if we can do it in a few steps. */
22369 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
22370 altivec_p);
22373 /* Inform reload about cases where moving X with a mode MODE to a register in
22374 RCLASS requires an extra scratch or immediate register. Return the class
22375 needed for the immediate register.
22377 For VSX and Altivec, we may need a register to convert sp+offset into
22378 reg+sp.
22380 For misaligned 64-bit gpr loads and stores we need a register to
22381 convert an offset address to indirect. */
22383 static reg_class_t
22384 rs6000_secondary_reload (bool in_p,
22385 rtx x,
22386 reg_class_t rclass_i,
22387 machine_mode mode,
22388 secondary_reload_info *sri)
22390 enum reg_class rclass = (enum reg_class) rclass_i;
22391 reg_class_t ret = ALL_REGS;
22392 enum insn_code icode;
22393 bool default_p = false;
22394 bool done_p = false;
22396 /* Allow subreg of memory before/during reload. */
22397 bool memory_p = (MEM_P (x)
22398 || (!reload_completed && GET_CODE (x) == SUBREG
22399 && MEM_P (SUBREG_REG (x))));
22401 sri->icode = CODE_FOR_nothing;
22402 sri->t_icode = CODE_FOR_nothing;
22403 sri->extra_cost = 0;
22404 icode = ((in_p)
22405 ? reg_addr[mode].reload_load
22406 : reg_addr[mode].reload_store);
22408 if (REG_P (x) || register_operand (x, mode))
22410 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
22411 bool altivec_p = (rclass == ALTIVEC_REGS);
22412 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
22414 if (!in_p)
22415 std::swap (to_type, from_type);
22417 /* Can we do a direct move of some sort? */
22418 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
22419 altivec_p))
22421 icode = (enum insn_code)sri->icode;
22422 default_p = false;
22423 done_p = true;
22424 ret = NO_REGS;
22428 /* Make sure 0.0 is not reloaded or forced into memory. */
22429 if (x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
22431 ret = NO_REGS;
22432 default_p = false;
22433 done_p = true;
22436 /* If this is a scalar floating point value and we want to load it into the
22437 traditional Altivec registers, do it via a move via a traditional floating
22438 point register, unless we have D-form addressing. Also make sure that
22439 non-zero constants use a FPR. */
22440 if (!done_p && reg_addr[mode].scalar_in_vmx_p
22441 && !mode_supports_vmx_dform (mode)
22442 && (rclass == VSX_REGS || rclass == ALTIVEC_REGS)
22443 && (memory_p || (GET_CODE (x) == CONST_DOUBLE)))
22445 ret = FLOAT_REGS;
22446 default_p = false;
22447 done_p = true;
22450 /* Handle reload of load/stores if we have reload helper functions. */
22451 if (!done_p && icode != CODE_FOR_nothing && memory_p)
22453 int extra_cost = rs6000_secondary_reload_memory (XEXP (x, 0), rclass,
22454 mode);
22456 if (extra_cost >= 0)
22458 done_p = true;
22459 ret = NO_REGS;
22460 if (extra_cost > 0)
22462 sri->extra_cost = extra_cost;
22463 sri->icode = icode;
22468 /* Handle unaligned loads and stores of integer registers. */
22469 if (!done_p && TARGET_POWERPC64
22470 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
22471 && memory_p
22472 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
22474 rtx addr = XEXP (x, 0);
22475 rtx off = address_offset (addr);
22477 if (off != NULL_RTX)
22479 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
22480 unsigned HOST_WIDE_INT offset = INTVAL (off);
22482 /* We need a secondary reload when our legitimate_address_p
22483 says the address is good (as otherwise the entire address
22484 will be reloaded), and the offset is not a multiple of
22485 four or we have an address wrap. Address wrap will only
22486 occur for LO_SUMs since legitimate_offset_address_p
22487 rejects addresses for 16-byte mems that will wrap. */
22488 if (GET_CODE (addr) == LO_SUM
22489 ? (1 /* legitimate_address_p allows any offset for lo_sum */
22490 && ((offset & 3) != 0
22491 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
22492 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
22493 && (offset & 3) != 0))
22495 /* -m32 -mpowerpc64 needs to use a 32-bit scratch register. */
22496 if (in_p)
22497 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_load
22498 : CODE_FOR_reload_di_load);
22499 else
22500 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_store
22501 : CODE_FOR_reload_di_store);
22502 sri->extra_cost = 2;
22503 ret = NO_REGS;
22504 done_p = true;
22506 else
22507 default_p = true;
22509 else
22510 default_p = true;
22513 if (!done_p && !TARGET_POWERPC64
22514 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
22515 && memory_p
22516 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
22518 rtx addr = XEXP (x, 0);
22519 rtx off = address_offset (addr);
22521 if (off != NULL_RTX)
22523 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
22524 unsigned HOST_WIDE_INT offset = INTVAL (off);
22526 /* We need a secondary reload when our legitimate_address_p
22527 says the address is good (as otherwise the entire address
22528 will be reloaded), and we have a wrap.
22530 legitimate_lo_sum_address_p allows LO_SUM addresses to
22531 have any offset so test for wrap in the low 16 bits.
22533 legitimate_offset_address_p checks for the range
22534 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
22535 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
22536 [0x7ff4,0x7fff] respectively, so test for the
22537 intersection of these ranges, [0x7ffc,0x7fff] and
22538 [0x7ff4,0x7ff7] respectively.
22540 Note that the address we see here may have been
22541 manipulated by legitimize_reload_address. */
22542 if (GET_CODE (addr) == LO_SUM
22543 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
22544 : offset - (0x8000 - extra) < UNITS_PER_WORD)
22546 if (in_p)
22547 sri->icode = CODE_FOR_reload_si_load;
22548 else
22549 sri->icode = CODE_FOR_reload_si_store;
22550 sri->extra_cost = 2;
22551 ret = NO_REGS;
22552 done_p = true;
22554 else
22555 default_p = true;
22557 else
22558 default_p = true;
22561 if (!done_p)
22562 default_p = true;
22564 if (default_p)
22565 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
22567 gcc_assert (ret != ALL_REGS);
22569 if (TARGET_DEBUG_ADDR)
22571 fprintf (stderr,
22572 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
22573 "mode = %s",
22574 reg_class_names[ret],
22575 in_p ? "true" : "false",
22576 reg_class_names[rclass],
22577 GET_MODE_NAME (mode));
22579 if (reload_completed)
22580 fputs (", after reload", stderr);
22582 if (!done_p)
22583 fputs (", done_p not set", stderr);
22585 if (default_p)
22586 fputs (", default secondary reload", stderr);
22588 if (sri->icode != CODE_FOR_nothing)
22589 fprintf (stderr, ", reload func = %s, extra cost = %d",
22590 insn_data[sri->icode].name, sri->extra_cost);
22592 else if (sri->extra_cost > 0)
22593 fprintf (stderr, ", extra cost = %d", sri->extra_cost);
22595 fputs ("\n", stderr);
22596 debug_rtx (x);
22599 return ret;
22602 /* Better tracing for rs6000_secondary_reload_inner. */
22604 static void
22605 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
22606 bool store_p)
22608 rtx set, clobber;
22610 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
22612 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
22613 store_p ? "store" : "load");
22615 if (store_p)
22616 set = gen_rtx_SET (mem, reg);
22617 else
22618 set = gen_rtx_SET (reg, mem);
22620 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
22621 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
22624 static void rs6000_secondary_reload_fail (int, rtx, rtx, rtx, bool)
22625 ATTRIBUTE_NORETURN;
22627 static void
22628 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
22629 bool store_p)
22631 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
22632 gcc_unreachable ();
22635 /* Fixup reload addresses for values in GPR, FPR, and VMX registers that have
22636 reload helper functions. These were identified in
22637 rs6000_secondary_reload_memory, and if reload decided to use the secondary
22638 reload, it calls the insns:
22639 reload_<RELOAD:mode>_<P:mptrsize>_store
22640 reload_<RELOAD:mode>_<P:mptrsize>_load
22642 which in turn calls this function, to do whatever is necessary to create
22643 valid addresses. */
22645 void
22646 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
22648 int regno = true_regnum (reg);
22649 machine_mode mode = GET_MODE (reg);
22650 addr_mask_type addr_mask;
22651 rtx addr;
22652 rtx new_addr;
22653 rtx op_reg, op0, op1;
22654 rtx and_op;
22655 rtx cc_clobber;
22656 rtvec rv;
22658 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER || !MEM_P (mem)
22659 || !base_reg_operand (scratch, GET_MODE (scratch)))
22660 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22662 if (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO))
22663 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
22665 else if (IN_RANGE (regno, FIRST_FPR_REGNO, LAST_FPR_REGNO))
22666 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
22668 else if (IN_RANGE (regno, FIRST_ALTIVEC_REGNO, LAST_ALTIVEC_REGNO))
22669 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
22671 else
22672 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22674 /* Make sure the mode is valid in this register class. */
22675 if ((addr_mask & RELOAD_REG_VALID) == 0)
22676 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22678 if (TARGET_DEBUG_ADDR)
22679 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
22681 new_addr = addr = XEXP (mem, 0);
22682 switch (GET_CODE (addr))
22684 /* Does the register class support auto update forms for this mode? If
22685 not, do the update now. We don't need a scratch register, since the
22686 powerpc only supports PRE_INC, PRE_DEC, and PRE_MODIFY. */
22687 case PRE_INC:
22688 case PRE_DEC:
22689 op_reg = XEXP (addr, 0);
22690 if (!base_reg_operand (op_reg, Pmode))
22691 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22693 if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
22695 emit_insn (gen_add2_insn (op_reg, GEN_INT (GET_MODE_SIZE (mode))));
22696 new_addr = op_reg;
22698 break;
22700 case PRE_MODIFY:
22701 op0 = XEXP (addr, 0);
22702 op1 = XEXP (addr, 1);
22703 if (!base_reg_operand (op0, Pmode)
22704 || GET_CODE (op1) != PLUS
22705 || !rtx_equal_p (op0, XEXP (op1, 0)))
22706 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22708 if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
22710 emit_insn (gen_rtx_SET (op0, op1));
22711 new_addr = reg;
22713 break;
22715 /* Do we need to simulate AND -16 to clear the bottom address bits used
22716 in VMX load/stores? */
22717 case AND:
22718 op0 = XEXP (addr, 0);
22719 op1 = XEXP (addr, 1);
22720 if ((addr_mask & RELOAD_REG_AND_M16) == 0)
22722 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
22723 op_reg = op0;
22725 else if (GET_CODE (op1) == PLUS)
22727 emit_insn (gen_rtx_SET (scratch, op1));
22728 op_reg = scratch;
22731 else
22732 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22734 and_op = gen_rtx_AND (GET_MODE (scratch), op_reg, op1);
22735 cc_clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (CCmode));
22736 rv = gen_rtvec (2, gen_rtx_SET (scratch, and_op), cc_clobber);
22737 emit_insn (gen_rtx_PARALLEL (VOIDmode, rv));
22738 new_addr = scratch;
22740 break;
22742 /* If this is an indirect address, make sure it is a base register. */
22743 case REG:
22744 case SUBREG:
22745 if (!base_reg_operand (addr, GET_MODE (addr)))
22747 emit_insn (gen_rtx_SET (scratch, addr));
22748 new_addr = scratch;
22750 break;
22752 /* If this is an indexed address, make sure the register class can handle
22753 indexed addresses for this mode. */
22754 case PLUS:
22755 op0 = XEXP (addr, 0);
22756 op1 = XEXP (addr, 1);
22757 if (!base_reg_operand (op0, Pmode))
22758 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22760 else if (int_reg_operand (op1, Pmode))
22762 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
22764 emit_insn (gen_rtx_SET (scratch, addr));
22765 new_addr = scratch;
22769 else if (mode_supports_vsx_dform_quad (mode) && CONST_INT_P (op1))
22771 if (((addr_mask & RELOAD_REG_QUAD_OFFSET) == 0)
22772 || !quad_address_p (addr, mode, false))
22774 emit_insn (gen_rtx_SET (scratch, addr));
22775 new_addr = scratch;
22779 /* Make sure the register class can handle offset addresses. */
22780 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
22782 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22784 emit_insn (gen_rtx_SET (scratch, addr));
22785 new_addr = scratch;
22789 else
22790 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22792 break;
22794 case LO_SUM:
22795 op0 = XEXP (addr, 0);
22796 op1 = XEXP (addr, 1);
22797 if (!base_reg_operand (op0, Pmode))
22798 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22800 else if (int_reg_operand (op1, Pmode))
22802 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
22804 emit_insn (gen_rtx_SET (scratch, addr));
22805 new_addr = scratch;
22809 /* Quad offsets are restricted and can't handle normal addresses. */
22810 else if (mode_supports_vsx_dform_quad (mode))
22812 emit_insn (gen_rtx_SET (scratch, addr));
22813 new_addr = scratch;
22816 /* Make sure the register class can handle offset addresses. */
22817 else if (legitimate_lo_sum_address_p (mode, addr, false))
22819 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
22821 emit_insn (gen_rtx_SET (scratch, addr));
22822 new_addr = scratch;
22826 else
22827 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22829 break;
22831 case SYMBOL_REF:
22832 case CONST:
22833 case LABEL_REF:
22834 rs6000_emit_move (scratch, addr, Pmode);
22835 new_addr = scratch;
22836 break;
22838 default:
22839 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
22842 /* Adjust the address if it changed. */
22843 if (addr != new_addr)
22845 mem = replace_equiv_address_nv (mem, new_addr);
22846 if (TARGET_DEBUG_ADDR)
22847 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
22850 /* Now create the move. */
22851 if (store_p)
22852 emit_insn (gen_rtx_SET (mem, reg));
22853 else
22854 emit_insn (gen_rtx_SET (reg, mem));
22856 return;
22859 /* Convert reloads involving 64-bit gprs and misaligned offset
22860 addressing, or multiple 32-bit gprs and offsets that are too large,
22861 to use indirect addressing. */
22863 void
22864 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
22866 int regno = true_regnum (reg);
22867 enum reg_class rclass;
22868 rtx addr;
22869 rtx scratch_or_premodify = scratch;
22871 if (TARGET_DEBUG_ADDR)
22873 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
22874 store_p ? "store" : "load");
22875 fprintf (stderr, "reg:\n");
22876 debug_rtx (reg);
22877 fprintf (stderr, "mem:\n");
22878 debug_rtx (mem);
22879 fprintf (stderr, "scratch:\n");
22880 debug_rtx (scratch);
22883 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
22884 gcc_assert (GET_CODE (mem) == MEM);
22885 rclass = REGNO_REG_CLASS (regno);
22886 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
22887 addr = XEXP (mem, 0);
22889 if (GET_CODE (addr) == PRE_MODIFY)
22891 gcc_assert (REG_P (XEXP (addr, 0))
22892 && GET_CODE (XEXP (addr, 1)) == PLUS
22893 && XEXP (XEXP (addr, 1), 0) == XEXP (addr, 0));
22894 scratch_or_premodify = XEXP (addr, 0);
22895 if (!HARD_REGISTER_P (scratch_or_premodify))
22896 /* If we have a pseudo here then reload will have arranged
22897 to have it replaced, but only in the original insn.
22898 Use the replacement here too. */
22899 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
22901 /* RTL emitted by rs6000_secondary_reload_gpr uses RTL
22902 expressions from the original insn, without unsharing them.
22903 Any RTL that points into the original insn will of course
22904 have register replacements applied. That is why we don't
22905 need to look for replacements under the PLUS. */
22906 addr = XEXP (addr, 1);
22908 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
22910 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
22912 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
22914 /* Now create the move. */
22915 if (store_p)
22916 emit_insn (gen_rtx_SET (mem, reg));
22917 else
22918 emit_insn (gen_rtx_SET (reg, mem));
22920 return;
22923 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
22924 this function has any SDmode references. If we are on a power7 or later, we
22925 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
22926 can load/store the value. */
22928 static void
22929 rs6000_alloc_sdmode_stack_slot (void)
22931 tree t;
22932 basic_block bb;
22933 gimple_stmt_iterator gsi;
22935 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
22936 /* We use a different approach for dealing with the secondary
22937 memory in LRA. */
22938 if (ira_use_lra_p)
22939 return;
22941 if (TARGET_NO_SDMODE_STACK)
22942 return;
22944 FOR_EACH_BB_FN (bb, cfun)
22945 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
22947 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
22948 if (ret)
22950 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
22951 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
22952 SDmode, 0);
22953 return;
22957 /* Check for any SDmode parameters of the function. */
22958 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
22960 if (TREE_TYPE (t) == error_mark_node)
22961 continue;
22963 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
22964 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
22966 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
22967 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
22968 SDmode, 0);
22969 return;
22974 static void
22975 rs6000_instantiate_decls (void)
22977 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
22978 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
22981 /* Given an rtx X being reloaded into a reg required to be
22982 in class CLASS, return the class of reg to actually use.
22983 In general this is just CLASS; but on some machines
22984 in some cases it is preferable to use a more restrictive class.
22986 On the RS/6000, we have to return NO_REGS when we want to reload a
22987 floating-point CONST_DOUBLE to force it to be copied to memory.
22989 We also don't want to reload integer values into floating-point
22990 registers if we can at all help it. In fact, this can
22991 cause reload to die, if it tries to generate a reload of CTR
22992 into a FP register and discovers it doesn't have the memory location
22993 required.
22995 ??? Would it be a good idea to have reload do the converse, that is
22996 try to reload floating modes into FP registers if possible?
22999 static enum reg_class
23000 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
23002 machine_mode mode = GET_MODE (x);
23003 bool is_constant = CONSTANT_P (x);
23005 /* If a mode can't go in FPR/ALTIVEC/VSX registers, don't return a preferred
23006 reload class for it. */
23007 if ((rclass == ALTIVEC_REGS || rclass == VSX_REGS)
23008 && (reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID) == 0)
23009 return NO_REGS;
23011 if ((rclass == FLOAT_REGS || rclass == VSX_REGS)
23012 && (reg_addr[mode].addr_mask[RELOAD_REG_FPR] & RELOAD_REG_VALID) == 0)
23013 return NO_REGS;
23015 /* For VSX, see if we should prefer FLOAT_REGS or ALTIVEC_REGS. Do not allow
23016 the reloading of address expressions using PLUS into floating point
23017 registers. */
23018 if (TARGET_VSX && VSX_REG_CLASS_P (rclass) && GET_CODE (x) != PLUS)
23020 if (is_constant)
23022 /* Zero is always allowed in all VSX registers. */
23023 if (x == CONST0_RTX (mode))
23024 return rclass;
23026 /* If this is a vector constant that can be formed with a few Altivec
23027 instructions, we want altivec registers. */
23028 if (GET_CODE (x) == CONST_VECTOR && easy_vector_constant (x, mode))
23029 return ALTIVEC_REGS;
23031 /* If this is an integer constant that can easily be loaded into
23032 vector registers, allow it. */
23033 if (CONST_INT_P (x))
23035 HOST_WIDE_INT value = INTVAL (x);
23037 /* ISA 2.07 can generate -1 in all registers with XXLORC. ISA
23038 2.06 can generate it in the Altivec registers with
23039 VSPLTI<x>. */
23040 if (value == -1)
23042 if (TARGET_P8_VECTOR)
23043 return rclass;
23044 else if (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
23045 return ALTIVEC_REGS;
23046 else
23047 return NO_REGS;
23050 /* ISA 3.0 can load -128..127 using the XXSPLTIB instruction and
23051 a sign extend in the Altivec registers. */
23052 if (IN_RANGE (value, -128, 127) && TARGET_P9_VECTOR
23053 && TARGET_VSX_SMALL_INTEGER
23054 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS))
23055 return ALTIVEC_REGS;
23058 /* Force constant to memory. */
23059 return NO_REGS;
23062 /* D-form addressing can easily reload the value. */
23063 if (mode_supports_vmx_dform (mode)
23064 || mode_supports_vsx_dform_quad (mode))
23065 return rclass;
23067 /* If this is a scalar floating point value and we don't have D-form
23068 addressing, prefer the traditional floating point registers so that we
23069 can use D-form (register+offset) addressing. */
23070 if (rclass == VSX_REGS
23071 && (mode == SFmode || GET_MODE_SIZE (mode) == 8))
23072 return FLOAT_REGS;
23074 /* Prefer the Altivec registers if Altivec is handling the vector
23075 operations (i.e. V16QI, V8HI, and V4SI), or if we prefer Altivec
23076 loads. */
23077 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
23078 || mode == V1TImode)
23079 return ALTIVEC_REGS;
23081 return rclass;
23084 if (is_constant || GET_CODE (x) == PLUS)
23086 if (reg_class_subset_p (GENERAL_REGS, rclass))
23087 return GENERAL_REGS;
23088 if (reg_class_subset_p (BASE_REGS, rclass))
23089 return BASE_REGS;
23090 return NO_REGS;
23093 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
23094 return GENERAL_REGS;
23096 return rclass;
23099 /* Debug version of rs6000_preferred_reload_class. */
23100 static enum reg_class
23101 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
23103 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
23105 fprintf (stderr,
23106 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
23107 "mode = %s, x:\n",
23108 reg_class_names[ret], reg_class_names[rclass],
23109 GET_MODE_NAME (GET_MODE (x)));
23110 debug_rtx (x);
23112 return ret;
23115 /* If we are copying between FP or AltiVec registers and anything else, we need
23116 a memory location. The exception is when we are targeting ppc64 and the
23117 move to/from fpr to gpr instructions are available. Also, under VSX, you
23118 can copy vector registers from the FP register set to the Altivec register
23119 set and vice versa. */
23121 static bool
23122 rs6000_secondary_memory_needed (enum reg_class from_class,
23123 enum reg_class to_class,
23124 machine_mode mode)
23126 enum rs6000_reg_type from_type, to_type;
23127 bool altivec_p = ((from_class == ALTIVEC_REGS)
23128 || (to_class == ALTIVEC_REGS));
23130 /* If a simple/direct move is available, we don't need secondary memory */
23131 from_type = reg_class_to_reg_type[(int)from_class];
23132 to_type = reg_class_to_reg_type[(int)to_class];
23134 if (rs6000_secondary_reload_move (to_type, from_type, mode,
23135 (secondary_reload_info *)0, altivec_p))
23136 return false;
23138 /* If we have a floating point or vector register class, we need to use
23139 memory to transfer the data. */
23140 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
23141 return true;
23143 return false;
23146 /* Debug version of rs6000_secondary_memory_needed. */
23147 static bool
23148 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
23149 enum reg_class to_class,
23150 machine_mode mode)
23152 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
23154 fprintf (stderr,
23155 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
23156 "to_class = %s, mode = %s\n",
23157 ret ? "true" : "false",
23158 reg_class_names[from_class],
23159 reg_class_names[to_class],
23160 GET_MODE_NAME (mode));
23162 return ret;
23165 /* Return the register class of a scratch register needed to copy IN into
23166 or out of a register in RCLASS in MODE. If it can be done directly,
23167 NO_REGS is returned. */
23169 static enum reg_class
23170 rs6000_secondary_reload_class (enum reg_class rclass, machine_mode mode,
23171 rtx in)
23173 int regno;
23175 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
23176 #if TARGET_MACHO
23177 && MACHOPIC_INDIRECT
23178 #endif
23181 /* We cannot copy a symbolic operand directly into anything
23182 other than BASE_REGS for TARGET_ELF. So indicate that a
23183 register from BASE_REGS is needed as an intermediate
23184 register.
23186 On Darwin, pic addresses require a load from memory, which
23187 needs a base register. */
23188 if (rclass != BASE_REGS
23189 && (GET_CODE (in) == SYMBOL_REF
23190 || GET_CODE (in) == HIGH
23191 || GET_CODE (in) == LABEL_REF
23192 || GET_CODE (in) == CONST))
23193 return BASE_REGS;
23196 if (GET_CODE (in) == REG)
23198 regno = REGNO (in);
23199 if (regno >= FIRST_PSEUDO_REGISTER)
23201 regno = true_regnum (in);
23202 if (regno >= FIRST_PSEUDO_REGISTER)
23203 regno = -1;
23206 else if (GET_CODE (in) == SUBREG)
23208 regno = true_regnum (in);
23209 if (regno >= FIRST_PSEUDO_REGISTER)
23210 regno = -1;
23212 else
23213 regno = -1;
23215 /* If we have VSX register moves, prefer moving scalar values between
23216 Altivec registers and GPR by going via an FPR (and then via memory)
23217 instead of reloading the secondary memory address for Altivec moves. */
23218 if (TARGET_VSX
23219 && GET_MODE_SIZE (mode) < 16
23220 && !mode_supports_vmx_dform (mode)
23221 && (((rclass == GENERAL_REGS || rclass == BASE_REGS)
23222 && (regno >= 0 && ALTIVEC_REGNO_P (regno)))
23223 || ((rclass == VSX_REGS || rclass == ALTIVEC_REGS)
23224 && (regno >= 0 && INT_REGNO_P (regno)))))
23225 return FLOAT_REGS;
23227 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
23228 into anything. */
23229 if (rclass == GENERAL_REGS || rclass == BASE_REGS
23230 || (regno >= 0 && INT_REGNO_P (regno)))
23231 return NO_REGS;
23233 /* Constants, memory, and VSX registers can go into VSX registers (both the
23234 traditional floating point and the altivec registers). */
23235 if (rclass == VSX_REGS
23236 && (regno == -1 || VSX_REGNO_P (regno)))
23237 return NO_REGS;
23239 /* Constants, memory, and FP registers can go into FP registers. */
23240 if ((regno == -1 || FP_REGNO_P (regno))
23241 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
23242 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
23244 /* Memory, and AltiVec registers can go into AltiVec registers. */
23245 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
23246 && rclass == ALTIVEC_REGS)
23247 return NO_REGS;
23249 /* We can copy among the CR registers. */
23250 if ((rclass == CR_REGS || rclass == CR0_REGS)
23251 && regno >= 0 && CR_REGNO_P (regno))
23252 return NO_REGS;
23254 /* Otherwise, we need GENERAL_REGS. */
23255 return GENERAL_REGS;
23258 /* Debug version of rs6000_secondary_reload_class. */
23259 static enum reg_class
23260 rs6000_debug_secondary_reload_class (enum reg_class rclass,
23261 machine_mode mode, rtx in)
23263 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
23264 fprintf (stderr,
23265 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
23266 "mode = %s, input rtx:\n",
23267 reg_class_names[ret], reg_class_names[rclass],
23268 GET_MODE_NAME (mode));
23269 debug_rtx (in);
23271 return ret;
23274 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
23276 static bool
23277 rs6000_cannot_change_mode_class (machine_mode from,
23278 machine_mode to,
23279 enum reg_class rclass)
23281 unsigned from_size = GET_MODE_SIZE (from);
23282 unsigned to_size = GET_MODE_SIZE (to);
23284 if (from_size != to_size)
23286 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
23288 if (reg_classes_intersect_p (xclass, rclass))
23290 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
23291 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
23292 bool to_float128_vector_p = FLOAT128_VECTOR_P (to);
23293 bool from_float128_vector_p = FLOAT128_VECTOR_P (from);
23295 /* Don't allow 64-bit types to overlap with 128-bit types that take a
23296 single register under VSX because the scalar part of the register
23297 is in the upper 64-bits, and not the lower 64-bits. Types like
23298 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
23299 IEEE floating point can't overlap, and neither can small
23300 values. */
23302 if (to_float128_vector_p && from_float128_vector_p)
23303 return false;
23305 else if (to_float128_vector_p || from_float128_vector_p)
23306 return true;
23308 /* TDmode in floating-mode registers must always go into a register
23309 pair with the most significant word in the even-numbered register
23310 to match ISA requirements. In little-endian mode, this does not
23311 match subreg numbering, so we cannot allow subregs. */
23312 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
23313 return true;
23315 if (from_size < 8 || to_size < 8)
23316 return true;
23318 if (from_size == 8 && (8 * to_nregs) != to_size)
23319 return true;
23321 if (to_size == 8 && (8 * from_nregs) != from_size)
23322 return true;
23324 return false;
23326 else
23327 return false;
23330 if (TARGET_E500_DOUBLE
23331 && ((((to) == DFmode) + ((from) == DFmode)) == 1
23332 || (((to) == TFmode) + ((from) == TFmode)) == 1
23333 || (((to) == IFmode) + ((from) == IFmode)) == 1
23334 || (((to) == KFmode) + ((from) == KFmode)) == 1
23335 || (((to) == DDmode) + ((from) == DDmode)) == 1
23336 || (((to) == TDmode) + ((from) == TDmode)) == 1
23337 || (((to) == DImode) + ((from) == DImode)) == 1))
23338 return true;
23340 /* Since the VSX register set includes traditional floating point registers
23341 and altivec registers, just check for the size being different instead of
23342 trying to check whether the modes are vector modes. Otherwise it won't
23343 allow say DF and DI to change classes. For types like TFmode and TDmode
23344 that take 2 64-bit registers, rather than a single 128-bit register, don't
23345 allow subregs of those types to other 128 bit types. */
23346 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
23348 unsigned num_regs = (from_size + 15) / 16;
23349 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
23350 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
23351 return true;
23353 return (from_size != 8 && from_size != 16);
23356 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
23357 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
23358 return true;
23360 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
23361 && reg_classes_intersect_p (GENERAL_REGS, rclass))
23362 return true;
23364 return false;
23367 /* Debug version of rs6000_cannot_change_mode_class. */
23368 static bool
23369 rs6000_debug_cannot_change_mode_class (machine_mode from,
23370 machine_mode to,
23371 enum reg_class rclass)
23373 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
23375 fprintf (stderr,
23376 "rs6000_cannot_change_mode_class, return %s, from = %s, "
23377 "to = %s, rclass = %s\n",
23378 ret ? "true" : "false",
23379 GET_MODE_NAME (from), GET_MODE_NAME (to),
23380 reg_class_names[rclass]);
23382 return ret;
23385 /* Return a string to do a move operation of 128 bits of data. */
23387 const char *
23388 rs6000_output_move_128bit (rtx operands[])
23390 rtx dest = operands[0];
23391 rtx src = operands[1];
23392 machine_mode mode = GET_MODE (dest);
23393 int dest_regno;
23394 int src_regno;
23395 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
23396 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
23398 if (REG_P (dest))
23400 dest_regno = REGNO (dest);
23401 dest_gpr_p = INT_REGNO_P (dest_regno);
23402 dest_fp_p = FP_REGNO_P (dest_regno);
23403 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
23404 dest_vsx_p = dest_fp_p | dest_vmx_p;
23406 else
23408 dest_regno = -1;
23409 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
23412 if (REG_P (src))
23414 src_regno = REGNO (src);
23415 src_gpr_p = INT_REGNO_P (src_regno);
23416 src_fp_p = FP_REGNO_P (src_regno);
23417 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
23418 src_vsx_p = src_fp_p | src_vmx_p;
23420 else
23422 src_regno = -1;
23423 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
23426 /* Register moves. */
23427 if (dest_regno >= 0 && src_regno >= 0)
23429 if (dest_gpr_p)
23431 if (src_gpr_p)
23432 return "#";
23434 if (TARGET_DIRECT_MOVE_128 && src_vsx_p)
23435 return (WORDS_BIG_ENDIAN
23436 ? "mfvsrd %0,%x1\n\tmfvsrld %L0,%x1"
23437 : "mfvsrd %L0,%x1\n\tmfvsrld %0,%x1");
23439 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
23440 return "#";
23443 else if (TARGET_VSX && dest_vsx_p)
23445 if (src_vsx_p)
23446 return "xxlor %x0,%x1,%x1";
23448 else if (TARGET_DIRECT_MOVE_128 && src_gpr_p)
23449 return (WORDS_BIG_ENDIAN
23450 ? "mtvsrdd %x0,%1,%L1"
23451 : "mtvsrdd %x0,%L1,%1");
23453 else if (TARGET_DIRECT_MOVE && src_gpr_p)
23454 return "#";
23457 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
23458 return "vor %0,%1,%1";
23460 else if (dest_fp_p && src_fp_p)
23461 return "#";
23464 /* Loads. */
23465 else if (dest_regno >= 0 && MEM_P (src))
23467 if (dest_gpr_p)
23469 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
23470 return "lq %0,%1";
23471 else
23472 return "#";
23475 else if (TARGET_ALTIVEC && dest_vmx_p
23476 && altivec_indexed_or_indirect_operand (src, mode))
23477 return "lvx %0,%y1";
23479 else if (TARGET_VSX && dest_vsx_p)
23481 if (mode_supports_vsx_dform_quad (mode)
23482 && quad_address_p (XEXP (src, 0), mode, true))
23483 return "lxv %x0,%1";
23485 else if (TARGET_P9_VECTOR)
23486 return "lxvx %x0,%y1";
23488 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
23489 return "lxvw4x %x0,%y1";
23491 else
23492 return "lxvd2x %x0,%y1";
23495 else if (TARGET_ALTIVEC && dest_vmx_p)
23496 return "lvx %0,%y1";
23498 else if (dest_fp_p)
23499 return "#";
23502 /* Stores. */
23503 else if (src_regno >= 0 && MEM_P (dest))
23505 if (src_gpr_p)
23507 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
23508 return "stq %1,%0";
23509 else
23510 return "#";
23513 else if (TARGET_ALTIVEC && src_vmx_p
23514 && altivec_indexed_or_indirect_operand (src, mode))
23515 return "stvx %1,%y0";
23517 else if (TARGET_VSX && src_vsx_p)
23519 if (mode_supports_vsx_dform_quad (mode)
23520 && quad_address_p (XEXP (dest, 0), mode, true))
23521 return "stxv %x1,%0";
23523 else if (TARGET_P9_VECTOR)
23524 return "stxvx %x1,%y0";
23526 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
23527 return "stxvw4x %x1,%y0";
23529 else
23530 return "stxvd2x %x1,%y0";
23533 else if (TARGET_ALTIVEC && src_vmx_p)
23534 return "stvx %1,%y0";
23536 else if (src_fp_p)
23537 return "#";
23540 /* Constants. */
23541 else if (dest_regno >= 0
23542 && (GET_CODE (src) == CONST_INT
23543 || GET_CODE (src) == CONST_WIDE_INT
23544 || GET_CODE (src) == CONST_DOUBLE
23545 || GET_CODE (src) == CONST_VECTOR))
23547 if (dest_gpr_p)
23548 return "#";
23550 else if ((dest_vmx_p && TARGET_ALTIVEC)
23551 || (dest_vsx_p && TARGET_VSX))
23552 return output_vec_const_move (operands);
23555 fatal_insn ("Bad 128-bit move", gen_rtx_SET (dest, src));
23558 /* Validate a 128-bit move. */
23559 bool
23560 rs6000_move_128bit_ok_p (rtx operands[])
23562 machine_mode mode = GET_MODE (operands[0]);
23563 return (gpc_reg_operand (operands[0], mode)
23564 || gpc_reg_operand (operands[1], mode));
23567 /* Return true if a 128-bit move needs to be split. */
23568 bool
23569 rs6000_split_128bit_ok_p (rtx operands[])
23571 if (!reload_completed)
23572 return false;
23574 if (!gpr_or_gpr_p (operands[0], operands[1]))
23575 return false;
23577 if (quad_load_store_p (operands[0], operands[1]))
23578 return false;
23580 return true;
23584 /* Given a comparison operation, return the bit number in CCR to test. We
23585 know this is a valid comparison.
23587 SCC_P is 1 if this is for an scc. That means that %D will have been
23588 used instead of %C, so the bits will be in different places.
23590 Return -1 if OP isn't a valid comparison for some reason. */
23593 ccr_bit (rtx op, int scc_p)
23595 enum rtx_code code = GET_CODE (op);
23596 machine_mode cc_mode;
23597 int cc_regnum;
23598 int base_bit;
23599 rtx reg;
23601 if (!COMPARISON_P (op))
23602 return -1;
23604 reg = XEXP (op, 0);
23606 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
23608 cc_mode = GET_MODE (reg);
23609 cc_regnum = REGNO (reg);
23610 base_bit = 4 * (cc_regnum - CR0_REGNO);
23612 validate_condition_mode (code, cc_mode);
23614 /* When generating a sCOND operation, only positive conditions are
23615 allowed. */
23616 gcc_assert (!scc_p
23617 || code == EQ || code == GT || code == LT || code == UNORDERED
23618 || code == GTU || code == LTU);
23620 switch (code)
23622 case NE:
23623 return scc_p ? base_bit + 3 : base_bit + 2;
23624 case EQ:
23625 return base_bit + 2;
23626 case GT: case GTU: case UNLE:
23627 return base_bit + 1;
23628 case LT: case LTU: case UNGE:
23629 return base_bit;
23630 case ORDERED: case UNORDERED:
23631 return base_bit + 3;
23633 case GE: case GEU:
23634 /* If scc, we will have done a cror to put the bit in the
23635 unordered position. So test that bit. For integer, this is ! LT
23636 unless this is an scc insn. */
23637 return scc_p ? base_bit + 3 : base_bit;
23639 case LE: case LEU:
23640 return scc_p ? base_bit + 3 : base_bit + 1;
23642 default:
23643 gcc_unreachable ();
23647 /* Return the GOT register. */
23650 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
23652 /* The second flow pass currently (June 1999) can't update
23653 regs_ever_live without disturbing other parts of the compiler, so
23654 update it here to make the prolog/epilogue code happy. */
23655 if (!can_create_pseudo_p ()
23656 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
23657 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
23659 crtl->uses_pic_offset_table = 1;
23661 return pic_offset_table_rtx;
23664 static rs6000_stack_t stack_info;
23666 /* Function to init struct machine_function.
23667 This will be called, via a pointer variable,
23668 from push_function_context. */
23670 static struct machine_function *
23671 rs6000_init_machine_status (void)
23673 stack_info.reload_completed = 0;
23674 return ggc_cleared_alloc<machine_function> ();
23677 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
23679 /* Write out a function code label. */
23681 void
23682 rs6000_output_function_entry (FILE *file, const char *fname)
23684 if (fname[0] != '.')
23686 switch (DEFAULT_ABI)
23688 default:
23689 gcc_unreachable ();
23691 case ABI_AIX:
23692 if (DOT_SYMBOLS)
23693 putc ('.', file);
23694 else
23695 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
23696 break;
23698 case ABI_ELFv2:
23699 case ABI_V4:
23700 case ABI_DARWIN:
23701 break;
23705 RS6000_OUTPUT_BASENAME (file, fname);
23708 /* Print an operand. Recognize special options, documented below. */
23710 #if TARGET_ELF
23711 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
23712 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
23713 #else
23714 #define SMALL_DATA_RELOC "sda21"
23715 #define SMALL_DATA_REG 0
23716 #endif
23718 void
23719 print_operand (FILE *file, rtx x, int code)
23721 int i;
23722 unsigned HOST_WIDE_INT uval;
23724 switch (code)
23726 /* %a is output_address. */
23728 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
23729 output_operand. */
23731 case 'D':
23732 /* Like 'J' but get to the GT bit only. */
23733 gcc_assert (REG_P (x));
23735 /* Bit 1 is GT bit. */
23736 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
23738 /* Add one for shift count in rlinm for scc. */
23739 fprintf (file, "%d", i + 1);
23740 return;
23742 case 'e':
23743 /* If the low 16 bits are 0, but some other bit is set, write 's'. */
23744 if (! INT_P (x))
23746 output_operand_lossage ("invalid %%e value");
23747 return;
23750 uval = INTVAL (x);
23751 if ((uval & 0xffff) == 0 && uval != 0)
23752 putc ('s', file);
23753 return;
23755 case 'E':
23756 /* X is a CR register. Print the number of the EQ bit of the CR */
23757 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23758 output_operand_lossage ("invalid %%E value");
23759 else
23760 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
23761 return;
23763 case 'f':
23764 /* X is a CR register. Print the shift count needed to move it
23765 to the high-order four bits. */
23766 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23767 output_operand_lossage ("invalid %%f value");
23768 else
23769 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
23770 return;
23772 case 'F':
23773 /* Similar, but print the count for the rotate in the opposite
23774 direction. */
23775 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23776 output_operand_lossage ("invalid %%F value");
23777 else
23778 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
23779 return;
23781 case 'G':
23782 /* X is a constant integer. If it is negative, print "m",
23783 otherwise print "z". This is to make an aze or ame insn. */
23784 if (GET_CODE (x) != CONST_INT)
23785 output_operand_lossage ("invalid %%G value");
23786 else if (INTVAL (x) >= 0)
23787 putc ('z', file);
23788 else
23789 putc ('m', file);
23790 return;
23792 case 'h':
23793 /* If constant, output low-order five bits. Otherwise, write
23794 normally. */
23795 if (INT_P (x))
23796 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
23797 else
23798 print_operand (file, x, 0);
23799 return;
23801 case 'H':
23802 /* If constant, output low-order six bits. Otherwise, write
23803 normally. */
23804 if (INT_P (x))
23805 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
23806 else
23807 print_operand (file, x, 0);
23808 return;
23810 case 'I':
23811 /* Print `i' if this is a constant, else nothing. */
23812 if (INT_P (x))
23813 putc ('i', file);
23814 return;
23816 case 'j':
23817 /* Write the bit number in CCR for jump. */
23818 i = ccr_bit (x, 0);
23819 if (i == -1)
23820 output_operand_lossage ("invalid %%j code");
23821 else
23822 fprintf (file, "%d", i);
23823 return;
23825 case 'J':
23826 /* Similar, but add one for shift count in rlinm for scc and pass
23827 scc flag to `ccr_bit'. */
23828 i = ccr_bit (x, 1);
23829 if (i == -1)
23830 output_operand_lossage ("invalid %%J code");
23831 else
23832 /* If we want bit 31, write a shift count of zero, not 32. */
23833 fprintf (file, "%d", i == 31 ? 0 : i + 1);
23834 return;
23836 case 'k':
23837 /* X must be a constant. Write the 1's complement of the
23838 constant. */
23839 if (! INT_P (x))
23840 output_operand_lossage ("invalid %%k value");
23841 else
23842 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
23843 return;
23845 case 'K':
23846 /* X must be a symbolic constant on ELF. Write an
23847 expression suitable for an 'addi' that adds in the low 16
23848 bits of the MEM. */
23849 if (GET_CODE (x) == CONST)
23851 if (GET_CODE (XEXP (x, 0)) != PLUS
23852 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
23853 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
23854 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
23855 output_operand_lossage ("invalid %%K value");
23857 print_operand_address (file, x);
23858 fputs ("@l", file);
23859 return;
23861 /* %l is output_asm_label. */
23863 case 'L':
23864 /* Write second word of DImode or DFmode reference. Works on register
23865 or non-indexed memory only. */
23866 if (REG_P (x))
23867 fputs (reg_names[REGNO (x) + 1], file);
23868 else if (MEM_P (x))
23870 machine_mode mode = GET_MODE (x);
23871 /* Handle possible auto-increment. Since it is pre-increment and
23872 we have already done it, we can just use an offset of word. */
23873 if (GET_CODE (XEXP (x, 0)) == PRE_INC
23874 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
23875 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
23876 UNITS_PER_WORD));
23877 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
23878 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
23879 UNITS_PER_WORD));
23880 else
23881 output_address (mode, XEXP (adjust_address_nv (x, SImode,
23882 UNITS_PER_WORD),
23883 0));
23885 if (small_data_operand (x, GET_MODE (x)))
23886 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
23887 reg_names[SMALL_DATA_REG]);
23889 return;
23891 case 'N':
23892 /* Write the number of elements in the vector times 4. */
23893 if (GET_CODE (x) != PARALLEL)
23894 output_operand_lossage ("invalid %%N value");
23895 else
23896 fprintf (file, "%d", XVECLEN (x, 0) * 4);
23897 return;
23899 case 'O':
23900 /* Similar, but subtract 1 first. */
23901 if (GET_CODE (x) != PARALLEL)
23902 output_operand_lossage ("invalid %%O value");
23903 else
23904 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
23905 return;
23907 case 'p':
23908 /* X is a CONST_INT that is a power of two. Output the logarithm. */
23909 if (! INT_P (x)
23910 || INTVAL (x) < 0
23911 || (i = exact_log2 (INTVAL (x))) < 0)
23912 output_operand_lossage ("invalid %%p value");
23913 else
23914 fprintf (file, "%d", i);
23915 return;
23917 case 'P':
23918 /* The operand must be an indirect memory reference. The result
23919 is the register name. */
23920 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
23921 || REGNO (XEXP (x, 0)) >= 32)
23922 output_operand_lossage ("invalid %%P value");
23923 else
23924 fputs (reg_names[REGNO (XEXP (x, 0))], file);
23925 return;
23927 case 'q':
23928 /* This outputs the logical code corresponding to a boolean
23929 expression. The expression may have one or both operands
23930 negated (if one, only the first one). For condition register
23931 logical operations, it will also treat the negated
23932 CR codes as NOTs, but not handle NOTs of them. */
23934 const char *const *t = 0;
23935 const char *s;
23936 enum rtx_code code = GET_CODE (x);
23937 static const char * const tbl[3][3] = {
23938 { "and", "andc", "nor" },
23939 { "or", "orc", "nand" },
23940 { "xor", "eqv", "xor" } };
23942 if (code == AND)
23943 t = tbl[0];
23944 else if (code == IOR)
23945 t = tbl[1];
23946 else if (code == XOR)
23947 t = tbl[2];
23948 else
23949 output_operand_lossage ("invalid %%q value");
23951 if (GET_CODE (XEXP (x, 0)) != NOT)
23952 s = t[0];
23953 else
23955 if (GET_CODE (XEXP (x, 1)) == NOT)
23956 s = t[2];
23957 else
23958 s = t[1];
23961 fputs (s, file);
23963 return;
23965 case 'Q':
23966 if (! TARGET_MFCRF)
23967 return;
23968 fputc (',', file);
23969 /* FALLTHRU */
23971 case 'R':
23972 /* X is a CR register. Print the mask for `mtcrf'. */
23973 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
23974 output_operand_lossage ("invalid %%R value");
23975 else
23976 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
23977 return;
23979 case 's':
23980 /* Low 5 bits of 32 - value */
23981 if (! INT_P (x))
23982 output_operand_lossage ("invalid %%s value");
23983 else
23984 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
23985 return;
23987 case 't':
23988 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
23989 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
23991 /* Bit 3 is OV bit. */
23992 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
23994 /* If we want bit 31, write a shift count of zero, not 32. */
23995 fprintf (file, "%d", i == 31 ? 0 : i + 1);
23996 return;
23998 case 'T':
23999 /* Print the symbolic name of a branch target register. */
24000 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
24001 && REGNO (x) != CTR_REGNO))
24002 output_operand_lossage ("invalid %%T value");
24003 else if (REGNO (x) == LR_REGNO)
24004 fputs ("lr", file);
24005 else
24006 fputs ("ctr", file);
24007 return;
24009 case 'u':
24010 /* High-order or low-order 16 bits of constant, whichever is non-zero,
24011 for use in unsigned operand. */
24012 if (! INT_P (x))
24014 output_operand_lossage ("invalid %%u value");
24015 return;
24018 uval = INTVAL (x);
24019 if ((uval & 0xffff) == 0)
24020 uval >>= 16;
24022 fprintf (file, HOST_WIDE_INT_PRINT_HEX, uval & 0xffff);
24023 return;
24025 case 'v':
24026 /* High-order 16 bits of constant for use in signed operand. */
24027 if (! INT_P (x))
24028 output_operand_lossage ("invalid %%v value");
24029 else
24030 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
24031 (INTVAL (x) >> 16) & 0xffff);
24032 return;
24034 case 'U':
24035 /* Print `u' if this has an auto-increment or auto-decrement. */
24036 if (MEM_P (x)
24037 && (GET_CODE (XEXP (x, 0)) == PRE_INC
24038 || GET_CODE (XEXP (x, 0)) == PRE_DEC
24039 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
24040 putc ('u', file);
24041 return;
24043 case 'V':
24044 /* Print the trap code for this operand. */
24045 switch (GET_CODE (x))
24047 case EQ:
24048 fputs ("eq", file); /* 4 */
24049 break;
24050 case NE:
24051 fputs ("ne", file); /* 24 */
24052 break;
24053 case LT:
24054 fputs ("lt", file); /* 16 */
24055 break;
24056 case LE:
24057 fputs ("le", file); /* 20 */
24058 break;
24059 case GT:
24060 fputs ("gt", file); /* 8 */
24061 break;
24062 case GE:
24063 fputs ("ge", file); /* 12 */
24064 break;
24065 case LTU:
24066 fputs ("llt", file); /* 2 */
24067 break;
24068 case LEU:
24069 fputs ("lle", file); /* 6 */
24070 break;
24071 case GTU:
24072 fputs ("lgt", file); /* 1 */
24073 break;
24074 case GEU:
24075 fputs ("lge", file); /* 5 */
24076 break;
24077 default:
24078 gcc_unreachable ();
24080 break;
24082 case 'w':
24083 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
24084 normally. */
24085 if (INT_P (x))
24086 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
24087 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
24088 else
24089 print_operand (file, x, 0);
24090 return;
24092 case 'x':
24093 /* X is a FPR or Altivec register used in a VSX context. */
24094 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
24095 output_operand_lossage ("invalid %%x value");
24096 else
24098 int reg = REGNO (x);
24099 int vsx_reg = (FP_REGNO_P (reg)
24100 ? reg - 32
24101 : reg - FIRST_ALTIVEC_REGNO + 32);
24103 #ifdef TARGET_REGNAMES
24104 if (TARGET_REGNAMES)
24105 fprintf (file, "%%vs%d", vsx_reg);
24106 else
24107 #endif
24108 fprintf (file, "%d", vsx_reg);
24110 return;
24112 case 'X':
24113 if (MEM_P (x)
24114 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
24115 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
24116 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
24117 putc ('x', file);
24118 return;
24120 case 'Y':
24121 /* Like 'L', for third word of TImode/PTImode */
24122 if (REG_P (x))
24123 fputs (reg_names[REGNO (x) + 2], file);
24124 else if (MEM_P (x))
24126 machine_mode mode = GET_MODE (x);
24127 if (GET_CODE (XEXP (x, 0)) == PRE_INC
24128 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
24129 output_address (mode, plus_constant (Pmode,
24130 XEXP (XEXP (x, 0), 0), 8));
24131 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
24132 output_address (mode, plus_constant (Pmode,
24133 XEXP (XEXP (x, 0), 0), 8));
24134 else
24135 output_address (mode, XEXP (adjust_address_nv (x, SImode, 8), 0));
24136 if (small_data_operand (x, GET_MODE (x)))
24137 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
24138 reg_names[SMALL_DATA_REG]);
24140 return;
24142 case 'z':
24143 /* X is a SYMBOL_REF. Write out the name preceded by a
24144 period and without any trailing data in brackets. Used for function
24145 names. If we are configured for System V (or the embedded ABI) on
24146 the PowerPC, do not emit the period, since those systems do not use
24147 TOCs and the like. */
24148 gcc_assert (GET_CODE (x) == SYMBOL_REF);
24150 /* For macho, check to see if we need a stub. */
24151 if (TARGET_MACHO)
24153 const char *name = XSTR (x, 0);
24154 #if TARGET_MACHO
24155 if (darwin_emit_branch_islands
24156 && MACHOPIC_INDIRECT
24157 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
24158 name = machopic_indirection_name (x, /*stub_p=*/true);
24159 #endif
24160 assemble_name (file, name);
24162 else if (!DOT_SYMBOLS)
24163 assemble_name (file, XSTR (x, 0));
24164 else
24165 rs6000_output_function_entry (file, XSTR (x, 0));
24166 return;
24168 case 'Z':
24169 /* Like 'L', for last word of TImode/PTImode. */
24170 if (REG_P (x))
24171 fputs (reg_names[REGNO (x) + 3], file);
24172 else if (MEM_P (x))
24174 machine_mode mode = GET_MODE (x);
24175 if (GET_CODE (XEXP (x, 0)) == PRE_INC
24176 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
24177 output_address (mode, plus_constant (Pmode,
24178 XEXP (XEXP (x, 0), 0), 12));
24179 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
24180 output_address (mode, plus_constant (Pmode,
24181 XEXP (XEXP (x, 0), 0), 12));
24182 else
24183 output_address (mode, XEXP (adjust_address_nv (x, SImode, 12), 0));
24184 if (small_data_operand (x, GET_MODE (x)))
24185 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
24186 reg_names[SMALL_DATA_REG]);
24188 return;
24190 /* Print AltiVec or SPE memory operand. */
24191 case 'y':
24193 rtx tmp;
24195 gcc_assert (MEM_P (x));
24197 tmp = XEXP (x, 0);
24199 /* Ugly hack because %y is overloaded. */
24200 if ((TARGET_SPE || TARGET_E500_DOUBLE)
24201 && (GET_MODE_SIZE (GET_MODE (x)) == 8
24202 || FLOAT128_2REG_P (GET_MODE (x))
24203 || GET_MODE (x) == TImode
24204 || GET_MODE (x) == PTImode))
24206 /* Handle [reg]. */
24207 if (REG_P (tmp))
24209 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
24210 break;
24212 /* Handle [reg+UIMM]. */
24213 else if (GET_CODE (tmp) == PLUS &&
24214 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
24216 int x;
24218 gcc_assert (REG_P (XEXP (tmp, 0)));
24220 x = INTVAL (XEXP (tmp, 1));
24221 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
24222 break;
24225 /* Fall through. Must be [reg+reg]. */
24227 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
24228 && GET_CODE (tmp) == AND
24229 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
24230 && INTVAL (XEXP (tmp, 1)) == -16)
24231 tmp = XEXP (tmp, 0);
24232 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
24233 && GET_CODE (tmp) == PRE_MODIFY)
24234 tmp = XEXP (tmp, 1);
24235 if (REG_P (tmp))
24236 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
24237 else
24239 if (GET_CODE (tmp) != PLUS
24240 || !REG_P (XEXP (tmp, 0))
24241 || !REG_P (XEXP (tmp, 1)))
24243 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
24244 break;
24247 if (REGNO (XEXP (tmp, 0)) == 0)
24248 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
24249 reg_names[ REGNO (XEXP (tmp, 0)) ]);
24250 else
24251 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
24252 reg_names[ REGNO (XEXP (tmp, 1)) ]);
24254 break;
24257 case 0:
24258 if (REG_P (x))
24259 fprintf (file, "%s", reg_names[REGNO (x)]);
24260 else if (MEM_P (x))
24262 /* We need to handle PRE_INC and PRE_DEC here, since we need to
24263 know the width from the mode. */
24264 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
24265 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
24266 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
24267 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
24268 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
24269 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
24270 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
24271 output_address (GET_MODE (x), XEXP (XEXP (x, 0), 1));
24272 else
24273 output_address (GET_MODE (x), XEXP (x, 0));
24275 else
24277 if (toc_relative_expr_p (x, false))
24278 /* This hack along with a corresponding hack in
24279 rs6000_output_addr_const_extra arranges to output addends
24280 where the assembler expects to find them. eg.
24281 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
24282 without this hack would be output as "x@toc+4". We
24283 want "x+4@toc". */
24284 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
24285 else
24286 output_addr_const (file, x);
24288 return;
24290 case '&':
24291 if (const char *name = get_some_local_dynamic_name ())
24292 assemble_name (file, name);
24293 else
24294 output_operand_lossage ("'%%&' used without any "
24295 "local dynamic TLS references");
24296 return;
24298 default:
24299 output_operand_lossage ("invalid %%xn code");
24303 /* Print the address of an operand. */
24305 void
24306 print_operand_address (FILE *file, rtx x)
24308 if (REG_P (x))
24309 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
24310 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
24311 || GET_CODE (x) == LABEL_REF)
24313 output_addr_const (file, x);
24314 if (small_data_operand (x, GET_MODE (x)))
24315 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
24316 reg_names[SMALL_DATA_REG]);
24317 else
24318 gcc_assert (!TARGET_TOC);
24320 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
24321 && REG_P (XEXP (x, 1)))
24323 if (REGNO (XEXP (x, 0)) == 0)
24324 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
24325 reg_names[ REGNO (XEXP (x, 0)) ]);
24326 else
24327 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
24328 reg_names[ REGNO (XEXP (x, 1)) ]);
24330 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
24331 && GET_CODE (XEXP (x, 1)) == CONST_INT)
24332 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
24333 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
24334 #if TARGET_MACHO
24335 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
24336 && CONSTANT_P (XEXP (x, 1)))
24338 fprintf (file, "lo16(");
24339 output_addr_const (file, XEXP (x, 1));
24340 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
24342 #endif
24343 #if TARGET_ELF
24344 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
24345 && CONSTANT_P (XEXP (x, 1)))
24347 output_addr_const (file, XEXP (x, 1));
24348 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
24350 #endif
24351 else if (toc_relative_expr_p (x, false))
24353 /* This hack along with a corresponding hack in
24354 rs6000_output_addr_const_extra arranges to output addends
24355 where the assembler expects to find them. eg.
24356 (lo_sum (reg 9)
24357 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
24358 without this hack would be output as "x@toc+8@l(9)". We
24359 want "x+8@toc@l(9)". */
24360 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
24361 if (GET_CODE (x) == LO_SUM)
24362 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
24363 else
24364 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
24366 else
24367 gcc_unreachable ();
24370 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
24372 static bool
24373 rs6000_output_addr_const_extra (FILE *file, rtx x)
24375 if (GET_CODE (x) == UNSPEC)
24376 switch (XINT (x, 1))
24378 case UNSPEC_TOCREL:
24379 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
24380 && REG_P (XVECEXP (x, 0, 1))
24381 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
24382 output_addr_const (file, XVECEXP (x, 0, 0));
24383 if (x == tocrel_base && tocrel_offset != const0_rtx)
24385 if (INTVAL (tocrel_offset) >= 0)
24386 fprintf (file, "+");
24387 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
24389 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
24391 putc ('-', file);
24392 assemble_name (file, toc_label_name);
24393 need_toc_init = 1;
24395 else if (TARGET_ELF)
24396 fputs ("@toc", file);
24397 return true;
24399 #if TARGET_MACHO
24400 case UNSPEC_MACHOPIC_OFFSET:
24401 output_addr_const (file, XVECEXP (x, 0, 0));
24402 putc ('-', file);
24403 machopic_output_function_base_name (file);
24404 return true;
24405 #endif
24407 return false;
24410 /* Target hook for assembling integer objects. The PowerPC version has
24411 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
24412 is defined. It also needs to handle DI-mode objects on 64-bit
24413 targets. */
24415 static bool
24416 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
24418 #ifdef RELOCATABLE_NEEDS_FIXUP
24419 /* Special handling for SI values. */
24420 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
24422 static int recurse = 0;
24424 /* For -mrelocatable, we mark all addresses that need to be fixed up in
24425 the .fixup section. Since the TOC section is already relocated, we
24426 don't need to mark it here. We used to skip the text section, but it
24427 should never be valid for relocated addresses to be placed in the text
24428 section. */
24429 if (DEFAULT_ABI == ABI_V4
24430 && (TARGET_RELOCATABLE || flag_pic > 1)
24431 && in_section != toc_section
24432 && !recurse
24433 && !CONST_SCALAR_INT_P (x)
24434 && CONSTANT_P (x))
24436 char buf[256];
24438 recurse = 1;
24439 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
24440 fixuplabelno++;
24441 ASM_OUTPUT_LABEL (asm_out_file, buf);
24442 fprintf (asm_out_file, "\t.long\t(");
24443 output_addr_const (asm_out_file, x);
24444 fprintf (asm_out_file, ")@fixup\n");
24445 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
24446 ASM_OUTPUT_ALIGN (asm_out_file, 2);
24447 fprintf (asm_out_file, "\t.long\t");
24448 assemble_name (asm_out_file, buf);
24449 fprintf (asm_out_file, "\n\t.previous\n");
24450 recurse = 0;
24451 return true;
24453 /* Remove initial .'s to turn a -mcall-aixdesc function
24454 address into the address of the descriptor, not the function
24455 itself. */
24456 else if (GET_CODE (x) == SYMBOL_REF
24457 && XSTR (x, 0)[0] == '.'
24458 && DEFAULT_ABI == ABI_AIX)
24460 const char *name = XSTR (x, 0);
24461 while (*name == '.')
24462 name++;
24464 fprintf (asm_out_file, "\t.long\t%s\n", name);
24465 return true;
24468 #endif /* RELOCATABLE_NEEDS_FIXUP */
24469 return default_assemble_integer (x, size, aligned_p);
24472 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
24473 /* Emit an assembler directive to set symbol visibility for DECL to
24474 VISIBILITY_TYPE. */
24476 static void
24477 rs6000_assemble_visibility (tree decl, int vis)
24479 if (TARGET_XCOFF)
24480 return;
24482 /* Functions need to have their entry point symbol visibility set as
24483 well as their descriptor symbol visibility. */
24484 if (DEFAULT_ABI == ABI_AIX
24485 && DOT_SYMBOLS
24486 && TREE_CODE (decl) == FUNCTION_DECL)
24488 static const char * const visibility_types[] = {
24489 NULL, "protected", "hidden", "internal"
24492 const char *name, *type;
24494 name = ((* targetm.strip_name_encoding)
24495 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
24496 type = visibility_types[vis];
24498 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
24499 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
24501 else
24502 default_assemble_visibility (decl, vis);
24504 #endif
24506 enum rtx_code
24507 rs6000_reverse_condition (machine_mode mode, enum rtx_code code)
24509 /* Reversal of FP compares takes care -- an ordered compare
24510 becomes an unordered compare and vice versa. */
24511 if (mode == CCFPmode
24512 && (!flag_finite_math_only
24513 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
24514 || code == UNEQ || code == LTGT))
24515 return reverse_condition_maybe_unordered (code);
24516 else
24517 return reverse_condition (code);
24520 /* Generate a compare for CODE. Return a brand-new rtx that
24521 represents the result of the compare. */
24523 static rtx
24524 rs6000_generate_compare (rtx cmp, machine_mode mode)
24526 machine_mode comp_mode;
24527 rtx compare_result;
24528 enum rtx_code code = GET_CODE (cmp);
24529 rtx op0 = XEXP (cmp, 0);
24530 rtx op1 = XEXP (cmp, 1);
24532 if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
24533 comp_mode = CCmode;
24534 else if (FLOAT_MODE_P (mode))
24535 comp_mode = CCFPmode;
24536 else if (code == GTU || code == LTU
24537 || code == GEU || code == LEU)
24538 comp_mode = CCUNSmode;
24539 else if ((code == EQ || code == NE)
24540 && unsigned_reg_p (op0)
24541 && (unsigned_reg_p (op1)
24542 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
24543 /* These are unsigned values, perhaps there will be a later
24544 ordering compare that can be shared with this one. */
24545 comp_mode = CCUNSmode;
24546 else
24547 comp_mode = CCmode;
24549 /* If we have an unsigned compare, make sure we don't have a signed value as
24550 an immediate. */
24551 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
24552 && INTVAL (op1) < 0)
24554 op0 = copy_rtx_if_shared (op0);
24555 op1 = force_reg (GET_MODE (op0), op1);
24556 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
24559 /* First, the compare. */
24560 compare_result = gen_reg_rtx (comp_mode);
24562 /* E500 FP compare instructions on the GPRs. Yuck! */
24563 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
24564 && FLOAT_MODE_P (mode))
24566 rtx cmp, or_result, compare_result2;
24567 machine_mode op_mode = GET_MODE (op0);
24568 bool reverse_p;
24570 if (op_mode == VOIDmode)
24571 op_mode = GET_MODE (op1);
24573 /* First reverse the condition codes that aren't directly supported. */
24574 switch (code)
24576 case NE:
24577 case UNLT:
24578 case UNLE:
24579 case UNGT:
24580 case UNGE:
24581 code = reverse_condition_maybe_unordered (code);
24582 reverse_p = true;
24583 break;
24585 case EQ:
24586 case LT:
24587 case LE:
24588 case GT:
24589 case GE:
24590 reverse_p = false;
24591 break;
24593 default:
24594 gcc_unreachable ();
24597 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
24598 This explains the following mess. */
24600 switch (code)
24602 case EQ:
24603 switch (op_mode)
24605 case E_SFmode:
24606 cmp = (flag_finite_math_only && !flag_trapping_math)
24607 ? gen_tstsfeq_gpr (compare_result, op0, op1)
24608 : gen_cmpsfeq_gpr (compare_result, op0, op1);
24609 break;
24611 case E_DFmode:
24612 cmp = (flag_finite_math_only && !flag_trapping_math)
24613 ? gen_tstdfeq_gpr (compare_result, op0, op1)
24614 : gen_cmpdfeq_gpr (compare_result, op0, op1);
24615 break;
24617 case E_TFmode:
24618 case E_IFmode:
24619 case E_KFmode:
24620 cmp = (flag_finite_math_only && !flag_trapping_math)
24621 ? gen_tsttfeq_gpr (compare_result, op0, op1)
24622 : gen_cmptfeq_gpr (compare_result, op0, op1);
24623 break;
24625 default:
24626 gcc_unreachable ();
24628 break;
24630 case GT:
24631 case GE:
24632 switch (op_mode)
24634 case E_SFmode:
24635 cmp = (flag_finite_math_only && !flag_trapping_math)
24636 ? gen_tstsfgt_gpr (compare_result, op0, op1)
24637 : gen_cmpsfgt_gpr (compare_result, op0, op1);
24638 break;
24640 case E_DFmode:
24641 cmp = (flag_finite_math_only && !flag_trapping_math)
24642 ? gen_tstdfgt_gpr (compare_result, op0, op1)
24643 : gen_cmpdfgt_gpr (compare_result, op0, op1);
24644 break;
24646 case E_TFmode:
24647 case E_IFmode:
24648 case E_KFmode:
24649 cmp = (flag_finite_math_only && !flag_trapping_math)
24650 ? gen_tsttfgt_gpr (compare_result, op0, op1)
24651 : gen_cmptfgt_gpr (compare_result, op0, op1);
24652 break;
24654 default:
24655 gcc_unreachable ();
24657 break;
24659 case LT:
24660 case LE:
24661 switch (op_mode)
24663 case E_SFmode:
24664 cmp = (flag_finite_math_only && !flag_trapping_math)
24665 ? gen_tstsflt_gpr (compare_result, op0, op1)
24666 : gen_cmpsflt_gpr (compare_result, op0, op1);
24667 break;
24669 case E_DFmode:
24670 cmp = (flag_finite_math_only && !flag_trapping_math)
24671 ? gen_tstdflt_gpr (compare_result, op0, op1)
24672 : gen_cmpdflt_gpr (compare_result, op0, op1);
24673 break;
24675 case E_TFmode:
24676 case E_IFmode:
24677 case E_KFmode:
24678 cmp = (flag_finite_math_only && !flag_trapping_math)
24679 ? gen_tsttflt_gpr (compare_result, op0, op1)
24680 : gen_cmptflt_gpr (compare_result, op0, op1);
24681 break;
24683 default:
24684 gcc_unreachable ();
24686 break;
24688 default:
24689 gcc_unreachable ();
24692 /* Synthesize LE and GE from LT/GT || EQ. */
24693 if (code == LE || code == GE)
24695 emit_insn (cmp);
24697 compare_result2 = gen_reg_rtx (CCFPmode);
24699 /* Do the EQ. */
24700 switch (op_mode)
24702 case E_SFmode:
24703 cmp = (flag_finite_math_only && !flag_trapping_math)
24704 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
24705 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
24706 break;
24708 case E_DFmode:
24709 cmp = (flag_finite_math_only && !flag_trapping_math)
24710 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
24711 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
24712 break;
24714 case E_TFmode:
24715 case E_IFmode:
24716 case E_KFmode:
24717 cmp = (flag_finite_math_only && !flag_trapping_math)
24718 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
24719 : gen_cmptfeq_gpr (compare_result2, op0, op1);
24720 break;
24722 default:
24723 gcc_unreachable ();
24726 emit_insn (cmp);
24728 /* OR them together. */
24729 or_result = gen_reg_rtx (CCFPmode);
24730 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
24731 compare_result2);
24732 compare_result = or_result;
24735 code = reverse_p ? NE : EQ;
24737 emit_insn (cmp);
24740 /* IEEE 128-bit support in VSX registers when we do not have hardware
24741 support. */
24742 else if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
24744 rtx libfunc = NULL_RTX;
24745 bool check_nan = false;
24746 rtx dest;
24748 switch (code)
24750 case EQ:
24751 case NE:
24752 libfunc = optab_libfunc (eq_optab, mode);
24753 break;
24755 case GT:
24756 case GE:
24757 libfunc = optab_libfunc (ge_optab, mode);
24758 break;
24760 case LT:
24761 case LE:
24762 libfunc = optab_libfunc (le_optab, mode);
24763 break;
24765 case UNORDERED:
24766 case ORDERED:
24767 libfunc = optab_libfunc (unord_optab, mode);
24768 code = (code == UNORDERED) ? NE : EQ;
24769 break;
24771 case UNGE:
24772 case UNGT:
24773 check_nan = true;
24774 libfunc = optab_libfunc (ge_optab, mode);
24775 code = (code == UNGE) ? GE : GT;
24776 break;
24778 case UNLE:
24779 case UNLT:
24780 check_nan = true;
24781 libfunc = optab_libfunc (le_optab, mode);
24782 code = (code == UNLE) ? LE : LT;
24783 break;
24785 case UNEQ:
24786 case LTGT:
24787 check_nan = true;
24788 libfunc = optab_libfunc (eq_optab, mode);
24789 code = (code = UNEQ) ? EQ : NE;
24790 break;
24792 default:
24793 gcc_unreachable ();
24796 gcc_assert (libfunc);
24798 if (!check_nan)
24799 dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
24800 SImode, op0, mode, op1, mode);
24802 /* The library signals an exception for signalling NaNs, so we need to
24803 handle isgreater, etc. by first checking isordered. */
24804 else
24806 rtx ne_rtx, normal_dest, unord_dest;
24807 rtx unord_func = optab_libfunc (unord_optab, mode);
24808 rtx join_label = gen_label_rtx ();
24809 rtx join_ref = gen_rtx_LABEL_REF (VOIDmode, join_label);
24810 rtx unord_cmp = gen_reg_rtx (comp_mode);
24813 /* Test for either value being a NaN. */
24814 gcc_assert (unord_func);
24815 unord_dest = emit_library_call_value (unord_func, NULL_RTX, LCT_CONST,
24816 SImode, op0, mode, op1, mode);
24818 /* Set value (0) if either value is a NaN, and jump to the join
24819 label. */
24820 dest = gen_reg_rtx (SImode);
24821 emit_move_insn (dest, const1_rtx);
24822 emit_insn (gen_rtx_SET (unord_cmp,
24823 gen_rtx_COMPARE (comp_mode, unord_dest,
24824 const0_rtx)));
24826 ne_rtx = gen_rtx_NE (comp_mode, unord_cmp, const0_rtx);
24827 emit_jump_insn (gen_rtx_SET (pc_rtx,
24828 gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
24829 join_ref,
24830 pc_rtx)));
24832 /* Do the normal comparison, knowing that the values are not
24833 NaNs. */
24834 normal_dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
24835 SImode, op0, mode, op1, mode);
24837 emit_insn (gen_cstoresi4 (dest,
24838 gen_rtx_fmt_ee (code, SImode, normal_dest,
24839 const0_rtx),
24840 normal_dest, const0_rtx));
24842 /* Join NaN and non-Nan paths. Compare dest against 0. */
24843 emit_label (join_label);
24844 code = NE;
24847 emit_insn (gen_rtx_SET (compare_result,
24848 gen_rtx_COMPARE (comp_mode, dest, const0_rtx)));
24851 else
24853 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
24854 CLOBBERs to match cmptf_internal2 pattern. */
24855 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
24856 && FLOAT128_IBM_P (GET_MODE (op0))
24857 && TARGET_HARD_FLOAT && TARGET_FPRS)
24858 emit_insn (gen_rtx_PARALLEL (VOIDmode,
24859 gen_rtvec (10,
24860 gen_rtx_SET (compare_result,
24861 gen_rtx_COMPARE (comp_mode, op0, op1)),
24862 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24863 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24864 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24865 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24866 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24867 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24868 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24869 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
24870 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
24871 else if (GET_CODE (op1) == UNSPEC
24872 && XINT (op1, 1) == UNSPEC_SP_TEST)
24874 rtx op1b = XVECEXP (op1, 0, 0);
24875 comp_mode = CCEQmode;
24876 compare_result = gen_reg_rtx (CCEQmode);
24877 if (TARGET_64BIT)
24878 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
24879 else
24880 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
24882 else
24883 emit_insn (gen_rtx_SET (compare_result,
24884 gen_rtx_COMPARE (comp_mode, op0, op1)));
24887 /* Some kinds of FP comparisons need an OR operation;
24888 under flag_finite_math_only we don't bother. */
24889 if (FLOAT_MODE_P (mode)
24890 && (!FLOAT128_IEEE_P (mode) || TARGET_FLOAT128_HW)
24891 && !flag_finite_math_only
24892 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
24893 && (code == LE || code == GE
24894 || code == UNEQ || code == LTGT
24895 || code == UNGT || code == UNLT))
24897 enum rtx_code or1, or2;
24898 rtx or1_rtx, or2_rtx, compare2_rtx;
24899 rtx or_result = gen_reg_rtx (CCEQmode);
24901 switch (code)
24903 case LE: or1 = LT; or2 = EQ; break;
24904 case GE: or1 = GT; or2 = EQ; break;
24905 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
24906 case LTGT: or1 = LT; or2 = GT; break;
24907 case UNGT: or1 = UNORDERED; or2 = GT; break;
24908 case UNLT: or1 = UNORDERED; or2 = LT; break;
24909 default: gcc_unreachable ();
24911 validate_condition_mode (or1, comp_mode);
24912 validate_condition_mode (or2, comp_mode);
24913 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
24914 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
24915 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
24916 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
24917 const_true_rtx);
24918 emit_insn (gen_rtx_SET (or_result, compare2_rtx));
24920 compare_result = or_result;
24921 code = EQ;
24924 validate_condition_mode (code, GET_MODE (compare_result));
24926 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
24930 /* Return the diagnostic message string if the binary operation OP is
24931 not permitted on TYPE1 and TYPE2, NULL otherwise. */
24933 static const char*
24934 rs6000_invalid_binary_op (int op ATTRIBUTE_UNUSED,
24935 const_tree type1,
24936 const_tree type2)
24938 machine_mode mode1 = TYPE_MODE (type1);
24939 machine_mode mode2 = TYPE_MODE (type2);
24941 /* For complex modes, use the inner type. */
24942 if (COMPLEX_MODE_P (mode1))
24943 mode1 = GET_MODE_INNER (mode1);
24945 if (COMPLEX_MODE_P (mode2))
24946 mode2 = GET_MODE_INNER (mode2);
24948 /* Don't allow IEEE 754R 128-bit binary floating point and IBM extended
24949 double to intermix unless -mfloat128-convert. */
24950 if (mode1 == mode2)
24951 return NULL;
24953 if (!TARGET_FLOAT128_CVT)
24955 if ((mode1 == KFmode && mode2 == IFmode)
24956 || (mode1 == IFmode && mode2 == KFmode))
24957 return N_("__float128 and __ibm128 cannot be used in the same "
24958 "expression");
24960 if (TARGET_IEEEQUAD
24961 && ((mode1 == IFmode && mode2 == TFmode)
24962 || (mode1 == TFmode && mode2 == IFmode)))
24963 return N_("__ibm128 and long double cannot be used in the same "
24964 "expression");
24966 if (!TARGET_IEEEQUAD
24967 && ((mode1 == KFmode && mode2 == TFmode)
24968 || (mode1 == TFmode && mode2 == KFmode)))
24969 return N_("__float128 and long double cannot be used in the same "
24970 "expression");
24973 return NULL;
24977 /* Expand floating point conversion to/from __float128 and __ibm128. */
24979 void
24980 rs6000_expand_float128_convert (rtx dest, rtx src, bool unsigned_p)
24982 machine_mode dest_mode = GET_MODE (dest);
24983 machine_mode src_mode = GET_MODE (src);
24984 convert_optab cvt = unknown_optab;
24985 bool do_move = false;
24986 rtx libfunc = NULL_RTX;
24987 rtx dest2;
24988 typedef rtx (*rtx_2func_t) (rtx, rtx);
24989 rtx_2func_t hw_convert = (rtx_2func_t)0;
24990 size_t kf_or_tf;
24992 struct hw_conv_t {
24993 rtx_2func_t from_df;
24994 rtx_2func_t from_sf;
24995 rtx_2func_t from_si_sign;
24996 rtx_2func_t from_si_uns;
24997 rtx_2func_t from_di_sign;
24998 rtx_2func_t from_di_uns;
24999 rtx_2func_t to_df;
25000 rtx_2func_t to_sf;
25001 rtx_2func_t to_si_sign;
25002 rtx_2func_t to_si_uns;
25003 rtx_2func_t to_di_sign;
25004 rtx_2func_t to_di_uns;
25005 } hw_conversions[2] = {
25006 /* convertions to/from KFmode */
25008 gen_extenddfkf2_hw, /* KFmode <- DFmode. */
25009 gen_extendsfkf2_hw, /* KFmode <- SFmode. */
25010 gen_float_kfsi2_hw, /* KFmode <- SImode (signed). */
25011 gen_floatuns_kfsi2_hw, /* KFmode <- SImode (unsigned). */
25012 gen_float_kfdi2_hw, /* KFmode <- DImode (signed). */
25013 gen_floatuns_kfdi2_hw, /* KFmode <- DImode (unsigned). */
25014 gen_trunckfdf2_hw, /* DFmode <- KFmode. */
25015 gen_trunckfsf2_hw, /* SFmode <- KFmode. */
25016 gen_fix_kfsi2_hw, /* SImode <- KFmode (signed). */
25017 gen_fixuns_kfsi2_hw, /* SImode <- KFmode (unsigned). */
25018 gen_fix_kfdi2_hw, /* DImode <- KFmode (signed). */
25019 gen_fixuns_kfdi2_hw, /* DImode <- KFmode (unsigned). */
25022 /* convertions to/from TFmode */
25024 gen_extenddftf2_hw, /* TFmode <- DFmode. */
25025 gen_extendsftf2_hw, /* TFmode <- SFmode. */
25026 gen_float_tfsi2_hw, /* TFmode <- SImode (signed). */
25027 gen_floatuns_tfsi2_hw, /* TFmode <- SImode (unsigned). */
25028 gen_float_tfdi2_hw, /* TFmode <- DImode (signed). */
25029 gen_floatuns_tfdi2_hw, /* TFmode <- DImode (unsigned). */
25030 gen_trunctfdf2_hw, /* DFmode <- TFmode. */
25031 gen_trunctfsf2_hw, /* SFmode <- TFmode. */
25032 gen_fix_tfsi2_hw, /* SImode <- TFmode (signed). */
25033 gen_fixuns_tfsi2_hw, /* SImode <- TFmode (unsigned). */
25034 gen_fix_tfdi2_hw, /* DImode <- TFmode (signed). */
25035 gen_fixuns_tfdi2_hw, /* DImode <- TFmode (unsigned). */
25039 if (dest_mode == src_mode)
25040 gcc_unreachable ();
25042 /* Eliminate memory operations. */
25043 if (MEM_P (src))
25044 src = force_reg (src_mode, src);
25046 if (MEM_P (dest))
25048 rtx tmp = gen_reg_rtx (dest_mode);
25049 rs6000_expand_float128_convert (tmp, src, unsigned_p);
25050 rs6000_emit_move (dest, tmp, dest_mode);
25051 return;
25054 /* Convert to IEEE 128-bit floating point. */
25055 if (FLOAT128_IEEE_P (dest_mode))
25057 if (dest_mode == KFmode)
25058 kf_or_tf = 0;
25059 else if (dest_mode == TFmode)
25060 kf_or_tf = 1;
25061 else
25062 gcc_unreachable ();
25064 switch (src_mode)
25066 case E_DFmode:
25067 cvt = sext_optab;
25068 hw_convert = hw_conversions[kf_or_tf].from_df;
25069 break;
25071 case E_SFmode:
25072 cvt = sext_optab;
25073 hw_convert = hw_conversions[kf_or_tf].from_sf;
25074 break;
25076 case E_KFmode:
25077 case E_IFmode:
25078 case E_TFmode:
25079 if (FLOAT128_IBM_P (src_mode))
25080 cvt = sext_optab;
25081 else
25082 do_move = true;
25083 break;
25085 case E_SImode:
25086 if (unsigned_p)
25088 cvt = ufloat_optab;
25089 hw_convert = hw_conversions[kf_or_tf].from_si_uns;
25091 else
25093 cvt = sfloat_optab;
25094 hw_convert = hw_conversions[kf_or_tf].from_si_sign;
25096 break;
25098 case E_DImode:
25099 if (unsigned_p)
25101 cvt = ufloat_optab;
25102 hw_convert = hw_conversions[kf_or_tf].from_di_uns;
25104 else
25106 cvt = sfloat_optab;
25107 hw_convert = hw_conversions[kf_or_tf].from_di_sign;
25109 break;
25111 default:
25112 gcc_unreachable ();
25116 /* Convert from IEEE 128-bit floating point. */
25117 else if (FLOAT128_IEEE_P (src_mode))
25119 if (src_mode == KFmode)
25120 kf_or_tf = 0;
25121 else if (src_mode == TFmode)
25122 kf_or_tf = 1;
25123 else
25124 gcc_unreachable ();
25126 switch (dest_mode)
25128 case E_DFmode:
25129 cvt = trunc_optab;
25130 hw_convert = hw_conversions[kf_or_tf].to_df;
25131 break;
25133 case E_SFmode:
25134 cvt = trunc_optab;
25135 hw_convert = hw_conversions[kf_or_tf].to_sf;
25136 break;
25138 case E_KFmode:
25139 case E_IFmode:
25140 case E_TFmode:
25141 if (FLOAT128_IBM_P (dest_mode))
25142 cvt = trunc_optab;
25143 else
25144 do_move = true;
25145 break;
25147 case E_SImode:
25148 if (unsigned_p)
25150 cvt = ufix_optab;
25151 hw_convert = hw_conversions[kf_or_tf].to_si_uns;
25153 else
25155 cvt = sfix_optab;
25156 hw_convert = hw_conversions[kf_or_tf].to_si_sign;
25158 break;
25160 case E_DImode:
25161 if (unsigned_p)
25163 cvt = ufix_optab;
25164 hw_convert = hw_conversions[kf_or_tf].to_di_uns;
25166 else
25168 cvt = sfix_optab;
25169 hw_convert = hw_conversions[kf_or_tf].to_di_sign;
25171 break;
25173 default:
25174 gcc_unreachable ();
25178 /* Both IBM format. */
25179 else if (FLOAT128_IBM_P (dest_mode) && FLOAT128_IBM_P (src_mode))
25180 do_move = true;
25182 else
25183 gcc_unreachable ();
25185 /* Handle conversion between TFmode/KFmode. */
25186 if (do_move)
25187 emit_move_insn (dest, gen_lowpart (dest_mode, src));
25189 /* Handle conversion if we have hardware support. */
25190 else if (TARGET_FLOAT128_HW && hw_convert)
25191 emit_insn ((hw_convert) (dest, src));
25193 /* Call an external function to do the conversion. */
25194 else if (cvt != unknown_optab)
25196 libfunc = convert_optab_libfunc (cvt, dest_mode, src_mode);
25197 gcc_assert (libfunc != NULL_RTX);
25199 dest2 = emit_library_call_value (libfunc, dest, LCT_CONST, dest_mode,
25200 src, src_mode);
25202 gcc_assert (dest2 != NULL_RTX);
25203 if (!rtx_equal_p (dest, dest2))
25204 emit_move_insn (dest, dest2);
25207 else
25208 gcc_unreachable ();
25210 return;
25214 /* Emit the RTL for an sISEL pattern. */
25216 void
25217 rs6000_emit_sISEL (machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
25219 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
25222 /* Emit RTL that sets a register to zero if OP1 and OP2 are equal. SCRATCH
25223 can be used as that dest register. Return the dest register. */
25226 rs6000_emit_eqne (machine_mode mode, rtx op1, rtx op2, rtx scratch)
25228 if (op2 == const0_rtx)
25229 return op1;
25231 if (GET_CODE (scratch) == SCRATCH)
25232 scratch = gen_reg_rtx (mode);
25234 if (logical_operand (op2, mode))
25235 emit_insn (gen_rtx_SET (scratch, gen_rtx_XOR (mode, op1, op2)));
25236 else
25237 emit_insn (gen_rtx_SET (scratch,
25238 gen_rtx_PLUS (mode, op1, negate_rtx (mode, op2))));
25240 return scratch;
25243 void
25244 rs6000_emit_sCOND (machine_mode mode, rtx operands[])
25246 rtx condition_rtx;
25247 machine_mode op_mode;
25248 enum rtx_code cond_code;
25249 rtx result = operands[0];
25251 condition_rtx = rs6000_generate_compare (operands[1], mode);
25252 cond_code = GET_CODE (condition_rtx);
25254 if (FLOAT_MODE_P (mode)
25255 && !TARGET_FPRS && TARGET_HARD_FLOAT)
25257 rtx t;
25259 PUT_MODE (condition_rtx, SImode);
25260 t = XEXP (condition_rtx, 0);
25262 gcc_assert (cond_code == NE || cond_code == EQ);
25264 if (cond_code == NE)
25265 emit_insn (gen_e500_flip_gt_bit (t, t));
25267 emit_insn (gen_move_from_CR_gt_bit (result, t));
25268 return;
25271 if (cond_code == NE
25272 || cond_code == GE || cond_code == LE
25273 || cond_code == GEU || cond_code == LEU
25274 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
25276 rtx not_result = gen_reg_rtx (CCEQmode);
25277 rtx not_op, rev_cond_rtx;
25278 machine_mode cc_mode;
25280 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
25282 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
25283 SImode, XEXP (condition_rtx, 0), const0_rtx);
25284 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
25285 emit_insn (gen_rtx_SET (not_result, not_op));
25286 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
25289 op_mode = GET_MODE (XEXP (operands[1], 0));
25290 if (op_mode == VOIDmode)
25291 op_mode = GET_MODE (XEXP (operands[1], 1));
25293 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
25295 PUT_MODE (condition_rtx, DImode);
25296 convert_move (result, condition_rtx, 0);
25298 else
25300 PUT_MODE (condition_rtx, SImode);
25301 emit_insn (gen_rtx_SET (result, condition_rtx));
25305 /* Emit a branch of kind CODE to location LOC. */
25307 void
25308 rs6000_emit_cbranch (machine_mode mode, rtx operands[])
25310 rtx condition_rtx, loc_ref;
25312 condition_rtx = rs6000_generate_compare (operands[0], mode);
25313 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
25314 emit_jump_insn (gen_rtx_SET (pc_rtx,
25315 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
25316 loc_ref, pc_rtx)));
25319 /* Return the string to output a conditional branch to LABEL, which is
25320 the operand template of the label, or NULL if the branch is really a
25321 conditional return.
25323 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
25324 condition code register and its mode specifies what kind of
25325 comparison we made.
25327 REVERSED is nonzero if we should reverse the sense of the comparison.
25329 INSN is the insn. */
25331 char *
25332 output_cbranch (rtx op, const char *label, int reversed, rtx_insn *insn)
25334 static char string[64];
25335 enum rtx_code code = GET_CODE (op);
25336 rtx cc_reg = XEXP (op, 0);
25337 machine_mode mode = GET_MODE (cc_reg);
25338 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
25339 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
25340 int really_reversed = reversed ^ need_longbranch;
25341 char *s = string;
25342 const char *ccode;
25343 const char *pred;
25344 rtx note;
25346 validate_condition_mode (code, mode);
25348 /* Work out which way this really branches. We could use
25349 reverse_condition_maybe_unordered here always but this
25350 makes the resulting assembler clearer. */
25351 if (really_reversed)
25353 /* Reversal of FP compares takes care -- an ordered compare
25354 becomes an unordered compare and vice versa. */
25355 if (mode == CCFPmode)
25356 code = reverse_condition_maybe_unordered (code);
25357 else
25358 code = reverse_condition (code);
25361 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
25363 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
25364 to the GT bit. */
25365 switch (code)
25367 case EQ:
25368 /* Opposite of GT. */
25369 code = GT;
25370 break;
25372 case NE:
25373 code = UNLE;
25374 break;
25376 default:
25377 gcc_unreachable ();
25381 switch (code)
25383 /* Not all of these are actually distinct opcodes, but
25384 we distinguish them for clarity of the resulting assembler. */
25385 case NE: case LTGT:
25386 ccode = "ne"; break;
25387 case EQ: case UNEQ:
25388 ccode = "eq"; break;
25389 case GE: case GEU:
25390 ccode = "ge"; break;
25391 case GT: case GTU: case UNGT:
25392 ccode = "gt"; break;
25393 case LE: case LEU:
25394 ccode = "le"; break;
25395 case LT: case LTU: case UNLT:
25396 ccode = "lt"; break;
25397 case UNORDERED: ccode = "un"; break;
25398 case ORDERED: ccode = "nu"; break;
25399 case UNGE: ccode = "nl"; break;
25400 case UNLE: ccode = "ng"; break;
25401 default:
25402 gcc_unreachable ();
25405 /* Maybe we have a guess as to how likely the branch is. */
25406 pred = "";
25407 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
25408 if (note != NULL_RTX)
25410 /* PROB is the difference from 50%. */
25411 int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
25412 .to_reg_br_prob_base () - REG_BR_PROB_BASE / 2;
25414 /* Only hint for highly probable/improbable branches on newer cpus when
25415 we have real profile data, as static prediction overrides processor
25416 dynamic prediction. For older cpus we may as well always hint, but
25417 assume not taken for branches that are very close to 50% as a
25418 mispredicted taken branch is more expensive than a
25419 mispredicted not-taken branch. */
25420 if (rs6000_always_hint
25421 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
25422 && (profile_status_for_fn (cfun) != PROFILE_GUESSED)
25423 && br_prob_note_reliable_p (note)))
25425 if (abs (prob) > REG_BR_PROB_BASE / 20
25426 && ((prob > 0) ^ need_longbranch))
25427 pred = "+";
25428 else
25429 pred = "-";
25433 if (label == NULL)
25434 s += sprintf (s, "b%slr%s ", ccode, pred);
25435 else
25436 s += sprintf (s, "b%s%s ", ccode, pred);
25438 /* We need to escape any '%' characters in the reg_names string.
25439 Assume they'd only be the first character.... */
25440 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
25441 *s++ = '%';
25442 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
25444 if (label != NULL)
25446 /* If the branch distance was too far, we may have to use an
25447 unconditional branch to go the distance. */
25448 if (need_longbranch)
25449 s += sprintf (s, ",$+8\n\tb %s", label);
25450 else
25451 s += sprintf (s, ",%s", label);
25454 return string;
25457 /* Return the string to flip the GT bit on a CR. */
25458 char *
25459 output_e500_flip_gt_bit (rtx dst, rtx src)
25461 static char string[64];
25462 int a, b;
25464 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
25465 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
25467 /* GT bit. */
25468 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
25469 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
25471 sprintf (string, "crnot %d,%d", a, b);
25472 return string;
25475 /* Return insn for VSX or Altivec comparisons. */
25477 static rtx
25478 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
25480 rtx mask;
25481 machine_mode mode = GET_MODE (op0);
25483 switch (code)
25485 default:
25486 break;
25488 case GE:
25489 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
25490 return NULL_RTX;
25491 /* FALLTHRU */
25493 case EQ:
25494 case GT:
25495 case GTU:
25496 case ORDERED:
25497 case UNORDERED:
25498 case UNEQ:
25499 case LTGT:
25500 mask = gen_reg_rtx (mode);
25501 emit_insn (gen_rtx_SET (mask, gen_rtx_fmt_ee (code, mode, op0, op1)));
25502 return mask;
25505 return NULL_RTX;
25508 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
25509 DMODE is expected destination mode. This is a recursive function. */
25511 static rtx
25512 rs6000_emit_vector_compare (enum rtx_code rcode,
25513 rtx op0, rtx op1,
25514 machine_mode dmode)
25516 rtx mask;
25517 bool swap_operands = false;
25518 bool try_again = false;
25520 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
25521 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
25523 /* See if the comparison works as is. */
25524 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
25525 if (mask)
25526 return mask;
25528 switch (rcode)
25530 case LT:
25531 rcode = GT;
25532 swap_operands = true;
25533 try_again = true;
25534 break;
25535 case LTU:
25536 rcode = GTU;
25537 swap_operands = true;
25538 try_again = true;
25539 break;
25540 case NE:
25541 case UNLE:
25542 case UNLT:
25543 case UNGE:
25544 case UNGT:
25545 /* Invert condition and try again.
25546 e.g., A != B becomes ~(A==B). */
25548 enum rtx_code rev_code;
25549 enum insn_code nor_code;
25550 rtx mask2;
25552 rev_code = reverse_condition_maybe_unordered (rcode);
25553 if (rev_code == UNKNOWN)
25554 return NULL_RTX;
25556 nor_code = optab_handler (one_cmpl_optab, dmode);
25557 if (nor_code == CODE_FOR_nothing)
25558 return NULL_RTX;
25560 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
25561 if (!mask2)
25562 return NULL_RTX;
25564 mask = gen_reg_rtx (dmode);
25565 emit_insn (GEN_FCN (nor_code) (mask, mask2));
25566 return mask;
25568 break;
25569 case GE:
25570 case GEU:
25571 case LE:
25572 case LEU:
25573 /* Try GT/GTU/LT/LTU OR EQ */
25575 rtx c_rtx, eq_rtx;
25576 enum insn_code ior_code;
25577 enum rtx_code new_code;
25579 switch (rcode)
25581 case GE:
25582 new_code = GT;
25583 break;
25585 case GEU:
25586 new_code = GTU;
25587 break;
25589 case LE:
25590 new_code = LT;
25591 break;
25593 case LEU:
25594 new_code = LTU;
25595 break;
25597 default:
25598 gcc_unreachable ();
25601 ior_code = optab_handler (ior_optab, dmode);
25602 if (ior_code == CODE_FOR_nothing)
25603 return NULL_RTX;
25605 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
25606 if (!c_rtx)
25607 return NULL_RTX;
25609 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
25610 if (!eq_rtx)
25611 return NULL_RTX;
25613 mask = gen_reg_rtx (dmode);
25614 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
25615 return mask;
25617 break;
25618 default:
25619 return NULL_RTX;
25622 if (try_again)
25624 if (swap_operands)
25625 std::swap (op0, op1);
25627 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
25628 if (mask)
25629 return mask;
25632 /* You only get two chances. */
25633 return NULL_RTX;
25636 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
25637 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
25638 operands for the relation operation COND. */
25641 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
25642 rtx cond, rtx cc_op0, rtx cc_op1)
25644 machine_mode dest_mode = GET_MODE (dest);
25645 machine_mode mask_mode = GET_MODE (cc_op0);
25646 enum rtx_code rcode = GET_CODE (cond);
25647 machine_mode cc_mode = CCmode;
25648 rtx mask;
25649 rtx cond2;
25650 bool invert_move = false;
25652 if (VECTOR_UNIT_NONE_P (dest_mode))
25653 return 0;
25655 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
25656 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
25658 switch (rcode)
25660 /* Swap operands if we can, and fall back to doing the operation as
25661 specified, and doing a NOR to invert the test. */
25662 case NE:
25663 case UNLE:
25664 case UNLT:
25665 case UNGE:
25666 case UNGT:
25667 /* Invert condition and try again.
25668 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
25669 invert_move = true;
25670 rcode = reverse_condition_maybe_unordered (rcode);
25671 if (rcode == UNKNOWN)
25672 return 0;
25673 break;
25675 case GE:
25676 case LE:
25677 if (GET_MODE_CLASS (mask_mode) == MODE_VECTOR_INT)
25679 /* Invert condition to avoid compound test. */
25680 invert_move = true;
25681 rcode = reverse_condition (rcode);
25683 break;
25685 case GTU:
25686 case GEU:
25687 case LTU:
25688 case LEU:
25689 /* Mark unsigned tests with CCUNSmode. */
25690 cc_mode = CCUNSmode;
25692 /* Invert condition to avoid compound test if necessary. */
25693 if (rcode == GEU || rcode == LEU)
25695 invert_move = true;
25696 rcode = reverse_condition (rcode);
25698 break;
25700 default:
25701 break;
25704 /* Get the vector mask for the given relational operations. */
25705 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
25707 if (!mask)
25708 return 0;
25710 if (invert_move)
25711 std::swap (op_true, op_false);
25713 /* Optimize vec1 == vec2, to know the mask generates -1/0. */
25714 if (GET_MODE_CLASS (dest_mode) == MODE_VECTOR_INT
25715 && (GET_CODE (op_true) == CONST_VECTOR
25716 || GET_CODE (op_false) == CONST_VECTOR))
25718 rtx constant_0 = CONST0_RTX (dest_mode);
25719 rtx constant_m1 = CONSTM1_RTX (dest_mode);
25721 if (op_true == constant_m1 && op_false == constant_0)
25723 emit_move_insn (dest, mask);
25724 return 1;
25727 else if (op_true == constant_0 && op_false == constant_m1)
25729 emit_insn (gen_rtx_SET (dest, gen_rtx_NOT (dest_mode, mask)));
25730 return 1;
25733 /* If we can't use the vector comparison directly, perhaps we can use
25734 the mask for the true or false fields, instead of loading up a
25735 constant. */
25736 if (op_true == constant_m1)
25737 op_true = mask;
25739 if (op_false == constant_0)
25740 op_false = mask;
25743 if (!REG_P (op_true) && !SUBREG_P (op_true))
25744 op_true = force_reg (dest_mode, op_true);
25746 if (!REG_P (op_false) && !SUBREG_P (op_false))
25747 op_false = force_reg (dest_mode, op_false);
25749 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
25750 CONST0_RTX (dest_mode));
25751 emit_insn (gen_rtx_SET (dest,
25752 gen_rtx_IF_THEN_ELSE (dest_mode,
25753 cond2,
25754 op_true,
25755 op_false)));
25756 return 1;
25759 /* ISA 3.0 (power9) minmax subcase to emit a XSMAXCDP or XSMINCDP instruction
25760 for SF/DF scalars. Move TRUE_COND to DEST if OP of the operands of the last
25761 comparison is nonzero/true, FALSE_COND if it is zero/false. Return 0 if the
25762 hardware has no such operation. */
25764 static int
25765 rs6000_emit_p9_fp_minmax (rtx dest, rtx op, rtx true_cond, rtx false_cond)
25767 enum rtx_code code = GET_CODE (op);
25768 rtx op0 = XEXP (op, 0);
25769 rtx op1 = XEXP (op, 1);
25770 machine_mode compare_mode = GET_MODE (op0);
25771 machine_mode result_mode = GET_MODE (dest);
25772 bool max_p = false;
25774 if (result_mode != compare_mode)
25775 return 0;
25777 if (code == GE || code == GT)
25778 max_p = true;
25779 else if (code == LE || code == LT)
25780 max_p = false;
25781 else
25782 return 0;
25784 if (rtx_equal_p (op0, true_cond) && rtx_equal_p (op1, false_cond))
25787 else if (rtx_equal_p (op1, true_cond) && rtx_equal_p (op0, false_cond))
25788 max_p = !max_p;
25790 else
25791 return 0;
25793 rs6000_emit_minmax (dest, max_p ? SMAX : SMIN, op0, op1);
25794 return 1;
25797 /* ISA 3.0 (power9) conditional move subcase to emit XSCMP{EQ,GE,GT,NE}DP and
25798 XXSEL instructions for SF/DF scalars. Move TRUE_COND to DEST if OP of the
25799 operands of the last comparison is nonzero/true, FALSE_COND if it is
25800 zero/false. Return 0 if the hardware has no such operation. */
25802 static int
25803 rs6000_emit_p9_fp_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
25805 enum rtx_code code = GET_CODE (op);
25806 rtx op0 = XEXP (op, 0);
25807 rtx op1 = XEXP (op, 1);
25808 machine_mode result_mode = GET_MODE (dest);
25809 rtx compare_rtx;
25810 rtx cmove_rtx;
25811 rtx clobber_rtx;
25813 if (!can_create_pseudo_p ())
25814 return 0;
25816 switch (code)
25818 case EQ:
25819 case GE:
25820 case GT:
25821 break;
25823 case NE:
25824 case LT:
25825 case LE:
25826 code = swap_condition (code);
25827 std::swap (op0, op1);
25828 break;
25830 default:
25831 return 0;
25834 /* Generate: [(parallel [(set (dest)
25835 (if_then_else (op (cmp1) (cmp2))
25836 (true)
25837 (false)))
25838 (clobber (scratch))])]. */
25840 compare_rtx = gen_rtx_fmt_ee (code, CCFPmode, op0, op1);
25841 cmove_rtx = gen_rtx_SET (dest,
25842 gen_rtx_IF_THEN_ELSE (result_mode,
25843 compare_rtx,
25844 true_cond,
25845 false_cond));
25847 clobber_rtx = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (V2DImode));
25848 emit_insn (gen_rtx_PARALLEL (VOIDmode,
25849 gen_rtvec (2, cmove_rtx, clobber_rtx)));
25851 return 1;
25854 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
25855 operands of the last comparison is nonzero/true, FALSE_COND if it
25856 is zero/false. Return 0 if the hardware has no such operation. */
25859 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
25861 enum rtx_code code = GET_CODE (op);
25862 rtx op0 = XEXP (op, 0);
25863 rtx op1 = XEXP (op, 1);
25864 machine_mode compare_mode = GET_MODE (op0);
25865 machine_mode result_mode = GET_MODE (dest);
25866 rtx temp;
25867 bool is_against_zero;
25869 /* These modes should always match. */
25870 if (GET_MODE (op1) != compare_mode
25871 /* In the isel case however, we can use a compare immediate, so
25872 op1 may be a small constant. */
25873 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
25874 return 0;
25875 if (GET_MODE (true_cond) != result_mode)
25876 return 0;
25877 if (GET_MODE (false_cond) != result_mode)
25878 return 0;
25880 /* See if we can use the ISA 3.0 (power9) min/max/compare functions. */
25881 if (TARGET_P9_MINMAX
25882 && (compare_mode == SFmode || compare_mode == DFmode)
25883 && (result_mode == SFmode || result_mode == DFmode))
25885 if (rs6000_emit_p9_fp_minmax (dest, op, true_cond, false_cond))
25886 return 1;
25888 if (rs6000_emit_p9_fp_cmove (dest, op, true_cond, false_cond))
25889 return 1;
25892 /* Don't allow using floating point comparisons for integer results for
25893 now. */
25894 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
25895 return 0;
25897 /* First, work out if the hardware can do this at all, or
25898 if it's too slow.... */
25899 if (!FLOAT_MODE_P (compare_mode))
25901 if (TARGET_ISEL)
25902 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
25903 return 0;
25905 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
25906 && SCALAR_FLOAT_MODE_P (compare_mode))
25907 return 0;
25909 is_against_zero = op1 == CONST0_RTX (compare_mode);
25911 /* A floating-point subtract might overflow, underflow, or produce
25912 an inexact result, thus changing the floating-point flags, so it
25913 can't be generated if we care about that. It's safe if one side
25914 of the construct is zero, since then no subtract will be
25915 generated. */
25916 if (SCALAR_FLOAT_MODE_P (compare_mode)
25917 && flag_trapping_math && ! is_against_zero)
25918 return 0;
25920 /* Eliminate half of the comparisons by switching operands, this
25921 makes the remaining code simpler. */
25922 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
25923 || code == LTGT || code == LT || code == UNLE)
25925 code = reverse_condition_maybe_unordered (code);
25926 temp = true_cond;
25927 true_cond = false_cond;
25928 false_cond = temp;
25931 /* UNEQ and LTGT take four instructions for a comparison with zero,
25932 it'll probably be faster to use a branch here too. */
25933 if (code == UNEQ && HONOR_NANS (compare_mode))
25934 return 0;
25936 /* We're going to try to implement comparisons by performing
25937 a subtract, then comparing against zero. Unfortunately,
25938 Inf - Inf is NaN which is not zero, and so if we don't
25939 know that the operand is finite and the comparison
25940 would treat EQ different to UNORDERED, we can't do it. */
25941 if (HONOR_INFINITIES (compare_mode)
25942 && code != GT && code != UNGE
25943 && (GET_CODE (op1) != CONST_DOUBLE
25944 || real_isinf (CONST_DOUBLE_REAL_VALUE (op1)))
25945 /* Constructs of the form (a OP b ? a : b) are safe. */
25946 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
25947 || (! rtx_equal_p (op0, true_cond)
25948 && ! rtx_equal_p (op1, true_cond))))
25949 return 0;
25951 /* At this point we know we can use fsel. */
25953 /* Reduce the comparison to a comparison against zero. */
25954 if (! is_against_zero)
25956 temp = gen_reg_rtx (compare_mode);
25957 emit_insn (gen_rtx_SET (temp, gen_rtx_MINUS (compare_mode, op0, op1)));
25958 op0 = temp;
25959 op1 = CONST0_RTX (compare_mode);
25962 /* If we don't care about NaNs we can reduce some of the comparisons
25963 down to faster ones. */
25964 if (! HONOR_NANS (compare_mode))
25965 switch (code)
25967 case GT:
25968 code = LE;
25969 temp = true_cond;
25970 true_cond = false_cond;
25971 false_cond = temp;
25972 break;
25973 case UNGE:
25974 code = GE;
25975 break;
25976 case UNEQ:
25977 code = EQ;
25978 break;
25979 default:
25980 break;
25983 /* Now, reduce everything down to a GE. */
25984 switch (code)
25986 case GE:
25987 break;
25989 case LE:
25990 temp = gen_reg_rtx (compare_mode);
25991 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
25992 op0 = temp;
25993 break;
25995 case ORDERED:
25996 temp = gen_reg_rtx (compare_mode);
25997 emit_insn (gen_rtx_SET (temp, gen_rtx_ABS (compare_mode, op0)));
25998 op0 = temp;
25999 break;
26001 case EQ:
26002 temp = gen_reg_rtx (compare_mode);
26003 emit_insn (gen_rtx_SET (temp,
26004 gen_rtx_NEG (compare_mode,
26005 gen_rtx_ABS (compare_mode, op0))));
26006 op0 = temp;
26007 break;
26009 case UNGE:
26010 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
26011 temp = gen_reg_rtx (result_mode);
26012 emit_insn (gen_rtx_SET (temp,
26013 gen_rtx_IF_THEN_ELSE (result_mode,
26014 gen_rtx_GE (VOIDmode,
26015 op0, op1),
26016 true_cond, false_cond)));
26017 false_cond = true_cond;
26018 true_cond = temp;
26020 temp = gen_reg_rtx (compare_mode);
26021 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
26022 op0 = temp;
26023 break;
26025 case GT:
26026 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
26027 temp = gen_reg_rtx (result_mode);
26028 emit_insn (gen_rtx_SET (temp,
26029 gen_rtx_IF_THEN_ELSE (result_mode,
26030 gen_rtx_GE (VOIDmode,
26031 op0, op1),
26032 true_cond, false_cond)));
26033 true_cond = false_cond;
26034 false_cond = temp;
26036 temp = gen_reg_rtx (compare_mode);
26037 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
26038 op0 = temp;
26039 break;
26041 default:
26042 gcc_unreachable ();
26045 emit_insn (gen_rtx_SET (dest,
26046 gen_rtx_IF_THEN_ELSE (result_mode,
26047 gen_rtx_GE (VOIDmode,
26048 op0, op1),
26049 true_cond, false_cond)));
26050 return 1;
26053 /* Same as above, but for ints (isel). */
26055 static int
26056 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
26058 rtx condition_rtx, cr;
26059 machine_mode mode = GET_MODE (dest);
26060 enum rtx_code cond_code;
26061 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
26062 bool signedp;
26064 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
26065 return 0;
26067 /* We still have to do the compare, because isel doesn't do a
26068 compare, it just looks at the CRx bits set by a previous compare
26069 instruction. */
26070 condition_rtx = rs6000_generate_compare (op, mode);
26071 cond_code = GET_CODE (condition_rtx);
26072 cr = XEXP (condition_rtx, 0);
26073 signedp = GET_MODE (cr) == CCmode;
26075 isel_func = (mode == SImode
26076 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
26077 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
26079 switch (cond_code)
26081 case LT: case GT: case LTU: case GTU: case EQ:
26082 /* isel handles these directly. */
26083 break;
26085 default:
26086 /* We need to swap the sense of the comparison. */
26088 std::swap (false_cond, true_cond);
26089 PUT_CODE (condition_rtx, reverse_condition (cond_code));
26091 break;
26094 false_cond = force_reg (mode, false_cond);
26095 if (true_cond != const0_rtx)
26096 true_cond = force_reg (mode, true_cond);
26098 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
26100 return 1;
26103 const char *
26104 output_isel (rtx *operands)
26106 enum rtx_code code;
26108 code = GET_CODE (operands[1]);
26110 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
26112 gcc_assert (GET_CODE (operands[2]) == REG
26113 && GET_CODE (operands[3]) == REG);
26114 PUT_CODE (operands[1], reverse_condition (code));
26115 return "isel %0,%3,%2,%j1";
26118 return "isel %0,%2,%3,%j1";
26121 void
26122 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
26124 machine_mode mode = GET_MODE (op0);
26125 enum rtx_code c;
26126 rtx target;
26128 /* VSX/altivec have direct min/max insns. */
26129 if ((code == SMAX || code == SMIN)
26130 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
26131 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
26133 emit_insn (gen_rtx_SET (dest, gen_rtx_fmt_ee (code, mode, op0, op1)));
26134 return;
26137 if (code == SMAX || code == SMIN)
26138 c = GE;
26139 else
26140 c = GEU;
26142 if (code == SMAX || code == UMAX)
26143 target = emit_conditional_move (dest, c, op0, op1, mode,
26144 op0, op1, mode, 0);
26145 else
26146 target = emit_conditional_move (dest, c, op0, op1, mode,
26147 op1, op0, mode, 0);
26148 gcc_assert (target);
26149 if (target != dest)
26150 emit_move_insn (dest, target);
26153 /* Split a signbit operation on 64-bit machines with direct move. Also allow
26154 for the value to come from memory or if it is already loaded into a GPR. */
26156 void
26157 rs6000_split_signbit (rtx dest, rtx src)
26159 machine_mode d_mode = GET_MODE (dest);
26160 machine_mode s_mode = GET_MODE (src);
26161 rtx dest_di = (d_mode == DImode) ? dest : gen_lowpart (DImode, dest);
26162 rtx shift_reg = dest_di;
26164 gcc_assert (FLOAT128_IEEE_P (s_mode) && TARGET_POWERPC64);
26166 if (MEM_P (src))
26168 rtx mem = (WORDS_BIG_ENDIAN
26169 ? adjust_address (src, DImode, 0)
26170 : adjust_address (src, DImode, 8));
26171 emit_insn (gen_rtx_SET (dest_di, mem));
26174 else
26176 unsigned int r = reg_or_subregno (src);
26178 if (INT_REGNO_P (r))
26179 shift_reg = gen_rtx_REG (DImode, r + (BYTES_BIG_ENDIAN == 0));
26181 else
26183 /* Generate the special mfvsrd instruction to get it in a GPR. */
26184 gcc_assert (VSX_REGNO_P (r));
26185 if (s_mode == KFmode)
26186 emit_insn (gen_signbitkf2_dm2 (dest_di, src));
26187 else
26188 emit_insn (gen_signbittf2_dm2 (dest_di, src));
26192 emit_insn (gen_lshrdi3 (dest_di, shift_reg, GEN_INT (63)));
26193 return;
26196 /* A subroutine of the atomic operation splitters. Jump to LABEL if
26197 COND is true. Mark the jump as unlikely to be taken. */
26199 static void
26200 emit_unlikely_jump (rtx cond, rtx label)
26202 rtx x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
26203 rtx_insn *insn = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
26204 add_reg_br_prob_note (insn, profile_probability::very_unlikely ());
26207 /* A subroutine of the atomic operation splitters. Emit a load-locked
26208 instruction in MODE. For QI/HImode, possibly use a pattern than includes
26209 the zero_extend operation. */
26211 static void
26212 emit_load_locked (machine_mode mode, rtx reg, rtx mem)
26214 rtx (*fn) (rtx, rtx) = NULL;
26216 switch (mode)
26218 case E_QImode:
26219 fn = gen_load_lockedqi;
26220 break;
26221 case E_HImode:
26222 fn = gen_load_lockedhi;
26223 break;
26224 case E_SImode:
26225 if (GET_MODE (mem) == QImode)
26226 fn = gen_load_lockedqi_si;
26227 else if (GET_MODE (mem) == HImode)
26228 fn = gen_load_lockedhi_si;
26229 else
26230 fn = gen_load_lockedsi;
26231 break;
26232 case E_DImode:
26233 fn = gen_load_lockeddi;
26234 break;
26235 case E_TImode:
26236 fn = gen_load_lockedti;
26237 break;
26238 default:
26239 gcc_unreachable ();
26241 emit_insn (fn (reg, mem));
26244 /* A subroutine of the atomic operation splitters. Emit a store-conditional
26245 instruction in MODE. */
26247 static void
26248 emit_store_conditional (machine_mode mode, rtx res, rtx mem, rtx val)
26250 rtx (*fn) (rtx, rtx, rtx) = NULL;
26252 switch (mode)
26254 case E_QImode:
26255 fn = gen_store_conditionalqi;
26256 break;
26257 case E_HImode:
26258 fn = gen_store_conditionalhi;
26259 break;
26260 case E_SImode:
26261 fn = gen_store_conditionalsi;
26262 break;
26263 case E_DImode:
26264 fn = gen_store_conditionaldi;
26265 break;
26266 case E_TImode:
26267 fn = gen_store_conditionalti;
26268 break;
26269 default:
26270 gcc_unreachable ();
26273 /* Emit sync before stwcx. to address PPC405 Erratum. */
26274 if (PPC405_ERRATUM77)
26275 emit_insn (gen_hwsync ());
26277 emit_insn (fn (res, mem, val));
26280 /* Expand barriers before and after a load_locked/store_cond sequence. */
26282 static rtx
26283 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
26285 rtx addr = XEXP (mem, 0);
26286 int strict_p = (reload_in_progress || reload_completed);
26288 if (!legitimate_indirect_address_p (addr, strict_p)
26289 && !legitimate_indexed_address_p (addr, strict_p))
26291 addr = force_reg (Pmode, addr);
26292 mem = replace_equiv_address_nv (mem, addr);
26295 switch (model)
26297 case MEMMODEL_RELAXED:
26298 case MEMMODEL_CONSUME:
26299 case MEMMODEL_ACQUIRE:
26300 break;
26301 case MEMMODEL_RELEASE:
26302 case MEMMODEL_ACQ_REL:
26303 emit_insn (gen_lwsync ());
26304 break;
26305 case MEMMODEL_SEQ_CST:
26306 emit_insn (gen_hwsync ());
26307 break;
26308 default:
26309 gcc_unreachable ();
26311 return mem;
26314 static void
26315 rs6000_post_atomic_barrier (enum memmodel model)
26317 switch (model)
26319 case MEMMODEL_RELAXED:
26320 case MEMMODEL_CONSUME:
26321 case MEMMODEL_RELEASE:
26322 break;
26323 case MEMMODEL_ACQUIRE:
26324 case MEMMODEL_ACQ_REL:
26325 case MEMMODEL_SEQ_CST:
26326 emit_insn (gen_isync ());
26327 break;
26328 default:
26329 gcc_unreachable ();
26333 /* A subroutine of the various atomic expanders. For sub-word operations,
26334 we must adjust things to operate on SImode. Given the original MEM,
26335 return a new aligned memory. Also build and return the quantities by
26336 which to shift and mask. */
26338 static rtx
26339 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
26341 rtx addr, align, shift, mask, mem;
26342 HOST_WIDE_INT shift_mask;
26343 machine_mode mode = GET_MODE (orig_mem);
26345 /* For smaller modes, we have to implement this via SImode. */
26346 shift_mask = (mode == QImode ? 0x18 : 0x10);
26348 addr = XEXP (orig_mem, 0);
26349 addr = force_reg (GET_MODE (addr), addr);
26351 /* Aligned memory containing subword. Generate a new memory. We
26352 do not want any of the existing MEM_ATTR data, as we're now
26353 accessing memory outside the original object. */
26354 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
26355 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26356 mem = gen_rtx_MEM (SImode, align);
26357 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
26358 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
26359 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
26361 /* Shift amount for subword relative to aligned word. */
26362 shift = gen_reg_rtx (SImode);
26363 addr = gen_lowpart (SImode, addr);
26364 rtx tmp = gen_reg_rtx (SImode);
26365 emit_insn (gen_ashlsi3 (tmp, addr, GEN_INT (3)));
26366 emit_insn (gen_andsi3 (shift, tmp, GEN_INT (shift_mask)));
26367 if (BYTES_BIG_ENDIAN)
26368 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
26369 shift, 1, OPTAB_LIB_WIDEN);
26370 *pshift = shift;
26372 /* Mask for insertion. */
26373 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
26374 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
26375 *pmask = mask;
26377 return mem;
26380 /* A subroutine of the various atomic expanders. For sub-word operands,
26381 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
26383 static rtx
26384 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
26386 rtx x;
26388 x = gen_reg_rtx (SImode);
26389 emit_insn (gen_rtx_SET (x, gen_rtx_AND (SImode,
26390 gen_rtx_NOT (SImode, mask),
26391 oldval)));
26393 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
26395 return x;
26398 /* A subroutine of the various atomic expanders. For sub-word operands,
26399 extract WIDE to NARROW via SHIFT. */
26401 static void
26402 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
26404 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
26405 wide, 1, OPTAB_LIB_WIDEN);
26406 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
26409 /* Expand an atomic compare and swap operation. */
26411 void
26412 rs6000_expand_atomic_compare_and_swap (rtx operands[])
26414 rtx boolval, retval, mem, oldval, newval, cond;
26415 rtx label1, label2, x, mask, shift;
26416 machine_mode mode, orig_mode;
26417 enum memmodel mod_s, mod_f;
26418 bool is_weak;
26420 boolval = operands[0];
26421 retval = operands[1];
26422 mem = operands[2];
26423 oldval = operands[3];
26424 newval = operands[4];
26425 is_weak = (INTVAL (operands[5]) != 0);
26426 mod_s = memmodel_base (INTVAL (operands[6]));
26427 mod_f = memmodel_base (INTVAL (operands[7]));
26428 orig_mode = mode = GET_MODE (mem);
26430 mask = shift = NULL_RTX;
26431 if (mode == QImode || mode == HImode)
26433 /* Before power8, we didn't have access to lbarx/lharx, so generate a
26434 lwarx and shift/mask operations. With power8, we need to do the
26435 comparison in SImode, but the store is still done in QI/HImode. */
26436 oldval = convert_modes (SImode, mode, oldval, 1);
26438 if (!TARGET_SYNC_HI_QI)
26440 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
26442 /* Shift and mask OLDVAL into position with the word. */
26443 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
26444 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26446 /* Shift and mask NEWVAL into position within the word. */
26447 newval = convert_modes (SImode, mode, newval, 1);
26448 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
26449 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26452 /* Prepare to adjust the return value. */
26453 retval = gen_reg_rtx (SImode);
26454 mode = SImode;
26456 else if (reg_overlap_mentioned_p (retval, oldval))
26457 oldval = copy_to_reg (oldval);
26459 if (mode != TImode && !reg_or_short_operand (oldval, mode))
26460 oldval = copy_to_mode_reg (mode, oldval);
26462 if (reg_overlap_mentioned_p (retval, newval))
26463 newval = copy_to_reg (newval);
26465 mem = rs6000_pre_atomic_barrier (mem, mod_s);
26467 label1 = NULL_RTX;
26468 if (!is_weak)
26470 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
26471 emit_label (XEXP (label1, 0));
26473 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
26475 emit_load_locked (mode, retval, mem);
26477 x = retval;
26478 if (mask)
26479 x = expand_simple_binop (SImode, AND, retval, mask,
26480 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26482 cond = gen_reg_rtx (CCmode);
26483 /* If we have TImode, synthesize a comparison. */
26484 if (mode != TImode)
26485 x = gen_rtx_COMPARE (CCmode, x, oldval);
26486 else
26488 rtx xor1_result = gen_reg_rtx (DImode);
26489 rtx xor2_result = gen_reg_rtx (DImode);
26490 rtx or_result = gen_reg_rtx (DImode);
26491 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
26492 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
26493 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
26494 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
26496 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
26497 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
26498 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
26499 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
26502 emit_insn (gen_rtx_SET (cond, x));
26504 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26505 emit_unlikely_jump (x, label2);
26507 x = newval;
26508 if (mask)
26509 x = rs6000_mask_atomic_subword (retval, newval, mask);
26511 emit_store_conditional (orig_mode, cond, mem, x);
26513 if (!is_weak)
26515 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26516 emit_unlikely_jump (x, label1);
26519 if (!is_mm_relaxed (mod_f))
26520 emit_label (XEXP (label2, 0));
26522 rs6000_post_atomic_barrier (mod_s);
26524 if (is_mm_relaxed (mod_f))
26525 emit_label (XEXP (label2, 0));
26527 if (shift)
26528 rs6000_finish_atomic_subword (operands[1], retval, shift);
26529 else if (mode != GET_MODE (operands[1]))
26530 convert_move (operands[1], retval, 1);
26532 /* In all cases, CR0 contains EQ on success, and NE on failure. */
26533 x = gen_rtx_EQ (SImode, cond, const0_rtx);
26534 emit_insn (gen_rtx_SET (boolval, x));
26537 /* Expand an atomic exchange operation. */
26539 void
26540 rs6000_expand_atomic_exchange (rtx operands[])
26542 rtx retval, mem, val, cond;
26543 machine_mode mode;
26544 enum memmodel model;
26545 rtx label, x, mask, shift;
26547 retval = operands[0];
26548 mem = operands[1];
26549 val = operands[2];
26550 model = memmodel_base (INTVAL (operands[3]));
26551 mode = GET_MODE (mem);
26553 mask = shift = NULL_RTX;
26554 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
26556 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
26558 /* Shift and mask VAL into position with the word. */
26559 val = convert_modes (SImode, mode, val, 1);
26560 val = expand_simple_binop (SImode, ASHIFT, val, shift,
26561 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26563 /* Prepare to adjust the return value. */
26564 retval = gen_reg_rtx (SImode);
26565 mode = SImode;
26568 mem = rs6000_pre_atomic_barrier (mem, model);
26570 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
26571 emit_label (XEXP (label, 0));
26573 emit_load_locked (mode, retval, mem);
26575 x = val;
26576 if (mask)
26577 x = rs6000_mask_atomic_subword (retval, val, mask);
26579 cond = gen_reg_rtx (CCmode);
26580 emit_store_conditional (mode, cond, mem, x);
26582 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26583 emit_unlikely_jump (x, label);
26585 rs6000_post_atomic_barrier (model);
26587 if (shift)
26588 rs6000_finish_atomic_subword (operands[0], retval, shift);
26591 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
26592 to perform. MEM is the memory on which to operate. VAL is the second
26593 operand of the binary operator. BEFORE and AFTER are optional locations to
26594 return the value of MEM either before of after the operation. MODEL_RTX
26595 is a CONST_INT containing the memory model to use. */
26597 void
26598 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
26599 rtx orig_before, rtx orig_after, rtx model_rtx)
26601 enum memmodel model = memmodel_base (INTVAL (model_rtx));
26602 machine_mode mode = GET_MODE (mem);
26603 machine_mode store_mode = mode;
26604 rtx label, x, cond, mask, shift;
26605 rtx before = orig_before, after = orig_after;
26607 mask = shift = NULL_RTX;
26608 /* On power8, we want to use SImode for the operation. On previous systems,
26609 use the operation in a subword and shift/mask to get the proper byte or
26610 halfword. */
26611 if (mode == QImode || mode == HImode)
26613 if (TARGET_SYNC_HI_QI)
26615 val = convert_modes (SImode, mode, val, 1);
26617 /* Prepare to adjust the return value. */
26618 before = gen_reg_rtx (SImode);
26619 if (after)
26620 after = gen_reg_rtx (SImode);
26621 mode = SImode;
26623 else
26625 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
26627 /* Shift and mask VAL into position with the word. */
26628 val = convert_modes (SImode, mode, val, 1);
26629 val = expand_simple_binop (SImode, ASHIFT, val, shift,
26630 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26632 switch (code)
26634 case IOR:
26635 case XOR:
26636 /* We've already zero-extended VAL. That is sufficient to
26637 make certain that it does not affect other bits. */
26638 mask = NULL;
26639 break;
26641 case AND:
26642 /* If we make certain that all of the other bits in VAL are
26643 set, that will be sufficient to not affect other bits. */
26644 x = gen_rtx_NOT (SImode, mask);
26645 x = gen_rtx_IOR (SImode, x, val);
26646 emit_insn (gen_rtx_SET (val, x));
26647 mask = NULL;
26648 break;
26650 case NOT:
26651 case PLUS:
26652 case MINUS:
26653 /* These will all affect bits outside the field and need
26654 adjustment via MASK within the loop. */
26655 break;
26657 default:
26658 gcc_unreachable ();
26661 /* Prepare to adjust the return value. */
26662 before = gen_reg_rtx (SImode);
26663 if (after)
26664 after = gen_reg_rtx (SImode);
26665 store_mode = mode = SImode;
26669 mem = rs6000_pre_atomic_barrier (mem, model);
26671 label = gen_label_rtx ();
26672 emit_label (label);
26673 label = gen_rtx_LABEL_REF (VOIDmode, label);
26675 if (before == NULL_RTX)
26676 before = gen_reg_rtx (mode);
26678 emit_load_locked (mode, before, mem);
26680 if (code == NOT)
26682 x = expand_simple_binop (mode, AND, before, val,
26683 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26684 after = expand_simple_unop (mode, NOT, x, after, 1);
26686 else
26688 after = expand_simple_binop (mode, code, before, val,
26689 after, 1, OPTAB_LIB_WIDEN);
26692 x = after;
26693 if (mask)
26695 x = expand_simple_binop (SImode, AND, after, mask,
26696 NULL_RTX, 1, OPTAB_LIB_WIDEN);
26697 x = rs6000_mask_atomic_subword (before, x, mask);
26699 else if (store_mode != mode)
26700 x = convert_modes (store_mode, mode, x, 1);
26702 cond = gen_reg_rtx (CCmode);
26703 emit_store_conditional (store_mode, cond, mem, x);
26705 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
26706 emit_unlikely_jump (x, label);
26708 rs6000_post_atomic_barrier (model);
26710 if (shift)
26712 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
26713 then do the calcuations in a SImode register. */
26714 if (orig_before)
26715 rs6000_finish_atomic_subword (orig_before, before, shift);
26716 if (orig_after)
26717 rs6000_finish_atomic_subword (orig_after, after, shift);
26719 else if (store_mode != mode)
26721 /* QImode/HImode on machines with lbarx/lharx where we do the native
26722 operation and then do the calcuations in a SImode register. */
26723 if (orig_before)
26724 convert_move (orig_before, before, 1);
26725 if (orig_after)
26726 convert_move (orig_after, after, 1);
26728 else if (orig_after && after != orig_after)
26729 emit_move_insn (orig_after, after);
26732 /* Emit instructions to move SRC to DST. Called by splitters for
26733 multi-register moves. It will emit at most one instruction for
26734 each register that is accessed; that is, it won't emit li/lis pairs
26735 (or equivalent for 64-bit code). One of SRC or DST must be a hard
26736 register. */
26738 void
26739 rs6000_split_multireg_move (rtx dst, rtx src)
26741 /* The register number of the first register being moved. */
26742 int reg;
26743 /* The mode that is to be moved. */
26744 machine_mode mode;
26745 /* The mode that the move is being done in, and its size. */
26746 machine_mode reg_mode;
26747 int reg_mode_size;
26748 /* The number of registers that will be moved. */
26749 int nregs;
26751 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
26752 mode = GET_MODE (dst);
26753 nregs = hard_regno_nregs[reg][mode];
26754 if (FP_REGNO_P (reg))
26755 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
26756 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
26757 else if (ALTIVEC_REGNO_P (reg))
26758 reg_mode = V16QImode;
26759 else if (TARGET_E500_DOUBLE && FLOAT128_2REG_P (mode))
26760 reg_mode = DFmode;
26761 else
26762 reg_mode = word_mode;
26763 reg_mode_size = GET_MODE_SIZE (reg_mode);
26765 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
26767 /* TDmode residing in FP registers is special, since the ISA requires that
26768 the lower-numbered word of a register pair is always the most significant
26769 word, even in little-endian mode. This does not match the usual subreg
26770 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
26771 the appropriate constituent registers "by hand" in little-endian mode.
26773 Note we do not need to check for destructive overlap here since TDmode
26774 can only reside in even/odd register pairs. */
26775 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
26777 rtx p_src, p_dst;
26778 int i;
26780 for (i = 0; i < nregs; i++)
26782 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
26783 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
26784 else
26785 p_src = simplify_gen_subreg (reg_mode, src, mode,
26786 i * reg_mode_size);
26788 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
26789 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
26790 else
26791 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
26792 i * reg_mode_size);
26794 emit_insn (gen_rtx_SET (p_dst, p_src));
26797 return;
26800 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
26802 /* Move register range backwards, if we might have destructive
26803 overlap. */
26804 int i;
26805 for (i = nregs - 1; i >= 0; i--)
26806 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
26807 i * reg_mode_size),
26808 simplify_gen_subreg (reg_mode, src, mode,
26809 i * reg_mode_size)));
26811 else
26813 int i;
26814 int j = -1;
26815 bool used_update = false;
26816 rtx restore_basereg = NULL_RTX;
26818 if (MEM_P (src) && INT_REGNO_P (reg))
26820 rtx breg;
26822 if (GET_CODE (XEXP (src, 0)) == PRE_INC
26823 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
26825 rtx delta_rtx;
26826 breg = XEXP (XEXP (src, 0), 0);
26827 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
26828 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
26829 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
26830 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
26831 src = replace_equiv_address (src, breg);
26833 else if (! rs6000_offsettable_memref_p (src, reg_mode))
26835 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
26837 rtx basereg = XEXP (XEXP (src, 0), 0);
26838 if (TARGET_UPDATE)
26840 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
26841 emit_insn (gen_rtx_SET (ndst,
26842 gen_rtx_MEM (reg_mode,
26843 XEXP (src, 0))));
26844 used_update = true;
26846 else
26847 emit_insn (gen_rtx_SET (basereg,
26848 XEXP (XEXP (src, 0), 1)));
26849 src = replace_equiv_address (src, basereg);
26851 else
26853 rtx basereg = gen_rtx_REG (Pmode, reg);
26854 emit_insn (gen_rtx_SET (basereg, XEXP (src, 0)));
26855 src = replace_equiv_address (src, basereg);
26859 breg = XEXP (src, 0);
26860 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
26861 breg = XEXP (breg, 0);
26863 /* If the base register we are using to address memory is
26864 also a destination reg, then change that register last. */
26865 if (REG_P (breg)
26866 && REGNO (breg) >= REGNO (dst)
26867 && REGNO (breg) < REGNO (dst) + nregs)
26868 j = REGNO (breg) - REGNO (dst);
26870 else if (MEM_P (dst) && INT_REGNO_P (reg))
26872 rtx breg;
26874 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
26875 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
26877 rtx delta_rtx;
26878 breg = XEXP (XEXP (dst, 0), 0);
26879 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
26880 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
26881 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
26883 /* We have to update the breg before doing the store.
26884 Use store with update, if available. */
26886 if (TARGET_UPDATE)
26888 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
26889 emit_insn (TARGET_32BIT
26890 ? (TARGET_POWERPC64
26891 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
26892 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
26893 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
26894 used_update = true;
26896 else
26897 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
26898 dst = replace_equiv_address (dst, breg);
26900 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
26901 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
26903 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
26905 rtx basereg = XEXP (XEXP (dst, 0), 0);
26906 if (TARGET_UPDATE)
26908 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
26909 emit_insn (gen_rtx_SET (gen_rtx_MEM (reg_mode,
26910 XEXP (dst, 0)),
26911 nsrc));
26912 used_update = true;
26914 else
26915 emit_insn (gen_rtx_SET (basereg,
26916 XEXP (XEXP (dst, 0), 1)));
26917 dst = replace_equiv_address (dst, basereg);
26919 else
26921 rtx basereg = XEXP (XEXP (dst, 0), 0);
26922 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
26923 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
26924 && REG_P (basereg)
26925 && REG_P (offsetreg)
26926 && REGNO (basereg) != REGNO (offsetreg));
26927 if (REGNO (basereg) == 0)
26929 rtx tmp = offsetreg;
26930 offsetreg = basereg;
26931 basereg = tmp;
26933 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
26934 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
26935 dst = replace_equiv_address (dst, basereg);
26938 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
26939 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
26942 for (i = 0; i < nregs; i++)
26944 /* Calculate index to next subword. */
26945 ++j;
26946 if (j == nregs)
26947 j = 0;
26949 /* If compiler already emitted move of first word by
26950 store with update, no need to do anything. */
26951 if (j == 0 && used_update)
26952 continue;
26954 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
26955 j * reg_mode_size),
26956 simplify_gen_subreg (reg_mode, src, mode,
26957 j * reg_mode_size)));
26959 if (restore_basereg != NULL_RTX)
26960 emit_insn (restore_basereg);
26965 /* This page contains routines that are used to determine what the
26966 function prologue and epilogue code will do and write them out. */
26968 static inline bool
26969 save_reg_p (int r)
26971 return !call_used_regs[r] && df_regs_ever_live_p (r);
26974 /* Determine whether the gp REG is really used. */
26976 static bool
26977 rs6000_reg_live_or_pic_offset_p (int reg)
26979 /* We need to mark the PIC offset register live for the same conditions
26980 as it is set up, or otherwise it won't be saved before we clobber it. */
26982 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
26984 if (TARGET_TOC && TARGET_MINIMAL_TOC
26985 && (crtl->calls_eh_return
26986 || df_regs_ever_live_p (reg)
26987 || !constant_pool_empty_p ()))
26988 return true;
26990 if ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
26991 && flag_pic)
26992 return true;
26995 /* If the function calls eh_return, claim used all the registers that would
26996 be checked for liveness otherwise. */
26998 return ((crtl->calls_eh_return || df_regs_ever_live_p (reg))
26999 && !call_used_regs[reg]);
27002 /* Return the first fixed-point register that is required to be
27003 saved. 32 if none. */
27006 first_reg_to_save (void)
27008 int first_reg;
27010 /* Find lowest numbered live register. */
27011 for (first_reg = 13; first_reg <= 31; first_reg++)
27012 if (save_reg_p (first_reg))
27013 break;
27015 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
27016 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
27017 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
27018 || (TARGET_TOC && TARGET_MINIMAL_TOC))
27019 && rs6000_reg_live_or_pic_offset_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
27020 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
27022 #if TARGET_MACHO
27023 if (flag_pic
27024 && crtl->uses_pic_offset_table
27025 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
27026 return RS6000_PIC_OFFSET_TABLE_REGNUM;
27027 #endif
27029 return first_reg;
27032 /* Similar, for FP regs. */
27035 first_fp_reg_to_save (void)
27037 int first_reg;
27039 /* Find lowest numbered live register. */
27040 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
27041 if (save_reg_p (first_reg))
27042 break;
27044 return first_reg;
27047 /* Similar, for AltiVec regs. */
27049 static int
27050 first_altivec_reg_to_save (void)
27052 int i;
27054 /* Stack frame remains as is unless we are in AltiVec ABI. */
27055 if (! TARGET_ALTIVEC_ABI)
27056 return LAST_ALTIVEC_REGNO + 1;
27058 /* On Darwin, the unwind routines are compiled without
27059 TARGET_ALTIVEC, and use save_world to save/restore the
27060 altivec registers when necessary. */
27061 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
27062 && ! TARGET_ALTIVEC)
27063 return FIRST_ALTIVEC_REGNO + 20;
27065 /* Find lowest numbered live register. */
27066 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
27067 if (save_reg_p (i))
27068 break;
27070 return i;
27073 /* Return a 32-bit mask of the AltiVec registers we need to set in
27074 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
27075 the 32-bit word is 0. */
27077 static unsigned int
27078 compute_vrsave_mask (void)
27080 unsigned int i, mask = 0;
27082 /* On Darwin, the unwind routines are compiled without
27083 TARGET_ALTIVEC, and use save_world to save/restore the
27084 call-saved altivec registers when necessary. */
27085 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
27086 && ! TARGET_ALTIVEC)
27087 mask |= 0xFFF;
27089 /* First, find out if we use _any_ altivec registers. */
27090 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
27091 if (df_regs_ever_live_p (i))
27092 mask |= ALTIVEC_REG_BIT (i);
27094 if (mask == 0)
27095 return mask;
27097 /* Next, remove the argument registers from the set. These must
27098 be in the VRSAVE mask set by the caller, so we don't need to add
27099 them in again. More importantly, the mask we compute here is
27100 used to generate CLOBBERs in the set_vrsave insn, and we do not
27101 wish the argument registers to die. */
27102 for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
27103 mask &= ~ALTIVEC_REG_BIT (i);
27105 /* Similarly, remove the return value from the set. */
27107 bool yes = false;
27108 diddle_return_value (is_altivec_return_reg, &yes);
27109 if (yes)
27110 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
27113 return mask;
27116 /* For a very restricted set of circumstances, we can cut down the
27117 size of prologues/epilogues by calling our own save/restore-the-world
27118 routines. */
27120 static void
27121 compute_save_world_info (rs6000_stack_t *info)
27123 info->world_save_p = 1;
27124 info->world_save_p
27125 = (WORLD_SAVE_P (info)
27126 && DEFAULT_ABI == ABI_DARWIN
27127 && !cfun->has_nonlocal_label
27128 && info->first_fp_reg_save == FIRST_SAVED_FP_REGNO
27129 && info->first_gp_reg_save == FIRST_SAVED_GP_REGNO
27130 && info->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
27131 && info->cr_save_p);
27133 /* This will not work in conjunction with sibcalls. Make sure there
27134 are none. (This check is expensive, but seldom executed.) */
27135 if (WORLD_SAVE_P (info))
27137 rtx_insn *insn;
27138 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
27139 if (CALL_P (insn) && SIBLING_CALL_P (insn))
27141 info->world_save_p = 0;
27142 break;
27146 if (WORLD_SAVE_P (info))
27148 /* Even if we're not touching VRsave, make sure there's room on the
27149 stack for it, if it looks like we're calling SAVE_WORLD, which
27150 will attempt to save it. */
27151 info->vrsave_size = 4;
27153 /* If we are going to save the world, we need to save the link register too. */
27154 info->lr_save_p = 1;
27156 /* "Save" the VRsave register too if we're saving the world. */
27157 if (info->vrsave_mask == 0)
27158 info->vrsave_mask = compute_vrsave_mask ();
27160 /* Because the Darwin register save/restore routines only handle
27161 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
27162 check. */
27163 gcc_assert (info->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
27164 && (info->first_altivec_reg_save
27165 >= FIRST_SAVED_ALTIVEC_REGNO));
27168 return;
27172 static void
27173 is_altivec_return_reg (rtx reg, void *xyes)
27175 bool *yes = (bool *) xyes;
27176 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
27177 *yes = true;
27181 /* Return whether REG is a global user reg or has been specifed by
27182 -ffixed-REG. We should not restore these, and so cannot use
27183 lmw or out-of-line restore functions if there are any. We also
27184 can't save them (well, emit frame notes for them), because frame
27185 unwinding during exception handling will restore saved registers. */
27187 static bool
27188 fixed_reg_p (int reg)
27190 /* Ignore fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] when the
27191 backend sets it, overriding anything the user might have given. */
27192 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
27193 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
27194 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
27195 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
27196 return false;
27198 return fixed_regs[reg];
27201 /* Determine the strategy for savings/restoring registers. */
27203 enum {
27204 SAVE_MULTIPLE = 0x1,
27205 SAVE_INLINE_GPRS = 0x2,
27206 SAVE_INLINE_FPRS = 0x4,
27207 SAVE_NOINLINE_GPRS_SAVES_LR = 0x8,
27208 SAVE_NOINLINE_FPRS_SAVES_LR = 0x10,
27209 SAVE_INLINE_VRS = 0x20,
27210 REST_MULTIPLE = 0x100,
27211 REST_INLINE_GPRS = 0x200,
27212 REST_INLINE_FPRS = 0x400,
27213 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x800,
27214 REST_INLINE_VRS = 0x1000
27217 static int
27218 rs6000_savres_strategy (rs6000_stack_t *info,
27219 bool using_static_chain_p)
27221 int strategy = 0;
27223 /* Select between in-line and out-of-line save and restore of regs.
27224 First, all the obvious cases where we don't use out-of-line. */
27225 if (crtl->calls_eh_return
27226 || cfun->machine->ra_need_lr)
27227 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
27228 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
27229 | SAVE_INLINE_VRS | REST_INLINE_VRS);
27231 if (info->first_gp_reg_save == 32)
27232 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27234 if (info->first_fp_reg_save == 64
27235 /* The out-of-line FP routines use double-precision stores;
27236 we can't use those routines if we don't have such stores. */
27237 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT))
27238 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27240 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
27241 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27243 /* Define cutoff for using out-of-line functions to save registers. */
27244 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
27246 if (!optimize_size)
27248 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27249 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27250 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27252 else
27254 /* Prefer out-of-line restore if it will exit. */
27255 if (info->first_fp_reg_save > 61)
27256 strategy |= SAVE_INLINE_FPRS;
27257 if (info->first_gp_reg_save > 29)
27259 if (info->first_fp_reg_save == 64)
27260 strategy |= SAVE_INLINE_GPRS;
27261 else
27262 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27264 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
27265 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27268 else if (DEFAULT_ABI == ABI_DARWIN)
27270 if (info->first_fp_reg_save > 60)
27271 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27272 if (info->first_gp_reg_save > 29)
27273 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27274 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27276 else
27278 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
27279 if ((flag_shrink_wrap_separate && optimize_function_for_speed_p (cfun))
27280 || info->first_fp_reg_save > 61)
27281 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
27282 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27283 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
27286 /* Don't bother to try to save things out-of-line if r11 is occupied
27287 by the static chain. It would require too much fiddling and the
27288 static chain is rarely used anyway. FPRs are saved w.r.t the stack
27289 pointer on Darwin, and AIX uses r1 or r12. */
27290 if (using_static_chain_p
27291 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
27292 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
27293 | SAVE_INLINE_GPRS
27294 | SAVE_INLINE_VRS);
27296 /* Saving CR interferes with the exit routines used on the SPE, so
27297 just punt here. */
27298 if (TARGET_SPE_ABI
27299 && info->spe_64bit_regs_used
27300 && info->cr_save_p)
27301 strategy |= REST_INLINE_GPRS;
27303 /* We can only use the out-of-line routines to restore fprs if we've
27304 saved all the registers from first_fp_reg_save in the prologue.
27305 Otherwise, we risk loading garbage. Of course, if we have saved
27306 out-of-line then we know we haven't skipped any fprs. */
27307 if ((strategy & SAVE_INLINE_FPRS)
27308 && !(strategy & REST_INLINE_FPRS))
27310 int i;
27312 for (i = info->first_fp_reg_save; i < 64; i++)
27313 if (fixed_regs[i] || !save_reg_p (i))
27315 strategy |= REST_INLINE_FPRS;
27316 break;
27320 /* Similarly, for altivec regs. */
27321 if ((strategy & SAVE_INLINE_VRS)
27322 && !(strategy & REST_INLINE_VRS))
27324 int i;
27326 for (i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
27327 if (fixed_regs[i] || !save_reg_p (i))
27329 strategy |= REST_INLINE_VRS;
27330 break;
27334 /* info->lr_save_p isn't yet set if the only reason lr needs to be
27335 saved is an out-of-line save or restore. Set up the value for
27336 the next test (excluding out-of-line gprs). */
27337 bool lr_save_p = (info->lr_save_p
27338 || !(strategy & SAVE_INLINE_FPRS)
27339 || !(strategy & SAVE_INLINE_VRS)
27340 || !(strategy & REST_INLINE_FPRS)
27341 || !(strategy & REST_INLINE_VRS));
27343 if (TARGET_MULTIPLE
27344 && !TARGET_POWERPC64
27345 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
27346 && info->first_gp_reg_save < 31
27347 && !(flag_shrink_wrap
27348 && flag_shrink_wrap_separate
27349 && optimize_function_for_speed_p (cfun)))
27351 /* Prefer store multiple for saves over out-of-line routines,
27352 since the store-multiple instruction will always be smaller. */
27353 strategy |= SAVE_INLINE_GPRS | SAVE_MULTIPLE;
27355 /* The situation is more complicated with load multiple. We'd
27356 prefer to use the out-of-line routines for restores, since the
27357 "exit" out-of-line routines can handle the restore of LR and the
27358 frame teardown. However if doesn't make sense to use the
27359 out-of-line routine if that is the only reason we'd need to save
27360 LR, and we can't use the "exit" out-of-line gpr restore if we
27361 have saved some fprs; In those cases it is advantageous to use
27362 load multiple when available. */
27363 if (info->first_fp_reg_save != 64 || !lr_save_p)
27364 strategy |= REST_INLINE_GPRS | REST_MULTIPLE;
27367 /* Using the "exit" out-of-line routine does not improve code size
27368 if using it would require lr to be saved and if only saving one
27369 or two gprs. */
27370 else if (!lr_save_p && info->first_gp_reg_save > 29)
27371 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
27373 /* We can only use load multiple or the out-of-line routines to
27374 restore gprs if we've saved all the registers from
27375 first_gp_reg_save. Otherwise, we risk loading garbage.
27376 Of course, if we have saved out-of-line or used stmw then we know
27377 we haven't skipped any gprs. */
27378 if ((strategy & (SAVE_INLINE_GPRS | SAVE_MULTIPLE)) == SAVE_INLINE_GPRS
27379 && (strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
27381 int i;
27383 for (i = info->first_gp_reg_save; i < 32; i++)
27384 if (fixed_reg_p (i) || !save_reg_p (i))
27386 strategy |= REST_INLINE_GPRS;
27387 strategy &= ~REST_MULTIPLE;
27388 break;
27392 if (TARGET_ELF && TARGET_64BIT)
27394 if (!(strategy & SAVE_INLINE_FPRS))
27395 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
27396 else if (!(strategy & SAVE_INLINE_GPRS)
27397 && info->first_fp_reg_save == 64)
27398 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
27400 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
27401 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
27403 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
27404 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
27406 return strategy;
27409 /* Calculate the stack information for the current function. This is
27410 complicated by having two separate calling sequences, the AIX calling
27411 sequence and the V.4 calling sequence.
27413 AIX (and Darwin/Mac OS X) stack frames look like:
27414 32-bit 64-bit
27415 SP----> +---------------------------------------+
27416 | back chain to caller | 0 0
27417 +---------------------------------------+
27418 | saved CR | 4 8 (8-11)
27419 +---------------------------------------+
27420 | saved LR | 8 16
27421 +---------------------------------------+
27422 | reserved for compilers | 12 24
27423 +---------------------------------------+
27424 | reserved for binders | 16 32
27425 +---------------------------------------+
27426 | saved TOC pointer | 20 40
27427 +---------------------------------------+
27428 | Parameter save area (+padding*) (P) | 24 48
27429 +---------------------------------------+
27430 | Alloca space (A) | 24+P etc.
27431 +---------------------------------------+
27432 | Local variable space (L) | 24+P+A
27433 +---------------------------------------+
27434 | Float/int conversion temporary (X) | 24+P+A+L
27435 +---------------------------------------+
27436 | Save area for AltiVec registers (W) | 24+P+A+L+X
27437 +---------------------------------------+
27438 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
27439 +---------------------------------------+
27440 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
27441 +---------------------------------------+
27442 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
27443 +---------------------------------------+
27444 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
27445 +---------------------------------------+
27446 old SP->| back chain to caller's caller |
27447 +---------------------------------------+
27449 * If the alloca area is present, the parameter save area is
27450 padded so that the former starts 16-byte aligned.
27452 The required alignment for AIX configurations is two words (i.e., 8
27453 or 16 bytes).
27455 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
27457 SP----> +---------------------------------------+
27458 | Back chain to caller | 0
27459 +---------------------------------------+
27460 | Save area for CR | 8
27461 +---------------------------------------+
27462 | Saved LR | 16
27463 +---------------------------------------+
27464 | Saved TOC pointer | 24
27465 +---------------------------------------+
27466 | Parameter save area (+padding*) (P) | 32
27467 +---------------------------------------+
27468 | Alloca space (A) | 32+P
27469 +---------------------------------------+
27470 | Local variable space (L) | 32+P+A
27471 +---------------------------------------+
27472 | Save area for AltiVec registers (W) | 32+P+A+L
27473 +---------------------------------------+
27474 | AltiVec alignment padding (Y) | 32+P+A+L+W
27475 +---------------------------------------+
27476 | Save area for GP registers (G) | 32+P+A+L+W+Y
27477 +---------------------------------------+
27478 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
27479 +---------------------------------------+
27480 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
27481 +---------------------------------------+
27483 * If the alloca area is present, the parameter save area is
27484 padded so that the former starts 16-byte aligned.
27486 V.4 stack frames look like:
27488 SP----> +---------------------------------------+
27489 | back chain to caller | 0
27490 +---------------------------------------+
27491 | caller's saved LR | 4
27492 +---------------------------------------+
27493 | Parameter save area (+padding*) (P) | 8
27494 +---------------------------------------+
27495 | Alloca space (A) | 8+P
27496 +---------------------------------------+
27497 | Varargs save area (V) | 8+P+A
27498 +---------------------------------------+
27499 | Local variable space (L) | 8+P+A+V
27500 +---------------------------------------+
27501 | Float/int conversion temporary (X) | 8+P+A+V+L
27502 +---------------------------------------+
27503 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
27504 +---------------------------------------+
27505 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
27506 +---------------------------------------+
27507 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
27508 +---------------------------------------+
27509 | SPE: area for 64-bit GP registers |
27510 +---------------------------------------+
27511 | SPE alignment padding |
27512 +---------------------------------------+
27513 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
27514 +---------------------------------------+
27515 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
27516 +---------------------------------------+
27517 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
27518 +---------------------------------------+
27519 old SP->| back chain to caller's caller |
27520 +---------------------------------------+
27522 * If the alloca area is present and the required alignment is
27523 16 bytes, the parameter save area is padded so that the
27524 alloca area starts 16-byte aligned.
27526 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
27527 given. (But note below and in sysv4.h that we require only 8 and
27528 may round up the size of our stack frame anyways. The historical
27529 reason is early versions of powerpc-linux which didn't properly
27530 align the stack at program startup. A happy side-effect is that
27531 -mno-eabi libraries can be used with -meabi programs.)
27533 The EABI configuration defaults to the V.4 layout. However,
27534 the stack alignment requirements may differ. If -mno-eabi is not
27535 given, the required stack alignment is 8 bytes; if -mno-eabi is
27536 given, the required alignment is 16 bytes. (But see V.4 comment
27537 above.) */
27539 #ifndef ABI_STACK_BOUNDARY
27540 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
27541 #endif
27543 static rs6000_stack_t *
27544 rs6000_stack_info (void)
27546 /* We should never be called for thunks, we are not set up for that. */
27547 gcc_assert (!cfun->is_thunk);
27549 rs6000_stack_t *info = &stack_info;
27550 int reg_size = TARGET_32BIT ? 4 : 8;
27551 int ehrd_size;
27552 int ehcr_size;
27553 int save_align;
27554 int first_gp;
27555 HOST_WIDE_INT non_fixed_size;
27556 bool using_static_chain_p;
27558 if (reload_completed && info->reload_completed)
27559 return info;
27561 memset (info, 0, sizeof (*info));
27562 info->reload_completed = reload_completed;
27564 if (TARGET_SPE)
27566 /* Cache value so we don't rescan instruction chain over and over. */
27567 if (cfun->machine->spe_insn_chain_scanned_p == 0)
27568 cfun->machine->spe_insn_chain_scanned_p
27569 = spe_func_has_64bit_regs_p () + 1;
27570 info->spe_64bit_regs_used = cfun->machine->spe_insn_chain_scanned_p - 1;
27573 /* Select which calling sequence. */
27574 info->abi = DEFAULT_ABI;
27576 /* Calculate which registers need to be saved & save area size. */
27577 info->first_gp_reg_save = first_reg_to_save ();
27578 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
27579 even if it currently looks like we won't. Reload may need it to
27580 get at a constant; if so, it will have already created a constant
27581 pool entry for it. */
27582 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
27583 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
27584 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
27585 && crtl->uses_const_pool
27586 && info->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
27587 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
27588 else
27589 first_gp = info->first_gp_reg_save;
27591 info->gp_size = reg_size * (32 - first_gp);
27593 /* For the SPE, we have an additional upper 32-bits on each GPR.
27594 Ideally we should save the entire 64-bits only when the upper
27595 half is used in SIMD instructions. Since we only record
27596 registers live (not the size they are used in), this proves
27597 difficult because we'd have to traverse the instruction chain at
27598 the right time, taking reload into account. This is a real pain,
27599 so we opt to save the GPRs in 64-bits always if but one register
27600 gets used in 64-bits. Otherwise, all the registers in the frame
27601 get saved in 32-bits.
27603 So... since when we save all GPRs (except the SP) in 64-bits, the
27604 traditional GP save area will be empty. */
27605 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
27606 info->gp_size = 0;
27608 info->first_fp_reg_save = first_fp_reg_to_save ();
27609 info->fp_size = 8 * (64 - info->first_fp_reg_save);
27611 info->first_altivec_reg_save = first_altivec_reg_to_save ();
27612 info->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
27613 - info->first_altivec_reg_save);
27615 /* Does this function call anything? */
27616 info->calls_p = (!crtl->is_leaf || cfun->machine->ra_needs_full_frame);
27618 /* Determine if we need to save the condition code registers. */
27619 if (save_reg_p (CR2_REGNO)
27620 || save_reg_p (CR3_REGNO)
27621 || save_reg_p (CR4_REGNO))
27623 info->cr_save_p = 1;
27624 if (DEFAULT_ABI == ABI_V4)
27625 info->cr_size = reg_size;
27628 /* If the current function calls __builtin_eh_return, then we need
27629 to allocate stack space for registers that will hold data for
27630 the exception handler. */
27631 if (crtl->calls_eh_return)
27633 unsigned int i;
27634 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
27635 continue;
27637 /* SPE saves EH registers in 64-bits. */
27638 ehrd_size = i * (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0
27639 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
27641 else
27642 ehrd_size = 0;
27644 /* In the ELFv2 ABI, we also need to allocate space for separate
27645 CR field save areas if the function calls __builtin_eh_return. */
27646 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
27648 /* This hard-codes that we have three call-saved CR fields. */
27649 ehcr_size = 3 * reg_size;
27650 /* We do *not* use the regular CR save mechanism. */
27651 info->cr_save_p = 0;
27653 else
27654 ehcr_size = 0;
27656 /* Determine various sizes. */
27657 info->reg_size = reg_size;
27658 info->fixed_size = RS6000_SAVE_AREA;
27659 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
27660 if (cfun->calls_alloca)
27661 info->parm_size =
27662 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
27663 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
27664 else
27665 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
27666 TARGET_ALTIVEC ? 16 : 8);
27667 if (FRAME_GROWS_DOWNWARD)
27668 info->vars_size
27669 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
27670 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
27671 - (info->fixed_size + info->vars_size + info->parm_size);
27673 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
27674 info->spe_gp_size = 8 * (32 - first_gp);
27676 if (TARGET_ALTIVEC_ABI)
27677 info->vrsave_mask = compute_vrsave_mask ();
27679 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
27680 info->vrsave_size = 4;
27682 compute_save_world_info (info);
27684 /* Calculate the offsets. */
27685 switch (DEFAULT_ABI)
27687 case ABI_NONE:
27688 default:
27689 gcc_unreachable ();
27691 case ABI_AIX:
27692 case ABI_ELFv2:
27693 case ABI_DARWIN:
27694 info->fp_save_offset = -info->fp_size;
27695 info->gp_save_offset = info->fp_save_offset - info->gp_size;
27697 if (TARGET_ALTIVEC_ABI)
27699 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
27701 /* Align stack so vector save area is on a quadword boundary.
27702 The padding goes above the vectors. */
27703 if (info->altivec_size != 0)
27704 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
27706 info->altivec_save_offset = info->vrsave_save_offset
27707 - info->altivec_padding_size
27708 - info->altivec_size;
27709 gcc_assert (info->altivec_size == 0
27710 || info->altivec_save_offset % 16 == 0);
27712 /* Adjust for AltiVec case. */
27713 info->ehrd_offset = info->altivec_save_offset - ehrd_size;
27715 else
27716 info->ehrd_offset = info->gp_save_offset - ehrd_size;
27718 info->ehcr_offset = info->ehrd_offset - ehcr_size;
27719 info->cr_save_offset = reg_size; /* first word when 64-bit. */
27720 info->lr_save_offset = 2*reg_size;
27721 break;
27723 case ABI_V4:
27724 info->fp_save_offset = -info->fp_size;
27725 info->gp_save_offset = info->fp_save_offset - info->gp_size;
27726 info->cr_save_offset = info->gp_save_offset - info->cr_size;
27728 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
27730 /* Align stack so SPE GPR save area is aligned on a
27731 double-word boundary. */
27732 if (info->spe_gp_size != 0 && info->cr_save_offset != 0)
27733 info->spe_padding_size = 8 - (-info->cr_save_offset % 8);
27734 else
27735 info->spe_padding_size = 0;
27737 info->spe_gp_save_offset = info->cr_save_offset
27738 - info->spe_padding_size
27739 - info->spe_gp_size;
27741 /* Adjust for SPE case. */
27742 info->ehrd_offset = info->spe_gp_save_offset;
27744 else if (TARGET_ALTIVEC_ABI)
27746 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
27748 /* Align stack so vector save area is on a quadword boundary. */
27749 if (info->altivec_size != 0)
27750 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
27752 info->altivec_save_offset = info->vrsave_save_offset
27753 - info->altivec_padding_size
27754 - info->altivec_size;
27756 /* Adjust for AltiVec case. */
27757 info->ehrd_offset = info->altivec_save_offset;
27759 else
27760 info->ehrd_offset = info->cr_save_offset;
27762 info->ehrd_offset -= ehrd_size;
27763 info->lr_save_offset = reg_size;
27766 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
27767 info->save_size = RS6000_ALIGN (info->fp_size
27768 + info->gp_size
27769 + info->altivec_size
27770 + info->altivec_padding_size
27771 + info->spe_gp_size
27772 + info->spe_padding_size
27773 + ehrd_size
27774 + ehcr_size
27775 + info->cr_size
27776 + info->vrsave_size,
27777 save_align);
27779 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
27781 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
27782 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
27784 /* Determine if we need to save the link register. */
27785 if (info->calls_p
27786 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
27787 && crtl->profile
27788 && !TARGET_PROFILE_KERNEL)
27789 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
27790 #ifdef TARGET_RELOCATABLE
27791 || (DEFAULT_ABI == ABI_V4
27792 && (TARGET_RELOCATABLE || flag_pic > 1)
27793 && !constant_pool_empty_p ())
27794 #endif
27795 || rs6000_ra_ever_killed ())
27796 info->lr_save_p = 1;
27798 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
27799 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
27800 && call_used_regs[STATIC_CHAIN_REGNUM]);
27801 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
27803 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
27804 || !(info->savres_strategy & SAVE_INLINE_FPRS)
27805 || !(info->savres_strategy & SAVE_INLINE_VRS)
27806 || !(info->savres_strategy & REST_INLINE_GPRS)
27807 || !(info->savres_strategy & REST_INLINE_FPRS)
27808 || !(info->savres_strategy & REST_INLINE_VRS))
27809 info->lr_save_p = 1;
27811 if (info->lr_save_p)
27812 df_set_regs_ever_live (LR_REGNO, true);
27814 /* Determine if we need to allocate any stack frame:
27816 For AIX we need to push the stack if a frame pointer is needed
27817 (because the stack might be dynamically adjusted), if we are
27818 debugging, if we make calls, or if the sum of fp_save, gp_save,
27819 and local variables are more than the space needed to save all
27820 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
27821 + 18*8 = 288 (GPR13 reserved).
27823 For V.4 we don't have the stack cushion that AIX uses, but assume
27824 that the debugger can handle stackless frames. */
27826 if (info->calls_p)
27827 info->push_p = 1;
27829 else if (DEFAULT_ABI == ABI_V4)
27830 info->push_p = non_fixed_size != 0;
27832 else if (frame_pointer_needed)
27833 info->push_p = 1;
27835 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
27836 info->push_p = 1;
27838 else
27839 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
27841 return info;
27844 /* Return true if the current function uses any GPRs in 64-bit SIMD
27845 mode. */
27847 static bool
27848 spe_func_has_64bit_regs_p (void)
27850 rtx_insn *insns, *insn;
27852 /* Functions that save and restore all the call-saved registers will
27853 need to save/restore the registers in 64-bits. */
27854 if (crtl->calls_eh_return
27855 || cfun->calls_setjmp
27856 || crtl->has_nonlocal_goto)
27857 return true;
27859 insns = get_insns ();
27861 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
27863 if (INSN_P (insn))
27865 rtx i;
27867 /* FIXME: This should be implemented with attributes...
27869 (set_attr "spe64" "true")....then,
27870 if (get_spe64(insn)) return true;
27872 It's the only reliable way to do the stuff below. */
27874 i = PATTERN (insn);
27875 if (GET_CODE (i) == SET)
27877 machine_mode mode = GET_MODE (SET_SRC (i));
27879 if (SPE_VECTOR_MODE (mode))
27880 return true;
27881 if (TARGET_E500_DOUBLE
27882 && (mode == DFmode || FLOAT128_2REG_P (mode)))
27883 return true;
27888 return false;
27891 static void
27892 debug_stack_info (rs6000_stack_t *info)
27894 const char *abi_string;
27896 if (! info)
27897 info = rs6000_stack_info ();
27899 fprintf (stderr, "\nStack information for function %s:\n",
27900 ((current_function_decl && DECL_NAME (current_function_decl))
27901 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
27902 : "<unknown>"));
27904 switch (info->abi)
27906 default: abi_string = "Unknown"; break;
27907 case ABI_NONE: abi_string = "NONE"; break;
27908 case ABI_AIX: abi_string = "AIX"; break;
27909 case ABI_ELFv2: abi_string = "ELFv2"; break;
27910 case ABI_DARWIN: abi_string = "Darwin"; break;
27911 case ABI_V4: abi_string = "V.4"; break;
27914 fprintf (stderr, "\tABI = %5s\n", abi_string);
27916 if (TARGET_ALTIVEC_ABI)
27917 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
27919 if (TARGET_SPE_ABI)
27920 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
27922 if (info->first_gp_reg_save != 32)
27923 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
27925 if (info->first_fp_reg_save != 64)
27926 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
27928 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
27929 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
27930 info->first_altivec_reg_save);
27932 if (info->lr_save_p)
27933 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
27935 if (info->cr_save_p)
27936 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
27938 if (info->vrsave_mask)
27939 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
27941 if (info->push_p)
27942 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
27944 if (info->calls_p)
27945 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
27947 if (info->gp_size)
27948 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
27950 if (info->fp_size)
27951 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
27953 if (info->altivec_size)
27954 fprintf (stderr, "\taltivec_save_offset = %5d\n",
27955 info->altivec_save_offset);
27957 if (info->spe_gp_size)
27958 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
27959 info->spe_gp_save_offset);
27961 if (info->vrsave_size)
27962 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
27963 info->vrsave_save_offset);
27965 if (info->lr_save_p)
27966 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
27968 if (info->cr_save_p)
27969 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
27971 if (info->varargs_save_offset)
27972 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
27974 if (info->total_size)
27975 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
27976 info->total_size);
27978 if (info->vars_size)
27979 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
27980 info->vars_size);
27982 if (info->parm_size)
27983 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
27985 if (info->fixed_size)
27986 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
27988 if (info->gp_size)
27989 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
27991 if (info->spe_gp_size)
27992 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
27994 if (info->fp_size)
27995 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
27997 if (info->altivec_size)
27998 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
28000 if (info->vrsave_size)
28001 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
28003 if (info->altivec_padding_size)
28004 fprintf (stderr, "\taltivec_padding_size= %5d\n",
28005 info->altivec_padding_size);
28007 if (info->spe_padding_size)
28008 fprintf (stderr, "\tspe_padding_size = %5d\n",
28009 info->spe_padding_size);
28011 if (info->cr_size)
28012 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
28014 if (info->save_size)
28015 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
28017 if (info->reg_size != 4)
28018 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
28020 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
28022 fprintf (stderr, "\n");
28026 rs6000_return_addr (int count, rtx frame)
28028 /* Currently we don't optimize very well between prolog and body
28029 code and for PIC code the code can be actually quite bad, so
28030 don't try to be too clever here. */
28031 if (count != 0
28032 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
28034 cfun->machine->ra_needs_full_frame = 1;
28036 return
28037 gen_rtx_MEM
28038 (Pmode,
28039 memory_address
28040 (Pmode,
28041 plus_constant (Pmode,
28042 copy_to_reg
28043 (gen_rtx_MEM (Pmode,
28044 memory_address (Pmode, frame))),
28045 RETURN_ADDRESS_OFFSET)));
28048 cfun->machine->ra_need_lr = 1;
28049 return get_hard_reg_initial_val (Pmode, LR_REGNO);
28052 /* Say whether a function is a candidate for sibcall handling or not. */
28054 static bool
28055 rs6000_function_ok_for_sibcall (tree decl, tree exp)
28057 tree fntype;
28059 if (decl)
28060 fntype = TREE_TYPE (decl);
28061 else
28062 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
28064 /* We can't do it if the called function has more vector parameters
28065 than the current function; there's nowhere to put the VRsave code. */
28066 if (TARGET_ALTIVEC_ABI
28067 && TARGET_ALTIVEC_VRSAVE
28068 && !(decl && decl == current_function_decl))
28070 function_args_iterator args_iter;
28071 tree type;
28072 int nvreg = 0;
28074 /* Functions with vector parameters are required to have a
28075 prototype, so the argument type info must be available
28076 here. */
28077 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
28078 if (TREE_CODE (type) == VECTOR_TYPE
28079 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
28080 nvreg++;
28082 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
28083 if (TREE_CODE (type) == VECTOR_TYPE
28084 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
28085 nvreg--;
28087 if (nvreg > 0)
28088 return false;
28091 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
28092 functions, because the callee may have a different TOC pointer to
28093 the caller and there's no way to ensure we restore the TOC when
28094 we return. With the secure-plt SYSV ABI we can't make non-local
28095 calls when -fpic/PIC because the plt call stubs use r30. */
28096 if (DEFAULT_ABI == ABI_DARWIN
28097 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28098 && decl
28099 && !DECL_EXTERNAL (decl)
28100 && !DECL_WEAK (decl)
28101 && (*targetm.binds_local_p) (decl))
28102 || (DEFAULT_ABI == ABI_V4
28103 && (!TARGET_SECURE_PLT
28104 || !flag_pic
28105 || (decl
28106 && (*targetm.binds_local_p) (decl)))))
28108 tree attr_list = TYPE_ATTRIBUTES (fntype);
28110 if (!lookup_attribute ("longcall", attr_list)
28111 || lookup_attribute ("shortcall", attr_list))
28112 return true;
28115 return false;
28118 static int
28119 rs6000_ra_ever_killed (void)
28121 rtx_insn *top;
28122 rtx reg;
28123 rtx_insn *insn;
28125 if (cfun->is_thunk)
28126 return 0;
28128 if (cfun->machine->lr_save_state)
28129 return cfun->machine->lr_save_state - 1;
28131 /* regs_ever_live has LR marked as used if any sibcalls are present,
28132 but this should not force saving and restoring in the
28133 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
28134 clobbers LR, so that is inappropriate. */
28136 /* Also, the prologue can generate a store into LR that
28137 doesn't really count, like this:
28139 move LR->R0
28140 bcl to set PIC register
28141 move LR->R31
28142 move R0->LR
28144 When we're called from the epilogue, we need to avoid counting
28145 this as a store. */
28147 push_topmost_sequence ();
28148 top = get_insns ();
28149 pop_topmost_sequence ();
28150 reg = gen_rtx_REG (Pmode, LR_REGNO);
28152 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
28154 if (INSN_P (insn))
28156 if (CALL_P (insn))
28158 if (!SIBLING_CALL_P (insn))
28159 return 1;
28161 else if (find_regno_note (insn, REG_INC, LR_REGNO))
28162 return 1;
28163 else if (set_of (reg, insn) != NULL_RTX
28164 && !prologue_epilogue_contains (insn))
28165 return 1;
28168 return 0;
28171 /* Emit instructions needed to load the TOC register.
28172 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
28173 a constant pool; or for SVR4 -fpic. */
28175 void
28176 rs6000_emit_load_toc_table (int fromprolog)
28178 rtx dest;
28179 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
28181 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
28183 char buf[30];
28184 rtx lab, tmp1, tmp2, got;
28186 lab = gen_label_rtx ();
28187 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
28188 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
28189 if (flag_pic == 2)
28191 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
28192 need_toc_init = 1;
28194 else
28195 got = rs6000_got_sym ();
28196 tmp1 = tmp2 = dest;
28197 if (!fromprolog)
28199 tmp1 = gen_reg_rtx (Pmode);
28200 tmp2 = gen_reg_rtx (Pmode);
28202 emit_insn (gen_load_toc_v4_PIC_1 (lab));
28203 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
28204 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
28205 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
28207 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
28209 emit_insn (gen_load_toc_v4_pic_si ());
28210 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
28212 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
28214 char buf[30];
28215 rtx temp0 = (fromprolog
28216 ? gen_rtx_REG (Pmode, 0)
28217 : gen_reg_rtx (Pmode));
28219 if (fromprolog)
28221 rtx symF, symL;
28223 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
28224 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
28226 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
28227 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
28229 emit_insn (gen_load_toc_v4_PIC_1 (symF));
28230 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
28231 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
28233 else
28235 rtx tocsym, lab;
28237 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
28238 need_toc_init = 1;
28239 lab = gen_label_rtx ();
28240 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
28241 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
28242 if (TARGET_LINK_STACK)
28243 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
28244 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
28246 emit_insn (gen_addsi3 (dest, temp0, dest));
28248 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
28250 /* This is for AIX code running in non-PIC ELF32. */
28251 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
28253 need_toc_init = 1;
28254 emit_insn (gen_elf_high (dest, realsym));
28255 emit_insn (gen_elf_low (dest, dest, realsym));
28257 else
28259 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
28261 if (TARGET_32BIT)
28262 emit_insn (gen_load_toc_aix_si (dest));
28263 else
28264 emit_insn (gen_load_toc_aix_di (dest));
28268 /* Emit instructions to restore the link register after determining where
28269 its value has been stored. */
28271 void
28272 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
28274 rs6000_stack_t *info = rs6000_stack_info ();
28275 rtx operands[2];
28277 operands[0] = source;
28278 operands[1] = scratch;
28280 if (info->lr_save_p)
28282 rtx frame_rtx = stack_pointer_rtx;
28283 HOST_WIDE_INT sp_offset = 0;
28284 rtx tmp;
28286 if (frame_pointer_needed
28287 || cfun->calls_alloca
28288 || info->total_size > 32767)
28290 tmp = gen_frame_mem (Pmode, frame_rtx);
28291 emit_move_insn (operands[1], tmp);
28292 frame_rtx = operands[1];
28294 else if (info->push_p)
28295 sp_offset = info->total_size;
28297 tmp = plus_constant (Pmode, frame_rtx,
28298 info->lr_save_offset + sp_offset);
28299 tmp = gen_frame_mem (Pmode, tmp);
28300 emit_move_insn (tmp, operands[0]);
28302 else
28303 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
28305 /* Freeze lr_save_p. We've just emitted rtl that depends on the
28306 state of lr_save_p so any change from here on would be a bug. In
28307 particular, stop rs6000_ra_ever_killed from considering the SET
28308 of lr we may have added just above. */
28309 cfun->machine->lr_save_state = info->lr_save_p + 1;
28312 static GTY(()) alias_set_type set = -1;
28314 alias_set_type
28315 get_TOC_alias_set (void)
28317 if (set == -1)
28318 set = new_alias_set ();
28319 return set;
28322 /* This returns nonzero if the current function uses the TOC. This is
28323 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
28324 is generated by the ABI_V4 load_toc_* patterns. */
28325 #if TARGET_ELF
28326 static int
28327 uses_TOC (void)
28329 rtx_insn *insn;
28331 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
28332 if (INSN_P (insn))
28334 rtx pat = PATTERN (insn);
28335 int i;
28337 if (GET_CODE (pat) == PARALLEL)
28338 for (i = 0; i < XVECLEN (pat, 0); i++)
28340 rtx sub = XVECEXP (pat, 0, i);
28341 if (GET_CODE (sub) == USE)
28343 sub = XEXP (sub, 0);
28344 if (GET_CODE (sub) == UNSPEC
28345 && XINT (sub, 1) == UNSPEC_TOC)
28346 return 1;
28350 return 0;
28352 #endif
28355 create_TOC_reference (rtx symbol, rtx largetoc_reg)
28357 rtx tocrel, tocreg, hi;
28359 if (TARGET_DEBUG_ADDR)
28361 if (GET_CODE (symbol) == SYMBOL_REF)
28362 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
28363 XSTR (symbol, 0));
28364 else
28366 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
28367 GET_RTX_NAME (GET_CODE (symbol)));
28368 debug_rtx (symbol);
28372 if (!can_create_pseudo_p ())
28373 df_set_regs_ever_live (TOC_REGISTER, true);
28375 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
28376 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
28377 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
28378 return tocrel;
28380 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
28381 if (largetoc_reg != NULL)
28383 emit_move_insn (largetoc_reg, hi);
28384 hi = largetoc_reg;
28386 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
28389 /* Issue assembly directives that create a reference to the given DWARF
28390 FRAME_TABLE_LABEL from the current function section. */
28391 void
28392 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
28394 fprintf (asm_out_file, "\t.ref %s\n",
28395 (* targetm.strip_name_encoding) (frame_table_label));
28398 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
28399 and the change to the stack pointer. */
28401 static void
28402 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
28404 rtvec p;
28405 int i;
28406 rtx regs[3];
28408 i = 0;
28409 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28410 if (hard_frame_needed)
28411 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
28412 if (!(REGNO (fp) == STACK_POINTER_REGNUM
28413 || (hard_frame_needed
28414 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
28415 regs[i++] = fp;
28417 p = rtvec_alloc (i);
28418 while (--i >= 0)
28420 rtx mem = gen_frame_mem (BLKmode, regs[i]);
28421 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
28424 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
28427 /* Emit the correct code for allocating stack space, as insns.
28428 If COPY_REG, make sure a copy of the old frame is left there.
28429 The generated code may use hard register 0 as a temporary. */
28431 static rtx_insn *
28432 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
28434 rtx_insn *insn;
28435 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28436 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
28437 rtx todec = gen_int_mode (-size, Pmode);
28438 rtx par, set, mem;
28440 if (INTVAL (todec) != -size)
28442 warning (0, "stack frame too large");
28443 emit_insn (gen_trap ());
28444 return 0;
28447 if (crtl->limit_stack)
28449 if (REG_P (stack_limit_rtx)
28450 && REGNO (stack_limit_rtx) > 1
28451 && REGNO (stack_limit_rtx) <= 31)
28453 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
28454 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
28455 const0_rtx));
28457 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
28458 && TARGET_32BIT
28459 && DEFAULT_ABI == ABI_V4
28460 && !flag_pic)
28462 rtx toload = gen_rtx_CONST (VOIDmode,
28463 gen_rtx_PLUS (Pmode,
28464 stack_limit_rtx,
28465 GEN_INT (size)));
28467 emit_insn (gen_elf_high (tmp_reg, toload));
28468 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
28469 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
28470 const0_rtx));
28472 else
28473 warning (0, "stack limit expression is not supported");
28476 if (copy_reg)
28478 if (copy_off != 0)
28479 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
28480 else
28481 emit_move_insn (copy_reg, stack_reg);
28484 if (size > 32767)
28486 /* Need a note here so that try_split doesn't get confused. */
28487 if (get_last_insn () == NULL_RTX)
28488 emit_note (NOTE_INSN_DELETED);
28489 insn = emit_move_insn (tmp_reg, todec);
28490 try_split (PATTERN (insn), insn, 0);
28491 todec = tmp_reg;
28494 insn = emit_insn (TARGET_32BIT
28495 ? gen_movsi_update_stack (stack_reg, stack_reg,
28496 todec, stack_reg)
28497 : gen_movdi_di_update_stack (stack_reg, stack_reg,
28498 todec, stack_reg));
28499 /* Since we didn't use gen_frame_mem to generate the MEM, grab
28500 it now and set the alias set/attributes. The above gen_*_update
28501 calls will generate a PARALLEL with the MEM set being the first
28502 operation. */
28503 par = PATTERN (insn);
28504 gcc_assert (GET_CODE (par) == PARALLEL);
28505 set = XVECEXP (par, 0, 0);
28506 gcc_assert (GET_CODE (set) == SET);
28507 mem = SET_DEST (set);
28508 gcc_assert (MEM_P (mem));
28509 MEM_NOTRAP_P (mem) = 1;
28510 set_mem_alias_set (mem, get_frame_alias_set ());
28512 RTX_FRAME_RELATED_P (insn) = 1;
28513 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
28514 gen_rtx_SET (stack_reg, gen_rtx_PLUS (Pmode, stack_reg,
28515 GEN_INT (-size))));
28516 return insn;
28519 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
28521 #if PROBE_INTERVAL > 32768
28522 #error Cannot use indexed addressing mode for stack probing
28523 #endif
28525 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
28526 inclusive. These are offsets from the current stack pointer. */
28528 static void
28529 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
28531 /* See if we have a constant small number of probes to generate. If so,
28532 that's the easy case. */
28533 if (first + size <= 32768)
28535 HOST_WIDE_INT i;
28537 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
28538 it exceeds SIZE. If only one probe is needed, this will not
28539 generate any code. Then probe at FIRST + SIZE. */
28540 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
28541 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
28542 -(first + i)));
28544 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
28545 -(first + size)));
28548 /* Otherwise, do the same as above, but in a loop. Note that we must be
28549 extra careful with variables wrapping around because we might be at
28550 the very top (or the very bottom) of the address space and we have
28551 to be able to handle this case properly; in particular, we use an
28552 equality test for the loop condition. */
28553 else
28555 HOST_WIDE_INT rounded_size;
28556 rtx r12 = gen_rtx_REG (Pmode, 12);
28557 rtx r0 = gen_rtx_REG (Pmode, 0);
28559 /* Sanity check for the addressing mode we're going to use. */
28560 gcc_assert (first <= 32768);
28562 /* Step 1: round SIZE to the previous multiple of the interval. */
28564 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
28567 /* Step 2: compute initial and final value of the loop counter. */
28569 /* TEST_ADDR = SP + FIRST. */
28570 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
28571 -first)));
28573 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
28574 if (rounded_size > 32768)
28576 emit_move_insn (r0, GEN_INT (-rounded_size));
28577 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
28579 else
28580 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
28581 -rounded_size)));
28584 /* Step 3: the loop
28588 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
28589 probe at TEST_ADDR
28591 while (TEST_ADDR != LAST_ADDR)
28593 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
28594 until it is equal to ROUNDED_SIZE. */
28596 if (TARGET_64BIT)
28597 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
28598 else
28599 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
28602 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
28603 that SIZE is equal to ROUNDED_SIZE. */
28605 if (size != rounded_size)
28606 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
28610 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
28611 absolute addresses. */
28613 const char *
28614 output_probe_stack_range (rtx reg1, rtx reg2)
28616 static int labelno = 0;
28617 char loop_lab[32];
28618 rtx xops[2];
28620 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
28622 /* Loop. */
28623 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
28625 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
28626 xops[0] = reg1;
28627 xops[1] = GEN_INT (-PROBE_INTERVAL);
28628 output_asm_insn ("addi %0,%0,%1", xops);
28630 /* Probe at TEST_ADDR. */
28631 xops[1] = gen_rtx_REG (Pmode, 0);
28632 output_asm_insn ("stw %1,0(%0)", xops);
28634 /* Test if TEST_ADDR == LAST_ADDR. */
28635 xops[1] = reg2;
28636 if (TARGET_64BIT)
28637 output_asm_insn ("cmpd 0,%0,%1", xops);
28638 else
28639 output_asm_insn ("cmpw 0,%0,%1", xops);
28641 /* Branch. */
28642 fputs ("\tbne 0,", asm_out_file);
28643 assemble_name_raw (asm_out_file, loop_lab);
28644 fputc ('\n', asm_out_file);
28646 return "";
28649 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
28650 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
28651 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
28652 deduce these equivalences by itself so it wasn't necessary to hold
28653 its hand so much. Don't be tempted to always supply d2_f_d_e with
28654 the actual cfa register, ie. r31 when we are using a hard frame
28655 pointer. That fails when saving regs off r1, and sched moves the
28656 r31 setup past the reg saves. */
28658 static rtx_insn *
28659 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
28660 rtx reg2, rtx repl2)
28662 rtx repl;
28664 if (REGNO (reg) == STACK_POINTER_REGNUM)
28666 gcc_checking_assert (val == 0);
28667 repl = NULL_RTX;
28669 else
28670 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
28671 GEN_INT (val));
28673 rtx pat = PATTERN (insn);
28674 if (!repl && !reg2)
28676 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
28677 if (GET_CODE (pat) == PARALLEL)
28678 for (int i = 0; i < XVECLEN (pat, 0); i++)
28679 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
28681 rtx set = XVECEXP (pat, 0, i);
28683 /* If this PARALLEL has been emitted for out-of-line
28684 register save functions, or store multiple, then omit
28685 eh_frame info for any user-defined global regs. If
28686 eh_frame info is supplied, frame unwinding will
28687 restore a user reg. */
28688 if (!REG_P (SET_SRC (set))
28689 || !fixed_reg_p (REGNO (SET_SRC (set))))
28690 RTX_FRAME_RELATED_P (set) = 1;
28692 RTX_FRAME_RELATED_P (insn) = 1;
28693 return insn;
28696 /* We expect that 'pat' is either a SET or a PARALLEL containing
28697 SETs (and possibly other stuff). In a PARALLEL, all the SETs
28698 are important so they all have to be marked RTX_FRAME_RELATED_P.
28699 Call simplify_replace_rtx on the SETs rather than the whole insn
28700 so as to leave the other stuff alone (for example USE of r12). */
28702 set_used_flags (pat);
28703 if (GET_CODE (pat) == SET)
28705 if (repl)
28706 pat = simplify_replace_rtx (pat, reg, repl);
28707 if (reg2)
28708 pat = simplify_replace_rtx (pat, reg2, repl2);
28710 else if (GET_CODE (pat) == PARALLEL)
28712 pat = shallow_copy_rtx (pat);
28713 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
28715 for (int i = 0; i < XVECLEN (pat, 0); i++)
28716 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
28718 rtx set = XVECEXP (pat, 0, i);
28720 if (repl)
28721 set = simplify_replace_rtx (set, reg, repl);
28722 if (reg2)
28723 set = simplify_replace_rtx (set, reg2, repl2);
28724 XVECEXP (pat, 0, i) = set;
28726 /* Omit eh_frame info for any user-defined global regs. */
28727 if (!REG_P (SET_SRC (set))
28728 || !fixed_reg_p (REGNO (SET_SRC (set))))
28729 RTX_FRAME_RELATED_P (set) = 1;
28732 else
28733 gcc_unreachable ();
28735 RTX_FRAME_RELATED_P (insn) = 1;
28736 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
28738 return insn;
28741 /* Returns an insn that has a vrsave set operation with the
28742 appropriate CLOBBERs. */
28744 static rtx
28745 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
28747 int nclobs, i;
28748 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
28749 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
28751 clobs[0]
28752 = gen_rtx_SET (vrsave,
28753 gen_rtx_UNSPEC_VOLATILE (SImode,
28754 gen_rtvec (2, reg, vrsave),
28755 UNSPECV_SET_VRSAVE));
28757 nclobs = 1;
28759 /* We need to clobber the registers in the mask so the scheduler
28760 does not move sets to VRSAVE before sets of AltiVec registers.
28762 However, if the function receives nonlocal gotos, reload will set
28763 all call saved registers live. We will end up with:
28765 (set (reg 999) (mem))
28766 (parallel [ (set (reg vrsave) (unspec blah))
28767 (clobber (reg 999))])
28769 The clobber will cause the store into reg 999 to be dead, and
28770 flow will attempt to delete an epilogue insn. In this case, we
28771 need an unspec use/set of the register. */
28773 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
28774 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
28776 if (!epiloguep || call_used_regs [i])
28777 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
28778 gen_rtx_REG (V4SImode, i));
28779 else
28781 rtx reg = gen_rtx_REG (V4SImode, i);
28783 clobs[nclobs++]
28784 = gen_rtx_SET (reg,
28785 gen_rtx_UNSPEC (V4SImode,
28786 gen_rtvec (1, reg), 27));
28790 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
28792 for (i = 0; i < nclobs; ++i)
28793 XVECEXP (insn, 0, i) = clobs[i];
28795 return insn;
28798 static rtx
28799 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
28801 rtx addr, mem;
28803 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
28804 mem = gen_frame_mem (GET_MODE (reg), addr);
28805 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
28808 static rtx
28809 gen_frame_load (rtx reg, rtx frame_reg, int offset)
28811 return gen_frame_set (reg, frame_reg, offset, false);
28814 static rtx
28815 gen_frame_store (rtx reg, rtx frame_reg, int offset)
28817 return gen_frame_set (reg, frame_reg, offset, true);
28820 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
28821 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
28823 static rtx_insn *
28824 emit_frame_save (rtx frame_reg, machine_mode mode,
28825 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
28827 rtx reg;
28829 /* Some cases that need register indexed addressing. */
28830 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
28831 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
28832 || (TARGET_E500_DOUBLE && mode == DFmode)
28833 || (TARGET_SPE_ABI
28834 && SPE_VECTOR_MODE (mode)
28835 && !SPE_CONST_OFFSET_OK (offset))));
28837 reg = gen_rtx_REG (mode, regno);
28838 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
28839 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
28840 NULL_RTX, NULL_RTX);
28843 /* Emit an offset memory reference suitable for a frame store, while
28844 converting to a valid addressing mode. */
28846 static rtx
28847 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
28849 rtx int_rtx, offset_rtx;
28851 int_rtx = GEN_INT (offset);
28853 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
28854 || (TARGET_E500_DOUBLE && mode == DFmode))
28856 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
28857 emit_move_insn (offset_rtx, int_rtx);
28859 else
28860 offset_rtx = int_rtx;
28862 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
28865 #ifndef TARGET_FIX_AND_CONTINUE
28866 #define TARGET_FIX_AND_CONTINUE 0
28867 #endif
28869 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
28870 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
28871 #define LAST_SAVRES_REGISTER 31
28872 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
28874 enum {
28875 SAVRES_LR = 0x1,
28876 SAVRES_SAVE = 0x2,
28877 SAVRES_REG = 0x0c,
28878 SAVRES_GPR = 0,
28879 SAVRES_FPR = 4,
28880 SAVRES_VR = 8
28883 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
28885 /* Temporary holding space for an out-of-line register save/restore
28886 routine name. */
28887 static char savres_routine_name[30];
28889 /* Return the name for an out-of-line register save/restore routine.
28890 We are saving/restoring GPRs if GPR is true. */
28892 static char *
28893 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
28895 const char *prefix = "";
28896 const char *suffix = "";
28898 /* Different targets are supposed to define
28899 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
28900 routine name could be defined with:
28902 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
28904 This is a nice idea in practice, but in reality, things are
28905 complicated in several ways:
28907 - ELF targets have save/restore routines for GPRs.
28909 - SPE targets use different prefixes for 32/64-bit registers, and
28910 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
28912 - PPC64 ELF targets have routines for save/restore of GPRs that
28913 differ in what they do with the link register, so having a set
28914 prefix doesn't work. (We only use one of the save routines at
28915 the moment, though.)
28917 - PPC32 elf targets have "exit" versions of the restore routines
28918 that restore the link register and can save some extra space.
28919 These require an extra suffix. (There are also "tail" versions
28920 of the restore routines and "GOT" versions of the save routines,
28921 but we don't generate those at present. Same problems apply,
28922 though.)
28924 We deal with all this by synthesizing our own prefix/suffix and
28925 using that for the simple sprintf call shown above. */
28926 if (TARGET_SPE)
28928 /* No floating point saves on the SPE. */
28929 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
28931 if ((sel & SAVRES_SAVE))
28932 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
28933 else
28934 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
28936 if ((sel & SAVRES_LR))
28937 suffix = "_x";
28939 else if (DEFAULT_ABI == ABI_V4)
28941 if (TARGET_64BIT)
28942 goto aix_names;
28944 if ((sel & SAVRES_REG) == SAVRES_GPR)
28945 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
28946 else if ((sel & SAVRES_REG) == SAVRES_FPR)
28947 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
28948 else if ((sel & SAVRES_REG) == SAVRES_VR)
28949 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
28950 else
28951 abort ();
28953 if ((sel & SAVRES_LR))
28954 suffix = "_x";
28956 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28958 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
28959 /* No out-of-line save/restore routines for GPRs on AIX. */
28960 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
28961 #endif
28963 aix_names:
28964 if ((sel & SAVRES_REG) == SAVRES_GPR)
28965 prefix = ((sel & SAVRES_SAVE)
28966 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
28967 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
28968 else if ((sel & SAVRES_REG) == SAVRES_FPR)
28970 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
28971 if ((sel & SAVRES_LR))
28972 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
28973 else
28974 #endif
28976 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
28977 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
28980 else if ((sel & SAVRES_REG) == SAVRES_VR)
28981 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
28982 else
28983 abort ();
28986 if (DEFAULT_ABI == ABI_DARWIN)
28988 /* The Darwin approach is (slightly) different, in order to be
28989 compatible with code generated by the system toolchain. There is a
28990 single symbol for the start of save sequence, and the code here
28991 embeds an offset into that code on the basis of the first register
28992 to be saved. */
28993 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
28994 if ((sel & SAVRES_REG) == SAVRES_GPR)
28995 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
28996 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
28997 (regno - 13) * 4, prefix, regno);
28998 else if ((sel & SAVRES_REG) == SAVRES_FPR)
28999 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
29000 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
29001 else if ((sel & SAVRES_REG) == SAVRES_VR)
29002 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
29003 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
29004 else
29005 abort ();
29007 else
29008 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
29010 return savres_routine_name;
29013 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
29014 We are saving/restoring GPRs if GPR is true. */
29016 static rtx
29017 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
29019 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
29020 ? info->first_gp_reg_save
29021 : (sel & SAVRES_REG) == SAVRES_FPR
29022 ? info->first_fp_reg_save - 32
29023 : (sel & SAVRES_REG) == SAVRES_VR
29024 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
29025 : -1);
29026 rtx sym;
29027 int select = sel;
29029 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
29030 versions of the gpr routines. */
29031 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
29032 && info->spe_64bit_regs_used)
29033 select ^= SAVRES_FPR ^ SAVRES_GPR;
29035 /* Don't generate bogus routine names. */
29036 gcc_assert (FIRST_SAVRES_REGISTER <= regno
29037 && regno <= LAST_SAVRES_REGISTER
29038 && select >= 0 && select <= 12);
29040 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
29042 if (sym == NULL)
29044 char *name;
29046 name = rs6000_savres_routine_name (info, regno, sel);
29048 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
29049 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
29050 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
29053 return sym;
29056 /* Emit a sequence of insns, including a stack tie if needed, for
29057 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
29058 reset the stack pointer, but move the base of the frame into
29059 reg UPDT_REGNO for use by out-of-line register restore routines. */
29061 static rtx
29062 rs6000_emit_stack_reset (rs6000_stack_t *info,
29063 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
29064 unsigned updt_regno)
29066 /* If there is nothing to do, don't do anything. */
29067 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
29068 return NULL_RTX;
29070 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
29072 /* This blockage is needed so that sched doesn't decide to move
29073 the sp change before the register restores. */
29074 if (DEFAULT_ABI == ABI_V4
29075 || (TARGET_SPE_ABI
29076 && info->spe_64bit_regs_used != 0
29077 && info->first_gp_reg_save != 32))
29078 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
29079 GEN_INT (frame_off)));
29081 /* If we are restoring registers out-of-line, we will be using the
29082 "exit" variants of the restore routines, which will reset the
29083 stack for us. But we do need to point updt_reg into the
29084 right place for those routines. */
29085 if (frame_off != 0)
29086 return emit_insn (gen_add3_insn (updt_reg_rtx,
29087 frame_reg_rtx, GEN_INT (frame_off)));
29088 else
29089 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
29091 return NULL_RTX;
29094 /* Return the register number used as a pointer by out-of-line
29095 save/restore functions. */
29097 static inline unsigned
29098 ptr_regno_for_savres (int sel)
29100 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29101 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
29102 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
29105 /* Construct a parallel rtx describing the effect of a call to an
29106 out-of-line register save/restore routine, and emit the insn
29107 or jump_insn as appropriate. */
29109 static rtx_insn *
29110 rs6000_emit_savres_rtx (rs6000_stack_t *info,
29111 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
29112 machine_mode reg_mode, int sel)
29114 int i;
29115 int offset, start_reg, end_reg, n_regs, use_reg;
29116 int reg_size = GET_MODE_SIZE (reg_mode);
29117 rtx sym;
29118 rtvec p;
29119 rtx par;
29120 rtx_insn *insn;
29122 offset = 0;
29123 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
29124 ? info->first_gp_reg_save
29125 : (sel & SAVRES_REG) == SAVRES_FPR
29126 ? info->first_fp_reg_save
29127 : (sel & SAVRES_REG) == SAVRES_VR
29128 ? info->first_altivec_reg_save
29129 : -1);
29130 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
29131 ? 32
29132 : (sel & SAVRES_REG) == SAVRES_FPR
29133 ? 64
29134 : (sel & SAVRES_REG) == SAVRES_VR
29135 ? LAST_ALTIVEC_REGNO + 1
29136 : -1);
29137 n_regs = end_reg - start_reg;
29138 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
29139 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
29140 + n_regs);
29142 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
29143 RTVEC_ELT (p, offset++) = ret_rtx;
29145 RTVEC_ELT (p, offset++)
29146 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
29148 sym = rs6000_savres_routine_sym (info, sel);
29149 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
29151 use_reg = ptr_regno_for_savres (sel);
29152 if ((sel & SAVRES_REG) == SAVRES_VR)
29154 /* Vector regs are saved/restored using [reg+reg] addressing. */
29155 RTVEC_ELT (p, offset++)
29156 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
29157 RTVEC_ELT (p, offset++)
29158 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
29160 else
29161 RTVEC_ELT (p, offset++)
29162 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
29164 for (i = 0; i < end_reg - start_reg; i++)
29165 RTVEC_ELT (p, i + offset)
29166 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
29167 frame_reg_rtx, save_area_offset + reg_size * i,
29168 (sel & SAVRES_SAVE) != 0);
29170 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
29171 RTVEC_ELT (p, i + offset)
29172 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
29174 par = gen_rtx_PARALLEL (VOIDmode, p);
29176 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
29178 insn = emit_jump_insn (par);
29179 JUMP_LABEL (insn) = ret_rtx;
29181 else
29182 insn = emit_insn (par);
29183 return insn;
29186 /* Emit code to store CR fields that need to be saved into REG. */
29188 static void
29189 rs6000_emit_move_from_cr (rtx reg)
29191 /* Only the ELFv2 ABI allows storing only selected fields. */
29192 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
29194 int i, cr_reg[8], count = 0;
29196 /* Collect CR fields that must be saved. */
29197 for (i = 0; i < 8; i++)
29198 if (save_reg_p (CR0_REGNO + i))
29199 cr_reg[count++] = i;
29201 /* If it's just a single one, use mfcrf. */
29202 if (count == 1)
29204 rtvec p = rtvec_alloc (1);
29205 rtvec r = rtvec_alloc (2);
29206 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
29207 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
29208 RTVEC_ELT (p, 0)
29209 = gen_rtx_SET (reg,
29210 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
29212 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
29213 return;
29216 /* ??? It might be better to handle count == 2 / 3 cases here
29217 as well, using logical operations to combine the values. */
29220 emit_insn (gen_movesi_from_cr (reg));
29223 /* Return whether the split-stack arg pointer (r12) is used. */
29225 static bool
29226 split_stack_arg_pointer_used_p (void)
29228 /* If the pseudo holding the arg pointer is no longer a pseudo,
29229 then the arg pointer is used. */
29230 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
29231 && (!REG_P (cfun->machine->split_stack_arg_pointer)
29232 || (REGNO (cfun->machine->split_stack_arg_pointer)
29233 < FIRST_PSEUDO_REGISTER)))
29234 return true;
29236 /* Unfortunately we also need to do some code scanning, since
29237 r12 may have been substituted for the pseudo. */
29238 rtx_insn *insn;
29239 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
29240 FOR_BB_INSNS (bb, insn)
29241 if (NONDEBUG_INSN_P (insn))
29243 /* A call destroys r12. */
29244 if (CALL_P (insn))
29245 return false;
29247 df_ref use;
29248 FOR_EACH_INSN_USE (use, insn)
29250 rtx x = DF_REF_REG (use);
29251 if (REG_P (x) && REGNO (x) == 12)
29252 return true;
29254 df_ref def;
29255 FOR_EACH_INSN_DEF (def, insn)
29257 rtx x = DF_REF_REG (def);
29258 if (REG_P (x) && REGNO (x) == 12)
29259 return false;
29262 return bitmap_bit_p (DF_LR_OUT (bb), 12);
29265 /* Return whether we need to emit an ELFv2 global entry point prologue. */
29267 static bool
29268 rs6000_global_entry_point_needed_p (void)
29270 /* Only needed for the ELFv2 ABI. */
29271 if (DEFAULT_ABI != ABI_ELFv2)
29272 return false;
29274 /* With -msingle-pic-base, we assume the whole program shares the same
29275 TOC, so no global entry point prologues are needed anywhere. */
29276 if (TARGET_SINGLE_PIC_BASE)
29277 return false;
29279 /* Ensure we have a global entry point for thunks. ??? We could
29280 avoid that if the target routine doesn't need a global entry point,
29281 but we do not know whether this is the case at this point. */
29282 if (cfun->is_thunk)
29283 return true;
29285 /* For regular functions, rs6000_emit_prologue sets this flag if the
29286 routine ever uses the TOC pointer. */
29287 return cfun->machine->r2_setup_needed;
29290 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
29291 static sbitmap
29292 rs6000_get_separate_components (void)
29294 rs6000_stack_t *info = rs6000_stack_info ();
29296 if (WORLD_SAVE_P (info))
29297 return NULL;
29299 if (TARGET_SPE_ABI)
29300 return NULL;
29302 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
29303 && !(info->savres_strategy & REST_MULTIPLE));
29305 /* Component 0 is the save/restore of LR (done via GPR0).
29306 Components 13..31 are the save/restore of GPR13..GPR31.
29307 Components 46..63 are the save/restore of FPR14..FPR31. */
29309 cfun->machine->n_components = 64;
29311 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
29312 bitmap_clear (components);
29314 int reg_size = TARGET_32BIT ? 4 : 8;
29315 int fp_reg_size = 8;
29317 /* The GPRs we need saved to the frame. */
29318 if ((info->savres_strategy & SAVE_INLINE_GPRS)
29319 && (info->savres_strategy & REST_INLINE_GPRS))
29321 int offset = info->gp_save_offset;
29322 if (info->push_p)
29323 offset += info->total_size;
29325 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
29327 if (IN_RANGE (offset, -0x8000, 0x7fff)
29328 && rs6000_reg_live_or_pic_offset_p (regno))
29329 bitmap_set_bit (components, regno);
29331 offset += reg_size;
29335 /* Don't mess with the hard frame pointer. */
29336 if (frame_pointer_needed)
29337 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
29339 /* Don't mess with the fixed TOC register. */
29340 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
29341 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
29342 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
29343 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
29345 /* The FPRs we need saved to the frame. */
29346 if ((info->savres_strategy & SAVE_INLINE_FPRS)
29347 && (info->savres_strategy & REST_INLINE_FPRS))
29349 int offset = info->fp_save_offset;
29350 if (info->push_p)
29351 offset += info->total_size;
29353 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
29355 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
29356 bitmap_set_bit (components, regno);
29358 offset += fp_reg_size;
29362 /* Optimize LR save and restore if we can. This is component 0. Any
29363 out-of-line register save/restore routines need LR. */
29364 if (info->lr_save_p
29365 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
29366 && (info->savres_strategy & SAVE_INLINE_GPRS)
29367 && (info->savres_strategy & REST_INLINE_GPRS)
29368 && (info->savres_strategy & SAVE_INLINE_FPRS)
29369 && (info->savres_strategy & REST_INLINE_FPRS)
29370 && (info->savres_strategy & SAVE_INLINE_VRS)
29371 && (info->savres_strategy & REST_INLINE_VRS))
29373 int offset = info->lr_save_offset;
29374 if (info->push_p)
29375 offset += info->total_size;
29376 if (IN_RANGE (offset, -0x8000, 0x7fff))
29377 bitmap_set_bit (components, 0);
29380 return components;
29383 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
29384 static sbitmap
29385 rs6000_components_for_bb (basic_block bb)
29387 rs6000_stack_t *info = rs6000_stack_info ();
29389 bitmap in = DF_LIVE_IN (bb);
29390 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
29391 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
29393 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
29394 bitmap_clear (components);
29396 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
29398 /* GPRs. */
29399 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
29400 if (bitmap_bit_p (in, regno)
29401 || bitmap_bit_p (gen, regno)
29402 || bitmap_bit_p (kill, regno))
29403 bitmap_set_bit (components, regno);
29405 /* FPRs. */
29406 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
29407 if (bitmap_bit_p (in, regno)
29408 || bitmap_bit_p (gen, regno)
29409 || bitmap_bit_p (kill, regno))
29410 bitmap_set_bit (components, regno);
29412 /* The link register. */
29413 if (bitmap_bit_p (in, LR_REGNO)
29414 || bitmap_bit_p (gen, LR_REGNO)
29415 || bitmap_bit_p (kill, LR_REGNO))
29416 bitmap_set_bit (components, 0);
29418 return components;
29421 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
29422 static void
29423 rs6000_disqualify_components (sbitmap components, edge e,
29424 sbitmap edge_components, bool /*is_prologue*/)
29426 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
29427 live where we want to place that code. */
29428 if (bitmap_bit_p (edge_components, 0)
29429 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
29431 if (dump_file)
29432 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
29433 "on entry to bb %d\n", e->dest->index);
29434 bitmap_clear_bit (components, 0);
29438 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
29439 static void
29440 rs6000_emit_prologue_components (sbitmap components)
29442 rs6000_stack_t *info = rs6000_stack_info ();
29443 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
29444 ? HARD_FRAME_POINTER_REGNUM
29445 : STACK_POINTER_REGNUM);
29447 machine_mode reg_mode = Pmode;
29448 int reg_size = TARGET_32BIT ? 4 : 8;
29449 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
29450 ? DFmode : SFmode;
29451 int fp_reg_size = 8;
29453 /* Prologue for LR. */
29454 if (bitmap_bit_p (components, 0))
29456 rtx reg = gen_rtx_REG (reg_mode, 0);
29457 rtx_insn *insn = emit_move_insn (reg, gen_rtx_REG (reg_mode, LR_REGNO));
29458 RTX_FRAME_RELATED_P (insn) = 1;
29459 add_reg_note (insn, REG_CFA_REGISTER, NULL);
29461 int offset = info->lr_save_offset;
29462 if (info->push_p)
29463 offset += info->total_size;
29465 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
29466 RTX_FRAME_RELATED_P (insn) = 1;
29467 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
29468 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
29469 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
29472 /* Prologue for the GPRs. */
29473 int offset = info->gp_save_offset;
29474 if (info->push_p)
29475 offset += info->total_size;
29477 for (int i = info->first_gp_reg_save; i < 32; i++)
29479 if (bitmap_bit_p (components, i))
29481 rtx reg = gen_rtx_REG (reg_mode, i);
29482 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
29483 RTX_FRAME_RELATED_P (insn) = 1;
29484 rtx set = copy_rtx (single_set (insn));
29485 add_reg_note (insn, REG_CFA_OFFSET, set);
29488 offset += reg_size;
29491 /* Prologue for the FPRs. */
29492 offset = info->fp_save_offset;
29493 if (info->push_p)
29494 offset += info->total_size;
29496 for (int i = info->first_fp_reg_save; i < 64; i++)
29498 if (bitmap_bit_p (components, i))
29500 rtx reg = gen_rtx_REG (fp_reg_mode, i);
29501 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
29502 RTX_FRAME_RELATED_P (insn) = 1;
29503 rtx set = copy_rtx (single_set (insn));
29504 add_reg_note (insn, REG_CFA_OFFSET, set);
29507 offset += fp_reg_size;
29511 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
29512 static void
29513 rs6000_emit_epilogue_components (sbitmap components)
29515 rs6000_stack_t *info = rs6000_stack_info ();
29516 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
29517 ? HARD_FRAME_POINTER_REGNUM
29518 : STACK_POINTER_REGNUM);
29520 machine_mode reg_mode = Pmode;
29521 int reg_size = TARGET_32BIT ? 4 : 8;
29523 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
29524 ? DFmode : SFmode;
29525 int fp_reg_size = 8;
29527 /* Epilogue for the FPRs. */
29528 int offset = info->fp_save_offset;
29529 if (info->push_p)
29530 offset += info->total_size;
29532 for (int i = info->first_fp_reg_save; i < 64; i++)
29534 if (bitmap_bit_p (components, i))
29536 rtx reg = gen_rtx_REG (fp_reg_mode, i);
29537 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
29538 RTX_FRAME_RELATED_P (insn) = 1;
29539 add_reg_note (insn, REG_CFA_RESTORE, reg);
29542 offset += fp_reg_size;
29545 /* Epilogue for the GPRs. */
29546 offset = info->gp_save_offset;
29547 if (info->push_p)
29548 offset += info->total_size;
29550 for (int i = info->first_gp_reg_save; i < 32; i++)
29552 if (bitmap_bit_p (components, i))
29554 rtx reg = gen_rtx_REG (reg_mode, i);
29555 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
29556 RTX_FRAME_RELATED_P (insn) = 1;
29557 add_reg_note (insn, REG_CFA_RESTORE, reg);
29560 offset += reg_size;
29563 /* Epilogue for LR. */
29564 if (bitmap_bit_p (components, 0))
29566 int offset = info->lr_save_offset;
29567 if (info->push_p)
29568 offset += info->total_size;
29570 rtx reg = gen_rtx_REG (reg_mode, 0);
29571 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
29573 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
29574 insn = emit_move_insn (lr, reg);
29575 RTX_FRAME_RELATED_P (insn) = 1;
29576 add_reg_note (insn, REG_CFA_RESTORE, lr);
29580 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
29581 static void
29582 rs6000_set_handled_components (sbitmap components)
29584 rs6000_stack_t *info = rs6000_stack_info ();
29586 for (int i = info->first_gp_reg_save; i < 32; i++)
29587 if (bitmap_bit_p (components, i))
29588 cfun->machine->gpr_is_wrapped_separately[i] = true;
29590 for (int i = info->first_fp_reg_save; i < 64; i++)
29591 if (bitmap_bit_p (components, i))
29592 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
29594 if (bitmap_bit_p (components, 0))
29595 cfun->machine->lr_is_wrapped_separately = true;
29598 /* Emit function prologue as insns. */
29600 void
29601 rs6000_emit_prologue (void)
29603 rs6000_stack_t *info = rs6000_stack_info ();
29604 machine_mode reg_mode = Pmode;
29605 int reg_size = TARGET_32BIT ? 4 : 8;
29606 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
29607 ? DFmode : SFmode;
29608 int fp_reg_size = 8;
29609 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29610 rtx frame_reg_rtx = sp_reg_rtx;
29611 unsigned int cr_save_regno;
29612 rtx cr_save_rtx = NULL_RTX;
29613 rtx_insn *insn;
29614 int strategy;
29615 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
29616 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
29617 && call_used_regs[STATIC_CHAIN_REGNUM]);
29618 int using_split_stack = (flag_split_stack
29619 && (lookup_attribute ("no_split_stack",
29620 DECL_ATTRIBUTES (cfun->decl))
29621 == NULL));
29623 /* Offset to top of frame for frame_reg and sp respectively. */
29624 HOST_WIDE_INT frame_off = 0;
29625 HOST_WIDE_INT sp_off = 0;
29626 /* sp_adjust is the stack adjusting instruction, tracked so that the
29627 insn setting up the split-stack arg pointer can be emitted just
29628 prior to it, when r12 is not used here for other purposes. */
29629 rtx_insn *sp_adjust = 0;
29631 #if CHECKING_P
29632 /* Track and check usage of r0, r11, r12. */
29633 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
29634 #define START_USE(R) do \
29636 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
29637 reg_inuse |= 1 << (R); \
29638 } while (0)
29639 #define END_USE(R) do \
29641 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
29642 reg_inuse &= ~(1 << (R)); \
29643 } while (0)
29644 #define NOT_INUSE(R) do \
29646 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
29647 } while (0)
29648 #else
29649 #define START_USE(R) do {} while (0)
29650 #define END_USE(R) do {} while (0)
29651 #define NOT_INUSE(R) do {} while (0)
29652 #endif
29654 if (DEFAULT_ABI == ABI_ELFv2
29655 && !TARGET_SINGLE_PIC_BASE)
29657 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
29659 /* With -mminimal-toc we may generate an extra use of r2 below. */
29660 if (TARGET_TOC && TARGET_MINIMAL_TOC
29661 && !constant_pool_empty_p ())
29662 cfun->machine->r2_setup_needed = true;
29666 if (flag_stack_usage_info)
29667 current_function_static_stack_size = info->total_size;
29669 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
29671 HOST_WIDE_INT size = info->total_size;
29673 if (crtl->is_leaf && !cfun->calls_alloca)
29675 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
29676 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT,
29677 size - STACK_CHECK_PROTECT);
29679 else if (size > 0)
29680 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
29683 if (TARGET_FIX_AND_CONTINUE)
29685 /* gdb on darwin arranges to forward a function from the old
29686 address by modifying the first 5 instructions of the function
29687 to branch to the overriding function. This is necessary to
29688 permit function pointers that point to the old function to
29689 actually forward to the new function. */
29690 emit_insn (gen_nop ());
29691 emit_insn (gen_nop ());
29692 emit_insn (gen_nop ());
29693 emit_insn (gen_nop ());
29694 emit_insn (gen_nop ());
29697 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
29699 reg_mode = V2SImode;
29700 reg_size = 8;
29703 /* Handle world saves specially here. */
29704 if (WORLD_SAVE_P (info))
29706 int i, j, sz;
29707 rtx treg;
29708 rtvec p;
29709 rtx reg0;
29711 /* save_world expects lr in r0. */
29712 reg0 = gen_rtx_REG (Pmode, 0);
29713 if (info->lr_save_p)
29715 insn = emit_move_insn (reg0,
29716 gen_rtx_REG (Pmode, LR_REGNO));
29717 RTX_FRAME_RELATED_P (insn) = 1;
29720 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
29721 assumptions about the offsets of various bits of the stack
29722 frame. */
29723 gcc_assert (info->gp_save_offset == -220
29724 && info->fp_save_offset == -144
29725 && info->lr_save_offset == 8
29726 && info->cr_save_offset == 4
29727 && info->push_p
29728 && info->lr_save_p
29729 && (!crtl->calls_eh_return
29730 || info->ehrd_offset == -432)
29731 && info->vrsave_save_offset == -224
29732 && info->altivec_save_offset == -416);
29734 treg = gen_rtx_REG (SImode, 11);
29735 emit_move_insn (treg, GEN_INT (-info->total_size));
29737 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
29738 in R11. It also clobbers R12, so beware! */
29740 /* Preserve CR2 for save_world prologues */
29741 sz = 5;
29742 sz += 32 - info->first_gp_reg_save;
29743 sz += 64 - info->first_fp_reg_save;
29744 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
29745 p = rtvec_alloc (sz);
29746 j = 0;
29747 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
29748 gen_rtx_REG (SImode,
29749 LR_REGNO));
29750 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
29751 gen_rtx_SYMBOL_REF (Pmode,
29752 "*save_world"));
29753 /* We do floats first so that the instruction pattern matches
29754 properly. */
29755 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
29756 RTVEC_ELT (p, j++)
29757 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
29758 ? DFmode : SFmode,
29759 info->first_fp_reg_save + i),
29760 frame_reg_rtx,
29761 info->fp_save_offset + frame_off + 8 * i);
29762 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
29763 RTVEC_ELT (p, j++)
29764 = gen_frame_store (gen_rtx_REG (V4SImode,
29765 info->first_altivec_reg_save + i),
29766 frame_reg_rtx,
29767 info->altivec_save_offset + frame_off + 16 * i);
29768 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
29769 RTVEC_ELT (p, j++)
29770 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
29771 frame_reg_rtx,
29772 info->gp_save_offset + frame_off + reg_size * i);
29774 /* CR register traditionally saved as CR2. */
29775 RTVEC_ELT (p, j++)
29776 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
29777 frame_reg_rtx, info->cr_save_offset + frame_off);
29778 /* Explain about use of R0. */
29779 if (info->lr_save_p)
29780 RTVEC_ELT (p, j++)
29781 = gen_frame_store (reg0,
29782 frame_reg_rtx, info->lr_save_offset + frame_off);
29783 /* Explain what happens to the stack pointer. */
29785 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
29786 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
29789 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
29790 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
29791 treg, GEN_INT (-info->total_size));
29792 sp_off = frame_off = info->total_size;
29795 strategy = info->savres_strategy;
29797 /* For V.4, update stack before we do any saving and set back pointer. */
29798 if (! WORLD_SAVE_P (info)
29799 && info->push_p
29800 && (DEFAULT_ABI == ABI_V4
29801 || crtl->calls_eh_return))
29803 bool need_r11 = (TARGET_SPE
29804 ? (!(strategy & SAVE_INLINE_GPRS)
29805 && info->spe_64bit_regs_used == 0)
29806 : (!(strategy & SAVE_INLINE_FPRS)
29807 || !(strategy & SAVE_INLINE_GPRS)
29808 || !(strategy & SAVE_INLINE_VRS)));
29809 int ptr_regno = -1;
29810 rtx ptr_reg = NULL_RTX;
29811 int ptr_off = 0;
29813 if (info->total_size < 32767)
29814 frame_off = info->total_size;
29815 else if (need_r11)
29816 ptr_regno = 11;
29817 else if (info->cr_save_p
29818 || info->lr_save_p
29819 || info->first_fp_reg_save < 64
29820 || info->first_gp_reg_save < 32
29821 || info->altivec_size != 0
29822 || info->vrsave_size != 0
29823 || crtl->calls_eh_return)
29824 ptr_regno = 12;
29825 else
29827 /* The prologue won't be saving any regs so there is no need
29828 to set up a frame register to access any frame save area.
29829 We also won't be using frame_off anywhere below, but set
29830 the correct value anyway to protect against future
29831 changes to this function. */
29832 frame_off = info->total_size;
29834 if (ptr_regno != -1)
29836 /* Set up the frame offset to that needed by the first
29837 out-of-line save function. */
29838 START_USE (ptr_regno);
29839 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
29840 frame_reg_rtx = ptr_reg;
29841 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
29842 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
29843 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
29844 ptr_off = info->gp_save_offset + info->gp_size;
29845 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
29846 ptr_off = info->altivec_save_offset + info->altivec_size;
29847 frame_off = -ptr_off;
29849 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
29850 ptr_reg, ptr_off);
29851 if (REGNO (frame_reg_rtx) == 12)
29852 sp_adjust = 0;
29853 sp_off = info->total_size;
29854 if (frame_reg_rtx != sp_reg_rtx)
29855 rs6000_emit_stack_tie (frame_reg_rtx, false);
29858 /* If we use the link register, get it into r0. */
29859 if (!WORLD_SAVE_P (info) && info->lr_save_p
29860 && !cfun->machine->lr_is_wrapped_separately)
29862 rtx addr, reg, mem;
29864 reg = gen_rtx_REG (Pmode, 0);
29865 START_USE (0);
29866 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
29867 RTX_FRAME_RELATED_P (insn) = 1;
29869 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
29870 | SAVE_NOINLINE_FPRS_SAVES_LR)))
29872 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
29873 GEN_INT (info->lr_save_offset + frame_off));
29874 mem = gen_rtx_MEM (Pmode, addr);
29875 /* This should not be of rs6000_sr_alias_set, because of
29876 __builtin_return_address. */
29878 insn = emit_move_insn (mem, reg);
29879 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
29880 NULL_RTX, NULL_RTX);
29881 END_USE (0);
29885 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
29886 r12 will be needed by out-of-line gpr restore. */
29887 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29888 && !(strategy & (SAVE_INLINE_GPRS
29889 | SAVE_NOINLINE_GPRS_SAVES_LR))
29890 ? 11 : 12);
29891 if (!WORLD_SAVE_P (info)
29892 && info->cr_save_p
29893 && REGNO (frame_reg_rtx) != cr_save_regno
29894 && !(using_static_chain_p && cr_save_regno == 11)
29895 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
29897 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
29898 START_USE (cr_save_regno);
29899 rs6000_emit_move_from_cr (cr_save_rtx);
29902 /* Do any required saving of fpr's. If only one or two to save, do
29903 it ourselves. Otherwise, call function. */
29904 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
29906 int offset = info->fp_save_offset + frame_off;
29907 for (int i = info->first_fp_reg_save; i < 64; i++)
29909 if (save_reg_p (i)
29910 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
29911 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
29912 sp_off - frame_off);
29914 offset += fp_reg_size;
29917 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
29919 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
29920 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
29921 unsigned ptr_regno = ptr_regno_for_savres (sel);
29922 rtx ptr_reg = frame_reg_rtx;
29924 if (REGNO (frame_reg_rtx) == ptr_regno)
29925 gcc_checking_assert (frame_off == 0);
29926 else
29928 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
29929 NOT_INUSE (ptr_regno);
29930 emit_insn (gen_add3_insn (ptr_reg,
29931 frame_reg_rtx, GEN_INT (frame_off)));
29933 insn = rs6000_emit_savres_rtx (info, ptr_reg,
29934 info->fp_save_offset,
29935 info->lr_save_offset,
29936 DFmode, sel);
29937 rs6000_frame_related (insn, ptr_reg, sp_off,
29938 NULL_RTX, NULL_RTX);
29939 if (lr)
29940 END_USE (0);
29943 /* Save GPRs. This is done as a PARALLEL if we are using
29944 the store-multiple instructions. */
29945 if (!WORLD_SAVE_P (info)
29946 && TARGET_SPE_ABI
29947 && info->spe_64bit_regs_used != 0
29948 && info->first_gp_reg_save != 32)
29950 int i;
29951 rtx spe_save_area_ptr;
29952 HOST_WIDE_INT save_off;
29953 int ool_adjust = 0;
29955 /* Determine whether we can address all of the registers that need
29956 to be saved with an offset from frame_reg_rtx that fits in
29957 the small const field for SPE memory instructions. */
29958 int spe_regs_addressable
29959 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
29960 + reg_size * (32 - info->first_gp_reg_save - 1))
29961 && (strategy & SAVE_INLINE_GPRS));
29963 if (spe_regs_addressable)
29965 spe_save_area_ptr = frame_reg_rtx;
29966 save_off = frame_off;
29968 else
29970 /* Make r11 point to the start of the SPE save area. We need
29971 to be careful here if r11 is holding the static chain. If
29972 it is, then temporarily save it in r0. */
29973 HOST_WIDE_INT offset;
29975 if (!(strategy & SAVE_INLINE_GPRS))
29976 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
29977 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
29978 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
29979 save_off = frame_off - offset;
29981 if (using_static_chain_p)
29983 rtx r0 = gen_rtx_REG (Pmode, 0);
29985 START_USE (0);
29986 gcc_assert (info->first_gp_reg_save > 11);
29988 emit_move_insn (r0, spe_save_area_ptr);
29990 else if (REGNO (frame_reg_rtx) != 11)
29991 START_USE (11);
29993 emit_insn (gen_addsi3 (spe_save_area_ptr,
29994 frame_reg_rtx, GEN_INT (offset)));
29995 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
29996 frame_off = -info->spe_gp_save_offset + ool_adjust;
29999 if ((strategy & SAVE_INLINE_GPRS))
30001 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
30002 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
30003 emit_frame_save (spe_save_area_ptr, reg_mode,
30004 info->first_gp_reg_save + i,
30005 (info->spe_gp_save_offset + save_off
30006 + reg_size * i),
30007 sp_off - save_off);
30009 else
30011 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
30012 info->spe_gp_save_offset + save_off,
30013 0, reg_mode,
30014 SAVRES_SAVE | SAVRES_GPR);
30016 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
30017 NULL_RTX, NULL_RTX);
30020 /* Move the static chain pointer back. */
30021 if (!spe_regs_addressable)
30023 if (using_static_chain_p)
30025 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
30026 END_USE (0);
30028 else if (REGNO (frame_reg_rtx) != 11)
30029 END_USE (11);
30032 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
30034 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
30035 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
30036 unsigned ptr_regno = ptr_regno_for_savres (sel);
30037 rtx ptr_reg = frame_reg_rtx;
30038 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
30039 int end_save = info->gp_save_offset + info->gp_size;
30040 int ptr_off;
30042 if (ptr_regno == 12)
30043 sp_adjust = 0;
30044 if (!ptr_set_up)
30045 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
30047 /* Need to adjust r11 (r12) if we saved any FPRs. */
30048 if (end_save + frame_off != 0)
30050 rtx offset = GEN_INT (end_save + frame_off);
30052 if (ptr_set_up)
30053 frame_off = -end_save;
30054 else
30055 NOT_INUSE (ptr_regno);
30056 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
30058 else if (!ptr_set_up)
30060 NOT_INUSE (ptr_regno);
30061 emit_move_insn (ptr_reg, frame_reg_rtx);
30063 ptr_off = -end_save;
30064 insn = rs6000_emit_savres_rtx (info, ptr_reg,
30065 info->gp_save_offset + ptr_off,
30066 info->lr_save_offset + ptr_off,
30067 reg_mode, sel);
30068 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
30069 NULL_RTX, NULL_RTX);
30070 if (lr)
30071 END_USE (0);
30073 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
30075 rtvec p;
30076 int i;
30077 p = rtvec_alloc (32 - info->first_gp_reg_save);
30078 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
30079 RTVEC_ELT (p, i)
30080 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
30081 frame_reg_rtx,
30082 info->gp_save_offset + frame_off + reg_size * i);
30083 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
30084 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
30085 NULL_RTX, NULL_RTX);
30087 else if (!WORLD_SAVE_P (info))
30089 int offset = info->gp_save_offset + frame_off;
30090 for (int i = info->first_gp_reg_save; i < 32; i++)
30092 if (rs6000_reg_live_or_pic_offset_p (i)
30093 && !cfun->machine->gpr_is_wrapped_separately[i])
30094 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
30095 sp_off - frame_off);
30097 offset += reg_size;
30101 if (crtl->calls_eh_return)
30103 unsigned int i;
30104 rtvec p;
30106 for (i = 0; ; ++i)
30108 unsigned int regno = EH_RETURN_DATA_REGNO (i);
30109 if (regno == INVALID_REGNUM)
30110 break;
30113 p = rtvec_alloc (i);
30115 for (i = 0; ; ++i)
30117 unsigned int regno = EH_RETURN_DATA_REGNO (i);
30118 if (regno == INVALID_REGNUM)
30119 break;
30121 rtx set
30122 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
30123 sp_reg_rtx,
30124 info->ehrd_offset + sp_off + reg_size * (int) i);
30125 RTVEC_ELT (p, i) = set;
30126 RTX_FRAME_RELATED_P (set) = 1;
30129 insn = emit_insn (gen_blockage ());
30130 RTX_FRAME_RELATED_P (insn) = 1;
30131 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
30134 /* In AIX ABI we need to make sure r2 is really saved. */
30135 if (TARGET_AIX && crtl->calls_eh_return)
30137 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
30138 rtx join_insn, note;
30139 rtx_insn *save_insn;
30140 long toc_restore_insn;
30142 tmp_reg = gen_rtx_REG (Pmode, 11);
30143 tmp_reg_si = gen_rtx_REG (SImode, 11);
30144 if (using_static_chain_p)
30146 START_USE (0);
30147 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
30149 else
30150 START_USE (11);
30151 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
30152 /* Peek at instruction to which this function returns. If it's
30153 restoring r2, then we know we've already saved r2. We can't
30154 unconditionally save r2 because the value we have will already
30155 be updated if we arrived at this function via a plt call or
30156 toc adjusting stub. */
30157 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
30158 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
30159 + RS6000_TOC_SAVE_SLOT);
30160 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
30161 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
30162 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
30163 validate_condition_mode (EQ, CCUNSmode);
30164 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
30165 emit_insn (gen_rtx_SET (compare_result,
30166 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
30167 toc_save_done = gen_label_rtx ();
30168 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
30169 gen_rtx_EQ (VOIDmode, compare_result,
30170 const0_rtx),
30171 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
30172 pc_rtx);
30173 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
30174 JUMP_LABEL (jump) = toc_save_done;
30175 LABEL_NUSES (toc_save_done) += 1;
30177 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
30178 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
30179 sp_off - frame_off);
30181 emit_label (toc_save_done);
30183 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
30184 have a CFG that has different saves along different paths.
30185 Move the note to a dummy blockage insn, which describes that
30186 R2 is unconditionally saved after the label. */
30187 /* ??? An alternate representation might be a special insn pattern
30188 containing both the branch and the store. That might let the
30189 code that minimizes the number of DW_CFA_advance opcodes better
30190 freedom in placing the annotations. */
30191 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
30192 if (note)
30193 remove_note (save_insn, note);
30194 else
30195 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
30196 copy_rtx (PATTERN (save_insn)), NULL_RTX);
30197 RTX_FRAME_RELATED_P (save_insn) = 0;
30199 join_insn = emit_insn (gen_blockage ());
30200 REG_NOTES (join_insn) = note;
30201 RTX_FRAME_RELATED_P (join_insn) = 1;
30203 if (using_static_chain_p)
30205 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
30206 END_USE (0);
30208 else
30209 END_USE (11);
30212 /* Save CR if we use any that must be preserved. */
30213 if (!WORLD_SAVE_P (info) && info->cr_save_p)
30215 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
30216 GEN_INT (info->cr_save_offset + frame_off));
30217 rtx mem = gen_frame_mem (SImode, addr);
30219 /* If we didn't copy cr before, do so now using r0. */
30220 if (cr_save_rtx == NULL_RTX)
30222 START_USE (0);
30223 cr_save_rtx = gen_rtx_REG (SImode, 0);
30224 rs6000_emit_move_from_cr (cr_save_rtx);
30227 /* Saving CR requires a two-instruction sequence: one instruction
30228 to move the CR to a general-purpose register, and a second
30229 instruction that stores the GPR to memory.
30231 We do not emit any DWARF CFI records for the first of these,
30232 because we cannot properly represent the fact that CR is saved in
30233 a register. One reason is that we cannot express that multiple
30234 CR fields are saved; another reason is that on 64-bit, the size
30235 of the CR register in DWARF (4 bytes) differs from the size of
30236 a general-purpose register.
30238 This means if any intervening instruction were to clobber one of
30239 the call-saved CR fields, we'd have incorrect CFI. To prevent
30240 this from happening, we mark the store to memory as a use of
30241 those CR fields, which prevents any such instruction from being
30242 scheduled in between the two instructions. */
30243 rtx crsave_v[9];
30244 int n_crsave = 0;
30245 int i;
30247 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
30248 for (i = 0; i < 8; i++)
30249 if (save_reg_p (CR0_REGNO + i))
30250 crsave_v[n_crsave++]
30251 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
30253 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
30254 gen_rtvec_v (n_crsave, crsave_v)));
30255 END_USE (REGNO (cr_save_rtx));
30257 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
30258 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
30259 so we need to construct a frame expression manually. */
30260 RTX_FRAME_RELATED_P (insn) = 1;
30262 /* Update address to be stack-pointer relative, like
30263 rs6000_frame_related would do. */
30264 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
30265 GEN_INT (info->cr_save_offset + sp_off));
30266 mem = gen_frame_mem (SImode, addr);
30268 if (DEFAULT_ABI == ABI_ELFv2)
30270 /* In the ELFv2 ABI we generate separate CFI records for each
30271 CR field that was actually saved. They all point to the
30272 same 32-bit stack slot. */
30273 rtx crframe[8];
30274 int n_crframe = 0;
30276 for (i = 0; i < 8; i++)
30277 if (save_reg_p (CR0_REGNO + i))
30279 crframe[n_crframe]
30280 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
30282 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
30283 n_crframe++;
30286 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
30287 gen_rtx_PARALLEL (VOIDmode,
30288 gen_rtvec_v (n_crframe, crframe)));
30290 else
30292 /* In other ABIs, by convention, we use a single CR regnum to
30293 represent the fact that all call-saved CR fields are saved.
30294 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
30295 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
30296 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
30300 /* In the ELFv2 ABI we need to save all call-saved CR fields into
30301 *separate* slots if the routine calls __builtin_eh_return, so
30302 that they can be independently restored by the unwinder. */
30303 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
30305 int i, cr_off = info->ehcr_offset;
30306 rtx crsave;
30308 /* ??? We might get better performance by using multiple mfocrf
30309 instructions. */
30310 crsave = gen_rtx_REG (SImode, 0);
30311 emit_insn (gen_movesi_from_cr (crsave));
30313 for (i = 0; i < 8; i++)
30314 if (!call_used_regs[CR0_REGNO + i])
30316 rtvec p = rtvec_alloc (2);
30317 RTVEC_ELT (p, 0)
30318 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
30319 RTVEC_ELT (p, 1)
30320 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
30322 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
30324 RTX_FRAME_RELATED_P (insn) = 1;
30325 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
30326 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
30327 sp_reg_rtx, cr_off + sp_off));
30329 cr_off += reg_size;
30333 /* Update stack and set back pointer unless this is V.4,
30334 for which it was done previously. */
30335 if (!WORLD_SAVE_P (info) && info->push_p
30336 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
30338 rtx ptr_reg = NULL;
30339 int ptr_off = 0;
30341 /* If saving altivec regs we need to be able to address all save
30342 locations using a 16-bit offset. */
30343 if ((strategy & SAVE_INLINE_VRS) == 0
30344 || (info->altivec_size != 0
30345 && (info->altivec_save_offset + info->altivec_size - 16
30346 + info->total_size - frame_off) > 32767)
30347 || (info->vrsave_size != 0
30348 && (info->vrsave_save_offset
30349 + info->total_size - frame_off) > 32767))
30351 int sel = SAVRES_SAVE | SAVRES_VR;
30352 unsigned ptr_regno = ptr_regno_for_savres (sel);
30354 if (using_static_chain_p
30355 && ptr_regno == STATIC_CHAIN_REGNUM)
30356 ptr_regno = 12;
30357 if (REGNO (frame_reg_rtx) != ptr_regno)
30358 START_USE (ptr_regno);
30359 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
30360 frame_reg_rtx = ptr_reg;
30361 ptr_off = info->altivec_save_offset + info->altivec_size;
30362 frame_off = -ptr_off;
30364 else if (REGNO (frame_reg_rtx) == 1)
30365 frame_off = info->total_size;
30366 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
30367 ptr_reg, ptr_off);
30368 if (REGNO (frame_reg_rtx) == 12)
30369 sp_adjust = 0;
30370 sp_off = info->total_size;
30371 if (frame_reg_rtx != sp_reg_rtx)
30372 rs6000_emit_stack_tie (frame_reg_rtx, false);
30375 /* Set frame pointer, if needed. */
30376 if (frame_pointer_needed)
30378 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
30379 sp_reg_rtx);
30380 RTX_FRAME_RELATED_P (insn) = 1;
30383 /* Save AltiVec registers if needed. Save here because the red zone does
30384 not always include AltiVec registers. */
30385 if (!WORLD_SAVE_P (info)
30386 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
30388 int end_save = info->altivec_save_offset + info->altivec_size;
30389 int ptr_off;
30390 /* Oddly, the vector save/restore functions point r0 at the end
30391 of the save area, then use r11 or r12 to load offsets for
30392 [reg+reg] addressing. */
30393 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
30394 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
30395 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
30397 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
30398 NOT_INUSE (0);
30399 if (scratch_regno == 12)
30400 sp_adjust = 0;
30401 if (end_save + frame_off != 0)
30403 rtx offset = GEN_INT (end_save + frame_off);
30405 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
30407 else
30408 emit_move_insn (ptr_reg, frame_reg_rtx);
30410 ptr_off = -end_save;
30411 insn = rs6000_emit_savres_rtx (info, scratch_reg,
30412 info->altivec_save_offset + ptr_off,
30413 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
30414 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
30415 NULL_RTX, NULL_RTX);
30416 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
30418 /* The oddity mentioned above clobbered our frame reg. */
30419 emit_move_insn (frame_reg_rtx, ptr_reg);
30420 frame_off = ptr_off;
30423 else if (!WORLD_SAVE_P (info)
30424 && info->altivec_size != 0)
30426 int i;
30428 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
30429 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
30431 rtx areg, savereg, mem;
30432 HOST_WIDE_INT offset;
30434 offset = (info->altivec_save_offset + frame_off
30435 + 16 * (i - info->first_altivec_reg_save));
30437 savereg = gen_rtx_REG (V4SImode, i);
30439 if (TARGET_P9_DFORM_VECTOR && quad_address_offset_p (offset))
30441 mem = gen_frame_mem (V4SImode,
30442 gen_rtx_PLUS (Pmode, frame_reg_rtx,
30443 GEN_INT (offset)));
30444 insn = emit_insn (gen_rtx_SET (mem, savereg));
30445 areg = NULL_RTX;
30447 else
30449 NOT_INUSE (0);
30450 areg = gen_rtx_REG (Pmode, 0);
30451 emit_move_insn (areg, GEN_INT (offset));
30453 /* AltiVec addressing mode is [reg+reg]. */
30454 mem = gen_frame_mem (V4SImode,
30455 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
30457 /* Rather than emitting a generic move, force use of the stvx
30458 instruction, which we always want on ISA 2.07 (power8) systems.
30459 In particular we don't want xxpermdi/stxvd2x for little
30460 endian. */
30461 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
30464 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
30465 areg, GEN_INT (offset));
30469 /* VRSAVE is a bit vector representing which AltiVec registers
30470 are used. The OS uses this to determine which vector
30471 registers to save on a context switch. We need to save
30472 VRSAVE on the stack frame, add whatever AltiVec registers we
30473 used in this function, and do the corresponding magic in the
30474 epilogue. */
30476 if (!WORLD_SAVE_P (info)
30477 && info->vrsave_size != 0)
30479 rtx reg, vrsave;
30480 int offset;
30481 int save_regno;
30483 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
30484 be using r12 as frame_reg_rtx and r11 as the static chain
30485 pointer for nested functions. */
30486 save_regno = 12;
30487 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
30488 && !using_static_chain_p)
30489 save_regno = 11;
30490 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
30492 save_regno = 11;
30493 if (using_static_chain_p)
30494 save_regno = 0;
30497 NOT_INUSE (save_regno);
30498 reg = gen_rtx_REG (SImode, save_regno);
30499 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
30500 if (TARGET_MACHO)
30501 emit_insn (gen_get_vrsave_internal (reg));
30502 else
30503 emit_insn (gen_rtx_SET (reg, vrsave));
30505 /* Save VRSAVE. */
30506 offset = info->vrsave_save_offset + frame_off;
30507 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
30509 /* Include the registers in the mask. */
30510 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
30512 insn = emit_insn (generate_set_vrsave (reg, info, 0));
30515 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
30516 if (!TARGET_SINGLE_PIC_BASE
30517 && ((TARGET_TOC && TARGET_MINIMAL_TOC
30518 && !constant_pool_empty_p ())
30519 || (DEFAULT_ABI == ABI_V4
30520 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
30521 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
30523 /* If emit_load_toc_table will use the link register, we need to save
30524 it. We use R12 for this purpose because emit_load_toc_table
30525 can use register 0. This allows us to use a plain 'blr' to return
30526 from the procedure more often. */
30527 int save_LR_around_toc_setup = (TARGET_ELF
30528 && DEFAULT_ABI == ABI_V4
30529 && flag_pic
30530 && ! info->lr_save_p
30531 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
30532 if (save_LR_around_toc_setup)
30534 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
30535 rtx tmp = gen_rtx_REG (Pmode, 12);
30537 sp_adjust = 0;
30538 insn = emit_move_insn (tmp, lr);
30539 RTX_FRAME_RELATED_P (insn) = 1;
30541 rs6000_emit_load_toc_table (TRUE);
30543 insn = emit_move_insn (lr, tmp);
30544 add_reg_note (insn, REG_CFA_RESTORE, lr);
30545 RTX_FRAME_RELATED_P (insn) = 1;
30547 else
30548 rs6000_emit_load_toc_table (TRUE);
30551 #if TARGET_MACHO
30552 if (!TARGET_SINGLE_PIC_BASE
30553 && DEFAULT_ABI == ABI_DARWIN
30554 && flag_pic && crtl->uses_pic_offset_table)
30556 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
30557 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
30559 /* Save and restore LR locally around this call (in R0). */
30560 if (!info->lr_save_p)
30561 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
30563 emit_insn (gen_load_macho_picbase (src));
30565 emit_move_insn (gen_rtx_REG (Pmode,
30566 RS6000_PIC_OFFSET_TABLE_REGNUM),
30567 lr);
30569 if (!info->lr_save_p)
30570 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
30572 #endif
30574 /* If we need to, save the TOC register after doing the stack setup.
30575 Do not emit eh frame info for this save. The unwinder wants info,
30576 conceptually attached to instructions in this function, about
30577 register values in the caller of this function. This R2 may have
30578 already been changed from the value in the caller.
30579 We don't attempt to write accurate DWARF EH frame info for R2
30580 because code emitted by gcc for a (non-pointer) function call
30581 doesn't save and restore R2. Instead, R2 is managed out-of-line
30582 by a linker generated plt call stub when the function resides in
30583 a shared library. This behavior is costly to describe in DWARF,
30584 both in terms of the size of DWARF info and the time taken in the
30585 unwinder to interpret it. R2 changes, apart from the
30586 calls_eh_return case earlier in this function, are handled by
30587 linux-unwind.h frob_update_context. */
30588 if (rs6000_save_toc_in_prologue_p ())
30590 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
30591 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
30594 if (using_split_stack && split_stack_arg_pointer_used_p ())
30596 /* Set up the arg pointer (r12) for -fsplit-stack code. If
30597 __morestack was called, it left the arg pointer to the old
30598 stack in r29. Otherwise, the arg pointer is the top of the
30599 current frame. */
30600 cfun->machine->split_stack_argp_used = true;
30601 if (sp_adjust)
30603 rtx r12 = gen_rtx_REG (Pmode, 12);
30604 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
30605 emit_insn_before (set_r12, sp_adjust);
30607 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
30609 rtx r12 = gen_rtx_REG (Pmode, 12);
30610 if (frame_off == 0)
30611 emit_move_insn (r12, frame_reg_rtx);
30612 else
30613 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
30615 if (info->push_p)
30617 rtx r12 = gen_rtx_REG (Pmode, 12);
30618 rtx r29 = gen_rtx_REG (Pmode, 29);
30619 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
30620 rtx not_more = gen_label_rtx ();
30621 rtx jump;
30623 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
30624 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
30625 gen_rtx_LABEL_REF (VOIDmode, not_more),
30626 pc_rtx);
30627 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
30628 JUMP_LABEL (jump) = not_more;
30629 LABEL_NUSES (not_more) += 1;
30630 emit_move_insn (r12, r29);
30631 emit_label (not_more);
30636 /* Output .extern statements for the save/restore routines we use. */
30638 static void
30639 rs6000_output_savres_externs (FILE *file)
30641 rs6000_stack_t *info = rs6000_stack_info ();
30643 if (TARGET_DEBUG_STACK)
30644 debug_stack_info (info);
30646 /* Write .extern for any function we will call to save and restore
30647 fp values. */
30648 if (info->first_fp_reg_save < 64
30649 && !TARGET_MACHO
30650 && !TARGET_ELF)
30652 char *name;
30653 int regno = info->first_fp_reg_save - 32;
30655 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
30657 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
30658 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
30659 name = rs6000_savres_routine_name (info, regno, sel);
30660 fprintf (file, "\t.extern %s\n", name);
30662 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
30664 bool lr = (info->savres_strategy
30665 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
30666 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
30667 name = rs6000_savres_routine_name (info, regno, sel);
30668 fprintf (file, "\t.extern %s\n", name);
30673 /* Write function prologue. */
30675 static void
30676 rs6000_output_function_prologue (FILE *file)
30678 if (!cfun->is_thunk)
30679 rs6000_output_savres_externs (file);
30681 /* ELFv2 ABI r2 setup code and local entry point. This must follow
30682 immediately after the global entry point label. */
30683 if (rs6000_global_entry_point_needed_p ())
30685 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
30687 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
30689 if (TARGET_CMODEL != CMODEL_LARGE)
30691 /* In the small and medium code models, we assume the TOC is less
30692 2 GB away from the text section, so it can be computed via the
30693 following two-instruction sequence. */
30694 char buf[256];
30696 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
30697 fprintf (file, "0:\taddis 2,12,.TOC.-");
30698 assemble_name (file, buf);
30699 fprintf (file, "@ha\n");
30700 fprintf (file, "\taddi 2,2,.TOC.-");
30701 assemble_name (file, buf);
30702 fprintf (file, "@l\n");
30704 else
30706 /* In the large code model, we allow arbitrary offsets between the
30707 TOC and the text section, so we have to load the offset from
30708 memory. The data field is emitted directly before the global
30709 entry point in rs6000_elf_declare_function_name. */
30710 char buf[256];
30712 #ifdef HAVE_AS_ENTRY_MARKERS
30713 /* If supported by the linker, emit a marker relocation. If the
30714 total code size of the final executable or shared library
30715 happens to fit into 2 GB after all, the linker will replace
30716 this code sequence with the sequence for the small or medium
30717 code model. */
30718 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
30719 #endif
30720 fprintf (file, "\tld 2,");
30721 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
30722 assemble_name (file, buf);
30723 fprintf (file, "-");
30724 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
30725 assemble_name (file, buf);
30726 fprintf (file, "(12)\n");
30727 fprintf (file, "\tadd 2,2,12\n");
30730 fputs ("\t.localentry\t", file);
30731 assemble_name (file, name);
30732 fputs (",.-", file);
30733 assemble_name (file, name);
30734 fputs ("\n", file);
30737 /* Output -mprofile-kernel code. This needs to be done here instead of
30738 in output_function_profile since it must go after the ELFv2 ABI
30739 local entry point. */
30740 if (TARGET_PROFILE_KERNEL && crtl->profile)
30742 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
30743 gcc_assert (!TARGET_32BIT);
30745 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
30747 /* In the ELFv2 ABI we have no compiler stack word. It must be
30748 the resposibility of _mcount to preserve the static chain
30749 register if required. */
30750 if (DEFAULT_ABI != ABI_ELFv2
30751 && cfun->static_chain_decl != NULL)
30753 asm_fprintf (file, "\tstd %s,24(%s)\n",
30754 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
30755 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
30756 asm_fprintf (file, "\tld %s,24(%s)\n",
30757 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
30759 else
30760 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
30763 rs6000_pic_labelno++;
30766 /* -mprofile-kernel code calls mcount before the function prolog,
30767 so a profiled leaf function should stay a leaf function. */
30768 static bool
30769 rs6000_keep_leaf_when_profiled ()
30771 return TARGET_PROFILE_KERNEL;
30774 /* Non-zero if vmx regs are restored before the frame pop, zero if
30775 we restore after the pop when possible. */
30776 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
30778 /* Restoring cr is a two step process: loading a reg from the frame
30779 save, then moving the reg to cr. For ABI_V4 we must let the
30780 unwinder know that the stack location is no longer valid at or
30781 before the stack deallocation, but we can't emit a cfa_restore for
30782 cr at the stack deallocation like we do for other registers.
30783 The trouble is that it is possible for the move to cr to be
30784 scheduled after the stack deallocation. So say exactly where cr
30785 is located on each of the two insns. */
30787 static rtx
30788 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
30790 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
30791 rtx reg = gen_rtx_REG (SImode, regno);
30792 rtx_insn *insn = emit_move_insn (reg, mem);
30794 if (!exit_func && DEFAULT_ABI == ABI_V4)
30796 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
30797 rtx set = gen_rtx_SET (reg, cr);
30799 add_reg_note (insn, REG_CFA_REGISTER, set);
30800 RTX_FRAME_RELATED_P (insn) = 1;
30802 return reg;
30805 /* Reload CR from REG. */
30807 static void
30808 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
30810 int count = 0;
30811 int i;
30813 if (using_mfcr_multiple)
30815 for (i = 0; i < 8; i++)
30816 if (save_reg_p (CR0_REGNO + i))
30817 count++;
30818 gcc_assert (count);
30821 if (using_mfcr_multiple && count > 1)
30823 rtx_insn *insn;
30824 rtvec p;
30825 int ndx;
30827 p = rtvec_alloc (count);
30829 ndx = 0;
30830 for (i = 0; i < 8; i++)
30831 if (save_reg_p (CR0_REGNO + i))
30833 rtvec r = rtvec_alloc (2);
30834 RTVEC_ELT (r, 0) = reg;
30835 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
30836 RTVEC_ELT (p, ndx) =
30837 gen_rtx_SET (gen_rtx_REG (CCmode, CR0_REGNO + i),
30838 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
30839 ndx++;
30841 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
30842 gcc_assert (ndx == count);
30844 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
30845 CR field separately. */
30846 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
30848 for (i = 0; i < 8; i++)
30849 if (save_reg_p (CR0_REGNO + i))
30850 add_reg_note (insn, REG_CFA_RESTORE,
30851 gen_rtx_REG (SImode, CR0_REGNO + i));
30853 RTX_FRAME_RELATED_P (insn) = 1;
30856 else
30857 for (i = 0; i < 8; i++)
30858 if (save_reg_p (CR0_REGNO + i))
30860 rtx insn = emit_insn (gen_movsi_to_cr_one
30861 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
30863 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
30864 CR field separately, attached to the insn that in fact
30865 restores this particular CR field. */
30866 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
30868 add_reg_note (insn, REG_CFA_RESTORE,
30869 gen_rtx_REG (SImode, CR0_REGNO + i));
30871 RTX_FRAME_RELATED_P (insn) = 1;
30875 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
30876 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
30877 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
30879 rtx_insn *insn = get_last_insn ();
30880 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
30882 add_reg_note (insn, REG_CFA_RESTORE, cr);
30883 RTX_FRAME_RELATED_P (insn) = 1;
30887 /* Like cr, the move to lr instruction can be scheduled after the
30888 stack deallocation, but unlike cr, its stack frame save is still
30889 valid. So we only need to emit the cfa_restore on the correct
30890 instruction. */
30892 static void
30893 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
30895 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
30896 rtx reg = gen_rtx_REG (Pmode, regno);
30898 emit_move_insn (reg, mem);
30901 static void
30902 restore_saved_lr (int regno, bool exit_func)
30904 rtx reg = gen_rtx_REG (Pmode, regno);
30905 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
30906 rtx_insn *insn = emit_move_insn (lr, reg);
30908 if (!exit_func && flag_shrink_wrap)
30910 add_reg_note (insn, REG_CFA_RESTORE, lr);
30911 RTX_FRAME_RELATED_P (insn) = 1;
30915 static rtx
30916 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
30918 if (DEFAULT_ABI == ABI_ELFv2)
30920 int i;
30921 for (i = 0; i < 8; i++)
30922 if (save_reg_p (CR0_REGNO + i))
30924 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
30925 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
30926 cfa_restores);
30929 else if (info->cr_save_p)
30930 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
30931 gen_rtx_REG (SImode, CR2_REGNO),
30932 cfa_restores);
30934 if (info->lr_save_p)
30935 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
30936 gen_rtx_REG (Pmode, LR_REGNO),
30937 cfa_restores);
30938 return cfa_restores;
30941 /* Return true if OFFSET from stack pointer can be clobbered by signals.
30942 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
30943 below stack pointer not cloberred by signals. */
30945 static inline bool
30946 offset_below_red_zone_p (HOST_WIDE_INT offset)
30948 return offset < (DEFAULT_ABI == ABI_V4
30950 : TARGET_32BIT ? -220 : -288);
30953 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
30955 static void
30956 emit_cfa_restores (rtx cfa_restores)
30958 rtx_insn *insn = get_last_insn ();
30959 rtx *loc = &REG_NOTES (insn);
30961 while (*loc)
30962 loc = &XEXP (*loc, 1);
30963 *loc = cfa_restores;
30964 RTX_FRAME_RELATED_P (insn) = 1;
30967 /* Emit function epilogue as insns. */
30969 void
30970 rs6000_emit_epilogue (int sibcall)
30972 rs6000_stack_t *info;
30973 int restoring_GPRs_inline;
30974 int restoring_FPRs_inline;
30975 int using_load_multiple;
30976 int using_mtcr_multiple;
30977 int use_backchain_to_restore_sp;
30978 int restore_lr;
30979 int strategy;
30980 HOST_WIDE_INT frame_off = 0;
30981 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
30982 rtx frame_reg_rtx = sp_reg_rtx;
30983 rtx cfa_restores = NULL_RTX;
30984 rtx insn;
30985 rtx cr_save_reg = NULL_RTX;
30986 machine_mode reg_mode = Pmode;
30987 int reg_size = TARGET_32BIT ? 4 : 8;
30988 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
30989 ? DFmode : SFmode;
30990 int fp_reg_size = 8;
30991 int i;
30992 bool exit_func;
30993 unsigned ptr_regno;
30995 info = rs6000_stack_info ();
30997 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
30999 reg_mode = V2SImode;
31000 reg_size = 8;
31003 strategy = info->savres_strategy;
31004 using_load_multiple = strategy & REST_MULTIPLE;
31005 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
31006 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
31007 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
31008 || rs6000_cpu == PROCESSOR_PPC603
31009 || rs6000_cpu == PROCESSOR_PPC750
31010 || optimize_size);
31011 /* Restore via the backchain when we have a large frame, since this
31012 is more efficient than an addis, addi pair. The second condition
31013 here will not trigger at the moment; We don't actually need a
31014 frame pointer for alloca, but the generic parts of the compiler
31015 give us one anyway. */
31016 use_backchain_to_restore_sp = (info->total_size + (info->lr_save_p
31017 ? info->lr_save_offset
31018 : 0) > 32767
31019 || (cfun->calls_alloca
31020 && !frame_pointer_needed));
31021 restore_lr = (info->lr_save_p
31022 && (restoring_FPRs_inline
31023 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
31024 && (restoring_GPRs_inline
31025 || info->first_fp_reg_save < 64)
31026 && !cfun->machine->lr_is_wrapped_separately);
31029 if (WORLD_SAVE_P (info))
31031 int i, j;
31032 char rname[30];
31033 const char *alloc_rname;
31034 rtvec p;
31036 /* eh_rest_world_r10 will return to the location saved in the LR
31037 stack slot (which is not likely to be our caller.)
31038 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
31039 rest_world is similar, except any R10 parameter is ignored.
31040 The exception-handling stuff that was here in 2.95 is no
31041 longer necessary. */
31043 p = rtvec_alloc (9
31044 + 32 - info->first_gp_reg_save
31045 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
31046 + 63 + 1 - info->first_fp_reg_save);
31048 strcpy (rname, ((crtl->calls_eh_return) ?
31049 "*eh_rest_world_r10" : "*rest_world"));
31050 alloc_rname = ggc_strdup (rname);
31052 j = 0;
31053 RTVEC_ELT (p, j++) = ret_rtx;
31054 RTVEC_ELT (p, j++)
31055 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
31056 /* The instruction pattern requires a clobber here;
31057 it is shared with the restVEC helper. */
31058 RTVEC_ELT (p, j++)
31059 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
31062 /* CR register traditionally saved as CR2. */
31063 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
31064 RTVEC_ELT (p, j++)
31065 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
31066 if (flag_shrink_wrap)
31068 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
31069 gen_rtx_REG (Pmode, LR_REGNO),
31070 cfa_restores);
31071 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31075 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
31077 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
31078 RTVEC_ELT (p, j++)
31079 = gen_frame_load (reg,
31080 frame_reg_rtx, info->gp_save_offset + reg_size * i);
31081 if (flag_shrink_wrap)
31082 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31084 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
31086 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
31087 RTVEC_ELT (p, j++)
31088 = gen_frame_load (reg,
31089 frame_reg_rtx, info->altivec_save_offset + 16 * i);
31090 if (flag_shrink_wrap)
31091 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31093 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
31095 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
31096 ? DFmode : SFmode),
31097 info->first_fp_reg_save + i);
31098 RTVEC_ELT (p, j++)
31099 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
31100 if (flag_shrink_wrap)
31101 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31103 RTVEC_ELT (p, j++)
31104 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
31105 RTVEC_ELT (p, j++)
31106 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
31107 RTVEC_ELT (p, j++)
31108 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
31109 RTVEC_ELT (p, j++)
31110 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
31111 RTVEC_ELT (p, j++)
31112 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
31113 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
31115 if (flag_shrink_wrap)
31117 REG_NOTES (insn) = cfa_restores;
31118 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
31119 RTX_FRAME_RELATED_P (insn) = 1;
31121 return;
31124 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
31125 if (info->push_p)
31126 frame_off = info->total_size;
31128 /* Restore AltiVec registers if we must do so before adjusting the
31129 stack. */
31130 if (info->altivec_size != 0
31131 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31132 || (DEFAULT_ABI != ABI_V4
31133 && offset_below_red_zone_p (info->altivec_save_offset))))
31135 int i;
31136 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
31138 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
31139 if (use_backchain_to_restore_sp)
31141 int frame_regno = 11;
31143 if ((strategy & REST_INLINE_VRS) == 0)
31145 /* Of r11 and r12, select the one not clobbered by an
31146 out-of-line restore function for the frame register. */
31147 frame_regno = 11 + 12 - scratch_regno;
31149 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
31150 emit_move_insn (frame_reg_rtx,
31151 gen_rtx_MEM (Pmode, sp_reg_rtx));
31152 frame_off = 0;
31154 else if (frame_pointer_needed)
31155 frame_reg_rtx = hard_frame_pointer_rtx;
31157 if ((strategy & REST_INLINE_VRS) == 0)
31159 int end_save = info->altivec_save_offset + info->altivec_size;
31160 int ptr_off;
31161 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
31162 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
31164 if (end_save + frame_off != 0)
31166 rtx offset = GEN_INT (end_save + frame_off);
31168 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
31170 else
31171 emit_move_insn (ptr_reg, frame_reg_rtx);
31173 ptr_off = -end_save;
31174 insn = rs6000_emit_savres_rtx (info, scratch_reg,
31175 info->altivec_save_offset + ptr_off,
31176 0, V4SImode, SAVRES_VR);
31178 else
31180 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31181 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
31183 rtx addr, areg, mem, insn;
31184 rtx reg = gen_rtx_REG (V4SImode, i);
31185 HOST_WIDE_INT offset
31186 = (info->altivec_save_offset + frame_off
31187 + 16 * (i - info->first_altivec_reg_save));
31189 if (TARGET_P9_DFORM_VECTOR && quad_address_offset_p (offset))
31191 mem = gen_frame_mem (V4SImode,
31192 gen_rtx_PLUS (Pmode, frame_reg_rtx,
31193 GEN_INT (offset)));
31194 insn = gen_rtx_SET (reg, mem);
31196 else
31198 areg = gen_rtx_REG (Pmode, 0);
31199 emit_move_insn (areg, GEN_INT (offset));
31201 /* AltiVec addressing mode is [reg+reg]. */
31202 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
31203 mem = gen_frame_mem (V4SImode, addr);
31205 /* Rather than emitting a generic move, force use of the
31206 lvx instruction, which we always want. In particular we
31207 don't want lxvd2x/xxpermdi for little endian. */
31208 insn = gen_altivec_lvx_v4si_internal (reg, mem);
31211 (void) emit_insn (insn);
31215 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31216 if (((strategy & REST_INLINE_VRS) == 0
31217 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
31218 && (flag_shrink_wrap
31219 || (offset_below_red_zone_p
31220 (info->altivec_save_offset
31221 + 16 * (i - info->first_altivec_reg_save)))))
31223 rtx reg = gen_rtx_REG (V4SImode, i);
31224 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31228 /* Restore VRSAVE if we must do so before adjusting the stack. */
31229 if (info->vrsave_size != 0
31230 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31231 || (DEFAULT_ABI != ABI_V4
31232 && offset_below_red_zone_p (info->vrsave_save_offset))))
31234 rtx reg;
31236 if (frame_reg_rtx == sp_reg_rtx)
31238 if (use_backchain_to_restore_sp)
31240 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31241 emit_move_insn (frame_reg_rtx,
31242 gen_rtx_MEM (Pmode, sp_reg_rtx));
31243 frame_off = 0;
31245 else if (frame_pointer_needed)
31246 frame_reg_rtx = hard_frame_pointer_rtx;
31249 reg = gen_rtx_REG (SImode, 12);
31250 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31251 info->vrsave_save_offset + frame_off));
31253 emit_insn (generate_set_vrsave (reg, info, 1));
31256 insn = NULL_RTX;
31257 /* If we have a large stack frame, restore the old stack pointer
31258 using the backchain. */
31259 if (use_backchain_to_restore_sp)
31261 if (frame_reg_rtx == sp_reg_rtx)
31263 /* Under V.4, don't reset the stack pointer until after we're done
31264 loading the saved registers. */
31265 if (DEFAULT_ABI == ABI_V4)
31266 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31268 insn = emit_move_insn (frame_reg_rtx,
31269 gen_rtx_MEM (Pmode, sp_reg_rtx));
31270 frame_off = 0;
31272 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31273 && DEFAULT_ABI == ABI_V4)
31274 /* frame_reg_rtx has been set up by the altivec restore. */
31276 else
31278 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
31279 frame_reg_rtx = sp_reg_rtx;
31282 /* If we have a frame pointer, we can restore the old stack pointer
31283 from it. */
31284 else if (frame_pointer_needed)
31286 frame_reg_rtx = sp_reg_rtx;
31287 if (DEFAULT_ABI == ABI_V4)
31288 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31289 /* Prevent reordering memory accesses against stack pointer restore. */
31290 else if (cfun->calls_alloca
31291 || offset_below_red_zone_p (-info->total_size))
31292 rs6000_emit_stack_tie (frame_reg_rtx, true);
31294 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
31295 GEN_INT (info->total_size)));
31296 frame_off = 0;
31298 else if (info->push_p
31299 && DEFAULT_ABI != ABI_V4
31300 && !crtl->calls_eh_return)
31302 /* Prevent reordering memory accesses against stack pointer restore. */
31303 if (cfun->calls_alloca
31304 || offset_below_red_zone_p (-info->total_size))
31305 rs6000_emit_stack_tie (frame_reg_rtx, false);
31306 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
31307 GEN_INT (info->total_size)));
31308 frame_off = 0;
31310 if (insn && frame_reg_rtx == sp_reg_rtx)
31312 if (cfa_restores)
31314 REG_NOTES (insn) = cfa_restores;
31315 cfa_restores = NULL_RTX;
31317 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
31318 RTX_FRAME_RELATED_P (insn) = 1;
31321 /* Restore AltiVec registers if we have not done so already. */
31322 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31323 && info->altivec_size != 0
31324 && (DEFAULT_ABI == ABI_V4
31325 || !offset_below_red_zone_p (info->altivec_save_offset)))
31327 int i;
31329 if ((strategy & REST_INLINE_VRS) == 0)
31331 int end_save = info->altivec_save_offset + info->altivec_size;
31332 int ptr_off;
31333 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
31334 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
31335 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
31337 if (end_save + frame_off != 0)
31339 rtx offset = GEN_INT (end_save + frame_off);
31341 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
31343 else
31344 emit_move_insn (ptr_reg, frame_reg_rtx);
31346 ptr_off = -end_save;
31347 insn = rs6000_emit_savres_rtx (info, scratch_reg,
31348 info->altivec_save_offset + ptr_off,
31349 0, V4SImode, SAVRES_VR);
31350 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
31352 /* Frame reg was clobbered by out-of-line save. Restore it
31353 from ptr_reg, and if we are calling out-of-line gpr or
31354 fpr restore set up the correct pointer and offset. */
31355 unsigned newptr_regno = 1;
31356 if (!restoring_GPRs_inline)
31358 bool lr = info->gp_save_offset + info->gp_size == 0;
31359 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
31360 newptr_regno = ptr_regno_for_savres (sel);
31361 end_save = info->gp_save_offset + info->gp_size;
31363 else if (!restoring_FPRs_inline)
31365 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
31366 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
31367 newptr_regno = ptr_regno_for_savres (sel);
31368 end_save = info->fp_save_offset + info->fp_size;
31371 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
31372 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
31374 if (end_save + ptr_off != 0)
31376 rtx offset = GEN_INT (end_save + ptr_off);
31378 frame_off = -end_save;
31379 if (TARGET_32BIT)
31380 emit_insn (gen_addsi3_carry (frame_reg_rtx,
31381 ptr_reg, offset));
31382 else
31383 emit_insn (gen_adddi3_carry (frame_reg_rtx,
31384 ptr_reg, offset));
31386 else
31388 frame_off = ptr_off;
31389 emit_move_insn (frame_reg_rtx, ptr_reg);
31393 else
31395 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31396 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
31398 rtx addr, areg, mem, insn;
31399 rtx reg = gen_rtx_REG (V4SImode, i);
31400 HOST_WIDE_INT offset
31401 = (info->altivec_save_offset + frame_off
31402 + 16 * (i - info->first_altivec_reg_save));
31404 if (TARGET_P9_DFORM_VECTOR && quad_address_offset_p (offset))
31406 mem = gen_frame_mem (V4SImode,
31407 gen_rtx_PLUS (Pmode, frame_reg_rtx,
31408 GEN_INT (offset)));
31409 insn = gen_rtx_SET (reg, mem);
31411 else
31413 areg = gen_rtx_REG (Pmode, 0);
31414 emit_move_insn (areg, GEN_INT (offset));
31416 /* AltiVec addressing mode is [reg+reg]. */
31417 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
31418 mem = gen_frame_mem (V4SImode, addr);
31420 /* Rather than emitting a generic move, force use of the
31421 lvx instruction, which we always want. In particular we
31422 don't want lxvd2x/xxpermdi for little endian. */
31423 insn = gen_altivec_lvx_v4si_internal (reg, mem);
31426 (void) emit_insn (insn);
31430 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
31431 if (((strategy & REST_INLINE_VRS) == 0
31432 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
31433 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
31435 rtx reg = gen_rtx_REG (V4SImode, i);
31436 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31440 /* Restore VRSAVE if we have not done so already. */
31441 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
31442 && info->vrsave_size != 0
31443 && (DEFAULT_ABI == ABI_V4
31444 || !offset_below_red_zone_p (info->vrsave_save_offset)))
31446 rtx reg;
31448 reg = gen_rtx_REG (SImode, 12);
31449 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31450 info->vrsave_save_offset + frame_off));
31452 emit_insn (generate_set_vrsave (reg, info, 1));
31455 /* If we exit by an out-of-line restore function on ABI_V4 then that
31456 function will deallocate the stack, so we don't need to worry
31457 about the unwinder restoring cr from an invalid stack frame
31458 location. */
31459 exit_func = (!restoring_FPRs_inline
31460 || (!restoring_GPRs_inline
31461 && info->first_fp_reg_save == 64));
31463 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
31464 *separate* slots if the routine calls __builtin_eh_return, so
31465 that they can be independently restored by the unwinder. */
31466 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
31468 int i, cr_off = info->ehcr_offset;
31470 for (i = 0; i < 8; i++)
31471 if (!call_used_regs[CR0_REGNO + i])
31473 rtx reg = gen_rtx_REG (SImode, 0);
31474 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31475 cr_off + frame_off));
31477 insn = emit_insn (gen_movsi_to_cr_one
31478 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
31480 if (!exit_func && flag_shrink_wrap)
31482 add_reg_note (insn, REG_CFA_RESTORE,
31483 gen_rtx_REG (SImode, CR0_REGNO + i));
31485 RTX_FRAME_RELATED_P (insn) = 1;
31488 cr_off += reg_size;
31492 /* Get the old lr if we saved it. If we are restoring registers
31493 out-of-line, then the out-of-line routines can do this for us. */
31494 if (restore_lr && restoring_GPRs_inline)
31495 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
31497 /* Get the old cr if we saved it. */
31498 if (info->cr_save_p)
31500 unsigned cr_save_regno = 12;
31502 if (!restoring_GPRs_inline)
31504 /* Ensure we don't use the register used by the out-of-line
31505 gpr register restore below. */
31506 bool lr = info->gp_save_offset + info->gp_size == 0;
31507 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
31508 int gpr_ptr_regno = ptr_regno_for_savres (sel);
31510 if (gpr_ptr_regno == 12)
31511 cr_save_regno = 11;
31512 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
31514 else if (REGNO (frame_reg_rtx) == 12)
31515 cr_save_regno = 11;
31517 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
31518 info->cr_save_offset + frame_off,
31519 exit_func);
31522 /* Set LR here to try to overlap restores below. */
31523 if (restore_lr && restoring_GPRs_inline)
31524 restore_saved_lr (0, exit_func);
31526 /* Load exception handler data registers, if needed. */
31527 if (crtl->calls_eh_return)
31529 unsigned int i, regno;
31531 if (TARGET_AIX)
31533 rtx reg = gen_rtx_REG (reg_mode, 2);
31534 emit_insn (gen_frame_load (reg, frame_reg_rtx,
31535 frame_off + RS6000_TOC_SAVE_SLOT));
31538 for (i = 0; ; ++i)
31540 rtx mem;
31542 regno = EH_RETURN_DATA_REGNO (i);
31543 if (regno == INVALID_REGNUM)
31544 break;
31546 /* Note: possible use of r0 here to address SPE regs. */
31547 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
31548 info->ehrd_offset + frame_off
31549 + reg_size * (int) i);
31551 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
31555 /* Restore GPRs. This is done as a PARALLEL if we are using
31556 the load-multiple instructions. */
31557 if (TARGET_SPE_ABI
31558 && info->spe_64bit_regs_used
31559 && info->first_gp_reg_save != 32)
31561 /* Determine whether we can address all of the registers that need
31562 to be saved with an offset from frame_reg_rtx that fits in
31563 the small const field for SPE memory instructions. */
31564 int spe_regs_addressable
31565 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
31566 + reg_size * (32 - info->first_gp_reg_save - 1))
31567 && restoring_GPRs_inline);
31569 if (!spe_regs_addressable)
31571 int ool_adjust = 0;
31572 rtx old_frame_reg_rtx = frame_reg_rtx;
31573 /* Make r11 point to the start of the SPE save area. We worried about
31574 not clobbering it when we were saving registers in the prologue.
31575 There's no need to worry here because the static chain is passed
31576 anew to every function. */
31578 if (!restoring_GPRs_inline)
31579 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
31580 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
31581 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
31582 GEN_INT (info->spe_gp_save_offset
31583 + frame_off
31584 - ool_adjust)));
31585 /* Keep the invariant that frame_reg_rtx + frame_off points
31586 at the top of the stack frame. */
31587 frame_off = -info->spe_gp_save_offset + ool_adjust;
31590 if (restoring_GPRs_inline)
31592 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
31594 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
31595 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
31597 rtx offset, addr, mem, reg;
31599 /* We're doing all this to ensure that the immediate offset
31600 fits into the immediate field of 'evldd'. */
31601 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
31603 offset = GEN_INT (spe_offset + reg_size * i);
31604 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
31605 mem = gen_rtx_MEM (V2SImode, addr);
31606 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
31608 emit_move_insn (reg, mem);
31611 else
31612 rs6000_emit_savres_rtx (info, frame_reg_rtx,
31613 info->spe_gp_save_offset + frame_off,
31614 info->lr_save_offset + frame_off,
31615 reg_mode,
31616 SAVRES_GPR | SAVRES_LR);
31618 else if (!restoring_GPRs_inline)
31620 /* We are jumping to an out-of-line function. */
31621 rtx ptr_reg;
31622 int end_save = info->gp_save_offset + info->gp_size;
31623 bool can_use_exit = end_save == 0;
31624 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
31625 int ptr_off;
31627 /* Emit stack reset code if we need it. */
31628 ptr_regno = ptr_regno_for_savres (sel);
31629 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
31630 if (can_use_exit)
31631 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
31632 else if (end_save + frame_off != 0)
31633 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
31634 GEN_INT (end_save + frame_off)));
31635 else if (REGNO (frame_reg_rtx) != ptr_regno)
31636 emit_move_insn (ptr_reg, frame_reg_rtx);
31637 if (REGNO (frame_reg_rtx) == ptr_regno)
31638 frame_off = -end_save;
31640 if (can_use_exit && info->cr_save_p)
31641 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
31643 ptr_off = -end_save;
31644 rs6000_emit_savres_rtx (info, ptr_reg,
31645 info->gp_save_offset + ptr_off,
31646 info->lr_save_offset + ptr_off,
31647 reg_mode, sel);
31649 else if (using_load_multiple)
31651 rtvec p;
31652 p = rtvec_alloc (32 - info->first_gp_reg_save);
31653 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
31654 RTVEC_ELT (p, i)
31655 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
31656 frame_reg_rtx,
31657 info->gp_save_offset + frame_off + reg_size * i);
31658 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
31660 else
31662 int offset = info->gp_save_offset + frame_off;
31663 for (i = info->first_gp_reg_save; i < 32; i++)
31665 if (rs6000_reg_live_or_pic_offset_p (i)
31666 && !cfun->machine->gpr_is_wrapped_separately[i])
31668 rtx reg = gen_rtx_REG (reg_mode, i);
31669 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
31672 offset += reg_size;
31676 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
31678 /* If the frame pointer was used then we can't delay emitting
31679 a REG_CFA_DEF_CFA note. This must happen on the insn that
31680 restores the frame pointer, r31. We may have already emitted
31681 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
31682 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
31683 be harmless if emitted. */
31684 if (frame_pointer_needed)
31686 insn = get_last_insn ();
31687 add_reg_note (insn, REG_CFA_DEF_CFA,
31688 plus_constant (Pmode, frame_reg_rtx, frame_off));
31689 RTX_FRAME_RELATED_P (insn) = 1;
31692 /* Set up cfa_restores. We always need these when
31693 shrink-wrapping. If not shrink-wrapping then we only need
31694 the cfa_restore when the stack location is no longer valid.
31695 The cfa_restores must be emitted on or before the insn that
31696 invalidates the stack, and of course must not be emitted
31697 before the insn that actually does the restore. The latter
31698 is why it is a bad idea to emit the cfa_restores as a group
31699 on the last instruction here that actually does a restore:
31700 That insn may be reordered with respect to others doing
31701 restores. */
31702 if (flag_shrink_wrap
31703 && !restoring_GPRs_inline
31704 && info->first_fp_reg_save == 64)
31705 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
31707 for (i = info->first_gp_reg_save; i < 32; i++)
31708 if (!restoring_GPRs_inline
31709 || using_load_multiple
31710 || rs6000_reg_live_or_pic_offset_p (i))
31712 if (cfun->machine->gpr_is_wrapped_separately[i])
31713 continue;
31715 rtx reg = gen_rtx_REG (reg_mode, i);
31716 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31720 if (!restoring_GPRs_inline
31721 && info->first_fp_reg_save == 64)
31723 /* We are jumping to an out-of-line function. */
31724 if (cfa_restores)
31725 emit_cfa_restores (cfa_restores);
31726 return;
31729 if (restore_lr && !restoring_GPRs_inline)
31731 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
31732 restore_saved_lr (0, exit_func);
31735 /* Restore fpr's if we need to do it without calling a function. */
31736 if (restoring_FPRs_inline)
31738 int offset = info->fp_save_offset + frame_off;
31739 for (i = info->first_fp_reg_save; i < 64; i++)
31741 if (save_reg_p (i)
31742 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
31744 rtx reg = gen_rtx_REG (fp_reg_mode, i);
31745 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
31746 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
31747 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
31748 cfa_restores);
31751 offset += fp_reg_size;
31755 /* If we saved cr, restore it here. Just those that were used. */
31756 if (info->cr_save_p)
31757 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
31759 /* If this is V.4, unwind the stack pointer after all of the loads
31760 have been done, or set up r11 if we are restoring fp out of line. */
31761 ptr_regno = 1;
31762 if (!restoring_FPRs_inline)
31764 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
31765 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
31766 ptr_regno = ptr_regno_for_savres (sel);
31769 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
31770 if (REGNO (frame_reg_rtx) == ptr_regno)
31771 frame_off = 0;
31773 if (insn && restoring_FPRs_inline)
31775 if (cfa_restores)
31777 REG_NOTES (insn) = cfa_restores;
31778 cfa_restores = NULL_RTX;
31780 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
31781 RTX_FRAME_RELATED_P (insn) = 1;
31784 if (crtl->calls_eh_return)
31786 rtx sa = EH_RETURN_STACKADJ_RTX;
31787 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
31790 if (!sibcall && restoring_FPRs_inline)
31792 if (cfa_restores)
31794 /* We can't hang the cfa_restores off a simple return,
31795 since the shrink-wrap code sometimes uses an existing
31796 return. This means there might be a path from
31797 pre-prologue code to this return, and dwarf2cfi code
31798 wants the eh_frame unwinder state to be the same on
31799 all paths to any point. So we need to emit the
31800 cfa_restores before the return. For -m64 we really
31801 don't need epilogue cfa_restores at all, except for
31802 this irritating dwarf2cfi with shrink-wrap
31803 requirement; The stack red-zone means eh_frame info
31804 from the prologue telling the unwinder to restore
31805 from the stack is perfectly good right to the end of
31806 the function. */
31807 emit_insn (gen_blockage ());
31808 emit_cfa_restores (cfa_restores);
31809 cfa_restores = NULL_RTX;
31812 emit_jump_insn (targetm.gen_simple_return ());
31815 if (!sibcall && !restoring_FPRs_inline)
31817 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
31818 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
31819 int elt = 0;
31820 RTVEC_ELT (p, elt++) = ret_rtx;
31821 if (lr)
31822 RTVEC_ELT (p, elt++)
31823 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
31825 /* We have to restore more than two FP registers, so branch to the
31826 restore function. It will return to our caller. */
31827 int i;
31828 int reg;
31829 rtx sym;
31831 if (flag_shrink_wrap)
31832 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
31834 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
31835 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
31836 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
31837 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
31839 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
31841 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
31843 RTVEC_ELT (p, elt++)
31844 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
31845 if (flag_shrink_wrap)
31846 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
31849 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
31852 if (cfa_restores)
31854 if (sibcall)
31855 /* Ensure the cfa_restores are hung off an insn that won't
31856 be reordered above other restores. */
31857 emit_insn (gen_blockage ());
31859 emit_cfa_restores (cfa_restores);
31863 /* Write function epilogue. */
31865 static void
31866 rs6000_output_function_epilogue (FILE *file)
31868 #if TARGET_MACHO
31869 macho_branch_islands ();
31872 rtx_insn *insn = get_last_insn ();
31873 rtx_insn *deleted_debug_label = NULL;
31875 /* Mach-O doesn't support labels at the end of objects, so if
31876 it looks like we might want one, take special action.
31878 First, collect any sequence of deleted debug labels. */
31879 while (insn
31880 && NOTE_P (insn)
31881 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
31883 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
31884 notes only, instead set their CODE_LABEL_NUMBER to -1,
31885 otherwise there would be code generation differences
31886 in between -g and -g0. */
31887 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
31888 deleted_debug_label = insn;
31889 insn = PREV_INSN (insn);
31892 /* Second, if we have:
31893 label:
31894 barrier
31895 then this needs to be detected, so skip past the barrier. */
31897 if (insn && BARRIER_P (insn))
31898 insn = PREV_INSN (insn);
31900 /* Up to now we've only seen notes or barriers. */
31901 if (insn)
31903 if (LABEL_P (insn)
31904 || (NOTE_P (insn)
31905 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
31906 /* Trailing label: <barrier>. */
31907 fputs ("\tnop\n", file);
31908 else
31910 /* Lastly, see if we have a completely empty function body. */
31911 while (insn && ! INSN_P (insn))
31912 insn = PREV_INSN (insn);
31913 /* If we don't find any insns, we've got an empty function body;
31914 I.e. completely empty - without a return or branch. This is
31915 taken as the case where a function body has been removed
31916 because it contains an inline __builtin_unreachable(). GCC
31917 states that reaching __builtin_unreachable() means UB so we're
31918 not obliged to do anything special; however, we want
31919 non-zero-sized function bodies. To meet this, and help the
31920 user out, let's trap the case. */
31921 if (insn == NULL)
31922 fputs ("\ttrap\n", file);
31925 else if (deleted_debug_label)
31926 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
31927 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
31928 CODE_LABEL_NUMBER (insn) = -1;
31930 #endif
31932 /* Output a traceback table here. See /usr/include/sys/debug.h for info
31933 on its format.
31935 We don't output a traceback table if -finhibit-size-directive was
31936 used. The documentation for -finhibit-size-directive reads
31937 ``don't output a @code{.size} assembler directive, or anything
31938 else that would cause trouble if the function is split in the
31939 middle, and the two halves are placed at locations far apart in
31940 memory.'' The traceback table has this property, since it
31941 includes the offset from the start of the function to the
31942 traceback table itself.
31944 System V.4 Powerpc's (and the embedded ABI derived from it) use a
31945 different traceback table. */
31946 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
31947 && ! flag_inhibit_size_directive
31948 && rs6000_traceback != traceback_none && !cfun->is_thunk)
31950 const char *fname = NULL;
31951 const char *language_string = lang_hooks.name;
31952 int fixed_parms = 0, float_parms = 0, parm_info = 0;
31953 int i;
31954 int optional_tbtab;
31955 rs6000_stack_t *info = rs6000_stack_info ();
31957 if (rs6000_traceback == traceback_full)
31958 optional_tbtab = 1;
31959 else if (rs6000_traceback == traceback_part)
31960 optional_tbtab = 0;
31961 else
31962 optional_tbtab = !optimize_size && !TARGET_ELF;
31964 if (optional_tbtab)
31966 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
31967 while (*fname == '.') /* V.4 encodes . in the name */
31968 fname++;
31970 /* Need label immediately before tbtab, so we can compute
31971 its offset from the function start. */
31972 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
31973 ASM_OUTPUT_LABEL (file, fname);
31976 /* The .tbtab pseudo-op can only be used for the first eight
31977 expressions, since it can't handle the possibly variable
31978 length fields that follow. However, if you omit the optional
31979 fields, the assembler outputs zeros for all optional fields
31980 anyways, giving each variable length field is minimum length
31981 (as defined in sys/debug.h). Thus we can not use the .tbtab
31982 pseudo-op at all. */
31984 /* An all-zero word flags the start of the tbtab, for debuggers
31985 that have to find it by searching forward from the entry
31986 point or from the current pc. */
31987 fputs ("\t.long 0\n", file);
31989 /* Tbtab format type. Use format type 0. */
31990 fputs ("\t.byte 0,", file);
31992 /* Language type. Unfortunately, there does not seem to be any
31993 official way to discover the language being compiled, so we
31994 use language_string.
31995 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
31996 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
31997 a number, so for now use 9. LTO, Go and JIT aren't assigned numbers
31998 either, so for now use 0. */
31999 if (lang_GNU_C ()
32000 || ! strcmp (language_string, "GNU GIMPLE")
32001 || ! strcmp (language_string, "GNU Go")
32002 || ! strcmp (language_string, "libgccjit"))
32003 i = 0;
32004 else if (! strcmp (language_string, "GNU F77")
32005 || lang_GNU_Fortran ())
32006 i = 1;
32007 else if (! strcmp (language_string, "GNU Pascal"))
32008 i = 2;
32009 else if (! strcmp (language_string, "GNU Ada"))
32010 i = 3;
32011 else if (lang_GNU_CXX ()
32012 || ! strcmp (language_string, "GNU Objective-C++"))
32013 i = 9;
32014 else if (! strcmp (language_string, "GNU Java"))
32015 i = 13;
32016 else if (! strcmp (language_string, "GNU Objective-C"))
32017 i = 14;
32018 else
32019 gcc_unreachable ();
32020 fprintf (file, "%d,", i);
32022 /* 8 single bit fields: global linkage (not set for C extern linkage,
32023 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
32024 from start of procedure stored in tbtab, internal function, function
32025 has controlled storage, function has no toc, function uses fp,
32026 function logs/aborts fp operations. */
32027 /* Assume that fp operations are used if any fp reg must be saved. */
32028 fprintf (file, "%d,",
32029 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
32031 /* 6 bitfields: function is interrupt handler, name present in
32032 proc table, function calls alloca, on condition directives
32033 (controls stack walks, 3 bits), saves condition reg, saves
32034 link reg. */
32035 /* The `function calls alloca' bit seems to be set whenever reg 31 is
32036 set up as a frame pointer, even when there is no alloca call. */
32037 fprintf (file, "%d,",
32038 ((optional_tbtab << 6)
32039 | ((optional_tbtab & frame_pointer_needed) << 5)
32040 | (info->cr_save_p << 1)
32041 | (info->lr_save_p)));
32043 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
32044 (6 bits). */
32045 fprintf (file, "%d,",
32046 (info->push_p << 7) | (64 - info->first_fp_reg_save));
32048 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
32049 fprintf (file, "%d,", (32 - first_reg_to_save ()));
32051 if (optional_tbtab)
32053 /* Compute the parameter info from the function decl argument
32054 list. */
32055 tree decl;
32056 int next_parm_info_bit = 31;
32058 for (decl = DECL_ARGUMENTS (current_function_decl);
32059 decl; decl = DECL_CHAIN (decl))
32061 rtx parameter = DECL_INCOMING_RTL (decl);
32062 machine_mode mode = GET_MODE (parameter);
32064 if (GET_CODE (parameter) == REG)
32066 if (SCALAR_FLOAT_MODE_P (mode))
32068 int bits;
32070 float_parms++;
32072 switch (mode)
32074 case E_SFmode:
32075 case E_SDmode:
32076 bits = 0x2;
32077 break;
32079 case E_DFmode:
32080 case E_DDmode:
32081 case E_TFmode:
32082 case E_TDmode:
32083 case E_IFmode:
32084 case E_KFmode:
32085 bits = 0x3;
32086 break;
32088 default:
32089 gcc_unreachable ();
32092 /* If only one bit will fit, don't or in this entry. */
32093 if (next_parm_info_bit > 0)
32094 parm_info |= (bits << (next_parm_info_bit - 1));
32095 next_parm_info_bit -= 2;
32097 else
32099 fixed_parms += ((GET_MODE_SIZE (mode)
32100 + (UNITS_PER_WORD - 1))
32101 / UNITS_PER_WORD);
32102 next_parm_info_bit -= 1;
32108 /* Number of fixed point parameters. */
32109 /* This is actually the number of words of fixed point parameters; thus
32110 an 8 byte struct counts as 2; and thus the maximum value is 8. */
32111 fprintf (file, "%d,", fixed_parms);
32113 /* 2 bitfields: number of floating point parameters (7 bits), parameters
32114 all on stack. */
32115 /* This is actually the number of fp registers that hold parameters;
32116 and thus the maximum value is 13. */
32117 /* Set parameters on stack bit if parameters are not in their original
32118 registers, regardless of whether they are on the stack? Xlc
32119 seems to set the bit when not optimizing. */
32120 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
32122 if (optional_tbtab)
32124 /* Optional fields follow. Some are variable length. */
32126 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
32127 float, 11 double float. */
32128 /* There is an entry for each parameter in a register, in the order
32129 that they occur in the parameter list. Any intervening arguments
32130 on the stack are ignored. If the list overflows a long (max
32131 possible length 34 bits) then completely leave off all elements
32132 that don't fit. */
32133 /* Only emit this long if there was at least one parameter. */
32134 if (fixed_parms || float_parms)
32135 fprintf (file, "\t.long %d\n", parm_info);
32137 /* Offset from start of code to tb table. */
32138 fputs ("\t.long ", file);
32139 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
32140 RS6000_OUTPUT_BASENAME (file, fname);
32141 putc ('-', file);
32142 rs6000_output_function_entry (file, fname);
32143 putc ('\n', file);
32145 /* Interrupt handler mask. */
32146 /* Omit this long, since we never set the interrupt handler bit
32147 above. */
32149 /* Number of CTL (controlled storage) anchors. */
32150 /* Omit this long, since the has_ctl bit is never set above. */
32152 /* Displacement into stack of each CTL anchor. */
32153 /* Omit this list of longs, because there are no CTL anchors. */
32155 /* Length of function name. */
32156 if (*fname == '*')
32157 ++fname;
32158 fprintf (file, "\t.short %d\n", (int) strlen (fname));
32160 /* Function name. */
32161 assemble_string (fname, strlen (fname));
32163 /* Register for alloca automatic storage; this is always reg 31.
32164 Only emit this if the alloca bit was set above. */
32165 if (frame_pointer_needed)
32166 fputs ("\t.byte 31\n", file);
32168 fputs ("\t.align 2\n", file);
32172 /* Arrange to define .LCTOC1 label, if not already done. */
32173 if (need_toc_init)
32175 need_toc_init = 0;
32176 if (!toc_initialized)
32178 switch_to_section (toc_section);
32179 switch_to_section (current_function_section ());
32184 /* -fsplit-stack support. */
32186 /* A SYMBOL_REF for __morestack. */
32187 static GTY(()) rtx morestack_ref;
32189 static rtx
32190 gen_add3_const (rtx rt, rtx ra, long c)
32192 if (TARGET_64BIT)
32193 return gen_adddi3 (rt, ra, GEN_INT (c));
32194 else
32195 return gen_addsi3 (rt, ra, GEN_INT (c));
32198 /* Emit -fsplit-stack prologue, which goes before the regular function
32199 prologue (at local entry point in the case of ELFv2). */
32201 void
32202 rs6000_expand_split_stack_prologue (void)
32204 rs6000_stack_t *info = rs6000_stack_info ();
32205 unsigned HOST_WIDE_INT allocate;
32206 long alloc_hi, alloc_lo;
32207 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
32208 rtx_insn *insn;
32210 gcc_assert (flag_split_stack && reload_completed);
32212 if (!info->push_p)
32213 return;
32215 if (global_regs[29])
32217 error ("-fsplit-stack uses register r29");
32218 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
32219 "conflicts with %qD", global_regs_decl[29]);
32222 allocate = info->total_size;
32223 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
32225 sorry ("Stack frame larger than 2G is not supported for -fsplit-stack");
32226 return;
32228 if (morestack_ref == NULL_RTX)
32230 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
32231 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
32232 | SYMBOL_FLAG_FUNCTION);
32235 r0 = gen_rtx_REG (Pmode, 0);
32236 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32237 r12 = gen_rtx_REG (Pmode, 12);
32238 emit_insn (gen_load_split_stack_limit (r0));
32239 /* Always emit two insns here to calculate the requested stack,
32240 so that the linker can edit them when adjusting size for calling
32241 non-split-stack code. */
32242 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
32243 alloc_lo = -allocate - alloc_hi;
32244 if (alloc_hi != 0)
32246 emit_insn (gen_add3_const (r12, r1, alloc_hi));
32247 if (alloc_lo != 0)
32248 emit_insn (gen_add3_const (r12, r12, alloc_lo));
32249 else
32250 emit_insn (gen_nop ());
32252 else
32254 emit_insn (gen_add3_const (r12, r1, alloc_lo));
32255 emit_insn (gen_nop ());
32258 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
32259 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
32260 ok_label = gen_label_rtx ();
32261 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
32262 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
32263 gen_rtx_LABEL_REF (VOIDmode, ok_label),
32264 pc_rtx);
32265 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
32266 JUMP_LABEL (insn) = ok_label;
32267 /* Mark the jump as very likely to be taken. */
32268 add_reg_br_prob_note (insn, profile_probability::very_likely ());
32270 lr = gen_rtx_REG (Pmode, LR_REGNO);
32271 insn = emit_move_insn (r0, lr);
32272 RTX_FRAME_RELATED_P (insn) = 1;
32273 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
32274 RTX_FRAME_RELATED_P (insn) = 1;
32276 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
32277 const0_rtx, const0_rtx));
32278 call_fusage = NULL_RTX;
32279 use_reg (&call_fusage, r12);
32280 /* Say the call uses r0, even though it doesn't, to stop regrename
32281 from twiddling with the insns saving lr, trashing args for cfun.
32282 The insns restoring lr are similarly protected by making
32283 split_stack_return use r0. */
32284 use_reg (&call_fusage, r0);
32285 add_function_usage_to (insn, call_fusage);
32286 /* Indicate that this function can't jump to non-local gotos. */
32287 make_reg_eh_region_note_nothrow_nononlocal (insn);
32288 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
32289 insn = emit_move_insn (lr, r0);
32290 add_reg_note (insn, REG_CFA_RESTORE, lr);
32291 RTX_FRAME_RELATED_P (insn) = 1;
32292 emit_insn (gen_split_stack_return ());
32294 emit_label (ok_label);
32295 LABEL_NUSES (ok_label) = 1;
32298 /* Return the internal arg pointer used for function incoming
32299 arguments. When -fsplit-stack, the arg pointer is r12 so we need
32300 to copy it to a pseudo in order for it to be preserved over calls
32301 and suchlike. We'd really like to use a pseudo here for the
32302 internal arg pointer but data-flow analysis is not prepared to
32303 accept pseudos as live at the beginning of a function. */
32305 static rtx
32306 rs6000_internal_arg_pointer (void)
32308 if (flag_split_stack
32309 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
32310 == NULL))
32313 if (cfun->machine->split_stack_arg_pointer == NULL_RTX)
32315 rtx pat;
32317 cfun->machine->split_stack_arg_pointer = gen_reg_rtx (Pmode);
32318 REG_POINTER (cfun->machine->split_stack_arg_pointer) = 1;
32320 /* Put the pseudo initialization right after the note at the
32321 beginning of the function. */
32322 pat = gen_rtx_SET (cfun->machine->split_stack_arg_pointer,
32323 gen_rtx_REG (Pmode, 12));
32324 push_topmost_sequence ();
32325 emit_insn_after (pat, get_insns ());
32326 pop_topmost_sequence ();
32328 return plus_constant (Pmode, cfun->machine->split_stack_arg_pointer,
32329 FIRST_PARM_OFFSET (current_function_decl));
32331 return virtual_incoming_args_rtx;
32334 /* We may have to tell the dataflow pass that the split stack prologue
32335 is initializing a register. */
32337 static void
32338 rs6000_live_on_entry (bitmap regs)
32340 if (flag_split_stack)
32341 bitmap_set_bit (regs, 12);
32344 /* Emit -fsplit-stack dynamic stack allocation space check. */
32346 void
32347 rs6000_split_stack_space_check (rtx size, rtx label)
32349 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32350 rtx limit = gen_reg_rtx (Pmode);
32351 rtx requested = gen_reg_rtx (Pmode);
32352 rtx cmp = gen_reg_rtx (CCUNSmode);
32353 rtx jump;
32355 emit_insn (gen_load_split_stack_limit (limit));
32356 if (CONST_INT_P (size))
32357 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
32358 else
32360 size = force_reg (Pmode, size);
32361 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
32363 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
32364 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
32365 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
32366 gen_rtx_LABEL_REF (VOIDmode, label),
32367 pc_rtx);
32368 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
32369 JUMP_LABEL (jump) = label;
32372 /* A C compound statement that outputs the assembler code for a thunk
32373 function, used to implement C++ virtual function calls with
32374 multiple inheritance. The thunk acts as a wrapper around a virtual
32375 function, adjusting the implicit object parameter before handing
32376 control off to the real function.
32378 First, emit code to add the integer DELTA to the location that
32379 contains the incoming first argument. Assume that this argument
32380 contains a pointer, and is the one used to pass the `this' pointer
32381 in C++. This is the incoming argument *before* the function
32382 prologue, e.g. `%o0' on a sparc. The addition must preserve the
32383 values of all other incoming arguments.
32385 After the addition, emit code to jump to FUNCTION, which is a
32386 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
32387 not touch the return address. Hence returning from FUNCTION will
32388 return to whoever called the current `thunk'.
32390 The effect must be as if FUNCTION had been called directly with the
32391 adjusted first argument. This macro is responsible for emitting
32392 all of the code for a thunk function; output_function_prologue()
32393 and output_function_epilogue() are not invoked.
32395 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
32396 been extracted from it.) It might possibly be useful on some
32397 targets, but probably not.
32399 If you do not define this macro, the target-independent code in the
32400 C++ frontend will generate a less efficient heavyweight thunk that
32401 calls FUNCTION instead of jumping to it. The generic approach does
32402 not support varargs. */
32404 static void
32405 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
32406 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
32407 tree function)
32409 rtx this_rtx, funexp;
32410 rtx_insn *insn;
32412 reload_completed = 1;
32413 epilogue_completed = 1;
32415 /* Mark the end of the (empty) prologue. */
32416 emit_note (NOTE_INSN_PROLOGUE_END);
32418 /* Find the "this" pointer. If the function returns a structure,
32419 the structure return pointer is in r3. */
32420 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
32421 this_rtx = gen_rtx_REG (Pmode, 4);
32422 else
32423 this_rtx = gen_rtx_REG (Pmode, 3);
32425 /* Apply the constant offset, if required. */
32426 if (delta)
32427 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
32429 /* Apply the offset from the vtable, if required. */
32430 if (vcall_offset)
32432 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
32433 rtx tmp = gen_rtx_REG (Pmode, 12);
32435 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
32436 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
32438 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
32439 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
32441 else
32443 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
32445 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
32447 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
32450 /* Generate a tail call to the target function. */
32451 if (!TREE_USED (function))
32453 assemble_external (function);
32454 TREE_USED (function) = 1;
32456 funexp = XEXP (DECL_RTL (function), 0);
32457 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
32459 #if TARGET_MACHO
32460 if (MACHOPIC_INDIRECT)
32461 funexp = machopic_indirect_call_target (funexp);
32462 #endif
32464 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
32465 generate sibcall RTL explicitly. */
32466 insn = emit_call_insn (
32467 gen_rtx_PARALLEL (VOIDmode,
32468 gen_rtvec (3,
32469 gen_rtx_CALL (VOIDmode,
32470 funexp, const0_rtx),
32471 gen_rtx_USE (VOIDmode, const0_rtx),
32472 simple_return_rtx)));
32473 SIBLING_CALL_P (insn) = 1;
32474 emit_barrier ();
32476 /* Run just enough of rest_of_compilation to get the insns emitted.
32477 There's not really enough bulk here to make other passes such as
32478 instruction scheduling worth while. Note that use_thunk calls
32479 assemble_start_function and assemble_end_function. */
32480 insn = get_insns ();
32481 shorten_branches (insn);
32482 final_start_function (insn, file, 1);
32483 final (insn, file, 1);
32484 final_end_function ();
32486 reload_completed = 0;
32487 epilogue_completed = 0;
32490 /* A quick summary of the various types of 'constant-pool tables'
32491 under PowerPC:
32493 Target Flags Name One table per
32494 AIX (none) AIX TOC object file
32495 AIX -mfull-toc AIX TOC object file
32496 AIX -mminimal-toc AIX minimal TOC translation unit
32497 SVR4/EABI (none) SVR4 SDATA object file
32498 SVR4/EABI -fpic SVR4 pic object file
32499 SVR4/EABI -fPIC SVR4 PIC translation unit
32500 SVR4/EABI -mrelocatable EABI TOC function
32501 SVR4/EABI -maix AIX TOC object file
32502 SVR4/EABI -maix -mminimal-toc
32503 AIX minimal TOC translation unit
32505 Name Reg. Set by entries contains:
32506 made by addrs? fp? sum?
32508 AIX TOC 2 crt0 as Y option option
32509 AIX minimal TOC 30 prolog gcc Y Y option
32510 SVR4 SDATA 13 crt0 gcc N Y N
32511 SVR4 pic 30 prolog ld Y not yet N
32512 SVR4 PIC 30 prolog gcc Y option option
32513 EABI TOC 30 prolog gcc Y option option
32517 /* Hash functions for the hash table. */
32519 static unsigned
32520 rs6000_hash_constant (rtx k)
32522 enum rtx_code code = GET_CODE (k);
32523 machine_mode mode = GET_MODE (k);
32524 unsigned result = (code << 3) ^ mode;
32525 const char *format;
32526 int flen, fidx;
32528 format = GET_RTX_FORMAT (code);
32529 flen = strlen (format);
32530 fidx = 0;
32532 switch (code)
32534 case LABEL_REF:
32535 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
32537 case CONST_WIDE_INT:
32539 int i;
32540 flen = CONST_WIDE_INT_NUNITS (k);
32541 for (i = 0; i < flen; i++)
32542 result = result * 613 + CONST_WIDE_INT_ELT (k, i);
32543 return result;
32546 case CONST_DOUBLE:
32547 if (mode != VOIDmode)
32548 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
32549 flen = 2;
32550 break;
32552 case CODE_LABEL:
32553 fidx = 3;
32554 break;
32556 default:
32557 break;
32560 for (; fidx < flen; fidx++)
32561 switch (format[fidx])
32563 case 's':
32565 unsigned i, len;
32566 const char *str = XSTR (k, fidx);
32567 len = strlen (str);
32568 result = result * 613 + len;
32569 for (i = 0; i < len; i++)
32570 result = result * 613 + (unsigned) str[i];
32571 break;
32573 case 'u':
32574 case 'e':
32575 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
32576 break;
32577 case 'i':
32578 case 'n':
32579 result = result * 613 + (unsigned) XINT (k, fidx);
32580 break;
32581 case 'w':
32582 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
32583 result = result * 613 + (unsigned) XWINT (k, fidx);
32584 else
32586 size_t i;
32587 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
32588 result = result * 613 + (unsigned) (XWINT (k, fidx)
32589 >> CHAR_BIT * i);
32591 break;
32592 case '0':
32593 break;
32594 default:
32595 gcc_unreachable ();
32598 return result;
32601 hashval_t
32602 toc_hasher::hash (toc_hash_struct *thc)
32604 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
32607 /* Compare H1 and H2 for equivalence. */
32609 bool
32610 toc_hasher::equal (toc_hash_struct *h1, toc_hash_struct *h2)
32612 rtx r1 = h1->key;
32613 rtx r2 = h2->key;
32615 if (h1->key_mode != h2->key_mode)
32616 return 0;
32618 return rtx_equal_p (r1, r2);
32621 /* These are the names given by the C++ front-end to vtables, and
32622 vtable-like objects. Ideally, this logic should not be here;
32623 instead, there should be some programmatic way of inquiring as
32624 to whether or not an object is a vtable. */
32626 #define VTABLE_NAME_P(NAME) \
32627 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
32628 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
32629 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
32630 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
32631 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
32633 #ifdef NO_DOLLAR_IN_LABEL
32634 /* Return a GGC-allocated character string translating dollar signs in
32635 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
32637 const char *
32638 rs6000_xcoff_strip_dollar (const char *name)
32640 char *strip, *p;
32641 const char *q;
32642 size_t len;
32644 q = (const char *) strchr (name, '$');
32646 if (q == 0 || q == name)
32647 return name;
32649 len = strlen (name);
32650 strip = XALLOCAVEC (char, len + 1);
32651 strcpy (strip, name);
32652 p = strip + (q - name);
32653 while (p)
32655 *p = '_';
32656 p = strchr (p + 1, '$');
32659 return ggc_alloc_string (strip, len);
32661 #endif
32663 void
32664 rs6000_output_symbol_ref (FILE *file, rtx x)
32666 const char *name = XSTR (x, 0);
32668 /* Currently C++ toc references to vtables can be emitted before it
32669 is decided whether the vtable is public or private. If this is
32670 the case, then the linker will eventually complain that there is
32671 a reference to an unknown section. Thus, for vtables only,
32672 we emit the TOC reference to reference the identifier and not the
32673 symbol. */
32674 if (VTABLE_NAME_P (name))
32676 RS6000_OUTPUT_BASENAME (file, name);
32678 else
32679 assemble_name (file, name);
32682 /* Output a TOC entry. We derive the entry name from what is being
32683 written. */
32685 void
32686 output_toc (FILE *file, rtx x, int labelno, machine_mode mode)
32688 char buf[256];
32689 const char *name = buf;
32690 rtx base = x;
32691 HOST_WIDE_INT offset = 0;
32693 gcc_assert (!TARGET_NO_TOC);
32695 /* When the linker won't eliminate them, don't output duplicate
32696 TOC entries (this happens on AIX if there is any kind of TOC,
32697 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
32698 CODE_LABELs. */
32699 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
32701 struct toc_hash_struct *h;
32703 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
32704 time because GGC is not initialized at that point. */
32705 if (toc_hash_table == NULL)
32706 toc_hash_table = hash_table<toc_hasher>::create_ggc (1021);
32708 h = ggc_alloc<toc_hash_struct> ();
32709 h->key = x;
32710 h->key_mode = mode;
32711 h->labelno = labelno;
32713 toc_hash_struct **found = toc_hash_table->find_slot (h, INSERT);
32714 if (*found == NULL)
32715 *found = h;
32716 else /* This is indeed a duplicate.
32717 Set this label equal to that label. */
32719 fputs ("\t.set ", file);
32720 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
32721 fprintf (file, "%d,", labelno);
32722 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
32723 fprintf (file, "%d\n", ((*found)->labelno));
32725 #ifdef HAVE_AS_TLS
32726 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
32727 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
32728 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
32730 fputs ("\t.set ", file);
32731 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
32732 fprintf (file, "%d,", labelno);
32733 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
32734 fprintf (file, "%d\n", ((*found)->labelno));
32736 #endif
32737 return;
32741 /* If we're going to put a double constant in the TOC, make sure it's
32742 aligned properly when strict alignment is on. */
32743 if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
32744 && STRICT_ALIGNMENT
32745 && GET_MODE_BITSIZE (mode) >= 64
32746 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
32747 ASM_OUTPUT_ALIGN (file, 3);
32750 (*targetm.asm_out.internal_label) (file, "LC", labelno);
32752 /* Handle FP constants specially. Note that if we have a minimal
32753 TOC, things we put here aren't actually in the TOC, so we can allow
32754 FP constants. */
32755 if (GET_CODE (x) == CONST_DOUBLE &&
32756 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode
32757 || GET_MODE (x) == IFmode || GET_MODE (x) == KFmode))
32759 long k[4];
32761 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
32762 REAL_VALUE_TO_TARGET_DECIMAL128 (*CONST_DOUBLE_REAL_VALUE (x), k);
32763 else
32764 REAL_VALUE_TO_TARGET_LONG_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
32766 if (TARGET_64BIT)
32768 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32769 fputs (DOUBLE_INT_ASM_OP, file);
32770 else
32771 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
32772 k[0] & 0xffffffff, k[1] & 0xffffffff,
32773 k[2] & 0xffffffff, k[3] & 0xffffffff);
32774 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
32775 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
32776 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
32777 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
32778 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
32779 return;
32781 else
32783 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32784 fputs ("\t.long ", file);
32785 else
32786 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
32787 k[0] & 0xffffffff, k[1] & 0xffffffff,
32788 k[2] & 0xffffffff, k[3] & 0xffffffff);
32789 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
32790 k[0] & 0xffffffff, k[1] & 0xffffffff,
32791 k[2] & 0xffffffff, k[3] & 0xffffffff);
32792 return;
32795 else if (GET_CODE (x) == CONST_DOUBLE &&
32796 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
32798 long k[2];
32800 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
32801 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (x), k);
32802 else
32803 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
32805 if (TARGET_64BIT)
32807 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32808 fputs (DOUBLE_INT_ASM_OP, file);
32809 else
32810 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
32811 k[0] & 0xffffffff, k[1] & 0xffffffff);
32812 fprintf (file, "0x%lx%08lx\n",
32813 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
32814 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
32815 return;
32817 else
32819 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32820 fputs ("\t.long ", file);
32821 else
32822 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
32823 k[0] & 0xffffffff, k[1] & 0xffffffff);
32824 fprintf (file, "0x%lx,0x%lx\n",
32825 k[0] & 0xffffffff, k[1] & 0xffffffff);
32826 return;
32829 else if (GET_CODE (x) == CONST_DOUBLE &&
32830 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
32832 long l;
32834 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
32835 REAL_VALUE_TO_TARGET_DECIMAL32 (*CONST_DOUBLE_REAL_VALUE (x), l);
32836 else
32837 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
32839 if (TARGET_64BIT)
32841 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32842 fputs (DOUBLE_INT_ASM_OP, file);
32843 else
32844 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
32845 if (WORDS_BIG_ENDIAN)
32846 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
32847 else
32848 fprintf (file, "0x%lx\n", l & 0xffffffff);
32849 return;
32851 else
32853 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32854 fputs ("\t.long ", file);
32855 else
32856 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
32857 fprintf (file, "0x%lx\n", l & 0xffffffff);
32858 return;
32861 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
32863 unsigned HOST_WIDE_INT low;
32864 HOST_WIDE_INT high;
32866 low = INTVAL (x) & 0xffffffff;
32867 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
32869 /* TOC entries are always Pmode-sized, so when big-endian
32870 smaller integer constants in the TOC need to be padded.
32871 (This is still a win over putting the constants in
32872 a separate constant pool, because then we'd have
32873 to have both a TOC entry _and_ the actual constant.)
32875 For a 32-bit target, CONST_INT values are loaded and shifted
32876 entirely within `low' and can be stored in one TOC entry. */
32878 /* It would be easy to make this work, but it doesn't now. */
32879 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
32881 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
32883 low |= high << 32;
32884 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
32885 high = (HOST_WIDE_INT) low >> 32;
32886 low &= 0xffffffff;
32889 if (TARGET_64BIT)
32891 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32892 fputs (DOUBLE_INT_ASM_OP, file);
32893 else
32894 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
32895 (long) high & 0xffffffff, (long) low & 0xffffffff);
32896 fprintf (file, "0x%lx%08lx\n",
32897 (long) high & 0xffffffff, (long) low & 0xffffffff);
32898 return;
32900 else
32902 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
32904 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32905 fputs ("\t.long ", file);
32906 else
32907 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
32908 (long) high & 0xffffffff, (long) low & 0xffffffff);
32909 fprintf (file, "0x%lx,0x%lx\n",
32910 (long) high & 0xffffffff, (long) low & 0xffffffff);
32912 else
32914 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32915 fputs ("\t.long ", file);
32916 else
32917 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
32918 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
32920 return;
32924 if (GET_CODE (x) == CONST)
32926 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
32927 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
32929 base = XEXP (XEXP (x, 0), 0);
32930 offset = INTVAL (XEXP (XEXP (x, 0), 1));
32933 switch (GET_CODE (base))
32935 case SYMBOL_REF:
32936 name = XSTR (base, 0);
32937 break;
32939 case LABEL_REF:
32940 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
32941 CODE_LABEL_NUMBER (XEXP (base, 0)));
32942 break;
32944 case CODE_LABEL:
32945 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
32946 break;
32948 default:
32949 gcc_unreachable ();
32952 if (TARGET_ELF || TARGET_MINIMAL_TOC)
32953 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
32954 else
32956 fputs ("\t.tc ", file);
32957 RS6000_OUTPUT_BASENAME (file, name);
32959 if (offset < 0)
32960 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
32961 else if (offset)
32962 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
32964 /* Mark large TOC symbols on AIX with [TE] so they are mapped
32965 after other TOC symbols, reducing overflow of small TOC access
32966 to [TC] symbols. */
32967 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
32968 ? "[TE]," : "[TC],", file);
32971 /* Currently C++ toc references to vtables can be emitted before it
32972 is decided whether the vtable is public or private. If this is
32973 the case, then the linker will eventually complain that there is
32974 a TOC reference to an unknown section. Thus, for vtables only,
32975 we emit the TOC reference to reference the symbol and not the
32976 section. */
32977 if (VTABLE_NAME_P (name))
32979 RS6000_OUTPUT_BASENAME (file, name);
32980 if (offset < 0)
32981 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
32982 else if (offset > 0)
32983 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
32985 else
32986 output_addr_const (file, x);
32988 #if HAVE_AS_TLS
32989 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF)
32991 switch (SYMBOL_REF_TLS_MODEL (base))
32993 case 0:
32994 break;
32995 case TLS_MODEL_LOCAL_EXEC:
32996 fputs ("@le", file);
32997 break;
32998 case TLS_MODEL_INITIAL_EXEC:
32999 fputs ("@ie", file);
33000 break;
33001 /* Use global-dynamic for local-dynamic. */
33002 case TLS_MODEL_GLOBAL_DYNAMIC:
33003 case TLS_MODEL_LOCAL_DYNAMIC:
33004 putc ('\n', file);
33005 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
33006 fputs ("\t.tc .", file);
33007 RS6000_OUTPUT_BASENAME (file, name);
33008 fputs ("[TC],", file);
33009 output_addr_const (file, x);
33010 fputs ("@m", file);
33011 break;
33012 default:
33013 gcc_unreachable ();
33016 #endif
33018 putc ('\n', file);
33021 /* Output an assembler pseudo-op to write an ASCII string of N characters
33022 starting at P to FILE.
33024 On the RS/6000, we have to do this using the .byte operation and
33025 write out special characters outside the quoted string.
33026 Also, the assembler is broken; very long strings are truncated,
33027 so we must artificially break them up early. */
33029 void
33030 output_ascii (FILE *file, const char *p, int n)
33032 char c;
33033 int i, count_string;
33034 const char *for_string = "\t.byte \"";
33035 const char *for_decimal = "\t.byte ";
33036 const char *to_close = NULL;
33038 count_string = 0;
33039 for (i = 0; i < n; i++)
33041 c = *p++;
33042 if (c >= ' ' && c < 0177)
33044 if (for_string)
33045 fputs (for_string, file);
33046 putc (c, file);
33048 /* Write two quotes to get one. */
33049 if (c == '"')
33051 putc (c, file);
33052 ++count_string;
33055 for_string = NULL;
33056 for_decimal = "\"\n\t.byte ";
33057 to_close = "\"\n";
33058 ++count_string;
33060 if (count_string >= 512)
33062 fputs (to_close, file);
33064 for_string = "\t.byte \"";
33065 for_decimal = "\t.byte ";
33066 to_close = NULL;
33067 count_string = 0;
33070 else
33072 if (for_decimal)
33073 fputs (for_decimal, file);
33074 fprintf (file, "%d", c);
33076 for_string = "\n\t.byte \"";
33077 for_decimal = ", ";
33078 to_close = "\n";
33079 count_string = 0;
33083 /* Now close the string if we have written one. Then end the line. */
33084 if (to_close)
33085 fputs (to_close, file);
33088 /* Generate a unique section name for FILENAME for a section type
33089 represented by SECTION_DESC. Output goes into BUF.
33091 SECTION_DESC can be any string, as long as it is different for each
33092 possible section type.
33094 We name the section in the same manner as xlc. The name begins with an
33095 underscore followed by the filename (after stripping any leading directory
33096 names) with the last period replaced by the string SECTION_DESC. If
33097 FILENAME does not contain a period, SECTION_DESC is appended to the end of
33098 the name. */
33100 void
33101 rs6000_gen_section_name (char **buf, const char *filename,
33102 const char *section_desc)
33104 const char *q, *after_last_slash, *last_period = 0;
33105 char *p;
33106 int len;
33108 after_last_slash = filename;
33109 for (q = filename; *q; q++)
33111 if (*q == '/')
33112 after_last_slash = q + 1;
33113 else if (*q == '.')
33114 last_period = q;
33117 len = strlen (after_last_slash) + strlen (section_desc) + 2;
33118 *buf = (char *) xmalloc (len);
33120 p = *buf;
33121 *p++ = '_';
33123 for (q = after_last_slash; *q; q++)
33125 if (q == last_period)
33127 strcpy (p, section_desc);
33128 p += strlen (section_desc);
33129 break;
33132 else if (ISALNUM (*q))
33133 *p++ = *q;
33136 if (last_period == 0)
33137 strcpy (p, section_desc);
33138 else
33139 *p = '\0';
33142 /* Emit profile function. */
33144 void
33145 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
33147 /* Non-standard profiling for kernels, which just saves LR then calls
33148 _mcount without worrying about arg saves. The idea is to change
33149 the function prologue as little as possible as it isn't easy to
33150 account for arg save/restore code added just for _mcount. */
33151 if (TARGET_PROFILE_KERNEL)
33152 return;
33154 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
33156 #ifndef NO_PROFILE_COUNTERS
33157 # define NO_PROFILE_COUNTERS 0
33158 #endif
33159 if (NO_PROFILE_COUNTERS)
33160 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
33161 LCT_NORMAL, VOIDmode);
33162 else
33164 char buf[30];
33165 const char *label_name;
33166 rtx fun;
33168 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
33169 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
33170 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
33172 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
33173 LCT_NORMAL, VOIDmode, fun, Pmode);
33176 else if (DEFAULT_ABI == ABI_DARWIN)
33178 const char *mcount_name = RS6000_MCOUNT;
33179 int caller_addr_regno = LR_REGNO;
33181 /* Be conservative and always set this, at least for now. */
33182 crtl->uses_pic_offset_table = 1;
33184 #if TARGET_MACHO
33185 /* For PIC code, set up a stub and collect the caller's address
33186 from r0, which is where the prologue puts it. */
33187 if (MACHOPIC_INDIRECT
33188 && crtl->uses_pic_offset_table)
33189 caller_addr_regno = 0;
33190 #endif
33191 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
33192 LCT_NORMAL, VOIDmode,
33193 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
33197 /* Write function profiler code. */
33199 void
33200 output_function_profiler (FILE *file, int labelno)
33202 char buf[100];
33204 switch (DEFAULT_ABI)
33206 default:
33207 gcc_unreachable ();
33209 case ABI_V4:
33210 if (!TARGET_32BIT)
33212 warning (0, "no profiling of 64-bit code for this ABI");
33213 return;
33215 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
33216 fprintf (file, "\tmflr %s\n", reg_names[0]);
33217 if (NO_PROFILE_COUNTERS)
33219 asm_fprintf (file, "\tstw %s,4(%s)\n",
33220 reg_names[0], reg_names[1]);
33222 else if (TARGET_SECURE_PLT && flag_pic)
33224 if (TARGET_LINK_STACK)
33226 char name[32];
33227 get_ppc476_thunk_name (name);
33228 asm_fprintf (file, "\tbl %s\n", name);
33230 else
33231 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
33232 asm_fprintf (file, "\tstw %s,4(%s)\n",
33233 reg_names[0], reg_names[1]);
33234 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
33235 asm_fprintf (file, "\taddis %s,%s,",
33236 reg_names[12], reg_names[12]);
33237 assemble_name (file, buf);
33238 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
33239 assemble_name (file, buf);
33240 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
33242 else if (flag_pic == 1)
33244 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
33245 asm_fprintf (file, "\tstw %s,4(%s)\n",
33246 reg_names[0], reg_names[1]);
33247 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
33248 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
33249 assemble_name (file, buf);
33250 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
33252 else if (flag_pic > 1)
33254 asm_fprintf (file, "\tstw %s,4(%s)\n",
33255 reg_names[0], reg_names[1]);
33256 /* Now, we need to get the address of the label. */
33257 if (TARGET_LINK_STACK)
33259 char name[32];
33260 get_ppc476_thunk_name (name);
33261 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
33262 assemble_name (file, buf);
33263 fputs ("-.\n1:", file);
33264 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
33265 asm_fprintf (file, "\taddi %s,%s,4\n",
33266 reg_names[11], reg_names[11]);
33268 else
33270 fputs ("\tbcl 20,31,1f\n\t.long ", file);
33271 assemble_name (file, buf);
33272 fputs ("-.\n1:", file);
33273 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
33275 asm_fprintf (file, "\tlwz %s,0(%s)\n",
33276 reg_names[0], reg_names[11]);
33277 asm_fprintf (file, "\tadd %s,%s,%s\n",
33278 reg_names[0], reg_names[0], reg_names[11]);
33280 else
33282 asm_fprintf (file, "\tlis %s,", reg_names[12]);
33283 assemble_name (file, buf);
33284 fputs ("@ha\n", file);
33285 asm_fprintf (file, "\tstw %s,4(%s)\n",
33286 reg_names[0], reg_names[1]);
33287 asm_fprintf (file, "\tla %s,", reg_names[0]);
33288 assemble_name (file, buf);
33289 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
33292 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
33293 fprintf (file, "\tbl %s%s\n",
33294 RS6000_MCOUNT, flag_pic ? "@plt" : "");
33295 break;
33297 case ABI_AIX:
33298 case ABI_ELFv2:
33299 case ABI_DARWIN:
33300 /* Don't do anything, done in output_profile_hook (). */
33301 break;
33307 /* The following variable value is the last issued insn. */
33309 static rtx_insn *last_scheduled_insn;
33311 /* The following variable helps to balance issuing of load and
33312 store instructions */
33314 static int load_store_pendulum;
33316 /* The following variable helps pair divide insns during scheduling. */
33317 static int divide_cnt;
33318 /* The following variable helps pair and alternate vector and vector load
33319 insns during scheduling. */
33320 static int vec_pairing;
33323 /* Power4 load update and store update instructions are cracked into a
33324 load or store and an integer insn which are executed in the same cycle.
33325 Branches have their own dispatch slot which does not count against the
33326 GCC issue rate, but it changes the program flow so there are no other
33327 instructions to issue in this cycle. */
33329 static int
33330 rs6000_variable_issue_1 (rtx_insn *insn, int more)
33332 last_scheduled_insn = insn;
33333 if (GET_CODE (PATTERN (insn)) == USE
33334 || GET_CODE (PATTERN (insn)) == CLOBBER)
33336 cached_can_issue_more = more;
33337 return cached_can_issue_more;
33340 if (insn_terminates_group_p (insn, current_group))
33342 cached_can_issue_more = 0;
33343 return cached_can_issue_more;
33346 /* If no reservation, but reach here */
33347 if (recog_memoized (insn) < 0)
33348 return more;
33350 if (rs6000_sched_groups)
33352 if (is_microcoded_insn (insn))
33353 cached_can_issue_more = 0;
33354 else if (is_cracked_insn (insn))
33355 cached_can_issue_more = more > 2 ? more - 2 : 0;
33356 else
33357 cached_can_issue_more = more - 1;
33359 return cached_can_issue_more;
33362 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
33363 return 0;
33365 cached_can_issue_more = more - 1;
33366 return cached_can_issue_more;
33369 static int
33370 rs6000_variable_issue (FILE *stream, int verbose, rtx_insn *insn, int more)
33372 int r = rs6000_variable_issue_1 (insn, more);
33373 if (verbose)
33374 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
33375 return r;
33378 /* Adjust the cost of a scheduling dependency. Return the new cost of
33379 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
33381 static int
33382 rs6000_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
33383 unsigned int)
33385 enum attr_type attr_type;
33387 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
33388 return cost;
33390 switch (dep_type)
33392 case REG_DEP_TRUE:
33394 /* Data dependency; DEP_INSN writes a register that INSN reads
33395 some cycles later. */
33397 /* Separate a load from a narrower, dependent store. */
33398 if ((rs6000_sched_groups || rs6000_cpu_attr == CPU_POWER9)
33399 && GET_CODE (PATTERN (insn)) == SET
33400 && GET_CODE (PATTERN (dep_insn)) == SET
33401 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
33402 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
33403 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
33404 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
33405 return cost + 14;
33407 attr_type = get_attr_type (insn);
33409 switch (attr_type)
33411 case TYPE_JMPREG:
33412 /* Tell the first scheduling pass about the latency between
33413 a mtctr and bctr (and mtlr and br/blr). The first
33414 scheduling pass will not know about this latency since
33415 the mtctr instruction, which has the latency associated
33416 to it, will be generated by reload. */
33417 return 4;
33418 case TYPE_BRANCH:
33419 /* Leave some extra cycles between a compare and its
33420 dependent branch, to inhibit expensive mispredicts. */
33421 if ((rs6000_cpu_attr == CPU_PPC603
33422 || rs6000_cpu_attr == CPU_PPC604
33423 || rs6000_cpu_attr == CPU_PPC604E
33424 || rs6000_cpu_attr == CPU_PPC620
33425 || rs6000_cpu_attr == CPU_PPC630
33426 || rs6000_cpu_attr == CPU_PPC750
33427 || rs6000_cpu_attr == CPU_PPC7400
33428 || rs6000_cpu_attr == CPU_PPC7450
33429 || rs6000_cpu_attr == CPU_PPCE5500
33430 || rs6000_cpu_attr == CPU_PPCE6500
33431 || rs6000_cpu_attr == CPU_POWER4
33432 || rs6000_cpu_attr == CPU_POWER5
33433 || rs6000_cpu_attr == CPU_POWER7
33434 || rs6000_cpu_attr == CPU_POWER8
33435 || rs6000_cpu_attr == CPU_POWER9
33436 || rs6000_cpu_attr == CPU_CELL)
33437 && recog_memoized (dep_insn)
33438 && (INSN_CODE (dep_insn) >= 0))
33440 switch (get_attr_type (dep_insn))
33442 case TYPE_CMP:
33443 case TYPE_FPCOMPARE:
33444 case TYPE_CR_LOGICAL:
33445 case TYPE_DELAYED_CR:
33446 return cost + 2;
33447 case TYPE_EXTS:
33448 case TYPE_MUL:
33449 if (get_attr_dot (dep_insn) == DOT_YES)
33450 return cost + 2;
33451 else
33452 break;
33453 case TYPE_SHIFT:
33454 if (get_attr_dot (dep_insn) == DOT_YES
33455 && get_attr_var_shift (dep_insn) == VAR_SHIFT_NO)
33456 return cost + 2;
33457 else
33458 break;
33459 default:
33460 break;
33462 break;
33464 case TYPE_STORE:
33465 case TYPE_FPSTORE:
33466 if ((rs6000_cpu == PROCESSOR_POWER6)
33467 && recog_memoized (dep_insn)
33468 && (INSN_CODE (dep_insn) >= 0))
33471 if (GET_CODE (PATTERN (insn)) != SET)
33472 /* If this happens, we have to extend this to schedule
33473 optimally. Return default for now. */
33474 return cost;
33476 /* Adjust the cost for the case where the value written
33477 by a fixed point operation is used as the address
33478 gen value on a store. */
33479 switch (get_attr_type (dep_insn))
33481 case TYPE_LOAD:
33482 case TYPE_CNTLZ:
33484 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33485 return get_attr_sign_extend (dep_insn)
33486 == SIGN_EXTEND_YES ? 6 : 4;
33487 break;
33489 case TYPE_SHIFT:
33491 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33492 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
33493 6 : 3;
33494 break;
33496 case TYPE_INTEGER:
33497 case TYPE_ADD:
33498 case TYPE_LOGICAL:
33499 case TYPE_EXTS:
33500 case TYPE_INSERT:
33502 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33503 return 3;
33504 break;
33506 case TYPE_STORE:
33507 case TYPE_FPLOAD:
33508 case TYPE_FPSTORE:
33510 if (get_attr_update (dep_insn) == UPDATE_YES
33511 && ! rs6000_store_data_bypass_p (dep_insn, insn))
33512 return 3;
33513 break;
33515 case TYPE_MUL:
33517 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33518 return 17;
33519 break;
33521 case TYPE_DIV:
33523 if (! rs6000_store_data_bypass_p (dep_insn, insn))
33524 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
33525 break;
33527 default:
33528 break;
33531 break;
33533 case TYPE_LOAD:
33534 if ((rs6000_cpu == PROCESSOR_POWER6)
33535 && recog_memoized (dep_insn)
33536 && (INSN_CODE (dep_insn) >= 0))
33539 /* Adjust the cost for the case where the value written
33540 by a fixed point instruction is used within the address
33541 gen portion of a subsequent load(u)(x) */
33542 switch (get_attr_type (dep_insn))
33544 case TYPE_LOAD:
33545 case TYPE_CNTLZ:
33547 if (set_to_load_agen (dep_insn, insn))
33548 return get_attr_sign_extend (dep_insn)
33549 == SIGN_EXTEND_YES ? 6 : 4;
33550 break;
33552 case TYPE_SHIFT:
33554 if (set_to_load_agen (dep_insn, insn))
33555 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
33556 6 : 3;
33557 break;
33559 case TYPE_INTEGER:
33560 case TYPE_ADD:
33561 case TYPE_LOGICAL:
33562 case TYPE_EXTS:
33563 case TYPE_INSERT:
33565 if (set_to_load_agen (dep_insn, insn))
33566 return 3;
33567 break;
33569 case TYPE_STORE:
33570 case TYPE_FPLOAD:
33571 case TYPE_FPSTORE:
33573 if (get_attr_update (dep_insn) == UPDATE_YES
33574 && set_to_load_agen (dep_insn, insn))
33575 return 3;
33576 break;
33578 case TYPE_MUL:
33580 if (set_to_load_agen (dep_insn, insn))
33581 return 17;
33582 break;
33584 case TYPE_DIV:
33586 if (set_to_load_agen (dep_insn, insn))
33587 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
33588 break;
33590 default:
33591 break;
33594 break;
33596 case TYPE_FPLOAD:
33597 if ((rs6000_cpu == PROCESSOR_POWER6)
33598 && get_attr_update (insn) == UPDATE_NO
33599 && recog_memoized (dep_insn)
33600 && (INSN_CODE (dep_insn) >= 0)
33601 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
33602 return 2;
33604 default:
33605 break;
33608 /* Fall out to return default cost. */
33610 break;
33612 case REG_DEP_OUTPUT:
33613 /* Output dependency; DEP_INSN writes a register that INSN writes some
33614 cycles later. */
33615 if ((rs6000_cpu == PROCESSOR_POWER6)
33616 && recog_memoized (dep_insn)
33617 && (INSN_CODE (dep_insn) >= 0))
33619 attr_type = get_attr_type (insn);
33621 switch (attr_type)
33623 case TYPE_FP:
33624 case TYPE_FPSIMPLE:
33625 if (get_attr_type (dep_insn) == TYPE_FP
33626 || get_attr_type (dep_insn) == TYPE_FPSIMPLE)
33627 return 1;
33628 break;
33629 case TYPE_FPLOAD:
33630 if (get_attr_update (insn) == UPDATE_NO
33631 && get_attr_type (dep_insn) == TYPE_MFFGPR)
33632 return 2;
33633 break;
33634 default:
33635 break;
33638 /* Fall through, no cost for output dependency. */
33639 /* FALLTHRU */
33641 case REG_DEP_ANTI:
33642 /* Anti dependency; DEP_INSN reads a register that INSN writes some
33643 cycles later. */
33644 return 0;
33646 default:
33647 gcc_unreachable ();
33650 return cost;
33653 /* Debug version of rs6000_adjust_cost. */
33655 static int
33656 rs6000_debug_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn,
33657 int cost, unsigned int dw)
33659 int ret = rs6000_adjust_cost (insn, dep_type, dep_insn, cost, dw);
33661 if (ret != cost)
33663 const char *dep;
33665 switch (dep_type)
33667 default: dep = "unknown depencency"; break;
33668 case REG_DEP_TRUE: dep = "data dependency"; break;
33669 case REG_DEP_OUTPUT: dep = "output dependency"; break;
33670 case REG_DEP_ANTI: dep = "anti depencency"; break;
33673 fprintf (stderr,
33674 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
33675 "%s, insn:\n", ret, cost, dep);
33677 debug_rtx (insn);
33680 return ret;
33683 /* The function returns a true if INSN is microcoded.
33684 Return false otherwise. */
33686 static bool
33687 is_microcoded_insn (rtx_insn *insn)
33689 if (!insn || !NONDEBUG_INSN_P (insn)
33690 || GET_CODE (PATTERN (insn)) == USE
33691 || GET_CODE (PATTERN (insn)) == CLOBBER)
33692 return false;
33694 if (rs6000_cpu_attr == CPU_CELL)
33695 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
33697 if (rs6000_sched_groups
33698 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
33700 enum attr_type type = get_attr_type (insn);
33701 if ((type == TYPE_LOAD
33702 && get_attr_update (insn) == UPDATE_YES
33703 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
33704 || ((type == TYPE_LOAD || type == TYPE_STORE)
33705 && get_attr_update (insn) == UPDATE_YES
33706 && get_attr_indexed (insn) == INDEXED_YES)
33707 || type == TYPE_MFCR)
33708 return true;
33711 return false;
33714 /* The function returns true if INSN is cracked into 2 instructions
33715 by the processor (and therefore occupies 2 issue slots). */
33717 static bool
33718 is_cracked_insn (rtx_insn *insn)
33720 if (!insn || !NONDEBUG_INSN_P (insn)
33721 || GET_CODE (PATTERN (insn)) == USE
33722 || GET_CODE (PATTERN (insn)) == CLOBBER)
33723 return false;
33725 if (rs6000_sched_groups
33726 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
33728 enum attr_type type = get_attr_type (insn);
33729 if ((type == TYPE_LOAD
33730 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
33731 && get_attr_update (insn) == UPDATE_NO)
33732 || (type == TYPE_LOAD
33733 && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
33734 && get_attr_update (insn) == UPDATE_YES
33735 && get_attr_indexed (insn) == INDEXED_NO)
33736 || (type == TYPE_STORE
33737 && get_attr_update (insn) == UPDATE_YES
33738 && get_attr_indexed (insn) == INDEXED_NO)
33739 || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
33740 && get_attr_update (insn) == UPDATE_YES)
33741 || type == TYPE_DELAYED_CR
33742 || (type == TYPE_EXTS
33743 && get_attr_dot (insn) == DOT_YES)
33744 || (type == TYPE_SHIFT
33745 && get_attr_dot (insn) == DOT_YES
33746 && get_attr_var_shift (insn) == VAR_SHIFT_NO)
33747 || (type == TYPE_MUL
33748 && get_attr_dot (insn) == DOT_YES)
33749 || type == TYPE_DIV
33750 || (type == TYPE_INSERT
33751 && get_attr_size (insn) == SIZE_32))
33752 return true;
33755 return false;
33758 /* The function returns true if INSN can be issued only from
33759 the branch slot. */
33761 static bool
33762 is_branch_slot_insn (rtx_insn *insn)
33764 if (!insn || !NONDEBUG_INSN_P (insn)
33765 || GET_CODE (PATTERN (insn)) == USE
33766 || GET_CODE (PATTERN (insn)) == CLOBBER)
33767 return false;
33769 if (rs6000_sched_groups)
33771 enum attr_type type = get_attr_type (insn);
33772 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
33773 return true;
33774 return false;
33777 return false;
33780 /* The function returns true if out_inst sets a value that is
33781 used in the address generation computation of in_insn */
33782 static bool
33783 set_to_load_agen (rtx_insn *out_insn, rtx_insn *in_insn)
33785 rtx out_set, in_set;
33787 /* For performance reasons, only handle the simple case where
33788 both loads are a single_set. */
33789 out_set = single_set (out_insn);
33790 if (out_set)
33792 in_set = single_set (in_insn);
33793 if (in_set)
33794 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
33797 return false;
33800 /* Try to determine base/offset/size parts of the given MEM.
33801 Return true if successful, false if all the values couldn't
33802 be determined.
33804 This function only looks for REG or REG+CONST address forms.
33805 REG+REG address form will return false. */
33807 static bool
33808 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
33809 HOST_WIDE_INT *size)
33811 rtx addr_rtx;
33812 if MEM_SIZE_KNOWN_P (mem)
33813 *size = MEM_SIZE (mem);
33814 else
33815 return false;
33817 addr_rtx = (XEXP (mem, 0));
33818 if (GET_CODE (addr_rtx) == PRE_MODIFY)
33819 addr_rtx = XEXP (addr_rtx, 1);
33821 *offset = 0;
33822 while (GET_CODE (addr_rtx) == PLUS
33823 && CONST_INT_P (XEXP (addr_rtx, 1)))
33825 *offset += INTVAL (XEXP (addr_rtx, 1));
33826 addr_rtx = XEXP (addr_rtx, 0);
33828 if (!REG_P (addr_rtx))
33829 return false;
33831 *base = addr_rtx;
33832 return true;
33835 /* The function returns true if the target storage location of
33836 mem1 is adjacent to the target storage location of mem2 */
33837 /* Return 1 if memory locations are adjacent. */
33839 static bool
33840 adjacent_mem_locations (rtx mem1, rtx mem2)
33842 rtx reg1, reg2;
33843 HOST_WIDE_INT off1, size1, off2, size2;
33845 if (get_memref_parts (mem1, &reg1, &off1, &size1)
33846 && get_memref_parts (mem2, &reg2, &off2, &size2))
33847 return ((REGNO (reg1) == REGNO (reg2))
33848 && ((off1 + size1 == off2)
33849 || (off2 + size2 == off1)));
33851 return false;
33854 /* This function returns true if it can be determined that the two MEM
33855 locations overlap by at least 1 byte based on base reg/offset/size. */
33857 static bool
33858 mem_locations_overlap (rtx mem1, rtx mem2)
33860 rtx reg1, reg2;
33861 HOST_WIDE_INT off1, size1, off2, size2;
33863 if (get_memref_parts (mem1, &reg1, &off1, &size1)
33864 && get_memref_parts (mem2, &reg2, &off2, &size2))
33865 return ((REGNO (reg1) == REGNO (reg2))
33866 && (((off1 <= off2) && (off1 + size1 > off2))
33867 || ((off2 <= off1) && (off2 + size2 > off1))));
33869 return false;
33872 /* A C statement (sans semicolon) to update the integer scheduling
33873 priority INSN_PRIORITY (INSN). Increase the priority to execute the
33874 INSN earlier, reduce the priority to execute INSN later. Do not
33875 define this macro if you do not need to adjust the scheduling
33876 priorities of insns. */
33878 static int
33879 rs6000_adjust_priority (rtx_insn *insn ATTRIBUTE_UNUSED, int priority)
33881 rtx load_mem, str_mem;
33882 /* On machines (like the 750) which have asymmetric integer units,
33883 where one integer unit can do multiply and divides and the other
33884 can't, reduce the priority of multiply/divide so it is scheduled
33885 before other integer operations. */
33887 #if 0
33888 if (! INSN_P (insn))
33889 return priority;
33891 if (GET_CODE (PATTERN (insn)) == USE)
33892 return priority;
33894 switch (rs6000_cpu_attr) {
33895 case CPU_PPC750:
33896 switch (get_attr_type (insn))
33898 default:
33899 break;
33901 case TYPE_MUL:
33902 case TYPE_DIV:
33903 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
33904 priority, priority);
33905 if (priority >= 0 && priority < 0x01000000)
33906 priority >>= 3;
33907 break;
33910 #endif
33912 if (insn_must_be_first_in_group (insn)
33913 && reload_completed
33914 && current_sched_info->sched_max_insns_priority
33915 && rs6000_sched_restricted_insns_priority)
33918 /* Prioritize insns that can be dispatched only in the first
33919 dispatch slot. */
33920 if (rs6000_sched_restricted_insns_priority == 1)
33921 /* Attach highest priority to insn. This means that in
33922 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
33923 precede 'priority' (critical path) considerations. */
33924 return current_sched_info->sched_max_insns_priority;
33925 else if (rs6000_sched_restricted_insns_priority == 2)
33926 /* Increase priority of insn by a minimal amount. This means that in
33927 haifa-sched.c:ready_sort(), only 'priority' (critical path)
33928 considerations precede dispatch-slot restriction considerations. */
33929 return (priority + 1);
33932 if (rs6000_cpu == PROCESSOR_POWER6
33933 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
33934 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
33935 /* Attach highest priority to insn if the scheduler has just issued two
33936 stores and this instruction is a load, or two loads and this instruction
33937 is a store. Power6 wants loads and stores scheduled alternately
33938 when possible */
33939 return current_sched_info->sched_max_insns_priority;
33941 return priority;
33944 /* Return true if the instruction is nonpipelined on the Cell. */
33945 static bool
33946 is_nonpipeline_insn (rtx_insn *insn)
33948 enum attr_type type;
33949 if (!insn || !NONDEBUG_INSN_P (insn)
33950 || GET_CODE (PATTERN (insn)) == USE
33951 || GET_CODE (PATTERN (insn)) == CLOBBER)
33952 return false;
33954 type = get_attr_type (insn);
33955 if (type == TYPE_MUL
33956 || type == TYPE_DIV
33957 || type == TYPE_SDIV
33958 || type == TYPE_DDIV
33959 || type == TYPE_SSQRT
33960 || type == TYPE_DSQRT
33961 || type == TYPE_MFCR
33962 || type == TYPE_MFCRF
33963 || type == TYPE_MFJMPR)
33965 return true;
33967 return false;
33971 /* Return how many instructions the machine can issue per cycle. */
33973 static int
33974 rs6000_issue_rate (void)
33976 /* Unless scheduling for register pressure, use issue rate of 1 for
33977 first scheduling pass to decrease degradation. */
33978 if (!reload_completed && !flag_sched_pressure)
33979 return 1;
33981 switch (rs6000_cpu_attr) {
33982 case CPU_RS64A:
33983 case CPU_PPC601: /* ? */
33984 case CPU_PPC7450:
33985 return 3;
33986 case CPU_PPC440:
33987 case CPU_PPC603:
33988 case CPU_PPC750:
33989 case CPU_PPC7400:
33990 case CPU_PPC8540:
33991 case CPU_PPC8548:
33992 case CPU_CELL:
33993 case CPU_PPCE300C2:
33994 case CPU_PPCE300C3:
33995 case CPU_PPCE500MC:
33996 case CPU_PPCE500MC64:
33997 case CPU_PPCE5500:
33998 case CPU_PPCE6500:
33999 case CPU_TITAN:
34000 return 2;
34001 case CPU_PPC476:
34002 case CPU_PPC604:
34003 case CPU_PPC604E:
34004 case CPU_PPC620:
34005 case CPU_PPC630:
34006 return 4;
34007 case CPU_POWER4:
34008 case CPU_POWER5:
34009 case CPU_POWER6:
34010 case CPU_POWER7:
34011 return 5;
34012 case CPU_POWER8:
34013 return 7;
34014 case CPU_POWER9:
34015 return 6;
34016 default:
34017 return 1;
34021 /* Return how many instructions to look ahead for better insn
34022 scheduling. */
34024 static int
34025 rs6000_use_sched_lookahead (void)
34027 switch (rs6000_cpu_attr)
34029 case CPU_PPC8540:
34030 case CPU_PPC8548:
34031 return 4;
34033 case CPU_CELL:
34034 return (reload_completed ? 8 : 0);
34036 default:
34037 return 0;
34041 /* We are choosing insn from the ready queue. Return zero if INSN can be
34042 chosen. */
34043 static int
34044 rs6000_use_sched_lookahead_guard (rtx_insn *insn, int ready_index)
34046 if (ready_index == 0)
34047 return 0;
34049 if (rs6000_cpu_attr != CPU_CELL)
34050 return 0;
34052 gcc_assert (insn != NULL_RTX && INSN_P (insn));
34054 if (!reload_completed
34055 || is_nonpipeline_insn (insn)
34056 || is_microcoded_insn (insn))
34057 return 1;
34059 return 0;
34062 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
34063 and return true. */
34065 static bool
34066 find_mem_ref (rtx pat, rtx *mem_ref)
34068 const char * fmt;
34069 int i, j;
34071 /* stack_tie does not produce any real memory traffic. */
34072 if (tie_operand (pat, VOIDmode))
34073 return false;
34075 if (GET_CODE (pat) == MEM)
34077 *mem_ref = pat;
34078 return true;
34081 /* Recursively process the pattern. */
34082 fmt = GET_RTX_FORMAT (GET_CODE (pat));
34084 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
34086 if (fmt[i] == 'e')
34088 if (find_mem_ref (XEXP (pat, i), mem_ref))
34089 return true;
34091 else if (fmt[i] == 'E')
34092 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
34094 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
34095 return true;
34099 return false;
34102 /* Determine if PAT is a PATTERN of a load insn. */
34104 static bool
34105 is_load_insn1 (rtx pat, rtx *load_mem)
34107 if (!pat || pat == NULL_RTX)
34108 return false;
34110 if (GET_CODE (pat) == SET)
34111 return find_mem_ref (SET_SRC (pat), load_mem);
34113 if (GET_CODE (pat) == PARALLEL)
34115 int i;
34117 for (i = 0; i < XVECLEN (pat, 0); i++)
34118 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
34119 return true;
34122 return false;
34125 /* Determine if INSN loads from memory. */
34127 static bool
34128 is_load_insn (rtx insn, rtx *load_mem)
34130 if (!insn || !INSN_P (insn))
34131 return false;
34133 if (CALL_P (insn))
34134 return false;
34136 return is_load_insn1 (PATTERN (insn), load_mem);
34139 /* Determine if PAT is a PATTERN of a store insn. */
34141 static bool
34142 is_store_insn1 (rtx pat, rtx *str_mem)
34144 if (!pat || pat == NULL_RTX)
34145 return false;
34147 if (GET_CODE (pat) == SET)
34148 return find_mem_ref (SET_DEST (pat), str_mem);
34150 if (GET_CODE (pat) == PARALLEL)
34152 int i;
34154 for (i = 0; i < XVECLEN (pat, 0); i++)
34155 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
34156 return true;
34159 return false;
34162 /* Determine if INSN stores to memory. */
34164 static bool
34165 is_store_insn (rtx insn, rtx *str_mem)
34167 if (!insn || !INSN_P (insn))
34168 return false;
34170 return is_store_insn1 (PATTERN (insn), str_mem);
34173 /* Return whether TYPE is a Power9 pairable vector instruction type. */
34175 static bool
34176 is_power9_pairable_vec_type (enum attr_type type)
34178 switch (type)
34180 case TYPE_VECSIMPLE:
34181 case TYPE_VECCOMPLEX:
34182 case TYPE_VECDIV:
34183 case TYPE_VECCMP:
34184 case TYPE_VECPERM:
34185 case TYPE_VECFLOAT:
34186 case TYPE_VECFDIV:
34187 case TYPE_VECDOUBLE:
34188 return true;
34189 default:
34190 break;
34192 return false;
34195 /* Returns whether the dependence between INSN and NEXT is considered
34196 costly by the given target. */
34198 static bool
34199 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
34201 rtx insn;
34202 rtx next;
34203 rtx load_mem, str_mem;
34205 /* If the flag is not enabled - no dependence is considered costly;
34206 allow all dependent insns in the same group.
34207 This is the most aggressive option. */
34208 if (rs6000_sched_costly_dep == no_dep_costly)
34209 return false;
34211 /* If the flag is set to 1 - a dependence is always considered costly;
34212 do not allow dependent instructions in the same group.
34213 This is the most conservative option. */
34214 if (rs6000_sched_costly_dep == all_deps_costly)
34215 return true;
34217 insn = DEP_PRO (dep);
34218 next = DEP_CON (dep);
34220 if (rs6000_sched_costly_dep == store_to_load_dep_costly
34221 && is_load_insn (next, &load_mem)
34222 && is_store_insn (insn, &str_mem))
34223 /* Prevent load after store in the same group. */
34224 return true;
34226 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
34227 && is_load_insn (next, &load_mem)
34228 && is_store_insn (insn, &str_mem)
34229 && DEP_TYPE (dep) == REG_DEP_TRUE
34230 && mem_locations_overlap(str_mem, load_mem))
34231 /* Prevent load after store in the same group if it is a true
34232 dependence. */
34233 return true;
34235 /* The flag is set to X; dependences with latency >= X are considered costly,
34236 and will not be scheduled in the same group. */
34237 if (rs6000_sched_costly_dep <= max_dep_latency
34238 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
34239 return true;
34241 return false;
34244 /* Return the next insn after INSN that is found before TAIL is reached,
34245 skipping any "non-active" insns - insns that will not actually occupy
34246 an issue slot. Return NULL_RTX if such an insn is not found. */
34248 static rtx_insn *
34249 get_next_active_insn (rtx_insn *insn, rtx_insn *tail)
34251 if (insn == NULL_RTX || insn == tail)
34252 return NULL;
34254 while (1)
34256 insn = NEXT_INSN (insn);
34257 if (insn == NULL_RTX || insn == tail)
34258 return NULL;
34260 if (CALL_P (insn)
34261 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
34262 || (NONJUMP_INSN_P (insn)
34263 && GET_CODE (PATTERN (insn)) != USE
34264 && GET_CODE (PATTERN (insn)) != CLOBBER
34265 && INSN_CODE (insn) != CODE_FOR_stack_tie))
34266 break;
34268 return insn;
34271 /* Do Power9 specific sched_reorder2 reordering of ready list. */
34273 static int
34274 power9_sched_reorder2 (rtx_insn **ready, int lastpos)
34276 int pos;
34277 int i;
34278 rtx_insn *tmp;
34279 enum attr_type type, type2;
34281 type = get_attr_type (last_scheduled_insn);
34283 /* Try to issue fixed point divides back-to-back in pairs so they will be
34284 routed to separate execution units and execute in parallel. */
34285 if (type == TYPE_DIV && divide_cnt == 0)
34287 /* First divide has been scheduled. */
34288 divide_cnt = 1;
34290 /* Scan the ready list looking for another divide, if found move it
34291 to the end of the list so it is chosen next. */
34292 pos = lastpos;
34293 while (pos >= 0)
34295 if (recog_memoized (ready[pos]) >= 0
34296 && get_attr_type (ready[pos]) == TYPE_DIV)
34298 tmp = ready[pos];
34299 for (i = pos; i < lastpos; i++)
34300 ready[i] = ready[i + 1];
34301 ready[lastpos] = tmp;
34302 break;
34304 pos--;
34307 else
34309 /* Last insn was the 2nd divide or not a divide, reset the counter. */
34310 divide_cnt = 0;
34312 /* The best dispatch throughput for vector and vector load insns can be
34313 achieved by interleaving a vector and vector load such that they'll
34314 dispatch to the same superslice. If this pairing cannot be achieved
34315 then it is best to pair vector insns together and vector load insns
34316 together.
34318 To aid in this pairing, vec_pairing maintains the current state with
34319 the following values:
34321 0 : Initial state, no vecload/vector pairing has been started.
34323 1 : A vecload or vector insn has been issued and a candidate for
34324 pairing has been found and moved to the end of the ready
34325 list. */
34326 if (type == TYPE_VECLOAD)
34328 /* Issued a vecload. */
34329 if (vec_pairing == 0)
34331 int vecload_pos = -1;
34332 /* We issued a single vecload, look for a vector insn to pair it
34333 with. If one isn't found, try to pair another vecload. */
34334 pos = lastpos;
34335 while (pos >= 0)
34337 if (recog_memoized (ready[pos]) >= 0)
34339 type2 = get_attr_type (ready[pos]);
34340 if (is_power9_pairable_vec_type (type2))
34342 /* Found a vector insn to pair with, move it to the
34343 end of the ready list so it is scheduled next. */
34344 tmp = ready[pos];
34345 for (i = pos; i < lastpos; i++)
34346 ready[i] = ready[i + 1];
34347 ready[lastpos] = tmp;
34348 vec_pairing = 1;
34349 return cached_can_issue_more;
34351 else if (type2 == TYPE_VECLOAD && vecload_pos == -1)
34352 /* Remember position of first vecload seen. */
34353 vecload_pos = pos;
34355 pos--;
34357 if (vecload_pos >= 0)
34359 /* Didn't find a vector to pair with but did find a vecload,
34360 move it to the end of the ready list. */
34361 tmp = ready[vecload_pos];
34362 for (i = vecload_pos; i < lastpos; i++)
34363 ready[i] = ready[i + 1];
34364 ready[lastpos] = tmp;
34365 vec_pairing = 1;
34366 return cached_can_issue_more;
34370 else if (is_power9_pairable_vec_type (type))
34372 /* Issued a vector operation. */
34373 if (vec_pairing == 0)
34375 int vec_pos = -1;
34376 /* We issued a single vector insn, look for a vecload to pair it
34377 with. If one isn't found, try to pair another vector. */
34378 pos = lastpos;
34379 while (pos >= 0)
34381 if (recog_memoized (ready[pos]) >= 0)
34383 type2 = get_attr_type (ready[pos]);
34384 if (type2 == TYPE_VECLOAD)
34386 /* Found a vecload insn to pair with, move it to the
34387 end of the ready list so it is scheduled next. */
34388 tmp = ready[pos];
34389 for (i = pos; i < lastpos; i++)
34390 ready[i] = ready[i + 1];
34391 ready[lastpos] = tmp;
34392 vec_pairing = 1;
34393 return cached_can_issue_more;
34395 else if (is_power9_pairable_vec_type (type2)
34396 && vec_pos == -1)
34397 /* Remember position of first vector insn seen. */
34398 vec_pos = pos;
34400 pos--;
34402 if (vec_pos >= 0)
34404 /* Didn't find a vecload to pair with but did find a vector
34405 insn, move it to the end of the ready list. */
34406 tmp = ready[vec_pos];
34407 for (i = vec_pos; i < lastpos; i++)
34408 ready[i] = ready[i + 1];
34409 ready[lastpos] = tmp;
34410 vec_pairing = 1;
34411 return cached_can_issue_more;
34416 /* We've either finished a vec/vecload pair, couldn't find an insn to
34417 continue the current pair, or the last insn had nothing to do with
34418 with pairing. In any case, reset the state. */
34419 vec_pairing = 0;
34422 return cached_can_issue_more;
34425 /* We are about to begin issuing insns for this clock cycle. */
34427 static int
34428 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
34429 rtx_insn **ready ATTRIBUTE_UNUSED,
34430 int *pn_ready ATTRIBUTE_UNUSED,
34431 int clock_var ATTRIBUTE_UNUSED)
34433 int n_ready = *pn_ready;
34435 if (sched_verbose)
34436 fprintf (dump, "// rs6000_sched_reorder :\n");
34438 /* Reorder the ready list, if the second to last ready insn
34439 is a nonepipeline insn. */
34440 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
34442 if (is_nonpipeline_insn (ready[n_ready - 1])
34443 && (recog_memoized (ready[n_ready - 2]) > 0))
34444 /* Simply swap first two insns. */
34445 std::swap (ready[n_ready - 1], ready[n_ready - 2]);
34448 if (rs6000_cpu == PROCESSOR_POWER6)
34449 load_store_pendulum = 0;
34451 return rs6000_issue_rate ();
34454 /* Like rs6000_sched_reorder, but called after issuing each insn. */
34456 static int
34457 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx_insn **ready,
34458 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
34460 if (sched_verbose)
34461 fprintf (dump, "// rs6000_sched_reorder2 :\n");
34463 /* For Power6, we need to handle some special cases to try and keep the
34464 store queue from overflowing and triggering expensive flushes.
34466 This code monitors how load and store instructions are being issued
34467 and skews the ready list one way or the other to increase the likelihood
34468 that a desired instruction is issued at the proper time.
34470 A couple of things are done. First, we maintain a "load_store_pendulum"
34471 to track the current state of load/store issue.
34473 - If the pendulum is at zero, then no loads or stores have been
34474 issued in the current cycle so we do nothing.
34476 - If the pendulum is 1, then a single load has been issued in this
34477 cycle and we attempt to locate another load in the ready list to
34478 issue with it.
34480 - If the pendulum is -2, then two stores have already been
34481 issued in this cycle, so we increase the priority of the first load
34482 in the ready list to increase it's likelihood of being chosen first
34483 in the next cycle.
34485 - If the pendulum is -1, then a single store has been issued in this
34486 cycle and we attempt to locate another store in the ready list to
34487 issue with it, preferring a store to an adjacent memory location to
34488 facilitate store pairing in the store queue.
34490 - If the pendulum is 2, then two loads have already been
34491 issued in this cycle, so we increase the priority of the first store
34492 in the ready list to increase it's likelihood of being chosen first
34493 in the next cycle.
34495 - If the pendulum < -2 or > 2, then do nothing.
34497 Note: This code covers the most common scenarios. There exist non
34498 load/store instructions which make use of the LSU and which
34499 would need to be accounted for to strictly model the behavior
34500 of the machine. Those instructions are currently unaccounted
34501 for to help minimize compile time overhead of this code.
34503 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
34505 int pos;
34506 int i;
34507 rtx_insn *tmp;
34508 rtx load_mem, str_mem;
34510 if (is_store_insn (last_scheduled_insn, &str_mem))
34511 /* Issuing a store, swing the load_store_pendulum to the left */
34512 load_store_pendulum--;
34513 else if (is_load_insn (last_scheduled_insn, &load_mem))
34514 /* Issuing a load, swing the load_store_pendulum to the right */
34515 load_store_pendulum++;
34516 else
34517 return cached_can_issue_more;
34519 /* If the pendulum is balanced, or there is only one instruction on
34520 the ready list, then all is well, so return. */
34521 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
34522 return cached_can_issue_more;
34524 if (load_store_pendulum == 1)
34526 /* A load has been issued in this cycle. Scan the ready list
34527 for another load to issue with it */
34528 pos = *pn_ready-1;
34530 while (pos >= 0)
34532 if (is_load_insn (ready[pos], &load_mem))
34534 /* Found a load. Move it to the head of the ready list,
34535 and adjust it's priority so that it is more likely to
34536 stay there */
34537 tmp = ready[pos];
34538 for (i=pos; i<*pn_ready-1; i++)
34539 ready[i] = ready[i + 1];
34540 ready[*pn_ready-1] = tmp;
34542 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
34543 INSN_PRIORITY (tmp)++;
34544 break;
34546 pos--;
34549 else if (load_store_pendulum == -2)
34551 /* Two stores have been issued in this cycle. Increase the
34552 priority of the first load in the ready list to favor it for
34553 issuing in the next cycle. */
34554 pos = *pn_ready-1;
34556 while (pos >= 0)
34558 if (is_load_insn (ready[pos], &load_mem)
34559 && !sel_sched_p ()
34560 && INSN_PRIORITY_KNOWN (ready[pos]))
34562 INSN_PRIORITY (ready[pos])++;
34564 /* Adjust the pendulum to account for the fact that a load
34565 was found and increased in priority. This is to prevent
34566 increasing the priority of multiple loads */
34567 load_store_pendulum--;
34569 break;
34571 pos--;
34574 else if (load_store_pendulum == -1)
34576 /* A store has been issued in this cycle. Scan the ready list for
34577 another store to issue with it, preferring a store to an adjacent
34578 memory location */
34579 int first_store_pos = -1;
34581 pos = *pn_ready-1;
34583 while (pos >= 0)
34585 if (is_store_insn (ready[pos], &str_mem))
34587 rtx str_mem2;
34588 /* Maintain the index of the first store found on the
34589 list */
34590 if (first_store_pos == -1)
34591 first_store_pos = pos;
34593 if (is_store_insn (last_scheduled_insn, &str_mem2)
34594 && adjacent_mem_locations (str_mem, str_mem2))
34596 /* Found an adjacent store. Move it to the head of the
34597 ready list, and adjust it's priority so that it is
34598 more likely to stay there */
34599 tmp = ready[pos];
34600 for (i=pos; i<*pn_ready-1; i++)
34601 ready[i] = ready[i + 1];
34602 ready[*pn_ready-1] = tmp;
34604 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
34605 INSN_PRIORITY (tmp)++;
34607 first_store_pos = -1;
34609 break;
34612 pos--;
34615 if (first_store_pos >= 0)
34617 /* An adjacent store wasn't found, but a non-adjacent store was,
34618 so move the non-adjacent store to the front of the ready
34619 list, and adjust its priority so that it is more likely to
34620 stay there. */
34621 tmp = ready[first_store_pos];
34622 for (i=first_store_pos; i<*pn_ready-1; i++)
34623 ready[i] = ready[i + 1];
34624 ready[*pn_ready-1] = tmp;
34625 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
34626 INSN_PRIORITY (tmp)++;
34629 else if (load_store_pendulum == 2)
34631 /* Two loads have been issued in this cycle. Increase the priority
34632 of the first store in the ready list to favor it for issuing in
34633 the next cycle. */
34634 pos = *pn_ready-1;
34636 while (pos >= 0)
34638 if (is_store_insn (ready[pos], &str_mem)
34639 && !sel_sched_p ()
34640 && INSN_PRIORITY_KNOWN (ready[pos]))
34642 INSN_PRIORITY (ready[pos])++;
34644 /* Adjust the pendulum to account for the fact that a store
34645 was found and increased in priority. This is to prevent
34646 increasing the priority of multiple stores */
34647 load_store_pendulum++;
34649 break;
34651 pos--;
34656 /* Do Power9 dependent reordering if necessary. */
34657 if (rs6000_cpu == PROCESSOR_POWER9 && last_scheduled_insn
34658 && recog_memoized (last_scheduled_insn) >= 0)
34659 return power9_sched_reorder2 (ready, *pn_ready - 1);
34661 return cached_can_issue_more;
34664 /* Return whether the presence of INSN causes a dispatch group termination
34665 of group WHICH_GROUP.
34667 If WHICH_GROUP == current_group, this function will return true if INSN
34668 causes the termination of the current group (i.e, the dispatch group to
34669 which INSN belongs). This means that INSN will be the last insn in the
34670 group it belongs to.
34672 If WHICH_GROUP == previous_group, this function will return true if INSN
34673 causes the termination of the previous group (i.e, the dispatch group that
34674 precedes the group to which INSN belongs). This means that INSN will be
34675 the first insn in the group it belongs to). */
34677 static bool
34678 insn_terminates_group_p (rtx_insn *insn, enum group_termination which_group)
34680 bool first, last;
34682 if (! insn)
34683 return false;
34685 first = insn_must_be_first_in_group (insn);
34686 last = insn_must_be_last_in_group (insn);
34688 if (first && last)
34689 return true;
34691 if (which_group == current_group)
34692 return last;
34693 else if (which_group == previous_group)
34694 return first;
34696 return false;
34700 static bool
34701 insn_must_be_first_in_group (rtx_insn *insn)
34703 enum attr_type type;
34705 if (!insn
34706 || NOTE_P (insn)
34707 || DEBUG_INSN_P (insn)
34708 || GET_CODE (PATTERN (insn)) == USE
34709 || GET_CODE (PATTERN (insn)) == CLOBBER)
34710 return false;
34712 switch (rs6000_cpu)
34714 case PROCESSOR_POWER5:
34715 if (is_cracked_insn (insn))
34716 return true;
34717 /* FALLTHRU */
34718 case PROCESSOR_POWER4:
34719 if (is_microcoded_insn (insn))
34720 return true;
34722 if (!rs6000_sched_groups)
34723 return false;
34725 type = get_attr_type (insn);
34727 switch (type)
34729 case TYPE_MFCR:
34730 case TYPE_MFCRF:
34731 case TYPE_MTCR:
34732 case TYPE_DELAYED_CR:
34733 case TYPE_CR_LOGICAL:
34734 case TYPE_MTJMPR:
34735 case TYPE_MFJMPR:
34736 case TYPE_DIV:
34737 case TYPE_LOAD_L:
34738 case TYPE_STORE_C:
34739 case TYPE_ISYNC:
34740 case TYPE_SYNC:
34741 return true;
34742 default:
34743 break;
34745 break;
34746 case PROCESSOR_POWER6:
34747 type = get_attr_type (insn);
34749 switch (type)
34751 case TYPE_EXTS:
34752 case TYPE_CNTLZ:
34753 case TYPE_TRAP:
34754 case TYPE_MUL:
34755 case TYPE_INSERT:
34756 case TYPE_FPCOMPARE:
34757 case TYPE_MFCR:
34758 case TYPE_MTCR:
34759 case TYPE_MFJMPR:
34760 case TYPE_MTJMPR:
34761 case TYPE_ISYNC:
34762 case TYPE_SYNC:
34763 case TYPE_LOAD_L:
34764 case TYPE_STORE_C:
34765 return true;
34766 case TYPE_SHIFT:
34767 if (get_attr_dot (insn) == DOT_NO
34768 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
34769 return true;
34770 else
34771 break;
34772 case TYPE_DIV:
34773 if (get_attr_size (insn) == SIZE_32)
34774 return true;
34775 else
34776 break;
34777 case TYPE_LOAD:
34778 case TYPE_STORE:
34779 case TYPE_FPLOAD:
34780 case TYPE_FPSTORE:
34781 if (get_attr_update (insn) == UPDATE_YES)
34782 return true;
34783 else
34784 break;
34785 default:
34786 break;
34788 break;
34789 case PROCESSOR_POWER7:
34790 type = get_attr_type (insn);
34792 switch (type)
34794 case TYPE_CR_LOGICAL:
34795 case TYPE_MFCR:
34796 case TYPE_MFCRF:
34797 case TYPE_MTCR:
34798 case TYPE_DIV:
34799 case TYPE_ISYNC:
34800 case TYPE_LOAD_L:
34801 case TYPE_STORE_C:
34802 case TYPE_MFJMPR:
34803 case TYPE_MTJMPR:
34804 return true;
34805 case TYPE_MUL:
34806 case TYPE_SHIFT:
34807 case TYPE_EXTS:
34808 if (get_attr_dot (insn) == DOT_YES)
34809 return true;
34810 else
34811 break;
34812 case TYPE_LOAD:
34813 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34814 || get_attr_update (insn) == UPDATE_YES)
34815 return true;
34816 else
34817 break;
34818 case TYPE_STORE:
34819 case TYPE_FPLOAD:
34820 case TYPE_FPSTORE:
34821 if (get_attr_update (insn) == UPDATE_YES)
34822 return true;
34823 else
34824 break;
34825 default:
34826 break;
34828 break;
34829 case PROCESSOR_POWER8:
34830 type = get_attr_type (insn);
34832 switch (type)
34834 case TYPE_CR_LOGICAL:
34835 case TYPE_DELAYED_CR:
34836 case TYPE_MFCR:
34837 case TYPE_MFCRF:
34838 case TYPE_MTCR:
34839 case TYPE_SYNC:
34840 case TYPE_ISYNC:
34841 case TYPE_LOAD_L:
34842 case TYPE_STORE_C:
34843 case TYPE_VECSTORE:
34844 case TYPE_MFJMPR:
34845 case TYPE_MTJMPR:
34846 return true;
34847 case TYPE_SHIFT:
34848 case TYPE_EXTS:
34849 case TYPE_MUL:
34850 if (get_attr_dot (insn) == DOT_YES)
34851 return true;
34852 else
34853 break;
34854 case TYPE_LOAD:
34855 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34856 || get_attr_update (insn) == UPDATE_YES)
34857 return true;
34858 else
34859 break;
34860 case TYPE_STORE:
34861 if (get_attr_update (insn) == UPDATE_YES
34862 && get_attr_indexed (insn) == INDEXED_YES)
34863 return true;
34864 else
34865 break;
34866 default:
34867 break;
34869 break;
34870 default:
34871 break;
34874 return false;
34877 static bool
34878 insn_must_be_last_in_group (rtx_insn *insn)
34880 enum attr_type type;
34882 if (!insn
34883 || NOTE_P (insn)
34884 || DEBUG_INSN_P (insn)
34885 || GET_CODE (PATTERN (insn)) == USE
34886 || GET_CODE (PATTERN (insn)) == CLOBBER)
34887 return false;
34889 switch (rs6000_cpu) {
34890 case PROCESSOR_POWER4:
34891 case PROCESSOR_POWER5:
34892 if (is_microcoded_insn (insn))
34893 return true;
34895 if (is_branch_slot_insn (insn))
34896 return true;
34898 break;
34899 case PROCESSOR_POWER6:
34900 type = get_attr_type (insn);
34902 switch (type)
34904 case TYPE_EXTS:
34905 case TYPE_CNTLZ:
34906 case TYPE_TRAP:
34907 case TYPE_MUL:
34908 case TYPE_FPCOMPARE:
34909 case TYPE_MFCR:
34910 case TYPE_MTCR:
34911 case TYPE_MFJMPR:
34912 case TYPE_MTJMPR:
34913 case TYPE_ISYNC:
34914 case TYPE_SYNC:
34915 case TYPE_LOAD_L:
34916 case TYPE_STORE_C:
34917 return true;
34918 case TYPE_SHIFT:
34919 if (get_attr_dot (insn) == DOT_NO
34920 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
34921 return true;
34922 else
34923 break;
34924 case TYPE_DIV:
34925 if (get_attr_size (insn) == SIZE_32)
34926 return true;
34927 else
34928 break;
34929 default:
34930 break;
34932 break;
34933 case PROCESSOR_POWER7:
34934 type = get_attr_type (insn);
34936 switch (type)
34938 case TYPE_ISYNC:
34939 case TYPE_SYNC:
34940 case TYPE_LOAD_L:
34941 case TYPE_STORE_C:
34942 return true;
34943 case TYPE_LOAD:
34944 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34945 && get_attr_update (insn) == UPDATE_YES)
34946 return true;
34947 else
34948 break;
34949 case TYPE_STORE:
34950 if (get_attr_update (insn) == UPDATE_YES
34951 && get_attr_indexed (insn) == INDEXED_YES)
34952 return true;
34953 else
34954 break;
34955 default:
34956 break;
34958 break;
34959 case PROCESSOR_POWER8:
34960 type = get_attr_type (insn);
34962 switch (type)
34964 case TYPE_MFCR:
34965 case TYPE_MTCR:
34966 case TYPE_ISYNC:
34967 case TYPE_SYNC:
34968 case TYPE_LOAD_L:
34969 case TYPE_STORE_C:
34970 return true;
34971 case TYPE_LOAD:
34972 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
34973 && get_attr_update (insn) == UPDATE_YES)
34974 return true;
34975 else
34976 break;
34977 case TYPE_STORE:
34978 if (get_attr_update (insn) == UPDATE_YES
34979 && get_attr_indexed (insn) == INDEXED_YES)
34980 return true;
34981 else
34982 break;
34983 default:
34984 break;
34986 break;
34987 default:
34988 break;
34991 return false;
34994 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
34995 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
34997 static bool
34998 is_costly_group (rtx *group_insns, rtx next_insn)
35000 int i;
35001 int issue_rate = rs6000_issue_rate ();
35003 for (i = 0; i < issue_rate; i++)
35005 sd_iterator_def sd_it;
35006 dep_t dep;
35007 rtx insn = group_insns[i];
35009 if (!insn)
35010 continue;
35012 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
35014 rtx next = DEP_CON (dep);
35016 if (next == next_insn
35017 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
35018 return true;
35022 return false;
35025 /* Utility of the function redefine_groups.
35026 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
35027 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
35028 to keep it "far" (in a separate group) from GROUP_INSNS, following
35029 one of the following schemes, depending on the value of the flag
35030 -minsert_sched_nops = X:
35031 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
35032 in order to force NEXT_INSN into a separate group.
35033 (2) X < sched_finish_regroup_exact: insert exactly X nops.
35034 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
35035 insertion (has a group just ended, how many vacant issue slots remain in the
35036 last group, and how many dispatch groups were encountered so far). */
35038 static int
35039 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
35040 rtx_insn *next_insn, bool *group_end, int can_issue_more,
35041 int *group_count)
35043 rtx nop;
35044 bool force;
35045 int issue_rate = rs6000_issue_rate ();
35046 bool end = *group_end;
35047 int i;
35049 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
35050 return can_issue_more;
35052 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
35053 return can_issue_more;
35055 force = is_costly_group (group_insns, next_insn);
35056 if (!force)
35057 return can_issue_more;
35059 if (sched_verbose > 6)
35060 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
35061 *group_count ,can_issue_more);
35063 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
35065 if (*group_end)
35066 can_issue_more = 0;
35068 /* Since only a branch can be issued in the last issue_slot, it is
35069 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
35070 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
35071 in this case the last nop will start a new group and the branch
35072 will be forced to the new group. */
35073 if (can_issue_more && !is_branch_slot_insn (next_insn))
35074 can_issue_more--;
35076 /* Do we have a special group ending nop? */
35077 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
35078 || rs6000_cpu_attr == CPU_POWER8)
35080 nop = gen_group_ending_nop ();
35081 emit_insn_before (nop, next_insn);
35082 can_issue_more = 0;
35084 else
35085 while (can_issue_more > 0)
35087 nop = gen_nop ();
35088 emit_insn_before (nop, next_insn);
35089 can_issue_more--;
35092 *group_end = true;
35093 return 0;
35096 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
35098 int n_nops = rs6000_sched_insert_nops;
35100 /* Nops can't be issued from the branch slot, so the effective
35101 issue_rate for nops is 'issue_rate - 1'. */
35102 if (can_issue_more == 0)
35103 can_issue_more = issue_rate;
35104 can_issue_more--;
35105 if (can_issue_more == 0)
35107 can_issue_more = issue_rate - 1;
35108 (*group_count)++;
35109 end = true;
35110 for (i = 0; i < issue_rate; i++)
35112 group_insns[i] = 0;
35116 while (n_nops > 0)
35118 nop = gen_nop ();
35119 emit_insn_before (nop, next_insn);
35120 if (can_issue_more == issue_rate - 1) /* new group begins */
35121 end = false;
35122 can_issue_more--;
35123 if (can_issue_more == 0)
35125 can_issue_more = issue_rate - 1;
35126 (*group_count)++;
35127 end = true;
35128 for (i = 0; i < issue_rate; i++)
35130 group_insns[i] = 0;
35133 n_nops--;
35136 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
35137 can_issue_more++;
35139 /* Is next_insn going to start a new group? */
35140 *group_end
35141 = (end
35142 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
35143 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
35144 || (can_issue_more < issue_rate &&
35145 insn_terminates_group_p (next_insn, previous_group)));
35146 if (*group_end && end)
35147 (*group_count)--;
35149 if (sched_verbose > 6)
35150 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
35151 *group_count, can_issue_more);
35152 return can_issue_more;
35155 return can_issue_more;
35158 /* This function tries to synch the dispatch groups that the compiler "sees"
35159 with the dispatch groups that the processor dispatcher is expected to
35160 form in practice. It tries to achieve this synchronization by forcing the
35161 estimated processor grouping on the compiler (as opposed to the function
35162 'pad_goups' which tries to force the scheduler's grouping on the processor).
35164 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
35165 examines the (estimated) dispatch groups that will be formed by the processor
35166 dispatcher. It marks these group boundaries to reflect the estimated
35167 processor grouping, overriding the grouping that the scheduler had marked.
35168 Depending on the value of the flag '-minsert-sched-nops' this function can
35169 force certain insns into separate groups or force a certain distance between
35170 them by inserting nops, for example, if there exists a "costly dependence"
35171 between the insns.
35173 The function estimates the group boundaries that the processor will form as
35174 follows: It keeps track of how many vacant issue slots are available after
35175 each insn. A subsequent insn will start a new group if one of the following
35176 4 cases applies:
35177 - no more vacant issue slots remain in the current dispatch group.
35178 - only the last issue slot, which is the branch slot, is vacant, but the next
35179 insn is not a branch.
35180 - only the last 2 or less issue slots, including the branch slot, are vacant,
35181 which means that a cracked insn (which occupies two issue slots) can't be
35182 issued in this group.
35183 - less than 'issue_rate' slots are vacant, and the next insn always needs to
35184 start a new group. */
35186 static int
35187 redefine_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
35188 rtx_insn *tail)
35190 rtx_insn *insn, *next_insn;
35191 int issue_rate;
35192 int can_issue_more;
35193 int slot, i;
35194 bool group_end;
35195 int group_count = 0;
35196 rtx *group_insns;
35198 /* Initialize. */
35199 issue_rate = rs6000_issue_rate ();
35200 group_insns = XALLOCAVEC (rtx, issue_rate);
35201 for (i = 0; i < issue_rate; i++)
35203 group_insns[i] = 0;
35205 can_issue_more = issue_rate;
35206 slot = 0;
35207 insn = get_next_active_insn (prev_head_insn, tail);
35208 group_end = false;
35210 while (insn != NULL_RTX)
35212 slot = (issue_rate - can_issue_more);
35213 group_insns[slot] = insn;
35214 can_issue_more =
35215 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
35216 if (insn_terminates_group_p (insn, current_group))
35217 can_issue_more = 0;
35219 next_insn = get_next_active_insn (insn, tail);
35220 if (next_insn == NULL_RTX)
35221 return group_count + 1;
35223 /* Is next_insn going to start a new group? */
35224 group_end
35225 = (can_issue_more == 0
35226 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
35227 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
35228 || (can_issue_more < issue_rate &&
35229 insn_terminates_group_p (next_insn, previous_group)));
35231 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
35232 next_insn, &group_end, can_issue_more,
35233 &group_count);
35235 if (group_end)
35237 group_count++;
35238 can_issue_more = 0;
35239 for (i = 0; i < issue_rate; i++)
35241 group_insns[i] = 0;
35245 if (GET_MODE (next_insn) == TImode && can_issue_more)
35246 PUT_MODE (next_insn, VOIDmode);
35247 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
35248 PUT_MODE (next_insn, TImode);
35250 insn = next_insn;
35251 if (can_issue_more == 0)
35252 can_issue_more = issue_rate;
35253 } /* while */
35255 return group_count;
35258 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
35259 dispatch group boundaries that the scheduler had marked. Pad with nops
35260 any dispatch groups which have vacant issue slots, in order to force the
35261 scheduler's grouping on the processor dispatcher. The function
35262 returns the number of dispatch groups found. */
35264 static int
35265 pad_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
35266 rtx_insn *tail)
35268 rtx_insn *insn, *next_insn;
35269 rtx nop;
35270 int issue_rate;
35271 int can_issue_more;
35272 int group_end;
35273 int group_count = 0;
35275 /* Initialize issue_rate. */
35276 issue_rate = rs6000_issue_rate ();
35277 can_issue_more = issue_rate;
35279 insn = get_next_active_insn (prev_head_insn, tail);
35280 next_insn = get_next_active_insn (insn, tail);
35282 while (insn != NULL_RTX)
35284 can_issue_more =
35285 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
35287 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
35289 if (next_insn == NULL_RTX)
35290 break;
35292 if (group_end)
35294 /* If the scheduler had marked group termination at this location
35295 (between insn and next_insn), and neither insn nor next_insn will
35296 force group termination, pad the group with nops to force group
35297 termination. */
35298 if (can_issue_more
35299 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
35300 && !insn_terminates_group_p (insn, current_group)
35301 && !insn_terminates_group_p (next_insn, previous_group))
35303 if (!is_branch_slot_insn (next_insn))
35304 can_issue_more--;
35306 while (can_issue_more)
35308 nop = gen_nop ();
35309 emit_insn_before (nop, next_insn);
35310 can_issue_more--;
35314 can_issue_more = issue_rate;
35315 group_count++;
35318 insn = next_insn;
35319 next_insn = get_next_active_insn (insn, tail);
35322 return group_count;
35325 /* We're beginning a new block. Initialize data structures as necessary. */
35327 static void
35328 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
35329 int sched_verbose ATTRIBUTE_UNUSED,
35330 int max_ready ATTRIBUTE_UNUSED)
35332 last_scheduled_insn = NULL;
35333 load_store_pendulum = 0;
35334 divide_cnt = 0;
35335 vec_pairing = 0;
35338 /* The following function is called at the end of scheduling BB.
35339 After reload, it inserts nops at insn group bundling. */
35341 static void
35342 rs6000_sched_finish (FILE *dump, int sched_verbose)
35344 int n_groups;
35346 if (sched_verbose)
35347 fprintf (dump, "=== Finishing schedule.\n");
35349 if (reload_completed && rs6000_sched_groups)
35351 /* Do not run sched_finish hook when selective scheduling enabled. */
35352 if (sel_sched_p ())
35353 return;
35355 if (rs6000_sched_insert_nops == sched_finish_none)
35356 return;
35358 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
35359 n_groups = pad_groups (dump, sched_verbose,
35360 current_sched_info->prev_head,
35361 current_sched_info->next_tail);
35362 else
35363 n_groups = redefine_groups (dump, sched_verbose,
35364 current_sched_info->prev_head,
35365 current_sched_info->next_tail);
35367 if (sched_verbose >= 6)
35369 fprintf (dump, "ngroups = %d\n", n_groups);
35370 print_rtl (dump, current_sched_info->prev_head);
35371 fprintf (dump, "Done finish_sched\n");
35376 struct rs6000_sched_context
35378 short cached_can_issue_more;
35379 rtx_insn *last_scheduled_insn;
35380 int load_store_pendulum;
35381 int divide_cnt;
35382 int vec_pairing;
35385 typedef struct rs6000_sched_context rs6000_sched_context_def;
35386 typedef rs6000_sched_context_def *rs6000_sched_context_t;
35388 /* Allocate store for new scheduling context. */
35389 static void *
35390 rs6000_alloc_sched_context (void)
35392 return xmalloc (sizeof (rs6000_sched_context_def));
35395 /* If CLEAN_P is true then initializes _SC with clean data,
35396 and from the global context otherwise. */
35397 static void
35398 rs6000_init_sched_context (void *_sc, bool clean_p)
35400 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
35402 if (clean_p)
35404 sc->cached_can_issue_more = 0;
35405 sc->last_scheduled_insn = NULL;
35406 sc->load_store_pendulum = 0;
35407 sc->divide_cnt = 0;
35408 sc->vec_pairing = 0;
35410 else
35412 sc->cached_can_issue_more = cached_can_issue_more;
35413 sc->last_scheduled_insn = last_scheduled_insn;
35414 sc->load_store_pendulum = load_store_pendulum;
35415 sc->divide_cnt = divide_cnt;
35416 sc->vec_pairing = vec_pairing;
35420 /* Sets the global scheduling context to the one pointed to by _SC. */
35421 static void
35422 rs6000_set_sched_context (void *_sc)
35424 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
35426 gcc_assert (sc != NULL);
35428 cached_can_issue_more = sc->cached_can_issue_more;
35429 last_scheduled_insn = sc->last_scheduled_insn;
35430 load_store_pendulum = sc->load_store_pendulum;
35431 divide_cnt = sc->divide_cnt;
35432 vec_pairing = sc->vec_pairing;
35435 /* Free _SC. */
35436 static void
35437 rs6000_free_sched_context (void *_sc)
35439 gcc_assert (_sc != NULL);
35441 free (_sc);
35444 static bool
35445 rs6000_sched_can_speculate_insn (rtx_insn *insn)
35447 switch (get_attr_type (insn))
35449 case TYPE_DIV:
35450 case TYPE_SDIV:
35451 case TYPE_DDIV:
35452 case TYPE_VECDIV:
35453 case TYPE_SSQRT:
35454 case TYPE_DSQRT:
35455 return false;
35457 default:
35458 return true;
35462 /* Length in units of the trampoline for entering a nested function. */
35465 rs6000_trampoline_size (void)
35467 int ret = 0;
35469 switch (DEFAULT_ABI)
35471 default:
35472 gcc_unreachable ();
35474 case ABI_AIX:
35475 ret = (TARGET_32BIT) ? 12 : 24;
35476 break;
35478 case ABI_ELFv2:
35479 gcc_assert (!TARGET_32BIT);
35480 ret = 32;
35481 break;
35483 case ABI_DARWIN:
35484 case ABI_V4:
35485 ret = (TARGET_32BIT) ? 40 : 48;
35486 break;
35489 return ret;
35492 /* Emit RTL insns to initialize the variable parts of a trampoline.
35493 FNADDR is an RTX for the address of the function's pure code.
35494 CXT is an RTX for the static chain value for the function. */
35496 static void
35497 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
35499 int regsize = (TARGET_32BIT) ? 4 : 8;
35500 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
35501 rtx ctx_reg = force_reg (Pmode, cxt);
35502 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
35504 switch (DEFAULT_ABI)
35506 default:
35507 gcc_unreachable ();
35509 /* Under AIX, just build the 3 word function descriptor */
35510 case ABI_AIX:
35512 rtx fnmem, fn_reg, toc_reg;
35514 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
35515 error ("You cannot take the address of a nested function if you use "
35516 "the -mno-pointers-to-nested-functions option.");
35518 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
35519 fn_reg = gen_reg_rtx (Pmode);
35520 toc_reg = gen_reg_rtx (Pmode);
35522 /* Macro to shorten the code expansions below. */
35523 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
35525 m_tramp = replace_equiv_address (m_tramp, addr);
35527 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
35528 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
35529 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
35530 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
35531 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
35533 # undef MEM_PLUS
35535 break;
35537 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
35538 case ABI_ELFv2:
35539 case ABI_DARWIN:
35540 case ABI_V4:
35541 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
35542 LCT_NORMAL, VOIDmode,
35543 addr, Pmode,
35544 GEN_INT (rs6000_trampoline_size ()), SImode,
35545 fnaddr, Pmode,
35546 ctx_reg, Pmode);
35547 break;
35552 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
35553 identifier as an argument, so the front end shouldn't look it up. */
35555 static bool
35556 rs6000_attribute_takes_identifier_p (const_tree attr_id)
35558 return is_attribute_p ("altivec", attr_id);
35561 /* Handle the "altivec" attribute. The attribute may have
35562 arguments as follows:
35564 __attribute__((altivec(vector__)))
35565 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
35566 __attribute__((altivec(bool__))) (always followed by 'unsigned')
35568 and may appear more than once (e.g., 'vector bool char') in a
35569 given declaration. */
35571 static tree
35572 rs6000_handle_altivec_attribute (tree *node,
35573 tree name ATTRIBUTE_UNUSED,
35574 tree args,
35575 int flags ATTRIBUTE_UNUSED,
35576 bool *no_add_attrs)
35578 tree type = *node, result = NULL_TREE;
35579 machine_mode mode;
35580 int unsigned_p;
35581 char altivec_type
35582 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
35583 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
35584 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
35585 : '?');
35587 while (POINTER_TYPE_P (type)
35588 || TREE_CODE (type) == FUNCTION_TYPE
35589 || TREE_CODE (type) == METHOD_TYPE
35590 || TREE_CODE (type) == ARRAY_TYPE)
35591 type = TREE_TYPE (type);
35593 mode = TYPE_MODE (type);
35595 /* Check for invalid AltiVec type qualifiers. */
35596 if (type == long_double_type_node)
35597 error ("use of %<long double%> in AltiVec types is invalid");
35598 else if (type == boolean_type_node)
35599 error ("use of boolean types in AltiVec types is invalid");
35600 else if (TREE_CODE (type) == COMPLEX_TYPE)
35601 error ("use of %<complex%> in AltiVec types is invalid");
35602 else if (DECIMAL_FLOAT_MODE_P (mode))
35603 error ("use of decimal floating point types in AltiVec types is invalid");
35604 else if (!TARGET_VSX)
35606 if (type == long_unsigned_type_node || type == long_integer_type_node)
35608 if (TARGET_64BIT)
35609 error ("use of %<long%> in AltiVec types is invalid for "
35610 "64-bit code without -mvsx");
35611 else if (rs6000_warn_altivec_long)
35612 warning (0, "use of %<long%> in AltiVec types is deprecated; "
35613 "use %<int%>");
35615 else if (type == long_long_unsigned_type_node
35616 || type == long_long_integer_type_node)
35617 error ("use of %<long long%> in AltiVec types is invalid without "
35618 "-mvsx");
35619 else if (type == double_type_node)
35620 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
35623 switch (altivec_type)
35625 case 'v':
35626 unsigned_p = TYPE_UNSIGNED (type);
35627 switch (mode)
35629 case E_TImode:
35630 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
35631 break;
35632 case E_DImode:
35633 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
35634 break;
35635 case E_SImode:
35636 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
35637 break;
35638 case E_HImode:
35639 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
35640 break;
35641 case E_QImode:
35642 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
35643 break;
35644 case E_SFmode: result = V4SF_type_node; break;
35645 case E_DFmode: result = V2DF_type_node; break;
35646 /* If the user says 'vector int bool', we may be handed the 'bool'
35647 attribute _before_ the 'vector' attribute, and so select the
35648 proper type in the 'b' case below. */
35649 case E_V4SImode: case E_V8HImode: case E_V16QImode: case E_V4SFmode:
35650 case E_V2DImode: case E_V2DFmode:
35651 result = type;
35652 default: break;
35654 break;
35655 case 'b':
35656 switch (mode)
35658 case E_DImode: case E_V2DImode: result = bool_V2DI_type_node; break;
35659 case E_SImode: case E_V4SImode: result = bool_V4SI_type_node; break;
35660 case E_HImode: case E_V8HImode: result = bool_V8HI_type_node; break;
35661 case E_QImode: case E_V16QImode: result = bool_V16QI_type_node;
35662 default: break;
35664 break;
35665 case 'p':
35666 switch (mode)
35668 case E_V8HImode: result = pixel_V8HI_type_node;
35669 default: break;
35671 default: break;
35674 /* Propagate qualifiers attached to the element type
35675 onto the vector type. */
35676 if (result && result != type && TYPE_QUALS (type))
35677 result = build_qualified_type (result, TYPE_QUALS (type));
35679 *no_add_attrs = true; /* No need to hang on to the attribute. */
35681 if (result)
35682 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
35684 return NULL_TREE;
35687 /* AltiVec defines four built-in scalar types that serve as vector
35688 elements; we must teach the compiler how to mangle them. */
35690 static const char *
35691 rs6000_mangle_type (const_tree type)
35693 type = TYPE_MAIN_VARIANT (type);
35695 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
35696 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
35697 return NULL;
35699 if (type == bool_char_type_node) return "U6__boolc";
35700 if (type == bool_short_type_node) return "U6__bools";
35701 if (type == pixel_type_node) return "u7__pixel";
35702 if (type == bool_int_type_node) return "U6__booli";
35703 if (type == bool_long_type_node) return "U6__booll";
35705 /* Use a unique name for __float128 rather than trying to use "e" or "g". Use
35706 "g" for IBM extended double, no matter whether it is long double (using
35707 -mabi=ibmlongdouble) or the distinct __ibm128 type. */
35708 if (TARGET_FLOAT128_TYPE)
35710 if (type == ieee128_float_type_node)
35711 return "U10__float128";
35713 if (type == ibm128_float_type_node)
35714 return "g";
35716 if (type == long_double_type_node && TARGET_LONG_DOUBLE_128)
35717 return (TARGET_IEEEQUAD) ? "U10__float128" : "g";
35720 /* Mangle IBM extended float long double as `g' (__float128) on
35721 powerpc*-linux where long-double-64 previously was the default. */
35722 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
35723 && TARGET_ELF
35724 && TARGET_LONG_DOUBLE_128
35725 && !TARGET_IEEEQUAD)
35726 return "g";
35728 /* For all other types, use normal C++ mangling. */
35729 return NULL;
35732 /* Handle a "longcall" or "shortcall" attribute; arguments as in
35733 struct attribute_spec.handler. */
35735 static tree
35736 rs6000_handle_longcall_attribute (tree *node, tree name,
35737 tree args ATTRIBUTE_UNUSED,
35738 int flags ATTRIBUTE_UNUSED,
35739 bool *no_add_attrs)
35741 if (TREE_CODE (*node) != FUNCTION_TYPE
35742 && TREE_CODE (*node) != FIELD_DECL
35743 && TREE_CODE (*node) != TYPE_DECL)
35745 warning (OPT_Wattributes, "%qE attribute only applies to functions",
35746 name);
35747 *no_add_attrs = true;
35750 return NULL_TREE;
35753 /* Set longcall attributes on all functions declared when
35754 rs6000_default_long_calls is true. */
35755 static void
35756 rs6000_set_default_type_attributes (tree type)
35758 if (rs6000_default_long_calls
35759 && (TREE_CODE (type) == FUNCTION_TYPE
35760 || TREE_CODE (type) == METHOD_TYPE))
35761 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
35762 NULL_TREE,
35763 TYPE_ATTRIBUTES (type));
35765 #if TARGET_MACHO
35766 darwin_set_default_type_attributes (type);
35767 #endif
35770 /* Return a reference suitable for calling a function with the
35771 longcall attribute. */
35774 rs6000_longcall_ref (rtx call_ref)
35776 const char *call_name;
35777 tree node;
35779 if (GET_CODE (call_ref) != SYMBOL_REF)
35780 return call_ref;
35782 /* System V adds '.' to the internal name, so skip them. */
35783 call_name = XSTR (call_ref, 0);
35784 if (*call_name == '.')
35786 while (*call_name == '.')
35787 call_name++;
35789 node = get_identifier (call_name);
35790 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
35793 return force_reg (Pmode, call_ref);
35796 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
35797 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
35798 #endif
35800 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
35801 struct attribute_spec.handler. */
35802 static tree
35803 rs6000_handle_struct_attribute (tree *node, tree name,
35804 tree args ATTRIBUTE_UNUSED,
35805 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
35807 tree *type = NULL;
35808 if (DECL_P (*node))
35810 if (TREE_CODE (*node) == TYPE_DECL)
35811 type = &TREE_TYPE (*node);
35813 else
35814 type = node;
35816 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
35817 || TREE_CODE (*type) == UNION_TYPE)))
35819 warning (OPT_Wattributes, "%qE attribute ignored", name);
35820 *no_add_attrs = true;
35823 else if ((is_attribute_p ("ms_struct", name)
35824 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
35825 || ((is_attribute_p ("gcc_struct", name)
35826 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
35828 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
35829 name);
35830 *no_add_attrs = true;
35833 return NULL_TREE;
35836 static bool
35837 rs6000_ms_bitfield_layout_p (const_tree record_type)
35839 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
35840 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
35841 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
35844 #ifdef USING_ELFOS_H
35846 /* A get_unnamed_section callback, used for switching to toc_section. */
35848 static void
35849 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
35851 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
35852 && TARGET_MINIMAL_TOC)
35854 if (!toc_initialized)
35856 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
35857 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35858 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
35859 fprintf (asm_out_file, "\t.tc ");
35860 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
35861 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
35862 fprintf (asm_out_file, "\n");
35864 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
35865 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35866 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
35867 fprintf (asm_out_file, " = .+32768\n");
35868 toc_initialized = 1;
35870 else
35871 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
35873 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
35875 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
35876 if (!toc_initialized)
35878 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35879 toc_initialized = 1;
35882 else
35884 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
35885 if (!toc_initialized)
35887 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
35888 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
35889 fprintf (asm_out_file, " = .+32768\n");
35890 toc_initialized = 1;
35895 /* Implement TARGET_ASM_INIT_SECTIONS. */
35897 static void
35898 rs6000_elf_asm_init_sections (void)
35900 toc_section
35901 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
35903 sdata2_section
35904 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
35905 SDATA2_SECTION_ASM_OP);
35908 /* Implement TARGET_SELECT_RTX_SECTION. */
35910 static section *
35911 rs6000_elf_select_rtx_section (machine_mode mode, rtx x,
35912 unsigned HOST_WIDE_INT align)
35914 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
35915 return toc_section;
35916 else
35917 return default_elf_select_rtx_section (mode, x, align);
35920 /* For a SYMBOL_REF, set generic flags and then perform some
35921 target-specific processing.
35923 When the AIX ABI is requested on a non-AIX system, replace the
35924 function name with the real name (with a leading .) rather than the
35925 function descriptor name. This saves a lot of overriding code to
35926 read the prefixes. */
35928 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
35929 static void
35930 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
35932 default_encode_section_info (decl, rtl, first);
35934 if (first
35935 && TREE_CODE (decl) == FUNCTION_DECL
35936 && !TARGET_AIX
35937 && DEFAULT_ABI == ABI_AIX)
35939 rtx sym_ref = XEXP (rtl, 0);
35940 size_t len = strlen (XSTR (sym_ref, 0));
35941 char *str = XALLOCAVEC (char, len + 2);
35942 str[0] = '.';
35943 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
35944 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
35948 static inline bool
35949 compare_section_name (const char *section, const char *templ)
35951 int len;
35953 len = strlen (templ);
35954 return (strncmp (section, templ, len) == 0
35955 && (section[len] == 0 || section[len] == '.'));
35958 bool
35959 rs6000_elf_in_small_data_p (const_tree decl)
35961 if (rs6000_sdata == SDATA_NONE)
35962 return false;
35964 /* We want to merge strings, so we never consider them small data. */
35965 if (TREE_CODE (decl) == STRING_CST)
35966 return false;
35968 /* Functions are never in the small data area. */
35969 if (TREE_CODE (decl) == FUNCTION_DECL)
35970 return false;
35972 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
35974 const char *section = DECL_SECTION_NAME (decl);
35975 if (compare_section_name (section, ".sdata")
35976 || compare_section_name (section, ".sdata2")
35977 || compare_section_name (section, ".gnu.linkonce.s")
35978 || compare_section_name (section, ".sbss")
35979 || compare_section_name (section, ".sbss2")
35980 || compare_section_name (section, ".gnu.linkonce.sb")
35981 || strcmp (section, ".PPC.EMB.sdata0") == 0
35982 || strcmp (section, ".PPC.EMB.sbss0") == 0)
35983 return true;
35985 else
35987 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
35989 if (size > 0
35990 && size <= g_switch_value
35991 /* If it's not public, and we're not going to reference it there,
35992 there's no need to put it in the small data section. */
35993 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
35994 return true;
35997 return false;
36000 #endif /* USING_ELFOS_H */
36002 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
36004 static bool
36005 rs6000_use_blocks_for_constant_p (machine_mode mode, const_rtx x)
36007 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
36010 /* Do not place thread-local symbols refs in the object blocks. */
36012 static bool
36013 rs6000_use_blocks_for_decl_p (const_tree decl)
36015 return !DECL_THREAD_LOCAL_P (decl);
36018 /* Return a REG that occurs in ADDR with coefficient 1.
36019 ADDR can be effectively incremented by incrementing REG.
36021 r0 is special and we must not select it as an address
36022 register by this routine since our caller will try to
36023 increment the returned register via an "la" instruction. */
36026 find_addr_reg (rtx addr)
36028 while (GET_CODE (addr) == PLUS)
36030 if (GET_CODE (XEXP (addr, 0)) == REG
36031 && REGNO (XEXP (addr, 0)) != 0)
36032 addr = XEXP (addr, 0);
36033 else if (GET_CODE (XEXP (addr, 1)) == REG
36034 && REGNO (XEXP (addr, 1)) != 0)
36035 addr = XEXP (addr, 1);
36036 else if (CONSTANT_P (XEXP (addr, 0)))
36037 addr = XEXP (addr, 1);
36038 else if (CONSTANT_P (XEXP (addr, 1)))
36039 addr = XEXP (addr, 0);
36040 else
36041 gcc_unreachable ();
36043 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
36044 return addr;
36047 void
36048 rs6000_fatal_bad_address (rtx op)
36050 fatal_insn ("bad address", op);
36053 #if TARGET_MACHO
36055 typedef struct branch_island_d {
36056 tree function_name;
36057 tree label_name;
36058 int line_number;
36059 } branch_island;
36062 static vec<branch_island, va_gc> *branch_islands;
36064 /* Remember to generate a branch island for far calls to the given
36065 function. */
36067 static void
36068 add_compiler_branch_island (tree label_name, tree function_name,
36069 int line_number)
36071 branch_island bi = {function_name, label_name, line_number};
36072 vec_safe_push (branch_islands, bi);
36075 /* Generate far-jump branch islands for everything recorded in
36076 branch_islands. Invoked immediately after the last instruction of
36077 the epilogue has been emitted; the branch islands must be appended
36078 to, and contiguous with, the function body. Mach-O stubs are
36079 generated in machopic_output_stub(). */
36081 static void
36082 macho_branch_islands (void)
36084 char tmp_buf[512];
36086 while (!vec_safe_is_empty (branch_islands))
36088 branch_island *bi = &branch_islands->last ();
36089 const char *label = IDENTIFIER_POINTER (bi->label_name);
36090 const char *name = IDENTIFIER_POINTER (bi->function_name);
36091 char name_buf[512];
36092 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
36093 if (name[0] == '*' || name[0] == '&')
36094 strcpy (name_buf, name+1);
36095 else
36097 name_buf[0] = '_';
36098 strcpy (name_buf+1, name);
36100 strcpy (tmp_buf, "\n");
36101 strcat (tmp_buf, label);
36102 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
36103 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
36104 dbxout_stabd (N_SLINE, bi->line_number);
36105 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
36106 if (flag_pic)
36108 if (TARGET_LINK_STACK)
36110 char name[32];
36111 get_ppc476_thunk_name (name);
36112 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
36113 strcat (tmp_buf, name);
36114 strcat (tmp_buf, "\n");
36115 strcat (tmp_buf, label);
36116 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
36118 else
36120 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
36121 strcat (tmp_buf, label);
36122 strcat (tmp_buf, "_pic\n");
36123 strcat (tmp_buf, label);
36124 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
36127 strcat (tmp_buf, "\taddis r11,r11,ha16(");
36128 strcat (tmp_buf, name_buf);
36129 strcat (tmp_buf, " - ");
36130 strcat (tmp_buf, label);
36131 strcat (tmp_buf, "_pic)\n");
36133 strcat (tmp_buf, "\tmtlr r0\n");
36135 strcat (tmp_buf, "\taddi r12,r11,lo16(");
36136 strcat (tmp_buf, name_buf);
36137 strcat (tmp_buf, " - ");
36138 strcat (tmp_buf, label);
36139 strcat (tmp_buf, "_pic)\n");
36141 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
36143 else
36145 strcat (tmp_buf, ":\nlis r12,hi16(");
36146 strcat (tmp_buf, name_buf);
36147 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
36148 strcat (tmp_buf, name_buf);
36149 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
36151 output_asm_insn (tmp_buf, 0);
36152 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
36153 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
36154 dbxout_stabd (N_SLINE, bi->line_number);
36155 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
36156 branch_islands->pop ();
36160 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
36161 already there or not. */
36163 static int
36164 no_previous_def (tree function_name)
36166 branch_island *bi;
36167 unsigned ix;
36169 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
36170 if (function_name == bi->function_name)
36171 return 0;
36172 return 1;
36175 /* GET_PREV_LABEL gets the label name from the previous definition of
36176 the function. */
36178 static tree
36179 get_prev_label (tree function_name)
36181 branch_island *bi;
36182 unsigned ix;
36184 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
36185 if (function_name == bi->function_name)
36186 return bi->label_name;
36187 return NULL_TREE;
36190 /* INSN is either a function call or a millicode call. It may have an
36191 unconditional jump in its delay slot.
36193 CALL_DEST is the routine we are calling. */
36195 char *
36196 output_call (rtx_insn *insn, rtx *operands, int dest_operand_number,
36197 int cookie_operand_number)
36199 static char buf[256];
36200 if (darwin_emit_branch_islands
36201 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
36202 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
36204 tree labelname;
36205 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
36207 if (no_previous_def (funname))
36209 rtx label_rtx = gen_label_rtx ();
36210 char *label_buf, temp_buf[256];
36211 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
36212 CODE_LABEL_NUMBER (label_rtx));
36213 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
36214 labelname = get_identifier (label_buf);
36215 add_compiler_branch_island (labelname, funname, insn_line (insn));
36217 else
36218 labelname = get_prev_label (funname);
36220 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
36221 instruction will reach 'foo', otherwise link as 'bl L42'".
36222 "L42" should be a 'branch island', that will do a far jump to
36223 'foo'. Branch islands are generated in
36224 macho_branch_islands(). */
36225 sprintf (buf, "jbsr %%z%d,%.246s",
36226 dest_operand_number, IDENTIFIER_POINTER (labelname));
36228 else
36229 sprintf (buf, "bl %%z%d", dest_operand_number);
36230 return buf;
36233 /* Generate PIC and indirect symbol stubs. */
36235 void
36236 machopic_output_stub (FILE *file, const char *symb, const char *stub)
36238 unsigned int length;
36239 char *symbol_name, *lazy_ptr_name;
36240 char *local_label_0;
36241 static int label = 0;
36243 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
36244 symb = (*targetm.strip_name_encoding) (symb);
36247 length = strlen (symb);
36248 symbol_name = XALLOCAVEC (char, length + 32);
36249 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
36251 lazy_ptr_name = XALLOCAVEC (char, length + 32);
36252 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
36254 if (flag_pic == 2)
36255 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
36256 else
36257 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
36259 if (flag_pic == 2)
36261 fprintf (file, "\t.align 5\n");
36263 fprintf (file, "%s:\n", stub);
36264 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
36266 label++;
36267 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
36268 sprintf (local_label_0, "\"L%011d$spb\"", label);
36270 fprintf (file, "\tmflr r0\n");
36271 if (TARGET_LINK_STACK)
36273 char name[32];
36274 get_ppc476_thunk_name (name);
36275 fprintf (file, "\tbl %s\n", name);
36276 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
36278 else
36280 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
36281 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
36283 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
36284 lazy_ptr_name, local_label_0);
36285 fprintf (file, "\tmtlr r0\n");
36286 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
36287 (TARGET_64BIT ? "ldu" : "lwzu"),
36288 lazy_ptr_name, local_label_0);
36289 fprintf (file, "\tmtctr r12\n");
36290 fprintf (file, "\tbctr\n");
36292 else
36294 fprintf (file, "\t.align 4\n");
36296 fprintf (file, "%s:\n", stub);
36297 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
36299 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
36300 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
36301 (TARGET_64BIT ? "ldu" : "lwzu"),
36302 lazy_ptr_name);
36303 fprintf (file, "\tmtctr r12\n");
36304 fprintf (file, "\tbctr\n");
36307 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
36308 fprintf (file, "%s:\n", lazy_ptr_name);
36309 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
36310 fprintf (file, "%sdyld_stub_binding_helper\n",
36311 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
36314 /* Legitimize PIC addresses. If the address is already
36315 position-independent, we return ORIG. Newly generated
36316 position-independent addresses go into a reg. This is REG if non
36317 zero, otherwise we allocate register(s) as necessary. */
36319 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
36322 rs6000_machopic_legitimize_pic_address (rtx orig, machine_mode mode,
36323 rtx reg)
36325 rtx base, offset;
36327 if (reg == NULL && ! reload_in_progress && ! reload_completed)
36328 reg = gen_reg_rtx (Pmode);
36330 if (GET_CODE (orig) == CONST)
36332 rtx reg_temp;
36334 if (GET_CODE (XEXP (orig, 0)) == PLUS
36335 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
36336 return orig;
36338 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
36340 /* Use a different reg for the intermediate value, as
36341 it will be marked UNCHANGING. */
36342 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
36343 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
36344 Pmode, reg_temp);
36345 offset =
36346 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
36347 Pmode, reg);
36349 if (GET_CODE (offset) == CONST_INT)
36351 if (SMALL_INT (offset))
36352 return plus_constant (Pmode, base, INTVAL (offset));
36353 else if (! reload_in_progress && ! reload_completed)
36354 offset = force_reg (Pmode, offset);
36355 else
36357 rtx mem = force_const_mem (Pmode, orig);
36358 return machopic_legitimize_pic_address (mem, Pmode, reg);
36361 return gen_rtx_PLUS (Pmode, base, offset);
36364 /* Fall back on generic machopic code. */
36365 return machopic_legitimize_pic_address (orig, mode, reg);
36368 /* Output a .machine directive for the Darwin assembler, and call
36369 the generic start_file routine. */
36371 static void
36372 rs6000_darwin_file_start (void)
36374 static const struct
36376 const char *arg;
36377 const char *name;
36378 HOST_WIDE_INT if_set;
36379 } mapping[] = {
36380 { "ppc64", "ppc64", MASK_64BIT },
36381 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
36382 { "power4", "ppc970", 0 },
36383 { "G5", "ppc970", 0 },
36384 { "7450", "ppc7450", 0 },
36385 { "7400", "ppc7400", MASK_ALTIVEC },
36386 { "G4", "ppc7400", 0 },
36387 { "750", "ppc750", 0 },
36388 { "740", "ppc750", 0 },
36389 { "G3", "ppc750", 0 },
36390 { "604e", "ppc604e", 0 },
36391 { "604", "ppc604", 0 },
36392 { "603e", "ppc603", 0 },
36393 { "603", "ppc603", 0 },
36394 { "601", "ppc601", 0 },
36395 { NULL, "ppc", 0 } };
36396 const char *cpu_id = "";
36397 size_t i;
36399 rs6000_file_start ();
36400 darwin_file_start ();
36402 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
36404 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
36405 cpu_id = rs6000_default_cpu;
36407 if (global_options_set.x_rs6000_cpu_index)
36408 cpu_id = processor_target_table[rs6000_cpu_index].name;
36410 /* Look through the mapping array. Pick the first name that either
36411 matches the argument, has a bit set in IF_SET that is also set
36412 in the target flags, or has a NULL name. */
36414 i = 0;
36415 while (mapping[i].arg != NULL
36416 && strcmp (mapping[i].arg, cpu_id) != 0
36417 && (mapping[i].if_set & rs6000_isa_flags) == 0)
36418 i++;
36420 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
36423 #endif /* TARGET_MACHO */
36425 #if TARGET_ELF
36426 static int
36427 rs6000_elf_reloc_rw_mask (void)
36429 if (flag_pic)
36430 return 3;
36431 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
36432 return 2;
36433 else
36434 return 0;
36437 /* Record an element in the table of global constructors. SYMBOL is
36438 a SYMBOL_REF of the function to be called; PRIORITY is a number
36439 between 0 and MAX_INIT_PRIORITY.
36441 This differs from default_named_section_asm_out_constructor in
36442 that we have special handling for -mrelocatable. */
36444 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
36445 static void
36446 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
36448 const char *section = ".ctors";
36449 char buf[18];
36451 if (priority != DEFAULT_INIT_PRIORITY)
36453 sprintf (buf, ".ctors.%.5u",
36454 /* Invert the numbering so the linker puts us in the proper
36455 order; constructors are run from right to left, and the
36456 linker sorts in increasing order. */
36457 MAX_INIT_PRIORITY - priority);
36458 section = buf;
36461 switch_to_section (get_section (section, SECTION_WRITE, NULL));
36462 assemble_align (POINTER_SIZE);
36464 if (DEFAULT_ABI == ABI_V4
36465 && (TARGET_RELOCATABLE || flag_pic > 1))
36467 fputs ("\t.long (", asm_out_file);
36468 output_addr_const (asm_out_file, symbol);
36469 fputs (")@fixup\n", asm_out_file);
36471 else
36472 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
36475 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
36476 static void
36477 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
36479 const char *section = ".dtors";
36480 char buf[18];
36482 if (priority != DEFAULT_INIT_PRIORITY)
36484 sprintf (buf, ".dtors.%.5u",
36485 /* Invert the numbering so the linker puts us in the proper
36486 order; constructors are run from right to left, and the
36487 linker sorts in increasing order. */
36488 MAX_INIT_PRIORITY - priority);
36489 section = buf;
36492 switch_to_section (get_section (section, SECTION_WRITE, NULL));
36493 assemble_align (POINTER_SIZE);
36495 if (DEFAULT_ABI == ABI_V4
36496 && (TARGET_RELOCATABLE || flag_pic > 1))
36498 fputs ("\t.long (", asm_out_file);
36499 output_addr_const (asm_out_file, symbol);
36500 fputs (")@fixup\n", asm_out_file);
36502 else
36503 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
36506 void
36507 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
36509 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
36511 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
36512 ASM_OUTPUT_LABEL (file, name);
36513 fputs (DOUBLE_INT_ASM_OP, file);
36514 rs6000_output_function_entry (file, name);
36515 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
36516 if (DOT_SYMBOLS)
36518 fputs ("\t.size\t", file);
36519 assemble_name (file, name);
36520 fputs (",24\n\t.type\t.", file);
36521 assemble_name (file, name);
36522 fputs (",@function\n", file);
36523 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
36525 fputs ("\t.globl\t.", file);
36526 assemble_name (file, name);
36527 putc ('\n', file);
36530 else
36531 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
36532 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
36533 rs6000_output_function_entry (file, name);
36534 fputs (":\n", file);
36535 return;
36538 if (DEFAULT_ABI == ABI_V4
36539 && (TARGET_RELOCATABLE || flag_pic > 1)
36540 && !TARGET_SECURE_PLT
36541 && (!constant_pool_empty_p () || crtl->profile)
36542 && uses_TOC ())
36544 char buf[256];
36546 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
36548 fprintf (file, "\t.long ");
36549 assemble_name (file, toc_label_name);
36550 need_toc_init = 1;
36551 putc ('-', file);
36552 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
36553 assemble_name (file, buf);
36554 putc ('\n', file);
36557 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
36558 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
36560 if (TARGET_CMODEL == CMODEL_LARGE && rs6000_global_entry_point_needed_p ())
36562 char buf[256];
36564 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
36566 fprintf (file, "\t.quad .TOC.-");
36567 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
36568 assemble_name (file, buf);
36569 putc ('\n', file);
36572 if (DEFAULT_ABI == ABI_AIX)
36574 const char *desc_name, *orig_name;
36576 orig_name = (*targetm.strip_name_encoding) (name);
36577 desc_name = orig_name;
36578 while (*desc_name == '.')
36579 desc_name++;
36581 if (TREE_PUBLIC (decl))
36582 fprintf (file, "\t.globl %s\n", desc_name);
36584 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
36585 fprintf (file, "%s:\n", desc_name);
36586 fprintf (file, "\t.long %s\n", orig_name);
36587 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
36588 fputs ("\t.long 0\n", file);
36589 fprintf (file, "\t.previous\n");
36591 ASM_OUTPUT_LABEL (file, name);
36594 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
36595 static void
36596 rs6000_elf_file_end (void)
36598 #ifdef HAVE_AS_GNU_ATTRIBUTE
36599 /* ??? The value emitted depends on options active at file end.
36600 Assume anyone using #pragma or attributes that might change
36601 options knows what they are doing. */
36602 if ((TARGET_64BIT || DEFAULT_ABI == ABI_V4)
36603 && rs6000_passes_float)
36605 int fp;
36607 if (TARGET_DF_FPR | TARGET_DF_SPE)
36608 fp = 1;
36609 else if (TARGET_SF_FPR | TARGET_SF_SPE)
36610 fp = 3;
36611 else
36612 fp = 2;
36613 if (rs6000_passes_long_double)
36615 if (!TARGET_LONG_DOUBLE_128)
36616 fp |= 2 * 4;
36617 else if (TARGET_IEEEQUAD)
36618 fp |= 3 * 4;
36619 else
36620 fp |= 1 * 4;
36622 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", fp);
36624 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
36626 if (rs6000_passes_vector)
36627 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
36628 (TARGET_ALTIVEC_ABI ? 2
36629 : TARGET_SPE_ABI ? 3
36630 : 1));
36631 if (rs6000_returns_struct)
36632 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
36633 aix_struct_return ? 2 : 1);
36635 #endif
36636 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
36637 if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
36638 file_end_indicate_exec_stack ();
36639 #endif
36641 if (flag_split_stack)
36642 file_end_indicate_split_stack ();
36644 if (cpu_builtin_p)
36646 /* We have expanded a CPU builtin, so we need to emit a reference to
36647 the special symbol that LIBC uses to declare it supports the
36648 AT_PLATFORM and AT_HWCAP/AT_HWCAP2 in the TCB feature. */
36649 switch_to_section (data_section);
36650 fprintf (asm_out_file, "\t.align %u\n", TARGET_32BIT ? 2 : 3);
36651 fprintf (asm_out_file, "\t%s %s\n",
36652 TARGET_32BIT ? ".long" : ".quad", tcb_verification_symbol);
36655 #endif
36657 #if TARGET_XCOFF
36659 #ifndef HAVE_XCOFF_DWARF_EXTRAS
36660 #define HAVE_XCOFF_DWARF_EXTRAS 0
36661 #endif
36663 static enum unwind_info_type
36664 rs6000_xcoff_debug_unwind_info (void)
36666 return UI_NONE;
36669 static void
36670 rs6000_xcoff_asm_output_anchor (rtx symbol)
36672 char buffer[100];
36674 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
36675 SYMBOL_REF_BLOCK_OFFSET (symbol));
36676 fprintf (asm_out_file, "%s", SET_ASM_OP);
36677 RS6000_OUTPUT_BASENAME (asm_out_file, XSTR (symbol, 0));
36678 fprintf (asm_out_file, ",");
36679 RS6000_OUTPUT_BASENAME (asm_out_file, buffer);
36680 fprintf (asm_out_file, "\n");
36683 static void
36684 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
36686 fputs (GLOBAL_ASM_OP, stream);
36687 RS6000_OUTPUT_BASENAME (stream, name);
36688 putc ('\n', stream);
36691 /* A get_unnamed_decl callback, used for read-only sections. PTR
36692 points to the section string variable. */
36694 static void
36695 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
36697 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
36698 *(const char *const *) directive,
36699 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
36702 /* Likewise for read-write sections. */
36704 static void
36705 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
36707 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
36708 *(const char *const *) directive,
36709 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
36712 static void
36713 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
36715 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
36716 *(const char *const *) directive,
36717 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
36720 /* A get_unnamed_section callback, used for switching to toc_section. */
36722 static void
36723 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
36725 if (TARGET_MINIMAL_TOC)
36727 /* toc_section is always selected at least once from
36728 rs6000_xcoff_file_start, so this is guaranteed to
36729 always be defined once and only once in each file. */
36730 if (!toc_initialized)
36732 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
36733 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
36734 toc_initialized = 1;
36736 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
36737 (TARGET_32BIT ? "" : ",3"));
36739 else
36740 fputs ("\t.toc\n", asm_out_file);
36743 /* Implement TARGET_ASM_INIT_SECTIONS. */
36745 static void
36746 rs6000_xcoff_asm_init_sections (void)
36748 read_only_data_section
36749 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
36750 &xcoff_read_only_section_name);
36752 private_data_section
36753 = get_unnamed_section (SECTION_WRITE,
36754 rs6000_xcoff_output_readwrite_section_asm_op,
36755 &xcoff_private_data_section_name);
36757 tls_data_section
36758 = get_unnamed_section (SECTION_TLS,
36759 rs6000_xcoff_output_tls_section_asm_op,
36760 &xcoff_tls_data_section_name);
36762 tls_private_data_section
36763 = get_unnamed_section (SECTION_TLS,
36764 rs6000_xcoff_output_tls_section_asm_op,
36765 &xcoff_private_data_section_name);
36767 read_only_private_data_section
36768 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
36769 &xcoff_private_data_section_name);
36771 toc_section
36772 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
36774 readonly_data_section = read_only_data_section;
36777 static int
36778 rs6000_xcoff_reloc_rw_mask (void)
36780 return 3;
36783 static void
36784 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
36785 tree decl ATTRIBUTE_UNUSED)
36787 int smclass;
36788 static const char * const suffix[5] = { "PR", "RO", "RW", "TL", "XO" };
36790 if (flags & SECTION_EXCLUDE)
36791 smclass = 4;
36792 else if (flags & SECTION_DEBUG)
36794 fprintf (asm_out_file, "\t.dwsect %s\n", name);
36795 return;
36797 else if (flags & SECTION_CODE)
36798 smclass = 0;
36799 else if (flags & SECTION_TLS)
36800 smclass = 3;
36801 else if (flags & SECTION_WRITE)
36802 smclass = 2;
36803 else
36804 smclass = 1;
36806 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
36807 (flags & SECTION_CODE) ? "." : "",
36808 name, suffix[smclass], flags & SECTION_ENTSIZE);
36811 #define IN_NAMED_SECTION(DECL) \
36812 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
36813 && DECL_SECTION_NAME (DECL) != NULL)
36815 static section *
36816 rs6000_xcoff_select_section (tree decl, int reloc,
36817 unsigned HOST_WIDE_INT align)
36819 /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
36820 named section. */
36821 if (align > BIGGEST_ALIGNMENT)
36823 resolve_unique_section (decl, reloc, true);
36824 if (IN_NAMED_SECTION (decl))
36825 return get_named_section (decl, NULL, reloc);
36828 if (decl_readonly_section (decl, reloc))
36830 if (TREE_PUBLIC (decl))
36831 return read_only_data_section;
36832 else
36833 return read_only_private_data_section;
36835 else
36837 #if HAVE_AS_TLS
36838 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
36840 if (TREE_PUBLIC (decl))
36841 return tls_data_section;
36842 else if (bss_initializer_p (decl))
36844 /* Convert to COMMON to emit in BSS. */
36845 DECL_COMMON (decl) = 1;
36846 return tls_comm_section;
36848 else
36849 return tls_private_data_section;
36851 else
36852 #endif
36853 if (TREE_PUBLIC (decl))
36854 return data_section;
36855 else
36856 return private_data_section;
36860 static void
36861 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
36863 const char *name;
36865 /* Use select_section for private data and uninitialized data with
36866 alignment <= BIGGEST_ALIGNMENT. */
36867 if (!TREE_PUBLIC (decl)
36868 || DECL_COMMON (decl)
36869 || (DECL_INITIAL (decl) == NULL_TREE
36870 && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
36871 || DECL_INITIAL (decl) == error_mark_node
36872 || (flag_zero_initialized_in_bss
36873 && initializer_zerop (DECL_INITIAL (decl))))
36874 return;
36876 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
36877 name = (*targetm.strip_name_encoding) (name);
36878 set_decl_section_name (decl, name);
36881 /* Select section for constant in constant pool.
36883 On RS/6000, all constants are in the private read-only data area.
36884 However, if this is being placed in the TOC it must be output as a
36885 toc entry. */
36887 static section *
36888 rs6000_xcoff_select_rtx_section (machine_mode mode, rtx x,
36889 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
36891 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
36892 return toc_section;
36893 else
36894 return read_only_private_data_section;
36897 /* Remove any trailing [DS] or the like from the symbol name. */
36899 static const char *
36900 rs6000_xcoff_strip_name_encoding (const char *name)
36902 size_t len;
36903 if (*name == '*')
36904 name++;
36905 len = strlen (name);
36906 if (name[len - 1] == ']')
36907 return ggc_alloc_string (name, len - 4);
36908 else
36909 return name;
36912 /* Section attributes. AIX is always PIC. */
36914 static unsigned int
36915 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
36917 unsigned int align;
36918 unsigned int flags = default_section_type_flags (decl, name, reloc);
36920 /* Align to at least UNIT size. */
36921 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
36922 align = MIN_UNITS_PER_WORD;
36923 else
36924 /* Increase alignment of large objects if not already stricter. */
36925 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
36926 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
36927 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
36929 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
36932 /* Output at beginning of assembler file.
36934 Initialize the section names for the RS/6000 at this point.
36936 Specify filename, including full path, to assembler.
36938 We want to go into the TOC section so at least one .toc will be emitted.
36939 Also, in order to output proper .bs/.es pairs, we need at least one static
36940 [RW] section emitted.
36942 Finally, declare mcount when profiling to make the assembler happy. */
36944 static void
36945 rs6000_xcoff_file_start (void)
36947 rs6000_gen_section_name (&xcoff_bss_section_name,
36948 main_input_filename, ".bss_");
36949 rs6000_gen_section_name (&xcoff_private_data_section_name,
36950 main_input_filename, ".rw_");
36951 rs6000_gen_section_name (&xcoff_read_only_section_name,
36952 main_input_filename, ".ro_");
36953 rs6000_gen_section_name (&xcoff_tls_data_section_name,
36954 main_input_filename, ".tls_");
36955 rs6000_gen_section_name (&xcoff_tbss_section_name,
36956 main_input_filename, ".tbss_[UL]");
36958 fputs ("\t.file\t", asm_out_file);
36959 output_quoted_string (asm_out_file, main_input_filename);
36960 fputc ('\n', asm_out_file);
36961 if (write_symbols != NO_DEBUG)
36962 switch_to_section (private_data_section);
36963 switch_to_section (toc_section);
36964 switch_to_section (text_section);
36965 if (profile_flag)
36966 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
36967 rs6000_file_start ();
36970 /* Output at end of assembler file.
36971 On the RS/6000, referencing data should automatically pull in text. */
36973 static void
36974 rs6000_xcoff_file_end (void)
36976 switch_to_section (text_section);
36977 fputs ("_section_.text:\n", asm_out_file);
36978 switch_to_section (data_section);
36979 fputs (TARGET_32BIT
36980 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
36981 asm_out_file);
36984 struct declare_alias_data
36986 FILE *file;
36987 bool function_descriptor;
36990 /* Declare alias N. A helper function for for_node_and_aliases. */
36992 static bool
36993 rs6000_declare_alias (struct symtab_node *n, void *d)
36995 struct declare_alias_data *data = (struct declare_alias_data *)d;
36996 /* Main symbol is output specially, because varasm machinery does part of
36997 the job for us - we do not need to declare .globl/lglobs and such. */
36998 if (!n->alias || n->weakref)
36999 return false;
37001 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (n->decl)))
37002 return false;
37004 /* Prevent assemble_alias from trying to use .set pseudo operation
37005 that does not behave as expected by the middle-end. */
37006 TREE_ASM_WRITTEN (n->decl) = true;
37008 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl));
37009 char *buffer = (char *) alloca (strlen (name) + 2);
37010 char *p;
37011 int dollar_inside = 0;
37013 strcpy (buffer, name);
37014 p = strchr (buffer, '$');
37015 while (p) {
37016 *p = '_';
37017 dollar_inside++;
37018 p = strchr (p + 1, '$');
37020 if (TREE_PUBLIC (n->decl))
37022 if (!RS6000_WEAK || !DECL_WEAK (n->decl))
37024 if (dollar_inside) {
37025 if (data->function_descriptor)
37026 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
37027 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
37029 if (data->function_descriptor)
37031 fputs ("\t.globl .", data->file);
37032 RS6000_OUTPUT_BASENAME (data->file, buffer);
37033 putc ('\n', data->file);
37035 fputs ("\t.globl ", data->file);
37036 RS6000_OUTPUT_BASENAME (data->file, buffer);
37037 putc ('\n', data->file);
37039 #ifdef ASM_WEAKEN_DECL
37040 else if (DECL_WEAK (n->decl) && !data->function_descriptor)
37041 ASM_WEAKEN_DECL (data->file, n->decl, name, NULL);
37042 #endif
37044 else
37046 if (dollar_inside)
37048 if (data->function_descriptor)
37049 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
37050 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
37052 if (data->function_descriptor)
37054 fputs ("\t.lglobl .", data->file);
37055 RS6000_OUTPUT_BASENAME (data->file, buffer);
37056 putc ('\n', data->file);
37058 fputs ("\t.lglobl ", data->file);
37059 RS6000_OUTPUT_BASENAME (data->file, buffer);
37060 putc ('\n', data->file);
37062 if (data->function_descriptor)
37063 fputs (".", data->file);
37064 RS6000_OUTPUT_BASENAME (data->file, buffer);
37065 fputs (":\n", data->file);
37066 return false;
37070 #ifdef HAVE_GAS_HIDDEN
37071 /* Helper function to calculate visibility of a DECL
37072 and return the value as a const string. */
37074 static const char *
37075 rs6000_xcoff_visibility (tree decl)
37077 static const char * const visibility_types[] = {
37078 "", ",protected", ",hidden", ",internal"
37081 enum symbol_visibility vis = DECL_VISIBILITY (decl);
37083 if (TREE_CODE (decl) == FUNCTION_DECL
37084 && cgraph_node::get (decl)
37085 && cgraph_node::get (decl)->instrumentation_clone
37086 && cgraph_node::get (decl)->instrumented_version)
37087 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
37089 return visibility_types[vis];
37091 #endif
37094 /* This macro produces the initial definition of a function name.
37095 On the RS/6000, we need to place an extra '.' in the function name and
37096 output the function descriptor.
37097 Dollar signs are converted to underscores.
37099 The csect for the function will have already been created when
37100 text_section was selected. We do have to go back to that csect, however.
37102 The third and fourth parameters to the .function pseudo-op (16 and 044)
37103 are placeholders which no longer have any use.
37105 Because AIX assembler's .set command has unexpected semantics, we output
37106 all aliases as alternative labels in front of the definition. */
37108 void
37109 rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
37111 char *buffer = (char *) alloca (strlen (name) + 1);
37112 char *p;
37113 int dollar_inside = 0;
37114 struct declare_alias_data data = {file, false};
37116 strcpy (buffer, name);
37117 p = strchr (buffer, '$');
37118 while (p) {
37119 *p = '_';
37120 dollar_inside++;
37121 p = strchr (p + 1, '$');
37123 if (TREE_PUBLIC (decl))
37125 if (!RS6000_WEAK || !DECL_WEAK (decl))
37127 if (dollar_inside) {
37128 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
37129 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
37131 fputs ("\t.globl .", file);
37132 RS6000_OUTPUT_BASENAME (file, buffer);
37133 #ifdef HAVE_GAS_HIDDEN
37134 fputs (rs6000_xcoff_visibility (decl), file);
37135 #endif
37136 putc ('\n', file);
37139 else
37141 if (dollar_inside) {
37142 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
37143 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
37145 fputs ("\t.lglobl .", file);
37146 RS6000_OUTPUT_BASENAME (file, buffer);
37147 putc ('\n', file);
37149 fputs ("\t.csect ", file);
37150 RS6000_OUTPUT_BASENAME (file, buffer);
37151 fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
37152 RS6000_OUTPUT_BASENAME (file, buffer);
37153 fputs (":\n", file);
37154 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
37155 &data, true);
37156 fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
37157 RS6000_OUTPUT_BASENAME (file, buffer);
37158 fputs (", TOC[tc0], 0\n", file);
37159 in_section = NULL;
37160 switch_to_section (function_section (decl));
37161 putc ('.', file);
37162 RS6000_OUTPUT_BASENAME (file, buffer);
37163 fputs (":\n", file);
37164 data.function_descriptor = true;
37165 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
37166 &data, true);
37167 if (!DECL_IGNORED_P (decl))
37169 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
37170 xcoffout_declare_function (file, decl, buffer);
37171 else if (write_symbols == DWARF2_DEBUG)
37173 name = (*targetm.strip_name_encoding) (name);
37174 fprintf (file, "\t.function .%s,.%s,2,0\n", name, name);
37177 return;
37181 /* Output assembly language to globalize a symbol from a DECL,
37182 possibly with visibility. */
37184 void
37185 rs6000_xcoff_asm_globalize_decl_name (FILE *stream, tree decl)
37187 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
37188 fputs (GLOBAL_ASM_OP, stream);
37189 RS6000_OUTPUT_BASENAME (stream, name);
37190 #ifdef HAVE_GAS_HIDDEN
37191 fputs (rs6000_xcoff_visibility (decl), stream);
37192 #endif
37193 putc ('\n', stream);
37196 /* Output assembly language to define a symbol as COMMON from a DECL,
37197 possibly with visibility. */
37199 void
37200 rs6000_xcoff_asm_output_aligned_decl_common (FILE *stream,
37201 tree decl ATTRIBUTE_UNUSED,
37202 const char *name,
37203 unsigned HOST_WIDE_INT size,
37204 unsigned HOST_WIDE_INT align)
37206 unsigned HOST_WIDE_INT align2 = 2;
37208 if (align > 32)
37209 align2 = floor_log2 (align / BITS_PER_UNIT);
37210 else if (size > 4)
37211 align2 = 3;
37213 fputs (COMMON_ASM_OP, stream);
37214 RS6000_OUTPUT_BASENAME (stream, name);
37216 fprintf (stream,
37217 "," HOST_WIDE_INT_PRINT_UNSIGNED "," HOST_WIDE_INT_PRINT_UNSIGNED,
37218 size, align2);
37220 #ifdef HAVE_GAS_HIDDEN
37221 fputs (rs6000_xcoff_visibility (decl), stream);
37222 #endif
37223 putc ('\n', stream);
37226 /* This macro produces the initial definition of a object (variable) name.
37227 Because AIX assembler's .set command has unexpected semantics, we output
37228 all aliases as alternative labels in front of the definition. */
37230 void
37231 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
37233 struct declare_alias_data data = {file, false};
37234 RS6000_OUTPUT_BASENAME (file, name);
37235 fputs (":\n", file);
37236 symtab_node::get_create (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
37237 &data, true);
37240 /* Overide the default 'SYMBOL-.' syntax with AIX compatible 'SYMBOL-$'. */
37242 void
37243 rs6000_asm_output_dwarf_pcrel (FILE *file, int size, const char *label)
37245 fputs (integer_asm_op (size, FALSE), file);
37246 assemble_name (file, label);
37247 fputs ("-$", file);
37250 /* Output a symbol offset relative to the dbase for the current object.
37251 We use __gcc_unwind_dbase as an arbitrary base for dbase and assume
37252 signed offsets.
37254 __gcc_unwind_dbase is embedded in all executables/libraries through
37255 libgcc/config/rs6000/crtdbase.S. */
37257 void
37258 rs6000_asm_output_dwarf_datarel (FILE *file, int size, const char *label)
37260 fputs (integer_asm_op (size, FALSE), file);
37261 assemble_name (file, label);
37262 fputs("-__gcc_unwind_dbase", file);
37265 #ifdef HAVE_AS_TLS
37266 static void
37267 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
37269 rtx symbol;
37270 int flags;
37271 const char *symname;
37273 default_encode_section_info (decl, rtl, first);
37275 /* Careful not to prod global register variables. */
37276 if (!MEM_P (rtl))
37277 return;
37278 symbol = XEXP (rtl, 0);
37279 if (GET_CODE (symbol) != SYMBOL_REF)
37280 return;
37282 flags = SYMBOL_REF_FLAGS (symbol);
37284 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
37285 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
37287 SYMBOL_REF_FLAGS (symbol) = flags;
37289 /* Append mapping class to extern decls. */
37290 symname = XSTR (symbol, 0);
37291 if (decl /* sync condition with assemble_external () */
37292 && DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
37293 && ((TREE_CODE (decl) == VAR_DECL && !DECL_THREAD_LOCAL_P (decl))
37294 || TREE_CODE (decl) == FUNCTION_DECL)
37295 && symname[strlen (symname) - 1] != ']')
37297 char *newname = (char *) alloca (strlen (symname) + 5);
37298 strcpy (newname, symname);
37299 strcat (newname, (TREE_CODE (decl) == FUNCTION_DECL
37300 ? "[DS]" : "[UA]"));
37301 XSTR (symbol, 0) = ggc_strdup (newname);
37304 #endif /* HAVE_AS_TLS */
37305 #endif /* TARGET_XCOFF */
37307 void
37308 rs6000_asm_weaken_decl (FILE *stream, tree decl,
37309 const char *name, const char *val)
37311 fputs ("\t.weak\t", stream);
37312 RS6000_OUTPUT_BASENAME (stream, name);
37313 if (decl && TREE_CODE (decl) == FUNCTION_DECL
37314 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
37316 if (TARGET_XCOFF)
37317 fputs ("[DS]", stream);
37318 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
37319 if (TARGET_XCOFF)
37320 fputs (rs6000_xcoff_visibility (decl), stream);
37321 #endif
37322 fputs ("\n\t.weak\t.", stream);
37323 RS6000_OUTPUT_BASENAME (stream, name);
37325 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
37326 if (TARGET_XCOFF)
37327 fputs (rs6000_xcoff_visibility (decl), stream);
37328 #endif
37329 fputc ('\n', stream);
37330 if (val)
37332 #ifdef ASM_OUTPUT_DEF
37333 ASM_OUTPUT_DEF (stream, name, val);
37334 #endif
37335 if (decl && TREE_CODE (decl) == FUNCTION_DECL
37336 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
37338 fputs ("\t.set\t.", stream);
37339 RS6000_OUTPUT_BASENAME (stream, name);
37340 fputs (",.", stream);
37341 RS6000_OUTPUT_BASENAME (stream, val);
37342 fputc ('\n', stream);
37348 /* Return true if INSN should not be copied. */
37350 static bool
37351 rs6000_cannot_copy_insn_p (rtx_insn *insn)
37353 return recog_memoized (insn) >= 0
37354 && get_attr_cannot_copy (insn);
37357 /* Compute a (partial) cost for rtx X. Return true if the complete
37358 cost has been computed, and false if subexpressions should be
37359 scanned. In either case, *TOTAL contains the cost result. */
37361 static bool
37362 rs6000_rtx_costs (rtx x, machine_mode mode, int outer_code,
37363 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
37365 int code = GET_CODE (x);
37367 switch (code)
37369 /* On the RS/6000, if it is valid in the insn, it is free. */
37370 case CONST_INT:
37371 if (((outer_code == SET
37372 || outer_code == PLUS
37373 || outer_code == MINUS)
37374 && (satisfies_constraint_I (x)
37375 || satisfies_constraint_L (x)))
37376 || (outer_code == AND
37377 && (satisfies_constraint_K (x)
37378 || (mode == SImode
37379 ? satisfies_constraint_L (x)
37380 : satisfies_constraint_J (x))))
37381 || ((outer_code == IOR || outer_code == XOR)
37382 && (satisfies_constraint_K (x)
37383 || (mode == SImode
37384 ? satisfies_constraint_L (x)
37385 : satisfies_constraint_J (x))))
37386 || outer_code == ASHIFT
37387 || outer_code == ASHIFTRT
37388 || outer_code == LSHIFTRT
37389 || outer_code == ROTATE
37390 || outer_code == ROTATERT
37391 || outer_code == ZERO_EXTRACT
37392 || (outer_code == MULT
37393 && satisfies_constraint_I (x))
37394 || ((outer_code == DIV || outer_code == UDIV
37395 || outer_code == MOD || outer_code == UMOD)
37396 && exact_log2 (INTVAL (x)) >= 0)
37397 || (outer_code == COMPARE
37398 && (satisfies_constraint_I (x)
37399 || satisfies_constraint_K (x)))
37400 || ((outer_code == EQ || outer_code == NE)
37401 && (satisfies_constraint_I (x)
37402 || satisfies_constraint_K (x)
37403 || (mode == SImode
37404 ? satisfies_constraint_L (x)
37405 : satisfies_constraint_J (x))))
37406 || (outer_code == GTU
37407 && satisfies_constraint_I (x))
37408 || (outer_code == LTU
37409 && satisfies_constraint_P (x)))
37411 *total = 0;
37412 return true;
37414 else if ((outer_code == PLUS
37415 && reg_or_add_cint_operand (x, VOIDmode))
37416 || (outer_code == MINUS
37417 && reg_or_sub_cint_operand (x, VOIDmode))
37418 || ((outer_code == SET
37419 || outer_code == IOR
37420 || outer_code == XOR)
37421 && (INTVAL (x)
37422 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
37424 *total = COSTS_N_INSNS (1);
37425 return true;
37427 /* FALLTHRU */
37429 case CONST_DOUBLE:
37430 case CONST_WIDE_INT:
37431 case CONST:
37432 case HIGH:
37433 case SYMBOL_REF:
37434 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
37435 return true;
37437 case MEM:
37438 /* When optimizing for size, MEM should be slightly more expensive
37439 than generating address, e.g., (plus (reg) (const)).
37440 L1 cache latency is about two instructions. */
37441 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
37442 if (SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (x)))
37443 *total += COSTS_N_INSNS (100);
37444 return true;
37446 case LABEL_REF:
37447 *total = 0;
37448 return true;
37450 case PLUS:
37451 case MINUS:
37452 if (FLOAT_MODE_P (mode))
37453 *total = rs6000_cost->fp;
37454 else
37455 *total = COSTS_N_INSNS (1);
37456 return false;
37458 case MULT:
37459 if (GET_CODE (XEXP (x, 1)) == CONST_INT
37460 && satisfies_constraint_I (XEXP (x, 1)))
37462 if (INTVAL (XEXP (x, 1)) >= -256
37463 && INTVAL (XEXP (x, 1)) <= 255)
37464 *total = rs6000_cost->mulsi_const9;
37465 else
37466 *total = rs6000_cost->mulsi_const;
37468 else if (mode == SFmode)
37469 *total = rs6000_cost->fp;
37470 else if (FLOAT_MODE_P (mode))
37471 *total = rs6000_cost->dmul;
37472 else if (mode == DImode)
37473 *total = rs6000_cost->muldi;
37474 else
37475 *total = rs6000_cost->mulsi;
37476 return false;
37478 case FMA:
37479 if (mode == SFmode)
37480 *total = rs6000_cost->fp;
37481 else
37482 *total = rs6000_cost->dmul;
37483 break;
37485 case DIV:
37486 case MOD:
37487 if (FLOAT_MODE_P (mode))
37489 *total = mode == DFmode ? rs6000_cost->ddiv
37490 : rs6000_cost->sdiv;
37491 return false;
37493 /* FALLTHRU */
37495 case UDIV:
37496 case UMOD:
37497 if (GET_CODE (XEXP (x, 1)) == CONST_INT
37498 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
37500 if (code == DIV || code == MOD)
37501 /* Shift, addze */
37502 *total = COSTS_N_INSNS (2);
37503 else
37504 /* Shift */
37505 *total = COSTS_N_INSNS (1);
37507 else
37509 if (GET_MODE (XEXP (x, 1)) == DImode)
37510 *total = rs6000_cost->divdi;
37511 else
37512 *total = rs6000_cost->divsi;
37514 /* Add in shift and subtract for MOD unless we have a mod instruction. */
37515 if (!TARGET_MODULO && (code == MOD || code == UMOD))
37516 *total += COSTS_N_INSNS (2);
37517 return false;
37519 case CTZ:
37520 *total = COSTS_N_INSNS (TARGET_CTZ ? 1 : 4);
37521 return false;
37523 case FFS:
37524 *total = COSTS_N_INSNS (4);
37525 return false;
37527 case POPCOUNT:
37528 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
37529 return false;
37531 case PARITY:
37532 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
37533 return false;
37535 case NOT:
37536 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
37537 *total = 0;
37538 else
37539 *total = COSTS_N_INSNS (1);
37540 return false;
37542 case AND:
37543 if (CONST_INT_P (XEXP (x, 1)))
37545 rtx left = XEXP (x, 0);
37546 rtx_code left_code = GET_CODE (left);
37548 /* rotate-and-mask: 1 insn. */
37549 if ((left_code == ROTATE
37550 || left_code == ASHIFT
37551 || left_code == LSHIFTRT)
37552 && rs6000_is_valid_shift_mask (XEXP (x, 1), left, mode))
37554 *total = rtx_cost (XEXP (left, 0), mode, left_code, 0, speed);
37555 if (!CONST_INT_P (XEXP (left, 1)))
37556 *total += rtx_cost (XEXP (left, 1), SImode, left_code, 1, speed);
37557 *total += COSTS_N_INSNS (1);
37558 return true;
37561 /* rotate-and-mask (no rotate), andi., andis.: 1 insn. */
37562 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
37563 if (rs6000_is_valid_and_mask (XEXP (x, 1), mode)
37564 || (val & 0xffff) == val
37565 || (val & 0xffff0000) == val
37566 || ((val & 0xffff) == 0 && mode == SImode))
37568 *total = rtx_cost (left, mode, AND, 0, speed);
37569 *total += COSTS_N_INSNS (1);
37570 return true;
37573 /* 2 insns. */
37574 if (rs6000_is_valid_2insn_and (XEXP (x, 1), mode))
37576 *total = rtx_cost (left, mode, AND, 0, speed);
37577 *total += COSTS_N_INSNS (2);
37578 return true;
37582 *total = COSTS_N_INSNS (1);
37583 return false;
37585 case IOR:
37586 /* FIXME */
37587 *total = COSTS_N_INSNS (1);
37588 return true;
37590 case CLZ:
37591 case XOR:
37592 case ZERO_EXTRACT:
37593 *total = COSTS_N_INSNS (1);
37594 return false;
37596 case ASHIFT:
37597 /* The EXTSWSLI instruction is a combined instruction. Don't count both
37598 the sign extend and shift separately within the insn. */
37599 if (TARGET_EXTSWSLI && mode == DImode
37600 && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
37601 && GET_MODE (XEXP (XEXP (x, 0), 0)) == SImode)
37603 *total = 0;
37604 return false;
37606 /* fall through */
37608 case ASHIFTRT:
37609 case LSHIFTRT:
37610 case ROTATE:
37611 case ROTATERT:
37612 /* Handle mul_highpart. */
37613 if (outer_code == TRUNCATE
37614 && GET_CODE (XEXP (x, 0)) == MULT)
37616 if (mode == DImode)
37617 *total = rs6000_cost->muldi;
37618 else
37619 *total = rs6000_cost->mulsi;
37620 return true;
37622 else if (outer_code == AND)
37623 *total = 0;
37624 else
37625 *total = COSTS_N_INSNS (1);
37626 return false;
37628 case SIGN_EXTEND:
37629 case ZERO_EXTEND:
37630 if (GET_CODE (XEXP (x, 0)) == MEM)
37631 *total = 0;
37632 else
37633 *total = COSTS_N_INSNS (1);
37634 return false;
37636 case COMPARE:
37637 case NEG:
37638 case ABS:
37639 if (!FLOAT_MODE_P (mode))
37641 *total = COSTS_N_INSNS (1);
37642 return false;
37644 /* FALLTHRU */
37646 case FLOAT:
37647 case UNSIGNED_FLOAT:
37648 case FIX:
37649 case UNSIGNED_FIX:
37650 case FLOAT_TRUNCATE:
37651 *total = rs6000_cost->fp;
37652 return false;
37654 case FLOAT_EXTEND:
37655 if (mode == DFmode)
37656 *total = rs6000_cost->sfdf_convert;
37657 else
37658 *total = rs6000_cost->fp;
37659 return false;
37661 case UNSPEC:
37662 switch (XINT (x, 1))
37664 case UNSPEC_FRSP:
37665 *total = rs6000_cost->fp;
37666 return true;
37668 default:
37669 break;
37671 break;
37673 case CALL:
37674 case IF_THEN_ELSE:
37675 if (!speed)
37677 *total = COSTS_N_INSNS (1);
37678 return true;
37680 else if (FLOAT_MODE_P (mode)
37681 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
37683 *total = rs6000_cost->fp;
37684 return false;
37686 break;
37688 case NE:
37689 case EQ:
37690 case GTU:
37691 case LTU:
37692 /* Carry bit requires mode == Pmode.
37693 NEG or PLUS already counted so only add one. */
37694 if (mode == Pmode
37695 && (outer_code == NEG || outer_code == PLUS))
37697 *total = COSTS_N_INSNS (1);
37698 return true;
37700 if (outer_code == SET)
37702 if (XEXP (x, 1) == const0_rtx)
37704 if (TARGET_ISEL && !TARGET_MFCRF)
37705 *total = COSTS_N_INSNS (8);
37706 else
37707 *total = COSTS_N_INSNS (2);
37708 return true;
37710 else
37712 *total = COSTS_N_INSNS (3);
37713 return false;
37716 /* FALLTHRU */
37718 case GT:
37719 case LT:
37720 case UNORDERED:
37721 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
37723 if (TARGET_ISEL && !TARGET_MFCRF)
37724 *total = COSTS_N_INSNS (8);
37725 else
37726 *total = COSTS_N_INSNS (2);
37727 return true;
37729 /* CC COMPARE. */
37730 if (outer_code == COMPARE)
37732 *total = 0;
37733 return true;
37735 break;
37737 default:
37738 break;
37741 return false;
37744 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
37746 static bool
37747 rs6000_debug_rtx_costs (rtx x, machine_mode mode, int outer_code,
37748 int opno, int *total, bool speed)
37750 bool ret = rs6000_rtx_costs (x, mode, outer_code, opno, total, speed);
37752 fprintf (stderr,
37753 "\nrs6000_rtx_costs, return = %s, mode = %s, outer_code = %s, "
37754 "opno = %d, total = %d, speed = %s, x:\n",
37755 ret ? "complete" : "scan inner",
37756 GET_MODE_NAME (mode),
37757 GET_RTX_NAME (outer_code),
37758 opno,
37759 *total,
37760 speed ? "true" : "false");
37762 debug_rtx (x);
37764 return ret;
37767 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
37769 static int
37770 rs6000_debug_address_cost (rtx x, machine_mode mode,
37771 addr_space_t as, bool speed)
37773 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
37775 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
37776 ret, speed ? "true" : "false");
37777 debug_rtx (x);
37779 return ret;
37783 /* A C expression returning the cost of moving data from a register of class
37784 CLASS1 to one of CLASS2. */
37786 static int
37787 rs6000_register_move_cost (machine_mode mode,
37788 reg_class_t from, reg_class_t to)
37790 int ret;
37792 if (TARGET_DEBUG_COST)
37793 dbg_cost_ctrl++;
37795 /* Moves from/to GENERAL_REGS. */
37796 if (reg_classes_intersect_p (to, GENERAL_REGS)
37797 || reg_classes_intersect_p (from, GENERAL_REGS))
37799 reg_class_t rclass = from;
37801 if (! reg_classes_intersect_p (to, GENERAL_REGS))
37802 rclass = to;
37804 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
37805 ret = (rs6000_memory_move_cost (mode, rclass, false)
37806 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
37808 /* It's more expensive to move CR_REGS than CR0_REGS because of the
37809 shift. */
37810 else if (rclass == CR_REGS)
37811 ret = 4;
37813 /* For those processors that have slow LR/CTR moves, make them more
37814 expensive than memory in order to bias spills to memory .*/
37815 else if ((rs6000_cpu == PROCESSOR_POWER6
37816 || rs6000_cpu == PROCESSOR_POWER7
37817 || rs6000_cpu == PROCESSOR_POWER8
37818 || rs6000_cpu == PROCESSOR_POWER9)
37819 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
37820 ret = 6 * hard_regno_nregs[0][mode];
37822 else
37823 /* A move will cost one instruction per GPR moved. */
37824 ret = 2 * hard_regno_nregs[0][mode];
37827 /* If we have VSX, we can easily move between FPR or Altivec registers. */
37828 else if (VECTOR_MEM_VSX_P (mode)
37829 && reg_classes_intersect_p (to, VSX_REGS)
37830 && reg_classes_intersect_p (from, VSX_REGS))
37831 ret = 2 * hard_regno_nregs[FIRST_FPR_REGNO][mode];
37833 /* Moving between two similar registers is just one instruction. */
37834 else if (reg_classes_intersect_p (to, from))
37835 ret = (FLOAT128_2REG_P (mode)) ? 4 : 2;
37837 /* Everything else has to go through GENERAL_REGS. */
37838 else
37839 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
37840 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
37842 if (TARGET_DEBUG_COST)
37844 if (dbg_cost_ctrl == 1)
37845 fprintf (stderr,
37846 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
37847 ret, GET_MODE_NAME (mode), reg_class_names[from],
37848 reg_class_names[to]);
37849 dbg_cost_ctrl--;
37852 return ret;
37855 /* A C expressions returning the cost of moving data of MODE from a register to
37856 or from memory. */
37858 static int
37859 rs6000_memory_move_cost (machine_mode mode, reg_class_t rclass,
37860 bool in ATTRIBUTE_UNUSED)
37862 int ret;
37864 if (TARGET_DEBUG_COST)
37865 dbg_cost_ctrl++;
37867 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
37868 ret = 4 * hard_regno_nregs[0][mode];
37869 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
37870 || reg_classes_intersect_p (rclass, VSX_REGS)))
37871 ret = 4 * hard_regno_nregs[32][mode];
37872 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
37873 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
37874 else
37875 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
37877 if (TARGET_DEBUG_COST)
37879 if (dbg_cost_ctrl == 1)
37880 fprintf (stderr,
37881 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
37882 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
37883 dbg_cost_ctrl--;
37886 return ret;
37889 /* Returns a code for a target-specific builtin that implements
37890 reciprocal of the function, or NULL_TREE if not available. */
37892 static tree
37893 rs6000_builtin_reciprocal (tree fndecl)
37895 switch (DECL_FUNCTION_CODE (fndecl))
37897 case VSX_BUILTIN_XVSQRTDP:
37898 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
37899 return NULL_TREE;
37901 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
37903 case VSX_BUILTIN_XVSQRTSP:
37904 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
37905 return NULL_TREE;
37907 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
37909 default:
37910 return NULL_TREE;
37914 /* Load up a constant. If the mode is a vector mode, splat the value across
37915 all of the vector elements. */
37917 static rtx
37918 rs6000_load_constant_and_splat (machine_mode mode, REAL_VALUE_TYPE dconst)
37920 rtx reg;
37922 if (mode == SFmode || mode == DFmode)
37924 rtx d = const_double_from_real_value (dconst, mode);
37925 reg = force_reg (mode, d);
37927 else if (mode == V4SFmode)
37929 rtx d = const_double_from_real_value (dconst, SFmode);
37930 rtvec v = gen_rtvec (4, d, d, d, d);
37931 reg = gen_reg_rtx (mode);
37932 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
37934 else if (mode == V2DFmode)
37936 rtx d = const_double_from_real_value (dconst, DFmode);
37937 rtvec v = gen_rtvec (2, d, d);
37938 reg = gen_reg_rtx (mode);
37939 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
37941 else
37942 gcc_unreachable ();
37944 return reg;
37947 /* Generate an FMA instruction. */
37949 static void
37950 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
37952 machine_mode mode = GET_MODE (target);
37953 rtx dst;
37955 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
37956 gcc_assert (dst != NULL);
37958 if (dst != target)
37959 emit_move_insn (target, dst);
37962 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
37964 static void
37965 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
37967 machine_mode mode = GET_MODE (dst);
37968 rtx r;
37970 /* This is a tad more complicated, since the fnma_optab is for
37971 a different expression: fma(-m1, m2, a), which is the same
37972 thing except in the case of signed zeros.
37974 Fortunately we know that if FMA is supported that FNMSUB is
37975 also supported in the ISA. Just expand it directly. */
37977 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
37979 r = gen_rtx_NEG (mode, a);
37980 r = gen_rtx_FMA (mode, m1, m2, r);
37981 r = gen_rtx_NEG (mode, r);
37982 emit_insn (gen_rtx_SET (dst, r));
37985 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
37986 add a reg_note saying that this was a division. Support both scalar and
37987 vector divide. Assumes no trapping math and finite arguments. */
37989 void
37990 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
37992 machine_mode mode = GET_MODE (dst);
37993 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
37994 int i;
37996 /* Low precision estimates guarantee 5 bits of accuracy. High
37997 precision estimates guarantee 14 bits of accuracy. SFmode
37998 requires 23 bits of accuracy. DFmode requires 52 bits of
37999 accuracy. Each pass at least doubles the accuracy, leading
38000 to the following. */
38001 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
38002 if (mode == DFmode || mode == V2DFmode)
38003 passes++;
38005 enum insn_code code = optab_handler (smul_optab, mode);
38006 insn_gen_fn gen_mul = GEN_FCN (code);
38008 gcc_assert (code != CODE_FOR_nothing);
38010 one = rs6000_load_constant_and_splat (mode, dconst1);
38012 /* x0 = 1./d estimate */
38013 x0 = gen_reg_rtx (mode);
38014 emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
38015 UNSPEC_FRES)));
38017 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
38018 if (passes > 1) {
38020 /* e0 = 1. - d * x0 */
38021 e0 = gen_reg_rtx (mode);
38022 rs6000_emit_nmsub (e0, d, x0, one);
38024 /* x1 = x0 + e0 * x0 */
38025 x1 = gen_reg_rtx (mode);
38026 rs6000_emit_madd (x1, e0, x0, x0);
38028 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
38029 ++i, xprev = xnext, eprev = enext) {
38031 /* enext = eprev * eprev */
38032 enext = gen_reg_rtx (mode);
38033 emit_insn (gen_mul (enext, eprev, eprev));
38035 /* xnext = xprev + enext * xprev */
38036 xnext = gen_reg_rtx (mode);
38037 rs6000_emit_madd (xnext, enext, xprev, xprev);
38040 } else
38041 xprev = x0;
38043 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
38045 /* u = n * xprev */
38046 u = gen_reg_rtx (mode);
38047 emit_insn (gen_mul (u, n, xprev));
38049 /* v = n - (d * u) */
38050 v = gen_reg_rtx (mode);
38051 rs6000_emit_nmsub (v, d, u, n);
38053 /* dst = (v * xprev) + u */
38054 rs6000_emit_madd (dst, v, xprev, u);
38056 if (note_p)
38057 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
38060 /* Goldschmidt's Algorithm for single/double-precision floating point
38061 sqrt and rsqrt. Assumes no trapping math and finite arguments. */
38063 void
38064 rs6000_emit_swsqrt (rtx dst, rtx src, bool recip)
38066 machine_mode mode = GET_MODE (src);
38067 rtx e = gen_reg_rtx (mode);
38068 rtx g = gen_reg_rtx (mode);
38069 rtx h = gen_reg_rtx (mode);
38071 /* Low precision estimates guarantee 5 bits of accuracy. High
38072 precision estimates guarantee 14 bits of accuracy. SFmode
38073 requires 23 bits of accuracy. DFmode requires 52 bits of
38074 accuracy. Each pass at least doubles the accuracy, leading
38075 to the following. */
38076 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
38077 if (mode == DFmode || mode == V2DFmode)
38078 passes++;
38080 int i;
38081 rtx mhalf;
38082 enum insn_code code = optab_handler (smul_optab, mode);
38083 insn_gen_fn gen_mul = GEN_FCN (code);
38085 gcc_assert (code != CODE_FOR_nothing);
38087 mhalf = rs6000_load_constant_and_splat (mode, dconsthalf);
38089 /* e = rsqrt estimate */
38090 emit_insn (gen_rtx_SET (e, gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
38091 UNSPEC_RSQRT)));
38093 /* If (src == 0.0) filter infinity to prevent NaN for sqrt(0.0). */
38094 if (!recip)
38096 rtx zero = force_reg (mode, CONST0_RTX (mode));
38098 if (mode == SFmode)
38100 rtx target = emit_conditional_move (e, GT, src, zero, mode,
38101 e, zero, mode, 0);
38102 if (target != e)
38103 emit_move_insn (e, target);
38105 else
38107 rtx cond = gen_rtx_GT (VOIDmode, e, zero);
38108 rs6000_emit_vector_cond_expr (e, e, zero, cond, src, zero);
38112 /* g = sqrt estimate. */
38113 emit_insn (gen_mul (g, e, src));
38114 /* h = 1/(2*sqrt) estimate. */
38115 emit_insn (gen_mul (h, e, mhalf));
38117 if (recip)
38119 if (passes == 1)
38121 rtx t = gen_reg_rtx (mode);
38122 rs6000_emit_nmsub (t, g, h, mhalf);
38123 /* Apply correction directly to 1/rsqrt estimate. */
38124 rs6000_emit_madd (dst, e, t, e);
38126 else
38128 for (i = 0; i < passes; i++)
38130 rtx t1 = gen_reg_rtx (mode);
38131 rtx g1 = gen_reg_rtx (mode);
38132 rtx h1 = gen_reg_rtx (mode);
38134 rs6000_emit_nmsub (t1, g, h, mhalf);
38135 rs6000_emit_madd (g1, g, t1, g);
38136 rs6000_emit_madd (h1, h, t1, h);
38138 g = g1;
38139 h = h1;
38141 /* Multiply by 2 for 1/rsqrt. */
38142 emit_insn (gen_add3_insn (dst, h, h));
38145 else
38147 rtx t = gen_reg_rtx (mode);
38148 rs6000_emit_nmsub (t, g, h, mhalf);
38149 rs6000_emit_madd (dst, g, t, g);
38152 return;
38155 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
38156 (Power7) targets. DST is the target, and SRC is the argument operand. */
38158 void
38159 rs6000_emit_popcount (rtx dst, rtx src)
38161 machine_mode mode = GET_MODE (dst);
38162 rtx tmp1, tmp2;
38164 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
38165 if (TARGET_POPCNTD)
38167 if (mode == SImode)
38168 emit_insn (gen_popcntdsi2 (dst, src));
38169 else
38170 emit_insn (gen_popcntddi2 (dst, src));
38171 return;
38174 tmp1 = gen_reg_rtx (mode);
38176 if (mode == SImode)
38178 emit_insn (gen_popcntbsi2 (tmp1, src));
38179 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
38180 NULL_RTX, 0);
38181 tmp2 = force_reg (SImode, tmp2);
38182 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
38184 else
38186 emit_insn (gen_popcntbdi2 (tmp1, src));
38187 tmp2 = expand_mult (DImode, tmp1,
38188 GEN_INT ((HOST_WIDE_INT)
38189 0x01010101 << 32 | 0x01010101),
38190 NULL_RTX, 0);
38191 tmp2 = force_reg (DImode, tmp2);
38192 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
38197 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
38198 target, and SRC is the argument operand. */
38200 void
38201 rs6000_emit_parity (rtx dst, rtx src)
38203 machine_mode mode = GET_MODE (dst);
38204 rtx tmp;
38206 tmp = gen_reg_rtx (mode);
38208 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
38209 if (TARGET_CMPB)
38211 if (mode == SImode)
38213 emit_insn (gen_popcntbsi2 (tmp, src));
38214 emit_insn (gen_paritysi2_cmpb (dst, tmp));
38216 else
38218 emit_insn (gen_popcntbdi2 (tmp, src));
38219 emit_insn (gen_paritydi2_cmpb (dst, tmp));
38221 return;
38224 if (mode == SImode)
38226 /* Is mult+shift >= shift+xor+shift+xor? */
38227 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
38229 rtx tmp1, tmp2, tmp3, tmp4;
38231 tmp1 = gen_reg_rtx (SImode);
38232 emit_insn (gen_popcntbsi2 (tmp1, src));
38234 tmp2 = gen_reg_rtx (SImode);
38235 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
38236 tmp3 = gen_reg_rtx (SImode);
38237 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
38239 tmp4 = gen_reg_rtx (SImode);
38240 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
38241 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
38243 else
38244 rs6000_emit_popcount (tmp, src);
38245 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
38247 else
38249 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
38250 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
38252 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
38254 tmp1 = gen_reg_rtx (DImode);
38255 emit_insn (gen_popcntbdi2 (tmp1, src));
38257 tmp2 = gen_reg_rtx (DImode);
38258 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
38259 tmp3 = gen_reg_rtx (DImode);
38260 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
38262 tmp4 = gen_reg_rtx (DImode);
38263 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
38264 tmp5 = gen_reg_rtx (DImode);
38265 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
38267 tmp6 = gen_reg_rtx (DImode);
38268 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
38269 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
38271 else
38272 rs6000_emit_popcount (tmp, src);
38273 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
38277 /* Expand an Altivec constant permutation for little endian mode.
38278 There are two issues: First, the two input operands must be
38279 swapped so that together they form a double-wide array in LE
38280 order. Second, the vperm instruction has surprising behavior
38281 in LE mode: it interprets the elements of the source vectors
38282 in BE mode ("left to right") and interprets the elements of
38283 the destination vector in LE mode ("right to left"). To
38284 correct for this, we must subtract each element of the permute
38285 control vector from 31.
38287 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
38288 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
38289 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
38290 serve as the permute control vector. Then, in BE mode,
38292 vperm 9,10,11,12
38294 places the desired result in vr9. However, in LE mode the
38295 vector contents will be
38297 vr10 = 00000003 00000002 00000001 00000000
38298 vr11 = 00000007 00000006 00000005 00000004
38300 The result of the vperm using the same permute control vector is
38302 vr9 = 05000000 07000000 01000000 03000000
38304 That is, the leftmost 4 bytes of vr10 are interpreted as the
38305 source for the rightmost 4 bytes of vr9, and so on.
38307 If we change the permute control vector to
38309 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
38311 and issue
38313 vperm 9,11,10,12
38315 we get the desired
38317 vr9 = 00000006 00000004 00000002 00000000. */
38319 void
38320 altivec_expand_vec_perm_const_le (rtx operands[4])
38322 unsigned int i;
38323 rtx perm[16];
38324 rtx constv, unspec;
38325 rtx target = operands[0];
38326 rtx op0 = operands[1];
38327 rtx op1 = operands[2];
38328 rtx sel = operands[3];
38330 /* Unpack and adjust the constant selector. */
38331 for (i = 0; i < 16; ++i)
38333 rtx e = XVECEXP (sel, 0, i);
38334 unsigned int elt = 31 - (INTVAL (e) & 31);
38335 perm[i] = GEN_INT (elt);
38338 /* Expand to a permute, swapping the inputs and using the
38339 adjusted selector. */
38340 if (!REG_P (op0))
38341 op0 = force_reg (V16QImode, op0);
38342 if (!REG_P (op1))
38343 op1 = force_reg (V16QImode, op1);
38345 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
38346 constv = force_reg (V16QImode, constv);
38347 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
38348 UNSPEC_VPERM);
38349 if (!REG_P (target))
38351 rtx tmp = gen_reg_rtx (V16QImode);
38352 emit_move_insn (tmp, unspec);
38353 unspec = tmp;
38356 emit_move_insn (target, unspec);
38359 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
38360 permute control vector. But here it's not a constant, so we must
38361 generate a vector NAND or NOR to do the adjustment. */
38363 void
38364 altivec_expand_vec_perm_le (rtx operands[4])
38366 rtx notx, iorx, unspec;
38367 rtx target = operands[0];
38368 rtx op0 = operands[1];
38369 rtx op1 = operands[2];
38370 rtx sel = operands[3];
38371 rtx tmp = target;
38372 rtx norreg = gen_reg_rtx (V16QImode);
38373 machine_mode mode = GET_MODE (target);
38375 /* Get everything in regs so the pattern matches. */
38376 if (!REG_P (op0))
38377 op0 = force_reg (mode, op0);
38378 if (!REG_P (op1))
38379 op1 = force_reg (mode, op1);
38380 if (!REG_P (sel))
38381 sel = force_reg (V16QImode, sel);
38382 if (!REG_P (target))
38383 tmp = gen_reg_rtx (mode);
38385 if (TARGET_P9_VECTOR)
38387 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op0, op1, sel),
38388 UNSPEC_VPERMR);
38390 else
38392 /* Invert the selector with a VNAND if available, else a VNOR.
38393 The VNAND is preferred for future fusion opportunities. */
38394 notx = gen_rtx_NOT (V16QImode, sel);
38395 iorx = (TARGET_P8_VECTOR
38396 ? gen_rtx_IOR (V16QImode, notx, notx)
38397 : gen_rtx_AND (V16QImode, notx, notx));
38398 emit_insn (gen_rtx_SET (norreg, iorx));
38400 /* Permute with operands reversed and adjusted selector. */
38401 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
38402 UNSPEC_VPERM);
38405 /* Copy into target, possibly by way of a register. */
38406 if (!REG_P (target))
38408 emit_move_insn (tmp, unspec);
38409 unspec = tmp;
38412 emit_move_insn (target, unspec);
38415 /* Expand an Altivec constant permutation. Return true if we match
38416 an efficient implementation; false to fall back to VPERM. */
38418 bool
38419 altivec_expand_vec_perm_const (rtx operands[4])
38421 struct altivec_perm_insn {
38422 HOST_WIDE_INT mask;
38423 enum insn_code impl;
38424 unsigned char perm[16];
38426 static const struct altivec_perm_insn patterns[] = {
38427 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
38428 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
38429 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
38430 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
38431 { OPTION_MASK_ALTIVEC,
38432 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
38433 : CODE_FOR_altivec_vmrglb_direct),
38434 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
38435 { OPTION_MASK_ALTIVEC,
38436 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
38437 : CODE_FOR_altivec_vmrglh_direct),
38438 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
38439 { OPTION_MASK_ALTIVEC,
38440 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
38441 : CODE_FOR_altivec_vmrglw_direct),
38442 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
38443 { OPTION_MASK_ALTIVEC,
38444 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
38445 : CODE_FOR_altivec_vmrghb_direct),
38446 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
38447 { OPTION_MASK_ALTIVEC,
38448 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
38449 : CODE_FOR_altivec_vmrghh_direct),
38450 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
38451 { OPTION_MASK_ALTIVEC,
38452 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
38453 : CODE_FOR_altivec_vmrghw_direct),
38454 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
38455 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
38456 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
38457 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
38458 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
38461 unsigned int i, j, elt, which;
38462 unsigned char perm[16];
38463 rtx target, op0, op1, sel, x;
38464 bool one_vec;
38466 target = operands[0];
38467 op0 = operands[1];
38468 op1 = operands[2];
38469 sel = operands[3];
38471 /* Unpack the constant selector. */
38472 for (i = which = 0; i < 16; ++i)
38474 rtx e = XVECEXP (sel, 0, i);
38475 elt = INTVAL (e) & 31;
38476 which |= (elt < 16 ? 1 : 2);
38477 perm[i] = elt;
38480 /* Simplify the constant selector based on operands. */
38481 switch (which)
38483 default:
38484 gcc_unreachable ();
38486 case 3:
38487 one_vec = false;
38488 if (!rtx_equal_p (op0, op1))
38489 break;
38490 /* FALLTHRU */
38492 case 2:
38493 for (i = 0; i < 16; ++i)
38494 perm[i] &= 15;
38495 op0 = op1;
38496 one_vec = true;
38497 break;
38499 case 1:
38500 op1 = op0;
38501 one_vec = true;
38502 break;
38505 /* Look for splat patterns. */
38506 if (one_vec)
38508 elt = perm[0];
38510 for (i = 0; i < 16; ++i)
38511 if (perm[i] != elt)
38512 break;
38513 if (i == 16)
38515 if (!BYTES_BIG_ENDIAN)
38516 elt = 15 - elt;
38517 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
38518 return true;
38521 if (elt % 2 == 0)
38523 for (i = 0; i < 16; i += 2)
38524 if (perm[i] != elt || perm[i + 1] != elt + 1)
38525 break;
38526 if (i == 16)
38528 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
38529 x = gen_reg_rtx (V8HImode);
38530 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
38531 GEN_INT (field)));
38532 emit_move_insn (target, gen_lowpart (V16QImode, x));
38533 return true;
38537 if (elt % 4 == 0)
38539 for (i = 0; i < 16; i += 4)
38540 if (perm[i] != elt
38541 || perm[i + 1] != elt + 1
38542 || perm[i + 2] != elt + 2
38543 || perm[i + 3] != elt + 3)
38544 break;
38545 if (i == 16)
38547 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
38548 x = gen_reg_rtx (V4SImode);
38549 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
38550 GEN_INT (field)));
38551 emit_move_insn (target, gen_lowpart (V16QImode, x));
38552 return true;
38557 /* Look for merge and pack patterns. */
38558 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
38560 bool swapped;
38562 if ((patterns[j].mask & rs6000_isa_flags) == 0)
38563 continue;
38565 elt = patterns[j].perm[0];
38566 if (perm[0] == elt)
38567 swapped = false;
38568 else if (perm[0] == elt + 16)
38569 swapped = true;
38570 else
38571 continue;
38572 for (i = 1; i < 16; ++i)
38574 elt = patterns[j].perm[i];
38575 if (swapped)
38576 elt = (elt >= 16 ? elt - 16 : elt + 16);
38577 else if (one_vec && elt >= 16)
38578 elt -= 16;
38579 if (perm[i] != elt)
38580 break;
38582 if (i == 16)
38584 enum insn_code icode = patterns[j].impl;
38585 machine_mode omode = insn_data[icode].operand[0].mode;
38586 machine_mode imode = insn_data[icode].operand[1].mode;
38588 /* For little-endian, don't use vpkuwum and vpkuhum if the
38589 underlying vector type is not V4SI and V8HI, respectively.
38590 For example, using vpkuwum with a V8HI picks up the even
38591 halfwords (BE numbering) when the even halfwords (LE
38592 numbering) are what we need. */
38593 if (!BYTES_BIG_ENDIAN
38594 && icode == CODE_FOR_altivec_vpkuwum_direct
38595 && ((GET_CODE (op0) == REG
38596 && GET_MODE (op0) != V4SImode)
38597 || (GET_CODE (op0) == SUBREG
38598 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
38599 continue;
38600 if (!BYTES_BIG_ENDIAN
38601 && icode == CODE_FOR_altivec_vpkuhum_direct
38602 && ((GET_CODE (op0) == REG
38603 && GET_MODE (op0) != V8HImode)
38604 || (GET_CODE (op0) == SUBREG
38605 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
38606 continue;
38608 /* For little-endian, the two input operands must be swapped
38609 (or swapped back) to ensure proper right-to-left numbering
38610 from 0 to 2N-1. */
38611 if (swapped ^ !BYTES_BIG_ENDIAN)
38612 std::swap (op0, op1);
38613 if (imode != V16QImode)
38615 op0 = gen_lowpart (imode, op0);
38616 op1 = gen_lowpart (imode, op1);
38618 if (omode == V16QImode)
38619 x = target;
38620 else
38621 x = gen_reg_rtx (omode);
38622 emit_insn (GEN_FCN (icode) (x, op0, op1));
38623 if (omode != V16QImode)
38624 emit_move_insn (target, gen_lowpart (V16QImode, x));
38625 return true;
38629 if (!BYTES_BIG_ENDIAN)
38631 altivec_expand_vec_perm_const_le (operands);
38632 return true;
38635 return false;
38638 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
38639 Return true if we match an efficient implementation. */
38641 static bool
38642 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
38643 unsigned char perm0, unsigned char perm1)
38645 rtx x;
38647 /* If both selectors come from the same operand, fold to single op. */
38648 if ((perm0 & 2) == (perm1 & 2))
38650 if (perm0 & 2)
38651 op0 = op1;
38652 else
38653 op1 = op0;
38655 /* If both operands are equal, fold to simpler permutation. */
38656 if (rtx_equal_p (op0, op1))
38658 perm0 = perm0 & 1;
38659 perm1 = (perm1 & 1) + 2;
38661 /* If the first selector comes from the second operand, swap. */
38662 else if (perm0 & 2)
38664 if (perm1 & 2)
38665 return false;
38666 perm0 -= 2;
38667 perm1 += 2;
38668 std::swap (op0, op1);
38670 /* If the second selector does not come from the second operand, fail. */
38671 else if ((perm1 & 2) == 0)
38672 return false;
38674 /* Success! */
38675 if (target != NULL)
38677 machine_mode vmode, dmode;
38678 rtvec v;
38680 vmode = GET_MODE (target);
38681 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
38682 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
38683 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
38684 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
38685 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
38686 emit_insn (gen_rtx_SET (target, x));
38688 return true;
38691 bool
38692 rs6000_expand_vec_perm_const (rtx operands[4])
38694 rtx target, op0, op1, sel;
38695 unsigned char perm0, perm1;
38697 target = operands[0];
38698 op0 = operands[1];
38699 op1 = operands[2];
38700 sel = operands[3];
38702 /* Unpack the constant selector. */
38703 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
38704 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
38706 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
38709 /* Test whether a constant permutation is supported. */
38711 static bool
38712 rs6000_vectorize_vec_perm_const_ok (machine_mode vmode,
38713 const unsigned char *sel)
38715 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
38716 if (TARGET_ALTIVEC)
38717 return true;
38719 /* Check for ps_merge* or evmerge* insns. */
38720 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
38721 || (TARGET_SPE && vmode == V2SImode))
38723 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
38724 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
38725 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
38728 return false;
38731 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
38733 static void
38734 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
38735 machine_mode vmode, unsigned nelt, rtx perm[])
38737 machine_mode imode;
38738 rtx x;
38740 imode = vmode;
38741 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
38742 imode = mode_for_int_vector (vmode).require ();
38744 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
38745 x = expand_vec_perm (vmode, op0, op1, x, target);
38746 if (x != target)
38747 emit_move_insn (target, x);
38750 /* Expand an extract even operation. */
38752 void
38753 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
38755 machine_mode vmode = GET_MODE (target);
38756 unsigned i, nelt = GET_MODE_NUNITS (vmode);
38757 rtx perm[16];
38759 for (i = 0; i < nelt; i++)
38760 perm[i] = GEN_INT (i * 2);
38762 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
38765 /* Expand a vector interleave operation. */
38767 void
38768 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
38770 machine_mode vmode = GET_MODE (target);
38771 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
38772 rtx perm[16];
38774 high = (highp ? 0 : nelt / 2);
38775 for (i = 0; i < nelt / 2; i++)
38777 perm[i * 2] = GEN_INT (i + high);
38778 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
38781 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
38784 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT. */
38785 void
38786 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
38788 HOST_WIDE_INT hwi_scale (scale);
38789 REAL_VALUE_TYPE r_pow;
38790 rtvec v = rtvec_alloc (2);
38791 rtx elt;
38792 rtx scale_vec = gen_reg_rtx (V2DFmode);
38793 (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
38794 elt = const_double_from_real_value (r_pow, DFmode);
38795 RTVEC_ELT (v, 0) = elt;
38796 RTVEC_ELT (v, 1) = elt;
38797 rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
38798 emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
38801 /* Return an RTX representing where to find the function value of a
38802 function returning MODE. */
38803 static rtx
38804 rs6000_complex_function_value (machine_mode mode)
38806 unsigned int regno;
38807 rtx r1, r2;
38808 machine_mode inner = GET_MODE_INNER (mode);
38809 unsigned int inner_bytes = GET_MODE_UNIT_SIZE (mode);
38811 if (TARGET_FLOAT128_TYPE
38812 && (mode == KCmode
38813 || (mode == TCmode && TARGET_IEEEQUAD)))
38814 regno = ALTIVEC_ARG_RETURN;
38816 else if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
38817 regno = FP_ARG_RETURN;
38819 else
38821 regno = GP_ARG_RETURN;
38823 /* 32-bit is OK since it'll go in r3/r4. */
38824 if (TARGET_32BIT && inner_bytes >= 4)
38825 return gen_rtx_REG (mode, regno);
38828 if (inner_bytes >= 8)
38829 return gen_rtx_REG (mode, regno);
38831 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
38832 const0_rtx);
38833 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
38834 GEN_INT (inner_bytes));
38835 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
38838 /* Return an rtx describing a return value of MODE as a PARALLEL
38839 in N_ELTS registers, each of mode ELT_MODE, starting at REGNO,
38840 stride REG_STRIDE. */
38842 static rtx
38843 rs6000_parallel_return (machine_mode mode,
38844 int n_elts, machine_mode elt_mode,
38845 unsigned int regno, unsigned int reg_stride)
38847 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
38849 int i;
38850 for (i = 0; i < n_elts; i++)
38852 rtx r = gen_rtx_REG (elt_mode, regno);
38853 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
38854 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
38855 regno += reg_stride;
38858 return par;
38861 /* Target hook for TARGET_FUNCTION_VALUE.
38863 On the SPE, both FPs and vectors are returned in r3.
38865 On RS/6000 an integer value is in r3 and a floating-point value is in
38866 fp1, unless -msoft-float. */
38868 static rtx
38869 rs6000_function_value (const_tree valtype,
38870 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
38871 bool outgoing ATTRIBUTE_UNUSED)
38873 machine_mode mode;
38874 unsigned int regno;
38875 machine_mode elt_mode;
38876 int n_elts;
38878 /* Special handling for structs in darwin64. */
38879 if (TARGET_MACHO
38880 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
38882 CUMULATIVE_ARGS valcum;
38883 rtx valret;
38885 valcum.words = 0;
38886 valcum.fregno = FP_ARG_MIN_REG;
38887 valcum.vregno = ALTIVEC_ARG_MIN_REG;
38888 /* Do a trial code generation as if this were going to be passed as
38889 an argument; if any part goes in memory, we return NULL. */
38890 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
38891 if (valret)
38892 return valret;
38893 /* Otherwise fall through to standard ABI rules. */
38896 mode = TYPE_MODE (valtype);
38898 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
38899 if (rs6000_discover_homogeneous_aggregate (mode, valtype, &elt_mode, &n_elts))
38901 int first_reg, n_regs;
38903 if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (elt_mode))
38905 /* _Decimal128 must use even/odd register pairs. */
38906 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
38907 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
38909 else
38911 first_reg = ALTIVEC_ARG_RETURN;
38912 n_regs = 1;
38915 return rs6000_parallel_return (mode, n_elts, elt_mode, first_reg, n_regs);
38918 /* Some return value types need be split in -mpowerpc64, 32bit ABI. */
38919 if (TARGET_32BIT && TARGET_POWERPC64)
38920 switch (mode)
38922 default:
38923 break;
38924 case E_DImode:
38925 case E_SCmode:
38926 case E_DCmode:
38927 case E_TCmode:
38928 int count = GET_MODE_SIZE (mode) / 4;
38929 return rs6000_parallel_return (mode, count, SImode, GP_ARG_RETURN, 1);
38932 if ((INTEGRAL_TYPE_P (valtype)
38933 && GET_MODE_BITSIZE (mode) < (TARGET_32BIT ? 32 : 64))
38934 || POINTER_TYPE_P (valtype))
38935 mode = TARGET_32BIT ? SImode : DImode;
38937 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
38938 /* _Decimal128 must use an even/odd register pair. */
38939 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
38940 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
38941 && !FLOAT128_VECTOR_P (mode)
38942 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
38943 regno = FP_ARG_RETURN;
38944 else if (TREE_CODE (valtype) == COMPLEX_TYPE
38945 && targetm.calls.split_complex_arg)
38946 return rs6000_complex_function_value (mode);
38947 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
38948 return register is used in both cases, and we won't see V2DImode/V2DFmode
38949 for pure altivec, combine the two cases. */
38950 else if ((TREE_CODE (valtype) == VECTOR_TYPE || FLOAT128_VECTOR_P (mode))
38951 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
38952 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
38953 regno = ALTIVEC_ARG_RETURN;
38954 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
38955 && (mode == DFmode || mode == DCmode
38956 || FLOAT128_IBM_P (mode) || mode == TCmode))
38957 return spe_build_register_parallel (mode, GP_ARG_RETURN);
38958 else
38959 regno = GP_ARG_RETURN;
38961 return gen_rtx_REG (mode, regno);
38964 /* Define how to find the value returned by a library function
38965 assuming the value has mode MODE. */
38967 rs6000_libcall_value (machine_mode mode)
38969 unsigned int regno;
38971 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
38972 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
38973 return rs6000_parallel_return (mode, 2, SImode, GP_ARG_RETURN, 1);
38975 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
38976 /* _Decimal128 must use an even/odd register pair. */
38977 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
38978 else if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode)
38979 && TARGET_HARD_FLOAT && TARGET_FPRS
38980 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
38981 regno = FP_ARG_RETURN;
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 (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
38986 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
38987 regno = ALTIVEC_ARG_RETURN;
38988 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
38989 return rs6000_complex_function_value (mode);
38990 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
38991 && (mode == DFmode || mode == DCmode
38992 || FLOAT128_IBM_P (mode) || mode == TCmode))
38993 return spe_build_register_parallel (mode, GP_ARG_RETURN);
38994 else
38995 regno = GP_ARG_RETURN;
38997 return gen_rtx_REG (mode, regno);
39001 /* Return true if we use LRA instead of reload pass. */
39002 static bool
39003 rs6000_lra_p (void)
39005 return TARGET_LRA;
39008 /* Compute register pressure classes. We implement the target hook to avoid
39009 IRA picking something like NON_SPECIAL_REGS as a pressure class, which can
39010 lead to incorrect estimates of number of available registers and therefor
39011 increased register pressure/spill. */
39012 static int
39013 rs6000_compute_pressure_classes (enum reg_class *pressure_classes)
39015 int n;
39017 n = 0;
39018 pressure_classes[n++] = GENERAL_REGS;
39019 if (TARGET_VSX)
39020 pressure_classes[n++] = VSX_REGS;
39021 else
39023 if (TARGET_ALTIVEC)
39024 pressure_classes[n++] = ALTIVEC_REGS;
39025 if (TARGET_HARD_FLOAT && TARGET_FPRS)
39026 pressure_classes[n++] = FLOAT_REGS;
39028 pressure_classes[n++] = CR_REGS;
39029 pressure_classes[n++] = SPECIAL_REGS;
39031 return n;
39034 /* Given FROM and TO register numbers, say whether this elimination is allowed.
39035 Frame pointer elimination is automatically handled.
39037 For the RS/6000, if frame pointer elimination is being done, we would like
39038 to convert ap into fp, not sp.
39040 We need r30 if -mminimal-toc was specified, and there are constant pool
39041 references. */
39043 static bool
39044 rs6000_can_eliminate (const int from, const int to)
39046 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
39047 ? ! frame_pointer_needed
39048 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
39049 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC
39050 || constant_pool_empty_p ()
39051 : true);
39054 /* Define the offset between two registers, FROM to be eliminated and its
39055 replacement TO, at the start of a routine. */
39056 HOST_WIDE_INT
39057 rs6000_initial_elimination_offset (int from, int to)
39059 rs6000_stack_t *info = rs6000_stack_info ();
39060 HOST_WIDE_INT offset;
39062 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
39063 offset = info->push_p ? 0 : -info->total_size;
39064 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
39066 offset = info->push_p ? 0 : -info->total_size;
39067 if (FRAME_GROWS_DOWNWARD)
39068 offset += info->fixed_size + info->vars_size + info->parm_size;
39070 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
39071 offset = FRAME_GROWS_DOWNWARD
39072 ? info->fixed_size + info->vars_size + info->parm_size
39073 : 0;
39074 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
39075 offset = info->total_size;
39076 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
39077 offset = info->push_p ? info->total_size : 0;
39078 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
39079 offset = 0;
39080 else
39081 gcc_unreachable ();
39083 return offset;
39086 static rtx
39087 rs6000_dwarf_register_span (rtx reg)
39089 rtx parts[8];
39090 int i, words;
39091 unsigned regno = REGNO (reg);
39092 machine_mode mode = GET_MODE (reg);
39094 if (TARGET_SPE
39095 && regno < 32
39096 && (SPE_VECTOR_MODE (GET_MODE (reg))
39097 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
39098 && mode != SFmode && mode != SDmode && mode != SCmode)))
39100 else
39101 return NULL_RTX;
39103 regno = REGNO (reg);
39105 /* The duality of the SPE register size wreaks all kinds of havoc.
39106 This is a way of distinguishing r0 in 32-bits from r0 in
39107 64-bits. */
39108 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
39109 gcc_assert (words <= 4);
39110 for (i = 0; i < words; i++, regno++)
39112 if (BYTES_BIG_ENDIAN)
39114 parts[2 * i] = gen_rtx_REG (SImode, regno + FIRST_SPE_HIGH_REGNO);
39115 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
39117 else
39119 parts[2 * i] = gen_rtx_REG (SImode, regno);
39120 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + FIRST_SPE_HIGH_REGNO);
39124 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
39127 /* Fill in sizes for SPE register high parts in table used by unwinder. */
39129 static void
39130 rs6000_init_dwarf_reg_sizes_extra (tree address)
39132 if (TARGET_SPE)
39134 int i;
39135 machine_mode mode = TYPE_MODE (char_type_node);
39136 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
39137 rtx mem = gen_rtx_MEM (BLKmode, addr);
39138 rtx value = gen_int_mode (4, mode);
39140 for (i = FIRST_SPE_HIGH_REGNO; i < LAST_SPE_HIGH_REGNO+1; i++)
39142 int column = DWARF_REG_TO_UNWIND_COLUMN
39143 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
39144 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
39146 emit_move_insn (adjust_address (mem, mode, offset), value);
39150 if (TARGET_MACHO && ! TARGET_ALTIVEC)
39152 int i;
39153 machine_mode mode = TYPE_MODE (char_type_node);
39154 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
39155 rtx mem = gen_rtx_MEM (BLKmode, addr);
39156 rtx value = gen_int_mode (16, mode);
39158 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
39159 The unwinder still needs to know the size of Altivec registers. */
39161 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
39163 int column = DWARF_REG_TO_UNWIND_COLUMN
39164 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
39165 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
39167 emit_move_insn (adjust_address (mem, mode, offset), value);
39172 /* Map internal gcc register numbers to debug format register numbers.
39173 FORMAT specifies the type of debug register number to use:
39174 0 -- debug information, except for frame-related sections
39175 1 -- DWARF .debug_frame section
39176 2 -- DWARF .eh_frame section */
39178 unsigned int
39179 rs6000_dbx_register_number (unsigned int regno, unsigned int format)
39181 /* We never use the GCC internal number for SPE high registers.
39182 Those are mapped to the 1200..1231 range for all debug formats. */
39183 if (SPE_HIGH_REGNO_P (regno))
39184 return regno - FIRST_SPE_HIGH_REGNO + 1200;
39186 /* Except for the above, we use the internal number for non-DWARF
39187 debug information, and also for .eh_frame. */
39188 if ((format == 0 && write_symbols != DWARF2_DEBUG) || format == 2)
39189 return regno;
39191 /* On some platforms, we use the standard DWARF register
39192 numbering for .debug_info and .debug_frame. */
39193 #ifdef RS6000_USE_DWARF_NUMBERING
39194 if (regno <= 63)
39195 return regno;
39196 if (regno == LR_REGNO)
39197 return 108;
39198 if (regno == CTR_REGNO)
39199 return 109;
39200 /* Special handling for CR for .debug_frame: rs6000_emit_prologue has
39201 translated any combination of CR2, CR3, CR4 saves to a save of CR2.
39202 The actual code emitted saves the whole of CR, so we map CR2_REGNO
39203 to the DWARF reg for CR. */
39204 if (format == 1 && regno == CR2_REGNO)
39205 return 64;
39206 if (CR_REGNO_P (regno))
39207 return regno - CR0_REGNO + 86;
39208 if (regno == CA_REGNO)
39209 return 101; /* XER */
39210 if (ALTIVEC_REGNO_P (regno))
39211 return regno - FIRST_ALTIVEC_REGNO + 1124;
39212 if (regno == VRSAVE_REGNO)
39213 return 356;
39214 if (regno == VSCR_REGNO)
39215 return 67;
39216 if (regno == SPE_ACC_REGNO)
39217 return 99;
39218 if (regno == SPEFSCR_REGNO)
39219 return 612;
39220 #endif
39221 return regno;
39224 /* target hook eh_return_filter_mode */
39225 static scalar_int_mode
39226 rs6000_eh_return_filter_mode (void)
39228 return TARGET_32BIT ? SImode : word_mode;
39231 /* Target hook for scalar_mode_supported_p. */
39232 static bool
39233 rs6000_scalar_mode_supported_p (scalar_mode mode)
39235 /* -m32 does not support TImode. This is the default, from
39236 default_scalar_mode_supported_p. For -m32 -mpowerpc64 we want the
39237 same ABI as for -m32. But default_scalar_mode_supported_p allows
39238 integer modes of precision 2 * BITS_PER_WORD, which matches TImode
39239 for -mpowerpc64. */
39240 if (TARGET_32BIT && mode == TImode)
39241 return false;
39243 if (DECIMAL_FLOAT_MODE_P (mode))
39244 return default_decimal_float_supported_p ();
39245 else if (TARGET_FLOAT128_TYPE && (mode == KFmode || mode == IFmode))
39246 return true;
39247 else
39248 return default_scalar_mode_supported_p (mode);
39251 /* Target hook for vector_mode_supported_p. */
39252 static bool
39253 rs6000_vector_mode_supported_p (machine_mode mode)
39256 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
39257 return true;
39259 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
39260 return true;
39262 /* There is no vector form for IEEE 128-bit. If we return true for IEEE
39263 128-bit, the compiler might try to widen IEEE 128-bit to IBM
39264 double-double. */
39265 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode) && !FLOAT128_IEEE_P (mode))
39266 return true;
39268 else
39269 return false;
39272 /* Target hook for floatn_mode. */
39273 static opt_scalar_float_mode
39274 rs6000_floatn_mode (int n, bool extended)
39276 if (extended)
39278 switch (n)
39280 case 32:
39281 return DFmode;
39283 case 64:
39284 if (TARGET_FLOAT128_KEYWORD)
39285 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
39286 else
39287 return opt_scalar_float_mode ();
39289 case 128:
39290 return opt_scalar_float_mode ();
39292 default:
39293 /* Those are the only valid _FloatNx types. */
39294 gcc_unreachable ();
39297 else
39299 switch (n)
39301 case 32:
39302 return SFmode;
39304 case 64:
39305 return DFmode;
39307 case 128:
39308 if (TARGET_FLOAT128_KEYWORD)
39309 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
39310 else
39311 return opt_scalar_float_mode ();
39313 default:
39314 return opt_scalar_float_mode ();
39320 /* Target hook for c_mode_for_suffix. */
39321 static machine_mode
39322 rs6000_c_mode_for_suffix (char suffix)
39324 if (TARGET_FLOAT128_TYPE)
39326 if (suffix == 'q' || suffix == 'Q')
39327 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
39329 /* At the moment, we are not defining a suffix for IBM extended double.
39330 If/when the default for -mabi=ieeelongdouble is changed, and we want
39331 to support __ibm128 constants in legacy library code, we may need to
39332 re-evalaute this decision. Currently, c-lex.c only supports 'w' and
39333 'q' as machine dependent suffixes. The x86_64 port uses 'w' for
39334 __float80 constants. */
39337 return VOIDmode;
39340 /* Target hook for invalid_arg_for_unprototyped_fn. */
39341 static const char *
39342 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
39344 return (!rs6000_darwin64_abi
39345 && typelist == 0
39346 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
39347 && (funcdecl == NULL_TREE
39348 || (TREE_CODE (funcdecl) == FUNCTION_DECL
39349 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
39350 ? N_("AltiVec argument passed to unprototyped function")
39351 : NULL;
39354 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
39355 setup by using __stack_chk_fail_local hidden function instead of
39356 calling __stack_chk_fail directly. Otherwise it is better to call
39357 __stack_chk_fail directly. */
39359 static tree ATTRIBUTE_UNUSED
39360 rs6000_stack_protect_fail (void)
39362 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
39363 ? default_hidden_stack_protect_fail ()
39364 : default_external_stack_protect_fail ();
39367 void
39368 rs6000_final_prescan_insn (rtx_insn *insn, rtx *operand ATTRIBUTE_UNUSED,
39369 int num_operands ATTRIBUTE_UNUSED)
39371 if (rs6000_warn_cell_microcode)
39373 const char *temp;
39374 int insn_code_number = recog_memoized (insn);
39375 location_t location = INSN_LOCATION (insn);
39377 /* Punt on insns we cannot recognize. */
39378 if (insn_code_number < 0)
39379 return;
39381 /* get_insn_template can modify recog_data, so save and restore it. */
39382 struct recog_data_d recog_data_save = recog_data;
39383 for (int i = 0; i < recog_data.n_operands; i++)
39384 recog_data.operand[i] = copy_rtx (recog_data.operand[i]);
39385 temp = get_insn_template (insn_code_number, insn);
39386 recog_data = recog_data_save;
39388 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
39389 warning_at (location, OPT_mwarn_cell_microcode,
39390 "emitting microcode insn %s\t[%s] #%d",
39391 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
39392 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
39393 warning_at (location, OPT_mwarn_cell_microcode,
39394 "emitting conditional microcode insn %s\t[%s] #%d",
39395 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
39399 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
39401 #if TARGET_ELF
39402 static unsigned HOST_WIDE_INT
39403 rs6000_asan_shadow_offset (void)
39405 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
39407 #endif
39409 /* Mask options that we want to support inside of attribute((target)) and
39410 #pragma GCC target operations. Note, we do not include things like
39411 64/32-bit, endianness, hard/soft floating point, etc. that would have
39412 different calling sequences. */
39414 struct rs6000_opt_mask {
39415 const char *name; /* option name */
39416 HOST_WIDE_INT mask; /* mask to set */
39417 bool invert; /* invert sense of mask */
39418 bool valid_target; /* option is a target option */
39421 static struct rs6000_opt_mask const rs6000_opt_masks[] =
39423 { "altivec", OPTION_MASK_ALTIVEC, false, true },
39424 { "cmpb", OPTION_MASK_CMPB, false, true },
39425 { "crypto", OPTION_MASK_CRYPTO, false, true },
39426 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
39427 { "dlmzb", OPTION_MASK_DLMZB, false, true },
39428 { "efficient-unaligned-vsx", OPTION_MASK_EFFICIENT_UNALIGNED_VSX,
39429 false, true },
39430 { "float128", OPTION_MASK_FLOAT128_KEYWORD, false, false },
39431 { "float128-type", OPTION_MASK_FLOAT128_TYPE, false, false },
39432 { "float128-hardware", OPTION_MASK_FLOAT128_HW, false, false },
39433 { "fprnd", OPTION_MASK_FPRND, false, true },
39434 { "hard-dfp", OPTION_MASK_DFP, false, true },
39435 { "htm", OPTION_MASK_HTM, false, true },
39436 { "isel", OPTION_MASK_ISEL, false, true },
39437 { "mfcrf", OPTION_MASK_MFCRF, false, true },
39438 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
39439 { "modulo", OPTION_MASK_MODULO, false, true },
39440 { "mulhw", OPTION_MASK_MULHW, false, true },
39441 { "multiple", OPTION_MASK_MULTIPLE, false, true },
39442 { "popcntb", OPTION_MASK_POPCNTB, false, true },
39443 { "popcntd", OPTION_MASK_POPCNTD, false, true },
39444 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
39445 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
39446 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
39447 { "power9-dform-scalar", OPTION_MASK_P9_DFORM_SCALAR, false, true },
39448 { "power9-dform-vector", OPTION_MASK_P9_DFORM_VECTOR, false, true },
39449 { "power9-fusion", OPTION_MASK_P9_FUSION, false, true },
39450 { "power9-minmax", OPTION_MASK_P9_MINMAX, false, true },
39451 { "power9-misc", OPTION_MASK_P9_MISC, false, true },
39452 { "power9-vector", OPTION_MASK_P9_VECTOR, false, true },
39453 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
39454 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
39455 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
39456 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
39457 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
39458 { "save-toc-indirect", OPTION_MASK_SAVE_TOC_INDIRECT, false, true },
39459 { "string", OPTION_MASK_STRING, false, true },
39460 { "toc-fusion", OPTION_MASK_TOC_FUSION, false, true },
39461 { "update", OPTION_MASK_NO_UPDATE, true , true },
39462 { "upper-regs-di", OPTION_MASK_UPPER_REGS_DI, false, true },
39463 { "upper-regs-df", OPTION_MASK_UPPER_REGS_DF, false, true },
39464 { "upper-regs-sf", OPTION_MASK_UPPER_REGS_SF, false, true },
39465 { "vsx", OPTION_MASK_VSX, false, true },
39466 { "vsx-small-integer", OPTION_MASK_VSX_SMALL_INTEGER, false, true },
39467 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
39468 #ifdef OPTION_MASK_64BIT
39469 #if TARGET_AIX_OS
39470 { "aix64", OPTION_MASK_64BIT, false, false },
39471 { "aix32", OPTION_MASK_64BIT, true, false },
39472 #else
39473 { "64", OPTION_MASK_64BIT, false, false },
39474 { "32", OPTION_MASK_64BIT, true, false },
39475 #endif
39476 #endif
39477 #ifdef OPTION_MASK_EABI
39478 { "eabi", OPTION_MASK_EABI, false, false },
39479 #endif
39480 #ifdef OPTION_MASK_LITTLE_ENDIAN
39481 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
39482 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
39483 #endif
39484 #ifdef OPTION_MASK_RELOCATABLE
39485 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
39486 #endif
39487 #ifdef OPTION_MASK_STRICT_ALIGN
39488 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
39489 #endif
39490 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
39491 { "string", OPTION_MASK_STRING, false, false },
39494 /* Builtin mask mapping for printing the flags. */
39495 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
39497 { "altivec", RS6000_BTM_ALTIVEC, false, false },
39498 { "vsx", RS6000_BTM_VSX, false, false },
39499 { "spe", RS6000_BTM_SPE, false, false },
39500 { "paired", RS6000_BTM_PAIRED, false, false },
39501 { "fre", RS6000_BTM_FRE, false, false },
39502 { "fres", RS6000_BTM_FRES, false, false },
39503 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
39504 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
39505 { "popcntd", RS6000_BTM_POPCNTD, false, false },
39506 { "cell", RS6000_BTM_CELL, false, false },
39507 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
39508 { "power9-vector", RS6000_BTM_P9_VECTOR, false, false },
39509 { "power9-misc", RS6000_BTM_P9_MISC, false, false },
39510 { "crypto", RS6000_BTM_CRYPTO, false, false },
39511 { "htm", RS6000_BTM_HTM, false, false },
39512 { "hard-dfp", RS6000_BTM_DFP, false, false },
39513 { "hard-float", RS6000_BTM_HARD_FLOAT, false, false },
39514 { "long-double-128", RS6000_BTM_LDBL128, false, false },
39515 { "float128", RS6000_BTM_FLOAT128, false, false },
39518 /* Option variables that we want to support inside attribute((target)) and
39519 #pragma GCC target operations. */
39521 struct rs6000_opt_var {
39522 const char *name; /* option name */
39523 size_t global_offset; /* offset of the option in global_options. */
39524 size_t target_offset; /* offset of the option in target options. */
39527 static struct rs6000_opt_var const rs6000_opt_vars[] =
39529 { "friz",
39530 offsetof (struct gcc_options, x_TARGET_FRIZ),
39531 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
39532 { "avoid-indexed-addresses",
39533 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
39534 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
39535 { "paired",
39536 offsetof (struct gcc_options, x_rs6000_paired_float),
39537 offsetof (struct cl_target_option, x_rs6000_paired_float), },
39538 { "longcall",
39539 offsetof (struct gcc_options, x_rs6000_default_long_calls),
39540 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
39541 { "optimize-swaps",
39542 offsetof (struct gcc_options, x_rs6000_optimize_swaps),
39543 offsetof (struct cl_target_option, x_rs6000_optimize_swaps), },
39544 { "allow-movmisalign",
39545 offsetof (struct gcc_options, x_TARGET_ALLOW_MOVMISALIGN),
39546 offsetof (struct cl_target_option, x_TARGET_ALLOW_MOVMISALIGN), },
39547 { "allow-df-permute",
39548 offsetof (struct gcc_options, x_TARGET_ALLOW_DF_PERMUTE),
39549 offsetof (struct cl_target_option, x_TARGET_ALLOW_DF_PERMUTE), },
39550 { "sched-groups",
39551 offsetof (struct gcc_options, x_TARGET_SCHED_GROUPS),
39552 offsetof (struct cl_target_option, x_TARGET_SCHED_GROUPS), },
39553 { "always-hint",
39554 offsetof (struct gcc_options, x_TARGET_ALWAYS_HINT),
39555 offsetof (struct cl_target_option, x_TARGET_ALWAYS_HINT), },
39556 { "align-branch-targets",
39557 offsetof (struct gcc_options, x_TARGET_ALIGN_BRANCH_TARGETS),
39558 offsetof (struct cl_target_option, x_TARGET_ALIGN_BRANCH_TARGETS), },
39559 { "vectorize-builtins",
39560 offsetof (struct gcc_options, x_TARGET_VECTORIZE_BUILTINS),
39561 offsetof (struct cl_target_option, x_TARGET_VECTORIZE_BUILTINS), },
39562 { "tls-markers",
39563 offsetof (struct gcc_options, x_tls_markers),
39564 offsetof (struct cl_target_option, x_tls_markers), },
39565 { "sched-prolog",
39566 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
39567 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
39568 { "sched-epilog",
39569 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
39570 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
39571 { "gen-cell-microcode",
39572 offsetof (struct gcc_options, x_rs6000_gen_cell_microcode),
39573 offsetof (struct cl_target_option, x_rs6000_gen_cell_microcode), },
39574 { "warn-cell-microcode",
39575 offsetof (struct gcc_options, x_rs6000_warn_cell_microcode),
39576 offsetof (struct cl_target_option, x_rs6000_warn_cell_microcode), },
39579 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
39580 parsing. Return true if there were no errors. */
39582 static bool
39583 rs6000_inner_target_options (tree args, bool attr_p)
39585 bool ret = true;
39587 if (args == NULL_TREE)
39590 else if (TREE_CODE (args) == STRING_CST)
39592 char *p = ASTRDUP (TREE_STRING_POINTER (args));
39593 char *q;
39595 while ((q = strtok (p, ",")) != NULL)
39597 bool error_p = false;
39598 bool not_valid_p = false;
39599 const char *cpu_opt = NULL;
39601 p = NULL;
39602 if (strncmp (q, "cpu=", 4) == 0)
39604 int cpu_index = rs6000_cpu_name_lookup (q+4);
39605 if (cpu_index >= 0)
39606 rs6000_cpu_index = cpu_index;
39607 else
39609 error_p = true;
39610 cpu_opt = q+4;
39613 else if (strncmp (q, "tune=", 5) == 0)
39615 int tune_index = rs6000_cpu_name_lookup (q+5);
39616 if (tune_index >= 0)
39617 rs6000_tune_index = tune_index;
39618 else
39620 error_p = true;
39621 cpu_opt = q+5;
39624 else
39626 size_t i;
39627 bool invert = false;
39628 char *r = q;
39630 error_p = true;
39631 if (strncmp (r, "no-", 3) == 0)
39633 invert = true;
39634 r += 3;
39637 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
39638 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
39640 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
39642 if (!rs6000_opt_masks[i].valid_target)
39643 not_valid_p = true;
39644 else
39646 error_p = false;
39647 rs6000_isa_flags_explicit |= mask;
39649 /* VSX needs altivec, so -mvsx automagically sets
39650 altivec and disables -mavoid-indexed-addresses. */
39651 if (!invert)
39653 if (mask == OPTION_MASK_VSX)
39655 mask |= OPTION_MASK_ALTIVEC;
39656 TARGET_AVOID_XFORM = 0;
39660 if (rs6000_opt_masks[i].invert)
39661 invert = !invert;
39663 if (invert)
39664 rs6000_isa_flags &= ~mask;
39665 else
39666 rs6000_isa_flags |= mask;
39668 break;
39671 if (error_p && !not_valid_p)
39673 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
39674 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
39676 size_t j = rs6000_opt_vars[i].global_offset;
39677 *((int *) ((char *)&global_options + j)) = !invert;
39678 error_p = false;
39679 not_valid_p = false;
39680 break;
39685 if (error_p)
39687 const char *eprefix, *esuffix;
39689 ret = false;
39690 if (attr_p)
39692 eprefix = "__attribute__((__target__(";
39693 esuffix = ")))";
39695 else
39697 eprefix = "#pragma GCC target ";
39698 esuffix = "";
39701 if (cpu_opt)
39702 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
39703 q, esuffix);
39704 else if (not_valid_p)
39705 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
39706 else
39707 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
39712 else if (TREE_CODE (args) == TREE_LIST)
39716 tree value = TREE_VALUE (args);
39717 if (value)
39719 bool ret2 = rs6000_inner_target_options (value, attr_p);
39720 if (!ret2)
39721 ret = false;
39723 args = TREE_CHAIN (args);
39725 while (args != NULL_TREE);
39728 else
39730 error ("attribute %<target%> argument not a string");
39731 return false;
39734 return ret;
39737 /* Print out the target options as a list for -mdebug=target. */
39739 static void
39740 rs6000_debug_target_options (tree args, const char *prefix)
39742 if (args == NULL_TREE)
39743 fprintf (stderr, "%s<NULL>", prefix);
39745 else if (TREE_CODE (args) == STRING_CST)
39747 char *p = ASTRDUP (TREE_STRING_POINTER (args));
39748 char *q;
39750 while ((q = strtok (p, ",")) != NULL)
39752 p = NULL;
39753 fprintf (stderr, "%s\"%s\"", prefix, q);
39754 prefix = ", ";
39758 else if (TREE_CODE (args) == TREE_LIST)
39762 tree value = TREE_VALUE (args);
39763 if (value)
39765 rs6000_debug_target_options (value, prefix);
39766 prefix = ", ";
39768 args = TREE_CHAIN (args);
39770 while (args != NULL_TREE);
39773 else
39774 gcc_unreachable ();
39776 return;
39780 /* Hook to validate attribute((target("..."))). */
39782 static bool
39783 rs6000_valid_attribute_p (tree fndecl,
39784 tree ARG_UNUSED (name),
39785 tree args,
39786 int flags)
39788 struct cl_target_option cur_target;
39789 bool ret;
39790 tree old_optimize = build_optimization_node (&global_options);
39791 tree new_target, new_optimize;
39792 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
39794 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
39796 if (TARGET_DEBUG_TARGET)
39798 tree tname = DECL_NAME (fndecl);
39799 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
39800 if (tname)
39801 fprintf (stderr, "function: %.*s\n",
39802 (int) IDENTIFIER_LENGTH (tname),
39803 IDENTIFIER_POINTER (tname));
39804 else
39805 fprintf (stderr, "function: unknown\n");
39807 fprintf (stderr, "args:");
39808 rs6000_debug_target_options (args, " ");
39809 fprintf (stderr, "\n");
39811 if (flags)
39812 fprintf (stderr, "flags: 0x%x\n", flags);
39814 fprintf (stderr, "--------------------\n");
39817 old_optimize = build_optimization_node (&global_options);
39818 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
39820 /* If the function changed the optimization levels as well as setting target
39821 options, start with the optimizations specified. */
39822 if (func_optimize && func_optimize != old_optimize)
39823 cl_optimization_restore (&global_options,
39824 TREE_OPTIMIZATION (func_optimize));
39826 /* The target attributes may also change some optimization flags, so update
39827 the optimization options if necessary. */
39828 cl_target_option_save (&cur_target, &global_options);
39829 rs6000_cpu_index = rs6000_tune_index = -1;
39830 ret = rs6000_inner_target_options (args, true);
39832 /* Set up any additional state. */
39833 if (ret)
39835 ret = rs6000_option_override_internal (false);
39836 new_target = build_target_option_node (&global_options);
39838 else
39839 new_target = NULL;
39841 new_optimize = build_optimization_node (&global_options);
39843 if (!new_target)
39844 ret = false;
39846 else if (fndecl)
39848 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
39850 if (old_optimize != new_optimize)
39851 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
39854 cl_target_option_restore (&global_options, &cur_target);
39856 if (old_optimize != new_optimize)
39857 cl_optimization_restore (&global_options,
39858 TREE_OPTIMIZATION (old_optimize));
39860 return ret;
39864 /* Hook to validate the current #pragma GCC target and set the state, and
39865 update the macros based on what was changed. If ARGS is NULL, then
39866 POP_TARGET is used to reset the options. */
39868 bool
39869 rs6000_pragma_target_parse (tree args, tree pop_target)
39871 tree prev_tree = build_target_option_node (&global_options);
39872 tree cur_tree;
39873 struct cl_target_option *prev_opt, *cur_opt;
39874 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
39875 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
39877 if (TARGET_DEBUG_TARGET)
39879 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
39880 fprintf (stderr, "args:");
39881 rs6000_debug_target_options (args, " ");
39882 fprintf (stderr, "\n");
39884 if (pop_target)
39886 fprintf (stderr, "pop_target:\n");
39887 debug_tree (pop_target);
39889 else
39890 fprintf (stderr, "pop_target: <NULL>\n");
39892 fprintf (stderr, "--------------------\n");
39895 if (! args)
39897 cur_tree = ((pop_target)
39898 ? pop_target
39899 : target_option_default_node);
39900 cl_target_option_restore (&global_options,
39901 TREE_TARGET_OPTION (cur_tree));
39903 else
39905 rs6000_cpu_index = rs6000_tune_index = -1;
39906 if (!rs6000_inner_target_options (args, false)
39907 || !rs6000_option_override_internal (false)
39908 || (cur_tree = build_target_option_node (&global_options))
39909 == NULL_TREE)
39911 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
39912 fprintf (stderr, "invalid pragma\n");
39914 return false;
39918 target_option_current_node = cur_tree;
39920 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
39921 change the macros that are defined. */
39922 if (rs6000_target_modify_macros_ptr)
39924 prev_opt = TREE_TARGET_OPTION (prev_tree);
39925 prev_bumask = prev_opt->x_rs6000_builtin_mask;
39926 prev_flags = prev_opt->x_rs6000_isa_flags;
39928 cur_opt = TREE_TARGET_OPTION (cur_tree);
39929 cur_flags = cur_opt->x_rs6000_isa_flags;
39930 cur_bumask = cur_opt->x_rs6000_builtin_mask;
39932 diff_bumask = (prev_bumask ^ cur_bumask);
39933 diff_flags = (prev_flags ^ cur_flags);
39935 if ((diff_flags != 0) || (diff_bumask != 0))
39937 /* Delete old macros. */
39938 rs6000_target_modify_macros_ptr (false,
39939 prev_flags & diff_flags,
39940 prev_bumask & diff_bumask);
39942 /* Define new macros. */
39943 rs6000_target_modify_macros_ptr (true,
39944 cur_flags & diff_flags,
39945 cur_bumask & diff_bumask);
39949 return true;
39953 /* Remember the last target of rs6000_set_current_function. */
39954 static GTY(()) tree rs6000_previous_fndecl;
39956 /* Establish appropriate back-end context for processing the function
39957 FNDECL. The argument might be NULL to indicate processing at top
39958 level, outside of any function scope. */
39959 static void
39960 rs6000_set_current_function (tree fndecl)
39962 tree old_tree = (rs6000_previous_fndecl
39963 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
39964 : NULL_TREE);
39966 tree new_tree = (fndecl
39967 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
39968 : NULL_TREE);
39970 if (TARGET_DEBUG_TARGET)
39972 bool print_final = false;
39973 fprintf (stderr, "\n==================== rs6000_set_current_function");
39975 if (fndecl)
39976 fprintf (stderr, ", fndecl %s (%p)",
39977 (DECL_NAME (fndecl)
39978 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
39979 : "<unknown>"), (void *)fndecl);
39981 if (rs6000_previous_fndecl)
39982 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
39984 fprintf (stderr, "\n");
39985 if (new_tree)
39987 fprintf (stderr, "\nnew fndecl target specific options:\n");
39988 debug_tree (new_tree);
39989 print_final = true;
39992 if (old_tree)
39994 fprintf (stderr, "\nold fndecl target specific options:\n");
39995 debug_tree (old_tree);
39996 print_final = true;
39999 if (print_final)
40000 fprintf (stderr, "--------------------\n");
40003 /* Only change the context if the function changes. This hook is called
40004 several times in the course of compiling a function, and we don't want to
40005 slow things down too much or call target_reinit when it isn't safe. */
40006 if (fndecl && fndecl != rs6000_previous_fndecl)
40008 rs6000_previous_fndecl = fndecl;
40009 if (old_tree == new_tree)
40012 else if (new_tree && new_tree != target_option_default_node)
40014 cl_target_option_restore (&global_options,
40015 TREE_TARGET_OPTION (new_tree));
40016 if (TREE_TARGET_GLOBALS (new_tree))
40017 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
40018 else
40019 TREE_TARGET_GLOBALS (new_tree)
40020 = save_target_globals_default_opts ();
40023 else if (old_tree && old_tree != target_option_default_node)
40025 new_tree = target_option_current_node;
40026 cl_target_option_restore (&global_options,
40027 TREE_TARGET_OPTION (new_tree));
40028 if (TREE_TARGET_GLOBALS (new_tree))
40029 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
40030 else if (new_tree == target_option_default_node)
40031 restore_target_globals (&default_target_globals);
40032 else
40033 TREE_TARGET_GLOBALS (new_tree)
40034 = save_target_globals_default_opts ();
40040 /* Save the current options */
40042 static void
40043 rs6000_function_specific_save (struct cl_target_option *ptr,
40044 struct gcc_options *opts)
40046 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
40047 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
40050 /* Restore the current options */
40052 static void
40053 rs6000_function_specific_restore (struct gcc_options *opts,
40054 struct cl_target_option *ptr)
40057 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
40058 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
40059 (void) rs6000_option_override_internal (false);
40062 /* Print the current options */
40064 static void
40065 rs6000_function_specific_print (FILE *file, int indent,
40066 struct cl_target_option *ptr)
40068 rs6000_print_isa_options (file, indent, "Isa options set",
40069 ptr->x_rs6000_isa_flags);
40071 rs6000_print_isa_options (file, indent, "Isa options explicit",
40072 ptr->x_rs6000_isa_flags_explicit);
40075 /* Helper function to print the current isa or misc options on a line. */
40077 static void
40078 rs6000_print_options_internal (FILE *file,
40079 int indent,
40080 const char *string,
40081 HOST_WIDE_INT flags,
40082 const char *prefix,
40083 const struct rs6000_opt_mask *opts,
40084 size_t num_elements)
40086 size_t i;
40087 size_t start_column = 0;
40088 size_t cur_column;
40089 size_t max_column = 120;
40090 size_t prefix_len = strlen (prefix);
40091 size_t comma_len = 0;
40092 const char *comma = "";
40094 if (indent)
40095 start_column += fprintf (file, "%*s", indent, "");
40097 if (!flags)
40099 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
40100 return;
40103 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
40105 /* Print the various mask options. */
40106 cur_column = start_column;
40107 for (i = 0; i < num_elements; i++)
40109 bool invert = opts[i].invert;
40110 const char *name = opts[i].name;
40111 const char *no_str = "";
40112 HOST_WIDE_INT mask = opts[i].mask;
40113 size_t len = comma_len + prefix_len + strlen (name);
40115 if (!invert)
40117 if ((flags & mask) == 0)
40119 no_str = "no-";
40120 len += sizeof ("no-") - 1;
40123 flags &= ~mask;
40126 else
40128 if ((flags & mask) != 0)
40130 no_str = "no-";
40131 len += sizeof ("no-") - 1;
40134 flags |= mask;
40137 cur_column += len;
40138 if (cur_column > max_column)
40140 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
40141 cur_column = start_column + len;
40142 comma = "";
40145 fprintf (file, "%s%s%s%s", comma, prefix, no_str, name);
40146 comma = ", ";
40147 comma_len = sizeof (", ") - 1;
40150 fputs ("\n", file);
40153 /* Helper function to print the current isa options on a line. */
40155 static void
40156 rs6000_print_isa_options (FILE *file, int indent, const char *string,
40157 HOST_WIDE_INT flags)
40159 rs6000_print_options_internal (file, indent, string, flags, "-m",
40160 &rs6000_opt_masks[0],
40161 ARRAY_SIZE (rs6000_opt_masks));
40164 static void
40165 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
40166 HOST_WIDE_INT flags)
40168 rs6000_print_options_internal (file, indent, string, flags, "",
40169 &rs6000_builtin_mask_names[0],
40170 ARRAY_SIZE (rs6000_builtin_mask_names));
40173 /* If the user used -mno-vsx, we need turn off all of the implicit ISA 2.06,
40174 2.07, and 3.0 options that relate to the vector unit (-mdirect-move,
40175 -mvsx-timode, -mupper-regs-df).
40177 If the user used -mno-power8-vector, we need to turn off all of the implicit
40178 ISA 2.07 and 3.0 options that relate to the vector unit.
40180 If the user used -mno-power9-vector, we need to turn off all of the implicit
40181 ISA 3.0 options that relate to the vector unit.
40183 This function does not handle explicit options such as the user specifying
40184 -mdirect-move. These are handled in rs6000_option_override_internal, and
40185 the appropriate error is given if needed.
40187 We return a mask of all of the implicit options that should not be enabled
40188 by default. */
40190 static HOST_WIDE_INT
40191 rs6000_disable_incompatible_switches (void)
40193 HOST_WIDE_INT ignore_masks = rs6000_isa_flags_explicit;
40194 size_t i, j;
40196 static const struct {
40197 const HOST_WIDE_INT no_flag; /* flag explicitly turned off. */
40198 const HOST_WIDE_INT dep_flags; /* flags that depend on this option. */
40199 const char *const name; /* name of the switch. */
40200 } flags[] = {
40201 { OPTION_MASK_P9_VECTOR, OTHER_P9_VECTOR_MASKS, "power9-vector" },
40202 { OPTION_MASK_P8_VECTOR, OTHER_P8_VECTOR_MASKS, "power8-vector" },
40203 { OPTION_MASK_VSX, OTHER_VSX_VECTOR_MASKS, "vsx" },
40206 for (i = 0; i < ARRAY_SIZE (flags); i++)
40208 HOST_WIDE_INT no_flag = flags[i].no_flag;
40210 if ((rs6000_isa_flags & no_flag) == 0
40211 && (rs6000_isa_flags_explicit & no_flag) != 0)
40213 HOST_WIDE_INT dep_flags = flags[i].dep_flags;
40214 HOST_WIDE_INT set_flags = (rs6000_isa_flags_explicit
40215 & rs6000_isa_flags
40216 & dep_flags);
40218 if (set_flags)
40220 for (j = 0; j < ARRAY_SIZE (rs6000_opt_masks); j++)
40221 if ((set_flags & rs6000_opt_masks[j].mask) != 0)
40223 set_flags &= ~rs6000_opt_masks[j].mask;
40224 error ("-mno-%s turns off -m%s",
40225 flags[i].name,
40226 rs6000_opt_masks[j].name);
40229 gcc_assert (!set_flags);
40232 rs6000_isa_flags &= ~dep_flags;
40233 ignore_masks |= no_flag | dep_flags;
40237 if (!TARGET_P9_VECTOR
40238 && (rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) != 0
40239 && TARGET_P9_DFORM_BOTH > 0)
40241 error ("-mno-power9-vector turns off -mpower9-dform");
40242 TARGET_P9_DFORM_BOTH = 0;
40245 return ignore_masks;
40249 /* Hook to determine if one function can safely inline another. */
40251 static bool
40252 rs6000_can_inline_p (tree caller, tree callee)
40254 bool ret = false;
40255 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
40256 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
40258 /* If callee has no option attributes, then it is ok to inline. */
40259 if (!callee_tree)
40260 ret = true;
40262 /* If caller has no option attributes, but callee does then it is not ok to
40263 inline. */
40264 else if (!caller_tree)
40265 ret = false;
40267 else
40269 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
40270 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
40272 /* Callee's options should a subset of the caller's, i.e. a vsx function
40273 can inline an altivec function but a non-vsx function can't inline a
40274 vsx function. */
40275 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
40276 == callee_opts->x_rs6000_isa_flags)
40277 ret = true;
40280 if (TARGET_DEBUG_TARGET)
40281 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
40282 (DECL_NAME (caller)
40283 ? IDENTIFIER_POINTER (DECL_NAME (caller))
40284 : "<unknown>"),
40285 (DECL_NAME (callee)
40286 ? IDENTIFIER_POINTER (DECL_NAME (callee))
40287 : "<unknown>"),
40288 (ret ? "can" : "cannot"));
40290 return ret;
40293 /* Allocate a stack temp and fixup the address so it meets the particular
40294 memory requirements (either offetable or REG+REG addressing). */
40297 rs6000_allocate_stack_temp (machine_mode mode,
40298 bool offsettable_p,
40299 bool reg_reg_p)
40301 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
40302 rtx addr = XEXP (stack, 0);
40303 int strict_p = (reload_in_progress || reload_completed);
40305 if (!legitimate_indirect_address_p (addr, strict_p))
40307 if (offsettable_p
40308 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
40309 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
40311 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
40312 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
40315 return stack;
40318 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
40319 to such a form to deal with memory reference instructions like STFIWX that
40320 only take reg+reg addressing. */
40323 rs6000_address_for_fpconvert (rtx x)
40325 int strict_p = (reload_in_progress || reload_completed);
40326 rtx addr;
40328 gcc_assert (MEM_P (x));
40329 addr = XEXP (x, 0);
40330 if (! legitimate_indirect_address_p (addr, strict_p)
40331 && ! legitimate_indexed_address_p (addr, strict_p))
40333 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
40335 rtx reg = XEXP (addr, 0);
40336 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
40337 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
40338 gcc_assert (REG_P (reg));
40339 emit_insn (gen_add3_insn (reg, reg, size_rtx));
40340 addr = reg;
40342 else if (GET_CODE (addr) == PRE_MODIFY)
40344 rtx reg = XEXP (addr, 0);
40345 rtx expr = XEXP (addr, 1);
40346 gcc_assert (REG_P (reg));
40347 gcc_assert (GET_CODE (expr) == PLUS);
40348 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
40349 addr = reg;
40352 x = replace_equiv_address (x, copy_addr_to_reg (addr));
40355 return x;
40358 /* Given a memory reference, if it is not in the form for altivec memory
40359 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
40360 convert to the altivec format. */
40363 rs6000_address_for_altivec (rtx x)
40365 gcc_assert (MEM_P (x));
40366 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
40368 rtx addr = XEXP (x, 0);
40369 int strict_p = (reload_in_progress || reload_completed);
40371 if (!legitimate_indexed_address_p (addr, strict_p)
40372 && !legitimate_indirect_address_p (addr, strict_p))
40373 addr = copy_to_mode_reg (Pmode, addr);
40375 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
40376 x = change_address (x, GET_MODE (x), addr);
40379 return x;
40382 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
40384 On the RS/6000, all integer constants are acceptable, most won't be valid
40385 for particular insns, though. Only easy FP constants are acceptable. */
40387 static bool
40388 rs6000_legitimate_constant_p (machine_mode mode, rtx x)
40390 if (TARGET_ELF && tls_referenced_p (x))
40391 return false;
40393 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
40394 || GET_MODE (x) == VOIDmode
40395 || (TARGET_POWERPC64 && mode == DImode)
40396 || easy_fp_constant (x, mode)
40397 || easy_vector_constant (x, mode));
40401 /* Return TRUE iff the sequence ending in LAST sets the static chain. */
40403 static bool
40404 chain_already_loaded (rtx_insn *last)
40406 for (; last != NULL; last = PREV_INSN (last))
40408 if (NONJUMP_INSN_P (last))
40410 rtx patt = PATTERN (last);
40412 if (GET_CODE (patt) == SET)
40414 rtx lhs = XEXP (patt, 0);
40416 if (REG_P (lhs) && REGNO (lhs) == STATIC_CHAIN_REGNUM)
40417 return true;
40421 return false;
40424 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
40426 void
40427 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
40429 const bool direct_call_p
40430 = GET_CODE (func_desc) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (func_desc);
40431 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
40432 rtx toc_load = NULL_RTX;
40433 rtx toc_restore = NULL_RTX;
40434 rtx func_addr;
40435 rtx abi_reg = NULL_RTX;
40436 rtx call[4];
40437 int n_call;
40438 rtx insn;
40440 /* Handle longcall attributes. */
40441 if (INTVAL (cookie) & CALL_LONG)
40442 func_desc = rs6000_longcall_ref (func_desc);
40444 /* Handle indirect calls. */
40445 if (GET_CODE (func_desc) != SYMBOL_REF
40446 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
40448 /* Save the TOC into its reserved slot before the call,
40449 and prepare to restore it after the call. */
40450 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
40451 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
40452 rtx stack_toc_mem = gen_frame_mem (Pmode,
40453 gen_rtx_PLUS (Pmode, stack_ptr,
40454 stack_toc_offset));
40455 rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode,
40456 gen_rtvec (1, stack_toc_offset),
40457 UNSPEC_TOCSLOT);
40458 toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec);
40460 /* Can we optimize saving the TOC in the prologue or
40461 do we need to do it at every call? */
40462 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
40463 cfun->machine->save_toc_in_prologue = true;
40464 else
40466 MEM_VOLATILE_P (stack_toc_mem) = 1;
40467 emit_move_insn (stack_toc_mem, toc_reg);
40470 if (DEFAULT_ABI == ABI_ELFv2)
40472 /* A function pointer in the ELFv2 ABI is just a plain address, but
40473 the ABI requires it to be loaded into r12 before the call. */
40474 func_addr = gen_rtx_REG (Pmode, 12);
40475 emit_move_insn (func_addr, func_desc);
40476 abi_reg = func_addr;
40478 else
40480 /* A function pointer under AIX is a pointer to a data area whose
40481 first word contains the actual address of the function, whose
40482 second word contains a pointer to its TOC, and whose third word
40483 contains a value to place in the static chain register (r11).
40484 Note that if we load the static chain, our "trampoline" need
40485 not have any executable code. */
40487 /* Load up address of the actual function. */
40488 func_desc = force_reg (Pmode, func_desc);
40489 func_addr = gen_reg_rtx (Pmode);
40490 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
40492 /* Prepare to load the TOC of the called function. Note that the
40493 TOC load must happen immediately before the actual call so
40494 that unwinding the TOC registers works correctly. See the
40495 comment in frob_update_context. */
40496 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
40497 rtx func_toc_mem = gen_rtx_MEM (Pmode,
40498 gen_rtx_PLUS (Pmode, func_desc,
40499 func_toc_offset));
40500 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
40502 /* If we have a static chain, load it up. But, if the call was
40503 originally direct, the 3rd word has not been written since no
40504 trampoline has been built, so we ought not to load it, lest we
40505 override a static chain value. */
40506 if (!direct_call_p
40507 && TARGET_POINTERS_TO_NESTED_FUNCTIONS
40508 && !chain_already_loaded (get_current_sequence ()->next->last))
40510 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
40511 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
40512 rtx func_sc_mem = gen_rtx_MEM (Pmode,
40513 gen_rtx_PLUS (Pmode, func_desc,
40514 func_sc_offset));
40515 emit_move_insn (sc_reg, func_sc_mem);
40516 abi_reg = sc_reg;
40520 else
40522 /* Direct calls use the TOC: for local calls, the callee will
40523 assume the TOC register is set; for non-local calls, the
40524 PLT stub needs the TOC register. */
40525 abi_reg = toc_reg;
40526 func_addr = func_desc;
40529 /* Create the call. */
40530 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
40531 if (value != NULL_RTX)
40532 call[0] = gen_rtx_SET (value, call[0]);
40533 n_call = 1;
40535 if (toc_load)
40536 call[n_call++] = toc_load;
40537 if (toc_restore)
40538 call[n_call++] = toc_restore;
40540 call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
40542 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
40543 insn = emit_call_insn (insn);
40545 /* Mention all registers defined by the ABI to hold information
40546 as uses in CALL_INSN_FUNCTION_USAGE. */
40547 if (abi_reg)
40548 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
40551 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
40553 void
40554 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
40556 rtx call[2];
40557 rtx insn;
40559 gcc_assert (INTVAL (cookie) == 0);
40561 /* Create the call. */
40562 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
40563 if (value != NULL_RTX)
40564 call[0] = gen_rtx_SET (value, call[0]);
40566 call[1] = simple_return_rtx;
40568 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
40569 insn = emit_call_insn (insn);
40571 /* Note use of the TOC register. */
40572 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
40575 /* Return whether we need to always update the saved TOC pointer when we update
40576 the stack pointer. */
40578 static bool
40579 rs6000_save_toc_in_prologue_p (void)
40581 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
40584 #ifdef HAVE_GAS_HIDDEN
40585 # define USE_HIDDEN_LINKONCE 1
40586 #else
40587 # define USE_HIDDEN_LINKONCE 0
40588 #endif
40590 /* Fills in the label name that should be used for a 476 link stack thunk. */
40592 void
40593 get_ppc476_thunk_name (char name[32])
40595 gcc_assert (TARGET_LINK_STACK);
40597 if (USE_HIDDEN_LINKONCE)
40598 sprintf (name, "__ppc476.get_thunk");
40599 else
40600 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
40603 /* This function emits the simple thunk routine that is used to preserve
40604 the link stack on the 476 cpu. */
40606 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
40607 static void
40608 rs6000_code_end (void)
40610 char name[32];
40611 tree decl;
40613 if (!TARGET_LINK_STACK)
40614 return;
40616 get_ppc476_thunk_name (name);
40618 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
40619 build_function_type_list (void_type_node, NULL_TREE));
40620 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
40621 NULL_TREE, void_type_node);
40622 TREE_PUBLIC (decl) = 1;
40623 TREE_STATIC (decl) = 1;
40625 #if RS6000_WEAK
40626 if (USE_HIDDEN_LINKONCE && !TARGET_XCOFF)
40628 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
40629 targetm.asm_out.unique_section (decl, 0);
40630 switch_to_section (get_named_section (decl, NULL, 0));
40631 DECL_WEAK (decl) = 1;
40632 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
40633 targetm.asm_out.globalize_label (asm_out_file, name);
40634 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
40635 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
40637 else
40638 #endif
40640 switch_to_section (text_section);
40641 ASM_OUTPUT_LABEL (asm_out_file, name);
40644 DECL_INITIAL (decl) = make_node (BLOCK);
40645 current_function_decl = decl;
40646 allocate_struct_function (decl, false);
40647 init_function_start (decl);
40648 first_function_block_is_cold = false;
40649 /* Make sure unwind info is emitted for the thunk if needed. */
40650 final_start_function (emit_barrier (), asm_out_file, 1);
40652 fputs ("\tblr\n", asm_out_file);
40654 final_end_function ();
40655 init_insn_lengths ();
40656 free_after_compilation (cfun);
40657 set_cfun (NULL);
40658 current_function_decl = NULL;
40661 /* Add r30 to hard reg set if the prologue sets it up and it is not
40662 pic_offset_table_rtx. */
40664 static void
40665 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
40667 if (!TARGET_SINGLE_PIC_BASE
40668 && TARGET_TOC
40669 && TARGET_MINIMAL_TOC
40670 && !constant_pool_empty_p ())
40671 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
40672 if (cfun->machine->split_stack_argp_used)
40673 add_to_hard_reg_set (&set->set, Pmode, 12);
40677 /* Helper function for rs6000_split_logical to emit a logical instruction after
40678 spliting the operation to single GPR registers.
40680 DEST is the destination register.
40681 OP1 and OP2 are the input source registers.
40682 CODE is the base operation (AND, IOR, XOR, NOT).
40683 MODE is the machine mode.
40684 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
40685 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
40686 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
40688 static void
40689 rs6000_split_logical_inner (rtx dest,
40690 rtx op1,
40691 rtx op2,
40692 enum rtx_code code,
40693 machine_mode mode,
40694 bool complement_final_p,
40695 bool complement_op1_p,
40696 bool complement_op2_p)
40698 rtx bool_rtx;
40700 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
40701 if (op2 && GET_CODE (op2) == CONST_INT
40702 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
40703 && !complement_final_p && !complement_op1_p && !complement_op2_p)
40705 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
40706 HOST_WIDE_INT value = INTVAL (op2) & mask;
40708 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
40709 if (code == AND)
40711 if (value == 0)
40713 emit_insn (gen_rtx_SET (dest, const0_rtx));
40714 return;
40717 else if (value == mask)
40719 if (!rtx_equal_p (dest, op1))
40720 emit_insn (gen_rtx_SET (dest, op1));
40721 return;
40725 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
40726 into separate ORI/ORIS or XORI/XORIS instrucitons. */
40727 else if (code == IOR || code == XOR)
40729 if (value == 0)
40731 if (!rtx_equal_p (dest, op1))
40732 emit_insn (gen_rtx_SET (dest, op1));
40733 return;
40738 if (code == AND && mode == SImode
40739 && !complement_final_p && !complement_op1_p && !complement_op2_p)
40741 emit_insn (gen_andsi3 (dest, op1, op2));
40742 return;
40745 if (complement_op1_p)
40746 op1 = gen_rtx_NOT (mode, op1);
40748 if (complement_op2_p)
40749 op2 = gen_rtx_NOT (mode, op2);
40751 /* For canonical RTL, if only one arm is inverted it is the first. */
40752 if (!complement_op1_p && complement_op2_p)
40753 std::swap (op1, op2);
40755 bool_rtx = ((code == NOT)
40756 ? gen_rtx_NOT (mode, op1)
40757 : gen_rtx_fmt_ee (code, mode, op1, op2));
40759 if (complement_final_p)
40760 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
40762 emit_insn (gen_rtx_SET (dest, bool_rtx));
40765 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
40766 operations are split immediately during RTL generation to allow for more
40767 optimizations of the AND/IOR/XOR.
40769 OPERANDS is an array containing the destination and two input operands.
40770 CODE is the base operation (AND, IOR, XOR, NOT).
40771 MODE is the machine mode.
40772 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
40773 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
40774 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
40775 CLOBBER_REG is either NULL or a scratch register of type CC to allow
40776 formation of the AND instructions. */
40778 static void
40779 rs6000_split_logical_di (rtx operands[3],
40780 enum rtx_code code,
40781 bool complement_final_p,
40782 bool complement_op1_p,
40783 bool complement_op2_p)
40785 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
40786 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
40787 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
40788 enum hi_lo { hi = 0, lo = 1 };
40789 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
40790 size_t i;
40792 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
40793 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
40794 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
40795 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
40797 if (code == NOT)
40798 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
40799 else
40801 if (GET_CODE (operands[2]) != CONST_INT)
40803 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
40804 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
40806 else
40808 HOST_WIDE_INT value = INTVAL (operands[2]);
40809 HOST_WIDE_INT value_hi_lo[2];
40811 gcc_assert (!complement_final_p);
40812 gcc_assert (!complement_op1_p);
40813 gcc_assert (!complement_op2_p);
40815 value_hi_lo[hi] = value >> 32;
40816 value_hi_lo[lo] = value & lower_32bits;
40818 for (i = 0; i < 2; i++)
40820 HOST_WIDE_INT sub_value = value_hi_lo[i];
40822 if (sub_value & sign_bit)
40823 sub_value |= upper_32bits;
40825 op2_hi_lo[i] = GEN_INT (sub_value);
40827 /* If this is an AND instruction, check to see if we need to load
40828 the value in a register. */
40829 if (code == AND && sub_value != -1 && sub_value != 0
40830 && !and_operand (op2_hi_lo[i], SImode))
40831 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
40836 for (i = 0; i < 2; i++)
40838 /* Split large IOR/XOR operations. */
40839 if ((code == IOR || code == XOR)
40840 && GET_CODE (op2_hi_lo[i]) == CONST_INT
40841 && !complement_final_p
40842 && !complement_op1_p
40843 && !complement_op2_p
40844 && !logical_const_operand (op2_hi_lo[i], SImode))
40846 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
40847 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
40848 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
40849 rtx tmp = gen_reg_rtx (SImode);
40851 /* Make sure the constant is sign extended. */
40852 if ((hi_16bits & sign_bit) != 0)
40853 hi_16bits |= upper_32bits;
40855 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
40856 code, SImode, false, false, false);
40858 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
40859 code, SImode, false, false, false);
40861 else
40862 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
40863 code, SImode, complement_final_p,
40864 complement_op1_p, complement_op2_p);
40867 return;
40870 /* Split the insns that make up boolean operations operating on multiple GPR
40871 registers. The boolean MD patterns ensure that the inputs either are
40872 exactly the same as the output registers, or there is no overlap.
40874 OPERANDS is an array containing the destination and two input operands.
40875 CODE is the base operation (AND, IOR, XOR, NOT).
40876 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
40877 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
40878 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
40880 void
40881 rs6000_split_logical (rtx operands[3],
40882 enum rtx_code code,
40883 bool complement_final_p,
40884 bool complement_op1_p,
40885 bool complement_op2_p)
40887 machine_mode mode = GET_MODE (operands[0]);
40888 machine_mode sub_mode;
40889 rtx op0, op1, op2;
40890 int sub_size, regno0, regno1, nregs, i;
40892 /* If this is DImode, use the specialized version that can run before
40893 register allocation. */
40894 if (mode == DImode && !TARGET_POWERPC64)
40896 rs6000_split_logical_di (operands, code, complement_final_p,
40897 complement_op1_p, complement_op2_p);
40898 return;
40901 op0 = operands[0];
40902 op1 = operands[1];
40903 op2 = (code == NOT) ? NULL_RTX : operands[2];
40904 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
40905 sub_size = GET_MODE_SIZE (sub_mode);
40906 regno0 = REGNO (op0);
40907 regno1 = REGNO (op1);
40909 gcc_assert (reload_completed);
40910 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
40911 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
40913 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
40914 gcc_assert (nregs > 1);
40916 if (op2 && REG_P (op2))
40917 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
40919 for (i = 0; i < nregs; i++)
40921 int offset = i * sub_size;
40922 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
40923 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
40924 rtx sub_op2 = ((code == NOT)
40925 ? NULL_RTX
40926 : simplify_subreg (sub_mode, op2, mode, offset));
40928 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
40929 complement_final_p, complement_op1_p,
40930 complement_op2_p);
40933 return;
40937 /* Return true if the peephole2 can combine a load involving a combination of
40938 an addis instruction and a load with an offset that can be fused together on
40939 a power8. */
40941 bool
40942 fusion_gpr_load_p (rtx addis_reg, /* register set via addis. */
40943 rtx addis_value, /* addis value. */
40944 rtx target, /* target register that is loaded. */
40945 rtx mem) /* bottom part of the memory addr. */
40947 rtx addr;
40948 rtx base_reg;
40950 /* Validate arguments. */
40951 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
40952 return false;
40954 if (!base_reg_operand (target, GET_MODE (target)))
40955 return false;
40957 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
40958 return false;
40960 /* Allow sign/zero extension. */
40961 if (GET_CODE (mem) == ZERO_EXTEND
40962 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
40963 mem = XEXP (mem, 0);
40965 if (!MEM_P (mem))
40966 return false;
40968 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
40969 return false;
40971 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
40972 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
40973 return false;
40975 /* Validate that the register used to load the high value is either the
40976 register being loaded, or we can safely replace its use.
40978 This function is only called from the peephole2 pass and we assume that
40979 there are 2 instructions in the peephole (addis and load), so we want to
40980 check if the target register was not used in the memory address and the
40981 register to hold the addis result is dead after the peephole. */
40982 if (REGNO (addis_reg) != REGNO (target))
40984 if (reg_mentioned_p (target, mem))
40985 return false;
40987 if (!peep2_reg_dead_p (2, addis_reg))
40988 return false;
40990 /* If the target register being loaded is the stack pointer, we must
40991 avoid loading any other value into it, even temporarily. */
40992 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
40993 return false;
40996 base_reg = XEXP (addr, 0);
40997 return REGNO (addis_reg) == REGNO (base_reg);
41000 /* During the peephole2 pass, adjust and expand the insns for a load fusion
41001 sequence. We adjust the addis register to use the target register. If the
41002 load sign extends, we adjust the code to do the zero extending load, and an
41003 explicit sign extension later since the fusion only covers zero extending
41004 loads.
41006 The operands are:
41007 operands[0] register set with addis (to be replaced with target)
41008 operands[1] value set via addis
41009 operands[2] target register being loaded
41010 operands[3] D-form memory reference using operands[0]. */
41012 void
41013 expand_fusion_gpr_load (rtx *operands)
41015 rtx addis_value = operands[1];
41016 rtx target = operands[2];
41017 rtx orig_mem = operands[3];
41018 rtx new_addr, new_mem, orig_addr, offset;
41019 enum rtx_code plus_or_lo_sum;
41020 machine_mode target_mode = GET_MODE (target);
41021 machine_mode extend_mode = target_mode;
41022 machine_mode ptr_mode = Pmode;
41023 enum rtx_code extend = UNKNOWN;
41025 if (GET_CODE (orig_mem) == ZERO_EXTEND
41026 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
41028 extend = GET_CODE (orig_mem);
41029 orig_mem = XEXP (orig_mem, 0);
41030 target_mode = GET_MODE (orig_mem);
41033 gcc_assert (MEM_P (orig_mem));
41035 orig_addr = XEXP (orig_mem, 0);
41036 plus_or_lo_sum = GET_CODE (orig_addr);
41037 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
41039 offset = XEXP (orig_addr, 1);
41040 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
41041 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
41043 if (extend != UNKNOWN)
41044 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
41046 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
41047 UNSPEC_FUSION_GPR);
41048 emit_insn (gen_rtx_SET (target, new_mem));
41050 if (extend == SIGN_EXTEND)
41052 int sub_off = ((BYTES_BIG_ENDIAN)
41053 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
41054 : 0);
41055 rtx sign_reg
41056 = simplify_subreg (target_mode, target, extend_mode, sub_off);
41058 emit_insn (gen_rtx_SET (target,
41059 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
41062 return;
41065 /* Emit the addis instruction that will be part of a fused instruction
41066 sequence. */
41068 void
41069 emit_fusion_addis (rtx target, rtx addis_value, const char *comment,
41070 const char *mode_name)
41072 rtx fuse_ops[10];
41073 char insn_template[80];
41074 const char *addis_str = NULL;
41075 const char *comment_str = ASM_COMMENT_START;
41077 if (*comment_str == ' ')
41078 comment_str++;
41080 /* Emit the addis instruction. */
41081 fuse_ops[0] = target;
41082 if (satisfies_constraint_L (addis_value))
41084 fuse_ops[1] = addis_value;
41085 addis_str = "lis %0,%v1";
41088 else if (GET_CODE (addis_value) == PLUS)
41090 rtx op0 = XEXP (addis_value, 0);
41091 rtx op1 = XEXP (addis_value, 1);
41093 if (REG_P (op0) && CONST_INT_P (op1)
41094 && satisfies_constraint_L (op1))
41096 fuse_ops[1] = op0;
41097 fuse_ops[2] = op1;
41098 addis_str = "addis %0,%1,%v2";
41102 else if (GET_CODE (addis_value) == HIGH)
41104 rtx value = XEXP (addis_value, 0);
41105 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
41107 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
41108 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
41109 if (TARGET_ELF)
41110 addis_str = "addis %0,%2,%1@toc@ha";
41112 else if (TARGET_XCOFF)
41113 addis_str = "addis %0,%1@u(%2)";
41115 else
41116 gcc_unreachable ();
41119 else if (GET_CODE (value) == PLUS)
41121 rtx op0 = XEXP (value, 0);
41122 rtx op1 = XEXP (value, 1);
41124 if (GET_CODE (op0) == UNSPEC
41125 && XINT (op0, 1) == UNSPEC_TOCREL
41126 && CONST_INT_P (op1))
41128 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
41129 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
41130 fuse_ops[3] = op1;
41131 if (TARGET_ELF)
41132 addis_str = "addis %0,%2,%1+%3@toc@ha";
41134 else if (TARGET_XCOFF)
41135 addis_str = "addis %0,%1+%3@u(%2)";
41137 else
41138 gcc_unreachable ();
41142 else if (satisfies_constraint_L (value))
41144 fuse_ops[1] = value;
41145 addis_str = "lis %0,%v1";
41148 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
41150 fuse_ops[1] = value;
41151 addis_str = "lis %0,%1@ha";
41155 if (!addis_str)
41156 fatal_insn ("Could not generate addis value for fusion", addis_value);
41158 sprintf (insn_template, "%s\t\t%s %s, type %s", addis_str, comment_str,
41159 comment, mode_name);
41160 output_asm_insn (insn_template, fuse_ops);
41163 /* Emit a D-form load or store instruction that is the second instruction
41164 of a fusion sequence. */
41166 void
41167 emit_fusion_load_store (rtx load_store_reg, rtx addis_reg, rtx offset,
41168 const char *insn_str)
41170 rtx fuse_ops[10];
41171 char insn_template[80];
41173 fuse_ops[0] = load_store_reg;
41174 fuse_ops[1] = addis_reg;
41176 if (CONST_INT_P (offset) && satisfies_constraint_I (offset))
41178 sprintf (insn_template, "%s %%0,%%2(%%1)", insn_str);
41179 fuse_ops[2] = offset;
41180 output_asm_insn (insn_template, fuse_ops);
41183 else if (GET_CODE (offset) == UNSPEC
41184 && XINT (offset, 1) == UNSPEC_TOCREL)
41186 if (TARGET_ELF)
41187 sprintf (insn_template, "%s %%0,%%2@toc@l(%%1)", insn_str);
41189 else if (TARGET_XCOFF)
41190 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
41192 else
41193 gcc_unreachable ();
41195 fuse_ops[2] = XVECEXP (offset, 0, 0);
41196 output_asm_insn (insn_template, fuse_ops);
41199 else if (GET_CODE (offset) == PLUS
41200 && GET_CODE (XEXP (offset, 0)) == UNSPEC
41201 && XINT (XEXP (offset, 0), 1) == UNSPEC_TOCREL
41202 && CONST_INT_P (XEXP (offset, 1)))
41204 rtx tocrel_unspec = XEXP (offset, 0);
41205 if (TARGET_ELF)
41206 sprintf (insn_template, "%s %%0,%%2+%%3@toc@l(%%1)", insn_str);
41208 else if (TARGET_XCOFF)
41209 sprintf (insn_template, "%s %%0,%%2+%%3@l(%%1)", insn_str);
41211 else
41212 gcc_unreachable ();
41214 fuse_ops[2] = XVECEXP (tocrel_unspec, 0, 0);
41215 fuse_ops[3] = XEXP (offset, 1);
41216 output_asm_insn (insn_template, fuse_ops);
41219 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (offset))
41221 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
41223 fuse_ops[2] = offset;
41224 output_asm_insn (insn_template, fuse_ops);
41227 else
41228 fatal_insn ("Unable to generate load/store offset for fusion", offset);
41230 return;
41233 /* Wrap a TOC address that can be fused to indicate that special fusion
41234 processing is needed. */
41237 fusion_wrap_memory_address (rtx old_mem)
41239 rtx old_addr = XEXP (old_mem, 0);
41240 rtvec v = gen_rtvec (1, old_addr);
41241 rtx new_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_FUSION_ADDIS);
41242 return replace_equiv_address_nv (old_mem, new_addr, false);
41245 /* Given an address, convert it into the addis and load offset parts. Addresses
41246 created during the peephole2 process look like:
41247 (lo_sum (high (unspec [(sym)] UNSPEC_TOCREL))
41248 (unspec [(...)] UNSPEC_TOCREL))
41250 Addresses created via toc fusion look like:
41251 (unspec [(unspec [(...)] UNSPEC_TOCREL)] UNSPEC_FUSION_ADDIS)) */
41253 static void
41254 fusion_split_address (rtx addr, rtx *p_hi, rtx *p_lo)
41256 rtx hi, lo;
41258 if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_FUSION_ADDIS)
41260 lo = XVECEXP (addr, 0, 0);
41261 hi = gen_rtx_HIGH (Pmode, lo);
41263 else if (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
41265 hi = XEXP (addr, 0);
41266 lo = XEXP (addr, 1);
41268 else
41269 gcc_unreachable ();
41271 *p_hi = hi;
41272 *p_lo = lo;
41275 /* Return a string to fuse an addis instruction with a gpr load to the same
41276 register that we loaded up the addis instruction. The address that is used
41277 is the logical address that was formed during peephole2:
41278 (lo_sum (high) (low-part))
41280 Or the address is the TOC address that is wrapped before register allocation:
41281 (unspec [(addr) (toc-reg)] UNSPEC_FUSION_ADDIS)
41283 The code is complicated, so we call output_asm_insn directly, and just
41284 return "". */
41286 const char *
41287 emit_fusion_gpr_load (rtx target, rtx mem)
41289 rtx addis_value;
41290 rtx addr;
41291 rtx load_offset;
41292 const char *load_str = NULL;
41293 const char *mode_name = NULL;
41294 machine_mode mode;
41296 if (GET_CODE (mem) == ZERO_EXTEND)
41297 mem = XEXP (mem, 0);
41299 gcc_assert (REG_P (target) && MEM_P (mem));
41301 addr = XEXP (mem, 0);
41302 fusion_split_address (addr, &addis_value, &load_offset);
41304 /* Now emit the load instruction to the same register. */
41305 mode = GET_MODE (mem);
41306 switch (mode)
41308 case E_QImode:
41309 mode_name = "char";
41310 load_str = "lbz";
41311 break;
41313 case E_HImode:
41314 mode_name = "short";
41315 load_str = "lhz";
41316 break;
41318 case E_SImode:
41319 case E_SFmode:
41320 mode_name = (mode == SFmode) ? "float" : "int";
41321 load_str = "lwz";
41322 break;
41324 case E_DImode:
41325 case E_DFmode:
41326 gcc_assert (TARGET_POWERPC64);
41327 mode_name = (mode == DFmode) ? "double" : "long";
41328 load_str = "ld";
41329 break;
41331 default:
41332 fatal_insn ("Bad GPR fusion", gen_rtx_SET (target, mem));
41335 /* Emit the addis instruction. */
41336 emit_fusion_addis (target, addis_value, "gpr load fusion", mode_name);
41338 /* Emit the D-form load instruction. */
41339 emit_fusion_load_store (target, target, load_offset, load_str);
41341 return "";
41345 /* Return true if the peephole2 can combine a load/store involving a
41346 combination of an addis instruction and the memory operation. This was
41347 added to the ISA 3.0 (power9) hardware. */
41349 bool
41350 fusion_p9_p (rtx addis_reg, /* register set via addis. */
41351 rtx addis_value, /* addis value. */
41352 rtx dest, /* destination (memory or register). */
41353 rtx src) /* source (register or memory). */
41355 rtx addr, mem, offset;
41356 machine_mode mode = GET_MODE (src);
41358 /* Validate arguments. */
41359 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
41360 return false;
41362 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
41363 return false;
41365 /* Ignore extend operations that are part of the load. */
41366 if (GET_CODE (src) == FLOAT_EXTEND || GET_CODE (src) == ZERO_EXTEND)
41367 src = XEXP (src, 0);
41369 /* Test for memory<-register or register<-memory. */
41370 if (fpr_reg_operand (src, mode) || int_reg_operand (src, mode))
41372 if (!MEM_P (dest))
41373 return false;
41375 mem = dest;
41378 else if (MEM_P (src))
41380 if (!fpr_reg_operand (dest, mode) && !int_reg_operand (dest, mode))
41381 return false;
41383 mem = src;
41386 else
41387 return false;
41389 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
41390 if (GET_CODE (addr) == PLUS)
41392 if (!rtx_equal_p (addis_reg, XEXP (addr, 0)))
41393 return false;
41395 return satisfies_constraint_I (XEXP (addr, 1));
41398 else if (GET_CODE (addr) == LO_SUM)
41400 if (!rtx_equal_p (addis_reg, XEXP (addr, 0)))
41401 return false;
41403 offset = XEXP (addr, 1);
41404 if (TARGET_XCOFF || (TARGET_ELF && TARGET_POWERPC64))
41405 return small_toc_ref (offset, GET_MODE (offset));
41407 else if (TARGET_ELF && !TARGET_POWERPC64)
41408 return CONSTANT_P (offset);
41411 return false;
41414 /* During the peephole2 pass, adjust and expand the insns for an extended fusion
41415 load sequence.
41417 The operands are:
41418 operands[0] register set with addis
41419 operands[1] value set via addis
41420 operands[2] target register being loaded
41421 operands[3] D-form memory reference using operands[0].
41423 This is similar to the fusion introduced with power8, except it scales to
41424 both loads/stores and does not require the result register to be the same as
41425 the base register. At the moment, we only do this if register set with addis
41426 is dead. */
41428 void
41429 expand_fusion_p9_load (rtx *operands)
41431 rtx tmp_reg = operands[0];
41432 rtx addis_value = operands[1];
41433 rtx target = operands[2];
41434 rtx orig_mem = operands[3];
41435 rtx new_addr, new_mem, orig_addr, offset, set, clobber, insn;
41436 enum rtx_code plus_or_lo_sum;
41437 machine_mode target_mode = GET_MODE (target);
41438 machine_mode extend_mode = target_mode;
41439 machine_mode ptr_mode = Pmode;
41440 enum rtx_code extend = UNKNOWN;
41442 if (GET_CODE (orig_mem) == FLOAT_EXTEND || GET_CODE (orig_mem) == ZERO_EXTEND)
41444 extend = GET_CODE (orig_mem);
41445 orig_mem = XEXP (orig_mem, 0);
41446 target_mode = GET_MODE (orig_mem);
41449 gcc_assert (MEM_P (orig_mem));
41451 orig_addr = XEXP (orig_mem, 0);
41452 plus_or_lo_sum = GET_CODE (orig_addr);
41453 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
41455 offset = XEXP (orig_addr, 1);
41456 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
41457 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
41459 if (extend != UNKNOWN)
41460 new_mem = gen_rtx_fmt_e (extend, extend_mode, new_mem);
41462 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
41463 UNSPEC_FUSION_P9);
41465 set = gen_rtx_SET (target, new_mem);
41466 clobber = gen_rtx_CLOBBER (VOIDmode, tmp_reg);
41467 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber));
41468 emit_insn (insn);
41470 return;
41473 /* During the peephole2 pass, adjust and expand the insns for an extended fusion
41474 store sequence.
41476 The operands are:
41477 operands[0] register set with addis
41478 operands[1] value set via addis
41479 operands[2] target D-form memory being stored to
41480 operands[3] register being stored
41482 This is similar to the fusion introduced with power8, except it scales to
41483 both loads/stores and does not require the result register to be the same as
41484 the base register. At the moment, we only do this if register set with addis
41485 is dead. */
41487 void
41488 expand_fusion_p9_store (rtx *operands)
41490 rtx tmp_reg = operands[0];
41491 rtx addis_value = operands[1];
41492 rtx orig_mem = operands[2];
41493 rtx src = operands[3];
41494 rtx new_addr, new_mem, orig_addr, offset, set, clobber, insn, new_src;
41495 enum rtx_code plus_or_lo_sum;
41496 machine_mode target_mode = GET_MODE (orig_mem);
41497 machine_mode ptr_mode = Pmode;
41499 gcc_assert (MEM_P (orig_mem));
41501 orig_addr = XEXP (orig_mem, 0);
41502 plus_or_lo_sum = GET_CODE (orig_addr);
41503 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
41505 offset = XEXP (orig_addr, 1);
41506 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
41507 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
41509 new_src = gen_rtx_UNSPEC (target_mode, gen_rtvec (1, src),
41510 UNSPEC_FUSION_P9);
41512 set = gen_rtx_SET (new_mem, new_src);
41513 clobber = gen_rtx_CLOBBER (VOIDmode, tmp_reg);
41514 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber));
41515 emit_insn (insn);
41517 return;
41520 /* Return a string to fuse an addis instruction with a load using extended
41521 fusion. The address that is used is the logical address that was formed
41522 during peephole2: (lo_sum (high) (low-part))
41524 The code is complicated, so we call output_asm_insn directly, and just
41525 return "". */
41527 const char *
41528 emit_fusion_p9_load (rtx reg, rtx mem, rtx tmp_reg)
41530 machine_mode mode = GET_MODE (reg);
41531 rtx hi;
41532 rtx lo;
41533 rtx addr;
41534 const char *load_string;
41535 int r;
41537 if (GET_CODE (mem) == FLOAT_EXTEND || GET_CODE (mem) == ZERO_EXTEND)
41539 mem = XEXP (mem, 0);
41540 mode = GET_MODE (mem);
41543 if (GET_CODE (reg) == SUBREG)
41545 gcc_assert (SUBREG_BYTE (reg) == 0);
41546 reg = SUBREG_REG (reg);
41549 if (!REG_P (reg))
41550 fatal_insn ("emit_fusion_p9_load, bad reg #1", reg);
41552 r = REGNO (reg);
41553 if (FP_REGNO_P (r))
41555 if (mode == SFmode)
41556 load_string = "lfs";
41557 else if (mode == DFmode || mode == DImode)
41558 load_string = "lfd";
41559 else
41560 gcc_unreachable ();
41562 else if (ALTIVEC_REGNO_P (r) && TARGET_P9_DFORM_SCALAR)
41564 if (mode == SFmode)
41565 load_string = "lxssp";
41566 else if (mode == DFmode || mode == DImode)
41567 load_string = "lxsd";
41568 else
41569 gcc_unreachable ();
41571 else if (INT_REGNO_P (r))
41573 switch (mode)
41575 case E_QImode:
41576 load_string = "lbz";
41577 break;
41578 case E_HImode:
41579 load_string = "lhz";
41580 break;
41581 case E_SImode:
41582 case E_SFmode:
41583 load_string = "lwz";
41584 break;
41585 case E_DImode:
41586 case E_DFmode:
41587 if (!TARGET_POWERPC64)
41588 gcc_unreachable ();
41589 load_string = "ld";
41590 break;
41591 default:
41592 gcc_unreachable ();
41595 else
41596 fatal_insn ("emit_fusion_p9_load, bad reg #2", reg);
41598 if (!MEM_P (mem))
41599 fatal_insn ("emit_fusion_p9_load not MEM", mem);
41601 addr = XEXP (mem, 0);
41602 fusion_split_address (addr, &hi, &lo);
41604 /* Emit the addis instruction. */
41605 emit_fusion_addis (tmp_reg, hi, "power9 load fusion", GET_MODE_NAME (mode));
41607 /* Emit the D-form load instruction. */
41608 emit_fusion_load_store (reg, tmp_reg, lo, load_string);
41610 return "";
41613 /* Return a string to fuse an addis instruction with a store using extended
41614 fusion. The address that is used is the logical address that was formed
41615 during peephole2: (lo_sum (high) (low-part))
41617 The code is complicated, so we call output_asm_insn directly, and just
41618 return "". */
41620 const char *
41621 emit_fusion_p9_store (rtx mem, rtx reg, rtx tmp_reg)
41623 machine_mode mode = GET_MODE (reg);
41624 rtx hi;
41625 rtx lo;
41626 rtx addr;
41627 const char *store_string;
41628 int r;
41630 if (GET_CODE (reg) == SUBREG)
41632 gcc_assert (SUBREG_BYTE (reg) == 0);
41633 reg = SUBREG_REG (reg);
41636 if (!REG_P (reg))
41637 fatal_insn ("emit_fusion_p9_store, bad reg #1", reg);
41639 r = REGNO (reg);
41640 if (FP_REGNO_P (r))
41642 if (mode == SFmode)
41643 store_string = "stfs";
41644 else if (mode == DFmode)
41645 store_string = "stfd";
41646 else
41647 gcc_unreachable ();
41649 else if (ALTIVEC_REGNO_P (r) && TARGET_P9_DFORM_SCALAR)
41651 if (mode == SFmode)
41652 store_string = "stxssp";
41653 else if (mode == DFmode || mode == DImode)
41654 store_string = "stxsd";
41655 else
41656 gcc_unreachable ();
41658 else if (INT_REGNO_P (r))
41660 switch (mode)
41662 case E_QImode:
41663 store_string = "stb";
41664 break;
41665 case E_HImode:
41666 store_string = "sth";
41667 break;
41668 case E_SImode:
41669 case E_SFmode:
41670 store_string = "stw";
41671 break;
41672 case E_DImode:
41673 case E_DFmode:
41674 if (!TARGET_POWERPC64)
41675 gcc_unreachable ();
41676 store_string = "std";
41677 break;
41678 default:
41679 gcc_unreachable ();
41682 else
41683 fatal_insn ("emit_fusion_p9_store, bad reg #2", reg);
41685 if (!MEM_P (mem))
41686 fatal_insn ("emit_fusion_p9_store not MEM", mem);
41688 addr = XEXP (mem, 0);
41689 fusion_split_address (addr, &hi, &lo);
41691 /* Emit the addis instruction. */
41692 emit_fusion_addis (tmp_reg, hi, "power9 store fusion", GET_MODE_NAME (mode));
41694 /* Emit the D-form load instruction. */
41695 emit_fusion_load_store (reg, tmp_reg, lo, store_string);
41697 return "";
41701 /* Analyze vector computations and remove unnecessary doubleword
41702 swaps (xxswapdi instructions). This pass is performed only
41703 for little-endian VSX code generation.
41705 For this specific case, loads and stores of 4x32 and 2x64 vectors
41706 are inefficient. These are implemented using the lvx2dx and
41707 stvx2dx instructions, which invert the order of doublewords in
41708 a vector register. Thus the code generation inserts an xxswapdi
41709 after each such load, and prior to each such store. (For spill
41710 code after register assignment, an additional xxswapdi is inserted
41711 following each store in order to return a hard register to its
41712 unpermuted value.)
41714 The extra xxswapdi instructions reduce performance. This can be
41715 particularly bad for vectorized code. The purpose of this pass
41716 is to reduce the number of xxswapdi instructions required for
41717 correctness.
41719 The primary insight is that much code that operates on vectors
41720 does not care about the relative order of elements in a register,
41721 so long as the correct memory order is preserved. If we have
41722 a computation where all input values are provided by lvxd2x/xxswapdi
41723 sequences, all outputs are stored using xxswapdi/stvxd2x sequences,
41724 and all intermediate computations are pure SIMD (independent of
41725 element order), then all the xxswapdi's associated with the loads
41726 and stores may be removed.
41728 This pass uses some of the infrastructure and logical ideas from
41729 the "web" pass in web.c. We create maximal webs of computations
41730 fitting the description above using union-find. Each such web is
41731 then optimized by removing its unnecessary xxswapdi instructions.
41733 The pass is placed prior to global optimization so that we can
41734 perform the optimization in the safest and simplest way possible;
41735 that is, by replacing each xxswapdi insn with a register copy insn.
41736 Subsequent forward propagation will remove copies where possible.
41738 There are some operations sensitive to element order for which we
41739 can still allow the operation, provided we modify those operations.
41740 These include CONST_VECTORs, for which we must swap the first and
41741 second halves of the constant vector; and SUBREGs, for which we
41742 must adjust the byte offset to account for the swapped doublewords.
41743 A remaining opportunity would be non-immediate-form splats, for
41744 which we should adjust the selected lane of the input. We should
41745 also make code generation adjustments for sum-across operations,
41746 since this is a common vectorizer reduction.
41748 Because we run prior to the first split, we can see loads and stores
41749 here that match *vsx_le_perm_{load,store}_<mode>. These are vanilla
41750 vector loads and stores that have not yet been split into a permuting
41751 load/store and a swap. (One way this can happen is with a builtin
41752 call to vec_vsx_{ld,st}.) We can handle these as well, but rather
41753 than deleting a swap, we convert the load/store into a permuting
41754 load/store (which effectively removes the swap). */
41756 /* Notes on Permutes
41758 We do not currently handle computations that contain permutes. There
41759 is a general transformation that can be performed correctly, but it
41760 may introduce more expensive code than it replaces. To handle these
41761 would require a cost model to determine when to perform the optimization.
41762 This commentary records how this could be done if desired.
41764 The most general permute is something like this (example for V16QI):
41766 (vec_select:V16QI (vec_concat:V32QI (op1:V16QI) (op2:V16QI))
41767 (parallel [(const_int a0) (const_int a1)
41769 (const_int a14) (const_int a15)]))
41771 where a0,...,a15 are in [0,31] and select elements from op1 and op2
41772 to produce in the result.
41774 Regardless of mode, we can convert the PARALLEL to a mask of 16
41775 byte-element selectors. Let's call this M, with M[i] representing
41776 the ith byte-element selector value. Then if we swap doublewords
41777 throughout the computation, we can get correct behavior by replacing
41778 M with M' as follows:
41780 M'[i] = { (M[i]+8)%16 : M[i] in [0,15]
41781 { ((M[i]+8)%16)+16 : M[i] in [16,31]
41783 This seems promising at first, since we are just replacing one mask
41784 with another. But certain masks are preferable to others. If M
41785 is a mask that matches a vmrghh pattern, for example, M' certainly
41786 will not. Instead of a single vmrghh, we would generate a load of
41787 M' and a vperm. So we would need to know how many xxswapd's we can
41788 remove as a result of this transformation to determine if it's
41789 profitable; and preferably the logic would need to be aware of all
41790 the special preferable masks.
41792 Another form of permute is an UNSPEC_VPERM, in which the mask is
41793 already in a register. In some cases, this mask may be a constant
41794 that we can discover with ud-chains, in which case the above
41795 transformation is ok. However, the common usage here is for the
41796 mask to be produced by an UNSPEC_LVSL, in which case the mask
41797 cannot be known at compile time. In such a case we would have to
41798 generate several instructions to compute M' as above at run time,
41799 and a cost model is needed again.
41801 However, when the mask M for an UNSPEC_VPERM is loaded from the
41802 constant pool, we can replace M with M' as above at no cost
41803 beyond adding a constant pool entry. */
41805 /* This is based on the union-find logic in web.c. web_entry_base is
41806 defined in df.h. */
41807 class swap_web_entry : public web_entry_base
41809 public:
41810 /* Pointer to the insn. */
41811 rtx_insn *insn;
41812 /* Set if insn contains a mention of a vector register. All other
41813 fields are undefined if this field is unset. */
41814 unsigned int is_relevant : 1;
41815 /* Set if insn is a load. */
41816 unsigned int is_load : 1;
41817 /* Set if insn is a store. */
41818 unsigned int is_store : 1;
41819 /* Set if insn is a doubleword swap. This can either be a register swap
41820 or a permuting load or store (test is_load and is_store for this). */
41821 unsigned int is_swap : 1;
41822 /* Set if the insn has a live-in use of a parameter register. */
41823 unsigned int is_live_in : 1;
41824 /* Set if the insn has a live-out def of a return register. */
41825 unsigned int is_live_out : 1;
41826 /* Set if the insn contains a subreg reference of a vector register. */
41827 unsigned int contains_subreg : 1;
41828 /* Set if the insn contains a 128-bit integer operand. */
41829 unsigned int is_128_int : 1;
41830 /* Set if this is a call-insn. */
41831 unsigned int is_call : 1;
41832 /* Set if this insn does not perform a vector operation for which
41833 element order matters, or if we know how to fix it up if it does.
41834 Undefined if is_swap is set. */
41835 unsigned int is_swappable : 1;
41836 /* A nonzero value indicates what kind of special handling for this
41837 insn is required if doublewords are swapped. Undefined if
41838 is_swappable is not set. */
41839 unsigned int special_handling : 4;
41840 /* Set if the web represented by this entry cannot be optimized. */
41841 unsigned int web_not_optimizable : 1;
41842 /* Set if this insn should be deleted. */
41843 unsigned int will_delete : 1;
41846 enum special_handling_values {
41847 SH_NONE = 0,
41848 SH_CONST_VECTOR,
41849 SH_SUBREG,
41850 SH_NOSWAP_LD,
41851 SH_NOSWAP_ST,
41852 SH_EXTRACT,
41853 SH_SPLAT,
41854 SH_XXPERMDI,
41855 SH_CONCAT,
41856 SH_VPERM
41859 /* Union INSN with all insns containing definitions that reach USE.
41860 Detect whether USE is live-in to the current function. */
41861 static void
41862 union_defs (swap_web_entry *insn_entry, rtx insn, df_ref use)
41864 struct df_link *link = DF_REF_CHAIN (use);
41866 if (!link)
41867 insn_entry[INSN_UID (insn)].is_live_in = 1;
41869 while (link)
41871 if (DF_REF_IS_ARTIFICIAL (link->ref))
41872 insn_entry[INSN_UID (insn)].is_live_in = 1;
41874 if (DF_REF_INSN_INFO (link->ref))
41876 rtx def_insn = DF_REF_INSN (link->ref);
41877 (void)unionfind_union (insn_entry + INSN_UID (insn),
41878 insn_entry + INSN_UID (def_insn));
41881 link = link->next;
41885 /* Union INSN with all insns containing uses reached from DEF.
41886 Detect whether DEF is live-out from the current function. */
41887 static void
41888 union_uses (swap_web_entry *insn_entry, rtx insn, df_ref def)
41890 struct df_link *link = DF_REF_CHAIN (def);
41892 if (!link)
41893 insn_entry[INSN_UID (insn)].is_live_out = 1;
41895 while (link)
41897 /* This could be an eh use or some other artificial use;
41898 we treat these all the same (killing the optimization). */
41899 if (DF_REF_IS_ARTIFICIAL (link->ref))
41900 insn_entry[INSN_UID (insn)].is_live_out = 1;
41902 if (DF_REF_INSN_INFO (link->ref))
41904 rtx use_insn = DF_REF_INSN (link->ref);
41905 (void)unionfind_union (insn_entry + INSN_UID (insn),
41906 insn_entry + INSN_UID (use_insn));
41909 link = link->next;
41913 /* Return 1 iff INSN is a load insn, including permuting loads that
41914 represent an lvxd2x instruction; else return 0. */
41915 static unsigned int
41916 insn_is_load_p (rtx insn)
41918 rtx body = PATTERN (insn);
41920 if (GET_CODE (body) == SET)
41922 if (GET_CODE (SET_SRC (body)) == MEM)
41923 return 1;
41925 if (GET_CODE (SET_SRC (body)) == VEC_SELECT
41926 && GET_CODE (XEXP (SET_SRC (body), 0)) == MEM)
41927 return 1;
41929 return 0;
41932 if (GET_CODE (body) != PARALLEL)
41933 return 0;
41935 rtx set = XVECEXP (body, 0, 0);
41937 if (GET_CODE (set) == SET && GET_CODE (SET_SRC (set)) == MEM)
41938 return 1;
41940 return 0;
41943 /* Return 1 iff INSN is a store insn, including permuting stores that
41944 represent an stvxd2x instruction; else return 0. */
41945 static unsigned int
41946 insn_is_store_p (rtx insn)
41948 rtx body = PATTERN (insn);
41949 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == MEM)
41950 return 1;
41951 if (GET_CODE (body) != PARALLEL)
41952 return 0;
41953 rtx set = XVECEXP (body, 0, 0);
41954 if (GET_CODE (set) == SET && GET_CODE (SET_DEST (set)) == MEM)
41955 return 1;
41956 return 0;
41959 /* Return 1 iff INSN swaps doublewords. This may be a reg-reg swap,
41960 a permuting load, or a permuting store. */
41961 static unsigned int
41962 insn_is_swap_p (rtx insn)
41964 rtx body = PATTERN (insn);
41965 if (GET_CODE (body) != SET)
41966 return 0;
41967 rtx rhs = SET_SRC (body);
41968 if (GET_CODE (rhs) != VEC_SELECT)
41969 return 0;
41970 rtx parallel = XEXP (rhs, 1);
41971 if (GET_CODE (parallel) != PARALLEL)
41972 return 0;
41973 unsigned int len = XVECLEN (parallel, 0);
41974 if (len != 2 && len != 4 && len != 8 && len != 16)
41975 return 0;
41976 for (unsigned int i = 0; i < len / 2; ++i)
41978 rtx op = XVECEXP (parallel, 0, i);
41979 if (GET_CODE (op) != CONST_INT || INTVAL (op) != len / 2 + i)
41980 return 0;
41982 for (unsigned int i = len / 2; i < len; ++i)
41984 rtx op = XVECEXP (parallel, 0, i);
41985 if (GET_CODE (op) != CONST_INT || INTVAL (op) != i - len / 2)
41986 return 0;
41988 return 1;
41991 /* Return TRUE if insn is a swap fed by a load from the constant pool. */
41992 static bool
41993 const_load_sequence_p (swap_web_entry *insn_entry, rtx insn)
41995 unsigned uid = INSN_UID (insn);
41996 if (!insn_entry[uid].is_swap || insn_entry[uid].is_load)
41997 return false;
41999 /* Find the unique use in the swap and locate its def. If the def
42000 isn't unique, punt. */
42001 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42002 df_ref use;
42003 FOR_EACH_INSN_INFO_USE (use, insn_info)
42005 struct df_link *def_link = DF_REF_CHAIN (use);
42006 if (!def_link || def_link->next)
42007 return false;
42009 rtx def_insn = DF_REF_INSN (def_link->ref);
42010 unsigned uid2 = INSN_UID (def_insn);
42011 if (!insn_entry[uid2].is_load || !insn_entry[uid2].is_swap)
42012 return false;
42014 rtx body = PATTERN (def_insn);
42015 if (GET_CODE (body) != SET
42016 || GET_CODE (SET_SRC (body)) != VEC_SELECT
42017 || GET_CODE (XEXP (SET_SRC (body), 0)) != MEM)
42018 return false;
42020 rtx mem = XEXP (SET_SRC (body), 0);
42021 rtx base_reg = XEXP (mem, 0);
42023 df_ref base_use;
42024 insn_info = DF_INSN_INFO_GET (def_insn);
42025 FOR_EACH_INSN_INFO_USE (base_use, insn_info)
42027 if (!rtx_equal_p (DF_REF_REG (base_use), base_reg))
42028 continue;
42030 struct df_link *base_def_link = DF_REF_CHAIN (base_use);
42031 if (!base_def_link || base_def_link->next)
42032 return false;
42034 rtx tocrel_insn = DF_REF_INSN (base_def_link->ref);
42035 rtx tocrel_body = PATTERN (tocrel_insn);
42036 rtx base, offset;
42037 if (GET_CODE (tocrel_body) != SET)
42038 return false;
42039 /* There is an extra level of indirection for small/large
42040 code models. */
42041 rtx tocrel_expr = SET_SRC (tocrel_body);
42042 if (GET_CODE (tocrel_expr) == MEM)
42043 tocrel_expr = XEXP (tocrel_expr, 0);
42044 if (!toc_relative_expr_p (tocrel_expr, false))
42045 return false;
42046 split_const (XVECEXP (tocrel_base, 0, 0), &base, &offset);
42047 if (GET_CODE (base) != SYMBOL_REF || !CONSTANT_POOL_ADDRESS_P (base))
42048 return false;
42051 return true;
42054 /* Return TRUE iff OP matches a V2DF reduction pattern. See the
42055 definition of vsx_reduc_<VEC_reduc_name>_v2df in vsx.md. */
42056 static bool
42057 v2df_reduction_p (rtx op)
42059 if (GET_MODE (op) != V2DFmode)
42060 return false;
42062 enum rtx_code code = GET_CODE (op);
42063 if (code != PLUS && code != SMIN && code != SMAX)
42064 return false;
42066 rtx concat = XEXP (op, 0);
42067 if (GET_CODE (concat) != VEC_CONCAT)
42068 return false;
42070 rtx select0 = XEXP (concat, 0);
42071 rtx select1 = XEXP (concat, 1);
42072 if (GET_CODE (select0) != VEC_SELECT || GET_CODE (select1) != VEC_SELECT)
42073 return false;
42075 rtx reg0 = XEXP (select0, 0);
42076 rtx reg1 = XEXP (select1, 0);
42077 if (!rtx_equal_p (reg0, reg1) || !REG_P (reg0))
42078 return false;
42080 rtx parallel0 = XEXP (select0, 1);
42081 rtx parallel1 = XEXP (select1, 1);
42082 if (GET_CODE (parallel0) != PARALLEL || GET_CODE (parallel1) != PARALLEL)
42083 return false;
42085 if (!rtx_equal_p (XVECEXP (parallel0, 0, 0), const1_rtx)
42086 || !rtx_equal_p (XVECEXP (parallel1, 0, 0), const0_rtx))
42087 return false;
42089 return true;
42092 /* Return 1 iff OP is an operand that will not be affected by having
42093 vector doublewords swapped in memory. */
42094 static unsigned int
42095 rtx_is_swappable_p (rtx op, unsigned int *special)
42097 enum rtx_code code = GET_CODE (op);
42098 int i, j;
42099 rtx parallel;
42101 switch (code)
42103 case LABEL_REF:
42104 case SYMBOL_REF:
42105 case CLOBBER:
42106 case REG:
42107 return 1;
42109 case VEC_CONCAT:
42110 case ASM_INPUT:
42111 case ASM_OPERANDS:
42112 return 0;
42114 case CONST_VECTOR:
42116 *special = SH_CONST_VECTOR;
42117 return 1;
42120 case VEC_DUPLICATE:
42121 /* Opportunity: If XEXP (op, 0) has the same mode as the result,
42122 and XEXP (op, 1) is a PARALLEL with a single QImode const int,
42123 it represents a vector splat for which we can do special
42124 handling. */
42125 if (GET_CODE (XEXP (op, 0)) == CONST_INT)
42126 return 1;
42127 else if (REG_P (XEXP (op, 0))
42128 && GET_MODE_INNER (GET_MODE (op)) == GET_MODE (XEXP (op, 0)))
42129 /* This catches V2DF and V2DI splat, at a minimum. */
42130 return 1;
42131 else if (GET_CODE (XEXP (op, 0)) == TRUNCATE
42132 && REG_P (XEXP (XEXP (op, 0), 0))
42133 && GET_MODE_INNER (GET_MODE (op)) == GET_MODE (XEXP (op, 0)))
42134 /* This catches splat of a truncated value. */
42135 return 1;
42136 else if (GET_CODE (XEXP (op, 0)) == VEC_SELECT)
42137 /* If the duplicated item is from a select, defer to the select
42138 processing to see if we can change the lane for the splat. */
42139 return rtx_is_swappable_p (XEXP (op, 0), special);
42140 else
42141 return 0;
42143 case VEC_SELECT:
42144 /* A vec_extract operation is ok if we change the lane. */
42145 if (GET_CODE (XEXP (op, 0)) == REG
42146 && GET_MODE_INNER (GET_MODE (XEXP (op, 0))) == GET_MODE (op)
42147 && GET_CODE ((parallel = XEXP (op, 1))) == PARALLEL
42148 && XVECLEN (parallel, 0) == 1
42149 && GET_CODE (XVECEXP (parallel, 0, 0)) == CONST_INT)
42151 *special = SH_EXTRACT;
42152 return 1;
42154 /* An XXPERMDI is ok if we adjust the lanes. Note that if the
42155 XXPERMDI is a swap operation, it will be identified by
42156 insn_is_swap_p and therefore we won't get here. */
42157 else if (GET_CODE (XEXP (op, 0)) == VEC_CONCAT
42158 && (GET_MODE (XEXP (op, 0)) == V4DFmode
42159 || GET_MODE (XEXP (op, 0)) == V4DImode)
42160 && GET_CODE ((parallel = XEXP (op, 1))) == PARALLEL
42161 && XVECLEN (parallel, 0) == 2
42162 && GET_CODE (XVECEXP (parallel, 0, 0)) == CONST_INT
42163 && GET_CODE (XVECEXP (parallel, 0, 1)) == CONST_INT)
42165 *special = SH_XXPERMDI;
42166 return 1;
42168 else if (v2df_reduction_p (op))
42169 return 1;
42170 else
42171 return 0;
42173 case UNSPEC:
42175 /* Various operations are unsafe for this optimization, at least
42176 without significant additional work. Permutes are obviously
42177 problematic, as both the permute control vector and the ordering
42178 of the target values are invalidated by doubleword swapping.
42179 Vector pack and unpack modify the number of vector lanes.
42180 Merge-high/low will not operate correctly on swapped operands.
42181 Vector shifts across element boundaries are clearly uncool,
42182 as are vector select and concatenate operations. Vector
42183 sum-across instructions define one operand with a specific
42184 order-dependent element, so additional fixup code would be
42185 needed to make those work. Vector set and non-immediate-form
42186 vector splat are element-order sensitive. A few of these
42187 cases might be workable with special handling if required.
42188 Adding cost modeling would be appropriate in some cases. */
42189 int val = XINT (op, 1);
42190 switch (val)
42192 default:
42193 break;
42194 case UNSPEC_VMRGH_DIRECT:
42195 case UNSPEC_VMRGL_DIRECT:
42196 case UNSPEC_VPACK_SIGN_SIGN_SAT:
42197 case UNSPEC_VPACK_SIGN_UNS_SAT:
42198 case UNSPEC_VPACK_UNS_UNS_MOD:
42199 case UNSPEC_VPACK_UNS_UNS_MOD_DIRECT:
42200 case UNSPEC_VPACK_UNS_UNS_SAT:
42201 case UNSPEC_VPERM:
42202 case UNSPEC_VPERM_UNS:
42203 case UNSPEC_VPERMHI:
42204 case UNSPEC_VPERMSI:
42205 case UNSPEC_VPKPX:
42206 case UNSPEC_VSLDOI:
42207 case UNSPEC_VSLO:
42208 case UNSPEC_VSRO:
42209 case UNSPEC_VSUM2SWS:
42210 case UNSPEC_VSUM4S:
42211 case UNSPEC_VSUM4UBS:
42212 case UNSPEC_VSUMSWS:
42213 case UNSPEC_VSUMSWS_DIRECT:
42214 case UNSPEC_VSX_CONCAT:
42215 case UNSPEC_VSX_SET:
42216 case UNSPEC_VSX_SLDWI:
42217 case UNSPEC_VUNPACK_HI_SIGN:
42218 case UNSPEC_VUNPACK_HI_SIGN_DIRECT:
42219 case UNSPEC_VUNPACK_LO_SIGN:
42220 case UNSPEC_VUNPACK_LO_SIGN_DIRECT:
42221 case UNSPEC_VUPKHPX:
42222 case UNSPEC_VUPKHS_V4SF:
42223 case UNSPEC_VUPKHU_V4SF:
42224 case UNSPEC_VUPKLPX:
42225 case UNSPEC_VUPKLS_V4SF:
42226 case UNSPEC_VUPKLU_V4SF:
42227 case UNSPEC_VSX_CVDPSPN:
42228 case UNSPEC_VSX_CVSPDP:
42229 case UNSPEC_VSX_CVSPDPN:
42230 case UNSPEC_VSX_EXTRACT:
42231 case UNSPEC_VSX_VSLO:
42232 case UNSPEC_VSX_VEC_INIT:
42233 return 0;
42234 case UNSPEC_VSPLT_DIRECT:
42235 case UNSPEC_VSX_XXSPLTD:
42236 *special = SH_SPLAT;
42237 return 1;
42238 case UNSPEC_REDUC_PLUS:
42239 case UNSPEC_REDUC:
42240 return 1;
42244 default:
42245 break;
42248 const char *fmt = GET_RTX_FORMAT (code);
42249 int ok = 1;
42251 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
42252 if (fmt[i] == 'e' || fmt[i] == 'u')
42254 unsigned int special_op = SH_NONE;
42255 ok &= rtx_is_swappable_p (XEXP (op, i), &special_op);
42256 if (special_op == SH_NONE)
42257 continue;
42258 /* Ensure we never have two kinds of special handling
42259 for the same insn. */
42260 if (*special != SH_NONE && *special != special_op)
42261 return 0;
42262 *special = special_op;
42264 else if (fmt[i] == 'E')
42265 for (j = 0; j < XVECLEN (op, i); ++j)
42267 unsigned int special_op = SH_NONE;
42268 ok &= rtx_is_swappable_p (XVECEXP (op, i, j), &special_op);
42269 if (special_op == SH_NONE)
42270 continue;
42271 /* Ensure we never have two kinds of special handling
42272 for the same insn. */
42273 if (*special != SH_NONE && *special != special_op)
42274 return 0;
42275 *special = special_op;
42278 return ok;
42281 /* Return 1 iff INSN is an operand that will not be affected by
42282 having vector doublewords swapped in memory (in which case
42283 *SPECIAL is unchanged), or that can be modified to be correct
42284 if vector doublewords are swapped in memory (in which case
42285 *SPECIAL is changed to a value indicating how). */
42286 static unsigned int
42287 insn_is_swappable_p (swap_web_entry *insn_entry, rtx insn,
42288 unsigned int *special)
42290 /* Calls are always bad. */
42291 if (GET_CODE (insn) == CALL_INSN)
42292 return 0;
42294 /* Loads and stores seen here are not permuting, but we can still
42295 fix them up by converting them to permuting ones. Exceptions:
42296 UNSPEC_LVE, UNSPEC_LVX, and UNSPEC_STVX, which have a PARALLEL
42297 body instead of a SET; and UNSPEC_STVE, which has an UNSPEC
42298 for the SET source. Also we must now make an exception for lvx
42299 and stvx when they are not in the UNSPEC_LVX/STVX form (with the
42300 explicit "& -16") since this leads to unrecognizable insns. */
42301 rtx body = PATTERN (insn);
42302 int i = INSN_UID (insn);
42304 if (insn_entry[i].is_load)
42306 if (GET_CODE (body) == SET)
42308 rtx rhs = SET_SRC (body);
42309 /* Even without a swap, the RHS might be a vec_select for, say,
42310 a byte-reversing load. */
42311 if (GET_CODE (rhs) != MEM)
42312 return 0;
42313 if (GET_CODE (XEXP (rhs, 0)) == AND)
42314 return 0;
42316 *special = SH_NOSWAP_LD;
42317 return 1;
42319 else
42320 return 0;
42323 if (insn_entry[i].is_store)
42325 if (GET_CODE (body) == SET
42326 && GET_CODE (SET_SRC (body)) != UNSPEC)
42328 rtx lhs = SET_DEST (body);
42329 /* Even without a swap, the LHS might be a vec_select for, say,
42330 a byte-reversing store. */
42331 if (GET_CODE (lhs) != MEM)
42332 return 0;
42333 if (GET_CODE (XEXP (lhs, 0)) == AND)
42334 return 0;
42336 *special = SH_NOSWAP_ST;
42337 return 1;
42339 else
42340 return 0;
42343 /* A convert to single precision can be left as is provided that
42344 all of its uses are in xxspltw instructions that splat BE element
42345 zero. */
42346 if (GET_CODE (body) == SET
42347 && GET_CODE (SET_SRC (body)) == UNSPEC
42348 && XINT (SET_SRC (body), 1) == UNSPEC_VSX_CVDPSPN)
42350 df_ref def;
42351 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42353 FOR_EACH_INSN_INFO_DEF (def, insn_info)
42355 struct df_link *link = DF_REF_CHAIN (def);
42356 if (!link)
42357 return 0;
42359 for (; link; link = link->next) {
42360 rtx use_insn = DF_REF_INSN (link->ref);
42361 rtx use_body = PATTERN (use_insn);
42362 if (GET_CODE (use_body) != SET
42363 || GET_CODE (SET_SRC (use_body)) != UNSPEC
42364 || XINT (SET_SRC (use_body), 1) != UNSPEC_VSX_XXSPLTW
42365 || XVECEXP (SET_SRC (use_body), 0, 1) != const0_rtx)
42366 return 0;
42370 return 1;
42373 /* A concatenation of two doublewords is ok if we reverse the
42374 order of the inputs. */
42375 if (GET_CODE (body) == SET
42376 && GET_CODE (SET_SRC (body)) == VEC_CONCAT
42377 && (GET_MODE (SET_SRC (body)) == V2DFmode
42378 || GET_MODE (SET_SRC (body)) == V2DImode))
42380 *special = SH_CONCAT;
42381 return 1;
42384 /* V2DF reductions are always swappable. */
42385 if (GET_CODE (body) == PARALLEL)
42387 rtx expr = XVECEXP (body, 0, 0);
42388 if (GET_CODE (expr) == SET
42389 && v2df_reduction_p (SET_SRC (expr)))
42390 return 1;
42393 /* An UNSPEC_VPERM is ok if the mask operand is loaded from the
42394 constant pool. */
42395 if (GET_CODE (body) == SET
42396 && GET_CODE (SET_SRC (body)) == UNSPEC
42397 && XINT (SET_SRC (body), 1) == UNSPEC_VPERM
42398 && XVECLEN (SET_SRC (body), 0) == 3
42399 && GET_CODE (XVECEXP (SET_SRC (body), 0, 2)) == REG)
42401 rtx mask_reg = XVECEXP (SET_SRC (body), 0, 2);
42402 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42403 df_ref use;
42404 FOR_EACH_INSN_INFO_USE (use, insn_info)
42405 if (rtx_equal_p (DF_REF_REG (use), mask_reg))
42407 struct df_link *def_link = DF_REF_CHAIN (use);
42408 /* Punt if multiple definitions for this reg. */
42409 if (def_link && !def_link->next &&
42410 const_load_sequence_p (insn_entry,
42411 DF_REF_INSN (def_link->ref)))
42413 *special = SH_VPERM;
42414 return 1;
42419 /* Otherwise check the operands for vector lane violations. */
42420 return rtx_is_swappable_p (body, special);
42423 enum chain_purpose { FOR_LOADS, FOR_STORES };
42425 /* Return true if the UD or DU chain headed by LINK is non-empty,
42426 and every entry on the chain references an insn that is a
42427 register swap. Furthermore, if PURPOSE is FOR_LOADS, each such
42428 register swap must have only permuting loads as reaching defs.
42429 If PURPOSE is FOR_STORES, each such register swap must have only
42430 register swaps or permuting stores as reached uses. */
42431 static bool
42432 chain_contains_only_swaps (swap_web_entry *insn_entry, struct df_link *link,
42433 enum chain_purpose purpose)
42435 if (!link)
42436 return false;
42438 for (; link; link = link->next)
42440 if (!ALTIVEC_OR_VSX_VECTOR_MODE (GET_MODE (DF_REF_REG (link->ref))))
42441 continue;
42443 if (DF_REF_IS_ARTIFICIAL (link->ref))
42444 return false;
42446 rtx reached_insn = DF_REF_INSN (link->ref);
42447 unsigned uid = INSN_UID (reached_insn);
42448 struct df_insn_info *insn_info = DF_INSN_INFO_GET (reached_insn);
42450 if (!insn_entry[uid].is_swap || insn_entry[uid].is_load
42451 || insn_entry[uid].is_store)
42452 return false;
42454 if (purpose == FOR_LOADS)
42456 df_ref use;
42457 FOR_EACH_INSN_INFO_USE (use, insn_info)
42459 struct df_link *swap_link = DF_REF_CHAIN (use);
42461 while (swap_link)
42463 if (DF_REF_IS_ARTIFICIAL (link->ref))
42464 return false;
42466 rtx swap_def_insn = DF_REF_INSN (swap_link->ref);
42467 unsigned uid2 = INSN_UID (swap_def_insn);
42469 /* Only permuting loads are allowed. */
42470 if (!insn_entry[uid2].is_swap || !insn_entry[uid2].is_load)
42471 return false;
42473 swap_link = swap_link->next;
42477 else if (purpose == FOR_STORES)
42479 df_ref def;
42480 FOR_EACH_INSN_INFO_DEF (def, insn_info)
42482 struct df_link *swap_link = DF_REF_CHAIN (def);
42484 while (swap_link)
42486 if (DF_REF_IS_ARTIFICIAL (link->ref))
42487 return false;
42489 rtx swap_use_insn = DF_REF_INSN (swap_link->ref);
42490 unsigned uid2 = INSN_UID (swap_use_insn);
42492 /* Permuting stores or register swaps are allowed. */
42493 if (!insn_entry[uid2].is_swap || insn_entry[uid2].is_load)
42494 return false;
42496 swap_link = swap_link->next;
42502 return true;
42505 /* Mark the xxswapdi instructions associated with permuting loads and
42506 stores for removal. Note that we only flag them for deletion here,
42507 as there is a possibility of a swap being reached from multiple
42508 loads, etc. */
42509 static void
42510 mark_swaps_for_removal (swap_web_entry *insn_entry, unsigned int i)
42512 rtx insn = insn_entry[i].insn;
42513 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42515 if (insn_entry[i].is_load)
42517 df_ref def;
42518 FOR_EACH_INSN_INFO_DEF (def, insn_info)
42520 struct df_link *link = DF_REF_CHAIN (def);
42522 /* We know by now that these are swaps, so we can delete
42523 them confidently. */
42524 while (link)
42526 rtx use_insn = DF_REF_INSN (link->ref);
42527 insn_entry[INSN_UID (use_insn)].will_delete = 1;
42528 link = link->next;
42532 else if (insn_entry[i].is_store)
42534 df_ref use;
42535 FOR_EACH_INSN_INFO_USE (use, insn_info)
42537 /* Ignore uses for addressability. */
42538 machine_mode mode = GET_MODE (DF_REF_REG (use));
42539 if (!ALTIVEC_OR_VSX_VECTOR_MODE (mode))
42540 continue;
42542 struct df_link *link = DF_REF_CHAIN (use);
42544 /* We know by now that these are swaps, so we can delete
42545 them confidently. */
42546 while (link)
42548 rtx def_insn = DF_REF_INSN (link->ref);
42549 insn_entry[INSN_UID (def_insn)].will_delete = 1;
42550 link = link->next;
42556 /* OP is either a CONST_VECTOR or an expression containing one.
42557 Swap the first half of the vector with the second in the first
42558 case. Recurse to find it in the second. */
42559 static void
42560 swap_const_vector_halves (rtx op)
42562 int i;
42563 enum rtx_code code = GET_CODE (op);
42564 if (GET_CODE (op) == CONST_VECTOR)
42566 int half_units = GET_MODE_NUNITS (GET_MODE (op)) / 2;
42567 for (i = 0; i < half_units; ++i)
42569 rtx temp = CONST_VECTOR_ELT (op, i);
42570 CONST_VECTOR_ELT (op, i) = CONST_VECTOR_ELT (op, i + half_units);
42571 CONST_VECTOR_ELT (op, i + half_units) = temp;
42574 else
42576 int j;
42577 const char *fmt = GET_RTX_FORMAT (code);
42578 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
42579 if (fmt[i] == 'e' || fmt[i] == 'u')
42580 swap_const_vector_halves (XEXP (op, i));
42581 else if (fmt[i] == 'E')
42582 for (j = 0; j < XVECLEN (op, i); ++j)
42583 swap_const_vector_halves (XVECEXP (op, i, j));
42587 /* Find all subregs of a vector expression that perform a narrowing,
42588 and adjust the subreg index to account for doubleword swapping. */
42589 static void
42590 adjust_subreg_index (rtx op)
42592 enum rtx_code code = GET_CODE (op);
42593 if (code == SUBREG
42594 && (GET_MODE_SIZE (GET_MODE (op))
42595 < GET_MODE_SIZE (GET_MODE (XEXP (op, 0)))))
42597 unsigned int index = SUBREG_BYTE (op);
42598 if (index < 8)
42599 index += 8;
42600 else
42601 index -= 8;
42602 SUBREG_BYTE (op) = index;
42605 const char *fmt = GET_RTX_FORMAT (code);
42606 int i,j;
42607 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
42608 if (fmt[i] == 'e' || fmt[i] == 'u')
42609 adjust_subreg_index (XEXP (op, i));
42610 else if (fmt[i] == 'E')
42611 for (j = 0; j < XVECLEN (op, i); ++j)
42612 adjust_subreg_index (XVECEXP (op, i, j));
42615 /* Convert the non-permuting load INSN to a permuting one. */
42616 static void
42617 permute_load (rtx_insn *insn)
42619 rtx body = PATTERN (insn);
42620 rtx mem_op = SET_SRC (body);
42621 rtx tgt_reg = SET_DEST (body);
42622 machine_mode mode = GET_MODE (tgt_reg);
42623 int n_elts = GET_MODE_NUNITS (mode);
42624 int half_elts = n_elts / 2;
42625 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
42626 int i, j;
42627 for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
42628 XVECEXP (par, 0, i) = GEN_INT (j);
42629 for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
42630 XVECEXP (par, 0, i) = GEN_INT (j);
42631 rtx sel = gen_rtx_VEC_SELECT (mode, mem_op, par);
42632 SET_SRC (body) = sel;
42633 INSN_CODE (insn) = -1; /* Force re-recognition. */
42634 df_insn_rescan (insn);
42636 if (dump_file)
42637 fprintf (dump_file, "Replacing load %d with permuted load\n",
42638 INSN_UID (insn));
42641 /* Convert the non-permuting store INSN to a permuting one. */
42642 static void
42643 permute_store (rtx_insn *insn)
42645 rtx body = PATTERN (insn);
42646 rtx src_reg = SET_SRC (body);
42647 machine_mode mode = GET_MODE (src_reg);
42648 int n_elts = GET_MODE_NUNITS (mode);
42649 int half_elts = n_elts / 2;
42650 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
42651 int i, j;
42652 for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
42653 XVECEXP (par, 0, i) = GEN_INT (j);
42654 for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
42655 XVECEXP (par, 0, i) = GEN_INT (j);
42656 rtx sel = gen_rtx_VEC_SELECT (mode, src_reg, par);
42657 SET_SRC (body) = sel;
42658 INSN_CODE (insn) = -1; /* Force re-recognition. */
42659 df_insn_rescan (insn);
42661 if (dump_file)
42662 fprintf (dump_file, "Replacing store %d with permuted store\n",
42663 INSN_UID (insn));
42666 /* Given OP that contains a vector extract operation, adjust the index
42667 of the extracted lane to account for the doubleword swap. */
42668 static void
42669 adjust_extract (rtx_insn *insn)
42671 rtx pattern = PATTERN (insn);
42672 if (GET_CODE (pattern) == PARALLEL)
42673 pattern = XVECEXP (pattern, 0, 0);
42674 rtx src = SET_SRC (pattern);
42675 /* The vec_select may be wrapped in a vec_duplicate for a splat, so
42676 account for that. */
42677 rtx sel = GET_CODE (src) == VEC_DUPLICATE ? XEXP (src, 0) : src;
42678 rtx par = XEXP (sel, 1);
42679 int half_elts = GET_MODE_NUNITS (GET_MODE (XEXP (sel, 0))) >> 1;
42680 int lane = INTVAL (XVECEXP (par, 0, 0));
42681 lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
42682 XVECEXP (par, 0, 0) = GEN_INT (lane);
42683 INSN_CODE (insn) = -1; /* Force re-recognition. */
42684 df_insn_rescan (insn);
42686 if (dump_file)
42687 fprintf (dump_file, "Changing lane for extract %d\n", INSN_UID (insn));
42690 /* Given OP that contains a vector direct-splat operation, adjust the index
42691 of the source lane to account for the doubleword swap. */
42692 static void
42693 adjust_splat (rtx_insn *insn)
42695 rtx body = PATTERN (insn);
42696 rtx unspec = XEXP (body, 1);
42697 int half_elts = GET_MODE_NUNITS (GET_MODE (unspec)) >> 1;
42698 int lane = INTVAL (XVECEXP (unspec, 0, 1));
42699 lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
42700 XVECEXP (unspec, 0, 1) = GEN_INT (lane);
42701 INSN_CODE (insn) = -1; /* Force re-recognition. */
42702 df_insn_rescan (insn);
42704 if (dump_file)
42705 fprintf (dump_file, "Changing lane for splat %d\n", INSN_UID (insn));
42708 /* Given OP that contains an XXPERMDI operation (that is not a doubleword
42709 swap), reverse the order of the source operands and adjust the indices
42710 of the source lanes to account for doubleword reversal. */
42711 static void
42712 adjust_xxpermdi (rtx_insn *insn)
42714 rtx set = PATTERN (insn);
42715 rtx select = XEXP (set, 1);
42716 rtx concat = XEXP (select, 0);
42717 rtx src0 = XEXP (concat, 0);
42718 XEXP (concat, 0) = XEXP (concat, 1);
42719 XEXP (concat, 1) = src0;
42720 rtx parallel = XEXP (select, 1);
42721 int lane0 = INTVAL (XVECEXP (parallel, 0, 0));
42722 int lane1 = INTVAL (XVECEXP (parallel, 0, 1));
42723 int new_lane0 = 3 - lane1;
42724 int new_lane1 = 3 - lane0;
42725 XVECEXP (parallel, 0, 0) = GEN_INT (new_lane0);
42726 XVECEXP (parallel, 0, 1) = GEN_INT (new_lane1);
42727 INSN_CODE (insn) = -1; /* Force re-recognition. */
42728 df_insn_rescan (insn);
42730 if (dump_file)
42731 fprintf (dump_file, "Changing lanes for xxpermdi %d\n", INSN_UID (insn));
42734 /* Given OP that contains a VEC_CONCAT operation of two doublewords,
42735 reverse the order of those inputs. */
42736 static void
42737 adjust_concat (rtx_insn *insn)
42739 rtx set = PATTERN (insn);
42740 rtx concat = XEXP (set, 1);
42741 rtx src0 = XEXP (concat, 0);
42742 XEXP (concat, 0) = XEXP (concat, 1);
42743 XEXP (concat, 1) = src0;
42744 INSN_CODE (insn) = -1; /* Force re-recognition. */
42745 df_insn_rescan (insn);
42747 if (dump_file)
42748 fprintf (dump_file, "Reversing inputs for concat %d\n", INSN_UID (insn));
42751 /* Given an UNSPEC_VPERM insn, modify the mask loaded from the
42752 constant pool to reflect swapped doublewords. */
42753 static void
42754 adjust_vperm (rtx_insn *insn)
42756 /* We previously determined that the UNSPEC_VPERM was fed by a
42757 swap of a swapping load of a TOC-relative constant pool symbol.
42758 Find the MEM in the swapping load and replace it with a MEM for
42759 the adjusted mask constant. */
42760 rtx set = PATTERN (insn);
42761 rtx mask_reg = XVECEXP (SET_SRC (set), 0, 2);
42763 /* Find the swap. */
42764 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
42765 df_ref use;
42766 rtx_insn *swap_insn = 0;
42767 FOR_EACH_INSN_INFO_USE (use, insn_info)
42768 if (rtx_equal_p (DF_REF_REG (use), mask_reg))
42770 struct df_link *def_link = DF_REF_CHAIN (use);
42771 gcc_assert (def_link && !def_link->next);
42772 swap_insn = DF_REF_INSN (def_link->ref);
42773 break;
42775 gcc_assert (swap_insn);
42777 /* Find the load. */
42778 insn_info = DF_INSN_INFO_GET (swap_insn);
42779 rtx_insn *load_insn = 0;
42780 FOR_EACH_INSN_INFO_USE (use, insn_info)
42782 struct df_link *def_link = DF_REF_CHAIN (use);
42783 gcc_assert (def_link && !def_link->next);
42784 load_insn = DF_REF_INSN (def_link->ref);
42785 break;
42787 gcc_assert (load_insn);
42789 /* Find the TOC-relative symbol access. */
42790 insn_info = DF_INSN_INFO_GET (load_insn);
42791 rtx_insn *tocrel_insn = 0;
42792 FOR_EACH_INSN_INFO_USE (use, insn_info)
42794 struct df_link *def_link = DF_REF_CHAIN (use);
42795 gcc_assert (def_link && !def_link->next);
42796 tocrel_insn = DF_REF_INSN (def_link->ref);
42797 break;
42799 gcc_assert (tocrel_insn);
42801 /* Find the embedded CONST_VECTOR. We have to call toc_relative_expr_p
42802 to set tocrel_base; otherwise it would be unnecessary as we've
42803 already established it will return true. */
42804 rtx base, offset;
42805 rtx tocrel_expr = SET_SRC (PATTERN (tocrel_insn));
42806 /* There is an extra level of indirection for small/large code models. */
42807 if (GET_CODE (tocrel_expr) == MEM)
42808 tocrel_expr = XEXP (tocrel_expr, 0);
42809 if (!toc_relative_expr_p (tocrel_expr, false))
42810 gcc_unreachable ();
42811 split_const (XVECEXP (tocrel_base, 0, 0), &base, &offset);
42812 rtx const_vector = get_pool_constant (base);
42813 /* With the extra indirection, get_pool_constant will produce the
42814 real constant from the reg_equal expression, so get the real
42815 constant. */
42816 if (GET_CODE (const_vector) == SYMBOL_REF)
42817 const_vector = get_pool_constant (const_vector);
42818 gcc_assert (GET_CODE (const_vector) == CONST_VECTOR);
42820 /* Create an adjusted mask from the initial mask. */
42821 unsigned int new_mask[16], i, val;
42822 for (i = 0; i < 16; ++i) {
42823 val = INTVAL (XVECEXP (const_vector, 0, i));
42824 if (val < 16)
42825 new_mask[i] = (val + 8) % 16;
42826 else
42827 new_mask[i] = ((val + 8) % 16) + 16;
42830 /* Create a new CONST_VECTOR and a MEM that references it. */
42831 rtx vals = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
42832 for (i = 0; i < 16; ++i)
42833 XVECEXP (vals, 0, i) = GEN_INT (new_mask[i]);
42834 rtx new_const_vector = gen_rtx_CONST_VECTOR (V16QImode, XVEC (vals, 0));
42835 rtx new_mem = force_const_mem (V16QImode, new_const_vector);
42836 /* This gives us a MEM whose base operand is a SYMBOL_REF, which we
42837 can't recognize. Force the SYMBOL_REF into a register. */
42838 if (!REG_P (XEXP (new_mem, 0))) {
42839 rtx base_reg = force_reg (Pmode, XEXP (new_mem, 0));
42840 XEXP (new_mem, 0) = base_reg;
42841 /* Move the newly created insn ahead of the load insn. */
42842 rtx_insn *force_insn = get_last_insn ();
42843 remove_insn (force_insn);
42844 rtx_insn *before_load_insn = PREV_INSN (load_insn);
42845 add_insn_after (force_insn, before_load_insn, BLOCK_FOR_INSN (load_insn));
42846 df_insn_rescan (before_load_insn);
42847 df_insn_rescan (force_insn);
42850 /* Replace the MEM in the load instruction and rescan it. */
42851 XEXP (SET_SRC (PATTERN (load_insn)), 0) = new_mem;
42852 INSN_CODE (load_insn) = -1; /* Force re-recognition. */
42853 df_insn_rescan (load_insn);
42855 if (dump_file)
42856 fprintf (dump_file, "Adjusting mask for vperm %d\n", INSN_UID (insn));
42859 /* The insn described by INSN_ENTRY[I] can be swapped, but only
42860 with special handling. Take care of that here. */
42861 static void
42862 handle_special_swappables (swap_web_entry *insn_entry, unsigned i)
42864 rtx_insn *insn = insn_entry[i].insn;
42865 rtx body = PATTERN (insn);
42867 switch (insn_entry[i].special_handling)
42869 default:
42870 gcc_unreachable ();
42871 case SH_CONST_VECTOR:
42873 /* A CONST_VECTOR will only show up somewhere in the RHS of a SET. */
42874 gcc_assert (GET_CODE (body) == SET);
42875 rtx rhs = SET_SRC (body);
42876 swap_const_vector_halves (rhs);
42877 if (dump_file)
42878 fprintf (dump_file, "Swapping constant halves in insn %d\n", i);
42879 break;
42881 case SH_SUBREG:
42882 /* A subreg of the same size is already safe. For subregs that
42883 select a smaller portion of a reg, adjust the index for
42884 swapped doublewords. */
42885 adjust_subreg_index (body);
42886 if (dump_file)
42887 fprintf (dump_file, "Adjusting subreg in insn %d\n", i);
42888 break;
42889 case SH_NOSWAP_LD:
42890 /* Convert a non-permuting load to a permuting one. */
42891 permute_load (insn);
42892 break;
42893 case SH_NOSWAP_ST:
42894 /* Convert a non-permuting store to a permuting one. */
42895 permute_store (insn);
42896 break;
42897 case SH_EXTRACT:
42898 /* Change the lane on an extract operation. */
42899 adjust_extract (insn);
42900 break;
42901 case SH_SPLAT:
42902 /* Change the lane on a direct-splat operation. */
42903 adjust_splat (insn);
42904 break;
42905 case SH_XXPERMDI:
42906 /* Change the lanes on an XXPERMDI operation. */
42907 adjust_xxpermdi (insn);
42908 break;
42909 case SH_CONCAT:
42910 /* Reverse the order of a concatenation operation. */
42911 adjust_concat (insn);
42912 break;
42913 case SH_VPERM:
42914 /* Change the mask loaded from the constant pool for a VPERM. */
42915 adjust_vperm (insn);
42916 break;
42920 /* Find the insn from the Ith table entry, which is known to be a
42921 register swap Y = SWAP(X). Replace it with a copy Y = X. */
42922 static void
42923 replace_swap_with_copy (swap_web_entry *insn_entry, unsigned i)
42925 rtx_insn *insn = insn_entry[i].insn;
42926 rtx body = PATTERN (insn);
42927 rtx src_reg = XEXP (SET_SRC (body), 0);
42928 rtx copy = gen_rtx_SET (SET_DEST (body), src_reg);
42929 rtx_insn *new_insn = emit_insn_before (copy, insn);
42930 set_block_for_insn (new_insn, BLOCK_FOR_INSN (insn));
42931 df_insn_rescan (new_insn);
42933 if (dump_file)
42935 unsigned int new_uid = INSN_UID (new_insn);
42936 fprintf (dump_file, "Replacing swap %d with copy %d\n", i, new_uid);
42939 df_insn_delete (insn);
42940 remove_insn (insn);
42941 insn->set_deleted ();
42944 /* Dump the swap table to DUMP_FILE. */
42945 static void
42946 dump_swap_insn_table (swap_web_entry *insn_entry)
42948 int e = get_max_uid ();
42949 fprintf (dump_file, "\nRelevant insns with their flag settings\n\n");
42951 for (int i = 0; i < e; ++i)
42952 if (insn_entry[i].is_relevant)
42954 swap_web_entry *pred_entry = (swap_web_entry *)insn_entry[i].pred ();
42955 fprintf (dump_file, "%6d %6d ", i,
42956 pred_entry && pred_entry->insn
42957 ? INSN_UID (pred_entry->insn) : 0);
42958 if (insn_entry[i].is_load)
42959 fputs ("load ", dump_file);
42960 if (insn_entry[i].is_store)
42961 fputs ("store ", dump_file);
42962 if (insn_entry[i].is_swap)
42963 fputs ("swap ", dump_file);
42964 if (insn_entry[i].is_live_in)
42965 fputs ("live-in ", dump_file);
42966 if (insn_entry[i].is_live_out)
42967 fputs ("live-out ", dump_file);
42968 if (insn_entry[i].contains_subreg)
42969 fputs ("subreg ", dump_file);
42970 if (insn_entry[i].is_128_int)
42971 fputs ("int128 ", dump_file);
42972 if (insn_entry[i].is_call)
42973 fputs ("call ", dump_file);
42974 if (insn_entry[i].is_swappable)
42976 fputs ("swappable ", dump_file);
42977 if (insn_entry[i].special_handling == SH_CONST_VECTOR)
42978 fputs ("special:constvec ", dump_file);
42979 else if (insn_entry[i].special_handling == SH_SUBREG)
42980 fputs ("special:subreg ", dump_file);
42981 else if (insn_entry[i].special_handling == SH_NOSWAP_LD)
42982 fputs ("special:load ", dump_file);
42983 else if (insn_entry[i].special_handling == SH_NOSWAP_ST)
42984 fputs ("special:store ", dump_file);
42985 else if (insn_entry[i].special_handling == SH_EXTRACT)
42986 fputs ("special:extract ", dump_file);
42987 else if (insn_entry[i].special_handling == SH_SPLAT)
42988 fputs ("special:splat ", dump_file);
42989 else if (insn_entry[i].special_handling == SH_XXPERMDI)
42990 fputs ("special:xxpermdi ", dump_file);
42991 else if (insn_entry[i].special_handling == SH_CONCAT)
42992 fputs ("special:concat ", dump_file);
42993 else if (insn_entry[i].special_handling == SH_VPERM)
42994 fputs ("special:vperm ", dump_file);
42996 if (insn_entry[i].web_not_optimizable)
42997 fputs ("unoptimizable ", dump_file);
42998 if (insn_entry[i].will_delete)
42999 fputs ("delete ", dump_file);
43000 fputs ("\n", dump_file);
43002 fputs ("\n", dump_file);
43005 /* Return RTX with its address canonicalized to (reg) or (+ reg reg).
43006 Here RTX is an (& addr (const_int -16)). Always return a new copy
43007 to avoid problems with combine. */
43008 static rtx
43009 alignment_with_canonical_addr (rtx align)
43011 rtx canon;
43012 rtx addr = XEXP (align, 0);
43014 if (REG_P (addr))
43015 canon = addr;
43017 else if (GET_CODE (addr) == PLUS)
43019 rtx addrop0 = XEXP (addr, 0);
43020 rtx addrop1 = XEXP (addr, 1);
43022 if (!REG_P (addrop0))
43023 addrop0 = force_reg (GET_MODE (addrop0), addrop0);
43025 if (!REG_P (addrop1))
43026 addrop1 = force_reg (GET_MODE (addrop1), addrop1);
43028 canon = gen_rtx_PLUS (GET_MODE (addr), addrop0, addrop1);
43031 else
43032 canon = force_reg (GET_MODE (addr), addr);
43034 return gen_rtx_AND (GET_MODE (align), canon, GEN_INT (-16));
43037 /* Check whether an rtx is an alignment mask, and if so, return
43038 a fully-expanded rtx for the masking operation. */
43039 static rtx
43040 alignment_mask (rtx_insn *insn)
43042 rtx body = PATTERN (insn);
43044 if (GET_CODE (body) != SET
43045 || GET_CODE (SET_SRC (body)) != AND
43046 || !REG_P (XEXP (SET_SRC (body), 0)))
43047 return 0;
43049 rtx mask = XEXP (SET_SRC (body), 1);
43051 if (GET_CODE (mask) == CONST_INT)
43053 if (INTVAL (mask) == -16)
43054 return alignment_with_canonical_addr (SET_SRC (body));
43055 else
43056 return 0;
43059 if (!REG_P (mask))
43060 return 0;
43062 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43063 df_ref use;
43064 rtx real_mask = 0;
43066 FOR_EACH_INSN_INFO_USE (use, insn_info)
43068 if (!rtx_equal_p (DF_REF_REG (use), mask))
43069 continue;
43071 struct df_link *def_link = DF_REF_CHAIN (use);
43072 if (!def_link || def_link->next)
43073 return 0;
43075 rtx_insn *const_insn = DF_REF_INSN (def_link->ref);
43076 rtx const_body = PATTERN (const_insn);
43077 if (GET_CODE (const_body) != SET)
43078 return 0;
43080 real_mask = SET_SRC (const_body);
43082 if (GET_CODE (real_mask) != CONST_INT
43083 || INTVAL (real_mask) != -16)
43084 return 0;
43087 if (real_mask == 0)
43088 return 0;
43090 return alignment_with_canonical_addr (SET_SRC (body));
43093 /* Given INSN that's a load or store based at BASE_REG, look for a
43094 feeding computation that aligns its address on a 16-byte boundary. */
43095 static rtx
43096 find_alignment_op (rtx_insn *insn, rtx base_reg)
43098 df_ref base_use;
43099 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43100 rtx and_operation = 0;
43102 FOR_EACH_INSN_INFO_USE (base_use, insn_info)
43104 if (!rtx_equal_p (DF_REF_REG (base_use), base_reg))
43105 continue;
43107 struct df_link *base_def_link = DF_REF_CHAIN (base_use);
43108 if (!base_def_link || base_def_link->next)
43109 break;
43111 /* With stack-protector code enabled, and possibly in other
43112 circumstances, there may not be an associated insn for
43113 the def. */
43114 if (DF_REF_IS_ARTIFICIAL (base_def_link->ref))
43115 break;
43117 rtx_insn *and_insn = DF_REF_INSN (base_def_link->ref);
43118 and_operation = alignment_mask (and_insn);
43119 if (and_operation != 0)
43120 break;
43123 return and_operation;
43126 struct del_info { bool replace; rtx_insn *replace_insn; };
43128 /* If INSN is the load for an lvx pattern, put it in canonical form. */
43129 static void
43130 recombine_lvx_pattern (rtx_insn *insn, del_info *to_delete)
43132 rtx body = PATTERN (insn);
43133 gcc_assert (GET_CODE (body) == SET
43134 && GET_CODE (SET_SRC (body)) == VEC_SELECT
43135 && GET_CODE (XEXP (SET_SRC (body), 0)) == MEM);
43137 rtx mem = XEXP (SET_SRC (body), 0);
43138 rtx base_reg = XEXP (mem, 0);
43140 rtx and_operation = find_alignment_op (insn, base_reg);
43142 if (and_operation != 0)
43144 df_ref def;
43145 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43146 FOR_EACH_INSN_INFO_DEF (def, insn_info)
43148 struct df_link *link = DF_REF_CHAIN (def);
43149 if (!link || link->next)
43150 break;
43152 rtx_insn *swap_insn = DF_REF_INSN (link->ref);
43153 if (!insn_is_swap_p (swap_insn)
43154 || insn_is_load_p (swap_insn)
43155 || insn_is_store_p (swap_insn))
43156 break;
43158 /* Expected lvx pattern found. Change the swap to
43159 a copy, and propagate the AND operation into the
43160 load. */
43161 to_delete[INSN_UID (swap_insn)].replace = true;
43162 to_delete[INSN_UID (swap_insn)].replace_insn = swap_insn;
43164 XEXP (mem, 0) = and_operation;
43165 SET_SRC (body) = mem;
43166 INSN_CODE (insn) = -1; /* Force re-recognition. */
43167 df_insn_rescan (insn);
43169 if (dump_file)
43170 fprintf (dump_file, "lvx opportunity found at %d\n",
43171 INSN_UID (insn));
43176 /* If INSN is the store for an stvx pattern, put it in canonical form. */
43177 static void
43178 recombine_stvx_pattern (rtx_insn *insn, del_info *to_delete)
43180 rtx body = PATTERN (insn);
43181 gcc_assert (GET_CODE (body) == SET
43182 && GET_CODE (SET_DEST (body)) == MEM
43183 && GET_CODE (SET_SRC (body)) == VEC_SELECT);
43184 rtx mem = SET_DEST (body);
43185 rtx base_reg = XEXP (mem, 0);
43187 rtx and_operation = find_alignment_op (insn, base_reg);
43189 if (and_operation != 0)
43191 rtx src_reg = XEXP (SET_SRC (body), 0);
43192 df_ref src_use;
43193 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43194 FOR_EACH_INSN_INFO_USE (src_use, insn_info)
43196 if (!rtx_equal_p (DF_REF_REG (src_use), src_reg))
43197 continue;
43199 struct df_link *link = DF_REF_CHAIN (src_use);
43200 if (!link || link->next)
43201 break;
43203 rtx_insn *swap_insn = DF_REF_INSN (link->ref);
43204 if (!insn_is_swap_p (swap_insn)
43205 || insn_is_load_p (swap_insn)
43206 || insn_is_store_p (swap_insn))
43207 break;
43209 /* Expected stvx pattern found. Change the swap to
43210 a copy, and propagate the AND operation into the
43211 store. */
43212 to_delete[INSN_UID (swap_insn)].replace = true;
43213 to_delete[INSN_UID (swap_insn)].replace_insn = swap_insn;
43215 XEXP (mem, 0) = and_operation;
43216 SET_SRC (body) = src_reg;
43217 INSN_CODE (insn) = -1; /* Force re-recognition. */
43218 df_insn_rescan (insn);
43220 if (dump_file)
43221 fprintf (dump_file, "stvx opportunity found at %d\n",
43222 INSN_UID (insn));
43227 /* Look for patterns created from builtin lvx and stvx calls, and
43228 canonicalize them to be properly recognized as such. */
43229 static void
43230 recombine_lvx_stvx_patterns (function *fun)
43232 int i;
43233 basic_block bb;
43234 rtx_insn *insn;
43236 int num_insns = get_max_uid ();
43237 del_info *to_delete = XCNEWVEC (del_info, num_insns);
43239 FOR_ALL_BB_FN (bb, fun)
43240 FOR_BB_INSNS (bb, insn)
43242 if (!NONDEBUG_INSN_P (insn))
43243 continue;
43245 if (insn_is_load_p (insn) && insn_is_swap_p (insn))
43246 recombine_lvx_pattern (insn, to_delete);
43247 else if (insn_is_store_p (insn) && insn_is_swap_p (insn))
43248 recombine_stvx_pattern (insn, to_delete);
43251 /* Turning swaps into copies is delayed until now, to avoid problems
43252 with deleting instructions during the insn walk. */
43253 for (i = 0; i < num_insns; i++)
43254 if (to_delete[i].replace)
43256 rtx swap_body = PATTERN (to_delete[i].replace_insn);
43257 rtx src_reg = XEXP (SET_SRC (swap_body), 0);
43258 rtx copy = gen_rtx_SET (SET_DEST (swap_body), src_reg);
43259 rtx_insn *new_insn = emit_insn_before (copy,
43260 to_delete[i].replace_insn);
43261 set_block_for_insn (new_insn,
43262 BLOCK_FOR_INSN (to_delete[i].replace_insn));
43263 df_insn_rescan (new_insn);
43264 df_insn_delete (to_delete[i].replace_insn);
43265 remove_insn (to_delete[i].replace_insn);
43266 to_delete[i].replace_insn->set_deleted ();
43269 free (to_delete);
43272 /* Main entry point for this pass. */
43273 unsigned int
43274 rs6000_analyze_swaps (function *fun)
43276 swap_web_entry *insn_entry;
43277 basic_block bb;
43278 rtx_insn *insn, *curr_insn = 0;
43280 /* Dataflow analysis for use-def chains. */
43281 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
43282 df_chain_add_problem (DF_DU_CHAIN | DF_UD_CHAIN);
43283 df_analyze ();
43284 df_set_flags (DF_DEFER_INSN_RESCAN);
43286 /* Pre-pass to recombine lvx and stvx patterns so we don't lose info. */
43287 recombine_lvx_stvx_patterns (fun);
43289 /* Allocate structure to represent webs of insns. */
43290 insn_entry = XCNEWVEC (swap_web_entry, get_max_uid ());
43292 /* Walk the insns to gather basic data. */
43293 FOR_ALL_BB_FN (bb, fun)
43294 FOR_BB_INSNS_SAFE (bb, insn, curr_insn)
43296 unsigned int uid = INSN_UID (insn);
43297 if (NONDEBUG_INSN_P (insn))
43299 insn_entry[uid].insn = insn;
43301 if (GET_CODE (insn) == CALL_INSN)
43302 insn_entry[uid].is_call = 1;
43304 /* Walk the uses and defs to see if we mention vector regs.
43305 Record any constraints on optimization of such mentions. */
43306 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43307 df_ref mention;
43308 FOR_EACH_INSN_INFO_USE (mention, insn_info)
43310 /* We use DF_REF_REAL_REG here to get inside any subregs. */
43311 machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
43313 /* If a use gets its value from a call insn, it will be
43314 a hard register and will look like (reg:V4SI 3 3).
43315 The df analysis creates two mentions for GPR3 and GPR4,
43316 both DImode. We must recognize this and treat it as a
43317 vector mention to ensure the call is unioned with this
43318 use. */
43319 if (mode == DImode && DF_REF_INSN_INFO (mention))
43321 rtx feeder = DF_REF_INSN (mention);
43322 /* FIXME: It is pretty hard to get from the df mention
43323 to the mode of the use in the insn. We arbitrarily
43324 pick a vector mode here, even though the use might
43325 be a real DImode. We can be too conservative
43326 (create a web larger than necessary) because of
43327 this, so consider eventually fixing this. */
43328 if (GET_CODE (feeder) == CALL_INSN)
43329 mode = V4SImode;
43332 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode) || mode == TImode)
43334 insn_entry[uid].is_relevant = 1;
43335 if (mode == TImode || mode == V1TImode
43336 || FLOAT128_VECTOR_P (mode))
43337 insn_entry[uid].is_128_int = 1;
43338 if (DF_REF_INSN_INFO (mention))
43339 insn_entry[uid].contains_subreg
43340 = !rtx_equal_p (DF_REF_REG (mention),
43341 DF_REF_REAL_REG (mention));
43342 union_defs (insn_entry, insn, mention);
43345 FOR_EACH_INSN_INFO_DEF (mention, insn_info)
43347 /* We use DF_REF_REAL_REG here to get inside any subregs. */
43348 machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
43350 /* If we're loading up a hard vector register for a call,
43351 it looks like (set (reg:V4SI 9 9) (...)). The df
43352 analysis creates two mentions for GPR9 and GPR10, both
43353 DImode. So relying on the mode from the mentions
43354 isn't sufficient to ensure we union the call into the
43355 web with the parameter setup code. */
43356 if (mode == DImode && GET_CODE (insn) == SET
43357 && ALTIVEC_OR_VSX_VECTOR_MODE (GET_MODE (SET_DEST (insn))))
43358 mode = GET_MODE (SET_DEST (insn));
43360 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode) || mode == TImode)
43362 insn_entry[uid].is_relevant = 1;
43363 if (mode == TImode || mode == V1TImode
43364 || FLOAT128_VECTOR_P (mode))
43365 insn_entry[uid].is_128_int = 1;
43366 if (DF_REF_INSN_INFO (mention))
43367 insn_entry[uid].contains_subreg
43368 = !rtx_equal_p (DF_REF_REG (mention),
43369 DF_REF_REAL_REG (mention));
43370 /* REG_FUNCTION_VALUE_P is not valid for subregs. */
43371 else if (REG_FUNCTION_VALUE_P (DF_REF_REG (mention)))
43372 insn_entry[uid].is_live_out = 1;
43373 union_uses (insn_entry, insn, mention);
43377 if (insn_entry[uid].is_relevant)
43379 /* Determine if this is a load or store. */
43380 insn_entry[uid].is_load = insn_is_load_p (insn);
43381 insn_entry[uid].is_store = insn_is_store_p (insn);
43383 /* Determine if this is a doubleword swap. If not,
43384 determine whether it can legally be swapped. */
43385 if (insn_is_swap_p (insn))
43386 insn_entry[uid].is_swap = 1;
43387 else
43389 unsigned int special = SH_NONE;
43390 insn_entry[uid].is_swappable
43391 = insn_is_swappable_p (insn_entry, insn, &special);
43392 if (special != SH_NONE && insn_entry[uid].contains_subreg)
43393 insn_entry[uid].is_swappable = 0;
43394 else if (special != SH_NONE)
43395 insn_entry[uid].special_handling = special;
43396 else if (insn_entry[uid].contains_subreg)
43397 insn_entry[uid].special_handling = SH_SUBREG;
43403 if (dump_file)
43405 fprintf (dump_file, "\nSwap insn entry table when first built\n");
43406 dump_swap_insn_table (insn_entry);
43409 /* Record unoptimizable webs. */
43410 unsigned e = get_max_uid (), i;
43411 for (i = 0; i < e; ++i)
43413 if (!insn_entry[i].is_relevant)
43414 continue;
43416 swap_web_entry *root
43417 = (swap_web_entry*)(&insn_entry[i])->unionfind_root ();
43419 if (insn_entry[i].is_live_in || insn_entry[i].is_live_out
43420 || (insn_entry[i].contains_subreg
43421 && insn_entry[i].special_handling != SH_SUBREG)
43422 || insn_entry[i].is_128_int || insn_entry[i].is_call
43423 || !(insn_entry[i].is_swappable || insn_entry[i].is_swap))
43424 root->web_not_optimizable = 1;
43426 /* If we have loads or stores that aren't permuting then the
43427 optimization isn't appropriate. */
43428 else if ((insn_entry[i].is_load || insn_entry[i].is_store)
43429 && !insn_entry[i].is_swap && !insn_entry[i].is_swappable)
43430 root->web_not_optimizable = 1;
43432 /* If we have permuting loads or stores that are not accompanied
43433 by a register swap, the optimization isn't appropriate. */
43434 else if (insn_entry[i].is_load && insn_entry[i].is_swap)
43436 rtx insn = insn_entry[i].insn;
43437 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43438 df_ref def;
43440 FOR_EACH_INSN_INFO_DEF (def, insn_info)
43442 struct df_link *link = DF_REF_CHAIN (def);
43444 if (!chain_contains_only_swaps (insn_entry, link, FOR_LOADS))
43446 root->web_not_optimizable = 1;
43447 break;
43451 else if (insn_entry[i].is_store && insn_entry[i].is_swap)
43453 rtx insn = insn_entry[i].insn;
43454 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
43455 df_ref use;
43457 FOR_EACH_INSN_INFO_USE (use, insn_info)
43459 struct df_link *link = DF_REF_CHAIN (use);
43461 if (!chain_contains_only_swaps (insn_entry, link, FOR_STORES))
43463 root->web_not_optimizable = 1;
43464 break;
43470 if (dump_file)
43472 fprintf (dump_file, "\nSwap insn entry table after web analysis\n");
43473 dump_swap_insn_table (insn_entry);
43476 /* For each load and store in an optimizable web (which implies
43477 the loads and stores are permuting), find the associated
43478 register swaps and mark them for removal. Due to various
43479 optimizations we may mark the same swap more than once. Also
43480 perform special handling for swappable insns that require it. */
43481 for (i = 0; i < e; ++i)
43482 if ((insn_entry[i].is_load || insn_entry[i].is_store)
43483 && insn_entry[i].is_swap)
43485 swap_web_entry* root_entry
43486 = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
43487 if (!root_entry->web_not_optimizable)
43488 mark_swaps_for_removal (insn_entry, i);
43490 else if (insn_entry[i].is_swappable && insn_entry[i].special_handling)
43492 swap_web_entry* root_entry
43493 = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
43494 if (!root_entry->web_not_optimizable)
43495 handle_special_swappables (insn_entry, i);
43498 /* Now delete the swaps marked for removal. */
43499 for (i = 0; i < e; ++i)
43500 if (insn_entry[i].will_delete)
43501 replace_swap_with_copy (insn_entry, i);
43503 /* Clean up. */
43504 free (insn_entry);
43505 return 0;
43508 const pass_data pass_data_analyze_swaps =
43510 RTL_PASS, /* type */
43511 "swaps", /* name */
43512 OPTGROUP_NONE, /* optinfo_flags */
43513 TV_NONE, /* tv_id */
43514 0, /* properties_required */
43515 0, /* properties_provided */
43516 0, /* properties_destroyed */
43517 0, /* todo_flags_start */
43518 TODO_df_finish, /* todo_flags_finish */
43521 class pass_analyze_swaps : public rtl_opt_pass
43523 public:
43524 pass_analyze_swaps(gcc::context *ctxt)
43525 : rtl_opt_pass(pass_data_analyze_swaps, ctxt)
43528 /* opt_pass methods: */
43529 virtual bool gate (function *)
43531 return (optimize > 0 && !BYTES_BIG_ENDIAN && TARGET_VSX
43532 && !TARGET_P9_VECTOR && rs6000_optimize_swaps);
43535 virtual unsigned int execute (function *fun)
43537 return rs6000_analyze_swaps (fun);
43540 opt_pass *clone ()
43542 return new pass_analyze_swaps (m_ctxt);
43545 }; // class pass_analyze_swaps
43547 rtl_opt_pass *
43548 make_pass_analyze_swaps (gcc::context *ctxt)
43550 return new pass_analyze_swaps (ctxt);
43553 #ifdef RS6000_GLIBC_ATOMIC_FENV
43554 /* Function declarations for rs6000_atomic_assign_expand_fenv. */
43555 static tree atomic_hold_decl, atomic_clear_decl, atomic_update_decl;
43556 #endif
43558 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
43560 static void
43561 rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
43563 if (!TARGET_HARD_FLOAT || !TARGET_FPRS)
43565 #ifdef RS6000_GLIBC_ATOMIC_FENV
43566 if (atomic_hold_decl == NULL_TREE)
43568 atomic_hold_decl
43569 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
43570 get_identifier ("__atomic_feholdexcept"),
43571 build_function_type_list (void_type_node,
43572 double_ptr_type_node,
43573 NULL_TREE));
43574 TREE_PUBLIC (atomic_hold_decl) = 1;
43575 DECL_EXTERNAL (atomic_hold_decl) = 1;
43578 if (atomic_clear_decl == NULL_TREE)
43580 atomic_clear_decl
43581 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
43582 get_identifier ("__atomic_feclearexcept"),
43583 build_function_type_list (void_type_node,
43584 NULL_TREE));
43585 TREE_PUBLIC (atomic_clear_decl) = 1;
43586 DECL_EXTERNAL (atomic_clear_decl) = 1;
43589 tree const_double = build_qualified_type (double_type_node,
43590 TYPE_QUAL_CONST);
43591 tree const_double_ptr = build_pointer_type (const_double);
43592 if (atomic_update_decl == NULL_TREE)
43594 atomic_update_decl
43595 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
43596 get_identifier ("__atomic_feupdateenv"),
43597 build_function_type_list (void_type_node,
43598 const_double_ptr,
43599 NULL_TREE));
43600 TREE_PUBLIC (atomic_update_decl) = 1;
43601 DECL_EXTERNAL (atomic_update_decl) = 1;
43604 tree fenv_var = create_tmp_var_raw (double_type_node);
43605 TREE_ADDRESSABLE (fenv_var) = 1;
43606 tree fenv_addr = build1 (ADDR_EXPR, double_ptr_type_node, fenv_var);
43608 *hold = build_call_expr (atomic_hold_decl, 1, fenv_addr);
43609 *clear = build_call_expr (atomic_clear_decl, 0);
43610 *update = build_call_expr (atomic_update_decl, 1,
43611 fold_convert (const_double_ptr, fenv_addr));
43612 #endif
43613 return;
43616 tree mffs = rs6000_builtin_decls[RS6000_BUILTIN_MFFS];
43617 tree mtfsf = rs6000_builtin_decls[RS6000_BUILTIN_MTFSF];
43618 tree call_mffs = build_call_expr (mffs, 0);
43620 /* Generates the equivalent of feholdexcept (&fenv_var)
43622 *fenv_var = __builtin_mffs ();
43623 double fenv_hold;
43624 *(uint64_t*)&fenv_hold = *(uint64_t*)fenv_var & 0xffffffff00000007LL;
43625 __builtin_mtfsf (0xff, fenv_hold); */
43627 /* Mask to clear everything except for the rounding modes and non-IEEE
43628 arithmetic flag. */
43629 const unsigned HOST_WIDE_INT hold_exception_mask =
43630 HOST_WIDE_INT_C (0xffffffff00000007);
43632 tree fenv_var = create_tmp_var_raw (double_type_node);
43634 tree hold_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_var, call_mffs);
43636 tree fenv_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_var);
43637 tree fenv_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
43638 build_int_cst (uint64_type_node,
43639 hold_exception_mask));
43641 tree fenv_hold_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
43642 fenv_llu_and);
43644 tree hold_mtfsf = build_call_expr (mtfsf, 2,
43645 build_int_cst (unsigned_type_node, 0xff),
43646 fenv_hold_mtfsf);
43648 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_mffs, hold_mtfsf);
43650 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT):
43652 double fenv_clear = __builtin_mffs ();
43653 *(uint64_t)&fenv_clear &= 0xffffffff00000000LL;
43654 __builtin_mtfsf (0xff, fenv_clear); */
43656 /* Mask to clear everything except for the rounding modes and non-IEEE
43657 arithmetic flag. */
43658 const unsigned HOST_WIDE_INT clear_exception_mask =
43659 HOST_WIDE_INT_C (0xffffffff00000000);
43661 tree fenv_clear = create_tmp_var_raw (double_type_node);
43663 tree clear_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_clear, call_mffs);
43665 tree fenv_clean_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_clear);
43666 tree fenv_clear_llu_and = build2 (BIT_AND_EXPR, uint64_type_node,
43667 fenv_clean_llu,
43668 build_int_cst (uint64_type_node,
43669 clear_exception_mask));
43671 tree fenv_clear_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
43672 fenv_clear_llu_and);
43674 tree clear_mtfsf = build_call_expr (mtfsf, 2,
43675 build_int_cst (unsigned_type_node, 0xff),
43676 fenv_clear_mtfsf);
43678 *clear = build2 (COMPOUND_EXPR, void_type_node, clear_mffs, clear_mtfsf);
43680 /* Generates the equivalent of feupdateenv (&fenv_var)
43682 double old_fenv = __builtin_mffs ();
43683 double fenv_update;
43684 *(uint64_t*)&fenv_update = (*(uint64_t*)&old & 0xffffffff1fffff00LL) |
43685 (*(uint64_t*)fenv_var 0x1ff80fff);
43686 __builtin_mtfsf (0xff, fenv_update); */
43688 const unsigned HOST_WIDE_INT update_exception_mask =
43689 HOST_WIDE_INT_C (0xffffffff1fffff00);
43690 const unsigned HOST_WIDE_INT new_exception_mask =
43691 HOST_WIDE_INT_C (0x1ff80fff);
43693 tree old_fenv = create_tmp_var_raw (double_type_node);
43694 tree update_mffs = build2 (MODIFY_EXPR, void_type_node, old_fenv, call_mffs);
43696 tree old_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, old_fenv);
43697 tree old_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, old_llu,
43698 build_int_cst (uint64_type_node,
43699 update_exception_mask));
43701 tree new_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
43702 build_int_cst (uint64_type_node,
43703 new_exception_mask));
43705 tree new_llu_mask = build2 (BIT_IOR_EXPR, uint64_type_node,
43706 old_llu_and, new_llu_and);
43708 tree fenv_update_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
43709 new_llu_mask);
43711 tree update_mtfsf = build_call_expr (mtfsf, 2,
43712 build_int_cst (unsigned_type_node, 0xff),
43713 fenv_update_mtfsf);
43715 *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf);
43718 /* Implement the TARGET_OPTAB_SUPPORTED_P hook. */
43720 static bool
43721 rs6000_optab_supported_p (int op, machine_mode mode1, machine_mode,
43722 optimization_type opt_type)
43724 switch (op)
43726 case rsqrt_optab:
43727 return (opt_type == OPTIMIZE_FOR_SPEED
43728 && RS6000_RECIP_AUTO_RSQRTE_P (mode1));
43730 default:
43731 return true;
43735 struct gcc_target targetm = TARGET_INITIALIZER;
43737 #include "gt-powerpcspe.h"