[15/77] Add scalar_int_mode
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blob5086a7a785e27acfcc9845f9b29a3a59e3099583
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 "expmed.h"
35 #include "optabs.h"
36 #include "regs.h"
37 #include "ira.h"
38 #include "recog.h"
39 #include "cgraph.h"
40 #include "diagnostic-core.h"
41 #include "insn-attr.h"
42 #include "flags.h"
43 #include "alias.h"
44 #include "fold-const.h"
45 #include "attribs.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"
80 #include "tree-ssa-propagate.h"
82 /* This file should be included last. */
83 #include "target-def.h"
85 #ifndef TARGET_NO_PROTOTYPE
86 #define TARGET_NO_PROTOTYPE 0
87 #endif
89 #define min(A,B) ((A) < (B) ? (A) : (B))
90 #define max(A,B) ((A) > (B) ? (A) : (B))
92 /* Structure used to define the rs6000 stack */
93 typedef struct rs6000_stack {
94 int reload_completed; /* stack info won't change from here on */
95 int first_gp_reg_save; /* first callee saved GP register used */
96 int first_fp_reg_save; /* first callee saved FP register used */
97 int first_altivec_reg_save; /* first callee saved AltiVec register used */
98 int lr_save_p; /* true if the link reg needs to be saved */
99 int cr_save_p; /* true if the CR reg needs to be saved */
100 unsigned int vrsave_mask; /* mask of vec registers to save */
101 int push_p; /* true if we need to allocate stack space */
102 int calls_p; /* true if the function makes any calls */
103 int world_save_p; /* true if we're saving *everything*:
104 r13-r31, cr, f14-f31, vrsave, v20-v31 */
105 enum rs6000_abi abi; /* which ABI to use */
106 int gp_save_offset; /* offset to save GP regs from initial SP */
107 int fp_save_offset; /* offset to save FP regs from initial SP */
108 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
109 int lr_save_offset; /* offset to save LR from initial SP */
110 int cr_save_offset; /* offset to save CR from initial SP */
111 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
112 int varargs_save_offset; /* offset to save the varargs registers */
113 int ehrd_offset; /* offset to EH return data */
114 int ehcr_offset; /* offset to EH CR field data */
115 int reg_size; /* register size (4 or 8) */
116 HOST_WIDE_INT vars_size; /* variable save area size */
117 int parm_size; /* outgoing parameter size */
118 int save_size; /* save area size */
119 int fixed_size; /* fixed size of stack frame */
120 int gp_size; /* size of saved GP registers */
121 int fp_size; /* size of saved FP registers */
122 int altivec_size; /* size of saved AltiVec registers */
123 int cr_size; /* size to hold CR if not in fixed area */
124 int vrsave_size; /* size to hold VRSAVE */
125 int altivec_padding_size; /* size of altivec alignment padding */
126 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
127 int savres_strategy;
128 } rs6000_stack_t;
130 /* A C structure for machine-specific, per-function data.
131 This is added to the cfun structure. */
132 typedef struct GTY(()) machine_function
134 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
135 int ra_needs_full_frame;
136 /* Flags if __builtin_return_address (0) was used. */
137 int ra_need_lr;
138 /* Cache lr_save_p after expansion of builtin_eh_return. */
139 int lr_save_state;
140 /* Whether we need to save the TOC to the reserved stack location in the
141 function prologue. */
142 bool save_toc_in_prologue;
143 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
144 varargs save area. */
145 HOST_WIDE_INT varargs_save_offset;
146 /* Alternative internal arg pointer for -fsplit-stack. */
147 rtx split_stack_arg_pointer;
148 bool split_stack_argp_used;
149 /* Flag if r2 setup is needed with ELFv2 ABI. */
150 bool r2_setup_needed;
151 /* The number of components we use for separate shrink-wrapping. */
152 int n_components;
153 /* The components already handled by separate shrink-wrapping, which should
154 not be considered by the prologue and epilogue. */
155 bool gpr_is_wrapped_separately[32];
156 bool fpr_is_wrapped_separately[32];
157 bool lr_is_wrapped_separately;
158 } machine_function;
160 /* Support targetm.vectorize.builtin_mask_for_load. */
161 static GTY(()) tree altivec_builtin_mask_for_load;
163 /* Set to nonzero once AIX common-mode calls have been defined. */
164 static GTY(()) int common_mode_defined;
166 /* Label number of label created for -mrelocatable, to call to so we can
167 get the address of the GOT section */
168 static int rs6000_pic_labelno;
170 #ifdef USING_ELFOS_H
171 /* Counter for labels which are to be placed in .fixup. */
172 int fixuplabelno = 0;
173 #endif
175 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
176 int dot_symbols;
178 /* Specify the machine mode that pointers have. After generation of rtl, the
179 compiler makes no further distinction between pointers and any other objects
180 of this machine mode. */
181 scalar_int_mode rs6000_pmode;
183 /* Width in bits of a pointer. */
184 unsigned rs6000_pointer_size;
186 #ifdef HAVE_AS_GNU_ATTRIBUTE
187 # ifndef HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE
188 # define HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE 0
189 # endif
190 /* Flag whether floating point values have been passed/returned.
191 Note that this doesn't say whether fprs are used, since the
192 Tag_GNU_Power_ABI_FP .gnu.attributes value this flag controls
193 should be set for soft-float values passed in gprs and ieee128
194 values passed in vsx registers. */
195 static bool rs6000_passes_float;
196 static bool rs6000_passes_long_double;
197 /* Flag whether vector values have been passed/returned. */
198 static bool rs6000_passes_vector;
199 /* Flag whether small (<= 8 byte) structures have been returned. */
200 static bool rs6000_returns_struct;
201 #endif
203 /* Value is TRUE if register/mode pair is acceptable. */
204 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
206 /* Maximum number of registers needed for a given register class and mode. */
207 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
209 /* How many registers are needed for a given register and mode. */
210 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
212 /* Map register number to register class. */
213 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
215 static int dbg_cost_ctrl;
217 /* Built in types. */
218 tree rs6000_builtin_types[RS6000_BTI_MAX];
219 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
221 /* Flag to say the TOC is initialized */
222 int toc_initialized, need_toc_init;
223 char toc_label_name[10];
225 /* Cached value of rs6000_variable_issue. This is cached in
226 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
227 static short cached_can_issue_more;
229 static GTY(()) section *read_only_data_section;
230 static GTY(()) section *private_data_section;
231 static GTY(()) section *tls_data_section;
232 static GTY(()) section *tls_private_data_section;
233 static GTY(()) section *read_only_private_data_section;
234 static GTY(()) section *sdata2_section;
235 static GTY(()) section *toc_section;
237 struct builtin_description
239 const HOST_WIDE_INT mask;
240 const enum insn_code icode;
241 const char *const name;
242 const enum rs6000_builtins code;
245 /* Describe the vector unit used for modes. */
246 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
247 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
249 /* Register classes for various constraints that are based on the target
250 switches. */
251 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
253 /* Describe the alignment of a vector. */
254 int rs6000_vector_align[NUM_MACHINE_MODES];
256 /* Map selected modes to types for builtins. */
257 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
259 /* What modes to automatically generate reciprocal divide estimate (fre) and
260 reciprocal sqrt (frsqrte) for. */
261 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
263 /* Masks to determine which reciprocal esitmate instructions to generate
264 automatically. */
265 enum rs6000_recip_mask {
266 RECIP_SF_DIV = 0x001, /* Use divide estimate */
267 RECIP_DF_DIV = 0x002,
268 RECIP_V4SF_DIV = 0x004,
269 RECIP_V2DF_DIV = 0x008,
271 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
272 RECIP_DF_RSQRT = 0x020,
273 RECIP_V4SF_RSQRT = 0x040,
274 RECIP_V2DF_RSQRT = 0x080,
276 /* Various combination of flags for -mrecip=xxx. */
277 RECIP_NONE = 0,
278 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
279 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
280 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
282 RECIP_HIGH_PRECISION = RECIP_ALL,
284 /* On low precision machines like the power5, don't enable double precision
285 reciprocal square root estimate, since it isn't accurate enough. */
286 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
289 /* -mrecip options. */
290 static struct
292 const char *string; /* option name */
293 unsigned int mask; /* mask bits to set */
294 } recip_options[] = {
295 { "all", RECIP_ALL },
296 { "none", RECIP_NONE },
297 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
298 | RECIP_V2DF_DIV) },
299 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
300 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
301 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
302 | RECIP_V2DF_RSQRT) },
303 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
304 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
307 /* Used by __builtin_cpu_is(), mapping from PLATFORM names to values. */
308 static const struct
310 const char *cpu;
311 unsigned int cpuid;
312 } cpu_is_info[] = {
313 { "power9", PPC_PLATFORM_POWER9 },
314 { "power8", PPC_PLATFORM_POWER8 },
315 { "power7", PPC_PLATFORM_POWER7 },
316 { "power6x", PPC_PLATFORM_POWER6X },
317 { "power6", PPC_PLATFORM_POWER6 },
318 { "power5+", PPC_PLATFORM_POWER5_PLUS },
319 { "power5", PPC_PLATFORM_POWER5 },
320 { "ppc970", PPC_PLATFORM_PPC970 },
321 { "power4", PPC_PLATFORM_POWER4 },
322 { "ppca2", PPC_PLATFORM_PPCA2 },
323 { "ppc476", PPC_PLATFORM_PPC476 },
324 { "ppc464", PPC_PLATFORM_PPC464 },
325 { "ppc440", PPC_PLATFORM_PPC440 },
326 { "ppc405", PPC_PLATFORM_PPC405 },
327 { "ppc-cell-be", PPC_PLATFORM_CELL_BE }
330 /* Used by __builtin_cpu_supports(), mapping from HWCAP names to masks. */
331 static const struct
333 const char *hwcap;
334 int mask;
335 unsigned int id;
336 } cpu_supports_info[] = {
337 /* AT_HWCAP masks. */
338 { "4xxmac", PPC_FEATURE_HAS_4xxMAC, 0 },
339 { "altivec", PPC_FEATURE_HAS_ALTIVEC, 0 },
340 { "arch_2_05", PPC_FEATURE_ARCH_2_05, 0 },
341 { "arch_2_06", PPC_FEATURE_ARCH_2_06, 0 },
342 { "archpmu", PPC_FEATURE_PERFMON_COMPAT, 0 },
343 { "booke", PPC_FEATURE_BOOKE, 0 },
344 { "cellbe", PPC_FEATURE_CELL_BE, 0 },
345 { "dfp", PPC_FEATURE_HAS_DFP, 0 },
346 { "efpdouble", PPC_FEATURE_HAS_EFP_DOUBLE, 0 },
347 { "efpsingle", PPC_FEATURE_HAS_EFP_SINGLE, 0 },
348 { "fpu", PPC_FEATURE_HAS_FPU, 0 },
349 { "ic_snoop", PPC_FEATURE_ICACHE_SNOOP, 0 },
350 { "mmu", PPC_FEATURE_HAS_MMU, 0 },
351 { "notb", PPC_FEATURE_NO_TB, 0 },
352 { "pa6t", PPC_FEATURE_PA6T, 0 },
353 { "power4", PPC_FEATURE_POWER4, 0 },
354 { "power5", PPC_FEATURE_POWER5, 0 },
355 { "power5+", PPC_FEATURE_POWER5_PLUS, 0 },
356 { "power6x", PPC_FEATURE_POWER6_EXT, 0 },
357 { "ppc32", PPC_FEATURE_32, 0 },
358 { "ppc601", PPC_FEATURE_601_INSTR, 0 },
359 { "ppc64", PPC_FEATURE_64, 0 },
360 { "ppcle", PPC_FEATURE_PPC_LE, 0 },
361 { "smt", PPC_FEATURE_SMT, 0 },
362 { "spe", PPC_FEATURE_HAS_SPE, 0 },
363 { "true_le", PPC_FEATURE_TRUE_LE, 0 },
364 { "ucache", PPC_FEATURE_UNIFIED_CACHE, 0 },
365 { "vsx", PPC_FEATURE_HAS_VSX, 0 },
367 /* AT_HWCAP2 masks. */
368 { "arch_2_07", PPC_FEATURE2_ARCH_2_07, 1 },
369 { "dscr", PPC_FEATURE2_HAS_DSCR, 1 },
370 { "ebb", PPC_FEATURE2_HAS_EBB, 1 },
371 { "htm", PPC_FEATURE2_HAS_HTM, 1 },
372 { "htm-nosc", PPC_FEATURE2_HTM_NOSC, 1 },
373 { "isel", PPC_FEATURE2_HAS_ISEL, 1 },
374 { "tar", PPC_FEATURE2_HAS_TAR, 1 },
375 { "vcrypto", PPC_FEATURE2_HAS_VEC_CRYPTO, 1 },
376 { "arch_3_00", PPC_FEATURE2_ARCH_3_00, 1 },
377 { "ieee128", PPC_FEATURE2_HAS_IEEE128, 1 },
378 { "darn", PPC_FEATURE2_DARN, 1 },
379 { "scv", PPC_FEATURE2_SCV, 1 }
382 /* On PowerPC, we have a limited number of target clones that we care about
383 which means we can use an array to hold the options, rather than having more
384 elaborate data structures to identify each possible variation. Order the
385 clones from the default to the highest ISA. */
386 enum {
387 CLONE_DEFAULT = 0, /* default clone. */
388 CLONE_ISA_2_05, /* ISA 2.05 (power6). */
389 CLONE_ISA_2_06, /* ISA 2.06 (power7). */
390 CLONE_ISA_2_07, /* ISA 2.07 (power8). */
391 CLONE_ISA_3_00, /* ISA 3.00 (power9). */
392 CLONE_MAX
395 /* Map compiler ISA bits into HWCAP names. */
396 struct clone_map {
397 HOST_WIDE_INT isa_mask; /* rs6000_isa mask */
398 const char *name; /* name to use in __builtin_cpu_supports. */
401 static const struct clone_map rs6000_clone_map[CLONE_MAX] = {
402 { 0, "" }, /* Default options. */
403 { OPTION_MASK_CMPB, "arch_2_05" }, /* ISA 2.05 (power6). */
404 { OPTION_MASK_POPCNTD, "arch_2_06" }, /* ISA 2.06 (power7). */
405 { OPTION_MASK_P8_VECTOR, "arch_2_07" }, /* ISA 2.07 (power8). */
406 { OPTION_MASK_P9_VECTOR, "arch_3_00" }, /* ISA 3.00 (power9). */
410 /* Newer LIBCs explicitly export this symbol to declare that they provide
411 the AT_PLATFORM and AT_HWCAP/AT_HWCAP2 values in the TCB. We emit a
412 reference to this symbol whenever we expand a CPU builtin, so that
413 we never link against an old LIBC. */
414 const char *tcb_verification_symbol = "__parse_hwcap_and_convert_at_platform";
416 /* True if we have expanded a CPU builtin. */
417 bool cpu_builtin_p;
419 /* Pointer to function (in rs6000-c.c) that can define or undefine target
420 macros that have changed. Languages that don't support the preprocessor
421 don't link in rs6000-c.c, so we can't call it directly. */
422 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
424 /* Simplfy register classes into simpler classifications. We assume
425 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
426 check for standard register classes (gpr/floating/altivec/vsx) and
427 floating/vector classes (float/altivec/vsx). */
429 enum rs6000_reg_type {
430 NO_REG_TYPE,
431 PSEUDO_REG_TYPE,
432 GPR_REG_TYPE,
433 VSX_REG_TYPE,
434 ALTIVEC_REG_TYPE,
435 FPR_REG_TYPE,
436 SPR_REG_TYPE,
437 CR_REG_TYPE
440 /* Map register class to register type. */
441 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
443 /* First/last register type for the 'normal' register types (i.e. general
444 purpose, floating point, altivec, and VSX registers). */
445 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
447 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
450 /* Register classes we care about in secondary reload or go if legitimate
451 address. We only need to worry about GPR, FPR, and Altivec registers here,
452 along an ANY field that is the OR of the 3 register classes. */
454 enum rs6000_reload_reg_type {
455 RELOAD_REG_GPR, /* General purpose registers. */
456 RELOAD_REG_FPR, /* Traditional floating point regs. */
457 RELOAD_REG_VMX, /* Altivec (VMX) registers. */
458 RELOAD_REG_ANY, /* OR of GPR, FPR, Altivec masks. */
459 N_RELOAD_REG
462 /* For setting up register classes, loop through the 3 register classes mapping
463 into real registers, and skip the ANY class, which is just an OR of the
464 bits. */
465 #define FIRST_RELOAD_REG_CLASS RELOAD_REG_GPR
466 #define LAST_RELOAD_REG_CLASS RELOAD_REG_VMX
468 /* Map reload register type to a register in the register class. */
469 struct reload_reg_map_type {
470 const char *name; /* Register class name. */
471 int reg; /* Register in the register class. */
474 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
475 { "Gpr", FIRST_GPR_REGNO }, /* RELOAD_REG_GPR. */
476 { "Fpr", FIRST_FPR_REGNO }, /* RELOAD_REG_FPR. */
477 { "VMX", FIRST_ALTIVEC_REGNO }, /* RELOAD_REG_VMX. */
478 { "Any", -1 }, /* RELOAD_REG_ANY. */
481 /* Mask bits for each register class, indexed per mode. Historically the
482 compiler has been more restrictive which types can do PRE_MODIFY instead of
483 PRE_INC and PRE_DEC, so keep track of sepaate bits for these two. */
484 typedef unsigned char addr_mask_type;
486 #define RELOAD_REG_VALID 0x01 /* Mode valid in register.. */
487 #define RELOAD_REG_MULTIPLE 0x02 /* Mode takes multiple registers. */
488 #define RELOAD_REG_INDEXED 0x04 /* Reg+reg addressing. */
489 #define RELOAD_REG_OFFSET 0x08 /* Reg+offset addressing. */
490 #define RELOAD_REG_PRE_INCDEC 0x10 /* PRE_INC/PRE_DEC valid. */
491 #define RELOAD_REG_PRE_MODIFY 0x20 /* PRE_MODIFY valid. */
492 #define RELOAD_REG_AND_M16 0x40 /* AND -16 addressing. */
493 #define RELOAD_REG_QUAD_OFFSET 0x80 /* quad offset is limited. */
495 /* Register type masks based on the type, of valid addressing modes. */
496 struct rs6000_reg_addr {
497 enum insn_code reload_load; /* INSN to reload for loading. */
498 enum insn_code reload_store; /* INSN to reload for storing. */
499 enum insn_code reload_fpr_gpr; /* INSN to move from FPR to GPR. */
500 enum insn_code reload_gpr_vsx; /* INSN to move from GPR to VSX. */
501 enum insn_code reload_vsx_gpr; /* INSN to move from VSX to GPR. */
502 enum insn_code fusion_gpr_ld; /* INSN for fusing gpr ADDIS/loads. */
503 /* INSNs for fusing addi with loads
504 or stores for each reg. class. */
505 enum insn_code fusion_addi_ld[(int)N_RELOAD_REG];
506 enum insn_code fusion_addi_st[(int)N_RELOAD_REG];
507 /* INSNs for fusing addis with loads
508 or stores for each reg. class. */
509 enum insn_code fusion_addis_ld[(int)N_RELOAD_REG];
510 enum insn_code fusion_addis_st[(int)N_RELOAD_REG];
511 addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks. */
512 bool scalar_in_vmx_p; /* Scalar value can go in VMX. */
513 bool fused_toc; /* Mode supports TOC fusion. */
516 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
518 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC. */
519 static inline bool
520 mode_supports_pre_incdec_p (machine_mode mode)
522 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
523 != 0);
526 /* Helper function to say whether a mode supports PRE_MODIFY. */
527 static inline bool
528 mode_supports_pre_modify_p (machine_mode mode)
530 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
531 != 0);
534 /* Given that there exists at least one variable that is set (produced)
535 by OUT_INSN and read (consumed) by IN_INSN, return true iff
536 IN_INSN represents one or more memory store operations and none of
537 the variables set by OUT_INSN is used by IN_INSN as the address of a
538 store operation. If either IN_INSN or OUT_INSN does not represent
539 a "single" RTL SET expression (as loosely defined by the
540 implementation of the single_set function) or a PARALLEL with only
541 SETs, CLOBBERs, and USEs inside, this function returns false.
543 This rs6000-specific version of store_data_bypass_p checks for
544 certain conditions that result in assertion failures (and internal
545 compiler errors) in the generic store_data_bypass_p function and
546 returns false rather than calling store_data_bypass_p if one of the
547 problematic conditions is detected. */
550 rs6000_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
552 rtx out_set, in_set;
553 rtx out_pat, in_pat;
554 rtx out_exp, in_exp;
555 int i, j;
557 in_set = single_set (in_insn);
558 if (in_set)
560 if (MEM_P (SET_DEST (in_set)))
562 out_set = single_set (out_insn);
563 if (!out_set)
565 out_pat = PATTERN (out_insn);
566 if (GET_CODE (out_pat) == PARALLEL)
568 for (i = 0; i < XVECLEN (out_pat, 0); i++)
570 out_exp = XVECEXP (out_pat, 0, i);
571 if ((GET_CODE (out_exp) == CLOBBER)
572 || (GET_CODE (out_exp) == USE))
573 continue;
574 else if (GET_CODE (out_exp) != SET)
575 return false;
581 else
583 in_pat = PATTERN (in_insn);
584 if (GET_CODE (in_pat) != PARALLEL)
585 return false;
587 for (i = 0; i < XVECLEN (in_pat, 0); i++)
589 in_exp = XVECEXP (in_pat, 0, i);
590 if ((GET_CODE (in_exp) == CLOBBER) || (GET_CODE (in_exp) == USE))
591 continue;
592 else if (GET_CODE (in_exp) != SET)
593 return false;
595 if (MEM_P (SET_DEST (in_exp)))
597 out_set = single_set (out_insn);
598 if (!out_set)
600 out_pat = PATTERN (out_insn);
601 if (GET_CODE (out_pat) != PARALLEL)
602 return false;
603 for (j = 0; j < XVECLEN (out_pat, 0); j++)
605 out_exp = XVECEXP (out_pat, 0, j);
606 if ((GET_CODE (out_exp) == CLOBBER)
607 || (GET_CODE (out_exp) == USE))
608 continue;
609 else if (GET_CODE (out_exp) != SET)
610 return false;
616 return store_data_bypass_p (out_insn, in_insn);
619 /* Return true if we have D-form addressing in altivec registers. */
620 static inline bool
621 mode_supports_vmx_dform (machine_mode mode)
623 return ((reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_OFFSET) != 0);
626 /* Return true if we have D-form addressing in VSX registers. This addressing
627 is more limited than normal d-form addressing in that the offset must be
628 aligned on a 16-byte boundary. */
629 static inline bool
630 mode_supports_vsx_dform_quad (machine_mode mode)
632 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_QUAD_OFFSET)
633 != 0);
637 /* Target cpu costs. */
639 struct processor_costs {
640 const int mulsi; /* cost of SImode multiplication. */
641 const int mulsi_const; /* cost of SImode multiplication by constant. */
642 const int mulsi_const9; /* cost of SImode mult by short constant. */
643 const int muldi; /* cost of DImode multiplication. */
644 const int divsi; /* cost of SImode division. */
645 const int divdi; /* cost of DImode division. */
646 const int fp; /* cost of simple SFmode and DFmode insns. */
647 const int dmul; /* cost of DFmode multiplication (and fmadd). */
648 const int sdiv; /* cost of SFmode division (fdivs). */
649 const int ddiv; /* cost of DFmode division (fdiv). */
650 const int cache_line_size; /* cache line size in bytes. */
651 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
652 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
653 const int simultaneous_prefetches; /* number of parallel prefetch
654 operations. */
655 const int sfdf_convert; /* cost of SF->DF conversion. */
658 const struct processor_costs *rs6000_cost;
660 /* Processor costs (relative to an add) */
662 /* Instruction size costs on 32bit processors. */
663 static const
664 struct processor_costs size32_cost = {
665 COSTS_N_INSNS (1), /* mulsi */
666 COSTS_N_INSNS (1), /* mulsi_const */
667 COSTS_N_INSNS (1), /* mulsi_const9 */
668 COSTS_N_INSNS (1), /* muldi */
669 COSTS_N_INSNS (1), /* divsi */
670 COSTS_N_INSNS (1), /* divdi */
671 COSTS_N_INSNS (1), /* fp */
672 COSTS_N_INSNS (1), /* dmul */
673 COSTS_N_INSNS (1), /* sdiv */
674 COSTS_N_INSNS (1), /* ddiv */
675 32, /* cache line size */
676 0, /* l1 cache */
677 0, /* l2 cache */
678 0, /* streams */
679 0, /* SF->DF convert */
682 /* Instruction size costs on 64bit processors. */
683 static const
684 struct processor_costs size64_cost = {
685 COSTS_N_INSNS (1), /* mulsi */
686 COSTS_N_INSNS (1), /* mulsi_const */
687 COSTS_N_INSNS (1), /* mulsi_const9 */
688 COSTS_N_INSNS (1), /* muldi */
689 COSTS_N_INSNS (1), /* divsi */
690 COSTS_N_INSNS (1), /* divdi */
691 COSTS_N_INSNS (1), /* fp */
692 COSTS_N_INSNS (1), /* dmul */
693 COSTS_N_INSNS (1), /* sdiv */
694 COSTS_N_INSNS (1), /* ddiv */
695 128, /* cache line size */
696 0, /* l1 cache */
697 0, /* l2 cache */
698 0, /* streams */
699 0, /* SF->DF convert */
702 /* Instruction costs on RS64A processors. */
703 static const
704 struct processor_costs rs64a_cost = {
705 COSTS_N_INSNS (20), /* mulsi */
706 COSTS_N_INSNS (12), /* mulsi_const */
707 COSTS_N_INSNS (8), /* mulsi_const9 */
708 COSTS_N_INSNS (34), /* muldi */
709 COSTS_N_INSNS (65), /* divsi */
710 COSTS_N_INSNS (67), /* divdi */
711 COSTS_N_INSNS (4), /* fp */
712 COSTS_N_INSNS (4), /* dmul */
713 COSTS_N_INSNS (31), /* sdiv */
714 COSTS_N_INSNS (31), /* ddiv */
715 128, /* cache line size */
716 128, /* l1 cache */
717 2048, /* l2 cache */
718 1, /* streams */
719 0, /* SF->DF convert */
722 /* Instruction costs on MPCCORE processors. */
723 static const
724 struct processor_costs mpccore_cost = {
725 COSTS_N_INSNS (2), /* mulsi */
726 COSTS_N_INSNS (2), /* mulsi_const */
727 COSTS_N_INSNS (2), /* mulsi_const9 */
728 COSTS_N_INSNS (2), /* muldi */
729 COSTS_N_INSNS (6), /* divsi */
730 COSTS_N_INSNS (6), /* divdi */
731 COSTS_N_INSNS (4), /* fp */
732 COSTS_N_INSNS (5), /* dmul */
733 COSTS_N_INSNS (10), /* sdiv */
734 COSTS_N_INSNS (17), /* ddiv */
735 32, /* cache line size */
736 4, /* l1 cache */
737 16, /* l2 cache */
738 1, /* streams */
739 0, /* SF->DF convert */
742 /* Instruction costs on PPC403 processors. */
743 static const
744 struct processor_costs ppc403_cost = {
745 COSTS_N_INSNS (4), /* mulsi */
746 COSTS_N_INSNS (4), /* mulsi_const */
747 COSTS_N_INSNS (4), /* mulsi_const9 */
748 COSTS_N_INSNS (4), /* muldi */
749 COSTS_N_INSNS (33), /* divsi */
750 COSTS_N_INSNS (33), /* divdi */
751 COSTS_N_INSNS (11), /* fp */
752 COSTS_N_INSNS (11), /* dmul */
753 COSTS_N_INSNS (11), /* sdiv */
754 COSTS_N_INSNS (11), /* ddiv */
755 32, /* cache line size */
756 4, /* l1 cache */
757 16, /* l2 cache */
758 1, /* streams */
759 0, /* SF->DF convert */
762 /* Instruction costs on PPC405 processors. */
763 static const
764 struct processor_costs ppc405_cost = {
765 COSTS_N_INSNS (5), /* mulsi */
766 COSTS_N_INSNS (4), /* mulsi_const */
767 COSTS_N_INSNS (3), /* mulsi_const9 */
768 COSTS_N_INSNS (5), /* muldi */
769 COSTS_N_INSNS (35), /* divsi */
770 COSTS_N_INSNS (35), /* divdi */
771 COSTS_N_INSNS (11), /* fp */
772 COSTS_N_INSNS (11), /* dmul */
773 COSTS_N_INSNS (11), /* sdiv */
774 COSTS_N_INSNS (11), /* ddiv */
775 32, /* cache line size */
776 16, /* l1 cache */
777 128, /* l2 cache */
778 1, /* streams */
779 0, /* SF->DF convert */
782 /* Instruction costs on PPC440 processors. */
783 static const
784 struct processor_costs ppc440_cost = {
785 COSTS_N_INSNS (3), /* mulsi */
786 COSTS_N_INSNS (2), /* mulsi_const */
787 COSTS_N_INSNS (2), /* mulsi_const9 */
788 COSTS_N_INSNS (3), /* muldi */
789 COSTS_N_INSNS (34), /* divsi */
790 COSTS_N_INSNS (34), /* divdi */
791 COSTS_N_INSNS (5), /* fp */
792 COSTS_N_INSNS (5), /* dmul */
793 COSTS_N_INSNS (19), /* sdiv */
794 COSTS_N_INSNS (33), /* ddiv */
795 32, /* cache line size */
796 32, /* l1 cache */
797 256, /* l2 cache */
798 1, /* streams */
799 0, /* SF->DF convert */
802 /* Instruction costs on PPC476 processors. */
803 static const
804 struct processor_costs ppc476_cost = {
805 COSTS_N_INSNS (4), /* mulsi */
806 COSTS_N_INSNS (4), /* mulsi_const */
807 COSTS_N_INSNS (4), /* mulsi_const9 */
808 COSTS_N_INSNS (4), /* muldi */
809 COSTS_N_INSNS (11), /* divsi */
810 COSTS_N_INSNS (11), /* divdi */
811 COSTS_N_INSNS (6), /* fp */
812 COSTS_N_INSNS (6), /* dmul */
813 COSTS_N_INSNS (19), /* sdiv */
814 COSTS_N_INSNS (33), /* ddiv */
815 32, /* l1 cache line size */
816 32, /* l1 cache */
817 512, /* l2 cache */
818 1, /* streams */
819 0, /* SF->DF convert */
822 /* Instruction costs on PPC601 processors. */
823 static const
824 struct processor_costs ppc601_cost = {
825 COSTS_N_INSNS (5), /* mulsi */
826 COSTS_N_INSNS (5), /* mulsi_const */
827 COSTS_N_INSNS (5), /* mulsi_const9 */
828 COSTS_N_INSNS (5), /* muldi */
829 COSTS_N_INSNS (36), /* divsi */
830 COSTS_N_INSNS (36), /* divdi */
831 COSTS_N_INSNS (4), /* fp */
832 COSTS_N_INSNS (5), /* dmul */
833 COSTS_N_INSNS (17), /* sdiv */
834 COSTS_N_INSNS (31), /* ddiv */
835 32, /* cache line size */
836 32, /* l1 cache */
837 256, /* l2 cache */
838 1, /* streams */
839 0, /* SF->DF convert */
842 /* Instruction costs on PPC603 processors. */
843 static const
844 struct processor_costs ppc603_cost = {
845 COSTS_N_INSNS (5), /* mulsi */
846 COSTS_N_INSNS (3), /* mulsi_const */
847 COSTS_N_INSNS (2), /* mulsi_const9 */
848 COSTS_N_INSNS (5), /* muldi */
849 COSTS_N_INSNS (37), /* divsi */
850 COSTS_N_INSNS (37), /* divdi */
851 COSTS_N_INSNS (3), /* fp */
852 COSTS_N_INSNS (4), /* dmul */
853 COSTS_N_INSNS (18), /* sdiv */
854 COSTS_N_INSNS (33), /* ddiv */
855 32, /* cache line size */
856 8, /* l1 cache */
857 64, /* l2 cache */
858 1, /* streams */
859 0, /* SF->DF convert */
862 /* Instruction costs on PPC604 processors. */
863 static const
864 struct processor_costs ppc604_cost = {
865 COSTS_N_INSNS (4), /* mulsi */
866 COSTS_N_INSNS (4), /* mulsi_const */
867 COSTS_N_INSNS (4), /* mulsi_const9 */
868 COSTS_N_INSNS (4), /* muldi */
869 COSTS_N_INSNS (20), /* divsi */
870 COSTS_N_INSNS (20), /* divdi */
871 COSTS_N_INSNS (3), /* fp */
872 COSTS_N_INSNS (3), /* dmul */
873 COSTS_N_INSNS (18), /* sdiv */
874 COSTS_N_INSNS (32), /* ddiv */
875 32, /* cache line size */
876 16, /* l1 cache */
877 512, /* l2 cache */
878 1, /* streams */
879 0, /* SF->DF convert */
882 /* Instruction costs on PPC604e processors. */
883 static const
884 struct processor_costs ppc604e_cost = {
885 COSTS_N_INSNS (2), /* mulsi */
886 COSTS_N_INSNS (2), /* mulsi_const */
887 COSTS_N_INSNS (2), /* mulsi_const9 */
888 COSTS_N_INSNS (2), /* muldi */
889 COSTS_N_INSNS (20), /* divsi */
890 COSTS_N_INSNS (20), /* divdi */
891 COSTS_N_INSNS (3), /* fp */
892 COSTS_N_INSNS (3), /* dmul */
893 COSTS_N_INSNS (18), /* sdiv */
894 COSTS_N_INSNS (32), /* ddiv */
895 32, /* cache line size */
896 32, /* l1 cache */
897 1024, /* l2 cache */
898 1, /* streams */
899 0, /* SF->DF convert */
902 /* Instruction costs on PPC620 processors. */
903 static const
904 struct processor_costs ppc620_cost = {
905 COSTS_N_INSNS (5), /* mulsi */
906 COSTS_N_INSNS (4), /* mulsi_const */
907 COSTS_N_INSNS (3), /* mulsi_const9 */
908 COSTS_N_INSNS (7), /* muldi */
909 COSTS_N_INSNS (21), /* divsi */
910 COSTS_N_INSNS (37), /* divdi */
911 COSTS_N_INSNS (3), /* fp */
912 COSTS_N_INSNS (3), /* dmul */
913 COSTS_N_INSNS (18), /* sdiv */
914 COSTS_N_INSNS (32), /* ddiv */
915 128, /* cache line size */
916 32, /* l1 cache */
917 1024, /* l2 cache */
918 1, /* streams */
919 0, /* SF->DF convert */
922 /* Instruction costs on PPC630 processors. */
923 static const
924 struct processor_costs ppc630_cost = {
925 COSTS_N_INSNS (5), /* mulsi */
926 COSTS_N_INSNS (4), /* mulsi_const */
927 COSTS_N_INSNS (3), /* mulsi_const9 */
928 COSTS_N_INSNS (7), /* muldi */
929 COSTS_N_INSNS (21), /* divsi */
930 COSTS_N_INSNS (37), /* divdi */
931 COSTS_N_INSNS (3), /* fp */
932 COSTS_N_INSNS (3), /* dmul */
933 COSTS_N_INSNS (17), /* sdiv */
934 COSTS_N_INSNS (21), /* ddiv */
935 128, /* cache line size */
936 64, /* l1 cache */
937 1024, /* l2 cache */
938 1, /* streams */
939 0, /* SF->DF convert */
942 /* Instruction costs on Cell processor. */
943 /* COSTS_N_INSNS (1) ~ one add. */
944 static const
945 struct processor_costs ppccell_cost = {
946 COSTS_N_INSNS (9/2)+2, /* mulsi */
947 COSTS_N_INSNS (6/2), /* mulsi_const */
948 COSTS_N_INSNS (6/2), /* mulsi_const9 */
949 COSTS_N_INSNS (15/2)+2, /* muldi */
950 COSTS_N_INSNS (38/2), /* divsi */
951 COSTS_N_INSNS (70/2), /* divdi */
952 COSTS_N_INSNS (10/2), /* fp */
953 COSTS_N_INSNS (10/2), /* dmul */
954 COSTS_N_INSNS (74/2), /* sdiv */
955 COSTS_N_INSNS (74/2), /* ddiv */
956 128, /* cache line size */
957 32, /* l1 cache */
958 512, /* l2 cache */
959 6, /* streams */
960 0, /* SF->DF convert */
963 /* Instruction costs on PPC750 and PPC7400 processors. */
964 static const
965 struct processor_costs ppc750_cost = {
966 COSTS_N_INSNS (5), /* mulsi */
967 COSTS_N_INSNS (3), /* mulsi_const */
968 COSTS_N_INSNS (2), /* mulsi_const9 */
969 COSTS_N_INSNS (5), /* muldi */
970 COSTS_N_INSNS (17), /* divsi */
971 COSTS_N_INSNS (17), /* divdi */
972 COSTS_N_INSNS (3), /* fp */
973 COSTS_N_INSNS (3), /* dmul */
974 COSTS_N_INSNS (17), /* sdiv */
975 COSTS_N_INSNS (31), /* ddiv */
976 32, /* cache line size */
977 32, /* l1 cache */
978 512, /* l2 cache */
979 1, /* streams */
980 0, /* SF->DF convert */
983 /* Instruction costs on PPC7450 processors. */
984 static const
985 struct processor_costs ppc7450_cost = {
986 COSTS_N_INSNS (4), /* mulsi */
987 COSTS_N_INSNS (3), /* mulsi_const */
988 COSTS_N_INSNS (3), /* mulsi_const9 */
989 COSTS_N_INSNS (4), /* muldi */
990 COSTS_N_INSNS (23), /* divsi */
991 COSTS_N_INSNS (23), /* divdi */
992 COSTS_N_INSNS (5), /* fp */
993 COSTS_N_INSNS (5), /* dmul */
994 COSTS_N_INSNS (21), /* sdiv */
995 COSTS_N_INSNS (35), /* ddiv */
996 32, /* cache line size */
997 32, /* l1 cache */
998 1024, /* l2 cache */
999 1, /* streams */
1000 0, /* SF->DF convert */
1003 /* Instruction costs on PPC8540 processors. */
1004 static const
1005 struct processor_costs ppc8540_cost = {
1006 COSTS_N_INSNS (4), /* mulsi */
1007 COSTS_N_INSNS (4), /* mulsi_const */
1008 COSTS_N_INSNS (4), /* mulsi_const9 */
1009 COSTS_N_INSNS (4), /* muldi */
1010 COSTS_N_INSNS (19), /* divsi */
1011 COSTS_N_INSNS (19), /* divdi */
1012 COSTS_N_INSNS (4), /* fp */
1013 COSTS_N_INSNS (4), /* dmul */
1014 COSTS_N_INSNS (29), /* sdiv */
1015 COSTS_N_INSNS (29), /* ddiv */
1016 32, /* cache line size */
1017 32, /* l1 cache */
1018 256, /* l2 cache */
1019 1, /* prefetch streams /*/
1020 0, /* SF->DF convert */
1023 /* Instruction costs on E300C2 and E300C3 cores. */
1024 static const
1025 struct processor_costs ppce300c2c3_cost = {
1026 COSTS_N_INSNS (4), /* mulsi */
1027 COSTS_N_INSNS (4), /* mulsi_const */
1028 COSTS_N_INSNS (4), /* mulsi_const9 */
1029 COSTS_N_INSNS (4), /* muldi */
1030 COSTS_N_INSNS (19), /* divsi */
1031 COSTS_N_INSNS (19), /* divdi */
1032 COSTS_N_INSNS (3), /* fp */
1033 COSTS_N_INSNS (4), /* dmul */
1034 COSTS_N_INSNS (18), /* sdiv */
1035 COSTS_N_INSNS (33), /* ddiv */
1037 16, /* l1 cache */
1038 16, /* l2 cache */
1039 1, /* prefetch streams /*/
1040 0, /* SF->DF convert */
1043 /* Instruction costs on PPCE500MC processors. */
1044 static const
1045 struct processor_costs ppce500mc_cost = {
1046 COSTS_N_INSNS (4), /* mulsi */
1047 COSTS_N_INSNS (4), /* mulsi_const */
1048 COSTS_N_INSNS (4), /* mulsi_const9 */
1049 COSTS_N_INSNS (4), /* muldi */
1050 COSTS_N_INSNS (14), /* divsi */
1051 COSTS_N_INSNS (14), /* divdi */
1052 COSTS_N_INSNS (8), /* fp */
1053 COSTS_N_INSNS (10), /* dmul */
1054 COSTS_N_INSNS (36), /* sdiv */
1055 COSTS_N_INSNS (66), /* ddiv */
1056 64, /* cache line size */
1057 32, /* l1 cache */
1058 128, /* l2 cache */
1059 1, /* prefetch streams /*/
1060 0, /* SF->DF convert */
1063 /* Instruction costs on PPCE500MC64 processors. */
1064 static const
1065 struct processor_costs ppce500mc64_cost = {
1066 COSTS_N_INSNS (4), /* mulsi */
1067 COSTS_N_INSNS (4), /* mulsi_const */
1068 COSTS_N_INSNS (4), /* mulsi_const9 */
1069 COSTS_N_INSNS (4), /* muldi */
1070 COSTS_N_INSNS (14), /* divsi */
1071 COSTS_N_INSNS (14), /* divdi */
1072 COSTS_N_INSNS (4), /* fp */
1073 COSTS_N_INSNS (10), /* dmul */
1074 COSTS_N_INSNS (36), /* sdiv */
1075 COSTS_N_INSNS (66), /* ddiv */
1076 64, /* cache line size */
1077 32, /* l1 cache */
1078 128, /* l2 cache */
1079 1, /* prefetch streams /*/
1080 0, /* SF->DF convert */
1083 /* Instruction costs on PPCE5500 processors. */
1084 static const
1085 struct processor_costs ppce5500_cost = {
1086 COSTS_N_INSNS (5), /* mulsi */
1087 COSTS_N_INSNS (5), /* mulsi_const */
1088 COSTS_N_INSNS (4), /* mulsi_const9 */
1089 COSTS_N_INSNS (5), /* muldi */
1090 COSTS_N_INSNS (14), /* divsi */
1091 COSTS_N_INSNS (14), /* divdi */
1092 COSTS_N_INSNS (7), /* fp */
1093 COSTS_N_INSNS (10), /* dmul */
1094 COSTS_N_INSNS (36), /* sdiv */
1095 COSTS_N_INSNS (66), /* ddiv */
1096 64, /* cache line size */
1097 32, /* l1 cache */
1098 128, /* l2 cache */
1099 1, /* prefetch streams /*/
1100 0, /* SF->DF convert */
1103 /* Instruction costs on PPCE6500 processors. */
1104 static const
1105 struct processor_costs ppce6500_cost = {
1106 COSTS_N_INSNS (5), /* mulsi */
1107 COSTS_N_INSNS (5), /* mulsi_const */
1108 COSTS_N_INSNS (4), /* mulsi_const9 */
1109 COSTS_N_INSNS (5), /* muldi */
1110 COSTS_N_INSNS (14), /* divsi */
1111 COSTS_N_INSNS (14), /* divdi */
1112 COSTS_N_INSNS (7), /* fp */
1113 COSTS_N_INSNS (10), /* dmul */
1114 COSTS_N_INSNS (36), /* sdiv */
1115 COSTS_N_INSNS (66), /* ddiv */
1116 64, /* cache line size */
1117 32, /* l1 cache */
1118 128, /* l2 cache */
1119 1, /* prefetch streams /*/
1120 0, /* SF->DF convert */
1123 /* Instruction costs on AppliedMicro Titan processors. */
1124 static const
1125 struct processor_costs titan_cost = {
1126 COSTS_N_INSNS (5), /* mulsi */
1127 COSTS_N_INSNS (5), /* mulsi_const */
1128 COSTS_N_INSNS (5), /* mulsi_const9 */
1129 COSTS_N_INSNS (5), /* muldi */
1130 COSTS_N_INSNS (18), /* divsi */
1131 COSTS_N_INSNS (18), /* divdi */
1132 COSTS_N_INSNS (10), /* fp */
1133 COSTS_N_INSNS (10), /* dmul */
1134 COSTS_N_INSNS (46), /* sdiv */
1135 COSTS_N_INSNS (72), /* ddiv */
1136 32, /* cache line size */
1137 32, /* l1 cache */
1138 512, /* l2 cache */
1139 1, /* prefetch streams /*/
1140 0, /* SF->DF convert */
1143 /* Instruction costs on POWER4 and POWER5 processors. */
1144 static const
1145 struct processor_costs power4_cost = {
1146 COSTS_N_INSNS (3), /* mulsi */
1147 COSTS_N_INSNS (2), /* mulsi_const */
1148 COSTS_N_INSNS (2), /* mulsi_const9 */
1149 COSTS_N_INSNS (4), /* muldi */
1150 COSTS_N_INSNS (18), /* divsi */
1151 COSTS_N_INSNS (34), /* divdi */
1152 COSTS_N_INSNS (3), /* fp */
1153 COSTS_N_INSNS (3), /* dmul */
1154 COSTS_N_INSNS (17), /* sdiv */
1155 COSTS_N_INSNS (17), /* ddiv */
1156 128, /* cache line size */
1157 32, /* l1 cache */
1158 1024, /* l2 cache */
1159 8, /* prefetch streams /*/
1160 0, /* SF->DF convert */
1163 /* Instruction costs on POWER6 processors. */
1164 static const
1165 struct processor_costs power6_cost = {
1166 COSTS_N_INSNS (8), /* mulsi */
1167 COSTS_N_INSNS (8), /* mulsi_const */
1168 COSTS_N_INSNS (8), /* mulsi_const9 */
1169 COSTS_N_INSNS (8), /* muldi */
1170 COSTS_N_INSNS (22), /* divsi */
1171 COSTS_N_INSNS (28), /* divdi */
1172 COSTS_N_INSNS (3), /* fp */
1173 COSTS_N_INSNS (3), /* dmul */
1174 COSTS_N_INSNS (13), /* sdiv */
1175 COSTS_N_INSNS (16), /* ddiv */
1176 128, /* cache line size */
1177 64, /* l1 cache */
1178 2048, /* l2 cache */
1179 16, /* prefetch streams */
1180 0, /* SF->DF convert */
1183 /* Instruction costs on POWER7 processors. */
1184 static const
1185 struct processor_costs power7_cost = {
1186 COSTS_N_INSNS (2), /* mulsi */
1187 COSTS_N_INSNS (2), /* mulsi_const */
1188 COSTS_N_INSNS (2), /* mulsi_const9 */
1189 COSTS_N_INSNS (2), /* muldi */
1190 COSTS_N_INSNS (18), /* divsi */
1191 COSTS_N_INSNS (34), /* divdi */
1192 COSTS_N_INSNS (3), /* fp */
1193 COSTS_N_INSNS (3), /* dmul */
1194 COSTS_N_INSNS (13), /* sdiv */
1195 COSTS_N_INSNS (16), /* ddiv */
1196 128, /* cache line size */
1197 32, /* l1 cache */
1198 256, /* l2 cache */
1199 12, /* prefetch streams */
1200 COSTS_N_INSNS (3), /* SF->DF convert */
1203 /* Instruction costs on POWER8 processors. */
1204 static const
1205 struct processor_costs power8_cost = {
1206 COSTS_N_INSNS (3), /* mulsi */
1207 COSTS_N_INSNS (3), /* mulsi_const */
1208 COSTS_N_INSNS (3), /* mulsi_const9 */
1209 COSTS_N_INSNS (3), /* muldi */
1210 COSTS_N_INSNS (19), /* divsi */
1211 COSTS_N_INSNS (35), /* divdi */
1212 COSTS_N_INSNS (3), /* fp */
1213 COSTS_N_INSNS (3), /* dmul */
1214 COSTS_N_INSNS (14), /* sdiv */
1215 COSTS_N_INSNS (17), /* ddiv */
1216 128, /* cache line size */
1217 32, /* l1 cache */
1218 256, /* l2 cache */
1219 12, /* prefetch streams */
1220 COSTS_N_INSNS (3), /* SF->DF convert */
1223 /* Instruction costs on POWER9 processors. */
1224 static const
1225 struct processor_costs power9_cost = {
1226 COSTS_N_INSNS (3), /* mulsi */
1227 COSTS_N_INSNS (3), /* mulsi_const */
1228 COSTS_N_INSNS (3), /* mulsi_const9 */
1229 COSTS_N_INSNS (3), /* muldi */
1230 COSTS_N_INSNS (8), /* divsi */
1231 COSTS_N_INSNS (12), /* divdi */
1232 COSTS_N_INSNS (3), /* fp */
1233 COSTS_N_INSNS (3), /* dmul */
1234 COSTS_N_INSNS (13), /* sdiv */
1235 COSTS_N_INSNS (18), /* ddiv */
1236 128, /* cache line size */
1237 32, /* l1 cache */
1238 512, /* l2 cache */
1239 8, /* prefetch streams */
1240 COSTS_N_INSNS (3), /* SF->DF convert */
1243 /* Instruction costs on POWER A2 processors. */
1244 static const
1245 struct processor_costs ppca2_cost = {
1246 COSTS_N_INSNS (16), /* mulsi */
1247 COSTS_N_INSNS (16), /* mulsi_const */
1248 COSTS_N_INSNS (16), /* mulsi_const9 */
1249 COSTS_N_INSNS (16), /* muldi */
1250 COSTS_N_INSNS (22), /* divsi */
1251 COSTS_N_INSNS (28), /* divdi */
1252 COSTS_N_INSNS (3), /* fp */
1253 COSTS_N_INSNS (3), /* dmul */
1254 COSTS_N_INSNS (59), /* sdiv */
1255 COSTS_N_INSNS (72), /* ddiv */
1257 16, /* l1 cache */
1258 2048, /* l2 cache */
1259 16, /* prefetch streams */
1260 0, /* SF->DF convert */
1264 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
1265 #undef RS6000_BUILTIN_0
1266 #undef RS6000_BUILTIN_1
1267 #undef RS6000_BUILTIN_2
1268 #undef RS6000_BUILTIN_3
1269 #undef RS6000_BUILTIN_A
1270 #undef RS6000_BUILTIN_D
1271 #undef RS6000_BUILTIN_H
1272 #undef RS6000_BUILTIN_P
1273 #undef RS6000_BUILTIN_Q
1274 #undef RS6000_BUILTIN_X
1276 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE) \
1277 { NAME, ICODE, MASK, ATTR },
1279 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
1280 { NAME, ICODE, MASK, ATTR },
1282 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
1283 { NAME, ICODE, MASK, ATTR },
1285 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
1286 { NAME, ICODE, MASK, ATTR },
1288 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
1289 { NAME, ICODE, MASK, ATTR },
1291 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
1292 { NAME, ICODE, MASK, ATTR },
1294 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
1295 { NAME, ICODE, MASK, ATTR },
1297 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
1298 { NAME, ICODE, MASK, ATTR },
1300 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
1301 { NAME, ICODE, MASK, ATTR },
1303 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
1304 { NAME, ICODE, MASK, ATTR },
1306 struct rs6000_builtin_info_type {
1307 const char *name;
1308 const enum insn_code icode;
1309 const HOST_WIDE_INT mask;
1310 const unsigned attr;
1313 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1315 #include "rs6000-builtin.def"
1318 #undef RS6000_BUILTIN_0
1319 #undef RS6000_BUILTIN_1
1320 #undef RS6000_BUILTIN_2
1321 #undef RS6000_BUILTIN_3
1322 #undef RS6000_BUILTIN_A
1323 #undef RS6000_BUILTIN_D
1324 #undef RS6000_BUILTIN_H
1325 #undef RS6000_BUILTIN_P
1326 #undef RS6000_BUILTIN_Q
1327 #undef RS6000_BUILTIN_X
1329 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
1330 static tree (*rs6000_veclib_handler) (combined_fn, tree, tree);
1333 static bool rs6000_debug_legitimate_address_p (machine_mode, rtx, bool);
1334 static struct machine_function * rs6000_init_machine_status (void);
1335 static int rs6000_ra_ever_killed (void);
1336 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1337 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1338 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1339 static tree rs6000_builtin_vectorized_libmass (combined_fn, tree, tree);
1340 static void rs6000_emit_set_long_const (rtx, HOST_WIDE_INT);
1341 static int rs6000_memory_move_cost (machine_mode, reg_class_t, bool);
1342 static bool rs6000_debug_rtx_costs (rtx, machine_mode, int, int, int *, bool);
1343 static int rs6000_debug_address_cost (rtx, machine_mode, addr_space_t,
1344 bool);
1345 static int rs6000_debug_adjust_cost (rtx_insn *, int, rtx_insn *, int,
1346 unsigned int);
1347 static bool is_microcoded_insn (rtx_insn *);
1348 static bool is_nonpipeline_insn (rtx_insn *);
1349 static bool is_cracked_insn (rtx_insn *);
1350 static bool is_load_insn (rtx, rtx *);
1351 static bool is_store_insn (rtx, rtx *);
1352 static bool set_to_load_agen (rtx_insn *,rtx_insn *);
1353 static bool insn_terminates_group_p (rtx_insn *, enum group_termination);
1354 static bool insn_must_be_first_in_group (rtx_insn *);
1355 static bool insn_must_be_last_in_group (rtx_insn *);
1356 static void altivec_init_builtins (void);
1357 static tree builtin_function_type (machine_mode, machine_mode,
1358 machine_mode, machine_mode,
1359 enum rs6000_builtins, const char *name);
1360 static void rs6000_common_init_builtins (void);
1361 static void paired_init_builtins (void);
1362 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1363 static void htm_init_builtins (void);
1364 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1365 static rs6000_stack_t *rs6000_stack_info (void);
1366 static void is_altivec_return_reg (rtx, void *);
1367 int easy_vector_constant (rtx, machine_mode);
1368 static rtx rs6000_debug_legitimize_address (rtx, rtx, machine_mode);
1369 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1370 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1371 bool, bool);
1372 #if TARGET_MACHO
1373 static void macho_branch_islands (void);
1374 #endif
1375 static rtx rs6000_legitimize_reload_address (rtx, machine_mode, int, int,
1376 int, int *);
1377 static rtx rs6000_debug_legitimize_reload_address (rtx, machine_mode, int,
1378 int, int, int *);
1379 static bool rs6000_mode_dependent_address (const_rtx);
1380 static bool rs6000_debug_mode_dependent_address (const_rtx);
1381 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1382 machine_mode, rtx);
1383 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1384 machine_mode,
1385 rtx);
1386 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1387 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1388 enum reg_class);
1389 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1390 machine_mode);
1391 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1392 enum reg_class,
1393 machine_mode);
1394 static bool rs6000_cannot_change_mode_class (machine_mode,
1395 machine_mode,
1396 enum reg_class);
1397 static bool rs6000_debug_cannot_change_mode_class (machine_mode,
1398 machine_mode,
1399 enum reg_class);
1400 static bool rs6000_save_toc_in_prologue_p (void);
1401 static rtx rs6000_internal_arg_pointer (void);
1403 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, machine_mode, int, int,
1404 int, int *)
1405 = rs6000_legitimize_reload_address;
1407 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1408 = rs6000_mode_dependent_address;
1410 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1411 machine_mode, rtx)
1412 = rs6000_secondary_reload_class;
1414 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1415 = rs6000_preferred_reload_class;
1417 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1418 machine_mode)
1419 = rs6000_secondary_memory_needed;
1421 bool (*rs6000_cannot_change_mode_class_ptr) (machine_mode,
1422 machine_mode,
1423 enum reg_class)
1424 = rs6000_cannot_change_mode_class;
1426 const int INSN_NOT_AVAILABLE = -1;
1428 static void rs6000_print_isa_options (FILE *, int, const char *,
1429 HOST_WIDE_INT);
1430 static void rs6000_print_builtin_options (FILE *, int, const char *,
1431 HOST_WIDE_INT);
1432 static HOST_WIDE_INT rs6000_disable_incompatible_switches (void);
1434 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1435 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1436 enum rs6000_reg_type,
1437 machine_mode,
1438 secondary_reload_info *,
1439 bool);
1440 rtl_opt_pass *make_pass_analyze_swaps (gcc::context*);
1441 static bool rs6000_keep_leaf_when_profiled () __attribute__ ((unused));
1442 static tree rs6000_fold_builtin (tree, int, tree *, bool);
1444 /* Hash table stuff for keeping track of TOC entries. */
1446 struct GTY((for_user)) toc_hash_struct
1448 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1449 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1450 rtx key;
1451 machine_mode key_mode;
1452 int labelno;
1455 struct toc_hasher : ggc_ptr_hash<toc_hash_struct>
1457 static hashval_t hash (toc_hash_struct *);
1458 static bool equal (toc_hash_struct *, toc_hash_struct *);
1461 static GTY (()) hash_table<toc_hasher> *toc_hash_table;
1463 /* Hash table to keep track of the argument types for builtin functions. */
1465 struct GTY((for_user)) builtin_hash_struct
1467 tree type;
1468 machine_mode mode[4]; /* return value + 3 arguments. */
1469 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1472 struct builtin_hasher : ggc_ptr_hash<builtin_hash_struct>
1474 static hashval_t hash (builtin_hash_struct *);
1475 static bool equal (builtin_hash_struct *, builtin_hash_struct *);
1478 static GTY (()) hash_table<builtin_hasher> *builtin_hash_table;
1481 /* Default register names. */
1482 char rs6000_reg_names[][8] =
1484 "0", "1", "2", "3", "4", "5", "6", "7",
1485 "8", "9", "10", "11", "12", "13", "14", "15",
1486 "16", "17", "18", "19", "20", "21", "22", "23",
1487 "24", "25", "26", "27", "28", "29", "30", "31",
1488 "0", "1", "2", "3", "4", "5", "6", "7",
1489 "8", "9", "10", "11", "12", "13", "14", "15",
1490 "16", "17", "18", "19", "20", "21", "22", "23",
1491 "24", "25", "26", "27", "28", "29", "30", "31",
1492 "mq", "lr", "ctr","ap",
1493 "0", "1", "2", "3", "4", "5", "6", "7",
1494 "ca",
1495 /* AltiVec registers. */
1496 "0", "1", "2", "3", "4", "5", "6", "7",
1497 "8", "9", "10", "11", "12", "13", "14", "15",
1498 "16", "17", "18", "19", "20", "21", "22", "23",
1499 "24", "25", "26", "27", "28", "29", "30", "31",
1500 "vrsave", "vscr",
1501 /* Soft frame pointer. */
1502 "sfp",
1503 /* HTM SPR registers. */
1504 "tfhar", "tfiar", "texasr"
1507 #ifdef TARGET_REGNAMES
1508 static const char alt_reg_names[][8] =
1510 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1511 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1512 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1513 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1514 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1515 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1516 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1517 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1518 "mq", "lr", "ctr", "ap",
1519 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1520 "ca",
1521 /* AltiVec registers. */
1522 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1523 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1524 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1525 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1526 "vrsave", "vscr",
1527 /* Soft frame pointer. */
1528 "sfp",
1529 /* HTM SPR registers. */
1530 "tfhar", "tfiar", "texasr"
1532 #endif
1534 /* Table of valid machine attributes. */
1536 static const struct attribute_spec rs6000_attribute_table[] =
1538 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1539 affects_type_identity } */
1540 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1541 false },
1542 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1543 false },
1544 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1545 false },
1546 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1547 false },
1548 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1549 false },
1550 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1551 SUBTARGET_ATTRIBUTE_TABLE,
1552 #endif
1553 { NULL, 0, 0, false, false, false, NULL, false }
1556 #ifndef TARGET_PROFILE_KERNEL
1557 #define TARGET_PROFILE_KERNEL 0
1558 #endif
1560 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1561 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1563 /* Initialize the GCC target structure. */
1564 #undef TARGET_ATTRIBUTE_TABLE
1565 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1566 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1567 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1568 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1569 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1571 #undef TARGET_ASM_ALIGNED_DI_OP
1572 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1574 /* Default unaligned ops are only provided for ELF. Find the ops needed
1575 for non-ELF systems. */
1576 #ifndef OBJECT_FORMAT_ELF
1577 #if TARGET_XCOFF
1578 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1579 64-bit targets. */
1580 #undef TARGET_ASM_UNALIGNED_HI_OP
1581 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1582 #undef TARGET_ASM_UNALIGNED_SI_OP
1583 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1584 #undef TARGET_ASM_UNALIGNED_DI_OP
1585 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1586 #else
1587 /* For Darwin. */
1588 #undef TARGET_ASM_UNALIGNED_HI_OP
1589 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1590 #undef TARGET_ASM_UNALIGNED_SI_OP
1591 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1592 #undef TARGET_ASM_UNALIGNED_DI_OP
1593 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1594 #undef TARGET_ASM_ALIGNED_DI_OP
1595 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1596 #endif
1597 #endif
1599 /* This hook deals with fixups for relocatable code and DI-mode objects
1600 in 64-bit code. */
1601 #undef TARGET_ASM_INTEGER
1602 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1604 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1605 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1606 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1607 #endif
1609 #undef TARGET_SET_UP_BY_PROLOGUE
1610 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1612 #undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
1613 #define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS rs6000_get_separate_components
1614 #undef TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB
1615 #define TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB rs6000_components_for_bb
1616 #undef TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS
1617 #define TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS rs6000_disqualify_components
1618 #undef TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS
1619 #define TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS rs6000_emit_prologue_components
1620 #undef TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS
1621 #define TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS rs6000_emit_epilogue_components
1622 #undef TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS
1623 #define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS rs6000_set_handled_components
1625 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1626 #define TARGET_EXTRA_LIVE_ON_ENTRY rs6000_live_on_entry
1628 #undef TARGET_INTERNAL_ARG_POINTER
1629 #define TARGET_INTERNAL_ARG_POINTER rs6000_internal_arg_pointer
1631 #undef TARGET_HAVE_TLS
1632 #define TARGET_HAVE_TLS HAVE_AS_TLS
1634 #undef TARGET_CANNOT_FORCE_CONST_MEM
1635 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1637 #undef TARGET_DELEGITIMIZE_ADDRESS
1638 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1640 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1641 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1643 #undef TARGET_LEGITIMATE_COMBINED_INSN
1644 #define TARGET_LEGITIMATE_COMBINED_INSN rs6000_legitimate_combined_insn
1646 #undef TARGET_ASM_FUNCTION_PROLOGUE
1647 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1648 #undef TARGET_ASM_FUNCTION_EPILOGUE
1649 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1651 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1652 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1654 #undef TARGET_LEGITIMIZE_ADDRESS
1655 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1657 #undef TARGET_SCHED_VARIABLE_ISSUE
1658 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1660 #undef TARGET_SCHED_ISSUE_RATE
1661 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1662 #undef TARGET_SCHED_ADJUST_COST
1663 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1664 #undef TARGET_SCHED_ADJUST_PRIORITY
1665 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1666 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1667 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1668 #undef TARGET_SCHED_INIT
1669 #define TARGET_SCHED_INIT rs6000_sched_init
1670 #undef TARGET_SCHED_FINISH
1671 #define TARGET_SCHED_FINISH rs6000_sched_finish
1672 #undef TARGET_SCHED_REORDER
1673 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1674 #undef TARGET_SCHED_REORDER2
1675 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1677 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1678 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1680 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1681 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1683 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1684 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1685 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1686 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1687 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1688 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1689 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1690 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1692 #undef TARGET_SCHED_CAN_SPECULATE_INSN
1693 #define TARGET_SCHED_CAN_SPECULATE_INSN rs6000_sched_can_speculate_insn
1695 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1696 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1697 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1698 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1699 rs6000_builtin_support_vector_misalignment
1700 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1701 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1702 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1703 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1704 rs6000_builtin_vectorization_cost
1705 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1706 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1707 rs6000_preferred_simd_mode
1708 #undef TARGET_VECTORIZE_INIT_COST
1709 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1710 #undef TARGET_VECTORIZE_ADD_STMT_COST
1711 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1712 #undef TARGET_VECTORIZE_FINISH_COST
1713 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1714 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1715 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1717 #undef TARGET_INIT_BUILTINS
1718 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1719 #undef TARGET_BUILTIN_DECL
1720 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1722 #undef TARGET_FOLD_BUILTIN
1723 #define TARGET_FOLD_BUILTIN rs6000_fold_builtin
1724 #undef TARGET_GIMPLE_FOLD_BUILTIN
1725 #define TARGET_GIMPLE_FOLD_BUILTIN rs6000_gimple_fold_builtin
1727 #undef TARGET_EXPAND_BUILTIN
1728 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1730 #undef TARGET_MANGLE_TYPE
1731 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1733 #undef TARGET_INIT_LIBFUNCS
1734 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1736 #if TARGET_MACHO
1737 #undef TARGET_BINDS_LOCAL_P
1738 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1739 #endif
1741 #undef TARGET_MS_BITFIELD_LAYOUT_P
1742 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1744 #undef TARGET_ASM_OUTPUT_MI_THUNK
1745 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1747 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1748 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1750 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1751 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1753 #undef TARGET_REGISTER_MOVE_COST
1754 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1755 #undef TARGET_MEMORY_MOVE_COST
1756 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1757 #undef TARGET_CANNOT_COPY_INSN_P
1758 #define TARGET_CANNOT_COPY_INSN_P rs6000_cannot_copy_insn_p
1759 #undef TARGET_RTX_COSTS
1760 #define TARGET_RTX_COSTS rs6000_rtx_costs
1761 #undef TARGET_ADDRESS_COST
1762 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1764 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1765 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1767 #undef TARGET_PROMOTE_FUNCTION_MODE
1768 #define TARGET_PROMOTE_FUNCTION_MODE rs6000_promote_function_mode
1770 #undef TARGET_RETURN_IN_MEMORY
1771 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1773 #undef TARGET_RETURN_IN_MSB
1774 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1776 #undef TARGET_SETUP_INCOMING_VARARGS
1777 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1779 /* Always strict argument naming on rs6000. */
1780 #undef TARGET_STRICT_ARGUMENT_NAMING
1781 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1782 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1783 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1784 #undef TARGET_SPLIT_COMPLEX_ARG
1785 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1786 #undef TARGET_MUST_PASS_IN_STACK
1787 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1788 #undef TARGET_PASS_BY_REFERENCE
1789 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1790 #undef TARGET_ARG_PARTIAL_BYTES
1791 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1792 #undef TARGET_FUNCTION_ARG_ADVANCE
1793 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1794 #undef TARGET_FUNCTION_ARG
1795 #define TARGET_FUNCTION_ARG rs6000_function_arg
1796 #undef TARGET_FUNCTION_ARG_BOUNDARY
1797 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1799 #undef TARGET_BUILD_BUILTIN_VA_LIST
1800 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1802 #undef TARGET_EXPAND_BUILTIN_VA_START
1803 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1805 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1806 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1808 #undef TARGET_EH_RETURN_FILTER_MODE
1809 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1811 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1812 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1814 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1815 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1817 #undef TARGET_FLOATN_MODE
1818 #define TARGET_FLOATN_MODE rs6000_floatn_mode
1820 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1821 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1823 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1824 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1826 #undef TARGET_MD_ASM_ADJUST
1827 #define TARGET_MD_ASM_ADJUST rs6000_md_asm_adjust
1829 #undef TARGET_OPTION_OVERRIDE
1830 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1832 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1833 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1834 rs6000_builtin_vectorized_function
1836 #undef TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION
1837 #define TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION \
1838 rs6000_builtin_md_vectorized_function
1840 #undef TARGET_STACK_PROTECT_GUARD
1841 #define TARGET_STACK_PROTECT_GUARD rs6000_init_stack_protect_guard
1843 #if !TARGET_MACHO
1844 #undef TARGET_STACK_PROTECT_FAIL
1845 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1846 #endif
1848 #ifdef HAVE_AS_TLS
1849 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1850 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1851 #endif
1853 /* Use a 32-bit anchor range. This leads to sequences like:
1855 addis tmp,anchor,high
1856 add dest,tmp,low
1858 where tmp itself acts as an anchor, and can be shared between
1859 accesses to the same 64k page. */
1860 #undef TARGET_MIN_ANCHOR_OFFSET
1861 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1862 #undef TARGET_MAX_ANCHOR_OFFSET
1863 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1864 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1865 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1866 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1867 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1869 #undef TARGET_BUILTIN_RECIPROCAL
1870 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1872 #undef TARGET_SECONDARY_RELOAD
1873 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1875 #undef TARGET_LEGITIMATE_ADDRESS_P
1876 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1878 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1879 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1881 #undef TARGET_COMPUTE_PRESSURE_CLASSES
1882 #define TARGET_COMPUTE_PRESSURE_CLASSES rs6000_compute_pressure_classes
1884 #undef TARGET_CAN_ELIMINATE
1885 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1887 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1888 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1890 #undef TARGET_SCHED_REASSOCIATION_WIDTH
1891 #define TARGET_SCHED_REASSOCIATION_WIDTH rs6000_reassociation_width
1893 #undef TARGET_TRAMPOLINE_INIT
1894 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1896 #undef TARGET_FUNCTION_VALUE
1897 #define TARGET_FUNCTION_VALUE rs6000_function_value
1899 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1900 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1902 #undef TARGET_OPTION_SAVE
1903 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1905 #undef TARGET_OPTION_RESTORE
1906 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1908 #undef TARGET_OPTION_PRINT
1909 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1911 #undef TARGET_CAN_INLINE_P
1912 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1914 #undef TARGET_SET_CURRENT_FUNCTION
1915 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1917 #undef TARGET_LEGITIMATE_CONSTANT_P
1918 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1920 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1921 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1923 #undef TARGET_CAN_USE_DOLOOP_P
1924 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1926 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
1927 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV rs6000_atomic_assign_expand_fenv
1929 #undef TARGET_LIBGCC_CMP_RETURN_MODE
1930 #define TARGET_LIBGCC_CMP_RETURN_MODE rs6000_abi_word_mode
1931 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
1932 #define TARGET_LIBGCC_SHIFT_COUNT_MODE rs6000_abi_word_mode
1933 #undef TARGET_UNWIND_WORD_MODE
1934 #define TARGET_UNWIND_WORD_MODE rs6000_abi_word_mode
1936 #undef TARGET_OFFLOAD_OPTIONS
1937 #define TARGET_OFFLOAD_OPTIONS rs6000_offload_options
1939 #undef TARGET_C_MODE_FOR_SUFFIX
1940 #define TARGET_C_MODE_FOR_SUFFIX rs6000_c_mode_for_suffix
1942 #undef TARGET_INVALID_BINARY_OP
1943 #define TARGET_INVALID_BINARY_OP rs6000_invalid_binary_op
1945 #undef TARGET_OPTAB_SUPPORTED_P
1946 #define TARGET_OPTAB_SUPPORTED_P rs6000_optab_supported_p
1948 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
1949 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
1951 #undef TARGET_COMPARE_VERSION_PRIORITY
1952 #define TARGET_COMPARE_VERSION_PRIORITY rs6000_compare_version_priority
1954 #undef TARGET_GENERATE_VERSION_DISPATCHER_BODY
1955 #define TARGET_GENERATE_VERSION_DISPATCHER_BODY \
1956 rs6000_generate_version_dispatcher_body
1958 #undef TARGET_GET_FUNCTION_VERSIONS_DISPATCHER
1959 #define TARGET_GET_FUNCTION_VERSIONS_DISPATCHER \
1960 rs6000_get_function_versions_dispatcher
1962 #undef TARGET_OPTION_FUNCTION_VERSIONS
1963 #define TARGET_OPTION_FUNCTION_VERSIONS common_function_versions
1967 /* Processor table. */
1968 struct rs6000_ptt
1970 const char *const name; /* Canonical processor name. */
1971 const enum processor_type processor; /* Processor type enum value. */
1972 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1975 static struct rs6000_ptt const processor_target_table[] =
1977 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1978 #include "rs6000-cpus.def"
1979 #undef RS6000_CPU
1982 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1983 name is invalid. */
1985 static int
1986 rs6000_cpu_name_lookup (const char *name)
1988 size_t i;
1990 if (name != NULL)
1992 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1993 if (! strcmp (name, processor_target_table[i].name))
1994 return (int)i;
1997 return -1;
2001 /* Return number of consecutive hard regs needed starting at reg REGNO
2002 to hold something of mode MODE.
2003 This is ordinarily the length in words of a value of mode MODE
2004 but can be less for certain modes in special long registers.
2006 POWER and PowerPC GPRs hold 32 bits worth;
2007 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
2009 static int
2010 rs6000_hard_regno_nregs_internal (int regno, machine_mode mode)
2012 unsigned HOST_WIDE_INT reg_size;
2014 /* 128-bit floating point usually takes 2 registers, unless it is IEEE
2015 128-bit floating point that can go in vector registers, which has VSX
2016 memory addressing. */
2017 if (FP_REGNO_P (regno))
2018 reg_size = (VECTOR_MEM_VSX_P (mode) || FLOAT128_VECTOR_P (mode)
2019 ? UNITS_PER_VSX_WORD
2020 : UNITS_PER_FP_WORD);
2022 else if (ALTIVEC_REGNO_P (regno))
2023 reg_size = UNITS_PER_ALTIVEC_WORD;
2025 else
2026 reg_size = UNITS_PER_WORD;
2028 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
2031 /* Value is 1 if hard register REGNO can hold a value of machine-mode
2032 MODE. */
2033 static int
2034 rs6000_hard_regno_mode_ok (int regno, machine_mode mode)
2036 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
2038 if (COMPLEX_MODE_P (mode))
2039 mode = GET_MODE_INNER (mode);
2041 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
2042 register combinations, and use PTImode where we need to deal with quad
2043 word memory operations. Don't allow quad words in the argument or frame
2044 pointer registers, just registers 0..31. */
2045 if (mode == PTImode)
2046 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2047 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2048 && ((regno & 1) == 0));
2050 /* VSX registers that overlap the FPR registers are larger than for non-VSX
2051 implementations. Don't allow an item to be split between a FP register
2052 and an Altivec register. Allow TImode in all VSX registers if the user
2053 asked for it. */
2054 if (TARGET_VSX && VSX_REGNO_P (regno)
2055 && (VECTOR_MEM_VSX_P (mode)
2056 || FLOAT128_VECTOR_P (mode)
2057 || reg_addr[mode].scalar_in_vmx_p
2058 || mode == TImode
2059 || (TARGET_VADDUQM && mode == V1TImode)))
2061 if (FP_REGNO_P (regno))
2062 return FP_REGNO_P (last_regno);
2064 if (ALTIVEC_REGNO_P (regno))
2066 if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
2067 return 0;
2069 return ALTIVEC_REGNO_P (last_regno);
2073 /* The GPRs can hold any mode, but values bigger than one register
2074 cannot go past R31. */
2075 if (INT_REGNO_P (regno))
2076 return INT_REGNO_P (last_regno);
2078 /* The float registers (except for VSX vector modes) can only hold floating
2079 modes and DImode. */
2080 if (FP_REGNO_P (regno))
2082 if (FLOAT128_VECTOR_P (mode))
2083 return false;
2085 if (SCALAR_FLOAT_MODE_P (mode)
2086 && (mode != TDmode || (regno % 2) == 0)
2087 && FP_REGNO_P (last_regno))
2088 return 1;
2090 if (GET_MODE_CLASS (mode) == MODE_INT)
2092 if(GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
2093 return 1;
2095 if (TARGET_P8_VECTOR && (mode == SImode))
2096 return 1;
2098 if (TARGET_P9_VECTOR && (mode == QImode || mode == HImode))
2099 return 1;
2102 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
2103 && PAIRED_VECTOR_MODE (mode))
2104 return 1;
2106 return 0;
2109 /* The CR register can only hold CC modes. */
2110 if (CR_REGNO_P (regno))
2111 return GET_MODE_CLASS (mode) == MODE_CC;
2113 if (CA_REGNO_P (regno))
2114 return mode == Pmode || mode == SImode;
2116 /* AltiVec only in AldyVec registers. */
2117 if (ALTIVEC_REGNO_P (regno))
2118 return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
2119 || mode == V1TImode);
2121 /* We cannot put non-VSX TImode or PTImode anywhere except general register
2122 and it must be able to fit within the register set. */
2124 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
2127 /* Print interesting facts about registers. */
2128 static void
2129 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
2131 int r, m;
2133 for (r = first_regno; r <= last_regno; ++r)
2135 const char *comma = "";
2136 int len;
2138 if (first_regno == last_regno)
2139 fprintf (stderr, "%s:\t", reg_name);
2140 else
2141 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
2143 len = 8;
2144 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2145 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
2147 if (len > 70)
2149 fprintf (stderr, ",\n\t");
2150 len = 8;
2151 comma = "";
2154 if (rs6000_hard_regno_nregs[m][r] > 1)
2155 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
2156 rs6000_hard_regno_nregs[m][r]);
2157 else
2158 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
2160 comma = ", ";
2163 if (call_used_regs[r])
2165 if (len > 70)
2167 fprintf (stderr, ",\n\t");
2168 len = 8;
2169 comma = "";
2172 len += fprintf (stderr, "%s%s", comma, "call-used");
2173 comma = ", ";
2176 if (fixed_regs[r])
2178 if (len > 70)
2180 fprintf (stderr, ",\n\t");
2181 len = 8;
2182 comma = "";
2185 len += fprintf (stderr, "%s%s", comma, "fixed");
2186 comma = ", ";
2189 if (len > 70)
2191 fprintf (stderr, ",\n\t");
2192 comma = "";
2195 len += fprintf (stderr, "%sreg-class = %s", comma,
2196 reg_class_names[(int)rs6000_regno_regclass[r]]);
2197 comma = ", ";
2199 if (len > 70)
2201 fprintf (stderr, ",\n\t");
2202 comma = "";
2205 fprintf (stderr, "%sregno = %d\n", comma, r);
2209 static const char *
2210 rs6000_debug_vector_unit (enum rs6000_vector v)
2212 const char *ret;
2214 switch (v)
2216 case VECTOR_NONE: ret = "none"; break;
2217 case VECTOR_ALTIVEC: ret = "altivec"; break;
2218 case VECTOR_VSX: ret = "vsx"; break;
2219 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
2220 case VECTOR_PAIRED: ret = "paired"; break;
2221 case VECTOR_OTHER: ret = "other"; break;
2222 default: ret = "unknown"; break;
2225 return ret;
2228 /* Inner function printing just the address mask for a particular reload
2229 register class. */
2230 DEBUG_FUNCTION char *
2231 rs6000_debug_addr_mask (addr_mask_type mask, bool keep_spaces)
2233 static char ret[8];
2234 char *p = ret;
2236 if ((mask & RELOAD_REG_VALID) != 0)
2237 *p++ = 'v';
2238 else if (keep_spaces)
2239 *p++ = ' ';
2241 if ((mask & RELOAD_REG_MULTIPLE) != 0)
2242 *p++ = 'm';
2243 else if (keep_spaces)
2244 *p++ = ' ';
2246 if ((mask & RELOAD_REG_INDEXED) != 0)
2247 *p++ = 'i';
2248 else if (keep_spaces)
2249 *p++ = ' ';
2251 if ((mask & RELOAD_REG_QUAD_OFFSET) != 0)
2252 *p++ = 'O';
2253 else if ((mask & RELOAD_REG_OFFSET) != 0)
2254 *p++ = 'o';
2255 else if (keep_spaces)
2256 *p++ = ' ';
2258 if ((mask & RELOAD_REG_PRE_INCDEC) != 0)
2259 *p++ = '+';
2260 else if (keep_spaces)
2261 *p++ = ' ';
2263 if ((mask & RELOAD_REG_PRE_MODIFY) != 0)
2264 *p++ = '+';
2265 else if (keep_spaces)
2266 *p++ = ' ';
2268 if ((mask & RELOAD_REG_AND_M16) != 0)
2269 *p++ = '&';
2270 else if (keep_spaces)
2271 *p++ = ' ';
2273 *p = '\0';
2275 return ret;
2278 /* Print the address masks in a human readble fashion. */
2279 DEBUG_FUNCTION void
2280 rs6000_debug_print_mode (ssize_t m)
2282 ssize_t rc;
2283 int spaces = 0;
2284 bool fuse_extra_p;
2286 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
2287 for (rc = 0; rc < N_RELOAD_REG; rc++)
2288 fprintf (stderr, " %s: %s", reload_reg_map[rc].name,
2289 rs6000_debug_addr_mask (reg_addr[m].addr_mask[rc], true));
2291 if ((reg_addr[m].reload_store != CODE_FOR_nothing)
2292 || (reg_addr[m].reload_load != CODE_FOR_nothing))
2293 fprintf (stderr, " Reload=%c%c",
2294 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
2295 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
2296 else
2297 spaces += sizeof (" Reload=sl") - 1;
2299 if (reg_addr[m].scalar_in_vmx_p)
2301 fprintf (stderr, "%*s Upper=y", spaces, "");
2302 spaces = 0;
2304 else
2305 spaces += sizeof (" Upper=y") - 1;
2307 fuse_extra_p = ((reg_addr[m].fusion_gpr_ld != CODE_FOR_nothing)
2308 || reg_addr[m].fused_toc);
2309 if (!fuse_extra_p)
2311 for (rc = 0; rc < N_RELOAD_REG; rc++)
2313 if (rc != RELOAD_REG_ANY)
2315 if (reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing
2316 || reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing
2317 || reg_addr[m].fusion_addi_st[rc] != CODE_FOR_nothing
2318 || reg_addr[m].fusion_addis_ld[rc] != CODE_FOR_nothing
2319 || reg_addr[m].fusion_addis_st[rc] != CODE_FOR_nothing)
2321 fuse_extra_p = true;
2322 break;
2328 if (fuse_extra_p)
2330 fprintf (stderr, "%*s Fuse:", spaces, "");
2331 spaces = 0;
2333 for (rc = 0; rc < N_RELOAD_REG; rc++)
2335 if (rc != RELOAD_REG_ANY)
2337 char load, store;
2339 if (reg_addr[m].fusion_addis_ld[rc] != CODE_FOR_nothing)
2340 load = 'l';
2341 else if (reg_addr[m].fusion_addi_ld[rc] != CODE_FOR_nothing)
2342 load = 'L';
2343 else
2344 load = '-';
2346 if (reg_addr[m].fusion_addis_st[rc] != CODE_FOR_nothing)
2347 store = 's';
2348 else if (reg_addr[m].fusion_addi_st[rc] != CODE_FOR_nothing)
2349 store = 'S';
2350 else
2351 store = '-';
2353 if (load == '-' && store == '-')
2354 spaces += 5;
2355 else
2357 fprintf (stderr, "%*s%c=%c%c", (spaces + 1), "",
2358 reload_reg_map[rc].name[0], load, store);
2359 spaces = 0;
2364 if (reg_addr[m].fusion_gpr_ld != CODE_FOR_nothing)
2366 fprintf (stderr, "%*sP8gpr", (spaces + 1), "");
2367 spaces = 0;
2369 else
2370 spaces += sizeof (" P8gpr") - 1;
2372 if (reg_addr[m].fused_toc)
2374 fprintf (stderr, "%*sToc", (spaces + 1), "");
2375 spaces = 0;
2377 else
2378 spaces += sizeof (" Toc") - 1;
2380 else
2381 spaces += sizeof (" Fuse: G=ls F=ls v=ls P8gpr Toc") - 1;
2383 if (rs6000_vector_unit[m] != VECTOR_NONE
2384 || rs6000_vector_mem[m] != VECTOR_NONE)
2386 fprintf (stderr, "%*s vector: arith=%-10s mem=%s",
2387 spaces, "",
2388 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
2389 rs6000_debug_vector_unit (rs6000_vector_mem[m]));
2392 fputs ("\n", stderr);
2395 #define DEBUG_FMT_ID "%-32s= "
2396 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
2397 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
2398 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
2400 /* Print various interesting information with -mdebug=reg. */
2401 static void
2402 rs6000_debug_reg_global (void)
2404 static const char *const tf[2] = { "false", "true" };
2405 const char *nl = (const char *)0;
2406 int m;
2407 size_t m1, m2, v;
2408 char costly_num[20];
2409 char nop_num[20];
2410 char flags_buffer[40];
2411 const char *costly_str;
2412 const char *nop_str;
2413 const char *trace_str;
2414 const char *abi_str;
2415 const char *cmodel_str;
2416 struct cl_target_option cl_opts;
2418 /* Modes we want tieable information on. */
2419 static const machine_mode print_tieable_modes[] = {
2420 QImode,
2421 HImode,
2422 SImode,
2423 DImode,
2424 TImode,
2425 PTImode,
2426 SFmode,
2427 DFmode,
2428 TFmode,
2429 IFmode,
2430 KFmode,
2431 SDmode,
2432 DDmode,
2433 TDmode,
2434 V2SImode,
2435 V16QImode,
2436 V8HImode,
2437 V4SImode,
2438 V2DImode,
2439 V1TImode,
2440 V32QImode,
2441 V16HImode,
2442 V8SImode,
2443 V4DImode,
2444 V2TImode,
2445 V2SFmode,
2446 V4SFmode,
2447 V2DFmode,
2448 V8SFmode,
2449 V4DFmode,
2450 CCmode,
2451 CCUNSmode,
2452 CCEQmode,
2455 /* Virtual regs we are interested in. */
2456 const static struct {
2457 int regno; /* register number. */
2458 const char *name; /* register name. */
2459 } virtual_regs[] = {
2460 { STACK_POINTER_REGNUM, "stack pointer:" },
2461 { TOC_REGNUM, "toc: " },
2462 { STATIC_CHAIN_REGNUM, "static chain: " },
2463 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
2464 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
2465 { ARG_POINTER_REGNUM, "arg pointer: " },
2466 { FRAME_POINTER_REGNUM, "frame pointer:" },
2467 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
2468 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
2469 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
2470 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
2471 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
2472 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
2473 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
2474 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
2475 { LAST_VIRTUAL_REGISTER, "last virtual: " },
2478 fputs ("\nHard register information:\n", stderr);
2479 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2480 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2481 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2482 LAST_ALTIVEC_REGNO,
2483 "vs");
2484 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2485 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2486 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2487 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2488 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2489 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2491 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2492 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2493 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2495 fprintf (stderr,
2496 "\n"
2497 "d reg_class = %s\n"
2498 "f reg_class = %s\n"
2499 "v reg_class = %s\n"
2500 "wa reg_class = %s\n"
2501 "wb reg_class = %s\n"
2502 "wd reg_class = %s\n"
2503 "we reg_class = %s\n"
2504 "wf reg_class = %s\n"
2505 "wg reg_class = %s\n"
2506 "wh reg_class = %s\n"
2507 "wi reg_class = %s\n"
2508 "wj reg_class = %s\n"
2509 "wk reg_class = %s\n"
2510 "wl reg_class = %s\n"
2511 "wm reg_class = %s\n"
2512 "wo reg_class = %s\n"
2513 "wp reg_class = %s\n"
2514 "wq reg_class = %s\n"
2515 "wr reg_class = %s\n"
2516 "ws reg_class = %s\n"
2517 "wt reg_class = %s\n"
2518 "wu reg_class = %s\n"
2519 "wv reg_class = %s\n"
2520 "ww reg_class = %s\n"
2521 "wx reg_class = %s\n"
2522 "wy reg_class = %s\n"
2523 "wz reg_class = %s\n"
2524 "wA reg_class = %s\n"
2525 "wH reg_class = %s\n"
2526 "wI reg_class = %s\n"
2527 "wJ reg_class = %s\n"
2528 "wK reg_class = %s\n"
2529 "\n",
2530 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2531 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2532 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2533 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2534 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wb]],
2535 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2536 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_we]],
2537 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2538 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2539 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wh]],
2540 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wi]],
2541 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wj]],
2542 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wk]],
2543 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2544 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2545 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wo]],
2546 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wp]],
2547 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wq]],
2548 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2549 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2550 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2551 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2552 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2553 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2554 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2555 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2556 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]],
2557 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wA]],
2558 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wH]],
2559 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wI]],
2560 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wJ]],
2561 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wK]]);
2563 nl = "\n";
2564 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2565 rs6000_debug_print_mode (m);
2567 fputs ("\n", stderr);
2569 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2571 machine_mode mode1 = print_tieable_modes[m1];
2572 bool first_time = true;
2574 nl = (const char *)0;
2575 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2577 machine_mode mode2 = print_tieable_modes[m2];
2578 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
2580 if (first_time)
2582 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2583 nl = "\n";
2584 first_time = false;
2587 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2591 if (!first_time)
2592 fputs ("\n", stderr);
2595 if (nl)
2596 fputs (nl, stderr);
2598 if (rs6000_recip_control)
2600 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2602 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2603 if (rs6000_recip_bits[m])
2605 fprintf (stderr,
2606 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2607 GET_MODE_NAME (m),
2608 (RS6000_RECIP_AUTO_RE_P (m)
2609 ? "auto"
2610 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2611 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2612 ? "auto"
2613 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2616 fputs ("\n", stderr);
2619 if (rs6000_cpu_index >= 0)
2621 const char *name = processor_target_table[rs6000_cpu_index].name;
2622 HOST_WIDE_INT flags
2623 = processor_target_table[rs6000_cpu_index].target_enable;
2625 sprintf (flags_buffer, "-mcpu=%s flags", name);
2626 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2628 else
2629 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2631 if (rs6000_tune_index >= 0)
2633 const char *name = processor_target_table[rs6000_tune_index].name;
2634 HOST_WIDE_INT flags
2635 = processor_target_table[rs6000_tune_index].target_enable;
2637 sprintf (flags_buffer, "-mtune=%s flags", name);
2638 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2640 else
2641 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2643 cl_target_option_save (&cl_opts, &global_options);
2644 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2645 rs6000_isa_flags);
2647 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2648 rs6000_isa_flags_explicit);
2650 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2651 rs6000_builtin_mask);
2653 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2655 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2656 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2658 switch (rs6000_sched_costly_dep)
2660 case max_dep_latency:
2661 costly_str = "max_dep_latency";
2662 break;
2664 case no_dep_costly:
2665 costly_str = "no_dep_costly";
2666 break;
2668 case all_deps_costly:
2669 costly_str = "all_deps_costly";
2670 break;
2672 case true_store_to_load_dep_costly:
2673 costly_str = "true_store_to_load_dep_costly";
2674 break;
2676 case store_to_load_dep_costly:
2677 costly_str = "store_to_load_dep_costly";
2678 break;
2680 default:
2681 costly_str = costly_num;
2682 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2683 break;
2686 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2688 switch (rs6000_sched_insert_nops)
2690 case sched_finish_regroup_exact:
2691 nop_str = "sched_finish_regroup_exact";
2692 break;
2694 case sched_finish_pad_groups:
2695 nop_str = "sched_finish_pad_groups";
2696 break;
2698 case sched_finish_none:
2699 nop_str = "sched_finish_none";
2700 break;
2702 default:
2703 nop_str = nop_num;
2704 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2705 break;
2708 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2710 switch (rs6000_sdata)
2712 default:
2713 case SDATA_NONE:
2714 break;
2716 case SDATA_DATA:
2717 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2718 break;
2720 case SDATA_SYSV:
2721 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2722 break;
2724 case SDATA_EABI:
2725 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2726 break;
2730 switch (rs6000_traceback)
2732 case traceback_default: trace_str = "default"; break;
2733 case traceback_none: trace_str = "none"; break;
2734 case traceback_part: trace_str = "part"; break;
2735 case traceback_full: trace_str = "full"; break;
2736 default: trace_str = "unknown"; break;
2739 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2741 switch (rs6000_current_cmodel)
2743 case CMODEL_SMALL: cmodel_str = "small"; break;
2744 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2745 case CMODEL_LARGE: cmodel_str = "large"; break;
2746 default: cmodel_str = "unknown"; break;
2749 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2751 switch (rs6000_current_abi)
2753 case ABI_NONE: abi_str = "none"; break;
2754 case ABI_AIX: abi_str = "aix"; break;
2755 case ABI_ELFv2: abi_str = "ELFv2"; break;
2756 case ABI_V4: abi_str = "V4"; break;
2757 case ABI_DARWIN: abi_str = "darwin"; break;
2758 default: abi_str = "unknown"; break;
2761 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2763 if (rs6000_altivec_abi)
2764 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2766 if (rs6000_darwin64_abi)
2767 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2769 fprintf (stderr, DEBUG_FMT_S, "single_float",
2770 (TARGET_SINGLE_FLOAT ? "true" : "false"));
2772 fprintf (stderr, DEBUG_FMT_S, "double_float",
2773 (TARGET_DOUBLE_FLOAT ? "true" : "false"));
2775 fprintf (stderr, DEBUG_FMT_S, "soft_float",
2776 (TARGET_SOFT_FLOAT ? "true" : "false"));
2778 if (TARGET_LINK_STACK)
2779 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2781 if (TARGET_P8_FUSION)
2783 char options[80];
2785 strcpy (options, (TARGET_P9_FUSION) ? "power9" : "power8");
2786 if (TARGET_TOC_FUSION)
2787 strcat (options, ", toc");
2789 if (TARGET_P8_FUSION_SIGN)
2790 strcat (options, ", sign");
2792 fprintf (stderr, DEBUG_FMT_S, "fusion", options);
2795 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2796 TARGET_SECURE_PLT ? "secure" : "bss");
2797 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2798 aix_struct_return ? "aix" : "sysv");
2799 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2800 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2801 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2802 tf[!!rs6000_align_branch_targets]);
2803 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2804 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2805 rs6000_long_double_type_size);
2806 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2807 (int)rs6000_sched_restricted_insns_priority);
2808 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2809 (int)END_BUILTINS);
2810 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2811 (int)RS6000_BUILTIN_COUNT);
2813 fprintf (stderr, DEBUG_FMT_D, "Enable float128 on VSX",
2814 (int)TARGET_FLOAT128_ENABLE_TYPE);
2816 if (TARGET_VSX)
2817 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2818 (int)VECTOR_ELEMENT_SCALAR_64BIT);
2820 if (TARGET_DIRECT_MOVE_128)
2821 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit mfvsrld element",
2822 (int)VECTOR_ELEMENT_MFVSRLD_64BIT);
2826 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2827 legitimate address support to figure out the appropriate addressing to
2828 use. */
2830 static void
2831 rs6000_setup_reg_addr_masks (void)
2833 ssize_t rc, reg, m, nregs;
2834 addr_mask_type any_addr_mask, addr_mask;
2836 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2838 machine_mode m2 = (machine_mode) m;
2839 bool complex_p = false;
2840 bool small_int_p = (m2 == QImode || m2 == HImode || m2 == SImode);
2841 size_t msize;
2843 if (COMPLEX_MODE_P (m2))
2845 complex_p = true;
2846 m2 = GET_MODE_INNER (m2);
2849 msize = GET_MODE_SIZE (m2);
2851 /* SDmode is special in that we want to access it only via REG+REG
2852 addressing on power7 and above, since we want to use the LFIWZX and
2853 STFIWZX instructions to load it. */
2854 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2856 any_addr_mask = 0;
2857 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2859 addr_mask = 0;
2860 reg = reload_reg_map[rc].reg;
2862 /* Can mode values go in the GPR/FPR/Altivec registers? */
2863 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2865 bool small_int_vsx_p = (small_int_p
2866 && (rc == RELOAD_REG_FPR
2867 || rc == RELOAD_REG_VMX));
2869 nregs = rs6000_hard_regno_nregs[m][reg];
2870 addr_mask |= RELOAD_REG_VALID;
2872 /* Indicate if the mode takes more than 1 physical register. If
2873 it takes a single register, indicate it can do REG+REG
2874 addressing. Small integers in VSX registers can only do
2875 REG+REG addressing. */
2876 if (small_int_vsx_p)
2877 addr_mask |= RELOAD_REG_INDEXED;
2878 else if (nregs > 1 || m == BLKmode || complex_p)
2879 addr_mask |= RELOAD_REG_MULTIPLE;
2880 else
2881 addr_mask |= RELOAD_REG_INDEXED;
2883 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2884 addressing. If we allow scalars into Altivec registers,
2885 don't allow PRE_INC, PRE_DEC, or PRE_MODIFY. */
2887 if (TARGET_UPDATE
2888 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2889 && msize <= 8
2890 && !VECTOR_MODE_P (m2)
2891 && !FLOAT128_VECTOR_P (m2)
2892 && !complex_p
2893 && !small_int_vsx_p)
2895 addr_mask |= RELOAD_REG_PRE_INCDEC;
2897 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2898 we don't allow PRE_MODIFY for some multi-register
2899 operations. */
2900 switch (m)
2902 default:
2903 addr_mask |= RELOAD_REG_PRE_MODIFY;
2904 break;
2906 case E_DImode:
2907 if (TARGET_POWERPC64)
2908 addr_mask |= RELOAD_REG_PRE_MODIFY;
2909 break;
2911 case E_DFmode:
2912 case E_DDmode:
2913 if (TARGET_DF_INSN)
2914 addr_mask |= RELOAD_REG_PRE_MODIFY;
2915 break;
2920 /* GPR and FPR registers can do REG+OFFSET addressing, except
2921 possibly for SDmode. ISA 3.0 (i.e. power9) adds D-form addressing
2922 for 64-bit scalars and 32-bit SFmode to altivec registers. */
2923 if ((addr_mask != 0) && !indexed_only_p
2924 && msize <= 8
2925 && (rc == RELOAD_REG_GPR
2926 || ((msize == 8 || m2 == SFmode)
2927 && (rc == RELOAD_REG_FPR
2928 || (rc == RELOAD_REG_VMX && TARGET_P9_VECTOR)))))
2929 addr_mask |= RELOAD_REG_OFFSET;
2931 /* VSX registers can do REG+OFFSET addresssing if ISA 3.0
2932 instructions are enabled. The offset for 128-bit VSX registers is
2933 only 12-bits. While GPRs can handle the full offset range, VSX
2934 registers can only handle the restricted range. */
2935 else if ((addr_mask != 0) && !indexed_only_p
2936 && msize == 16 && TARGET_P9_VECTOR
2937 && (ALTIVEC_OR_VSX_VECTOR_MODE (m2)
2938 || (m2 == TImode && TARGET_VSX)))
2940 addr_mask |= RELOAD_REG_OFFSET;
2941 if (rc == RELOAD_REG_FPR || rc == RELOAD_REG_VMX)
2942 addr_mask |= RELOAD_REG_QUAD_OFFSET;
2945 /* VMX registers can do (REG & -16) and ((REG+REG) & -16)
2946 addressing on 128-bit types. */
2947 if (rc == RELOAD_REG_VMX && msize == 16
2948 && (addr_mask & RELOAD_REG_VALID) != 0)
2949 addr_mask |= RELOAD_REG_AND_M16;
2951 reg_addr[m].addr_mask[rc] = addr_mask;
2952 any_addr_mask |= addr_mask;
2955 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2960 /* Initialize the various global tables that are based on register size. */
2961 static void
2962 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2964 ssize_t r, m, c;
2965 int align64;
2966 int align32;
2968 /* Precalculate REGNO_REG_CLASS. */
2969 rs6000_regno_regclass[0] = GENERAL_REGS;
2970 for (r = 1; r < 32; ++r)
2971 rs6000_regno_regclass[r] = BASE_REGS;
2973 for (r = 32; r < 64; ++r)
2974 rs6000_regno_regclass[r] = FLOAT_REGS;
2976 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2977 rs6000_regno_regclass[r] = NO_REGS;
2979 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2980 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2982 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2983 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2984 rs6000_regno_regclass[r] = CR_REGS;
2986 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2987 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2988 rs6000_regno_regclass[CA_REGNO] = NO_REGS;
2989 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2990 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2991 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2992 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2993 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2994 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2995 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2997 /* Precalculate register class to simpler reload register class. We don't
2998 need all of the register classes that are combinations of different
2999 classes, just the simple ones that have constraint letters. */
3000 for (c = 0; c < N_REG_CLASSES; c++)
3001 reg_class_to_reg_type[c] = NO_REG_TYPE;
3003 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
3004 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
3005 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
3006 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
3007 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
3008 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
3009 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
3010 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
3011 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
3012 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
3014 if (TARGET_VSX)
3016 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
3017 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
3019 else
3021 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
3022 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
3025 /* Precalculate the valid memory formats as well as the vector information,
3026 this must be set up before the rs6000_hard_regno_nregs_internal calls
3027 below. */
3028 gcc_assert ((int)VECTOR_NONE == 0);
3029 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
3030 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
3032 gcc_assert ((int)CODE_FOR_nothing == 0);
3033 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
3035 gcc_assert ((int)NO_REGS == 0);
3036 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
3038 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
3039 believes it can use native alignment or still uses 128-bit alignment. */
3040 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
3042 align64 = 64;
3043 align32 = 32;
3045 else
3047 align64 = 128;
3048 align32 = 128;
3051 /* KF mode (IEEE 128-bit in VSX registers). We do not have arithmetic, so
3052 only set the memory modes. Include TFmode if -mabi=ieeelongdouble. */
3053 if (TARGET_FLOAT128_TYPE)
3055 rs6000_vector_mem[KFmode] = VECTOR_VSX;
3056 rs6000_vector_align[KFmode] = 128;
3058 if (FLOAT128_IEEE_P (TFmode))
3060 rs6000_vector_mem[TFmode] = VECTOR_VSX;
3061 rs6000_vector_align[TFmode] = 128;
3065 /* V2DF mode, VSX only. */
3066 if (TARGET_VSX)
3068 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
3069 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
3070 rs6000_vector_align[V2DFmode] = align64;
3073 /* V4SF mode, either VSX or Altivec. */
3074 if (TARGET_VSX)
3076 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
3077 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
3078 rs6000_vector_align[V4SFmode] = align32;
3080 else if (TARGET_ALTIVEC)
3082 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
3083 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
3084 rs6000_vector_align[V4SFmode] = align32;
3087 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
3088 and stores. */
3089 if (TARGET_ALTIVEC)
3091 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
3092 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
3093 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
3094 rs6000_vector_align[V4SImode] = align32;
3095 rs6000_vector_align[V8HImode] = align32;
3096 rs6000_vector_align[V16QImode] = align32;
3098 if (TARGET_VSX)
3100 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
3101 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
3102 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
3104 else
3106 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
3107 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
3108 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
3112 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
3113 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
3114 if (TARGET_VSX)
3116 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
3117 rs6000_vector_unit[V2DImode]
3118 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3119 rs6000_vector_align[V2DImode] = align64;
3121 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
3122 rs6000_vector_unit[V1TImode]
3123 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3124 rs6000_vector_align[V1TImode] = 128;
3127 /* DFmode, see if we want to use the VSX unit. Memory is handled
3128 differently, so don't set rs6000_vector_mem. */
3129 if (TARGET_VSX)
3131 rs6000_vector_unit[DFmode] = VECTOR_VSX;
3132 rs6000_vector_align[DFmode] = 64;
3135 /* SFmode, see if we want to use the VSX unit. */
3136 if (TARGET_P8_VECTOR)
3138 rs6000_vector_unit[SFmode] = VECTOR_VSX;
3139 rs6000_vector_align[SFmode] = 32;
3142 /* Allow TImode in VSX register and set the VSX memory macros. */
3143 if (TARGET_VSX)
3145 rs6000_vector_mem[TImode] = VECTOR_VSX;
3146 rs6000_vector_align[TImode] = align64;
3149 /* TODO add paired floating point vector support. */
3151 /* Register class constraints for the constraints that depend on compile
3152 switches. When the VSX code was added, different constraints were added
3153 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
3154 of the VSX registers are used. The register classes for scalar floating
3155 point types is set, based on whether we allow that type into the upper
3156 (Altivec) registers. GCC has register classes to target the Altivec
3157 registers for load/store operations, to select using a VSX memory
3158 operation instead of the traditional floating point operation. The
3159 constraints are:
3161 d - Register class to use with traditional DFmode instructions.
3162 f - Register class to use with traditional SFmode instructions.
3163 v - Altivec register.
3164 wa - Any VSX register.
3165 wc - Reserved to represent individual CR bits (used in LLVM).
3166 wd - Preferred register class for V2DFmode.
3167 wf - Preferred register class for V4SFmode.
3168 wg - Float register for power6x move insns.
3169 wh - FP register for direct move instructions.
3170 wi - FP or VSX register to hold 64-bit integers for VSX insns.
3171 wj - FP or VSX register to hold 64-bit integers for direct moves.
3172 wk - FP or VSX register to hold 64-bit doubles for direct moves.
3173 wl - Float register if we can do 32-bit signed int loads.
3174 wm - VSX register for ISA 2.07 direct move operations.
3175 wn - always NO_REGS.
3176 wr - GPR if 64-bit mode is permitted.
3177 ws - Register class to do ISA 2.06 DF operations.
3178 wt - VSX register for TImode in VSX registers.
3179 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
3180 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
3181 ww - Register class to do SF conversions in with VSX operations.
3182 wx - Float register if we can do 32-bit int stores.
3183 wy - Register class to do ISA 2.07 SF operations.
3184 wz - Float register if we can do 32-bit unsigned int loads.
3185 wH - Altivec register if SImode is allowed in VSX registers.
3186 wI - VSX register if SImode is allowed in VSX registers.
3187 wJ - VSX register if QImode/HImode are allowed in VSX registers.
3188 wK - Altivec register if QImode/HImode are allowed in VSX registers. */
3190 if (TARGET_HARD_FLOAT)
3191 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS; /* SFmode */
3193 if (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
3194 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS; /* DFmode */
3196 if (TARGET_VSX)
3198 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
3199 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS; /* V2DFmode */
3200 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS; /* V4SFmode */
3201 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS; /* DFmode */
3202 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS; /* DFmode */
3203 rs6000_constraints[RS6000_CONSTRAINT_wi] = VSX_REGS; /* DImode */
3204 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS; /* TImode */
3207 /* Add conditional constraints based on various options, to allow us to
3208 collapse multiple insn patterns. */
3209 if (TARGET_ALTIVEC)
3210 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
3212 if (TARGET_MFPGPR) /* DFmode */
3213 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
3215 if (TARGET_LFIWAX)
3216 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS; /* DImode */
3218 if (TARGET_DIRECT_MOVE)
3220 rs6000_constraints[RS6000_CONSTRAINT_wh] = FLOAT_REGS;
3221 rs6000_constraints[RS6000_CONSTRAINT_wj] /* DImode */
3222 = rs6000_constraints[RS6000_CONSTRAINT_wi];
3223 rs6000_constraints[RS6000_CONSTRAINT_wk] /* DFmode */
3224 = rs6000_constraints[RS6000_CONSTRAINT_ws];
3225 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
3228 if (TARGET_POWERPC64)
3230 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
3231 rs6000_constraints[RS6000_CONSTRAINT_wA] = BASE_REGS;
3234 if (TARGET_P8_VECTOR) /* SFmode */
3236 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
3237 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
3238 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
3240 else if (TARGET_VSX)
3241 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
3243 if (TARGET_STFIWX)
3244 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS; /* DImode */
3246 if (TARGET_LFIWZX)
3247 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS; /* DImode */
3249 if (TARGET_FLOAT128_TYPE)
3251 rs6000_constraints[RS6000_CONSTRAINT_wq] = VSX_REGS; /* KFmode */
3252 if (FLOAT128_IEEE_P (TFmode))
3253 rs6000_constraints[RS6000_CONSTRAINT_wp] = VSX_REGS; /* TFmode */
3256 if (TARGET_P9_VECTOR)
3258 /* Support for new D-form instructions. */
3259 rs6000_constraints[RS6000_CONSTRAINT_wb] = ALTIVEC_REGS;
3261 /* Support for ISA 3.0 (power9) vectors. */
3262 rs6000_constraints[RS6000_CONSTRAINT_wo] = VSX_REGS;
3265 /* Support for new direct moves (ISA 3.0 + 64bit). */
3266 if (TARGET_DIRECT_MOVE_128)
3267 rs6000_constraints[RS6000_CONSTRAINT_we] = VSX_REGS;
3269 /* Support small integers in VSX registers. */
3270 if (TARGET_P8_VECTOR)
3272 rs6000_constraints[RS6000_CONSTRAINT_wH] = ALTIVEC_REGS;
3273 rs6000_constraints[RS6000_CONSTRAINT_wI] = FLOAT_REGS;
3274 if (TARGET_P9_VECTOR)
3276 rs6000_constraints[RS6000_CONSTRAINT_wJ] = FLOAT_REGS;
3277 rs6000_constraints[RS6000_CONSTRAINT_wK] = ALTIVEC_REGS;
3281 /* Set up the reload helper and direct move functions. */
3282 if (TARGET_VSX || TARGET_ALTIVEC)
3284 if (TARGET_64BIT)
3286 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
3287 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
3288 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
3289 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
3290 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
3291 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
3292 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
3293 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
3294 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
3295 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
3296 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
3297 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
3298 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
3299 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
3300 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
3301 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
3302 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
3303 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
3304 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
3305 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
3307 if (FLOAT128_VECTOR_P (KFmode))
3309 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_di_store;
3310 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_di_load;
3313 if (FLOAT128_VECTOR_P (TFmode))
3315 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_di_store;
3316 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_di_load;
3319 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3320 available. */
3321 if (TARGET_NO_SDMODE_STACK)
3323 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
3324 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
3327 if (TARGET_VSX)
3329 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
3330 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
3333 if (TARGET_DIRECT_MOVE && !TARGET_DIRECT_MOVE_128)
3335 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
3336 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
3337 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
3338 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
3339 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
3340 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
3341 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
3342 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
3343 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
3345 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
3346 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
3347 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
3348 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
3349 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
3350 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
3351 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
3352 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
3353 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
3355 if (FLOAT128_VECTOR_P (KFmode))
3357 reg_addr[KFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxkf;
3358 reg_addr[KFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprkf;
3361 if (FLOAT128_VECTOR_P (TFmode))
3363 reg_addr[TFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxtf;
3364 reg_addr[TFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprtf;
3368 else
3370 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
3371 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
3372 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
3373 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
3374 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
3375 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
3376 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
3377 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
3378 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
3379 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
3380 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
3381 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
3382 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
3383 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
3384 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
3385 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
3386 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
3387 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
3388 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
3389 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
3391 if (FLOAT128_VECTOR_P (KFmode))
3393 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_si_store;
3394 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_si_load;
3397 if (FLOAT128_IEEE_P (TFmode))
3399 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_si_store;
3400 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_si_load;
3403 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3404 available. */
3405 if (TARGET_NO_SDMODE_STACK)
3407 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
3408 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
3411 if (TARGET_VSX)
3413 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
3414 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
3417 if (TARGET_DIRECT_MOVE)
3419 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
3420 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
3421 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
3425 reg_addr[DFmode].scalar_in_vmx_p = true;
3426 reg_addr[DImode].scalar_in_vmx_p = true;
3428 if (TARGET_P8_VECTOR)
3430 reg_addr[SFmode].scalar_in_vmx_p = true;
3431 reg_addr[SImode].scalar_in_vmx_p = true;
3433 if (TARGET_P9_VECTOR)
3435 reg_addr[HImode].scalar_in_vmx_p = true;
3436 reg_addr[QImode].scalar_in_vmx_p = true;
3441 /* Setup the fusion operations. */
3442 if (TARGET_P8_FUSION)
3444 reg_addr[QImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_qi;
3445 reg_addr[HImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_hi;
3446 reg_addr[SImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_si;
3447 if (TARGET_64BIT)
3448 reg_addr[DImode].fusion_gpr_ld = CODE_FOR_fusion_gpr_load_di;
3451 if (TARGET_P9_FUSION)
3453 struct fuse_insns {
3454 enum machine_mode mode; /* mode of the fused type. */
3455 enum machine_mode pmode; /* pointer mode. */
3456 enum rs6000_reload_reg_type rtype; /* register type. */
3457 enum insn_code load; /* load insn. */
3458 enum insn_code store; /* store insn. */
3461 static const struct fuse_insns addis_insns[] = {
3462 { E_SFmode, E_DImode, RELOAD_REG_FPR,
3463 CODE_FOR_fusion_vsx_di_sf_load,
3464 CODE_FOR_fusion_vsx_di_sf_store },
3466 { E_SFmode, E_SImode, RELOAD_REG_FPR,
3467 CODE_FOR_fusion_vsx_si_sf_load,
3468 CODE_FOR_fusion_vsx_si_sf_store },
3470 { E_DFmode, E_DImode, RELOAD_REG_FPR,
3471 CODE_FOR_fusion_vsx_di_df_load,
3472 CODE_FOR_fusion_vsx_di_df_store },
3474 { E_DFmode, E_SImode, RELOAD_REG_FPR,
3475 CODE_FOR_fusion_vsx_si_df_load,
3476 CODE_FOR_fusion_vsx_si_df_store },
3478 { E_DImode, E_DImode, RELOAD_REG_FPR,
3479 CODE_FOR_fusion_vsx_di_di_load,
3480 CODE_FOR_fusion_vsx_di_di_store },
3482 { E_DImode, E_SImode, RELOAD_REG_FPR,
3483 CODE_FOR_fusion_vsx_si_di_load,
3484 CODE_FOR_fusion_vsx_si_di_store },
3486 { E_QImode, E_DImode, RELOAD_REG_GPR,
3487 CODE_FOR_fusion_gpr_di_qi_load,
3488 CODE_FOR_fusion_gpr_di_qi_store },
3490 { E_QImode, E_SImode, RELOAD_REG_GPR,
3491 CODE_FOR_fusion_gpr_si_qi_load,
3492 CODE_FOR_fusion_gpr_si_qi_store },
3494 { E_HImode, E_DImode, RELOAD_REG_GPR,
3495 CODE_FOR_fusion_gpr_di_hi_load,
3496 CODE_FOR_fusion_gpr_di_hi_store },
3498 { E_HImode, E_SImode, RELOAD_REG_GPR,
3499 CODE_FOR_fusion_gpr_si_hi_load,
3500 CODE_FOR_fusion_gpr_si_hi_store },
3502 { E_SImode, E_DImode, RELOAD_REG_GPR,
3503 CODE_FOR_fusion_gpr_di_si_load,
3504 CODE_FOR_fusion_gpr_di_si_store },
3506 { E_SImode, E_SImode, RELOAD_REG_GPR,
3507 CODE_FOR_fusion_gpr_si_si_load,
3508 CODE_FOR_fusion_gpr_si_si_store },
3510 { E_SFmode, E_DImode, RELOAD_REG_GPR,
3511 CODE_FOR_fusion_gpr_di_sf_load,
3512 CODE_FOR_fusion_gpr_di_sf_store },
3514 { E_SFmode, E_SImode, RELOAD_REG_GPR,
3515 CODE_FOR_fusion_gpr_si_sf_load,
3516 CODE_FOR_fusion_gpr_si_sf_store },
3518 { E_DImode, E_DImode, RELOAD_REG_GPR,
3519 CODE_FOR_fusion_gpr_di_di_load,
3520 CODE_FOR_fusion_gpr_di_di_store },
3522 { E_DFmode, E_DImode, RELOAD_REG_GPR,
3523 CODE_FOR_fusion_gpr_di_df_load,
3524 CODE_FOR_fusion_gpr_di_df_store },
3527 machine_mode cur_pmode = Pmode;
3528 size_t i;
3530 for (i = 0; i < ARRAY_SIZE (addis_insns); i++)
3532 machine_mode xmode = addis_insns[i].mode;
3533 enum rs6000_reload_reg_type rtype = addis_insns[i].rtype;
3535 if (addis_insns[i].pmode != cur_pmode)
3536 continue;
3538 if (rtype == RELOAD_REG_FPR && !TARGET_HARD_FLOAT)
3539 continue;
3541 reg_addr[xmode].fusion_addis_ld[rtype] = addis_insns[i].load;
3542 reg_addr[xmode].fusion_addis_st[rtype] = addis_insns[i].store;
3544 if (rtype == RELOAD_REG_FPR && TARGET_P9_VECTOR)
3546 reg_addr[xmode].fusion_addis_ld[RELOAD_REG_VMX]
3547 = addis_insns[i].load;
3548 reg_addr[xmode].fusion_addis_st[RELOAD_REG_VMX]
3549 = addis_insns[i].store;
3554 /* Note which types we support fusing TOC setup plus memory insn. We only do
3555 fused TOCs for medium/large code models. */
3556 if (TARGET_P8_FUSION && TARGET_TOC_FUSION && TARGET_POWERPC64
3557 && (TARGET_CMODEL != CMODEL_SMALL))
3559 reg_addr[QImode].fused_toc = true;
3560 reg_addr[HImode].fused_toc = true;
3561 reg_addr[SImode].fused_toc = true;
3562 reg_addr[DImode].fused_toc = true;
3563 if (TARGET_HARD_FLOAT)
3565 if (TARGET_SINGLE_FLOAT)
3566 reg_addr[SFmode].fused_toc = true;
3567 if (TARGET_DOUBLE_FLOAT)
3568 reg_addr[DFmode].fused_toc = true;
3572 /* Precalculate HARD_REGNO_NREGS. */
3573 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
3574 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3575 rs6000_hard_regno_nregs[m][r]
3576 = rs6000_hard_regno_nregs_internal (r, (machine_mode)m);
3578 /* Precalculate HARD_REGNO_MODE_OK. */
3579 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
3580 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3581 if (rs6000_hard_regno_mode_ok (r, (machine_mode)m))
3582 rs6000_hard_regno_mode_ok_p[m][r] = true;
3584 /* Precalculate CLASS_MAX_NREGS sizes. */
3585 for (c = 0; c < LIM_REG_CLASSES; ++c)
3587 int reg_size;
3589 if (TARGET_VSX && VSX_REG_CLASS_P (c))
3590 reg_size = UNITS_PER_VSX_WORD;
3592 else if (c == ALTIVEC_REGS)
3593 reg_size = UNITS_PER_ALTIVEC_WORD;
3595 else if (c == FLOAT_REGS)
3596 reg_size = UNITS_PER_FP_WORD;
3598 else
3599 reg_size = UNITS_PER_WORD;
3601 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3603 machine_mode m2 = (machine_mode)m;
3604 int reg_size2 = reg_size;
3606 /* TDmode & IBM 128-bit floating point always takes 2 registers, even
3607 in VSX. */
3608 if (TARGET_VSX && VSX_REG_CLASS_P (c) && FLOAT128_2REG_P (m))
3609 reg_size2 = UNITS_PER_FP_WORD;
3611 rs6000_class_max_nregs[m][c]
3612 = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
3616 /* Calculate which modes to automatically generate code to use a the
3617 reciprocal divide and square root instructions. In the future, possibly
3618 automatically generate the instructions even if the user did not specify
3619 -mrecip. The older machines double precision reciprocal sqrt estimate is
3620 not accurate enough. */
3621 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
3622 if (TARGET_FRES)
3623 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3624 if (TARGET_FRE)
3625 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3626 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3627 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3628 if (VECTOR_UNIT_VSX_P (V2DFmode))
3629 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3631 if (TARGET_FRSQRTES)
3632 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3633 if (TARGET_FRSQRTE)
3634 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3635 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3636 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3637 if (VECTOR_UNIT_VSX_P (V2DFmode))
3638 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3640 if (rs6000_recip_control)
3642 if (!flag_finite_math_only)
3643 warning (0, "%qs requires %qs or %qs", "-mrecip", "-ffinite-math",
3644 "-ffast-math");
3645 if (flag_trapping_math)
3646 warning (0, "%qs requires %qs or %qs", "-mrecip",
3647 "-fno-trapping-math", "-ffast-math");
3648 if (!flag_reciprocal_math)
3649 warning (0, "%qs requires %qs or %qs", "-mrecip", "-freciprocal-math",
3650 "-ffast-math");
3651 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
3653 if (RS6000_RECIP_HAVE_RE_P (SFmode)
3654 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
3655 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3657 if (RS6000_RECIP_HAVE_RE_P (DFmode)
3658 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
3659 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3661 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
3662 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
3663 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3665 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
3666 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
3667 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3669 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
3670 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
3671 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3673 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
3674 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
3675 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3677 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
3678 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
3679 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3681 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
3682 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
3683 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3687 /* Update the addr mask bits in reg_addr to help secondary reload and go if
3688 legitimate address support to figure out the appropriate addressing to
3689 use. */
3690 rs6000_setup_reg_addr_masks ();
3692 if (global_init_p || TARGET_DEBUG_TARGET)
3694 if (TARGET_DEBUG_REG)
3695 rs6000_debug_reg_global ();
3697 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
3698 fprintf (stderr,
3699 "SImode variable mult cost = %d\n"
3700 "SImode constant mult cost = %d\n"
3701 "SImode short constant mult cost = %d\n"
3702 "DImode multipliciation cost = %d\n"
3703 "SImode division cost = %d\n"
3704 "DImode division cost = %d\n"
3705 "Simple fp operation cost = %d\n"
3706 "DFmode multiplication cost = %d\n"
3707 "SFmode division cost = %d\n"
3708 "DFmode division cost = %d\n"
3709 "cache line size = %d\n"
3710 "l1 cache size = %d\n"
3711 "l2 cache size = %d\n"
3712 "simultaneous prefetches = %d\n"
3713 "\n",
3714 rs6000_cost->mulsi,
3715 rs6000_cost->mulsi_const,
3716 rs6000_cost->mulsi_const9,
3717 rs6000_cost->muldi,
3718 rs6000_cost->divsi,
3719 rs6000_cost->divdi,
3720 rs6000_cost->fp,
3721 rs6000_cost->dmul,
3722 rs6000_cost->sdiv,
3723 rs6000_cost->ddiv,
3724 rs6000_cost->cache_line_size,
3725 rs6000_cost->l1_cache_size,
3726 rs6000_cost->l2_cache_size,
3727 rs6000_cost->simultaneous_prefetches);
3731 #if TARGET_MACHO
3732 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
3734 static void
3735 darwin_rs6000_override_options (void)
3737 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
3738 off. */
3739 rs6000_altivec_abi = 1;
3740 TARGET_ALTIVEC_VRSAVE = 1;
3741 rs6000_current_abi = ABI_DARWIN;
3743 if (DEFAULT_ABI == ABI_DARWIN
3744 && TARGET_64BIT)
3745 darwin_one_byte_bool = 1;
3747 if (TARGET_64BIT && ! TARGET_POWERPC64)
3749 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
3750 warning (0, "%qs requires PowerPC64 architecture, enabling", "-m64");
3752 if (flag_mkernel)
3754 rs6000_default_long_calls = 1;
3755 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
3758 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
3759 Altivec. */
3760 if (!flag_mkernel && !flag_apple_kext
3761 && TARGET_64BIT
3762 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3763 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3765 /* Unless the user (not the configurer) has explicitly overridden
3766 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3767 G4 unless targeting the kernel. */
3768 if (!flag_mkernel
3769 && !flag_apple_kext
3770 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3771 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3772 && ! global_options_set.x_rs6000_cpu_index)
3774 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3777 #endif
3779 /* If not otherwise specified by a target, make 'long double' equivalent to
3780 'double'. */
3782 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3783 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3784 #endif
3786 /* Return the builtin mask of the various options used that could affect which
3787 builtins were used. In the past we used target_flags, but we've run out of
3788 bits, and some options like PAIRED are no longer in target_flags. */
3790 HOST_WIDE_INT
3791 rs6000_builtin_mask_calculate (void)
3793 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3794 | ((TARGET_CMPB) ? RS6000_BTM_CMPB : 0)
3795 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3796 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
3797 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3798 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3799 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3800 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3801 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3802 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3803 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3804 | ((TARGET_P9_VECTOR) ? RS6000_BTM_P9_VECTOR : 0)
3805 | ((TARGET_P9_MISC) ? RS6000_BTM_P9_MISC : 0)
3806 | ((TARGET_MODULO) ? RS6000_BTM_MODULO : 0)
3807 | ((TARGET_64BIT) ? RS6000_BTM_64BIT : 0)
3808 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3809 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0)
3810 | ((TARGET_DFP) ? RS6000_BTM_DFP : 0)
3811 | ((TARGET_HARD_FLOAT) ? RS6000_BTM_HARD_FLOAT : 0)
3812 | ((TARGET_LONG_DOUBLE_128) ? RS6000_BTM_LDBL128 : 0)
3813 | ((TARGET_FLOAT128_TYPE) ? RS6000_BTM_FLOAT128 : 0));
3816 /* Implement TARGET_MD_ASM_ADJUST. All asm statements are considered
3817 to clobber the XER[CA] bit because clobbering that bit without telling
3818 the compiler worked just fine with versions of GCC before GCC 5, and
3819 breaking a lot of older code in ways that are hard to track down is
3820 not such a great idea. */
3822 static rtx_insn *
3823 rs6000_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
3824 vec<const char *> &/*constraints*/,
3825 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
3827 clobbers.safe_push (gen_rtx_REG (SImode, CA_REGNO));
3828 SET_HARD_REG_BIT (clobbered_regs, CA_REGNO);
3829 return NULL;
3832 /* Override command line options.
3834 Combine build-specific configuration information with options
3835 specified on the command line to set various state variables which
3836 influence code generation, optimization, and expansion of built-in
3837 functions. Assure that command-line configuration preferences are
3838 compatible with each other and with the build configuration; issue
3839 warnings while adjusting configuration or error messages while
3840 rejecting configuration.
3842 Upon entry to this function:
3844 This function is called once at the beginning of
3845 compilation, and then again at the start and end of compiling
3846 each section of code that has a different configuration, as
3847 indicated, for example, by adding the
3849 __attribute__((__target__("cpu=power9")))
3851 qualifier to a function definition or, for example, by bracketing
3852 code between
3854 #pragma GCC target("altivec")
3858 #pragma GCC reset_options
3860 directives. Parameter global_init_p is true for the initial
3861 invocation, which initializes global variables, and false for all
3862 subsequent invocations.
3865 Various global state information is assumed to be valid. This
3866 includes OPTION_TARGET_CPU_DEFAULT, representing the name of the
3867 default CPU specified at build configure time, TARGET_DEFAULT,
3868 representing the default set of option flags for the default
3869 target, and global_options_set.x_rs6000_isa_flags, representing
3870 which options were requested on the command line.
3872 Upon return from this function:
3874 rs6000_isa_flags_explicit has a non-zero bit for each flag that
3875 was set by name on the command line. Additionally, if certain
3876 attributes are automatically enabled or disabled by this function
3877 in order to assure compatibility between options and
3878 configuration, the flags associated with those attributes are
3879 also set. By setting these "explicit bits", we avoid the risk
3880 that other code might accidentally overwrite these particular
3881 attributes with "default values".
3883 The various bits of rs6000_isa_flags are set to indicate the
3884 target options that have been selected for the most current
3885 compilation efforts. This has the effect of also turning on the
3886 associated TARGET_XXX values since these are macros which are
3887 generally defined to test the corresponding bit of the
3888 rs6000_isa_flags variable.
3890 The variable rs6000_builtin_mask is set to represent the target
3891 options for the most current compilation efforts, consistent with
3892 the current contents of rs6000_isa_flags. This variable controls
3893 expansion of built-in functions.
3895 Various other global variables and fields of global structures
3896 (over 50 in all) are initialized to reflect the desired options
3897 for the most current compilation efforts. */
3899 static bool
3900 rs6000_option_override_internal (bool global_init_p)
3902 bool ret = true;
3903 bool have_cpu = false;
3905 /* The default cpu requested at configure time, if any. */
3906 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
3908 HOST_WIDE_INT set_masks;
3909 HOST_WIDE_INT ignore_masks;
3910 int cpu_index;
3911 int tune_index;
3912 struct cl_target_option *main_target_opt
3913 = ((global_init_p || target_option_default_node == NULL)
3914 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3916 /* Print defaults. */
3917 if ((TARGET_DEBUG_REG || TARGET_DEBUG_TARGET) && global_init_p)
3918 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
3920 /* Remember the explicit arguments. */
3921 if (global_init_p)
3922 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3924 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3925 library functions, so warn about it. The flag may be useful for
3926 performance studies from time to time though, so don't disable it
3927 entirely. */
3928 if (global_options_set.x_rs6000_alignment_flags
3929 && rs6000_alignment_flags == MASK_ALIGN_POWER
3930 && DEFAULT_ABI == ABI_DARWIN
3931 && TARGET_64BIT)
3932 warning (0, "%qs is not supported for 64-bit Darwin;"
3933 " it is incompatible with the installed C and C++ libraries",
3934 "-malign-power");
3936 /* Numerous experiment shows that IRA based loop pressure
3937 calculation works better for RTL loop invariant motion on targets
3938 with enough (>= 32) registers. It is an expensive optimization.
3939 So it is on only for peak performance. */
3940 if (optimize >= 3 && global_init_p
3941 && !global_options_set.x_flag_ira_loop_pressure)
3942 flag_ira_loop_pressure = 1;
3944 /* -fsanitize=address needs to turn on -fasynchronous-unwind-tables in order
3945 for tracebacks to be complete but not if any -fasynchronous-unwind-tables
3946 options were already specified. */
3947 if (flag_sanitize & SANITIZE_USER_ADDRESS
3948 && !global_options_set.x_flag_asynchronous_unwind_tables)
3949 flag_asynchronous_unwind_tables = 1;
3951 /* Set the pointer size. */
3952 if (TARGET_64BIT)
3954 rs6000_pmode = DImode;
3955 rs6000_pointer_size = 64;
3957 else
3959 rs6000_pmode = SImode;
3960 rs6000_pointer_size = 32;
3963 /* Some OSs don't support saving the high part of 64-bit registers on context
3964 switch. Other OSs don't support saving Altivec registers. On those OSs,
3965 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3966 if the user wants either, the user must explicitly specify them and we
3967 won't interfere with the user's specification. */
3969 set_masks = POWERPC_MASKS;
3970 #ifdef OS_MISSING_POWERPC64
3971 if (OS_MISSING_POWERPC64)
3972 set_masks &= ~OPTION_MASK_POWERPC64;
3973 #endif
3974 #ifdef OS_MISSING_ALTIVEC
3975 if (OS_MISSING_ALTIVEC)
3976 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX
3977 | OTHER_VSX_VECTOR_MASKS);
3978 #endif
3980 /* Don't override by the processor default if given explicitly. */
3981 set_masks &= ~rs6000_isa_flags_explicit;
3983 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
3984 the cpu in a target attribute or pragma, but did not specify a tuning
3985 option, use the cpu for the tuning option rather than the option specified
3986 with -mtune on the command line. Process a '--with-cpu' configuration
3987 request as an implicit --cpu. */
3988 if (rs6000_cpu_index >= 0)
3990 cpu_index = rs6000_cpu_index;
3991 have_cpu = true;
3993 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3995 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
3996 have_cpu = true;
3998 else if (implicit_cpu)
4000 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
4001 have_cpu = true;
4003 else
4005 /* PowerPC 64-bit LE requires at least ISA 2.07. */
4006 const char *default_cpu = ((!TARGET_POWERPC64)
4007 ? "powerpc"
4008 : ((BYTES_BIG_ENDIAN)
4009 ? "powerpc64"
4010 : "powerpc64le"));
4012 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
4013 have_cpu = false;
4016 gcc_assert (cpu_index >= 0);
4018 if (have_cpu)
4020 #ifndef HAVE_AS_POWER9
4021 if (processor_target_table[rs6000_cpu_index].processor
4022 == PROCESSOR_POWER9)
4024 have_cpu = false;
4025 warning (0, "will not generate power9 instructions because "
4026 "assembler lacks power9 support");
4028 #endif
4029 #ifndef HAVE_AS_POWER8
4030 if (processor_target_table[rs6000_cpu_index].processor
4031 == PROCESSOR_POWER8)
4033 have_cpu = false;
4034 warning (0, "will not generate power8 instructions because "
4035 "assembler lacks power8 support");
4037 #endif
4038 #ifndef HAVE_AS_POPCNTD
4039 if (processor_target_table[rs6000_cpu_index].processor
4040 == PROCESSOR_POWER7)
4042 have_cpu = false;
4043 warning (0, "will not generate power7 instructions because "
4044 "assembler lacks power7 support");
4046 #endif
4047 #ifndef HAVE_AS_DFP
4048 if (processor_target_table[rs6000_cpu_index].processor
4049 == PROCESSOR_POWER6)
4051 have_cpu = false;
4052 warning (0, "will not generate power6 instructions because "
4053 "assembler lacks power6 support");
4055 #endif
4056 #ifndef HAVE_AS_POPCNTB
4057 if (processor_target_table[rs6000_cpu_index].processor
4058 == PROCESSOR_POWER5)
4060 have_cpu = false;
4061 warning (0, "will not generate power5 instructions because "
4062 "assembler lacks power5 support");
4064 #endif
4066 if (!have_cpu)
4068 /* PowerPC 64-bit LE requires at least ISA 2.07. */
4069 const char *default_cpu = (!TARGET_POWERPC64
4070 ? "powerpc"
4071 : (BYTES_BIG_ENDIAN
4072 ? "powerpc64"
4073 : "powerpc64le"));
4075 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
4079 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
4080 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
4081 with those from the cpu, except for options that were explicitly set. If
4082 we don't have a cpu, do not override the target bits set in
4083 TARGET_DEFAULT. */
4084 if (have_cpu)
4086 rs6000_isa_flags &= ~set_masks;
4087 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
4088 & set_masks);
4090 else
4092 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
4093 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
4094 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
4095 to using rs6000_isa_flags, we need to do the initialization here.
4097 If there is a TARGET_DEFAULT, use that. Otherwise fall back to using
4098 -mcpu=powerpc, -mcpu=powerpc64, or -mcpu=powerpc64le defaults. */
4099 HOST_WIDE_INT flags = ((TARGET_DEFAULT) ? TARGET_DEFAULT
4100 : processor_target_table[cpu_index].target_enable);
4101 rs6000_isa_flags |= (flags & ~rs6000_isa_flags_explicit);
4104 if (rs6000_tune_index >= 0)
4105 tune_index = rs6000_tune_index;
4106 else if (have_cpu)
4107 rs6000_tune_index = tune_index = cpu_index;
4108 else
4110 size_t i;
4111 enum processor_type tune_proc
4112 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
4114 tune_index = -1;
4115 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
4116 if (processor_target_table[i].processor == tune_proc)
4118 rs6000_tune_index = tune_index = i;
4119 break;
4123 gcc_assert (tune_index >= 0);
4124 rs6000_cpu = processor_target_table[tune_index].processor;
4126 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
4127 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
4128 || rs6000_cpu == PROCESSOR_PPCE5500)
4130 if (TARGET_ALTIVEC)
4131 error ("AltiVec not supported in this target");
4134 /* If we are optimizing big endian systems for space, use the load/store
4135 multiple and string instructions. */
4136 if (BYTES_BIG_ENDIAN && optimize_size)
4137 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
4138 | OPTION_MASK_STRING);
4140 /* Don't allow -mmultiple or -mstring on little endian systems
4141 unless the cpu is a 750, because the hardware doesn't support the
4142 instructions used in little endian mode, and causes an alignment
4143 trap. The 750 does not cause an alignment trap (except when the
4144 target is unaligned). */
4146 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
4148 if (TARGET_MULTIPLE)
4150 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
4151 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
4152 warning (0, "%qs is not supported on little endian systems",
4153 "-mmultiple");
4156 if (TARGET_STRING)
4158 rs6000_isa_flags &= ~OPTION_MASK_STRING;
4159 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
4160 warning (0, "%qs is not supported on little endian systems",
4161 "-mstring");
4165 /* If little-endian, default to -mstrict-align on older processors.
4166 Testing for htm matches power8 and later. */
4167 if (!BYTES_BIG_ENDIAN
4168 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
4169 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
4171 /* -maltivec={le,be} implies -maltivec. */
4172 if (rs6000_altivec_element_order != 0)
4173 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
4175 /* Disallow -maltivec=le in big endian mode for now. This is not
4176 known to be useful for anyone. */
4177 if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
4179 warning (0, N_("-maltivec=le not allowed for big-endian targets"));
4180 rs6000_altivec_element_order = 0;
4183 if (!rs6000_fold_gimple)
4184 fprintf (stderr,
4185 "gimple folding of rs6000 builtins has been disabled.\n");
4187 /* Add some warnings for VSX. */
4188 if (TARGET_VSX)
4190 const char *msg = NULL;
4191 if (!TARGET_HARD_FLOAT || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
4193 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4194 msg = N_("-mvsx requires hardware floating point");
4195 else
4197 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
4198 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4201 else if (TARGET_PAIRED_FLOAT)
4202 msg = N_("-mvsx and -mpaired are incompatible");
4203 else if (TARGET_AVOID_XFORM > 0)
4204 msg = N_("-mvsx needs indexed addressing");
4205 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
4206 & OPTION_MASK_ALTIVEC))
4208 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4209 msg = N_("-mvsx and -mno-altivec are incompatible");
4210 else
4211 msg = N_("-mno-altivec disables vsx");
4214 if (msg)
4216 warning (0, msg);
4217 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
4218 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4222 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
4223 the -mcpu setting to enable options that conflict. */
4224 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
4225 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
4226 | OPTION_MASK_ALTIVEC
4227 | OPTION_MASK_VSX)) != 0)
4228 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
4229 | OPTION_MASK_DIRECT_MOVE)
4230 & ~rs6000_isa_flags_explicit);
4232 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4233 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
4235 /* Handle explicit -mno-{altivec,vsx,power8-vector,power9-vector} and turn
4236 off all of the options that depend on those flags. */
4237 ignore_masks = rs6000_disable_incompatible_switches ();
4239 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
4240 unless the user explicitly used the -mno-<option> to disable the code. */
4241 if (TARGET_P9_VECTOR || TARGET_MODULO || TARGET_P9_MISC)
4242 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
4243 else if (TARGET_P9_MINMAX)
4245 if (have_cpu)
4247 if (cpu_index == PROCESSOR_POWER9)
4249 /* legacy behavior: allow -mcpu=power9 with certain
4250 capabilities explicitly disabled. */
4251 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
4253 else
4254 error ("power9 target option is incompatible with %<%s=<xxx>%> "
4255 "for <xxx> less than power9", "-mcpu");
4257 else if ((ISA_3_0_MASKS_SERVER & rs6000_isa_flags_explicit)
4258 != (ISA_3_0_MASKS_SERVER & rs6000_isa_flags
4259 & rs6000_isa_flags_explicit))
4260 /* Enforce that none of the ISA_3_0_MASKS_SERVER flags
4261 were explicitly cleared. */
4262 error ("%qs incompatible with explicitly disabled options",
4263 "-mpower9-minmax");
4264 else
4265 rs6000_isa_flags |= ISA_3_0_MASKS_SERVER;
4267 else if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
4268 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~ignore_masks);
4269 else if (TARGET_VSX)
4270 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~ignore_masks);
4271 else if (TARGET_POPCNTD)
4272 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~ignore_masks);
4273 else if (TARGET_DFP)
4274 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~ignore_masks);
4275 else if (TARGET_CMPB)
4276 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~ignore_masks);
4277 else if (TARGET_FPRND)
4278 rs6000_isa_flags |= (ISA_2_4_MASKS & ~ignore_masks);
4279 else if (TARGET_POPCNTB)
4280 rs6000_isa_flags |= (ISA_2_2_MASKS & ~ignore_masks);
4281 else if (TARGET_ALTIVEC)
4282 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~ignore_masks);
4284 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
4286 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
4287 error ("%qs requires %qs", "-mcrypto", "-maltivec");
4288 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
4291 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
4293 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
4294 error ("%qs requires %qs", "-mdirect-move", "-mvsx");
4295 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
4298 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
4300 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4301 error ("%qs requires %qs", "-mpower8-vector", "-maltivec");
4302 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4305 if (TARGET_P8_VECTOR && !TARGET_VSX)
4307 if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4308 && (rs6000_isa_flags_explicit & OPTION_MASK_VSX))
4309 error ("%qs requires %qs", "-mpower8-vector", "-mvsx");
4310 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR) == 0)
4312 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4313 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4314 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4316 else
4318 /* OPTION_MASK_P8_VECTOR is explicit, and OPTION_MASK_VSX is
4319 not explicit. */
4320 rs6000_isa_flags |= OPTION_MASK_VSX;
4321 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4325 if (TARGET_DFP && !TARGET_HARD_FLOAT)
4327 if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
4328 error ("%qs requires %qs", "-mhard-dfp", "-mhard-float");
4329 rs6000_isa_flags &= ~OPTION_MASK_DFP;
4332 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
4333 silently turn off quad memory mode. */
4334 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
4336 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4337 warning (0, N_("-mquad-memory requires 64-bit mode"));
4339 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
4340 warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
4342 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
4343 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
4346 /* Non-atomic quad memory load/store are disabled for little endian, since
4347 the words are reversed, but atomic operations can still be done by
4348 swapping the words. */
4349 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
4351 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4352 warning (0, N_("-mquad-memory is not available in little endian "
4353 "mode"));
4355 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
4358 /* Assume if the user asked for normal quad memory instructions, they want
4359 the atomic versions as well, unless they explicity told us not to use quad
4360 word atomic instructions. */
4361 if (TARGET_QUAD_MEMORY
4362 && !TARGET_QUAD_MEMORY_ATOMIC
4363 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
4364 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
4366 /* Enable power8 fusion if we are tuning for power8, even if we aren't
4367 generating power8 instructions. */
4368 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
4369 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
4370 & OPTION_MASK_P8_FUSION);
4372 /* Setting additional fusion flags turns on base fusion. */
4373 if (!TARGET_P8_FUSION && (TARGET_P8_FUSION_SIGN || TARGET_TOC_FUSION))
4375 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4377 if (TARGET_P8_FUSION_SIGN)
4378 error ("%qs requires %qs", "-mpower8-fusion-sign",
4379 "-mpower8-fusion");
4381 if (TARGET_TOC_FUSION)
4382 error ("%qs requires %qs", "-mtoc-fusion", "-mpower8-fusion");
4384 rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
4386 else
4387 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4390 /* Power9 fusion is a superset over power8 fusion. */
4391 if (TARGET_P9_FUSION && !TARGET_P8_FUSION)
4393 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4395 /* We prefer to not mention undocumented options in
4396 error messages. However, if users have managed to select
4397 power9-fusion without selecting power8-fusion, they
4398 already know about undocumented flags. */
4399 error ("%qs requires %qs", "-mpower9-fusion", "-mpower8-fusion");
4400 rs6000_isa_flags &= ~OPTION_MASK_P9_FUSION;
4402 else
4403 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4406 /* Enable power9 fusion if we are tuning for power9, even if we aren't
4407 generating power9 instructions. */
4408 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P9_FUSION))
4409 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
4410 & OPTION_MASK_P9_FUSION);
4412 /* Power8 does not fuse sign extended loads with the addis. If we are
4413 optimizing at high levels for speed, convert a sign extended load into a
4414 zero extending load, and an explicit sign extension. */
4415 if (TARGET_P8_FUSION
4416 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
4417 && optimize_function_for_speed_p (cfun)
4418 && optimize >= 3)
4419 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
4421 /* TOC fusion requires 64-bit and medium/large code model. */
4422 if (TARGET_TOC_FUSION && !TARGET_POWERPC64)
4424 rs6000_isa_flags &= ~OPTION_MASK_TOC_FUSION;
4425 if ((rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION) != 0)
4426 warning (0, N_("-mtoc-fusion requires 64-bit"));
4429 if (TARGET_TOC_FUSION && (TARGET_CMODEL == CMODEL_SMALL))
4431 rs6000_isa_flags &= ~OPTION_MASK_TOC_FUSION;
4432 if ((rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION) != 0)
4433 warning (0, N_("-mtoc-fusion requires medium/large code model"));
4436 /* Turn on -mtoc-fusion by default if p8-fusion and 64-bit medium/large code
4437 model. */
4438 if (TARGET_P8_FUSION && !TARGET_TOC_FUSION && TARGET_POWERPC64
4439 && (TARGET_CMODEL != CMODEL_SMALL)
4440 && !(rs6000_isa_flags_explicit & OPTION_MASK_TOC_FUSION))
4441 rs6000_isa_flags |= OPTION_MASK_TOC_FUSION;
4443 /* ISA 3.0 vector instructions include ISA 2.07. */
4444 if (TARGET_P9_VECTOR && !TARGET_P8_VECTOR)
4446 /* We prefer to not mention undocumented options in
4447 error messages. However, if users have managed to select
4448 power9-vector without selecting power8-vector, they
4449 already know about undocumented flags. */
4450 if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) &&
4451 (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR))
4452 error ("%qs requires %qs", "-mpower9-vector", "-mpower8-vector");
4453 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) == 0)
4455 rs6000_isa_flags &= ~OPTION_MASK_P9_VECTOR;
4456 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4457 rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
4459 else
4461 /* OPTION_MASK_P9_VECTOR is explicit and
4462 OPTION_MASK_P8_VECTOR is not explicit. */
4463 rs6000_isa_flags |= OPTION_MASK_P8_VECTOR;
4464 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4468 /* Set -mallow-movmisalign to explicitly on if we have full ISA 2.07
4469 support. If we only have ISA 2.06 support, and the user did not specify
4470 the switch, leave it set to -1 so the movmisalign patterns are enabled,
4471 but we don't enable the full vectorization support */
4472 if (TARGET_ALLOW_MOVMISALIGN == -1 && TARGET_P8_VECTOR && TARGET_DIRECT_MOVE)
4473 TARGET_ALLOW_MOVMISALIGN = 1;
4475 else if (TARGET_ALLOW_MOVMISALIGN && !TARGET_VSX)
4477 if (TARGET_ALLOW_MOVMISALIGN > 0
4478 && global_options_set.x_TARGET_ALLOW_MOVMISALIGN)
4479 error ("%qs requires %qs", "-mallow-movmisalign", "-mvsx");
4481 TARGET_ALLOW_MOVMISALIGN = 0;
4484 /* Determine when unaligned vector accesses are permitted, and when
4485 they are preferred over masked Altivec loads. Note that if
4486 TARGET_ALLOW_MOVMISALIGN has been disabled by the user, then
4487 TARGET_EFFICIENT_UNALIGNED_VSX must be as well. The converse is
4488 not true. */
4489 if (TARGET_EFFICIENT_UNALIGNED_VSX)
4491 if (!TARGET_VSX)
4493 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4494 error ("%qs requires %qs", "-mefficient-unaligned-vsx", "-mvsx");
4496 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4499 else if (!TARGET_ALLOW_MOVMISALIGN)
4501 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4502 error ("%qs requires %qs", "-munefficient-unaligned-vsx",
4503 "-mallow-movmisalign");
4505 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4509 /* Set long double size before the IEEE 128-bit tests. */
4510 if (!global_options_set.x_rs6000_long_double_type_size)
4512 if (main_target_opt != NULL
4513 && (main_target_opt->x_rs6000_long_double_type_size
4514 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
4515 error ("target attribute or pragma changes long double size");
4516 else
4517 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
4520 /* Set -mabi=ieeelongdouble on some old targets. Note, AIX and Darwin
4521 explicitly redefine TARGET_IEEEQUAD to 0, so those systems will not
4522 pick up this default. */
4523 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
4524 if (!global_options_set.x_rs6000_ieeequad)
4525 rs6000_ieeequad = 1;
4526 #endif
4528 /* Enable the default support for IEEE 128-bit floating point on Linux VSX
4529 sytems, but don't enable the __float128 keyword. */
4530 if (TARGET_VSX && TARGET_LONG_DOUBLE_128
4531 && (TARGET_FLOAT128_ENABLE_TYPE || TARGET_IEEEQUAD)
4532 && ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_TYPE) == 0))
4533 rs6000_isa_flags |= OPTION_MASK_FLOAT128_TYPE;
4535 /* IEEE 128-bit floating point requires VSX support. */
4536 if (!TARGET_VSX)
4538 if (TARGET_FLOAT128_KEYWORD)
4540 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
4541 error ("%qs requires VSX support", "-mfloat128");
4543 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4544 | OPTION_MASK_FLOAT128_KEYWORD
4545 | OPTION_MASK_FLOAT128_HW);
4548 else if (TARGET_FLOAT128_TYPE)
4550 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_TYPE) != 0)
4551 error ("%qs requires VSX support", "-mfloat128-type");
4553 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4554 | OPTION_MASK_FLOAT128_KEYWORD
4555 | OPTION_MASK_FLOAT128_HW);
4559 /* -mfloat128 and -mfloat128-hardware internally require the underlying IEEE
4560 128-bit floating point support to be enabled. */
4561 if (!TARGET_FLOAT128_TYPE)
4563 if (TARGET_FLOAT128_KEYWORD)
4565 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
4567 error ("%qs requires %qs", "-mfloat128", "-mfloat128-type");
4568 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4569 | OPTION_MASK_FLOAT128_KEYWORD
4570 | OPTION_MASK_FLOAT128_HW);
4572 else
4573 rs6000_isa_flags |= OPTION_MASK_FLOAT128_TYPE;
4576 if (TARGET_FLOAT128_HW)
4578 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
4580 error ("%qs requires %qs", "-mfloat128-hardware",
4581 "-mfloat128-type");
4582 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
4584 else
4585 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_TYPE
4586 | OPTION_MASK_FLOAT128_KEYWORD
4587 | OPTION_MASK_FLOAT128_HW);
4591 /* If we have -mfloat128-type and full ISA 3.0 support, enable
4592 -mfloat128-hardware by default. However, don't enable the __float128
4593 keyword. If the user explicitly turned on -mfloat128-hardware, enable the
4594 -mfloat128 option as well if it was not already set. */
4595 if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_HW
4596 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) == ISA_3_0_MASKS_IEEE
4597 && !(rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW))
4598 rs6000_isa_flags |= OPTION_MASK_FLOAT128_HW;
4600 if (TARGET_FLOAT128_HW
4601 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) != ISA_3_0_MASKS_IEEE)
4603 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
4604 error ("%qs requires full ISA 3.0 support", "-mfloat128-hardware");
4606 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
4609 if (TARGET_FLOAT128_HW && !TARGET_64BIT)
4611 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
4612 error ("%qs requires %qs", "-mfloat128-hardware", "-m64");
4614 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
4617 if (TARGET_FLOAT128_HW && !TARGET_FLOAT128_KEYWORD
4618 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0
4619 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) == 0)
4620 rs6000_isa_flags |= OPTION_MASK_FLOAT128_KEYWORD;
4622 /* Print the options after updating the defaults. */
4623 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4624 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
4626 /* E500mc does "better" if we inline more aggressively. Respect the
4627 user's opinion, though. */
4628 if (rs6000_block_move_inline_limit == 0
4629 && (rs6000_cpu == PROCESSOR_PPCE500MC
4630 || rs6000_cpu == PROCESSOR_PPCE500MC64
4631 || rs6000_cpu == PROCESSOR_PPCE5500
4632 || rs6000_cpu == PROCESSOR_PPCE6500))
4633 rs6000_block_move_inline_limit = 128;
4635 /* store_one_arg depends on expand_block_move to handle at least the
4636 size of reg_parm_stack_space. */
4637 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
4638 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
4640 if (global_init_p)
4642 /* If the appropriate debug option is enabled, replace the target hooks
4643 with debug versions that call the real version and then prints
4644 debugging information. */
4645 if (TARGET_DEBUG_COST)
4647 targetm.rtx_costs = rs6000_debug_rtx_costs;
4648 targetm.address_cost = rs6000_debug_address_cost;
4649 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
4652 if (TARGET_DEBUG_ADDR)
4654 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
4655 targetm.legitimize_address = rs6000_debug_legitimize_address;
4656 rs6000_secondary_reload_class_ptr
4657 = rs6000_debug_secondary_reload_class;
4658 rs6000_secondary_memory_needed_ptr
4659 = rs6000_debug_secondary_memory_needed;
4660 rs6000_cannot_change_mode_class_ptr
4661 = rs6000_debug_cannot_change_mode_class;
4662 rs6000_preferred_reload_class_ptr
4663 = rs6000_debug_preferred_reload_class;
4664 rs6000_legitimize_reload_address_ptr
4665 = rs6000_debug_legitimize_reload_address;
4666 rs6000_mode_dependent_address_ptr
4667 = rs6000_debug_mode_dependent_address;
4670 if (rs6000_veclibabi_name)
4672 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
4673 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
4674 else
4676 error ("unknown vectorization library ABI type (%qs) for "
4677 "%qs switch", rs6000_veclibabi_name, "-mveclibabi=");
4678 ret = false;
4683 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
4684 target attribute or pragma which automatically enables both options,
4685 unless the altivec ABI was set. This is set by default for 64-bit, but
4686 not for 32-bit. */
4687 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
4688 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC
4689 | OPTION_MASK_FLOAT128_TYPE
4690 | OPTION_MASK_FLOAT128_KEYWORD)
4691 & ~rs6000_isa_flags_explicit);
4693 /* Enable Altivec ABI for AIX -maltivec. */
4694 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
4696 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
4697 error ("target attribute or pragma changes AltiVec ABI");
4698 else
4699 rs6000_altivec_abi = 1;
4702 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
4703 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
4704 be explicitly overridden in either case. */
4705 if (TARGET_ELF)
4707 if (!global_options_set.x_rs6000_altivec_abi
4708 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
4710 if (main_target_opt != NULL &&
4711 !main_target_opt->x_rs6000_altivec_abi)
4712 error ("target attribute or pragma changes AltiVec ABI");
4713 else
4714 rs6000_altivec_abi = 1;
4718 /* Set the Darwin64 ABI as default for 64-bit Darwin.
4719 So far, the only darwin64 targets are also MACH-O. */
4720 if (TARGET_MACHO
4721 && DEFAULT_ABI == ABI_DARWIN
4722 && TARGET_64BIT)
4724 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
4725 error ("target attribute or pragma changes darwin64 ABI");
4726 else
4728 rs6000_darwin64_abi = 1;
4729 /* Default to natural alignment, for better performance. */
4730 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
4734 /* Place FP constants in the constant pool instead of TOC
4735 if section anchors enabled. */
4736 if (flag_section_anchors
4737 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
4738 TARGET_NO_FP_IN_TOC = 1;
4740 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4741 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
4743 #ifdef SUBTARGET_OVERRIDE_OPTIONS
4744 SUBTARGET_OVERRIDE_OPTIONS;
4745 #endif
4746 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
4747 SUBSUBTARGET_OVERRIDE_OPTIONS;
4748 #endif
4749 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
4750 SUB3TARGET_OVERRIDE_OPTIONS;
4751 #endif
4753 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4754 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
4756 /* For the E500 family of cores, reset the single/double FP flags to let us
4757 check that they remain constant across attributes or pragmas. Also,
4758 clear a possible request for string instructions, not supported and which
4759 we might have silently queried above for -Os.
4761 For other families, clear ISEL in case it was set implicitly.
4764 switch (rs6000_cpu)
4766 case PROCESSOR_PPC8540:
4767 case PROCESSOR_PPC8548:
4768 case PROCESSOR_PPCE500MC:
4769 case PROCESSOR_PPCE500MC64:
4770 case PROCESSOR_PPCE5500:
4771 case PROCESSOR_PPCE6500:
4773 rs6000_single_float = 0;
4774 rs6000_double_float = 0;
4776 rs6000_isa_flags &= ~OPTION_MASK_STRING;
4778 break;
4780 default:
4782 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
4783 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
4785 break;
4788 if (main_target_opt)
4790 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
4791 error ("target attribute or pragma changes single precision floating "
4792 "point");
4793 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
4794 error ("target attribute or pragma changes double precision floating "
4795 "point");
4798 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
4799 && rs6000_cpu != PROCESSOR_POWER5
4800 && rs6000_cpu != PROCESSOR_POWER6
4801 && rs6000_cpu != PROCESSOR_POWER7
4802 && rs6000_cpu != PROCESSOR_POWER8
4803 && rs6000_cpu != PROCESSOR_POWER9
4804 && rs6000_cpu != PROCESSOR_PPCA2
4805 && rs6000_cpu != PROCESSOR_CELL
4806 && rs6000_cpu != PROCESSOR_PPC476);
4807 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
4808 || rs6000_cpu == PROCESSOR_POWER5
4809 || rs6000_cpu == PROCESSOR_POWER7
4810 || rs6000_cpu == PROCESSOR_POWER8);
4811 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
4812 || rs6000_cpu == PROCESSOR_POWER5
4813 || rs6000_cpu == PROCESSOR_POWER6
4814 || rs6000_cpu == PROCESSOR_POWER7
4815 || rs6000_cpu == PROCESSOR_POWER8
4816 || rs6000_cpu == PROCESSOR_POWER9
4817 || rs6000_cpu == PROCESSOR_PPCE500MC
4818 || rs6000_cpu == PROCESSOR_PPCE500MC64
4819 || rs6000_cpu == PROCESSOR_PPCE5500
4820 || rs6000_cpu == PROCESSOR_PPCE6500);
4822 /* Allow debug switches to override the above settings. These are set to -1
4823 in rs6000.opt to indicate the user hasn't directly set the switch. */
4824 if (TARGET_ALWAYS_HINT >= 0)
4825 rs6000_always_hint = TARGET_ALWAYS_HINT;
4827 if (TARGET_SCHED_GROUPS >= 0)
4828 rs6000_sched_groups = TARGET_SCHED_GROUPS;
4830 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
4831 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
4833 rs6000_sched_restricted_insns_priority
4834 = (rs6000_sched_groups ? 1 : 0);
4836 /* Handle -msched-costly-dep option. */
4837 rs6000_sched_costly_dep
4838 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
4840 if (rs6000_sched_costly_dep_str)
4842 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
4843 rs6000_sched_costly_dep = no_dep_costly;
4844 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
4845 rs6000_sched_costly_dep = all_deps_costly;
4846 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
4847 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
4848 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
4849 rs6000_sched_costly_dep = store_to_load_dep_costly;
4850 else
4851 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
4852 atoi (rs6000_sched_costly_dep_str));
4855 /* Handle -minsert-sched-nops option. */
4856 rs6000_sched_insert_nops
4857 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
4859 if (rs6000_sched_insert_nops_str)
4861 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
4862 rs6000_sched_insert_nops = sched_finish_none;
4863 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
4864 rs6000_sched_insert_nops = sched_finish_pad_groups;
4865 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
4866 rs6000_sched_insert_nops = sched_finish_regroup_exact;
4867 else
4868 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
4869 atoi (rs6000_sched_insert_nops_str));
4872 /* Handle stack protector */
4873 if (!global_options_set.x_rs6000_stack_protector_guard)
4874 #ifdef TARGET_THREAD_SSP_OFFSET
4875 rs6000_stack_protector_guard = SSP_TLS;
4876 #else
4877 rs6000_stack_protector_guard = SSP_GLOBAL;
4878 #endif
4880 #ifdef TARGET_THREAD_SSP_OFFSET
4881 rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
4882 rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
4883 #endif
4885 if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
4887 char *endp;
4888 const char *str = rs6000_stack_protector_guard_offset_str;
4890 errno = 0;
4891 long offset = strtol (str, &endp, 0);
4892 if (!*str || *endp || errno)
4893 error ("%qs is not a valid number in %qs", str,
4894 "-mstack-protector-guard-offset=");
4896 if (!IN_RANGE (offset, -0x8000, 0x7fff)
4897 || (TARGET_64BIT && (offset & 3)))
4898 error ("%qs is not a valid offset in %qs", str,
4899 "-mstack-protector-guard-offset=");
4901 rs6000_stack_protector_guard_offset = offset;
4904 if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
4906 const char *str = rs6000_stack_protector_guard_reg_str;
4907 int reg = decode_reg_name (str);
4909 if (!IN_RANGE (reg, 1, 31))
4910 error ("%qs is not a valid base register in %qs", str,
4911 "-mstack-protector-guard-reg=");
4913 rs6000_stack_protector_guard_reg = reg;
4916 if (rs6000_stack_protector_guard == SSP_TLS
4917 && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
4918 error ("%qs needs a valid base register", "-mstack-protector-guard=tls");
4920 if (global_init_p)
4922 #ifdef TARGET_REGNAMES
4923 /* If the user desires alternate register names, copy in the
4924 alternate names now. */
4925 if (TARGET_REGNAMES)
4926 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
4927 #endif
4929 /* Set aix_struct_return last, after the ABI is determined.
4930 If -maix-struct-return or -msvr4-struct-return was explicitly
4931 used, don't override with the ABI default. */
4932 if (!global_options_set.x_aix_struct_return)
4933 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
4935 #if 0
4936 /* IBM XL compiler defaults to unsigned bitfields. */
4937 if (TARGET_XL_COMPAT)
4938 flag_signed_bitfields = 0;
4939 #endif
4941 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
4942 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
4944 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
4946 /* We can only guarantee the availability of DI pseudo-ops when
4947 assembling for 64-bit targets. */
4948 if (!TARGET_64BIT)
4950 targetm.asm_out.aligned_op.di = NULL;
4951 targetm.asm_out.unaligned_op.di = NULL;
4955 /* Set branch target alignment, if not optimizing for size. */
4956 if (!optimize_size)
4958 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
4959 aligned 8byte to avoid misprediction by the branch predictor. */
4960 if (rs6000_cpu == PROCESSOR_TITAN
4961 || rs6000_cpu == PROCESSOR_CELL)
4963 if (align_functions <= 0)
4964 align_functions = 8;
4965 if (align_jumps <= 0)
4966 align_jumps = 8;
4967 if (align_loops <= 0)
4968 align_loops = 8;
4970 if (rs6000_align_branch_targets)
4972 if (align_functions <= 0)
4973 align_functions = 16;
4974 if (align_jumps <= 0)
4975 align_jumps = 16;
4976 if (align_loops <= 0)
4978 can_override_loop_align = 1;
4979 align_loops = 16;
4982 if (align_jumps_max_skip <= 0)
4983 align_jumps_max_skip = 15;
4984 if (align_loops_max_skip <= 0)
4985 align_loops_max_skip = 15;
4988 /* Arrange to save and restore machine status around nested functions. */
4989 init_machine_status = rs6000_init_machine_status;
4991 /* We should always be splitting complex arguments, but we can't break
4992 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
4993 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
4994 targetm.calls.split_complex_arg = NULL;
4996 /* The AIX and ELFv1 ABIs define standard function descriptors. */
4997 if (DEFAULT_ABI == ABI_AIX)
4998 targetm.calls.custom_function_descriptors = 0;
5001 /* Initialize rs6000_cost with the appropriate target costs. */
5002 if (optimize_size)
5003 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
5004 else
5005 switch (rs6000_cpu)
5007 case PROCESSOR_RS64A:
5008 rs6000_cost = &rs64a_cost;
5009 break;
5011 case PROCESSOR_MPCCORE:
5012 rs6000_cost = &mpccore_cost;
5013 break;
5015 case PROCESSOR_PPC403:
5016 rs6000_cost = &ppc403_cost;
5017 break;
5019 case PROCESSOR_PPC405:
5020 rs6000_cost = &ppc405_cost;
5021 break;
5023 case PROCESSOR_PPC440:
5024 rs6000_cost = &ppc440_cost;
5025 break;
5027 case PROCESSOR_PPC476:
5028 rs6000_cost = &ppc476_cost;
5029 break;
5031 case PROCESSOR_PPC601:
5032 rs6000_cost = &ppc601_cost;
5033 break;
5035 case PROCESSOR_PPC603:
5036 rs6000_cost = &ppc603_cost;
5037 break;
5039 case PROCESSOR_PPC604:
5040 rs6000_cost = &ppc604_cost;
5041 break;
5043 case PROCESSOR_PPC604e:
5044 rs6000_cost = &ppc604e_cost;
5045 break;
5047 case PROCESSOR_PPC620:
5048 rs6000_cost = &ppc620_cost;
5049 break;
5051 case PROCESSOR_PPC630:
5052 rs6000_cost = &ppc630_cost;
5053 break;
5055 case PROCESSOR_CELL:
5056 rs6000_cost = &ppccell_cost;
5057 break;
5059 case PROCESSOR_PPC750:
5060 case PROCESSOR_PPC7400:
5061 rs6000_cost = &ppc750_cost;
5062 break;
5064 case PROCESSOR_PPC7450:
5065 rs6000_cost = &ppc7450_cost;
5066 break;
5068 case PROCESSOR_PPC8540:
5069 case PROCESSOR_PPC8548:
5070 rs6000_cost = &ppc8540_cost;
5071 break;
5073 case PROCESSOR_PPCE300C2:
5074 case PROCESSOR_PPCE300C3:
5075 rs6000_cost = &ppce300c2c3_cost;
5076 break;
5078 case PROCESSOR_PPCE500MC:
5079 rs6000_cost = &ppce500mc_cost;
5080 break;
5082 case PROCESSOR_PPCE500MC64:
5083 rs6000_cost = &ppce500mc64_cost;
5084 break;
5086 case PROCESSOR_PPCE5500:
5087 rs6000_cost = &ppce5500_cost;
5088 break;
5090 case PROCESSOR_PPCE6500:
5091 rs6000_cost = &ppce6500_cost;
5092 break;
5094 case PROCESSOR_TITAN:
5095 rs6000_cost = &titan_cost;
5096 break;
5098 case PROCESSOR_POWER4:
5099 case PROCESSOR_POWER5:
5100 rs6000_cost = &power4_cost;
5101 break;
5103 case PROCESSOR_POWER6:
5104 rs6000_cost = &power6_cost;
5105 break;
5107 case PROCESSOR_POWER7:
5108 rs6000_cost = &power7_cost;
5109 break;
5111 case PROCESSOR_POWER8:
5112 rs6000_cost = &power8_cost;
5113 break;
5115 case PROCESSOR_POWER9:
5116 rs6000_cost = &power9_cost;
5117 break;
5119 case PROCESSOR_PPCA2:
5120 rs6000_cost = &ppca2_cost;
5121 break;
5123 default:
5124 gcc_unreachable ();
5127 if (global_init_p)
5129 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
5130 rs6000_cost->simultaneous_prefetches,
5131 global_options.x_param_values,
5132 global_options_set.x_param_values);
5133 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
5134 global_options.x_param_values,
5135 global_options_set.x_param_values);
5136 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
5137 rs6000_cost->cache_line_size,
5138 global_options.x_param_values,
5139 global_options_set.x_param_values);
5140 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
5141 global_options.x_param_values,
5142 global_options_set.x_param_values);
5144 /* Increase loop peeling limits based on performance analysis. */
5145 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
5146 global_options.x_param_values,
5147 global_options_set.x_param_values);
5148 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
5149 global_options.x_param_values,
5150 global_options_set.x_param_values);
5152 /* Use the 'model' -fsched-pressure algorithm by default. */
5153 maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM,
5154 SCHED_PRESSURE_MODEL,
5155 global_options.x_param_values,
5156 global_options_set.x_param_values);
5158 /* If using typedef char *va_list, signal that
5159 __builtin_va_start (&ap, 0) can be optimized to
5160 ap = __builtin_next_arg (0). */
5161 if (DEFAULT_ABI != ABI_V4)
5162 targetm.expand_builtin_va_start = NULL;
5165 /* Set up single/double float flags.
5166 If TARGET_HARD_FLOAT is set, but neither single or double is set,
5167 then set both flags. */
5168 if (TARGET_HARD_FLOAT && rs6000_single_float == 0 && rs6000_double_float == 0)
5169 rs6000_single_float = rs6000_double_float = 1;
5171 /* If not explicitly specified via option, decide whether to generate indexed
5172 load/store instructions. A value of -1 indicates that the
5173 initial value of this variable has not been overwritten. During
5174 compilation, TARGET_AVOID_XFORM is either 0 or 1. */
5175 if (TARGET_AVOID_XFORM == -1)
5176 /* Avoid indexed addressing when targeting Power6 in order to avoid the
5177 DERAT mispredict penalty. However the LVE and STVE altivec instructions
5178 need indexed accesses and the type used is the scalar type of the element
5179 being loaded or stored. */
5180 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
5181 && !TARGET_ALTIVEC);
5183 /* Set the -mrecip options. */
5184 if (rs6000_recip_name)
5186 char *p = ASTRDUP (rs6000_recip_name);
5187 char *q;
5188 unsigned int mask, i;
5189 bool invert;
5191 while ((q = strtok (p, ",")) != NULL)
5193 p = NULL;
5194 if (*q == '!')
5196 invert = true;
5197 q++;
5199 else
5200 invert = false;
5202 if (!strcmp (q, "default"))
5203 mask = ((TARGET_RECIP_PRECISION)
5204 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
5205 else
5207 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
5208 if (!strcmp (q, recip_options[i].string))
5210 mask = recip_options[i].mask;
5211 break;
5214 if (i == ARRAY_SIZE (recip_options))
5216 error ("unknown option for %<%s=%s%>", "-mrecip", q);
5217 invert = false;
5218 mask = 0;
5219 ret = false;
5223 if (invert)
5224 rs6000_recip_control &= ~mask;
5225 else
5226 rs6000_recip_control |= mask;
5230 /* Set the builtin mask of the various options used that could affect which
5231 builtins were used. In the past we used target_flags, but we've run out
5232 of bits, and some options like PAIRED are no longer in target_flags. */
5233 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
5234 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
5235 rs6000_print_builtin_options (stderr, 0, "builtin mask",
5236 rs6000_builtin_mask);
5238 /* Initialize all of the registers. */
5239 rs6000_init_hard_regno_mode_ok (global_init_p);
5241 /* Save the initial options in case the user does function specific options */
5242 if (global_init_p)
5243 target_option_default_node = target_option_current_node
5244 = build_target_option_node (&global_options);
5246 /* If not explicitly specified via option, decide whether to generate the
5247 extra blr's required to preserve the link stack on some cpus (eg, 476). */
5248 if (TARGET_LINK_STACK == -1)
5249 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
5251 return ret;
5254 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
5255 define the target cpu type. */
5257 static void
5258 rs6000_option_override (void)
5260 (void) rs6000_option_override_internal (true);
5264 /* Implement targetm.vectorize.builtin_mask_for_load. */
5265 static tree
5266 rs6000_builtin_mask_for_load (void)
5268 /* Don't use lvsl/vperm for P8 and similarly efficient machines. */
5269 if ((TARGET_ALTIVEC && !TARGET_VSX)
5270 || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
5271 return altivec_builtin_mask_for_load;
5272 else
5273 return 0;
5276 /* Implement LOOP_ALIGN. */
5278 rs6000_loop_align (rtx label)
5280 basic_block bb;
5281 int ninsns;
5283 /* Don't override loop alignment if -falign-loops was specified. */
5284 if (!can_override_loop_align)
5285 return align_loops_log;
5287 bb = BLOCK_FOR_INSN (label);
5288 ninsns = num_loop_insns(bb->loop_father);
5290 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
5291 if (ninsns > 4 && ninsns <= 8
5292 && (rs6000_cpu == PROCESSOR_POWER4
5293 || rs6000_cpu == PROCESSOR_POWER5
5294 || rs6000_cpu == PROCESSOR_POWER6
5295 || rs6000_cpu == PROCESSOR_POWER7
5296 || rs6000_cpu == PROCESSOR_POWER8
5297 || rs6000_cpu == PROCESSOR_POWER9))
5298 return 5;
5299 else
5300 return align_loops_log;
5303 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
5304 static int
5305 rs6000_loop_align_max_skip (rtx_insn *label)
5307 return (1 << rs6000_loop_align (label)) - 1;
5310 /* Return true iff, data reference of TYPE can reach vector alignment (16)
5311 after applying N number of iterations. This routine does not determine
5312 how may iterations are required to reach desired alignment. */
5314 static bool
5315 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
5317 if (is_packed)
5318 return false;
5320 if (TARGET_32BIT)
5322 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
5323 return true;
5325 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
5326 return true;
5328 return false;
5330 else
5332 if (TARGET_MACHO)
5333 return false;
5335 /* Assuming that all other types are naturally aligned. CHECKME! */
5336 return true;
5340 /* Return true if the vector misalignment factor is supported by the
5341 target. */
5342 static bool
5343 rs6000_builtin_support_vector_misalignment (machine_mode mode,
5344 const_tree type,
5345 int misalignment,
5346 bool is_packed)
5348 if (TARGET_VSX)
5350 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5351 return true;
5353 /* Return if movmisalign pattern is not supported for this mode. */
5354 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
5355 return false;
5357 if (misalignment == -1)
5359 /* Misalignment factor is unknown at compile time but we know
5360 it's word aligned. */
5361 if (rs6000_vector_alignment_reachable (type, is_packed))
5363 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
5365 if (element_size == 64 || element_size == 32)
5366 return true;
5369 return false;
5372 /* VSX supports word-aligned vector. */
5373 if (misalignment % 4 == 0)
5374 return true;
5376 return false;
5379 /* Implement targetm.vectorize.builtin_vectorization_cost. */
5380 static int
5381 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
5382 tree vectype, int misalign)
5384 unsigned elements;
5385 tree elem_type;
5387 switch (type_of_cost)
5389 case scalar_stmt:
5390 case scalar_load:
5391 case scalar_store:
5392 case vector_stmt:
5393 case vector_load:
5394 case vector_store:
5395 case vec_to_scalar:
5396 case scalar_to_vec:
5397 case cond_branch_not_taken:
5398 return 1;
5400 case vec_perm:
5401 if (TARGET_VSX)
5402 return 3;
5403 else
5404 return 1;
5406 case vec_promote_demote:
5407 if (TARGET_VSX)
5408 return 4;
5409 else
5410 return 1;
5412 case cond_branch_taken:
5413 return 3;
5415 case unaligned_load:
5416 if (TARGET_P9_VECTOR)
5417 return 3;
5419 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5420 return 1;
5422 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5424 elements = TYPE_VECTOR_SUBPARTS (vectype);
5425 if (elements == 2)
5426 /* Double word aligned. */
5427 return 2;
5429 if (elements == 4)
5431 switch (misalign)
5433 case 8:
5434 /* Double word aligned. */
5435 return 2;
5437 case -1:
5438 /* Unknown misalignment. */
5439 case 4:
5440 case 12:
5441 /* Word aligned. */
5442 return 22;
5444 default:
5445 gcc_unreachable ();
5450 if (TARGET_ALTIVEC)
5451 /* Misaligned loads are not supported. */
5452 gcc_unreachable ();
5454 return 2;
5456 case unaligned_store:
5457 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5458 return 1;
5460 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5462 elements = TYPE_VECTOR_SUBPARTS (vectype);
5463 if (elements == 2)
5464 /* Double word aligned. */
5465 return 2;
5467 if (elements == 4)
5469 switch (misalign)
5471 case 8:
5472 /* Double word aligned. */
5473 return 2;
5475 case -1:
5476 /* Unknown misalignment. */
5477 case 4:
5478 case 12:
5479 /* Word aligned. */
5480 return 23;
5482 default:
5483 gcc_unreachable ();
5488 if (TARGET_ALTIVEC)
5489 /* Misaligned stores are not supported. */
5490 gcc_unreachable ();
5492 return 2;
5494 case vec_construct:
5495 /* This is a rough approximation assuming non-constant elements
5496 constructed into a vector via element insertion. FIXME:
5497 vec_construct is not granular enough for uniformly good
5498 decisions. If the initialization is a splat, this is
5499 cheaper than we estimate. Improve this someday. */
5500 elem_type = TREE_TYPE (vectype);
5501 /* 32-bit vectors loaded into registers are stored as double
5502 precision, so we need 2 permutes, 2 converts, and 1 merge
5503 to construct a vector of short floats from them. */
5504 if (SCALAR_FLOAT_TYPE_P (elem_type)
5505 && TYPE_PRECISION (elem_type) == 32)
5506 return 5;
5507 /* On POWER9, integer vector types are built up in GPRs and then
5508 use a direct move (2 cycles). For POWER8 this is even worse,
5509 as we need two direct moves and a merge, and the direct moves
5510 are five cycles. */
5511 else if (INTEGRAL_TYPE_P (elem_type))
5513 if (TARGET_P9_VECTOR)
5514 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 2;
5515 else
5516 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 5;
5518 else
5519 /* V2DFmode doesn't need a direct move. */
5520 return 2;
5522 default:
5523 gcc_unreachable ();
5527 /* Implement targetm.vectorize.preferred_simd_mode. */
5529 static machine_mode
5530 rs6000_preferred_simd_mode (machine_mode mode)
5532 if (TARGET_VSX)
5533 switch (mode)
5535 case E_DFmode:
5536 return V2DFmode;
5537 default:;
5539 if (TARGET_ALTIVEC || TARGET_VSX)
5540 switch (mode)
5542 case E_SFmode:
5543 return V4SFmode;
5544 case E_TImode:
5545 return V1TImode;
5546 case E_DImode:
5547 return V2DImode;
5548 case E_SImode:
5549 return V4SImode;
5550 case E_HImode:
5551 return V8HImode;
5552 case E_QImode:
5553 return V16QImode;
5554 default:;
5556 if (TARGET_PAIRED_FLOAT
5557 && mode == SFmode)
5558 return V2SFmode;
5559 return word_mode;
5562 typedef struct _rs6000_cost_data
5564 struct loop *loop_info;
5565 unsigned cost[3];
5566 } rs6000_cost_data;
5568 /* Test for likely overcommitment of vector hardware resources. If a
5569 loop iteration is relatively large, and too large a percentage of
5570 instructions in the loop are vectorized, the cost model may not
5571 adequately reflect delays from unavailable vector resources.
5572 Penalize the loop body cost for this case. */
5574 static void
5575 rs6000_density_test (rs6000_cost_data *data)
5577 const int DENSITY_PCT_THRESHOLD = 85;
5578 const int DENSITY_SIZE_THRESHOLD = 70;
5579 const int DENSITY_PENALTY = 10;
5580 struct loop *loop = data->loop_info;
5581 basic_block *bbs = get_loop_body (loop);
5582 int nbbs = loop->num_nodes;
5583 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
5584 int i, density_pct;
5586 for (i = 0; i < nbbs; i++)
5588 basic_block bb = bbs[i];
5589 gimple_stmt_iterator gsi;
5591 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5593 gimple *stmt = gsi_stmt (gsi);
5594 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5596 if (!STMT_VINFO_RELEVANT_P (stmt_info)
5597 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
5598 not_vec_cost++;
5602 free (bbs);
5603 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
5605 if (density_pct > DENSITY_PCT_THRESHOLD
5606 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
5608 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
5609 if (dump_enabled_p ())
5610 dump_printf_loc (MSG_NOTE, vect_location,
5611 "density %d%%, cost %d exceeds threshold, penalizing "
5612 "loop body cost by %d%%", density_pct,
5613 vec_cost + not_vec_cost, DENSITY_PENALTY);
5617 /* Implement targetm.vectorize.init_cost. */
5619 /* For each vectorized loop, this var holds TRUE iff a non-memory vector
5620 instruction is needed by the vectorization. */
5621 static bool rs6000_vect_nonmem;
5623 static void *
5624 rs6000_init_cost (struct loop *loop_info)
5626 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
5627 data->loop_info = loop_info;
5628 data->cost[vect_prologue] = 0;
5629 data->cost[vect_body] = 0;
5630 data->cost[vect_epilogue] = 0;
5631 rs6000_vect_nonmem = false;
5632 return data;
5635 /* Implement targetm.vectorize.add_stmt_cost. */
5637 static unsigned
5638 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
5639 struct _stmt_vec_info *stmt_info, int misalign,
5640 enum vect_cost_model_location where)
5642 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
5643 unsigned retval = 0;
5645 if (flag_vect_cost_model)
5647 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
5648 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
5649 misalign);
5650 /* Statements in an inner loop relative to the loop being
5651 vectorized are weighted more heavily. The value here is
5652 arbitrary and could potentially be improved with analysis. */
5653 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
5654 count *= 50; /* FIXME. */
5656 retval = (unsigned) (count * stmt_cost);
5657 cost_data->cost[where] += retval;
5659 /* Check whether we're doing something other than just a copy loop.
5660 Not all such loops may be profitably vectorized; see
5661 rs6000_finish_cost. */
5662 if ((kind == vec_to_scalar || kind == vec_perm
5663 || kind == vec_promote_demote || kind == vec_construct
5664 || kind == scalar_to_vec)
5665 || (where == vect_body && kind == vector_stmt))
5666 rs6000_vect_nonmem = true;
5669 return retval;
5672 /* Implement targetm.vectorize.finish_cost. */
5674 static void
5675 rs6000_finish_cost (void *data, unsigned *prologue_cost,
5676 unsigned *body_cost, unsigned *epilogue_cost)
5678 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
5680 if (cost_data->loop_info)
5681 rs6000_density_test (cost_data);
5683 /* Don't vectorize minimum-vectorization-factor, simple copy loops
5684 that require versioning for any reason. The vectorization is at
5685 best a wash inside the loop, and the versioning checks make
5686 profitability highly unlikely and potentially quite harmful. */
5687 if (cost_data->loop_info)
5689 loop_vec_info vec_info = loop_vec_info_for_loop (cost_data->loop_info);
5690 if (!rs6000_vect_nonmem
5691 && LOOP_VINFO_VECT_FACTOR (vec_info) == 2
5692 && LOOP_REQUIRES_VERSIONING (vec_info))
5693 cost_data->cost[vect_body] += 10000;
5696 *prologue_cost = cost_data->cost[vect_prologue];
5697 *body_cost = cost_data->cost[vect_body];
5698 *epilogue_cost = cost_data->cost[vect_epilogue];
5701 /* Implement targetm.vectorize.destroy_cost_data. */
5703 static void
5704 rs6000_destroy_cost_data (void *data)
5706 free (data);
5709 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
5710 library with vectorized intrinsics. */
5712 static tree
5713 rs6000_builtin_vectorized_libmass (combined_fn fn, tree type_out,
5714 tree type_in)
5716 char name[32];
5717 const char *suffix = NULL;
5718 tree fntype, new_fndecl, bdecl = NULL_TREE;
5719 int n_args = 1;
5720 const char *bname;
5721 machine_mode el_mode, in_mode;
5722 int n, in_n;
5724 /* Libmass is suitable for unsafe math only as it does not correctly support
5725 parts of IEEE with the required precision such as denormals. Only support
5726 it if we have VSX to use the simd d2 or f4 functions.
5727 XXX: Add variable length support. */
5728 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
5729 return NULL_TREE;
5731 el_mode = TYPE_MODE (TREE_TYPE (type_out));
5732 n = TYPE_VECTOR_SUBPARTS (type_out);
5733 in_mode = TYPE_MODE (TREE_TYPE (type_in));
5734 in_n = TYPE_VECTOR_SUBPARTS (type_in);
5735 if (el_mode != in_mode
5736 || n != in_n)
5737 return NULL_TREE;
5739 switch (fn)
5741 CASE_CFN_ATAN2:
5742 CASE_CFN_HYPOT:
5743 CASE_CFN_POW:
5744 n_args = 2;
5745 gcc_fallthrough ();
5747 CASE_CFN_ACOS:
5748 CASE_CFN_ACOSH:
5749 CASE_CFN_ASIN:
5750 CASE_CFN_ASINH:
5751 CASE_CFN_ATAN:
5752 CASE_CFN_ATANH:
5753 CASE_CFN_CBRT:
5754 CASE_CFN_COS:
5755 CASE_CFN_COSH:
5756 CASE_CFN_ERF:
5757 CASE_CFN_ERFC:
5758 CASE_CFN_EXP2:
5759 CASE_CFN_EXP:
5760 CASE_CFN_EXPM1:
5761 CASE_CFN_LGAMMA:
5762 CASE_CFN_LOG10:
5763 CASE_CFN_LOG1P:
5764 CASE_CFN_LOG2:
5765 CASE_CFN_LOG:
5766 CASE_CFN_SIN:
5767 CASE_CFN_SINH:
5768 CASE_CFN_SQRT:
5769 CASE_CFN_TAN:
5770 CASE_CFN_TANH:
5771 if (el_mode == DFmode && n == 2)
5773 bdecl = mathfn_built_in (double_type_node, fn);
5774 suffix = "d2"; /* pow -> powd2 */
5776 else if (el_mode == SFmode && n == 4)
5778 bdecl = mathfn_built_in (float_type_node, fn);
5779 suffix = "4"; /* powf -> powf4 */
5781 else
5782 return NULL_TREE;
5783 if (!bdecl)
5784 return NULL_TREE;
5785 break;
5787 default:
5788 return NULL_TREE;
5791 gcc_assert (suffix != NULL);
5792 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
5793 if (!bname)
5794 return NULL_TREE;
5796 strcpy (name, bname + sizeof ("__builtin_") - 1);
5797 strcat (name, suffix);
5799 if (n_args == 1)
5800 fntype = build_function_type_list (type_out, type_in, NULL);
5801 else if (n_args == 2)
5802 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
5803 else
5804 gcc_unreachable ();
5806 /* Build a function declaration for the vectorized function. */
5807 new_fndecl = build_decl (BUILTINS_LOCATION,
5808 FUNCTION_DECL, get_identifier (name), fntype);
5809 TREE_PUBLIC (new_fndecl) = 1;
5810 DECL_EXTERNAL (new_fndecl) = 1;
5811 DECL_IS_NOVOPS (new_fndecl) = 1;
5812 TREE_READONLY (new_fndecl) = 1;
5814 return new_fndecl;
5817 /* Returns a function decl for a vectorized version of the builtin function
5818 with builtin function code FN and the result vector type TYPE, or NULL_TREE
5819 if it is not available. */
5821 static tree
5822 rs6000_builtin_vectorized_function (unsigned int fn, tree type_out,
5823 tree type_in)
5825 machine_mode in_mode, out_mode;
5826 int in_n, out_n;
5828 if (TARGET_DEBUG_BUILTIN)
5829 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
5830 combined_fn_name (combined_fn (fn)),
5831 GET_MODE_NAME (TYPE_MODE (type_out)),
5832 GET_MODE_NAME (TYPE_MODE (type_in)));
5834 if (TREE_CODE (type_out) != VECTOR_TYPE
5835 || TREE_CODE (type_in) != VECTOR_TYPE)
5836 return NULL_TREE;
5838 out_mode = TYPE_MODE (TREE_TYPE (type_out));
5839 out_n = TYPE_VECTOR_SUBPARTS (type_out);
5840 in_mode = TYPE_MODE (TREE_TYPE (type_in));
5841 in_n = TYPE_VECTOR_SUBPARTS (type_in);
5843 switch (fn)
5845 CASE_CFN_COPYSIGN:
5846 if (VECTOR_UNIT_VSX_P (V2DFmode)
5847 && out_mode == DFmode && out_n == 2
5848 && in_mode == DFmode && in_n == 2)
5849 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
5850 if (VECTOR_UNIT_VSX_P (V4SFmode)
5851 && out_mode == SFmode && out_n == 4
5852 && in_mode == SFmode && in_n == 4)
5853 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
5854 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5855 && out_mode == SFmode && out_n == 4
5856 && in_mode == SFmode && in_n == 4)
5857 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
5858 break;
5859 CASE_CFN_CEIL:
5860 if (VECTOR_UNIT_VSX_P (V2DFmode)
5861 && out_mode == DFmode && out_n == 2
5862 && in_mode == DFmode && in_n == 2)
5863 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
5864 if (VECTOR_UNIT_VSX_P (V4SFmode)
5865 && out_mode == SFmode && out_n == 4
5866 && in_mode == SFmode && in_n == 4)
5867 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
5868 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5869 && out_mode == SFmode && out_n == 4
5870 && in_mode == SFmode && in_n == 4)
5871 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
5872 break;
5873 CASE_CFN_FLOOR:
5874 if (VECTOR_UNIT_VSX_P (V2DFmode)
5875 && out_mode == DFmode && out_n == 2
5876 && in_mode == DFmode && in_n == 2)
5877 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
5878 if (VECTOR_UNIT_VSX_P (V4SFmode)
5879 && out_mode == SFmode && out_n == 4
5880 && in_mode == SFmode && in_n == 4)
5881 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
5882 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5883 && out_mode == SFmode && out_n == 4
5884 && in_mode == SFmode && in_n == 4)
5885 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
5886 break;
5887 CASE_CFN_FMA:
5888 if (VECTOR_UNIT_VSX_P (V2DFmode)
5889 && out_mode == DFmode && out_n == 2
5890 && in_mode == DFmode && in_n == 2)
5891 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
5892 if (VECTOR_UNIT_VSX_P (V4SFmode)
5893 && out_mode == SFmode && out_n == 4
5894 && in_mode == SFmode && in_n == 4)
5895 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
5896 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5897 && out_mode == SFmode && out_n == 4
5898 && in_mode == SFmode && in_n == 4)
5899 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
5900 break;
5901 CASE_CFN_TRUNC:
5902 if (VECTOR_UNIT_VSX_P (V2DFmode)
5903 && out_mode == DFmode && out_n == 2
5904 && in_mode == DFmode && in_n == 2)
5905 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
5906 if (VECTOR_UNIT_VSX_P (V4SFmode)
5907 && out_mode == SFmode && out_n == 4
5908 && in_mode == SFmode && in_n == 4)
5909 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
5910 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5911 && out_mode == SFmode && out_n == 4
5912 && in_mode == SFmode && in_n == 4)
5913 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
5914 break;
5915 CASE_CFN_NEARBYINT:
5916 if (VECTOR_UNIT_VSX_P (V2DFmode)
5917 && flag_unsafe_math_optimizations
5918 && out_mode == DFmode && out_n == 2
5919 && in_mode == DFmode && in_n == 2)
5920 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
5921 if (VECTOR_UNIT_VSX_P (V4SFmode)
5922 && flag_unsafe_math_optimizations
5923 && out_mode == SFmode && out_n == 4
5924 && in_mode == SFmode && in_n == 4)
5925 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
5926 break;
5927 CASE_CFN_RINT:
5928 if (VECTOR_UNIT_VSX_P (V2DFmode)
5929 && !flag_trapping_math
5930 && out_mode == DFmode && out_n == 2
5931 && in_mode == DFmode && in_n == 2)
5932 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
5933 if (VECTOR_UNIT_VSX_P (V4SFmode)
5934 && !flag_trapping_math
5935 && out_mode == SFmode && out_n == 4
5936 && in_mode == SFmode && in_n == 4)
5937 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
5938 break;
5939 default:
5940 break;
5943 /* Generate calls to libmass if appropriate. */
5944 if (rs6000_veclib_handler)
5945 return rs6000_veclib_handler (combined_fn (fn), type_out, type_in);
5947 return NULL_TREE;
5950 /* Implement TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION. */
5952 static tree
5953 rs6000_builtin_md_vectorized_function (tree fndecl, tree type_out,
5954 tree type_in)
5956 machine_mode in_mode, out_mode;
5957 int in_n, out_n;
5959 if (TARGET_DEBUG_BUILTIN)
5960 fprintf (stderr, "rs6000_builtin_md_vectorized_function (%s, %s, %s)\n",
5961 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
5962 GET_MODE_NAME (TYPE_MODE (type_out)),
5963 GET_MODE_NAME (TYPE_MODE (type_in)));
5965 if (TREE_CODE (type_out) != VECTOR_TYPE
5966 || TREE_CODE (type_in) != VECTOR_TYPE)
5967 return NULL_TREE;
5969 out_mode = TYPE_MODE (TREE_TYPE (type_out));
5970 out_n = TYPE_VECTOR_SUBPARTS (type_out);
5971 in_mode = TYPE_MODE (TREE_TYPE (type_in));
5972 in_n = TYPE_VECTOR_SUBPARTS (type_in);
5974 enum rs6000_builtins fn
5975 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
5976 switch (fn)
5978 case RS6000_BUILTIN_RSQRTF:
5979 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
5980 && out_mode == SFmode && out_n == 4
5981 && in_mode == SFmode && in_n == 4)
5982 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
5983 break;
5984 case RS6000_BUILTIN_RSQRT:
5985 if (VECTOR_UNIT_VSX_P (V2DFmode)
5986 && out_mode == DFmode && out_n == 2
5987 && in_mode == DFmode && in_n == 2)
5988 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
5989 break;
5990 case RS6000_BUILTIN_RECIPF:
5991 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
5992 && out_mode == SFmode && out_n == 4
5993 && in_mode == SFmode && in_n == 4)
5994 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
5995 break;
5996 case RS6000_BUILTIN_RECIP:
5997 if (VECTOR_UNIT_VSX_P (V2DFmode)
5998 && out_mode == DFmode && out_n == 2
5999 && in_mode == DFmode && in_n == 2)
6000 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
6001 break;
6002 default:
6003 break;
6005 return NULL_TREE;
6008 /* Default CPU string for rs6000*_file_start functions. */
6009 static const char *rs6000_default_cpu;
6011 /* Do anything needed at the start of the asm file. */
6013 static void
6014 rs6000_file_start (void)
6016 char buffer[80];
6017 const char *start = buffer;
6018 FILE *file = asm_out_file;
6020 rs6000_default_cpu = TARGET_CPU_DEFAULT;
6022 default_file_start ();
6024 if (flag_verbose_asm)
6026 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
6028 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
6030 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
6031 start = "";
6034 if (global_options_set.x_rs6000_cpu_index)
6036 fprintf (file, "%s -mcpu=%s", start,
6037 processor_target_table[rs6000_cpu_index].name);
6038 start = "";
6041 if (global_options_set.x_rs6000_tune_index)
6043 fprintf (file, "%s -mtune=%s", start,
6044 processor_target_table[rs6000_tune_index].name);
6045 start = "";
6048 if (PPC405_ERRATUM77)
6050 fprintf (file, "%s PPC405CR_ERRATUM77", start);
6051 start = "";
6054 #ifdef USING_ELFOS_H
6055 switch (rs6000_sdata)
6057 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
6058 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
6059 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
6060 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
6063 if (rs6000_sdata && g_switch_value)
6065 fprintf (file, "%s -G %d", start,
6066 g_switch_value);
6067 start = "";
6069 #endif
6071 if (*start == '\0')
6072 putc ('\n', file);
6075 #ifdef USING_ELFOS_H
6076 if (!(rs6000_default_cpu && rs6000_default_cpu[0])
6077 && !global_options_set.x_rs6000_cpu_index)
6079 fputs ("\t.machine ", asm_out_file);
6080 if ((rs6000_isa_flags & OPTION_MASK_MODULO) != 0)
6081 fputs ("power9\n", asm_out_file);
6082 else if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
6083 fputs ("power8\n", asm_out_file);
6084 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTD) != 0)
6085 fputs ("power7\n", asm_out_file);
6086 else if ((rs6000_isa_flags & OPTION_MASK_CMPB) != 0)
6087 fputs ("power6\n", asm_out_file);
6088 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTB) != 0)
6089 fputs ("power5\n", asm_out_file);
6090 else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
6091 fputs ("power4\n", asm_out_file);
6092 else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
6093 fputs ("ppc64\n", asm_out_file);
6094 else
6095 fputs ("ppc\n", asm_out_file);
6097 #endif
6099 if (DEFAULT_ABI == ABI_ELFv2)
6100 fprintf (file, "\t.abiversion 2\n");
6104 /* Return nonzero if this function is known to have a null epilogue. */
6107 direct_return (void)
6109 if (reload_completed)
6111 rs6000_stack_t *info = rs6000_stack_info ();
6113 if (info->first_gp_reg_save == 32
6114 && info->first_fp_reg_save == 64
6115 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
6116 && ! info->lr_save_p
6117 && ! info->cr_save_p
6118 && info->vrsave_size == 0
6119 && ! info->push_p)
6120 return 1;
6123 return 0;
6126 /* Return the number of instructions it takes to form a constant in an
6127 integer register. */
6130 num_insns_constant_wide (HOST_WIDE_INT value)
6132 /* signed constant loadable with addi */
6133 if (((unsigned HOST_WIDE_INT) value + 0x8000) < 0x10000)
6134 return 1;
6136 /* constant loadable with addis */
6137 else if ((value & 0xffff) == 0
6138 && (value >> 31 == -1 || value >> 31 == 0))
6139 return 1;
6141 else if (TARGET_POWERPC64)
6143 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
6144 HOST_WIDE_INT high = value >> 31;
6146 if (high == 0 || high == -1)
6147 return 2;
6149 high >>= 1;
6151 if (low == 0)
6152 return num_insns_constant_wide (high) + 1;
6153 else if (high == 0)
6154 return num_insns_constant_wide (low) + 1;
6155 else
6156 return (num_insns_constant_wide (high)
6157 + num_insns_constant_wide (low) + 1);
6160 else
6161 return 2;
6165 num_insns_constant (rtx op, machine_mode mode)
6167 HOST_WIDE_INT low, high;
6169 switch (GET_CODE (op))
6171 case CONST_INT:
6172 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
6173 && rs6000_is_valid_and_mask (op, mode))
6174 return 2;
6175 else
6176 return num_insns_constant_wide (INTVAL (op));
6178 case CONST_WIDE_INT:
6180 int i;
6181 int ins = CONST_WIDE_INT_NUNITS (op) - 1;
6182 for (i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
6183 ins += num_insns_constant_wide (CONST_WIDE_INT_ELT (op, i));
6184 return ins;
6187 case CONST_DOUBLE:
6188 if (mode == SFmode || mode == SDmode)
6190 long l;
6192 if (DECIMAL_FLOAT_MODE_P (mode))
6193 REAL_VALUE_TO_TARGET_DECIMAL32
6194 (*CONST_DOUBLE_REAL_VALUE (op), l);
6195 else
6196 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), l);
6197 return num_insns_constant_wide ((HOST_WIDE_INT) l);
6200 long l[2];
6201 if (DECIMAL_FLOAT_MODE_P (mode))
6202 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (op), l);
6203 else
6204 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (op), l);
6205 high = l[WORDS_BIG_ENDIAN == 0];
6206 low = l[WORDS_BIG_ENDIAN != 0];
6208 if (TARGET_32BIT)
6209 return (num_insns_constant_wide (low)
6210 + num_insns_constant_wide (high));
6211 else
6213 if ((high == 0 && low >= 0)
6214 || (high == -1 && low < 0))
6215 return num_insns_constant_wide (low);
6217 else if (rs6000_is_valid_and_mask (op, mode))
6218 return 2;
6220 else if (low == 0)
6221 return num_insns_constant_wide (high) + 1;
6223 else
6224 return (num_insns_constant_wide (high)
6225 + num_insns_constant_wide (low) + 1);
6228 default:
6229 gcc_unreachable ();
6233 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
6234 If the mode of OP is MODE_VECTOR_INT, this simply returns the
6235 corresponding element of the vector, but for V4SFmode and V2SFmode,
6236 the corresponding "float" is interpreted as an SImode integer. */
6238 HOST_WIDE_INT
6239 const_vector_elt_as_int (rtx op, unsigned int elt)
6241 rtx tmp;
6243 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
6244 gcc_assert (GET_MODE (op) != V2DImode
6245 && GET_MODE (op) != V2DFmode);
6247 tmp = CONST_VECTOR_ELT (op, elt);
6248 if (GET_MODE (op) == V4SFmode
6249 || GET_MODE (op) == V2SFmode)
6250 tmp = gen_lowpart (SImode, tmp);
6251 return INTVAL (tmp);
6254 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
6255 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
6256 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
6257 all items are set to the same value and contain COPIES replicas of the
6258 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
6259 operand and the others are set to the value of the operand's msb. */
6261 static bool
6262 vspltis_constant (rtx op, unsigned step, unsigned copies)
6264 machine_mode mode = GET_MODE (op);
6265 machine_mode inner = GET_MODE_INNER (mode);
6267 unsigned i;
6268 unsigned nunits;
6269 unsigned bitsize;
6270 unsigned mask;
6272 HOST_WIDE_INT val;
6273 HOST_WIDE_INT splat_val;
6274 HOST_WIDE_INT msb_val;
6276 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
6277 return false;
6279 nunits = GET_MODE_NUNITS (mode);
6280 bitsize = GET_MODE_BITSIZE (inner);
6281 mask = GET_MODE_MASK (inner);
6283 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6284 splat_val = val;
6285 msb_val = val >= 0 ? 0 : -1;
6287 /* Construct the value to be splatted, if possible. If not, return 0. */
6288 for (i = 2; i <= copies; i *= 2)
6290 HOST_WIDE_INT small_val;
6291 bitsize /= 2;
6292 small_val = splat_val >> bitsize;
6293 mask >>= bitsize;
6294 if (splat_val != ((HOST_WIDE_INT)
6295 ((unsigned HOST_WIDE_INT) small_val << bitsize)
6296 | (small_val & mask)))
6297 return false;
6298 splat_val = small_val;
6301 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
6302 if (EASY_VECTOR_15 (splat_val))
6305 /* Also check if we can splat, and then add the result to itself. Do so if
6306 the value is positive, of if the splat instruction is using OP's mode;
6307 for splat_val < 0, the splat and the add should use the same mode. */
6308 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
6309 && (splat_val >= 0 || (step == 1 && copies == 1)))
6312 /* Also check if are loading up the most significant bit which can be done by
6313 loading up -1 and shifting the value left by -1. */
6314 else if (EASY_VECTOR_MSB (splat_val, inner))
6317 else
6318 return false;
6320 /* Check if VAL is present in every STEP-th element, and the
6321 other elements are filled with its most significant bit. */
6322 for (i = 1; i < nunits; ++i)
6324 HOST_WIDE_INT desired_val;
6325 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
6326 if ((i & (step - 1)) == 0)
6327 desired_val = val;
6328 else
6329 desired_val = msb_val;
6331 if (desired_val != const_vector_elt_as_int (op, elt))
6332 return false;
6335 return true;
6338 /* Like vsplitis_constant, but allow the value to be shifted left with a VSLDOI
6339 instruction, filling in the bottom elements with 0 or -1.
6341 Return 0 if the constant cannot be generated with VSLDOI. Return positive
6342 for the number of zeroes to shift in, or negative for the number of 0xff
6343 bytes to shift in.
6345 OP is a CONST_VECTOR. */
6348 vspltis_shifted (rtx op)
6350 machine_mode mode = GET_MODE (op);
6351 machine_mode inner = GET_MODE_INNER (mode);
6353 unsigned i, j;
6354 unsigned nunits;
6355 unsigned mask;
6357 HOST_WIDE_INT val;
6359 if (mode != V16QImode && mode != V8HImode && mode != V4SImode)
6360 return false;
6362 /* We need to create pseudo registers to do the shift, so don't recognize
6363 shift vector constants after reload. */
6364 if (!can_create_pseudo_p ())
6365 return false;
6367 nunits = GET_MODE_NUNITS (mode);
6368 mask = GET_MODE_MASK (inner);
6370 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? 0 : nunits - 1);
6372 /* Check if the value can really be the operand of a vspltis[bhw]. */
6373 if (EASY_VECTOR_15 (val))
6376 /* Also check if we are loading up the most significant bit which can be done
6377 by loading up -1 and shifting the value left by -1. */
6378 else if (EASY_VECTOR_MSB (val, inner))
6381 else
6382 return 0;
6384 /* Check if VAL is present in every STEP-th element until we find elements
6385 that are 0 or all 1 bits. */
6386 for (i = 1; i < nunits; ++i)
6388 unsigned elt = BYTES_BIG_ENDIAN ? i : nunits - 1 - i;
6389 HOST_WIDE_INT elt_val = const_vector_elt_as_int (op, elt);
6391 /* If the value isn't the splat value, check for the remaining elements
6392 being 0/-1. */
6393 if (val != elt_val)
6395 if (elt_val == 0)
6397 for (j = i+1; j < nunits; ++j)
6399 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6400 if (const_vector_elt_as_int (op, elt2) != 0)
6401 return 0;
6404 return (nunits - i) * GET_MODE_SIZE (inner);
6407 else if ((elt_val & mask) == mask)
6409 for (j = i+1; j < nunits; ++j)
6411 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6412 if ((const_vector_elt_as_int (op, elt2) & mask) != mask)
6413 return 0;
6416 return -((nunits - i) * GET_MODE_SIZE (inner));
6419 else
6420 return 0;
6424 /* If all elements are equal, we don't need to do VLSDOI. */
6425 return 0;
6429 /* Return true if OP is of the given MODE and can be synthesized
6430 with a vspltisb, vspltish or vspltisw. */
6432 bool
6433 easy_altivec_constant (rtx op, machine_mode mode)
6435 unsigned step, copies;
6437 if (mode == VOIDmode)
6438 mode = GET_MODE (op);
6439 else if (mode != GET_MODE (op))
6440 return false;
6442 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
6443 constants. */
6444 if (mode == V2DFmode)
6445 return zero_constant (op, mode);
6447 else if (mode == V2DImode)
6449 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
6450 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
6451 return false;
6453 if (zero_constant (op, mode))
6454 return true;
6456 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
6457 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
6458 return true;
6460 return false;
6463 /* V1TImode is a special container for TImode. Ignore for now. */
6464 else if (mode == V1TImode)
6465 return false;
6467 /* Start with a vspltisw. */
6468 step = GET_MODE_NUNITS (mode) / 4;
6469 copies = 1;
6471 if (vspltis_constant (op, step, copies))
6472 return true;
6474 /* Then try with a vspltish. */
6475 if (step == 1)
6476 copies <<= 1;
6477 else
6478 step >>= 1;
6480 if (vspltis_constant (op, step, copies))
6481 return true;
6483 /* And finally a vspltisb. */
6484 if (step == 1)
6485 copies <<= 1;
6486 else
6487 step >>= 1;
6489 if (vspltis_constant (op, step, copies))
6490 return true;
6492 if (vspltis_shifted (op) != 0)
6493 return true;
6495 return false;
6498 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
6499 result is OP. Abort if it is not possible. */
6502 gen_easy_altivec_constant (rtx op)
6504 machine_mode mode = GET_MODE (op);
6505 int nunits = GET_MODE_NUNITS (mode);
6506 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6507 unsigned step = nunits / 4;
6508 unsigned copies = 1;
6510 /* Start with a vspltisw. */
6511 if (vspltis_constant (op, step, copies))
6512 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
6514 /* Then try with a vspltish. */
6515 if (step == 1)
6516 copies <<= 1;
6517 else
6518 step >>= 1;
6520 if (vspltis_constant (op, step, copies))
6521 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
6523 /* And finally a vspltisb. */
6524 if (step == 1)
6525 copies <<= 1;
6526 else
6527 step >>= 1;
6529 if (vspltis_constant (op, step, copies))
6530 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
6532 gcc_unreachable ();
6535 /* Return true if OP is of the given MODE and can be synthesized with ISA 3.0
6536 instructions (xxspltib, vupkhsb/vextsb2w/vextb2d).
6538 Return the number of instructions needed (1 or 2) into the address pointed
6539 via NUM_INSNS_PTR.
6541 Return the constant that is being split via CONSTANT_PTR. */
6543 bool
6544 xxspltib_constant_p (rtx op,
6545 machine_mode mode,
6546 int *num_insns_ptr,
6547 int *constant_ptr)
6549 size_t nunits = GET_MODE_NUNITS (mode);
6550 size_t i;
6551 HOST_WIDE_INT value;
6552 rtx element;
6554 /* Set the returned values to out of bound values. */
6555 *num_insns_ptr = -1;
6556 *constant_ptr = 256;
6558 if (!TARGET_P9_VECTOR)
6559 return false;
6561 if (mode == VOIDmode)
6562 mode = GET_MODE (op);
6564 else if (mode != GET_MODE (op) && GET_MODE (op) != VOIDmode)
6565 return false;
6567 /* Handle (vec_duplicate <constant>). */
6568 if (GET_CODE (op) == VEC_DUPLICATE)
6570 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
6571 && mode != V2DImode)
6572 return false;
6574 element = XEXP (op, 0);
6575 if (!CONST_INT_P (element))
6576 return false;
6578 value = INTVAL (element);
6579 if (!IN_RANGE (value, -128, 127))
6580 return false;
6583 /* Handle (const_vector [...]). */
6584 else if (GET_CODE (op) == CONST_VECTOR)
6586 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
6587 && mode != V2DImode)
6588 return false;
6590 element = CONST_VECTOR_ELT (op, 0);
6591 if (!CONST_INT_P (element))
6592 return false;
6594 value = INTVAL (element);
6595 if (!IN_RANGE (value, -128, 127))
6596 return false;
6598 for (i = 1; i < nunits; i++)
6600 element = CONST_VECTOR_ELT (op, i);
6601 if (!CONST_INT_P (element))
6602 return false;
6604 if (value != INTVAL (element))
6605 return false;
6609 /* Handle integer constants being loaded into the upper part of the VSX
6610 register as a scalar. If the value isn't 0/-1, only allow it if the mode
6611 can go in Altivec registers. Prefer VSPLTISW/VUPKHSW over XXSPLITIB. */
6612 else if (CONST_INT_P (op))
6614 if (!SCALAR_INT_MODE_P (mode))
6615 return false;
6617 value = INTVAL (op);
6618 if (!IN_RANGE (value, -128, 127))
6619 return false;
6621 if (!IN_RANGE (value, -1, 0))
6623 if (!(reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID))
6624 return false;
6626 if (EASY_VECTOR_15 (value))
6627 return false;
6631 else
6632 return false;
6634 /* See if we could generate vspltisw/vspltish directly instead of xxspltib +
6635 sign extend. Special case 0/-1 to allow getting any VSX register instead
6636 of an Altivec register. */
6637 if ((mode == V4SImode || mode == V8HImode) && !IN_RANGE (value, -1, 0)
6638 && EASY_VECTOR_15 (value))
6639 return false;
6641 /* Return # of instructions and the constant byte for XXSPLTIB. */
6642 if (mode == V16QImode)
6643 *num_insns_ptr = 1;
6645 else if (IN_RANGE (value, -1, 0))
6646 *num_insns_ptr = 1;
6648 else
6649 *num_insns_ptr = 2;
6651 *constant_ptr = (int) value;
6652 return true;
6655 const char *
6656 output_vec_const_move (rtx *operands)
6658 int shift;
6659 machine_mode mode;
6660 rtx dest, vec;
6662 dest = operands[0];
6663 vec = operands[1];
6664 mode = GET_MODE (dest);
6666 if (TARGET_VSX)
6668 bool dest_vmx_p = ALTIVEC_REGNO_P (REGNO (dest));
6669 int xxspltib_value = 256;
6670 int num_insns = -1;
6672 if (zero_constant (vec, mode))
6674 if (TARGET_P9_VECTOR)
6675 return "xxspltib %x0,0";
6677 else if (dest_vmx_p)
6678 return "vspltisw %0,0";
6680 else
6681 return "xxlxor %x0,%x0,%x0";
6684 if (all_ones_constant (vec, mode))
6686 if (TARGET_P9_VECTOR)
6687 return "xxspltib %x0,255";
6689 else if (dest_vmx_p)
6690 return "vspltisw %0,-1";
6692 else if (TARGET_P8_VECTOR)
6693 return "xxlorc %x0,%x0,%x0";
6695 else
6696 gcc_unreachable ();
6699 if (TARGET_P9_VECTOR
6700 && xxspltib_constant_p (vec, mode, &num_insns, &xxspltib_value))
6702 if (num_insns == 1)
6704 operands[2] = GEN_INT (xxspltib_value & 0xff);
6705 return "xxspltib %x0,%2";
6708 return "#";
6712 if (TARGET_ALTIVEC)
6714 rtx splat_vec;
6716 gcc_assert (ALTIVEC_REGNO_P (REGNO (dest)));
6717 if (zero_constant (vec, mode))
6718 return "vspltisw %0,0";
6720 if (all_ones_constant (vec, mode))
6721 return "vspltisw %0,-1";
6723 /* Do we need to construct a value using VSLDOI? */
6724 shift = vspltis_shifted (vec);
6725 if (shift != 0)
6726 return "#";
6728 splat_vec = gen_easy_altivec_constant (vec);
6729 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
6730 operands[1] = XEXP (splat_vec, 0);
6731 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
6732 return "#";
6734 switch (GET_MODE (splat_vec))
6736 case E_V4SImode:
6737 return "vspltisw %0,%1";
6739 case E_V8HImode:
6740 return "vspltish %0,%1";
6742 case E_V16QImode:
6743 return "vspltisb %0,%1";
6745 default:
6746 gcc_unreachable ();
6750 gcc_unreachable ();
6753 /* Initialize TARGET of vector PAIRED to VALS. */
6755 void
6756 paired_expand_vector_init (rtx target, rtx vals)
6758 machine_mode mode = GET_MODE (target);
6759 int n_elts = GET_MODE_NUNITS (mode);
6760 int n_var = 0;
6761 rtx x, new_rtx, tmp, constant_op, op1, op2;
6762 int i;
6764 for (i = 0; i < n_elts; ++i)
6766 x = XVECEXP (vals, 0, i);
6767 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
6768 ++n_var;
6770 if (n_var == 0)
6772 /* Load from constant pool. */
6773 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
6774 return;
6777 if (n_var == 2)
6779 /* The vector is initialized only with non-constants. */
6780 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
6781 XVECEXP (vals, 0, 1));
6783 emit_move_insn (target, new_rtx);
6784 return;
6787 /* One field is non-constant and the other one is a constant. Load the
6788 constant from the constant pool and use ps_merge instruction to
6789 construct the whole vector. */
6790 op1 = XVECEXP (vals, 0, 0);
6791 op2 = XVECEXP (vals, 0, 1);
6793 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
6795 tmp = gen_reg_rtx (GET_MODE (constant_op));
6796 emit_move_insn (tmp, constant_op);
6798 if (CONSTANT_P (op1))
6799 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
6800 else
6801 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
6803 emit_move_insn (target, new_rtx);
6806 void
6807 paired_expand_vector_move (rtx operands[])
6809 rtx op0 = operands[0], op1 = operands[1];
6811 emit_move_insn (op0, op1);
6814 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
6815 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
6816 operands for the relation operation COND. This is a recursive
6817 function. */
6819 static void
6820 paired_emit_vector_compare (enum rtx_code rcode,
6821 rtx dest, rtx op0, rtx op1,
6822 rtx cc_op0, rtx cc_op1)
6824 rtx tmp = gen_reg_rtx (V2SFmode);
6825 rtx tmp1, max, min;
6827 gcc_assert (TARGET_PAIRED_FLOAT);
6828 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
6830 switch (rcode)
6832 case LT:
6833 case LTU:
6834 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
6835 return;
6836 case GE:
6837 case GEU:
6838 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
6839 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
6840 return;
6841 case LE:
6842 case LEU:
6843 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
6844 return;
6845 case GT:
6846 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
6847 return;
6848 case EQ:
6849 tmp1 = gen_reg_rtx (V2SFmode);
6850 max = gen_reg_rtx (V2SFmode);
6851 min = gen_reg_rtx (V2SFmode);
6852 gen_reg_rtx (V2SFmode);
6854 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
6855 emit_insn (gen_selv2sf4
6856 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
6857 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
6858 emit_insn (gen_selv2sf4
6859 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
6860 emit_insn (gen_subv2sf3 (tmp1, min, max));
6861 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
6862 return;
6863 case NE:
6864 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
6865 return;
6866 case UNLE:
6867 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
6868 return;
6869 case UNLT:
6870 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
6871 return;
6872 case UNGE:
6873 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
6874 return;
6875 case UNGT:
6876 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
6877 return;
6878 default:
6879 gcc_unreachable ();
6882 return;
6885 /* Emit vector conditional expression.
6886 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
6887 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
6890 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
6891 rtx cond, rtx cc_op0, rtx cc_op1)
6893 enum rtx_code rcode = GET_CODE (cond);
6895 if (!TARGET_PAIRED_FLOAT)
6896 return 0;
6898 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
6900 return 1;
6903 /* Initialize vector TARGET to VALS. */
6905 void
6906 rs6000_expand_vector_init (rtx target, rtx vals)
6908 machine_mode mode = GET_MODE (target);
6909 machine_mode inner_mode = GET_MODE_INNER (mode);
6910 int n_elts = GET_MODE_NUNITS (mode);
6911 int n_var = 0, one_var = -1;
6912 bool all_same = true, all_const_zero = true;
6913 rtx x, mem;
6914 int i;
6916 for (i = 0; i < n_elts; ++i)
6918 x = XVECEXP (vals, 0, i);
6919 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
6920 ++n_var, one_var = i;
6921 else if (x != CONST0_RTX (inner_mode))
6922 all_const_zero = false;
6924 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
6925 all_same = false;
6928 if (n_var == 0)
6930 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
6931 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
6932 if ((int_vector_p || TARGET_VSX) && all_const_zero)
6934 /* Zero register. */
6935 emit_move_insn (target, CONST0_RTX (mode));
6936 return;
6938 else if (int_vector_p && easy_vector_constant (const_vec, mode))
6940 /* Splat immediate. */
6941 emit_insn (gen_rtx_SET (target, const_vec));
6942 return;
6944 else
6946 /* Load from constant pool. */
6947 emit_move_insn (target, const_vec);
6948 return;
6952 /* Double word values on VSX can use xxpermdi or lxvdsx. */
6953 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
6955 rtx op[2];
6956 size_t i;
6957 size_t num_elements = all_same ? 1 : 2;
6958 for (i = 0; i < num_elements; i++)
6960 op[i] = XVECEXP (vals, 0, i);
6961 /* Just in case there is a SUBREG with a smaller mode, do a
6962 conversion. */
6963 if (GET_MODE (op[i]) != inner_mode)
6965 rtx tmp = gen_reg_rtx (inner_mode);
6966 convert_move (tmp, op[i], 0);
6967 op[i] = tmp;
6969 /* Allow load with splat double word. */
6970 else if (MEM_P (op[i]))
6972 if (!all_same)
6973 op[i] = force_reg (inner_mode, op[i]);
6975 else if (!REG_P (op[i]))
6976 op[i] = force_reg (inner_mode, op[i]);
6979 if (all_same)
6981 if (mode == V2DFmode)
6982 emit_insn (gen_vsx_splat_v2df (target, op[0]));
6983 else
6984 emit_insn (gen_vsx_splat_v2di (target, op[0]));
6986 else
6988 if (mode == V2DFmode)
6989 emit_insn (gen_vsx_concat_v2df (target, op[0], op[1]));
6990 else
6991 emit_insn (gen_vsx_concat_v2di (target, op[0], op[1]));
6993 return;
6996 /* Special case initializing vector int if we are on 64-bit systems with
6997 direct move or we have the ISA 3.0 instructions. */
6998 if (mode == V4SImode && VECTOR_MEM_VSX_P (V4SImode)
6999 && TARGET_DIRECT_MOVE_64BIT)
7001 if (all_same)
7003 rtx element0 = XVECEXP (vals, 0, 0);
7004 if (MEM_P (element0))
7005 element0 = rs6000_address_for_fpconvert (element0);
7006 else
7007 element0 = force_reg (SImode, element0);
7009 if (TARGET_P9_VECTOR)
7010 emit_insn (gen_vsx_splat_v4si (target, element0));
7011 else
7013 rtx tmp = gen_reg_rtx (DImode);
7014 emit_insn (gen_zero_extendsidi2 (tmp, element0));
7015 emit_insn (gen_vsx_splat_v4si_di (target, tmp));
7017 return;
7019 else
7021 rtx elements[4];
7022 size_t i;
7024 for (i = 0; i < 4; i++)
7026 elements[i] = XVECEXP (vals, 0, i);
7027 if (!CONST_INT_P (elements[i]) && !REG_P (elements[i]))
7028 elements[i] = copy_to_mode_reg (SImode, elements[i]);
7031 emit_insn (gen_vsx_init_v4si (target, elements[0], elements[1],
7032 elements[2], elements[3]));
7033 return;
7037 /* With single precision floating point on VSX, know that internally single
7038 precision is actually represented as a double, and either make 2 V2DF
7039 vectors, and convert these vectors to single precision, or do one
7040 conversion, and splat the result to the other elements. */
7041 if (mode == V4SFmode && VECTOR_MEM_VSX_P (V4SFmode))
7043 if (all_same)
7045 rtx element0 = XVECEXP (vals, 0, 0);
7047 if (TARGET_P9_VECTOR)
7049 if (MEM_P (element0))
7050 element0 = rs6000_address_for_fpconvert (element0);
7052 emit_insn (gen_vsx_splat_v4sf (target, element0));
7055 else
7057 rtx freg = gen_reg_rtx (V4SFmode);
7058 rtx sreg = force_reg (SFmode, element0);
7059 rtx cvt = (TARGET_XSCVDPSPN
7060 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
7061 : gen_vsx_xscvdpsp_scalar (freg, sreg));
7063 emit_insn (cvt);
7064 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg,
7065 const0_rtx));
7068 else
7070 rtx dbl_even = gen_reg_rtx (V2DFmode);
7071 rtx dbl_odd = gen_reg_rtx (V2DFmode);
7072 rtx flt_even = gen_reg_rtx (V4SFmode);
7073 rtx flt_odd = gen_reg_rtx (V4SFmode);
7074 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
7075 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
7076 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
7077 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
7079 /* Use VMRGEW if we can instead of doing a permute. */
7080 if (TARGET_P8_VECTOR)
7082 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op2));
7083 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op1, op3));
7084 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
7085 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
7086 if (BYTES_BIG_ENDIAN)
7087 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_even, flt_odd));
7088 else
7089 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_odd, flt_even));
7091 else
7093 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
7094 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
7095 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
7096 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
7097 rs6000_expand_extract_even (target, flt_even, flt_odd);
7100 return;
7103 /* Special case initializing vector short/char that are splats if we are on
7104 64-bit systems with direct move. */
7105 if (all_same && TARGET_DIRECT_MOVE_64BIT
7106 && (mode == V16QImode || mode == V8HImode))
7108 rtx op0 = XVECEXP (vals, 0, 0);
7109 rtx di_tmp = gen_reg_rtx (DImode);
7111 if (!REG_P (op0))
7112 op0 = force_reg (GET_MODE_INNER (mode), op0);
7114 if (mode == V16QImode)
7116 emit_insn (gen_zero_extendqidi2 (di_tmp, op0));
7117 emit_insn (gen_vsx_vspltb_di (target, di_tmp));
7118 return;
7121 if (mode == V8HImode)
7123 emit_insn (gen_zero_extendhidi2 (di_tmp, op0));
7124 emit_insn (gen_vsx_vsplth_di (target, di_tmp));
7125 return;
7129 /* Store value to stack temp. Load vector element. Splat. However, splat
7130 of 64-bit items is not supported on Altivec. */
7131 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
7133 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
7134 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
7135 XVECEXP (vals, 0, 0));
7136 x = gen_rtx_UNSPEC (VOIDmode,
7137 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
7138 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7139 gen_rtvec (2,
7140 gen_rtx_SET (target, mem),
7141 x)));
7142 x = gen_rtx_VEC_SELECT (inner_mode, target,
7143 gen_rtx_PARALLEL (VOIDmode,
7144 gen_rtvec (1, const0_rtx)));
7145 emit_insn (gen_rtx_SET (target, gen_rtx_VEC_DUPLICATE (mode, x)));
7146 return;
7149 /* One field is non-constant. Load constant then overwrite
7150 varying field. */
7151 if (n_var == 1)
7153 rtx copy = copy_rtx (vals);
7155 /* Load constant part of vector, substitute neighboring value for
7156 varying element. */
7157 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
7158 rs6000_expand_vector_init (target, copy);
7160 /* Insert variable. */
7161 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
7162 return;
7165 /* Construct the vector in memory one field at a time
7166 and load the whole vector. */
7167 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
7168 for (i = 0; i < n_elts; i++)
7169 emit_move_insn (adjust_address_nv (mem, inner_mode,
7170 i * GET_MODE_SIZE (inner_mode)),
7171 XVECEXP (vals, 0, i));
7172 emit_move_insn (target, mem);
7175 /* Set field ELT of TARGET to VAL. */
7177 void
7178 rs6000_expand_vector_set (rtx target, rtx val, int elt)
7180 machine_mode mode = GET_MODE (target);
7181 machine_mode inner_mode = GET_MODE_INNER (mode);
7182 rtx reg = gen_reg_rtx (mode);
7183 rtx mask, mem, x;
7184 int width = GET_MODE_SIZE (inner_mode);
7185 int i;
7187 val = force_reg (GET_MODE (val), val);
7189 if (VECTOR_MEM_VSX_P (mode))
7191 rtx insn = NULL_RTX;
7192 rtx elt_rtx = GEN_INT (elt);
7194 if (mode == V2DFmode)
7195 insn = gen_vsx_set_v2df (target, target, val, elt_rtx);
7197 else if (mode == V2DImode)
7198 insn = gen_vsx_set_v2di (target, target, val, elt_rtx);
7200 else if (TARGET_P9_VECTOR && TARGET_POWERPC64)
7202 if (mode == V4SImode)
7203 insn = gen_vsx_set_v4si_p9 (target, target, val, elt_rtx);
7204 else if (mode == V8HImode)
7205 insn = gen_vsx_set_v8hi_p9 (target, target, val, elt_rtx);
7206 else if (mode == V16QImode)
7207 insn = gen_vsx_set_v16qi_p9 (target, target, val, elt_rtx);
7208 else if (mode == V4SFmode)
7209 insn = gen_vsx_set_v4sf_p9 (target, target, val, elt_rtx);
7212 if (insn)
7214 emit_insn (insn);
7215 return;
7219 /* Simplify setting single element vectors like V1TImode. */
7220 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
7222 emit_move_insn (target, gen_lowpart (mode, val));
7223 return;
7226 /* Load single variable value. */
7227 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
7228 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
7229 x = gen_rtx_UNSPEC (VOIDmode,
7230 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
7231 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7232 gen_rtvec (2,
7233 gen_rtx_SET (reg, mem),
7234 x)));
7236 /* Linear sequence. */
7237 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
7238 for (i = 0; i < 16; ++i)
7239 XVECEXP (mask, 0, i) = GEN_INT (i);
7241 /* Set permute mask to insert element into target. */
7242 for (i = 0; i < width; ++i)
7243 XVECEXP (mask, 0, elt*width + i)
7244 = GEN_INT (i + 0x10);
7245 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
7247 if (BYTES_BIG_ENDIAN)
7248 x = gen_rtx_UNSPEC (mode,
7249 gen_rtvec (3, target, reg,
7250 force_reg (V16QImode, x)),
7251 UNSPEC_VPERM);
7252 else
7254 if (TARGET_P9_VECTOR)
7255 x = gen_rtx_UNSPEC (mode,
7256 gen_rtvec (3, target, reg,
7257 force_reg (V16QImode, x)),
7258 UNSPEC_VPERMR);
7259 else
7261 /* Invert selector. We prefer to generate VNAND on P8 so
7262 that future fusion opportunities can kick in, but must
7263 generate VNOR elsewhere. */
7264 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
7265 rtx iorx = (TARGET_P8_VECTOR
7266 ? gen_rtx_IOR (V16QImode, notx, notx)
7267 : gen_rtx_AND (V16QImode, notx, notx));
7268 rtx tmp = gen_reg_rtx (V16QImode);
7269 emit_insn (gen_rtx_SET (tmp, iorx));
7271 /* Permute with operands reversed and adjusted selector. */
7272 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
7273 UNSPEC_VPERM);
7277 emit_insn (gen_rtx_SET (target, x));
7280 /* Extract field ELT from VEC into TARGET. */
7282 void
7283 rs6000_expand_vector_extract (rtx target, rtx vec, rtx elt)
7285 machine_mode mode = GET_MODE (vec);
7286 machine_mode inner_mode = GET_MODE_INNER (mode);
7287 rtx mem;
7289 if (VECTOR_MEM_VSX_P (mode) && CONST_INT_P (elt))
7291 switch (mode)
7293 default:
7294 break;
7295 case E_V1TImode:
7296 gcc_assert (INTVAL (elt) == 0 && inner_mode == TImode);
7297 emit_move_insn (target, gen_lowpart (TImode, vec));
7298 break;
7299 case E_V2DFmode:
7300 emit_insn (gen_vsx_extract_v2df (target, vec, elt));
7301 return;
7302 case E_V2DImode:
7303 emit_insn (gen_vsx_extract_v2di (target, vec, elt));
7304 return;
7305 case E_V4SFmode:
7306 emit_insn (gen_vsx_extract_v4sf (target, vec, elt));
7307 return;
7308 case E_V16QImode:
7309 if (TARGET_DIRECT_MOVE_64BIT)
7311 emit_insn (gen_vsx_extract_v16qi (target, vec, elt));
7312 return;
7314 else
7315 break;
7316 case E_V8HImode:
7317 if (TARGET_DIRECT_MOVE_64BIT)
7319 emit_insn (gen_vsx_extract_v8hi (target, vec, elt));
7320 return;
7322 else
7323 break;
7324 case E_V4SImode:
7325 if (TARGET_DIRECT_MOVE_64BIT)
7327 emit_insn (gen_vsx_extract_v4si (target, vec, elt));
7328 return;
7330 break;
7333 else if (VECTOR_MEM_VSX_P (mode) && !CONST_INT_P (elt)
7334 && TARGET_DIRECT_MOVE_64BIT)
7336 if (GET_MODE (elt) != DImode)
7338 rtx tmp = gen_reg_rtx (DImode);
7339 convert_move (tmp, elt, 0);
7340 elt = tmp;
7342 else if (!REG_P (elt))
7343 elt = force_reg (DImode, elt);
7345 switch (mode)
7347 case E_V2DFmode:
7348 emit_insn (gen_vsx_extract_v2df_var (target, vec, elt));
7349 return;
7351 case E_V2DImode:
7352 emit_insn (gen_vsx_extract_v2di_var (target, vec, elt));
7353 return;
7355 case E_V4SFmode:
7356 emit_insn (gen_vsx_extract_v4sf_var (target, vec, elt));
7357 return;
7359 case E_V4SImode:
7360 emit_insn (gen_vsx_extract_v4si_var (target, vec, elt));
7361 return;
7363 case E_V8HImode:
7364 emit_insn (gen_vsx_extract_v8hi_var (target, vec, elt));
7365 return;
7367 case E_V16QImode:
7368 emit_insn (gen_vsx_extract_v16qi_var (target, vec, elt));
7369 return;
7371 default:
7372 gcc_unreachable ();
7376 gcc_assert (CONST_INT_P (elt));
7378 /* Allocate mode-sized buffer. */
7379 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
7381 emit_move_insn (mem, vec);
7383 /* Add offset to field within buffer matching vector element. */
7384 mem = adjust_address_nv (mem, inner_mode,
7385 INTVAL (elt) * GET_MODE_SIZE (inner_mode));
7387 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
7390 /* Helper function to return the register number of a RTX. */
7391 static inline int
7392 regno_or_subregno (rtx op)
7394 if (REG_P (op))
7395 return REGNO (op);
7396 else if (SUBREG_P (op))
7397 return subreg_regno (op);
7398 else
7399 gcc_unreachable ();
7402 /* Adjust a memory address (MEM) of a vector type to point to a scalar field
7403 within the vector (ELEMENT) with a mode (SCALAR_MODE). Use a base register
7404 temporary (BASE_TMP) to fixup the address. Return the new memory address
7405 that is valid for reads or writes to a given register (SCALAR_REG). */
7408 rs6000_adjust_vec_address (rtx scalar_reg,
7409 rtx mem,
7410 rtx element,
7411 rtx base_tmp,
7412 machine_mode scalar_mode)
7414 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
7415 rtx addr = XEXP (mem, 0);
7416 rtx element_offset;
7417 rtx new_addr;
7418 bool valid_addr_p;
7420 /* Vector addresses should not have PRE_INC, PRE_DEC, or PRE_MODIFY. */
7421 gcc_assert (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC);
7423 /* Calculate what we need to add to the address to get the element
7424 address. */
7425 if (CONST_INT_P (element))
7426 element_offset = GEN_INT (INTVAL (element) * scalar_size);
7427 else
7429 int byte_shift = exact_log2 (scalar_size);
7430 gcc_assert (byte_shift >= 0);
7432 if (byte_shift == 0)
7433 element_offset = element;
7435 else
7437 if (TARGET_POWERPC64)
7438 emit_insn (gen_ashldi3 (base_tmp, element, GEN_INT (byte_shift)));
7439 else
7440 emit_insn (gen_ashlsi3 (base_tmp, element, GEN_INT (byte_shift)));
7442 element_offset = base_tmp;
7446 /* Create the new address pointing to the element within the vector. If we
7447 are adding 0, we don't have to change the address. */
7448 if (element_offset == const0_rtx)
7449 new_addr = addr;
7451 /* A simple indirect address can be converted into a reg + offset
7452 address. */
7453 else if (REG_P (addr) || SUBREG_P (addr))
7454 new_addr = gen_rtx_PLUS (Pmode, addr, element_offset);
7456 /* Optimize D-FORM addresses with constant offset with a constant element, to
7457 include the element offset in the address directly. */
7458 else if (GET_CODE (addr) == PLUS)
7460 rtx op0 = XEXP (addr, 0);
7461 rtx op1 = XEXP (addr, 1);
7462 rtx insn;
7464 gcc_assert (REG_P (op0) || SUBREG_P (op0));
7465 if (CONST_INT_P (op1) && CONST_INT_P (element_offset))
7467 HOST_WIDE_INT offset = INTVAL (op1) + INTVAL (element_offset);
7468 rtx offset_rtx = GEN_INT (offset);
7470 if (IN_RANGE (offset, -32768, 32767)
7471 && (scalar_size < 8 || (offset & 0x3) == 0))
7472 new_addr = gen_rtx_PLUS (Pmode, op0, offset_rtx);
7473 else
7475 emit_move_insn (base_tmp, offset_rtx);
7476 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7479 else
7481 bool op1_reg_p = (REG_P (op1) || SUBREG_P (op1));
7482 bool ele_reg_p = (REG_P (element_offset) || SUBREG_P (element_offset));
7484 /* Note, ADDI requires the register being added to be a base
7485 register. If the register was R0, load it up into the temporary
7486 and do the add. */
7487 if (op1_reg_p
7488 && (ele_reg_p || reg_or_subregno (op1) != FIRST_GPR_REGNO))
7490 insn = gen_add3_insn (base_tmp, op1, element_offset);
7491 gcc_assert (insn != NULL_RTX);
7492 emit_insn (insn);
7495 else if (ele_reg_p
7496 && reg_or_subregno (element_offset) != FIRST_GPR_REGNO)
7498 insn = gen_add3_insn (base_tmp, element_offset, op1);
7499 gcc_assert (insn != NULL_RTX);
7500 emit_insn (insn);
7503 else
7505 emit_move_insn (base_tmp, op1);
7506 emit_insn (gen_add2_insn (base_tmp, element_offset));
7509 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7513 else
7515 emit_move_insn (base_tmp, addr);
7516 new_addr = gen_rtx_PLUS (Pmode, base_tmp, element_offset);
7519 /* If we have a PLUS, we need to see whether the particular register class
7520 allows for D-FORM or X-FORM addressing. */
7521 if (GET_CODE (new_addr) == PLUS)
7523 rtx op1 = XEXP (new_addr, 1);
7524 addr_mask_type addr_mask;
7525 int scalar_regno = regno_or_subregno (scalar_reg);
7527 gcc_assert (scalar_regno < FIRST_PSEUDO_REGISTER);
7528 if (INT_REGNO_P (scalar_regno))
7529 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_GPR];
7531 else if (FP_REGNO_P (scalar_regno))
7532 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_FPR];
7534 else if (ALTIVEC_REGNO_P (scalar_regno))
7535 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_VMX];
7537 else
7538 gcc_unreachable ();
7540 if (REG_P (op1) || SUBREG_P (op1))
7541 valid_addr_p = (addr_mask & RELOAD_REG_INDEXED) != 0;
7542 else
7543 valid_addr_p = (addr_mask & RELOAD_REG_OFFSET) != 0;
7546 else if (REG_P (new_addr) || SUBREG_P (new_addr))
7547 valid_addr_p = true;
7549 else
7550 valid_addr_p = false;
7552 if (!valid_addr_p)
7554 emit_move_insn (base_tmp, new_addr);
7555 new_addr = base_tmp;
7558 return change_address (mem, scalar_mode, new_addr);
7561 /* Split a variable vec_extract operation into the component instructions. */
7563 void
7564 rs6000_split_vec_extract_var (rtx dest, rtx src, rtx element, rtx tmp_gpr,
7565 rtx tmp_altivec)
7567 machine_mode mode = GET_MODE (src);
7568 machine_mode scalar_mode = GET_MODE (dest);
7569 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
7570 int byte_shift = exact_log2 (scalar_size);
7572 gcc_assert (byte_shift >= 0);
7574 /* If we are given a memory address, optimize to load just the element. We
7575 don't have to adjust the vector element number on little endian
7576 systems. */
7577 if (MEM_P (src))
7579 gcc_assert (REG_P (tmp_gpr));
7580 emit_move_insn (dest, rs6000_adjust_vec_address (dest, src, element,
7581 tmp_gpr, scalar_mode));
7582 return;
7585 else if (REG_P (src) || SUBREG_P (src))
7587 int bit_shift = byte_shift + 3;
7588 rtx element2;
7589 int dest_regno = regno_or_subregno (dest);
7590 int src_regno = regno_or_subregno (src);
7591 int element_regno = regno_or_subregno (element);
7593 gcc_assert (REG_P (tmp_gpr));
7595 /* See if we want to generate VEXTU{B,H,W}{L,R}X if the destination is in
7596 a general purpose register. */
7597 if (TARGET_P9_VECTOR
7598 && (mode == V16QImode || mode == V8HImode || mode == V4SImode)
7599 && INT_REGNO_P (dest_regno)
7600 && ALTIVEC_REGNO_P (src_regno)
7601 && INT_REGNO_P (element_regno))
7603 rtx dest_si = gen_rtx_REG (SImode, dest_regno);
7604 rtx element_si = gen_rtx_REG (SImode, element_regno);
7606 if (mode == V16QImode)
7607 emit_insn (VECTOR_ELT_ORDER_BIG
7608 ? gen_vextublx (dest_si, element_si, src)
7609 : gen_vextubrx (dest_si, element_si, src));
7611 else if (mode == V8HImode)
7613 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
7614 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const1_rtx));
7615 emit_insn (VECTOR_ELT_ORDER_BIG
7616 ? gen_vextuhlx (dest_si, tmp_gpr_si, src)
7617 : gen_vextuhrx (dest_si, tmp_gpr_si, src));
7621 else
7623 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
7624 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const2_rtx));
7625 emit_insn (VECTOR_ELT_ORDER_BIG
7626 ? gen_vextuwlx (dest_si, tmp_gpr_si, src)
7627 : gen_vextuwrx (dest_si, tmp_gpr_si, src));
7630 return;
7634 gcc_assert (REG_P (tmp_altivec));
7636 /* For little endian, adjust element ordering. For V2DI/V2DF, we can use
7637 an XOR, otherwise we need to subtract. The shift amount is so VSLO
7638 will shift the element into the upper position (adding 3 to convert a
7639 byte shift into a bit shift). */
7640 if (scalar_size == 8)
7642 if (!VECTOR_ELT_ORDER_BIG)
7644 emit_insn (gen_xordi3 (tmp_gpr, element, const1_rtx));
7645 element2 = tmp_gpr;
7647 else
7648 element2 = element;
7650 /* Generate RLDIC directly to shift left 6 bits and retrieve 1
7651 bit. */
7652 emit_insn (gen_rtx_SET (tmp_gpr,
7653 gen_rtx_AND (DImode,
7654 gen_rtx_ASHIFT (DImode,
7655 element2,
7656 GEN_INT (6)),
7657 GEN_INT (64))));
7659 else
7661 if (!VECTOR_ELT_ORDER_BIG)
7663 rtx num_ele_m1 = GEN_INT (GET_MODE_NUNITS (mode) - 1);
7665 emit_insn (gen_anddi3 (tmp_gpr, element, num_ele_m1));
7666 emit_insn (gen_subdi3 (tmp_gpr, num_ele_m1, tmp_gpr));
7667 element2 = tmp_gpr;
7669 else
7670 element2 = element;
7672 emit_insn (gen_ashldi3 (tmp_gpr, element2, GEN_INT (bit_shift)));
7675 /* Get the value into the lower byte of the Altivec register where VSLO
7676 expects it. */
7677 if (TARGET_P9_VECTOR)
7678 emit_insn (gen_vsx_splat_v2di (tmp_altivec, tmp_gpr));
7679 else if (can_create_pseudo_p ())
7680 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_gpr, tmp_gpr));
7681 else
7683 rtx tmp_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
7684 emit_move_insn (tmp_di, tmp_gpr);
7685 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_di, tmp_di));
7688 /* Do the VSLO to get the value into the final location. */
7689 switch (mode)
7691 case E_V2DFmode:
7692 emit_insn (gen_vsx_vslo_v2df (dest, src, tmp_altivec));
7693 return;
7695 case E_V2DImode:
7696 emit_insn (gen_vsx_vslo_v2di (dest, src, tmp_altivec));
7697 return;
7699 case E_V4SFmode:
7701 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
7702 rtx tmp_altivec_v4sf = gen_rtx_REG (V4SFmode, REGNO (tmp_altivec));
7703 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
7704 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
7705 tmp_altivec));
7707 emit_insn (gen_vsx_xscvspdp_scalar2 (dest, tmp_altivec_v4sf));
7708 return;
7711 case E_V4SImode:
7712 case E_V8HImode:
7713 case E_V16QImode:
7715 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
7716 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
7717 rtx tmp_gpr_di = gen_rtx_REG (DImode, REGNO (dest));
7718 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
7719 tmp_altivec));
7720 emit_move_insn (tmp_gpr_di, tmp_altivec_di);
7721 emit_insn (gen_ashrdi3 (tmp_gpr_di, tmp_gpr_di,
7722 GEN_INT (64 - (8 * scalar_size))));
7723 return;
7726 default:
7727 gcc_unreachable ();
7730 return;
7732 else
7733 gcc_unreachable ();
7736 /* Helper function for rs6000_split_v4si_init to build up a DImode value from
7737 two SImode values. */
7739 static void
7740 rs6000_split_v4si_init_di_reg (rtx dest, rtx si1, rtx si2, rtx tmp)
7742 const unsigned HOST_WIDE_INT mask_32bit = HOST_WIDE_INT_C (0xffffffff);
7744 if (CONST_INT_P (si1) && CONST_INT_P (si2))
7746 unsigned HOST_WIDE_INT const1 = (UINTVAL (si1) & mask_32bit) << 32;
7747 unsigned HOST_WIDE_INT const2 = UINTVAL (si2) & mask_32bit;
7749 emit_move_insn (dest, GEN_INT (const1 | const2));
7750 return;
7753 /* Put si1 into upper 32-bits of dest. */
7754 if (CONST_INT_P (si1))
7755 emit_move_insn (dest, GEN_INT ((UINTVAL (si1) & mask_32bit) << 32));
7756 else
7758 /* Generate RLDIC. */
7759 rtx si1_di = gen_rtx_REG (DImode, regno_or_subregno (si1));
7760 rtx shift_rtx = gen_rtx_ASHIFT (DImode, si1_di, GEN_INT (32));
7761 rtx mask_rtx = GEN_INT (mask_32bit << 32);
7762 rtx and_rtx = gen_rtx_AND (DImode, shift_rtx, mask_rtx);
7763 gcc_assert (!reg_overlap_mentioned_p (dest, si1));
7764 emit_insn (gen_rtx_SET (dest, and_rtx));
7767 /* Put si2 into the temporary. */
7768 gcc_assert (!reg_overlap_mentioned_p (dest, tmp));
7769 if (CONST_INT_P (si2))
7770 emit_move_insn (tmp, GEN_INT (UINTVAL (si2) & mask_32bit));
7771 else
7772 emit_insn (gen_zero_extendsidi2 (tmp, si2));
7774 /* Combine the two parts. */
7775 emit_insn (gen_iordi3 (dest, dest, tmp));
7776 return;
7779 /* Split a V4SI initialization. */
7781 void
7782 rs6000_split_v4si_init (rtx operands[])
7784 rtx dest = operands[0];
7786 /* Destination is a GPR, build up the two DImode parts in place. */
7787 if (REG_P (dest) || SUBREG_P (dest))
7789 int d_regno = regno_or_subregno (dest);
7790 rtx scalar1 = operands[1];
7791 rtx scalar2 = operands[2];
7792 rtx scalar3 = operands[3];
7793 rtx scalar4 = operands[4];
7794 rtx tmp1 = operands[5];
7795 rtx tmp2 = operands[6];
7797 /* Even though we only need one temporary (plus the destination, which
7798 has an early clobber constraint, try to use two temporaries, one for
7799 each double word created. That way the 2nd insn scheduling pass can
7800 rearrange things so the two parts are done in parallel. */
7801 if (BYTES_BIG_ENDIAN)
7803 rtx di_lo = gen_rtx_REG (DImode, d_regno);
7804 rtx di_hi = gen_rtx_REG (DImode, d_regno + 1);
7805 rs6000_split_v4si_init_di_reg (di_lo, scalar1, scalar2, tmp1);
7806 rs6000_split_v4si_init_di_reg (di_hi, scalar3, scalar4, tmp2);
7808 else
7810 rtx di_lo = gen_rtx_REG (DImode, d_regno + 1);
7811 rtx di_hi = gen_rtx_REG (DImode, d_regno);
7812 gcc_assert (!VECTOR_ELT_ORDER_BIG);
7813 rs6000_split_v4si_init_di_reg (di_lo, scalar4, scalar3, tmp1);
7814 rs6000_split_v4si_init_di_reg (di_hi, scalar2, scalar1, tmp2);
7816 return;
7819 else
7820 gcc_unreachable ();
7823 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
7824 selects whether the alignment is abi mandated, optional, or
7825 both abi and optional alignment. */
7827 unsigned int
7828 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
7830 if (how != align_opt)
7832 if (TREE_CODE (type) == VECTOR_TYPE)
7834 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type)))
7836 if (align < 64)
7837 align = 64;
7839 else if (align < 128)
7840 align = 128;
7844 if (how != align_abi)
7846 if (TREE_CODE (type) == ARRAY_TYPE
7847 && TYPE_MODE (TREE_TYPE (type)) == QImode)
7849 if (align < BITS_PER_WORD)
7850 align = BITS_PER_WORD;
7854 return align;
7857 /* Previous GCC releases forced all vector types to have 16-byte alignment. */
7859 bool
7860 rs6000_special_adjust_field_align_p (tree type, unsigned int computed)
7862 if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
7864 if (computed != 128)
7866 static bool warned;
7867 if (!warned && warn_psabi)
7869 warned = true;
7870 inform (input_location,
7871 "the layout of aggregates containing vectors with"
7872 " %d-byte alignment has changed in GCC 5",
7873 computed / BITS_PER_UNIT);
7876 /* In current GCC there is no special case. */
7877 return false;
7880 return false;
7883 /* AIX increases natural record alignment to doubleword if the first
7884 field is an FP double while the FP fields remain word aligned. */
7886 unsigned int
7887 rs6000_special_round_type_align (tree type, unsigned int computed,
7888 unsigned int specified)
7890 unsigned int align = MAX (computed, specified);
7891 tree field = TYPE_FIELDS (type);
7893 /* Skip all non field decls */
7894 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
7895 field = DECL_CHAIN (field);
7897 if (field != NULL && field != type)
7899 type = TREE_TYPE (field);
7900 while (TREE_CODE (type) == ARRAY_TYPE)
7901 type = TREE_TYPE (type);
7903 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
7904 align = MAX (align, 64);
7907 return align;
7910 /* Darwin increases record alignment to the natural alignment of
7911 the first field. */
7913 unsigned int
7914 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
7915 unsigned int specified)
7917 unsigned int align = MAX (computed, specified);
7919 if (TYPE_PACKED (type))
7920 return align;
7922 /* Find the first field, looking down into aggregates. */
7923 do {
7924 tree field = TYPE_FIELDS (type);
7925 /* Skip all non field decls */
7926 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
7927 field = DECL_CHAIN (field);
7928 if (! field)
7929 break;
7930 /* A packed field does not contribute any extra alignment. */
7931 if (DECL_PACKED (field))
7932 return align;
7933 type = TREE_TYPE (field);
7934 while (TREE_CODE (type) == ARRAY_TYPE)
7935 type = TREE_TYPE (type);
7936 } while (AGGREGATE_TYPE_P (type));
7938 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
7939 align = MAX (align, TYPE_ALIGN (type));
7941 return align;
7944 /* Return 1 for an operand in small memory on V.4/eabi. */
7947 small_data_operand (rtx op ATTRIBUTE_UNUSED,
7948 machine_mode mode ATTRIBUTE_UNUSED)
7950 #if TARGET_ELF
7951 rtx sym_ref;
7953 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
7954 return 0;
7956 if (DEFAULT_ABI != ABI_V4)
7957 return 0;
7959 if (GET_CODE (op) == SYMBOL_REF)
7960 sym_ref = op;
7962 else if (GET_CODE (op) != CONST
7963 || GET_CODE (XEXP (op, 0)) != PLUS
7964 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
7965 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
7966 return 0;
7968 else
7970 rtx sum = XEXP (op, 0);
7971 HOST_WIDE_INT summand;
7973 /* We have to be careful here, because it is the referenced address
7974 that must be 32k from _SDA_BASE_, not just the symbol. */
7975 summand = INTVAL (XEXP (sum, 1));
7976 if (summand < 0 || summand > g_switch_value)
7977 return 0;
7979 sym_ref = XEXP (sum, 0);
7982 return SYMBOL_REF_SMALL_P (sym_ref);
7983 #else
7984 return 0;
7985 #endif
7988 /* Return true if either operand is a general purpose register. */
7990 bool
7991 gpr_or_gpr_p (rtx op0, rtx op1)
7993 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
7994 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
7997 /* Return true if this is a move direct operation between GPR registers and
7998 floating point/VSX registers. */
8000 bool
8001 direct_move_p (rtx op0, rtx op1)
8003 int regno0, regno1;
8005 if (!REG_P (op0) || !REG_P (op1))
8006 return false;
8008 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
8009 return false;
8011 regno0 = REGNO (op0);
8012 regno1 = REGNO (op1);
8013 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
8014 return false;
8016 if (INT_REGNO_P (regno0))
8017 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
8019 else if (INT_REGNO_P (regno1))
8021 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
8022 return true;
8024 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
8025 return true;
8028 return false;
8031 /* Return true if the OFFSET is valid for the quad address instructions that
8032 use d-form (register + offset) addressing. */
8034 static inline bool
8035 quad_address_offset_p (HOST_WIDE_INT offset)
8037 return (IN_RANGE (offset, -32768, 32767) && ((offset) & 0xf) == 0);
8040 /* Return true if the ADDR is an acceptable address for a quad memory
8041 operation of mode MODE (either LQ/STQ for general purpose registers, or
8042 LXV/STXV for vector registers under ISA 3.0. GPR_P is true if this address
8043 is intended for LQ/STQ. If it is false, the address is intended for the ISA
8044 3.0 LXV/STXV instruction. */
8046 bool
8047 quad_address_p (rtx addr, machine_mode mode, bool strict)
8049 rtx op0, op1;
8051 if (GET_MODE_SIZE (mode) != 16)
8052 return false;
8054 if (legitimate_indirect_address_p (addr, strict))
8055 return true;
8057 if (VECTOR_MODE_P (mode) && !mode_supports_vsx_dform_quad (mode))
8058 return false;
8060 if (GET_CODE (addr) != PLUS)
8061 return false;
8063 op0 = XEXP (addr, 0);
8064 if (!REG_P (op0) || !INT_REG_OK_FOR_BASE_P (op0, strict))
8065 return false;
8067 op1 = XEXP (addr, 1);
8068 if (!CONST_INT_P (op1))
8069 return false;
8071 return quad_address_offset_p (INTVAL (op1));
8074 /* Return true if this is a load or store quad operation. This function does
8075 not handle the atomic quad memory instructions. */
8077 bool
8078 quad_load_store_p (rtx op0, rtx op1)
8080 bool ret;
8082 if (!TARGET_QUAD_MEMORY)
8083 ret = false;
8085 else if (REG_P (op0) && MEM_P (op1))
8086 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
8087 && quad_memory_operand (op1, GET_MODE (op1))
8088 && !reg_overlap_mentioned_p (op0, op1));
8090 else if (MEM_P (op0) && REG_P (op1))
8091 ret = (quad_memory_operand (op0, GET_MODE (op0))
8092 && quad_int_reg_operand (op1, GET_MODE (op1)));
8094 else
8095 ret = false;
8097 if (TARGET_DEBUG_ADDR)
8099 fprintf (stderr, "\n========== quad_load_store, return %s\n",
8100 ret ? "true" : "false");
8101 debug_rtx (gen_rtx_SET (op0, op1));
8104 return ret;
8107 /* Given an address, return a constant offset term if one exists. */
8109 static rtx
8110 address_offset (rtx op)
8112 if (GET_CODE (op) == PRE_INC
8113 || GET_CODE (op) == PRE_DEC)
8114 op = XEXP (op, 0);
8115 else if (GET_CODE (op) == PRE_MODIFY
8116 || GET_CODE (op) == LO_SUM)
8117 op = XEXP (op, 1);
8119 if (GET_CODE (op) == CONST)
8120 op = XEXP (op, 0);
8122 if (GET_CODE (op) == PLUS)
8123 op = XEXP (op, 1);
8125 if (CONST_INT_P (op))
8126 return op;
8128 return NULL_RTX;
8131 /* Return true if the MEM operand is a memory operand suitable for use
8132 with a (full width, possibly multiple) gpr load/store. On
8133 powerpc64 this means the offset must be divisible by 4.
8134 Implements 'Y' constraint.
8136 Accept direct, indexed, offset, lo_sum and tocref. Since this is
8137 a constraint function we know the operand has satisfied a suitable
8138 memory predicate. Also accept some odd rtl generated by reload
8139 (see rs6000_legitimize_reload_address for various forms). It is
8140 important that reload rtl be accepted by appropriate constraints
8141 but not by the operand predicate.
8143 Offsetting a lo_sum should not be allowed, except where we know by
8144 alignment that a 32k boundary is not crossed, but see the ???
8145 comment in rs6000_legitimize_reload_address. Note that by
8146 "offsetting" here we mean a further offset to access parts of the
8147 MEM. It's fine to have a lo_sum where the inner address is offset
8148 from a sym, since the same sym+offset will appear in the high part
8149 of the address calculation. */
8151 bool
8152 mem_operand_gpr (rtx op, machine_mode mode)
8154 unsigned HOST_WIDE_INT offset;
8155 int extra;
8156 rtx addr = XEXP (op, 0);
8158 op = address_offset (addr);
8159 if (op == NULL_RTX)
8160 return true;
8162 offset = INTVAL (op);
8163 if (TARGET_POWERPC64 && (offset & 3) != 0)
8164 return false;
8166 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
8167 if (extra < 0)
8168 extra = 0;
8170 if (GET_CODE (addr) == LO_SUM)
8171 /* For lo_sum addresses, we must allow any offset except one that
8172 causes a wrap, so test only the low 16 bits. */
8173 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
8175 return offset + 0x8000 < 0x10000u - extra;
8178 /* As above, but for DS-FORM VSX insns. Unlike mem_operand_gpr,
8179 enforce an offset divisible by 4 even for 32-bit. */
8181 bool
8182 mem_operand_ds_form (rtx op, machine_mode mode)
8184 unsigned HOST_WIDE_INT offset;
8185 int extra;
8186 rtx addr = XEXP (op, 0);
8188 if (!offsettable_address_p (false, mode, addr))
8189 return false;
8191 op = address_offset (addr);
8192 if (op == NULL_RTX)
8193 return true;
8195 offset = INTVAL (op);
8196 if ((offset & 3) != 0)
8197 return false;
8199 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
8200 if (extra < 0)
8201 extra = 0;
8203 if (GET_CODE (addr) == LO_SUM)
8204 /* For lo_sum addresses, we must allow any offset except one that
8205 causes a wrap, so test only the low 16 bits. */
8206 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
8208 return offset + 0x8000 < 0x10000u - extra;
8211 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
8213 static bool
8214 reg_offset_addressing_ok_p (machine_mode mode)
8216 switch (mode)
8218 case E_V16QImode:
8219 case E_V8HImode:
8220 case E_V4SFmode:
8221 case E_V4SImode:
8222 case E_V2DFmode:
8223 case E_V2DImode:
8224 case E_V1TImode:
8225 case E_TImode:
8226 case E_TFmode:
8227 case E_KFmode:
8228 /* AltiVec/VSX vector modes. Only reg+reg addressing was valid until the
8229 ISA 3.0 vector d-form addressing mode was added. While TImode is not
8230 a vector mode, if we want to use the VSX registers to move it around,
8231 we need to restrict ourselves to reg+reg addressing. Similarly for
8232 IEEE 128-bit floating point that is passed in a single vector
8233 register. */
8234 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
8235 return mode_supports_vsx_dform_quad (mode);
8236 break;
8238 case E_V2SImode:
8239 case E_V2SFmode:
8240 /* Paired vector modes. Only reg+reg addressing is valid. */
8241 if (TARGET_PAIRED_FLOAT)
8242 return false;
8243 break;
8245 case E_SDmode:
8246 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
8247 addressing for the LFIWZX and STFIWX instructions. */
8248 if (TARGET_NO_SDMODE_STACK)
8249 return false;
8250 break;
8252 default:
8253 break;
8256 return true;
8259 static bool
8260 virtual_stack_registers_memory_p (rtx op)
8262 int regnum;
8264 if (GET_CODE (op) == REG)
8265 regnum = REGNO (op);
8267 else if (GET_CODE (op) == PLUS
8268 && GET_CODE (XEXP (op, 0)) == REG
8269 && GET_CODE (XEXP (op, 1)) == CONST_INT)
8270 regnum = REGNO (XEXP (op, 0));
8272 else
8273 return false;
8275 return (regnum >= FIRST_VIRTUAL_REGISTER
8276 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
8279 /* Return true if a MODE sized memory accesses to OP plus OFFSET
8280 is known to not straddle a 32k boundary. This function is used
8281 to determine whether -mcmodel=medium code can use TOC pointer
8282 relative addressing for OP. This means the alignment of the TOC
8283 pointer must also be taken into account, and unfortunately that is
8284 only 8 bytes. */
8286 #ifndef POWERPC64_TOC_POINTER_ALIGNMENT
8287 #define POWERPC64_TOC_POINTER_ALIGNMENT 8
8288 #endif
8290 static bool
8291 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
8292 machine_mode mode)
8294 tree decl;
8295 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
8297 if (GET_CODE (op) != SYMBOL_REF)
8298 return false;
8300 /* ISA 3.0 vector d-form addressing is restricted, don't allow
8301 SYMBOL_REF. */
8302 if (mode_supports_vsx_dform_quad (mode))
8303 return false;
8305 dsize = GET_MODE_SIZE (mode);
8306 decl = SYMBOL_REF_DECL (op);
8307 if (!decl)
8309 if (dsize == 0)
8310 return false;
8312 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
8313 replacing memory addresses with an anchor plus offset. We
8314 could find the decl by rummaging around in the block->objects
8315 VEC for the given offset but that seems like too much work. */
8316 dalign = BITS_PER_UNIT;
8317 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
8318 && SYMBOL_REF_ANCHOR_P (op)
8319 && SYMBOL_REF_BLOCK (op) != NULL)
8321 struct object_block *block = SYMBOL_REF_BLOCK (op);
8323 dalign = block->alignment;
8324 offset += SYMBOL_REF_BLOCK_OFFSET (op);
8326 else if (CONSTANT_POOL_ADDRESS_P (op))
8328 /* It would be nice to have get_pool_align().. */
8329 machine_mode cmode = get_pool_mode (op);
8331 dalign = GET_MODE_ALIGNMENT (cmode);
8334 else if (DECL_P (decl))
8336 dalign = DECL_ALIGN (decl);
8338 if (dsize == 0)
8340 /* Allow BLKmode when the entire object is known to not
8341 cross a 32k boundary. */
8342 if (!DECL_SIZE_UNIT (decl))
8343 return false;
8345 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
8346 return false;
8348 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8349 if (dsize > 32768)
8350 return false;
8352 dalign /= BITS_PER_UNIT;
8353 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
8354 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
8355 return dalign >= dsize;
8358 else
8359 gcc_unreachable ();
8361 /* Find how many bits of the alignment we know for this access. */
8362 dalign /= BITS_PER_UNIT;
8363 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
8364 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
8365 mask = dalign - 1;
8366 lsb = offset & -offset;
8367 mask &= lsb - 1;
8368 dalign = mask + 1;
8370 return dalign >= dsize;
8373 static bool
8374 constant_pool_expr_p (rtx op)
8376 rtx base, offset;
8378 split_const (op, &base, &offset);
8379 return (GET_CODE (base) == SYMBOL_REF
8380 && CONSTANT_POOL_ADDRESS_P (base)
8381 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
8384 /* These are only used to pass through from print_operand/print_operand_address
8385 to rs6000_output_addr_const_extra over the intervening function
8386 output_addr_const which is not target code. */
8387 static const_rtx tocrel_base_oac, tocrel_offset_oac;
8389 /* Return true if OP is a toc pointer relative address (the output
8390 of create_TOC_reference). If STRICT, do not match non-split
8391 -mcmodel=large/medium toc pointer relative addresses. If the pointers
8392 are non-NULL, place base and offset pieces in TOCREL_BASE_RET and
8393 TOCREL_OFFSET_RET respectively. */
8395 bool
8396 toc_relative_expr_p (const_rtx op, bool strict, const_rtx *tocrel_base_ret,
8397 const_rtx *tocrel_offset_ret)
8399 if (!TARGET_TOC)
8400 return false;
8402 if (TARGET_CMODEL != CMODEL_SMALL)
8404 /* When strict ensure we have everything tidy. */
8405 if (strict
8406 && !(GET_CODE (op) == LO_SUM
8407 && REG_P (XEXP (op, 0))
8408 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict)))
8409 return false;
8411 /* When not strict, allow non-split TOC addresses and also allow
8412 (lo_sum (high ..)) TOC addresses created during reload. */
8413 if (GET_CODE (op) == LO_SUM)
8414 op = XEXP (op, 1);
8417 const_rtx tocrel_base = op;
8418 const_rtx tocrel_offset = const0_rtx;
8420 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
8422 tocrel_base = XEXP (op, 0);
8423 tocrel_offset = XEXP (op, 1);
8426 if (tocrel_base_ret)
8427 *tocrel_base_ret = tocrel_base;
8428 if (tocrel_offset_ret)
8429 *tocrel_offset_ret = tocrel_offset;
8431 return (GET_CODE (tocrel_base) == UNSPEC
8432 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
8435 /* Return true if X is a constant pool address, and also for cmodel=medium
8436 if X is a toc-relative address known to be offsettable within MODE. */
8438 bool
8439 legitimate_constant_pool_address_p (const_rtx x, machine_mode mode,
8440 bool strict)
8442 const_rtx tocrel_base, tocrel_offset;
8443 return (toc_relative_expr_p (x, strict, &tocrel_base, &tocrel_offset)
8444 && (TARGET_CMODEL != CMODEL_MEDIUM
8445 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
8446 || mode == QImode
8447 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
8448 INTVAL (tocrel_offset), mode)));
8451 static bool
8452 legitimate_small_data_p (machine_mode mode, rtx x)
8454 return (DEFAULT_ABI == ABI_V4
8455 && !flag_pic && !TARGET_TOC
8456 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
8457 && small_data_operand (x, mode));
8460 bool
8461 rs6000_legitimate_offset_address_p (machine_mode mode, rtx x,
8462 bool strict, bool worst_case)
8464 unsigned HOST_WIDE_INT offset;
8465 unsigned int extra;
8467 if (GET_CODE (x) != PLUS)
8468 return false;
8469 if (!REG_P (XEXP (x, 0)))
8470 return false;
8471 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
8472 return false;
8473 if (mode_supports_vsx_dform_quad (mode))
8474 return quad_address_p (x, mode, strict);
8475 if (!reg_offset_addressing_ok_p (mode))
8476 return virtual_stack_registers_memory_p (x);
8477 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
8478 return true;
8479 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
8480 return false;
8482 offset = INTVAL (XEXP (x, 1));
8483 extra = 0;
8484 switch (mode)
8486 case E_V2SImode:
8487 case E_V2SFmode:
8488 /* Paired single modes: offset addressing isn't valid. */
8489 return false;
8491 case E_DFmode:
8492 case E_DDmode:
8493 case E_DImode:
8494 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
8495 addressing. */
8496 if (VECTOR_MEM_VSX_P (mode))
8497 return false;
8499 if (!worst_case)
8500 break;
8501 if (!TARGET_POWERPC64)
8502 extra = 4;
8503 else if (offset & 3)
8504 return false;
8505 break;
8507 case E_TFmode:
8508 case E_IFmode:
8509 case E_KFmode:
8510 case E_TDmode:
8511 case E_TImode:
8512 case E_PTImode:
8513 extra = 8;
8514 if (!worst_case)
8515 break;
8516 if (!TARGET_POWERPC64)
8517 extra = 12;
8518 else if (offset & 3)
8519 return false;
8520 break;
8522 default:
8523 break;
8526 offset += 0x8000;
8527 return offset < 0x10000 - extra;
8530 bool
8531 legitimate_indexed_address_p (rtx x, int strict)
8533 rtx op0, op1;
8535 if (GET_CODE (x) != PLUS)
8536 return false;
8538 op0 = XEXP (x, 0);
8539 op1 = XEXP (x, 1);
8541 return (REG_P (op0) && REG_P (op1)
8542 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
8543 && INT_REG_OK_FOR_INDEX_P (op1, strict))
8544 || (INT_REG_OK_FOR_BASE_P (op1, strict)
8545 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
8548 bool
8549 avoiding_indexed_address_p (machine_mode mode)
8551 /* Avoid indexed addressing for modes that have non-indexed
8552 load/store instruction forms. */
8553 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
8556 bool
8557 legitimate_indirect_address_p (rtx x, int strict)
8559 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
8562 bool
8563 macho_lo_sum_memory_operand (rtx x, machine_mode mode)
8565 if (!TARGET_MACHO || !flag_pic
8566 || mode != SImode || GET_CODE (x) != MEM)
8567 return false;
8568 x = XEXP (x, 0);
8570 if (GET_CODE (x) != LO_SUM)
8571 return false;
8572 if (GET_CODE (XEXP (x, 0)) != REG)
8573 return false;
8574 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
8575 return false;
8576 x = XEXP (x, 1);
8578 return CONSTANT_P (x);
8581 static bool
8582 legitimate_lo_sum_address_p (machine_mode mode, rtx x, int strict)
8584 if (GET_CODE (x) != LO_SUM)
8585 return false;
8586 if (GET_CODE (XEXP (x, 0)) != REG)
8587 return false;
8588 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
8589 return false;
8590 /* quad word addresses are restricted, and we can't use LO_SUM. */
8591 if (mode_supports_vsx_dform_quad (mode))
8592 return false;
8593 x = XEXP (x, 1);
8595 if (TARGET_ELF || TARGET_MACHO)
8597 bool large_toc_ok;
8599 if (DEFAULT_ABI == ABI_V4 && flag_pic)
8600 return false;
8601 /* LRA doesn't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
8602 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
8603 recognizes some LO_SUM addresses as valid although this
8604 function says opposite. In most cases, LRA through different
8605 transformations can generate correct code for address reloads.
8606 It can not manage only some LO_SUM cases. So we need to add
8607 code analogous to one in rs6000_legitimize_reload_address for
8608 LOW_SUM here saying that some addresses are still valid. */
8609 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
8610 && small_toc_ref (x, VOIDmode));
8611 if (TARGET_TOC && ! large_toc_ok)
8612 return false;
8613 if (GET_MODE_NUNITS (mode) != 1)
8614 return false;
8615 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
8616 && !(/* ??? Assume floating point reg based on mode? */
8617 TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
8618 && (mode == DFmode || mode == DDmode)))
8619 return false;
8621 return CONSTANT_P (x) || large_toc_ok;
8624 return false;
8628 /* Try machine-dependent ways of modifying an illegitimate address
8629 to be legitimate. If we find one, return the new, valid address.
8630 This is used from only one place: `memory_address' in explow.c.
8632 OLDX is the address as it was before break_out_memory_refs was
8633 called. In some cases it is useful to look at this to decide what
8634 needs to be done.
8636 It is always safe for this function to do nothing. It exists to
8637 recognize opportunities to optimize the output.
8639 On RS/6000, first check for the sum of a register with a constant
8640 integer that is out of range. If so, generate code to add the
8641 constant with the low-order 16 bits masked to the register and force
8642 this result into another register (this can be done with `cau').
8643 Then generate an address of REG+(CONST&0xffff), allowing for the
8644 possibility of bit 16 being a one.
8646 Then check for the sum of a register and something not constant, try to
8647 load the other things into a register and return the sum. */
8649 static rtx
8650 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
8651 machine_mode mode)
8653 unsigned int extra;
8655 if (!reg_offset_addressing_ok_p (mode)
8656 || mode_supports_vsx_dform_quad (mode))
8658 if (virtual_stack_registers_memory_p (x))
8659 return x;
8661 /* In theory we should not be seeing addresses of the form reg+0,
8662 but just in case it is generated, optimize it away. */
8663 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
8664 return force_reg (Pmode, XEXP (x, 0));
8666 /* For TImode with load/store quad, restrict addresses to just a single
8667 pointer, so it works with both GPRs and VSX registers. */
8668 /* Make sure both operands are registers. */
8669 else if (GET_CODE (x) == PLUS
8670 && (mode != TImode || !TARGET_VSX))
8671 return gen_rtx_PLUS (Pmode,
8672 force_reg (Pmode, XEXP (x, 0)),
8673 force_reg (Pmode, XEXP (x, 1)));
8674 else
8675 return force_reg (Pmode, x);
8677 if (GET_CODE (x) == SYMBOL_REF)
8679 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
8680 if (model != 0)
8681 return rs6000_legitimize_tls_address (x, model);
8684 extra = 0;
8685 switch (mode)
8687 case E_TFmode:
8688 case E_TDmode:
8689 case E_TImode:
8690 case E_PTImode:
8691 case E_IFmode:
8692 case E_KFmode:
8693 /* As in legitimate_offset_address_p we do not assume
8694 worst-case. The mode here is just a hint as to the registers
8695 used. A TImode is usually in gprs, but may actually be in
8696 fprs. Leave worst-case scenario for reload to handle via
8697 insn constraints. PTImode is only GPRs. */
8698 extra = 8;
8699 break;
8700 default:
8701 break;
8704 if (GET_CODE (x) == PLUS
8705 && GET_CODE (XEXP (x, 0)) == REG
8706 && GET_CODE (XEXP (x, 1)) == CONST_INT
8707 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
8708 >= 0x10000 - extra)
8709 && !PAIRED_VECTOR_MODE (mode))
8711 HOST_WIDE_INT high_int, low_int;
8712 rtx sum;
8713 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
8714 if (low_int >= 0x8000 - extra)
8715 low_int = 0;
8716 high_int = INTVAL (XEXP (x, 1)) - low_int;
8717 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
8718 GEN_INT (high_int)), 0);
8719 return plus_constant (Pmode, sum, low_int);
8721 else if (GET_CODE (x) == PLUS
8722 && GET_CODE (XEXP (x, 0)) == REG
8723 && GET_CODE (XEXP (x, 1)) != CONST_INT
8724 && GET_MODE_NUNITS (mode) == 1
8725 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
8726 || (/* ??? Assume floating point reg based on mode? */
8727 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8728 && (mode == DFmode || mode == DDmode)))
8729 && !avoiding_indexed_address_p (mode))
8731 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
8732 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
8734 else if (PAIRED_VECTOR_MODE (mode))
8736 if (mode == DImode)
8737 return x;
8738 /* We accept [reg + reg]. */
8740 if (GET_CODE (x) == PLUS)
8742 rtx op1 = XEXP (x, 0);
8743 rtx op2 = XEXP (x, 1);
8744 rtx y;
8746 op1 = force_reg (Pmode, op1);
8747 op2 = force_reg (Pmode, op2);
8749 /* We can't always do [reg + reg] for these, because [reg +
8750 reg + offset] is not a legitimate addressing mode. */
8751 y = gen_rtx_PLUS (Pmode, op1, op2);
8753 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
8754 return force_reg (Pmode, y);
8755 else
8756 return y;
8759 return force_reg (Pmode, x);
8761 else if ((TARGET_ELF
8762 #if TARGET_MACHO
8763 || !MACHO_DYNAMIC_NO_PIC_P
8764 #endif
8766 && TARGET_32BIT
8767 && TARGET_NO_TOC
8768 && ! flag_pic
8769 && GET_CODE (x) != CONST_INT
8770 && GET_CODE (x) != CONST_WIDE_INT
8771 && GET_CODE (x) != CONST_DOUBLE
8772 && CONSTANT_P (x)
8773 && GET_MODE_NUNITS (mode) == 1
8774 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
8775 || (/* ??? Assume floating point reg based on mode? */
8776 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8777 && (mode == DFmode || mode == DDmode))))
8779 rtx reg = gen_reg_rtx (Pmode);
8780 if (TARGET_ELF)
8781 emit_insn (gen_elf_high (reg, x));
8782 else
8783 emit_insn (gen_macho_high (reg, x));
8784 return gen_rtx_LO_SUM (Pmode, reg, x);
8786 else if (TARGET_TOC
8787 && GET_CODE (x) == SYMBOL_REF
8788 && constant_pool_expr_p (x)
8789 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
8790 return create_TOC_reference (x, NULL_RTX);
8791 else
8792 return x;
8795 /* Debug version of rs6000_legitimize_address. */
8796 static rtx
8797 rs6000_debug_legitimize_address (rtx x, rtx oldx, machine_mode mode)
8799 rtx ret;
8800 rtx_insn *insns;
8802 start_sequence ();
8803 ret = rs6000_legitimize_address (x, oldx, mode);
8804 insns = get_insns ();
8805 end_sequence ();
8807 if (ret != x)
8809 fprintf (stderr,
8810 "\nrs6000_legitimize_address: mode %s, old code %s, "
8811 "new code %s, modified\n",
8812 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
8813 GET_RTX_NAME (GET_CODE (ret)));
8815 fprintf (stderr, "Original address:\n");
8816 debug_rtx (x);
8818 fprintf (stderr, "oldx:\n");
8819 debug_rtx (oldx);
8821 fprintf (stderr, "New address:\n");
8822 debug_rtx (ret);
8824 if (insns)
8826 fprintf (stderr, "Insns added:\n");
8827 debug_rtx_list (insns, 20);
8830 else
8832 fprintf (stderr,
8833 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
8834 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
8836 debug_rtx (x);
8839 if (insns)
8840 emit_insn (insns);
8842 return ret;
8845 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
8846 We need to emit DTP-relative relocations. */
8848 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
8849 static void
8850 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
8852 switch (size)
8854 case 4:
8855 fputs ("\t.long\t", file);
8856 break;
8857 case 8:
8858 fputs (DOUBLE_INT_ASM_OP, file);
8859 break;
8860 default:
8861 gcc_unreachable ();
8863 output_addr_const (file, x);
8864 if (TARGET_ELF)
8865 fputs ("@dtprel+0x8000", file);
8866 else if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF)
8868 switch (SYMBOL_REF_TLS_MODEL (x))
8870 case 0:
8871 break;
8872 case TLS_MODEL_LOCAL_EXEC:
8873 fputs ("@le", file);
8874 break;
8875 case TLS_MODEL_INITIAL_EXEC:
8876 fputs ("@ie", file);
8877 break;
8878 case TLS_MODEL_GLOBAL_DYNAMIC:
8879 case TLS_MODEL_LOCAL_DYNAMIC:
8880 fputs ("@m", file);
8881 break;
8882 default:
8883 gcc_unreachable ();
8888 /* Return true if X is a symbol that refers to real (rather than emulated)
8889 TLS. */
8891 static bool
8892 rs6000_real_tls_symbol_ref_p (rtx x)
8894 return (GET_CODE (x) == SYMBOL_REF
8895 && SYMBOL_REF_TLS_MODEL (x) >= TLS_MODEL_REAL);
8898 /* In the name of slightly smaller debug output, and to cater to
8899 general assembler lossage, recognize various UNSPEC sequences
8900 and turn them back into a direct symbol reference. */
8902 static rtx
8903 rs6000_delegitimize_address (rtx orig_x)
8905 rtx x, y, offset;
8907 orig_x = delegitimize_mem_from_attrs (orig_x);
8908 x = orig_x;
8909 if (MEM_P (x))
8910 x = XEXP (x, 0);
8912 y = x;
8913 if (TARGET_CMODEL != CMODEL_SMALL
8914 && GET_CODE (y) == LO_SUM)
8915 y = XEXP (y, 1);
8917 offset = NULL_RTX;
8918 if (GET_CODE (y) == PLUS
8919 && GET_MODE (y) == Pmode
8920 && CONST_INT_P (XEXP (y, 1)))
8922 offset = XEXP (y, 1);
8923 y = XEXP (y, 0);
8926 if (GET_CODE (y) == UNSPEC
8927 && XINT (y, 1) == UNSPEC_TOCREL)
8929 y = XVECEXP (y, 0, 0);
8931 #ifdef HAVE_AS_TLS
8932 /* Do not associate thread-local symbols with the original
8933 constant pool symbol. */
8934 if (TARGET_XCOFF
8935 && GET_CODE (y) == SYMBOL_REF
8936 && CONSTANT_POOL_ADDRESS_P (y)
8937 && rs6000_real_tls_symbol_ref_p (get_pool_constant (y)))
8938 return orig_x;
8939 #endif
8941 if (offset != NULL_RTX)
8942 y = gen_rtx_PLUS (Pmode, y, offset);
8943 if (!MEM_P (orig_x))
8944 return y;
8945 else
8946 return replace_equiv_address_nv (orig_x, y);
8949 if (TARGET_MACHO
8950 && GET_CODE (orig_x) == LO_SUM
8951 && GET_CODE (XEXP (orig_x, 1)) == CONST)
8953 y = XEXP (XEXP (orig_x, 1), 0);
8954 if (GET_CODE (y) == UNSPEC
8955 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
8956 return XVECEXP (y, 0, 0);
8959 return orig_x;
8962 /* Return true if X shouldn't be emitted into the debug info.
8963 The linker doesn't like .toc section references from
8964 .debug_* sections, so reject .toc section symbols. */
8966 static bool
8967 rs6000_const_not_ok_for_debug_p (rtx x)
8969 if (GET_CODE (x) == SYMBOL_REF
8970 && CONSTANT_POOL_ADDRESS_P (x))
8972 rtx c = get_pool_constant (x);
8973 machine_mode cmode = get_pool_mode (x);
8974 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
8975 return true;
8978 return false;
8982 /* Implement the TARGET_LEGITIMATE_COMBINED_INSN hook. */
8984 static bool
8985 rs6000_legitimate_combined_insn (rtx_insn *insn)
8987 int icode = INSN_CODE (insn);
8989 /* Reject creating doloop insns. Combine should not be allowed
8990 to create these for a number of reasons:
8991 1) In a nested loop, if combine creates one of these in an
8992 outer loop and the register allocator happens to allocate ctr
8993 to the outer loop insn, then the inner loop can't use ctr.
8994 Inner loops ought to be more highly optimized.
8995 2) Combine often wants to create one of these from what was
8996 originally a three insn sequence, first combining the three
8997 insns to two, then to ctrsi/ctrdi. When ctrsi/ctrdi is not
8998 allocated ctr, the splitter takes use back to the three insn
8999 sequence. It's better to stop combine at the two insn
9000 sequence.
9001 3) Faced with not being able to allocate ctr for ctrsi/crtdi
9002 insns, the register allocator sometimes uses floating point
9003 or vector registers for the pseudo. Since ctrsi/ctrdi is a
9004 jump insn and output reloads are not implemented for jumps,
9005 the ctrsi/ctrdi splitters need to handle all possible cases.
9006 That's a pain, and it gets to be seriously difficult when a
9007 splitter that runs after reload needs memory to transfer from
9008 a gpr to fpr. See PR70098 and PR71763 which are not fixed
9009 for the difficult case. It's better to not create problems
9010 in the first place. */
9011 if (icode != CODE_FOR_nothing
9012 && (icode == CODE_FOR_ctrsi_internal1
9013 || icode == CODE_FOR_ctrdi_internal1
9014 || icode == CODE_FOR_ctrsi_internal2
9015 || icode == CODE_FOR_ctrdi_internal2
9016 || icode == CODE_FOR_ctrsi_internal3
9017 || icode == CODE_FOR_ctrdi_internal3
9018 || icode == CODE_FOR_ctrsi_internal4
9019 || icode == CODE_FOR_ctrdi_internal4))
9020 return false;
9022 return true;
9025 /* Construct the SYMBOL_REF for the tls_get_addr function. */
9027 static GTY(()) rtx rs6000_tls_symbol;
9028 static rtx
9029 rs6000_tls_get_addr (void)
9031 if (!rs6000_tls_symbol)
9032 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
9034 return rs6000_tls_symbol;
9037 /* Construct the SYMBOL_REF for TLS GOT references. */
9039 static GTY(()) rtx rs6000_got_symbol;
9040 static rtx
9041 rs6000_got_sym (void)
9043 if (!rs6000_got_symbol)
9045 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
9046 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
9047 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
9050 return rs6000_got_symbol;
9053 /* AIX Thread-Local Address support. */
9055 static rtx
9056 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
9058 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
9059 const char *name;
9060 char *tlsname;
9062 name = XSTR (addr, 0);
9063 /* Append TLS CSECT qualifier, unless the symbol already is qualified
9064 or the symbol will be in TLS private data section. */
9065 if (name[strlen (name) - 1] != ']'
9066 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
9067 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
9069 tlsname = XALLOCAVEC (char, strlen (name) + 4);
9070 strcpy (tlsname, name);
9071 strcat (tlsname,
9072 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
9073 tlsaddr = copy_rtx (addr);
9074 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
9076 else
9077 tlsaddr = addr;
9079 /* Place addr into TOC constant pool. */
9080 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
9082 /* Output the TOC entry and create the MEM referencing the value. */
9083 if (constant_pool_expr_p (XEXP (sym, 0))
9084 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
9086 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
9087 mem = gen_const_mem (Pmode, tocref);
9088 set_mem_alias_set (mem, get_TOC_alias_set ());
9090 else
9091 return sym;
9093 /* Use global-dynamic for local-dynamic. */
9094 if (model == TLS_MODEL_GLOBAL_DYNAMIC
9095 || model == TLS_MODEL_LOCAL_DYNAMIC)
9097 /* Create new TOC reference for @m symbol. */
9098 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
9099 tlsname = XALLOCAVEC (char, strlen (name) + 1);
9100 strcpy (tlsname, "*LCM");
9101 strcat (tlsname, name + 3);
9102 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
9103 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
9104 tocref = create_TOC_reference (modaddr, NULL_RTX);
9105 rtx modmem = gen_const_mem (Pmode, tocref);
9106 set_mem_alias_set (modmem, get_TOC_alias_set ());
9108 rtx modreg = gen_reg_rtx (Pmode);
9109 emit_insn (gen_rtx_SET (modreg, modmem));
9111 tmpreg = gen_reg_rtx (Pmode);
9112 emit_insn (gen_rtx_SET (tmpreg, mem));
9114 dest = gen_reg_rtx (Pmode);
9115 if (TARGET_32BIT)
9116 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
9117 else
9118 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
9119 return dest;
9121 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
9122 else if (TARGET_32BIT)
9124 tlsreg = gen_reg_rtx (SImode);
9125 emit_insn (gen_tls_get_tpointer (tlsreg));
9127 else
9128 tlsreg = gen_rtx_REG (DImode, 13);
9130 /* Load the TOC value into temporary register. */
9131 tmpreg = gen_reg_rtx (Pmode);
9132 emit_insn (gen_rtx_SET (tmpreg, mem));
9133 set_unique_reg_note (get_last_insn (), REG_EQUAL,
9134 gen_rtx_MINUS (Pmode, addr, tlsreg));
9136 /* Add TOC symbol value to TLS pointer. */
9137 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
9139 return dest;
9142 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
9143 this (thread-local) address. */
9145 static rtx
9146 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
9148 rtx dest, insn;
9150 if (TARGET_XCOFF)
9151 return rs6000_legitimize_tls_address_aix (addr, model);
9153 dest = gen_reg_rtx (Pmode);
9154 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
9156 rtx tlsreg;
9158 if (TARGET_64BIT)
9160 tlsreg = gen_rtx_REG (Pmode, 13);
9161 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
9163 else
9165 tlsreg = gen_rtx_REG (Pmode, 2);
9166 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
9168 emit_insn (insn);
9170 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
9172 rtx tlsreg, tmp;
9174 tmp = gen_reg_rtx (Pmode);
9175 if (TARGET_64BIT)
9177 tlsreg = gen_rtx_REG (Pmode, 13);
9178 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
9180 else
9182 tlsreg = gen_rtx_REG (Pmode, 2);
9183 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
9185 emit_insn (insn);
9186 if (TARGET_64BIT)
9187 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
9188 else
9189 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
9190 emit_insn (insn);
9192 else
9194 rtx r3, got, tga, tmp1, tmp2, call_insn;
9196 /* We currently use relocations like @got@tlsgd for tls, which
9197 means the linker will handle allocation of tls entries, placing
9198 them in the .got section. So use a pointer to the .got section,
9199 not one to secondary TOC sections used by 64-bit -mminimal-toc,
9200 or to secondary GOT sections used by 32-bit -fPIC. */
9201 if (TARGET_64BIT)
9202 got = gen_rtx_REG (Pmode, 2);
9203 else
9205 if (flag_pic == 1)
9206 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
9207 else
9209 rtx gsym = rs6000_got_sym ();
9210 got = gen_reg_rtx (Pmode);
9211 if (flag_pic == 0)
9212 rs6000_emit_move (got, gsym, Pmode);
9213 else
9215 rtx mem, lab;
9217 tmp1 = gen_reg_rtx (Pmode);
9218 tmp2 = gen_reg_rtx (Pmode);
9219 mem = gen_const_mem (Pmode, tmp1);
9220 lab = gen_label_rtx ();
9221 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
9222 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
9223 if (TARGET_LINK_STACK)
9224 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
9225 emit_move_insn (tmp2, mem);
9226 rtx_insn *last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
9227 set_unique_reg_note (last, REG_EQUAL, gsym);
9232 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
9234 tga = rs6000_tls_get_addr ();
9235 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
9236 1, const0_rtx, Pmode);
9238 r3 = gen_rtx_REG (Pmode, 3);
9239 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9241 if (TARGET_64BIT)
9242 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
9243 else
9244 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
9246 else if (DEFAULT_ABI == ABI_V4)
9247 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
9248 else
9249 gcc_unreachable ();
9250 call_insn = last_call_insn ();
9251 PATTERN (call_insn) = insn;
9252 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
9253 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
9254 pic_offset_table_rtx);
9256 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
9258 tga = rs6000_tls_get_addr ();
9259 tmp1 = gen_reg_rtx (Pmode);
9260 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
9261 1, const0_rtx, Pmode);
9263 r3 = gen_rtx_REG (Pmode, 3);
9264 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9266 if (TARGET_64BIT)
9267 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
9268 else
9269 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
9271 else if (DEFAULT_ABI == ABI_V4)
9272 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
9273 else
9274 gcc_unreachable ();
9275 call_insn = last_call_insn ();
9276 PATTERN (call_insn) = insn;
9277 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
9278 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
9279 pic_offset_table_rtx);
9281 if (rs6000_tls_size == 16)
9283 if (TARGET_64BIT)
9284 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
9285 else
9286 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
9288 else if (rs6000_tls_size == 32)
9290 tmp2 = gen_reg_rtx (Pmode);
9291 if (TARGET_64BIT)
9292 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
9293 else
9294 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
9295 emit_insn (insn);
9296 if (TARGET_64BIT)
9297 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
9298 else
9299 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
9301 else
9303 tmp2 = gen_reg_rtx (Pmode);
9304 if (TARGET_64BIT)
9305 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
9306 else
9307 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
9308 emit_insn (insn);
9309 insn = gen_rtx_SET (dest, gen_rtx_PLUS (Pmode, tmp2, tmp1));
9311 emit_insn (insn);
9313 else
9315 /* IE, or 64-bit offset LE. */
9316 tmp2 = gen_reg_rtx (Pmode);
9317 if (TARGET_64BIT)
9318 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
9319 else
9320 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
9321 emit_insn (insn);
9322 if (TARGET_64BIT)
9323 insn = gen_tls_tls_64 (dest, tmp2, addr);
9324 else
9325 insn = gen_tls_tls_32 (dest, tmp2, addr);
9326 emit_insn (insn);
9330 return dest;
9333 /* Only create the global variable for the stack protect guard if we are using
9334 the global flavor of that guard. */
9335 static tree
9336 rs6000_init_stack_protect_guard (void)
9338 if (rs6000_stack_protector_guard == SSP_GLOBAL)
9339 return default_stack_protect_guard ();
9341 return NULL_TREE;
9344 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
9346 static bool
9347 rs6000_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
9349 if (GET_CODE (x) == HIGH
9350 && GET_CODE (XEXP (x, 0)) == UNSPEC)
9351 return true;
9353 /* A TLS symbol in the TOC cannot contain a sum. */
9354 if (GET_CODE (x) == CONST
9355 && GET_CODE (XEXP (x, 0)) == PLUS
9356 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
9357 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
9358 return true;
9360 /* Do not place an ELF TLS symbol in the constant pool. */
9361 return TARGET_ELF && tls_referenced_p (x);
9364 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
9365 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
9366 can be addressed relative to the toc pointer. */
9368 static bool
9369 use_toc_relative_ref (rtx sym, machine_mode mode)
9371 return ((constant_pool_expr_p (sym)
9372 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
9373 get_pool_mode (sym)))
9374 || (TARGET_CMODEL == CMODEL_MEDIUM
9375 && SYMBOL_REF_LOCAL_P (sym)
9376 && GET_MODE_SIZE (mode) <= POWERPC64_TOC_POINTER_ALIGNMENT));
9379 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
9380 replace the input X, or the original X if no replacement is called for.
9381 The output parameter *WIN is 1 if the calling macro should goto WIN,
9382 0 if it should not.
9384 For RS/6000, we wish to handle large displacements off a base
9385 register by splitting the addend across an addiu/addis and the mem insn.
9386 This cuts number of extra insns needed from 3 to 1.
9388 On Darwin, we use this to generate code for floating point constants.
9389 A movsf_low is generated so we wind up with 2 instructions rather than 3.
9390 The Darwin code is inside #if TARGET_MACHO because only then are the
9391 machopic_* functions defined. */
9392 static rtx
9393 rs6000_legitimize_reload_address (rtx x, machine_mode mode,
9394 int opnum, int type,
9395 int ind_levels ATTRIBUTE_UNUSED, int *win)
9397 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
9398 bool quad_offset_p = mode_supports_vsx_dform_quad (mode);
9400 /* Nasty hack for vsx_splat_v2df/v2di load from mem, which takes a
9401 DFmode/DImode MEM. Ditto for ISA 3.0 vsx_splat_v4sf/v4si. */
9402 if (reg_offset_p
9403 && opnum == 1
9404 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
9405 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)
9406 || (mode == SFmode && recog_data.operand_mode[0] == V4SFmode
9407 && TARGET_P9_VECTOR)
9408 || (mode == SImode && recog_data.operand_mode[0] == V4SImode
9409 && TARGET_P9_VECTOR)))
9410 reg_offset_p = false;
9412 /* We must recognize output that we have already generated ourselves. */
9413 if (GET_CODE (x) == PLUS
9414 && GET_CODE (XEXP (x, 0)) == PLUS
9415 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
9416 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
9417 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9419 if (TARGET_DEBUG_ADDR)
9421 fprintf (stderr, "\nlegitimize_reload_address push_reload #1:\n");
9422 debug_rtx (x);
9424 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9425 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
9426 opnum, (enum reload_type) type);
9427 *win = 1;
9428 return x;
9431 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
9432 if (GET_CODE (x) == LO_SUM
9433 && GET_CODE (XEXP (x, 0)) == HIGH)
9435 if (TARGET_DEBUG_ADDR)
9437 fprintf (stderr, "\nlegitimize_reload_address push_reload #2:\n");
9438 debug_rtx (x);
9440 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9441 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
9442 opnum, (enum reload_type) type);
9443 *win = 1;
9444 return x;
9447 #if TARGET_MACHO
9448 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
9449 && GET_CODE (x) == LO_SUM
9450 && GET_CODE (XEXP (x, 0)) == PLUS
9451 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
9452 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
9453 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
9454 && machopic_operand_p (XEXP (x, 1)))
9456 /* Result of previous invocation of this function on Darwin
9457 floating point constant. */
9458 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9459 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
9460 opnum, (enum reload_type) type);
9461 *win = 1;
9462 return x;
9464 #endif
9466 if (TARGET_CMODEL != CMODEL_SMALL
9467 && reg_offset_p
9468 && !quad_offset_p
9469 && small_toc_ref (x, VOIDmode))
9471 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
9472 x = gen_rtx_LO_SUM (Pmode, hi, x);
9473 if (TARGET_DEBUG_ADDR)
9475 fprintf (stderr, "\nlegitimize_reload_address push_reload #3:\n");
9476 debug_rtx (x);
9478 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9479 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
9480 opnum, (enum reload_type) type);
9481 *win = 1;
9482 return x;
9485 if (GET_CODE (x) == PLUS
9486 && REG_P (XEXP (x, 0))
9487 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
9488 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
9489 && CONST_INT_P (XEXP (x, 1))
9490 && reg_offset_p
9491 && !PAIRED_VECTOR_MODE (mode)
9492 && (quad_offset_p || !VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
9494 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
9495 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
9496 HOST_WIDE_INT high
9497 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
9499 /* Check for 32-bit overflow or quad addresses with one of the
9500 four least significant bits set. */
9501 if (high + low != val
9502 || (quad_offset_p && (low & 0xf)))
9504 *win = 0;
9505 return x;
9508 /* Reload the high part into a base reg; leave the low part
9509 in the mem directly. */
9511 x = gen_rtx_PLUS (GET_MODE (x),
9512 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
9513 GEN_INT (high)),
9514 GEN_INT (low));
9516 if (TARGET_DEBUG_ADDR)
9518 fprintf (stderr, "\nlegitimize_reload_address push_reload #4:\n");
9519 debug_rtx (x);
9521 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9522 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
9523 opnum, (enum reload_type) type);
9524 *win = 1;
9525 return x;
9528 if (GET_CODE (x) == SYMBOL_REF
9529 && reg_offset_p
9530 && !quad_offset_p
9531 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
9532 && !PAIRED_VECTOR_MODE (mode)
9533 #if TARGET_MACHO
9534 && DEFAULT_ABI == ABI_DARWIN
9535 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
9536 && machopic_symbol_defined_p (x)
9537 #else
9538 && DEFAULT_ABI == ABI_V4
9539 && !flag_pic
9540 #endif
9541 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
9542 The same goes for DImode without 64-bit gprs and DFmode and DDmode
9543 without fprs.
9544 ??? Assume floating point reg based on mode? This assumption is
9545 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
9546 where reload ends up doing a DFmode load of a constant from
9547 mem using two gprs. Unfortunately, at this point reload
9548 hasn't yet selected regs so poking around in reload data
9549 won't help and even if we could figure out the regs reliably,
9550 we'd still want to allow this transformation when the mem is
9551 naturally aligned. Since we say the address is good here, we
9552 can't disable offsets from LO_SUMs in mem_operand_gpr.
9553 FIXME: Allow offset from lo_sum for other modes too, when
9554 mem is sufficiently aligned.
9556 Also disallow this if the type can go in VMX/Altivec registers, since
9557 those registers do not have d-form (reg+offset) address modes. */
9558 && !reg_addr[mode].scalar_in_vmx_p
9559 && mode != TFmode
9560 && mode != TDmode
9561 && mode != IFmode
9562 && mode != KFmode
9563 && (mode != TImode || !TARGET_VSX)
9564 && mode != PTImode
9565 && (mode != DImode || TARGET_POWERPC64)
9566 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
9567 || (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)))
9569 #if TARGET_MACHO
9570 if (flag_pic)
9572 rtx offset = machopic_gen_offset (x);
9573 x = gen_rtx_LO_SUM (GET_MODE (x),
9574 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
9575 gen_rtx_HIGH (Pmode, offset)), offset);
9577 else
9578 #endif
9579 x = gen_rtx_LO_SUM (GET_MODE (x),
9580 gen_rtx_HIGH (Pmode, x), x);
9582 if (TARGET_DEBUG_ADDR)
9584 fprintf (stderr, "\nlegitimize_reload_address push_reload #5:\n");
9585 debug_rtx (x);
9587 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9588 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
9589 opnum, (enum reload_type) type);
9590 *win = 1;
9591 return x;
9594 /* Reload an offset address wrapped by an AND that represents the
9595 masking of the lower bits. Strip the outer AND and let reload
9596 convert the offset address into an indirect address. For VSX,
9597 force reload to create the address with an AND in a separate
9598 register, because we can't guarantee an altivec register will
9599 be used. */
9600 if (VECTOR_MEM_ALTIVEC_P (mode)
9601 && GET_CODE (x) == AND
9602 && GET_CODE (XEXP (x, 0)) == PLUS
9603 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
9604 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
9605 && GET_CODE (XEXP (x, 1)) == CONST_INT
9606 && INTVAL (XEXP (x, 1)) == -16)
9608 x = XEXP (x, 0);
9609 *win = 1;
9610 return x;
9613 if (TARGET_TOC
9614 && reg_offset_p
9615 && !quad_offset_p
9616 && GET_CODE (x) == SYMBOL_REF
9617 && use_toc_relative_ref (x, mode))
9619 x = create_TOC_reference (x, NULL_RTX);
9620 if (TARGET_CMODEL != CMODEL_SMALL)
9622 if (TARGET_DEBUG_ADDR)
9624 fprintf (stderr, "\nlegitimize_reload_address push_reload #6:\n");
9625 debug_rtx (x);
9627 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
9628 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
9629 opnum, (enum reload_type) type);
9631 *win = 1;
9632 return x;
9634 *win = 0;
9635 return x;
9638 /* Debug version of rs6000_legitimize_reload_address. */
9639 static rtx
9640 rs6000_debug_legitimize_reload_address (rtx x, machine_mode mode,
9641 int opnum, int type,
9642 int ind_levels, int *win)
9644 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
9645 ind_levels, win);
9646 fprintf (stderr,
9647 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
9648 "type = %d, ind_levels = %d, win = %d, original addr:\n",
9649 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
9650 debug_rtx (x);
9652 if (x == ret)
9653 fprintf (stderr, "Same address returned\n");
9654 else if (!ret)
9655 fprintf (stderr, "NULL returned\n");
9656 else
9658 fprintf (stderr, "New address:\n");
9659 debug_rtx (ret);
9662 return ret;
9665 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
9666 that is a valid memory address for an instruction.
9667 The MODE argument is the machine mode for the MEM expression
9668 that wants to use this address.
9670 On the RS/6000, there are four valid address: a SYMBOL_REF that
9671 refers to a constant pool entry of an address (or the sum of it
9672 plus a constant), a short (16-bit signed) constant plus a register,
9673 the sum of two registers, or a register indirect, possibly with an
9674 auto-increment. For DFmode, DDmode and DImode with a constant plus
9675 register, we must ensure that both words are addressable or PowerPC64
9676 with offset word aligned.
9678 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
9679 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
9680 because adjacent memory cells are accessed by adding word-sized offsets
9681 during assembly output. */
9682 static bool
9683 rs6000_legitimate_address_p (machine_mode mode, rtx x, bool reg_ok_strict)
9685 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
9686 bool quad_offset_p = mode_supports_vsx_dform_quad (mode);
9688 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
9689 if (VECTOR_MEM_ALTIVEC_P (mode)
9690 && GET_CODE (x) == AND
9691 && GET_CODE (XEXP (x, 1)) == CONST_INT
9692 && INTVAL (XEXP (x, 1)) == -16)
9693 x = XEXP (x, 0);
9695 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
9696 return 0;
9697 if (legitimate_indirect_address_p (x, reg_ok_strict))
9698 return 1;
9699 if (TARGET_UPDATE
9700 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
9701 && mode_supports_pre_incdec_p (mode)
9702 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
9703 return 1;
9704 /* Handle restricted vector d-form offsets in ISA 3.0. */
9705 if (quad_offset_p)
9707 if (quad_address_p (x, mode, reg_ok_strict))
9708 return 1;
9710 else if (virtual_stack_registers_memory_p (x))
9711 return 1;
9713 else if (reg_offset_p)
9715 if (legitimate_small_data_p (mode, x))
9716 return 1;
9717 if (legitimate_constant_pool_address_p (x, mode,
9718 reg_ok_strict || lra_in_progress))
9719 return 1;
9720 if (reg_addr[mode].fused_toc && GET_CODE (x) == UNSPEC
9721 && XINT (x, 1) == UNSPEC_FUSION_ADDIS)
9722 return 1;
9725 /* For TImode, if we have TImode in VSX registers, only allow register
9726 indirect addresses. This will allow the values to go in either GPRs
9727 or VSX registers without reloading. The vector types would tend to
9728 go into VSX registers, so we allow REG+REG, while TImode seems
9729 somewhat split, in that some uses are GPR based, and some VSX based. */
9730 /* FIXME: We could loosen this by changing the following to
9731 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX)
9732 but currently we cannot allow REG+REG addressing for TImode. See
9733 PR72827 for complete details on how this ends up hoodwinking DSE. */
9734 if (mode == TImode && TARGET_VSX)
9735 return 0;
9736 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
9737 if (! reg_ok_strict
9738 && reg_offset_p
9739 && GET_CODE (x) == PLUS
9740 && GET_CODE (XEXP (x, 0)) == REG
9741 && (XEXP (x, 0) == virtual_stack_vars_rtx
9742 || XEXP (x, 0) == arg_pointer_rtx)
9743 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9744 return 1;
9745 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
9746 return 1;
9747 if (!FLOAT128_2REG_P (mode)
9748 && ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9749 || TARGET_POWERPC64
9750 || (mode != DFmode && mode != DDmode))
9751 && (TARGET_POWERPC64 || mode != DImode)
9752 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
9753 && mode != PTImode
9754 && !avoiding_indexed_address_p (mode)
9755 && legitimate_indexed_address_p (x, reg_ok_strict))
9756 return 1;
9757 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
9758 && mode_supports_pre_modify_p (mode)
9759 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
9760 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
9761 reg_ok_strict, false)
9762 || (!avoiding_indexed_address_p (mode)
9763 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
9764 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
9765 return 1;
9766 if (reg_offset_p && !quad_offset_p
9767 && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
9768 return 1;
9769 return 0;
9772 /* Debug version of rs6000_legitimate_address_p. */
9773 static bool
9774 rs6000_debug_legitimate_address_p (machine_mode mode, rtx x,
9775 bool reg_ok_strict)
9777 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
9778 fprintf (stderr,
9779 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
9780 "strict = %d, reload = %s, code = %s\n",
9781 ret ? "true" : "false",
9782 GET_MODE_NAME (mode),
9783 reg_ok_strict,
9784 (reload_completed ? "after" : "before"),
9785 GET_RTX_NAME (GET_CODE (x)));
9786 debug_rtx (x);
9788 return ret;
9791 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
9793 static bool
9794 rs6000_mode_dependent_address_p (const_rtx addr,
9795 addr_space_t as ATTRIBUTE_UNUSED)
9797 return rs6000_mode_dependent_address_ptr (addr);
9800 /* Go to LABEL if ADDR (a legitimate address expression)
9801 has an effect that depends on the machine mode it is used for.
9803 On the RS/6000 this is true of all integral offsets (since AltiVec
9804 and VSX modes don't allow them) or is a pre-increment or decrement.
9806 ??? Except that due to conceptual problems in offsettable_address_p
9807 we can't really report the problems of integral offsets. So leave
9808 this assuming that the adjustable offset must be valid for the
9809 sub-words of a TFmode operand, which is what we had before. */
9811 static bool
9812 rs6000_mode_dependent_address (const_rtx addr)
9814 switch (GET_CODE (addr))
9816 case PLUS:
9817 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
9818 is considered a legitimate address before reload, so there
9819 are no offset restrictions in that case. Note that this
9820 condition is safe in strict mode because any address involving
9821 virtual_stack_vars_rtx or arg_pointer_rtx would already have
9822 been rejected as illegitimate. */
9823 if (XEXP (addr, 0) != virtual_stack_vars_rtx
9824 && XEXP (addr, 0) != arg_pointer_rtx
9825 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
9827 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
9828 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
9830 break;
9832 case LO_SUM:
9833 /* Anything in the constant pool is sufficiently aligned that
9834 all bytes have the same high part address. */
9835 return !legitimate_constant_pool_address_p (addr, QImode, false);
9837 /* Auto-increment cases are now treated generically in recog.c. */
9838 case PRE_MODIFY:
9839 return TARGET_UPDATE;
9841 /* AND is only allowed in Altivec loads. */
9842 case AND:
9843 return true;
9845 default:
9846 break;
9849 return false;
9852 /* Debug version of rs6000_mode_dependent_address. */
9853 static bool
9854 rs6000_debug_mode_dependent_address (const_rtx addr)
9856 bool ret = rs6000_mode_dependent_address (addr);
9858 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
9859 ret ? "true" : "false");
9860 debug_rtx (addr);
9862 return ret;
9865 /* Implement FIND_BASE_TERM. */
9868 rs6000_find_base_term (rtx op)
9870 rtx base;
9872 base = op;
9873 if (GET_CODE (base) == CONST)
9874 base = XEXP (base, 0);
9875 if (GET_CODE (base) == PLUS)
9876 base = XEXP (base, 0);
9877 if (GET_CODE (base) == UNSPEC)
9878 switch (XINT (base, 1))
9880 case UNSPEC_TOCREL:
9881 case UNSPEC_MACHOPIC_OFFSET:
9882 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
9883 for aliasing purposes. */
9884 return XVECEXP (base, 0, 0);
9887 return op;
9890 /* More elaborate version of recog's offsettable_memref_p predicate
9891 that works around the ??? note of rs6000_mode_dependent_address.
9892 In particular it accepts
9894 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
9896 in 32-bit mode, that the recog predicate rejects. */
9898 static bool
9899 rs6000_offsettable_memref_p (rtx op, machine_mode reg_mode)
9901 bool worst_case;
9903 if (!MEM_P (op))
9904 return false;
9906 /* First mimic offsettable_memref_p. */
9907 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
9908 return true;
9910 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
9911 the latter predicate knows nothing about the mode of the memory
9912 reference and, therefore, assumes that it is the largest supported
9913 mode (TFmode). As a consequence, legitimate offsettable memory
9914 references are rejected. rs6000_legitimate_offset_address_p contains
9915 the correct logic for the PLUS case of rs6000_mode_dependent_address,
9916 at least with a little bit of help here given that we know the
9917 actual registers used. */
9918 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
9919 || GET_MODE_SIZE (reg_mode) == 4);
9920 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
9921 true, worst_case);
9924 /* Determine the reassociation width to be used in reassociate_bb.
9925 This takes into account how many parallel operations we
9926 can actually do of a given type, and also the latency.
9928 int add/sub 6/cycle
9929 mul 2/cycle
9930 vect add/sub/mul 2/cycle
9931 fp add/sub/mul 2/cycle
9932 dfp 1/cycle
9935 static int
9936 rs6000_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
9937 machine_mode mode)
9939 switch (rs6000_cpu)
9941 case PROCESSOR_POWER8:
9942 case PROCESSOR_POWER9:
9943 if (DECIMAL_FLOAT_MODE_P (mode))
9944 return 1;
9945 if (VECTOR_MODE_P (mode))
9946 return 4;
9947 if (INTEGRAL_MODE_P (mode))
9948 return opc == MULT_EXPR ? 4 : 6;
9949 if (FLOAT_MODE_P (mode))
9950 return 4;
9951 break;
9952 default:
9953 break;
9955 return 1;
9958 /* Change register usage conditional on target flags. */
9959 static void
9960 rs6000_conditional_register_usage (void)
9962 int i;
9964 if (TARGET_DEBUG_TARGET)
9965 fprintf (stderr, "rs6000_conditional_register_usage called\n");
9967 /* Set MQ register fixed (already call_used) so that it will not be
9968 allocated. */
9969 fixed_regs[64] = 1;
9971 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
9972 if (TARGET_64BIT)
9973 fixed_regs[13] = call_used_regs[13]
9974 = call_really_used_regs[13] = 1;
9976 /* Conditionally disable FPRs. */
9977 if (TARGET_SOFT_FLOAT)
9978 for (i = 32; i < 64; i++)
9979 fixed_regs[i] = call_used_regs[i]
9980 = call_really_used_regs[i] = 1;
9982 /* The TOC register is not killed across calls in a way that is
9983 visible to the compiler. */
9984 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9985 call_really_used_regs[2] = 0;
9987 if (DEFAULT_ABI == ABI_V4 && flag_pic == 2)
9988 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9990 if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
9991 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9992 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9993 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9995 if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
9996 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9997 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9998 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10000 if (TARGET_TOC && TARGET_MINIMAL_TOC)
10001 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
10002 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
10004 if (!TARGET_ALTIVEC && !TARGET_VSX)
10006 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10007 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
10008 call_really_used_regs[VRSAVE_REGNO] = 1;
10011 if (TARGET_ALTIVEC || TARGET_VSX)
10012 global_regs[VSCR_REGNO] = 1;
10014 if (TARGET_ALTIVEC_ABI)
10016 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
10017 call_used_regs[i] = call_really_used_regs[i] = 1;
10019 /* AIX reserves VR20:31 in non-extended ABI mode. */
10020 if (TARGET_XCOFF)
10021 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
10022 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
10027 /* Output insns to set DEST equal to the constant SOURCE as a series of
10028 lis, ori and shl instructions and return TRUE. */
10030 bool
10031 rs6000_emit_set_const (rtx dest, rtx source)
10033 machine_mode mode = GET_MODE (dest);
10034 rtx temp, set;
10035 rtx_insn *insn;
10036 HOST_WIDE_INT c;
10038 gcc_checking_assert (CONST_INT_P (source));
10039 c = INTVAL (source);
10040 switch (mode)
10042 case E_QImode:
10043 case E_HImode:
10044 emit_insn (gen_rtx_SET (dest, source));
10045 return true;
10047 case E_SImode:
10048 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
10050 emit_insn (gen_rtx_SET (copy_rtx (temp),
10051 GEN_INT (c & ~(HOST_WIDE_INT) 0xffff)));
10052 emit_insn (gen_rtx_SET (dest,
10053 gen_rtx_IOR (SImode, copy_rtx (temp),
10054 GEN_INT (c & 0xffff))));
10055 break;
10057 case E_DImode:
10058 if (!TARGET_POWERPC64)
10060 rtx hi, lo;
10062 hi = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN == 0,
10063 DImode);
10064 lo = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
10065 DImode);
10066 emit_move_insn (hi, GEN_INT (c >> 32));
10067 c = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
10068 emit_move_insn (lo, GEN_INT (c));
10070 else
10071 rs6000_emit_set_long_const (dest, c);
10072 break;
10074 default:
10075 gcc_unreachable ();
10078 insn = get_last_insn ();
10079 set = single_set (insn);
10080 if (! CONSTANT_P (SET_SRC (set)))
10081 set_unique_reg_note (insn, REG_EQUAL, GEN_INT (c));
10083 return true;
10086 /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode.
10087 Output insns to set DEST equal to the constant C as a series of
10088 lis, ori and shl instructions. */
10090 static void
10091 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c)
10093 rtx temp;
10094 HOST_WIDE_INT ud1, ud2, ud3, ud4;
10096 ud1 = c & 0xffff;
10097 c = c >> 16;
10098 ud2 = c & 0xffff;
10099 c = c >> 16;
10100 ud3 = c & 0xffff;
10101 c = c >> 16;
10102 ud4 = c & 0xffff;
10104 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
10105 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
10106 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
10108 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
10109 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
10111 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10113 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10114 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
10115 if (ud1 != 0)
10116 emit_move_insn (dest,
10117 gen_rtx_IOR (DImode, copy_rtx (temp),
10118 GEN_INT (ud1)));
10120 else if (ud3 == 0 && ud4 == 0)
10122 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10124 gcc_assert (ud2 & 0x8000);
10125 emit_move_insn (copy_rtx (temp),
10126 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
10127 if (ud1 != 0)
10128 emit_move_insn (copy_rtx (temp),
10129 gen_rtx_IOR (DImode, copy_rtx (temp),
10130 GEN_INT (ud1)));
10131 emit_move_insn (dest,
10132 gen_rtx_ZERO_EXTEND (DImode,
10133 gen_lowpart (SImode,
10134 copy_rtx (temp))));
10136 else if ((ud4 == 0xffff && (ud3 & 0x8000))
10137 || (ud4 == 0 && ! (ud3 & 0x8000)))
10139 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10141 emit_move_insn (copy_rtx (temp),
10142 GEN_INT (((ud3 << 16) ^ 0x80000000) - 0x80000000));
10143 if (ud2 != 0)
10144 emit_move_insn (copy_rtx (temp),
10145 gen_rtx_IOR (DImode, copy_rtx (temp),
10146 GEN_INT (ud2)));
10147 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10148 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
10149 GEN_INT (16)));
10150 if (ud1 != 0)
10151 emit_move_insn (dest,
10152 gen_rtx_IOR (DImode, copy_rtx (temp),
10153 GEN_INT (ud1)));
10155 else
10157 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
10159 emit_move_insn (copy_rtx (temp),
10160 GEN_INT (((ud4 << 16) ^ 0x80000000) - 0x80000000));
10161 if (ud3 != 0)
10162 emit_move_insn (copy_rtx (temp),
10163 gen_rtx_IOR (DImode, copy_rtx (temp),
10164 GEN_INT (ud3)));
10166 emit_move_insn (ud2 != 0 || ud1 != 0 ? copy_rtx (temp) : dest,
10167 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
10168 GEN_INT (32)));
10169 if (ud2 != 0)
10170 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
10171 gen_rtx_IOR (DImode, copy_rtx (temp),
10172 GEN_INT (ud2 << 16)));
10173 if (ud1 != 0)
10174 emit_move_insn (dest,
10175 gen_rtx_IOR (DImode, copy_rtx (temp),
10176 GEN_INT (ud1)));
10180 /* Helper for the following. Get rid of [r+r] memory refs
10181 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
10183 static void
10184 rs6000_eliminate_indexed_memrefs (rtx operands[2])
10186 if (GET_CODE (operands[0]) == MEM
10187 && GET_CODE (XEXP (operands[0], 0)) != REG
10188 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
10189 GET_MODE (operands[0]), false))
10190 operands[0]
10191 = replace_equiv_address (operands[0],
10192 copy_addr_to_reg (XEXP (operands[0], 0)));
10194 if (GET_CODE (operands[1]) == MEM
10195 && GET_CODE (XEXP (operands[1], 0)) != REG
10196 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
10197 GET_MODE (operands[1]), false))
10198 operands[1]
10199 = replace_equiv_address (operands[1],
10200 copy_addr_to_reg (XEXP (operands[1], 0)));
10203 /* Generate a vector of constants to permute MODE for a little-endian
10204 storage operation by swapping the two halves of a vector. */
10205 static rtvec
10206 rs6000_const_vec (machine_mode mode)
10208 int i, subparts;
10209 rtvec v;
10211 switch (mode)
10213 case E_V1TImode:
10214 subparts = 1;
10215 break;
10216 case E_V2DFmode:
10217 case E_V2DImode:
10218 subparts = 2;
10219 break;
10220 case E_V4SFmode:
10221 case E_V4SImode:
10222 subparts = 4;
10223 break;
10224 case E_V8HImode:
10225 subparts = 8;
10226 break;
10227 case E_V16QImode:
10228 subparts = 16;
10229 break;
10230 default:
10231 gcc_unreachable();
10234 v = rtvec_alloc (subparts);
10236 for (i = 0; i < subparts / 2; ++i)
10237 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
10238 for (i = subparts / 2; i < subparts; ++i)
10239 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
10241 return v;
10244 /* Emit an lxvd2x, stxvd2x, or xxpermdi instruction for a VSX load or
10245 store operation. */
10246 void
10247 rs6000_emit_le_vsx_permute (rtx dest, rtx source, machine_mode mode)
10249 /* Scalar permutations are easier to express in integer modes rather than
10250 floating-point modes, so cast them here. We use V1TImode instead
10251 of TImode to ensure that the values don't go through GPRs. */
10252 if (FLOAT128_VECTOR_P (mode))
10254 dest = gen_lowpart (V1TImode, dest);
10255 source = gen_lowpart (V1TImode, source);
10256 mode = V1TImode;
10259 /* Use ROTATE instead of VEC_SELECT if the mode contains only a single
10260 scalar. */
10261 if (mode == TImode || mode == V1TImode)
10262 emit_insn (gen_rtx_SET (dest, gen_rtx_ROTATE (mode, source,
10263 GEN_INT (64))));
10264 else
10266 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
10267 emit_insn (gen_rtx_SET (dest, gen_rtx_VEC_SELECT (mode, source, par)));
10271 /* Emit a little-endian load from vector memory location SOURCE to VSX
10272 register DEST in mode MODE. The load is done with two permuting
10273 insn's that represent an lxvd2x and xxpermdi. */
10274 void
10275 rs6000_emit_le_vsx_load (rtx dest, rtx source, machine_mode mode)
10277 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
10278 V1TImode). */
10279 if (mode == TImode || mode == V1TImode)
10281 mode = V2DImode;
10282 dest = gen_lowpart (V2DImode, dest);
10283 source = adjust_address (source, V2DImode, 0);
10286 rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
10287 rs6000_emit_le_vsx_permute (tmp, source, mode);
10288 rs6000_emit_le_vsx_permute (dest, tmp, mode);
10291 /* Emit a little-endian store to vector memory location DEST from VSX
10292 register SOURCE in mode MODE. The store is done with two permuting
10293 insn's that represent an xxpermdi and an stxvd2x. */
10294 void
10295 rs6000_emit_le_vsx_store (rtx dest, rtx source, machine_mode mode)
10297 /* This should never be called during or after LRA, because it does
10298 not re-permute the source register. It is intended only for use
10299 during expand. */
10300 gcc_assert (!lra_in_progress && !reload_completed);
10302 /* Use V2DImode to do swaps of types with 128-bit scalar parts (TImode,
10303 V1TImode). */
10304 if (mode == TImode || mode == V1TImode)
10306 mode = V2DImode;
10307 dest = adjust_address (dest, V2DImode, 0);
10308 source = gen_lowpart (V2DImode, source);
10311 rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
10312 rs6000_emit_le_vsx_permute (tmp, source, mode);
10313 rs6000_emit_le_vsx_permute (dest, tmp, mode);
10316 /* Emit a sequence representing a little-endian VSX load or store,
10317 moving data from SOURCE to DEST in mode MODE. This is done
10318 separately from rs6000_emit_move to ensure it is called only
10319 during expand. LE VSX loads and stores introduced later are
10320 handled with a split. The expand-time RTL generation allows
10321 us to optimize away redundant pairs of register-permutes. */
10322 void
10323 rs6000_emit_le_vsx_move (rtx dest, rtx source, machine_mode mode)
10325 gcc_assert (!BYTES_BIG_ENDIAN
10326 && VECTOR_MEM_VSX_P (mode)
10327 && !TARGET_P9_VECTOR
10328 && !gpr_or_gpr_p (dest, source)
10329 && (MEM_P (source) ^ MEM_P (dest)));
10331 if (MEM_P (source))
10333 gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
10334 rs6000_emit_le_vsx_load (dest, source, mode);
10336 else
10338 if (!REG_P (source))
10339 source = force_reg (mode, source);
10340 rs6000_emit_le_vsx_store (dest, source, mode);
10344 /* Return whether a SFmode or SImode move can be done without converting one
10345 mode to another. This arrises when we have:
10347 (SUBREG:SF (REG:SI ...))
10348 (SUBREG:SI (REG:SF ...))
10350 and one of the values is in a floating point/vector register, where SFmode
10351 scalars are stored in DFmode format. */
10353 bool
10354 valid_sf_si_move (rtx dest, rtx src, machine_mode mode)
10356 if (TARGET_ALLOW_SF_SUBREG)
10357 return true;
10359 if (mode != SFmode && GET_MODE_CLASS (mode) != MODE_INT)
10360 return true;
10362 if (!SUBREG_P (src) || !sf_subreg_operand (src, mode))
10363 return true;
10365 /*. Allow (set (SUBREG:SI (REG:SF)) (SUBREG:SI (REG:SF))). */
10366 if (SUBREG_P (dest))
10368 rtx dest_subreg = SUBREG_REG (dest);
10369 rtx src_subreg = SUBREG_REG (src);
10370 return GET_MODE (dest_subreg) == GET_MODE (src_subreg);
10373 return false;
10377 /* Helper function to change moves with:
10379 (SUBREG:SF (REG:SI)) and
10380 (SUBREG:SI (REG:SF))
10382 into separate UNSPEC insns. In the PowerPC architecture, scalar SFmode
10383 values are stored as DFmode values in the VSX registers. We need to convert
10384 the bits before we can use a direct move or operate on the bits in the
10385 vector register as an integer type.
10387 Skip things like (set (SUBREG:SI (...) (SUBREG:SI (...)). */
10389 static bool
10390 rs6000_emit_move_si_sf_subreg (rtx dest, rtx source, machine_mode mode)
10392 if (TARGET_DIRECT_MOVE_64BIT && !lra_in_progress && !reload_completed
10393 && (!SUBREG_P (dest) || !sf_subreg_operand (dest, mode))
10394 && SUBREG_P (source) && sf_subreg_operand (source, mode))
10396 rtx inner_source = SUBREG_REG (source);
10397 machine_mode inner_mode = GET_MODE (inner_source);
10399 if (mode == SImode && inner_mode == SFmode)
10401 emit_insn (gen_movsi_from_sf (dest, inner_source));
10402 return true;
10405 if (mode == SFmode && inner_mode == SImode)
10407 emit_insn (gen_movsf_from_si (dest, inner_source));
10408 return true;
10412 return false;
10415 /* Emit a move from SOURCE to DEST in mode MODE. */
10416 void
10417 rs6000_emit_move (rtx dest, rtx source, machine_mode mode)
10419 rtx operands[2];
10420 operands[0] = dest;
10421 operands[1] = source;
10423 if (TARGET_DEBUG_ADDR)
10425 fprintf (stderr,
10426 "\nrs6000_emit_move: mode = %s, lra_in_progress = %d, "
10427 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
10428 GET_MODE_NAME (mode),
10429 lra_in_progress,
10430 reload_completed,
10431 can_create_pseudo_p ());
10432 debug_rtx (dest);
10433 fprintf (stderr, "source:\n");
10434 debug_rtx (source);
10437 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
10438 if (CONST_WIDE_INT_P (operands[1])
10439 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10441 /* This should be fixed with the introduction of CONST_WIDE_INT. */
10442 gcc_unreachable ();
10445 /* See if we need to special case SImode/SFmode SUBREG moves. */
10446 if ((mode == SImode || mode == SFmode) && SUBREG_P (source)
10447 && rs6000_emit_move_si_sf_subreg (dest, source, mode))
10448 return;
10450 /* Check if GCC is setting up a block move that will end up using FP
10451 registers as temporaries. We must make sure this is acceptable. */
10452 if (GET_CODE (operands[0]) == MEM
10453 && GET_CODE (operands[1]) == MEM
10454 && mode == DImode
10455 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
10456 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
10457 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
10458 ? 32 : MEM_ALIGN (operands[0])))
10459 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
10460 ? 32
10461 : MEM_ALIGN (operands[1]))))
10462 && ! MEM_VOLATILE_P (operands [0])
10463 && ! MEM_VOLATILE_P (operands [1]))
10465 emit_move_insn (adjust_address (operands[0], SImode, 0),
10466 adjust_address (operands[1], SImode, 0));
10467 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
10468 adjust_address (copy_rtx (operands[1]), SImode, 4));
10469 return;
10472 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
10473 && !gpc_reg_operand (operands[1], mode))
10474 operands[1] = force_reg (mode, operands[1]);
10476 /* Recognize the case where operand[1] is a reference to thread-local
10477 data and load its address to a register. */
10478 if (tls_referenced_p (operands[1]))
10480 enum tls_model model;
10481 rtx tmp = operands[1];
10482 rtx addend = NULL;
10484 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
10486 addend = XEXP (XEXP (tmp, 0), 1);
10487 tmp = XEXP (XEXP (tmp, 0), 0);
10490 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
10491 model = SYMBOL_REF_TLS_MODEL (tmp);
10492 gcc_assert (model != 0);
10494 tmp = rs6000_legitimize_tls_address (tmp, model);
10495 if (addend)
10497 tmp = gen_rtx_PLUS (mode, tmp, addend);
10498 tmp = force_operand (tmp, operands[0]);
10500 operands[1] = tmp;
10503 /* 128-bit constant floating-point values on Darwin should really be loaded
10504 as two parts. However, this premature splitting is a problem when DFmode
10505 values can go into Altivec registers. */
10506 if (FLOAT128_IBM_P (mode) && !reg_addr[DFmode].scalar_in_vmx_p
10507 && GET_CODE (operands[1]) == CONST_DOUBLE)
10509 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
10510 simplify_gen_subreg (DFmode, operands[1], mode, 0),
10511 DFmode);
10512 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
10513 GET_MODE_SIZE (DFmode)),
10514 simplify_gen_subreg (DFmode, operands[1], mode,
10515 GET_MODE_SIZE (DFmode)),
10516 DFmode);
10517 return;
10520 /* Transform (p0:DD, (SUBREG:DD p1:SD)) to ((SUBREG:SD p0:DD),
10521 p1:SD) if p1 is not of floating point class and p0 is spilled as
10522 we can have no analogous movsd_store for this. */
10523 if (lra_in_progress && mode == DDmode
10524 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
10525 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
10526 && GET_CODE (operands[1]) == SUBREG && REG_P (SUBREG_REG (operands[1]))
10527 && GET_MODE (SUBREG_REG (operands[1])) == SDmode)
10529 enum reg_class cl;
10530 int regno = REGNO (SUBREG_REG (operands[1]));
10532 if (regno >= FIRST_PSEUDO_REGISTER)
10534 cl = reg_preferred_class (regno);
10535 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][1];
10537 if (regno >= 0 && ! FP_REGNO_P (regno))
10539 mode = SDmode;
10540 operands[0] = gen_lowpart_SUBREG (SDmode, operands[0]);
10541 operands[1] = SUBREG_REG (operands[1]);
10544 if (lra_in_progress
10545 && mode == SDmode
10546 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
10547 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
10548 && (REG_P (operands[1])
10549 || (GET_CODE (operands[1]) == SUBREG
10550 && REG_P (SUBREG_REG (operands[1])))))
10552 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
10553 ? SUBREG_REG (operands[1]) : operands[1]);
10554 enum reg_class cl;
10556 if (regno >= FIRST_PSEUDO_REGISTER)
10558 cl = reg_preferred_class (regno);
10559 gcc_assert (cl != NO_REGS);
10560 regno = ira_class_hard_regs[cl][0];
10562 if (FP_REGNO_P (regno))
10564 if (GET_MODE (operands[0]) != DDmode)
10565 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
10566 emit_insn (gen_movsd_store (operands[0], operands[1]));
10568 else if (INT_REGNO_P (regno))
10569 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
10570 else
10571 gcc_unreachable();
10572 return;
10574 /* Transform ((SUBREG:DD p0:SD), p1:DD) to (p0:SD, (SUBREG:SD
10575 p:DD)) if p0 is not of floating point class and p1 is spilled as
10576 we can have no analogous movsd_load for this. */
10577 if (lra_in_progress && mode == DDmode
10578 && GET_CODE (operands[0]) == SUBREG && REG_P (SUBREG_REG (operands[0]))
10579 && GET_MODE (SUBREG_REG (operands[0])) == SDmode
10580 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
10581 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
10583 enum reg_class cl;
10584 int regno = REGNO (SUBREG_REG (operands[0]));
10586 if (regno >= FIRST_PSEUDO_REGISTER)
10588 cl = reg_preferred_class (regno);
10589 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][0];
10591 if (regno >= 0 && ! FP_REGNO_P (regno))
10593 mode = SDmode;
10594 operands[0] = SUBREG_REG (operands[0]);
10595 operands[1] = gen_lowpart_SUBREG (SDmode, operands[1]);
10598 if (lra_in_progress
10599 && mode == SDmode
10600 && (REG_P (operands[0])
10601 || (GET_CODE (operands[0]) == SUBREG
10602 && REG_P (SUBREG_REG (operands[0]))))
10603 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
10604 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
10606 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
10607 ? SUBREG_REG (operands[0]) : operands[0]);
10608 enum reg_class cl;
10610 if (regno >= FIRST_PSEUDO_REGISTER)
10612 cl = reg_preferred_class (regno);
10613 gcc_assert (cl != NO_REGS);
10614 regno = ira_class_hard_regs[cl][0];
10616 if (FP_REGNO_P (regno))
10618 if (GET_MODE (operands[1]) != DDmode)
10619 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
10620 emit_insn (gen_movsd_load (operands[0], operands[1]));
10622 else if (INT_REGNO_P (regno))
10623 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
10624 else
10625 gcc_unreachable();
10626 return;
10629 /* FIXME: In the long term, this switch statement should go away
10630 and be replaced by a sequence of tests based on things like
10631 mode == Pmode. */
10632 switch (mode)
10634 case E_HImode:
10635 case E_QImode:
10636 if (CONSTANT_P (operands[1])
10637 && GET_CODE (operands[1]) != CONST_INT)
10638 operands[1] = force_const_mem (mode, operands[1]);
10639 break;
10641 case E_TFmode:
10642 case E_TDmode:
10643 case E_IFmode:
10644 case E_KFmode:
10645 if (FLOAT128_2REG_P (mode))
10646 rs6000_eliminate_indexed_memrefs (operands);
10647 /* fall through */
10649 case E_DFmode:
10650 case E_DDmode:
10651 case E_SFmode:
10652 case E_SDmode:
10653 if (CONSTANT_P (operands[1])
10654 && ! easy_fp_constant (operands[1], mode))
10655 operands[1] = force_const_mem (mode, operands[1]);
10656 break;
10658 case E_V16QImode:
10659 case E_V8HImode:
10660 case E_V4SFmode:
10661 case E_V4SImode:
10662 case E_V2SFmode:
10663 case E_V2SImode:
10664 case E_V2DFmode:
10665 case E_V2DImode:
10666 case E_V1TImode:
10667 if (CONSTANT_P (operands[1])
10668 && !easy_vector_constant (operands[1], mode))
10669 operands[1] = force_const_mem (mode, operands[1]);
10670 break;
10672 case E_SImode:
10673 case E_DImode:
10674 /* Use default pattern for address of ELF small data */
10675 if (TARGET_ELF
10676 && mode == Pmode
10677 && DEFAULT_ABI == ABI_V4
10678 && (GET_CODE (operands[1]) == SYMBOL_REF
10679 || GET_CODE (operands[1]) == CONST)
10680 && small_data_operand (operands[1], mode))
10682 emit_insn (gen_rtx_SET (operands[0], operands[1]));
10683 return;
10686 if (DEFAULT_ABI == ABI_V4
10687 && mode == Pmode && mode == SImode
10688 && flag_pic == 1 && got_operand (operands[1], mode))
10690 emit_insn (gen_movsi_got (operands[0], operands[1]));
10691 return;
10694 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
10695 && TARGET_NO_TOC
10696 && ! flag_pic
10697 && mode == Pmode
10698 && CONSTANT_P (operands[1])
10699 && GET_CODE (operands[1]) != HIGH
10700 && GET_CODE (operands[1]) != CONST_INT)
10702 rtx target = (!can_create_pseudo_p ()
10703 ? operands[0]
10704 : gen_reg_rtx (mode));
10706 /* If this is a function address on -mcall-aixdesc,
10707 convert it to the address of the descriptor. */
10708 if (DEFAULT_ABI == ABI_AIX
10709 && GET_CODE (operands[1]) == SYMBOL_REF
10710 && XSTR (operands[1], 0)[0] == '.')
10712 const char *name = XSTR (operands[1], 0);
10713 rtx new_ref;
10714 while (*name == '.')
10715 name++;
10716 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
10717 CONSTANT_POOL_ADDRESS_P (new_ref)
10718 = CONSTANT_POOL_ADDRESS_P (operands[1]);
10719 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
10720 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
10721 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
10722 operands[1] = new_ref;
10725 if (DEFAULT_ABI == ABI_DARWIN)
10727 #if TARGET_MACHO
10728 if (MACHO_DYNAMIC_NO_PIC_P)
10730 /* Take care of any required data indirection. */
10731 operands[1] = rs6000_machopic_legitimize_pic_address (
10732 operands[1], mode, operands[0]);
10733 if (operands[0] != operands[1])
10734 emit_insn (gen_rtx_SET (operands[0], operands[1]));
10735 return;
10737 #endif
10738 emit_insn (gen_macho_high (target, operands[1]));
10739 emit_insn (gen_macho_low (operands[0], target, operands[1]));
10740 return;
10743 emit_insn (gen_elf_high (target, operands[1]));
10744 emit_insn (gen_elf_low (operands[0], target, operands[1]));
10745 return;
10748 /* If this is a SYMBOL_REF that refers to a constant pool entry,
10749 and we have put it in the TOC, we just need to make a TOC-relative
10750 reference to it. */
10751 if (TARGET_TOC
10752 && GET_CODE (operands[1]) == SYMBOL_REF
10753 && use_toc_relative_ref (operands[1], mode))
10754 operands[1] = create_TOC_reference (operands[1], operands[0]);
10755 else if (mode == Pmode
10756 && CONSTANT_P (operands[1])
10757 && GET_CODE (operands[1]) != HIGH
10758 && ((GET_CODE (operands[1]) != CONST_INT
10759 && ! easy_fp_constant (operands[1], mode))
10760 || (GET_CODE (operands[1]) == CONST_INT
10761 && (num_insns_constant (operands[1], mode)
10762 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
10763 || (GET_CODE (operands[0]) == REG
10764 && FP_REGNO_P (REGNO (operands[0]))))
10765 && !toc_relative_expr_p (operands[1], false, NULL, NULL)
10766 && (TARGET_CMODEL == CMODEL_SMALL
10767 || can_create_pseudo_p ()
10768 || (REG_P (operands[0])
10769 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
10772 #if TARGET_MACHO
10773 /* Darwin uses a special PIC legitimizer. */
10774 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
10776 operands[1] =
10777 rs6000_machopic_legitimize_pic_address (operands[1], mode,
10778 operands[0]);
10779 if (operands[0] != operands[1])
10780 emit_insn (gen_rtx_SET (operands[0], operands[1]));
10781 return;
10783 #endif
10785 /* If we are to limit the number of things we put in the TOC and
10786 this is a symbol plus a constant we can add in one insn,
10787 just put the symbol in the TOC and add the constant. */
10788 if (GET_CODE (operands[1]) == CONST
10789 && TARGET_NO_SUM_IN_TOC
10790 && GET_CODE (XEXP (operands[1], 0)) == PLUS
10791 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
10792 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
10793 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
10794 && ! side_effects_p (operands[0]))
10796 rtx sym =
10797 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
10798 rtx other = XEXP (XEXP (operands[1], 0), 1);
10800 sym = force_reg (mode, sym);
10801 emit_insn (gen_add3_insn (operands[0], sym, other));
10802 return;
10805 operands[1] = force_const_mem (mode, operands[1]);
10807 if (TARGET_TOC
10808 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
10809 && use_toc_relative_ref (XEXP (operands[1], 0), mode))
10811 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
10812 operands[0]);
10813 operands[1] = gen_const_mem (mode, tocref);
10814 set_mem_alias_set (operands[1], get_TOC_alias_set ());
10817 break;
10819 case E_TImode:
10820 if (!VECTOR_MEM_VSX_P (TImode))
10821 rs6000_eliminate_indexed_memrefs (operands);
10822 break;
10824 case E_PTImode:
10825 rs6000_eliminate_indexed_memrefs (operands);
10826 break;
10828 default:
10829 fatal_insn ("bad move", gen_rtx_SET (dest, source));
10832 /* Above, we may have called force_const_mem which may have returned
10833 an invalid address. If we can, fix this up; otherwise, reload will
10834 have to deal with it. */
10835 if (GET_CODE (operands[1]) == MEM)
10836 operands[1] = validize_mem (operands[1]);
10838 emit_insn (gen_rtx_SET (operands[0], operands[1]));
10841 /* Nonzero if we can use a floating-point register to pass this arg. */
10842 #define USE_FP_FOR_ARG_P(CUM,MODE) \
10843 (SCALAR_FLOAT_MODE_NOT_VECTOR_P (MODE) \
10844 && (CUM)->fregno <= FP_ARG_MAX_REG \
10845 && TARGET_HARD_FLOAT)
10847 /* Nonzero if we can use an AltiVec register to pass this arg. */
10848 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
10849 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
10850 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
10851 && TARGET_ALTIVEC_ABI \
10852 && (NAMED))
10854 /* Walk down the type tree of TYPE counting consecutive base elements.
10855 If *MODEP is VOIDmode, then set it to the first valid floating point
10856 or vector type. If a non-floating point or vector type is found, or
10857 if a floating point or vector type that doesn't match a non-VOIDmode
10858 *MODEP is found, then return -1, otherwise return the count in the
10859 sub-tree. */
10861 static int
10862 rs6000_aggregate_candidate (const_tree type, machine_mode *modep)
10864 machine_mode mode;
10865 HOST_WIDE_INT size;
10867 switch (TREE_CODE (type))
10869 case REAL_TYPE:
10870 mode = TYPE_MODE (type);
10871 if (!SCALAR_FLOAT_MODE_P (mode))
10872 return -1;
10874 if (*modep == VOIDmode)
10875 *modep = mode;
10877 if (*modep == mode)
10878 return 1;
10880 break;
10882 case COMPLEX_TYPE:
10883 mode = TYPE_MODE (TREE_TYPE (type));
10884 if (!SCALAR_FLOAT_MODE_P (mode))
10885 return -1;
10887 if (*modep == VOIDmode)
10888 *modep = mode;
10890 if (*modep == mode)
10891 return 2;
10893 break;
10895 case VECTOR_TYPE:
10896 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
10897 return -1;
10899 /* Use V4SImode as representative of all 128-bit vector types. */
10900 size = int_size_in_bytes (type);
10901 switch (size)
10903 case 16:
10904 mode = V4SImode;
10905 break;
10906 default:
10907 return -1;
10910 if (*modep == VOIDmode)
10911 *modep = mode;
10913 /* Vector modes are considered to be opaque: two vectors are
10914 equivalent for the purposes of being homogeneous aggregates
10915 if they are the same size. */
10916 if (*modep == mode)
10917 return 1;
10919 break;
10921 case ARRAY_TYPE:
10923 int count;
10924 tree index = TYPE_DOMAIN (type);
10926 /* Can't handle incomplete types nor sizes that are not
10927 fixed. */
10928 if (!COMPLETE_TYPE_P (type)
10929 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10930 return -1;
10932 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
10933 if (count == -1
10934 || !index
10935 || !TYPE_MAX_VALUE (index)
10936 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
10937 || !TYPE_MIN_VALUE (index)
10938 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
10939 || count < 0)
10940 return -1;
10942 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
10943 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
10945 /* There must be no padding. */
10946 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
10947 return -1;
10949 return count;
10952 case RECORD_TYPE:
10954 int count = 0;
10955 int sub_count;
10956 tree field;
10958 /* Can't handle incomplete types nor sizes that are not
10959 fixed. */
10960 if (!COMPLETE_TYPE_P (type)
10961 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10962 return -1;
10964 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
10966 if (TREE_CODE (field) != FIELD_DECL)
10967 continue;
10969 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
10970 if (sub_count < 0)
10971 return -1;
10972 count += sub_count;
10975 /* There must be no padding. */
10976 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
10977 return -1;
10979 return count;
10982 case UNION_TYPE:
10983 case QUAL_UNION_TYPE:
10985 /* These aren't very interesting except in a degenerate case. */
10986 int count = 0;
10987 int sub_count;
10988 tree field;
10990 /* Can't handle incomplete types nor sizes that are not
10991 fixed. */
10992 if (!COMPLETE_TYPE_P (type)
10993 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10994 return -1;
10996 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
10998 if (TREE_CODE (field) != FIELD_DECL)
10999 continue;
11001 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
11002 if (sub_count < 0)
11003 return -1;
11004 count = count > sub_count ? count : sub_count;
11007 /* There must be no padding. */
11008 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
11009 return -1;
11011 return count;
11014 default:
11015 break;
11018 return -1;
11021 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
11022 float or vector aggregate that shall be passed in FP/vector registers
11023 according to the ELFv2 ABI, return the homogeneous element mode in
11024 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
11026 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
11028 static bool
11029 rs6000_discover_homogeneous_aggregate (machine_mode mode, const_tree type,
11030 machine_mode *elt_mode,
11031 int *n_elts)
11033 /* Note that we do not accept complex types at the top level as
11034 homogeneous aggregates; these types are handled via the
11035 targetm.calls.split_complex_arg mechanism. Complex types
11036 can be elements of homogeneous aggregates, however. */
11037 if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
11039 machine_mode field_mode = VOIDmode;
11040 int field_count = rs6000_aggregate_candidate (type, &field_mode);
11042 if (field_count > 0)
11044 int n_regs = (SCALAR_FLOAT_MODE_P (field_mode) ?
11045 (GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
11047 /* The ELFv2 ABI allows homogeneous aggregates to occupy
11048 up to AGGR_ARG_NUM_REG registers. */
11049 if (field_count * n_regs <= AGGR_ARG_NUM_REG)
11051 if (elt_mode)
11052 *elt_mode = field_mode;
11053 if (n_elts)
11054 *n_elts = field_count;
11055 return true;
11060 if (elt_mode)
11061 *elt_mode = mode;
11062 if (n_elts)
11063 *n_elts = 1;
11064 return false;
11067 /* Return a nonzero value to say to return the function value in
11068 memory, just as large structures are always returned. TYPE will be
11069 the data type of the value, and FNTYPE will be the type of the
11070 function doing the returning, or @code{NULL} for libcalls.
11072 The AIX ABI for the RS/6000 specifies that all structures are
11073 returned in memory. The Darwin ABI does the same.
11075 For the Darwin 64 Bit ABI, a function result can be returned in
11076 registers or in memory, depending on the size of the return data
11077 type. If it is returned in registers, the value occupies the same
11078 registers as it would if it were the first and only function
11079 argument. Otherwise, the function places its result in memory at
11080 the location pointed to by GPR3.
11082 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
11083 but a draft put them in memory, and GCC used to implement the draft
11084 instead of the final standard. Therefore, aix_struct_return
11085 controls this instead of DEFAULT_ABI; V.4 targets needing backward
11086 compatibility can change DRAFT_V4_STRUCT_RET to override the
11087 default, and -m switches get the final word. See
11088 rs6000_option_override_internal for more details.
11090 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
11091 long double support is enabled. These values are returned in memory.
11093 int_size_in_bytes returns -1 for variable size objects, which go in
11094 memory always. The cast to unsigned makes -1 > 8. */
11096 static bool
11097 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
11099 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
11100 if (TARGET_MACHO
11101 && rs6000_darwin64_abi
11102 && TREE_CODE (type) == RECORD_TYPE
11103 && int_size_in_bytes (type) > 0)
11105 CUMULATIVE_ARGS valcum;
11106 rtx valret;
11108 valcum.words = 0;
11109 valcum.fregno = FP_ARG_MIN_REG;
11110 valcum.vregno = ALTIVEC_ARG_MIN_REG;
11111 /* Do a trial code generation as if this were going to be passed
11112 as an argument; if any part goes in memory, we return NULL. */
11113 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
11114 if (valret)
11115 return false;
11116 /* Otherwise fall through to more conventional ABI rules. */
11119 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
11120 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
11121 NULL, NULL))
11122 return false;
11124 /* The ELFv2 ABI returns aggregates up to 16B in registers */
11125 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
11126 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
11127 return false;
11129 if (AGGREGATE_TYPE_P (type)
11130 && (aix_struct_return
11131 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
11132 return true;
11134 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
11135 modes only exist for GCC vector types if -maltivec. */
11136 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
11137 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11138 return false;
11140 /* Return synthetic vectors in memory. */
11141 if (TREE_CODE (type) == VECTOR_TYPE
11142 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
11144 static bool warned_for_return_big_vectors = false;
11145 if (!warned_for_return_big_vectors)
11147 warning (OPT_Wpsabi, "GCC vector returned by reference: "
11148 "non-standard ABI extension with no compatibility "
11149 "guarantee");
11150 warned_for_return_big_vectors = true;
11152 return true;
11155 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
11156 && FLOAT128_IEEE_P (TYPE_MODE (type)))
11157 return true;
11159 return false;
11162 /* Specify whether values returned in registers should be at the most
11163 significant end of a register. We want aggregates returned by
11164 value to match the way aggregates are passed to functions. */
11166 static bool
11167 rs6000_return_in_msb (const_tree valtype)
11169 return (DEFAULT_ABI == ABI_ELFv2
11170 && BYTES_BIG_ENDIAN
11171 && AGGREGATE_TYPE_P (valtype)
11172 && FUNCTION_ARG_PADDING (TYPE_MODE (valtype), valtype) == upward);
11175 #ifdef HAVE_AS_GNU_ATTRIBUTE
11176 /* Return TRUE if a call to function FNDECL may be one that
11177 potentially affects the function calling ABI of the object file. */
11179 static bool
11180 call_ABI_of_interest (tree fndecl)
11182 if (rs6000_gnu_attr && symtab->state == EXPANSION)
11184 struct cgraph_node *c_node;
11186 /* Libcalls are always interesting. */
11187 if (fndecl == NULL_TREE)
11188 return true;
11190 /* Any call to an external function is interesting. */
11191 if (DECL_EXTERNAL (fndecl))
11192 return true;
11194 /* Interesting functions that we are emitting in this object file. */
11195 c_node = cgraph_node::get (fndecl);
11196 c_node = c_node->ultimate_alias_target ();
11197 return !c_node->only_called_directly_p ();
11199 return false;
11201 #endif
11203 /* Initialize a variable CUM of type CUMULATIVE_ARGS
11204 for a call to a function whose data type is FNTYPE.
11205 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
11207 For incoming args we set the number of arguments in the prototype large
11208 so we never return a PARALLEL. */
11210 void
11211 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
11212 rtx libname ATTRIBUTE_UNUSED, int incoming,
11213 int libcall, int n_named_args,
11214 tree fndecl ATTRIBUTE_UNUSED,
11215 machine_mode return_mode ATTRIBUTE_UNUSED)
11217 static CUMULATIVE_ARGS zero_cumulative;
11219 *cum = zero_cumulative;
11220 cum->words = 0;
11221 cum->fregno = FP_ARG_MIN_REG;
11222 cum->vregno = ALTIVEC_ARG_MIN_REG;
11223 cum->prototype = (fntype && prototype_p (fntype));
11224 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
11225 ? CALL_LIBCALL : CALL_NORMAL);
11226 cum->sysv_gregno = GP_ARG_MIN_REG;
11227 cum->stdarg = stdarg_p (fntype);
11228 cum->libcall = libcall;
11230 cum->nargs_prototype = 0;
11231 if (incoming || cum->prototype)
11232 cum->nargs_prototype = n_named_args;
11234 /* Check for a longcall attribute. */
11235 if ((!fntype && rs6000_default_long_calls)
11236 || (fntype
11237 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
11238 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
11239 cum->call_cookie |= CALL_LONG;
11241 if (TARGET_DEBUG_ARG)
11243 fprintf (stderr, "\ninit_cumulative_args:");
11244 if (fntype)
11246 tree ret_type = TREE_TYPE (fntype);
11247 fprintf (stderr, " ret code = %s,",
11248 get_tree_code_name (TREE_CODE (ret_type)));
11251 if (cum->call_cookie & CALL_LONG)
11252 fprintf (stderr, " longcall,");
11254 fprintf (stderr, " proto = %d, nargs = %d\n",
11255 cum->prototype, cum->nargs_prototype);
11258 #ifdef HAVE_AS_GNU_ATTRIBUTE
11259 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4))
11261 cum->escapes = call_ABI_of_interest (fndecl);
11262 if (cum->escapes)
11264 tree return_type;
11266 if (fntype)
11268 return_type = TREE_TYPE (fntype);
11269 return_mode = TYPE_MODE (return_type);
11271 else
11272 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
11274 if (return_type != NULL)
11276 if (TREE_CODE (return_type) == RECORD_TYPE
11277 && TYPE_TRANSPARENT_AGGR (return_type))
11279 return_type = TREE_TYPE (first_field (return_type));
11280 return_mode = TYPE_MODE (return_type);
11282 if (AGGREGATE_TYPE_P (return_type)
11283 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
11284 <= 8))
11285 rs6000_returns_struct = true;
11287 if (SCALAR_FLOAT_MODE_P (return_mode))
11289 rs6000_passes_float = true;
11290 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
11291 && (FLOAT128_IBM_P (return_mode)
11292 || FLOAT128_IEEE_P (return_mode)
11293 || (return_type != NULL
11294 && (TYPE_MAIN_VARIANT (return_type)
11295 == long_double_type_node))))
11296 rs6000_passes_long_double = true;
11298 if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
11299 || PAIRED_VECTOR_MODE (return_mode))
11300 rs6000_passes_vector = true;
11303 #endif
11305 if (fntype
11306 && !TARGET_ALTIVEC
11307 && TARGET_ALTIVEC_ABI
11308 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
11310 error ("cannot return value in vector register because"
11311 " altivec instructions are disabled, use %qs"
11312 " to enable them", "-maltivec");
11316 /* The mode the ABI uses for a word. This is not the same as word_mode
11317 for -m32 -mpowerpc64. This is used to implement various target hooks. */
11319 static machine_mode
11320 rs6000_abi_word_mode (void)
11322 return TARGET_32BIT ? SImode : DImode;
11325 /* Implement the TARGET_OFFLOAD_OPTIONS hook. */
11326 static char *
11327 rs6000_offload_options (void)
11329 if (TARGET_64BIT)
11330 return xstrdup ("-foffload-abi=lp64");
11331 else
11332 return xstrdup ("-foffload-abi=ilp32");
11335 /* On rs6000, function arguments are promoted, as are function return
11336 values. */
11338 static machine_mode
11339 rs6000_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
11340 machine_mode mode,
11341 int *punsignedp ATTRIBUTE_UNUSED,
11342 const_tree, int)
11344 PROMOTE_MODE (mode, *punsignedp, type);
11346 return mode;
11349 /* Return true if TYPE must be passed on the stack and not in registers. */
11351 static bool
11352 rs6000_must_pass_in_stack (machine_mode mode, const_tree type)
11354 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
11355 return must_pass_in_stack_var_size (mode, type);
11356 else
11357 return must_pass_in_stack_var_size_or_pad (mode, type);
11360 static inline bool
11361 is_complex_IBM_long_double (machine_mode mode)
11363 return mode == ICmode || (!TARGET_IEEEQUAD && mode == TCmode);
11366 /* Whether ABI_V4 passes MODE args to a function in floating point
11367 registers. */
11369 static bool
11370 abi_v4_pass_in_fpr (machine_mode mode)
11372 if (!TARGET_HARD_FLOAT)
11373 return false;
11374 if (TARGET_SINGLE_FLOAT && mode == SFmode)
11375 return true;
11376 if (TARGET_DOUBLE_FLOAT && mode == DFmode)
11377 return true;
11378 /* ABI_V4 passes complex IBM long double in 8 gprs.
11379 Stupid, but we can't change the ABI now. */
11380 if (is_complex_IBM_long_double (mode))
11381 return false;
11382 if (FLOAT128_2REG_P (mode))
11383 return true;
11384 if (DECIMAL_FLOAT_MODE_P (mode))
11385 return true;
11386 return false;
11389 /* If defined, a C expression which determines whether, and in which
11390 direction, to pad out an argument with extra space. The value
11391 should be of type `enum direction': either `upward' to pad above
11392 the argument, `downward' to pad below, or `none' to inhibit
11393 padding.
11395 For the AIX ABI structs are always stored left shifted in their
11396 argument slot. */
11398 enum direction
11399 function_arg_padding (machine_mode mode, const_tree type)
11401 #ifndef AGGREGATE_PADDING_FIXED
11402 #define AGGREGATE_PADDING_FIXED 0
11403 #endif
11404 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
11405 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
11406 #endif
11408 if (!AGGREGATE_PADDING_FIXED)
11410 /* GCC used to pass structures of the same size as integer types as
11411 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
11412 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
11413 passed padded downward, except that -mstrict-align further
11414 muddied the water in that multi-component structures of 2 and 4
11415 bytes in size were passed padded upward.
11417 The following arranges for best compatibility with previous
11418 versions of gcc, but removes the -mstrict-align dependency. */
11419 if (BYTES_BIG_ENDIAN)
11421 HOST_WIDE_INT size = 0;
11423 if (mode == BLKmode)
11425 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11426 size = int_size_in_bytes (type);
11428 else
11429 size = GET_MODE_SIZE (mode);
11431 if (size == 1 || size == 2 || size == 4)
11432 return downward;
11434 return upward;
11437 if (AGGREGATES_PAD_UPWARD_ALWAYS)
11439 if (type != 0 && AGGREGATE_TYPE_P (type))
11440 return upward;
11443 /* Fall back to the default. */
11444 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
11447 /* If defined, a C expression that gives the alignment boundary, in bits,
11448 of an argument with the specified mode and type. If it is not defined,
11449 PARM_BOUNDARY is used for all arguments.
11451 V.4 wants long longs and doubles to be double word aligned. Just
11452 testing the mode size is a boneheaded way to do this as it means
11453 that other types such as complex int are also double word aligned.
11454 However, we're stuck with this because changing the ABI might break
11455 existing library interfaces.
11457 Quadword align Altivec/VSX vectors.
11458 Quadword align large synthetic vector types. */
11460 static unsigned int
11461 rs6000_function_arg_boundary (machine_mode mode, const_tree type)
11463 machine_mode elt_mode;
11464 int n_elts;
11466 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
11468 if (DEFAULT_ABI == ABI_V4
11469 && (GET_MODE_SIZE (mode) == 8
11470 || (TARGET_HARD_FLOAT
11471 && !is_complex_IBM_long_double (mode)
11472 && FLOAT128_2REG_P (mode))))
11473 return 64;
11474 else if (FLOAT128_VECTOR_P (mode))
11475 return 128;
11476 else if (PAIRED_VECTOR_MODE (mode)
11477 || (type && TREE_CODE (type) == VECTOR_TYPE
11478 && int_size_in_bytes (type) >= 8
11479 && int_size_in_bytes (type) < 16))
11480 return 64;
11481 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
11482 || (type && TREE_CODE (type) == VECTOR_TYPE
11483 && int_size_in_bytes (type) >= 16))
11484 return 128;
11486 /* Aggregate types that need > 8 byte alignment are quadword-aligned
11487 in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
11488 -mcompat-align-parm is used. */
11489 if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
11490 || DEFAULT_ABI == ABI_ELFv2)
11491 && type && TYPE_ALIGN (type) > 64)
11493 /* "Aggregate" means any AGGREGATE_TYPE except for single-element
11494 or homogeneous float/vector aggregates here. We already handled
11495 vector aggregates above, but still need to check for float here. */
11496 bool aggregate_p = (AGGREGATE_TYPE_P (type)
11497 && !SCALAR_FLOAT_MODE_P (elt_mode));
11499 /* We used to check for BLKmode instead of the above aggregate type
11500 check. Warn when this results in any difference to the ABI. */
11501 if (aggregate_p != (mode == BLKmode))
11503 static bool warned;
11504 if (!warned && warn_psabi)
11506 warned = true;
11507 inform (input_location,
11508 "the ABI of passing aggregates with %d-byte alignment"
11509 " has changed in GCC 5",
11510 (int) TYPE_ALIGN (type) / BITS_PER_UNIT);
11514 if (aggregate_p)
11515 return 128;
11518 /* Similar for the Darwin64 ABI. Note that for historical reasons we
11519 implement the "aggregate type" check as a BLKmode check here; this
11520 means certain aggregate types are in fact not aligned. */
11521 if (TARGET_MACHO && rs6000_darwin64_abi
11522 && mode == BLKmode
11523 && type && TYPE_ALIGN (type) > 64)
11524 return 128;
11526 return PARM_BOUNDARY;
11529 /* The offset in words to the start of the parameter save area. */
11531 static unsigned int
11532 rs6000_parm_offset (void)
11534 return (DEFAULT_ABI == ABI_V4 ? 2
11535 : DEFAULT_ABI == ABI_ELFv2 ? 4
11536 : 6);
11539 /* For a function parm of MODE and TYPE, return the starting word in
11540 the parameter area. NWORDS of the parameter area are already used. */
11542 static unsigned int
11543 rs6000_parm_start (machine_mode mode, const_tree type,
11544 unsigned int nwords)
11546 unsigned int align;
11548 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
11549 return nwords + (-(rs6000_parm_offset () + nwords) & align);
11552 /* Compute the size (in words) of a function argument. */
11554 static unsigned long
11555 rs6000_arg_size (machine_mode mode, const_tree type)
11557 unsigned long size;
11559 if (mode != BLKmode)
11560 size = GET_MODE_SIZE (mode);
11561 else
11562 size = int_size_in_bytes (type);
11564 if (TARGET_32BIT)
11565 return (size + 3) >> 2;
11566 else
11567 return (size + 7) >> 3;
11570 /* Use this to flush pending int fields. */
11572 static void
11573 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
11574 HOST_WIDE_INT bitpos, int final)
11576 unsigned int startbit, endbit;
11577 int intregs, intoffset;
11578 machine_mode mode;
11580 /* Handle the situations where a float is taking up the first half
11581 of the GPR, and the other half is empty (typically due to
11582 alignment restrictions). We can detect this by a 8-byte-aligned
11583 int field, or by seeing that this is the final flush for this
11584 argument. Count the word and continue on. */
11585 if (cum->floats_in_gpr == 1
11586 && (cum->intoffset % 64 == 0
11587 || (cum->intoffset == -1 && final)))
11589 cum->words++;
11590 cum->floats_in_gpr = 0;
11593 if (cum->intoffset == -1)
11594 return;
11596 intoffset = cum->intoffset;
11597 cum->intoffset = -1;
11598 cum->floats_in_gpr = 0;
11600 if (intoffset % BITS_PER_WORD != 0)
11602 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
11603 MODE_INT, 0);
11604 if (mode == BLKmode)
11606 /* We couldn't find an appropriate mode, which happens,
11607 e.g., in packed structs when there are 3 bytes to load.
11608 Back intoffset back to the beginning of the word in this
11609 case. */
11610 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
11614 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
11615 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
11616 intregs = (endbit - startbit) / BITS_PER_WORD;
11617 cum->words += intregs;
11618 /* words should be unsigned. */
11619 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
11621 int pad = (endbit/BITS_PER_WORD) - cum->words;
11622 cum->words += pad;
11626 /* The darwin64 ABI calls for us to recurse down through structs,
11627 looking for elements passed in registers. Unfortunately, we have
11628 to track int register count here also because of misalignments
11629 in powerpc alignment mode. */
11631 static void
11632 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
11633 const_tree type,
11634 HOST_WIDE_INT startbitpos)
11636 tree f;
11638 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
11639 if (TREE_CODE (f) == FIELD_DECL)
11641 HOST_WIDE_INT bitpos = startbitpos;
11642 tree ftype = TREE_TYPE (f);
11643 machine_mode mode;
11644 if (ftype == error_mark_node)
11645 continue;
11646 mode = TYPE_MODE (ftype);
11648 if (DECL_SIZE (f) != 0
11649 && tree_fits_uhwi_p (bit_position (f)))
11650 bitpos += int_bit_position (f);
11652 /* ??? FIXME: else assume zero offset. */
11654 if (TREE_CODE (ftype) == RECORD_TYPE)
11655 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
11656 else if (USE_FP_FOR_ARG_P (cum, mode))
11658 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
11659 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
11660 cum->fregno += n_fpregs;
11661 /* Single-precision floats present a special problem for
11662 us, because they are smaller than an 8-byte GPR, and so
11663 the structure-packing rules combined with the standard
11664 varargs behavior mean that we want to pack float/float
11665 and float/int combinations into a single register's
11666 space. This is complicated by the arg advance flushing,
11667 which works on arbitrarily large groups of int-type
11668 fields. */
11669 if (mode == SFmode)
11671 if (cum->floats_in_gpr == 1)
11673 /* Two floats in a word; count the word and reset
11674 the float count. */
11675 cum->words++;
11676 cum->floats_in_gpr = 0;
11678 else if (bitpos % 64 == 0)
11680 /* A float at the beginning of an 8-byte word;
11681 count it and put off adjusting cum->words until
11682 we see if a arg advance flush is going to do it
11683 for us. */
11684 cum->floats_in_gpr++;
11686 else
11688 /* The float is at the end of a word, preceded
11689 by integer fields, so the arg advance flush
11690 just above has already set cum->words and
11691 everything is taken care of. */
11694 else
11695 cum->words += n_fpregs;
11697 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
11699 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
11700 cum->vregno++;
11701 cum->words += 2;
11703 else if (cum->intoffset == -1)
11704 cum->intoffset = bitpos;
11708 /* Check for an item that needs to be considered specially under the darwin 64
11709 bit ABI. These are record types where the mode is BLK or the structure is
11710 8 bytes in size. */
11711 static int
11712 rs6000_darwin64_struct_check_p (machine_mode mode, const_tree type)
11714 return rs6000_darwin64_abi
11715 && ((mode == BLKmode
11716 && TREE_CODE (type) == RECORD_TYPE
11717 && int_size_in_bytes (type) > 0)
11718 || (type && TREE_CODE (type) == RECORD_TYPE
11719 && int_size_in_bytes (type) == 8)) ? 1 : 0;
11722 /* Update the data in CUM to advance over an argument
11723 of mode MODE and data type TYPE.
11724 (TYPE is null for libcalls where that information may not be available.)
11726 Note that for args passed by reference, function_arg will be called
11727 with MODE and TYPE set to that of the pointer to the arg, not the arg
11728 itself. */
11730 static void
11731 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, machine_mode mode,
11732 const_tree type, bool named, int depth)
11734 machine_mode elt_mode;
11735 int n_elts;
11737 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
11739 /* Only tick off an argument if we're not recursing. */
11740 if (depth == 0)
11741 cum->nargs_prototype--;
11743 #ifdef HAVE_AS_GNU_ATTRIBUTE
11744 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4)
11745 && cum->escapes)
11747 if (SCALAR_FLOAT_MODE_P (mode))
11749 rs6000_passes_float = true;
11750 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
11751 && (FLOAT128_IBM_P (mode)
11752 || FLOAT128_IEEE_P (mode)
11753 || (type != NULL
11754 && TYPE_MAIN_VARIANT (type) == long_double_type_node)))
11755 rs6000_passes_long_double = true;
11757 if ((named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
11758 || (PAIRED_VECTOR_MODE (mode)
11759 && !cum->stdarg
11760 && cum->sysv_gregno <= GP_ARG_MAX_REG))
11761 rs6000_passes_vector = true;
11763 #endif
11765 if (TARGET_ALTIVEC_ABI
11766 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
11767 || (type && TREE_CODE (type) == VECTOR_TYPE
11768 && int_size_in_bytes (type) == 16)))
11770 bool stack = false;
11772 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
11774 cum->vregno += n_elts;
11776 if (!TARGET_ALTIVEC)
11777 error ("cannot pass argument in vector register because"
11778 " altivec instructions are disabled, use %qs"
11779 " to enable them", "-maltivec");
11781 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
11782 even if it is going to be passed in a vector register.
11783 Darwin does the same for variable-argument functions. */
11784 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
11785 && TARGET_64BIT)
11786 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
11787 stack = true;
11789 else
11790 stack = true;
11792 if (stack)
11794 int align;
11796 /* Vector parameters must be 16-byte aligned. In 32-bit
11797 mode this means we need to take into account the offset
11798 to the parameter save area. In 64-bit mode, they just
11799 have to start on an even word, since the parameter save
11800 area is 16-byte aligned. */
11801 if (TARGET_32BIT)
11802 align = -(rs6000_parm_offset () + cum->words) & 3;
11803 else
11804 align = cum->words & 1;
11805 cum->words += align + rs6000_arg_size (mode, type);
11807 if (TARGET_DEBUG_ARG)
11809 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
11810 cum->words, align);
11811 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
11812 cum->nargs_prototype, cum->prototype,
11813 GET_MODE_NAME (mode));
11817 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
11819 int size = int_size_in_bytes (type);
11820 /* Variable sized types have size == -1 and are
11821 treated as if consisting entirely of ints.
11822 Pad to 16 byte boundary if needed. */
11823 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
11824 && (cum->words % 2) != 0)
11825 cum->words++;
11826 /* For varargs, we can just go up by the size of the struct. */
11827 if (!named)
11828 cum->words += (size + 7) / 8;
11829 else
11831 /* It is tempting to say int register count just goes up by
11832 sizeof(type)/8, but this is wrong in a case such as
11833 { int; double; int; } [powerpc alignment]. We have to
11834 grovel through the fields for these too. */
11835 cum->intoffset = 0;
11836 cum->floats_in_gpr = 0;
11837 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
11838 rs6000_darwin64_record_arg_advance_flush (cum,
11839 size * BITS_PER_UNIT, 1);
11841 if (TARGET_DEBUG_ARG)
11843 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
11844 cum->words, TYPE_ALIGN (type), size);
11845 fprintf (stderr,
11846 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
11847 cum->nargs_prototype, cum->prototype,
11848 GET_MODE_NAME (mode));
11851 else if (DEFAULT_ABI == ABI_V4)
11853 if (abi_v4_pass_in_fpr (mode))
11855 /* _Decimal128 must use an even/odd register pair. This assumes
11856 that the register number is odd when fregno is odd. */
11857 if (mode == TDmode && (cum->fregno % 2) == 1)
11858 cum->fregno++;
11860 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
11861 <= FP_ARG_V4_MAX_REG)
11862 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
11863 else
11865 cum->fregno = FP_ARG_V4_MAX_REG + 1;
11866 if (mode == DFmode || FLOAT128_IBM_P (mode)
11867 || mode == DDmode || mode == TDmode)
11868 cum->words += cum->words & 1;
11869 cum->words += rs6000_arg_size (mode, type);
11872 else
11874 int n_words = rs6000_arg_size (mode, type);
11875 int gregno = cum->sysv_gregno;
11877 /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
11878 As does any other 2 word item such as complex int due to a
11879 historical mistake. */
11880 if (n_words == 2)
11881 gregno += (1 - gregno) & 1;
11883 /* Multi-reg args are not split between registers and stack. */
11884 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
11886 /* Long long is aligned on the stack. So are other 2 word
11887 items such as complex int due to a historical mistake. */
11888 if (n_words == 2)
11889 cum->words += cum->words & 1;
11890 cum->words += n_words;
11893 /* Note: continuing to accumulate gregno past when we've started
11894 spilling to the stack indicates the fact that we've started
11895 spilling to the stack to expand_builtin_saveregs. */
11896 cum->sysv_gregno = gregno + n_words;
11899 if (TARGET_DEBUG_ARG)
11901 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
11902 cum->words, cum->fregno);
11903 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
11904 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
11905 fprintf (stderr, "mode = %4s, named = %d\n",
11906 GET_MODE_NAME (mode), named);
11909 else
11911 int n_words = rs6000_arg_size (mode, type);
11912 int start_words = cum->words;
11913 int align_words = rs6000_parm_start (mode, type, start_words);
11915 cum->words = align_words + n_words;
11917 if (SCALAR_FLOAT_MODE_P (elt_mode) && TARGET_HARD_FLOAT)
11919 /* _Decimal128 must be passed in an even/odd float register pair.
11920 This assumes that the register number is odd when fregno is
11921 odd. */
11922 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
11923 cum->fregno++;
11924 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
11927 if (TARGET_DEBUG_ARG)
11929 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
11930 cum->words, cum->fregno);
11931 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
11932 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
11933 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
11934 named, align_words - start_words, depth);
11939 static void
11940 rs6000_function_arg_advance (cumulative_args_t cum, machine_mode mode,
11941 const_tree type, bool named)
11943 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
11947 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
11948 structure between cum->intoffset and bitpos to integer registers. */
11950 static void
11951 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
11952 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
11954 machine_mode mode;
11955 unsigned int regno;
11956 unsigned int startbit, endbit;
11957 int this_regno, intregs, intoffset;
11958 rtx reg;
11960 if (cum->intoffset == -1)
11961 return;
11963 intoffset = cum->intoffset;
11964 cum->intoffset = -1;
11966 /* If this is the trailing part of a word, try to only load that
11967 much into the register. Otherwise load the whole register. Note
11968 that in the latter case we may pick up unwanted bits. It's not a
11969 problem at the moment but may wish to revisit. */
11971 if (intoffset % BITS_PER_WORD != 0)
11973 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
11974 MODE_INT, 0);
11975 if (mode == BLKmode)
11977 /* We couldn't find an appropriate mode, which happens,
11978 e.g., in packed structs when there are 3 bytes to load.
11979 Back intoffset back to the beginning of the word in this
11980 case. */
11981 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
11982 mode = word_mode;
11985 else
11986 mode = word_mode;
11988 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
11989 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
11990 intregs = (endbit - startbit) / BITS_PER_WORD;
11991 this_regno = cum->words + intoffset / BITS_PER_WORD;
11993 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
11994 cum->use_stack = 1;
11996 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
11997 if (intregs <= 0)
11998 return;
12000 intoffset /= BITS_PER_UNIT;
12003 regno = GP_ARG_MIN_REG + this_regno;
12004 reg = gen_rtx_REG (mode, regno);
12005 rvec[(*k)++] =
12006 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
12008 this_regno += 1;
12009 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
12010 mode = word_mode;
12011 intregs -= 1;
12013 while (intregs > 0);
12016 /* Recursive workhorse for the following. */
12018 static void
12019 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
12020 HOST_WIDE_INT startbitpos, rtx rvec[],
12021 int *k)
12023 tree f;
12025 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
12026 if (TREE_CODE (f) == FIELD_DECL)
12028 HOST_WIDE_INT bitpos = startbitpos;
12029 tree ftype = TREE_TYPE (f);
12030 machine_mode mode;
12031 if (ftype == error_mark_node)
12032 continue;
12033 mode = TYPE_MODE (ftype);
12035 if (DECL_SIZE (f) != 0
12036 && tree_fits_uhwi_p (bit_position (f)))
12037 bitpos += int_bit_position (f);
12039 /* ??? FIXME: else assume zero offset. */
12041 if (TREE_CODE (ftype) == RECORD_TYPE)
12042 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
12043 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
12045 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
12046 #if 0
12047 switch (mode)
12049 case E_SCmode: mode = SFmode; break;
12050 case E_DCmode: mode = DFmode; break;
12051 case E_TCmode: mode = TFmode; break;
12052 default: break;
12054 #endif
12055 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
12056 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
12058 gcc_assert (cum->fregno == FP_ARG_MAX_REG
12059 && (mode == TFmode || mode == TDmode));
12060 /* Long double or _Decimal128 split over regs and memory. */
12061 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
12062 cum->use_stack=1;
12064 rvec[(*k)++]
12065 = gen_rtx_EXPR_LIST (VOIDmode,
12066 gen_rtx_REG (mode, cum->fregno++),
12067 GEN_INT (bitpos / BITS_PER_UNIT));
12068 if (FLOAT128_2REG_P (mode))
12069 cum->fregno++;
12071 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
12073 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
12074 rvec[(*k)++]
12075 = gen_rtx_EXPR_LIST (VOIDmode,
12076 gen_rtx_REG (mode, cum->vregno++),
12077 GEN_INT (bitpos / BITS_PER_UNIT));
12079 else if (cum->intoffset == -1)
12080 cum->intoffset = bitpos;
12084 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
12085 the register(s) to be used for each field and subfield of a struct
12086 being passed by value, along with the offset of where the
12087 register's value may be found in the block. FP fields go in FP
12088 register, vector fields go in vector registers, and everything
12089 else goes in int registers, packed as in memory.
12091 This code is also used for function return values. RETVAL indicates
12092 whether this is the case.
12094 Much of this is taken from the SPARC V9 port, which has a similar
12095 calling convention. */
12097 static rtx
12098 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
12099 bool named, bool retval)
12101 rtx rvec[FIRST_PSEUDO_REGISTER];
12102 int k = 1, kbase = 1;
12103 HOST_WIDE_INT typesize = int_size_in_bytes (type);
12104 /* This is a copy; modifications are not visible to our caller. */
12105 CUMULATIVE_ARGS copy_cum = *orig_cum;
12106 CUMULATIVE_ARGS *cum = &copy_cum;
12108 /* Pad to 16 byte boundary if needed. */
12109 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
12110 && (cum->words % 2) != 0)
12111 cum->words++;
12113 cum->intoffset = 0;
12114 cum->use_stack = 0;
12115 cum->named = named;
12117 /* Put entries into rvec[] for individual FP and vector fields, and
12118 for the chunks of memory that go in int regs. Note we start at
12119 element 1; 0 is reserved for an indication of using memory, and
12120 may or may not be filled in below. */
12121 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
12122 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
12124 /* If any part of the struct went on the stack put all of it there.
12125 This hack is because the generic code for
12126 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
12127 parts of the struct are not at the beginning. */
12128 if (cum->use_stack)
12130 if (retval)
12131 return NULL_RTX; /* doesn't go in registers at all */
12132 kbase = 0;
12133 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12135 if (k > 1 || cum->use_stack)
12136 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
12137 else
12138 return NULL_RTX;
12141 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
12143 static rtx
12144 rs6000_mixed_function_arg (machine_mode mode, const_tree type,
12145 int align_words)
12147 int n_units;
12148 int i, k;
12149 rtx rvec[GP_ARG_NUM_REG + 1];
12151 if (align_words >= GP_ARG_NUM_REG)
12152 return NULL_RTX;
12154 n_units = rs6000_arg_size (mode, type);
12156 /* Optimize the simple case where the arg fits in one gpr, except in
12157 the case of BLKmode due to assign_parms assuming that registers are
12158 BITS_PER_WORD wide. */
12159 if (n_units == 0
12160 || (n_units == 1 && mode != BLKmode))
12161 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
12163 k = 0;
12164 if (align_words + n_units > GP_ARG_NUM_REG)
12165 /* Not all of the arg fits in gprs. Say that it goes in memory too,
12166 using a magic NULL_RTX component.
12167 This is not strictly correct. Only some of the arg belongs in
12168 memory, not all of it. However, the normal scheme using
12169 function_arg_partial_nregs can result in unusual subregs, eg.
12170 (subreg:SI (reg:DF) 4), which are not handled well. The code to
12171 store the whole arg to memory is often more efficient than code
12172 to store pieces, and we know that space is available in the right
12173 place for the whole arg. */
12174 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12176 i = 0;
12179 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
12180 rtx off = GEN_INT (i++ * 4);
12181 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12183 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
12185 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
12188 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
12189 but must also be copied into the parameter save area starting at
12190 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
12191 to the GPRs and/or memory. Return the number of elements used. */
12193 static int
12194 rs6000_psave_function_arg (machine_mode mode, const_tree type,
12195 int align_words, rtx *rvec)
12197 int k = 0;
12199 if (align_words < GP_ARG_NUM_REG)
12201 int n_words = rs6000_arg_size (mode, type);
12203 if (align_words + n_words > GP_ARG_NUM_REG
12204 || mode == BLKmode
12205 || (TARGET_32BIT && TARGET_POWERPC64))
12207 /* If this is partially on the stack, then we only
12208 include the portion actually in registers here. */
12209 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
12210 int i = 0;
12212 if (align_words + n_words > GP_ARG_NUM_REG)
12214 /* Not all of the arg fits in gprs. Say that it goes in memory
12215 too, using a magic NULL_RTX component. Also see comment in
12216 rs6000_mixed_function_arg for why the normal
12217 function_arg_partial_nregs scheme doesn't work in this case. */
12218 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12223 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
12224 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
12225 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12227 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
12229 else
12231 /* The whole arg fits in gprs. */
12232 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
12233 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
12236 else
12238 /* It's entirely in memory. */
12239 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
12242 return k;
12245 /* RVEC is a vector of K components of an argument of mode MODE.
12246 Construct the final function_arg return value from it. */
12248 static rtx
12249 rs6000_finish_function_arg (machine_mode mode, rtx *rvec, int k)
12251 gcc_assert (k >= 1);
12253 /* Avoid returning a PARALLEL in the trivial cases. */
12254 if (k == 1)
12256 if (XEXP (rvec[0], 0) == NULL_RTX)
12257 return NULL_RTX;
12259 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
12260 return XEXP (rvec[0], 0);
12263 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
12266 /* Determine where to put an argument to a function.
12267 Value is zero to push the argument on the stack,
12268 or a hard register in which to store the argument.
12270 MODE is the argument's machine mode.
12271 TYPE is the data type of the argument (as a tree).
12272 This is null for libcalls where that information may
12273 not be available.
12274 CUM is a variable of type CUMULATIVE_ARGS which gives info about
12275 the preceding args and about the function being called. It is
12276 not modified in this routine.
12277 NAMED is nonzero if this argument is a named parameter
12278 (otherwise it is an extra parameter matching an ellipsis).
12280 On RS/6000 the first eight words of non-FP are normally in registers
12281 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
12282 Under V.4, the first 8 FP args are in registers.
12284 If this is floating-point and no prototype is specified, we use
12285 both an FP and integer register (or possibly FP reg and stack). Library
12286 functions (when CALL_LIBCALL is set) always have the proper types for args,
12287 so we can pass the FP value just in one register. emit_library_function
12288 doesn't support PARALLEL anyway.
12290 Note that for args passed by reference, function_arg will be called
12291 with MODE and TYPE set to that of the pointer to the arg, not the arg
12292 itself. */
12294 static rtx
12295 rs6000_function_arg (cumulative_args_t cum_v, machine_mode mode,
12296 const_tree type, bool named)
12298 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
12299 enum rs6000_abi abi = DEFAULT_ABI;
12300 machine_mode elt_mode;
12301 int n_elts;
12303 /* Return a marker to indicate whether CR1 needs to set or clear the
12304 bit that V.4 uses to say fp args were passed in registers.
12305 Assume that we don't need the marker for software floating point,
12306 or compiler generated library calls. */
12307 if (mode == VOIDmode)
12309 if (abi == ABI_V4
12310 && (cum->call_cookie & CALL_LIBCALL) == 0
12311 && (cum->stdarg
12312 || (cum->nargs_prototype < 0
12313 && (cum->prototype || TARGET_NO_PROTOTYPE)))
12314 && TARGET_HARD_FLOAT)
12315 return GEN_INT (cum->call_cookie
12316 | ((cum->fregno == FP_ARG_MIN_REG)
12317 ? CALL_V4_SET_FP_ARGS
12318 : CALL_V4_CLEAR_FP_ARGS));
12320 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
12323 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
12325 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
12327 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
12328 if (rslt != NULL_RTX)
12329 return rslt;
12330 /* Else fall through to usual handling. */
12333 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
12335 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
12336 rtx r, off;
12337 int i, k = 0;
12339 /* Do we also need to pass this argument in the parameter save area?
12340 Library support functions for IEEE 128-bit are assumed to not need the
12341 value passed both in GPRs and in vector registers. */
12342 if (TARGET_64BIT && !cum->prototype
12343 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
12345 int align_words = ROUND_UP (cum->words, 2);
12346 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
12349 /* Describe where this argument goes in the vector registers. */
12350 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
12352 r = gen_rtx_REG (elt_mode, cum->vregno + i);
12353 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
12354 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12357 return rs6000_finish_function_arg (mode, rvec, k);
12359 else if (TARGET_ALTIVEC_ABI
12360 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
12361 || (type && TREE_CODE (type) == VECTOR_TYPE
12362 && int_size_in_bytes (type) == 16)))
12364 if (named || abi == ABI_V4)
12365 return NULL_RTX;
12366 else
12368 /* Vector parameters to varargs functions under AIX or Darwin
12369 get passed in memory and possibly also in GPRs. */
12370 int align, align_words, n_words;
12371 machine_mode part_mode;
12373 /* Vector parameters must be 16-byte aligned. In 32-bit
12374 mode this means we need to take into account the offset
12375 to the parameter save area. In 64-bit mode, they just
12376 have to start on an even word, since the parameter save
12377 area is 16-byte aligned. */
12378 if (TARGET_32BIT)
12379 align = -(rs6000_parm_offset () + cum->words) & 3;
12380 else
12381 align = cum->words & 1;
12382 align_words = cum->words + align;
12384 /* Out of registers? Memory, then. */
12385 if (align_words >= GP_ARG_NUM_REG)
12386 return NULL_RTX;
12388 if (TARGET_32BIT && TARGET_POWERPC64)
12389 return rs6000_mixed_function_arg (mode, type, align_words);
12391 /* The vector value goes in GPRs. Only the part of the
12392 value in GPRs is reported here. */
12393 part_mode = mode;
12394 n_words = rs6000_arg_size (mode, type);
12395 if (align_words + n_words > GP_ARG_NUM_REG)
12396 /* Fortunately, there are only two possibilities, the value
12397 is either wholly in GPRs or half in GPRs and half not. */
12398 part_mode = DImode;
12400 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
12404 else if (abi == ABI_V4)
12406 if (abi_v4_pass_in_fpr (mode))
12408 /* _Decimal128 must use an even/odd register pair. This assumes
12409 that the register number is odd when fregno is odd. */
12410 if (mode == TDmode && (cum->fregno % 2) == 1)
12411 cum->fregno++;
12413 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
12414 <= FP_ARG_V4_MAX_REG)
12415 return gen_rtx_REG (mode, cum->fregno);
12416 else
12417 return NULL_RTX;
12419 else
12421 int n_words = rs6000_arg_size (mode, type);
12422 int gregno = cum->sysv_gregno;
12424 /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
12425 As does any other 2 word item such as complex int due to a
12426 historical mistake. */
12427 if (n_words == 2)
12428 gregno += (1 - gregno) & 1;
12430 /* Multi-reg args are not split between registers and stack. */
12431 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
12432 return NULL_RTX;
12434 if (TARGET_32BIT && TARGET_POWERPC64)
12435 return rs6000_mixed_function_arg (mode, type,
12436 gregno - GP_ARG_MIN_REG);
12437 return gen_rtx_REG (mode, gregno);
12440 else
12442 int align_words = rs6000_parm_start (mode, type, cum->words);
12444 /* _Decimal128 must be passed in an even/odd float register pair.
12445 This assumes that the register number is odd when fregno is odd. */
12446 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
12447 cum->fregno++;
12449 if (USE_FP_FOR_ARG_P (cum, elt_mode))
12451 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
12452 rtx r, off;
12453 int i, k = 0;
12454 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
12455 int fpr_words;
12457 /* Do we also need to pass this argument in the parameter
12458 save area? */
12459 if (type && (cum->nargs_prototype <= 0
12460 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
12461 && TARGET_XL_COMPAT
12462 && align_words >= GP_ARG_NUM_REG)))
12463 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
12465 /* Describe where this argument goes in the fprs. */
12466 for (i = 0; i < n_elts
12467 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
12469 /* Check if the argument is split over registers and memory.
12470 This can only ever happen for long double or _Decimal128;
12471 complex types are handled via split_complex_arg. */
12472 machine_mode fmode = elt_mode;
12473 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
12475 gcc_assert (FLOAT128_2REG_P (fmode));
12476 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
12479 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
12480 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
12481 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12484 /* If there were not enough FPRs to hold the argument, the rest
12485 usually goes into memory. However, if the current position
12486 is still within the register parameter area, a portion may
12487 actually have to go into GPRs.
12489 Note that it may happen that the portion of the argument
12490 passed in the first "half" of the first GPR was already
12491 passed in the last FPR as well.
12493 For unnamed arguments, we already set up GPRs to cover the
12494 whole argument in rs6000_psave_function_arg, so there is
12495 nothing further to do at this point. */
12496 fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
12497 if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
12498 && cum->nargs_prototype > 0)
12500 static bool warned;
12502 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
12503 int n_words = rs6000_arg_size (mode, type);
12505 align_words += fpr_words;
12506 n_words -= fpr_words;
12510 r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
12511 off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
12512 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
12514 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
12516 if (!warned && warn_psabi)
12518 warned = true;
12519 inform (input_location,
12520 "the ABI of passing homogeneous float aggregates"
12521 " has changed in GCC 5");
12525 return rs6000_finish_function_arg (mode, rvec, k);
12527 else if (align_words < GP_ARG_NUM_REG)
12529 if (TARGET_32BIT && TARGET_POWERPC64)
12530 return rs6000_mixed_function_arg (mode, type, align_words);
12532 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
12534 else
12535 return NULL_RTX;
12539 /* For an arg passed partly in registers and partly in memory, this is
12540 the number of bytes passed in registers. For args passed entirely in
12541 registers or entirely in memory, zero. When an arg is described by a
12542 PARALLEL, perhaps using more than one register type, this function
12543 returns the number of bytes used by the first element of the PARALLEL. */
12545 static int
12546 rs6000_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
12547 tree type, bool named)
12549 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
12550 bool passed_in_gprs = true;
12551 int ret = 0;
12552 int align_words;
12553 machine_mode elt_mode;
12554 int n_elts;
12556 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
12558 if (DEFAULT_ABI == ABI_V4)
12559 return 0;
12561 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
12563 /* If we are passing this arg in the fixed parameter save area (gprs or
12564 memory) as well as VRs, we do not use the partial bytes mechanism;
12565 instead, rs6000_function_arg will return a PARALLEL including a memory
12566 element as necessary. Library support functions for IEEE 128-bit are
12567 assumed to not need the value passed both in GPRs and in vector
12568 registers. */
12569 if (TARGET_64BIT && !cum->prototype
12570 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
12571 return 0;
12573 /* Otherwise, we pass in VRs only. Check for partial copies. */
12574 passed_in_gprs = false;
12575 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
12576 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
12579 /* In this complicated case we just disable the partial_nregs code. */
12580 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
12581 return 0;
12583 align_words = rs6000_parm_start (mode, type, cum->words);
12585 if (USE_FP_FOR_ARG_P (cum, elt_mode))
12587 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
12589 /* If we are passing this arg in the fixed parameter save area
12590 (gprs or memory) as well as FPRs, we do not use the partial
12591 bytes mechanism; instead, rs6000_function_arg will return a
12592 PARALLEL including a memory element as necessary. */
12593 if (type
12594 && (cum->nargs_prototype <= 0
12595 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
12596 && TARGET_XL_COMPAT
12597 && align_words >= GP_ARG_NUM_REG)))
12598 return 0;
12600 /* Otherwise, we pass in FPRs only. Check for partial copies. */
12601 passed_in_gprs = false;
12602 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
12604 /* Compute number of bytes / words passed in FPRs. If there
12605 is still space available in the register parameter area
12606 *after* that amount, a part of the argument will be passed
12607 in GPRs. In that case, the total amount passed in any
12608 registers is equal to the amount that would have been passed
12609 in GPRs if everything were passed there, so we fall back to
12610 the GPR code below to compute the appropriate value. */
12611 int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
12612 * MIN (8, GET_MODE_SIZE (elt_mode)));
12613 int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
12615 if (align_words + fpr_words < GP_ARG_NUM_REG)
12616 passed_in_gprs = true;
12617 else
12618 ret = fpr;
12622 if (passed_in_gprs
12623 && align_words < GP_ARG_NUM_REG
12624 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
12625 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
12627 if (ret != 0 && TARGET_DEBUG_ARG)
12628 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
12630 return ret;
12633 /* A C expression that indicates when an argument must be passed by
12634 reference. If nonzero for an argument, a copy of that argument is
12635 made in memory and a pointer to the argument is passed instead of
12636 the argument itself. The pointer is passed in whatever way is
12637 appropriate for passing a pointer to that type.
12639 Under V.4, aggregates and long double are passed by reference.
12641 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
12642 reference unless the AltiVec vector extension ABI is in force.
12644 As an extension to all ABIs, variable sized types are passed by
12645 reference. */
12647 static bool
12648 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
12649 machine_mode mode, const_tree type,
12650 bool named ATTRIBUTE_UNUSED)
12652 if (!type)
12653 return 0;
12655 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
12656 && FLOAT128_IEEE_P (TYPE_MODE (type)))
12658 if (TARGET_DEBUG_ARG)
12659 fprintf (stderr, "function_arg_pass_by_reference: V4 IEEE 128-bit\n");
12660 return 1;
12663 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
12665 if (TARGET_DEBUG_ARG)
12666 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
12667 return 1;
12670 if (int_size_in_bytes (type) < 0)
12672 if (TARGET_DEBUG_ARG)
12673 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
12674 return 1;
12677 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
12678 modes only exist for GCC vector types if -maltivec. */
12679 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
12681 if (TARGET_DEBUG_ARG)
12682 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
12683 return 1;
12686 /* Pass synthetic vectors in memory. */
12687 if (TREE_CODE (type) == VECTOR_TYPE
12688 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
12690 static bool warned_for_pass_big_vectors = false;
12691 if (TARGET_DEBUG_ARG)
12692 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
12693 if (!warned_for_pass_big_vectors)
12695 warning (OPT_Wpsabi, "GCC vector passed by reference: "
12696 "non-standard ABI extension with no compatibility "
12697 "guarantee");
12698 warned_for_pass_big_vectors = true;
12700 return 1;
12703 return 0;
12706 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
12707 already processes. Return true if the parameter must be passed
12708 (fully or partially) on the stack. */
12710 static bool
12711 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
12713 machine_mode mode;
12714 int unsignedp;
12715 rtx entry_parm;
12717 /* Catch errors. */
12718 if (type == NULL || type == error_mark_node)
12719 return true;
12721 /* Handle types with no storage requirement. */
12722 if (TYPE_MODE (type) == VOIDmode)
12723 return false;
12725 /* Handle complex types. */
12726 if (TREE_CODE (type) == COMPLEX_TYPE)
12727 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
12728 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
12730 /* Handle transparent aggregates. */
12731 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
12732 && TYPE_TRANSPARENT_AGGR (type))
12733 type = TREE_TYPE (first_field (type));
12735 /* See if this arg was passed by invisible reference. */
12736 if (pass_by_reference (get_cumulative_args (args_so_far),
12737 TYPE_MODE (type), type, true))
12738 type = build_pointer_type (type);
12740 /* Find mode as it is passed by the ABI. */
12741 unsignedp = TYPE_UNSIGNED (type);
12742 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
12744 /* If we must pass in stack, we need a stack. */
12745 if (rs6000_must_pass_in_stack (mode, type))
12746 return true;
12748 /* If there is no incoming register, we need a stack. */
12749 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
12750 if (entry_parm == NULL)
12751 return true;
12753 /* Likewise if we need to pass both in registers and on the stack. */
12754 if (GET_CODE (entry_parm) == PARALLEL
12755 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
12756 return true;
12758 /* Also true if we're partially in registers and partially not. */
12759 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
12760 return true;
12762 /* Update info on where next arg arrives in registers. */
12763 rs6000_function_arg_advance (args_so_far, mode, type, true);
12764 return false;
12767 /* Return true if FUN has no prototype, has a variable argument
12768 list, or passes any parameter in memory. */
12770 static bool
12771 rs6000_function_parms_need_stack (tree fun, bool incoming)
12773 tree fntype, result;
12774 CUMULATIVE_ARGS args_so_far_v;
12775 cumulative_args_t args_so_far;
12777 if (!fun)
12778 /* Must be a libcall, all of which only use reg parms. */
12779 return false;
12781 fntype = fun;
12782 if (!TYPE_P (fun))
12783 fntype = TREE_TYPE (fun);
12785 /* Varargs functions need the parameter save area. */
12786 if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
12787 return true;
12789 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
12790 args_so_far = pack_cumulative_args (&args_so_far_v);
12792 /* When incoming, we will have been passed the function decl.
12793 It is necessary to use the decl to handle K&R style functions,
12794 where TYPE_ARG_TYPES may not be available. */
12795 if (incoming)
12797 gcc_assert (DECL_P (fun));
12798 result = DECL_RESULT (fun);
12800 else
12801 result = TREE_TYPE (fntype);
12803 if (result && aggregate_value_p (result, fntype))
12805 if (!TYPE_P (result))
12806 result = TREE_TYPE (result);
12807 result = build_pointer_type (result);
12808 rs6000_parm_needs_stack (args_so_far, result);
12811 if (incoming)
12813 tree parm;
12815 for (parm = DECL_ARGUMENTS (fun);
12816 parm && parm != void_list_node;
12817 parm = TREE_CHAIN (parm))
12818 if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
12819 return true;
12821 else
12823 function_args_iterator args_iter;
12824 tree arg_type;
12826 FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
12827 if (rs6000_parm_needs_stack (args_so_far, arg_type))
12828 return true;
12831 return false;
12834 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
12835 usually a constant depending on the ABI. However, in the ELFv2 ABI
12836 the register parameter area is optional when calling a function that
12837 has a prototype is scope, has no variable argument list, and passes
12838 all parameters in registers. */
12841 rs6000_reg_parm_stack_space (tree fun, bool incoming)
12843 int reg_parm_stack_space;
12845 switch (DEFAULT_ABI)
12847 default:
12848 reg_parm_stack_space = 0;
12849 break;
12851 case ABI_AIX:
12852 case ABI_DARWIN:
12853 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
12854 break;
12856 case ABI_ELFv2:
12857 /* ??? Recomputing this every time is a bit expensive. Is there
12858 a place to cache this information? */
12859 if (rs6000_function_parms_need_stack (fun, incoming))
12860 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
12861 else
12862 reg_parm_stack_space = 0;
12863 break;
12866 return reg_parm_stack_space;
12869 static void
12870 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
12872 int i;
12873 machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
12875 if (nregs == 0)
12876 return;
12878 for (i = 0; i < nregs; i++)
12880 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
12881 if (reload_completed)
12883 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
12884 tem = NULL_RTX;
12885 else
12886 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
12887 i * GET_MODE_SIZE (reg_mode));
12889 else
12890 tem = replace_equiv_address (tem, XEXP (tem, 0));
12892 gcc_assert (tem);
12894 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
12898 /* Perform any needed actions needed for a function that is receiving a
12899 variable number of arguments.
12901 CUM is as above.
12903 MODE and TYPE are the mode and type of the current parameter.
12905 PRETEND_SIZE is a variable that should be set to the amount of stack
12906 that must be pushed by the prolog to pretend that our caller pushed
12909 Normally, this macro will push all remaining incoming registers on the
12910 stack and set PRETEND_SIZE to the length of the registers pushed. */
12912 static void
12913 setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
12914 tree type, int *pretend_size ATTRIBUTE_UNUSED,
12915 int no_rtl)
12917 CUMULATIVE_ARGS next_cum;
12918 int reg_size = TARGET_32BIT ? 4 : 8;
12919 rtx save_area = NULL_RTX, mem;
12920 int first_reg_offset;
12921 alias_set_type set;
12923 /* Skip the last named argument. */
12924 next_cum = *get_cumulative_args (cum);
12925 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
12927 if (DEFAULT_ABI == ABI_V4)
12929 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
12931 if (! no_rtl)
12933 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
12934 HOST_WIDE_INT offset = 0;
12936 /* Try to optimize the size of the varargs save area.
12937 The ABI requires that ap.reg_save_area is doubleword
12938 aligned, but we don't need to allocate space for all
12939 the bytes, only those to which we actually will save
12940 anything. */
12941 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
12942 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
12943 if (TARGET_HARD_FLOAT
12944 && next_cum.fregno <= FP_ARG_V4_MAX_REG
12945 && cfun->va_list_fpr_size)
12947 if (gpr_reg_num)
12948 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
12949 * UNITS_PER_FP_WORD;
12950 if (cfun->va_list_fpr_size
12951 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
12952 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
12953 else
12954 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
12955 * UNITS_PER_FP_WORD;
12957 if (gpr_reg_num)
12959 offset = -((first_reg_offset * reg_size) & ~7);
12960 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
12962 gpr_reg_num = cfun->va_list_gpr_size;
12963 if (reg_size == 4 && (first_reg_offset & 1))
12964 gpr_reg_num++;
12966 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
12968 else if (fpr_size)
12969 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
12970 * UNITS_PER_FP_WORD
12971 - (int) (GP_ARG_NUM_REG * reg_size);
12973 if (gpr_size + fpr_size)
12975 rtx reg_save_area
12976 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
12977 gcc_assert (GET_CODE (reg_save_area) == MEM);
12978 reg_save_area = XEXP (reg_save_area, 0);
12979 if (GET_CODE (reg_save_area) == PLUS)
12981 gcc_assert (XEXP (reg_save_area, 0)
12982 == virtual_stack_vars_rtx);
12983 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
12984 offset += INTVAL (XEXP (reg_save_area, 1));
12986 else
12987 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
12990 cfun->machine->varargs_save_offset = offset;
12991 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
12994 else
12996 first_reg_offset = next_cum.words;
12997 save_area = crtl->args.internal_arg_pointer;
12999 if (targetm.calls.must_pass_in_stack (mode, type))
13000 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
13003 set = get_varargs_alias_set ();
13004 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
13005 && cfun->va_list_gpr_size)
13007 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
13009 if (va_list_gpr_counter_field)
13010 /* V4 va_list_gpr_size counts number of registers needed. */
13011 n_gpr = cfun->va_list_gpr_size;
13012 else
13013 /* char * va_list instead counts number of bytes needed. */
13014 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
13016 if (nregs > n_gpr)
13017 nregs = n_gpr;
13019 mem = gen_rtx_MEM (BLKmode,
13020 plus_constant (Pmode, save_area,
13021 first_reg_offset * reg_size));
13022 MEM_NOTRAP_P (mem) = 1;
13023 set_mem_alias_set (mem, set);
13024 set_mem_align (mem, BITS_PER_WORD);
13026 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
13027 nregs);
13030 /* Save FP registers if needed. */
13031 if (DEFAULT_ABI == ABI_V4
13032 && TARGET_HARD_FLOAT
13033 && ! no_rtl
13034 && next_cum.fregno <= FP_ARG_V4_MAX_REG
13035 && cfun->va_list_fpr_size)
13037 int fregno = next_cum.fregno, nregs;
13038 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
13039 rtx lab = gen_label_rtx ();
13040 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
13041 * UNITS_PER_FP_WORD);
13043 emit_jump_insn
13044 (gen_rtx_SET (pc_rtx,
13045 gen_rtx_IF_THEN_ELSE (VOIDmode,
13046 gen_rtx_NE (VOIDmode, cr1,
13047 const0_rtx),
13048 gen_rtx_LABEL_REF (VOIDmode, lab),
13049 pc_rtx)));
13051 for (nregs = 0;
13052 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
13053 fregno++, off += UNITS_PER_FP_WORD, nregs++)
13055 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13056 ? DFmode : SFmode,
13057 plus_constant (Pmode, save_area, off));
13058 MEM_NOTRAP_P (mem) = 1;
13059 set_mem_alias_set (mem, set);
13060 set_mem_align (mem, GET_MODE_ALIGNMENT (
13061 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13062 ? DFmode : SFmode));
13063 emit_move_insn (mem, gen_rtx_REG (
13064 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
13065 ? DFmode : SFmode, fregno));
13068 emit_label (lab);
13072 /* Create the va_list data type. */
13074 static tree
13075 rs6000_build_builtin_va_list (void)
13077 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
13079 /* For AIX, prefer 'char *' because that's what the system
13080 header files like. */
13081 if (DEFAULT_ABI != ABI_V4)
13082 return build_pointer_type (char_type_node);
13084 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
13085 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
13086 get_identifier ("__va_list_tag"), record);
13088 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
13089 unsigned_char_type_node);
13090 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
13091 unsigned_char_type_node);
13092 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
13093 every user file. */
13094 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13095 get_identifier ("reserved"), short_unsigned_type_node);
13096 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13097 get_identifier ("overflow_arg_area"),
13098 ptr_type_node);
13099 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
13100 get_identifier ("reg_save_area"),
13101 ptr_type_node);
13103 va_list_gpr_counter_field = f_gpr;
13104 va_list_fpr_counter_field = f_fpr;
13106 DECL_FIELD_CONTEXT (f_gpr) = record;
13107 DECL_FIELD_CONTEXT (f_fpr) = record;
13108 DECL_FIELD_CONTEXT (f_res) = record;
13109 DECL_FIELD_CONTEXT (f_ovf) = record;
13110 DECL_FIELD_CONTEXT (f_sav) = record;
13112 TYPE_STUB_DECL (record) = type_decl;
13113 TYPE_NAME (record) = type_decl;
13114 TYPE_FIELDS (record) = f_gpr;
13115 DECL_CHAIN (f_gpr) = f_fpr;
13116 DECL_CHAIN (f_fpr) = f_res;
13117 DECL_CHAIN (f_res) = f_ovf;
13118 DECL_CHAIN (f_ovf) = f_sav;
13120 layout_type (record);
13122 /* The correct type is an array type of one element. */
13123 return build_array_type (record, build_index_type (size_zero_node));
13126 /* Implement va_start. */
13128 static void
13129 rs6000_va_start (tree valist, rtx nextarg)
13131 HOST_WIDE_INT words, n_gpr, n_fpr;
13132 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
13133 tree gpr, fpr, ovf, sav, t;
13135 /* Only SVR4 needs something special. */
13136 if (DEFAULT_ABI != ABI_V4)
13138 std_expand_builtin_va_start (valist, nextarg);
13139 return;
13142 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
13143 f_fpr = DECL_CHAIN (f_gpr);
13144 f_res = DECL_CHAIN (f_fpr);
13145 f_ovf = DECL_CHAIN (f_res);
13146 f_sav = DECL_CHAIN (f_ovf);
13148 valist = build_simple_mem_ref (valist);
13149 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
13150 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
13151 f_fpr, NULL_TREE);
13152 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
13153 f_ovf, NULL_TREE);
13154 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
13155 f_sav, NULL_TREE);
13157 /* Count number of gp and fp argument registers used. */
13158 words = crtl->args.info.words;
13159 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
13160 GP_ARG_NUM_REG);
13161 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
13162 FP_ARG_NUM_REG);
13164 if (TARGET_DEBUG_ARG)
13165 fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
13166 HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
13167 words, n_gpr, n_fpr);
13169 if (cfun->va_list_gpr_size)
13171 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
13172 build_int_cst (NULL_TREE, n_gpr));
13173 TREE_SIDE_EFFECTS (t) = 1;
13174 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13177 if (cfun->va_list_fpr_size)
13179 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
13180 build_int_cst (NULL_TREE, n_fpr));
13181 TREE_SIDE_EFFECTS (t) = 1;
13182 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13184 #ifdef HAVE_AS_GNU_ATTRIBUTE
13185 if (call_ABI_of_interest (cfun->decl))
13186 rs6000_passes_float = true;
13187 #endif
13190 /* Find the overflow area. */
13191 t = make_tree (TREE_TYPE (ovf), crtl->args.internal_arg_pointer);
13192 if (words != 0)
13193 t = fold_build_pointer_plus_hwi (t, words * MIN_UNITS_PER_WORD);
13194 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
13195 TREE_SIDE_EFFECTS (t) = 1;
13196 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13198 /* If there were no va_arg invocations, don't set up the register
13199 save area. */
13200 if (!cfun->va_list_gpr_size
13201 && !cfun->va_list_fpr_size
13202 && n_gpr < GP_ARG_NUM_REG
13203 && n_fpr < FP_ARG_V4_MAX_REG)
13204 return;
13206 /* Find the register save area. */
13207 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
13208 if (cfun->machine->varargs_save_offset)
13209 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
13210 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
13211 TREE_SIDE_EFFECTS (t) = 1;
13212 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
13215 /* Implement va_arg. */
13217 static tree
13218 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
13219 gimple_seq *post_p)
13221 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
13222 tree gpr, fpr, ovf, sav, reg, t, u;
13223 int size, rsize, n_reg, sav_ofs, sav_scale;
13224 tree lab_false, lab_over, addr;
13225 int align;
13226 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
13227 int regalign = 0;
13228 gimple *stmt;
13230 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
13232 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
13233 return build_va_arg_indirect_ref (t);
13236 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
13237 earlier version of gcc, with the property that it always applied alignment
13238 adjustments to the va-args (even for zero-sized types). The cheapest way
13239 to deal with this is to replicate the effect of the part of
13240 std_gimplify_va_arg_expr that carries out the align adjust, for the case
13241 of relevance.
13242 We don't need to check for pass-by-reference because of the test above.
13243 We can return a simplifed answer, since we know there's no offset to add. */
13245 if (((TARGET_MACHO
13246 && rs6000_darwin64_abi)
13247 || DEFAULT_ABI == ABI_ELFv2
13248 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
13249 && integer_zerop (TYPE_SIZE (type)))
13251 unsigned HOST_WIDE_INT align, boundary;
13252 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
13253 align = PARM_BOUNDARY / BITS_PER_UNIT;
13254 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
13255 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
13256 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
13257 boundary /= BITS_PER_UNIT;
13258 if (boundary > align)
13260 tree t ;
13261 /* This updates arg ptr by the amount that would be necessary
13262 to align the zero-sized (but not zero-alignment) item. */
13263 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
13264 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
13265 gimplify_and_add (t, pre_p);
13267 t = fold_convert (sizetype, valist_tmp);
13268 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
13269 fold_convert (TREE_TYPE (valist),
13270 fold_build2 (BIT_AND_EXPR, sizetype, t,
13271 size_int (-boundary))));
13272 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
13273 gimplify_and_add (t, pre_p);
13275 /* Since it is zero-sized there's no increment for the item itself. */
13276 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
13277 return build_va_arg_indirect_ref (valist_tmp);
13280 if (DEFAULT_ABI != ABI_V4)
13282 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
13284 tree elem_type = TREE_TYPE (type);
13285 machine_mode elem_mode = TYPE_MODE (elem_type);
13286 int elem_size = GET_MODE_SIZE (elem_mode);
13288 if (elem_size < UNITS_PER_WORD)
13290 tree real_part, imag_part;
13291 gimple_seq post = NULL;
13293 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
13294 &post);
13295 /* Copy the value into a temporary, lest the formal temporary
13296 be reused out from under us. */
13297 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
13298 gimple_seq_add_seq (pre_p, post);
13300 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
13301 post_p);
13303 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
13307 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
13310 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
13311 f_fpr = DECL_CHAIN (f_gpr);
13312 f_res = DECL_CHAIN (f_fpr);
13313 f_ovf = DECL_CHAIN (f_res);
13314 f_sav = DECL_CHAIN (f_ovf);
13316 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
13317 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
13318 f_fpr, NULL_TREE);
13319 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
13320 f_ovf, NULL_TREE);
13321 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
13322 f_sav, NULL_TREE);
13324 size = int_size_in_bytes (type);
13325 rsize = (size + 3) / 4;
13326 int pad = 4 * rsize - size;
13327 align = 1;
13329 machine_mode mode = TYPE_MODE (type);
13330 if (abi_v4_pass_in_fpr (mode))
13332 /* FP args go in FP registers, if present. */
13333 reg = fpr;
13334 n_reg = (size + 7) / 8;
13335 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
13336 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
13337 if (mode != SFmode && mode != SDmode)
13338 align = 8;
13340 else
13342 /* Otherwise into GP registers. */
13343 reg = gpr;
13344 n_reg = rsize;
13345 sav_ofs = 0;
13346 sav_scale = 4;
13347 if (n_reg == 2)
13348 align = 8;
13351 /* Pull the value out of the saved registers.... */
13353 lab_over = NULL;
13354 addr = create_tmp_var (ptr_type_node, "addr");
13356 /* AltiVec vectors never go in registers when -mabi=altivec. */
13357 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
13358 align = 16;
13359 else
13361 lab_false = create_artificial_label (input_location);
13362 lab_over = create_artificial_label (input_location);
13364 /* Long long is aligned in the registers. As are any other 2 gpr
13365 item such as complex int due to a historical mistake. */
13366 u = reg;
13367 if (n_reg == 2 && reg == gpr)
13369 regalign = 1;
13370 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
13371 build_int_cst (TREE_TYPE (reg), n_reg - 1));
13372 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
13373 unshare_expr (reg), u);
13375 /* _Decimal128 is passed in even/odd fpr pairs; the stored
13376 reg number is 0 for f1, so we want to make it odd. */
13377 else if (reg == fpr && mode == TDmode)
13379 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
13380 build_int_cst (TREE_TYPE (reg), 1));
13381 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
13384 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
13385 t = build2 (GE_EXPR, boolean_type_node, u, t);
13386 u = build1 (GOTO_EXPR, void_type_node, lab_false);
13387 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
13388 gimplify_and_add (t, pre_p);
13390 t = sav;
13391 if (sav_ofs)
13392 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
13394 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
13395 build_int_cst (TREE_TYPE (reg), n_reg));
13396 u = fold_convert (sizetype, u);
13397 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
13398 t = fold_build_pointer_plus (t, u);
13400 /* _Decimal32 varargs are located in the second word of the 64-bit
13401 FP register for 32-bit binaries. */
13402 if (TARGET_32BIT && TARGET_HARD_FLOAT && mode == SDmode)
13403 t = fold_build_pointer_plus_hwi (t, size);
13405 /* Args are passed right-aligned. */
13406 if (BYTES_BIG_ENDIAN)
13407 t = fold_build_pointer_plus_hwi (t, pad);
13409 gimplify_assign (addr, t, pre_p);
13411 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
13413 stmt = gimple_build_label (lab_false);
13414 gimple_seq_add_stmt (pre_p, stmt);
13416 if ((n_reg == 2 && !regalign) || n_reg > 2)
13418 /* Ensure that we don't find any more args in regs.
13419 Alignment has taken care of for special cases. */
13420 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
13424 /* ... otherwise out of the overflow area. */
13426 /* Care for on-stack alignment if needed. */
13427 t = ovf;
13428 if (align != 1)
13430 t = fold_build_pointer_plus_hwi (t, align - 1);
13431 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
13432 build_int_cst (TREE_TYPE (t), -align));
13435 /* Args are passed right-aligned. */
13436 if (BYTES_BIG_ENDIAN)
13437 t = fold_build_pointer_plus_hwi (t, pad);
13439 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
13441 gimplify_assign (unshare_expr (addr), t, pre_p);
13443 t = fold_build_pointer_plus_hwi (t, size);
13444 gimplify_assign (unshare_expr (ovf), t, pre_p);
13446 if (lab_over)
13448 stmt = gimple_build_label (lab_over);
13449 gimple_seq_add_stmt (pre_p, stmt);
13452 if (STRICT_ALIGNMENT
13453 && (TYPE_ALIGN (type)
13454 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
13456 /* The value (of type complex double, for example) may not be
13457 aligned in memory in the saved registers, so copy via a
13458 temporary. (This is the same code as used for SPARC.) */
13459 tree tmp = create_tmp_var (type, "va_arg_tmp");
13460 tree dest_addr = build_fold_addr_expr (tmp);
13462 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
13463 3, dest_addr, addr, size_int (rsize * 4));
13465 gimplify_and_add (copy, pre_p);
13466 addr = dest_addr;
13469 addr = fold_convert (ptrtype, addr);
13470 return build_va_arg_indirect_ref (addr);
13473 /* Builtins. */
13475 static void
13476 def_builtin (const char *name, tree type, enum rs6000_builtins code)
13478 tree t;
13479 unsigned classify = rs6000_builtin_info[(int)code].attr;
13480 const char *attr_string = "";
13482 gcc_assert (name != NULL);
13483 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
13485 if (rs6000_builtin_decls[(int)code])
13486 fatal_error (input_location,
13487 "internal error: builtin function %qs already processed",
13488 name);
13490 rs6000_builtin_decls[(int)code] = t =
13491 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
13493 /* Set any special attributes. */
13494 if ((classify & RS6000_BTC_CONST) != 0)
13496 /* const function, function only depends on the inputs. */
13497 TREE_READONLY (t) = 1;
13498 TREE_NOTHROW (t) = 1;
13499 attr_string = ", const";
13501 else if ((classify & RS6000_BTC_PURE) != 0)
13503 /* pure function, function can read global memory, but does not set any
13504 external state. */
13505 DECL_PURE_P (t) = 1;
13506 TREE_NOTHROW (t) = 1;
13507 attr_string = ", pure";
13509 else if ((classify & RS6000_BTC_FP) != 0)
13511 /* Function is a math function. If rounding mode is on, then treat the
13512 function as not reading global memory, but it can have arbitrary side
13513 effects. If it is off, then assume the function is a const function.
13514 This mimics the ATTR_MATHFN_FPROUNDING attribute in
13515 builtin-attribute.def that is used for the math functions. */
13516 TREE_NOTHROW (t) = 1;
13517 if (flag_rounding_math)
13519 DECL_PURE_P (t) = 1;
13520 DECL_IS_NOVOPS (t) = 1;
13521 attr_string = ", fp, pure";
13523 else
13525 TREE_READONLY (t) = 1;
13526 attr_string = ", fp, const";
13529 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
13530 gcc_unreachable ();
13532 if (TARGET_DEBUG_BUILTIN)
13533 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
13534 (int)code, name, attr_string);
13537 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
13539 #undef RS6000_BUILTIN_0
13540 #undef RS6000_BUILTIN_1
13541 #undef RS6000_BUILTIN_2
13542 #undef RS6000_BUILTIN_3
13543 #undef RS6000_BUILTIN_A
13544 #undef RS6000_BUILTIN_D
13545 #undef RS6000_BUILTIN_H
13546 #undef RS6000_BUILTIN_P
13547 #undef RS6000_BUILTIN_Q
13548 #undef RS6000_BUILTIN_X
13550 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13551 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13552 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13553 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
13554 { MASK, ICODE, NAME, ENUM },
13556 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13557 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13558 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13559 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13560 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13561 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13563 static const struct builtin_description bdesc_3arg[] =
13565 #include "rs6000-builtin.def"
13568 /* DST operations: void foo (void *, const int, const char). */
13570 #undef RS6000_BUILTIN_0
13571 #undef RS6000_BUILTIN_1
13572 #undef RS6000_BUILTIN_2
13573 #undef RS6000_BUILTIN_3
13574 #undef RS6000_BUILTIN_A
13575 #undef RS6000_BUILTIN_D
13576 #undef RS6000_BUILTIN_H
13577 #undef RS6000_BUILTIN_P
13578 #undef RS6000_BUILTIN_Q
13579 #undef RS6000_BUILTIN_X
13581 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13582 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13583 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13584 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13585 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13586 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
13587 { MASK, ICODE, NAME, ENUM },
13589 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13590 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13591 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13592 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13594 static const struct builtin_description bdesc_dst[] =
13596 #include "rs6000-builtin.def"
13599 /* Simple binary operations: VECc = foo (VECa, VECb). */
13601 #undef RS6000_BUILTIN_0
13602 #undef RS6000_BUILTIN_1
13603 #undef RS6000_BUILTIN_2
13604 #undef RS6000_BUILTIN_3
13605 #undef RS6000_BUILTIN_A
13606 #undef RS6000_BUILTIN_D
13607 #undef RS6000_BUILTIN_H
13608 #undef RS6000_BUILTIN_P
13609 #undef RS6000_BUILTIN_Q
13610 #undef RS6000_BUILTIN_X
13612 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13613 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13614 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
13615 { MASK, ICODE, NAME, ENUM },
13617 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13618 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13619 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13620 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13621 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13622 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13623 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13625 static const struct builtin_description bdesc_2arg[] =
13627 #include "rs6000-builtin.def"
13630 #undef RS6000_BUILTIN_0
13631 #undef RS6000_BUILTIN_1
13632 #undef RS6000_BUILTIN_2
13633 #undef RS6000_BUILTIN_3
13634 #undef RS6000_BUILTIN_A
13635 #undef RS6000_BUILTIN_D
13636 #undef RS6000_BUILTIN_H
13637 #undef RS6000_BUILTIN_P
13638 #undef RS6000_BUILTIN_Q
13639 #undef RS6000_BUILTIN_X
13641 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13642 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13643 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13644 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13645 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13646 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13647 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13648 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
13649 { MASK, ICODE, NAME, ENUM },
13651 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13652 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13654 /* AltiVec predicates. */
13656 static const struct builtin_description bdesc_altivec_preds[] =
13658 #include "rs6000-builtin.def"
13661 /* PAIRED predicates. */
13662 #undef RS6000_BUILTIN_0
13663 #undef RS6000_BUILTIN_1
13664 #undef RS6000_BUILTIN_2
13665 #undef RS6000_BUILTIN_3
13666 #undef RS6000_BUILTIN_A
13667 #undef RS6000_BUILTIN_D
13668 #undef RS6000_BUILTIN_H
13669 #undef RS6000_BUILTIN_P
13670 #undef RS6000_BUILTIN_Q
13671 #undef RS6000_BUILTIN_X
13673 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13674 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13675 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13676 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13677 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13678 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13679 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13680 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13681 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
13682 { MASK, ICODE, NAME, ENUM },
13684 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13686 static const struct builtin_description bdesc_paired_preds[] =
13688 #include "rs6000-builtin.def"
13691 /* ABS* operations. */
13693 #undef RS6000_BUILTIN_0
13694 #undef RS6000_BUILTIN_1
13695 #undef RS6000_BUILTIN_2
13696 #undef RS6000_BUILTIN_3
13697 #undef RS6000_BUILTIN_A
13698 #undef RS6000_BUILTIN_D
13699 #undef RS6000_BUILTIN_H
13700 #undef RS6000_BUILTIN_P
13701 #undef RS6000_BUILTIN_Q
13702 #undef RS6000_BUILTIN_X
13704 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13705 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13706 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13707 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13708 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
13709 { MASK, ICODE, NAME, ENUM },
13711 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13712 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13713 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13714 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13715 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13717 static const struct builtin_description bdesc_abs[] =
13719 #include "rs6000-builtin.def"
13722 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
13723 foo (VECa). */
13725 #undef RS6000_BUILTIN_0
13726 #undef RS6000_BUILTIN_1
13727 #undef RS6000_BUILTIN_2
13728 #undef RS6000_BUILTIN_3
13729 #undef RS6000_BUILTIN_A
13730 #undef RS6000_BUILTIN_D
13731 #undef RS6000_BUILTIN_H
13732 #undef RS6000_BUILTIN_P
13733 #undef RS6000_BUILTIN_Q
13734 #undef RS6000_BUILTIN_X
13736 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13737 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
13738 { MASK, ICODE, NAME, ENUM },
13740 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13741 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13742 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13743 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13744 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13745 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13746 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13747 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13749 static const struct builtin_description bdesc_1arg[] =
13751 #include "rs6000-builtin.def"
13754 /* Simple no-argument operations: result = __builtin_darn_32 () */
13756 #undef RS6000_BUILTIN_0
13757 #undef RS6000_BUILTIN_1
13758 #undef RS6000_BUILTIN_2
13759 #undef RS6000_BUILTIN_3
13760 #undef RS6000_BUILTIN_A
13761 #undef RS6000_BUILTIN_D
13762 #undef RS6000_BUILTIN_H
13763 #undef RS6000_BUILTIN_P
13764 #undef RS6000_BUILTIN_Q
13765 #undef RS6000_BUILTIN_X
13767 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE) \
13768 { MASK, ICODE, NAME, ENUM },
13770 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13771 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13772 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13773 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13774 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13775 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
13776 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13777 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13778 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13780 static const struct builtin_description bdesc_0arg[] =
13782 #include "rs6000-builtin.def"
13785 /* HTM builtins. */
13786 #undef RS6000_BUILTIN_0
13787 #undef RS6000_BUILTIN_1
13788 #undef RS6000_BUILTIN_2
13789 #undef RS6000_BUILTIN_3
13790 #undef RS6000_BUILTIN_A
13791 #undef RS6000_BUILTIN_D
13792 #undef RS6000_BUILTIN_H
13793 #undef RS6000_BUILTIN_P
13794 #undef RS6000_BUILTIN_Q
13795 #undef RS6000_BUILTIN_X
13797 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
13798 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
13799 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
13800 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
13801 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
13802 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
13803 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
13804 { MASK, ICODE, NAME, ENUM },
13806 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
13807 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
13808 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
13810 static const struct builtin_description bdesc_htm[] =
13812 #include "rs6000-builtin.def"
13815 #undef RS6000_BUILTIN_0
13816 #undef RS6000_BUILTIN_1
13817 #undef RS6000_BUILTIN_2
13818 #undef RS6000_BUILTIN_3
13819 #undef RS6000_BUILTIN_A
13820 #undef RS6000_BUILTIN_D
13821 #undef RS6000_BUILTIN_H
13822 #undef RS6000_BUILTIN_P
13823 #undef RS6000_BUILTIN_Q
13825 /* Return true if a builtin function is overloaded. */
13826 bool
13827 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
13829 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
13832 const char *
13833 rs6000_overloaded_builtin_name (enum rs6000_builtins fncode)
13835 return rs6000_builtin_info[(int)fncode].name;
13838 /* Expand an expression EXP that calls a builtin without arguments. */
13839 static rtx
13840 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
13842 rtx pat;
13843 machine_mode tmode = insn_data[icode].operand[0].mode;
13845 if (icode == CODE_FOR_nothing)
13846 /* Builtin not supported on this processor. */
13847 return 0;
13849 if (target == 0
13850 || GET_MODE (target) != tmode
13851 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13852 target = gen_reg_rtx (tmode);
13854 pat = GEN_FCN (icode) (target);
13855 if (! pat)
13856 return 0;
13857 emit_insn (pat);
13859 return target;
13863 static rtx
13864 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
13866 rtx pat;
13867 tree arg0 = CALL_EXPR_ARG (exp, 0);
13868 tree arg1 = CALL_EXPR_ARG (exp, 1);
13869 rtx op0 = expand_normal (arg0);
13870 rtx op1 = expand_normal (arg1);
13871 machine_mode mode0 = insn_data[icode].operand[0].mode;
13872 machine_mode mode1 = insn_data[icode].operand[1].mode;
13874 if (icode == CODE_FOR_nothing)
13875 /* Builtin not supported on this processor. */
13876 return 0;
13878 /* If we got invalid arguments bail out before generating bad rtl. */
13879 if (arg0 == error_mark_node || arg1 == error_mark_node)
13880 return const0_rtx;
13882 if (GET_CODE (op0) != CONST_INT
13883 || INTVAL (op0) > 255
13884 || INTVAL (op0) < 0)
13886 error ("argument 1 must be an 8-bit field value");
13887 return const0_rtx;
13890 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13891 op0 = copy_to_mode_reg (mode0, op0);
13893 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13894 op1 = copy_to_mode_reg (mode1, op1);
13896 pat = GEN_FCN (icode) (op0, op1);
13897 if (! pat)
13898 return const0_rtx;
13899 emit_insn (pat);
13901 return NULL_RTX;
13904 static rtx
13905 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
13907 rtx pat;
13908 tree arg0 = CALL_EXPR_ARG (exp, 0);
13909 rtx op0 = expand_normal (arg0);
13910 machine_mode tmode = insn_data[icode].operand[0].mode;
13911 machine_mode mode0 = insn_data[icode].operand[1].mode;
13913 if (icode == CODE_FOR_nothing)
13914 /* Builtin not supported on this processor. */
13915 return 0;
13917 /* If we got invalid arguments bail out before generating bad rtl. */
13918 if (arg0 == error_mark_node)
13919 return const0_rtx;
13921 if (icode == CODE_FOR_altivec_vspltisb
13922 || icode == CODE_FOR_altivec_vspltish
13923 || icode == CODE_FOR_altivec_vspltisw)
13925 /* Only allow 5-bit *signed* literals. */
13926 if (GET_CODE (op0) != CONST_INT
13927 || INTVAL (op0) > 15
13928 || INTVAL (op0) < -16)
13930 error ("argument 1 must be a 5-bit signed literal");
13931 return CONST0_RTX (tmode);
13935 if (target == 0
13936 || GET_MODE (target) != tmode
13937 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13938 target = gen_reg_rtx (tmode);
13940 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13941 op0 = copy_to_mode_reg (mode0, op0);
13943 pat = GEN_FCN (icode) (target, op0);
13944 if (! pat)
13945 return 0;
13946 emit_insn (pat);
13948 return target;
13951 static rtx
13952 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
13954 rtx pat, scratch1, scratch2;
13955 tree arg0 = CALL_EXPR_ARG (exp, 0);
13956 rtx op0 = expand_normal (arg0);
13957 machine_mode tmode = insn_data[icode].operand[0].mode;
13958 machine_mode mode0 = insn_data[icode].operand[1].mode;
13960 /* If we have invalid arguments, bail out before generating bad rtl. */
13961 if (arg0 == error_mark_node)
13962 return const0_rtx;
13964 if (target == 0
13965 || GET_MODE (target) != tmode
13966 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13967 target = gen_reg_rtx (tmode);
13969 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13970 op0 = copy_to_mode_reg (mode0, op0);
13972 scratch1 = gen_reg_rtx (mode0);
13973 scratch2 = gen_reg_rtx (mode0);
13975 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
13976 if (! pat)
13977 return 0;
13978 emit_insn (pat);
13980 return target;
13983 static rtx
13984 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
13986 rtx pat;
13987 tree arg0 = CALL_EXPR_ARG (exp, 0);
13988 tree arg1 = CALL_EXPR_ARG (exp, 1);
13989 rtx op0 = expand_normal (arg0);
13990 rtx op1 = expand_normal (arg1);
13991 machine_mode tmode = insn_data[icode].operand[0].mode;
13992 machine_mode mode0 = insn_data[icode].operand[1].mode;
13993 machine_mode mode1 = insn_data[icode].operand[2].mode;
13995 if (icode == CODE_FOR_nothing)
13996 /* Builtin not supported on this processor. */
13997 return 0;
13999 /* If we got invalid arguments bail out before generating bad rtl. */
14000 if (arg0 == error_mark_node || arg1 == error_mark_node)
14001 return const0_rtx;
14003 switch (icode)
14005 default:
14006 break;
14007 case CODE_FOR_altivec_vcfux:
14008 case CODE_FOR_altivec_vcfsx:
14009 case CODE_FOR_altivec_vctsxs:
14010 case CODE_FOR_altivec_vctuxs:
14011 case CODE_FOR_altivec_vspltb:
14012 case CODE_FOR_altivec_vsplth:
14013 case CODE_FOR_altivec_vspltw:
14014 /* Only allow 5-bit unsigned literals. */
14015 STRIP_NOPS (arg1);
14016 if (TREE_CODE (arg1) != INTEGER_CST
14017 || TREE_INT_CST_LOW (arg1) & ~0x1f)
14019 error ("argument 2 must be a 5-bit unsigned literal");
14020 return CONST0_RTX (tmode);
14022 break;
14023 case CODE_FOR_dfptstsfi_eq_dd:
14024 case CODE_FOR_dfptstsfi_lt_dd:
14025 case CODE_FOR_dfptstsfi_gt_dd:
14026 case CODE_FOR_dfptstsfi_unordered_dd:
14027 case CODE_FOR_dfptstsfi_eq_td:
14028 case CODE_FOR_dfptstsfi_lt_td:
14029 case CODE_FOR_dfptstsfi_gt_td:
14030 case CODE_FOR_dfptstsfi_unordered_td:
14031 /* Only allow 6-bit unsigned literals. */
14032 STRIP_NOPS (arg0);
14033 if (TREE_CODE (arg0) != INTEGER_CST
14034 || !IN_RANGE (TREE_INT_CST_LOW (arg0), 0, 63))
14036 error ("argument 1 must be a 6-bit unsigned literal");
14037 return CONST0_RTX (tmode);
14039 break;
14040 case CODE_FOR_xststdcqp:
14041 case CODE_FOR_xststdcdp:
14042 case CODE_FOR_xststdcsp:
14043 case CODE_FOR_xvtstdcdp:
14044 case CODE_FOR_xvtstdcsp:
14045 /* Only allow 7-bit unsigned literals. */
14046 STRIP_NOPS (arg1);
14047 if (TREE_CODE (arg1) != INTEGER_CST
14048 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 127))
14050 error ("argument 2 must be a 7-bit unsigned literal");
14051 return CONST0_RTX (tmode);
14053 break;
14054 case CODE_FOR_unpackv1ti:
14055 case CODE_FOR_unpackkf:
14056 case CODE_FOR_unpacktf:
14057 case CODE_FOR_unpackif:
14058 case CODE_FOR_unpacktd:
14059 /* Only allow 1-bit unsigned literals. */
14060 STRIP_NOPS (arg1);
14061 if (TREE_CODE (arg1) != INTEGER_CST
14062 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
14064 error ("argument 2 must be a 1-bit unsigned literal");
14065 return CONST0_RTX (tmode);
14067 break;
14070 if (target == 0
14071 || GET_MODE (target) != tmode
14072 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14073 target = gen_reg_rtx (tmode);
14075 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14076 op0 = copy_to_mode_reg (mode0, op0);
14077 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14078 op1 = copy_to_mode_reg (mode1, op1);
14080 pat = GEN_FCN (icode) (target, op0, op1);
14081 if (! pat)
14082 return 0;
14083 emit_insn (pat);
14085 return target;
14088 static rtx
14089 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
14091 rtx pat, scratch;
14092 tree cr6_form = CALL_EXPR_ARG (exp, 0);
14093 tree arg0 = CALL_EXPR_ARG (exp, 1);
14094 tree arg1 = CALL_EXPR_ARG (exp, 2);
14095 rtx op0 = expand_normal (arg0);
14096 rtx op1 = expand_normal (arg1);
14097 machine_mode tmode = SImode;
14098 machine_mode mode0 = insn_data[icode].operand[1].mode;
14099 machine_mode mode1 = insn_data[icode].operand[2].mode;
14100 int cr6_form_int;
14102 if (TREE_CODE (cr6_form) != INTEGER_CST)
14104 error ("argument 1 of %qs must be a constant",
14105 "__builtin_altivec_predicate");
14106 return const0_rtx;
14108 else
14109 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
14111 gcc_assert (mode0 == mode1);
14113 /* If we have invalid arguments, bail out before generating bad rtl. */
14114 if (arg0 == error_mark_node || arg1 == error_mark_node)
14115 return const0_rtx;
14117 if (target == 0
14118 || GET_MODE (target) != tmode
14119 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14120 target = gen_reg_rtx (tmode);
14122 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14123 op0 = copy_to_mode_reg (mode0, op0);
14124 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14125 op1 = copy_to_mode_reg (mode1, op1);
14127 /* Note that for many of the relevant operations (e.g. cmpne or
14128 cmpeq) with float or double operands, it makes more sense for the
14129 mode of the allocated scratch register to select a vector of
14130 integer. But the choice to copy the mode of operand 0 was made
14131 long ago and there are no plans to change it. */
14132 scratch = gen_reg_rtx (mode0);
14134 pat = GEN_FCN (icode) (scratch, op0, op1);
14135 if (! pat)
14136 return 0;
14137 emit_insn (pat);
14139 /* The vec_any* and vec_all* predicates use the same opcodes for two
14140 different operations, but the bits in CR6 will be different
14141 depending on what information we want. So we have to play tricks
14142 with CR6 to get the right bits out.
14144 If you think this is disgusting, look at the specs for the
14145 AltiVec predicates. */
14147 switch (cr6_form_int)
14149 case 0:
14150 emit_insn (gen_cr6_test_for_zero (target));
14151 break;
14152 case 1:
14153 emit_insn (gen_cr6_test_for_zero_reverse (target));
14154 break;
14155 case 2:
14156 emit_insn (gen_cr6_test_for_lt (target));
14157 break;
14158 case 3:
14159 emit_insn (gen_cr6_test_for_lt_reverse (target));
14160 break;
14161 default:
14162 error ("argument 1 of %qs is out of range",
14163 "__builtin_altivec_predicate");
14164 break;
14167 return target;
14170 static rtx
14171 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
14173 rtx pat, addr;
14174 tree arg0 = CALL_EXPR_ARG (exp, 0);
14175 tree arg1 = CALL_EXPR_ARG (exp, 1);
14176 machine_mode tmode = insn_data[icode].operand[0].mode;
14177 machine_mode mode0 = Pmode;
14178 machine_mode mode1 = Pmode;
14179 rtx op0 = expand_normal (arg0);
14180 rtx op1 = expand_normal (arg1);
14182 if (icode == CODE_FOR_nothing)
14183 /* Builtin not supported on this processor. */
14184 return 0;
14186 /* If we got invalid arguments bail out before generating bad rtl. */
14187 if (arg0 == error_mark_node || arg1 == error_mark_node)
14188 return const0_rtx;
14190 if (target == 0
14191 || GET_MODE (target) != tmode
14192 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14193 target = gen_reg_rtx (tmode);
14195 op1 = copy_to_mode_reg (mode1, op1);
14197 if (op0 == const0_rtx)
14199 addr = gen_rtx_MEM (tmode, op1);
14201 else
14203 op0 = copy_to_mode_reg (mode0, op0);
14204 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
14207 pat = GEN_FCN (icode) (target, addr);
14209 if (! pat)
14210 return 0;
14211 emit_insn (pat);
14213 return target;
14216 /* Return a constant vector for use as a little-endian permute control vector
14217 to reverse the order of elements of the given vector mode. */
14218 static rtx
14219 swap_selector_for_mode (machine_mode mode)
14221 /* These are little endian vectors, so their elements are reversed
14222 from what you would normally expect for a permute control vector. */
14223 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
14224 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
14225 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
14226 unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
14227 unsigned int *swaparray, i;
14228 rtx perm[16];
14230 switch (mode)
14232 case E_V2DFmode:
14233 case E_V2DImode:
14234 swaparray = swap2;
14235 break;
14236 case E_V4SFmode:
14237 case E_V4SImode:
14238 swaparray = swap4;
14239 break;
14240 case E_V8HImode:
14241 swaparray = swap8;
14242 break;
14243 case E_V16QImode:
14244 swaparray = swap16;
14245 break;
14246 default:
14247 gcc_unreachable ();
14250 for (i = 0; i < 16; ++i)
14251 perm[i] = GEN_INT (swaparray[i]);
14253 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
14256 /* Generate code for an "lvxl", or "lve*x" built-in for a little endian target
14257 with -maltivec=be specified. Issue the load followed by an element-
14258 reversing permute. */
14259 void
14260 altivec_expand_lvx_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
14262 rtx tmp = gen_reg_rtx (mode);
14263 rtx load = gen_rtx_SET (tmp, op1);
14264 rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
14265 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
14266 rtx sel = swap_selector_for_mode (mode);
14267 rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
14269 gcc_assert (REG_P (op0));
14270 emit_insn (par);
14271 emit_insn (gen_rtx_SET (op0, vperm));
14274 /* Generate code for a "stvxl" built-in for a little endian target with
14275 -maltivec=be specified. Issue the store preceded by an element-reversing
14276 permute. */
14277 void
14278 altivec_expand_stvx_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
14280 rtx tmp = gen_reg_rtx (mode);
14281 rtx store = gen_rtx_SET (op0, tmp);
14282 rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
14283 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
14284 rtx sel = swap_selector_for_mode (mode);
14285 rtx vperm;
14287 gcc_assert (REG_P (op1));
14288 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
14289 emit_insn (gen_rtx_SET (tmp, vperm));
14290 emit_insn (par);
14293 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
14294 specified. Issue the store preceded by an element-reversing permute. */
14295 void
14296 altivec_expand_stvex_be (rtx op0, rtx op1, machine_mode mode, unsigned unspec)
14298 machine_mode inner_mode = GET_MODE_INNER (mode);
14299 rtx tmp = gen_reg_rtx (mode);
14300 rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
14301 rtx sel = swap_selector_for_mode (mode);
14302 rtx vperm;
14304 gcc_assert (REG_P (op1));
14305 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
14306 emit_insn (gen_rtx_SET (tmp, vperm));
14307 emit_insn (gen_rtx_SET (op0, stvx));
14310 static rtx
14311 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
14313 rtx pat, addr;
14314 tree arg0 = CALL_EXPR_ARG (exp, 0);
14315 tree arg1 = CALL_EXPR_ARG (exp, 1);
14316 machine_mode tmode = insn_data[icode].operand[0].mode;
14317 machine_mode mode0 = Pmode;
14318 machine_mode mode1 = Pmode;
14319 rtx op0 = expand_normal (arg0);
14320 rtx op1 = expand_normal (arg1);
14322 if (icode == CODE_FOR_nothing)
14323 /* Builtin not supported on this processor. */
14324 return 0;
14326 /* If we got invalid arguments bail out before generating bad rtl. */
14327 if (arg0 == error_mark_node || arg1 == error_mark_node)
14328 return const0_rtx;
14330 if (target == 0
14331 || GET_MODE (target) != tmode
14332 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14333 target = gen_reg_rtx (tmode);
14335 op1 = copy_to_mode_reg (mode1, op1);
14337 /* For LVX, express the RTL accurately by ANDing the address with -16.
14338 LVXL and LVE*X expand to use UNSPECs to hide their special behavior,
14339 so the raw address is fine. */
14340 if (icode == CODE_FOR_altivec_lvx_v2df_2op
14341 || icode == CODE_FOR_altivec_lvx_v2di_2op
14342 || icode == CODE_FOR_altivec_lvx_v4sf_2op
14343 || icode == CODE_FOR_altivec_lvx_v4si_2op
14344 || icode == CODE_FOR_altivec_lvx_v8hi_2op
14345 || icode == CODE_FOR_altivec_lvx_v16qi_2op)
14347 rtx rawaddr;
14348 if (op0 == const0_rtx)
14349 rawaddr = op1;
14350 else
14352 op0 = copy_to_mode_reg (mode0, op0);
14353 rawaddr = gen_rtx_PLUS (Pmode, op1, op0);
14355 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
14356 addr = gen_rtx_MEM (blk ? BLKmode : tmode, addr);
14358 /* For -maltivec=be, emit the load and follow it up with a
14359 permute to swap the elements. */
14360 if (!BYTES_BIG_ENDIAN && VECTOR_ELT_ORDER_BIG)
14362 rtx temp = gen_reg_rtx (tmode);
14363 emit_insn (gen_rtx_SET (temp, addr));
14365 rtx sel = swap_selector_for_mode (tmode);
14366 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, temp, temp, sel),
14367 UNSPEC_VPERM);
14368 emit_insn (gen_rtx_SET (target, vperm));
14370 else
14371 emit_insn (gen_rtx_SET (target, addr));
14373 else
14375 if (op0 == const0_rtx)
14376 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
14377 else
14379 op0 = copy_to_mode_reg (mode0, op0);
14380 addr = gen_rtx_MEM (blk ? BLKmode : tmode,
14381 gen_rtx_PLUS (Pmode, op1, op0));
14384 pat = GEN_FCN (icode) (target, addr);
14385 if (! pat)
14386 return 0;
14387 emit_insn (pat);
14390 return target;
14393 static rtx
14394 altivec_expand_xl_be_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
14396 rtx pat, addr;
14397 tree arg0 = CALL_EXPR_ARG (exp, 0);
14398 tree arg1 = CALL_EXPR_ARG (exp, 1);
14399 machine_mode tmode = insn_data[icode].operand[0].mode;
14400 machine_mode mode0 = Pmode;
14401 machine_mode mode1 = Pmode;
14402 rtx op0 = expand_normal (arg0);
14403 rtx op1 = expand_normal (arg1);
14405 if (icode == CODE_FOR_nothing)
14406 /* Builtin not supported on this processor. */
14407 return 0;
14409 /* If we got invalid arguments bail out before generating bad rtl. */
14410 if (arg0 == error_mark_node || arg1 == error_mark_node)
14411 return const0_rtx;
14413 if (target == 0
14414 || GET_MODE (target) != tmode
14415 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14416 target = gen_reg_rtx (tmode);
14418 op1 = copy_to_mode_reg (mode1, op1);
14420 if (op0 == const0_rtx)
14421 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
14422 else
14424 op0 = copy_to_mode_reg (mode0, op0);
14425 addr = gen_rtx_MEM (blk ? BLKmode : tmode,
14426 gen_rtx_PLUS (Pmode, op1, op0));
14429 pat = GEN_FCN (icode) (target, addr);
14430 if (!pat)
14431 return 0;
14433 emit_insn (pat);
14434 /* Reverse element order of elements if in LE mode */
14435 if (!VECTOR_ELT_ORDER_BIG)
14437 rtx sel = swap_selector_for_mode (tmode);
14438 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, target, target, sel),
14439 UNSPEC_VPERM);
14440 emit_insn (gen_rtx_SET (target, vperm));
14442 return target;
14445 static rtx
14446 paired_expand_stv_builtin (enum insn_code icode, tree exp)
14448 tree arg0 = CALL_EXPR_ARG (exp, 0);
14449 tree arg1 = CALL_EXPR_ARG (exp, 1);
14450 tree arg2 = CALL_EXPR_ARG (exp, 2);
14451 rtx op0 = expand_normal (arg0);
14452 rtx op1 = expand_normal (arg1);
14453 rtx op2 = expand_normal (arg2);
14454 rtx pat, addr;
14455 machine_mode tmode = insn_data[icode].operand[0].mode;
14456 machine_mode mode1 = Pmode;
14457 machine_mode mode2 = Pmode;
14459 /* Invalid arguments. Bail before doing anything stoopid! */
14460 if (arg0 == error_mark_node
14461 || arg1 == error_mark_node
14462 || arg2 == error_mark_node)
14463 return const0_rtx;
14465 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
14466 op0 = copy_to_mode_reg (tmode, op0);
14468 op2 = copy_to_mode_reg (mode2, op2);
14470 if (op1 == const0_rtx)
14472 addr = gen_rtx_MEM (tmode, op2);
14474 else
14476 op1 = copy_to_mode_reg (mode1, op1);
14477 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
14480 pat = GEN_FCN (icode) (addr, op0);
14481 if (pat)
14482 emit_insn (pat);
14483 return NULL_RTX;
14486 static rtx
14487 altivec_expand_stxvl_builtin (enum insn_code icode, tree exp)
14489 rtx pat;
14490 tree arg0 = CALL_EXPR_ARG (exp, 0);
14491 tree arg1 = CALL_EXPR_ARG (exp, 1);
14492 tree arg2 = CALL_EXPR_ARG (exp, 2);
14493 rtx op0 = expand_normal (arg0);
14494 rtx op1 = expand_normal (arg1);
14495 rtx op2 = expand_normal (arg2);
14496 machine_mode mode0 = insn_data[icode].operand[0].mode;
14497 machine_mode mode1 = insn_data[icode].operand[1].mode;
14498 machine_mode mode2 = insn_data[icode].operand[2].mode;
14500 if (icode == CODE_FOR_nothing)
14501 /* Builtin not supported on this processor. */
14502 return NULL_RTX;
14504 /* If we got invalid arguments bail out before generating bad rtl. */
14505 if (arg0 == error_mark_node
14506 || arg1 == error_mark_node
14507 || arg2 == error_mark_node)
14508 return NULL_RTX;
14510 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14511 op0 = copy_to_mode_reg (mode0, op0);
14512 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14513 op1 = copy_to_mode_reg (mode1, op1);
14514 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
14515 op2 = copy_to_mode_reg (mode2, op2);
14517 pat = GEN_FCN (icode) (op0, op1, op2);
14518 if (pat)
14519 emit_insn (pat);
14521 return NULL_RTX;
14524 static rtx
14525 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
14527 tree arg0 = CALL_EXPR_ARG (exp, 0);
14528 tree arg1 = CALL_EXPR_ARG (exp, 1);
14529 tree arg2 = CALL_EXPR_ARG (exp, 2);
14530 rtx op0 = expand_normal (arg0);
14531 rtx op1 = expand_normal (arg1);
14532 rtx op2 = expand_normal (arg2);
14533 rtx pat, addr, rawaddr;
14534 machine_mode tmode = insn_data[icode].operand[0].mode;
14535 machine_mode smode = insn_data[icode].operand[1].mode;
14536 machine_mode mode1 = Pmode;
14537 machine_mode mode2 = Pmode;
14539 /* Invalid arguments. Bail before doing anything stoopid! */
14540 if (arg0 == error_mark_node
14541 || arg1 == error_mark_node
14542 || arg2 == error_mark_node)
14543 return const0_rtx;
14545 op2 = copy_to_mode_reg (mode2, op2);
14547 /* For STVX, express the RTL accurately by ANDing the address with -16.
14548 STVXL and STVE*X expand to use UNSPECs to hide their special behavior,
14549 so the raw address is fine. */
14550 if (icode == CODE_FOR_altivec_stvx_v2df_2op
14551 || icode == CODE_FOR_altivec_stvx_v2di_2op
14552 || icode == CODE_FOR_altivec_stvx_v4sf_2op
14553 || icode == CODE_FOR_altivec_stvx_v4si_2op
14554 || icode == CODE_FOR_altivec_stvx_v8hi_2op
14555 || icode == CODE_FOR_altivec_stvx_v16qi_2op)
14557 if (op1 == const0_rtx)
14558 rawaddr = op2;
14559 else
14561 op1 = copy_to_mode_reg (mode1, op1);
14562 rawaddr = gen_rtx_PLUS (Pmode, op2, op1);
14565 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
14566 addr = gen_rtx_MEM (tmode, addr);
14568 op0 = copy_to_mode_reg (tmode, op0);
14570 /* For -maltivec=be, emit a permute to swap the elements, followed
14571 by the store. */
14572 if (!BYTES_BIG_ENDIAN && VECTOR_ELT_ORDER_BIG)
14574 rtx temp = gen_reg_rtx (tmode);
14575 rtx sel = swap_selector_for_mode (tmode);
14576 rtx vperm = gen_rtx_UNSPEC (tmode, gen_rtvec (3, op0, op0, sel),
14577 UNSPEC_VPERM);
14578 emit_insn (gen_rtx_SET (temp, vperm));
14579 emit_insn (gen_rtx_SET (addr, temp));
14581 else
14582 emit_insn (gen_rtx_SET (addr, op0));
14584 else
14586 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
14587 op0 = copy_to_mode_reg (smode, op0);
14589 if (op1 == const0_rtx)
14590 addr = gen_rtx_MEM (tmode, op2);
14591 else
14593 op1 = copy_to_mode_reg (mode1, op1);
14594 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op2, op1));
14597 pat = GEN_FCN (icode) (addr, op0);
14598 if (pat)
14599 emit_insn (pat);
14602 return NULL_RTX;
14605 /* Return the appropriate SPR number associated with the given builtin. */
14606 static inline HOST_WIDE_INT
14607 htm_spr_num (enum rs6000_builtins code)
14609 if (code == HTM_BUILTIN_GET_TFHAR
14610 || code == HTM_BUILTIN_SET_TFHAR)
14611 return TFHAR_SPR;
14612 else if (code == HTM_BUILTIN_GET_TFIAR
14613 || code == HTM_BUILTIN_SET_TFIAR)
14614 return TFIAR_SPR;
14615 else if (code == HTM_BUILTIN_GET_TEXASR
14616 || code == HTM_BUILTIN_SET_TEXASR)
14617 return TEXASR_SPR;
14618 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
14619 || code == HTM_BUILTIN_SET_TEXASRU);
14620 return TEXASRU_SPR;
14623 /* Return the appropriate SPR regno associated with the given builtin. */
14624 static inline HOST_WIDE_INT
14625 htm_spr_regno (enum rs6000_builtins code)
14627 if (code == HTM_BUILTIN_GET_TFHAR
14628 || code == HTM_BUILTIN_SET_TFHAR)
14629 return TFHAR_REGNO;
14630 else if (code == HTM_BUILTIN_GET_TFIAR
14631 || code == HTM_BUILTIN_SET_TFIAR)
14632 return TFIAR_REGNO;
14633 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
14634 || code == HTM_BUILTIN_SET_TEXASR
14635 || code == HTM_BUILTIN_GET_TEXASRU
14636 || code == HTM_BUILTIN_SET_TEXASRU);
14637 return TEXASR_REGNO;
14640 /* Return the correct ICODE value depending on whether we are
14641 setting or reading the HTM SPRs. */
14642 static inline enum insn_code
14643 rs6000_htm_spr_icode (bool nonvoid)
14645 if (nonvoid)
14646 return (TARGET_POWERPC64) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
14647 else
14648 return (TARGET_POWERPC64) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
14651 /* Expand the HTM builtin in EXP and store the result in TARGET.
14652 Store true in *EXPANDEDP if we found a builtin to expand. */
14653 static rtx
14654 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
14656 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14657 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
14658 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
14659 const struct builtin_description *d;
14660 size_t i;
14662 *expandedp = true;
14664 if (!TARGET_POWERPC64
14665 && (fcode == HTM_BUILTIN_TABORTDC
14666 || fcode == HTM_BUILTIN_TABORTDCI))
14668 size_t uns_fcode = (size_t)fcode;
14669 const char *name = rs6000_builtin_info[uns_fcode].name;
14670 error ("builtin %qs is only valid in 64-bit mode", name);
14671 return const0_rtx;
14674 /* Expand the HTM builtins. */
14675 d = bdesc_htm;
14676 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
14677 if (d->code == fcode)
14679 rtx op[MAX_HTM_OPERANDS], pat;
14680 int nopnds = 0;
14681 tree arg;
14682 call_expr_arg_iterator iter;
14683 unsigned attr = rs6000_builtin_info[fcode].attr;
14684 enum insn_code icode = d->icode;
14685 const struct insn_operand_data *insn_op;
14686 bool uses_spr = (attr & RS6000_BTC_SPR);
14687 rtx cr = NULL_RTX;
14689 if (uses_spr)
14690 icode = rs6000_htm_spr_icode (nonvoid);
14691 insn_op = &insn_data[icode].operand[0];
14693 if (nonvoid)
14695 machine_mode tmode = (uses_spr) ? insn_op->mode : E_SImode;
14696 if (!target
14697 || GET_MODE (target) != tmode
14698 || (uses_spr && !(*insn_op->predicate) (target, tmode)))
14699 target = gen_reg_rtx (tmode);
14700 if (uses_spr)
14701 op[nopnds++] = target;
14704 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
14706 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
14707 return const0_rtx;
14709 insn_op = &insn_data[icode].operand[nopnds];
14711 op[nopnds] = expand_normal (arg);
14713 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
14715 if (!strcmp (insn_op->constraint, "n"))
14717 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
14718 if (!CONST_INT_P (op[nopnds]))
14719 error ("argument %d must be an unsigned literal", arg_num);
14720 else
14721 error ("argument %d is an unsigned literal that is "
14722 "out of range", arg_num);
14723 return const0_rtx;
14725 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
14728 nopnds++;
14731 /* Handle the builtins for extended mnemonics. These accept
14732 no arguments, but map to builtins that take arguments. */
14733 switch (fcode)
14735 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
14736 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
14737 op[nopnds++] = GEN_INT (1);
14738 if (flag_checking)
14739 attr |= RS6000_BTC_UNARY;
14740 break;
14741 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
14742 op[nopnds++] = GEN_INT (0);
14743 if (flag_checking)
14744 attr |= RS6000_BTC_UNARY;
14745 break;
14746 default:
14747 break;
14750 /* If this builtin accesses SPRs, then pass in the appropriate
14751 SPR number and SPR regno as the last two operands. */
14752 if (uses_spr)
14754 machine_mode mode = (TARGET_POWERPC64) ? DImode : SImode;
14755 op[nopnds++] = gen_rtx_CONST_INT (mode, htm_spr_num (fcode));
14756 op[nopnds++] = gen_rtx_REG (mode, htm_spr_regno (fcode));
14758 /* If this builtin accesses a CR, then pass in a scratch
14759 CR as the last operand. */
14760 else if (attr & RS6000_BTC_CR)
14761 { cr = gen_reg_rtx (CCmode);
14762 op[nopnds++] = cr;
14765 if (flag_checking)
14767 int expected_nopnds = 0;
14768 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
14769 expected_nopnds = 1;
14770 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
14771 expected_nopnds = 2;
14772 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
14773 expected_nopnds = 3;
14774 if (!(attr & RS6000_BTC_VOID))
14775 expected_nopnds += 1;
14776 if (uses_spr)
14777 expected_nopnds += 2;
14779 gcc_assert (nopnds == expected_nopnds
14780 && nopnds <= MAX_HTM_OPERANDS);
14783 switch (nopnds)
14785 case 1:
14786 pat = GEN_FCN (icode) (op[0]);
14787 break;
14788 case 2:
14789 pat = GEN_FCN (icode) (op[0], op[1]);
14790 break;
14791 case 3:
14792 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
14793 break;
14794 case 4:
14795 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
14796 break;
14797 default:
14798 gcc_unreachable ();
14800 if (!pat)
14801 return NULL_RTX;
14802 emit_insn (pat);
14804 if (attr & RS6000_BTC_CR)
14806 if (fcode == HTM_BUILTIN_TBEGIN)
14808 /* Emit code to set TARGET to true or false depending on
14809 whether the tbegin. instruction successfully or failed
14810 to start a transaction. We do this by placing the 1's
14811 complement of CR's EQ bit into TARGET. */
14812 rtx scratch = gen_reg_rtx (SImode);
14813 emit_insn (gen_rtx_SET (scratch,
14814 gen_rtx_EQ (SImode, cr,
14815 const0_rtx)));
14816 emit_insn (gen_rtx_SET (target,
14817 gen_rtx_XOR (SImode, scratch,
14818 GEN_INT (1))));
14820 else
14822 /* Emit code to copy the 4-bit condition register field
14823 CR into the least significant end of register TARGET. */
14824 rtx scratch1 = gen_reg_rtx (SImode);
14825 rtx scratch2 = gen_reg_rtx (SImode);
14826 rtx subreg = simplify_gen_subreg (CCmode, scratch1, SImode, 0);
14827 emit_insn (gen_movcc (subreg, cr));
14828 emit_insn (gen_lshrsi3 (scratch2, scratch1, GEN_INT (28)));
14829 emit_insn (gen_andsi3 (target, scratch2, GEN_INT (0xf)));
14833 if (nonvoid)
14834 return target;
14835 return const0_rtx;
14838 *expandedp = false;
14839 return NULL_RTX;
14842 /* Expand the CPU builtin in FCODE and store the result in TARGET. */
14844 static rtx
14845 cpu_expand_builtin (enum rs6000_builtins fcode, tree exp ATTRIBUTE_UNUSED,
14846 rtx target)
14848 /* __builtin_cpu_init () is a nop, so expand to nothing. */
14849 if (fcode == RS6000_BUILTIN_CPU_INIT)
14850 return const0_rtx;
14852 if (target == 0 || GET_MODE (target) != SImode)
14853 target = gen_reg_rtx (SImode);
14855 #ifdef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
14856 tree arg = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
14857 /* Target clones creates an ARRAY_REF instead of STRING_CST, convert it back
14858 to a STRING_CST. */
14859 if (TREE_CODE (arg) == ARRAY_REF
14860 && TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST
14861 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST
14862 && compare_tree_int (TREE_OPERAND (arg, 1), 0) == 0)
14863 arg = TREE_OPERAND (arg, 0);
14865 if (TREE_CODE (arg) != STRING_CST)
14867 error ("builtin %qs only accepts a string argument",
14868 rs6000_builtin_info[(size_t) fcode].name);
14869 return const0_rtx;
14872 if (fcode == RS6000_BUILTIN_CPU_IS)
14874 const char *cpu = TREE_STRING_POINTER (arg);
14875 rtx cpuid = NULL_RTX;
14876 for (size_t i = 0; i < ARRAY_SIZE (cpu_is_info); i++)
14877 if (strcmp (cpu, cpu_is_info[i].cpu) == 0)
14879 /* The CPUID value in the TCB is offset by _DL_FIRST_PLATFORM. */
14880 cpuid = GEN_INT (cpu_is_info[i].cpuid + _DL_FIRST_PLATFORM);
14881 break;
14883 if (cpuid == NULL_RTX)
14885 /* Invalid CPU argument. */
14886 error ("cpu %qs is an invalid argument to builtin %qs",
14887 cpu, rs6000_builtin_info[(size_t) fcode].name);
14888 return const0_rtx;
14891 rtx platform = gen_reg_rtx (SImode);
14892 rtx tcbmem = gen_const_mem (SImode,
14893 gen_rtx_PLUS (Pmode,
14894 gen_rtx_REG (Pmode, TLS_REGNUM),
14895 GEN_INT (TCB_PLATFORM_OFFSET)));
14896 emit_move_insn (platform, tcbmem);
14897 emit_insn (gen_eqsi3 (target, platform, cpuid));
14899 else if (fcode == RS6000_BUILTIN_CPU_SUPPORTS)
14901 const char *hwcap = TREE_STRING_POINTER (arg);
14902 rtx mask = NULL_RTX;
14903 int hwcap_offset;
14904 for (size_t i = 0; i < ARRAY_SIZE (cpu_supports_info); i++)
14905 if (strcmp (hwcap, cpu_supports_info[i].hwcap) == 0)
14907 mask = GEN_INT (cpu_supports_info[i].mask);
14908 hwcap_offset = TCB_HWCAP_OFFSET (cpu_supports_info[i].id);
14909 break;
14911 if (mask == NULL_RTX)
14913 /* Invalid HWCAP argument. */
14914 error ("%s %qs is an invalid argument to builtin %qs",
14915 "hwcap", hwcap, rs6000_builtin_info[(size_t) fcode].name);
14916 return const0_rtx;
14919 rtx tcb_hwcap = gen_reg_rtx (SImode);
14920 rtx tcbmem = gen_const_mem (SImode,
14921 gen_rtx_PLUS (Pmode,
14922 gen_rtx_REG (Pmode, TLS_REGNUM),
14923 GEN_INT (hwcap_offset)));
14924 emit_move_insn (tcb_hwcap, tcbmem);
14925 rtx scratch1 = gen_reg_rtx (SImode);
14926 emit_insn (gen_rtx_SET (scratch1, gen_rtx_AND (SImode, tcb_hwcap, mask)));
14927 rtx scratch2 = gen_reg_rtx (SImode);
14928 emit_insn (gen_eqsi3 (scratch2, scratch1, const0_rtx));
14929 emit_insn (gen_rtx_SET (target, gen_rtx_XOR (SImode, scratch2, const1_rtx)));
14931 else
14932 gcc_unreachable ();
14934 /* Record that we have expanded a CPU builtin, so that we can later
14935 emit a reference to the special symbol exported by LIBC to ensure we
14936 do not link against an old LIBC that doesn't support this feature. */
14937 cpu_builtin_p = true;
14939 #else
14940 warning (0, "builtin %qs needs GLIBC (2.23 and newer) that exports hardware "
14941 "capability bits", rs6000_builtin_info[(size_t) fcode].name);
14943 /* For old LIBCs, always return FALSE. */
14944 emit_move_insn (target, GEN_INT (0));
14945 #endif /* TARGET_LIBC_PROVIDES_HWCAP_IN_TCB */
14947 return target;
14950 static rtx
14951 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
14953 rtx pat;
14954 tree arg0 = CALL_EXPR_ARG (exp, 0);
14955 tree arg1 = CALL_EXPR_ARG (exp, 1);
14956 tree arg2 = CALL_EXPR_ARG (exp, 2);
14957 rtx op0 = expand_normal (arg0);
14958 rtx op1 = expand_normal (arg1);
14959 rtx op2 = expand_normal (arg2);
14960 machine_mode tmode = insn_data[icode].operand[0].mode;
14961 machine_mode mode0 = insn_data[icode].operand[1].mode;
14962 machine_mode mode1 = insn_data[icode].operand[2].mode;
14963 machine_mode mode2 = insn_data[icode].operand[3].mode;
14965 if (icode == CODE_FOR_nothing)
14966 /* Builtin not supported on this processor. */
14967 return 0;
14969 /* If we got invalid arguments bail out before generating bad rtl. */
14970 if (arg0 == error_mark_node
14971 || arg1 == error_mark_node
14972 || arg2 == error_mark_node)
14973 return const0_rtx;
14975 /* Check and prepare argument depending on the instruction code.
14977 Note that a switch statement instead of the sequence of tests
14978 would be incorrect as many of the CODE_FOR values could be
14979 CODE_FOR_nothing and that would yield multiple alternatives
14980 with identical values. We'd never reach here at runtime in
14981 this case. */
14982 if (icode == CODE_FOR_altivec_vsldoi_v4sf
14983 || icode == CODE_FOR_altivec_vsldoi_v2df
14984 || icode == CODE_FOR_altivec_vsldoi_v4si
14985 || icode == CODE_FOR_altivec_vsldoi_v8hi
14986 || icode == CODE_FOR_altivec_vsldoi_v16qi)
14988 /* Only allow 4-bit unsigned literals. */
14989 STRIP_NOPS (arg2);
14990 if (TREE_CODE (arg2) != INTEGER_CST
14991 || TREE_INT_CST_LOW (arg2) & ~0xf)
14993 error ("argument 3 must be a 4-bit unsigned literal");
14994 return CONST0_RTX (tmode);
14997 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
14998 || icode == CODE_FOR_vsx_xxpermdi_v2di
14999 || icode == CODE_FOR_vsx_xxpermdi_v2df_be
15000 || icode == CODE_FOR_vsx_xxpermdi_v2di_be
15001 || icode == CODE_FOR_vsx_xxpermdi_v1ti
15002 || icode == CODE_FOR_vsx_xxpermdi_v4sf
15003 || icode == CODE_FOR_vsx_xxpermdi_v4si
15004 || icode == CODE_FOR_vsx_xxpermdi_v8hi
15005 || icode == CODE_FOR_vsx_xxpermdi_v16qi
15006 || icode == CODE_FOR_vsx_xxsldwi_v16qi
15007 || icode == CODE_FOR_vsx_xxsldwi_v8hi
15008 || icode == CODE_FOR_vsx_xxsldwi_v4si
15009 || icode == CODE_FOR_vsx_xxsldwi_v4sf
15010 || icode == CODE_FOR_vsx_xxsldwi_v2di
15011 || icode == CODE_FOR_vsx_xxsldwi_v2df)
15013 /* Only allow 2-bit unsigned literals. */
15014 STRIP_NOPS (arg2);
15015 if (TREE_CODE (arg2) != INTEGER_CST
15016 || TREE_INT_CST_LOW (arg2) & ~0x3)
15018 error ("argument 3 must be a 2-bit unsigned literal");
15019 return CONST0_RTX (tmode);
15022 else if (icode == CODE_FOR_vsx_set_v2df
15023 || icode == CODE_FOR_vsx_set_v2di
15024 || icode == CODE_FOR_bcdadd
15025 || icode == CODE_FOR_bcdadd_lt
15026 || icode == CODE_FOR_bcdadd_eq
15027 || icode == CODE_FOR_bcdadd_gt
15028 || icode == CODE_FOR_bcdsub
15029 || icode == CODE_FOR_bcdsub_lt
15030 || icode == CODE_FOR_bcdsub_eq
15031 || icode == CODE_FOR_bcdsub_gt)
15033 /* Only allow 1-bit unsigned literals. */
15034 STRIP_NOPS (arg2);
15035 if (TREE_CODE (arg2) != INTEGER_CST
15036 || TREE_INT_CST_LOW (arg2) & ~0x1)
15038 error ("argument 3 must be a 1-bit unsigned literal");
15039 return CONST0_RTX (tmode);
15042 else if (icode == CODE_FOR_dfp_ddedpd_dd
15043 || icode == CODE_FOR_dfp_ddedpd_td)
15045 /* Only allow 2-bit unsigned literals where the value is 0 or 2. */
15046 STRIP_NOPS (arg0);
15047 if (TREE_CODE (arg0) != INTEGER_CST
15048 || TREE_INT_CST_LOW (arg2) & ~0x3)
15050 error ("argument 1 must be 0 or 2");
15051 return CONST0_RTX (tmode);
15054 else if (icode == CODE_FOR_dfp_denbcd_dd
15055 || icode == CODE_FOR_dfp_denbcd_td)
15057 /* Only allow 1-bit unsigned literals. */
15058 STRIP_NOPS (arg0);
15059 if (TREE_CODE (arg0) != INTEGER_CST
15060 || TREE_INT_CST_LOW (arg0) & ~0x1)
15062 error ("argument 1 must be a 1-bit unsigned literal");
15063 return CONST0_RTX (tmode);
15066 else if (icode == CODE_FOR_dfp_dscli_dd
15067 || icode == CODE_FOR_dfp_dscli_td
15068 || icode == CODE_FOR_dfp_dscri_dd
15069 || icode == CODE_FOR_dfp_dscri_td)
15071 /* Only allow 6-bit unsigned literals. */
15072 STRIP_NOPS (arg1);
15073 if (TREE_CODE (arg1) != INTEGER_CST
15074 || TREE_INT_CST_LOW (arg1) & ~0x3f)
15076 error ("argument 2 must be a 6-bit unsigned literal");
15077 return CONST0_RTX (tmode);
15080 else if (icode == CODE_FOR_crypto_vshasigmaw
15081 || icode == CODE_FOR_crypto_vshasigmad)
15083 /* Check whether the 2nd and 3rd arguments are integer constants and in
15084 range and prepare arguments. */
15085 STRIP_NOPS (arg1);
15086 if (TREE_CODE (arg1) != INTEGER_CST || wi::geu_p (arg1, 2))
15088 error ("argument 2 must be 0 or 1");
15089 return CONST0_RTX (tmode);
15092 STRIP_NOPS (arg2);
15093 if (TREE_CODE (arg2) != INTEGER_CST || wi::geu_p (arg2, 16))
15095 error ("argument 3 must be in the range 0..15");
15096 return CONST0_RTX (tmode);
15100 if (target == 0
15101 || GET_MODE (target) != tmode
15102 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15103 target = gen_reg_rtx (tmode);
15105 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15106 op0 = copy_to_mode_reg (mode0, op0);
15107 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15108 op1 = copy_to_mode_reg (mode1, op1);
15109 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15110 op2 = copy_to_mode_reg (mode2, op2);
15112 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
15113 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
15114 else
15115 pat = GEN_FCN (icode) (target, op0, op1, op2);
15116 if (! pat)
15117 return 0;
15118 emit_insn (pat);
15120 return target;
15123 /* Expand the lvx builtins. */
15124 static rtx
15125 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
15127 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15128 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15129 tree arg0;
15130 machine_mode tmode, mode0;
15131 rtx pat, op0;
15132 enum insn_code icode;
15134 switch (fcode)
15136 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
15137 icode = CODE_FOR_vector_altivec_load_v16qi;
15138 break;
15139 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
15140 icode = CODE_FOR_vector_altivec_load_v8hi;
15141 break;
15142 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
15143 icode = CODE_FOR_vector_altivec_load_v4si;
15144 break;
15145 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
15146 icode = CODE_FOR_vector_altivec_load_v4sf;
15147 break;
15148 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
15149 icode = CODE_FOR_vector_altivec_load_v2df;
15150 break;
15151 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
15152 icode = CODE_FOR_vector_altivec_load_v2di;
15153 break;
15154 case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
15155 icode = CODE_FOR_vector_altivec_load_v1ti;
15156 break;
15157 default:
15158 *expandedp = false;
15159 return NULL_RTX;
15162 *expandedp = true;
15164 arg0 = CALL_EXPR_ARG (exp, 0);
15165 op0 = expand_normal (arg0);
15166 tmode = insn_data[icode].operand[0].mode;
15167 mode0 = insn_data[icode].operand[1].mode;
15169 if (target == 0
15170 || GET_MODE (target) != tmode
15171 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15172 target = gen_reg_rtx (tmode);
15174 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15175 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15177 pat = GEN_FCN (icode) (target, op0);
15178 if (! pat)
15179 return 0;
15180 emit_insn (pat);
15181 return target;
15184 /* Expand the stvx builtins. */
15185 static rtx
15186 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
15187 bool *expandedp)
15189 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15190 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15191 tree arg0, arg1;
15192 machine_mode mode0, mode1;
15193 rtx pat, op0, op1;
15194 enum insn_code icode;
15196 switch (fcode)
15198 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
15199 icode = CODE_FOR_vector_altivec_store_v16qi;
15200 break;
15201 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
15202 icode = CODE_FOR_vector_altivec_store_v8hi;
15203 break;
15204 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
15205 icode = CODE_FOR_vector_altivec_store_v4si;
15206 break;
15207 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
15208 icode = CODE_FOR_vector_altivec_store_v4sf;
15209 break;
15210 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
15211 icode = CODE_FOR_vector_altivec_store_v2df;
15212 break;
15213 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
15214 icode = CODE_FOR_vector_altivec_store_v2di;
15215 break;
15216 case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
15217 icode = CODE_FOR_vector_altivec_store_v1ti;
15218 break;
15219 default:
15220 *expandedp = false;
15221 return NULL_RTX;
15224 arg0 = CALL_EXPR_ARG (exp, 0);
15225 arg1 = CALL_EXPR_ARG (exp, 1);
15226 op0 = expand_normal (arg0);
15227 op1 = expand_normal (arg1);
15228 mode0 = insn_data[icode].operand[0].mode;
15229 mode1 = insn_data[icode].operand[1].mode;
15231 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
15232 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15233 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
15234 op1 = copy_to_mode_reg (mode1, op1);
15236 pat = GEN_FCN (icode) (op0, op1);
15237 if (pat)
15238 emit_insn (pat);
15240 *expandedp = true;
15241 return NULL_RTX;
15244 /* Expand the dst builtins. */
15245 static rtx
15246 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
15247 bool *expandedp)
15249 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15250 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
15251 tree arg0, arg1, arg2;
15252 machine_mode mode0, mode1;
15253 rtx pat, op0, op1, op2;
15254 const struct builtin_description *d;
15255 size_t i;
15257 *expandedp = false;
15259 /* Handle DST variants. */
15260 d = bdesc_dst;
15261 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
15262 if (d->code == fcode)
15264 arg0 = CALL_EXPR_ARG (exp, 0);
15265 arg1 = CALL_EXPR_ARG (exp, 1);
15266 arg2 = CALL_EXPR_ARG (exp, 2);
15267 op0 = expand_normal (arg0);
15268 op1 = expand_normal (arg1);
15269 op2 = expand_normal (arg2);
15270 mode0 = insn_data[d->icode].operand[0].mode;
15271 mode1 = insn_data[d->icode].operand[1].mode;
15273 /* Invalid arguments, bail out before generating bad rtl. */
15274 if (arg0 == error_mark_node
15275 || arg1 == error_mark_node
15276 || arg2 == error_mark_node)
15277 return const0_rtx;
15279 *expandedp = true;
15280 STRIP_NOPS (arg2);
15281 if (TREE_CODE (arg2) != INTEGER_CST
15282 || TREE_INT_CST_LOW (arg2) & ~0x3)
15284 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
15285 return const0_rtx;
15288 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
15289 op0 = copy_to_mode_reg (Pmode, op0);
15290 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
15291 op1 = copy_to_mode_reg (mode1, op1);
15293 pat = GEN_FCN (d->icode) (op0, op1, op2);
15294 if (pat != 0)
15295 emit_insn (pat);
15297 return NULL_RTX;
15300 return NULL_RTX;
15303 /* Expand vec_init builtin. */
15304 static rtx
15305 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
15307 machine_mode tmode = TYPE_MODE (type);
15308 machine_mode inner_mode = GET_MODE_INNER (tmode);
15309 int i, n_elt = GET_MODE_NUNITS (tmode);
15311 gcc_assert (VECTOR_MODE_P (tmode));
15312 gcc_assert (n_elt == call_expr_nargs (exp));
15314 if (!target || !register_operand (target, tmode))
15315 target = gen_reg_rtx (tmode);
15317 /* If we have a vector compromised of a single element, such as V1TImode, do
15318 the initialization directly. */
15319 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
15321 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
15322 emit_move_insn (target, gen_lowpart (tmode, x));
15324 else
15326 rtvec v = rtvec_alloc (n_elt);
15328 for (i = 0; i < n_elt; ++i)
15330 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
15331 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
15334 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
15337 return target;
15340 /* Return the integer constant in ARG. Constrain it to be in the range
15341 of the subparts of VEC_TYPE; issue an error if not. */
15343 static int
15344 get_element_number (tree vec_type, tree arg)
15346 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
15348 if (!tree_fits_uhwi_p (arg)
15349 || (elt = tree_to_uhwi (arg), elt > max))
15351 error ("selector must be an integer constant in the range 0..%wi", max);
15352 return 0;
15355 return elt;
15358 /* Expand vec_set builtin. */
15359 static rtx
15360 altivec_expand_vec_set_builtin (tree exp)
15362 machine_mode tmode, mode1;
15363 tree arg0, arg1, arg2;
15364 int elt;
15365 rtx op0, op1;
15367 arg0 = CALL_EXPR_ARG (exp, 0);
15368 arg1 = CALL_EXPR_ARG (exp, 1);
15369 arg2 = CALL_EXPR_ARG (exp, 2);
15371 tmode = TYPE_MODE (TREE_TYPE (arg0));
15372 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15373 gcc_assert (VECTOR_MODE_P (tmode));
15375 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
15376 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
15377 elt = get_element_number (TREE_TYPE (arg0), arg2);
15379 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
15380 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
15382 op0 = force_reg (tmode, op0);
15383 op1 = force_reg (mode1, op1);
15385 rs6000_expand_vector_set (op0, op1, elt);
15387 return op0;
15390 /* Expand vec_ext builtin. */
15391 static rtx
15392 altivec_expand_vec_ext_builtin (tree exp, rtx target)
15394 machine_mode tmode, mode0;
15395 tree arg0, arg1;
15396 rtx op0;
15397 rtx op1;
15399 arg0 = CALL_EXPR_ARG (exp, 0);
15400 arg1 = CALL_EXPR_ARG (exp, 1);
15402 op0 = expand_normal (arg0);
15403 op1 = expand_normal (arg1);
15405 /* Call get_element_number to validate arg1 if it is a constant. */
15406 if (TREE_CODE (arg1) == INTEGER_CST)
15407 (void) get_element_number (TREE_TYPE (arg0), arg1);
15409 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15410 mode0 = TYPE_MODE (TREE_TYPE (arg0));
15411 gcc_assert (VECTOR_MODE_P (mode0));
15413 op0 = force_reg (mode0, op0);
15415 if (optimize || !target || !register_operand (target, tmode))
15416 target = gen_reg_rtx (tmode);
15418 rs6000_expand_vector_extract (target, op0, op1);
15420 return target;
15423 /* Expand the builtin in EXP and store the result in TARGET. Store
15424 true in *EXPANDEDP if we found a builtin to expand. */
15425 static rtx
15426 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
15428 const struct builtin_description *d;
15429 size_t i;
15430 enum insn_code icode;
15431 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15432 tree arg0, arg1, arg2;
15433 rtx op0, pat;
15434 machine_mode tmode, mode0;
15435 enum rs6000_builtins fcode
15436 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
15438 if (rs6000_overloaded_builtin_p (fcode))
15440 *expandedp = true;
15441 error ("unresolved overload for Altivec builtin %qF", fndecl);
15443 /* Given it is invalid, just generate a normal call. */
15444 return expand_call (exp, target, false);
15447 target = altivec_expand_ld_builtin (exp, target, expandedp);
15448 if (*expandedp)
15449 return target;
15451 target = altivec_expand_st_builtin (exp, target, expandedp);
15452 if (*expandedp)
15453 return target;
15455 target = altivec_expand_dst_builtin (exp, target, expandedp);
15456 if (*expandedp)
15457 return target;
15459 *expandedp = true;
15461 switch (fcode)
15463 case ALTIVEC_BUILTIN_STVX_V2DF:
15464 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df_2op, exp);
15465 case ALTIVEC_BUILTIN_STVX_V2DI:
15466 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di_2op, exp);
15467 case ALTIVEC_BUILTIN_STVX_V4SF:
15468 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf_2op, exp);
15469 case ALTIVEC_BUILTIN_STVX:
15470 case ALTIVEC_BUILTIN_STVX_V4SI:
15471 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si_2op, exp);
15472 case ALTIVEC_BUILTIN_STVX_V8HI:
15473 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi_2op, exp);
15474 case ALTIVEC_BUILTIN_STVX_V16QI:
15475 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi_2op, exp);
15476 case ALTIVEC_BUILTIN_STVEBX:
15477 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
15478 case ALTIVEC_BUILTIN_STVEHX:
15479 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
15480 case ALTIVEC_BUILTIN_STVEWX:
15481 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
15482 case ALTIVEC_BUILTIN_STVXL_V2DF:
15483 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
15484 case ALTIVEC_BUILTIN_STVXL_V2DI:
15485 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
15486 case ALTIVEC_BUILTIN_STVXL_V4SF:
15487 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
15488 case ALTIVEC_BUILTIN_STVXL:
15489 case ALTIVEC_BUILTIN_STVXL_V4SI:
15490 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
15491 case ALTIVEC_BUILTIN_STVXL_V8HI:
15492 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
15493 case ALTIVEC_BUILTIN_STVXL_V16QI:
15494 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
15496 case ALTIVEC_BUILTIN_STVLX:
15497 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
15498 case ALTIVEC_BUILTIN_STVLXL:
15499 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
15500 case ALTIVEC_BUILTIN_STVRX:
15501 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
15502 case ALTIVEC_BUILTIN_STVRXL:
15503 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
15505 case P9V_BUILTIN_STXVL:
15506 return altivec_expand_stxvl_builtin (CODE_FOR_stxvl, exp);
15508 case VSX_BUILTIN_STXVD2X_V1TI:
15509 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
15510 case VSX_BUILTIN_STXVD2X_V2DF:
15511 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
15512 case VSX_BUILTIN_STXVD2X_V2DI:
15513 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
15514 case VSX_BUILTIN_STXVW4X_V4SF:
15515 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
15516 case VSX_BUILTIN_STXVW4X_V4SI:
15517 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
15518 case VSX_BUILTIN_STXVW4X_V8HI:
15519 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
15520 case VSX_BUILTIN_STXVW4X_V16QI:
15521 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
15523 /* For the following on big endian, it's ok to use any appropriate
15524 unaligned-supporting store, so use a generic expander. For
15525 little-endian, the exact element-reversing instruction must
15526 be used. */
15527 case VSX_BUILTIN_ST_ELEMREV_V2DF:
15529 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2df
15530 : CODE_FOR_vsx_st_elemrev_v2df);
15531 return altivec_expand_stv_builtin (code, exp);
15533 case VSX_BUILTIN_ST_ELEMREV_V2DI:
15535 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2di
15536 : CODE_FOR_vsx_st_elemrev_v2di);
15537 return altivec_expand_stv_builtin (code, exp);
15539 case VSX_BUILTIN_ST_ELEMREV_V4SF:
15541 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4sf
15542 : CODE_FOR_vsx_st_elemrev_v4sf);
15543 return altivec_expand_stv_builtin (code, exp);
15545 case VSX_BUILTIN_ST_ELEMREV_V4SI:
15547 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4si
15548 : CODE_FOR_vsx_st_elemrev_v4si);
15549 return altivec_expand_stv_builtin (code, exp);
15551 case VSX_BUILTIN_ST_ELEMREV_V8HI:
15553 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v8hi
15554 : CODE_FOR_vsx_st_elemrev_v8hi);
15555 return altivec_expand_stv_builtin (code, exp);
15557 case VSX_BUILTIN_ST_ELEMREV_V16QI:
15559 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v16qi
15560 : CODE_FOR_vsx_st_elemrev_v16qi);
15561 return altivec_expand_stv_builtin (code, exp);
15564 case ALTIVEC_BUILTIN_MFVSCR:
15565 icode = CODE_FOR_altivec_mfvscr;
15566 tmode = insn_data[icode].operand[0].mode;
15568 if (target == 0
15569 || GET_MODE (target) != tmode
15570 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15571 target = gen_reg_rtx (tmode);
15573 pat = GEN_FCN (icode) (target);
15574 if (! pat)
15575 return 0;
15576 emit_insn (pat);
15577 return target;
15579 case ALTIVEC_BUILTIN_MTVSCR:
15580 icode = CODE_FOR_altivec_mtvscr;
15581 arg0 = CALL_EXPR_ARG (exp, 0);
15582 op0 = expand_normal (arg0);
15583 mode0 = insn_data[icode].operand[0].mode;
15585 /* If we got invalid arguments bail out before generating bad rtl. */
15586 if (arg0 == error_mark_node)
15587 return const0_rtx;
15589 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
15590 op0 = copy_to_mode_reg (mode0, op0);
15592 pat = GEN_FCN (icode) (op0);
15593 if (pat)
15594 emit_insn (pat);
15595 return NULL_RTX;
15597 case ALTIVEC_BUILTIN_DSSALL:
15598 emit_insn (gen_altivec_dssall ());
15599 return NULL_RTX;
15601 case ALTIVEC_BUILTIN_DSS:
15602 icode = CODE_FOR_altivec_dss;
15603 arg0 = CALL_EXPR_ARG (exp, 0);
15604 STRIP_NOPS (arg0);
15605 op0 = expand_normal (arg0);
15606 mode0 = insn_data[icode].operand[0].mode;
15608 /* If we got invalid arguments bail out before generating bad rtl. */
15609 if (arg0 == error_mark_node)
15610 return const0_rtx;
15612 if (TREE_CODE (arg0) != INTEGER_CST
15613 || TREE_INT_CST_LOW (arg0) & ~0x3)
15615 error ("argument to %qs must be a 2-bit unsigned literal", "dss");
15616 return const0_rtx;
15619 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
15620 op0 = copy_to_mode_reg (mode0, op0);
15622 emit_insn (gen_altivec_dss (op0));
15623 return NULL_RTX;
15625 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
15626 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
15627 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
15628 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
15629 case VSX_BUILTIN_VEC_INIT_V2DF:
15630 case VSX_BUILTIN_VEC_INIT_V2DI:
15631 case VSX_BUILTIN_VEC_INIT_V1TI:
15632 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
15634 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
15635 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
15636 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
15637 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
15638 case VSX_BUILTIN_VEC_SET_V2DF:
15639 case VSX_BUILTIN_VEC_SET_V2DI:
15640 case VSX_BUILTIN_VEC_SET_V1TI:
15641 return altivec_expand_vec_set_builtin (exp);
15643 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
15644 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
15645 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
15646 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
15647 case VSX_BUILTIN_VEC_EXT_V2DF:
15648 case VSX_BUILTIN_VEC_EXT_V2DI:
15649 case VSX_BUILTIN_VEC_EXT_V1TI:
15650 return altivec_expand_vec_ext_builtin (exp, target);
15652 case P9V_BUILTIN_VEXTRACT4B:
15653 case P9V_BUILTIN_VEC_VEXTRACT4B:
15654 arg1 = CALL_EXPR_ARG (exp, 1);
15655 STRIP_NOPS (arg1);
15657 /* Generate a normal call if it is invalid. */
15658 if (arg1 == error_mark_node)
15659 return expand_call (exp, target, false);
15661 if (TREE_CODE (arg1) != INTEGER_CST || TREE_INT_CST_LOW (arg1) > 12)
15663 error ("second argument to %qs must be 0..12", "vec_vextract4b");
15664 return expand_call (exp, target, false);
15666 break;
15668 case P9V_BUILTIN_VINSERT4B:
15669 case P9V_BUILTIN_VINSERT4B_DI:
15670 case P9V_BUILTIN_VEC_VINSERT4B:
15671 arg2 = CALL_EXPR_ARG (exp, 2);
15672 STRIP_NOPS (arg2);
15674 /* Generate a normal call if it is invalid. */
15675 if (arg2 == error_mark_node)
15676 return expand_call (exp, target, false);
15678 if (TREE_CODE (arg2) != INTEGER_CST || TREE_INT_CST_LOW (arg2) > 12)
15680 error ("third argument to %qs must be 0..12", "vec_vinsert4b");
15681 return expand_call (exp, target, false);
15683 break;
15685 default:
15686 break;
15687 /* Fall through. */
15690 /* Expand abs* operations. */
15691 d = bdesc_abs;
15692 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
15693 if (d->code == fcode)
15694 return altivec_expand_abs_builtin (d->icode, exp, target);
15696 /* Expand the AltiVec predicates. */
15697 d = bdesc_altivec_preds;
15698 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
15699 if (d->code == fcode)
15700 return altivec_expand_predicate_builtin (d->icode, exp, target);
15702 /* LV* are funky. We initialized them differently. */
15703 switch (fcode)
15705 case ALTIVEC_BUILTIN_LVSL:
15706 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
15707 exp, target, false);
15708 case ALTIVEC_BUILTIN_LVSR:
15709 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
15710 exp, target, false);
15711 case ALTIVEC_BUILTIN_LVEBX:
15712 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
15713 exp, target, false);
15714 case ALTIVEC_BUILTIN_LVEHX:
15715 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
15716 exp, target, false);
15717 case ALTIVEC_BUILTIN_LVEWX:
15718 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
15719 exp, target, false);
15720 case ALTIVEC_BUILTIN_LVXL_V2DF:
15721 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
15722 exp, target, false);
15723 case ALTIVEC_BUILTIN_LVXL_V2DI:
15724 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
15725 exp, target, false);
15726 case ALTIVEC_BUILTIN_LVXL_V4SF:
15727 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
15728 exp, target, false);
15729 case ALTIVEC_BUILTIN_LVXL:
15730 case ALTIVEC_BUILTIN_LVXL_V4SI:
15731 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
15732 exp, target, false);
15733 case ALTIVEC_BUILTIN_LVXL_V8HI:
15734 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
15735 exp, target, false);
15736 case ALTIVEC_BUILTIN_LVXL_V16QI:
15737 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
15738 exp, target, false);
15739 case ALTIVEC_BUILTIN_LVX_V2DF:
15740 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df_2op,
15741 exp, target, false);
15742 case ALTIVEC_BUILTIN_LVX_V2DI:
15743 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di_2op,
15744 exp, target, false);
15745 case ALTIVEC_BUILTIN_LVX_V4SF:
15746 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf_2op,
15747 exp, target, false);
15748 case ALTIVEC_BUILTIN_LVX:
15749 case ALTIVEC_BUILTIN_LVX_V4SI:
15750 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si_2op,
15751 exp, target, false);
15752 case ALTIVEC_BUILTIN_LVX_V8HI:
15753 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi_2op,
15754 exp, target, false);
15755 case ALTIVEC_BUILTIN_LVX_V16QI:
15756 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi_2op,
15757 exp, target, false);
15758 case ALTIVEC_BUILTIN_LVLX:
15759 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
15760 exp, target, true);
15761 case ALTIVEC_BUILTIN_LVLXL:
15762 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
15763 exp, target, true);
15764 case ALTIVEC_BUILTIN_LVRX:
15765 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
15766 exp, target, true);
15767 case ALTIVEC_BUILTIN_LVRXL:
15768 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
15769 exp, target, true);
15770 case VSX_BUILTIN_LXVD2X_V1TI:
15771 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
15772 exp, target, false);
15773 case VSX_BUILTIN_LXVD2X_V2DF:
15774 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
15775 exp, target, false);
15776 case VSX_BUILTIN_LXVD2X_V2DI:
15777 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
15778 exp, target, false);
15779 case VSX_BUILTIN_LXVW4X_V4SF:
15780 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
15781 exp, target, false);
15782 case VSX_BUILTIN_LXVW4X_V4SI:
15783 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
15784 exp, target, false);
15785 case VSX_BUILTIN_LXVW4X_V8HI:
15786 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
15787 exp, target, false);
15788 case VSX_BUILTIN_LXVW4X_V16QI:
15789 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
15790 exp, target, false);
15791 /* For the following on big endian, it's ok to use any appropriate
15792 unaligned-supporting load, so use a generic expander. For
15793 little-endian, the exact element-reversing instruction must
15794 be used. */
15795 case VSX_BUILTIN_LD_ELEMREV_V2DF:
15797 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2df
15798 : CODE_FOR_vsx_ld_elemrev_v2df);
15799 return altivec_expand_lv_builtin (code, exp, target, false);
15801 case VSX_BUILTIN_LD_ELEMREV_V2DI:
15803 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2di
15804 : CODE_FOR_vsx_ld_elemrev_v2di);
15805 return altivec_expand_lv_builtin (code, exp, target, false);
15807 case VSX_BUILTIN_LD_ELEMREV_V4SF:
15809 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4sf
15810 : CODE_FOR_vsx_ld_elemrev_v4sf);
15811 return altivec_expand_lv_builtin (code, exp, target, false);
15813 case VSX_BUILTIN_LD_ELEMREV_V4SI:
15815 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4si
15816 : CODE_FOR_vsx_ld_elemrev_v4si);
15817 return altivec_expand_lv_builtin (code, exp, target, false);
15819 case VSX_BUILTIN_LD_ELEMREV_V8HI:
15821 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v8hi
15822 : CODE_FOR_vsx_ld_elemrev_v8hi);
15823 return altivec_expand_lv_builtin (code, exp, target, false);
15825 case VSX_BUILTIN_LD_ELEMREV_V16QI:
15827 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v16qi
15828 : CODE_FOR_vsx_ld_elemrev_v16qi);
15829 return altivec_expand_lv_builtin (code, exp, target, false);
15831 break;
15832 default:
15833 break;
15834 /* Fall through. */
15837 /* XL_BE We initialized them to always load in big endian order. */
15838 switch (fcode)
15840 case VSX_BUILTIN_XL_BE_V2DI:
15842 enum insn_code code = CODE_FOR_vsx_load_v2di;
15843 return altivec_expand_xl_be_builtin (code, exp, target, false);
15845 break;
15846 case VSX_BUILTIN_XL_BE_V4SI:
15848 enum insn_code code = CODE_FOR_vsx_load_v4si;
15849 return altivec_expand_xl_be_builtin (code, exp, target, false);
15851 break;
15852 case VSX_BUILTIN_XL_BE_V8HI:
15854 enum insn_code code = CODE_FOR_vsx_load_v8hi;
15855 return altivec_expand_xl_be_builtin (code, exp, target, false);
15857 break;
15858 case VSX_BUILTIN_XL_BE_V16QI:
15860 enum insn_code code = CODE_FOR_vsx_load_v16qi;
15861 return altivec_expand_xl_be_builtin (code, exp, target, false);
15863 break;
15864 case VSX_BUILTIN_XL_BE_V2DF:
15866 enum insn_code code = CODE_FOR_vsx_load_v2df;
15867 return altivec_expand_xl_be_builtin (code, exp, target, false);
15869 break;
15870 case VSX_BUILTIN_XL_BE_V4SF:
15872 enum insn_code code = CODE_FOR_vsx_load_v4sf;
15873 return altivec_expand_xl_be_builtin (code, exp, target, false);
15875 break;
15876 default:
15877 break;
15878 /* Fall through. */
15881 *expandedp = false;
15882 return NULL_RTX;
15885 /* Expand the builtin in EXP and store the result in TARGET. Store
15886 true in *EXPANDEDP if we found a builtin to expand. */
15887 static rtx
15888 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
15890 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15891 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
15892 const struct builtin_description *d;
15893 size_t i;
15895 *expandedp = true;
15897 switch (fcode)
15899 case PAIRED_BUILTIN_STX:
15900 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
15901 case PAIRED_BUILTIN_LX:
15902 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
15903 default:
15904 break;
15905 /* Fall through. */
15908 /* Expand the paired predicates. */
15909 d = bdesc_paired_preds;
15910 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
15911 if (d->code == fcode)
15912 return paired_expand_predicate_builtin (d->icode, exp, target);
15914 *expandedp = false;
15915 return NULL_RTX;
15918 static rtx
15919 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
15921 rtx pat, scratch, tmp;
15922 tree form = CALL_EXPR_ARG (exp, 0);
15923 tree arg0 = CALL_EXPR_ARG (exp, 1);
15924 tree arg1 = CALL_EXPR_ARG (exp, 2);
15925 rtx op0 = expand_normal (arg0);
15926 rtx op1 = expand_normal (arg1);
15927 machine_mode mode0 = insn_data[icode].operand[1].mode;
15928 machine_mode mode1 = insn_data[icode].operand[2].mode;
15929 int form_int;
15930 enum rtx_code code;
15932 if (TREE_CODE (form) != INTEGER_CST)
15934 error ("argument 1 of %s must be a constant",
15935 "__builtin_paired_predicate");
15936 return const0_rtx;
15938 else
15939 form_int = TREE_INT_CST_LOW (form);
15941 gcc_assert (mode0 == mode1);
15943 if (arg0 == error_mark_node || arg1 == error_mark_node)
15944 return const0_rtx;
15946 if (target == 0
15947 || GET_MODE (target) != SImode
15948 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
15949 target = gen_reg_rtx (SImode);
15950 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
15951 op0 = copy_to_mode_reg (mode0, op0);
15952 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
15953 op1 = copy_to_mode_reg (mode1, op1);
15955 scratch = gen_reg_rtx (CCFPmode);
15957 pat = GEN_FCN (icode) (scratch, op0, op1);
15958 if (!pat)
15959 return const0_rtx;
15961 emit_insn (pat);
15963 switch (form_int)
15965 /* LT bit. */
15966 case 0:
15967 code = LT;
15968 break;
15969 /* GT bit. */
15970 case 1:
15971 code = GT;
15972 break;
15973 /* EQ bit. */
15974 case 2:
15975 code = EQ;
15976 break;
15977 /* UN bit. */
15978 case 3:
15979 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
15980 return target;
15981 default:
15982 error ("argument 1 of %qs is out of range",
15983 "__builtin_paired_predicate");
15984 return const0_rtx;
15987 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
15988 emit_move_insn (target, tmp);
15989 return target;
15992 /* Raise an error message for a builtin function that is called without the
15993 appropriate target options being set. */
15995 static void
15996 rs6000_invalid_builtin (enum rs6000_builtins fncode)
15998 size_t uns_fncode = (size_t) fncode;
15999 const char *name = rs6000_builtin_info[uns_fncode].name;
16000 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
16002 gcc_assert (name != NULL);
16003 if ((fnmask & RS6000_BTM_CELL) != 0)
16004 error ("builtin function %qs is only valid for the cell processor", name);
16005 else if ((fnmask & RS6000_BTM_VSX) != 0)
16006 error ("builtin function %qs requires the %qs option", name, "-mvsx");
16007 else if ((fnmask & RS6000_BTM_HTM) != 0)
16008 error ("builtin function %qs requires the %qs option", name, "-mhtm");
16009 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
16010 error ("builtin function %qs requires the %qs option", name, "-maltivec");
16011 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
16012 error ("builtin function %qs requires the %qs option", name, "-mpaired");
16013 else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
16014 == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
16015 error ("builtin function %qs requires the %qs and %qs options",
16016 name, "-mhard-dfp", "-mpower8-vector");
16017 else if ((fnmask & RS6000_BTM_DFP) != 0)
16018 error ("builtin function %qs requires the %qs option", name, "-mhard-dfp");
16019 else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
16020 error ("builtin function %qs requires the %qs option", name,
16021 "-mpower8-vector");
16022 else if ((fnmask & (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
16023 == (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
16024 error ("builtin function %qs requires the %qs and %qs options",
16025 name, "-mcpu=power9", "-m64");
16026 else if ((fnmask & RS6000_BTM_P9_VECTOR) != 0)
16027 error ("builtin function %qs requires the %qs option", name,
16028 "-mcpu=power9");
16029 else if ((fnmask & (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
16030 == (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
16031 error ("builtin function %qs requires the %qs and %qs options",
16032 name, "-mcpu=power9", "-m64");
16033 else if ((fnmask & RS6000_BTM_P9_MISC) == RS6000_BTM_P9_MISC)
16034 error ("builtin function %qs requires the %qs option", name,
16035 "-mcpu=power9");
16036 else if ((fnmask & (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
16037 == (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
16038 error ("builtin function %qs requires the %qs and %qs options",
16039 name, "-mhard-float", "-mlong-double-128");
16040 else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
16041 error ("builtin function %qs requires the %qs option", name,
16042 "-mhard-float");
16043 else if ((fnmask & RS6000_BTM_FLOAT128) != 0)
16044 error ("builtin function %qs requires the %qs option", name, "-mfloat128");
16045 else
16046 error ("builtin function %qs is not supported with the current options",
16047 name);
16050 /* Target hook for early folding of built-ins, shamelessly stolen
16051 from ia64.c. */
16053 static tree
16054 rs6000_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED,
16055 tree *args, bool ignore ATTRIBUTE_UNUSED)
16057 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
16059 enum rs6000_builtins fn_code
16060 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16061 switch (fn_code)
16063 case RS6000_BUILTIN_NANQ:
16064 case RS6000_BUILTIN_NANSQ:
16066 tree type = TREE_TYPE (TREE_TYPE (fndecl));
16067 const char *str = c_getstr (*args);
16068 int quiet = fn_code == RS6000_BUILTIN_NANQ;
16069 REAL_VALUE_TYPE real;
16071 if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
16072 return build_real (type, real);
16073 return NULL_TREE;
16075 case RS6000_BUILTIN_INFQ:
16076 case RS6000_BUILTIN_HUGE_VALQ:
16078 tree type = TREE_TYPE (TREE_TYPE (fndecl));
16079 REAL_VALUE_TYPE inf;
16080 real_inf (&inf);
16081 return build_real (type, inf);
16083 default:
16084 break;
16087 #ifdef SUBTARGET_FOLD_BUILTIN
16088 return SUBTARGET_FOLD_BUILTIN (fndecl, n_args, args, ignore);
16089 #else
16090 return NULL_TREE;
16091 #endif
16094 /* Fold a machine-dependent built-in in GIMPLE. (For folding into
16095 a constant, use rs6000_fold_builtin.) */
16097 bool
16098 rs6000_gimple_fold_builtin (gimple_stmt_iterator *gsi)
16100 gimple *stmt = gsi_stmt (*gsi);
16101 tree fndecl = gimple_call_fndecl (stmt);
16102 gcc_checking_assert (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD);
16103 enum rs6000_builtins fn_code
16104 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
16105 tree arg0, arg1, lhs;
16107 size_t uns_fncode = (size_t) fn_code;
16108 enum insn_code icode = rs6000_builtin_info[uns_fncode].icode;
16109 const char *fn_name1 = rs6000_builtin_info[uns_fncode].name;
16110 const char *fn_name2 = (icode != CODE_FOR_nothing)
16111 ? get_insn_name ((int) icode)
16112 : "nothing";
16114 if (TARGET_DEBUG_BUILTIN)
16115 fprintf (stderr, "rs6000_gimple_fold_builtin %d %s %s\n",
16116 fn_code, fn_name1, fn_name2);
16118 if (!rs6000_fold_gimple)
16119 return false;
16121 /* Generic solution to prevent gimple folding of code without a LHS. */
16122 if (!gimple_call_lhs (stmt))
16123 return false;
16125 switch (fn_code)
16127 /* Flavors of vec_add. We deliberately don't expand
16128 P8V_BUILTIN_VADDUQM as it gets lowered from V1TImode to
16129 TImode, resulting in much poorer code generation. */
16130 case ALTIVEC_BUILTIN_VADDUBM:
16131 case ALTIVEC_BUILTIN_VADDUHM:
16132 case ALTIVEC_BUILTIN_VADDUWM:
16133 case P8V_BUILTIN_VADDUDM:
16134 case ALTIVEC_BUILTIN_VADDFP:
16135 case VSX_BUILTIN_XVADDDP:
16137 arg0 = gimple_call_arg (stmt, 0);
16138 arg1 = gimple_call_arg (stmt, 1);
16139 lhs = gimple_call_lhs (stmt);
16140 gimple *g = gimple_build_assign (lhs, PLUS_EXPR, arg0, arg1);
16141 gimple_set_location (g, gimple_location (stmt));
16142 gsi_replace (gsi, g, true);
16143 return true;
16145 /* Flavors of vec_sub. We deliberately don't expand
16146 P8V_BUILTIN_VSUBUQM. */
16147 case ALTIVEC_BUILTIN_VSUBUBM:
16148 case ALTIVEC_BUILTIN_VSUBUHM:
16149 case ALTIVEC_BUILTIN_VSUBUWM:
16150 case P8V_BUILTIN_VSUBUDM:
16151 case ALTIVEC_BUILTIN_VSUBFP:
16152 case VSX_BUILTIN_XVSUBDP:
16154 arg0 = gimple_call_arg (stmt, 0);
16155 arg1 = gimple_call_arg (stmt, 1);
16156 lhs = gimple_call_lhs (stmt);
16157 gimple *g = gimple_build_assign (lhs, MINUS_EXPR, arg0, arg1);
16158 gimple_set_location (g, gimple_location (stmt));
16159 gsi_replace (gsi, g, true);
16160 return true;
16162 case VSX_BUILTIN_XVMULSP:
16163 case VSX_BUILTIN_XVMULDP:
16165 arg0 = gimple_call_arg (stmt, 0);
16166 arg1 = gimple_call_arg (stmt, 1);
16167 lhs = gimple_call_lhs (stmt);
16168 gimple *g = gimple_build_assign (lhs, MULT_EXPR, arg0, arg1);
16169 gimple_set_location (g, gimple_location (stmt));
16170 gsi_replace (gsi, g, true);
16171 return true;
16173 /* Even element flavors of vec_mul (signed). */
16174 case ALTIVEC_BUILTIN_VMULESB:
16175 case ALTIVEC_BUILTIN_VMULESH:
16176 /* Even element flavors of vec_mul (unsigned). */
16177 case ALTIVEC_BUILTIN_VMULEUB:
16178 case ALTIVEC_BUILTIN_VMULEUH:
16180 arg0 = gimple_call_arg (stmt, 0);
16181 arg1 = gimple_call_arg (stmt, 1);
16182 lhs = gimple_call_lhs (stmt);
16183 gimple *g = gimple_build_assign (lhs, VEC_WIDEN_MULT_EVEN_EXPR, arg0, arg1);
16184 gimple_set_location (g, gimple_location (stmt));
16185 gsi_replace (gsi, g, true);
16186 return true;
16188 /* Odd element flavors of vec_mul (signed). */
16189 case ALTIVEC_BUILTIN_VMULOSB:
16190 case ALTIVEC_BUILTIN_VMULOSH:
16191 /* Odd element flavors of vec_mul (unsigned). */
16192 case ALTIVEC_BUILTIN_VMULOUB:
16193 case ALTIVEC_BUILTIN_VMULOUH:
16195 arg0 = gimple_call_arg (stmt, 0);
16196 arg1 = gimple_call_arg (stmt, 1);
16197 lhs = gimple_call_lhs (stmt);
16198 gimple *g = gimple_build_assign (lhs, VEC_WIDEN_MULT_ODD_EXPR, arg0, arg1);
16199 gimple_set_location (g, gimple_location (stmt));
16200 gsi_replace (gsi, g, true);
16201 return true;
16203 /* Flavors of vec_div (Integer). */
16204 case VSX_BUILTIN_DIV_V2DI:
16205 case VSX_BUILTIN_UDIV_V2DI:
16207 arg0 = gimple_call_arg (stmt, 0);
16208 arg1 = gimple_call_arg (stmt, 1);
16209 lhs = gimple_call_lhs (stmt);
16210 gimple *g = gimple_build_assign (lhs, TRUNC_DIV_EXPR, arg0, arg1);
16211 gimple_set_location (g, gimple_location (stmt));
16212 gsi_replace (gsi, g, true);
16213 return true;
16215 /* Flavors of vec_div (Float). */
16216 case VSX_BUILTIN_XVDIVSP:
16217 case VSX_BUILTIN_XVDIVDP:
16219 arg0 = gimple_call_arg (stmt, 0);
16220 arg1 = gimple_call_arg (stmt, 1);
16221 lhs = gimple_call_lhs (stmt);
16222 gimple *g = gimple_build_assign (lhs, RDIV_EXPR, arg0, arg1);
16223 gimple_set_location (g, gimple_location (stmt));
16224 gsi_replace (gsi, g, true);
16225 return true;
16227 /* Flavors of vec_and. */
16228 case ALTIVEC_BUILTIN_VAND:
16230 arg0 = gimple_call_arg (stmt, 0);
16231 arg1 = gimple_call_arg (stmt, 1);
16232 lhs = gimple_call_lhs (stmt);
16233 gimple *g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, arg1);
16234 gimple_set_location (g, gimple_location (stmt));
16235 gsi_replace (gsi, g, true);
16236 return true;
16238 /* Flavors of vec_andc. */
16239 case ALTIVEC_BUILTIN_VANDC:
16241 arg0 = gimple_call_arg (stmt, 0);
16242 arg1 = gimple_call_arg (stmt, 1);
16243 lhs = gimple_call_lhs (stmt);
16244 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
16245 gimple *g = gimple_build_assign(temp, BIT_NOT_EXPR, arg1);
16246 gimple_set_location (g, gimple_location (stmt));
16247 gsi_insert_before(gsi, g, GSI_SAME_STMT);
16248 g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, temp);
16249 gimple_set_location (g, gimple_location (stmt));
16250 gsi_replace (gsi, g, true);
16251 return true;
16253 /* Flavors of vec_nand. */
16254 case P8V_BUILTIN_VEC_NAND:
16255 case P8V_BUILTIN_NAND_V16QI:
16256 case P8V_BUILTIN_NAND_V8HI:
16257 case P8V_BUILTIN_NAND_V4SI:
16258 case P8V_BUILTIN_NAND_V4SF:
16259 case P8V_BUILTIN_NAND_V2DF:
16260 case P8V_BUILTIN_NAND_V2DI:
16262 arg0 = gimple_call_arg (stmt, 0);
16263 arg1 = gimple_call_arg (stmt, 1);
16264 lhs = gimple_call_lhs (stmt);
16265 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
16266 gimple *g = gimple_build_assign(temp, BIT_AND_EXPR, arg0, arg1);
16267 gimple_set_location (g, gimple_location (stmt));
16268 gsi_insert_before(gsi, g, GSI_SAME_STMT);
16269 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
16270 gimple_set_location (g, gimple_location (stmt));
16271 gsi_replace (gsi, g, true);
16272 return true;
16274 /* Flavors of vec_or. */
16275 case ALTIVEC_BUILTIN_VOR:
16277 arg0 = gimple_call_arg (stmt, 0);
16278 arg1 = gimple_call_arg (stmt, 1);
16279 lhs = gimple_call_lhs (stmt);
16280 gimple *g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, arg1);
16281 gimple_set_location (g, gimple_location (stmt));
16282 gsi_replace (gsi, g, true);
16283 return true;
16285 /* flavors of vec_orc. */
16286 case P8V_BUILTIN_ORC_V16QI:
16287 case P8V_BUILTIN_ORC_V8HI:
16288 case P8V_BUILTIN_ORC_V4SI:
16289 case P8V_BUILTIN_ORC_V4SF:
16290 case P8V_BUILTIN_ORC_V2DF:
16291 case P8V_BUILTIN_ORC_V2DI:
16293 arg0 = gimple_call_arg (stmt, 0);
16294 arg1 = gimple_call_arg (stmt, 1);
16295 lhs = gimple_call_lhs (stmt);
16296 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
16297 gimple *g = gimple_build_assign(temp, BIT_NOT_EXPR, arg1);
16298 gimple_set_location (g, gimple_location (stmt));
16299 gsi_insert_before(gsi, g, GSI_SAME_STMT);
16300 g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, temp);
16301 gimple_set_location (g, gimple_location (stmt));
16302 gsi_replace (gsi, g, true);
16303 return true;
16305 /* Flavors of vec_xor. */
16306 case ALTIVEC_BUILTIN_VXOR:
16308 arg0 = gimple_call_arg (stmt, 0);
16309 arg1 = gimple_call_arg (stmt, 1);
16310 lhs = gimple_call_lhs (stmt);
16311 gimple *g = gimple_build_assign (lhs, BIT_XOR_EXPR, arg0, arg1);
16312 gimple_set_location (g, gimple_location (stmt));
16313 gsi_replace (gsi, g, true);
16314 return true;
16316 /* Flavors of vec_nor. */
16317 case ALTIVEC_BUILTIN_VNOR:
16319 arg0 = gimple_call_arg (stmt, 0);
16320 arg1 = gimple_call_arg (stmt, 1);
16321 lhs = gimple_call_lhs (stmt);
16322 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
16323 gimple *g = gimple_build_assign (temp, BIT_IOR_EXPR, arg0, arg1);
16324 gimple_set_location (g, gimple_location (stmt));
16325 gsi_insert_before(gsi, g, GSI_SAME_STMT);
16326 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
16327 gimple_set_location (g, gimple_location (stmt));
16328 gsi_replace (gsi, g, true);
16329 return true;
16331 /* flavors of vec_abs. */
16332 case ALTIVEC_BUILTIN_ABS_V16QI:
16333 case ALTIVEC_BUILTIN_ABS_V8HI:
16334 case ALTIVEC_BUILTIN_ABS_V4SI:
16335 case ALTIVEC_BUILTIN_ABS_V4SF:
16336 case P8V_BUILTIN_ABS_V2DI:
16337 case VSX_BUILTIN_XVABSDP:
16339 arg0 = gimple_call_arg (stmt, 0);
16340 if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
16341 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (arg0))))
16342 return false;
16343 lhs = gimple_call_lhs (stmt);
16344 gimple *g = gimple_build_assign (lhs, ABS_EXPR, arg0);
16345 gimple_set_location (g, gimple_location (stmt));
16346 gsi_replace (gsi, g, true);
16347 return true;
16349 /* flavors of vec_min. */
16350 case VSX_BUILTIN_XVMINDP:
16351 case P8V_BUILTIN_VMINSD:
16352 case P8V_BUILTIN_VMINUD:
16353 case ALTIVEC_BUILTIN_VMINSB:
16354 case ALTIVEC_BUILTIN_VMINSH:
16355 case ALTIVEC_BUILTIN_VMINSW:
16356 case ALTIVEC_BUILTIN_VMINUB:
16357 case ALTIVEC_BUILTIN_VMINUH:
16358 case ALTIVEC_BUILTIN_VMINUW:
16359 case ALTIVEC_BUILTIN_VMINFP:
16361 arg0 = gimple_call_arg (stmt, 0);
16362 arg1 = gimple_call_arg (stmt, 1);
16363 lhs = gimple_call_lhs (stmt);
16364 gimple *g = gimple_build_assign (lhs, MIN_EXPR, arg0, arg1);
16365 gimple_set_location (g, gimple_location (stmt));
16366 gsi_replace (gsi, g, true);
16367 return true;
16369 /* flavors of vec_max. */
16370 case VSX_BUILTIN_XVMAXDP:
16371 case P8V_BUILTIN_VMAXSD:
16372 case P8V_BUILTIN_VMAXUD:
16373 case ALTIVEC_BUILTIN_VMAXSB:
16374 case ALTIVEC_BUILTIN_VMAXSH:
16375 case ALTIVEC_BUILTIN_VMAXSW:
16376 case ALTIVEC_BUILTIN_VMAXUB:
16377 case ALTIVEC_BUILTIN_VMAXUH:
16378 case ALTIVEC_BUILTIN_VMAXUW:
16379 case ALTIVEC_BUILTIN_VMAXFP:
16381 arg0 = gimple_call_arg (stmt, 0);
16382 arg1 = gimple_call_arg (stmt, 1);
16383 lhs = gimple_call_lhs (stmt);
16384 gimple *g = gimple_build_assign (lhs, MAX_EXPR, arg0, arg1);
16385 gimple_set_location (g, gimple_location (stmt));
16386 gsi_replace (gsi, g, true);
16387 return true;
16389 /* Flavors of vec_eqv. */
16390 case P8V_BUILTIN_EQV_V16QI:
16391 case P8V_BUILTIN_EQV_V8HI:
16392 case P8V_BUILTIN_EQV_V4SI:
16393 case P8V_BUILTIN_EQV_V4SF:
16394 case P8V_BUILTIN_EQV_V2DF:
16395 case P8V_BUILTIN_EQV_V2DI:
16397 arg0 = gimple_call_arg (stmt, 0);
16398 arg1 = gimple_call_arg (stmt, 1);
16399 lhs = gimple_call_lhs (stmt);
16400 tree temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
16401 gimple *g = gimple_build_assign (temp, BIT_XOR_EXPR, arg0, arg1);
16402 gimple_set_location (g, gimple_location (stmt));
16403 gsi_insert_before (gsi, g, GSI_SAME_STMT);
16404 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
16405 gimple_set_location (g, gimple_location (stmt));
16406 gsi_replace (gsi, g, true);
16407 return true;
16409 /* Flavors of vec_rotate_left. */
16410 case ALTIVEC_BUILTIN_VRLB:
16411 case ALTIVEC_BUILTIN_VRLH:
16412 case ALTIVEC_BUILTIN_VRLW:
16413 case P8V_BUILTIN_VRLD:
16415 arg0 = gimple_call_arg (stmt, 0);
16416 arg1 = gimple_call_arg (stmt, 1);
16417 lhs = gimple_call_lhs (stmt);
16418 gimple *g = gimple_build_assign (lhs, LROTATE_EXPR, arg0, arg1);
16419 gimple_set_location (g, gimple_location (stmt));
16420 gsi_replace (gsi, g, true);
16421 return true;
16423 /* Flavors of vector shift right algebraic.
16424 vec_sra{b,h,w} -> vsra{b,h,w}. */
16425 case ALTIVEC_BUILTIN_VSRAB:
16426 case ALTIVEC_BUILTIN_VSRAH:
16427 case ALTIVEC_BUILTIN_VSRAW:
16428 case P8V_BUILTIN_VSRAD:
16430 arg0 = gimple_call_arg (stmt, 0);
16431 arg1 = gimple_call_arg (stmt, 1);
16432 lhs = gimple_call_lhs (stmt);
16433 gimple *g = gimple_build_assign (lhs, RSHIFT_EXPR, arg0, arg1);
16434 gimple_set_location (g, gimple_location (stmt));
16435 gsi_replace (gsi, g, true);
16436 return true;
16438 /* Flavors of vector shift left.
16439 builtin_altivec_vsl{b,h,w} -> vsl{b,h,w}. */
16440 case ALTIVEC_BUILTIN_VSLB:
16441 case ALTIVEC_BUILTIN_VSLH:
16442 case ALTIVEC_BUILTIN_VSLW:
16443 case P8V_BUILTIN_VSLD:
16445 arg0 = gimple_call_arg (stmt, 0);
16446 if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
16447 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (arg0))))
16448 return false;
16449 arg1 = gimple_call_arg (stmt, 1);
16450 lhs = gimple_call_lhs (stmt);
16451 gimple *g = gimple_build_assign (lhs, LSHIFT_EXPR, arg0, arg1);
16452 gimple_set_location (g, gimple_location (stmt));
16453 gsi_replace (gsi, g, true);
16454 return true;
16456 /* Flavors of vector shift right. */
16457 case ALTIVEC_BUILTIN_VSRB:
16458 case ALTIVEC_BUILTIN_VSRH:
16459 case ALTIVEC_BUILTIN_VSRW:
16460 case P8V_BUILTIN_VSRD:
16462 arg0 = gimple_call_arg (stmt, 0);
16463 arg1 = gimple_call_arg (stmt, 1);
16464 lhs = gimple_call_lhs (stmt);
16465 gimple_seq stmts = NULL;
16466 /* Convert arg0 to unsigned. */
16467 tree arg0_unsigned
16468 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
16469 unsigned_type_for (TREE_TYPE (arg0)), arg0);
16470 tree res
16471 = gimple_build (&stmts, RSHIFT_EXPR,
16472 TREE_TYPE (arg0_unsigned), arg0_unsigned, arg1);
16473 /* Convert result back to the lhs type. */
16474 res = gimple_build (&stmts, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), res);
16475 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
16476 update_call_from_tree (gsi, res);
16477 return true;
16479 default:
16480 if (TARGET_DEBUG_BUILTIN)
16481 fprintf (stderr, "gimple builtin intrinsic not matched:%d %s %s\n",
16482 fn_code, fn_name1, fn_name2);
16483 break;
16486 return false;
16489 /* Expand an expression EXP that calls a built-in function,
16490 with result going to TARGET if that's convenient
16491 (and in mode MODE if that's convenient).
16492 SUBTARGET may be used as the target for computing one of EXP's operands.
16493 IGNORE is nonzero if the value is to be ignored. */
16495 static rtx
16496 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
16497 machine_mode mode ATTRIBUTE_UNUSED,
16498 int ignore ATTRIBUTE_UNUSED)
16500 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16501 enum rs6000_builtins fcode
16502 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
16503 size_t uns_fcode = (size_t)fcode;
16504 const struct builtin_description *d;
16505 size_t i;
16506 rtx ret;
16507 bool success;
16508 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
16509 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
16511 if (TARGET_DEBUG_BUILTIN)
16513 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
16514 const char *name1 = rs6000_builtin_info[uns_fcode].name;
16515 const char *name2 = (icode != CODE_FOR_nothing)
16516 ? get_insn_name ((int) icode)
16517 : "nothing";
16518 const char *name3;
16520 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
16522 default: name3 = "unknown"; break;
16523 case RS6000_BTC_SPECIAL: name3 = "special"; break;
16524 case RS6000_BTC_UNARY: name3 = "unary"; break;
16525 case RS6000_BTC_BINARY: name3 = "binary"; break;
16526 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
16527 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
16528 case RS6000_BTC_ABS: name3 = "abs"; break;
16529 case RS6000_BTC_DST: name3 = "dst"; break;
16533 fprintf (stderr,
16534 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
16535 (name1) ? name1 : "---", fcode,
16536 (name2) ? name2 : "---", (int) icode,
16537 name3,
16538 func_valid_p ? "" : ", not valid");
16541 if (!func_valid_p)
16543 rs6000_invalid_builtin (fcode);
16545 /* Given it is invalid, just generate a normal call. */
16546 return expand_call (exp, target, ignore);
16549 switch (fcode)
16551 case RS6000_BUILTIN_RECIP:
16552 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
16554 case RS6000_BUILTIN_RECIPF:
16555 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
16557 case RS6000_BUILTIN_RSQRTF:
16558 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
16560 case RS6000_BUILTIN_RSQRT:
16561 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
16563 case POWER7_BUILTIN_BPERMD:
16564 return rs6000_expand_binop_builtin (((TARGET_64BIT)
16565 ? CODE_FOR_bpermd_di
16566 : CODE_FOR_bpermd_si), exp, target);
16568 case RS6000_BUILTIN_GET_TB:
16569 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
16570 target);
16572 case RS6000_BUILTIN_MFTB:
16573 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
16574 ? CODE_FOR_rs6000_mftb_di
16575 : CODE_FOR_rs6000_mftb_si),
16576 target);
16578 case RS6000_BUILTIN_MFFS:
16579 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
16581 case RS6000_BUILTIN_MTFSF:
16582 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
16584 case RS6000_BUILTIN_CPU_INIT:
16585 case RS6000_BUILTIN_CPU_IS:
16586 case RS6000_BUILTIN_CPU_SUPPORTS:
16587 return cpu_expand_builtin (fcode, exp, target);
16589 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
16590 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
16592 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
16593 : (int) CODE_FOR_altivec_lvsl_direct);
16594 machine_mode tmode = insn_data[icode].operand[0].mode;
16595 machine_mode mode = insn_data[icode].operand[1].mode;
16596 tree arg;
16597 rtx op, addr, pat;
16599 gcc_assert (TARGET_ALTIVEC);
16601 arg = CALL_EXPR_ARG (exp, 0);
16602 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
16603 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
16604 addr = memory_address (mode, op);
16605 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
16606 op = addr;
16607 else
16609 /* For the load case need to negate the address. */
16610 op = gen_reg_rtx (GET_MODE (addr));
16611 emit_insn (gen_rtx_SET (op, gen_rtx_NEG (GET_MODE (addr), addr)));
16613 op = gen_rtx_MEM (mode, op);
16615 if (target == 0
16616 || GET_MODE (target) != tmode
16617 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16618 target = gen_reg_rtx (tmode);
16620 pat = GEN_FCN (icode) (target, op);
16621 if (!pat)
16622 return 0;
16623 emit_insn (pat);
16625 return target;
16628 case ALTIVEC_BUILTIN_VCFUX:
16629 case ALTIVEC_BUILTIN_VCFSX:
16630 case ALTIVEC_BUILTIN_VCTUXS:
16631 case ALTIVEC_BUILTIN_VCTSXS:
16632 /* FIXME: There's got to be a nicer way to handle this case than
16633 constructing a new CALL_EXPR. */
16634 if (call_expr_nargs (exp) == 1)
16636 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
16637 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
16639 break;
16641 default:
16642 break;
16645 if (TARGET_ALTIVEC)
16647 ret = altivec_expand_builtin (exp, target, &success);
16649 if (success)
16650 return ret;
16652 if (TARGET_PAIRED_FLOAT)
16654 ret = paired_expand_builtin (exp, target, &success);
16656 if (success)
16657 return ret;
16659 if (TARGET_HTM)
16661 ret = htm_expand_builtin (exp, target, &success);
16663 if (success)
16664 return ret;
16667 unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
16668 /* RS6000_BTC_SPECIAL represents no-operand operators. */
16669 gcc_assert (attr == RS6000_BTC_UNARY
16670 || attr == RS6000_BTC_BINARY
16671 || attr == RS6000_BTC_TERNARY
16672 || attr == RS6000_BTC_SPECIAL);
16674 /* Handle simple unary operations. */
16675 d = bdesc_1arg;
16676 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16677 if (d->code == fcode)
16678 return rs6000_expand_unop_builtin (d->icode, exp, target);
16680 /* Handle simple binary operations. */
16681 d = bdesc_2arg;
16682 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16683 if (d->code == fcode)
16684 return rs6000_expand_binop_builtin (d->icode, exp, target);
16686 /* Handle simple ternary operations. */
16687 d = bdesc_3arg;
16688 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
16689 if (d->code == fcode)
16690 return rs6000_expand_ternop_builtin (d->icode, exp, target);
16692 /* Handle simple no-argument operations. */
16693 d = bdesc_0arg;
16694 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
16695 if (d->code == fcode)
16696 return rs6000_expand_zeroop_builtin (d->icode, target);
16698 gcc_unreachable ();
16701 /* Create a builtin vector type with a name. Taking care not to give
16702 the canonical type a name. */
16704 static tree
16705 rs6000_vector_type (const char *name, tree elt_type, unsigned num_elts)
16707 tree result = build_vector_type (elt_type, num_elts);
16709 /* Copy so we don't give the canonical type a name. */
16710 result = build_variant_type_copy (result);
16712 add_builtin_type (name, result);
16714 return result;
16717 static void
16718 rs6000_init_builtins (void)
16720 tree tdecl;
16721 tree ftype;
16722 machine_mode mode;
16724 if (TARGET_DEBUG_BUILTIN)
16725 fprintf (stderr, "rs6000_init_builtins%s%s%s\n",
16726 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
16727 (TARGET_ALTIVEC) ? ", altivec" : "",
16728 (TARGET_VSX) ? ", vsx" : "");
16730 V2SI_type_node = build_vector_type (intSI_type_node, 2);
16731 V2SF_type_node = build_vector_type (float_type_node, 2);
16732 V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64 ? "__vector long"
16733 : "__vector long long",
16734 intDI_type_node, 2);
16735 V2DF_type_node = rs6000_vector_type ("__vector double", double_type_node, 2);
16736 V4SI_type_node = rs6000_vector_type ("__vector signed int",
16737 intSI_type_node, 4);
16738 V4SF_type_node = rs6000_vector_type ("__vector float", float_type_node, 4);
16739 V8HI_type_node = rs6000_vector_type ("__vector signed short",
16740 intHI_type_node, 8);
16741 V16QI_type_node = rs6000_vector_type ("__vector signed char",
16742 intQI_type_node, 16);
16744 unsigned_V16QI_type_node = rs6000_vector_type ("__vector unsigned char",
16745 unsigned_intQI_type_node, 16);
16746 unsigned_V8HI_type_node = rs6000_vector_type ("__vector unsigned short",
16747 unsigned_intHI_type_node, 8);
16748 unsigned_V4SI_type_node = rs6000_vector_type ("__vector unsigned int",
16749 unsigned_intSI_type_node, 4);
16750 unsigned_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
16751 ? "__vector unsigned long"
16752 : "__vector unsigned long long",
16753 unsigned_intDI_type_node, 2);
16755 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
16756 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
16757 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
16758 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
16760 const_str_type_node
16761 = build_pointer_type (build_qualified_type (char_type_node,
16762 TYPE_QUAL_CONST));
16764 /* We use V1TI mode as a special container to hold __int128_t items that
16765 must live in VSX registers. */
16766 if (intTI_type_node)
16768 V1TI_type_node = rs6000_vector_type ("__vector __int128",
16769 intTI_type_node, 1);
16770 unsigned_V1TI_type_node
16771 = rs6000_vector_type ("__vector unsigned __int128",
16772 unsigned_intTI_type_node, 1);
16775 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
16776 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
16777 'vector unsigned short'. */
16779 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
16780 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
16781 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
16782 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
16783 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
16785 long_integer_type_internal_node = long_integer_type_node;
16786 long_unsigned_type_internal_node = long_unsigned_type_node;
16787 long_long_integer_type_internal_node = long_long_integer_type_node;
16788 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
16789 intQI_type_internal_node = intQI_type_node;
16790 uintQI_type_internal_node = unsigned_intQI_type_node;
16791 intHI_type_internal_node = intHI_type_node;
16792 uintHI_type_internal_node = unsigned_intHI_type_node;
16793 intSI_type_internal_node = intSI_type_node;
16794 uintSI_type_internal_node = unsigned_intSI_type_node;
16795 intDI_type_internal_node = intDI_type_node;
16796 uintDI_type_internal_node = unsigned_intDI_type_node;
16797 intTI_type_internal_node = intTI_type_node;
16798 uintTI_type_internal_node = unsigned_intTI_type_node;
16799 float_type_internal_node = float_type_node;
16800 double_type_internal_node = double_type_node;
16801 long_double_type_internal_node = long_double_type_node;
16802 dfloat64_type_internal_node = dfloat64_type_node;
16803 dfloat128_type_internal_node = dfloat128_type_node;
16804 void_type_internal_node = void_type_node;
16806 /* 128-bit floating point support. KFmode is IEEE 128-bit floating point.
16807 IFmode is the IBM extended 128-bit format that is a pair of doubles.
16808 TFmode will be either IEEE 128-bit floating point or the IBM double-double
16809 format that uses a pair of doubles, depending on the switches and
16810 defaults.
16812 We do not enable the actual __float128 keyword unless the user explicitly
16813 asks for it, because the library support is not yet complete.
16815 If we don't support for either 128-bit IBM double double or IEEE 128-bit
16816 floating point, we need make sure the type is non-zero or else self-test
16817 fails during bootstrap.
16819 We don't register a built-in type for __ibm128 if the type is the same as
16820 long double. Instead we add a #define for __ibm128 in
16821 rs6000_cpu_cpp_builtins to long double. */
16822 if (TARGET_LONG_DOUBLE_128 && FLOAT128_IEEE_P (TFmode))
16824 ibm128_float_type_node = make_node (REAL_TYPE);
16825 TYPE_PRECISION (ibm128_float_type_node) = 128;
16826 SET_TYPE_MODE (ibm128_float_type_node, IFmode);
16827 layout_type (ibm128_float_type_node);
16829 lang_hooks.types.register_builtin_type (ibm128_float_type_node,
16830 "__ibm128");
16832 else
16833 ibm128_float_type_node = long_double_type_node;
16835 if (TARGET_FLOAT128_KEYWORD)
16837 ieee128_float_type_node = float128_type_node;
16838 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
16839 "__float128");
16842 else if (TARGET_FLOAT128_TYPE)
16844 ieee128_float_type_node = make_node (REAL_TYPE);
16845 TYPE_PRECISION (ibm128_float_type_node) = 128;
16846 SET_TYPE_MODE (ieee128_float_type_node, KFmode);
16847 layout_type (ieee128_float_type_node);
16849 /* If we are not exporting the __float128/_Float128 keywords, we need a
16850 keyword to get the types created. Use __ieee128 as the dummy
16851 keyword. */
16852 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
16853 "__ieee128");
16856 else
16857 ieee128_float_type_node = long_double_type_node;
16859 /* Initialize the modes for builtin_function_type, mapping a machine mode to
16860 tree type node. */
16861 builtin_mode_to_type[QImode][0] = integer_type_node;
16862 builtin_mode_to_type[HImode][0] = integer_type_node;
16863 builtin_mode_to_type[SImode][0] = intSI_type_node;
16864 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
16865 builtin_mode_to_type[DImode][0] = intDI_type_node;
16866 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
16867 builtin_mode_to_type[TImode][0] = intTI_type_node;
16868 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
16869 builtin_mode_to_type[SFmode][0] = float_type_node;
16870 builtin_mode_to_type[DFmode][0] = double_type_node;
16871 builtin_mode_to_type[IFmode][0] = ibm128_float_type_node;
16872 builtin_mode_to_type[KFmode][0] = ieee128_float_type_node;
16873 builtin_mode_to_type[TFmode][0] = long_double_type_node;
16874 builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
16875 builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
16876 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
16877 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
16878 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
16879 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
16880 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
16881 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
16882 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
16883 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
16884 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
16885 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
16886 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
16887 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
16888 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
16889 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
16891 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
16892 TYPE_NAME (bool_char_type_node) = tdecl;
16894 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
16895 TYPE_NAME (bool_short_type_node) = tdecl;
16897 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
16898 TYPE_NAME (bool_int_type_node) = tdecl;
16900 tdecl = add_builtin_type ("__pixel", pixel_type_node);
16901 TYPE_NAME (pixel_type_node) = tdecl;
16903 bool_V16QI_type_node = rs6000_vector_type ("__vector __bool char",
16904 bool_char_type_node, 16);
16905 bool_V8HI_type_node = rs6000_vector_type ("__vector __bool short",
16906 bool_short_type_node, 8);
16907 bool_V4SI_type_node = rs6000_vector_type ("__vector __bool int",
16908 bool_int_type_node, 4);
16909 bool_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
16910 ? "__vector __bool long"
16911 : "__vector __bool long long",
16912 bool_long_type_node, 2);
16913 pixel_V8HI_type_node = rs6000_vector_type ("__vector __pixel",
16914 pixel_type_node, 8);
16916 /* Paired builtins are only available if you build a compiler with the
16917 appropriate options, so only create those builtins with the appropriate
16918 compiler option. Create Altivec and VSX builtins on machines with at
16919 least the general purpose extensions (970 and newer) to allow the use of
16920 the target attribute. */
16921 if (TARGET_PAIRED_FLOAT)
16922 paired_init_builtins ();
16923 if (TARGET_EXTRA_BUILTINS)
16924 altivec_init_builtins ();
16925 if (TARGET_HTM)
16926 htm_init_builtins ();
16928 if (TARGET_EXTRA_BUILTINS || TARGET_PAIRED_FLOAT)
16929 rs6000_common_init_builtins ();
16931 ftype = build_function_type_list (ieee128_float_type_node,
16932 const_str_type_node, NULL_TREE);
16933 def_builtin ("__builtin_nanq", ftype, RS6000_BUILTIN_NANQ);
16934 def_builtin ("__builtin_nansq", ftype, RS6000_BUILTIN_NANSQ);
16936 ftype = build_function_type_list (ieee128_float_type_node, NULL_TREE);
16937 def_builtin ("__builtin_infq", ftype, RS6000_BUILTIN_INFQ);
16938 def_builtin ("__builtin_huge_valq", ftype, RS6000_BUILTIN_HUGE_VALQ);
16940 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
16941 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
16942 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
16944 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
16945 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
16946 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
16948 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
16949 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
16950 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
16952 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
16953 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
16954 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
16956 mode = (TARGET_64BIT) ? DImode : SImode;
16957 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
16958 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
16959 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
16961 ftype = build_function_type_list (unsigned_intDI_type_node,
16962 NULL_TREE);
16963 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
16965 if (TARGET_64BIT)
16966 ftype = build_function_type_list (unsigned_intDI_type_node,
16967 NULL_TREE);
16968 else
16969 ftype = build_function_type_list (unsigned_intSI_type_node,
16970 NULL_TREE);
16971 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
16973 ftype = build_function_type_list (double_type_node, NULL_TREE);
16974 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
16976 ftype = build_function_type_list (void_type_node,
16977 intSI_type_node, double_type_node,
16978 NULL_TREE);
16979 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
16981 ftype = build_function_type_list (void_type_node, NULL_TREE);
16982 def_builtin ("__builtin_cpu_init", ftype, RS6000_BUILTIN_CPU_INIT);
16984 ftype = build_function_type_list (bool_int_type_node, const_ptr_type_node,
16985 NULL_TREE);
16986 def_builtin ("__builtin_cpu_is", ftype, RS6000_BUILTIN_CPU_IS);
16987 def_builtin ("__builtin_cpu_supports", ftype, RS6000_BUILTIN_CPU_SUPPORTS);
16989 /* AIX libm provides clog as __clog. */
16990 if (TARGET_XCOFF &&
16991 (tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
16992 set_user_assembler_name (tdecl, "__clog");
16994 #ifdef SUBTARGET_INIT_BUILTINS
16995 SUBTARGET_INIT_BUILTINS;
16996 #endif
16999 /* Returns the rs6000 builtin decl for CODE. */
17001 static tree
17002 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
17004 HOST_WIDE_INT fnmask;
17006 if (code >= RS6000_BUILTIN_COUNT)
17007 return error_mark_node;
17009 fnmask = rs6000_builtin_info[code].mask;
17010 if ((fnmask & rs6000_builtin_mask) != fnmask)
17012 rs6000_invalid_builtin ((enum rs6000_builtins)code);
17013 return error_mark_node;
17016 return rs6000_builtin_decls[code];
17019 static void
17020 paired_init_builtins (void)
17022 const struct builtin_description *d;
17023 size_t i;
17024 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
17026 tree int_ftype_int_v2sf_v2sf
17027 = build_function_type_list (integer_type_node,
17028 integer_type_node,
17029 V2SF_type_node,
17030 V2SF_type_node,
17031 NULL_TREE);
17032 tree pcfloat_type_node =
17033 build_pointer_type (build_qualified_type
17034 (float_type_node, TYPE_QUAL_CONST));
17036 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
17037 long_integer_type_node,
17038 pcfloat_type_node,
17039 NULL_TREE);
17040 tree void_ftype_v2sf_long_pcfloat =
17041 build_function_type_list (void_type_node,
17042 V2SF_type_node,
17043 long_integer_type_node,
17044 pcfloat_type_node,
17045 NULL_TREE);
17048 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
17049 PAIRED_BUILTIN_LX);
17052 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
17053 PAIRED_BUILTIN_STX);
17055 /* Predicates. */
17056 d = bdesc_paired_preds;
17057 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
17059 tree type;
17060 HOST_WIDE_INT mask = d->mask;
17062 if ((mask & builtin_mask) != mask)
17064 if (TARGET_DEBUG_BUILTIN)
17065 fprintf (stderr, "paired_init_builtins, skip predicate %s\n",
17066 d->name);
17067 continue;
17070 /* Cannot define builtin if the instruction is disabled. */
17071 gcc_assert (d->icode != CODE_FOR_nothing);
17073 if (TARGET_DEBUG_BUILTIN)
17074 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
17075 (int)i, get_insn_name (d->icode), (int)d->icode,
17076 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
17078 switch (insn_data[d->icode].operand[1].mode)
17080 case E_V2SFmode:
17081 type = int_ftype_int_v2sf_v2sf;
17082 break;
17083 default:
17084 gcc_unreachable ();
17087 def_builtin (d->name, type, d->code);
17091 static void
17092 altivec_init_builtins (void)
17094 const struct builtin_description *d;
17095 size_t i;
17096 tree ftype;
17097 tree decl;
17098 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
17100 tree pvoid_type_node = build_pointer_type (void_type_node);
17102 tree pcvoid_type_node
17103 = build_pointer_type (build_qualified_type (void_type_node,
17104 TYPE_QUAL_CONST));
17106 tree int_ftype_opaque
17107 = build_function_type_list (integer_type_node,
17108 opaque_V4SI_type_node, NULL_TREE);
17109 tree opaque_ftype_opaque
17110 = build_function_type_list (integer_type_node, NULL_TREE);
17111 tree opaque_ftype_opaque_int
17112 = build_function_type_list (opaque_V4SI_type_node,
17113 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
17114 tree opaque_ftype_opaque_opaque_int
17115 = build_function_type_list (opaque_V4SI_type_node,
17116 opaque_V4SI_type_node, opaque_V4SI_type_node,
17117 integer_type_node, NULL_TREE);
17118 tree opaque_ftype_opaque_opaque_opaque
17119 = build_function_type_list (opaque_V4SI_type_node,
17120 opaque_V4SI_type_node, opaque_V4SI_type_node,
17121 opaque_V4SI_type_node, NULL_TREE);
17122 tree opaque_ftype_opaque_opaque
17123 = build_function_type_list (opaque_V4SI_type_node,
17124 opaque_V4SI_type_node, opaque_V4SI_type_node,
17125 NULL_TREE);
17126 tree int_ftype_int_opaque_opaque
17127 = build_function_type_list (integer_type_node,
17128 integer_type_node, opaque_V4SI_type_node,
17129 opaque_V4SI_type_node, NULL_TREE);
17130 tree int_ftype_int_v4si_v4si
17131 = build_function_type_list (integer_type_node,
17132 integer_type_node, V4SI_type_node,
17133 V4SI_type_node, NULL_TREE);
17134 tree int_ftype_int_v2di_v2di
17135 = build_function_type_list (integer_type_node,
17136 integer_type_node, V2DI_type_node,
17137 V2DI_type_node, NULL_TREE);
17138 tree void_ftype_v4si
17139 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
17140 tree v8hi_ftype_void
17141 = build_function_type_list (V8HI_type_node, NULL_TREE);
17142 tree void_ftype_void
17143 = build_function_type_list (void_type_node, NULL_TREE);
17144 tree void_ftype_int
17145 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
17147 tree opaque_ftype_long_pcvoid
17148 = build_function_type_list (opaque_V4SI_type_node,
17149 long_integer_type_node, pcvoid_type_node,
17150 NULL_TREE);
17151 tree v16qi_ftype_long_pcvoid
17152 = build_function_type_list (V16QI_type_node,
17153 long_integer_type_node, pcvoid_type_node,
17154 NULL_TREE);
17155 tree v8hi_ftype_long_pcvoid
17156 = build_function_type_list (V8HI_type_node,
17157 long_integer_type_node, pcvoid_type_node,
17158 NULL_TREE);
17159 tree v4si_ftype_long_pcvoid
17160 = build_function_type_list (V4SI_type_node,
17161 long_integer_type_node, pcvoid_type_node,
17162 NULL_TREE);
17163 tree v4sf_ftype_long_pcvoid
17164 = build_function_type_list (V4SF_type_node,
17165 long_integer_type_node, pcvoid_type_node,
17166 NULL_TREE);
17167 tree v2df_ftype_long_pcvoid
17168 = build_function_type_list (V2DF_type_node,
17169 long_integer_type_node, pcvoid_type_node,
17170 NULL_TREE);
17171 tree v2di_ftype_long_pcvoid
17172 = build_function_type_list (V2DI_type_node,
17173 long_integer_type_node, pcvoid_type_node,
17174 NULL_TREE);
17176 tree void_ftype_opaque_long_pvoid
17177 = build_function_type_list (void_type_node,
17178 opaque_V4SI_type_node, long_integer_type_node,
17179 pvoid_type_node, NULL_TREE);
17180 tree void_ftype_v4si_long_pvoid
17181 = build_function_type_list (void_type_node,
17182 V4SI_type_node, long_integer_type_node,
17183 pvoid_type_node, NULL_TREE);
17184 tree void_ftype_v16qi_long_pvoid
17185 = build_function_type_list (void_type_node,
17186 V16QI_type_node, long_integer_type_node,
17187 pvoid_type_node, NULL_TREE);
17189 tree void_ftype_v16qi_pvoid_long
17190 = build_function_type_list (void_type_node,
17191 V16QI_type_node, pvoid_type_node,
17192 long_integer_type_node, NULL_TREE);
17194 tree void_ftype_v8hi_long_pvoid
17195 = build_function_type_list (void_type_node,
17196 V8HI_type_node, long_integer_type_node,
17197 pvoid_type_node, NULL_TREE);
17198 tree void_ftype_v4sf_long_pvoid
17199 = build_function_type_list (void_type_node,
17200 V4SF_type_node, long_integer_type_node,
17201 pvoid_type_node, NULL_TREE);
17202 tree void_ftype_v2df_long_pvoid
17203 = build_function_type_list (void_type_node,
17204 V2DF_type_node, long_integer_type_node,
17205 pvoid_type_node, NULL_TREE);
17206 tree void_ftype_v2di_long_pvoid
17207 = build_function_type_list (void_type_node,
17208 V2DI_type_node, long_integer_type_node,
17209 pvoid_type_node, NULL_TREE);
17210 tree int_ftype_int_v8hi_v8hi
17211 = build_function_type_list (integer_type_node,
17212 integer_type_node, V8HI_type_node,
17213 V8HI_type_node, NULL_TREE);
17214 tree int_ftype_int_v16qi_v16qi
17215 = build_function_type_list (integer_type_node,
17216 integer_type_node, V16QI_type_node,
17217 V16QI_type_node, NULL_TREE);
17218 tree int_ftype_int_v4sf_v4sf
17219 = build_function_type_list (integer_type_node,
17220 integer_type_node, V4SF_type_node,
17221 V4SF_type_node, NULL_TREE);
17222 tree int_ftype_int_v2df_v2df
17223 = build_function_type_list (integer_type_node,
17224 integer_type_node, V2DF_type_node,
17225 V2DF_type_node, NULL_TREE);
17226 tree v2di_ftype_v2di
17227 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
17228 tree v4si_ftype_v4si
17229 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
17230 tree v8hi_ftype_v8hi
17231 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
17232 tree v16qi_ftype_v16qi
17233 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
17234 tree v4sf_ftype_v4sf
17235 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
17236 tree v2df_ftype_v2df
17237 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
17238 tree void_ftype_pcvoid_int_int
17239 = build_function_type_list (void_type_node,
17240 pcvoid_type_node, integer_type_node,
17241 integer_type_node, NULL_TREE);
17243 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
17244 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
17245 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
17246 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
17247 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
17248 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
17249 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
17250 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
17251 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
17252 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
17253 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
17254 ALTIVEC_BUILTIN_LVXL_V2DF);
17255 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
17256 ALTIVEC_BUILTIN_LVXL_V2DI);
17257 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
17258 ALTIVEC_BUILTIN_LVXL_V4SF);
17259 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
17260 ALTIVEC_BUILTIN_LVXL_V4SI);
17261 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
17262 ALTIVEC_BUILTIN_LVXL_V8HI);
17263 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
17264 ALTIVEC_BUILTIN_LVXL_V16QI);
17265 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
17266 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
17267 ALTIVEC_BUILTIN_LVX_V2DF);
17268 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
17269 ALTIVEC_BUILTIN_LVX_V2DI);
17270 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
17271 ALTIVEC_BUILTIN_LVX_V4SF);
17272 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
17273 ALTIVEC_BUILTIN_LVX_V4SI);
17274 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
17275 ALTIVEC_BUILTIN_LVX_V8HI);
17276 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
17277 ALTIVEC_BUILTIN_LVX_V16QI);
17278 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
17279 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
17280 ALTIVEC_BUILTIN_STVX_V2DF);
17281 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
17282 ALTIVEC_BUILTIN_STVX_V2DI);
17283 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
17284 ALTIVEC_BUILTIN_STVX_V4SF);
17285 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
17286 ALTIVEC_BUILTIN_STVX_V4SI);
17287 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
17288 ALTIVEC_BUILTIN_STVX_V8HI);
17289 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
17290 ALTIVEC_BUILTIN_STVX_V16QI);
17291 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
17292 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
17293 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
17294 ALTIVEC_BUILTIN_STVXL_V2DF);
17295 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
17296 ALTIVEC_BUILTIN_STVXL_V2DI);
17297 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
17298 ALTIVEC_BUILTIN_STVXL_V4SF);
17299 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
17300 ALTIVEC_BUILTIN_STVXL_V4SI);
17301 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
17302 ALTIVEC_BUILTIN_STVXL_V8HI);
17303 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
17304 ALTIVEC_BUILTIN_STVXL_V16QI);
17305 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
17306 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
17307 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
17308 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
17309 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
17310 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
17311 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
17312 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
17313 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
17314 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
17315 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
17316 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
17317 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
17318 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
17319 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
17320 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
17322 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
17323 VSX_BUILTIN_LXVD2X_V2DF);
17324 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
17325 VSX_BUILTIN_LXVD2X_V2DI);
17326 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
17327 VSX_BUILTIN_LXVW4X_V4SF);
17328 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
17329 VSX_BUILTIN_LXVW4X_V4SI);
17330 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
17331 VSX_BUILTIN_LXVW4X_V8HI);
17332 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
17333 VSX_BUILTIN_LXVW4X_V16QI);
17334 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
17335 VSX_BUILTIN_STXVD2X_V2DF);
17336 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
17337 VSX_BUILTIN_STXVD2X_V2DI);
17338 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
17339 VSX_BUILTIN_STXVW4X_V4SF);
17340 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
17341 VSX_BUILTIN_STXVW4X_V4SI);
17342 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
17343 VSX_BUILTIN_STXVW4X_V8HI);
17344 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
17345 VSX_BUILTIN_STXVW4X_V16QI);
17347 def_builtin ("__builtin_vsx_ld_elemrev_v2df", v2df_ftype_long_pcvoid,
17348 VSX_BUILTIN_LD_ELEMREV_V2DF);
17349 def_builtin ("__builtin_vsx_ld_elemrev_v2di", v2di_ftype_long_pcvoid,
17350 VSX_BUILTIN_LD_ELEMREV_V2DI);
17351 def_builtin ("__builtin_vsx_ld_elemrev_v4sf", v4sf_ftype_long_pcvoid,
17352 VSX_BUILTIN_LD_ELEMREV_V4SF);
17353 def_builtin ("__builtin_vsx_ld_elemrev_v4si", v4si_ftype_long_pcvoid,
17354 VSX_BUILTIN_LD_ELEMREV_V4SI);
17355 def_builtin ("__builtin_vsx_st_elemrev_v2df", void_ftype_v2df_long_pvoid,
17356 VSX_BUILTIN_ST_ELEMREV_V2DF);
17357 def_builtin ("__builtin_vsx_st_elemrev_v2di", void_ftype_v2di_long_pvoid,
17358 VSX_BUILTIN_ST_ELEMREV_V2DI);
17359 def_builtin ("__builtin_vsx_st_elemrev_v4sf", void_ftype_v4sf_long_pvoid,
17360 VSX_BUILTIN_ST_ELEMREV_V4SF);
17361 def_builtin ("__builtin_vsx_st_elemrev_v4si", void_ftype_v4si_long_pvoid,
17362 VSX_BUILTIN_ST_ELEMREV_V4SI);
17364 def_builtin ("__builtin_vsx_le_be_v8hi", v8hi_ftype_long_pcvoid,
17365 VSX_BUILTIN_XL_BE_V8HI);
17366 def_builtin ("__builtin_vsx_le_be_v4si", v4si_ftype_long_pcvoid,
17367 VSX_BUILTIN_XL_BE_V4SI);
17368 def_builtin ("__builtin_vsx_le_be_v2di", v2di_ftype_long_pcvoid,
17369 VSX_BUILTIN_XL_BE_V2DI);
17370 def_builtin ("__builtin_vsx_le_be_v4sf", v4sf_ftype_long_pcvoid,
17371 VSX_BUILTIN_XL_BE_V4SF);
17372 def_builtin ("__builtin_vsx_le_be_v2df", v2df_ftype_long_pcvoid,
17373 VSX_BUILTIN_XL_BE_V2DF);
17374 def_builtin ("__builtin_vsx_le_be_v16qi", v16qi_ftype_long_pcvoid,
17375 VSX_BUILTIN_XL_BE_V16QI);
17377 if (TARGET_P9_VECTOR)
17379 def_builtin ("__builtin_vsx_ld_elemrev_v8hi", v8hi_ftype_long_pcvoid,
17380 VSX_BUILTIN_LD_ELEMREV_V8HI);
17381 def_builtin ("__builtin_vsx_ld_elemrev_v16qi", v16qi_ftype_long_pcvoid,
17382 VSX_BUILTIN_LD_ELEMREV_V16QI);
17383 def_builtin ("__builtin_vsx_st_elemrev_v8hi",
17384 void_ftype_v8hi_long_pvoid, VSX_BUILTIN_ST_ELEMREV_V8HI);
17385 def_builtin ("__builtin_vsx_st_elemrev_v16qi",
17386 void_ftype_v16qi_long_pvoid, VSX_BUILTIN_ST_ELEMREV_V16QI);
17388 else
17390 rs6000_builtin_decls[(int) VSX_BUILTIN_LD_ELEMREV_V8HI]
17391 = rs6000_builtin_decls[(int) VSX_BUILTIN_LXVW4X_V8HI];
17392 rs6000_builtin_decls[(int) VSX_BUILTIN_LD_ELEMREV_V16QI]
17393 = rs6000_builtin_decls[(int) VSX_BUILTIN_LXVW4X_V16QI];
17394 rs6000_builtin_decls[(int) VSX_BUILTIN_ST_ELEMREV_V8HI]
17395 = rs6000_builtin_decls[(int) VSX_BUILTIN_STXVW4X_V8HI];
17396 rs6000_builtin_decls[(int) VSX_BUILTIN_ST_ELEMREV_V16QI]
17397 = rs6000_builtin_decls[(int) VSX_BUILTIN_STXVW4X_V16QI];
17400 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
17401 VSX_BUILTIN_VEC_LD);
17402 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
17403 VSX_BUILTIN_VEC_ST);
17404 def_builtin ("__builtin_vec_xl", opaque_ftype_long_pcvoid,
17405 VSX_BUILTIN_VEC_XL);
17406 def_builtin ("__builtin_vec_xl_be", opaque_ftype_long_pcvoid,
17407 VSX_BUILTIN_VEC_XL_BE);
17408 def_builtin ("__builtin_vec_xst", void_ftype_opaque_long_pvoid,
17409 VSX_BUILTIN_VEC_XST);
17411 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
17412 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
17413 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
17415 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
17416 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
17417 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
17418 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
17419 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
17420 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
17421 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
17422 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
17423 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
17424 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
17425 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
17426 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
17428 def_builtin ("__builtin_vec_adde", opaque_ftype_opaque_opaque_opaque,
17429 ALTIVEC_BUILTIN_VEC_ADDE);
17430 def_builtin ("__builtin_vec_addec", opaque_ftype_opaque_opaque_opaque,
17431 ALTIVEC_BUILTIN_VEC_ADDEC);
17432 def_builtin ("__builtin_vec_cmpne", opaque_ftype_opaque_opaque,
17433 ALTIVEC_BUILTIN_VEC_CMPNE);
17434 def_builtin ("__builtin_vec_mul", opaque_ftype_opaque_opaque,
17435 ALTIVEC_BUILTIN_VEC_MUL);
17436 def_builtin ("__builtin_vec_sube", opaque_ftype_opaque_opaque_opaque,
17437 ALTIVEC_BUILTIN_VEC_SUBE);
17438 def_builtin ("__builtin_vec_subec", opaque_ftype_opaque_opaque_opaque,
17439 ALTIVEC_BUILTIN_VEC_SUBEC);
17441 /* Cell builtins. */
17442 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
17443 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
17444 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
17445 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
17447 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
17448 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
17449 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
17450 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
17452 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
17453 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
17454 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
17455 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
17457 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
17458 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
17459 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
17460 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
17462 if (TARGET_P9_VECTOR)
17463 def_builtin ("__builtin_altivec_stxvl", void_ftype_v16qi_pvoid_long,
17464 P9V_BUILTIN_STXVL);
17466 /* Add the DST variants. */
17467 d = bdesc_dst;
17468 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
17470 HOST_WIDE_INT mask = d->mask;
17472 /* It is expected that these dst built-in functions may have
17473 d->icode equal to CODE_FOR_nothing. */
17474 if ((mask & builtin_mask) != mask)
17476 if (TARGET_DEBUG_BUILTIN)
17477 fprintf (stderr, "altivec_init_builtins, skip dst %s\n",
17478 d->name);
17479 continue;
17481 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
17484 /* Initialize the predicates. */
17485 d = bdesc_altivec_preds;
17486 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
17488 machine_mode mode1;
17489 tree type;
17490 HOST_WIDE_INT mask = d->mask;
17492 if ((mask & builtin_mask) != mask)
17494 if (TARGET_DEBUG_BUILTIN)
17495 fprintf (stderr, "altivec_init_builtins, skip predicate %s\n",
17496 d->name);
17497 continue;
17500 if (rs6000_overloaded_builtin_p (d->code))
17501 mode1 = VOIDmode;
17502 else
17504 /* Cannot define builtin if the instruction is disabled. */
17505 gcc_assert (d->icode != CODE_FOR_nothing);
17506 mode1 = insn_data[d->icode].operand[1].mode;
17509 switch (mode1)
17511 case E_VOIDmode:
17512 type = int_ftype_int_opaque_opaque;
17513 break;
17514 case E_V2DImode:
17515 type = int_ftype_int_v2di_v2di;
17516 break;
17517 case E_V4SImode:
17518 type = int_ftype_int_v4si_v4si;
17519 break;
17520 case E_V8HImode:
17521 type = int_ftype_int_v8hi_v8hi;
17522 break;
17523 case E_V16QImode:
17524 type = int_ftype_int_v16qi_v16qi;
17525 break;
17526 case E_V4SFmode:
17527 type = int_ftype_int_v4sf_v4sf;
17528 break;
17529 case E_V2DFmode:
17530 type = int_ftype_int_v2df_v2df;
17531 break;
17532 default:
17533 gcc_unreachable ();
17536 def_builtin (d->name, type, d->code);
17539 /* Initialize the abs* operators. */
17540 d = bdesc_abs;
17541 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
17543 machine_mode mode0;
17544 tree type;
17545 HOST_WIDE_INT mask = d->mask;
17547 if ((mask & builtin_mask) != mask)
17549 if (TARGET_DEBUG_BUILTIN)
17550 fprintf (stderr, "altivec_init_builtins, skip abs %s\n",
17551 d->name);
17552 continue;
17555 /* Cannot define builtin if the instruction is disabled. */
17556 gcc_assert (d->icode != CODE_FOR_nothing);
17557 mode0 = insn_data[d->icode].operand[0].mode;
17559 switch (mode0)
17561 case E_V2DImode:
17562 type = v2di_ftype_v2di;
17563 break;
17564 case E_V4SImode:
17565 type = v4si_ftype_v4si;
17566 break;
17567 case E_V8HImode:
17568 type = v8hi_ftype_v8hi;
17569 break;
17570 case E_V16QImode:
17571 type = v16qi_ftype_v16qi;
17572 break;
17573 case E_V4SFmode:
17574 type = v4sf_ftype_v4sf;
17575 break;
17576 case E_V2DFmode:
17577 type = v2df_ftype_v2df;
17578 break;
17579 default:
17580 gcc_unreachable ();
17583 def_builtin (d->name, type, d->code);
17586 /* Initialize target builtin that implements
17587 targetm.vectorize.builtin_mask_for_load. */
17589 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
17590 v16qi_ftype_long_pcvoid,
17591 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
17592 BUILT_IN_MD, NULL, NULL_TREE);
17593 TREE_READONLY (decl) = 1;
17594 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
17595 altivec_builtin_mask_for_load = decl;
17597 /* Access to the vec_init patterns. */
17598 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
17599 integer_type_node, integer_type_node,
17600 integer_type_node, NULL_TREE);
17601 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
17603 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
17604 short_integer_type_node,
17605 short_integer_type_node,
17606 short_integer_type_node,
17607 short_integer_type_node,
17608 short_integer_type_node,
17609 short_integer_type_node,
17610 short_integer_type_node, NULL_TREE);
17611 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
17613 ftype = build_function_type_list (V16QI_type_node, char_type_node,
17614 char_type_node, char_type_node,
17615 char_type_node, char_type_node,
17616 char_type_node, char_type_node,
17617 char_type_node, char_type_node,
17618 char_type_node, char_type_node,
17619 char_type_node, char_type_node,
17620 char_type_node, char_type_node,
17621 char_type_node, NULL_TREE);
17622 def_builtin ("__builtin_vec_init_v16qi", ftype,
17623 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
17625 ftype = build_function_type_list (V4SF_type_node, float_type_node,
17626 float_type_node, float_type_node,
17627 float_type_node, NULL_TREE);
17628 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
17630 /* VSX builtins. */
17631 ftype = build_function_type_list (V2DF_type_node, double_type_node,
17632 double_type_node, NULL_TREE);
17633 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
17635 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
17636 intDI_type_node, NULL_TREE);
17637 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
17639 /* Access to the vec_set patterns. */
17640 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
17641 intSI_type_node,
17642 integer_type_node, NULL_TREE);
17643 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
17645 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
17646 intHI_type_node,
17647 integer_type_node, NULL_TREE);
17648 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
17650 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
17651 intQI_type_node,
17652 integer_type_node, NULL_TREE);
17653 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
17655 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
17656 float_type_node,
17657 integer_type_node, NULL_TREE);
17658 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
17660 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
17661 double_type_node,
17662 integer_type_node, NULL_TREE);
17663 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
17665 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
17666 intDI_type_node,
17667 integer_type_node, NULL_TREE);
17668 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
17670 /* Access to the vec_extract patterns. */
17671 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
17672 integer_type_node, NULL_TREE);
17673 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
17675 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
17676 integer_type_node, NULL_TREE);
17677 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
17679 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
17680 integer_type_node, NULL_TREE);
17681 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
17683 ftype = build_function_type_list (float_type_node, V4SF_type_node,
17684 integer_type_node, NULL_TREE);
17685 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
17687 ftype = build_function_type_list (double_type_node, V2DF_type_node,
17688 integer_type_node, NULL_TREE);
17689 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
17691 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
17692 integer_type_node, NULL_TREE);
17693 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
17696 if (V1TI_type_node)
17698 tree v1ti_ftype_long_pcvoid
17699 = build_function_type_list (V1TI_type_node,
17700 long_integer_type_node, pcvoid_type_node,
17701 NULL_TREE);
17702 tree void_ftype_v1ti_long_pvoid
17703 = build_function_type_list (void_type_node,
17704 V1TI_type_node, long_integer_type_node,
17705 pvoid_type_node, NULL_TREE);
17706 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
17707 VSX_BUILTIN_LXVD2X_V1TI);
17708 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
17709 VSX_BUILTIN_STXVD2X_V1TI);
17710 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
17711 NULL_TREE, NULL_TREE);
17712 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
17713 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
17714 intTI_type_node,
17715 integer_type_node, NULL_TREE);
17716 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
17717 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
17718 integer_type_node, NULL_TREE);
17719 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
17724 static void
17725 htm_init_builtins (void)
17727 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
17728 const struct builtin_description *d;
17729 size_t i;
17731 d = bdesc_htm;
17732 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
17734 tree op[MAX_HTM_OPERANDS], type;
17735 HOST_WIDE_INT mask = d->mask;
17736 unsigned attr = rs6000_builtin_info[d->code].attr;
17737 bool void_func = (attr & RS6000_BTC_VOID);
17738 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
17739 int nopnds = 0;
17740 tree gpr_type_node;
17741 tree rettype;
17742 tree argtype;
17744 /* It is expected that these htm built-in functions may have
17745 d->icode equal to CODE_FOR_nothing. */
17747 if (TARGET_32BIT && TARGET_POWERPC64)
17748 gpr_type_node = long_long_unsigned_type_node;
17749 else
17750 gpr_type_node = long_unsigned_type_node;
17752 if (attr & RS6000_BTC_SPR)
17754 rettype = gpr_type_node;
17755 argtype = gpr_type_node;
17757 else if (d->code == HTM_BUILTIN_TABORTDC
17758 || d->code == HTM_BUILTIN_TABORTDCI)
17760 rettype = unsigned_type_node;
17761 argtype = gpr_type_node;
17763 else
17765 rettype = unsigned_type_node;
17766 argtype = unsigned_type_node;
17769 if ((mask & builtin_mask) != mask)
17771 if (TARGET_DEBUG_BUILTIN)
17772 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
17773 continue;
17776 if (d->name == 0)
17778 if (TARGET_DEBUG_BUILTIN)
17779 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
17780 (long unsigned) i);
17781 continue;
17784 op[nopnds++] = (void_func) ? void_type_node : rettype;
17786 if (attr_args == RS6000_BTC_UNARY)
17787 op[nopnds++] = argtype;
17788 else if (attr_args == RS6000_BTC_BINARY)
17790 op[nopnds++] = argtype;
17791 op[nopnds++] = argtype;
17793 else if (attr_args == RS6000_BTC_TERNARY)
17795 op[nopnds++] = argtype;
17796 op[nopnds++] = argtype;
17797 op[nopnds++] = argtype;
17800 switch (nopnds)
17802 case 1:
17803 type = build_function_type_list (op[0], NULL_TREE);
17804 break;
17805 case 2:
17806 type = build_function_type_list (op[0], op[1], NULL_TREE);
17807 break;
17808 case 3:
17809 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
17810 break;
17811 case 4:
17812 type = build_function_type_list (op[0], op[1], op[2], op[3],
17813 NULL_TREE);
17814 break;
17815 default:
17816 gcc_unreachable ();
17819 def_builtin (d->name, type, d->code);
17823 /* Hash function for builtin functions with up to 3 arguments and a return
17824 type. */
17825 hashval_t
17826 builtin_hasher::hash (builtin_hash_struct *bh)
17828 unsigned ret = 0;
17829 int i;
17831 for (i = 0; i < 4; i++)
17833 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
17834 ret = (ret * 2) + bh->uns_p[i];
17837 return ret;
17840 /* Compare builtin hash entries H1 and H2 for equivalence. */
17841 bool
17842 builtin_hasher::equal (builtin_hash_struct *p1, builtin_hash_struct *p2)
17844 return ((p1->mode[0] == p2->mode[0])
17845 && (p1->mode[1] == p2->mode[1])
17846 && (p1->mode[2] == p2->mode[2])
17847 && (p1->mode[3] == p2->mode[3])
17848 && (p1->uns_p[0] == p2->uns_p[0])
17849 && (p1->uns_p[1] == p2->uns_p[1])
17850 && (p1->uns_p[2] == p2->uns_p[2])
17851 && (p1->uns_p[3] == p2->uns_p[3]));
17854 /* Map types for builtin functions with an explicit return type and up to 3
17855 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
17856 of the argument. */
17857 static tree
17858 builtin_function_type (machine_mode mode_ret, machine_mode mode_arg0,
17859 machine_mode mode_arg1, machine_mode mode_arg2,
17860 enum rs6000_builtins builtin, const char *name)
17862 struct builtin_hash_struct h;
17863 struct builtin_hash_struct *h2;
17864 int num_args = 3;
17865 int i;
17866 tree ret_type = NULL_TREE;
17867 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
17869 /* Create builtin_hash_table. */
17870 if (builtin_hash_table == NULL)
17871 builtin_hash_table = hash_table<builtin_hasher>::create_ggc (1500);
17873 h.type = NULL_TREE;
17874 h.mode[0] = mode_ret;
17875 h.mode[1] = mode_arg0;
17876 h.mode[2] = mode_arg1;
17877 h.mode[3] = mode_arg2;
17878 h.uns_p[0] = 0;
17879 h.uns_p[1] = 0;
17880 h.uns_p[2] = 0;
17881 h.uns_p[3] = 0;
17883 /* If the builtin is a type that produces unsigned results or takes unsigned
17884 arguments, and it is returned as a decl for the vectorizer (such as
17885 widening multiplies, permute), make sure the arguments and return value
17886 are type correct. */
17887 switch (builtin)
17889 /* unsigned 1 argument functions. */
17890 case CRYPTO_BUILTIN_VSBOX:
17891 case P8V_BUILTIN_VGBBD:
17892 case MISC_BUILTIN_CDTBCD:
17893 case MISC_BUILTIN_CBCDTD:
17894 h.uns_p[0] = 1;
17895 h.uns_p[1] = 1;
17896 break;
17898 /* unsigned 2 argument functions. */
17899 case ALTIVEC_BUILTIN_VMULEUB:
17900 case ALTIVEC_BUILTIN_VMULEUH:
17901 case ALTIVEC_BUILTIN_VMULEUW:
17902 case ALTIVEC_BUILTIN_VMULOUB:
17903 case ALTIVEC_BUILTIN_VMULOUH:
17904 case ALTIVEC_BUILTIN_VMULOUW:
17905 case CRYPTO_BUILTIN_VCIPHER:
17906 case CRYPTO_BUILTIN_VCIPHERLAST:
17907 case CRYPTO_BUILTIN_VNCIPHER:
17908 case CRYPTO_BUILTIN_VNCIPHERLAST:
17909 case CRYPTO_BUILTIN_VPMSUMB:
17910 case CRYPTO_BUILTIN_VPMSUMH:
17911 case CRYPTO_BUILTIN_VPMSUMW:
17912 case CRYPTO_BUILTIN_VPMSUMD:
17913 case CRYPTO_BUILTIN_VPMSUM:
17914 case MISC_BUILTIN_ADDG6S:
17915 case MISC_BUILTIN_DIVWEU:
17916 case MISC_BUILTIN_DIVWEUO:
17917 case MISC_BUILTIN_DIVDEU:
17918 case MISC_BUILTIN_DIVDEUO:
17919 case VSX_BUILTIN_UDIV_V2DI:
17920 case ALTIVEC_BUILTIN_VMAXUB:
17921 case ALTIVEC_BUILTIN_VMINUB:
17922 case ALTIVEC_BUILTIN_VMAXUH:
17923 case ALTIVEC_BUILTIN_VMINUH:
17924 case ALTIVEC_BUILTIN_VMAXUW:
17925 case ALTIVEC_BUILTIN_VMINUW:
17926 case P8V_BUILTIN_VMAXUD:
17927 case P8V_BUILTIN_VMINUD:
17928 h.uns_p[0] = 1;
17929 h.uns_p[1] = 1;
17930 h.uns_p[2] = 1;
17931 break;
17933 /* unsigned 3 argument functions. */
17934 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
17935 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
17936 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
17937 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
17938 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
17939 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
17940 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
17941 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
17942 case VSX_BUILTIN_VPERM_16QI_UNS:
17943 case VSX_BUILTIN_VPERM_8HI_UNS:
17944 case VSX_BUILTIN_VPERM_4SI_UNS:
17945 case VSX_BUILTIN_VPERM_2DI_UNS:
17946 case VSX_BUILTIN_XXSEL_16QI_UNS:
17947 case VSX_BUILTIN_XXSEL_8HI_UNS:
17948 case VSX_BUILTIN_XXSEL_4SI_UNS:
17949 case VSX_BUILTIN_XXSEL_2DI_UNS:
17950 case CRYPTO_BUILTIN_VPERMXOR:
17951 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
17952 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
17953 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
17954 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
17955 case CRYPTO_BUILTIN_VSHASIGMAW:
17956 case CRYPTO_BUILTIN_VSHASIGMAD:
17957 case CRYPTO_BUILTIN_VSHASIGMA:
17958 h.uns_p[0] = 1;
17959 h.uns_p[1] = 1;
17960 h.uns_p[2] = 1;
17961 h.uns_p[3] = 1;
17962 break;
17964 /* signed permute functions with unsigned char mask. */
17965 case ALTIVEC_BUILTIN_VPERM_16QI:
17966 case ALTIVEC_BUILTIN_VPERM_8HI:
17967 case ALTIVEC_BUILTIN_VPERM_4SI:
17968 case ALTIVEC_BUILTIN_VPERM_4SF:
17969 case ALTIVEC_BUILTIN_VPERM_2DI:
17970 case ALTIVEC_BUILTIN_VPERM_2DF:
17971 case VSX_BUILTIN_VPERM_16QI:
17972 case VSX_BUILTIN_VPERM_8HI:
17973 case VSX_BUILTIN_VPERM_4SI:
17974 case VSX_BUILTIN_VPERM_4SF:
17975 case VSX_BUILTIN_VPERM_2DI:
17976 case VSX_BUILTIN_VPERM_2DF:
17977 h.uns_p[3] = 1;
17978 break;
17980 /* unsigned args, signed return. */
17981 case VSX_BUILTIN_XVCVUXDSP:
17982 case VSX_BUILTIN_XVCVUXDDP_UNS:
17983 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
17984 h.uns_p[1] = 1;
17985 break;
17987 /* signed args, unsigned return. */
17988 case VSX_BUILTIN_XVCVDPUXDS_UNS:
17989 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
17990 case MISC_BUILTIN_UNPACK_TD:
17991 case MISC_BUILTIN_UNPACK_V1TI:
17992 h.uns_p[0] = 1;
17993 break;
17995 /* unsigned arguments for 128-bit pack instructions. */
17996 case MISC_BUILTIN_PACK_TD:
17997 case MISC_BUILTIN_PACK_V1TI:
17998 h.uns_p[1] = 1;
17999 h.uns_p[2] = 1;
18000 break;
18002 /* unsigned second arguments (vector shift right). */
18003 case ALTIVEC_BUILTIN_VSRB:
18004 case ALTIVEC_BUILTIN_VSRH:
18005 case ALTIVEC_BUILTIN_VSRW:
18006 case P8V_BUILTIN_VSRD:
18007 h.uns_p[2] = 1;
18008 break;
18010 default:
18011 break;
18014 /* Figure out how many args are present. */
18015 while (num_args > 0 && h.mode[num_args] == VOIDmode)
18016 num_args--;
18018 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
18019 if (!ret_type && h.uns_p[0])
18020 ret_type = builtin_mode_to_type[h.mode[0]][0];
18022 if (!ret_type)
18023 fatal_error (input_location,
18024 "internal error: builtin function %qs had an unexpected "
18025 "return type %qs", name, GET_MODE_NAME (h.mode[0]));
18027 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
18028 arg_type[i] = NULL_TREE;
18030 for (i = 0; i < num_args; i++)
18032 int m = (int) h.mode[i+1];
18033 int uns_p = h.uns_p[i+1];
18035 arg_type[i] = builtin_mode_to_type[m][uns_p];
18036 if (!arg_type[i] && uns_p)
18037 arg_type[i] = builtin_mode_to_type[m][0];
18039 if (!arg_type[i])
18040 fatal_error (input_location,
18041 "internal error: builtin function %qs, argument %d "
18042 "had unexpected argument type %qs", name, i,
18043 GET_MODE_NAME (m));
18046 builtin_hash_struct **found = builtin_hash_table->find_slot (&h, INSERT);
18047 if (*found == NULL)
18049 h2 = ggc_alloc<builtin_hash_struct> ();
18050 *h2 = h;
18051 *found = h2;
18053 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
18054 arg_type[2], NULL_TREE);
18057 return (*found)->type;
18060 static void
18061 rs6000_common_init_builtins (void)
18063 const struct builtin_description *d;
18064 size_t i;
18066 tree opaque_ftype_opaque = NULL_TREE;
18067 tree opaque_ftype_opaque_opaque = NULL_TREE;
18068 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
18069 tree v2si_ftype = NULL_TREE;
18070 tree v2si_ftype_qi = NULL_TREE;
18071 tree v2si_ftype_v2si_qi = NULL_TREE;
18072 tree v2si_ftype_int_qi = NULL_TREE;
18073 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
18075 if (!TARGET_PAIRED_FLOAT)
18077 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
18078 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
18081 /* Paired builtins are only available if you build a compiler with the
18082 appropriate options, so only create those builtins with the appropriate
18083 compiler option. Create Altivec and VSX builtins on machines with at
18084 least the general purpose extensions (970 and newer) to allow the use of
18085 the target attribute.. */
18087 if (TARGET_EXTRA_BUILTINS)
18088 builtin_mask |= RS6000_BTM_COMMON;
18090 /* Add the ternary operators. */
18091 d = bdesc_3arg;
18092 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
18094 tree type;
18095 HOST_WIDE_INT mask = d->mask;
18097 if ((mask & builtin_mask) != mask)
18099 if (TARGET_DEBUG_BUILTIN)
18100 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
18101 continue;
18104 if (rs6000_overloaded_builtin_p (d->code))
18106 if (! (type = opaque_ftype_opaque_opaque_opaque))
18107 type = opaque_ftype_opaque_opaque_opaque
18108 = build_function_type_list (opaque_V4SI_type_node,
18109 opaque_V4SI_type_node,
18110 opaque_V4SI_type_node,
18111 opaque_V4SI_type_node,
18112 NULL_TREE);
18114 else
18116 enum insn_code icode = d->icode;
18117 if (d->name == 0)
18119 if (TARGET_DEBUG_BUILTIN)
18120 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
18121 (long unsigned)i);
18123 continue;
18126 if (icode == CODE_FOR_nothing)
18128 if (TARGET_DEBUG_BUILTIN)
18129 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
18130 d->name);
18132 continue;
18135 type = builtin_function_type (insn_data[icode].operand[0].mode,
18136 insn_data[icode].operand[1].mode,
18137 insn_data[icode].operand[2].mode,
18138 insn_data[icode].operand[3].mode,
18139 d->code, d->name);
18142 def_builtin (d->name, type, d->code);
18145 /* Add the binary operators. */
18146 d = bdesc_2arg;
18147 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
18149 machine_mode mode0, mode1, mode2;
18150 tree type;
18151 HOST_WIDE_INT mask = d->mask;
18153 if ((mask & builtin_mask) != mask)
18155 if (TARGET_DEBUG_BUILTIN)
18156 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
18157 continue;
18160 if (rs6000_overloaded_builtin_p (d->code))
18162 if (! (type = opaque_ftype_opaque_opaque))
18163 type = opaque_ftype_opaque_opaque
18164 = build_function_type_list (opaque_V4SI_type_node,
18165 opaque_V4SI_type_node,
18166 opaque_V4SI_type_node,
18167 NULL_TREE);
18169 else
18171 enum insn_code icode = d->icode;
18172 if (d->name == 0)
18174 if (TARGET_DEBUG_BUILTIN)
18175 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
18176 (long unsigned)i);
18178 continue;
18181 if (icode == CODE_FOR_nothing)
18183 if (TARGET_DEBUG_BUILTIN)
18184 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
18185 d->name);
18187 continue;
18190 mode0 = insn_data[icode].operand[0].mode;
18191 mode1 = insn_data[icode].operand[1].mode;
18192 mode2 = insn_data[icode].operand[2].mode;
18194 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
18196 if (! (type = v2si_ftype_v2si_qi))
18197 type = v2si_ftype_v2si_qi
18198 = build_function_type_list (opaque_V2SI_type_node,
18199 opaque_V2SI_type_node,
18200 char_type_node,
18201 NULL_TREE);
18204 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
18205 && mode2 == QImode)
18207 if (! (type = v2si_ftype_int_qi))
18208 type = v2si_ftype_int_qi
18209 = build_function_type_list (opaque_V2SI_type_node,
18210 integer_type_node,
18211 char_type_node,
18212 NULL_TREE);
18215 else
18216 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
18217 d->code, d->name);
18220 def_builtin (d->name, type, d->code);
18223 /* Add the simple unary operators. */
18224 d = bdesc_1arg;
18225 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
18227 machine_mode mode0, mode1;
18228 tree type;
18229 HOST_WIDE_INT mask = d->mask;
18231 if ((mask & builtin_mask) != mask)
18233 if (TARGET_DEBUG_BUILTIN)
18234 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
18235 continue;
18238 if (rs6000_overloaded_builtin_p (d->code))
18240 if (! (type = opaque_ftype_opaque))
18241 type = opaque_ftype_opaque
18242 = build_function_type_list (opaque_V4SI_type_node,
18243 opaque_V4SI_type_node,
18244 NULL_TREE);
18246 else
18248 enum insn_code icode = d->icode;
18249 if (d->name == 0)
18251 if (TARGET_DEBUG_BUILTIN)
18252 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
18253 (long unsigned)i);
18255 continue;
18258 if (icode == CODE_FOR_nothing)
18260 if (TARGET_DEBUG_BUILTIN)
18261 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
18262 d->name);
18264 continue;
18267 mode0 = insn_data[icode].operand[0].mode;
18268 mode1 = insn_data[icode].operand[1].mode;
18270 if (mode0 == V2SImode && mode1 == QImode)
18272 if (! (type = v2si_ftype_qi))
18273 type = v2si_ftype_qi
18274 = build_function_type_list (opaque_V2SI_type_node,
18275 char_type_node,
18276 NULL_TREE);
18279 else
18280 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
18281 d->code, d->name);
18284 def_builtin (d->name, type, d->code);
18287 /* Add the simple no-argument operators. */
18288 d = bdesc_0arg;
18289 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
18291 machine_mode mode0;
18292 tree type;
18293 HOST_WIDE_INT mask = d->mask;
18295 if ((mask & builtin_mask) != mask)
18297 if (TARGET_DEBUG_BUILTIN)
18298 fprintf (stderr, "rs6000_builtin, skip no-argument %s\n", d->name);
18299 continue;
18301 if (rs6000_overloaded_builtin_p (d->code))
18303 if (!opaque_ftype_opaque)
18304 opaque_ftype_opaque
18305 = build_function_type_list (opaque_V4SI_type_node, NULL_TREE);
18306 type = opaque_ftype_opaque;
18308 else
18310 enum insn_code icode = d->icode;
18311 if (d->name == 0)
18313 if (TARGET_DEBUG_BUILTIN)
18314 fprintf (stderr, "rs6000_builtin, bdesc_0arg[%lu] no name\n",
18315 (long unsigned) i);
18316 continue;
18318 if (icode == CODE_FOR_nothing)
18320 if (TARGET_DEBUG_BUILTIN)
18321 fprintf (stderr,
18322 "rs6000_builtin, skip no-argument %s (no code)\n",
18323 d->name);
18324 continue;
18326 mode0 = insn_data[icode].operand[0].mode;
18327 if (mode0 == V2SImode)
18329 /* code for paired single */
18330 if (! (type = v2si_ftype))
18332 v2si_ftype
18333 = build_function_type_list (opaque_V2SI_type_node,
18334 NULL_TREE);
18335 type = v2si_ftype;
18338 else
18339 type = builtin_function_type (mode0, VOIDmode, VOIDmode, VOIDmode,
18340 d->code, d->name);
18342 def_builtin (d->name, type, d->code);
18346 /* Set up AIX/Darwin/64-bit Linux quad floating point routines. */
18347 static void
18348 init_float128_ibm (machine_mode mode)
18350 if (!TARGET_XL_COMPAT)
18352 set_optab_libfunc (add_optab, mode, "__gcc_qadd");
18353 set_optab_libfunc (sub_optab, mode, "__gcc_qsub");
18354 set_optab_libfunc (smul_optab, mode, "__gcc_qmul");
18355 set_optab_libfunc (sdiv_optab, mode, "__gcc_qdiv");
18357 if (!TARGET_HARD_FLOAT)
18359 set_optab_libfunc (neg_optab, mode, "__gcc_qneg");
18360 set_optab_libfunc (eq_optab, mode, "__gcc_qeq");
18361 set_optab_libfunc (ne_optab, mode, "__gcc_qne");
18362 set_optab_libfunc (gt_optab, mode, "__gcc_qgt");
18363 set_optab_libfunc (ge_optab, mode, "__gcc_qge");
18364 set_optab_libfunc (lt_optab, mode, "__gcc_qlt");
18365 set_optab_libfunc (le_optab, mode, "__gcc_qle");
18366 set_optab_libfunc (unord_optab, mode, "__gcc_qunord");
18368 set_conv_libfunc (sext_optab, mode, SFmode, "__gcc_stoq");
18369 set_conv_libfunc (sext_optab, mode, DFmode, "__gcc_dtoq");
18370 set_conv_libfunc (trunc_optab, SFmode, mode, "__gcc_qtos");
18371 set_conv_libfunc (trunc_optab, DFmode, mode, "__gcc_qtod");
18372 set_conv_libfunc (sfix_optab, SImode, mode, "__gcc_qtoi");
18373 set_conv_libfunc (ufix_optab, SImode, mode, "__gcc_qtou");
18374 set_conv_libfunc (sfloat_optab, mode, SImode, "__gcc_itoq");
18375 set_conv_libfunc (ufloat_optab, mode, SImode, "__gcc_utoq");
18378 else
18380 set_optab_libfunc (add_optab, mode, "_xlqadd");
18381 set_optab_libfunc (sub_optab, mode, "_xlqsub");
18382 set_optab_libfunc (smul_optab, mode, "_xlqmul");
18383 set_optab_libfunc (sdiv_optab, mode, "_xlqdiv");
18386 /* Add various conversions for IFmode to use the traditional TFmode
18387 names. */
18388 if (mode == IFmode)
18390 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdtf2");
18391 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddtf2");
18392 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctftd2");
18393 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunctfsd2");
18394 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunctfdd2");
18395 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtdtf2");
18397 if (TARGET_POWERPC64)
18399 set_conv_libfunc (sfix_optab, TImode, mode, "__fixtfti");
18400 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunstfti");
18401 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattitf");
18402 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntitf");
18407 /* Set up IEEE 128-bit floating point routines. Use different names if the
18408 arguments can be passed in a vector register. The historical PowerPC
18409 implementation of IEEE 128-bit floating point used _q_<op> for the names, so
18410 continue to use that if we aren't using vector registers to pass IEEE
18411 128-bit floating point. */
18413 static void
18414 init_float128_ieee (machine_mode mode)
18416 if (FLOAT128_VECTOR_P (mode))
18418 set_optab_libfunc (add_optab, mode, "__addkf3");
18419 set_optab_libfunc (sub_optab, mode, "__subkf3");
18420 set_optab_libfunc (neg_optab, mode, "__negkf2");
18421 set_optab_libfunc (smul_optab, mode, "__mulkf3");
18422 set_optab_libfunc (sdiv_optab, mode, "__divkf3");
18423 set_optab_libfunc (sqrt_optab, mode, "__sqrtkf2");
18424 set_optab_libfunc (abs_optab, mode, "__abstkf2");
18426 set_optab_libfunc (eq_optab, mode, "__eqkf2");
18427 set_optab_libfunc (ne_optab, mode, "__nekf2");
18428 set_optab_libfunc (gt_optab, mode, "__gtkf2");
18429 set_optab_libfunc (ge_optab, mode, "__gekf2");
18430 set_optab_libfunc (lt_optab, mode, "__ltkf2");
18431 set_optab_libfunc (le_optab, mode, "__lekf2");
18432 set_optab_libfunc (unord_optab, mode, "__unordkf2");
18434 set_conv_libfunc (sext_optab, mode, SFmode, "__extendsfkf2");
18435 set_conv_libfunc (sext_optab, mode, DFmode, "__extenddfkf2");
18436 set_conv_libfunc (trunc_optab, SFmode, mode, "__trunckfsf2");
18437 set_conv_libfunc (trunc_optab, DFmode, mode, "__trunckfdf2");
18439 set_conv_libfunc (sext_optab, mode, IFmode, "__extendtfkf2");
18440 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
18441 set_conv_libfunc (sext_optab, mode, TFmode, "__extendtfkf2");
18443 set_conv_libfunc (trunc_optab, IFmode, mode, "__trunckftf2");
18444 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
18445 set_conv_libfunc (trunc_optab, TFmode, mode, "__trunckftf2");
18447 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdkf2");
18448 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddkf2");
18449 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunckftd2");
18450 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunckfsd2");
18451 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunckfdd2");
18452 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtdkf2");
18454 set_conv_libfunc (sfix_optab, SImode, mode, "__fixkfsi");
18455 set_conv_libfunc (ufix_optab, SImode, mode, "__fixunskfsi");
18456 set_conv_libfunc (sfix_optab, DImode, mode, "__fixkfdi");
18457 set_conv_libfunc (ufix_optab, DImode, mode, "__fixunskfdi");
18459 set_conv_libfunc (sfloat_optab, mode, SImode, "__floatsikf");
18460 set_conv_libfunc (ufloat_optab, mode, SImode, "__floatunsikf");
18461 set_conv_libfunc (sfloat_optab, mode, DImode, "__floatdikf");
18462 set_conv_libfunc (ufloat_optab, mode, DImode, "__floatundikf");
18464 if (TARGET_POWERPC64)
18466 set_conv_libfunc (sfix_optab, TImode, mode, "__fixkfti");
18467 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunskfti");
18468 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattikf");
18469 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntikf");
18473 else
18475 set_optab_libfunc (add_optab, mode, "_q_add");
18476 set_optab_libfunc (sub_optab, mode, "_q_sub");
18477 set_optab_libfunc (neg_optab, mode, "_q_neg");
18478 set_optab_libfunc (smul_optab, mode, "_q_mul");
18479 set_optab_libfunc (sdiv_optab, mode, "_q_div");
18480 if (TARGET_PPC_GPOPT)
18481 set_optab_libfunc (sqrt_optab, mode, "_q_sqrt");
18483 set_optab_libfunc (eq_optab, mode, "_q_feq");
18484 set_optab_libfunc (ne_optab, mode, "_q_fne");
18485 set_optab_libfunc (gt_optab, mode, "_q_fgt");
18486 set_optab_libfunc (ge_optab, mode, "_q_fge");
18487 set_optab_libfunc (lt_optab, mode, "_q_flt");
18488 set_optab_libfunc (le_optab, mode, "_q_fle");
18490 set_conv_libfunc (sext_optab, mode, SFmode, "_q_stoq");
18491 set_conv_libfunc (sext_optab, mode, DFmode, "_q_dtoq");
18492 set_conv_libfunc (trunc_optab, SFmode, mode, "_q_qtos");
18493 set_conv_libfunc (trunc_optab, DFmode, mode, "_q_qtod");
18494 set_conv_libfunc (sfix_optab, SImode, mode, "_q_qtoi");
18495 set_conv_libfunc (ufix_optab, SImode, mode, "_q_qtou");
18496 set_conv_libfunc (sfloat_optab, mode, SImode, "_q_itoq");
18497 set_conv_libfunc (ufloat_optab, mode, SImode, "_q_utoq");
18501 static void
18502 rs6000_init_libfuncs (void)
18504 /* __float128 support. */
18505 if (TARGET_FLOAT128_TYPE)
18507 init_float128_ibm (IFmode);
18508 init_float128_ieee (KFmode);
18511 /* AIX/Darwin/64-bit Linux quad floating point routines. */
18512 if (TARGET_LONG_DOUBLE_128)
18514 if (!TARGET_IEEEQUAD)
18515 init_float128_ibm (TFmode);
18517 /* IEEE 128-bit including 32-bit SVR4 quad floating point routines. */
18518 else
18519 init_float128_ieee (TFmode);
18523 /* Emit a potentially record-form instruction, setting DST from SRC.
18524 If DOT is 0, that is all; otherwise, set CCREG to the result of the
18525 signed comparison of DST with zero. If DOT is 1, the generated RTL
18526 doesn't care about the DST result; if DOT is 2, it does. If CCREG
18527 is CR0 do a single dot insn (as a PARALLEL); otherwise, do a SET and
18528 a separate COMPARE. */
18530 void
18531 rs6000_emit_dot_insn (rtx dst, rtx src, int dot, rtx ccreg)
18533 if (dot == 0)
18535 emit_move_insn (dst, src);
18536 return;
18539 if (cc_reg_not_cr0_operand (ccreg, CCmode))
18541 emit_move_insn (dst, src);
18542 emit_move_insn (ccreg, gen_rtx_COMPARE (CCmode, dst, const0_rtx));
18543 return;
18546 rtx ccset = gen_rtx_SET (ccreg, gen_rtx_COMPARE (CCmode, src, const0_rtx));
18547 if (dot == 1)
18549 rtx clobber = gen_rtx_CLOBBER (VOIDmode, dst);
18550 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, clobber)));
18552 else
18554 rtx set = gen_rtx_SET (dst, src);
18555 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, set)));
18560 /* A validation routine: say whether CODE, a condition code, and MODE
18561 match. The other alternatives either don't make sense or should
18562 never be generated. */
18564 void
18565 validate_condition_mode (enum rtx_code code, machine_mode mode)
18567 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
18568 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
18569 && GET_MODE_CLASS (mode) == MODE_CC);
18571 /* These don't make sense. */
18572 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
18573 || mode != CCUNSmode);
18575 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
18576 || mode == CCUNSmode);
18578 gcc_assert (mode == CCFPmode
18579 || (code != ORDERED && code != UNORDERED
18580 && code != UNEQ && code != LTGT
18581 && code != UNGT && code != UNLT
18582 && code != UNGE && code != UNLE));
18584 /* These should never be generated except for
18585 flag_finite_math_only. */
18586 gcc_assert (mode != CCFPmode
18587 || flag_finite_math_only
18588 || (code != LE && code != GE
18589 && code != UNEQ && code != LTGT
18590 && code != UNGT && code != UNLT));
18592 /* These are invalid; the information is not there. */
18593 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
18597 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm,
18598 rldicl, rldicr, or rldic instruction in mode MODE. If so, if E is
18599 not zero, store there the bit offset (counted from the right) where
18600 the single stretch of 1 bits begins; and similarly for B, the bit
18601 offset where it ends. */
18603 bool
18604 rs6000_is_valid_mask (rtx mask, int *b, int *e, machine_mode mode)
18606 unsigned HOST_WIDE_INT val = INTVAL (mask);
18607 unsigned HOST_WIDE_INT bit;
18608 int nb, ne;
18609 int n = GET_MODE_PRECISION (mode);
18611 if (mode != DImode && mode != SImode)
18612 return false;
18614 if (INTVAL (mask) >= 0)
18616 bit = val & -val;
18617 ne = exact_log2 (bit);
18618 nb = exact_log2 (val + bit);
18620 else if (val + 1 == 0)
18622 nb = n;
18623 ne = 0;
18625 else if (val & 1)
18627 val = ~val;
18628 bit = val & -val;
18629 nb = exact_log2 (bit);
18630 ne = exact_log2 (val + bit);
18632 else
18634 bit = val & -val;
18635 ne = exact_log2 (bit);
18636 if (val + bit == 0)
18637 nb = n;
18638 else
18639 nb = 0;
18642 nb--;
18644 if (nb < 0 || ne < 0 || nb >= n || ne >= n)
18645 return false;
18647 if (b)
18648 *b = nb;
18649 if (e)
18650 *e = ne;
18652 return true;
18655 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm, rldicl,
18656 or rldicr instruction, to implement an AND with it in mode MODE. */
18658 bool
18659 rs6000_is_valid_and_mask (rtx mask, machine_mode mode)
18661 int nb, ne;
18663 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
18664 return false;
18666 /* For DImode, we need a rldicl, rldicr, or a rlwinm with mask that
18667 does not wrap. */
18668 if (mode == DImode)
18669 return (ne == 0 || nb == 63 || (nb < 32 && ne <= nb));
18671 /* For SImode, rlwinm can do everything. */
18672 if (mode == SImode)
18673 return (nb < 32 && ne < 32);
18675 return false;
18678 /* Return the instruction template for an AND with mask in mode MODE, with
18679 operands OPERANDS. If DOT is true, make it a record-form instruction. */
18681 const char *
18682 rs6000_insn_for_and_mask (machine_mode mode, rtx *operands, bool dot)
18684 int nb, ne;
18686 if (!rs6000_is_valid_mask (operands[2], &nb, &ne, mode))
18687 gcc_unreachable ();
18689 if (mode == DImode && ne == 0)
18691 operands[3] = GEN_INT (63 - nb);
18692 if (dot)
18693 return "rldicl. %0,%1,0,%3";
18694 return "rldicl %0,%1,0,%3";
18697 if (mode == DImode && nb == 63)
18699 operands[3] = GEN_INT (63 - ne);
18700 if (dot)
18701 return "rldicr. %0,%1,0,%3";
18702 return "rldicr %0,%1,0,%3";
18705 if (nb < 32 && ne < 32)
18707 operands[3] = GEN_INT (31 - nb);
18708 operands[4] = GEN_INT (31 - ne);
18709 if (dot)
18710 return "rlwinm. %0,%1,0,%3,%4";
18711 return "rlwinm %0,%1,0,%3,%4";
18714 gcc_unreachable ();
18717 /* Return whether MASK (a CONST_INT) is a valid mask for any rlw[i]nm,
18718 rld[i]cl, rld[i]cr, or rld[i]c instruction, to implement an AND with
18719 shift SHIFT (a ROTATE, ASHIFT, or LSHIFTRT) in mode MODE. */
18721 bool
18722 rs6000_is_valid_shift_mask (rtx mask, rtx shift, machine_mode mode)
18724 int nb, ne;
18726 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
18727 return false;
18729 int n = GET_MODE_PRECISION (mode);
18730 int sh = -1;
18732 if (CONST_INT_P (XEXP (shift, 1)))
18734 sh = INTVAL (XEXP (shift, 1));
18735 if (sh < 0 || sh >= n)
18736 return false;
18739 rtx_code code = GET_CODE (shift);
18741 /* Convert any shift by 0 to a rotate, to simplify below code. */
18742 if (sh == 0)
18743 code = ROTATE;
18745 /* Convert rotate to simple shift if we can, to make analysis simpler. */
18746 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
18747 code = ASHIFT;
18748 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
18750 code = LSHIFTRT;
18751 sh = n - sh;
18754 /* DImode rotates need rld*. */
18755 if (mode == DImode && code == ROTATE)
18756 return (nb == 63 || ne == 0 || ne == sh);
18758 /* SImode rotates need rlw*. */
18759 if (mode == SImode && code == ROTATE)
18760 return (nb < 32 && ne < 32 && sh < 32);
18762 /* Wrap-around masks are only okay for rotates. */
18763 if (ne > nb)
18764 return false;
18766 /* Variable shifts are only okay for rotates. */
18767 if (sh < 0)
18768 return false;
18770 /* Don't allow ASHIFT if the mask is wrong for that. */
18771 if (code == ASHIFT && ne < sh)
18772 return false;
18774 /* If we can do it with an rlw*, we can do it. Don't allow LSHIFTRT
18775 if the mask is wrong for that. */
18776 if (nb < 32 && ne < 32 && sh < 32
18777 && !(code == LSHIFTRT && nb >= 32 - sh))
18778 return true;
18780 /* If we can do it with an rld*, we can do it. Don't allow LSHIFTRT
18781 if the mask is wrong for that. */
18782 if (code == LSHIFTRT)
18783 sh = 64 - sh;
18784 if (nb == 63 || ne == 0 || ne == sh)
18785 return !(code == LSHIFTRT && nb >= sh);
18787 return false;
18790 /* Return the instruction template for a shift with mask in mode MODE, with
18791 operands OPERANDS. If DOT is true, make it a record-form instruction. */
18793 const char *
18794 rs6000_insn_for_shift_mask (machine_mode mode, rtx *operands, bool dot)
18796 int nb, ne;
18798 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
18799 gcc_unreachable ();
18801 if (mode == DImode && ne == 0)
18803 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
18804 operands[2] = GEN_INT (64 - INTVAL (operands[2]));
18805 operands[3] = GEN_INT (63 - nb);
18806 if (dot)
18807 return "rld%I2cl. %0,%1,%2,%3";
18808 return "rld%I2cl %0,%1,%2,%3";
18811 if (mode == DImode && nb == 63)
18813 operands[3] = GEN_INT (63 - ne);
18814 if (dot)
18815 return "rld%I2cr. %0,%1,%2,%3";
18816 return "rld%I2cr %0,%1,%2,%3";
18819 if (mode == DImode
18820 && GET_CODE (operands[4]) != LSHIFTRT
18821 && CONST_INT_P (operands[2])
18822 && ne == INTVAL (operands[2]))
18824 operands[3] = GEN_INT (63 - nb);
18825 if (dot)
18826 return "rld%I2c. %0,%1,%2,%3";
18827 return "rld%I2c %0,%1,%2,%3";
18830 if (nb < 32 && ne < 32)
18832 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
18833 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
18834 operands[3] = GEN_INT (31 - nb);
18835 operands[4] = GEN_INT (31 - ne);
18836 /* This insn can also be a 64-bit rotate with mask that really makes
18837 it just a shift right (with mask); the %h below are to adjust for
18838 that situation (shift count is >= 32 in that case). */
18839 if (dot)
18840 return "rlw%I2nm. %0,%1,%h2,%3,%4";
18841 return "rlw%I2nm %0,%1,%h2,%3,%4";
18844 gcc_unreachable ();
18847 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwimi or
18848 rldimi instruction, to implement an insert with shift SHIFT (a ROTATE,
18849 ASHIFT, or LSHIFTRT) in mode MODE. */
18851 bool
18852 rs6000_is_valid_insert_mask (rtx mask, rtx shift, machine_mode mode)
18854 int nb, ne;
18856 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
18857 return false;
18859 int n = GET_MODE_PRECISION (mode);
18861 int sh = INTVAL (XEXP (shift, 1));
18862 if (sh < 0 || sh >= n)
18863 return false;
18865 rtx_code code = GET_CODE (shift);
18867 /* Convert any shift by 0 to a rotate, to simplify below code. */
18868 if (sh == 0)
18869 code = ROTATE;
18871 /* Convert rotate to simple shift if we can, to make analysis simpler. */
18872 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
18873 code = ASHIFT;
18874 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
18876 code = LSHIFTRT;
18877 sh = n - sh;
18880 /* DImode rotates need rldimi. */
18881 if (mode == DImode && code == ROTATE)
18882 return (ne == sh);
18884 /* SImode rotates need rlwimi. */
18885 if (mode == SImode && code == ROTATE)
18886 return (nb < 32 && ne < 32 && sh < 32);
18888 /* Wrap-around masks are only okay for rotates. */
18889 if (ne > nb)
18890 return false;
18892 /* Don't allow ASHIFT if the mask is wrong for that. */
18893 if (code == ASHIFT && ne < sh)
18894 return false;
18896 /* If we can do it with an rlwimi, we can do it. Don't allow LSHIFTRT
18897 if the mask is wrong for that. */
18898 if (nb < 32 && ne < 32 && sh < 32
18899 && !(code == LSHIFTRT && nb >= 32 - sh))
18900 return true;
18902 /* If we can do it with an rldimi, we can do it. Don't allow LSHIFTRT
18903 if the mask is wrong for that. */
18904 if (code == LSHIFTRT)
18905 sh = 64 - sh;
18906 if (ne == sh)
18907 return !(code == LSHIFTRT && nb >= sh);
18909 return false;
18912 /* Return the instruction template for an insert with mask in mode MODE, with
18913 operands OPERANDS. If DOT is true, make it a record-form instruction. */
18915 const char *
18916 rs6000_insn_for_insert_mask (machine_mode mode, rtx *operands, bool dot)
18918 int nb, ne;
18920 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
18921 gcc_unreachable ();
18923 /* Prefer rldimi because rlwimi is cracked. */
18924 if (TARGET_POWERPC64
18925 && (!dot || mode == DImode)
18926 && GET_CODE (operands[4]) != LSHIFTRT
18927 && ne == INTVAL (operands[2]))
18929 operands[3] = GEN_INT (63 - nb);
18930 if (dot)
18931 return "rldimi. %0,%1,%2,%3";
18932 return "rldimi %0,%1,%2,%3";
18935 if (nb < 32 && ne < 32)
18937 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
18938 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
18939 operands[3] = GEN_INT (31 - nb);
18940 operands[4] = GEN_INT (31 - ne);
18941 if (dot)
18942 return "rlwimi. %0,%1,%2,%3,%4";
18943 return "rlwimi %0,%1,%2,%3,%4";
18946 gcc_unreachable ();
18949 /* Return whether an AND with C (a CONST_INT) in mode MODE can be done
18950 using two machine instructions. */
18952 bool
18953 rs6000_is_valid_2insn_and (rtx c, machine_mode mode)
18955 /* There are two kinds of AND we can handle with two insns:
18956 1) those we can do with two rl* insn;
18957 2) ori[s];xori[s].
18959 We do not handle that last case yet. */
18961 /* If there is just one stretch of ones, we can do it. */
18962 if (rs6000_is_valid_mask (c, NULL, NULL, mode))
18963 return true;
18965 /* Otherwise, fill in the lowest "hole"; if we can do the result with
18966 one insn, we can do the whole thing with two. */
18967 unsigned HOST_WIDE_INT val = INTVAL (c);
18968 unsigned HOST_WIDE_INT bit1 = val & -val;
18969 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
18970 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
18971 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
18972 return rs6000_is_valid_and_mask (GEN_INT (val + bit3 - bit2), mode);
18975 /* Emit the two insns to do an AND in mode MODE, with operands OPERANDS.
18976 If EXPAND is true, split rotate-and-mask instructions we generate to
18977 their constituent parts as well (this is used during expand); if DOT
18978 is 1, make the last insn a record-form instruction clobbering the
18979 destination GPR and setting the CC reg (from operands[3]); if 2, set
18980 that GPR as well as the CC reg. */
18982 void
18983 rs6000_emit_2insn_and (machine_mode mode, rtx *operands, bool expand, int dot)
18985 gcc_assert (!(expand && dot));
18987 unsigned HOST_WIDE_INT val = INTVAL (operands[2]);
18989 /* If it is one stretch of ones, it is DImode; shift left, mask, then
18990 shift right. This generates better code than doing the masks without
18991 shifts, or shifting first right and then left. */
18992 int nb, ne;
18993 if (rs6000_is_valid_mask (operands[2], &nb, &ne, mode) && nb >= ne)
18995 gcc_assert (mode == DImode);
18997 int shift = 63 - nb;
18998 if (expand)
19000 rtx tmp1 = gen_reg_rtx (DImode);
19001 rtx tmp2 = gen_reg_rtx (DImode);
19002 emit_insn (gen_ashldi3 (tmp1, operands[1], GEN_INT (shift)));
19003 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (val << shift)));
19004 emit_insn (gen_lshrdi3 (operands[0], tmp2, GEN_INT (shift)));
19006 else
19008 rtx tmp = gen_rtx_ASHIFT (mode, operands[1], GEN_INT (shift));
19009 tmp = gen_rtx_AND (mode, tmp, GEN_INT (val << shift));
19010 emit_move_insn (operands[0], tmp);
19011 tmp = gen_rtx_LSHIFTRT (mode, operands[0], GEN_INT (shift));
19012 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
19014 return;
19017 /* Otherwise, make a mask2 that cuts out the lowest "hole", and a mask1
19018 that does the rest. */
19019 unsigned HOST_WIDE_INT bit1 = val & -val;
19020 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
19021 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
19022 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
19024 unsigned HOST_WIDE_INT mask1 = -bit3 + bit2 - 1;
19025 unsigned HOST_WIDE_INT mask2 = val + bit3 - bit2;
19027 gcc_assert (rs6000_is_valid_and_mask (GEN_INT (mask2), mode));
19029 /* Two "no-rotate"-and-mask instructions, for SImode. */
19030 if (rs6000_is_valid_and_mask (GEN_INT (mask1), mode))
19032 gcc_assert (mode == SImode);
19034 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
19035 rtx tmp = gen_rtx_AND (mode, operands[1], GEN_INT (mask1));
19036 emit_move_insn (reg, tmp);
19037 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
19038 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
19039 return;
19042 gcc_assert (mode == DImode);
19044 /* Two "no-rotate"-and-mask instructions, for DImode: both are rlwinm
19045 insns; we have to do the first in SImode, because it wraps. */
19046 if (mask2 <= 0xffffffff
19047 && rs6000_is_valid_and_mask (GEN_INT (mask1), SImode))
19049 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
19050 rtx tmp = gen_rtx_AND (SImode, gen_lowpart (SImode, operands[1]),
19051 GEN_INT (mask1));
19052 rtx reg_low = gen_lowpart (SImode, reg);
19053 emit_move_insn (reg_low, tmp);
19054 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
19055 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
19056 return;
19059 /* Two rld* insns: rotate, clear the hole in the middle (which now is
19060 at the top end), rotate back and clear the other hole. */
19061 int right = exact_log2 (bit3);
19062 int left = 64 - right;
19064 /* Rotate the mask too. */
19065 mask1 = (mask1 >> right) | ((bit2 - 1) << left);
19067 if (expand)
19069 rtx tmp1 = gen_reg_rtx (DImode);
19070 rtx tmp2 = gen_reg_rtx (DImode);
19071 rtx tmp3 = gen_reg_rtx (DImode);
19072 emit_insn (gen_rotldi3 (tmp1, operands[1], GEN_INT (left)));
19073 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (mask1)));
19074 emit_insn (gen_rotldi3 (tmp3, tmp2, GEN_INT (right)));
19075 emit_insn (gen_anddi3 (operands[0], tmp3, GEN_INT (mask2)));
19077 else
19079 rtx tmp = gen_rtx_ROTATE (mode, operands[1], GEN_INT (left));
19080 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask1));
19081 emit_move_insn (operands[0], tmp);
19082 tmp = gen_rtx_ROTATE (mode, operands[0], GEN_INT (right));
19083 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask2));
19084 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
19088 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
19089 for lfq and stfq insns iff the registers are hard registers. */
19092 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
19094 /* We might have been passed a SUBREG. */
19095 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
19096 return 0;
19098 /* We might have been passed non floating point registers. */
19099 if (!FP_REGNO_P (REGNO (reg1))
19100 || !FP_REGNO_P (REGNO (reg2)))
19101 return 0;
19103 return (REGNO (reg1) == REGNO (reg2) - 1);
19106 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
19107 addr1 and addr2 must be in consecutive memory locations
19108 (addr2 == addr1 + 8). */
19111 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
19113 rtx addr1, addr2;
19114 unsigned int reg1, reg2;
19115 int offset1, offset2;
19117 /* The mems cannot be volatile. */
19118 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
19119 return 0;
19121 addr1 = XEXP (mem1, 0);
19122 addr2 = XEXP (mem2, 0);
19124 /* Extract an offset (if used) from the first addr. */
19125 if (GET_CODE (addr1) == PLUS)
19127 /* If not a REG, return zero. */
19128 if (GET_CODE (XEXP (addr1, 0)) != REG)
19129 return 0;
19130 else
19132 reg1 = REGNO (XEXP (addr1, 0));
19133 /* The offset must be constant! */
19134 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
19135 return 0;
19136 offset1 = INTVAL (XEXP (addr1, 1));
19139 else if (GET_CODE (addr1) != REG)
19140 return 0;
19141 else
19143 reg1 = REGNO (addr1);
19144 /* This was a simple (mem (reg)) expression. Offset is 0. */
19145 offset1 = 0;
19148 /* And now for the second addr. */
19149 if (GET_CODE (addr2) == PLUS)
19151 /* If not a REG, return zero. */
19152 if (GET_CODE (XEXP (addr2, 0)) != REG)
19153 return 0;
19154 else
19156 reg2 = REGNO (XEXP (addr2, 0));
19157 /* The offset must be constant. */
19158 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
19159 return 0;
19160 offset2 = INTVAL (XEXP (addr2, 1));
19163 else if (GET_CODE (addr2) != REG)
19164 return 0;
19165 else
19167 reg2 = REGNO (addr2);
19168 /* This was a simple (mem (reg)) expression. Offset is 0. */
19169 offset2 = 0;
19172 /* Both of these must have the same base register. */
19173 if (reg1 != reg2)
19174 return 0;
19176 /* The offset for the second addr must be 8 more than the first addr. */
19177 if (offset2 != offset1 + 8)
19178 return 0;
19180 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
19181 instructions. */
19182 return 1;
19185 /* Return the mode to be used for memory when a secondary memory
19186 location is needed. For SDmode values we need to use DDmode, in
19187 all other cases we can use the same mode. */
19188 machine_mode
19189 rs6000_secondary_memory_needed_mode (machine_mode mode)
19191 if (lra_in_progress && mode == SDmode)
19192 return DDmode;
19193 return mode;
19196 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
19197 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
19198 only work on the traditional altivec registers, note if an altivec register
19199 was chosen. */
19201 static enum rs6000_reg_type
19202 register_to_reg_type (rtx reg, bool *is_altivec)
19204 HOST_WIDE_INT regno;
19205 enum reg_class rclass;
19207 if (GET_CODE (reg) == SUBREG)
19208 reg = SUBREG_REG (reg);
19210 if (!REG_P (reg))
19211 return NO_REG_TYPE;
19213 regno = REGNO (reg);
19214 if (regno >= FIRST_PSEUDO_REGISTER)
19216 if (!lra_in_progress && !reload_completed)
19217 return PSEUDO_REG_TYPE;
19219 regno = true_regnum (reg);
19220 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
19221 return PSEUDO_REG_TYPE;
19224 gcc_assert (regno >= 0);
19226 if (is_altivec && ALTIVEC_REGNO_P (regno))
19227 *is_altivec = true;
19229 rclass = rs6000_regno_regclass[regno];
19230 return reg_class_to_reg_type[(int)rclass];
19233 /* Helper function to return the cost of adding a TOC entry address. */
19235 static inline int
19236 rs6000_secondary_reload_toc_costs (addr_mask_type addr_mask)
19238 int ret;
19240 if (TARGET_CMODEL != CMODEL_SMALL)
19241 ret = ((addr_mask & RELOAD_REG_OFFSET) == 0) ? 1 : 2;
19243 else
19244 ret = (TARGET_MINIMAL_TOC) ? 6 : 3;
19246 return ret;
19249 /* Helper function for rs6000_secondary_reload to determine whether the memory
19250 address (ADDR) with a given register class (RCLASS) and machine mode (MODE)
19251 needs reloading. Return negative if the memory is not handled by the memory
19252 helper functions and to try a different reload method, 0 if no additional
19253 instructions are need, and positive to give the extra cost for the
19254 memory. */
19256 static int
19257 rs6000_secondary_reload_memory (rtx addr,
19258 enum reg_class rclass,
19259 machine_mode mode)
19261 int extra_cost = 0;
19262 rtx reg, and_arg, plus_arg0, plus_arg1;
19263 addr_mask_type addr_mask;
19264 const char *type = NULL;
19265 const char *fail_msg = NULL;
19267 if (GPR_REG_CLASS_P (rclass))
19268 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
19270 else if (rclass == FLOAT_REGS)
19271 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
19273 else if (rclass == ALTIVEC_REGS)
19274 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
19276 /* For the combined VSX_REGS, turn off Altivec AND -16. */
19277 else if (rclass == VSX_REGS)
19278 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_VMX]
19279 & ~RELOAD_REG_AND_M16);
19281 /* If the register allocator hasn't made up its mind yet on the register
19282 class to use, settle on defaults to use. */
19283 else if (rclass == NO_REGS)
19285 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_ANY]
19286 & ~RELOAD_REG_AND_M16);
19288 if ((addr_mask & RELOAD_REG_MULTIPLE) != 0)
19289 addr_mask &= ~(RELOAD_REG_INDEXED
19290 | RELOAD_REG_PRE_INCDEC
19291 | RELOAD_REG_PRE_MODIFY);
19294 else
19295 addr_mask = 0;
19297 /* If the register isn't valid in this register class, just return now. */
19298 if ((addr_mask & RELOAD_REG_VALID) == 0)
19300 if (TARGET_DEBUG_ADDR)
19302 fprintf (stderr,
19303 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
19304 "not valid in class\n",
19305 GET_MODE_NAME (mode), reg_class_names[rclass]);
19306 debug_rtx (addr);
19309 return -1;
19312 switch (GET_CODE (addr))
19314 /* Does the register class supports auto update forms for this mode? We
19315 don't need a scratch register, since the powerpc only supports
19316 PRE_INC, PRE_DEC, and PRE_MODIFY. */
19317 case PRE_INC:
19318 case PRE_DEC:
19319 reg = XEXP (addr, 0);
19320 if (!base_reg_operand (addr, GET_MODE (reg)))
19322 fail_msg = "no base register #1";
19323 extra_cost = -1;
19326 else if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
19328 extra_cost = 1;
19329 type = "update";
19331 break;
19333 case PRE_MODIFY:
19334 reg = XEXP (addr, 0);
19335 plus_arg1 = XEXP (addr, 1);
19336 if (!base_reg_operand (reg, GET_MODE (reg))
19337 || GET_CODE (plus_arg1) != PLUS
19338 || !rtx_equal_p (reg, XEXP (plus_arg1, 0)))
19340 fail_msg = "bad PRE_MODIFY";
19341 extra_cost = -1;
19344 else if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
19346 extra_cost = 1;
19347 type = "update";
19349 break;
19351 /* Do we need to simulate AND -16 to clear the bottom address bits used
19352 in VMX load/stores? Only allow the AND for vector sizes. */
19353 case AND:
19354 and_arg = XEXP (addr, 0);
19355 if (GET_MODE_SIZE (mode) != 16
19356 || GET_CODE (XEXP (addr, 1)) != CONST_INT
19357 || INTVAL (XEXP (addr, 1)) != -16)
19359 fail_msg = "bad Altivec AND #1";
19360 extra_cost = -1;
19363 if (rclass != ALTIVEC_REGS)
19365 if (legitimate_indirect_address_p (and_arg, false))
19366 extra_cost = 1;
19368 else if (legitimate_indexed_address_p (and_arg, false))
19369 extra_cost = 2;
19371 else
19373 fail_msg = "bad Altivec AND #2";
19374 extra_cost = -1;
19377 type = "and";
19379 break;
19381 /* If this is an indirect address, make sure it is a base register. */
19382 case REG:
19383 case SUBREG:
19384 if (!legitimate_indirect_address_p (addr, false))
19386 extra_cost = 1;
19387 type = "move";
19389 break;
19391 /* If this is an indexed address, make sure the register class can handle
19392 indexed addresses for this mode. */
19393 case PLUS:
19394 plus_arg0 = XEXP (addr, 0);
19395 plus_arg1 = XEXP (addr, 1);
19397 /* (plus (plus (reg) (constant)) (constant)) is generated during
19398 push_reload processing, so handle it now. */
19399 if (GET_CODE (plus_arg0) == PLUS && CONST_INT_P (plus_arg1))
19401 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
19403 extra_cost = 1;
19404 type = "offset";
19408 /* (plus (plus (reg) (constant)) (reg)) is also generated during
19409 push_reload processing, so handle it now. */
19410 else if (GET_CODE (plus_arg0) == PLUS && REG_P (plus_arg1))
19412 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
19414 extra_cost = 1;
19415 type = "indexed #2";
19419 else if (!base_reg_operand (plus_arg0, GET_MODE (plus_arg0)))
19421 fail_msg = "no base register #2";
19422 extra_cost = -1;
19425 else if (int_reg_operand (plus_arg1, GET_MODE (plus_arg1)))
19427 if ((addr_mask & RELOAD_REG_INDEXED) == 0
19428 || !legitimate_indexed_address_p (addr, false))
19430 extra_cost = 1;
19431 type = "indexed";
19435 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0
19436 && CONST_INT_P (plus_arg1))
19438 if (!quad_address_offset_p (INTVAL (plus_arg1)))
19440 extra_cost = 1;
19441 type = "vector d-form offset";
19445 /* Make sure the register class can handle offset addresses. */
19446 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
19448 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
19450 extra_cost = 1;
19451 type = "offset #2";
19455 else
19457 fail_msg = "bad PLUS";
19458 extra_cost = -1;
19461 break;
19463 case LO_SUM:
19464 /* Quad offsets are restricted and can't handle normal addresses. */
19465 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
19467 extra_cost = -1;
19468 type = "vector d-form lo_sum";
19471 else if (!legitimate_lo_sum_address_p (mode, addr, false))
19473 fail_msg = "bad LO_SUM";
19474 extra_cost = -1;
19477 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
19479 extra_cost = 1;
19480 type = "lo_sum";
19482 break;
19484 /* Static addresses need to create a TOC entry. */
19485 case CONST:
19486 case SYMBOL_REF:
19487 case LABEL_REF:
19488 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
19490 extra_cost = -1;
19491 type = "vector d-form lo_sum #2";
19494 else
19496 type = "address";
19497 extra_cost = rs6000_secondary_reload_toc_costs (addr_mask);
19499 break;
19501 /* TOC references look like offsetable memory. */
19502 case UNSPEC:
19503 if (TARGET_CMODEL == CMODEL_SMALL || XINT (addr, 1) != UNSPEC_TOCREL)
19505 fail_msg = "bad UNSPEC";
19506 extra_cost = -1;
19509 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
19511 extra_cost = -1;
19512 type = "vector d-form lo_sum #3";
19515 else if ((addr_mask & RELOAD_REG_OFFSET) == 0)
19517 extra_cost = 1;
19518 type = "toc reference";
19520 break;
19522 default:
19524 fail_msg = "bad address";
19525 extra_cost = -1;
19529 if (TARGET_DEBUG_ADDR /* && extra_cost != 0 */)
19531 if (extra_cost < 0)
19532 fprintf (stderr,
19533 "rs6000_secondary_reload_memory error: mode = %s, "
19534 "class = %s, addr_mask = '%s', %s\n",
19535 GET_MODE_NAME (mode),
19536 reg_class_names[rclass],
19537 rs6000_debug_addr_mask (addr_mask, false),
19538 (fail_msg != NULL) ? fail_msg : "<bad address>");
19540 else
19541 fprintf (stderr,
19542 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
19543 "addr_mask = '%s', extra cost = %d, %s\n",
19544 GET_MODE_NAME (mode),
19545 reg_class_names[rclass],
19546 rs6000_debug_addr_mask (addr_mask, false),
19547 extra_cost,
19548 (type) ? type : "<none>");
19550 debug_rtx (addr);
19553 return extra_cost;
19556 /* Helper function for rs6000_secondary_reload to return true if a move to a
19557 different register classe is really a simple move. */
19559 static bool
19560 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
19561 enum rs6000_reg_type from_type,
19562 machine_mode mode)
19564 int size = GET_MODE_SIZE (mode);
19566 /* Add support for various direct moves available. In this function, we only
19567 look at cases where we don't need any extra registers, and one or more
19568 simple move insns are issued. Originally small integers are not allowed
19569 in FPR/VSX registers. Single precision binary floating is not a simple
19570 move because we need to convert to the single precision memory layout.
19571 The 4-byte SDmode can be moved. TDmode values are disallowed since they
19572 need special direct move handling, which we do not support yet. */
19573 if (TARGET_DIRECT_MOVE
19574 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
19575 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
19577 if (TARGET_POWERPC64)
19579 /* ISA 2.07: MTVSRD or MVFVSRD. */
19580 if (size == 8)
19581 return true;
19583 /* ISA 3.0: MTVSRDD or MFVSRD + MFVSRLD. */
19584 if (size == 16 && TARGET_P9_VECTOR && mode != TDmode)
19585 return true;
19588 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
19589 if (TARGET_P8_VECTOR)
19591 if (mode == SImode)
19592 return true;
19594 if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
19595 return true;
19598 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
19599 if (mode == SDmode)
19600 return true;
19603 /* Power6+: MFTGPR or MFFGPR. */
19604 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
19605 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
19606 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
19607 return true;
19609 /* Move to/from SPR. */
19610 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
19611 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
19612 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
19613 return true;
19615 return false;
19618 /* Direct move helper function for rs6000_secondary_reload, handle all of the
19619 special direct moves that involve allocating an extra register, return the
19620 insn code of the helper function if there is such a function or
19621 CODE_FOR_nothing if not. */
19623 static bool
19624 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
19625 enum rs6000_reg_type from_type,
19626 machine_mode mode,
19627 secondary_reload_info *sri,
19628 bool altivec_p)
19630 bool ret = false;
19631 enum insn_code icode = CODE_FOR_nothing;
19632 int cost = 0;
19633 int size = GET_MODE_SIZE (mode);
19635 if (TARGET_POWERPC64 && size == 16)
19637 /* Handle moving 128-bit values from GPRs to VSX point registers on
19638 ISA 2.07 (power8, power9) when running in 64-bit mode using
19639 XXPERMDI to glue the two 64-bit values back together. */
19640 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
19642 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
19643 icode = reg_addr[mode].reload_vsx_gpr;
19646 /* Handle moving 128-bit values from VSX point registers to GPRs on
19647 ISA 2.07 when running in 64-bit mode using XXPERMDI to get access to the
19648 bottom 64-bit value. */
19649 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
19651 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
19652 icode = reg_addr[mode].reload_gpr_vsx;
19656 else if (TARGET_POWERPC64 && mode == SFmode)
19658 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
19660 cost = 3; /* xscvdpspn, mfvsrd, and. */
19661 icode = reg_addr[mode].reload_gpr_vsx;
19664 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
19666 cost = 2; /* mtvsrz, xscvspdpn. */
19667 icode = reg_addr[mode].reload_vsx_gpr;
19671 else if (!TARGET_POWERPC64 && size == 8)
19673 /* Handle moving 64-bit values from GPRs to floating point registers on
19674 ISA 2.07 when running in 32-bit mode using FMRGOW to glue the two
19675 32-bit values back together. Altivec register classes must be handled
19676 specially since a different instruction is used, and the secondary
19677 reload support requires a single instruction class in the scratch
19678 register constraint. However, right now TFmode is not allowed in
19679 Altivec registers, so the pattern will never match. */
19680 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
19682 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
19683 icode = reg_addr[mode].reload_fpr_gpr;
19687 if (icode != CODE_FOR_nothing)
19689 ret = true;
19690 if (sri)
19692 sri->icode = icode;
19693 sri->extra_cost = cost;
19697 return ret;
19700 /* Return whether a move between two register classes can be done either
19701 directly (simple move) or via a pattern that uses a single extra temporary
19702 (using ISA 2.07's direct move in this case. */
19704 static bool
19705 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
19706 enum rs6000_reg_type from_type,
19707 machine_mode mode,
19708 secondary_reload_info *sri,
19709 bool altivec_p)
19711 /* Fall back to load/store reloads if either type is not a register. */
19712 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
19713 return false;
19715 /* If we haven't allocated registers yet, assume the move can be done for the
19716 standard register types. */
19717 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
19718 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
19719 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
19720 return true;
19722 /* Moves to the same set of registers is a simple move for non-specialized
19723 registers. */
19724 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
19725 return true;
19727 /* Check whether a simple move can be done directly. */
19728 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
19730 if (sri)
19732 sri->icode = CODE_FOR_nothing;
19733 sri->extra_cost = 0;
19735 return true;
19738 /* Now check if we can do it in a few steps. */
19739 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
19740 altivec_p);
19743 /* Inform reload about cases where moving X with a mode MODE to a register in
19744 RCLASS requires an extra scratch or immediate register. Return the class
19745 needed for the immediate register.
19747 For VSX and Altivec, we may need a register to convert sp+offset into
19748 reg+sp.
19750 For misaligned 64-bit gpr loads and stores we need a register to
19751 convert an offset address to indirect. */
19753 static reg_class_t
19754 rs6000_secondary_reload (bool in_p,
19755 rtx x,
19756 reg_class_t rclass_i,
19757 machine_mode mode,
19758 secondary_reload_info *sri)
19760 enum reg_class rclass = (enum reg_class) rclass_i;
19761 reg_class_t ret = ALL_REGS;
19762 enum insn_code icode;
19763 bool default_p = false;
19764 bool done_p = false;
19766 /* Allow subreg of memory before/during reload. */
19767 bool memory_p = (MEM_P (x)
19768 || (!reload_completed && GET_CODE (x) == SUBREG
19769 && MEM_P (SUBREG_REG (x))));
19771 sri->icode = CODE_FOR_nothing;
19772 sri->t_icode = CODE_FOR_nothing;
19773 sri->extra_cost = 0;
19774 icode = ((in_p)
19775 ? reg_addr[mode].reload_load
19776 : reg_addr[mode].reload_store);
19778 if (REG_P (x) || register_operand (x, mode))
19780 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
19781 bool altivec_p = (rclass == ALTIVEC_REGS);
19782 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
19784 if (!in_p)
19785 std::swap (to_type, from_type);
19787 /* Can we do a direct move of some sort? */
19788 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
19789 altivec_p))
19791 icode = (enum insn_code)sri->icode;
19792 default_p = false;
19793 done_p = true;
19794 ret = NO_REGS;
19798 /* Make sure 0.0 is not reloaded or forced into memory. */
19799 if (x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
19801 ret = NO_REGS;
19802 default_p = false;
19803 done_p = true;
19806 /* If this is a scalar floating point value and we want to load it into the
19807 traditional Altivec registers, do it via a move via a traditional floating
19808 point register, unless we have D-form addressing. Also make sure that
19809 non-zero constants use a FPR. */
19810 if (!done_p && reg_addr[mode].scalar_in_vmx_p
19811 && !mode_supports_vmx_dform (mode)
19812 && (rclass == VSX_REGS || rclass == ALTIVEC_REGS)
19813 && (memory_p || (GET_CODE (x) == CONST_DOUBLE)))
19815 ret = FLOAT_REGS;
19816 default_p = false;
19817 done_p = true;
19820 /* Handle reload of load/stores if we have reload helper functions. */
19821 if (!done_p && icode != CODE_FOR_nothing && memory_p)
19823 int extra_cost = rs6000_secondary_reload_memory (XEXP (x, 0), rclass,
19824 mode);
19826 if (extra_cost >= 0)
19828 done_p = true;
19829 ret = NO_REGS;
19830 if (extra_cost > 0)
19832 sri->extra_cost = extra_cost;
19833 sri->icode = icode;
19838 /* Handle unaligned loads and stores of integer registers. */
19839 if (!done_p && TARGET_POWERPC64
19840 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
19841 && memory_p
19842 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
19844 rtx addr = XEXP (x, 0);
19845 rtx off = address_offset (addr);
19847 if (off != NULL_RTX)
19849 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
19850 unsigned HOST_WIDE_INT offset = INTVAL (off);
19852 /* We need a secondary reload when our legitimate_address_p
19853 says the address is good (as otherwise the entire address
19854 will be reloaded), and the offset is not a multiple of
19855 four or we have an address wrap. Address wrap will only
19856 occur for LO_SUMs since legitimate_offset_address_p
19857 rejects addresses for 16-byte mems that will wrap. */
19858 if (GET_CODE (addr) == LO_SUM
19859 ? (1 /* legitimate_address_p allows any offset for lo_sum */
19860 && ((offset & 3) != 0
19861 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
19862 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
19863 && (offset & 3) != 0))
19865 /* -m32 -mpowerpc64 needs to use a 32-bit scratch register. */
19866 if (in_p)
19867 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_load
19868 : CODE_FOR_reload_di_load);
19869 else
19870 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_store
19871 : CODE_FOR_reload_di_store);
19872 sri->extra_cost = 2;
19873 ret = NO_REGS;
19874 done_p = true;
19876 else
19877 default_p = true;
19879 else
19880 default_p = true;
19883 if (!done_p && !TARGET_POWERPC64
19884 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
19885 && memory_p
19886 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
19888 rtx addr = XEXP (x, 0);
19889 rtx off = address_offset (addr);
19891 if (off != NULL_RTX)
19893 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
19894 unsigned HOST_WIDE_INT offset = INTVAL (off);
19896 /* We need a secondary reload when our legitimate_address_p
19897 says the address is good (as otherwise the entire address
19898 will be reloaded), and we have a wrap.
19900 legitimate_lo_sum_address_p allows LO_SUM addresses to
19901 have any offset so test for wrap in the low 16 bits.
19903 legitimate_offset_address_p checks for the range
19904 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
19905 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
19906 [0x7ff4,0x7fff] respectively, so test for the
19907 intersection of these ranges, [0x7ffc,0x7fff] and
19908 [0x7ff4,0x7ff7] respectively.
19910 Note that the address we see here may have been
19911 manipulated by legitimize_reload_address. */
19912 if (GET_CODE (addr) == LO_SUM
19913 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
19914 : offset - (0x8000 - extra) < UNITS_PER_WORD)
19916 if (in_p)
19917 sri->icode = CODE_FOR_reload_si_load;
19918 else
19919 sri->icode = CODE_FOR_reload_si_store;
19920 sri->extra_cost = 2;
19921 ret = NO_REGS;
19922 done_p = true;
19924 else
19925 default_p = true;
19927 else
19928 default_p = true;
19931 if (!done_p)
19932 default_p = true;
19934 if (default_p)
19935 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
19937 gcc_assert (ret != ALL_REGS);
19939 if (TARGET_DEBUG_ADDR)
19941 fprintf (stderr,
19942 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
19943 "mode = %s",
19944 reg_class_names[ret],
19945 in_p ? "true" : "false",
19946 reg_class_names[rclass],
19947 GET_MODE_NAME (mode));
19949 if (reload_completed)
19950 fputs (", after reload", stderr);
19952 if (!done_p)
19953 fputs (", done_p not set", stderr);
19955 if (default_p)
19956 fputs (", default secondary reload", stderr);
19958 if (sri->icode != CODE_FOR_nothing)
19959 fprintf (stderr, ", reload func = %s, extra cost = %d",
19960 insn_data[sri->icode].name, sri->extra_cost);
19962 else if (sri->extra_cost > 0)
19963 fprintf (stderr, ", extra cost = %d", sri->extra_cost);
19965 fputs ("\n", stderr);
19966 debug_rtx (x);
19969 return ret;
19972 /* Better tracing for rs6000_secondary_reload_inner. */
19974 static void
19975 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
19976 bool store_p)
19978 rtx set, clobber;
19980 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
19982 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
19983 store_p ? "store" : "load");
19985 if (store_p)
19986 set = gen_rtx_SET (mem, reg);
19987 else
19988 set = gen_rtx_SET (reg, mem);
19990 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
19991 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
19994 static void rs6000_secondary_reload_fail (int, rtx, rtx, rtx, bool)
19995 ATTRIBUTE_NORETURN;
19997 static void
19998 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
19999 bool store_p)
20001 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
20002 gcc_unreachable ();
20005 /* Fixup reload addresses for values in GPR, FPR, and VMX registers that have
20006 reload helper functions. These were identified in
20007 rs6000_secondary_reload_memory, and if reload decided to use the secondary
20008 reload, it calls the insns:
20009 reload_<RELOAD:mode>_<P:mptrsize>_store
20010 reload_<RELOAD:mode>_<P:mptrsize>_load
20012 which in turn calls this function, to do whatever is necessary to create
20013 valid addresses. */
20015 void
20016 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
20018 int regno = true_regnum (reg);
20019 machine_mode mode = GET_MODE (reg);
20020 addr_mask_type addr_mask;
20021 rtx addr;
20022 rtx new_addr;
20023 rtx op_reg, op0, op1;
20024 rtx and_op;
20025 rtx cc_clobber;
20026 rtvec rv;
20028 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER || !MEM_P (mem)
20029 || !base_reg_operand (scratch, GET_MODE (scratch)))
20030 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20032 if (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO))
20033 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
20035 else if (IN_RANGE (regno, FIRST_FPR_REGNO, LAST_FPR_REGNO))
20036 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
20038 else if (IN_RANGE (regno, FIRST_ALTIVEC_REGNO, LAST_ALTIVEC_REGNO))
20039 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
20041 else
20042 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20044 /* Make sure the mode is valid in this register class. */
20045 if ((addr_mask & RELOAD_REG_VALID) == 0)
20046 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20048 if (TARGET_DEBUG_ADDR)
20049 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
20051 new_addr = addr = XEXP (mem, 0);
20052 switch (GET_CODE (addr))
20054 /* Does the register class support auto update forms for this mode? If
20055 not, do the update now. We don't need a scratch register, since the
20056 powerpc only supports PRE_INC, PRE_DEC, and PRE_MODIFY. */
20057 case PRE_INC:
20058 case PRE_DEC:
20059 op_reg = XEXP (addr, 0);
20060 if (!base_reg_operand (op_reg, Pmode))
20061 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20063 if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
20065 emit_insn (gen_add2_insn (op_reg, GEN_INT (GET_MODE_SIZE (mode))));
20066 new_addr = op_reg;
20068 break;
20070 case PRE_MODIFY:
20071 op0 = XEXP (addr, 0);
20072 op1 = XEXP (addr, 1);
20073 if (!base_reg_operand (op0, Pmode)
20074 || GET_CODE (op1) != PLUS
20075 || !rtx_equal_p (op0, XEXP (op1, 0)))
20076 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20078 if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
20080 emit_insn (gen_rtx_SET (op0, op1));
20081 new_addr = reg;
20083 break;
20085 /* Do we need to simulate AND -16 to clear the bottom address bits used
20086 in VMX load/stores? */
20087 case AND:
20088 op0 = XEXP (addr, 0);
20089 op1 = XEXP (addr, 1);
20090 if ((addr_mask & RELOAD_REG_AND_M16) == 0)
20092 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
20093 op_reg = op0;
20095 else if (GET_CODE (op1) == PLUS)
20097 emit_insn (gen_rtx_SET (scratch, op1));
20098 op_reg = scratch;
20101 else
20102 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20104 and_op = gen_rtx_AND (GET_MODE (scratch), op_reg, op1);
20105 cc_clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (CCmode));
20106 rv = gen_rtvec (2, gen_rtx_SET (scratch, and_op), cc_clobber);
20107 emit_insn (gen_rtx_PARALLEL (VOIDmode, rv));
20108 new_addr = scratch;
20110 break;
20112 /* If this is an indirect address, make sure it is a base register. */
20113 case REG:
20114 case SUBREG:
20115 if (!base_reg_operand (addr, GET_MODE (addr)))
20117 emit_insn (gen_rtx_SET (scratch, addr));
20118 new_addr = scratch;
20120 break;
20122 /* If this is an indexed address, make sure the register class can handle
20123 indexed addresses for this mode. */
20124 case PLUS:
20125 op0 = XEXP (addr, 0);
20126 op1 = XEXP (addr, 1);
20127 if (!base_reg_operand (op0, Pmode))
20128 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20130 else if (int_reg_operand (op1, Pmode))
20132 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
20134 emit_insn (gen_rtx_SET (scratch, addr));
20135 new_addr = scratch;
20139 else if (mode_supports_vsx_dform_quad (mode) && CONST_INT_P (op1))
20141 if (((addr_mask & RELOAD_REG_QUAD_OFFSET) == 0)
20142 || !quad_address_p (addr, mode, false))
20144 emit_insn (gen_rtx_SET (scratch, addr));
20145 new_addr = scratch;
20149 /* Make sure the register class can handle offset addresses. */
20150 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
20152 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
20154 emit_insn (gen_rtx_SET (scratch, addr));
20155 new_addr = scratch;
20159 else
20160 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20162 break;
20164 case LO_SUM:
20165 op0 = XEXP (addr, 0);
20166 op1 = XEXP (addr, 1);
20167 if (!base_reg_operand (op0, Pmode))
20168 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20170 else if (int_reg_operand (op1, Pmode))
20172 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
20174 emit_insn (gen_rtx_SET (scratch, addr));
20175 new_addr = scratch;
20179 /* Quad offsets are restricted and can't handle normal addresses. */
20180 else if (mode_supports_vsx_dform_quad (mode))
20182 emit_insn (gen_rtx_SET (scratch, addr));
20183 new_addr = scratch;
20186 /* Make sure the register class can handle offset addresses. */
20187 else if (legitimate_lo_sum_address_p (mode, addr, false))
20189 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
20191 emit_insn (gen_rtx_SET (scratch, addr));
20192 new_addr = scratch;
20196 else
20197 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20199 break;
20201 case SYMBOL_REF:
20202 case CONST:
20203 case LABEL_REF:
20204 rs6000_emit_move (scratch, addr, Pmode);
20205 new_addr = scratch;
20206 break;
20208 default:
20209 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
20212 /* Adjust the address if it changed. */
20213 if (addr != new_addr)
20215 mem = replace_equiv_address_nv (mem, new_addr);
20216 if (TARGET_DEBUG_ADDR)
20217 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
20220 /* Now create the move. */
20221 if (store_p)
20222 emit_insn (gen_rtx_SET (mem, reg));
20223 else
20224 emit_insn (gen_rtx_SET (reg, mem));
20226 return;
20229 /* Convert reloads involving 64-bit gprs and misaligned offset
20230 addressing, or multiple 32-bit gprs and offsets that are too large,
20231 to use indirect addressing. */
20233 void
20234 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
20236 int regno = true_regnum (reg);
20237 enum reg_class rclass;
20238 rtx addr;
20239 rtx scratch_or_premodify = scratch;
20241 if (TARGET_DEBUG_ADDR)
20243 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
20244 store_p ? "store" : "load");
20245 fprintf (stderr, "reg:\n");
20246 debug_rtx (reg);
20247 fprintf (stderr, "mem:\n");
20248 debug_rtx (mem);
20249 fprintf (stderr, "scratch:\n");
20250 debug_rtx (scratch);
20253 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
20254 gcc_assert (GET_CODE (mem) == MEM);
20255 rclass = REGNO_REG_CLASS (regno);
20256 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
20257 addr = XEXP (mem, 0);
20259 if (GET_CODE (addr) == PRE_MODIFY)
20261 gcc_assert (REG_P (XEXP (addr, 0))
20262 && GET_CODE (XEXP (addr, 1)) == PLUS
20263 && XEXP (XEXP (addr, 1), 0) == XEXP (addr, 0));
20264 scratch_or_premodify = XEXP (addr, 0);
20265 if (!HARD_REGISTER_P (scratch_or_premodify))
20266 /* If we have a pseudo here then reload will have arranged
20267 to have it replaced, but only in the original insn.
20268 Use the replacement here too. */
20269 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
20271 /* RTL emitted by rs6000_secondary_reload_gpr uses RTL
20272 expressions from the original insn, without unsharing them.
20273 Any RTL that points into the original insn will of course
20274 have register replacements applied. That is why we don't
20275 need to look for replacements under the PLUS. */
20276 addr = XEXP (addr, 1);
20278 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
20280 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
20282 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
20284 /* Now create the move. */
20285 if (store_p)
20286 emit_insn (gen_rtx_SET (mem, reg));
20287 else
20288 emit_insn (gen_rtx_SET (reg, mem));
20290 return;
20293 /* Given an rtx X being reloaded into a reg required to be
20294 in class CLASS, return the class of reg to actually use.
20295 In general this is just CLASS; but on some machines
20296 in some cases it is preferable to use a more restrictive class.
20298 On the RS/6000, we have to return NO_REGS when we want to reload a
20299 floating-point CONST_DOUBLE to force it to be copied to memory.
20301 We also don't want to reload integer values into floating-point
20302 registers if we can at all help it. In fact, this can
20303 cause reload to die, if it tries to generate a reload of CTR
20304 into a FP register and discovers it doesn't have the memory location
20305 required.
20307 ??? Would it be a good idea to have reload do the converse, that is
20308 try to reload floating modes into FP registers if possible?
20311 static enum reg_class
20312 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
20314 machine_mode mode = GET_MODE (x);
20315 bool is_constant = CONSTANT_P (x);
20317 /* If a mode can't go in FPR/ALTIVEC/VSX registers, don't return a preferred
20318 reload class for it. */
20319 if ((rclass == ALTIVEC_REGS || rclass == VSX_REGS)
20320 && (reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID) == 0)
20321 return NO_REGS;
20323 if ((rclass == FLOAT_REGS || rclass == VSX_REGS)
20324 && (reg_addr[mode].addr_mask[RELOAD_REG_FPR] & RELOAD_REG_VALID) == 0)
20325 return NO_REGS;
20327 /* For VSX, see if we should prefer FLOAT_REGS or ALTIVEC_REGS. Do not allow
20328 the reloading of address expressions using PLUS into floating point
20329 registers. */
20330 if (TARGET_VSX && VSX_REG_CLASS_P (rclass) && GET_CODE (x) != PLUS)
20332 if (is_constant)
20334 /* Zero is always allowed in all VSX registers. */
20335 if (x == CONST0_RTX (mode))
20336 return rclass;
20338 /* If this is a vector constant that can be formed with a few Altivec
20339 instructions, we want altivec registers. */
20340 if (GET_CODE (x) == CONST_VECTOR && easy_vector_constant (x, mode))
20341 return ALTIVEC_REGS;
20343 /* If this is an integer constant that can easily be loaded into
20344 vector registers, allow it. */
20345 if (CONST_INT_P (x))
20347 HOST_WIDE_INT value = INTVAL (x);
20349 /* ISA 2.07 can generate -1 in all registers with XXLORC. ISA
20350 2.06 can generate it in the Altivec registers with
20351 VSPLTI<x>. */
20352 if (value == -1)
20354 if (TARGET_P8_VECTOR)
20355 return rclass;
20356 else if (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
20357 return ALTIVEC_REGS;
20358 else
20359 return NO_REGS;
20362 /* ISA 3.0 can load -128..127 using the XXSPLTIB instruction and
20363 a sign extend in the Altivec registers. */
20364 if (IN_RANGE (value, -128, 127) && TARGET_P9_VECTOR
20365 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS))
20366 return ALTIVEC_REGS;
20369 /* Force constant to memory. */
20370 return NO_REGS;
20373 /* D-form addressing can easily reload the value. */
20374 if (mode_supports_vmx_dform (mode)
20375 || mode_supports_vsx_dform_quad (mode))
20376 return rclass;
20378 /* If this is a scalar floating point value and we don't have D-form
20379 addressing, prefer the traditional floating point registers so that we
20380 can use D-form (register+offset) addressing. */
20381 if (rclass == VSX_REGS
20382 && (mode == SFmode || GET_MODE_SIZE (mode) == 8))
20383 return FLOAT_REGS;
20385 /* Prefer the Altivec registers if Altivec is handling the vector
20386 operations (i.e. V16QI, V8HI, and V4SI), or if we prefer Altivec
20387 loads. */
20388 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
20389 || mode == V1TImode)
20390 return ALTIVEC_REGS;
20392 return rclass;
20395 if (is_constant || GET_CODE (x) == PLUS)
20397 if (reg_class_subset_p (GENERAL_REGS, rclass))
20398 return GENERAL_REGS;
20399 if (reg_class_subset_p (BASE_REGS, rclass))
20400 return BASE_REGS;
20401 return NO_REGS;
20404 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
20405 return GENERAL_REGS;
20407 return rclass;
20410 /* Debug version of rs6000_preferred_reload_class. */
20411 static enum reg_class
20412 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
20414 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
20416 fprintf (stderr,
20417 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
20418 "mode = %s, x:\n",
20419 reg_class_names[ret], reg_class_names[rclass],
20420 GET_MODE_NAME (GET_MODE (x)));
20421 debug_rtx (x);
20423 return ret;
20426 /* If we are copying between FP or AltiVec registers and anything else, we need
20427 a memory location. The exception is when we are targeting ppc64 and the
20428 move to/from fpr to gpr instructions are available. Also, under VSX, you
20429 can copy vector registers from the FP register set to the Altivec register
20430 set and vice versa. */
20432 static bool
20433 rs6000_secondary_memory_needed (enum reg_class from_class,
20434 enum reg_class to_class,
20435 machine_mode mode)
20437 enum rs6000_reg_type from_type, to_type;
20438 bool altivec_p = ((from_class == ALTIVEC_REGS)
20439 || (to_class == ALTIVEC_REGS));
20441 /* If a simple/direct move is available, we don't need secondary memory */
20442 from_type = reg_class_to_reg_type[(int)from_class];
20443 to_type = reg_class_to_reg_type[(int)to_class];
20445 if (rs6000_secondary_reload_move (to_type, from_type, mode,
20446 (secondary_reload_info *)0, altivec_p))
20447 return false;
20449 /* If we have a floating point or vector register class, we need to use
20450 memory to transfer the data. */
20451 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
20452 return true;
20454 return false;
20457 /* Debug version of rs6000_secondary_memory_needed. */
20458 static bool
20459 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
20460 enum reg_class to_class,
20461 machine_mode mode)
20463 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
20465 fprintf (stderr,
20466 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
20467 "to_class = %s, mode = %s\n",
20468 ret ? "true" : "false",
20469 reg_class_names[from_class],
20470 reg_class_names[to_class],
20471 GET_MODE_NAME (mode));
20473 return ret;
20476 /* Return the register class of a scratch register needed to copy IN into
20477 or out of a register in RCLASS in MODE. If it can be done directly,
20478 NO_REGS is returned. */
20480 static enum reg_class
20481 rs6000_secondary_reload_class (enum reg_class rclass, machine_mode mode,
20482 rtx in)
20484 int regno;
20486 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
20487 #if TARGET_MACHO
20488 && MACHOPIC_INDIRECT
20489 #endif
20492 /* We cannot copy a symbolic operand directly into anything
20493 other than BASE_REGS for TARGET_ELF. So indicate that a
20494 register from BASE_REGS is needed as an intermediate
20495 register.
20497 On Darwin, pic addresses require a load from memory, which
20498 needs a base register. */
20499 if (rclass != BASE_REGS
20500 && (GET_CODE (in) == SYMBOL_REF
20501 || GET_CODE (in) == HIGH
20502 || GET_CODE (in) == LABEL_REF
20503 || GET_CODE (in) == CONST))
20504 return BASE_REGS;
20507 if (GET_CODE (in) == REG)
20509 regno = REGNO (in);
20510 if (regno >= FIRST_PSEUDO_REGISTER)
20512 regno = true_regnum (in);
20513 if (regno >= FIRST_PSEUDO_REGISTER)
20514 regno = -1;
20517 else if (GET_CODE (in) == SUBREG)
20519 regno = true_regnum (in);
20520 if (regno >= FIRST_PSEUDO_REGISTER)
20521 regno = -1;
20523 else
20524 regno = -1;
20526 /* If we have VSX register moves, prefer moving scalar values between
20527 Altivec registers and GPR by going via an FPR (and then via memory)
20528 instead of reloading the secondary memory address for Altivec moves. */
20529 if (TARGET_VSX
20530 && GET_MODE_SIZE (mode) < 16
20531 && !mode_supports_vmx_dform (mode)
20532 && (((rclass == GENERAL_REGS || rclass == BASE_REGS)
20533 && (regno >= 0 && ALTIVEC_REGNO_P (regno)))
20534 || ((rclass == VSX_REGS || rclass == ALTIVEC_REGS)
20535 && (regno >= 0 && INT_REGNO_P (regno)))))
20536 return FLOAT_REGS;
20538 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
20539 into anything. */
20540 if (rclass == GENERAL_REGS || rclass == BASE_REGS
20541 || (regno >= 0 && INT_REGNO_P (regno)))
20542 return NO_REGS;
20544 /* Constants, memory, and VSX registers can go into VSX registers (both the
20545 traditional floating point and the altivec registers). */
20546 if (rclass == VSX_REGS
20547 && (regno == -1 || VSX_REGNO_P (regno)))
20548 return NO_REGS;
20550 /* Constants, memory, and FP registers can go into FP registers. */
20551 if ((regno == -1 || FP_REGNO_P (regno))
20552 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
20553 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
20555 /* Memory, and AltiVec registers can go into AltiVec registers. */
20556 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
20557 && rclass == ALTIVEC_REGS)
20558 return NO_REGS;
20560 /* We can copy among the CR registers. */
20561 if ((rclass == CR_REGS || rclass == CR0_REGS)
20562 && regno >= 0 && CR_REGNO_P (regno))
20563 return NO_REGS;
20565 /* Otherwise, we need GENERAL_REGS. */
20566 return GENERAL_REGS;
20569 /* Debug version of rs6000_secondary_reload_class. */
20570 static enum reg_class
20571 rs6000_debug_secondary_reload_class (enum reg_class rclass,
20572 machine_mode mode, rtx in)
20574 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
20575 fprintf (stderr,
20576 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
20577 "mode = %s, input rtx:\n",
20578 reg_class_names[ret], reg_class_names[rclass],
20579 GET_MODE_NAME (mode));
20580 debug_rtx (in);
20582 return ret;
20585 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
20587 static bool
20588 rs6000_cannot_change_mode_class (machine_mode from,
20589 machine_mode to,
20590 enum reg_class rclass)
20592 unsigned from_size = GET_MODE_SIZE (from);
20593 unsigned to_size = GET_MODE_SIZE (to);
20595 if (from_size != to_size)
20597 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
20599 if (reg_classes_intersect_p (xclass, rclass))
20601 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
20602 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
20603 bool to_float128_vector_p = FLOAT128_VECTOR_P (to);
20604 bool from_float128_vector_p = FLOAT128_VECTOR_P (from);
20606 /* Don't allow 64-bit types to overlap with 128-bit types that take a
20607 single register under VSX because the scalar part of the register
20608 is in the upper 64-bits, and not the lower 64-bits. Types like
20609 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
20610 IEEE floating point can't overlap, and neither can small
20611 values. */
20613 if (to_float128_vector_p && from_float128_vector_p)
20614 return false;
20616 else if (to_float128_vector_p || from_float128_vector_p)
20617 return true;
20619 /* TDmode in floating-mode registers must always go into a register
20620 pair with the most significant word in the even-numbered register
20621 to match ISA requirements. In little-endian mode, this does not
20622 match subreg numbering, so we cannot allow subregs. */
20623 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
20624 return true;
20626 if (from_size < 8 || to_size < 8)
20627 return true;
20629 if (from_size == 8 && (8 * to_nregs) != to_size)
20630 return true;
20632 if (to_size == 8 && (8 * from_nregs) != from_size)
20633 return true;
20635 return false;
20637 else
20638 return false;
20641 /* Since the VSX register set includes traditional floating point registers
20642 and altivec registers, just check for the size being different instead of
20643 trying to check whether the modes are vector modes. Otherwise it won't
20644 allow say DF and DI to change classes. For types like TFmode and TDmode
20645 that take 2 64-bit registers, rather than a single 128-bit register, don't
20646 allow subregs of those types to other 128 bit types. */
20647 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
20649 unsigned num_regs = (from_size + 15) / 16;
20650 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
20651 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
20652 return true;
20654 return (from_size != 8 && from_size != 16);
20657 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
20658 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
20659 return true;
20661 return false;
20664 /* Debug version of rs6000_cannot_change_mode_class. */
20665 static bool
20666 rs6000_debug_cannot_change_mode_class (machine_mode from,
20667 machine_mode to,
20668 enum reg_class rclass)
20670 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
20672 fprintf (stderr,
20673 "rs6000_cannot_change_mode_class, return %s, from = %s, "
20674 "to = %s, rclass = %s\n",
20675 ret ? "true" : "false",
20676 GET_MODE_NAME (from), GET_MODE_NAME (to),
20677 reg_class_names[rclass]);
20679 return ret;
20682 /* Return a string to do a move operation of 128 bits of data. */
20684 const char *
20685 rs6000_output_move_128bit (rtx operands[])
20687 rtx dest = operands[0];
20688 rtx src = operands[1];
20689 machine_mode mode = GET_MODE (dest);
20690 int dest_regno;
20691 int src_regno;
20692 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
20693 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
20695 if (REG_P (dest))
20697 dest_regno = REGNO (dest);
20698 dest_gpr_p = INT_REGNO_P (dest_regno);
20699 dest_fp_p = FP_REGNO_P (dest_regno);
20700 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
20701 dest_vsx_p = dest_fp_p | dest_vmx_p;
20703 else
20705 dest_regno = -1;
20706 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
20709 if (REG_P (src))
20711 src_regno = REGNO (src);
20712 src_gpr_p = INT_REGNO_P (src_regno);
20713 src_fp_p = FP_REGNO_P (src_regno);
20714 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
20715 src_vsx_p = src_fp_p | src_vmx_p;
20717 else
20719 src_regno = -1;
20720 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
20723 /* Register moves. */
20724 if (dest_regno >= 0 && src_regno >= 0)
20726 if (dest_gpr_p)
20728 if (src_gpr_p)
20729 return "#";
20731 if (TARGET_DIRECT_MOVE_128 && src_vsx_p)
20732 return (WORDS_BIG_ENDIAN
20733 ? "mfvsrd %0,%x1\n\tmfvsrld %L0,%x1"
20734 : "mfvsrd %L0,%x1\n\tmfvsrld %0,%x1");
20736 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
20737 return "#";
20740 else if (TARGET_VSX && dest_vsx_p)
20742 if (src_vsx_p)
20743 return "xxlor %x0,%x1,%x1";
20745 else if (TARGET_DIRECT_MOVE_128 && src_gpr_p)
20746 return (WORDS_BIG_ENDIAN
20747 ? "mtvsrdd %x0,%1,%L1"
20748 : "mtvsrdd %x0,%L1,%1");
20750 else if (TARGET_DIRECT_MOVE && src_gpr_p)
20751 return "#";
20754 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
20755 return "vor %0,%1,%1";
20757 else if (dest_fp_p && src_fp_p)
20758 return "#";
20761 /* Loads. */
20762 else if (dest_regno >= 0 && MEM_P (src))
20764 if (dest_gpr_p)
20766 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
20767 return "lq %0,%1";
20768 else
20769 return "#";
20772 else if (TARGET_ALTIVEC && dest_vmx_p
20773 && altivec_indexed_or_indirect_operand (src, mode))
20774 return "lvx %0,%y1";
20776 else if (TARGET_VSX && dest_vsx_p)
20778 if (mode_supports_vsx_dform_quad (mode)
20779 && quad_address_p (XEXP (src, 0), mode, true))
20780 return "lxv %x0,%1";
20782 else if (TARGET_P9_VECTOR)
20783 return "lxvx %x0,%y1";
20785 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
20786 return "lxvw4x %x0,%y1";
20788 else
20789 return "lxvd2x %x0,%y1";
20792 else if (TARGET_ALTIVEC && dest_vmx_p)
20793 return "lvx %0,%y1";
20795 else if (dest_fp_p)
20796 return "#";
20799 /* Stores. */
20800 else if (src_regno >= 0 && MEM_P (dest))
20802 if (src_gpr_p)
20804 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
20805 return "stq %1,%0";
20806 else
20807 return "#";
20810 else if (TARGET_ALTIVEC && src_vmx_p
20811 && altivec_indexed_or_indirect_operand (src, mode))
20812 return "stvx %1,%y0";
20814 else if (TARGET_VSX && src_vsx_p)
20816 if (mode_supports_vsx_dform_quad (mode)
20817 && quad_address_p (XEXP (dest, 0), mode, true))
20818 return "stxv %x1,%0";
20820 else if (TARGET_P9_VECTOR)
20821 return "stxvx %x1,%y0";
20823 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
20824 return "stxvw4x %x1,%y0";
20826 else
20827 return "stxvd2x %x1,%y0";
20830 else if (TARGET_ALTIVEC && src_vmx_p)
20831 return "stvx %1,%y0";
20833 else if (src_fp_p)
20834 return "#";
20837 /* Constants. */
20838 else if (dest_regno >= 0
20839 && (GET_CODE (src) == CONST_INT
20840 || GET_CODE (src) == CONST_WIDE_INT
20841 || GET_CODE (src) == CONST_DOUBLE
20842 || GET_CODE (src) == CONST_VECTOR))
20844 if (dest_gpr_p)
20845 return "#";
20847 else if ((dest_vmx_p && TARGET_ALTIVEC)
20848 || (dest_vsx_p && TARGET_VSX))
20849 return output_vec_const_move (operands);
20852 fatal_insn ("Bad 128-bit move", gen_rtx_SET (dest, src));
20855 /* Validate a 128-bit move. */
20856 bool
20857 rs6000_move_128bit_ok_p (rtx operands[])
20859 machine_mode mode = GET_MODE (operands[0]);
20860 return (gpc_reg_operand (operands[0], mode)
20861 || gpc_reg_operand (operands[1], mode));
20864 /* Return true if a 128-bit move needs to be split. */
20865 bool
20866 rs6000_split_128bit_ok_p (rtx operands[])
20868 if (!reload_completed)
20869 return false;
20871 if (!gpr_or_gpr_p (operands[0], operands[1]))
20872 return false;
20874 if (quad_load_store_p (operands[0], operands[1]))
20875 return false;
20877 return true;
20881 /* Given a comparison operation, return the bit number in CCR to test. We
20882 know this is a valid comparison.
20884 SCC_P is 1 if this is for an scc. That means that %D will have been
20885 used instead of %C, so the bits will be in different places.
20887 Return -1 if OP isn't a valid comparison for some reason. */
20890 ccr_bit (rtx op, int scc_p)
20892 enum rtx_code code = GET_CODE (op);
20893 machine_mode cc_mode;
20894 int cc_regnum;
20895 int base_bit;
20896 rtx reg;
20898 if (!COMPARISON_P (op))
20899 return -1;
20901 reg = XEXP (op, 0);
20903 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
20905 cc_mode = GET_MODE (reg);
20906 cc_regnum = REGNO (reg);
20907 base_bit = 4 * (cc_regnum - CR0_REGNO);
20909 validate_condition_mode (code, cc_mode);
20911 /* When generating a sCOND operation, only positive conditions are
20912 allowed. */
20913 gcc_assert (!scc_p
20914 || code == EQ || code == GT || code == LT || code == UNORDERED
20915 || code == GTU || code == LTU);
20917 switch (code)
20919 case NE:
20920 return scc_p ? base_bit + 3 : base_bit + 2;
20921 case EQ:
20922 return base_bit + 2;
20923 case GT: case GTU: case UNLE:
20924 return base_bit + 1;
20925 case LT: case LTU: case UNGE:
20926 return base_bit;
20927 case ORDERED: case UNORDERED:
20928 return base_bit + 3;
20930 case GE: case GEU:
20931 /* If scc, we will have done a cror to put the bit in the
20932 unordered position. So test that bit. For integer, this is ! LT
20933 unless this is an scc insn. */
20934 return scc_p ? base_bit + 3 : base_bit;
20936 case LE: case LEU:
20937 return scc_p ? base_bit + 3 : base_bit + 1;
20939 default:
20940 gcc_unreachable ();
20944 /* Return the GOT register. */
20947 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
20949 /* The second flow pass currently (June 1999) can't update
20950 regs_ever_live without disturbing other parts of the compiler, so
20951 update it here to make the prolog/epilogue code happy. */
20952 if (!can_create_pseudo_p ()
20953 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
20954 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
20956 crtl->uses_pic_offset_table = 1;
20958 return pic_offset_table_rtx;
20961 static rs6000_stack_t stack_info;
20963 /* Function to init struct machine_function.
20964 This will be called, via a pointer variable,
20965 from push_function_context. */
20967 static struct machine_function *
20968 rs6000_init_machine_status (void)
20970 stack_info.reload_completed = 0;
20971 return ggc_cleared_alloc<machine_function> ();
20974 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
20976 /* Write out a function code label. */
20978 void
20979 rs6000_output_function_entry (FILE *file, const char *fname)
20981 if (fname[0] != '.')
20983 switch (DEFAULT_ABI)
20985 default:
20986 gcc_unreachable ();
20988 case ABI_AIX:
20989 if (DOT_SYMBOLS)
20990 putc ('.', file);
20991 else
20992 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
20993 break;
20995 case ABI_ELFv2:
20996 case ABI_V4:
20997 case ABI_DARWIN:
20998 break;
21002 RS6000_OUTPUT_BASENAME (file, fname);
21005 /* Print an operand. Recognize special options, documented below. */
21007 #if TARGET_ELF
21008 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
21009 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
21010 #else
21011 #define SMALL_DATA_RELOC "sda21"
21012 #define SMALL_DATA_REG 0
21013 #endif
21015 void
21016 print_operand (FILE *file, rtx x, int code)
21018 int i;
21019 unsigned HOST_WIDE_INT uval;
21021 switch (code)
21023 /* %a is output_address. */
21025 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
21026 output_operand. */
21028 case 'D':
21029 /* Like 'J' but get to the GT bit only. */
21030 gcc_assert (REG_P (x));
21032 /* Bit 1 is GT bit. */
21033 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
21035 /* Add one for shift count in rlinm for scc. */
21036 fprintf (file, "%d", i + 1);
21037 return;
21039 case 'e':
21040 /* If the low 16 bits are 0, but some other bit is set, write 's'. */
21041 if (! INT_P (x))
21043 output_operand_lossage ("invalid %%e value");
21044 return;
21047 uval = INTVAL (x);
21048 if ((uval & 0xffff) == 0 && uval != 0)
21049 putc ('s', file);
21050 return;
21052 case 'E':
21053 /* X is a CR register. Print the number of the EQ bit of the CR */
21054 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
21055 output_operand_lossage ("invalid %%E value");
21056 else
21057 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
21058 return;
21060 case 'f':
21061 /* X is a CR register. Print the shift count needed to move it
21062 to the high-order four bits. */
21063 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
21064 output_operand_lossage ("invalid %%f value");
21065 else
21066 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
21067 return;
21069 case 'F':
21070 /* Similar, but print the count for the rotate in the opposite
21071 direction. */
21072 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
21073 output_operand_lossage ("invalid %%F value");
21074 else
21075 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
21076 return;
21078 case 'G':
21079 /* X is a constant integer. If it is negative, print "m",
21080 otherwise print "z". This is to make an aze or ame insn. */
21081 if (GET_CODE (x) != CONST_INT)
21082 output_operand_lossage ("invalid %%G value");
21083 else if (INTVAL (x) >= 0)
21084 putc ('z', file);
21085 else
21086 putc ('m', file);
21087 return;
21089 case 'h':
21090 /* If constant, output low-order five bits. Otherwise, write
21091 normally. */
21092 if (INT_P (x))
21093 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
21094 else
21095 print_operand (file, x, 0);
21096 return;
21098 case 'H':
21099 /* If constant, output low-order six bits. Otherwise, write
21100 normally. */
21101 if (INT_P (x))
21102 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
21103 else
21104 print_operand (file, x, 0);
21105 return;
21107 case 'I':
21108 /* Print `i' if this is a constant, else nothing. */
21109 if (INT_P (x))
21110 putc ('i', file);
21111 return;
21113 case 'j':
21114 /* Write the bit number in CCR for jump. */
21115 i = ccr_bit (x, 0);
21116 if (i == -1)
21117 output_operand_lossage ("invalid %%j code");
21118 else
21119 fprintf (file, "%d", i);
21120 return;
21122 case 'J':
21123 /* Similar, but add one for shift count in rlinm for scc and pass
21124 scc flag to `ccr_bit'. */
21125 i = ccr_bit (x, 1);
21126 if (i == -1)
21127 output_operand_lossage ("invalid %%J code");
21128 else
21129 /* If we want bit 31, write a shift count of zero, not 32. */
21130 fprintf (file, "%d", i == 31 ? 0 : i + 1);
21131 return;
21133 case 'k':
21134 /* X must be a constant. Write the 1's complement of the
21135 constant. */
21136 if (! INT_P (x))
21137 output_operand_lossage ("invalid %%k value");
21138 else
21139 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
21140 return;
21142 case 'K':
21143 /* X must be a symbolic constant on ELF. Write an
21144 expression suitable for an 'addi' that adds in the low 16
21145 bits of the MEM. */
21146 if (GET_CODE (x) == CONST)
21148 if (GET_CODE (XEXP (x, 0)) != PLUS
21149 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
21150 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
21151 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
21152 output_operand_lossage ("invalid %%K value");
21154 print_operand_address (file, x);
21155 fputs ("@l", file);
21156 return;
21158 /* %l is output_asm_label. */
21160 case 'L':
21161 /* Write second word of DImode or DFmode reference. Works on register
21162 or non-indexed memory only. */
21163 if (REG_P (x))
21164 fputs (reg_names[REGNO (x) + 1], file);
21165 else if (MEM_P (x))
21167 machine_mode mode = GET_MODE (x);
21168 /* Handle possible auto-increment. Since it is pre-increment and
21169 we have already done it, we can just use an offset of word. */
21170 if (GET_CODE (XEXP (x, 0)) == PRE_INC
21171 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
21172 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
21173 UNITS_PER_WORD));
21174 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
21175 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
21176 UNITS_PER_WORD));
21177 else
21178 output_address (mode, XEXP (adjust_address_nv (x, SImode,
21179 UNITS_PER_WORD),
21180 0));
21182 if (small_data_operand (x, GET_MODE (x)))
21183 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
21184 reg_names[SMALL_DATA_REG]);
21186 return;
21188 case 'N':
21189 /* Write the number of elements in the vector times 4. */
21190 if (GET_CODE (x) != PARALLEL)
21191 output_operand_lossage ("invalid %%N value");
21192 else
21193 fprintf (file, "%d", XVECLEN (x, 0) * 4);
21194 return;
21196 case 'O':
21197 /* Similar, but subtract 1 first. */
21198 if (GET_CODE (x) != PARALLEL)
21199 output_operand_lossage ("invalid %%O value");
21200 else
21201 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
21202 return;
21204 case 'p':
21205 /* X is a CONST_INT that is a power of two. Output the logarithm. */
21206 if (! INT_P (x)
21207 || INTVAL (x) < 0
21208 || (i = exact_log2 (INTVAL (x))) < 0)
21209 output_operand_lossage ("invalid %%p value");
21210 else
21211 fprintf (file, "%d", i);
21212 return;
21214 case 'P':
21215 /* The operand must be an indirect memory reference. The result
21216 is the register name. */
21217 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
21218 || REGNO (XEXP (x, 0)) >= 32)
21219 output_operand_lossage ("invalid %%P value");
21220 else
21221 fputs (reg_names[REGNO (XEXP (x, 0))], file);
21222 return;
21224 case 'q':
21225 /* This outputs the logical code corresponding to a boolean
21226 expression. The expression may have one or both operands
21227 negated (if one, only the first one). For condition register
21228 logical operations, it will also treat the negated
21229 CR codes as NOTs, but not handle NOTs of them. */
21231 const char *const *t = 0;
21232 const char *s;
21233 enum rtx_code code = GET_CODE (x);
21234 static const char * const tbl[3][3] = {
21235 { "and", "andc", "nor" },
21236 { "or", "orc", "nand" },
21237 { "xor", "eqv", "xor" } };
21239 if (code == AND)
21240 t = tbl[0];
21241 else if (code == IOR)
21242 t = tbl[1];
21243 else if (code == XOR)
21244 t = tbl[2];
21245 else
21246 output_operand_lossage ("invalid %%q value");
21248 if (GET_CODE (XEXP (x, 0)) != NOT)
21249 s = t[0];
21250 else
21252 if (GET_CODE (XEXP (x, 1)) == NOT)
21253 s = t[2];
21254 else
21255 s = t[1];
21258 fputs (s, file);
21260 return;
21262 case 'Q':
21263 if (! TARGET_MFCRF)
21264 return;
21265 fputc (',', file);
21266 /* FALLTHRU */
21268 case 'R':
21269 /* X is a CR register. Print the mask for `mtcrf'. */
21270 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
21271 output_operand_lossage ("invalid %%R value");
21272 else
21273 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
21274 return;
21276 case 's':
21277 /* Low 5 bits of 32 - value */
21278 if (! INT_P (x))
21279 output_operand_lossage ("invalid %%s value");
21280 else
21281 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
21282 return;
21284 case 't':
21285 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
21286 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
21288 /* Bit 3 is OV bit. */
21289 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
21291 /* If we want bit 31, write a shift count of zero, not 32. */
21292 fprintf (file, "%d", i == 31 ? 0 : i + 1);
21293 return;
21295 case 'T':
21296 /* Print the symbolic name of a branch target register. */
21297 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
21298 && REGNO (x) != CTR_REGNO))
21299 output_operand_lossage ("invalid %%T value");
21300 else if (REGNO (x) == LR_REGNO)
21301 fputs ("lr", file);
21302 else
21303 fputs ("ctr", file);
21304 return;
21306 case 'u':
21307 /* High-order or low-order 16 bits of constant, whichever is non-zero,
21308 for use in unsigned operand. */
21309 if (! INT_P (x))
21311 output_operand_lossage ("invalid %%u value");
21312 return;
21315 uval = INTVAL (x);
21316 if ((uval & 0xffff) == 0)
21317 uval >>= 16;
21319 fprintf (file, HOST_WIDE_INT_PRINT_HEX, uval & 0xffff);
21320 return;
21322 case 'v':
21323 /* High-order 16 bits of constant for use in signed operand. */
21324 if (! INT_P (x))
21325 output_operand_lossage ("invalid %%v value");
21326 else
21327 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
21328 (INTVAL (x) >> 16) & 0xffff);
21329 return;
21331 case 'U':
21332 /* Print `u' if this has an auto-increment or auto-decrement. */
21333 if (MEM_P (x)
21334 && (GET_CODE (XEXP (x, 0)) == PRE_INC
21335 || GET_CODE (XEXP (x, 0)) == PRE_DEC
21336 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
21337 putc ('u', file);
21338 return;
21340 case 'V':
21341 /* Print the trap code for this operand. */
21342 switch (GET_CODE (x))
21344 case EQ:
21345 fputs ("eq", file); /* 4 */
21346 break;
21347 case NE:
21348 fputs ("ne", file); /* 24 */
21349 break;
21350 case LT:
21351 fputs ("lt", file); /* 16 */
21352 break;
21353 case LE:
21354 fputs ("le", file); /* 20 */
21355 break;
21356 case GT:
21357 fputs ("gt", file); /* 8 */
21358 break;
21359 case GE:
21360 fputs ("ge", file); /* 12 */
21361 break;
21362 case LTU:
21363 fputs ("llt", file); /* 2 */
21364 break;
21365 case LEU:
21366 fputs ("lle", file); /* 6 */
21367 break;
21368 case GTU:
21369 fputs ("lgt", file); /* 1 */
21370 break;
21371 case GEU:
21372 fputs ("lge", file); /* 5 */
21373 break;
21374 default:
21375 gcc_unreachable ();
21377 break;
21379 case 'w':
21380 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
21381 normally. */
21382 if (INT_P (x))
21383 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
21384 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
21385 else
21386 print_operand (file, x, 0);
21387 return;
21389 case 'x':
21390 /* X is a FPR or Altivec register used in a VSX context. */
21391 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
21392 output_operand_lossage ("invalid %%x value");
21393 else
21395 int reg = REGNO (x);
21396 int vsx_reg = (FP_REGNO_P (reg)
21397 ? reg - 32
21398 : reg - FIRST_ALTIVEC_REGNO + 32);
21400 #ifdef TARGET_REGNAMES
21401 if (TARGET_REGNAMES)
21402 fprintf (file, "%%vs%d", vsx_reg);
21403 else
21404 #endif
21405 fprintf (file, "%d", vsx_reg);
21407 return;
21409 case 'X':
21410 if (MEM_P (x)
21411 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
21412 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
21413 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
21414 putc ('x', file);
21415 return;
21417 case 'Y':
21418 /* Like 'L', for third word of TImode/PTImode */
21419 if (REG_P (x))
21420 fputs (reg_names[REGNO (x) + 2], file);
21421 else if (MEM_P (x))
21423 machine_mode mode = GET_MODE (x);
21424 if (GET_CODE (XEXP (x, 0)) == PRE_INC
21425 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
21426 output_address (mode, plus_constant (Pmode,
21427 XEXP (XEXP (x, 0), 0), 8));
21428 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
21429 output_address (mode, plus_constant (Pmode,
21430 XEXP (XEXP (x, 0), 0), 8));
21431 else
21432 output_address (mode, XEXP (adjust_address_nv (x, SImode, 8), 0));
21433 if (small_data_operand (x, GET_MODE (x)))
21434 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
21435 reg_names[SMALL_DATA_REG]);
21437 return;
21439 case 'z':
21440 /* X is a SYMBOL_REF. Write out the name preceded by a
21441 period and without any trailing data in brackets. Used for function
21442 names. If we are configured for System V (or the embedded ABI) on
21443 the PowerPC, do not emit the period, since those systems do not use
21444 TOCs and the like. */
21445 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21447 /* For macho, check to see if we need a stub. */
21448 if (TARGET_MACHO)
21450 const char *name = XSTR (x, 0);
21451 #if TARGET_MACHO
21452 if (darwin_emit_branch_islands
21453 && MACHOPIC_INDIRECT
21454 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
21455 name = machopic_indirection_name (x, /*stub_p=*/true);
21456 #endif
21457 assemble_name (file, name);
21459 else if (!DOT_SYMBOLS)
21460 assemble_name (file, XSTR (x, 0));
21461 else
21462 rs6000_output_function_entry (file, XSTR (x, 0));
21463 return;
21465 case 'Z':
21466 /* Like 'L', for last word of TImode/PTImode. */
21467 if (REG_P (x))
21468 fputs (reg_names[REGNO (x) + 3], file);
21469 else if (MEM_P (x))
21471 machine_mode mode = GET_MODE (x);
21472 if (GET_CODE (XEXP (x, 0)) == PRE_INC
21473 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
21474 output_address (mode, plus_constant (Pmode,
21475 XEXP (XEXP (x, 0), 0), 12));
21476 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
21477 output_address (mode, plus_constant (Pmode,
21478 XEXP (XEXP (x, 0), 0), 12));
21479 else
21480 output_address (mode, XEXP (adjust_address_nv (x, SImode, 12), 0));
21481 if (small_data_operand (x, GET_MODE (x)))
21482 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
21483 reg_names[SMALL_DATA_REG]);
21485 return;
21487 /* Print AltiVec memory operand. */
21488 case 'y':
21490 rtx tmp;
21492 gcc_assert (MEM_P (x));
21494 tmp = XEXP (x, 0);
21496 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
21497 && GET_CODE (tmp) == AND
21498 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
21499 && INTVAL (XEXP (tmp, 1)) == -16)
21500 tmp = XEXP (tmp, 0);
21501 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
21502 && GET_CODE (tmp) == PRE_MODIFY)
21503 tmp = XEXP (tmp, 1);
21504 if (REG_P (tmp))
21505 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
21506 else
21508 if (GET_CODE (tmp) != PLUS
21509 || !REG_P (XEXP (tmp, 0))
21510 || !REG_P (XEXP (tmp, 1)))
21512 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
21513 break;
21516 if (REGNO (XEXP (tmp, 0)) == 0)
21517 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
21518 reg_names[ REGNO (XEXP (tmp, 0)) ]);
21519 else
21520 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
21521 reg_names[ REGNO (XEXP (tmp, 1)) ]);
21523 break;
21526 case 0:
21527 if (REG_P (x))
21528 fprintf (file, "%s", reg_names[REGNO (x)]);
21529 else if (MEM_P (x))
21531 /* We need to handle PRE_INC and PRE_DEC here, since we need to
21532 know the width from the mode. */
21533 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
21534 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
21535 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
21536 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
21537 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
21538 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
21539 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
21540 output_address (GET_MODE (x), XEXP (XEXP (x, 0), 1));
21541 else
21542 output_address (GET_MODE (x), XEXP (x, 0));
21544 else
21546 if (toc_relative_expr_p (x, false, &tocrel_base_oac, &tocrel_offset_oac))
21547 /* This hack along with a corresponding hack in
21548 rs6000_output_addr_const_extra arranges to output addends
21549 where the assembler expects to find them. eg.
21550 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
21551 without this hack would be output as "x@toc+4". We
21552 want "x+4@toc". */
21553 output_addr_const (file, CONST_CAST_RTX (tocrel_base_oac));
21554 else
21555 output_addr_const (file, x);
21557 return;
21559 case '&':
21560 if (const char *name = get_some_local_dynamic_name ())
21561 assemble_name (file, name);
21562 else
21563 output_operand_lossage ("'%%&' used without any "
21564 "local dynamic TLS references");
21565 return;
21567 default:
21568 output_operand_lossage ("invalid %%xn code");
21572 /* Print the address of an operand. */
21574 void
21575 print_operand_address (FILE *file, rtx x)
21577 if (REG_P (x))
21578 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
21579 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
21580 || GET_CODE (x) == LABEL_REF)
21582 output_addr_const (file, x);
21583 if (small_data_operand (x, GET_MODE (x)))
21584 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
21585 reg_names[SMALL_DATA_REG]);
21586 else
21587 gcc_assert (!TARGET_TOC);
21589 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
21590 && REG_P (XEXP (x, 1)))
21592 if (REGNO (XEXP (x, 0)) == 0)
21593 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
21594 reg_names[ REGNO (XEXP (x, 0)) ]);
21595 else
21596 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
21597 reg_names[ REGNO (XEXP (x, 1)) ]);
21599 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
21600 && GET_CODE (XEXP (x, 1)) == CONST_INT)
21601 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
21602 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
21603 #if TARGET_MACHO
21604 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
21605 && CONSTANT_P (XEXP (x, 1)))
21607 fprintf (file, "lo16(");
21608 output_addr_const (file, XEXP (x, 1));
21609 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
21611 #endif
21612 #if TARGET_ELF
21613 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
21614 && CONSTANT_P (XEXP (x, 1)))
21616 output_addr_const (file, XEXP (x, 1));
21617 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
21619 #endif
21620 else if (toc_relative_expr_p (x, false, &tocrel_base_oac, &tocrel_offset_oac))
21622 /* This hack along with a corresponding hack in
21623 rs6000_output_addr_const_extra arranges to output addends
21624 where the assembler expects to find them. eg.
21625 (lo_sum (reg 9)
21626 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
21627 without this hack would be output as "x@toc+8@l(9)". We
21628 want "x+8@toc@l(9)". */
21629 output_addr_const (file, CONST_CAST_RTX (tocrel_base_oac));
21630 if (GET_CODE (x) == LO_SUM)
21631 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
21632 else
21633 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base_oac, 0, 1))]);
21635 else
21636 gcc_unreachable ();
21639 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
21641 static bool
21642 rs6000_output_addr_const_extra (FILE *file, rtx x)
21644 if (GET_CODE (x) == UNSPEC)
21645 switch (XINT (x, 1))
21647 case UNSPEC_TOCREL:
21648 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
21649 && REG_P (XVECEXP (x, 0, 1))
21650 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
21651 output_addr_const (file, XVECEXP (x, 0, 0));
21652 if (x == tocrel_base_oac && tocrel_offset_oac != const0_rtx)
21654 if (INTVAL (tocrel_offset_oac) >= 0)
21655 fprintf (file, "+");
21656 output_addr_const (file, CONST_CAST_RTX (tocrel_offset_oac));
21658 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
21660 putc ('-', file);
21661 assemble_name (file, toc_label_name);
21662 need_toc_init = 1;
21664 else if (TARGET_ELF)
21665 fputs ("@toc", file);
21666 return true;
21668 #if TARGET_MACHO
21669 case UNSPEC_MACHOPIC_OFFSET:
21670 output_addr_const (file, XVECEXP (x, 0, 0));
21671 putc ('-', file);
21672 machopic_output_function_base_name (file);
21673 return true;
21674 #endif
21676 return false;
21679 /* Target hook for assembling integer objects. The PowerPC version has
21680 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
21681 is defined. It also needs to handle DI-mode objects on 64-bit
21682 targets. */
21684 static bool
21685 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
21687 #ifdef RELOCATABLE_NEEDS_FIXUP
21688 /* Special handling for SI values. */
21689 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
21691 static int recurse = 0;
21693 /* For -mrelocatable, we mark all addresses that need to be fixed up in
21694 the .fixup section. Since the TOC section is already relocated, we
21695 don't need to mark it here. We used to skip the text section, but it
21696 should never be valid for relocated addresses to be placed in the text
21697 section. */
21698 if (DEFAULT_ABI == ABI_V4
21699 && (TARGET_RELOCATABLE || flag_pic > 1)
21700 && in_section != toc_section
21701 && !recurse
21702 && !CONST_SCALAR_INT_P (x)
21703 && CONSTANT_P (x))
21705 char buf[256];
21707 recurse = 1;
21708 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
21709 fixuplabelno++;
21710 ASM_OUTPUT_LABEL (asm_out_file, buf);
21711 fprintf (asm_out_file, "\t.long\t(");
21712 output_addr_const (asm_out_file, x);
21713 fprintf (asm_out_file, ")@fixup\n");
21714 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
21715 ASM_OUTPUT_ALIGN (asm_out_file, 2);
21716 fprintf (asm_out_file, "\t.long\t");
21717 assemble_name (asm_out_file, buf);
21718 fprintf (asm_out_file, "\n\t.previous\n");
21719 recurse = 0;
21720 return true;
21722 /* Remove initial .'s to turn a -mcall-aixdesc function
21723 address into the address of the descriptor, not the function
21724 itself. */
21725 else if (GET_CODE (x) == SYMBOL_REF
21726 && XSTR (x, 0)[0] == '.'
21727 && DEFAULT_ABI == ABI_AIX)
21729 const char *name = XSTR (x, 0);
21730 while (*name == '.')
21731 name++;
21733 fprintf (asm_out_file, "\t.long\t%s\n", name);
21734 return true;
21737 #endif /* RELOCATABLE_NEEDS_FIXUP */
21738 return default_assemble_integer (x, size, aligned_p);
21741 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
21742 /* Emit an assembler directive to set symbol visibility for DECL to
21743 VISIBILITY_TYPE. */
21745 static void
21746 rs6000_assemble_visibility (tree decl, int vis)
21748 if (TARGET_XCOFF)
21749 return;
21751 /* Functions need to have their entry point symbol visibility set as
21752 well as their descriptor symbol visibility. */
21753 if (DEFAULT_ABI == ABI_AIX
21754 && DOT_SYMBOLS
21755 && TREE_CODE (decl) == FUNCTION_DECL)
21757 static const char * const visibility_types[] = {
21758 NULL, "protected", "hidden", "internal"
21761 const char *name, *type;
21763 name = ((* targetm.strip_name_encoding)
21764 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
21765 type = visibility_types[vis];
21767 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
21768 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
21770 else
21771 default_assemble_visibility (decl, vis);
21773 #endif
21775 enum rtx_code
21776 rs6000_reverse_condition (machine_mode mode, enum rtx_code code)
21778 /* Reversal of FP compares takes care -- an ordered compare
21779 becomes an unordered compare and vice versa. */
21780 if (mode == CCFPmode
21781 && (!flag_finite_math_only
21782 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
21783 || code == UNEQ || code == LTGT))
21784 return reverse_condition_maybe_unordered (code);
21785 else
21786 return reverse_condition (code);
21789 /* Generate a compare for CODE. Return a brand-new rtx that
21790 represents the result of the compare. */
21792 static rtx
21793 rs6000_generate_compare (rtx cmp, machine_mode mode)
21795 machine_mode comp_mode;
21796 rtx compare_result;
21797 enum rtx_code code = GET_CODE (cmp);
21798 rtx op0 = XEXP (cmp, 0);
21799 rtx op1 = XEXP (cmp, 1);
21801 if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
21802 comp_mode = CCmode;
21803 else if (FLOAT_MODE_P (mode))
21804 comp_mode = CCFPmode;
21805 else if (code == GTU || code == LTU
21806 || code == GEU || code == LEU)
21807 comp_mode = CCUNSmode;
21808 else if ((code == EQ || code == NE)
21809 && unsigned_reg_p (op0)
21810 && (unsigned_reg_p (op1)
21811 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
21812 /* These are unsigned values, perhaps there will be a later
21813 ordering compare that can be shared with this one. */
21814 comp_mode = CCUNSmode;
21815 else
21816 comp_mode = CCmode;
21818 /* If we have an unsigned compare, make sure we don't have a signed value as
21819 an immediate. */
21820 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
21821 && INTVAL (op1) < 0)
21823 op0 = copy_rtx_if_shared (op0);
21824 op1 = force_reg (GET_MODE (op0), op1);
21825 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
21828 /* First, the compare. */
21829 compare_result = gen_reg_rtx (comp_mode);
21831 /* IEEE 128-bit support in VSX registers when we do not have hardware
21832 support. */
21833 if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
21835 rtx libfunc = NULL_RTX;
21836 bool check_nan = false;
21837 rtx dest;
21839 switch (code)
21841 case EQ:
21842 case NE:
21843 libfunc = optab_libfunc (eq_optab, mode);
21844 break;
21846 case GT:
21847 case GE:
21848 libfunc = optab_libfunc (ge_optab, mode);
21849 break;
21851 case LT:
21852 case LE:
21853 libfunc = optab_libfunc (le_optab, mode);
21854 break;
21856 case UNORDERED:
21857 case ORDERED:
21858 libfunc = optab_libfunc (unord_optab, mode);
21859 code = (code == UNORDERED) ? NE : EQ;
21860 break;
21862 case UNGE:
21863 case UNGT:
21864 check_nan = true;
21865 libfunc = optab_libfunc (ge_optab, mode);
21866 code = (code == UNGE) ? GE : GT;
21867 break;
21869 case UNLE:
21870 case UNLT:
21871 check_nan = true;
21872 libfunc = optab_libfunc (le_optab, mode);
21873 code = (code == UNLE) ? LE : LT;
21874 break;
21876 case UNEQ:
21877 case LTGT:
21878 check_nan = true;
21879 libfunc = optab_libfunc (eq_optab, mode);
21880 code = (code = UNEQ) ? EQ : NE;
21881 break;
21883 default:
21884 gcc_unreachable ();
21887 gcc_assert (libfunc);
21889 if (!check_nan)
21890 dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
21891 SImode, 2, op0, mode, op1, mode);
21893 /* The library signals an exception for signalling NaNs, so we need to
21894 handle isgreater, etc. by first checking isordered. */
21895 else
21897 rtx ne_rtx, normal_dest, unord_dest;
21898 rtx unord_func = optab_libfunc (unord_optab, mode);
21899 rtx join_label = gen_label_rtx ();
21900 rtx join_ref = gen_rtx_LABEL_REF (VOIDmode, join_label);
21901 rtx unord_cmp = gen_reg_rtx (comp_mode);
21904 /* Test for either value being a NaN. */
21905 gcc_assert (unord_func);
21906 unord_dest = emit_library_call_value (unord_func, NULL_RTX, LCT_CONST,
21907 SImode, 2, op0, mode, op1,
21908 mode);
21910 /* Set value (0) if either value is a NaN, and jump to the join
21911 label. */
21912 dest = gen_reg_rtx (SImode);
21913 emit_move_insn (dest, const1_rtx);
21914 emit_insn (gen_rtx_SET (unord_cmp,
21915 gen_rtx_COMPARE (comp_mode, unord_dest,
21916 const0_rtx)));
21918 ne_rtx = gen_rtx_NE (comp_mode, unord_cmp, const0_rtx);
21919 emit_jump_insn (gen_rtx_SET (pc_rtx,
21920 gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
21921 join_ref,
21922 pc_rtx)));
21924 /* Do the normal comparison, knowing that the values are not
21925 NaNs. */
21926 normal_dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
21927 SImode, 2, op0, mode, op1,
21928 mode);
21930 emit_insn (gen_cstoresi4 (dest,
21931 gen_rtx_fmt_ee (code, SImode, normal_dest,
21932 const0_rtx),
21933 normal_dest, const0_rtx));
21935 /* Join NaN and non-Nan paths. Compare dest against 0. */
21936 emit_label (join_label);
21937 code = NE;
21940 emit_insn (gen_rtx_SET (compare_result,
21941 gen_rtx_COMPARE (comp_mode, dest, const0_rtx)));
21944 else
21946 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
21947 CLOBBERs to match cmptf_internal2 pattern. */
21948 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
21949 && FLOAT128_IBM_P (GET_MODE (op0))
21950 && TARGET_HARD_FLOAT)
21951 emit_insn (gen_rtx_PARALLEL (VOIDmode,
21952 gen_rtvec (10,
21953 gen_rtx_SET (compare_result,
21954 gen_rtx_COMPARE (comp_mode, op0, op1)),
21955 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21956 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21957 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21958 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21959 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21960 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21961 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21962 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21963 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
21964 else if (GET_CODE (op1) == UNSPEC
21965 && XINT (op1, 1) == UNSPEC_SP_TEST)
21967 rtx op1b = XVECEXP (op1, 0, 0);
21968 comp_mode = CCEQmode;
21969 compare_result = gen_reg_rtx (CCEQmode);
21970 if (TARGET_64BIT)
21971 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
21972 else
21973 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
21975 else
21976 emit_insn (gen_rtx_SET (compare_result,
21977 gen_rtx_COMPARE (comp_mode, op0, op1)));
21980 /* Some kinds of FP comparisons need an OR operation;
21981 under flag_finite_math_only we don't bother. */
21982 if (FLOAT_MODE_P (mode)
21983 && (!FLOAT128_IEEE_P (mode) || TARGET_FLOAT128_HW)
21984 && !flag_finite_math_only
21985 && (code == LE || code == GE
21986 || code == UNEQ || code == LTGT
21987 || code == UNGT || code == UNLT))
21989 enum rtx_code or1, or2;
21990 rtx or1_rtx, or2_rtx, compare2_rtx;
21991 rtx or_result = gen_reg_rtx (CCEQmode);
21993 switch (code)
21995 case LE: or1 = LT; or2 = EQ; break;
21996 case GE: or1 = GT; or2 = EQ; break;
21997 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
21998 case LTGT: or1 = LT; or2 = GT; break;
21999 case UNGT: or1 = UNORDERED; or2 = GT; break;
22000 case UNLT: or1 = UNORDERED; or2 = LT; break;
22001 default: gcc_unreachable ();
22003 validate_condition_mode (or1, comp_mode);
22004 validate_condition_mode (or2, comp_mode);
22005 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
22006 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
22007 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
22008 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
22009 const_true_rtx);
22010 emit_insn (gen_rtx_SET (or_result, compare2_rtx));
22012 compare_result = or_result;
22013 code = EQ;
22016 validate_condition_mode (code, GET_MODE (compare_result));
22018 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
22022 /* Return the diagnostic message string if the binary operation OP is
22023 not permitted on TYPE1 and TYPE2, NULL otherwise. */
22025 static const char*
22026 rs6000_invalid_binary_op (int op ATTRIBUTE_UNUSED,
22027 const_tree type1,
22028 const_tree type2)
22030 machine_mode mode1 = TYPE_MODE (type1);
22031 machine_mode mode2 = TYPE_MODE (type2);
22033 /* For complex modes, use the inner type. */
22034 if (COMPLEX_MODE_P (mode1))
22035 mode1 = GET_MODE_INNER (mode1);
22037 if (COMPLEX_MODE_P (mode2))
22038 mode2 = GET_MODE_INNER (mode2);
22040 /* Don't allow IEEE 754R 128-bit binary floating point and IBM extended
22041 double to intermix unless -mfloat128-convert. */
22042 if (mode1 == mode2)
22043 return NULL;
22045 if (!TARGET_FLOAT128_CVT)
22047 if ((mode1 == KFmode && mode2 == IFmode)
22048 || (mode1 == IFmode && mode2 == KFmode))
22049 return N_("__float128 and __ibm128 cannot be used in the same "
22050 "expression");
22052 if (TARGET_IEEEQUAD
22053 && ((mode1 == IFmode && mode2 == TFmode)
22054 || (mode1 == TFmode && mode2 == IFmode)))
22055 return N_("__ibm128 and long double cannot be used in the same "
22056 "expression");
22058 if (!TARGET_IEEEQUAD
22059 && ((mode1 == KFmode && mode2 == TFmode)
22060 || (mode1 == TFmode && mode2 == KFmode)))
22061 return N_("__float128 and long double cannot be used in the same "
22062 "expression");
22065 return NULL;
22069 /* Expand floating point conversion to/from __float128 and __ibm128. */
22071 void
22072 rs6000_expand_float128_convert (rtx dest, rtx src, bool unsigned_p)
22074 machine_mode dest_mode = GET_MODE (dest);
22075 machine_mode src_mode = GET_MODE (src);
22076 convert_optab cvt = unknown_optab;
22077 bool do_move = false;
22078 rtx libfunc = NULL_RTX;
22079 rtx dest2;
22080 typedef rtx (*rtx_2func_t) (rtx, rtx);
22081 rtx_2func_t hw_convert = (rtx_2func_t)0;
22082 size_t kf_or_tf;
22084 struct hw_conv_t {
22085 rtx_2func_t from_df;
22086 rtx_2func_t from_sf;
22087 rtx_2func_t from_si_sign;
22088 rtx_2func_t from_si_uns;
22089 rtx_2func_t from_di_sign;
22090 rtx_2func_t from_di_uns;
22091 rtx_2func_t to_df;
22092 rtx_2func_t to_sf;
22093 rtx_2func_t to_si_sign;
22094 rtx_2func_t to_si_uns;
22095 rtx_2func_t to_di_sign;
22096 rtx_2func_t to_di_uns;
22097 } hw_conversions[2] = {
22098 /* convertions to/from KFmode */
22100 gen_extenddfkf2_hw, /* KFmode <- DFmode. */
22101 gen_extendsfkf2_hw, /* KFmode <- SFmode. */
22102 gen_float_kfsi2_hw, /* KFmode <- SImode (signed). */
22103 gen_floatuns_kfsi2_hw, /* KFmode <- SImode (unsigned). */
22104 gen_float_kfdi2_hw, /* KFmode <- DImode (signed). */
22105 gen_floatuns_kfdi2_hw, /* KFmode <- DImode (unsigned). */
22106 gen_trunckfdf2_hw, /* DFmode <- KFmode. */
22107 gen_trunckfsf2_hw, /* SFmode <- KFmode. */
22108 gen_fix_kfsi2_hw, /* SImode <- KFmode (signed). */
22109 gen_fixuns_kfsi2_hw, /* SImode <- KFmode (unsigned). */
22110 gen_fix_kfdi2_hw, /* DImode <- KFmode (signed). */
22111 gen_fixuns_kfdi2_hw, /* DImode <- KFmode (unsigned). */
22114 /* convertions to/from TFmode */
22116 gen_extenddftf2_hw, /* TFmode <- DFmode. */
22117 gen_extendsftf2_hw, /* TFmode <- SFmode. */
22118 gen_float_tfsi2_hw, /* TFmode <- SImode (signed). */
22119 gen_floatuns_tfsi2_hw, /* TFmode <- SImode (unsigned). */
22120 gen_float_tfdi2_hw, /* TFmode <- DImode (signed). */
22121 gen_floatuns_tfdi2_hw, /* TFmode <- DImode (unsigned). */
22122 gen_trunctfdf2_hw, /* DFmode <- TFmode. */
22123 gen_trunctfsf2_hw, /* SFmode <- TFmode. */
22124 gen_fix_tfsi2_hw, /* SImode <- TFmode (signed). */
22125 gen_fixuns_tfsi2_hw, /* SImode <- TFmode (unsigned). */
22126 gen_fix_tfdi2_hw, /* DImode <- TFmode (signed). */
22127 gen_fixuns_tfdi2_hw, /* DImode <- TFmode (unsigned). */
22131 if (dest_mode == src_mode)
22132 gcc_unreachable ();
22134 /* Eliminate memory operations. */
22135 if (MEM_P (src))
22136 src = force_reg (src_mode, src);
22138 if (MEM_P (dest))
22140 rtx tmp = gen_reg_rtx (dest_mode);
22141 rs6000_expand_float128_convert (tmp, src, unsigned_p);
22142 rs6000_emit_move (dest, tmp, dest_mode);
22143 return;
22146 /* Convert to IEEE 128-bit floating point. */
22147 if (FLOAT128_IEEE_P (dest_mode))
22149 if (dest_mode == KFmode)
22150 kf_or_tf = 0;
22151 else if (dest_mode == TFmode)
22152 kf_or_tf = 1;
22153 else
22154 gcc_unreachable ();
22156 switch (src_mode)
22158 case E_DFmode:
22159 cvt = sext_optab;
22160 hw_convert = hw_conversions[kf_or_tf].from_df;
22161 break;
22163 case E_SFmode:
22164 cvt = sext_optab;
22165 hw_convert = hw_conversions[kf_or_tf].from_sf;
22166 break;
22168 case E_KFmode:
22169 case E_IFmode:
22170 case E_TFmode:
22171 if (FLOAT128_IBM_P (src_mode))
22172 cvt = sext_optab;
22173 else
22174 do_move = true;
22175 break;
22177 case E_SImode:
22178 if (unsigned_p)
22180 cvt = ufloat_optab;
22181 hw_convert = hw_conversions[kf_or_tf].from_si_uns;
22183 else
22185 cvt = sfloat_optab;
22186 hw_convert = hw_conversions[kf_or_tf].from_si_sign;
22188 break;
22190 case E_DImode:
22191 if (unsigned_p)
22193 cvt = ufloat_optab;
22194 hw_convert = hw_conversions[kf_or_tf].from_di_uns;
22196 else
22198 cvt = sfloat_optab;
22199 hw_convert = hw_conversions[kf_or_tf].from_di_sign;
22201 break;
22203 default:
22204 gcc_unreachable ();
22208 /* Convert from IEEE 128-bit floating point. */
22209 else if (FLOAT128_IEEE_P (src_mode))
22211 if (src_mode == KFmode)
22212 kf_or_tf = 0;
22213 else if (src_mode == TFmode)
22214 kf_or_tf = 1;
22215 else
22216 gcc_unreachable ();
22218 switch (dest_mode)
22220 case E_DFmode:
22221 cvt = trunc_optab;
22222 hw_convert = hw_conversions[kf_or_tf].to_df;
22223 break;
22225 case E_SFmode:
22226 cvt = trunc_optab;
22227 hw_convert = hw_conversions[kf_or_tf].to_sf;
22228 break;
22230 case E_KFmode:
22231 case E_IFmode:
22232 case E_TFmode:
22233 if (FLOAT128_IBM_P (dest_mode))
22234 cvt = trunc_optab;
22235 else
22236 do_move = true;
22237 break;
22239 case E_SImode:
22240 if (unsigned_p)
22242 cvt = ufix_optab;
22243 hw_convert = hw_conversions[kf_or_tf].to_si_uns;
22245 else
22247 cvt = sfix_optab;
22248 hw_convert = hw_conversions[kf_or_tf].to_si_sign;
22250 break;
22252 case E_DImode:
22253 if (unsigned_p)
22255 cvt = ufix_optab;
22256 hw_convert = hw_conversions[kf_or_tf].to_di_uns;
22258 else
22260 cvt = sfix_optab;
22261 hw_convert = hw_conversions[kf_or_tf].to_di_sign;
22263 break;
22265 default:
22266 gcc_unreachable ();
22270 /* Both IBM format. */
22271 else if (FLOAT128_IBM_P (dest_mode) && FLOAT128_IBM_P (src_mode))
22272 do_move = true;
22274 else
22275 gcc_unreachable ();
22277 /* Handle conversion between TFmode/KFmode. */
22278 if (do_move)
22279 emit_move_insn (dest, gen_lowpart (dest_mode, src));
22281 /* Handle conversion if we have hardware support. */
22282 else if (TARGET_FLOAT128_HW && hw_convert)
22283 emit_insn ((hw_convert) (dest, src));
22285 /* Call an external function to do the conversion. */
22286 else if (cvt != unknown_optab)
22288 libfunc = convert_optab_libfunc (cvt, dest_mode, src_mode);
22289 gcc_assert (libfunc != NULL_RTX);
22291 dest2 = emit_library_call_value (libfunc, dest, LCT_CONST, dest_mode, 1, src,
22292 src_mode);
22294 gcc_assert (dest2 != NULL_RTX);
22295 if (!rtx_equal_p (dest, dest2))
22296 emit_move_insn (dest, dest2);
22299 else
22300 gcc_unreachable ();
22302 return;
22306 /* Emit the RTL for an sISEL pattern. */
22308 void
22309 rs6000_emit_sISEL (machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
22311 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
22314 /* Emit RTL that sets a register to zero if OP1 and OP2 are equal. SCRATCH
22315 can be used as that dest register. Return the dest register. */
22318 rs6000_emit_eqne (machine_mode mode, rtx op1, rtx op2, rtx scratch)
22320 if (op2 == const0_rtx)
22321 return op1;
22323 if (GET_CODE (scratch) == SCRATCH)
22324 scratch = gen_reg_rtx (mode);
22326 if (logical_operand (op2, mode))
22327 emit_insn (gen_rtx_SET (scratch, gen_rtx_XOR (mode, op1, op2)));
22328 else
22329 emit_insn (gen_rtx_SET (scratch,
22330 gen_rtx_PLUS (mode, op1, negate_rtx (mode, op2))));
22332 return scratch;
22335 void
22336 rs6000_emit_sCOND (machine_mode mode, rtx operands[])
22338 rtx condition_rtx;
22339 machine_mode op_mode;
22340 enum rtx_code cond_code;
22341 rtx result = operands[0];
22343 condition_rtx = rs6000_generate_compare (operands[1], mode);
22344 cond_code = GET_CODE (condition_rtx);
22346 if (cond_code == NE
22347 || cond_code == GE || cond_code == LE
22348 || cond_code == GEU || cond_code == LEU
22349 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
22351 rtx not_result = gen_reg_rtx (CCEQmode);
22352 rtx not_op, rev_cond_rtx;
22353 machine_mode cc_mode;
22355 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
22357 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
22358 SImode, XEXP (condition_rtx, 0), const0_rtx);
22359 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
22360 emit_insn (gen_rtx_SET (not_result, not_op));
22361 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
22364 op_mode = GET_MODE (XEXP (operands[1], 0));
22365 if (op_mode == VOIDmode)
22366 op_mode = GET_MODE (XEXP (operands[1], 1));
22368 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
22370 PUT_MODE (condition_rtx, DImode);
22371 convert_move (result, condition_rtx, 0);
22373 else
22375 PUT_MODE (condition_rtx, SImode);
22376 emit_insn (gen_rtx_SET (result, condition_rtx));
22380 /* Emit a branch of kind CODE to location LOC. */
22382 void
22383 rs6000_emit_cbranch (machine_mode mode, rtx operands[])
22385 rtx condition_rtx, loc_ref;
22387 condition_rtx = rs6000_generate_compare (operands[0], mode);
22388 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
22389 emit_jump_insn (gen_rtx_SET (pc_rtx,
22390 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
22391 loc_ref, pc_rtx)));
22394 /* Return the string to output a conditional branch to LABEL, which is
22395 the operand template of the label, or NULL if the branch is really a
22396 conditional return.
22398 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
22399 condition code register and its mode specifies what kind of
22400 comparison we made.
22402 REVERSED is nonzero if we should reverse the sense of the comparison.
22404 INSN is the insn. */
22406 char *
22407 output_cbranch (rtx op, const char *label, int reversed, rtx_insn *insn)
22409 static char string[64];
22410 enum rtx_code code = GET_CODE (op);
22411 rtx cc_reg = XEXP (op, 0);
22412 machine_mode mode = GET_MODE (cc_reg);
22413 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
22414 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
22415 int really_reversed = reversed ^ need_longbranch;
22416 char *s = string;
22417 const char *ccode;
22418 const char *pred;
22419 rtx note;
22421 validate_condition_mode (code, mode);
22423 /* Work out which way this really branches. We could use
22424 reverse_condition_maybe_unordered here always but this
22425 makes the resulting assembler clearer. */
22426 if (really_reversed)
22428 /* Reversal of FP compares takes care -- an ordered compare
22429 becomes an unordered compare and vice versa. */
22430 if (mode == CCFPmode)
22431 code = reverse_condition_maybe_unordered (code);
22432 else
22433 code = reverse_condition (code);
22436 switch (code)
22438 /* Not all of these are actually distinct opcodes, but
22439 we distinguish them for clarity of the resulting assembler. */
22440 case NE: case LTGT:
22441 ccode = "ne"; break;
22442 case EQ: case UNEQ:
22443 ccode = "eq"; break;
22444 case GE: case GEU:
22445 ccode = "ge"; break;
22446 case GT: case GTU: case UNGT:
22447 ccode = "gt"; break;
22448 case LE: case LEU:
22449 ccode = "le"; break;
22450 case LT: case LTU: case UNLT:
22451 ccode = "lt"; break;
22452 case UNORDERED: ccode = "un"; break;
22453 case ORDERED: ccode = "nu"; break;
22454 case UNGE: ccode = "nl"; break;
22455 case UNLE: ccode = "ng"; break;
22456 default:
22457 gcc_unreachable ();
22460 /* Maybe we have a guess as to how likely the branch is. */
22461 pred = "";
22462 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
22463 if (note != NULL_RTX)
22465 /* PROB is the difference from 50%. */
22466 int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
22467 .to_reg_br_prob_base () - REG_BR_PROB_BASE / 2;
22469 /* Only hint for highly probable/improbable branches on newer cpus when
22470 we have real profile data, as static prediction overrides processor
22471 dynamic prediction. For older cpus we may as well always hint, but
22472 assume not taken for branches that are very close to 50% as a
22473 mispredicted taken branch is more expensive than a
22474 mispredicted not-taken branch. */
22475 if (rs6000_always_hint
22476 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
22477 && (profile_status_for_fn (cfun) != PROFILE_GUESSED)
22478 && br_prob_note_reliable_p (note)))
22480 if (abs (prob) > REG_BR_PROB_BASE / 20
22481 && ((prob > 0) ^ need_longbranch))
22482 pred = "+";
22483 else
22484 pred = "-";
22488 if (label == NULL)
22489 s += sprintf (s, "b%slr%s ", ccode, pred);
22490 else
22491 s += sprintf (s, "b%s%s ", ccode, pred);
22493 /* We need to escape any '%' characters in the reg_names string.
22494 Assume they'd only be the first character.... */
22495 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
22496 *s++ = '%';
22497 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
22499 if (label != NULL)
22501 /* If the branch distance was too far, we may have to use an
22502 unconditional branch to go the distance. */
22503 if (need_longbranch)
22504 s += sprintf (s, ",$+8\n\tb %s", label);
22505 else
22506 s += sprintf (s, ",%s", label);
22509 return string;
22512 /* Return insn for VSX or Altivec comparisons. */
22514 static rtx
22515 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
22517 rtx mask;
22518 machine_mode mode = GET_MODE (op0);
22520 switch (code)
22522 default:
22523 break;
22525 case GE:
22526 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
22527 return NULL_RTX;
22528 /* FALLTHRU */
22530 case EQ:
22531 case GT:
22532 case GTU:
22533 case ORDERED:
22534 case UNORDERED:
22535 case UNEQ:
22536 case LTGT:
22537 mask = gen_reg_rtx (mode);
22538 emit_insn (gen_rtx_SET (mask, gen_rtx_fmt_ee (code, mode, op0, op1)));
22539 return mask;
22542 return NULL_RTX;
22545 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
22546 DMODE is expected destination mode. This is a recursive function. */
22548 static rtx
22549 rs6000_emit_vector_compare (enum rtx_code rcode,
22550 rtx op0, rtx op1,
22551 machine_mode dmode)
22553 rtx mask;
22554 bool swap_operands = false;
22555 bool try_again = false;
22557 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
22558 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
22560 /* See if the comparison works as is. */
22561 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
22562 if (mask)
22563 return mask;
22565 switch (rcode)
22567 case LT:
22568 rcode = GT;
22569 swap_operands = true;
22570 try_again = true;
22571 break;
22572 case LTU:
22573 rcode = GTU;
22574 swap_operands = true;
22575 try_again = true;
22576 break;
22577 case NE:
22578 case UNLE:
22579 case UNLT:
22580 case UNGE:
22581 case UNGT:
22582 /* Invert condition and try again.
22583 e.g., A != B becomes ~(A==B). */
22585 enum rtx_code rev_code;
22586 enum insn_code nor_code;
22587 rtx mask2;
22589 rev_code = reverse_condition_maybe_unordered (rcode);
22590 if (rev_code == UNKNOWN)
22591 return NULL_RTX;
22593 nor_code = optab_handler (one_cmpl_optab, dmode);
22594 if (nor_code == CODE_FOR_nothing)
22595 return NULL_RTX;
22597 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
22598 if (!mask2)
22599 return NULL_RTX;
22601 mask = gen_reg_rtx (dmode);
22602 emit_insn (GEN_FCN (nor_code) (mask, mask2));
22603 return mask;
22605 break;
22606 case GE:
22607 case GEU:
22608 case LE:
22609 case LEU:
22610 /* Try GT/GTU/LT/LTU OR EQ */
22612 rtx c_rtx, eq_rtx;
22613 enum insn_code ior_code;
22614 enum rtx_code new_code;
22616 switch (rcode)
22618 case GE:
22619 new_code = GT;
22620 break;
22622 case GEU:
22623 new_code = GTU;
22624 break;
22626 case LE:
22627 new_code = LT;
22628 break;
22630 case LEU:
22631 new_code = LTU;
22632 break;
22634 default:
22635 gcc_unreachable ();
22638 ior_code = optab_handler (ior_optab, dmode);
22639 if (ior_code == CODE_FOR_nothing)
22640 return NULL_RTX;
22642 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
22643 if (!c_rtx)
22644 return NULL_RTX;
22646 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
22647 if (!eq_rtx)
22648 return NULL_RTX;
22650 mask = gen_reg_rtx (dmode);
22651 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
22652 return mask;
22654 break;
22655 default:
22656 return NULL_RTX;
22659 if (try_again)
22661 if (swap_operands)
22662 std::swap (op0, op1);
22664 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
22665 if (mask)
22666 return mask;
22669 /* You only get two chances. */
22670 return NULL_RTX;
22673 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
22674 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
22675 operands for the relation operation COND. */
22678 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
22679 rtx cond, rtx cc_op0, rtx cc_op1)
22681 machine_mode dest_mode = GET_MODE (dest);
22682 machine_mode mask_mode = GET_MODE (cc_op0);
22683 enum rtx_code rcode = GET_CODE (cond);
22684 machine_mode cc_mode = CCmode;
22685 rtx mask;
22686 rtx cond2;
22687 bool invert_move = false;
22689 if (VECTOR_UNIT_NONE_P (dest_mode))
22690 return 0;
22692 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
22693 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
22695 switch (rcode)
22697 /* Swap operands if we can, and fall back to doing the operation as
22698 specified, and doing a NOR to invert the test. */
22699 case NE:
22700 case UNLE:
22701 case UNLT:
22702 case UNGE:
22703 case UNGT:
22704 /* Invert condition and try again.
22705 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
22706 invert_move = true;
22707 rcode = reverse_condition_maybe_unordered (rcode);
22708 if (rcode == UNKNOWN)
22709 return 0;
22710 break;
22712 case GE:
22713 case LE:
22714 if (GET_MODE_CLASS (mask_mode) == MODE_VECTOR_INT)
22716 /* Invert condition to avoid compound test. */
22717 invert_move = true;
22718 rcode = reverse_condition (rcode);
22720 break;
22722 case GTU:
22723 case GEU:
22724 case LTU:
22725 case LEU:
22726 /* Mark unsigned tests with CCUNSmode. */
22727 cc_mode = CCUNSmode;
22729 /* Invert condition to avoid compound test if necessary. */
22730 if (rcode == GEU || rcode == LEU)
22732 invert_move = true;
22733 rcode = reverse_condition (rcode);
22735 break;
22737 default:
22738 break;
22741 /* Get the vector mask for the given relational operations. */
22742 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
22744 if (!mask)
22745 return 0;
22747 if (invert_move)
22748 std::swap (op_true, op_false);
22750 /* Optimize vec1 == vec2, to know the mask generates -1/0. */
22751 if (GET_MODE_CLASS (dest_mode) == MODE_VECTOR_INT
22752 && (GET_CODE (op_true) == CONST_VECTOR
22753 || GET_CODE (op_false) == CONST_VECTOR))
22755 rtx constant_0 = CONST0_RTX (dest_mode);
22756 rtx constant_m1 = CONSTM1_RTX (dest_mode);
22758 if (op_true == constant_m1 && op_false == constant_0)
22760 emit_move_insn (dest, mask);
22761 return 1;
22764 else if (op_true == constant_0 && op_false == constant_m1)
22766 emit_insn (gen_rtx_SET (dest, gen_rtx_NOT (dest_mode, mask)));
22767 return 1;
22770 /* If we can't use the vector comparison directly, perhaps we can use
22771 the mask for the true or false fields, instead of loading up a
22772 constant. */
22773 if (op_true == constant_m1)
22774 op_true = mask;
22776 if (op_false == constant_0)
22777 op_false = mask;
22780 if (!REG_P (op_true) && !SUBREG_P (op_true))
22781 op_true = force_reg (dest_mode, op_true);
22783 if (!REG_P (op_false) && !SUBREG_P (op_false))
22784 op_false = force_reg (dest_mode, op_false);
22786 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
22787 CONST0_RTX (dest_mode));
22788 emit_insn (gen_rtx_SET (dest,
22789 gen_rtx_IF_THEN_ELSE (dest_mode,
22790 cond2,
22791 op_true,
22792 op_false)));
22793 return 1;
22796 /* ISA 3.0 (power9) minmax subcase to emit a XSMAXCDP or XSMINCDP instruction
22797 for SF/DF scalars. Move TRUE_COND to DEST if OP of the operands of the last
22798 comparison is nonzero/true, FALSE_COND if it is zero/false. Return 0 if the
22799 hardware has no such operation. */
22801 static int
22802 rs6000_emit_p9_fp_minmax (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22804 enum rtx_code code = GET_CODE (op);
22805 rtx op0 = XEXP (op, 0);
22806 rtx op1 = XEXP (op, 1);
22807 machine_mode compare_mode = GET_MODE (op0);
22808 machine_mode result_mode = GET_MODE (dest);
22809 bool max_p = false;
22811 if (result_mode != compare_mode)
22812 return 0;
22814 if (code == GE || code == GT)
22815 max_p = true;
22816 else if (code == LE || code == LT)
22817 max_p = false;
22818 else
22819 return 0;
22821 if (rtx_equal_p (op0, true_cond) && rtx_equal_p (op1, false_cond))
22824 else if (rtx_equal_p (op1, true_cond) && rtx_equal_p (op0, false_cond))
22825 max_p = !max_p;
22827 else
22828 return 0;
22830 rs6000_emit_minmax (dest, max_p ? SMAX : SMIN, op0, op1);
22831 return 1;
22834 /* ISA 3.0 (power9) conditional move subcase to emit XSCMP{EQ,GE,GT,NE}DP and
22835 XXSEL instructions for SF/DF scalars. Move TRUE_COND to DEST if OP of the
22836 operands of the last comparison is nonzero/true, FALSE_COND if it is
22837 zero/false. Return 0 if the hardware has no such operation. */
22839 static int
22840 rs6000_emit_p9_fp_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22842 enum rtx_code code = GET_CODE (op);
22843 rtx op0 = XEXP (op, 0);
22844 rtx op1 = XEXP (op, 1);
22845 machine_mode result_mode = GET_MODE (dest);
22846 rtx compare_rtx;
22847 rtx cmove_rtx;
22848 rtx clobber_rtx;
22850 if (!can_create_pseudo_p ())
22851 return 0;
22853 switch (code)
22855 case EQ:
22856 case GE:
22857 case GT:
22858 break;
22860 case NE:
22861 case LT:
22862 case LE:
22863 code = swap_condition (code);
22864 std::swap (op0, op1);
22865 break;
22867 default:
22868 return 0;
22871 /* Generate: [(parallel [(set (dest)
22872 (if_then_else (op (cmp1) (cmp2))
22873 (true)
22874 (false)))
22875 (clobber (scratch))])]. */
22877 compare_rtx = gen_rtx_fmt_ee (code, CCFPmode, op0, op1);
22878 cmove_rtx = gen_rtx_SET (dest,
22879 gen_rtx_IF_THEN_ELSE (result_mode,
22880 compare_rtx,
22881 true_cond,
22882 false_cond));
22884 clobber_rtx = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (V2DImode));
22885 emit_insn (gen_rtx_PARALLEL (VOIDmode,
22886 gen_rtvec (2, cmove_rtx, clobber_rtx)));
22888 return 1;
22891 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
22892 operands of the last comparison is nonzero/true, FALSE_COND if it
22893 is zero/false. Return 0 if the hardware has no such operation. */
22896 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22898 enum rtx_code code = GET_CODE (op);
22899 rtx op0 = XEXP (op, 0);
22900 rtx op1 = XEXP (op, 1);
22901 machine_mode compare_mode = GET_MODE (op0);
22902 machine_mode result_mode = GET_MODE (dest);
22903 rtx temp;
22904 bool is_against_zero;
22906 /* These modes should always match. */
22907 if (GET_MODE (op1) != compare_mode
22908 /* In the isel case however, we can use a compare immediate, so
22909 op1 may be a small constant. */
22910 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
22911 return 0;
22912 if (GET_MODE (true_cond) != result_mode)
22913 return 0;
22914 if (GET_MODE (false_cond) != result_mode)
22915 return 0;
22917 /* See if we can use the ISA 3.0 (power9) min/max/compare functions. */
22918 if (TARGET_P9_MINMAX
22919 && (compare_mode == SFmode || compare_mode == DFmode)
22920 && (result_mode == SFmode || result_mode == DFmode))
22922 if (rs6000_emit_p9_fp_minmax (dest, op, true_cond, false_cond))
22923 return 1;
22925 if (rs6000_emit_p9_fp_cmove (dest, op, true_cond, false_cond))
22926 return 1;
22929 /* Don't allow using floating point comparisons for integer results for
22930 now. */
22931 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
22932 return 0;
22934 /* First, work out if the hardware can do this at all, or
22935 if it's too slow.... */
22936 if (!FLOAT_MODE_P (compare_mode))
22938 if (TARGET_ISEL)
22939 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
22940 return 0;
22943 is_against_zero = op1 == CONST0_RTX (compare_mode);
22945 /* A floating-point subtract might overflow, underflow, or produce
22946 an inexact result, thus changing the floating-point flags, so it
22947 can't be generated if we care about that. It's safe if one side
22948 of the construct is zero, since then no subtract will be
22949 generated. */
22950 if (SCALAR_FLOAT_MODE_P (compare_mode)
22951 && flag_trapping_math && ! is_against_zero)
22952 return 0;
22954 /* Eliminate half of the comparisons by switching operands, this
22955 makes the remaining code simpler. */
22956 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
22957 || code == LTGT || code == LT || code == UNLE)
22959 code = reverse_condition_maybe_unordered (code);
22960 temp = true_cond;
22961 true_cond = false_cond;
22962 false_cond = temp;
22965 /* UNEQ and LTGT take four instructions for a comparison with zero,
22966 it'll probably be faster to use a branch here too. */
22967 if (code == UNEQ && HONOR_NANS (compare_mode))
22968 return 0;
22970 /* We're going to try to implement comparisons by performing
22971 a subtract, then comparing against zero. Unfortunately,
22972 Inf - Inf is NaN which is not zero, and so if we don't
22973 know that the operand is finite and the comparison
22974 would treat EQ different to UNORDERED, we can't do it. */
22975 if (HONOR_INFINITIES (compare_mode)
22976 && code != GT && code != UNGE
22977 && (GET_CODE (op1) != CONST_DOUBLE
22978 || real_isinf (CONST_DOUBLE_REAL_VALUE (op1)))
22979 /* Constructs of the form (a OP b ? a : b) are safe. */
22980 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
22981 || (! rtx_equal_p (op0, true_cond)
22982 && ! rtx_equal_p (op1, true_cond))))
22983 return 0;
22985 /* At this point we know we can use fsel. */
22987 /* Reduce the comparison to a comparison against zero. */
22988 if (! is_against_zero)
22990 temp = gen_reg_rtx (compare_mode);
22991 emit_insn (gen_rtx_SET (temp, gen_rtx_MINUS (compare_mode, op0, op1)));
22992 op0 = temp;
22993 op1 = CONST0_RTX (compare_mode);
22996 /* If we don't care about NaNs we can reduce some of the comparisons
22997 down to faster ones. */
22998 if (! HONOR_NANS (compare_mode))
22999 switch (code)
23001 case GT:
23002 code = LE;
23003 temp = true_cond;
23004 true_cond = false_cond;
23005 false_cond = temp;
23006 break;
23007 case UNGE:
23008 code = GE;
23009 break;
23010 case UNEQ:
23011 code = EQ;
23012 break;
23013 default:
23014 break;
23017 /* Now, reduce everything down to a GE. */
23018 switch (code)
23020 case GE:
23021 break;
23023 case LE:
23024 temp = gen_reg_rtx (compare_mode);
23025 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
23026 op0 = temp;
23027 break;
23029 case ORDERED:
23030 temp = gen_reg_rtx (compare_mode);
23031 emit_insn (gen_rtx_SET (temp, gen_rtx_ABS (compare_mode, op0)));
23032 op0 = temp;
23033 break;
23035 case EQ:
23036 temp = gen_reg_rtx (compare_mode);
23037 emit_insn (gen_rtx_SET (temp,
23038 gen_rtx_NEG (compare_mode,
23039 gen_rtx_ABS (compare_mode, op0))));
23040 op0 = temp;
23041 break;
23043 case UNGE:
23044 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
23045 temp = gen_reg_rtx (result_mode);
23046 emit_insn (gen_rtx_SET (temp,
23047 gen_rtx_IF_THEN_ELSE (result_mode,
23048 gen_rtx_GE (VOIDmode,
23049 op0, op1),
23050 true_cond, false_cond)));
23051 false_cond = true_cond;
23052 true_cond = temp;
23054 temp = gen_reg_rtx (compare_mode);
23055 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
23056 op0 = temp;
23057 break;
23059 case GT:
23060 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
23061 temp = gen_reg_rtx (result_mode);
23062 emit_insn (gen_rtx_SET (temp,
23063 gen_rtx_IF_THEN_ELSE (result_mode,
23064 gen_rtx_GE (VOIDmode,
23065 op0, op1),
23066 true_cond, false_cond)));
23067 true_cond = false_cond;
23068 false_cond = temp;
23070 temp = gen_reg_rtx (compare_mode);
23071 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
23072 op0 = temp;
23073 break;
23075 default:
23076 gcc_unreachable ();
23079 emit_insn (gen_rtx_SET (dest,
23080 gen_rtx_IF_THEN_ELSE (result_mode,
23081 gen_rtx_GE (VOIDmode,
23082 op0, op1),
23083 true_cond, false_cond)));
23084 return 1;
23087 /* Same as above, but for ints (isel). */
23089 static int
23090 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
23092 rtx condition_rtx, cr;
23093 machine_mode mode = GET_MODE (dest);
23094 enum rtx_code cond_code;
23095 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
23096 bool signedp;
23098 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
23099 return 0;
23101 /* We still have to do the compare, because isel doesn't do a
23102 compare, it just looks at the CRx bits set by a previous compare
23103 instruction. */
23104 condition_rtx = rs6000_generate_compare (op, mode);
23105 cond_code = GET_CODE (condition_rtx);
23106 cr = XEXP (condition_rtx, 0);
23107 signedp = GET_MODE (cr) == CCmode;
23109 isel_func = (mode == SImode
23110 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
23111 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
23113 switch (cond_code)
23115 case LT: case GT: case LTU: case GTU: case EQ:
23116 /* isel handles these directly. */
23117 break;
23119 default:
23120 /* We need to swap the sense of the comparison. */
23122 std::swap (false_cond, true_cond);
23123 PUT_CODE (condition_rtx, reverse_condition (cond_code));
23125 break;
23128 false_cond = force_reg (mode, false_cond);
23129 if (true_cond != const0_rtx)
23130 true_cond = force_reg (mode, true_cond);
23132 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
23134 return 1;
23137 const char *
23138 output_isel (rtx *operands)
23140 enum rtx_code code;
23142 code = GET_CODE (operands[1]);
23144 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
23146 gcc_assert (GET_CODE (operands[2]) == REG
23147 && GET_CODE (operands[3]) == REG);
23148 PUT_CODE (operands[1], reverse_condition (code));
23149 return "isel %0,%3,%2,%j1";
23152 return "isel %0,%2,%3,%j1";
23155 void
23156 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
23158 machine_mode mode = GET_MODE (op0);
23159 enum rtx_code c;
23160 rtx target;
23162 /* VSX/altivec have direct min/max insns. */
23163 if ((code == SMAX || code == SMIN)
23164 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
23165 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
23167 emit_insn (gen_rtx_SET (dest, gen_rtx_fmt_ee (code, mode, op0, op1)));
23168 return;
23171 if (code == SMAX || code == SMIN)
23172 c = GE;
23173 else
23174 c = GEU;
23176 if (code == SMAX || code == UMAX)
23177 target = emit_conditional_move (dest, c, op0, op1, mode,
23178 op0, op1, mode, 0);
23179 else
23180 target = emit_conditional_move (dest, c, op0, op1, mode,
23181 op1, op0, mode, 0);
23182 gcc_assert (target);
23183 if (target != dest)
23184 emit_move_insn (dest, target);
23187 /* Split a signbit operation on 64-bit machines with direct move. Also allow
23188 for the value to come from memory or if it is already loaded into a GPR. */
23190 void
23191 rs6000_split_signbit (rtx dest, rtx src)
23193 machine_mode d_mode = GET_MODE (dest);
23194 machine_mode s_mode = GET_MODE (src);
23195 rtx dest_di = (d_mode == DImode) ? dest : gen_lowpart (DImode, dest);
23196 rtx shift_reg = dest_di;
23198 gcc_assert (FLOAT128_IEEE_P (s_mode) && TARGET_POWERPC64);
23200 if (MEM_P (src))
23202 rtx mem = (WORDS_BIG_ENDIAN
23203 ? adjust_address (src, DImode, 0)
23204 : adjust_address (src, DImode, 8));
23205 emit_insn (gen_rtx_SET (dest_di, mem));
23208 else
23210 unsigned int r = reg_or_subregno (src);
23212 if (INT_REGNO_P (r))
23213 shift_reg = gen_rtx_REG (DImode, r + (BYTES_BIG_ENDIAN == 0));
23215 else
23217 /* Generate the special mfvsrd instruction to get it in a GPR. */
23218 gcc_assert (VSX_REGNO_P (r));
23219 if (s_mode == KFmode)
23220 emit_insn (gen_signbitkf2_dm2 (dest_di, src));
23221 else
23222 emit_insn (gen_signbittf2_dm2 (dest_di, src));
23226 emit_insn (gen_lshrdi3 (dest_di, shift_reg, GEN_INT (63)));
23227 return;
23230 /* A subroutine of the atomic operation splitters. Jump to LABEL if
23231 COND is true. Mark the jump as unlikely to be taken. */
23233 static void
23234 emit_unlikely_jump (rtx cond, rtx label)
23236 rtx x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
23237 rtx_insn *insn = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
23238 add_reg_br_prob_note (insn, profile_probability::very_unlikely ());
23241 /* A subroutine of the atomic operation splitters. Emit a load-locked
23242 instruction in MODE. For QI/HImode, possibly use a pattern than includes
23243 the zero_extend operation. */
23245 static void
23246 emit_load_locked (machine_mode mode, rtx reg, rtx mem)
23248 rtx (*fn) (rtx, rtx) = NULL;
23250 switch (mode)
23252 case E_QImode:
23253 fn = gen_load_lockedqi;
23254 break;
23255 case E_HImode:
23256 fn = gen_load_lockedhi;
23257 break;
23258 case E_SImode:
23259 if (GET_MODE (mem) == QImode)
23260 fn = gen_load_lockedqi_si;
23261 else if (GET_MODE (mem) == HImode)
23262 fn = gen_load_lockedhi_si;
23263 else
23264 fn = gen_load_lockedsi;
23265 break;
23266 case E_DImode:
23267 fn = gen_load_lockeddi;
23268 break;
23269 case E_TImode:
23270 fn = gen_load_lockedti;
23271 break;
23272 default:
23273 gcc_unreachable ();
23275 emit_insn (fn (reg, mem));
23278 /* A subroutine of the atomic operation splitters. Emit a store-conditional
23279 instruction in MODE. */
23281 static void
23282 emit_store_conditional (machine_mode mode, rtx res, rtx mem, rtx val)
23284 rtx (*fn) (rtx, rtx, rtx) = NULL;
23286 switch (mode)
23288 case E_QImode:
23289 fn = gen_store_conditionalqi;
23290 break;
23291 case E_HImode:
23292 fn = gen_store_conditionalhi;
23293 break;
23294 case E_SImode:
23295 fn = gen_store_conditionalsi;
23296 break;
23297 case E_DImode:
23298 fn = gen_store_conditionaldi;
23299 break;
23300 case E_TImode:
23301 fn = gen_store_conditionalti;
23302 break;
23303 default:
23304 gcc_unreachable ();
23307 /* Emit sync before stwcx. to address PPC405 Erratum. */
23308 if (PPC405_ERRATUM77)
23309 emit_insn (gen_hwsync ());
23311 emit_insn (fn (res, mem, val));
23314 /* Expand barriers before and after a load_locked/store_cond sequence. */
23316 static rtx
23317 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
23319 rtx addr = XEXP (mem, 0);
23321 if (!legitimate_indirect_address_p (addr, reload_completed)
23322 && !legitimate_indexed_address_p (addr, reload_completed))
23324 addr = force_reg (Pmode, addr);
23325 mem = replace_equiv_address_nv (mem, addr);
23328 switch (model)
23330 case MEMMODEL_RELAXED:
23331 case MEMMODEL_CONSUME:
23332 case MEMMODEL_ACQUIRE:
23333 break;
23334 case MEMMODEL_RELEASE:
23335 case MEMMODEL_ACQ_REL:
23336 emit_insn (gen_lwsync ());
23337 break;
23338 case MEMMODEL_SEQ_CST:
23339 emit_insn (gen_hwsync ());
23340 break;
23341 default:
23342 gcc_unreachable ();
23344 return mem;
23347 static void
23348 rs6000_post_atomic_barrier (enum memmodel model)
23350 switch (model)
23352 case MEMMODEL_RELAXED:
23353 case MEMMODEL_CONSUME:
23354 case MEMMODEL_RELEASE:
23355 break;
23356 case MEMMODEL_ACQUIRE:
23357 case MEMMODEL_ACQ_REL:
23358 case MEMMODEL_SEQ_CST:
23359 emit_insn (gen_isync ());
23360 break;
23361 default:
23362 gcc_unreachable ();
23366 /* A subroutine of the various atomic expanders. For sub-word operations,
23367 we must adjust things to operate on SImode. Given the original MEM,
23368 return a new aligned memory. Also build and return the quantities by
23369 which to shift and mask. */
23371 static rtx
23372 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
23374 rtx addr, align, shift, mask, mem;
23375 HOST_WIDE_INT shift_mask;
23376 machine_mode mode = GET_MODE (orig_mem);
23378 /* For smaller modes, we have to implement this via SImode. */
23379 shift_mask = (mode == QImode ? 0x18 : 0x10);
23381 addr = XEXP (orig_mem, 0);
23382 addr = force_reg (GET_MODE (addr), addr);
23384 /* Aligned memory containing subword. Generate a new memory. We
23385 do not want any of the existing MEM_ATTR data, as we're now
23386 accessing memory outside the original object. */
23387 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
23388 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23389 mem = gen_rtx_MEM (SImode, align);
23390 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
23391 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
23392 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
23394 /* Shift amount for subword relative to aligned word. */
23395 shift = gen_reg_rtx (SImode);
23396 addr = gen_lowpart (SImode, addr);
23397 rtx tmp = gen_reg_rtx (SImode);
23398 emit_insn (gen_ashlsi3 (tmp, addr, GEN_INT (3)));
23399 emit_insn (gen_andsi3 (shift, tmp, GEN_INT (shift_mask)));
23400 if (BYTES_BIG_ENDIAN)
23401 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
23402 shift, 1, OPTAB_LIB_WIDEN);
23403 *pshift = shift;
23405 /* Mask for insertion. */
23406 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
23407 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
23408 *pmask = mask;
23410 return mem;
23413 /* A subroutine of the various atomic expanders. For sub-word operands,
23414 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
23416 static rtx
23417 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
23419 rtx x;
23421 x = gen_reg_rtx (SImode);
23422 emit_insn (gen_rtx_SET (x, gen_rtx_AND (SImode,
23423 gen_rtx_NOT (SImode, mask),
23424 oldval)));
23426 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
23428 return x;
23431 /* A subroutine of the various atomic expanders. For sub-word operands,
23432 extract WIDE to NARROW via SHIFT. */
23434 static void
23435 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
23437 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
23438 wide, 1, OPTAB_LIB_WIDEN);
23439 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
23442 /* Expand an atomic compare and swap operation. */
23444 void
23445 rs6000_expand_atomic_compare_and_swap (rtx operands[])
23447 rtx boolval, retval, mem, oldval, newval, cond;
23448 rtx label1, label2, x, mask, shift;
23449 machine_mode mode, orig_mode;
23450 enum memmodel mod_s, mod_f;
23451 bool is_weak;
23453 boolval = operands[0];
23454 retval = operands[1];
23455 mem = operands[2];
23456 oldval = operands[3];
23457 newval = operands[4];
23458 is_weak = (INTVAL (operands[5]) != 0);
23459 mod_s = memmodel_base (INTVAL (operands[6]));
23460 mod_f = memmodel_base (INTVAL (operands[7]));
23461 orig_mode = mode = GET_MODE (mem);
23463 mask = shift = NULL_RTX;
23464 if (mode == QImode || mode == HImode)
23466 /* Before power8, we didn't have access to lbarx/lharx, so generate a
23467 lwarx and shift/mask operations. With power8, we need to do the
23468 comparison in SImode, but the store is still done in QI/HImode. */
23469 oldval = convert_modes (SImode, mode, oldval, 1);
23471 if (!TARGET_SYNC_HI_QI)
23473 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
23475 /* Shift and mask OLDVAL into position with the word. */
23476 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
23477 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23479 /* Shift and mask NEWVAL into position within the word. */
23480 newval = convert_modes (SImode, mode, newval, 1);
23481 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
23482 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23485 /* Prepare to adjust the return value. */
23486 retval = gen_reg_rtx (SImode);
23487 mode = SImode;
23489 else if (reg_overlap_mentioned_p (retval, oldval))
23490 oldval = copy_to_reg (oldval);
23492 if (mode != TImode && !reg_or_short_operand (oldval, mode))
23493 oldval = copy_to_mode_reg (mode, oldval);
23495 if (reg_overlap_mentioned_p (retval, newval))
23496 newval = copy_to_reg (newval);
23498 mem = rs6000_pre_atomic_barrier (mem, mod_s);
23500 label1 = NULL_RTX;
23501 if (!is_weak)
23503 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
23504 emit_label (XEXP (label1, 0));
23506 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
23508 emit_load_locked (mode, retval, mem);
23510 x = retval;
23511 if (mask)
23512 x = expand_simple_binop (SImode, AND, retval, mask,
23513 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23515 cond = gen_reg_rtx (CCmode);
23516 /* If we have TImode, synthesize a comparison. */
23517 if (mode != TImode)
23518 x = gen_rtx_COMPARE (CCmode, x, oldval);
23519 else
23521 rtx xor1_result = gen_reg_rtx (DImode);
23522 rtx xor2_result = gen_reg_rtx (DImode);
23523 rtx or_result = gen_reg_rtx (DImode);
23524 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
23525 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
23526 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
23527 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
23529 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
23530 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
23531 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
23532 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
23535 emit_insn (gen_rtx_SET (cond, x));
23537 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23538 emit_unlikely_jump (x, label2);
23540 x = newval;
23541 if (mask)
23542 x = rs6000_mask_atomic_subword (retval, newval, mask);
23544 emit_store_conditional (orig_mode, cond, mem, x);
23546 if (!is_weak)
23548 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23549 emit_unlikely_jump (x, label1);
23552 if (!is_mm_relaxed (mod_f))
23553 emit_label (XEXP (label2, 0));
23555 rs6000_post_atomic_barrier (mod_s);
23557 if (is_mm_relaxed (mod_f))
23558 emit_label (XEXP (label2, 0));
23560 if (shift)
23561 rs6000_finish_atomic_subword (operands[1], retval, shift);
23562 else if (mode != GET_MODE (operands[1]))
23563 convert_move (operands[1], retval, 1);
23565 /* In all cases, CR0 contains EQ on success, and NE on failure. */
23566 x = gen_rtx_EQ (SImode, cond, const0_rtx);
23567 emit_insn (gen_rtx_SET (boolval, x));
23570 /* Expand an atomic exchange operation. */
23572 void
23573 rs6000_expand_atomic_exchange (rtx operands[])
23575 rtx retval, mem, val, cond;
23576 machine_mode mode;
23577 enum memmodel model;
23578 rtx label, x, mask, shift;
23580 retval = operands[0];
23581 mem = operands[1];
23582 val = operands[2];
23583 model = memmodel_base (INTVAL (operands[3]));
23584 mode = GET_MODE (mem);
23586 mask = shift = NULL_RTX;
23587 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
23589 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
23591 /* Shift and mask VAL into position with the word. */
23592 val = convert_modes (SImode, mode, val, 1);
23593 val = expand_simple_binop (SImode, ASHIFT, val, shift,
23594 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23596 /* Prepare to adjust the return value. */
23597 retval = gen_reg_rtx (SImode);
23598 mode = SImode;
23601 mem = rs6000_pre_atomic_barrier (mem, model);
23603 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
23604 emit_label (XEXP (label, 0));
23606 emit_load_locked (mode, retval, mem);
23608 x = val;
23609 if (mask)
23610 x = rs6000_mask_atomic_subword (retval, val, mask);
23612 cond = gen_reg_rtx (CCmode);
23613 emit_store_conditional (mode, cond, mem, x);
23615 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23616 emit_unlikely_jump (x, label);
23618 rs6000_post_atomic_barrier (model);
23620 if (shift)
23621 rs6000_finish_atomic_subword (operands[0], retval, shift);
23624 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
23625 to perform. MEM is the memory on which to operate. VAL is the second
23626 operand of the binary operator. BEFORE and AFTER are optional locations to
23627 return the value of MEM either before of after the operation. MODEL_RTX
23628 is a CONST_INT containing the memory model to use. */
23630 void
23631 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
23632 rtx orig_before, rtx orig_after, rtx model_rtx)
23634 enum memmodel model = memmodel_base (INTVAL (model_rtx));
23635 machine_mode mode = GET_MODE (mem);
23636 machine_mode store_mode = mode;
23637 rtx label, x, cond, mask, shift;
23638 rtx before = orig_before, after = orig_after;
23640 mask = shift = NULL_RTX;
23641 /* On power8, we want to use SImode for the operation. On previous systems,
23642 use the operation in a subword and shift/mask to get the proper byte or
23643 halfword. */
23644 if (mode == QImode || mode == HImode)
23646 if (TARGET_SYNC_HI_QI)
23648 val = convert_modes (SImode, mode, val, 1);
23650 /* Prepare to adjust the return value. */
23651 before = gen_reg_rtx (SImode);
23652 if (after)
23653 after = gen_reg_rtx (SImode);
23654 mode = SImode;
23656 else
23658 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
23660 /* Shift and mask VAL into position with the word. */
23661 val = convert_modes (SImode, mode, val, 1);
23662 val = expand_simple_binop (SImode, ASHIFT, val, shift,
23663 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23665 switch (code)
23667 case IOR:
23668 case XOR:
23669 /* We've already zero-extended VAL. That is sufficient to
23670 make certain that it does not affect other bits. */
23671 mask = NULL;
23672 break;
23674 case AND:
23675 /* If we make certain that all of the other bits in VAL are
23676 set, that will be sufficient to not affect other bits. */
23677 x = gen_rtx_NOT (SImode, mask);
23678 x = gen_rtx_IOR (SImode, x, val);
23679 emit_insn (gen_rtx_SET (val, x));
23680 mask = NULL;
23681 break;
23683 case NOT:
23684 case PLUS:
23685 case MINUS:
23686 /* These will all affect bits outside the field and need
23687 adjustment via MASK within the loop. */
23688 break;
23690 default:
23691 gcc_unreachable ();
23694 /* Prepare to adjust the return value. */
23695 before = gen_reg_rtx (SImode);
23696 if (after)
23697 after = gen_reg_rtx (SImode);
23698 store_mode = mode = SImode;
23702 mem = rs6000_pre_atomic_barrier (mem, model);
23704 label = gen_label_rtx ();
23705 emit_label (label);
23706 label = gen_rtx_LABEL_REF (VOIDmode, label);
23708 if (before == NULL_RTX)
23709 before = gen_reg_rtx (mode);
23711 emit_load_locked (mode, before, mem);
23713 if (code == NOT)
23715 x = expand_simple_binop (mode, AND, before, val,
23716 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23717 after = expand_simple_unop (mode, NOT, x, after, 1);
23719 else
23721 after = expand_simple_binop (mode, code, before, val,
23722 after, 1, OPTAB_LIB_WIDEN);
23725 x = after;
23726 if (mask)
23728 x = expand_simple_binop (SImode, AND, after, mask,
23729 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23730 x = rs6000_mask_atomic_subword (before, x, mask);
23732 else if (store_mode != mode)
23733 x = convert_modes (store_mode, mode, x, 1);
23735 cond = gen_reg_rtx (CCmode);
23736 emit_store_conditional (store_mode, cond, mem, x);
23738 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23739 emit_unlikely_jump (x, label);
23741 rs6000_post_atomic_barrier (model);
23743 if (shift)
23745 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
23746 then do the calcuations in a SImode register. */
23747 if (orig_before)
23748 rs6000_finish_atomic_subword (orig_before, before, shift);
23749 if (orig_after)
23750 rs6000_finish_atomic_subword (orig_after, after, shift);
23752 else if (store_mode != mode)
23754 /* QImode/HImode on machines with lbarx/lharx where we do the native
23755 operation and then do the calcuations in a SImode register. */
23756 if (orig_before)
23757 convert_move (orig_before, before, 1);
23758 if (orig_after)
23759 convert_move (orig_after, after, 1);
23761 else if (orig_after && after != orig_after)
23762 emit_move_insn (orig_after, after);
23765 /* Emit instructions to move SRC to DST. Called by splitters for
23766 multi-register moves. It will emit at most one instruction for
23767 each register that is accessed; that is, it won't emit li/lis pairs
23768 (or equivalent for 64-bit code). One of SRC or DST must be a hard
23769 register. */
23771 void
23772 rs6000_split_multireg_move (rtx dst, rtx src)
23774 /* The register number of the first register being moved. */
23775 int reg;
23776 /* The mode that is to be moved. */
23777 machine_mode mode;
23778 /* The mode that the move is being done in, and its size. */
23779 machine_mode reg_mode;
23780 int reg_mode_size;
23781 /* The number of registers that will be moved. */
23782 int nregs;
23784 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
23785 mode = GET_MODE (dst);
23786 nregs = hard_regno_nregs[reg][mode];
23787 if (FP_REGNO_P (reg))
23788 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
23789 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
23790 else if (ALTIVEC_REGNO_P (reg))
23791 reg_mode = V16QImode;
23792 else
23793 reg_mode = word_mode;
23794 reg_mode_size = GET_MODE_SIZE (reg_mode);
23796 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
23798 /* TDmode residing in FP registers is special, since the ISA requires that
23799 the lower-numbered word of a register pair is always the most significant
23800 word, even in little-endian mode. This does not match the usual subreg
23801 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
23802 the appropriate constituent registers "by hand" in little-endian mode.
23804 Note we do not need to check for destructive overlap here since TDmode
23805 can only reside in even/odd register pairs. */
23806 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
23808 rtx p_src, p_dst;
23809 int i;
23811 for (i = 0; i < nregs; i++)
23813 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
23814 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
23815 else
23816 p_src = simplify_gen_subreg (reg_mode, src, mode,
23817 i * reg_mode_size);
23819 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
23820 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
23821 else
23822 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
23823 i * reg_mode_size);
23825 emit_insn (gen_rtx_SET (p_dst, p_src));
23828 return;
23831 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
23833 /* Move register range backwards, if we might have destructive
23834 overlap. */
23835 int i;
23836 for (i = nregs - 1; i >= 0; i--)
23837 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
23838 i * reg_mode_size),
23839 simplify_gen_subreg (reg_mode, src, mode,
23840 i * reg_mode_size)));
23842 else
23844 int i;
23845 int j = -1;
23846 bool used_update = false;
23847 rtx restore_basereg = NULL_RTX;
23849 if (MEM_P (src) && INT_REGNO_P (reg))
23851 rtx breg;
23853 if (GET_CODE (XEXP (src, 0)) == PRE_INC
23854 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
23856 rtx delta_rtx;
23857 breg = XEXP (XEXP (src, 0), 0);
23858 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
23859 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
23860 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
23861 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
23862 src = replace_equiv_address (src, breg);
23864 else if (! rs6000_offsettable_memref_p (src, reg_mode))
23866 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
23868 rtx basereg = XEXP (XEXP (src, 0), 0);
23869 if (TARGET_UPDATE)
23871 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
23872 emit_insn (gen_rtx_SET (ndst,
23873 gen_rtx_MEM (reg_mode,
23874 XEXP (src, 0))));
23875 used_update = true;
23877 else
23878 emit_insn (gen_rtx_SET (basereg,
23879 XEXP (XEXP (src, 0), 1)));
23880 src = replace_equiv_address (src, basereg);
23882 else
23884 rtx basereg = gen_rtx_REG (Pmode, reg);
23885 emit_insn (gen_rtx_SET (basereg, XEXP (src, 0)));
23886 src = replace_equiv_address (src, basereg);
23890 breg = XEXP (src, 0);
23891 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
23892 breg = XEXP (breg, 0);
23894 /* If the base register we are using to address memory is
23895 also a destination reg, then change that register last. */
23896 if (REG_P (breg)
23897 && REGNO (breg) >= REGNO (dst)
23898 && REGNO (breg) < REGNO (dst) + nregs)
23899 j = REGNO (breg) - REGNO (dst);
23901 else if (MEM_P (dst) && INT_REGNO_P (reg))
23903 rtx breg;
23905 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
23906 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
23908 rtx delta_rtx;
23909 breg = XEXP (XEXP (dst, 0), 0);
23910 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
23911 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
23912 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
23914 /* We have to update the breg before doing the store.
23915 Use store with update, if available. */
23917 if (TARGET_UPDATE)
23919 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
23920 emit_insn (TARGET_32BIT
23921 ? (TARGET_POWERPC64
23922 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
23923 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
23924 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
23925 used_update = true;
23927 else
23928 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
23929 dst = replace_equiv_address (dst, breg);
23931 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
23932 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
23934 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
23936 rtx basereg = XEXP (XEXP (dst, 0), 0);
23937 if (TARGET_UPDATE)
23939 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
23940 emit_insn (gen_rtx_SET (gen_rtx_MEM (reg_mode,
23941 XEXP (dst, 0)),
23942 nsrc));
23943 used_update = true;
23945 else
23946 emit_insn (gen_rtx_SET (basereg,
23947 XEXP (XEXP (dst, 0), 1)));
23948 dst = replace_equiv_address (dst, basereg);
23950 else
23952 rtx basereg = XEXP (XEXP (dst, 0), 0);
23953 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
23954 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
23955 && REG_P (basereg)
23956 && REG_P (offsetreg)
23957 && REGNO (basereg) != REGNO (offsetreg));
23958 if (REGNO (basereg) == 0)
23960 rtx tmp = offsetreg;
23961 offsetreg = basereg;
23962 basereg = tmp;
23964 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
23965 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
23966 dst = replace_equiv_address (dst, basereg);
23969 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
23970 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
23973 for (i = 0; i < nregs; i++)
23975 /* Calculate index to next subword. */
23976 ++j;
23977 if (j == nregs)
23978 j = 0;
23980 /* If compiler already emitted move of first word by
23981 store with update, no need to do anything. */
23982 if (j == 0 && used_update)
23983 continue;
23985 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
23986 j * reg_mode_size),
23987 simplify_gen_subreg (reg_mode, src, mode,
23988 j * reg_mode_size)));
23990 if (restore_basereg != NULL_RTX)
23991 emit_insn (restore_basereg);
23996 /* This page contains routines that are used to determine what the
23997 function prologue and epilogue code will do and write them out. */
23999 /* Determine whether the REG is really used. */
24001 static bool
24002 save_reg_p (int reg)
24004 /* We need to mark the PIC offset register live for the same conditions
24005 as it is set up, or otherwise it won't be saved before we clobber it. */
24007 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
24009 /* When calling eh_return, we must return true for all the cases
24010 where conditional_register_usage marks the PIC offset reg
24011 call used. */
24012 if (TARGET_TOC && TARGET_MINIMAL_TOC
24013 && (crtl->calls_eh_return
24014 || df_regs_ever_live_p (reg)
24015 || !constant_pool_empty_p ()))
24016 return true;
24018 if ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
24019 && flag_pic)
24020 return true;
24023 return !call_used_regs[reg] && df_regs_ever_live_p (reg);
24026 /* Return the first fixed-point register that is required to be
24027 saved. 32 if none. */
24030 first_reg_to_save (void)
24032 int first_reg;
24034 /* Find lowest numbered live register. */
24035 for (first_reg = 13; first_reg <= 31; first_reg++)
24036 if (save_reg_p (first_reg))
24037 break;
24039 #if TARGET_MACHO
24040 if (flag_pic
24041 && crtl->uses_pic_offset_table
24042 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
24043 return RS6000_PIC_OFFSET_TABLE_REGNUM;
24044 #endif
24046 return first_reg;
24049 /* Similar, for FP regs. */
24052 first_fp_reg_to_save (void)
24054 int first_reg;
24056 /* Find lowest numbered live register. */
24057 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
24058 if (save_reg_p (first_reg))
24059 break;
24061 return first_reg;
24064 /* Similar, for AltiVec regs. */
24066 static int
24067 first_altivec_reg_to_save (void)
24069 int i;
24071 /* Stack frame remains as is unless we are in AltiVec ABI. */
24072 if (! TARGET_ALTIVEC_ABI)
24073 return LAST_ALTIVEC_REGNO + 1;
24075 /* On Darwin, the unwind routines are compiled without
24076 TARGET_ALTIVEC, and use save_world to save/restore the
24077 altivec registers when necessary. */
24078 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
24079 && ! TARGET_ALTIVEC)
24080 return FIRST_ALTIVEC_REGNO + 20;
24082 /* Find lowest numbered live register. */
24083 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
24084 if (save_reg_p (i))
24085 break;
24087 return i;
24090 /* Return a 32-bit mask of the AltiVec registers we need to set in
24091 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
24092 the 32-bit word is 0. */
24094 static unsigned int
24095 compute_vrsave_mask (void)
24097 unsigned int i, mask = 0;
24099 /* On Darwin, the unwind routines are compiled without
24100 TARGET_ALTIVEC, and use save_world to save/restore the
24101 call-saved altivec registers when necessary. */
24102 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
24103 && ! TARGET_ALTIVEC)
24104 mask |= 0xFFF;
24106 /* First, find out if we use _any_ altivec registers. */
24107 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
24108 if (df_regs_ever_live_p (i))
24109 mask |= ALTIVEC_REG_BIT (i);
24111 if (mask == 0)
24112 return mask;
24114 /* Next, remove the argument registers from the set. These must
24115 be in the VRSAVE mask set by the caller, so we don't need to add
24116 them in again. More importantly, the mask we compute here is
24117 used to generate CLOBBERs in the set_vrsave insn, and we do not
24118 wish the argument registers to die. */
24119 for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
24120 mask &= ~ALTIVEC_REG_BIT (i);
24122 /* Similarly, remove the return value from the set. */
24124 bool yes = false;
24125 diddle_return_value (is_altivec_return_reg, &yes);
24126 if (yes)
24127 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
24130 return mask;
24133 /* For a very restricted set of circumstances, we can cut down the
24134 size of prologues/epilogues by calling our own save/restore-the-world
24135 routines. */
24137 static void
24138 compute_save_world_info (rs6000_stack_t *info)
24140 info->world_save_p = 1;
24141 info->world_save_p
24142 = (WORLD_SAVE_P (info)
24143 && DEFAULT_ABI == ABI_DARWIN
24144 && !cfun->has_nonlocal_label
24145 && info->first_fp_reg_save == FIRST_SAVED_FP_REGNO
24146 && info->first_gp_reg_save == FIRST_SAVED_GP_REGNO
24147 && info->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
24148 && info->cr_save_p);
24150 /* This will not work in conjunction with sibcalls. Make sure there
24151 are none. (This check is expensive, but seldom executed.) */
24152 if (WORLD_SAVE_P (info))
24154 rtx_insn *insn;
24155 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
24156 if (CALL_P (insn) && SIBLING_CALL_P (insn))
24158 info->world_save_p = 0;
24159 break;
24163 if (WORLD_SAVE_P (info))
24165 /* Even if we're not touching VRsave, make sure there's room on the
24166 stack for it, if it looks like we're calling SAVE_WORLD, which
24167 will attempt to save it. */
24168 info->vrsave_size = 4;
24170 /* If we are going to save the world, we need to save the link register too. */
24171 info->lr_save_p = 1;
24173 /* "Save" the VRsave register too if we're saving the world. */
24174 if (info->vrsave_mask == 0)
24175 info->vrsave_mask = compute_vrsave_mask ();
24177 /* Because the Darwin register save/restore routines only handle
24178 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
24179 check. */
24180 gcc_assert (info->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
24181 && (info->first_altivec_reg_save
24182 >= FIRST_SAVED_ALTIVEC_REGNO));
24185 return;
24189 static void
24190 is_altivec_return_reg (rtx reg, void *xyes)
24192 bool *yes = (bool *) xyes;
24193 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
24194 *yes = true;
24198 /* Return whether REG is a global user reg or has been specifed by
24199 -ffixed-REG. We should not restore these, and so cannot use
24200 lmw or out-of-line restore functions if there are any. We also
24201 can't save them (well, emit frame notes for them), because frame
24202 unwinding during exception handling will restore saved registers. */
24204 static bool
24205 fixed_reg_p (int reg)
24207 /* Ignore fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] when the
24208 backend sets it, overriding anything the user might have given. */
24209 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
24210 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
24211 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
24212 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
24213 return false;
24215 return fixed_regs[reg];
24218 /* Determine the strategy for savings/restoring registers. */
24220 enum {
24221 SAVE_MULTIPLE = 0x1,
24222 SAVE_INLINE_GPRS = 0x2,
24223 SAVE_INLINE_FPRS = 0x4,
24224 SAVE_NOINLINE_GPRS_SAVES_LR = 0x8,
24225 SAVE_NOINLINE_FPRS_SAVES_LR = 0x10,
24226 SAVE_INLINE_VRS = 0x20,
24227 REST_MULTIPLE = 0x100,
24228 REST_INLINE_GPRS = 0x200,
24229 REST_INLINE_FPRS = 0x400,
24230 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x800,
24231 REST_INLINE_VRS = 0x1000
24234 static int
24235 rs6000_savres_strategy (rs6000_stack_t *info,
24236 bool using_static_chain_p)
24238 int strategy = 0;
24240 /* Select between in-line and out-of-line save and restore of regs.
24241 First, all the obvious cases where we don't use out-of-line. */
24242 if (crtl->calls_eh_return
24243 || cfun->machine->ra_need_lr)
24244 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
24245 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
24246 | SAVE_INLINE_VRS | REST_INLINE_VRS);
24248 if (info->first_gp_reg_save == 32)
24249 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24251 if (info->first_fp_reg_save == 64
24252 /* The out-of-line FP routines use double-precision stores;
24253 we can't use those routines if we don't have such stores. */
24254 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT))
24255 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
24257 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
24258 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24260 /* Define cutoff for using out-of-line functions to save registers. */
24261 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
24263 if (!optimize_size)
24265 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
24266 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24267 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24269 else
24271 /* Prefer out-of-line restore if it will exit. */
24272 if (info->first_fp_reg_save > 61)
24273 strategy |= SAVE_INLINE_FPRS;
24274 if (info->first_gp_reg_save > 29)
24276 if (info->first_fp_reg_save == 64)
24277 strategy |= SAVE_INLINE_GPRS;
24278 else
24279 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24281 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
24282 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24285 else if (DEFAULT_ABI == ABI_DARWIN)
24287 if (info->first_fp_reg_save > 60)
24288 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
24289 if (info->first_gp_reg_save > 29)
24290 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24291 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24293 else
24295 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
24296 if ((flag_shrink_wrap_separate && optimize_function_for_speed_p (cfun))
24297 || info->first_fp_reg_save > 61)
24298 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
24299 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24300 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24303 /* Don't bother to try to save things out-of-line if r11 is occupied
24304 by the static chain. It would require too much fiddling and the
24305 static chain is rarely used anyway. FPRs are saved w.r.t the stack
24306 pointer on Darwin, and AIX uses r1 or r12. */
24307 if (using_static_chain_p
24308 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
24309 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
24310 | SAVE_INLINE_GPRS
24311 | SAVE_INLINE_VRS);
24313 /* Don't ever restore fixed regs. That means we can't use the
24314 out-of-line register restore functions if a fixed reg is in the
24315 range of regs restored. */
24316 if (!(strategy & REST_INLINE_FPRS))
24317 for (int i = info->first_fp_reg_save; i < 64; i++)
24318 if (fixed_regs[i])
24320 strategy |= REST_INLINE_FPRS;
24321 break;
24324 /* We can only use the out-of-line routines to restore fprs if we've
24325 saved all the registers from first_fp_reg_save in the prologue.
24326 Otherwise, we risk loading garbage. Of course, if we have saved
24327 out-of-line then we know we haven't skipped any fprs. */
24328 if ((strategy & SAVE_INLINE_FPRS)
24329 && !(strategy & REST_INLINE_FPRS))
24330 for (int i = info->first_fp_reg_save; i < 64; i++)
24331 if (!save_reg_p (i))
24333 strategy |= REST_INLINE_FPRS;
24334 break;
24337 /* Similarly, for altivec regs. */
24338 if (!(strategy & REST_INLINE_VRS))
24339 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
24340 if (fixed_regs[i])
24342 strategy |= REST_INLINE_VRS;
24343 break;
24346 if ((strategy & SAVE_INLINE_VRS)
24347 && !(strategy & REST_INLINE_VRS))
24348 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
24349 if (!save_reg_p (i))
24351 strategy |= REST_INLINE_VRS;
24352 break;
24355 /* info->lr_save_p isn't yet set if the only reason lr needs to be
24356 saved is an out-of-line save or restore. Set up the value for
24357 the next test (excluding out-of-line gprs). */
24358 bool lr_save_p = (info->lr_save_p
24359 || !(strategy & SAVE_INLINE_FPRS)
24360 || !(strategy & SAVE_INLINE_VRS)
24361 || !(strategy & REST_INLINE_FPRS)
24362 || !(strategy & REST_INLINE_VRS));
24364 if (TARGET_MULTIPLE
24365 && !TARGET_POWERPC64
24366 && info->first_gp_reg_save < 31
24367 && !(flag_shrink_wrap
24368 && flag_shrink_wrap_separate
24369 && optimize_function_for_speed_p (cfun)))
24371 int count = 0;
24372 for (int i = info->first_gp_reg_save; i < 32; i++)
24373 if (save_reg_p (i))
24374 count++;
24376 if (count <= 1)
24377 /* Don't use store multiple if only one reg needs to be
24378 saved. This can occur for example when the ABI_V4 pic reg
24379 (r30) needs to be saved to make calls, but r31 is not
24380 used. */
24381 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24382 else
24384 /* Prefer store multiple for saves over out-of-line
24385 routines, since the store-multiple instruction will
24386 always be smaller. */
24387 strategy |= SAVE_INLINE_GPRS | SAVE_MULTIPLE;
24389 /* The situation is more complicated with load multiple.
24390 We'd prefer to use the out-of-line routines for restores,
24391 since the "exit" out-of-line routines can handle the
24392 restore of LR and the frame teardown. However if doesn't
24393 make sense to use the out-of-line routine if that is the
24394 only reason we'd need to save LR, and we can't use the
24395 "exit" out-of-line gpr restore if we have saved some
24396 fprs; In those cases it is advantageous to use load
24397 multiple when available. */
24398 if (info->first_fp_reg_save != 64 || !lr_save_p)
24399 strategy |= REST_INLINE_GPRS | REST_MULTIPLE;
24403 /* Using the "exit" out-of-line routine does not improve code size
24404 if using it would require lr to be saved and if only saving one
24405 or two gprs. */
24406 else if (!lr_save_p && info->first_gp_reg_save > 29)
24407 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24409 /* Don't ever restore fixed regs. */
24410 if ((strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
24411 for (int i = info->first_gp_reg_save; i < 32; i++)
24412 if (fixed_reg_p (i))
24414 strategy |= REST_INLINE_GPRS;
24415 strategy &= ~REST_MULTIPLE;
24416 break;
24419 /* We can only use load multiple or the out-of-line routines to
24420 restore gprs if we've saved all the registers from
24421 first_gp_reg_save. Otherwise, we risk loading garbage.
24422 Of course, if we have saved out-of-line or used stmw then we know
24423 we haven't skipped any gprs. */
24424 if ((strategy & (SAVE_INLINE_GPRS | SAVE_MULTIPLE)) == SAVE_INLINE_GPRS
24425 && (strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
24426 for (int i = info->first_gp_reg_save; i < 32; i++)
24427 if (!save_reg_p (i))
24429 strategy |= REST_INLINE_GPRS;
24430 strategy &= ~REST_MULTIPLE;
24431 break;
24434 if (TARGET_ELF && TARGET_64BIT)
24436 if (!(strategy & SAVE_INLINE_FPRS))
24437 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
24438 else if (!(strategy & SAVE_INLINE_GPRS)
24439 && info->first_fp_reg_save == 64)
24440 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
24442 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
24443 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
24445 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
24446 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
24448 return strategy;
24451 /* Calculate the stack information for the current function. This is
24452 complicated by having two separate calling sequences, the AIX calling
24453 sequence and the V.4 calling sequence.
24455 AIX (and Darwin/Mac OS X) stack frames look like:
24456 32-bit 64-bit
24457 SP----> +---------------------------------------+
24458 | back chain to caller | 0 0
24459 +---------------------------------------+
24460 | saved CR | 4 8 (8-11)
24461 +---------------------------------------+
24462 | saved LR | 8 16
24463 +---------------------------------------+
24464 | reserved for compilers | 12 24
24465 +---------------------------------------+
24466 | reserved for binders | 16 32
24467 +---------------------------------------+
24468 | saved TOC pointer | 20 40
24469 +---------------------------------------+
24470 | Parameter save area (+padding*) (P) | 24 48
24471 +---------------------------------------+
24472 | Alloca space (A) | 24+P etc.
24473 +---------------------------------------+
24474 | Local variable space (L) | 24+P+A
24475 +---------------------------------------+
24476 | Float/int conversion temporary (X) | 24+P+A+L
24477 +---------------------------------------+
24478 | Save area for AltiVec registers (W) | 24+P+A+L+X
24479 +---------------------------------------+
24480 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
24481 +---------------------------------------+
24482 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
24483 +---------------------------------------+
24484 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
24485 +---------------------------------------+
24486 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
24487 +---------------------------------------+
24488 old SP->| back chain to caller's caller |
24489 +---------------------------------------+
24491 * If the alloca area is present, the parameter save area is
24492 padded so that the former starts 16-byte aligned.
24494 The required alignment for AIX configurations is two words (i.e., 8
24495 or 16 bytes).
24497 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
24499 SP----> +---------------------------------------+
24500 | Back chain to caller | 0
24501 +---------------------------------------+
24502 | Save area for CR | 8
24503 +---------------------------------------+
24504 | Saved LR | 16
24505 +---------------------------------------+
24506 | Saved TOC pointer | 24
24507 +---------------------------------------+
24508 | Parameter save area (+padding*) (P) | 32
24509 +---------------------------------------+
24510 | Alloca space (A) | 32+P
24511 +---------------------------------------+
24512 | Local variable space (L) | 32+P+A
24513 +---------------------------------------+
24514 | Save area for AltiVec registers (W) | 32+P+A+L
24515 +---------------------------------------+
24516 | AltiVec alignment padding (Y) | 32+P+A+L+W
24517 +---------------------------------------+
24518 | Save area for GP registers (G) | 32+P+A+L+W+Y
24519 +---------------------------------------+
24520 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
24521 +---------------------------------------+
24522 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
24523 +---------------------------------------+
24525 * If the alloca area is present, the parameter save area is
24526 padded so that the former starts 16-byte aligned.
24528 V.4 stack frames look like:
24530 SP----> +---------------------------------------+
24531 | back chain to caller | 0
24532 +---------------------------------------+
24533 | caller's saved LR | 4
24534 +---------------------------------------+
24535 | Parameter save area (+padding*) (P) | 8
24536 +---------------------------------------+
24537 | Alloca space (A) | 8+P
24538 +---------------------------------------+
24539 | Varargs save area (V) | 8+P+A
24540 +---------------------------------------+
24541 | Local variable space (L) | 8+P+A+V
24542 +---------------------------------------+
24543 | Float/int conversion temporary (X) | 8+P+A+V+L
24544 +---------------------------------------+
24545 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
24546 +---------------------------------------+
24547 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
24548 +---------------------------------------+
24549 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
24550 +---------------------------------------+
24551 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
24552 +---------------------------------------+
24553 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
24554 +---------------------------------------+
24555 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
24556 +---------------------------------------+
24557 old SP->| back chain to caller's caller |
24558 +---------------------------------------+
24560 * If the alloca area is present and the required alignment is
24561 16 bytes, the parameter save area is padded so that the
24562 alloca area starts 16-byte aligned.
24564 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
24565 given. (But note below and in sysv4.h that we require only 8 and
24566 may round up the size of our stack frame anyways. The historical
24567 reason is early versions of powerpc-linux which didn't properly
24568 align the stack at program startup. A happy side-effect is that
24569 -mno-eabi libraries can be used with -meabi programs.)
24571 The EABI configuration defaults to the V.4 layout. However,
24572 the stack alignment requirements may differ. If -mno-eabi is not
24573 given, the required stack alignment is 8 bytes; if -mno-eabi is
24574 given, the required alignment is 16 bytes. (But see V.4 comment
24575 above.) */
24577 #ifndef ABI_STACK_BOUNDARY
24578 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
24579 #endif
24581 static rs6000_stack_t *
24582 rs6000_stack_info (void)
24584 /* We should never be called for thunks, we are not set up for that. */
24585 gcc_assert (!cfun->is_thunk);
24587 rs6000_stack_t *info = &stack_info;
24588 int reg_size = TARGET_32BIT ? 4 : 8;
24589 int ehrd_size;
24590 int ehcr_size;
24591 int save_align;
24592 int first_gp;
24593 HOST_WIDE_INT non_fixed_size;
24594 bool using_static_chain_p;
24596 if (reload_completed && info->reload_completed)
24597 return info;
24599 memset (info, 0, sizeof (*info));
24600 info->reload_completed = reload_completed;
24602 /* Select which calling sequence. */
24603 info->abi = DEFAULT_ABI;
24605 /* Calculate which registers need to be saved & save area size. */
24606 info->first_gp_reg_save = first_reg_to_save ();
24607 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
24608 even if it currently looks like we won't. Reload may need it to
24609 get at a constant; if so, it will have already created a constant
24610 pool entry for it. */
24611 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
24612 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
24613 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
24614 && crtl->uses_const_pool
24615 && info->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
24616 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
24617 else
24618 first_gp = info->first_gp_reg_save;
24620 info->gp_size = reg_size * (32 - first_gp);
24622 info->first_fp_reg_save = first_fp_reg_to_save ();
24623 info->fp_size = 8 * (64 - info->first_fp_reg_save);
24625 info->first_altivec_reg_save = first_altivec_reg_to_save ();
24626 info->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
24627 - info->first_altivec_reg_save);
24629 /* Does this function call anything? */
24630 info->calls_p = (!crtl->is_leaf || cfun->machine->ra_needs_full_frame);
24632 /* Determine if we need to save the condition code registers. */
24633 if (save_reg_p (CR2_REGNO)
24634 || save_reg_p (CR3_REGNO)
24635 || save_reg_p (CR4_REGNO))
24637 info->cr_save_p = 1;
24638 if (DEFAULT_ABI == ABI_V4)
24639 info->cr_size = reg_size;
24642 /* If the current function calls __builtin_eh_return, then we need
24643 to allocate stack space for registers that will hold data for
24644 the exception handler. */
24645 if (crtl->calls_eh_return)
24647 unsigned int i;
24648 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
24649 continue;
24651 ehrd_size = i * UNITS_PER_WORD;
24653 else
24654 ehrd_size = 0;
24656 /* In the ELFv2 ABI, we also need to allocate space for separate
24657 CR field save areas if the function calls __builtin_eh_return. */
24658 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
24660 /* This hard-codes that we have three call-saved CR fields. */
24661 ehcr_size = 3 * reg_size;
24662 /* We do *not* use the regular CR save mechanism. */
24663 info->cr_save_p = 0;
24665 else
24666 ehcr_size = 0;
24668 /* Determine various sizes. */
24669 info->reg_size = reg_size;
24670 info->fixed_size = RS6000_SAVE_AREA;
24671 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
24672 if (cfun->calls_alloca)
24673 info->parm_size =
24674 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
24675 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
24676 else
24677 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
24678 TARGET_ALTIVEC ? 16 : 8);
24679 if (FRAME_GROWS_DOWNWARD)
24680 info->vars_size
24681 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
24682 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
24683 - (info->fixed_size + info->vars_size + info->parm_size);
24685 if (TARGET_ALTIVEC_ABI)
24686 info->vrsave_mask = compute_vrsave_mask ();
24688 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
24689 info->vrsave_size = 4;
24691 compute_save_world_info (info);
24693 /* Calculate the offsets. */
24694 switch (DEFAULT_ABI)
24696 case ABI_NONE:
24697 default:
24698 gcc_unreachable ();
24700 case ABI_AIX:
24701 case ABI_ELFv2:
24702 case ABI_DARWIN:
24703 info->fp_save_offset = -info->fp_size;
24704 info->gp_save_offset = info->fp_save_offset - info->gp_size;
24706 if (TARGET_ALTIVEC_ABI)
24708 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
24710 /* Align stack so vector save area is on a quadword boundary.
24711 The padding goes above the vectors. */
24712 if (info->altivec_size != 0)
24713 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
24715 info->altivec_save_offset = info->vrsave_save_offset
24716 - info->altivec_padding_size
24717 - info->altivec_size;
24718 gcc_assert (info->altivec_size == 0
24719 || info->altivec_save_offset % 16 == 0);
24721 /* Adjust for AltiVec case. */
24722 info->ehrd_offset = info->altivec_save_offset - ehrd_size;
24724 else
24725 info->ehrd_offset = info->gp_save_offset - ehrd_size;
24727 info->ehcr_offset = info->ehrd_offset - ehcr_size;
24728 info->cr_save_offset = reg_size; /* first word when 64-bit. */
24729 info->lr_save_offset = 2*reg_size;
24730 break;
24732 case ABI_V4:
24733 info->fp_save_offset = -info->fp_size;
24734 info->gp_save_offset = info->fp_save_offset - info->gp_size;
24735 info->cr_save_offset = info->gp_save_offset - info->cr_size;
24737 if (TARGET_ALTIVEC_ABI)
24739 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
24741 /* Align stack so vector save area is on a quadword boundary. */
24742 if (info->altivec_size != 0)
24743 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
24745 info->altivec_save_offset = info->vrsave_save_offset
24746 - info->altivec_padding_size
24747 - info->altivec_size;
24749 /* Adjust for AltiVec case. */
24750 info->ehrd_offset = info->altivec_save_offset;
24752 else
24753 info->ehrd_offset = info->cr_save_offset;
24755 info->ehrd_offset -= ehrd_size;
24756 info->lr_save_offset = reg_size;
24759 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
24760 info->save_size = RS6000_ALIGN (info->fp_size
24761 + info->gp_size
24762 + info->altivec_size
24763 + info->altivec_padding_size
24764 + ehrd_size
24765 + ehcr_size
24766 + info->cr_size
24767 + info->vrsave_size,
24768 save_align);
24770 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
24772 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
24773 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
24775 /* Determine if we need to save the link register. */
24776 if (info->calls_p
24777 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
24778 && crtl->profile
24779 && !TARGET_PROFILE_KERNEL)
24780 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
24781 #ifdef TARGET_RELOCATABLE
24782 || (DEFAULT_ABI == ABI_V4
24783 && (TARGET_RELOCATABLE || flag_pic > 1)
24784 && !constant_pool_empty_p ())
24785 #endif
24786 || rs6000_ra_ever_killed ())
24787 info->lr_save_p = 1;
24789 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
24790 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
24791 && call_used_regs[STATIC_CHAIN_REGNUM]);
24792 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
24794 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
24795 || !(info->savres_strategy & SAVE_INLINE_FPRS)
24796 || !(info->savres_strategy & SAVE_INLINE_VRS)
24797 || !(info->savres_strategy & REST_INLINE_GPRS)
24798 || !(info->savres_strategy & REST_INLINE_FPRS)
24799 || !(info->savres_strategy & REST_INLINE_VRS))
24800 info->lr_save_p = 1;
24802 if (info->lr_save_p)
24803 df_set_regs_ever_live (LR_REGNO, true);
24805 /* Determine if we need to allocate any stack frame:
24807 For AIX we need to push the stack if a frame pointer is needed
24808 (because the stack might be dynamically adjusted), if we are
24809 debugging, if we make calls, or if the sum of fp_save, gp_save,
24810 and local variables are more than the space needed to save all
24811 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
24812 + 18*8 = 288 (GPR13 reserved).
24814 For V.4 we don't have the stack cushion that AIX uses, but assume
24815 that the debugger can handle stackless frames. */
24817 if (info->calls_p)
24818 info->push_p = 1;
24820 else if (DEFAULT_ABI == ABI_V4)
24821 info->push_p = non_fixed_size != 0;
24823 else if (frame_pointer_needed)
24824 info->push_p = 1;
24826 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
24827 info->push_p = 1;
24829 else
24830 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
24832 return info;
24835 static void
24836 debug_stack_info (rs6000_stack_t *info)
24838 const char *abi_string;
24840 if (! info)
24841 info = rs6000_stack_info ();
24843 fprintf (stderr, "\nStack information for function %s:\n",
24844 ((current_function_decl && DECL_NAME (current_function_decl))
24845 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
24846 : "<unknown>"));
24848 switch (info->abi)
24850 default: abi_string = "Unknown"; break;
24851 case ABI_NONE: abi_string = "NONE"; break;
24852 case ABI_AIX: abi_string = "AIX"; break;
24853 case ABI_ELFv2: abi_string = "ELFv2"; break;
24854 case ABI_DARWIN: abi_string = "Darwin"; break;
24855 case ABI_V4: abi_string = "V.4"; break;
24858 fprintf (stderr, "\tABI = %5s\n", abi_string);
24860 if (TARGET_ALTIVEC_ABI)
24861 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
24863 if (info->first_gp_reg_save != 32)
24864 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
24866 if (info->first_fp_reg_save != 64)
24867 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
24869 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
24870 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
24871 info->first_altivec_reg_save);
24873 if (info->lr_save_p)
24874 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
24876 if (info->cr_save_p)
24877 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
24879 if (info->vrsave_mask)
24880 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
24882 if (info->push_p)
24883 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
24885 if (info->calls_p)
24886 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
24888 if (info->gp_size)
24889 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
24891 if (info->fp_size)
24892 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
24894 if (info->altivec_size)
24895 fprintf (stderr, "\taltivec_save_offset = %5d\n",
24896 info->altivec_save_offset);
24898 if (info->vrsave_size)
24899 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
24900 info->vrsave_save_offset);
24902 if (info->lr_save_p)
24903 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
24905 if (info->cr_save_p)
24906 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
24908 if (info->varargs_save_offset)
24909 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
24911 if (info->total_size)
24912 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
24913 info->total_size);
24915 if (info->vars_size)
24916 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
24917 info->vars_size);
24919 if (info->parm_size)
24920 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
24922 if (info->fixed_size)
24923 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
24925 if (info->gp_size)
24926 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
24928 if (info->fp_size)
24929 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
24931 if (info->altivec_size)
24932 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
24934 if (info->vrsave_size)
24935 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
24937 if (info->altivec_padding_size)
24938 fprintf (stderr, "\taltivec_padding_size= %5d\n",
24939 info->altivec_padding_size);
24941 if (info->cr_size)
24942 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
24944 if (info->save_size)
24945 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
24947 if (info->reg_size != 4)
24948 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
24950 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
24952 fprintf (stderr, "\n");
24956 rs6000_return_addr (int count, rtx frame)
24958 /* Currently we don't optimize very well between prolog and body
24959 code and for PIC code the code can be actually quite bad, so
24960 don't try to be too clever here. */
24961 if (count != 0
24962 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
24964 cfun->machine->ra_needs_full_frame = 1;
24966 return
24967 gen_rtx_MEM
24968 (Pmode,
24969 memory_address
24970 (Pmode,
24971 plus_constant (Pmode,
24972 copy_to_reg
24973 (gen_rtx_MEM (Pmode,
24974 memory_address (Pmode, frame))),
24975 RETURN_ADDRESS_OFFSET)));
24978 cfun->machine->ra_need_lr = 1;
24979 return get_hard_reg_initial_val (Pmode, LR_REGNO);
24982 /* Say whether a function is a candidate for sibcall handling or not. */
24984 static bool
24985 rs6000_function_ok_for_sibcall (tree decl, tree exp)
24987 tree fntype;
24989 if (decl)
24990 fntype = TREE_TYPE (decl);
24991 else
24992 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
24994 /* We can't do it if the called function has more vector parameters
24995 than the current function; there's nowhere to put the VRsave code. */
24996 if (TARGET_ALTIVEC_ABI
24997 && TARGET_ALTIVEC_VRSAVE
24998 && !(decl && decl == current_function_decl))
25000 function_args_iterator args_iter;
25001 tree type;
25002 int nvreg = 0;
25004 /* Functions with vector parameters are required to have a
25005 prototype, so the argument type info must be available
25006 here. */
25007 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
25008 if (TREE_CODE (type) == VECTOR_TYPE
25009 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
25010 nvreg++;
25012 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
25013 if (TREE_CODE (type) == VECTOR_TYPE
25014 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
25015 nvreg--;
25017 if (nvreg > 0)
25018 return false;
25021 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
25022 functions, because the callee may have a different TOC pointer to
25023 the caller and there's no way to ensure we restore the TOC when
25024 we return. With the secure-plt SYSV ABI we can't make non-local
25025 calls when -fpic/PIC because the plt call stubs use r30. */
25026 if (DEFAULT_ABI == ABI_DARWIN
25027 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25028 && decl
25029 && !DECL_EXTERNAL (decl)
25030 && !DECL_WEAK (decl)
25031 && (*targetm.binds_local_p) (decl))
25032 || (DEFAULT_ABI == ABI_V4
25033 && (!TARGET_SECURE_PLT
25034 || !flag_pic
25035 || (decl
25036 && (*targetm.binds_local_p) (decl)))))
25038 tree attr_list = TYPE_ATTRIBUTES (fntype);
25040 if (!lookup_attribute ("longcall", attr_list)
25041 || lookup_attribute ("shortcall", attr_list))
25042 return true;
25045 return false;
25048 static int
25049 rs6000_ra_ever_killed (void)
25051 rtx_insn *top;
25052 rtx reg;
25053 rtx_insn *insn;
25055 if (cfun->is_thunk)
25056 return 0;
25058 if (cfun->machine->lr_save_state)
25059 return cfun->machine->lr_save_state - 1;
25061 /* regs_ever_live has LR marked as used if any sibcalls are present,
25062 but this should not force saving and restoring in the
25063 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
25064 clobbers LR, so that is inappropriate. */
25066 /* Also, the prologue can generate a store into LR that
25067 doesn't really count, like this:
25069 move LR->R0
25070 bcl to set PIC register
25071 move LR->R31
25072 move R0->LR
25074 When we're called from the epilogue, we need to avoid counting
25075 this as a store. */
25077 push_topmost_sequence ();
25078 top = get_insns ();
25079 pop_topmost_sequence ();
25080 reg = gen_rtx_REG (Pmode, LR_REGNO);
25082 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
25084 if (INSN_P (insn))
25086 if (CALL_P (insn))
25088 if (!SIBLING_CALL_P (insn))
25089 return 1;
25091 else if (find_regno_note (insn, REG_INC, LR_REGNO))
25092 return 1;
25093 else if (set_of (reg, insn) != NULL_RTX
25094 && !prologue_epilogue_contains (insn))
25095 return 1;
25098 return 0;
25101 /* Emit instructions needed to load the TOC register.
25102 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
25103 a constant pool; or for SVR4 -fpic. */
25105 void
25106 rs6000_emit_load_toc_table (int fromprolog)
25108 rtx dest;
25109 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
25111 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
25113 char buf[30];
25114 rtx lab, tmp1, tmp2, got;
25116 lab = gen_label_rtx ();
25117 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
25118 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
25119 if (flag_pic == 2)
25121 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
25122 need_toc_init = 1;
25124 else
25125 got = rs6000_got_sym ();
25126 tmp1 = tmp2 = dest;
25127 if (!fromprolog)
25129 tmp1 = gen_reg_rtx (Pmode);
25130 tmp2 = gen_reg_rtx (Pmode);
25132 emit_insn (gen_load_toc_v4_PIC_1 (lab));
25133 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
25134 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
25135 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
25137 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
25139 emit_insn (gen_load_toc_v4_pic_si ());
25140 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
25142 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
25144 char buf[30];
25145 rtx temp0 = (fromprolog
25146 ? gen_rtx_REG (Pmode, 0)
25147 : gen_reg_rtx (Pmode));
25149 if (fromprolog)
25151 rtx symF, symL;
25153 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
25154 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
25156 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
25157 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
25159 emit_insn (gen_load_toc_v4_PIC_1 (symF));
25160 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
25161 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
25163 else
25165 rtx tocsym, lab;
25167 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
25168 need_toc_init = 1;
25169 lab = gen_label_rtx ();
25170 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
25171 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
25172 if (TARGET_LINK_STACK)
25173 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
25174 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
25176 emit_insn (gen_addsi3 (dest, temp0, dest));
25178 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
25180 /* This is for AIX code running in non-PIC ELF32. */
25181 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
25183 need_toc_init = 1;
25184 emit_insn (gen_elf_high (dest, realsym));
25185 emit_insn (gen_elf_low (dest, dest, realsym));
25187 else
25189 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
25191 if (TARGET_32BIT)
25192 emit_insn (gen_load_toc_aix_si (dest));
25193 else
25194 emit_insn (gen_load_toc_aix_di (dest));
25198 /* Emit instructions to restore the link register after determining where
25199 its value has been stored. */
25201 void
25202 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
25204 rs6000_stack_t *info = rs6000_stack_info ();
25205 rtx operands[2];
25207 operands[0] = source;
25208 operands[1] = scratch;
25210 if (info->lr_save_p)
25212 rtx frame_rtx = stack_pointer_rtx;
25213 HOST_WIDE_INT sp_offset = 0;
25214 rtx tmp;
25216 if (frame_pointer_needed
25217 || cfun->calls_alloca
25218 || info->total_size > 32767)
25220 tmp = gen_frame_mem (Pmode, frame_rtx);
25221 emit_move_insn (operands[1], tmp);
25222 frame_rtx = operands[1];
25224 else if (info->push_p)
25225 sp_offset = info->total_size;
25227 tmp = plus_constant (Pmode, frame_rtx,
25228 info->lr_save_offset + sp_offset);
25229 tmp = gen_frame_mem (Pmode, tmp);
25230 emit_move_insn (tmp, operands[0]);
25232 else
25233 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
25235 /* Freeze lr_save_p. We've just emitted rtl that depends on the
25236 state of lr_save_p so any change from here on would be a bug. In
25237 particular, stop rs6000_ra_ever_killed from considering the SET
25238 of lr we may have added just above. */
25239 cfun->machine->lr_save_state = info->lr_save_p + 1;
25242 static GTY(()) alias_set_type set = -1;
25244 alias_set_type
25245 get_TOC_alias_set (void)
25247 if (set == -1)
25248 set = new_alias_set ();
25249 return set;
25252 /* This returns nonzero if the current function uses the TOC. This is
25253 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
25254 is generated by the ABI_V4 load_toc_* patterns. */
25255 #if TARGET_ELF
25256 static int
25257 uses_TOC (void)
25259 rtx_insn *insn;
25261 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
25262 if (INSN_P (insn))
25264 rtx pat = PATTERN (insn);
25265 int i;
25267 if (GET_CODE (pat) == PARALLEL)
25268 for (i = 0; i < XVECLEN (pat, 0); i++)
25270 rtx sub = XVECEXP (pat, 0, i);
25271 if (GET_CODE (sub) == USE)
25273 sub = XEXP (sub, 0);
25274 if (GET_CODE (sub) == UNSPEC
25275 && XINT (sub, 1) == UNSPEC_TOC)
25276 return 1;
25280 return 0;
25282 #endif
25285 create_TOC_reference (rtx symbol, rtx largetoc_reg)
25287 rtx tocrel, tocreg, hi;
25289 if (TARGET_DEBUG_ADDR)
25291 if (GET_CODE (symbol) == SYMBOL_REF)
25292 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
25293 XSTR (symbol, 0));
25294 else
25296 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
25297 GET_RTX_NAME (GET_CODE (symbol)));
25298 debug_rtx (symbol);
25302 if (!can_create_pseudo_p ())
25303 df_set_regs_ever_live (TOC_REGISTER, true);
25305 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
25306 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
25307 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
25308 return tocrel;
25310 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
25311 if (largetoc_reg != NULL)
25313 emit_move_insn (largetoc_reg, hi);
25314 hi = largetoc_reg;
25316 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
25319 /* Issue assembly directives that create a reference to the given DWARF
25320 FRAME_TABLE_LABEL from the current function section. */
25321 void
25322 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
25324 fprintf (asm_out_file, "\t.ref %s\n",
25325 (* targetm.strip_name_encoding) (frame_table_label));
25328 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
25329 and the change to the stack pointer. */
25331 static void
25332 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
25334 rtvec p;
25335 int i;
25336 rtx regs[3];
25338 i = 0;
25339 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
25340 if (hard_frame_needed)
25341 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
25342 if (!(REGNO (fp) == STACK_POINTER_REGNUM
25343 || (hard_frame_needed
25344 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
25345 regs[i++] = fp;
25347 p = rtvec_alloc (i);
25348 while (--i >= 0)
25350 rtx mem = gen_frame_mem (BLKmode, regs[i]);
25351 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
25354 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
25357 /* Emit the correct code for allocating stack space, as insns.
25358 If COPY_REG, make sure a copy of the old frame is left there.
25359 The generated code may use hard register 0 as a temporary. */
25361 static rtx_insn *
25362 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
25364 rtx_insn *insn;
25365 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
25366 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
25367 rtx todec = gen_int_mode (-size, Pmode);
25368 rtx par, set, mem;
25370 if (INTVAL (todec) != -size)
25372 warning (0, "stack frame too large");
25373 emit_insn (gen_trap ());
25374 return 0;
25377 if (crtl->limit_stack)
25379 if (REG_P (stack_limit_rtx)
25380 && REGNO (stack_limit_rtx) > 1
25381 && REGNO (stack_limit_rtx) <= 31)
25383 rtx_insn *insn
25384 = gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size));
25385 gcc_assert (insn);
25386 emit_insn (insn);
25387 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg, const0_rtx));
25389 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
25390 && TARGET_32BIT
25391 && DEFAULT_ABI == ABI_V4
25392 && !flag_pic)
25394 rtx toload = gen_rtx_CONST (VOIDmode,
25395 gen_rtx_PLUS (Pmode,
25396 stack_limit_rtx,
25397 GEN_INT (size)));
25399 emit_insn (gen_elf_high (tmp_reg, toload));
25400 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
25401 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
25402 const0_rtx));
25404 else
25405 warning (0, "stack limit expression is not supported");
25408 if (copy_reg)
25410 if (copy_off != 0)
25411 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
25412 else
25413 emit_move_insn (copy_reg, stack_reg);
25416 if (size > 32767)
25418 /* Need a note here so that try_split doesn't get confused. */
25419 if (get_last_insn () == NULL_RTX)
25420 emit_note (NOTE_INSN_DELETED);
25421 insn = emit_move_insn (tmp_reg, todec);
25422 try_split (PATTERN (insn), insn, 0);
25423 todec = tmp_reg;
25426 insn = emit_insn (TARGET_32BIT
25427 ? gen_movsi_update_stack (stack_reg, stack_reg,
25428 todec, stack_reg)
25429 : gen_movdi_di_update_stack (stack_reg, stack_reg,
25430 todec, stack_reg));
25431 /* Since we didn't use gen_frame_mem to generate the MEM, grab
25432 it now and set the alias set/attributes. The above gen_*_update
25433 calls will generate a PARALLEL with the MEM set being the first
25434 operation. */
25435 par = PATTERN (insn);
25436 gcc_assert (GET_CODE (par) == PARALLEL);
25437 set = XVECEXP (par, 0, 0);
25438 gcc_assert (GET_CODE (set) == SET);
25439 mem = SET_DEST (set);
25440 gcc_assert (MEM_P (mem));
25441 MEM_NOTRAP_P (mem) = 1;
25442 set_mem_alias_set (mem, get_frame_alias_set ());
25444 RTX_FRAME_RELATED_P (insn) = 1;
25445 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
25446 gen_rtx_SET (stack_reg, gen_rtx_PLUS (Pmode, stack_reg,
25447 GEN_INT (-size))));
25448 return insn;
25451 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
25453 #if PROBE_INTERVAL > 32768
25454 #error Cannot use indexed addressing mode for stack probing
25455 #endif
25457 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
25458 inclusive. These are offsets from the current stack pointer. */
25460 static void
25461 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
25463 /* See if we have a constant small number of probes to generate. If so,
25464 that's the easy case. */
25465 if (first + size <= 32768)
25467 HOST_WIDE_INT i;
25469 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
25470 it exceeds SIZE. If only one probe is needed, this will not
25471 generate any code. Then probe at FIRST + SIZE. */
25472 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
25473 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
25474 -(first + i)));
25476 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
25477 -(first + size)));
25480 /* Otherwise, do the same as above, but in a loop. Note that we must be
25481 extra careful with variables wrapping around because we might be at
25482 the very top (or the very bottom) of the address space and we have
25483 to be able to handle this case properly; in particular, we use an
25484 equality test for the loop condition. */
25485 else
25487 HOST_WIDE_INT rounded_size;
25488 rtx r12 = gen_rtx_REG (Pmode, 12);
25489 rtx r0 = gen_rtx_REG (Pmode, 0);
25491 /* Sanity check for the addressing mode we're going to use. */
25492 gcc_assert (first <= 32768);
25494 /* Step 1: round SIZE to the previous multiple of the interval. */
25496 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
25499 /* Step 2: compute initial and final value of the loop counter. */
25501 /* TEST_ADDR = SP + FIRST. */
25502 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
25503 -first)));
25505 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
25506 if (rounded_size > 32768)
25508 emit_move_insn (r0, GEN_INT (-rounded_size));
25509 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
25511 else
25512 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
25513 -rounded_size)));
25516 /* Step 3: the loop
25520 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
25521 probe at TEST_ADDR
25523 while (TEST_ADDR != LAST_ADDR)
25525 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
25526 until it is equal to ROUNDED_SIZE. */
25528 if (TARGET_64BIT)
25529 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
25530 else
25531 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
25534 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
25535 that SIZE is equal to ROUNDED_SIZE. */
25537 if (size != rounded_size)
25538 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
25542 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
25543 absolute addresses. */
25545 const char *
25546 output_probe_stack_range (rtx reg1, rtx reg2)
25548 static int labelno = 0;
25549 char loop_lab[32];
25550 rtx xops[2];
25552 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
25554 /* Loop. */
25555 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
25557 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
25558 xops[0] = reg1;
25559 xops[1] = GEN_INT (-PROBE_INTERVAL);
25560 output_asm_insn ("addi %0,%0,%1", xops);
25562 /* Probe at TEST_ADDR. */
25563 xops[1] = gen_rtx_REG (Pmode, 0);
25564 output_asm_insn ("stw %1,0(%0)", xops);
25566 /* Test if TEST_ADDR == LAST_ADDR. */
25567 xops[1] = reg2;
25568 if (TARGET_64BIT)
25569 output_asm_insn ("cmpd 0,%0,%1", xops);
25570 else
25571 output_asm_insn ("cmpw 0,%0,%1", xops);
25573 /* Branch. */
25574 fputs ("\tbne 0,", asm_out_file);
25575 assemble_name_raw (asm_out_file, loop_lab);
25576 fputc ('\n', asm_out_file);
25578 return "";
25581 /* This function is called when rs6000_frame_related is processing
25582 SETs within a PARALLEL, and returns whether the REGNO save ought to
25583 be marked RTX_FRAME_RELATED_P. The PARALLELs involved are those
25584 for out-of-line register save functions, store multiple, and the
25585 Darwin world_save. They may contain registers that don't really
25586 need saving. */
25588 static bool
25589 interesting_frame_related_regno (unsigned int regno)
25591 /* Saves apparently of r0 are actually saving LR. It doesn't make
25592 sense to substitute the regno here to test save_reg_p (LR_REGNO).
25593 We *know* LR needs saving, and dwarf2cfi.c is able to deduce that
25594 (set (mem) (r0)) is saving LR from a prior (set (r0) (lr)) marked
25595 as frame related. */
25596 if (regno == 0)
25597 return true;
25598 /* If we see CR2 then we are here on a Darwin world save. Saves of
25599 CR2 signify the whole CR is being saved. This is a long-standing
25600 ABI wart fixed by ELFv2. As for r0/lr there is no need to check
25601 that CR needs to be saved. */
25602 if (regno == CR2_REGNO)
25603 return true;
25604 /* Omit frame info for any user-defined global regs. If frame info
25605 is supplied for them, frame unwinding will restore a user reg.
25606 Also omit frame info for any reg we don't need to save, as that
25607 bloats frame info and can cause problems with shrink wrapping.
25608 Since global regs won't be seen as needing to be saved, both of
25609 these conditions are covered by save_reg_p. */
25610 return save_reg_p (regno);
25613 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
25614 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
25615 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
25616 deduce these equivalences by itself so it wasn't necessary to hold
25617 its hand so much. Don't be tempted to always supply d2_f_d_e with
25618 the actual cfa register, ie. r31 when we are using a hard frame
25619 pointer. That fails when saving regs off r1, and sched moves the
25620 r31 setup past the reg saves. */
25622 static rtx_insn *
25623 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
25624 rtx reg2, rtx repl2)
25626 rtx repl;
25628 if (REGNO (reg) == STACK_POINTER_REGNUM)
25630 gcc_checking_assert (val == 0);
25631 repl = NULL_RTX;
25633 else
25634 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
25635 GEN_INT (val));
25637 rtx pat = PATTERN (insn);
25638 if (!repl && !reg2)
25640 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
25641 if (GET_CODE (pat) == PARALLEL)
25642 for (int i = 0; i < XVECLEN (pat, 0); i++)
25643 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
25645 rtx set = XVECEXP (pat, 0, i);
25647 if (!REG_P (SET_SRC (set))
25648 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
25649 RTX_FRAME_RELATED_P (set) = 1;
25651 RTX_FRAME_RELATED_P (insn) = 1;
25652 return insn;
25655 /* We expect that 'pat' is either a SET or a PARALLEL containing
25656 SETs (and possibly other stuff). In a PARALLEL, all the SETs
25657 are important so they all have to be marked RTX_FRAME_RELATED_P.
25658 Call simplify_replace_rtx on the SETs rather than the whole insn
25659 so as to leave the other stuff alone (for example USE of r12). */
25661 set_used_flags (pat);
25662 if (GET_CODE (pat) == SET)
25664 if (repl)
25665 pat = simplify_replace_rtx (pat, reg, repl);
25666 if (reg2)
25667 pat = simplify_replace_rtx (pat, reg2, repl2);
25669 else if (GET_CODE (pat) == PARALLEL)
25671 pat = shallow_copy_rtx (pat);
25672 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
25674 for (int i = 0; i < XVECLEN (pat, 0); i++)
25675 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
25677 rtx set = XVECEXP (pat, 0, i);
25679 if (repl)
25680 set = simplify_replace_rtx (set, reg, repl);
25681 if (reg2)
25682 set = simplify_replace_rtx (set, reg2, repl2);
25683 XVECEXP (pat, 0, i) = set;
25685 if (!REG_P (SET_SRC (set))
25686 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
25687 RTX_FRAME_RELATED_P (set) = 1;
25690 else
25691 gcc_unreachable ();
25693 RTX_FRAME_RELATED_P (insn) = 1;
25694 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
25696 return insn;
25699 /* Returns an insn that has a vrsave set operation with the
25700 appropriate CLOBBERs. */
25702 static rtx
25703 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
25705 int nclobs, i;
25706 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
25707 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
25709 clobs[0]
25710 = gen_rtx_SET (vrsave,
25711 gen_rtx_UNSPEC_VOLATILE (SImode,
25712 gen_rtvec (2, reg, vrsave),
25713 UNSPECV_SET_VRSAVE));
25715 nclobs = 1;
25717 /* We need to clobber the registers in the mask so the scheduler
25718 does not move sets to VRSAVE before sets of AltiVec registers.
25720 However, if the function receives nonlocal gotos, reload will set
25721 all call saved registers live. We will end up with:
25723 (set (reg 999) (mem))
25724 (parallel [ (set (reg vrsave) (unspec blah))
25725 (clobber (reg 999))])
25727 The clobber will cause the store into reg 999 to be dead, and
25728 flow will attempt to delete an epilogue insn. In this case, we
25729 need an unspec use/set of the register. */
25731 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
25732 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
25734 if (!epiloguep || call_used_regs [i])
25735 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
25736 gen_rtx_REG (V4SImode, i));
25737 else
25739 rtx reg = gen_rtx_REG (V4SImode, i);
25741 clobs[nclobs++]
25742 = gen_rtx_SET (reg,
25743 gen_rtx_UNSPEC (V4SImode,
25744 gen_rtvec (1, reg), 27));
25748 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
25750 for (i = 0; i < nclobs; ++i)
25751 XVECEXP (insn, 0, i) = clobs[i];
25753 return insn;
25756 static rtx
25757 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
25759 rtx addr, mem;
25761 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
25762 mem = gen_frame_mem (GET_MODE (reg), addr);
25763 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
25766 static rtx
25767 gen_frame_load (rtx reg, rtx frame_reg, int offset)
25769 return gen_frame_set (reg, frame_reg, offset, false);
25772 static rtx
25773 gen_frame_store (rtx reg, rtx frame_reg, int offset)
25775 return gen_frame_set (reg, frame_reg, offset, true);
25778 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
25779 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
25781 static rtx_insn *
25782 emit_frame_save (rtx frame_reg, machine_mode mode,
25783 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
25785 rtx reg;
25787 /* Some cases that need register indexed addressing. */
25788 gcc_checking_assert (!(TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
25789 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode)));
25791 reg = gen_rtx_REG (mode, regno);
25792 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
25793 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
25794 NULL_RTX, NULL_RTX);
25797 /* Emit an offset memory reference suitable for a frame store, while
25798 converting to a valid addressing mode. */
25800 static rtx
25801 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
25803 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, GEN_INT (offset)));
25806 #ifndef TARGET_FIX_AND_CONTINUE
25807 #define TARGET_FIX_AND_CONTINUE 0
25808 #endif
25810 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
25811 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
25812 #define LAST_SAVRES_REGISTER 31
25813 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
25815 enum {
25816 SAVRES_LR = 0x1,
25817 SAVRES_SAVE = 0x2,
25818 SAVRES_REG = 0x0c,
25819 SAVRES_GPR = 0,
25820 SAVRES_FPR = 4,
25821 SAVRES_VR = 8
25824 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
25826 /* Temporary holding space for an out-of-line register save/restore
25827 routine name. */
25828 static char savres_routine_name[30];
25830 /* Return the name for an out-of-line register save/restore routine.
25831 We are saving/restoring GPRs if GPR is true. */
25833 static char *
25834 rs6000_savres_routine_name (int regno, int sel)
25836 const char *prefix = "";
25837 const char *suffix = "";
25839 /* Different targets are supposed to define
25840 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
25841 routine name could be defined with:
25843 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
25845 This is a nice idea in practice, but in reality, things are
25846 complicated in several ways:
25848 - ELF targets have save/restore routines for GPRs.
25850 - PPC64 ELF targets have routines for save/restore of GPRs that
25851 differ in what they do with the link register, so having a set
25852 prefix doesn't work. (We only use one of the save routines at
25853 the moment, though.)
25855 - PPC32 elf targets have "exit" versions of the restore routines
25856 that restore the link register and can save some extra space.
25857 These require an extra suffix. (There are also "tail" versions
25858 of the restore routines and "GOT" versions of the save routines,
25859 but we don't generate those at present. Same problems apply,
25860 though.)
25862 We deal with all this by synthesizing our own prefix/suffix and
25863 using that for the simple sprintf call shown above. */
25864 if (DEFAULT_ABI == ABI_V4)
25866 if (TARGET_64BIT)
25867 goto aix_names;
25869 if ((sel & SAVRES_REG) == SAVRES_GPR)
25870 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
25871 else if ((sel & SAVRES_REG) == SAVRES_FPR)
25872 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
25873 else if ((sel & SAVRES_REG) == SAVRES_VR)
25874 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
25875 else
25876 abort ();
25878 if ((sel & SAVRES_LR))
25879 suffix = "_x";
25881 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25883 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
25884 /* No out-of-line save/restore routines for GPRs on AIX. */
25885 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
25886 #endif
25888 aix_names:
25889 if ((sel & SAVRES_REG) == SAVRES_GPR)
25890 prefix = ((sel & SAVRES_SAVE)
25891 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
25892 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
25893 else if ((sel & SAVRES_REG) == SAVRES_FPR)
25895 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
25896 if ((sel & SAVRES_LR))
25897 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
25898 else
25899 #endif
25901 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
25902 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
25905 else if ((sel & SAVRES_REG) == SAVRES_VR)
25906 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
25907 else
25908 abort ();
25911 if (DEFAULT_ABI == ABI_DARWIN)
25913 /* The Darwin approach is (slightly) different, in order to be
25914 compatible with code generated by the system toolchain. There is a
25915 single symbol for the start of save sequence, and the code here
25916 embeds an offset into that code on the basis of the first register
25917 to be saved. */
25918 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
25919 if ((sel & SAVRES_REG) == SAVRES_GPR)
25920 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
25921 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
25922 (regno - 13) * 4, prefix, regno);
25923 else if ((sel & SAVRES_REG) == SAVRES_FPR)
25924 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
25925 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
25926 else if ((sel & SAVRES_REG) == SAVRES_VR)
25927 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
25928 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
25929 else
25930 abort ();
25932 else
25933 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
25935 return savres_routine_name;
25938 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
25939 We are saving/restoring GPRs if GPR is true. */
25941 static rtx
25942 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
25944 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
25945 ? info->first_gp_reg_save
25946 : (sel & SAVRES_REG) == SAVRES_FPR
25947 ? info->first_fp_reg_save - 32
25948 : (sel & SAVRES_REG) == SAVRES_VR
25949 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
25950 : -1);
25951 rtx sym;
25952 int select = sel;
25954 /* Don't generate bogus routine names. */
25955 gcc_assert (FIRST_SAVRES_REGISTER <= regno
25956 && regno <= LAST_SAVRES_REGISTER
25957 && select >= 0 && select <= 12);
25959 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
25961 if (sym == NULL)
25963 char *name;
25965 name = rs6000_savres_routine_name (regno, sel);
25967 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
25968 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
25969 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
25972 return sym;
25975 /* Emit a sequence of insns, including a stack tie if needed, for
25976 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
25977 reset the stack pointer, but move the base of the frame into
25978 reg UPDT_REGNO for use by out-of-line register restore routines. */
25980 static rtx
25981 rs6000_emit_stack_reset (rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
25982 unsigned updt_regno)
25984 /* If there is nothing to do, don't do anything. */
25985 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
25986 return NULL_RTX;
25988 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
25990 /* This blockage is needed so that sched doesn't decide to move
25991 the sp change before the register restores. */
25992 if (DEFAULT_ABI == ABI_V4)
25993 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
25994 GEN_INT (frame_off)));
25996 /* If we are restoring registers out-of-line, we will be using the
25997 "exit" variants of the restore routines, which will reset the
25998 stack for us. But we do need to point updt_reg into the
25999 right place for those routines. */
26000 if (frame_off != 0)
26001 return emit_insn (gen_add3_insn (updt_reg_rtx,
26002 frame_reg_rtx, GEN_INT (frame_off)));
26003 else
26004 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
26006 return NULL_RTX;
26009 /* Return the register number used as a pointer by out-of-line
26010 save/restore functions. */
26012 static inline unsigned
26013 ptr_regno_for_savres (int sel)
26015 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
26016 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
26017 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
26020 /* Construct a parallel rtx describing the effect of a call to an
26021 out-of-line register save/restore routine, and emit the insn
26022 or jump_insn as appropriate. */
26024 static rtx_insn *
26025 rs6000_emit_savres_rtx (rs6000_stack_t *info,
26026 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
26027 machine_mode reg_mode, int sel)
26029 int i;
26030 int offset, start_reg, end_reg, n_regs, use_reg;
26031 int reg_size = GET_MODE_SIZE (reg_mode);
26032 rtx sym;
26033 rtvec p;
26034 rtx par;
26035 rtx_insn *insn;
26037 offset = 0;
26038 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
26039 ? info->first_gp_reg_save
26040 : (sel & SAVRES_REG) == SAVRES_FPR
26041 ? info->first_fp_reg_save
26042 : (sel & SAVRES_REG) == SAVRES_VR
26043 ? info->first_altivec_reg_save
26044 : -1);
26045 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
26046 ? 32
26047 : (sel & SAVRES_REG) == SAVRES_FPR
26048 ? 64
26049 : (sel & SAVRES_REG) == SAVRES_VR
26050 ? LAST_ALTIVEC_REGNO + 1
26051 : -1);
26052 n_regs = end_reg - start_reg;
26053 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
26054 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
26055 + n_regs);
26057 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
26058 RTVEC_ELT (p, offset++) = ret_rtx;
26060 RTVEC_ELT (p, offset++)
26061 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
26063 sym = rs6000_savres_routine_sym (info, sel);
26064 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
26066 use_reg = ptr_regno_for_savres (sel);
26067 if ((sel & SAVRES_REG) == SAVRES_VR)
26069 /* Vector regs are saved/restored using [reg+reg] addressing. */
26070 RTVEC_ELT (p, offset++)
26071 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
26072 RTVEC_ELT (p, offset++)
26073 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
26075 else
26076 RTVEC_ELT (p, offset++)
26077 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
26079 for (i = 0; i < end_reg - start_reg; i++)
26080 RTVEC_ELT (p, i + offset)
26081 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
26082 frame_reg_rtx, save_area_offset + reg_size * i,
26083 (sel & SAVRES_SAVE) != 0);
26085 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
26086 RTVEC_ELT (p, i + offset)
26087 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
26089 par = gen_rtx_PARALLEL (VOIDmode, p);
26091 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
26093 insn = emit_jump_insn (par);
26094 JUMP_LABEL (insn) = ret_rtx;
26096 else
26097 insn = emit_insn (par);
26098 return insn;
26101 /* Emit code to store CR fields that need to be saved into REG. */
26103 static void
26104 rs6000_emit_move_from_cr (rtx reg)
26106 /* Only the ELFv2 ABI allows storing only selected fields. */
26107 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
26109 int i, cr_reg[8], count = 0;
26111 /* Collect CR fields that must be saved. */
26112 for (i = 0; i < 8; i++)
26113 if (save_reg_p (CR0_REGNO + i))
26114 cr_reg[count++] = i;
26116 /* If it's just a single one, use mfcrf. */
26117 if (count == 1)
26119 rtvec p = rtvec_alloc (1);
26120 rtvec r = rtvec_alloc (2);
26121 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
26122 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
26123 RTVEC_ELT (p, 0)
26124 = gen_rtx_SET (reg,
26125 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
26127 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
26128 return;
26131 /* ??? It might be better to handle count == 2 / 3 cases here
26132 as well, using logical operations to combine the values. */
26135 emit_insn (gen_movesi_from_cr (reg));
26138 /* Return whether the split-stack arg pointer (r12) is used. */
26140 static bool
26141 split_stack_arg_pointer_used_p (void)
26143 /* If the pseudo holding the arg pointer is no longer a pseudo,
26144 then the arg pointer is used. */
26145 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
26146 && (!REG_P (cfun->machine->split_stack_arg_pointer)
26147 || (REGNO (cfun->machine->split_stack_arg_pointer)
26148 < FIRST_PSEUDO_REGISTER)))
26149 return true;
26151 /* Unfortunately we also need to do some code scanning, since
26152 r12 may have been substituted for the pseudo. */
26153 rtx_insn *insn;
26154 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
26155 FOR_BB_INSNS (bb, insn)
26156 if (NONDEBUG_INSN_P (insn))
26158 /* A call destroys r12. */
26159 if (CALL_P (insn))
26160 return false;
26162 df_ref use;
26163 FOR_EACH_INSN_USE (use, insn)
26165 rtx x = DF_REF_REG (use);
26166 if (REG_P (x) && REGNO (x) == 12)
26167 return true;
26169 df_ref def;
26170 FOR_EACH_INSN_DEF (def, insn)
26172 rtx x = DF_REF_REG (def);
26173 if (REG_P (x) && REGNO (x) == 12)
26174 return false;
26177 return bitmap_bit_p (DF_LR_OUT (bb), 12);
26180 /* Return whether we need to emit an ELFv2 global entry point prologue. */
26182 static bool
26183 rs6000_global_entry_point_needed_p (void)
26185 /* Only needed for the ELFv2 ABI. */
26186 if (DEFAULT_ABI != ABI_ELFv2)
26187 return false;
26189 /* With -msingle-pic-base, we assume the whole program shares the same
26190 TOC, so no global entry point prologues are needed anywhere. */
26191 if (TARGET_SINGLE_PIC_BASE)
26192 return false;
26194 /* Ensure we have a global entry point for thunks. ??? We could
26195 avoid that if the target routine doesn't need a global entry point,
26196 but we do not know whether this is the case at this point. */
26197 if (cfun->is_thunk)
26198 return true;
26200 /* For regular functions, rs6000_emit_prologue sets this flag if the
26201 routine ever uses the TOC pointer. */
26202 return cfun->machine->r2_setup_needed;
26205 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
26206 static sbitmap
26207 rs6000_get_separate_components (void)
26209 rs6000_stack_t *info = rs6000_stack_info ();
26211 if (WORLD_SAVE_P (info))
26212 return NULL;
26214 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
26215 && !(info->savres_strategy & REST_MULTIPLE));
26217 /* Component 0 is the save/restore of LR (done via GPR0).
26218 Components 13..31 are the save/restore of GPR13..GPR31.
26219 Components 46..63 are the save/restore of FPR14..FPR31. */
26221 cfun->machine->n_components = 64;
26223 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
26224 bitmap_clear (components);
26226 int reg_size = TARGET_32BIT ? 4 : 8;
26227 int fp_reg_size = 8;
26229 /* The GPRs we need saved to the frame. */
26230 if ((info->savres_strategy & SAVE_INLINE_GPRS)
26231 && (info->savres_strategy & REST_INLINE_GPRS))
26233 int offset = info->gp_save_offset;
26234 if (info->push_p)
26235 offset += info->total_size;
26237 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
26239 if (IN_RANGE (offset, -0x8000, 0x7fff)
26240 && save_reg_p (regno))
26241 bitmap_set_bit (components, regno);
26243 offset += reg_size;
26247 /* Don't mess with the hard frame pointer. */
26248 if (frame_pointer_needed)
26249 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
26251 /* Don't mess with the fixed TOC register. */
26252 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
26253 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
26254 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
26255 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
26257 /* The FPRs we need saved to the frame. */
26258 if ((info->savres_strategy & SAVE_INLINE_FPRS)
26259 && (info->savres_strategy & REST_INLINE_FPRS))
26261 int offset = info->fp_save_offset;
26262 if (info->push_p)
26263 offset += info->total_size;
26265 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
26267 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
26268 bitmap_set_bit (components, regno);
26270 offset += fp_reg_size;
26274 /* Optimize LR save and restore if we can. This is component 0. Any
26275 out-of-line register save/restore routines need LR. */
26276 if (info->lr_save_p
26277 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
26278 && (info->savres_strategy & SAVE_INLINE_GPRS)
26279 && (info->savres_strategy & REST_INLINE_GPRS)
26280 && (info->savres_strategy & SAVE_INLINE_FPRS)
26281 && (info->savres_strategy & REST_INLINE_FPRS)
26282 && (info->savres_strategy & SAVE_INLINE_VRS)
26283 && (info->savres_strategy & REST_INLINE_VRS))
26285 int offset = info->lr_save_offset;
26286 if (info->push_p)
26287 offset += info->total_size;
26288 if (IN_RANGE (offset, -0x8000, 0x7fff))
26289 bitmap_set_bit (components, 0);
26292 return components;
26295 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
26296 static sbitmap
26297 rs6000_components_for_bb (basic_block bb)
26299 rs6000_stack_t *info = rs6000_stack_info ();
26301 bitmap in = DF_LIVE_IN (bb);
26302 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
26303 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
26305 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
26306 bitmap_clear (components);
26308 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
26310 /* GPRs. */
26311 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
26312 if (bitmap_bit_p (in, regno)
26313 || bitmap_bit_p (gen, regno)
26314 || bitmap_bit_p (kill, regno))
26315 bitmap_set_bit (components, regno);
26317 /* FPRs. */
26318 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
26319 if (bitmap_bit_p (in, regno)
26320 || bitmap_bit_p (gen, regno)
26321 || bitmap_bit_p (kill, regno))
26322 bitmap_set_bit (components, regno);
26324 /* The link register. */
26325 if (bitmap_bit_p (in, LR_REGNO)
26326 || bitmap_bit_p (gen, LR_REGNO)
26327 || bitmap_bit_p (kill, LR_REGNO))
26328 bitmap_set_bit (components, 0);
26330 return components;
26333 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
26334 static void
26335 rs6000_disqualify_components (sbitmap components, edge e,
26336 sbitmap edge_components, bool /*is_prologue*/)
26338 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
26339 live where we want to place that code. */
26340 if (bitmap_bit_p (edge_components, 0)
26341 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
26343 if (dump_file)
26344 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
26345 "on entry to bb %d\n", e->dest->index);
26346 bitmap_clear_bit (components, 0);
26350 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
26351 static void
26352 rs6000_emit_prologue_components (sbitmap components)
26354 rs6000_stack_t *info = rs6000_stack_info ();
26355 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
26356 ? HARD_FRAME_POINTER_REGNUM
26357 : STACK_POINTER_REGNUM);
26359 machine_mode reg_mode = Pmode;
26360 int reg_size = TARGET_32BIT ? 4 : 8;
26361 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
26362 ? DFmode : SFmode;
26363 int fp_reg_size = 8;
26365 /* Prologue for LR. */
26366 if (bitmap_bit_p (components, 0))
26368 rtx reg = gen_rtx_REG (reg_mode, 0);
26369 rtx_insn *insn = emit_move_insn (reg, gen_rtx_REG (reg_mode, LR_REGNO));
26370 RTX_FRAME_RELATED_P (insn) = 1;
26371 add_reg_note (insn, REG_CFA_REGISTER, NULL);
26373 int offset = info->lr_save_offset;
26374 if (info->push_p)
26375 offset += info->total_size;
26377 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
26378 RTX_FRAME_RELATED_P (insn) = 1;
26379 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
26380 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
26381 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
26384 /* Prologue for the GPRs. */
26385 int offset = info->gp_save_offset;
26386 if (info->push_p)
26387 offset += info->total_size;
26389 for (int i = info->first_gp_reg_save; i < 32; i++)
26391 if (bitmap_bit_p (components, i))
26393 rtx reg = gen_rtx_REG (reg_mode, i);
26394 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
26395 RTX_FRAME_RELATED_P (insn) = 1;
26396 rtx set = copy_rtx (single_set (insn));
26397 add_reg_note (insn, REG_CFA_OFFSET, set);
26400 offset += reg_size;
26403 /* Prologue for the FPRs. */
26404 offset = info->fp_save_offset;
26405 if (info->push_p)
26406 offset += info->total_size;
26408 for (int i = info->first_fp_reg_save; i < 64; i++)
26410 if (bitmap_bit_p (components, i))
26412 rtx reg = gen_rtx_REG (fp_reg_mode, i);
26413 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
26414 RTX_FRAME_RELATED_P (insn) = 1;
26415 rtx set = copy_rtx (single_set (insn));
26416 add_reg_note (insn, REG_CFA_OFFSET, set);
26419 offset += fp_reg_size;
26423 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
26424 static void
26425 rs6000_emit_epilogue_components (sbitmap components)
26427 rs6000_stack_t *info = rs6000_stack_info ();
26428 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
26429 ? HARD_FRAME_POINTER_REGNUM
26430 : STACK_POINTER_REGNUM);
26432 machine_mode reg_mode = Pmode;
26433 int reg_size = TARGET_32BIT ? 4 : 8;
26435 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
26436 ? DFmode : SFmode;
26437 int fp_reg_size = 8;
26439 /* Epilogue for the FPRs. */
26440 int offset = info->fp_save_offset;
26441 if (info->push_p)
26442 offset += info->total_size;
26444 for (int i = info->first_fp_reg_save; i < 64; i++)
26446 if (bitmap_bit_p (components, i))
26448 rtx reg = gen_rtx_REG (fp_reg_mode, i);
26449 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
26450 RTX_FRAME_RELATED_P (insn) = 1;
26451 add_reg_note (insn, REG_CFA_RESTORE, reg);
26454 offset += fp_reg_size;
26457 /* Epilogue for the GPRs. */
26458 offset = info->gp_save_offset;
26459 if (info->push_p)
26460 offset += info->total_size;
26462 for (int i = info->first_gp_reg_save; i < 32; i++)
26464 if (bitmap_bit_p (components, i))
26466 rtx reg = gen_rtx_REG (reg_mode, i);
26467 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
26468 RTX_FRAME_RELATED_P (insn) = 1;
26469 add_reg_note (insn, REG_CFA_RESTORE, reg);
26472 offset += reg_size;
26475 /* Epilogue for LR. */
26476 if (bitmap_bit_p (components, 0))
26478 int offset = info->lr_save_offset;
26479 if (info->push_p)
26480 offset += info->total_size;
26482 rtx reg = gen_rtx_REG (reg_mode, 0);
26483 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
26485 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
26486 insn = emit_move_insn (lr, reg);
26487 RTX_FRAME_RELATED_P (insn) = 1;
26488 add_reg_note (insn, REG_CFA_RESTORE, lr);
26492 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
26493 static void
26494 rs6000_set_handled_components (sbitmap components)
26496 rs6000_stack_t *info = rs6000_stack_info ();
26498 for (int i = info->first_gp_reg_save; i < 32; i++)
26499 if (bitmap_bit_p (components, i))
26500 cfun->machine->gpr_is_wrapped_separately[i] = true;
26502 for (int i = info->first_fp_reg_save; i < 64; i++)
26503 if (bitmap_bit_p (components, i))
26504 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
26506 if (bitmap_bit_p (components, 0))
26507 cfun->machine->lr_is_wrapped_separately = true;
26510 /* VRSAVE is a bit vector representing which AltiVec registers
26511 are used. The OS uses this to determine which vector
26512 registers to save on a context switch. We need to save
26513 VRSAVE on the stack frame, add whatever AltiVec registers we
26514 used in this function, and do the corresponding magic in the
26515 epilogue. */
26516 static void
26517 emit_vrsave_prologue (rs6000_stack_t *info, int save_regno,
26518 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
26520 /* Get VRSAVE into a GPR. */
26521 rtx reg = gen_rtx_REG (SImode, save_regno);
26522 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
26523 if (TARGET_MACHO)
26524 emit_insn (gen_get_vrsave_internal (reg));
26525 else
26526 emit_insn (gen_rtx_SET (reg, vrsave));
26528 /* Save VRSAVE. */
26529 int offset = info->vrsave_save_offset + frame_off;
26530 emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
26532 /* Include the registers in the mask. */
26533 emit_insn (gen_iorsi3 (reg, reg, GEN_INT (info->vrsave_mask)));
26535 emit_insn (generate_set_vrsave (reg, info, 0));
26538 /* Set up the arg pointer (r12) for -fsplit-stack code. If __morestack was
26539 called, it left the arg pointer to the old stack in r29. Otherwise, the
26540 arg pointer is the top of the current frame. */
26541 static void
26542 emit_split_stack_prologue (rs6000_stack_t *info, rtx_insn *sp_adjust,
26543 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
26545 cfun->machine->split_stack_argp_used = true;
26547 if (sp_adjust)
26549 rtx r12 = gen_rtx_REG (Pmode, 12);
26550 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
26551 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
26552 emit_insn_before (set_r12, sp_adjust);
26554 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
26556 rtx r12 = gen_rtx_REG (Pmode, 12);
26557 if (frame_off == 0)
26558 emit_move_insn (r12, frame_reg_rtx);
26559 else
26560 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
26563 if (info->push_p)
26565 rtx r12 = gen_rtx_REG (Pmode, 12);
26566 rtx r29 = gen_rtx_REG (Pmode, 29);
26567 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
26568 rtx not_more = gen_label_rtx ();
26569 rtx jump;
26571 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
26572 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
26573 gen_rtx_LABEL_REF (VOIDmode, not_more),
26574 pc_rtx);
26575 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
26576 JUMP_LABEL (jump) = not_more;
26577 LABEL_NUSES (not_more) += 1;
26578 emit_move_insn (r12, r29);
26579 emit_label (not_more);
26583 /* Emit function prologue as insns. */
26585 void
26586 rs6000_emit_prologue (void)
26588 rs6000_stack_t *info = rs6000_stack_info ();
26589 machine_mode reg_mode = Pmode;
26590 int reg_size = TARGET_32BIT ? 4 : 8;
26591 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
26592 ? DFmode : SFmode;
26593 int fp_reg_size = 8;
26594 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
26595 rtx frame_reg_rtx = sp_reg_rtx;
26596 unsigned int cr_save_regno;
26597 rtx cr_save_rtx = NULL_RTX;
26598 rtx_insn *insn;
26599 int strategy;
26600 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
26601 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
26602 && call_used_regs[STATIC_CHAIN_REGNUM]);
26603 int using_split_stack = (flag_split_stack
26604 && (lookup_attribute ("no_split_stack",
26605 DECL_ATTRIBUTES (cfun->decl))
26606 == NULL));
26608 /* Offset to top of frame for frame_reg and sp respectively. */
26609 HOST_WIDE_INT frame_off = 0;
26610 HOST_WIDE_INT sp_off = 0;
26611 /* sp_adjust is the stack adjusting instruction, tracked so that the
26612 insn setting up the split-stack arg pointer can be emitted just
26613 prior to it, when r12 is not used here for other purposes. */
26614 rtx_insn *sp_adjust = 0;
26616 #if CHECKING_P
26617 /* Track and check usage of r0, r11, r12. */
26618 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
26619 #define START_USE(R) do \
26621 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
26622 reg_inuse |= 1 << (R); \
26623 } while (0)
26624 #define END_USE(R) do \
26626 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
26627 reg_inuse &= ~(1 << (R)); \
26628 } while (0)
26629 #define NOT_INUSE(R) do \
26631 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
26632 } while (0)
26633 #else
26634 #define START_USE(R) do {} while (0)
26635 #define END_USE(R) do {} while (0)
26636 #define NOT_INUSE(R) do {} while (0)
26637 #endif
26639 if (DEFAULT_ABI == ABI_ELFv2
26640 && !TARGET_SINGLE_PIC_BASE)
26642 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
26644 /* With -mminimal-toc we may generate an extra use of r2 below. */
26645 if (TARGET_TOC && TARGET_MINIMAL_TOC
26646 && !constant_pool_empty_p ())
26647 cfun->machine->r2_setup_needed = true;
26651 if (flag_stack_usage_info)
26652 current_function_static_stack_size = info->total_size;
26654 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
26656 HOST_WIDE_INT size = info->total_size;
26658 if (crtl->is_leaf && !cfun->calls_alloca)
26660 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
26661 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT,
26662 size - STACK_CHECK_PROTECT);
26664 else if (size > 0)
26665 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
26668 if (TARGET_FIX_AND_CONTINUE)
26670 /* gdb on darwin arranges to forward a function from the old
26671 address by modifying the first 5 instructions of the function
26672 to branch to the overriding function. This is necessary to
26673 permit function pointers that point to the old function to
26674 actually forward to the new function. */
26675 emit_insn (gen_nop ());
26676 emit_insn (gen_nop ());
26677 emit_insn (gen_nop ());
26678 emit_insn (gen_nop ());
26679 emit_insn (gen_nop ());
26682 /* Handle world saves specially here. */
26683 if (WORLD_SAVE_P (info))
26685 int i, j, sz;
26686 rtx treg;
26687 rtvec p;
26688 rtx reg0;
26690 /* save_world expects lr in r0. */
26691 reg0 = gen_rtx_REG (Pmode, 0);
26692 if (info->lr_save_p)
26694 insn = emit_move_insn (reg0,
26695 gen_rtx_REG (Pmode, LR_REGNO));
26696 RTX_FRAME_RELATED_P (insn) = 1;
26699 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
26700 assumptions about the offsets of various bits of the stack
26701 frame. */
26702 gcc_assert (info->gp_save_offset == -220
26703 && info->fp_save_offset == -144
26704 && info->lr_save_offset == 8
26705 && info->cr_save_offset == 4
26706 && info->push_p
26707 && info->lr_save_p
26708 && (!crtl->calls_eh_return
26709 || info->ehrd_offset == -432)
26710 && info->vrsave_save_offset == -224
26711 && info->altivec_save_offset == -416);
26713 treg = gen_rtx_REG (SImode, 11);
26714 emit_move_insn (treg, GEN_INT (-info->total_size));
26716 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
26717 in R11. It also clobbers R12, so beware! */
26719 /* Preserve CR2 for save_world prologues */
26720 sz = 5;
26721 sz += 32 - info->first_gp_reg_save;
26722 sz += 64 - info->first_fp_reg_save;
26723 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
26724 p = rtvec_alloc (sz);
26725 j = 0;
26726 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
26727 gen_rtx_REG (SImode,
26728 LR_REGNO));
26729 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
26730 gen_rtx_SYMBOL_REF (Pmode,
26731 "*save_world"));
26732 /* We do floats first so that the instruction pattern matches
26733 properly. */
26734 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
26735 RTVEC_ELT (p, j++)
26736 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
26737 ? DFmode : SFmode,
26738 info->first_fp_reg_save + i),
26739 frame_reg_rtx,
26740 info->fp_save_offset + frame_off + 8 * i);
26741 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
26742 RTVEC_ELT (p, j++)
26743 = gen_frame_store (gen_rtx_REG (V4SImode,
26744 info->first_altivec_reg_save + i),
26745 frame_reg_rtx,
26746 info->altivec_save_offset + frame_off + 16 * i);
26747 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
26748 RTVEC_ELT (p, j++)
26749 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
26750 frame_reg_rtx,
26751 info->gp_save_offset + frame_off + reg_size * i);
26753 /* CR register traditionally saved as CR2. */
26754 RTVEC_ELT (p, j++)
26755 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
26756 frame_reg_rtx, info->cr_save_offset + frame_off);
26757 /* Explain about use of R0. */
26758 if (info->lr_save_p)
26759 RTVEC_ELT (p, j++)
26760 = gen_frame_store (reg0,
26761 frame_reg_rtx, info->lr_save_offset + frame_off);
26762 /* Explain what happens to the stack pointer. */
26764 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
26765 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
26768 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
26769 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
26770 treg, GEN_INT (-info->total_size));
26771 sp_off = frame_off = info->total_size;
26774 strategy = info->savres_strategy;
26776 /* For V.4, update stack before we do any saving and set back pointer. */
26777 if (! WORLD_SAVE_P (info)
26778 && info->push_p
26779 && (DEFAULT_ABI == ABI_V4
26780 || crtl->calls_eh_return))
26782 bool need_r11 = (!(strategy & SAVE_INLINE_FPRS)
26783 || !(strategy & SAVE_INLINE_GPRS)
26784 || !(strategy & SAVE_INLINE_VRS));
26785 int ptr_regno = -1;
26786 rtx ptr_reg = NULL_RTX;
26787 int ptr_off = 0;
26789 if (info->total_size < 32767)
26790 frame_off = info->total_size;
26791 else if (need_r11)
26792 ptr_regno = 11;
26793 else if (info->cr_save_p
26794 || info->lr_save_p
26795 || info->first_fp_reg_save < 64
26796 || info->first_gp_reg_save < 32
26797 || info->altivec_size != 0
26798 || info->vrsave_size != 0
26799 || crtl->calls_eh_return)
26800 ptr_regno = 12;
26801 else
26803 /* The prologue won't be saving any regs so there is no need
26804 to set up a frame register to access any frame save area.
26805 We also won't be using frame_off anywhere below, but set
26806 the correct value anyway to protect against future
26807 changes to this function. */
26808 frame_off = info->total_size;
26810 if (ptr_regno != -1)
26812 /* Set up the frame offset to that needed by the first
26813 out-of-line save function. */
26814 START_USE (ptr_regno);
26815 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
26816 frame_reg_rtx = ptr_reg;
26817 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
26818 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
26819 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
26820 ptr_off = info->gp_save_offset + info->gp_size;
26821 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
26822 ptr_off = info->altivec_save_offset + info->altivec_size;
26823 frame_off = -ptr_off;
26825 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
26826 ptr_reg, ptr_off);
26827 if (REGNO (frame_reg_rtx) == 12)
26828 sp_adjust = 0;
26829 sp_off = info->total_size;
26830 if (frame_reg_rtx != sp_reg_rtx)
26831 rs6000_emit_stack_tie (frame_reg_rtx, false);
26834 /* If we use the link register, get it into r0. */
26835 if (!WORLD_SAVE_P (info) && info->lr_save_p
26836 && !cfun->machine->lr_is_wrapped_separately)
26838 rtx addr, reg, mem;
26840 reg = gen_rtx_REG (Pmode, 0);
26841 START_USE (0);
26842 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
26843 RTX_FRAME_RELATED_P (insn) = 1;
26845 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
26846 | SAVE_NOINLINE_FPRS_SAVES_LR)))
26848 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
26849 GEN_INT (info->lr_save_offset + frame_off));
26850 mem = gen_rtx_MEM (Pmode, addr);
26851 /* This should not be of rs6000_sr_alias_set, because of
26852 __builtin_return_address. */
26854 insn = emit_move_insn (mem, reg);
26855 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
26856 NULL_RTX, NULL_RTX);
26857 END_USE (0);
26861 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
26862 r12 will be needed by out-of-line gpr restore. */
26863 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
26864 && !(strategy & (SAVE_INLINE_GPRS
26865 | SAVE_NOINLINE_GPRS_SAVES_LR))
26866 ? 11 : 12);
26867 if (!WORLD_SAVE_P (info)
26868 && info->cr_save_p
26869 && REGNO (frame_reg_rtx) != cr_save_regno
26870 && !(using_static_chain_p && cr_save_regno == 11)
26871 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
26873 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
26874 START_USE (cr_save_regno);
26875 rs6000_emit_move_from_cr (cr_save_rtx);
26878 /* Do any required saving of fpr's. If only one or two to save, do
26879 it ourselves. Otherwise, call function. */
26880 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
26882 int offset = info->fp_save_offset + frame_off;
26883 for (int i = info->first_fp_reg_save; i < 64; i++)
26885 if (save_reg_p (i)
26886 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
26887 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
26888 sp_off - frame_off);
26890 offset += fp_reg_size;
26893 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
26895 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
26896 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
26897 unsigned ptr_regno = ptr_regno_for_savres (sel);
26898 rtx ptr_reg = frame_reg_rtx;
26900 if (REGNO (frame_reg_rtx) == ptr_regno)
26901 gcc_checking_assert (frame_off == 0);
26902 else
26904 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
26905 NOT_INUSE (ptr_regno);
26906 emit_insn (gen_add3_insn (ptr_reg,
26907 frame_reg_rtx, GEN_INT (frame_off)));
26909 insn = rs6000_emit_savres_rtx (info, ptr_reg,
26910 info->fp_save_offset,
26911 info->lr_save_offset,
26912 DFmode, sel);
26913 rs6000_frame_related (insn, ptr_reg, sp_off,
26914 NULL_RTX, NULL_RTX);
26915 if (lr)
26916 END_USE (0);
26919 /* Save GPRs. This is done as a PARALLEL if we are using
26920 the store-multiple instructions. */
26921 if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
26923 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
26924 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
26925 unsigned ptr_regno = ptr_regno_for_savres (sel);
26926 rtx ptr_reg = frame_reg_rtx;
26927 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
26928 int end_save = info->gp_save_offset + info->gp_size;
26929 int ptr_off;
26931 if (ptr_regno == 12)
26932 sp_adjust = 0;
26933 if (!ptr_set_up)
26934 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
26936 /* Need to adjust r11 (r12) if we saved any FPRs. */
26937 if (end_save + frame_off != 0)
26939 rtx offset = GEN_INT (end_save + frame_off);
26941 if (ptr_set_up)
26942 frame_off = -end_save;
26943 else
26944 NOT_INUSE (ptr_regno);
26945 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
26947 else if (!ptr_set_up)
26949 NOT_INUSE (ptr_regno);
26950 emit_move_insn (ptr_reg, frame_reg_rtx);
26952 ptr_off = -end_save;
26953 insn = rs6000_emit_savres_rtx (info, ptr_reg,
26954 info->gp_save_offset + ptr_off,
26955 info->lr_save_offset + ptr_off,
26956 reg_mode, sel);
26957 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
26958 NULL_RTX, NULL_RTX);
26959 if (lr)
26960 END_USE (0);
26962 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
26964 rtvec p;
26965 int i;
26966 p = rtvec_alloc (32 - info->first_gp_reg_save);
26967 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
26968 RTVEC_ELT (p, i)
26969 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
26970 frame_reg_rtx,
26971 info->gp_save_offset + frame_off + reg_size * i);
26972 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
26973 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
26974 NULL_RTX, NULL_RTX);
26976 else if (!WORLD_SAVE_P (info))
26978 int offset = info->gp_save_offset + frame_off;
26979 for (int i = info->first_gp_reg_save; i < 32; i++)
26981 if (save_reg_p (i)
26982 && !cfun->machine->gpr_is_wrapped_separately[i])
26983 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
26984 sp_off - frame_off);
26986 offset += reg_size;
26990 if (crtl->calls_eh_return)
26992 unsigned int i;
26993 rtvec p;
26995 for (i = 0; ; ++i)
26997 unsigned int regno = EH_RETURN_DATA_REGNO (i);
26998 if (regno == INVALID_REGNUM)
26999 break;
27002 p = rtvec_alloc (i);
27004 for (i = 0; ; ++i)
27006 unsigned int regno = EH_RETURN_DATA_REGNO (i);
27007 if (regno == INVALID_REGNUM)
27008 break;
27010 rtx set
27011 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
27012 sp_reg_rtx,
27013 info->ehrd_offset + sp_off + reg_size * (int) i);
27014 RTVEC_ELT (p, i) = set;
27015 RTX_FRAME_RELATED_P (set) = 1;
27018 insn = emit_insn (gen_blockage ());
27019 RTX_FRAME_RELATED_P (insn) = 1;
27020 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
27023 /* In AIX ABI we need to make sure r2 is really saved. */
27024 if (TARGET_AIX && crtl->calls_eh_return)
27026 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
27027 rtx join_insn, note;
27028 rtx_insn *save_insn;
27029 long toc_restore_insn;
27031 tmp_reg = gen_rtx_REG (Pmode, 11);
27032 tmp_reg_si = gen_rtx_REG (SImode, 11);
27033 if (using_static_chain_p)
27035 START_USE (0);
27036 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
27038 else
27039 START_USE (11);
27040 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
27041 /* Peek at instruction to which this function returns. If it's
27042 restoring r2, then we know we've already saved r2. We can't
27043 unconditionally save r2 because the value we have will already
27044 be updated if we arrived at this function via a plt call or
27045 toc adjusting stub. */
27046 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
27047 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
27048 + RS6000_TOC_SAVE_SLOT);
27049 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
27050 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
27051 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
27052 validate_condition_mode (EQ, CCUNSmode);
27053 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
27054 emit_insn (gen_rtx_SET (compare_result,
27055 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
27056 toc_save_done = gen_label_rtx ();
27057 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
27058 gen_rtx_EQ (VOIDmode, compare_result,
27059 const0_rtx),
27060 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
27061 pc_rtx);
27062 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
27063 JUMP_LABEL (jump) = toc_save_done;
27064 LABEL_NUSES (toc_save_done) += 1;
27066 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
27067 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
27068 sp_off - frame_off);
27070 emit_label (toc_save_done);
27072 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
27073 have a CFG that has different saves along different paths.
27074 Move the note to a dummy blockage insn, which describes that
27075 R2 is unconditionally saved after the label. */
27076 /* ??? An alternate representation might be a special insn pattern
27077 containing both the branch and the store. That might let the
27078 code that minimizes the number of DW_CFA_advance opcodes better
27079 freedom in placing the annotations. */
27080 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
27081 if (note)
27082 remove_note (save_insn, note);
27083 else
27084 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
27085 copy_rtx (PATTERN (save_insn)), NULL_RTX);
27086 RTX_FRAME_RELATED_P (save_insn) = 0;
27088 join_insn = emit_insn (gen_blockage ());
27089 REG_NOTES (join_insn) = note;
27090 RTX_FRAME_RELATED_P (join_insn) = 1;
27092 if (using_static_chain_p)
27094 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
27095 END_USE (0);
27097 else
27098 END_USE (11);
27101 /* Save CR if we use any that must be preserved. */
27102 if (!WORLD_SAVE_P (info) && info->cr_save_p)
27104 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
27105 GEN_INT (info->cr_save_offset + frame_off));
27106 rtx mem = gen_frame_mem (SImode, addr);
27108 /* If we didn't copy cr before, do so now using r0. */
27109 if (cr_save_rtx == NULL_RTX)
27111 START_USE (0);
27112 cr_save_rtx = gen_rtx_REG (SImode, 0);
27113 rs6000_emit_move_from_cr (cr_save_rtx);
27116 /* Saving CR requires a two-instruction sequence: one instruction
27117 to move the CR to a general-purpose register, and a second
27118 instruction that stores the GPR to memory.
27120 We do not emit any DWARF CFI records for the first of these,
27121 because we cannot properly represent the fact that CR is saved in
27122 a register. One reason is that we cannot express that multiple
27123 CR fields are saved; another reason is that on 64-bit, the size
27124 of the CR register in DWARF (4 bytes) differs from the size of
27125 a general-purpose register.
27127 This means if any intervening instruction were to clobber one of
27128 the call-saved CR fields, we'd have incorrect CFI. To prevent
27129 this from happening, we mark the store to memory as a use of
27130 those CR fields, which prevents any such instruction from being
27131 scheduled in between the two instructions. */
27132 rtx crsave_v[9];
27133 int n_crsave = 0;
27134 int i;
27136 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
27137 for (i = 0; i < 8; i++)
27138 if (save_reg_p (CR0_REGNO + i))
27139 crsave_v[n_crsave++]
27140 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
27142 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
27143 gen_rtvec_v (n_crsave, crsave_v)));
27144 END_USE (REGNO (cr_save_rtx));
27146 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
27147 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
27148 so we need to construct a frame expression manually. */
27149 RTX_FRAME_RELATED_P (insn) = 1;
27151 /* Update address to be stack-pointer relative, like
27152 rs6000_frame_related would do. */
27153 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
27154 GEN_INT (info->cr_save_offset + sp_off));
27155 mem = gen_frame_mem (SImode, addr);
27157 if (DEFAULT_ABI == ABI_ELFv2)
27159 /* In the ELFv2 ABI we generate separate CFI records for each
27160 CR field that was actually saved. They all point to the
27161 same 32-bit stack slot. */
27162 rtx crframe[8];
27163 int n_crframe = 0;
27165 for (i = 0; i < 8; i++)
27166 if (save_reg_p (CR0_REGNO + i))
27168 crframe[n_crframe]
27169 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
27171 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
27172 n_crframe++;
27175 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
27176 gen_rtx_PARALLEL (VOIDmode,
27177 gen_rtvec_v (n_crframe, crframe)));
27179 else
27181 /* In other ABIs, by convention, we use a single CR regnum to
27182 represent the fact that all call-saved CR fields are saved.
27183 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
27184 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
27185 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
27189 /* In the ELFv2 ABI we need to save all call-saved CR fields into
27190 *separate* slots if the routine calls __builtin_eh_return, so
27191 that they can be independently restored by the unwinder. */
27192 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
27194 int i, cr_off = info->ehcr_offset;
27195 rtx crsave;
27197 /* ??? We might get better performance by using multiple mfocrf
27198 instructions. */
27199 crsave = gen_rtx_REG (SImode, 0);
27200 emit_insn (gen_movesi_from_cr (crsave));
27202 for (i = 0; i < 8; i++)
27203 if (!call_used_regs[CR0_REGNO + i])
27205 rtvec p = rtvec_alloc (2);
27206 RTVEC_ELT (p, 0)
27207 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
27208 RTVEC_ELT (p, 1)
27209 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
27211 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
27213 RTX_FRAME_RELATED_P (insn) = 1;
27214 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
27215 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
27216 sp_reg_rtx, cr_off + sp_off));
27218 cr_off += reg_size;
27222 /* Update stack and set back pointer unless this is V.4,
27223 for which it was done previously. */
27224 if (!WORLD_SAVE_P (info) && info->push_p
27225 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
27227 rtx ptr_reg = NULL;
27228 int ptr_off = 0;
27230 /* If saving altivec regs we need to be able to address all save
27231 locations using a 16-bit offset. */
27232 if ((strategy & SAVE_INLINE_VRS) == 0
27233 || (info->altivec_size != 0
27234 && (info->altivec_save_offset + info->altivec_size - 16
27235 + info->total_size - frame_off) > 32767)
27236 || (info->vrsave_size != 0
27237 && (info->vrsave_save_offset
27238 + info->total_size - frame_off) > 32767))
27240 int sel = SAVRES_SAVE | SAVRES_VR;
27241 unsigned ptr_regno = ptr_regno_for_savres (sel);
27243 if (using_static_chain_p
27244 && ptr_regno == STATIC_CHAIN_REGNUM)
27245 ptr_regno = 12;
27246 if (REGNO (frame_reg_rtx) != ptr_regno)
27247 START_USE (ptr_regno);
27248 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
27249 frame_reg_rtx = ptr_reg;
27250 ptr_off = info->altivec_save_offset + info->altivec_size;
27251 frame_off = -ptr_off;
27253 else if (REGNO (frame_reg_rtx) == 1)
27254 frame_off = info->total_size;
27255 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
27256 ptr_reg, ptr_off);
27257 if (REGNO (frame_reg_rtx) == 12)
27258 sp_adjust = 0;
27259 sp_off = info->total_size;
27260 if (frame_reg_rtx != sp_reg_rtx)
27261 rs6000_emit_stack_tie (frame_reg_rtx, false);
27264 /* Set frame pointer, if needed. */
27265 if (frame_pointer_needed)
27267 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
27268 sp_reg_rtx);
27269 RTX_FRAME_RELATED_P (insn) = 1;
27272 /* Save AltiVec registers if needed. Save here because the red zone does
27273 not always include AltiVec registers. */
27274 if (!WORLD_SAVE_P (info)
27275 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
27277 int end_save = info->altivec_save_offset + info->altivec_size;
27278 int ptr_off;
27279 /* Oddly, the vector save/restore functions point r0 at the end
27280 of the save area, then use r11 or r12 to load offsets for
27281 [reg+reg] addressing. */
27282 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
27283 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
27284 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
27286 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
27287 NOT_INUSE (0);
27288 if (scratch_regno == 12)
27289 sp_adjust = 0;
27290 if (end_save + frame_off != 0)
27292 rtx offset = GEN_INT (end_save + frame_off);
27294 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
27296 else
27297 emit_move_insn (ptr_reg, frame_reg_rtx);
27299 ptr_off = -end_save;
27300 insn = rs6000_emit_savres_rtx (info, scratch_reg,
27301 info->altivec_save_offset + ptr_off,
27302 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
27303 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
27304 NULL_RTX, NULL_RTX);
27305 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
27307 /* The oddity mentioned above clobbered our frame reg. */
27308 emit_move_insn (frame_reg_rtx, ptr_reg);
27309 frame_off = ptr_off;
27312 else if (!WORLD_SAVE_P (info)
27313 && info->altivec_size != 0)
27315 int i;
27317 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
27318 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
27320 rtx areg, savereg, mem;
27321 HOST_WIDE_INT offset;
27323 offset = (info->altivec_save_offset + frame_off
27324 + 16 * (i - info->first_altivec_reg_save));
27326 savereg = gen_rtx_REG (V4SImode, i);
27328 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
27330 mem = gen_frame_mem (V4SImode,
27331 gen_rtx_PLUS (Pmode, frame_reg_rtx,
27332 GEN_INT (offset)));
27333 insn = emit_insn (gen_rtx_SET (mem, savereg));
27334 areg = NULL_RTX;
27336 else
27338 NOT_INUSE (0);
27339 areg = gen_rtx_REG (Pmode, 0);
27340 emit_move_insn (areg, GEN_INT (offset));
27342 /* AltiVec addressing mode is [reg+reg]. */
27343 mem = gen_frame_mem (V4SImode,
27344 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
27346 /* Rather than emitting a generic move, force use of the stvx
27347 instruction, which we always want on ISA 2.07 (power8) systems.
27348 In particular we don't want xxpermdi/stxvd2x for little
27349 endian. */
27350 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
27353 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
27354 areg, GEN_INT (offset));
27358 /* VRSAVE is a bit vector representing which AltiVec registers
27359 are used. The OS uses this to determine which vector
27360 registers to save on a context switch. We need to save
27361 VRSAVE on the stack frame, add whatever AltiVec registers we
27362 used in this function, and do the corresponding magic in the
27363 epilogue. */
27365 if (!WORLD_SAVE_P (info) && info->vrsave_size != 0)
27367 /* Get VRSAVE into a GPR. Note that ABI_V4 and ABI_DARWIN might
27368 be using r12 as frame_reg_rtx and r11 as the static chain
27369 pointer for nested functions. */
27370 int save_regno = 12;
27371 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
27372 && !using_static_chain_p)
27373 save_regno = 11;
27374 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
27376 save_regno = 11;
27377 if (using_static_chain_p)
27378 save_regno = 0;
27380 NOT_INUSE (save_regno);
27382 emit_vrsave_prologue (info, save_regno, frame_off, frame_reg_rtx);
27385 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
27386 if (!TARGET_SINGLE_PIC_BASE
27387 && ((TARGET_TOC && TARGET_MINIMAL_TOC
27388 && !constant_pool_empty_p ())
27389 || (DEFAULT_ABI == ABI_V4
27390 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
27391 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
27393 /* If emit_load_toc_table will use the link register, we need to save
27394 it. We use R12 for this purpose because emit_load_toc_table
27395 can use register 0. This allows us to use a plain 'blr' to return
27396 from the procedure more often. */
27397 int save_LR_around_toc_setup = (TARGET_ELF
27398 && DEFAULT_ABI == ABI_V4
27399 && flag_pic
27400 && ! info->lr_save_p
27401 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
27402 if (save_LR_around_toc_setup)
27404 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
27405 rtx tmp = gen_rtx_REG (Pmode, 12);
27407 sp_adjust = 0;
27408 insn = emit_move_insn (tmp, lr);
27409 RTX_FRAME_RELATED_P (insn) = 1;
27411 rs6000_emit_load_toc_table (TRUE);
27413 insn = emit_move_insn (lr, tmp);
27414 add_reg_note (insn, REG_CFA_RESTORE, lr);
27415 RTX_FRAME_RELATED_P (insn) = 1;
27417 else
27418 rs6000_emit_load_toc_table (TRUE);
27421 #if TARGET_MACHO
27422 if (!TARGET_SINGLE_PIC_BASE
27423 && DEFAULT_ABI == ABI_DARWIN
27424 && flag_pic && crtl->uses_pic_offset_table)
27426 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
27427 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
27429 /* Save and restore LR locally around this call (in R0). */
27430 if (!info->lr_save_p)
27431 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
27433 emit_insn (gen_load_macho_picbase (src));
27435 emit_move_insn (gen_rtx_REG (Pmode,
27436 RS6000_PIC_OFFSET_TABLE_REGNUM),
27437 lr);
27439 if (!info->lr_save_p)
27440 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
27442 #endif
27444 /* If we need to, save the TOC register after doing the stack setup.
27445 Do not emit eh frame info for this save. The unwinder wants info,
27446 conceptually attached to instructions in this function, about
27447 register values in the caller of this function. This R2 may have
27448 already been changed from the value in the caller.
27449 We don't attempt to write accurate DWARF EH frame info for R2
27450 because code emitted by gcc for a (non-pointer) function call
27451 doesn't save and restore R2. Instead, R2 is managed out-of-line
27452 by a linker generated plt call stub when the function resides in
27453 a shared library. This behavior is costly to describe in DWARF,
27454 both in terms of the size of DWARF info and the time taken in the
27455 unwinder to interpret it. R2 changes, apart from the
27456 calls_eh_return case earlier in this function, are handled by
27457 linux-unwind.h frob_update_context. */
27458 if (rs6000_save_toc_in_prologue_p ())
27460 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
27461 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
27464 /* Set up the arg pointer (r12) for -fsplit-stack code. */
27465 if (using_split_stack && split_stack_arg_pointer_used_p ())
27466 emit_split_stack_prologue (info, sp_adjust, frame_off, frame_reg_rtx);
27469 /* Output .extern statements for the save/restore routines we use. */
27471 static void
27472 rs6000_output_savres_externs (FILE *file)
27474 rs6000_stack_t *info = rs6000_stack_info ();
27476 if (TARGET_DEBUG_STACK)
27477 debug_stack_info (info);
27479 /* Write .extern for any function we will call to save and restore
27480 fp values. */
27481 if (info->first_fp_reg_save < 64
27482 && !TARGET_MACHO
27483 && !TARGET_ELF)
27485 char *name;
27486 int regno = info->first_fp_reg_save - 32;
27488 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
27490 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
27491 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
27492 name = rs6000_savres_routine_name (regno, sel);
27493 fprintf (file, "\t.extern %s\n", name);
27495 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
27497 bool lr = (info->savres_strategy
27498 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
27499 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
27500 name = rs6000_savres_routine_name (regno, sel);
27501 fprintf (file, "\t.extern %s\n", name);
27506 /* Write function prologue. */
27508 static void
27509 rs6000_output_function_prologue (FILE *file)
27511 if (!cfun->is_thunk)
27512 rs6000_output_savres_externs (file);
27514 /* ELFv2 ABI r2 setup code and local entry point. This must follow
27515 immediately after the global entry point label. */
27516 if (rs6000_global_entry_point_needed_p ())
27518 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
27520 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
27522 if (TARGET_CMODEL != CMODEL_LARGE)
27524 /* In the small and medium code models, we assume the TOC is less
27525 2 GB away from the text section, so it can be computed via the
27526 following two-instruction sequence. */
27527 char buf[256];
27529 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27530 fprintf (file, "0:\taddis 2,12,.TOC.-");
27531 assemble_name (file, buf);
27532 fprintf (file, "@ha\n");
27533 fprintf (file, "\taddi 2,2,.TOC.-");
27534 assemble_name (file, buf);
27535 fprintf (file, "@l\n");
27537 else
27539 /* In the large code model, we allow arbitrary offsets between the
27540 TOC and the text section, so we have to load the offset from
27541 memory. The data field is emitted directly before the global
27542 entry point in rs6000_elf_declare_function_name. */
27543 char buf[256];
27545 #ifdef HAVE_AS_ENTRY_MARKERS
27546 /* If supported by the linker, emit a marker relocation. If the
27547 total code size of the final executable or shared library
27548 happens to fit into 2 GB after all, the linker will replace
27549 this code sequence with the sequence for the small or medium
27550 code model. */
27551 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
27552 #endif
27553 fprintf (file, "\tld 2,");
27554 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
27555 assemble_name (file, buf);
27556 fprintf (file, "-");
27557 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27558 assemble_name (file, buf);
27559 fprintf (file, "(12)\n");
27560 fprintf (file, "\tadd 2,2,12\n");
27563 fputs ("\t.localentry\t", file);
27564 assemble_name (file, name);
27565 fputs (",.-", file);
27566 assemble_name (file, name);
27567 fputs ("\n", file);
27570 /* Output -mprofile-kernel code. This needs to be done here instead of
27571 in output_function_profile since it must go after the ELFv2 ABI
27572 local entry point. */
27573 if (TARGET_PROFILE_KERNEL && crtl->profile)
27575 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
27576 gcc_assert (!TARGET_32BIT);
27578 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
27580 /* In the ELFv2 ABI we have no compiler stack word. It must be
27581 the resposibility of _mcount to preserve the static chain
27582 register if required. */
27583 if (DEFAULT_ABI != ABI_ELFv2
27584 && cfun->static_chain_decl != NULL)
27586 asm_fprintf (file, "\tstd %s,24(%s)\n",
27587 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
27588 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
27589 asm_fprintf (file, "\tld %s,24(%s)\n",
27590 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
27592 else
27593 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
27596 rs6000_pic_labelno++;
27599 /* -mprofile-kernel code calls mcount before the function prolog,
27600 so a profiled leaf function should stay a leaf function. */
27601 static bool
27602 rs6000_keep_leaf_when_profiled ()
27604 return TARGET_PROFILE_KERNEL;
27607 /* Non-zero if vmx regs are restored before the frame pop, zero if
27608 we restore after the pop when possible. */
27609 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
27611 /* Restoring cr is a two step process: loading a reg from the frame
27612 save, then moving the reg to cr. For ABI_V4 we must let the
27613 unwinder know that the stack location is no longer valid at or
27614 before the stack deallocation, but we can't emit a cfa_restore for
27615 cr at the stack deallocation like we do for other registers.
27616 The trouble is that it is possible for the move to cr to be
27617 scheduled after the stack deallocation. So say exactly where cr
27618 is located on each of the two insns. */
27620 static rtx
27621 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
27623 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
27624 rtx reg = gen_rtx_REG (SImode, regno);
27625 rtx_insn *insn = emit_move_insn (reg, mem);
27627 if (!exit_func && DEFAULT_ABI == ABI_V4)
27629 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
27630 rtx set = gen_rtx_SET (reg, cr);
27632 add_reg_note (insn, REG_CFA_REGISTER, set);
27633 RTX_FRAME_RELATED_P (insn) = 1;
27635 return reg;
27638 /* Reload CR from REG. */
27640 static void
27641 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
27643 int count = 0;
27644 int i;
27646 if (using_mfcr_multiple)
27648 for (i = 0; i < 8; i++)
27649 if (save_reg_p (CR0_REGNO + i))
27650 count++;
27651 gcc_assert (count);
27654 if (using_mfcr_multiple && count > 1)
27656 rtx_insn *insn;
27657 rtvec p;
27658 int ndx;
27660 p = rtvec_alloc (count);
27662 ndx = 0;
27663 for (i = 0; i < 8; i++)
27664 if (save_reg_p (CR0_REGNO + i))
27666 rtvec r = rtvec_alloc (2);
27667 RTVEC_ELT (r, 0) = reg;
27668 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
27669 RTVEC_ELT (p, ndx) =
27670 gen_rtx_SET (gen_rtx_REG (CCmode, CR0_REGNO + i),
27671 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
27672 ndx++;
27674 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
27675 gcc_assert (ndx == count);
27677 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
27678 CR field separately. */
27679 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
27681 for (i = 0; i < 8; i++)
27682 if (save_reg_p (CR0_REGNO + i))
27683 add_reg_note (insn, REG_CFA_RESTORE,
27684 gen_rtx_REG (SImode, CR0_REGNO + i));
27686 RTX_FRAME_RELATED_P (insn) = 1;
27689 else
27690 for (i = 0; i < 8; i++)
27691 if (save_reg_p (CR0_REGNO + i))
27693 rtx insn = emit_insn (gen_movsi_to_cr_one
27694 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
27696 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
27697 CR field separately, attached to the insn that in fact
27698 restores this particular CR field. */
27699 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
27701 add_reg_note (insn, REG_CFA_RESTORE,
27702 gen_rtx_REG (SImode, CR0_REGNO + i));
27704 RTX_FRAME_RELATED_P (insn) = 1;
27708 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
27709 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
27710 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
27712 rtx_insn *insn = get_last_insn ();
27713 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
27715 add_reg_note (insn, REG_CFA_RESTORE, cr);
27716 RTX_FRAME_RELATED_P (insn) = 1;
27720 /* Like cr, the move to lr instruction can be scheduled after the
27721 stack deallocation, but unlike cr, its stack frame save is still
27722 valid. So we only need to emit the cfa_restore on the correct
27723 instruction. */
27725 static void
27726 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
27728 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
27729 rtx reg = gen_rtx_REG (Pmode, regno);
27731 emit_move_insn (reg, mem);
27734 static void
27735 restore_saved_lr (int regno, bool exit_func)
27737 rtx reg = gen_rtx_REG (Pmode, regno);
27738 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
27739 rtx_insn *insn = emit_move_insn (lr, reg);
27741 if (!exit_func && flag_shrink_wrap)
27743 add_reg_note (insn, REG_CFA_RESTORE, lr);
27744 RTX_FRAME_RELATED_P (insn) = 1;
27748 static rtx
27749 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
27751 if (DEFAULT_ABI == ABI_ELFv2)
27753 int i;
27754 for (i = 0; i < 8; i++)
27755 if (save_reg_p (CR0_REGNO + i))
27757 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
27758 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
27759 cfa_restores);
27762 else if (info->cr_save_p)
27763 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
27764 gen_rtx_REG (SImode, CR2_REGNO),
27765 cfa_restores);
27767 if (info->lr_save_p)
27768 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
27769 gen_rtx_REG (Pmode, LR_REGNO),
27770 cfa_restores);
27771 return cfa_restores;
27774 /* Return true if OFFSET from stack pointer can be clobbered by signals.
27775 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
27776 below stack pointer not cloberred by signals. */
27778 static inline bool
27779 offset_below_red_zone_p (HOST_WIDE_INT offset)
27781 return offset < (DEFAULT_ABI == ABI_V4
27783 : TARGET_32BIT ? -220 : -288);
27786 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
27788 static void
27789 emit_cfa_restores (rtx cfa_restores)
27791 rtx_insn *insn = get_last_insn ();
27792 rtx *loc = &REG_NOTES (insn);
27794 while (*loc)
27795 loc = &XEXP (*loc, 1);
27796 *loc = cfa_restores;
27797 RTX_FRAME_RELATED_P (insn) = 1;
27800 /* Emit function epilogue as insns. */
27802 void
27803 rs6000_emit_epilogue (int sibcall)
27805 rs6000_stack_t *info;
27806 int restoring_GPRs_inline;
27807 int restoring_FPRs_inline;
27808 int using_load_multiple;
27809 int using_mtcr_multiple;
27810 int use_backchain_to_restore_sp;
27811 int restore_lr;
27812 int strategy;
27813 HOST_WIDE_INT frame_off = 0;
27814 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
27815 rtx frame_reg_rtx = sp_reg_rtx;
27816 rtx cfa_restores = NULL_RTX;
27817 rtx insn;
27818 rtx cr_save_reg = NULL_RTX;
27819 machine_mode reg_mode = Pmode;
27820 int reg_size = TARGET_32BIT ? 4 : 8;
27821 machine_mode fp_reg_mode = (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
27822 ? DFmode : SFmode;
27823 int fp_reg_size = 8;
27824 int i;
27825 bool exit_func;
27826 unsigned ptr_regno;
27828 info = rs6000_stack_info ();
27830 strategy = info->savres_strategy;
27831 using_load_multiple = strategy & REST_MULTIPLE;
27832 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
27833 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
27834 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
27835 || rs6000_cpu == PROCESSOR_PPC603
27836 || rs6000_cpu == PROCESSOR_PPC750
27837 || optimize_size);
27838 /* Restore via the backchain when we have a large frame, since this
27839 is more efficient than an addis, addi pair. The second condition
27840 here will not trigger at the moment; We don't actually need a
27841 frame pointer for alloca, but the generic parts of the compiler
27842 give us one anyway. */
27843 use_backchain_to_restore_sp = (info->total_size + (info->lr_save_p
27844 ? info->lr_save_offset
27845 : 0) > 32767
27846 || (cfun->calls_alloca
27847 && !frame_pointer_needed));
27848 restore_lr = (info->lr_save_p
27849 && (restoring_FPRs_inline
27850 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
27851 && (restoring_GPRs_inline
27852 || info->first_fp_reg_save < 64)
27853 && !cfun->machine->lr_is_wrapped_separately);
27856 if (WORLD_SAVE_P (info))
27858 int i, j;
27859 char rname[30];
27860 const char *alloc_rname;
27861 rtvec p;
27863 /* eh_rest_world_r10 will return to the location saved in the LR
27864 stack slot (which is not likely to be our caller.)
27865 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
27866 rest_world is similar, except any R10 parameter is ignored.
27867 The exception-handling stuff that was here in 2.95 is no
27868 longer necessary. */
27870 p = rtvec_alloc (9
27871 + 32 - info->first_gp_reg_save
27872 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
27873 + 63 + 1 - info->first_fp_reg_save);
27875 strcpy (rname, ((crtl->calls_eh_return) ?
27876 "*eh_rest_world_r10" : "*rest_world"));
27877 alloc_rname = ggc_strdup (rname);
27879 j = 0;
27880 RTVEC_ELT (p, j++) = ret_rtx;
27881 RTVEC_ELT (p, j++)
27882 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
27883 /* The instruction pattern requires a clobber here;
27884 it is shared with the restVEC helper. */
27885 RTVEC_ELT (p, j++)
27886 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
27889 /* CR register traditionally saved as CR2. */
27890 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
27891 RTVEC_ELT (p, j++)
27892 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
27893 if (flag_shrink_wrap)
27895 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
27896 gen_rtx_REG (Pmode, LR_REGNO),
27897 cfa_restores);
27898 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27902 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
27904 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
27905 RTVEC_ELT (p, j++)
27906 = gen_frame_load (reg,
27907 frame_reg_rtx, info->gp_save_offset + reg_size * i);
27908 if (flag_shrink_wrap
27909 && save_reg_p (info->first_gp_reg_save + i))
27910 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27912 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
27914 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
27915 RTVEC_ELT (p, j++)
27916 = gen_frame_load (reg,
27917 frame_reg_rtx, info->altivec_save_offset + 16 * i);
27918 if (flag_shrink_wrap
27919 && save_reg_p (info->first_altivec_reg_save + i))
27920 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27922 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
27924 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
27925 ? DFmode : SFmode),
27926 info->first_fp_reg_save + i);
27927 RTVEC_ELT (p, j++)
27928 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
27929 if (flag_shrink_wrap
27930 && save_reg_p (info->first_fp_reg_save + i))
27931 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27933 RTVEC_ELT (p, j++)
27934 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
27935 RTVEC_ELT (p, j++)
27936 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
27937 RTVEC_ELT (p, j++)
27938 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
27939 RTVEC_ELT (p, j++)
27940 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
27941 RTVEC_ELT (p, j++)
27942 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
27943 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
27945 if (flag_shrink_wrap)
27947 REG_NOTES (insn) = cfa_restores;
27948 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
27949 RTX_FRAME_RELATED_P (insn) = 1;
27951 return;
27954 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
27955 if (info->push_p)
27956 frame_off = info->total_size;
27958 /* Restore AltiVec registers if we must do so before adjusting the
27959 stack. */
27960 if (info->altivec_size != 0
27961 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
27962 || (DEFAULT_ABI != ABI_V4
27963 && offset_below_red_zone_p (info->altivec_save_offset))))
27965 int i;
27966 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
27968 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
27969 if (use_backchain_to_restore_sp)
27971 int frame_regno = 11;
27973 if ((strategy & REST_INLINE_VRS) == 0)
27975 /* Of r11 and r12, select the one not clobbered by an
27976 out-of-line restore function for the frame register. */
27977 frame_regno = 11 + 12 - scratch_regno;
27979 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
27980 emit_move_insn (frame_reg_rtx,
27981 gen_rtx_MEM (Pmode, sp_reg_rtx));
27982 frame_off = 0;
27984 else if (frame_pointer_needed)
27985 frame_reg_rtx = hard_frame_pointer_rtx;
27987 if ((strategy & REST_INLINE_VRS) == 0)
27989 int end_save = info->altivec_save_offset + info->altivec_size;
27990 int ptr_off;
27991 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
27992 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
27994 if (end_save + frame_off != 0)
27996 rtx offset = GEN_INT (end_save + frame_off);
27998 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
28000 else
28001 emit_move_insn (ptr_reg, frame_reg_rtx);
28003 ptr_off = -end_save;
28004 insn = rs6000_emit_savres_rtx (info, scratch_reg,
28005 info->altivec_save_offset + ptr_off,
28006 0, V4SImode, SAVRES_VR);
28008 else
28010 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28011 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
28013 rtx addr, areg, mem, insn;
28014 rtx reg = gen_rtx_REG (V4SImode, i);
28015 HOST_WIDE_INT offset
28016 = (info->altivec_save_offset + frame_off
28017 + 16 * (i - info->first_altivec_reg_save));
28019 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
28021 mem = gen_frame_mem (V4SImode,
28022 gen_rtx_PLUS (Pmode, frame_reg_rtx,
28023 GEN_INT (offset)));
28024 insn = gen_rtx_SET (reg, mem);
28026 else
28028 areg = gen_rtx_REG (Pmode, 0);
28029 emit_move_insn (areg, GEN_INT (offset));
28031 /* AltiVec addressing mode is [reg+reg]. */
28032 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
28033 mem = gen_frame_mem (V4SImode, addr);
28035 /* Rather than emitting a generic move, force use of the
28036 lvx instruction, which we always want. In particular we
28037 don't want lxvd2x/xxpermdi for little endian. */
28038 insn = gen_altivec_lvx_v4si_internal (reg, mem);
28041 (void) emit_insn (insn);
28045 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28046 if (((strategy & REST_INLINE_VRS) == 0
28047 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
28048 && (flag_shrink_wrap
28049 || (offset_below_red_zone_p
28050 (info->altivec_save_offset
28051 + 16 * (i - info->first_altivec_reg_save))))
28052 && save_reg_p (i))
28054 rtx reg = gen_rtx_REG (V4SImode, i);
28055 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28059 /* Restore VRSAVE if we must do so before adjusting the stack. */
28060 if (info->vrsave_size != 0
28061 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28062 || (DEFAULT_ABI != ABI_V4
28063 && offset_below_red_zone_p (info->vrsave_save_offset))))
28065 rtx reg;
28067 if (frame_reg_rtx == sp_reg_rtx)
28069 if (use_backchain_to_restore_sp)
28071 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
28072 emit_move_insn (frame_reg_rtx,
28073 gen_rtx_MEM (Pmode, sp_reg_rtx));
28074 frame_off = 0;
28076 else if (frame_pointer_needed)
28077 frame_reg_rtx = hard_frame_pointer_rtx;
28080 reg = gen_rtx_REG (SImode, 12);
28081 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28082 info->vrsave_save_offset + frame_off));
28084 emit_insn (generate_set_vrsave (reg, info, 1));
28087 insn = NULL_RTX;
28088 /* If we have a large stack frame, restore the old stack pointer
28089 using the backchain. */
28090 if (use_backchain_to_restore_sp)
28092 if (frame_reg_rtx == sp_reg_rtx)
28094 /* Under V.4, don't reset the stack pointer until after we're done
28095 loading the saved registers. */
28096 if (DEFAULT_ABI == ABI_V4)
28097 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
28099 insn = emit_move_insn (frame_reg_rtx,
28100 gen_rtx_MEM (Pmode, sp_reg_rtx));
28101 frame_off = 0;
28103 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28104 && DEFAULT_ABI == ABI_V4)
28105 /* frame_reg_rtx has been set up by the altivec restore. */
28107 else
28109 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
28110 frame_reg_rtx = sp_reg_rtx;
28113 /* If we have a frame pointer, we can restore the old stack pointer
28114 from it. */
28115 else if (frame_pointer_needed)
28117 frame_reg_rtx = sp_reg_rtx;
28118 if (DEFAULT_ABI == ABI_V4)
28119 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
28120 /* Prevent reordering memory accesses against stack pointer restore. */
28121 else if (cfun->calls_alloca
28122 || offset_below_red_zone_p (-info->total_size))
28123 rs6000_emit_stack_tie (frame_reg_rtx, true);
28125 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
28126 GEN_INT (info->total_size)));
28127 frame_off = 0;
28129 else if (info->push_p
28130 && DEFAULT_ABI != ABI_V4
28131 && !crtl->calls_eh_return)
28133 /* Prevent reordering memory accesses against stack pointer restore. */
28134 if (cfun->calls_alloca
28135 || offset_below_red_zone_p (-info->total_size))
28136 rs6000_emit_stack_tie (frame_reg_rtx, false);
28137 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
28138 GEN_INT (info->total_size)));
28139 frame_off = 0;
28141 if (insn && frame_reg_rtx == sp_reg_rtx)
28143 if (cfa_restores)
28145 REG_NOTES (insn) = cfa_restores;
28146 cfa_restores = NULL_RTX;
28148 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
28149 RTX_FRAME_RELATED_P (insn) = 1;
28152 /* Restore AltiVec registers if we have not done so already. */
28153 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28154 && info->altivec_size != 0
28155 && (DEFAULT_ABI == ABI_V4
28156 || !offset_below_red_zone_p (info->altivec_save_offset)))
28158 int i;
28160 if ((strategy & REST_INLINE_VRS) == 0)
28162 int end_save = info->altivec_save_offset + info->altivec_size;
28163 int ptr_off;
28164 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
28165 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
28166 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
28168 if (end_save + frame_off != 0)
28170 rtx offset = GEN_INT (end_save + frame_off);
28172 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
28174 else
28175 emit_move_insn (ptr_reg, frame_reg_rtx);
28177 ptr_off = -end_save;
28178 insn = rs6000_emit_savres_rtx (info, scratch_reg,
28179 info->altivec_save_offset + ptr_off,
28180 0, V4SImode, SAVRES_VR);
28181 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
28183 /* Frame reg was clobbered by out-of-line save. Restore it
28184 from ptr_reg, and if we are calling out-of-line gpr or
28185 fpr restore set up the correct pointer and offset. */
28186 unsigned newptr_regno = 1;
28187 if (!restoring_GPRs_inline)
28189 bool lr = info->gp_save_offset + info->gp_size == 0;
28190 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
28191 newptr_regno = ptr_regno_for_savres (sel);
28192 end_save = info->gp_save_offset + info->gp_size;
28194 else if (!restoring_FPRs_inline)
28196 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
28197 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
28198 newptr_regno = ptr_regno_for_savres (sel);
28199 end_save = info->fp_save_offset + info->fp_size;
28202 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
28203 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
28205 if (end_save + ptr_off != 0)
28207 rtx offset = GEN_INT (end_save + ptr_off);
28209 frame_off = -end_save;
28210 if (TARGET_32BIT)
28211 emit_insn (gen_addsi3_carry (frame_reg_rtx,
28212 ptr_reg, offset));
28213 else
28214 emit_insn (gen_adddi3_carry (frame_reg_rtx,
28215 ptr_reg, offset));
28217 else
28219 frame_off = ptr_off;
28220 emit_move_insn (frame_reg_rtx, ptr_reg);
28224 else
28226 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28227 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
28229 rtx addr, areg, mem, insn;
28230 rtx reg = gen_rtx_REG (V4SImode, i);
28231 HOST_WIDE_INT offset
28232 = (info->altivec_save_offset + frame_off
28233 + 16 * (i - info->first_altivec_reg_save));
28235 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
28237 mem = gen_frame_mem (V4SImode,
28238 gen_rtx_PLUS (Pmode, frame_reg_rtx,
28239 GEN_INT (offset)));
28240 insn = gen_rtx_SET (reg, mem);
28242 else
28244 areg = gen_rtx_REG (Pmode, 0);
28245 emit_move_insn (areg, GEN_INT (offset));
28247 /* AltiVec addressing mode is [reg+reg]. */
28248 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
28249 mem = gen_frame_mem (V4SImode, addr);
28251 /* Rather than emitting a generic move, force use of the
28252 lvx instruction, which we always want. In particular we
28253 don't want lxvd2x/xxpermdi for little endian. */
28254 insn = gen_altivec_lvx_v4si_internal (reg, mem);
28257 (void) emit_insn (insn);
28261 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28262 if (((strategy & REST_INLINE_VRS) == 0
28263 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
28264 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
28265 && save_reg_p (i))
28267 rtx reg = gen_rtx_REG (V4SImode, i);
28268 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28272 /* Restore VRSAVE if we have not done so already. */
28273 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28274 && info->vrsave_size != 0
28275 && (DEFAULT_ABI == ABI_V4
28276 || !offset_below_red_zone_p (info->vrsave_save_offset)))
28278 rtx reg;
28280 reg = gen_rtx_REG (SImode, 12);
28281 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28282 info->vrsave_save_offset + frame_off));
28284 emit_insn (generate_set_vrsave (reg, info, 1));
28287 /* If we exit by an out-of-line restore function on ABI_V4 then that
28288 function will deallocate the stack, so we don't need to worry
28289 about the unwinder restoring cr from an invalid stack frame
28290 location. */
28291 exit_func = (!restoring_FPRs_inline
28292 || (!restoring_GPRs_inline
28293 && info->first_fp_reg_save == 64));
28295 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
28296 *separate* slots if the routine calls __builtin_eh_return, so
28297 that they can be independently restored by the unwinder. */
28298 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
28300 int i, cr_off = info->ehcr_offset;
28302 for (i = 0; i < 8; i++)
28303 if (!call_used_regs[CR0_REGNO + i])
28305 rtx reg = gen_rtx_REG (SImode, 0);
28306 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28307 cr_off + frame_off));
28309 insn = emit_insn (gen_movsi_to_cr_one
28310 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
28312 if (!exit_func && flag_shrink_wrap)
28314 add_reg_note (insn, REG_CFA_RESTORE,
28315 gen_rtx_REG (SImode, CR0_REGNO + i));
28317 RTX_FRAME_RELATED_P (insn) = 1;
28320 cr_off += reg_size;
28324 /* Get the old lr if we saved it. If we are restoring registers
28325 out-of-line, then the out-of-line routines can do this for us. */
28326 if (restore_lr && restoring_GPRs_inline)
28327 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
28329 /* Get the old cr if we saved it. */
28330 if (info->cr_save_p)
28332 unsigned cr_save_regno = 12;
28334 if (!restoring_GPRs_inline)
28336 /* Ensure we don't use the register used by the out-of-line
28337 gpr register restore below. */
28338 bool lr = info->gp_save_offset + info->gp_size == 0;
28339 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
28340 int gpr_ptr_regno = ptr_regno_for_savres (sel);
28342 if (gpr_ptr_regno == 12)
28343 cr_save_regno = 11;
28344 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
28346 else if (REGNO (frame_reg_rtx) == 12)
28347 cr_save_regno = 11;
28349 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
28350 info->cr_save_offset + frame_off,
28351 exit_func);
28354 /* Set LR here to try to overlap restores below. */
28355 if (restore_lr && restoring_GPRs_inline)
28356 restore_saved_lr (0, exit_func);
28358 /* Load exception handler data registers, if needed. */
28359 if (crtl->calls_eh_return)
28361 unsigned int i, regno;
28363 if (TARGET_AIX)
28365 rtx reg = gen_rtx_REG (reg_mode, 2);
28366 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28367 frame_off + RS6000_TOC_SAVE_SLOT));
28370 for (i = 0; ; ++i)
28372 rtx mem;
28374 regno = EH_RETURN_DATA_REGNO (i);
28375 if (regno == INVALID_REGNUM)
28376 break;
28378 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
28379 info->ehrd_offset + frame_off
28380 + reg_size * (int) i);
28382 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
28386 /* Restore GPRs. This is done as a PARALLEL if we are using
28387 the load-multiple instructions. */
28388 if (!restoring_GPRs_inline)
28390 /* We are jumping to an out-of-line function. */
28391 rtx ptr_reg;
28392 int end_save = info->gp_save_offset + info->gp_size;
28393 bool can_use_exit = end_save == 0;
28394 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
28395 int ptr_off;
28397 /* Emit stack reset code if we need it. */
28398 ptr_regno = ptr_regno_for_savres (sel);
28399 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
28400 if (can_use_exit)
28401 rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
28402 else if (end_save + frame_off != 0)
28403 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
28404 GEN_INT (end_save + frame_off)));
28405 else if (REGNO (frame_reg_rtx) != ptr_regno)
28406 emit_move_insn (ptr_reg, frame_reg_rtx);
28407 if (REGNO (frame_reg_rtx) == ptr_regno)
28408 frame_off = -end_save;
28410 if (can_use_exit && info->cr_save_p)
28411 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
28413 ptr_off = -end_save;
28414 rs6000_emit_savres_rtx (info, ptr_reg,
28415 info->gp_save_offset + ptr_off,
28416 info->lr_save_offset + ptr_off,
28417 reg_mode, sel);
28419 else if (using_load_multiple)
28421 rtvec p;
28422 p = rtvec_alloc (32 - info->first_gp_reg_save);
28423 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
28424 RTVEC_ELT (p, i)
28425 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
28426 frame_reg_rtx,
28427 info->gp_save_offset + frame_off + reg_size * i);
28428 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
28430 else
28432 int offset = info->gp_save_offset + frame_off;
28433 for (i = info->first_gp_reg_save; i < 32; i++)
28435 if (save_reg_p (i)
28436 && !cfun->machine->gpr_is_wrapped_separately[i])
28438 rtx reg = gen_rtx_REG (reg_mode, i);
28439 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
28442 offset += reg_size;
28446 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
28448 /* If the frame pointer was used then we can't delay emitting
28449 a REG_CFA_DEF_CFA note. This must happen on the insn that
28450 restores the frame pointer, r31. We may have already emitted
28451 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
28452 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
28453 be harmless if emitted. */
28454 if (frame_pointer_needed)
28456 insn = get_last_insn ();
28457 add_reg_note (insn, REG_CFA_DEF_CFA,
28458 plus_constant (Pmode, frame_reg_rtx, frame_off));
28459 RTX_FRAME_RELATED_P (insn) = 1;
28462 /* Set up cfa_restores. We always need these when
28463 shrink-wrapping. If not shrink-wrapping then we only need
28464 the cfa_restore when the stack location is no longer valid.
28465 The cfa_restores must be emitted on or before the insn that
28466 invalidates the stack, and of course must not be emitted
28467 before the insn that actually does the restore. The latter
28468 is why it is a bad idea to emit the cfa_restores as a group
28469 on the last instruction here that actually does a restore:
28470 That insn may be reordered with respect to others doing
28471 restores. */
28472 if (flag_shrink_wrap
28473 && !restoring_GPRs_inline
28474 && info->first_fp_reg_save == 64)
28475 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
28477 for (i = info->first_gp_reg_save; i < 32; i++)
28478 if (save_reg_p (i)
28479 && !cfun->machine->gpr_is_wrapped_separately[i])
28481 rtx reg = gen_rtx_REG (reg_mode, i);
28482 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28486 if (!restoring_GPRs_inline
28487 && info->first_fp_reg_save == 64)
28489 /* We are jumping to an out-of-line function. */
28490 if (cfa_restores)
28491 emit_cfa_restores (cfa_restores);
28492 return;
28495 if (restore_lr && !restoring_GPRs_inline)
28497 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
28498 restore_saved_lr (0, exit_func);
28501 /* Restore fpr's if we need to do it without calling a function. */
28502 if (restoring_FPRs_inline)
28504 int offset = info->fp_save_offset + frame_off;
28505 for (i = info->first_fp_reg_save; i < 64; i++)
28507 if (save_reg_p (i)
28508 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
28510 rtx reg = gen_rtx_REG (fp_reg_mode, i);
28511 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
28512 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
28513 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
28514 cfa_restores);
28517 offset += fp_reg_size;
28521 /* If we saved cr, restore it here. Just those that were used. */
28522 if (info->cr_save_p)
28523 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
28525 /* If this is V.4, unwind the stack pointer after all of the loads
28526 have been done, or set up r11 if we are restoring fp out of line. */
28527 ptr_regno = 1;
28528 if (!restoring_FPRs_inline)
28530 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
28531 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
28532 ptr_regno = ptr_regno_for_savres (sel);
28535 insn = rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
28536 if (REGNO (frame_reg_rtx) == ptr_regno)
28537 frame_off = 0;
28539 if (insn && restoring_FPRs_inline)
28541 if (cfa_restores)
28543 REG_NOTES (insn) = cfa_restores;
28544 cfa_restores = NULL_RTX;
28546 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
28547 RTX_FRAME_RELATED_P (insn) = 1;
28550 if (crtl->calls_eh_return)
28552 rtx sa = EH_RETURN_STACKADJ_RTX;
28553 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
28556 if (!sibcall && restoring_FPRs_inline)
28558 if (cfa_restores)
28560 /* We can't hang the cfa_restores off a simple return,
28561 since the shrink-wrap code sometimes uses an existing
28562 return. This means there might be a path from
28563 pre-prologue code to this return, and dwarf2cfi code
28564 wants the eh_frame unwinder state to be the same on
28565 all paths to any point. So we need to emit the
28566 cfa_restores before the return. For -m64 we really
28567 don't need epilogue cfa_restores at all, except for
28568 this irritating dwarf2cfi with shrink-wrap
28569 requirement; The stack red-zone means eh_frame info
28570 from the prologue telling the unwinder to restore
28571 from the stack is perfectly good right to the end of
28572 the function. */
28573 emit_insn (gen_blockage ());
28574 emit_cfa_restores (cfa_restores);
28575 cfa_restores = NULL_RTX;
28578 emit_jump_insn (targetm.gen_simple_return ());
28581 if (!sibcall && !restoring_FPRs_inline)
28583 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
28584 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
28585 int elt = 0;
28586 RTVEC_ELT (p, elt++) = ret_rtx;
28587 if (lr)
28588 RTVEC_ELT (p, elt++)
28589 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
28591 /* We have to restore more than two FP registers, so branch to the
28592 restore function. It will return to our caller. */
28593 int i;
28594 int reg;
28595 rtx sym;
28597 if (flag_shrink_wrap)
28598 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
28600 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
28601 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
28602 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
28603 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
28605 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
28607 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
28609 RTVEC_ELT (p, elt++)
28610 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
28611 if (flag_shrink_wrap
28612 && save_reg_p (info->first_fp_reg_save + i))
28613 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28616 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
28619 if (cfa_restores)
28621 if (sibcall)
28622 /* Ensure the cfa_restores are hung off an insn that won't
28623 be reordered above other restores. */
28624 emit_insn (gen_blockage ());
28626 emit_cfa_restores (cfa_restores);
28630 /* Write function epilogue. */
28632 static void
28633 rs6000_output_function_epilogue (FILE *file)
28635 #if TARGET_MACHO
28636 macho_branch_islands ();
28639 rtx_insn *insn = get_last_insn ();
28640 rtx_insn *deleted_debug_label = NULL;
28642 /* Mach-O doesn't support labels at the end of objects, so if
28643 it looks like we might want one, take special action.
28645 First, collect any sequence of deleted debug labels. */
28646 while (insn
28647 && NOTE_P (insn)
28648 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
28650 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
28651 notes only, instead set their CODE_LABEL_NUMBER to -1,
28652 otherwise there would be code generation differences
28653 in between -g and -g0. */
28654 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
28655 deleted_debug_label = insn;
28656 insn = PREV_INSN (insn);
28659 /* Second, if we have:
28660 label:
28661 barrier
28662 then this needs to be detected, so skip past the barrier. */
28664 if (insn && BARRIER_P (insn))
28665 insn = PREV_INSN (insn);
28667 /* Up to now we've only seen notes or barriers. */
28668 if (insn)
28670 if (LABEL_P (insn)
28671 || (NOTE_P (insn)
28672 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
28673 /* Trailing label: <barrier>. */
28674 fputs ("\tnop\n", file);
28675 else
28677 /* Lastly, see if we have a completely empty function body. */
28678 while (insn && ! INSN_P (insn))
28679 insn = PREV_INSN (insn);
28680 /* If we don't find any insns, we've got an empty function body;
28681 I.e. completely empty - without a return or branch. This is
28682 taken as the case where a function body has been removed
28683 because it contains an inline __builtin_unreachable(). GCC
28684 states that reaching __builtin_unreachable() means UB so we're
28685 not obliged to do anything special; however, we want
28686 non-zero-sized function bodies. To meet this, and help the
28687 user out, let's trap the case. */
28688 if (insn == NULL)
28689 fputs ("\ttrap\n", file);
28692 else if (deleted_debug_label)
28693 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
28694 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
28695 CODE_LABEL_NUMBER (insn) = -1;
28697 #endif
28699 /* Output a traceback table here. See /usr/include/sys/debug.h for info
28700 on its format.
28702 We don't output a traceback table if -finhibit-size-directive was
28703 used. The documentation for -finhibit-size-directive reads
28704 ``don't output a @code{.size} assembler directive, or anything
28705 else that would cause trouble if the function is split in the
28706 middle, and the two halves are placed at locations far apart in
28707 memory.'' The traceback table has this property, since it
28708 includes the offset from the start of the function to the
28709 traceback table itself.
28711 System V.4 Powerpc's (and the embedded ABI derived from it) use a
28712 different traceback table. */
28713 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28714 && ! flag_inhibit_size_directive
28715 && rs6000_traceback != traceback_none && !cfun->is_thunk)
28717 const char *fname = NULL;
28718 const char *language_string = lang_hooks.name;
28719 int fixed_parms = 0, float_parms = 0, parm_info = 0;
28720 int i;
28721 int optional_tbtab;
28722 rs6000_stack_t *info = rs6000_stack_info ();
28724 if (rs6000_traceback == traceback_full)
28725 optional_tbtab = 1;
28726 else if (rs6000_traceback == traceback_part)
28727 optional_tbtab = 0;
28728 else
28729 optional_tbtab = !optimize_size && !TARGET_ELF;
28731 if (optional_tbtab)
28733 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
28734 while (*fname == '.') /* V.4 encodes . in the name */
28735 fname++;
28737 /* Need label immediately before tbtab, so we can compute
28738 its offset from the function start. */
28739 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
28740 ASM_OUTPUT_LABEL (file, fname);
28743 /* The .tbtab pseudo-op can only be used for the first eight
28744 expressions, since it can't handle the possibly variable
28745 length fields that follow. However, if you omit the optional
28746 fields, the assembler outputs zeros for all optional fields
28747 anyways, giving each variable length field is minimum length
28748 (as defined in sys/debug.h). Thus we can not use the .tbtab
28749 pseudo-op at all. */
28751 /* An all-zero word flags the start of the tbtab, for debuggers
28752 that have to find it by searching forward from the entry
28753 point or from the current pc. */
28754 fputs ("\t.long 0\n", file);
28756 /* Tbtab format type. Use format type 0. */
28757 fputs ("\t.byte 0,", file);
28759 /* Language type. Unfortunately, there does not seem to be any
28760 official way to discover the language being compiled, so we
28761 use language_string.
28762 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
28763 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
28764 a number, so for now use 9. LTO, Go and JIT aren't assigned numbers
28765 either, so for now use 0. */
28766 if (lang_GNU_C ()
28767 || ! strcmp (language_string, "GNU GIMPLE")
28768 || ! strcmp (language_string, "GNU Go")
28769 || ! strcmp (language_string, "libgccjit"))
28770 i = 0;
28771 else if (! strcmp (language_string, "GNU F77")
28772 || lang_GNU_Fortran ())
28773 i = 1;
28774 else if (! strcmp (language_string, "GNU Pascal"))
28775 i = 2;
28776 else if (! strcmp (language_string, "GNU Ada"))
28777 i = 3;
28778 else if (lang_GNU_CXX ()
28779 || ! strcmp (language_string, "GNU Objective-C++"))
28780 i = 9;
28781 else if (! strcmp (language_string, "GNU Java"))
28782 i = 13;
28783 else if (! strcmp (language_string, "GNU Objective-C"))
28784 i = 14;
28785 else
28786 gcc_unreachable ();
28787 fprintf (file, "%d,", i);
28789 /* 8 single bit fields: global linkage (not set for C extern linkage,
28790 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
28791 from start of procedure stored in tbtab, internal function, function
28792 has controlled storage, function has no toc, function uses fp,
28793 function logs/aborts fp operations. */
28794 /* Assume that fp operations are used if any fp reg must be saved. */
28795 fprintf (file, "%d,",
28796 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
28798 /* 6 bitfields: function is interrupt handler, name present in
28799 proc table, function calls alloca, on condition directives
28800 (controls stack walks, 3 bits), saves condition reg, saves
28801 link reg. */
28802 /* The `function calls alloca' bit seems to be set whenever reg 31 is
28803 set up as a frame pointer, even when there is no alloca call. */
28804 fprintf (file, "%d,",
28805 ((optional_tbtab << 6)
28806 | ((optional_tbtab & frame_pointer_needed) << 5)
28807 | (info->cr_save_p << 1)
28808 | (info->lr_save_p)));
28810 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
28811 (6 bits). */
28812 fprintf (file, "%d,",
28813 (info->push_p << 7) | (64 - info->first_fp_reg_save));
28815 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
28816 fprintf (file, "%d,", (32 - first_reg_to_save ()));
28818 if (optional_tbtab)
28820 /* Compute the parameter info from the function decl argument
28821 list. */
28822 tree decl;
28823 int next_parm_info_bit = 31;
28825 for (decl = DECL_ARGUMENTS (current_function_decl);
28826 decl; decl = DECL_CHAIN (decl))
28828 rtx parameter = DECL_INCOMING_RTL (decl);
28829 machine_mode mode = GET_MODE (parameter);
28831 if (GET_CODE (parameter) == REG)
28833 if (SCALAR_FLOAT_MODE_P (mode))
28835 int bits;
28837 float_parms++;
28839 switch (mode)
28841 case E_SFmode:
28842 case E_SDmode:
28843 bits = 0x2;
28844 break;
28846 case E_DFmode:
28847 case E_DDmode:
28848 case E_TFmode:
28849 case E_TDmode:
28850 case E_IFmode:
28851 case E_KFmode:
28852 bits = 0x3;
28853 break;
28855 default:
28856 gcc_unreachable ();
28859 /* If only one bit will fit, don't or in this entry. */
28860 if (next_parm_info_bit > 0)
28861 parm_info |= (bits << (next_parm_info_bit - 1));
28862 next_parm_info_bit -= 2;
28864 else
28866 fixed_parms += ((GET_MODE_SIZE (mode)
28867 + (UNITS_PER_WORD - 1))
28868 / UNITS_PER_WORD);
28869 next_parm_info_bit -= 1;
28875 /* Number of fixed point parameters. */
28876 /* This is actually the number of words of fixed point parameters; thus
28877 an 8 byte struct counts as 2; and thus the maximum value is 8. */
28878 fprintf (file, "%d,", fixed_parms);
28880 /* 2 bitfields: number of floating point parameters (7 bits), parameters
28881 all on stack. */
28882 /* This is actually the number of fp registers that hold parameters;
28883 and thus the maximum value is 13. */
28884 /* Set parameters on stack bit if parameters are not in their original
28885 registers, regardless of whether they are on the stack? Xlc
28886 seems to set the bit when not optimizing. */
28887 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
28889 if (optional_tbtab)
28891 /* Optional fields follow. Some are variable length. */
28893 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
28894 float, 11 double float. */
28895 /* There is an entry for each parameter in a register, in the order
28896 that they occur in the parameter list. Any intervening arguments
28897 on the stack are ignored. If the list overflows a long (max
28898 possible length 34 bits) then completely leave off all elements
28899 that don't fit. */
28900 /* Only emit this long if there was at least one parameter. */
28901 if (fixed_parms || float_parms)
28902 fprintf (file, "\t.long %d\n", parm_info);
28904 /* Offset from start of code to tb table. */
28905 fputs ("\t.long ", file);
28906 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
28907 RS6000_OUTPUT_BASENAME (file, fname);
28908 putc ('-', file);
28909 rs6000_output_function_entry (file, fname);
28910 putc ('\n', file);
28912 /* Interrupt handler mask. */
28913 /* Omit this long, since we never set the interrupt handler bit
28914 above. */
28916 /* Number of CTL (controlled storage) anchors. */
28917 /* Omit this long, since the has_ctl bit is never set above. */
28919 /* Displacement into stack of each CTL anchor. */
28920 /* Omit this list of longs, because there are no CTL anchors. */
28922 /* Length of function name. */
28923 if (*fname == '*')
28924 ++fname;
28925 fprintf (file, "\t.short %d\n", (int) strlen (fname));
28927 /* Function name. */
28928 assemble_string (fname, strlen (fname));
28930 /* Register for alloca automatic storage; this is always reg 31.
28931 Only emit this if the alloca bit was set above. */
28932 if (frame_pointer_needed)
28933 fputs ("\t.byte 31\n", file);
28935 fputs ("\t.align 2\n", file);
28939 /* Arrange to define .LCTOC1 label, if not already done. */
28940 if (need_toc_init)
28942 need_toc_init = 0;
28943 if (!toc_initialized)
28945 switch_to_section (toc_section);
28946 switch_to_section (current_function_section ());
28951 /* -fsplit-stack support. */
28953 /* A SYMBOL_REF for __morestack. */
28954 static GTY(()) rtx morestack_ref;
28956 static rtx
28957 gen_add3_const (rtx rt, rtx ra, long c)
28959 if (TARGET_64BIT)
28960 return gen_adddi3 (rt, ra, GEN_INT (c));
28961 else
28962 return gen_addsi3 (rt, ra, GEN_INT (c));
28965 /* Emit -fsplit-stack prologue, which goes before the regular function
28966 prologue (at local entry point in the case of ELFv2). */
28968 void
28969 rs6000_expand_split_stack_prologue (void)
28971 rs6000_stack_t *info = rs6000_stack_info ();
28972 unsigned HOST_WIDE_INT allocate;
28973 long alloc_hi, alloc_lo;
28974 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
28975 rtx_insn *insn;
28977 gcc_assert (flag_split_stack && reload_completed);
28979 if (!info->push_p)
28980 return;
28982 if (global_regs[29])
28984 error ("%qs uses register r29", "-fsplit-stack");
28985 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
28986 "conflicts with %qD", global_regs_decl[29]);
28989 allocate = info->total_size;
28990 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
28992 sorry ("Stack frame larger than 2G is not supported for -fsplit-stack");
28993 return;
28995 if (morestack_ref == NULL_RTX)
28997 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
28998 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
28999 | SYMBOL_FLAG_FUNCTION);
29002 r0 = gen_rtx_REG (Pmode, 0);
29003 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29004 r12 = gen_rtx_REG (Pmode, 12);
29005 emit_insn (gen_load_split_stack_limit (r0));
29006 /* Always emit two insns here to calculate the requested stack,
29007 so that the linker can edit them when adjusting size for calling
29008 non-split-stack code. */
29009 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
29010 alloc_lo = -allocate - alloc_hi;
29011 if (alloc_hi != 0)
29013 emit_insn (gen_add3_const (r12, r1, alloc_hi));
29014 if (alloc_lo != 0)
29015 emit_insn (gen_add3_const (r12, r12, alloc_lo));
29016 else
29017 emit_insn (gen_nop ());
29019 else
29021 emit_insn (gen_add3_const (r12, r1, alloc_lo));
29022 emit_insn (gen_nop ());
29025 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
29026 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
29027 ok_label = gen_label_rtx ();
29028 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
29029 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
29030 gen_rtx_LABEL_REF (VOIDmode, ok_label),
29031 pc_rtx);
29032 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
29033 JUMP_LABEL (insn) = ok_label;
29034 /* Mark the jump as very likely to be taken. */
29035 add_reg_br_prob_note (insn, profile_probability::very_likely ());
29037 lr = gen_rtx_REG (Pmode, LR_REGNO);
29038 insn = emit_move_insn (r0, lr);
29039 RTX_FRAME_RELATED_P (insn) = 1;
29040 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
29041 RTX_FRAME_RELATED_P (insn) = 1;
29043 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
29044 const0_rtx, const0_rtx));
29045 call_fusage = NULL_RTX;
29046 use_reg (&call_fusage, r12);
29047 /* Say the call uses r0, even though it doesn't, to stop regrename
29048 from twiddling with the insns saving lr, trashing args for cfun.
29049 The insns restoring lr are similarly protected by making
29050 split_stack_return use r0. */
29051 use_reg (&call_fusage, r0);
29052 add_function_usage_to (insn, call_fusage);
29053 /* Indicate that this function can't jump to non-local gotos. */
29054 make_reg_eh_region_note_nothrow_nononlocal (insn);
29055 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
29056 insn = emit_move_insn (lr, r0);
29057 add_reg_note (insn, REG_CFA_RESTORE, lr);
29058 RTX_FRAME_RELATED_P (insn) = 1;
29059 emit_insn (gen_split_stack_return ());
29061 emit_label (ok_label);
29062 LABEL_NUSES (ok_label) = 1;
29065 /* Return the internal arg pointer used for function incoming
29066 arguments. When -fsplit-stack, the arg pointer is r12 so we need
29067 to copy it to a pseudo in order for it to be preserved over calls
29068 and suchlike. We'd really like to use a pseudo here for the
29069 internal arg pointer but data-flow analysis is not prepared to
29070 accept pseudos as live at the beginning of a function. */
29072 static rtx
29073 rs6000_internal_arg_pointer (void)
29075 if (flag_split_stack
29076 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
29077 == NULL))
29080 if (cfun->machine->split_stack_arg_pointer == NULL_RTX)
29082 rtx pat;
29084 cfun->machine->split_stack_arg_pointer = gen_reg_rtx (Pmode);
29085 REG_POINTER (cfun->machine->split_stack_arg_pointer) = 1;
29087 /* Put the pseudo initialization right after the note at the
29088 beginning of the function. */
29089 pat = gen_rtx_SET (cfun->machine->split_stack_arg_pointer,
29090 gen_rtx_REG (Pmode, 12));
29091 push_topmost_sequence ();
29092 emit_insn_after (pat, get_insns ());
29093 pop_topmost_sequence ();
29095 return plus_constant (Pmode, cfun->machine->split_stack_arg_pointer,
29096 FIRST_PARM_OFFSET (current_function_decl));
29098 return virtual_incoming_args_rtx;
29101 /* We may have to tell the dataflow pass that the split stack prologue
29102 is initializing a register. */
29104 static void
29105 rs6000_live_on_entry (bitmap regs)
29107 if (flag_split_stack)
29108 bitmap_set_bit (regs, 12);
29111 /* Emit -fsplit-stack dynamic stack allocation space check. */
29113 void
29114 rs6000_split_stack_space_check (rtx size, rtx label)
29116 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29117 rtx limit = gen_reg_rtx (Pmode);
29118 rtx requested = gen_reg_rtx (Pmode);
29119 rtx cmp = gen_reg_rtx (CCUNSmode);
29120 rtx jump;
29122 emit_insn (gen_load_split_stack_limit (limit));
29123 if (CONST_INT_P (size))
29124 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
29125 else
29127 size = force_reg (Pmode, size);
29128 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
29130 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
29131 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
29132 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
29133 gen_rtx_LABEL_REF (VOIDmode, label),
29134 pc_rtx);
29135 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
29136 JUMP_LABEL (jump) = label;
29139 /* A C compound statement that outputs the assembler code for a thunk
29140 function, used to implement C++ virtual function calls with
29141 multiple inheritance. The thunk acts as a wrapper around a virtual
29142 function, adjusting the implicit object parameter before handing
29143 control off to the real function.
29145 First, emit code to add the integer DELTA to the location that
29146 contains the incoming first argument. Assume that this argument
29147 contains a pointer, and is the one used to pass the `this' pointer
29148 in C++. This is the incoming argument *before* the function
29149 prologue, e.g. `%o0' on a sparc. The addition must preserve the
29150 values of all other incoming arguments.
29152 After the addition, emit code to jump to FUNCTION, which is a
29153 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
29154 not touch the return address. Hence returning from FUNCTION will
29155 return to whoever called the current `thunk'.
29157 The effect must be as if FUNCTION had been called directly with the
29158 adjusted first argument. This macro is responsible for emitting
29159 all of the code for a thunk function; output_function_prologue()
29160 and output_function_epilogue() are not invoked.
29162 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
29163 been extracted from it.) It might possibly be useful on some
29164 targets, but probably not.
29166 If you do not define this macro, the target-independent code in the
29167 C++ frontend will generate a less efficient heavyweight thunk that
29168 calls FUNCTION instead of jumping to it. The generic approach does
29169 not support varargs. */
29171 static void
29172 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
29173 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
29174 tree function)
29176 rtx this_rtx, funexp;
29177 rtx_insn *insn;
29179 reload_completed = 1;
29180 epilogue_completed = 1;
29182 /* Mark the end of the (empty) prologue. */
29183 emit_note (NOTE_INSN_PROLOGUE_END);
29185 /* Find the "this" pointer. If the function returns a structure,
29186 the structure return pointer is in r3. */
29187 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
29188 this_rtx = gen_rtx_REG (Pmode, 4);
29189 else
29190 this_rtx = gen_rtx_REG (Pmode, 3);
29192 /* Apply the constant offset, if required. */
29193 if (delta)
29194 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
29196 /* Apply the offset from the vtable, if required. */
29197 if (vcall_offset)
29199 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
29200 rtx tmp = gen_rtx_REG (Pmode, 12);
29202 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
29203 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
29205 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
29206 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
29208 else
29210 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
29212 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
29214 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
29217 /* Generate a tail call to the target function. */
29218 if (!TREE_USED (function))
29220 assemble_external (function);
29221 TREE_USED (function) = 1;
29223 funexp = XEXP (DECL_RTL (function), 0);
29224 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
29226 #if TARGET_MACHO
29227 if (MACHOPIC_INDIRECT)
29228 funexp = machopic_indirect_call_target (funexp);
29229 #endif
29231 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
29232 generate sibcall RTL explicitly. */
29233 insn = emit_call_insn (
29234 gen_rtx_PARALLEL (VOIDmode,
29235 gen_rtvec (3,
29236 gen_rtx_CALL (VOIDmode,
29237 funexp, const0_rtx),
29238 gen_rtx_USE (VOIDmode, const0_rtx),
29239 simple_return_rtx)));
29240 SIBLING_CALL_P (insn) = 1;
29241 emit_barrier ();
29243 /* Run just enough of rest_of_compilation to get the insns emitted.
29244 There's not really enough bulk here to make other passes such as
29245 instruction scheduling worth while. Note that use_thunk calls
29246 assemble_start_function and assemble_end_function. */
29247 insn = get_insns ();
29248 shorten_branches (insn);
29249 final_start_function (insn, file, 1);
29250 final (insn, file, 1);
29251 final_end_function ();
29253 reload_completed = 0;
29254 epilogue_completed = 0;
29257 /* A quick summary of the various types of 'constant-pool tables'
29258 under PowerPC:
29260 Target Flags Name One table per
29261 AIX (none) AIX TOC object file
29262 AIX -mfull-toc AIX TOC object file
29263 AIX -mminimal-toc AIX minimal TOC translation unit
29264 SVR4/EABI (none) SVR4 SDATA object file
29265 SVR4/EABI -fpic SVR4 pic object file
29266 SVR4/EABI -fPIC SVR4 PIC translation unit
29267 SVR4/EABI -mrelocatable EABI TOC function
29268 SVR4/EABI -maix AIX TOC object file
29269 SVR4/EABI -maix -mminimal-toc
29270 AIX minimal TOC translation unit
29272 Name Reg. Set by entries contains:
29273 made by addrs? fp? sum?
29275 AIX TOC 2 crt0 as Y option option
29276 AIX minimal TOC 30 prolog gcc Y Y option
29277 SVR4 SDATA 13 crt0 gcc N Y N
29278 SVR4 pic 30 prolog ld Y not yet N
29279 SVR4 PIC 30 prolog gcc Y option option
29280 EABI TOC 30 prolog gcc Y option option
29284 /* Hash functions for the hash table. */
29286 static unsigned
29287 rs6000_hash_constant (rtx k)
29289 enum rtx_code code = GET_CODE (k);
29290 machine_mode mode = GET_MODE (k);
29291 unsigned result = (code << 3) ^ mode;
29292 const char *format;
29293 int flen, fidx;
29295 format = GET_RTX_FORMAT (code);
29296 flen = strlen (format);
29297 fidx = 0;
29299 switch (code)
29301 case LABEL_REF:
29302 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
29304 case CONST_WIDE_INT:
29306 int i;
29307 flen = CONST_WIDE_INT_NUNITS (k);
29308 for (i = 0; i < flen; i++)
29309 result = result * 613 + CONST_WIDE_INT_ELT (k, i);
29310 return result;
29313 case CONST_DOUBLE:
29314 if (mode != VOIDmode)
29315 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
29316 flen = 2;
29317 break;
29319 case CODE_LABEL:
29320 fidx = 3;
29321 break;
29323 default:
29324 break;
29327 for (; fidx < flen; fidx++)
29328 switch (format[fidx])
29330 case 's':
29332 unsigned i, len;
29333 const char *str = XSTR (k, fidx);
29334 len = strlen (str);
29335 result = result * 613 + len;
29336 for (i = 0; i < len; i++)
29337 result = result * 613 + (unsigned) str[i];
29338 break;
29340 case 'u':
29341 case 'e':
29342 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
29343 break;
29344 case 'i':
29345 case 'n':
29346 result = result * 613 + (unsigned) XINT (k, fidx);
29347 break;
29348 case 'w':
29349 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
29350 result = result * 613 + (unsigned) XWINT (k, fidx);
29351 else
29353 size_t i;
29354 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
29355 result = result * 613 + (unsigned) (XWINT (k, fidx)
29356 >> CHAR_BIT * i);
29358 break;
29359 case '0':
29360 break;
29361 default:
29362 gcc_unreachable ();
29365 return result;
29368 hashval_t
29369 toc_hasher::hash (toc_hash_struct *thc)
29371 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
29374 /* Compare H1 and H2 for equivalence. */
29376 bool
29377 toc_hasher::equal (toc_hash_struct *h1, toc_hash_struct *h2)
29379 rtx r1 = h1->key;
29380 rtx r2 = h2->key;
29382 if (h1->key_mode != h2->key_mode)
29383 return 0;
29385 return rtx_equal_p (r1, r2);
29388 /* These are the names given by the C++ front-end to vtables, and
29389 vtable-like objects. Ideally, this logic should not be here;
29390 instead, there should be some programmatic way of inquiring as
29391 to whether or not an object is a vtable. */
29393 #define VTABLE_NAME_P(NAME) \
29394 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
29395 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
29396 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
29397 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
29398 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
29400 #ifdef NO_DOLLAR_IN_LABEL
29401 /* Return a GGC-allocated character string translating dollar signs in
29402 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
29404 const char *
29405 rs6000_xcoff_strip_dollar (const char *name)
29407 char *strip, *p;
29408 const char *q;
29409 size_t len;
29411 q = (const char *) strchr (name, '$');
29413 if (q == 0 || q == name)
29414 return name;
29416 len = strlen (name);
29417 strip = XALLOCAVEC (char, len + 1);
29418 strcpy (strip, name);
29419 p = strip + (q - name);
29420 while (p)
29422 *p = '_';
29423 p = strchr (p + 1, '$');
29426 return ggc_alloc_string (strip, len);
29428 #endif
29430 void
29431 rs6000_output_symbol_ref (FILE *file, rtx x)
29433 const char *name = XSTR (x, 0);
29435 /* Currently C++ toc references to vtables can be emitted before it
29436 is decided whether the vtable is public or private. If this is
29437 the case, then the linker will eventually complain that there is
29438 a reference to an unknown section. Thus, for vtables only,
29439 we emit the TOC reference to reference the identifier and not the
29440 symbol. */
29441 if (VTABLE_NAME_P (name))
29443 RS6000_OUTPUT_BASENAME (file, name);
29445 else
29446 assemble_name (file, name);
29449 /* Output a TOC entry. We derive the entry name from what is being
29450 written. */
29452 void
29453 output_toc (FILE *file, rtx x, int labelno, machine_mode mode)
29455 char buf[256];
29456 const char *name = buf;
29457 rtx base = x;
29458 HOST_WIDE_INT offset = 0;
29460 gcc_assert (!TARGET_NO_TOC);
29462 /* When the linker won't eliminate them, don't output duplicate
29463 TOC entries (this happens on AIX if there is any kind of TOC,
29464 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
29465 CODE_LABELs. */
29466 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
29468 struct toc_hash_struct *h;
29470 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
29471 time because GGC is not initialized at that point. */
29472 if (toc_hash_table == NULL)
29473 toc_hash_table = hash_table<toc_hasher>::create_ggc (1021);
29475 h = ggc_alloc<toc_hash_struct> ();
29476 h->key = x;
29477 h->key_mode = mode;
29478 h->labelno = labelno;
29480 toc_hash_struct **found = toc_hash_table->find_slot (h, INSERT);
29481 if (*found == NULL)
29482 *found = h;
29483 else /* This is indeed a duplicate.
29484 Set this label equal to that label. */
29486 fputs ("\t.set ", file);
29487 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
29488 fprintf (file, "%d,", labelno);
29489 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
29490 fprintf (file, "%d\n", ((*found)->labelno));
29492 #ifdef HAVE_AS_TLS
29493 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
29494 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
29495 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
29497 fputs ("\t.set ", file);
29498 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
29499 fprintf (file, "%d,", labelno);
29500 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
29501 fprintf (file, "%d\n", ((*found)->labelno));
29503 #endif
29504 return;
29508 /* If we're going to put a double constant in the TOC, make sure it's
29509 aligned properly when strict alignment is on. */
29510 if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
29511 && STRICT_ALIGNMENT
29512 && GET_MODE_BITSIZE (mode) >= 64
29513 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
29514 ASM_OUTPUT_ALIGN (file, 3);
29517 (*targetm.asm_out.internal_label) (file, "LC", labelno);
29519 /* Handle FP constants specially. Note that if we have a minimal
29520 TOC, things we put here aren't actually in the TOC, so we can allow
29521 FP constants. */
29522 if (GET_CODE (x) == CONST_DOUBLE &&
29523 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode
29524 || GET_MODE (x) == IFmode || GET_MODE (x) == KFmode))
29526 long k[4];
29528 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
29529 REAL_VALUE_TO_TARGET_DECIMAL128 (*CONST_DOUBLE_REAL_VALUE (x), k);
29530 else
29531 REAL_VALUE_TO_TARGET_LONG_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
29533 if (TARGET_64BIT)
29535 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29536 fputs (DOUBLE_INT_ASM_OP, file);
29537 else
29538 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
29539 k[0] & 0xffffffff, k[1] & 0xffffffff,
29540 k[2] & 0xffffffff, k[3] & 0xffffffff);
29541 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
29542 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
29543 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
29544 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
29545 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
29546 return;
29548 else
29550 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29551 fputs ("\t.long ", file);
29552 else
29553 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
29554 k[0] & 0xffffffff, k[1] & 0xffffffff,
29555 k[2] & 0xffffffff, k[3] & 0xffffffff);
29556 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
29557 k[0] & 0xffffffff, k[1] & 0xffffffff,
29558 k[2] & 0xffffffff, k[3] & 0xffffffff);
29559 return;
29562 else if (GET_CODE (x) == CONST_DOUBLE &&
29563 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
29565 long k[2];
29567 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
29568 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (x), k);
29569 else
29570 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
29572 if (TARGET_64BIT)
29574 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29575 fputs (DOUBLE_INT_ASM_OP, file);
29576 else
29577 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
29578 k[0] & 0xffffffff, k[1] & 0xffffffff);
29579 fprintf (file, "0x%lx%08lx\n",
29580 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
29581 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
29582 return;
29584 else
29586 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29587 fputs ("\t.long ", file);
29588 else
29589 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
29590 k[0] & 0xffffffff, k[1] & 0xffffffff);
29591 fprintf (file, "0x%lx,0x%lx\n",
29592 k[0] & 0xffffffff, k[1] & 0xffffffff);
29593 return;
29596 else if (GET_CODE (x) == CONST_DOUBLE &&
29597 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
29599 long l;
29601 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
29602 REAL_VALUE_TO_TARGET_DECIMAL32 (*CONST_DOUBLE_REAL_VALUE (x), l);
29603 else
29604 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
29606 if (TARGET_64BIT)
29608 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29609 fputs (DOUBLE_INT_ASM_OP, file);
29610 else
29611 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
29612 if (WORDS_BIG_ENDIAN)
29613 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
29614 else
29615 fprintf (file, "0x%lx\n", l & 0xffffffff);
29616 return;
29618 else
29620 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29621 fputs ("\t.long ", file);
29622 else
29623 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
29624 fprintf (file, "0x%lx\n", l & 0xffffffff);
29625 return;
29628 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
29630 unsigned HOST_WIDE_INT low;
29631 HOST_WIDE_INT high;
29633 low = INTVAL (x) & 0xffffffff;
29634 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
29636 /* TOC entries are always Pmode-sized, so when big-endian
29637 smaller integer constants in the TOC need to be padded.
29638 (This is still a win over putting the constants in
29639 a separate constant pool, because then we'd have
29640 to have both a TOC entry _and_ the actual constant.)
29642 For a 32-bit target, CONST_INT values are loaded and shifted
29643 entirely within `low' and can be stored in one TOC entry. */
29645 /* It would be easy to make this work, but it doesn't now. */
29646 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
29648 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
29650 low |= high << 32;
29651 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
29652 high = (HOST_WIDE_INT) low >> 32;
29653 low &= 0xffffffff;
29656 if (TARGET_64BIT)
29658 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29659 fputs (DOUBLE_INT_ASM_OP, file);
29660 else
29661 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
29662 (long) high & 0xffffffff, (long) low & 0xffffffff);
29663 fprintf (file, "0x%lx%08lx\n",
29664 (long) high & 0xffffffff, (long) low & 0xffffffff);
29665 return;
29667 else
29669 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
29671 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29672 fputs ("\t.long ", file);
29673 else
29674 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
29675 (long) high & 0xffffffff, (long) low & 0xffffffff);
29676 fprintf (file, "0x%lx,0x%lx\n",
29677 (long) high & 0xffffffff, (long) low & 0xffffffff);
29679 else
29681 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29682 fputs ("\t.long ", file);
29683 else
29684 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
29685 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
29687 return;
29691 if (GET_CODE (x) == CONST)
29693 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
29694 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
29696 base = XEXP (XEXP (x, 0), 0);
29697 offset = INTVAL (XEXP (XEXP (x, 0), 1));
29700 switch (GET_CODE (base))
29702 case SYMBOL_REF:
29703 name = XSTR (base, 0);
29704 break;
29706 case LABEL_REF:
29707 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
29708 CODE_LABEL_NUMBER (XEXP (base, 0)));
29709 break;
29711 case CODE_LABEL:
29712 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
29713 break;
29715 default:
29716 gcc_unreachable ();
29719 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29720 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
29721 else
29723 fputs ("\t.tc ", file);
29724 RS6000_OUTPUT_BASENAME (file, name);
29726 if (offset < 0)
29727 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
29728 else if (offset)
29729 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
29731 /* Mark large TOC symbols on AIX with [TE] so they are mapped
29732 after other TOC symbols, reducing overflow of small TOC access
29733 to [TC] symbols. */
29734 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
29735 ? "[TE]," : "[TC],", file);
29738 /* Currently C++ toc references to vtables can be emitted before it
29739 is decided whether the vtable is public or private. If this is
29740 the case, then the linker will eventually complain that there is
29741 a TOC reference to an unknown section. Thus, for vtables only,
29742 we emit the TOC reference to reference the symbol and not the
29743 section. */
29744 if (VTABLE_NAME_P (name))
29746 RS6000_OUTPUT_BASENAME (file, name);
29747 if (offset < 0)
29748 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
29749 else if (offset > 0)
29750 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
29752 else
29753 output_addr_const (file, x);
29755 #if HAVE_AS_TLS
29756 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF)
29758 switch (SYMBOL_REF_TLS_MODEL (base))
29760 case 0:
29761 break;
29762 case TLS_MODEL_LOCAL_EXEC:
29763 fputs ("@le", file);
29764 break;
29765 case TLS_MODEL_INITIAL_EXEC:
29766 fputs ("@ie", file);
29767 break;
29768 /* Use global-dynamic for local-dynamic. */
29769 case TLS_MODEL_GLOBAL_DYNAMIC:
29770 case TLS_MODEL_LOCAL_DYNAMIC:
29771 putc ('\n', file);
29772 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
29773 fputs ("\t.tc .", file);
29774 RS6000_OUTPUT_BASENAME (file, name);
29775 fputs ("[TC],", file);
29776 output_addr_const (file, x);
29777 fputs ("@m", file);
29778 break;
29779 default:
29780 gcc_unreachable ();
29783 #endif
29785 putc ('\n', file);
29788 /* Output an assembler pseudo-op to write an ASCII string of N characters
29789 starting at P to FILE.
29791 On the RS/6000, we have to do this using the .byte operation and
29792 write out special characters outside the quoted string.
29793 Also, the assembler is broken; very long strings are truncated,
29794 so we must artificially break them up early. */
29796 void
29797 output_ascii (FILE *file, const char *p, int n)
29799 char c;
29800 int i, count_string;
29801 const char *for_string = "\t.byte \"";
29802 const char *for_decimal = "\t.byte ";
29803 const char *to_close = NULL;
29805 count_string = 0;
29806 for (i = 0; i < n; i++)
29808 c = *p++;
29809 if (c >= ' ' && c < 0177)
29811 if (for_string)
29812 fputs (for_string, file);
29813 putc (c, file);
29815 /* Write two quotes to get one. */
29816 if (c == '"')
29818 putc (c, file);
29819 ++count_string;
29822 for_string = NULL;
29823 for_decimal = "\"\n\t.byte ";
29824 to_close = "\"\n";
29825 ++count_string;
29827 if (count_string >= 512)
29829 fputs (to_close, file);
29831 for_string = "\t.byte \"";
29832 for_decimal = "\t.byte ";
29833 to_close = NULL;
29834 count_string = 0;
29837 else
29839 if (for_decimal)
29840 fputs (for_decimal, file);
29841 fprintf (file, "%d", c);
29843 for_string = "\n\t.byte \"";
29844 for_decimal = ", ";
29845 to_close = "\n";
29846 count_string = 0;
29850 /* Now close the string if we have written one. Then end the line. */
29851 if (to_close)
29852 fputs (to_close, file);
29855 /* Generate a unique section name for FILENAME for a section type
29856 represented by SECTION_DESC. Output goes into BUF.
29858 SECTION_DESC can be any string, as long as it is different for each
29859 possible section type.
29861 We name the section in the same manner as xlc. The name begins with an
29862 underscore followed by the filename (after stripping any leading directory
29863 names) with the last period replaced by the string SECTION_DESC. If
29864 FILENAME does not contain a period, SECTION_DESC is appended to the end of
29865 the name. */
29867 void
29868 rs6000_gen_section_name (char **buf, const char *filename,
29869 const char *section_desc)
29871 const char *q, *after_last_slash, *last_period = 0;
29872 char *p;
29873 int len;
29875 after_last_slash = filename;
29876 for (q = filename; *q; q++)
29878 if (*q == '/')
29879 after_last_slash = q + 1;
29880 else if (*q == '.')
29881 last_period = q;
29884 len = strlen (after_last_slash) + strlen (section_desc) + 2;
29885 *buf = (char *) xmalloc (len);
29887 p = *buf;
29888 *p++ = '_';
29890 for (q = after_last_slash; *q; q++)
29892 if (q == last_period)
29894 strcpy (p, section_desc);
29895 p += strlen (section_desc);
29896 break;
29899 else if (ISALNUM (*q))
29900 *p++ = *q;
29903 if (last_period == 0)
29904 strcpy (p, section_desc);
29905 else
29906 *p = '\0';
29909 /* Emit profile function. */
29911 void
29912 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
29914 /* Non-standard profiling for kernels, which just saves LR then calls
29915 _mcount without worrying about arg saves. The idea is to change
29916 the function prologue as little as possible as it isn't easy to
29917 account for arg save/restore code added just for _mcount. */
29918 if (TARGET_PROFILE_KERNEL)
29919 return;
29921 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29923 #ifndef NO_PROFILE_COUNTERS
29924 # define NO_PROFILE_COUNTERS 0
29925 #endif
29926 if (NO_PROFILE_COUNTERS)
29927 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
29928 LCT_NORMAL, VOIDmode, 0);
29929 else
29931 char buf[30];
29932 const char *label_name;
29933 rtx fun;
29935 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
29936 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
29937 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
29939 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
29940 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
29943 else if (DEFAULT_ABI == ABI_DARWIN)
29945 const char *mcount_name = RS6000_MCOUNT;
29946 int caller_addr_regno = LR_REGNO;
29948 /* Be conservative and always set this, at least for now. */
29949 crtl->uses_pic_offset_table = 1;
29951 #if TARGET_MACHO
29952 /* For PIC code, set up a stub and collect the caller's address
29953 from r0, which is where the prologue puts it. */
29954 if (MACHOPIC_INDIRECT
29955 && crtl->uses_pic_offset_table)
29956 caller_addr_regno = 0;
29957 #endif
29958 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
29959 LCT_NORMAL, VOIDmode, 1,
29960 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
29964 /* Write function profiler code. */
29966 void
29967 output_function_profiler (FILE *file, int labelno)
29969 char buf[100];
29971 switch (DEFAULT_ABI)
29973 default:
29974 gcc_unreachable ();
29976 case ABI_V4:
29977 if (!TARGET_32BIT)
29979 warning (0, "no profiling of 64-bit code for this ABI");
29980 return;
29982 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
29983 fprintf (file, "\tmflr %s\n", reg_names[0]);
29984 if (NO_PROFILE_COUNTERS)
29986 asm_fprintf (file, "\tstw %s,4(%s)\n",
29987 reg_names[0], reg_names[1]);
29989 else if (TARGET_SECURE_PLT && flag_pic)
29991 if (TARGET_LINK_STACK)
29993 char name[32];
29994 get_ppc476_thunk_name (name);
29995 asm_fprintf (file, "\tbl %s\n", name);
29997 else
29998 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
29999 asm_fprintf (file, "\tstw %s,4(%s)\n",
30000 reg_names[0], reg_names[1]);
30001 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
30002 asm_fprintf (file, "\taddis %s,%s,",
30003 reg_names[12], reg_names[12]);
30004 assemble_name (file, buf);
30005 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
30006 assemble_name (file, buf);
30007 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
30009 else if (flag_pic == 1)
30011 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
30012 asm_fprintf (file, "\tstw %s,4(%s)\n",
30013 reg_names[0], reg_names[1]);
30014 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
30015 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
30016 assemble_name (file, buf);
30017 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
30019 else if (flag_pic > 1)
30021 asm_fprintf (file, "\tstw %s,4(%s)\n",
30022 reg_names[0], reg_names[1]);
30023 /* Now, we need to get the address of the label. */
30024 if (TARGET_LINK_STACK)
30026 char name[32];
30027 get_ppc476_thunk_name (name);
30028 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
30029 assemble_name (file, buf);
30030 fputs ("-.\n1:", file);
30031 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
30032 asm_fprintf (file, "\taddi %s,%s,4\n",
30033 reg_names[11], reg_names[11]);
30035 else
30037 fputs ("\tbcl 20,31,1f\n\t.long ", file);
30038 assemble_name (file, buf);
30039 fputs ("-.\n1:", file);
30040 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
30042 asm_fprintf (file, "\tlwz %s,0(%s)\n",
30043 reg_names[0], reg_names[11]);
30044 asm_fprintf (file, "\tadd %s,%s,%s\n",
30045 reg_names[0], reg_names[0], reg_names[11]);
30047 else
30049 asm_fprintf (file, "\tlis %s,", reg_names[12]);
30050 assemble_name (file, buf);
30051 fputs ("@ha\n", file);
30052 asm_fprintf (file, "\tstw %s,4(%s)\n",
30053 reg_names[0], reg_names[1]);
30054 asm_fprintf (file, "\tla %s,", reg_names[0]);
30055 assemble_name (file, buf);
30056 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
30059 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
30060 fprintf (file, "\tbl %s%s\n",
30061 RS6000_MCOUNT, flag_pic ? "@plt" : "");
30062 break;
30064 case ABI_AIX:
30065 case ABI_ELFv2:
30066 case ABI_DARWIN:
30067 /* Don't do anything, done in output_profile_hook (). */
30068 break;
30074 /* The following variable value is the last issued insn. */
30076 static rtx_insn *last_scheduled_insn;
30078 /* The following variable helps to balance issuing of load and
30079 store instructions */
30081 static int load_store_pendulum;
30083 /* The following variable helps pair divide insns during scheduling. */
30084 static int divide_cnt;
30085 /* The following variable helps pair and alternate vector and vector load
30086 insns during scheduling. */
30087 static int vec_pairing;
30090 /* Power4 load update and store update instructions are cracked into a
30091 load or store and an integer insn which are executed in the same cycle.
30092 Branches have their own dispatch slot which does not count against the
30093 GCC issue rate, but it changes the program flow so there are no other
30094 instructions to issue in this cycle. */
30096 static int
30097 rs6000_variable_issue_1 (rtx_insn *insn, int more)
30099 last_scheduled_insn = insn;
30100 if (GET_CODE (PATTERN (insn)) == USE
30101 || GET_CODE (PATTERN (insn)) == CLOBBER)
30103 cached_can_issue_more = more;
30104 return cached_can_issue_more;
30107 if (insn_terminates_group_p (insn, current_group))
30109 cached_can_issue_more = 0;
30110 return cached_can_issue_more;
30113 /* If no reservation, but reach here */
30114 if (recog_memoized (insn) < 0)
30115 return more;
30117 if (rs6000_sched_groups)
30119 if (is_microcoded_insn (insn))
30120 cached_can_issue_more = 0;
30121 else if (is_cracked_insn (insn))
30122 cached_can_issue_more = more > 2 ? more - 2 : 0;
30123 else
30124 cached_can_issue_more = more - 1;
30126 return cached_can_issue_more;
30129 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
30130 return 0;
30132 cached_can_issue_more = more - 1;
30133 return cached_can_issue_more;
30136 static int
30137 rs6000_variable_issue (FILE *stream, int verbose, rtx_insn *insn, int more)
30139 int r = rs6000_variable_issue_1 (insn, more);
30140 if (verbose)
30141 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
30142 return r;
30145 /* Adjust the cost of a scheduling dependency. Return the new cost of
30146 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
30148 static int
30149 rs6000_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
30150 unsigned int)
30152 enum attr_type attr_type;
30154 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
30155 return cost;
30157 switch (dep_type)
30159 case REG_DEP_TRUE:
30161 /* Data dependency; DEP_INSN writes a register that INSN reads
30162 some cycles later. */
30164 /* Separate a load from a narrower, dependent store. */
30165 if ((rs6000_sched_groups || rs6000_cpu_attr == CPU_POWER9)
30166 && GET_CODE (PATTERN (insn)) == SET
30167 && GET_CODE (PATTERN (dep_insn)) == SET
30168 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
30169 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
30170 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
30171 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
30172 return cost + 14;
30174 attr_type = get_attr_type (insn);
30176 switch (attr_type)
30178 case TYPE_JMPREG:
30179 /* Tell the first scheduling pass about the latency between
30180 a mtctr and bctr (and mtlr and br/blr). The first
30181 scheduling pass will not know about this latency since
30182 the mtctr instruction, which has the latency associated
30183 to it, will be generated by reload. */
30184 return 4;
30185 case TYPE_BRANCH:
30186 /* Leave some extra cycles between a compare and its
30187 dependent branch, to inhibit expensive mispredicts. */
30188 if ((rs6000_cpu_attr == CPU_PPC603
30189 || rs6000_cpu_attr == CPU_PPC604
30190 || rs6000_cpu_attr == CPU_PPC604E
30191 || rs6000_cpu_attr == CPU_PPC620
30192 || rs6000_cpu_attr == CPU_PPC630
30193 || rs6000_cpu_attr == CPU_PPC750
30194 || rs6000_cpu_attr == CPU_PPC7400
30195 || rs6000_cpu_attr == CPU_PPC7450
30196 || rs6000_cpu_attr == CPU_PPCE5500
30197 || rs6000_cpu_attr == CPU_PPCE6500
30198 || rs6000_cpu_attr == CPU_POWER4
30199 || rs6000_cpu_attr == CPU_POWER5
30200 || rs6000_cpu_attr == CPU_POWER7
30201 || rs6000_cpu_attr == CPU_POWER8
30202 || rs6000_cpu_attr == CPU_POWER9
30203 || rs6000_cpu_attr == CPU_CELL)
30204 && recog_memoized (dep_insn)
30205 && (INSN_CODE (dep_insn) >= 0))
30207 switch (get_attr_type (dep_insn))
30209 case TYPE_CMP:
30210 case TYPE_FPCOMPARE:
30211 case TYPE_CR_LOGICAL:
30212 case TYPE_DELAYED_CR:
30213 return cost + 2;
30214 case TYPE_EXTS:
30215 case TYPE_MUL:
30216 if (get_attr_dot (dep_insn) == DOT_YES)
30217 return cost + 2;
30218 else
30219 break;
30220 case TYPE_SHIFT:
30221 if (get_attr_dot (dep_insn) == DOT_YES
30222 && get_attr_var_shift (dep_insn) == VAR_SHIFT_NO)
30223 return cost + 2;
30224 else
30225 break;
30226 default:
30227 break;
30229 break;
30231 case TYPE_STORE:
30232 case TYPE_FPSTORE:
30233 if ((rs6000_cpu == PROCESSOR_POWER6)
30234 && recog_memoized (dep_insn)
30235 && (INSN_CODE (dep_insn) >= 0))
30238 if (GET_CODE (PATTERN (insn)) != SET)
30239 /* If this happens, we have to extend this to schedule
30240 optimally. Return default for now. */
30241 return cost;
30243 /* Adjust the cost for the case where the value written
30244 by a fixed point operation is used as the address
30245 gen value on a store. */
30246 switch (get_attr_type (dep_insn))
30248 case TYPE_LOAD:
30249 case TYPE_CNTLZ:
30251 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30252 return get_attr_sign_extend (dep_insn)
30253 == SIGN_EXTEND_YES ? 6 : 4;
30254 break;
30256 case TYPE_SHIFT:
30258 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30259 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
30260 6 : 3;
30261 break;
30263 case TYPE_INTEGER:
30264 case TYPE_ADD:
30265 case TYPE_LOGICAL:
30266 case TYPE_EXTS:
30267 case TYPE_INSERT:
30269 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30270 return 3;
30271 break;
30273 case TYPE_STORE:
30274 case TYPE_FPLOAD:
30275 case TYPE_FPSTORE:
30277 if (get_attr_update (dep_insn) == UPDATE_YES
30278 && ! rs6000_store_data_bypass_p (dep_insn, insn))
30279 return 3;
30280 break;
30282 case TYPE_MUL:
30284 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30285 return 17;
30286 break;
30288 case TYPE_DIV:
30290 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30291 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
30292 break;
30294 default:
30295 break;
30298 break;
30300 case TYPE_LOAD:
30301 if ((rs6000_cpu == PROCESSOR_POWER6)
30302 && recog_memoized (dep_insn)
30303 && (INSN_CODE (dep_insn) >= 0))
30306 /* Adjust the cost for the case where the value written
30307 by a fixed point instruction is used within the address
30308 gen portion of a subsequent load(u)(x) */
30309 switch (get_attr_type (dep_insn))
30311 case TYPE_LOAD:
30312 case TYPE_CNTLZ:
30314 if (set_to_load_agen (dep_insn, insn))
30315 return get_attr_sign_extend (dep_insn)
30316 == SIGN_EXTEND_YES ? 6 : 4;
30317 break;
30319 case TYPE_SHIFT:
30321 if (set_to_load_agen (dep_insn, insn))
30322 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
30323 6 : 3;
30324 break;
30326 case TYPE_INTEGER:
30327 case TYPE_ADD:
30328 case TYPE_LOGICAL:
30329 case TYPE_EXTS:
30330 case TYPE_INSERT:
30332 if (set_to_load_agen (dep_insn, insn))
30333 return 3;
30334 break;
30336 case TYPE_STORE:
30337 case TYPE_FPLOAD:
30338 case TYPE_FPSTORE:
30340 if (get_attr_update (dep_insn) == UPDATE_YES
30341 && set_to_load_agen (dep_insn, insn))
30342 return 3;
30343 break;
30345 case TYPE_MUL:
30347 if (set_to_load_agen (dep_insn, insn))
30348 return 17;
30349 break;
30351 case TYPE_DIV:
30353 if (set_to_load_agen (dep_insn, insn))
30354 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
30355 break;
30357 default:
30358 break;
30361 break;
30363 case TYPE_FPLOAD:
30364 if ((rs6000_cpu == PROCESSOR_POWER6)
30365 && get_attr_update (insn) == UPDATE_NO
30366 && recog_memoized (dep_insn)
30367 && (INSN_CODE (dep_insn) >= 0)
30368 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
30369 return 2;
30371 default:
30372 break;
30375 /* Fall out to return default cost. */
30377 break;
30379 case REG_DEP_OUTPUT:
30380 /* Output dependency; DEP_INSN writes a register that INSN writes some
30381 cycles later. */
30382 if ((rs6000_cpu == PROCESSOR_POWER6)
30383 && recog_memoized (dep_insn)
30384 && (INSN_CODE (dep_insn) >= 0))
30386 attr_type = get_attr_type (insn);
30388 switch (attr_type)
30390 case TYPE_FP:
30391 case TYPE_FPSIMPLE:
30392 if (get_attr_type (dep_insn) == TYPE_FP
30393 || get_attr_type (dep_insn) == TYPE_FPSIMPLE)
30394 return 1;
30395 break;
30396 case TYPE_FPLOAD:
30397 if (get_attr_update (insn) == UPDATE_NO
30398 && get_attr_type (dep_insn) == TYPE_MFFGPR)
30399 return 2;
30400 break;
30401 default:
30402 break;
30405 /* Fall through, no cost for output dependency. */
30406 /* FALLTHRU */
30408 case REG_DEP_ANTI:
30409 /* Anti dependency; DEP_INSN reads a register that INSN writes some
30410 cycles later. */
30411 return 0;
30413 default:
30414 gcc_unreachable ();
30417 return cost;
30420 /* Debug version of rs6000_adjust_cost. */
30422 static int
30423 rs6000_debug_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn,
30424 int cost, unsigned int dw)
30426 int ret = rs6000_adjust_cost (insn, dep_type, dep_insn, cost, dw);
30428 if (ret != cost)
30430 const char *dep;
30432 switch (dep_type)
30434 default: dep = "unknown depencency"; break;
30435 case REG_DEP_TRUE: dep = "data dependency"; break;
30436 case REG_DEP_OUTPUT: dep = "output dependency"; break;
30437 case REG_DEP_ANTI: dep = "anti depencency"; break;
30440 fprintf (stderr,
30441 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
30442 "%s, insn:\n", ret, cost, dep);
30444 debug_rtx (insn);
30447 return ret;
30450 /* The function returns a true if INSN is microcoded.
30451 Return false otherwise. */
30453 static bool
30454 is_microcoded_insn (rtx_insn *insn)
30456 if (!insn || !NONDEBUG_INSN_P (insn)
30457 || GET_CODE (PATTERN (insn)) == USE
30458 || GET_CODE (PATTERN (insn)) == CLOBBER)
30459 return false;
30461 if (rs6000_cpu_attr == CPU_CELL)
30462 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
30464 if (rs6000_sched_groups
30465 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
30467 enum attr_type type = get_attr_type (insn);
30468 if ((type == TYPE_LOAD
30469 && get_attr_update (insn) == UPDATE_YES
30470 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
30471 || ((type == TYPE_LOAD || type == TYPE_STORE)
30472 && get_attr_update (insn) == UPDATE_YES
30473 && get_attr_indexed (insn) == INDEXED_YES)
30474 || type == TYPE_MFCR)
30475 return true;
30478 return false;
30481 /* The function returns true if INSN is cracked into 2 instructions
30482 by the processor (and therefore occupies 2 issue slots). */
30484 static bool
30485 is_cracked_insn (rtx_insn *insn)
30487 if (!insn || !NONDEBUG_INSN_P (insn)
30488 || GET_CODE (PATTERN (insn)) == USE
30489 || GET_CODE (PATTERN (insn)) == CLOBBER)
30490 return false;
30492 if (rs6000_sched_groups
30493 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
30495 enum attr_type type = get_attr_type (insn);
30496 if ((type == TYPE_LOAD
30497 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
30498 && get_attr_update (insn) == UPDATE_NO)
30499 || (type == TYPE_LOAD
30500 && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
30501 && get_attr_update (insn) == UPDATE_YES
30502 && get_attr_indexed (insn) == INDEXED_NO)
30503 || (type == TYPE_STORE
30504 && get_attr_update (insn) == UPDATE_YES
30505 && get_attr_indexed (insn) == INDEXED_NO)
30506 || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
30507 && get_attr_update (insn) == UPDATE_YES)
30508 || type == TYPE_DELAYED_CR
30509 || (type == TYPE_EXTS
30510 && get_attr_dot (insn) == DOT_YES)
30511 || (type == TYPE_SHIFT
30512 && get_attr_dot (insn) == DOT_YES
30513 && get_attr_var_shift (insn) == VAR_SHIFT_NO)
30514 || (type == TYPE_MUL
30515 && get_attr_dot (insn) == DOT_YES)
30516 || type == TYPE_DIV
30517 || (type == TYPE_INSERT
30518 && get_attr_size (insn) == SIZE_32))
30519 return true;
30522 return false;
30525 /* The function returns true if INSN can be issued only from
30526 the branch slot. */
30528 static bool
30529 is_branch_slot_insn (rtx_insn *insn)
30531 if (!insn || !NONDEBUG_INSN_P (insn)
30532 || GET_CODE (PATTERN (insn)) == USE
30533 || GET_CODE (PATTERN (insn)) == CLOBBER)
30534 return false;
30536 if (rs6000_sched_groups)
30538 enum attr_type type = get_attr_type (insn);
30539 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
30540 return true;
30541 return false;
30544 return false;
30547 /* The function returns true if out_inst sets a value that is
30548 used in the address generation computation of in_insn */
30549 static bool
30550 set_to_load_agen (rtx_insn *out_insn, rtx_insn *in_insn)
30552 rtx out_set, in_set;
30554 /* For performance reasons, only handle the simple case where
30555 both loads are a single_set. */
30556 out_set = single_set (out_insn);
30557 if (out_set)
30559 in_set = single_set (in_insn);
30560 if (in_set)
30561 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
30564 return false;
30567 /* Try to determine base/offset/size parts of the given MEM.
30568 Return true if successful, false if all the values couldn't
30569 be determined.
30571 This function only looks for REG or REG+CONST address forms.
30572 REG+REG address form will return false. */
30574 static bool
30575 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
30576 HOST_WIDE_INT *size)
30578 rtx addr_rtx;
30579 if MEM_SIZE_KNOWN_P (mem)
30580 *size = MEM_SIZE (mem);
30581 else
30582 return false;
30584 addr_rtx = (XEXP (mem, 0));
30585 if (GET_CODE (addr_rtx) == PRE_MODIFY)
30586 addr_rtx = XEXP (addr_rtx, 1);
30588 *offset = 0;
30589 while (GET_CODE (addr_rtx) == PLUS
30590 && CONST_INT_P (XEXP (addr_rtx, 1)))
30592 *offset += INTVAL (XEXP (addr_rtx, 1));
30593 addr_rtx = XEXP (addr_rtx, 0);
30595 if (!REG_P (addr_rtx))
30596 return false;
30598 *base = addr_rtx;
30599 return true;
30602 /* The function returns true if the target storage location of
30603 mem1 is adjacent to the target storage location of mem2 */
30604 /* Return 1 if memory locations are adjacent. */
30606 static bool
30607 adjacent_mem_locations (rtx mem1, rtx mem2)
30609 rtx reg1, reg2;
30610 HOST_WIDE_INT off1, size1, off2, size2;
30612 if (get_memref_parts (mem1, &reg1, &off1, &size1)
30613 && get_memref_parts (mem2, &reg2, &off2, &size2))
30614 return ((REGNO (reg1) == REGNO (reg2))
30615 && ((off1 + size1 == off2)
30616 || (off2 + size2 == off1)));
30618 return false;
30621 /* This function returns true if it can be determined that the two MEM
30622 locations overlap by at least 1 byte based on base reg/offset/size. */
30624 static bool
30625 mem_locations_overlap (rtx mem1, rtx mem2)
30627 rtx reg1, reg2;
30628 HOST_WIDE_INT off1, size1, off2, size2;
30630 if (get_memref_parts (mem1, &reg1, &off1, &size1)
30631 && get_memref_parts (mem2, &reg2, &off2, &size2))
30632 return ((REGNO (reg1) == REGNO (reg2))
30633 && (((off1 <= off2) && (off1 + size1 > off2))
30634 || ((off2 <= off1) && (off2 + size2 > off1))));
30636 return false;
30639 /* A C statement (sans semicolon) to update the integer scheduling
30640 priority INSN_PRIORITY (INSN). Increase the priority to execute the
30641 INSN earlier, reduce the priority to execute INSN later. Do not
30642 define this macro if you do not need to adjust the scheduling
30643 priorities of insns. */
30645 static int
30646 rs6000_adjust_priority (rtx_insn *insn ATTRIBUTE_UNUSED, int priority)
30648 rtx load_mem, str_mem;
30649 /* On machines (like the 750) which have asymmetric integer units,
30650 where one integer unit can do multiply and divides and the other
30651 can't, reduce the priority of multiply/divide so it is scheduled
30652 before other integer operations. */
30654 #if 0
30655 if (! INSN_P (insn))
30656 return priority;
30658 if (GET_CODE (PATTERN (insn)) == USE)
30659 return priority;
30661 switch (rs6000_cpu_attr) {
30662 case CPU_PPC750:
30663 switch (get_attr_type (insn))
30665 default:
30666 break;
30668 case TYPE_MUL:
30669 case TYPE_DIV:
30670 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
30671 priority, priority);
30672 if (priority >= 0 && priority < 0x01000000)
30673 priority >>= 3;
30674 break;
30677 #endif
30679 if (insn_must_be_first_in_group (insn)
30680 && reload_completed
30681 && current_sched_info->sched_max_insns_priority
30682 && rs6000_sched_restricted_insns_priority)
30685 /* Prioritize insns that can be dispatched only in the first
30686 dispatch slot. */
30687 if (rs6000_sched_restricted_insns_priority == 1)
30688 /* Attach highest priority to insn. This means that in
30689 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
30690 precede 'priority' (critical path) considerations. */
30691 return current_sched_info->sched_max_insns_priority;
30692 else if (rs6000_sched_restricted_insns_priority == 2)
30693 /* Increase priority of insn by a minimal amount. This means that in
30694 haifa-sched.c:ready_sort(), only 'priority' (critical path)
30695 considerations precede dispatch-slot restriction considerations. */
30696 return (priority + 1);
30699 if (rs6000_cpu == PROCESSOR_POWER6
30700 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
30701 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
30702 /* Attach highest priority to insn if the scheduler has just issued two
30703 stores and this instruction is a load, or two loads and this instruction
30704 is a store. Power6 wants loads and stores scheduled alternately
30705 when possible */
30706 return current_sched_info->sched_max_insns_priority;
30708 return priority;
30711 /* Return true if the instruction is nonpipelined on the Cell. */
30712 static bool
30713 is_nonpipeline_insn (rtx_insn *insn)
30715 enum attr_type type;
30716 if (!insn || !NONDEBUG_INSN_P (insn)
30717 || GET_CODE (PATTERN (insn)) == USE
30718 || GET_CODE (PATTERN (insn)) == CLOBBER)
30719 return false;
30721 type = get_attr_type (insn);
30722 if (type == TYPE_MUL
30723 || type == TYPE_DIV
30724 || type == TYPE_SDIV
30725 || type == TYPE_DDIV
30726 || type == TYPE_SSQRT
30727 || type == TYPE_DSQRT
30728 || type == TYPE_MFCR
30729 || type == TYPE_MFCRF
30730 || type == TYPE_MFJMPR)
30732 return true;
30734 return false;
30738 /* Return how many instructions the machine can issue per cycle. */
30740 static int
30741 rs6000_issue_rate (void)
30743 /* Unless scheduling for register pressure, use issue rate of 1 for
30744 first scheduling pass to decrease degradation. */
30745 if (!reload_completed && !flag_sched_pressure)
30746 return 1;
30748 switch (rs6000_cpu_attr) {
30749 case CPU_RS64A:
30750 case CPU_PPC601: /* ? */
30751 case CPU_PPC7450:
30752 return 3;
30753 case CPU_PPC440:
30754 case CPU_PPC603:
30755 case CPU_PPC750:
30756 case CPU_PPC7400:
30757 case CPU_PPC8540:
30758 case CPU_PPC8548:
30759 case CPU_CELL:
30760 case CPU_PPCE300C2:
30761 case CPU_PPCE300C3:
30762 case CPU_PPCE500MC:
30763 case CPU_PPCE500MC64:
30764 case CPU_PPCE5500:
30765 case CPU_PPCE6500:
30766 case CPU_TITAN:
30767 return 2;
30768 case CPU_PPC476:
30769 case CPU_PPC604:
30770 case CPU_PPC604E:
30771 case CPU_PPC620:
30772 case CPU_PPC630:
30773 return 4;
30774 case CPU_POWER4:
30775 case CPU_POWER5:
30776 case CPU_POWER6:
30777 case CPU_POWER7:
30778 return 5;
30779 case CPU_POWER8:
30780 return 7;
30781 case CPU_POWER9:
30782 return 6;
30783 default:
30784 return 1;
30788 /* Return how many instructions to look ahead for better insn
30789 scheduling. */
30791 static int
30792 rs6000_use_sched_lookahead (void)
30794 switch (rs6000_cpu_attr)
30796 case CPU_PPC8540:
30797 case CPU_PPC8548:
30798 return 4;
30800 case CPU_CELL:
30801 return (reload_completed ? 8 : 0);
30803 default:
30804 return 0;
30808 /* We are choosing insn from the ready queue. Return zero if INSN can be
30809 chosen. */
30810 static int
30811 rs6000_use_sched_lookahead_guard (rtx_insn *insn, int ready_index)
30813 if (ready_index == 0)
30814 return 0;
30816 if (rs6000_cpu_attr != CPU_CELL)
30817 return 0;
30819 gcc_assert (insn != NULL_RTX && INSN_P (insn));
30821 if (!reload_completed
30822 || is_nonpipeline_insn (insn)
30823 || is_microcoded_insn (insn))
30824 return 1;
30826 return 0;
30829 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
30830 and return true. */
30832 static bool
30833 find_mem_ref (rtx pat, rtx *mem_ref)
30835 const char * fmt;
30836 int i, j;
30838 /* stack_tie does not produce any real memory traffic. */
30839 if (tie_operand (pat, VOIDmode))
30840 return false;
30842 if (GET_CODE (pat) == MEM)
30844 *mem_ref = pat;
30845 return true;
30848 /* Recursively process the pattern. */
30849 fmt = GET_RTX_FORMAT (GET_CODE (pat));
30851 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
30853 if (fmt[i] == 'e')
30855 if (find_mem_ref (XEXP (pat, i), mem_ref))
30856 return true;
30858 else if (fmt[i] == 'E')
30859 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
30861 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
30862 return true;
30866 return false;
30869 /* Determine if PAT is a PATTERN of a load insn. */
30871 static bool
30872 is_load_insn1 (rtx pat, rtx *load_mem)
30874 if (!pat || pat == NULL_RTX)
30875 return false;
30877 if (GET_CODE (pat) == SET)
30878 return find_mem_ref (SET_SRC (pat), load_mem);
30880 if (GET_CODE (pat) == PARALLEL)
30882 int i;
30884 for (i = 0; i < XVECLEN (pat, 0); i++)
30885 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
30886 return true;
30889 return false;
30892 /* Determine if INSN loads from memory. */
30894 static bool
30895 is_load_insn (rtx insn, rtx *load_mem)
30897 if (!insn || !INSN_P (insn))
30898 return false;
30900 if (CALL_P (insn))
30901 return false;
30903 return is_load_insn1 (PATTERN (insn), load_mem);
30906 /* Determine if PAT is a PATTERN of a store insn. */
30908 static bool
30909 is_store_insn1 (rtx pat, rtx *str_mem)
30911 if (!pat || pat == NULL_RTX)
30912 return false;
30914 if (GET_CODE (pat) == SET)
30915 return find_mem_ref (SET_DEST (pat), str_mem);
30917 if (GET_CODE (pat) == PARALLEL)
30919 int i;
30921 for (i = 0; i < XVECLEN (pat, 0); i++)
30922 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
30923 return true;
30926 return false;
30929 /* Determine if INSN stores to memory. */
30931 static bool
30932 is_store_insn (rtx insn, rtx *str_mem)
30934 if (!insn || !INSN_P (insn))
30935 return false;
30937 return is_store_insn1 (PATTERN (insn), str_mem);
30940 /* Return whether TYPE is a Power9 pairable vector instruction type. */
30942 static bool
30943 is_power9_pairable_vec_type (enum attr_type type)
30945 switch (type)
30947 case TYPE_VECSIMPLE:
30948 case TYPE_VECCOMPLEX:
30949 case TYPE_VECDIV:
30950 case TYPE_VECCMP:
30951 case TYPE_VECPERM:
30952 case TYPE_VECFLOAT:
30953 case TYPE_VECFDIV:
30954 case TYPE_VECDOUBLE:
30955 return true;
30956 default:
30957 break;
30959 return false;
30962 /* Returns whether the dependence between INSN and NEXT is considered
30963 costly by the given target. */
30965 static bool
30966 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
30968 rtx insn;
30969 rtx next;
30970 rtx load_mem, str_mem;
30972 /* If the flag is not enabled - no dependence is considered costly;
30973 allow all dependent insns in the same group.
30974 This is the most aggressive option. */
30975 if (rs6000_sched_costly_dep == no_dep_costly)
30976 return false;
30978 /* If the flag is set to 1 - a dependence is always considered costly;
30979 do not allow dependent instructions in the same group.
30980 This is the most conservative option. */
30981 if (rs6000_sched_costly_dep == all_deps_costly)
30982 return true;
30984 insn = DEP_PRO (dep);
30985 next = DEP_CON (dep);
30987 if (rs6000_sched_costly_dep == store_to_load_dep_costly
30988 && is_load_insn (next, &load_mem)
30989 && is_store_insn (insn, &str_mem))
30990 /* Prevent load after store in the same group. */
30991 return true;
30993 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
30994 && is_load_insn (next, &load_mem)
30995 && is_store_insn (insn, &str_mem)
30996 && DEP_TYPE (dep) == REG_DEP_TRUE
30997 && mem_locations_overlap(str_mem, load_mem))
30998 /* Prevent load after store in the same group if it is a true
30999 dependence. */
31000 return true;
31002 /* The flag is set to X; dependences with latency >= X are considered costly,
31003 and will not be scheduled in the same group. */
31004 if (rs6000_sched_costly_dep <= max_dep_latency
31005 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
31006 return true;
31008 return false;
31011 /* Return the next insn after INSN that is found before TAIL is reached,
31012 skipping any "non-active" insns - insns that will not actually occupy
31013 an issue slot. Return NULL_RTX if such an insn is not found. */
31015 static rtx_insn *
31016 get_next_active_insn (rtx_insn *insn, rtx_insn *tail)
31018 if (insn == NULL_RTX || insn == tail)
31019 return NULL;
31021 while (1)
31023 insn = NEXT_INSN (insn);
31024 if (insn == NULL_RTX || insn == tail)
31025 return NULL;
31027 if (CALL_P (insn)
31028 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
31029 || (NONJUMP_INSN_P (insn)
31030 && GET_CODE (PATTERN (insn)) != USE
31031 && GET_CODE (PATTERN (insn)) != CLOBBER
31032 && INSN_CODE (insn) != CODE_FOR_stack_tie))
31033 break;
31035 return insn;
31038 /* Do Power9 specific sched_reorder2 reordering of ready list. */
31040 static int
31041 power9_sched_reorder2 (rtx_insn **ready, int lastpos)
31043 int pos;
31044 int i;
31045 rtx_insn *tmp;
31046 enum attr_type type, type2;
31048 type = get_attr_type (last_scheduled_insn);
31050 /* Try to issue fixed point divides back-to-back in pairs so they will be
31051 routed to separate execution units and execute in parallel. */
31052 if (type == TYPE_DIV && divide_cnt == 0)
31054 /* First divide has been scheduled. */
31055 divide_cnt = 1;
31057 /* Scan the ready list looking for another divide, if found move it
31058 to the end of the list so it is chosen next. */
31059 pos = lastpos;
31060 while (pos >= 0)
31062 if (recog_memoized (ready[pos]) >= 0
31063 && get_attr_type (ready[pos]) == TYPE_DIV)
31065 tmp = ready[pos];
31066 for (i = pos; i < lastpos; i++)
31067 ready[i] = ready[i + 1];
31068 ready[lastpos] = tmp;
31069 break;
31071 pos--;
31074 else
31076 /* Last insn was the 2nd divide or not a divide, reset the counter. */
31077 divide_cnt = 0;
31079 /* The best dispatch throughput for vector and vector load insns can be
31080 achieved by interleaving a vector and vector load such that they'll
31081 dispatch to the same superslice. If this pairing cannot be achieved
31082 then it is best to pair vector insns together and vector load insns
31083 together.
31085 To aid in this pairing, vec_pairing maintains the current state with
31086 the following values:
31088 0 : Initial state, no vecload/vector pairing has been started.
31090 1 : A vecload or vector insn has been issued and a candidate for
31091 pairing has been found and moved to the end of the ready
31092 list. */
31093 if (type == TYPE_VECLOAD)
31095 /* Issued a vecload. */
31096 if (vec_pairing == 0)
31098 int vecload_pos = -1;
31099 /* We issued a single vecload, look for a vector insn to pair it
31100 with. If one isn't found, try to pair another vecload. */
31101 pos = lastpos;
31102 while (pos >= 0)
31104 if (recog_memoized (ready[pos]) >= 0)
31106 type2 = get_attr_type (ready[pos]);
31107 if (is_power9_pairable_vec_type (type2))
31109 /* Found a vector insn to pair with, move it to the
31110 end of the ready list so it is scheduled next. */
31111 tmp = ready[pos];
31112 for (i = pos; i < lastpos; i++)
31113 ready[i] = ready[i + 1];
31114 ready[lastpos] = tmp;
31115 vec_pairing = 1;
31116 return cached_can_issue_more;
31118 else if (type2 == TYPE_VECLOAD && vecload_pos == -1)
31119 /* Remember position of first vecload seen. */
31120 vecload_pos = pos;
31122 pos--;
31124 if (vecload_pos >= 0)
31126 /* Didn't find a vector to pair with but did find a vecload,
31127 move it to the end of the ready list. */
31128 tmp = ready[vecload_pos];
31129 for (i = vecload_pos; i < lastpos; i++)
31130 ready[i] = ready[i + 1];
31131 ready[lastpos] = tmp;
31132 vec_pairing = 1;
31133 return cached_can_issue_more;
31137 else if (is_power9_pairable_vec_type (type))
31139 /* Issued a vector operation. */
31140 if (vec_pairing == 0)
31142 int vec_pos = -1;
31143 /* We issued a single vector insn, look for a vecload to pair it
31144 with. If one isn't found, try to pair another vector. */
31145 pos = lastpos;
31146 while (pos >= 0)
31148 if (recog_memoized (ready[pos]) >= 0)
31150 type2 = get_attr_type (ready[pos]);
31151 if (type2 == TYPE_VECLOAD)
31153 /* Found a vecload insn to pair with, move it to the
31154 end of the ready list so it is scheduled next. */
31155 tmp = ready[pos];
31156 for (i = pos; i < lastpos; i++)
31157 ready[i] = ready[i + 1];
31158 ready[lastpos] = tmp;
31159 vec_pairing = 1;
31160 return cached_can_issue_more;
31162 else if (is_power9_pairable_vec_type (type2)
31163 && vec_pos == -1)
31164 /* Remember position of first vector insn seen. */
31165 vec_pos = pos;
31167 pos--;
31169 if (vec_pos >= 0)
31171 /* Didn't find a vecload to pair with but did find a vector
31172 insn, move it to the end of the ready list. */
31173 tmp = ready[vec_pos];
31174 for (i = vec_pos; i < lastpos; i++)
31175 ready[i] = ready[i + 1];
31176 ready[lastpos] = tmp;
31177 vec_pairing = 1;
31178 return cached_can_issue_more;
31183 /* We've either finished a vec/vecload pair, couldn't find an insn to
31184 continue the current pair, or the last insn had nothing to do with
31185 with pairing. In any case, reset the state. */
31186 vec_pairing = 0;
31189 return cached_can_issue_more;
31192 /* We are about to begin issuing insns for this clock cycle. */
31194 static int
31195 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
31196 rtx_insn **ready ATTRIBUTE_UNUSED,
31197 int *pn_ready ATTRIBUTE_UNUSED,
31198 int clock_var ATTRIBUTE_UNUSED)
31200 int n_ready = *pn_ready;
31202 if (sched_verbose)
31203 fprintf (dump, "// rs6000_sched_reorder :\n");
31205 /* Reorder the ready list, if the second to last ready insn
31206 is a nonepipeline insn. */
31207 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
31209 if (is_nonpipeline_insn (ready[n_ready - 1])
31210 && (recog_memoized (ready[n_ready - 2]) > 0))
31211 /* Simply swap first two insns. */
31212 std::swap (ready[n_ready - 1], ready[n_ready - 2]);
31215 if (rs6000_cpu == PROCESSOR_POWER6)
31216 load_store_pendulum = 0;
31218 return rs6000_issue_rate ();
31221 /* Like rs6000_sched_reorder, but called after issuing each insn. */
31223 static int
31224 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx_insn **ready,
31225 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
31227 if (sched_verbose)
31228 fprintf (dump, "// rs6000_sched_reorder2 :\n");
31230 /* For Power6, we need to handle some special cases to try and keep the
31231 store queue from overflowing and triggering expensive flushes.
31233 This code monitors how load and store instructions are being issued
31234 and skews the ready list one way or the other to increase the likelihood
31235 that a desired instruction is issued at the proper time.
31237 A couple of things are done. First, we maintain a "load_store_pendulum"
31238 to track the current state of load/store issue.
31240 - If the pendulum is at zero, then no loads or stores have been
31241 issued in the current cycle so we do nothing.
31243 - If the pendulum is 1, then a single load has been issued in this
31244 cycle and we attempt to locate another load in the ready list to
31245 issue with it.
31247 - If the pendulum is -2, then two stores have already been
31248 issued in this cycle, so we increase the priority of the first load
31249 in the ready list to increase it's likelihood of being chosen first
31250 in the next cycle.
31252 - If the pendulum is -1, then a single store has been issued in this
31253 cycle and we attempt to locate another store in the ready list to
31254 issue with it, preferring a store to an adjacent memory location to
31255 facilitate store pairing in the store queue.
31257 - If the pendulum is 2, then two loads have already been
31258 issued in this cycle, so we increase the priority of the first store
31259 in the ready list to increase it's likelihood of being chosen first
31260 in the next cycle.
31262 - If the pendulum < -2 or > 2, then do nothing.
31264 Note: This code covers the most common scenarios. There exist non
31265 load/store instructions which make use of the LSU and which
31266 would need to be accounted for to strictly model the behavior
31267 of the machine. Those instructions are currently unaccounted
31268 for to help minimize compile time overhead of this code.
31270 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
31272 int pos;
31273 int i;
31274 rtx_insn *tmp;
31275 rtx load_mem, str_mem;
31277 if (is_store_insn (last_scheduled_insn, &str_mem))
31278 /* Issuing a store, swing the load_store_pendulum to the left */
31279 load_store_pendulum--;
31280 else if (is_load_insn (last_scheduled_insn, &load_mem))
31281 /* Issuing a load, swing the load_store_pendulum to the right */
31282 load_store_pendulum++;
31283 else
31284 return cached_can_issue_more;
31286 /* If the pendulum is balanced, or there is only one instruction on
31287 the ready list, then all is well, so return. */
31288 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
31289 return cached_can_issue_more;
31291 if (load_store_pendulum == 1)
31293 /* A load has been issued in this cycle. Scan the ready list
31294 for another load to issue with it */
31295 pos = *pn_ready-1;
31297 while (pos >= 0)
31299 if (is_load_insn (ready[pos], &load_mem))
31301 /* Found a load. Move it to the head of the ready list,
31302 and adjust it's priority so that it is more likely to
31303 stay there */
31304 tmp = ready[pos];
31305 for (i=pos; i<*pn_ready-1; i++)
31306 ready[i] = ready[i + 1];
31307 ready[*pn_ready-1] = tmp;
31309 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
31310 INSN_PRIORITY (tmp)++;
31311 break;
31313 pos--;
31316 else if (load_store_pendulum == -2)
31318 /* Two stores have been issued in this cycle. Increase the
31319 priority of the first load in the ready list to favor it for
31320 issuing in the next cycle. */
31321 pos = *pn_ready-1;
31323 while (pos >= 0)
31325 if (is_load_insn (ready[pos], &load_mem)
31326 && !sel_sched_p ()
31327 && INSN_PRIORITY_KNOWN (ready[pos]))
31329 INSN_PRIORITY (ready[pos])++;
31331 /* Adjust the pendulum to account for the fact that a load
31332 was found and increased in priority. This is to prevent
31333 increasing the priority of multiple loads */
31334 load_store_pendulum--;
31336 break;
31338 pos--;
31341 else if (load_store_pendulum == -1)
31343 /* A store has been issued in this cycle. Scan the ready list for
31344 another store to issue with it, preferring a store to an adjacent
31345 memory location */
31346 int first_store_pos = -1;
31348 pos = *pn_ready-1;
31350 while (pos >= 0)
31352 if (is_store_insn (ready[pos], &str_mem))
31354 rtx str_mem2;
31355 /* Maintain the index of the first store found on the
31356 list */
31357 if (first_store_pos == -1)
31358 first_store_pos = pos;
31360 if (is_store_insn (last_scheduled_insn, &str_mem2)
31361 && adjacent_mem_locations (str_mem, str_mem2))
31363 /* Found an adjacent store. Move it to the head of the
31364 ready list, and adjust it's priority so that it is
31365 more likely to stay there */
31366 tmp = ready[pos];
31367 for (i=pos; i<*pn_ready-1; i++)
31368 ready[i] = ready[i + 1];
31369 ready[*pn_ready-1] = tmp;
31371 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
31372 INSN_PRIORITY (tmp)++;
31374 first_store_pos = -1;
31376 break;
31379 pos--;
31382 if (first_store_pos >= 0)
31384 /* An adjacent store wasn't found, but a non-adjacent store was,
31385 so move the non-adjacent store to the front of the ready
31386 list, and adjust its priority so that it is more likely to
31387 stay there. */
31388 tmp = ready[first_store_pos];
31389 for (i=first_store_pos; i<*pn_ready-1; i++)
31390 ready[i] = ready[i + 1];
31391 ready[*pn_ready-1] = tmp;
31392 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
31393 INSN_PRIORITY (tmp)++;
31396 else if (load_store_pendulum == 2)
31398 /* Two loads have been issued in this cycle. Increase the priority
31399 of the first store in the ready list to favor it for issuing in
31400 the next cycle. */
31401 pos = *pn_ready-1;
31403 while (pos >= 0)
31405 if (is_store_insn (ready[pos], &str_mem)
31406 && !sel_sched_p ()
31407 && INSN_PRIORITY_KNOWN (ready[pos]))
31409 INSN_PRIORITY (ready[pos])++;
31411 /* Adjust the pendulum to account for the fact that a store
31412 was found and increased in priority. This is to prevent
31413 increasing the priority of multiple stores */
31414 load_store_pendulum++;
31416 break;
31418 pos--;
31423 /* Do Power9 dependent reordering if necessary. */
31424 if (rs6000_cpu == PROCESSOR_POWER9 && last_scheduled_insn
31425 && recog_memoized (last_scheduled_insn) >= 0)
31426 return power9_sched_reorder2 (ready, *pn_ready - 1);
31428 return cached_can_issue_more;
31431 /* Return whether the presence of INSN causes a dispatch group termination
31432 of group WHICH_GROUP.
31434 If WHICH_GROUP == current_group, this function will return true if INSN
31435 causes the termination of the current group (i.e, the dispatch group to
31436 which INSN belongs). This means that INSN will be the last insn in the
31437 group it belongs to.
31439 If WHICH_GROUP == previous_group, this function will return true if INSN
31440 causes the termination of the previous group (i.e, the dispatch group that
31441 precedes the group to which INSN belongs). This means that INSN will be
31442 the first insn in the group it belongs to). */
31444 static bool
31445 insn_terminates_group_p (rtx_insn *insn, enum group_termination which_group)
31447 bool first, last;
31449 if (! insn)
31450 return false;
31452 first = insn_must_be_first_in_group (insn);
31453 last = insn_must_be_last_in_group (insn);
31455 if (first && last)
31456 return true;
31458 if (which_group == current_group)
31459 return last;
31460 else if (which_group == previous_group)
31461 return first;
31463 return false;
31467 static bool
31468 insn_must_be_first_in_group (rtx_insn *insn)
31470 enum attr_type type;
31472 if (!insn
31473 || NOTE_P (insn)
31474 || DEBUG_INSN_P (insn)
31475 || GET_CODE (PATTERN (insn)) == USE
31476 || GET_CODE (PATTERN (insn)) == CLOBBER)
31477 return false;
31479 switch (rs6000_cpu)
31481 case PROCESSOR_POWER5:
31482 if (is_cracked_insn (insn))
31483 return true;
31484 /* FALLTHRU */
31485 case PROCESSOR_POWER4:
31486 if (is_microcoded_insn (insn))
31487 return true;
31489 if (!rs6000_sched_groups)
31490 return false;
31492 type = get_attr_type (insn);
31494 switch (type)
31496 case TYPE_MFCR:
31497 case TYPE_MFCRF:
31498 case TYPE_MTCR:
31499 case TYPE_DELAYED_CR:
31500 case TYPE_CR_LOGICAL:
31501 case TYPE_MTJMPR:
31502 case TYPE_MFJMPR:
31503 case TYPE_DIV:
31504 case TYPE_LOAD_L:
31505 case TYPE_STORE_C:
31506 case TYPE_ISYNC:
31507 case TYPE_SYNC:
31508 return true;
31509 default:
31510 break;
31512 break;
31513 case PROCESSOR_POWER6:
31514 type = get_attr_type (insn);
31516 switch (type)
31518 case TYPE_EXTS:
31519 case TYPE_CNTLZ:
31520 case TYPE_TRAP:
31521 case TYPE_MUL:
31522 case TYPE_INSERT:
31523 case TYPE_FPCOMPARE:
31524 case TYPE_MFCR:
31525 case TYPE_MTCR:
31526 case TYPE_MFJMPR:
31527 case TYPE_MTJMPR:
31528 case TYPE_ISYNC:
31529 case TYPE_SYNC:
31530 case TYPE_LOAD_L:
31531 case TYPE_STORE_C:
31532 return true;
31533 case TYPE_SHIFT:
31534 if (get_attr_dot (insn) == DOT_NO
31535 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
31536 return true;
31537 else
31538 break;
31539 case TYPE_DIV:
31540 if (get_attr_size (insn) == SIZE_32)
31541 return true;
31542 else
31543 break;
31544 case TYPE_LOAD:
31545 case TYPE_STORE:
31546 case TYPE_FPLOAD:
31547 case TYPE_FPSTORE:
31548 if (get_attr_update (insn) == UPDATE_YES)
31549 return true;
31550 else
31551 break;
31552 default:
31553 break;
31555 break;
31556 case PROCESSOR_POWER7:
31557 type = get_attr_type (insn);
31559 switch (type)
31561 case TYPE_CR_LOGICAL:
31562 case TYPE_MFCR:
31563 case TYPE_MFCRF:
31564 case TYPE_MTCR:
31565 case TYPE_DIV:
31566 case TYPE_ISYNC:
31567 case TYPE_LOAD_L:
31568 case TYPE_STORE_C:
31569 case TYPE_MFJMPR:
31570 case TYPE_MTJMPR:
31571 return true;
31572 case TYPE_MUL:
31573 case TYPE_SHIFT:
31574 case TYPE_EXTS:
31575 if (get_attr_dot (insn) == DOT_YES)
31576 return true;
31577 else
31578 break;
31579 case TYPE_LOAD:
31580 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31581 || get_attr_update (insn) == UPDATE_YES)
31582 return true;
31583 else
31584 break;
31585 case TYPE_STORE:
31586 case TYPE_FPLOAD:
31587 case TYPE_FPSTORE:
31588 if (get_attr_update (insn) == UPDATE_YES)
31589 return true;
31590 else
31591 break;
31592 default:
31593 break;
31595 break;
31596 case PROCESSOR_POWER8:
31597 type = get_attr_type (insn);
31599 switch (type)
31601 case TYPE_CR_LOGICAL:
31602 case TYPE_DELAYED_CR:
31603 case TYPE_MFCR:
31604 case TYPE_MFCRF:
31605 case TYPE_MTCR:
31606 case TYPE_SYNC:
31607 case TYPE_ISYNC:
31608 case TYPE_LOAD_L:
31609 case TYPE_STORE_C:
31610 case TYPE_VECSTORE:
31611 case TYPE_MFJMPR:
31612 case TYPE_MTJMPR:
31613 return true;
31614 case TYPE_SHIFT:
31615 case TYPE_EXTS:
31616 case TYPE_MUL:
31617 if (get_attr_dot (insn) == DOT_YES)
31618 return true;
31619 else
31620 break;
31621 case TYPE_LOAD:
31622 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31623 || get_attr_update (insn) == UPDATE_YES)
31624 return true;
31625 else
31626 break;
31627 case TYPE_STORE:
31628 if (get_attr_update (insn) == UPDATE_YES
31629 && get_attr_indexed (insn) == INDEXED_YES)
31630 return true;
31631 else
31632 break;
31633 default:
31634 break;
31636 break;
31637 default:
31638 break;
31641 return false;
31644 static bool
31645 insn_must_be_last_in_group (rtx_insn *insn)
31647 enum attr_type type;
31649 if (!insn
31650 || NOTE_P (insn)
31651 || DEBUG_INSN_P (insn)
31652 || GET_CODE (PATTERN (insn)) == USE
31653 || GET_CODE (PATTERN (insn)) == CLOBBER)
31654 return false;
31656 switch (rs6000_cpu) {
31657 case PROCESSOR_POWER4:
31658 case PROCESSOR_POWER5:
31659 if (is_microcoded_insn (insn))
31660 return true;
31662 if (is_branch_slot_insn (insn))
31663 return true;
31665 break;
31666 case PROCESSOR_POWER6:
31667 type = get_attr_type (insn);
31669 switch (type)
31671 case TYPE_EXTS:
31672 case TYPE_CNTLZ:
31673 case TYPE_TRAP:
31674 case TYPE_MUL:
31675 case TYPE_FPCOMPARE:
31676 case TYPE_MFCR:
31677 case TYPE_MTCR:
31678 case TYPE_MFJMPR:
31679 case TYPE_MTJMPR:
31680 case TYPE_ISYNC:
31681 case TYPE_SYNC:
31682 case TYPE_LOAD_L:
31683 case TYPE_STORE_C:
31684 return true;
31685 case TYPE_SHIFT:
31686 if (get_attr_dot (insn) == DOT_NO
31687 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
31688 return true;
31689 else
31690 break;
31691 case TYPE_DIV:
31692 if (get_attr_size (insn) == SIZE_32)
31693 return true;
31694 else
31695 break;
31696 default:
31697 break;
31699 break;
31700 case PROCESSOR_POWER7:
31701 type = get_attr_type (insn);
31703 switch (type)
31705 case TYPE_ISYNC:
31706 case TYPE_SYNC:
31707 case TYPE_LOAD_L:
31708 case TYPE_STORE_C:
31709 return true;
31710 case TYPE_LOAD:
31711 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31712 && get_attr_update (insn) == UPDATE_YES)
31713 return true;
31714 else
31715 break;
31716 case TYPE_STORE:
31717 if (get_attr_update (insn) == UPDATE_YES
31718 && get_attr_indexed (insn) == INDEXED_YES)
31719 return true;
31720 else
31721 break;
31722 default:
31723 break;
31725 break;
31726 case PROCESSOR_POWER8:
31727 type = get_attr_type (insn);
31729 switch (type)
31731 case TYPE_MFCR:
31732 case TYPE_MTCR:
31733 case TYPE_ISYNC:
31734 case TYPE_SYNC:
31735 case TYPE_LOAD_L:
31736 case TYPE_STORE_C:
31737 return true;
31738 case TYPE_LOAD:
31739 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31740 && get_attr_update (insn) == UPDATE_YES)
31741 return true;
31742 else
31743 break;
31744 case TYPE_STORE:
31745 if (get_attr_update (insn) == UPDATE_YES
31746 && get_attr_indexed (insn) == INDEXED_YES)
31747 return true;
31748 else
31749 break;
31750 default:
31751 break;
31753 break;
31754 default:
31755 break;
31758 return false;
31761 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
31762 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
31764 static bool
31765 is_costly_group (rtx *group_insns, rtx next_insn)
31767 int i;
31768 int issue_rate = rs6000_issue_rate ();
31770 for (i = 0; i < issue_rate; i++)
31772 sd_iterator_def sd_it;
31773 dep_t dep;
31774 rtx insn = group_insns[i];
31776 if (!insn)
31777 continue;
31779 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
31781 rtx next = DEP_CON (dep);
31783 if (next == next_insn
31784 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
31785 return true;
31789 return false;
31792 /* Utility of the function redefine_groups.
31793 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
31794 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
31795 to keep it "far" (in a separate group) from GROUP_INSNS, following
31796 one of the following schemes, depending on the value of the flag
31797 -minsert_sched_nops = X:
31798 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
31799 in order to force NEXT_INSN into a separate group.
31800 (2) X < sched_finish_regroup_exact: insert exactly X nops.
31801 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
31802 insertion (has a group just ended, how many vacant issue slots remain in the
31803 last group, and how many dispatch groups were encountered so far). */
31805 static int
31806 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
31807 rtx_insn *next_insn, bool *group_end, int can_issue_more,
31808 int *group_count)
31810 rtx nop;
31811 bool force;
31812 int issue_rate = rs6000_issue_rate ();
31813 bool end = *group_end;
31814 int i;
31816 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
31817 return can_issue_more;
31819 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
31820 return can_issue_more;
31822 force = is_costly_group (group_insns, next_insn);
31823 if (!force)
31824 return can_issue_more;
31826 if (sched_verbose > 6)
31827 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
31828 *group_count ,can_issue_more);
31830 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
31832 if (*group_end)
31833 can_issue_more = 0;
31835 /* Since only a branch can be issued in the last issue_slot, it is
31836 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
31837 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
31838 in this case the last nop will start a new group and the branch
31839 will be forced to the new group. */
31840 if (can_issue_more && !is_branch_slot_insn (next_insn))
31841 can_issue_more--;
31843 /* Do we have a special group ending nop? */
31844 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
31845 || rs6000_cpu_attr == CPU_POWER8)
31847 nop = gen_group_ending_nop ();
31848 emit_insn_before (nop, next_insn);
31849 can_issue_more = 0;
31851 else
31852 while (can_issue_more > 0)
31854 nop = gen_nop ();
31855 emit_insn_before (nop, next_insn);
31856 can_issue_more--;
31859 *group_end = true;
31860 return 0;
31863 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
31865 int n_nops = rs6000_sched_insert_nops;
31867 /* Nops can't be issued from the branch slot, so the effective
31868 issue_rate for nops is 'issue_rate - 1'. */
31869 if (can_issue_more == 0)
31870 can_issue_more = issue_rate;
31871 can_issue_more--;
31872 if (can_issue_more == 0)
31874 can_issue_more = issue_rate - 1;
31875 (*group_count)++;
31876 end = true;
31877 for (i = 0; i < issue_rate; i++)
31879 group_insns[i] = 0;
31883 while (n_nops > 0)
31885 nop = gen_nop ();
31886 emit_insn_before (nop, next_insn);
31887 if (can_issue_more == issue_rate - 1) /* new group begins */
31888 end = false;
31889 can_issue_more--;
31890 if (can_issue_more == 0)
31892 can_issue_more = issue_rate - 1;
31893 (*group_count)++;
31894 end = true;
31895 for (i = 0; i < issue_rate; i++)
31897 group_insns[i] = 0;
31900 n_nops--;
31903 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
31904 can_issue_more++;
31906 /* Is next_insn going to start a new group? */
31907 *group_end
31908 = (end
31909 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
31910 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
31911 || (can_issue_more < issue_rate &&
31912 insn_terminates_group_p (next_insn, previous_group)));
31913 if (*group_end && end)
31914 (*group_count)--;
31916 if (sched_verbose > 6)
31917 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
31918 *group_count, can_issue_more);
31919 return can_issue_more;
31922 return can_issue_more;
31925 /* This function tries to synch the dispatch groups that the compiler "sees"
31926 with the dispatch groups that the processor dispatcher is expected to
31927 form in practice. It tries to achieve this synchronization by forcing the
31928 estimated processor grouping on the compiler (as opposed to the function
31929 'pad_goups' which tries to force the scheduler's grouping on the processor).
31931 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
31932 examines the (estimated) dispatch groups that will be formed by the processor
31933 dispatcher. It marks these group boundaries to reflect the estimated
31934 processor grouping, overriding the grouping that the scheduler had marked.
31935 Depending on the value of the flag '-minsert-sched-nops' this function can
31936 force certain insns into separate groups or force a certain distance between
31937 them by inserting nops, for example, if there exists a "costly dependence"
31938 between the insns.
31940 The function estimates the group boundaries that the processor will form as
31941 follows: It keeps track of how many vacant issue slots are available after
31942 each insn. A subsequent insn will start a new group if one of the following
31943 4 cases applies:
31944 - no more vacant issue slots remain in the current dispatch group.
31945 - only the last issue slot, which is the branch slot, is vacant, but the next
31946 insn is not a branch.
31947 - only the last 2 or less issue slots, including the branch slot, are vacant,
31948 which means that a cracked insn (which occupies two issue slots) can't be
31949 issued in this group.
31950 - less than 'issue_rate' slots are vacant, and the next insn always needs to
31951 start a new group. */
31953 static int
31954 redefine_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
31955 rtx_insn *tail)
31957 rtx_insn *insn, *next_insn;
31958 int issue_rate;
31959 int can_issue_more;
31960 int slot, i;
31961 bool group_end;
31962 int group_count = 0;
31963 rtx *group_insns;
31965 /* Initialize. */
31966 issue_rate = rs6000_issue_rate ();
31967 group_insns = XALLOCAVEC (rtx, issue_rate);
31968 for (i = 0; i < issue_rate; i++)
31970 group_insns[i] = 0;
31972 can_issue_more = issue_rate;
31973 slot = 0;
31974 insn = get_next_active_insn (prev_head_insn, tail);
31975 group_end = false;
31977 while (insn != NULL_RTX)
31979 slot = (issue_rate - can_issue_more);
31980 group_insns[slot] = insn;
31981 can_issue_more =
31982 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
31983 if (insn_terminates_group_p (insn, current_group))
31984 can_issue_more = 0;
31986 next_insn = get_next_active_insn (insn, tail);
31987 if (next_insn == NULL_RTX)
31988 return group_count + 1;
31990 /* Is next_insn going to start a new group? */
31991 group_end
31992 = (can_issue_more == 0
31993 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
31994 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
31995 || (can_issue_more < issue_rate &&
31996 insn_terminates_group_p (next_insn, previous_group)));
31998 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
31999 next_insn, &group_end, can_issue_more,
32000 &group_count);
32002 if (group_end)
32004 group_count++;
32005 can_issue_more = 0;
32006 for (i = 0; i < issue_rate; i++)
32008 group_insns[i] = 0;
32012 if (GET_MODE (next_insn) == TImode && can_issue_more)
32013 PUT_MODE (next_insn, VOIDmode);
32014 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
32015 PUT_MODE (next_insn, TImode);
32017 insn = next_insn;
32018 if (can_issue_more == 0)
32019 can_issue_more = issue_rate;
32020 } /* while */
32022 return group_count;
32025 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
32026 dispatch group boundaries that the scheduler had marked. Pad with nops
32027 any dispatch groups which have vacant issue slots, in order to force the
32028 scheduler's grouping on the processor dispatcher. The function
32029 returns the number of dispatch groups found. */
32031 static int
32032 pad_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
32033 rtx_insn *tail)
32035 rtx_insn *insn, *next_insn;
32036 rtx nop;
32037 int issue_rate;
32038 int can_issue_more;
32039 int group_end;
32040 int group_count = 0;
32042 /* Initialize issue_rate. */
32043 issue_rate = rs6000_issue_rate ();
32044 can_issue_more = issue_rate;
32046 insn = get_next_active_insn (prev_head_insn, tail);
32047 next_insn = get_next_active_insn (insn, tail);
32049 while (insn != NULL_RTX)
32051 can_issue_more =
32052 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
32054 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
32056 if (next_insn == NULL_RTX)
32057 break;
32059 if (group_end)
32061 /* If the scheduler had marked group termination at this location
32062 (between insn and next_insn), and neither insn nor next_insn will
32063 force group termination, pad the group with nops to force group
32064 termination. */
32065 if (can_issue_more
32066 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
32067 && !insn_terminates_group_p (insn, current_group)
32068 && !insn_terminates_group_p (next_insn, previous_group))
32070 if (!is_branch_slot_insn (next_insn))
32071 can_issue_more--;
32073 while (can_issue_more)
32075 nop = gen_nop ();
32076 emit_insn_before (nop, next_insn);
32077 can_issue_more--;
32081 can_issue_more = issue_rate;
32082 group_count++;
32085 insn = next_insn;
32086 next_insn = get_next_active_insn (insn, tail);
32089 return group_count;
32092 /* We're beginning a new block. Initialize data structures as necessary. */
32094 static void
32095 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
32096 int sched_verbose ATTRIBUTE_UNUSED,
32097 int max_ready ATTRIBUTE_UNUSED)
32099 last_scheduled_insn = NULL;
32100 load_store_pendulum = 0;
32101 divide_cnt = 0;
32102 vec_pairing = 0;
32105 /* The following function is called at the end of scheduling BB.
32106 After reload, it inserts nops at insn group bundling. */
32108 static void
32109 rs6000_sched_finish (FILE *dump, int sched_verbose)
32111 int n_groups;
32113 if (sched_verbose)
32114 fprintf (dump, "=== Finishing schedule.\n");
32116 if (reload_completed && rs6000_sched_groups)
32118 /* Do not run sched_finish hook when selective scheduling enabled. */
32119 if (sel_sched_p ())
32120 return;
32122 if (rs6000_sched_insert_nops == sched_finish_none)
32123 return;
32125 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
32126 n_groups = pad_groups (dump, sched_verbose,
32127 current_sched_info->prev_head,
32128 current_sched_info->next_tail);
32129 else
32130 n_groups = redefine_groups (dump, sched_verbose,
32131 current_sched_info->prev_head,
32132 current_sched_info->next_tail);
32134 if (sched_verbose >= 6)
32136 fprintf (dump, "ngroups = %d\n", n_groups);
32137 print_rtl (dump, current_sched_info->prev_head);
32138 fprintf (dump, "Done finish_sched\n");
32143 struct rs6000_sched_context
32145 short cached_can_issue_more;
32146 rtx_insn *last_scheduled_insn;
32147 int load_store_pendulum;
32148 int divide_cnt;
32149 int vec_pairing;
32152 typedef struct rs6000_sched_context rs6000_sched_context_def;
32153 typedef rs6000_sched_context_def *rs6000_sched_context_t;
32155 /* Allocate store for new scheduling context. */
32156 static void *
32157 rs6000_alloc_sched_context (void)
32159 return xmalloc (sizeof (rs6000_sched_context_def));
32162 /* If CLEAN_P is true then initializes _SC with clean data,
32163 and from the global context otherwise. */
32164 static void
32165 rs6000_init_sched_context (void *_sc, bool clean_p)
32167 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
32169 if (clean_p)
32171 sc->cached_can_issue_more = 0;
32172 sc->last_scheduled_insn = NULL;
32173 sc->load_store_pendulum = 0;
32174 sc->divide_cnt = 0;
32175 sc->vec_pairing = 0;
32177 else
32179 sc->cached_can_issue_more = cached_can_issue_more;
32180 sc->last_scheduled_insn = last_scheduled_insn;
32181 sc->load_store_pendulum = load_store_pendulum;
32182 sc->divide_cnt = divide_cnt;
32183 sc->vec_pairing = vec_pairing;
32187 /* Sets the global scheduling context to the one pointed to by _SC. */
32188 static void
32189 rs6000_set_sched_context (void *_sc)
32191 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
32193 gcc_assert (sc != NULL);
32195 cached_can_issue_more = sc->cached_can_issue_more;
32196 last_scheduled_insn = sc->last_scheduled_insn;
32197 load_store_pendulum = sc->load_store_pendulum;
32198 divide_cnt = sc->divide_cnt;
32199 vec_pairing = sc->vec_pairing;
32202 /* Free _SC. */
32203 static void
32204 rs6000_free_sched_context (void *_sc)
32206 gcc_assert (_sc != NULL);
32208 free (_sc);
32211 static bool
32212 rs6000_sched_can_speculate_insn (rtx_insn *insn)
32214 switch (get_attr_type (insn))
32216 case TYPE_DIV:
32217 case TYPE_SDIV:
32218 case TYPE_DDIV:
32219 case TYPE_VECDIV:
32220 case TYPE_SSQRT:
32221 case TYPE_DSQRT:
32222 return false;
32224 default:
32225 return true;
32229 /* Length in units of the trampoline for entering a nested function. */
32232 rs6000_trampoline_size (void)
32234 int ret = 0;
32236 switch (DEFAULT_ABI)
32238 default:
32239 gcc_unreachable ();
32241 case ABI_AIX:
32242 ret = (TARGET_32BIT) ? 12 : 24;
32243 break;
32245 case ABI_ELFv2:
32246 gcc_assert (!TARGET_32BIT);
32247 ret = 32;
32248 break;
32250 case ABI_DARWIN:
32251 case ABI_V4:
32252 ret = (TARGET_32BIT) ? 40 : 48;
32253 break;
32256 return ret;
32259 /* Emit RTL insns to initialize the variable parts of a trampoline.
32260 FNADDR is an RTX for the address of the function's pure code.
32261 CXT is an RTX for the static chain value for the function. */
32263 static void
32264 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
32266 int regsize = (TARGET_32BIT) ? 4 : 8;
32267 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
32268 rtx ctx_reg = force_reg (Pmode, cxt);
32269 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
32271 switch (DEFAULT_ABI)
32273 default:
32274 gcc_unreachable ();
32276 /* Under AIX, just build the 3 word function descriptor */
32277 case ABI_AIX:
32279 rtx fnmem, fn_reg, toc_reg;
32281 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
32282 error ("you cannot take the address of a nested function if you use "
32283 "the %qs option", "-mno-pointers-to-nested-functions");
32285 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
32286 fn_reg = gen_reg_rtx (Pmode);
32287 toc_reg = gen_reg_rtx (Pmode);
32289 /* Macro to shorten the code expansions below. */
32290 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
32292 m_tramp = replace_equiv_address (m_tramp, addr);
32294 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
32295 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
32296 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
32297 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
32298 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
32300 # undef MEM_PLUS
32302 break;
32304 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
32305 case ABI_ELFv2:
32306 case ABI_DARWIN:
32307 case ABI_V4:
32308 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
32309 LCT_NORMAL, VOIDmode, 4,
32310 addr, Pmode,
32311 GEN_INT (rs6000_trampoline_size ()), SImode,
32312 fnaddr, Pmode,
32313 ctx_reg, Pmode);
32314 break;
32319 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
32320 identifier as an argument, so the front end shouldn't look it up. */
32322 static bool
32323 rs6000_attribute_takes_identifier_p (const_tree attr_id)
32325 return is_attribute_p ("altivec", attr_id);
32328 /* Handle the "altivec" attribute. The attribute may have
32329 arguments as follows:
32331 __attribute__((altivec(vector__)))
32332 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
32333 __attribute__((altivec(bool__))) (always followed by 'unsigned')
32335 and may appear more than once (e.g., 'vector bool char') in a
32336 given declaration. */
32338 static tree
32339 rs6000_handle_altivec_attribute (tree *node,
32340 tree name ATTRIBUTE_UNUSED,
32341 tree args,
32342 int flags ATTRIBUTE_UNUSED,
32343 bool *no_add_attrs)
32345 tree type = *node, result = NULL_TREE;
32346 machine_mode mode;
32347 int unsigned_p;
32348 char altivec_type
32349 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
32350 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
32351 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
32352 : '?');
32354 while (POINTER_TYPE_P (type)
32355 || TREE_CODE (type) == FUNCTION_TYPE
32356 || TREE_CODE (type) == METHOD_TYPE
32357 || TREE_CODE (type) == ARRAY_TYPE)
32358 type = TREE_TYPE (type);
32360 mode = TYPE_MODE (type);
32362 /* Check for invalid AltiVec type qualifiers. */
32363 if (type == long_double_type_node)
32364 error ("use of %<long double%> in AltiVec types is invalid");
32365 else if (type == boolean_type_node)
32366 error ("use of boolean types in AltiVec types is invalid");
32367 else if (TREE_CODE (type) == COMPLEX_TYPE)
32368 error ("use of %<complex%> in AltiVec types is invalid");
32369 else if (DECIMAL_FLOAT_MODE_P (mode))
32370 error ("use of decimal floating point types in AltiVec types is invalid");
32371 else if (!TARGET_VSX)
32373 if (type == long_unsigned_type_node || type == long_integer_type_node)
32375 if (TARGET_64BIT)
32376 error ("use of %<long%> in AltiVec types is invalid for "
32377 "64-bit code without %qs", "-mvsx");
32378 else if (rs6000_warn_altivec_long)
32379 warning (0, "use of %<long%> in AltiVec types is deprecated; "
32380 "use %<int%>");
32382 else if (type == long_long_unsigned_type_node
32383 || type == long_long_integer_type_node)
32384 error ("use of %<long long%> in AltiVec types is invalid without %qs",
32385 "-mvsx");
32386 else if (type == double_type_node)
32387 error ("use of %<double%> in AltiVec types is invalid without %qs",
32388 "-mvsx");
32391 switch (altivec_type)
32393 case 'v':
32394 unsigned_p = TYPE_UNSIGNED (type);
32395 switch (mode)
32397 case E_TImode:
32398 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
32399 break;
32400 case E_DImode:
32401 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
32402 break;
32403 case E_SImode:
32404 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
32405 break;
32406 case E_HImode:
32407 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
32408 break;
32409 case E_QImode:
32410 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
32411 break;
32412 case E_SFmode: result = V4SF_type_node; break;
32413 case E_DFmode: result = V2DF_type_node; break;
32414 /* If the user says 'vector int bool', we may be handed the 'bool'
32415 attribute _before_ the 'vector' attribute, and so select the
32416 proper type in the 'b' case below. */
32417 case E_V4SImode: case E_V8HImode: case E_V16QImode: case E_V4SFmode:
32418 case E_V2DImode: case E_V2DFmode:
32419 result = type;
32420 default: break;
32422 break;
32423 case 'b':
32424 switch (mode)
32426 case E_DImode: case E_V2DImode: result = bool_V2DI_type_node; break;
32427 case E_SImode: case E_V4SImode: result = bool_V4SI_type_node; break;
32428 case E_HImode: case E_V8HImode: result = bool_V8HI_type_node; break;
32429 case E_QImode: case E_V16QImode: result = bool_V16QI_type_node;
32430 default: break;
32432 break;
32433 case 'p':
32434 switch (mode)
32436 case E_V8HImode: result = pixel_V8HI_type_node;
32437 default: break;
32439 default: break;
32442 /* Propagate qualifiers attached to the element type
32443 onto the vector type. */
32444 if (result && result != type && TYPE_QUALS (type))
32445 result = build_qualified_type (result, TYPE_QUALS (type));
32447 *no_add_attrs = true; /* No need to hang on to the attribute. */
32449 if (result)
32450 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
32452 return NULL_TREE;
32455 /* AltiVec defines four built-in scalar types that serve as vector
32456 elements; we must teach the compiler how to mangle them. */
32458 static const char *
32459 rs6000_mangle_type (const_tree type)
32461 type = TYPE_MAIN_VARIANT (type);
32463 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
32464 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
32465 return NULL;
32467 if (type == bool_char_type_node) return "U6__boolc";
32468 if (type == bool_short_type_node) return "U6__bools";
32469 if (type == pixel_type_node) return "u7__pixel";
32470 if (type == bool_int_type_node) return "U6__booli";
32471 if (type == bool_long_type_node) return "U6__booll";
32473 /* Use a unique name for __float128 rather than trying to use "e" or "g". Use
32474 "g" for IBM extended double, no matter whether it is long double (using
32475 -mabi=ibmlongdouble) or the distinct __ibm128 type. */
32476 if (TARGET_FLOAT128_TYPE)
32478 if (type == ieee128_float_type_node)
32479 return "U10__float128";
32481 if (type == ibm128_float_type_node)
32482 return "g";
32484 if (type == long_double_type_node && TARGET_LONG_DOUBLE_128)
32485 return (TARGET_IEEEQUAD) ? "U10__float128" : "g";
32488 /* Mangle IBM extended float long double as `g' (__float128) on
32489 powerpc*-linux where long-double-64 previously was the default. */
32490 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
32491 && TARGET_ELF
32492 && TARGET_LONG_DOUBLE_128
32493 && !TARGET_IEEEQUAD)
32494 return "g";
32496 /* For all other types, use normal C++ mangling. */
32497 return NULL;
32500 /* Handle a "longcall" or "shortcall" attribute; arguments as in
32501 struct attribute_spec.handler. */
32503 static tree
32504 rs6000_handle_longcall_attribute (tree *node, tree name,
32505 tree args ATTRIBUTE_UNUSED,
32506 int flags ATTRIBUTE_UNUSED,
32507 bool *no_add_attrs)
32509 if (TREE_CODE (*node) != FUNCTION_TYPE
32510 && TREE_CODE (*node) != FIELD_DECL
32511 && TREE_CODE (*node) != TYPE_DECL)
32513 warning (OPT_Wattributes, "%qE attribute only applies to functions",
32514 name);
32515 *no_add_attrs = true;
32518 return NULL_TREE;
32521 /* Set longcall attributes on all functions declared when
32522 rs6000_default_long_calls is true. */
32523 static void
32524 rs6000_set_default_type_attributes (tree type)
32526 if (rs6000_default_long_calls
32527 && (TREE_CODE (type) == FUNCTION_TYPE
32528 || TREE_CODE (type) == METHOD_TYPE))
32529 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
32530 NULL_TREE,
32531 TYPE_ATTRIBUTES (type));
32533 #if TARGET_MACHO
32534 darwin_set_default_type_attributes (type);
32535 #endif
32538 /* Return a reference suitable for calling a function with the
32539 longcall attribute. */
32542 rs6000_longcall_ref (rtx call_ref)
32544 const char *call_name;
32545 tree node;
32547 if (GET_CODE (call_ref) != SYMBOL_REF)
32548 return call_ref;
32550 /* System V adds '.' to the internal name, so skip them. */
32551 call_name = XSTR (call_ref, 0);
32552 if (*call_name == '.')
32554 while (*call_name == '.')
32555 call_name++;
32557 node = get_identifier (call_name);
32558 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
32561 return force_reg (Pmode, call_ref);
32564 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
32565 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
32566 #endif
32568 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
32569 struct attribute_spec.handler. */
32570 static tree
32571 rs6000_handle_struct_attribute (tree *node, tree name,
32572 tree args ATTRIBUTE_UNUSED,
32573 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
32575 tree *type = NULL;
32576 if (DECL_P (*node))
32578 if (TREE_CODE (*node) == TYPE_DECL)
32579 type = &TREE_TYPE (*node);
32581 else
32582 type = node;
32584 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
32585 || TREE_CODE (*type) == UNION_TYPE)))
32587 warning (OPT_Wattributes, "%qE attribute ignored", name);
32588 *no_add_attrs = true;
32591 else if ((is_attribute_p ("ms_struct", name)
32592 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
32593 || ((is_attribute_p ("gcc_struct", name)
32594 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
32596 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
32597 name);
32598 *no_add_attrs = true;
32601 return NULL_TREE;
32604 static bool
32605 rs6000_ms_bitfield_layout_p (const_tree record_type)
32607 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
32608 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
32609 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
32612 #ifdef USING_ELFOS_H
32614 /* A get_unnamed_section callback, used for switching to toc_section. */
32616 static void
32617 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
32619 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
32620 && TARGET_MINIMAL_TOC)
32622 if (!toc_initialized)
32624 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
32625 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32626 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
32627 fprintf (asm_out_file, "\t.tc ");
32628 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
32629 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
32630 fprintf (asm_out_file, "\n");
32632 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
32633 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32634 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
32635 fprintf (asm_out_file, " = .+32768\n");
32636 toc_initialized = 1;
32638 else
32639 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
32641 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
32643 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
32644 if (!toc_initialized)
32646 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32647 toc_initialized = 1;
32650 else
32652 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
32653 if (!toc_initialized)
32655 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32656 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
32657 fprintf (asm_out_file, " = .+32768\n");
32658 toc_initialized = 1;
32663 /* Implement TARGET_ASM_INIT_SECTIONS. */
32665 static void
32666 rs6000_elf_asm_init_sections (void)
32668 toc_section
32669 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
32671 sdata2_section
32672 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
32673 SDATA2_SECTION_ASM_OP);
32676 /* Implement TARGET_SELECT_RTX_SECTION. */
32678 static section *
32679 rs6000_elf_select_rtx_section (machine_mode mode, rtx x,
32680 unsigned HOST_WIDE_INT align)
32682 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
32683 return toc_section;
32684 else
32685 return default_elf_select_rtx_section (mode, x, align);
32688 /* For a SYMBOL_REF, set generic flags and then perform some
32689 target-specific processing.
32691 When the AIX ABI is requested on a non-AIX system, replace the
32692 function name with the real name (with a leading .) rather than the
32693 function descriptor name. This saves a lot of overriding code to
32694 read the prefixes. */
32696 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
32697 static void
32698 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
32700 default_encode_section_info (decl, rtl, first);
32702 if (first
32703 && TREE_CODE (decl) == FUNCTION_DECL
32704 && !TARGET_AIX
32705 && DEFAULT_ABI == ABI_AIX)
32707 rtx sym_ref = XEXP (rtl, 0);
32708 size_t len = strlen (XSTR (sym_ref, 0));
32709 char *str = XALLOCAVEC (char, len + 2);
32710 str[0] = '.';
32711 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
32712 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
32716 static inline bool
32717 compare_section_name (const char *section, const char *templ)
32719 int len;
32721 len = strlen (templ);
32722 return (strncmp (section, templ, len) == 0
32723 && (section[len] == 0 || section[len] == '.'));
32726 bool
32727 rs6000_elf_in_small_data_p (const_tree decl)
32729 if (rs6000_sdata == SDATA_NONE)
32730 return false;
32732 /* We want to merge strings, so we never consider them small data. */
32733 if (TREE_CODE (decl) == STRING_CST)
32734 return false;
32736 /* Functions are never in the small data area. */
32737 if (TREE_CODE (decl) == FUNCTION_DECL)
32738 return false;
32740 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
32742 const char *section = DECL_SECTION_NAME (decl);
32743 if (compare_section_name (section, ".sdata")
32744 || compare_section_name (section, ".sdata2")
32745 || compare_section_name (section, ".gnu.linkonce.s")
32746 || compare_section_name (section, ".sbss")
32747 || compare_section_name (section, ".sbss2")
32748 || compare_section_name (section, ".gnu.linkonce.sb")
32749 || strcmp (section, ".PPC.EMB.sdata0") == 0
32750 || strcmp (section, ".PPC.EMB.sbss0") == 0)
32751 return true;
32753 else
32755 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
32757 if (size > 0
32758 && size <= g_switch_value
32759 /* If it's not public, and we're not going to reference it there,
32760 there's no need to put it in the small data section. */
32761 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
32762 return true;
32765 return false;
32768 #endif /* USING_ELFOS_H */
32770 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
32772 static bool
32773 rs6000_use_blocks_for_constant_p (machine_mode mode, const_rtx x)
32775 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
32778 /* Do not place thread-local symbols refs in the object blocks. */
32780 static bool
32781 rs6000_use_blocks_for_decl_p (const_tree decl)
32783 return !DECL_THREAD_LOCAL_P (decl);
32786 /* Return a REG that occurs in ADDR with coefficient 1.
32787 ADDR can be effectively incremented by incrementing REG.
32789 r0 is special and we must not select it as an address
32790 register by this routine since our caller will try to
32791 increment the returned register via an "la" instruction. */
32794 find_addr_reg (rtx addr)
32796 while (GET_CODE (addr) == PLUS)
32798 if (GET_CODE (XEXP (addr, 0)) == REG
32799 && REGNO (XEXP (addr, 0)) != 0)
32800 addr = XEXP (addr, 0);
32801 else if (GET_CODE (XEXP (addr, 1)) == REG
32802 && REGNO (XEXP (addr, 1)) != 0)
32803 addr = XEXP (addr, 1);
32804 else if (CONSTANT_P (XEXP (addr, 0)))
32805 addr = XEXP (addr, 1);
32806 else if (CONSTANT_P (XEXP (addr, 1)))
32807 addr = XEXP (addr, 0);
32808 else
32809 gcc_unreachable ();
32811 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
32812 return addr;
32815 void
32816 rs6000_fatal_bad_address (rtx op)
32818 fatal_insn ("bad address", op);
32821 #if TARGET_MACHO
32823 typedef struct branch_island_d {
32824 tree function_name;
32825 tree label_name;
32826 int line_number;
32827 } branch_island;
32830 static vec<branch_island, va_gc> *branch_islands;
32832 /* Remember to generate a branch island for far calls to the given
32833 function. */
32835 static void
32836 add_compiler_branch_island (tree label_name, tree function_name,
32837 int line_number)
32839 branch_island bi = {function_name, label_name, line_number};
32840 vec_safe_push (branch_islands, bi);
32843 /* Generate far-jump branch islands for everything recorded in
32844 branch_islands. Invoked immediately after the last instruction of
32845 the epilogue has been emitted; the branch islands must be appended
32846 to, and contiguous with, the function body. Mach-O stubs are
32847 generated in machopic_output_stub(). */
32849 static void
32850 macho_branch_islands (void)
32852 char tmp_buf[512];
32854 while (!vec_safe_is_empty (branch_islands))
32856 branch_island *bi = &branch_islands->last ();
32857 const char *label = IDENTIFIER_POINTER (bi->label_name);
32858 const char *name = IDENTIFIER_POINTER (bi->function_name);
32859 char name_buf[512];
32860 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
32861 if (name[0] == '*' || name[0] == '&')
32862 strcpy (name_buf, name+1);
32863 else
32865 name_buf[0] = '_';
32866 strcpy (name_buf+1, name);
32868 strcpy (tmp_buf, "\n");
32869 strcat (tmp_buf, label);
32870 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
32871 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
32872 dbxout_stabd (N_SLINE, bi->line_number);
32873 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
32874 if (flag_pic)
32876 if (TARGET_LINK_STACK)
32878 char name[32];
32879 get_ppc476_thunk_name (name);
32880 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
32881 strcat (tmp_buf, name);
32882 strcat (tmp_buf, "\n");
32883 strcat (tmp_buf, label);
32884 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
32886 else
32888 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
32889 strcat (tmp_buf, label);
32890 strcat (tmp_buf, "_pic\n");
32891 strcat (tmp_buf, label);
32892 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
32895 strcat (tmp_buf, "\taddis r11,r11,ha16(");
32896 strcat (tmp_buf, name_buf);
32897 strcat (tmp_buf, " - ");
32898 strcat (tmp_buf, label);
32899 strcat (tmp_buf, "_pic)\n");
32901 strcat (tmp_buf, "\tmtlr r0\n");
32903 strcat (tmp_buf, "\taddi r12,r11,lo16(");
32904 strcat (tmp_buf, name_buf);
32905 strcat (tmp_buf, " - ");
32906 strcat (tmp_buf, label);
32907 strcat (tmp_buf, "_pic)\n");
32909 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
32911 else
32913 strcat (tmp_buf, ":\nlis r12,hi16(");
32914 strcat (tmp_buf, name_buf);
32915 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
32916 strcat (tmp_buf, name_buf);
32917 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
32919 output_asm_insn (tmp_buf, 0);
32920 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
32921 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
32922 dbxout_stabd (N_SLINE, bi->line_number);
32923 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
32924 branch_islands->pop ();
32928 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
32929 already there or not. */
32931 static int
32932 no_previous_def (tree function_name)
32934 branch_island *bi;
32935 unsigned ix;
32937 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
32938 if (function_name == bi->function_name)
32939 return 0;
32940 return 1;
32943 /* GET_PREV_LABEL gets the label name from the previous definition of
32944 the function. */
32946 static tree
32947 get_prev_label (tree function_name)
32949 branch_island *bi;
32950 unsigned ix;
32952 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
32953 if (function_name == bi->function_name)
32954 return bi->label_name;
32955 return NULL_TREE;
32958 /* INSN is either a function call or a millicode call. It may have an
32959 unconditional jump in its delay slot.
32961 CALL_DEST is the routine we are calling. */
32963 char *
32964 output_call (rtx_insn *insn, rtx *operands, int dest_operand_number,
32965 int cookie_operand_number)
32967 static char buf[256];
32968 if (darwin_emit_branch_islands
32969 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
32970 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
32972 tree labelname;
32973 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
32975 if (no_previous_def (funname))
32977 rtx label_rtx = gen_label_rtx ();
32978 char *label_buf, temp_buf[256];
32979 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
32980 CODE_LABEL_NUMBER (label_rtx));
32981 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
32982 labelname = get_identifier (label_buf);
32983 add_compiler_branch_island (labelname, funname, insn_line (insn));
32985 else
32986 labelname = get_prev_label (funname);
32988 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
32989 instruction will reach 'foo', otherwise link as 'bl L42'".
32990 "L42" should be a 'branch island', that will do a far jump to
32991 'foo'. Branch islands are generated in
32992 macho_branch_islands(). */
32993 sprintf (buf, "jbsr %%z%d,%.246s",
32994 dest_operand_number, IDENTIFIER_POINTER (labelname));
32996 else
32997 sprintf (buf, "bl %%z%d", dest_operand_number);
32998 return buf;
33001 /* Generate PIC and indirect symbol stubs. */
33003 void
33004 machopic_output_stub (FILE *file, const char *symb, const char *stub)
33006 unsigned int length;
33007 char *symbol_name, *lazy_ptr_name;
33008 char *local_label_0;
33009 static int label = 0;
33011 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
33012 symb = (*targetm.strip_name_encoding) (symb);
33015 length = strlen (symb);
33016 symbol_name = XALLOCAVEC (char, length + 32);
33017 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
33019 lazy_ptr_name = XALLOCAVEC (char, length + 32);
33020 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
33022 if (flag_pic == 2)
33023 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
33024 else
33025 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
33027 if (flag_pic == 2)
33029 fprintf (file, "\t.align 5\n");
33031 fprintf (file, "%s:\n", stub);
33032 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
33034 label++;
33035 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
33036 sprintf (local_label_0, "\"L%011d$spb\"", label);
33038 fprintf (file, "\tmflr r0\n");
33039 if (TARGET_LINK_STACK)
33041 char name[32];
33042 get_ppc476_thunk_name (name);
33043 fprintf (file, "\tbl %s\n", name);
33044 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
33046 else
33048 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
33049 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
33051 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
33052 lazy_ptr_name, local_label_0);
33053 fprintf (file, "\tmtlr r0\n");
33054 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
33055 (TARGET_64BIT ? "ldu" : "lwzu"),
33056 lazy_ptr_name, local_label_0);
33057 fprintf (file, "\tmtctr r12\n");
33058 fprintf (file, "\tbctr\n");
33060 else
33062 fprintf (file, "\t.align 4\n");
33064 fprintf (file, "%s:\n", stub);
33065 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
33067 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
33068 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
33069 (TARGET_64BIT ? "ldu" : "lwzu"),
33070 lazy_ptr_name);
33071 fprintf (file, "\tmtctr r12\n");
33072 fprintf (file, "\tbctr\n");
33075 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
33076 fprintf (file, "%s:\n", lazy_ptr_name);
33077 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
33078 fprintf (file, "%sdyld_stub_binding_helper\n",
33079 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
33082 /* Legitimize PIC addresses. If the address is already
33083 position-independent, we return ORIG. Newly generated
33084 position-independent addresses go into a reg. This is REG if non
33085 zero, otherwise we allocate register(s) as necessary. */
33087 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
33090 rs6000_machopic_legitimize_pic_address (rtx orig, machine_mode mode,
33091 rtx reg)
33093 rtx base, offset;
33095 if (reg == NULL && !reload_completed)
33096 reg = gen_reg_rtx (Pmode);
33098 if (GET_CODE (orig) == CONST)
33100 rtx reg_temp;
33102 if (GET_CODE (XEXP (orig, 0)) == PLUS
33103 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
33104 return orig;
33106 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
33108 /* Use a different reg for the intermediate value, as
33109 it will be marked UNCHANGING. */
33110 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
33111 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
33112 Pmode, reg_temp);
33113 offset =
33114 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
33115 Pmode, reg);
33117 if (GET_CODE (offset) == CONST_INT)
33119 if (SMALL_INT (offset))
33120 return plus_constant (Pmode, base, INTVAL (offset));
33121 else if (!reload_completed)
33122 offset = force_reg (Pmode, offset);
33123 else
33125 rtx mem = force_const_mem (Pmode, orig);
33126 return machopic_legitimize_pic_address (mem, Pmode, reg);
33129 return gen_rtx_PLUS (Pmode, base, offset);
33132 /* Fall back on generic machopic code. */
33133 return machopic_legitimize_pic_address (orig, mode, reg);
33136 /* Output a .machine directive for the Darwin assembler, and call
33137 the generic start_file routine. */
33139 static void
33140 rs6000_darwin_file_start (void)
33142 static const struct
33144 const char *arg;
33145 const char *name;
33146 HOST_WIDE_INT if_set;
33147 } mapping[] = {
33148 { "ppc64", "ppc64", MASK_64BIT },
33149 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
33150 { "power4", "ppc970", 0 },
33151 { "G5", "ppc970", 0 },
33152 { "7450", "ppc7450", 0 },
33153 { "7400", "ppc7400", MASK_ALTIVEC },
33154 { "G4", "ppc7400", 0 },
33155 { "750", "ppc750", 0 },
33156 { "740", "ppc750", 0 },
33157 { "G3", "ppc750", 0 },
33158 { "604e", "ppc604e", 0 },
33159 { "604", "ppc604", 0 },
33160 { "603e", "ppc603", 0 },
33161 { "603", "ppc603", 0 },
33162 { "601", "ppc601", 0 },
33163 { NULL, "ppc", 0 } };
33164 const char *cpu_id = "";
33165 size_t i;
33167 rs6000_file_start ();
33168 darwin_file_start ();
33170 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
33172 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
33173 cpu_id = rs6000_default_cpu;
33175 if (global_options_set.x_rs6000_cpu_index)
33176 cpu_id = processor_target_table[rs6000_cpu_index].name;
33178 /* Look through the mapping array. Pick the first name that either
33179 matches the argument, has a bit set in IF_SET that is also set
33180 in the target flags, or has a NULL name. */
33182 i = 0;
33183 while (mapping[i].arg != NULL
33184 && strcmp (mapping[i].arg, cpu_id) != 0
33185 && (mapping[i].if_set & rs6000_isa_flags) == 0)
33186 i++;
33188 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
33191 #endif /* TARGET_MACHO */
33193 #if TARGET_ELF
33194 static int
33195 rs6000_elf_reloc_rw_mask (void)
33197 if (flag_pic)
33198 return 3;
33199 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
33200 return 2;
33201 else
33202 return 0;
33205 /* Record an element in the table of global constructors. SYMBOL is
33206 a SYMBOL_REF of the function to be called; PRIORITY is a number
33207 between 0 and MAX_INIT_PRIORITY.
33209 This differs from default_named_section_asm_out_constructor in
33210 that we have special handling for -mrelocatable. */
33212 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
33213 static void
33214 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
33216 const char *section = ".ctors";
33217 char buf[18];
33219 if (priority != DEFAULT_INIT_PRIORITY)
33221 sprintf (buf, ".ctors.%.5u",
33222 /* Invert the numbering so the linker puts us in the proper
33223 order; constructors are run from right to left, and the
33224 linker sorts in increasing order. */
33225 MAX_INIT_PRIORITY - priority);
33226 section = buf;
33229 switch_to_section (get_section (section, SECTION_WRITE, NULL));
33230 assemble_align (POINTER_SIZE);
33232 if (DEFAULT_ABI == ABI_V4
33233 && (TARGET_RELOCATABLE || flag_pic > 1))
33235 fputs ("\t.long (", asm_out_file);
33236 output_addr_const (asm_out_file, symbol);
33237 fputs (")@fixup\n", asm_out_file);
33239 else
33240 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
33243 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
33244 static void
33245 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
33247 const char *section = ".dtors";
33248 char buf[18];
33250 if (priority != DEFAULT_INIT_PRIORITY)
33252 sprintf (buf, ".dtors.%.5u",
33253 /* Invert the numbering so the linker puts us in the proper
33254 order; constructors are run from right to left, and the
33255 linker sorts in increasing order. */
33256 MAX_INIT_PRIORITY - priority);
33257 section = buf;
33260 switch_to_section (get_section (section, SECTION_WRITE, NULL));
33261 assemble_align (POINTER_SIZE);
33263 if (DEFAULT_ABI == ABI_V4
33264 && (TARGET_RELOCATABLE || flag_pic > 1))
33266 fputs ("\t.long (", asm_out_file);
33267 output_addr_const (asm_out_file, symbol);
33268 fputs (")@fixup\n", asm_out_file);
33270 else
33271 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
33274 void
33275 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
33277 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
33279 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
33280 ASM_OUTPUT_LABEL (file, name);
33281 fputs (DOUBLE_INT_ASM_OP, file);
33282 rs6000_output_function_entry (file, name);
33283 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
33284 if (DOT_SYMBOLS)
33286 fputs ("\t.size\t", file);
33287 assemble_name (file, name);
33288 fputs (",24\n\t.type\t.", file);
33289 assemble_name (file, name);
33290 fputs (",@function\n", file);
33291 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
33293 fputs ("\t.globl\t.", file);
33294 assemble_name (file, name);
33295 putc ('\n', file);
33298 else
33299 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
33300 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
33301 rs6000_output_function_entry (file, name);
33302 fputs (":\n", file);
33303 return;
33306 if (DEFAULT_ABI == ABI_V4
33307 && (TARGET_RELOCATABLE || flag_pic > 1)
33308 && !TARGET_SECURE_PLT
33309 && (!constant_pool_empty_p () || crtl->profile)
33310 && uses_TOC ())
33312 char buf[256];
33314 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
33316 fprintf (file, "\t.long ");
33317 assemble_name (file, toc_label_name);
33318 need_toc_init = 1;
33319 putc ('-', file);
33320 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
33321 assemble_name (file, buf);
33322 putc ('\n', file);
33325 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
33326 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
33328 if (TARGET_CMODEL == CMODEL_LARGE && rs6000_global_entry_point_needed_p ())
33330 char buf[256];
33332 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
33334 fprintf (file, "\t.quad .TOC.-");
33335 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
33336 assemble_name (file, buf);
33337 putc ('\n', file);
33340 if (DEFAULT_ABI == ABI_AIX)
33342 const char *desc_name, *orig_name;
33344 orig_name = (*targetm.strip_name_encoding) (name);
33345 desc_name = orig_name;
33346 while (*desc_name == '.')
33347 desc_name++;
33349 if (TREE_PUBLIC (decl))
33350 fprintf (file, "\t.globl %s\n", desc_name);
33352 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
33353 fprintf (file, "%s:\n", desc_name);
33354 fprintf (file, "\t.long %s\n", orig_name);
33355 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
33356 fputs ("\t.long 0\n", file);
33357 fprintf (file, "\t.previous\n");
33359 ASM_OUTPUT_LABEL (file, name);
33362 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
33363 static void
33364 rs6000_elf_file_end (void)
33366 #ifdef HAVE_AS_GNU_ATTRIBUTE
33367 /* ??? The value emitted depends on options active at file end.
33368 Assume anyone using #pragma or attributes that might change
33369 options knows what they are doing. */
33370 if ((TARGET_64BIT || DEFAULT_ABI == ABI_V4)
33371 && rs6000_passes_float)
33373 int fp;
33375 if (TARGET_DF_FPR)
33376 fp = 1;
33377 else if (TARGET_SF_FPR)
33378 fp = 3;
33379 else
33380 fp = 2;
33381 if (rs6000_passes_long_double)
33383 if (!TARGET_LONG_DOUBLE_128)
33384 fp |= 2 * 4;
33385 else if (TARGET_IEEEQUAD)
33386 fp |= 3 * 4;
33387 else
33388 fp |= 1 * 4;
33390 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", fp);
33392 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
33394 if (rs6000_passes_vector)
33395 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
33396 (TARGET_ALTIVEC_ABI ? 2 : 1));
33397 if (rs6000_returns_struct)
33398 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
33399 aix_struct_return ? 2 : 1);
33401 #endif
33402 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
33403 if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
33404 file_end_indicate_exec_stack ();
33405 #endif
33407 if (flag_split_stack)
33408 file_end_indicate_split_stack ();
33410 if (cpu_builtin_p)
33412 /* We have expanded a CPU builtin, so we need to emit a reference to
33413 the special symbol that LIBC uses to declare it supports the
33414 AT_PLATFORM and AT_HWCAP/AT_HWCAP2 in the TCB feature. */
33415 switch_to_section (data_section);
33416 fprintf (asm_out_file, "\t.align %u\n", TARGET_32BIT ? 2 : 3);
33417 fprintf (asm_out_file, "\t%s %s\n",
33418 TARGET_32BIT ? ".long" : ".quad", tcb_verification_symbol);
33421 #endif
33423 #if TARGET_XCOFF
33425 #ifndef HAVE_XCOFF_DWARF_EXTRAS
33426 #define HAVE_XCOFF_DWARF_EXTRAS 0
33427 #endif
33429 static enum unwind_info_type
33430 rs6000_xcoff_debug_unwind_info (void)
33432 return UI_NONE;
33435 static void
33436 rs6000_xcoff_asm_output_anchor (rtx symbol)
33438 char buffer[100];
33440 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
33441 SYMBOL_REF_BLOCK_OFFSET (symbol));
33442 fprintf (asm_out_file, "%s", SET_ASM_OP);
33443 RS6000_OUTPUT_BASENAME (asm_out_file, XSTR (symbol, 0));
33444 fprintf (asm_out_file, ",");
33445 RS6000_OUTPUT_BASENAME (asm_out_file, buffer);
33446 fprintf (asm_out_file, "\n");
33449 static void
33450 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
33452 fputs (GLOBAL_ASM_OP, stream);
33453 RS6000_OUTPUT_BASENAME (stream, name);
33454 putc ('\n', stream);
33457 /* A get_unnamed_decl callback, used for read-only sections. PTR
33458 points to the section string variable. */
33460 static void
33461 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
33463 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
33464 *(const char *const *) directive,
33465 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
33468 /* Likewise for read-write sections. */
33470 static void
33471 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
33473 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
33474 *(const char *const *) directive,
33475 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
33478 static void
33479 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
33481 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
33482 *(const char *const *) directive,
33483 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
33486 /* A get_unnamed_section callback, used for switching to toc_section. */
33488 static void
33489 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
33491 if (TARGET_MINIMAL_TOC)
33493 /* toc_section is always selected at least once from
33494 rs6000_xcoff_file_start, so this is guaranteed to
33495 always be defined once and only once in each file. */
33496 if (!toc_initialized)
33498 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
33499 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
33500 toc_initialized = 1;
33502 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
33503 (TARGET_32BIT ? "" : ",3"));
33505 else
33506 fputs ("\t.toc\n", asm_out_file);
33509 /* Implement TARGET_ASM_INIT_SECTIONS. */
33511 static void
33512 rs6000_xcoff_asm_init_sections (void)
33514 read_only_data_section
33515 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
33516 &xcoff_read_only_section_name);
33518 private_data_section
33519 = get_unnamed_section (SECTION_WRITE,
33520 rs6000_xcoff_output_readwrite_section_asm_op,
33521 &xcoff_private_data_section_name);
33523 tls_data_section
33524 = get_unnamed_section (SECTION_TLS,
33525 rs6000_xcoff_output_tls_section_asm_op,
33526 &xcoff_tls_data_section_name);
33528 tls_private_data_section
33529 = get_unnamed_section (SECTION_TLS,
33530 rs6000_xcoff_output_tls_section_asm_op,
33531 &xcoff_private_data_section_name);
33533 read_only_private_data_section
33534 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
33535 &xcoff_private_data_section_name);
33537 toc_section
33538 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
33540 readonly_data_section = read_only_data_section;
33543 static int
33544 rs6000_xcoff_reloc_rw_mask (void)
33546 return 3;
33549 static void
33550 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
33551 tree decl ATTRIBUTE_UNUSED)
33553 int smclass;
33554 static const char * const suffix[5] = { "PR", "RO", "RW", "TL", "XO" };
33556 if (flags & SECTION_EXCLUDE)
33557 smclass = 4;
33558 else if (flags & SECTION_DEBUG)
33560 fprintf (asm_out_file, "\t.dwsect %s\n", name);
33561 return;
33563 else if (flags & SECTION_CODE)
33564 smclass = 0;
33565 else if (flags & SECTION_TLS)
33566 smclass = 3;
33567 else if (flags & SECTION_WRITE)
33568 smclass = 2;
33569 else
33570 smclass = 1;
33572 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
33573 (flags & SECTION_CODE) ? "." : "",
33574 name, suffix[smclass], flags & SECTION_ENTSIZE);
33577 #define IN_NAMED_SECTION(DECL) \
33578 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
33579 && DECL_SECTION_NAME (DECL) != NULL)
33581 static section *
33582 rs6000_xcoff_select_section (tree decl, int reloc,
33583 unsigned HOST_WIDE_INT align)
33585 /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
33586 named section. */
33587 if (align > BIGGEST_ALIGNMENT)
33589 resolve_unique_section (decl, reloc, true);
33590 if (IN_NAMED_SECTION (decl))
33591 return get_named_section (decl, NULL, reloc);
33594 if (decl_readonly_section (decl, reloc))
33596 if (TREE_PUBLIC (decl))
33597 return read_only_data_section;
33598 else
33599 return read_only_private_data_section;
33601 else
33603 #if HAVE_AS_TLS
33604 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
33606 if (TREE_PUBLIC (decl))
33607 return tls_data_section;
33608 else if (bss_initializer_p (decl))
33610 /* Convert to COMMON to emit in BSS. */
33611 DECL_COMMON (decl) = 1;
33612 return tls_comm_section;
33614 else
33615 return tls_private_data_section;
33617 else
33618 #endif
33619 if (TREE_PUBLIC (decl))
33620 return data_section;
33621 else
33622 return private_data_section;
33626 static void
33627 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
33629 const char *name;
33631 /* Use select_section for private data and uninitialized data with
33632 alignment <= BIGGEST_ALIGNMENT. */
33633 if (!TREE_PUBLIC (decl)
33634 || DECL_COMMON (decl)
33635 || (DECL_INITIAL (decl) == NULL_TREE
33636 && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
33637 || DECL_INITIAL (decl) == error_mark_node
33638 || (flag_zero_initialized_in_bss
33639 && initializer_zerop (DECL_INITIAL (decl))))
33640 return;
33642 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
33643 name = (*targetm.strip_name_encoding) (name);
33644 set_decl_section_name (decl, name);
33647 /* Select section for constant in constant pool.
33649 On RS/6000, all constants are in the private read-only data area.
33650 However, if this is being placed in the TOC it must be output as a
33651 toc entry. */
33653 static section *
33654 rs6000_xcoff_select_rtx_section (machine_mode mode, rtx x,
33655 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
33657 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
33658 return toc_section;
33659 else
33660 return read_only_private_data_section;
33663 /* Remove any trailing [DS] or the like from the symbol name. */
33665 static const char *
33666 rs6000_xcoff_strip_name_encoding (const char *name)
33668 size_t len;
33669 if (*name == '*')
33670 name++;
33671 len = strlen (name);
33672 if (name[len - 1] == ']')
33673 return ggc_alloc_string (name, len - 4);
33674 else
33675 return name;
33678 /* Section attributes. AIX is always PIC. */
33680 static unsigned int
33681 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
33683 unsigned int align;
33684 unsigned int flags = default_section_type_flags (decl, name, reloc);
33686 /* Align to at least UNIT size. */
33687 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
33688 align = MIN_UNITS_PER_WORD;
33689 else
33690 /* Increase alignment of large objects if not already stricter. */
33691 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
33692 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
33693 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
33695 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
33698 /* Output at beginning of assembler file.
33700 Initialize the section names for the RS/6000 at this point.
33702 Specify filename, including full path, to assembler.
33704 We want to go into the TOC section so at least one .toc will be emitted.
33705 Also, in order to output proper .bs/.es pairs, we need at least one static
33706 [RW] section emitted.
33708 Finally, declare mcount when profiling to make the assembler happy. */
33710 static void
33711 rs6000_xcoff_file_start (void)
33713 rs6000_gen_section_name (&xcoff_bss_section_name,
33714 main_input_filename, ".bss_");
33715 rs6000_gen_section_name (&xcoff_private_data_section_name,
33716 main_input_filename, ".rw_");
33717 rs6000_gen_section_name (&xcoff_read_only_section_name,
33718 main_input_filename, ".ro_");
33719 rs6000_gen_section_name (&xcoff_tls_data_section_name,
33720 main_input_filename, ".tls_");
33721 rs6000_gen_section_name (&xcoff_tbss_section_name,
33722 main_input_filename, ".tbss_[UL]");
33724 fputs ("\t.file\t", asm_out_file);
33725 output_quoted_string (asm_out_file, main_input_filename);
33726 fputc ('\n', asm_out_file);
33727 if (write_symbols != NO_DEBUG)
33728 switch_to_section (private_data_section);
33729 switch_to_section (toc_section);
33730 switch_to_section (text_section);
33731 if (profile_flag)
33732 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
33733 rs6000_file_start ();
33736 /* Output at end of assembler file.
33737 On the RS/6000, referencing data should automatically pull in text. */
33739 static void
33740 rs6000_xcoff_file_end (void)
33742 switch_to_section (text_section);
33743 fputs ("_section_.text:\n", asm_out_file);
33744 switch_to_section (data_section);
33745 fputs (TARGET_32BIT
33746 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
33747 asm_out_file);
33750 struct declare_alias_data
33752 FILE *file;
33753 bool function_descriptor;
33756 /* Declare alias N. A helper function for for_node_and_aliases. */
33758 static bool
33759 rs6000_declare_alias (struct symtab_node *n, void *d)
33761 struct declare_alias_data *data = (struct declare_alias_data *)d;
33762 /* Main symbol is output specially, because varasm machinery does part of
33763 the job for us - we do not need to declare .globl/lglobs and such. */
33764 if (!n->alias || n->weakref)
33765 return false;
33767 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (n->decl)))
33768 return false;
33770 /* Prevent assemble_alias from trying to use .set pseudo operation
33771 that does not behave as expected by the middle-end. */
33772 TREE_ASM_WRITTEN (n->decl) = true;
33774 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl));
33775 char *buffer = (char *) alloca (strlen (name) + 2);
33776 char *p;
33777 int dollar_inside = 0;
33779 strcpy (buffer, name);
33780 p = strchr (buffer, '$');
33781 while (p) {
33782 *p = '_';
33783 dollar_inside++;
33784 p = strchr (p + 1, '$');
33786 if (TREE_PUBLIC (n->decl))
33788 if (!RS6000_WEAK || !DECL_WEAK (n->decl))
33790 if (dollar_inside) {
33791 if (data->function_descriptor)
33792 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
33793 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
33795 if (data->function_descriptor)
33797 fputs ("\t.globl .", data->file);
33798 RS6000_OUTPUT_BASENAME (data->file, buffer);
33799 putc ('\n', data->file);
33801 fputs ("\t.globl ", data->file);
33802 RS6000_OUTPUT_BASENAME (data->file, buffer);
33803 putc ('\n', data->file);
33805 #ifdef ASM_WEAKEN_DECL
33806 else if (DECL_WEAK (n->decl) && !data->function_descriptor)
33807 ASM_WEAKEN_DECL (data->file, n->decl, name, NULL);
33808 #endif
33810 else
33812 if (dollar_inside)
33814 if (data->function_descriptor)
33815 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
33816 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
33818 if (data->function_descriptor)
33820 fputs ("\t.lglobl .", data->file);
33821 RS6000_OUTPUT_BASENAME (data->file, buffer);
33822 putc ('\n', data->file);
33824 fputs ("\t.lglobl ", data->file);
33825 RS6000_OUTPUT_BASENAME (data->file, buffer);
33826 putc ('\n', data->file);
33828 if (data->function_descriptor)
33829 fputs (".", data->file);
33830 RS6000_OUTPUT_BASENAME (data->file, buffer);
33831 fputs (":\n", data->file);
33832 return false;
33836 #ifdef HAVE_GAS_HIDDEN
33837 /* Helper function to calculate visibility of a DECL
33838 and return the value as a const string. */
33840 static const char *
33841 rs6000_xcoff_visibility (tree decl)
33843 static const char * const visibility_types[] = {
33844 "", ",protected", ",hidden", ",internal"
33847 enum symbol_visibility vis = DECL_VISIBILITY (decl);
33849 if (TREE_CODE (decl) == FUNCTION_DECL
33850 && cgraph_node::get (decl)
33851 && cgraph_node::get (decl)->instrumentation_clone
33852 && cgraph_node::get (decl)->instrumented_version)
33853 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
33855 return visibility_types[vis];
33857 #endif
33860 /* This macro produces the initial definition of a function name.
33861 On the RS/6000, we need to place an extra '.' in the function name and
33862 output the function descriptor.
33863 Dollar signs are converted to underscores.
33865 The csect for the function will have already been created when
33866 text_section was selected. We do have to go back to that csect, however.
33868 The third and fourth parameters to the .function pseudo-op (16 and 044)
33869 are placeholders which no longer have any use.
33871 Because AIX assembler's .set command has unexpected semantics, we output
33872 all aliases as alternative labels in front of the definition. */
33874 void
33875 rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
33877 char *buffer = (char *) alloca (strlen (name) + 1);
33878 char *p;
33879 int dollar_inside = 0;
33880 struct declare_alias_data data = {file, false};
33882 strcpy (buffer, name);
33883 p = strchr (buffer, '$');
33884 while (p) {
33885 *p = '_';
33886 dollar_inside++;
33887 p = strchr (p + 1, '$');
33889 if (TREE_PUBLIC (decl))
33891 if (!RS6000_WEAK || !DECL_WEAK (decl))
33893 if (dollar_inside) {
33894 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
33895 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
33897 fputs ("\t.globl .", file);
33898 RS6000_OUTPUT_BASENAME (file, buffer);
33899 #ifdef HAVE_GAS_HIDDEN
33900 fputs (rs6000_xcoff_visibility (decl), file);
33901 #endif
33902 putc ('\n', file);
33905 else
33907 if (dollar_inside) {
33908 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
33909 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
33911 fputs ("\t.lglobl .", file);
33912 RS6000_OUTPUT_BASENAME (file, buffer);
33913 putc ('\n', file);
33915 fputs ("\t.csect ", file);
33916 RS6000_OUTPUT_BASENAME (file, buffer);
33917 fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
33918 RS6000_OUTPUT_BASENAME (file, buffer);
33919 fputs (":\n", file);
33920 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
33921 &data, true);
33922 fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
33923 RS6000_OUTPUT_BASENAME (file, buffer);
33924 fputs (", TOC[tc0], 0\n", file);
33925 in_section = NULL;
33926 switch_to_section (function_section (decl));
33927 putc ('.', file);
33928 RS6000_OUTPUT_BASENAME (file, buffer);
33929 fputs (":\n", file);
33930 data.function_descriptor = true;
33931 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
33932 &data, true);
33933 if (!DECL_IGNORED_P (decl))
33935 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
33936 xcoffout_declare_function (file, decl, buffer);
33937 else if (write_symbols == DWARF2_DEBUG)
33939 name = (*targetm.strip_name_encoding) (name);
33940 fprintf (file, "\t.function .%s,.%s,2,0\n", name, name);
33943 return;
33947 /* Output assembly language to globalize a symbol from a DECL,
33948 possibly with visibility. */
33950 void
33951 rs6000_xcoff_asm_globalize_decl_name (FILE *stream, tree decl)
33953 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
33954 fputs (GLOBAL_ASM_OP, stream);
33955 RS6000_OUTPUT_BASENAME (stream, name);
33956 #ifdef HAVE_GAS_HIDDEN
33957 fputs (rs6000_xcoff_visibility (decl), stream);
33958 #endif
33959 putc ('\n', stream);
33962 /* Output assembly language to define a symbol as COMMON from a DECL,
33963 possibly with visibility. */
33965 void
33966 rs6000_xcoff_asm_output_aligned_decl_common (FILE *stream,
33967 tree decl ATTRIBUTE_UNUSED,
33968 const char *name,
33969 unsigned HOST_WIDE_INT size,
33970 unsigned HOST_WIDE_INT align)
33972 unsigned HOST_WIDE_INT align2 = 2;
33974 if (align > 32)
33975 align2 = floor_log2 (align / BITS_PER_UNIT);
33976 else if (size > 4)
33977 align2 = 3;
33979 fputs (COMMON_ASM_OP, stream);
33980 RS6000_OUTPUT_BASENAME (stream, name);
33982 fprintf (stream,
33983 "," HOST_WIDE_INT_PRINT_UNSIGNED "," HOST_WIDE_INT_PRINT_UNSIGNED,
33984 size, align2);
33986 #ifdef HAVE_GAS_HIDDEN
33987 fputs (rs6000_xcoff_visibility (decl), stream);
33988 #endif
33989 putc ('\n', stream);
33992 /* This macro produces the initial definition of a object (variable) name.
33993 Because AIX assembler's .set command has unexpected semantics, we output
33994 all aliases as alternative labels in front of the definition. */
33996 void
33997 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
33999 struct declare_alias_data data = {file, false};
34000 RS6000_OUTPUT_BASENAME (file, name);
34001 fputs (":\n", file);
34002 symtab_node::get_create (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
34003 &data, true);
34006 /* Overide the default 'SYMBOL-.' syntax with AIX compatible 'SYMBOL-$'. */
34008 void
34009 rs6000_asm_output_dwarf_pcrel (FILE *file, int size, const char *label)
34011 fputs (integer_asm_op (size, FALSE), file);
34012 assemble_name (file, label);
34013 fputs ("-$", file);
34016 /* Output a symbol offset relative to the dbase for the current object.
34017 We use __gcc_unwind_dbase as an arbitrary base for dbase and assume
34018 signed offsets.
34020 __gcc_unwind_dbase is embedded in all executables/libraries through
34021 libgcc/config/rs6000/crtdbase.S. */
34023 void
34024 rs6000_asm_output_dwarf_datarel (FILE *file, int size, const char *label)
34026 fputs (integer_asm_op (size, FALSE), file);
34027 assemble_name (file, label);
34028 fputs("-__gcc_unwind_dbase", file);
34031 #ifdef HAVE_AS_TLS
34032 static void
34033 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
34035 rtx symbol;
34036 int flags;
34037 const char *symname;
34039 default_encode_section_info (decl, rtl, first);
34041 /* Careful not to prod global register variables. */
34042 if (!MEM_P (rtl))
34043 return;
34044 symbol = XEXP (rtl, 0);
34045 if (GET_CODE (symbol) != SYMBOL_REF)
34046 return;
34048 flags = SYMBOL_REF_FLAGS (symbol);
34050 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
34051 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
34053 SYMBOL_REF_FLAGS (symbol) = flags;
34055 /* Append mapping class to extern decls. */
34056 symname = XSTR (symbol, 0);
34057 if (decl /* sync condition with assemble_external () */
34058 && DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
34059 && ((TREE_CODE (decl) == VAR_DECL && !DECL_THREAD_LOCAL_P (decl))
34060 || TREE_CODE (decl) == FUNCTION_DECL)
34061 && symname[strlen (symname) - 1] != ']')
34063 char *newname = (char *) alloca (strlen (symname) + 5);
34064 strcpy (newname, symname);
34065 strcat (newname, (TREE_CODE (decl) == FUNCTION_DECL
34066 ? "[DS]" : "[UA]"));
34067 XSTR (symbol, 0) = ggc_strdup (newname);
34070 #endif /* HAVE_AS_TLS */
34071 #endif /* TARGET_XCOFF */
34073 void
34074 rs6000_asm_weaken_decl (FILE *stream, tree decl,
34075 const char *name, const char *val)
34077 fputs ("\t.weak\t", stream);
34078 RS6000_OUTPUT_BASENAME (stream, name);
34079 if (decl && TREE_CODE (decl) == FUNCTION_DECL
34080 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
34082 if (TARGET_XCOFF)
34083 fputs ("[DS]", stream);
34084 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
34085 if (TARGET_XCOFF)
34086 fputs (rs6000_xcoff_visibility (decl), stream);
34087 #endif
34088 fputs ("\n\t.weak\t.", stream);
34089 RS6000_OUTPUT_BASENAME (stream, name);
34091 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
34092 if (TARGET_XCOFF)
34093 fputs (rs6000_xcoff_visibility (decl), stream);
34094 #endif
34095 fputc ('\n', stream);
34096 if (val)
34098 #ifdef ASM_OUTPUT_DEF
34099 ASM_OUTPUT_DEF (stream, name, val);
34100 #endif
34101 if (decl && TREE_CODE (decl) == FUNCTION_DECL
34102 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
34104 fputs ("\t.set\t.", stream);
34105 RS6000_OUTPUT_BASENAME (stream, name);
34106 fputs (",.", stream);
34107 RS6000_OUTPUT_BASENAME (stream, val);
34108 fputc ('\n', stream);
34114 /* Return true if INSN should not be copied. */
34116 static bool
34117 rs6000_cannot_copy_insn_p (rtx_insn *insn)
34119 return recog_memoized (insn) >= 0
34120 && get_attr_cannot_copy (insn);
34123 /* Compute a (partial) cost for rtx X. Return true if the complete
34124 cost has been computed, and false if subexpressions should be
34125 scanned. In either case, *TOTAL contains the cost result. */
34127 static bool
34128 rs6000_rtx_costs (rtx x, machine_mode mode, int outer_code,
34129 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
34131 int code = GET_CODE (x);
34133 switch (code)
34135 /* On the RS/6000, if it is valid in the insn, it is free. */
34136 case CONST_INT:
34137 if (((outer_code == SET
34138 || outer_code == PLUS
34139 || outer_code == MINUS)
34140 && (satisfies_constraint_I (x)
34141 || satisfies_constraint_L (x)))
34142 || (outer_code == AND
34143 && (satisfies_constraint_K (x)
34144 || (mode == SImode
34145 ? satisfies_constraint_L (x)
34146 : satisfies_constraint_J (x))))
34147 || ((outer_code == IOR || outer_code == XOR)
34148 && (satisfies_constraint_K (x)
34149 || (mode == SImode
34150 ? satisfies_constraint_L (x)
34151 : satisfies_constraint_J (x))))
34152 || outer_code == ASHIFT
34153 || outer_code == ASHIFTRT
34154 || outer_code == LSHIFTRT
34155 || outer_code == ROTATE
34156 || outer_code == ROTATERT
34157 || outer_code == ZERO_EXTRACT
34158 || (outer_code == MULT
34159 && satisfies_constraint_I (x))
34160 || ((outer_code == DIV || outer_code == UDIV
34161 || outer_code == MOD || outer_code == UMOD)
34162 && exact_log2 (INTVAL (x)) >= 0)
34163 || (outer_code == COMPARE
34164 && (satisfies_constraint_I (x)
34165 || satisfies_constraint_K (x)))
34166 || ((outer_code == EQ || outer_code == NE)
34167 && (satisfies_constraint_I (x)
34168 || satisfies_constraint_K (x)
34169 || (mode == SImode
34170 ? satisfies_constraint_L (x)
34171 : satisfies_constraint_J (x))))
34172 || (outer_code == GTU
34173 && satisfies_constraint_I (x))
34174 || (outer_code == LTU
34175 && satisfies_constraint_P (x)))
34177 *total = 0;
34178 return true;
34180 else if ((outer_code == PLUS
34181 && reg_or_add_cint_operand (x, VOIDmode))
34182 || (outer_code == MINUS
34183 && reg_or_sub_cint_operand (x, VOIDmode))
34184 || ((outer_code == SET
34185 || outer_code == IOR
34186 || outer_code == XOR)
34187 && (INTVAL (x)
34188 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
34190 *total = COSTS_N_INSNS (1);
34191 return true;
34193 /* FALLTHRU */
34195 case CONST_DOUBLE:
34196 case CONST_WIDE_INT:
34197 case CONST:
34198 case HIGH:
34199 case SYMBOL_REF:
34200 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
34201 return true;
34203 case MEM:
34204 /* When optimizing for size, MEM should be slightly more expensive
34205 than generating address, e.g., (plus (reg) (const)).
34206 L1 cache latency is about two instructions. */
34207 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
34208 if (SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (x)))
34209 *total += COSTS_N_INSNS (100);
34210 return true;
34212 case LABEL_REF:
34213 *total = 0;
34214 return true;
34216 case PLUS:
34217 case MINUS:
34218 if (FLOAT_MODE_P (mode))
34219 *total = rs6000_cost->fp;
34220 else
34221 *total = COSTS_N_INSNS (1);
34222 return false;
34224 case MULT:
34225 if (GET_CODE (XEXP (x, 1)) == CONST_INT
34226 && satisfies_constraint_I (XEXP (x, 1)))
34228 if (INTVAL (XEXP (x, 1)) >= -256
34229 && INTVAL (XEXP (x, 1)) <= 255)
34230 *total = rs6000_cost->mulsi_const9;
34231 else
34232 *total = rs6000_cost->mulsi_const;
34234 else if (mode == SFmode)
34235 *total = rs6000_cost->fp;
34236 else if (FLOAT_MODE_P (mode))
34237 *total = rs6000_cost->dmul;
34238 else if (mode == DImode)
34239 *total = rs6000_cost->muldi;
34240 else
34241 *total = rs6000_cost->mulsi;
34242 return false;
34244 case FMA:
34245 if (mode == SFmode)
34246 *total = rs6000_cost->fp;
34247 else
34248 *total = rs6000_cost->dmul;
34249 break;
34251 case DIV:
34252 case MOD:
34253 if (FLOAT_MODE_P (mode))
34255 *total = mode == DFmode ? rs6000_cost->ddiv
34256 : rs6000_cost->sdiv;
34257 return false;
34259 /* FALLTHRU */
34261 case UDIV:
34262 case UMOD:
34263 if (GET_CODE (XEXP (x, 1)) == CONST_INT
34264 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
34266 if (code == DIV || code == MOD)
34267 /* Shift, addze */
34268 *total = COSTS_N_INSNS (2);
34269 else
34270 /* Shift */
34271 *total = COSTS_N_INSNS (1);
34273 else
34275 if (GET_MODE (XEXP (x, 1)) == DImode)
34276 *total = rs6000_cost->divdi;
34277 else
34278 *total = rs6000_cost->divsi;
34280 /* Add in shift and subtract for MOD unless we have a mod instruction. */
34281 if (!TARGET_MODULO && (code == MOD || code == UMOD))
34282 *total += COSTS_N_INSNS (2);
34283 return false;
34285 case CTZ:
34286 *total = COSTS_N_INSNS (TARGET_CTZ ? 1 : 4);
34287 return false;
34289 case FFS:
34290 *total = COSTS_N_INSNS (4);
34291 return false;
34293 case POPCOUNT:
34294 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
34295 return false;
34297 case PARITY:
34298 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
34299 return false;
34301 case NOT:
34302 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
34303 *total = 0;
34304 else
34305 *total = COSTS_N_INSNS (1);
34306 return false;
34308 case AND:
34309 if (CONST_INT_P (XEXP (x, 1)))
34311 rtx left = XEXP (x, 0);
34312 rtx_code left_code = GET_CODE (left);
34314 /* rotate-and-mask: 1 insn. */
34315 if ((left_code == ROTATE
34316 || left_code == ASHIFT
34317 || left_code == LSHIFTRT)
34318 && rs6000_is_valid_shift_mask (XEXP (x, 1), left, mode))
34320 *total = rtx_cost (XEXP (left, 0), mode, left_code, 0, speed);
34321 if (!CONST_INT_P (XEXP (left, 1)))
34322 *total += rtx_cost (XEXP (left, 1), SImode, left_code, 1, speed);
34323 *total += COSTS_N_INSNS (1);
34324 return true;
34327 /* rotate-and-mask (no rotate), andi., andis.: 1 insn. */
34328 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
34329 if (rs6000_is_valid_and_mask (XEXP (x, 1), mode)
34330 || (val & 0xffff) == val
34331 || (val & 0xffff0000) == val
34332 || ((val & 0xffff) == 0 && mode == SImode))
34334 *total = rtx_cost (left, mode, AND, 0, speed);
34335 *total += COSTS_N_INSNS (1);
34336 return true;
34339 /* 2 insns. */
34340 if (rs6000_is_valid_2insn_and (XEXP (x, 1), mode))
34342 *total = rtx_cost (left, mode, AND, 0, speed);
34343 *total += COSTS_N_INSNS (2);
34344 return true;
34348 *total = COSTS_N_INSNS (1);
34349 return false;
34351 case IOR:
34352 /* FIXME */
34353 *total = COSTS_N_INSNS (1);
34354 return true;
34356 case CLZ:
34357 case XOR:
34358 case ZERO_EXTRACT:
34359 *total = COSTS_N_INSNS (1);
34360 return false;
34362 case ASHIFT:
34363 /* The EXTSWSLI instruction is a combined instruction. Don't count both
34364 the sign extend and shift separately within the insn. */
34365 if (TARGET_EXTSWSLI && mode == DImode
34366 && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
34367 && GET_MODE (XEXP (XEXP (x, 0), 0)) == SImode)
34369 *total = 0;
34370 return false;
34372 /* fall through */
34374 case ASHIFTRT:
34375 case LSHIFTRT:
34376 case ROTATE:
34377 case ROTATERT:
34378 /* Handle mul_highpart. */
34379 if (outer_code == TRUNCATE
34380 && GET_CODE (XEXP (x, 0)) == MULT)
34382 if (mode == DImode)
34383 *total = rs6000_cost->muldi;
34384 else
34385 *total = rs6000_cost->mulsi;
34386 return true;
34388 else if (outer_code == AND)
34389 *total = 0;
34390 else
34391 *total = COSTS_N_INSNS (1);
34392 return false;
34394 case SIGN_EXTEND:
34395 case ZERO_EXTEND:
34396 if (GET_CODE (XEXP (x, 0)) == MEM)
34397 *total = 0;
34398 else
34399 *total = COSTS_N_INSNS (1);
34400 return false;
34402 case COMPARE:
34403 case NEG:
34404 case ABS:
34405 if (!FLOAT_MODE_P (mode))
34407 *total = COSTS_N_INSNS (1);
34408 return false;
34410 /* FALLTHRU */
34412 case FLOAT:
34413 case UNSIGNED_FLOAT:
34414 case FIX:
34415 case UNSIGNED_FIX:
34416 case FLOAT_TRUNCATE:
34417 *total = rs6000_cost->fp;
34418 return false;
34420 case FLOAT_EXTEND:
34421 if (mode == DFmode)
34422 *total = rs6000_cost->sfdf_convert;
34423 else
34424 *total = rs6000_cost->fp;
34425 return false;
34427 case UNSPEC:
34428 switch (XINT (x, 1))
34430 case UNSPEC_FRSP:
34431 *total = rs6000_cost->fp;
34432 return true;
34434 default:
34435 break;
34437 break;
34439 case CALL:
34440 case IF_THEN_ELSE:
34441 if (!speed)
34443 *total = COSTS_N_INSNS (1);
34444 return true;
34446 else if (FLOAT_MODE_P (mode) && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT)
34448 *total = rs6000_cost->fp;
34449 return false;
34451 break;
34453 case NE:
34454 case EQ:
34455 case GTU:
34456 case LTU:
34457 /* Carry bit requires mode == Pmode.
34458 NEG or PLUS already counted so only add one. */
34459 if (mode == Pmode
34460 && (outer_code == NEG || outer_code == PLUS))
34462 *total = COSTS_N_INSNS (1);
34463 return true;
34465 if (outer_code == SET)
34467 if (XEXP (x, 1) == const0_rtx)
34469 if (TARGET_ISEL && !TARGET_MFCRF)
34470 *total = COSTS_N_INSNS (8);
34471 else
34472 *total = COSTS_N_INSNS (2);
34473 return true;
34475 else
34477 *total = COSTS_N_INSNS (3);
34478 return false;
34481 /* FALLTHRU */
34483 case GT:
34484 case LT:
34485 case UNORDERED:
34486 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
34488 if (TARGET_ISEL && !TARGET_MFCRF)
34489 *total = COSTS_N_INSNS (8);
34490 else
34491 *total = COSTS_N_INSNS (2);
34492 return true;
34494 /* CC COMPARE. */
34495 if (outer_code == COMPARE)
34497 *total = 0;
34498 return true;
34500 break;
34502 default:
34503 break;
34506 return false;
34509 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
34511 static bool
34512 rs6000_debug_rtx_costs (rtx x, machine_mode mode, int outer_code,
34513 int opno, int *total, bool speed)
34515 bool ret = rs6000_rtx_costs (x, mode, outer_code, opno, total, speed);
34517 fprintf (stderr,
34518 "\nrs6000_rtx_costs, return = %s, mode = %s, outer_code = %s, "
34519 "opno = %d, total = %d, speed = %s, x:\n",
34520 ret ? "complete" : "scan inner",
34521 GET_MODE_NAME (mode),
34522 GET_RTX_NAME (outer_code),
34523 opno,
34524 *total,
34525 speed ? "true" : "false");
34527 debug_rtx (x);
34529 return ret;
34532 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
34534 static int
34535 rs6000_debug_address_cost (rtx x, machine_mode mode,
34536 addr_space_t as, bool speed)
34538 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
34540 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
34541 ret, speed ? "true" : "false");
34542 debug_rtx (x);
34544 return ret;
34548 /* A C expression returning the cost of moving data from a register of class
34549 CLASS1 to one of CLASS2. */
34551 static int
34552 rs6000_register_move_cost (machine_mode mode,
34553 reg_class_t from, reg_class_t to)
34555 int ret;
34557 if (TARGET_DEBUG_COST)
34558 dbg_cost_ctrl++;
34560 /* Moves from/to GENERAL_REGS. */
34561 if (reg_classes_intersect_p (to, GENERAL_REGS)
34562 || reg_classes_intersect_p (from, GENERAL_REGS))
34564 reg_class_t rclass = from;
34566 if (! reg_classes_intersect_p (to, GENERAL_REGS))
34567 rclass = to;
34569 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
34570 ret = (rs6000_memory_move_cost (mode, rclass, false)
34571 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
34573 /* It's more expensive to move CR_REGS than CR0_REGS because of the
34574 shift. */
34575 else if (rclass == CR_REGS)
34576 ret = 4;
34578 /* For those processors that have slow LR/CTR moves, make them more
34579 expensive than memory in order to bias spills to memory .*/
34580 else if ((rs6000_cpu == PROCESSOR_POWER6
34581 || rs6000_cpu == PROCESSOR_POWER7
34582 || rs6000_cpu == PROCESSOR_POWER8
34583 || rs6000_cpu == PROCESSOR_POWER9)
34584 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
34585 ret = 6 * hard_regno_nregs[0][mode];
34587 else
34588 /* A move will cost one instruction per GPR moved. */
34589 ret = 2 * hard_regno_nregs[0][mode];
34592 /* If we have VSX, we can easily move between FPR or Altivec registers. */
34593 else if (VECTOR_MEM_VSX_P (mode)
34594 && reg_classes_intersect_p (to, VSX_REGS)
34595 && reg_classes_intersect_p (from, VSX_REGS))
34596 ret = 2 * hard_regno_nregs[FIRST_FPR_REGNO][mode];
34598 /* Moving between two similar registers is just one instruction. */
34599 else if (reg_classes_intersect_p (to, from))
34600 ret = (FLOAT128_2REG_P (mode)) ? 4 : 2;
34602 /* Everything else has to go through GENERAL_REGS. */
34603 else
34604 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
34605 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
34607 if (TARGET_DEBUG_COST)
34609 if (dbg_cost_ctrl == 1)
34610 fprintf (stderr,
34611 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
34612 ret, GET_MODE_NAME (mode), reg_class_names[from],
34613 reg_class_names[to]);
34614 dbg_cost_ctrl--;
34617 return ret;
34620 /* A C expressions returning the cost of moving data of MODE from a register to
34621 or from memory. */
34623 static int
34624 rs6000_memory_move_cost (machine_mode mode, reg_class_t rclass,
34625 bool in ATTRIBUTE_UNUSED)
34627 int ret;
34629 if (TARGET_DEBUG_COST)
34630 dbg_cost_ctrl++;
34632 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
34633 ret = 4 * hard_regno_nregs[0][mode];
34634 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
34635 || reg_classes_intersect_p (rclass, VSX_REGS)))
34636 ret = 4 * hard_regno_nregs[32][mode];
34637 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
34638 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
34639 else
34640 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
34642 if (TARGET_DEBUG_COST)
34644 if (dbg_cost_ctrl == 1)
34645 fprintf (stderr,
34646 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
34647 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
34648 dbg_cost_ctrl--;
34651 return ret;
34654 /* Returns a code for a target-specific builtin that implements
34655 reciprocal of the function, or NULL_TREE if not available. */
34657 static tree
34658 rs6000_builtin_reciprocal (tree fndecl)
34660 switch (DECL_FUNCTION_CODE (fndecl))
34662 case VSX_BUILTIN_XVSQRTDP:
34663 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
34664 return NULL_TREE;
34666 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
34668 case VSX_BUILTIN_XVSQRTSP:
34669 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
34670 return NULL_TREE;
34672 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
34674 default:
34675 return NULL_TREE;
34679 /* Load up a constant. If the mode is a vector mode, splat the value across
34680 all of the vector elements. */
34682 static rtx
34683 rs6000_load_constant_and_splat (machine_mode mode, REAL_VALUE_TYPE dconst)
34685 rtx reg;
34687 if (mode == SFmode || mode == DFmode)
34689 rtx d = const_double_from_real_value (dconst, mode);
34690 reg = force_reg (mode, d);
34692 else if (mode == V4SFmode)
34694 rtx d = const_double_from_real_value (dconst, SFmode);
34695 rtvec v = gen_rtvec (4, d, d, d, d);
34696 reg = gen_reg_rtx (mode);
34697 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
34699 else if (mode == V2DFmode)
34701 rtx d = const_double_from_real_value (dconst, DFmode);
34702 rtvec v = gen_rtvec (2, d, d);
34703 reg = gen_reg_rtx (mode);
34704 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
34706 else
34707 gcc_unreachable ();
34709 return reg;
34712 /* Generate an FMA instruction. */
34714 static void
34715 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
34717 machine_mode mode = GET_MODE (target);
34718 rtx dst;
34720 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
34721 gcc_assert (dst != NULL);
34723 if (dst != target)
34724 emit_move_insn (target, dst);
34727 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
34729 static void
34730 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
34732 machine_mode mode = GET_MODE (dst);
34733 rtx r;
34735 /* This is a tad more complicated, since the fnma_optab is for
34736 a different expression: fma(-m1, m2, a), which is the same
34737 thing except in the case of signed zeros.
34739 Fortunately we know that if FMA is supported that FNMSUB is
34740 also supported in the ISA. Just expand it directly. */
34742 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
34744 r = gen_rtx_NEG (mode, a);
34745 r = gen_rtx_FMA (mode, m1, m2, r);
34746 r = gen_rtx_NEG (mode, r);
34747 emit_insn (gen_rtx_SET (dst, r));
34750 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
34751 add a reg_note saying that this was a division. Support both scalar and
34752 vector divide. Assumes no trapping math and finite arguments. */
34754 void
34755 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
34757 machine_mode mode = GET_MODE (dst);
34758 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
34759 int i;
34761 /* Low precision estimates guarantee 5 bits of accuracy. High
34762 precision estimates guarantee 14 bits of accuracy. SFmode
34763 requires 23 bits of accuracy. DFmode requires 52 bits of
34764 accuracy. Each pass at least doubles the accuracy, leading
34765 to the following. */
34766 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
34767 if (mode == DFmode || mode == V2DFmode)
34768 passes++;
34770 enum insn_code code = optab_handler (smul_optab, mode);
34771 insn_gen_fn gen_mul = GEN_FCN (code);
34773 gcc_assert (code != CODE_FOR_nothing);
34775 one = rs6000_load_constant_and_splat (mode, dconst1);
34777 /* x0 = 1./d estimate */
34778 x0 = gen_reg_rtx (mode);
34779 emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
34780 UNSPEC_FRES)));
34782 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
34783 if (passes > 1) {
34785 /* e0 = 1. - d * x0 */
34786 e0 = gen_reg_rtx (mode);
34787 rs6000_emit_nmsub (e0, d, x0, one);
34789 /* x1 = x0 + e0 * x0 */
34790 x1 = gen_reg_rtx (mode);
34791 rs6000_emit_madd (x1, e0, x0, x0);
34793 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
34794 ++i, xprev = xnext, eprev = enext) {
34796 /* enext = eprev * eprev */
34797 enext = gen_reg_rtx (mode);
34798 emit_insn (gen_mul (enext, eprev, eprev));
34800 /* xnext = xprev + enext * xprev */
34801 xnext = gen_reg_rtx (mode);
34802 rs6000_emit_madd (xnext, enext, xprev, xprev);
34805 } else
34806 xprev = x0;
34808 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
34810 /* u = n * xprev */
34811 u = gen_reg_rtx (mode);
34812 emit_insn (gen_mul (u, n, xprev));
34814 /* v = n - (d * u) */
34815 v = gen_reg_rtx (mode);
34816 rs6000_emit_nmsub (v, d, u, n);
34818 /* dst = (v * xprev) + u */
34819 rs6000_emit_madd (dst, v, xprev, u);
34821 if (note_p)
34822 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
34825 /* Goldschmidt's Algorithm for single/double-precision floating point
34826 sqrt and rsqrt. Assumes no trapping math and finite arguments. */
34828 void
34829 rs6000_emit_swsqrt (rtx dst, rtx src, bool recip)
34831 machine_mode mode = GET_MODE (src);
34832 rtx e = gen_reg_rtx (mode);
34833 rtx g = gen_reg_rtx (mode);
34834 rtx h = gen_reg_rtx (mode);
34836 /* Low precision estimates guarantee 5 bits of accuracy. High
34837 precision estimates guarantee 14 bits of accuracy. SFmode
34838 requires 23 bits of accuracy. DFmode requires 52 bits of
34839 accuracy. Each pass at least doubles the accuracy, leading
34840 to the following. */
34841 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
34842 if (mode == DFmode || mode == V2DFmode)
34843 passes++;
34845 int i;
34846 rtx mhalf;
34847 enum insn_code code = optab_handler (smul_optab, mode);
34848 insn_gen_fn gen_mul = GEN_FCN (code);
34850 gcc_assert (code != CODE_FOR_nothing);
34852 mhalf = rs6000_load_constant_and_splat (mode, dconsthalf);
34854 /* e = rsqrt estimate */
34855 emit_insn (gen_rtx_SET (e, gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
34856 UNSPEC_RSQRT)));
34858 /* If (src == 0.0) filter infinity to prevent NaN for sqrt(0.0). */
34859 if (!recip)
34861 rtx zero = force_reg (mode, CONST0_RTX (mode));
34863 if (mode == SFmode)
34865 rtx target = emit_conditional_move (e, GT, src, zero, mode,
34866 e, zero, mode, 0);
34867 if (target != e)
34868 emit_move_insn (e, target);
34870 else
34872 rtx cond = gen_rtx_GT (VOIDmode, e, zero);
34873 rs6000_emit_vector_cond_expr (e, e, zero, cond, src, zero);
34877 /* g = sqrt estimate. */
34878 emit_insn (gen_mul (g, e, src));
34879 /* h = 1/(2*sqrt) estimate. */
34880 emit_insn (gen_mul (h, e, mhalf));
34882 if (recip)
34884 if (passes == 1)
34886 rtx t = gen_reg_rtx (mode);
34887 rs6000_emit_nmsub (t, g, h, mhalf);
34888 /* Apply correction directly to 1/rsqrt estimate. */
34889 rs6000_emit_madd (dst, e, t, e);
34891 else
34893 for (i = 0; i < passes; i++)
34895 rtx t1 = gen_reg_rtx (mode);
34896 rtx g1 = gen_reg_rtx (mode);
34897 rtx h1 = gen_reg_rtx (mode);
34899 rs6000_emit_nmsub (t1, g, h, mhalf);
34900 rs6000_emit_madd (g1, g, t1, g);
34901 rs6000_emit_madd (h1, h, t1, h);
34903 g = g1;
34904 h = h1;
34906 /* Multiply by 2 for 1/rsqrt. */
34907 emit_insn (gen_add3_insn (dst, h, h));
34910 else
34912 rtx t = gen_reg_rtx (mode);
34913 rs6000_emit_nmsub (t, g, h, mhalf);
34914 rs6000_emit_madd (dst, g, t, g);
34917 return;
34920 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
34921 (Power7) targets. DST is the target, and SRC is the argument operand. */
34923 void
34924 rs6000_emit_popcount (rtx dst, rtx src)
34926 machine_mode mode = GET_MODE (dst);
34927 rtx tmp1, tmp2;
34929 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
34930 if (TARGET_POPCNTD)
34932 if (mode == SImode)
34933 emit_insn (gen_popcntdsi2 (dst, src));
34934 else
34935 emit_insn (gen_popcntddi2 (dst, src));
34936 return;
34939 tmp1 = gen_reg_rtx (mode);
34941 if (mode == SImode)
34943 emit_insn (gen_popcntbsi2 (tmp1, src));
34944 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
34945 NULL_RTX, 0);
34946 tmp2 = force_reg (SImode, tmp2);
34947 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
34949 else
34951 emit_insn (gen_popcntbdi2 (tmp1, src));
34952 tmp2 = expand_mult (DImode, tmp1,
34953 GEN_INT ((HOST_WIDE_INT)
34954 0x01010101 << 32 | 0x01010101),
34955 NULL_RTX, 0);
34956 tmp2 = force_reg (DImode, tmp2);
34957 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
34962 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
34963 target, and SRC is the argument operand. */
34965 void
34966 rs6000_emit_parity (rtx dst, rtx src)
34968 machine_mode mode = GET_MODE (dst);
34969 rtx tmp;
34971 tmp = gen_reg_rtx (mode);
34973 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
34974 if (TARGET_CMPB)
34976 if (mode == SImode)
34978 emit_insn (gen_popcntbsi2 (tmp, src));
34979 emit_insn (gen_paritysi2_cmpb (dst, tmp));
34981 else
34983 emit_insn (gen_popcntbdi2 (tmp, src));
34984 emit_insn (gen_paritydi2_cmpb (dst, tmp));
34986 return;
34989 if (mode == SImode)
34991 /* Is mult+shift >= shift+xor+shift+xor? */
34992 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
34994 rtx tmp1, tmp2, tmp3, tmp4;
34996 tmp1 = gen_reg_rtx (SImode);
34997 emit_insn (gen_popcntbsi2 (tmp1, src));
34999 tmp2 = gen_reg_rtx (SImode);
35000 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
35001 tmp3 = gen_reg_rtx (SImode);
35002 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
35004 tmp4 = gen_reg_rtx (SImode);
35005 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
35006 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
35008 else
35009 rs6000_emit_popcount (tmp, src);
35010 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
35012 else
35014 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
35015 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
35017 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
35019 tmp1 = gen_reg_rtx (DImode);
35020 emit_insn (gen_popcntbdi2 (tmp1, src));
35022 tmp2 = gen_reg_rtx (DImode);
35023 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
35024 tmp3 = gen_reg_rtx (DImode);
35025 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
35027 tmp4 = gen_reg_rtx (DImode);
35028 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
35029 tmp5 = gen_reg_rtx (DImode);
35030 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
35032 tmp6 = gen_reg_rtx (DImode);
35033 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
35034 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
35036 else
35037 rs6000_emit_popcount (tmp, src);
35038 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
35042 /* Expand an Altivec constant permutation for little endian mode.
35043 There are two issues: First, the two input operands must be
35044 swapped so that together they form a double-wide array in LE
35045 order. Second, the vperm instruction has surprising behavior
35046 in LE mode: it interprets the elements of the source vectors
35047 in BE mode ("left to right") and interprets the elements of
35048 the destination vector in LE mode ("right to left"). To
35049 correct for this, we must subtract each element of the permute
35050 control vector from 31.
35052 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
35053 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
35054 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
35055 serve as the permute control vector. Then, in BE mode,
35057 vperm 9,10,11,12
35059 places the desired result in vr9. However, in LE mode the
35060 vector contents will be
35062 vr10 = 00000003 00000002 00000001 00000000
35063 vr11 = 00000007 00000006 00000005 00000004
35065 The result of the vperm using the same permute control vector is
35067 vr9 = 05000000 07000000 01000000 03000000
35069 That is, the leftmost 4 bytes of vr10 are interpreted as the
35070 source for the rightmost 4 bytes of vr9, and so on.
35072 If we change the permute control vector to
35074 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
35076 and issue
35078 vperm 9,11,10,12
35080 we get the desired
35082 vr9 = 00000006 00000004 00000002 00000000. */
35084 void
35085 altivec_expand_vec_perm_const_le (rtx operands[4])
35087 unsigned int i;
35088 rtx perm[16];
35089 rtx constv, unspec;
35090 rtx target = operands[0];
35091 rtx op0 = operands[1];
35092 rtx op1 = operands[2];
35093 rtx sel = operands[3];
35095 /* Unpack and adjust the constant selector. */
35096 for (i = 0; i < 16; ++i)
35098 rtx e = XVECEXP (sel, 0, i);
35099 unsigned int elt = 31 - (INTVAL (e) & 31);
35100 perm[i] = GEN_INT (elt);
35103 /* Expand to a permute, swapping the inputs and using the
35104 adjusted selector. */
35105 if (!REG_P (op0))
35106 op0 = force_reg (V16QImode, op0);
35107 if (!REG_P (op1))
35108 op1 = force_reg (V16QImode, op1);
35110 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
35111 constv = force_reg (V16QImode, constv);
35112 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
35113 UNSPEC_VPERM);
35114 if (!REG_P (target))
35116 rtx tmp = gen_reg_rtx (V16QImode);
35117 emit_move_insn (tmp, unspec);
35118 unspec = tmp;
35121 emit_move_insn (target, unspec);
35124 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
35125 permute control vector. But here it's not a constant, so we must
35126 generate a vector NAND or NOR to do the adjustment. */
35128 void
35129 altivec_expand_vec_perm_le (rtx operands[4])
35131 rtx notx, iorx, unspec;
35132 rtx target = operands[0];
35133 rtx op0 = operands[1];
35134 rtx op1 = operands[2];
35135 rtx sel = operands[3];
35136 rtx tmp = target;
35137 rtx norreg = gen_reg_rtx (V16QImode);
35138 machine_mode mode = GET_MODE (target);
35140 /* Get everything in regs so the pattern matches. */
35141 if (!REG_P (op0))
35142 op0 = force_reg (mode, op0);
35143 if (!REG_P (op1))
35144 op1 = force_reg (mode, op1);
35145 if (!REG_P (sel))
35146 sel = force_reg (V16QImode, sel);
35147 if (!REG_P (target))
35148 tmp = gen_reg_rtx (mode);
35150 if (TARGET_P9_VECTOR)
35152 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op0, op1, sel),
35153 UNSPEC_VPERMR);
35155 else
35157 /* Invert the selector with a VNAND if available, else a VNOR.
35158 The VNAND is preferred for future fusion opportunities. */
35159 notx = gen_rtx_NOT (V16QImode, sel);
35160 iorx = (TARGET_P8_VECTOR
35161 ? gen_rtx_IOR (V16QImode, notx, notx)
35162 : gen_rtx_AND (V16QImode, notx, notx));
35163 emit_insn (gen_rtx_SET (norreg, iorx));
35165 /* Permute with operands reversed and adjusted selector. */
35166 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
35167 UNSPEC_VPERM);
35170 /* Copy into target, possibly by way of a register. */
35171 if (!REG_P (target))
35173 emit_move_insn (tmp, unspec);
35174 unspec = tmp;
35177 emit_move_insn (target, unspec);
35180 /* Expand an Altivec constant permutation. Return true if we match
35181 an efficient implementation; false to fall back to VPERM. */
35183 bool
35184 altivec_expand_vec_perm_const (rtx operands[4])
35186 struct altivec_perm_insn {
35187 HOST_WIDE_INT mask;
35188 enum insn_code impl;
35189 unsigned char perm[16];
35191 static const struct altivec_perm_insn patterns[] = {
35192 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
35193 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
35194 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
35195 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
35196 { OPTION_MASK_ALTIVEC,
35197 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
35198 : CODE_FOR_altivec_vmrglb_direct),
35199 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
35200 { OPTION_MASK_ALTIVEC,
35201 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
35202 : CODE_FOR_altivec_vmrglh_direct),
35203 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
35204 { OPTION_MASK_ALTIVEC,
35205 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
35206 : CODE_FOR_altivec_vmrglw_direct),
35207 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
35208 { OPTION_MASK_ALTIVEC,
35209 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
35210 : CODE_FOR_altivec_vmrghb_direct),
35211 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
35212 { OPTION_MASK_ALTIVEC,
35213 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
35214 : CODE_FOR_altivec_vmrghh_direct),
35215 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
35216 { OPTION_MASK_ALTIVEC,
35217 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
35218 : CODE_FOR_altivec_vmrghw_direct),
35219 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
35220 { OPTION_MASK_P8_VECTOR,
35221 (BYTES_BIG_ENDIAN ? CODE_FOR_p8_vmrgew_v4sf_direct
35222 : CODE_FOR_p8_vmrgow_v4sf_direct),
35223 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
35224 { OPTION_MASK_P8_VECTOR,
35225 (BYTES_BIG_ENDIAN ? CODE_FOR_p8_vmrgow_v4sf_direct
35226 : CODE_FOR_p8_vmrgew_v4sf_direct),
35227 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
35230 unsigned int i, j, elt, which;
35231 unsigned char perm[16];
35232 rtx target, op0, op1, sel, x;
35233 bool one_vec;
35235 target = operands[0];
35236 op0 = operands[1];
35237 op1 = operands[2];
35238 sel = operands[3];
35240 /* Unpack the constant selector. */
35241 for (i = which = 0; i < 16; ++i)
35243 rtx e = XVECEXP (sel, 0, i);
35244 elt = INTVAL (e) & 31;
35245 which |= (elt < 16 ? 1 : 2);
35246 perm[i] = elt;
35249 /* Simplify the constant selector based on operands. */
35250 switch (which)
35252 default:
35253 gcc_unreachable ();
35255 case 3:
35256 one_vec = false;
35257 if (!rtx_equal_p (op0, op1))
35258 break;
35259 /* FALLTHRU */
35261 case 2:
35262 for (i = 0; i < 16; ++i)
35263 perm[i] &= 15;
35264 op0 = op1;
35265 one_vec = true;
35266 break;
35268 case 1:
35269 op1 = op0;
35270 one_vec = true;
35271 break;
35274 /* Look for splat patterns. */
35275 if (one_vec)
35277 elt = perm[0];
35279 for (i = 0; i < 16; ++i)
35280 if (perm[i] != elt)
35281 break;
35282 if (i == 16)
35284 if (!BYTES_BIG_ENDIAN)
35285 elt = 15 - elt;
35286 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
35287 return true;
35290 if (elt % 2 == 0)
35292 for (i = 0; i < 16; i += 2)
35293 if (perm[i] != elt || perm[i + 1] != elt + 1)
35294 break;
35295 if (i == 16)
35297 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
35298 x = gen_reg_rtx (V8HImode);
35299 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
35300 GEN_INT (field)));
35301 emit_move_insn (target, gen_lowpart (V16QImode, x));
35302 return true;
35306 if (elt % 4 == 0)
35308 for (i = 0; i < 16; i += 4)
35309 if (perm[i] != elt
35310 || perm[i + 1] != elt + 1
35311 || perm[i + 2] != elt + 2
35312 || perm[i + 3] != elt + 3)
35313 break;
35314 if (i == 16)
35316 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
35317 x = gen_reg_rtx (V4SImode);
35318 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
35319 GEN_INT (field)));
35320 emit_move_insn (target, gen_lowpart (V16QImode, x));
35321 return true;
35326 /* Look for merge and pack patterns. */
35327 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
35329 bool swapped;
35331 if ((patterns[j].mask & rs6000_isa_flags) == 0)
35332 continue;
35334 elt = patterns[j].perm[0];
35335 if (perm[0] == elt)
35336 swapped = false;
35337 else if (perm[0] == elt + 16)
35338 swapped = true;
35339 else
35340 continue;
35341 for (i = 1; i < 16; ++i)
35343 elt = patterns[j].perm[i];
35344 if (swapped)
35345 elt = (elt >= 16 ? elt - 16 : elt + 16);
35346 else if (one_vec && elt >= 16)
35347 elt -= 16;
35348 if (perm[i] != elt)
35349 break;
35351 if (i == 16)
35353 enum insn_code icode = patterns[j].impl;
35354 machine_mode omode = insn_data[icode].operand[0].mode;
35355 machine_mode imode = insn_data[icode].operand[1].mode;
35357 /* For little-endian, don't use vpkuwum and vpkuhum if the
35358 underlying vector type is not V4SI and V8HI, respectively.
35359 For example, using vpkuwum with a V8HI picks up the even
35360 halfwords (BE numbering) when the even halfwords (LE
35361 numbering) are what we need. */
35362 if (!BYTES_BIG_ENDIAN
35363 && icode == CODE_FOR_altivec_vpkuwum_direct
35364 && ((GET_CODE (op0) == REG
35365 && GET_MODE (op0) != V4SImode)
35366 || (GET_CODE (op0) == SUBREG
35367 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
35368 continue;
35369 if (!BYTES_BIG_ENDIAN
35370 && icode == CODE_FOR_altivec_vpkuhum_direct
35371 && ((GET_CODE (op0) == REG
35372 && GET_MODE (op0) != V8HImode)
35373 || (GET_CODE (op0) == SUBREG
35374 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
35375 continue;
35377 /* For little-endian, the two input operands must be swapped
35378 (or swapped back) to ensure proper right-to-left numbering
35379 from 0 to 2N-1. */
35380 if (swapped ^ !BYTES_BIG_ENDIAN)
35381 std::swap (op0, op1);
35382 if (imode != V16QImode)
35384 op0 = gen_lowpart (imode, op0);
35385 op1 = gen_lowpart (imode, op1);
35387 if (omode == V16QImode)
35388 x = target;
35389 else
35390 x = gen_reg_rtx (omode);
35391 emit_insn (GEN_FCN (icode) (x, op0, op1));
35392 if (omode != V16QImode)
35393 emit_move_insn (target, gen_lowpart (V16QImode, x));
35394 return true;
35398 if (!BYTES_BIG_ENDIAN)
35400 altivec_expand_vec_perm_const_le (operands);
35401 return true;
35404 return false;
35407 /* Expand a Paired Single or VSX Permute Doubleword constant permutation.
35408 Return true if we match an efficient implementation. */
35410 static bool
35411 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
35412 unsigned char perm0, unsigned char perm1)
35414 rtx x;
35416 /* If both selectors come from the same operand, fold to single op. */
35417 if ((perm0 & 2) == (perm1 & 2))
35419 if (perm0 & 2)
35420 op0 = op1;
35421 else
35422 op1 = op0;
35424 /* If both operands are equal, fold to simpler permutation. */
35425 if (rtx_equal_p (op0, op1))
35427 perm0 = perm0 & 1;
35428 perm1 = (perm1 & 1) + 2;
35430 /* If the first selector comes from the second operand, swap. */
35431 else if (perm0 & 2)
35433 if (perm1 & 2)
35434 return false;
35435 perm0 -= 2;
35436 perm1 += 2;
35437 std::swap (op0, op1);
35439 /* If the second selector does not come from the second operand, fail. */
35440 else if ((perm1 & 2) == 0)
35441 return false;
35443 /* Success! */
35444 if (target != NULL)
35446 machine_mode vmode, dmode;
35447 rtvec v;
35449 vmode = GET_MODE (target);
35450 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
35451 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
35452 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
35453 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
35454 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
35455 emit_insn (gen_rtx_SET (target, x));
35457 return true;
35460 bool
35461 rs6000_expand_vec_perm_const (rtx operands[4])
35463 rtx target, op0, op1, sel;
35464 unsigned char perm0, perm1;
35466 target = operands[0];
35467 op0 = operands[1];
35468 op1 = operands[2];
35469 sel = operands[3];
35471 /* Unpack the constant selector. */
35472 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
35473 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
35475 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
35478 /* Test whether a constant permutation is supported. */
35480 static bool
35481 rs6000_vectorize_vec_perm_const_ok (machine_mode vmode,
35482 const unsigned char *sel)
35484 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
35485 if (TARGET_ALTIVEC)
35486 return true;
35488 /* Check for ps_merge* or evmerge* insns. */
35489 if (TARGET_PAIRED_FLOAT && vmode == V2SFmode)
35491 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
35492 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
35493 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
35496 return false;
35499 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
35501 static void
35502 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
35503 machine_mode vmode, unsigned nelt, rtx perm[])
35505 machine_mode imode;
35506 rtx x;
35508 imode = vmode;
35509 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
35511 imode = mode_for_size (GET_MODE_UNIT_BITSIZE (vmode), MODE_INT, 0);
35512 imode = mode_for_vector (imode, nelt);
35515 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
35516 x = expand_vec_perm (vmode, op0, op1, x, target);
35517 if (x != target)
35518 emit_move_insn (target, x);
35521 /* Expand an extract even operation. */
35523 void
35524 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
35526 machine_mode vmode = GET_MODE (target);
35527 unsigned i, nelt = GET_MODE_NUNITS (vmode);
35528 rtx perm[16];
35530 for (i = 0; i < nelt; i++)
35531 perm[i] = GEN_INT (i * 2);
35533 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
35536 /* Expand a vector interleave operation. */
35538 void
35539 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
35541 machine_mode vmode = GET_MODE (target);
35542 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
35543 rtx perm[16];
35545 high = (highp ? 0 : nelt / 2);
35546 for (i = 0; i < nelt / 2; i++)
35548 perm[i * 2] = GEN_INT (i + high);
35549 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
35552 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
35555 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT. */
35556 void
35557 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
35559 HOST_WIDE_INT hwi_scale (scale);
35560 REAL_VALUE_TYPE r_pow;
35561 rtvec v = rtvec_alloc (2);
35562 rtx elt;
35563 rtx scale_vec = gen_reg_rtx (V2DFmode);
35564 (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
35565 elt = const_double_from_real_value (r_pow, DFmode);
35566 RTVEC_ELT (v, 0) = elt;
35567 RTVEC_ELT (v, 1) = elt;
35568 rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
35569 emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
35572 /* Return an RTX representing where to find the function value of a
35573 function returning MODE. */
35574 static rtx
35575 rs6000_complex_function_value (machine_mode mode)
35577 unsigned int regno;
35578 rtx r1, r2;
35579 machine_mode inner = GET_MODE_INNER (mode);
35580 unsigned int inner_bytes = GET_MODE_UNIT_SIZE (mode);
35582 if (TARGET_FLOAT128_TYPE
35583 && (mode == KCmode
35584 || (mode == TCmode && TARGET_IEEEQUAD)))
35585 regno = ALTIVEC_ARG_RETURN;
35587 else if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
35588 regno = FP_ARG_RETURN;
35590 else
35592 regno = GP_ARG_RETURN;
35594 /* 32-bit is OK since it'll go in r3/r4. */
35595 if (TARGET_32BIT && inner_bytes >= 4)
35596 return gen_rtx_REG (mode, regno);
35599 if (inner_bytes >= 8)
35600 return gen_rtx_REG (mode, regno);
35602 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
35603 const0_rtx);
35604 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
35605 GEN_INT (inner_bytes));
35606 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
35609 /* Return an rtx describing a return value of MODE as a PARALLEL
35610 in N_ELTS registers, each of mode ELT_MODE, starting at REGNO,
35611 stride REG_STRIDE. */
35613 static rtx
35614 rs6000_parallel_return (machine_mode mode,
35615 int n_elts, machine_mode elt_mode,
35616 unsigned int regno, unsigned int reg_stride)
35618 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
35620 int i;
35621 for (i = 0; i < n_elts; i++)
35623 rtx r = gen_rtx_REG (elt_mode, regno);
35624 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
35625 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
35626 regno += reg_stride;
35629 return par;
35632 /* Target hook for TARGET_FUNCTION_VALUE.
35634 An integer value is in r3 and a floating-point value is in fp1,
35635 unless -msoft-float. */
35637 static rtx
35638 rs6000_function_value (const_tree valtype,
35639 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
35640 bool outgoing ATTRIBUTE_UNUSED)
35642 machine_mode mode;
35643 unsigned int regno;
35644 machine_mode elt_mode;
35645 int n_elts;
35647 /* Special handling for structs in darwin64. */
35648 if (TARGET_MACHO
35649 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
35651 CUMULATIVE_ARGS valcum;
35652 rtx valret;
35654 valcum.words = 0;
35655 valcum.fregno = FP_ARG_MIN_REG;
35656 valcum.vregno = ALTIVEC_ARG_MIN_REG;
35657 /* Do a trial code generation as if this were going to be passed as
35658 an argument; if any part goes in memory, we return NULL. */
35659 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
35660 if (valret)
35661 return valret;
35662 /* Otherwise fall through to standard ABI rules. */
35665 mode = TYPE_MODE (valtype);
35667 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
35668 if (rs6000_discover_homogeneous_aggregate (mode, valtype, &elt_mode, &n_elts))
35670 int first_reg, n_regs;
35672 if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (elt_mode))
35674 /* _Decimal128 must use even/odd register pairs. */
35675 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
35676 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
35678 else
35680 first_reg = ALTIVEC_ARG_RETURN;
35681 n_regs = 1;
35684 return rs6000_parallel_return (mode, n_elts, elt_mode, first_reg, n_regs);
35687 /* Some return value types need be split in -mpowerpc64, 32bit ABI. */
35688 if (TARGET_32BIT && TARGET_POWERPC64)
35689 switch (mode)
35691 default:
35692 break;
35693 case E_DImode:
35694 case E_SCmode:
35695 case E_DCmode:
35696 case E_TCmode:
35697 int count = GET_MODE_SIZE (mode) / 4;
35698 return rs6000_parallel_return (mode, count, SImode, GP_ARG_RETURN, 1);
35701 if ((INTEGRAL_TYPE_P (valtype)
35702 && GET_MODE_BITSIZE (mode) < (TARGET_32BIT ? 32 : 64))
35703 || POINTER_TYPE_P (valtype))
35704 mode = TARGET_32BIT ? SImode : DImode;
35706 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
35707 /* _Decimal128 must use an even/odd register pair. */
35708 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
35709 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT
35710 && !FLOAT128_VECTOR_P (mode)
35711 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
35712 regno = FP_ARG_RETURN;
35713 else if (TREE_CODE (valtype) == COMPLEX_TYPE
35714 && targetm.calls.split_complex_arg)
35715 return rs6000_complex_function_value (mode);
35716 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
35717 return register is used in both cases, and we won't see V2DImode/V2DFmode
35718 for pure altivec, combine the two cases. */
35719 else if ((TREE_CODE (valtype) == VECTOR_TYPE || FLOAT128_VECTOR_P (mode))
35720 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
35721 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
35722 regno = ALTIVEC_ARG_RETURN;
35723 else
35724 regno = GP_ARG_RETURN;
35726 return gen_rtx_REG (mode, regno);
35729 /* Define how to find the value returned by a library function
35730 assuming the value has mode MODE. */
35732 rs6000_libcall_value (machine_mode mode)
35734 unsigned int regno;
35736 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
35737 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
35738 return rs6000_parallel_return (mode, 2, SImode, GP_ARG_RETURN, 1);
35740 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
35741 /* _Decimal128 must use an even/odd register pair. */
35742 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
35743 else if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode)
35744 && TARGET_HARD_FLOAT
35745 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
35746 regno = FP_ARG_RETURN;
35747 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
35748 return register is used in both cases, and we won't see V2DImode/V2DFmode
35749 for pure altivec, combine the two cases. */
35750 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
35751 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
35752 regno = ALTIVEC_ARG_RETURN;
35753 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
35754 return rs6000_complex_function_value (mode);
35755 else
35756 regno = GP_ARG_RETURN;
35758 return gen_rtx_REG (mode, regno);
35761 /* Compute register pressure classes. We implement the target hook to avoid
35762 IRA picking something like NON_SPECIAL_REGS as a pressure class, which can
35763 lead to incorrect estimates of number of available registers and therefor
35764 increased register pressure/spill. */
35765 static int
35766 rs6000_compute_pressure_classes (enum reg_class *pressure_classes)
35768 int n;
35770 n = 0;
35771 pressure_classes[n++] = GENERAL_REGS;
35772 if (TARGET_VSX)
35773 pressure_classes[n++] = VSX_REGS;
35774 else
35776 if (TARGET_ALTIVEC)
35777 pressure_classes[n++] = ALTIVEC_REGS;
35778 if (TARGET_HARD_FLOAT)
35779 pressure_classes[n++] = FLOAT_REGS;
35781 pressure_classes[n++] = CR_REGS;
35782 pressure_classes[n++] = SPECIAL_REGS;
35784 return n;
35787 /* Given FROM and TO register numbers, say whether this elimination is allowed.
35788 Frame pointer elimination is automatically handled.
35790 For the RS/6000, if frame pointer elimination is being done, we would like
35791 to convert ap into fp, not sp.
35793 We need r30 if -mminimal-toc was specified, and there are constant pool
35794 references. */
35796 static bool
35797 rs6000_can_eliminate (const int from, const int to)
35799 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
35800 ? ! frame_pointer_needed
35801 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
35802 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC
35803 || constant_pool_empty_p ()
35804 : true);
35807 /* Define the offset between two registers, FROM to be eliminated and its
35808 replacement TO, at the start of a routine. */
35809 HOST_WIDE_INT
35810 rs6000_initial_elimination_offset (int from, int to)
35812 rs6000_stack_t *info = rs6000_stack_info ();
35813 HOST_WIDE_INT offset;
35815 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
35816 offset = info->push_p ? 0 : -info->total_size;
35817 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
35819 offset = info->push_p ? 0 : -info->total_size;
35820 if (FRAME_GROWS_DOWNWARD)
35821 offset += info->fixed_size + info->vars_size + info->parm_size;
35823 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
35824 offset = FRAME_GROWS_DOWNWARD
35825 ? info->fixed_size + info->vars_size + info->parm_size
35826 : 0;
35827 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
35828 offset = info->total_size;
35829 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
35830 offset = info->push_p ? info->total_size : 0;
35831 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
35832 offset = 0;
35833 else
35834 gcc_unreachable ();
35836 return offset;
35839 /* Fill in sizes of registers used by unwinder. */
35841 static void
35842 rs6000_init_dwarf_reg_sizes_extra (tree address)
35844 if (TARGET_MACHO && ! TARGET_ALTIVEC)
35846 int i;
35847 machine_mode mode = TYPE_MODE (char_type_node);
35848 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
35849 rtx mem = gen_rtx_MEM (BLKmode, addr);
35850 rtx value = gen_int_mode (16, mode);
35852 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
35853 The unwinder still needs to know the size of Altivec registers. */
35855 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
35857 int column = DWARF_REG_TO_UNWIND_COLUMN
35858 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
35859 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
35861 emit_move_insn (adjust_address (mem, mode, offset), value);
35866 /* Map internal gcc register numbers to debug format register numbers.
35867 FORMAT specifies the type of debug register number to use:
35868 0 -- debug information, except for frame-related sections
35869 1 -- DWARF .debug_frame section
35870 2 -- DWARF .eh_frame section */
35872 unsigned int
35873 rs6000_dbx_register_number (unsigned int regno, unsigned int format)
35875 /* Except for the above, we use the internal number for non-DWARF
35876 debug information, and also for .eh_frame. */
35877 if ((format == 0 && write_symbols != DWARF2_DEBUG) || format == 2)
35878 return regno;
35880 /* On some platforms, we use the standard DWARF register
35881 numbering for .debug_info and .debug_frame. */
35882 #ifdef RS6000_USE_DWARF_NUMBERING
35883 if (regno <= 63)
35884 return regno;
35885 if (regno == LR_REGNO)
35886 return 108;
35887 if (regno == CTR_REGNO)
35888 return 109;
35889 /* Special handling for CR for .debug_frame: rs6000_emit_prologue has
35890 translated any combination of CR2, CR3, CR4 saves to a save of CR2.
35891 The actual code emitted saves the whole of CR, so we map CR2_REGNO
35892 to the DWARF reg for CR. */
35893 if (format == 1 && regno == CR2_REGNO)
35894 return 64;
35895 if (CR_REGNO_P (regno))
35896 return regno - CR0_REGNO + 86;
35897 if (regno == CA_REGNO)
35898 return 101; /* XER */
35899 if (ALTIVEC_REGNO_P (regno))
35900 return regno - FIRST_ALTIVEC_REGNO + 1124;
35901 if (regno == VRSAVE_REGNO)
35902 return 356;
35903 if (regno == VSCR_REGNO)
35904 return 67;
35905 #endif
35906 return regno;
35909 /* target hook eh_return_filter_mode */
35910 static machine_mode
35911 rs6000_eh_return_filter_mode (void)
35913 return TARGET_32BIT ? SImode : word_mode;
35916 /* Target hook for scalar_mode_supported_p. */
35917 static bool
35918 rs6000_scalar_mode_supported_p (machine_mode mode)
35920 /* -m32 does not support TImode. This is the default, from
35921 default_scalar_mode_supported_p. For -m32 -mpowerpc64 we want the
35922 same ABI as for -m32. But default_scalar_mode_supported_p allows
35923 integer modes of precision 2 * BITS_PER_WORD, which matches TImode
35924 for -mpowerpc64. */
35925 if (TARGET_32BIT && mode == TImode)
35926 return false;
35928 if (DECIMAL_FLOAT_MODE_P (mode))
35929 return default_decimal_float_supported_p ();
35930 else if (TARGET_FLOAT128_TYPE && (mode == KFmode || mode == IFmode))
35931 return true;
35932 else
35933 return default_scalar_mode_supported_p (mode);
35936 /* Target hook for vector_mode_supported_p. */
35937 static bool
35938 rs6000_vector_mode_supported_p (machine_mode mode)
35941 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
35942 return true;
35944 /* There is no vector form for IEEE 128-bit. If we return true for IEEE
35945 128-bit, the compiler might try to widen IEEE 128-bit to IBM
35946 double-double. */
35947 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode) && !FLOAT128_IEEE_P (mode))
35948 return true;
35950 else
35951 return false;
35954 /* Target hook for floatn_mode. */
35955 static opt_scalar_float_mode
35956 rs6000_floatn_mode (int n, bool extended)
35958 if (extended)
35960 switch (n)
35962 case 32:
35963 return DFmode;
35965 case 64:
35966 if (TARGET_FLOAT128_KEYWORD)
35967 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
35968 else
35969 return opt_scalar_float_mode ();
35971 case 128:
35972 return opt_scalar_float_mode ();
35974 default:
35975 /* Those are the only valid _FloatNx types. */
35976 gcc_unreachable ();
35979 else
35981 switch (n)
35983 case 32:
35984 return SFmode;
35986 case 64:
35987 return DFmode;
35989 case 128:
35990 if (TARGET_FLOAT128_KEYWORD)
35991 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
35992 else
35993 return opt_scalar_float_mode ();
35995 default:
35996 return opt_scalar_float_mode ();
36002 /* Target hook for c_mode_for_suffix. */
36003 static machine_mode
36004 rs6000_c_mode_for_suffix (char suffix)
36006 if (TARGET_FLOAT128_TYPE)
36008 if (suffix == 'q' || suffix == 'Q')
36009 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
36011 /* At the moment, we are not defining a suffix for IBM extended double.
36012 If/when the default for -mabi=ieeelongdouble is changed, and we want
36013 to support __ibm128 constants in legacy library code, we may need to
36014 re-evalaute this decision. Currently, c-lex.c only supports 'w' and
36015 'q' as machine dependent suffixes. The x86_64 port uses 'w' for
36016 __float80 constants. */
36019 return VOIDmode;
36022 /* Target hook for invalid_arg_for_unprototyped_fn. */
36023 static const char *
36024 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
36026 return (!rs6000_darwin64_abi
36027 && typelist == 0
36028 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
36029 && (funcdecl == NULL_TREE
36030 || (TREE_CODE (funcdecl) == FUNCTION_DECL
36031 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
36032 ? N_("AltiVec argument passed to unprototyped function")
36033 : NULL;
36036 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
36037 setup by using __stack_chk_fail_local hidden function instead of
36038 calling __stack_chk_fail directly. Otherwise it is better to call
36039 __stack_chk_fail directly. */
36041 static tree ATTRIBUTE_UNUSED
36042 rs6000_stack_protect_fail (void)
36044 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
36045 ? default_hidden_stack_protect_fail ()
36046 : default_external_stack_protect_fail ();
36049 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
36051 #if TARGET_ELF
36052 static unsigned HOST_WIDE_INT
36053 rs6000_asan_shadow_offset (void)
36055 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
36057 #endif
36059 /* Mask options that we want to support inside of attribute((target)) and
36060 #pragma GCC target operations. Note, we do not include things like
36061 64/32-bit, endianness, hard/soft floating point, etc. that would have
36062 different calling sequences. */
36064 struct rs6000_opt_mask {
36065 const char *name; /* option name */
36066 HOST_WIDE_INT mask; /* mask to set */
36067 bool invert; /* invert sense of mask */
36068 bool valid_target; /* option is a target option */
36071 static struct rs6000_opt_mask const rs6000_opt_masks[] =
36073 { "altivec", OPTION_MASK_ALTIVEC, false, true },
36074 { "cmpb", OPTION_MASK_CMPB, false, true },
36075 { "crypto", OPTION_MASK_CRYPTO, false, true },
36076 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
36077 { "dlmzb", OPTION_MASK_DLMZB, false, true },
36078 { "efficient-unaligned-vsx", OPTION_MASK_EFFICIENT_UNALIGNED_VSX,
36079 false, true },
36080 { "float128", OPTION_MASK_FLOAT128_KEYWORD, false, false },
36081 { "float128-type", OPTION_MASK_FLOAT128_TYPE, false, false },
36082 { "float128-hardware", OPTION_MASK_FLOAT128_HW, false, false },
36083 { "fprnd", OPTION_MASK_FPRND, false, true },
36084 { "hard-dfp", OPTION_MASK_DFP, false, true },
36085 { "htm", OPTION_MASK_HTM, false, true },
36086 { "isel", OPTION_MASK_ISEL, false, true },
36087 { "mfcrf", OPTION_MASK_MFCRF, false, true },
36088 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
36089 { "modulo", OPTION_MASK_MODULO, false, true },
36090 { "mulhw", OPTION_MASK_MULHW, false, true },
36091 { "multiple", OPTION_MASK_MULTIPLE, false, true },
36092 { "popcntb", OPTION_MASK_POPCNTB, false, true },
36093 { "popcntd", OPTION_MASK_POPCNTD, false, true },
36094 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
36095 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
36096 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
36097 { "power9-fusion", OPTION_MASK_P9_FUSION, false, true },
36098 { "power9-minmax", OPTION_MASK_P9_MINMAX, false, true },
36099 { "power9-misc", OPTION_MASK_P9_MISC, false, true },
36100 { "power9-vector", OPTION_MASK_P9_VECTOR, false, true },
36101 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
36102 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
36103 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
36104 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
36105 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
36106 { "save-toc-indirect", OPTION_MASK_SAVE_TOC_INDIRECT, false, true },
36107 { "string", OPTION_MASK_STRING, false, true },
36108 { "toc-fusion", OPTION_MASK_TOC_FUSION, false, true },
36109 { "update", OPTION_MASK_NO_UPDATE, true , true },
36110 { "vsx", OPTION_MASK_VSX, false, true },
36111 #ifdef OPTION_MASK_64BIT
36112 #if TARGET_AIX_OS
36113 { "aix64", OPTION_MASK_64BIT, false, false },
36114 { "aix32", OPTION_MASK_64BIT, true, false },
36115 #else
36116 { "64", OPTION_MASK_64BIT, false, false },
36117 { "32", OPTION_MASK_64BIT, true, false },
36118 #endif
36119 #endif
36120 #ifdef OPTION_MASK_EABI
36121 { "eabi", OPTION_MASK_EABI, false, false },
36122 #endif
36123 #ifdef OPTION_MASK_LITTLE_ENDIAN
36124 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
36125 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
36126 #endif
36127 #ifdef OPTION_MASK_RELOCATABLE
36128 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
36129 #endif
36130 #ifdef OPTION_MASK_STRICT_ALIGN
36131 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
36132 #endif
36133 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
36134 { "string", OPTION_MASK_STRING, false, false },
36137 /* Builtin mask mapping for printing the flags. */
36138 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
36140 { "altivec", RS6000_BTM_ALTIVEC, false, false },
36141 { "vsx", RS6000_BTM_VSX, false, false },
36142 { "paired", RS6000_BTM_PAIRED, false, false },
36143 { "fre", RS6000_BTM_FRE, false, false },
36144 { "fres", RS6000_BTM_FRES, false, false },
36145 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
36146 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
36147 { "popcntd", RS6000_BTM_POPCNTD, false, false },
36148 { "cell", RS6000_BTM_CELL, false, false },
36149 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
36150 { "power9-vector", RS6000_BTM_P9_VECTOR, false, false },
36151 { "power9-misc", RS6000_BTM_P9_MISC, false, false },
36152 { "crypto", RS6000_BTM_CRYPTO, false, false },
36153 { "htm", RS6000_BTM_HTM, false, false },
36154 { "hard-dfp", RS6000_BTM_DFP, false, false },
36155 { "hard-float", RS6000_BTM_HARD_FLOAT, false, false },
36156 { "long-double-128", RS6000_BTM_LDBL128, false, false },
36157 { "float128", RS6000_BTM_FLOAT128, false, false },
36160 /* Option variables that we want to support inside attribute((target)) and
36161 #pragma GCC target operations. */
36163 struct rs6000_opt_var {
36164 const char *name; /* option name */
36165 size_t global_offset; /* offset of the option in global_options. */
36166 size_t target_offset; /* offset of the option in target options. */
36169 static struct rs6000_opt_var const rs6000_opt_vars[] =
36171 { "friz",
36172 offsetof (struct gcc_options, x_TARGET_FRIZ),
36173 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
36174 { "avoid-indexed-addresses",
36175 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
36176 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
36177 { "paired",
36178 offsetof (struct gcc_options, x_rs6000_paired_float),
36179 offsetof (struct cl_target_option, x_rs6000_paired_float), },
36180 { "longcall",
36181 offsetof (struct gcc_options, x_rs6000_default_long_calls),
36182 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
36183 { "optimize-swaps",
36184 offsetof (struct gcc_options, x_rs6000_optimize_swaps),
36185 offsetof (struct cl_target_option, x_rs6000_optimize_swaps), },
36186 { "allow-movmisalign",
36187 offsetof (struct gcc_options, x_TARGET_ALLOW_MOVMISALIGN),
36188 offsetof (struct cl_target_option, x_TARGET_ALLOW_MOVMISALIGN), },
36189 { "sched-groups",
36190 offsetof (struct gcc_options, x_TARGET_SCHED_GROUPS),
36191 offsetof (struct cl_target_option, x_TARGET_SCHED_GROUPS), },
36192 { "always-hint",
36193 offsetof (struct gcc_options, x_TARGET_ALWAYS_HINT),
36194 offsetof (struct cl_target_option, x_TARGET_ALWAYS_HINT), },
36195 { "align-branch-targets",
36196 offsetof (struct gcc_options, x_TARGET_ALIGN_BRANCH_TARGETS),
36197 offsetof (struct cl_target_option, x_TARGET_ALIGN_BRANCH_TARGETS), },
36198 { "tls-markers",
36199 offsetof (struct gcc_options, x_tls_markers),
36200 offsetof (struct cl_target_option, x_tls_markers), },
36201 { "sched-prolog",
36202 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
36203 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
36204 { "sched-epilog",
36205 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
36206 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
36209 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
36210 parsing. Return true if there were no errors. */
36212 static bool
36213 rs6000_inner_target_options (tree args, bool attr_p)
36215 bool ret = true;
36217 if (args == NULL_TREE)
36220 else if (TREE_CODE (args) == STRING_CST)
36222 char *p = ASTRDUP (TREE_STRING_POINTER (args));
36223 char *q;
36225 while ((q = strtok (p, ",")) != NULL)
36227 bool error_p = false;
36228 bool not_valid_p = false;
36229 const char *cpu_opt = NULL;
36231 p = NULL;
36232 if (strncmp (q, "cpu=", 4) == 0)
36234 int cpu_index = rs6000_cpu_name_lookup (q+4);
36235 if (cpu_index >= 0)
36236 rs6000_cpu_index = cpu_index;
36237 else
36239 error_p = true;
36240 cpu_opt = q+4;
36243 else if (strncmp (q, "tune=", 5) == 0)
36245 int tune_index = rs6000_cpu_name_lookup (q+5);
36246 if (tune_index >= 0)
36247 rs6000_tune_index = tune_index;
36248 else
36250 error_p = true;
36251 cpu_opt = q+5;
36254 else
36256 size_t i;
36257 bool invert = false;
36258 char *r = q;
36260 error_p = true;
36261 if (strncmp (r, "no-", 3) == 0)
36263 invert = true;
36264 r += 3;
36267 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
36268 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
36270 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
36272 if (!rs6000_opt_masks[i].valid_target)
36273 not_valid_p = true;
36274 else
36276 error_p = false;
36277 rs6000_isa_flags_explicit |= mask;
36279 /* VSX needs altivec, so -mvsx automagically sets
36280 altivec and disables -mavoid-indexed-addresses. */
36281 if (!invert)
36283 if (mask == OPTION_MASK_VSX)
36285 mask |= OPTION_MASK_ALTIVEC;
36286 TARGET_AVOID_XFORM = 0;
36290 if (rs6000_opt_masks[i].invert)
36291 invert = !invert;
36293 if (invert)
36294 rs6000_isa_flags &= ~mask;
36295 else
36296 rs6000_isa_flags |= mask;
36298 break;
36301 if (error_p && !not_valid_p)
36303 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
36304 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
36306 size_t j = rs6000_opt_vars[i].global_offset;
36307 *((int *) ((char *)&global_options + j)) = !invert;
36308 error_p = false;
36309 not_valid_p = false;
36310 break;
36315 if (error_p)
36317 const char *eprefix, *esuffix;
36319 ret = false;
36320 if (attr_p)
36322 eprefix = "__attribute__((__target__(";
36323 esuffix = ")))";
36325 else
36327 eprefix = "#pragma GCC target ";
36328 esuffix = "";
36331 if (cpu_opt)
36332 error ("invalid cpu %qs for %s%qs%s", cpu_opt, eprefix,
36333 q, esuffix);
36334 else if (not_valid_p)
36335 error ("%s%qs%s is not allowed", eprefix, q, esuffix);
36336 else
36337 error ("%s%qs%s is invalid", eprefix, q, esuffix);
36342 else if (TREE_CODE (args) == TREE_LIST)
36346 tree value = TREE_VALUE (args);
36347 if (value)
36349 bool ret2 = rs6000_inner_target_options (value, attr_p);
36350 if (!ret2)
36351 ret = false;
36353 args = TREE_CHAIN (args);
36355 while (args != NULL_TREE);
36358 else
36360 error ("attribute %<target%> argument not a string");
36361 return false;
36364 return ret;
36367 /* Print out the target options as a list for -mdebug=target. */
36369 static void
36370 rs6000_debug_target_options (tree args, const char *prefix)
36372 if (args == NULL_TREE)
36373 fprintf (stderr, "%s<NULL>", prefix);
36375 else if (TREE_CODE (args) == STRING_CST)
36377 char *p = ASTRDUP (TREE_STRING_POINTER (args));
36378 char *q;
36380 while ((q = strtok (p, ",")) != NULL)
36382 p = NULL;
36383 fprintf (stderr, "%s\"%s\"", prefix, q);
36384 prefix = ", ";
36388 else if (TREE_CODE (args) == TREE_LIST)
36392 tree value = TREE_VALUE (args);
36393 if (value)
36395 rs6000_debug_target_options (value, prefix);
36396 prefix = ", ";
36398 args = TREE_CHAIN (args);
36400 while (args != NULL_TREE);
36403 else
36404 gcc_unreachable ();
36406 return;
36410 /* Hook to validate attribute((target("..."))). */
36412 static bool
36413 rs6000_valid_attribute_p (tree fndecl,
36414 tree ARG_UNUSED (name),
36415 tree args,
36416 int flags)
36418 struct cl_target_option cur_target;
36419 bool ret;
36420 tree old_optimize = build_optimization_node (&global_options);
36421 tree new_target, new_optimize;
36422 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
36424 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
36426 if (TARGET_DEBUG_TARGET)
36428 tree tname = DECL_NAME (fndecl);
36429 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
36430 if (tname)
36431 fprintf (stderr, "function: %.*s\n",
36432 (int) IDENTIFIER_LENGTH (tname),
36433 IDENTIFIER_POINTER (tname));
36434 else
36435 fprintf (stderr, "function: unknown\n");
36437 fprintf (stderr, "args:");
36438 rs6000_debug_target_options (args, " ");
36439 fprintf (stderr, "\n");
36441 if (flags)
36442 fprintf (stderr, "flags: 0x%x\n", flags);
36444 fprintf (stderr, "--------------------\n");
36447 /* attribute((target("default"))) does nothing, beyond
36448 affecting multi-versioning. */
36449 if (TREE_VALUE (args)
36450 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
36451 && TREE_CHAIN (args) == NULL_TREE
36452 && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
36453 return true;
36455 old_optimize = build_optimization_node (&global_options);
36456 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
36458 /* If the function changed the optimization levels as well as setting target
36459 options, start with the optimizations specified. */
36460 if (func_optimize && func_optimize != old_optimize)
36461 cl_optimization_restore (&global_options,
36462 TREE_OPTIMIZATION (func_optimize));
36464 /* The target attributes may also change some optimization flags, so update
36465 the optimization options if necessary. */
36466 cl_target_option_save (&cur_target, &global_options);
36467 rs6000_cpu_index = rs6000_tune_index = -1;
36468 ret = rs6000_inner_target_options (args, true);
36470 /* Set up any additional state. */
36471 if (ret)
36473 ret = rs6000_option_override_internal (false);
36474 new_target = build_target_option_node (&global_options);
36476 else
36477 new_target = NULL;
36479 new_optimize = build_optimization_node (&global_options);
36481 if (!new_target)
36482 ret = false;
36484 else if (fndecl)
36486 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
36488 if (old_optimize != new_optimize)
36489 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
36492 cl_target_option_restore (&global_options, &cur_target);
36494 if (old_optimize != new_optimize)
36495 cl_optimization_restore (&global_options,
36496 TREE_OPTIMIZATION (old_optimize));
36498 return ret;
36502 /* Hook to validate the current #pragma GCC target and set the state, and
36503 update the macros based on what was changed. If ARGS is NULL, then
36504 POP_TARGET is used to reset the options. */
36506 bool
36507 rs6000_pragma_target_parse (tree args, tree pop_target)
36509 tree prev_tree = build_target_option_node (&global_options);
36510 tree cur_tree;
36511 struct cl_target_option *prev_opt, *cur_opt;
36512 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
36513 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
36515 if (TARGET_DEBUG_TARGET)
36517 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
36518 fprintf (stderr, "args:");
36519 rs6000_debug_target_options (args, " ");
36520 fprintf (stderr, "\n");
36522 if (pop_target)
36524 fprintf (stderr, "pop_target:\n");
36525 debug_tree (pop_target);
36527 else
36528 fprintf (stderr, "pop_target: <NULL>\n");
36530 fprintf (stderr, "--------------------\n");
36533 if (! args)
36535 cur_tree = ((pop_target)
36536 ? pop_target
36537 : target_option_default_node);
36538 cl_target_option_restore (&global_options,
36539 TREE_TARGET_OPTION (cur_tree));
36541 else
36543 rs6000_cpu_index = rs6000_tune_index = -1;
36544 if (!rs6000_inner_target_options (args, false)
36545 || !rs6000_option_override_internal (false)
36546 || (cur_tree = build_target_option_node (&global_options))
36547 == NULL_TREE)
36549 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
36550 fprintf (stderr, "invalid pragma\n");
36552 return false;
36556 target_option_current_node = cur_tree;
36558 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
36559 change the macros that are defined. */
36560 if (rs6000_target_modify_macros_ptr)
36562 prev_opt = TREE_TARGET_OPTION (prev_tree);
36563 prev_bumask = prev_opt->x_rs6000_builtin_mask;
36564 prev_flags = prev_opt->x_rs6000_isa_flags;
36566 cur_opt = TREE_TARGET_OPTION (cur_tree);
36567 cur_flags = cur_opt->x_rs6000_isa_flags;
36568 cur_bumask = cur_opt->x_rs6000_builtin_mask;
36570 diff_bumask = (prev_bumask ^ cur_bumask);
36571 diff_flags = (prev_flags ^ cur_flags);
36573 if ((diff_flags != 0) || (diff_bumask != 0))
36575 /* Delete old macros. */
36576 rs6000_target_modify_macros_ptr (false,
36577 prev_flags & diff_flags,
36578 prev_bumask & diff_bumask);
36580 /* Define new macros. */
36581 rs6000_target_modify_macros_ptr (true,
36582 cur_flags & diff_flags,
36583 cur_bumask & diff_bumask);
36587 return true;
36591 /* Remember the last target of rs6000_set_current_function. */
36592 static GTY(()) tree rs6000_previous_fndecl;
36594 /* Restore target's globals from NEW_TREE and invalidate the
36595 rs6000_previous_fndecl cache. */
36597 static void
36598 rs6000_activate_target_options (tree new_tree)
36600 cl_target_option_restore (&global_options, TREE_TARGET_OPTION (new_tree));
36601 if (TREE_TARGET_GLOBALS (new_tree))
36602 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
36603 else if (new_tree == target_option_default_node)
36604 restore_target_globals (&default_target_globals);
36605 else
36606 TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
36607 rs6000_previous_fndecl = NULL_TREE;
36610 /* Establish appropriate back-end context for processing the function
36611 FNDECL. The argument might be NULL to indicate processing at top
36612 level, outside of any function scope. */
36613 static void
36614 rs6000_set_current_function (tree fndecl)
36616 if (TARGET_DEBUG_TARGET)
36618 fprintf (stderr, "\n==================== rs6000_set_current_function");
36620 if (fndecl)
36621 fprintf (stderr, ", fndecl %s (%p)",
36622 (DECL_NAME (fndecl)
36623 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
36624 : "<unknown>"), (void *)fndecl);
36626 if (rs6000_previous_fndecl)
36627 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
36629 fprintf (stderr, "\n");
36632 /* Only change the context if the function changes. This hook is called
36633 several times in the course of compiling a function, and we don't want to
36634 slow things down too much or call target_reinit when it isn't safe. */
36635 if (fndecl == rs6000_previous_fndecl)
36636 return;
36638 tree old_tree;
36639 if (rs6000_previous_fndecl == NULL_TREE)
36640 old_tree = target_option_current_node;
36641 else if (DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl))
36642 old_tree = DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl);
36643 else
36644 old_tree = target_option_default_node;
36646 tree new_tree;
36647 if (fndecl == NULL_TREE)
36649 if (old_tree != target_option_current_node)
36650 new_tree = target_option_current_node;
36651 else
36652 new_tree = NULL_TREE;
36654 else
36656 new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
36657 if (new_tree == NULL_TREE)
36658 new_tree = target_option_default_node;
36661 if (TARGET_DEBUG_TARGET)
36663 if (new_tree)
36665 fprintf (stderr, "\nnew fndecl target specific options:\n");
36666 debug_tree (new_tree);
36669 if (old_tree)
36671 fprintf (stderr, "\nold fndecl target specific options:\n");
36672 debug_tree (old_tree);
36675 if (old_tree != NULL_TREE || new_tree != NULL_TREE)
36676 fprintf (stderr, "--------------------\n");
36679 if (new_tree && old_tree != new_tree)
36680 rs6000_activate_target_options (new_tree);
36682 if (fndecl)
36683 rs6000_previous_fndecl = fndecl;
36687 /* Save the current options */
36689 static void
36690 rs6000_function_specific_save (struct cl_target_option *ptr,
36691 struct gcc_options *opts)
36693 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
36694 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
36697 /* Restore the current options */
36699 static void
36700 rs6000_function_specific_restore (struct gcc_options *opts,
36701 struct cl_target_option *ptr)
36704 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
36705 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
36706 (void) rs6000_option_override_internal (false);
36709 /* Print the current options */
36711 static void
36712 rs6000_function_specific_print (FILE *file, int indent,
36713 struct cl_target_option *ptr)
36715 rs6000_print_isa_options (file, indent, "Isa options set",
36716 ptr->x_rs6000_isa_flags);
36718 rs6000_print_isa_options (file, indent, "Isa options explicit",
36719 ptr->x_rs6000_isa_flags_explicit);
36722 /* Helper function to print the current isa or misc options on a line. */
36724 static void
36725 rs6000_print_options_internal (FILE *file,
36726 int indent,
36727 const char *string,
36728 HOST_WIDE_INT flags,
36729 const char *prefix,
36730 const struct rs6000_opt_mask *opts,
36731 size_t num_elements)
36733 size_t i;
36734 size_t start_column = 0;
36735 size_t cur_column;
36736 size_t max_column = 120;
36737 size_t prefix_len = strlen (prefix);
36738 size_t comma_len = 0;
36739 const char *comma = "";
36741 if (indent)
36742 start_column += fprintf (file, "%*s", indent, "");
36744 if (!flags)
36746 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
36747 return;
36750 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
36752 /* Print the various mask options. */
36753 cur_column = start_column;
36754 for (i = 0; i < num_elements; i++)
36756 bool invert = opts[i].invert;
36757 const char *name = opts[i].name;
36758 const char *no_str = "";
36759 HOST_WIDE_INT mask = opts[i].mask;
36760 size_t len = comma_len + prefix_len + strlen (name);
36762 if (!invert)
36764 if ((flags & mask) == 0)
36766 no_str = "no-";
36767 len += sizeof ("no-") - 1;
36770 flags &= ~mask;
36773 else
36775 if ((flags & mask) != 0)
36777 no_str = "no-";
36778 len += sizeof ("no-") - 1;
36781 flags |= mask;
36784 cur_column += len;
36785 if (cur_column > max_column)
36787 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
36788 cur_column = start_column + len;
36789 comma = "";
36792 fprintf (file, "%s%s%s%s", comma, prefix, no_str, name);
36793 comma = ", ";
36794 comma_len = sizeof (", ") - 1;
36797 fputs ("\n", file);
36800 /* Helper function to print the current isa options on a line. */
36802 static void
36803 rs6000_print_isa_options (FILE *file, int indent, const char *string,
36804 HOST_WIDE_INT flags)
36806 rs6000_print_options_internal (file, indent, string, flags, "-m",
36807 &rs6000_opt_masks[0],
36808 ARRAY_SIZE (rs6000_opt_masks));
36811 static void
36812 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
36813 HOST_WIDE_INT flags)
36815 rs6000_print_options_internal (file, indent, string, flags, "",
36816 &rs6000_builtin_mask_names[0],
36817 ARRAY_SIZE (rs6000_builtin_mask_names));
36820 /* If the user used -mno-vsx, we need turn off all of the implicit ISA 2.06,
36821 2.07, and 3.0 options that relate to the vector unit (-mdirect-move,
36822 -mupper-regs-df, etc.).
36824 If the user used -mno-power8-vector, we need to turn off all of the implicit
36825 ISA 2.07 and 3.0 options that relate to the vector unit.
36827 If the user used -mno-power9-vector, we need to turn off all of the implicit
36828 ISA 3.0 options that relate to the vector unit.
36830 This function does not handle explicit options such as the user specifying
36831 -mdirect-move. These are handled in rs6000_option_override_internal, and
36832 the appropriate error is given if needed.
36834 We return a mask of all of the implicit options that should not be enabled
36835 by default. */
36837 static HOST_WIDE_INT
36838 rs6000_disable_incompatible_switches (void)
36840 HOST_WIDE_INT ignore_masks = rs6000_isa_flags_explicit;
36841 size_t i, j;
36843 static const struct {
36844 const HOST_WIDE_INT no_flag; /* flag explicitly turned off. */
36845 const HOST_WIDE_INT dep_flags; /* flags that depend on this option. */
36846 const char *const name; /* name of the switch. */
36847 } flags[] = {
36848 { OPTION_MASK_P9_VECTOR, OTHER_P9_VECTOR_MASKS, "power9-vector" },
36849 { OPTION_MASK_P8_VECTOR, OTHER_P8_VECTOR_MASKS, "power8-vector" },
36850 { OPTION_MASK_VSX, OTHER_VSX_VECTOR_MASKS, "vsx" },
36853 for (i = 0; i < ARRAY_SIZE (flags); i++)
36855 HOST_WIDE_INT no_flag = flags[i].no_flag;
36857 if ((rs6000_isa_flags & no_flag) == 0
36858 && (rs6000_isa_flags_explicit & no_flag) != 0)
36860 HOST_WIDE_INT dep_flags = flags[i].dep_flags;
36861 HOST_WIDE_INT set_flags = (rs6000_isa_flags_explicit
36862 & rs6000_isa_flags
36863 & dep_flags);
36865 if (set_flags)
36867 for (j = 0; j < ARRAY_SIZE (rs6000_opt_masks); j++)
36868 if ((set_flags & rs6000_opt_masks[j].mask) != 0)
36870 set_flags &= ~rs6000_opt_masks[j].mask;
36871 error ("%<-mno-%s%> turns off %<-m%s%>",
36872 flags[i].name,
36873 rs6000_opt_masks[j].name);
36876 gcc_assert (!set_flags);
36879 rs6000_isa_flags &= ~dep_flags;
36880 ignore_masks |= no_flag | dep_flags;
36884 return ignore_masks;
36888 /* Helper function for printing the function name when debugging. */
36890 static const char *
36891 get_decl_name (tree fn)
36893 tree name;
36895 if (!fn)
36896 return "<null>";
36898 name = DECL_NAME (fn);
36899 if (!name)
36900 return "<no-name>";
36902 return IDENTIFIER_POINTER (name);
36905 /* Return the clone id of the target we are compiling code for in a target
36906 clone. The clone id is ordered from 0 (default) to CLONE_MAX-1 and gives
36907 the priority list for the target clones (ordered from lowest to
36908 highest). */
36910 static int
36911 rs6000_clone_priority (tree fndecl)
36913 tree fn_opts = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
36914 HOST_WIDE_INT isa_masks;
36915 int ret = CLONE_DEFAULT;
36916 tree attrs = lookup_attribute ("target", DECL_ATTRIBUTES (fndecl));
36917 const char *attrs_str = NULL;
36919 attrs = TREE_VALUE (TREE_VALUE (attrs));
36920 attrs_str = TREE_STRING_POINTER (attrs);
36922 /* Return priority zero for default function. Return the ISA needed for the
36923 function if it is not the default. */
36924 if (strcmp (attrs_str, "default") != 0)
36926 if (fn_opts == NULL_TREE)
36927 fn_opts = target_option_default_node;
36929 if (!fn_opts || !TREE_TARGET_OPTION (fn_opts))
36930 isa_masks = rs6000_isa_flags;
36931 else
36932 isa_masks = TREE_TARGET_OPTION (fn_opts)->x_rs6000_isa_flags;
36934 for (ret = CLONE_MAX - 1; ret != 0; ret--)
36935 if ((rs6000_clone_map[ret].isa_mask & isa_masks) != 0)
36936 break;
36939 if (TARGET_DEBUG_TARGET)
36940 fprintf (stderr, "rs6000_get_function_version_priority (%s) => %d\n",
36941 get_decl_name (fndecl), ret);
36943 return ret;
36946 /* This compares the priority of target features in function DECL1 and DECL2.
36947 It returns positive value if DECL1 is higher priority, negative value if
36948 DECL2 is higher priority and 0 if they are the same. Note, priorities are
36949 ordered from lowest (CLONE_DEFAULT) to highest (currently CLONE_ISA_3_0). */
36951 static int
36952 rs6000_compare_version_priority (tree decl1, tree decl2)
36954 int priority1 = rs6000_clone_priority (decl1);
36955 int priority2 = rs6000_clone_priority (decl2);
36956 int ret = priority1 - priority2;
36958 if (TARGET_DEBUG_TARGET)
36959 fprintf (stderr, "rs6000_compare_version_priority (%s, %s) => %d\n",
36960 get_decl_name (decl1), get_decl_name (decl2), ret);
36962 return ret;
36965 /* Make a dispatcher declaration for the multi-versioned function DECL.
36966 Calls to DECL function will be replaced with calls to the dispatcher
36967 by the front-end. Returns the decl of the dispatcher function. */
36969 static tree
36970 rs6000_get_function_versions_dispatcher (void *decl)
36972 tree fn = (tree) decl;
36973 struct cgraph_node *node = NULL;
36974 struct cgraph_node *default_node = NULL;
36975 struct cgraph_function_version_info *node_v = NULL;
36976 struct cgraph_function_version_info *first_v = NULL;
36978 tree dispatch_decl = NULL;
36980 struct cgraph_function_version_info *default_version_info = NULL;
36981 gcc_assert (fn != NULL && DECL_FUNCTION_VERSIONED (fn));
36983 if (TARGET_DEBUG_TARGET)
36984 fprintf (stderr, "rs6000_get_function_versions_dispatcher (%s)\n",
36985 get_decl_name (fn));
36987 node = cgraph_node::get (fn);
36988 gcc_assert (node != NULL);
36990 node_v = node->function_version ();
36991 gcc_assert (node_v != NULL);
36993 if (node_v->dispatcher_resolver != NULL)
36994 return node_v->dispatcher_resolver;
36996 /* Find the default version and make it the first node. */
36997 first_v = node_v;
36998 /* Go to the beginning of the chain. */
36999 while (first_v->prev != NULL)
37000 first_v = first_v->prev;
37002 default_version_info = first_v;
37003 while (default_version_info != NULL)
37005 const tree decl2 = default_version_info->this_node->decl;
37006 if (is_function_default_version (decl2))
37007 break;
37008 default_version_info = default_version_info->next;
37011 /* If there is no default node, just return NULL. */
37012 if (default_version_info == NULL)
37013 return NULL;
37015 /* Make default info the first node. */
37016 if (first_v != default_version_info)
37018 default_version_info->prev->next = default_version_info->next;
37019 if (default_version_info->next)
37020 default_version_info->next->prev = default_version_info->prev;
37021 first_v->prev = default_version_info;
37022 default_version_info->next = first_v;
37023 default_version_info->prev = NULL;
37026 default_node = default_version_info->this_node;
37028 #ifndef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
37029 error_at (DECL_SOURCE_LOCATION (default_node->decl),
37030 "target_clones attribute needs GLIBC (2.23 and newer) that "
37031 "exports hardware capability bits");
37032 #else
37034 if (targetm.has_ifunc_p ())
37036 struct cgraph_function_version_info *it_v = NULL;
37037 struct cgraph_node *dispatcher_node = NULL;
37038 struct cgraph_function_version_info *dispatcher_version_info = NULL;
37040 /* Right now, the dispatching is done via ifunc. */
37041 dispatch_decl = make_dispatcher_decl (default_node->decl);
37043 dispatcher_node = cgraph_node::get_create (dispatch_decl);
37044 gcc_assert (dispatcher_node != NULL);
37045 dispatcher_node->dispatcher_function = 1;
37046 dispatcher_version_info
37047 = dispatcher_node->insert_new_function_version ();
37048 dispatcher_version_info->next = default_version_info;
37049 dispatcher_node->definition = 1;
37051 /* Set the dispatcher for all the versions. */
37052 it_v = default_version_info;
37053 while (it_v != NULL)
37055 it_v->dispatcher_resolver = dispatch_decl;
37056 it_v = it_v->next;
37059 else
37061 error_at (DECL_SOURCE_LOCATION (default_node->decl),
37062 "multiversioning needs ifunc which is not supported "
37063 "on this target");
37065 #endif
37067 return dispatch_decl;
37070 /* Make the resolver function decl to dispatch the versions of a multi-
37071 versioned function, DEFAULT_DECL. Create an empty basic block in the
37072 resolver and store the pointer in EMPTY_BB. Return the decl of the resolver
37073 function. */
37075 static tree
37076 make_resolver_func (const tree default_decl,
37077 const tree dispatch_decl,
37078 basic_block *empty_bb)
37080 /* Make the resolver function static. The resolver function returns
37081 void *. */
37082 tree decl_name = clone_function_name (default_decl, "resolver");
37083 const char *resolver_name = IDENTIFIER_POINTER (decl_name);
37084 tree type = build_function_type_list (ptr_type_node, NULL_TREE);
37085 tree decl = build_fn_decl (resolver_name, type);
37086 SET_DECL_ASSEMBLER_NAME (decl, decl_name);
37088 DECL_NAME (decl) = decl_name;
37089 TREE_USED (decl) = 1;
37090 DECL_ARTIFICIAL (decl) = 1;
37091 DECL_IGNORED_P (decl) = 0;
37092 TREE_PUBLIC (decl) = 0;
37093 DECL_UNINLINABLE (decl) = 1;
37095 /* Resolver is not external, body is generated. */
37096 DECL_EXTERNAL (decl) = 0;
37097 DECL_EXTERNAL (dispatch_decl) = 0;
37099 DECL_CONTEXT (decl) = NULL_TREE;
37100 DECL_INITIAL (decl) = make_node (BLOCK);
37101 DECL_STATIC_CONSTRUCTOR (decl) = 0;
37103 /* Build result decl and add to function_decl. */
37104 tree t = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, ptr_type_node);
37105 DECL_ARTIFICIAL (t) = 1;
37106 DECL_IGNORED_P (t) = 1;
37107 DECL_RESULT (decl) = t;
37109 gimplify_function_tree (decl);
37110 push_cfun (DECL_STRUCT_FUNCTION (decl));
37111 *empty_bb = init_lowered_empty_function (decl, false,
37112 profile_count::uninitialized ());
37114 cgraph_node::add_new_function (decl, true);
37115 symtab->call_cgraph_insertion_hooks (cgraph_node::get_create (decl));
37117 pop_cfun ();
37119 /* Mark dispatch_decl as "ifunc" with resolver as resolver_name. */
37120 DECL_ATTRIBUTES (dispatch_decl)
37121 = make_attribute ("ifunc", resolver_name, DECL_ATTRIBUTES (dispatch_decl));
37123 cgraph_node::create_same_body_alias (dispatch_decl, decl);
37125 return decl;
37128 /* This adds a condition to the basic_block NEW_BB in function FUNCTION_DECL to
37129 return a pointer to VERSION_DECL if we are running on a machine that
37130 supports the index CLONE_ISA hardware architecture bits. This function will
37131 be called during version dispatch to decide which function version to
37132 execute. It returns the basic block at the end, to which more conditions
37133 can be added. */
37135 static basic_block
37136 add_condition_to_bb (tree function_decl, tree version_decl,
37137 int clone_isa, basic_block new_bb)
37139 push_cfun (DECL_STRUCT_FUNCTION (function_decl));
37141 gcc_assert (new_bb != NULL);
37142 gimple_seq gseq = bb_seq (new_bb);
37145 tree convert_expr = build1 (CONVERT_EXPR, ptr_type_node,
37146 build_fold_addr_expr (version_decl));
37147 tree result_var = create_tmp_var (ptr_type_node);
37148 gimple *convert_stmt = gimple_build_assign (result_var, convert_expr);
37149 gimple *return_stmt = gimple_build_return (result_var);
37151 if (clone_isa == CLONE_DEFAULT)
37153 gimple_seq_add_stmt (&gseq, convert_stmt);
37154 gimple_seq_add_stmt (&gseq, return_stmt);
37155 set_bb_seq (new_bb, gseq);
37156 gimple_set_bb (convert_stmt, new_bb);
37157 gimple_set_bb (return_stmt, new_bb);
37158 pop_cfun ();
37159 return new_bb;
37162 tree bool_zero = build_int_cst (bool_int_type_node, 0);
37163 tree cond_var = create_tmp_var (bool_int_type_node);
37164 tree predicate_decl = rs6000_builtin_decls [(int) RS6000_BUILTIN_CPU_SUPPORTS];
37165 const char *arg_str = rs6000_clone_map[clone_isa].name;
37166 tree predicate_arg = build_string_literal (strlen (arg_str) + 1, arg_str);
37167 gimple *call_cond_stmt = gimple_build_call (predicate_decl, 1, predicate_arg);
37168 gimple_call_set_lhs (call_cond_stmt, cond_var);
37170 gimple_set_block (call_cond_stmt, DECL_INITIAL (function_decl));
37171 gimple_set_bb (call_cond_stmt, new_bb);
37172 gimple_seq_add_stmt (&gseq, call_cond_stmt);
37174 gimple *if_else_stmt = gimple_build_cond (NE_EXPR, cond_var, bool_zero,
37175 NULL_TREE, NULL_TREE);
37176 gimple_set_block (if_else_stmt, DECL_INITIAL (function_decl));
37177 gimple_set_bb (if_else_stmt, new_bb);
37178 gimple_seq_add_stmt (&gseq, if_else_stmt);
37180 gimple_seq_add_stmt (&gseq, convert_stmt);
37181 gimple_seq_add_stmt (&gseq, return_stmt);
37182 set_bb_seq (new_bb, gseq);
37184 basic_block bb1 = new_bb;
37185 edge e12 = split_block (bb1, if_else_stmt);
37186 basic_block bb2 = e12->dest;
37187 e12->flags &= ~EDGE_FALLTHRU;
37188 e12->flags |= EDGE_TRUE_VALUE;
37190 edge e23 = split_block (bb2, return_stmt);
37191 gimple_set_bb (convert_stmt, bb2);
37192 gimple_set_bb (return_stmt, bb2);
37194 basic_block bb3 = e23->dest;
37195 make_edge (bb1, bb3, EDGE_FALSE_VALUE);
37197 remove_edge (e23);
37198 make_edge (bb2, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
37200 pop_cfun ();
37201 return bb3;
37204 /* This function generates the dispatch function for multi-versioned functions.
37205 DISPATCH_DECL is the function which will contain the dispatch logic.
37206 FNDECLS are the function choices for dispatch, and is a tree chain.
37207 EMPTY_BB is the basic block pointer in DISPATCH_DECL in which the dispatch
37208 code is generated. */
37210 static int
37211 dispatch_function_versions (tree dispatch_decl,
37212 void *fndecls_p,
37213 basic_block *empty_bb)
37215 int ix;
37216 tree ele;
37217 vec<tree> *fndecls;
37218 tree clones[CLONE_MAX];
37220 if (TARGET_DEBUG_TARGET)
37221 fputs ("dispatch_function_versions, top\n", stderr);
37223 gcc_assert (dispatch_decl != NULL
37224 && fndecls_p != NULL
37225 && empty_bb != NULL);
37227 /* fndecls_p is actually a vector. */
37228 fndecls = static_cast<vec<tree> *> (fndecls_p);
37230 /* At least one more version other than the default. */
37231 gcc_assert (fndecls->length () >= 2);
37233 /* The first version in the vector is the default decl. */
37234 memset ((void *) clones, '\0', sizeof (clones));
37235 clones[CLONE_DEFAULT] = (*fndecls)[0];
37237 /* On the PowerPC, we do not need to call __builtin_cpu_init, which is a NOP
37238 on the PowerPC (on the x86_64, it is not a NOP). The builtin function
37239 __builtin_cpu_support ensures that the TOC fields are setup by requiring a
37240 recent glibc. If we ever need to call __builtin_cpu_init, we would need
37241 to insert the code here to do the call. */
37243 for (ix = 1; fndecls->iterate (ix, &ele); ++ix)
37245 int priority = rs6000_clone_priority (ele);
37246 if (!clones[priority])
37247 clones[priority] = ele;
37250 for (ix = CLONE_MAX - 1; ix >= 0; ix--)
37251 if (clones[ix])
37253 if (TARGET_DEBUG_TARGET)
37254 fprintf (stderr, "dispatch_function_versions, clone %d, %s\n",
37255 ix, get_decl_name (clones[ix]));
37257 *empty_bb = add_condition_to_bb (dispatch_decl, clones[ix], ix,
37258 *empty_bb);
37261 return 0;
37264 /* Generate the dispatching code body to dispatch multi-versioned function
37265 DECL. The target hook is called to process the "target" attributes and
37266 provide the code to dispatch the right function at run-time. NODE points
37267 to the dispatcher decl whose body will be created. */
37269 static tree
37270 rs6000_generate_version_dispatcher_body (void *node_p)
37272 tree resolver;
37273 basic_block empty_bb;
37274 struct cgraph_node *node = (cgraph_node *) node_p;
37275 struct cgraph_function_version_info *ninfo = node->function_version ();
37277 if (ninfo->dispatcher_resolver)
37278 return ninfo->dispatcher_resolver;
37280 /* node is going to be an alias, so remove the finalized bit. */
37281 node->definition = false;
37283 /* The first version in the chain corresponds to the default version. */
37284 ninfo->dispatcher_resolver = resolver
37285 = make_resolver_func (ninfo->next->this_node->decl, node->decl, &empty_bb);
37287 if (TARGET_DEBUG_TARGET)
37288 fprintf (stderr, "rs6000_get_function_versions_dispatcher, %s\n",
37289 get_decl_name (resolver));
37291 push_cfun (DECL_STRUCT_FUNCTION (resolver));
37292 auto_vec<tree, 2> fn_ver_vec;
37294 for (struct cgraph_function_version_info *vinfo = ninfo->next;
37295 vinfo;
37296 vinfo = vinfo->next)
37298 struct cgraph_node *version = vinfo->this_node;
37299 /* Check for virtual functions here again, as by this time it should
37300 have been determined if this function needs a vtable index or
37301 not. This happens for methods in derived classes that override
37302 virtual methods in base classes but are not explicitly marked as
37303 virtual. */
37304 if (DECL_VINDEX (version->decl))
37305 sorry ("Virtual function multiversioning not supported");
37307 fn_ver_vec.safe_push (version->decl);
37310 dispatch_function_versions (resolver, &fn_ver_vec, &empty_bb);
37311 cgraph_edge::rebuild_edges ();
37312 pop_cfun ();
37313 return resolver;
37317 /* Hook to determine if one function can safely inline another. */
37319 static bool
37320 rs6000_can_inline_p (tree caller, tree callee)
37322 bool ret = false;
37323 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
37324 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
37326 /* If callee has no option attributes, then it is ok to inline. */
37327 if (!callee_tree)
37328 ret = true;
37330 /* If caller has no option attributes, but callee does then it is not ok to
37331 inline. */
37332 else if (!caller_tree)
37333 ret = false;
37335 else
37337 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
37338 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
37340 /* Callee's options should a subset of the caller's, i.e. a vsx function
37341 can inline an altivec function but a non-vsx function can't inline a
37342 vsx function. */
37343 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
37344 == callee_opts->x_rs6000_isa_flags)
37345 ret = true;
37348 if (TARGET_DEBUG_TARGET)
37349 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
37350 get_decl_name (caller), get_decl_name (callee),
37351 (ret ? "can" : "cannot"));
37353 return ret;
37356 /* Allocate a stack temp and fixup the address so it meets the particular
37357 memory requirements (either offetable or REG+REG addressing). */
37360 rs6000_allocate_stack_temp (machine_mode mode,
37361 bool offsettable_p,
37362 bool reg_reg_p)
37364 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
37365 rtx addr = XEXP (stack, 0);
37366 int strict_p = reload_completed;
37368 if (!legitimate_indirect_address_p (addr, strict_p))
37370 if (offsettable_p
37371 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
37372 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
37374 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
37375 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
37378 return stack;
37381 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
37382 to such a form to deal with memory reference instructions like STFIWX that
37383 only take reg+reg addressing. */
37386 rs6000_address_for_fpconvert (rtx x)
37388 rtx addr;
37390 gcc_assert (MEM_P (x));
37391 addr = XEXP (x, 0);
37392 if (! legitimate_indirect_address_p (addr, reload_completed)
37393 && ! legitimate_indexed_address_p (addr, reload_completed))
37395 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
37397 rtx reg = XEXP (addr, 0);
37398 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
37399 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
37400 gcc_assert (REG_P (reg));
37401 emit_insn (gen_add3_insn (reg, reg, size_rtx));
37402 addr = reg;
37404 else if (GET_CODE (addr) == PRE_MODIFY)
37406 rtx reg = XEXP (addr, 0);
37407 rtx expr = XEXP (addr, 1);
37408 gcc_assert (REG_P (reg));
37409 gcc_assert (GET_CODE (expr) == PLUS);
37410 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
37411 addr = reg;
37414 x = replace_equiv_address (x, copy_addr_to_reg (addr));
37417 return x;
37420 /* Given a memory reference, if it is not in the form for altivec memory
37421 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
37422 convert to the altivec format. */
37425 rs6000_address_for_altivec (rtx x)
37427 gcc_assert (MEM_P (x));
37428 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
37430 rtx addr = XEXP (x, 0);
37432 if (!legitimate_indexed_address_p (addr, reload_completed)
37433 && !legitimate_indirect_address_p (addr, reload_completed))
37434 addr = copy_to_mode_reg (Pmode, addr);
37436 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
37437 x = change_address (x, GET_MODE (x), addr);
37440 return x;
37443 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
37445 On the RS/6000, all integer constants are acceptable, most won't be valid
37446 for particular insns, though. Only easy FP constants are acceptable. */
37448 static bool
37449 rs6000_legitimate_constant_p (machine_mode mode, rtx x)
37451 if (TARGET_ELF && tls_referenced_p (x))
37452 return false;
37454 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
37455 || GET_MODE (x) == VOIDmode
37456 || (TARGET_POWERPC64 && mode == DImode)
37457 || easy_fp_constant (x, mode)
37458 || easy_vector_constant (x, mode));
37462 /* Return TRUE iff the sequence ending in LAST sets the static chain. */
37464 static bool
37465 chain_already_loaded (rtx_insn *last)
37467 for (; last != NULL; last = PREV_INSN (last))
37469 if (NONJUMP_INSN_P (last))
37471 rtx patt = PATTERN (last);
37473 if (GET_CODE (patt) == SET)
37475 rtx lhs = XEXP (patt, 0);
37477 if (REG_P (lhs) && REGNO (lhs) == STATIC_CHAIN_REGNUM)
37478 return true;
37482 return false;
37485 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
37487 void
37488 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
37490 const bool direct_call_p
37491 = GET_CODE (func_desc) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (func_desc);
37492 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
37493 rtx toc_load = NULL_RTX;
37494 rtx toc_restore = NULL_RTX;
37495 rtx func_addr;
37496 rtx abi_reg = NULL_RTX;
37497 rtx call[4];
37498 int n_call;
37499 rtx insn;
37501 /* Handle longcall attributes. */
37502 if (INTVAL (cookie) & CALL_LONG)
37503 func_desc = rs6000_longcall_ref (func_desc);
37505 /* Handle indirect calls. */
37506 if (GET_CODE (func_desc) != SYMBOL_REF
37507 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
37509 /* Save the TOC into its reserved slot before the call,
37510 and prepare to restore it after the call. */
37511 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
37512 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
37513 rtx stack_toc_mem = gen_frame_mem (Pmode,
37514 gen_rtx_PLUS (Pmode, stack_ptr,
37515 stack_toc_offset));
37516 rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode,
37517 gen_rtvec (1, stack_toc_offset),
37518 UNSPEC_TOCSLOT);
37519 toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec);
37521 /* Can we optimize saving the TOC in the prologue or
37522 do we need to do it at every call? */
37523 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
37524 cfun->machine->save_toc_in_prologue = true;
37525 else
37527 MEM_VOLATILE_P (stack_toc_mem) = 1;
37528 emit_move_insn (stack_toc_mem, toc_reg);
37531 if (DEFAULT_ABI == ABI_ELFv2)
37533 /* A function pointer in the ELFv2 ABI is just a plain address, but
37534 the ABI requires it to be loaded into r12 before the call. */
37535 func_addr = gen_rtx_REG (Pmode, 12);
37536 emit_move_insn (func_addr, func_desc);
37537 abi_reg = func_addr;
37539 else
37541 /* A function pointer under AIX is a pointer to a data area whose
37542 first word contains the actual address of the function, whose
37543 second word contains a pointer to its TOC, and whose third word
37544 contains a value to place in the static chain register (r11).
37545 Note that if we load the static chain, our "trampoline" need
37546 not have any executable code. */
37548 /* Load up address of the actual function. */
37549 func_desc = force_reg (Pmode, func_desc);
37550 func_addr = gen_reg_rtx (Pmode);
37551 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
37553 /* Prepare to load the TOC of the called function. Note that the
37554 TOC load must happen immediately before the actual call so
37555 that unwinding the TOC registers works correctly. See the
37556 comment in frob_update_context. */
37557 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
37558 rtx func_toc_mem = gen_rtx_MEM (Pmode,
37559 gen_rtx_PLUS (Pmode, func_desc,
37560 func_toc_offset));
37561 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
37563 /* If we have a static chain, load it up. But, if the call was
37564 originally direct, the 3rd word has not been written since no
37565 trampoline has been built, so we ought not to load it, lest we
37566 override a static chain value. */
37567 if (!direct_call_p
37568 && TARGET_POINTERS_TO_NESTED_FUNCTIONS
37569 && !chain_already_loaded (get_current_sequence ()->next->last))
37571 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
37572 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
37573 rtx func_sc_mem = gen_rtx_MEM (Pmode,
37574 gen_rtx_PLUS (Pmode, func_desc,
37575 func_sc_offset));
37576 emit_move_insn (sc_reg, func_sc_mem);
37577 abi_reg = sc_reg;
37581 else
37583 /* Direct calls use the TOC: for local calls, the callee will
37584 assume the TOC register is set; for non-local calls, the
37585 PLT stub needs the TOC register. */
37586 abi_reg = toc_reg;
37587 func_addr = func_desc;
37590 /* Create the call. */
37591 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
37592 if (value != NULL_RTX)
37593 call[0] = gen_rtx_SET (value, call[0]);
37594 n_call = 1;
37596 if (toc_load)
37597 call[n_call++] = toc_load;
37598 if (toc_restore)
37599 call[n_call++] = toc_restore;
37601 call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
37603 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
37604 insn = emit_call_insn (insn);
37606 /* Mention all registers defined by the ABI to hold information
37607 as uses in CALL_INSN_FUNCTION_USAGE. */
37608 if (abi_reg)
37609 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
37612 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
37614 void
37615 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
37617 rtx call[2];
37618 rtx insn;
37620 gcc_assert (INTVAL (cookie) == 0);
37622 /* Create the call. */
37623 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
37624 if (value != NULL_RTX)
37625 call[0] = gen_rtx_SET (value, call[0]);
37627 call[1] = simple_return_rtx;
37629 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
37630 insn = emit_call_insn (insn);
37632 /* Note use of the TOC register. */
37633 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
37636 /* Return whether we need to always update the saved TOC pointer when we update
37637 the stack pointer. */
37639 static bool
37640 rs6000_save_toc_in_prologue_p (void)
37642 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
37645 #ifdef HAVE_GAS_HIDDEN
37646 # define USE_HIDDEN_LINKONCE 1
37647 #else
37648 # define USE_HIDDEN_LINKONCE 0
37649 #endif
37651 /* Fills in the label name that should be used for a 476 link stack thunk. */
37653 void
37654 get_ppc476_thunk_name (char name[32])
37656 gcc_assert (TARGET_LINK_STACK);
37658 if (USE_HIDDEN_LINKONCE)
37659 sprintf (name, "__ppc476.get_thunk");
37660 else
37661 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
37664 /* This function emits the simple thunk routine that is used to preserve
37665 the link stack on the 476 cpu. */
37667 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
37668 static void
37669 rs6000_code_end (void)
37671 char name[32];
37672 tree decl;
37674 if (!TARGET_LINK_STACK)
37675 return;
37677 get_ppc476_thunk_name (name);
37679 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
37680 build_function_type_list (void_type_node, NULL_TREE));
37681 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
37682 NULL_TREE, void_type_node);
37683 TREE_PUBLIC (decl) = 1;
37684 TREE_STATIC (decl) = 1;
37686 #if RS6000_WEAK
37687 if (USE_HIDDEN_LINKONCE && !TARGET_XCOFF)
37689 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
37690 targetm.asm_out.unique_section (decl, 0);
37691 switch_to_section (get_named_section (decl, NULL, 0));
37692 DECL_WEAK (decl) = 1;
37693 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
37694 targetm.asm_out.globalize_label (asm_out_file, name);
37695 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
37696 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
37698 else
37699 #endif
37701 switch_to_section (text_section);
37702 ASM_OUTPUT_LABEL (asm_out_file, name);
37705 DECL_INITIAL (decl) = make_node (BLOCK);
37706 current_function_decl = decl;
37707 allocate_struct_function (decl, false);
37708 init_function_start (decl);
37709 first_function_block_is_cold = false;
37710 /* Make sure unwind info is emitted for the thunk if needed. */
37711 final_start_function (emit_barrier (), asm_out_file, 1);
37713 fputs ("\tblr\n", asm_out_file);
37715 final_end_function ();
37716 init_insn_lengths ();
37717 free_after_compilation (cfun);
37718 set_cfun (NULL);
37719 current_function_decl = NULL;
37722 /* Add r30 to hard reg set if the prologue sets it up and it is not
37723 pic_offset_table_rtx. */
37725 static void
37726 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
37728 if (!TARGET_SINGLE_PIC_BASE
37729 && TARGET_TOC
37730 && TARGET_MINIMAL_TOC
37731 && !constant_pool_empty_p ())
37732 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
37733 if (cfun->machine->split_stack_argp_used)
37734 add_to_hard_reg_set (&set->set, Pmode, 12);
37738 /* Helper function for rs6000_split_logical to emit a logical instruction after
37739 spliting the operation to single GPR registers.
37741 DEST is the destination register.
37742 OP1 and OP2 are the input source registers.
37743 CODE is the base operation (AND, IOR, XOR, NOT).
37744 MODE is the machine mode.
37745 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
37746 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
37747 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
37749 static void
37750 rs6000_split_logical_inner (rtx dest,
37751 rtx op1,
37752 rtx op2,
37753 enum rtx_code code,
37754 machine_mode mode,
37755 bool complement_final_p,
37756 bool complement_op1_p,
37757 bool complement_op2_p)
37759 rtx bool_rtx;
37761 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
37762 if (op2 && GET_CODE (op2) == CONST_INT
37763 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
37764 && !complement_final_p && !complement_op1_p && !complement_op2_p)
37766 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
37767 HOST_WIDE_INT value = INTVAL (op2) & mask;
37769 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
37770 if (code == AND)
37772 if (value == 0)
37774 emit_insn (gen_rtx_SET (dest, const0_rtx));
37775 return;
37778 else if (value == mask)
37780 if (!rtx_equal_p (dest, op1))
37781 emit_insn (gen_rtx_SET (dest, op1));
37782 return;
37786 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
37787 into separate ORI/ORIS or XORI/XORIS instrucitons. */
37788 else if (code == IOR || code == XOR)
37790 if (value == 0)
37792 if (!rtx_equal_p (dest, op1))
37793 emit_insn (gen_rtx_SET (dest, op1));
37794 return;
37799 if (code == AND && mode == SImode
37800 && !complement_final_p && !complement_op1_p && !complement_op2_p)
37802 emit_insn (gen_andsi3 (dest, op1, op2));
37803 return;
37806 if (complement_op1_p)
37807 op1 = gen_rtx_NOT (mode, op1);
37809 if (complement_op2_p)
37810 op2 = gen_rtx_NOT (mode, op2);
37812 /* For canonical RTL, if only one arm is inverted it is the first. */
37813 if (!complement_op1_p && complement_op2_p)
37814 std::swap (op1, op2);
37816 bool_rtx = ((code == NOT)
37817 ? gen_rtx_NOT (mode, op1)
37818 : gen_rtx_fmt_ee (code, mode, op1, op2));
37820 if (complement_final_p)
37821 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
37823 emit_insn (gen_rtx_SET (dest, bool_rtx));
37826 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
37827 operations are split immediately during RTL generation to allow for more
37828 optimizations of the AND/IOR/XOR.
37830 OPERANDS is an array containing the destination and two input operands.
37831 CODE is the base operation (AND, IOR, XOR, NOT).
37832 MODE is the machine mode.
37833 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
37834 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
37835 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
37836 CLOBBER_REG is either NULL or a scratch register of type CC to allow
37837 formation of the AND instructions. */
37839 static void
37840 rs6000_split_logical_di (rtx operands[3],
37841 enum rtx_code code,
37842 bool complement_final_p,
37843 bool complement_op1_p,
37844 bool complement_op2_p)
37846 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
37847 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
37848 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
37849 enum hi_lo { hi = 0, lo = 1 };
37850 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
37851 size_t i;
37853 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
37854 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
37855 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
37856 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
37858 if (code == NOT)
37859 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
37860 else
37862 if (GET_CODE (operands[2]) != CONST_INT)
37864 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
37865 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
37867 else
37869 HOST_WIDE_INT value = INTVAL (operands[2]);
37870 HOST_WIDE_INT value_hi_lo[2];
37872 gcc_assert (!complement_final_p);
37873 gcc_assert (!complement_op1_p);
37874 gcc_assert (!complement_op2_p);
37876 value_hi_lo[hi] = value >> 32;
37877 value_hi_lo[lo] = value & lower_32bits;
37879 for (i = 0; i < 2; i++)
37881 HOST_WIDE_INT sub_value = value_hi_lo[i];
37883 if (sub_value & sign_bit)
37884 sub_value |= upper_32bits;
37886 op2_hi_lo[i] = GEN_INT (sub_value);
37888 /* If this is an AND instruction, check to see if we need to load
37889 the value in a register. */
37890 if (code == AND && sub_value != -1 && sub_value != 0
37891 && !and_operand (op2_hi_lo[i], SImode))
37892 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
37897 for (i = 0; i < 2; i++)
37899 /* Split large IOR/XOR operations. */
37900 if ((code == IOR || code == XOR)
37901 && GET_CODE (op2_hi_lo[i]) == CONST_INT
37902 && !complement_final_p
37903 && !complement_op1_p
37904 && !complement_op2_p
37905 && !logical_const_operand (op2_hi_lo[i], SImode))
37907 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
37908 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
37909 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
37910 rtx tmp = gen_reg_rtx (SImode);
37912 /* Make sure the constant is sign extended. */
37913 if ((hi_16bits & sign_bit) != 0)
37914 hi_16bits |= upper_32bits;
37916 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
37917 code, SImode, false, false, false);
37919 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
37920 code, SImode, false, false, false);
37922 else
37923 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
37924 code, SImode, complement_final_p,
37925 complement_op1_p, complement_op2_p);
37928 return;
37931 /* Split the insns that make up boolean operations operating on multiple GPR
37932 registers. The boolean MD patterns ensure that the inputs either are
37933 exactly the same as the output registers, or there is no overlap.
37935 OPERANDS is an array containing the destination and two input operands.
37936 CODE is the base operation (AND, IOR, XOR, NOT).
37937 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
37938 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
37939 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
37941 void
37942 rs6000_split_logical (rtx operands[3],
37943 enum rtx_code code,
37944 bool complement_final_p,
37945 bool complement_op1_p,
37946 bool complement_op2_p)
37948 machine_mode mode = GET_MODE (operands[0]);
37949 machine_mode sub_mode;
37950 rtx op0, op1, op2;
37951 int sub_size, regno0, regno1, nregs, i;
37953 /* If this is DImode, use the specialized version that can run before
37954 register allocation. */
37955 if (mode == DImode && !TARGET_POWERPC64)
37957 rs6000_split_logical_di (operands, code, complement_final_p,
37958 complement_op1_p, complement_op2_p);
37959 return;
37962 op0 = operands[0];
37963 op1 = operands[1];
37964 op2 = (code == NOT) ? NULL_RTX : operands[2];
37965 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
37966 sub_size = GET_MODE_SIZE (sub_mode);
37967 regno0 = REGNO (op0);
37968 regno1 = REGNO (op1);
37970 gcc_assert (reload_completed);
37971 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
37972 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
37974 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
37975 gcc_assert (nregs > 1);
37977 if (op2 && REG_P (op2))
37978 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
37980 for (i = 0; i < nregs; i++)
37982 int offset = i * sub_size;
37983 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
37984 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
37985 rtx sub_op2 = ((code == NOT)
37986 ? NULL_RTX
37987 : simplify_subreg (sub_mode, op2, mode, offset));
37989 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
37990 complement_final_p, complement_op1_p,
37991 complement_op2_p);
37994 return;
37998 /* Return true if the peephole2 can combine a load involving a combination of
37999 an addis instruction and a load with an offset that can be fused together on
38000 a power8. */
38002 bool
38003 fusion_gpr_load_p (rtx addis_reg, /* register set via addis. */
38004 rtx addis_value, /* addis value. */
38005 rtx target, /* target register that is loaded. */
38006 rtx mem) /* bottom part of the memory addr. */
38008 rtx addr;
38009 rtx base_reg;
38011 /* Validate arguments. */
38012 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
38013 return false;
38015 if (!base_reg_operand (target, GET_MODE (target)))
38016 return false;
38018 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
38019 return false;
38021 /* Allow sign/zero extension. */
38022 if (GET_CODE (mem) == ZERO_EXTEND
38023 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
38024 mem = XEXP (mem, 0);
38026 if (!MEM_P (mem))
38027 return false;
38029 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
38030 return false;
38032 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
38033 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
38034 return false;
38036 /* Validate that the register used to load the high value is either the
38037 register being loaded, or we can safely replace its use.
38039 This function is only called from the peephole2 pass and we assume that
38040 there are 2 instructions in the peephole (addis and load), so we want to
38041 check if the target register was not used in the memory address and the
38042 register to hold the addis result is dead after the peephole. */
38043 if (REGNO (addis_reg) != REGNO (target))
38045 if (reg_mentioned_p (target, mem))
38046 return false;
38048 if (!peep2_reg_dead_p (2, addis_reg))
38049 return false;
38051 /* If the target register being loaded is the stack pointer, we must
38052 avoid loading any other value into it, even temporarily. */
38053 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
38054 return false;
38057 base_reg = XEXP (addr, 0);
38058 return REGNO (addis_reg) == REGNO (base_reg);
38061 /* During the peephole2 pass, adjust and expand the insns for a load fusion
38062 sequence. We adjust the addis register to use the target register. If the
38063 load sign extends, we adjust the code to do the zero extending load, and an
38064 explicit sign extension later since the fusion only covers zero extending
38065 loads.
38067 The operands are:
38068 operands[0] register set with addis (to be replaced with target)
38069 operands[1] value set via addis
38070 operands[2] target register being loaded
38071 operands[3] D-form memory reference using operands[0]. */
38073 void
38074 expand_fusion_gpr_load (rtx *operands)
38076 rtx addis_value = operands[1];
38077 rtx target = operands[2];
38078 rtx orig_mem = operands[3];
38079 rtx new_addr, new_mem, orig_addr, offset;
38080 enum rtx_code plus_or_lo_sum;
38081 machine_mode target_mode = GET_MODE (target);
38082 machine_mode extend_mode = target_mode;
38083 machine_mode ptr_mode = Pmode;
38084 enum rtx_code extend = UNKNOWN;
38086 if (GET_CODE (orig_mem) == ZERO_EXTEND
38087 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
38089 extend = GET_CODE (orig_mem);
38090 orig_mem = XEXP (orig_mem, 0);
38091 target_mode = GET_MODE (orig_mem);
38094 gcc_assert (MEM_P (orig_mem));
38096 orig_addr = XEXP (orig_mem, 0);
38097 plus_or_lo_sum = GET_CODE (orig_addr);
38098 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
38100 offset = XEXP (orig_addr, 1);
38101 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
38102 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
38104 if (extend != UNKNOWN)
38105 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
38107 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
38108 UNSPEC_FUSION_GPR);
38109 emit_insn (gen_rtx_SET (target, new_mem));
38111 if (extend == SIGN_EXTEND)
38113 int sub_off = ((BYTES_BIG_ENDIAN)
38114 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
38115 : 0);
38116 rtx sign_reg
38117 = simplify_subreg (target_mode, target, extend_mode, sub_off);
38119 emit_insn (gen_rtx_SET (target,
38120 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
38123 return;
38126 /* Emit the addis instruction that will be part of a fused instruction
38127 sequence. */
38129 void
38130 emit_fusion_addis (rtx target, rtx addis_value, const char *comment,
38131 const char *mode_name)
38133 rtx fuse_ops[10];
38134 char insn_template[80];
38135 const char *addis_str = NULL;
38136 const char *comment_str = ASM_COMMENT_START;
38138 if (*comment_str == ' ')
38139 comment_str++;
38141 /* Emit the addis instruction. */
38142 fuse_ops[0] = target;
38143 if (satisfies_constraint_L (addis_value))
38145 fuse_ops[1] = addis_value;
38146 addis_str = "lis %0,%v1";
38149 else if (GET_CODE (addis_value) == PLUS)
38151 rtx op0 = XEXP (addis_value, 0);
38152 rtx op1 = XEXP (addis_value, 1);
38154 if (REG_P (op0) && CONST_INT_P (op1)
38155 && satisfies_constraint_L (op1))
38157 fuse_ops[1] = op0;
38158 fuse_ops[2] = op1;
38159 addis_str = "addis %0,%1,%v2";
38163 else if (GET_CODE (addis_value) == HIGH)
38165 rtx value = XEXP (addis_value, 0);
38166 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
38168 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
38169 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
38170 if (TARGET_ELF)
38171 addis_str = "addis %0,%2,%1@toc@ha";
38173 else if (TARGET_XCOFF)
38174 addis_str = "addis %0,%1@u(%2)";
38176 else
38177 gcc_unreachable ();
38180 else if (GET_CODE (value) == PLUS)
38182 rtx op0 = XEXP (value, 0);
38183 rtx op1 = XEXP (value, 1);
38185 if (GET_CODE (op0) == UNSPEC
38186 && XINT (op0, 1) == UNSPEC_TOCREL
38187 && CONST_INT_P (op1))
38189 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
38190 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
38191 fuse_ops[3] = op1;
38192 if (TARGET_ELF)
38193 addis_str = "addis %0,%2,%1+%3@toc@ha";
38195 else if (TARGET_XCOFF)
38196 addis_str = "addis %0,%1+%3@u(%2)";
38198 else
38199 gcc_unreachable ();
38203 else if (satisfies_constraint_L (value))
38205 fuse_ops[1] = value;
38206 addis_str = "lis %0,%v1";
38209 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
38211 fuse_ops[1] = value;
38212 addis_str = "lis %0,%1@ha";
38216 if (!addis_str)
38217 fatal_insn ("Could not generate addis value for fusion", addis_value);
38219 sprintf (insn_template, "%s\t\t%s %s, type %s", addis_str, comment_str,
38220 comment, mode_name);
38221 output_asm_insn (insn_template, fuse_ops);
38224 /* Emit a D-form load or store instruction that is the second instruction
38225 of a fusion sequence. */
38227 void
38228 emit_fusion_load_store (rtx load_store_reg, rtx addis_reg, rtx offset,
38229 const char *insn_str)
38231 rtx fuse_ops[10];
38232 char insn_template[80];
38234 fuse_ops[0] = load_store_reg;
38235 fuse_ops[1] = addis_reg;
38237 if (CONST_INT_P (offset) && satisfies_constraint_I (offset))
38239 sprintf (insn_template, "%s %%0,%%2(%%1)", insn_str);
38240 fuse_ops[2] = offset;
38241 output_asm_insn (insn_template, fuse_ops);
38244 else if (GET_CODE (offset) == UNSPEC
38245 && XINT (offset, 1) == UNSPEC_TOCREL)
38247 if (TARGET_ELF)
38248 sprintf (insn_template, "%s %%0,%%2@toc@l(%%1)", insn_str);
38250 else if (TARGET_XCOFF)
38251 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
38253 else
38254 gcc_unreachable ();
38256 fuse_ops[2] = XVECEXP (offset, 0, 0);
38257 output_asm_insn (insn_template, fuse_ops);
38260 else if (GET_CODE (offset) == PLUS
38261 && GET_CODE (XEXP (offset, 0)) == UNSPEC
38262 && XINT (XEXP (offset, 0), 1) == UNSPEC_TOCREL
38263 && CONST_INT_P (XEXP (offset, 1)))
38265 rtx tocrel_unspec = XEXP (offset, 0);
38266 if (TARGET_ELF)
38267 sprintf (insn_template, "%s %%0,%%2+%%3@toc@l(%%1)", insn_str);
38269 else if (TARGET_XCOFF)
38270 sprintf (insn_template, "%s %%0,%%2+%%3@l(%%1)", insn_str);
38272 else
38273 gcc_unreachable ();
38275 fuse_ops[2] = XVECEXP (tocrel_unspec, 0, 0);
38276 fuse_ops[3] = XEXP (offset, 1);
38277 output_asm_insn (insn_template, fuse_ops);
38280 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (offset))
38282 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
38284 fuse_ops[2] = offset;
38285 output_asm_insn (insn_template, fuse_ops);
38288 else
38289 fatal_insn ("Unable to generate load/store offset for fusion", offset);
38291 return;
38294 /* Wrap a TOC address that can be fused to indicate that special fusion
38295 processing is needed. */
38298 fusion_wrap_memory_address (rtx old_mem)
38300 rtx old_addr = XEXP (old_mem, 0);
38301 rtvec v = gen_rtvec (1, old_addr);
38302 rtx new_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_FUSION_ADDIS);
38303 return replace_equiv_address_nv (old_mem, new_addr, false);
38306 /* Given an address, convert it into the addis and load offset parts. Addresses
38307 created during the peephole2 process look like:
38308 (lo_sum (high (unspec [(sym)] UNSPEC_TOCREL))
38309 (unspec [(...)] UNSPEC_TOCREL))
38311 Addresses created via toc fusion look like:
38312 (unspec [(unspec [(...)] UNSPEC_TOCREL)] UNSPEC_FUSION_ADDIS)) */
38314 static void
38315 fusion_split_address (rtx addr, rtx *p_hi, rtx *p_lo)
38317 rtx hi, lo;
38319 if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_FUSION_ADDIS)
38321 lo = XVECEXP (addr, 0, 0);
38322 hi = gen_rtx_HIGH (Pmode, lo);
38324 else if (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
38326 hi = XEXP (addr, 0);
38327 lo = XEXP (addr, 1);
38329 else
38330 gcc_unreachable ();
38332 *p_hi = hi;
38333 *p_lo = lo;
38336 /* Return a string to fuse an addis instruction with a gpr load to the same
38337 register that we loaded up the addis instruction. The address that is used
38338 is the logical address that was formed during peephole2:
38339 (lo_sum (high) (low-part))
38341 Or the address is the TOC address that is wrapped before register allocation:
38342 (unspec [(addr) (toc-reg)] UNSPEC_FUSION_ADDIS)
38344 The code is complicated, so we call output_asm_insn directly, and just
38345 return "". */
38347 const char *
38348 emit_fusion_gpr_load (rtx target, rtx mem)
38350 rtx addis_value;
38351 rtx addr;
38352 rtx load_offset;
38353 const char *load_str = NULL;
38354 const char *mode_name = NULL;
38355 machine_mode mode;
38357 if (GET_CODE (mem) == ZERO_EXTEND)
38358 mem = XEXP (mem, 0);
38360 gcc_assert (REG_P (target) && MEM_P (mem));
38362 addr = XEXP (mem, 0);
38363 fusion_split_address (addr, &addis_value, &load_offset);
38365 /* Now emit the load instruction to the same register. */
38366 mode = GET_MODE (mem);
38367 switch (mode)
38369 case E_QImode:
38370 mode_name = "char";
38371 load_str = "lbz";
38372 break;
38374 case E_HImode:
38375 mode_name = "short";
38376 load_str = "lhz";
38377 break;
38379 case E_SImode:
38380 case E_SFmode:
38381 mode_name = (mode == SFmode) ? "float" : "int";
38382 load_str = "lwz";
38383 break;
38385 case E_DImode:
38386 case E_DFmode:
38387 gcc_assert (TARGET_POWERPC64);
38388 mode_name = (mode == DFmode) ? "double" : "long";
38389 load_str = "ld";
38390 break;
38392 default:
38393 fatal_insn ("Bad GPR fusion", gen_rtx_SET (target, mem));
38396 /* Emit the addis instruction. */
38397 emit_fusion_addis (target, addis_value, "gpr load fusion", mode_name);
38399 /* Emit the D-form load instruction. */
38400 emit_fusion_load_store (target, target, load_offset, load_str);
38402 return "";
38406 /* Return true if the peephole2 can combine a load/store involving a
38407 combination of an addis instruction and the memory operation. This was
38408 added to the ISA 3.0 (power9) hardware. */
38410 bool
38411 fusion_p9_p (rtx addis_reg, /* register set via addis. */
38412 rtx addis_value, /* addis value. */
38413 rtx dest, /* destination (memory or register). */
38414 rtx src) /* source (register or memory). */
38416 rtx addr, mem, offset;
38417 machine_mode mode = GET_MODE (src);
38419 /* Validate arguments. */
38420 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
38421 return false;
38423 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
38424 return false;
38426 /* Ignore extend operations that are part of the load. */
38427 if (GET_CODE (src) == FLOAT_EXTEND || GET_CODE (src) == ZERO_EXTEND)
38428 src = XEXP (src, 0);
38430 /* Test for memory<-register or register<-memory. */
38431 if (fpr_reg_operand (src, mode) || int_reg_operand (src, mode))
38433 if (!MEM_P (dest))
38434 return false;
38436 mem = dest;
38439 else if (MEM_P (src))
38441 if (!fpr_reg_operand (dest, mode) && !int_reg_operand (dest, mode))
38442 return false;
38444 mem = src;
38447 else
38448 return false;
38450 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
38451 if (GET_CODE (addr) == PLUS)
38453 if (!rtx_equal_p (addis_reg, XEXP (addr, 0)))
38454 return false;
38456 return satisfies_constraint_I (XEXP (addr, 1));
38459 else if (GET_CODE (addr) == LO_SUM)
38461 if (!rtx_equal_p (addis_reg, XEXP (addr, 0)))
38462 return false;
38464 offset = XEXP (addr, 1);
38465 if (TARGET_XCOFF || (TARGET_ELF && TARGET_POWERPC64))
38466 return small_toc_ref (offset, GET_MODE (offset));
38468 else if (TARGET_ELF && !TARGET_POWERPC64)
38469 return CONSTANT_P (offset);
38472 return false;
38475 /* During the peephole2 pass, adjust and expand the insns for an extended fusion
38476 load sequence.
38478 The operands are:
38479 operands[0] register set with addis
38480 operands[1] value set via addis
38481 operands[2] target register being loaded
38482 operands[3] D-form memory reference using operands[0].
38484 This is similar to the fusion introduced with power8, except it scales to
38485 both loads/stores and does not require the result register to be the same as
38486 the base register. At the moment, we only do this if register set with addis
38487 is dead. */
38489 void
38490 expand_fusion_p9_load (rtx *operands)
38492 rtx tmp_reg = operands[0];
38493 rtx addis_value = operands[1];
38494 rtx target = operands[2];
38495 rtx orig_mem = operands[3];
38496 rtx new_addr, new_mem, orig_addr, offset, set, clobber, insn;
38497 enum rtx_code plus_or_lo_sum;
38498 machine_mode target_mode = GET_MODE (target);
38499 machine_mode extend_mode = target_mode;
38500 machine_mode ptr_mode = Pmode;
38501 enum rtx_code extend = UNKNOWN;
38503 if (GET_CODE (orig_mem) == FLOAT_EXTEND || GET_CODE (orig_mem) == ZERO_EXTEND)
38505 extend = GET_CODE (orig_mem);
38506 orig_mem = XEXP (orig_mem, 0);
38507 target_mode = GET_MODE (orig_mem);
38510 gcc_assert (MEM_P (orig_mem));
38512 orig_addr = XEXP (orig_mem, 0);
38513 plus_or_lo_sum = GET_CODE (orig_addr);
38514 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
38516 offset = XEXP (orig_addr, 1);
38517 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
38518 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
38520 if (extend != UNKNOWN)
38521 new_mem = gen_rtx_fmt_e (extend, extend_mode, new_mem);
38523 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
38524 UNSPEC_FUSION_P9);
38526 set = gen_rtx_SET (target, new_mem);
38527 clobber = gen_rtx_CLOBBER (VOIDmode, tmp_reg);
38528 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber));
38529 emit_insn (insn);
38531 return;
38534 /* During the peephole2 pass, adjust and expand the insns for an extended fusion
38535 store sequence.
38537 The operands are:
38538 operands[0] register set with addis
38539 operands[1] value set via addis
38540 operands[2] target D-form memory being stored to
38541 operands[3] register being stored
38543 This is similar to the fusion introduced with power8, except it scales to
38544 both loads/stores and does not require the result register to be the same as
38545 the base register. At the moment, we only do this if register set with addis
38546 is dead. */
38548 void
38549 expand_fusion_p9_store (rtx *operands)
38551 rtx tmp_reg = operands[0];
38552 rtx addis_value = operands[1];
38553 rtx orig_mem = operands[2];
38554 rtx src = operands[3];
38555 rtx new_addr, new_mem, orig_addr, offset, set, clobber, insn, new_src;
38556 enum rtx_code plus_or_lo_sum;
38557 machine_mode target_mode = GET_MODE (orig_mem);
38558 machine_mode ptr_mode = Pmode;
38560 gcc_assert (MEM_P (orig_mem));
38562 orig_addr = XEXP (orig_mem, 0);
38563 plus_or_lo_sum = GET_CODE (orig_addr);
38564 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
38566 offset = XEXP (orig_addr, 1);
38567 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
38568 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
38570 new_src = gen_rtx_UNSPEC (target_mode, gen_rtvec (1, src),
38571 UNSPEC_FUSION_P9);
38573 set = gen_rtx_SET (new_mem, new_src);
38574 clobber = gen_rtx_CLOBBER (VOIDmode, tmp_reg);
38575 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber));
38576 emit_insn (insn);
38578 return;
38581 /* Return a string to fuse an addis instruction with a load using extended
38582 fusion. The address that is used is the logical address that was formed
38583 during peephole2: (lo_sum (high) (low-part))
38585 The code is complicated, so we call output_asm_insn directly, and just
38586 return "". */
38588 const char *
38589 emit_fusion_p9_load (rtx reg, rtx mem, rtx tmp_reg)
38591 machine_mode mode = GET_MODE (reg);
38592 rtx hi;
38593 rtx lo;
38594 rtx addr;
38595 const char *load_string;
38596 int r;
38598 if (GET_CODE (mem) == FLOAT_EXTEND || GET_CODE (mem) == ZERO_EXTEND)
38600 mem = XEXP (mem, 0);
38601 mode = GET_MODE (mem);
38604 if (GET_CODE (reg) == SUBREG)
38606 gcc_assert (SUBREG_BYTE (reg) == 0);
38607 reg = SUBREG_REG (reg);
38610 if (!REG_P (reg))
38611 fatal_insn ("emit_fusion_p9_load, bad reg #1", reg);
38613 r = REGNO (reg);
38614 if (FP_REGNO_P (r))
38616 if (mode == SFmode)
38617 load_string = "lfs";
38618 else if (mode == DFmode || mode == DImode)
38619 load_string = "lfd";
38620 else
38621 gcc_unreachable ();
38623 else if (ALTIVEC_REGNO_P (r) && TARGET_P9_VECTOR)
38625 if (mode == SFmode)
38626 load_string = "lxssp";
38627 else if (mode == DFmode || mode == DImode)
38628 load_string = "lxsd";
38629 else
38630 gcc_unreachable ();
38632 else if (INT_REGNO_P (r))
38634 switch (mode)
38636 case E_QImode:
38637 load_string = "lbz";
38638 break;
38639 case E_HImode:
38640 load_string = "lhz";
38641 break;
38642 case E_SImode:
38643 case E_SFmode:
38644 load_string = "lwz";
38645 break;
38646 case E_DImode:
38647 case E_DFmode:
38648 if (!TARGET_POWERPC64)
38649 gcc_unreachable ();
38650 load_string = "ld";
38651 break;
38652 default:
38653 gcc_unreachable ();
38656 else
38657 fatal_insn ("emit_fusion_p9_load, bad reg #2", reg);
38659 if (!MEM_P (mem))
38660 fatal_insn ("emit_fusion_p9_load not MEM", mem);
38662 addr = XEXP (mem, 0);
38663 fusion_split_address (addr, &hi, &lo);
38665 /* Emit the addis instruction. */
38666 emit_fusion_addis (tmp_reg, hi, "power9 load fusion", GET_MODE_NAME (mode));
38668 /* Emit the D-form load instruction. */
38669 emit_fusion_load_store (reg, tmp_reg, lo, load_string);
38671 return "";
38674 /* Return a string to fuse an addis instruction with a store using extended
38675 fusion. The address that is used is the logical address that was formed
38676 during peephole2: (lo_sum (high) (low-part))
38678 The code is complicated, so we call output_asm_insn directly, and just
38679 return "". */
38681 const char *
38682 emit_fusion_p9_store (rtx mem, rtx reg, rtx tmp_reg)
38684 machine_mode mode = GET_MODE (reg);
38685 rtx hi;
38686 rtx lo;
38687 rtx addr;
38688 const char *store_string;
38689 int r;
38691 if (GET_CODE (reg) == SUBREG)
38693 gcc_assert (SUBREG_BYTE (reg) == 0);
38694 reg = SUBREG_REG (reg);
38697 if (!REG_P (reg))
38698 fatal_insn ("emit_fusion_p9_store, bad reg #1", reg);
38700 r = REGNO (reg);
38701 if (FP_REGNO_P (r))
38703 if (mode == SFmode)
38704 store_string = "stfs";
38705 else if (mode == DFmode)
38706 store_string = "stfd";
38707 else
38708 gcc_unreachable ();
38710 else if (ALTIVEC_REGNO_P (r) && TARGET_P9_VECTOR)
38712 if (mode == SFmode)
38713 store_string = "stxssp";
38714 else if (mode == DFmode || mode == DImode)
38715 store_string = "stxsd";
38716 else
38717 gcc_unreachable ();
38719 else if (INT_REGNO_P (r))
38721 switch (mode)
38723 case E_QImode:
38724 store_string = "stb";
38725 break;
38726 case E_HImode:
38727 store_string = "sth";
38728 break;
38729 case E_SImode:
38730 case E_SFmode:
38731 store_string = "stw";
38732 break;
38733 case E_DImode:
38734 case E_DFmode:
38735 if (!TARGET_POWERPC64)
38736 gcc_unreachable ();
38737 store_string = "std";
38738 break;
38739 default:
38740 gcc_unreachable ();
38743 else
38744 fatal_insn ("emit_fusion_p9_store, bad reg #2", reg);
38746 if (!MEM_P (mem))
38747 fatal_insn ("emit_fusion_p9_store not MEM", mem);
38749 addr = XEXP (mem, 0);
38750 fusion_split_address (addr, &hi, &lo);
38752 /* Emit the addis instruction. */
38753 emit_fusion_addis (tmp_reg, hi, "power9 store fusion", GET_MODE_NAME (mode));
38755 /* Emit the D-form load instruction. */
38756 emit_fusion_load_store (reg, tmp_reg, lo, store_string);
38758 return "";
38761 #ifdef RS6000_GLIBC_ATOMIC_FENV
38762 /* Function declarations for rs6000_atomic_assign_expand_fenv. */
38763 static tree atomic_hold_decl, atomic_clear_decl, atomic_update_decl;
38764 #endif
38766 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
38768 static void
38769 rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
38771 if (!TARGET_HARD_FLOAT)
38773 #ifdef RS6000_GLIBC_ATOMIC_FENV
38774 if (atomic_hold_decl == NULL_TREE)
38776 atomic_hold_decl
38777 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
38778 get_identifier ("__atomic_feholdexcept"),
38779 build_function_type_list (void_type_node,
38780 double_ptr_type_node,
38781 NULL_TREE));
38782 TREE_PUBLIC (atomic_hold_decl) = 1;
38783 DECL_EXTERNAL (atomic_hold_decl) = 1;
38786 if (atomic_clear_decl == NULL_TREE)
38788 atomic_clear_decl
38789 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
38790 get_identifier ("__atomic_feclearexcept"),
38791 build_function_type_list (void_type_node,
38792 NULL_TREE));
38793 TREE_PUBLIC (atomic_clear_decl) = 1;
38794 DECL_EXTERNAL (atomic_clear_decl) = 1;
38797 tree const_double = build_qualified_type (double_type_node,
38798 TYPE_QUAL_CONST);
38799 tree const_double_ptr = build_pointer_type (const_double);
38800 if (atomic_update_decl == NULL_TREE)
38802 atomic_update_decl
38803 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
38804 get_identifier ("__atomic_feupdateenv"),
38805 build_function_type_list (void_type_node,
38806 const_double_ptr,
38807 NULL_TREE));
38808 TREE_PUBLIC (atomic_update_decl) = 1;
38809 DECL_EXTERNAL (atomic_update_decl) = 1;
38812 tree fenv_var = create_tmp_var_raw (double_type_node);
38813 TREE_ADDRESSABLE (fenv_var) = 1;
38814 tree fenv_addr = build1 (ADDR_EXPR, double_ptr_type_node, fenv_var);
38816 *hold = build_call_expr (atomic_hold_decl, 1, fenv_addr);
38817 *clear = build_call_expr (atomic_clear_decl, 0);
38818 *update = build_call_expr (atomic_update_decl, 1,
38819 fold_convert (const_double_ptr, fenv_addr));
38820 #endif
38821 return;
38824 tree mffs = rs6000_builtin_decls[RS6000_BUILTIN_MFFS];
38825 tree mtfsf = rs6000_builtin_decls[RS6000_BUILTIN_MTFSF];
38826 tree call_mffs = build_call_expr (mffs, 0);
38828 /* Generates the equivalent of feholdexcept (&fenv_var)
38830 *fenv_var = __builtin_mffs ();
38831 double fenv_hold;
38832 *(uint64_t*)&fenv_hold = *(uint64_t*)fenv_var & 0xffffffff00000007LL;
38833 __builtin_mtfsf (0xff, fenv_hold); */
38835 /* Mask to clear everything except for the rounding modes and non-IEEE
38836 arithmetic flag. */
38837 const unsigned HOST_WIDE_INT hold_exception_mask =
38838 HOST_WIDE_INT_C (0xffffffff00000007);
38840 tree fenv_var = create_tmp_var_raw (double_type_node);
38842 tree hold_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_var, call_mffs);
38844 tree fenv_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_var);
38845 tree fenv_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
38846 build_int_cst (uint64_type_node,
38847 hold_exception_mask));
38849 tree fenv_hold_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
38850 fenv_llu_and);
38852 tree hold_mtfsf = build_call_expr (mtfsf, 2,
38853 build_int_cst (unsigned_type_node, 0xff),
38854 fenv_hold_mtfsf);
38856 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_mffs, hold_mtfsf);
38858 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT):
38860 double fenv_clear = __builtin_mffs ();
38861 *(uint64_t)&fenv_clear &= 0xffffffff00000000LL;
38862 __builtin_mtfsf (0xff, fenv_clear); */
38864 /* Mask to clear everything except for the rounding modes and non-IEEE
38865 arithmetic flag. */
38866 const unsigned HOST_WIDE_INT clear_exception_mask =
38867 HOST_WIDE_INT_C (0xffffffff00000000);
38869 tree fenv_clear = create_tmp_var_raw (double_type_node);
38871 tree clear_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_clear, call_mffs);
38873 tree fenv_clean_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_clear);
38874 tree fenv_clear_llu_and = build2 (BIT_AND_EXPR, uint64_type_node,
38875 fenv_clean_llu,
38876 build_int_cst (uint64_type_node,
38877 clear_exception_mask));
38879 tree fenv_clear_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
38880 fenv_clear_llu_and);
38882 tree clear_mtfsf = build_call_expr (mtfsf, 2,
38883 build_int_cst (unsigned_type_node, 0xff),
38884 fenv_clear_mtfsf);
38886 *clear = build2 (COMPOUND_EXPR, void_type_node, clear_mffs, clear_mtfsf);
38888 /* Generates the equivalent of feupdateenv (&fenv_var)
38890 double old_fenv = __builtin_mffs ();
38891 double fenv_update;
38892 *(uint64_t*)&fenv_update = (*(uint64_t*)&old & 0xffffffff1fffff00LL) |
38893 (*(uint64_t*)fenv_var 0x1ff80fff);
38894 __builtin_mtfsf (0xff, fenv_update); */
38896 const unsigned HOST_WIDE_INT update_exception_mask =
38897 HOST_WIDE_INT_C (0xffffffff1fffff00);
38898 const unsigned HOST_WIDE_INT new_exception_mask =
38899 HOST_WIDE_INT_C (0x1ff80fff);
38901 tree old_fenv = create_tmp_var_raw (double_type_node);
38902 tree update_mffs = build2 (MODIFY_EXPR, void_type_node, old_fenv, call_mffs);
38904 tree old_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, old_fenv);
38905 tree old_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, old_llu,
38906 build_int_cst (uint64_type_node,
38907 update_exception_mask));
38909 tree new_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
38910 build_int_cst (uint64_type_node,
38911 new_exception_mask));
38913 tree new_llu_mask = build2 (BIT_IOR_EXPR, uint64_type_node,
38914 old_llu_and, new_llu_and);
38916 tree fenv_update_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
38917 new_llu_mask);
38919 tree update_mtfsf = build_call_expr (mtfsf, 2,
38920 build_int_cst (unsigned_type_node, 0xff),
38921 fenv_update_mtfsf);
38923 *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf);
38926 void
38927 rs6000_generate_float2_code (bool signed_convert, rtx dst, rtx src1, rtx src2)
38929 rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
38931 rtx_tmp0 = gen_reg_rtx (V2DImode);
38932 rtx_tmp1 = gen_reg_rtx (V2DImode);
38934 /* The destination of the vmrgew instruction layout is:
38935 rtx_tmp2[0] rtx_tmp3[0] rtx_tmp2[1] rtx_tmp3[0].
38936 Setup rtx_tmp0 and rtx_tmp1 to ensure the order of the elements after the
38937 vmrgew instruction will be correct. */
38938 if (VECTOR_ELT_ORDER_BIG)
38940 emit_insn (gen_vsx_xxpermdi_v2di_be (rtx_tmp0, src1, src2, GEN_INT (0)));
38941 emit_insn (gen_vsx_xxpermdi_v2di_be (rtx_tmp1, src1, src2, GEN_INT (3)));
38943 else
38945 emit_insn (gen_vsx_xxpermdi_v2di (rtx_tmp0, src1, src2, GEN_INT (3)));
38946 emit_insn (gen_vsx_xxpermdi_v2di (rtx_tmp1, src1, src2, GEN_INT (0)));
38949 rtx_tmp2 = gen_reg_rtx (V4SFmode);
38950 rtx_tmp3 = gen_reg_rtx (V4SFmode);
38952 if (signed_convert)
38954 emit_insn (gen_vsx_xvcvsxdsp (rtx_tmp2, rtx_tmp0));
38955 emit_insn (gen_vsx_xvcvsxdsp (rtx_tmp3, rtx_tmp1));
38957 else
38959 emit_insn (gen_vsx_xvcvuxdsp (rtx_tmp2, rtx_tmp0));
38960 emit_insn (gen_vsx_xvcvuxdsp (rtx_tmp3, rtx_tmp1));
38963 if (VECTOR_ELT_ORDER_BIG)
38964 emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp2, rtx_tmp3));
38965 else
38966 emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp3, rtx_tmp2));
38969 void
38970 rs6000_generate_vsigned2_code (bool signed_convert, rtx dst, rtx src1,
38971 rtx src2)
38973 rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
38975 rtx_tmp0 = gen_reg_rtx (V2DFmode);
38976 rtx_tmp1 = gen_reg_rtx (V2DFmode);
38978 emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp0, src1, src2, GEN_INT (0)));
38979 emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp1, src1, src2, GEN_INT (3)));
38981 rtx_tmp2 = gen_reg_rtx (V4SImode);
38982 rtx_tmp3 = gen_reg_rtx (V4SImode);
38984 if (signed_convert)
38986 emit_insn (gen_vsx_xvcvdpsxws (rtx_tmp2, rtx_tmp0));
38987 emit_insn (gen_vsx_xvcvdpsxws (rtx_tmp3, rtx_tmp1));
38989 else
38991 emit_insn (gen_vsx_xvcvdpuxws (rtx_tmp2, rtx_tmp0));
38992 emit_insn (gen_vsx_xvcvdpuxws (rtx_tmp3, rtx_tmp1));
38995 emit_insn (gen_p8_vmrgew_v4si (dst, rtx_tmp2, rtx_tmp3));
38998 /* Implement the TARGET_OPTAB_SUPPORTED_P hook. */
39000 static bool
39001 rs6000_optab_supported_p (int op, machine_mode mode1, machine_mode,
39002 optimization_type opt_type)
39004 switch (op)
39006 case rsqrt_optab:
39007 return (opt_type == OPTIMIZE_FOR_SPEED
39008 && RS6000_RECIP_AUTO_RSQRTE_P (mode1));
39010 default:
39011 return true;
39015 struct gcc_target targetm = TARGET_INITIALIZER;
39017 #include "gt-rs6000.h"